larskanis/libusb

[QUESTION] Get USB device name and path

nickgnd opened this issue · 2 comments

Hi, I apologize in advance for this question, I'm sure I missed something in the documentation.

I'm trying to use libusb gem to handle usb devices, but i'm not able to get information like name of the device and its path where is mounted. How can i retrieve this kind of informations? Is it possible?

Use case:

USB device mounted in /Volumes/

❯ df -lH | grep "Filesystem"; df -lH | grep "/Volumes/*"
Filesystem     Size   Used  Avail Capacity  iused   ifree %iused  Mounted on
/dev/disk1s1   8.0G   1.4G   6.6G    19%        0       0  100%   /Volumes/NICKGND

Try to get USB informations in a simple basic Ruby script (debug with byebug)

    4: require 'libusb'
    5: require 'byebug'
    6: 
    7: usb = LIBUSB::Context.new
    8: device = usb.devices.first
    9: byebug
=> 10: p device

(byebug) device
#<LIBUSB::Device 253/6 0951:162d Kingston DataTraveler 102 001CC0EC348CBB3177100147 (MassStorage SCSI Bulk-Only)>
(byebug) device.device_address
6
(byebug) device.endpoints
[#<LIBUSB::Endpoint 1 in bulk>, #<LIBUSB::Endpoint 2 out bulk>]
(byebug) device.inspect
"#<LIBUSB::Device 253/6 0951:162d Kingston DataTraveler 102 001CC0EC348CBB3177100147 (MassStorage SCSI Bulk-Only)>"
(byebug) device.port_numbers
[1, 3, 1]
(byebug) device.settings
[#<LIBUSB::Setting 0 MassStorage SCSI Bulk-Only>]
(byebug) device.configurations
[#<LIBUSB::Configuration 1>]

I don't know which method use.

BTW thanks a lot for your work!
Nico

You're probably mixing different layers of the software stack. Libusb is
made for communication with arbitrary USB devices. In case of a mass
storage device you could write your own driver which sends read-sector or
write-sector commands from your user mode application. However if you mount
a device, this task is done by a kernel mode driver of the operating system.

If you want to retrieve additional information about the USB-device that is
mounted into the file system, you need to ask the OS, which USB device is
used by the kernel driver to provide the /dev/disk* node. This is beyond
the scope of libusb.

However if you do have the particular LIBUSB::Device object, you can query
the name per #product or the #serial_number . LIBUSB::Device#port_numbers
gives you the physical port numbers, that the USB device is connected to
(starting with the host controller).

Thank you for your clear and helpful explanation!