]> mj.ucw.cz Git - pciutils.git/blobdiff - lib/sysfs.c
libpci: hwdb: Remove ID_SUBSYSTEM and ID_GEN_SUBSYSTEM usage from pci_id_hwdb_lookup()
[pciutils.git] / lib / sysfs.c
index fe3f0e5ece534369d833ba73ece7861b901fea07..0e763dcbb9124e532c7863cdf60580583f3c1b64 100644 (file)
@@ -2,9 +2,11 @@
  *     The PCI Library -- Configuration Access via /sys/bus/pci
  *
  *     Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
- *     Copyright (c) 1997--2008 Martin Mares <mj@ucw.cz>
+ *     Copyright (c) 1997--2023 Martin Mares <mj@ucw.cz>
  *
- *     Can be freely distributed and used under the terms of the GNU GPL.
+ *     Can be freely distributed and used under the terms of the GNU GPL v2+.
+ *
+ *     SPDX-License-Identifier: GPL-2.0-or-later
  */
 
 #define _GNU_SOURCE
@@ -20,7 +22,6 @@
 #include <sys/types.h>
 
 #include "internal.h"
-#include "pread.h"
 
 static void
 sysfs_config(struct pci_access *a)
@@ -104,12 +105,13 @@ sysfs_get_string(struct pci_dev *d, char *object, char *buf, int mandatory)
       return 0;
     }
   n = read(fd, buf, OBJBUFSIZE);
+  int read_errno = errno;
   close(fd);
   if (n < 0)
     {
-      warn("Error reading %s: %s", namebuf, strerror(errno));
+      warn("Error reading %s: %s", namebuf, strerror(read_errno));
       return 0;
-     }
+    }
   if (n >= OBJBUFSIZE)
     {
       warn("Value in %s too long", namebuf);
@@ -333,6 +335,8 @@ sysfs_fill_slots(struct pci_access *a)
 static void
 sysfs_fill_info(struct pci_dev *d, unsigned int flags)
 {
+  int value, want_class, want_class_ext;
+
   if (!d->access->buscentric)
     {
       /*
@@ -345,12 +349,79 @@ sysfs_fill_info(struct pci_dev *d, unsigned int flags)
          d->vendor_id = sysfs_get_value(d, "vendor", 1);
          d->device_id = sysfs_get_value(d, "device", 1);
        }
-      if (want_fill(d, flags, PCI_FILL_CLASS))
-       d->device_class = sysfs_get_value(d, "class", 1) >> 8;
+      want_class = want_fill(d, flags, PCI_FILL_CLASS);
+      want_class_ext = want_fill(d, flags, PCI_FILL_CLASS_EXT);
+      if (want_class || want_class_ext)
+        {
+         value = sysfs_get_value(d, "class", 1);
+         if (want_class)
+           d->device_class = value >> 8;
+         if (want_class_ext)
+           {
+             d->prog_if = value & 0xff;
+             value = sysfs_get_value(d, "revision", 0);
+             if (value < 0)
+               value = pci_read_byte(d, PCI_REVISION_ID);
+             if (value >= 0)
+               d->rev_id = value;
+           }
+       }
+      if (want_fill(d, flags, PCI_FILL_SUBSYS))
+       {
+         value = sysfs_get_value(d, "subsystem_vendor", 0);
+         if (value >= 0)
+           {
+             d->subsys_vendor_id = value;
+             value = sysfs_get_value(d, "subsystem_device", 0);
+             if (value >= 0)
+               d->subsys_id = value;
+           }
+         else
+           clear_fill(d, PCI_FILL_SUBSYS);
+       }
       if (want_fill(d, flags, PCI_FILL_IRQ))
          d->irq = sysfs_get_value(d, "irq", 1);
       if (want_fill(d, flags, PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES | PCI_FILL_IO_FLAGS | PCI_FILL_BRIDGE_BASES))
          sysfs_get_resources(d);
+      if (want_fill(d, flags, PCI_FILL_PARENT))
+       {
+         unsigned int domain, bus, dev, func;
+         char *path_abs, *path_canon, *name;
+         char path_rel[OBJNAMELEN];
+         struct pci_dev *parent;
+
+         /* Construct sysfs path for parent device */
+         sysfs_obj_name(d, "..", path_rel);
+         path_abs = realpath(path_rel, NULL);
+         name = path_abs ? strrchr(path_abs, '/') : NULL;
+         name = name ? name+1 : name;
+         parent = NULL;
+
+         if (name && sscanf(name, "%x:%x:%x.%d", &domain, &bus, &dev, &func) == 4 && domain <= 0x7fffffff)
+           for (parent = d->access->devices; parent; parent = parent->next)
+             if (parent->domain == (int)domain && parent->bus == bus && parent->dev == dev && parent->func == func)
+               break;
+
+         if (parent)
+           {
+             /* Check if parsed BDF address from parent sysfs device is really expected PCI device */
+             sysfs_obj_name(parent, ".", path_rel);
+             path_canon = realpath(path_rel, NULL);
+             if (!path_canon || strcmp(path_canon, path_abs) != 0)
+               parent = NULL;
+
+             if (path_canon)
+               free(path_canon);
+           }
+
+         if (parent)
+           d->parent = parent;
+         else
+           clear_fill(d, PCI_FILL_PARENT);
+
+         if (path_abs)
+           free(path_abs);
+       }
     }
 
   if (want_fill(d, flags, PCI_FILL_PHYS_SLOT))
@@ -398,6 +469,20 @@ sysfs_fill_info(struct pci_dev *d, unsigned int flags)
        }
     }
 
+  if (want_fill(d, flags, PCI_FILL_DRIVER))
+    {
+      char *driver_path = sysfs_deref_link(d, "driver");
+      if (driver_path)
+        {
+          char *driver = strrchr(driver_path, '/');
+          driver = driver ? driver+1 : driver_path;
+          pci_set_property(d, PCI_FILL_DRIVER, driver);
+          free(driver_path);
+        }
+      else
+        clear_fill(d, PCI_FILL_DRIVER);
+    }
+
   pci_generic_fill_info(d, flags);
 }
 
@@ -439,7 +524,6 @@ sysfs_setup(struct pci_dev *d, int intent)
       a->fd = open(namebuf, a->fd_rw ? O_RDWR : O_RDONLY);
       if (a->fd < 0)
        a->warning("Cannot open %s", namebuf);
-      a->fd_pos = 0;
     }
   return a->fd;
 }
@@ -451,7 +535,7 @@ static int sysfs_read(struct pci_dev *d, int pos, byte *buf, int len)
 
   if (fd < 0)
     return 0;
-  res = do_read(d, fd, buf, len, pos);
+  res = pread(fd, buf, len, pos);
   if (res < 0)
     {
       d->access->warning("sysfs_read: read failed: %s", strerror(errno));
@@ -469,7 +553,7 @@ static int sysfs_write(struct pci_dev *d, int pos, byte *buf, int len)
 
   if (fd < 0)
     return 0;
-  res = do_write(d, fd, buf, len, pos);
+  res = pwrite(fd, buf, len, pos);
   if (res < 0)
     {
       d->access->warning("sysfs_write: write failed: %s", strerror(errno));
@@ -483,17 +567,6 @@ static int sysfs_write(struct pci_dev *d, int pos, byte *buf, int len)
   return 1;
 }
 
-#ifdef PCI_HAVE_DO_READ
-
-/* pread() is not available and do_read() only works for a single fd, so we
- * cannot implement read_vpd properly. */
-static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
-{
-  return 0;
-}
-
-#else /* !PCI_HAVE_DO_READ */
-
 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
 {
   int fd = sysfs_setup(d, SETUP_READ_VPD);
@@ -512,8 +585,6 @@ static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
   return 1;
 }
 
-#endif /* PCI_HAVE_DO_READ */
-
 static void sysfs_cleanup_dev(struct pci_dev *d)
 {
   struct pci_access *a = d->access;
@@ -523,17 +594,16 @@ static void sysfs_cleanup_dev(struct pci_dev *d)
 }
 
 struct pci_methods pm_linux_sysfs = {
-  "linux-sysfs",
-  "The sys filesystem on Linux",
-  sysfs_config,
-  sysfs_detect,
-  sysfs_init,
-  sysfs_cleanup,
-  sysfs_scan,
-  sysfs_fill_info,
-  sysfs_read,
-  sysfs_write,
-  sysfs_read_vpd,
-  NULL,                                        /* init_dev */
-  sysfs_cleanup_dev
+  .name = "linux-sysfs",
+  .help = "The sys filesystem on Linux",
+  .config = sysfs_config,
+  .detect = sysfs_detect,
+  .init = sysfs_init,
+  .cleanup = sysfs_cleanup,
+  .scan = sysfs_scan,
+  .fill_info = sysfs_fill_info,
+  .read = sysfs_read,
+  .write = sysfs_write,
+  .read_vpd = sysfs_read_vpd,
+  .cleanup_dev = sysfs_cleanup_dev,
 };