在嵌入式项目里面,会扩展很多usb端口,由于主芯片本身的局限性还会增加2.0,3.0 的usb hub。有时还需要切换usb 触摸的链路,这样还需要添加switch 开关。
小板和子卡上面的usb hub 会与主板连接后,由于时序等其他兼容性问题,尝尝会导致hub 初始化失败或者挂死,这个时候就需要重置恢复正常工作。这里我们只分享一下hub 的软复位。
linux下一切设备皆文件,现在我们就找到连接在主板上面的hub文件。
Mstar:/ # lsusb
Bus 003 Device 001: ID 1d6b:0002
Bus 002 Device 004: ID 29bd:4101
Bus 001 Device 001: ID 1d6b:0002
Bus 001 Device 005: ID 090c:1000
Bus 001 Device 003: ID 2109:2817
Bus 001 Device 002: ID 1a40:0101
Bus 004 Device 001: ID 1d6b:0002
Bus 002 Device 001: ID 1d6b:0002
Bus 001 Device 004: ID 0a12:0001
上面一组vid&pid 唯一确定一个usb设备。
Mstar:/ # find / -type d -iname "hub"
/sys/bus/usb/drivers/hub
Mstar:/ # cd /sys/bus/usb/drivers/hub
在hub 文件夹下,有6个usb 设备1-0:1.0,1-1.1:1.0,1-1:1.0,2-0:1.0,3-0:1.0,4-0:1.0,6个usb 设备对应的文件在
/sys/devices/Mstar-ehci-2/usb1/1-0:1.0
/sys/devices/Mstar-ehci-2/usb1/1-1/1-1.1/1-1.1:1.0
/sys/devices/Mstar-ehci-2/usb1/1-1/1-1:1.0
/sys/devices/Mstar-ehci-1/usb2/2-0:1.0
/sys/devices/Mstar-ehci-3/usb3/3-0:1.0
/sys/devices/Mstar-ehci-4/usb4/4-0:1.0
如下图:
其中有两个比较重要的节点bind,unbind 使用方法可以参考:
https://www.ibm.com/developerworks/cn/linux/l-cn-sysfs/
以其中的/sys/devices/Mstar-ehci-2/usb1/1-1/1-1.1 为例
Mstar:/ # cd /sys/devices/Mstar-ehci-2/usb1/1-1/1-1.1/
Mstar:/sys/devices/Mstar-ehci-2/usb1/1-1/1-1.1 # ls
1-1.1:1.0 bMaxPower dev manufacturer speed
authorized bNumConfigurations devnum maxchild subsystem
avoid_reset_quirk bNumInterfaces devpath port uevent
bConfigurationValue bcdDevice driver power urbnum
bDeviceClass bmAttributes ep_00 product version
bDeviceProtocol busnum idProduct quirks
bDeviceSubClass configuration idVendor removable
bMaxPacketSize0 descriptors ltm_capable remove
Mstar:/sys/devices/Mstar-ehci-2/usb1/1-1/1-1.1 # cat product
USB2.0 Hub
Mstar:/sys/devices/Mstar-ehci-2/usb1/1-1/1-1.1 # cat idProduct
2817
Mstar:/sys/devices/Mstar-ehci-2/usb1/1-1/1-1.1 # cat idVendor
2109
Mstar:/sys/devices/Mstar-ehci-2/usb1/1-1/1-1.1 # cat quirks
0x0
Mstar:/sys/devices/Mstar-ehci-2/usb1/1-1/1-1.1 # cat speed
480
Mstar:/sys/devices/Mstar-ehci-2/usb1/1-1/1-1.1 # cat bMaxPower
0mA
Mstar:/sys/devices/Mstar-ehci-2/usb1/1-1/1-1.1 # cat bDeviceProtocol
02
Mstar:/sys/devices/Mstar-ehci-2/usb1/1-1/1-1.1 # cat bDeviceSubClass
00
Mstar:/sys/devices/Mstar-ehci-2/usb1/1-1/1-1.1 # cat bDeviceClass
09
通过这些属性结合usb的协议,我们基本可以了解这个usb 设备的信息。这里我们主要需要的idProduct,idVendor以及product信息来唯一确定我们需要操作。
Mstar:/sys/devices/Mstar-ehci-2/usb1/1-1/1-1.1 # echo 1 > remove
[ 8396.820996] hub_port_disable: 0 hub->err: 0
[ 8396.826702] usb 1-1.1: USB disconnect, device number 7
通过remove 节点来完成软重置。
#define VENDORPATH ("/sys/devices/Mstar-ehci-2/usb1/1-1/1-1.1/idVendor")
#define PRODUCTPATH ("/sys/devices/Mstar-ehci-2/usb1/1-1/1-1.1/idProduct")
static MAPI_BOOL resetFrontBoardBhub()
{
if(access(VENDORPATH, F_OK|R_OK) != 0)
{
printf("[%s][%d] can`t find the file [%s] .\n", __FUNCTION__, __LINE__, VENDORPATH);
return MAPI_TRUE;
}
if(access(PRODUCTPATH, F_OK|R_OK) != 0)
{
printf("[%s][%d] can`t find the file [%s] .\n", __FUNCTION__, __LINE__, PRODUCTPATH);
return MAPI_TRUE;
}
char u8VBuffer [8] = {
0x00};
char u8PBuffer [8] = {
0x00};
int uVendorId = 0;
int uProductId = 0;
int unVendorFd = open(VENDORPATH, O_RDONLY);
if(unVendorFd >= 0)
{
if(read(unVendorFd, u8VBuffer, 8) > 0)
{
uVendorId = atoi(u8VBuffer);
close(unVendorFd);
}
else
{
close(unVendorFd);
printf("[%s][%d] can`t get the vendor id .\n", __FUNCTION__, __LINE__);
return MAPI_FALSE;
}
}
int unProductFd = open(PRODUCTPATH, O_RDONLY);
if(unProductFd >= 0)
{
if(read(unProductFd, u8PBuffer, 8) > 0)
{
uProductId = atoi(u8PBuffer);
close(unProductFd);
}
else
{
printf("[%s][%d] can`t get the product id .\n", __FUNCTION__, __LINE__);
close(unProductFd);
return MAPI_FALSE;
}
}
printf("[%s][%d] the front board hub vid: 0x%4d, pid: 0x%4d\n",__FUNCTION__,__LINE__,uVendorId,uProductId);
if(2109 == uVendorId && 2817 == uProductId)
{
char rst_cmd[256];
memset(rst_cmd, 0x00, sizeof(rst_cmd));
snprintf(rst_cmd, 256, "echo 1 > %s","/sys/devices/Mstar-ehci-2/usb1/1-1/1-1.1/remove");
system((char * const)rst_cmd);
}
return MAPI_TRUE;
}
一般来说,hub 的状态出现异常,通过软重置可以恢复正常,这个是在hub 没有reset pin ,power pin 给主板控制的情况下的备选方案。
扩展:
/sys/devices/Mstar-ehci-1/usb2/
/sys/devices/Mstar-ehci-2/usb1/
/sys/devices/Mstar-ehci-3/usb3/
/sys/devices/Mstar-ehci-4/usb4/