Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux usb connect/disconnect event

Hello I am working on an embedded linux device with a usb port that uses the g_ether driver for usb networking.

When the usb plug is connected the dmesg output is:

g_ether gadget: full speed config #2: RNDIS

When the usb cable is unplugged no message is written to dmesg.

Using C how can I listen for the connect/disconnect events?

The embedded linux OS does not have any extras. There is no dbus daemon or hotplug helper script. I am not even sure if these would of been helpful.

like image 480
eat_a_lemon Avatar asked Aug 19 '11 00:08

eat_a_lemon


1 Answers

You can use libudev or parse udevadm output as @Ambroz Bizjak suggested. Although, I advise against adding an additional process (stdbuf) and language (NCD), just to parse udevadm's output.

A step between plain libudev and parsing output is modifying the udevadm sources. This solution reduces the needed resources and skips the parsing process altogether. When you look at the udev package, you will find the sources for udevd and udevadm in the udev directory.

There, you have the main routine in udevadm.c and the source for udevadm monitor in udevadm-monitor.c. Every event received will be printed through print_device(). This is where you insert your code.

If you're tight on memory, you can strip off unneeded code for control, info, settle, test-builtin, test and trigger. On my system (Ubuntu 12.04), this reduces the size of udevadm by about 75%.

like image 92
Olaf Dietsche Avatar answered Nov 16 '22 04:11

Olaf Dietsche