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

Revision 2049, 54.7 kB (checked in by adi, 11 years ago)

DICE: generic speed-dependent EAP routing prototypes

Patch by Philippe Carriere

Routing depends on the sample rate, so Philippe has introduced three
different setup functions for single/double/quad speed.

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