usb.c revision 1.95.4.2 1 /* $NetBSD: usb.c,v 1.95.4.2 2007/06/17 01:22:12 itohy Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart (at) augustsson.net) at
9 * Carlstedt Research & Technology.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * USB specifications and other documentation can be found at
42 * http://www.usb.org/developers/docs/ and
43 * http://www.usb.org/developers/devclass_docs/
44 */
45
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.95.4.2 2007/06/17 01:22:12 itohy Exp $");
48 /* __FBSDID("$FreeBSD: src/sys/dev/usb/usb.c,v 1.111 2006/10/19 01:15:58 iedowse Exp $"); */
49
50 #if defined(__NetBSD__)
51 #include "opt_compat_netbsd.h"
52 #include "ohci.h"
53 #include "uhci.h"
54 #endif
55
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/kernel.h>
59 #include <sys/lock.h>
60 #include <sys/malloc.h>
61 #include <sys/fcntl.h>
62 #if defined(__NetBSD__) || defined(__OpenBSD__)
63 #include <sys/device.h>
64 #include <sys/vnode.h>
65 #elif defined(__FreeBSD__)
66 #if __FreeBSD_version >= 500000
67 #include <sys/mutex.h>
68 #endif
69 #include <sys/unistd.h>
70 #include <sys/module.h>
71 #include <sys/bus.h>
72 #include <sys/filio.h>
73 #include <sys/uio.h>
74 #endif
75 #include <sys/kthread.h>
76 #include <sys/proc.h>
77 #include <sys/conf.h>
78 #include <sys/poll.h>
79 #if defined(__FreeBSD__) && __FreeBSD_version >= 500014
80 #include <sys/selinfo.h>
81 #else
82 #include <sys/select.h>
83 #endif
84 #include <sys/signalvar.h>
85 #include <sys/sysctl.h>
86 #include <sys/uio.h>
87
88 #include <dev/usb/usb.h>
89 #include <dev/usb/usbdi.h>
90 #include <dev/usb/usbdi_util.h>
91
92 #define USBUNIT(d) (minor(d)) /* usb_discover device nodes, kthread */
93 #define USB_DEV_MINOR 255 /* event queue device */
94
95 MALLOC_DEFINE(M_USB, "USB", "USB");
96 MALLOC_DEFINE(M_USBDEV, "USBdev", "USB device");
97 MALLOC_DEFINE(M_USBHC, "USBHC", "USB host controller");
98
99 #if defined(__FreeBSD__)
100 #include "usb_if.h"
101 #endif /* defined(__FreeBSD__) */
102
103 #include <machine/bus.h>
104
105 #include <dev/usb/usbdivar.h>
106 #include <dev/usb/usb_quirks.h>
107
108 #ifdef USB_DEBUG
109 #define DPRINTF(x) if (usbdebug) logprintf x
110 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
111 int usbdebug = 0;
112 #if defined(UHCI_DEBUG) && NUHCI > 0
113 extern int uhcidebug;
114 #endif
115 #if defined(OHCI_DEBUG) && NOHCI > 0
116 extern int ohcidebug;
117 #endif
118 /*
119 * 0 - do usual exploration
120 * 1 - do not use timeout exploration
121 * >1 - do no exploration
122 */
123 int usb_noexplore = 0;
124 #else
125 #define DPRINTF(x)
126 #define DPRINTFN(n,x)
127 #endif
128
129 #ifdef __FreeBSD__
130 /* Define this unconditionally in case a kernel module is loaded that
131 * has been compiled with debugging options.
132 */
133 SYSCTL_NODE(_hw, OID_AUTO, usb, CTLFLAG_RW, 0, "USB debugging");
134 SYSCTL_INT(_hw_usb, OID_AUTO, debug, CTLFLAG_RW,
135 &usbdebug, 0, "usb debug level");
136 #endif
137
138 struct usb_softc {
139 USBBASEDEVICE sc_dev; /* base device */
140 #ifdef __FreeBSD__
141 struct cdev *sc_usbdev; /* /dev/usbN device */
142 TAILQ_ENTRY(usb_softc) sc_coldexplist; /* cold needs-explore list */
143 #endif
144 usbd_bus_handle sc_bus; /* USB controller */
145 struct usbd_port sc_port; /* dummy port for root hub */
146
147 struct proc *sc_event_thread;
148
149 char sc_dying;
150 };
151
152 struct usb_taskq {
153 TAILQ_HEAD(, usb_task) tasks;
154 struct proc *task_thread_proc;
155 const char *name;
156 int taskcreated; /* task thread exists. */
157 };
158
159 static struct usb_taskq usb_taskq[USB_NUM_TASKQS] = {
160 { TAILQ_HEAD_INITIALIZER(usb_taskq[0].tasks), NULL, NULL, 0 },
161 { TAILQ_HEAD_INITIALIZER(usb_taskq[1].tasks), NULL, NULL, 0 }
162 };
163
164 #if defined(__NetBSD__) || defined(__OpenBSD__)
165 dev_type_open(usbopen);
166 dev_type_close(usbclose);
167 dev_type_read(usbread);
168 dev_type_ioctl(usbioctl);
169 dev_type_poll(usbpoll);
170 dev_type_kqfilter(usbkqfilter);
171
172 const struct cdevsw usb_cdevsw = {
173 usbopen, usbclose, usbread, nowrite, usbioctl,
174 nostop, notty, usbpoll, nommap, usbkqfilter, D_OTHER,
175 };
176 #elif defined(__FreeBSD__)
177 d_open_t usbopen;
178 d_close_t usbclose;
179 d_read_t usbread;
180 d_ioctl_t usbioctl;
181 d_poll_t usbpoll;
182
183 struct cdevsw usb_cdevsw = {
184 .d_version = D_VERSION,
185 .d_flags = D_NEEDGIANT,
186 .d_open = usbopen,
187 .d_close = usbclose,
188 .d_read = usbread,
189 .d_ioctl = usbioctl,
190 .d_poll = usbpoll,
191 .d_name = "usb",
192 #if __FreeBSD_version < 500014
193 .d_bmaj = -1
194 #endif
195 };
196 #endif
197
198 Static void usb_discover(void *);
199 #ifdef __FreeBSD__
200 Static bus_child_detached_t usb_child_detached;
201 #endif
202 Static void usb_create_event_thread(void *);
203 Static void usb_event_thread(void *);
204 Static void usb_task_thread(void *);
205
206 Static int usb_nbusses; /* Number of /dev/usbN busses. */
207 #ifdef __FreeBSD__
208 Static struct cdev *usb_dev; /* The /dev/usb device. */
209 /* Busses to explore at the end of boot-time device configuration. */
210 Static TAILQ_HEAD(, usb_softc) usb_coldexplist =
211 TAILQ_HEAD_INITIALIZER(usb_coldexplist);
212 #endif
213
214 #define USB_MAX_EVENTS 100
215 struct usb_event_q {
216 struct usb_event ue;
217 TAILQ_ENTRY(usb_event_q) next;
218 };
219 Static TAILQ_HEAD(, usb_event_q) usb_events =
220 TAILQ_HEAD_INITIALIZER(usb_events);
221 Static int usb_nevents = 0;
222 Static struct selinfo usb_selevent;
223 Static usb_sigproc_ptr usb_async_proc; /* process that wants USB SIGIO */
224 Static int usb_dev_open = 0;
225 Static struct usb_event *usb_alloc_event(void);
226 Static void usb_free_event(struct usb_event *);
227 Static void usb_add_event(int, struct usb_event *);
228
229 Static int usb_get_next_event(struct usb_event *);
230
231 #if defined(__NetBSD__) && defined(COMPAT_30)
232 Static void usb_copy_old_devinfo(struct usb_device_info_old *, const struct usb_device_info *);
233 #endif
234
235 Static const char *usbrev_str[] = USBREV_STR;
236
237 #if defined(__NetBSD__) || defined(__OpenBSD__)
238 USB_DECLARE_DRIVER(usb);
239 #elif defined(__FreeBSD__)
240 USB_DECLARE_DRIVER_INIT(usb,
241 DEVMETHOD(bus_child_detached, usb_child_detached),
242 DEVMETHOD(device_suspend, bus_generic_suspend),
243 DEVMETHOD(device_resume, bus_generic_resume),
244 DEVMETHOD(device_shutdown, bus_generic_shutdown)
245 );
246 MODULE_VERSION(usb, 1);
247 #endif
248
249 USB_MATCH(usb)
250 {
251 DPRINTF(("usbd_match\n"));
252 return (UMATCH_GENERIC);
253 }
254
255 USB_ATTACH(usb)
256 {
257 #if defined(__NetBSD__) || defined(__OpenBSD__)
258 struct usb_softc *sc = (struct usb_softc *)self;
259 #elif defined(__FreeBSD__)
260 struct usb_softc *sc = device_get_softc(self);
261 void *aux = device_get_ivars(self);
262 #endif
263 usbd_device_handle dev;
264 usbd_status err;
265 int usbrev;
266 int speed;
267 struct usb_event *ue;
268
269 #ifdef __FreeBSD__
270 sc->sc_dev = self;
271 #endif
272
273 DPRINTF(("usbd_attach\n"));
274
275 sc->sc_bus = aux;
276 sc->sc_bus->usbctl = sc;
277 sc->sc_port.power = USB_MAX_POWER;
278
279 #if defined(__FreeBSD__)
280 printf("%s", USBDEVNAME(sc->sc_dev));
281 #endif
282 usbrev = sc->sc_bus->usbrev;
283 printf(": USB revision %s", usbrev_str[usbrev]);
284 switch (usbrev) {
285 case USBREV_1_0:
286 case USBREV_1_1:
287 speed = USB_SPEED_FULL;
288 break;
289 case USBREV_2_0:
290 speed = USB_SPEED_HIGH;
291 break;
292 default:
293 printf(", not supported\n");
294 sc->sc_dying = 1;
295 USB_ATTACH_ERROR_RETURN;
296 }
297 printf("\n");
298
299 /* Make sure not to use tsleep() if we are cold booting. */
300 if (cold)
301 sc->sc_bus->use_polling++;
302
303 ue = usb_alloc_event();
304 ue->u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
305 usb_add_event(USB_EVENT_CTRLR_ATTACH, ue);
306
307 #ifdef USB_USE_SOFTINTR
308 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
309 /* XXX we should have our own level */
310 sc->sc_bus->soft = softintr_establish(IPL_SOFTNET,
311 sc->sc_bus->methods->soft_intr, sc->sc_bus);
312 if (sc->sc_bus->soft == NULL) {
313 printf("%s: can't register softintr\n", USBDEVNAME(sc->sc_dev));
314 sc->sc_dying = 1;
315 USB_ATTACH_ERROR_RETURN;
316 }
317 #else
318 usb_callout_init(sc->sc_bus->softi);
319 #endif
320 #endif
321
322 err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, speed, 0,
323 &sc->sc_port);
324 if (!err) {
325 dev = sc->sc_port.device;
326 if (dev->hub == NULL) {
327 sc->sc_dying = 1;
328 printf("%s: root device is not a hub\n",
329 USBDEVNAME(sc->sc_dev));
330 USB_ATTACH_ERROR_RETURN;
331 }
332 sc->sc_bus->root_hub = dev;
333 #if 1
334 /*
335 * Turning this code off will delay attachment of USB devices
336 * until the USB event thread is running, which means that
337 * the keyboard will not work until after cold boot.
338 */
339 #if defined(__FreeBSD__)
340 if (cold) {
341 /* Explore high-speed busses before others. */
342 if (speed == USB_SPEED_HIGH)
343 dev->hub->explore(sc->sc_bus->root_hub);
344 else
345 TAILQ_INSERT_TAIL(&usb_coldexplist, sc,
346 sc_coldexplist);
347 }
348 #else
349 if (cold && (device_cfdata(&sc->sc_dev)->cf_flags & 1))
350 dev->hub->explore(sc->sc_bus->root_hub);
351 #endif
352 #endif
353 } else {
354 printf("%s: root hub problem, error=%d\n",
355 USBDEVNAME(sc->sc_dev), err);
356 sc->sc_dying = 1;
357 }
358 if (cold)
359 sc->sc_bus->use_polling--;
360
361 config_pending_incr();
362 #if defined(__NetBSD__) || defined(__OpenBSD__)
363 usb_kthread_create(usb_create_event_thread, sc);
364 #endif
365
366 usb_nbusses++;
367 #if defined(__FreeBSD__)
368 usb_create_event_thread(sc);
369 /* The per controller devices (used for usb_discover) */
370 /* XXX This is redundant now, but old usbd's will want it */
371 sc->sc_usbdev = make_dev(&usb_cdevsw, device_get_unit(self), UID_ROOT,
372 GID_OPERATOR, 0660, "usb%d", device_get_unit(self));
373 if (usb_nbusses == 1) {
374 /* The device spitting out events */
375 usb_dev = make_dev(&usb_cdevsw, USB_DEV_MINOR, UID_ROOT,
376 GID_OPERATOR, 0660, "usb");
377 }
378 #endif
379
380 USB_ATTACH_SUCCESS_RETURN;
381 }
382
383 static const char *taskq_names[] = USB_TASKQ_NAMES;
384
385 void
386 usb_create_event_thread(void *arg)
387 {
388 struct usb_softc *sc = arg;
389 struct usb_taskq *taskq;
390 int i;
391
392 if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
393 "%s", USBDEVNAME(sc->sc_dev))) {
394 printf("%s: unable to create event thread for\n",
395 USBDEVNAME(sc->sc_dev));
396 panic("usb_create_event_thread");
397 }
398 for (i = 0; i < USB_NUM_TASKQS; i++) {
399 taskq = &usb_taskq[i];
400
401 if (taskq->taskcreated == 0) {
402 taskq->taskcreated = 1;
403 taskq->name = taskq_names[i];
404 if (usb_kthread_create2(usb_task_thread, taskq,
405 &taskq->task_thread_proc, taskq->name)) {
406 printf("unable to create task thread: %s\n",
407 taskq->name);
408 panic("usb_create_event_thread task");
409 }
410 }
411 }
412 }
413
414 /*
415 * Add a task to be performed by the task thread. This function can be
416 * called from any context and the task will be executed in a process
417 * context ASAP.
418 */
419 void
420 usb_add_task(usbd_device_handle dev, struct usb_task *task, int queue)
421 {
422 struct usb_taskq *taskq;
423 int s;
424
425 s = splusb();
426 taskq = &usb_taskq[queue];
427 if (task->queue == -1) {
428 DPRINTFN(2,("usb_add_task: task=%p\n", task));
429 TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
430 task->queue = queue;
431 } else {
432 DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
433 }
434 wakeup(&taskq->tasks);
435 splx(s);
436 }
437
438 void
439 usb_rem_task(usbd_device_handle dev, struct usb_task *task)
440 {
441 struct usb_taskq *taskq;
442 int s;
443
444 s = splusb();
445 if (task->queue != -1) {
446 taskq = &usb_taskq[task->queue];
447 TAILQ_REMOVE(&taskq->tasks, task, next);
448 task->queue = -1;
449 }
450 splx(s);
451 }
452
453 void
454 usb_event_thread(void *arg)
455 {
456 static int newthread_wchan;
457 struct usb_softc *sc = arg;
458
459 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
460 mtx_lock(&Giant);
461 #endif
462
463 DPRINTF(("usb_event_thread: start\n"));
464
465 /*
466 * In case this controller is a companion controller to an
467 * EHCI controller we need to wait until the EHCI controller
468 * has grabbed the port. What we do here is wait until no new
469 * USB threads have been created in a while. XXX we actually
470 * just want to wait for the PCI slot to be fully scanned.
471 *
472 * Note that when you `kldload usb' it actually attaches the
473 * devices in order that the drivers appear in the kld, not the
474 * normal PCI order, since the addition of each driver within
475 * usb.ko (ohci, ehci etc.) causes a separate PCI bus re-scan.
476 */
477 wakeup(&newthread_wchan);
478 for (;;) {
479 if (tsleep(&newthread_wchan , PWAIT, "usbets", hz * 4) != 0)
480 break;
481 }
482
483 /* Make sure first discover does something. */
484 sc->sc_bus->needs_explore = 1;
485 usb_discover(sc);
486 config_pending_decr();
487
488 while (!sc->sc_dying) {
489 #ifdef USB_DEBUG
490 if (usb_noexplore < 2)
491 #endif
492 usb_discover(sc);
493 #ifdef USB_DEBUG
494 (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
495 usb_noexplore ? 0 : hz * 60);
496 #else
497 (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
498 hz * 60);
499 #endif
500 DPRINTFN(2,("usb_event_thread: woke up\n"));
501 }
502 sc->sc_event_thread = NULL;
503
504 /* In case parent is waiting for us to exit. */
505 wakeup(sc);
506
507 DPRINTF(("usb_event_thread: exit\n"));
508 kthread_exit(0);
509 }
510
511 void
512 usb_task_thread(void *arg)
513 {
514 struct usb_task *task;
515 struct usb_taskq *taskq;
516 int s;
517
518 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
519 mtx_lock(&Giant);
520 #endif
521
522 taskq = arg;
523 DPRINTF(("usb_task_thread: start taskq %s\n", taskq->name));
524
525 s = splusb();
526
527 while (usb_nbusses > 0) {
528 task = TAILQ_FIRST(&taskq->tasks);
529 if (task == NULL) {
530 tsleep(&taskq->tasks, PWAIT, "usbtsk", 0);
531 task = TAILQ_FIRST(&taskq->tasks);
532 }
533 DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
534 if (task != NULL) {
535 TAILQ_REMOVE(&taskq->tasks, task, next);
536 task->queue = -1;
537 splx(s);
538 task->fun(task->arg);
539 s = splusb();
540 }
541 }
542
543 splx(s);
544
545 taskq->taskcreated = 0;
546 wakeup(&taskq->taskcreated);
547
548 DPRINTF(("usb_event_thread: exit\n"));
549 kthread_exit(0);
550 }
551
552 #if defined(__NetBSD__) || defined(__OpenBSD__)
553 int
554 usbctlprint(void *aux, const char *pnp)
555 {
556 /* only "usb"es can attach to host controllers */
557 if (pnp)
558 aprint_normal("usb at %s", pnp);
559
560 return (UNCONF);
561 }
562 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
563
564 int
565 usbopen(usb_cdev_t dev, int flag, int mode, usb_proc_ptr p)
566 {
567 int unit = USBUNIT(dev);
568 struct usb_softc *sc;
569
570 if (unit == USB_DEV_MINOR) {
571 if (usb_dev_open)
572 return (EBUSY);
573 usb_dev_open = 1;
574 usb_async_proc = 0;
575 return (0);
576 }
577
578 USB_GET_SC_OPEN(usb, unit, sc);
579
580 if (sc->sc_dying)
581 return (EIO);
582
583 return (0);
584 }
585
586 int
587 usbread(usb_cdev_t dev, struct uio *uio, int flag)
588 {
589 struct usb_event *ue;
590 #if defined(__NetBSD__) && defined(COMPAT_30)
591 struct usb_event_old *ueo = NULL; /* XXXGCC */
592 int useold = 0;
593 #endif
594 int s, error, n;
595
596 if (USBUNIT(dev) != USB_DEV_MINOR)
597 return (ENXIO);
598
599 switch (uio->uio_resid) {
600 #if defined(__NetBSD__) && defined(COMPAT_30)
601 case sizeof(struct usb_event_old):
602 ueo = malloc(sizeof(struct usb_event_old), M_USBDEV,
603 M_WAITOK|M_ZERO);
604 useold = 1;
605 /* FALLTHRU */
606 #endif
607 case sizeof(struct usb_event):
608 ue = usb_alloc_event();
609 break;
610 default:
611 return (EINVAL);
612 }
613
614 error = 0;
615 s = splusb();
616 for (;;) {
617 n = usb_get_next_event(ue);
618 if (n != 0)
619 break;
620 if (flag &
621 #ifdef __FreeBSD__
622 O_NONBLOCK
623 #else
624 IO_NDELAY
625 #endif
626 ) {
627 error = EWOULDBLOCK;
628 break;
629 }
630 error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
631 if (error)
632 break;
633 }
634 splx(s);
635 if (!error) {
636 #if defined(__NetBSD__) && defined(COMPAT_30)
637 if (useold) { /* copy fields to old struct */
638 ueo->ue_type = ue->ue_type;
639 memcpy(&ueo->ue_time, &ue->ue_time,
640 sizeof(struct timespec));
641 switch (ue->ue_type) {
642 case USB_EVENT_DEVICE_ATTACH:
643 case USB_EVENT_DEVICE_DETACH:
644 usb_copy_old_devinfo(&ueo->u.ue_device, &ue->u.ue_device);
645 break;
646
647 case USB_EVENT_CTRLR_ATTACH:
648 case USB_EVENT_CTRLR_DETACH:
649 ueo->u.ue_ctrlr.ue_bus=ue->u.ue_ctrlr.ue_bus;
650 break;
651
652 case USB_EVENT_DRIVER_ATTACH:
653 case USB_EVENT_DRIVER_DETACH:
654 ueo->u.ue_driver.ue_cookie=ue->u.ue_driver.ue_cookie;
655 memcpy(ueo->u.ue_driver.ue_devname,
656 ue->u.ue_driver.ue_devname,
657 sizeof(ue->u.ue_driver.ue_devname));
658 break;
659 default:
660 ;
661 }
662
663 error = uiomove((void *)ueo, uio->uio_resid, uio);
664 } else
665 #endif
666 error = uiomove((void *)ue, uio->uio_resid, uio);
667 }
668 usb_free_event(ue);
669 #if defined(__NetBSD__) && defined(COMPAT_30)
670 if (useold)
671 free(ueo, M_USBDEV);
672 #endif
673
674 return (error);
675 }
676
677 int
678 usbclose(usb_cdev_t dev, int flag, int mode, usb_proc_ptr p)
679 {
680 int unit = USBUNIT(dev);
681
682 if (unit == USB_DEV_MINOR) {
683 usb_async_proc = 0;
684 usb_dev_open = 0;
685 }
686
687 return (0);
688 }
689
690 int
691 usbioctl(usb_cdev_t devt, u_long cmd, usb_ioctlarg_t data, int flag,
692 usb_proc_ptr p)
693 {
694 struct usb_softc *sc;
695 int unit = USBUNIT(devt);
696
697 if (unit == USB_DEV_MINOR) {
698 switch (cmd) {
699 case FIONBIO:
700 /* All handled in the upper FS layer. */
701 return (0);
702
703 case FIOASYNC:
704 if (*(int *)data)
705 #if defined(__NetBSD__) && __NetBSD_Version__ >= 399001400
706 usb_async_proc = p->l_proc;
707 #elif defined(__FreeBSD__) && __FreeBSD_version >= 500000
708 usb_async_proc = p->td_proc;
709 #else
710 usb_async_proc = p;
711 #endif
712 else
713 usb_async_proc = 0;
714 return (0);
715
716 default:
717 return (EINVAL);
718 }
719 }
720
721 USB_GET_SC(usb, unit, sc);
722
723 if (sc->sc_dying)
724 return (EIO);
725
726 switch (cmd) {
727 #if defined(USB_DEBUG)
728 case USB_SETDEBUG:
729 if (!(flag & FWRITE))
730 return (EBADF);
731 usbdebug = ((*(int *)data) & 0x000000ff);
732 #if defined(UHCI_DEBUG) && NUHCI > 0
733 uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
734 #endif
735 #if defined(OHCI_DEBUG) && NOHCI > 0
736 ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
737 #endif
738 break;
739 #endif /* USB_DEBUG */
740 #if defined(__FreeBSD__)
741 /* This part should be deleted */
742 case USB_DISCOVER:
743 break;
744 #endif
745 case USB_REQUEST:
746 {
747 struct usb_ctl_request *ur = (void *)data;
748 int len = UGETW(ur->ucr_request.wLength);
749 struct iovec iov;
750 struct uio uio;
751 void *ptr = 0;
752 int addr = ur->ucr_addr;
753 usbd_status err;
754 int error = 0;
755
756 if (!(flag & FWRITE))
757 return (EBADF);
758
759 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
760 if (len < 0 || len > 32768)
761 return (EINVAL);
762 if (addr < 0 || addr >= USB_MAX_DEVICES ||
763 sc->sc_bus->devices[addr] == 0)
764 return (EINVAL);
765 if (len != 0) {
766 iov.iov_base = (void *)ur->ucr_data;
767 iov.iov_len = len;
768 uio.uio_iov = &iov;
769 uio.uio_iovcnt = 1;
770 uio.uio_resid = len;
771 uio.uio_offset = 0;
772 uio.uio_rw =
773 ur->ucr_request.bmRequestType & UT_READ ?
774 UIO_READ : UIO_WRITE;
775 USB_UIO_SET_PROC(&uio, p);
776 ptr = malloc(len, M_TEMP, M_WAITOK);
777 if (uio.uio_rw == UIO_WRITE) {
778 error = uiomove(ptr, len, &uio);
779 if (error)
780 goto ret;
781 }
782 }
783 err = usbd_do_request_flags(sc->sc_bus->devices[addr],
784 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
785 USBD_DEFAULT_TIMEOUT);
786 if (err) {
787 error = EIO;
788 goto ret;
789 }
790 if (len != 0) {
791 if (uio.uio_rw == UIO_READ) {
792 error = uiomove(ptr, len, &uio);
793 if (error)
794 goto ret;
795 }
796 }
797 ret:
798 if (ptr)
799 free(ptr, M_TEMP);
800 return (error);
801 }
802
803 case USB_DEVICEINFO:
804 {
805 usbd_device_handle dev;
806 struct usb_device_info *di = (void *)data;
807 int addr = di->udi_addr;
808
809 if (addr < 1 || addr >= USB_MAX_DEVICES)
810 return EINVAL;
811 if ((dev = sc->sc_bus->devices[addr]) == NULL)
812 return ENXIO;
813 usbd_fill_deviceinfo(dev, di, 1);
814 break;
815 }
816
817 #if defined(__NetBSD__) && defined(COMPAT_30)
818 case USB_DEVICEINFO_OLD:
819 {
820 usbd_device_handle dev;
821 struct usb_device_info_old *di = (void *)data;
822 int addr = di->udi_addr;
823
824 if (addr < 1 || addr >= USB_MAX_DEVICES)
825 return EINVAL;
826 if ((dev = sc->sc_bus->devices[addr]) == NULL)
827 return ENXIO;
828 usbd_fill_deviceinfo_old(dev, di, 1);
829 break;
830 }
831 #endif
832
833 case USB_DEVICESTATS:
834 *(struct usb_device_stats *)data = sc->sc_bus->stats;
835 break;
836
837 default:
838 return (EINVAL);
839 }
840 return (0);
841 }
842
843 int
844 usbpoll(usb_cdev_t dev, int events, usb_proc_ptr p)
845 {
846 int revents, mask, s;
847 int unit = USBUNIT(dev);
848
849 if (unit == USB_DEV_MINOR) {
850 revents = 0;
851 mask = POLLIN | POLLRDNORM;
852
853 s = splusb();
854 if (events & mask && usb_nevents > 0)
855 revents |= events & mask;
856 if (revents == 0 && events & mask)
857 selrecord(p, &usb_selevent);
858 splx(s);
859
860 return (revents);
861 } else {
862 return (0);
863 }
864 }
865
866 #if defined(__NetBSD__) || defined(__OpenBSD__)
867 static void
868 filt_usbrdetach(struct knote *kn)
869 {
870 int s;
871
872 s = splusb();
873 SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext);
874 splx(s);
875 }
876
877 static int
878 filt_usbread(struct knote *kn, long hint)
879 {
880
881 if (usb_nevents == 0)
882 return (0);
883
884 kn->kn_data = sizeof(struct usb_event);
885 return (1);
886 }
887
888 static const struct filterops usbread_filtops =
889 { 1, NULL, filt_usbrdetach, filt_usbread };
890
891 int
892 usbkqfilter(dev_t dev, struct knote *kn)
893 {
894 struct klist *klist;
895 int s;
896
897 switch (kn->kn_filter) {
898 case EVFILT_READ:
899 if (minor(dev) != USB_DEV_MINOR)
900 return (1);
901 klist = &usb_selevent.sel_klist;
902 kn->kn_fop = &usbread_filtops;
903 break;
904
905 default:
906 return (1);
907 }
908
909 kn->kn_hook = NULL;
910
911 s = splusb();
912 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
913 splx(s);
914
915 return (0);
916 }
917 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
918
919 /* Explore device tree from the root. */
920 Static void
921 usb_discover(void *v)
922 {
923 struct usb_softc *sc = v;
924 /* splxxx should be changed to mutexes for preemption safety some day */
925 int s;
926
927 DPRINTFN(2,("usb_discover\n"));
928 #ifdef USB_DEBUG
929 if (usb_noexplore > 1)
930 return;
931 #endif
932
933 /*
934 * We need mutual exclusion while traversing the device tree,
935 * but this is guaranteed since this function is only called
936 * from the event thread for the controller.
937 */
938 s = splusb();
939 while (sc->sc_bus->needs_explore && !sc->sc_dying) {
940 sc->sc_bus->needs_explore = 0;
941 splx(s);
942 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
943 s = splusb();
944 }
945 splx(s);
946 }
947
948 void
949 usb_needs_explore(usbd_device_handle dev)
950 {
951 DPRINTFN(2,("usb_needs_explore\n"));
952 dev->bus->needs_explore = 1;
953 wakeup(&dev->bus->needs_explore);
954 }
955
956 void
957 usb_needs_reattach(usbd_device_handle dev)
958 {
959 DPRINTFN(2,("usb_needs_reattach\n"));
960 dev->powersrc->reattach = 1;
961 dev->bus->needs_explore = 1;
962 wakeup(&dev->bus->needs_explore);
963 }
964
965 /* Called at splusb() */
966 int
967 usb_get_next_event(struct usb_event *ue)
968 {
969 struct usb_event_q *ueq;
970
971 if (usb_nevents <= 0)
972 return (0);
973 ueq = TAILQ_FIRST(&usb_events);
974 #ifdef DIAGNOSTIC
975 if (ueq == NULL) {
976 printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
977 usb_nevents = 0;
978 return (0);
979 }
980 #endif
981 if (ue)
982 *ue = ueq->ue;
983 TAILQ_REMOVE(&usb_events, ueq, next);
984 usb_free_event((struct usb_event *)(void *)ueq);
985 usb_nevents--;
986 return (1);
987 }
988
989 void
990 usbd_add_dev_event(int type, usbd_device_handle udev)
991 {
992 struct usb_event *ue = usb_alloc_event();
993
994 usbd_fill_deviceinfo(udev, &ue->u.ue_device, USB_EVENT_IS_ATTACH(type));
995 usb_add_event(type, ue);
996 }
997
998 void
999 usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
1000 {
1001 struct usb_event *ue = usb_alloc_event();
1002
1003 ue->u.ue_driver.ue_cookie = udev->cookie;
1004 strncpy(ue->u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
1005 sizeof ue->u.ue_driver.ue_devname);
1006 usb_add_event(type, ue);
1007 }
1008
1009 Static struct usb_event *
1010 usb_alloc_event(void)
1011 {
1012 /* Yes, this is right; we allocate enough so that we can use it later */
1013 return malloc(sizeof(struct usb_event_q), M_USBDEV, M_WAITOK|M_ZERO);
1014 }
1015
1016 Static void
1017 usb_free_event(struct usb_event *uep)
1018 {
1019 free(uep, M_USBDEV);
1020 }
1021
1022 Static void
1023 usb_add_event(int type, struct usb_event *uep)
1024 {
1025 struct usb_event_q *ueq;
1026 struct timeval thetime;
1027 int s;
1028
1029 microtime(&thetime);
1030 /* Don't want to wait here inside splusb() */
1031 ueq = (struct usb_event_q *)(void *)uep;
1032 ueq->ue = *uep;
1033 ueq->ue.ue_type = type;
1034 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
1035
1036 s = splusb();
1037 if (USB_EVENT_IS_DETACH(type)) {
1038 struct usb_event_q *ueqi, *ueqi_next;
1039
1040 for (ueqi = TAILQ_FIRST(&usb_events); ueqi; ueqi = ueqi_next) {
1041 ueqi_next = TAILQ_NEXT(ueqi, next);
1042 if (ueqi->ue.u.ue_driver.ue_cookie.cookie ==
1043 uep->u.ue_device.udi_cookie.cookie) {
1044 TAILQ_REMOVE(&usb_events, ueqi, next);
1045 free(ueqi, M_USBDEV);
1046 usb_nevents--;
1047 ueqi_next = TAILQ_FIRST(&usb_events);
1048 }
1049 }
1050 }
1051 if (usb_nevents >= USB_MAX_EVENTS) {
1052 /* Too many queued events, drop an old one. */
1053 DPRINTFN(-1,("usb: event dropped\n"));
1054 (void)usb_get_next_event(0);
1055 }
1056 TAILQ_INSERT_TAIL(&usb_events, ueq, next);
1057 usb_nevents++;
1058 wakeup(&usb_events);
1059 #if defined(__NetBSD__) || defined(__OpenBSD__)
1060 selnotify(&usb_selevent, 0);
1061 #elif defined(__FreeBSD__)
1062 selwakeuppri(&usb_selevent, PZERO);
1063 #endif
1064 if (usb_async_proc != NULL) {
1065 USB_PROC_LOCK(usb_async_proc);
1066 psignal(usb_async_proc, SIGIO);
1067 USB_PROC_UNLOCK(usb_async_proc);
1068 }
1069 splx(s);
1070 }
1071
1072 void
1073 usb_schedsoftintr(usbd_bus_handle bus)
1074 {
1075 DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
1076 #ifdef USB_USE_SOFTINTR
1077 if (bus->use_polling) {
1078 bus->methods->soft_intr(bus);
1079 } else {
1080 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
1081 softintr_schedule(bus->soft);
1082 #else
1083 if (!callout_pending(&bus->softi))
1084 callout_reset(&bus->softi, 0, bus->methods->soft_intr,
1085 bus);
1086 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
1087 }
1088 #else
1089 bus->methods->soft_intr(bus);
1090 #endif /* USB_USE_SOFTINTR */
1091 }
1092
1093 #if defined(__NetBSD__) || defined(__OpenBSD__)
1094 int
1095 usb_activate(device_ptr_t self, enum devact act)
1096 {
1097 struct usb_softc *sc = (struct usb_softc *)self;
1098 usbd_device_handle dev = sc->sc_port.device;
1099 int i, rv = 0;
1100
1101 switch (act) {
1102 case DVACT_ACTIVATE:
1103 return (EOPNOTSUPP);
1104
1105 case DVACT_DEACTIVATE:
1106 sc->sc_dying = 1;
1107 if (dev != NULL && dev->cdesc != NULL && dev->subdevs != NULL) {
1108 for (i = 0; dev->subdevs[i]; i++)
1109 rv |= config_deactivate(dev->subdevs[i]);
1110 }
1111 break;
1112 }
1113 return (rv);
1114 }
1115 #endif
1116
1117 USB_DETACH(usb)
1118 {
1119 USB_DETACH_START(usb, sc);
1120 struct usb_event *ue;
1121 struct usb_taskq *taskq;
1122 int i;
1123
1124 DPRINTF(("usb_detach: start\n"));
1125
1126 sc->sc_dying = 1;
1127
1128 /* Make all devices disconnect. */
1129 if (sc->sc_port.device != NULL)
1130 usb_disconnect_port(&sc->sc_port, self);
1131
1132 /* Kill off event thread. */
1133 if (sc->sc_event_thread != NULL) {
1134 wakeup(&sc->sc_bus->needs_explore);
1135 if (tsleep(sc, PWAIT, "usbdet", hz * 60))
1136 printf("%s: event thread didn't die\n",
1137 USBDEVNAME(sc->sc_dev));
1138 DPRINTF(("usb_detach: event thread dead\n"));
1139 }
1140
1141 #ifdef __FreeBSD__
1142 destroy_dev(sc->sc_usbdev);
1143 #endif
1144 if (--usb_nbusses == 0) {
1145 #ifdef __FreeBSD__
1146 destroy_dev(usb_dev);
1147 usb_dev = NULL;
1148 #endif
1149 for (i = 0; i < USB_NUM_TASKQS; i++) {
1150 taskq = &usb_taskq[i];
1151 wakeup(&taskq->tasks);
1152 if (tsleep(&taskq->taskcreated, PWAIT, "usbtdt",
1153 hz * 60)) {
1154 printf("usb task thread %s didn't die\n",
1155 taskq->name);
1156 }
1157 }
1158 }
1159
1160 #ifdef USB_USE_SOFTINTR
1161 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
1162 if (sc->sc_bus->soft != NULL) {
1163 softintr_disestablish(sc->sc_bus->soft);
1164 sc->sc_bus->soft = NULL;
1165 }
1166 #else
1167 callout_stop(&sc->sc_bus->softi);
1168 #endif
1169 #endif
1170
1171 ue = usb_alloc_event();
1172 ue->u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
1173 usb_add_event(USB_EVENT_CTRLR_DETACH, ue);
1174
1175 return (0);
1176 }
1177
1178 #if defined(__NetBSD__) && defined(COMPAT_30)
1179 Static void
1180 usb_copy_old_devinfo(struct usb_device_info_old *uo,
1181 const struct usb_device_info *ue)
1182 {
1183 const unsigned char *p;
1184 unsigned char *q;
1185 int i, n;
1186
1187 uo->udi_bus = ue->udi_bus;
1188 uo->udi_addr = ue->udi_addr;
1189 uo->udi_cookie = ue->udi_cookie;
1190 for (i = 0, p = (const unsigned char *)ue->udi_product,
1191 q = (unsigned char *)uo->udi_product;
1192 *p && i < USB_MAX_STRING_LEN - 1; p++) {
1193 if (*p < 0x80)
1194 q[i++] = *p;
1195 else {
1196 q[i++] = '?';
1197 if ((*p & 0xe0) == 0xe0)
1198 p++;
1199 p++;
1200 }
1201 }
1202 q[i] = 0;
1203
1204 for (i = 0, p = ue->udi_vendor, q = uo->udi_vendor;
1205 *p && i < USB_MAX_STRING_LEN - 1; p++) {
1206 if (* p < 0x80)
1207 q[i++] = *p;
1208 else {
1209 q[i++] = '?';
1210 p++;
1211 if ((*p & 0xe0) == 0xe0)
1212 p++;
1213 }
1214 }
1215 q[i] = 0;
1216
1217 memcpy(uo->udi_release, ue->udi_release, sizeof(uo->udi_release));
1218
1219 uo->udi_productNo = ue->udi_productNo;
1220 uo->udi_vendorNo = ue->udi_vendorNo;
1221 uo->udi_releaseNo = ue->udi_releaseNo;
1222 uo->udi_class = ue->udi_class;
1223 uo->udi_subclass = ue->udi_subclass;
1224 uo->udi_protocol = ue->udi_protocol;
1225 uo->udi_config = ue->udi_config;
1226 uo->udi_speed = ue->udi_speed;
1227 uo->udi_power = ue->udi_power;
1228 uo->udi_nports = ue->udi_nports;
1229
1230 for (n=0; n<USB_MAX_DEVNAMES; n++)
1231 memcpy(uo->udi_devnames[n],
1232 ue->udi_devnames[n], USB_MAX_DEVNAMELEN);
1233 memcpy(uo->udi_ports, ue->udi_ports, sizeof(uo->udi_ports));
1234 }
1235 #endif
1236
1237 #if defined(__FreeBSD__)
1238 Static void
1239 usb_child_detached(device_t self, device_t child)
1240 {
1241 struct usb_softc *sc = device_get_softc(self);
1242
1243 /* XXX, should check it is the right device. */
1244 sc->sc_port.device = NULL;
1245 }
1246
1247 /* Explore USB busses at the end of device configuration. */
1248 Static void
1249 usb_cold_explore(void *arg)
1250 {
1251 struct usb_softc *sc;
1252
1253 USB_KASSERT2(cold || TAILQ_EMPTY(&usb_coldexplist),
1254 ("usb_cold_explore: busses to explore when !cold"));
1255 while (!TAILQ_EMPTY(&usb_coldexplist)) {
1256 sc = TAILQ_FIRST(&usb_coldexplist);
1257 TAILQ_REMOVE(&usb_coldexplist, sc, sc_coldexplist);
1258
1259 sc->sc_bus->use_polling++;
1260 sc->sc_port.device->hub->explore(sc->sc_bus->root_hub);
1261 sc->sc_bus->use_polling--;
1262 }
1263 }
1264
1265 DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
1266 DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
1267 DRIVER_MODULE(usb, ehci, usb_driver, usb_devclass, 0, 0);
1268 DRIVER_MODULE(usb, slhci, usb_driver, usb_devclass, 0, 0);
1269 SYSINIT(usb_cold_explore, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE,
1270 usb_cold_explore, NULL);
1271 #endif
1272