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

Revision 584, 30.0 kB (checked in by ppalmers, 17 years ago)

- fix bug introduced in previous commit
- focusrite and terratec now have specific bebob devices

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