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

Revision 2093, 60.6 kB (checked in by adi, 11 years ago)

DICE-EAP add (possibly) printing the full router content with test-dice-eap

By Philippe Carriere.

Additional debug information.

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