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

Revision 2140, 66.4 kB (checked in by jwoithe, 12 years ago)

dice: default router setup patch 7/10 from Philippe Carriere: DICE EAP Correct the default router config of second audio port. Generic device.

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             // 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 #if 0
1228     // figure out the number of i/o's
1229     int nb_inputs = m_eap.m_mixer_nb_tx;
1230     int nb_outputs = m_eap.m_mixer_nb_rx;
1231
1232     // clear the previous map
1233     m_input_route_map.clear();
1234     m_output_route_map.clear();
1235
1236     // find the active router configuration
1237     RouterConfig * rcfg = m_eap.getActiveRouterConfig();
1238     if(rcfg == NULL) {
1239         debugError("Could not get active routing info\n");
1240         return;
1241     }
1242
1243     // find the inputs
1244     for(int i=0; i < nb_inputs; i++) {
1245         int ch = i;
1246         // the destination id of the mixer input
1247         int dest_int = m_eap.m_mixer_tx_id;
1248
1249         // from the DICE mixer spec:
1250         // we can have 16 channels per "block"
1251         // if there are more, consecutive block id's are assumed
1252         while(ch > 15) {
1253             ch -= 16;
1254             dest_int += 1;
1255         }
1256         // the destination block and channel corresponding with this
1257         // mixer input is now known
1258         enum eRouteDestination dest = rcfg->intToRouteDestination(dest_int);
1259
1260         // get the source for this mixer channel
1261         m_input_route_map[i] = rcfg->getRouteForDestination(dest, ch);
1262
1263         debugOutput(DEBUG_LEVEL_VERBOSE, "Mixer input channel %2d source: %s (%d)\n", i,
1264                                           srcBlockToString(m_input_route_map[i].src),
1265                                           m_input_route_map[i].srcChannel);
1266     }
1267
1268     // find where the outputs are connected to
1269     for(int i=0; i < nb_outputs; i++) {
1270         int ch = i;
1271         // the source id of the mixer input
1272         int src_int = m_eap.m_mixer_rx_id;
1273
1274         // from the DICE mixer spec:
1275         // we can have 16 channels per "block"
1276         // if there are more, consecutive block id's are assumed
1277         while(ch > 15) {
1278             ch -= 16;
1279             src_int += 1;
1280         }
1281
1282         // the source block and channel corresponding with this
1283         // mixer output is now known
1284         enum eRouteSource src = rcfg->intToRouteSource(src_int);
1285
1286         // get the routing destinations for this mixer channel
1287         m_output_route_map[i] = rcfg->getRoutesForSource(src, ch);
1288
1289         #ifdef DEBUG
1290         std::string destinations;
1291         for ( RouterConfig::RouteVectorIterator it = m_output_route_map[i].begin();
1292             it != m_output_route_map[i].end();
1293             ++it )
1294         {
1295             RouterConfig::Route r = *it;
1296             // check whether the destination is valid
1297             if((r.dst != eRD_Invalid) && (r.dstChannel >= 0)) {
1298                 char tmp[128];
1299                 snprintf(tmp, 128, "%s:%d,", dstBlockToString(r.dst), r.dstChannel);
1300                 destinations += tmp;
1301             }
1302         }
1303         debugOutput(DEBUG_LEVEL_VERBOSE, "Mixer output channel %2d destinations: %s\n", i, destinations.c_str());
1304         #endif
1305     }
1306 #endif
1307 }
1308
1309 void
1310 EAP::Mixer::show()
1311 {
1312     int nb_inputs = m_eap.m_mixer_nb_tx;
1313     int nb_outputs = m_eap.m_mixer_nb_rx;
1314
1315     updateNameCache();
1316
1317     const size_t bufflen = 4096;
1318     char tmp[bufflen];
1319     int cnt;
1320
1321     //
1322     // Caution the user that, displaying as further, because inputs index varies first
1323     //  inputs will appears as columns at the opposite of the "ffado convention"
1324     printMessage("   -- inputs index -->>\n");
1325     cnt = 0;
1326     for(int j=0; j < nb_inputs; j++) {
1327         cnt += snprintf(tmp+cnt, bufflen-cnt, "   %02d   ", j);
1328     }
1329     printMessage("%s\n", tmp);
1330
1331     cnt = 0;
1332     for(int j=0; j < nb_inputs; j++) {
1333         cnt += snprintf(tmp+cnt, bufflen-cnt, "%s ", getRowName(j).data());
1334     }
1335     printMessage("%s\n", tmp);
1336
1337     // display coefficients
1338     for(int i=0; i < nb_outputs; i++) {
1339         cnt = 0;
1340         for(int j=0; j < nb_inputs; j++) {
1341             cnt += snprintf(tmp+cnt, bufflen-cnt, "%07d ", *(m_coeff + nb_inputs * i + j));
1342         }
1343  
1344         // Display destinations name
1345         cnt += snprintf(tmp+cnt, bufflen-cnt, "=[%02d]=> %s", i, getColName(i).data());
1346         printMessage("%s\n", tmp);
1347     }
1348
1349 }
1350
1351 int
1352 EAP::Mixer::canWrite( const int row, const int col)
1353 {
1354     if(m_eap.m_mixer_readonly) {
1355         return false;
1356     }
1357     return (row >= 0 && row < m_eap.m_mixer_nb_tx && col >= 0 && col < m_eap.m_mixer_nb_rx);
1358 }
1359
1360 double
1361 EAP::Mixer::setValue( const int row, const int col, const double val)
1362 {
1363     if(m_eap.m_mixer_readonly) {
1364         debugWarning("Mixer is read-only\n");
1365         return false;
1366     }
1367     int nb_inputs = m_eap.m_mixer_nb_tx;
1368     int addr = ((nb_inputs * col) + row) * 4;
1369     quadlet_t tmp = (quadlet_t) val;
1370     if(!m_eap.writeRegBlock(eRT_Mixer, 4+addr, &tmp, 4)) {
1371         debugError("Failed to write coefficient\n");
1372         return 0;
1373     }
1374     return (double)(tmp);
1375 }
1376
1377 double
1378 EAP::Mixer::getValue( const int row, const int col)
1379 {
1380     int nb_inputs = m_eap.m_mixer_nb_tx;
1381     int addr = ((nb_inputs * col) + row) * 4;
1382     quadlet_t tmp;
1383     if(!m_eap.readRegBlock(eRT_Mixer, 4+addr, &tmp, 4)) {
1384         debugError("Failed to read coefficient\n");
1385         return 0;
1386     }
1387     return (double)(tmp);
1388 }
1389
1390 int
1391 EAP::Mixer::getRowCount()
1392 {
1393     return m_eap.m_mixer_nb_tx;
1394 }
1395
1396 int
1397 EAP::Mixer::getColCount()
1398 {
1399     return m_eap.m_mixer_nb_rx;
1400 }
1401
1402 // full map updates are unsupported
1403 bool
1404 EAP::Mixer::getCoefficientMap(int &) {
1405     return false;
1406 }
1407
1408 bool
1409 EAP::Mixer::storeCoefficientMap(int &) {
1410     if(m_eap.m_mixer_readonly) {
1411         debugWarning("Mixer is read-only\n");
1412         return false;
1413     }
1414     return false;
1415 }
1416
1417 // Names
1418 std::string
1419 EAP::Mixer::getRowName(const int row) {
1420     std::string mixer_src;
1421     if (row < 0 || row > m_eap.m_mixer_nb_tx) return "Invalid";
1422     unsigned int dstid = (eRD_Mixer0<<4) + row; // Mixer has consecutive ID's
1423     debugOutput(DEBUG_LEVEL_VERBOSE, "EAP::Mixer::getRowName( %d ): ID's %d\n", row, dstid);
1424     if (m_eap.m_router){
1425       std::string mixer_dst = m_eap.m_router->getDestinationName(dstid);
1426       mixer_src = m_eap.m_router->getSourceForDestination(mixer_dst);
1427       debugOutput(DEBUG_LEVEL_VERBOSE, "EAP::Mixer::found %s as source for %s\n", mixer_src.data(),
1428                   mixer_dst.data());
1429     }
1430     else {
1431       char tmp[32];
1432       snprintf(tmp, 32, "MixIn:%d", row);
1433       mixer_src = tmp;
1434     }
1435
1436     return mixer_src;
1437 }
1438
1439 std::string
1440 EAP::Mixer::getColName(const int col) {
1441     std::string mixer_dst;
1442     stringlist dest_names;
1443    
1444     // invalid col index
1445     if (col < 0 || col > m_eap.m_mixer_nb_rx) {
1446       mixer_dst.append("Invalid");
1447       return mixer_dst;
1448     }
1449    
1450     unsigned int srcid = (eRS_Mixer<<4) + col; // Mixer has consecutive ID's
1451     debugOutput(DEBUG_LEVEL_VERBOSE, "EAP::Mixer::getColName( %d ): ID's %d\n", col, srcid);
1452     if (m_eap.m_router){
1453       std::string mixer_src = m_eap.m_router->getSourceName(srcid);
1454       dest_names = m_eap.m_router->getDestinationsForSource(mixer_src);
1455       if (dest_names.size() > 0) {
1456         stringlist::iterator it_d = dest_names.begin();
1457         stringlist::iterator it_d_end_m1 = dest_names.end(); --it_d_end_m1;
1458         while (it_d != it_d_end_m1) {
1459           mixer_dst.append((*it_d).c_str()); mixer_dst.append(";\n");
1460           it_d++;
1461         }
1462         mixer_dst.append((*it_d).c_str());
1463       }
1464     } else {
1465       char tmp[32];
1466       snprintf(tmp, 32, "MixOut:%d", col);
1467       mixer_dst.append(tmp);
1468     }
1469
1470     return mixer_dst;
1471 }
1472
1473 //
1474 // ----------- Router -------------
1475 //
1476
1477 EAP::Router::Router(EAP &p)
1478 : Control::CrossbarRouter(&p.m_device, "Router")
1479 , m_eap(p)
1480 , m_peak( *(new PeakSpace(p)) )
1481 , m_debugModule(p.m_debugModule)
1482 {
1483 }
1484
1485 EAP::Router::~Router()
1486 {
1487     delete &m_peak;
1488 }
1489
1490 void
1491 EAP::Router::update()
1492 {
1493     // Clear and
1494     //   define new sources and destinations for the router
1495     m_sources.clear();
1496     m_eap.setupSources();
1497     m_destinations.clear();
1498     m_eap.setupDestinations();
1499     return;
1500 }
1501
1502 void
1503 EAP::Router::addSource(const std::string& basename, enum eRouteSource srcid,
1504                        unsigned int base, unsigned int cnt, unsigned int offset)
1505 {
1506     std::string name = basename + ":";
1507     char tmp[4];
1508     for (unsigned int i=0; i<cnt; i++) {
1509         snprintf(tmp, 4, "%02d", offset+i);
1510         m_sources[name+tmp] = (srcid<<4) + base+i;
1511     }
1512 }
1513
1514 void
1515 EAP::Router::addDestination(const std::string& basename, enum eRouteDestination dstid,
1516                             unsigned int base, unsigned int cnt, unsigned int offset)
1517 {
1518     std::string name = basename + ":";
1519     char tmp[4];
1520     for (unsigned int i=0; i<cnt; i++) {
1521         snprintf(tmp, 4, "%02d", offset+i);
1522         m_destinations[name+tmp] = (dstid<<4) + base+i;
1523     }
1524 }
1525
1526 std::string
1527 EAP::Router::getSourceName(const int srcid)
1528 {
1529     for (std::map<std::string, int>::iterator it=m_sources.begin(); it!=m_sources.end(); ++it) {
1530         if (it->second == srcid) {
1531             return it->first;
1532         }
1533     }
1534     return "";
1535 }
1536
1537 std::string
1538 EAP::Router::getDestinationName(const int dstid)
1539 {
1540     for (std::map<std::string, int>::iterator it=m_destinations.begin(); it!=m_destinations.end(); ++it) {
1541         if (it->second == dstid) {
1542             return it->first;
1543         }
1544     }
1545     return "";
1546 }
1547
1548 int
1549 EAP::Router::getSourceIndex(std::string name)
1550 {
1551     if (m_sources.count(name) < 1)
1552         return -1;
1553     return m_sources[name];
1554 }
1555
1556 int
1557 EAP::Router::getDestinationIndex(std::string name)
1558 {
1559     if (m_destinations.count(name) < 1)
1560         return -1;
1561     return m_destinations[name];
1562 }
1563
1564 stringlist
1565 EAP::Router::getSourceNames()
1566 {
1567     stringlist n;
1568
1569     for (std::map<std::string, int>::iterator it=m_sources.begin(); it!=m_sources.end(); ++it)
1570         n.push_back(it->first);
1571     return n;
1572 }
1573
1574 stringlist
1575 EAP::Router::getDestinationNames()
1576 {
1577     stringlist n;
1578     for (std::map<std::string, int>::iterator it=m_destinations.begin(); it!=m_destinations.end(); ++it)
1579         n.push_back(it->first);
1580     return n;
1581 }
1582
1583 stringlist
1584 EAP::Router::getDestinationsForSource(const std::string& srcname) {
1585     RouterConfig* rcfg = m_eap.getActiveRouterConfig();
1586     if(rcfg == NULL) {
1587         debugError("Could not request active router configuration\n");
1588         return stringlist();
1589     }
1590     stringlist ret;
1591     std::vector<unsigned char> dests = rcfg->getDestinationsForSource(m_sources[srcname]);
1592     std::string name;
1593     for (unsigned int i=0; i<dests.size(); ++i) {
1594         if ((name = getDestinationName(dests[i])) != "") {
1595           ret.push_back(name);
1596         }
1597     }
1598     return ret;
1599 }
1600 std::string
1601 EAP::Router::getSourceForDestination(const std::string& dstname) {
1602     RouterConfig* rcfg = m_eap.getActiveRouterConfig();
1603     if(rcfg == NULL) {
1604         debugError("Could not request active router configuration\n");
1605         return "";
1606     }
1607     int source = rcfg->getSourceForDestination(m_destinations[dstname]);
1608     return getSourceName(source);
1609 }
1610
1611
1612 bool
1613 EAP::Router::canConnect(const int source, const int dest)
1614 {
1615     debugWarning("TODO: Implement canConnect(0x%02x, 0x%02x)\n", source, dest);
1616
1617     // we can connect anything
1618     // FIXME: can we?
1619     return true;
1620 }
1621
1622 bool
1623 EAP::Router::setConnectionState(const int source, const int dest, const bool enable)
1624 {
1625     debugOutput(DEBUG_LEVEL_VERBOSE,"Router::setConnectionState(0x%02x -> 0x%02x ? %i)\n", source, dest, enable);
1626     // get the routing configuration
1627     RouterConfig *rcfg = m_eap.getActiveRouterConfig();
1628     if(rcfg == NULL) {
1629         debugError("Could not request active router configuration\n");
1630         return false;
1631     }
1632
1633     bool ret = false;
1634     if (enable) {
1635         ret = rcfg->setupRoute(source, dest);
1636     } else {
1637         ret = rcfg->muteRoute(dest);
1638         // FixMe:
1639         //   Not all devices are intended to work correctly with a variable number of router entries
1640         //     so muting the destination is preferable
1641         //   Now, this might be useful for some other ones, but is it the right place for this ?
1642         //   ret = rcfg->removeRoute(source, dest);
1643     }
1644     m_eap.updateCurrentRouterConfig(*rcfg);
1645     return ret;
1646 }
1647
1648 bool
1649 EAP::Router::getConnectionState(const int source, const int dest)
1650 {
1651     // get the routing configuration
1652     RouterConfig *rcfg = m_eap.getActiveRouterConfig();
1653     if(rcfg == NULL) {
1654         debugError("Could not request active router configuration\n");
1655         return false;
1656     }
1657     if (rcfg->getSourceForDestination(dest) == source) {
1658         return true;
1659     }
1660     return false;
1661 }
1662
1663 bool
1664 EAP::Router::canConnect(const std::string& src, const std::string& dst)
1665 {
1666     int srcidx = getSourceIndex(src);
1667     int dstidx = getDestinationIndex(dst);
1668     return canConnect(srcidx, dstidx);
1669 }
1670
1671 bool
1672 EAP::Router::setConnectionState(const std::string& src, const std::string& dst, const bool enable)
1673 {
1674     int srcidx = getSourceIndex(src);
1675     int dstidx = getDestinationIndex(dst);
1676     return setConnectionState(srcidx, dstidx, enable);
1677 }
1678
1679 bool
1680 EAP::Router::getConnectionState(const std::string& src, const std::string& dst)
1681 {
1682     int srcidx = getSourceIndex(src);
1683     int dstidx = getDestinationIndex(dst);
1684     return getConnectionState(srcidx, dstidx);
1685 }
1686
1687
1688 bool
1689 EAP::Router::clearAllConnections()
1690 {
1691     // build a new empty routing configuration
1692     RouterConfig newcfg = EAP::RouterConfig(m_eap);
1693
1694     // upload the new router config
1695     if(!m_eap.updateCurrentRouterConfig(newcfg)) {
1696         debugError("Could not update router config\n");
1697         return false;
1698     }
1699     return true;
1700 }
1701
1702 bool
1703 EAP::Router::hasPeakMetering()
1704 {
1705     return m_eap.m_router_exposed;
1706 }
1707
1708 double
1709 EAP::Router::getPeakValue(const std::string& dest)
1710 {
1711     m_peak.read();
1712     unsigned char dst = m_destinations[dest];
1713     return m_peak.getPeak(dst);
1714 }
1715
1716 std::map<std::string, double>
1717 EAP::Router::getPeakValues()
1718 {
1719     m_peak.read();
1720     std::map<std::string, double> ret;
1721     std::map<unsigned char, int> peaks = m_peak.getPeaks();
1722     for (std::map<unsigned char, int>::iterator it=peaks.begin(); it!=peaks.end(); ++it) {
1723         ret[getDestinationName(it->first)] = it->second;
1724     }
1725     return ret;
1726 }
1727
1728 void
1729 EAP::Router::show()
1730 {
1731     // print the peak space as it also contains the routing configuration
1732     printMessage("Router sources:\n");
1733     printMessage(" %llu sources:\n", (unsigned long long)m_sources.size());
1734     for ( std::map<std::string, int>::iterator it=m_sources.begin(); it!=m_sources.end(); ++it ) {
1735         printMessage(" 0x%02x : %s\n", (*it).second, (*it).first.c_str());
1736     }
1737     printMessage("Router destinations:\n");
1738     printMessage(" %llu destinations:\n", (unsigned long long)m_destinations.size());
1739     for ( std::map<std::string, int>::iterator it=m_destinations.begin(); it!=m_destinations.end(); ++it ) {
1740         printMessage(" 0x%02x : %s\n", (*it).second, (*it).first.c_str());
1741     }
1742     printMessage("Router connections:\n");
1743     stringlist sources = getSourceNames();
1744     stringlist destinations = getDestinationNames();
1745     for (stringlist::iterator it1=sources.begin(); it1!=sources.end(); ++it1) {
1746         for (stringlist::iterator it2=destinations.begin(); it2!=destinations.end(); ++it2) {
1747             if (getConnectionState(*it1, *it2)) {
1748                 printMessage(" %s -> %s\n", it1->c_str(), it2->c_str());
1749             }
1750         }
1751     }
1752     printMessage("Active router config:\n");
1753     m_eap.getActiveRouterConfig()->show();
1754     printMessage("Active peak config:\n");
1755     m_peak.read();
1756     m_peak.show();
1757 }
1758
1759 // ----------- routing config -------------
1760 EAP::RouterConfig::RouterConfig(EAP &p)
1761 : m_eap(p)
1762 , m_base(eRT_None), m_offset(0)
1763 , m_debugModule(p.m_debugModule)
1764 {}
1765
1766 EAP::RouterConfig::RouterConfig(EAP &p, enum eRegBase b, unsigned int o)
1767 : m_eap(p)
1768 , m_base(b), m_offset(o)
1769 , m_debugModule(p.m_debugModule)
1770 {}
1771
1772 EAP::RouterConfig::~RouterConfig()
1773 {}
1774
1775 bool
1776 EAP::RouterConfig::read(enum eRegBase base, unsigned offset)
1777 {
1778     // first clear the current route vector
1779     clearRoutes();
1780
1781     uint32_t nb_routes;
1782     if(!m_eap.readRegBlock(base, offset, &nb_routes, 4)) {
1783         debugError("Failed to read number of entries\n");
1784         return false;
1785     }
1786     if(nb_routes == 0) {
1787         debugWarning("No routes found. Base 0x%x, offset 0x%x\n", base, offset);
1788     }
1789
1790     // read the route info
1791     uint32_t tmp_entries[nb_routes];
1792     if(!m_eap.readRegBlock(base, offset+4, tmp_entries, nb_routes*4)) {
1793         debugError("Failed to read router config block information\n");
1794         return false;
1795     }
1796
1797     // decode into the routing map
1798     for(unsigned int i=0; i < nb_routes; i++) {
1799         m_routes2.push_back(std::make_pair(tmp_entries[i]&0xff, (tmp_entries[i]>>8)&0xff));
1800     }
1801     return true;
1802 }
1803
1804 bool
1805 EAP::RouterConfig::write(enum eRegBase base, unsigned offset)
1806 {
1807     uint32_t nb_routes = m_routes2.size();
1808     if(nb_routes == 0) {
1809         debugWarning("Writing 0 routes? This will deactivate routing and make the device very silent...\n");
1810     }
1811     if (nb_routes > 128) {
1812         debugError("More then 128 are not possible, only the first 128 routes will get saved!\n");
1813         nb_routes = 128;
1814     }
1815     uint32_t tmp_entries[nb_routes];
1816
1817     // encode from the routing vector
1818     int i=0;
1819     for (RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it) {
1820         tmp_entries[i] = ((it->second<<8) + it->first)&0xffff;
1821         ++i;
1822     }
1823
1824     unsigned int nb_routes_max = m_eap.getMaxNbRouterEntries();
1825     uint32_t zeros[nb_routes_max+1];
1826
1827     for (unsigned int i=0; i<nb_routes_max+1; ++i) zeros[i] = 0;
1828     if(!m_eap.writeRegBlock(base, offset, zeros, (nb_routes_max+1)*4)) {
1829         debugError("Failed to write zeros to router config block\n");
1830         return false;
1831     }
1832
1833     // write the result to the device
1834     if(!m_eap.writeRegBlock(base, offset+4, tmp_entries, nb_routes*4)) {
1835         debugError("Failed to write router config block information\n");
1836         return false;
1837     }
1838     if(!m_eap.writeRegBlock(base, offset, &nb_routes, 4)) {
1839         debugError("Failed to write number of entries\n");
1840         return false;
1841     }
1842     return true;
1843 }
1844
1845 // Clear the route vector;
1846 bool
1847 EAP::RouterConfig::clearRoutes() {
1848     m_routes2.clear();
1849     return true;
1850 }
1851
1852 // Create a destination with a prescribed source
1853 bool
1854 EAP::RouterConfig::createRoute(unsigned char src, unsigned char dest) {
1855     debugOutput(DEBUG_LEVEL_VERBOSE,"RouterConfig::createRoute( 0x%02x, 0x%02x )\n", src, dest);
1856     m_routes2.push_back(std::make_pair(dest, src));
1857     return true;
1858 }
1859
1860 bool
1861 EAP::RouterConfig::setupRoute(unsigned char src, unsigned char dest) {
1862     debugOutput(DEBUG_LEVEL_VERBOSE,"RouterConfig::setupRoute( 0x%02x, 0x%02x )\n", src, dest);
1863     // modify exisiting routing
1864     for (RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it) {
1865         if (it->first == dest) {
1866           it->second = src;
1867           return true;
1868         }
1869     }
1870     // destination does not yet exist; create it
1871     return createRoute(src, dest);
1872 }
1873
1874
1875 bool
1876 EAP::RouterConfig::removeRoute(unsigned char src, unsigned char dest) {
1877     debugOutput(DEBUG_LEVEL_VERBOSE,"RouterConfig::removeRoute( 0x%02x, 0x%02x )\n", src, dest);
1878     for (RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it) {
1879         if (it->first == dest) {
1880           if (it->second != src) {
1881             return false;
1882           }
1883           m_routes2.erase(it);
1884           return true;
1885         }
1886     }
1887     return false;
1888 }
1889
1890 bool
1891 EAP::RouterConfig::muteRoute(unsigned char dest) {
1892     for (RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it) {
1893         if (it->first == dest) {
1894           it->second = m_eap.getSMuteId();
1895           return true;
1896         }
1897     }
1898     return false;
1899 }
1900
1901 bool
1902 EAP::RouterConfig::removeRoute(unsigned char dest) {
1903     debugOutput(DEBUG_LEVEL_VERBOSE,"RouterConfig::removeRoute( 0x%02x )\n", dest);
1904     for (RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it) {
1905         if (it->first == dest) {
1906           m_routes2.erase(it);
1907           return true;
1908         }
1909     }
1910     return false;
1911 }
1912
1913 unsigned char
1914 EAP::RouterConfig::getSourceForDestination(unsigned char dest) {
1915     for (RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it) {
1916         if (it->first == dest) {
1917           return it->second;
1918         }
1919     }
1920     return -1;
1921 }
1922
1923 std::vector<unsigned char>
1924 EAP::RouterConfig::getDestinationsForSource(unsigned char source) {
1925     std::vector<unsigned char> ret;
1926     for (RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it) {
1927         if (it->second == source) {
1928             ret.push_back(it->first);
1929         }
1930     }
1931     return ret;
1932 }
1933
1934 void
1935 EAP::RouterConfig::show()
1936 {
1937     printMessage("%llu routes\n", (unsigned long long)m_routes2.size());
1938     for ( RouteVectorV2::iterator it=m_routes2.begin(); it!=m_routes2.end(); ++it ) {
1939         printMessage("0x%02x -> 0x%02x\n", it->second, it->first);
1940     }
1941 }
1942
1943 //
1944 // ----------- peak space -------------
1945 //
1946
1947 bool
1948 EAP::PeakSpace::read(enum eRegBase base, unsigned offset)
1949 {
1950     uint32_t nb_routes;
1951     // we have to figure out the number of entries through the currently
1952     // active router config
1953     RouterConfig *rcfg = m_eap.getActiveRouterConfig();
1954     if(rcfg == NULL) {
1955         debugError("Could not get active router config\n");
1956         return false;
1957     }
1958     nb_routes = rcfg->getNbRoutes();
1959
1960     // read the peak/route info
1961     uint32_t tmp_entries[nb_routes];
1962     if(!m_eap.readRegBlock(base, offset, tmp_entries, nb_routes*4)) {
1963         debugError("Failed to read peak block information\n");
1964         return false;
1965     }
1966     // parse the peaks into the map
1967     for (unsigned int i=0; i<nb_routes; ++i) {
1968         unsigned char dest = tmp_entries[i]&0xff;
1969         int peak = (tmp_entries[i]&0xfff0000)>>16;
1970         if (m_peaks.count(dest) == 0 || m_peaks[dest] < peak) {
1971             m_peaks[dest] = peak;
1972         }
1973     }
1974     return true;
1975 }
1976
1977
1978 void
1979 EAP::PeakSpace::show()
1980 {
1981     printMessage("  %zi peaks\n", m_peaks.size());
1982     for (std::map<unsigned char, int>::iterator it=m_peaks.begin(); it!=m_peaks.end(); ++it) {
1983         printMessage("0x%02x : %i\n", it->first, it->second);
1984     }
1985 }
1986
1987 int
1988 EAP::PeakSpace::getPeak(unsigned char dst) {
1989     int ret = m_peaks[dst];
1990     m_peaks.erase(dst);
1991     return ret;
1992 }
1993
1994 std::map<unsigned char, int>
1995 EAP::PeakSpace::getPeaks() {
1996     // Create a new empty map
1997     std::map<unsigned char, int> ret;
1998     // Swap the peak map with the new and empty one
1999     ret.swap(m_peaks);
2000     // Return the now filled map of the peaks :-)
2001     return ret;
2002 }
2003
2004 // ----------- stream config block -------------
2005 EAP::StreamConfig::StreamConfig(EAP &p, enum eRegBase b, unsigned int o)
2006 : m_eap(p)
2007 , m_base(b), m_offset(o)
2008 , m_nb_tx(0), m_nb_rx(0)
2009 , m_tx_configs(NULL), m_rx_configs(NULL)
2010 , m_debugModule(p.m_debugModule)
2011 {
2012
2013 }
2014
2015 EAP::StreamConfig::~StreamConfig()
2016 {
2017     if(m_tx_configs) delete[]m_tx_configs;
2018     if(m_rx_configs) delete[]m_rx_configs;
2019 }
2020
2021 bool
2022 EAP::StreamConfig::read(enum eRegBase base, unsigned offset)
2023 {
2024     if(!m_eap.readRegBlock(base, offset, &m_nb_tx, 4)) {
2025         debugError("Failed to read number of tx entries\n");
2026         return false;
2027     }
2028     if(!m_eap.readRegBlock(base, offset+4, &m_nb_rx, 4)) {
2029         debugError("Failed to read number of rx entries\n");
2030         return false;
2031     }
2032     debugOutput(DEBUG_LEVEL_VERBOSE, " Entries: TX: %u, RX: %u\n", m_nb_tx, m_nb_rx);
2033
2034     if(m_tx_configs) {
2035         delete[]m_tx_configs;
2036         m_tx_configs = NULL;
2037     }
2038     if(m_rx_configs) {
2039         delete[]m_rx_configs;
2040         m_rx_configs = NULL;
2041     }
2042    
2043     offset += 8;
2044     if(m_nb_tx > 0) {
2045         m_tx_configs = new struct ConfigBlock[m_nb_tx];
2046         for(unsigned int i=0; i<m_nb_tx; i++) {
2047             fb_quadlet_t *ptr = reinterpret_cast<fb_quadlet_t *>(&(m_tx_configs[i]));
2048             if(!m_eap.readRegBlock(base, offset, ptr, sizeof(struct ConfigBlock))) {
2049                 debugError("Failed to read tx entry %d\n", i);
2050                 return false;
2051             }
2052             offset += sizeof(struct ConfigBlock);
2053         }
2054     }
2055
2056     if(m_nb_rx > 0) {
2057         m_rx_configs = new struct ConfigBlock[m_nb_rx];
2058         for(unsigned int i=0; i<m_nb_rx; i++) {
2059             fb_quadlet_t *ptr = reinterpret_cast<fb_quadlet_t *>(&(m_rx_configs[i]));
2060             if(!m_eap.readRegBlock(base, offset, ptr, sizeof(struct ConfigBlock))) {
2061                 debugError("Failed to read rx entry %d\n", i);
2062                 return false;
2063             }
2064             offset += sizeof(struct ConfigBlock);
2065         }
2066     }
2067     return true;
2068 }
2069
2070 bool
2071 EAP::StreamConfig::write(enum eRegBase base, unsigned offset)
2072 {
2073     if(!m_eap.writeRegBlock(base, offset, &m_nb_tx, 4)) {
2074         debugError("Failed to write number of tx entries\n");
2075         return false;
2076     }
2077     if(!m_eap.writeRegBlock(base, offset+4, &m_nb_rx, 4)) {
2078         debugError("Failed to write number of rx entries\n");
2079         return false;
2080     }
2081
2082     offset += 8;
2083     for(unsigned int i=0; i<m_nb_tx; i++) {
2084         fb_quadlet_t *ptr = reinterpret_cast<fb_quadlet_t *>(&(m_tx_configs[i]));
2085         if(!m_eap.writeRegBlock(base, offset, ptr, sizeof(struct ConfigBlock))) {
2086             debugError("Failed to write tx entry %d\n", i);
2087             return false;
2088         }
2089         offset += sizeof(struct ConfigBlock);
2090     }
2091
2092     for(unsigned int i=0; i<m_nb_rx; i++) {
2093         fb_quadlet_t *ptr = reinterpret_cast<fb_quadlet_t *>(&(m_rx_configs[i]));
2094         if(!m_eap.writeRegBlock(base, offset, ptr, sizeof(struct ConfigBlock))) {
2095             debugError("Failed to write rx entry %d\n", i);
2096             return false;
2097         }
2098         offset += sizeof(struct ConfigBlock);
2099     }
2100     return true;
2101 }
2102
2103 stringlist
2104 EAP::StreamConfig::getNamesForBlock(struct ConfigBlock &b)
2105 {
2106     stringlist names;
2107     char namestring[DICE_EAP_CHANNEL_CONFIG_NAMESTR_LEN_BYTES+1];
2108
2109     memcpy(namestring, b.names, DICE_EAP_CHANNEL_CONFIG_NAMESTR_LEN_BYTES);
2110
2111     // Strings from the device are always little-endian,
2112     // so byteswap for big-endian machines
2113     #if __BYTE_ORDER == __BIG_ENDIAN
2114     byteSwapBlock((quadlet_t *)namestring, DICE_EAP_CHANNEL_CONFIG_NAMESTR_LEN_QUADS);
2115     #endif
2116
2117     namestring[DICE_EAP_CHANNEL_CONFIG_NAMESTR_LEN_BYTES]='\0';
2118     return m_eap.m_device.splitNameString(std::string(namestring));
2119 }
2120
2121 stringlist
2122 EAP::StreamConfig::getTxNamesString(unsigned int i)
2123 {
2124     return getNamesForBlock(m_tx_configs[i]);
2125 }
2126
2127 stringlist
2128 EAP::StreamConfig::getRxNamesString(unsigned int i)
2129 {
2130     return getNamesForBlock(m_rx_configs[i]);
2131 }
2132
2133 void
2134 EAP::StreamConfig::showConfigBlock(struct ConfigBlock &b)
2135 {
2136     printMessage(" Channel count : %u audio, %u midi\n", b.nb_audio, b.nb_midi);
2137     printMessage(" AC3 Map       : 0x%08X\n", b.ac3_map);
2138     stringlist channel_names  = getNamesForBlock(b);
2139     printMessage("  Channel names :\n");
2140     for ( stringlist::iterator it = channel_names.begin();
2141         it != channel_names.end();
2142         ++it )
2143     {
2144         printMessage("     %s\n", (*it).c_str());
2145     }
2146 }
2147
2148 void
2149 EAP::StreamConfig::show()
2150 {
2151     for(unsigned int i=0; i<m_nb_tx; i++) {
2152         printMessage("TX Config block %d\n", i);
2153         showConfigBlock(m_tx_configs[i]);
2154     }
2155     for(unsigned int i=0; i<m_nb_rx; i++) {
2156         printMessage("RX Config block %d\n", i);
2157         showConfigBlock(m_rx_configs[i]);
2158     }
2159 }
2160
2161 } // namespace Dice
2162
2163
2164 // vim: et
Note: See TracBrowser for help on using the browser.