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

Revision 2137, 66.3 kB (checked in by jwoithe, 10 years ago)

dice: default router setup patch 4/10 from Philippe Carriere: DICE EAP Introduce EAP::setDefaultRouterConfig. This introduce the public EAP function to be called for resetting the content of the router memory space to a default config. The related functions for low, mid and high rate are also introduced for a generic device. These latters are intended to be customized for specific devices.

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);
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);
252             // the ADAT receiver
253             addSource("ADAT", 0, 8, eRS_ADAT);
254             // the Mixer outputs
255             addSource("MixerOut", 0, 16, eRS_Mixer);
256             // the first audio port
257             addSource("InS0", 0, 8, eRS_InS0);
258             // the ARM audio port
259             addSource("ARM", 0, 8, eRS_ARM);
260             // the 1394 stream receivers
261             addSource("1394_0", 0, 16, eRS_ARX0);
262             addSource("1394_1", 0, 16, eRS_ARX1);
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);
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);
313             // the ADAT receiver
314             addDestination("ADAT", 0, 8, eRD_ADAT);
315             // the Mixer outputs
316             addDestination("MixerIn", 0, 16, eRD_Mixer0);
317             addDestination("MixerIn", 0, 2, eRD_Mixer1, 16);
318             // the first audio port
319             addDestination("InS0", 0, 8, eRD_InS0);
320             // the ARM audio port
321             addDestination("ARM", 0, 8, eRD_ARM);
322             // the 1394 stream receivers
323             addDestination("1394_0", 0, 16, eRD_ATX0);
324             addDestination("1394_1", 0, 16, eRD_ATX1);
325             // mute
326             addDestination("Mute", 0, 1, eRD_Muted);
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             addDestination("InS1", 0, 8, eRD_InS1);
570         case DICE_EAP_CAP_GENERAL_CHIP_DICEMINI:
571             // the 1394 stream receivers
572             for (i=0; i<8; i++) {
573               addRoute(eRS_InS0, i, eRD_ATX0, i);
574             }
575             for (i=0; i<8; i++) {
576               addRoute(eRS_InS1, i, eRD_ATX0, i+8);
577             }
578             for (i=0; i<8; i++) {
579               addRoute(eRS_ADAT, i, eRD_ATX1, i);
580             }
581             for (i=0; i<8; i++) {
582               addRoute(eRS_AES, i, eRD_ATX1, i+8);
583             }
584             // The audio ports
585             // Ensure that audio port are not muted
586             for (i=0; i<8; i++) {
587               addRoute(eRS_ARX0, i, eRD_InS0, i);
588             }
589             // the AES receiver
590             for (i=0; i<8; i++) {
591               addRoute(eRS_Muted, 0, eRD_AES, i);
592             }
593             // the ADAT receiver
594             for (i=0; i<8; i++) {
595               addRoute(eRS_Muted, 0, eRD_ADAT, i);
596             }
597             // the Mixer outputs
598             for (i=0; i<8; i++) {
599               addRoute(eRS_InS0, i, eRD_Mixer0, i);
600             }
601             for (i=0; i<8; i++) {
602               addRoute(eRS_ADAT, i, eRD_Mixer0, i+8);
603             }
604             for (i=0; i<2; i++) {
605               addRoute(eRS_Muted, 0, eRD_Mixer0, i+16);
606             }
607             // the ARM audio port
608             for (i=0; i<0; i++) {
609               addRoute(eRS_Muted, 0, eRD_ARM, i);
610             }
611             // mute
612             addRoute(eRS_Muted, 0, eRD_Muted, 0);
613             break;
614         default:
615             // this is an unsupported chip
616             break;
617     }
618 }
619
620 void
621 EAP::setupDefaultRouterConfig_mid() {
622     setupDefaultRouterConfig_low();
623 }
624
625 void
626 EAP::setupDefaultRouterConfig_high() {
627     setupDefaultRouterConfig_low();
628 }
629
630 void
631 EAP::setupDefaultRouterConfig() {
632     // First clear routes
633     RouterConfig *rcfg = getActiveRouterConfig();
634     rcfg->clearRoutes();
635
636     // Then define the configuration depending on the samplerate
637     switch(m_device.getCurrentConfig()) {
638         case Device::eDC_Low: setupDefaultRouterConfig_low(); break;
639         case Device::eDC_Mid: setupDefaultRouterConfig_mid(); break;
640         case Device::eDC_High: setupDefaultRouterConfig_high(); break;
641         default:
642             debugError("Unsupported configuration mode\n");
643             return;
644     }
645
646     updateCurrentRouterConfig(*rcfg);
647 }
648
649 /**
650  * Add (create) a route from source to destination
651  */
652 bool
653 EAP::addRoute(enum eRouteSource srcid, unsigned int base_src, enum eRouteDestination dstid,
654               unsigned int base_dst)
655 {
656     RouterConfig *rcfg = getActiveRouterConfig();
657     return rcfg->createRoute((srcid<<4) + base_src, (dstid<<4) + base_dst);
658 }
659
660
661 /**
662  * Uploads a new stream configuration to the device
663  * @param scfg The new StreamConfig
664  * @param low store as config for the low rates
665  * @param mid store as config for the mid rates
666  * @param high store as config for the high rates
667  * @return true if successful, false otherwise
668  */
669 bool
670 EAP::updateStreamConfig(StreamConfig& scfg, bool low, bool mid, bool high) {
671     // write the stream config to the appropriate memory space on the device
672     if(!scfg.write(eRT_NewStreamCfg, 0)) {
673         debugError("Could not write new stream configuration\n");
674         return false;
675     }
676     // perform the store operation
677     if(!loadStreamConfig(low, mid, high)) {
678         debugError("Could not activate new stream configuration\n");
679         updateConfigurationCache(); // for consistency
680         return false;
681     }
682     return updateConfigurationCache();
683 }
684
685 /**
686  * Uploads a new router and stream configuration to the device
687  * @param  rcfg The new RouterConfig
688  * @param  scfg The new StreamConfig
689  * @param low store as config for the low rates
690  * @param mid store as config for the mid rates
691  * @param high store as config for the high rates
692  * @return true if successful, false otherwise
693  */
694 bool
695 EAP::updateStreamConfig(RouterConfig& rcfg, StreamConfig& scfg, bool low, bool mid, bool high) {
696     // write the router config to the appropriate memory space on the device
697     if(!rcfg.write(eRT_NewRouting, 0)) {
698         debugError("Could not write new router configuration\n");
699         return false;
700     }
701     // write the stream config to the appropriate memory space on the device
702     if(!scfg.write(eRT_NewStreamCfg, 0)) {
703         debugError("Could not write new stream configuration\n");
704         return false;
705     }
706     // perform the store operation
707     if(!loadRouterAndStreamConfig(low, mid, high)) {
708         debugError("Could not activate new router/stream configuration\n");
709         updateConfigurationCache(); // for consistency
710         return false;
711     }
712     return updateConfigurationCache();
713 }
714
715
716 bool
717 EAP::loadFlashConfig() {
718     bool retval = true;
719     debugWarning("Untested code\n");
720     fb_quadlet_t cmd = DICE_EAP_CMD_OPCODE_LD_FLASH_CFG;
721     cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_EXECUTE;
722     if(!commandHelper(cmd)) {
723         debugWarning("Command failed\n");
724         retval = false;
725     }
726     retval &= updateConfigurationCache();
727     return retval;
728 }
729
730 bool
731 EAP::storeFlashConfig() {
732     //debugWarning("Untested code\n") // Works. -Arnold;
733     fb_quadlet_t cmd = DICE_EAP_CMD_OPCODE_ST_FLASH_CFG;
734     cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_EXECUTE;
735     return commandHelper(cmd);
736 }
737
738 // helpers
739 void
740 EAP::show()
741 {
742     printMessage("== DICE EAP ==\n");
743     printMessage("Parameter Space info:\n");
744     printMessage(" Capability        : offset=%04X size=%06d\n", m_capability_offset, m_capability_size);
745     printMessage(" Command           : offset=%04X size=%06d\n", m_cmd_offset, m_cmd_size);
746     printMessage(" Mixer             : offset=%04X size=%06d\n", m_mixer_offset, m_mixer_size);
747     printMessage(" Peak              : offset=%04X size=%06d\n", m_peak_offset, m_peak_size);
748     printMessage(" New Routing Cfg   : offset=%04X size=%06d\n", m_new_routing_offset, m_new_routing_size);
749     printMessage(" New Stream Cfg    : offset=%04X size=%06d\n", m_new_stream_cfg_offset, m_new_stream_cfg_size);
750     printMessage(" Current Cfg       : offset=%04X size=%06d\n", m_curr_cfg_offset, m_curr_cfg_size);
751     printMessage(" Standalone Cfg    : offset=%04X size=%06d\n", m_standalone_offset, m_standalone_size);
752     printMessage(" Application Space : offset=%04X size=%06d\n", m_app_offset, m_app_size);
753
754     printMessage("Capabilities:\n");
755     printMessage(" Router: %sexposed, %swritable, %sstored, %d routes\n",
756                                      (m_router_exposed?"":"not "),
757                                      (m_router_readonly?"not ":""),
758                                      (m_router_flashstored?"":"not "),
759                                      m_router_nb_entries);
760     printMessage(" Mixer : %sexposed, %swritable, %sstored\n",
761                                      (m_mixer_exposed?"":"not "),
762                                      (m_mixer_readonly?"not ":""),
763                                      (m_mixer_flashstored?"":"not "));
764     printMessage("         tx id: (%d==eRD_Mixer0) ? %s, rx id: (%d==eRS_Mixer) ? %s\n",
765                                      m_mixer_tx_id, (m_mixer_tx_id == eRD_Mixer0)?"true":"false",
766                                      m_mixer_rx_id, (m_mixer_rx_id == eRS_Mixer) ?"true":"false");
767     printMessage("         nb tx channels: %d, nb rx channels: %d\n", m_mixer_nb_tx, m_mixer_nb_rx);
768     printMessage(" General: dynamic stream config %ssupported\n",
769                                      (m_general_support_dynstream?"":"not "));
770     printMessage("          flash load and store %ssupported\n",
771                                      (m_general_support_flash?"":"not "));
772     printMessage("          peak metering %s\n",
773                                      (m_general_peak_enabled?"enabled":"disabled"));
774     printMessage("          stream config %sstored\n",
775                                      (m_general_stream_cfg_stored?"":"not "));
776     printMessage("          max TX streams: %d, max RX streams: %d\n",
777                                      m_general_max_tx, m_general_max_rx);
778
779     if(m_general_chip == DICE_EAP_CAP_GENERAL_CHIP_DICEII) {
780         printMessage("          Chip: DICE-II\n");
781     } else if(m_general_chip == DICE_EAP_CAP_GENERAL_CHIP_DICEMINI) {
782         printMessage("          Chip: DICE Mini (TCD2210)\n");
783     } else if(m_general_chip == DICE_EAP_CAP_GENERAL_CHIP_DICEJR) {
784         printMessage("          Chip: DICE Junior (TCD2220)\n");
785     }
786
787     printMessage("--- Mixer configuration ---\n");
788     if(m_mixer) {
789         m_mixer->show();
790     }
791     printMessage("--- Router/Peak space ---\n");
792     if(m_router) {
793         m_router->show();
794     }
795
796     printMessage("--- Active Router ---\n");
797     RouterConfig *rcfg = getActiveRouterConfig();
798     if(rcfg) {
799         rcfg->show();
800     }
801     printMessage("--- Active Stream ---\n");
802     StreamConfig *scfg = getActiveStreamConfig();
803     if(scfg) {
804         scfg->show();
805     }
806
807 // fixme
808 //     size_t len = 0x1000;
809 //     quadlet_t tmp[len];
810 //     if(!readRegBlock( eRT_CurrentCfg, DICE_EAP_CURRCFG_LOW_STREAM, tmp, len*4) ) {
811 //         debugError("Failed to read block\n");
812 //     } else {
813 //         hexDumpQuadlets(tmp, len);
814 //     }
815
816 }
817 void
818 EAP::showApplication()
819 {
820     printMessage("--- Application space ---\n");
821     fb_quadlet_t* tmp = (fb_quadlet_t *)calloc(128, sizeof(fb_quadlet_t));
822     unsigned int appsize = m_app_size; /// m_app_size is rather big. Start with the first four block of 128 quadlets...
823     unsigned int offset = 0;
824     while ( appsize > 0 ) {
825         if ( ! readRegBlock( eRT_Application, offset, tmp, ((appsize<128)?appsize:128)*sizeof(fb_quadlet_t) ) )
826             appsize = 0;
827         else {
828             hexDumpQuadlets(tmp, 128);
829             offset += 128*sizeof(fb_quadlet_t);
830             appsize -= 128*sizeof(fb_quadlet_t);
831         }
832     }
833 }
834
835 void
836 EAP::showFullRouter()
837 {
838     printMessage("--- Full router content ---\n");
839
840     printMessage(" %d entries to read\n", m_router_nb_entries);
841
842     unsigned int offset;
843     switch(m_device.getCurrentConfig()) {
844         case Device::eDC_Low: offset = DICE_EAP_CURRCFG_LOW_ROUTER; break;
845         case Device::eDC_Mid: offset = DICE_EAP_CURRCFG_MID_ROUTER; break;
846         case Device::eDC_High: offset = DICE_EAP_CURRCFG_HIGH_ROUTER; break;
847         default: offset = 0; break;
848     }
849
850     // Current config
851     printMessage("  Current Configuration:\n");
852     // First bloc is the effective number of routes
853     uint32_t nb_routes;
854     if(!readRegBlock(eRT_CurrentCfg, offset, &nb_routes, 4)) {
855         printMessage("Failed to read number of entries\n");
856         return;
857     }
858     printMessage("   %d routes\n", nb_routes);
859
860     // read the route info
861     uint32_t tmp_entries[m_router_nb_entries];
862     if(!readRegBlock(eRT_CurrentCfg, offset+4, tmp_entries, m_router_nb_entries*4)) {
863         printMessage("Failed to read router config block information\n");
864         return;
865     }
866
867     // decode and print
868     for(unsigned int i=0; i < m_router_nb_entries; i++) {
869         printMessage("    %d: 0x%02x <- 0x%02x;\n", i, tmp_entries[i]&0xff, (tmp_entries[i]>>8)&0xff);
870     }
871
872     // New config
873     printMessage("  New Configuration:\n");
874     // First bloc is the effective number of routes
875     if(!readRegBlock(eRT_NewRouting, 0, &nb_routes, 4)) {
876         printMessage("Failed to read number of entries\n");
877         return;
878     }
879     printMessage("   %d routes\n", nb_routes);
880
881     // read the route info
882     if(!readRegBlock(eRT_NewRouting, 4, tmp_entries, m_router_nb_entries*4)) {
883         printMessage("Failed to read router config block information\n");
884         return;
885     }
886
887     // decode and print
888     for(unsigned int i=0; i < m_router_nb_entries; i++) {
889         printMessage("    %d: 0x%02x <- 0x%02x;\n", i, tmp_entries[i]&0xff, (tmp_entries[i]>>8)&0xff);
890     }
891
892     return;
893 }
894
895 void
896 EAP::showFullPeakSpace()
897 {
898     printMessage("--- Full Peak space content ---\n");
899
900     // read the peak/route info
901     uint32_t tmp_entries[m_router_nb_entries];
902     if(!readRegBlock(eRT_Peak, 0, tmp_entries, m_router_nb_entries*4)) {
903         debugError("Failed to read peak block information\n");
904         return;
905     }
906     // decode and print
907     for (unsigned int i=0; i<m_router_nb_entries; ++i) {
908         printMessage("  %d: 0x%02x: %d;\n", i, tmp_entries[i]&0xff, (tmp_entries[i]&0xfff0000)>>16);
909     }
910     return;
911 }
912
913 // EAP load/store operations
914
915 enum EAP::eWaitReturn
916 EAP::operationBusy() {
917     fb_quadlet_t tmp;
918     if(!readReg(eRT_Command, DICE_EAP_COMMAND_OPCODE, &tmp)) {
919         debugError("Could not read opcode register\n");
920         return eWR_Error;
921     }
922     if( (tmp & DICE_EAP_CMD_OPCODE_FLAG_LD_EXECUTE) == DICE_EAP_CMD_OPCODE_FLAG_LD_EXECUTE) {
923         return eWR_Busy;
924     } else {
925         return eWR_Done;
926     }
927 }
928
929 enum EAP::eWaitReturn
930 EAP::waitForOperationEnd(int max_wait_time_ms) {
931     int max_waits = max_wait_time_ms;
932
933     while(max_waits--) {
934         enum eWaitReturn retval = operationBusy();
935         switch(retval) {
936             case eWR_Busy:
937                 break; // not done yet, keep waiting
938             case eWR_Done:
939                 return eWR_Done;
940             case eWR_Error:
941             case eWR_Timeout:
942                 debugError("Error while waiting for operation to end. (%d)\n", retval);
943         }
944         Util::SystemTimeSource::SleepUsecRelative(1000);
945     }
946     return eWR_Timeout;
947 }
948
949 bool
950 EAP::commandHelper(fb_quadlet_t cmd) {
951     // check whether another command is still running
952     if(operationBusy() == eWR_Busy) {
953         debugError("Other operation in progress\n");
954         return false;
955     }
956
957     // execute the command
958     if(!writeReg(eRT_Command, DICE_EAP_COMMAND_OPCODE, cmd)) {
959         debugError("Could not write opcode register\n");
960         return false;
961     }
962
963     // wait for the operation to end
964     enum eWaitReturn retval = waitForOperationEnd();
965     switch(retval) {
966         case eWR_Done:
967             break; // do nothing
968         case eWR_Timeout:
969             debugWarning("Time-out while waiting for operation to end. (%d)\n", retval);
970             return false;
971         case eWR_Error:
972         case eWR_Busy: // can't be returned
973             debugError("Error while waiting for operation to end. (%d)\n", retval);
974             return false;
975     }
976
977     // check the return value
978     if(!readReg(eRT_Command, DICE_EAP_COMMAND_RETVAL, &cmd)) {
979         debugError("Could not read return value register\n");
980         return false;
981     }
982     if(cmd != 0) {
983         debugWarning("Command failed\n");
984         return false;
985     } else {
986         debugOutput(DEBUG_LEVEL_VERBOSE, "Command successful\n");
987         return true;
988     }
989 }
990
991 bool
992 EAP::loadRouterConfig(bool low, bool mid, bool high) {
993     fb_quadlet_t cmd = DICE_EAP_CMD_OPCODE_LD_ROUTER;
994     if(low) cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_LOW;
995     if(mid) cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_MID;
996     if(high) cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_HIGH;
997     cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_EXECUTE;
998     return commandHelper(cmd);
999 }
1000
1001 bool
1002 EAP::loadStreamConfig(bool low, bool mid, bool high) {
1003     debugWarning("Untested code\n");
1004     fb_quadlet_t cmd = DICE_EAP_CMD_OPCODE_LD_STRM_CFG;
1005     if(low) cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_LOW;
1006     if(mid) cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_MID;
1007     if(high) cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_HIGH;
1008     cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_EXECUTE;
1009     return commandHelper(cmd);
1010 }
1011
1012 bool
1013 EAP::loadRouterAndStreamConfig(bool low, bool mid, bool high) {
1014     debugWarning("Untested code\n");
1015     fb_quadlet_t cmd = DICE_EAP_CMD_OPCODE_LD_RTR_STRM_CFG;
1016     if(low) cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_LOW;
1017     if(mid) cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_MID;
1018     if(high) cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_HIGH;
1019     cmd |= DICE_EAP_CMD_OPCODE_FLAG_LD_EXECUTE;
1020     return commandHelper(cmd);
1021 }
1022
1023 /*
1024   I/O operations
1025   */
1026 bool
1027 EAP::readReg(enum eRegBase base, unsigned offset, fb_quadlet_t *result) {
1028     fb_nodeaddr_t addr = offsetGen(base, offset, 4);
1029     return m_device.readReg(addr, result);
1030 }
1031
1032 bool
1033 EAP::writeReg(enum eRegBase base, unsigned offset, fb_quadlet_t data) {
1034     fb_nodeaddr_t addr = offsetGen(base, offset, 4);
1035     return m_device.writeReg(addr, data);
1036 }
1037
1038 bool
1039 EAP::readRegBlock(enum eRegBase base, unsigned offset, fb_quadlet_t *data, size_t length) {
1040     fb_nodeaddr_t addr = offsetGen(base, offset, length);
1041     return m_device.readRegBlock(addr, data, length);
1042 }
1043
1044 bool
1045 EAP::writeRegBlock(enum eRegBase base, unsigned offset, fb_quadlet_t *data, size_t length) {
1046     fb_nodeaddr_t addr = offsetGen(base, offset, length);
1047     return m_device.writeRegBlock(addr, data, length);
1048 }
1049
1050 fb_nodeaddr_t
1051 EAP::offsetGen(enum eRegBase base, unsigned offset, size_t length) {
1052     fb_nodeaddr_t addr;
1053     fb_nodeaddr_t maxlen;
1054     switch(base) {
1055         case eRT_Base:
1056             addr = 0;
1057             maxlen = DICE_EAP_MAX_SIZE;
1058             break;
1059         case eRT_Capability:
1060             addr = m_capability_offset;
1061             maxlen = m_capability_size;
1062             break;
1063         case eRT_Command:
1064             addr = m_cmd_offset;
1065             maxlen = m_cmd_size;
1066             break;
1067         case eRT_Mixer:
1068             addr = m_mixer_offset;
1069             maxlen = m_mixer_size;
1070             break;
1071         case eRT_Peak:
1072             addr = m_peak_offset;
1073             maxlen = m_peak_size;
1074             break;
1075         case eRT_NewRouting:
1076             addr = m_new_routing_offset;
1077             maxlen = m_new_routing_size;
1078             break;
1079         case eRT_NewStreamCfg:
1080             addr = m_new_stream_cfg_offset;
1081             maxlen = m_new_stream_cfg_size;
1082             break;
1083         case eRT_CurrentCfg:
1084             addr = m_curr_cfg_offset;
1085             maxlen = m_curr_cfg_size;
1086             break;
1087         case eRT_Standalone:
1088             addr = m_standalone_offset;
1089             maxlen = m_standalone_size;
1090             break;
1091         case eRT_Application:
1092             addr = m_app_offset;
1093             maxlen = m_app_size;
1094             break;
1095         default:
1096             debugError("Unsupported base address\n");
1097             return 0;
1098     };
1099
1100     // out-of-range check
1101     if(length > maxlen) {
1102         debugError("requested length too large: %zd > %"PRIu64"\n", length, maxlen);
1103         return DICE_INVALID_OFFSET;
1104     }
1105     return DICE_EAP_BASE + addr + offset;
1106 }
1107
1108 /**
1109  * Check whether a device supports eap
1110  * @param d the device to check
1111  * @return true if the device supports EAP
1112  */
1113 bool
1114 EAP::supportsEAP(Device &d)
1115 {
1116     DebugModule &m_debugModule = d.m_debugModule;
1117     quadlet_t tmp;
1118     if(!d.readReg(DICE_EAP_BASE, &tmp)) {
1119         debugOutput(DEBUG_LEVEL_VERBOSE, "Could not read from DICE EAP base address\n");
1120         return false;
1121     }
1122     if(!d.readReg(DICE_EAP_BASE + DICE_EAP_ZERO_MARKER_1, &tmp)) {
1123         debugOutput(DEBUG_LEVEL_VERBOSE, "Could not read from DICE EAP zero marker\n");
1124         return false;
1125     }
1126     if(tmp != 0) {
1127         debugOutput(DEBUG_LEVEL_VERBOSE, "DICE EAP zero marker not zero\n");
1128         return false;
1129     }
1130     return true;
1131 }
1132
1133 // -------------------------------- Mixer ----------------------------------
1134 //   Dice matrix-mixer is a one-dimensional array with inputs index varying
1135 //    first
1136 //
1137 //   "ffado convention" is that inputs are rows and outputs are columns
1138 //
1139 EAP::Mixer::Mixer(EAP &p)
1140 : Control::MatrixMixer(&p.m_device, "MatrixMixer")
1141 , m_eap(p)
1142 , m_coeff(NULL)
1143 , m_debugModule(p.m_debugModule)
1144 {
1145 }
1146
1147 EAP::Mixer::~Mixer()
1148 {
1149     if (m_coeff) {
1150         free(m_coeff);
1151         m_coeff = NULL;
1152     }
1153 }
1154
1155 bool
1156 EAP::Mixer::init()
1157 {
1158     if(!m_eap.m_mixer_exposed) {
1159         debugError("Device does not expose mixer\n");
1160         return false;
1161     }
1162
1163     // remove previous coefficient array
1164     if(m_coeff) {
1165         free(m_coeff);
1166         m_coeff = NULL;
1167     }
1168    
1169     // allocate coefficient array
1170     int nb_inputs = m_eap.m_mixer_nb_tx;
1171     int nb_outputs = m_eap.m_mixer_nb_rx;
1172
1173     m_coeff = (fb_quadlet_t *)calloc(nb_outputs * nb_inputs, sizeof(fb_quadlet_t));
1174
1175     // load initial values
1176     if(!loadCoefficients()) {
1177         debugWarning("Could not initialize coefficients\n");
1178         return false;
1179     }
1180     updateNameCache();
1181     return true;
1182 }
1183
1184 bool
1185 EAP::Mixer::loadCoefficients()
1186 {
1187     if(m_coeff == NULL) {
1188         debugError("Coefficient cache not initialized\n");
1189         return false;
1190     }
1191     int nb_inputs = m_eap.m_mixer_nb_tx;
1192     int nb_outputs = m_eap.m_mixer_nb_rx;
1193     if(!m_eap.readRegBlock(eRT_Mixer, 4, m_coeff, nb_inputs * nb_outputs * 4)) {
1194         debugError("Failed to read coefficients\n");
1195         return false;
1196     }
1197     return true;
1198 }
1199
1200 bool
1201 EAP::Mixer::storeCoefficients()
1202 {
1203     if(m_coeff == NULL) {
1204         debugError("Coefficient cache not initialized\n");
1205         return false;
1206     }
1207     if(m_eap.m_mixer_readonly) {
1208         debugWarning("Mixer is read-only\n");
1209         return false;
1210     }
1211     int nb_inputs = m_eap.m_mixer_nb_tx;
1212     int nb_outputs = m_eap.m_mixer_nb_rx;
1213     if(!m_eap.writeRegBlock(eRT_Mixer, 4, m_coeff, nb_inputs * nb_outputs * 4)) {
1214         debugError("Failed to read coefficients\n");
1215         return false;
1216     }
1217     return true;
1218 }
1219
1220 void
1221 EAP::Mixer::updateNameCache()
1222 {
1223     debugWarning("What is this function about?\n");
1224 #if 0
1225     // figure out the number of i/o's
1226     int nb_inputs = m_eap.m_mixer_nb_tx;
1227     int nb_outputs = m_eap.m_mixer_nb_rx;
1228
1229     // clear the previous map
1230     m_input_route_map.clear();
1231     m_output_route_map.clear();
1232
1233     // find the active router configuration
1234     RouterConfig * rcfg = m_eap.getActiveRouterConfig();
1235     if(rcfg == NULL) {
1236         debugError("Could not get active routing info\n");
1237         return;
1238     }
1239
1240     // find the inputs
1241     for(int i=0; i < nb_inputs; i++) {
1242         int ch = i;
1243         // the destination id of the mixer input
1244         int dest_int = m_eap.m_mixer_tx_id;
1245
1246         // from the DICE mixer spec:
1247         // we can have 16 channels per "block"
1248         // if there are more, consecutive block id's are assumed
1249         while(ch > 15) {
1250             ch -= 16;
1251             dest_int += 1;
1252         }
1253         // the destination block and channel corresponding with this
1254         // mixer input is now known
1255         enum eRouteDestination dest = rcfg->intToRouteDestination(dest_int);
1256
1257         // get the source for this mixer channel
1258         m_input_route_map[i] = rcfg->getRouteForDestination(dest, ch);
1259
1260         debugOutput(DEBUG_LEVEL_VERBOSE, "Mixer input channel %2d source: %s (%d)\n", i,
1261                                           srcBlockToString(m_input_route_map[i].src),
1262                                           m_input_route_map[i].srcChannel);
1263     }
1264
1265     // find where the outputs are connected to
1266     for(int i=0; i < nb_outputs; i++) {
1267         int ch = i;
1268         // the source id of the mixer input
1269         int src_int = m_eap.m_mixer_rx_id;
1270
1271         // from the DICE mixer spec:
1272         // we can have 16 channels per "block"
1273         // if there are more, consecutive block id's are assumed
1274         while(ch > 15) {
1275             ch -= 16;
1276             src_int += 1;
1277         }
1278
1279         // the source block and channel corresponding with this
1280         // mixer output is now known
1281         enum eRouteSource src = rcfg->intToRouteSource(src_int);
1282
1283         // get the routing destinations for this mixer channel
1284         m_output_route_map[i] = rcfg->getRoutesForSource(src, ch);
1285
1286         #ifdef DEBUG
1287         std::string destinations;
1288         for ( RouterConfig::RouteVectorIterator it = m_output_route_map[i].begin();
1289             it != m_output_route_map[i].end();
1290             ++it )
1291         {
1292             RouterConfig::Route r = *it;
1293             // check whether the destination is valid
1294             if((r.dst != eRD_Invalid) && (r.dstChannel >= 0)) {
1295                 char tmp[128];
1296                 snprintf(tmp, 128, "%s:%d,", dstBlockToString(r.dst), r.dstChannel);
1297                 destinations += tmp;
1298             }
1299         }
1300         debugOutput(DEBUG_LEVEL_VERBOSE, "Mixer output channel %2d destinations: %s\n", i, destinations.c_str());
1301         #endif
1302     }
1303 #endif
1304 }
1305
1306 void
1307 EAP::Mixer::show()
1308 {
1309     int nb_inputs = m_eap.m_mixer_nb_tx;
1310     int nb_outputs = m_eap.m_mixer_nb_rx;
1311
1312     updateNameCache();
1313
1314     const size_t bufflen = 4096;
1315     char tmp[bufflen];
1316     int cnt;
1317
1318     //
1319     // Caution the user that, displaying as further, because inputs index varies first
1320     //  inputs will appears as columns at the opposite of the "ffado convention"
1321     printMessage("   -- inputs index -->>\n");
1322     cnt = 0;
1323     for(int j=0; j < nb_inputs; j++) {
1324         cnt += snprintf(tmp+cnt, bufflen-cnt, "   %02d   ", j);
1325     }
1326     printMessage("%s\n", tmp);
1327
1328     cnt = 0;
1329     for(int j=0; j < nb_inputs; j++) {
1330         cnt += snprintf(tmp+cnt, bufflen-cnt, "%s ", getRowName(j).data());
1331     }
1332     printMessage("%s\n", tmp);
1333
1334     // display coefficients
1335     for(int i=0; i < nb_outputs; i++) {
1336         cnt = 0;
1337         for(int j=0; j < nb_inputs; j++) {
1338             cnt += snprintf(tmp+cnt, bufflen-cnt, "%07d ", *(m_coeff + nb_inputs * i + j));
1339         }
1340  
1341         // Display destinations name
1342         cnt += snprintf(tmp+cnt, bufflen-cnt, "=[%02d]=> %s", i, getColName(i).data());
1343         printMessage("%s\n", tmp);
1344     }
1345
1346 }
1347
1348 int
1349 EAP::Mixer::canWrite( const int row, const int col)
1350 {
1351     if(m_eap.m_mixer_readonly) {
1352         return false;
1353     }
1354     return (row >= 0 && row < m_eap.m_mixer_nb_tx && col >= 0 && col < m_eap.m_mixer_nb_rx);
1355 }
1356
1357 double
1358 EAP::Mixer::setValue( const int row, const int col, const double val)
1359 {
1360     if(m_eap.m_mixer_readonly) {
1361         debugWarning("Mixer is read-only\n");
1362         return false;
1363     }
1364     int nb_inputs = m_eap.m_mixer_nb_tx;
1365     int addr = ((nb_inputs * col) + row) * 4;
1366     quadlet_t tmp = (quadlet_t) val;
1367     if(!m_eap.writeRegBlock(eRT_Mixer, 4+addr, &tmp, 4)) {
1368         debugError("Failed to write coefficient\n");
1369         return 0;
1370     }
1371     return (double)(tmp);
1372 }
1373
1374 double
1375 EAP::Mixer::getValue( const int row, const int col)
1376 {
1377     int nb_inputs = m_eap.m_mixer_nb_tx;
1378     int addr = ((nb_inputs * col) + row) * 4;
1379     quadlet_t tmp;
1380     if(!m_eap.readRegBlock(eRT_Mixer, 4+addr, &tmp, 4)) {
1381         debugError("Failed to read coefficient\n");
1382         return 0;
1383     }
1384     return (double)(tmp);
1385 }
1386
1387 int
1388 EAP::Mixer::getRowCount()
1389 {
1390     return m_eap.m_mixer_nb_tx;
1391 }
1392
1393 int
1394 EAP::Mixer::getColCount()
1395 {
1396     return m_eap.m_mixer_nb_rx;
1397 }
1398
1399 // full map updates are unsupported
1400 bool
1401 EAP::Mixer::getCoefficientMap(int &) {
1402     return false;
1403 }
1404
1405 bool
1406 EAP::Mixer::storeCoefficientMap(int &) {
1407     if(m_eap.m_mixer_readonly) {
1408         debugWarning("Mixer is read-only\n");
1409         return false;
1410     }
1411     return false;
1412 }
1413
1414 // Names
1415 std::string
1416 EAP::Mixer::getRowName(const int row) {
1417     std::string mixer_src;
1418     if (row < 0 || row > m_eap.m_mixer_nb_tx) return "Invalid";
1419     unsigned int dstid = (eRD_Mixer0<<4) + row; // Mixer has consecutive ID's
1420     debugOutput(DEBUG_LEVEL_VERBOSE, "EAP::Mixer::getRowName( %d ): ID's %d\n", row, dstid);
1421     if (m_eap.m_router){
1422       std::string mixer_dst = m_eap.m_router->getDestinationName(dstid);
1423       mixer_src = m_eap.m_router->getSourceForDestination(mixer_dst);
1424       debugOutput(DEBUG_LEVEL_VERBOSE, "EAP::Mixer::found %s as source for %s\n", mixer_src.data(),
1425                   mixer_dst.data());
1426     }
1427     else {
1428       char tmp[32];
1429       snprintf(tmp, 32, "MixIn:%d", row);
1430       mixer_src = tmp;
1431     }
1432
1433     return mixer_src;
1434 }
1435
1436 std::string
1437 EAP::Mixer::getColName(const int col) {
1438     std::string mixer_dst;
1439     stringlist dest_names;
1440    
1441     // invalid col index
1442     if (col < 0 || col > m_eap.m_mixer_nb_rx) {
1443       mixer_dst.append("Invalid");
1444       return mixer_dst;
1445     }
1446    
1447     unsigned int srcid = (eRS_Mixer<<4) + col; // Mixer has consecutive ID's
1448     debugOutput(DEBUG_LEVEL_VERBOSE, "EAP::Mixer::getColName( %d ): ID's %d\n", col, srcid);
1449     if (m_eap.m_router){
1450       std::string mixer_src = m_eap.m_router->getSourceName(srcid);
1451       dest_names = m_eap.m_router->getDestinationsForSource(mixer_src);
1452       if (dest_names.size() > 0) {
1453         stringlist::iterator it_d = dest_names.begin();
1454         stringlist::iterator it_d_end_m1 = dest_names.end(); --it_d_end_m1;
1455         while (it_d != it_d_end_m1) {
1456           mixer_dst.append((*it_d).c_str()); mixer_dst.append(";\n");
1457           it_d++;
1458         }
1459         mixer_dst.append((*it_d).c_str());
1460       }
1461     } else {
1462       char tmp[32];
1463       snprintf(tmp, 32, "MixOut:%d", col);
1464       mixer_dst.append(tmp);
1465     }
1466
1467     return mixer_dst;
1468 }
1469
1470 //
1471 // ----------- Router -------------
1472 //
1473
1474 EAP::Router::Router(EAP &p)
1475 : Control::CrossbarRouter(&p.m_device, "Router")
1476 , m_eap(p)
1477 , m_peak( *(new PeakSpace(p)) )
1478 , m_debugModule(p.m_debugModule)
1479 {
1480 }
1481
1482 EAP::Router::~Router()
1483 {
1484     delete &m_peak;
1485 }
1486
1487 void
1488 EAP::Router::update()
1489 {
1490     // Clear and
1491     //   define new sources and destinations for the router
1492     m_sources.clear();
1493     m_eap.setupSources();
1494     m_destinations.clear();
1495     m_eap.setupDestinations();
1496     return;
1497 }
1498
1499 void
1500 EAP::Router::addSource(const std::string& basename, enum eRouteSource srcid,
1501                        unsigned int base, unsigned int cnt, unsigned int offset)
1502 {
1503     std::string name = basename + ":";
1504     char tmp[4];
1505     for (unsigned int i=0; i<cnt; i++) {
1506         snprintf(tmp, 4, "%02d", offset+i);
1507         m_sources[name+tmp] = (srcid<<4) + base+i;
1508     }
1509 }
1510
1511 void
1512 EAP::Router::addDestination(const std::string& basename, enum eRouteDestination dstid,
1513                             unsigned int base, unsigned int cnt, unsigned int offset)
1514 {
1515     std::string name = basename + ":";
1516     char tmp[4];
1517     for (unsigned int i=0; i<cnt; i++) {
1518         snprintf(tmp, 4, "%02d", offset+i);
1519         m_destinations[name+tmp] = (dstid<<4) + base+i;
1520     }
1521 }
1522
1523 std::string
1524 EAP::Router::getSourceName(const int srcid)
1525 {
1526     for (std::map<std::string, int>::iterator it=m_sources.begin(); it!=m_sources.end(); ++it) {
1527         if (it->second == srcid) {
1528             return it->first;
1529         }
1530     }
1531     return "";
1532 }
1533
1534 std::string
1535 EAP::Router::getDestinationName(const int dstid)
1536 {
1537     for (std::map<std::string, int>::iterator it=m_destinations.begin(); it!=m_destinations.end(); ++it) {
1538         if (it->second == dstid) {
1539             return it->first;
1540         }
1541     }
1542     return "";
1543 }
1544
1545 int
1546 EAP::Router::getSourceIndex(std::string name)
1547 {
1548     if (m_sources.count(name) < 1)
1549         return -1;
1550     return m_sources[name];
1551 }
1552
1553 int
1554 EAP::Router::getDestinationIndex(std::string name)
1555 {
1556     if (m_destinations.count(name) < 1)
1557         return -1;
1558     return m_destinations[name];
1559 }
1560
1561 stringlist
1562 EAP::Router::getSourceNames()
1563 {
1564     stringlist n;
1565
1566     for (std::map<std::string, int>::iterator it=m_sources.begin(); it!=m_sources.end(); ++it)
1567         n.push_back(it->first);
1568     return n;
1569 }
1570
1571 stringlist
1572 EAP::Router::getDestinationNames()
1573 {
1574     stringlist n;
1575     for (std::map<std::string, int>::iterator it=m_destinations.begin(); it!=m_destinations.end(); ++it)
1576         n.push_back(it->first);
1577     return n;
1578 }
1579
1580 stringlist
1581 EAP::Router::getDestinationsForSource(const std::string& srcname) {
1582     RouterConfig* rcfg = m_eap.getActiveRouterConfig();
1583     if(rcfg == NULL) {
1584         debugError("Could not request active router configuration\n");
1585         return stringlist();
1586     }
1587     stringlist ret;
1588     std::vector<unsigned char> dests = rcfg->getDestinationsForSource(m_sources[srcname]);
1589     std::string name;
1590     for (unsigned int i=0; i<dests.size(); ++i) {
1591         if ((name = getDestinationName(dests[i])) != "") {
1592           ret.push_back(name);
1593         }
1594     }
1595     return ret;
1596 }
1597 std::string
1598 EAP::Router::getSourceForDestination(const std::string& dstname) {
1599     RouterConfig* rcfg = m_eap.getActiveRouterConfig();
1600     if(rcfg == NULL) {
1601         debugError("Could not request active router configuration\n");
1602         return "";
1603     }
1604     int source = rcfg->getSourceForDestination(m_destinations[dstname]);
1605     return getSourceName(source);
1606 }
1607
1608
1609 bool
1610 EAP::Router::canConnect(const int source, const int dest)
1611 {
1612     debugWarning("TODO: Implement canConnect(0x%02x, 0x%02x)\n", source, dest);
1613
1614     // we can connect anything
1615     // FIXME: can we?
1616     return true;
1617 }
1618
1619 bool
1620 EAP::Router::setConnectionState(const int source, const int dest, const bool enable)
1621 {
1622     debugOutput(DEBUG_LEVEL_VERBOSE,"Router::setConnectionState(0x%02x -> 0x%02x ? %i)\n", source, dest, enable);
1623     // get the routing configuration
1624     RouterConfig *rcfg = m_eap.getActiveRouterConfig();
1625     if(rcfg == NULL) {
1626         debugError("Could not request active router configuration\n");
1627         return false;
1628     }
1629
1630     bool ret = false;
1631     if (enable) {
1632         ret = rcfg->setupRoute(source, dest);
1633     } else {
1634         ret = rcfg->muteRoute(dest);
1635         // FixMe:
1636         //   Not all devices are intended to work correctly with a variable number of router entries
1637         //     so muting the destination is preferable
1638         //   Now, this might be useful for some other ones, but is it the right place for this ?
1639         //   ret = rcfg->removeRoute(source, dest);
1640     }
1641     m_eap.updateCurrentRouterConfig(*rcfg);
1642     return ret;
1643 }
1644
1645 bool
1646 EAP::Router::getConnectionState(const int source, const int dest)
1647 {
1648     // get the routing configuration
1649     RouterConfig *rcfg = m_eap.getActiveRouterConfig();
1650     if(rcfg == NULL) {
1651         debugError("Could not request active router configuration\n");
1652         return false;
1653     }
1654     if (rcfg->getSourceForDestination(dest) == source) {
1655         return true;
1656     }
1657     return false;
1658 }
1659
1660 bool
1661 EAP::Router::canConnect(const std::string& src, const std::string& dst)
1662 {
1663     int srcidx = getSourceIndex(src);
1664     int dstidx = getDestinationIndex(dst);
1665     return canConnect(srcidx, dstidx);
1666 }
1667
1668 bool
1669 EAP::Router::setConnectionState(const std::string& src, const std::string& dst, const bool enable)
1670 {
1671     int srcidx = getSourceIndex(src);
1672     int dstidx = getDestinationIndex(dst);
1673     return setConnectionState(srcidx, dstidx, enable);
1674 }
1675
1676 bool
1677 EAP::Router::getConnectionState(const std::string& src, const std::string& dst)
1678 {
1679     int srcidx = getSourceIndex(src);
1680     int dstidx = getDestinationIndex(dst);
1681     return getConnectionState(srcidx, dstidx);
1682 }
1683
1684
1685 bool
1686 EAP::Router::clearAllConnections()
1687 {
1688     // build a new empty routing configuration
1689     RouterConfig newcfg = EAP::RouterConfig(m_eap);
1690
1691     // upload the new router config
1692     if(!m_eap.updateCurrentRouterConfig(newcfg)) {
1693         debugError("Could not update router config\n");
1694         return false;
1695     }
1696     return true;
1697 }
1698
1699 bool
1700 EAP::Router::hasPeakMetering()
1701 {
1702     return m_eap.m_router_exposed;
1703 }
1704
1705 double
1706 EAP::Router::getPeakValue(const std::string& dest)
1707 {
1708     m_peak.read();
1709     unsigned char dst = m_destinations[dest];
1710     return m_peak.getPeak(dst);
1711 }
1712
1713 std::map<std::string, double>
1714 EAP::Router::getPeakValues()
1715 {
1716     m_peak.read();
1717     std::map<std::string, double> ret;
1718     std::map<unsigned char, int> peaks = m_peak.getPeaks();
1719     for (std::map<unsigned char, int>::iterator it=peaks.begin(); it!=peaks.end(); ++it) {
1720         ret[getDestinationName(it->first)] = it->second;
1721     }
1722     return ret;
1723 }
1724
1725 void
1726 EAP::Router::show()
1727 {
1728     // print the peak space as it also contains the routing configuration
1729     printMessage("Router sources:\n");
1730     printMessage(" %llu sources:\n", (unsigned long long)m_sources.size());
1731     for ( std::map<std::string, int>::iterator it=m_sources.begin(); it!=m_sources.end(); ++it ) {
1732         printMessage(" 0x%02x : %s\n", (*it).second, (*it).first.c_str());
1733     }
1734     printMessage("Router destinations:\n");
1735     printMessage(" %llu destinations:\n", (unsigned long long)m_destinations.size());
1736     for ( std::map<std::string, int>::iterator it=m_destinations.begin(); it!=m_destinations.end(); ++it ) {
1737         printMessage(" 0x%02x : %s\n", (*it).second, (*it).first.c_str());
1738     }
1739     printMessage("Router connections:\n");
1740     stringlist sources = getSourceNames();
1741     stringlist destinations = getDestinationNames();
1742     for (stringlist::iterator it1=sources.begin(); it1!=sources.end(); ++it1) {
1743         for (stringlist::iterator it2=destinations.begin(); it2!=destinations.end(); ++it2) {
1744             if (getConnectionState(*it1, *it2)) {
1745                 printMessage(" %s -> %s\n", it1->c_str(), it2->c_str());
1746             }
1747         }
1748     }
1749     printMessage("Active router config:\n");
1750     m_eap.getActiveRouterConfig()->show();
1751     printMessage("Active peak config:\n");
1752     m_peak.read();
1753     m_peak.show();
1754 }
1755
1756 // ----------- routing config -------------
1757 EAP::RouterConfig::RouterConfig(EAP &p)
1758 : m_eap(p)
1759 , m_base(eRT_None), m_offset(0)
1760 , m_debugModule(p.m_debugModule)
1761 {}
1762
1763 EAP::RouterConfig::RouterConfig(EAP &p, enum eRegBase b, unsigned int o)
1764 : m_eap(p)
1765 , m_base(b), m_offset(o)
1766 , m_debugModule(p.m_debugModule)
1767 {}
1768
1769 EAP::RouterConfig::~RouterConfig()
1770 {}
1771
1772 bool
1773 EAP::RouterConfig::read(enum eRegBase base, unsigned offset)
1774 {
1775     // first clear the current route vector
1776     clearRoutes();
1777
1778     uint32_t nb_routes;
1779     if(!m_eap.readRegBlock(base, offset, &nb_routes, 4)) {
1780         debugError("Failed to read number of entries\n");
1781         return false;
1782     }
1783     if(nb_routes == 0) {
1784         debugWarning("No routes found. Base 0x%x, offset 0x%x\n", base, offset);
1785     }
1786
1787     // read the route info
1788     uint32_t tmp_entries[nb_routes];
1789     if(!m_eap.readRegBlock(base, offset+4, tmp_entries, nb_routes*4)) {
1790         debugError("Failed to read router config block information\n");
1791         return false;
1792     }
1793
1794     // decode into the routing map
1795     for(unsigned int i=0; i < nb_routes; i++) {
1796         m_routes2.push_back(std::make_pair(tmp_entries[i]&0xff, (tmp_entries[i]>>8)&0xff));
1797     }
1798     return true;
1799 }
1800
1801 bool
1802 EAP::RouterConfig::write(enum eRegBase base, unsigned offset)
1803 {
1804     uint32_t nb_routes = m_routes2.size();
1805     if(nb_routes == 0) {
1806         debugWarning("Writing 0 routes? This will deactivate routing and make the device very silent...\n");
1807     }
1808     if (nb_routes > 128) {
1809         debugError("More then 128 are not possible, only the first 128 routes will get saved!\n");
1810         nb_routes = 128;
1811     }
1812     uint32_t tmp_entries[nb_routes];
1813
1814     // encode from the routing vector
1815     int i=0;
1816     for (RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it) {
1817         tmp_entries[i] = ((it->second<<8) + it->first)&0xffff;
1818         ++i;
1819     }
1820
1821     unsigned int nb_routes_max = m_eap.getMaxNbRouterEntries();
1822     uint32_t zeros[nb_routes_max+1];
1823
1824     for (unsigned int i=0; i<nb_routes_max+1; ++i) zeros[i] = 0;
1825     if(!m_eap.writeRegBlock(base, offset, zeros, (nb_routes_max+1)*4)) {
1826         debugError("Failed to write zeros to router config block\n");
1827         return false;
1828     }
1829
1830     // write the result to the device
1831     if(!m_eap.writeRegBlock(base, offset+4, tmp_entries, nb_routes*4)) {
1832         debugError("Failed to write router config block information\n");
1833         return false;
1834     }
1835     if(!m_eap.writeRegBlock(base, offset, &nb_routes, 4)) {
1836         debugError("Failed to write number of entries\n");
1837         return false;
1838     }
1839     return true;
1840 }
1841
1842 // Clear the route vector;
1843 bool
1844 EAP::RouterConfig::clearRoutes() {
1845     m_routes2.clear();
1846     return true;
1847 }
1848
1849 // Create a destination with a prescribed source
1850 bool
1851 EAP::RouterConfig::createRoute(unsigned char src, unsigned char dest) {
1852     debugOutput(DEBUG_LEVEL_VERBOSE,"RouterConfig::createRoute( 0x%02x, 0x%02x )\n", src, dest);
1853     m_routes2.push_back(std::make_pair(dest, src));
1854     return true;
1855 }
1856
1857 bool
1858 EAP::RouterConfig::setupRoute(unsigned char src, unsigned char dest) {
1859     debugOutput(DEBUG_LEVEL_VERBOSE,"RouterConfig::setupRoute( 0x%02x, 0x%02x )\n", src, dest);
1860     // modify exisiting routing
1861     for (RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it) {
1862         if (it->first == dest) {
1863           it->second = src;
1864           return true;
1865         }
1866     }
1867     // destination does not yet exist; create it
1868     return createRoute(src, dest);
1869 }
1870
1871
1872 bool
1873 EAP::RouterConfig::removeRoute(unsigned char src, unsigned char dest) {
1874     debugOutput(DEBUG_LEVEL_VERBOSE,"RouterConfig::removeRoute( 0x%02x, 0x%02x )\n", src, dest);
1875     for (RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it) {
1876         if (it->first == dest) {
1877           if (it->second != src) {
1878             return false;
1879           }
1880           m_routes2.erase(it);
1881           return true;
1882         }
1883     }
1884     return false;
1885 }
1886
1887 bool
1888 EAP::RouterConfig::muteRoute(unsigned char dest) {
1889     for (RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it) {
1890         if (it->first == dest) {
1891           it->second = m_eap.getSMuteId();
1892           return true;
1893         }
1894     }
1895     return false;
1896 }
1897
1898 bool
1899 EAP::RouterConfig::removeRoute(unsigned char dest) {
1900     debugOutput(DEBUG_LEVEL_VERBOSE,"RouterConfig::removeRoute( 0x%02x )\n", dest);
1901     for (RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it) {
1902         if (it->first == dest) {
1903           m_routes2.erase(it);
1904           return true;
1905         }
1906     }
1907     return false;
1908 }
1909
1910 unsigned char
1911 EAP::RouterConfig::getSourceForDestination(unsigned char dest) {
1912     for (RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it) {
1913         if (it->first == dest) {
1914           return it->second;
1915         }
1916     }
1917     return -1;
1918 }
1919
1920 std::vector<unsigned char>
1921 EAP::RouterConfig::getDestinationsForSource(unsigned char source) {
1922     std::vector<unsigned char> ret;
1923     for (RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it) {
1924         if (it->second == source) {
1925             ret.push_back(it->first);
1926         }
1927     }
1928     return ret;
1929 }
1930
1931 void
1932 EAP::RouterConfig::show()
1933 {
1934     printMessage("%llu routes\n", (unsigned long long)m_routes2.size());
1935     for ( RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it ) {
1936         printMessage("0x%02x -> 0x%02x\n", it->second, it->first);
1937     }
1938 }
1939
1940 //
1941 // ----------- peak space -------------
1942 //
1943
1944 bool
1945 EAP::PeakSpace::read(enum eRegBase base, unsigned offset)
1946 {
1947     uint32_t nb_routes;
1948     // we have to figure out the number of entries through the currently
1949     // active router config
1950     RouterConfig *rcfg = m_eap.getActiveRouterConfig();
1951     if(rcfg == NULL) {
1952         debugError("Could not get active router config\n");
1953         return false;
1954     }
1955     nb_routes = rcfg->getNbRoutes();
1956
1957     // read the peak/route info
1958     uint32_t tmp_entries[nb_routes];
1959     if(!m_eap.readRegBlock(base, offset, tmp_entries, nb_routes*4)) {
1960         debugError("Failed to read peak block information\n");
1961         return false;
1962     }
1963     // parse the peaks into the map
1964     for (unsigned int i=0; i<nb_routes; ++i) {
1965         unsigned char dest = tmp_entries[i]&0xff;
1966         int peak = (tmp_entries[i]&0xfff0000)>>16;
1967         if (m_peaks.count(dest) == 0 || m_peaks[dest] < peak) {
1968             m_peaks[dest] = peak;
1969         }
1970     }
1971     return true;
1972 }
1973
1974
1975 void
1976 EAP::PeakSpace::show()
1977 {
1978     printMessage("  %zi peaks\n", m_peaks.size());
1979     for (std::map<unsigned char, int>::iterator it=m_peaks.begin(); it!=m_peaks.end(); ++it) {
1980         printMessage("0x%02x : %i\n", it->first, it->second);
1981     }
1982 }
1983
1984 int
1985 EAP::PeakSpace::getPeak(unsigned char dst) {
1986     int ret = m_peaks[dst];
1987     m_peaks.erase(dst);
1988     return ret;
1989 }
1990
1991 std::map<unsigned char, int>
1992 EAP::PeakSpace::getPeaks() {
1993     // Create a new empty map
1994     std::map<unsigned char, int> ret;
1995     // Swap the peak map with the new and empty one
1996     ret.swap(m_peaks);
1997     // Return the now filled map of the peaks :-)
1998     return ret;
1999 }
2000
2001 // ----------- stream config block -------------
2002 EAP::StreamConfig::StreamConfig(EAP &p, enum eRegBase b, unsigned int o)
2003 : m_eap(p)
2004 , m_base(b), m_offset(o)
2005 , m_nb_tx(0), m_nb_rx(0)
2006 , m_tx_configs(NULL), m_rx_configs(NULL)
2007 , m_debugModule(p.m_debugModule)
2008 {
2009
2010 }
2011
2012 EAP::StreamConfig::~StreamConfig()
2013 {
2014     if(m_tx_configs) delete[]m_tx_configs;
2015     if(m_rx_configs) delete[]m_rx_configs;
2016 }
2017
2018 bool
2019 EAP::StreamConfig::read(enum eRegBase base, unsigned offset)
2020 {
2021     if(!m_eap.readRegBlock(base, offset, &m_nb_tx, 4)) {
2022         debugError("Failed to read number of tx entries\n");
2023         return false;
2024     }
2025     if(!m_eap.readRegBlock(base, offset+4, &m_nb_rx, 4)) {
2026         debugError("Failed to read number of rx entries\n");
2027         return false;
2028     }
2029     debugOutput(DEBUG_LEVEL_VERBOSE, " Entries: TX: %u, RX: %u\n", m_nb_tx, m_nb_rx);
2030
2031     if(m_tx_configs) {
2032         delete[]m_tx_configs;
2033         m_tx_configs = NULL;
2034     }
2035     if(m_rx_configs) {
2036         delete[]m_rx_configs;
2037         m_rx_configs = NULL;
2038     }
2039    
2040     offset += 8;
2041     if(m_nb_tx > 0) {
2042         m_tx_configs = new struct ConfigBlock[m_nb_tx];
2043         for(unsigned int i=0; i<m_nb_tx; i++) {
2044             fb_quadlet_t *ptr = reinterpret_cast<fb_quadlet_t *>(&(m_tx_configs[i]));
2045             if(!m_eap.readRegBlock(base, offset, ptr, sizeof(struct ConfigBlock))) {
2046                 debugError("Failed to read tx entry %d\n", i);
2047                 return false;
2048             }
2049             offset += sizeof(struct ConfigBlock);
2050         }
2051     }
2052
2053     if(m_nb_rx > 0) {
2054         m_rx_configs = new struct ConfigBlock[m_nb_rx];
2055         for(unsigned int i=0; i<m_nb_rx; i++) {
2056             fb_quadlet_t *ptr = reinterpret_cast<fb_quadlet_t *>(&(m_rx_configs[i]));
2057             if(!m_eap.readRegBlock(base, offset, ptr, sizeof(struct ConfigBlock))) {
2058                 debugError("Failed to read rx entry %d\n", i);
2059                 return false;
2060             }
2061             offset += sizeof(struct ConfigBlock);
2062         }
2063     }
2064     return true;
2065 }
2066
2067 bool
2068 EAP::StreamConfig::write(enum eRegBase base, unsigned offset)
2069 {
2070     if(!m_eap.writeRegBlock(base, offset, &m_nb_tx, 4)) {
2071         debugError("Failed to write number of tx entries\n");
2072         return false;
2073     }
2074     if(!m_eap.writeRegBlock(base, offset+4, &m_nb_rx, 4)) {
2075         debugError("Failed to write number of rx entries\n");
2076         return false;
2077     }
2078
2079     offset += 8;
2080     for(unsigned int i=0; i<m_nb_tx; i++) {
2081         fb_quadlet_t *ptr = reinterpret_cast<fb_quadlet_t *>(&(m_tx_configs[i]));
2082         if(!m_eap.writeRegBlock(base, offset, ptr, sizeof(struct ConfigBlock))) {
2083             debugError("Failed to write tx entry %d\n", i);
2084             return false;
2085         }
2086         offset += sizeof(struct ConfigBlock);
2087     }
2088
2089     for(unsigned int i=0; i<m_nb_rx; i++) {
2090         fb_quadlet_t *ptr = reinterpret_cast<fb_quadlet_t *>(&(m_rx_configs[i]));
2091         if(!m_eap.writeRegBlock(base, offset, ptr, sizeof(struct ConfigBlock))) {
2092             debugError("Failed to write rx entry %d\n", i);
2093             return false;
2094         }
2095         offset += sizeof(struct ConfigBlock);
2096     }
2097     return true;
2098 }
2099
2100 stringlist
2101 EAP::StreamConfig::getNamesForBlock(struct ConfigBlock &b)
2102 {
2103     stringlist names;
2104     char namestring[DICE_EAP_CHANNEL_CONFIG_NAMESTR_LEN_BYTES+1];
2105
2106     memcpy(namestring, b.names, DICE_EAP_CHANNEL_CONFIG_NAMESTR_LEN_BYTES);
2107
2108     // Strings from the device are always little-endian,
2109     // so byteswap for big-endian machines
2110     #if __BYTE_ORDER == __BIG_ENDIAN
2111     byteSwapBlock((quadlet_t *)namestring, DICE_EAP_CHANNEL_CONFIG_NAMESTR_LEN_QUADS);
2112     #endif
2113
2114     namestring[DICE_EAP_CHANNEL_CONFIG_NAMESTR_LEN_BYTES]='\0';
2115     return m_eap.m_device.splitNameString(std::string(namestring));
2116 }
2117
2118 stringlist
2119 EAP::StreamConfig::getTxNamesString(unsigned int i)
2120 {
2121     return getNamesForBlock(m_tx_configs[i]);
2122 }
2123
2124 stringlist
2125 EAP::StreamConfig::getRxNamesString(unsigned int i)
2126 {
2127     return getNamesForBlock(m_rx_configs[i]);
2128 }
2129
2130 void
2131 EAP::StreamConfig::showConfigBlock(struct ConfigBlock &b)
2132 {
2133     printMessage(" Channel count : %u audio, %u midi\n", b.nb_audio, b.nb_midi);
2134     printMessage(" AC3 Map       : 0x%08X\n", b.ac3_map);
2135     stringlist channel_names  = getNamesForBlock(b);
2136     printMessage("  Channel names :\n");
2137     for ( stringlist::iterator it = channel_names.begin();
2138         it != channel_names.end();
2139         ++it )
2140     {
2141         printMessage("     %s\n", (*it).c_str());
2142     }
2143 }
2144
2145 void
2146 EAP::StreamConfig::show()
2147 {
2148     for(unsigned int i=0; i<m_nb_tx; i++) {
2149         printMessage("TX Config block %d\n", i);
2150         showConfigBlock(m_tx_configs[i]);
2151     }
2152     for(unsigned int i=0; i<m_nb_rx; i++) {
2153         printMessage("RX Config block %d\n", i);
2154         showConfigBlock(m_rx_configs[i]);
2155     }
2156 }
2157
2158 } // namespace Dice
2159
2160
2161 // vim: et
Note: See TracBrowser for help on using the browser.