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

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

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

1,504件の閲覧回数
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!

ラベル(3)
タグ(3)
0 件の賞賛
1 返信

595件の閲覧回数
b36401
NXP Employee
NXP Employee

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 件の賞賛