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