xref: /libpciaccess/src/linux_sysfs.c (revision 5d1bdf0c)
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 linux_sysfs.c
27  * Access PCI subsystem using Linux's sysfs interface.  This interface is
28  * available starting somewhere in the late 2.5.x kernel phase, and is the
29  * preferred method on all 2.6.x kernels.
30  *
31  * \author Ian Romanick <[email protected]>
32  */
33 
34 #define _GNU_SOURCE
35 
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdio.h>
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43 #include <sys/mman.h>
44 #include <dirent.h>
45 #include <errno.h>
46 
47 #include "config.h"
48 
49 #ifdef HAVE_MTRR
50 #include <asm/mtrr.h>
51 #include <sys/ioctl.h>
52 #endif
53 
54 #include "pciaccess.h"
55 #include "pciaccess_private.h"
56 #include "linux_devmem.h"
57 
58 static void pci_device_linux_sysfs_enable(struct pci_device *dev);
59 
60 static int pci_device_linux_sysfs_read_rom( struct pci_device * dev,
61     void * buffer );
62 
63 static int pci_device_linux_sysfs_probe( struct pci_device * dev );
64 
65 static int pci_device_linux_sysfs_map_range(struct pci_device *dev,
66     struct pci_device_mapping *map);
67 
68 static int pci_device_linux_sysfs_unmap_range(struct pci_device *dev,
69     struct pci_device_mapping *map);
70 
71 static int pci_device_linux_sysfs_read( struct pci_device * dev, void * data,
72     pciaddr_t offset, pciaddr_t size, pciaddr_t * bytes_read );
73 
74 static int pci_device_linux_sysfs_write( struct pci_device * dev,
75     const void * data, pciaddr_t offset, pciaddr_t size,
76     pciaddr_t * bytes_written );
77 
78 static int pci_device_linux_sysfs_boot_vga( struct pci_device * dev );
79 static int pci_device_linux_sysfs_has_kernel_driver(struct pci_device *dev);
80 
81 static const struct pci_system_methods linux_sysfs_methods = {
82     .destroy = NULL,
83     .destroy_device = NULL,
84     .read_rom = pci_device_linux_sysfs_read_rom,
85     .probe = pci_device_linux_sysfs_probe,
86     .map_range = pci_device_linux_sysfs_map_range,
87     .unmap_range = pci_device_linux_sysfs_unmap_range,
88 
89     .read = pci_device_linux_sysfs_read,
90     .write = pci_device_linux_sysfs_write,
91 
92     .fill_capabilities = pci_fill_capabilities_generic,
93     .enable = pci_device_linux_sysfs_enable,
94     .boot_vga = pci_device_linux_sysfs_boot_vga,
95     .has_kernel_driver = pci_device_linux_sysfs_has_kernel_driver,
96 };
97 
98 #define SYS_BUS_PCI "/sys/bus/pci/devices"
99 
100 
101 static int populate_entries(struct pci_system * pci_sys);
102 
103 
104 /**
105  * Attempt to access PCI subsystem using Linux's sysfs interface.
106  */
107 _pci_hidden int
108 pci_system_linux_sysfs_create( void )
109 {
110     int err = 0;
111     struct stat st;
112 
113 
114     /* If the directory "/sys/bus/pci/devices" exists, then the PCI subsystem
115      * can be accessed using this interface.
116      */
117 
118     if ( stat( SYS_BUS_PCI, & st ) == 0 ) {
119 	pci_sys = calloc( 1, sizeof( struct pci_system ) );
120 	if ( pci_sys != NULL ) {
121 	    pci_sys->methods = & linux_sysfs_methods;
122 #ifdef HAVE_MTRR
123 	    pci_sys->mtrr_fd = open("/proc/mtrr", O_WRONLY);
124 #endif
125 	    err = populate_entries(pci_sys);
126 	}
127 	else {
128 	    err = ENOMEM;
129 	}
130     }
131     else {
132 	err = errno;
133     }
134 
135     return err;
136 }
137 
138 
139 /**
140  * Filter out the names "." and ".." from the scanned sysfs entries.
141  *
142  * \param d  Directory entry being processed by \c scandir.
143  *
144  * \return
145  * Zero if the entry name matches either "." or "..", non-zero otherwise.
146  *
147  * \sa scandir, populate_entries
148  */
149 static int
150 scan_sys_pci_filter( const struct dirent * d )
151 {
152     return !((strcmp( d->d_name, "." ) == 0)
153 	     || (strcmp( d->d_name, ".." ) == 0));
154 }
155 
156 
157 int
158 populate_entries( struct pci_system * p )
159 {
160     struct dirent ** devices;
161     int n;
162     int i;
163     int err = 0;
164 
165 
166     n = scandir( SYS_BUS_PCI, & devices, scan_sys_pci_filter, alphasort );
167     if ( n > 0 ) {
168 	p->num_devices = n;
169 	p->devices = calloc( n, sizeof( struct pci_device_private ) );
170 
171 	if (p->devices != NULL) {
172 	    for (i = 0 ; i < n ; i++) {
173 		uint8_t config[48];
174 		pciaddr_t bytes;
175 		unsigned dom, bus, dev, func;
176 		struct pci_device_private *device =
177 			(struct pci_device_private *) &p->devices[i];
178 
179 
180 		sscanf(devices[i]->d_name, "%04x:%02x:%02x.%1u",
181 		       & dom, & bus, & dev, & func);
182 
183 		device->base.domain = dom;
184 		device->base.bus = bus;
185 		device->base.dev = dev;
186 		device->base.func = func;
187 
188 
189 		err = pci_device_linux_sysfs_read(& device->base, config, 0,
190 						  48, & bytes);
191 		if ((bytes == 48) && !err) {
192 		    device->base.vendor_id = (uint16_t)config[0]
193 			+ ((uint16_t)config[1] << 8);
194 		    device->base.device_id = (uint16_t)config[2]
195 			+ ((uint16_t)config[3] << 8);
196 		    device->base.device_class = (uint32_t)config[9]
197 			+ ((uint32_t)config[10] << 8)
198 			+ ((uint32_t)config[11] << 16);
199 		    device->base.revision = config[8];
200 		    device->base.subvendor_id = (uint16_t)config[44]
201 			+ ((uint16_t)config[45] << 8);
202 		    device->base.subdevice_id = (uint16_t)config[46]
203 			+ ((uint16_t)config[47] << 8);
204 		}
205 
206 		if (err) {
207 		    break;
208 		}
209 	    }
210 	}
211 	else {
212 	    err = ENOMEM;
213 	}
214     }
215 
216     if (err) {
217 	free(p->devices);
218 	p->devices = NULL;
219     }
220 
221     return err;
222 }
223 
224 
225 static int
226 pci_device_linux_sysfs_probe( struct pci_device * dev )
227 {
228     char     name[256];
229     uint8_t  config[256];
230     char     resource[512];
231     int fd;
232     pciaddr_t bytes;
233     unsigned i;
234     int err;
235 
236 
237     err = pci_device_linux_sysfs_read( dev, config, 0, 256, & bytes );
238     if ( bytes >= 64 ) {
239 	struct pci_device_private *priv = (struct pci_device_private *) dev;
240 
241 	dev->irq = config[60];
242 	priv->header_type = config[14];
243 
244 
245 	/* The PCI config registers can be used to obtain information
246 	 * about the memory and I/O regions for the device.  However,
247 	 * doing so requires some tricky parsing (to correctly handle
248 	 * 64-bit memory regions) and requires writing to the config
249 	 * registers.  Since we'd like to avoid having to deal with the
250 	 * parsing issues and non-root users can write to PCI config
251 	 * registers, we use a different file in the device's sysfs
252 	 * directory called "resource".
253 	 *
254 	 * The resource file contains all of the needed information in
255 	 * a format that is consistent across all platforms.  Each BAR
256 	 * and the expansion ROM have a single line of data containing
257 	 * 3, 64-bit hex values:  the first address in the region,
258 	 * the last address in the region, and the region's flags.
259 	 */
260 	snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/resource",
261 		  SYS_BUS_PCI,
262 		  dev->domain,
263 		  dev->bus,
264 		  dev->dev,
265 		  dev->func );
266 	fd = open( name, O_RDONLY );
267 	if ( fd != -1 ) {
268 	    char * next;
269 	    pciaddr_t  low_addr;
270 	    pciaddr_t  high_addr;
271 	    pciaddr_t  flags;
272 
273 
274 	    bytes = read( fd, resource, 512 );
275 	    resource[511] = '\0';
276 
277 	    close( fd );
278 
279 	    next = resource;
280 	    for ( i = 0 ; i < 6 ; i++ ) {
281 
282 		dev->regions[i].base_addr = strtoull( next, & next, 16 );
283 		high_addr = strtoull( next, & next, 16 );
284 		flags = strtoull( next, & next, 16 );
285 
286 		if ( dev->regions[i].base_addr != 0 ) {
287 		    dev->regions[i].size = (high_addr
288 					    - dev->regions[i].base_addr) + 1;
289 
290 		    dev->regions[i].is_IO = (flags & 0x01);
291 		    dev->regions[i].is_64 = (flags & 0x04);
292 		    dev->regions[i].is_prefetchable = (flags & 0x08);
293 		}
294 	    }
295 
296 	    low_addr = strtoull( next, & next, 16 );
297 	    high_addr = strtoull( next, & next, 16 );
298 	    flags = strtoull( next, & next, 16 );
299 	    if ( low_addr != 0 ) {
300 		priv->rom_base = low_addr;
301 		dev->rom_size = (high_addr - low_addr) + 1;
302 	    }
303 	}
304     }
305 
306     return err;
307 }
308 
309 
310 static int
311 pci_device_linux_sysfs_read_rom( struct pci_device * dev, void * buffer )
312 {
313     char name[256];
314     int fd;
315     struct stat  st;
316     int err = 0;
317     size_t rom_size;
318     size_t total_bytes;
319 
320 
321     snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/rom",
322 	      SYS_BUS_PCI,
323 	      dev->domain,
324 	      dev->bus,
325 	      dev->dev,
326 	      dev->func );
327 
328     fd = open( name, O_RDWR );
329     if ( fd == -1 ) {
330 	/* If reading the ROM using sysfs fails, fall back to the old
331 	 * /dev/mem based interface.
332 	 */
333 	return pci_device_linux_devmem_read_rom(dev, buffer);
334     }
335 
336 
337     if ( fstat( fd, & st ) == -1 ) {
338 	close( fd );
339 	return errno;
340     }
341 
342     rom_size = st.st_size;
343     if ( rom_size == 0 )
344 	rom_size = 0x10000;
345 
346     /* This is a quirky thing on Linux.  Even though the ROM and the file
347      * for the ROM in sysfs are read-only, the string "1" must be written to
348      * the file to enable the ROM.  After the data has been read, "0" must be
349      * written to the file to disable the ROM.
350      */
351     write( fd, "1", 1 );
352     lseek( fd, 0, SEEK_SET );
353 
354     for ( total_bytes = 0 ; total_bytes < rom_size ; /* empty */ ) {
355 	const int bytes = read( fd, (char *) buffer + total_bytes,
356 				rom_size - total_bytes );
357 	if ( bytes == -1 ) {
358 	    err = errno;
359 	    break;
360 	}
361 	else if ( bytes == 0 ) {
362 	    break;
363 	}
364 
365 	total_bytes += bytes;
366     }
367 
368 
369     lseek( fd, 0, SEEK_SET );
370     write( fd, "0", 1 );
371 
372     close( fd );
373     return err;
374 }
375 
376 
377 static int
378 pci_device_linux_sysfs_read( struct pci_device * dev, void * data,
379 			     pciaddr_t offset, pciaddr_t size,
380 			     pciaddr_t * bytes_read )
381 {
382     char name[256];
383     pciaddr_t temp_size = size;
384     int err = 0;
385     int fd;
386     char *data_bytes = data;
387 
388     if ( bytes_read != NULL ) {
389 	*bytes_read = 0;
390     }
391 
392     /* Each device has a directory under sysfs.  Within that directory there
393      * is a file named "config".  This file used to access the PCI config
394      * space.  It is used here to obtain most of the information about the
395      * device.
396      */
397     snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/config",
398 	      SYS_BUS_PCI,
399 	      dev->domain,
400 	      dev->bus,
401 	      dev->dev,
402 	      dev->func );
403 
404     fd = open( name, O_RDONLY );
405     if ( fd == -1 ) {
406 	return errno;
407     }
408 
409 
410     while ( temp_size > 0 ) {
411 	const ssize_t bytes = pread64( fd, data_bytes, temp_size, offset );
412 
413 	/* If zero bytes were read, then we assume it's the end of the
414 	 * config file.
415 	 */
416 	if ( bytes <= 0 ) {
417 	    err = errno;
418 	    break;
419 	}
420 
421 	temp_size -= bytes;
422 	offset += bytes;
423 	data_bytes += bytes;
424     }
425 
426     if ( bytes_read != NULL ) {
427 	*bytes_read = size - temp_size;
428     }
429 
430     close( fd );
431     return err;
432 }
433 
434 
435 static int
436 pci_device_linux_sysfs_write( struct pci_device * dev, const void * data,
437 			     pciaddr_t offset, pciaddr_t size,
438 			     pciaddr_t * bytes_written )
439 {
440     char name[256];
441     pciaddr_t temp_size = size;
442     int err = 0;
443     int fd;
444     const char *data_bytes = data;
445 
446     if ( bytes_written != NULL ) {
447 	*bytes_written = 0;
448     }
449 
450     /* Each device has a directory under sysfs.  Within that directory there
451      * is a file named "config".  This file used to access the PCI config
452      * space.  It is used here to obtain most of the information about the
453      * device.
454      */
455     snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/config",
456 	      SYS_BUS_PCI,
457 	      dev->domain,
458 	      dev->bus,
459 	      dev->dev,
460 	      dev->func );
461 
462     fd = open( name, O_WRONLY );
463     if ( fd == -1 ) {
464 	return errno;
465     }
466 
467 
468     while ( temp_size > 0 ) {
469 	const ssize_t bytes = pwrite64( fd, data_bytes, temp_size, offset );
470 
471 	/* If zero bytes were written, then we assume it's the end of the
472 	 * config file.
473 	 */
474 	if ( bytes <= 0 ) {
475 	    err = errno;
476 	    break;
477 	}
478 
479 	temp_size -= bytes;
480 	offset += bytes;
481 	data_bytes += bytes;
482     }
483 
484     if ( bytes_written != NULL ) {
485 	*bytes_written = size - temp_size;
486     }
487 
488     close( fd );
489     return err;
490 }
491 
492 static int
493 pci_device_linux_sysfs_map_range_wc(struct pci_device *dev,
494 				    struct pci_device_mapping *map)
495 {
496     char name[256];
497     int fd;
498     const int prot = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
499         ? (PROT_READ | PROT_WRITE) : PROT_READ;
500     const int open_flags = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
501         ? O_RDWR : O_RDONLY;
502     const off_t offset = map->base - dev->regions[map->region].base_addr;
503 
504     snprintf(name, 255, "%s/%04x:%02x:%02x.%1u/resource%u_wc",
505 	     SYS_BUS_PCI,
506 	     dev->domain,
507 	     dev->bus,
508 	     dev->dev,
509 	     dev->func,
510 	     map->region);
511     fd = open(name, open_flags);
512     if (fd == -1)
513 	    return errno;
514 
515     map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
516     if (map->memory == MAP_FAILED) {
517         map->memory = NULL;
518 	close(fd);
519 	return errno;
520     }
521 
522     close(fd);
523 
524     return 0;
525 }
526 
527 /**
528  * Map a memory region for a device using the Linux sysfs interface.
529  *
530  * \param dev   Device whose memory region is to be mapped.
531  * \param map   Parameters of the mapping that is to be created.
532  *
533  * \return
534  * Zero on success or an \c errno value on failure.
535  *
536  * \sa pci_device_map_rrange, pci_device_linux_sysfs_unmap_range
537  *
538  * \todo
539  * Some older 2.6.x kernels don't implement the resourceN files.  On those
540  * systems /dev/mem must be used.  On these systems it is also possible that
541  * \c mmap64 may need to be used.
542  */
543 static int
544 pci_device_linux_sysfs_map_range(struct pci_device *dev,
545                                  struct pci_device_mapping *map)
546 {
547     char name[256];
548     int fd;
549     int err = 0;
550     const int prot = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
551         ? (PROT_READ | PROT_WRITE) : PROT_READ;
552     const int open_flags = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
553         ? O_RDWR : O_RDONLY;
554     const off_t offset = map->base - dev->regions[map->region].base_addr;
555 #ifdef HAVE_MTRR
556     struct mtrr_sentry sentry = {
557 	.base = map->base,
558         .size = map->size,
559 	.type = MTRR_TYPE_UNCACHABLE
560     };
561 #endif
562 
563     /* For WC mappings, try sysfs resourceN_wc file first */
564     if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) &&
565 	!pci_device_linux_sysfs_map_range_wc(dev, map))
566 	    return 0;
567 
568     snprintf(name, 255, "%s/%04x:%02x:%02x.%1u/resource%u",
569              SYS_BUS_PCI,
570              dev->domain,
571              dev->bus,
572              dev->dev,
573              dev->func,
574              map->region);
575 
576     fd = open(name, open_flags);
577     if (fd == -1) {
578         return errno;
579     }
580 
581 
582     map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
583     if (map->memory == MAP_FAILED) {
584         map->memory = NULL;
585 	close(fd);
586 	return errno;
587     }
588 
589 #ifdef HAVE_MTRR
590     if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) != 0) {
591         sentry.type = MTRR_TYPE_WRBACK;
592     } else if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) != 0) {
593         sentry.type = MTRR_TYPE_WRCOMB;
594     }
595 
596     if (pci_sys->mtrr_fd != -1 && sentry.type != MTRR_TYPE_UNCACHABLE) {
597 	if (ioctl(pci_sys->mtrr_fd, MTRRIOC_ADD_ENTRY, &sentry) < 0) {
598 	    /* FIXME: Should we report an error in this case?
599 	     */
600 	    fprintf(stderr, "error setting MTRR "
601 		    "(base = 0x%08lx, size = 0x%08x, type = %u) %s (%d)\n",
602 		    sentry.base, sentry.size, sentry.type,
603 		    strerror(errno), errno);
604 /*            err = errno;*/
605 	}
606 	/* KLUDGE ALERT -- rewrite the PTEs to turn off the CD and WT bits */
607 	mprotect (map->memory, map->size, PROT_NONE);
608 	err = mprotect (map->memory, map->size, PROT_READ|PROT_WRITE);
609 
610 	if (err != 0) {
611 	    fprintf(stderr, "mprotect(PROT_READ | PROT_WRITE) failed: %s\n",
612 		    strerror(errno));
613 	    fprintf(stderr, "remapping without mprotect performance kludge.\n");
614 
615 	    munmap(map->memory, map->size);
616 	    map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
617 	    if (map->memory == MAP_FAILED) {
618 		map->memory = NULL;
619 		close(fd);
620 		return errno;
621 	    }
622 	}
623     }
624 #endif
625 
626     close(fd);
627 
628     return 0;
629 }
630 
631 /**
632  * Unmap a memory region for a device using the Linux sysfs interface.
633  *
634  * \param dev   Device whose memory region is to be unmapped.
635  * \param map   Parameters of the mapping that is to be destroyed.
636  *
637  * \return
638  * Zero on success or an \c errno value on failure.
639  *
640  * \sa pci_device_map_rrange, pci_device_linux_sysfs_map_range
641  *
642  * \todo
643  * Some older 2.6.x kernels don't implement the resourceN files.  On those
644  * systems /dev/mem must be used.  On these systems it is also possible that
645  * \c mmap64 may need to be used.
646  */
647 static int
648 pci_device_linux_sysfs_unmap_range(struct pci_device *dev,
649 				   struct pci_device_mapping *map)
650 {
651     int err = 0;
652 #ifdef HAVE_MTRR
653     struct mtrr_sentry sentry = {
654 	.base = map->base,
655         .size = map->size,
656 	.type = MTRR_TYPE_UNCACHABLE
657     };
658 #endif
659 
660     err = pci_device_generic_unmap_range (dev, map);
661     if (err)
662 	return err;
663 
664 #ifdef HAVE_MTRR
665     if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) != 0) {
666         sentry.type = MTRR_TYPE_WRBACK;
667     } else if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) != 0) {
668         sentry.type = MTRR_TYPE_WRCOMB;
669     }
670 
671     if (pci_sys->mtrr_fd != -1 && sentry.type != MTRR_TYPE_UNCACHABLE) {
672 	if (ioctl(pci_sys->mtrr_fd, MTRRIOC_DEL_ENTRY, &sentry) < 0) {
673 	    /* FIXME: Should we report an error in this case?
674 	     */
675 	    fprintf(stderr, "error setting MTRR "
676 		    "(base = 0x%08lx, size = 0x%08x, type = %u) %s (%d)\n",
677 		    sentry.base, sentry.size, sentry.type,
678 		    strerror(errno), errno);
679 /*            err = errno;*/
680 	}
681     }
682 #endif
683 
684     return err;
685 }
686 
687 static void pci_device_linux_sysfs_enable(struct pci_device *dev)
688 {
689     char name[256];
690     int fd;
691 
692     snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/enable",
693 	      SYS_BUS_PCI,
694 	      dev->domain,
695 	      dev->bus,
696 	      dev->dev,
697 	      dev->func );
698 
699     fd = open( name, O_RDWR );
700     if (fd == -1)
701        return;
702 
703     write( fd, "1", 1 );
704     close(fd);
705 }
706 
707 static int pci_device_linux_sysfs_boot_vga(struct pci_device *dev)
708 {
709     char name[256];
710     char reply[3];
711     int fd, bytes_read;
712     int ret = 0;
713 
714     snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/boot_vga",
715 	      SYS_BUS_PCI,
716 	      dev->domain,
717 	      dev->bus,
718 	      dev->dev,
719 	      dev->func );
720 
721     fd = open( name, O_RDONLY );
722     if (fd == -1)
723        return 0;
724 
725     bytes_read = read(fd, reply, 1);
726     if (bytes_read != 1)
727 	goto out;
728     if (reply[0] == '1')
729 	ret = 1;
730 out:
731     close(fd);
732     return ret;
733 }
734 
735 static int pci_device_linux_sysfs_has_kernel_driver(struct pci_device *dev)
736 {
737     char name[256];
738     struct stat dummy;
739     int ret;
740 
741     snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/driver",
742 	      SYS_BUS_PCI,
743 	      dev->domain,
744 	      dev->bus,
745 	      dev->dev,
746 	      dev->func );
747 
748     ret = stat(name, &dummy);
749     if (ret < 0)
750 	return 0;
751     return 1;
752 }
753