tuna-f1sh / cyme

List system USB buses and devices; a lib and modern cross-platform lsusb that attempts to maintain compatibility with, but also add new features
GNU General Public License v3.0
141 stars 7 forks source link

Fallback to lsusb behaviour for iSerial #13

Closed haata closed 7 months ago

haata commented 7 months ago

On linux the true iProduct/iManufacturer/iSerial are available (without root) through sysfs. Usually the device provided names are more accurate than the database (or may have additional information, such as in my case).

What cyme currently does

[dev]: cyme -d 0x308f --lsusb -v                                     

Bus 005 Device 025: ID 308f:0013 Input Club Kira
Couldn't open device, some information will be missing
Device Descriptor:
  bcdUSB              2.00
  bDeviceClass           0 UseInterfaceDescriptor
  bDeviceSubClass        0
  bDeviceProtocol        0
  bMaxPacketSize0       64
  idVendor          0x308f Input Club
  idProduct         0x0013 Kira
  bcdDevice           4.c6
  iManufacturer          2 Input Club
  iProduct               1 Kira
  iSerialNumber          3

What lsusb does

[dev]: lsusb -d 0x308f: -v                                            

Bus 005 Device 025: ID 308f:0013 Input Club Kira
Couldn't open device, some information will be missing
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.00
  bDeviceClass            0 [unknown]
  bDeviceSubClass         0 [unknown]
  bDeviceProtocol         0
  bMaxPacketSize0        64
  idVendor           0x308f Input Club
  idProduct          0x0013 Kira
  bcdDevice            4.c6
  iManufacturer           1 Input Club
  iProduct                2 Keyboard - Kira PixelMap USB
  iSerial                 3 5337310036384B323430313035353031 - sam4s8

Accessing this info from sysfs

: cat /sys/bus/usb/devices/5-4/5-4.3/{manufacturer,product,serial}
Input Club
Keyboard - Kira PixelMap USB
5337310036384B323430313035353031 - sam4s8
: ls -l  /sys/bus/usb/devices/5-4/5-4.3/{manufacturer,product,serial}
-r--r--r-- 1 root root 4096 Nov  9 04:59 /sys/bus/usb/devices/5-4/5-4.3/manufacturer
-r--r--r-- 1 root root 4096 Nov  9 04:59 /sys/bus/usb/devices/5-4/5-4.3/product
-r--r--r-- 1 root root 4096 Nov  9 04:59 /sys/bus/usb/devices/5-4/5-4.3/serial

No udev rules have been applied to this device

: ls -l /dev/bus/usb/005/025                          
crw-rw-r-- 1 root root 189, 536 Nov  9 04:59 /dev/bus/usb/005/025

lsusb (linux) prefers sysfs afaict https://github.com/gregkh/usbutils/blob/master/lsusb.c#L267

haata commented 7 months ago

Looking through the code it might take a bit of refactoring around here: https://github.com/tuna-f1sh/cyme/blob/main/src/lsusb.rs#L390

In my mind the priority should go something like this:

  1. lsusb attempt to read strings from device (these are always the most accurate, but may not be available due to permission issue)
  2. Use some sort of OS database that read the descriptors (i.e. sysfs/udev on linux)
  3. Fall back to linux device database

I may take a stab at this over the next couple of days.