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

Revision 1006, 51.1 kB (checked in by jwoithe, 13 years ago)

MOTU: implement "phones source" control.
MOTU: commence work on optical mode control. For various reasons this isn't functional yet.

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/DelayLockedLoop.h"
39 #include "libutil/Time.h"
40
41 #include "libcontrol/BasicElements.h"
42
43 #include <string>
44 #include <stdint.h>
45 #include <assert.h>
46 #include <netinet/in.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, 40},
196     {"Main-R", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 43},
197     {"Mix-L", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 10},
198     {"Mix-R", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 13},
199     {"Mic1", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 16},
200     {"Mic2", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 19},
201     {"Analog1", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 16},
202     {"Analog2", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 19},
203     {"Analog3", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 22},
204     {"Analog4", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 25},
205     {"Analog5", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 28},
206     {"Analog6", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 31},
207     {"Analog7", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 34},
208     {"Analog8", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 37},
209     {"Phones-L", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 10},
210     {"Phones-R", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 13},
211     {"SPDIF1", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 46},
212     {"SPDIF2", MOTU_DIR_INOUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 49},
213 };
214
215 const PortEntry Ports_8PRE[] =
216 {
217     {"Analog1", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 16},
218     {"Analog2", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 19},
219     {"Analog3", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 22},
220     {"Analog4", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 25},
221     {"Analog5", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 28},
222     {"Analog6", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 31},
223     {"Analog7", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 34},
224     {"Analog8", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 37},
225     {"Mix-L", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 10},
226     {"Mix-R", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 13},
227     {"Main-L", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 16},
228     {"Main-R", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 19},
229     {"Phones-L", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 10},
230     {"Phones-R", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 13},
231     {"ADAT1", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 40},
232     {"ADAT1", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 22},
233     {"ADAT2", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 43},
234     {"ADAT2", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 25},
235     {"ADAT3", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 46},
236     {"ADAT3", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 28},
237     {"ADAT4", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 49},
238     {"ADAT4", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 31},
239     {"ADAT5", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 52},
240     {"ADAT5", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 34},
241     {"ADAT6", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 55},
242     {"ADAT6", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 37},
243     {"ADAT7", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 58},
244     {"ADAT7", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 40},
245     {"ADAT8", MOTU_DIR_IN, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 61},
246     {"ADAT8", MOTU_DIR_OUT, MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ADAT, 43},
247 };
248
249 // Mixer registers
250 const MixerCtrl MixerCtrls_Traveler[] = {
251     {"Mix1/Ana1_", "Mix 1 analog 1 ", "", MOTU_CTRL_STD_CHANNEL, 0x4000, },
252     {"Mix1/Ana2_", "Mix 1 analog 2 ", "", MOTU_CTRL_STD_CHANNEL, 0x4004, },
253     {"Mix1/Ana3_", "Mix 1 analog 3 ", "", MOTU_CTRL_STD_CHANNEL, 0x4008, },
254     {"Mix1/Ana4_", "Mix 1 analog 4 ", "", MOTU_CTRL_STD_CHANNEL, 0x400c, },
255     {"Mix1/Ana5_", "Mix 1 analog 5 ", "", MOTU_CTRL_STD_CHANNEL, 0x4010, },
256     {"Mix1/Ana6_", "Mix 1 analog 6 ", "", MOTU_CTRL_STD_CHANNEL, 0x4014, },
257     {"Mix1/Ana7_", "Mix 1 analog 7 ", "", MOTU_CTRL_STD_CHANNEL, 0x4018, },
258     {"Mix1/Ana8_", "Mix 1 analog 8 ", "", MOTU_CTRL_STD_CHANNEL, 0x401c, },
259
260     {"Mix1/Mix_", "Mix 1 ", "", MOTU_CTRL_STD_MIX, 0x0c20, },
261
262     /* For line input controls, the "register" is the zero-based channel number */
263     {"Control/Ana5_", "Analog 5 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 4},
264     {"Control/Ana6_", "Analog 6 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 5},
265     {"Control/Ana7_", "Analog 7 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 6},
266     {"Control/Ana8_", "Analog 8 input ", "", MOTU_CTRL_TRAVELER_LINE_INPUT_CTRLS, 7},
267
268     {"Control/Phones_", "Phones source", "", MOTU_CTRL_PHONES_SRC, 0},
269
270     {"Control/OpticalIn_mode", "Optical input mode ", "", MOTU_CTRL_OPTICAL_MODE, MOTU_DIR_IN},
271     {"Control/OpticalOut_mode", "Optical output mode ", "", MOTU_CTRL_OPTICAL_MODE, MOTU_DIR_OUT},
272 };
273
274 // For convenience during initial testing, just make the 828MkII and 896HD
275 // use the Traveler's mixer definition.  Separate definitions for these
276 // models will come once the final mixer structure is in place.  For now
277 // it's in a state of flux and subject to significant change.
278 #define MixerCtrls_828MkII MixerCtrls_Traveler
279 #define MixerCtrls_896HD   MixerCtrls_Traveler
280
281 /* The order of DevicesProperty entries must match the numeric order of the
282  * MOTU model enumeration (EMotuModel).
283  */
284 const DevicePropertyEntry DevicesProperty[] = {
285 //  { Ports_map,       N_ELEMENTS( Ports_map ),        MaxSR },
286     { Ports_828MKII,   N_ELEMENTS( Ports_828MKII ),    96000, MixerCtrls_828MkII, N_ELEMENTS(MixerCtrls_828MkII), },
287     { Ports_TRAVELER,  N_ELEMENTS( Ports_TRAVELER ),  192000, MixerCtrls_Traveler, N_ELEMENTS(MixerCtrls_Traveler), },
288     { Ports_ULTRALITE, N_ELEMENTS( Ports_ULTRALITE ),  96000 },
289     { Ports_8PRE,      N_ELEMENTS( Ports_8PRE ),       96000 },
290     { Ports_828MKI,    N_ELEMENTS( Ports_828MKI ),     48000 },
291     { Ports_896HD,     N_ELEMENTS( Ports_896HD ),     192000, MixerCtrls_896HD, N_ELEMENTS(MixerCtrls_896HD),  },
292 };
293
294 MotuDevice::MotuDevice( DeviceManager& d, std::auto_ptr<ConfigRom>( configRom ))
295     : FFADODevice( d, configRom )
296     , m_motu_model( MOTU_MODEL_NONE )
297     , m_iso_recv_channel ( -1 )
298     , m_iso_send_channel ( -1 )
299     , m_rx_bandwidth ( -1 )
300     , m_tx_bandwidth ( -1 )
301     , m_receiveProcessor ( 0 )
302     , m_transmitProcessor ( 0 )
303     , m_MixerContainer ( NULL )
304     , m_ControlContainer ( NULL )
305 {
306     debugOutput( DEBUG_LEVEL_VERBOSE, "Created Motu::MotuDevice (NodeID %d)\n",
307                  getConfigRom().getNodeId() );
308 }
309
310 MotuDevice::~MotuDevice()
311 {
312     delete m_receiveProcessor;
313     delete m_transmitProcessor;
314
315     // Free ieee1394 bus resources if they have been allocated
316     if (m_iso_recv_channel>=0 && !get1394Service().freeIsoChannel(m_iso_recv_channel)) {
317         debugOutput(DEBUG_LEVEL_VERBOSE, "Could not free recv iso channel %d\n", m_iso_recv_channel);
318     }
319     if (m_iso_send_channel>=0 && !get1394Service().freeIsoChannel(m_iso_send_channel)) {
320         debugOutput(DEBUG_LEVEL_VERBOSE, "Could not free send iso channel %d\n", m_iso_send_channel);
321     }
322
323     destroyMixer();
324 }
325
326 bool
327 MotuDevice::buildMixer() {
328     unsigned int i;
329     bool result = true;
330     debugOutput(DEBUG_LEVEL_VERBOSE, "Building a MOTU mixer...\n");
331
332     destroyMixer();
333        
334     // create the mixer object container
335     m_MixerContainer = new Control::Container("Mixer");
336     if (!m_MixerContainer) {
337         debugError("Could not create mixer container...\n");
338         return false;
339     }
340
341     // Mixer controls get added here
342     for (i=0; i<DevicesProperty[m_motu_model-1].n_mixer_ctrls; i++) {
343         unsigned int type = DevicesProperty[m_motu_model-1].mixer_ctrl[i].type;
344         char name[100];
345         char label[100];
346         if (type & MOTU_CTRL_CHANNEL_FADER) {
347             snprintf(name, 100, "%s%s", DevicesProperty[m_motu_model-1].mixer_ctrl[i].name, "fader");
348             snprintf(label,100, "%s%s", DevicesProperty[m_motu_model-1].mixer_ctrl[i].label,"fader");
349             result &= m_MixerContainer->addElement(
350                 new ChannelFader(*this,
351                     DevicesProperty[m_motu_model-1].mixer_ctrl[i].dev_register,
352                     name, label,
353                     DevicesProperty[m_motu_model-1].mixer_ctrl[i].desc));
354             type &= ~MOTU_CTRL_CHANNEL_FADER;
355         }
356         if (type & MOTU_CTRL_CHANNEL_PAN) {
357             snprintf(name, 100, "%s%s", DevicesProperty[m_motu_model-1].mixer_ctrl[i].name, "pan");
358             snprintf(label,100, "%s%s", DevicesProperty[m_motu_model-1].mixer_ctrl[i].label,"pan");
359             result &= m_MixerContainer->addElement(
360                 new ChannelPan(*this,
361                     DevicesProperty[m_motu_model-1].mixer_ctrl[i].dev_register,
362                     name, label,
363                     DevicesProperty[m_motu_model-1].mixer_ctrl[i].desc));
364             type &= ~MOTU_CTRL_CHANNEL_PAN;
365         }
366         if (type & MOTU_CTRL_CHANNEL_MUTE) {
367             snprintf(name, 100, "%s%s", DevicesProperty[m_motu_model-1].mixer_ctrl[i].name, "mute");
368             snprintf(label,100, "%s%s", DevicesProperty[m_motu_model-1].mixer_ctrl[i].label,"mute");
369             result &= m_MixerContainer->addElement(
370                 new MotuBinarySwitch(*this,
371                     DevicesProperty[m_motu_model-1].mixer_ctrl[i].dev_register,
372                     MOTU_CTRL_MASK_MUTE_VALUE, MOTU_CTRL_MASK_MUTE_SETENABLE,
373                     name, label,
374                     DevicesProperty[m_motu_model-1].mixer_ctrl[i].desc));
375             type &= ~MOTU_CTRL_CHANNEL_MUTE;
376         }
377         if (type & MOTU_CTRL_CHANNEL_SOLO) {
378             snprintf(name, 100, "%s%s", DevicesProperty[m_motu_model-1].mixer_ctrl[i].name, "solo");
379             snprintf(label,100, "%s%s", DevicesProperty[m_motu_model-1].mixer_ctrl[i].label,"solo");
380             result &= m_MixerContainer->addElement(
381                 new MotuBinarySwitch(*this,
382                     DevicesProperty[m_motu_model-1].mixer_ctrl[i].dev_register,
383                     MOTU_CTRL_MASK_SOLO_VALUE, MOTU_CTRL_MASK_SOLO_SETENABLE,
384                     name, label,
385                     DevicesProperty[m_motu_model-1].mixer_ctrl[i].desc));
386             type &= ~MOTU_CTRL_CHANNEL_SOLO;
387         }
388
389         if (type & MOTU_CTRL_MIX_FADER) {
390             snprintf(name, 100, "%s%s", DevicesProperty[m_motu_model-1].mixer_ctrl[i].name, "fader");
391             snprintf(label,100, "%s%s", DevicesProperty[m_motu_model-1].mixer_ctrl[i].label,"fader");
392             result &= m_MixerContainer->addElement(
393                 new MixFader(*this,
394                     DevicesProperty[m_motu_model-1].mixer_ctrl[i].dev_register,
395                     name, label,
396                     DevicesProperty[m_motu_model-1].mixer_ctrl[i].desc));
397             type &= ~MOTU_CTRL_MIX_FADER;
398         }
399         if (type & MOTU_CTRL_MIX_MUTE) {
400             snprintf(name, 100, "%s%s", DevicesProperty[m_motu_model-1].mixer_ctrl[i].name, "mute");
401             snprintf(label,100, "%s%s", DevicesProperty[m_motu_model-1].mixer_ctrl[i].label,"mute");
402             result &= m_MixerContainer->addElement(
403                 new MixMute(*this,
404                     DevicesProperty[m_motu_model-1].mixer_ctrl[i].dev_register,
405                     name, label,
406                     DevicesProperty[m_motu_model-1].mixer_ctrl[i].desc));
407             type &= ~MOTU_CTRL_MIX_MUTE;
408         }
409         if (type & MOTU_CTRL_MIX_DEST) {
410             snprintf(name, 100, "%s%s", DevicesProperty[m_motu_model-1].mixer_ctrl[i].name, "dest");
411             snprintf(label,100, "%s%s", DevicesProperty[m_motu_model-1].mixer_ctrl[i].label,"dest");
412             result &= m_MixerContainer->addElement(
413                 new MixDest(*this,
414                     DevicesProperty[m_motu_model-1].mixer_ctrl[i].dev_register,
415                     name, label,
416                     DevicesProperty[m_motu_model-1].mixer_ctrl[i].desc));
417             type &= ~MOTU_CTRL_MIX_DEST;
418         }
419
420         if (type & MOTU_CTRL_INPUT_LEVEL) {
421             snprintf(name, 100, "%s%s", DevicesProperty[m_motu_model-1].mixer_ctrl[i].name, "level");
422             snprintf(label,100, "%s%s", DevicesProperty[m_motu_model-1].mixer_ctrl[i].label,"level");
423             result &= m_MixerContainer->addElement(
424                 new MotuBinarySwitch(*this,
425                     MOTU_REG_INPUT_LEVEL,
426                     1<<DevicesProperty[m_motu_model-1].mixer_ctrl[i].dev_register, 0,
427                     name, label,
428                     DevicesProperty[m_motu_model-1].mixer_ctrl[i].desc));
429             type &= ~MOTU_CTRL_INPUT_LEVEL;
430         }
431         if (type & MOTU_CTRL_INPUT_BOOST) {
432             snprintf(name, 100, "%s%s", DevicesProperty[m_motu_model-1].mixer_ctrl[i].name, "boost");
433             snprintf(label,100, "%s%s", DevicesProperty[m_motu_model-1].mixer_ctrl[i].label,"boost");
434             result &= m_MixerContainer->addElement(
435                 new MotuBinarySwitch(*this,
436                     MOTU_REG_INPUT_BOOST,
437                     1<<DevicesProperty[m_motu_model-1].mixer_ctrl[i].dev_register, 0,
438                     name, label,
439                     DevicesProperty[m_motu_model-1].mixer_ctrl[i].desc));
440             type &= ~MOTU_CTRL_INPUT_BOOST;
441         }
442         if (type & MOTU_CTRL_PHONES_SRC) {
443             snprintf(name, 100, "%s%s", DevicesProperty[m_motu_model-1].mixer_ctrl[i].name, "src");
444             snprintf(label,100, "%s%s", DevicesProperty[m_motu_model-1].mixer_ctrl[i].label,"src");
445             result &= m_MixerContainer->addElement(
446                 new PhonesSrc(*this,
447                     name, label,
448                     DevicesProperty[m_motu_model-1].mixer_ctrl[i].desc));
449             type &= ~MOTU_CTRL_PHONES_SRC;
450         }
451         if (type & MOTU_CTRL_OPTICAL_MODE) {
452             result &= m_MixerContainer->addElement(
453                 new OpticalMode(*this, DevicesProperty[m_motu_model-1].mixer_ctrl[i].dev_register,
454                     DevicesProperty[m_motu_model-1].mixer_ctrl[i].name,
455                     DevicesProperty[m_motu_model-1].mixer_ctrl[i].label,
456                     DevicesProperty[m_motu_model-1].mixer_ctrl[i].desc));
457             type &= ~MOTU_CTRL_OPTICAL_MODE;
458         }
459
460         if (type) {
461             debugOutput(DEBUG_LEVEL_VERBOSE, "Unknown mixer control type flag bits 0x%08x\n", DevicesProperty[m_motu_model-1].mixer_ctrl[i].type);
462         }
463     }
464
465     /* Now add some general device information controls.  These may yet
466      * become device-specific if it turns out to be easier that way.
467      */
468     result &= m_MixerContainer->addElement(
469         new InfoElement(*this, MOTU_INFO_IS_STREAMING, "Info/IsStreaming", "Is device streaming", ""));
470     result &= m_MixerContainer->addElement(
471         new InfoElement(*this, MOTU_INFO_SAMPLE_RATE, "Info/SampleRate", "Device sample rate", ""));
472     result &= m_MixerContainer->addElement(
473         new InfoElement(*this, MOTU_INFO_HAS_MIC_INPUTS, "Info/HasMicInputs", "Device has mic inputs", ""));
474     result &= m_MixerContainer->addElement(
475         new InfoElement(*this, MOTU_INFO_HAS_AESEBU_INPUTS, "Info/HasAESEBUInputs", "Device has AES/EBU inputs", ""));
476     result &= m_MixerContainer->addElement(
477         new InfoElement(*this, MOTU_INFO_HAS_SPDIF_INPUTS, "Info/HasSPDIFInputs", "Device has SPDIF inputs", ""));
478
479     if (!addElement(m_MixerContainer)) {
480         debugWarning("Could not register mixer to device\n");
481         // clean up
482         destroyMixer();
483         return false;
484     }
485
486     // Special controls
487     m_ControlContainer = new Control::Container("Control");
488     if (!m_ControlContainer) {
489         debugError("Could not create control container...\n");
490         return false;
491     }
492
493     // Special controls get added here
494
495     if (!result) {
496         debugWarning("One or more device control elements could not be created.");
497         // clean up those that couldn't be created
498         destroyMixer();
499         return false;
500     }
501     if (!addElement(m_ControlContainer)) {
502         debugWarning("Could not register controls to device\n");
503         // clean up
504         destroyMixer();
505         return false;
506     }
507
508     return true;
509 }
510
511
512 bool
513 MotuDevice::destroyMixer() {
514     debugOutput(DEBUG_LEVEL_VERBOSE, "destroy mixer...\n");
515
516     if (m_MixerContainer == NULL) {
517         debugOutput(DEBUG_LEVEL_VERBOSE, "no mixer to destroy...\n");
518         return true;
519     }
520    
521     if (!deleteElement(m_MixerContainer)) {
522         debugError("Mixer present but not registered to the avdevice\n");
523         return false;
524     }
525
526     // remove and delete (as in free) child control elements
527     m_MixerContainer->clearElements(true);
528     delete m_MixerContainer;
529     m_MixerContainer = NULL;
530
531     // remove control container
532     if (m_ControlContainer == NULL) {
533         debugOutput(DEBUG_LEVEL_VERBOSE, "no controls to destroy...\n");
534         return true;
535     }
536    
537     if (!deleteElement(m_ControlContainer)) {
538         debugError("Controls present but not registered to the avdevice\n");
539         return false;
540     }
541    
542     // remove and delete (as in free) child control elements
543     m_ControlContainer->clearElements(true);
544     delete m_ControlContainer;
545     m_ControlContainer = NULL;
546
547     return true;
548 }
549
550 bool
551 MotuDevice::probe( ConfigRom& configRom )
552 {
553     unsigned int vendorId = configRom.getNodeVendorId();
554     unsigned int unitVersion = configRom.getUnitVersion();
555     unsigned int unitSpecifierId = configRom.getUnitSpecifierId();
556
557     for ( unsigned int i = 0;
558           i < ( sizeof( supportedDeviceList )/sizeof( VendorModelEntry ) );
559           ++i )
560     {
561         if ( ( supportedDeviceList[i].vendor_id == vendorId )
562              && ( supportedDeviceList[i].unit_version == unitVersion )
563              && ( supportedDeviceList[i].unit_specifier_id == unitSpecifierId )
564            )
565         {
566             return true;
567         }
568     }
569
570     return false;
571 }
572
573 FFADODevice *
574 MotuDevice::createDevice(DeviceManager& d, std::auto_ptr<ConfigRom>( configRom ))
575 {
576     return new MotuDevice(d, configRom);
577 }
578
579 bool
580 MotuDevice::discover()
581 {
582     unsigned int vendorId = getConfigRom().getNodeVendorId();
583     unsigned int unitVersion = getConfigRom().getUnitVersion();
584     unsigned int unitSpecifierId = getConfigRom().getUnitSpecifierId();
585
586     for ( unsigned int i = 0;
587           i < ( sizeof( supportedDeviceList )/sizeof( VendorModelEntry ) );
588           ++i )
589     {
590         if ( ( supportedDeviceList[i].vendor_id == vendorId )
591              && ( supportedDeviceList[i].unit_version == unitVersion )
592              && ( supportedDeviceList[i].unit_specifier_id == unitSpecifierId )
593            )
594         {
595             m_model = &(supportedDeviceList[i]);
596             m_motu_model=supportedDeviceList[i].model;
597         }
598     }
599
600     if (m_model == NULL) {
601         return false;
602     }
603
604     debugOutput( DEBUG_LEVEL_VERBOSE, "found %s %s\n",
605         m_model->vendor_name, m_model->model_name);
606
607     if (!buildMixer()) {
608         debugWarning("Could not build mixer\n");
609     }
610
611     return true;
612 }
613
614 int
615 MotuDevice::getSamplingFrequency( ) {
616 /*
617  * Retrieve the current sample rate from the MOTU device.
618  */
619     quadlet_t q = ReadRegister(MOTU_REG_CLK_CTRL);
620     int rate = 0;
621
622     switch (q & MOTU_RATE_BASE_MASK) {
623         case MOTU_RATE_BASE_44100:
624             rate = 44100;
625             break;
626         case MOTU_RATE_BASE_48000:
627             rate = 48000;
628             break;
629     }
630     switch (q & MOTU_RATE_MULTIPLIER_MASK) {
631         case MOTU_RATE_MULTIPLIER_2X:
632             rate *= 2;
633             break;
634         case MOTU_RATE_MULTIPLIER_4X:
635             rate *= 4;
636             break;
637     }
638     return rate;
639 }
640
641 int
642 MotuDevice::getConfigurationId()
643 {
644     return 0;
645 }
646
647 bool
648 MotuDevice::setSamplingFrequency( int samplingFrequency )
649 {
650 /*
651  * Set the MOTU device's samplerate.
652  */
653     char *src_name;
654     quadlet_t q, new_rate=0;
655     int i, supported=true, cancel_adat=false;
656
657     if ( samplingFrequency > DevicesProperty[m_motu_model-1].MaxSampleRate )
658        return false;
659
660     switch ( samplingFrequency ) {
661         case 22050:
662         case 24000:
663         case 32000:
664             supported=false;
665             break;
666         case 44100:
667             new_rate = MOTU_RATE_BASE_44100 | MOTU_RATE_MULTIPLIER_1X;
668             break;
669         case 48000:
670             new_rate = MOTU_RATE_BASE_48000 | MOTU_RATE_MULTIPLIER_1X;
671             break;
672         case 88200:
673             new_rate = MOTU_RATE_BASE_44100 | MOTU_RATE_MULTIPLIER_2X;
674             break;
675         case 96000:
676             new_rate = MOTU_RATE_BASE_48000 | MOTU_RATE_MULTIPLIER_2X;
677             break;
678         case 176400:
679             new_rate = MOTU_RATE_BASE_44100 | MOTU_RATE_MULTIPLIER_4X;
680             cancel_adat = true;  // current ADAT protocol doesn't support sample rate > 96000
681             break;
682         case 192000:
683             new_rate = MOTU_RATE_BASE_48000 | MOTU_RATE_MULTIPLIER_4X;
684             cancel_adat = true;
685             break;
686         default:
687             supported=false;
688     }
689
690     // Update the clock control register.  FIXME: while this is now rather
691     // comprehensive there may still be a need to manipulate MOTU_REG_CLK_CTRL
692     // a little more than we do.
693     if (supported) {
694         quadlet_t value=ReadRegister(MOTU_REG_CLK_CTRL);
695
696         // If optical port must be disabled (because a 4x sample rate has
697         // been selected) then do so before changing the sample rate.  At
698         // this stage it will be up to the user to re-enable the optical
699         // port if the sample rate is set to a 1x or 2x rate later.
700         if (cancel_adat) {
701             setOpticalMode(MOTU_DIR_INOUT, MOTU_OPTICAL_MODE_OFF);
702         }
703
704         value &= ~(MOTU_RATE_BASE_MASK|MOTU_RATE_MULTIPLIER_MASK);
705         value |= new_rate;
706
707         // In other OSes bit 26 of MOTU_REG_CLK_CTRL always seems
708         // to be set when this register is written to although the
709         // reason isn't currently known.  When we set it, it appears
710         // to prevent output being produced so we'll leave it unset
711         // until we work out what's going on.  Other systems write
712         // to MOTU_REG_CLK_CTRL multiple times, so that may be
713         // part of the mystery.
714         //   value |= 0x04000000;
715         if (WriteRegister(MOTU_REG_CLK_CTRL, value) == 0) {
716             supported=true;
717         } else {
718             supported=false;
719         }
720         // A write to the rate/clock control register requires the
721         // textual name of the current clock source be sent to the
722         // clock source name registers.
723         switch (value & MOTU_CLKSRC_MASK) {
724             case MOTU_CLKSRC_INTERNAL:
725                 src_name = "Internal        ";
726                 break;
727             case MOTU_CLKSRC_ADAT_OPTICAL:
728                 src_name = "ADAT Optical    ";
729                 break;
730             case MOTU_CLKSRC_SPDIF_TOSLINK:
731                 if (getOpticalMode(MOTU_DIR_IN)  == MOTU_OPTICAL_MODE_TOSLINK)
732                     src_name = "TOSLink         ";
733                 else
734                     src_name = "SPDIF           ";
735                 break;
736             case MOTU_CLKSRC_SMTPE:
737                 src_name = "SMPTE           ";
738                 break;
739             case MOTU_CLKSRC_WORDCLOCK:
740                 src_name = "Word Clock In   ";
741                 break;
742             case MOTU_CLKSRC_ADAT_9PIN:
743                 src_name = "ADAT 9-pin      ";
744                 break;
745             case MOTU_CLKSRC_AES_EBU:
746                 src_name = "AES-EBU         ";
747                 break;
748             default:
749                 src_name = "Unknown         ";
750         }
751         for (i=0; i<16; i+=4) {
752             q = (src_name[i]<<24) | (src_name[i+1]<<16) |
753                 (src_name[i+2]<<8) | src_name[i+3];
754             WriteRegister(MOTU_REG_CLKSRC_NAME0+i, q);
755         }
756     }
757     return supported;
758 }
759
760 FFADODevice::ClockSourceVector
761 MotuDevice::getSupportedClockSources() {
762     FFADODevice::ClockSourceVector r;
763     return r;
764 }
765
766 bool
767 MotuDevice::setActiveClockSource(ClockSource s) {
768     return false;
769 }
770
771 FFADODevice::ClockSource
772 MotuDevice::getActiveClockSource() {
773     ClockSource s;
774     return s;
775 }
776
777 bool
778 MotuDevice::lock() {
779
780     return true;
781 }
782
783
784 bool
785 MotuDevice::unlock() {
786
787     return true;
788 }
789
790 void
791 MotuDevice::showDevice()
792 {
793     debugOutput(DEBUG_LEVEL_VERBOSE,
794         "%s %s at node %d\n", m_model->vendor_name, m_model->model_name,
795         getNodeId());
796 }
797
798 bool
799 MotuDevice::prepare() {
800
801     int samp_freq = getSamplingFrequency();
802     unsigned int optical_in_mode = getOpticalMode(MOTU_DIR_IN);
803     unsigned int optical_out_mode = getOpticalMode(MOTU_DIR_OUT);
804     unsigned int event_size_in = getEventSize(MOTU_DIR_IN);
805     unsigned int event_size_out= getEventSize(MOTU_DIR_OUT);
806
807     debugOutput(DEBUG_LEVEL_NORMAL, "Preparing MotuDevice...\n" );
808
809     // Allocate bandwidth if not previously done.
810     // FIXME: The bandwidth allocation calculation can probably be
811     // refined somewhat since this is currently based on a rudimentary
812     // understanding of the ieee1394 iso protocol.
813     // Currently we assume the following.
814     //   * Ack/iso gap = 0.05 us
815     //   * DATA_PREFIX = 0.16 us
816     //   * DATA_END    = 0.26 us
817     // These numbers are the worst-case figures given in the ieee1394
818     // standard.  This gives approximately 0.5 us of overheads per packet -
819     // around 25 bandwidth allocation units (from the ieee1394 standard 1
820     // bandwidth allocation unit is 125/6144 us).  We further assume the
821     // MOTU is running at S400 (which it should be) so one allocation unit
822     // is equivalent to 1 transmitted byte; thus the bandwidth allocation
823     // required for the packets themselves is just the size of the packet.
824     // We used to allocate based on the maximum packet size (1160 bytes at
825     // 192 kHz for the traveler) but now do this based on the actual device
826     // state by utilising the result from getEventSize() and remembering
827     // that each packet has an 8 byte CIP header.  Note that bandwidth is
828     // allocated on a *per stream* basis - it must be allocated for both the
829     // transmit and receive streams.  While most MOTU modules are close to
830     // symmetric in terms of the number of in/out channels there are
831     // exceptions, so we deal with receive and transmit bandwidth separately.
832     signed int n_events_per_packet = samp_freq<=48000?8:(samp_freq<=96000?16:32);
833     m_rx_bandwidth = 25 + (n_events_per_packet*event_size_in);
834     m_tx_bandwidth = 25 + (n_events_per_packet*event_size_out);
835
836     // Assign iso channels if not already done
837     if (m_iso_recv_channel < 0)
838         m_iso_recv_channel = get1394Service().allocateIsoChannelGeneric(m_rx_bandwidth);
839
840     if (m_iso_send_channel < 0)
841         m_iso_send_channel = get1394Service().allocateIsoChannelGeneric(m_tx_bandwidth);
842
843     debugOutput(DEBUG_LEVEL_VERBOSE, "recv channel = %d, send channel = %d\n",
844         m_iso_recv_channel, m_iso_send_channel);
845
846     if (m_iso_recv_channel<0 || m_iso_send_channel<0) {
847         // be nice and deallocate
848         if (m_iso_recv_channel >= 0)
849             get1394Service().freeIsoChannel(m_iso_recv_channel);
850         if (m_iso_send_channel >= 0)
851             get1394Service().freeIsoChannel(m_iso_send_channel);
852
853         debugFatal("Could not allocate iso channels!\n");
854         return false;
855     }
856
857     m_receiveProcessor=new Streaming::MotuReceiveStreamProcessor(*this, event_size_in);
858
859     // The first thing is to initialize the processor.  This creates the
860     // data structures.
861     if(!m_receiveProcessor->init()) {
862         debugFatal("Could not initialize receive processor!\n");
863         return false;
864     }
865     m_receiveProcessor->setVerboseLevel(getDebugLevel());
866
867     // Now we add ports to the processor
868     debugOutput(DEBUG_LEVEL_VERBOSE,"Adding ports to receive processor\n");
869
870     char *buff;
871     Streaming::Port *p=NULL;
872
873     // retrieve the ID
874     std::string id=std::string("dev?");
875     if(!getOption("id", id)) {
876         debugWarning("Could not retrieve id parameter, defauling to 'dev?'\n");
877     }
878
879     // Add audio capture ports
880     if (!addDirPorts(Streaming::Port::E_Capture, samp_freq, optical_in_mode)) {
881         return false;
882     }
883
884     // Add MIDI port.  The MOTU only has one MIDI input port, with each
885     // MIDI byte sent using a 3 byte sequence starting at byte 4 of the
886     // event data.
887     asprintf(&buff,"%s_cap_MIDI0",id.c_str());
888     p = new Streaming::MotuMidiPort(*m_receiveProcessor, buff,
889         Streaming::Port::E_Capture, 4);
890     if (!p) {
891         debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n", buff);
892     }
893     free(buff);
894
895     // example of adding an control port:
896 //    asprintf(&buff,"%s_cap_%s",id.c_str(),"myportnamehere");
897 //    p=new Streaming::MotuControlPort(
898 //            buff,
899 //            Streaming::Port::E_Capture,
900 //            0 // you can add all other port specific stuff you
901 //              // need to pass by extending MotuXXXPort and MotuPortInfo
902 //    );
903 //    free(buff);
904 //
905 //    if (!p) {
906 //        debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n",buff);
907 //    } else {
908 //
909 //        if (!m_receiveProcessor->addPort(p)) {
910 //            debugWarning("Could not register port with stream processor\n");
911 //            return false;
912 //        } else {
913 //            debugOutput(DEBUG_LEVEL_VERBOSE, "Added port %s\n",buff);
914 //        }
915 //    }
916
917     // Do the same for the transmit processor
918     m_transmitProcessor=new Streaming::MotuTransmitStreamProcessor(*this, event_size_out);
919
920     m_transmitProcessor->setVerboseLevel(getDebugLevel());
921
922     if(!m_transmitProcessor->init()) {
923         debugFatal("Could not initialize transmit processor!\n");
924         return false;
925     }
926
927     // Now we add ports to the processor
928     debugOutput(DEBUG_LEVEL_VERBOSE,"Adding ports to transmit processor\n");
929
930     // Add audio playback ports
931     if (!addDirPorts(Streaming::Port::E_Playback, samp_freq, optical_out_mode)) {
932         return false;
933     }
934
935     // Add MIDI port.  The MOTU only has one output MIDI port, with each
936     // MIDI byte transmitted using a 3 byte sequence starting at byte 4
937     // of the event data.
938     asprintf(&buff,"%s_pbk_MIDI0",id.c_str());
939     p = new Streaming::MotuMidiPort(*m_transmitProcessor, buff,
940         Streaming::Port::E_Capture, 4);
941     if (!p) {
942         debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n", buff);
943     }
944     free(buff);
945
946     // example of adding an control port:
947 //    asprintf(&buff,"%s_pbk_%s",id.c_str(),"myportnamehere");
948 //
949 //    p=new Streaming::MotuControlPort(
950 //            buff,
951 //            Streaming::Port::E_Playback,
952 //            0 // you can add all other port specific stuff you
953 //              // need to pass by extending MotuXXXPort and MotuPortInfo
954 //    );
955 //    free(buff);
956 //
957 //    if (!p) {
958 //        debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n",buff);
959 //    } else {
960 //        if (!m_transmitProcessor->addPort(p)) {
961 //            debugWarning("Could not register port with stream processor\n");
962 //            return false;
963 //        } else {
964 //            debugOutput(DEBUG_LEVEL_VERBOSE, "Added port %s\n",buff);
965 //        }
966 //    }
967
968     return true;
969 }
970
971 int
972 MotuDevice::getStreamCount() {
973      return 2; // one receive, one transmit
974 }
975
976 Streaming::StreamProcessor *
977 MotuDevice::getStreamProcessorByIndex(int i) {
978     switch (i) {
979     case 0:
980         return m_receiveProcessor;
981     case 1:
982          return m_transmitProcessor;
983     default:
984         return NULL;
985     }
986     return 0;
987 }
988
989 bool
990 MotuDevice::startStreamByIndex(int i) {
991
992 quadlet_t isoctrl = ReadRegister(MOTU_REG_ISOCTRL);
993
994     // NOTE: this assumes that you have two streams
995     switch (i) {
996     case 0:
997         // TODO: do the stuff that is nescessary to make the device
998         // receive a stream
999
1000         // Set the streamprocessor channel to the one obtained by
1001         // the connection management
1002         m_receiveProcessor->setChannel(m_iso_recv_channel);
1003
1004         // Mask out current transmit settings of the MOTU and replace
1005         // with new ones.  Turn bit 24 on to enable changes to the
1006         // MOTU's iso transmit settings when the iso control register
1007         // is written.  Bit 23 enables iso transmit from the MOTU.
1008         isoctrl &= 0xff00ffff;
1009         isoctrl |= (m_iso_recv_channel << 16);
1010         isoctrl |= 0x00c00000;
1011         WriteRegister(MOTU_REG_ISOCTRL, isoctrl);
1012         break;
1013     case 1:
1014         // TODO: do the stuff that is nescessary to make the device
1015         // transmit a stream
1016
1017         // Set the streamprocessor channel to the one obtained by
1018         // the connection management
1019         m_transmitProcessor->setChannel(m_iso_send_channel);
1020
1021         // Mask out current receive settings of the MOTU and replace
1022         // with new ones.  Turn bit 31 on to enable changes to the
1023         // MOTU's iso receive settings when the iso control register
1024         // is written.  Bit 30 enables iso receive by the MOTU.
1025         isoctrl &= 0x00ffffff;
1026         isoctrl |= (m_iso_send_channel << 24);
1027         isoctrl |= 0xc0000000;
1028         WriteRegister(MOTU_REG_ISOCTRL, isoctrl);
1029         break;
1030
1031     default: // Invalid stream index
1032         return false;
1033     }
1034
1035     return true;
1036 }
1037
1038 bool
1039 MotuDevice::stopStreamByIndex(int i) {
1040
1041 quadlet_t isoctrl = ReadRegister(MOTU_REG_ISOCTRL);
1042
1043     // TODO: connection management: break connection
1044     // cfr the start function
1045
1046     // NOTE: this assumes that you have two streams
1047     switch (i) {
1048     case 0:
1049         // Turn bit 22 off to disable iso send by the MOTU.  Turn
1050         // bit 23 on to enable changes to the MOTU's iso transmit
1051         // settings when the iso control register is written.
1052         isoctrl &= 0xffbfffff;
1053         isoctrl |= 0x00800000;
1054         WriteRegister(MOTU_REG_ISOCTRL, isoctrl);
1055         break;
1056     case 1:
1057         // Turn bit 30 off to disable iso receive by the MOTU.  Turn
1058         // bit 31 on to enable changes to the MOTU's iso receive
1059         // settings when the iso control register is written.
1060         isoctrl &= 0xbfffffff;
1061         isoctrl |= 0x80000000;
1062         WriteRegister(MOTU_REG_ISOCTRL, isoctrl);
1063         break;
1064
1065     default: // Invalid stream index
1066         return false;
1067     }
1068
1069     return true;
1070 }
1071
1072 signed int MotuDevice::getIsoRecvChannel(void) {
1073     return m_iso_recv_channel;
1074 }
1075
1076 signed int MotuDevice::getIsoSendChannel(void) {
1077     return m_iso_send_channel;
1078 }
1079
1080 unsigned int MotuDevice::getOpticalMode(unsigned int dir) {
1081     unsigned int reg = ReadRegister(MOTU_REG_ROUTE_PORT_CONF);
1082
1083 debugOutput(DEBUG_LEVEL_VERBOSE, "optical mode: %x %x %x %x\n",dir, reg, reg & MOTU_OPTICAL_IN_MODE_MASK,
1084 reg & MOTU_OPTICAL_OUT_MODE_MASK);
1085
1086     if (dir == MOTU_DIR_IN)
1087         return (reg & MOTU_OPTICAL_IN_MODE_MASK) >> 8;
1088     else
1089         return (reg & MOTU_OPTICAL_OUT_MODE_MASK) >> 10;
1090 }
1091
1092 signed int MotuDevice::setOpticalMode(unsigned int dir, unsigned int mode) {
1093     unsigned int reg = ReadRegister(MOTU_REG_ROUTE_PORT_CONF);
1094     unsigned int opt_ctrl = 0x0000002;
1095
1096     /* THe 896HD doesn't have an SPDIF/TOSLINK optical mode, so don't try to
1097      * set it
1098      */
1099     if (m_motu_model==MOTU_MODEL_896HD && mode==MOTU_OPTICAL_MODE_TOSLINK)
1100         return -1;
1101
1102     // Set up the optical control register value according to the current
1103     // optical port modes.  At this stage it's not completely understood
1104     // what the "Optical control" register does, so the values it's set to
1105     // are more or less "magic" numbers.
1106     if (reg & MOTU_OPTICAL_IN_MODE_MASK != (MOTU_OPTICAL_MODE_ADAT<<8))
1107         opt_ctrl |= 0x00000080;
1108     if (reg & MOTU_OPTICAL_OUT_MODE_MASK != (MOTU_OPTICAL_MODE_ADAT<<10))
1109         opt_ctrl |= 0x00000040;
1110
1111     if (mode & MOTU_DIR_IN) {
1112         reg &= ~MOTU_OPTICAL_IN_MODE_MASK;
1113         reg |= (mode << 8) & MOTU_OPTICAL_IN_MODE_MASK;
1114         if (mode != MOTU_OPTICAL_MODE_ADAT)
1115             opt_ctrl |= 0x00000080;
1116         else
1117             opt_ctrl &= ~0x00000080;
1118     }
1119     if (mode & MOTU_DIR_OUT) {
1120         reg &= ~MOTU_OPTICAL_OUT_MODE_MASK;
1121         reg |= (mode <<10) & MOTU_OPTICAL_OUT_MODE_MASK;
1122         if (mode != MOTU_OPTICAL_MODE_ADAT)
1123             opt_ctrl |= 0x00000040;
1124         else
1125             opt_ctrl &= ~0x00000040;
1126     }
1127
1128     // FIXME: there seems to be more to it than this, but for
1129     // the moment at least this seems to work.
1130     WriteRegister(MOTU_REG_ROUTE_PORT_CONF, reg);
1131     return WriteRegister(MOTU_REG_OPTICAL_CTRL, opt_ctrl);
1132 }
1133
1134 signed int MotuDevice::getEventSize(unsigned int direction) {
1135 //
1136 // Return the size in bytes of a single event sent to (dir==MOTU_OUT) or
1137 // from (dir==MOTU_IN) the MOTU as part of an iso data packet.
1138 //
1139 // FIXME: for performance it may turn out best to calculate the event
1140 // size in setOpticalMode and cache the result in a data field.  However,
1141 // as it stands this will not adapt to dynamic changes in sample rate - we'd
1142 // need a setFrameRate() for that.
1143 //
1144 // At the very least an event consists of the SPH (4 bytes) and the control/MIDI
1145 // bytes (6 bytes).
1146 // Note that all audio channels are sent using 3 bytes.
1147 signed int sample_rate = getSamplingFrequency();
1148 signed int optical_mode = getOpticalMode(direction);
1149 signed int size = 4+6;
1150
1151 unsigned int i;
1152 unsigned int dir = direction==Streaming::Port::E_Capture?MOTU_DIR_IN:MOTU_DIR_OUT;
1153 unsigned int flags = (1 << ( optical_mode + 4 ));
1154
1155     if ( sample_rate > 96000 )
1156         flags |= MOTU_PA_RATE_4x;
1157     else if ( sample_rate > 48000 )
1158         flags |= MOTU_PA_RATE_2x;
1159     else
1160         flags |= MOTU_PA_RATE_1x;
1161
1162     for (i=0; i < DevicesProperty[m_motu_model-1].n_port_entries; i++) {
1163         if (( DevicesProperty[m_motu_model-1].port_entry[i].port_dir & dir ) &&
1164            ( DevicesProperty[m_motu_model-1].port_entry[i].port_flags & MOTU_PA_RATE_MASK & flags ) &&
1165            ( DevicesProperty[m_motu_model-1].port_entry[i].port_flags & MOTU_PA_OPTICAL_MASK & flags )) {
1166             size += 3;
1167         }
1168     }
1169
1170     // Finally round size up to the next quadlet boundary
1171     return ((size+3)/4)*4;
1172 }
1173 /* ======================================================================= */
1174
1175 bool MotuDevice::addPort(Streaming::StreamProcessor *s_processor,
1176   char *name, enum Streaming::Port::E_Direction direction,
1177   int position, int size) {
1178 /*
1179  * Internal helper function to add a MOTU port to a given stream processor.
1180  * This just saves the unnecessary replication of what is essentially
1181  * boilerplate code.  Note that the port name is freed by this function
1182  * prior to exit.
1183  */
1184 Streaming::Port *p=NULL;
1185
1186     p = new Streaming::MotuAudioPort(*s_processor, name, direction, position, size);
1187
1188     if (!p) {
1189         debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n",name);
1190     }
1191     free(name);
1192     return true;
1193 }
1194 /* ======================================================================= */
1195
1196 bool MotuDevice::addDirPorts(
1197   enum Streaming::Port::E_Direction direction,
1198   unsigned int sample_rate, unsigned int optical_mode) {
1199 /*
1200  * Internal helper method: adds all required ports for the given direction
1201  * based on the indicated sample rate and optical mode.
1202  *
1203  * Notes: currently ports are not created if they are disabled due to sample
1204  * rate or optical mode.  However, it might be better to unconditionally
1205  * create all ports and just disable those which are not active.
1206  */
1207 const char *mode_str = direction==Streaming::Port::E_Capture?"cap":"pbk";
1208 Streaming::StreamProcessor *s_processor;
1209 unsigned int i;
1210 char *buff;
1211 unsigned int dir = direction==Streaming::Port::E_Capture?MOTU_DIR_IN:MOTU_DIR_OUT;
1212 unsigned int flags = (1 << ( optical_mode + 4 ));
1213
1214     if ( sample_rate > 96000 )
1215         flags |= MOTU_PA_RATE_4x;
1216     else if ( sample_rate > 48000 )
1217         flags |= MOTU_PA_RATE_2x;
1218     else
1219         flags |= MOTU_PA_RATE_1x;
1220
1221     // retrieve the ID
1222     std::string id=std::string("dev?");
1223     if(!getOption("id", id)) {
1224         debugWarning("Could not retrieve id parameter, defaulting to 'dev?'\n");
1225     }
1226
1227     if (direction == Streaming::Port::E_Capture) {
1228         s_processor = m_receiveProcessor;
1229     } else {
1230         s_processor = m_transmitProcessor;
1231     }
1232
1233     for (i=0; i < DevicesProperty[m_motu_model-1].n_port_entries; i++) {
1234         if (( DevicesProperty[m_motu_model-1].port_entry[i].port_dir & dir ) &&
1235            ( DevicesProperty[m_motu_model-1].port_entry[i].port_flags & MOTU_PA_RATE_MASK & flags ) &&
1236            ( DevicesProperty[m_motu_model-1].port_entry[i].port_flags & MOTU_PA_OPTICAL_MASK & flags )) {
1237             asprintf(&buff,"%s_%s_%s" , id.c_str(), mode_str,
1238               DevicesProperty[m_motu_model-1].port_entry[i].port_name);
1239             if (!addPort(s_processor, buff, direction, DevicesProperty[m_motu_model-1].port_entry[i].port_offset, 0))
1240                 return false;
1241         }
1242     }
1243    
1244     return true;
1245 }
1246 /* ======================================================================== */
1247
1248 unsigned int MotuDevice::ReadRegister(unsigned int reg) {
1249 /*
1250  * Attempts to read the requested register from the MOTU.
1251  */
1252
1253   quadlet_t quadlet;
1254
1255   quadlet = 0;
1256   // Note: 1394Service::read() expects a physical ID, not the node id
1257   if (get1394Service().read(0xffc0 | getNodeId(), MOTU_BASE_ADDR+reg, 1, &quadlet) < 0) {
1258     debugError("Error doing motu read from register 0x%06x\n",reg);
1259   }
1260
1261   return ntohl(quadlet);
1262 }
1263
1264 signed int MotuDevice::WriteRegister(unsigned int reg, quadlet_t data) {
1265 /*
1266  * Attempts to write the given data to the requested MOTU register.
1267  */
1268
1269   unsigned int err = 0;
1270   data = htonl(data);
1271
1272   // Note: 1394Service::write() expects a physical ID, not the node id
1273   if (get1394Service().write(0xffc0 | getNodeId(), MOTU_BASE_ADDR+reg, 1, &data) < 0) {
1274     err = 1;
1275     debugError("Error doing motu write to register 0x%06x\n",reg);
1276   }
1277
1278   SleepRelativeUsec(100);
1279   return (err==0)?0:-1;
1280 }
1281
1282 }
Note: See TracBrowser for help on using the browser.