linux控制USB的绑定/解绑

今天工作中遇到一个问题, 要用代码实现USB的enable和disable. 谷歌了一番, 最终找到理想答案, 我在这里做一个简短porting. 来源:墙外某博

  • 首先通过lsusb -t来查看USB端口信息:
    /:  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, 来确定到底是哪个BUS的哪个port.)
在这里我要操作BUS 02的port 1下的port 6.

  • 执行以下command去unbind这个USB:
echo '2-1.6' | sudo tee /sys/bus/usb/drivers/usb/unbind

其实用不用tee都行, 直接重定向也可以. 需要注意的是, 所要echo的字符串:"$bus_num"-"$port1_num"."$port2_num"
大概是这么个format.

  • 重新bind的command, 只需要把上面command中的unbind改成bind.
    echo '2-1.6' | sudo tee /sys/bus/usb/drivers/usb/bind

That's it.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • USB设备驱动程序用来驱动相应的USB设备,USB设备驱动用usb_driver表示,它主要用来将USB设备挂接到...
    Joe_HUST阅读 6,334评论 1 5
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,853评论 18 139
  • linux设备模型bus,device,driver作者 codercjg 在 10 十一月 2015, 2:43...
    codercjg阅读 421评论 0 1
  • linux设备模型bus,device,driver作者 codercjg 在 10 十一月 2015, 2:43...
    codercjg阅读 573评论 0 1
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,489评论 2 45