我们知道usb的设备和驱动是根据id进行匹配,具体的匹配过程如下
在kernel4.4/drivers/usb/storage/usb.c的storage_probe函数中加上WARN_ON(1)把相应的堆栈打印出来,如下
[ 74.336697] sprd-typec 41800000.spi:pmic@0:typec@380: now works as drp and is in Attached.SRC state,event 1,status 5 cc_status e002
[ 74.369709] musb-sprd 20200000.usb: sprd_musb_enable:HOST ENABLE 81
[ 74.369752] musb-sprd 20200000.usb: enter into resume mode
[ 74.370473] musb-sprd 20200000.usb: is running as HOST
[ 74.543117] musb_interrupt
[ 74.543151] musb-hdrc musb-hdrc.0.auto: ** IRQ host usb0010 tx0000 rx0000
[ 74.543165] musb-hdrc musb-hdrc.0.auto: <== DevCtl=5d, int_usb=0x10
[ 74.543198] musb-hdrc musb-hdrc.0.auto: CONNECT (a_host) devctl 5d
[ 74.543274] usb usb1: usb wakeup-resume
[ 74.543308] hub 1-0:1.0: hub_resume
[ 74.543512] usb usb1-port1: status 0101 change 0001
[ 74.580904] n=0,value=0x0,addr_ic=0x2a4,val=0x0
[ 74.643842] kick_hub_wq
[ 74.643895] queue_work hub_event
[ 74.643944] hub_event
[ 74.643968] hub 1-0:1.0: state 8 ports 1 chg 0002 evt 0000
[ 74.643993] port_event
[ 74.644210] usb usb1-port1: status 0101, change 0000, 12 Mb/s
[ 74.644230] hub_port_connect
[ 74.644301] hub_port_init
[ 74.756284] usb 1-1: new high-speed USB device number 2 using musb-hdrc
[ 74.884839] usb 1-1: udev 2, busnum 1, minor = 1
[ 74.884862] usb 1-1: New USB device found, idVendor=1908, idProduct=0226
[ 74.884883] usb 1-1: New USB device strings: Mfr=0, Product=0, SerialNumber=0
[ 74.888995] generic_probe
[ 74.891031] ------------[ cut here ]------------
[ 74.891057] WARNING: at /kernel4.4/drivers/usb/storage/usb.c:1089
[ 74.891164] CPU: 0 PID: 723 Comm: kworker/0:2 Tainted: G W O 4.4.147 #34
[ 74.891177] Hardware name: Spreadtrum SC9863A Board (DT)
[ 74.891202] Workqueue: usb_hub_wq hub_event
[ 74.891221] task: ffffffc06eac2080 task.stack: ffffffc0733ec000
[ 74.891241] PC is at storage_probe+0x2c/0x110
[ 74.891258] LR is at usb_probe_interface+0x1b8/0x30c
[ 74.904736] [<ffffff80086b05c8>] storage_probe+0x2c/0x110
[ 74.904756] [<ffffff800867e194>] usb_probe_interface+0x1b8/0x30c
[ 74.904780] [<ffffff800853858c>] driver_probe_device+0x204/0x45c
[ 74.904799] [<ffffff8008538928>] __device_attach_driver+0x94/0xd8
[ 74.904821] [<ffffff8008535db0>] bus_for_each_drv+0x74/0xc4
[ 74.904840] [<ffffff80085381c8>] __device_attach+0xd8/0x154
[ 74.904855] [<ffffff8008538b14>] device_initial_probe+0x24/0x30
[ 74.904875] [<ffffff800853758c>] bus_probe_device+0x9c/0xa4
[ 74.904895] [<ffffff8008534864>] device_add+0x42c/0x5a4
[ 74.904916] [<ffffff800867c24c>] usb_set_configuration+0x4a4/0x834
[ 74.904932] [<ffffff800868ace4>] generic_probe+0x48/0xac
[ 74.904952] [<ffffff800867db24>] usb_probe_device+0x48/0xa4
[ 74.904968] [<ffffff800853858c>] driver_probe_device+0x204/0x45c
[ 74.904983] [<ffffff8008538928>] __device_attach_driver+0x94/0xd8
[ 74.905000] [<ffffff8008535db0>] bus_for_each_drv+0x74/0xc4
[ 74.905015] [<ffffff80085381c8>] __device_attach+0xd8/0x154
[ 74.905030] [<ffffff8008538b14>] device_initial_probe+0x24/0x30
[ 74.905046] [<ffffff800853758c>] bus_probe_device+0x9c/0xa4
[ 74.905062] [<ffffff8008534864>] device_add+0x42c/0x5a4
[ 74.905082] [<ffffff8008671d40>] usb_new_device+0x248/0x47c
[ 74.905102] [<ffffff80086735d4>] hub_event+0x978/0x1154
[ 74.905125] [<ffffff80080c9024>] process_one_work+0x154/0x444
[ 74.905145] [<ffffff80080c9440>] worker_thread+0x12c/0x448
[ 74.905167] [<ffffff80080d0358>] kthread+0xe8/0xfc
[ 74.905188] [<ffffff8008085ab0>] ret_from_fork+0x10/0x20
[ 74.905427] usb-storage 1-1:1.0: USB Mass Storage device detected
[ 74.910746] scsi host0: usb-storage 1-1:1.0
[ 74.921651] hub_event
[ 74.921679] hub 1-0:1.0: state 8 ports 1 chg 0000 evt 0002
[ 74.921700] port_event
[ 74.921851] usb usb1-port1: enable change, status 00000503
usb设备和驱动的匹配过程稍有点复杂,关键代码如下
drivers/usb/core.c
static int __init usb_init(void)
{
bus_register(&usb_bus_type);
usb_register_device_driver(&usb_generic_driver, THIS_MODULE);
}
struct bus_type usb_bus_type = {
.name = "usb",
.match = usb_device_match,
.uevent = usb_uevent,
};
static inline int is_usb_device(const struct device *dev)
{
return dev->type == &usb_device_type;
}
static inline int is_usb_interface(const struct device *dev)
{
return dev->type == &usb_if_device_type;
}
static inline int is_usb_device_driver(struct device_driver *drv)
{
return container_of(drv, struct usbdrv_wrap, driver)->
for_devices;
}
static int usb_device_match(struct device *dev, struct device_driver *drv)
{
/* devices and interfaces are handled separately */
if (is_usb_device(dev)) {
/* interface drivers never match devices */
if (!is_usb_device_driver(drv))
return 0;
/* TODO: Add real matching code */
return 1;
} else if (is_usb_interface(dev)) {
struct usb_interface *intf;
struct usb_driver *usb_drv;
const struct usb_device_id *id;
/* device drivers never match interfaces */
if (is_usb_device_driver(drv))
return 0;
intf = to_usb_interface(dev);
usb_drv = to_usb_driver(drv);
id = usb_match_id(intf, usb_drv->id_table);
if (id)
return 1;
id = usb_match_dynamic_id(intf, usb_drv);
if (id)
return 1;
}
return 0;
}
相应的数据流向如下