如何在 Linux 上以编程方式获取 USB 供应商和产品信息?

发布于 2024-09-10 14:18:37 字数 758 浏览 5 评论 0原文

使用 udev,我已经能够获取某个 USB 设备的信息:

idVendor: 13b1
idProduct: 0018
manufacturer:  
product: USB 2.0 Network Adapter ver.2 
serial: 00FFFF

现在我想获取与供应商和产品 ID 关联的完整字符串。我发现文件 /usr/share/misc/usb.ids 包含我正在查找的信息:

13b1  Linksys
        000b  WUSB11 v4.0 802.11b Adapter
        000d  WUSB54G Wireless Adapter
        0011  WUSB54GP v4.0 802.11g Adapter
        0018  USB200M 10/100 Ethernet Adapter
        001a  HU200TS Wireless Adapter
        001e  WUSBF54G 802.11bg
        0020  WUSB54GC 802.11g Adapter [ralink rt73]
        0023  WUSB54GR
        0024  WUSBF54G v1.1 802.11bg

但是,我不清楚如何在应用程序中检索此数据。是否有可用的 API 或者我应该只解析该文件?如果我选择解析它,那么 /usr/share/misc/usb.ids 总是正确的位置吗?

Using udev I have been able to get this information for a certain USB device:

idVendor: 13b1
idProduct: 0018
manufacturer:  
product: USB 2.0 Network Adapter ver.2 
serial: 00FFFF

Now I want to get the full strings that are associated with the vendor and product ids. I found that the file /usr/share/misc/usb.ids contains the information that I'm looking for:

13b1  Linksys
        000b  WUSB11 v4.0 802.11b Adapter
        000d  WUSB54G Wireless Adapter
        0011  WUSB54GP v4.0 802.11g Adapter
        0018  USB200M 10/100 Ethernet Adapter
        001a  HU200TS Wireless Adapter
        001e  WUSBF54G 802.11bg
        0020  WUSB54GC 802.11g Adapter [ralink rt73]
        0023  WUSB54GR
        0024  WUSBF54G v1.1 802.11bg

However, it's not clear to me how I should retrieve this data in my application. Is there an API available or should I just parse the file? If I choose to parse it, then is /usr/share/misc/usb.ids always going to be the correct location?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

桜花祭 2024-09-17 14:18:37

lsusb 命令查询当前插入的 USB 设备的信息。您可以使用其 -d 选项来查询某个供应商/产品(但它似乎仅适用于当前插入的设备):

$ lsusb -d 0e21:0750
Bus 001 Device 005: ID 0e21:0750 Cowon Systems, Inc.

您可以显示所有设备的信息:

$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 004: ID 0421:01c7 Nokia Mobile Phones
Bus 001 Device 003: ID 0bda:8187 Realtek Semiconductor Corp. RTL8187 Wireless Adapter
Bus 001 Device 005: ID 0e21:0750 Cowon Systems, Inc.
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 006 Device 002: ID 046d:c01b Logitech, Inc. MX310 Optical Mouse
Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 008 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

您还可以使其变得详细( lsusb -v)并打印很多的东西。

请注意,在 Linux 操作系统中访问有关系统的信息时,最好通过 shell 命令(例如 lsusb)来完成,而不是直接解析这些命令访问的系统文件。

lsusb command queries information about currently plugged USB devices. You can use its -d option to query a certain vendor/product (but it seems to work only for currently plugged devices):

$ lsusb -d 0e21:0750
Bus 001 Device 005: ID 0e21:0750 Cowon Systems, Inc.

You can show information for all devices:

$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 004: ID 0421:01c7 Nokia Mobile Phones
Bus 001 Device 003: ID 0bda:8187 Realtek Semiconductor Corp. RTL8187 Wireless Adapter
Bus 001 Device 005: ID 0e21:0750 Cowon Systems, Inc.
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 006 Device 002: ID 046d:c01b Logitech, Inc. MX310 Optical Mouse
Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 008 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

You can also make it be verbose (lsusb -v) and printing a lot of stuff.

Note that when accessing information about the system in Linux OS, it's much preferred to do it via shell commands (such as lsusb) than to directly parse the system files these commands access.

◇流星雨 2024-09-17 14:18:37

我自己没有尝试过,但是libudev 的 udev_device_get_property_value 应该是它;它用于 pulseaudio 的 udev-util .cudev_device_get_property_value(card, "ID_VENDOR_FROM_DATABASE"))

这是我刚刚放在一起的一个小示例,基于 udev-util.c - 请注意,我使用了带有 FTDI FT232 芯片的 Arduino Duemillanove,我使用 udevadm< 找到了其 udev 路径/code> (请参阅下面代码中的注释),然后我将其硬编码到下面的程序中,udevl.c

// sudo apt-get install libudev-dev
// build with: gcc -o udevl -ludev -Wall -g udevl.c

#include <stdio.h>

#include <libudev.h>

int main( int argc, char **argv )
{
  const char *v;
  char t[256];
  struct udev *udev;
  struct udev_device *card = NULL;

  if (!(udev = udev_new())) {
      fprintf(stderr, "Failed to allocate udev context.\n");
      return -1;
  }

  // $ lsusb | grep FT232
  // Bus 002 Device 002: ID 0403:6001 Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC    
  // $ udevadm info --name=/dev/ttyUSB0 --attribute-walk | grep "looking at device"
  // looking at device '/devices/pci0000:00/0000:00:1d.0/usb2/2-2/2-2:1.0/ttyUSB0/tty/ttyUSB0'
  // (that one is under /sys)
  // hardcode that path below:

  // udev_get_sys_path(udev) for me: '/sys'
  sprintf(t, "%s/devices/pci0000:00/0000:00:1d.0/usb2/2-2/2-2:1.0/ttyUSB0/tty/ttyUSB0", udev_get_sys_path(udev));
  fprintf(stdout, " path: %s\n", t);

  card = udev_device_new_from_syspath(udev, t);
  fprintf(stdout, " udev_device: 0x%08X\n", (unsigned int)card);

  if ((v = udev_device_get_property_value(card, "ID_MODEL_FROM_DATABASE")) )
    fprintf(stdout, "got ID_MODEL_FROM_DATABASE: %s\n", v);
  else
    fprintf(stdout, "failed getting ID_MODEL_FROM_DATABASE: %s\n", v);

  fprintf(stdout, "Done.\n");

  if (card)
    udev_device_unref(card);

  if (udev)
    udev_unref(udev);

  return 0;
}

该程序(附有 Arduino)输出:

$ ./udevl 
 path: /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-2/2-2:1.0/ttyUSB0/tty/ttyUSB0
 udev_device: 0x09FBF080
got ID_MODEL_FROM_DATABASE: FT232 USB-Serial (UART) IC
Done.

。 .. 而“FT232 USB 串行 (UART) IC” 是 usb.ids

希望这有帮助,
干杯!

Haven't tried this myself, but libudev's udev_device_get_property_value should be it; it is used in pulseaudio's udev-util.c as udev_device_get_property_value(card, "ID_VENDOR_FROM_DATABASE")).

Here is a small example I just put together, based on udev-util.c - note that I've used an Arduino Duemillanove with FTDI FT232 chip, whose udev path I find using udevadm (see comments in code below), and then I hardcoded it in the below program, udevl.c:

// sudo apt-get install libudev-dev
// build with: gcc -o udevl -ludev -Wall -g udevl.c

#include <stdio.h>

#include <libudev.h>

int main( int argc, char **argv )
{
  const char *v;
  char t[256];
  struct udev *udev;
  struct udev_device *card = NULL;

  if (!(udev = udev_new())) {
      fprintf(stderr, "Failed to allocate udev context.\n");
      return -1;
  }

  // $ lsusb | grep FT232
  // Bus 002 Device 002: ID 0403:6001 Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC    
  // $ udevadm info --name=/dev/ttyUSB0 --attribute-walk | grep "looking at device"
  // looking at device '/devices/pci0000:00/0000:00:1d.0/usb2/2-2/2-2:1.0/ttyUSB0/tty/ttyUSB0'
  // (that one is under /sys)
  // hardcode that path below:

  // udev_get_sys_path(udev) for me: '/sys'
  sprintf(t, "%s/devices/pci0000:00/0000:00:1d.0/usb2/2-2/2-2:1.0/ttyUSB0/tty/ttyUSB0", udev_get_sys_path(udev));
  fprintf(stdout, " path: %s\n", t);

  card = udev_device_new_from_syspath(udev, t);
  fprintf(stdout, " udev_device: 0x%08X\n", (unsigned int)card);

  if ((v = udev_device_get_property_value(card, "ID_MODEL_FROM_DATABASE")) )
    fprintf(stdout, "got ID_MODEL_FROM_DATABASE: %s\n", v);
  else
    fprintf(stdout, "failed getting ID_MODEL_FROM_DATABASE: %s\n", v);

  fprintf(stdout, "Done.\n");

  if (card)
    udev_device_unref(card);

  if (udev)
    udev_unref(udev);

  return 0;
}

This program (with the Arduino attached) outputs:

$ ./udevl 
 path: /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-2/2-2:1.0/ttyUSB0/tty/ttyUSB0
 udev_device: 0x09FBF080
got ID_MODEL_FROM_DATABASE: FT232 USB-Serial (UART) IC
Done.

... and "FT232 USB-Serial (UART) IC" is the right entry for VID:PID 0403:6001 in usb.ids.

Hope this helps,
Cheers!

揽清风入怀 2024-09-17 14:18:37

在我的 Ubuntu 系统上,lsusb(1) 联机帮助页显示 /var/lib/usbutils/usb.ids 是 id 文件的位置;事实上,有两个符号链接,其中之一是您的 /usr/share/misc/usb.ids。在信任符号链接之前我会信任实际位置:

$ ls -l /usr/share/misc/usb.ids /var/lib/misc/usb.ids /var/lib/usbutils/usb.ids
lrwxrwxrwx 1 root root     25 2010-04-29 18:08 /usr/share/misc/usb.ids -> /var/lib/usbutils/usb.ids
lrwxrwxrwx 1 root root     19 2010-04-29 18:08 /var/lib/misc/usb.ids -> ../usbutils/usb.ids
-rw-r--r-- 1 root root 368377 2009-11-06 09:26 /var/lib/usbutils/usb.ids

On my Ubuntu system, the lsusb(1) manpage says that /var/lib/usbutils/usb.ids is the location of the id file; in fact, there are two symlinks, one of which is your /usr/share/misc/usb.ids. I'd trust the actual location before trusting the symlinks:

$ ls -l /usr/share/misc/usb.ids /var/lib/misc/usb.ids /var/lib/usbutils/usb.ids
lrwxrwxrwx 1 root root     25 2010-04-29 18:08 /usr/share/misc/usb.ids -> /var/lib/usbutils/usb.ids
lrwxrwxrwx 1 root root     19 2010-04-29 18:08 /var/lib/misc/usb.ids -> ../usbutils/usb.ids
-rw-r--r-- 1 root root 368377 2009-11-06 09:26 /var/lib/usbutils/usb.ids
凶凌 2024-09-17 14:18:37
lsusb

你会得到这样的结果

Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 009: ID 138a:0010 Validity Sensors, Inc. VFS Fingerprint sensor
Bus 001 Device 008: ID 13d3:3491 IMC Networks 

ID 1d6b:0003 可以被视为:vendor = 1d6bproduct = 0003

lsusb

you get something like this

Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 009: ID 138a:0010 Validity Sensors, Inc. VFS Fingerprint sensor
Bus 001 Device 008: ID 13d3:3491 IMC Networks 

then: ID 1d6b:0003 can be seen as : vendor = 1d6b and product = 0003

云淡风轻 2024-09-17 14:18:37

您的 USB 设备不需要将供应商和产品 ID 与实际的正确名称相匹配。

使用 libusb 或 lsusb 等方式从设备本身获取此信息会更安全。

Your USB device does not need to match vendor and product ids to the actual correct names.

It would be safer to get this info from the device itself with something like libusb or lsusb.

新雨望断虹 2024-09-17 14:18:37

我认为这是使用 libudev 问题的最佳解决方案:

udev_device_get_sysattr_value(usbDev, "idVendor")

I think this is the best solution for the issue with using libudev:

udev_device_get_sysattr_value(usbDev, "idVendor")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文