首页 > 解决方案 > 以编程方式获取 Linux 中的 USB 主机控制器参数

问题描述

我需要获取一些与 Linux 中的 USB 主机控制器相关的参数。我在互联网上进行了很多搜索,发现的唯一方法是使用 pciutils lib。每个 PCI 设备都在 pci.h 中的 pci_dev 结构中:

struct pci_dev {
  struct pci_dev *next;         /* Next device in the chain */
  u16 domain_16;            /* 16-bit version of the PCI domain for backward compatibility */
                    /* 0xffff if the real domain doesn't fit in 16 bits */
  u8 bus, dev, func;            /* Bus inside domain, device and function */

  /* These fields are set by pci_fill_info() */
  int known_fields;         /* Set of info fields already known */
  u16 vendor_id, device_id;     /* Identity of the device */
  u16 device_class;         /* PCI device class */
  int irq;              /* IRQ number */
  pciaddr_t base_addr[6];       /* Base addresses including flags in lower bits */
  pciaddr_t size[6];            /* Region sizes */
  pciaddr_t rom_base_addr;      /* Expansion ROM base address */
  pciaddr_t rom_size;           /* Expansion ROM size */
  struct pci_cap *first_cap;        /* List of capabilities */
  char *phy_slot;           /* Physical slot */
  char *module_alias;           /* Linux kernel module alias */
  char *label;              /* Device name as exported by BIOS */
  int numa_node;            /* NUMA node */
  pciaddr_t flags[6];           /* PCI_IORESOURCE_* flags for regions */
  pciaddr_t rom_flags;          /* PCI_IORESOURCE_* flags for expansion ROM */
  int domain;               /* PCI domain (host bridge) */

  /* Fields used internally */
  struct pci_access *access;
  struct pci_methods *methods;
  u8 *cache;                /* Cached config registers */
  int cache_len;
  int hdrtype;              /* Cached low 7 bits of header type, -1 if unknown */
  void *aux;                /* Auxiliary data */
  struct pci_property *properties;  /* A linked list of extra properties */
  struct pci_cap *last_cap;     /* Last capability in the list */
};

我还发现我可以解析 pci 设备如下:

struct pci_access *pacc1;
struct pci_dev *dev1;
unsigned int c1;
char namebuf1[1024], *name1;

pacc1 = pci_alloc();        /* Get the pci_access structure */
/* Set all options you want -- here we stick with the defaults */
pci_init(pacc1);        /* Initialize the PCI library */
pci_scan_bus(pacc1);        /* We want to get the list of devices */
for (dev1=pacc1->devices; dev1; dev1=dev1->next)    /* Iterate over all devices */
{
    pci_fill_info(dev1, PCI_FILL_IDENT | PCI_FILL_BASES | PCI_FILL_CLASS);  /* Fill in header info we need */
    c1 = pci_read_byte(dev1, PCI_INTERRUPT_PIN);                /* Read config register directly */
    printf("%04x:%02x:%02x.%d vendor=%04x device=%04x class=%04x irq=%d (pin %d) base0=%lx",
     dev1->domain, dev1->bus, dev1->dev, dev1->func, dev1->vendor_id, dev1->device_id,
     dev1->device_class, dev1->irq, c1, (long) dev1->base_addr[0]);

    /* Look up and print the full name of the device */
    name1 = pci_lookup_name(pacc, namebuf1, sizeof(namebuf1), PCI_LOOKUP_DEVICE, dev1->vendor_id, dev1->device_id);
    printf(" ***************** %d: (%s)\n", dev1->dev, name1);
}
pci_cleanup(pacc1);     /* Close everything */

如果相应的 pci 设备对应于 USB 主机控制器,有没有办法检查循环?或者有没有更简单的方法来获取 USB 主机控制器信息?

标签: clinuxusbpci

解决方案


推荐阅读