root/branches/libffado-2.0/src/bebob/focusrite/focusrite_generic.cpp

Revision 1219, 14.2 kB (checked in by ppalmers, 16 years ago)

fix various problems with DM1x00 devices being flooded with data

Line 
1 /*
2  * Copyright (C) 2005-2008 by Pieter Palmers
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 2 of the License, or
12  * (at your option) version 3 of the License.
13  *
14  * This program 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
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #include "focusrite_saffirepro.h"
25 #include "focusrite_cmd.h"
26
27 #include "libutil/ByteSwap.h"
28 #include "libutil/SystemTimeSource.h"
29
30 namespace BeBoB {
31 namespace Focusrite {
32
33 FocusriteDevice::FocusriteDevice( DeviceManager& d, std::auto_ptr<ConfigRom>( configRom ))
34     : BeBoB::AvDevice( d, configRom)
35 {
36     debugOutput( DEBUG_LEVEL_VERBOSE, "Created BeBoB::Focusrite::FocusriteDevice (NodeID %d)\n",
37                  getConfigRom().getNodeId() );
38     addOption(Util::OptionContainer::Option("useAvcForParameters", false));
39 }
40
41 void
42 FocusriteDevice::showDevice()
43 {
44     debugOutput(DEBUG_LEVEL_NORMAL, "This is a BeBoB::Focusrite::FocusriteDevice\n");
45     BeBoB::AvDevice::showDevice();
46 }
47
48 void
49 FocusriteDevice::setVerboseLevel(int l)
50 {
51     debugOutput( DEBUG_LEVEL_VERBOSE, "Setting verbose level to %d...\n", l );
52
53     BeBoB::AvDevice::setVerboseLevel(l);
54 }
55
56 bool
57 FocusriteDevice::setSpecificValue(uint32_t id, uint32_t v)
58 {
59     bool use_avc=false;
60     if(!getOption("useAvcForParameters", use_avc)) {
61         debugWarning("Could not retrieve useAvcForParameters parameter, defauling to false\n");
62     }
63     if (use_avc) {
64         return setSpecificValueAvc(id, v);
65     } else {
66         return setSpecificValueARM(id, v);
67     }
68 }
69
70 bool
71 FocusriteDevice::getSpecificValue(uint32_t id, uint32_t *v)
72 {
73     bool use_avc=false;
74     if(!getOption("useAvcForParameters", use_avc)) {
75         debugWarning("Could not retrieve useAvcForParameters parameter, defauling to false\n");
76     }
77     if (use_avc) {
78         return getSpecificValueAvc(id, v);
79     } else {
80         return getSpecificValueARM(id, v);
81     }
82 }
83
84 // The AV/C methods to set parameters
85 bool
86 FocusriteDevice::setSpecificValueAvc(uint32_t id, uint32_t v)
87 {
88    
89     FocusriteVendorDependentCmd cmd( get1394Service() );
90     cmd.setCommandType( AVC::AVCCommand::eCT_Control );
91     cmd.setNodeId( getConfigRom().getNodeId() );
92     cmd.setSubunitType( AVC::eST_Unit  );
93     cmd.setSubunitId( 0xff );
94    
95     cmd.setVerbose( getDebugLevel() );
96 //         cmd.setVerbose( DEBUG_LEVEL_VERY_VERBOSE );
97    
98     cmd.m_id=id;
99     cmd.m_value=v;
100    
101     if ( !cmd.fire() ) {
102         debugError( "FocusriteVendorDependentCmd info command failed\n" );
103         return false;
104     }
105     return true;
106 }
107
108 bool
109 FocusriteDevice::getSpecificValueAvc(uint32_t id, uint32_t *v)
110 {
111    
112     FocusriteVendorDependentCmd cmd( get1394Service() );
113     cmd.setCommandType( AVC::AVCCommand::eCT_Status );
114     cmd.setNodeId( getConfigRom().getNodeId() );
115     cmd.setSubunitType( AVC::eST_Unit  );
116     cmd.setSubunitId( 0xff );
117    
118     cmd.setVerbose( getDebugLevel() );
119 //         cmd.setVerbose( DEBUG_LEVEL_VERY_VERBOSE );
120    
121     cmd.m_id=id;
122    
123     if ( !cmd.fire() ) {
124         debugError( "FocusriteVendorDependentCmd info command failed\n" );
125         return false;
126     }
127
128     *v=cmd.m_value;
129     return true;
130 }
131
132 // The ARM methods to set parameters
133 bool
134 FocusriteDevice::setSpecificValueARM(uint32_t id, uint32_t v)
135 {
136     fb_quadlet_t data=v;
137     debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"Writing parameter address space id 0x%08lX (%u), data: 0x%08lX\n",
138         id, id, data);
139
140     fb_nodeaddr_t addr = FR_PARAM_SPACE_START + (id * 4);
141     fb_nodeid_t nodeId = getNodeId() | 0xFFC0;
142
143     if(!get1394Service().write_quadlet( nodeId, addr, CondSwapToBus32(data) ) ) {
144         debugError("Could not write to node 0x%04X addr 0x%012X\n", nodeId, addr);
145         Util::SystemTimeSource::SleepUsecRelative(AVC::AVCCommand::getSleepAfterAVCCommand());
146         return false;
147     }
148     Util::SystemTimeSource::SleepUsecRelative(AVC::AVCCommand::getSleepAfterAVCCommand());
149     return true;
150 }
151
152 bool
153 FocusriteDevice::getSpecificValueARM(uint32_t id, uint32_t *v)
154 {
155     fb_quadlet_t result;
156     debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"Reading parameter address space id 0x%08lX\n", id);
157
158     fb_nodeaddr_t addr = FR_PARAM_SPACE_START + (id * 4);
159     fb_nodeid_t nodeId = getNodeId() | 0xFFC0;
160
161     if(!get1394Service().read_quadlet( nodeId, addr, &result ) ) {
162         debugError("Could not read from node 0x%04llX addr 0x%012llX\n", nodeId, addr);
163         Util::SystemTimeSource::SleepUsecRelative(AVC::AVCCommand::getSleepAfterAVCCommand());
164         return false;
165     }
166
167     result=CondSwapFromBus32(result);
168     debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"Read result: 0x%08llX\n", result);
169
170     *v = result;
171     Util::SystemTimeSource::SleepUsecRelative(AVC::AVCCommand::getSleepAfterAVCCommand());
172     return true;
173 }
174
175 int
176 FocusriteDevice::convertDefToSr( uint32_t def ) {
177     switch(def) {
178         case FOCUSRITE_CMD_SAMPLERATE_44K1:  return 44100;
179         case FOCUSRITE_CMD_SAMPLERATE_48K:   return 48000;
180         case FOCUSRITE_CMD_SAMPLERATE_88K2:  return 88200;
181         case FOCUSRITE_CMD_SAMPLERATE_96K:   return 96000;
182         case FOCUSRITE_CMD_SAMPLERATE_176K4: return 176400;
183         case FOCUSRITE_CMD_SAMPLERATE_192K:  return 192000;
184         default:
185             debugWarning("Unsupported samplerate def: %08X\n", def);
186             return 0;
187     }
188 }
189
190 uint32_t
191 FocusriteDevice::convertSrToDef( int sr ) {
192     switch(sr) {
193         case 44100:  return FOCUSRITE_CMD_SAMPLERATE_44K1;
194         case 48000:  return FOCUSRITE_CMD_SAMPLERATE_48K;
195         case 88200:  return FOCUSRITE_CMD_SAMPLERATE_88K2;
196         case 96000:  return FOCUSRITE_CMD_SAMPLERATE_96K;
197         case 176400: return FOCUSRITE_CMD_SAMPLERATE_176K4;
198         case 192000: return FOCUSRITE_CMD_SAMPLERATE_192K;
199         default:
200             debugWarning("Unsupported samplerate: %d\n", sr);
201             return 0;
202     }
203 }
204
205 // --- element implementation classes
206
207 BinaryControl::BinaryControl(FocusriteDevice& parent, int id, int bit)
208 : Control::Discrete(&parent)
209 , m_Parent(parent)
210 , m_cmd_id ( id )
211 , m_cmd_bit ( bit )
212 {}
213 BinaryControl::BinaryControl(FocusriteDevice& parent, int id, int bit,
214                 std::string name, std::string label, std::string descr)
215 : Control::Discrete(&parent)
216 , m_Parent(parent)
217 , m_cmd_id ( id )
218 , m_cmd_bit ( bit )
219 {
220     setName(name);
221     setLabel(label);
222     setDescription(descr);
223 }
224
225 bool
226 BinaryControl::setValue(int v)
227 {
228     uint32_t reg;
229     uint32_t old_reg;
230
231     if ( !m_Parent.getSpecificValue(m_cmd_id, &reg) ) {
232         debugError( "getSpecificValue failed\n" );
233         return 0;
234     }
235    
236     old_reg=reg;
237     if (v) {
238         reg |= (1<<m_cmd_bit);
239     } else {
240         reg &= ~(1<<m_cmd_bit);
241     }
242     debugOutput(DEBUG_LEVEL_VERBOSE, "setValue for id %d to %d (reg: 0x%08X => 0x%08X)\n",
243                                      m_cmd_id, v, old_reg, reg);
244
245     if ( !m_Parent.setSpecificValue(m_cmd_id, reg) ) {
246         debugError( "setSpecificValue failed\n" );
247         return false;
248     } else return true;
249 }
250
251 int
252 BinaryControl::getValue()
253 {
254     uint32_t reg;
255
256     if ( !m_Parent.getSpecificValue(m_cmd_id, &reg) ) {
257         debugError( "getSpecificValue failed\n" );
258         return 0;
259     } else {
260         bool val= (reg & (1<<m_cmd_bit)) != 0;
261         debugOutput(DEBUG_LEVEL_VERBOSE, "getValue for %d: reg: 0x%08X, result=%d\n",
262                                          m_cmd_id, reg, val);
263         return val;
264     }
265 }
266
267 // --- element implementation classes
268
269 VolumeControl::VolumeControl(FocusriteDevice& parent, int id)
270 : Control::Discrete(&parent)
271 , m_Parent(parent)
272 , m_cmd_id ( id )
273 {}
274 VolumeControl::VolumeControl(FocusriteDevice& parent, int id,
275                 std::string name, std::string label, std::string descr)
276 : Control::Discrete(&parent)
277 , m_Parent(parent)
278 , m_cmd_id ( id )
279 {
280     setName(name);
281     setLabel(label);
282     setDescription(descr);
283 }
284
285 bool
286 VolumeControl::setValue(int v)
287 {
288     if (v>0x07FFF) v=0x07FFF;
289     else if (v<0) v=0;
290
291     debugOutput(DEBUG_LEVEL_VERBOSE, "setValue for id %d to %d\n",
292                                      m_cmd_id, v);
293
294     if ( !m_Parent.setSpecificValue(m_cmd_id, v) ) {
295         debugError( "setSpecificValue failed\n" );
296         return false;
297     } else return true;
298 }
299
300 int
301 VolumeControl::getValue()
302 {
303     uint32_t val=0;
304
305     if ( !m_Parent.getSpecificValue(m_cmd_id, &val) ) {
306         debugError( "getSpecificValue failed\n" );
307         return 0;
308     } else {
309         debugOutput(DEBUG_LEVEL_VERBOSE, "getValue for %d = %d\n",
310                                          m_cmd_id, val);
311         return val;
312     }
313 }
314
315 // reg control
316 RegisterControl::RegisterControl(FocusriteDevice& parent)
317 : Control::Register(&parent)
318 , m_Parent(parent)
319 {}
320 RegisterControl::RegisterControl(FocusriteDevice& parent,
321                  std::string name, std::string label, std::string descr)
322 : Control::Register(&parent)
323 , m_Parent(parent)
324 {
325     setName(name);
326     setLabel(label);
327     setDescription(descr);
328 }
329
330 bool
331 RegisterControl::setValue(uint64_t addr, uint64_t v)
332 {
333     debugOutput(DEBUG_LEVEL_VERBOSE, "setValue for addr %llu to %llu\n",
334                                      addr, v);
335
336     if ( !m_Parent.setSpecificValue(addr, v) ) {
337         debugError( "setSpecificValue failed\n" );
338         return false;
339     } else return true;
340 }
341
342 uint64_t
343 RegisterControl::getValue(uint64_t addr)
344 {
345     uint32_t val=0;
346
347     if ( !m_Parent.getSpecificValue(addr, &val) ) {
348         debugError( "getSpecificValue failed\n" );
349         return 0;
350     } else {
351         debugOutput(DEBUG_LEVEL_VERBOSE, "getValue for %llu = %lu\n",
352                                          addr, val);
353         return val;
354     }
355 }
356
357 // low resolution volume control
358 VolumeControlLowRes::VolumeControlLowRes(FocusriteDevice& parent, int id, int shift)
359 : Control::Discrete(&parent)
360 , m_Parent(parent)
361 , m_cmd_id ( id )
362 , m_bit_shift( shift )
363 {}
364 VolumeControlLowRes::VolumeControlLowRes(FocusriteDevice& parent, int id, int shift,
365                 std::string name, std::string label, std::string descr)
366 : Control::Discrete(&parent)
367 , m_Parent(parent)
368 , m_cmd_id ( id )
369 , m_bit_shift( shift )
370 {
371     setName(name);
372     setLabel(label);
373     setDescription(descr);
374 }
375
376 bool
377 VolumeControlLowRes::setValue(int v)
378 {
379     uint32_t reg;
380     uint32_t old_reg;
381    
382     if (v>0xFF) v=0xFF;
383     else if (v<0) v=0;
384
385     if ( !m_Parent.getSpecificValue(m_cmd_id, &reg) ) {
386         debugError( "getSpecificValue failed\n" );
387         return 0;
388     }
389    
390     old_reg=reg;
391     reg &= ~(0xFF<<m_bit_shift);
392     reg |= (v<<m_bit_shift);
393
394     debugOutput(DEBUG_LEVEL_VERBOSE, "setValue for id %d to %d, shift %d (reg: 0x%08X => 0x%08X)\n",
395                                      m_cmd_id, v, m_bit_shift, old_reg, reg);
396
397     if ( !m_Parent.setSpecificValue(m_cmd_id, reg) ) {
398         debugError( "setSpecificValue failed\n" );
399         return false;
400     } else return true;
401 }
402
403 int
404 VolumeControlLowRes::getValue()
405 {
406     uint32_t val, reg;
407
408     if ( !m_Parent.getSpecificValue(m_cmd_id, &reg) ) {
409         debugError( "getSpecificValue failed\n" );
410         return 0;
411     } else {
412         val = (reg & 0xFF)>>m_bit_shift;
413         debugOutput(DEBUG_LEVEL_VERBOSE, "getValue for %d: reg: 0x%08X, result=%d\n",
414                                          m_cmd_id, reg, val);
415         return val;
416     }
417 }
418
419
420 // Saffire pro matrix mixer element
421 FocusriteMatrixMixer::FocusriteMatrixMixer(FocusriteDevice& p)
422 : Control::MatrixMixer(&p, "MatrixMixer")
423 , m_Parent(p)
424 {
425 }
426
427 FocusriteMatrixMixer::FocusriteMatrixMixer(FocusriteDevice& p,std::string n)
428 : Control::MatrixMixer(&p, n)
429 , m_Parent(p)
430 {
431 }
432
433 void FocusriteMatrixMixer::addSignalInfo(std::vector<struct sSignalInfo> &target,
434     std::string name, std::string label, std::string descr)
435 {
436     struct sSignalInfo s;
437     s.name=name;
438     s.label=label;
439     s.description=descr;
440
441     target.push_back(s);
442 }
443
444 void FocusriteMatrixMixer::setCellInfo(int row, int col, int addr, bool valid)
445 {
446     struct sCellInfo c;
447     c.row=row;
448     c.col=col;
449     c.valid=valid;
450     c.address=addr;
451
452     m_CellInfo.at(row).at(col) = c;
453 }
454
455 void FocusriteMatrixMixer::show()
456 {
457     debugOutput(DEBUG_LEVEL_NORMAL, "Focusrite Matrix mixer\n");
458 }
459
460 std::string FocusriteMatrixMixer::getRowName( const int row )
461 {
462     debugOutput(DEBUG_LEVEL_VERBOSE, "name for row %d is %s\n",
463                                      row, m_RowInfo.at(row).name.c_str());
464     return m_RowInfo.at(row).name;
465 }
466
467 std::string FocusriteMatrixMixer::getColName( const int col )
468 {
469     debugOutput(DEBUG_LEVEL_VERBOSE, "name for col %d is %s\n",
470                                      col, m_ColInfo.at(col).name.c_str());
471     return m_ColInfo.at(col).name;
472 }
473
474 int FocusriteMatrixMixer::canWrite( const int row, const int col )
475 {
476     debugOutput(DEBUG_LEVEL_VERBOSE, "canWrite for row %d col %d is %d\n",
477                                      row, col, m_CellInfo.at(row).at(col).valid);
478     return m_CellInfo.at(row).at(col).valid;
479 }
480
481 double FocusriteMatrixMixer::setValue( const int row, const int col, const double val )
482 {
483     int32_t v = (int32_t)val;
484     struct sCellInfo c = m_CellInfo.at(row).at(col);
485
486     debugOutput(DEBUG_LEVEL_VERBOSE, "setValue for id %d row %d col %d to %lf (%ld)\n",
487                                      c.address, row, col, val, v);
488
489     if (v>0x07FFF) v=0x07FFF;
490     else if (v<0) v=0;
491
492     if ( !m_Parent.setSpecificValue(c.address, v) ) {
493         debugError( "setSpecificValue failed\n" );
494         return false;
495     } else return true;
496 }
497
498 double FocusriteMatrixMixer::getValue( const int row, const int col )
499 {
500     struct sCellInfo c=m_CellInfo.at(row).at(col);
501     uint32_t val=0;
502
503     if ( !m_Parent.getSpecificValue(c.address, &val) ) {
504         debugError( "getSpecificValue failed\n" );
505         return 0;
506     } else {
507         debugOutput(DEBUG_LEVEL_VERBOSE, "getValue for id %d row %d col %d = %lu\n",
508                                          c.address, row, col, val);
509         return val;
510     }
511 }
512
513 int FocusriteMatrixMixer::getRowCount( )
514 {
515     return m_RowInfo.size();
516 }
517
518 int FocusriteMatrixMixer::getColCount( )
519 {
520     return m_ColInfo.size();
521 }
522
523
524 } // Focusrite
525 } // BeBoB
Note: See TracBrowser for help on using the browser.