Determine USB device file Path
Asked Answered
S

4

18

How can i get USB device file path correctly in Linux. I used command: find / -iname "usb" and got the result as below:

/dev/bus/usb
/sys/bus/usb
/sys/bus/usb/drivers/usb
/sys/kernel/debug/usb

Under /dev/bus/usb i see:

001  002  003  004  005  006

But I think they aren't files as i need.

Under /sys/bus/usb/devices/:

sh-3.2# ls /sys/bus/usb/devices/
1-0:1.0  1-1:1.0  3-0:1.0  5-0:1.0  usb1     usb3     usb5
1-1      2-0:1.0  4-0:1.0  6-0:1.0  usb2     usb4     usb6

And Under /sys/bus/scsi/devices/ when i pluged an USB i see:

2:0:0:0      host0        host2        target2:0:0

And when i removed USB i see:

sh-3.2# ls
host0

So which device file is used for USB? How can i indentify it? I need to make a C program with USB device file...

Further more, could you explain to me the number 1-1:1.0? What does it mean?

Thank you.

Sheley answered 15/10, 2015 at 5:47 Comment(5)
What do you need the device file for?Eisenhart
I need to check informations which related to USB device. So i think i will send ioctl to USB device file and read the feedback data? Is this possible?Sheley
What kind of information?Eisenhart
Read/write verification to USB device, and get USB device's information such as: speed, name, ...Sheley
What do you mean with "read/write verification"? Is this for some specific device? Or are you trying to duplicate lsusb?Eisenhart
B
32

So which device file is used for USB? How can i indentify it?

What you see behind /sys/ is mainly configuration/information about devices. /dev/bus/usb is what you are looking for. I think that the following article can help you

http://www.linuxjournal.com/article/7466?page=0,0

Is quite old, but still it can help you. (In the article they speak about /proc/bus/usb, today we have /dev/bus/usb)

Further more, could you explain to me the number 1-1:1.0? What does it mean?

The generic form is

X-Y.Z:A.B

Each field identify the connection point of your device. The first two field are mandatory:

  • X is the USB bus of your motherboard where is connected the USB system.
  • Y is the port in use on the bus system

So the USB device identified with the string 3-3 is the device connected on the port 3 of the bus 3.

If you connect an USB hub, you are extending the connection capability of a single USB port. The Linux kernel identify this situation by appending the Z field.

  • Z is the port is use on an hub

So, the USB device identified with the string 1-2.5 is the device connected on the port 5 of the hub connected on the port 2 of the bus 1.

USB specification allow you to connect in cascade more then one USB hub, so the Linux kernel continue to append the port in use on the different hubs. So, the USB device identified with the string 1-2.1.1 is the device connected on the port 1 of the hub connected on the port 1 of the hub connected to the port 2 of the bus 1.

A fast way to retrieve these information is to read the kernel messages (if you can).

$ dmesg | grep usb
[... snip ...]
[ 2.047950] usb 4-1: new full-speed USB device number 2 using ohci_hcd
[ 2.202628] usb 4-1: New USB device found, idVendor=046d, idProduct=c318
[ 2.202638] usb 4-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 2.202643] usb 4-1: Product: Logitech Illuminated Keyboard
[ 2.202648] usb 4-1: Manufacturer: Logitech
[... snip ...]

Then, the last two fields of the pattern (after colon) identify an internal section of an USB device :

  • A is the configuration number of the device
  • B is the interface number of a configuration

So, the string 4-1:1.1 means: the interface 1, on configuration 1 that is connected on the port 1 of the bus 4.

You can retrieve these information with the command lsusb.

Bemean answered 15/10, 2015 at 7:49 Comment(6)
This is very helpful to me. But i still don't know which file can i send USB ioctl to it to get some informations...Sheley
Maybe you should have a look at libusb. It will abstract most of the work for you.Lohengrin
As I wrote in the answer, /dev/bus/usb is the directory where you have to look. Which file exactly? It depends on where you connected the USB device. It's explained in the article and in the answer. Then as Alexandre said and suggested by the article, use libusbBemean
Are these ports unique over multiple hubs?Wanwand
This Oct 15, 2015 at 7:49 answer may be outdated? I navigate to /dev/bus/usb/001 and see device 004 as per Terminal$ lsusb: Bus 001 Device 004: ID 18ec:5555 Arkmicro Technologies Inc. USB2.0 PC CAMERA However in VLC -> Capture Device path: /dev/bus/usb/001, Play shows error Your input can't be opened: VLC is unable to open the MRL 'v4l2:///dev/bus/usb/001'. Check the log for details.Hawfinch
[guessing] I do not know your device. Could it be that you need to provide /dev/bus/usb/001/004 instead? Could it be that you need to provide /dev/videoXXX instead?Bemean
M
0

I know this is an old question that has been answered a long time ago, but I keep stumbling upon it when looking for the command below. It might not be what OP asked for, but its possible that I'm not the only one who gets directed to this page when looking for this:

ls -l /dev/serial/by-id/

I'm a Linux noob and could not find any other command that gives me a list of serial devices with both device names and paths (e.g. /dev/ttyUSB0). For expample dmesg | grep usb does not give me the path that I need.

Mirianmirielle answered 14/3, 2023 at 13:38 Comment(0)
E
0

Use ls /sys/bus/usb/devices/2* and cat /sys/bus/usb/devices/2*/manufacturer and progressively add numbers to the 2* part.. like 2-3:*, 2-3:2.* until you can match the name from lsusb, or use idProduct or idVendor instead of manufacturer.

Execrate answered 1/8, 2023 at 4:43 Comment(0)
V
0

The code in Python3 below worked for me on Ubuntu 22.04:


import os
import glob

def get_last_inserted_usb():

    # Directory containing symlinks to USB devices
    disk_by_id_path = '/dev/disk/by-id'

    # Find all USB device symlinks
    usb_devices = glob.glob(os.path.join(disk_by_id_path, 'usb-*'))

    if not usb_devices:
        return None

    # Create a list to hold device details
    devices = []

    for usb_device in usb_devices:
        # Resolve the symlink to the actual device node path
        device_node = os.path.realpath(usb_device)

        # Get the symlink creation time
        dev_stat = os.stat(usb_device)
        dev_time = dev_stat.st_ctime

        devices.append((device_node, dev_time))

    # Sort devices by time (most recent first)
    devices.sort(key=lambda x: x[1], reverse=True)

    # Return the most recently added device node path
    if devices:
        return devices[0][0]
    else:
        return None


if __name__ == "__main__":
    last_usb_device = get_last_inserted_usb()
    if last_usb_device:
        print(f"Device node path of the last inserted USB drive: {last_usb_device}")
    else:
        print("No USB devices found.")

Vidavidal answered 25/5 at 10:40 Comment(1)
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?Chopin

© 2022 - 2024 — McMap. All rights reserved.