2020/07/23

LinuxでのUSB機器の取り外し

LinuxでUSB機器を外す場合、マウントしていたらumountでマウント解除する必要があるが、それ以上に取り外すにはどうするか。
答えは、取り外したい機器のBus番号とPort番号を/sys/bus/usb/drivers/usb/unbindに書き込む。
# echo -n "4-1" > /sys/bus/usb/drivers/usb/unbind


赤で示したデバイスを取り外したい。
Bus番号とPort番号は黄色でハイライトしたとおり。
# lsusb -t
/:  Bus 04.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/4p, 5000M
    |__ Port 1: Dev 3, If 0, Class=Mass Storage, Driver=usb-storage, 5000M
    |__ Port 2: Dev 2, If 0, Class=Mass Storage, Driver=usbfs, 5000M
/:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/4p, 480M
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/2p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/8p, 480M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/2p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/6p, 480M
        |__ Port 2: Dev 3, If 0, Class=Human Interface Device, Driver=usbhid, 12M

ちなみに当該機器はdmesg上では以下のように記録されている。

[116939.536373] usb 4-1: new SuperSpeed Gen 1 USB device number 3 using xhci_hcd
[116939.551916] usb 4-1: New USB device found, idVendor=0789, idProduct=0185, bcdDevice= 1.00
[116939.551919] usb 4-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[116939.551921] usb 4-1: Product: LHR USB Device
[116939.551922] usb 4-1: Manufacturer: Logitec
[116939.551923] usb 4-1: SerialNumber: DS05EU30000011B1
[116939.552459] usb 4-1: Set SEL for device-initiated U1 failed.
[116939.552540] usb 4-1: Set SEL for device-initiated U2 failed.
[116939.552593] usb-storage 4-1:1.0: USB Mass Storage device detected
[116939.552724] scsi host6: usb-storage 4-1:1.0
[116941.610572] scsi 6:0:0:0: Direct-Access     Logitec  LHR USB Device   01.0 PQ: 0 ANSI: 0
[116941.610987] sd 6:0:0:0: Attached scsi generic sg2 type 0
[116941.611440] sd 6:0:0:0: [sdc] 1953525168 512-byte logical blocks: (1.00 TB/932 GiB)
[116941.611671] sd 6:0:0:0: [sdc] Write Protect is off
[116941.611675] sd 6:0:0:0: [sdc] Mode Sense: 03 00 00 00
[116941.611884] sd 6:0:0:0: [sdc] No Caching mode page found
[116941.611887] sd 6:0:0:0: [sdc] Assuming drive cache: write through
[116941.686644]  sdc: sdc1
[116941.687560] sd 6:0:0:0: [sdc] Attached SCSI disk
[117366.011841]  sdc: sdc1
[118963.664479] XFS (sdc1): Mounting V4 Filesystem
[118963.739968] XFS (sdc1): Ending clean mount
[118963.740002] xfs filesystem being mounted at /mnt/tmp supports timestamps until 2038 (0x7fffffff)
[119265.569178] XFS (sdc1): Unmounting Filesystem

Bus番号が4、Port番号が1なので、この機器は4-1。
# echo -n "4-1" > /sys/bus/usb/drivers/usb/unbind

上記コマンドの結果、lsusbの出力上から、取り外したかった機器がなくなっていることがわかる。dmesgには何もログは出ていなかった。
# lsusb -t
/:  Bus 04.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/4p, 5000M
    |__ Port 1: Dev 3, If 0, Class=Mass Storage, Driver=usb-storage, 5000M
    |__ Port 2: Dev 2, If 0, Class=Mass Storage, Driver=usbfs, 5000M
/:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/4p, 480M
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/2p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/8p, 480M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/2p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/6p, 480M
        |__ Port 2: Dev 3, If 0, Class=Human Interface Device, Driver=usbhid, 12M

取り外した後、再度接続したい場合は、/sys/bus/usb/drivers/usb/bindに書き込むと接続される。
# echo -n "4-1" > /sys/bus/usb/drivers/usb/bind

lsusbの結果は最初に記載したとおりなので省略するが、dmesgに接続された記録が出ている。
[119575.821240] usb 4-1: Set SEL for device-initiated U1 failed.
[119575.821315] usb 4-1: Set SEL for device-initiated U2 failed.
[119575.821401] usb-storage 4-1:1.0: USB Mass Storage device detected
[119575.821662] scsi host6: usb-storage 4-1:1.0
[119576.829073] scsi 6:0:0:0: Direct-Access     Logitec  LHR USB Device   01.0 PQ: 0 ANSI: 0
[119576.829299] sd 6:0:0:0: Attached scsi generic sg2 type 0
[119576.829496] sd 6:0:0:0: [sdc] 1953525168 512-byte logical blocks: (1.00 TB/932 GiB)
[119576.829683] sd 6:0:0:0: [sdc] Write Protect is off
[119576.829685] sd 6:0:0:0: [sdc] Mode Sense: 03 00 00 00
[119576.829883] sd 6:0:0:0: [sdc] No Caching mode page found
[119576.829884] sd 6:0:0:0: [sdc] Assuming drive cache: write through