侧边栏壁纸
  • 累计撰写 185 篇文章
  • 累计创建 77 个标签
  • 累计收到 17 条评论

目 录CONTENT

文章目录

linux中USB设备的绑定和解绑(类似于Windows中disable和enable)

码峰
2022-08-21 / 0 评论 / 0 点赞 / 1,357 阅读 / 574 字 / 正在检测是否收录...
广告 广告

前言

Linux中要通过命令实现USB设备的enable和disable,类似于在Windows设备管理器中禁用再启动设备一样,在某些场景,如调试USB固件时,重新加载了固件时,又不希望物理上插拔设备时,可以采用绑定和解绑这种方式。具体实现如下。

查看USB端口的信息

通过lsusb -t命令来查看当前系统中的USB设备的列表的信息,示例如下:

root@ferris-pc# lsusb -t
    /:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/3p, 480M
    |__ Port 1: Dev 2, If 0, Class=hub, Driver=hub/8p, 480M
        |__ Port 6: Dev 78, If 0, Class=vend., Driver=, 480M
        |__ Port 6: Dev 78, If 1, Class=vend., Driver=usbfs, 480M
        |__ Port 6: Dev 78, If 2, Class=comm., Driver=cdc_acm, 480M
        |__ Port 6: Dev 78, If 3, Class=data, Driver=cdc_acm, 480M
        |__ Port 6: Dev 78, If 4, Class=comm., Driver=cdc_acm, 480M
        |__ Port 6: Dev 78, If 5, Class=data, Driver=cdc_acm, 480M
        |__ Port 6: Dev 78, If 6, Class=comm., Driver=cdc_acm, 480M
        |__ Port 6: Dev 78, If 7, Class=data, Driver=cdc_acm, 480M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/3p, 480M
    |__ Port 1: Dev 2, If 0, Class=hub, Driver=hub/6p, 480M
        |__ Port 1: Dev 6, If 0, Class=HID, Driver=usbhid, 1.5M
        |__ Port 1: Dev 6, If 1, Class=HID, Driver=usbhid, 1.5M
        |__ Port 3: Dev 9, If 0, Class=HID, Driver=usbhid, 1.5M

如果不确定要控制的USB是哪一个,可以通过手动插拔一下该设备,每个USB硬件端口,port号都是固定的。比如,这里,我们要操作USB 02的port1下的port6上的设备。

执行unbind USB设备的命令

unbind相当于disable,通过以下命令来实现:

echo '2-1.6' | sudo tee /sys/bus/usb/drivers/usb/unbind

这里用不用tee命令都可以,也可以直接重定向。 需要注意的是, echo后面的字符串:“busnum""bus_num"-"port1_num”.“$port2_num”,以这样的格式。

重新bind USB设备的命令

bind相当于enable,只需要吧上面的命令中的unbind改为bind执行即可。

echo '2-1.6' | sudo tee /sys/bus/usb/drivers/usb/bind

以上就是在Linux中对USB设备进行类似于disable和enable的操作,是不是很简单?😄

0
广告 广告

评论区