当前位置:   article > 正文

linux USB驱动分析(三)USB host controller驱动分析_usb host驱动

usb host驱动

关键词:linux、驱动、usb、usb host controller、usb主机控制器

在rk3128 的 TRM 文件《Rockchip RK312X TRM V1.0 Part2 20160630.pdf》中,USB主机控制器 USB HOST CONTROLLER 通过 UTMI 接口与 USB HOST PHY 连接,USB HOST PHY 通过 USB 线与其它设备连接,USB 的 PHY 与以太网的 PHY 类似,用于数字信号和电气信号的转换。

USB 主机控制器使用函数 usb_create_hcd() 和 usb_add_hcd() 创建并注册,在函数usb_add_hcd() 中会调用函数phy_get() 获取 USB PHY驱动中注册的 struct phy *phy,并调用 USB PHY 驱动的初始化函数struct phy *phy->ops->init

  1. err = usb_add_hcd(hcd, irq, IRQF_SHARED)->
  2. struct phy *phy = phy_get(hcd->self.controller, "usb");
  3. index = of_property_match_string(dev->of_node, "phy-names", string);
  4. struct phy *phy = _of_phy_get(dev->of_node, index)-> //获取 usb phy 驱动中注册的 struct phy
  5. ret = of_parse_phandle_with_args(np, "phys", "#phy-cells", index, &args);
  6. struct phy_provider *phy_provider = of_phy_provider_lookup(args.np)->
  7. list_for_each_entry(phy_provider, &phy_provider_list, list)
  8. {
  9. /*
  10. 在 usb phy 驱动中会将注册的 struct phy *phy 添加到链表 phy_provider_list 中
  11. rockchip_usb2phy_probe()->
  12. provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate)->
  13. __devm_of_phy_provider_register((dev), THIS_MODULE, (xlate))
  14. struct phy_provider *phy_provider = __of_phy_provider_register(dev, owner, of_xlate)->
  15. struct phy_provider *phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
  16. phy_provider->dev = dev;
  17. phy_provider->owner = owner;
  18. phy_provider->of_xlate = of_xlate;
  19. list_add_tail(&phy_provider->list, &phy_provider_list);
  20. */
  21. if (phy_provider->dev->of_node == node)
  22. return phy_provider;
  23. for_each_child_of_node(phy_provider->dev->of_node, child)
  24. if (child == node)
  25. return phy_provider;
  26. }
  27. // 以 rk3128 为例, phy_provider->of_xlate = of_phy_simple_xlate
  28. phy = phy_provider->of_xlate(phy_provider->dev, &args);
  29. struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args *args)->
  30. class_dev_iter_init(&iter, phy_class, NULL, NULL);
  31. while ((dev = class_dev_iter_next(&iter)))
  32. {
  33. phy = to_phy(dev);
  34. class_dev_iter_exit(&iter);
  35. return phy;
  36. }
  37. return phy;
  38. hcd->phy = phy; // USB 主机控制器 struct usb_hcd *hcd 与 USB PHY 驱动 struct phy *phy 结合在一起
  39. retval = phy_init(phy)->
  40. ret = phy->ops->init(phy);//以rk3128为例: struct phy_ops rockchip_usb2phy_ops.init = rockchip_usb2phy_init
  41. static int rockchip_usb2phy_init(struct phy *phy)->
  42. if (rport->port_id == USB2PHY_PORT_OTG && rport->bvalid_irq > 0)
  43. {
  44. ret = rockchip_usb2phy_enable_vbus_irq(rphy, rport, true);
  45. ret = rockchip_usb2phy_enable_id_irq(rphy, rport, true);
  46. schedule_delayed_work(&rport->otg_sm_work, OTG_SCHEDULE_DELAY);
  47. }
  48. else if (rport->port_id == USB2PHY_PORT_HOST)
  49. {
  50. ret = rockchip_usb2phy_enable_line_irq(rphy, rport, true);
  51. schedule_delayed_work(&rport->sm_work, SCHEDULE_DELAY);
  52. }

USB主机控制器驱动结构体struct usb_hcd 

USB主机控制器驱动struct usb_hcd 的主要成员变量如下:

  1. struct usb_hcd {
  2. const char *product_desc; /* product/vendor string */
  3. int speed;
  4. char irq_descr[24]; /* driver + bus # */
  5. struct timer_list rh_timer; /* drives root-hub polling */
  6. struct urb *status_urb; /* the current status urb */
  7. const struct hc_driver *driver; /* hw-specific hooks */
  8. /*
  9. * OTG and some Host controllers need software interaction with phys;
  10. * other external phys should be software-transparent
  11. */
  12. struct usb_phy *usb_phy;
  13. struct phy *phy; // 与USB主机控制器连接的 USB PHY
  14. unsigned long flags;
  15. /* Flags that get set only during HCD registration or removal. */
  16. unsigned rh_registered:1;/* is root hub registered? */
  17. unsigned rh_pollable:1; /* may we poll the root hub? */
  18. unsigned int irq; /* irq allocated */
  19. void __iomem *regs; /* device memory/io */
  20. };

以rk3128为例,内核版本号为 linux-4.4,ehci 驱动 struct platform_driver ehci_platform_driver 与如下的EHCI USB 主机控制器的设备树匹配:

  1. usb_host_ehci: usb@101c0000 {
  2. compatible = "generic-ehci";
  3. reg = <0x101c0000 0x20000>;
  4. interrupts = <GIC_SPI 11 IRQ_TYPE_LEVEL_HIGH>;
  5. clocks = <&cru HCLK_HOST2>, <&u2phy>;
  6. clock-names = "usbhost", "utmi";
  7. phys = <&u2phy_host>;
  8. phy-names = "usb";
  9. status = "okay";
  10. };

在内核启动过程中会根据设备树 usb_host_ehci 注册平台设备/sys/devices/platform/101c0000.usb : 

 

  1. root@mxlos:/sys/devices/platform/101c0000.usb# ll
  2. total 0
  3. drwxr-xr-x 5 root root 0 Jan 1 00:53 ./
  4. drwxr-xr-x 78 root root 0 Jan 1 00:53 ../
  5. -rw-r--r-- 1 root root 4096 Jan 1 00:55 companion
  6. lrwxrwxrwx 1 root root 0 Jan 1 00:55 driver -> ../../../bus/platform/drivers/ehci-platform/
  7. -rw-r--r-- 1 root root 4096 Jan 1 00:55 driver_override
  8. -r--r--r-- 1 root root 4096 Jan 1 00:55 modalias
  9. lrwxrwxrwx 1 root root 0 Jan 1 00:55 of_node -> ../../../firmware/devicetree/base/usb@101c0000/
  10. -r--r--r-- 1 root root 4096 Jan 1 00:55 pools
  11. drwxr-xr-x 2 root root 0 Jan 1 00:55 power/
  12. lrwxrwxrwx 1 root root 0 Jan 1 00:53 subsystem -> ../../../bus/platform/
  13. -rw-r--r-- 1 root root 4096 Jan 1 00:53 uevent
  14. -rw-r--r-- 1 root root 4096 Jan 1 00:55 uframe_periodic_max
  15. drwxr-xr-x 6 root root 0 Jan 1 00:53 usb2/
  16. drwxr-xr-x 3 root root 0 Jan 1 00:53 usbmon/
  17. root@mxlos:/sys/devices/platform/101c0000.usb#
  18. root@mxlos:/sys/devices/platform/101c0000.usb# cat modalias
  19. of:NusbT<NULL>Cgeneric-ehci
  20. root@mxlos:/sys/devices/platform/101c0000.usb# cat uevent
  21. DRIVER=ehci-platform
  22. OF_NAME=usb
  23. OF_FULLNAME=/usb@101c0000
  24. OF_COMPATIBLE_0=generic-ehci
  25. OF_COMPATIBLE_N=1
  26. MODALIAS=of:NusbT<NULL>Cgeneric-ehci
  27. root@mxlos:/sys/devices/platform/101c0000.usb# cat companion

ehci 驱动 struct platform_driver ehci_platform_driver 的相关定义如下:

  1. static struct platform_driver ehci_platform_driver = {
  2. .id_table = ehci_platform_table,
  3. .probe = ehci_platform_probe,
  4. .remove = ehci_platform_remove,
  5. .shutdown = usb_hcd_platform_shutdown,
  6. .driver = {
  7. .name = "ehci-platform",
  8. .pm = &ehci_platform_pm_ops,
  9. .of_match_table = vt8500_ehci_ids,
  10. .acpi_match_table = ACPI_PTR(ehci_acpi_match),
  11. }
  12. };
  13. static const struct platform_device_id ehci_platform_table[] = {
  14. { "ehci-platform", 0 },
  15. { }
  16. };
  17. static const struct of_device_id vt8500_ehci_ids[] = {
  18. { .compatible = "via,vt8500-ehci", },
  19. { .compatible = "wm,prizm-ehci", },
  20. { .compatible = "generic-ehci", },
  21. { .compatible = "cavium,octeon-6335-ehci", },
  22. {}
  23. };

参见内核源码文件 linux-rk3128\drivers\usb\host\ehci-platform.c:

  1. module_init(ehci_platform_init);
  2. static int __init ehci_platform_init(void)
  3. {
  4. /*
  5. [ 2.788385] phy phy-20008000.syscon:usb2-phy@17c.0: charger = USB_FLOATING_CHARGER
  6. [ 2.797461] ehci_hcd: USB 2.0 'Enhanced' Host Controller (EHCI) Driver
  7. [ 2.804138] ehci-platform: EHCI generic platform driver
  8. #define DRIVER_DESC "EHCI generic platform driver"
  9. */
  10. pr_info("%s: " DRIVER_DESC "\n", hcd_name); //实际打印:ehci-platform: EHCI generic platform driver
  11. ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides)->
  12. *drv = ehci_hc_driver; // Copy the generic table to drv and then apply the overrides
  13. /*
  14. static const struct hc_driver ehci_hc_driver = {
  15. .description = hcd_name,
  16. .product_desc = "EHCI Host Controller",
  17. .hcd_priv_size = sizeof(struct ehci_hcd),
  18. // generic hardware linkage
  19. .irq = ehci_irq,
  20. .flags = HCD_MEMORY | HCD_USB2 | HCD_BH,
  21. //basic lifecycle operations
  22. .reset = ehci_setup,
  23. .start = ehci_run,
  24. .stop = ehci_stop,
  25. .shutdown = ehci_shutdown,
  26. //managing i/o requests and associated device resources
  27. .urb_enqueue = ehci_urb_enqueue,
  28. .urb_dequeue = ehci_urb_dequeue,
  29. .endpoint_disable = ehci_endpoint_disable,
  30. .endpoint_reset = ehci_endpoint_reset,
  31. .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
  32. // scheduling support
  33. .get_frame_number = ehci_get_frame,
  34. // root hub support
  35. .hub_status_data = ehci_hub_status_data,
  36. .hub_control = ehci_hub_control,
  37. .bus_suspend = ehci_bus_suspend,
  38. .bus_resume = ehci_bus_resume,
  39. .relinquish_port = ehci_relinquish_port,
  40. .port_handed_over = ehci_port_handed_over,
  41. // device support
  42. .free_dev = ehci_remove_device,
  43. };
  44. */
  45. return platform_driver_register(&ehci_platform_driver)-> //对应目录 /sys/bus/platform/drivers/ehci-platform
  46. /*
  47. root@mxlos:/sys/bus/platform/drivers/ehci-platform# pwd
  48. /sys/bus/platform/drivers/ehci-platform
  49. root@mxlos:/sys/bus/platform/drivers/ehci-platform# ls
  50. 101c0000.usb bind uevent unbind
  51. static struct platform_driver ehci_platform_driver = {
  52. .id_table = ehci_platform_table,
  53. .probe = ehci_platform_probe,
  54. .remove = ehci_platform_remove,
  55. .shutdown = usb_hcd_platform_shutdown,
  56. .driver = {
  57. .name = "ehci-platform",
  58. .pm = &ehci_platform_pm_ops,
  59. .of_match_table = vt8500_ehci_ids,
  60. .acpi_match_table = ACPI_PTR(ehci_acpi_match),
  61. }
  62. };
  63. static const struct platform_device_id ehci_platform_table[] = {
  64. { "ehci-platform", 0 },
  65. { }
  66. };
  67. static const struct of_device_id vt8500_ehci_ids[] = {
  68. { .compatible = "via,vt8500-ehci", },
  69. { .compatible = "wm,prizm-ehci", },
  70. { .compatible = "generic-ehci", },
  71. { .compatible = "cavium,octeon-6335-ehci", },
  72. {}
  73. };
  74. */
  75. __platform_driver_register(drv, THIS_MODULE)->
  76. drv->driver.owner = owner;
  77. drv->driver.bus = &platform_bus_type; //对应目录 /sys/bus/platform
  78. drv->driver.probe = platform_drv_probe;
  79. drv->driver.remove = platform_drv_remove;
  80. drv->driver.shutdown = platform_drv_shutdown;
  81. return driver_register(&drv->driver)->
  82. other = driver_find(drv->name, drv->bus);
  83. ret = bus_add_driver(drv)->
  84. kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,"%s", drv->name);
  85. error = driver_attach(drv)-> //驱动与设备匹配,这个过程会调用驱动的 probe = platform_drv_probe
  86. return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach)->
  87. klist_iter_init_node(&bus->p->klist_devices, &i, (start ? &start->p->knode_bus : NULL));
  88. fn(dev, data)=__driver_attach;
  89. static int __driver_attach(struct device *dev, void *data)->
  90. if (!driver_match_device(drv, dev))->
  91. //struct bus_type platform_bus_type.match= platform_match
  92. return drv->bus->match ? drv->bus->match(dev, drv) : 1;
  93. static int platform_match(struct device *dev, struct device_driver *drv)->
  94. // Attempt an OF style match first :尝试匹配设备树
  95. if (of_driver_match_device(dev, drv))
  96. return 1;
  97. /* Then try to match against the id table */
  98. if (pdrv->id_table)
  99. return platform_match_id(pdrv->id_table, pdev) != NULL;
  100. // fall-back to driver name match :尝试匹配设备名与驱动名
  101. return (strcmp(pdev->name, drv->name) == 0);
  102. {
  103. return 0;
  104. }
  105. if (!dev->driver)
  106. driver_probe_device(drv, dev);
  107. module_add_driver(drv->owner, drv);
  108. driver_create_file(drv, &driver_attr_uevent);
  109. ret = driver_add_groups(drv, drv->groups);
  110. kobject_uevent(&drv->p->kobj, KOBJ_ADD);
  111. }
  112. 最终调用ehci 驱动 struct platform_driver ehci_platform_driver 的 probe 函数:
  113. static int ehci_platform_probe(struct platform_device *dev)
  114. err = dma_coerce_mask_and_coherent(&dev->dev,pdata->dma_mask_64 ? DMA_BIT_MASK(64) : DMA_BIT_MASK(32));
  115. irq = platform_get_irq(dev, 0);
  116. struct usb_hcd *hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev,dev_name(&dev->dev))->
  117. return usb_create_shared_hcd(driver, dev, bus_name, NULL)-> //创建结构体 struct usb_hcd *hcd
  118. struct usb_hcd *hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
  119. usb_bus_init(&hcd->self)->
  120. bus->devnum_next = 1;
  121. bus->root_hub = NULL;
  122. bus->busnum = -1;
  123. bus->bandwidth_allocated = 0;
  124. bus->bandwidth_int_reqs = 0;
  125. bus->bandwidth_isoc_reqs = 0;
  126. hcd->self.controller = dev;
  127. hcd->self.bus_name = bus_name;
  128. hcd->self.uses_dma = (dev->dma_mask != NULL);
  129. init_timer(&hcd->rh_timer);
  130. hcd->rh_timer.function = rh_timer_func; //usb_hcd_poll_rh_status((struct usb_hcd *) _hcd);
  131. hcd->rh_timer.data = (unsigned long) hcd;
  132. INIT_WORK(&hcd->wakeup_work, hcd_resume_work);
  133. hcd->driver = driver = struct hc_driver ehci_hc_driver;
  134. /*
  135. static const struct hc_driver ehci_hc_driver = {
  136. .description = hcd_name, //static const char hcd_name [] = "ehci_hcd";
  137. .product_desc = "EHCI Host Controller",
  138. .irq = ehci_irq,
  139. //basic lifecycle operations
  140. .reset = ehci_setup,
  141. .start = ehci_run,
  142. .stop = ehci_stop,
  143. .shutdown = ehci_shutdown,
  144. //managing i/o requests and associated device resources
  145. .urb_enqueue = ehci_urb_enqueue,
  146. .urb_dequeue = ehci_urb_dequeue,
  147. .endpoint_disable = ehci_endpoint_disable,
  148. .endpoint_reset = ehci_endpoint_reset,
  149. .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
  150. // scheduling support
  151. .get_frame_number = ehci_get_frame,
  152. // root hub support
  153. .hub_status_data = ehci_hub_status_data,
  154. .hub_control = ehci_hub_control,
  155. .bus_suspend = ehci_bus_suspend,
  156. .bus_resume = ehci_bus_resume,
  157. .relinquish_port = ehci_relinquish_port,
  158. .port_handed_over = ehci_port_handed_over,
  159. // device support
  160. .free_dev = ehci_remove_device,
  161. };
  162. */
  163. hcd->speed = driver->flags & HCD_MASK;
  164. hcd->product_desc = (driver->product_desc) ? driver->product_desc : "USB Host Controller";
  165. platform_set_drvdata(dev, hcd);
  166. dev->dev.platform_data = pdata;
  167. priv = hcd_to_ehci_priv(hcd);
  168. ehci = hcd_to_ehci(hcd);
  169. //获取 USB 的 phys
  170. priv->num_phys = of_count_phandle_with_args(dev->dev.of_node,"phys", "#phy-cells");
  171. /*
  172. usb_host_ehci: usb@101c0000 {
  173. compatible = "generic-ehci";
  174. phys = <&u2phy_host>;
  175. };
  176. u2phy_host: host-port {
  177. #phy-cells = <0>;
  178. interrupts = <GIC_SPI 53 IRQ_TYPE_LEVEL_HIGH>;
  179. interrupt-names = "linestate";
  180. status = "okay";
  181. };
  182. */
  183. priv->num_phys = 0;
  184. priv->rst = devm_reset_control_get_optional(&dev->dev, NULL);
  185. pm_runtime_set_active(&dev->dev);
  186. pm_runtime_enable(&dev->dev);
  187. pm_runtime_get_sync(&dev->dev);
  188. res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
  189. hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
  190. hcd->rsrc_start = res_mem->start;
  191. hcd->rsrc_len = resource_size(res_mem);
  192. err = usb_add_hcd(hcd, irq, IRQF_SHARED)->
  193. struct phy *phy = phy_get(hcd->self.controller, "usb");
  194. index = of_property_match_string(dev->of_node, "phy-names", string);
  195. struct phy *phy = _of_phy_get(dev->of_node, index)-> //获取 usb phy 驱动中注册的 struct phy
  196. ret = of_parse_phandle_with_args(np, "phys", "#phy-cells", index, &args);
  197. struct phy_provider *phy_provider = of_phy_provider_lookup(args.np)->
  198. list_for_each_entry(phy_provider, &phy_provider_list, list)
  199. {
  200. /*
  201. 在 usb phy 驱动中会将注册的 struct phy *phy 添加到链表 phy_provider_list 中
  202. rockchip_usb2phy_probe()->
  203. provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate)->
  204. __devm_of_phy_provider_register((dev), THIS_MODULE, (xlate))
  205. struct phy_provider *phy_provider = __of_phy_provider_register(dev, owner, of_xlate)->
  206. struct phy_provider *phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
  207. phy_provider->dev = dev;
  208. phy_provider->owner = owner;
  209. phy_provider->of_xlate = of_xlate;
  210. list_add_tail(&phy_provider->list, &phy_provider_list);
  211. */
  212. if (phy_provider->dev->of_node == node)
  213. return phy_provider;
  214. for_each_child_of_node(phy_provider->dev->of_node, child)
  215. if (child == node)
  216. return phy_provider;
  217. }
  218. // 以 rk3128 为例, phy_provider->of_xlate = of_phy_simple_xlate
  219. phy = phy_provider->of_xlate(phy_provider->dev, &args);
  220. struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args *args)->
  221. class_dev_iter_init(&iter, phy_class, NULL, NULL);
  222. while ((dev = class_dev_iter_next(&iter)))
  223. {
  224. phy = to_phy(dev);
  225. class_dev_iter_exit(&iter);
  226. return phy;
  227. }
  228. return phy;
  229. retval = phy_init(phy)->
  230. ret = phy->ops->init(phy);//以rk3128为例: struct phy_ops rockchip_usb2phy_ops.init = rockchip_usb2phy_init
  231. static int rockchip_usb2phy_init(struct phy *phy)->
  232. if (rport->port_id == USB2PHY_PORT_OTG && rport->bvalid_irq > 0)
  233. {
  234. ret = rockchip_usb2phy_enable_vbus_irq(rphy, rport, true);
  235. ret = rockchip_usb2phy_enable_id_irq(rphy, rport, true);
  236. schedule_delayed_work(&rport->otg_sm_work, OTG_SCHEDULE_DELAY);
  237. }
  238. else if (rport->port_id == USB2PHY_PORT_HOST)
  239. {
  240. ret = rockchip_usb2phy_enable_line_irq(rphy, rport, true);
  241. schedule_delayed_work(&rport->sm_work, SCHEDULE_DELAY);
  242. }
  243. retval = phy_power_on(phy);
  244. dev_info(hcd->self.controller, "%s\n", hcd->product_desc); //实际打印:ehci-platform 101c0000.usb: EHCI Host Controller
  245. retval = hcd_buffer_create(hcd)->
  246. snprintf(name, sizeof(name), "buffer-%d", size);
  247. hcd->pool[i] = dma_pool_create(name, hcd->self.controller, size, size, 0);
  248. retval = usb_register_bus(&hcd->self)-> //registers the USB host controller with the usb core
  249. busnum = find_next_zero_bit(busmap, USB_MAXBUS, 1);
  250. bus->busnum = busnum;
  251. // Add it to the local list of buses
  252. list_add (&bus->bus_list, &usb_bus_list);
  253. usb_notify_add_bus(bus)->
  254. blocking_notifier_call_chain(&usb_notifier_list, USB_BUS_ADD, ubus);
  255. //对于 rk3128 来说,实际打印:ehci-platform 101c0000.usb: new USB bus registered, assigned bus number 2
  256. dev_info (bus->controller, "new USB bus registered, assigned bus number %d\n", bus->busnum);
  257. struct usb_device *rhdev = usb_alloc_dev(NULL, &hcd->self, 0)->
  258. struct usb_hcd *usb_hcd = bus_to_hcd(bus);
  259. struct usb_device *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  260. device_initialize(&dev->dev);
  261. dev->dev.bus = &usb_bus_type;
  262. dev->dev.type = &usb_device_type;
  263. dev->dev.groups = usb_device_groups;
  264. dev->dev.dma_mask = bus->controller->dma_mask;
  265. set_dev_node(&dev->dev, dev_to_node(bus->controller));
  266. dev->state = USB_STATE_ATTACHED;
  267. dev->lpm_disable_count = 1;
  268. atomic_set(&dev->urbnum, 0);
  269. INIT_LIST_HEAD(&dev->ep0.urb_list);
  270. dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;
  271. dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;
  272. /* ep0 maxpacket comes later, from device descriptor */
  273. usb_enable_endpoint(dev, &dev->ep0, false);
  274. dev->can_submit = 1;
  275. /* Save readable and stable topology id, distinguishing devices
  276. * by location for diagnostics, tools, driver model, etc. The
  277. * string is a path along hub ports, from the root. Each device's
  278. * dev->devpath will be stable until USB is re-cabled, and hubs
  279. * are often labeled with these port numbers. The name isn't
  280. * as stable: bus->busnum changes easily from modprobe order,
  281. * cardbus or pci hotplugging, and so on.
  282. */
  283. if (unlikely(!parent))
  284. {
  285. dev->devpath[0] = '0';
  286. dev->route = 0;
  287. dev->dev.parent = bus->controller;
  288. dev_set_name(&dev->dev, "usb%d", bus->busnum);
  289. root_hub = 1;
  290. }
  291. else
  292. {
  293. /* match any labeling on the hubs; it's one-based */
  294. if (parent->devpath[0] == '0')
  295. {
  296. snprintf(dev->devpath, sizeof dev->devpath,"%d", port1);
  297. /* Root ports are not counted in route string */
  298. dev->route = 0;
  299. }
  300. else
  301. {
  302. snprintf(dev->devpath, sizeof dev->devpath, "%s.%d", parent->devpath, port1);
  303. /* Route string assumes hubs have less than 16 ports */
  304. if (port1 < 15)
  305. dev->route = parent->route +(port1 << ((parent->level - 1)*4));
  306. else
  307. dev->route = parent->route +(15 << ((parent->level - 1)*4));
  308. }
  309. dev->dev.parent = &parent->dev;
  310. dev_set_name(&dev->dev, "%d-%s", bus->busnum, dev->devpath);
  311. /* hub driver sets up TT records */
  312. }
  313. dev->portnum = port1;
  314. dev->bus = bus;
  315. dev->parent = parent;
  316. INIT_LIST_HEAD(&dev->filelist);
  317. if (root_hub) /* Root hub always ok [and always wired] */
  318. dev->authorized = 1;
  319. else
  320. {
  321. dev->authorized = !!HCD_DEV_AUTHORIZED(usb_hcd);
  322. dev->wusb = usb_bus_is_wusb(bus) ? 1 : 0;
  323. }
  324. hcd->self.root_hub = rhdev; //设置主机控制器的根集线器
  325. switch (hcd->speed)
  326. {
  327. case HCD_USB11:
  328. rhdev->speed = USB_SPEED_FULL;
  329. break;
  330. case HCD_USB2:
  331. rhdev->speed = USB_SPEED_HIGH;
  332. break;
  333. case HCD_USB25:
  334. rhdev->speed = USB_SPEED_WIRELESS;
  335. break;
  336. case HCD_USB3:
  337. case HCD_USB31:
  338. rhdev->speed = USB_SPEED_SUPER;
  339. break;
  340. default:
  341. retval = -EINVAL;
  342. goto err_set_rh_speed;
  343. }
  344. retval = hcd->driver->reset(hcd) = struct hc_driver ehci_hc_driver.reset =ehci_setup
  345. int ehci_setup(struct usb_hcd *hcd)->
  346. ehci_init(hcd)->
  347. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  348. hrtimer_init(&ehci->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  349. ehci->hrtimer.function = ehci_hrtimer_func;
  350. ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
  351. hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
  352. ehci_mem_init(ehci, GFP_KERNEL)->
  353. /* QTDs for control/bulk/intr transfers */
  354. ehci->qtd_pool = dma_pool_create ("ehci_qtd",
  355. ehci_to_hcd(ehci)->self.controller,
  356. sizeof (struct ehci_qtd),
  357. 32 /* byte alignment (for hw parts) */,
  358. 4096 /* can't cross 4K */);
  359. /* QHs for control/bulk/intr transfers */
  360. ehci->qh_pool = dma_pool_create ("ehci_qh",
  361. ehci_to_hcd(ehci)->self.controller,
  362. sizeof(struct ehci_qh_hw),
  363. 32 /* byte alignment (for hw parts) */,
  364. 4096 /* can't cross 4K */);
  365. ehci->async = ehci_qh_alloc (ehci, flags)->
  366. struct ehci_qh *qh = kzalloc(sizeof *qh, GFP_ATOMIC);
  367. qh->hw = (struct ehci_qh_hw *)dma_pool_alloc(ehci->qh_pool, flags, &dma);
  368. qh->dummy = ehci_qtd_alloc (ehci, flags);
  369. struct ehci_qtd *qtd = dma_pool_alloc (ehci->qtd_pool, flags, &dma);
  370. /* ITD for high speed ISO transfers */
  371. ehci->itd_pool = dma_pool_create ("ehci_itd",
  372. ehci_to_hcd(ehci)->self.controller,
  373. sizeof (struct ehci_itd),
  374. 32 /* byte alignment (for hw parts) */,
  375. 4096 /* can't cross 4K */);
  376. /* SITD for full/low speed split ISO transfers */
  377. ehci->sitd_pool = dma_pool_create ("ehci_sitd",
  378. ehci_to_hcd(ehci)->self.controller,
  379. sizeof (struct ehci_sitd),
  380. 32 /* byte alignment (for hw parts) */,
  381. 4096 /* can't cross 4K */);
  382. /* Hardware periodic table */
  383. ehci->periodic = (__le32 *)
  384. dma_alloc_coherent (ehci_to_hcd(ehci)->self.controller,
  385. ehci->periodic_size * sizeof(__le32),
  386. &ehci->periodic_dma, flags);
  387. ehci_halt(ehci);
  388. ehci_reset(ehci);
  389. hcd->rh_pollable = 1;
  390. init_giveback_urb_bh(&hcd->high_prio_bh);
  391. init_giveback_urb_bh(&hcd->low_prio_bh);
  392. /* enable irqs just before we start the controller,
  393. * if the BIOS provides legacy PCI irqs.
  394. */
  395. if (usb_hcd_is_primary_hcd(hcd) && irqnum)
  396. {
  397. usb_hcd_request_irqs(hcd, irqnum, irqflags)->
  398. if (hcd->driver->irq) //struct hc_driver ehci_hc_driver.irq = ehci_irq
  399. {
  400. snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
  401. hcd->driver->description, hcd->self.busnum); //中断名为 :ehci_hcd:usb2
  402. request_irq(irqnum, &usb_hcd_irq, irqflags, hcd->irq_descr, hcd);
  403. irqreturn_t usb_hcd_irq (int irq, void *__hcd)->
  404. hcd->driver->irq(hcd) = struct hc_driver ehci_hc_driver.irq = ehci_irq(hdc)->
  405. /*
  406. EHCI 的 interrupt 在 HCD 中被分为了 6 种类型,如下宏定义:
  407. // these STS_* flags are also intr_enable bits (USBINTR)
  408. #define STS_IAA (1<<5) //Interrupted on async advance 当要从asynchronous schedule传输队列中移除一个QH时
  409. #define STS_FATAL (1<<4) /* such as some PCI access errors */
  410. #define STS_FLR (1<<3) /* frame list rolled over */
  411. #define STS_PCD (1<<2) /* port change detect:root hub上某个端口上有设备插入或拔出所产生的中断,
  412. 需要进一步的读取root hub上各port对应的register判断是插入还是拔出。 */
  413. #define STS_ERR (1<<1) /* "error" completion (overflow, ...) */
  414. #define STS_INT (1<<0) /* "normal" completion (short, ...) 正确完成一次数据传输后所发出的中断*/
  415. */
  416. spin_lock_irqsave(&ehci->lock, flags);
  417. status = ehci_readl(ehci, &ehci->regs->status);
  418. #define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
  419. masked_status = status & (INTR_MASK | STS_FLR);
  420. ehci_writel(ehci, masked_status, &ehci->regs->status);/* clear (just) interrupts:清除中断标志位。 */
  421. /* remote wakeup [4.3.1] */
  422. if (status & STS_PCD) //port change detect:root hub上某个端口上有设备插入或拔出所产生的中断
  423. {
  424. unsigned i = HCS_N_PORTS (ehci->hcs_params);
  425. u32 ppcd = ~0;
  426. /* kick root hub later */
  427. pcd_status = status;
  428. /* resume root hub? */
  429. if (ehci->rh_state == EHCI_RH_SUSPENDED)
  430. usb_hcd_resume_root_hub(hcd);
  431. /* get per-port change detect bits */
  432. if (ehci->has_ppcd)
  433. ppcd = status >> 16;
  434. while (i--) {
  435. int pstatus;
  436. ehci_dbg (ehci, "port %d remote wakeup\n", i + 1);
  437. usb_hcd_start_port_resume(&hcd->self, i);
  438. mod_timer(&hcd->rh_timer, ehci->reset_done[i])->
  439. expires = apply_slack(timer, expires);
  440. return __mod_timer(timer, expires, false, TIMER_NOT_PINNED);
  441. }
  442. }
  443. ehci_work (ehci)->
  444. ehci->scanning = true;
  445. if (ehci->intr_count > 0)
  446. {
  447. scan_intr(ehci)->
  448. qh_completions(ehci, qh)->
  449. list_entry (entry, struct ehci_qtd, qtd_list);
  450. ehci_urb_done(ehci, last->urb, last_status)->
  451. //ehci-platform 101c0000.usb: ehci_urb_done 1
  452. //urb ee71e780 ep1in status 0 len 1/1
  453. ehci_dbg (ehci,
  454. "%s %s urb %p ep%d%s status %d len %d/%d\n",
  455. __func__, urb->dev->devpath, urb,
  456. usb_pipeendpoint (urb->pipe),
  457. usb_pipein (urb->pipe) ? "in" : "out",
  458. status,
  459. urb->actual_length, urb->transfer_buffer_length);
  460. usb_hcd_giveback_urb(ehci_to_hcd(ehci), urb, status)->
  461. __usb_hcd_giveback_urb(urb)->
  462. urb->complete(urb);//urb->complete =hub_irq
  463. }
  464. turn_on_io_watchdog(ehci);
  465. if (pcd_status)
  466. usb_hcd_poll_rh_status(hcd)->
  467. length = hcd->driver->hub_status_data(hcd, buffer) = ehci_hub_status_data(hcd, buffer);
  468. //get per-port change detect bits:获取每个端口变化检测位
  469. ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16;
  470. struct urb *urb = hcd->status_urb;
  471. urb->actual_length = length;
  472. memcpy(urb->transfer_buffer, buffer, length);
  473. usb_hcd_unlink_urb_from_ep(hcd, urb);
  474. usb_hcd_giveback_urb(hcd, urb, 0)-》
  475. if (!hcd_giveback_urb_in_bh(hcd) && !is_root_hub(urb->dev))
  476. {
  477. __usb_hcd_giveback_urb(urb)->
  478. usbmon_urb_complete(&hcd->self, urb, status);
  479. usb_anchor_suspend_wakeups(anchor);
  480. usb_unanchor_urb(urb);
  481. urb->complete(urb);
  482. usb_anchor_resume_wakeups(anchor);
  483. return;
  484. }
  485. list_add_tail(&urb->urb_list, &bh->head);
  486. mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
  487. /*
  488. root@mxlos:~# cat /proc/interrupts
  489. CPU0 CPU1 CPU2 CPU3
  490. 39: 5 0 0 0 GIC 42 Level 10180000.usb, 10180000.usb, dwc2_hsotg:usb1
  491. 40: 492059 0 0 0 GIC 43 Level ehci_hcd:usb2
  492. 41: 0 0 0 0 GIC 64 Level ohci_hcd:usb3
  493. */
  494. hcd->irq = irqnum;
  495. dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum,
  496. (hcd->driver->flags & HCD_MEMORY) ?
  497. "io mem" : "io base",(unsigned long long)hcd->rsrc_start);
  498. //实际打印:ehci-platform 101c0000.usb: irq 40, io mem 0x101c0000
  499. }
  500. }
  501. hcd->state = HC_STATE_RUNNING;
  502. retval = hcd->driver->start(hcd) = struct hc_driver ehci_hc_driver.start = ehci_run
  503. static int ehci_run(struct usb_hcd *hcd)->
  504. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  505. hcd->uses_new_polling = 1;
  506. ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
  507. ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
  508. temp = HC_VERSION(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
  509. ehci_info (ehci, "USB %x.%x started, EHCI %x.%02x%s\n",
  510. ((ehci->sbrn & 0xf0)>>4), (ehci->sbrn & 0x0f),
  511. temp >> 8, temp & 0xff, ignore_oc ? ", overcurrent ignored" : "");
  512. //对于 rk3128 来说实际打印:ehci-platform 101c0000.usb: USB 2.0 started, EHCI 1.00
  513. create_debug_files(ehci);
  514. create_sysfs_files(ehci)->
  515. struct device *controller = ehci_to_hcd(ehci)->self.controller;
  516. //对于rk3128对应文件 /sys/devices/platform/101c0000.usb/uframe_periodic_max
  517. i = device_create_file(controller, &dev_attr_uframe_periodic_max);
  518. retval = register_root_hub(hcd)-> //注册根集线器 root hub
  519. struct usb_device *usb_dev = hcd->self.root_hub;
  520. struct usb_device *usb_dev->devnum = devnum;
  521. usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
  522. usb_dev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
  523. usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE)-> //获取根集线器描述符
  524. struct usb_device_descriptor *desc = kmalloc(sizeof(*desc), GFP_NOIO);
  525. usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size)->
  526. for (i = 0; i < 3; ++i) //尝试读取3次
  527. {
  528. /* retry on length 0 or error; some devices are flakey */
  529. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  530. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
  531. (type << 8) + index, 0, buf, size,
  532. USB_CTRL_GET_TIMEOUT);
  533. }
  534. memcpy(&dev->descriptor, desc, size);
  535. usb_new_device (usb_dev)->
  536. usb_enumerate_device(udev)-> /* Read descriptors */
  537. dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
  538. udev->devnum, udev->bus->busnum,
  539. (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
  540. /* export the usbdev device-node for libusb */
  541. //#define USB_DEVICE_MAJOR 189
  542. udev->dev.devt = MKDEV(USB_DEVICE_MAJOR, (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
  543. announce_device(udev)-> /* Tell the world! 告诉全世界我诞生了 */
  544. /*
  545. 对于 USB2.0 的 root hub 实际打印:1d6b 和 0002 代表 USB2.0 root hub
  546. usb usb2: New USB device found, idVendor=1d6b, idProduct=0002
  547. usb usb2: New USB device strings: Mfr=3, Product=2, SerialNumber=1
  548. usb usb2: Product: EHCI Host Controller
  549. usb usb2: Manufacturer: Linux 4.4.194 ehci_hcd
  550. usb usb2: SerialNumber: 101c0000.usb
  551. */
  552. dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
  553. le16_to_cpu(udev->descriptor.idVendor),
  554. le16_to_cpu(udev->descriptor.idProduct));
  555. dev_info(&udev->dev,
  556. "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
  557. udev->descriptor.iManufacturer,
  558. udev->descriptor.iProduct,
  559. udev->descriptor.iSerialNumber);
  560. show_string(udev, "Product", udev->product);
  561. show_string(udev, "Manufacturer", udev->manufacturer);
  562. show_string(udev, "SerialNumber", udev->serial);
  563. device_enable_async_suspend(&udev->dev);
  564. device_add(&udev->dev)->
  565. dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
  566. // 对于 USB2.0 的 root hub 实际打印:device: 'usb2': device_add
  567. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  568. error = bus_add_device(dev)->
  569. struct bus_type *bus = bus_get(dev->bus);
  570. //对于 USB2.0 的 root hub 实际打印: bus: 'usb': add device usb2
  571. pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev));
  572. error = sysfs_create_link(&dev->kobj, &dev->bus->p->subsys.kobj, "subsystem");
  573. klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices);
  574. blocking_notifier_call_chain(&dev->bus->p->bus_notifier, BUS_NOTIFY_ADD_DEVICE, dev);
  575. kobject_uevent(&dev->kobj, KOBJ_ADD);
  576. bus_probe_device(dev)->device_initial_probe(dev)->__device_attach(dev, true)->
  577. bus_for_each_drv(dev->bus, NULL, &data, _device_attach_driver)->
  578. while ((drv = next_driver(&i)) && !error)
  579. {
  580. error = fn(drv, data)=_device_attach_driver;
  581. static int __device_attach_driver(struct device_driver *drv, void *_data)->
  582. if (!driver_match_device(drv, dev))->
  583. return drv->bus->match ? drv->bus->match(dev, drv) : 1;
  584. struct bus_type usb_bus_type.match = usb_device_match,
  585. // usb_device_match 有匹配的驱动时返回值为 1
  586. static int usb_device_match(struct device *dev, struct device_driver *drv)
  587. {
  588. /* devices and interfaces are handled separately */
  589. if (is_usb_device(dev)) //return dev->type == &usb_device_type;
  590. {
  591. /* interface drivers never match devices */
  592. if (!is_usb_device_driver(drv))
  593. return 0;
  594. /* TODO: Add real matching code */
  595. return 1;
  596. }
  597. else if (is_usb_interface(dev))
  598. {
  599. struct usb_interface *intf;
  600. struct usb_driver *usb_drv;
  601. const struct usb_device_id *id;
  602. /* device drivers never match interfaces */
  603. if (is_usb_device_driver(drv))
  604. return 0;
  605. intf = to_usb_interface(dev);
  606. usb_drv = to_usb_driver(drv);
  607. id = usb_match_id(intf, usb_drv->id_table);
  608. if (id)
  609. return 1;
  610. id = usb_match_dynamic_id(intf, usb_drv);
  611. if (id)
  612. return 1;
  613. }
  614. return 0;
  615. }
  616. {
  617. return 0;
  618. }
  619. return driver_probe_device(drv, dev)->
  620. //对USB2.0 的 root hub 实际打印: bus: 'usb': driver_probe_device: matched device usb2 with driver usb
  621. pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
  622. drv->bus->name, __func__, dev_name(dev), drv->name);
  623. really_probe(dev, drv)->
  624. //USB2.0 的 root hub 打印:bus: 'usb': driver_probe_device: matched device usb2 with driver usb
  625. pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
  626. drv->bus->name, __func__, drv->name, dev_name(dev));
  627. dev->driver = drv;
  628. // 所有USB子设备struct usb_device和USB接口设备struct usb_interface
  629. //都挂在USB总线上struct struct bus_type usb_bus_type
  630. if (dev->bus->probe) //struct struct bus_type usb_bus_type.probe = NULL
  631. {
  632. /*
  633. struct bus_type usb_bus_type = {
  634. .name = "usb",
  635. .match = usb_device_match,
  636. .uevent = usb_uevent,
  637. .need_parent_lock = true,
  638. };
  639. */
  640. ret = dev->bus->probe(dev);
  641. }
  642. else if (drv->probe)
  643. {//对 usb hub 驱动 struct usb_driver hub_driver hub.drvwrap.driver.probe = usb_probe_interface
  644. drv->probe(dev)=usb_probe_interface;
  645. static int usb_probe_interface(struct device *dev)->
  646. struct usb_device_id *id = usb_match_dynamic_id(intf, driver);
  647. if (!id)
  648. {
  649. id = usb_match_id(intf, driver->id_table)->
  650. for (; id->idVendor || id->idProduct || id->bDeviceClass ||
  651. id->bInterfaceClass || id->driver_info; id++) {
  652. if (usb_match_one_id(interface, id))->
  653. intf = interface->cur_altsetting;
  654. dev = interface_to_usbdev(interface);
  655. if (!usb_match_device(dev, id))
  656. return 0;
  657. return usb_match_one_id_intf(dev, intf, id);
  658. {
  659. return id;
  660. }
  661. }
  662. }
  663. dev_dbg(dev, "%s - got id\n", __func__);
  664. //对于 usb hub 驱动 struct usb_driver hub_driver.probe = hub_probe
  665. driver->probe(intf, id);
  666. int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)->
  667. dev_info(&intf->dev, "USB hub found\n");/* We found a hub */
  668. struct usb_hub *hub = kzalloc(sizeof(*hub), GFP_KERNEL);
  669. hub->intfdev = &intf->dev;
  670. hub->hdev = hdev;
  671. INIT_DELAYED_WORK(&hub->leds, led_work);
  672. INIT_DELAYED_WORK(&hub->init_work, NULL);
  673. INIT_WORK(&hub->events, hub_event); //用于处理hub的事件
  674. usb_get_intf(intf);
  675. usb_get_dev(hdev);
  676. usb_set_intfdata(intf, hub);
  677. intf->needs_remote_wakeup = 1;
  678. hub_configure(hub, endpoint)->// //设置hub的端点0
  679. hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
  680. hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
  681. hub->descriptor = kzalloc(sizeof(*hub->descriptor), ..);
  682. //Request the entire hub descriptor.
  683. get_hub_descriptor(hdev, hub->descriptor);
  684. maxchild = hub->descriptor->bNbrPorts;
  685. // USB2.0 的 root hub 打印: hub 2-0:1.0: 1 port detected
  686. //对于 GL852G 打印: hub 2-1:1.0: 4 ports detected
  687. dev_info(hub_dev, "%d port%s detected\n",
  688. maxchild, (maxchild == 1) ? "" : "s");
  689. hub->ports = kzalloc(maxchild * sizeof(struct usb_port *), ..);
  690. wHubCharacteristics =
  691. le16_to_cpu(hub->descriptor->wHubCharacteristics);
  692. // USB2.0 的 root hub打印:hub 2-0:1.0: standalone hub
  693. dev_dbg(hub_dev, "standalone hub\n");
  694. //对于 GL852G 打印:hub 2-1:1.0: compound device;
  695. //port removable status: FFFF
  696. dev_dbg(hub_dev, "compound device;
  697. port removable status: %s\n", portstr);
  698. switch (wHubCharacteristics & HUB_CHAR_LPSM)
  699. {
  700. case HUB_CHAR_COMMON_LPSM:
  701. dev_dbg(hub_dev,"ganged power switching\n");
  702. break;
  703. case HUB_CHAR_INDV_PORT_LPSM:
  704. // USB2.0 的 root hub打印:
  705. //hub 2-0:1.0: individual port power switching
  706. dev_dbg(hub_dev, "individual port power switching\n");
  707. break;
  708. case HUB_CHAR_NO_LPSM:
  709. case HUB_CHAR_LPSM:
  710. dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
  711. break;
  712. }
  713. switch (wHubCharacteristics & HUB_CHAR_OCPM) {
  714. case HUB_CHAR_INDV_PORT_OCPM:
  715. //USB2.0 的 root hub 打印:
  716. //hub 2-0:1.0: individual port over-current protection
  717. dev_dbg(hub_dev,
  718. "individual port over-current protection\n");
  719. break;
  720. case HUB_CHAR_NO_OCPM:
  721. case HUB_CHAR_OCPM:
  722. dev_dbg(hub_dev, "no over-current protection\n");
  723. break;
  724. }
  725. INIT_LIST_HEAD(&hub->tt.clear_list);
  726. INIT_WORK(&hub->tt.clear_work, hub_tt_work);
  727. // USB2.0 的 root hub打印:
  728. //hub 2-0:1.0: power on to power good time: 20ms
  729. dev_dbg(hub_dev, "power on to power good time: %dms\n",
  730. hub->descriptor->bPwrOn2PwrGood * 2);
  731. usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
  732. hcd = bus_to_hcd(hdev->bus);
  733. hub_hub_status(hub, &hubstatus, &hubchange);
  734. //USB2.0 root hub :hub 2-0:1.0: local power source is good
  735. dev_dbg(hub_dev, "local power source is %s\n",
  736. (hubstatus & HUB_STATUS_LOCAL_POWER)
  737. ? "lost (inactive)" : "good");
  738. hub->urb = usb_alloc_urb(0, GFP_KERNEL);
  739. /*
  740. USB设备需要热插拔,因此在hub_probe函数中调用hub_configure函数来配置hub,在这个函数中主要是利用函数usb_alloc_urb函数来分配一个urb,利用usb_fill_int_urb来初始化这个urb结构,包括hub的中断服务程序hub_irq的,查询的周期等。
  741. 每当有设备连接到USB接口时,USB总线在查询hub状态信息的时候会触发hub的中断服务程序hub_irq, 在该函数中置位event_bits,运行工作队列。进入hub_event函数,该函数用来处理端口变化的事件。然后通过一个for循环来检测每个端口的状态信息。利用usb_port_status获取端口信息,如果发生变化就调用hub_port_connect_change函数来配置端口等。
  742. */
  743. usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp,
  744. hub_irq, hub, endpoint->bInterval);
  745. for (i = 0; i < maxchild; i++)
  746. {
  747. usb_hub_create_port_device(hub, i + 1)->
  748. port_dev=kzalloc(sizeof(*port_dev), GFP_KERNEL);
  749. port_dev->req= kzalloc(sizeof(*(port_dev->req)), .);
  750. hub->ports[port1 - 1] = port_dev;
  751. port_dev->portnum = port1;
  752. set_bit(port1, hub->power_bits);
  753. port_dev->dev.parent = hub->intfdev;
  754. port_dev->dev.groups = port_dev_group;
  755. port_dev->dev.type = &usb_port_device_type;
  756. port_dev->dev.driver = &usb_port_driver;
  757. // USB2.0 的 root hub 上的
  758. //第一个USB子设备名为 : usb2-port1
  759. dev_set_name(&port_dev->dev,
  760. "%s-port%d",
  761. dev_name(&hub->hdev->dev), port1);
  762. device_register(&port_dev->dev)->
  763. device_initialize(dev);
  764. return device_add(dev);
  765. }
  766. hub_activate(hub, HUB_INIT)->
  767. //上电后延时,使hub稳定
  768. delay = hub_power_on_good_delay(hub);
  769. hub_power_on(hub, false)->
  770. //对于 USB2.0 的 root hub 实际打印:
  771. //hub 2-0:1.0: enabling power on all ports
  772. dev_dbg(hub->intfdev,
  773. "enabling power on all ports\n");
  774. INIT_DELAYED_WORK(&hub->init_work,
  775. hub_init_func2);
  776. queue_delayed_work(system_power_efficient_wq,
  777. &hub->init_work,
  778. msecs_to_jiffies(delay));
  779. init3:
  780. //提交urb,等执行完成就会回调hub_irq
  781. usb_submit_urb(hub->urb, GFP_NOIO);
  782. /* Scan all ports that need attention */
  783. kick_hub_wq(hub)->
  784. intf = to_usb_interface(hub->intfdev);
  785. //把hub_event加入工作队列,开始运行
  786. queue_work(hub_wq, &hub->events);
  787. intf->condition = USB_INTERFACE_BOUND;
  788. }
  789. else if (drv->probe) //匹配USB子设备 struct usb_device
  790. {
  791. // struct usb_device_driver usb_generic_driver.probe = generic_probe,
  792. // usb_register_device_driver()->
  793. // new_udriver->drvwrap.driver.probe = usb_probe_device;
  794. ret = drv->probe(dev);
  795. static int generic_probe(struct usb_device *udev)->
  796. usb_choose_configuration(udev)->
  797. num_configs = udev->descriptor.bNumConfigurations;
  798. usb_get_max_power(udev, c);
  799. i = best->desc.bConfigurationValue;
  800. //对于 USB2.0 的 root hub 实际打印:
  801. // usb usb2: configuration #1 chosen from 1 choice
  802. dev_dbg(&udev->dev, "configuration #%d chosen from %d
  803. choice%s\n", i, num_configs,
  804. plural(num_configs));
  805. usb_set_configuration(udev, c)->
  806. int nintf = cp->desc.bNumInterfaces;
  807. struct usb_interface **new_interfaces =
  808. kmalloc(nintf * sizeof(*new_interfaces),
  809. i = dev->bus_mA - usb_get_max_power(dev, cp);
  810. usb_autoresume_device(dev);
  811. usb_hcd_alloc_bandwidth(dev, cp, NULL, NULL);
  812. struct usb_host_interface *alt = usb_altnum_to_altsetting(intf, 0);
  813. struct usb_interface *intf->cur_altsetting = alt;
  814. usb_enable_interface(dev, intf, true);
  815. intf->dev.parent = &dev->dev;
  816. intf->dev.driver = NULL;
  817. intf->dev.bus = &usb_bus_type;
  818. intf->dev.type = &usb_if_device_type; //USB 子设备
  819. intf->dev.groups = usb_interface_groups;
  820. intf->dev.dma_mask = dev->dev.dma_mask;
  821. INIT_WORK(&intf->reset_ws, __usb_queue_reset_device); /*
  822. __usb_queue_reset_device(struct work_struct *ws)-> usb_reset_device(udev)->
  823. usb_reset_and_verify_device(udev)->
  824. usb_ep0_reinit(udev);
  825. descriptors_changed()->
  826. usb_string(udev,..)->
  827. usb_get_langid(dev, tbuf)->
  828. //对GL852G hub 芯片实际打印:usb 2-1: default language 0x0409
  829. dev_dbg(&dev->dev, "default language 0x%04x\n",
  830. dev->string_langid); */
  831. intf->minor = -1;
  832. device_initialize(&intf->dev);
  833. pm_runtime_no_callbacks(&intf->dev);
  834. dev_set_name(&intf->dev, "%d-%s:%d.%d",
  835. dev->bus->busnum, dev->devpath,
  836. configuration, alt->desc.bInterfaceNumber);
  837. usb_get_dev(dev);
  838. usb_control_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION, 0, configuration,
  839. 0,
  840. NULL, 0, USB_CTRL_SET_TIMEOUT);
  841. usb_set_device_state(dev, USB_STATE_CONFIGURED);
  842. for (i = 0; i < nintf; ++i)
  843. {
  844. struct usb_interface *intf = cp->interface[i];
  845. /*
  846. 对于 USB2.0 的 root hub 实际打印: usb usb2: adding 2-0:1.0 (config #1, interface 0)
  847. root@mxlos:/sys/bus/usb/drivers/usb/usb2# cd 2-0\:1.0/
  848. root@mxlos:/sys/bus/usb/drivers/usb/usb2/2-0:1.0# ls
  849. authorized bInterfaceClass bInterfaceProtocol bNumEndpoints
  850. ep_81 power supports_autosuspend usb2-port1
  851. bAlternateSetting bInterfaceNumber bInterfaceSubClass driver
  852. modalias subsystem uevent
  853. root@mxlos:/sys/bus/usb/drivers/usb/usb2/2-0:1.0# pwd
  854. /sys/bus/usb/drivers/usb/usb2/2-0:1.0
  855. root@mxlos:/sys/bus/usb/drivers/usb/usb2/2-0:1.0# cat modalias
  856. usb:v1D6Bp0002d0404dc09dsc00dp00ic09isc00ip00in00
  857. root@mxlos:/sys/bus/usb/drivers/usb/usb2/2-0:1.0# cat bInterfaceNumber
  858. 00
  859. root@mxlos:/sys/bus/usb/drivers/usb/usb2/2-0:1.0# cat bAlternateSetting
  860. 0
  861. root@mxlos:/sys/bus/usb/drivers/usb/usb2/2-0:1.0# cat uevent
  862. DEVTYPE=usb_interface
  863. DRIVER=hub
  864. PRODUCT=1d6b/2/404
  865. TYPE=9/0/0
  866. INTERFACE=9/0/0 MODALIAS=usb:v1D6Bp0002d0404dc09dsc00dp00ic09isc00ip00in00
  867. root@mxlos:/sys/bus/usb/drivers/usb/usb2/2-0:1.0# cat bInterfaceClass
  868. 09
  869. */ dev_dbg(&dev->dev,
  870. "adding %s (config #%d, interface %d)\n",
  871. dev_name(&intf->dev), configuration,
  872. intf->cur_altsetting->desc.bInterfaceNumber);
  873. device_enable_async_suspend(&intf->dev);
  874. ret = device_add(&intf->dev)->
  875. dev_set_name(dev, "%s%u", dev->bus->dev_name,
  876. dev->id);
  877. //对于USB2.0 的 root hub 的 interface 0 实际打印:
  878. // device: '2-0:1.0': device_add
  879. pr_debug("device: '%s': %s\n",
  880. dev_name(dev), __func__);
  881. kobject_uevent(&dev->kobj, KOBJ_ADD);
  882. bus_probe_device(dev);
  883. create_intf_ep_devs(intf);
  884. }
  885. usb_notify_add_device(udev)->
  886. blocking_notifier_call_chain(&usb_notifier_list, USB_DEVICE_ADD, udev);
  887. }
  888. driver_bound(dev);
  889. //对于 USB2.0 的 root hub 实际打印: bus: 'usb': really_probe: bound device usb2 to driver usb
  890. pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
  891. drv->bus->name, __func__, dev_name(dev), drv->name);
  892. }
  893. /* Create link files between child device and usb port device. */
  894. if (udev->parent) {
  895. struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
  896. int port1 = udev->portnum;
  897. struct usb_port *port_dev = hub->ports[port1 - 1];
  898. err = sysfs_create_link(&udev->dev.kobj, &port_dev->dev.kobj, "port");
  899. err = sysfs_create_link(&port_dev->dev.kobj,&udev->dev.kobj, "device");
  900. }
  901. usb_create_ep_devs(&udev->dev, &udev->ep0, udev)->
  902. struct ep_device *ep_dev = kzalloc(sizeof(*ep_dev), GFP_KERNEL);
  903. ep_dev->desc = &endpoint->desc;
  904. ep_dev->udev = udev;
  905. ep_dev->dev.groups = ep_dev_groups;
  906. ep_dev->dev.type = &usb_ep_device_type;
  907. ep_dev->dev.parent = parent;
  908. //对于 USB2.0 的 root hub 对应 :/sys/bus/usb/drivers/usb/usb2/ep_00
  909. dev_set_name(&ep_dev->dev, "ep_%02x", endpoint->desc.bEndpointAddress);
  910. device_register(&ep_dev->dev);
  911. endpoint->ep_dev = ep_dev;
  912. hcd->rh_registered = 1;
  913. retval = sysfs_create_group(&rhdev->dev.kobj, &usb_bus_attr_group);
  914. usb_hcd_poll_rh_status(hcd);
  915. device_wakeup_enable(hcd->self.controller);
  916. platform_set_drvdata(dev, hcd);

 USB2.0 root hub根集线器

在注册 USB 主机控制器 host controller 的函数usb_add_hcd() 中会调用函数register_root_hub() 注册主机控制器下的 root hub,并最终调用 generic_probe() 遍历 root hub 下挂载的USB设备:

  1. usb_add_hcd(hcd, irq, IRQF_SHARED)->
  2. hcd->self.root_hub = rhdev; //设置主机控制器的根集线器
  3. struct usb_device *rhdev = usb_alloc_dev(NULL, &hcd->self, 0)->
  4. dev->dev.bus = &usb_bus_type;
  5. dev->dev.type = &usb_device_type;
  6. register_root_hub(hcd)->
  7. usb_new_device (usb_dev)->
  8. announce_device(udev);
  9. usb_enumerate_device(udev)
  10. device_add(&udev->dev)->
  11. bus_probe_device(dev)->device_initial_probe(dev)->__device_attach(dev, true)->
  12. bus_for_each_drv(dev->bus, NULL, &data, _device_attach_driver)->
  13. static int __device_attach_driver(struct device_driver *drv, void *_data)->
  14. static int usb_device_match(struct device *dev, struct device_driver *drv)
  15. /* devices and interfaces are handled separately */
  16. if (is_usb_device(dev)) //return dev->type == &usb_device_type;
  17. {
  18. /* interface drivers never match devices */
  19. /*
  20. 在函数usb_hub_init() 中注册 struct usb_driver hub_driver 时设置for_devices = 0:
  21. usb_register(&hub_driver)->
  22. usb_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)->
  23. new_driver->drvwrap.for_devices = 0;
  24. usb_init() 中注册 struct usb_device_driver usb_generic_driver 时设置for_devices = 1:
  25. usb_register_device_driver(&usb_generic_driver, THIS_MODULE)->
  26. new_udriver->drvwrap.for_devices = 1;
  27. */
  28. if (!is_usb_device_driver(drv))->
  29. return container_of(drv, struct usbdrv_wrap, driver)->for_devices;
  30. {
  31. return 0;
  32. }
  33. return 1;
  34. }
  35. return driver_probe_device(drv, dev)-> //匹配到 root hub 的驱动为 struct usb_device_driver usb_generic_driver
  36. really_probe(dev, drv)-> //struct usb_device_driver usb_generic_driver.probe = generic_probe
  37. generic_probe(struct usb_device *udev)->
  38. usb_choose_configuration(udev);
  39. usb_set_configuration(udev, c);

 usb interface class

在内核源文件 linux-4.4\drivers\usb\core\driver.c 的函数usb_match_one_id_intf() 根据 usb interface class 来匹配USB 设备的驱动:

  1. int usb_match_one_id_intf(struct usb_device *dev,
  2. struct usb_host_interface *intf,
  3. const struct usb_device_id *id)
  4. {
  5. /* The interface class, subclass, protocol and number should never be
  6. * checked for a match if the device class is Vendor Specific,
  7. * unless the match record specifies the Vendor ID. */
  8. if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
  9. !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  10. (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
  11. USB_DEVICE_ID_MATCH_INT_SUBCLASS |
  12. USB_DEVICE_ID_MATCH_INT_PROTOCOL |
  13. USB_DEVICE_ID_MATCH_INT_NUMBER)))
  14. return 0;
  15. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
  16. (id->bInterfaceClass != intf->desc.bInterfaceClass))
  17. return 0;
  18. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
  19. (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
  20. return 0;
  21. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
  22. (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
  23. return 0;
  24. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
  25. (id->bInterfaceNumber != intf->desc.bInterfaceNumber))
  26. return 0;
  27. return 1;
  28. }

在文件 linux-4.4\include\uapi\linux\usb\ch9.h 定义的 usb interface class codes :

/*

 * Device and/or Interface Class codes

 * as found in bDeviceClass or bInterfaceClass

 * and defined by www.usb.org documents

 */

#define USB_CLASS_PER_INTERFACE                                   0               /* for DeviceClass */

#define USB_CLASS_AUDIO                                                   1               #音频设备

#define USB_CLASS_COMM                                                  2               #调制解调器,网卡,ISDN连接

#define USB_CLASS_HID                                        3               #人机交互设备,,如鼠标,键盘

#define USB_CLASS_PHYSICAL                            5               # 物理设备

#define USB_CLASS_STILL_IMAGE                      6               #静止图像捕捉设备

#define USB_CLASS_PRINTER                              7               #打印机

#define USB_CLASS_MASS_STORAGE                               8               #大容量存储器

#define USB_CLASS_HUB                                      9               #集线器

#define USB_CLASS_CDC_DATA                          0x0a         #

#define USB_CLASS_CSCID                                                     0x0b        /* chip+ smart card :智能卡*/

#define USB_CLASS_CONTENT_SEC                                   0x0d        /* content security */

#define USB_CLASS_VIDEO                                                    0x0e        // 视频设备,如网络摄像头

#define USB_CLASS_WIRELESS_CONTROLLER                 0xe0

#define USB_CLASS_MISC                                     0xef

#define USB_CLASS_APP_SPEC                           0xfe

#define USB_CLASS_VENDOR_SPEC                                    0xff

#define USB_SUBCLASS_VENDOR_SPEC          0xff

在USB 官网Defined Class Codes | USB-IF 规定了 usb interface class :

复制网址 Defined Class Codes | USB-IF 的内容如下:

Base Class

Descriptor Usage

Description

00h

Device

Use class information in the Interface Descriptors

01h

Interface

Audio

02h

Both

Communications and CDC Control

03h

Interface

HID (Human Interface Device)

05h

Interface

Physical

06h

Interface

Image

07h

Interface

Printer

08h

Interface

Mass Storage

09h

Device

Hub

0Ah

Interface

CDC-Data

0Bh

Interface

Smart Card

0Dh

Interface

Content Security

0Eh

Interface

Video

0Fh

Interface

Personal Healthcare

10h

Interface

Audio/Video Devices

11h

Device

Billboard Device Class

12h

Interface

USB Type-C Bridge Class

13h

Interface

USB Bulk Display Protocol Device Class

14h

Interface

MCTP over USB Protocol Endpoint Device Class

3Ch

Interface

I3C Device Class

DCh

Both

Diagnostic Device

E0h

Interface

Wireless Controller

EFh

Both

Miscellaneous

FEh

Interface

Application Specific

FFh

Both

Vendor Specific

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/614045
推荐阅读
相关标签
  

闽ICP备14008679号