root/trunk/libffado/src/fireworks/fireworks_device.cpp

Revision 2019, 28.2 kB (checked in by jwoithe, 12 years ago)

From Orcan via trac ticket #344. Writes Orcan:

It is that time of the year that the gcc folks decide to impose the
standards at a higher level. Attached is a trivial patch that will make
libffado compile against new gcc.

Thanks for the patch Orcan.

Line 
1 /*
2  * Copyright (C) 2005-2008 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 "config.h"
25 #include "devicemanager.h"
26 #include "fireworks_device.h"
27 #include "efc/efc_avc_cmd.h"
28 #include "efc/efc_cmd.h"
29 #include "efc/efc_cmds_hardware.h"
30 #include "efc/efc_cmds_hardware_ctrl.h"
31 #include "efc/efc_cmds_flash.h"
32
33 #include "audiofire/audiofire_device.h"
34
35 #include "libieee1394/configrom.h"
36 #include "libieee1394/ieee1394service.h"
37
38 #include "fireworks/fireworks_control.h"
39
40 #include "libutil/PosixMutex.h"
41
42 #include "IntelFlashMap.h"
43
44 #define ECHO_FLASH_ERASE_TIMEOUT_MILLISECS 2000
45 #define FIREWORKS_MIN_FIRMWARE_VERSION 0x04080000
46
47 #include <sstream>
48 #include <unistd.h>
49 using namespace std;
50
51 // FireWorks is the platform used and developed by ECHO AUDIO
52 namespace FireWorks {
53
54 Device::Device(DeviceManager& d, std::auto_ptr<ConfigRom>( configRom ))
55     : GenericAVC::Device( d, configRom)
56     , m_poll_lock( new Util::PosixMutex("DEVPOLL") )
57     , m_efc_discovery_done ( false )
58     , m_MixerContainer ( NULL )
59     , m_HwInfoContainer ( NULL )
60 {
61     debugOutput( DEBUG_LEVEL_VERBOSE, "Created FireWorks::Device (NodeID %d)\n",
62                  getConfigRom().getNodeId() );
63 }
64
65 Device::~Device()
66 {
67     destroyMixer();
68 }
69
70 void
71 Device::showDevice()
72 {
73     debugOutput(DEBUG_LEVEL_VERBOSE, "This is a FireWorks::Device\n");
74     if ( !m_efc_discovery_done) {
75         if (!discoverUsingEFC()) {
76             debugError("EFC discovery failed\n");
77         }
78     }
79     m_HwInfo.showEfcCmd();
80     GenericAVC::Device::showDevice();
81 }
82
83 bool
84 Device::probe( Util::Configuration& c, ConfigRom& configRom, bool generic )
85 {
86     if(generic) {
87         // try an EFC command
88         EfcOverAVCCmd cmd( configRom.get1394Service() );
89         cmd.setCommandType( AVC::AVCCommand::eCT_Control );
90         cmd.setNodeId( configRom.getNodeId() );
91         cmd.setSubunitType( AVC::eST_Unit  );
92         cmd.setSubunitId( 0xff );
93         cmd.setVerbose( configRom.getVerboseLevel() );
94
95         EfcHardwareInfoCmd hwInfo;
96         hwInfo.setVerboseLevel(configRom.getVerboseLevel());
97         cmd.m_cmd = &hwInfo;
98
99         if ( !cmd.fire()) {
100             return false;
101         }
102
103         if ( cmd.getResponse() != AVC::AVCCommand::eR_Accepted) {
104             return false;
105         }
106         if ( hwInfo.m_header.retval != EfcCmd::eERV_Ok
107              && hwInfo.m_header.retval != EfcCmd::eERV_FlashBusy) {
108              debugError( "EFC command failed\n" );
109              return false;
110         }
111         return true;
112     } else {
113         unsigned int vendorId = configRom.getNodeVendorId();
114         unsigned int modelId = configRom.getModelId();
115         Util::Configuration::VendorModelEntry vme = c.findDeviceVME( vendorId, modelId );
116         return c.isValid(vme) && vme.driver == Util::Configuration::eD_FireWorks;
117     }
118 }
119
120 bool
121 Device::discover()
122 {
123     unsigned int vendorId = getConfigRom().getNodeVendorId();
124     unsigned int modelId = getConfigRom().getModelId();
125
126     Util::Configuration &c = getDeviceManager().getConfiguration();
127     Util::Configuration::VendorModelEntry vme = c.findDeviceVME( vendorId, modelId );
128
129     if (c.isValid(vme) && vme.driver == Util::Configuration::eD_FireWorks) {
130         debugOutput( DEBUG_LEVEL_VERBOSE, "found %s %s\n",
131                      vme.vendor_name.c_str(),
132                      vme.model_name.c_str());
133     } else {
134         debugWarning("Using generic ECHO Audio FireWorks support for unsupported device '%s %s'\n",
135                      getConfigRom().getVendorName().c_str(), getConfigRom().getModelName().c_str());
136     }
137
138     // get the info from the EFC
139     if ( !discoverUsingEFC() ) {
140         return false;
141     }
142
143     // discover AVC-wise
144     if ( !GenericAVC::Device::discoverGeneric() ) {
145         debugError( "Could not discover GenericAVC::Device\n" );
146         return false;
147     }
148
149     if(!buildMixer()) {
150         debugWarning("Could not build mixer\n");
151     }
152
153     return true;
154 }
155
156 bool
157 Device::discoverUsingEFC()
158 {
159     m_efc_discovery_done = false;
160     m_HwInfo.setVerboseLevel(getDebugLevel());
161
162     if (!doEfcOverAVC(m_HwInfo)) {
163         debugError("Could not read hardware capabilities\n");
164         return false;
165     }
166
167     // check the firmware version
168     if (m_HwInfo.m_arm_version < FIREWORKS_MIN_FIRMWARE_VERSION) {
169         debugError("Firmware version %u.%u (rev %u) not recent enough. FFADO requires at least version %u.%u (rev %u).\n",
170                     (m_HwInfo.m_arm_version >> 24) & 0xFF,
171                     (m_HwInfo.m_arm_version >> 16) & 0xFF,
172                     (m_HwInfo.m_arm_version >> 0) & 0xFFFF,
173                     (FIREWORKS_MIN_FIRMWARE_VERSION >> 24) & 0xFF,
174                     (FIREWORKS_MIN_FIRMWARE_VERSION >> 16) & 0xFF,
175                     (FIREWORKS_MIN_FIRMWARE_VERSION >> 0) & 0xFFFF
176                     );
177         return false;
178     }
179
180     // save the EFC version, since some stuff
181     // depends on this
182     m_efc_version = m_HwInfo.m_header.version;
183
184     if (!updatePolledValues()) {
185         debugError("Could not update polled values\n");
186         return false;
187     }
188
189     m_efc_discovery_done = true;
190     return true;
191 }
192
193 FFADODevice *
194 Device::createDevice(DeviceManager& d, std::auto_ptr<ConfigRom>( configRom ))
195 {
196     unsigned int vendorId = configRom->getNodeVendorId();
197 //     unsigned int modelId = configRom->getModelId();
198
199     switch(vendorId) {
200         case FW_VENDORID_ECHO: return new ECHO::AudioFire(d, configRom );
201         default: return new Device(d, configRom );
202     }
203 }
204
205 bool
206 Device::doEfcOverAVC(EfcCmd &c) {
207     EfcOverAVCCmd cmd( get1394Service() );
208     cmd.setCommandType( AVC::AVCCommand::eCT_Control );
209     cmd.setNodeId( getConfigRom().getNodeId() );
210     cmd.setSubunitType( AVC::eST_Unit  );
211     cmd.setSubunitId( 0xff );
212
213     cmd.setVerbose( getDebugLevel() );
214     cmd.m_cmd = &c;
215
216     if ( !cmd.fire()) {
217         debugError( "EfcOverAVCCmd command failed\n" );
218         c.showEfcCmd();
219         return false;
220     }
221
222     if ( cmd.getResponse() != AVC::AVCCommand::eR_Accepted) {
223         debugError( "EfcOverAVCCmd not accepted\n" );
224         return false;
225     }
226
227     if (   c.m_header.retval != EfcCmd::eERV_Ok
228         && c.m_header.retval != EfcCmd::eERV_FlashBusy) {
229         debugError( "EFC command failed\n" );
230         c.showEfcCmd();
231         return false;
232     }
233
234     return true;
235 }
236
237 bool
238 Device::buildMixer()
239 {
240     bool result=true;
241     debugOutput(DEBUG_LEVEL_VERBOSE, "Building a FireWorks mixer...\n");
242    
243     destroyMixer();
244    
245     // create the mixer object container
246     m_MixerContainer = new Control::Container(this, "Mixer");
247
248     if (!m_MixerContainer) {
249         debugError("Could not create mixer container...\n");
250         return false;
251     }
252
253     // create control objects for the audiofire
254
255     // matrix mix controls
256     result &= m_MixerContainer->addElement(
257         new MonitorControl(*this, MonitorControl::eMC_Gain, "MonitorGain"));
258
259     result &= m_MixerContainer->addElement(
260         new MonitorControl(*this, MonitorControl::eMC_Mute, "MonitorMute"));
261
262     result &= m_MixerContainer->addElement(
263         new MonitorControl(*this, MonitorControl::eMC_Solo, "MonitorSolo"));
264
265     result &= m_MixerContainer->addElement(
266         new MonitorControl(*this, MonitorControl::eMC_Pan, "MonitorPan"));
267
268     // Playback mix controls
269     for (unsigned int ch=0;ch<m_HwInfo.m_nb_1394_playback_channels;ch++) {
270         std::ostringstream node_name;
271         node_name << "PC" << ch;
272        
273         result &= m_MixerContainer->addElement(
274             new BinaryControl(*this, eMT_PlaybackMix, eMC_Mute, ch, 0, node_name.str()+"Mute"));
275         result &= m_MixerContainer->addElement(
276             new BinaryControl(*this, eMT_PlaybackMix, eMC_Solo, ch, 0, node_name.str()+"Solo"));
277         result &= m_MixerContainer->addElement(
278             new SimpleControl(*this, eMT_PlaybackMix, eMC_Gain, ch, node_name.str()+"Gain"));
279     }
280    
281     // Physical output mix controls
282     for (unsigned int ch=0;ch<m_HwInfo.m_nb_phys_audio_out;ch++) {
283         std::ostringstream node_name;
284         node_name << "OUT" << ch;
285        
286         result &= m_MixerContainer->addElement(
287             new BinaryControl(*this, eMT_PhysicalOutputMix, eMC_Mute, ch, 0, node_name.str()+"Mute"));
288         result &= m_MixerContainer->addElement(
289             new BinaryControl(*this, eMT_PhysicalOutputMix, eMC_Nominal, ch, 1, node_name.str()+"Nominal"));
290         result &= m_MixerContainer->addElement(
291             new SimpleControl(*this, eMT_PhysicalOutputMix, eMC_Gain, ch, node_name.str()+"Gain"));
292     }
293    
294     // Physical input mix controls
295     for (unsigned int ch=0;ch<m_HwInfo.m_nb_phys_audio_in;ch++) {
296         std::ostringstream node_name;
297         node_name << "IN" << ch;
298        
299         // result &= m_MixerContainer->addElement(
300         //     new BinaryControl(*this, eMT_PhysicalInputMix, eMC_Pad, ch, 0, node_name.str()+"Pad"));
301         result &= m_MixerContainer->addElement(
302             new BinaryControl(*this, eMT_PhysicalInputMix, eMC_Nominal, ch, 1, node_name.str()+"Nominal"));
303     }
304
305     // add hardware information controls
306     m_HwInfoContainer = new Control::Container(this, "HwInfo");
307     result &= m_HwInfoContainer->addElement(
308         new HwInfoControl(*this, HwInfoControl::eHIF_PhysicalAudioOutCount, "PhysicalAudioOutCount"));
309     result &= m_HwInfoContainer->addElement(
310         new HwInfoControl(*this, HwInfoControl::eHIF_PhysicalAudioInCount, "PhysicalAudioInCount"));
311     result &= m_HwInfoContainer->addElement(
312         new HwInfoControl(*this, HwInfoControl::eHIF_1394PlaybackCount, "1394PlaybackCount"));
313     result &= m_HwInfoContainer->addElement(
314         new HwInfoControl(*this, HwInfoControl::eHIF_1394RecordCount, "1394RecordCount"));
315     result &= m_HwInfoContainer->addElement(
316         new HwInfoControl(*this, HwInfoControl::eHIF_GroupOutCount, "GroupOutCount"));
317     result &= m_HwInfoContainer->addElement(
318         new HwInfoControl(*this, HwInfoControl::eHIF_GroupInCount, "GroupInCount"));
319     result &= m_HwInfoContainer->addElement(
320         new HwInfoControl(*this, HwInfoControl::eHIF_PhantomPower, "PhantomPower"));
321
322     // add a save settings control
323     result &= this->addElement(
324         new MultiControl(*this, MultiControl::eT_SaveSession, "SaveSettings"));
325
326     // add an identify control
327     result &= this->addElement(
328         new MultiControl(*this, MultiControl::eT_Identify, "Identify"));
329
330     // spdif mode control
331     result &= this->addElement(
332         new SpdifModeControl(*this, "SpdifMode"));
333
334     // check for IO config controls and add them if necessary
335     if(m_HwInfo.hasMirroring()) {
336         result &= this->addElement(
337             new IOConfigControl(*this, eCR_Mirror, "ChannelMirror"));
338     }
339     if(m_HwInfo.hasSoftwarePhantom()) {
340         result &= this->addElement(
341             new IOConfigControl(*this, eCR_Phantom, "PhantomPower"));
342     }
343
344     if (!result) {
345         debugWarning("One or more control elements could not be created.");
346         // clean up those that couldn't be created
347         destroyMixer();
348         return false;
349     }
350
351     if (!addElement(m_MixerContainer)) {
352         debugWarning("Could not register mixer to device\n");
353         // clean up
354         destroyMixer();
355         return false;
356     }
357
358     if (!addElement(m_HwInfoContainer)) {
359         debugWarning("Could not register hwinfo to device\n");
360         // clean up
361         destroyMixer();
362         return false;
363     }
364
365     // load the session block
366     if (!loadSession()) {
367         debugWarning("Could not load session\n");
368     }
369
370     return true;
371 }
372
373 bool
374 Device::destroyMixer()
375 {
376     debugOutput(DEBUG_LEVEL_VERBOSE, "destroy mixer...\n");
377
378     if (m_MixerContainer == NULL) {
379         debugOutput(DEBUG_LEVEL_VERBOSE, "no mixer to destroy...\n");
380     } else {
381         if (!deleteElement(m_MixerContainer)) {
382             debugError("Mixer present but not registered to the avdevice\n");
383             return false;
384         }
385
386         // remove and delete (as in free) child control elements
387         m_MixerContainer->clearElements(true);
388         delete m_MixerContainer;
389         m_MixerContainer = NULL;
390     }
391
392     if (m_HwInfoContainer == NULL) {
393         debugOutput(DEBUG_LEVEL_VERBOSE, "no hwinfo to destroy...\n");
394     } else {
395         if (!deleteElement(m_HwInfoContainer)) {
396             debugError("HwInfo present but not registered to the avdevice\n");
397             return false;
398         }
399
400         // remove and delete (as in free) child control elements
401         m_HwInfoContainer->clearElements(true);
402         delete m_HwInfoContainer;
403         m_HwInfoContainer = NULL;
404     }
405     return true;
406 }
407
408 bool
409 Device::saveSession()
410 {
411     // save the session block
412 //     if ( !updateSession() ) {
413 //         debugError( "Could not update session\n" );
414 //     } else {
415         if ( !m_session.saveToDevice(*this) ) {
416             debugError( "Could not save session block\n" );
417         }
418 //     }
419
420     return true;
421 }
422
423 bool
424 Device::loadSession()
425 {
426     if ( !m_session.loadFromDevice(*this) ) {
427         debugError( "Could not load session block\n" );
428         return false;
429     }
430     return true;
431 }
432
433 bool
434 Device::updatePolledValues() {
435     Util::MutexLockHelper lock(*m_poll_lock);
436     return doEfcOverAVC(m_Polled);
437 }
438
439 #define ECHO_CHECK_AND_ADD_SR(v, x) \
440     { if(x >= m_HwInfo.m_min_sample_rate && x <= m_HwInfo.m_max_sample_rate) \
441       v.push_back(x); }
442 std::vector<int>
443 Device::getSupportedSamplingFrequencies()
444 {
445     std::vector<int> frequencies;
446     ECHO_CHECK_AND_ADD_SR(frequencies, 22050);
447     ECHO_CHECK_AND_ADD_SR(frequencies, 24000);
448     ECHO_CHECK_AND_ADD_SR(frequencies, 32000);
449     ECHO_CHECK_AND_ADD_SR(frequencies, 44100);
450     ECHO_CHECK_AND_ADD_SR(frequencies, 48000);
451     ECHO_CHECK_AND_ADD_SR(frequencies, 88200);
452     ECHO_CHECK_AND_ADD_SR(frequencies, 96000);
453     ECHO_CHECK_AND_ADD_SR(frequencies, 176400);
454     ECHO_CHECK_AND_ADD_SR(frequencies, 192000);
455     return frequencies;
456 }
457
458 FFADODevice::ClockSourceVector
459 Device::getSupportedClockSources() {
460     FFADODevice::ClockSourceVector r;
461
462     if (!m_efc_discovery_done) {
463         debugError("EFC discovery not done yet!\n");
464         return r;
465     }
466
467     uint32_t active_clock=getClock();
468
469     if(EFC_CMD_HW_CHECK_FLAG(m_HwInfo.m_supported_clocks, EFC_CMD_HW_CLOCK_INTERNAL)) {
470         debugOutput(DEBUG_LEVEL_VERBOSE, "Internal clock supported\n");
471         ClockSource s=clockIdToClockSource(EFC_CMD_HW_CLOCK_INTERNAL);
472         s.active=(active_clock == EFC_CMD_HW_CLOCK_INTERNAL);
473         if (s.type != eCT_Invalid) r.push_back(s);
474     }
475     if(EFC_CMD_HW_CHECK_FLAG(m_HwInfo.m_supported_clocks, EFC_CMD_HW_CLOCK_SYTMATCH)) {
476         debugOutput(DEBUG_LEVEL_VERBOSE, "Syt Match clock supported\n");
477         ClockSource s=clockIdToClockSource(EFC_CMD_HW_CLOCK_SYTMATCH);
478         s.active=(active_clock == EFC_CMD_HW_CLOCK_SYTMATCH);
479         if (s.type != eCT_Invalid) r.push_back(s);
480     }
481     if(EFC_CMD_HW_CHECK_FLAG(m_HwInfo.m_supported_clocks, EFC_CMD_HW_CLOCK_WORDCLOCK)) {
482         debugOutput(DEBUG_LEVEL_VERBOSE, "WordClock supported\n");
483         ClockSource s=clockIdToClockSource(EFC_CMD_HW_CLOCK_WORDCLOCK);
484         s.active=(active_clock == EFC_CMD_HW_CLOCK_WORDCLOCK);
485         if (s.type != eCT_Invalid) r.push_back(s);
486     }
487     if(EFC_CMD_HW_CHECK_FLAG(m_HwInfo.m_supported_clocks, EFC_CMD_HW_CLOCK_SPDIF)) {
488         debugOutput(DEBUG_LEVEL_VERBOSE, "SPDIF clock supported\n");
489         ClockSource s=clockIdToClockSource(EFC_CMD_HW_CLOCK_SPDIF);
490         s.active=(active_clock == EFC_CMD_HW_CLOCK_SPDIF);
491         if (s.type != eCT_Invalid) r.push_back(s);
492     }
493     if(EFC_CMD_HW_CHECK_FLAG(m_HwInfo.m_supported_clocks, EFC_CMD_HW_CLOCK_ADAT_1)) {
494         debugOutput(DEBUG_LEVEL_VERBOSE, "ADAT 1 clock supported\n");
495         ClockSource s=clockIdToClockSource(EFC_CMD_HW_CLOCK_ADAT_1);
496         s.active=(active_clock == EFC_CMD_HW_CLOCK_ADAT_1);
497         if (s.type != eCT_Invalid) r.push_back(s);
498     }
499     if(EFC_CMD_HW_CHECK_FLAG(m_HwInfo.m_supported_clocks, EFC_CMD_HW_CLOCK_ADAT_2)) {
500         debugOutput(DEBUG_LEVEL_VERBOSE, "ADAT 2 clock supported\n");
501         ClockSource s=clockIdToClockSource(EFC_CMD_HW_CLOCK_ADAT_2);
502         s.active=(active_clock == EFC_CMD_HW_CLOCK_ADAT_2);
503         if (s.type != eCT_Invalid) r.push_back(s);
504     }
505     return r;
506 }
507
508 bool
509 Device::isClockValid(uint32_t id) {
510     // always valid
511     if (id==EFC_CMD_HW_CLOCK_INTERNAL) return true;
512
513     // the polled values are used to detect
514     // whether clocks are valid
515     if (!updatePolledValues()) {
516         debugError("Could not update polled values\n");
517         return false;
518     }
519     return EFC_CMD_HW_CHECK_FLAG(m_Polled.m_status,id);
520 }
521
522 bool
523 Device::setActiveClockSource(ClockSource s) {
524     bool result;
525
526     debugOutput(DEBUG_LEVEL_VERBOSE, "setting clock source to id: %d\n",s.id);
527
528     if(!isClockValid(s.id)) {
529         debugError("Clock not valid\n");
530         return false;
531     }
532
533     result=setClock(s.id);
534
535     // From the ECHO sources:
536     // "If this is a 1200F and the sample rate is being set via EFC, then
537     // send the "phy reconnect command" so the device will vanish and reappear
538     // with a new descriptor."
539
540 //     EfcPhyReconnectCmd rccmd;
541 //     if(!doEfcOverAVC(rccmd)) {
542 //         debugError("Phy reconnect failed\n");
543 //     } else {
544 //         // sleep for one second such that the phy can get reconnected
545 //         sleep(1);
546 //     }
547
548     return result;
549 }
550
551 FFADODevice::ClockSource
552 Device::getActiveClockSource() {
553     ClockSource s;
554     uint32_t active_clock=getClock();
555     s=clockIdToClockSource(active_clock);
556     s.active=true;
557     return s;
558 }
559
560 FFADODevice::ClockSource
561 Device::clockIdToClockSource(uint32_t clockid) {
562     ClockSource s;
563     debugOutput(DEBUG_LEVEL_VERBOSE, "clock id: %u\n", clockid);
564
565     // the polled values are used to detect
566     // whether clocks are valid
567     if (!updatePolledValues()) {
568         debugError("Could not update polled values\n");
569         return s;
570     }
571
572     switch (clockid) {
573         case EFC_CMD_HW_CLOCK_INTERNAL:
574             debugOutput(DEBUG_LEVEL_VERBOSE, "Internal clock\n");
575             s.type=eCT_Internal;
576             s.description="Internal sync";
577             break;
578
579         case EFC_CMD_HW_CLOCK_SYTMATCH:
580             debugOutput(DEBUG_LEVEL_VERBOSE, "Syt Match\n");
581             s.type=eCT_SytMatch;
582             s.description="SYT Match";
583             break;
584
585         case EFC_CMD_HW_CLOCK_WORDCLOCK:
586             debugOutput(DEBUG_LEVEL_VERBOSE, "WordClock\n");
587             s.type=eCT_WordClock;
588             s.description="Word Clock";
589             break;
590
591         case EFC_CMD_HW_CLOCK_SPDIF:
592             debugOutput(DEBUG_LEVEL_VERBOSE, "SPDIF clock\n");
593             s.type=eCT_SPDIF;
594             s.description="SPDIF";
595             break;
596
597         case EFC_CMD_HW_CLOCK_ADAT_1:
598             debugOutput(DEBUG_LEVEL_VERBOSE, "ADAT 1 clock\n");
599             s.type=eCT_ADAT;
600             s.description="ADAT 1";
601             break;
602
603         case EFC_CMD_HW_CLOCK_ADAT_2:
604             debugOutput(DEBUG_LEVEL_VERBOSE, "ADAT 2 clock\n");
605             s.type=eCT_ADAT;
606             s.description="ADAT 2";
607             break;
608
609         default:
610             debugError("Invalid clock id: %d\n",clockid);
611             return s; // return an invalid ClockSource
612     }
613
614     s.id=clockid;
615     s.valid=isClockValid(clockid);
616
617     return s;
618 }
619
620 uint32_t
621 Device::getClock() {
622     EfcGetClockCmd gccmd;
623     if (!doEfcOverAVC(gccmd)) {
624         debugError("Could not get clock info\n");
625         return EFC_CMD_HW_CLOCK_UNSPECIFIED;
626     }
627     debugOutput(DEBUG_LEVEL_VERBOSE, "Active clock: 0x%08X\n",gccmd.m_clock);
628     gccmd.showEfcCmd();
629
630     return gccmd.m_clock;
631 }
632
633 bool
634 Device::setClock(uint32_t id) {
635     EfcGetClockCmd gccmd;
636     if (!doEfcOverAVC(gccmd)) {
637         debugError("Could not get clock info\n");
638         return false;
639     }
640     debugOutput(DEBUG_LEVEL_VERBOSE, "Set clock: 0x%08X\n", id);
641
642     EfcSetClockCmd sccmd;
643     sccmd.m_clock=id;
644     sccmd.m_samplerate=gccmd.m_samplerate;
645     sccmd.m_index=0;
646     if (!doEfcOverAVC(sccmd)) {
647         debugError("Could not set clock info\n");
648         return false;
649     }
650     return true;
651 }
652
653 bool
654 Device::lockFlash(bool lock) {
655     // some hardware doesn't need/support flash lock
656     if (m_HwInfo.hasDSP()) {
657         debugOutput(DEBUG_LEVEL_VERBOSE, "flash lock not needed\n");
658         return true;
659     }
660
661     EfcFlashLockCmd cmd;
662     cmd.m_lock = lock;
663
664     if(!doEfcOverAVC(cmd)) {
665         debugError("Flash lock failed\n");
666         return false;
667     }
668     return true;
669 }
670
671 bool
672 Device::writeFlash(uint32_t start, uint32_t len, uint32_t* buffer) {
673
674     if(len <= 0 || 0xFFFFFFFF - len*4 < start) {
675         debugError("bogus start/len: 0x%08X / %u\n", start, len);
676         return false;
677     }
678     if(start & 0x03) {
679         debugError("start address not quadlet aligned: 0x%08X\n", start);
680         return false;
681     }
682
683     uint32_t start_addr = start;
684     uint32_t stop_addr = start + len*4;
685     uint32_t *target_buffer = buffer;
686
687     EfcFlashWriteCmd cmd;
688     // write EFC_FLASH_SIZE_BYTES at a time
689     for(start_addr = start; start_addr < stop_addr; start_addr += EFC_FLASH_SIZE_BYTES) {
690         cmd.m_address = start_addr;
691         unsigned int quads_to_write = (stop_addr - start_addr)/4;
692         if (quads_to_write > EFC_FLASH_SIZE_QUADS) {
693             quads_to_write = EFC_FLASH_SIZE_QUADS;
694         }
695         cmd.m_nb_quadlets = quads_to_write;
696         for(unsigned int i=0; i<quads_to_write; i++) {
697             cmd.m_data[i] = *target_buffer;
698             target_buffer++;
699         }
700         if(!doEfcOverAVC(cmd)) {
701             debugError("Flash write failed for block 0x%08X (%d quadlets)\n", start_addr, quads_to_write);
702             return false;
703         }
704     }
705     return true;
706 }
707
708 bool
709 Device::readFlash(uint32_t start, uint32_t len, uint32_t* buffer) {
710
711     if(len <= 0 || 0xFFFFFFFF - len*4 < start) {
712         debugError("bogus start/len: 0x%08X / %u\n", start, len);
713         return false;
714     }
715     if(start & 0x03) {
716         debugError("start address not quadlet aligned: 0x%08X\n", start);
717         return false;
718     }
719
720     uint32_t start_addr = start;
721     uint32_t stop_addr = start + len*4;
722     uint32_t *target_buffer = buffer;
723
724     EfcFlashReadCmd cmd;
725     // read EFC_FLASH_SIZE_BYTES at a time
726     for(start_addr = start; start_addr < stop_addr; start_addr += EFC_FLASH_SIZE_BYTES) {
727         unsigned int quads_to_read = (stop_addr - start_addr)/4;
728         if (quads_to_read > EFC_FLASH_SIZE_QUADS) {
729             quads_to_read = EFC_FLASH_SIZE_QUADS;
730         }
731         uint32_t quadlets_read = 0;
732         int ntries = 10000;
733         do {
734             cmd.m_address = start_addr + quadlets_read*4;
735             unsigned int new_to_read = quads_to_read - quadlets_read;
736             cmd.m_nb_quadlets = new_to_read;
737             if(!doEfcOverAVC(cmd)) {
738                 debugError("Flash read failed for block 0x%08X (%d quadlets)\n", start_addr, quads_to_read);
739                 return false;
740             }
741             if(cmd.m_nb_quadlets != new_to_read) {
742                 debugOutput(DEBUG_LEVEL_VERBOSE,
743                             "Flash read didn't return enough data (%u/%u) \n",
744                             cmd.m_nb_quadlets, new_to_read);
745                 // continue trying
746             }
747             quadlets_read += cmd.m_nb_quadlets;
748
749             // copy content
750             for(unsigned int i=0; i<cmd.m_nb_quadlets; i++) {
751                 *target_buffer = cmd.m_data[i];
752                 target_buffer++;
753             }
754         } while(quadlets_read < quads_to_read && ntries--);
755         if(ntries==0) {
756             debugError("deadlock while reading flash\n");
757             return false;
758         }
759     }
760     return true;
761 }
762
763 bool
764 Device::eraseFlash(uint32_t addr) {
765     if(addr & 0x03) {
766         debugError("start address not quadlet aligned: 0x%08X\n", addr);
767         return false;
768     }
769     EfcFlashEraseCmd cmd;
770     cmd.m_address = addr;
771     if(!doEfcOverAVC(cmd)) {
772         if (cmd.m_header.retval == EfcCmd::eERV_FlashBusy) {
773             return true;
774         }
775         debugError("Flash erase failed for block 0x%08X\n", addr);
776         return false;
777     }
778     return true;
779 }
780
781 bool
782 Device::eraseFlashBlocks(uint32_t start_address, unsigned int nb_quads)
783 {
784     uint32_t blocksize_bytes;
785     uint32_t blocksize_quads;
786     unsigned int quads_left = nb_quads;
787     bool success = true;
788
789     const unsigned int max_nb_tries = 10;
790     unsigned int nb_tries = 0;
791
792     do {
793         // the erase block size is fixed by the HW, and depends
794         // on the flash section we're in
795         if (start_address < MAINBLOCKS_BASE_OFFSET_BYTES)
796                 blocksize_bytes = PROGRAMBLOCK_SIZE_BYTES;
797         else
798                 blocksize_bytes = MAINBLOCK_SIZE_BYTES;
799         start_address &= ~(blocksize_bytes - 1);
800         blocksize_quads = blocksize_bytes / 4;
801
802         uint32_t verify[blocksize_quads];
803
804         // corner case: requested to erase less than one block
805         if (blocksize_quads > quads_left) {
806             blocksize_quads = quads_left;
807         }
808
809         // do the actual erase
810         if (!eraseFlash(start_address)) {
811             debugWarning("Could not erase flash block at 0x%08X\n", start_address);
812             success = false;
813         } else {
814             // wait for the flash to become ready again
815             if (!waitForFlash(ECHO_FLASH_ERASE_TIMEOUT_MILLISECS)) {
816                 debugError("Wait for flash timed out at address 0x%08X\n", start_address);
817                 return false;
818             }
819
820             // verify that the block is empty as an extra precaution
821             if (!readFlash(start_address, blocksize_quads, verify)) {
822                 debugError("Could not read flash block at 0x%08X\n", start_address);
823                 return false;
824             }
825
826             // everything should be 0xFFFFFFFF if the erase was successful
827             for (unsigned int i = 0; i < blocksize_quads; i++) {
828                 if (0xFFFFFFFF != verify[i]) {
829                     debugWarning("Flash erase verification failed.\n");
830                     success = false;
831                     break;
832                 }
833             }
834         }
835
836         if (success) {
837             start_address += blocksize_bytes;
838             quads_left -= blocksize_quads;
839             nb_tries = 0;
840         } else {
841             nb_tries++;
842         }
843         if (nb_tries > max_nb_tries) {
844             debugError("Needed too many tries to erase flash at 0x%08X\n", start_address);
845             return false;
846         }
847     } while (quads_left > 0);
848
849     return true;
850 }
851
852 bool
853 Device::waitForFlash(unsigned int msecs)
854 {
855     bool ready;
856
857     EfcFlashGetStatusCmd statusCmd;
858     const unsigned int time_to_sleep_usecs = 10000;
859     int wait_cycles = msecs * 1000 / time_to_sleep_usecs;
860
861     do {
862         if (!doEfcOverAVC(statusCmd)) {
863             debugError("Could not read flash status\n");
864             return false;
865         }
866         if (statusCmd.m_header.retval == EfcCmd::eERV_FlashBusy) {
867             ready = false;
868         } else {
869             ready = statusCmd.m_ready;
870         }
871         usleep(time_to_sleep_usecs);
872     } while (!ready && wait_cycles--);
873
874     if(wait_cycles == 0) {
875         debugError("Timeout while waiting for flash\n");
876         return false;
877     }
878
879     return ready;
880 }
881
882 uint32_t
883 Device::getSessionBase()
884 {
885     EfcFlashGetSessionBaseCmd cmd;
886     if(!doEfcOverAVC(cmd)) {
887         debugError("Could not get session base address\n");
888         return 0; // FIXME: arbitrary
889     }
890     return cmd.m_address;
891 }
892
893 int
894 Device::getSamplingFrequency( ) {
895     EfcGetClockCmd gccmd;
896     if (!doEfcOverAVC(gccmd)) {
897         debugError("Could not get clock info\n");
898         return false;
899     }
900     return gccmd.m_samplerate;
901 }
902 bool
903 Device::setSamplingFrequency( int s )
904 {
905     EfcGetClockCmd gccmd;
906     if (!doEfcOverAVC(gccmd)) {
907         debugError("Could not get clock info\n");
908         return false;
909     }
910     debugOutput(DEBUG_LEVEL_VERBOSE, "Set samplerate: %d\n", s);
911
912     EfcSetClockCmd sccmd;
913     sccmd.m_clock=gccmd.m_clock;
914     sccmd.m_samplerate=s;
915     sccmd.m_index=0;
916     if (!doEfcOverAVC(sccmd)) {
917         debugError("Could not set sample rate\n");
918         return false;
919     }
920     return true;
921 }
922
923
924 } // FireWorks
Note: See TracBrowser for help on using the browser.