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

Revision 2123, 62.6 kB (checked in by adi, 11 years ago)

DICE EAP Use the correct value of offset in EAP::showFullRouter()

The correction allows to read the content of the current router config
hardware depending on the samplerate in use.

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