2524
2525 static struct platform_driver musb_driver = {
2526 .driver = {
2527 .name = (char *)musb_driver_name,
2528 .bus = &platform_bus_type,
2529 .owner = THIS_MODULE,
2530 .pm = MUSB_DEV_PM_OPS,
2531 },
2532 .probe = musb_probe,
2533 .remove = __exit_p(musb_remove),
2534 .shutdown = musb_shutdown,
2535 };
2536
2537 /*-------------------------------------------------------------------------*/
2538
2539 static int __init musb_init(void)
2540 {
2541 if (usb_disabled())
2542 return 0;
2543
2544 pr_info("%s: version " MUSB_VERSION ", "
2545 "?dma?"
2546 ", "
2547 "otg (peripheral+host)",
2548 musb_driver_name);
2549 return platform_driver_register(&musb_driver);
2550 }
2551
2552 /* make us init after usbcore and i2c (transceivers, regulators, etc)
2553 * and before usb gadget and host-side drivers start to register
2554 */
2555 fs_initcall(musb_init);
2556
2557 static void __exit musb_cleanup(void)
2558 {
2559 platform_driver_unregister(&musb_driver);
2560 }
2561 module_exit(musb_cleanup);
根据匹配运行probe
2218
2219 /* all implementations (PCI bridge to FPGA, VLYNQ, etc) should just
2220 * bridge to a platform device; this driver then suffices.
2221 */
2222 static int __devinit musb_probe(struct platform_device *pdev)
2223 {
2224 struct device *dev = &pdev->dev;
2225 int irq = platform_get_irq_byname(pdev, "mc");
2226 int status;
2227 struct resource *iomem;
2228 void __iomem *base;
2229 char res_name[20];
2230
2231 if (pdev->id == -1)
2232 strcpy(res_name, "mc");
2233 else
2234 sprintf(res_name, "musb%d-irq", pdev->id);
2235 irq = platform_get_irq_byname(pdev, res_name);
2236
2237 if (pdev->id == -1)
2238 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2239 else {
2240 sprintf(res_name, "musb%d", pdev->id);
2241 iomem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2242 res_name);
2243 }
2244
2245 if (!iomem || irq <= 0)
2246 return -ENODEV;
2247
2248 base = ioremap(iomem->start, resource_size(iomem));
2249 if (!base) {
2250 dev_err(dev, "ioremap failed\n");
2251 return -ENOMEM;
2252 }
2253
2254 status = musb_init_controller(dev, irq, base);
2255 if (status < 0)
2256 iounmap(base);
2257
2258 return status;
2259 }
2260
看到前面是获取中断号musb0-irq、获取内存资源musb0
2254行:musb_init_controller
1953 /*
1954 * Perform generic per-controller initialization.
1955 *
1956 * @pDevice: the controller (already clocked, etc)
1957 * @nIrq: irq
1958 * @mregs: virtual address of controller registers,
1959 * not yet corrected for platform-specific offsets
1960 */
1961 static int __devinit
1962 musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
1963 {
1964 int status;
1965 struct musb *musb;
1966 struct musb_hdrc_platform_data *plat = dev->platform_data;
1967 struct platform_device *pdev = to_platform_device(dev);
1968
1969 /* The driver might handle more features than the board; OK.
1970 * Fail when the board needs a feature that's not enabled.
1971 */
1972 if (!plat) {
1973 dev_dbg(dev, "no platform_data?\n");
1974 status = -ENODEV;
1975 goto fail0;
1976 }
1977
1978 /* allocate */
1979 musb = allocate_instance(dev, plat->config, ctrl);
1980 if (!musb) {
1981 status = -ENOMEM;
1982 goto fail0;
1983 }
1984
1985 pm_runtime_use_autosuspend(musb->controller);
1986 pm_runtime_set_autosuspend_delay(musb->controller, 200);
1987 pm_runtime_enable(musb->controller);
1988
1989 spin_lock_init(&musb->lock);
1990 musb->board_mode = plat->mode;
1991 musb->board_set_power = plat->set_power;
1992 musb->min_power = plat->min_power;
1993 musb->ops = plat->platform_ops;
1994 musb->id = pdev->id;
1995 musb->first = 1;
1996 if (is_host_enabled(musb))
1997 spin_lock_init(&musb->gb_lock);
1998
1999 musb->fifo_mode = musb->ops->fifo_mode;
2000
2001 #ifndef CONFIG_MUSB_PIO_ONLY
2002 musb->orig_dma_mask = dev->dma_mask;
2003 #endif
2004 if (musb->ops->flags & MUSB_GLUE_TUSB_STYLE) {
2005 musb_readb = __tusb_musb_readb;
2006 musb_writeb = __tusb_musb_writeb;
2007 } else {
2008 musb_readb = __musb_readb;
2009 musb_writeb = __musb_writeb;
2010 }
2011
2012 dev_info(dev, "dma type: %s\n", get_dma_name(musb));
2013
2014 /* The musb_platform_init() call:
2015 * - adjusts musb->mregs and musb->isr if needed,
2016 * - may initialize an integrated tranceiver
2017 * - initializes musb->xceiv, usually by otg_get_transceiver()
2018 * - stops powering VBUS
2019 *
2020 * There are various transceiver configurations. Blackfin,
2021 * DaVinci, TUSB60x0, and others integrate them. OMAP3 uses
2022 * external/discrete ones in various flavors (twl4030 family,
2023 * isp1504, non-OTG, etc) mostly hooking up through ULPI.
2024 */
2025 musb->isr = generic_interrupt;
2026 status = musb_platform_init(musb);
2027 if (status < 0)
2028 goto fail1;
2029
2030 if (!musb->isr) {
2031 status = -ENODEV;
2032 goto fail3;
2033 }
2034
2035 if (!musb->xceiv->io_ops) {
2036 musb->xceiv->io_priv = musb->mregs;
2037 musb->xceiv->io_ops = &musb_ulpi_access;
2038 }
2039
2040 #ifndef CONFIG_MUSB_PIO_ONLY
2041 if (dev->dma_mask) {
2042 struct dma_controller *c;
2043
2044 if (!musb->ops->dma_controller_create) {
2045 dev_err(dev, "no dma_controller_create for non-PIO mode!\n");
2046 status = -ENODEV;
2047 goto fail3;
2048 }
2049 c = musb->ops->dma_controller_create(musb, musb->mregs);
2050 musb->dma_controller = c;
2051 if (c)
2052 (void) c->start(c);
2053 }
2054 #endif
2055 /* ideally this would be abstracted in platform setup */
2056 if (!is_dma_capable() || !musb->dma_controller)
2057 dev->dma_mask = NULL;
2058
2059 /* be sure interrupts are disabled before connecting ISR */
2060 musb_platform_disable(musb);
2061 musb_generic_disable(musb);
2062
2063 /* setup musb parts of the core (especially endpoints) */
2064 status = musb_core_init(plat->config->multipoint
2065 ? MUSB_CONTROLLER_MHDRC
2066 : MUSB_CONTROLLER_HDRC, musb);
2067 if (status < 0)
2068 goto fail3;
2069
2070 /* Init IRQ workqueue before request_irq */
2071 INIT_WORK(&musb->irq_work, musb_irq_work);
2072
2073 /* attach to the IRQ */
2074 if (request_irq(nIrq, musb->isr, 0, dev_name(dev), musb)) {
2075 dev_err(dev, "request_irq %d failed!\n", nIrq);
2076 status = -ENODEV;
2077 goto fail3;
2078 }
2079 musb->nIrq = nIrq;
2080 /* FIXME this handles wakeup irqs wrong */
2081 if (enable_irq_wake(nIrq) == 0)
2082 musb->irq_wake = 1;
2083 else
2084 musb->irq_wake = 0;
2085
2086 device_init_wakeup(dev, 1);
2087
2088 /* host side needs more setup */
2089 if (is_host_enabled(musb)) {
2090 struct usb_hcd *hcd = musb_to_hcd(musb);
2091
2092 otg_set_host(musb->xceiv, &hcd->self);
2093
2094 if (is_otg_enabled(musb))
2095 hcd->self.otg_port = 1;
2096 musb->xceiv->host = &hcd->self;
2097 hcd->power_budget = 2 * (plat->power ? : 250);
2098
2099 /* program PHY to use external vBus if required */
2100 if (plat->extvbus) {
2101 u8 busctl = musb_read_ulpi_buscontrol(musb->mregs);
2102 busctl |= MUSB_ULPI_USE_EXTVBUS;
2103 musb_write_ulpi_buscontrol(musb->mregs, busctl);
2104 }
2105 }
2106
2107 /* For the host-only role, we can activate right away.
2108 * (We expect the ID pin to be forcibly grounded!!)
2109 * Otherwise, wait till the gadget driver hooks up.
2110 */
2111 if (!is_otg_enabled(musb) && is_host_enabled(musb)) {
2112 struct usb_hcd *hcd = musb_to_hcd(musb);
2113
2114 MUSB_HST_MODE(musb);
2115 musb->xceiv->default_a = 1;
2116 musb->xceiv->state = OTG_STATE_A_IDLE;
2117
2118 status = usb_add_hcd(musb_to_hcd(musb), -1, 0);
2119 device_set_wakeup_enable(dev, 0);
2120 hcd->self.uses_pio_for_control = 1;
2121 dev_dbg(musb->controller, "%s mode, status %d, devctl %02x %c\n",
2122 "HOST", status,
2123 musb_readb(musb->mregs, MUSB_DEVCTL),
2124 (musb_readb(musb->mregs, MUSB_DEVCTL)
2125 & MUSB_DEVCTL_BDEVICE
2126 ? 'B' : 'A'));
2127
2128 } else /* peripheral is enabled */ {
2129 MUSB_DEV_MODE(musb);
2130 musb->xceiv->default_a = 0;
2131 musb->xceiv->state = OTG_STATE_B_IDLE;
2132
2133 status = musb_gadget_setup(musb);
2134 device_set_wakeup_enable(dev, 0);
2135
2136 dev_dbg(musb->controller, "%s mode, status %d, dev%02x\n",
2137 is_otg_enabled(musb) ? "OTG" : "PERIPHERAL",
2138 status,
2139 musb_readb(musb->mregs, MUSB_DEVCTL));
2140
2141 }
2142 if (status < 0)
2143 goto fail3;
2144
2145 status = musb_init_debugfs(musb);
2146 if (status < 0)
2147 goto fail4;
2148
2149 #ifdef CONFIG_SYSFS
2150 status = sysfs_create_group(&musb->controller->kobj, &musb_attr_group);
2151 if (status)
2152 goto fail5;
2153 #endif
2154
2155 dev_info(dev, "USB %s mode controller at %p using %s, IRQ %d\n",
2156 ({char *s;
2157 switch (musb->board_mode) {
2158 case MUSB_HOST: s = "Host"; break;
2159 case MUSB_PERIPHERAL: s = "Peripheral"; break;
2160 default: s = "OTG"; break;
2161 }; s; }),
2162 ctrl,
2163 (is_dma_capable() && musb->dma_controller)
2164 ? "DMA" : "PIO",
2165 musb->nIrq);
2166
2167 if (status == 0) {
2168 u8 drvbuf[19];
2169 sprintf(drvbuf, "driver/musb_hdrc.%d", musb->id);
2170 musb_debug_create(drvbuf, musb);
2171 }
2172
2173 if (is_host_enabled(musb)) {
2174 musb->gb_queue = create_singlethread_workqueue(dev_name(dev));
2175 if (musb->gb_queue == NULL)
2176 goto fail6;
2177 /* Init giveback workqueue */
2178 INIT_WORK(&musb->gb_work, musb_gb_work);
2179 }
2180
2181 /* setup otg_timer */
2182 if (is_otg_enabled(musb))
2183 setup_timer(&musb->otg_timer, musb_otg_timer_func,
2184 (unsigned long) musb);
2185 return 0;
2186
2187 fail6:
2188 if (is_host_enabled(musb))
2189 destroy_workqueue(musb->gb_queue);
2190
2191 fail5:
2192 musb_exit_debugfs(musb);
2193
2194 fail4:
2195 if (!is_otg_enabled(musb) && is_host_enabled(musb))
2196 usb_remove_hcd(musb_to_hcd(musb));
2197 else
2198 musb_gadget_cleanup(musb);
2199
2200 fail3:
2201 if (musb->irq_wake)
2202 device_init_wakeup(dev, 0);
2203 musb_platform_exit(musb);
2204
2205 fail1:
2206 dev_err(musb->controller,
2207 "musb_init_controller failed with status %d\n", status);
2208
2209 musb_free(musb);
2210
2211 fail0:
2212
2213 return status;
2214
2215 }
1797行:allocate_instance
1864
1865 static struct musb *__devinit
1866 allocate_instance(struct device *dev,
1867 struct musb_hdrc_config *config, void __iomem *mbase)
1868 {
1869 struct musb *musb;
1870 struct musb_hw_ep *ep;
1871 int epnum;
1872 struct usb_hcd *hcd;
1873 struct musb_hdrc_platform_data *plat = dev->platform_data;
1874
1875 if (plat->mode != MUSB_PERIPHERAL) {
1876 hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
1877 if (!hcd)
1878 return NULL;
1879 /* usbcore sets dev->driver_data to hcd, and sometimes uses
1880 * that...
1881 */
1882
1883 musb = hcd_to_musb(hcd);
1884 INIT_LIST_HEAD(&musb->control);
1885 INIT_LIST_HEAD(&musb->in_bulk);
1886 INIT_LIST_HEAD(&musb->out_bulk);
1887 INIT_LIST_HEAD(&musb->gb_list);
1888
1889 hcd->uses_new_polling = 1;
1890 hcd->has_tt = 1;
1891 musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
1892 } else {
1893 musb = kzalloc(sizeof *musb, GFP_KERNEL);
1894 if (!musb)
1895 return NULL;
1896 }
1897 dev_set_drvdata(dev, musb);
1898 musb->mregs = mbase;
1899 musb->ctrl_base = mbase;
1900 musb->nIrq = -ENODEV;
1901 musb->config = config;
1902 BUG_ON(musb->config->num_eps > MUSB_C_NUM_EPS);
1903 for (epnum = 0, ep = musb->endpoints;
1904 epnum < musb->config->num_eps;
1905 epnum++, ep++) {
1906 ep->musb = musb;
1907 ep->epnum = epnum;
1908 }
1909
1910 musb->controller = dev;
1911
1912 return musb;
1913 }
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:
2280 /**
2281 * usb_create_hcd - create and initialize an HCD structure
2282 * @driver: HC driver that will use this hcd
2283 * @dev: device for this HC, stored in hcd->self.controller
2284 * @bus_name: value to store in hcd->self.bus_name
2285 * Context: !in_interrupt()
2286 *
2287 * Allocate a struct usb_hcd, with extra space at the end for the
2288 * HC driver's private data. Initialize the generic members of the
2289 * hcd structure.
2290 *
2291 * If memory is unavailable, returns NULL.
2292 */
2293 struct usb_hcd *usb_create_hcd(const struct hc_driver *driver,
2294 struct device *dev, const char *bus_name)
2295 {
2296 return usb_create_shared_hcd(driver, dev, bus_name, NULL);
2297 }
2298 EXPORT_SYMBOL_GPL(usb_create_hcd);
2296行:
2213
2214 /**
2215 * usb_create_shared_hcd - create and initialize an HCD structure
2216 * @driver: HC driver that will use this hcd
2217 * @dev: device for this HC, stored in hcd->self.controller
2218 * @bus_name: value to store in hcd->self.bus_name
2219 * @primary_hcd: a pointer to the usb_hcd structure that is sharing the
2220 * PCI device. Only allocate certain resources for the primary HCD
2221 * Context: !in_interrupt()
2222 *
2223 * Allocate a struct usb_hcd, with extra space at the end for the
2224 * HC driver's private data. Initialize the generic members of the
2225 * hcd structure.
2226 *
2227 * If memory is unavailable, returns NULL.
2228 */
2229 struct usb_hcd *usb_create_shared_hcd(const struct hc_driver *driver,
2230 struct device *dev, const char *bus_name,
2231 struct usb_hcd *primary_hcd)
2232 {
2233 struct usb_hcd *hcd;
2234
2235 hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
2236 if (!hcd) {
2237 dev_dbg (dev, "hcd alloc failed\n");
2238 return NULL;
2239 }
2240 if (primary_hcd == NULL) {
2241 hcd->bandwidth_mutex = kmalloc(sizeof(*hcd->bandwidth_mutex),
2242 GFP_KERNEL);
2243 if (!hcd->bandwidth_mutex) {
2244 kfree(hcd);
2245 dev_dbg(dev, "hcd bandwidth mutex alloc failed\n");
2246 return NULL;
2247 }
2248 mutex_init(hcd->bandwidth_mutex);
2249 dev_set_drvdata(dev, hcd);
2250 } else {
2251 hcd->bandwidth_mutex = primary_hcd->bandwidth_mutex;
2252 hcd->primary_hcd = primary_hcd;
2253 primary_hcd->primary_hcd = primary_hcd;
2254 hcd->shared_hcd = primary_hcd;
2255 primary_hcd->shared_hcd = hcd;
2256 }
2257
2258 kref_init(&hcd->kref);
2259
2260 usb_bus_init(&hcd->self);
2261 hcd->self.controller = dev;
2262 hcd->self.bus_name = bus_name;
2263 hcd->self.uses_dma = (dev->dma_mask != NULL);
2264
2265 init_timer(&hcd->rh_timer);
2266 hcd->rh_timer.function = rh_timer_func;
2267 hcd->rh_timer.data = (unsigned long) hcd;
2268 #ifdef CONFIG_USB_SUSPEND
2269 INIT_WORK(&hcd->wakeup_work, hcd_resume_work);
2270 #endif
2271
2272 hcd->driver = driver;
2273 hcd->speed = driver->flags & HCD_MASK;
2274 hcd->product_desc = (driver->product_desc) ? driver->product_desc :
2275 "USB Host Controller";
2276 return hcd;
2277 }
2278 EXPORT_SYMBOL_GPL(usb_create_shared_hcd);
第一个参数 struct hc_driver,这个结构体掀开了我们对 usb host controller driver 的认识,它来自 drivers/usb/core/hcd.h:
202
203 struct hc_driver {
204 const char *description; /* "ehci-hcd" etc */
205 const char *product_desc; /* product/vendor string */
206 size_t hcd_priv_size; /* size of private data */
207
208 /* irq handler */
209 irqreturn_t (*irq) (struct usb_hcd *hcd);
210
211 int flags;
212 #define HCD_MEMORY 0x0001 /* HC regs use memory (else I/O) */
213 #define HCD_LOCAL_MEM 0x0002 /* HC needs local memory */
214 #define HCD_SHARED 0x0004 /* Two (or more) usb_hcds share HW */
215 #define HCD_USB11 0x0010 /* USB 1.1 */
216 #define HCD_USB2 0x0020 /* USB 2.0 */
217 #define HCD_USB3 0x0040 /* USB 3.0 */
218 #define HCD_MASK 0x0070
219
220 /* called to init HCD and root hub */
221 int (*reset) (struct usb_hcd *hcd);
222 int (*start) (struct usb_hcd *hcd);
223
224 /* NOTE: these suspend/resume calls relate to the HC as
225 * a whole, not just the root hub; they're for PCI bus glue.
226 */
227 /* called after suspending the hub, before entering D3 etc */
228 int (*pci_suspend)(struct usb_hcd *hcd, bool do_wakeup);
229
230 /* called after entering D0 (etc), before resuming the hub */
231 int (*pci_resume)(struct usb_hcd *hcd, bool hibernated);
232
233 /* cleanly make HCD stop writing memory and doing I/O */
234 void (*stop) (struct usb_hcd *hcd);
235
236 /* shutdown HCD */
237 void (*shutdown) (struct usb_hcd *hcd);
238
239 /* return current frame number */
240 int (*get_frame_number) (struct usb_hcd *hcd);
241
242 /* manage i/o requests, device state */
243 int (*urb_enqueue)(struct usb_hcd *hcd,
244 struct urb *urb, gfp_t mem_flags);
245 int (*urb_dequeue)(struct usb_hcd *hcd,
246 struct urb *urb, int status);
247
248 /*
249 * (optional) these hooks allow an HCD to override the default DMA
250 * mapping and unmapping routines. In general, they shouldn't be
251 * necessary unless the host controller has special DMA requirements,
252 * such as alignment contraints. If these are not specified, the
253 * general usb_hcd_(un)?map_urb_for_dma functions will be used instead
254 * (and it may be a good idea to call these functions in your HCD
255 * implementation)
256 */
257 int (*map_urb_for_dma)(struct usb_hcd *hcd, struct urb *urb,
258 gfp_t mem_flags);
259 void (*unmap_urb_for_dma)(struct usb_hcd *hcd, struct urb *urb);
260
261 /* hw synch, freeing endpoint resources that urb_dequeue can't */
262 void (*endpoint_disable)(struct usb_hcd *hcd,
263 struct usb_host_endpoint *ep);
264
265 /* (optional) reset any endpoint state such as sequence number
266 and current window */
267 void (*endpoint_reset)(struct usb_hcd *hcd,
268 struct usb_host_endpoint *ep);
269
270 /* root hub support */
271 int (*hub_status_data) (struct usb_hcd *hcd, char *buf);
272 int (*hub_control) (struct usb_hcd *hcd,
273 u16 typeReq, u16 wValue, u16 wIndex,
274 char *buf, u16 wLength);
275 int (*bus_suspend)(struct usb_hcd *);
276 int (*bus_resume)(struct usb_hcd *);
277 int (*start_port_reset)(struct usb_hcd *, unsigned port_num);
278
279 /* force handover of high-speed port to full-speed companion */
280 void (*relinquish_port)(struct usb_hcd *, int);
281 /* has a port been handed over to a companion? */
282 int (*port_handed_over)(struct usb_hcd *, int);
283
284 /* CLEAR_TT_BUFFER completion callback */
285 void (*clear_tt_buffer_complete)(struct usb_hcd *,
286 struct usb_host_endpoint *);
287
288 /* xHCI specific functions */
289 /* Called by usb_alloc_dev to alloc HC device structures */
290 int (*alloc_dev)(struct usb_hcd *, struct usb_device *);
291 /* Called by usb_disconnect to free HC device structures */
292 void (*free_dev)(struct usb_hcd *, struct usb_device *);
293 /* Change a group of bulk endpoints to support multiple stream IDs */
294 int (*alloc_streams)(struct usb_hcd *hcd, struct usb_device *udev,
295 struct usb_host_endpoint **eps, unsigned int num_eps,
296 unsigned int num_streams, gfp_t mem_flags);
297 /* Reverts a group of bulk endpoints back to not using stream IDs.
298 * Can fail if we run out of memory.
299 */
300 int (*free_streams)(struct usb_hcd *hcd, struct usb_device *udev,
301 struct usb_host_endpoint **eps, unsigned int num_eps,
302 gfp_t mem_flags);
303
304 /* Bandwidth computation functions */
305 /* Note that add_endpoint() can only be called once per endpoint before
306 * check_bandwidth() or reset_bandwidth() must be called.
307 * drop_endpoint() can only be called once per endpoint also.
308 * A call to xhci_drop_endpoint() followed by a call to
309 * xhci_add_endpoint() will add the endpoint to the schedule with
310 * possibly new parameters denoted by a different endpoint descriptor
311 * in usb_host_endpoint. A call to xhci_add_endpoint() followed by a
312 * call to xhci_drop_endpoint() is not allowed.
313 */
314 /* Allocate endpoint resources and add them to a new schedule */
315 int (*add_endpoint)(struct usb_hcd *, struct usb_device *,
316 struct usb_host_endpoint *);
317 /* Drop an endpoint from a new schedule */
318 int (*drop_endpoint)(struct usb_hcd *, struct usb_device *,
319 struct usb_host_endpoint *);
320 /* Check that a new hardware configuration, set using
321 * endpoint_enable and endpoint_disable, does not exceed bus
322 * bandwidth. This must be called before any set configuration
323 * or set interface requests are sent to the device.
324 */
325 int (*check_bandwidth)(struct usb_hcd *, struct usb_device *);
326 /* Reset the device schedule to the last known good schedule,
327 * which was set from a previous successful call to
328 * check_bandwidth(). This reverts any add_endpoint() and
329 * drop_endpoint() calls since that last successful call.
330 * Used for when a check_bandwidth() call fails due to resource
331 * or bandwidth constraints.
332 */
333 void (*reset_bandwidth)(struct usb_hcd *, struct usb_device *);
334 /* Returns the hardware-chosen device address */
335 int (*address_device)(struct usb_hcd *, struct usb_device *udev);
336 /* Notifies the HCD after a hub descriptor is fetched.
337 * Will block.
338 */
339 int (*update_hub_device)(struct usb_hcd *, struct usb_device *hdev,
340 struct usb_tt *tt, gfp_t mem_flags);
341 int (*reset_device)(struct usb_hcd *, struct usb_device *);
342 /* Notifies the HCD after a device is connected and its
343 * address is set
344 */
345 int (*update_device)(struct usb_hcd *, struct usb_device *);
346 int (*set_usb2_hw_lpm)(struct usb_hcd *, struct usb_device *, int);
347 };
每个 hcd 都得对应这么一个结构体变量.比如咱们的MUSB HDRC,drivers\usb\musb\musb_host.c中就有这么一段:
2496 const struct hc_driver musb_hc_driver = {
2497 .description = "musb-hcd",
2498 .product_desc = "MUSB HDRC host driver",
2499 .hcd_priv_size = sizeof(struct musb),
2500 .flags = HCD_USB2 | HCD_MEMORY,
2501
2502 /* not using irq handler or reset hooks from usbcore, since
2503 * those must be shared with peripheral code for OTG configs
2504 */
2505
2506 .start = musb_h_start,
2507 .stop = musb_h_stop,
2508
2509 .get_frame_number = musb_h_get_frame_number,
2510
2511 .urb_enqueue = musb_urb_enqueue,
2512 .urb_dequeue = musb_urb_dequeue,
2513 .endpoint_disable = musb_h_disable,
2514
2515 .hub_status_data = musb_hub_status_data,
2516 .hub_control = musb_hub_control,
2517 .bus_suspend = musb_bus_suspend,
2518 .bus_resume = musb_bus_resume,
2519 /* .start_port_reset = NULL, */
2520 /* .hub_irq_enable = NULL, */
2521 };
继续看,2233行,一个变态的数据结构还不够,还得来一个更变态的.struct usb_hcd,这意思很
明确,有一个 hcd 就得有这么一个结构体,也是来自 drivers/usb/core/hcd.h:
68 /*-------------------------------------------------------------------------*/
69
70 struct usb_hcd {
71
72 /*
73 * housekeeping
74 */
75 struct usb_bus self; /* hcd is-a bus */
76 struct kref kref; /* reference counter */
77
78 const char *product_desc; /* product/vendor string */
79 int speed; /* Speed for this roothub.
80 * May be different from
81 * hcd->driver->flags & HCD_MASK
82 */
83 char irq_descr[24]; /* driver + bus # */
84
85 struct timer_list rh_timer; /* drives root-hub polling */
86 struct urb *status_urb; /* the current status urb */
87 #ifdef CONFIG_USB_SUSPEND
88 struct work_struct wakeup_work; /* for remote wakeup */
89 #endif
90
91 /*
92 * hardware info/state
93 */
94 const struct hc_driver *driver; /* hw-specific hooks */
95
96 /* Flags that need to be manipulated atomically because they can
97 * change while the host controller is running. Always use
98 * set_bit() or clear_bit() to change their values.
99 */
100 unsigned long flags;
101 #define HCD_FLAG_HW_ACCESSIBLE 0 /* at full power */
102 #define HCD_FLAG_SAW_IRQ 1
103 #define HCD_FLAG_POLL_RH 2 /* poll for rh status? */
104 #define HCD_FLAG_POLL_PENDING 3 /* status has changed? */
105 #define HCD_FLAG_WAKEUP_PENDING 4 /* root hub is resuming? */
106 #define HCD_FLAG_RH_RUNNING 5 /* root hub is running? */
107 #define HCD_FLAG_DEAD 6 /* controller has died? */
108
109 /* The flags can be tested using these macros; they are likely to
110 * be slightly faster than test_bit().
111 */
112 #define HCD_HW_ACCESSIBLE(hcd) ((hcd)->flags & (1U << HCD_FLAG_HW_ACCESSIBLE))
113 #define HCD_SAW_IRQ(hcd) ((hcd)->flags & (1U << HCD_FLAG_SAW_IRQ))
114 #define HCD_POLL_RH(hcd) ((hcd)->flags & (1U << HCD_FLAG_POLL_RH))
115 #define HCD_POLL_PENDING(hcd) ((hcd)->flags & (1U << HCD_FLAG_POLL_PENDING))
116 #define HCD_WAKEUP_PENDING(hcd) ((hcd)->flags & (1U << HCD_FLAG_WAKEUP_PENDING))
117 #define HCD_RH_RUNNING(hcd) ((hcd)->flags & (1U << HCD_FLAG_RH_RUNNING))
118 #define HCD_DEAD(hcd) ((hcd)->flags & (1U << HCD_FLAG_DEAD))
119
120 /* Flags that get set only during HCD registration or removal. */
121 unsigned rh_registered:1;/* is root hub registered? */
122 unsigned rh_pollable:1; /* may we poll the root hub? */
123 unsigned msix_enabled:1; /* driver has MSI-X enabled? */
124
125 /* The next flag is a stopgap, to be removed when all the HCDs
126 * support the new root-hub polling mechanism. */
127 unsigned uses_new_polling:1;
128 unsigned wireless:1; /* Wireless USB HCD */
129 unsigned authorized_default:1;
130 unsigned has_tt:1; /* Integrated TT in root hub */
131
132 int irq; /* irq allocated */
133 void __iomem *regs; /* device memory/io */
134 u64 rsrc_start; /* memory/io resource start */
135 u64 rsrc_len; /* memory/io resource length */
136 unsigned power_budget; /* in mA, 0 = no limit */
137
138 /* bandwidth_mutex should be taken before adding or removing
139 * any new bus bandwidth constraints:
140 * 1. Before adding a configuration for a new device.
141 * 2. Before removing the configuration to put the device into
142 * the addressed state.
143 * 3. Before selecting a different configuration.
144 * 4. Before selecting an alternate interface setting.
145 *
146 * bandwidth_mutex should be dropped after a successful control message
147 * to the device, or resetting the bandwidth after a failed attempt.
148 */
149 struct mutex *bandwidth_mutex;
150 struct usb_hcd *shared_hcd;
151 struct usb_hcd *primary_hcd;
152
153
154 #define HCD_BUFFER_POOLS 4
155 struct dma_pool *pool[HCD_BUFFER_POOLS];
156
157 int state;
158 # define __ACTIVE 0x01
159 # define __SUSPEND 0x04
160 # define __TRANSIENT 0x80
161
162 # define HC_STATE_HALT 0
163 # define HC_STATE_RUNNING (__ACTIVE)
164 # define HC_STATE_QUIESCING (__SUSPEND|__TRANSIENT|__ACTIVE)
165 # define HC_STATE_RESUMING (__SUSPEND|__TRANSIENT)
166 # define HC_STATE_SUSPENDED (__SUSPEND)
167
168 #define HC_IS_RUNNING(state) ((state) & __ACTIVE)
169 #define HC_IS_SUSPENDED(state) ((state) & __SUSPEND)
170
171 /* more shared queuing code would be good; it should support
172 * smarter scheduling, handle transaction translators, etc;
173 * input size of periodic table to an interrupt scheduler.
174 * (ohci 32, uhci 1024, ehci 256/512/1024).
175 */
176
177 /* The HC driver's private data is stored at the end of
178 * this structure.
179 */
180 unsigned long hcd_priv[0]
181 __attribute__ ((aligned(sizeof(s64))));
182 };
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,
320 * Allocated per bus (tree of devices) we have:
321 */
322 struct usb_bus {
323 struct device *controller; /* host/master side hardware */
324 int busnum; /* Bus number (in order of reg) */
325 const char *bus_name; /* stable id (PCI slot_name etc) */
326 u8 uses_dma; /* Does the host controller use DMA? */
327 u8 uses_pio_for_control; /*
328 * Does the host controller use PIO
329 * for control transfers?
330 */
331 u8 otg_port; /* 0, or number of OTG/HNP port */
332 unsigned is_b_host:1; /* true during some HNP roleswitches */
333 unsigned b_hnp_enable:1; /* OTG: did A-Host enable HNP? */
334 unsigned sg_tablesize; /* 0 or largest number of sg list entries */
335
336 int devnum_next; /* Next open device number in
337 * round-robin allocation */
338
339 struct usb_devmap devmap; /* device address allocation map */
340 struct usb_device *root_hub; /* Root hub */
341 struct usb_bus *hs_companion; /* Companion EHCI bus, if any */
342 struct list_head bus_list; /* list of busses */
343
344 int bandwidth_allocated; /* on this bus: how much of the time
345 * reserved for periodic (intr/iso)
346 * requests is used, on average?
347 * Units: microseconds/frame.
348 * Limits: Full/low speed reserve 90%,
349 * while high speed reserves 80%.
350 */
351 int bandwidth_int_reqs; /* number of Interrupt requests */
352 int bandwidth_isoc_reqs; /* number of Isoc. requests */
353
354 #ifdef CONFIG_USB_DEVICEFS
355 struct dentry *usbfs_dentry; /* usbfs dentry entry for the bus */
356 #endif
357
358 #if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE)
359 struct mon_bus *mon_bus; /* non-null when associated */
360 int monitored; /* non-zero when monitored */
361 #endif
362 };
usb_bus_init 来自drivers/usb/core/hcd.c,很显然,它就是初始化struct usb_bus结构体指
针.而这个结构体变量 hcd->self 的内存已经在刚才为 hcd 申请内存的时候一并申请了.
878 /*-------------------------------------------------------------------------*/
879
880 /**
881 * usb_bus_init - shared initialization code
882 * @bus: the bus structure being initialized
883 *
884 * This code is used to initialize a usb_bus structure, memory for which is
885 * separately managed.
886 */
887 static void usb_bus_init (struct usb_bus *bus)
888 {
889 memset (&bus->devmap, 0, sizeof(struct usb_devmap));
890
891 bus->devnum_next = 1;
892
893 bus->root_hub = NULL;
894 bus->busnum = -1;
895 bus->bandwidth_allocated = 0;
896 bus->bandwidth_int_reqs = 0;
897 bus->bandwidth_isoc_reqs = 0;
898
899 INIT_LIST_HEAD (&bus->bus_list);
900 }
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
static inline int musb_platform_init(struct musb *musb)
{
if (!musb->ops->init)
return -EINVAL;
return musb->ops->init(musb);
}
这里运行ti81xx_musb_init初始化,咱们现在先略过
2040~2053行:dma的操作
主要运行cppi41_dma_controller_create
1597 /**
1598 * cppi41_dma_controller_create -
1599 * instantiate an object representing DMA controller.
1600 */
1601 struct dma_controller * __devinit
1602 cppi41_dma_controller_create(struct musb *musb, void __iomem *mregs)
1603 {
1604 struct cppi41 *cppi;
1605
1606 cppi = kzalloc(sizeof *cppi, GFP_KERNEL);
1607 if (!cppi)
1608 return NULL;
1609
1610 /* Initialize the CPPI 4.1 DMA controller structure */
1611 cppi->musb = musb;
1612 cppi->controller.start = cppi41_controller_start;
1613 cppi->controller.stop = cppi41_controller_stop;
1614 cppi->controller.channel_alloc = cppi41_channel_alloc;
1615 cppi->controller.channel_release = cppi41_channel_release;
1616 cppi->controller.channel_program = cppi41_channel_program;
1617 cppi->controller.channel_abort = cppi41_channel_abort;
1618 cppi->cppi_info = (struct usb_cppi41_info *)&usb_cppi41_info[musb->id];;
1619 cppi->en_bd_intr = cppi->cppi_info->bd_intr_ctrl;
1620 cppi->txfifo_intr_enable = musb->txfifo_intr_enable;
1621 cppi->tx_isoc_sched_enable = musb->tx_isoc_sched_enable;
1622 INIT_WORK(&cppi->txdma_work, txdma_completion_work);
1623 INIT_WORK(&cppi->rxdma_work, rxdma_completion_work);
1624
1625 /*
1626 * Extra IN token has been seen when a file is transferred from one MSC
1627 * device to other due to xDMA IP bug when multiple masters access
1628 * mentor controller register space.
1629 * As a software workaround use transparent mode and correct data toggle
1630 * when they go wrong.
1631 * This issue is expected to be fixed in RTL version post 0xD.
1632 * Since RTL version is not available to distinguish the fix, based on
1633 * soc revision the rxdma generic rndis shall be enabled/disabled by
1634 * platform driver as mentioned below
1635 * set cppi_info->grndis_for_host_rx = 1 to enable
1636 * rxdma generic rndis mode
1637 * set cppi_info->grndis_for_host_rx = 0 and
1638 * cppi->musb->datatog_fix = 0 to disable the rxdma generic rndis.
1639 */
1640 if (cppi->cppi_info->rx_dma_mode == USB_GENERIC_RNDIS_MODE)
1641 cppi->musb->datatog_fix = 0;
1642 else
1643 cppi->musb->datatog_fix = 1;
1644 dev_dbg(musb->controller, "musb%d: %s cppi41 rxdma mode\n",
1645 musb->id, cppi->cppi_info->rx_dma_mode ? "generic rndis" :
1646 "transparent");
1647
1648 /* enable infinite mode only for ti81xx silicon rev2 */
1649 if (cpu_is_am33xx() || cpu_is_ti816x()) {
1650 /*
1651 * to enable inf_mode, generic rndis mode must be
1652 * enabled. also datatog_fix must be set to zero
1653 */
1654 cppi->cppi_info->rx_inf_mode = 0;
1655 dev_dbg(musb->controller, "cppi41dma supports infinite mode\n");
1656 }
1657
1658 return &cppi->controller;
1659 }
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 中:
2384 /**
2385 * usb_add_hcd - finish generic HCD structure initialization and register
2386 * @hcd: the usb_hcd structure to initialize
2387 * @irqnum: Interrupt line to allocate
2388 * @irqflags: Interrupt type flags
2389 *
2390 * Finish the remaining parts of generic HCD initialization: allocate the
2391 * buffers of consistent memory, register the bus, request the IRQ line,
2392 * and call the driver's reset() and start() routines.
2393 */
2394 int usb_add_hcd(struct usb_hcd *hcd,
2395 unsigned int irqnum, unsigned long irqflags)
2396 {
2397 int retval;
2398 struct usb_device *rhdev;
2399
2400 dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
2401
2402 /* Keep old behaviour if authorized_default is not in [0, 1]. */
2403 if (authorized_default < 0 || authorized_default > 1)
2404 hcd->authorized_default = hcd->wireless? 0 : 1;
2405 else
2406 hcd->authorized_default = authorized_default;
2407 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2408
2409 /* HC is in reset state, but accessible. Now do the one-time init,
2410 * bottom up so that hcds can customize the root hubs before khubd
2411 * starts talking to them. (Note, bus id is assigned early too.)
2412 */
2413 if ((retval = hcd_buffer_create(hcd)) != 0) {
2414 dev_dbg(hcd->self.controller, "pool alloc failed\n");
2415 return retval;
2416 }
2417
2418 if ((retval = usb_register_bus(&hcd->self)) < 0)
2419 goto err_register_bus;
2420
2421 if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) {
2422 dev_err(hcd->self.controller, "unable to allocate root hub\n");
2423 retval = -ENOMEM;
2424 goto err_allocate_root_hub;
2425 }
2426 hcd->self.root_hub = rhdev;
2427
2428 switch (hcd->speed) {
2429 case HCD_USB11:
2430 rhdev->speed = USB_SPEED_FULL;
2431 break;
2432 case HCD_USB2:
2433 rhdev->speed = USB_SPEED_HIGH;
2434 break;
2435 case HCD_USB3:
2436 rhdev->speed = USB_SPEED_SUPER;
2437 break;
2438 default:
2439 retval = -EINVAL;
2440 goto err_set_rh_speed;
2441 }
2442
2443 /* wakeup flag init defaults to "everything works" for root hubs,
2444 * but drivers can override it in reset() if needed, along with
2445 * recording the overall controller's system wakeup capability.
2446 */
2447 device_set_wakeup_capable(&rhdev->dev, 1);
2448
2449 /* HCD_FLAG_RH_RUNNING doesn't matter until the root hub is
2450 * registered. But since the controller can die at any time,
2451 * let's initialize the flag before touching the hardware.
2452 */
2453 set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2454
2455 /* "reset" is misnamed; its role is now one-time init. the controller
2456 * should already have been reset (and boot firmware kicked off etc).
2457 */
2458 if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) {
2459 dev_err(hcd->self.controller, "can't setup\n");
2460 goto err_hcd_driver_setup;
2461 }
2462 hcd->rh_pollable = 1;
2463
2464 /* NOTE: root hub and controller capabilities may not be the same */
2465 if (device_can_wakeup(hcd->self.controller)
2466 && device_can_wakeup(&hcd->self.root_hub->dev))
2467 dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
2468
2469 /* enable irqs just before we start the controller */
2470 if (usb_hcd_is_primary_hcd(hcd)) {
2471 retval = usb_hcd_request_irqs(hcd, irqnum, irqflags);
2472 if (retval)
2473 goto err_request_irq;
2474 }
2475
2476 hcd->state = HC_STATE_RUNNING;
2477 retval = hcd->driver->start(hcd);
2478 if (retval < 0) {
2479 dev_err(hcd->self.controller, "startup error %d\n", retval);
2480 goto err_hcd_driver_start;
2481 }
2482
2483 /* starting here, usbcore will pay attention to this root hub */
2484 rhdev->bus_mA = min(500u, hcd->power_budget);
2485 if ((retval = register_root_hub(hcd)) != 0)
2486 goto err_register_root_hub;
2487
2488 retval = sysfs_create_group(&rhdev->dev.kobj, &usb_bus_attr_group);
2489 if (retval < 0) {
2490 printk(KERN_ERR "Cannot register USB bus sysfs attributes: %d\n",
2491 retval);
2492 goto error_create_attr_group;
2493 }
2494 if (hcd->uses_new_polling && HCD_POLL_RH(hcd))
2495 usb_hcd_poll_rh_status(hcd);
2496
2497 /*
2498 * Host controllers don't generate their own wakeup requests;
2499 * they only forward requests from the root hub. Therefore
2500 * controllers should always be enabled for remote wakeup.
2501 */
2502 device_wakeup_enable(hcd->self.controller);
2503 return retval;
2504
2505 error_create_attr_group:
2506 clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2507 if (HC_IS_RUNNING(hcd->state))
2508 hcd->state = HC_STATE_QUIESCING;
2509 spin_lock_irq(&hcd_root_hub_lock);
2510 hcd->rh_registered = 0;
2511 spin_unlock_irq(&hcd_root_hub_lock);
2512
2513 #ifdef CONFIG_USB_SUSPEND
2514 cancel_work_sync(&hcd->wakeup_work);
2515 #endif
2516 mutex_lock(&usb_bus_list_lock);
2517 usb_disconnect(&rhdev); /* Sets rhdev to NULL */
2518 mutex_unlock(&usb_bus_list_lock);
2519 err_register_root_hub:
2520 hcd->rh_pollable = 0;
2521 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2522 del_timer_sync(&hcd->rh_timer);
2523 hcd->driver->stop(hcd);
2524 hcd->state = HC_STATE_HALT;
2525 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2526 del_timer_sync(&hcd->rh_timer);
2527 err_hcd_driver_start:
2528 if (usb_hcd_is_primary_hcd(hcd) && hcd->irq >= 0)
2529 free_irq(irqnum, hcd);
2530 err_request_irq:
2531 err_hcd_driver_setup:
2532 err_set_rh_speed:
2533 usb_put_dev(hcd->self.root_hub);
2534 err_allocate_root_hub:
2535 usb_deregister_bus(&hcd->self);
2536 err_register_bus:
2537 hcd_buffer_destroy(hcd);
2538 return retval;
2539 }
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,即直接内存访问.
36
37 /* SETUP primitives */
38
39 /**
40 * hcd_buffer_create - initialize buffer pools
41 * @hcd: the bus whose buffer pools are to be initialized
42 * Context: !in_interrupt()
43 *
44 * Call this as part of initializing a host controller that uses the dma
45 * memory allocators. It initializes some pools of dma-coherent memory that
46 * will be shared by all drivers using that controller, or returns a negative
47 * errno value on error.
48 *
49 * Call hcd_buffer_destroy() to clean up after using those pools.
50 */
51 int hcd_buffer_create(struct usb_hcd *hcd)
52 {
53 char name[16];
54 int i, size;
55
56 if (!hcd->self.controller->dma_mask &&
57 !(hcd->driver->flags & HCD_LOCAL_MEM))
58 return 0;
59
60 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
61 size = pool_max[i];
62 if (!size)
63 continue;
64 snprintf(name, sizeof name, "buffer-%d", size);
65 hcd->pool[i] = dma_pool_create(name, hcd->self.controller,
66 size, size, 0);
67 if (!hcd->pool[i]) {
68 hcd_buffer_destroy(hcd);
69 return -ENOMEM;
70 }
71 }
72 return 0;
73 }
74
看 64 行,调用了 dma_pool_create 函数,这个函数就是真正去创建内存池
的函数,或者更准确地讲,创建一个DMA池,内核中定义了一个结构体,struct dma_pool,如果创建失败就调用
hcd_buffer_destroy,还是来自同一个文件
75
76 /**
77 * hcd_buffer_destroy - deallocate buffer pools
78 * @hcd: the bus whose buffer pools are to be destroyed
79 * Context: !in_interrupt()
80 *
81 * This frees the buffer pools created by hcd_buffer_create().
82 */
83 void hcd_buffer_destroy(struct usb_hcd *hcd)
84 {
85 int i;
86
87 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
88 struct dma_pool *pool = hcd->pool[i];
89 if (pool) {
90 dma_pool_destroy(pool);
91 hcd->pool[i] = NULL;
92 }
93 }
94 }
95
看得出这里调用的是 dma_pool_destroy,其作用不言自明.
那么创建池子和销毁池子的函数我们知道了,如何从池子里索取或者把索取的释放回去呢?对应
的两个函数分别是,dma_pool_alloc 和 dma_pool_free,而这两个函数正是与我们说的
usb_buffer_alloc 以及 usb_buffer_free 相联系的.于是我们来看这两个函数的代码,来自
drivers/usb/core/usb.c:
96
97 /* sometimes alloc/free could use kmalloc with GFP_DMA, for
98 * better sharing and to leverage mm/slab.c intelligence.
99 */
100
101 void *hcd_buffer_alloc(
102 struct usb_bus *bus,
103 size_t size,
104 gfp_t mem_flags,
105 dma_addr_t *dma
106 )
107 {
108 struct usb_hcd *hcd = bus_to_hcd(bus);
109 int i;
110
111 /* some USB hosts just use PIO */
112 if (!bus->controller->dma_mask &&
113 !(hcd->driver->flags & HCD_LOCAL_MEM)) {
114 *dma = ~(dma_addr_t) 0;
115 return kmalloc(size, mem_flags);
116 }
117
118 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
119 if (size <= pool_max[i])
120 return dma_pool_alloc(hcd->pool[i], mem_flags, dma);
121 }
122 return dma_alloc_coherent(hcd->self.controller, size, dma, mem_flags);
123 }
124
125 void hcd_buffer_free(
126 struct usb_bus *bus,
127 size_t size,
128 void *addr,
129 dma_addr_t dma
130 )
131 {
132 struct usb_hcd *hcd = bus_to_hcd(bus);
133 int i;
134
135 if (!addr)
136 return;
137
138 if (!bus->controller->dma_mask &&
139 !(hcd->driver->flags & HCD_LOCAL_MEM)) {
140 kfree(addr);
141 return;
142 }
143
144 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
145 if (size <= pool_max[i]) {
146 dma_pool_free(hcd->pool[i], addr, dma);
147 return;
148 }
149 }
150 dma_free_coherent(hcd->self.controller, size, addr, dma);
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 中定义了一个数组:
24 /* FIXME tune these based on pool statistics ... */
25 static const size_t pool_max[HCD_BUFFER_POOLS] = {
26 /* platforms without dma-friendly caches might need to
27 * prevent cacheline sharing...
28 */
29 32,
30 128,
31 512,
32 PAGE_SIZE / 2
33 /* bigger --> allocate pages */
34 };
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:
902 /*-------------------------------------------------------------------------*/
903
904 /**
905 * usb_register_bus - registers the USB host controller with the usb core
906 * @bus: pointer to the bus to register
907 * Context: !in_interrupt()
908 *
909 * Assigns a bus number, and links the controller into usbcore data
910 * structures so that it can be seen by scanning the bus list.
911 */
912 static int usb_register_bus(struct usb_bus *bus)
913 {
914 int result = -E2BIG;
915 int busnum;
916
917 mutex_lock(&usb_bus_list_lock);
918 busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1);
919 if (busnum >= USB_MAXBUS) {
920 printk (KERN_ERR "%s: too many buses\n", usbcore_name);
921 goto error_find_busnum;
922 }
923 set_bit (busnum, busmap.busmap);
924 bus->busnum = busnum;
925
926 /* Add it to the local list of buses */
927 list_add (&bus->bus_list, &usb_bus_list);
928 mutex_unlock(&usb_bus_list_lock);
929
930 usb_notify_add_bus(bus);
931
932 dev_info (bus->controller, "new USB bus registered, assigned bus "
933 "number %d\n", bus->busnum);
934 return 0;
935
936 error_find_busnum:
937 mutex_unlock(&usb_bus_list_lock);
938 return result;
939 }
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
static int musb_h_start(struct usb_hcd *hcd)
{
struct musb *musb = hcd_to_musb(hcd);
/* NOTE: musb_start() is called when the hub driver turns
* on port power, or when (OTG) peripheral starts.
*/
hcd->state = HC_STATE_RUNNING;
musb->port1_status = 0;
return 0;
}
回到 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.
967 /**
968 * register_root_hub - called by usb_add_hcd() to register a root hub
969 * @hcd: host controller for this root hub
970 *
971 * This function registers the root hub with the USB subsystem. It sets up
972 * the device properly in the device tree and then calls usb_new_device()
973 * to register the usb device. It also assigns the root hub's USB address
974 * (always 1).
975 */
976 static int register_root_hub(struct usb_hcd *hcd)
977 {
978 struct device *parent_dev = hcd->self.controller;
979 struct usb_device *usb_dev = hcd->self.root_hub;
980 const int devnum = 1;
981 int retval;
982
983 usb_dev->devnum = devnum;
984 usb_dev->bus->devnum_next = devnum + 1;
985 memset (&usb_dev->bus->devmap.devicemap, 0,
986 sizeof usb_dev->bus->devmap.devicemap);
987 set_bit (devnum, usb_dev->bus->devmap.devicemap);
988 usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
989
990 mutex_lock(&usb_bus_list_lock);
991
992 usb_dev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
993 retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
994 if (retval != sizeof usb_dev->descriptor) {
995 mutex_unlock(&usb_bus_list_lock);
996 dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
997 dev_name(&usb_dev->dev), retval);
998 return (retval < 0) ? retval : -EMSGSIZE;
999 }
1000
1001 retval = usb_new_device (usb_dev);
1002 if (retval) {
1003 dev_err (parent_dev, "can't register root hub for %s, %d\n",
1004 dev_name(&usb_dev->dev), retval);
1005 }
1006 mutex_unlock(&usb_bus_list_lock);
1007
1008 if (retval == 0) {
1009 spin_lock_irq (&hcd_root_hub_lock);
1010 hcd->rh_registered = 1;
1011 spin_unlock_irq (&hcd_root_hub_lock);
1012
1013 /* Did the HC die before the root hub was registered? */
1014 if (HCD_DEAD(hcd))
1015 usb_hc_died (hcd); /* This time clean up */
1016 }
1017
1018 return retval;
1019 }
1020
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:
2168
2169 /**
2170 * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
2171 * @hcd: pointer to the HCD representing the controller
2172 *
2173 * This is called by bus glue to report a USB host controller that died
2174 * while operations may still have been pending. It's called automatically
2175 * by the PCI glue, so only glue for non-PCI busses should need to call it.
2176 *
2177 * Only call this function with the primary HCD.
2178 */
2179 void usb_hc_died (struct usb_hcd *hcd)
2180 {
2181 unsigned long flags;
2182
2183 dev_err (hcd->self.controller, "HC died; cleaning up\n");
2184
2185 spin_lock_irqsave (&hcd_root_hub_lock, flags);
2186 clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2187 set_bit(HCD_FLAG_DEAD, &hcd->flags);
2188 if (hcd->rh_registered) {
2189 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2190
2191 /* make khubd clean up old urbs and devices */
2192 usb_set_device_state (hcd->self.root_hub,
2193 USB_STATE_NOTATTACHED);
2194 usb_kick_khubd (hcd->self.root_hub);
2195 }
2196 if (usb_hcd_is_primary_hcd(hcd) && hcd->shared_hcd) {
2197 hcd = hcd->shared_hcd;
2198 if (hcd->rh_registered) {
2199 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2200
2201 /* make khubd clean up old urbs and devices */
2202 usb_set_device_state(hcd->self.root_hub,
2203 USB_STATE_NOTATTACHED);
2204 usb_kick_khubd(hcd->self.root_hub);
2205 }
2206 }
2207 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
2208 /* Make sure that the other roothub is also deallocated. */
2209 }
2210 EXPORT_SYMBOL_GPL (usb_hc_died);
void usb_kick_khubd(struct usb_device *hdev)
{
struct usb_hub *hub = hdev_to_hub(hdev);
if (hub)
kick_khubd(hub);
}
关于 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:
680 /*-------------------------------------------------------------------------*/
681
682 /*
683 * Root Hub interrupt transfers are polled using a timer if the
684 * driver requests it; otherwise the driver is responsible for
685 * calling usb_hcd_poll_rh_status() when an event occurs.
686 *
687 * Completions are called in_interrupt(), but they may or may not
688 * be in_irq().
689 */
690 void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
691 {
692 struct urb *urb;
693 int length;
694 unsigned long flags;
695 char buffer[6]; /* Any root hubs with > 31 ports? */
696
697 if (unlikely(!hcd->rh_pollable))
698 return;
699 if (!hcd->uses_new_polling && !hcd->status_urb)
700 return;
701
702 length = hcd->driver->hub_status_data(hcd, buffer);
703 if (length > 0) {
704
705 /* try to complete the status urb */
706 spin_lock_irqsave(&hcd_root_hub_lock, flags);
707 urb = hcd->status_urb;
708 if (urb) {
709 clear_bit(HCD_FLAG_POLL_PENDING, &hcd->flags);
710 hcd->status_urb = NULL;
711 urb->actual_length = length;
712 memcpy(urb->transfer_buffer, buffer, length);
713
714 usb_hcd_unlink_urb_from_ep(hcd, urb);
715 spin_unlock(&hcd_root_hub_lock);
716 usb_hcd_giveback_urb(hcd, urb, 0);
717 spin_lock(&hcd_root_hub_lock);
718 } else {
719 length = 0;
720 set_bit(HCD_FLAG_POLL_PENDING, &hcd->flags);
721 }
722 spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
723 }
724
725 /* The USB 2.0 spec says 256 ms. This is close enough and won't
726 * exceed that limit if HZ is 100. The math is more clunky than
727 * maybe expected, this is to make sure that all timers for USB devices
728 * fire at the same time to give the CPU a break in between */
729 if (hcd->uses_new_polling ? HCD_POLL_RH(hcd) :
730 (length == 0 && hcd->status_urb != NULL))
731 mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
732 }
733 EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status);
734
735 /* timer callback */
736 static void rh_timer_func (unsigned long _hcd)
737 {
738 usb_hcd_poll_rh_status((struct usb_hcd *) _hcd);
739 }
740
前 面 两 个 if 对 咱 们 来 说 肯 定 是 不 满 足 的 .rh_registered 咱 们 刚 刚 才 设 置 为 1.
uses_new_polling 咱们也在 uhci_start()中设置为了 1.所以,咱们继续昂首挺胸的往前走.
702行,driver->hub_status_data 是每个 driver 自己定义的musb_hub_status_data
203
204 /* Caller may or may not hold musb->lock */
205 int musb_hub_status_data(struct usb_hcd *hcd, char *buf)
206 {
207 struct musb *musb = hcd_to_musb(hcd);
208 int retval = 0;
209
210 /* called in_irq() via usb_hcd_poll_rh_status() */
211 if (musb->port1_status & 0xffff0000) {
212 *buf = 0x02;
213 retval = 1;
214 }
215 return retval;
216 }
usb_hcd_poll_rh_status 这个函数完成usb_add_hcd完成
现在虽然最伟大的 probe 函数就这样结束了。。