xref: /libpciaccess/src/common_device_name.c (revision 27f0ffca)
1 /*
2  * (C) Copyright IBM Corporation 2006
3  * All Rights Reserved.
4  *
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:
11  *
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
14  * Software.
15  *
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.
23  */
24 
25 /**
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.
29  */
30 
31 #include "config.h"
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <ctype.h>
35 
36 #if defined(HAVE_STRING_H)
37 # include <string.h>
38 #elif defined(HAVE_STRINGS_H)
39 # include <strings.h>
40 #endif
41 
42 #if defined(HAVE_INTTYPES_H)
43 # include <inttypes.h>
44 #elif defined(HAVE_STDINT_H)
45 # include <stdint.h>
46 #endif
47 
48 #include "pciaccess.h"
49 
50 #ifndef PCIIDS_PATH
51 # define PCIIDS_PATH "/usr/share/hwdata"
52 #endif
53 
54 #define DO_MATCH(a,b)  (((a) == PCI_MATCH_ANY) || ((a) == (b)))
55 
56 /**
57  * Node for sorting vendor IDs.
58  *
59  * Each structure forms an internal node of an n-way tree.  Each node selects
60  * \c pci_id_node::bits number of bits from the vendor ID.  Starting from the
61  * root of the tree, a slice of the low-order bits of the vendor ID are
62  * selected and used as an index into the \c pci_id_node::children array.
63  *
64  * At the leaf nodes (i.e., the node entered when all 16 bits of the vendor ID
65  * have been used), the \c pci_id_node::children is actually an array of
66  * pointers to \c pci_id_leaf structures.
67  *
68  * \todo
69  * Determine if there is a cleaner way (in the source code) to have the
70  * \c children array change type based on whether the node is internal or
71  * a leaf.
72  *
73  * \todo
74  * Currently \c bits is always 4.  Decide if this value can ever change
75  * (i.e., to pull-up levels of the n-way tree when all the children's children
76  * are full).  If it can, rip it out and hard-code it to 4 everywhere.
77  */
78 struct pci_id_node {
79     unsigned bits;
80     struct pci_id_node * children[16];
81 };
82 
83 struct pci_id_leaf {
84     uint16_t     vendor;
85     const char * vendor_name;
86 
87     size_t num_devices;
88     struct pci_device_leaf * devices;
89 };
90 
91 struct pci_device_leaf {
92     struct pci_id_match   id;
93     const char * device_name;
94 };
95 
96 /**
97  * Root of the PCI vendor ID search tree.
98  */
99 struct pci_id_node * tree = NULL;
100 
101 /**
102  * Name of the file containing the PCI ID information.
103  */
104 static const char pci_id_file[] = PCIIDS_PATH "/pci.ids";
105 
106 
107 /**
108  * Get a pointer to the leaf node for a vendor ID.
109  *
110  * If the vendor ID does not exist in the tree, it is added.
111  */
112 static struct pci_id_leaf *
113 insert( uint16_t vendor )
114 {
115     struct pci_id_node * n;
116     unsigned bits = 0;
117 
118     if ( tree == NULL ) {
119 	tree = calloc( 1, sizeof( struct pci_id_node ) );
120 	tree->bits = 4;
121     }
122 
123     n = tree;
124     while ( n != NULL ) {
125 	const unsigned used_bits = n->bits;
126 	const unsigned mask = (1 << used_bits) - 1;
127 	const unsigned idx = (vendor & (mask << bits)) >> bits;
128 
129 
130 	if ( bits >= 16 ) {
131 	    break;
132 	}
133 
134 	bits += used_bits;
135 
136 	if ( n->children[ idx ] == NULL ) {
137 	    if ( bits < 16 ) {
138 		struct pci_id_node * child =
139 		    calloc( 1, sizeof( struct pci_id_node ) );
140 
141 		child->bits = 4;
142 
143 		n->children[ idx ] = child;
144 	    }
145 	    else {
146 		struct pci_id_leaf * leaf =
147 		    calloc( 1, sizeof( struct pci_id_leaf ) );
148 
149 		leaf->vendor = vendor;
150 
151 		n->children[ idx ] = (struct pci_id_node *) leaf;
152 	    }
153 	}
154 
155 	n = n->children[ idx ];
156     }
157 
158     return (struct pci_id_leaf *) n;
159 }
160 
161 
162 /**
163  * Populate a vendor node with all the devices associated with that vendor
164  *
165  * \param vend  Vendor node that is to be filled from the pci.ids file.
166  *
167  * \todo
168  * The parsing in this function should be more rhobust.  There are some error
169  * cases (i.e., a 0-tab line followed by a 2-tab line) that aren't handled
170  * correctly.  I don't think there are any security problems with the code,
171  * but it's not impossible.
172  */
173 static void
174 populate_vendor( struct pci_id_leaf * vend, int fill_device_data )
175 {
176     FILE * f = fopen( pci_id_file, "r" );
177     char buf[128];
178     unsigned vendor = PCI_MATCH_ANY;
179 
180 
181     while( fgets( buf, sizeof( buf ), f ) != NULL ) {
182 	unsigned num_tabs;
183 	char * new_line;
184 	size_t length;
185 
186 	/* Each line either starts with zero, one, or two tabs followed by
187 	 * a series of 4 hex digits.  Any lines not matching that are ignored.
188 	 */
189 
190 	for ( num_tabs = 0 ; num_tabs < 3 ; num_tabs++ ) {
191 	    if ( buf[ num_tabs ] != '\t' ) {
192 		break;
193 	    }
194 	}
195 
196 	if ( !isxdigit( buf[ num_tabs + 0 ] )
197 	     || !isxdigit( buf[ num_tabs + 1 ] )
198 	     || !isxdigit( buf[ num_tabs + 2 ] )
199 	     || !isxdigit( buf[ num_tabs + 3 ] ) ) {
200 	    continue;
201 	}
202 
203 	new_line = strchr( buf, '\n' );
204 	if ( new_line != NULL ) {
205 	    *new_line = '\0';
206 	}
207 
208 	length = strlen( buf );
209 	(void) memset( buf + length, 0, sizeof( buf ) - length );
210 
211 
212 	if ( num_tabs == 0 ) {
213 	    vendor = (unsigned) strtoul( & buf[ num_tabs ], NULL, 16 );
214 	    if ( vend->vendor == vendor ) {
215 		vend->vendor_name = strdup( & buf[ num_tabs + 6 ] );
216 
217 		/* If we're not going to fill in all of the device data as
218 		 * well, then bail out now.  We have all the information that
219 		 * we need.
220 		 */
221 		if ( ! fill_device_data ) {
222 		    break;
223 		}
224 	    }
225 	}
226 	else if ( vendor == vend->vendor ) {
227 	    struct pci_device_leaf * d;
228 	    struct pci_device_leaf * dev;
229 	    struct pci_device_leaf * last_dev;
230 
231 
232 
233 	    d = realloc( vend->devices, (vend->num_devices + 1)
234 			 * sizeof( struct pci_device_leaf ) );
235 	    if ( d == NULL ) {
236 		return;
237 	    }
238 
239 	    last_dev = & d[ vend->num_devices - 1 ];
240 	    dev = & d[ vend->num_devices ];
241 	    vend->num_devices++;
242 	    vend->devices = d;
243 
244 	    if ( num_tabs == 1 ) {
245 		dev->id.vendor_id = vend->vendor;
246 		dev->id.device_id = (unsigned) strtoul( & buf[ num_tabs ],
247 							NULL, 16 );
248 		dev->id.subvendor_id = PCI_MATCH_ANY;
249 		dev->id.subdevice_id = PCI_MATCH_ANY;
250 
251 		dev->id.device_class = 0;
252 		dev->id.device_class_mask = 0;
253 		dev->id.match_data = 0;
254 
255 		dev->device_name = strdup( & buf[ num_tabs + 6 ] );
256 	    }
257 	    else {
258 		dev->id = last_dev->id;
259 
260 		dev->id.subvendor_id= (unsigned) strtoul( & buf[ num_tabs ],
261 							  NULL, 16 );
262 		dev->id.subdevice_id = (unsigned) strtoul( & buf[ num_tabs + 5 ],
263 							   NULL, 16 );
264 		dev->device_name = strdup( & buf[ num_tabs + 5 + 6 ] );
265 	    }
266 	}
267     }
268 
269     fclose( f );
270 }
271 
272 
273 /**
274  * Find the name of the specified device.
275  *
276  * Finds the actual product name of the specified device.  If a subvendor ID
277  * and subdevice ID are specified in \c m, the returned name will be the name
278  * of the subdevice.
279  */
280 static const char *
281 find_device_name( const struct pci_id_match * m )
282 {
283     struct pci_id_leaf * vend;
284     unsigned i;
285 
286 
287     if ( m->vendor_id == PCI_MATCH_ANY ) {
288 	return NULL;
289     }
290 
291 
292     vend = insert( m->vendor_id );
293     if ( vend == NULL ) {
294 	return NULL;
295     }
296 
297     if ( vend->num_devices == 0 ) {
298 	populate_vendor( vend, 1 );
299     }
300 
301 
302     for ( i = 0 ; i < vend->num_devices ; i++ ) {
303 	struct pci_device_leaf * d = & vend->devices[ i ];
304 
305 	if ( DO_MATCH( m->vendor_id, d->id.vendor_id )
306 	     && DO_MATCH( m->device_id, d->id.device_id )
307 	     && DO_MATCH( m->subvendor_id, d->id.subvendor_id )
308 	     && DO_MATCH( m->subdevice_id, d->id.subdevice_id ) ) {
309 	    return d->device_name;
310 	}
311     }
312 
313     return NULL;
314 }
315 
316 
317 /**
318  * Find the vendor name of the specified device.
319  *
320  * Finds the actual vendor name of the specified device.  If a subvendor ID
321  * and subdevice ID are specified in \c m, the returned name will be the name
322  * associated with the subvendor.
323  */
324 static const char *
325 find_vendor_name( const struct pci_id_match * m )
326 {
327     struct pci_id_leaf * vend;
328 
329 
330     if ( m->vendor_id == PCI_MATCH_ANY ) {
331 	return NULL;
332     }
333 
334 
335     vend = insert( m->vendor_id );
336     if ( vend == NULL ) {
337 	return NULL;
338     }
339 
340     if ( vend->vendor_name == NULL ) {
341 	populate_vendor( vend, 0 );
342     }
343 
344 
345     return vend->vendor_name;
346 }
347 
348 
349 /**
350  * Get a name based on an arbitrary PCI search structure.
351  */
352 void
353 pci_get_strings( const struct pci_id_match * m,
354 		 const char ** device_name,
355 		 const char ** vendor_name,
356 		 const char ** subdevice_name,
357 		 const char ** subvendor_name )
358 {
359     struct pci_id_match  temp;
360 
361 
362     temp = *m;
363     temp.subvendor_id = PCI_MATCH_ANY;
364     temp.subdevice_id = PCI_MATCH_ANY;
365 
366     if ( device_name != NULL ) {
367 	*device_name = find_device_name( & temp );
368     }
369 
370     if ( vendor_name != NULL ) {
371 	*vendor_name = find_vendor_name( & temp );
372     }
373 
374     if ( subdevice_name != NULL ) {
375 	*subdevice_name = find_device_name( m );
376     }
377 
378     if ( subvendor_name != NULL ) {
379 	*subvendor_name = find_vendor_name( m );
380     }
381 }
382 
383 
384 /**
385  * Get the name associated with the device's primary device ID.
386  */
387 const char *
388 pci_device_get_device_name( const struct pci_device * dev )
389 {
390     struct pci_id_match m;
391 
392 
393     m.vendor_id = dev->vendor_id;
394     m.device_id = dev->device_id;
395     m.subvendor_id = PCI_MATCH_ANY;
396     m.subdevice_id = PCI_MATCH_ANY;
397     m.device_class = 0;
398     m.device_class_mask = 0;
399     m.match_data = 0;
400 
401     return find_device_name( & m );
402 }
403 
404 
405 /**
406  * Get the name associated with the device's subdevice ID.
407  */
408 const char *
409 pci_device_get_subdevice_name( const struct pci_device * dev )
410 {
411     struct pci_id_match m;
412 
413 
414     if ( (dev->subvendor_id == 0) || (dev->subdevice_id == 0) ) {
415 	return NULL;
416     }
417 
418     m.vendor_id = dev->vendor_id;
419     m.device_id = dev->device_id;
420     m.subvendor_id = dev->subvendor_id;
421     m.subdevice_id = dev->subdevice_id;
422     m.device_class = 0;
423     m.device_class_mask = 0;
424     m.match_data = 0;
425 
426     return find_device_name( & m );
427 }
428 
429 
430 /**
431  * Get the name associated with the device's primary vendor ID.
432  */
433 const char *
434 pci_device_get_vendor_name( const struct pci_device * dev )
435 {
436     struct pci_id_match m;
437 
438 
439     m.vendor_id = dev->vendor_id;
440     m.device_id = PCI_MATCH_ANY;
441     m.subvendor_id = PCI_MATCH_ANY;
442     m.subdevice_id = PCI_MATCH_ANY;
443     m.device_class = 0;
444     m.device_class_mask = 0;
445     m.match_data = 0;
446 
447     return find_vendor_name( & m );
448 }
449 
450 
451 /**
452  * Get the name associated with the device's subvendor ID.
453  */
454 const char *
455 pci_device_get_subvendor_name( const struct pci_device * dev )
456 {
457     struct pci_id_match m;
458 
459 
460     if ( dev->subvendor_id == 0 ) {
461 	return NULL;
462     }
463 
464 
465     m.vendor_id = dev->subvendor_id;
466     m.device_id = PCI_MATCH_ANY;
467     m.subvendor_id = PCI_MATCH_ANY;
468     m.subdevice_id = PCI_MATCH_ANY;
469     m.device_class = 0;
470     m.device_class_mask = 0;
471     m.match_data = 0;
472 
473     return find_vendor_name( & m );
474 }
475