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