3

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}"
  • 1
    Check this thread Mass Storage devices on a selected USB port. – vembutech Jun 3 '15 at 11:03
  • @Helio 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}". I have no clue how to determine the right USB port. – Deep_Horizons Jun 3 '15 at 11:27
  • You can delete your comment. I put the rule on the question. – Helio Jun 3 '15 at 14:24
  • Can you post thelsusb output when the phone is plugged on that port? – Helio Jun 3 '15 at 15:26
1

In general, any plugged device get some corresponding nodes in SYSFS /sys, some adds other nodes in /dev.

  • Using sysfs:

    1. Monitor udev events

      sudo udevadm monitor
      
    2. 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:0

      The full name of USB port is 2-1.4.

    3. 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:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.