Execute Script when Android Phone is connected to specified/selected USB Port
I´m having some trouble with my udev
rule. At the moment, I had my udev rule setup to execute a script whenever a Android phone is connected to any USB port on the computer. But I don´t want the script to be executed from every USB port. I just want that the script to be execute in example when the phone is connected to the 4th USB port and nothing should happen when it is connected to 1st, 2nd or 3rd USB port.
How can I achieve that and how can I determine the for example 4th USB Port?
I would highly appreciate your help.
My current udev rule:
KERNELS="1-2:1.1", ACTION=="add", ENV{DEVTYPE}=="usb_device", ATTRS{idVendor}!="1d6b", ATTRS{idVendor}!="203a", ATTRS{idVendor}=="****", ATTRS{idProduct}=="****", RUN+="/usr/local/bin/tmp.sh $attr{serial}"
In general, any plugged device get some corresponding nodes in SYSFS /sys
, some adds other nodes in /dev
.
-
Using sysfs:
-
Monitor udev events
sudo udevadm monitor
-
Plug your device and watch for previous command output, example
monitor will print the received events for: UDEV - the event which udev sends out after rule processing KERNEL - the kernel uevent KERNEL[5797.642807] add /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.4 (usb) KERNEL[5797.643604] add /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.4/2-1.4:1.0 (usb) UDEV [5797.659463] add /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.4 (usb) UDEV [5798.753894] add /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.4/2-1.4:1.0 (usb)
So in the format of
bus-port.port:config.interface
see reference: Bus:2, Port:1 (root hub), Port:4 (external usb port), Config:1 (plugged device), Interface:0The full name of USB port is
2-1.4
. -
To check its attributes:
udevadm info -a /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.4
Or the device (that port is parent node of it)
udevadm info -a /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.4/2-1.4:1.0
-
-
Using
/dev
:Android phone like a modem, it creates a serial port in
/dev
. To know the new plugged device, See How to allow software access to any USB devices?udevadm info -a /dev/ttyACM0
-
Another way, to get port is
dmesg
or :tail -f /var/log/kernel.log
Then use port number for KERNELS=="2-1.4"
or KERNELS=="2-1.4:1.[0-9]*"
References:
- Linux USB FAQ:What are the sysfs structures for Linux USB?