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

5,723

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"
Share:
5,723

Related videos on Youtube

Pilot6
Author by

Pilot6

Updated on September 18, 2022

Comments

  • Pilot6
    Pilot6 over 1 year

    I have an HTC Android phone.

    Every time I connect it to computer I get a message:

    Android Phone: Could not open MTP device "[usb:002,003]"
    

    The numbers may be different. But the phone connects correctly.

    usb-devices output

    T:  Bus=02 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 28 Spd=480 MxCh= 0
    D:  Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs=  1
    P:  Vendor=0bb4 ProdID=0f91 Rev=02.33
    S:  Manufacturer=HTC
    S:  Product=Android Phone
    S:  SerialNumber=FA41BWB00560
    C:  #Ifs= 2 Cfg#= 1 Atr=c0 MxPwr=500mA
    I:  If#= 0 Alt= 0 #EPs= 3 Cls=06(still) Sub=01 Prot=01 Driver=usbfs
    I:  If#= 1 Alt= 0 #EPs= 2 Cls=ff(vend.) Sub=ff Prot=00 Driver=(none)
    

    lsusb

    Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 002 Device 002: ID 048d:1336 Integrated Technology Express, Inc. SD/MMC Cardreader
    Bus 002 Device 004: ID 0bb4:0f91 HTC (High Tech Computer Corp.) 
    Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
    Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 001 Device 002: ID 04b8:0898 Seiko Epson Corp. 
    Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
    Bus 004 Device 002: ID 046d:c05a Logitech, Inc. M90/M100 Optical Mouse
    Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    

    lsusb -t

    /:  Bus 07.Port 1: Dev 1, Class=root_hub, Driver=ohci-pci/2p, 12M
    /:  Bus 06.Port 1: Dev 1, Class=root_hub, Driver=ohci-pci/3p, 12M
    /:  Bus 05.Port 1: Dev 1, Class=root_hub, Driver=ohci-pci/3p, 12M
    /:  Bus 04.Port 1: Dev 1, Class=root_hub, Driver=ohci-pci/3p, 12M
        |__ Port 1: Dev 2, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
    /:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=ohci-pci/3p, 12M
    /:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/6p, 480M
        |__ Port 1: Dev 4, If 0, Class=Imaging, Driver=usbfs, 480M
        |__ Port 1: Dev 4, If 1, Class=Vendor Specific Class, Driver=, 480M
        |__ Port 3: Dev 2, If 0, Class=Mass Storage, Driver=usb-storage, 480M
    /:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/6p, 480M
        |__ Port 2: Dev 2, If 0, Class=Vendor Specific Class, Driver=, 480M
        |__ Port 2: Dev 2, If 1, Class=Printer, Driver=usblp, 480M
        |__ Port 2: Dev 2, If 2, Class=Mass Storage, Driver=usb-storage, 480M
    

    udevadm info -q all -n /dev/bus/usb/002/004

    P: /devices/pci0000:00/0000:00:13.2/usb2/2-1
    N: bus/usb/002/004
    S: libmtp-2-1
    E: BUSNUM=002
    E: COLORD_DEVICE=1
    E: COLORD_KIND=camera
    E: DEVLINKS=/dev/libmtp-2-1
    E: DEVNAME=/dev/bus/usb/002/004
    E: DEVNUM=004
    E: DEVPATH=/devices/pci0000:00/0000:00:13.2/usb2/2-1
    E: DEVTYPE=usb_device
    E: DRIVER=usb
    E: GPHOTO2_DRIVER=PTP
    E: ID_BUS=usb
    E: ID_FOR_SEAT=usb-pci-0000_00_13_2-usb-0_1
    E: ID_GPHOTO2=1
    E: ID_MEDIA_PLAYER=1
    E: ID_MODEL=Android_Phone
    E: ID_MODEL_ENC=Android\x20Phone
    E: ID_MODEL_ID=0f91
    E: ID_MTP_DEVICE=1
    E: ID_PATH=pci-0000:00:13.2-usb-0:1
    E: ID_PATH_TAG=pci-0000_00_13_2-usb-0_1
    E: ID_REVISION=0233
    E: ID_SERIAL=HTC_Android_Phone_FA41BWB00560
    E: ID_SERIAL_SHORT=FA41BWB00560
    E: ID_USB_INTERFACES=:060101:ffff00:
    E: ID_VENDOR=HTC
    E: ID_VENDOR_ENC=HTC
    E: ID_VENDOR_FROM_DATABASE=HTC (High Tech Computer Corp.)
    E: ID_VENDOR_ID=0bb4
    E: MAJOR=189
    E: MINOR=131
    E: PRODUCT=bb4/f91/233
    E: SUBSYSTEM=usb
    E: TAGS=:seat:uaccess:
    E: TYPE=0/0/0
    E: USEC_INITIALIZED=611981507
    

    dmesg

    [   40.632283] usb 2-1: new high-speed USB device number 3 using ehci-pci
    [   40.765458] usb 2-1: New USB device found, idVendor=0bb4, idProduct=0f91
    [   40.765469] usb 2-1: New USB device strings: Mfr=2, Product=3, SerialNumber=4
    [   40.765475] usb 2-1: Product: Android Phone
    [   40.765480] usb 2-1: Manufacturer: HTC
    [   40.765485] usb 2-1: SerialNumber: FA41BWB00560
    [   40.766646] usb-storage 2-1:1.1: USB Mass Storage device detected
    [   40.767102] scsi host10: usb-storage 2-1:1.1
    [   40.864690] usb 2-1: USB disconnect, device number 3
    [   41.613079] usb 2-1: new high-speed USB device number 4 using ehci-pci
    [   41.746616] usb 2-1: New USB device found, idVendor=0bb4, idProduct=0f91
    [   41.746626] usb 2-1: New USB device strings: Mfr=2, Product=3, SerialNumber=4
    [   41.746633] usb 2-1: Product: Android Phone
    [   41.746638] usb 2-1: Manufacturer: HTC
    [   41.746643] usb 2-1: SerialNumber: FA41BWB00560
    [  101.942087] usb 2-1: reset high-speed USB device number 4 using ehci-pci
    

    I also found that for a short period of time after the phone is connected a usb-storage device is declared

    I:  If#= 0 Alt= 0 #EPs= 3 Cls=06(still) Sub=01 Prot=01 Driver=usbfs
    I:  If#= 1 Alt= 0 #EPs= 2 Cls=08(stor.) Sub=06 Prot=50 Driver=usb-storage
    

    Then it disconnects and class is changed to ff.

    I:  If#= 0 Alt= 0 #EPs= 3 Cls=06(still) Sub=01 Prot=01 Driver=usbfs
    I:  If#= 1 Alt= 0 #EPs= 2 Cls=ff(vend.) Sub=ff Prot=00 Driver=(none)
    
    • A.B.
      A.B. over 8 years
      Please do the following for me: 1) lsusb and find your device or your devices (Bus and Device). 2) Start the command udevadm info -q all -n /dev/bus/usb/your_bus_id/your_device_id and use Bus and Device. Add the output of the command in step 2 into your question.
    • A.B.
      A.B. over 8 years
      Ok and the output of lsusb too, I need only the part of your Phone
    • Pilot6
      Pilot6 over 8 years
      I will add it, but you can see VID & PID in usb-devices. @A.B.
    • Pilot6
      Pilot6 over 8 years
      I did not see the first comment. Added that too.
    • A.B.
      A.B. over 8 years
      I'm not sure that those are really two USB devices.
    • A.B.
      A.B. over 8 years
      But you could try a ENV{ID_SERIAL_SHORT}=="FA41BWB00560". In my understanding, the values for E: can be accessed via ENV{…}
    • user.dz
      user.dz over 8 years
      [usb:002,007] & phone: [usb:002,004] are two separate devices. If you didn't reboot or change USB port between the two commands running. Could you confirm that @Pilot6 then post full lsusb
    • Pilot6
      Pilot6 over 8 years
      @Sneetsher You are correct. I am updating. It looks like there is some other device that could not be connected and does not appear in lsusb. But it is still in the phone.
    • user.dz
      user.dz over 8 years
      @Pilot6 , try also lsusb -t to see all interfaces
    • Pilot6
      Pilot6 over 8 years
      @Sneetsher I added the output. It looks like this is a similar issue with virtual CD, but it can't connect for some reason.
    • A.B.
      A.B. over 8 years
      Can you add the output of udevadm info -q all -n /dev/bus/usb/002/002? That should be the |__ Port 3: Dev 2, If 0, Class=Mass Storage, Driver=usb-storage, 480M of Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/6p, 480M in the output of lsusb -t
    • A.B.
      A.B. over 8 years
      I'm confused. What is your phone in the output of lsusb -t?
    • Pilot6
      Pilot6 over 8 years
      The problem is that after I reconnect the phone the numbers change. I will reboot, reconnect the phone and check the numbers.
    • Pilot6
      Pilot6 over 8 years
      Now it is all correct. It writes Android Phone: Could not open MTP device "[usb:002,003]". But it can connect the phone @A.B.
    • Pilot6
      Pilot6 over 8 years
      Added dmesg too @Sneetsher
    • Pilot6
      Pilot6 over 8 years
      Is it possible to fix it somehow? Maybe run some sleep 5 as a script, to let the phone switch the device? I think it does something like with the CD, but ejects by itself.
    • user.dz
      user.dz over 8 years
      @Pilot6 delay may not work as with respect to udev these are two separate devices. Each one has its own events line. libmtp is triggered for both. mtp udev rules are based on vendor/product ids. There should be something different between the 2 mode. Try unplug it, run udevadm monitor -u -p in terminal, then plug it , wait till it ready. Ctrl+C to end monitoring. then post it to paste.ubuntu.com .
    • Pilot6
      Pilot6 over 8 years
    • Pilot6
      Pilot6 over 8 years
  • user.dz
    user.dz over 8 years
    This chat discussion chat.stackexchange.com/rooms/28790/… related to this . Reported that: First time it worked well A second time after unplug there was an error then opened after a delay.
  • Pilot6
    Pilot6 over 8 years
    I do not see any errors now. But the delay is still there. It is up to a minute to get the device mounted. I get the error if I do not wait till it gets auto mounted and try to open it in nautilus.
  • Pilot6
    Pilot6 over 8 years
    But this does not seem related, because I commented out your line and the delay is still there. The phone appears in the launcher but it can't mount for some time.
  • Pilot6
    Pilot6 over 8 years
    The delay is not Ubuntu related. It is an Android bug. I plugged of the phone from one computer and connected to another. There is the same delay.
  • user.dz
    user.dz over 8 years
    @Pilot6 , Updated answer, with an easy way to catch it. Is the other computer not a Linux one?
  • Pilot6
    Pilot6 over 8 years
    I do not have non-linux computers ;-) But I have plenty of linux. The problem is that after the phone is disconnected it can't connect again for a while. I do not think it is linux related.
  • Pilot6
    Pilot6 over 8 years
    It was really an Android problem with the delay. I reinstalled firmware for another reason and the delay disappeared.
  • gmk57
    gmk57 over 3 years
    Unfortunately in my case ID_USB_INTERFACES were always the same, so I had to skip entire VID/PID.
  • user.dz
    user.dz over 3 years
    @gmk57, There is still a twisted workaround it even so because they usually appear in order. My hint here is to create script that creates a file (empty lock file) ignore the 1st interface but does not skip 2nd interface if it find our existing lock file, and delete the file. If you can test it and make new question and answer it will be worth sharing. I can help also with the udev rule and answer if you luck knowledge, just make new question and share info following this answer posted above.
  • gmk57
    gmk57 over 3 years
    @user.dz, I probably won't bother with scripts, because I mostly access this specific device through ADB, which still works and allows to copy files too. So I'm fine with your solution, just without ID_USB_INTERFACES part. Anyway, thanks for your willingness to help! :) BTW, I filed an issue to libmtp, maybe they can fix it in a more generic way.