How create a udev rules to disable one of Android devices?

Solution 1:

From dmesg output, it is clear that the phone connected twice.

  • [ 40.632283] 1st connected as USB bus 2 dev 3
  • [ 40.864690] disconnected
  • [ 41.613079] 2nd connected as USB bus 2 dev 4

The problem is:

  • Both phone connection modes are using same attributes idVendor/idProduct/bcdDevice.

  • libmtp udev rules use only idVendor/idProduct to filter devices in addition to non-important/common attributes ACTION!="add", ENV{MAJOR}!="?*" and SUBSYSTEM=="usb"

  • libmtp udev rules use ATTR (not ATTRS), it does target exactly this device node /devices/pci0000:00/0000:00:13.2/usb2/2-1. So we can't use interfaces nodes details as they are children nodes to this one.

To get what going on, use udevadm monitor. To see just events without details.

  1. Unplug the phone
  2. Open terminal and run udevadm monitor -u , -u to show UDEV events only (for clean output)
  3. Plug the phone and wait till things settle down
  4. Ctrl+C to stop monitoring

To get details (Environment properties) use udevadm monitor -u -p instead and compare output at that node:

  • UDEV [107.024195] add /devices/pci0000:00/0000:00:13.2/usb2/2-1 (usb)
  • UDEV [107.998137] add /devices/pci0000:00/0000:00:13.2/usb2/2-1 (usb)

Notice the difference in ID_USB_INTERFACES

Another cleaner way, using a udev rule to collect only what we need:

  1. Add a rule to /lib/udev/rules.d/69-libmtp.rules just after LABEL="libmtp_usb_rules":

    ATTR{idVendor}=="0bb4", ATTR{idProduct}=="0f91", RUN+="/bin/sh -c 'env >> /home/username/udev-phone-mtp_%E{SEQNUM}.log'"
    
  2. Reload rules

    sudo udevadm control -R
    
  3. Replug the phone once.

  4. This rule should be triggered twice. Comparing output at that node:

    diff udev-phone-mtp_*.log

    should bring: (this is just the interesting portion)

    < ID_USB_INTERFACES=:060101:080650:
    ---
    > ID_USB_INTERFACES=:060101:ffff00:
    

Exactly what Pilot6 (OP) could catch it using usb-devices before it reconnected.


I suggest adding this rule to /lib/udev/rules.d/69-libmtp.rules, just after LABEL="libmtp_usb_rules":

ATTR{idVendor}=="0bb4", ATTR{idProduct}=="0f91", ENV{ID_USB_INTERFACES}==":060101:080650:", GOTO="libmtp_rules_end"