当前位置:   article > 正文

usb主机控制程序_usb控制软件代码

usb控制软件代码
  1. 2524
  2. 2525 static struct platform_driver musb_driver = {
  3. 2526 .driver = {
  4. 2527 .name = (char *)musb_driver_name,
  5. 2528 .bus = &platform_bus_type,
  6. 2529 .owner = THIS_MODULE,
  7. 2530 .pm = MUSB_DEV_PM_OPS,
  8. 2531 },
  9. 2532 .probe = musb_probe,
  10. 2533 .remove = __exit_p(musb_remove),
  11. 2534 .shutdown = musb_shutdown,
  12. 2535 };
  13. 2536
  14. 2537 /*-------------------------------------------------------------------------*/
  15. 2538
  16. 2539 static int __init musb_init(void)
  17. 2540 {
  18. 2541 if (usb_disabled())
  19. 2542 return 0;
  20. 2543
  21. 2544 pr_info("%s: version " MUSB_VERSION ", "
  22. 2545 "?dma?"
  23. 2546 ", "
  24. 2547 "otg (peripheral+host)",
  25. 2548 musb_driver_name);
  26. 2549 return platform_driver_register(&musb_driver);
  27. 2550 }
  28. 2551
  29. 2552 /* make us init after usbcore and i2c (transceivers, regulators, etc)
  30. 2553 * and before usb gadget and host-side drivers start to register
  31. 2554 */
  32. 2555 fs_initcall(musb_init);
  33. 2556
  34. 2557 static void __exit musb_cleanup(void)
  35. 2558 {
  36. 2559 platform_driver_unregister(&musb_driver);
  37. 2560 }
  38. 2561 module_exit(musb_cleanup);

根据匹配运行probe

  1. 2218
  2. 2219 /* all implementations (PCI bridge to FPGA, VLYNQ, etc) should just
  3. 2220 * bridge to a platform device; this driver then suffices.
  4. 2221 */
  5. 2222 static int __devinit musb_probe(struct platform_device *pdev)
  6. 2223 {
  7. 2224 struct device *dev = &pdev->dev;
  8. 2225 int irq = platform_get_irq_byname(pdev, "mc");
  9. 2226 int status;
  10. 2227 struct resource *iomem;
  11. 2228 void __iomem *base;
  12. 2229 char res_name[20];
  13. 2230
  14. 2231 if (pdev->id == -1)
  15. 2232 strcpy(res_name, "mc");
  16. 2233 else
  17. 2234 sprintf(res_name, "musb%d-irq", pdev->id);
  18. 2235 irq = platform_get_irq_byname(pdev, res_name);
  19. 2236
  20. 2237 if (pdev->id == -1)
  21. 2238 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  22. 2239 else {
  23. 2240 sprintf(res_name, "musb%d", pdev->id);
  24. 2241 iomem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  25. 2242 res_name);
  26. 2243 }
  27. 2244
  28. 2245 if (!iomem || irq <= 0)
  29. 2246 return -ENODEV;
  30. 2247
  31. 2248 base = ioremap(iomem->start, resource_size(iomem));
  32. 2249 if (!base) {
  33. 2250 dev_err(dev, "ioremap failed\n");
  34. 2251 return -ENOMEM;
  35. 2252 }
  36. 2253
  37. 2254 status = musb_init_controller(dev, irq, base);
  38. 2255 if (status < 0)
  39. 2256 iounmap(base);
  40. 2257
  41. 2258 return status;
  42. 2259 }
  43. 2260

看到前面是获取中断号musb0-irq、获取内存资源musb0

2254行:musb_init_controller

  1. 1953 /*
  2. 1954 * Perform generic per-controller initialization.
  3. 1955 *
  4. 1956 * @pDevice: the controller (already clocked, etc)
  5. 1957 * @nIrq: irq
  6. 1958 * @mregs: virtual address of controller registers,
  7. 1959 * not yet corrected for platform-specific offsets
  8. 1960 */
  9. 1961 static int __devinit
  10. 1962 musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
  11. 1963 {
  12. 1964 int status;
  13. 1965 struct musb *musb;
  14. 1966 struct musb_hdrc_platform_data *plat = dev->platform_data;
  15. 1967 struct platform_device *pdev = to_platform_device(dev);
  16. 1968
  17. 1969 /* The driver might handle more features than the board; OK.
  18. 1970 * Fail when the board needs a feature that's not enabled.
  19. 1971 */
  20. 1972 if (!plat) {
  21. 1973 dev_dbg(dev, "no platform_data?\n");
  22. 1974 status = -ENODEV;
  23. 1975 goto fail0;
  24. 1976 }
  25. 1977
  26. 1978 /* allocate */
  27. 1979 musb = allocate_instance(dev, plat->config, ctrl);
  28. 1980 if (!musb) {
  29. 1981 status = -ENOMEM;
  30. 1982 goto fail0;
  31. 1983 }
  32. 1984
  33. 1985 pm_runtime_use_autosuspend(musb->controller);
  34. 1986 pm_runtime_set_autosuspend_delay(musb->controller, 200);
  35. 1987 pm_runtime_enable(musb->controller);
  36. 1988
  37. 1989 spin_lock_init(&musb->lock);
  38. 1990 musb->board_mode = plat->mode;
  39. 1991 musb->board_set_power = plat->set_power;
  40. 1992 musb->min_power = plat->min_power;
  41. 1993 musb->ops = plat->platform_ops;
  42. 1994 musb->id = pdev->id;
  43. 1995 musb->first = 1;
  44. 1996 if (is_host_enabled(musb))
  45. 1997 spin_lock_init(&musb->gb_lock);
  46. 1998
  47. 1999 musb->fifo_mode = musb->ops->fifo_mode;
  48. 2000
  49. 2001 #ifndef CONFIG_MUSB_PIO_ONLY
  50. 2002 musb->orig_dma_mask = dev->dma_mask;
  51. 2003 #endif
  52. 2004 if (musb->ops->flags & MUSB_GLUE_TUSB_STYLE) {
  53. 2005 musb_readb = __tusb_musb_readb;
  54. 2006 musb_writeb = __tusb_musb_writeb;
  55. 2007 } else {
  56. 2008 musb_readb = __musb_readb;
  57. 2009 musb_writeb = __musb_writeb;
  58. 2010 }
  59. 2011
  60. 2012 dev_info(dev, "dma type: %s\n", get_dma_name(musb));
  61. 2013
  62. 2014 /* The musb_platform_init() call:
  63. 2015 * - adjusts musb->mregs and musb->isr if needed,
  64. 2016 * - may initialize an integrated tranceiver
  65. 2017 * - initializes musb->xceiv, usually by otg_get_transceiver()
  66. 2018 * - stops powering VBUS
  67. 2019 *
  68. 2020 * There are various transceiver configurations. Blackfin,
  69. 2021 * DaVinci, TUSB60x0, and others integrate them. OMAP3 uses
  70. 2022 * external/discrete ones in various flavors (twl4030 family,
  71. 2023 * isp1504, non-OTG, etc) mostly hooking up through ULPI.
  72. 2024 */
  73. 2025 musb->isr = generic_interrupt;
  74. 2026 status = musb_platform_init(musb);
  75. 2027 if (status < 0)
  76. 2028 goto fail1;
  77. 2029
  78. 2030 if (!musb->isr) {
  79. 2031 status = -ENODEV;
  80. 2032 goto fail3;
  81. 2033 }
  82. 2034
  83. 2035 if (!musb->xceiv->io_ops) {
  84. 2036 musb->xceiv->io_priv = musb->mregs;
  85. 2037 musb->xceiv->io_ops = &musb_ulpi_access;
  86. 2038 }
  87. 2039
  88. 2040 #ifndef CONFIG_MUSB_PIO_ONLY
  89. 2041 if (dev->dma_mask) {
  90. 2042 struct dma_controller *c;
  91. 2043
  92. 2044 if (!musb->ops->dma_controller_create) {
  93. 2045 dev_err(dev, "no dma_controller_create for non-PIO mode!\n");
  94. 2046 status = -ENODEV;
  95. 2047 goto fail3;
  96. 2048 }
  97. 2049 c = musb->ops->dma_controller_create(musb, musb->mregs);
  98. 2050 musb->dma_controller = c;
  99. 2051 if (c)
  100. 2052 (void) c->start(c);
  101. 2053 }
  102. 2054 #endif
  103. 2055 /* ideally this would be abstracted in platform setup */
  104. 2056 if (!is_dma_capable() || !musb->dma_controller)
  105. 2057 dev->dma_mask = NULL;
  106. 2058
  107. 2059 /* be sure interrupts are disabled before connecting ISR */
  108. 2060 musb_platform_disable(musb);
  109. 2061 musb_generic_disable(musb);
  110. 2062
  111. 2063 /* setup musb parts of the core (especially endpoints) */
  112. 2064 status = musb_core_init(plat->config->multipoint
  113. 2065 ? MUSB_CONTROLLER_MHDRC
  114. 2066 : MUSB_CONTROLLER_HDRC, musb);
  115. 2067 if (status < 0)
  116. 2068 goto fail3;
  117. 2069
  118. 2070 /* Init IRQ workqueue before request_irq */
  119. 2071 INIT_WORK(&musb->irq_work, musb_irq_work);
  120. 2072
  121. 2073 /* attach to the IRQ */
  122. 2074 if (request_irq(nIrq, musb->isr, 0, dev_name(dev), musb)) {
  123. 2075 dev_err(dev, "request_irq %d failed!\n", nIrq);
  124. 2076 status = -ENODEV;
  125. 2077 goto fail3;
  126. 2078 }
  127. 2079 musb->nIrq = nIrq;
  128. 2080 /* FIXME this handles wakeup irqs wrong */
  129. 2081 if (enable_irq_wake(nIrq) == 0)
  130. 2082 musb->irq_wake = 1;
  131. 2083 else
  132. 2084 musb->irq_wake = 0;
  133. 2085
  134. 2086 device_init_wakeup(dev, 1);
  135. 2087
  136. 2088 /* host side needs more setup */
  137. 2089 if (is_host_enabled(musb)) {
  138. 2090 struct usb_hcd *hcd = musb_to_hcd(musb);
  139. 2091
  140. 2092 otg_set_host(musb->xceiv, &hcd->self);
  141. 2093
  142. 2094 if (is_otg_enabled(musb))
  143. 2095 hcd->self.otg_port = 1;
  144. 2096 musb->xceiv->host = &hcd->self;
  145. 2097 hcd->power_budget = 2 * (plat->power ? : 250);
  146. 2098
  147. 2099 /* program PHY to use external vBus if required */
  148. 2100 if (plat->extvbus) {
  149. 2101 u8 busctl = musb_read_ulpi_buscontrol(musb->mregs);
  150. 2102 busctl |= MUSB_ULPI_USE_EXTVBUS;
  151. 2103 musb_write_ulpi_buscontrol(musb->mregs, busctl);
  152. 2104 }
  153. 2105 }
  154. 2106
  155. 2107 /* For the host-only role, we can activate right away.
  156. 2108 * (We expect the ID pin to be forcibly grounded!!)
  157. 2109 * Otherwise, wait till the gadget driver hooks up.
  158. 2110 */
  159. 2111 if (!is_otg_enabled(musb) && is_host_enabled(musb)) {
  160. 2112 struct usb_hcd *hcd = musb_to_hcd(musb);
  161. 2113
  162. 2114 MUSB_HST_MODE(musb);
  163. 2115 musb->xceiv->default_a = 1;
  164. 2116 musb->xceiv->state = OTG_STATE_A_IDLE;
  165. 2117
  166. 2118 status = usb_add_hcd(musb_to_hcd(musb), -1, 0);
  167. 2119 device_set_wakeup_enable(dev, 0);
  168. 2120 hcd->self.uses_pio_for_control = 1;
  169. 2121 dev_dbg(musb->controller, "%s mode, status %d, devctl %02x %c\n",
  170. 2122 "HOST", status,
  171. 2123 musb_readb(musb->mregs, MUSB_DEVCTL),
  172. 2124 (musb_readb(musb->mregs, MUSB_DEVCTL)
  173. 2125 & MUSB_DEVCTL_BDEVICE
  174. 2126 ? 'B' : 'A'));
  175. 2127
  176. 2128 } else /* peripheral is enabled */ {
  177. 2129 MUSB_DEV_MODE(musb);
  178. 2130 musb->xceiv->default_a = 0;
  179. 2131 musb->xceiv->state = OTG_STATE_B_IDLE;
  180. 2132
  181. 2133 status = musb_gadget_setup(musb);
  182. 2134 device_set_wakeup_enable(dev, 0);
  183. 2135
  184. 2136 dev_dbg(musb->controller, "%s mode, status %d, dev%02x\n",
  185. 2137 is_otg_enabled(musb) ? "OTG" : "PERIPHERAL",
  186. 2138 status,
  187. 2139 musb_readb(musb->mregs, MUSB_DEVCTL));
  188. 2140
  189. 2141 }
  190. 2142 if (status < 0)
  191. 2143 goto fail3;
  192. 2144
  193. 2145 status = musb_init_debugfs(musb);
  194. 2146 if (status < 0)
  195. 2147 goto fail4;
  196. 2148
  197. 2149 #ifdef CONFIG_SYSFS
  198. 2150 status = sysfs_create_group(&musb->controller->kobj, &musb_attr_group);
  199. 2151 if (status)
  200. 2152 goto fail5;
  201. 2153 #endif
  202. 2154
  203. 2155 dev_info(dev, "USB %s mode controller at %p using %s, IRQ %d\n",
  204. 2156 ({char *s;
  205. 2157 switch (musb->board_mode) {
  206. 2158 case MUSB_HOST: s = "Host"; break;
  207. 2159 case MUSB_PERIPHERAL: s = "Peripheral"; break;
  208. 2160 default: s = "OTG"; break;
  209. 2161 }; s; }),
  210. 2162 ctrl,
  211. 2163 (is_dma_capable() && musb->dma_controller)
  212. 2164 ? "DMA" : "PIO",
  213. 2165 musb->nIrq);
  214. 2166
  215. 2167 if (status == 0) {
  216. 2168 u8 drvbuf[19];
  217. 2169 sprintf(drvbuf, "driver/musb_hdrc.%d", musb->id);
  218. 2170 musb_debug_create(drvbuf, musb);
  219. 2171 }
  220. 2172
  221. 2173 if (is_host_enabled(musb)) {
  222. 2174 musb->gb_queue = create_singlethread_workqueue(dev_name(dev));
  223. 2175 if (musb->gb_queue == NULL)
  224. 2176 goto fail6;
  225. 2177 /* Init giveback workqueue */
  226. 2178 INIT_WORK(&musb->gb_work, musb_gb_work);
  227. 2179 }
  228. 2180
  229. 2181 /* setup otg_timer */
  230. 2182 if (is_otg_enabled(musb))
  231. 2183 setup_timer(&musb->otg_timer, musb_otg_timer_func,
  232. 2184 (unsigned long) musb);
  233. 2185 return 0;
  234. 2186
  235. 2187 fail6:
  236. 2188 if (is_host_enabled(musb))
  237. 2189 destroy_workqueue(musb->gb_queue);
  238. 2190
  239. 2191 fail5:
  240. 2192 musb_exit_debugfs(musb);
  241. 2193
  242. 2194 fail4:
  243. 2195 if (!is_otg_enabled(musb) && is_host_enabled(musb))
  244. 2196 usb_remove_hcd(musb_to_hcd(musb));
  245. 2197 else
  246. 2198 musb_gadget_cleanup(musb);
  247. 2199
  248. 2200 fail3:
  249. 2201 if (musb->irq_wake)
  250. 2202 device_init_wakeup(dev, 0);
  251. 2203 musb_platform_exit(musb);
  252. 2204
  253. 2205 fail1:
  254. 2206 dev_err(musb->controller,
  255. 2207 "musb_init_controller failed with status %d\n", status);
  256. 2208
  257. 2209 musb_free(musb);
  258. 2210
  259. 2211 fail0:
  260. 2212
  261. 2213 return status;
  262. 2214
  263. 2215 }

1797行:allocate_instance

  1. 1864
  2. 1865 static struct musb *__devinit
  3. 1866 allocate_instance(struct device *dev,
  4. 1867 struct musb_hdrc_config *config, void __iomem *mbase)
  5. 1868 {
  6. 1869 struct musb *musb;
  7. 1870 struct musb_hw_ep *ep;
  8. 1871 int epnum;
  9. 1872 struct usb_hcd *hcd;
  10. 1873 struct musb_hdrc_platform_data *plat = dev->platform_data;
  11. 1874
  12. 1875 if (plat->mode != MUSB_PERIPHERAL) {
  13. 1876 hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
  14. 1877 if (!hcd)
  15. 1878 return NULL;
  16. 1879 /* usbcore sets dev->driver_data to hcd, and sometimes uses
  17. 1880 * that...
  18. 1881 */
  19. 1882
  20. 1883 musb = hcd_to_musb(hcd);
  21. 1884 INIT_LIST_HEAD(&musb->control);
  22. 1885 INIT_LIST_HEAD(&musb->in_bulk);
  23. 1886 INIT_LIST_HEAD(&musb->out_bulk);
  24. 1887 INIT_LIST_HEAD(&musb->gb_list);
  25. 1888
  26. 1889 hcd->uses_new_polling = 1;
  27. 1890 hcd->has_tt = 1;
  28. 1891 musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
  29. 1892 } else {
  30. 1893 musb = kzalloc(sizeof *musb, GFP_KERNEL);
  31. 1894 if (!musb)
  32. 1895 return NULL;
  33. 1896 }
  34. 1897 dev_set_drvdata(dev, musb);
  35. 1898 musb->mregs = mbase;
  36. 1899 musb->ctrl_base = mbase;
  37. 1900 musb->nIrq = -ENODEV;
  38. 1901 musb->config = config;
  39. 1902 BUG_ON(musb->config->num_eps > MUSB_C_NUM_EPS);
  40. 1903 for (epnum = 0, ep = musb->endpoints;
  41. 1904 epnum < musb->config->num_eps;
  42. 1905 epnum++, ep++) {
  43. 1906 ep->musb = musb;
  44. 1907 ep->epnum = epnum;
  45. 1908 }
  46. 1909
  47. 1910 musb->controller = dev;
  48. 1911
  49. 1912 return musb;
  50. 1913 }
  51. 1914

咱们前面plat->mode = (MUSB_HOST << 4) | MUSB_OTG,所以这个条件语句成功。

1876行:usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));这 回 才 是正 儿 八 经 的进 入 到 usb hcd 的 概念.这 个 函 数来 自drivers/usb/core/hcd.c:

  1. 2280 /**
  2. 2281 * usb_create_hcd - create and initialize an HCD structure
  3. 2282 * @driver: HC driver that will use this hcd
  4. 2283 * @dev: device for this HC, stored in hcd->self.controller
  5. 2284 * @bus_name: value to store in hcd->self.bus_name
  6. 2285 * Context: !in_interrupt()
  7. 2286 *
  8. 2287 * Allocate a struct usb_hcd, with extra space at the end for the
  9. 2288 * HC driver's private data. Initialize the generic members of the
  10. 2289 * hcd structure.
  11. 2290 *
  12. 2291 * If memory is unavailable, returns NULL.
  13. 2292 */
  14. 2293 struct usb_hcd *usb_create_hcd(const struct hc_driver *driver,
  15. 2294 struct device *dev, const char *bus_name)
  16. 2295 {
  17. 2296 return usb_create_shared_hcd(driver, dev, bus_name, NULL);
  18. 2297 }
  19. 2298 EXPORT_SYMBOL_GPL(usb_create_hcd);

2296行:

  1. 2213
  2. 2214 /**
  3. 2215 * usb_create_shared_hcd - create and initialize an HCD structure
  4. 2216 * @driver: HC driver that will use this hcd
  5. 2217 * @dev: device for this HC, stored in hcd->self.controller
  6. 2218 * @bus_name: value to store in hcd->self.bus_name
  7. 2219 * @primary_hcd: a pointer to the usb_hcd structure that is sharing the
  8. 2220 * PCI device. Only allocate certain resources for the primary HCD
  9. 2221 * Context: !in_interrupt()
  10. 2222 *
  11. 2223 * Allocate a struct usb_hcd, with extra space at the end for the
  12. 2224 * HC driver's private data. Initialize the generic members of the
  13. 2225 * hcd structure.
  14. 2226 *
  15. 2227 * If memory is unavailable, returns NULL.
  16. 2228 */
  17. 2229 struct usb_hcd *usb_create_shared_hcd(const struct hc_driver *driver,
  18. 2230 struct device *dev, const char *bus_name,
  19. 2231 struct usb_hcd *primary_hcd)
  20. 2232 {
  21. 2233 struct usb_hcd *hcd;
  22. 2234
  23. 2235 hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
  24. 2236 if (!hcd) {
  25. 2237 dev_dbg (dev, "hcd alloc failed\n");
  26. 2238 return NULL;
  27. 2239 }
  28. 2240 if (primary_hcd == NULL) {
  29. 2241 hcd->bandwidth_mutex = kmalloc(sizeof(*hcd->bandwidth_mutex),
  30. 2242 GFP_KERNEL);
  31. 2243 if (!hcd->bandwidth_mutex) {
  32. 2244 kfree(hcd);
  33. 2245 dev_dbg(dev, "hcd bandwidth mutex alloc failed\n");
  34. 2246 return NULL;
  35. 2247 }
  36. 2248 mutex_init(hcd->bandwidth_mutex);
  37. 2249 dev_set_drvdata(dev, hcd);
  38. 2250 } else {
  39. 2251 hcd->bandwidth_mutex = primary_hcd->bandwidth_mutex;
  40. 2252 hcd->primary_hcd = primary_hcd;
  41. 2253 primary_hcd->primary_hcd = primary_hcd;
  42. 2254 hcd->shared_hcd = primary_hcd;
  43. 2255 primary_hcd->shared_hcd = hcd;
  44. 2256 }
  45. 2257
  46. 2258 kref_init(&hcd->kref);
  47. 2259
  48. 2260 usb_bus_init(&hcd->self);
  49. 2261 hcd->self.controller = dev;
  50. 2262 hcd->self.bus_name = bus_name;
  51. 2263 hcd->self.uses_dma = (dev->dma_mask != NULL);
  52. 2264
  53. 2265 init_timer(&hcd->rh_timer);
  54. 2266 hcd->rh_timer.function = rh_timer_func;
  55. 2267 hcd->rh_timer.data = (unsigned long) hcd;
  56. 2268 #ifdef CONFIG_USB_SUSPEND
  57. 2269 INIT_WORK(&hcd->wakeup_work, hcd_resume_work);
  58. 2270 #endif
  59. 2271
  60. 2272 hcd->driver = driver;
  61. 2273 hcd->speed = driver->flags & HCD_MASK;
  62. 2274 hcd->product_desc = (driver->product_desc) ? driver->product_desc :
  63. 2275 "USB Host Controller";
  64. 2276 return hcd;
  65. 2277 }
  66. 2278 EXPORT_SYMBOL_GPL(usb_create_shared_hcd);

第一个参数 struct hc_driver,这个结构体掀开了我们对 usb host controller driver 的认识,它来自 drivers/usb/core/hcd.h:

  1. 202
  2. 203 struct hc_driver {
  3. 204 const char *description; /* "ehci-hcd" etc */
  4. 205 const char *product_desc; /* product/vendor string */
  5. 206 size_t hcd_priv_size; /* size of private data */
  6. 207
  7. 208 /* irq handler */
  8. 209 irqreturn_t (*irq) (struct usb_hcd *hcd);
  9. 210
  10. 211 int flags;
  11. 212 #define HCD_MEMORY 0x0001 /* HC regs use memory (else I/O) */
  12. 213 #define HCD_LOCAL_MEM 0x0002 /* HC needs local memory */
  13. 214 #define HCD_SHARED 0x0004 /* Two (or more) usb_hcds share HW */
  14. 215 #define HCD_USB11 0x0010 /* USB 1.1 */
  15. 216 #define HCD_USB2 0x0020 /* USB 2.0 */
  16. 217 #define HCD_USB3 0x0040 /* USB 3.0 */
  17. 218 #define HCD_MASK 0x0070
  18. 219
  19. 220 /* called to init HCD and root hub */
  20. 221 int (*reset) (struct usb_hcd *hcd);
  21. 222 int (*start) (struct usb_hcd *hcd);
  22. 223
  23. 224 /* NOTE: these suspend/resume calls relate to the HC as
  24. 225 * a whole, not just the root hub; they're for PCI bus glue.
  25. 226 */
  26. 227 /* called after suspending the hub, before entering D3 etc */
  27. 228 int (*pci_suspend)(struct usb_hcd *hcd, bool do_wakeup);
  28. 229
  29. 230 /* called after entering D0 (etc), before resuming the hub */
  30. 231 int (*pci_resume)(struct usb_hcd *hcd, bool hibernated);
  31. 232
  32. 233 /* cleanly make HCD stop writing memory and doing I/O */
  33. 234 void (*stop) (struct usb_hcd *hcd);
  34. 235
  35. 236 /* shutdown HCD */
  36. 237 void (*shutdown) (struct usb_hcd *hcd);
  37. 238
  38. 239 /* return current frame number */
  39. 240 int (*get_frame_number) (struct usb_hcd *hcd);
  40. 241
  41. 242 /* manage i/o requests, device state */
  42. 243 int (*urb_enqueue)(struct usb_hcd *hcd,
  43. 244 struct urb *urb, gfp_t mem_flags);
  44. 245 int (*urb_dequeue)(struct usb_hcd *hcd,
  45. 246 struct urb *urb, int status);
  46. 247
  47. 248 /*
  48. 249 * (optional) these hooks allow an HCD to override the default DMA
  49. 250 * mapping and unmapping routines. In general, they shouldn't be
  50. 251 * necessary unless the host controller has special DMA requirements,
  51. 252 * such as alignment contraints. If these are not specified, the
  52. 253 * general usb_hcd_(un)?map_urb_for_dma functions will be used instead
  53. 254 * (and it may be a good idea to call these functions in your HCD
  54. 255 * implementation)
  55. 256 */
  56. 257 int (*map_urb_for_dma)(struct usb_hcd *hcd, struct urb *urb,
  57. 258 gfp_t mem_flags);
  58. 259 void (*unmap_urb_for_dma)(struct usb_hcd *hcd, struct urb *urb);
  59. 260
  60. 261 /* hw synch, freeing endpoint resources that urb_dequeue can't */
  61. 262 void (*endpoint_disable)(struct usb_hcd *hcd,
  62. 263 struct usb_host_endpoint *ep);
  63. 264
  64. 265 /* (optional) reset any endpoint state such as sequence number
  65. 266 and current window */
  66. 267 void (*endpoint_reset)(struct usb_hcd *hcd,
  67. 268 struct usb_host_endpoint *ep);
  68. 269
  69. 270 /* root hub support */
  70. 271 int (*hub_status_data) (struct usb_hcd *hcd, char *buf);
  71. 272 int (*hub_control) (struct usb_hcd *hcd,
  72. 273 u16 typeReq, u16 wValue, u16 wIndex,
  73. 274 char *buf, u16 wLength);
  74. 275 int (*bus_suspend)(struct usb_hcd *);
  75. 276 int (*bus_resume)(struct usb_hcd *);
  76. 277 int (*start_port_reset)(struct usb_hcd *, unsigned port_num);
  77. 278
  78. 279 /* force handover of high-speed port to full-speed companion */
  79. 280 void (*relinquish_port)(struct usb_hcd *, int);
  80. 281 /* has a port been handed over to a companion? */
  81. 282 int (*port_handed_over)(struct usb_hcd *, int);
  82. 283
  83. 284 /* CLEAR_TT_BUFFER completion callback */
  84. 285 void (*clear_tt_buffer_complete)(struct usb_hcd *,
  85. 286 struct usb_host_endpoint *);
  86. 287
  87. 288 /* xHCI specific functions */
  88. 289 /* Called by usb_alloc_dev to alloc HC device structures */
  89. 290 int (*alloc_dev)(struct usb_hcd *, struct usb_device *);
  90. 291 /* Called by usb_disconnect to free HC device structures */
  91. 292 void (*free_dev)(struct usb_hcd *, struct usb_device *);
  92. 293 /* Change a group of bulk endpoints to support multiple stream IDs */
  93. 294 int (*alloc_streams)(struct usb_hcd *hcd, struct usb_device *udev,
  94. 295 struct usb_host_endpoint **eps, unsigned int num_eps,
  95. 296 unsigned int num_streams, gfp_t mem_flags);
  96. 297 /* Reverts a group of bulk endpoints back to not using stream IDs.
  97. 298 * Can fail if we run out of memory.
  98. 299 */
  99. 300 int (*free_streams)(struct usb_hcd *hcd, struct usb_device *udev,
  100. 301 struct usb_host_endpoint **eps, unsigned int num_eps,
  101. 302 gfp_t mem_flags);
  102. 303
  103. 304 /* Bandwidth computation functions */
  104. 305 /* Note that add_endpoint() can only be called once per endpoint before
  105. 306 * check_bandwidth() or reset_bandwidth() must be called.
  106. 307 * drop_endpoint() can only be called once per endpoint also.
  107. 308 * A call to xhci_drop_endpoint() followed by a call to
  108. 309 * xhci_add_endpoint() will add the endpoint to the schedule with
  109. 310 * possibly new parameters denoted by a different endpoint descriptor
  110. 311 * in usb_host_endpoint. A call to xhci_add_endpoint() followed by a
  111. 312 * call to xhci_drop_endpoint() is not allowed.
  112. 313 */
  113. 314 /* Allocate endpoint resources and add them to a new schedule */
  114. 315 int (*add_endpoint)(struct usb_hcd *, struct usb_device *,
  115. 316 struct usb_host_endpoint *);
  116. 317 /* Drop an endpoint from a new schedule */
  117. 318 int (*drop_endpoint)(struct usb_hcd *, struct usb_device *,
  118. 319 struct usb_host_endpoint *);
  119. 320 /* Check that a new hardware configuration, set using
  120. 321 * endpoint_enable and endpoint_disable, does not exceed bus
  121. 322 * bandwidth. This must be called before any set configuration
  122. 323 * or set interface requests are sent to the device.
  123. 324 */
  124. 325 int (*check_bandwidth)(struct usb_hcd *, struct usb_device *);
  125. 326 /* Reset the device schedule to the last known good schedule,
  126. 327 * which was set from a previous successful call to
  127. 328 * check_bandwidth(). This reverts any add_endpoint() and
  128. 329 * drop_endpoint() calls since that last successful call.
  129. 330 * Used for when a check_bandwidth() call fails due to resource
  130. 331 * or bandwidth constraints.
  131. 332 */
  132. 333 void (*reset_bandwidth)(struct usb_hcd *, struct usb_device *);
  133. 334 /* Returns the hardware-chosen device address */
  134. 335 int (*address_device)(struct usb_hcd *, struct usb_device *udev);
  135. 336 /* Notifies the HCD after a hub descriptor is fetched.
  136. 337 * Will block.
  137. 338 */
  138. 339 int (*update_hub_device)(struct usb_hcd *, struct usb_device *hdev,
  139. 340 struct usb_tt *tt, gfp_t mem_flags);
  140. 341 int (*reset_device)(struct usb_hcd *, struct usb_device *);
  141. 342 /* Notifies the HCD after a device is connected and its
  142. 343 * address is set
  143. 344 */
  144. 345 int (*update_device)(struct usb_hcd *, struct usb_device *);
  145. 346 int (*set_usb2_hw_lpm)(struct usb_hcd *, struct usb_device *, int);
  146. 347 };

每个 hcd 都得对应这么一个结构体变量.比如咱们的MUSB HDRC,drivers\usb\musb\musb_host.c中就有这么一段:

  1. 2496 const struct hc_driver musb_hc_driver = {
  2. 2497 .description = "musb-hcd",
  3. 2498 .product_desc = "MUSB HDRC host driver",
  4. 2499 .hcd_priv_size = sizeof(struct musb),
  5. 2500 .flags = HCD_USB2 | HCD_MEMORY,
  6. 2501
  7. 2502 /* not using irq handler or reset hooks from usbcore, since
  8. 2503 * those must be shared with peripheral code for OTG configs
  9. 2504 */
  10. 2505
  11. 2506 .start = musb_h_start,
  12. 2507 .stop = musb_h_stop,
  13. 2508
  14. 2509 .get_frame_number = musb_h_get_frame_number,
  15. 2510
  16. 2511 .urb_enqueue = musb_urb_enqueue,
  17. 2512 .urb_dequeue = musb_urb_dequeue,
  18. 2513 .endpoint_disable = musb_h_disable,
  19. 2514
  20. 2515 .hub_status_data = musb_hub_status_data,
  21. 2516 .hub_control = musb_hub_control,
  22. 2517 .bus_suspend = musb_bus_suspend,
  23. 2518 .bus_resume = musb_bus_resume,
  24. 2519 /* .start_port_reset = NULL, */
  25. 2520 /* .hub_irq_enable = NULL, */
  26. 2521 };

继续看,2233行,一个变态的数据结构还不够,还得来一个更变态的.struct usb_hcd,这意思很
明确,有一个 hcd 就得有这么一个结构体,也是来自 drivers/usb/core/hcd.h:

  1. 68 /*-------------------------------------------------------------------------*/
  2. 69
  3. 70 struct usb_hcd {
  4. 71
  5. 72 /*
  6. 73 * housekeeping
  7. 74 */
  8. 75 struct usb_bus self; /* hcd is-a bus */
  9. 76 struct kref kref; /* reference counter */
  10. 77
  11. 78 const char *product_desc; /* product/vendor string */
  12. 79 int speed; /* Speed for this roothub.
  13. 80 * May be different from
  14. 81 * hcd->driver->flags & HCD_MASK
  15. 82 */
  16. 83 char irq_descr[24]; /* driver + bus # */
  17. 84
  18. 85 struct timer_list rh_timer; /* drives root-hub polling */
  19. 86 struct urb *status_urb; /* the current status urb */
  20. 87 #ifdef CONFIG_USB_SUSPEND
  21. 88 struct work_struct wakeup_work; /* for remote wakeup */
  22. 89 #endif
  23. 90
  24. 91 /*
  25. 92 * hardware info/state
  26. 93 */
  27. 94 const struct hc_driver *driver; /* hw-specific hooks */
  28. 95
  29. 96 /* Flags that need to be manipulated atomically because they can
  30. 97 * change while the host controller is running. Always use
  31. 98 * set_bit() or clear_bit() to change their values.
  32. 99 */
  33. 100 unsigned long flags;
  34. 101 #define HCD_FLAG_HW_ACCESSIBLE 0 /* at full power */
  35. 102 #define HCD_FLAG_SAW_IRQ 1
  36. 103 #define HCD_FLAG_POLL_RH 2 /* poll for rh status? */
  37. 104 #define HCD_FLAG_POLL_PENDING 3 /* status has changed? */
  38. 105 #define HCD_FLAG_WAKEUP_PENDING 4 /* root hub is resuming? */
  39. 106 #define HCD_FLAG_RH_RUNNING 5 /* root hub is running? */
  40. 107 #define HCD_FLAG_DEAD 6 /* controller has died? */
  41. 108
  42. 109 /* The flags can be tested using these macros; they are likely to
  43. 110 * be slightly faster than test_bit().
  44. 111 */
  45. 112 #define HCD_HW_ACCESSIBLE(hcd) ((hcd)->flags & (1U << HCD_FLAG_HW_ACCESSIBLE))
  46. 113 #define HCD_SAW_IRQ(hcd) ((hcd)->flags & (1U << HCD_FLAG_SAW_IRQ))
  47. 114 #define HCD_POLL_RH(hcd) ((hcd)->flags & (1U << HCD_FLAG_POLL_RH))
  48. 115 #define HCD_POLL_PENDING(hcd) ((hcd)->flags & (1U << HCD_FLAG_POLL_PENDING))
  49. 116 #define HCD_WAKEUP_PENDING(hcd) ((hcd)->flags & (1U << HCD_FLAG_WAKEUP_PENDING))
  50. 117 #define HCD_RH_RUNNING(hcd) ((hcd)->flags & (1U << HCD_FLAG_RH_RUNNING))
  51. 118 #define HCD_DEAD(hcd) ((hcd)->flags & (1U << HCD_FLAG_DEAD))
  52. 119
  53. 120 /* Flags that get set only during HCD registration or removal. */
  54. 121 unsigned rh_registered:1;/* is root hub registered? */
  55. 122 unsigned rh_pollable:1; /* may we poll the root hub? */
  56. 123 unsigned msix_enabled:1; /* driver has MSI-X enabled? */
  57. 124
  58. 125 /* The next flag is a stopgap, to be removed when all the HCDs
  59. 126 * support the new root-hub polling mechanism. */
  60. 127 unsigned uses_new_polling:1;
  61. 128 unsigned wireless:1; /* Wireless USB HCD */
  62. 129 unsigned authorized_default:1;
  63. 130 unsigned has_tt:1; /* Integrated TT in root hub */
  64. 131
  65. 132 int irq; /* irq allocated */
  66. 133 void __iomem *regs; /* device memory/io */
  67. 134 u64 rsrc_start; /* memory/io resource start */
  68. 135 u64 rsrc_len; /* memory/io resource length */
  69. 136 unsigned power_budget; /* in mA, 0 = no limit */
  70. 137
  71. 138 /* bandwidth_mutex should be taken before adding or removing
  72. 139 * any new bus bandwidth constraints:
  73. 140 * 1. Before adding a configuration for a new device.
  74. 141 * 2. Before removing the configuration to put the device into
  75. 142 * the addressed state.
  76. 143 * 3. Before selecting a different configuration.
  77. 144 * 4. Before selecting an alternate interface setting.
  78. 145 *
  79. 146 * bandwidth_mutex should be dropped after a successful control message
  80. 147 * to the device, or resetting the bandwidth after a failed attempt.
  81. 148 */
  82. 149 struct mutex *bandwidth_mutex;
  83. 150 struct usb_hcd *shared_hcd;
  84. 151 struct usb_hcd *primary_hcd;
  85. 152
  86. 153
  87. 154 #define HCD_BUFFER_POOLS 4
  88. 155 struct dma_pool *pool[HCD_BUFFER_POOLS];
  89. 156
  90. 157 int state;
  91. 158 # define __ACTIVE 0x01
  92. 159 # define __SUSPEND 0x04
  93. 160 # define __TRANSIENT 0x80
  94. 161
  95. 162 # define HC_STATE_HALT 0
  96. 163 # define HC_STATE_RUNNING (__ACTIVE)
  97. 164 # define HC_STATE_QUIESCING (__SUSPEND|__TRANSIENT|__ACTIVE)
  98. 165 # define HC_STATE_RESUMING (__SUSPEND|__TRANSIENT)
  99. 166 # define HC_STATE_SUSPENDED (__SUSPEND)
  100. 167
  101. 168 #define HC_IS_RUNNING(state) ((state) & __ACTIVE)
  102. 169 #define HC_IS_SUSPENDED(state) ((state) & __SUSPEND)
  103. 170
  104. 171 /* more shared queuing code would be good; it should support
  105. 172 * smarter scheduling, handle transaction translators, etc;
  106. 173 * input size of periodic table to an interrupt scheduler.
  107. 174 * (ohci 32, uhci 1024, ehci 256/512/1024).
  108. 175 */
  109. 176
  110. 177 /* The HC driver's private data is stored at the end of
  111. 178 * this structure.
  112. 179 */
  113. 180 unsigned long hcd_priv[0]
  114. 181 __attribute__ ((aligned(sizeof(s64))));
  115. 182 };
  116. 183

 所以 usb_create_hcd 这个函数就是为 struct usb_hcd 申请内存空间,并且初始化.我们来看
它具体如何初始化的.

2235 行是申请内存,并且初值为 0.

2240行:咱们传入primary_hcd =null

2249行:dev->driver_data 等于咱们这里申请好的 hcd.

而 2258行就是初始化一个引用计数,struct usb_hcd 也不是白贴出来了,至少我们可以看到它
有一个成员 struct kref kref,这玩艺说白了就是一个引用计数的变量.

2260行:struct usb_hcd 中有一个成员struct usb_bus self,我们说了一个主机控制器就意味着

一条总线,所以这里又出来另一个结构体,struct usb_bus,
 

  1. 320 * Allocated per bus (tree of devices) we have:
  2. 321 */
  3. 322 struct usb_bus {
  4. 323 struct device *controller; /* host/master side hardware */
  5. 324 int busnum; /* Bus number (in order of reg) */
  6. 325 const char *bus_name; /* stable id (PCI slot_name etc) */
  7. 326 u8 uses_dma; /* Does the host controller use DMA? */
  8. 327 u8 uses_pio_for_control; /*
  9. 328 * Does the host controller use PIO
  10. 329 * for control transfers?
  11. 330 */
  12. 331 u8 otg_port; /* 0, or number of OTG/HNP port */
  13. 332 unsigned is_b_host:1; /* true during some HNP roleswitches */
  14. 333 unsigned b_hnp_enable:1; /* OTG: did A-Host enable HNP? */
  15. 334 unsigned sg_tablesize; /* 0 or largest number of sg list entries */
  16. 335
  17. 336 int devnum_next; /* Next open device number in
  18. 337 * round-robin allocation */
  19. 338
  20. 339 struct usb_devmap devmap; /* device address allocation map */
  21. 340 struct usb_device *root_hub; /* Root hub */
  22. 341 struct usb_bus *hs_companion; /* Companion EHCI bus, if any */
  23. 342 struct list_head bus_list; /* list of busses */
  24. 343
  25. 344 int bandwidth_allocated; /* on this bus: how much of the time
  26. 345 * reserved for periodic (intr/iso)
  27. 346 * requests is used, on average?
  28. 347 * Units: microseconds/frame.
  29. 348 * Limits: Full/low speed reserve 90%,
  30. 349 * while high speed reserves 80%.
  31. 350 */
  32. 351 int bandwidth_int_reqs; /* number of Interrupt requests */
  33. 352 int bandwidth_isoc_reqs; /* number of Isoc. requests */
  34. 353
  35. 354 #ifdef CONFIG_USB_DEVICEFS
  36. 355 struct dentry *usbfs_dentry; /* usbfs dentry entry for the bus */
  37. 356 #endif
  38. 357
  39. 358 #if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE)
  40. 359 struct mon_bus *mon_bus; /* non-null when associated */
  41. 360 int monitored; /* non-zero when monitored */
  42. 361 #endif
  43. 362 };

usb_bus_init 来自drivers/usb/core/hcd.c,很显然,它就是初始化struct usb_bus结构体指
针.而这个结构体变量 hcd->self 的内存已经在刚才为 hcd 申请内存的时候一并申请了.
 

  1. 878 /*-------------------------------------------------------------------------*/
  2. 879
  3. 880 /**
  4. 881 * usb_bus_init - shared initialization code
  5. 882 * @bus: the bus structure being initialized
  6. 883 *
  7. 884 * This code is used to initialize a usb_bus structure, memory for which is
  8. 885 * separately managed.
  9. 886 */
  10. 887 static void usb_bus_init (struct usb_bus *bus)
  11. 888 {
  12. 889 memset (&bus->devmap, 0, sizeof(struct usb_devmap));
  13. 890
  14. 891 bus->devnum_next = 1;
  15. 892
  16. 893 bus->root_hub = NULL;
  17. 894 bus->busnum = -1;
  18. 895 bus->bandwidth_allocated = 0;
  19. 896 bus->bandwidth_int_reqs = 0;
  20. 897 bus->bandwidth_isoc_reqs = 0;
  21. 898
  22. 899 INIT_LIST_HEAD (&bus->bus_list);
  23. 900 }
  24. 901

devnum_next 在总线初始化的时候会被设为 1,

回到 usb_create_shared_hcd中来,又是几行赋值,飘过

倒是 2265行引起了我的注意,又是可恶的时间机制,init_timer,这个函数我们也见过多次
了,usb-storage 里见过,hub 里见过,斑驳的陌生终于在时间的抚摸下变成了今日的熟悉.这里
我们设置的函数是 rh_timer_func,而传递给这个函数的参数是 hcd.这个函数具体做什么我们
走着瞧,不过你放心,咱们这个故事里会多次接触到这个 timer,想逃是逃不掉的,躲得过初一躲不
过十五.

2269行,INIT_WORK 咱们也在 hub 驱动里见过了,这里这个 hcd_resume_work 什么时候会
被调用咱们也到时候再看.

剩下两行赋值,2272行没啥好说的,struct usb_hcd 有一个 struct hc_driver 的结构体指针
员,所以就这样把它和咱们这个 uhci_driver 给联系起来了.而在 uhci_driver 中我们看到,其中
有一个 product_desc 被赋值为"MUSB HDRC host driver",所以这里也赋给
hcd->product_desc,因为 struct hc_driver 和 struct usb_hcd 这两个结构体中都有一个成
员const char *product_desc。

至此,usb_create_hcd 结束了,返回了这个申请好赋好值的 hcd.我们继续回到allocate_instance

函数中来

初始化控制链表、bulk in 、bulk out 链表、初始化gb_list链表

hcd->uses_new_polling = 1;

        hcd->has_tt = 1;

        musb->vbuserr_retry = VBUSERR_RETRY_COUNT;

退出allocate_instance

接下来2206行:musb_platform_init

Kernel_3.2_for_Linux\drivers\usb\musb\musb_core.h

  1. static inline int musb_platform_init(struct musb *musb)
  2. {
  3. if (!musb->ops->init)
  4. return -EINVAL;
  5. return musb->ops->init(musb);
  6. }

这里运行ti81xx_musb_init初始化,咱们现在先略过

2040~2053行:dma的操作

主要运行cppi41_dma_controller_create

  1. 1597 /**
  2. 1598 * cppi41_dma_controller_create -
  3. 1599 * instantiate an object representing DMA controller.
  4. 1600 */
  5. 1601 struct dma_controller * __devinit
  6. 1602 cppi41_dma_controller_create(struct musb *musb, void __iomem *mregs)
  7. 1603 {
  8. 1604 struct cppi41 *cppi;
  9. 1605
  10. 1606 cppi = kzalloc(sizeof *cppi, GFP_KERNEL);
  11. 1607 if (!cppi)
  12. 1608 return NULL;
  13. 1609
  14. 1610 /* Initialize the CPPI 4.1 DMA controller structure */
  15. 1611 cppi->musb = musb;
  16. 1612 cppi->controller.start = cppi41_controller_start;
  17. 1613 cppi->controller.stop = cppi41_controller_stop;
  18. 1614 cppi->controller.channel_alloc = cppi41_channel_alloc;
  19. 1615 cppi->controller.channel_release = cppi41_channel_release;
  20. 1616 cppi->controller.channel_program = cppi41_channel_program;
  21. 1617 cppi->controller.channel_abort = cppi41_channel_abort;
  22. 1618 cppi->cppi_info = (struct usb_cppi41_info *)&usb_cppi41_info[musb->id];;
  23. 1619 cppi->en_bd_intr = cppi->cppi_info->bd_intr_ctrl;
  24. 1620 cppi->txfifo_intr_enable = musb->txfifo_intr_enable;
  25. 1621 cppi->tx_isoc_sched_enable = musb->tx_isoc_sched_enable;
  26. 1622 INIT_WORK(&cppi->txdma_work, txdma_completion_work);
  27. 1623 INIT_WORK(&cppi->rxdma_work, rxdma_completion_work);
  28. 1624
  29. 1625 /*
  30. 1626 * Extra IN token has been seen when a file is transferred from one MSC
  31. 1627 * device to other due to xDMA IP bug when multiple masters access
  32. 1628 * mentor controller register space.
  33. 1629 * As a software workaround use transparent mode and correct data toggle
  34. 1630 * when they go wrong.
  35. 1631 * This issue is expected to be fixed in RTL version post 0xD.
  36. 1632 * Since RTL version is not available to distinguish the fix, based on
  37. 1633 * soc revision the rxdma generic rndis shall be enabled/disabled by
  38. 1634 * platform driver as mentioned below
  39. 1635 * set cppi_info->grndis_for_host_rx = 1 to enable
  40. 1636 * rxdma generic rndis mode
  41. 1637 * set cppi_info->grndis_for_host_rx = 0 and
  42. 1638 * cppi->musb->datatog_fix = 0 to disable the rxdma generic rndis.
  43. 1639 */
  44. 1640 if (cppi->cppi_info->rx_dma_mode == USB_GENERIC_RNDIS_MODE)
  45. 1641 cppi->musb->datatog_fix = 0;
  46. 1642 else
  47. 1643 cppi->musb->datatog_fix = 1;
  48. 1644 dev_dbg(musb->controller, "musb%d: %s cppi41 rxdma mode\n",
  49. 1645 musb->id, cppi->cppi_info->rx_dma_mode ? "generic rndis" :
  50. 1646 "transparent");
  51. 1647
  52. 1648 /* enable infinite mode only for ti81xx silicon rev2 */
  53. 1649 if (cpu_is_am33xx() || cpu_is_ti816x()) {
  54. 1650 /*
  55. 1651 * to enable inf_mode, generic rndis mode must be
  56. 1652 * enabled. also datatog_fix must be set to zero
  57. 1653 */
  58. 1654 cppi->cppi_info->rx_inf_mode = 0;
  59. 1655 dev_dbg(musb->controller, "cppi41dma supports infinite mode\n");
  60. 1656 }
  61. 1657
  62. 1658 return &cppi->controller;
  63. 1659 }
  64. 1660 EXPORT_SYMBOL(cppi41_dma_controller_create);

这就是初始化。返回cppi->controller

接着运行start函数cppi41_controller_start

 2118行                  status = usb_add_hcd(musb_to_hcd(musb), -1, 0);

这个函数位于drivers/usb/core/hcd.c 中:

  1. 2384 /**
  2. 2385 * usb_add_hcd - finish generic HCD structure initialization and register
  3. 2386 * @hcd: the usb_hcd structure to initialize
  4. 2387 * @irqnum: Interrupt line to allocate
  5. 2388 * @irqflags: Interrupt type flags
  6. 2389 *
  7. 2390 * Finish the remaining parts of generic HCD initialization: allocate the
  8. 2391 * buffers of consistent memory, register the bus, request the IRQ line,
  9. 2392 * and call the driver's reset() and start() routines.
  10. 2393 */
  11. 2394 int usb_add_hcd(struct usb_hcd *hcd,
  12. 2395 unsigned int irqnum, unsigned long irqflags)
  13. 2396 {
  14. 2397 int retval;
  15. 2398 struct usb_device *rhdev;
  16. 2399
  17. 2400 dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
  18. 2401
  19. 2402 /* Keep old behaviour if authorized_default is not in [0, 1]. */
  20. 2403 if (authorized_default < 0 || authorized_default > 1)
  21. 2404 hcd->authorized_default = hcd->wireless? 0 : 1;
  22. 2405 else
  23. 2406 hcd->authorized_default = authorized_default;
  24. 2407 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  25. 2408
  26. 2409 /* HC is in reset state, but accessible. Now do the one-time init,
  27. 2410 * bottom up so that hcds can customize the root hubs before khubd
  28. 2411 * starts talking to them. (Note, bus id is assigned early too.)
  29. 2412 */
  30. 2413 if ((retval = hcd_buffer_create(hcd)) != 0) {
  31. 2414 dev_dbg(hcd->self.controller, "pool alloc failed\n");
  32. 2415 return retval;
  33. 2416 }
  34. 2417
  35. 2418 if ((retval = usb_register_bus(&hcd->self)) < 0)
  36. 2419 goto err_register_bus;
  37. 2420
  38. 2421 if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) {
  39. 2422 dev_err(hcd->self.controller, "unable to allocate root hub\n");
  40. 2423 retval = -ENOMEM;
  41. 2424 goto err_allocate_root_hub;
  42. 2425 }
  43. 2426 hcd->self.root_hub = rhdev;
  44. 2427
  45. 2428 switch (hcd->speed) {
  46. 2429 case HCD_USB11:
  47. 2430 rhdev->speed = USB_SPEED_FULL;
  48. 2431 break;
  49. 2432 case HCD_USB2:
  50. 2433 rhdev->speed = USB_SPEED_HIGH;
  51. 2434 break;
  52. 2435 case HCD_USB3:
  53. 2436 rhdev->speed = USB_SPEED_SUPER;
  54. 2437 break;
  55. 2438 default:
  56. 2439 retval = -EINVAL;
  57. 2440 goto err_set_rh_speed;
  58. 2441 }
  59. 2442
  60. 2443 /* wakeup flag init defaults to "everything works" for root hubs,
  61. 2444 * but drivers can override it in reset() if needed, along with
  62. 2445 * recording the overall controller's system wakeup capability.
  63. 2446 */
  64. 2447 device_set_wakeup_capable(&rhdev->dev, 1);
  65. 2448
  66. 2449 /* HCD_FLAG_RH_RUNNING doesn't matter until the root hub is
  67. 2450 * registered. But since the controller can die at any time,
  68. 2451 * let's initialize the flag before touching the hardware.
  69. 2452 */
  70. 2453 set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  71. 2454
  72. 2455 /* "reset" is misnamed; its role is now one-time init. the controller
  73. 2456 * should already have been reset (and boot firmware kicked off etc).
  74. 2457 */
  75. 2458 if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) {
  76. 2459 dev_err(hcd->self.controller, "can't setup\n");
  77. 2460 goto err_hcd_driver_setup;
  78. 2461 }
  79. 2462 hcd->rh_pollable = 1;
  80. 2463
  81. 2464 /* NOTE: root hub and controller capabilities may not be the same */
  82. 2465 if (device_can_wakeup(hcd->self.controller)
  83. 2466 && device_can_wakeup(&hcd->self.root_hub->dev))
  84. 2467 dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
  85. 2468
  86. 2469 /* enable irqs just before we start the controller */
  87. 2470 if (usb_hcd_is_primary_hcd(hcd)) {
  88. 2471 retval = usb_hcd_request_irqs(hcd, irqnum, irqflags);
  89. 2472 if (retval)
  90. 2473 goto err_request_irq;
  91. 2474 }
  92. 2475
  93. 2476 hcd->state = HC_STATE_RUNNING;
  94. 2477 retval = hcd->driver->start(hcd);
  95. 2478 if (retval < 0) {
  96. 2479 dev_err(hcd->self.controller, "startup error %d\n", retval);
  97. 2480 goto err_hcd_driver_start;
  98. 2481 }
  99. 2482
  100. 2483 /* starting here, usbcore will pay attention to this root hub */
  101. 2484 rhdev->bus_mA = min(500u, hcd->power_budget);
  102. 2485 if ((retval = register_root_hub(hcd)) != 0)
  103. 2486 goto err_register_root_hub;
  104. 2487
  105. 2488 retval = sysfs_create_group(&rhdev->dev.kobj, &usb_bus_attr_group);
  106. 2489 if (retval < 0) {
  107. 2490 printk(KERN_ERR "Cannot register USB bus sysfs attributes: %d\n",
  108. 2491 retval);
  109. 2492 goto error_create_attr_group;
  110. 2493 }
  111. 2494 if (hcd->uses_new_polling && HCD_POLL_RH(hcd))
  112. 2495 usb_hcd_poll_rh_status(hcd);
  113. 2496
  114. 2497 /*
  115. 2498 * Host controllers don't generate their own wakeup requests;
  116. 2499 * they only forward requests from the root hub. Therefore
  117. 2500 * controllers should always be enabled for remote wakeup.
  118. 2501 */
  119. 2502 device_wakeup_enable(hcd->self.controller);
  120. 2503 return retval;
  121. 2504
  122. 2505 error_create_attr_group:
  123. 2506 clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  124. 2507 if (HC_IS_RUNNING(hcd->state))
  125. 2508 hcd->state = HC_STATE_QUIESCING;
  126. 2509 spin_lock_irq(&hcd_root_hub_lock);
  127. 2510 hcd->rh_registered = 0;
  128. 2511 spin_unlock_irq(&hcd_root_hub_lock);
  129. 2512
  130. 2513 #ifdef CONFIG_USB_SUSPEND
  131. 2514 cancel_work_sync(&hcd->wakeup_work);
  132. 2515 #endif
  133. 2516 mutex_lock(&usb_bus_list_lock);
  134. 2517 usb_disconnect(&rhdev); /* Sets rhdev to NULL */
  135. 2518 mutex_unlock(&usb_bus_list_lock);
  136. 2519 err_register_root_hub:
  137. 2520 hcd->rh_pollable = 0;
  138. 2521 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  139. 2522 del_timer_sync(&hcd->rh_timer);
  140. 2523 hcd->driver->stop(hcd);
  141. 2524 hcd->state = HC_STATE_HALT;
  142. 2525 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  143. 2526 del_timer_sync(&hcd->rh_timer);
  144. 2527 err_hcd_driver_start:
  145. 2528 if (usb_hcd_is_primary_hcd(hcd) && hcd->irq >= 0)
  146. 2529 free_irq(irqnum, hcd);
  147. 2530 err_request_irq:
  148. 2531 err_hcd_driver_setup:
  149. 2532 err_set_rh_speed:
  150. 2533 usb_put_dev(hcd->self.root_hub);
  151. 2534 err_allocate_root_hub:
  152. 2535 usb_deregister_bus(&hcd->self);
  153. 2536 err_register_bus:
  154. 2537 hcd_buffer_destroy(hcd);
  155. 2538 return retval;
  156. 2539 }
  157. 2540 EXPORT_SYMBOL_GPL(usb_add_hcd);

2407 行,设置一个 flag,至于设了干嘛用,等遇到了再说.

2413行:hcd_buffer_create,初始化一个 buffer 池.现在是时候说一说 DMA 了.我们知道一个
USB 主机控制器控制着一条 USB 总线,而 USB 主机控制器的一项重要工作是什么呢?在内存和
USB 总线之间传输数据.这个过程可以使用 DMA 或者不使用 DMA,不使用 DMA 的方式即所谓
的 PIO 方式.DMA 代表着 Direct Memory Access,即直接内存访问.
 

  1. 36
  2. 37 /* SETUP primitives */
  3. 38
  4. 39 /**
  5. 40 * hcd_buffer_create - initialize buffer pools
  6. 41 * @hcd: the bus whose buffer pools are to be initialized
  7. 42 * Context: !in_interrupt()
  8. 43 *
  9. 44 * Call this as part of initializing a host controller that uses the dma
  10. 45 * memory allocators. It initializes some pools of dma-coherent memory that
  11. 46 * will be shared by all drivers using that controller, or returns a negative
  12. 47 * errno value on error.
  13. 48 *
  14. 49 * Call hcd_buffer_destroy() to clean up after using those pools.
  15. 50 */
  16. 51 int hcd_buffer_create(struct usb_hcd *hcd)
  17. 52 {
  18. 53 char name[16];
  19. 54 int i, size;
  20. 55
  21. 56 if (!hcd->self.controller->dma_mask &&
  22. 57 !(hcd->driver->flags & HCD_LOCAL_MEM))
  23. 58 return 0;
  24. 59
  25. 60 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  26. 61 size = pool_max[i];
  27. 62 if (!size)
  28. 63 continue;
  29. 64 snprintf(name, sizeof name, "buffer-%d", size);
  30. 65 hcd->pool[i] = dma_pool_create(name, hcd->self.controller,
  31. 66 size, size, 0);
  32. 67 if (!hcd->pool[i]) {
  33. 68 hcd_buffer_destroy(hcd);
  34. 69 return -ENOMEM;
  35. 70 }
  36. 71 }
  37. 72 return 0;
  38. 73 }
  39. 74

看 64 行,调用了 dma_pool_create 函数,这个函数就是真正去创建内存池
的函数,或者更准确地讲,创建一个DMA池,内核中定义了一个结构体,struct dma_pool,如果创建失败就调用
hcd_buffer_destroy,还是来自同一个文件
 

  1. 75
  2. 76 /**
  3. 77 * hcd_buffer_destroy - deallocate buffer pools
  4. 78 * @hcd: the bus whose buffer pools are to be destroyed
  5. 79 * Context: !in_interrupt()
  6. 80 *
  7. 81 * This frees the buffer pools created by hcd_buffer_create().
  8. 82 */
  9. 83 void hcd_buffer_destroy(struct usb_hcd *hcd)
  10. 84 {
  11. 85 int i;
  12. 86
  13. 87 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  14. 88 struct dma_pool *pool = hcd->pool[i];
  15. 89 if (pool) {
  16. 90 dma_pool_destroy(pool);
  17. 91 hcd->pool[i] = NULL;
  18. 92 }
  19. 93 }
  20. 94 }
  21. 95

看得出这里调用的是 dma_pool_destroy,其作用不言自明.
那么创建池子和销毁池子的函数我们知道了,如何从池子里索取或者把索取的释放回去呢?对应
的两个函数分别是,dma_pool_alloc 和 dma_pool_free,而这两个函数正是与我们说的
usb_buffer_alloc 以及 usb_buffer_free 相联系的.于是我们来看这两个函数的代码,来自
drivers/usb/core/usb.c:
 

  1. 96
  2. 97 /* sometimes alloc/free could use kmalloc with GFP_DMA, for
  3. 98 * better sharing and to leverage mm/slab.c intelligence.
  4. 99 */
  5. 100
  6. 101 void *hcd_buffer_alloc(
  7. 102 struct usb_bus *bus,
  8. 103 size_t size,
  9. 104 gfp_t mem_flags,
  10. 105 dma_addr_t *dma
  11. 106 )
  12. 107 {
  13. 108 struct usb_hcd *hcd = bus_to_hcd(bus);
  14. 109 int i;
  15. 110
  16. 111 /* some USB hosts just use PIO */
  17. 112 if (!bus->controller->dma_mask &&
  18. 113 !(hcd->driver->flags & HCD_LOCAL_MEM)) {
  19. 114 *dma = ~(dma_addr_t) 0;
  20. 115 return kmalloc(size, mem_flags);
  21. 116 }
  22. 117
  23. 118 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  24. 119 if (size <= pool_max[i])
  25. 120 return dma_pool_alloc(hcd->pool[i], mem_flags, dma);
  26. 121 }
  27. 122 return dma_alloc_coherent(hcd->self.controller, size, dma, mem_flags);
  28. 123 }
  29. 124
  30. 125 void hcd_buffer_free(
  31. 126 struct usb_bus *bus,
  32. 127 size_t size,
  33. 128 void *addr,
  34. 129 dma_addr_t dma
  35. 130 )
  36. 131 {
  37. 132 struct usb_hcd *hcd = bus_to_hcd(bus);
  38. 133 int i;
  39. 134
  40. 135 if (!addr)
  41. 136 return;
  42. 137
  43. 138 if (!bus->controller->dma_mask &&
  44. 139 !(hcd->driver->flags & HCD_LOCAL_MEM)) {
  45. 140 kfree(addr);
  46. 141 return;
  47. 142 }
  48. 143
  49. 144 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  50. 145 if (size <= pool_max[i]) {
  51. 146 dma_pool_free(hcd->pool[i], addr, dma);
  52. 147 return;
  53. 148 }
  54. 149 }
  55. 150 dma_free_coherent(hcd->self.controller, size, addr, dma);
  56. 151 }

看见了吧,最终调用的就是dma_pool_alloc和dma_pool_free.那么主机控制器到底支持不支
持 DMA 操作呢?看见上面这个 dma_mask 了么?默认情况下,dma_mask 在总线枚举的时候


static u64 musb_dmamask = DMA_BIT_MASK(32);

把 dma_mask 设置为 0x00ffffff.因为标准的 设备都是32位的寻址的,所以标准情况就是设置的DMA_BIT_MASK(32)

要不,总结一下?以上这几个 DMA 函数我们就不细讲了.但需要对某些地方单独拿出来讲.


第一,hcd_buffer_alloc 函数中,111 行,判断,如果 dma_mask 为 NULL,说明这个主机控制器
不支持 DMA,那么使用原始的方法申请内存,即 kmalloc.然后申请好了就直接返回,而不会继续
去执行下面的那个 dma_pool_alloc 函数.同样的判断在 hcd_buffer_free 中也是一样的,没有
DMA 的就直接调用 kfree 释放内存,而不需要调用 dma_pool_free 了.


第二,你应该注意到这里还有另外两个函数我们根本没提起, dma_alloc_coherent 和
dma_free_coherent,这两个函数也是用来申请DMA内存的,但是它们适合申请比较大的内存,
比如N个page的那种,而DMA池的作用本来就是提供给小打小闹式的内存申请的.当前的USB
子系统里在 drivers/usb/core/buffer.c 中定义了一个数组:

  1. 24 /* FIXME tune these based on pool statistics ... */
  2. 25 static const size_t pool_max[HCD_BUFFER_POOLS] = {
  3. 26 /* platforms without dma-friendly caches might need to
  4. 27 * prevent cacheline sharing...
  5. 28 */
  6. 29 32,
  7. 30 128,
  8. 31 512,
  9. 32 PAGE_SIZE / 2
  10. 33 /* bigger --> allocate pages */
  11. 34 };
  12. 35

HCD_BUFFER_POOLS 这个宏的值为 4.结合 hcd_buffer_alloc 函数里面那个循环来看,可以
知道,你要是申请个 32 个字节以内,128 个字节以内,512 个字节以内,或者最多二分之一个
PAGE_SIZE 以内的,就直接使用这个内存池了,否则的话,就得用那个 dma_alloc_coherent了.
释放的时候也一样.
第三,struct usb_hcd 结构体有这么一个成员,struct dma_pool *pool
[HCD_BUFFER_POOLS], 这个数组的值是在 hcd_buffer_create 中赋上的,即当时以这个
pool_max 为模型创建了 4 个池子,所以 hcd_buffer_alloc 里就可以这样用.
第四,至于像 dma_pool_create/dma_pool_alloc/dma_alloc_coherent 这些函数具体怎么
实现的我想任何一个写设备驱动程序的都不用关心吧

讲完了 hcd_buffer_create 让我们还是回到 usb_add_hcd 中来,继续往下走.


下一个函数,2148 行,usb_register_bus.我们说过,一个 USB 主机控制器就意味着一条 USB
总线,因为主机控制器控制的正是一条总线.古人说,猫走不走直线,完全取决于耗子,而数据走不
走总线,完全取决于主机控制器.
所以这里作为主机控制器的驱动,我们必须从软件的角度来说,注册一条总线.来自drivers/usb/core/hcd.c:

  1. 902 /*-------------------------------------------------------------------------*/
  2. 903
  3. 904 /**
  4. 905 * usb_register_bus - registers the USB host controller with the usb core
  5. 906 * @bus: pointer to the bus to register
  6. 907 * Context: !in_interrupt()
  7. 908 *
  8. 909 * Assigns a bus number, and links the controller into usbcore data
  9. 910 * structures so that it can be seen by scanning the bus list.
  10. 911 */
  11. 912 static int usb_register_bus(struct usb_bus *bus)
  12. 913 {
  13. 914 int result = -E2BIG;
  14. 915 int busnum;
  15. 916
  16. 917 mutex_lock(&usb_bus_list_lock);
  17. 918 busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1);
  18. 919 if (busnum >= USB_MAXBUS) {
  19. 920 printk (KERN_ERR "%s: too many buses\n", usbcore_name);
  20. 921 goto error_find_busnum;
  21. 922 }
  22. 923 set_bit (busnum, busmap.busmap);
  23. 924 bus->busnum = busnum;
  24. 925
  25. 926 /* Add it to the local list of buses */
  26. 927 list_add (&bus->bus_list, &usb_bus_list);
  27. 928 mutex_unlock(&usb_bus_list_lock);
  28. 929
  29. 930 usb_notify_add_bus(bus);
  30. 931
  31. 932 dev_info (bus->controller, "new USB bus registered, assigned bus "
  32. 933 "number %d\n", bus->busnum);
  33. 934 return 0;
  34. 935
  35. 936 error_find_busnum:
  36. 937 mutex_unlock(&usb_bus_list_lock);
  37. 938 return result;
  38. 939 }
  39. 940

918行 :可知这里 busmap 则一共有 64 位.也就是说一共可以有 64 条 USB 总线.
 

930行,usb_notify_add_bus添加到设备。

好了,usb_register_bus 算是看完了,再一次回到 usb_add_hcd 中来.

2421行;usb_alloc_dev,多么熟悉啊,在讲 hub 驱动时这就是那个八大函数的第一个.这里做的就
是为 Root Hub 申请了一个 struct usb_device 结构体,并且初始化,将返回值赋给指针 rhdev.
回顾这个函数我们可以知道,Root Hub 的 parent 指针指向了 Controller 本身
 

2428行,确定 rhdev 的 speed,UHCI 和 OHCI 都是源于曾经的 USB1.1,而 EHCI 才是来自
USB2.0.只有 USB2.0 才定义了高速的设备,以前的设备只有两种速度,低速和全速.也只有
EHCI 的驱动才定义了一个 flag,HCD_USB2.所以咱们这里记录的 rhdev->speed 就是
USB_SPEED_FULL.不过我需要再次提醒一下,hcd->driver 在咱们的故事里就是那个
hc_driver,即 musb_hc_driver,有且只有这一个 driver.

2447行,device_set_wakeup_capable.也是咱们在 hub 驱动中见过的.第二个参数是 1 就意味着我们
把 Root Hub 的 Wakeup 能力打开了.正如注释里说的那样,你要是看不惯,你可以在自己的
driver 里面把它关掉.

2458 行,如果 musb_hc_driver中有 reset 函数,就调用它,咱们的 musb_hc_driver里面显然是没有

2471行:中断.看driver有没有一个irq函数,如果没有,就简单的记录hcd->irq为-1,hcd->irq
就是用来记录传说中的中断号的.如果有,那就有事情要做了当然,现在不用执行它,只是需要为它

做点事情.最重要的当然就是request_irq 这个函数.我们先来看看它直观的效果

 接下来,2477 行,下一个函数,driver->start 被调用.musb_h_start

  1. static int musb_h_start(struct usb_hcd *hcd)
  2. {
  3. struct musb *musb = hcd_to_musb(hcd);
  4. /* NOTE: musb_start() is called when the hub driver turns
  5. * on port power, or when (OTG) peripheral starts.
  6. */
  7. hcd->state = HC_STATE_RUNNING;
  8. musb->port1_status = 0;
  9. return 0;
  10. }

回到 usb_add_hcd 之后,2484 行,得出 rhdev 的 bus_mA,这个咱们在 Hub 驱动中已经讲过.
有些主机控制器是需要设置 power_budget,咱们没有设置过,就是默认值 0,所以这里得到的是
bus_mA就是0,0表示没有限制,hub驱动中我们看到了对于没有限制的情况我们是给每个端口
设置为最多 500mA,因为通常来讲计算机的 usb 端口能提供的最多就是 500mA
2485行,register_root_hub,来自 drivers/usb/core/hcd.c.

  1. 967 /**
  2. 968 * register_root_hub - called by usb_add_hcd() to register a root hub
  3. 969 * @hcd: host controller for this root hub
  4. 970 *
  5. 971 * This function registers the root hub with the USB subsystem. It sets up
  6. 972 * the device properly in the device tree and then calls usb_new_device()
  7. 973 * to register the usb device. It also assigns the root hub's USB address
  8. 974 * (always 1).
  9. 975 */
  10. 976 static int register_root_hub(struct usb_hcd *hcd)
  11. 977 {
  12. 978 struct device *parent_dev = hcd->self.controller;
  13. 979 struct usb_device *usb_dev = hcd->self.root_hub;
  14. 980 const int devnum = 1;
  15. 981 int retval;
  16. 982
  17. 983 usb_dev->devnum = devnum;
  18. 984 usb_dev->bus->devnum_next = devnum + 1;
  19. 985 memset (&usb_dev->bus->devmap.devicemap, 0,
  20. 986 sizeof usb_dev->bus->devmap.devicemap);
  21. 987 set_bit (devnum, usb_dev->bus->devmap.devicemap);
  22. 988 usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
  23. 989
  24. 990 mutex_lock(&usb_bus_list_lock);
  25. 991
  26. 992 usb_dev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
  27. 993 retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
  28. 994 if (retval != sizeof usb_dev->descriptor) {
  29. 995 mutex_unlock(&usb_bus_list_lock);
  30. 996 dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
  31. 997 dev_name(&usb_dev->dev), retval);
  32. 998 return (retval < 0) ? retval : -EMSGSIZE;
  33. 999 }
  34. 1000
  35. 1001 retval = usb_new_device (usb_dev);
  36. 1002 if (retval) {
  37. 1003 dev_err (parent_dev, "can't register root hub for %s, %d\n",
  38. 1004 dev_name(&usb_dev->dev), retval);
  39. 1005 }
  40. 1006 mutex_unlock(&usb_bus_list_lock);
  41. 1007
  42. 1008 if (retval == 0) {
  43. 1009 spin_lock_irq (&hcd_root_hub_lock);
  44. 1010 hcd->rh_registered = 1;
  45. 1011 spin_unlock_irq (&hcd_root_hub_lock);
  46. 1012
  47. 1013 /* Did the HC die before the root hub was registered? */
  48. 1014 if (HCD_DEAD(hcd))
  49. 1015 usb_hc_died (hcd); /* This time clean up */
  50. 1016 }
  51. 1017
  52. 1018 return retval;
  53. 1019 }
  54. 1020
  55. 1021

这个函数的意图很明显,我们说过,Host Controller 通常是带有一个 Root Hub 的,常言说得好,
不想吃天鹅肉的癞蛤蟆不是好癞蛤蟆,同样,不集成 Root Hub 的主机控制器也不是好主机控制
器,常言又说了,吃了天鹅肉的癞蛤蟆还是癞蛤蟆,同样,集成了 Root Hub 的主机控制器也还是主
机控制器.


980行,root hub 的 devnum 设置为 1,毫无疑问.因为整棵设备树就是起源于 Root Hub,如果
把 usb 设备树比做水泊梁山一百单八好汉,那么 Root Hub 就相当于及时雨宋江,他不做老大谁
做老大.
984行,从现在开始记录 bus 的 devnum_next,从 2 开始.

801,802,803 行,初始化 bus 的 devmap,并且把 root hub 的那把交椅先给占了.
804 行,usb_set_device_state(),不用多说了吧,hub driver 中那个八大函数中的第二
个.Root Hub 说:我准备好了!于是为它把状态设置为 USB_STATE_ADDRESS.
紧接着,808 和 809 行,设置 Root Hub 的 wMaxPacketSize 为 64,然后获取 Root Hub 的设
备描述符.
817 行,usb_new_device,我们更加不会陌生,hub driver 中八大函数的第七个.这个函数结束
之后,咱们的 Root Hub 就算从软件的角度来说彻底融入了整个 usb 世界,或者说这一步就算是
真正的注册了.一切顺利的话返回 0.
1011 行,设置 rh_registered 为 1.顾名思义,告诉全世界,咱们这个 HCD 的 Root Hub 现在算是
有户口的人了.
1012行这个 if 就是为变态们准备的,你这里正在注册呢,也不知哪位哥们儿缺德,帮你把主机控制
器 的 驱 动 给 卸 载 了,比 如 他 卸 载 了 uhci-hcd,那 么 usb_remove_hcd 会 被 执 行,于 是
hcd->state会被设置为HC_STATE_HALT,真遇上这么一件倒霉事那咱也没办法,没啥好说的,
执行usb_hc_died吧.这个函数是用来汇报说主机控制器不正常的shutdown了.这个函数并不
复杂,但是有好几处调用了它,咱们稍微看一下,来自 drivers/usb/core/hcd.c:

  1. 2168
  2. 2169 /**
  3. 2170 * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
  4. 2171 * @hcd: pointer to the HCD representing the controller
  5. 2172 *
  6. 2173 * This is called by bus glue to report a USB host controller that died
  7. 2174 * while operations may still have been pending. It's called automatically
  8. 2175 * by the PCI glue, so only glue for non-PCI busses should need to call it.
  9. 2176 *
  10. 2177 * Only call this function with the primary HCD.
  11. 2178 */
  12. 2179 void usb_hc_died (struct usb_hcd *hcd)
  13. 2180 {
  14. 2181 unsigned long flags;
  15. 2182
  16. 2183 dev_err (hcd->self.controller, "HC died; cleaning up\n");
  17. 2184
  18. 2185 spin_lock_irqsave (&hcd_root_hub_lock, flags);
  19. 2186 clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  20. 2187 set_bit(HCD_FLAG_DEAD, &hcd->flags);
  21. 2188 if (hcd->rh_registered) {
  22. 2189 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  23. 2190
  24. 2191 /* make khubd clean up old urbs and devices */
  25. 2192 usb_set_device_state (hcd->self.root_hub,
  26. 2193 USB_STATE_NOTATTACHED);
  27. 2194 usb_kick_khubd (hcd->self.root_hub);
  28. 2195 }
  29. 2196 if (usb_hcd_is_primary_hcd(hcd) && hcd->shared_hcd) {
  30. 2197 hcd = hcd->shared_hcd;
  31. 2198 if (hcd->rh_registered) {
  32. 2199 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  33. 2200
  34. 2201 /* make khubd clean up old urbs and devices */
  35. 2202 usb_set_device_state(hcd->self.root_hub,
  36. 2203 USB_STATE_NOTATTACHED);
  37. 2204 usb_kick_khubd(hcd->self.root_hub);
  38. 2205 }
  39. 2206 }
  40. 2207 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  41. 2208 /* Make sure that the other roothub is also deallocated. */
  42. 2209 }
  43. 2210 EXPORT_SYMBOL_GPL (usb_hc_died);
  1. void usb_kick_khubd(struct usb_device *hdev)
  2. {
  3. struct usb_hub *hub = hdev_to_hub(hdev);
  4. if (hub)
  5. kick_khubd(hub);
  6. }

关于 kick_khubd 我想就不用多说了吧,hub driver 中最重要的函数之一.我们知道这个函数会
触发 hub_events(),在 hub_events 中判断出 hub 处于了 USB_STATE_NOTATTACHED 的
状态,就会调用 hub_pre_reset 去处理那些后事.
接下来就该是 usb_hcd_poll_rh_status 了.这个函数在咱们整个故事将出现多次,甚至可以说
在任何一个 HCD 的故事中都将出现多次.为了继续走下去,我们必须做一个伟大的假设.假设现
在 Root Hub 上还没有连接任何设备,也就是说此时此刻,usb 设备树上只有 Root Hub 形单影
只.没有人来陪伴他,他只能静静的看青春难依难舍,只能听寂寞在唱歌,轻轻的,狠狠的,歌声是这
么残忍让人忍不住泪流成河.
我们以此为上下文开始往下看.
usb_hcd_poll_rh_status 来自 drivers/usb/core/hcd.c:

  1. 680 /*-------------------------------------------------------------------------*/
  2. 681
  3. 682 /*
  4. 683 * Root Hub interrupt transfers are polled using a timer if the
  5. 684 * driver requests it; otherwise the driver is responsible for
  6. 685 * calling usb_hcd_poll_rh_status() when an event occurs.
  7. 686 *
  8. 687 * Completions are called in_interrupt(), but they may or may not
  9. 688 * be in_irq().
  10. 689 */
  11. 690 void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
  12. 691 {
  13. 692 struct urb *urb;
  14. 693 int length;
  15. 694 unsigned long flags;
  16. 695 char buffer[6]; /* Any root hubs with > 31 ports? */
  17. 696
  18. 697 if (unlikely(!hcd->rh_pollable))
  19. 698 return;
  20. 699 if (!hcd->uses_new_polling && !hcd->status_urb)
  21. 700 return;
  22. 701
  23. 702 length = hcd->driver->hub_status_data(hcd, buffer);
  24. 703 if (length > 0) {
  25. 704
  26. 705 /* try to complete the status urb */
  27. 706 spin_lock_irqsave(&hcd_root_hub_lock, flags);
  28. 707 urb = hcd->status_urb;
  29. 708 if (urb) {
  30. 709 clear_bit(HCD_FLAG_POLL_PENDING, &hcd->flags);
  31. 710 hcd->status_urb = NULL;
  32. 711 urb->actual_length = length;
  33. 712 memcpy(urb->transfer_buffer, buffer, length);
  34. 713
  35. 714 usb_hcd_unlink_urb_from_ep(hcd, urb);
  36. 715 spin_unlock(&hcd_root_hub_lock);
  37. 716 usb_hcd_giveback_urb(hcd, urb, 0);
  38. 717 spin_lock(&hcd_root_hub_lock);
  39. 718 } else {
  40. 719 length = 0;
  41. 720 set_bit(HCD_FLAG_POLL_PENDING, &hcd->flags);
  42. 721 }
  43. 722 spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
  44. 723 }
  45. 724
  46. 725 /* The USB 2.0 spec says 256 ms. This is close enough and won't
  47. 726 * exceed that limit if HZ is 100. The math is more clunky than
  48. 727 * maybe expected, this is to make sure that all timers for USB devices
  49. 728 * fire at the same time to give the CPU a break in between */
  50. 729 if (hcd->uses_new_polling ? HCD_POLL_RH(hcd) :
  51. 730 (length == 0 && hcd->status_urb != NULL))
  52. 731 mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
  53. 732 }
  54. 733 EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status);
  55. 734
  56. 735 /* timer callback */
  57. 736 static void rh_timer_func (unsigned long _hcd)
  58. 737 {
  59. 738 usb_hcd_poll_rh_status((struct usb_hcd *) _hcd);
  60. 739 }
  61. 740

前 面 两 个 if 对 咱 们 来 说 肯 定 是 不 满 足 的 .rh_registered 咱 们 刚 刚 才 设 置 为 1.
uses_new_polling 咱们也在 uhci_start()中设置为了 1.所以,咱们继续昂首挺胸的往前走.
702行,driver->hub_status_data 是每个 driver 自己定义的musb_hub_status_data

  1. 203
  2. 204 /* Caller may or may not hold musb->lock */
  3. 205 int musb_hub_status_data(struct usb_hcd *hcd, char *buf)
  4. 206 {
  5. 207 struct musb *musb = hcd_to_musb(hcd);
  6. 208 int retval = 0;
  7. 209
  8. 210 /* called in_irq() via usb_hcd_poll_rh_status() */
  9. 211 if (musb->port1_status & 0xffff0000) {
  10. 212 *buf = 0x02;
  11. 213 retval = 1;
  12. 214 }
  13. 215 return retval;
  14. 216 }

usb_hcd_poll_rh_status 这个函数完成usb_add_hcd完成

现在虽然最伟大的 probe 函数就这样结束了。。


 

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

闽ICP备14008679号