root/trunk/libffado/src/dice/dice_eap.cpp

Revision 2244, 66.6 kB (checked in by jwoithe, 10 years ago)

Dice EAP: Set DEBUG_LEVEL_VERBOSE for message from EAP::Mixer::updateNameCache(). It is pointless to alert the user about this. Patch from Philippe Carriere.

Line 
1 /*
2  * Copyright (C) 2005-2009 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 "dice_avdevice.h"
25
26 #include "dice_eap.h"
27 #include "dice_defines.h"
28
29 #include "libutil/SystemTimeSource.h"
30 #include "libutil/ByteSwap.h"
31
32 #include <cstdio>
33
34 namespace Dice {
35
36 // ----------- helper functions -------------
37
38 #if 0
39 static const char *
40 srcBlockToString(const char id)
41 {
42     switch(id) {
43         case eRS_AES:   return "AES ";
44         case eRS_ADAT:  return "ADAT";
45         case eRS_Mixer: return "MXR ";
46         case eRS_InS0:  return "INS0";
47         case eRS_InS1:  return "INS1";
48         case eRS_ARM:   return "ARM ";
49         case eRS_ARX0:  return "AVS0";
50         case eRS_ARX1:  return "AVS1";
51         case eRS_Muted: return "MUTE";
52         default :       return "RSVD";
53     }
54 }
55
56 static  const char *
57 dstBlockToString(const char id)
58 {
59     switch(id) {
60         case eRD_AES:    return "AES ";
61         case eRD_ADAT:   return "ADAT";
62         case eRD_Mixer0: return "MXR0";
63         case eRD_Mixer1: return "MXR1";
64         case eRD_InS0:   return "INS0";
65         case eRD_InS1:   return "INS1";
66         case eRD_ARM:    return "ARM ";
67         case eRD_ATX0:   return "AVS0";
68         case eRD_ATX1:   return "AVS1";
69         case eRD_Muted:  return "MUTE";
70         default : return "RSVD";
71     }
72 }
73 #endif
74
75 IMPL_DEBUG_MODULE( EAP, EAP, DEBUG_LEVEL_NORMAL );
76
77 EAP::EAP(Device &d)
78 : Control::Container(&d, "EAP")
79 , m_device(d)
80 , m_mixer( NULL )
81 , m_router( NULL )
82 , m_current_cfg_routing_low ( RouterConfig(*this, eRT_CurrentCfg, DICE_EAP_CURRCFG_LOW_ROUTER ) )
83 , m_current_cfg_routing_mid ( RouterConfig(*this, eRT_CurrentCfg, DICE_EAP_CURRCFG_MID_ROUTER ) )
84 , m_current_cfg_routing_high( RouterConfig(*this, eRT_CurrentCfg, DICE_EAP_CURRCFG_HIGH_ROUTER) )
85 , m_current_cfg_stream_low  ( StreamConfig(*this, eRT_CurrentCfg, DICE_EAP_CURRCFG_LOW_STREAM ) )
86 , m_current_cfg_stream_mid  ( StreamConfig(*this, eRT_CurrentCfg, DICE_EAP_CURRCFG_MID_STREAM ) )
87 , m_current_cfg_stream_high ( StreamConfig(*this, eRT_CurrentCfg, DICE_EAP_CURRCFG_HIGH_STREAM) )
88 {
89 }
90
91 EAP::~EAP()
92 {
93     // remove all control elements registered to this device (w/o free)
94     clearElements(false);
95
96     // delete the helper classes
97     if(m_mixer) delete m_mixer;
98     if(m_router) delete m_router;
99 }
100
101 // offsets and sizes are returned in quadlets, but we use byte values, hence the *= 4
102 #define DICE_EAP_READREG_AND_CHECK(base, addr, var) { \
103     if(!readReg(base, addr, &var)) { \
104         debugError("Could not initialize " #var "\n"); \
105         return false; \
106     } \
107     var *= 4; \
108 }
109
110 bool
111 EAP::init() {
112     if(!supportsEAP(m_device)) {
113         debugWarning("no EAP mixer (device does not support EAP)\n");
114         return false;
115     }
116
117     // offsets and sizes are returned in quadlets, but we use byte values
118     DICE_EAP_READREG_AND_CHECK(eRT_Base, DICE_EAP_CAPABILITY_SPACE_OFF, m_capability_offset);
119     DICE_EAP_READREG_AND_CHECK(eRT_Base, DICE_EAP_CAPABILITY_SPACE_SZ, m_capability_size);
120     DICE_EAP_READREG_AND_CHECK(eRT_Base, DICE_EAP_CMD_SPACE_OFF, m_cmd_offset);
121     DICE_EAP_READREG_AND_CHECK(eRT_Base, DICE_EAP_CMD_SPACE_SZ, m_cmd_size);
122     DICE_EAP_READREG_AND_CHECK(eRT_Base, DICE_EAP_MIXER_SPACE_OFF, m_mixer_offset);
123     DICE_EAP_READREG_AND_CHECK(eRT_Base, DICE_EAP_MIXER_SPACE_SZ, m_mixer_size);
124     DICE_EAP_READREG_AND_CHECK(eRT_Base, DICE_EAP_PEAK_SPACE_OFF, m_peak_offset);
125     DICE_EAP_READREG_AND_CHECK(eRT_Base, DICE_EAP_PEAK_SPACE_SZ, m_peak_size);
126     DICE_EAP_READREG_AND_CHECK(eRT_Base, DICE_EAP_NEW_ROUTING_SPACE_OFF, m_new_routing_offset);
127     DICE_EAP_READREG_AND_CHECK(eRT_Base, DICE_EAP_NEW_ROUTING_SPACE_SZ, m_new_routing_size);
128     DICE_EAP_READREG_AND_CHECK(eRT_Base, DICE_EAP_NEW_STREAM_CFG_SPACE_OFF, m_new_stream_cfg_offset);
129     DICE_EAP_READREG_AND_CHECK(eRT_Base, DICE_EAP_NEW_STREAM_CFG_SPACE_SZ, m_new_stream_cfg_size);
130     DICE_EAP_READREG_AND_CHECK(eRT_Base, DICE_EAP_CURR_CFG_SPACE_OFF, m_curr_cfg_offset);
131     DICE_EAP_READREG_AND_CHECK(eRT_Base, DICE_EAP_CURR_CFG_SPACE_SZ, m_curr_cfg_size);
132     DICE_EAP_READREG_AND_CHECK(eRT_Base, DICE_EAP_STAND_ALONE_CFG_SPACE_OFF, m_standalone_offset);
133     DICE_EAP_READREG_AND_CHECK(eRT_Base, DICE_EAP_STAND_ALONE_CFG_SPACE_SZ, m_standalone_size);
134     DICE_EAP_READREG_AND_CHECK(eRT_Base, DICE_EAP_APP_SPACE_OFF, m_app_offset);
135     DICE_EAP_READREG_AND_CHECK(eRT_Base, DICE_EAP_APP_SPACE_SZ, m_app_size);
136
137     // initialize the capability info
138     quadlet_t tmp;
139     if(!readReg(eRT_Capability, DICE_EAP_CAPABILITY_ROUTER, &tmp)) {
140         debugError("Could not read router capabilities\n");
141         return false;
142     }
143     m_router_exposed = (tmp >> DICE_EAP_CAP_ROUTER_EXPOSED) & 0x01;
144     m_router_readonly = (tmp >> DICE_EAP_CAP_ROUTER_READONLY) & 0x01;
145     m_router_flashstored = (tmp >> DICE_EAP_CAP_ROUTER_FLASHSTORED) & 0x01;
146     m_router_nb_entries = (tmp >> DICE_EAP_CAP_ROUTER_MAXROUTES) & 0xFFFF;
147
148     if(!readReg(eRT_Capability, DICE_EAP_CAPABILITY_MIXER, &tmp)) {
149         debugError("Could not read mixer capabilities\n");
150         return false;
151     }
152     m_mixer_exposed = (tmp >> DICE_EAP_CAP_MIXER_EXPOSED) & 0x01;
153     m_mixer_readonly = (tmp >> DICE_EAP_CAP_MIXER_READONLY) & 0x01;
154     m_mixer_flashstored = (tmp >> DICE_EAP_CAP_MIXER_FLASHSTORED) & 0x01;
155     m_mixer_tx_id = (tmp >> DICE_EAP_CAP_MIXER_IN_DEV) & 0x000F;
156     m_mixer_rx_id = (tmp >> DICE_EAP_CAP_MIXER_OUT_DEV) & 0x000F;
157     m_mixer_nb_tx = (tmp >> DICE_EAP_CAP_MIXER_INPUTS) & 0x00FF;
158     m_mixer_nb_rx = (tmp >> DICE_EAP_CAP_MIXER_OUTPUTS) & 0x00FF;
159
160     if(!readReg(eRT_Capability, DICE_EAP_CAPABILITY_GENERAL, &tmp)) {
161         debugError("Could not read general capabilities\n");
162         return false;
163     }
164     m_general_support_dynstream = (tmp >> DICE_EAP_CAP_GENERAL_STRM_CFG_EN) & 0x01;
165     m_general_support_flash = (tmp >> DICE_EAP_CAP_GENERAL_FLASH_EN) & 0x01;
166     m_general_peak_enabled = (tmp >> DICE_EAP_CAP_GENERAL_PEAK_EN) & 0x01;
167     m_general_max_tx = (tmp >> DICE_EAP_CAP_GENERAL_MAX_TX_STREAM) & 0x0F;
168     m_general_max_rx = (tmp >> DICE_EAP_CAP_GENERAL_MAX_RX_STREAM) & 0x0F;
169     m_general_stream_cfg_stored = (tmp >> DICE_EAP_CAP_GENERAL_STRM_CFG_FLS) & 0x01;
170     m_general_chip = (tmp >> DICE_EAP_CAP_GENERAL_CHIP) & 0xFFFF;
171
172     // update our view on the current configuration
173     if(!updateConfigurationCache()) {
174         debugError("Could not initialize configuration cache\n");
175         return false;
176     }
177
178     // initialize the helper classes
179     if (m_mixer_exposed) {
180         // initialize the mixer
181         m_mixer = new EAP::Mixer(*this);
182         if(m_mixer == NULL) {
183             debugError("Could not allocate memory for mixer\n");
184             return false;
185         }
186         if(!m_mixer->init()) {
187             debugError("Could not initialize mixer\n");
188             delete m_mixer;
189             m_mixer = NULL;
190             return false;
191         }
192         // add the mixer to the EAP control container
193         if(!addElement(m_mixer)) {
194             debugWarning("Failed to add mixer to control tree\n");
195         }
196        
197         // initialize the peak meter
198         m_router = new EAP::Router(*this);
199         if(m_router == NULL) {
200             debugError("Could not allocate memory for router\n");
201             return false;
202         }
203         m_router->update();
204
205         // add the router to the EAP control container
206         if(!addElement(m_router)) {
207             debugWarning("Failed to add router to control tree\n");
208         }
209     }
210
211     return true;
212 }
213
214
215 void
216 EAP::update()
217 {
218     // update EAP from the last init
219     //   update router sources and destinations
220     if (m_router) {
221         m_router->update();
222     }
223 }
224
225 void
226 EAP::setupSources() {
227     // define router sources (possibly depending on the samplerate)
228     switch(m_device.getCurrentConfig()) {
229         case Device::eDC_Low: setupSources_low(); return;
230         case Device::eDC_Mid: setupSources_mid(); return;
231         case Device::eDC_High: setupSources_high(); return;
232         default:
233             debugError("Unsupported configuration mode\n");
234             return;
235     }
236 }
237
238 void
239 EAP::setupSources_low() {
240     // add the routing sources for a DICE chip
241     switch(m_general_chip) {
242         case DICE_EAP_CAP_GENERAL_CHIP_DICEII:
243             // router/EAP currently not supported
244             break;
245         case DICE_EAP_CAP_GENERAL_CHIP_DICEJR:
246             // second audio port (unique to the junior)
247             addSource("InS1", 0, 8, eRS_InS1, 1);
248         case DICE_EAP_CAP_GENERAL_CHIP_DICEMINI:
249             /// these are common to the mini and junior
250             // the AES receiver
251             addSource("AES", 0, 8, eRS_AES, 1);
252             // the ADAT receiver
253             addSource("ADAT", 0, 8, eRS_ADAT, 1);
254             // the Mixer outputs
255             addSource("MixerOut", 0, 16, eRS_Mixer, 1);
256             // the first audio port
257             addSource("InS0", 0, 8, eRS_InS0, 1);
258             // the ARM audio port
259             addSource("ARM", 0, 8, eRS_ARM, 1);
260             // the 1394 stream receivers
261             addSource("1394_0", 0, 16, eRS_ARX0, 1);
262             addSource("1394_1", 0, 16, eRS_ARX1, 1);
263             // mute
264             addSource("Mute", 0, 1, eRS_Muted);
265             break;
266         default:
267             // this is an unsupported chip
268             break;
269     }
270 }
271
272 void
273 EAP::setupSources_mid() {
274     setupSources_low();
275 }
276
277 void
278 EAP::setupSources_high() {
279     setupSources_low();
280 }
281
282 unsigned int
283 EAP::getSMuteId() {
284     return m_router->getSourceIndex("Mute:00");
285 }
286
287 void
288 EAP::setupDestinations() {
289     switch(m_device.getCurrentConfig()) {
290         case Device::eDC_Low: setupDestinations_low(); return;
291         case Device::eDC_Mid: setupDestinations_mid(); return;
292         case Device::eDC_High: setupDestinations_high(); return;
293         default:
294             debugError("Unsupported configuration mode\n");
295             return;
296     }
297 }
298
299 void
300 EAP::setupDestinations_low() {
301     // add the routing destinations for a DICE chip
302     switch(m_general_chip) {
303         case DICE_EAP_CAP_GENERAL_CHIP_DICEII:
304             // router/EAP currently not supported
305             break;
306         case DICE_EAP_CAP_GENERAL_CHIP_DICEJR:
307             // second audio port (unique to the junior)
308             addDestination("InS1", 0, 8, eRD_InS1, 1);
309         case DICE_EAP_CAP_GENERAL_CHIP_DICEMINI:
310             /// these are common to the mini and junior
311             // the AES receiver
312             addDestination("AES", 0, 8, eRD_AES, 1);
313             // the ADAT receiver
314             addDestination("ADAT", 0, 8, eRD_ADAT, 1);
315             // the Mixer outputs
316             addDestination("MixerIn", 0, 16, eRD_Mixer0, 1);
317             addDestination("MixerIn", 0, 2, eRD_Mixer1, 17);
318             // the first audio port
319             addDestination("InS0", 0, 8, eRD_InS0, 1);
320             // the ARM audio port
321             addDestination("ARM", 0, 8, eRD_ARM, 1);
322             // the 1394 stream receivers
323             addDestination("1394_0", 0, 16, eRD_ATX0, 1);
324             addDestination("1394_1", 0, 16, eRD_ATX1, 1);
325             // mute
326             addDestination("Mute", 0, 1, eRD_Muted, 1);
327             break;
328         default:
329             // this is an unsupported chip
330             break;
331     }
332 }
333 void
334 EAP::setupDestinations_mid() {
335     setupDestinations_low();
336 }
337 void
338 EAP::setupDestinations_high() {
339     setupDestinations_low();
340 }
341
342 void
343 EAP::addSource(const std::string name, unsigned int base, unsigned int count,
344                enum eRouteSource srcid, unsigned int offset)
345 {
346     m_router->addSource(name, srcid, base, count, offset);
347 }
348 void
349 EAP::addDestination(const std::string name, unsigned int base, unsigned int count,
350                     enum eRouteDestination destid, unsigned int offset)
351 {
352     m_router->addDestination(name, destid, base, count, offset);
353 }
354
355 bool
356 EAP::updateConfigurationCache()
357 {
358     if(!m_current_cfg_routing_low.read()) {
359         debugError("Could not initialize current routing configuration (low rates)\n");
360         return false;
361     }
362     if(!m_current_cfg_routing_mid.read()) {
363         debugError("Could not initialize current routing configuration (mid rates)\n");
364         return false;
365     }
366     if(!m_current_cfg_routing_high.read()) {
367         debugError("Could not initialize current routing configuration (high rates)\n");
368         return false;
369     }
370     if(!m_current_cfg_stream_low.read()) {
371         debugError("Could not initialize current stream configuration (low rates)\n");
372         return false;
373     }
374     if(!m_current_cfg_stream_mid.read()) {
375         debugError("Could not initialize current stream configuration (mid rates)\n");
376         return false;
377     }
378     if(!m_current_cfg_stream_high.read()) {
379         debugError("Could not initialize current stream configuration (high rates)\n");
380         return false;
381     }
382     if(m_mixer) m_mixer->updateNameCache();
383     return true;
384 }
385
386 // Get capture and playback names
387 //   If the device has a router, capture and playback are destinations and sources, respectively,
388 //     as defined above
389 //   If no router is found, transmitters and receivers names are returned
390
391 stringlist
392 EAP::getCptrNameString(unsigned int i) {
393     std::vector<unsigned int> destid;
394     unsigned int destid_0;
395     stringlist cptr_names;
396     std::string dest_name, src_name;
397    
398     if (m_router) {
399         switch (i) {
400           case 0: destid_0 = (eRD_ATX0<<4); break;
401           case 1: destid_0 = (eRD_ATX1<<4); break;
402           // Only 2 transmitter possible (?)
403           default: return cptr_names;
404         }
405         // At most 16 destinations per eRD
406         for (int j=0; j<16; j++) {
407           destid.push_back(destid_0+j);
408         }
409         for (unsigned it=0; it<destid.size(); it++) {
410           // If destination identifier is not part of the router, destination name will be empty
411           dest_name = m_router->getDestinationName(destid.at(it));
412           if (dest_name.size() > 0) {
413             // get a source possibly routed to the destination (only one source per destination)
414             src_name = m_router->getSourceForDestination(dest_name);
415             if (src_name.size() > 0) {
416               dest_name.append(" ("); dest_name.append(src_name); dest_name.append(")");
417             }
418             cptr_names.push_back(dest_name);
419           }
420         }
421
422     } else {
423         StreamConfig *scfg = getActiveStreamConfig();
424         if(scfg) {
425           cptr_names = scfg->getTxNamesString(i);
426         }
427     }
428     return cptr_names;
429 }
430
431 stringlist
432 EAP::getPbckNameString(unsigned int i) {
433     std::vector<unsigned int> srcid;
434     unsigned int srcid_0;
435     stringlist pbck_names, dest_names;
436     std::string src_name;
437    
438     if (m_router) {
439         switch (i) {
440            case 0: srcid_0 = (eRS_ARX0<<4); break;
441            case 1: srcid_0 = (eRS_ARX1<<4); break;
442             // Only 2 receiver possible (?)
443           default: return pbck_names;
444         }
445         // At most 16 destinations per eRD
446         for (int j=0; j<16; j++) {
447           srcid.push_back(srcid_0+j);
448         }
449         for (unsigned it=0; it<srcid.size(); it++) {
450           // If source identifier is not part of the router, source name will be empty
451           src_name = m_router->getSourceName(srcid.at(it));
452           if (src_name.size() > 0) {
453             // Search for destinations routed to this source
454             //   Multiple destinations for a single source are possible
455             dest_names = m_router->getDestinationsForSource(src_name);
456             if (dest_names.size() > 0) {
457               src_name.append(" (");
458               stringlist::iterator it_d = dest_names.begin();
459               stringlist::iterator it_d_end_m1 = dest_names.end(); --it_d_end_m1;
460               while (it_d != it_d_end_m1) {
461                 src_name.append((*it_d).c_str()); src_name.append("; ");
462                 it_d++;
463               }
464               src_name.append((*it_d).c_str()); src_name.append(")");
465             }
466             pbck_names.push_back(src_name);
467           }
468         }
469     } else {
470         StreamConfig *scfg = getActiveStreamConfig();
471         if(scfg) {
472           pbck_names = scfg->getRxNamesString(i);
473         }
474     }
475     return pbck_names;
476 }
477
478 /**
479  * Returns the router configuration for the current rate mode
480  */
481 EAP::RouterConfig *
482 EAP::getActiveRouterConfig()
483 {
484     switch(m_device.getCurrentConfig()) {
485         case Device::eDC_Low: return &m_current_cfg_routing_low;
486         case Device::eDC_Mid: return &m_current_cfg_routing_mid;
487         case Device::eDC_High: return &m_current_cfg_routing_high;
488         default:
489             debugError("Unsupported configuration mode\n");
490             return NULL;
491     }
492 }
493
494 /**
495  * Returns the stream configuration for the current rate mode
496  */
497 EAP::StreamConfig *
498 EAP::getActiveStreamConfig()
499 {
500     switch(m_device.getCurrentConfig()) {
501         case Device::eDC_Low: return &m_current_cfg_stream_low;
502         case Device::eDC_Mid: return &m_current_cfg_stream_mid;
503         case Device::eDC_High: return &m_current_cfg_stream_high;
504         default:
505             debugError("Unsupported configuration mode\n");
506             return NULL;
507     }
508 }
509
510 /**
511  * Uploads a new router configuration to the device
512  * @param  rcfg The new RouterConfig
513  * @param low store as config for the low rates
514  * @param mid store as config for the mid rates
515  * @param high store as config for the high rates
516  * @return true if successful, false otherwise
517  */
518 bool
519 EAP::updateRouterConfig(RouterConfig& rcfg, bool low, bool mid, bool high) {
520     // write the router config to the appropriate memory space on the device
521     if(!rcfg.write(eRT_NewRouting, 0)) {
522         debugError("Could not write new router configuration\n");
523         return false;
524     }
525     // perform the store operation
526     if(!loadRouterConfig(low, mid, high)) {
527         debugError("Could not activate new router configuration\n");
528         updateConfigurationCache(); // for consistency
529         return false;
530     }
531     return updateConfigurationCache();
532 }
533
534 /**
535  * Uploads a new router configuration to replace the configuration
536  * for the current rate.
537  * @param  rcfg The new RouterConfig
538  * @return true if successful, false otherwise
539  */
540 bool
541 EAP::updateCurrentRouterConfig(RouterConfig& rcfg) {
542     switch(m_device.getCurrentConfig()) {
543         case Device::eDC_Low: return updateRouterConfig(rcfg, true, false, false);
544         case Device::eDC_Mid: return updateRouterConfig(rcfg, false, true, false);
545         case Device::eDC_High: return updateRouterConfig(rcfg, false, false, true);
546         default:
547             debugError("Unsupported configuration mode\n");
548             return false;
549     }
550 }
551
552 /**
553  * Set a default configuration for the router.
554  *  This is necessary for somes devices for hardware coherence, in which case the next
555  *  has to be customized
556  * @param  rcfg The new RouterConfig
557  * @return true if successful, false otherwise
558  */
559 void
560 EAP::setupDefaultRouterConfig_low() {
561     unsigned int i;
562     // add the routing destinations for a DICE chip
563     switch(m_general_chip) {
564         case DICE_EAP_CAP_GENERAL_CHIP_DICEII:
565             // router/EAP currently not supported
566             break;
567         case DICE_EAP_CAP_GENERAL_CHIP_DICEJR:
568             // second audio port (unique to the junior)
569             // Ensure it is not muted
570             for (i=0; i<8; i++) {
571               addRoute(eRS_ARX0, i+8, eRD_InS1, i);
572             }
573         case DICE_EAP_CAP_GENERAL_CHIP_DICEMINI:
574             // the 1394 stream receivers
575             for (i=0; i<8; i++) {
576               addRoute(eRS_InS0, i, eRD_ATX0, i);
577             }
578             for (i=0; i<8; i++) {
579               addRoute(eRS_InS1, i, eRD_ATX0, i+8);
580             }
581             for (i=0; i<8; i++) {
582               addRoute(eRS_ADAT, i, eRD_ATX1, i);
583             }
584             for (i=0; i<8; i++) {
585               addRoute(eRS_AES, i, eRD_ATX1, i+8);
586             }
587             // The audio ports
588             // Ensure that audio port are not muted
589             for (i=0; i<8; i++) {
590               addRoute(eRS_ARX0, i, eRD_InS0, i);
591             }
592             // the AES receiver
593             for (i=0; i<8; i++) {
594               addRoute(eRS_Muted, 0, eRD_AES, i);
595             }
596             // the ADAT receiver
597             for (i=0; i<8; i++) {
598               addRoute(eRS_Muted, 0, eRD_ADAT, i);
599             }
600             // the Mixer outputs
601             for (i=0; i<8; i++) {
602               addRoute(eRS_InS0, i, eRD_Mixer0, i);
603             }
604             for (i=0; i<8; i++) {
605               addRoute(eRS_ADAT, i, eRD_Mixer0, i+8);
606             }
607             for (i=0; i<2; i++) {
608               addRoute(eRS_Muted, 0, eRD_Mixer0, i+16);
609             }
610             // the ARM audio port
611             for (i=0; i<0; i++) {
612               addRoute(eRS_Muted, 0, eRD_ARM, i);
613             }
614             // mute
615             addRoute(eRS_Muted, 0, eRD_Muted, 0);
616             break;
617         default:
618             // this is an unsupported chip
619             break;
620     }
621 }
622
623 void
624 EAP::setupDefaultRouterConfig_mid() {
625     setupDefaultRouterConfig_low();
626 }
627
628 void
629 EAP::setupDefaultRouterConfig_high() {
630     setupDefaultRouterConfig_low();
631 }
632
633 void
634 EAP::setupDefaultRouterConfig() {
635     // First clear routes
636     RouterConfig *rcfg = getActiveRouterConfig();
637     rcfg->clearRoutes();
638
639     // Then define the configuration depending on the samplerate
640     switch(m_device.getCurrentConfig()) {
641         case Device::eDC_Low: setupDefaultRouterConfig_low(); break;
642         case Device::eDC_Mid: setupDefaultRouterConfig_mid(); break;
643         case Device::eDC_High: setupDefaultRouterConfig_high(); break;
644         default:
645             debugError("Unsupported configuration mode\n");
646             return;
647     }
648
649     updateCurrentRouterConfig(*rcfg);
650 }
651
652 /**
653  * Add (create) a route from source to destination
654  */
655 bool
656 EAP::addRoute(enum eRouteSource srcid, unsigned int base_src, enum eRouteDestination dstid,
657               unsigned int base_dst)
658 {
659     RouterConfig *rcfg = getActiveRouterConfig();
660     return rcfg->createRoute((srcid<<4) + base_src, (dstid<<4) + base_dst);
661 }
662
663
664 /**
665  * Uploads a new stream configuration to the device
666  * @param scfg The new StreamConfig
667  * @param low store as config for the low rates
668  * @param mid store as config for the mid rates
669  * @param high store as config for the high rates
670  * @return true if successful, false otherwise
671  */
672 bool
673 EAP::updateStreamConfig(StreamConfig& scfg, bool low, bool mid, bool high) {
674     // write the stream config to the appropriate memory space on the device
675     if(!scfg.write(eRT_NewStreamCfg, 0)) {
676         debugError("Could not write new stream configuration\n");
677         return false;
678     }
679     // perform the store operation
680     if(!loadStreamConfig(low, mid, high)) {
681         debugError("Could not activate new stream configuration\n");
682         updateConfigurationCache(); // for consistency
683         return false;
684     }
685     return updateConfigurationCache();
686 }
687
688 /**
689  * Uploads a new router and stream configuration to the device
690  * @param  rcfg The new RouterConfig
691  * @param  scfg The new StreamConfig
692  * @param low store as config for the low rates
693  * @param mid store as config for the mid rates
694  * @param high store as config for the high rates
695  * @return true if successful, false otherwise
696  */
697 bool
698 EAP::updateStreamConfig(RouterConfig& rcfg, StreamConfig& scfg, bool low, bool mid, bool high) {
699     // write the router config to the appropriate memory space on the device
700     if(!rcfg.write(eRT_NewRouting, 0)) {
701         debugError("Could not write new router configuration\n");
702         return false;
703     }
704     // write the stream config to the appropriate memory space on the device
705     if(!scfg.write(eRT_NewStreamCfg, 0)) {
706         debugError("Could not write new stream configuration\n");
707         return false;
708     }
709     // perform the store operation
710     if(!loadRouterAndStreamConfig(low, mid, high)) {
711         debugError("Could not activate new router/stream configuration\n");
712         updateConfigurationCache(); // for consistency
713         return false;
714     }
715     return updateConfigurationCache();
716 }
717
718
719 bool
720 EAP::loadFlashConfig() {
721     bool retval = true;
722     debugWarning("Untested code\n");
723     fb_quadlet_t cmd = DICE_EAP_CMD_OPCODE_LD_FLASH_CFG;
724     cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_EXECUTE;
725     if(!commandHelper(cmd)) {
726         debugWarning("Command failed\n");
727         retval = false;
728     }
729     retval &= updateConfigurationCache();
730     return retval;
731 }
732
733 bool
734 EAP::storeFlashConfig() {
735     //debugWarning("Untested code\n") // Works. -Arnold;
736     fb_quadlet_t cmd = DICE_EAP_CMD_OPCODE_ST_FLASH_CFG;
737     cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_EXECUTE;
738     return commandHelper(cmd);
739 }
740
741 // helpers
742 void
743 EAP::show()
744 {
745     printMessage("== DICE EAP ==\n");
746     printMessage("Parameter Space info:\n");
747     printMessage(" Capability        : offset=%04X size=%06d\n", m_capability_offset, m_capability_size);
748     printMessage(" Command           : offset=%04X size=%06d\n", m_cmd_offset, m_cmd_size);
749     printMessage(" Mixer             : offset=%04X size=%06d\n", m_mixer_offset, m_mixer_size);
750     printMessage(" Peak              : offset=%04X size=%06d\n", m_peak_offset, m_peak_size);
751     printMessage(" New Routing Cfg   : offset=%04X size=%06d\n", m_new_routing_offset, m_new_routing_size);
752     printMessage(" New Stream Cfg    : offset=%04X size=%06d\n", m_new_stream_cfg_offset, m_new_stream_cfg_size);
753     printMessage(" Current Cfg       : offset=%04X size=%06d\n", m_curr_cfg_offset, m_curr_cfg_size);
754     printMessage(" Standalone Cfg    : offset=%04X size=%06d\n", m_standalone_offset, m_standalone_size);
755     printMessage(" Application Space : offset=%04X size=%06d\n", m_app_offset, m_app_size);
756
757     printMessage("Capabilities:\n");
758     printMessage(" Router: %sexposed, %swritable, %sstored, %d routes\n",
759                                      (m_router_exposed?"":"not "),
760                                      (m_router_readonly?"not ":""),
761                                      (m_router_flashstored?"":"not "),
762                                      m_router_nb_entries);
763     printMessage(" Mixer : %sexposed, %swritable, %sstored\n",
764                                      (m_mixer_exposed?"":"not "),
765                                      (m_mixer_readonly?"not ":""),
766                                      (m_mixer_flashstored?"":"not "));
767     printMessage("         tx id: (%d==eRD_Mixer0) ? %s, rx id: (%d==eRS_Mixer) ? %s\n",
768                                      m_mixer_tx_id, (m_mixer_tx_id == eRD_Mixer0)?"true":"false",
769                                      m_mixer_rx_id, (m_mixer_rx_id == eRS_Mixer) ?"true":"false");
770     printMessage("         nb tx channels: %d, nb rx channels: %d\n", m_mixer_nb_tx, m_mixer_nb_rx);
771     printMessage(" General: dynamic stream config %ssupported\n",
772                                      (m_general_support_dynstream?"":"not "));
773     printMessage("          flash load and store %ssupported\n",
774                                      (m_general_support_flash?"":"not "));
775     printMessage("          peak metering %s\n",
776                                      (m_general_peak_enabled?"enabled":"disabled"));
777     printMessage("          stream config %sstored\n",
778                                      (m_general_stream_cfg_stored?"":"not "));
779     printMessage("          max TX streams: %d, max RX streams: %d\n",
780                                      m_general_max_tx, m_general_max_rx);
781
782     if(m_general_chip == DICE_EAP_CAP_GENERAL_CHIP_DICEII) {
783         printMessage("          Chip: DICE-II\n");
784     } else if(m_general_chip == DICE_EAP_CAP_GENERAL_CHIP_DICEMINI) {
785         printMessage("          Chip: DICE Mini (TCD2210)\n");
786     } else if(m_general_chip == DICE_EAP_CAP_GENERAL_CHIP_DICEJR) {
787         printMessage("          Chip: DICE Junior (TCD2220)\n");
788     }
789
790     printMessage("--- Mixer configuration ---\n");
791     if(m_mixer) {
792         m_mixer->show();
793     }
794     printMessage("--- Router/Peak space ---\n");
795     if(m_router) {
796         m_router->show();
797     }
798
799     printMessage("--- Active Router ---\n");
800     RouterConfig *rcfg = getActiveRouterConfig();
801     if(rcfg) {
802         rcfg->show();
803     }
804     printMessage("--- Active Stream ---\n");
805     StreamConfig *scfg = getActiveStreamConfig();
806     if(scfg) {
807         scfg->show();
808     }
809
810 // fixme
811 //     size_t len = 0x1000;
812 //     quadlet_t tmp[len];
813 //     if(!readRegBlock( eRT_CurrentCfg, DICE_EAP_CURRCFG_LOW_STREAM, tmp, len*4) ) {
814 //         debugError("Failed to read block\n");
815 //     } else {
816 //         hexDumpQuadlets(tmp, len);
817 //     }
818
819 }
820 void
821 EAP::showApplication()
822 {
823     printMessage("--- Application space ---\n");
824     fb_quadlet_t* tmp = (fb_quadlet_t *)calloc(128, sizeof(fb_quadlet_t));
825     unsigned int appsize = m_app_size; /// m_app_size is rather big. Start with the first four block of 128 quadlets...
826     unsigned int offset = 0;
827     while ( appsize > 0 ) {
828         if ( ! readRegBlock( eRT_Application, offset, tmp, ((appsize<128)?appsize:128)*sizeof(fb_quadlet_t) ) )
829             appsize = 0;
830         else {
831             hexDumpQuadlets(tmp, 128);
832             offset += 128*sizeof(fb_quadlet_t);
833             appsize -= 128*sizeof(fb_quadlet_t);
834         }
835     }
836 }
837
838 void
839 EAP::showFullRouter()
840 {
841     printMessage("--- Full router content ---\n");
842
843     printMessage(" %d entries to read\n", m_router_nb_entries);
844
845     unsigned int offset;
846     switch(m_device.getCurrentConfig()) {
847         case Device::eDC_Low: offset = DICE_EAP_CURRCFG_LOW_ROUTER; break;
848         case Device::eDC_Mid: offset = DICE_EAP_CURRCFG_MID_ROUTER; break;
849         case Device::eDC_High: offset = DICE_EAP_CURRCFG_HIGH_ROUTER; break;
850         default: offset = 0; break;
851     }
852
853     // Current config
854     printMessage("  Current Configuration:\n");
855     // First bloc is the effective number of routes
856     uint32_t nb_routes;
857     if(!readRegBlock(eRT_CurrentCfg, offset, &nb_routes, 4)) {
858         printMessage("Failed to read number of entries\n");
859         return;
860     }
861     printMessage("   %d routes\n", nb_routes);
862
863     // read the route info
864     uint32_t tmp_entries[m_router_nb_entries];
865     if(!readRegBlock(eRT_CurrentCfg, offset+4, tmp_entries, m_router_nb_entries*4)) {
866         printMessage("Failed to read router config block information\n");
867         return;
868     }
869
870     // decode and print
871     for(unsigned int i=0; i < m_router_nb_entries; i++) {
872         printMessage("    %d: 0x%02x <- 0x%02x;\n", i, tmp_entries[i]&0xff, (tmp_entries[i]>>8)&0xff);
873     }
874
875     // New config
876     printMessage("  New Configuration:\n");
877     // First bloc is the effective number of routes
878     if(!readRegBlock(eRT_NewRouting, 0, &nb_routes, 4)) {
879         printMessage("Failed to read number of entries\n");
880         return;
881     }
882     printMessage("   %d routes\n", nb_routes);
883
884     // read the route info
885     if(!readRegBlock(eRT_NewRouting, 4, tmp_entries, m_router_nb_entries*4)) {
886         printMessage("Failed to read router config block information\n");
887         return;
888     }
889
890     // decode and print
891     for(unsigned int i=0; i < m_router_nb_entries; i++) {
892         printMessage("    %d: 0x%02x <- 0x%02x;\n", i, tmp_entries[i]&0xff, (tmp_entries[i]>>8)&0xff);
893     }
894
895     return;
896 }
897
898 void
899 EAP::showFullPeakSpace()
900 {
901     printMessage("--- Full Peak space content ---\n");
902
903     // read the peak/route info
904     uint32_t tmp_entries[m_router_nb_entries];
905     if(!readRegBlock(eRT_Peak, 0, tmp_entries, m_router_nb_entries*4)) {
906         debugError("Failed to read peak block information\n");
907         return;
908     }
909     // decode and print
910     for (unsigned int i=0; i<m_router_nb_entries; ++i) {
911         printMessage("  %d: 0x%02x: %d;\n", i, tmp_entries[i]&0xff, (tmp_entries[i]&0xfff0000)>>16);
912     }
913     return;
914 }
915
916 // EAP load/store operations
917
918 enum EAP::eWaitReturn
919 EAP::operationBusy() {
920     fb_quadlet_t tmp;
921     if(!readReg(eRT_Command, DICE_EAP_COMMAND_OPCODE, &tmp)) {
922         debugError("Could not read opcode register\n");
923         return eWR_Error;
924     }
925     if( (tmp & DICE_EAP_CMD_OPCODE_FLAG_LD_EXECUTE) == DICE_EAP_CMD_OPCODE_FLAG_LD_EXECUTE) {
926         return eWR_Busy;
927     } else {
928         return eWR_Done;
929     }
930 }
931
932 enum EAP::eWaitReturn
933 EAP::waitForOperationEnd(int max_wait_time_ms) {
934     int max_waits = max_wait_time_ms;
935
936     while(max_waits--) {
937         enum eWaitReturn retval = operationBusy();
938         switch(retval) {
939             case eWR_Busy:
940                 break; // not done yet, keep waiting
941             case eWR_Done:
942                 return eWR_Done;
943             case eWR_Error:
944             case eWR_Timeout:
945                 debugError("Error while waiting for operation to end. (%d)\n", retval);
946         }
947         Util::SystemTimeSource::SleepUsecRelative(1000);
948     }
949     return eWR_Timeout;
950 }
951
952 bool
953 EAP::commandHelper(fb_quadlet_t cmd) {
954     // check whether another command is still running
955     if(operationBusy() == eWR_Busy) {
956         debugError("Other operation in progress\n");
957         return false;
958     }
959
960     // execute the command
961     if(!writeReg(eRT_Command, DICE_EAP_COMMAND_OPCODE, cmd)) {
962         debugError("Could not write opcode register\n");
963         return false;
964     }
965
966     // wait for the operation to end
967     enum eWaitReturn retval = waitForOperationEnd();
968     switch(retval) {
969         case eWR_Done:
970             break; // do nothing
971         case eWR_Timeout:
972             debugWarning("Time-out while waiting for operation to end. (%d)\n", retval);
973             return false;
974         case eWR_Error:
975         case eWR_Busy: // can't be returned
976             debugError("Error while waiting for operation to end. (%d)\n", retval);
977             return false;
978     }
979
980     // check the return value
981     if(!readReg(eRT_Command, DICE_EAP_COMMAND_RETVAL, &cmd)) {
982         debugError("Could not read return value register\n");
983         return false;
984     }
985     if(cmd != 0) {
986         debugWarning("Command failed\n");
987         return false;
988     } else {
989         debugOutput(DEBUG_LEVEL_VERBOSE, "Command successful\n");
990         return true;
991     }
992 }
993
994 bool
995 EAP::loadRouterConfig(bool low, bool mid, bool high) {
996     fb_quadlet_t cmd = DICE_EAP_CMD_OPCODE_LD_ROUTER;
997     if(low) cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_LOW;
998     if(mid) cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_MID;
999     if(high) cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_HIGH;
1000     cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_EXECUTE;
1001     return commandHelper(cmd);
1002 }
1003
1004 bool
1005 EAP::loadStreamConfig(bool low, bool mid, bool high) {
1006     debugWarning("Untested code\n");
1007     fb_quadlet_t cmd = DICE_EAP_CMD_OPCODE_LD_STRM_CFG;
1008     if(low) cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_LOW;
1009     if(mid) cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_MID;
1010     if(high) cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_HIGH;
1011     cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_EXECUTE;
1012     return commandHelper(cmd);
1013 }
1014
1015 bool
1016 EAP::loadRouterAndStreamConfig(bool low, bool mid, bool high) {
1017     debugWarning("Untested code\n");
1018     fb_quadlet_t cmd = DICE_EAP_CMD_OPCODE_LD_RTR_STRM_CFG;
1019     if(low) cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_LOW;
1020     if(mid) cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_MID;
1021     if(high) cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_HIGH;
1022     cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_EXECUTE;
1023     return commandHelper(cmd);
1024 }
1025
1026 /*
1027   I/O operations
1028   */
1029 bool
1030 EAP::readReg(enum eRegBase base, unsigned offset, fb_quadlet_t *result) {
1031     fb_nodeaddr_t addr = offsetGen(base, offset, 4);
1032     return m_device.readReg(addr, result);
1033 }
1034
1035 bool
1036 EAP::writeReg(enum eRegBase base, unsigned offset, fb_quadlet_t data) {
1037     fb_nodeaddr_t addr = offsetGen(base, offset, 4);
1038     return m_device.writeReg(addr, data);
1039 }
1040
1041 bool
1042 EAP::readRegBlock(enum eRegBase base, unsigned offset, fb_quadlet_t *data, size_t length) {
1043     fb_nodeaddr_t addr = offsetGen(base, offset, length);
1044     return m_device.readRegBlock(addr, data, length);
1045 }
1046
1047 bool
1048 EAP::writeRegBlock(enum eRegBase base, unsigned offset, fb_quadlet_t *data, size_t length) {
1049     fb_nodeaddr_t addr = offsetGen(base, offset, length);
1050     return m_device.writeRegBlock(addr, data, length);
1051 }
1052
1053 fb_nodeaddr_t
1054 EAP::offsetGen(enum eRegBase base, unsigned offset, size_t length) {
1055     fb_nodeaddr_t addr;
1056     fb_nodeaddr_t maxlen;
1057     switch(base) {
1058         case eRT_Base:
1059             addr = 0;
1060             maxlen = DICE_EAP_MAX_SIZE;
1061             break;
1062         case eRT_Capability:
1063             addr = m_capability_offset;
1064             maxlen = m_capability_size;
1065             break;
1066         case eRT_Command:
1067             addr = m_cmd_offset;
1068             maxlen = m_cmd_size;
1069             break;
1070         case eRT_Mixer:
1071             addr = m_mixer_offset;
1072             maxlen = m_mixer_size;
1073             break;
1074         case eRT_Peak:
1075             addr = m_peak_offset;
1076             maxlen = m_peak_size;
1077             break;
1078         case eRT_NewRouting:
1079             addr = m_new_routing_offset;
1080             maxlen = m_new_routing_size;
1081             break;
1082         case eRT_NewStreamCfg:
1083             addr = m_new_stream_cfg_offset;
1084             maxlen = m_new_stream_cfg_size;
1085             break;
1086         case eRT_CurrentCfg:
1087             addr = m_curr_cfg_offset;
1088             maxlen = m_curr_cfg_size;
1089             break;
1090         case eRT_Standalone:
1091             addr = m_standalone_offset;
1092             maxlen = m_standalone_size;
1093             break;
1094         case eRT_Application:
1095             addr = m_app_offset;
1096             maxlen = m_app_size;
1097             break;
1098         default:
1099             debugError("Unsupported base address\n");
1100             return 0;
1101     };
1102
1103     // out-of-range check
1104     if(length > maxlen) {
1105         debugError("requested length too large: %zd > %"PRIu64"\n", length, maxlen);
1106         return DICE_INVALID_OFFSET;
1107     }
1108     return DICE_EAP_BASE + addr + offset;
1109 }
1110
1111 /**
1112  * Check whether a device supports eap
1113  * @param d the device to check
1114  * @return true if the device supports EAP
1115  */
1116 bool
1117 EAP::supportsEAP(Device &d)
1118 {
1119     DebugModule &m_debugModule = d.m_debugModule;
1120     quadlet_t tmp;
1121     if(!d.readReg(DICE_EAP_BASE, &tmp)) {
1122         debugOutput(DEBUG_LEVEL_VERBOSE, "Could not read from DICE EAP base address\n");
1123         return false;
1124     }
1125     if(!d.readReg(DICE_EAP_BASE + DICE_EAP_ZERO_MARKER_1, &tmp)) {
1126         debugOutput(DEBUG_LEVEL_VERBOSE, "Could not read from DICE EAP zero marker\n");
1127         return false;
1128     }
1129     if(tmp != 0) {
1130         debugOutput(DEBUG_LEVEL_VERBOSE, "DICE EAP zero marker not zero\n");
1131         return false;
1132     }
1133     return true;
1134 }
1135
1136 // -------------------------------- Mixer ----------------------------------
1137 //   Dice matrix-mixer is a one-dimensional array with inputs index varying
1138 //    first
1139 //
1140 //   "ffado convention" is that inputs are rows and outputs are columns
1141 //
1142 EAP::Mixer::Mixer(EAP &p)
1143 : Control::MatrixMixer(&p.m_device, "MatrixMixer")
1144 , m_eap(p)
1145 , m_coeff(NULL)
1146 , m_debugModule(p.m_debugModule)
1147 {
1148 }
1149
1150 EAP::Mixer::~Mixer()
1151 {
1152     if (m_coeff) {
1153         free(m_coeff);
1154         m_coeff = NULL;
1155     }
1156 }
1157
1158 bool
1159 EAP::Mixer::init()
1160 {
1161     if(!m_eap.m_mixer_exposed) {
1162         debugError("Device does not expose mixer\n");
1163         return false;
1164     }
1165
1166     // remove previous coefficient array
1167     if(m_coeff) {
1168         free(m_coeff);
1169         m_coeff = NULL;
1170     }
1171    
1172     // allocate coefficient array
1173     int nb_inputs = m_eap.m_mixer_nb_tx;
1174     int nb_outputs = m_eap.m_mixer_nb_rx;
1175
1176     m_coeff = (fb_quadlet_t *)calloc(nb_outputs * nb_inputs, sizeof(fb_quadlet_t));
1177
1178     // load initial values
1179     if(!loadCoefficients()) {
1180         debugWarning("Could not initialize coefficients\n");
1181         return false;
1182     }
1183     updateNameCache();
1184     return true;
1185 }
1186
1187 bool
1188 EAP::Mixer::loadCoefficients()
1189 {
1190     if(m_coeff == NULL) {
1191         debugError("Coefficient cache not initialized\n");
1192         return false;
1193     }
1194     int nb_inputs = m_eap.m_mixer_nb_tx;
1195     int nb_outputs = m_eap.m_mixer_nb_rx;
1196     if(!m_eap.readRegBlock(eRT_Mixer, 4, m_coeff, nb_inputs * nb_outputs * 4)) {
1197         debugError("Failed to read coefficients\n");
1198         return false;
1199     }
1200     return true;
1201 }
1202
1203 bool
1204 EAP::Mixer::storeCoefficients()
1205 {
1206     if(m_coeff == NULL) {
1207         debugError("Coefficient cache not initialized\n");
1208         return false;
1209     }
1210     if(m_eap.m_mixer_readonly) {
1211         debugWarning("Mixer is read-only\n");
1212         return false;
1213     }
1214     int nb_inputs = m_eap.m_mixer_nb_tx;
1215     int nb_outputs = m_eap.m_mixer_nb_rx;
1216     if(!m_eap.writeRegBlock(eRT_Mixer, 4, m_coeff, nb_inputs * nb_outputs * 4)) {
1217         debugError("Failed to read coefficients\n");
1218         return false;
1219     }
1220     return true;
1221 }
1222
1223 void
1224 EAP::Mixer::updateNameCache()
1225 {
1226 //    debugWarning("What is this function about?\n");
1227     debugOutput(DEBUG_LEVEL_VERBOSE, "What is this function about?\n");
1228 #if 0
1229     // figure out the number of i/o's
1230     int nb_inputs = m_eap.m_mixer_nb_tx;
1231     int nb_outputs = m_eap.m_mixer_nb_rx;
1232
1233     // clear the previous map
1234     m_input_route_map.clear();
1235     m_output_route_map.clear();
1236
1237     // find the active router configuration
1238     RouterConfig * rcfg = m_eap.getActiveRouterConfig();
1239     if(rcfg == NULL) {
1240         debugError("Could not get active routing info\n");
1241         return;
1242     }
1243
1244     // find the inputs
1245     for(int i=0; i < nb_inputs; i++) {
1246         int ch = i;
1247         // the destination id of the mixer input
1248         int dest_int = m_eap.m_mixer_tx_id;
1249
1250         // from the DICE mixer spec:
1251         // we can have 16 channels per "block"
1252         // if there are more, consecutive block id's are assumed
1253         while(ch > 15) {
1254             ch -= 16;
1255             dest_int += 1;
1256         }
1257         // the destination block and channel corresponding with this
1258         // mixer input is now known
1259         enum eRouteDestination dest = rcfg->intToRouteDestination(dest_int);
1260
1261         // get the source for this mixer channel
1262         m_input_route_map[i] = rcfg->getRouteForDestination(dest, ch);
1263
1264         debugOutput(DEBUG_LEVEL_VERBOSE, "Mixer input channel %2d source: %s (%d)\n", i,
1265                                           srcBlockToString(m_input_route_map[i].src),
1266                                           m_input_route_map[i].srcChannel);
1267     }
1268
1269     // find where the outputs are connected to
1270     for(int i=0; i < nb_outputs; i++) {
1271         int ch = i;
1272         // the source id of the mixer input
1273         int src_int = m_eap.m_mixer_rx_id;
1274
1275         // from the DICE mixer spec:
1276         // we can have 16 channels per "block"
1277         // if there are more, consecutive block id's are assumed
1278         while(ch > 15) {
1279             ch -= 16;
1280             src_int += 1;
1281         }
1282
1283         // the source block and channel corresponding with this
1284         // mixer output is now known
1285         enum eRouteSource src = rcfg->intToRouteSource(src_int);
1286
1287         // get the routing destinations for this mixer channel
1288         m_output_route_map[i] = rcfg->getRoutesForSource(src, ch);
1289
1290         #ifdef DEBUG
1291         std::string destinations;
1292         for ( RouterConfig::RouteVectorIterator it = m_output_route_map[i].begin();
1293             it != m_output_route_map[i].end();
1294             ++it )
1295         {
1296             RouterConfig::Route r = *it;
1297             // check whether the destination is valid
1298             if((r.dst != eRD_Invalid) && (r.dstChannel >= 0)) {
1299                 char tmp[128];
1300                 snprintf(tmp, 128, "%s:%d,", dstBlockToString(r.dst), r.dstChannel);
1301                 destinations += tmp;
1302             }
1303         }
1304         debugOutput(DEBUG_LEVEL_VERBOSE, "Mixer output channel %2d destinations: %s\n", i, destinations.c_str());
1305         #endif
1306     }
1307 #endif
1308 }
1309
1310 void
1311 EAP::Mixer::show()
1312 {
1313     int nb_inputs = m_eap.m_mixer_nb_tx;
1314     int nb_outputs = m_eap.m_mixer_nb_rx;
1315
1316     updateNameCache();
1317
1318     const size_t bufflen = 4096;
1319     char tmp[bufflen];
1320     int cnt;
1321
1322     //
1323     // Caution the user that, displaying as further, because inputs index varies first
1324     //  inputs will appears as columns at the opposite of the "ffado convention"
1325     printMessage("   -- inputs index -->>\n");
1326     cnt = 0;
1327     for(int j=0; j < nb_inputs; j++) {
1328         cnt += snprintf(tmp+cnt, bufflen-cnt, "   %02d   ", j);
1329     }
1330     printMessage("%s\n", tmp);
1331
1332     cnt = 0;
1333     for(int j=0; j < nb_inputs; j++) {
1334         cnt += snprintf(tmp+cnt, bufflen-cnt, "%s ", getRowName(j).data());
1335     }
1336     printMessage("%s\n", tmp);
1337
1338     // display coefficients
1339     for(int i=0; i < nb_outputs; i++) {
1340         cnt = 0;
1341         for(int j=0; j < nb_inputs; j++) {
1342             cnt += snprintf(tmp+cnt, bufflen-cnt, "%07d ", *(m_coeff + nb_inputs * i + j));
1343         }
1344  
1345         // Display destinations name
1346         cnt += snprintf(tmp+cnt, bufflen-cnt, "=[%02d]=> %s", i, getColName(i).data());
1347         printMessage("%s\n", tmp);
1348     }
1349
1350 }
1351
1352 int
1353 EAP::Mixer::canWrite( const int row, const int col)
1354 {
1355     if(m_eap.m_mixer_readonly) {
1356         return false;
1357     }
1358     return (row >= 0 && row < m_eap.m_mixer_nb_tx && col >= 0 && col < m_eap.m_mixer_nb_rx);
1359 }
1360
1361 double
1362 EAP::Mixer::setValue( const int row, const int col, const double val)
1363 {
1364     if(m_eap.m_mixer_readonly) {
1365         debugWarning("Mixer is read-only\n");
1366         return false;
1367     }
1368     int nb_inputs = m_eap.m_mixer_nb_tx;
1369     int addr = ((nb_inputs * col) + row) * 4;
1370     quadlet_t tmp = (quadlet_t) val;
1371     if(!m_eap.writeRegBlock(eRT_Mixer, 4+addr, &tmp, 4)) {
1372         debugError("Failed to write coefficient\n");
1373         return 0;
1374     }
1375     return (double)(tmp);
1376 }
1377
1378 double
1379 EAP::Mixer::getValue( const int row, const int col)
1380 {
1381     int nb_inputs = m_eap.m_mixer_nb_tx;
1382     int addr = ((nb_inputs * col) + row) * 4;
1383     quadlet_t tmp;
1384     if(!m_eap.readRegBlock(eRT_Mixer, 4+addr, &tmp, 4)) {
1385         debugError("Failed to read coefficient\n");
1386         return 0;
1387     }
1388     return (double)(tmp);
1389 }
1390
1391 int
1392 EAP::Mixer::getRowCount()
1393 {
1394     return m_eap.m_mixer_nb_tx;
1395 }
1396
1397 int
1398 EAP::Mixer::getColCount()
1399 {
1400     return m_eap.m_mixer_nb_rx;
1401 }
1402
1403 // full map updates are unsupported
1404 bool
1405 EAP::Mixer::getCoefficientMap(int &) {
1406     return false;
1407 }
1408
1409 bool
1410 EAP::Mixer::storeCoefficientMap(int &) {
1411     if(m_eap.m_mixer_readonly) {
1412         debugWarning("Mixer is read-only\n");
1413         return false;
1414     }
1415     return false;
1416 }
1417
1418 // Names
1419 std::string
1420 EAP::Mixer::getRowName(const int row) {
1421     std::string mixer_src;
1422     if (row < 0 || row > m_eap.m_mixer_nb_tx) return "Invalid";
1423     unsigned int dstid = (eRD_Mixer0<<4) + row; // Mixer has consecutive ID's
1424     debugOutput(DEBUG_LEVEL_VERBOSE, "EAP::Mixer::getRowName( %d ): ID's %d\n", row, dstid);
1425     if (m_eap.m_router){
1426       std::string mixer_dst = m_eap.m_router->getDestinationName(dstid);
1427       mixer_src = m_eap.m_router->getSourceForDestination(mixer_dst);
1428       debugOutput(DEBUG_LEVEL_VERBOSE, "EAP::Mixer::found %s as source for %s\n", mixer_src.data(),
1429                   mixer_dst.data());
1430     }
1431     else {
1432       char tmp[32];
1433       snprintf(tmp, 32, "MixIn:%d", row);
1434       mixer_src = tmp;
1435     }
1436
1437     return mixer_src;
1438 }
1439
1440 std::string
1441 EAP::Mixer::getColName(const int col) {
1442     std::string mixer_dst;
1443     stringlist dest_names;
1444    
1445     // invalid col index
1446     if (col < 0 || col > m_eap.m_mixer_nb_rx) {
1447       mixer_dst.append("Invalid");
1448       return mixer_dst;
1449     }
1450    
1451     unsigned int srcid = (eRS_Mixer<<4) + col; // Mixer has consecutive ID's
1452     debugOutput(DEBUG_LEVEL_VERBOSE, "EAP::Mixer::getColName( %d ): ID's %d\n", col, srcid);
1453     if (m_eap.m_router){
1454       std::string mixer_src = m_eap.m_router->getSourceName(srcid);
1455       dest_names = m_eap.m_router->getDestinationsForSource(mixer_src);
1456       if (dest_names.size() > 0) {
1457         stringlist::iterator it_d = dest_names.begin();
1458         stringlist::iterator it_d_end_m1 = dest_names.end(); --it_d_end_m1;
1459         while (it_d != it_d_end_m1) {
1460           mixer_dst.append((*it_d).c_str()); mixer_dst.append(";\n");
1461           it_d++;
1462         }
1463         mixer_dst.append((*it_d).c_str());
1464       }
1465     } else {
1466       char tmp[32];
1467       snprintf(tmp, 32, "MixOut:%d", col);
1468       mixer_dst.append(tmp);
1469     }
1470
1471     return mixer_dst;
1472 }
1473
1474 //
1475 // ----------- Router -------------
1476 //
1477
1478 EAP::Router::Router(EAP &p)
1479 : Control::CrossbarRouter(&p.m_device, "Router")
1480 , m_eap(p)
1481 , m_peak( *(new PeakSpace(p)) )
1482 , m_debugModule(p.m_debugModule)
1483 {
1484 }
1485
1486 EAP::Router::~Router()
1487 {
1488     delete &m_peak;
1489 }
1490
1491 void
1492 EAP::Router::update()
1493 {
1494     // Clear and
1495     //   define new sources and destinations for the router
1496     m_sources.clear();
1497     m_eap.setupSources();
1498     m_destinations.clear();
1499     m_eap.setupDestinations();
1500     return;
1501 }
1502
1503 void
1504 EAP::Router::addSource(const std::string& basename, enum eRouteSource srcid,
1505                        unsigned int base, unsigned int cnt, unsigned int offset)
1506 {
1507     std::string name = basename + ":";
1508     char tmp[4];
1509     for (unsigned int i=0; i<cnt; i++) {
1510         snprintf(tmp, 4, "%02d", offset+i);
1511         m_sources[name+tmp] = (srcid<<4) + base+i;
1512     }
1513 }
1514
1515 void
1516 EAP::Router::addDestination(const std::string& basename, enum eRouteDestination dstid,
1517                             unsigned int base, unsigned int cnt, unsigned int offset)
1518 {
1519     std::string name = basename + ":";
1520     char tmp[4];
1521     for (unsigned int i=0; i<cnt; i++) {
1522         snprintf(tmp, 4, "%02d", offset+i);
1523         m_destinations[name+tmp] = (dstid<<4) + base+i;
1524     }
1525 }
1526
1527 std::string
1528 EAP::Router::getSourceName(const int srcid)
1529 {
1530     for (std::map<std::string, int>::iterator it=m_sources.begin(); it!=m_sources.end(); ++it) {
1531         if (it->second == srcid) {
1532             return it->first;
1533         }
1534     }
1535     return "";
1536 }
1537
1538 std::string
1539 EAP::Router::getDestinationName(const int dstid)
1540 {
1541     for (std::map<std::string, int>::iterator it=m_destinations.begin(); it!=m_destinations.end(); ++it) {
1542         if (it->second == dstid) {
1543             return it->first;
1544         }
1545     }
1546     return "";
1547 }
1548
1549 int
1550 EAP::Router::getSourceIndex(std::string name)
1551 {
1552     if (m_sources.count(name) < 1)
1553         return -1;
1554     return m_sources[name];
1555 }
1556
1557 int
1558 EAP::Router::getDestinationIndex(std::string name)
1559 {
1560     if (m_destinations.count(name) < 1)
1561         return -1;
1562     return m_destinations[name];
1563 }
1564
1565 stringlist
1566 EAP::Router::getSourceNames()
1567 {
1568     stringlist n;
1569
1570     for (std::map<std::string, int>::iterator it=m_sources.begin(); it!=m_sources.end(); ++it)
1571         n.push_back(it->first);
1572     return n;
1573 }
1574
1575 stringlist
1576 EAP::Router::getDestinationNames()
1577 {
1578     stringlist n;
1579     for (std::map<std::string, int>::iterator it=m_destinations.begin(); it!=m_destinations.end(); ++it)
1580         n.push_back(it->first);
1581     return n;
1582 }
1583
1584 stringlist
1585 EAP::Router::getDestinationsForSource(const std::string& srcname) {
1586     RouterConfig* rcfg = m_eap.getActiveRouterConfig();
1587     if(rcfg == NULL) {
1588         debugError("Could not request active router configuration\n");
1589         return stringlist();
1590     }
1591     stringlist ret;
1592     std::vector<unsigned char> dests = rcfg->getDestinationsForSource(m_sources[srcname]);
1593     std::string name;
1594     for (unsigned int i=0; i<dests.size(); ++i) {
1595         if ((name = getDestinationName(dests[i])) != "") {
1596           ret.push_back(name);
1597         }
1598     }
1599     return ret;
1600 }
1601 std::string
1602 EAP::Router::getSourceForDestination(const std::string& dstname) {
1603     RouterConfig* rcfg = m_eap.getActiveRouterConfig();
1604     if(rcfg == NULL) {
1605         debugError("Could not request active router configuration\n");
1606         return "";
1607     }
1608     int source = rcfg->getSourceForDestination(m_destinations[dstname]);
1609     return getSourceName(source);
1610 }
1611
1612
1613 bool
1614 EAP::Router::canConnect(const int source, const int dest)
1615 {
1616     debugWarning("TODO: Implement canConnect(0x%02x, 0x%02x)\n", source, dest);
1617
1618     // we can connect anything
1619     // FIXME: can we?
1620     return true;
1621 }
1622
1623 bool
1624 EAP::Router::setConnectionState(const int source, const int dest, const bool enable)
1625 {
1626     debugOutput(DEBUG_LEVEL_VERBOSE,"Router::setConnectionState(0x%02x -> 0x%02x ? %i)\n", source, dest, enable);
1627     // get the routing configuration
1628     RouterConfig *rcfg = m_eap.getActiveRouterConfig();
1629     if(rcfg == NULL) {
1630         debugError("Could not request active router configuration\n");
1631         return false;
1632     }
1633
1634     bool ret = false;
1635     if (enable) {
1636         ret = rcfg->setupRoute(source, dest);
1637     } else {
1638         ret = rcfg->muteRoute(dest);
1639         // FixMe:
1640         //   Not all devices are intended to work correctly with a variable number of router entries
1641         //     so muting the destination is preferable
1642         //   Now, this might be useful for some other ones, but is it the right place for this ?
1643         //   ret = rcfg->removeRoute(source, dest);
1644     }
1645     m_eap.updateCurrentRouterConfig(*rcfg);
1646     return ret;
1647 }
1648
1649 bool
1650 EAP::Router::getConnectionState(const int source, const int dest)
1651 {
1652     // get the routing configuration
1653     RouterConfig *rcfg = m_eap.getActiveRouterConfig();
1654     if(rcfg == NULL) {
1655         debugError("Could not request active router configuration\n");
1656         return false;
1657     }
1658     if (rcfg->getSourceForDestination(dest) == source) {
1659         return true;
1660     }
1661     return false;
1662 }
1663
1664 bool
1665 EAP::Router::canConnect(const std::string& src, const std::string& dst)
1666 {
1667     int srcidx = getSourceIndex(src);
1668     int dstidx = getDestinationIndex(dst);
1669     return canConnect(srcidx, dstidx);
1670 }
1671
1672 bool
1673 EAP::Router::setConnectionState(const std::string& src, const std::string& dst, const bool enable)
1674 {
1675     int srcidx = getSourceIndex(src);
1676     int dstidx = getDestinationIndex(dst);
1677     return setConnectionState(srcidx, dstidx, enable);
1678 }
1679
1680 bool
1681 EAP::Router::getConnectionState(const std::string& src, const std::string& dst)
1682 {
1683     int srcidx = getSourceIndex(src);
1684     int dstidx = getDestinationIndex(dst);
1685     return getConnectionState(srcidx, dstidx);
1686 }
1687
1688
1689 bool
1690 EAP::Router::clearAllConnections()
1691 {
1692     // build a new empty routing configuration
1693     RouterConfig newcfg = EAP::RouterConfig(m_eap);
1694
1695     // upload the new router config
1696     if(!m_eap.updateCurrentRouterConfig(newcfg)) {
1697         debugError("Could not update router config\n");
1698         return false;
1699     }
1700     return true;
1701 }
1702
1703 bool
1704 EAP::Router::hasPeakMetering()
1705 {
1706     return m_eap.m_router_exposed;
1707 }
1708
1709 double
1710 EAP::Router::getPeakValue(const std::string& dest)
1711 {
1712     m_peak.read();
1713     unsigned char dst = m_destinations[dest];
1714     return m_peak.getPeak(dst);
1715 }
1716
1717 std::map<std::string, double>
1718 EAP::Router::getPeakValues()
1719 {
1720     m_peak.read();
1721     std::map<std::string, double> ret;
1722     std::map<unsigned char, int> peaks = m_peak.getPeaks();
1723     std::string name;
1724     for (std::map<unsigned char, int>::iterator it=peaks.begin(); it!=peaks.end(); ++it) {
1725         name = getDestinationName(it->first);
1726         if (name.size() != 0) {
1727           ret[name] = it->second;
1728         }
1729     }
1730     return ret;
1731 }
1732
1733 void
1734 EAP::Router::show()
1735 {
1736     // print the peak space as it also contains the routing configuration
1737     printMessage("Router sources:\n");
1738     printMessage(" %llu sources:\n", (unsigned long long)m_sources.size());
1739     for ( std::map<std::string, int>::iterator it=m_sources.begin(); it!=m_sources.end(); ++it ) {
1740         printMessage(" 0x%02x : %s\n", (*it).second, (*it).first.c_str());
1741     }
1742     printMessage("Router destinations:\n");
1743     printMessage(" %llu destinations:\n", (unsigned long long)m_destinations.size());
1744     for ( std::map<std::string, int>::iterator it=m_destinations.begin(); it!=m_destinations.end(); ++it ) {
1745         printMessage(" 0x%02x : %s\n", (*it).second, (*it).first.c_str());
1746     }
1747     printMessage("Router connections:\n");
1748     stringlist sources = getSourceNames();
1749     stringlist destinations = getDestinationNames();
1750     for (stringlist::iterator it1=sources.begin(); it1!=sources.end(); ++it1) {
1751         for (stringlist::iterator it2=destinations.begin(); it2!=destinations.end(); ++it2) {
1752             if (getConnectionState(*it1, *it2)) {
1753                 printMessage(" %s -> %s\n", it1->c_str(), it2->c_str());
1754             }
1755         }
1756     }
1757     printMessage("Active router config:\n");
1758     m_eap.getActiveRouterConfig()->show();
1759     printMessage("Active peak config:\n");
1760     m_peak.read();
1761     m_peak.show();
1762 }
1763
1764 // ----------- routing config -------------
1765 EAP::RouterConfig::RouterConfig(EAP &p)
1766 : m_eap(p)
1767 , m_base(eRT_None), m_offset(0)
1768 , m_debugModule(p.m_debugModule)
1769 {}
1770
1771 EAP::RouterConfig::RouterConfig(EAP &p, enum eRegBase b, unsigned int o)
1772 : m_eap(p)
1773 , m_base(b), m_offset(o)
1774 , m_debugModule(p.m_debugModule)
1775 {}
1776
1777 EAP::RouterConfig::~RouterConfig()
1778 {}
1779
1780 bool
1781 EAP::RouterConfig::read(enum eRegBase base, unsigned offset)
1782 {
1783     // first clear the current route vector
1784     clearRoutes();
1785
1786     uint32_t nb_routes;
1787     if(!m_eap.readRegBlock(base, offset, &nb_routes, 4)) {
1788         debugError("Failed to read number of entries\n");
1789         return false;
1790     }
1791     if(nb_routes == 0) {
1792         debugWarning("No routes found. Base 0x%x, offset 0x%x\n", base, offset);
1793     }
1794
1795     // read the route info
1796     uint32_t tmp_entries[nb_routes];
1797     if(!m_eap.readRegBlock(base, offset+4, tmp_entries, nb_routes*4)) {
1798         debugError("Failed to read router config block information\n");
1799         return false;
1800     }
1801
1802     // decode into the routing map
1803     for(unsigned int i=0; i < nb_routes; i++) {
1804         m_routes2.push_back(std::make_pair(tmp_entries[i]&0xff, (tmp_entries[i]>>8)&0xff));
1805     }
1806     return true;
1807 }
1808
1809 bool
1810 EAP::RouterConfig::write(enum eRegBase base, unsigned offset)
1811 {
1812     uint32_t nb_routes = m_routes2.size();
1813     if(nb_routes == 0) {
1814         debugWarning("Writing 0 routes? This will deactivate routing and make the device very silent...\n");
1815     }
1816     if (nb_routes > 128) {
1817         debugError("More then 128 are not possible, only the first 128 routes will get saved!\n");
1818         nb_routes = 128;
1819     }
1820     uint32_t tmp_entries[nb_routes];
1821
1822     // encode from the routing vector
1823     int i=0;
1824     for (RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it) {
1825         tmp_entries[i] = ((it->second<<8) + it->first)&0xffff;
1826         ++i;
1827     }
1828
1829     unsigned int nb_routes_max = m_eap.getMaxNbRouterEntries();
1830     uint32_t zeros[nb_routes_max+1];
1831
1832     for (unsigned int i=0; i<nb_routes_max+1; ++i) zeros[i] = 0;
1833     if(!m_eap.writeRegBlock(base, offset, zeros, (nb_routes_max+1)*4)) {
1834         debugError("Failed to write zeros to router config block\n");
1835         return false;
1836     }
1837
1838     // write the result to the device
1839     if(!m_eap.writeRegBlock(base, offset+4, tmp_entries, nb_routes*4)) {
1840         debugError("Failed to write router config block information\n");
1841         return false;
1842     }
1843     if(!m_eap.writeRegBlock(base, offset, &nb_routes, 4)) {
1844         debugError("Failed to write number of entries\n");
1845         return false;
1846     }
1847     return true;
1848 }
1849
1850 // Clear the route vector;
1851 bool
1852 EAP::RouterConfig::clearRoutes() {
1853     m_routes2.clear();
1854     return true;
1855 }
1856
1857 // Create a destination with a prescribed source
1858 bool
1859 EAP::RouterConfig::createRoute(unsigned char src, unsigned char dest) {
1860     debugOutput(DEBUG_LEVEL_VERBOSE,"RouterConfig::createRoute( 0x%02x, 0x%02x )\n", src, dest);
1861     m_routes2.push_back(std::make_pair(dest, src));
1862     return true;
1863 }
1864
1865 bool
1866 EAP::RouterConfig::setupRoute(unsigned char src, unsigned char dest) {
1867     debugOutput(DEBUG_LEVEL_VERBOSE,"RouterConfig::setupRoute( 0x%02x, 0x%02x )\n", src, dest);
1868     // modify exisiting routing
1869     for (RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it) {
1870         if (it->first == dest) {
1871           it->second = src;
1872           return true;
1873         }
1874     }
1875     // destination does not yet exist; create it
1876     return createRoute(src, dest);
1877 }
1878
1879
1880 bool
1881 EAP::RouterConfig::removeRoute(unsigned char src, unsigned char dest) {
1882     debugOutput(DEBUG_LEVEL_VERBOSE,"RouterConfig::removeRoute( 0x%02x, 0x%02x )\n", src, dest);
1883     for (RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it) {
1884         if (it->first == dest) {
1885           if (it->second != src) {
1886             return false;
1887           }
1888           m_routes2.erase(it);
1889           return true;
1890         }
1891     }
1892     return false;
1893 }
1894
1895 bool
1896 EAP::RouterConfig::muteRoute(unsigned char dest) {
1897     for (RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it) {
1898         if (it->first == dest) {
1899           it->second = m_eap.getSMuteId();
1900           return true;
1901         }
1902     }
1903     return false;
1904 }
1905
1906 bool
1907 EAP::RouterConfig::removeRoute(unsigned char dest) {
1908     debugOutput(DEBUG_LEVEL_VERBOSE,"RouterConfig::removeRoute( 0x%02x )\n", dest);
1909     for (RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it) {
1910         if (it->first == dest) {
1911           m_routes2.erase(it);
1912           return true;
1913         }
1914     }
1915     return false;
1916 }
1917
1918 unsigned char
1919 EAP::RouterConfig::getSourceForDestination(unsigned char dest) {
1920     for (RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it) {
1921         if (it->first == dest) {
1922           return it->second;
1923         }
1924     }
1925     return -1;
1926 }
1927
1928 std::vector<unsigned char>
1929 EAP::RouterConfig::getDestinationsForSource(unsigned char source) {
1930     std::vector<unsigned char> ret;
1931     for (RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it) {
1932         if (it->second == source) {
1933             ret.push_back(it->first);
1934         }
1935     }
1936     return ret;
1937 }
1938
1939 void
1940 EAP::RouterConfig::show()
1941 {
1942     printMessage("%llu routes\n", (unsigned long long)m_routes2.size());
1943     for ( RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it ) {
1944         printMessage("0x%02x -> 0x%02x\n", it->second, it->first);
1945     }
1946 }
1947
1948 //
1949 // ----------- peak space -------------
1950 //
1951
1952 bool
1953 EAP::PeakSpace::read(enum eRegBase base, unsigned offset)
1954 {
1955     uint32_t nb_routes;
1956     // we have to figure out the number of entries through the currently
1957     // active router config
1958     RouterConfig *rcfg = m_eap.getActiveRouterConfig();
1959     if(rcfg == NULL) {
1960         debugError("Could not get active router config\n");
1961         return false;
1962     }
1963     nb_routes = rcfg->getNbRoutes();
1964
1965     // read the peak/route info
1966     uint32_t tmp_entries[nb_routes];
1967     if(!m_eap.readRegBlock(base, offset, tmp_entries, nb_routes*4)) {
1968         debugError("Failed to read peak block information\n");
1969         return false;
1970     }
1971     // parse the peaks into the map
1972     for (unsigned int i=0; i<nb_routes; ++i) {
1973         unsigned char dest = tmp_entries[i]&0xff;
1974         int peak = (tmp_entries[i]&0xfff0000)>>16;
1975         if (m_peaks.count(dest) == 0 || m_peaks[dest] < peak) {
1976             m_peaks[dest] = peak;
1977         }
1978     }
1979     return true;
1980 }
1981
1982
1983 void
1984 EAP::PeakSpace::show()
1985 {
1986     printMessage("  %zi peaks\n", m_peaks.size());
1987     for (std::map<unsigned char, int>::iterator it=m_peaks.begin(); it!=m_peaks.end(); ++it) {
1988         printMessage("0x%02x : %i\n", it->first, it->second);
1989     }
1990 }
1991
1992 int
1993 EAP::PeakSpace::getPeak(unsigned char dst) {
1994     int ret = m_peaks[dst];
1995     m_peaks.erase(dst);
1996     return ret;
1997 }
1998
1999 std::map<unsigned char, int>
2000 EAP::PeakSpace::getPeaks() {
2001     // Create a new empty map
2002     std::map<unsigned char, int> ret;
2003     // Swap the peak map with the new and empty one
2004     ret.swap(m_peaks);
2005     // Return the now filled map of the peaks :-)
2006     return ret;
2007 }
2008
2009 // ----------- stream config block -------------
2010 EAP::StreamConfig::StreamConfig(EAP &p, enum eRegBase b, unsigned int o)
2011 : m_eap(p)
2012 , m_base(b), m_offset(o)
2013 , m_nb_tx(0), m_nb_rx(0)
2014 , m_tx_configs(NULL), m_rx_configs(NULL)
2015 , m_debugModule(p.m_debugModule)
2016 {
2017
2018 }
2019
2020 EAP::StreamConfig::~StreamConfig()
2021 {
2022     if(m_tx_configs) delete[]m_tx_configs;
2023     if(m_rx_configs) delete[]m_rx_configs;
2024 }
2025
2026 bool
2027 EAP::StreamConfig::read(enum eRegBase base, unsigned offset)
2028 {
2029     if(!m_eap.readRegBlock(base, offset, &m_nb_tx, 4)) {
2030         debugError("Failed to read number of tx entries\n");
2031         return false;
2032     }
2033     if(!m_eap.readRegBlock(base, offset+4, &m_nb_rx, 4)) {
2034         debugError("Failed to read number of rx entries\n");
2035         return false;
2036     }
2037     debugOutput(DEBUG_LEVEL_VERBOSE, " Entries: TX: %u, RX: %u\n", m_nb_tx, m_nb_rx);
2038
2039     if(m_tx_configs) {
2040         delete[]m_tx_configs;
2041         m_tx_configs = NULL;
2042     }
2043     if(m_rx_configs) {
2044         delete[]m_rx_configs;
2045         m_rx_configs = NULL;
2046     }
2047    
2048     offset += 8;
2049     if(m_nb_tx > 0) {
2050         m_tx_configs = new struct ConfigBlock[m_nb_tx];
2051         for(unsigned int i=0; i<m_nb_tx; i++) {
2052             fb_quadlet_t *ptr = reinterpret_cast<fb_quadlet_t *>(&(m_tx_configs[i]));
2053             if(!m_eap.readRegBlock(base, offset, ptr, sizeof(struct ConfigBlock))) {
2054                 debugError("Failed to read tx entry %d\n", i);
2055                 return false;
2056             }
2057             offset += sizeof(struct ConfigBlock);
2058         }
2059     }
2060
2061     if(m_nb_rx > 0) {
2062         m_rx_configs = new struct ConfigBlock[m_nb_rx];
2063         for(unsigned int i=0; i<m_nb_rx; i++) {
2064             fb_quadlet_t *ptr = reinterpret_cast<fb_quadlet_t *>(&(m_rx_configs[i]));
2065             if(!m_eap.readRegBlock(base, offset, ptr, sizeof(struct ConfigBlock))) {
2066                 debugError("Failed to read rx entry %d\n", i);
2067                 return false;
2068             }
2069             offset += sizeof(struct ConfigBlock);
2070         }
2071     }
2072     return true;
2073 }
2074
2075 bool
2076 EAP::StreamConfig::write(enum eRegBase base, unsigned offset)
2077 {
2078     if(!m_eap.writeRegBlock(base, offset, &m_nb_tx, 4)) {
2079         debugError("Failed to write number of tx entries\n");
2080         return false;
2081     }
2082     if(!m_eap.writeRegBlock(base, offset+4, &m_nb_rx, 4)) {
2083         debugError("Failed to write number of rx entries\n");
2084         return false;
2085     }
2086
2087     offset += 8;
2088     for(unsigned int i=0; i<m_nb_tx; i++) {
2089         fb_quadlet_t *ptr = reinterpret_cast<fb_quadlet_t *>(&(m_tx_configs[i]));
2090         if(!m_eap.writeRegBlock(base, offset, ptr, sizeof(struct ConfigBlock))) {
2091             debugError("Failed to write tx entry %d\n", i);
2092             return false;
2093         }
2094         offset += sizeof(struct ConfigBlock);
2095     }
2096
2097     for(unsigned int i=0; i<m_nb_rx; i++) {
2098         fb_quadlet_t *ptr = reinterpret_cast<fb_quadlet_t *>(&(m_rx_configs[i]));
2099         if(!m_eap.writeRegBlock(base, offset, ptr, sizeof(struct ConfigBlock))) {
2100             debugError("Failed to write rx entry %d\n", i);
2101             return false;
2102         }
2103         offset += sizeof(struct ConfigBlock);
2104     }
2105     return true;
2106 }
2107
2108 stringlist
2109 EAP::StreamConfig::getNamesForBlock(struct ConfigBlock &b)
2110 {
2111     stringlist names;
2112     char namestring[DICE_EAP_CHANNEL_CONFIG_NAMESTR_LEN_BYTES+1];
2113
2114     memcpy(namestring, b.names, DICE_EAP_CHANNEL_CONFIG_NAMESTR_LEN_BYTES);
2115
2116     // Strings from the device are always little-endian,
2117     // so byteswap for big-endian machines
2118     #if __BYTE_ORDER == __BIG_ENDIAN
2119     byteSwapBlock((quadlet_t *)namestring, DICE_EAP_CHANNEL_CONFIG_NAMESTR_LEN_QUADS);
2120     #endif
2121
2122     namestring[DICE_EAP_CHANNEL_CONFIG_NAMESTR_LEN_BYTES]='\0';
2123     return m_eap.m_device.splitNameString(std::string(namestring));
2124 }
2125
2126 stringlist
2127 EAP::StreamConfig::getTxNamesString(unsigned int i)
2128 {
2129     return getNamesForBlock(m_tx_configs[i]);
2130 }
2131
2132 stringlist
2133 EAP::StreamConfig::getRxNamesString(unsigned int i)
2134 {
2135     return getNamesForBlock(m_rx_configs[i]);
2136 }
2137
2138 void
2139 EAP::StreamConfig::showConfigBlock(struct ConfigBlock &b)
2140 {
2141     printMessage(" Channel count : %u audio, %u midi\n", b.nb_audio, b.nb_midi);
2142     printMessage(" AC3 Map       : 0x%08X\n", b.ac3_map);
2143     stringlist channel_names  = getNamesForBlock(b);
2144     printMessage("  Channel names :\n");
2145     for ( stringlist::iterator it = channel_names.begin();
2146         it != channel_names.end();
2147         ++it )
2148     {
2149         printMessage("     %s\n", (*it).c_str());
2150     }
2151 }
2152
2153 void
2154 EAP::StreamConfig::show()
2155 {
2156     for(unsigned int i=0; i<m_nb_tx; i++) {
2157         printMessage("TX Config block %d\n", i);
2158         showConfigBlock(m_tx_configs[i]);
2159     }
2160     for(unsigned int i=0; i<m_nb_rx; i++) {
2161         printMessage("RX Config block %d\n", i);
2162         showConfigBlock(m_rx_configs[i]);
2163     }
2164 }
2165
2166 } // namespace Dice
2167
2168
2169 // vim: et
Note: See TracBrowser for help on using the browser.