今天工作中遇到一个问题, 要用代码实现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.