xref: /freebsd/sys/dev/usb/controller/usb_controller.c (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
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]->hub) {
350 				err = USB_ERR_NO_ROOT_HUB;
351 			}
352 		}
353 	} else {
354 		err = USB_ERR_NOMEM;
355 	}
356 
357 	mtx_unlock(&Giant);
358 	USB_BUS_LOCK(bus);
359 
360 	if (err) {
361 		device_printf(bus->bdev, "Root HUB problem, error=%s\n",
362 		    usb2_errstr(err));
363 	}
364 
365 	/* set softc - we are ready */
366 	device_set_softc(dev, bus);
367 
368 	/* start watchdog - this function will unlock the BUS lock ! */
369 	usb2_power_wdog(bus);
370 
371 	/* need to return locked */
372 	USB_BUS_LOCK(bus);
373 }
374 
375 /*------------------------------------------------------------------------*
376  *	usb2_attach_sub
377  *
378  * This function creates a thread which runs the USB attach code. It
379  * is factored out, hence it can be called at two different places in
380  * time. During bootup this function is called from
381  * "usb2_post_init". During hot-plug it is called directly from the
382  * "usb2_attach()" method.
383  *------------------------------------------------------------------------*/
384 static void
385 usb2_attach_sub(device_t dev, struct usb2_bus *bus)
386 {
387 	const char *pname = device_get_nameunit(dev);
388 
389 	/* Initialise USB process messages */
390 	bus->explore_msg[0].hdr.pm_callback = &usb2_bus_explore;
391 	bus->explore_msg[0].bus = bus;
392 	bus->explore_msg[1].hdr.pm_callback = &usb2_bus_explore;
393 	bus->explore_msg[1].bus = bus;
394 
395 	bus->detach_msg[0].hdr.pm_callback = &usb2_bus_detach;
396 	bus->detach_msg[0].bus = bus;
397 	bus->detach_msg[1].hdr.pm_callback = &usb2_bus_detach;
398 	bus->detach_msg[1].bus = bus;
399 
400 	bus->attach_msg[0].hdr.pm_callback = &usb2_bus_attach;
401 	bus->attach_msg[0].bus = bus;
402 	bus->attach_msg[1].hdr.pm_callback = &usb2_bus_attach;
403 	bus->attach_msg[1].bus = bus;
404 
405 	bus->roothub_msg[0].hdr.pm_callback = &usb2_bus_roothub;
406 	bus->roothub_msg[0].bus = bus;
407 	bus->roothub_msg[1].hdr.pm_callback = &usb2_bus_roothub;
408 	bus->roothub_msg[1].bus = bus;
409 
410 	/* Create USB explore, roothub and callback processes */
411 
412 	if (usb2_proc_create(&bus->giant_callback_proc,
413 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
414 		printf("WARNING: Creation of USB Giant "
415 		    "callback process failed.\n");
416 	} else if (usb2_proc_create(&bus->non_giant_callback_proc,
417 	    &bus->bus_mtx, pname, USB_PRI_HIGH)) {
418 		printf("WARNING: Creation of USB non-Giant "
419 		    "callback process failed.\n");
420 	} else if (usb2_proc_create(&bus->roothub_proc,
421 	    &bus->bus_mtx, pname, USB_PRI_HIGH)) {
422 		printf("WARNING: Creation of USB roothub "
423 		    "process failed.\n");
424 	} else if (usb2_proc_create(&bus->explore_proc,
425 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
426 		printf("WARNING: Creation of USB explore "
427 		    "process failed.\n");
428 	} else {
429 		/* Get final attach going */
430 		USB_BUS_LOCK(bus);
431 		if (usb2_proc_msignal(&bus->explore_proc,
432 		    &bus->attach_msg[0], &bus->attach_msg[1])) {
433 			/* ignore */
434 		}
435 		USB_BUS_UNLOCK(bus);
436 	}
437 }
438 
439 /*------------------------------------------------------------------------*
440  *	usb2_post_init
441  *
442  * This function is called to attach all USB busses that were found
443  * during bootup.
444  *------------------------------------------------------------------------*/
445 static void
446 usb2_post_init(void *arg)
447 {
448 	struct usb2_bus *bus;
449 	devclass_t dc;
450 	device_t dev;
451 	int max;
452 	int n;
453 
454 	mtx_lock(&Giant);
455 
456 	usb2_devclass_ptr = devclass_find("usbus");
457 
458 	dc = usb2_devclass_ptr;
459 	if (dc) {
460 		max = devclass_get_maxunit(dc) + 1;
461 		for (n = 0; n != max; n++) {
462 			dev = devclass_get_device(dc, n);
463 			if (dev && device_is_attached(dev)) {
464 				bus = device_get_ivars(dev);
465 				if (bus) {
466 					mtx_lock(&Giant);
467 					usb2_attach_sub(dev, bus);
468 					mtx_unlock(&Giant);
469 				}
470 			}
471 		}
472 	} else {
473 		DPRINTFN(0, "no devclass\n");
474 	}
475 	usb2_post_init_called = 1;
476 
477 	/* explore all USB busses in parallell */
478 
479 	usb2_needs_explore_all();
480 
481 	mtx_unlock(&Giant);
482 }
483 
484 SYSINIT(usb2_post_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb2_post_init, NULL);
485 SYSUNINIT(usb2_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb2_bus_unload, NULL);
486 
487 /*------------------------------------------------------------------------*
488  *	usb2_bus_mem_flush_all_cb
489  *------------------------------------------------------------------------*/
490 static void
491 usb2_bus_mem_flush_all_cb(struct usb2_bus *bus, struct usb2_page_cache *pc,
492     struct usb2_page *pg, uint32_t size, uint32_t align)
493 {
494 	usb2_pc_cpu_flush(pc);
495 }
496 
497 /*------------------------------------------------------------------------*
498  *	usb2_bus_mem_flush_all - factored out code
499  *------------------------------------------------------------------------*/
500 void
501 usb2_bus_mem_flush_all(struct usb2_bus *bus, usb2_bus_mem_cb_t *cb)
502 {
503 	if (cb) {
504 		cb(bus, &usb2_bus_mem_flush_all_cb);
505 	}
506 }
507 
508 /*------------------------------------------------------------------------*
509  *	usb2_bus_mem_alloc_all_cb
510  *------------------------------------------------------------------------*/
511 static void
512 usb2_bus_mem_alloc_all_cb(struct usb2_bus *bus, struct usb2_page_cache *pc,
513     struct usb2_page *pg, uint32_t size, uint32_t align)
514 {
515 	/* need to initialize the page cache */
516 	pc->tag_parent = bus->dma_parent_tag;
517 
518 	if (usb2_pc_alloc_mem(pc, pg, size, align)) {
519 		bus->alloc_failed = 1;
520 	}
521 }
522 
523 /*------------------------------------------------------------------------*
524  *	usb2_bus_mem_alloc_all - factored out code
525  *
526  * Returns:
527  *    0: Success
528  * Else: Failure
529  *------------------------------------------------------------------------*/
530 uint8_t
531 usb2_bus_mem_alloc_all(struct usb2_bus *bus, bus_dma_tag_t dmat,
532     usb2_bus_mem_cb_t *cb)
533 {
534 	bus->alloc_failed = 0;
535 
536 	mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
537 	    NULL, MTX_DEF | MTX_RECURSE);
538 
539 	usb2_callout_init_mtx(&bus->power_wdog,
540 	    &bus->bus_mtx, CALLOUT_RETURNUNLOCKED);
541 
542 	TAILQ_INIT(&bus->intr_q.head);
543 
544 	usb2_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
545 	    dmat, &bus->bus_mtx, NULL, NULL, 32, USB_BUS_DMA_TAG_MAX);
546 
547 	if ((bus->devices_max > USB_MAX_DEVICES) ||
548 	    (bus->devices_max < USB_MIN_DEVICES) ||
549 	    (bus->devices == NULL)) {
550 		DPRINTFN(0, "Devices field has not been "
551 		    "initialised properly!\n");
552 		bus->alloc_failed = 1;		/* failure */
553 	}
554 	if (cb) {
555 		cb(bus, &usb2_bus_mem_alloc_all_cb);
556 	}
557 	if (bus->alloc_failed) {
558 		usb2_bus_mem_free_all(bus, cb);
559 	}
560 	return (bus->alloc_failed);
561 }
562 
563 /*------------------------------------------------------------------------*
564  *	usb2_bus_mem_free_all_cb
565  *------------------------------------------------------------------------*/
566 static void
567 usb2_bus_mem_free_all_cb(struct usb2_bus *bus, struct usb2_page_cache *pc,
568     struct usb2_page *pg, uint32_t size, uint32_t align)
569 {
570 	usb2_pc_free_mem(pc);
571 }
572 
573 /*------------------------------------------------------------------------*
574  *	usb2_bus_mem_free_all - factored out code
575  *------------------------------------------------------------------------*/
576 void
577 usb2_bus_mem_free_all(struct usb2_bus *bus, usb2_bus_mem_cb_t *cb)
578 {
579 	if (cb) {
580 		cb(bus, &usb2_bus_mem_free_all_cb);
581 	}
582 	usb2_dma_tag_unsetup(bus->dma_parent_tag);
583 
584 	mtx_destroy(&bus->bus_mtx);
585 }
586 
587 /*------------------------------------------------------------------------*
588  *	usb2_bus_roothub
589  *
590  * This function is used to execute roothub control requests on the
591  * roothub and is called from the roothub process.
592  *------------------------------------------------------------------------*/
593 static void
594 usb2_bus_roothub(struct usb2_proc_msg *pm)
595 {
596 	struct usb2_bus *bus;
597 
598 	bus = ((struct usb2_bus_msg *)pm)->bus;
599 
600 	USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
601 
602 	(bus->methods->roothub_exec) (bus);
603 }
604 
605 /*------------------------------------------------------------------------*
606  *	usb2_bus_roothub_exec
607  *
608  * This function is used to schedule the "roothub_done" bus callback
609  * method. The bus lock must be locked when calling this function.
610  *------------------------------------------------------------------------*/
611 void
612 usb2_bus_roothub_exec(struct usb2_bus *bus)
613 {
614 	USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
615 
616 	if (usb2_proc_msignal(&bus->roothub_proc,
617 	    &bus->roothub_msg[0], &bus->roothub_msg[1])) {
618 		/* ignore */
619 	}
620 }
621