通过终端识别、断开和重新连接 USB 设备

通过终端识别、断开和重新连接 USB 设备

我使用 USB Huawei ME906s-158 设备,如所述华为 ME906s-158 无法与 Ubuntu 16.04 兼容。如果模块失去连接,似乎无法重新连接到网络。所以我的想法是编写一些脚本,在后台运行,检查 LTE 连接是否正常,如果没有,则释放 USB 设备并使用终端命令重新连接。我在如何从终端启用/禁用移动宽带?在此论坛主题上,但是代码无法在我的 Ubuntu 16.04 上运行,这可能是问题所在。

以此行为例:

modprobe -r -f uhci_hcd

导致modprobe: FATAL: Module uhci_hcd is builtin.错误。如果我使用 xhci_hcd(用于我的设备),情况也是如此。所以我的问题是如何更改 modprobe 命令以移除/连接我的设备?

使用dmesg设备连接为

usb 3-13.1: new high-speed USB device number 117 using xhci_hcd
usb 3-13.1: New USB device found, idVendor=12d1, idProduct=15c1
usb 3-13.1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 3-13.1: Product: HUAWEI Mobile
usb 3-13.1: Manufacturer: Huawei Technologies Co., Ltd.
usb 3-13.1: SerialNumber: 0123456789ABCDEF
cdc_ether 3-13.1:2.0 usb0: register 'cdc_ether' at usb-0000:00:14.0-13.1, CDC Ethernet Device, 02:1e:10:1f:00:00

设备编号会发生变化,USB 端口取决于所使用的硬件端口。我想我只是不明白设备是如何命名或安装以便使用 modprobe 寻址的。

相关信息lsusbBus 003 Device 028: ID 12d1:15c1 Huawei Technologies Co., Ltd.lsusb -t交付

/:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/15p, 480M
    |__ Port 12: Dev 11, If 0, Class=Hub, Driver=hub/4p, 480M
        |__ Port 3: Dev 14, If 1, Class=Human Interface Device, Driver=, 1.5M
        |__ Port 3: Dev 14, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
        |__ Port 1: Dev 64, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
        |__ Port 2: Dev 13, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
        |__ Port 2: Dev 13, If 1, Class=Human Interface Device, Driver=usbhid, 1.5M
    |__ Port 13: Dev 3, If 0, Class=Hub, Driver=hub/4p, 480M
    |__ Port 14: Dev 29, If 0, Class=Communications, Driver=cdc_ether, 480M
    |__ Port 14: Dev 29, If 1, Class=CDC Data, Driver=cdc_ether, 480M
    |__ Port 14: Dev 29, If 2, Class=Vendor Specific Class, Driver=option, 480M
    |__ Port 14: Dev 29, If 3, Class=Vendor Specific Class, Driver=option, 480M
    |__ Port 14: Dev 29, If 4, Class=Vendor Specific Class, Driver=option, 480M
    |__ Port 14: Dev 29, If 5, Class=Vendor Specific Class, Driver=option, 480M
    |__ Port 14: Dev 29, If 6, Class=Vendor Specific Class, Driver=option, 480M

非常感谢您的帮助。

以下是完整dmesg输出:

[748010.825136] usb 3-13.1: new high-speed USB device number 79 using xhci_hcd
[748010.926143] usb 3-13.1: New USB device found, idVendor=12d1, idProduct=15c1
[748010.926146] usb 3-13.1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[748010.926147] usb 3-13.1: Product: HUAWEI Mobile
[748010.926148] usb 3-13.1: Manufacturer: Huawei Technologies Co., Ltd.
[748010.926149] usb 3-13.1: SerialNumber: 0123456789ABCDEF
[748010.929581] cdc_ether 3-13.1:2.0 usb0: register 'cdc_ether' at usb-0000:00:14.0-13.1, CDC Ethernet Device, 02:1e:10:1f:00:00
[748010.930008] option 3-13.1:2.2: GSM modem (1-port) converter detected
[748010.930178] usb 3-13.1: GSM modem (1-port) converter now attached to ttyUSB0
[748010.930451] option 3-13.1:2.3: GSM modem (1-port) converter detected
[748010.930588] usb 3-13.1: GSM modem (1-port) converter now attached to ttyUSB1
[748010.930829] option 3-13.1:2.4: GSM modem (1-port) converter detected
[748010.930929] usb 3-13.1: GSM modem (1-port) converter now attached to ttyUSB2
[748010.931153] option 3-13.1:2.5: GSM modem (1-port) converter detected
[748010.931254] usb 3-13.1: GSM modem (1-port) converter now attached to ttyUSB3
[748010.931474] option 3-13.1:2.6: GSM modem (1-port) converter detected
[748010.931580] usb 3-13.1: GSM modem (1-port) converter now attached to ttyUSB4
[748010.959172] cdc_ether 3-13.1:2.0 enp0s20u13u1c2: renamed from usb0
[748010.986087] IPv6: ADDRCONF(NETDEV_UP): enp0s20u13u1c2: link is not ready
[748010.986238] cdc_ether 3-13.1:2.0 enp0s20u13u1c2: kevent 12 may have been dropped
[748010.990538] IPv6: ADDRCONF(NETDEV_UP): enp0s20u13u1c2: link is not ready

的完整输出tree /sys/devices/pci0000:00/0000:00:14.0/usb3/3-13/ | grep driver为:

│   │   ├── driver -> ../../../../../../../bus/usb/drivers/cdc_ether
│   │   ├── driver -> ../../../../../../../bus/usb/drivers/cdc_ether
│   │   ├── driver -> ../../../../../../../bus/usb/drivers/option
│   │   │   ├── driver -> ../../../../../../../../bus/usb-serial/drivers/option1
│   │   ├── driver -> ../../../../../../../bus/usb/drivers/option
│   │   │   ├── driver -> ../../../../../../../../bus/usb-serial/drivers/option1
│   │   ├── driver -> ../../../../../../../bus/usb/drivers/option
│   │   │   ├── driver -> ../../../../../../../../bus/usb-serial/drivers/option1
│   │   ├── driver -> ../../../../../../../bus/usb/drivers/option
│   │   │   ├── driver -> ../../../../../../../../bus/usb-serial/drivers/option1
│   │   ├── driver -> ../../../../../../../bus/usb/drivers/option
│   │   │   ├── driver -> ../../../../../../../../bus/usb-serial/drivers/option1
│   ├── driver -> ../../../../../../bus/usb/drivers/usb
│   ├── driver -> ../../../../../../bus/usb/drivers/hub
├── driver -> ../../../../../bus/usb/drivers/usb

答案1

据我所知,modprobe它用于添加/删除模块/驱动程序。使用该命令,modprobe -r -f uhci_hcd您尝试删除内核内置的模块请参阅有关 Unix 和 Linux 的此文章

如果您想停用 USB 设备然后重新激活它,请查看此处的帖子:通过 CLI 断开并重新连接 USB 连接

或者尝试这里更简单的方法:有没有办法使用软件关闭 USB 设备?

一些有用的信息可以在这个 kernel.org 页面上有关电源管理的内容. 在 USB 端口电源控制下以及其下方的端口电源控制用户界面下。

我正在运行 Lubuntu 16.04。我用于测试的 USB 设备是一个 8GB 的​​ USB 棒。

首先打开一个终端并输入dmesg -w。接下来,插入您的 USB 设备。

[ 1897.772736] usb 2-2: new high-speed USB device number 7 using ehci-pci
[ 1897.907178] usb 2-2: New USB device found, idVendor=13fe, idProduct=5500
[ 1897.907185] usb 2-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 1897.907189] usb 2-2: Product: Silicon-Power8G
[ 1897.907193] usb 2-2: Manufacturer: UFD 3.0
[ 1897.907196] usb 2-2: SerialNumber: P1602059070C691668B63659
[ 1897.907802] usb-storage 2-2:1.0: USB Mass Storage device detected
[ 1897.909153] scsi host22: usb-storage 2-2:1.0
[ 1898.910257] scsi 22:0:0:0: Direct-Access     UFD 3.0  Silicon-Power8G  PMAP PQ: 0 ANSI: 6
[ 1898.910896] sd 22:0:0:0: Attached scsi generic sg4 type 0
[ 1898.911521] sd 22:0:0:0: [sdd] 15126528 512-byte logical blocks: (7.74 GB/7.21 GiB)
[ 1898.912261] sd 22:0:0:0: [sdd] Write Protect is off
[ 1898.912266] sd 22:0:0:0: [sdd] Mode Sense: 45 00 00 00
[ 1898.912982] sd 22:0:0:0: [sdd] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
[ 1898.950371]  sdd:
[ 1898.953487] sd 22:0:0:0: [sdd] Attached SCSI removable disk
[ 1899.243793] FAT-fs (sdd): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.

这里我们可以看到,一个新ehci-pci设备usb 2-2已被识别。几行之后,变为usb 2-2,最后变为 这些数字到底是什么意思,我不得而知。它与 USB 端口有关,但那不是全部。至少它是一个标识符。usb-storage 2-2:1.0scsi 22:0:0:0sd 22:0:0:0

无论如何,现在我们进入/sys/文件夹。在里面的某个地方,/sys/我们会找到负责我们的 USB 设备的驱动程序。我们想unbind从该驱动程序访问我们的设备。因此,我们需要找出驱动程序的路径及其unbind命令。我们将使用该tree命令。

我们输入tree /sys/ | grep 22:0:0:0

alex@ga-P55A-UD5:~$ tree /sys/ | grep 22:0:0:0
│   ├── sdd -> ../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0/block/sdd
│   │   │   ├── 22:0:0:0 -> ../../../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0
│   │   │   │   ├── 22:0:0:0 -> ../../../../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0
│   │   ├── sdd -> ../../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0/block/sdd
│   │   ├── 22:0:0:0 -> ../../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0/bsg/22:0:0:0
│   │   ├── 22:0:0:0 -> ../../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0/scsi_device/22:0:0:0
│   │   ├── 22:0:0:0 -> ../../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0/scsi_disk/22:0:0:0
│   │   └── sg4 -> ../../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0/scsi_generic/sg4
│   │   ├── 8:48 -> ../../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0/block/sdd
│       ├── 21:4 -> ../../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0/scsi_generic/sg4
│       ├── 249:4 -> ../../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0/bsg/22:0:0:0
│   │   │   │   │   │   │   │   ├── 22:0:0:0
│   │   │   │   │   │   │   │   │   │       ├── device -> ../../../22:0:0:0
│   │   │   │   │   │   │   │   │   │   └── 22:0:0:0
│   │   │   │   │   │   │   │   │   │       ├── device -> ../../../22:0:0:0
│   │   │   │   │   │   │   │   │   │   └── 22:0:0:0
│   │   │   │   │   │   │   │   │   │       ├── device -> ../../../22:0:0:0
│   │   │   │   │   │   │   │   │   │   └── 22:0:0:0
│   │   │   │   │   │   │   │   │   │       ├── device -> ../../../22:0:0:0
│   │   │   │   │   │   │   │   │   │       ├── device -> ../../../22:0:0:0

第一行已经告诉我们要去哪里:

../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0/block/sdd

请记住,我们仍在驱动程序文件夹中的某个位置搜索命令unbind,该命令以某种方式附加到我们的设备上。我们的设备,关于 ,dmesg也称为2-2:1.0,因此我们将其用作 的父文件夹tree

tree /sys/devices/pci0000\:00/0000\:00\:1d.7/usb2/2-2/2-2\:1.0/ | grep driver
alex@ga-P55A-UD5:~$ tree /sys/devices/pci0000\:00/0000\:00\:1d.7/usb2/2-2/2-2\:1.0/ | grep driver
├── driver -> ../../../../../../bus/usb/drivers/usb-storage
│   │   │   ├── driver -> ../../../../../../../../../bus/scsi/drivers/sd

现在我们已经找到了连接到 USB 设备的驱动程序的路径。

让我们ls /sys/bus/usb/drivers/usb-storage/

alex@ga-P55A-UD5:~$ ls /sys/bus/usb/drivers/usb-storage/
2-2:1.0  bind  module  new_id  remove_id  uevent  unbind

现在我们拥有了所需的所有信息,因此我们输入以下内容:

echo -n "2-2:1.0" | sudo tee /sys/bus/usb/drivers/usb-storage/unbind`

之后,因为你想要重新连接的效果:

echo -n "2-2:1.0" | sudo tee /sys/bus/usb/drivers/usb-storage/bind

输出dmesg -w

[ 3516.792135] usb-storage 2-2:1.0: USB Mass Storage device detected
[ 3516.792406] scsi host23: usb-storage 2-2:1.0
[ 3517.792201] scsi 23:0:0:0: Direct-Access     UFD 3.0  Silicon-Power8G  PMAP PQ: 0 ANSI: 6
[ 3517.792879] sd 23:0:0:0: Attached scsi generic sg4 type 0
[ 3517.793592] sd 23:0:0:0: [sdd] 15126528 512-byte logical blocks: (7.74 GB/7.21 GiB)
[ 3517.794340] sd 23:0:0:0: [sdd] Write Protect is off
[ 3517.794346] sd 23:0:0:0: [sdd] Mode Sense: 45 00 00 00
[ 3517.796083] sd 23:0:0:0: [sdd] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
[ 3517.801135]  sdd:
[ 3517.803788] sd 23:0:0:0: [sdd] Attached SCSI removable disk
[ 3518.096593] FAT-fs (sdd): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.

我希望这能帮助您找到解决方案。

如果您想了解有关该主题的更多信息:https://lwn.net/Articles/143397/

相关内容