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

Revision 1779, 54.0 kB (checked in by arnonym, 11 years ago)

Rewrite the RouterConfig? and save lots of code.

Plus, it seems I fixed a "bug" where the device would output a high pitched sound when an output was disconnected. Now this seems gone...

Next stop: PeakSpace?

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