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