]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/dev/usb/controller/usb_controller.c
Copy head to stable/8 as part of 8.0 Release cycle.
[FreeBSD/stable/8.git] / sys / dev / usb / controller / usb_controller.c
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/stdint.h>
28 #include <sys/stddef.h>
29 #include <sys/param.h>
30 #include <sys/queue.h>
31 #include <sys/types.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/bus.h>
35 #include <sys/linker_set.h>
36 #include <sys/module.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/condvar.h>
40 #include <sys/sysctl.h>
41 #include <sys/sx.h>
42 #include <sys/unistd.h>
43 #include <sys/callout.h>
44 #include <sys/malloc.h>
45 #include <sys/priv.h>
46
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbdi.h>
49
50 #define USB_DEBUG_VAR usb_ctrl_debug
51
52 #include <dev/usb/usb_core.h>
53 #include <dev/usb/usb_debug.h>
54 #include <dev/usb/usb_process.h>
55 #include <dev/usb/usb_busdma.h>
56 #include <dev/usb/usb_dynamic.h>
57 #include <dev/usb/usb_device.h>
58 #include <dev/usb/usb_hub.h>
59
60 #include <dev/usb/usb_controller.h>
61 #include <dev/usb/usb_bus.h>
62
63 /* function prototypes  */
64
65 static device_probe_t usb_probe;
66 static device_attach_t usb_attach;
67 static device_detach_t usb_detach;
68
69 static void     usb_attach_sub(device_t, struct usb_bus *);
70 static void     usb_post_init(void *);
71
72 /* static variables */
73
74 #ifdef USB_DEBUG
75 static int usb_ctrl_debug = 0;
76
77 SYSCTL_NODE(_hw_usb, OID_AUTO, ctrl, CTLFLAG_RW, 0, "USB controller");
78 SYSCTL_INT(_hw_usb_ctrl, OID_AUTO, debug, CTLFLAG_RW, &usb_ctrl_debug, 0,
79     "Debug level");
80 #endif
81
82 static int usb_no_boot_wait = 0;
83 TUNABLE_INT("hw.usb.no_boot_wait", &usb_no_boot_wait);
84 SYSCTL_INT(_hw_usb, OID_AUTO, no_boot_wait, CTLFLAG_RDTUN, &usb_no_boot_wait, 0,
85     "No device enumerate waiting at boot.");
86
87 static uint8_t usb_post_init_called = 0;
88
89 static devclass_t usb_devclass;
90
91 static device_method_t usb_methods[] = {
92         DEVMETHOD(device_probe, usb_probe),
93         DEVMETHOD(device_attach, usb_attach),
94         DEVMETHOD(device_detach, usb_detach),
95         DEVMETHOD(device_suspend, bus_generic_suspend),
96         DEVMETHOD(device_resume, bus_generic_resume),
97         DEVMETHOD(device_shutdown, bus_generic_shutdown),
98         {0, 0}
99 };
100
101 static driver_t usb_driver = {
102         .name = "usbus",
103         .methods = usb_methods,
104         .size = 0,
105 };
106
107 DRIVER_MODULE(usbus, ohci, usb_driver, usb_devclass, 0, 0);
108 DRIVER_MODULE(usbus, uhci, usb_driver, usb_devclass, 0, 0);
109 DRIVER_MODULE(usbus, ehci, usb_driver, usb_devclass, 0, 0);
110 DRIVER_MODULE(usbus, at91_udp, usb_driver, usb_devclass, 0, 0);
111 DRIVER_MODULE(usbus, uss820, usb_driver, usb_devclass, 0, 0);
112
113 /*------------------------------------------------------------------------*
114  *      usb_probe
115  *
116  * This function is called from "{ehci,ohci,uhci}_pci_attach()".
117  *------------------------------------------------------------------------*/
118 static int
119 usb_probe(device_t dev)
120 {
121         DPRINTF("\n");
122         return (0);
123 }
124
125 /*------------------------------------------------------------------------*
126  *      usb_attach
127  *------------------------------------------------------------------------*/
128 static int
129 usb_attach(device_t dev)
130 {
131         struct usb_bus *bus = device_get_ivars(dev);
132
133         DPRINTF("\n");
134
135         if (bus == NULL) {
136                 DPRINTFN(0, "USB device has no ivars\n");
137                 return (ENXIO);
138         }
139
140         if (usb_no_boot_wait == 0) {
141                 /* delay vfs_mountroot until the bus is explored */
142                 bus->bus_roothold = root_mount_hold(device_get_nameunit(dev));
143         }
144
145         if (usb_post_init_called) {
146                 usb_attach_sub(dev, bus);
147                 usb_needs_explore(bus, 1);
148         }
149         return (0);                     /* return success */
150 }
151
152 /*------------------------------------------------------------------------*
153  *      usb_detach
154  *------------------------------------------------------------------------*/
155 static int
156 usb_detach(device_t dev)
157 {
158         struct usb_bus *bus = device_get_softc(dev);
159
160         DPRINTF("\n");
161
162         if (bus == NULL) {
163                 /* was never setup properly */
164                 return (0);
165         }
166         /* Stop power watchdog */
167         usb_callout_drain(&bus->power_wdog);
168
169         /* Let the USB explore process detach all devices. */
170         if (bus->bus_roothold != NULL) {
171                 root_mount_rel(bus->bus_roothold);
172                 bus->bus_roothold = NULL;
173         }
174
175         USB_BUS_LOCK(bus);
176         if (usb_proc_msignal(&bus->explore_proc,
177             &bus->detach_msg[0], &bus->detach_msg[1])) {
178                 /* ignore */
179         }
180         /* Wait for detach to complete */
181
182         usb_proc_mwait(&bus->explore_proc,
183             &bus->detach_msg[0], &bus->detach_msg[1]);
184
185         USB_BUS_UNLOCK(bus);
186
187         /* Get rid of USB callback processes */
188
189         usb_proc_free(&bus->giant_callback_proc);
190         usb_proc_free(&bus->non_giant_callback_proc);
191
192         /* Get rid of USB explore process */
193
194         usb_proc_free(&bus->explore_proc);
195
196         /* Get rid of control transfer process */
197
198         usb_proc_free(&bus->control_xfer_proc);
199
200         return (0);
201 }
202
203 /*------------------------------------------------------------------------*
204  *      usb_bus_explore
205  *
206  * This function is used to explore the device tree from the root.
207  *------------------------------------------------------------------------*/
208 static void
209 usb_bus_explore(struct usb_proc_msg *pm)
210 {
211         struct usb_bus *bus;
212         struct usb_device *udev;
213
214         bus = ((struct usb_bus_msg *)pm)->bus;
215         udev = bus->devices[USB_ROOT_HUB_ADDR];
216
217         if (udev && udev->hub) {
218
219                 if (bus->do_probe) {
220                         bus->do_probe = 0;
221                         bus->driver_added_refcount++;
222                 }
223                 if (bus->driver_added_refcount == 0) {
224                         /* avoid zero, hence that is memory default */
225                         bus->driver_added_refcount = 1;
226                 }
227                 USB_BUS_UNLOCK(bus);
228
229                 /*
230                  * First update the USB power state!
231                  */
232                 usb_bus_powerd(bus);
233
234                  /* Explore the Root USB HUB. */
235                 (udev->hub->explore) (udev);
236                 USB_BUS_LOCK(bus);
237         }
238         if (bus->bus_roothold != NULL) {
239                 root_mount_rel(bus->bus_roothold);
240                 bus->bus_roothold = NULL;
241         }
242 }
243
244 /*------------------------------------------------------------------------*
245  *      usb_bus_detach
246  *
247  * This function is used to detach the device tree from the root.
248  *------------------------------------------------------------------------*/
249 static void
250 usb_bus_detach(struct usb_proc_msg *pm)
251 {
252         struct usb_bus *bus;
253         struct usb_device *udev;
254         device_t dev;
255
256         bus = ((struct usb_bus_msg *)pm)->bus;
257         udev = bus->devices[USB_ROOT_HUB_ADDR];
258         dev = bus->bdev;
259         /* clear the softc */
260         device_set_softc(dev, NULL);
261         USB_BUS_UNLOCK(bus);
262
263         newbus_xlock();
264
265         /* detach children first */
266         bus_generic_detach(dev);
267
268         /*
269          * Free USB Root device, but not any sub-devices, hence they
270          * are freed by the caller of this function:
271          */
272         usb_free_device(udev,
273             USB_UNCFG_FLAG_FREE_EP0);
274
275         newbus_xunlock();
276         USB_BUS_LOCK(bus);
277         /* clear bdev variable last */
278         bus->bdev = NULL;
279 }
280
281 static void
282 usb_power_wdog(void *arg)
283 {
284         struct usb_bus *bus = arg;
285
286         USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
287
288         usb_callout_reset(&bus->power_wdog,
289             4 * hz, usb_power_wdog, arg);
290
291         USB_BUS_UNLOCK(bus);
292
293         usb_bus_power_update(bus);
294
295         USB_BUS_LOCK(bus);
296 }
297
298 /*------------------------------------------------------------------------*
299  *      usb_bus_attach
300  *
301  * This function attaches USB in context of the explore thread.
302  *------------------------------------------------------------------------*/
303 static void
304 usb_bus_attach(struct usb_proc_msg *pm)
305 {
306         struct usb_bus *bus;
307         struct usb_device *child;
308         device_t dev;
309         usb_error_t err;
310         enum usb_dev_speed speed;
311
312         bus = ((struct usb_bus_msg *)pm)->bus;
313         dev = bus->bdev;
314
315         DPRINTF("\n");
316
317         switch (bus->usbrev) {
318         case USB_REV_1_0:
319                 speed = USB_SPEED_FULL;
320                 device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
321                 break;
322
323         case USB_REV_1_1:
324                 speed = USB_SPEED_FULL;
325                 device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
326                 break;
327
328         case USB_REV_2_0:
329                 speed = USB_SPEED_HIGH;
330                 device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
331                 break;
332
333         case USB_REV_2_5:
334                 speed = USB_SPEED_VARIABLE;
335                 device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
336                 break;
337
338         default:
339                 device_printf(bus->bdev, "Unsupported USB revision!\n");
340                 return;
341         }
342
343         USB_BUS_UNLOCK(bus);
344         newbus_xlock();
345
346         /* default power_mask value */
347         bus->hw_power_state =
348           USB_HW_POWER_CONTROL |
349           USB_HW_POWER_BULK |
350           USB_HW_POWER_INTERRUPT |
351           USB_HW_POWER_ISOC |
352           USB_HW_POWER_NON_ROOT_HUB;
353
354         /* make sure power is set at least once */
355
356         if (bus->methods->set_hw_power != NULL) {
357                 (bus->methods->set_hw_power) (bus);
358         }
359
360         /* Allocate the Root USB device */
361
362         child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
363             speed, USB_MODE_HOST);
364         if (child) {
365                 err = usb_probe_and_attach(child,
366                     USB_IFACE_INDEX_ANY);
367                 if (!err) {
368                         if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
369                             (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
370                                 err = USB_ERR_NO_ROOT_HUB;
371                         }
372                 }
373         } else {
374                 err = USB_ERR_NOMEM;
375         }
376
377         newbus_xunlock();
378         USB_BUS_LOCK(bus);
379
380         if (err) {
381                 device_printf(bus->bdev, "Root HUB problem, error=%s\n",
382                     usbd_errstr(err));
383         }
384
385         /* set softc - we are ready */
386         device_set_softc(dev, bus);
387
388         /* start watchdog */
389         usb_power_wdog(bus);
390 }
391
392 /*------------------------------------------------------------------------*
393  *      usb_attach_sub
394  *
395  * This function creates a thread which runs the USB attach code. It
396  * is factored out, hence it can be called at two different places in
397  * time. During bootup this function is called from
398  * "usb_post_init". During hot-plug it is called directly from the
399  * "usb_attach()" method.
400  *------------------------------------------------------------------------*/
401 static void
402 usb_attach_sub(device_t dev, struct usb_bus *bus)
403 {
404         const char *pname = device_get_nameunit(dev);
405
406         /* Initialise USB process messages */
407         bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore;
408         bus->explore_msg[0].bus = bus;
409         bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore;
410         bus->explore_msg[1].bus = bus;
411
412         bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach;
413         bus->detach_msg[0].bus = bus;
414         bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach;
415         bus->detach_msg[1].bus = bus;
416
417         bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach;
418         bus->attach_msg[0].bus = bus;
419         bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach;
420         bus->attach_msg[1].bus = bus;
421
422         /* Create USB explore and callback processes */
423
424         if (usb_proc_create(&bus->giant_callback_proc,
425             &bus->bus_mtx, pname, USB_PRI_MED)) {
426                 printf("WARNING: Creation of USB Giant "
427                     "callback process failed.\n");
428         } else if (usb_proc_create(&bus->non_giant_callback_proc,
429             &bus->bus_mtx, pname, USB_PRI_HIGH)) {
430                 printf("WARNING: Creation of USB non-Giant "
431                     "callback process failed.\n");
432         } else if (usb_proc_create(&bus->explore_proc,
433             &bus->bus_mtx, pname, USB_PRI_MED)) {
434                 printf("WARNING: Creation of USB explore "
435                     "process failed.\n");
436         } else if (usb_proc_create(&bus->control_xfer_proc,
437             &bus->bus_mtx, pname, USB_PRI_MED)) {
438                 printf("WARNING: Creation of USB control transfer "
439                     "process failed.\n");
440         } else {
441                 /* Get final attach going */
442                 USB_BUS_LOCK(bus);
443                 if (usb_proc_msignal(&bus->explore_proc,
444                     &bus->attach_msg[0], &bus->attach_msg[1])) {
445                         /* ignore */
446                 }
447                 USB_BUS_UNLOCK(bus);
448         }
449 }
450
451 /*------------------------------------------------------------------------*
452  *      usb_post_init
453  *
454  * This function is called to attach all USB busses that were found
455  * during bootup.
456  *------------------------------------------------------------------------*/
457 static void
458 usb_post_init(void *arg)
459 {
460         struct usb_bus *bus;
461         devclass_t dc;
462         device_t dev;
463         int max;
464         int n;
465
466         newbus_xlock();
467
468         usb_devclass_ptr = devclass_find("usbus");
469
470         dc = usb_devclass_ptr;
471         if (dc) {
472                 max = devclass_get_maxunit(dc) + 1;
473                 for (n = 0; n != max; n++) {
474                         dev = devclass_get_device(dc, n);
475                         if (dev && device_is_attached(dev)) {
476                                 bus = device_get_ivars(dev);
477                                 if (bus)
478                                         usb_attach_sub(dev, bus);
479                         }
480                 }
481         } else {
482                 DPRINTFN(0, "no devclass\n");
483         }
484         usb_post_init_called = 1;
485
486         /* explore all USB busses in parallell */
487
488         usb_needs_explore_all();
489
490         newbus_xunlock();
491 }
492
493 SYSINIT(usb_post_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_post_init, NULL);
494 SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL);
495
496 /*------------------------------------------------------------------------*
497  *      usb_bus_mem_flush_all_cb
498  *------------------------------------------------------------------------*/
499 #if USB_HAVE_BUSDMA
500 static void
501 usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
502     struct usb_page *pg, usb_size_t size, usb_size_t align)
503 {
504         usb_pc_cpu_flush(pc);
505 }
506 #endif
507
508 /*------------------------------------------------------------------------*
509  *      usb_bus_mem_flush_all - factored out code
510  *------------------------------------------------------------------------*/
511 #if USB_HAVE_BUSDMA
512 void
513 usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
514 {
515         if (cb) {
516                 cb(bus, &usb_bus_mem_flush_all_cb);
517         }
518 }
519 #endif
520
521 /*------------------------------------------------------------------------*
522  *      usb_bus_mem_alloc_all_cb
523  *------------------------------------------------------------------------*/
524 #if USB_HAVE_BUSDMA
525 static void
526 usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
527     struct usb_page *pg, usb_size_t size, usb_size_t align)
528 {
529         /* need to initialize the page cache */
530         pc->tag_parent = bus->dma_parent_tag;
531
532         if (usb_pc_alloc_mem(pc, pg, size, align)) {
533                 bus->alloc_failed = 1;
534         }
535 }
536 #endif
537
538 /*------------------------------------------------------------------------*
539  *      usb_bus_mem_alloc_all - factored out code
540  *
541  * Returns:
542  *    0: Success
543  * Else: Failure
544  *------------------------------------------------------------------------*/
545 uint8_t
546 usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat,
547     usb_bus_mem_cb_t *cb)
548 {
549         bus->alloc_failed = 0;
550
551         mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
552             NULL, MTX_DEF | MTX_RECURSE);
553
554         usb_callout_init_mtx(&bus->power_wdog,
555             &bus->bus_mtx, 0);
556
557         TAILQ_INIT(&bus->intr_q.head);
558
559 #if USB_HAVE_BUSDMA
560         usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
561             dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX);
562 #endif
563         if ((bus->devices_max > USB_MAX_DEVICES) ||
564             (bus->devices_max < USB_MIN_DEVICES) ||
565             (bus->devices == NULL)) {
566                 DPRINTFN(0, "Devices field has not been "
567                     "initialised properly!\n");
568                 bus->alloc_failed = 1;          /* failure */
569         }
570 #if USB_HAVE_BUSDMA
571         if (cb) {
572                 cb(bus, &usb_bus_mem_alloc_all_cb);
573         }
574 #endif
575         if (bus->alloc_failed) {
576                 usb_bus_mem_free_all(bus, cb);
577         }
578         return (bus->alloc_failed);
579 }
580
581 /*------------------------------------------------------------------------*
582  *      usb_bus_mem_free_all_cb
583  *------------------------------------------------------------------------*/
584 #if USB_HAVE_BUSDMA
585 static void
586 usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
587     struct usb_page *pg, usb_size_t size, usb_size_t align)
588 {
589         usb_pc_free_mem(pc);
590 }
591 #endif
592
593 /*------------------------------------------------------------------------*
594  *      usb_bus_mem_free_all - factored out code
595  *------------------------------------------------------------------------*/
596 void
597 usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
598 {
599 #if USB_HAVE_BUSDMA
600         if (cb) {
601                 cb(bus, &usb_bus_mem_free_all_cb);
602         }
603         usb_dma_tag_unsetup(bus->dma_parent_tag);
604 #endif
605
606         mtx_destroy(&bus->bus_mtx);
607 }