root/branches/libfreebob-motu/src/motu/motu_avdevice.cpp

Revision 197, 5.3 kB (checked in by pieterpalmers, 18 years ago)

- implemented first steps of MOTU device discovery

Line 
1 /* motu_avdevice.cpp
2  * Copyright (C) 2006 by Pieter Palmers
3  *
4  * This file is part of FreeBob.
5  *
6  * FreeBob is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * FreeBob is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with FreeBob; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18  * MA 02111-1307 USA.
19  */
20
21 #include "motu/motu_avdevice.h"
22 #include "configrom.h"
23
24 #include "libfreebobavc/ieee1394service.h"
25 #include "libfreebobavc/avc_definitions.h"
26
27 #include "debugmodule/debugmodule.h"
28
29 #include <string>
30 #include <stdint.h>
31 #include <assert.h>
32 #include <netinet/in.h>
33
34 namespace Motu {
35
36 IMPL_DEBUG_MODULE( MotuDevice, MotuDevice, DEBUG_LEVEL_NORMAL );
37
38 MotuDevice::MotuDevice( Ieee1394Service& ieee1394service,
39                         int nodeId,
40                         int verboseLevel )
41     : m_1394Service( &ieee1394service )
42     , m_nodeId( nodeId )
43     , m_verboseLevel( verboseLevel )
44 {
45     if ( m_verboseLevel ) {
46         setDebugLevel( DEBUG_LEVEL_VERBOSE );
47     }
48     debugOutput( DEBUG_LEVEL_VERBOSE, "Created Motu::MotuDevice (NodeID %d)\n",
49                  nodeId );
50     m_configRom = new ConfigRom( m_1394Service, m_nodeId );
51     m_configRom->initialize();
52
53 }
54
55 MotuDevice::~MotuDevice()
56 {
57         delete m_configRom;
58 }
59
60 ConfigRom&
61 MotuDevice::getConfigRom() const
62 {
63     return *m_configRom;
64 }
65
66 bool
67 MotuDevice::discover()
68 {
69         signed int i, is_motu_fw_audio;
70
71         /* A list of all IEEE1394 Vendor IDs used by MOTU.  One would expect this to
72         * include only one, but stranger things have happened in this world.  The
73         * list is terminated with a 0xffffffff value.
74         */
75         const unsigned int motu_vendor_ids[] = {
76                 VENDOR_MOTU,
77                 VENDOR_MOTU_TEST,
78                 0xffffffff,
79         };
80        
81         /* A list of all valid IEEE1394 model IDs for MOTU firewire audio devices,
82         * terminated by 0xffffffff.
83         */
84         const unsigned int motu_fw_audio_model_ids[] = {
85                 MOTU_828mkII, MOTU_TRAVELER, MOTU_TEST,
86                 0xffffffff,
87         };
88        
89         /* Find out if this device is one we know about */
90         is_motu_fw_audio = i = 0;
91
92         while ((motu_vendor_ids[i]!=0xffffffff)
93                         && (m_configRom->getVendorId() != motu_vendor_ids[i]))
94                 i++;
95
96         if (motu_vendor_ids[i] != 0xffffffff) {
97                 /* Device is made by MOTU.  See if the model is one we know about */
98                 i = 0;
99
100                 while ((motu_fw_audio_model_ids[i]!=0xffffffff)
101                                 && (m_configRom->getModelId() != motu_fw_audio_model_ids[i]))
102                         i++;
103
104                 if (motu_fw_audio_model_ids[i]!=0xffffffff)
105                         is_motu_fw_audio = 1;
106         }
107
108         if (is_motu_fw_audio) {
109                 debugOutput( DEBUG_LEVEL_VERBOSE, "found %s %s\n",
110                         m_configRom->getVendorName(), m_configRom->getModelName());
111                 return true;
112         }
113
114     return false;
115 }
116
117
118
119 bool
120 MotuDevice::setSamplingFrequency( ESamplingFrequency samplingFrequency )
121 {
122     quadlet_t new_rate=0;
123         int supported=true;
124
125         switch ( samplingFrequency ) {
126     case eSF_22050Hz:
127                 supported=false;
128         break;
129     case eSF_24000Hz:
130                 supported=false;
131         break;
132     case eSF_32000Hz:
133                 supported=false;
134         break;
135     case eSF_44100Hz:
136         new_rate = MOTU_BASE_RATE_44100 | MOTU_RATE_MULTIPLIER_1X;
137         break;
138     case eSF_48000Hz:
139         new_rate = MOTU_BASE_RATE_48000 | MOTU_RATE_MULTIPLIER_1X;
140         break;
141     case eSF_88200Hz:
142         new_rate = MOTU_BASE_RATE_44100 | MOTU_RATE_MULTIPLIER_2X;
143         break;
144     case eSF_96000Hz:
145         new_rate = MOTU_BASE_RATE_48000 | MOTU_RATE_MULTIPLIER_2X;
146         break;
147     case eSF_176400Hz:
148         new_rate = MOTU_BASE_RATE_44100 | MOTU_RATE_MULTIPLIER_4X;
149         break;
150     case eSF_192000Hz:
151         new_rate = MOTU_BASE_RATE_48000 | MOTU_RATE_MULTIPLIER_4X;
152         break;
153     default:
154         supported=false;
155     }
156
157         // update the register
158         if(supported) {
159                 quadlet_t value=ReadRegister(0x0B14);
160                 value &= ~(MOTU_RATE_MASK);
161                 value |= new_rate;
162
163                 if(WriteRegister(0x0B14,value) == 0) {
164                         supported=true;
165                 } else {
166                         supported=false;
167                 }
168         }
169
170     return supported;
171 }
172
173 void
174 MotuDevice::showDevice() const
175 {
176     printf( "%s %s at node %d\n",
177                 m_configRom->getVendorName(),
178                 m_configRom->getModelName(),
179                 m_nodeId );
180
181 }
182
183 bool
184 MotuDevice::addXmlDescription( xmlNodePtr deviceNode )
185 {
186     return true;
187 }
188
189 /* ======================================================================== */
190
191 unsigned int MotuDevice::ReadRegister(unsigned int reg) {
192 /*
193  * Attempts to read the requested register from the MOTU.
194  */
195
196         quadlet_t quadlet;
197         assert(m_1394Service);
198        
199         quadlet = 0;
200         if (m_1394Service->read(m_nodeId, MOTU_BASE_ADDR+reg, 4, &quadlet) < 0) {
201                 debugError("Error doing motu read from register 0x%06x\n",reg);
202         }
203
204
205         return ntohl(quadlet);
206 }
207
208 signed int MotuDevice::WriteRegister(unsigned int reg, quadlet_t data) {
209 /*
210  * Attempts to write the given data to the requested MOTU register.
211  */
212
213   unsigned int err = 0;
214
215   data = htonl(data);
216
217   if (m_1394Service->write(m_nodeId, MOTU_BASE_ADDR+reg, 4, &data) < 0) {
218     err = 1;
219     debugError("Error doing motu write to register 0x%06x\n",reg);
220   }
221
222   usleep(100);
223
224   return (err==0)?0:-1;
225 }
226
227 }
Note: See TracBrowser for help on using the browser.