Use sysfs to detect if a usb device is connected/disconnected?

cancel
Showing results for 
Search instead for 
Did you mean: 

Use sysfs to detect if a usb device is connected/disconnected?

1,179 Views
mercyyuen
Contributor II

I'm running a imx6qsabresd board.  whenever I plug in a usb device, I can see the message on the terminal:

usb 2-1: new low speed USB device number 3 using fsl-ehci
input: Microsoft Basic Optical Mouse as /devices/platform/fsl-ehci.1/usb2/2-1/2-1:1.0/input/input3
generic-usb 0003:045E:0084.0002: input,hidraw0: USB HID v1.10 Mouse [Microsoft Basic Optical Mouse] on usb-fsl-ehci.1-1/input0


when I disconnect, I see the message:

usb 2-1: USB disconnect, device number 3


I want to figure out a way to check the usb device is connected.

I thought I could do something simple like

file *fp = fopen("/sys/devices/platform/fsl-ehci.1/usb2/2-1/2-1:1.0/input/input3","r");

if(fp)

  printf("found USB2\n");

else

  printf("didn't find USB2\n");

But this code doesn't work. I always get the output "didn't find USB2"

could someone please give me some suggestions?  Please don't tell me to try using libudev.  It does not work on this compiler because of:  libudev.h: No such file or directory

Thanks in advance!

Labels (3)
Tags (3)
0 Kudos
1 Reply

270 Views
b36401
NXP TechSupport
NXP TechSupport

I can suggest you to write a script that parses "dmesg" output

for the messages like "usb wakeup is here" or "new USB device"

instead of sysfs to detect USB conection.

0 Kudos