usb驱动学习笔记-u盘加载流程(一)
hub_probe调用过程分析
自己整理的学习随笔内容,基于高通8953平台,由于是中途才想起来整理的,因此很多内容比较散乱,后期会一并补齐
开机默认从机模式,在开机后手动切换模式为主模式,此时OTG状态机处于OTG_STATE_A_IDLE状态,此时usb开始初始化root hub和XHCI主机控制器,下面是对hub_probe函数dump_stack的结果:
[<ffffffc0006ffa58>] hub_probe+0x58/0xa6c
[<ffffffc00070905c>] usb_probe_interface+0x1f4/0x288
[<ffffffc0005e4f64>] driver_probe_device+0x16c/0x378
[<ffffffc0005e52ec>] __device_attach_driver+0x90/0xa0
[<ffffffc0005e2dc8>] bus_for_each_drv+0x98/0xc8
[<ffffffc0005e4d34>] __device_attach+0x9c/0x130
[<ffffffc0005e545c>] device_initial_probe+0x24/0x30
[<ffffffc0005e4110>] bus_probe_device+0x38/0x98
[<ffffffc0005e1c50>] device_add+0x350/0x56c
[<ffffffc000707128>] usb_set_configuration+0x644/0x6ac
[<ffffffc0007135bc>] generic_probe+0x54/0x90
[<ffffffc000708e4c>] usb_probe_device+0x64/0x80
[<ffffffc0005e4f64>] driver_probe_device+0x16c/0x378
[<ffffffc0005e52ec>] __device_attach_driver+0x90/0xa0
[<ffffffc0005e2dc8>] bus_for_each_drv+0x98/0xc8
[<ffffffc0005e4d34>] __device_attach+0x9c/0x130
[<ffffffc0005e545c>] device_initial_probe+0x24/0x30
[<ffffffc0005e4110>] bus_probe_device+0x38/0x98
[<ffffffc0005e1c50>] device_add+0x350/0x56c
[<ffffffc0006fdbf4>] usb_new_device+0x2b8/0x3e4
[<ffffffc0007025a8>] usb_add_hcd+0x634/0x884
[<ffffffc000745a30>] xhci_plat_probe+0x214/0x480
[<ffffffc0005e7188>] platform_drv_probe+0x44/0x90
[<ffffffc0005e4f64>] driver_probe_device+0x16c/0x378
[<ffffffc0005e52ec>] __device_attach_driver+0x90/0xa0
[<ffffffc0005e2dc8>] bus_for_each_drv+0x98/0xc8
[<ffffffc0005e4d34>] __device_attach+0x9c/0x130
[<ffffffc0005e545c>] device_initial_probe+0x24/0x30
[<ffffffc0005e4110>] bus_probe_device+0x38/0x98
[<ffffffc0005e1c50>] device_add+0x350/0x56c
[<ffffffc0005e6fc8>] platform_device_add+0x19c/0x20c
[<ffffffc000739a7c>] dwc3_otg_start_host+0x108/0x394
[<ffffffc00073d500>] dwc3_msm_otg_sm_work+0x7d8/0x99c
[<ffffffc0000c8274>] process_one_work+0x270/0x41c
[<ffffffc0000c89e0>] worker_thread+0x304/0x428
[<ffffffc0000cd3ec>] kthread+0xec/0xf4
下面是到generic_probe的的主流程:
dwc3_otg_start_host \\主模式初始化的主要函数
platform_device_add(dwc->xhci)//xhci device初始化在dwc3_probe的接口dwc3_host_init中完成
xhci_plat_probe(struct platform_device *pdev)
usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev))
usb_add_hcd(hcd, irq, IRQF_SHARED)
usb_alloc_dev(NULL, &hcd->self, 0)//分配root_hub的usb_device
register_root_hub(hcd)
usb_new_device(usb_dev)
device_add(&udev->dev)
usb_probe_device()
generic_probe()
上面的函数调用流程到generic_probe结束,我们梳理一下上面的主要流程:
1. dwc3_otg_start_host函数除了初始化dwc3相关寄存器设置外,还调用了platform_device_add用于加载XHCI主机控制器设备(这里有人肯定会奇怪dwc->xhci这个platform_device从何而来,关于这点本由于篇幅原因不做详解,感兴趣的可以看dwc3_host_init函数的相关实现),当设备被加载到platform总线后,就会触发device_attach最终回调XHCI驱动的probe函数,也就是xhci_plat_probe
2. 关于xhci_plat_probe由于涉及XHCI主机控制器相关协议,本章内容只分析上面流程相关的函数,对于XHCI主机控制器的内容,后续有机会会专门整理,usb_hcd结构体是主机控制器的核心,每一个主机控制器都需要分配一个对应的usb_hcd结构,这里面涉及到两个函数usb_create_hcd和usb_add_hcd,usb_add_hcd中我们只关注usb_alloc_dev和register_root_hub中的usb_new_device,这两个函数涉及root hub的usb_device结构体的申请和初始化,下面是usb_alloc_dev函数实现,删减了部分不关注内容
405 struct usb_device *usb_alloc_dev(struct usb_device *parent,
406 struct usb_bus *bus, unsigned port1)
407 {
408 struct usb_device *dev;
409 struct usb_hcd *usb_hcd = bus_to_hcd(bus);
410 unsigned root_hub = 0;
411
412 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
413 if (!dev)
414 return NULL;
415
428 device_initialize(&dev->dev);
429 dev->dev.bus = &usb_bus_type;
430 dev->dev.type = &usb_device_type;
431 dev->dev.groups = usb_device_groups;
432 dev->dev.dma_mask = bus->controller->dma_mask;
433 set_dev_node(&dev->dev, dev_to_node(bus->controller));
434 dev->state = USB_STATE_ATTACHED;
453 if (unlikely(!parent)) {
454 dev->devpath[0] = '0';
455 dev->route = 0;
456
457 dev->dev.parent = bus->controller;
458 dev_set_name(&dev->dev, "usb%d", bus->busnum);
459 root_hub = 1;
460 }
478
479 dev->dev.parent = &parent->dev;
480 dev_set_name(&dev->dev, "%d-%s", bus->busnum, dev->devpath);
481
482 /* hub driver sets up TT records */
483 }
484
485 dev->portnum = port1;
486 dev->bus = bus;
487 dev->parent = parent;
488 INIT_LIST_HEAD(&dev->filelist);
489
496 if (root_hub) /* Root hub always ok [and always wired] */
497 dev->authorized = 1;
502 return dev;
503 }
上面函数主要做了三件事:
(1) 申请分配一个usb_device结构体
(2) 初始化device结构体,这里重点关注.bus和.type的赋值
(3) 初始化针对root hub相关设置,root hub 顾名思义就是根hub,通常和主机控制器一一绑定,现在的roothub基本就集成在主机控制器中,作为跟hub是没有父hub存在的
usb_bus_type结构体如下,match用于后续和driver匹配,具体的match函数在下面流程分析
1888 struct bus_type usb_bus_type = {
1889 .name = "usb",
1890 .match = usb_device_match,
1891 .uevent = usb_uevent,
1892 };
下面来看usb_new_device,具体实现如下,省略不关注部分
int usb_new_device(struct usb_device *udev)
2445 {
2446 int err;
2447
2467 err = usb_enumerate_device(udev); /* Read descriptors */
2477 /* Tell the world! */
2478 announce_device(udev);
2479
2498 err = device_add(&udev->dev);
2499 if (err) {
2500 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2501 goto fail;
2502 }
2536 }
上面的主要工作有两个:
(1) 通过usb_enumerate_device函数获取usb设备配置,包括各种描述符信息
(2) 然后就是root_hubd的device_add,此函数会触发设备和驱动的匹配,最终调用generic_probe
3.下面具体来看下从device_add到generic_probe的匹配过程
在分析上面的流程之前,首先需要了解usb子系统中的两种驱动的注册方式,这两种注册方式分别是usb_register_driver()和usb_register_device_driver(),先看下usb_register_device_driver
884 int usb_register_device_driver(struct usb_device_driver *new_udriver,
885 struct module *owner)
886 {
887 int retval = 0;
888
889 if (usb_disabled())
890 return -ENODEV;
891
892 new_udriver->drvwrap.for_devices = 1;//注意这里的标志,用于bus.match的匹配
893 new_udriver->drvwrap.driver.name = new_udriver->name;
894 new_udriver->drvwrap.driver.bus = &usb_bus_type;
895 new_udriver->drvwrap.driver.probe = usb_probe_device;
896 new_udriver->drvwrap.driver.remove = usb_unbind_device;
897 new_udriver->drvwrap.driver.owner = owner;
898
899 retval = driver_register(&new_udriver->drvwrap.driver);
900
901 if (!retval)
902 pr_info("%s: registered new device driver %s\n",
903 usbcore_name, new_udriver->name);
904 else
905 printk(KERN_ERR "%s: error %d registering device "
906 " driver %s\n",
907 usbcore_name, retval, new_udriver->name);
908
909 return retval;
910 }
上面函数主要做了三件事情:
(1)给for_device赋值1,此值用于bus->match函数,后面具体分析的时候会看到
(2)driver赋值,我们主要关注894、895行.bus和.probe的赋值,从894行可以看到这里的driver的bus也是usb_bus_type,和root hub的usb_device的bus一致,表明它们是一对
(3)调用driver_register注册驱动
看到这里会发现hub的device和device_driver都有了,那么引申出两个问题,第一个是device_drivers是谁注册的?第二个问题是如何device和driver如何匹配
先看问题一,在讲问题一前有几个事情要注意,首先是我们目前的流程仅仅是因为我们改变了usb的模式产生的,也就是一开始说的由开机时的从机模式变为主机模式,整个过程中并没有设备接入,所以root hub和主机控制器的初始化和设备是否插入无关,但是这两个驱动的初始化必须发生在设备插入前,因为从usb的拓扑结构来看,所有的子hub和设备最终都会接入到root hub上,那么root hub的device_driver注册就必然在usb系统的一开始也就是usb_init函数里面
static int __init usb_init(void)
1256 {
1257 int retval;
1258 if (usb_disabled()) {
1259 pr_info("%s: USB support disabled\n", usbcore_name);
1260 return 0;
1261 }
1262 usb_init_pool_max();
1263
1264 retval = usb_debugfs_init();
1265 if (retval)
1266 goto out;
1267
1268 usb_acpi_register();
1269 retval = bus_register(&usb_bus_type);
1270 if (retval)
1271 goto bus_register_failed;
1272 retval = bus_register_notifier(&usb_bus_type, &usb_bus_nb);
1273 if (retval)
1274 goto bus_notifier_failed;
1275 retval = usb_major_init();
1276 if (retval)
1277 goto major_init_failed;
1278 retval = usb_register(&usbfs_driver);
1279 if (retval)
1280 goto driver_register_failed;
1281 retval = usb_devio_init();
1282 if (retval)
1283 goto usb_devio_init_failed;
1284 retval = usb_hub_init();
1285 if (retval)
1286 goto hub_init_failed;
1287 retval = usb_register_device_driver(&usb_generic_driver, THIS_MODULE);
1288 if (!retval)
1289 goto out;
1307 }
1287行就是使用usb_register_device_driver注册通用的device_driver,usb_generic_driver实现如下:
struct usb_device_driver usb_generic_driver = {
283 .name = "usb",
284 .probe = generic_probe,
285 .disconnect = generic_disconnect,
286 #ifdef CONFIG_PM
287 .suspend = generic_suspend,
288 .resume = generic_resume,
289 #endif
290 .supports_autosuspend = 1,
291 };
回到问题二,如何实现driver和device的匹配就要分下下上面讲到的usb_bus_type的match函数usb_device_match
static int usb_device_match(struct device *dev, struct device_driver *drv)
797 {
798 /* devices and interfaces are handled separately */
799 if (is_usb_device(dev)) {
800
801 /* interface drivers never match devices */
802 if (!is_usb_device_driver(drv))
803 return 0;
804
805 /* TODO: Add real matching code */
806 return 1;
807
808 } else if (is_usb_interface(dev)) {
809 struct usb_interface *intf;
810 struct usb_driver *usb_drv;
811 const struct usb_device_id *id;
812
813 /* device drivers never match interfaces */
814 if (is_usb_device_driver(drv))
815 return 0;
816
817 intf = to_usb_interface(dev);
818 usb_drv = to_usb_driver(drv);
819
820 id = usb_match_id(intf, usb_drv->id_table);
821 if (id)
822 return 1;
823
824 id = usb_match_dynamic_id(intf, usb_drv);
825 if (id)
826 return 1;
827 }
828
829 return 0;
830 }
799行代码展开如下,结合之前aloc_usb_dev函数可以看出这一项是满足的
static inline int is_usb_device(const struct device *dev)
125 {
126 return dev->type == &usb_device_type;
127 }
802行代码展开如下,看到熟悉的for_device了吧,这个之前在 usb_register_device_driver函数中赋值了,因此此项判断不满足,直接返回1,调用usb_probe_device函数,此函数最终调用generic_probe,关于这两个函数的调用过程,此部分不再详述
static inline int is_usb_device_driver(struct device_driver *drv)
156 {
157 return container_of(drv, struct usbdrv_wrap, driver)->
158 for_devices;
159 }
刚才在上面讲到usb子系统有两个驱动注册接口,usb_register_driver()和usb_register_device_driver 这两个接口实际代表usb接口驱动注册和USB设备驱动的注册,当完成设备驱动注册后,就开始接口驱动注册了,接下来分析usb_register_driver及其相关流程
首先usb_register_driver的调用流程是usb_init->usb_hub_init-> usb_register(&hub_driver)
usb_register函数是对usb_register_driver函数的封装
#define usb_register(driver) \
1276 usb_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
hub_driver结构体实现如下:
static struct usb_driver hub_driver = {
5323 .name = "hub",
5324 .probe = hub_probe,
5325 .disconnect = hub_disconnect,
5326 .suspend = hub_suspend,
5327 .resume = hub_resume,
5328 .reset_resume = hub_reset_resume,
5329 .pre_reset = hub_pre_reset,
5330 .post_reset = hub_post_reset,
5331 .unlocked_ioctl = hub_ioctl,
5332 .id_table = hub_id_table,
5333 .supports_autosuspend = 1,
5334 };
usb_register_driver的实现如下:
int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
946 const char *mod_name)
947 {
948 int retval = 0;
949
950 if (usb_disabled())
951 return -ENODEV;
952
953 new_driver->drvwrap.for_devices = 0;
954 new_driver->drvwrap.driver.name = new_driver->name;
955 new_driver->drvwrap.driver.bus = &usb_bus_type;
956 new_driver->drvwrap.driver.probe = usb_probe_interface;
957 new_driver->drvwrap.driver.remove = usb_unbind_interface;
958 new_driver->drvwrap.driver.owner = owner;
959 new_driver->drvwrap.driver.mod_name = mod_name;
960 spin_lock_init(&new_driver->dynids.lock);
961 INIT_LIST_HEAD(&new_driver->dynids.list);
962
963 retval = driver_register(&new_driver->drvwrap.driver);
964 if (retval)
965 goto out;
966
967 retval = usb_create_newid_files(new_driver);
968 if (retval)
969 goto out_newid;
970
971 pr_info("%s: registered new interface driver %s\n",
972 usbcore_name, new_driver->name);
973
974 out:
975 return retval;
976
977 out_newid:
978 driver_unregister(&new_driver->drvwrap.driver);
979
980 printk(KERN_ERR "%s: error %d registering interface "
981 " driver %s\n",
982 usbcore_name, retval, new_driver->name);
983 goto out;
984 }
从上面的代码实现能够看到基本与usb_register_device_driver函数一致,不同的地方在于这里for_devices的是0,probe函数是usb_probe_interface,到这里接口驱动注册上了,那么接口设备在哪注册?接口和设备如何匹配?接下来我们继续来看generic_probe到hub_probe的流程,下面贴一段全面dump_stack的内容
[<ffffffc0006ffa58>] hub_probe+0x58/0xa6c
[<ffffffc00070905c>] usb_probe_interface+0x1f4/0x288
[<ffffffc0005e4f64>] driver_probe_device+0x16c/0x378
[<ffffffc0005e52ec>] __device_attach_driver+0x90/0xa0
[<ffffffc0005e2dc8>] bus_for_each_drv+0x98/0xc8
[<ffffffc0005e4d34>] __device_attach+0x9c/0x130
[<ffffffc0005e545c>] device_initial_probe+0x24/0x30
[<ffffffc0005e4110>] bus_probe_device+0x38/0x98
[<ffffffc0005e1c50>] device_add+0x350/0x56c
[<ffffffc000707128>] usb_set_configuration+0x644/0x6ac
[<ffffffc0007135bc>] generic_probe+0x54/0x90
下面是generic_probe的实现
static int generic_probe(struct usb_device *udev)
161 {
162 int err, c;
163
164 /* Choose and set the configuration. This registers the interfaces
165 * with the driver core and lets interface drivers bind to them.
166 */
167 if (udev->authorized == 0)
168 dev_err(&udev->dev, "Device is not authorized for usage\n");
169 else {
170 c = usb_choose_configuration(udev);
171 if (c >= 0) {
172 err = usb_set_configuration(udev, c);
173 if (err && err != -ENODEV) {
174 dev_err(&udev->dev, "can't set config #%d, error %d\n",
175 c, err);
176 /* This need not be fatal. The user can try to
177 * set other configurations. */
178 }
179 }
180 }
181 /* USB device state == configured ... usable */
182 usb_notify_add_device(udev);
183
184 return 0;
185 }
对于root hub而言authorized 默认是1(详情参考usb_alloc_dev实现),usb_choose_configuration主要是选择hub的一个配置,根据usb2.0协议(参考11.23章节)hub只有一个配置,细节这里不再细讲,具体实现如下:
int usb_choose_configuration(struct usb_device *udev)
81 {
82 int i;
83 int num_configs;
84 int insufficient_power = 0;
85 struct usb_host_config *c, *best;
86
87 if (usb_device_is_owned(udev))
88 return 0;
89
90 best = NULL;
91 c = udev->config;
92 num_configs = udev->descriptor.bNumConfigurations;
93 for (i = 0; i < num_configs; (i++, c++)) {
94 struct usb_interface_descriptor *desc = NULL;
95
96 /* It's possible that a config has no interfaces! */
97 if (c->desc.bNumInterfaces > 0)
98 desc = &c->intf_cache[0]->altsetting->desc;
99
100 /*
101 * HP's USB bus-powered keyboard has only one configuration
102 * and it claims to be self-powered; other devices may have
103 * similar errors in their descriptors. If the next test
104 * were allowed to execute, such configurations would always
105 * be rejected and the devices would not work as expected.
106 * In the meantime, we run the risk of selecting a config
107 * that requires external power at a time when that power
108 * isn't available. It seems to be the lesser of two evils.
109 *
110 * Bugzilla #6448 reports a device that appears to crash
111 * when it receives a GET_DEVICE_STATUS request! We don't
112 * have any other way to tell whether a device is self-powered,
113 * but since we don't use that information anywhere but here,
114 * the call has been removed.
115 *
116 * Maybe the GET_DEVICE_STATUS call and the test below can
117 * be reinstated when device firmwares become more reliable.
118 * Don't hold your breath.
119 */
120 #if 0
121 /* Rule out self-powered configs for a bus-powered device */
122 if (bus_powered && (c->desc.bmAttributes &
123 USB_CONFIG_ATT_SELFPOWER))
124 continue;
125 #endif
126
127 /*
128 * The next test may not be as effective as it should be.
129 * Some hubs have errors in their descriptor, claiming
130 * to be self-powered when they are really bus-powered.
131 * We will overestimate the amount of current such hubs
132 * make available for each port.
133 *
134 * This is a fairly benign sort of failure. It won't
135 * cause us to reject configurations that we should have
136 * accepted.
137 */
138
139 /* Rule out configs that draw too much bus current */
140 if (usb_get_max_power(udev, c) > udev->bus_mA) {
141 insufficient_power++;
142 continue;
143 }
144
145 /* When the first config's first interface is one of Microsoft's
146 * pet nonstandard Ethernet-over-USB protocols, ignore it unless
147 * this kernel has enabled the necessary host side driver.
148 * But: Don't ignore it if it's the only config.
149 */
150 if (i == 0 && num_configs > 1 && desc &&
151 (is_rndis(desc) || is_activesync(desc))) {
152 #if !defined(CONFIG_USB_NET_RNDIS_HOST) && !defined(CONFIG_USB_NET_RNDIS_HOST_MODULE)
153 continue;
154 #else
155 best = c;
156 #endif
157 }
158
159 /* From the remaining configs, choose the first one whose
160 * first interface is for a non-vendor-specific class.
161 * Reason: Linux is more likely to have a class driver
162 * than a vendor-specific driver. */
163 else if (udev->descriptor.bDeviceClass !=
164 USB_CLASS_VENDOR_SPEC &&
165 (desc && desc->bInterfaceClass !=
166 USB_CLASS_VENDOR_SPEC)) {
167 best = c;
168 break;
169 }
170 /* If all the remaining configs are vendor-specific,
171 * choose the first one. */
172 else if (!best)
173 best = c;
174 }
175
176 if (insufficient_power > 0)
177 dev_info(&udev->dev, "rejected %d configuration%s "
178 "due to insufficient available bus power\n",
179 insufficient_power, plural(insufficient_power));
180
181 if (best) {
182 /* choose device preferred config */
183 i = get_usb_audio_config(udev->bos);
184 if (i < 0)
185 i = best->desc.bConfigurationValue;
186 dev_dbg(&udev->dev,
187 "configuration #%d chosen from %d choice%s\n",
188 i, num_configs, plural(num_configs));
189 } else {
190 i = -1;
191 dev_warn(&udev->dev,
192 "no configuration chosen from %d choice%s\n",
193 num_configs, plural(num_configs));
194 }
195 return i;
196 }
选择好配置之后调用usb_set_configuration函数进行配置,函数实现如下:
int usb_set_configuration(struct usb_device *dev, int configuration)
1694 {
1695 int i, ret;
1696 struct usb_host_config *cp = NULL;
1697 struct usb_interface **new_interfaces = NULL;
1698 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
1699 int n, nintf;
1700
1701 if (dev->authorized == 0 || configuration == -1)
1702 configuration = 0;
1703 else {
1704 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
1705 if (dev->config[i].desc.bConfigurationValue ==
1706 configuration) {
1707 cp = &dev->config[i];
1708 break;
1709 }
1710 }
1711 }
1712 if ((!cp && configuration != 0))
1713 return -EINVAL;
1714
1715 /* The USB spec says configuration 0 means unconfigured.
1716 * But if a device includes a configuration numbered 0,
1717 * we will accept it as a correctly configured state.
1718 * Use -1 if you really want to unconfigure the device.
1719 */
1720 if (cp && configuration == 0)
1721 dev_warn(&dev->dev, "config 0 descriptor??\n");
1722
1723 /* Allocate memory for new interfaces before doing anything else,
1724 * so that if we run out then nothing will have changed. */
1725 n = nintf = 0;
1726 if (cp) {
1727 nintf = cp->desc.bNumInterfaces;
1728 new_interfaces = kmalloc(nintf * sizeof(*new_interfaces),
1729 GFP_NOIO);
1730 if (!new_interfaces) {
1731 dev_err(&dev->dev, "Out of memory\n");
1732 return -ENOMEM;
1733 }
1734
1735 for (; n < nintf; ++n) {
1736 new_interfaces[n] = kzalloc(
1737 sizeof(struct usb_interface),
1738 GFP_NOIO);
1739 if (!new_interfaces[n]) {
1740 dev_err(&dev->dev, "Out of memory\n");
1741 ret = -ENOMEM;
1742 free_interfaces:
1743 while (--n >= 0)
1744 kfree(new_interfaces[n]);
1745 kfree(new_interfaces);
1746 return ret;
1747 }
1748 }
1749
1750 i = dev->bus_mA - usb_get_max_power(dev, cp);
1751 if (i < 0)
1752 dev_warn(&dev->dev, "new config #%d exceeds power "
1753 "limit by %dmA\n",
1754 configuration, -i);
1755 }
1756
1757 /* Wake up the device so we can send it the Set-Config request */
1758 ret = usb_autoresume_device(dev);
1759 if (ret)
1760 goto free_interfaces;
1761
1762 /* if it's already configured, clear out old state first.
1763 * getting rid of old interfaces means unbinding their drivers.
1764 */
1765 if (dev->state != USB_STATE_ADDRESS)
1766 usb_disable_device(dev, 1); /* Skip ep0 */
1767
1768 /* Get rid of pending async Set-Config requests for this device */
1769 cancel_async_set_config(dev);
1770
1771 /* Make sure we have bandwidth (and available HCD resources) for this
1772 * configuration. Remove endpoints from the schedule if we're dropping
1773 * this configuration to set configuration 0. After this point, the
1774 * host controller will not allow submissions to dropped endpoints. If
1775 * this call fails, the device state is unchanged.
1776 */
1777 mutex_lock(hcd->bandwidth_mutex);
1778 /* Disable LPM, and re-enable it once the new configuration is
1779 * installed, so that the xHCI driver can recalculate the U1/U2
1780 * timeouts.
1781 */
1782 if (dev->actconfig && usb_disable_lpm(dev)) {
1783 dev_err(&dev->dev, "%s Failed to disable LPM\n.", __func__);
1784 mutex_unlock(hcd->bandwidth_mutex);
1785 ret = -ENOMEM;
1786 goto free_interfaces;
1787 }
1788 ret = usb_hcd_alloc_bandwidth(dev, cp, NULL, NULL);
1789 if (ret < 0) {
1790 if (dev->actconfig)
1791 usb_enable_lpm(dev);
1792 mutex_unlock(hcd->bandwidth_mutex);
1793 usb_autosuspend_device(dev);
1794 goto free_interfaces;
1795 }
1796
1797 /*
1798 * Initialize the new interface structures and the
1799 * hc/hcd/usbcore interface/endpoint state.
1800 */
1801 for (i = 0; i < nintf; ++i) {
1802 struct usb_interface_cache *intfc;
1803 struct usb_interface *intf;
1804 struct usb_host_interface *alt;
1805
1806 cp->interface[i] = intf = new_interfaces[i];
1807 intfc = cp->intf_cache[i];
1808 intf->altsetting = intfc->altsetting;
1809 intf->num_altsetting = intfc->num_altsetting;
1810 kref_get(&intfc->ref);
1811
1812 alt = usb_altnum_to_altsetting(intf, 0);
1813
1814 /* No altsetting 0? We'll assume the first altsetting.
1815 * We could use a GetInterface call, but if a device is
1816 * so non-compliant that it doesn't have altsetting 0
1817 * then I wouldn't trust its reply anyway.
1818 */
1819 if (!alt)
1820 alt = &intf->altsetting[0];
1821
1822 intf->intf_assoc =
1823 find_iad(dev, cp, alt->desc.bInterfaceNumber);
1824 intf->cur_altsetting = alt;
1825 usb_enable_interface(dev, intf, true);
1826 intf->dev.parent = &dev->dev;
1827 intf->dev.driver = NULL;
1828 intf->dev.bus = &usb_bus_type;
1829 intf->dev.type = &usb_if_device_type;
1830 intf->dev.groups = usb_interface_groups;
1831 intf->dev.dma_mask = dev->dev.dma_mask;
1832 INIT_WORK(&intf->reset_ws, __usb_queue_reset_device);
1833 intf->minor = -1;
1834 device_initialize(&intf->dev);
1835 pm_runtime_no_callbacks(&intf->dev);
1836 dev_set_name(&intf->dev, "%d-%s:%d.%d",
1837 dev->bus->busnum, dev->devpath,
1838 configuration, alt->desc.bInterfaceNumber);
1839 usb_get_dev(dev);
1840 }
1841 kfree(new_interfaces);
1842
1843 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1844 USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
1845 NULL, 0, USB_CTRL_SET_TIMEOUT);
1846 if (ret < 0 && cp) {
1847 /*
1848 * All the old state is gone, so what else can we do?
1849 * The device is probably useless now anyway.
1850 */
1851 usb_hcd_alloc_bandwidth(dev, NULL, NULL, NULL);
1852 for (i = 0; i < nintf; ++i) {
1853 usb_disable_interface(dev, cp->interface[i], true);
1854 put_device(&cp->interface[i]->dev);
1855 cp->interface[i] = NULL;
1856 }
1857 cp = NULL;
1858 }
1859
1860 dev->actconfig = cp;
1861 mutex_unlock(hcd->bandwidth_mutex);
1862
1863 if (!cp) {
1864 usb_set_device_state(dev, USB_STATE_ADDRESS);
1865
1866 /* Leave LPM disabled while the device is unconfigured. */
1867 usb_autosuspend_device(dev);
1868 return ret;
1869 }
1870 usb_set_device_state(dev, USB_STATE_CONFIGURED);
1871
1872 if (cp->string == NULL &&
1873 !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
1874 cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
1875
1876 /* Now that the interfaces are installed, re-enable LPM. */
1877 usb_unlocked_enable_lpm(dev);
1878 /* Enable LTM if it was turned off by usb_disable_device. */
1879 usb_enable_ltm(dev);
1880
1881 /* Now that all the interfaces are set up, register them
1882 * to trigger binding of drivers to interfaces. probe()
1883 * routines may install different altsettings and may
1884 * claim() any interfaces not yet bound. Many class drivers
1885 * need that: CDC, audio, video, etc.
1886 */
1887 for (i = 0; i < nintf; ++i) {
1888 struct usb_interface *intf = cp->interface[i];
1889
1890 dev_dbg(&dev->dev,
1891 "adding %s (config #%d, interface %d)\n",
1892 dev_name(&intf->dev), configuration,
1893 intf->cur_altsetting->desc.bInterfaceNumber);
1894 device_enable_async_suspend(&intf->dev);
1895 ret = device_add(&intf->dev);
1896 if (ret != 0) {
1897 dev_err(&dev->dev, "device_add(%s) --> %d\n",
1898 dev_name(&intf->dev), ret);
1899 continue;
1900 }
1901 create_intf_ep_devs(intf);
1902 }
1903
1904 usb_autosuspend_device(dev);
1905 return 0;
1906 }
之前枚举过程中获取了设备相关的所有配置、接口、端点等描述符,上面的函数根据指定的配置给此配置下面的所有接口进行初始化分配device,从1828、1829行代码可以看到此时bus还是usb_bus_type,types是usb_if_device_type,到此时接口设备也有了,根据之前的流程会调用到usb_probe_interface->hub_probe,
592 struct device_type usb_if_device_type = {
1593 .name = "usb_interface",
1594 .release = usb_release_interface,
1595 .uevent = usb_if_uevent,
1596 };