root/trunk/libffado/src/rme/rme_avdevice.cpp

Revision 1537, 10.8 kB (checked in by jwoithe, 15 years ago)

RME: more foundational work. Confirmed that reading device flash works.

Line 
1 /*
2  * Copyright (C) 2005-2009 by Jonathan Woithe
3  * Copyright (C) 2005-2008 by Pieter Palmers
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 #warning RME support is currently useless (detection only)
26
27 #include "rme/rme_avdevice.h"
28 #include "rme/fireface_def.h"
29
30 #include "libieee1394/configrom.h"
31 #include "libieee1394/ieee1394service.h"
32
33 #include "debugmodule/debugmodule.h"
34
35 #include <string>
36 #include <stdint.h>
37 #include <assert.h>
38 #include "libutil/ByteSwap.h"
39
40 #include <iostream>
41 #include <sstream>
42
43 #include <libraw1394/csr.h>
44
45 namespace Rme {
46
47 // The RME devices expect async packet data in little endian format (as
48 // opposed to bus order, which is big endian).  Therefore define our own
49 // 32-bit byteswap function to do this.
50 #if __BYTE_ORDER == __BIG_ENDIAN
51 static inline uint32_t
52 ByteSwapToDevice32(uint32_t d)
53 {
54     return byteswap_32(d);
55 }
56 ByteSwapFromDevice32(uint32_t d)
57 {
58     return byteswap_32(d);
59 }
60 #else
61 static inline uint32_t
62 ByteSwapToDevice32(uint32_t d)
63 {
64     return d;
65 }
66 static inline uint32_t
67 ByteSwapFromDevice32(uint32_t d)
68 {
69     return d;
70 }
71 #endif
72
73 // Template for a RmeDevice object method which intelligently returns a
74 // register or value applicable to the connected model and warns if something
75 // isn't quite right.
76 #define MODEL_SELECTOR(_name,_ff400_arg,_ff800_arg) \
77 unsigned long long int \
78 RmeDevice::_name() { \
79     switch (m_rme_model) { \
80         case RME_MODEL_FIREFACE400: return _ff400_arg; \
81         case RME_MODEL_FIREFACE800: return _ff800_arg; \
82     default: \
83       debugOutput( DEBUG_LEVEL_WARNING, "Bad RME model %d\n", m_rme_model ); \
84   } \
85   return 0xffffffffffffffffLL; \
86 }
87
88 // to define the supported devices
89 static VendorModelEntry supportedDeviceList[] =
90 {
91 //  {vendor_id, unit_version, model identifier, vendor name, model name,}
92     {FW_VENDORID_RME, 0x0001, RME_MODEL_FIREFACE800, "RME", "Fireface-800",},
93     {FW_VENDORID_RME, 0x0002, RME_MODEL_FIREFACE400, "RME", "Fireface-400",},
94 };
95
96 RmeDevice::RmeDevice( DeviceManager& d,
97                       std::auto_ptr<ConfigRom>( configRom ))
98     : FFADODevice( d, configRom )
99     , m_model( NULL )
100     , m_rme_model( RME_MODEL_NONE )
101     , m_ddsFreq( -1 )
102 {
103     debugOutput( DEBUG_LEVEL_VERBOSE, "Created Rme::RmeDevice (NodeID %d)\n",
104                  getConfigRom().getNodeId() );
105 }
106
107 RmeDevice::~RmeDevice()
108 {
109
110 }
111
112 MODEL_SELECTOR(cmd_buffer_addr, RME_FF400_CMD_BUFFER, RME_FF800_CMD_BUFFER)
113 MODEL_SELECTOR(stream_start_reg, RME_FF400_STREAM_START_REG, RME_FF800_STREAM_START_REG)
114 MODEL_SELECTOR(stream_end_reg, RME_FF400_STREAM_END_REG, RME_FF800_STREAM_END_REG)
115 MODEL_SELECTOR(flash_settings_addr, RME_FF400_FLASH_SETTINGS_ADDR, RME_FF800_FLASH_SETTINGS_ADDR)
116 MODEL_SELECTOR(flash_mixer_vol_addr, RME_FF400_FLASH_MIXER_VOLUME_ADDR, RME_FF800_FLASH_MIXER_VOLUME_ADDR)
117 MODEL_SELECTOR(flash_mixer_pan_addr, RME_FF400_FLASH_MIXER_PAN_ADDR, RME_FF800_FLASH_MIXER_PAN_ADDR)
118 MODEL_SELECTOR(flash_mixer_hw_addr, RME_FF400_FLASH_MIXER_HW_ADDR, RME_FF800_FLASH_MIXER_HW_ADDR)
119
120 bool
121 RmeDevice::probe( ConfigRom& configRom, bool generic )
122 {
123     if (generic) return false;
124     unsigned int vendorId = configRom.getNodeVendorId();
125     unsigned int unitVersion = configRom.getUnitVersion();
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].unit_version == unitVersion )
133            )
134         {
135             return true;
136         }
137     }
138
139     return false;
140 }
141
142 FFADODevice *
143 RmeDevice::createDevice(DeviceManager& d, std::auto_ptr<ConfigRom>( configRom ))
144 {
145     return new RmeDevice(d, configRom );
146 }
147
148 bool
149 RmeDevice::discover()
150 {
151     unsigned int vendorId = getConfigRom().getNodeVendorId();
152     unsigned int unitVersion = getConfigRom().getUnitVersion();
153
154     for ( unsigned int i = 0;
155           i < ( sizeof( supportedDeviceList )/sizeof( VendorModelEntry ) );
156           ++i )
157     {
158         if ( ( supportedDeviceList[i].vendor_id == vendorId )
159              && ( supportedDeviceList[i].unit_version == unitVersion )
160            )
161         {
162             m_model = &(supportedDeviceList[i]);
163             m_rme_model = supportedDeviceList[i].model;
164         }
165     }
166
167     if (m_model == NULL)
168         return false;
169
170     debugOutput( DEBUG_LEVEL_VERBOSE, "found %s %s\n",
171         m_model->vendor_name, m_model->model_name);
172
173     init_hardware();
174 read_device_settings();
175
176     return true;
177 }
178
179 int
180 RmeDevice::getSamplingFrequency( ) {
181 /*
182  * Retrieve the current sample rate from the RME device.  At this stage it
183  * seems that the "current rate" can't be retrieved from the device.  Other
184  * drivers don't read the DDS control register and there isn't anywhere else
185  * where the frequency is sent back to the PC.  Unless we test the DDS
186  * control register for readabilty and find it can be read we'll assume it
187  * can't and instead cache the DDS frequency.
188  *
189  * If the device frequency has not been set this function will return -1
190  * (the default value of m_ddsFreq).
191  */
192     return m_ddsFreq;
193 }
194
195 int
196 RmeDevice::getConfigurationId()
197 {
198     return 0;
199 }
200
201 bool
202 RmeDevice::setSamplingFrequency( int samplingFrequency )
203 {
204 /*
205  * Set the RME device's samplerate.  The RME can do sampling frequencies of
206  * 32k, 44.1k and 48k along with the corresponding 2x and 4x rates.
207  * However, it can also do +/- 4% from any of these "base" frequencies.
208  * This makes it a little long-winded to work out whether a given frequency
209  * is supported or not.
210  */
211
212     /* Work out whether the requested rate is supported */
213     if (!((samplingFrequency >= 32000*0.96 && samplingFrequency <= 32000*1.04) ||
214         (samplingFrequency >= 44100*0.96 && samplingFrequency <= 44100*1.04) ||
215         (samplingFrequency >= 48000*0.96 && samplingFrequency <= 48000*1.04) ||
216         (samplingFrequency >= 64000*0.96 && samplingFrequency <= 64000*1.04) ||
217         (samplingFrequency >= 88200*0.96 && samplingFrequency <= 88200*1.04) ||
218         (samplingFrequency >= 96000*0.96 && samplingFrequency <= 96000*1.04) ||
219         (samplingFrequency >= 128000*0.96 && samplingFrequency <= 128000*1.04) ||
220         (samplingFrequency >= 176000*0.96 && samplingFrequency <= 176000*1.04) ||
221         (samplingFrequency >= 192000*0.96 && samplingFrequency <= 192000*1.04))) {
222         return false;
223     }
224    
225     /* Send the desired frequency to the RME */
226     if (writeRegister(RME_REG_DDS_CONTROL, samplingFrequency) != 0)
227       return false;
228
229     m_ddsFreq = samplingFrequency;
230     return true;
231 }
232
233 #define RME_CHECK_AND_ADD_SR(v, x) \
234     { \
235     if (((x >= 32000*0.96 && x <= 32000*1.04) || \
236         (x >= 44100*0.96 && x <= 44100*1.04) || \
237         (x >= 48000*0.96 && x <= 48000*1.04) || \
238         (x >= 64000*0.96 && x <= 64000*1.04) || \
239         (x >= 88200*0.96 && x <= 88200*1.04) || \
240         (x >= 96000*0.96 && x <= 96000*1.04) || \
241         (x >= 128000*0.96 && x <= 128000*1.04) || \
242         (x >= 176000*0.96 && x <= 176000*1.04) || \
243         (x >= 192000*0.96 && x <= 192000*1.04))) { \
244         v.push_back(x); \
245     };};
246
247 std::vector<int>
248 RmeDevice::getSupportedSamplingFrequencies()
249 {
250     std::vector<int> frequencies;
251     RME_CHECK_AND_ADD_SR(frequencies, 32000);
252     RME_CHECK_AND_ADD_SR(frequencies, 44100);
253     RME_CHECK_AND_ADD_SR(frequencies, 48000);
254     RME_CHECK_AND_ADD_SR(frequencies, 64000);
255     RME_CHECK_AND_ADD_SR(frequencies, 88200);
256     RME_CHECK_AND_ADD_SR(frequencies, 96000);
257     RME_CHECK_AND_ADD_SR(frequencies, 128000);
258     RME_CHECK_AND_ADD_SR(frequencies, 176400);
259     RME_CHECK_AND_ADD_SR(frequencies, 192000);
260     return frequencies;
261 }
262
263 FFADODevice::ClockSourceVector
264 RmeDevice::getSupportedClockSources() {
265     FFADODevice::ClockSourceVector r;
266     return r;
267 }
268
269 bool
270 RmeDevice::setActiveClockSource(ClockSource s) {
271     return false;
272 }
273
274 FFADODevice::ClockSource
275 RmeDevice::getActiveClockSource() {
276     ClockSource s;
277     return s;
278 }
279
280 bool
281 RmeDevice::lock() {
282
283     return true;
284 }
285
286
287 bool
288 RmeDevice::unlock() {
289
290     return true;
291 }
292
293 void
294 RmeDevice::showDevice()
295 {
296         debugOutput(DEBUG_LEVEL_VERBOSE,
297                 "%s %s at node %d\n", m_model->vendor_name, m_model->model_name,
298                 getNodeId());
299 }
300
301 bool
302 RmeDevice::prepare() {
303
304         debugOutput(DEBUG_LEVEL_NORMAL, "Preparing RmeDevice...\n" );
305
306         return true;
307 }
308
309 int
310 RmeDevice::getStreamCount() {
311         return 0; // one receive, one transmit
312 }
313
314 Streaming::StreamProcessor *
315 RmeDevice::getStreamProcessorByIndex(int i) {
316     return NULL;
317 }
318
319 bool
320 RmeDevice::startStreamByIndex(int i) {
321     return false;
322 }
323
324 bool
325 RmeDevice::stopStreamByIndex(int i) {
326     return false;
327
328 }
329
330 unsigned int
331 RmeDevice::readRegister(fb_nodeaddr_t reg) {
332
333     quadlet_t quadlet;
334    
335     quadlet = 0;
336     if (get1394Service().read(0xffc0 | getNodeId(), reg, 1, &quadlet) <= 0) {
337         debugError("Error doing RME read from register 0x%06x\n",reg);
338     }
339     return ByteSwapFromDevice32(quadlet);
340 }
341
342 signed int
343 RmeDevice::readBlock(fb_nodeaddr_t reg, quadlet_t *buf, unsigned int n_quads) {
344
345     unsigned int i;
346
347     if (get1394Service().read(0xffc0 | getNodeId(), reg, n_quads, buf) <= 0) {
348         debugError("Error doing RME block read of %d quadlets from register 0x%06x\n",
349             n_quads, reg);
350         return -1;
351     }
352     for (i=0; i<n_quads; i++) {
353        buf[i] = ByteSwapFromDevice32(buf[i]);
354     }
355
356     return 0;
357 }
358
359 signed int
360 RmeDevice::writeRegister(fb_nodeaddr_t reg, quadlet_t data) {
361
362     unsigned int err = 0;
363     data = ByteSwapToDevice32(data);
364     if (get1394Service().write(0xffc0 | getNodeId(), reg, 1, &data) <= 0) {
365         err = 1;
366         debugError("Error doing RME write to register 0x%06x\n",reg);
367     }
368     return (err==0)?0:-1;
369 }
370
371 signed int
372 RmeDevice::writeBlock(fb_nodeaddr_t reg, quadlet_t *data, unsigned int n_quads) {
373 //
374 // Write a block of data to the device starting at address "reg".  Note that
375 // the conditional byteswap is done "in place" on data, so the contents of
376 // data may be modified by calling this function.
377 //
378     unsigned int err = 0;
379     unsigned int i;
380
381     for (i=0; i<n_quads; i++) {
382       data[i] = ByteSwapToDevice32(data[i]);
383     }
384     if (get1394Service().write(0xffc0 | getNodeId(), reg, n_quads, data) <= 0) {
385         err = 1;
386         debugError("Error doing RME block write of %d quadlets to register 0x%06x\n",
387           n_quads, reg);
388     }
389     return (err==0)?0:-1;
390 }
391                  
392 }
Note: See TracBrowser for help on using the browser.