xref: /freebsd-src/sys/dev/usb/controller/usb_controller.c (revision 09c817ba36db7c3a4ff5e25ac55816ca181a403d)
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 uint8_t usb_post_init_called = 0;
83 
84 static devclass_t usb_devclass;
85 
86 static device_method_t usb_methods[] = {
87 	DEVMETHOD(device_probe, usb_probe),
88 	DEVMETHOD(device_attach, usb_attach),
89 	DEVMETHOD(device_detach, usb_detach),
90 	DEVMETHOD(device_suspend, bus_generic_suspend),
91 	DEVMETHOD(device_resume, bus_generic_resume),
92 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
93 	{0, 0}
94 };
95 
96 static driver_t usb_driver = {
97 	.name = "usbus",
98 	.methods = usb_methods,
99 	.size = 0,
100 };
101 
102 DRIVER_MODULE(usbus, ohci, usb_driver, usb_devclass, 0, 0);
103 DRIVER_MODULE(usbus, uhci, usb_driver, usb_devclass, 0, 0);
104 DRIVER_MODULE(usbus, ehci, usb_driver, usb_devclass, 0, 0);
105 DRIVER_MODULE(usbus, at91_udp, usb_driver, usb_devclass, 0, 0);
106 DRIVER_MODULE(usbus, uss820, usb_driver, usb_devclass, 0, 0);
107 
108 /*------------------------------------------------------------------------*
109  *	usb_probe
110  *
111  * This function is called from "{ehci,ohci,uhci}_pci_attach()".
112  *------------------------------------------------------------------------*/
113 static int
114 usb_probe(device_t dev)
115 {
116 	DPRINTF("\n");
117 	return (0);
118 }
119 
120 /*------------------------------------------------------------------------*
121  *	usb_attach
122  *------------------------------------------------------------------------*/
123 static int
124 usb_attach(device_t dev)
125 {
126 	struct usb_bus *bus = device_get_ivars(dev);
127 
128 	DPRINTF("\n");
129 
130 	if (bus == NULL) {
131 		DPRINTFN(0, "USB device has no ivars\n");
132 		return (ENXIO);
133 	}
134 
135 	/* delay vfs_mountroot until the bus is explored */
136 	bus->bus_roothold = root_mount_hold(device_get_nameunit(dev));
137 
138 	if (usb_post_init_called) {
139 		mtx_lock(&Giant);
140 		usb_attach_sub(dev, bus);
141 		mtx_unlock(&Giant);
142 		usb_needs_explore(bus, 1);
143 	}
144 	return (0);			/* return success */
145 }
146 
147 /*------------------------------------------------------------------------*
148  *	usb_detach
149  *------------------------------------------------------------------------*/
150 static int
151 usb_detach(device_t dev)
152 {
153 	struct usb_bus *bus = device_get_softc(dev);
154 
155 	DPRINTF("\n");
156 
157 	if (bus == NULL) {
158 		/* was never setup properly */
159 		return (0);
160 	}
161 	/* Stop power watchdog */
162 	usb_callout_drain(&bus->power_wdog);
163 
164 	/* Let the USB explore process detach all devices. */
165 	if (bus->bus_roothold != NULL) {
166 		root_mount_rel(bus->bus_roothold);
167 		bus->bus_roothold = NULL;
168 	}
169 
170 	USB_BUS_LOCK(bus);
171 	if (usb_proc_msignal(&bus->explore_proc,
172 	    &bus->detach_msg[0], &bus->detach_msg[1])) {
173 		/* ignore */
174 	}
175 	/* Wait for detach to complete */
176 
177 	usb_proc_mwait(&bus->explore_proc,
178 	    &bus->detach_msg[0], &bus->detach_msg[1]);
179 
180 	USB_BUS_UNLOCK(bus);
181 
182 	/* Get rid of USB callback processes */
183 
184 	usb_proc_free(&bus->giant_callback_proc);
185 	usb_proc_free(&bus->non_giant_callback_proc);
186 
187 	/* Get rid of USB explore process */
188 
189 	usb_proc_free(&bus->explore_proc);
190 
191 	/* Get rid of control transfer process */
192 
193 	usb_proc_free(&bus->control_xfer_proc);
194 
195 	return (0);
196 }
197 
198 /*------------------------------------------------------------------------*
199  *	usb_bus_explore
200  *
201  * This function is used to explore the device tree from the root.
202  *------------------------------------------------------------------------*/
203 static void
204 usb_bus_explore(struct usb_proc_msg *pm)
205 {
206 	struct usb_bus *bus;
207 	struct usb_device *udev;
208 
209 	bus = ((struct usb_bus_msg *)pm)->bus;
210 	udev = bus->devices[USB_ROOT_HUB_ADDR];
211 
212 	if (udev && udev->hub) {
213 
214 		if (bus->do_probe) {
215 			bus->do_probe = 0;
216 			bus->driver_added_refcount++;
217 		}
218 		if (bus->driver_added_refcount == 0) {
219 			/* avoid zero, hence that is memory default */
220 			bus->driver_added_refcount = 1;
221 		}
222 		USB_BUS_UNLOCK(bus);
223 
224 		mtx_lock(&Giant);
225 
226 		/*
227 		 * First update the USB power state!
228 		 */
229 		usb_bus_powerd(bus);
230 		/*
231 		 * Explore the Root USB HUB. This call can sleep,
232 		 * exiting Giant, which is actually Giant.
233 		 */
234 		(udev->hub->explore) (udev);
235 
236 		mtx_unlock(&Giant);
237 
238 		USB_BUS_LOCK(bus);
239 	}
240 	if (bus->bus_roothold != NULL) {
241 		root_mount_rel(bus->bus_roothold);
242 		bus->bus_roothold = NULL;
243 	}
244 }
245 
246 /*------------------------------------------------------------------------*
247  *	usb_bus_detach
248  *
249  * This function is used to detach the device tree from the root.
250  *------------------------------------------------------------------------*/
251 static void
252 usb_bus_detach(struct usb_proc_msg *pm)
253 {
254 	struct usb_bus *bus;
255 	struct usb_device *udev;
256 	device_t dev;
257 
258 	bus = ((struct usb_bus_msg *)pm)->bus;
259 	udev = bus->devices[USB_ROOT_HUB_ADDR];
260 	dev = bus->bdev;
261 	/* clear the softc */
262 	device_set_softc(dev, NULL);
263 	USB_BUS_UNLOCK(bus);
264 
265 	mtx_lock(&Giant);
266 
267 	/* detach children first */
268 	bus_generic_detach(dev);
269 
270 	/*
271 	 * Free USB Root device, but not any sub-devices, hence they
272 	 * are freed by the caller of this function:
273 	 */
274 	usb_free_device(udev,
275 	    USB_UNCFG_FLAG_FREE_EP0);
276 
277 	mtx_unlock(&Giant);
278 	USB_BUS_LOCK(bus);
279 	/* clear bdev variable last */
280 	bus->bdev = NULL;
281 }
282 
283 static void
284 usb_power_wdog(void *arg)
285 {
286 	struct usb_bus *bus = arg;
287 
288 	USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
289 
290 	usb_callout_reset(&bus->power_wdog,
291 	    4 * hz, usb_power_wdog, arg);
292 
293 	USB_BUS_UNLOCK(bus);
294 
295 	usb_bus_power_update(bus);
296 
297 	USB_BUS_LOCK(bus);
298 }
299 
300 /*------------------------------------------------------------------------*
301  *	usb_bus_attach
302  *
303  * This function attaches USB in context of the explore thread.
304  *------------------------------------------------------------------------*/
305 static void
306 usb_bus_attach(struct usb_proc_msg *pm)
307 {
308 	struct usb_bus *bus;
309 	struct usb_device *child;
310 	device_t dev;
311 	usb_error_t err;
312 	enum usb_dev_speed speed;
313 
314 	bus = ((struct usb_bus_msg *)pm)->bus;
315 	dev = bus->bdev;
316 
317 	DPRINTF("\n");
318 
319 	switch (bus->usbrev) {
320 	case USB_REV_1_0:
321 		speed = USB_SPEED_FULL;
322 		device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
323 		break;
324 
325 	case USB_REV_1_1:
326 		speed = USB_SPEED_FULL;
327 		device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
328 		break;
329 
330 	case USB_REV_2_0:
331 		speed = USB_SPEED_HIGH;
332 		device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
333 		break;
334 
335 	case USB_REV_2_5:
336 		speed = USB_SPEED_VARIABLE;
337 		device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
338 		break;
339 
340 	default:
341 		device_printf(bus->bdev, "Unsupported USB revision!\n");
342 		return;
343 	}
344 
345 	USB_BUS_UNLOCK(bus);
346 	mtx_lock(&Giant);		/* XXX not required by USB */
347 
348 	/* default power_mask value */
349 	bus->hw_power_state =
350 	  USB_HW_POWER_CONTROL |
351 	  USB_HW_POWER_BULK |
352 	  USB_HW_POWER_INTERRUPT |
353 	  USB_HW_POWER_ISOC |
354 	  USB_HW_POWER_NON_ROOT_HUB;
355 
356 	/* make sure power is set at least once */
357 
358 	if (bus->methods->set_hw_power != NULL) {
359 		(bus->methods->set_hw_power) (bus);
360 	}
361 
362 	/* Allocate the Root USB device */
363 
364 	child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
365 	    speed, USB_MODE_HOST);
366 	if (child) {
367 		err = usb_probe_and_attach(child,
368 		    USB_IFACE_INDEX_ANY);
369 		if (!err) {
370 			if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
371 			    (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
372 				err = USB_ERR_NO_ROOT_HUB;
373 			}
374 		}
375 	} else {
376 		err = USB_ERR_NOMEM;
377 	}
378 
379 	mtx_unlock(&Giant);
380 	USB_BUS_LOCK(bus);
381 
382 	if (err) {
383 		device_printf(bus->bdev, "Root HUB problem, error=%s\n",
384 		    usbd_errstr(err));
385 	}
386 
387 	/* set softc - we are ready */
388 	device_set_softc(dev, bus);
389 
390 	/* start watchdog */
391 	usb_power_wdog(bus);
392 }
393 
394 /*------------------------------------------------------------------------*
395  *	usb_attach_sub
396  *
397  * This function creates a thread which runs the USB attach code. It
398  * is factored out, hence it can be called at two different places in
399  * time. During bootup this function is called from
400  * "usb_post_init". During hot-plug it is called directly from the
401  * "usb_attach()" method.
402  *------------------------------------------------------------------------*/
403 static void
404 usb_attach_sub(device_t dev, struct usb_bus *bus)
405 {
406 	const char *pname = device_get_nameunit(dev);
407 
408 	/* Initialise USB process messages */
409 	bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore;
410 	bus->explore_msg[0].bus = bus;
411 	bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore;
412 	bus->explore_msg[1].bus = bus;
413 
414 	bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach;
415 	bus->detach_msg[0].bus = bus;
416 	bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach;
417 	bus->detach_msg[1].bus = bus;
418 
419 	bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach;
420 	bus->attach_msg[0].bus = bus;
421 	bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach;
422 	bus->attach_msg[1].bus = bus;
423 
424 	/* Create USB explore and callback processes */
425 
426 	if (usb_proc_create(&bus->giant_callback_proc,
427 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
428 		printf("WARNING: Creation of USB Giant "
429 		    "callback process failed.\n");
430 	} else if (usb_proc_create(&bus->non_giant_callback_proc,
431 	    &bus->bus_mtx, pname, USB_PRI_HIGH)) {
432 		printf("WARNING: Creation of USB non-Giant "
433 		    "callback process failed.\n");
434 	} else if (usb_proc_create(&bus->explore_proc,
435 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
436 		printf("WARNING: Creation of USB explore "
437 		    "process failed.\n");
438 	} else if (usb_proc_create(&bus->control_xfer_proc,
439 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
440 		printf("WARNING: Creation of USB control transfer "
441 		    "process failed.\n");
442 	} else {
443 		/* Get final attach going */
444 		USB_BUS_LOCK(bus);
445 		if (usb_proc_msignal(&bus->explore_proc,
446 		    &bus->attach_msg[0], &bus->attach_msg[1])) {
447 			/* ignore */
448 		}
449 		USB_BUS_UNLOCK(bus);
450 	}
451 }
452 
453 /*------------------------------------------------------------------------*
454  *	usb_post_init
455  *
456  * This function is called to attach all USB busses that were found
457  * during bootup.
458  *------------------------------------------------------------------------*/
459 static void
460 usb_post_init(void *arg)
461 {
462 	struct usb_bus *bus;
463 	devclass_t dc;
464 	device_t dev;
465 	int max;
466 	int n;
467 
468 	mtx_lock(&Giant);
469 
470 	usb_devclass_ptr = devclass_find("usbus");
471 
472 	dc = usb_devclass_ptr;
473 	if (dc) {
474 		max = devclass_get_maxunit(dc) + 1;
475 		for (n = 0; n != max; n++) {
476 			dev = devclass_get_device(dc, n);
477 			if (dev && device_is_attached(dev)) {
478 				bus = device_get_ivars(dev);
479 				if (bus) {
480 					mtx_lock(&Giant);
481 					usb_attach_sub(dev, bus);
482 					mtx_unlock(&Giant);
483 				}
484 			}
485 		}
486 	} else {
487 		DPRINTFN(0, "no devclass\n");
488 	}
489 	usb_post_init_called = 1;
490 
491 	/* explore all USB busses in parallell */
492 
493 	usb_needs_explore_all();
494 
495 	mtx_unlock(&Giant);
496 }
497 
498 SYSINIT(usb_post_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_post_init, NULL);
499 SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL);
500 
501 /*------------------------------------------------------------------------*
502  *	usb_bus_mem_flush_all_cb
503  *------------------------------------------------------------------------*/
504 #if USB_HAVE_BUSDMA
505 static void
506 usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
507     struct usb_page *pg, usb_size_t size, usb_size_t align)
508 {
509 	usb_pc_cpu_flush(pc);
510 }
511 #endif
512 
513 /*------------------------------------------------------------------------*
514  *	usb_bus_mem_flush_all - factored out code
515  *------------------------------------------------------------------------*/
516 #if USB_HAVE_BUSDMA
517 void
518 usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
519 {
520 	if (cb) {
521 		cb(bus, &usb_bus_mem_flush_all_cb);
522 	}
523 }
524 #endif
525 
526 /*------------------------------------------------------------------------*
527  *	usb_bus_mem_alloc_all_cb
528  *------------------------------------------------------------------------*/
529 #if USB_HAVE_BUSDMA
530 static void
531 usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
532     struct usb_page *pg, usb_size_t size, usb_size_t align)
533 {
534 	/* need to initialize the page cache */
535 	pc->tag_parent = bus->dma_parent_tag;
536 
537 	if (usb_pc_alloc_mem(pc, pg, size, align)) {
538 		bus->alloc_failed = 1;
539 	}
540 }
541 #endif
542 
543 /*------------------------------------------------------------------------*
544  *	usb_bus_mem_alloc_all - factored out code
545  *
546  * Returns:
547  *    0: Success
548  * Else: Failure
549  *------------------------------------------------------------------------*/
550 uint8_t
551 usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat,
552     usb_bus_mem_cb_t *cb)
553 {
554 	bus->alloc_failed = 0;
555 
556 	mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
557 	    NULL, MTX_DEF | MTX_RECURSE);
558 
559 	usb_callout_init_mtx(&bus->power_wdog,
560 	    &bus->bus_mtx, 0);
561 
562 	TAILQ_INIT(&bus->intr_q.head);
563 
564 #if USB_HAVE_BUSDMA
565 	usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
566 	    dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX);
567 #endif
568 	if ((bus->devices_max > USB_MAX_DEVICES) ||
569 	    (bus->devices_max < USB_MIN_DEVICES) ||
570 	    (bus->devices == NULL)) {
571 		DPRINTFN(0, "Devices field has not been "
572 		    "initialised properly!\n");
573 		bus->alloc_failed = 1;		/* failure */
574 	}
575 #if USB_HAVE_BUSDMA
576 	if (cb) {
577 		cb(bus, &usb_bus_mem_alloc_all_cb);
578 	}
579 #endif
580 	if (bus->alloc_failed) {
581 		usb_bus_mem_free_all(bus, cb);
582 	}
583 	return (bus->alloc_failed);
584 }
585 
586 /*------------------------------------------------------------------------*
587  *	usb_bus_mem_free_all_cb
588  *------------------------------------------------------------------------*/
589 #if USB_HAVE_BUSDMA
590 static void
591 usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
592     struct usb_page *pg, usb_size_t size, usb_size_t align)
593 {
594 	usb_pc_free_mem(pc);
595 }
596 #endif
597 
598 /*------------------------------------------------------------------------*
599  *	usb_bus_mem_free_all - factored out code
600  *------------------------------------------------------------------------*/
601 void
602 usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
603 {
604 #if USB_HAVE_BUSDMA
605 	if (cb) {
606 		cb(bus, &usb_bus_mem_free_all_cb);
607 	}
608 	usb_dma_tag_unsetup(bus->dma_parent_tag);
609 #endif
610 
611 	mtx_destroy(&bus->bus_mtx);
612 }
613