0
votes

I need to be able to identify a plugged USB device (USB3 and/or USB2) and which port is connected to in the hub from a C program. I'm working on a embedded Linux system Yocto based.

I'm able to get this info from the command line with lsusb. E.g. I've got connected two USB cameras, one USB3 and the other USB2 and if I run lsusb -t I can see the port connections tree:

/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 5000M
    |__ Port 1: Dev 3, If 0, Class=Hub, Driver=hub/4p, 5000M
        |__ Port 4: Dev 6, If 0, Class=Miscellaneous Device, Driver=, 5000M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 480M
        |__ Port 3: Dev 3, If 0, Class=Video, Driver=uvcvideo, 480M
        |__ Port 3: Dev 3, If 1, Class=Video, Driver=uvcvideo, 480M
        |__ Port 3: Dev 3, If 2, Class=Human Interface Device, Driver=usbhid, 480M

With this I know easily that the USB3 camera is connected to Port 4 and the USB2 one to Port 3 (using a 4 ports HUB). If I run only lsusb got the IDs and names:

Bus 002 Device 006: ID 20f7:3001 USB3 Camera with CMOS sensor [MQ]
Bus 001 Device 003: ID 0403:de37 Future Technology Devices International, Ltd
Bus 001 Device 002: ID 04b4:650a Cypress Semiconductor Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 003: ID 04b4:6508 Cypress Semiconductor Corp.
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub

How could I easily scan the hub ports and see what device (name or ID) is connected where (hub port number) from a C application?

Apologise if this already answer somewhere but I couldn't find a specific answer.

Thanks in advance.

1
Does this answer your question : unix.stackexchange.com/questions/61484/… ?Storm
Thanks for the answer, I haven't seen that one, I'll try the solution and see how it goes.joe
I tried suggested solution bu it didn't work for me, couldn't compile the program. I wonder if it is because I'm working on a yocto image.....any other ideas would be much appreciated.joe
You need to install one of the libusb libraries on your Yocto image. A quick google search shows that there are available recipes for these libraries. If you want to generate an output similar to lsusb -t and get hub ports numbers, you may want to install libusb1, otherwise libusb-compat should do the jobStorm

1 Answers

0
votes

As pointed the answer is libusb, and I found the complete answer with an example in this question.

Also the API for libusb is defined here.