root/trunk/libffado/src/motu/motu_avdevice.cpp

Revision 1763, 56.6 kB (checked in by ppalmers, 13 years ago)

Merged revisions 1536,1541,1544-1546,1549,1554-1562,1571,1579-1581,1618,1632,1634-1635,1661,1677-1679,1703-1704,1715,1720-1723,1743-1745,1755 via svnmerge from
svn+ssh://ffadosvn@ffado.org/ffado/branches/libffado-2.0

Also fix remaining format string warnings.

Line 
1 /*
2  * Copyright (C) 2005-2008 by Pieter Palmers
3  * Copyright (C) 2005-2009 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 "config.h"
26
27 #include "motu/motu_avdevice.h"
28 #include "motu/motu_mixerdefs.h"
29 #include "motu/motu_mark3_mixerdefs.h"
30
31 #include "devicemanager.h"
32
33 #include "libieee1394/configrom.h"
34 #include "libieee1394/ieee1394service.h"
35
36 #include "libavc/avc_definitions.h"
37
38 #include "debugmodule/debugmodule.h"
39
40 #include "libstreaming/motu/MotuReceiveStreamProcessor.h"
41 #include "libstreaming/motu/MotuTransmitStreamProcessor.h"
42 #include "libstreaming/motu/MotuPort.h"
43
44 #include "libutil/Time.h"
45 #include "libutil/Configuration.h"
46
47 #include "libcontrol/BasicElements.h"
48
49 #include <string>
50 #include <stdint.h>
51 #include <assert.h>
52 #include "libutil/ByteSwap.h"
53 #include <iostream>
54 #include <sstream>
55
56 #include <libraw1394/csr.h>
57
58 namespace Motu {
59
60 // Define the supported devices.  Device ordering is arbitary here.
61 static VendorModelEntry supportedDeviceList[] =
62 {
63 //  {vendor_id, model_id, unit_version, unit_specifier_id, model, vendor_name,model_name}
64     {FW_VENDORID_MOTU, 0, 0x00000003, 0x000001f2, MOTU_MODEL_828mkII, "MOTU", "828MkII"},
65     {FW_VENDORID_MOTU, 0, 0x00000009, 0x000001f2, MOTU_MODEL_TRAVELER, "MOTU", "Traveler"},
66     {FW_VENDORID_MOTU, 0, 0x0000000d, 0x000001f2, MOTU_MODEL_ULTRALITE, "MOTU", "UltraLite"},
67     {FW_VENDORID_MOTU, 0, 0x0000000f, 0x000001f2, MOTU_MODEL_8PRE, "MOTU", "8pre"},
68     {FW_VENDORID_MOTU, 0, 0x00000001, 0x000001f2, MOTU_MODEL_828MkI, "MOTU", "828MkI"},
69     {FW_VENDORID_MOTU, 0, 0x00000005, 0x000001f2, MOTU_MODEL_896HD, "MOTU", "896HD"},
70     {FW_VENDORID_MOTU, 0, 0x00000015, 0x000001f2, MOTU_MODEL_828mk3, "MOTU", "828Mk3"},
71     {FW_VENDORID_MOTU, 0, 0x00000019, 0x000001f2, MOTU_MODEL_ULTRALITEmk3, "MOTU", "UltraLineMk3"},
72 };
73
74 // Ports declarations
75 const PortEntry Ports_828MKI[] =
76 {
77     {"Analog1", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 10},
78     {"Analog2", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 13},
79     {"Analog3", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 16},
80     {"Analog4", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 19},
81     {"Analog5", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 22},
82     {"Analog6", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 25},
83     {"Analog7", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 28},
84     {"Analog8", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 31},
85     {"SPDIF1", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 34},
86     {"SPDIF2", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 37},
87     {"ADAT1", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 40},
88     {"ADAT2", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 43},
89     {"ADAT3", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 46},
90     {"ADAT4", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 49},
91     {"ADAT5", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 52},
92     {"ADAT6", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 55},
93     {"ADAT7", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 58},
94     {"ADAT8", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 61},
95 };
96
97 const PortEntry Ports_896HD[] =
98 {
99     {"Mix-L", MOTU_PA_IN | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 10},
100     {"Mix-R", MOTU_PA_IN | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 13},
101     {"Phones-L", MOTU_PA_OUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 10},
102     {"Phones-R", MOTU_PA_OUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 13},
103     {"Analog1", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 16},
104     {"Analog1", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 10},
105     {"Analog2", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 19},
106     {"Analog2", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 13},
107     {"Analog3", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 22},
108     {"Analog3", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 16},
109     {"Analog4", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 25},
110     {"Analog4", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 19},
111     {"Analog5", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 28},
112     {"Analog5", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 22},
113     {"Analog6", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 31},
114     {"Analog6", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 25},
115     {"Analog7", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 34},
116     {"Analog7", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 28},
117     {"Analog8", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 37},
118     {"Analog8", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 31},
119     {"MainOut-L", MOTU_PA_OUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 40},
120     {"MainOut-R", MOTU_PA_OUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 43},
121     {"unknown-1", MOTU_PA_IN | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 40},
122     {"unknown-2", MOTU_PA_IN | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 43},
123     {"ADAT1", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 46},
124     {"ADAT2", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 49},
125     {"ADAT3", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 52},
126     {"ADAT4", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 55},
127     {"ADAT5", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 58},
128     {"ADAT6", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 61},
129     {"ADAT7", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 64},
130     {"ADAT8", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 67},
131     {"AES/EBU1", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ANY, 70},
132     {"AES/EBU2", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ANY, 73},
133     {"AES/EBU1", MOTU_PA_INOUT | MOTU_PA_RATE_2x|MOTU_PA_OPTICAL_ANY, 58},
134     {"AES/EBU2", MOTU_PA_INOUT | MOTU_PA_RATE_2x|MOTU_PA_OPTICAL_ANY, 61},
135 };
136
137 const PortEntry Ports_828MKII[] =
138 {
139     {"Main-L", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 40},
140     {"Main-R", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 43},
141     {"Mix-L", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 10},
142     {"Mix-R", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 13},
143     {"Analog1", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 16},
144     {"Analog2", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 19},
145     {"Analog3", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 22},
146     {"Analog4", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 25},
147     {"Analog5", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 28},
148     {"Analog6", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 31},
149     {"Analog7", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 34},
150     {"Analog8", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 37},
151     {"Phones-L", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 10},
152     {"Phones-R", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 13},
153     {"Mic1", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 40},
154     {"Mic2", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 43},
155     {"SPDIF1", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 46},
156     {"SPDIF2", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 49},
157     {"ADAT1", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 52},
158     {"ADAT2", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 55},
159     {"ADAT3", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 58},
160     {"ADAT4", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 61},
161     {"ADAT5", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 64},
162     {"ADAT6", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 67},
163     {"ADAT7", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 70},
164     {"ADAT8", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 73},
165 };
166
167 const PortEntry Ports_TRAVELER[] =
168 {
169     {"Mix-L", MOTU_PA_IN | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 10},
170     {"Mix-R", MOTU_PA_IN | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 13},
171     {"Phones-L", MOTU_PA_OUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 10},
172     {"Phones-R", MOTU_PA_OUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 13},
173     {"Analog1", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 16},
174     {"Analog1", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 10},
175     {"Analog2", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 19},
176     {"Analog2", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 13},
177     {"Analog3", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 22},
178     {"Analog3", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 16},
179     {"Analog4", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 25},
180     {"Analog4", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 19},
181     {"Analog5", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 28},
182     {"Analog5", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 22},
183     {"Analog6", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 31},
184     {"Analog6", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 25},
185     {"Analog7", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 34},
186     {"Analog7", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 28},
187     {"Analog8", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 37},
188     {"Analog8", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 31},
189     {"AES/EBU1", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 40},
190     {"AES/EBU2", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 43},
191     {"SPDIF1", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_OFF|MOTU_PA_OPTICAL_ADAT, 46},
192     {"SPDIF2", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_OFF|MOTU_PA_OPTICAL_ADAT, 49},
193     {"Toslink1", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_TOSLINK, 46},
194     {"Toslink2", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_TOSLINK, 49},
195     {"ADAT1", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 52},
196     {"ADAT2", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 55},
197     {"ADAT3", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 58},
198     {"ADAT4", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 61},
199     {"ADAT5", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 64},
200     {"ADAT6", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 67},
201     {"ADAT7", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 70},
202     {"ADAT8", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 73},
203 };
204
205 const PortEntry Ports_ULTRALITE[] =
206 {
207     {"Main-L", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 40},
208     {"Main-R", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 43},
209     {"Mix-L", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 10},
210     {"Mix-R", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 13},
211     {"Mic1", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 16},
212     {"Mic2", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 19},
213     {"Analog1", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 16},
214     {"Analog2", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 19},
215     {"Analog3", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 22},
216     {"Analog4", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 25},
217     {"Analog5", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 28},
218     {"Analog6", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 31},
219     {"Analog7", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 34},
220     {"Analog8", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 37},
221     {"Phones-L", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 10},
222     {"Phones-R", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 13},
223     {"SPDIF1", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 40},
224     {"SPDIF2", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 43},
225     {"Padding1", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY|MOTU_PA_PADDING, 46},
226     {"Padding2", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY|MOTU_PA_PADDING, 49},
227     {"SPDIF1", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 46},
228     {"SPDIF2", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 49},
229 };
230
231 const PortEntry Ports_8PRE[] =
232 {
233     {"Analog1", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 16},
234     {"Analog2", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 19},
235     {"Analog3", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 22},
236     {"Analog4", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 25},
237     {"Analog5", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 28},
238     {"Analog6", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 31},
239     {"Analog7", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 34},
240     {"Analog8", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 37},
241     {"Mix-L", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 10},
242     {"Mix-R", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 13},
243     {"Main-L", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 16},
244     {"Main-R", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 19},
245     {"Phones-L", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 10},
246     {"Phones-R", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 13},
247     {"ADAT1", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 40},
248     {"ADAT1", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 22},
249     {"ADAT2", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 43},
250     {"ADAT2", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 25},
251     {"ADAT3", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 46},
252     {"ADAT3", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 28},
253     {"ADAT4", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 49},
254     {"ADAT4", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 31},
255     {"ADAT5", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 52},
256     {"ADAT5", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 34},
257     {"ADAT6", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 55},
258     {"ADAT6", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 37},
259     {"ADAT7", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 58},
260     {"ADAT7", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 40},
261     {"ADAT8", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 61},
262     {"ADAT8", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 43},
263 };
264
265 const PortEntry Ports_828mk3[] =
266 {
267     {"Mix-L", MOTU_PA_IN | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 10},
268     {"Mix-R", MOTU_PA_IN | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 13},
269     {"Phones-L", MOTU_PA_OUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 10},
270     {"Phones-R", MOTU_PA_OUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 13},
271     {"Analog1", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 16},
272     {"Analog1", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 10},
273     {"Analog2", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 19},
274     {"Analog2", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 13},
275     {"Analog3", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 22},
276     {"Analog3", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 16},
277     {"Analog4", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 25},
278     {"Analog4", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 19},
279     {"Analog5", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 28},
280     {"Analog5", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 22},
281     {"Analog6", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 31},
282     {"Analog6", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 25},
283     {"Analog7", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 34},
284     {"Analog7", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 28},
285     {"Analog8", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 37},
286     {"Analog8", MOTU_PA_INOUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY, 31},
287     {"SPDIF1", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_OFF|MOTU_PA_OPTICAL_ADAT, 40},
288     {"SPDIF2", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_OFF|MOTU_PA_OPTICAL_ADAT, 43},
289     {"Padding1", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_OFF|MOTU_PA_OPTICAL_ADAT|MOTU_PA_PADDING, 46},
290     {"Padding2", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_OFF|MOTU_PA_OPTICAL_ADAT|MOTU_PA_PADDING, 49},
291     {"Toslink1", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_TOSLINK, 40},
292     {"Toslink2", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_TOSLINK, 43},
293     {"Toslink3", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_TOSLINK, 46},
294     {"Toslink4", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_TOSLINK, 49},
295     {"ADAT1", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 52},
296     {"ADAT2", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 55},
297     {"ADAT3", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 58},
298     {"ADAT4", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 61},
299     {"ADAT5", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 64},
300     {"ADAT6", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 67},
301     {"ADAT7", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 70},
302     {"ADAT8", MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ADAT, 73},
303     {"ADAT9", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 76},
304     {"ADAT10", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 79},
305     {"ADAT11", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 82},
306     {"ADAT12", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 85},
307     {"ADAT13", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 88},
308     {"ADAT14", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 91},
309     {"ADAT15", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 94},
310     {"ADAT16", MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 97},
311 };
312
313 const PortEntry Ports_ULTRALITEmk3[] =
314 {
315     {"Main-L", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 40},
316     {"Main-R", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 43},
317     {"Mix-L", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 10},
318     {"Mix-R", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 13},
319     {"Mic1", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 16},
320     {"Mic2", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 19},
321     {"Analog1", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 16},
322     {"Analog2", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 19},
323     {"Analog3", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 22},
324     {"Analog4", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 25},
325     {"Analog5", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 28},
326     {"Analog6", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 31},
327     {"Analog7", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 34},
328     {"Analog8", MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 37},
329     {"Phones-L", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 10},
330     {"Phones-R", MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 13},
331     {"SPDIF1", MOTU_PA_IN | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 40},
332     {"SPDIF2", MOTU_PA_IN | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 43},
333     {"Padding1", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY|MOTU_PA_PADDING, 46},
334     {"Padding2", MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY|MOTU_PA_PADDING, 49},
335     {"SPDIF1", MOTU_PA_OUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 46},
336     {"SPDIF2", MOTU_PA_OUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 49},
337 };
338
339 /* The order of DevicesProperty entries must match the numeric order of the
340  * MOTU model enumeration (EMotuModel).
341  */
342 const DevicePropertyEntry DevicesProperty[] = {
343 //  { Ports_map,          N_ELEMENTS( Ports_map ),        MaxSR, MixerDescrPtr, Mark3MixerDescrPtr },
344     { Ports_828MKII,      N_ELEMENTS( Ports_828MKII ),       96000, &Mixer_828Mk2, NULL, },
345     { Ports_TRAVELER,     N_ELEMENTS( Ports_TRAVELER ),     192000, &Mixer_Traveler, NULL, },
346     { Ports_ULTRALITE,    N_ELEMENTS( Ports_ULTRALITE ),     96000, &Mixer_Ultralite, NULL, },
347     { Ports_8PRE,         N_ELEMENTS( Ports_8PRE ),          96000, &Mixer_8pre, NULL, },
348     { Ports_828MKI,       N_ELEMENTS( Ports_828MKI ),        48000 },
349     { Ports_896HD,        N_ELEMENTS( Ports_896HD ),        192000, &Mixer_896HD, NULL, },
350     { Ports_828mk3,       N_ELEMENTS( Ports_828mk3 ),       192000 },
351     { Ports_ULTRALITEmk3, N_ELEMENTS( Ports_ULTRALITEmk3 ), 192000 },
352 };
353
354 MotuDevice::MotuDevice( DeviceManager& d, std::auto_ptr<ConfigRom>( configRom ))
355     : FFADODevice( d, configRom )
356     , m_motu_model( MOTU_MODEL_NONE )
357     , m_iso_recv_channel ( -1 )
358     , m_iso_send_channel ( -1 )
359     , m_rx_bandwidth ( -1 )
360     , m_tx_bandwidth ( -1 )
361     , m_receiveProcessor ( 0 )
362     , m_transmitProcessor ( 0 )
363     , m_MixerContainer ( NULL )
364     , m_ControlContainer ( NULL )
365 {
366     debugOutput( DEBUG_LEVEL_VERBOSE, "Created Motu::MotuDevice (NodeID %d)\n",
367                  getConfigRom().getNodeId() );
368 }
369
370 MotuDevice::~MotuDevice()
371 {
372     delete m_receiveProcessor;
373     delete m_transmitProcessor;
374
375     // Free ieee1394 bus resources if they have been allocated
376     if (m_iso_recv_channel>=0 && !get1394Service().freeIsoChannel(m_iso_recv_channel)) {
377         debugOutput(DEBUG_LEVEL_VERBOSE, "Could not free recv iso channel %d\n", m_iso_recv_channel);
378     }
379     if (m_iso_send_channel>=0 && !get1394Service().freeIsoChannel(m_iso_send_channel)) {
380         debugOutput(DEBUG_LEVEL_VERBOSE, "Could not free send iso channel %d\n", m_iso_send_channel);
381     }
382
383     destroyMixer();
384 }
385
386 bool
387 MotuDevice::probe( Util::Configuration& c, ConfigRom& configRom, bool generic)
388 {
389     if(generic) return false;
390
391     unsigned int vendorId = configRom.getNodeVendorId();
392     unsigned int unitVersion = configRom.getUnitVersion();
393     unsigned int unitSpecifierId = configRom.getUnitSpecifierId();
394
395     for ( unsigned int i = 0;
396           i < ( sizeof( supportedDeviceList )/sizeof( VendorModelEntry ) );
397           ++i )
398     {
399         if ( ( supportedDeviceList[i].vendor_id == vendorId )
400              && ( supportedDeviceList[i].unit_version == unitVersion )
401              && ( supportedDeviceList[i].unit_specifier_id == unitSpecifierId )
402            )
403         {
404             return true;
405         }
406     }
407
408     return false;
409 }
410
411 FFADODevice *
412 MotuDevice::createDevice(DeviceManager& d, std::auto_ptr<ConfigRom>( configRom ))
413 {
414     return new MotuDevice(d, configRom);
415 }
416
417 bool
418 MotuDevice::discover()
419 {
420     unsigned int vendorId = getConfigRom().getNodeVendorId();
421     unsigned int unitVersion = getConfigRom().getUnitVersion();
422     unsigned int unitSpecifierId = getConfigRom().getUnitSpecifierId();
423
424     for ( unsigned int i = 0;
425           i < ( sizeof( supportedDeviceList )/sizeof( VendorModelEntry ) );
426           ++i )
427     {
428         if ( ( supportedDeviceList[i].vendor_id == vendorId )
429              && ( supportedDeviceList[i].unit_version == unitVersion )
430              && ( supportedDeviceList[i].unit_specifier_id == unitSpecifierId )
431            )
432         {
433             m_model = &(supportedDeviceList[i]);
434             m_motu_model=supportedDeviceList[i].model;
435         }
436     }
437
438     if (m_model == NULL) {
439         return false;
440     }
441
442     debugOutput( DEBUG_LEVEL_VERBOSE, "found %s %s\n",
443         m_model->vendor_name, m_model->model_name);
444
445     if (!buildMixer()) {
446         debugWarning("Could not build mixer\n");
447     }
448
449     return true;
450 }
451
452 enum FFADODevice::eStreamingState
453 MotuDevice::getStreamingState()
454 {
455     unsigned int val = ReadRegister(MOTU_REG_ISOCTRL);
456     /* Streaming is active if either bit 22 (Motu->PC streaming
457      * enable) or bit 30 (PC->Motu streaming enable) is set.
458      */
459     debugOutput(DEBUG_LEVEL_VERBOSE, "MOTU_REG_ISOCTRL: %08x\n", val);
460
461     if((val & 0x40400000) != 0) {
462         return eSS_Both;
463     } else if ((val & 0x40000000) != 0) {
464         return eSS_Receiving;
465     } else if ((val & 0x00400000) != 0) {
466         return eSS_Sending;
467     } else {
468         return eSS_Idle;
469     }
470 }
471
472 int
473 MotuDevice::getSamplingFrequency( ) {
474 /*
475  * Retrieve the current sample rate from the MOTU device.
476  */
477     quadlet_t q = 0;
478     int rate = 0;
479
480     if (m_motu_model == MOTU_MODEL_828MkI) {
481         /* The original MOTU interfaces did things rather differently */
482         q = ReadRegister(MOTU_G1_REG_CONFIG);
483         if ((q & MOTU_G1_RATE_MASK) == MOTU_G1_RATE_44100)
484             rate = 44100;
485         else
486             rate = 48000;
487         return rate;
488     }
489
490     q = ReadRegister(MOTU_REG_CLK_CTRL);
491     switch (q & MOTU_RATE_BASE_MASK) {
492         case MOTU_RATE_BASE_44100:
493             rate = 44100;
494             break;
495         case MOTU_RATE_BASE_48000:
496             rate = 48000;
497             break;
498     }
499     switch (q & MOTU_RATE_MULTIPLIER_MASK) {
500         case MOTU_RATE_MULTIPLIER_2X:
501             rate *= 2;
502             break;
503         case MOTU_RATE_MULTIPLIER_4X:
504             rate *= 4;
505             break;
506     }
507     return rate;
508 }
509
510 int
511 MotuDevice::getConfigurationId()
512 {
513     return 0;
514 }
515
516 bool
517 MotuDevice::setClockCtrlRegister(signed int samplingFrequency, unsigned int clock_source)
518 {
519 /*
520  * Set the MOTU device's samplerate and/or clock source via the clock
521  * control register.  If samplingFrequency <= 0 it remains unchanged.  If
522  * clock_source is MOTU_CLKSRC_UNCHANGED the clock source remains unchanged.
523  */
524     const char *src_name;
525     quadlet_t q, new_rate=0xffffffff;
526     int i, supported=true, cancel_adat=false;
527     quadlet_t reg;
528
529     /* Don't touch anything if there's nothing to do */
530     if (samplingFrequency<=0 && clock_source==MOTU_CLKSRC_NONE)
531         return true;
532
533     if ( samplingFrequency > DevicesProperty[m_motu_model-1].MaxSampleRate )
534        return false;
535
536     /* The original MOTU devices do things differently; they are much
537      * simpler than the later interfaces.
538      */
539     if (m_motu_model == MOTU_MODEL_828MkI) {
540         reg = ReadRegister(MOTU_G1_REG_CONFIG);
541         if (samplingFrequency > 0) {
542             reg &= ~MOTU_G1_RATE_MASK;
543             switch (samplingFrequency) {
544                 case 44100:
545                     reg |= MOTU_G1_RATE_44100;
546                     break;
547                 case 48000:
548                     reg |= MOTU_G1_RATE_48000;
549                 default:
550                     // Unsupported rate
551                     return false;
552             }
553         }
554         if (clock_source != MOTU_CLKSRC_UNCHANGED) {
555             switch (clock_source) {
556                 case MOTU_CLKSRC_INTERNAL:
557                     clock_source = MOTU_G1_CLKSRC_INTERNAL; break;
558                 case MOTU_CLKSRC_SPDIF_TOSLINK:
559                     clock_source = MOTU_G1_CLKSRC_SPDIF; break;
560                 case MOTU_CLKSRC_ADAT_9PIN:
561                     clock_source = MOTU_G1_CLKSRC_ADAT_9PIN; break;
562                 default:
563                     // Unsupported clock source
564                     return false;
565             }
566             reg &= ~MOTU_G1_CLKSRC_MASK;
567             reg |= clock_source;
568         }
569         if (WriteRegister(MOTU_G1_REG_CONFIG, reg) != 0)
570             return false;
571         return true;
572     }
573
574     /* The rest of this function deals with later generation devices */
575
576     reg = ReadRegister(MOTU_REG_CLK_CTRL);
577
578     switch ( samplingFrequency ) {
579         case -1:
580             break;
581         case 44100:
582             new_rate = MOTU_RATE_BASE_44100 | MOTU_RATE_MULTIPLIER_1X;
583             break;
584         case 48000:
585             new_rate = MOTU_RATE_BASE_48000 | MOTU_RATE_MULTIPLIER_1X;
586             break;
587         case 88200:
588             new_rate = MOTU_RATE_BASE_44100 | MOTU_RATE_MULTIPLIER_2X;
589             break;
590         case 96000:
591             new_rate = MOTU_RATE_BASE_48000 | MOTU_RATE_MULTIPLIER_2X;
592             break;
593         case 176400:
594             new_rate = MOTU_RATE_BASE_44100 | MOTU_RATE_MULTIPLIER_4X;
595             cancel_adat = true;  // current ADAT protocol doesn't support sample rate > 96000
596             break;
597         case 192000:
598             new_rate = MOTU_RATE_BASE_48000 | MOTU_RATE_MULTIPLIER_4X;
599             cancel_adat = true;
600             break;
601         default:
602             supported=false;
603     }
604
605     // Sanity check the clock source
606     if ((clock_source>7 || clock_source==6) && clock_source!=MOTU_CLKSRC_UNCHANGED)
607         supported = false;
608
609     // Update the clock control register.  FIXME: while this is now rather
610     // comprehensive there may still be a need to manipulate MOTU_REG_CLK_CTRL
611     // a little more than we do.
612     if (supported) {
613
614         // If optical port must be disabled (because a 4x sample rate has
615         // been selected) then do so before changing the sample rate.  At
616         // this stage it will be up to the user to re-enable the optical
617         // port if the sample rate is set to a 1x or 2x rate later.
618         if (cancel_adat) {
619             setOpticalMode(MOTU_CTRL_DIR_INOUT, MOTU_OPTICAL_MODE_OFF);
620         }
621
622         // Set up new frequency if requested
623         if (new_rate != 0xffffffff) {
624             reg &= ~(MOTU_RATE_BASE_MASK|MOTU_RATE_MULTIPLIER_MASK);
625             reg |= new_rate;
626         }
627
628         // Set up new clock source if required
629         if (clock_source != MOTU_CLKSRC_UNCHANGED) {
630             reg &= ~MOTU_CLKSRC_MASK;
631             reg |= (clock_source & MOTU_CLKSRC_MASK);
632         }
633
634         // Bits 24-26 of MOTU_REG_CLK_CTRL behave a little differently
635         // depending on the model.  In addition, different bit patterns are
636         // written depending on whether streaming is enabled, disabled or is
637         // changing state.  For now we go with the combination used when
638         // streaming is enabled since it seems to work for the other states
639         // as well.  Since device muting can be effected by these bits, we
640         // may utilise this in future during streaming startup to prevent
641         // noises during stabilisation.
642         //
643         // For most models (possibly all except the Ultralite) all 3 bits
644         // can be zero and audio is still output.
645         //
646         // For the Traveler, if bit 26 is set (as it is under other OSes),
647         // bit 25 functions as a device mute bit: if set, audio is output
648         // while if 0 the entire device is muted.  If bit 26 is unset,
649         // setting bit 25 doesn't appear to be detrimental.
650         //
651         // For the Ultralite, other OSes leave bit 26 unset.  However, unlike
652         // other devices bit 25 seems to function as a mute bit in this case.
653         //
654         // The function of bit 24 is currently unknown.  Other OSes set it
655         // for all devices so we will too.
656         reg &= 0xf8ffffff;
657         if (m_motu_model == MOTU_MODEL_TRAVELER)
658             reg |= 0x04000000;
659         reg |= 0x03000000;
660         if (WriteRegister(MOTU_REG_CLK_CTRL, reg) == 0) {
661             supported=true;
662         } else {
663             supported=false;
664         }
665         // A write to the rate/clock control register requires the
666         // textual name of the current clock source be sent to the
667         // clock source name registers.
668         switch (reg & MOTU_CLKSRC_MASK) {
669             case MOTU_CLKSRC_INTERNAL:
670                 src_name = "Internal        ";
671                 break;
672             case MOTU_CLKSRC_ADAT_OPTICAL:
673                 src_name = "ADAT Optical    ";
674                 break;
675             case MOTU_CLKSRC_SPDIF_TOSLINK:
676                 if (getOpticalMode(MOTU_DIR_IN) == MOTU_OPTICAL_MODE_TOSLINK)
677                     src_name = "TOSLink         ";
678                 else
679                     src_name = "SPDIF           ";
680                 break;
681             case MOTU_CLKSRC_SMPTE:
682                 src_name = "SMPTE           ";
683                 break;
684             case MOTU_CLKSRC_WORDCLOCK:
685                 src_name = "Word Clock In   ";
686                 break;
687             case MOTU_CLKSRC_ADAT_9PIN:
688                 src_name = "ADAT 9-pin      ";
689                 break;
690             case MOTU_CLKSRC_AES_EBU:
691                 src_name = "AES-EBU         ";
692                 break;
693             default:
694                 src_name = "Unknown         ";
695         }
696         for (i=0; i<16; i+=4) {
697             q = (src_name[i]<<24) | (src_name[i+1]<<16) |
698                 (src_name[i+2]<<8) | src_name[i+3];
699             WriteRegister(MOTU_REG_CLKSRC_NAME0+i, q);
700         }
701     }
702     return supported;
703 }
704
705 bool
706 MotuDevice::setSamplingFrequency( int samplingFrequency )
707 {
708 /*
709  * Set the MOTU device's samplerate.
710  */
711     return setClockCtrlRegister(samplingFrequency, MOTU_CLKSRC_UNCHANGED);
712 }
713
714 std::vector<int>
715 MotuDevice::getSupportedSamplingFrequencies()
716 {
717     std::vector<int> frequencies;
718     signed int max_freq = DevicesProperty[m_motu_model-1].MaxSampleRate;
719
720     /* All MOTUs support 1x rates.  All others must be conditional. */
721     frequencies.push_back(44100);
722     frequencies.push_back(48000);
723
724     if (88200 <= max_freq)
725         frequencies.push_back(88200);
726     if (96000 <= max_freq)
727         frequencies.push_back(96000);
728     if (176400 <= max_freq)
729         frequencies.push_back(176400);
730     if (192000 <= max_freq)
731         frequencies.push_back(192000);
732     return frequencies;
733 }
734
735 FFADODevice::ClockSource
736 MotuDevice::clockIdToClockSource(unsigned int id) {
737     ClockSource s;
738     bool g1_model = (m_motu_model == MOTU_MODEL_828MkI);
739     s.id = id;
740
741     // Assume a clock source is valid/active unless otherwise overridden.
742     s.valid = true;
743     s.locked = true;
744     s.active = true;
745
746     switch (id) {
747         case MOTU_CLKSRC_INTERNAL:
748             s.type = eCT_Internal;
749             s.description = "Internal sync";
750             break;
751         case MOTU_CLKSRC_ADAT_OPTICAL:
752             s.type = eCT_ADAT;
753             s.description = "ADAT optical";
754             s.valid = s.active = s.locked = !g1_model;
755             break;
756         case MOTU_CLKSRC_SPDIF_TOSLINK:
757             s.type = eCT_SPDIF;
758             s.description = "SPDIF/Toslink";
759             break;
760         case MOTU_CLKSRC_SMPTE:
761             s.type = eCT_SMPTE;
762             s.description = "SMPTE";
763             // Since we don't currently know how to deal with SMPTE on these devices
764             // make sure the SMPTE clock source is disabled.
765             s.valid = false;
766             s.active = false;
767             s.locked = false;
768             break;
769         case MOTU_CLKSRC_WORDCLOCK:
770             s.type = eCT_WordClock;
771             s.description = "Wordclock";
772             s.valid = s.active = s.locked = !g1_model;
773             break;
774         case MOTU_CLKSRC_ADAT_9PIN:
775             s.type = eCT_ADAT;
776             s.description = "ADAT 9-pin";
777             break;
778         case MOTU_CLKSRC_AES_EBU:
779             s.type = eCT_AES;
780             s.description = "AES/EBU";
781             s.valid = s.active = s.locked = !g1_model;
782             break;
783         default:
784             s.type = eCT_Invalid;
785     }
786
787     s.slipping = false;
788     return s;
789 }
790
791 FFADODevice::ClockSourceVector
792 MotuDevice::getSupportedClockSources() {
793     FFADODevice::ClockSourceVector r;
794     ClockSource s;
795
796     /* Form a list of clocks supported by MOTU interfaces */
797     s = clockIdToClockSource(MOTU_CLKSRC_INTERNAL);
798     r.push_back(s);
799     s = clockIdToClockSource(MOTU_CLKSRC_ADAT_OPTICAL);
800     r.push_back(s);
801     s = clockIdToClockSource(MOTU_CLKSRC_SPDIF_TOSLINK);
802     r.push_back(s);
803     s = clockIdToClockSource(MOTU_CLKSRC_SMPTE);
804     r.push_back(s);
805     s = clockIdToClockSource(MOTU_CLKSRC_WORDCLOCK);
806     r.push_back(s);
807     s = clockIdToClockSource(MOTU_CLKSRC_ADAT_9PIN);
808     r.push_back(s);
809     s = clockIdToClockSource(MOTU_CLKSRC_AES_EBU);
810     r.push_back(s);
811
812     return r;
813 }
814
815 bool
816 MotuDevice::setActiveClockSource(ClockSource s) {
817     debugOutput(DEBUG_LEVEL_VERBOSE, "setting clock source to id: %d\n",s.id);
818
819     // FIXME: this could do with some error checking
820     return setClockCtrlRegister(-1, s.id);
821 }
822
823 FFADODevice::ClockSource
824 MotuDevice::getActiveClockSource() {
825     ClockSource s;
826     quadlet_t clock_id = ReadRegister(MOTU_REG_CLK_CTRL) & MOTU_CLKSRC_MASK;
827     s = clockIdToClockSource(clock_id);
828     s.active = true;
829     return s;
830 }
831
832 bool
833 MotuDevice::lock() {
834
835     return true;
836 }
837
838
839 bool
840 MotuDevice::unlock() {
841
842     return true;
843 }
844
845 void
846 MotuDevice::showDevice()
847 {
848     debugOutput(DEBUG_LEVEL_VERBOSE,
849         "%s %s at node %d\n", m_model->vendor_name, m_model->model_name,
850         getNodeId());
851 }
852
853 bool
854 MotuDevice::prepare() {
855
856     int samp_freq = getSamplingFrequency();
857     unsigned int optical_in_mode = getOpticalMode(MOTU_DIR_IN);
858     unsigned int optical_out_mode = getOpticalMode(MOTU_DIR_OUT);
859     unsigned int event_size_in = getEventSize(MOTU_DIR_IN);
860     unsigned int event_size_out= getEventSize(MOTU_DIR_OUT);
861
862     debugOutput(DEBUG_LEVEL_NORMAL, "Preparing MotuDevice...\n" );
863
864     // Explicitly set the optical mode, primarily to ensure that the
865     // MOTU_REG_OPTICAL_CTRL register is initialised.  We need to do this to
866     // because some interfaces (the Ultralite for example) appear to power
867     // up without this set to anything sensible.  In this case, writes to
868     // MOTU_REG_ISOCTRL fail more often than not, which is bad.
869     setOpticalMode(MOTU_DIR_IN, optical_in_mode);
870     setOpticalMode(MOTU_DIR_OUT, optical_out_mode);
871
872     // Allocate bandwidth if not previously done.
873     // FIXME: The bandwidth allocation calculation can probably be
874     // refined somewhat since this is currently based on a rudimentary
875     // understanding of the ieee1394 iso protocol.
876     // Currently we assume the following.
877     //   * Ack/iso gap = 0.05 us
878     //   * DATA_PREFIX = 0.16 us
879     //   * DATA_END    = 0.26 us
880     // These numbers are the worst-case figures given in the ieee1394
881     // standard.  This gives approximately 0.5 us of overheads per packet -
882     // around 25 bandwidth allocation units (from the ieee1394 standard 1
883     // bandwidth allocation unit is 125/6144 us).  We further assume the
884     // MOTU is running at S400 (which it should be) so one allocation unit
885     // is equivalent to 1 transmitted byte; thus the bandwidth allocation
886     // required for the packets themselves is just the size of the packet.
887     // We used to allocate based on the maximum packet size (1160 bytes at
888     // 192 kHz for the traveler) but now do this based on the actual device
889     // state by utilising the result from getEventSize() and remembering
890     // that each packet has an 8 byte CIP header.  Note that bandwidth is
891     // allocated on a *per stream* basis - it must be allocated for both the
892     // transmit and receive streams.  While most MOTU modules are close to
893     // symmetric in terms of the number of in/out channels there are
894     // exceptions, so we deal with receive and transmit bandwidth separately.
895     signed int n_events_per_packet = samp_freq<=48000?8:(samp_freq<=96000?16:32);
896     m_rx_bandwidth = 25 + (n_events_per_packet*event_size_in);
897     m_tx_bandwidth = 25 + (n_events_per_packet*event_size_out);
898
899     // Assign iso channels if not already done
900     if (m_iso_recv_channel < 0)
901         m_iso_recv_channel = get1394Service().allocateIsoChannelGeneric(m_rx_bandwidth);
902
903     if (m_iso_send_channel < 0)
904         m_iso_send_channel = get1394Service().allocateIsoChannelGeneric(m_tx_bandwidth);
905
906     debugOutput(DEBUG_LEVEL_VERBOSE, "recv channel = %d, send channel = %d\n",
907         m_iso_recv_channel, m_iso_send_channel);
908
909     if (m_iso_recv_channel<0 || m_iso_send_channel<0) {
910         // be nice and deallocate
911         if (m_iso_recv_channel >= 0)
912             get1394Service().freeIsoChannel(m_iso_recv_channel);
913         if (m_iso_send_channel >= 0)
914             get1394Service().freeIsoChannel(m_iso_send_channel);
915
916         debugFatal("Could not allocate iso channels!\n");
917         return false;
918     }
919
920     // get the device specific and/or global SP configuration
921     Util::Configuration &config = getDeviceManager().getConfiguration();
922     // base value is the config.h value
923     float recv_sp_dll_bw = STREAMPROCESSOR_DLL_BW_HZ;
924     float xmit_sp_dll_bw = STREAMPROCESSOR_DLL_BW_HZ;
925
926     // we can override that globally
927     config.getValueForSetting("streaming.spm.recv_sp_dll_bw", recv_sp_dll_bw);
928     config.getValueForSetting("streaming.spm.xmit_sp_dll_bw", xmit_sp_dll_bw);
929
930     // or override in the device section
931     config.getValueForDeviceSetting(getConfigRom().getNodeVendorId(), getConfigRom().getModelId(), "recv_sp_dll_bw", recv_sp_dll_bw);
932     config.getValueForDeviceSetting(getConfigRom().getNodeVendorId(), getConfigRom().getModelId(), "xmit_sp_dll_bw", xmit_sp_dll_bw);
933
934     m_receiveProcessor=new Streaming::MotuReceiveStreamProcessor(*this, event_size_in);
935     m_receiveProcessor->setVerboseLevel(getDebugLevel());
936
937     // The first thing is to initialize the processor.  This creates the
938     // data structures.
939     if(!m_receiveProcessor->init()) {
940         debugFatal("Could not initialize receive processor!\n");
941         return false;
942     }
943
944     if(!m_receiveProcessor->setDllBandwidth(recv_sp_dll_bw)) {
945         debugFatal("Could not set DLL bandwidth\n");
946         delete m_receiveProcessor;
947         m_receiveProcessor = NULL;
948         return false;
949     }
950
951     // Now we add ports to the processor
952     debugOutput(DEBUG_LEVEL_VERBOSE,"Adding ports to receive processor\n");
953
954     char *buff;
955     Streaming::Port *p=NULL;
956
957     // retrieve the ID
958     std::string id=std::string("dev?");
959     if(!getOption("id", id)) {
960         debugWarning("Could not retrieve id parameter, defaulting to 'dev?'\n");
961     }
962
963     // Add audio capture ports
964     if (!addDirPorts(Streaming::Port::E_Capture, samp_freq, optical_in_mode)) {
965         return false;
966     }
967
968     // Add MIDI port.  The MOTU only has one MIDI input port, with each
969     // MIDI byte sent using a 3 byte sequence starting at byte 4 of the
970     // event data.
971     asprintf(&buff,"%s_cap_MIDI0",id.c_str());
972     p = new Streaming::MotuMidiPort(*m_receiveProcessor, buff,
973         Streaming::Port::E_Capture, 4);
974     if (!p) {
975         debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n", buff);
976     }
977     free(buff);
978
979     // example of adding an control port:
980 //    asprintf(&buff,"%s_cap_%s",id.c_str(),"myportnamehere");
981 //    p=new Streaming::MotuControlPort(
982 //            buff,
983 //            Streaming::Port::E_Capture,
984 //            0 // you can add all other port specific stuff you
985 //              // need to pass by extending MotuXXXPort and MotuPortInfo
986 //    );
987 //    free(buff);
988 //
989 //    if (!p) {
990 //        debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n",buff);
991 //    } else {
992 //
993 //        if (!m_receiveProcessor->addPort(p)) {
994 //            debugWarning("Could not register port with stream processor\n");
995 //            return false;
996 //        } else {
997 //            debugOutput(DEBUG_LEVEL_VERBOSE, "Added port %s\n",buff);
998 //        }
999 //    }
1000
1001     // Do the same for the transmit processor
1002     m_transmitProcessor=new Streaming::MotuTransmitStreamProcessor(*this, event_size_out);
1003
1004     m_transmitProcessor->setVerboseLevel(getDebugLevel());
1005
1006     if(!m_transmitProcessor->init()) {
1007         debugFatal("Could not initialize transmit processor!\n");
1008         return false;
1009     }
1010
1011     if(!m_transmitProcessor->setDllBandwidth(xmit_sp_dll_bw)) {
1012         debugFatal("Could not set DLL bandwidth\n");
1013         delete m_transmitProcessor;
1014         m_transmitProcessor = NULL;
1015         return false;
1016     }
1017
1018     // Now we add ports to the processor
1019     debugOutput(DEBUG_LEVEL_VERBOSE,"Adding ports to transmit processor\n");
1020
1021     // Add audio playback ports
1022     if (!addDirPorts(Streaming::Port::E_Playback, samp_freq, optical_out_mode)) {
1023         return false;
1024     }
1025
1026     // Add MIDI port.  The MOTU only has one output MIDI port, with each
1027     // MIDI byte transmitted using a 3 byte sequence starting at byte 4
1028     // of the event data.
1029     asprintf(&buff,"%s_pbk_MIDI0",id.c_str());
1030     p = new Streaming::MotuMidiPort(*m_transmitProcessor, buff,
1031         Streaming::Port::E_Playback, 4);
1032     if (!p) {
1033         debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n", buff);
1034     }
1035     free(buff);
1036
1037     // example of adding an control port:
1038 //    asprintf(&buff,"%s_pbk_%s",id.c_str(),"myportnamehere");
1039 //
1040 //    p=new Streaming::MotuControlPort(
1041 //            buff,
1042 //            Streaming::Port::E_Playback,
1043 //            0 // you can add all other port specific stuff you
1044 //              // need to pass by extending MotuXXXPort and MotuPortInfo
1045 //    );
1046 //    free(buff);
1047 //
1048 //    if (!p) {
1049 //        debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n",buff);
1050 //    } else {
1051 //        if (!m_transmitProcessor->addPort(p)) {
1052 //            debugWarning("Could not register port with stream processor\n");
1053 //            return false;
1054 //        } else {
1055 //            debugOutput(DEBUG_LEVEL_VERBOSE, "Added port %s\n",buff);
1056 //        }
1057 //    }
1058
1059     return true;
1060 }
1061
1062 int
1063 MotuDevice::getStreamCount() {
1064      return 2; // one receive, one transmit
1065 }
1066
1067 Streaming::StreamProcessor *
1068 MotuDevice::getStreamProcessorByIndex(int i) {
1069     switch (i) {
1070     case 0:
1071         return m_receiveProcessor;
1072     case 1:
1073          return m_transmitProcessor;
1074     default:
1075         return NULL;
1076     }
1077     return 0;
1078 }
1079
1080 bool
1081 MotuDevice::startStreamByIndex(int i) {
1082
1083 quadlet_t isoctrl = ReadRegister(MOTU_REG_ISOCTRL);
1084
1085     if (m_motu_model == MOTU_MODEL_828MkI) {
1086         // The 828MkI device does this differently.
1087         // To be implemented
1088         return false;
1089     }
1090
1091     // NOTE: this assumes that you have two streams
1092     switch (i) {
1093     case 0:
1094         // TODO: do the stuff that is nescessary to make the device
1095         // receive a stream
1096
1097         // Set the streamprocessor channel to the one obtained by
1098         // the connection management
1099         m_receiveProcessor->setChannel(m_iso_recv_channel);
1100
1101         // Mask out current transmit settings of the MOTU and replace
1102         // with new ones.  Turn bit 24 on to enable changes to the
1103         // MOTU's iso transmit settings when the iso control register
1104         // is written.  Bit 23 enables iso transmit from the MOTU.
1105         isoctrl &= 0xff00ffff;
1106         isoctrl |= (m_iso_recv_channel << 16);
1107         isoctrl |= 0x00c00000;
1108         WriteRegister(MOTU_REG_ISOCTRL, isoctrl);
1109         break;
1110     case 1:
1111         // TODO: do the stuff that is nescessary to make the device
1112         // transmit a stream
1113
1114         // Set the streamprocessor channel to the one obtained by
1115         // the connection management
1116         m_transmitProcessor->setChannel(m_iso_send_channel);
1117
1118         // Mask out current receive settings of the MOTU and replace
1119         // with new ones.  Turn bit 31 on to enable changes to the
1120         // MOTU's iso receive settings when the iso control register
1121         // is written.  Bit 30 enables iso receive by the MOTU.
1122         isoctrl &= 0x00ffffff;
1123         isoctrl |= (m_iso_send_channel << 24);
1124         isoctrl |= 0xc0000000;
1125         WriteRegister(MOTU_REG_ISOCTRL, isoctrl);
1126         break;
1127
1128     default: // Invalid stream index
1129         return false;
1130     }
1131
1132     return true;
1133 }
1134
1135 bool
1136 MotuDevice::stopStreamByIndex(int i) {
1137
1138 quadlet_t isoctrl = ReadRegister(MOTU_REG_ISOCTRL);
1139
1140     // TODO: connection management: break connection
1141     // cfr the start function
1142
1143     if (m_motu_model == MOTU_MODEL_828MkI) {
1144         // The 828MkI device does this differently.
1145         // To be implemented
1146         return false;
1147     }
1148
1149     // NOTE: this assumes that you have two streams
1150     switch (i) {
1151     case 0:
1152         // Turn bit 22 off to disable iso send by the MOTU.  Turn
1153         // bit 23 on to enable changes to the MOTU's iso transmit
1154         // settings when the iso control register is written.
1155         isoctrl &= 0xffbfffff;
1156         isoctrl |= 0x00800000;
1157         WriteRegister(MOTU_REG_ISOCTRL, isoctrl);
1158         break;
1159     case 1:
1160         // Turn bit 30 off to disable iso receive by the MOTU.  Turn
1161         // bit 31 on to enable changes to the MOTU's iso receive
1162         // settings when the iso control register is written.
1163         isoctrl &= 0xbfffffff;
1164         isoctrl |= 0x80000000;
1165         WriteRegister(MOTU_REG_ISOCTRL, isoctrl);
1166         break;
1167
1168     default: // Invalid stream index
1169         return false;
1170     }
1171
1172     return true;
1173 }
1174
1175 signed int MotuDevice::getIsoRecvChannel(void) {
1176     return m_iso_recv_channel;
1177 }
1178
1179 signed int MotuDevice::getIsoSendChannel(void) {
1180     return m_iso_send_channel;
1181 }
1182
1183 unsigned int MotuDevice::getOpticalMode(unsigned int dir) {
1184     unsigned int reg;
1185
1186     if (m_motu_model == MOTU_MODEL_828MkI) {
1187         // The early devices used a different register layout. 
1188         unsigned int mask, shift;
1189         reg = ReadRegister(MOTU_G1_REG_CONFIG);
1190         mask = (dir==MOTU_DIR_IN)?MOTU_G1_OPT_IN_MODE_MASK:MOTU_G1_OPT_OUT_MODE_MASK;
1191         shift = (dir==MOTU_DIR_IN)?MOTU_G1_OPT_IN_MODE_BIT0:MOTU_G1_OPT_OUT_MODE_BIT0;
1192         switch (reg & mask) {
1193             case MOTU_G1_OPTICAL_OFF: return MOTU_OPTICAL_MODE_OFF;
1194             case MOTU_G1_OPTICAL_TOSLINK: return MOTU_OPTICAL_MODE_TOSLINK;
1195             // MOTU_G1_OPTICAL_OFF and MOTU_G1_OPTICAL_ADAT seem to be
1196             // identical, so currently we don't know how to differentiate
1197             // these two modes.
1198             // case MOTU_G1_OPTICAL_ADAT: return MOTU_OPTICAL_MODE_ADAT;
1199         }
1200         return 0;
1201     }
1202
1203     reg = ReadRegister(MOTU_REG_ROUTE_PORT_CONF);
1204
1205     if (dir == MOTU_DIR_IN)
1206         return (reg & MOTU_OPTICAL_IN_MODE_MASK) >> 8;
1207     else
1208         return (reg & MOTU_OPTICAL_OUT_MODE_MASK) >> 10;
1209 }
1210
1211 signed int MotuDevice::setOpticalMode(unsigned int dir, unsigned int mode) {
1212     unsigned int reg;
1213     unsigned int opt_ctrl = 0x0000002;
1214
1215     /* THe 896HD doesn't have an SPDIF/TOSLINK optical mode, so don't try to
1216      * set it
1217      */
1218     if (m_motu_model==MOTU_MODEL_896HD && mode==MOTU_OPTICAL_MODE_TOSLINK)
1219         return -1;
1220
1221     if (m_motu_model == MOTU_MODEL_828MkI) {
1222         // The earlier MOTUs handle this differently.
1223         unsigned int mask, shift, g1mode = 0;
1224         reg = ReadRegister(MOTU_G1_REG_CONFIG);
1225         mask = (dir==MOTU_DIR_IN)?MOTU_G1_OPT_IN_MODE_MASK:MOTU_G1_OPT_OUT_MODE_MASK;
1226         shift = (dir==MOTU_DIR_IN)?MOTU_G1_OPT_IN_MODE_BIT0:MOTU_G1_OPT_OUT_MODE_BIT0;
1227         switch (mode) {
1228             case MOTU_OPTICAL_MODE_OFF: g1mode = MOTU_G1_OPTICAL_OFF; break;
1229             case MOTU_OPTICAL_MODE_ADAT: g1mode = MOTU_G1_OPTICAL_ADAT; break;
1230             // See comment in getOpticalMode() about mode ambiguity
1231             // case MOTU_OPTICAL_MODE_TOSLINK: g1mode = MOTU_G1_OPTICAL_TOSLINK; break;
1232         }
1233         reg = (reg & ~mask) | (g1mode << shift);
1234         return WriteRegister(MOTU_G1_REG_CONFIG, reg);
1235     }
1236
1237     reg = ReadRegister(MOTU_REG_ROUTE_PORT_CONF);
1238
1239     // Set up the optical control register value according to the current
1240     // optical port modes.  At this stage it's not completely understood
1241     // what the "Optical control" register does, so the values it's set to
1242     // are more or less "magic" numbers.
1243     if ((reg & MOTU_OPTICAL_IN_MODE_MASK) != (MOTU_OPTICAL_MODE_ADAT<<8))
1244         opt_ctrl |= 0x00000080;
1245     if ((reg & MOTU_OPTICAL_OUT_MODE_MASK) != (MOTU_OPTICAL_MODE_ADAT<<10))
1246         opt_ctrl |= 0x00000040;
1247
1248     if (dir & MOTU_DIR_IN) {
1249         reg &= ~MOTU_OPTICAL_IN_MODE_MASK;
1250         reg |= (mode << 8) & MOTU_OPTICAL_IN_MODE_MASK;
1251         if (mode != MOTU_OPTICAL_MODE_ADAT)
1252             opt_ctrl |= 0x00000080;
1253         else
1254             opt_ctrl &= ~0x00000080;
1255     }
1256     if (dir & MOTU_DIR_OUT) {
1257         reg &= ~MOTU_OPTICAL_OUT_MODE_MASK;
1258         reg |= (mode <<10) & MOTU_OPTICAL_OUT_MODE_MASK;
1259         if (mode != MOTU_OPTICAL_MODE_ADAT)
1260             opt_ctrl |= 0x00000040;
1261         else
1262             opt_ctrl &= ~0x00000040;
1263     }
1264
1265     // FIXME: there seems to be more to it than this, but for
1266     // the moment at least this seems to work.
1267     WriteRegister(MOTU_REG_ROUTE_PORT_CONF, reg);
1268     return WriteRegister(MOTU_REG_OPTICAL_CTRL, opt_ctrl);
1269 }
1270
1271 signed int MotuDevice::getEventSize(unsigned int direction) {
1272 //
1273 // Return the size in bytes of a single event sent to (dir==MOTU_OUT) or
1274 // from (dir==MOTU_IN) the MOTU as part of an iso data packet.
1275 //
1276 // FIXME: for performance it may turn out best to calculate the event
1277 // size in setOpticalMode and cache the result in a data field.  However,
1278 // as it stands this will not adapt to dynamic changes in sample rate - we'd
1279 // need a setFrameRate() for that.
1280 //
1281 // At the very least an event consists of the SPH (4 bytes) and the control/MIDI
1282 // bytes (6 bytes).
1283 // Note that all audio channels are sent using 3 bytes.
1284 signed int sample_rate = getSamplingFrequency();
1285 signed int optical_mode = getOpticalMode(direction);
1286 signed int size = 4+6;
1287
1288 unsigned int i;
1289 unsigned int dir = direction==Streaming::Port::E_Capture?MOTU_PA_IN:MOTU_PA_OUT;
1290 unsigned int flags = (1 << ( optical_mode + 4 ));
1291
1292     if ( sample_rate > 96000 )
1293         flags |= MOTU_PA_RATE_4x;
1294     else if ( sample_rate > 48000 )
1295         flags |= MOTU_PA_RATE_2x;
1296     else
1297         flags |= MOTU_PA_RATE_1x;
1298
1299     // Don't test for padding port flag here since we need to include such
1300     // pseudo-ports when calculating the event size.
1301     for (i=0; i < DevicesProperty[m_motu_model-1].n_port_entries; i++) {
1302         if (( DevicesProperty[m_motu_model-1].port_entry[i].port_flags & dir ) &&
1303            ( DevicesProperty[m_motu_model-1].port_entry[i].port_flags & MOTU_PA_RATE_MASK & flags ) &&
1304            ( DevicesProperty[m_motu_model-1].port_entry[i].port_flags & MOTU_PA_OPTICAL_MASK & flags )) {
1305             size += 3;
1306         }
1307     }
1308
1309     // Finally round size up to the next quadlet boundary
1310     return ((size+3)/4)*4;
1311 }
1312 /* ======================================================================= */
1313
1314 bool MotuDevice::addPort(Streaming::StreamProcessor *s_processor,
1315   char *name, enum Streaming::Port::E_Direction direction,
1316   int position, int size) {
1317 /*
1318  * Internal helper function to add a MOTU port to a given stream processor.
1319  * This just saves the unnecessary replication of what is essentially
1320  * boilerplate code.  Note that the port name is freed by this function
1321  * prior to exit.
1322  */
1323 Streaming::Port *p=NULL;
1324
1325     p = new Streaming::MotuAudioPort(*s_processor, name, direction, position, size);
1326
1327     if (!p) {
1328         debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n",name);
1329     }
1330     free(name);
1331     return true;
1332 }
1333 /* ======================================================================= */
1334
1335 bool MotuDevice::addDirPorts(
1336   enum Streaming::Port::E_Direction direction,
1337   unsigned int sample_rate, unsigned int optical_mode) {
1338 /*
1339  * Internal helper method: adds all required ports for the given direction
1340  * based on the indicated sample rate and optical mode.
1341  *
1342  * Notes: currently ports are not created if they are disabled due to sample
1343  * rate or optical mode.  However, it might be better to unconditionally
1344  * create all ports and just disable those which are not active.
1345  */
1346 const char *mode_str = direction==Streaming::Port::E_Capture?"cap":"pbk";
1347 Streaming::StreamProcessor *s_processor;
1348 unsigned int i;
1349 char *buff;
1350 unsigned int dir = direction==Streaming::Port::E_Capture?MOTU_PA_IN:MOTU_PA_OUT;
1351 unsigned int flags = (1 << ( optical_mode + 4 ));
1352
1353     if ( sample_rate > 96000 )
1354         flags |= MOTU_PA_RATE_4x;
1355     else if ( sample_rate > 48000 )
1356         flags |= MOTU_PA_RATE_2x;
1357     else
1358         flags |= MOTU_PA_RATE_1x;
1359
1360     // retrieve the ID
1361     std::string id=std::string("dev?");
1362     if(!getOption("id", id)) {
1363         debugWarning("Could not retrieve id parameter, defaulting to 'dev?'\n");
1364     }
1365
1366     if (direction == Streaming::Port::E_Capture) {
1367         s_processor = m_receiveProcessor;
1368     } else {
1369         s_processor = m_transmitProcessor;
1370     }
1371
1372     for (i=0; i < DevicesProperty[m_motu_model-1].n_port_entries; i++) {
1373         if (( DevicesProperty[m_motu_model-1].port_entry[i].port_flags & dir ) &&
1374            ( DevicesProperty[m_motu_model-1].port_entry[i].port_flags & MOTU_PA_RATE_MASK & flags ) &&
1375            ( DevicesProperty[m_motu_model-1].port_entry[i].port_flags & MOTU_PA_OPTICAL_MASK & flags ) &&
1376            !( DevicesProperty[m_motu_model-1].port_entry[i].port_flags & MOTU_PA_PADDING )) {
1377             asprintf(&buff,"%s_%s_%s" , id.c_str(), mode_str,
1378               DevicesProperty[m_motu_model-1].port_entry[i].port_name);
1379             if (!addPort(s_processor, buff, direction, DevicesProperty[m_motu_model-1].port_entry[i].port_offset, 0))
1380                 return false;
1381         }
1382     }
1383    
1384     return true;
1385 }
1386 /* ======================================================================== */
1387
1388 unsigned int MotuDevice::ReadRegister(unsigned int reg) {
1389 /*
1390  * Attempts to read the requested register from the MOTU.
1391  */
1392
1393   quadlet_t quadlet;
1394
1395   quadlet = 0;
1396   // Note: 1394Service::read() expects a physical ID, not the node id
1397   if (get1394Service().read(0xffc0 | getNodeId(), MOTU_BASE_ADDR+reg, 1, &quadlet) <= 0) {
1398     debugError("Error doing motu read from register 0x%06x\n",reg);
1399   }
1400
1401   return CondSwapFromBus32(quadlet);
1402 }
1403
1404 signed int MotuDevice::WriteRegister(unsigned int reg, quadlet_t data) {
1405 /*
1406  * Attempts to write the given data to the requested MOTU register.
1407  */
1408
1409   unsigned int err = 0;
1410   data = CondSwapToBus32(data);
1411
1412   // Note: 1394Service::write() expects a physical ID, not the node id
1413   if (get1394Service().write(0xffc0 | getNodeId(), MOTU_BASE_ADDR+reg, 1, &data) <= 0) {
1414     err = 1;
1415     debugError("Error doing motu write to register 0x%06x\n",reg);
1416   }
1417
1418   SleepRelativeUsec(100);
1419   return (err==0)?0:-1;
1420 }
1421
1422 }
Note: See TracBrowser for help on using the browser.