如何创建 udev 规则来禁用某个 Android 设备?

如何创建 udev 规则来禁用某个 Android 设备?

我有一部 HTC Android 手机。

每次我将其连接到计算机时都会收到一条消息:

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

数字可能不同。但手机连接正确。

usb-devices输出

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

我还发现,在手机连接后的短时间内,USB 存储设备被声明

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

然后它断开连接并且类别更改为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)

答案1

dmesg输出结果可以清楚看出,手机连接了两次。

  • [ 40.632283] 第一个连接为 USB 总线 2 设备 3
  • [40.864690]已断开连接
  • [41.613079]第二个连接为 USB 总线 2 设备 4

问题是:

  • 两种电话连接模式都使用相同的属性idVendor/ idProduct/ bcdDevice

  • libmtp udev 规则仅使用idVendor/idProduct来过滤设备以及不重要​​/常见的属性ACTION!="add"ENV{MAJOR}!="?*"并且 SUBSYSTEM=="usb"

  • libmtp udev 规则使用ATTR(不是ATTRS),它确实以这个设备节点为目标/devices/pci0000:00/0000:00:13.2/usb2/2-1。因此我们不能使用接口节点详细信息,因为它们是这个节点的子节点。

要了解正在发生的事情,请使用udevadm monitor。仅查看事件而不查看详细信息。

  1. 拔下电话线
  2. 打开终端并运行udevadm monitor -u-u仅显示 UDEV 事件(用于干净的输出)
  3. 插上电话,等待一切平静下来
  4. Ctrl+C停止监控

要获取详细信息(环境属性),请使用udevadm monitor -u -p而是比较该节点的输出:

  • 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)

注意ID_USB_INTERFACES

另一种更简洁的方法是使用 udev 规则来仅收集我们需要的内容:

  1. /lib/udev/rules.d/69-libmtp.rules在 之后添加一条规则LABEL="libmtp_usb_rules"

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

    sudo udevadm control -R
    
  3. 重新插拔一次手机。

  4. 此规则应触发两次。比较该节点的输出:

    diff udev-phone-mtp_*.log

    应该带:(这只是有趣的部分)

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

在重新连接之前,Pilot6(OP)能够捕获到它的确切信息usb-devices


我建议将此规则添加/lib/udev/rules.d/69-libmtp.rules到 之后LABEL="libmtp_usb_rules"

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

相关内容