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

Revision 1384, 66.1 kB (checked in by jwoithe, 12 years ago)

MOTU: add placeholder elements to the Ultralite mixer for ADAT channels so requests from the mixer for these channels don't cause out-of-range errors. These may be removed if an efficient way can be found of flagging individual mixer elements as disabled within the python code. It seems at present that if the respective control's container is disabled they are visually disabled but their IsEnable?() method still returns True.

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 int
852 MotuDevice::getSamplingFrequency( ) {
853 /*
854  * Retrieve the current sample rate from the MOTU device.
855  */
856     quadlet_t q = ReadRegister(MOTU_REG_CLK_CTRL);
857     int rate = 0;
858
859     switch (q & MOTU_RATE_BASE_MASK) {
860         case MOTU_RATE_BASE_44100:
861             rate = 44100;
862             break;
863         case MOTU_RATE_BASE_48000:
864             rate = 48000;
865             break;
866     }
867     switch (q & MOTU_RATE_MULTIPLIER_MASK) {
868         case MOTU_RATE_MULTIPLIER_2X:
869             rate *= 2;
870             break;
871         case MOTU_RATE_MULTIPLIER_4X:
872             rate *= 4;
873             break;
874     }
875     return rate;
876 }
877
878 int
879 MotuDevice::getConfigurationId()
880 {
881     return 0;
882 }
883
884 bool
885 MotuDevice::setClockCtrlRegister(signed int samplingFrequency, unsigned int clock_source)
886 {
887 /*
888  * Set the MOTU device's samplerate and/or clock source via the clock
889  * control register.  If samplingFrequency <= 0 it remains unchanged.  If
890  * clock_source is MOTU_CLKSRC_UNCHANGED the clock source remains unchanged.
891  */
892     const char *src_name;
893     quadlet_t q, new_rate=0xffffffff;
894     int i, supported=true, cancel_adat=false;
895     quadlet_t reg;
896
897     /* Don't touch anything if there's nothing to do */
898     if (samplingFrequency<=0 && clock_source==MOTU_CLKSRC_NONE)
899         return true;
900
901     if ( samplingFrequency > DevicesProperty[m_motu_model-1].MaxSampleRate )
902        return false;
903
904     reg = ReadRegister(MOTU_REG_CLK_CTRL);
905
906     switch ( samplingFrequency ) {
907         case -1:
908             break;
909         case 44100:
910             new_rate = MOTU_RATE_BASE_44100 | MOTU_RATE_MULTIPLIER_1X;
911             break;
912         case 48000:
913             new_rate = MOTU_RATE_BASE_48000 | MOTU_RATE_MULTIPLIER_1X;
914             break;
915         case 88200:
916             new_rate = MOTU_RATE_BASE_44100 | MOTU_RATE_MULTIPLIER_2X;
917             break;
918         case 96000:
919             new_rate = MOTU_RATE_BASE_48000 | MOTU_RATE_MULTIPLIER_2X;
920             break;
921         case 176400:
922             new_rate = MOTU_RATE_BASE_44100 | MOTU_RATE_MULTIPLIER_4X;
923             cancel_adat = true;  // current ADAT protocol doesn't support sample rate > 96000
924             break;
925         case 192000:
926             new_rate = MOTU_RATE_BASE_48000 | MOTU_RATE_MULTIPLIER_4X;
927             cancel_adat = true;
928             break;
929         default:
930             supported=false;
931     }
932
933     // Sanity check the clock source
934     if ((clock_source>7 || clock_source==6) && clock_source!=MOTU_CLKSRC_UNCHANGED)
935         supported = false;
936
937     // Update the clock control register.  FIXME: while this is now rather
938     // comprehensive there may still be a need to manipulate MOTU_REG_CLK_CTRL
939     // a little more than we do.
940     if (supported) {
941
942         // If optical port must be disabled (because a 4x sample rate has
943         // been selected) then do so before changing the sample rate.  At
944         // this stage it will be up to the user to re-enable the optical
945         // port if the sample rate is set to a 1x or 2x rate later.
946         if (cancel_adat) {
947             setOpticalMode(MOTU_DIR_INOUT, MOTU_OPTICAL_MODE_OFF);
948         }
949
950         // Set up new frequency if requested
951         if (new_rate != 0xffffffff) {
952             reg &= ~(MOTU_RATE_BASE_MASK|MOTU_RATE_MULTIPLIER_MASK);
953             reg |= new_rate;
954         }
955
956         // Set up new clock source if required
957         if (clock_source != MOTU_CLKSRC_UNCHANGED) {
958             reg &= ~MOTU_CLKSRC_MASK;
959             reg |= (clock_source & MOTU_CLKSRC_MASK);
960         }
961
962         // In other OSes bit 26 of MOTU_REG_CLK_CTRL always seems
963         // to be set when this register is written to although the
964         // reason isn't currently known.  When we set it, it appears
965         // to prevent output being produced so we'll leave it unset
966         // until we work out what's going on.  Other systems write
967         // to MOTU_REG_CLK_CTRL multiple times, so that may be
968         // part of the mystery.
969         //   value |= 0x04000000;
970         if (WriteRegister(MOTU_REG_CLK_CTRL, reg) == 0) {
971             supported=true;
972         } else {
973             supported=false;
974         }
975         // A write to the rate/clock control register requires the
976         // textual name of the current clock source be sent to the
977         // clock source name registers.
978         switch (reg & MOTU_CLKSRC_MASK) {
979             case MOTU_CLKSRC_INTERNAL:
980                 src_name = "Internal        ";
981                 break;
982             case MOTU_CLKSRC_ADAT_OPTICAL:
983                 src_name = "ADAT Optical    ";
984                 break;
985             case MOTU_CLKSRC_SPDIF_TOSLINK:
986                 if (getOpticalMode(MOTU_DIR_IN) == MOTU_OPTICAL_MODE_TOSLINK)
987                     src_name = "TOSLink         ";
988                 else
989                     src_name = "SPDIF           ";
990                 break;
991             case MOTU_CLKSRC_SMPTE:
992                 src_name = "SMPTE           ";
993                 break;
994             case MOTU_CLKSRC_WORDCLOCK:
995                 src_name = "Word Clock In   ";
996                 break;
997             case MOTU_CLKSRC_ADAT_9PIN:
998                 src_name = "ADAT 9-pin      ";
999                 break;
1000             case MOTU_CLKSRC_AES_EBU:
1001                 src_name = "AES-EBU         ";
1002                 break;
1003             default:
1004                 src_name = "Unknown         ";
1005         }
1006         for (i=0; i<16; i+=4) {
1007             q = (src_name[i]<<24) | (src_name[i+1]<<16) |
1008                 (src_name[i+2]<<8) | src_name[i+3];
1009             WriteRegister(MOTU_REG_CLKSRC_NAME0+i, q);
1010         }
1011     }
1012     return supported;
1013 }
1014
1015 bool
1016 MotuDevice::setSamplingFrequency( int samplingFrequency )
1017 {
1018 /*
1019  * Set the MOTU device's samplerate.
1020  */
1021     return setClockCtrlRegister(samplingFrequency, MOTU_CLKSRC_UNCHANGED);
1022 }
1023
1024 std::vector<int>
1025 MotuDevice::getSupportedSamplingFrequencies()
1026 {
1027     std::vector<int> frequencies;
1028     signed int max_freq = DevicesProperty[m_motu_model-1].MaxSampleRate;
1029
1030     /* All MOTUs support 1x rates.  All others must be conditional. */
1031     frequencies.push_back(44100);
1032     frequencies.push_back(48000);
1033
1034     if (88200 <= max_freq)
1035         frequencies.push_back(88200);
1036     if (96000 <= max_freq)
1037         frequencies.push_back(96000);
1038     if (176400 <= max_freq)
1039         frequencies.push_back(176400);
1040     if (192000 <= max_freq)
1041         frequencies.push_back(192000);
1042     return frequencies;
1043 }
1044
1045 FFADODevice::ClockSource
1046 MotuDevice::clockIdToClockSource(unsigned int id) {
1047     ClockSource s;
1048     s.id = id;
1049
1050     // Assume a clock source is valid/active unless otherwise overridden.
1051     s.valid = true;
1052     s.locked = true;
1053     s.active = true;
1054
1055     switch (id) {
1056         case MOTU_CLKSRC_INTERNAL:
1057             s.type = eCT_Internal;
1058             s.description = "Internal sync";
1059             break;
1060         case MOTU_CLKSRC_ADAT_OPTICAL:
1061             s.type = eCT_ADAT;
1062             s.description = "ADAT optical";
1063             break;
1064         case MOTU_CLKSRC_SPDIF_TOSLINK:
1065             s.type = eCT_SPDIF;
1066             s.description = "SPDIF/Toslink";
1067             break;
1068         case MOTU_CLKSRC_SMPTE:
1069             s.type = eCT_SMPTE;
1070             s.description = "SMPTE";
1071             // Since we don't currently know how to deal with SMPTE on these devices
1072             // make sure the SMPTE clock source is disabled.
1073             s.valid = false;
1074             s.active = false;
1075             s.locked = false;
1076             break;
1077         case MOTU_CLKSRC_WORDCLOCK:
1078             s.type = eCT_WordClock;
1079             s.description = "Wordclock";
1080             break;
1081         case MOTU_CLKSRC_ADAT_9PIN:
1082             s.type = eCT_ADAT;
1083             s.description = "ADAT 9-pin";
1084             break;
1085         case MOTU_CLKSRC_AES_EBU:
1086             s.type = eCT_AES;
1087             s.description = "AES/EBU";
1088             break;
1089         default:
1090             s.type = eCT_Invalid;
1091     }
1092
1093     s.slipping = false;
1094     return s;
1095 }
1096
1097 FFADODevice::ClockSourceVector
1098 MotuDevice::getSupportedClockSources() {
1099     FFADODevice::ClockSourceVector r;
1100     ClockSource s;
1101
1102     /* Form a list of clocks supported by MOTU interfaces */
1103     s = clockIdToClockSource(MOTU_CLKSRC_INTERNAL);
1104     r.push_back(s);
1105     s = clockIdToClockSource(MOTU_CLKSRC_ADAT_OPTICAL);
1106     r.push_back(s);
1107     s = clockIdToClockSource(MOTU_CLKSRC_SPDIF_TOSLINK);
1108     r.push_back(s);
1109     s = clockIdToClockSource(MOTU_CLKSRC_SMPTE);
1110     r.push_back(s);
1111     s = clockIdToClockSource(MOTU_CLKSRC_WORDCLOCK);
1112     r.push_back(s);
1113     s = clockIdToClockSource(MOTU_CLKSRC_ADAT_9PIN);
1114     r.push_back(s);
1115     s = clockIdToClockSource(MOTU_CLKSRC_AES_EBU);
1116     r.push_back(s);
1117
1118     return r;
1119 }
1120
1121 bool
1122 MotuDevice::setActiveClockSource(ClockSource s) {
1123     debugOutput(DEBUG_LEVEL_VERBOSE, "setting clock source to id: %d\n",s.id);
1124
1125     // FIXME: this could do with some error checking
1126     return setClockCtrlRegister(-1, s.id);
1127 }
1128
1129 FFADODevice::ClockSource
1130 MotuDevice::getActiveClockSource() {
1131     ClockSource s;
1132     quadlet_t clock_id = ReadRegister(MOTU_REG_CLK_CTRL) & MOTU_CLKSRC_MASK;
1133     s = clockIdToClockSource(clock_id);
1134     s.active = true;
1135     return s;
1136 }
1137
1138 bool
1139 MotuDevice::lock() {
1140
1141     return true;
1142 }
1143
1144
1145 bool
1146 MotuDevice::unlock() {
1147
1148     return true;
1149 }
1150
1151 void
1152 MotuDevice::showDevice()
1153 {
1154     debugOutput(DEBUG_LEVEL_VERBOSE,
1155         "%s %s at node %d\n", m_model->vendor_name, m_model->model_name,
1156         getNodeId());
1157 }
1158
1159 bool
1160 MotuDevice::prepare() {
1161
1162     int samp_freq = getSamplingFrequency();
1163     unsigned int optical_in_mode = getOpticalMode(MOTU_DIR_IN);
1164     unsigned int optical_out_mode = getOpticalMode(MOTU_DIR_OUT);
1165     unsigned int event_size_in = getEventSize(MOTU_DIR_IN);
1166     unsigned int event_size_out= getEventSize(MOTU_DIR_OUT);
1167
1168     debugOutput(DEBUG_LEVEL_NORMAL, "Preparing MotuDevice...\n" );
1169
1170     // Explicitly set the optical mode, primarily to ensure that the
1171     // MOTU_REG_OPTICAL_CTRL register is initialised.  We need to do this to
1172     // because some interfaces (the Ultralite for example) appear to power
1173     // up without this set to anything sensible.  In this case, writes to
1174     // MOTU_REG_ISOCTRL fail more often than not, which is bad.
1175     setOpticalMode(MOTU_DIR_IN, optical_in_mode);
1176     setOpticalMode(MOTU_DIR_OUT, optical_out_mode);
1177
1178     // Allocate bandwidth if not previously done.
1179     // FIXME: The bandwidth allocation calculation can probably be
1180     // refined somewhat since this is currently based on a rudimentary
1181     // understanding of the ieee1394 iso protocol.
1182     // Currently we assume the following.
1183     //   * Ack/iso gap = 0.05 us
1184     //   * DATA_PREFIX = 0.16 us
1185     //   * DATA_END    = 0.26 us
1186     // These numbers are the worst-case figures given in the ieee1394
1187     // standard.  This gives approximately 0.5 us of overheads per packet -
1188     // around 25 bandwidth allocation units (from the ieee1394 standard 1
1189     // bandwidth allocation unit is 125/6144 us).  We further assume the
1190     // MOTU is running at S400 (which it should be) so one allocation unit
1191     // is equivalent to 1 transmitted byte; thus the bandwidth allocation
1192     // required for the packets themselves is just the size of the packet.
1193     // We used to allocate based on the maximum packet size (1160 bytes at
1194     // 192 kHz for the traveler) but now do this based on the actual device
1195     // state by utilising the result from getEventSize() and remembering
1196     // that each packet has an 8 byte CIP header.  Note that bandwidth is
1197     // allocated on a *per stream* basis - it must be allocated for both the
1198     // transmit and receive streams.  While most MOTU modules are close to
1199     // symmetric in terms of the number of in/out channels there are
1200     // exceptions, so we deal with receive and transmit bandwidth separately.
1201     signed int n_events_per_packet = samp_freq<=48000?8:(samp_freq<=96000?16:32);
1202     m_rx_bandwidth = 25 + (n_events_per_packet*event_size_in);
1203     m_tx_bandwidth = 25 + (n_events_per_packet*event_size_out);
1204
1205     // Assign iso channels if not already done
1206     if (m_iso_recv_channel < 0)
1207         m_iso_recv_channel = get1394Service().allocateIsoChannelGeneric(m_rx_bandwidth);
1208
1209     if (m_iso_send_channel < 0)
1210         m_iso_send_channel = get1394Service().allocateIsoChannelGeneric(m_tx_bandwidth);
1211
1212     debugOutput(DEBUG_LEVEL_VERBOSE, "recv channel = %d, send channel = %d\n",
1213         m_iso_recv_channel, m_iso_send_channel);
1214
1215     if (m_iso_recv_channel<0 || m_iso_send_channel<0) {
1216         // be nice and deallocate
1217         if (m_iso_recv_channel >= 0)
1218             get1394Service().freeIsoChannel(m_iso_recv_channel);
1219         if (m_iso_send_channel >= 0)
1220             get1394Service().freeIsoChannel(m_iso_send_channel);
1221
1222         debugFatal("Could not allocate iso channels!\n");
1223         return false;
1224     }
1225
1226     m_receiveProcessor=new Streaming::MotuReceiveStreamProcessor(*this, event_size_in);
1227
1228     // The first thing is to initialize the processor.  This creates the
1229     // data structures.
1230     if(!m_receiveProcessor->init()) {
1231         debugFatal("Could not initialize receive processor!\n");
1232         return false;
1233     }
1234     m_receiveProcessor->setVerboseLevel(getDebugLevel());
1235
1236     // Now we add ports to the processor
1237     debugOutput(DEBUG_LEVEL_VERBOSE,"Adding ports to receive processor\n");
1238
1239     char *buff;
1240     Streaming::Port *p=NULL;
1241
1242     // retrieve the ID
1243     std::string id=std::string("dev?");
1244     if(!getOption("id", id)) {
1245         debugWarning("Could not retrieve id parameter, defaulting to 'dev?'\n");
1246     }
1247
1248     // Add audio capture ports
1249     if (!addDirPorts(Streaming::Port::E_Capture, samp_freq, optical_in_mode)) {
1250         return false;
1251     }
1252
1253     // Add MIDI port.  The MOTU only has one MIDI input port, with each
1254     // MIDI byte sent using a 3 byte sequence starting at byte 4 of the
1255     // event data.
1256     asprintf(&buff,"%s_cap_MIDI0",id.c_str());
1257     p = new Streaming::MotuMidiPort(*m_receiveProcessor, buff,
1258         Streaming::Port::E_Capture, 4);
1259     if (!p) {
1260         debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n", buff);
1261     }
1262     free(buff);
1263
1264     // example of adding an control port:
1265 //    asprintf(&buff,"%s_cap_%s",id.c_str(),"myportnamehere");
1266 //    p=new Streaming::MotuControlPort(
1267 //            buff,
1268 //            Streaming::Port::E_Capture,
1269 //            0 // you can add all other port specific stuff you
1270 //              // need to pass by extending MotuXXXPort and MotuPortInfo
1271 //    );
1272 //    free(buff);
1273 //
1274 //    if (!p) {
1275 //        debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n",buff);
1276 //    } else {
1277 //
1278 //        if (!m_receiveProcessor->addPort(p)) {
1279 //            debugWarning("Could not register port with stream processor\n");
1280 //            return false;
1281 //        } else {
1282 //            debugOutput(DEBUG_LEVEL_VERBOSE, "Added port %s\n",buff);
1283 //        }
1284 //    }
1285
1286     // Do the same for the transmit processor
1287     m_transmitProcessor=new Streaming::MotuTransmitStreamProcessor(*this, event_size_out);
1288
1289     m_transmitProcessor->setVerboseLevel(getDebugLevel());
1290
1291     if(!m_transmitProcessor->init()) {
1292         debugFatal("Could not initialize transmit processor!\n");
1293         return false;
1294     }
1295
1296     // Now we add ports to the processor
1297     debugOutput(DEBUG_LEVEL_VERBOSE,"Adding ports to transmit processor\n");
1298
1299     // Add audio playback ports
1300     if (!addDirPorts(Streaming::Port::E_Playback, samp_freq, optical_out_mode)) {
1301         return false;
1302     }
1303
1304     // Add MIDI port.  The MOTU only has one output MIDI port, with each
1305     // MIDI byte transmitted using a 3 byte sequence starting at byte 4
1306     // of the event data.
1307     asprintf(&buff,"%s_pbk_MIDI0",id.c_str());
1308     p = new Streaming::MotuMidiPort(*m_transmitProcessor, buff,
1309         Streaming::Port::E_Capture, 4);
1310     if (!p) {
1311         debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n", buff);
1312     }
1313     free(buff);
1314
1315     // example of adding an control port:
1316 //    asprintf(&buff,"%s_pbk_%s",id.c_str(),"myportnamehere");
1317 //
1318 //    p=new Streaming::MotuControlPort(
1319 //            buff,
1320 //            Streaming::Port::E_Playback,
1321 //            0 // you can add all other port specific stuff you
1322 //              // need to pass by extending MotuXXXPort and MotuPortInfo
1323 //    );
1324 //    free(buff);
1325 //
1326 //    if (!p) {
1327 //        debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n",buff);
1328 //    } else {
1329 //        if (!m_transmitProcessor->addPort(p)) {
1330 //            debugWarning("Could not register port with stream processor\n");
1331 //            return false;
1332 //        } else {
1333 //            debugOutput(DEBUG_LEVEL_VERBOSE, "Added port %s\n",buff);
1334 //        }
1335 //    }
1336
1337     return true;
1338 }
1339
1340 int
1341 MotuDevice::getStreamCount() {
1342      return 2; // one receive, one transmit
1343 }
1344
1345 Streaming::StreamProcessor *
1346 MotuDevice::getStreamProcessorByIndex(int i) {
1347     switch (i) {
1348     case 0:
1349         return m_receiveProcessor;
1350     case 1:
1351          return m_transmitProcessor;
1352     default:
1353         return NULL;
1354     }
1355     return 0;
1356 }
1357
1358 bool
1359 MotuDevice::startStreamByIndex(int i) {
1360
1361 quadlet_t isoctrl = ReadRegister(MOTU_REG_ISOCTRL);
1362
1363     // NOTE: this assumes that you have two streams
1364     switch (i) {
1365     case 0:
1366         // TODO: do the stuff that is nescessary to make the device
1367         // receive a stream
1368
1369         // Set the streamprocessor channel to the one obtained by
1370         // the connection management
1371         m_receiveProcessor->setChannel(m_iso_recv_channel);
1372
1373         // Mask out current transmit settings of the MOTU and replace
1374         // with new ones.  Turn bit 24 on to enable changes to the
1375         // MOTU's iso transmit settings when the iso control register
1376         // is written.  Bit 23 enables iso transmit from the MOTU.
1377         isoctrl &= 0xff00ffff;
1378         isoctrl |= (m_iso_recv_channel << 16);
1379         isoctrl |= 0x00c00000;
1380         WriteRegister(MOTU_REG_ISOCTRL, isoctrl);
1381         break;
1382     case 1:
1383         // TODO: do the stuff that is nescessary to make the device
1384         // transmit a stream
1385
1386         // Set the streamprocessor channel to the one obtained by
1387         // the connection management
1388         m_transmitProcessor->setChannel(m_iso_send_channel);
1389
1390         // Mask out current receive settings of the MOTU and replace
1391         // with new ones.  Turn bit 31 on to enable changes to the
1392         // MOTU's iso receive settings when the iso control register
1393         // is written.  Bit 30 enables iso receive by the MOTU.
1394         isoctrl &= 0x00ffffff;
1395         isoctrl |= (m_iso_send_channel << 24);
1396         isoctrl |= 0xc0000000;
1397         WriteRegister(MOTU_REG_ISOCTRL, isoctrl);
1398         break;
1399
1400     default: // Invalid stream index
1401         return false;
1402     }
1403
1404     return true;
1405 }
1406
1407 bool
1408 MotuDevice::stopStreamByIndex(int i) {
1409
1410 quadlet_t isoctrl = ReadRegister(MOTU_REG_ISOCTRL);
1411
1412     // TODO: connection management: break connection
1413     // cfr the start function
1414
1415     // NOTE: this assumes that you have two streams
1416     switch (i) {
1417     case 0:
1418         // Turn bit 22 off to disable iso send by the MOTU.  Turn
1419         // bit 23 on to enable changes to the MOTU's iso transmit
1420         // settings when the iso control register is written.
1421         isoctrl &= 0xffbfffff;
1422         isoctrl |= 0x00800000;
1423         WriteRegister(MOTU_REG_ISOCTRL, isoctrl);
1424         break;
1425     case 1:
1426         // Turn bit 30 off to disable iso receive by the MOTU.  Turn
1427         // bit 31 on to enable changes to the MOTU's iso receive
1428         // settings when the iso control register is written.
1429         isoctrl &= 0xbfffffff;
1430         isoctrl |= 0x80000000;
1431         WriteRegister(MOTU_REG_ISOCTRL, isoctrl);
1432         break;
1433
1434     default: // Invalid stream index
1435         return false;
1436     }
1437
1438     return true;
1439 }
1440
1441 signed int MotuDevice::getIsoRecvChannel(void) {
1442     return m_iso_recv_channel;
1443 }
1444
1445 signed int MotuDevice::getIsoSendChannel(void) {
1446     return m_iso_send_channel;
1447 }
1448
1449 unsigned int MotuDevice::getOpticalMode(unsigned int dir) {
1450     unsigned int reg = ReadRegister(MOTU_REG_ROUTE_PORT_CONF);
1451
1452 debugOutput(DEBUG_LEVEL_VERBOSE, "optical mode: %x %x %x %x\n",dir, reg, reg & MOTU_OPTICAL_IN_MODE_MASK,
1453 reg & MOTU_OPTICAL_OUT_MODE_MASK);
1454
1455     if (dir == MOTU_DIR_IN)
1456         return (reg & MOTU_OPTICAL_IN_MODE_MASK) >> 8;
1457     else
1458         return (reg & MOTU_OPTICAL_OUT_MODE_MASK) >> 10;
1459 }
1460
1461 signed int MotuDevice::setOpticalMode(unsigned int dir, unsigned int mode) {
1462     unsigned int reg = ReadRegister(MOTU_REG_ROUTE_PORT_CONF);
1463     unsigned int opt_ctrl = 0x0000002;
1464
1465     /* THe 896HD doesn't have an SPDIF/TOSLINK optical mode, so don't try to
1466      * set it
1467      */
1468     if (m_motu_model==MOTU_MODEL_896HD && mode==MOTU_OPTICAL_MODE_TOSLINK)
1469         return -1;
1470
1471     // Set up the optical control register value according to the current
1472     // optical port modes.  At this stage it's not completely understood
1473     // what the "Optical control" register does, so the values it's set to
1474     // are more or less "magic" numbers.
1475     if ((reg & MOTU_OPTICAL_IN_MODE_MASK) != (MOTU_OPTICAL_MODE_ADAT<<8))
1476         opt_ctrl |= 0x00000080;
1477     if ((reg & MOTU_OPTICAL_OUT_MODE_MASK) != (MOTU_OPTICAL_MODE_ADAT<<10))
1478         opt_ctrl |= 0x00000040;
1479
1480     if (dir & MOTU_DIR_IN) {
1481         reg &= ~MOTU_OPTICAL_IN_MODE_MASK;
1482         reg |= (mode << 8) & MOTU_OPTICAL_IN_MODE_MASK;
1483         if (mode != MOTU_OPTICAL_MODE_ADAT)
1484             opt_ctrl |= 0x00000080;
1485         else
1486             opt_ctrl &= ~0x00000080;
1487     }
1488     if (dir & MOTU_DIR_OUT) {
1489         reg &= ~MOTU_OPTICAL_OUT_MODE_MASK;
1490         reg |= (mode <<10) & MOTU_OPTICAL_OUT_MODE_MASK;
1491         if (mode != MOTU_OPTICAL_MODE_ADAT)
1492             opt_ctrl |= 0x00000040;
1493         else
1494             opt_ctrl &= ~0x00000040;
1495     }
1496
1497     // FIXME: there seems to be more to it than this, but for
1498     // the moment at least this seems to work.
1499     WriteRegister(MOTU_REG_ROUTE_PORT_CONF, reg);
1500     return WriteRegister(MOTU_REG_OPTICAL_CTRL, opt_ctrl);
1501 }
1502
1503 signed int MotuDevice::getEventSize(unsigned int direction) {
1504 //
1505 // Return the size in bytes of a single event sent to (dir==MOTU_OUT) or
1506 // from (dir==MOTU_IN) the MOTU as part of an iso data packet.
1507 //
1508 // FIXME: for performance it may turn out best to calculate the event
1509 // size in setOpticalMode and cache the result in a data field.  However,
1510 // as it stands this will not adapt to dynamic changes in sample rate - we'd
1511 // need a setFrameRate() for that.
1512 //
1513 // At the very least an event consists of the SPH (4 bytes) and the control/MIDI
1514 // bytes (6 bytes).
1515 // Note that all audio channels are sent using 3 bytes.
1516 signed int sample_rate = getSamplingFrequency();
1517 signed int optical_mode = getOpticalMode(direction);
1518 signed int size = 4+6;
1519
1520 unsigned int i;
1521 unsigned int dir = direction==Streaming::Port::E_Capture?MOTU_DIR_IN:MOTU_DIR_OUT;
1522 unsigned int flags = (1 << ( optical_mode + 4 ));
1523
1524     if ( sample_rate > 96000 )
1525         flags |= MOTU_PA_RATE_4x;
1526     else if ( sample_rate > 48000 )
1527         flags |= MOTU_PA_RATE_2x;
1528     else
1529         flags |= MOTU_PA_RATE_1x;
1530
1531     // Don't test for padding port flag here since we need to include such
1532     // pseudo-ports when calculating the event size.
1533     for (i=0; i < DevicesProperty[m_motu_model-1].n_port_entries; i++) {
1534         if (( DevicesProperty[m_motu_model-1].port_entry[i].port_dir & dir ) &&
1535            ( DevicesProperty[m_motu_model-1].port_entry[i].port_flags & MOTU_PA_RATE_MASK & flags ) &&
1536            ( DevicesProperty[m_motu_model-1].port_entry[i].port_flags & MOTU_PA_OPTICAL_MASK & flags )) {
1537             size += 3;
1538         }
1539     }
1540
1541     // Finally round size up to the next quadlet boundary
1542     return ((size+3)/4)*4;
1543 }
1544 /* ======================================================================= */
1545
1546 bool MotuDevice::addPort(Streaming::StreamProcessor *s_processor,
1547   char *name, enum Streaming::Port::E_Direction direction,
1548   int position, int size) {
1549 /*
1550  * Internal helper function to add a MOTU port to a given stream processor.
1551  * This just saves the unnecessary replication of what is essentially
1552  * boilerplate code.  Note that the port name is freed by this function
1553  * prior to exit.
1554  */
1555 Streaming::Port *p=NULL;
1556
1557     p = new Streaming::MotuAudioPort(*s_processor, name, direction, position, size);
1558
1559     if (!p) {
1560         debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n",name);
1561     }
1562     free(name);
1563     return true;
1564 }
1565 /* ======================================================================= */
1566
1567 bool MotuDevice::addDirPorts(
1568   enum Streaming::Port::E_Direction direction,
1569   unsigned int sample_rate, unsigned int optical_mode) {
1570 /*
1571  * Internal helper method: adds all required ports for the given direction
1572  * based on the indicated sample rate and optical mode.
1573  *
1574  * Notes: currently ports are not created if they are disabled due to sample
1575  * rate or optical mode.  However, it might be better to unconditionally
1576  * create all ports and just disable those which are not active.
1577  */
1578 const char *mode_str = direction==Streaming::Port::E_Capture?"cap":"pbk";
1579 Streaming::StreamProcessor *s_processor;
1580 unsigned int i;
1581 char *buff;
1582 unsigned int dir = direction==Streaming::Port::E_Capture?MOTU_DIR_IN:MOTU_DIR_OUT;
1583 unsigned int flags = (1 << ( optical_mode + 4 ));
1584
1585     if ( sample_rate > 96000 )
1586         flags |= MOTU_PA_RATE_4x;
1587     else if ( sample_rate > 48000 )
1588         flags |= MOTU_PA_RATE_2x;
1589     else
1590         flags |= MOTU_PA_RATE_1x;
1591
1592     // retrieve the ID
1593     std::string id=std::string("dev?");
1594     if(!getOption("id", id)) {
1595         debugWarning("Could not retrieve id parameter, defaulting to 'dev?'\n");
1596     }
1597
1598     if (direction == Streaming::Port::E_Capture) {
1599         s_processor = m_receiveProcessor;
1600     } else {
1601         s_processor = m_transmitProcessor;
1602     }
1603
1604     for (i=0; i < DevicesProperty[m_motu_model-1].n_port_entries; i++) {
1605         if (( DevicesProperty[m_motu_model-1].port_entry[i].port_dir & dir ) &&
1606            ( DevicesProperty[m_motu_model-1].port_entry[i].port_flags & MOTU_PA_RATE_MASK & flags ) &&
1607            ( DevicesProperty[m_motu_model-1].port_entry[i].port_flags & MOTU_PA_OPTICAL_MASK & flags ) &&
1608            !( DevicesProperty[m_motu_model-1].port_entry[i].port_flags & MOTU_PA_PADDING )) {
1609             asprintf(&buff,"%s_%s_%s" , id.c_str(), mode_str,
1610               DevicesProperty[m_motu_model-1].port_entry[i].port_name);
1611             if (!addPort(s_processor, buff, direction, DevicesProperty[m_motu_model-1].port_entry[i].port_offset, 0))
1612                 return false;
1613         }
1614     }
1615    
1616     return true;
1617 }
1618 /* ======================================================================== */
1619
1620 unsigned int MotuDevice::ReadRegister(unsigned int reg) {
1621 /*
1622  * Attempts to read the requested register from the MOTU.
1623  */
1624
1625   quadlet_t quadlet;
1626
1627   quadlet = 0;
1628   // Note: 1394Service::read() expects a physical ID, not the node id
1629   if (get1394Service().read(0xffc0 | getNodeId(), MOTU_BASE_ADDR+reg, 1, &quadlet) <= 0) {
1630     debugError("Error doing motu read from register 0x%06x\n",reg);
1631   }
1632
1633   return CondSwapFromBus32(quadlet);
1634 }
1635
1636 signed int MotuDevice::WriteRegister(unsigned int reg, quadlet_t data) {
1637 /*
1638  * Attempts to write the given data to the requested MOTU register.
1639  */
1640
1641   unsigned int err = 0;
1642   data = CondSwapToBus32(data);
1643
1644   // Note: 1394Service::write() expects a physical ID, not the node id
1645   if (get1394Service().write(0xffc0 | getNodeId(), MOTU_BASE_ADDR+reg, 1, &data) <= 0) {
1646     err = 1;
1647     debugError("Error doing motu write to register 0x%06x\n",reg);
1648   }
1649
1650   SleepRelativeUsec(100);
1651   return (err==0)?0:-1;
1652 }
1653
1654 }
Note: See TracBrowser for help on using the browser.