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

Revision 2803, 30.1 kB (checked in by jwoithe, 1 year ago)

Cosmetic: capitalise "L" in "Linux".

"Linux" is a proper noun so it should start with a capital letter. These
changes are almost all within comments.

This patch was originally proposed by pander on the ffado-devel mailing
list. It has been expanded to cover all similar cases to maintain
consistency throughout the source tree.

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