root/trunk/libffado/src/bebob/focusrite/focusrite_generic.cpp

Revision 739, 12.7 kB (checked in by ppalmers, 13 years ago)

- Adapt the ffado external API (upgrade to v3)

NEEDS NEW JACK BACKEND

- simplify FFADODevice constructor even more
- implement first framework support for supporting multiple adapters.

currently all firewire adapters are scanned for supported devices unless specified otherwise
however attaching devices to separate adapters is not supported. using multiple adapters at
that are connected together might work.

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