root/branches/libffado-2.0/src/motu/motu_avdevice.cpp

Revision 1385, 66.7 kB (checked in by ppalmers, 12 years ago)

Implement a mechanism to disable the samplerate and clock source controls while the device is streaming in order to avoid changes that could mess up jack. The saffire pro controls that cause a device reset to
happen are also disabled while streaming is active.

Line 
1 /*
2  * Copyright (C) 2005-2008 by Pieter Palmers
3  * Copyright (C) 2005-2008 by Jonathan Woithe
4  *
5  * This file is part of FFADO
6  * FFADO = Free Firewire (pro-)audio drivers for linux
7  *
8  * FFADO is based upon FreeBoB.
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 2 of the License, or
13  * (at your option) version 3 of the License.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #include "motu/motu_avdevice.h"
26
27 #include "libieee1394/configrom.h"
28 #include "libieee1394/ieee1394service.h"
29
30 #include "libavc/avc_definitions.h"
31
32 #include "debugmodule/debugmodule.h"
33
34 #include "libstreaming/motu/MotuReceiveStreamProcessor.h"
35 #include "libstreaming/motu/MotuTransmitStreamProcessor.h"
36 #include "libstreaming/motu/MotuPort.h"
37
38 #include "libutil/Time.h"
39 #include "libutil/Configuration.h"
40
41 #include "libcontrol/BasicElements.h"
42
43 #include <string>
44 #include <stdint.h>
45 #include <assert.h>
46 #include "libutil/ByteSwap.h"
47 #include <iostream>
48 #include <sstream>
49
50 #include <libraw1394/csr.h>
51
52 namespace Motu {
53
54 // Define the supported devices.  Device ordering is arbitary here.
55 static VendorModelEntry supportedDeviceList[] =
56 {
57 //  {vendor_id, model_id, unit_version, unit_specifier_id, model, vendor_name,model_name}
58     {FW_VENDORID_MOTU, 0, 0x00000003, 0x000001f2, MOTU_MODEL_828mkII, "MOTU", "828MkII"},
59     {FW_VENDORID_MOTU, 0, 0x00000009, 0x000001f2, MOTU_MODEL_TRAVELER, "MOTU", "Traveler"},
60     {FW_VENDORID_MOTU, 0, 0x0000000d, 0x000001f2, MOTU_MODEL_ULTRALITE, "MOTU", "UltraLite"},
61     {FW_VENDORID_MOTU, 0, 0x0000000f, 0x000001f2, MOTU_MODEL_8PRE, "MOTU", "8pre"},
62     {FW_VENDORID_MOTU, 0, 0x00000001, 0x000001f2, MOTU_MODEL_828MkI, "MOTU", "828MkI"},
63     {FW_VENDORID_MOTU, 0, 0x00000005, 0x000001f2, MOTU_MODEL_896HD, "MOTU", "896HD"},
64 };
65
66 // Ports declarations
67 const PortEntry Ports_828MKI[] =
68 {
69     {"Analog1", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 10},
70     {"Analog2", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 13},
71     {"Analog3", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 16},
72     {"Analog4", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 19},
73     {"Analog5", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 22},
74     {"Analog6", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 25},
75     {"Analog7", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 28},
76     {"Analog8", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 31},
77     {"SPDIF1", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 34},
78     {"SPDIF2", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 37},
79     {"ADAT1", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 40},
80     {"ADAT2", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 43},
81     {"ADAT3", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 46},
82     {"ADAT4", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 49},
83     {"ADAT5", MOTU_DIR_INOUT, MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 52},
84     {"ADAT6", MOTU_DIR_INOUT, MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 55},
85     {"ADAT7", MOTU_DIR_INOUT, MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 58},
86     {"ADAT8", MOTU_DIR_INOUT, MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 61},
87 };
88
89 const PortEntry Ports_896HD[] =
90 {
91     {"Mix-L", MOTU_DIR_IN, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 10},
92     {"Mix-R", MOTU_DIR_IN, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 13},
93     {"Phones-L", MOTU_DIR_OUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 10},
94     {"Phones-R", MOTU_DIR_OUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 13},
95     {"Analog1", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 16},
96     {"Analog1", MOTU_DIR_INOUT, MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 10},
97     {"Analog2", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 19},
98     {"Analog2", MOTU_DIR_INOUT, MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 13},
99     {"Analog3", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 22},
100     {"Analog3", MOTU_DIR_INOUT, MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 16},
101     {"Analog4", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 25},
102     {"Analog4", MOTU_DIR_INOUT, MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 19},
103     {"Analog5", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 28},
104     {"Analog5", MOTU_DIR_INOUT, MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 22},
105     {"Analog6", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 31},
106     {"Analog6", MOTU_DIR_INOUT, MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 25},
107     {"Analog7", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 34},
108     {"Analog7", MOTU_DIR_INOUT, MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 28},
109     {"Analog8", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 37},
110     {"Analog8", MOTU_DIR_INOUT, MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 31},
111     {"MainOut-L", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 40},
112     {"MainOut-R", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 43},
113     {"AES/EBU1", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 46},
114     {"AES/EBU2", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 49},
115     {"ADAT1", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 52},
116     {"ADAT2", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 55},
117     {"ADAT3", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 58},
118     {"ADAT4", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 61},
119     {"ADAT5", MOTU_DIR_INOUT, MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 64},
120     {"ADAT6", MOTU_DIR_INOUT, MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 67},
121     {"ADAT7", MOTU_DIR_INOUT, MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 70},
122     {"ADAT8", MOTU_DIR_INOUT, MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 73},
123 };
124
125 const PortEntry Ports_828MKII[] =
126 {
127     {"Main-L", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 40},
128     {"Main-R", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 43},
129     {"Mix-L", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 10},
130     {"Mix-R", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 13},
131     {"Analog1", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 16},
132     {"Analog2", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 19},
133     {"Analog3", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 22},
134     {"Analog4", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 25},
135     {"Analog5", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 28},
136     {"Analog6", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 31},
137     {"Analog7", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 34},
138     {"Analog8", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 37},
139     {"Phones-L", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 10},
140     {"Phones-R", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 13},
141     {"Mic1", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 40},
142     {"Mic2", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 43},
143     {"SPDIF1", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 46},
144     {"SPDIF2", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 49},
145     {"ADAT1", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 52},
146     {"ADAT2", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 55},
147     {"ADAT3", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 58},
148     {"ADAT4", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 61},
149     {"ADAT5", MOTU_DIR_INOUT, MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 64},
150     {"ADAT6", MOTU_DIR_INOUT, MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 67},
151     {"ADAT7", MOTU_DIR_INOUT, MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 70},
152     {"ADAT8", MOTU_DIR_INOUT, MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 73},
153 };
154
155 const PortEntry Ports_TRAVELER[] =
156 {
157     {"Mix-L", MOTU_DIR_IN, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 10},
158     {"Mix-R", MOTU_DIR_IN, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 13},
159     {"Phones-L", MOTU_DIR_OUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 10},
160     {"Phones-R", MOTU_DIR_OUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 13},
161     {"Analog1", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 16},
162     {"Analog1", MOTU_DIR_INOUT, MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 10},
163     {"Analog2", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 19},
164     {"Analog2", MOTU_DIR_INOUT, MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 13},
165     {"Analog3", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 22},
166     {"Analog3", MOTU_DIR_INOUT, MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 16},
167     {"Analog4", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 25},
168     {"Analog4", MOTU_DIR_INOUT, MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 19},
169     {"Analog5", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 28},
170     {"Analog5", MOTU_DIR_INOUT, MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 22},
171     {"Analog6", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 31},
172     {"Analog6", MOTU_DIR_INOUT, MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 25},
173     {"Analog7", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 34},
174     {"Analog7", MOTU_DIR_INOUT, MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 28},
175     {"Analog8", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 37},
176     {"Analog8", MOTU_DIR_INOUT, MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 31},
177     {"AES/EBU1", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 40},
178     {"AES/EBU2", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 43},
179     {"SPDIF1", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_OFF|MOTU_PA_OPTICAL_ADAT, 46},
180     {"SPDIF2", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_OFF|MOTU_PA_OPTICAL_ADAT, 49},
181     {"Toslink1", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_TOSLINK, 46},
182     {"Toslink2", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_TOSLINK, 49},
183     {"ADAT1", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 52},
184     {"ADAT2", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 55},
185     {"ADAT3", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 58},
186     {"ADAT4", MOTU_DIR_INOUT, MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 61},
187     {"ADAT5", MOTU_DIR_INOUT, MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 64},
188     {"ADAT6", MOTU_DIR_INOUT, MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 67},
189     {"ADAT7", MOTU_DIR_INOUT, MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 70},
190     {"ADAT8", MOTU_DIR_INOUT, MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 73},
191 };
192
193 const PortEntry Ports_ULTRALITE[] =
194 {
195     {"Main-L", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 46},
196     {"Main-R", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 49},
197     {"Padding1", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY|MOTU_PA_PADDING, 46},
198     {"Padding2", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY|MOTU_PA_PADDING, 49},
199     {"Mix-L", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 10},
200     {"Mix-R", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 13},
201     {"Mic1", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 16},
202     {"Mic2", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 19},
203     {"Analog1", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 16},
204     {"Analog2", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 19},
205     {"Analog3", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 22},
206     {"Analog4", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 25},
207     {"Analog5", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 28},
208     {"Analog6", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 31},
209     {"Analog7", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 34},
210     {"Analog8", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 37},
211     {"Phones-L", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 10},
212     {"Phones-R", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 13},
213     {"SPDIF1", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 40},
214     {"SPDIF2", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 43},
215 };
216
217 const PortEntry Ports_8PRE[] =
218 {
219     {"Analog1", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 16},
220     {"Analog2", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 19},
221     {"Analog3", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 22},
222     {"Analog4", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 25},
223     {"Analog5", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 28},
224     {"Analog6", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 31},
225     {"Analog7", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 34},
226     {"Analog8", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 37},
227     {"Mix-L", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 10},
228     {"Mix-R", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 13},
229     {"Main-L", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 16},
230     {"Main-R", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 19},
231     {"Phones-L", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 10},
232     {"Phones-R", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 13},
233     {"ADAT1", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 40},
234     {"ADAT1", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 22},
235     {"ADAT2", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 43},
236     {"ADAT2", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 25},
237     {"ADAT3", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 46},
238     {"ADAT3", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 28},
239     {"ADAT4", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 49},
240     {"ADAT4", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 31},
241     {"ADAT5", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 52},
242     {"ADAT5", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 34},
243     {"ADAT6", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 55},
244     {"ADAT6", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 37},
245     {"ADAT7", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 58},
246     {"ADAT7", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 40},
247     {"ADAT8", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 61},
248     {"ADAT8", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 43},
249 };
250
251 // Mixer registers
252 const MatrixMixBus MixerBuses_Traveler[] = {
253     {"Mix 1", 0x4000, },
254     {"Mix 2", 0x4100, },
255     {"Mix 3", 0x4200, },
256     {"Mix 4", 0x4300, },
257 };
258
259 const MatrixMixChannel MixerChannels_Traveler[] = {
260     {"Analog 1", MOTU_CTRL_STD_CHANNEL, 0x0000, },
261     {"Analog 2", MOTU_CTRL_STD_CHANNEL, 0x0004, },
262     {"Analog 3", MOTU_CTRL_STD_CHANNEL, 0x0008, },
263     {"Analog 4", MOTU_CTRL_STD_CHANNEL, 0x000c, },
264     {"Analog 5", MOTU_CTRL_STD_CHANNEL, 0x0010, },
265     {"Analog 6", MOTU_CTRL_STD_CHANNEL, 0x0014, },
266     {"Analog 7", MOTU_CTRL_STD_CHANNEL, 0x0018, },
267     {"Analog 8", MOTU_CTRL_STD_CHANNEL, 0x001c, },
268     {"AES/EBU 1", MOTU_CTRL_STD_CHANNEL, 0x0020, },
269     {"AES/EBU 2", MOTU_CTRL_STD_CHANNEL, 0x0024, },
270     {"SPDIF 1", MOTU_CTRL_STD_CHANNEL, 0x0028, },
271     {"SPDIF 2", MOTU_CTRL_STD_CHANNEL, 0x002c, },
272     {"ADAT 1", MOTU_CTRL_STD_CHANNEL, 0x0030, },
273     {"ADAT 2", MOTU_CTRL_STD_CHANNEL, 0x0034, },
274     {"ADAT 3", MOTU_CTRL_STD_CHANNEL, 0x0038, },
275     {"ADAT 4", MOTU_CTRL_STD_CHANNEL, 0x003c, },
276     {"ADAT 5", MOTU_CTRL_STD_CHANNEL, 0x0040, },
277     {"ADAT 6", MOTU_CTRL_STD_CHANNEL, 0x0044, },
278     {"ADAT 7", MOTU_CTRL_STD_CHANNEL, 0x0048, },
279     {"ADAT 8", MOTU_CTRL_STD_CHANNEL, 0x004c, },
280 };
281
282 const MixerCtrl MixerCtrls_Traveler[] = {
283     {"Mix1/Mix_", "Mix 1 ", "", MOTU_CTRL_STD_MIX, 0x0c20, },
284     {"Mix2/Mix_", "Mix 2 ", "", MOTU_CTRL_STD_MIX, 0x0c24, },
285     {"Mix3/Mix_", "Mix 3 ", "", MOTU_CTRL_STD_MIX, 0x0c28, },
286     {"Mix4/Mix_", "Mix 4 ", "", MOTU_CTRL_STD_MIX, 0x0c2c, },
287
288     /* For mic/line input controls, the "register" is the zero-based channel number */
289     {"Control/Ana1_", "Analog 1 input ", "", MOTU_CTRL_TRAVELER_MIC_INPUT_CTRLS, 0},
290     {"Control/Ana2_", "Analog 2 input ", "", MOTU_CTRL_TRAVELER_MIC_INPUT_CTRLS, 1},
291     {"Control/Ana3_", "Analog 3 input ", "", MOTU_CTRL_TRAVELER_MIC_INPUT_CTRLS, 2},
292     {"Control/Ana4_", "Analog 4 input ", "", MOTU_CTRL_TRAVELER_MIC_INPUT_CTRLS, 3},
293     {"Control/Ana5_", "Analog 5 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 4},
294     {"Control/Ana6_", "Analog 6 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 5},
295     {"Control/Ana7_", "Analog 7 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 6},
296     {"Control/Ana8_", "Analog 8 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 7},
297
298     /* For phones source control, "register" is currently unused */
299     {"Control/Phones_", "Phones source", "", MOTU_CTRL_PHONES_SRC, 0},
300
301     /* For optical mode controls, the "register" is used to indicate direction */
302     {"Control/OpticalIn_mode", "Optical input mode ", "", MOTU_CTRL_OPTICAL_MODE, MOTU_DIR_IN},
303     {"Control/OpticalOut_mode", "Optical output mode ", "", MOTU_CTRL_OPTICAL_MODE, MOTU_DIR_OUT},
304 };
305
306 const MatrixMixBus MixerBuses_Ultralite[] = {
307     {"Mix 1", 0x4000, },
308     {"Mix 2", 0x4100, },
309     {"Mix 3", 0x4200, },
310     {"Mix 4", 0x4300, },
311 };
312
313 const MatrixMixChannel MixerChannels_Ultralite[] = {
314     {"Analog 1", MOTU_CTRL_STD_CHANNEL, 0x0000, },
315     {"Analog 2", MOTU_CTRL_STD_CHANNEL, 0x0004, },
316     {"Analog 3", MOTU_CTRL_STD_CHANNEL, 0x0008, },
317     {"Analog 4", MOTU_CTRL_STD_CHANNEL, 0x000c, },
318     {"Analog 5", MOTU_CTRL_STD_CHANNEL, 0x0010, },
319     {"Analog 6", MOTU_CTRL_STD_CHANNEL, 0x0014, },
320     {"Analog 7", MOTU_CTRL_STD_CHANNEL, 0x0018, },
321     {"Analog 8", MOTU_CTRL_STD_CHANNEL, 0x001c, },
322     {"AES/EBU 1", MOTU_CTRL_STD_CHANNEL, MOTU_CTRL_NONE, },
323     {"AES/EBU 2", MOTU_CTRL_STD_CHANNEL, MOTU_CTRL_NONE, },
324     {"SPDIF 1", MOTU_CTRL_STD_CHANNEL, 0x0020, },
325     {"SPDIF 2", MOTU_CTRL_STD_CHANNEL, 0x0024, },
326     {"ADAT 1", MOTU_CTRL_STD_CHANNEL, MOTU_CTRL_NONE, },
327     {"ADAT 2", MOTU_CTRL_STD_CHANNEL, MOTU_CTRL_NONE, },
328     {"ADAT 3", MOTU_CTRL_STD_CHANNEL, MOTU_CTRL_NONE, },
329     {"ADAT 4", MOTU_CTRL_STD_CHANNEL, MOTU_CTRL_NONE, },
330     {"ADAT 5", MOTU_CTRL_STD_CHANNEL, MOTU_CTRL_NONE, },
331     {"ADAT 6", MOTU_CTRL_STD_CHANNEL, MOTU_CTRL_NONE, },
332     {"ADAT 7", MOTU_CTRL_STD_CHANNEL, MOTU_CTRL_NONE, },
333     {"ADAT 8", MOTU_CTRL_STD_CHANNEL, MOTU_CTRL_NONE, },
334 };
335
336 const MixerCtrl MixerCtrls_Ultralite[] = {
337     {"Mix1/Mix_", "Mix 1 ", "", MOTU_CTRL_STD_MIX, 0x0c20, },
338     {"Mix2/Mix_", "Mix 2 ", "", MOTU_CTRL_STD_MIX, 0x0c24, },
339     {"Mix3/Mix_", "Mix 3 ", "", MOTU_CTRL_STD_MIX, 0x0c28, },
340     {"Mix4/Mix_", "Mix 4 ", "", MOTU_CTRL_STD_MIX, 0x0c2c, },
341
342     /* For mic/line input controls, the "register" is the zero-based channel number */
343     {"Control/Ana1_", "Analog 1 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 0},
344     {"Control/Ana2_", "Analog 2 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 1},
345     {"Control/Ana3_", "Analog 3 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 2},
346     {"Control/Ana4_", "Analog 4 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 3},
347     {"Control/Ana5_", "Analog 5 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 4},
348     {"Control/Ana6_", "Analog 6 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 5},
349     {"Control/Ana7_", "Analog 7 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 6},
350     {"Control/Ana8_", "Analog 8 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 7},
351
352     /* For phones source control, "register" is currently unused */
353     {"Control/Phones_", "Phones source", "", MOTU_CTRL_PHONES_SRC, 0},
354
355     /* For optical mode controls, the "register" is used to indicate direction */
356     {"Control/OpticalIn_mode", "Optical input mode ", "", MOTU_CTRL_OPTICAL_MODE, MOTU_DIR_IN},
357     {"Control/OpticalOut_mode", "Optical output mode ", "", MOTU_CTRL_OPTICAL_MODE, MOTU_DIR_OUT},
358 };
359
360 const MixerCtrl MixerCtrls_896HD[] = {
361     {"Mix1/Mix_", "Mix 1 ", "", MOTU_CTRL_STD_MIX, 0x0c20, },
362     {"Mix2/Mix_", "Mix 2 ", "", MOTU_CTRL_STD_MIX, 0x0c24, },
363     {"Mix3/Mix_", "Mix 3 ", "", MOTU_CTRL_STD_MIX, 0x0c28, },
364     {"Mix4/Mix_", "Mix 4 ", "", MOTU_CTRL_STD_MIX, 0x0c2c, },
365
366     /* For phones source control, "register" is currently unused */
367     {"Control/Phones_", "Phones source", "", MOTU_CTRL_PHONES_SRC, 0},
368
369     /* For optical mode controls, the "register" is used to indicate direction */
370     {"Control/OpticalIn_mode", "Optical input mode ", "", MOTU_CTRL_OPTICAL_MODE, MOTU_DIR_IN},
371     {"Control/OpticalOut_mode", "Optical output mode ", "", MOTU_CTRL_OPTICAL_MODE, MOTU_DIR_OUT},
372
373     /* For meter controls the "register" indicates which meter controls are available */
374     {"Control/Meter_", "Meter ", "", MOTU_CTRL_METER,
375       MOTU_CTRL_METER_PEAKHOLD | MOTU_CTRL_METER_CLIPHOLD | MOTU_CTRL_METER_AESEBU_SRC |
376       MOTU_CTRL_METER_PROG_SRC},
377 };
378
379 const MixerCtrl MixerCtrls_828Mk2[] = {
380     {"Mix1/Mix_", "Mix 1 ", "", MOTU_CTRL_STD_MIX, 0x0c20, },
381     {"Mix2/Mix_", "Mix 2 ", "", MOTU_CTRL_STD_MIX, 0x0c24, },
382     {"Mix3/Mix_", "Mix 3 ", "", MOTU_CTRL_STD_MIX, 0x0c28, },
383     {"Mix4/Mix_", "Mix 4 ", "", MOTU_CTRL_STD_MIX, 0x0c2c, },
384
385     /* For mic/line input controls, the "register" is the zero-based channel number */
386     {"Control/Ana1_", "Analog 1 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 0},
387     {"Control/Ana2_", "Analog 2 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 1},
388     {"Control/Ana3_", "Analog 3 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 2},
389     {"Control/Ana4_", "Analog 4 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 3},
390     {"Control/Ana5_", "Analog 5 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 4},
391     {"Control/Ana6_", "Analog 6 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 5},
392     {"Control/Ana7_", "Analog 7 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 6},
393     {"Control/Ana8_", "Analog 8 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 7},
394
395     /* For phones source control, "register" is currently unused */
396     {"Control/Phones_", "Phones source", "", MOTU_CTRL_PHONES_SRC, 0},
397
398     /* For optical mode controls, the "register" is used to indicate direction */
399     {"Control/OpticalIn_mode", "Optical input mode ", "", MOTU_CTRL_OPTICAL_MODE, MOTU_DIR_IN},
400     {"Control/OpticalOut_mode", "Optical output mode ", "", MOTU_CTRL_OPTICAL_MODE, MOTU_DIR_OUT},
401 };
402
403 const MotuMixer Mixer_Traveler = MOTUMIXER(
404     MixerCtrls_Traveler, MixerBuses_Traveler, MixerChannels_Traveler);
405
406 const MotuMixer Mixer_Ultralite = MOTUMIXER(
407     MixerCtrls_Ultralite, MixerBuses_Ultralite, MixerChannels_Ultralite);
408
409 const MotuMixer Mixer_828Mk2 = MOTUMIXER(
410     MixerCtrls_828Mk2, MixerBuses_Traveler, MixerChannels_Traveler);
411
412 const MotuMixer Mixer_896HD = MOTUMIXER(
413     MixerCtrls_896HD, MixerBuses_Traveler, MixerChannels_Traveler);
414
415 /* The order of DevicesProperty entries must match the numeric order of the
416  * MOTU model enumeration (EMotuModel).
417  */
418 const DevicePropertyEntry DevicesProperty[] = {
419 //  { Ports_map,       N_ELEMENTS( Ports_map ),        MaxSR, MixerDescrPtr },
420     { Ports_828MKII,   N_ELEMENTS( Ports_828MKII ),    96000, &Mixer_828Mk2, },
421     { Ports_TRAVELER,  N_ELEMENTS( Ports_TRAVELER ),  192000, &Mixer_Traveler, },
422     { Ports_ULTRALITE, N_ELEMENTS( Ports_ULTRALITE ),  96000, &Mixer_Ultralite, },
423     { Ports_8PRE,      N_ELEMENTS( Ports_8PRE ),       96000 },
424     { Ports_828MKI,    N_ELEMENTS( Ports_828MKI ),     48000 },
425     { Ports_896HD,     N_ELEMENTS( Ports_896HD ),     192000, &Mixer_896HD, },
426 };
427
428 MotuDevice::MotuDevice( DeviceManager& d, std::auto_ptr<ConfigRom>( configRom ))
429     : FFADODevice( d, configRom )
430     , m_motu_model( MOTU_MODEL_NONE )
431     , m_iso_recv_channel ( -1 )
432     , m_iso_send_channel ( -1 )
433     , m_rx_bandwidth ( -1 )
434     , m_tx_bandwidth ( -1 )
435     , m_receiveProcessor ( 0 )
436     , m_transmitProcessor ( 0 )
437     , m_MixerContainer ( NULL )
438     , m_ControlContainer ( NULL )
439 {
440     debugOutput( DEBUG_LEVEL_VERBOSE, "Created Motu::MotuDevice (NodeID %d)\n",
441                  getConfigRom().getNodeId() );
442 }
443
444 MotuDevice::~MotuDevice()
445 {
446     delete m_receiveProcessor;
447     delete m_transmitProcessor;
448
449     // Free ieee1394 bus resources if they have been allocated
450     if (m_iso_recv_channel>=0 && !get1394Service().freeIsoChannel(m_iso_recv_channel)) {
451         debugOutput(DEBUG_LEVEL_VERBOSE, "Could not free recv iso channel %d\n", m_iso_recv_channel);
452     }
453     if (m_iso_send_channel>=0 && !get1394Service().freeIsoChannel(m_iso_send_channel)) {
454         debugOutput(DEBUG_LEVEL_VERBOSE, "Could not free send iso channel %d\n", m_iso_send_channel);
455     }
456
457     destroyMixer();
458 }
459
460 bool
461 MotuDevice::buildMixerAudioControls(void) {
462
463     bool result = true;
464     MotuMatrixMixer *fader_mmixer = NULL;
465     MotuMatrixMixer *pan_mmixer = NULL;
466     MotuMatrixMixer *solo_mmixer = NULL;
467     MotuMatrixMixer *mute_mmixer = NULL;
468     unsigned int bus, ch, i;
469
470     if (DevicesProperty[m_motu_model-1].mixer == NULL) {
471         debugOutput(DEBUG_LEVEL_WARNING, "No mixer controls defined for model %d\n", m_motu_model);
472         result = false;
473     }
474     const struct MatrixMixBus *buses = DevicesProperty[m_motu_model-1].mixer->mixer_buses;
475     if (buses == NULL) {
476         debugOutput(DEBUG_LEVEL_WARNING, "No buses defined for model %d\n", m_motu_model);
477         result = false;
478     }
479     const struct MatrixMixChannel *channels = DevicesProperty[m_motu_model-1].mixer->mixer_channels;
480     if (channels == NULL) {
481         debugOutput(DEBUG_LEVEL_WARNING, "No channels defined for model %d\n", m_motu_model);
482         result = false;
483     }
484     if (result == false) {
485         return true;
486     }
487
488     /* Create the top-level matrix mixers */
489     fader_mmixer = new ChannelFaderMatrixMixer(*this, "fader");
490     result &= m_MixerContainer->addElement(fader_mmixer);
491     pan_mmixer = new ChannelPanMatrixMixer(*this, "pan");
492     result &= m_MixerContainer->addElement(pan_mmixer);
493     solo_mmixer = new ChannelBinSwMatrixMixer(*this, "solo",
494         MOTU_CTRL_MASK_SOLO_VALUE, MOTU_CTRL_MASK_SOLO_SETENABLE);
495     result &= m_MixerContainer->addElement(solo_mmixer);
496     mute_mmixer = new ChannelBinSwMatrixMixer(*this, "mute",
497         MOTU_CTRL_MASK_MUTE_VALUE, MOTU_CTRL_MASK_MUTE_SETENABLE);
498     result &= m_MixerContainer->addElement(mute_mmixer);
499
500     for (bus=0; bus<DevicesProperty[m_motu_model-1].mixer->n_mixer_buses; bus++) {
501         fader_mmixer->addRowInfo(buses[bus].name, 0, buses[bus].address);
502         pan_mmixer->addRowInfo(buses[bus].name, 0, buses[bus].address);
503         solo_mmixer->addRowInfo(buses[bus].name, 0, buses[bus].address);
504         mute_mmixer->addRowInfo(buses[bus].name, 0, buses[bus].address);
505     }
506
507     for (ch=0; ch<DevicesProperty[m_motu_model-1].mixer->n_mixer_channels; ch++) {
508         uint32_t flags = channels[ch].flags;
509         if (flags & MOTU_CTRL_CHANNEL_FADER)
510             fader_mmixer->addColInfo(channels[ch].name, 0, channels[ch].addr_ofs);
511         if (flags & MOTU_CTRL_CHANNEL_PAN)
512             pan_mmixer->addColInfo(channels[ch].name, 0, channels[ch].addr_ofs);
513         if (flags & MOTU_CTRL_CHANNEL_SOLO)
514             solo_mmixer->addColInfo(channels[ch].name, 0, channels[ch].addr_ofs);
515         if (flags & MOTU_CTRL_CHANNEL_MUTE)
516             mute_mmixer->addColInfo(channels[ch].name, 0, channels[ch].addr_ofs);
517         flags &= ~(MOTU_CTRL_CHANNEL_FADER|MOTU_CTRL_CHANNEL_PAN|MOTU_CTRL_CHANNEL_SOLO|MOTU_CTRL_CHANNEL_MUTE);
518         if (flags) {
519             debugOutput(DEBUG_LEVEL_WARNING, "Control %s: unknown flag bits 0x%08x\n", channels[ch].name, flags);
520         }
521     }
522
523     // Single non-matrixed mixer controls get added here.  Channel controls are supported
524     // here, but usually these will be a part of a matrix mixer.
525     for (i=0; i<DevicesProperty[m_motu_model-1].mixer->n_mixer_ctrls; i++) {
526         const struct MixerCtrl *ctrl = &DevicesProperty[m_motu_model-1].mixer->mixer_ctrl[i];
527         unsigned int type;
528         char name[100];
529         char label[100];
530
531         if (ctrl == NULL) {
532             debugOutput(DEBUG_LEVEL_WARNING, "NULL control at index %d for model %d\n", i, m_motu_model);
533             continue;
534         }
535         type = ctrl->type;
536         if (type & MOTU_CTRL_CHANNEL_FADER) {
537             snprintf(name, 100, "%s%s", ctrl->name, "fader");
538             snprintf(label,100, "%s%s", ctrl->label,"fader");
539             result &= m_MixerContainer->addElement(
540                 new ChannelFader(*this, ctrl->dev_register, name, label, ctrl->desc));
541             type &= ~MOTU_CTRL_CHANNEL_FADER;
542         }
543         if (type & MOTU_CTRL_CHANNEL_PAN) {
544             snprintf(name, 100, "%s%s", ctrl->name, "pan");
545             snprintf(label,100, "%s%s", ctrl->label,"pan");
546             result &= m_MixerContainer->addElement(
547                 new ChannelPan(*this,
548                     ctrl->dev_register,
549                     name, label,
550                     ctrl->desc));
551             type &= ~MOTU_CTRL_CHANNEL_PAN;
552         }
553         if (type & MOTU_CTRL_CHANNEL_MUTE) {
554             snprintf(name, 100, "%s%s", ctrl->name, "mute");
555             snprintf(label,100, "%s%s", ctrl->label,"mute");
556             result &= m_MixerContainer->addElement(
557                 new MotuBinarySwitch(*this, ctrl->dev_register,
558                     MOTU_CTRL_MASK_MUTE_VALUE, MOTU_CTRL_MASK_MUTE_SETENABLE,
559                     name, label, ctrl->desc));
560             type &= ~MOTU_CTRL_CHANNEL_MUTE;
561         }
562         if (type & MOTU_CTRL_CHANNEL_SOLO) {
563             snprintf(name, 100, "%s%s", ctrl->name, "solo");
564             snprintf(label,100, "%s%s", ctrl->label,"solo");
565             result &= m_MixerContainer->addElement(
566                 new MotuBinarySwitch(*this, ctrl->dev_register,
567                     MOTU_CTRL_MASK_SOLO_VALUE, MOTU_CTRL_MASK_SOLO_SETENABLE,
568                     name, label, ctrl->desc));
569             type &= ~MOTU_CTRL_CHANNEL_SOLO;
570         }
571
572         if (type & MOTU_CTRL_MIX_FADER) {
573             snprintf(name, 100, "%s%s", ctrl->name, "fader");
574             snprintf(label,100, "%s%s", ctrl->label,"fader");
575             result &= m_MixerContainer->addElement(
576                 new MixFader(*this, ctrl->dev_register, name, label, ctrl->desc));
577             type &= ~MOTU_CTRL_MIX_FADER;
578         }
579         if (type & MOTU_CTRL_MIX_MUTE) {
580             snprintf(name, 100, "%s%s", ctrl->name, "mute");
581             snprintf(label,100, "%s%s", ctrl->label,"mute");
582             result &= m_MixerContainer->addElement(
583                 new MixMute(*this, ctrl->dev_register, name, label, ctrl->desc));
584             type &= ~MOTU_CTRL_MIX_MUTE;
585         }
586         if (type & MOTU_CTRL_MIX_DEST) {
587             snprintf(name, 100, "%s%s", ctrl->name, "dest");
588             snprintf(label,100, "%s%s", ctrl->label,"dest");
589             result &= m_MixerContainer->addElement(
590                 new MixDest(*this, ctrl->dev_register, name, label, ctrl->desc));
591             type &= ~MOTU_CTRL_MIX_DEST;
592         }
593
594         if (type & MOTU_CTRL_INPUT_TRIMGAIN) {
595             snprintf(name, 100, "%s%s", ctrl->name, "trimgain");
596             snprintf(label,100, "%s%s", ctrl->label,"trimgain");
597             result &= m_MixerContainer->addElement(
598                 new InputGainPad(*this, ctrl->dev_register, MOTU_CTRL_MODE_TRIMGAIN,
599                     name, label, ctrl->desc));
600             type &= ~MOTU_CTRL_INPUT_TRIMGAIN;
601         }
602         if (type & MOTU_CTRL_INPUT_PAD) {
603             snprintf(name, 100, "%s%s", ctrl->name, "pad");
604             snprintf(label,100, "%s%s", ctrl->label,"pad");
605             result &= m_MixerContainer->addElement(
606                 new InputGainPad(*this, ctrl->dev_register, MOTU_CTRL_MODE_PAD,
607                     name, label, ctrl->desc));
608             type &= ~MOTU_CTRL_INPUT_PAD;
609         }
610
611         if (type & MOTU_CTRL_INPUT_LEVEL) {
612             snprintf(name, 100, "%s%s", ctrl->name, "level");
613             snprintf(label,100, "%s%s", ctrl->label,"level");
614             result &= m_MixerContainer->addElement(
615                 new MotuBinarySwitch(*this, MOTU_REG_INPUT_LEVEL,
616                     1<<ctrl->dev_register, 0, name, label, ctrl->desc));
617             type &= ~MOTU_CTRL_INPUT_LEVEL;
618         }
619         if (type & MOTU_CTRL_INPUT_BOOST) {
620             snprintf(name, 100, "%s%s", ctrl->name, "boost");
621             snprintf(label,100, "%s%s", ctrl->label,"boost");
622             result &= m_MixerContainer->addElement(
623                 new MotuBinarySwitch(*this, MOTU_REG_INPUT_BOOST,
624                     1<<ctrl->dev_register, 0, name, label, ctrl->desc));
625             type &= ~MOTU_CTRL_INPUT_BOOST;
626         }
627         if (type & MOTU_CTRL_PHONES_SRC) {
628             snprintf(name, 100, "%s%s", ctrl->name, "src");
629             snprintf(label,100, "%s%s", ctrl->label,"src");
630             result &= m_MixerContainer->addElement(
631                 new PhonesSrc(*this, name, label, ctrl->desc));
632             type &= ~MOTU_CTRL_PHONES_SRC;
633         }
634         if (type & MOTU_CTRL_OPTICAL_MODE) {
635             result &= m_MixerContainer->addElement(
636                 new OpticalMode(*this, ctrl->dev_register,
637                     ctrl->name, ctrl->label, ctrl->desc));
638             type &= ~MOTU_CTRL_OPTICAL_MODE;
639         }
640         if (type & MOTU_CTRL_METER) {
641             if (ctrl->dev_register & MOTU_CTRL_METER_PEAKHOLD) {
642                 snprintf(name, 100, "%s%s", ctrl->name, "peakhold_time");
643                 snprintf(label,100, "%s%s", ctrl->label,"peakhold time");
644                 result &= m_MixerContainer->addElement(
645                     new MeterControl(*this, MOTU_METER_PEAKHOLD_MASK,
646                         MOTU_METER_PEAKHOLD_SHIFT, name, label, ctrl->desc));
647             }
648             if (ctrl->dev_register & MOTU_CTRL_METER_CLIPHOLD) {
649                 snprintf(name, 100, "%s%s", ctrl->name, "cliphold_time");
650                 snprintf(label,100, "%s%s", ctrl->label,"cliphold time");
651                 result &= m_MixerContainer->addElement(
652                     new MeterControl(*this, MOTU_METER_CLIPHOLD_MASK,
653                         MOTU_METER_CLIPHOLD_SHIFT, name, label, ctrl->desc));
654             }
655             if (ctrl->dev_register & MOTU_CTRL_METER_AESEBU_SRC) {
656                 snprintf(name, 100, "%s%s", ctrl->name, "aesebu_src");
657                 snprintf(label,100, "%s%s", ctrl->label,"AESEBU source");
658                 result &= m_MixerContainer->addElement(
659                     new MeterControl(*this, MOTU_METER_AESEBU_SRC_MASK,
660                         MOTU_METER_AESEBU_SRC_SHIFT, name, label, ctrl->desc));
661             }
662             if (ctrl->dev_register & MOTU_CTRL_METER_PROG_SRC) {
663                 snprintf(name, 100, "%s%s", ctrl->name, "src");
664                 snprintf(label,100, "%s%s", ctrl->label,"source");
665                 result &= m_MixerContainer->addElement(
666                     new MeterControl(*this, MOTU_METER_PROG_SRC_MASK,
667                         MOTU_METER_PROG_SRC_SHIFT, name, label, ctrl->desc));
668             }
669             type &= ~MOTU_CTRL_METER;
670         }
671
672         if (type) {
673             debugOutput(DEBUG_LEVEL_WARNING, "Unknown mixer control type flag bits 0x%08x\n", ctrl->type);
674         }
675     }
676     return result;
677 }
678
679 bool
680 MotuDevice::buildMixer() {
681     bool result = true;
682     debugOutput(DEBUG_LEVEL_VERBOSE, "Building a MOTU mixer...\n");
683
684     destroyMixer();
685        
686     // create the mixer object container
687     m_MixerContainer = new Control::Container(this, "Mixer");
688     if (!m_MixerContainer) {
689         debugError("Could not create mixer container...\n");
690         return false;
691     }
692
693     // Create and populate the top-level matrix mixers
694     result = buildMixerAudioControls();
695
696     /* Now add some general device information controls.  These may yet
697      * become device-specific if it turns out to be easier that way.
698      */
699     result &= m_MixerContainer->addElement(
700         new InfoElement(*this, MOTU_INFO_MODEL, "Info/Model", "Model identifier", ""));
701     result &= m_MixerContainer->addElement(
702         new InfoElement(*this, MOTU_INFO_IS_STREAMING, "Info/IsStreaming", "Is device streaming", ""));
703     result &= m_MixerContainer->addElement(
704         new InfoElement(*this, MOTU_INFO_SAMPLE_RATE, "Info/SampleRate", "Device sample rate", ""));
705     result &= m_MixerContainer->addElement(
706         new InfoElement(*this, MOTU_INFO_HAS_MIC_INPUTS, "Info/HasMicInputs", "Device has mic inputs", ""));
707     result &= m_MixerContainer->addElement(
708         new InfoElement(*this, MOTU_INFO_HAS_AESEBU_INPUTS, "Info/HasAESEBUInputs", "Device has AES/EBU inputs", ""));
709     result &= m_MixerContainer->addElement(
710         new InfoElement(*this, MOTU_INFO_HAS_SPDIF_INPUTS, "Info/HasSPDIFInputs", "Device has SPDIF inputs", ""));
711     result &= m_MixerContainer->addElement(
712         new InfoElement(*this, MOTU_INFO_HAS_OPTICAL_SPDIF, "Info/HasOpticalSPDIF", "Device has Optical SPDIF", ""));
713
714     if (!addElement(m_MixerContainer)) {
715         debugWarning("Could not register mixer to device\n");
716         // clean up
717         destroyMixer();
718         return false;
719     }
720
721     // Special controls
722     m_ControlContainer = new Control::Container(this, "Control");
723     if (!m_ControlContainer) {
724         debugError("Could not create control container...\n");
725         return false;
726     }
727
728     // Special controls get added here
729
730     if (!result) {
731         debugWarning("One or more device control elements could not be created.");
732         // clean up those that couldn't be created
733         destroyMixer();
734         return false;
735     }
736     if (!addElement(m_ControlContainer)) {
737         debugWarning("Could not register controls to device\n");
738         // clean up
739         destroyMixer();
740         return false;
741     }
742
743     return true;
744 }
745
746
747 bool
748 MotuDevice::destroyMixer() {
749     debugOutput(DEBUG_LEVEL_VERBOSE, "destroy mixer...\n");
750
751     if (m_MixerContainer == NULL) {
752         debugOutput(DEBUG_LEVEL_VERBOSE, "no mixer to destroy...\n");
753         return true;
754     }
755    
756     if (!deleteElement(m_MixerContainer)) {
757         debugError("Mixer present but not registered to the avdevice\n");
758         return false;
759     }
760
761     // remove and delete (as in free) child control elements
762     m_MixerContainer->clearElements(true);
763     delete m_MixerContainer;
764     m_MixerContainer = NULL;
765
766     // remove control container
767     if (m_ControlContainer == NULL) {
768         debugOutput(DEBUG_LEVEL_VERBOSE, "no controls to destroy...\n");
769         return true;
770     }
771    
772     if (!deleteElement(m_ControlContainer)) {
773         debugError("Controls present but not registered to the avdevice\n");
774         return false;
775     }
776    
777     // remove and delete (as in free) child control elements
778     m_ControlContainer->clearElements(true);
779     delete m_ControlContainer;
780     m_ControlContainer = NULL;
781
782     return true;
783 }
784
785 bool
786 MotuDevice::probe( Util::Configuration& c, ConfigRom& configRom, bool generic)
787 {
788     if(generic) return false;
789
790     unsigned int vendorId = configRom.getNodeVendorId();
791     unsigned int unitVersion = configRom.getUnitVersion();
792     unsigned int unitSpecifierId = configRom.getUnitSpecifierId();
793
794     for ( unsigned int i = 0;
795           i < ( sizeof( supportedDeviceList )/sizeof( VendorModelEntry ) );
796           ++i )
797     {
798         if ( ( supportedDeviceList[i].vendor_id == vendorId )
799              && ( supportedDeviceList[i].unit_version == unitVersion )
800              && ( supportedDeviceList[i].unit_specifier_id == unitSpecifierId )
801            )
802         {
803             return true;
804         }
805     }
806
807     return false;
808 }
809
810 FFADODevice *
811 MotuDevice::createDevice(DeviceManager& d, std::auto_ptr<ConfigRom>( configRom ))
812 {
813     return new MotuDevice(d, configRom);
814 }
815
816 bool
817 MotuDevice::discover()
818 {
819     unsigned int vendorId = getConfigRom().getNodeVendorId();
820     unsigned int unitVersion = getConfigRom().getUnitVersion();
821     unsigned int unitSpecifierId = getConfigRom().getUnitSpecifierId();
822
823     for ( unsigned int i = 0;
824           i < ( sizeof( supportedDeviceList )/sizeof( VendorModelEntry ) );
825           ++i )
826     {
827         if ( ( supportedDeviceList[i].vendor_id == vendorId )
828              && ( supportedDeviceList[i].unit_version == unitVersion )
829              && ( supportedDeviceList[i].unit_specifier_id == unitSpecifierId )
830            )
831         {
832             m_model = &(supportedDeviceList[i]);
833             m_motu_model=supportedDeviceList[i].model;
834         }
835     }
836
837     if (m_model == NULL) {
838         return false;
839     }
840
841     debugOutput( DEBUG_LEVEL_VERBOSE, "found %s %s\n",
842         m_model->vendor_name, m_model->model_name);
843
844     if (!buildMixer()) {
845         debugWarning("Could not build mixer\n");
846     }
847
848     return true;
849 }
850
851 enum FFADODevice::eStreamingState
852 MotuDevice::getStreamingState()
853 {
854     unsigned int val = ReadRegister(MOTU_REG_ISOCTRL);
855     /* Streaming is active if either bit 22 (Motu->PC streaming
856      * enable) or bit 30 (PC->Motu streaming enable) is set.
857      */
858     debugOutput(DEBUG_LEVEL_VERBOSE, "MOTU_REG_ISOCTRL: %08x\n", val);
859
860     if((val & 0x40400000) != 0) {
861         return eSS_Both;
862     } else if ((val & 0x40000000) != 0) {
863         return eSS_Receiving;
864     } else if ((val & 0x00400000) != 0) {
865         return eSS_Sending;
866     } else {
867         return eSS_Idle;
868     }
869 }
870
871 int
872 MotuDevice::getSamplingFrequency( ) {
873 /*
874  * Retrieve the current sample rate from the MOTU device.
875  */
876     quadlet_t q = ReadRegister(MOTU_REG_CLK_CTRL);
877     int rate = 0;
878
879     switch (q & MOTU_RATE_BASE_MASK) {
880         case MOTU_RATE_BASE_44100:
881             rate = 44100;
882             break;
883         case MOTU_RATE_BASE_48000:
884             rate = 48000;
885             break;
886     }
887     switch (q & MOTU_RATE_MULTIPLIER_MASK) {
888         case MOTU_RATE_MULTIPLIER_2X:
889             rate *= 2;
890             break;
891         case MOTU_RATE_MULTIPLIER_4X:
892             rate *= 4;
893             break;
894     }
895     return rate;
896 }
897
898 int
899 MotuDevice::getConfigurationId()
900 {
901     return 0;
902 }
903
904 bool
905 MotuDevice::setClockCtrlRegister(signed int samplingFrequency, unsigned int clock_source)
906 {
907 /*
908  * Set the MOTU device's samplerate and/or clock source via the clock
909  * control register.  If samplingFrequency <= 0 it remains unchanged.  If
910  * clock_source is MOTU_CLKSRC_UNCHANGED the clock source remains unchanged.
911  */
912     const char *src_name;
913     quadlet_t q, new_rate=0xffffffff;
914     int i, supported=true, cancel_adat=false;
915     quadlet_t reg;
916
917     /* Don't touch anything if there's nothing to do */
918     if (samplingFrequency<=0 && clock_source==MOTU_CLKSRC_NONE)
919         return true;
920
921     if ( samplingFrequency > DevicesProperty[m_motu_model-1].MaxSampleRate )
922        return false;
923
924     reg = ReadRegister(MOTU_REG_CLK_CTRL);
925
926     switch ( samplingFrequency ) {
927         case -1:
928             break;
929         case 44100:
930             new_rate = MOTU_RATE_BASE_44100 | MOTU_RATE_MULTIPLIER_1X;
931             break;
932         case 48000:
933             new_rate = MOTU_RATE_BASE_48000 | MOTU_RATE_MULTIPLIER_1X;
934             break;
935         case 88200:
936             new_rate = MOTU_RATE_BASE_44100 | MOTU_RATE_MULTIPLIER_2X;
937             break;
938         case 96000:
939             new_rate = MOTU_RATE_BASE_48000 | MOTU_RATE_MULTIPLIER_2X;
940             break;
941         case 176400:
942             new_rate = MOTU_RATE_BASE_44100 | MOTU_RATE_MULTIPLIER_4X;
943             cancel_adat = true;  // current ADAT protocol doesn't support sample rate > 96000
944             break;
945         case 192000:
946             new_rate = MOTU_RATE_BASE_48000 | MOTU_RATE_MULTIPLIER_4X;
947             cancel_adat = true;
948             break;
949         default:
950             supported=false;
951     }
952
953     // Sanity check the clock source
954     if ((clock_source>7 || clock_source==6) && clock_source!=MOTU_CLKSRC_UNCHANGED)
955         supported = false;
956
957     // Update the clock control register.  FIXME: while this is now rather
958     // comprehensive there may still be a need to manipulate MOTU_REG_CLK_CTRL
959     // a little more than we do.
960     if (supported) {
961
962         // If optical port must be disabled (because a 4x sample rate has
963         // been selected) then do so before changing the sample rate.  At
964         // this stage it will be up to the user to re-enable the optical
965         // port if the sample rate is set to a 1x or 2x rate later.
966         if (cancel_adat) {
967             setOpticalMode(MOTU_DIR_INOUT, MOTU_OPTICAL_MODE_OFF);
968         }
969
970         // Set up new frequency if requested
971         if (new_rate != 0xffffffff) {
972             reg &= ~(MOTU_RATE_BASE_MASK|MOTU_RATE_MULTIPLIER_MASK);
973             reg |= new_rate;
974         }
975
976         // Set up new clock source if required
977         if (clock_source != MOTU_CLKSRC_UNCHANGED) {
978             reg &= ~MOTU_CLKSRC_MASK;
979             reg |= (clock_source & MOTU_CLKSRC_MASK);
980         }
981
982         // In other OSes bit 26 of MOTU_REG_CLK_CTRL always seems
983         // to be set when this register is written to although the
984         // reason isn't currently known.  When we set it, it appears
985         // to prevent output being produced so we'll leave it unset
986         // until we work out what's going on.  Other systems write
987         // to MOTU_REG_CLK_CTRL multiple times, so that may be
988         // part of the mystery.
989         //   value |= 0x04000000;
990         if (WriteRegister(MOTU_REG_CLK_CTRL, reg) == 0) {
991             supported=true;
992         } else {
993             supported=false;
994         }
995         // A write to the rate/clock control register requires the
996         // textual name of the current clock source be sent to the
997         // clock source name registers.
998         switch (reg & MOTU_CLKSRC_MASK) {
999             case MOTU_CLKSRC_INTERNAL:
1000                 src_name = "Internal        ";
1001                 break;
1002             case MOTU_CLKSRC_ADAT_OPTICAL:
1003                 src_name = "ADAT Optical    ";
1004                 break;
1005             case MOTU_CLKSRC_SPDIF_TOSLINK:
1006                 if (getOpticalMode(MOTU_DIR_IN) == MOTU_OPTICAL_MODE_TOSLINK)
1007                     src_name = "TOSLink         ";
1008                 else
1009                     src_name = "SPDIF           ";
1010                 break;
1011             case MOTU_CLKSRC_SMPTE:
1012                 src_name = "SMPTE           ";
1013                 break;
1014             case MOTU_CLKSRC_WORDCLOCK:
1015                 src_name = "Word Clock In   ";
1016                 break;
1017             case MOTU_CLKSRC_ADAT_9PIN:
1018                 src_name = "ADAT 9-pin      ";
1019                 break;
1020             case MOTU_CLKSRC_AES_EBU:
1021                 src_name = "AES-EBU         ";
1022                 break;
1023             default:
1024                 src_name = "Unknown         ";
1025         }
1026         for (i=0; i<16; i+=4) {
1027             q = (src_name[i]<<24) | (src_name[i+1]<<16) |
1028                 (src_name[i+2]<<8) | src_name[i+3];
1029             WriteRegister(MOTU_REG_CLKSRC_NAME0+i, q);
1030         }
1031     }
1032     return supported;
1033 }
1034
1035 bool
1036 MotuDevice::setSamplingFrequency( int samplingFrequency )
1037 {
1038 /*
1039  * Set the MOTU device's samplerate.
1040  */
1041     return setClockCtrlRegister(samplingFrequency, MOTU_CLKSRC_UNCHANGED);
1042 }
1043
1044 std::vector<int>
1045 MotuDevice::getSupportedSamplingFrequencies()
1046 {
1047     std::vector<int> frequencies;
1048     signed int max_freq = DevicesProperty[m_motu_model-1].MaxSampleRate;
1049
1050     /* All MOTUs support 1x rates.  All others must be conditional. */
1051     frequencies.push_back(44100);
1052     frequencies.push_back(48000);
1053
1054     if (88200 <= max_freq)
1055         frequencies.push_back(88200);
1056     if (96000 <= max_freq)
1057         frequencies.push_back(96000);
1058     if (176400 <= max_freq)
1059         frequencies.push_back(176400);
1060     if (192000 <= max_freq)
1061         frequencies.push_back(192000);
1062     return frequencies;
1063 }
1064
1065 FFADODevice::ClockSource
1066 MotuDevice::clockIdToClockSource(unsigned int id) {
1067     ClockSource s;
1068     s.id = id;
1069
1070     // Assume a clock source is valid/active unless otherwise overridden.
1071     s.valid = true;
1072     s.locked = true;
1073     s.active = true;
1074
1075     switch (id) {
1076         case MOTU_CLKSRC_INTERNAL:
1077             s.type = eCT_Internal;
1078             s.description = "Internal sync";
1079             break;
1080         case MOTU_CLKSRC_ADAT_OPTICAL:
1081             s.type = eCT_ADAT;
1082             s.description = "ADAT optical";
1083             break;
1084         case MOTU_CLKSRC_SPDIF_TOSLINK:
1085             s.type = eCT_SPDIF;
1086             s.description = "SPDIF/Toslink";
1087             break;
1088         case MOTU_CLKSRC_SMPTE:
1089             s.type = eCT_SMPTE;
1090             s.description = "SMPTE";
1091             // Since we don't currently know how to deal with SMPTE on these devices
1092             // make sure the SMPTE clock source is disabled.
1093             s.valid = false;
1094             s.active = false;
1095             s.locked = false;
1096             break;
1097         case MOTU_CLKSRC_WORDCLOCK:
1098             s.type = eCT_WordClock;
1099             s.description = "Wordclock";
1100             break;
1101         case MOTU_CLKSRC_ADAT_9PIN:
1102             s.type = eCT_ADAT;
1103             s.description = "ADAT 9-pin";
1104             break;
1105         case MOTU_CLKSRC_AES_EBU:
1106             s.type = eCT_AES;
1107             s.description = "AES/EBU";
1108             break;
1109         default:
1110             s.type = eCT_Invalid;
1111     }
1112
1113     s.slipping = false;
1114     return s;
1115 }
1116
1117 FFADODevice::ClockSourceVector
1118 MotuDevice::getSupportedClockSources() {
1119     FFADODevice::ClockSourceVector r;
1120     ClockSource s;
1121
1122     /* Form a list of clocks supported by MOTU interfaces */
1123     s = clockIdToClockSource(MOTU_CLKSRC_INTERNAL);
1124     r.push_back(s);
1125     s = clockIdToClockSource(MOTU_CLKSRC_ADAT_OPTICAL);
1126     r.push_back(s);
1127     s = clockIdToClockSource(MOTU_CLKSRC_SPDIF_TOSLINK);
1128     r.push_back(s);
1129     s = clockIdToClockSource(MOTU_CLKSRC_SMPTE);
1130     r.push_back(s);
1131     s = clockIdToClockSource(MOTU_CLKSRC_WORDCLOCK);
1132     r.push_back(s);
1133     s = clockIdToClockSource(MOTU_CLKSRC_ADAT_9PIN);
1134     r.push_back(s);
1135     s = clockIdToClockSource(MOTU_CLKSRC_AES_EBU);
1136     r.push_back(s);
1137
1138     return r;
1139 }
1140
1141 bool
1142 MotuDevice::setActiveClockSource(ClockSource s) {
1143     debugOutput(DEBUG_LEVEL_VERBOSE, "setting clock source to id: %d\n",s.id);
1144
1145     // FIXME: this could do with some error checking
1146     return setClockCtrlRegister(-1, s.id);
1147 }
1148
1149 FFADODevice::ClockSource
1150 MotuDevice::getActiveClockSource() {
1151     ClockSource s;
1152     quadlet_t clock_id = ReadRegister(MOTU_REG_CLK_CTRL) & MOTU_CLKSRC_MASK;
1153     s = clockIdToClockSource(clock_id);
1154     s.active = true;
1155     return s;
1156 }
1157
1158 bool
1159 MotuDevice::lock() {
1160
1161     return true;
1162 }
1163
1164
1165 bool
1166 MotuDevice::unlock() {
1167
1168     return true;
1169 }
1170
1171 void
1172 MotuDevice::showDevice()
1173 {
1174     debugOutput(DEBUG_LEVEL_VERBOSE,
1175         "%s %s at node %d\n", m_model->vendor_name, m_model->model_name,
1176         getNodeId());
1177 }
1178
1179 bool
1180 MotuDevice::prepare() {
1181
1182     int samp_freq = getSamplingFrequency();
1183     unsigned int optical_in_mode = getOpticalMode(MOTU_DIR_IN);
1184     unsigned int optical_out_mode = getOpticalMode(MOTU_DIR_OUT);
1185     unsigned int event_size_in = getEventSize(MOTU_DIR_IN);
1186     unsigned int event_size_out= getEventSize(MOTU_DIR_OUT);
1187
1188     debugOutput(DEBUG_LEVEL_NORMAL, "Preparing MotuDevice...\n" );
1189
1190     // Explicitly set the optical mode, primarily to ensure that the
1191     // MOTU_REG_OPTICAL_CTRL register is initialised.  We need to do this to
1192     // because some interfaces (the Ultralite for example) appear to power
1193     // up without this set to anything sensible.  In this case, writes to
1194     // MOTU_REG_ISOCTRL fail more often than not, which is bad.
1195     setOpticalMode(MOTU_DIR_IN, optical_in_mode);
1196     setOpticalMode(MOTU_DIR_OUT, optical_out_mode);
1197
1198     // Allocate bandwidth if not previously done.
1199     // FIXME: The bandwidth allocation calculation can probably be
1200     // refined somewhat since this is currently based on a rudimentary
1201     // understanding of the ieee1394 iso protocol.
1202     // Currently we assume the following.
1203     //   * Ack/iso gap = 0.05 us
1204     //   * DATA_PREFIX = 0.16 us
1205     //   * DATA_END    = 0.26 us
1206     // These numbers are the worst-case figures given in the ieee1394
1207     // standard.  This gives approximately 0.5 us of overheads per packet -
1208     // around 25 bandwidth allocation units (from the ieee1394 standard 1
1209     // bandwidth allocation unit is 125/6144 us).  We further assume the
1210     // MOTU is running at S400 (which it should be) so one allocation unit
1211     // is equivalent to 1 transmitted byte; thus the bandwidth allocation
1212     // required for the packets themselves is just the size of the packet.
1213     // We used to allocate based on the maximum packet size (1160 bytes at
1214     // 192 kHz for the traveler) but now do this based on the actual device
1215     // state by utilising the result from getEventSize() and remembering
1216     // that each packet has an 8 byte CIP header.  Note that bandwidth is
1217     // allocated on a *per stream* basis - it must be allocated for both the
1218     // transmit and receive streams.  While most MOTU modules are close to
1219     // symmetric in terms of the number of in/out channels there are
1220     // exceptions, so we deal with receive and transmit bandwidth separately.
1221     signed int n_events_per_packet = samp_freq<=48000?8:(samp_freq<=96000?16:32);
1222     m_rx_bandwidth = 25 + (n_events_per_packet*event_size_in);
1223     m_tx_bandwidth = 25 + (n_events_per_packet*event_size_out);
1224
1225     // Assign iso channels if not already done
1226     if (m_iso_recv_channel < 0)
1227         m_iso_recv_channel = get1394Service().allocateIsoChannelGeneric(m_rx_bandwidth);
1228
1229     if (m_iso_send_channel < 0)
1230         m_iso_send_channel = get1394Service().allocateIsoChannelGeneric(m_tx_bandwidth);
1231
1232     debugOutput(DEBUG_LEVEL_VERBOSE, "recv channel = %d, send channel = %d\n",
1233         m_iso_recv_channel, m_iso_send_channel);
1234
1235     if (m_iso_recv_channel<0 || m_iso_send_channel<0) {
1236         // be nice and deallocate
1237         if (m_iso_recv_channel >= 0)
1238             get1394Service().freeIsoChannel(m_iso_recv_channel);
1239         if (m_iso_send_channel >= 0)
1240             get1394Service().freeIsoChannel(m_iso_send_channel);
1241
1242         debugFatal("Could not allocate iso channels!\n");
1243         return false;
1244     }
1245
1246     m_receiveProcessor=new Streaming::MotuReceiveStreamProcessor(*this, event_size_in);
1247
1248     // The first thing is to initialize the processor.  This creates the
1249     // data structures.
1250     if(!m_receiveProcessor->init()) {
1251         debugFatal("Could not initialize receive processor!\n");
1252         return false;
1253     }
1254     m_receiveProcessor->setVerboseLevel(getDebugLevel());
1255
1256     // Now we add ports to the processor
1257     debugOutput(DEBUG_LEVEL_VERBOSE,"Adding ports to receive processor\n");
1258
1259     char *buff;
1260     Streaming::Port *p=NULL;
1261
1262     // retrieve the ID
1263     std::string id=std::string("dev?");
1264     if(!getOption("id", id)) {
1265         debugWarning("Could not retrieve id parameter, defaulting to 'dev?'\n");
1266     }
1267
1268     // Add audio capture ports
1269     if (!addDirPorts(Streaming::Port::E_Capture, samp_freq, optical_in_mode)) {
1270         return false;
1271     }
1272
1273     // Add MIDI port.  The MOTU only has one MIDI input port, with each
1274     // MIDI byte sent using a 3 byte sequence starting at byte 4 of the
1275     // event data.
1276     asprintf(&buff,"%s_cap_MIDI0",id.c_str());
1277     p = new Streaming::MotuMidiPort(*m_receiveProcessor, buff,
1278         Streaming::Port::E_Capture, 4);
1279     if (!p) {
1280         debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n", buff);
1281     }
1282     free(buff);
1283
1284     // example of adding an control port:
1285 //    asprintf(&buff,"%s_cap_%s",id.c_str(),"myportnamehere");
1286 //    p=new Streaming::MotuControlPort(
1287 //            buff,
1288 //            Streaming::Port::E_Capture,
1289 //            0 // you can add all other port specific stuff you
1290 //              // need to pass by extending MotuXXXPort and MotuPortInfo
1291 //    );
1292 //    free(buff);
1293 //
1294 //    if (!p) {
1295 //        debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n",buff);
1296 //    } else {
1297 //
1298 //        if (!m_receiveProcessor->addPort(p)) {
1299 //            debugWarning("Could not register port with stream processor\n");
1300 //            return false;
1301 //        } else {
1302 //            debugOutput(DEBUG_LEVEL_VERBOSE, "Added port %s\n",buff);
1303 //        }
1304 //    }
1305
1306     // Do the same for the transmit processor
1307     m_transmitProcessor=new Streaming::MotuTransmitStreamProcessor(*this, event_size_out);
1308
1309     m_transmitProcessor->setVerboseLevel(getDebugLevel());
1310
1311     if(!m_transmitProcessor->init()) {
1312         debugFatal("Could not initialize transmit processor!\n");
1313         return false;
1314     }
1315
1316     // Now we add ports to the processor
1317     debugOutput(DEBUG_LEVEL_VERBOSE,"Adding ports to transmit processor\n");
1318
1319     // Add audio playback ports
1320     if (!addDirPorts(Streaming::Port::E_Playback, samp_freq, optical_out_mode)) {
1321         return false;
1322     }
1323
1324     // Add MIDI port.  The MOTU only has one output MIDI port, with each
1325     // MIDI byte transmitted using a 3 byte sequence starting at byte 4
1326     // of the event data.
1327     asprintf(&buff,"%s_pbk_MIDI0",id.c_str());
1328     p = new Streaming::MotuMidiPort(*m_transmitProcessor, buff,
1329         Streaming::Port::E_Capture, 4);
1330     if (!p) {
1331         debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n", buff);
1332     }
1333     free(buff);
1334
1335     // example of adding an control port:
1336 //    asprintf(&buff,"%s_pbk_%s",id.c_str(),"myportnamehere");
1337 //
1338 //    p=new Streaming::MotuControlPort(
1339 //            buff,
1340 //            Streaming::Port::E_Playback,
1341 //            0 // you can add all other port specific stuff you
1342 //              // need to pass by extending MotuXXXPort and MotuPortInfo
1343 //    );
1344 //    free(buff);
1345 //
1346 //    if (!p) {
1347 //        debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n",buff);
1348 //    } else {
1349 //        if (!m_transmitProcessor->addPort(p)) {
1350 //            debugWarning("Could not register port with stream processor\n");
1351 //            return false;
1352 //        } else {
1353 //            debugOutput(DEBUG_LEVEL_VERBOSE, "Added port %s\n",buff);
1354 //        }
1355 //    }
1356
1357     return true;
1358 }
1359
1360 int
1361 MotuDevice::getStreamCount() {
1362      return 2; // one receive, one transmit
1363 }
1364
1365 Streaming::StreamProcessor *
1366 MotuDevice::getStreamProcessorByIndex(int i) {
1367     switch (i) {
1368     case 0:
1369         return m_receiveProcessor;
1370     case 1:
1371          return m_transmitProcessor;
1372     default:
1373         return NULL;
1374     }
1375     return 0;
1376 }
1377
1378 bool
1379 MotuDevice::startStreamByIndex(int i) {
1380
1381 quadlet_t isoctrl = ReadRegister(MOTU_REG_ISOCTRL);
1382
1383     // NOTE: this assumes that you have two streams
1384     switch (i) {
1385     case 0:
1386         // TODO: do the stuff that is nescessary to make the device
1387         // receive a stream
1388
1389         // Set the streamprocessor channel to the one obtained by
1390         // the connection management
1391         m_receiveProcessor->setChannel(m_iso_recv_channel);
1392
1393         // Mask out current transmit settings of the MOTU and replace
1394         // with new ones.  Turn bit 24 on to enable changes to the
1395         // MOTU's iso transmit settings when the iso control register
1396         // is written.  Bit 23 enables iso transmit from the MOTU.
1397         isoctrl &= 0xff00ffff;
1398         isoctrl |= (m_iso_recv_channel << 16);
1399         isoctrl |= 0x00c00000;
1400         WriteRegister(MOTU_REG_ISOCTRL, isoctrl);
1401         break;
1402     case 1:
1403         // TODO: do the stuff that is nescessary to make the device
1404         // transmit a stream
1405
1406         // Set the streamprocessor channel to the one obtained by
1407         // the connection management
1408         m_transmitProcessor->setChannel(m_iso_send_channel);
1409
1410         // Mask out current receive settings of the MOTU and replace
1411         // with new ones.  Turn bit 31 on to enable changes to the
1412         // MOTU's iso receive settings when the iso control register
1413         // is written.  Bit 30 enables iso receive by the MOTU.
1414         isoctrl &= 0x00ffffff;
1415         isoctrl |= (m_iso_send_channel << 24);
1416         isoctrl |= 0xc0000000;
1417         WriteRegister(MOTU_REG_ISOCTRL, isoctrl);
1418         break;
1419
1420     default: // Invalid stream index
1421         return false;
1422     }
1423
1424     return true;
1425 }
1426
1427 bool
1428 MotuDevice::stopStreamByIndex(int i) {
1429
1430 quadlet_t isoctrl = ReadRegister(MOTU_REG_ISOCTRL);
1431
1432     // TODO: connection management: break connection
1433     // cfr the start function
1434
1435     // NOTE: this assumes that you have two streams
1436     switch (i) {
1437     case 0:
1438         // Turn bit 22 off to disable iso send by the MOTU.  Turn
1439         // bit 23 on to enable changes to the MOTU's iso transmit
1440         // settings when the iso control register is written.
1441         isoctrl &= 0xffbfffff;
1442         isoctrl |= 0x00800000;
1443         WriteRegister(MOTU_REG_ISOCTRL, isoctrl);
1444         break;
1445     case 1:
1446         // Turn bit 30 off to disable iso receive by the MOTU.  Turn
1447         // bit 31 on to enable changes to the MOTU's iso receive
1448         // settings when the iso control register is written.
1449         isoctrl &= 0xbfffffff;
1450         isoctrl |= 0x80000000;
1451         WriteRegister(MOTU_REG_ISOCTRL, isoctrl);
1452         break;
1453
1454     default: // Invalid stream index
1455         return false;
1456     }
1457
1458     return true;
1459 }
1460
1461 signed int MotuDevice::getIsoRecvChannel(void) {
1462     return m_iso_recv_channel;
1463 }
1464
1465 signed int MotuDevice::getIsoSendChannel(void) {
1466     return m_iso_send_channel;
1467 }
1468
1469 unsigned int MotuDevice::getOpticalMode(unsigned int dir) {
1470     unsigned int reg = ReadRegister(MOTU_REG_ROUTE_PORT_CONF);
1471
1472 debugOutput(DEBUG_LEVEL_VERBOSE, "optical mode: %x %x %x %x\n",dir, reg, reg & MOTU_OPTICAL_IN_MODE_MASK,
1473 reg & MOTU_OPTICAL_OUT_MODE_MASK);
1474
1475     if (dir == MOTU_DIR_IN)
1476         return (reg & MOTU_OPTICAL_IN_MODE_MASK) >> 8;
1477     else
1478         return (reg & MOTU_OPTICAL_OUT_MODE_MASK) >> 10;
1479 }
1480
1481 signed int MotuDevice::setOpticalMode(unsigned int dir, unsigned int mode) {
1482     unsigned int reg = ReadRegister(MOTU_REG_ROUTE_PORT_CONF);
1483     unsigned int opt_ctrl = 0x0000002;
1484
1485     /* THe 896HD doesn't have an SPDIF/TOSLINK optical mode, so don't try to
1486      * set it
1487      */
1488     if (m_motu_model==MOTU_MODEL_896HD && mode==MOTU_OPTICAL_MODE_TOSLINK)
1489         return -1;
1490
1491     // Set up the optical control register value according to the current
1492     // optical port modes.  At this stage it's not completely understood
1493     // what the "Optical control" register does, so the values it's set to
1494     // are more or less "magic" numbers.
1495     if ((reg & MOTU_OPTICAL_IN_MODE_MASK) != (MOTU_OPTICAL_MODE_ADAT<<8))
1496         opt_ctrl |= 0x00000080;
1497     if ((reg & MOTU_OPTICAL_OUT_MODE_MASK) != (MOTU_OPTICAL_MODE_ADAT<<10))
1498         opt_ctrl |= 0x00000040;
1499
1500     if (dir & MOTU_DIR_IN) {
1501         reg &= ~MOTU_OPTICAL_IN_MODE_MASK;
1502         reg |= (mode << 8) & MOTU_OPTICAL_IN_MODE_MASK;
1503         if (mode != MOTU_OPTICAL_MODE_ADAT)
1504             opt_ctrl |= 0x00000080;
1505         else
1506             opt_ctrl &= ~0x00000080;
1507     }
1508     if (dir & MOTU_DIR_OUT) {
1509         reg &= ~MOTU_OPTICAL_OUT_MODE_MASK;
1510         reg |= (mode <<10) & MOTU_OPTICAL_OUT_MODE_MASK;
1511         if (mode != MOTU_OPTICAL_MODE_ADAT)
1512             opt_ctrl |= 0x00000040;
1513         else
1514             opt_ctrl &= ~0x00000040;
1515     }
1516
1517     // FIXME: there seems to be more to it than this, but for
1518     // the moment at least this seems to work.
1519     WriteRegister(MOTU_REG_ROUTE_PORT_CONF, reg);
1520     return WriteRegister(MOTU_REG_OPTICAL_CTRL, opt_ctrl);
1521 }
1522
1523 signed int MotuDevice::getEventSize(unsigned int direction) {
1524 //
1525 // Return the size in bytes of a single event sent to (dir==MOTU_OUT) or
1526 // from (dir==MOTU_IN) the MOTU as part of an iso data packet.
1527 //
1528 // FIXME: for performance it may turn out best to calculate the event
1529 // size in setOpticalMode and cache the result in a data field.  However,
1530 // as it stands this will not adapt to dynamic changes in sample rate - we'd
1531 // need a setFrameRate() for that.
1532 //
1533 // At the very least an event consists of the SPH (4 bytes) and the control/MIDI
1534 // bytes (6 bytes).
1535 // Note that all audio channels are sent using 3 bytes.
1536 signed int sample_rate = getSamplingFrequency();
1537 signed int optical_mode = getOpticalMode(direction);
1538 signed int size = 4+6;
1539
1540 unsigned int i;
1541 unsigned int dir = direction==Streaming::Port::E_Capture?MOTU_DIR_IN:MOTU_DIR_OUT;
1542 unsigned int flags = (1 << ( optical_mode + 4 ));
1543
1544     if ( sample_rate > 96000 )
1545         flags |= MOTU_PA_RATE_4x;
1546     else if ( sample_rate > 48000 )
1547         flags |= MOTU_PA_RATE_2x;
1548     else
1549         flags |= MOTU_PA_RATE_1x;
1550
1551     // Don't test for padding port flag here since we need to include such
1552     // pseudo-ports when calculating the event size.
1553     for (i=0; i < DevicesProperty[m_motu_model-1].n_port_entries; i++) {
1554         if (( DevicesProperty[m_motu_model-1].port_entry[i].port_dir & dir ) &&
1555            ( DevicesProperty[m_motu_model-1].port_entry[i].port_flags & MOTU_PA_RATE_MASK & flags ) &&
1556            ( DevicesProperty[m_motu_model-1].port_entry[i].port_flags & MOTU_PA_OPTICAL_MASK & flags )) {
1557             size += 3;
1558         }
1559     }
1560
1561     // Finally round size up to the next quadlet boundary
1562     return ((size+3)/4)*4;
1563 }
1564 /* ======================================================================= */
1565
1566 bool MotuDevice::addPort(Streaming::StreamProcessor *s_processor,
1567   char *name, enum Streaming::Port::E_Direction direction,
1568   int position, int size) {
1569 /*
1570  * Internal helper function to add a MOTU port to a given stream processor.
1571  * This just saves the unnecessary replication of what is essentially
1572  * boilerplate code.  Note that the port name is freed by this function
1573  * prior to exit.
1574  */
1575 Streaming::Port *p=NULL;
1576
1577     p = new Streaming::MotuAudioPort(*s_processor, name, direction, position, size);
1578
1579     if (!p) {
1580         debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n",name);
1581     }
1582     free(name);
1583     return true;
1584 }
1585 /* ======================================================================= */
1586
1587 bool MotuDevice::addDirPorts(
1588   enum Streaming::Port::E_Direction direction,
1589   unsigned int sample_rate, unsigned int optical_mode) {
1590 /*
1591  * Internal helper method: adds all required ports for the given direction
1592  * based on the indicated sample rate and optical mode.
1593  *
1594  * Notes: currently ports are not created if they are disabled due to sample
1595  * rate or optical mode.  However, it might be better to unconditionally
1596  * create all ports and just disable those which are not active.
1597  */
1598 const char *mode_str = direction==Streaming::Port::E_Capture?"cap":"pbk";
1599 Streaming::StreamProcessor *s_processor;
1600 unsigned int i;
1601 char *buff;
1602 unsigned int dir = direction==Streaming::Port::E_Capture?MOTU_DIR_IN:MOTU_DIR_OUT;
1603 unsigned int flags = (1 << ( optical_mode + 4 ));
1604
1605     if ( sample_rate > 96000 )
1606         flags |= MOTU_PA_RATE_4x;
1607     else if ( sample_rate > 48000 )
1608         flags |= MOTU_PA_RATE_2x;
1609     else
1610         flags |= MOTU_PA_RATE_1x;
1611
1612     // retrieve the ID
1613     std::string id=std::string("dev?");
1614     if(!getOption("id", id)) {
1615         debugWarning("Could not retrieve id parameter, defaulting to 'dev?'\n");
1616     }
1617
1618     if (direction == Streaming::Port::E_Capture) {
1619         s_processor = m_receiveProcessor;
1620     } else {
1621         s_processor = m_transmitProcessor;
1622     }
1623
1624     for (i=0; i < DevicesProperty[m_motu_model-1].n_port_entries; i++) {
1625         if (( DevicesProperty[m_motu_model-1].port_entry[i].port_dir & dir ) &&
1626            ( DevicesProperty[m_motu_model-1].port_entry[i].port_flags & MOTU_PA_RATE_MASK & flags ) &&
1627            ( DevicesProperty[m_motu_model-1].port_entry[i].port_flags & MOTU_PA_OPTICAL_MASK & flags ) &&
1628            !( DevicesProperty[m_motu_model-1].port_entry[i].port_flags & MOTU_PA_PADDING )) {
1629             asprintf(&buff,"%s_%s_%s" , id.c_str(), mode_str,
1630               DevicesProperty[m_motu_model-1].port_entry[i].port_name);
1631             if (!addPort(s_processor, buff, direction, DevicesProperty[m_motu_model-1].port_entry[i].port_offset, 0))
1632                 return false;
1633         }
1634     }
1635    
1636     return true;
1637 }
1638 /* ======================================================================== */
1639
1640 unsigned int MotuDevice::ReadRegister(unsigned int reg) {
1641 /*
1642  * Attempts to read the requested register from the MOTU.
1643  */
1644
1645   quadlet_t quadlet;
1646
1647   quadlet = 0;
1648   // Note: 1394Service::read() expects a physical ID, not the node id
1649   if (get1394Service().read(0xffc0 | getNodeId(), MOTU_BASE_ADDR+reg, 1, &quadlet) <= 0) {
1650     debugError("Error doing motu read from register 0x%06x\n",reg);
1651   }
1652
1653   return CondSwapFromBus32(quadlet);
1654 }
1655
1656 signed int MotuDevice::WriteRegister(unsigned int reg, quadlet_t data) {
1657 /*
1658  * Attempts to write the given data to the requested MOTU register.
1659  */
1660
1661   unsigned int err = 0;
1662   data = CondSwapToBus32(data);
1663
1664   // Note: 1394Service::write() expects a physical ID, not the node id
1665   if (get1394Service().write(0xffc0 | getNodeId(), MOTU_BASE_ADDR+reg, 1, &data) <= 0) {
1666     err = 1;
1667     debugError("Error doing motu write to register 0x%06x\n",reg);
1668   }
1669
1670   SleepRelativeUsec(100);
1671   return (err==0)?0:-1;
1672 }
1673
1674 }
Note: See TracBrowser for help on using the browser.