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