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

Revision 785, 36.2 kB (checked in by ppalmers, 14 years ago)

shutdown fix for DICE and MOTU

Line 
1 /*
2  * Copyright (C) 2005-2007 by Pieter Palmers
3  * Copyright (C) 2005-2007 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 3 of the License, or
13  * (at your option) any later version.
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 <string>
42 #include <stdint.h>
43 #include <assert.h>
44 #include <netinet/in.h>
45 #include <iostream>
46 #include <sstream>
47
48 #include <libraw1394/csr.h>
49
50 namespace Motu {
51
52 // to define the supported devices
53 static VendorModelEntry supportedDeviceList[] =
54 {
55 //  {vendor_id, model_id, unit_version, unit_specifier_id, model, vendor_name,model_name}
56     {FW_VENDORID_MOTU, 0, 0x00000003, 0x000001f2, MOTUFW_MODEL_828mkII, "MOTU", "828MkII"},
57     {FW_VENDORID_MOTU, 0, 0x00000009, 0x000001f2, MOTUFW_MODEL_TRAVELER, "MOTU", "Traveler"},
58     {FW_VENDORID_MOTU, 0, 0x0000000d, 0x000001f2, MOTUFW_MODEL_ULTRALITE, "MOTU", "UltraLite"},
59     {FW_VENDORID_MOTU, 0, 0x0000000f, 0x000001f2, MOTUFW_MODEL_8PRE, "MOTU", "8pre"},
60 };
61
62 // Ports declarations
63 const PortEntry Ports_828MKII[] =
64 {
65     {"Main-L", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 40},
66     {"Main-R", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 43},
67     {"Mix-L", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 10},
68     {"Mix-R", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 13},
69     {"Analog1", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 16},
70     {"Analog2", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 19},
71     {"Analog3", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 22},
72     {"Analog4", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 25},
73     {"Analog5", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 28},
74     {"Analog6", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 31},
75     {"Analog7", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 34},
76     {"Analog8", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 37},
77     {"Phones-L", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 10},
78     {"Phones-R", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 13},
79     {"Mic1", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 40},
80     {"Mic2", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 43},
81     {"SPDIF1", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 46},
82     {"SPDIF2", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 49},
83     {"ADAT1", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ADAT, 52},
84     {"ADAT2", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ADAT, 55},
85     {"ADAT3", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ADAT, 58},
86     {"ADAT4", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ADAT, 61},
87     {"ADAT5", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_1x|MOTUFW_PA_OPTICAL_ADAT, 63},
88     {"ADAT6", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_1x|MOTUFW_PA_OPTICAL_ADAT, 66},
89     {"ADAT7", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_1x|MOTUFW_PA_OPTICAL_ADAT, 69},
90     {"ADAT8", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_1x|MOTUFW_PA_OPTICAL_ADAT, 72},
91 };
92
93 const PortEntry Ports_TRAVELER[] =
94 {
95     {"Mix-L", MOTUFW_DIR_IN, MOTUFW_PA_RATE_1x2x|MOTUFW_PA_OPTICAL_ANY, 10},
96     {"Mix-R", MOTUFW_DIR_IN, MOTUFW_PA_RATE_1x2x|MOTUFW_PA_OPTICAL_ANY, 13},
97     {"Phones-L", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_1x2x|MOTUFW_PA_OPTICAL_ANY, 10},
98     {"Phones-R", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_1x2x|MOTUFW_PA_OPTICAL_ANY, 13},
99     {"Analog1", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 16},
100     {"Analog2", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 19},
101     {"Analog3", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 22},
102     {"Analog4", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 25},
103     {"Analog5", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 28},
104     {"Analog6", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 31},
105     {"Analog7", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 34},
106     {"Analog8", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 37},
107     {"AES/EBU1", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_1x2x|MOTUFW_PA_OPTICAL_ANY, 40},
108     {"AES/EBU2", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_1x2x|MOTUFW_PA_OPTICAL_ANY, 43},
109     {"SPDIF1", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_1x2x|MOTUFW_PA_OPTICAL_OFF|MOTUFW_PA_OPTICAL_ADAT, 46},
110     {"SPDIF2", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_1x2x|MOTUFW_PA_OPTICAL_OFF|MOTUFW_PA_OPTICAL_ADAT, 49},
111     {"Toslink1", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_1x2x|MOTUFW_PA_OPTICAL_TOSLINK, 46},
112     {"Toslink2", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_1x2x|MOTUFW_PA_OPTICAL_TOSLINK, 49},
113     {"ADAT1", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_1x2x|MOTUFW_PA_OPTICAL_ADAT, 52},
114     {"ADAT2", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_1x2x|MOTUFW_PA_OPTICAL_ADAT, 55},
115     {"ADAT3", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_1x2x|MOTUFW_PA_OPTICAL_ADAT, 58},
116     {"ADAT4", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_1x2x|MOTUFW_PA_OPTICAL_ADAT, 61},
117     {"ADAT5", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_1x|MOTUFW_PA_OPTICAL_ADAT, 63},
118     {"ADAT6", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_1x|MOTUFW_PA_OPTICAL_ADAT, 66},
119     {"ADAT7", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_1x|MOTUFW_PA_OPTICAL_ADAT, 69},
120     {"ADAT8", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_1x|MOTUFW_PA_OPTICAL_ADAT, 72},
121 };
122
123 const PortEntry Ports_ULTRALITE[] =
124 {
125     {"Main-L", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 40},
126     {"Main-R", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 43},
127     {"Mix-L", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 10},
128     {"Mix-R", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 13},
129     {"Mic1", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 16},
130     {"Mic2", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 19},
131     {"Analog1", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 16},
132     {"Analog2", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 19},
133     {"Analog3", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 22},
134     {"Analog4", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 25},
135     {"Analog5", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 28},
136     {"Analog6", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 31},
137     {"Analog7", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 34},
138     {"Analog8", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 37},
139     {"Phones-L", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 10},
140     {"Phones-R", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 13},
141     {"SPDIF1", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 40},
142     {"SPDIF2", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 43},
143     {"SPDIF1", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 46},
144     {"SPDIF2", MOTUFW_DIR_INOUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 49},
145 };
146
147 const PortEntry Ports_8PRE[] =
148 {
149     {"Analog1", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 16},
150     {"Analog2", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 19},
151     {"Analog3", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 22},
152     {"Analog4", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 25},
153     {"Analog5", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 28},
154     {"Analog6", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 31},
155     {"Analog7", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 34},
156     {"Analog8", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 37},
157     {"Mix-L", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 10},
158     {"Mix-R", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 13},
159     {"Main-L", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 16},
160     {"Main-R", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 19},
161     {"Phones-L", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 10},
162     {"Phones-R", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ANY, 13},
163     {"ADAT1", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ADAT, 40},
164     {"ADAT1", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ADAT, 22},
165     {"ADAT2", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ADAT, 43},
166     {"ADAT2", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ADAT, 25},
167     {"ADAT3", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ADAT, 46},
168     {"ADAT3", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ADAT, 28},
169     {"ADAT4", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ADAT, 49},
170     {"ADAT4", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ADAT, 31},
171     {"ADAT5", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ADAT, 52},
172     {"ADAT5", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ADAT, 34},
173     {"ADAT6", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ADAT, 55},
174     {"ADAT6", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ADAT, 37},
175     {"ADAT7", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ADAT, 58},
176     {"ADAT7", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ADAT, 40},
177     {"ADAT8", MOTUFW_DIR_IN, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ADAT, 61},
178     {"ADAT8", MOTUFW_DIR_OUT, MOTUFW_PA_RATE_ANY|MOTUFW_PA_OPTICAL_ADAT, 43},
179 };
180
181 const DevicePropertyEntry DevicesProperty[] = {
182 //  { Ports_map,       sizeof( Ports_map ),        MaxSR },
183     { Ports_828MKII,   sizeof( Ports_828MKII ),    96000 },
184     { Ports_TRAVELER,  sizeof( Ports_TRAVELER ),  192000 },
185     { Ports_ULTRALITE, sizeof( Ports_ULTRALITE ),  96000 },
186     { Ports_8PRE,      sizeof( Ports_8PRE ),       96000 },
187 };
188
189 MotuDevice::MotuDevice( DeviceManager& d, std::auto_ptr<ConfigRom>( configRom ))
190     : FFADODevice( d, configRom )
191     , m_motu_model( MOTUFW_MODEL_NONE )
192     , m_iso_recv_channel ( -1 )
193     , m_iso_send_channel ( -1 )
194     , m_rx_bandwidth ( -1 )
195     , m_tx_bandwidth ( -1 )
196     , m_receiveProcessor ( 0 )
197     , m_transmitProcessor ( 0 )
198
199 {
200     debugOutput( DEBUG_LEVEL_VERBOSE, "Created Motu::MotuDevice (NodeID %d)\n",
201                  getConfigRom().getNodeId() );
202
203 }
204
205 MotuDevice::~MotuDevice()
206 {
207     delete m_receiveProcessor;
208     delete m_transmitProcessor;
209
210     // Free ieee1394 bus resources if they have been allocated
211     if (m_iso_recv_channel>=0 && !get1394Service().freeIsoChannel(m_iso_recv_channel)) {
212         debugOutput(DEBUG_LEVEL_VERBOSE, "Could not free recv iso channel %d\n", m_iso_recv_channel);
213     }
214     if (m_iso_send_channel>=0 && !get1394Service().freeIsoChannel(m_iso_send_channel)) {
215         debugOutput(DEBUG_LEVEL_VERBOSE, "Could not free send iso channel %d\n", m_iso_send_channel);
216     }
217 }
218
219 bool
220 MotuDevice::probe( ConfigRom& configRom )
221 {
222     unsigned int vendorId = configRom.getNodeVendorId();
223 //     unsigned int modelId = configRom.getModelId();
224     unsigned int unitVersion = configRom.getUnitVersion();
225     unsigned int unitSpecifierId = configRom.getUnitSpecifierId();
226
227     for ( unsigned int i = 0;
228           i < ( sizeof( supportedDeviceList )/sizeof( VendorModelEntry ) );
229           ++i )
230     {
231         if ( ( supportedDeviceList[i].vendor_id == vendorId )
232 //              && ( supportedDeviceList[i].model_id == modelId )
233              && ( supportedDeviceList[i].unit_version == unitVersion )
234              && ( supportedDeviceList[i].unit_specifier_id == unitSpecifierId )
235            )
236         {
237             return true;
238         }
239     }
240
241     return false;
242 }
243
244 FFADODevice *
245 MotuDevice::createDevice(DeviceManager& d, std::auto_ptr<ConfigRom>( configRom ))
246 {
247     return new MotuDevice(d, configRom);
248 }
249
250 bool
251 MotuDevice::discover()
252 {
253     unsigned int vendorId = getConfigRom().getNodeVendorId();
254 //     unsigned int modelId = getConfigRom().getModelId();
255     unsigned int unitVersion = getConfigRom().getUnitVersion();
256     unsigned int unitSpecifierId = getConfigRom().getUnitSpecifierId();
257
258     for ( unsigned int i = 0;
259           i < ( sizeof( supportedDeviceList )/sizeof( VendorModelEntry ) );
260           ++i )
261     {
262         if ( ( supportedDeviceList[i].vendor_id == vendorId )
263 //              && ( supportedDeviceList[i].model_id == modelId )
264              && ( supportedDeviceList[i].unit_version == unitVersion )
265              && ( supportedDeviceList[i].unit_specifier_id == unitSpecifierId )
266            )
267         {
268             m_model = &(supportedDeviceList[i]);
269             m_motu_model=supportedDeviceList[i].model;
270         }
271     }
272
273     if (m_model != NULL) {
274         debugOutput( DEBUG_LEVEL_VERBOSE, "found %s %s\n",
275                 m_model->vendor_name, m_model->model_name);
276         return true;
277     }
278
279     return false;
280 }
281
282 int
283 MotuDevice::getSamplingFrequency( ) {
284 /*
285  * Retrieve the current sample rate from the MOTU device.
286  */
287     quadlet_t q = ReadRegister(MOTUFW_REG_CLK_CTRL);
288     int rate = 0;
289
290     switch (q & MOTUFW_RATE_BASE_MASK) {
291         case MOTUFW_RATE_BASE_44100:
292             rate = 44100;
293             break;
294         case MOTUFW_RATE_BASE_48000:
295             rate = 48000;
296             break;
297     }
298     switch (q & MOTUFW_RATE_MULTIPLIER_MASK) {
299         case MOTUFW_RATE_MULTIPLIER_2X:
300             rate *= 2;
301             break;
302         case MOTUFW_RATE_MULTIPLIER_4X:
303             rate *= 4;
304             break;
305     }
306     return rate;
307 }
308
309 int
310 MotuDevice::getConfigurationId()
311 {
312     return 0;
313 }
314
315 bool
316 MotuDevice::setSamplingFrequency( int samplingFrequency )
317 {
318 /*
319  * Set the MOTU device's samplerate.
320  */
321     char *src_name;
322     quadlet_t q, new_rate=0;
323     int i, supported=true, cancel_adat=false;
324
325     if ( samplingFrequency > DevicesProperty[m_motu_model-1].MaxSampleRate )
326        return false;
327
328     switch ( samplingFrequency ) {
329         case 22050:
330         case 24000:
331         case 32000:
332             supported=false;
333             break;
334         case 44100:
335             new_rate = MOTUFW_RATE_BASE_44100 | MOTUFW_RATE_MULTIPLIER_1X;
336             break;
337         case 48000:
338             new_rate = MOTUFW_RATE_BASE_48000 | MOTUFW_RATE_MULTIPLIER_1X;
339             break;
340         case 88200:
341             new_rate = MOTUFW_RATE_BASE_44100 | MOTUFW_RATE_MULTIPLIER_2X;
342             break;
343         case 96000:
344             new_rate = MOTUFW_RATE_BASE_48000 | MOTUFW_RATE_MULTIPLIER_2X;
345             break;
346         case 176400:
347             new_rate = MOTUFW_RATE_BASE_44100 | MOTUFW_RATE_MULTIPLIER_4X;
348             cancel_adat = true;  // current ADAT protocol doesn't support sample rate > 96000
349             break;
350         case 192000:
351             new_rate = MOTUFW_RATE_BASE_48000 | MOTUFW_RATE_MULTIPLIER_4X;
352             cancel_adat = true;
353             break;
354         default:
355             supported=false;
356     }
357
358     // Update the clock control register.  FIXME: while this is now rather
359     // comprehensive there may still be a need to manipulate MOTUFW_REG_CLK_CTRL
360     // a little more than we do.
361     if (supported) {
362         quadlet_t value=ReadRegister(MOTUFW_REG_CLK_CTRL);
363
364         // If optical port must be disabled (because a 4x sample rate has
365         // been selected) then do so before changing the sample rate.  At
366         // this stage it will be up to the user to re-enable the optical
367         // port if the sample rate is set to a 1x or 2x rate later.
368         if (cancel_adat) {
369             setOpticalMode(MOTUFW_DIR_INOUT, MOTUFW_OPTICAL_MODE_OFF);
370         }
371
372         value &= ~(MOTUFW_RATE_BASE_MASK|MOTUFW_RATE_MULTIPLIER_MASK);
373         value |= new_rate;
374
375         // In other OSes bit 26 of MOTUFW_REG_CLK_CTRL always seems
376         // to be set when this register is written to although the
377         // reason isn't currently known.  When we set it, it appears
378         // to prevent output being produced so we'll leave it unset
379         // until we work out what's going on.  Other systems write
380         // to MOTUFW_REG_CLK_CTRL multiple times, so that may be
381         // part of the mystery.
382         //   value |= 0x04000000;
383         if (WriteRegister(MOTUFW_REG_CLK_CTRL, value) == 0) {
384             supported=true;
385         } else {
386             supported=false;
387         }
388         // A write to the rate/clock control register requires the
389         // textual name of the current clock source be sent to the
390         // clock source name registers.
391         switch (value & MOTUFW_CLKSRC_MASK) {
392             case MOTUFW_CLKSRC_INTERNAL:
393                 src_name = "Internal        ";
394                 break;
395             case MOTUFW_CLKSRC_ADAT_OPTICAL:
396                 src_name = "ADAT Optical    ";
397                 break;
398             case MOTUFW_CLKSRC_SPDIF_TOSLINK:
399                 if (getOpticalMode(MOTUFW_DIR_IN)  == MOTUFW_OPTICAL_MODE_TOSLINK)
400                     src_name = "TOSLink         ";
401                 else
402                     src_name = "SPDIF           ";
403                 break;
404             case MOTUFW_CLKSRC_SMTPE:
405                 src_name = "SMPTE           ";
406                 break;
407             case MOTUFW_CLKSRC_WORDCLOCK:
408                 src_name = "Word Clock In   ";
409                 break;
410             case MOTUFW_CLKSRC_ADAT_9PIN:
411                 src_name = "ADAT 9-pin      ";
412                 break;
413             case MOTUFW_CLKSRC_AES_EBU:
414                 src_name = "AES-EBU         ";
415                 break;
416             default:
417                 src_name = "Unknown         ";
418         }
419         for (i=0; i<16; i+=4) {
420             q = (src_name[i]<<24) | (src_name[i+1]<<16) |
421                 (src_name[i+2]<<8) | src_name[i+3];
422             WriteRegister(MOTUFW_REG_CLKSRC_NAME0+i, q);
423         }
424     }
425     return supported;
426 }
427
428 FFADODevice::ClockSourceVector
429 MotuDevice::getSupportedClockSources() {
430     FFADODevice::ClockSourceVector r;
431     return r;
432 }
433
434 bool
435 MotuDevice::setActiveClockSource(ClockSource s) {
436     return false;
437 }
438
439 FFADODevice::ClockSource
440 MotuDevice::getActiveClockSource() {
441     ClockSource s;
442     return s;
443 }
444
445 bool
446 MotuDevice::lock() {
447
448     return true;
449 }
450
451
452 bool
453 MotuDevice::unlock() {
454
455     return true;
456 }
457
458 void
459 MotuDevice::showDevice()
460 {
461     debugOutput(DEBUG_LEVEL_VERBOSE,
462         "%s %s at node %d\n", m_model->vendor_name, m_model->model_name,
463         getNodeId());
464 }
465
466 bool
467 MotuDevice::prepare() {
468
469     int samp_freq = getSamplingFrequency();
470     unsigned int optical_in_mode = getOpticalMode(MOTUFW_DIR_IN);
471     unsigned int optical_out_mode = getOpticalMode(MOTUFW_DIR_OUT);
472     unsigned int event_size_in = getEventSize(MOTUFW_DIR_IN);
473     unsigned int event_size_out= getEventSize(MOTUFW_DIR_OUT);
474
475     debugOutput(DEBUG_LEVEL_NORMAL, "Preparing MotuDevice...\n" );
476
477     // Allocate bandwidth if not previously done.
478     // FIXME: The bandwidth allocation calculation can probably be
479     // refined somewhat since this is currently based on a rudimentary
480     // understanding of the ieee1394 iso protocol.
481     // Currently we assume the following.
482     //   * Ack/iso gap = 0.05 us
483     //   * DATA_PREFIX = 0.16 us
484     //   * DATA_END    = 0.26 us
485     // These numbers are the worst-case figures given in the ieee1394
486     // standard.  This gives approximately 0.5 us of overheads per packet -
487     // around 25 bandwidth allocation units (from the ieee1394 standard 1
488     // bandwidth allocation unit is 125/6144 us).  We further assume the
489     // MOTU is running at S400 (which it should be) so one allocation unit
490     // is equivalent to 1 transmitted byte; thus the bandwidth allocation
491     // required for the packets themselves is just the size of the packet.
492     // We used to allocate based on the maximum packet size (1160 bytes at
493     // 192 kHz for the traveler) but now do this based on the actual device
494     // state by utilising the result from getEventSize() and remembering
495     // that each packet has an 8 byte CIP header.  Note that bandwidth is
496     // allocated on a *per stream* basis - it must be allocated for both the
497     // transmit and receive streams.  While most MOTU modules are close to
498     // symmetric in terms of the number of in/out channels there are
499     // exceptions, so we deal with receive and transmit bandwidth separately.
500     signed int n_events_per_packet = samp_freq<=48000?8:(samp_freq<=96000?16:32);
501     m_rx_bandwidth = 25 + (n_events_per_packet*event_size_in);
502     m_tx_bandwidth = 25 + (n_events_per_packet*event_size_out);
503
504     // Assign iso channels if not already done
505     if (m_iso_recv_channel < 0)
506         m_iso_recv_channel = get1394Service().allocateIsoChannelGeneric(m_rx_bandwidth);
507
508     if (m_iso_send_channel < 0)
509         m_iso_send_channel = get1394Service().allocateIsoChannelGeneric(m_tx_bandwidth);
510
511     debugOutput(DEBUG_LEVEL_VERBOSE, "recv channel = %d, send channel = %d\n",
512         m_iso_recv_channel, m_iso_send_channel);
513
514     if (m_iso_recv_channel<0 || m_iso_send_channel<0) {
515         // be nice and deallocate
516         if (m_iso_recv_channel >= 0)
517             get1394Service().freeIsoChannel(m_iso_recv_channel);
518         if (m_iso_send_channel >= 0)
519             get1394Service().freeIsoChannel(m_iso_send_channel);
520
521         debugFatal("Could not allocate iso channels!\n");
522         return false;
523     }
524
525     m_receiveProcessor=new Streaming::MotuReceiveStreamProcessor(*this, event_size_in);
526
527     // The first thing is to initialize the processor.  This creates the
528     // data structures.
529     if(!m_receiveProcessor->init()) {
530         debugFatal("Could not initialize receive processor!\n");
531         return false;
532     }
533     m_receiveProcessor->setVerboseLevel(getDebugLevel());
534
535     // Now we add ports to the processor
536     debugOutput(DEBUG_LEVEL_VERBOSE,"Adding ports to receive processor\n");
537
538     char *buff;
539     Streaming::Port *p=NULL;
540
541     // retrieve the ID
542     std::string id=std::string("dev?");
543     if(!getOption("id", id)) {
544         debugWarning("Could not retrieve id parameter, defauling to 'dev?'\n");
545     }
546
547     // Add audio capture ports
548     if (!addDirPorts(Streaming::Port::E_Capture, samp_freq, optical_in_mode)) {
549         return false;
550     }
551
552     // Add MIDI port.  The MOTU only has one MIDI input port, with each
553     // MIDI byte sent using a 3 byte sequence starting at byte 4 of the
554     // event data.
555     asprintf(&buff,"%s_cap_MIDI0",id.c_str());
556     p = new Streaming::MotuMidiPort(buff,
557         Streaming::Port::E_Capture, 4);
558     if (!p) {
559         debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n", buff);
560     } else {
561         if (!m_receiveProcessor->addPort(p)) {
562             debugWarning("Could not register port with stream processor\n");
563             free(buff);
564             return false;
565         } else {
566             debugOutput(DEBUG_LEVEL_VERBOSE, "Added port %s\n", buff);
567         }
568     }
569     free(buff);
570
571     // example of adding an control port:
572 //    asprintf(&buff,"%s_cap_%s",id.c_str(),"myportnamehere");
573 //    p=new Streaming::MotuControlPort(
574 //            buff,
575 //            Streaming::Port::E_Capture,
576 //            0 // you can add all other port specific stuff you
577 //              // need to pass by extending MotuXXXPort and MotuPortInfo
578 //    );
579 //    free(buff);
580 //
581 //    if (!p) {
582 //        debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n",buff);
583 //    } else {
584 //
585 //        if (!m_receiveProcessor->addPort(p)) {
586 //            debugWarning("Could not register port with stream processor\n");
587 //            return false;
588 //        } else {
589 //            debugOutput(DEBUG_LEVEL_VERBOSE, "Added port %s\n",buff);
590 //        }
591 //    }
592
593     // Do the same for the transmit processor
594     m_transmitProcessor=new Streaming::MotuTransmitStreamProcessor(*this, event_size_out);
595
596     m_transmitProcessor->setVerboseLevel(getDebugLevel());
597
598     if(!m_transmitProcessor->init()) {
599         debugFatal("Could not initialize transmit processor!\n");
600         return false;
601     }
602
603     // Now we add ports to the processor
604     debugOutput(DEBUG_LEVEL_VERBOSE,"Adding ports to transmit processor\n");
605
606     // Add audio playback ports
607     if (!addDirPorts(Streaming::Port::E_Playback, samp_freq, optical_out_mode)) {
608         return false;
609     }
610
611     // Add MIDI port.  The MOTU only has one output MIDI port, with each
612     // MIDI byte transmitted using a 3 byte sequence starting at byte 4
613     // of the event data.
614     asprintf(&buff,"%s_pbk_MIDI0",id.c_str());
615     p = new Streaming::MotuMidiPort(buff,
616         Streaming::Port::E_Capture, 4);
617     if (!p) {
618         debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n", buff);
619     } else {
620         if (!m_receiveProcessor->addPort(p)) {
621             debugWarning("Could not register port with stream processor\n");
622             free(buff);
623             return false;
624         } else {
625             debugOutput(DEBUG_LEVEL_VERBOSE, "Added port %s\n", buff);
626         }
627     }
628     free(buff);
629
630     // example of adding an control port:
631 //    asprintf(&buff,"%s_pbk_%s",id.c_str(),"myportnamehere");
632 //
633 //    p=new Streaming::MotuControlPort(
634 //            buff,
635 //            Streaming::Port::E_Playback,
636 //            0 // you can add all other port specific stuff you
637 //              // need to pass by extending MotuXXXPort and MotuPortInfo
638 //    );
639 //    free(buff);
640 //
641 //    if (!p) {
642 //        debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n",buff);
643 //    } else {
644 //        if (!m_transmitProcessor->addPort(p)) {
645 //            debugWarning("Could not register port with stream processor\n");
646 //            return false;
647 //        } else {
648 //            debugOutput(DEBUG_LEVEL_VERBOSE, "Added port %s\n",buff);
649 //        }
650 //    }
651
652     return true;
653 }
654
655 int
656 MotuDevice::getStreamCount() {
657      return 2; // one receive, one transmit
658 }
659
660 Streaming::StreamProcessor *
661 MotuDevice::getStreamProcessorByIndex(int i) {
662     switch (i) {
663     case 0:
664         return m_receiveProcessor;
665     case 1:
666          return m_transmitProcessor;
667     default:
668         return NULL;
669     }
670     return 0;
671 }
672
673 bool
674 MotuDevice::startStreamByIndex(int i) {
675
676 quadlet_t isoctrl = ReadRegister(MOTUFW_REG_ISOCTRL);
677
678     // NOTE: this assumes that you have two streams
679     switch (i) {
680     case 0:
681         // TODO: do the stuff that is nescessary to make the device
682         // receive a stream
683
684         // Set the streamprocessor channel to the one obtained by
685         // the connection management
686         m_receiveProcessor->setChannel(m_iso_recv_channel);
687
688         // Mask out current transmit settings of the MOTU and replace
689         // with new ones.  Turn bit 24 on to enable changes to the
690         // MOTU's iso transmit settings when the iso control register
691         // is written.  Bit 23 enables iso transmit from the MOTU.
692         isoctrl &= 0xff00ffff;
693         isoctrl |= (m_iso_recv_channel << 16);
694         isoctrl |= 0x00c00000;
695         WriteRegister(MOTUFW_REG_ISOCTRL, isoctrl);
696         break;
697     case 1:
698         // TODO: do the stuff that is nescessary to make the device
699         // transmit a stream
700
701         // Set the streamprocessor channel to the one obtained by
702         // the connection management
703         m_transmitProcessor->setChannel(m_iso_send_channel);
704
705         // Mask out current receive settings of the MOTU and replace
706         // with new ones.  Turn bit 31 on to enable changes to the
707         // MOTU's iso receive settings when the iso control register
708         // is written.  Bit 30 enables iso receive by the MOTU.
709         isoctrl &= 0x00ffffff;
710         isoctrl |= (m_iso_send_channel << 24);
711         isoctrl |= 0xc0000000;
712         WriteRegister(MOTUFW_REG_ISOCTRL, isoctrl);
713         break;
714
715     default: // Invalid stream index
716         return false;
717     }
718
719     return true;
720 }
721
722 bool
723 MotuDevice::stopStreamByIndex(int i) {
724
725 quadlet_t isoctrl = ReadRegister(MOTUFW_REG_ISOCTRL);
726
727     // TODO: connection management: break connection
728     // cfr the start function
729
730     // NOTE: this assumes that you have two streams
731     switch (i) {
732     case 0:
733         // Turn bit 22 off to disable iso send by the MOTU.  Turn
734         // bit 23 on to enable changes to the MOTU's iso transmit
735         // settings when the iso control register is written.
736         isoctrl &= 0xffbfffff;
737         isoctrl |= 0x00800000;
738         WriteRegister(MOTUFW_REG_ISOCTRL, isoctrl);
739         break;
740     case 1:
741         // Turn bit 30 off to disable iso receive by the MOTU.  Turn
742         // bit 31 on to enable changes to the MOTU's iso receive
743         // settings when the iso control register is written.
744         isoctrl &= 0xbfffffff;
745         isoctrl |= 0x80000000;
746         WriteRegister(MOTUFW_REG_ISOCTRL, isoctrl);
747         break;
748
749     default: // Invalid stream index
750         return false;
751     }
752
753     return true;
754 }
755
756 signed int MotuDevice::getIsoRecvChannel(void) {
757     return m_iso_recv_channel;
758 }
759
760 signed int MotuDevice::getIsoSendChannel(void) {
761     return m_iso_send_channel;
762 }
763
764 unsigned int MotuDevice::getOpticalMode(unsigned int dir) {
765     unsigned int reg = ReadRegister(MOTUFW_REG_ROUTE_PORT_CONF);
766
767 debugOutput(DEBUG_LEVEL_VERBOSE, "optical mode: %x %x %x %x\n",dir, reg, reg & MOTUFW_OPTICAL_IN_MODE_MASK,
768 reg & MOTUFW_OPTICAL_OUT_MODE_MASK);
769
770     if (dir == MOTUFW_DIR_IN)
771         return (reg & MOTUFW_OPTICAL_IN_MODE_MASK) >> 8;
772     else
773         return (reg & MOTUFW_OPTICAL_OUT_MODE_MASK) >> 10;
774 }
775
776 signed int MotuDevice::setOpticalMode(unsigned int dir, unsigned int mode) {
777     unsigned int reg = ReadRegister(MOTUFW_REG_ROUTE_PORT_CONF);
778     unsigned int opt_ctrl = 0x0000002;
779
780     // Set up the optical control register value according to the current
781     // optical port modes.  At this stage it's not completely understood
782     // what the "Optical control" register does, so the values it's set to
783     // are more or less "magic" numbers.
784     if (reg & MOTUFW_OPTICAL_IN_MODE_MASK != (MOTUFW_OPTICAL_MODE_ADAT<<8))
785         opt_ctrl |= 0x00000080;
786     if (reg & MOTUFW_OPTICAL_OUT_MODE_MASK != (MOTUFW_OPTICAL_MODE_ADAT<<10))
787         opt_ctrl |= 0x00000040;
788
789     if (mode & MOTUFW_DIR_IN) {
790         reg &= ~MOTUFW_OPTICAL_IN_MODE_MASK;
791         reg |= (mode << 8) & MOTUFW_OPTICAL_IN_MODE_MASK;
792         if (mode != MOTUFW_OPTICAL_MODE_ADAT)
793             opt_ctrl |= 0x00000080;
794         else
795             opt_ctrl &= ~0x00000080;
796     }
797     if (mode & MOTUFW_DIR_OUT) {
798         reg &= ~MOTUFW_OPTICAL_OUT_MODE_MASK;
799         reg |= (mode <<10) & MOTUFW_OPTICAL_OUT_MODE_MASK;
800         if (mode != MOTUFW_OPTICAL_MODE_ADAT)
801             opt_ctrl |= 0x00000040;
802         else
803             opt_ctrl &= ~0x00000040;
804     }
805
806     // FIXME: there seems to be more to it than this, but for
807     // the moment at least this seems to work.
808     WriteRegister(MOTUFW_REG_ROUTE_PORT_CONF, reg);
809     return WriteRegister(MOTUFW_REG_OPTICAL_CTRL, opt_ctrl);
810 }
811
812 signed int MotuDevice::getEventSize(unsigned int direction) {
813 //
814 // Return the size in bytes of a single event sent to (dir==MOTUFW_OUT) or
815 // from (dir==MOTUFW_IN) the MOTU as part of an iso data packet.
816 //
817 // FIXME: for performance it may turn out best to calculate the event
818 // size in setOpticalMode and cache the result in a data field.  However,
819 // as it stands this will not adapt to dynamic changes in sample rate - we'd
820 // need a setFrameRate() for that.
821 //
822 // At the very least an event consists of the SPH (4 bytes) and the control/MIDI
823 // bytes (6 bytes).
824 // Note that all audio channels are sent using 3 bytes.
825 signed int sample_rate = getSamplingFrequency();
826 signed int optical_mode = getOpticalMode(direction);
827 signed int size = 4+6;
828
829 unsigned int i;
830 unsigned int dir = direction==Streaming::Port::E_Capture?MOTUFW_DIR_IN:MOTUFW_DIR_OUT;
831 unsigned int flags = (1 << ( optical_mode + 4 ));
832
833     if ( sample_rate > 96000 )
834         flags |= MOTUFW_PA_RATE_4x;
835     else if ( sample_rate > 48000 )
836         flags |= MOTUFW_PA_RATE_2x;
837     else
838         flags |= MOTUFW_PA_RATE_1x;
839
840     for (i=0; i < ( DevicesProperty[m_motu_model-1].PortsListLength /sizeof( PortEntry ) ); i++) {
841         if (( DevicesProperty[m_motu_model-1].PortsList[i].port_dir & dir ) &&
842            ( DevicesProperty[m_motu_model-1].PortsList[i].port_flags & MOTUFW_PA_RATE_MASK & flags ) &&
843            ( DevicesProperty[m_motu_model-1].PortsList[i].port_flags & MOTUFW_PA_OPTICAL_MASK & flags )) {
844             size += 3;
845         }
846     }
847
848     // Finally round size up to the next quadlet boundary
849     return ((size+3)/4)*4;
850 }
851 /* ======================================================================= */
852
853 bool MotuDevice::addPort(Streaming::StreamProcessor *s_processor,
854   char *name, enum Streaming::Port::E_Direction direction,
855   int position, int size) {
856 /*
857  * Internal helper function to add a MOTU port to a given stream processor.
858  * This just saves the unnecessary replication of what is essentially
859  * boilerplate code.  Note that the port name is freed by this function
860  * prior to exit.
861  */
862 Streaming::Port *p=NULL;
863
864     p = new Streaming::MotuAudioPort(name, direction, position, size);
865
866     if (!p) {
867         debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n",name);
868     } else {
869         if (!s_processor->addPort(p)) {
870             debugWarning("Could not register port with stream processor\n");
871             free(name);
872             return false;
873         } else {
874             debugOutput(DEBUG_LEVEL_VERBOSE, "Added port %s\n",name);
875         }
876         p->enable();
877     }
878     free(name);
879     return true;
880 }
881 /* ======================================================================= */
882
883 bool MotuDevice::addDirPorts(
884   enum Streaming::Port::E_Direction direction,
885   unsigned int sample_rate, unsigned int optical_mode) {
886 /*
887  * Internal helper method: adds all required ports for the given direction
888  * based on the indicated sample rate and optical mode.
889  *
890  * Notes: currently ports are not created if they are disabled due to sample
891  * rate or optical mode.  However, it might be better to unconditionally
892  * create all ports and just disable those which are not active.
893  */
894 const char *mode_str = direction==Streaming::Port::E_Capture?"cap":"pbk";
895 Streaming::StreamProcessor *s_processor;
896 unsigned int i;
897 char *buff;
898 unsigned int dir = direction==Streaming::Port::E_Capture?MOTUFW_DIR_IN:MOTUFW_DIR_OUT;
899 unsigned int flags = (1 << ( optical_mode + 4 ));
900
901     if ( sample_rate > 96000 )
902         flags |= MOTUFW_PA_RATE_4x;
903     else if ( sample_rate > 48000 )
904         flags |= MOTUFW_PA_RATE_2x;
905     else
906         flags |= MOTUFW_PA_RATE_1x;
907
908     // retrieve the ID
909     std::string id=std::string("dev?");
910     if(!getOption("id", id)) {
911         debugWarning("Could not retrieve id parameter, defauling to 'dev?'\n");
912     }
913
914     if (direction == Streaming::Port::E_Capture) {
915         s_processor = m_receiveProcessor;
916     } else {
917         s_processor = m_transmitProcessor;
918     }
919
920     for (i=0; i < ( DevicesProperty[m_motu_model-1].PortsListLength /sizeof( PortEntry ) ); i++) {
921         if (( DevicesProperty[m_motu_model-1].PortsList[i].port_dir & dir ) &&
922            ( DevicesProperty[m_motu_model-1].PortsList[i].port_flags & MOTUFW_PA_RATE_MASK & flags ) &&
923            ( DevicesProperty[m_motu_model-1].PortsList[i].port_flags & MOTUFW_PA_OPTICAL_MASK & flags )) {
924             asprintf(&buff,"%s_%s_%s" , id.c_str(), mode_str,
925               DevicesProperty[m_motu_model-1].PortsList[i].port_name);
926             if (!addPort(s_processor, buff, direction, DevicesProperty[m_motu_model-1].PortsList[i].port_offset, 0))
927                 return false;
928         }
929     }
930    
931     return true;
932 }
933 /* ======================================================================== */
934
935 unsigned int MotuDevice::ReadRegister(unsigned int reg) {
936 /*
937  * Attempts to read the requested register from the MOTU.
938  */
939
940   quadlet_t quadlet;
941
942   quadlet = 0;
943   // Note: 1394Service::read() expects a physical ID, not the node id
944   if (get1394Service().read(0xffc0 | getNodeId(), MOTUFW_BASE_ADDR+reg, 1, &quadlet) < 0) {
945     debugError("Error doing motu read from register 0x%06x\n",reg);
946   }
947
948   return ntohl(quadlet);
949 }
950
951 signed int MotuDevice::WriteRegister(unsigned int reg, quadlet_t data) {
952 /*
953  * Attempts to write the given data to the requested MOTU register.
954  */
955
956   unsigned int err = 0;
957   data = htonl(data);
958
959   // Note: 1394Service::write() expects a physical ID, not the node id
960   if (get1394Service().write(0xffc0 | getNodeId(), MOTUFW_BASE_ADDR+reg, 1, &data) < 0) {
961     err = 1;
962     debugError("Error doing motu write to register 0x%06x\n",reg);
963   }
964
965   SleepRelativeUsec(100);
966   return (err==0)?0:-1;
967 }
968
969 }
Note: See TracBrowser for help on using the browser.