2 * (C) Copyright IBM Corporation 2006
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
26 * \file common_device_name.c
27 * Support routines used to determine the vendor or device names associated
28 * with a particular device or vendor.
36 #if defined(HAVE_STRING_H)
38 #elif defined(HAVE_STRINGS_H)
42 #if defined(HAVE_INTTYPES_H)
43 # include <inttypes.h>
44 #elif defined(HAVE_STDINT_H)
48 #include "pciaccess.h"
49 #include "pciaccess_private.h"
51 #define DO_MATCH(a,b) (((a) == PCI_MATCH_ANY) || ((a) == (b)))
55 typedef gzFile pci_id_file;
58 pci_id_file_open(void)
62 result = gzopen(PCIIDS_PATH "/pci.ids.gz", "rb");
66 return gzopen(PCIIDS_PATH "/pci.ids", "rb");
69 #define pci_id_file_gets(l, s, f) gzgets(f, l, s)
70 #define pci_id_file_close(f) gzclose(f)
72 typedef FILE pci_id_file;
73 #define pci_id_file_open() fopen(PCIIDS_PATH "/pci.ids", "r")
74 #define pci_id_file_gets(l, s, f) fgets(l, s, f)
75 #define pci_id_file_close(f) fclose(f)
79 * Node for sorting vendor IDs.
81 * Each structure forms an internal node of an n-way tree. Each node selects
82 * \c pci_id_node::bits number of bits from the vendor ID. Starting from the
83 * root of the tree, a slice of the low-order bits of the vendor ID are
84 * selected and used as an index into the \c pci_id_node::children array.
86 * At the leaf nodes (i.e., the node entered when all 16 bits of the vendor ID
87 * have been used), the \c pci_id_node::children is actually an array of
88 * pointers to \c pci_id_leaf structures.
91 * Determine if there is a cleaner way (in the source code) to have the
92 * \c children array change type based on whether the node is internal or
96 * Currently \c bits is always 4. Decide if this value can ever change
97 * (i.e., to pull-up levels of the n-way tree when all the children's children
98 * are full). If it can, rip it out and hard-code it to 4 everywhere.
102 struct pci_id_node * children[16];
107 const char * vendor_name;
110 struct pci_device_leaf * devices;
113 struct pci_device_leaf {
114 struct pci_id_match id;
115 const char * device_name;
119 * Root of the PCI vendor ID search tree.
121 _pci_hidden struct pci_id_node * tree = NULL;
124 * Get a pointer to the leaf node for a vendor ID.
126 * If the vendor ID does not exist in the tree, it is added.
128 static struct pci_id_leaf *
129 insert( uint16_t vendor )
131 struct pci_id_node * n;
134 if ( tree == NULL ) {
135 tree = calloc( 1, sizeof( struct pci_id_node ) );
140 while ( n != NULL ) {
141 const unsigned used_bits = n->bits;
142 const unsigned mask = (1 << used_bits) - 1;
143 const unsigned idx = (vendor & (mask << bits)) >> bits;
152 if ( n->children[ idx ] == NULL ) {
154 struct pci_id_node * child =
155 calloc( 1, sizeof( struct pci_id_node ) );
159 n->children[ idx ] = child;
162 struct pci_id_leaf * leaf =
163 calloc( 1, sizeof( struct pci_id_leaf ) );
165 leaf->vendor = vendor;
167 n->children[ idx ] = (struct pci_id_node *) leaf;
171 n = n->children[ idx ];
174 return (struct pci_id_leaf *) n;
179 * Populate a vendor node with all the devices associated with that vendor
181 * \param vend Vendor node that is to be filled from the pci.ids file.
184 * The parsing in this function should be more rhobust. There are some error
185 * cases (i.e., a 0-tab line followed by a 2-tab line) that aren't handled
186 * correctly. I don't think there are any security problems with the code,
187 * but it's not impossible.
190 populate_vendor( struct pci_id_leaf * vend, int fill_device_data )
194 unsigned vendor = PCI_MATCH_ANY;
197 /* If the device tree for this vendor is already populated, don't do
198 * anything. This avoids wasted processing and potential memory leaks.
200 if (vend->num_devices != 0) {
204 f = pci_id_file_open();
206 /* If the pci.ids file could not be opened, there's nothing we can do.
212 while( pci_id_file_gets( buf, sizeof( buf ), f ) != NULL ) {
217 /* Each line either starts with zero, one, or two tabs followed by
218 * a series of 4 hex digits. Any lines not matching that are ignored.
221 for ( num_tabs = 0 ; num_tabs < 3 ; num_tabs++ ) {
222 if ( buf[ num_tabs ] != '\t' ) {
227 if ( !isxdigit( buf[ num_tabs + 0 ] )
228 || !isxdigit( buf[ num_tabs + 1 ] )
229 || !isxdigit( buf[ num_tabs + 2 ] )
230 || !isxdigit( buf[ num_tabs + 3 ] ) ) {
234 new_line = strchr( buf, '\n' );
235 if ( new_line != NULL ) {
239 length = strlen( buf );
240 (void) memset( buf + length, 0, sizeof( buf ) - length );
243 if ( num_tabs == 0 ) {
244 vendor = (unsigned) strtoul( & buf[ num_tabs ], NULL, 16 );
245 if ( vend->vendor == vendor ) {
246 /* vendor_name may already be set from a previous invocation
247 * of this function with fill_device_data = 0.
249 if (vend->vendor_name == NULL) {
250 vend->vendor_name = strdup( & buf[ num_tabs + 6 ] );
253 /* If we're not going to fill in all of the device data as
254 * well, then bail out now. We have all the information that
257 if ( ! fill_device_data ) {
262 else if ( vendor == vend->vendor ) {
263 struct pci_device_leaf * d;
264 struct pci_device_leaf * dev;
265 struct pci_device_leaf * last_dev;
269 d = realloc( vend->devices, (vend->num_devices + 1)
270 * sizeof( struct pci_device_leaf ) );
275 last_dev = & d[ vend->num_devices - 1 ];
276 dev = & d[ vend->num_devices ];
280 if ( num_tabs == 1 ) {
281 dev->id.vendor_id = vend->vendor;
282 dev->id.device_id = (unsigned) strtoul( & buf[ num_tabs ],
284 dev->id.subvendor_id = PCI_MATCH_ANY;
285 dev->id.subdevice_id = PCI_MATCH_ANY;
287 dev->id.device_class = 0;
288 dev->id.device_class_mask = 0;
289 dev->id.match_data = 0;
291 dev->device_name = strdup( & buf[ num_tabs + 6 ] );
294 dev->id = last_dev->id;
296 dev->id.subvendor_id= (unsigned) strtoul( & buf[ num_tabs ],
298 dev->id.subdevice_id = (unsigned) strtoul( & buf[ num_tabs + 5 ],
300 dev->device_name = strdup( & buf[ num_tabs + 5 + 6 ] );
305 pci_id_file_close( f );
310 * Find the name of the specified device.
312 * Finds the actual product name of the specified device. If a subvendor ID
313 * and subdevice ID are specified in \c m, the returned name will be the name
317 find_device_name( const struct pci_id_match * m )
319 struct pci_id_leaf * vend;
323 if ( m->vendor_id == PCI_MATCH_ANY ) {
328 vend = insert( m->vendor_id );
329 if ( vend == NULL ) {
333 if ( vend->num_devices == 0 ) {
334 populate_vendor( vend, 1 );
338 for ( i = 0 ; i < vend->num_devices ; i++ ) {
339 struct pci_device_leaf * d = & vend->devices[ i ];
341 if ( DO_MATCH( m->vendor_id, d->id.vendor_id )
342 && DO_MATCH( m->device_id, d->id.device_id )
343 && DO_MATCH( m->subvendor_id, d->id.subvendor_id )
344 && DO_MATCH( m->subdevice_id, d->id.subdevice_id ) ) {
345 return d->device_name;
354 * Find the vendor name of the specified device.
356 * Finds the actual vendor name of the specified device. If a subvendor ID
357 * and subdevice ID are specified in \c m, the returned name will be the name
358 * associated with the subvendor.
361 find_vendor_name( const struct pci_id_match * m )
363 struct pci_id_leaf * vend;
366 if ( m->vendor_id == PCI_MATCH_ANY ) {
371 vend = insert( m->vendor_id );
372 if ( vend == NULL ) {
376 if ( vend->vendor_name == NULL ) {
377 populate_vendor( vend, 0 );
381 return vend->vendor_name;
386 * Get a name based on an arbitrary PCI search structure.
389 pci_get_strings( const struct pci_id_match * m,
390 const char ** device_name,
391 const char ** vendor_name,
392 const char ** subdevice_name,
393 const char ** subvendor_name )
395 struct pci_id_match temp;
399 temp.subvendor_id = PCI_MATCH_ANY;
400 temp.subdevice_id = PCI_MATCH_ANY;
402 if ( device_name != NULL ) {
403 *device_name = find_device_name( & temp );
406 if ( vendor_name != NULL ) {
407 *vendor_name = find_vendor_name( & temp );
410 if ( subdevice_name != NULL ) {
411 *subdevice_name = find_device_name( m );
414 if ( subvendor_name != NULL ) {
415 *subvendor_name = find_vendor_name( m );
421 * Get the name associated with the device's primary device ID.
424 pci_device_get_device_name( const struct pci_device * dev )
426 struct pci_id_match m;
429 m.vendor_id = dev->vendor_id;
430 m.device_id = dev->device_id;
431 m.subvendor_id = PCI_MATCH_ANY;
432 m.subdevice_id = PCI_MATCH_ANY;
434 m.device_class_mask = 0;
437 return find_device_name( & m );
442 * Get the name associated with the device's subdevice ID.
445 pci_device_get_subdevice_name( const struct pci_device * dev )
447 struct pci_id_match m;
450 if ( (dev->subvendor_id == 0) || (dev->subdevice_id == 0) ) {
454 m.vendor_id = dev->vendor_id;
455 m.device_id = dev->device_id;
456 m.subvendor_id = dev->subvendor_id;
457 m.subdevice_id = dev->subdevice_id;
459 m.device_class_mask = 0;
462 return find_device_name( & m );
467 * Get the name associated with the device's primary vendor ID.
470 pci_device_get_vendor_name( const struct pci_device * dev )
472 struct pci_id_match m;
475 m.vendor_id = dev->vendor_id;
476 m.device_id = PCI_MATCH_ANY;
477 m.subvendor_id = PCI_MATCH_ANY;
478 m.subdevice_id = PCI_MATCH_ANY;
480 m.device_class_mask = 0;
483 return find_vendor_name( & m );
488 * Get the name associated with the device's subvendor ID.
491 pci_device_get_subvendor_name( const struct pci_device * dev )
493 struct pci_id_match m;
496 if ( dev->subvendor_id == 0 ) {
501 m.vendor_id = dev->subvendor_id;
502 m.device_id = PCI_MATCH_ANY;
503 m.subvendor_id = PCI_MATCH_ANY;
504 m.subdevice_id = PCI_MATCH_ANY;
506 m.device_class_mask = 0;
509 return find_vendor_name( & m );