xref: /freebsd-src/sys/dev/usb/controller/usb_controller.c (revision bdd4120608d18b2e0e0792828a7ceb91a812bf15)
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 <dev/usb/usb_mfunc.h>
28 #include <dev/usb/usb_error.h>
29 #include <dev/usb/usb.h>
30 
31 #define	USB_DEBUG_VAR usb2_ctrl_debug
32 
33 #include <dev/usb/usb_core.h>
34 #include <dev/usb/usb_debug.h>
35 #include <dev/usb/usb_process.h>
36 #include <dev/usb/usb_busdma.h>
37 #include <dev/usb/usb_dynamic.h>
38 #include <dev/usb/usb_device.h>
39 #include <dev/usb/usb_hub.h>
40 
41 #include <dev/usb/usb_controller.h>
42 #include <dev/usb/usb_bus.h>
43 
44 /* function prototypes */
45 
46 static device_probe_t usb2_probe;
47 static device_attach_t usb2_attach;
48 static device_detach_t usb2_detach;
49 
50 static void	usb2_attach_sub(device_t, struct usb2_bus *);
51 static void	usb2_post_init(void *);
52 static void	usb2_bus_roothub(struct usb2_proc_msg *pm);
53 
54 /* static variables */
55 
56 #if USB_DEBUG
57 static int usb2_ctrl_debug = 0;
58 
59 SYSCTL_NODE(_hw_usb2, OID_AUTO, ctrl, CTLFLAG_RW, 0, "USB controller");
60 SYSCTL_INT(_hw_usb2_ctrl, OID_AUTO, debug, CTLFLAG_RW, &usb2_ctrl_debug, 0,
61     "Debug level");
62 #endif
63 
64 static uint8_t usb2_post_init_called = 0;
65 
66 static devclass_t usb2_devclass;
67 
68 static device_method_t usb2_methods[] = {
69 	DEVMETHOD(device_probe, usb2_probe),
70 	DEVMETHOD(device_attach, usb2_attach),
71 	DEVMETHOD(device_detach, usb2_detach),
72 	DEVMETHOD(device_suspend, bus_generic_suspend),
73 	DEVMETHOD(device_resume, bus_generic_resume),
74 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
75 	{0, 0}
76 };
77 
78 static driver_t usb2_driver = {
79 	.name = "usbus",
80 	.methods = usb2_methods,
81 	.size = 0,
82 };
83 
84 DRIVER_MODULE(usbus, ohci, usb2_driver, usb2_devclass, 0, 0);
85 DRIVER_MODULE(usbus, uhci, usb2_driver, usb2_devclass, 0, 0);
86 DRIVER_MODULE(usbus, ehci, usb2_driver, usb2_devclass, 0, 0);
87 DRIVER_MODULE(usbus, at91_udp, usb2_driver, usb2_devclass, 0, 0);
88 DRIVER_MODULE(usbus, uss820, usb2_driver, usb2_devclass, 0, 0);
89 
90 /*------------------------------------------------------------------------*
91  *	usb2_probe
92  *
93  * This function is called from "{ehci,ohci,uhci}_pci_attach()".
94  *------------------------------------------------------------------------*/
95 static int
96 usb2_probe(device_t dev)
97 {
98 	DPRINTF("\n");
99 	return (0);
100 }
101 
102 /*------------------------------------------------------------------------*
103  *	usb2_attach
104  *------------------------------------------------------------------------*/
105 static int
106 usb2_attach(device_t dev)
107 {
108 	struct usb2_bus *bus = device_get_ivars(dev);
109 
110 	DPRINTF("\n");
111 
112 	if (bus == NULL) {
113 		DPRINTFN(0, "USB device has no ivars\n");
114 		return (ENXIO);
115 	}
116 
117 	/* delay vfs_mountroot until the bus is explored */
118 	bus->bus_roothold = root_mount_hold(device_get_nameunit(dev), M_WAITOK);
119 
120 	if (usb2_post_init_called) {
121 		mtx_lock(&Giant);
122 		usb2_attach_sub(dev, bus);
123 		mtx_unlock(&Giant);
124 		usb2_needs_explore(bus, 1);
125 	}
126 	return (0);			/* return success */
127 }
128 
129 /*------------------------------------------------------------------------*
130  *	usb2_detach
131  *------------------------------------------------------------------------*/
132 static int
133 usb2_detach(device_t dev)
134 {
135 	struct usb2_bus *bus = device_get_softc(dev);
136 
137 	DPRINTF("\n");
138 
139 	if (bus == NULL) {
140 		/* was never setup properly */
141 		return (0);
142 	}
143 	/* Stop power watchdog */
144 	usb2_callout_drain(&bus->power_wdog);
145 
146 	/* Let the USB explore process detach all devices. */
147 	if (bus->bus_roothold != NULL) {
148 		root_mount_rel(bus->bus_roothold);
149 		bus->bus_roothold = NULL;
150 	}
151 
152 	USB_BUS_LOCK(bus);
153 	if (usb2_proc_msignal(&bus->explore_proc,
154 	    &bus->detach_msg[0], &bus->detach_msg[1])) {
155 		/* ignore */
156 	}
157 	/* Wait for detach to complete */
158 
159 	usb2_proc_mwait(&bus->explore_proc,
160 	    &bus->detach_msg[0], &bus->detach_msg[1]);
161 
162 	USB_BUS_UNLOCK(bus);
163 
164 	/* Get rid of USB callback processes */
165 
166 	usb2_proc_free(&bus->giant_callback_proc);
167 	usb2_proc_free(&bus->non_giant_callback_proc);
168 
169 	/* Get rid of USB roothub process */
170 
171 	usb2_proc_free(&bus->roothub_proc);
172 
173 	/* Get rid of USB explore process */
174 
175 	usb2_proc_free(&bus->explore_proc);
176 
177 	return (0);
178 }
179 
180 /*------------------------------------------------------------------------*
181  *	usb2_bus_explore
182  *
183  * This function is used to explore the device tree from the root.
184  *------------------------------------------------------------------------*/
185 static void
186 usb2_bus_explore(struct usb2_proc_msg *pm)
187 {
188 	struct usb2_bus *bus;
189 	struct usb2_device *udev;
190 
191 	bus = ((struct usb2_bus_msg *)pm)->bus;
192 	udev = bus->devices[USB_ROOT_HUB_ADDR];
193 
194 	if (udev && udev->hub) {
195 
196 		if (bus->do_probe) {
197 			bus->do_probe = 0;
198 			bus->driver_added_refcount++;
199 		}
200 		if (bus->driver_added_refcount == 0) {
201 			/* avoid zero, hence that is memory default */
202 			bus->driver_added_refcount = 1;
203 		}
204 		USB_BUS_UNLOCK(bus);
205 
206 		mtx_lock(&Giant);
207 
208 		/*
209 		 * First update the USB power state!
210 		 */
211 		usb2_bus_powerd(bus);
212 
213 		/*
214 		 * Explore the Root USB HUB. This call can sleep,
215 		 * exiting Giant, which is actually Giant.
216 		 */
217 		(udev->hub->explore) (udev);
218 
219 		mtx_unlock(&Giant);
220 
221 		USB_BUS_LOCK(bus);
222 	}
223 	if (bus->bus_roothold != NULL) {
224 		root_mount_rel(bus->bus_roothold);
225 		bus->bus_roothold = NULL;
226 	}
227 }
228 
229 /*------------------------------------------------------------------------*
230  *	usb2_bus_detach
231  *
232  * This function is used to detach the device tree from the root.
233  *------------------------------------------------------------------------*/
234 static void
235 usb2_bus_detach(struct usb2_proc_msg *pm)
236 {
237 	struct usb2_bus *bus;
238 	struct usb2_device *udev;
239 	device_t dev;
240 
241 	bus = ((struct usb2_bus_msg *)pm)->bus;
242 	udev = bus->devices[USB_ROOT_HUB_ADDR];
243 	dev = bus->bdev;
244 	/* clear the softc */
245 	device_set_softc(dev, NULL);
246 	USB_BUS_UNLOCK(bus);
247 
248 	mtx_lock(&Giant);
249 
250 	/* detach children first */
251 	bus_generic_detach(dev);
252 
253 	/*
254 	 * Free USB Root device, but not any sub-devices, hence they
255 	 * are freed by the caller of this function:
256 	 */
257 	usb2_free_device(udev,
258 	    USB_UNCFG_FLAG_FREE_EP0);
259 
260 	mtx_unlock(&Giant);
261 	USB_BUS_LOCK(bus);
262 	/* clear bdev variable last */
263 	bus->bdev = NULL;
264 }
265 
266 static void
267 usb2_power_wdog(void *arg)
268 {
269 	struct usb2_bus *bus = arg;
270 
271 	USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
272 
273 	usb2_callout_reset(&bus->power_wdog,
274 	    4 * hz, usb2_power_wdog, arg);
275 
276 	USB_BUS_UNLOCK(bus);
277 
278 	usb2_bus_power_update(bus);
279 
280 	USB_BUS_LOCK(bus);
281 }
282 
283 /*------------------------------------------------------------------------*
284  *	usb2_bus_attach
285  *
286  * This function attaches USB in context of the explore thread.
287  *------------------------------------------------------------------------*/
288 static void
289 usb2_bus_attach(struct usb2_proc_msg *pm)
290 {
291 	struct usb2_bus *bus;
292 	struct usb2_device *child;
293 	device_t dev;
294 	usb2_error_t err;
295 	uint8_t speed;
296 
297 	bus = ((struct usb2_bus_msg *)pm)->bus;
298 	dev = bus->bdev;
299 
300 	DPRINTF("\n");
301 
302 	switch (bus->usbrev) {
303 	case USB_REV_1_0:
304 		speed = USB_SPEED_FULL;
305 		device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
306 		break;
307 
308 	case USB_REV_1_1:
309 		speed = USB_SPEED_FULL;
310 		device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
311 		break;
312 
313 	case USB_REV_2_0:
314 		speed = USB_SPEED_HIGH;
315 		device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
316 		break;
317 
318 	case USB_REV_2_5:
319 		speed = USB_SPEED_VARIABLE;
320 		device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
321 		break;
322 
323 	default:
324 		device_printf(bus->bdev, "Unsupported USB revision!\n");
325 		return;
326 	}
327 
328 	USB_BUS_UNLOCK(bus);
329 	mtx_lock(&Giant);		/* XXX not required by USB */
330 
331 	/* Allocate the Root USB device */
332 
333 	child = usb2_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
334 	    speed, USB_MODE_HOST);
335 	if (child) {
336 		err = usb2_probe_and_attach(child,
337 		    USB_IFACE_INDEX_ANY);
338 		if (!err) {
339 			if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
340 			    (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
341 				err = USB_ERR_NO_ROOT_HUB;
342 			}
343 		}
344 	} else {
345 		err = USB_ERR_NOMEM;
346 	}
347 
348 	mtx_unlock(&Giant);
349 	USB_BUS_LOCK(bus);
350 
351 	if (err) {
352 		device_printf(bus->bdev, "Root HUB problem, error=%s\n",
353 		    usb2_errstr(err));
354 	}
355 
356 	/* set softc - we are ready */
357 	device_set_softc(dev, bus);
358 
359 	/* start watchdog */
360 	usb2_power_wdog(bus);
361 }
362 
363 /*------------------------------------------------------------------------*
364  *	usb2_attach_sub
365  *
366  * This function creates a thread which runs the USB attach code. It
367  * is factored out, hence it can be called at two different places in
368  * time. During bootup this function is called from
369  * "usb2_post_init". During hot-plug it is called directly from the
370  * "usb2_attach()" method.
371  *------------------------------------------------------------------------*/
372 static void
373 usb2_attach_sub(device_t dev, struct usb2_bus *bus)
374 {
375 	const char *pname = device_get_nameunit(dev);
376 
377 	/* Initialise USB process messages */
378 	bus->explore_msg[0].hdr.pm_callback = &usb2_bus_explore;
379 	bus->explore_msg[0].bus = bus;
380 	bus->explore_msg[1].hdr.pm_callback = &usb2_bus_explore;
381 	bus->explore_msg[1].bus = bus;
382 
383 	bus->detach_msg[0].hdr.pm_callback = &usb2_bus_detach;
384 	bus->detach_msg[0].bus = bus;
385 	bus->detach_msg[1].hdr.pm_callback = &usb2_bus_detach;
386 	bus->detach_msg[1].bus = bus;
387 
388 	bus->attach_msg[0].hdr.pm_callback = &usb2_bus_attach;
389 	bus->attach_msg[0].bus = bus;
390 	bus->attach_msg[1].hdr.pm_callback = &usb2_bus_attach;
391 	bus->attach_msg[1].bus = bus;
392 
393 	bus->roothub_msg[0].hdr.pm_callback = &usb2_bus_roothub;
394 	bus->roothub_msg[0].bus = bus;
395 	bus->roothub_msg[1].hdr.pm_callback = &usb2_bus_roothub;
396 	bus->roothub_msg[1].bus = bus;
397 
398 	/* Create USB explore, roothub and callback processes */
399 
400 	if (usb2_proc_create(&bus->giant_callback_proc,
401 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
402 		printf("WARNING: Creation of USB Giant "
403 		    "callback process failed.\n");
404 	} else if (usb2_proc_create(&bus->non_giant_callback_proc,
405 	    &bus->bus_mtx, pname, USB_PRI_HIGH)) {
406 		printf("WARNING: Creation of USB non-Giant "
407 		    "callback process failed.\n");
408 	} else if (usb2_proc_create(&bus->roothub_proc,
409 	    &bus->bus_mtx, pname, USB_PRI_HIGH)) {
410 		printf("WARNING: Creation of USB roothub "
411 		    "process failed.\n");
412 	} else if (usb2_proc_create(&bus->explore_proc,
413 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
414 		printf("WARNING: Creation of USB explore "
415 		    "process failed.\n");
416 	} else {
417 		/* Get final attach going */
418 		USB_BUS_LOCK(bus);
419 		if (usb2_proc_msignal(&bus->explore_proc,
420 		    &bus->attach_msg[0], &bus->attach_msg[1])) {
421 			/* ignore */
422 		}
423 		USB_BUS_UNLOCK(bus);
424 	}
425 }
426 
427 /*------------------------------------------------------------------------*
428  *	usb2_post_init
429  *
430  * This function is called to attach all USB busses that were found
431  * during bootup.
432  *------------------------------------------------------------------------*/
433 static void
434 usb2_post_init(void *arg)
435 {
436 	struct usb2_bus *bus;
437 	devclass_t dc;
438 	device_t dev;
439 	int max;
440 	int n;
441 
442 	mtx_lock(&Giant);
443 
444 	usb2_devclass_ptr = devclass_find("usbus");
445 
446 	dc = usb2_devclass_ptr;
447 	if (dc) {
448 		max = devclass_get_maxunit(dc) + 1;
449 		for (n = 0; n != max; n++) {
450 			dev = devclass_get_device(dc, n);
451 			if (dev && device_is_attached(dev)) {
452 				bus = device_get_ivars(dev);
453 				if (bus) {
454 					mtx_lock(&Giant);
455 					usb2_attach_sub(dev, bus);
456 					mtx_unlock(&Giant);
457 				}
458 			}
459 		}
460 	} else {
461 		DPRINTFN(0, "no devclass\n");
462 	}
463 	usb2_post_init_called = 1;
464 
465 	/* explore all USB busses in parallell */
466 
467 	usb2_needs_explore_all();
468 
469 	mtx_unlock(&Giant);
470 }
471 
472 SYSINIT(usb2_post_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb2_post_init, NULL);
473 SYSUNINIT(usb2_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb2_bus_unload, NULL);
474 
475 /*------------------------------------------------------------------------*
476  *	usb2_bus_mem_flush_all_cb
477  *------------------------------------------------------------------------*/
478 #if USB_HAVE_BUSDMA
479 static void
480 usb2_bus_mem_flush_all_cb(struct usb2_bus *bus, struct usb2_page_cache *pc,
481     struct usb2_page *pg, uint32_t size, uint32_t align)
482 {
483 	usb2_pc_cpu_flush(pc);
484 }
485 #endif
486 
487 /*------------------------------------------------------------------------*
488  *	usb2_bus_mem_flush_all - factored out code
489  *------------------------------------------------------------------------*/
490 #if USB_HAVE_BUSDMA
491 void
492 usb2_bus_mem_flush_all(struct usb2_bus *bus, usb2_bus_mem_cb_t *cb)
493 {
494 	if (cb) {
495 		cb(bus, &usb2_bus_mem_flush_all_cb);
496 	}
497 }
498 #endif
499 
500 /*------------------------------------------------------------------------*
501  *	usb2_bus_mem_alloc_all_cb
502  *------------------------------------------------------------------------*/
503 #if USB_HAVE_BUSDMA
504 static void
505 usb2_bus_mem_alloc_all_cb(struct usb2_bus *bus, struct usb2_page_cache *pc,
506     struct usb2_page *pg, uint32_t size, uint32_t align)
507 {
508 	/* need to initialize the page cache */
509 	pc->tag_parent = bus->dma_parent_tag;
510 
511 	if (usb2_pc_alloc_mem(pc, pg, size, align)) {
512 		bus->alloc_failed = 1;
513 	}
514 }
515 #endif
516 
517 /*------------------------------------------------------------------------*
518  *	usb2_bus_mem_alloc_all - factored out code
519  *
520  * Returns:
521  *    0: Success
522  * Else: Failure
523  *------------------------------------------------------------------------*/
524 uint8_t
525 usb2_bus_mem_alloc_all(struct usb2_bus *bus, bus_dma_tag_t dmat,
526     usb2_bus_mem_cb_t *cb)
527 {
528 	bus->alloc_failed = 0;
529 
530 	mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
531 	    NULL, MTX_DEF | MTX_RECURSE);
532 
533 	usb2_callout_init_mtx(&bus->power_wdog,
534 	    &bus->bus_mtx, 0);
535 
536 	TAILQ_INIT(&bus->intr_q.head);
537 
538 #if USB_HAVE_BUSDMA
539 	usb2_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
540 	    dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX);
541 #endif
542 	if ((bus->devices_max > USB_MAX_DEVICES) ||
543 	    (bus->devices_max < USB_MIN_DEVICES) ||
544 	    (bus->devices == NULL)) {
545 		DPRINTFN(0, "Devices field has not been "
546 		    "initialised properly!\n");
547 		bus->alloc_failed = 1;		/* failure */
548 	}
549 #if USB_HAVE_BUSDMA
550 	if (cb) {
551 		cb(bus, &usb2_bus_mem_alloc_all_cb);
552 	}
553 #endif
554 	if (bus->alloc_failed) {
555 		usb2_bus_mem_free_all(bus, cb);
556 	}
557 	return (bus->alloc_failed);
558 }
559 
560 /*------------------------------------------------------------------------*
561  *	usb2_bus_mem_free_all_cb
562  *------------------------------------------------------------------------*/
563 #if USB_HAVE_BUSDMA
564 static void
565 usb2_bus_mem_free_all_cb(struct usb2_bus *bus, struct usb2_page_cache *pc,
566     struct usb2_page *pg, uint32_t size, uint32_t align)
567 {
568 	usb2_pc_free_mem(pc);
569 }
570 #endif
571 
572 /*------------------------------------------------------------------------*
573  *	usb2_bus_mem_free_all - factored out code
574  *------------------------------------------------------------------------*/
575 void
576 usb2_bus_mem_free_all(struct usb2_bus *bus, usb2_bus_mem_cb_t *cb)
577 {
578 #if USB_HAVE_BUSDMA
579 	if (cb) {
580 		cb(bus, &usb2_bus_mem_free_all_cb);
581 	}
582 	usb2_dma_tag_unsetup(bus->dma_parent_tag);
583 #endif
584 
585 	mtx_destroy(&bus->bus_mtx);
586 }
587 
588 /*------------------------------------------------------------------------*
589  *	usb2_bus_roothub
590  *
591  * This function is used to execute roothub control requests on the
592  * roothub and is called from the roothub process.
593  *------------------------------------------------------------------------*/
594 static void
595 usb2_bus_roothub(struct usb2_proc_msg *pm)
596 {
597 	struct usb2_bus *bus;
598 
599 	bus = ((struct usb2_bus_msg *)pm)->bus;
600 
601 	USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
602 
603 	(bus->methods->roothub_exec) (bus);
604 }
605 
606 /*------------------------------------------------------------------------*
607  *	usb2_bus_roothub_exec
608  *
609  * This function is used to schedule the "roothub_done" bus callback
610  * method. The bus lock must be locked when calling this function.
611  *------------------------------------------------------------------------*/
612 void
613 usb2_bus_roothub_exec(struct usb2_bus *bus)
614 {
615 	USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
616 
617 	if (usb2_proc_msignal(&bus->roothub_proc,
618 	    &bus->roothub_msg[0], &bus->roothub_msg[1])) {
619 		/* ignore */
620 	}
621 }
622