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