usb.c revision 1.136 1 /* $NetBSD: usb.c,v 1.136 2012/11/27 04:54:58 mrg Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2002, 2008, 2012 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 and Matthew R. Green (mrg (at) eterna.com.au).
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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * USB specifications and other documentation can be found at
35 * http://www.usb.org/developers/docs/ and
36 * http://www.usb.org/developers/devclass_docs/
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.136 2012/11/27 04:54:58 mrg Exp $");
41
42 #include "opt_compat_netbsd.h"
43 #include "opt_usb.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/device.h>
50 #include <sys/kthread.h>
51 #include <sys/proc.h>
52 #include <sys/conf.h>
53 #include <sys/fcntl.h>
54 #include <sys/poll.h>
55 #include <sys/select.h>
56 #include <sys/vnode.h>
57 #include <sys/signalvar.h>
58 #include <sys/intr.h>
59 #include <sys/module.h>
60 #include <sys/mutex.h>
61 #include <sys/bus.h>
62 #include <sys/once.h>
63
64 #include <dev/usb/usb.h>
65 #include <dev/usb/usbdi.h>
66 #include <dev/usb/usbdi_util.h>
67 #include <dev/usb/usbdivar.h>
68 #include <dev/usb/usb_verbose.h>
69 #include <dev/usb/usb_quirks.h>
70
71 #define USB_DEV_MINOR 255
72
73 #ifdef USB_DEBUG
74 #define DPRINTF(x) if (usbdebug) printf x
75 #define DPRINTFN(n,x) if (usbdebug>(n)) printf x
76 int usbdebug = 0;
77 /*
78 * 0 - do usual exploration
79 * 1 - do not use timeout exploration
80 * >1 - do no exploration
81 */
82 int usb_noexplore = 0;
83 #else
84 #define DPRINTF(x)
85 #define DPRINTFN(n,x)
86 #define usb_noexplore 0
87 #endif
88
89 struct usb_softc {
90 #if 0
91 device_t sc_dev; /* base device */
92 #endif
93 usbd_bus_handle sc_bus; /* USB controller */
94 struct usbd_port sc_port; /* dummy port for root hub */
95
96 struct lwp *sc_event_thread;
97
98 char sc_dying;
99 };
100
101 struct usb_taskq {
102 TAILQ_HEAD(, usb_task) tasks;
103 kmutex_t lock;
104 kcondvar_t cv;
105 struct lwp *task_thread_lwp;
106 const char *name;
107 };
108
109 static struct usb_taskq usb_taskq[USB_NUM_TASKQS];
110
111 dev_type_open(usbopen);
112 dev_type_close(usbclose);
113 dev_type_read(usbread);
114 dev_type_ioctl(usbioctl);
115 dev_type_poll(usbpoll);
116 dev_type_kqfilter(usbkqfilter);
117
118 const struct cdevsw usb_cdevsw = {
119 usbopen, usbclose, usbread, nowrite, usbioctl,
120 nostop, notty, usbpoll, nommap, usbkqfilter, D_OTHER,
121 };
122
123 Static void usb_discover(struct usb_softc *);
124 Static void usb_create_event_thread(device_t);
125 Static void usb_event_thread(void *);
126 Static void usb_task_thread(void *);
127
128 #define USB_MAX_EVENTS 100
129 struct usb_event_q {
130 struct usb_event ue;
131 SIMPLEQ_ENTRY(usb_event_q) next;
132 };
133 Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
134 SIMPLEQ_HEAD_INITIALIZER(usb_events);
135 Static int usb_nevents = 0;
136 Static struct selinfo usb_selevent;
137 Static kmutex_t usb_event_lock;
138 Static kcondvar_t usb_event_cv;
139 Static proc_t *usb_async_proc; /* process that wants USB SIGIO */
140 Static void *usb_async_sih;
141 Static int usb_dev_open = 0;
142 Static struct usb_event *usb_alloc_event(void);
143 Static void usb_free_event(struct usb_event *);
144 Static void usb_add_event(int, struct usb_event *);
145 Static int usb_get_next_event(struct usb_event *);
146 Static void usb_async_intr(void *);
147 Static void usb_soft_intr(void *);
148
149 #ifdef COMPAT_30
150 Static void usb_copy_old_devinfo(struct usb_device_info_old *, const struct usb_device_info *);
151 #endif
152
153 Static const char *usbrev_str[] = USBREV_STR;
154
155 static int usb_match(device_t, cfdata_t, void *);
156 static void usb_attach(device_t, device_t, void *);
157 static int usb_detach(device_t, int);
158 static int usb_activate(device_t, enum devact);
159 static void usb_childdet(device_t, device_t);
160 static int usb_once_init(void);
161 static void usb_doattach(device_t);
162
163 extern struct cfdriver usb_cd;
164
165 CFATTACH_DECL3_NEW(usb, sizeof(struct usb_softc),
166 usb_match, usb_attach, usb_detach, usb_activate, NULL, usb_childdet,
167 DVF_DETACH_SHUTDOWN);
168
169 static const char *taskq_names[] = USB_TASKQ_NAMES;
170
171 int
172 usb_match(device_t parent, cfdata_t match, void *aux)
173 {
174 DPRINTF(("usbd_match\n"));
175 return (UMATCH_GENERIC);
176 }
177
178 void
179 usb_attach(device_t parent, device_t self, void *aux)
180 {
181 static ONCE_DECL(init_control);
182 struct usb_softc *sc = device_private(self);
183 bool mpsafe;
184 int usbrev;
185
186 sc->sc_bus = aux;
187 mpsafe = sc->sc_bus->methods->get_lock ? true : false;
188 usbrev = sc->sc_bus->usbrev;
189
190 aprint_naive("\n");
191 aprint_normal(": USB revision %s", usbrev_str[usbrev]);
192 switch (usbrev) {
193 case USBREV_1_0:
194 case USBREV_1_1:
195 case USBREV_2_0:
196 break;
197 default:
198 aprint_error(", not supported\n");
199 sc->sc_dying = 1;
200 return;
201 }
202 aprint_normal("\n");
203
204 if (mpsafe)
205 sc->sc_bus->methods->get_lock(sc->sc_bus, &sc->sc_bus->lock);
206 else
207 sc->sc_bus->lock = NULL;
208
209 RUN_ONCE(&init_control, usb_once_init);
210 config_interrupts(self, usb_doattach);
211 }
212
213 static int
214 usb_once_init(void)
215 {
216 struct usb_taskq *taskq;
217 int i;
218
219 selinit(&usb_selevent);
220 mutex_init(&usb_event_lock, MUTEX_DEFAULT, IPL_NONE);
221 cv_init(&usb_event_cv, "usbrea");
222
223 for (i = 0; i < USB_NUM_TASKQS; i++) {
224 taskq = &usb_taskq[i];
225
226 TAILQ_INIT(&taskq->tasks);
227 /*
228 * Since USB tasks are callable from any context, we have to
229 * make this lock a spinlock.
230 */
231 mutex_init(&taskq->lock, MUTEX_DEFAULT, IPL_USB);
232 cv_init(&taskq->cv, "usbtsk");
233 taskq->name = taskq_names[i];
234 if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
235 usb_task_thread, taskq, &taskq->task_thread_lwp,
236 "%s", taskq->name)) {
237 printf("unable to create task thread: %s\n", taskq->name);
238 panic("usb_create_event_thread task");
239 }
240 /*
241 * XXX we should make sure these threads are alive before
242 * end up using them in usb_doattach().
243 */
244 }
245 return 0;
246 }
247
248 static void
249 usb_doattach(device_t self)
250 {
251 struct usb_softc *sc = device_private(self);
252 usbd_device_handle dev;
253 usbd_status err;
254 int speed;
255 struct usb_event *ue;
256 const bool mpsafe = sc->sc_bus->methods->get_lock ? true : false;
257
258 DPRINTF(("usbd_doattach\n"));
259
260 sc->sc_bus->usbctl = self;
261 sc->sc_port.power = USB_MAX_POWER;
262
263 switch (sc->sc_bus->usbrev) {
264 case USBREV_1_0:
265 case USBREV_1_1:
266 speed = USB_SPEED_FULL;
267 break;
268 case USBREV_2_0:
269 speed = USB_SPEED_HIGH;
270 break;
271 default:
272 panic("usb_doattach");
273 }
274
275 cv_init(&sc->sc_bus->needs_explore_cv, "usbevt");
276
277 ue = usb_alloc_event();
278 ue->u.ue_ctrlr.ue_bus = device_unit(self);
279 usb_add_event(USB_EVENT_CTRLR_ATTACH, ue);
280
281 /* XXX we should have our own level */
282 sc->sc_bus->soft = softint_establish(
283 SOFTINT_NET | (mpsafe ? SOFTINT_MPSAFE : 0),
284 usb_soft_intr, sc->sc_bus);
285 if (sc->sc_bus->soft == NULL) {
286 aprint_error("%s: can't register softintr\n",
287 device_xname(self));
288 sc->sc_dying = 1;
289 return;
290 }
291
292 err = usbd_new_device(self, sc->sc_bus, 0, speed, 0,
293 &sc->sc_port);
294 if (!err) {
295 dev = sc->sc_port.device;
296 if (dev->hub == NULL) {
297 sc->sc_dying = 1;
298 aprint_error("%s: root device is not a hub\n",
299 device_xname(self));
300 return;
301 }
302 sc->sc_bus->root_hub = dev;
303 usb_create_event_thread(self);
304 #if 1
305 /*
306 * Turning this code off will delay attachment of USB devices
307 * until the USB event thread is running, which means that
308 * the keyboard will not work until after cold boot.
309 */
310 if (cold && (device_cfdata(self)->cf_flags & 1))
311 dev->hub->explore(sc->sc_bus->root_hub);
312 #endif
313 } else {
314 aprint_error("%s: root hub problem, error=%d\n",
315 device_xname(self), err);
316 sc->sc_dying = 1;
317 }
318
319 config_pending_incr();
320
321 if (!pmf_device_register(self, NULL, NULL))
322 aprint_error_dev(self, "couldn't establish power handler\n");
323
324 usb_async_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
325 usb_async_intr, NULL);
326
327 return;
328 }
329
330 void
331 usb_create_event_thread(device_t self)
332 {
333 struct usb_softc *sc = device_private(self);
334
335 if (kthread_create(PRI_NONE,
336 sc->sc_bus->lock ? KTHREAD_MPSAFE : 0, NULL,
337 usb_event_thread, sc, &sc->sc_event_thread,
338 "%s", device_xname(self))) {
339 printf("%s: unable to create event thread for\n",
340 device_xname(self));
341 panic("usb_create_event_thread");
342 }
343 }
344
345 /*
346 * Add a task to be performed by the task thread. This function can be
347 * called from any context and the task will be executed in a process
348 * context ASAP.
349 */
350 void
351 usb_add_task(usbd_device_handle dev, struct usb_task *task, int queue)
352 {
353 struct usb_taskq *taskq;
354
355 taskq = &usb_taskq[queue];
356 mutex_enter(&taskq->lock);
357 if (task->queue == -1) {
358 DPRINTFN(2,("usb_add_task: task=%p\n", task));
359 TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
360 task->queue = queue;
361 } else {
362 DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
363 }
364 cv_signal(&taskq->cv);
365 mutex_exit(&taskq->lock);
366 }
367
368 void
369 usb_rem_task(usbd_device_handle dev, struct usb_task *task)
370 {
371
372 if (task->queue != -1) {
373 struct usb_taskq *taskq = &usb_taskq[task->queue];
374 mutex_enter(&taskq->lock);
375 TAILQ_REMOVE(&taskq->tasks, task, next);
376 task->queue = -1;
377 mutex_exit(&taskq->lock);
378 }
379 }
380
381 void
382 usb_event_thread(void *arg)
383 {
384 struct usb_softc *sc = arg;
385
386 DPRINTF(("usb_event_thread: start\n"));
387
388 /*
389 * In case this controller is a companion controller to an
390 * EHCI controller we need to wait until the EHCI controller
391 * has grabbed the port.
392 * XXX It would be nicer to do this with a tsleep(), but I don't
393 * know how to synchronize the creation of the threads so it
394 * will work.
395 */
396 usb_delay_ms(sc->sc_bus, 500);
397
398 /* Make sure first discover does something. */
399 if (sc->sc_bus->lock)
400 mutex_enter(sc->sc_bus->lock);
401 sc->sc_bus->needs_explore = 1;
402 usb_discover(sc);
403 if (sc->sc_bus->lock)
404 mutex_exit(sc->sc_bus->lock);
405 config_pending_decr();
406
407 if (sc->sc_bus->lock)
408 mutex_enter(sc->sc_bus->lock);
409 while (!sc->sc_dying) {
410 if (usb_noexplore < 2)
411 usb_discover(sc);
412
413 if (sc->sc_bus->lock)
414 cv_timedwait(&sc->sc_bus->needs_explore_cv,
415 sc->sc_bus->lock, usb_noexplore ? 0 : hz * 60);
416 else
417 (void)tsleep(&sc->sc_bus->needs_explore, /* XXXSMP ok */
418 PWAIT, "usbevt", usb_noexplore ? 0 : hz * 60);
419 DPRINTFN(2,("usb_event_thread: woke up\n"));
420 }
421 sc->sc_event_thread = NULL;
422
423 /* In case parent is waiting for us to exit. */
424 if (sc->sc_bus->lock) {
425 cv_signal(&sc->sc_bus->needs_explore_cv);
426 mutex_exit(sc->sc_bus->lock);
427 } else
428 wakeup(sc); /* XXXSMP ok */
429
430 DPRINTF(("usb_event_thread: exit\n"));
431 kthread_exit(0);
432 }
433
434 void
435 usb_task_thread(void *arg)
436 {
437 struct usb_task *task;
438 struct usb_taskq *taskq;
439
440 taskq = arg;
441 DPRINTF(("usb_task_thread: start taskq %s\n", taskq->name));
442
443 mutex_enter(&taskq->lock);
444 for (;;) {
445 task = TAILQ_FIRST(&taskq->tasks);
446 if (task == NULL) {
447 cv_wait(&taskq->cv, &taskq->lock);
448 task = TAILQ_FIRST(&taskq->tasks);
449 }
450 DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
451 if (task != NULL) {
452 TAILQ_REMOVE(&taskq->tasks, task, next);
453 task->queue = -1;
454 mutex_exit(&taskq->lock);
455 task->fun(task->arg);
456 mutex_enter(&taskq->lock);
457 }
458 }
459 mutex_exit(&taskq->lock);
460 }
461
462 int
463 usbctlprint(void *aux, const char *pnp)
464 {
465 /* only "usb"es can attach to host controllers */
466 if (pnp)
467 aprint_normal("usb at %s", pnp);
468
469 return (UNCONF);
470 }
471
472 int
473 usbopen(dev_t dev, int flag, int mode, struct lwp *l)
474 {
475 int unit = minor(dev);
476 struct usb_softc *sc;
477
478 if (unit == USB_DEV_MINOR) {
479 if (usb_dev_open)
480 return (EBUSY);
481 usb_dev_open = 1;
482 mutex_enter(proc_lock);
483 usb_async_proc = 0;
484 mutex_exit(proc_lock);
485 return (0);
486 }
487
488 sc = device_lookup_private(&usb_cd, unit);
489 if (!sc)
490 return (ENXIO);
491
492 if (sc->sc_dying)
493 return (EIO);
494
495 return (0);
496 }
497
498 int
499 usbread(dev_t dev, struct uio *uio, int flag)
500 {
501 struct usb_event *ue;
502 #ifdef COMPAT_30
503 struct usb_event_old *ueo = NULL; /* XXXGCC */
504 #endif
505 int error, n, useold;
506
507 if (minor(dev) != USB_DEV_MINOR)
508 return (ENXIO);
509
510 useold = 0;
511 switch (uio->uio_resid) {
512 #ifdef COMPAT_30
513 case sizeof(struct usb_event_old):
514 ueo = malloc(sizeof(struct usb_event_old), M_USBDEV,
515 M_WAITOK|M_ZERO);
516 useold = 1;
517 /* FALLTHRU */
518 #endif
519 case sizeof(struct usb_event):
520 ue = usb_alloc_event();
521 break;
522 default:
523 return (EINVAL);
524 }
525
526 error = 0;
527 mutex_enter(&usb_event_lock);
528 for (;;) {
529 n = usb_get_next_event(ue);
530 if (n != 0)
531 break;
532 if (flag & IO_NDELAY) {
533 error = EWOULDBLOCK;
534 break;
535 }
536 error = cv_wait_sig(&usb_event_cv, &usb_event_lock);
537 if (error)
538 break;
539 }
540 mutex_exit(&usb_event_lock);
541 if (!error) {
542 #ifdef COMPAT_30
543 if (useold) { /* copy fields to old struct */
544 ueo->ue_type = ue->ue_type;
545 memcpy(&ueo->ue_time, &ue->ue_time,
546 sizeof(struct timespec));
547 switch (ue->ue_type) {
548 case USB_EVENT_DEVICE_ATTACH:
549 case USB_EVENT_DEVICE_DETACH:
550 usb_copy_old_devinfo(&ueo->u.ue_device, &ue->u.ue_device);
551 break;
552
553 case USB_EVENT_CTRLR_ATTACH:
554 case USB_EVENT_CTRLR_DETACH:
555 ueo->u.ue_ctrlr.ue_bus=ue->u.ue_ctrlr.ue_bus;
556 break;
557
558 case USB_EVENT_DRIVER_ATTACH:
559 case USB_EVENT_DRIVER_DETACH:
560 ueo->u.ue_driver.ue_cookie=ue->u.ue_driver.ue_cookie;
561 memcpy(ueo->u.ue_driver.ue_devname,
562 ue->u.ue_driver.ue_devname,
563 sizeof(ue->u.ue_driver.ue_devname));
564 break;
565 default:
566 ;
567 }
568
569 error = uiomove((void *)ueo, sizeof *ueo, uio);
570 } else
571 #endif
572 error = uiomove((void *)ue, sizeof *ue, uio);
573 }
574 usb_free_event(ue);
575 #ifdef COMPAT_30
576 if (useold)
577 free(ueo, M_USBDEV);
578 #endif
579
580 return (error);
581 }
582
583 int
584 usbclose(dev_t dev, int flag, int mode,
585 struct lwp *l)
586 {
587 int unit = minor(dev);
588
589 if (unit == USB_DEV_MINOR) {
590 mutex_enter(proc_lock);
591 usb_async_proc = 0;
592 mutex_exit(proc_lock);
593 usb_dev_open = 0;
594 }
595
596 return (0);
597 }
598
599 int
600 usbioctl(dev_t devt, u_long cmd, void *data, int flag, struct lwp *l)
601 {
602 struct usb_softc *sc;
603 int unit = minor(devt);
604
605 if (unit == USB_DEV_MINOR) {
606 switch (cmd) {
607 case FIONBIO:
608 /* All handled in the upper FS layer. */
609 return (0);
610
611 case FIOASYNC:
612 mutex_enter(proc_lock);
613 if (*(int *)data)
614 usb_async_proc = l->l_proc;
615 else
616 usb_async_proc = 0;
617 mutex_exit(proc_lock);
618 return (0);
619
620 default:
621 return (EINVAL);
622 }
623 }
624
625 sc = device_lookup_private(&usb_cd, unit);
626
627 if (sc->sc_dying)
628 return (EIO);
629
630 switch (cmd) {
631 #ifdef USB_DEBUG
632 case USB_SETDEBUG:
633 if (!(flag & FWRITE))
634 return (EBADF);
635 usbdebug = ((*(int *)data) & 0x000000ff);
636 break;
637 #endif /* USB_DEBUG */
638 case USB_REQUEST:
639 {
640 struct usb_ctl_request *ur = (void *)data;
641 int len = UGETW(ur->ucr_request.wLength);
642 struct iovec iov;
643 struct uio uio;
644 void *ptr = 0;
645 int addr = ur->ucr_addr;
646 usbd_status err;
647 int error = 0;
648
649 if (!(flag & FWRITE))
650 return (EBADF);
651
652 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
653 if (len < 0 || len > 32768)
654 return (EINVAL);
655 if (addr < 0 || addr >= USB_MAX_DEVICES ||
656 sc->sc_bus->devices[addr] == 0)
657 return (EINVAL);
658 if (len != 0) {
659 iov.iov_base = (void *)ur->ucr_data;
660 iov.iov_len = len;
661 uio.uio_iov = &iov;
662 uio.uio_iovcnt = 1;
663 uio.uio_resid = len;
664 uio.uio_offset = 0;
665 uio.uio_rw =
666 ur->ucr_request.bmRequestType & UT_READ ?
667 UIO_READ : UIO_WRITE;
668 uio.uio_vmspace = l->l_proc->p_vmspace;
669 ptr = malloc(len, M_TEMP, M_WAITOK);
670 if (uio.uio_rw == UIO_WRITE) {
671 error = uiomove(ptr, len, &uio);
672 if (error)
673 goto ret;
674 }
675 }
676 err = usbd_do_request_flags(sc->sc_bus->devices[addr],
677 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
678 USBD_DEFAULT_TIMEOUT);
679 if (err) {
680 error = EIO;
681 goto ret;
682 }
683 if (len > ur->ucr_actlen)
684 len = ur->ucr_actlen;
685 if (len != 0) {
686 if (uio.uio_rw == UIO_READ) {
687 error = uiomove(ptr, len, &uio);
688 if (error)
689 goto ret;
690 }
691 }
692 ret:
693 if (ptr)
694 free(ptr, M_TEMP);
695 return (error);
696 }
697
698 case USB_DEVICEINFO:
699 {
700 usbd_device_handle dev;
701 struct usb_device_info *di = (void *)data;
702 int addr = di->udi_addr;
703
704 if (addr < 1 || addr >= USB_MAX_DEVICES)
705 return EINVAL;
706 if ((dev = sc->sc_bus->devices[addr]) == NULL)
707 return ENXIO;
708 usbd_fill_deviceinfo(dev, di, 1);
709 break;
710 }
711
712 #ifdef COMPAT_30
713 case USB_DEVICEINFO_OLD:
714 {
715 usbd_device_handle dev;
716 struct usb_device_info_old *di = (void *)data;
717 int addr = di->udi_addr;
718
719 if (addr < 1 || addr >= USB_MAX_DEVICES)
720 return EINVAL;
721 if ((dev = sc->sc_bus->devices[addr]) == NULL)
722 return ENXIO;
723 usbd_fill_deviceinfo_old(dev, di, 1);
724 break;
725 }
726 #endif
727
728 case USB_DEVICESTATS:
729 *(struct usb_device_stats *)data = sc->sc_bus->stats;
730 break;
731
732 default:
733 return (EINVAL);
734 }
735 return (0);
736 }
737
738 int
739 usbpoll(dev_t dev, int events, struct lwp *l)
740 {
741 int revents, mask;
742
743 if (minor(dev) == USB_DEV_MINOR) {
744 revents = 0;
745 mask = POLLIN | POLLRDNORM;
746
747 mutex_enter(&usb_event_lock);
748 if (events & mask && usb_nevents > 0)
749 revents |= events & mask;
750 if (revents == 0 && events & mask)
751 selrecord(l, &usb_selevent);
752 mutex_exit(&usb_event_lock);
753
754 return (revents);
755 } else {
756 return (0);
757 }
758 }
759
760 static void
761 filt_usbrdetach(struct knote *kn)
762 {
763
764 mutex_enter(&usb_event_lock);
765 SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext);
766 mutex_exit(&usb_event_lock);
767 }
768
769 static int
770 filt_usbread(struct knote *kn, long hint)
771 {
772
773 if (usb_nevents == 0)
774 return (0);
775
776 kn->kn_data = sizeof(struct usb_event);
777 return (1);
778 }
779
780 static const struct filterops usbread_filtops =
781 { 1, NULL, filt_usbrdetach, filt_usbread };
782
783 int
784 usbkqfilter(dev_t dev, struct knote *kn)
785 {
786 struct klist *klist;
787
788 switch (kn->kn_filter) {
789 case EVFILT_READ:
790 if (minor(dev) != USB_DEV_MINOR)
791 return (1);
792 klist = &usb_selevent.sel_klist;
793 kn->kn_fop = &usbread_filtops;
794 break;
795
796 default:
797 return (EINVAL);
798 }
799
800 kn->kn_hook = NULL;
801
802 mutex_enter(&usb_event_lock);
803 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
804 mutex_exit(&usb_event_lock);
805
806 return (0);
807 }
808
809 /* Explore device tree from the root. */
810 Static void
811 usb_discover(struct usb_softc *sc)
812 {
813
814 KASSERT(sc->sc_bus->lock == NULL || mutex_owned(sc->sc_bus->lock));
815
816 DPRINTFN(2,("usb_discover\n"));
817 if (usb_noexplore > 1)
818 return;
819 /*
820 * We need mutual exclusion while traversing the device tree,
821 * but this is guaranteed since this function is only called
822 * from the event thread for the controller.
823 *
824 * Also, we now have sc_bus->lock held for MPSAFE controllers.
825 */
826 while (sc->sc_bus->needs_explore && !sc->sc_dying) {
827 sc->sc_bus->needs_explore = 0;
828 if (sc->sc_bus->lock)
829 mutex_exit(sc->sc_bus->lock);
830 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
831 if (sc->sc_bus->lock)
832 mutex_enter(sc->sc_bus->lock);
833 }
834 }
835
836 void
837 usb_needs_explore(usbd_device_handle dev)
838 {
839 DPRINTFN(2,("usb_needs_explore\n"));
840 if (dev->bus->lock)
841 mutex_enter(dev->bus->lock);
842 dev->bus->needs_explore = 1;
843 if (dev->bus->lock) {
844 cv_signal(&dev->bus->needs_explore_cv);
845 mutex_exit(dev->bus->lock);
846 } else
847 wakeup(&dev->bus->needs_explore); /* XXXSMP ok */
848 }
849
850 void
851 usb_needs_reattach(usbd_device_handle dev)
852 {
853 DPRINTFN(2,("usb_needs_reattach\n"));
854 if (dev->bus->lock)
855 mutex_enter(dev->bus->lock);
856 dev->powersrc->reattach = 1;
857 dev->bus->needs_explore = 1;
858 if (dev->bus->lock) {
859 cv_signal(&dev->bus->needs_explore_cv);
860 mutex_exit(dev->bus->lock);
861 } else
862 wakeup(&dev->bus->needs_explore); /* XXXSMP ok */
863 }
864
865 /* Called at with usb_event_lock held. */
866 int
867 usb_get_next_event(struct usb_event *ue)
868 {
869 struct usb_event_q *ueq;
870
871 KASSERT(mutex_owned(&usb_event_lock));
872
873 if (usb_nevents <= 0)
874 return (0);
875 ueq = SIMPLEQ_FIRST(&usb_events);
876 #ifdef DIAGNOSTIC
877 if (ueq == NULL) {
878 printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
879 usb_nevents = 0;
880 return (0);
881 }
882 #endif
883 if (ue)
884 *ue = ueq->ue;
885 SIMPLEQ_REMOVE_HEAD(&usb_events, next);
886 usb_free_event((struct usb_event *)(void *)ueq);
887 usb_nevents--;
888 return (1);
889 }
890
891 void
892 usbd_add_dev_event(int type, usbd_device_handle udev)
893 {
894 struct usb_event *ue = usb_alloc_event();
895
896 usbd_fill_deviceinfo(udev, &ue->u.ue_device, USB_EVENT_IS_ATTACH(type));
897 usb_add_event(type, ue);
898 }
899
900 void
901 usbd_add_drv_event(int type, usbd_device_handle udev, device_t dev)
902 {
903 struct usb_event *ue = usb_alloc_event();
904
905 ue->u.ue_driver.ue_cookie = udev->cookie;
906 strncpy(ue->u.ue_driver.ue_devname, device_xname(dev),
907 sizeof ue->u.ue_driver.ue_devname);
908 usb_add_event(type, ue);
909 }
910
911 Static struct usb_event *
912 usb_alloc_event(void)
913 {
914 /* Yes, this is right; we allocate enough so that we can use it later */
915 return malloc(sizeof(struct usb_event_q), M_USBDEV, M_WAITOK|M_ZERO);
916 }
917
918 Static void
919 usb_free_event(struct usb_event *uep)
920 {
921 free(uep, M_USBDEV);
922 }
923
924 Static void
925 usb_add_event(int type, struct usb_event *uep)
926 {
927 struct usb_event_q *ueq;
928 struct timeval thetime;
929
930 microtime(&thetime);
931 /* Don't want to wait here with usb_event_lock held */
932 ueq = (struct usb_event_q *)(void *)uep;
933 ueq->ue = *uep;
934 ueq->ue.ue_type = type;
935 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
936
937 mutex_enter(&usb_event_lock);
938 if (++usb_nevents >= USB_MAX_EVENTS) {
939 /* Too many queued events, drop an old one. */
940 DPRINTFN(-1,("usb: event dropped\n"));
941 (void)usb_get_next_event(0);
942 }
943 SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
944 cv_signal(&usb_event_cv);
945 selnotify(&usb_selevent, 0, 0);
946 if (usb_async_proc != NULL) {
947 kpreempt_disable();
948 softint_schedule(usb_async_sih);
949 kpreempt_enable();
950 }
951 mutex_exit(&usb_event_lock);
952 }
953
954 Static void
955 usb_async_intr(void *cookie)
956 {
957 proc_t *proc;
958
959 mutex_enter(proc_lock);
960 if ((proc = usb_async_proc) != NULL)
961 psignal(proc, SIGIO);
962 mutex_exit(proc_lock);
963 }
964
965 Static void
966 usb_soft_intr(void *arg)
967 {
968 usbd_bus_handle bus = arg;
969
970 if (bus->lock)
971 mutex_enter(bus->lock);
972 (*bus->methods->soft_intr)(bus);
973 if (bus->lock)
974 mutex_exit(bus->lock);
975 }
976
977 void
978 usb_schedsoftintr(usbd_bus_handle bus)
979 {
980
981 DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
982
983 if (bus->use_polling) {
984 bus->methods->soft_intr(bus);
985 } else {
986 kpreempt_disable();
987 softint_schedule(bus->soft);
988 kpreempt_enable();
989 }
990 }
991
992 int
993 usb_activate(device_t self, enum devact act)
994 {
995 struct usb_softc *sc = device_private(self);
996
997 switch (act) {
998 case DVACT_DEACTIVATE:
999 sc->sc_dying = 1;
1000 return 0;
1001 default:
1002 return EOPNOTSUPP;
1003 }
1004 }
1005
1006 void
1007 usb_childdet(device_t self, device_t child)
1008 {
1009 int i;
1010 struct usb_softc *sc = device_private(self);
1011 struct usbd_device *dev;
1012
1013 if ((dev = sc->sc_port.device) == NULL || dev->subdevlen == 0)
1014 return;
1015
1016 for (i = 0; i < dev->subdevlen; i++)
1017 if (dev->subdevs[i] == child)
1018 dev->subdevs[i] = NULL;
1019 }
1020
1021 int
1022 usb_detach(device_t self, int flags)
1023 {
1024 struct usb_softc *sc = device_private(self);
1025 struct usb_event *ue;
1026 int rc;
1027
1028 DPRINTF(("usb_detach: start\n"));
1029
1030 /* Make all devices disconnect. */
1031 if (sc->sc_port.device != NULL &&
1032 (rc = usb_disconnect_port(&sc->sc_port, self, flags)) != 0)
1033 return rc;
1034
1035 pmf_device_deregister(self);
1036 /* Kill off event thread. */
1037 sc->sc_dying = 1;
1038 while (sc->sc_event_thread != NULL) {
1039 if (sc->sc_bus->lock) {
1040 mutex_enter(sc->sc_bus->lock);
1041 cv_signal(&sc->sc_bus->needs_explore_cv);
1042 cv_timedwait(&sc->sc_bus->needs_explore_cv,
1043 sc->sc_bus->lock, hz * 60);
1044 mutex_exit(sc->sc_bus->lock);
1045 } else {
1046 wakeup(&sc->sc_bus->needs_explore); /* XXXSMP ok */
1047 tsleep(sc, PWAIT, "usbdet", hz * 60); /* XXXSMP ok */
1048 }
1049 }
1050 DPRINTF(("usb_detach: event thread dead\n"));
1051
1052 if (sc->sc_bus->soft != NULL) {
1053 softint_disestablish(sc->sc_bus->soft);
1054 sc->sc_bus->soft = NULL;
1055 }
1056
1057 ue = usb_alloc_event();
1058 ue->u.ue_ctrlr.ue_bus = device_unit(self);
1059 usb_add_event(USB_EVENT_CTRLR_DETACH, ue);
1060
1061 cv_destroy(&sc->sc_bus->needs_explore_cv);
1062
1063 return (0);
1064 }
1065
1066 #ifdef COMPAT_30
1067 Static void
1068 usb_copy_old_devinfo(struct usb_device_info_old *uo,
1069 const struct usb_device_info *ue)
1070 {
1071 const unsigned char *p;
1072 unsigned char *q;
1073 int i, n;
1074
1075 uo->udi_bus = ue->udi_bus;
1076 uo->udi_addr = ue->udi_addr;
1077 uo->udi_cookie = ue->udi_cookie;
1078 for (i = 0, p = (const unsigned char *)ue->udi_product,
1079 q = (unsigned char *)uo->udi_product;
1080 *p && i < USB_MAX_STRING_LEN - 1; p++) {
1081 if (*p < 0x80)
1082 q[i++] = *p;
1083 else {
1084 q[i++] = '?';
1085 if ((*p & 0xe0) == 0xe0)
1086 p++;
1087 p++;
1088 }
1089 }
1090 q[i] = 0;
1091
1092 for (i = 0, p = ue->udi_vendor, q = uo->udi_vendor;
1093 *p && i < USB_MAX_STRING_LEN - 1; p++) {
1094 if (* p < 0x80)
1095 q[i++] = *p;
1096 else {
1097 q[i++] = '?';
1098 p++;
1099 if ((*p & 0xe0) == 0xe0)
1100 p++;
1101 }
1102 }
1103 q[i] = 0;
1104
1105 memcpy(uo->udi_release, ue->udi_release, sizeof(uo->udi_release));
1106
1107 uo->udi_productNo = ue->udi_productNo;
1108 uo->udi_vendorNo = ue->udi_vendorNo;
1109 uo->udi_releaseNo = ue->udi_releaseNo;
1110 uo->udi_class = ue->udi_class;
1111 uo->udi_subclass = ue->udi_subclass;
1112 uo->udi_protocol = ue->udi_protocol;
1113 uo->udi_config = ue->udi_config;
1114 uo->udi_speed = ue->udi_speed;
1115 uo->udi_power = ue->udi_power;
1116 uo->udi_nports = ue->udi_nports;
1117
1118 for (n=0; n<USB_MAX_DEVNAMES; n++)
1119 memcpy(uo->udi_devnames[n],
1120 ue->udi_devnames[n], USB_MAX_DEVNAMELEN);
1121 memcpy(uo->udi_ports, ue->udi_ports, sizeof(uo->udi_ports));
1122 }
1123 #endif
1124