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

Revision 644, 18.3 kB (checked in by ppalmers, 14 years ago)

- Adapt the phase 88 mixer control application to the new DBUS control interface

  • 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     if ( !csr_info->service->read( csr_info->nodeId,
231                                    addr,
232                                    (size_t)length/4,
233                                    ( quadlet_t* )buffer) )
234     {
235         //debugOutput( DEBUG_LEVEL_VERBOSE, "ConfigRom: Read failed\n");
236         return -1;
237     }
238
239     return 0;
240 }
241
242 static int
243 getMaxRom( u_int32_t* bus_info_data,
244            void* /*private_data*/)
245 {
246     return (CSR1212_BE32_TO_CPU( bus_info_data[2] ) >> 8) & 0x3;
247 }
248
249
250 void
251 ConfigRom::processUnitDirectory( struct csr1212_csr* csr,
252                                  struct csr1212_keyval* ud_kv,
253                                  unsigned int *id )
254 {
255     struct csr1212_dentry *dentry;
256     struct csr1212_keyval *kv;
257     unsigned int last_key_id = 0;
258
259     debugOutput( DEBUG_LEVEL_VERBOSE, "process unit directory:\n" );
260     csr1212_for_each_dir_entry(csr, kv, ud_kv, dentry) {
261     switch (kv->key.id) {
262         case CSR1212_KV_ID_VENDOR:
263         if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
264                     debugOutput( DEBUG_LEVEL_VERBOSE,
265                                  "\tvendor_id = 0x%08x\n",
266                                  kv->value.immediate);
267                     m_vendorId = kv->value.immediate;
268         }
269         break;
270
271         case CSR1212_KV_ID_MODEL:
272                 debugOutput( DEBUG_LEVEL_VERBOSE,
273                              "\tmodel_id = 0x%08x\n",
274                              kv->value.immediate);
275                 m_modelId = kv->value.immediate;
276         break;
277
278         case CSR1212_KV_ID_SPECIFIER_ID:
279                 debugOutput( DEBUG_LEVEL_VERBOSE,
280                              "\tspecifier_id = 0x%08x\n",
281                              kv->value.immediate);
282                 m_unit_specifier_id = kv->value.immediate;
283         break;
284
285         case CSR1212_KV_ID_VERSION:
286                 debugOutput( DEBUG_LEVEL_VERBOSE,
287                              "\tversion = 0x%08x\n",
288                              kv->value.immediate);
289                 m_unit_version = kv->value.immediate;
290                 if ( m_unit_specifier_id == 0x0000a02d ) // XXX
291                 {
292                     if ( kv->value.immediate == 0x10001 ) {
293                         m_avcDevice = true;
294                     }
295                 }
296         break;
297
298         case CSR1212_KV_ID_DESCRIPTOR:
299         if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
300             CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
301             CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
302             CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
303             CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
304             CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0)
305         {
306             switch (last_key_id) {
307             case CSR1212_KV_ID_VENDOR:
308                 csr1212_keep_keyval(kv);
309                             m_vendorNameKv = kv;
310                 break;
311
312             case CSR1212_KV_ID_MODEL:
313                             m_modelNameKv = kv;
314                 csr1212_keep_keyval(kv);
315                 break;
316
317             }
318         } /* else if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) ... */
319         break;
320
321         case CSR1212_KV_ID_DEPENDENT_INFO:
322         if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) {
323             /* This should really be done in SBP2 as this is
324              * doing SBP2 specific parsing. */
325             processUnitDirectory(csr, kv, id);
326         }
327
328         break;
329
330         default:
331         break;
332     }
333     last_key_id = kv->key.id;
334     }
335 }
336
337 void
338 ConfigRom::processRootDirectory(struct csr1212_csr* csr)
339 {
340     unsigned int ud_id = 0;
341     struct csr1212_dentry *dentry;
342     struct csr1212_keyval *kv;
343     unsigned int last_key_id = 0;
344
345     csr1212_for_each_dir_entry(csr, kv, csr->root_kv, dentry) {
346     switch (kv->key.id) {
347         case CSR1212_KV_ID_VENDOR:
348                 debugOutput( DEBUG_LEVEL_VERBOSE,
349                              "vendor id = 0x%08x\n", kv->value.immediate);
350         break;
351
352         case CSR1212_KV_ID_NODE_CAPABILITIES:
353                 debugOutput( DEBUG_LEVEL_VERBOSE,
354                              "capabilities = 0x%08x\n", kv->value.immediate);
355         break;
356
357         case CSR1212_KV_ID_UNIT:
358         processUnitDirectory(csr, kv, &ud_id);
359         break;
360
361         case CSR1212_KV_ID_DESCRIPTOR:
362         if (last_key_id == CSR1212_KV_ID_VENDOR) {
363             if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
364             CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
365             CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
366             CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
367             CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
368             CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0)
369                     {
370                         m_vendorNameKv = kv;
371             csr1212_keep_keyval(kv);
372             }
373         }
374         break;
375     }
376     last_key_id = kv->key.id;
377     }
378
379 }
380
381 const fb_nodeid_t
382 ConfigRom::getNodeId() const
383 {
384     return m_nodeId;
385 }
386
387 const fb_octlet_t
388 ConfigRom::getGuid() const
389 {
390     return m_guid;
391 }
392
393 const Glib::ustring
394 ConfigRom::getGuidString() const
395 {
396     char* buf;
397     asprintf( &buf, "%08x%08x",
398               ( unsigned int ) ( getGuid() >> 32 ),
399               ( unsigned int ) ( getGuid() & 0xffffffff ) );
400     Glib::ustring result = buf;
401     free( buf );
402     return result;
403 }
404
405 const Glib::ustring
406 ConfigRom::getModelName() const
407 {
408     return m_modelName;
409 }
410
411 const Glib::ustring
412 ConfigRom::getVendorName() const
413 {
414     return m_vendorName;
415 }
416
417 const unsigned int
418 ConfigRom::getModelId() const
419 {
420     return m_modelId;
421 }
422
423 const unsigned int
424 ConfigRom::getVendorId() const
425 {
426     return m_vendorId;
427 }
428
429 const unsigned int
430 ConfigRom::getUnitSpecifierId() const
431 {
432     return m_unit_specifier_id;
433 }
434
435 const unsigned int
436 ConfigRom::getUnitVersion() const
437 {
438     return m_unit_version;
439 }
440
441 bool
442 ConfigRom::updatedNodeId()
443 {
444     struct csr1212_csr* csr = 0;
445     for ( fb_nodeid_t nodeId = 0;
446           nodeId < m_1394Service->getNodeCount();
447           ++nodeId )
448     {
449         struct config_csr_info csr_info;
450         csr_info.service = m_1394Service;
451         csr_info.nodeId = 0xffc0 | nodeId;
452
453         csr = csr1212_create_csr( &configrom_csr1212_ops,
454                                   5 * sizeof(fb_quadlet_t),   // XXX Why 5 ?!?
455                                   &csr_info );
456
457         if (!csr || csr1212_parse_csr( csr ) != CSR1212_SUCCESS) {
458             if (csr) {
459                 csr1212_destroy_csr(csr);
460             }
461             continue;
462         }
463
464
465         octlet_t guid =
466             ((u_int64_t)CSR1212_BE32_TO_CPU(csr->bus_info_data[3]) << 32)
467             | CSR1212_BE32_TO_CPU(csr->bus_info_data[4]);
468
469         if ( guid == getGuid() ) {
470             if ( nodeId != getNodeId() ) {
471                 debugOutput( DEBUG_LEVEL_VERBOSE,
472                              "Device with GUID 0x%08x%08x changed node id "
473                              "from %d to %d\n",
474                              ( unsigned int ) ( getGuid() >> 32 ),
475                              ( unsigned int ) ( getGuid() & 0xffffffff ),
476                              getNodeId(),
477                              nodeId );
478                 m_nodeId = nodeId;
479             }
480             if (csr) {
481                 csr1212_destroy_csr(csr);
482             }
483             return true;
484         }
485     }
486
487     if (csr) {
488         csr1212_destroy_csr(csr);
489     }
490
491     debugOutput( DEBUG_LEVEL_NORMAL,
492                  "Device with GUID 0x%08x%08x could not be found on "
493                  "the bus anymore (removed?)\n",
494                  m_guid >> 32,
495                  m_guid & 0xffffffff );
496     return false;
497 }
498
499 void
500 ConfigRom::printConfigRom() const
501 {
502     using namespace std;
503     printf( "Config ROM\n" );
504     printf( "\tCurrent Node Id:\t%d\n",       getNodeId() );
505     printf( "\tGUID:\t\t\t0x%08x%08x\n",
506             ( unsigned int )( getGuid() >> 32 ),
507             ( unsigned int ) ( getGuid() & 0xffffffff ) );
508     printf( "\tVendor Name:\t\t%s\n",         getVendorName().c_str() );
509     printf( "\tModel Name:\t\t%s\n",          getModelName().c_str() );
510     printf( "\tNode Vendor ID:\t\t0x%06x\n",  getNodeVendorId() );
511     printf( "\tModel Id:\t\t0x%08x\n",        getModelId() );
512     printf( "\tUnit Specifier ID:\t0x%06x\n",  getUnitSpecifierId() );
513     printf( "\tUnit version:\t\t0x%08x\n",        getUnitVersion() );
514     printf( "\tISO resource manager:\t%d\n",  isIsoResourseManager() );
515     printf( "\tCycle master capable:\t%d\n",  isSupportsIsoOperations() );
516     printf( "\tBus manager capable:\t%d\n",   isBusManagerCapable() );
517     printf( "\tCycle clock accuracy:\t%d\n", getCycleClockAccurancy() );
518     printf( "\tMax rec:\t\t%d (max asy payload: %d bytes)\n",
519             getMaxRec(), getAsyMaxPayload() );
520 }
521
522 unsigned short
523 ConfigRom::getAsyMaxPayload() const
524 {
525     // XXX use pow instead?
526     return 1 << ( m_maxRec + 1 );
527 }
528
529 bool
530 ConfigRom::serialize( Glib::ustring path, Util::IOSerialize& ser )
531 {
532     bool result;
533     result  = ser.write( path + "m_nodeId", m_nodeId );
534     result &= ser.write( path + "m_avcDevice",  m_avcDevice );
535     result &= ser.write( path + "m_guid", m_guid );
536     result &= ser.write( path + "m_vendorName", Glib::ustring( m_vendorName ) );
537     result &= ser.write( path + "m_modelName", Glib::ustring( m_modelName ) );
538     result &= ser.write( path + "m_vendorId", m_vendorId );
539     result &= ser.write( path + "m_modelId", m_modelId );
540     result &= ser.write( path + "m_unit_specifier_id", m_unit_specifier_id );
541     result &= ser.write( path + "m_unit_version",  m_unit_version );
542     result &= ser.write( path + "m_isIsoResourceManager", m_isIsoResourceManager );
543     result &= ser.write( path + "m_isCycleMasterCapable", m_isCycleMasterCapable );
544     result &= ser.write( path + "m_isSupportIsoOperations", m_isSupportIsoOperations );
545     result &= ser.write( path + "m_isBusManagerCapable", m_isBusManagerCapable );
546     result &= ser.write( path + "m_cycleClkAcc", m_cycleClkAcc );
547     result &= ser.write( path + "m_maxRec", m_maxRec );
548     result &= ser.write( path + "m_nodeVendorId", m_nodeVendorId );
549     result &= ser.write( path + "m_chipIdHi", m_chipIdHi );
550     result &= ser.write( path + "m_chipIdLow", m_chipIdLow );
551     return result;
552 }
553
554 ConfigRom*
555 ConfigRom::deserialize( Glib::ustring path, Util::IODeserialize& deser, Ieee1394Service& ieee1394Service )
556 {
557     ConfigRom* pConfigRom = new ConfigRom;
558     if ( !pConfigRom ) {
559         return 0;
560     }
561
562     pConfigRom->m_1394Service = &ieee1394Service;
563
564     bool result;
565     result  = deser.read( path + "m_nodeId", pConfigRom->m_nodeId );
566     result &= deser.read( path + "m_avcDevice", pConfigRom->m_avcDevice );
567     result &= deser.read( path + "m_guid", pConfigRom->m_guid );
568     result &= deser.read( path + "m_vendorName", pConfigRom->m_vendorName );
569     result &= deser.read( path + "m_modelName", pConfigRom->m_modelName );
570     result &= deser.read( path + "m_vendorId", pConfigRom->m_vendorId );
571     result &= deser.read( path + "m_modelId", pConfigRom->m_modelId );
572     result &= deser.read( path + "m_unit_specifier_id", pConfigRom->m_unit_specifier_id );
573     result &= deser.read( path + "m_unit_version", pConfigRom->m_unit_version );
574     result &= deser.read( path + "m_isIsoResourceManager", pConfigRom->m_isIsoResourceManager );
575     result &= deser.read( path + "m_isCycleMasterCapable", pConfigRom->m_isCycleMasterCapable );
576     result &= deser.read( path + "m_isSupportIsoOperations", pConfigRom->m_isSupportIsoOperations );
577     result &= deser.read( path + "m_isBusManagerCapable", pConfigRom->m_isBusManagerCapable );
578     result &= deser.read( path + "m_cycleClkAcc", pConfigRom->m_cycleClkAcc );
579     result &= deser.read( path + "m_maxRec", pConfigRom->m_maxRec );
580     result &= deser.read( path + "m_nodeVendorId", pConfigRom->m_nodeVendorId );
581     result &= deser.read( path + "m_chipIdHi", pConfigRom->m_chipIdHi );
582     result &= deser.read( path + "m_chipIdLow", pConfigRom->m_chipIdLow );
583
584     if ( !result ) {
585         delete pConfigRom;
586         return 0;
587     }
588
589     return pConfigRom;
590 }
591
592 bool
593 ConfigRom::setNodeId( fb_nodeid_t nodeId )
594 {
595     m_nodeId = nodeId;
596     return true;
597 }
Note: See TracBrowser for help on using the browser.