在android 平台,我们再调试所有usb 设备时,硬件焊接好后,在内核usb 核心驱动正常情况下(当然只要USB外设遵循标准协议基本没有问题),我们都会通过lsusb 指令在确认usb 设置是否正常接入且被识别到,并且得到usb 设备的vid,pid,及bus id,device id。
lsub 命令
通过命令,我们可以查询到当前系统中,在usb 的三条总线分别插入的6个USB设备。
lsusb 源码分析
toyboy工具箱中的lsusb 工具源码在 external\toybox\toys\other\lsusb.c中
/* lsusb.c - list available USB devices
*
* Copyright 2013 Andre Renaud <andre@bluewatersys.com>
USE_LSUSB(NEWTOY(lsusb, NULL, TOYFLAG_USR|TOYFLAG_BIN))
config LSUSB
bool "lsusb"
default y
help
usage: lsusb
List USB hosts/devices.
*/
#include "toys.h"
static int list_device(struct dirtree *new)
{
FILE *file;
char *name;
int busnum = 0, devnum = 0, pid = 0, vid = 0;
if (!new->parent) return DIRTREE_RECURSE;
if (new->name[0] == '.') return 0;
name = dirtree_path(new, 0);
snprintf(toybuf, sizeof(toybuf), "%s/%s", name, "/uevent");
file = fopen(toybuf, "r"); // 打开uevent 设备文件
if (file) {
int count = 0;
// 循环读取/sys/bus/usb/devices/xxx/uevent 设备节点下的文件信息,一行一行读取
while (fgets(toybuf, sizeof(toybuf), file))
if (sscanf(toybuf, "BUSNUM=%u\n", &busnum) // 解析bus num
|| sscanf(toybuf, "DEVNUM=%u\n", &devnum) // 解析device num
|| sscanf(toybuf, "PRODUCT=%x/%x/", &pid, &vid)) count++; // 解析pid,vid
if (count == 3) // 解析完成后打印
printf("Bus %03d Device %03d: ID %04x:%04x\n", busnum, devnum, pid, vid);
fclose(file);
}
free(name);
return 0;
}
void lsusb_main(v