root/trunk/libffado/src/libieee1394/configrom.cpp

Revision 673, 18.9 kB (checked in by ppalmers, 13 years ago)

fix bus reset behavior

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*
2  * Copyright (C) 2005-2007 by Daniel Wagner
3  * Copyright (C) 2005-2007 by Jonathan Woithe
4  * Copyright (C) 2005-2007 by Pieter Palmers
5  *
6  * This file is part of FFADO
7  * FFADO = Free Firewire (pro-)audio drivers for linux
8  *
9  * FFADO is based upon FreeBoB
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License version 2.1, as published by the Free Software Foundation;
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23  * MA 02110-1301 USA
24  */
25
26 #include "configrom.h"
27 #include "ieee1394service.h"
28
29 #include <stdio.h>
30 #include <string.h>
31
32 #include <iostream>
33 #include <iomanip>
34
35 using namespace std;
36
37 IMPL_DEBUG_MODULE( ConfigRom, ConfigRom, DEBUG_LEVEL_NORMAL );
38
39 static int busRead( struct csr1212_csr* csr,
40                     u_int64_t addr,
41                     u_int16_t length,
42                     void* buffer,
43                     void* private_data );
44
45 static int getMaxRom( u_int32_t* bus_info_data,
46                       void* private_data );
47
48 static struct csr1212_bus_ops configrom_csr1212_ops = {
49     busRead,
50     0,
51     0,
52     getMaxRom
53 };
54
55 struct config_csr_info {
56     Ieee1394Service* service;
57     fb_nodeid_t      nodeId;
58 };
59
60 //-------------------------------------------------------------
61
62 ConfigRom::ConfigRom( Ieee1394Service& ieee1394service, fb_nodeid_t nodeId )
63     : Control::Element("ConfigRom")
64     , m_1394Service( &ieee1394service )
65     , m_nodeId( nodeId )
66     , m_avcDevice( false )
67     , m_guid( 0 )
68     , m_vendorName( "" )
69     , m_modelName( "" )
70     , m_vendorId( 0 )
71     , m_modelId( 0 )
72     , m_unit_specifier_id( 0 )
73     , m_unit_version( 0 )
74     , m_isIsoResourceManager( false )
75     , m_isCycleMasterCapable( false )
76     , m_isSupportIsoOperations( false )
77     , m_isBusManagerCapable( false )
78     , m_cycleClkAcc( 0 )
79     , m_maxRec( 0 )
80     , m_nodeVendorId( 0 )
81     , m_chipIdHi( 0 )
82     , m_chipIdLow( 0 )
83     , m_vendorNameKv( 0 )
84     , m_modelNameKv( 0 )
85     , m_csr( 0 )
86 {
87 }
88
89 ConfigRom::ConfigRom()
90     : Control::Element("ConfigRom")
91     , m_1394Service( 0 )
92     , m_nodeId( -1 )
93     , m_avcDevice( false )
94     , m_guid( 0 )
95     , m_vendorName( "" )
96     , m_modelName( "" )
97     , m_vendorId( 0 )
98     , m_modelId( 0 )
99     , m_unit_specifier_id( 0 )
100     , m_unit_version( 0 )
101     , m_isIsoResourceManager( false )
102     , m_isCycleMasterCapable( false )
103     , m_isSupportIsoOperations( false )
104     , m_isBusManagerCapable( false )
105     , m_cycleClkAcc( 0 )
106     , m_maxRec( 0 )
107     , m_nodeVendorId( 0 )
108     , m_chipIdHi( 0 )
109     , m_chipIdLow( 0 )
110     , m_vendorNameKv( 0 )
111     , m_modelNameKv( 0 )
112     , m_csr( 0 )
113 {
114 }
115
116 ConfigRom::~ConfigRom()
117 {
118 }
119
120 bool
121 ConfigRom::operator == ( const ConfigRom& rhs )
122 {
123     return m_guid == rhs.m_guid;
124 }
125
126 bool
127 ConfigRom::compareGUID( const ConfigRom& a, const ConfigRom& b ) {
128     return a.getGuid() > b.getGuid();
129 }
130
131 bool
132 ConfigRom::initialize()
133 {
134      struct config_csr_info csr_info;
135      csr_info.service = m_1394Service;
136      csr_info.nodeId = 0xffc0 | m_nodeId;
137
138      m_csr = csr1212_create_csr( &configrom_csr1212_ops,
139                                  5 * sizeof(fb_quadlet_t),   // XXX Why 5 ?!?
140                                  &csr_info );
141     if (!m_csr || csr1212_parse_csr( m_csr ) != CSR1212_SUCCESS) {
142         debugError( "Could not parse config rom of node %d on port %d\n", m_nodeId, m_1394Service->getPort() );
143         if (m_csr) {
144             csr1212_destroy_csr(m_csr);
145             m_csr = 0;
146         }
147         return false;
148     }
149
150     // Process Bus_Info_Block
151     m_isIsoResourceManager = CSR1212_BE32_TO_CPU(m_csr->bus_info_data[2] ) >> 31;
152     m_isCycleMasterCapable = ( CSR1212_BE32_TO_CPU(m_csr->bus_info_data[2] ) >> 30 ) & 0x1;
153     m_isSupportIsoOperations = ( CSR1212_BE32_TO_CPU(m_csr->bus_info_data[2] ) >> 29 ) & 0x1;
154     m_isBusManagerCapable = ( CSR1212_BE32_TO_CPU(m_csr->bus_info_data[2] ) >> 28 ) & 0x1;
155     m_cycleClkAcc = ( CSR1212_BE32_TO_CPU(m_csr->bus_info_data[2] ) >> 16 ) & 0xff;
156     m_maxRec = ( CSR1212_BE32_TO_CPU( m_csr->bus_info_data[2] ) >> 12 ) & 0xf;
157     m_nodeVendorId = ( CSR1212_BE32_TO_CPU( m_csr->bus_info_data[3] ) >> 8 );
158     m_chipIdHi = ( CSR1212_BE32_TO_CPU( m_csr->bus_info_data[3] ) ) & 0xff;
159     m_chipIdLow = CSR1212_BE32_TO_CPU( m_csr->bus_info_data[4] );
160
161     // Process Root Directory
162     processRootDirectory(m_csr);
163
164     if ( m_vendorNameKv ) {
165         int len = ( m_vendorNameKv->value.leaf.len - 2) * sizeof( quadlet_t );
166         char* buf = new char[len+2];
167         memcpy( buf,
168                 ( void* )CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA( m_vendorNameKv ),
169                 len );
170
171     while ((buf + len - 1) == '\0') {
172             len--;
173         }
174         // \todo XXX seems a bit strage to do this but the nodemgr.c code does
175         // it. try to figure out why this is needed (or not)
176     buf[len++] = ' ';
177     buf[len] = '\0';
178
179
180         debugOutput( DEBUG_LEVEL_VERBOSE, "Vendor name: '%s'\n", buf );
181         m_vendorName = buf;
182         delete[] buf;
183     }
184     if ( m_modelNameKv ) {
185         int len = ( m_modelNameKv->value.leaf.len - 2) * sizeof( quadlet_t );
186         char* buf = new char[len+2];
187         memcpy( buf,
188                 ( void* )CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA( m_modelNameKv ),
189                 len );
190     while ((buf + len - 1) == '\0') {
191             len--;
192         }
193         // \todo XXX for edirol fa-66 it seems somehow broken. see above
194         // todo as well.
195     buf[len++] = ' ';
196     buf[len] = '\0';
197
198         debugOutput( DEBUG_LEVEL_VERBOSE, "Model name: '%s'\n", buf);
199         m_modelName = buf;
200         delete[] buf;
201     }
202
203     m_guid = ((u_int64_t)CSR1212_BE32_TO_CPU(m_csr->bus_info_data[3]) << 32)
204              | CSR1212_BE32_TO_CPU(m_csr->bus_info_data[4]);
205
206     if ( m_vendorNameKv ) {
207         csr1212_release_keyval( m_vendorNameKv );
208         m_vendorNameKv = 0;
209     }
210     if ( m_modelNameKv ) {
211         csr1212_release_keyval( m_modelNameKv );
212         m_modelNameKv = 0;
213     }
214     if ( m_csr ) {
215         csr1212_destroy_csr(m_csr);
216         m_csr = 0;
217     }
218     return true;
219 }
220
221 static int
222 busRead( struct csr1212_csr* csr,
223          u_int64_t addr,
224          u_int16_t length,
225          void* buffer,
226          void* private_data )
227 {
228     struct config_csr_info* csr_info = (struct config_csr_info*) private_data;
229
230     int nb_retries = 5;
231
232     while ( !csr_info->service->read( csr_info->nodeId,
233                                    addr,
234                                    (size_t)length/4,
235                                    ( quadlet_t* )buffer) && nb_retries-- )
236     {// failed, retry
237     }
238
239     if (nb_retries) return 0; // success
240     else return -1; // failure
241 }
242
243 static int
244 getMaxRom( u_int32_t* bus_info_data,
245            void* /*private_data*/)
246 {
247     return (CSR1212_BE32_TO_CPU( bus_info_data[2] ) >> 8) & 0x3;
248 }
249
250
251 void
252 ConfigRom::processUnitDirectory( struct csr1212_csr* csr,
253                                  struct csr1212_keyval* ud_kv,
254                                  unsigned int *id )
255 {
256     struct csr1212_dentry *dentry;
257     struct csr1212_keyval *kv;
258     unsigned int last_key_id = 0;
259
260     debugOutput( DEBUG_LEVEL_VERBOSE, "process unit directory:\n" );
261     csr1212_for_each_dir_entry(csr, kv, ud_kv, dentry) {
262     switch (kv->key.id) {
263         case CSR1212_KV_ID_VENDOR:
264         if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
265                     debugOutput( DEBUG_LEVEL_VERBOSE,
266                                  "\tvendor_id = 0x%08x\n",
267                                  kv->value.immediate);
268                     m_vendorId = kv->value.immediate;
269         }
270         break;
271
272         case CSR1212_KV_ID_MODEL:
273                 debugOutput( DEBUG_LEVEL_VERBOSE,
274                              "\tmodel_id = 0x%08x\n",
275                              kv->value.immediate);
276                 m_modelId = kv->value.immediate;
277         break;
278
279         case CSR1212_KV_ID_SPECIFIER_ID:
280                 debugOutput( DEBUG_LEVEL_VERBOSE,
281                              "\tspecifier_id = 0x%08x\n",
282                              kv->value.immediate);
283                 m_unit_specifier_id = kv->value.immediate;
284         break;
285
286         case CSR1212_KV_ID_VERSION:
287                 debugOutput( DEBUG_LEVEL_VERBOSE,
288                              "\tversion = 0x%08x\n",
289                              kv->value.immediate);
290                 m_unit_version = kv->value.immediate;
291                 if ( m_unit_specifier_id == 0x0000a02d ) // XXX
292                 {
293                     if ( kv->value.immediate == 0x10001 ) {
294                         m_avcDevice = true;
295                     }
296                 }
297         break;
298
299         case CSR1212_KV_ID_DESCRIPTOR:
300         if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
301             CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
302             CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
303             CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
304             CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
305             CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0)
306         {
307             switch (last_key_id) {
308             case CSR1212_KV_ID_VENDOR:
309                 csr1212_keep_keyval(kv);
310                             m_vendorNameKv = kv;
311                 break;
312
313             case CSR1212_KV_ID_MODEL:
314                             m_modelNameKv = kv;
315                 csr1212_keep_keyval(kv);
316                 break;
317
318             }
319         } /* else if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) ... */
320         break;
321
322         case CSR1212_KV_ID_DEPENDENT_INFO:
323         if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) {
324             /* This should really be done in SBP2 as this is
325              * doing SBP2 specific parsing. */
326             processUnitDirectory(csr, kv, id);
327         }
328
329         break;
330
331         default:
332         break;
333     }
334     last_key_id = kv->key.id;
335     }
336 }
337
338 void
339 ConfigRom::processRootDirectory(struct csr1212_csr* csr)
340 {
341     unsigned int ud_id = 0;
342     struct csr1212_dentry *dentry;
343     struct csr1212_keyval *kv;
344     unsigned int last_key_id = 0;
345
346     csr1212_for_each_dir_entry(csr, kv, csr->root_kv, dentry) {
347     switch (kv->key.id) {
348         case CSR1212_KV_ID_VENDOR:
349                 debugOutput( DEBUG_LEVEL_VERBOSE,
350                              "vendor id = 0x%08x\n", kv->value.immediate);
351         break;
352
353         case CSR1212_KV_ID_NODE_CAPABILITIES:
354                 debugOutput( DEBUG_LEVEL_VERBOSE,
355                              "capabilities = 0x%08x\n", kv->value.immediate);
356         break;
357
358         case CSR1212_KV_ID_UNIT:
359         processUnitDirectory(csr, kv, &ud_id);
360         break;
361
362         case CSR1212_KV_ID_DESCRIPTOR:
363         if (last_key_id == CSR1212_KV_ID_VENDOR) {
364             if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
365             CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
366             CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
367             CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
368             CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
369             CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0)
370                     {
371                         m_vendorNameKv = kv;
372             csr1212_keep_keyval(kv);
373             }
374         }
375         break;
376     }
377     last_key_id = kv->key.id;
378     }
379
380 }
381
382 const fb_nodeid_t
383 ConfigRom::getNodeId() const
384 {
385     return m_nodeId;
386 }
387
388 const fb_octlet_t
389 ConfigRom::getGuid() const
390 {
391     return m_guid;
392 }
393
394 const Glib::ustring
395 ConfigRom::getGuidString() const
396 {
397     char* buf;
398     asprintf( &buf, "%08x%08x",
399               ( unsigned int ) ( getGuid() >> 32 ),
400               ( unsigned int ) ( getGuid() & 0xffffffff ) );
401     Glib::ustring result = buf;
402     free( buf );
403     return result;
404 }
405
406 const Glib::ustring
407 ConfigRom::getModelName() const
408 {
409     return m_modelName;
410 }
411
412 const Glib::ustring
413 ConfigRom::getVendorName() const
414 {
415     return m_vendorName;
416 }
417
418 const unsigned int
419 ConfigRom::getModelId() const
420 {
421     return m_modelId;
422 }
423
424 const unsigned int
425 ConfigRom::getVendorId() const
426 {
427     return m_vendorId;
428 }
429
430 const unsigned int
431 ConfigRom::getUnitSpecifierId() const
432 {
433     return m_unit_specifier_id;
434 }
435
436 const unsigned int
437 ConfigRom::getUnitVersion() const
438 {
439     return m_unit_version;
440 }
441
442 bool
443 ConfigRom::updatedNodeId()
444 {
445     debugOutput( DEBUG_LEVEL_VERBOSE,
446                  "Checking for updated node id for device with GUID 0x%016llX...\n",
447                  getGuid());
448
449     struct csr1212_csr* csr = NULL;
450     for ( fb_nodeid_t nodeId = 0;
451           nodeId < m_1394Service->getNodeCount();
452           ++nodeId )
453     {
454         struct config_csr_info csr_info;
455         csr_info.service = m_1394Service;
456         csr_info.nodeId = 0xffc0 | nodeId;
457         debugOutput( DEBUG_LEVEL_VERBOSE, "Looking at node %d...\n", nodeId);
458
459         csr = csr1212_create_csr( &configrom_csr1212_ops,
460                                   5 * sizeof(fb_quadlet_t),   // XXX Why 5 ?!?
461                                   &csr_info );
462
463         if (!csr || csr1212_parse_csr( csr ) != CSR1212_SUCCESS) {
464             debugWarning( "Failed to get/parse CSR\n");
465             if (csr) {
466                 csr1212_destroy_csr(csr);
467                 csr = NULL;
468             }
469             continue;
470         }
471
472         octlet_t guid =
473             ((u_int64_t)CSR1212_BE32_TO_CPU(csr->bus_info_data[3]) << 32)
474             | CSR1212_BE32_TO_CPU(csr->bus_info_data[4]);
475
476         debugOutput( DEBUG_LEVEL_VERBOSE,
477                         " Node has GUID 0x%016llX\n",
478                         guid);
479
480         if ( guid == getGuid() ) {
481             debugOutput( DEBUG_LEVEL_VERBOSE, "GUID matches ours\n");
482             if ( nodeId != getNodeId() ) {
483                 debugOutput( DEBUG_LEVEL_VERBOSE,
484                              "Device with GUID 0x%016llX changed node id "
485                              "from %d to %d\n",
486                              getGuid(),
487                              getNodeId(),
488                              nodeId );
489                 m_nodeId = nodeId;
490             } else {
491                 debugOutput( DEBUG_LEVEL_VERBOSE,
492                              "Device with GUID 0x%016llX kept node id %d\n",
493                              getGuid(),
494                              getNodeId());
495             }
496             if (csr) {
497                 csr1212_destroy_csr(csr);
498                 csr = NULL;
499             }
500             return true;
501         }
502     }
503
504     if (csr) {
505         csr1212_destroy_csr(csr);
506     }
507
508     debugOutput( DEBUG_LEVEL_NORMAL,
509                  "Device with GUID 0x%08x%08x could not be found on "
510                  "the bus anymore (removed?)\n",
511                  m_guid >> 32,
512                  m_guid & 0xffffffff );
513     return false;
514 }
515
516 void
517 ConfigRom::printConfigRom() const
518 {
519     using namespace std;
520     printf( "Config ROM\n" );
521     printf( "\tCurrent Node Id:\t%d\n",       getNodeId() );
522     printf( "\tGUID:\t\t\t0x%016llX\n",       getGuid());
523     printf( "\tVendor Name:\t\t%s\n",         getVendorName().c_str() );
524     printf( "\tModel Name:\t\t%s\n",          getModelName().c_str() );
525     printf( "\tNode Vendor ID:\t\t0x%06x\n",  getNodeVendorId() );
526     printf( "\tModel Id:\t\t0x%08x\n",        getModelId() );
527     printf( "\tUnit Specifier ID:\t0x%06x\n",  getUnitSpecifierId() );
528     printf( "\tUnit version:\t\t0x%08x\n",        getUnitVersion() );
529     printf( "\tISO resource manager:\t%d\n",  isIsoResourseManager() );
530     printf( "\tCycle master capable:\t%d\n",  isSupportsIsoOperations() );
531     printf( "\tBus manager capable:\t%d\n",   isBusManagerCapable() );
532     printf( "\tCycle clock accuracy:\t%d\n", getCycleClockAccurancy() );
533     printf( "\tMax rec:\t\t%d (max asy payload: %d bytes)\n",
534             getMaxRec(), getAsyMaxPayload() );
535 }
536
537 unsigned short
538 ConfigRom::getAsyMaxPayload() const
539 {
540     // XXX use pow instead?
541     return 1 << ( m_maxRec + 1 );
542 }
543
544 bool
545 ConfigRom::serialize( Glib::ustring path, Util::IOSerialize& ser )
546 {
547     bool result;
548     result  = ser.write( path + "m_nodeId", m_nodeId );
549     result &= ser.write( path + "m_avcDevice",  m_avcDevice );
550     result &= ser.write( path + "m_guid", m_guid );
551     result &= ser.write( path + "m_vendorName", Glib::ustring( m_vendorName ) );
552     result &= ser.write( path + "m_modelName", Glib::ustring( m_modelName ) );
553     result &= ser.write( path + "m_vendorId", m_vendorId );
554     result &= ser.write( path + "m_modelId", m_modelId );
555     result &= ser.write( path + "m_unit_specifier_id", m_unit_specifier_id );
556     result &= ser.write( path + "m_unit_version",  m_unit_version );
557     result &= ser.write( path + "m_isIsoResourceManager", m_isIsoResourceManager );
558     result &= ser.write( path + "m_isCycleMasterCapable", m_isCycleMasterCapable );
559     result &= ser.write( path + "m_isSupportIsoOperations", m_isSupportIsoOperations );
560     result &= ser.write( path + "m_isBusManagerCapable", m_isBusManagerCapable );
561     result &= ser.write( path + "m_cycleClkAcc", m_cycleClkAcc );
562     result &= ser.write( path + "m_maxRec", m_maxRec );
563     result &= ser.write( path + "m_nodeVendorId", m_nodeVendorId );
564     result &= ser.write( path + "m_chipIdHi", m_chipIdHi );
565     result &= ser.write( path + "m_chipIdLow", m_chipIdLow );
566     return result;
567 }
568
569 ConfigRom*
570 ConfigRom::deserialize( Glib::ustring path, Util::IODeserialize& deser, Ieee1394Service& ieee1394Service )
571 {
572     ConfigRom* pConfigRom = new ConfigRom;
573     if ( !pConfigRom ) {
574         return 0;
575     }
576
577     pConfigRom->m_1394Service = &ieee1394Service;
578
579     bool result;
580     result  = deser.read( path + "m_nodeId", pConfigRom->m_nodeId );
581     result &= deser.read( path + "m_avcDevice", pConfigRom->m_avcDevice );
582     result &= deser.read( path + "m_guid", pConfigRom->m_guid );
583     result &= deser.read( path + "m_vendorName", pConfigRom->m_vendorName );
584     result &= deser.read( path + "m_modelName", pConfigRom->m_modelName );
585     result &= deser.read( path + "m_vendorId", pConfigRom->m_vendorId );
586     result &= deser.read( path + "m_modelId", pConfigRom->m_modelId );
587     result &= deser.read( path + "m_unit_specifier_id", pConfigRom->m_unit_specifier_id );
588     result &= deser.read( path + "m_unit_version", pConfigRom->m_unit_version );
589     result &= deser.read( path + "m_isIsoResourceManager", pConfigRom->m_isIsoResourceManager );
590     result &= deser.read( path + "m_isCycleMasterCapable", pConfigRom->m_isCycleMasterCapable );
591     result &= deser.read( path + "m_isSupportIsoOperations", pConfigRom->m_isSupportIsoOperations );
592     result &= deser.read( path + "m_isBusManagerCapable", pConfigRom->m_isBusManagerCapable );
593     result &= deser.read( path + "m_cycleClkAcc", pConfigRom->m_cycleClkAcc );
594     result &= deser.read( path + "m_maxRec", pConfigRom->m_maxRec );
595     result &= deser.read( path + "m_nodeVendorId", pConfigRom->m_nodeVendorId );
596     result &= deser.read( path + "m_chipIdHi", pConfigRom->m_chipIdHi );
597     result &= deser.read( path + "m_chipIdLow", pConfigRom->m_chipIdLow );
598
599     if ( !result ) {
600         delete pConfigRom;
601         return 0;
602     }
603
604     return pConfigRom;
605 }
606
607 bool
608 ConfigRom::setNodeId( fb_nodeid_t nodeId )
609 {
610     m_nodeId = nodeId;
611     return true;
612 }
Note: See TracBrowser for help on using the browser.