usb.c revision 1.133 1 /* $NetBSD: usb.c,v 1.133 2012/07/20 02:23:35 christos 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.133 2012/07/20 02:23:35 christos 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
366 if (task->queue != -1) {
367 struct usb_taskq *taskq = &usb_taskq[task->queue];
368 mutex_enter(&taskq->lock);
369 TAILQ_REMOVE(&taskq->tasks, task, next);
370 task->queue = -1;
371 mutex_exit(&taskq->lock);
372 }
373 }
374
375 void
376 usb_event_thread(void *arg)
377 {
378 struct usb_softc *sc = arg;
379
380 DPRINTF(("usb_event_thread: start\n"));
381
382 /*
383 * In case this controller is a companion controller to an
384 * EHCI controller we need to wait until the EHCI controller
385 * has grabbed the port.
386 * XXX It would be nicer to do this with a tsleep(), but I don't
387 * know how to synchronize the creation of the threads so it
388 * will work.
389 */
390 usb_delay_ms(sc->sc_bus, 500);
391
392 /* Make sure first discover does something. */
393 if (sc->sc_bus->lock)
394 mutex_enter(sc->sc_bus->lock);
395 sc->sc_bus->needs_explore = 1;
396 usb_discover(sc);
397 if (sc->sc_bus->lock)
398 mutex_exit(sc->sc_bus->lock);
399 config_pending_decr();
400
401 if (sc->sc_bus->lock)
402 mutex_enter(sc->sc_bus->lock);
403 while (!sc->sc_dying) {
404 if (usb_noexplore < 2)
405 usb_discover(sc);
406
407 if (sc->sc_bus->lock)
408 cv_timedwait(&sc->sc_bus->needs_explore_cv,
409 sc->sc_bus->lock, usb_noexplore ? 0 : hz * 60);
410 else
411 (void)tsleep(&sc->sc_bus->needs_explore, /* XXXSMP ok */
412 PWAIT, "usbevt", usb_noexplore ? 0 : hz * 60);
413 DPRINTFN(2,("usb_event_thread: woke up\n"));
414 }
415 sc->sc_event_thread = NULL;
416
417 /* In case parent is waiting for us to exit. */
418 if (sc->sc_bus->lock) {
419 cv_signal(&sc->sc_bus->needs_explore_cv);
420 mutex_exit(sc->sc_bus->lock);
421 } else
422 wakeup(sc); /* XXXSMP ok */
423
424 DPRINTF(("usb_event_thread: exit\n"));
425 kthread_exit(0);
426 }
427
428 void
429 usb_task_thread(void *arg)
430 {
431 struct usb_task *task;
432 struct usb_taskq *taskq;
433
434 taskq = arg;
435 DPRINTF(("usb_task_thread: start taskq %s\n", taskq->name));
436
437 mutex_enter(&taskq->lock);
438 for (;;) {
439 task = TAILQ_FIRST(&taskq->tasks);
440 if (task == NULL) {
441 cv_wait(&taskq->cv, &taskq->lock);
442 task = TAILQ_FIRST(&taskq->tasks);
443 }
444 DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
445 if (task != NULL) {
446 TAILQ_REMOVE(&taskq->tasks, task, next);
447 task->queue = -1;
448 mutex_exit(&taskq->lock);
449 task->fun(task->arg);
450 mutex_enter(&taskq->lock);
451 }
452 }
453 mutex_exit(&taskq->lock);
454 }
455
456 int
457 usbctlprint(void *aux, const char *pnp)
458 {
459 /* only "usb"es can attach to host controllers */
460 if (pnp)
461 aprint_normal("usb at %s", pnp);
462
463 return (UNCONF);
464 }
465
466 int
467 usbopen(dev_t dev, int flag, int mode, struct lwp *l)
468 {
469 int unit = minor(dev);
470 struct usb_softc *sc;
471
472 if (unit == USB_DEV_MINOR) {
473 if (usb_dev_open)
474 return (EBUSY);
475 usb_dev_open = 1;
476 mutex_enter(proc_lock);
477 usb_async_proc = 0;
478 mutex_exit(proc_lock);
479 return (0);
480 }
481
482 sc = device_lookup_private(&usb_cd, unit);
483 if (!sc)
484 return (ENXIO);
485
486 if (sc->sc_dying)
487 return (EIO);
488
489 return (0);
490 }
491
492 int
493 usbread(dev_t dev, struct uio *uio, int flag)
494 {
495 struct usb_event *ue;
496 #ifdef COMPAT_30
497 struct usb_event_old *ueo = NULL; /* XXXGCC */
498 #endif
499 int error, n, useold;
500
501 if (minor(dev) != USB_DEV_MINOR)
502 return (ENXIO);
503
504 useold = 0;
505 switch (uio->uio_resid) {
506 #ifdef COMPAT_30
507 case sizeof(struct usb_event_old):
508 ueo = malloc(sizeof(struct usb_event_old), M_USBDEV,
509 M_WAITOK|M_ZERO);
510 useold = 1;
511 /* FALLTHRU */
512 #endif
513 case sizeof(struct usb_event):
514 ue = usb_alloc_event();
515 break;
516 default:
517 return (EINVAL);
518 }
519
520 error = 0;
521 mutex_enter(&usb_event_lock);
522 for (;;) {
523 n = usb_get_next_event(ue);
524 if (n != 0)
525 break;
526 if (flag & IO_NDELAY) {
527 error = EWOULDBLOCK;
528 break;
529 }
530 error = cv_wait_sig(&usb_event_cv, &usb_event_lock);
531 if (error)
532 break;
533 }
534 mutex_exit(&usb_event_lock);
535 if (!error) {
536 #ifdef COMPAT_30
537 if (useold) { /* copy fields to old struct */
538 ueo->ue_type = ue->ue_type;
539 memcpy(&ueo->ue_time, &ue->ue_time,
540 sizeof(struct timespec));
541 switch (ue->ue_type) {
542 case USB_EVENT_DEVICE_ATTACH:
543 case USB_EVENT_DEVICE_DETACH:
544 usb_copy_old_devinfo(&ueo->u.ue_device, &ue->u.ue_device);
545 break;
546
547 case USB_EVENT_CTRLR_ATTACH:
548 case USB_EVENT_CTRLR_DETACH:
549 ueo->u.ue_ctrlr.ue_bus=ue->u.ue_ctrlr.ue_bus;
550 break;
551
552 case USB_EVENT_DRIVER_ATTACH:
553 case USB_EVENT_DRIVER_DETACH:
554 ueo->u.ue_driver.ue_cookie=ue->u.ue_driver.ue_cookie;
555 memcpy(ueo->u.ue_driver.ue_devname,
556 ue->u.ue_driver.ue_devname,
557 sizeof(ue->u.ue_driver.ue_devname));
558 break;
559 default:
560 ;
561 }
562
563 error = uiomove((void *)ueo, sizeof *ueo, uio);
564 } else
565 #endif
566 error = uiomove((void *)ue, sizeof *ue, uio);
567 }
568 usb_free_event(ue);
569 #ifdef COMPAT_30
570 if (useold)
571 free(ueo, M_USBDEV);
572 #endif
573
574 return (error);
575 }
576
577 int
578 usbclose(dev_t dev, int flag, int mode,
579 struct lwp *l)
580 {
581 int unit = minor(dev);
582
583 if (unit == USB_DEV_MINOR) {
584 mutex_enter(proc_lock);
585 usb_async_proc = 0;
586 mutex_exit(proc_lock);
587 usb_dev_open = 0;
588 }
589
590 return (0);
591 }
592
593 int
594 usbioctl(dev_t devt, u_long cmd, void *data, int flag, struct lwp *l)
595 {
596 struct usb_softc *sc;
597 int unit = minor(devt);
598
599 if (unit == USB_DEV_MINOR) {
600 switch (cmd) {
601 case FIONBIO:
602 /* All handled in the upper FS layer. */
603 return (0);
604
605 case FIOASYNC:
606 mutex_enter(proc_lock);
607 if (*(int *)data)
608 usb_async_proc = l->l_proc;
609 else
610 usb_async_proc = 0;
611 mutex_exit(proc_lock);
612 return (0);
613
614 default:
615 return (EINVAL);
616 }
617 }
618
619 sc = device_lookup_private(&usb_cd, unit);
620
621 if (sc->sc_dying)
622 return (EIO);
623
624 switch (cmd) {
625 #ifdef USB_DEBUG
626 case USB_SETDEBUG:
627 if (!(flag & FWRITE))
628 return (EBADF);
629 usbdebug = ((*(int *)data) & 0x000000ff);
630 break;
631 #endif /* USB_DEBUG */
632 case USB_REQUEST:
633 {
634 struct usb_ctl_request *ur = (void *)data;
635 int len = UGETW(ur->ucr_request.wLength);
636 struct iovec iov;
637 struct uio uio;
638 void *ptr = 0;
639 int addr = ur->ucr_addr;
640 usbd_status err;
641 int error = 0;
642
643 if (!(flag & FWRITE))
644 return (EBADF);
645
646 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
647 if (len < 0 || len > 32768)
648 return (EINVAL);
649 if (addr < 0 || addr >= USB_MAX_DEVICES ||
650 sc->sc_bus->devices[addr] == 0)
651 return (EINVAL);
652 if (len != 0) {
653 iov.iov_base = (void *)ur->ucr_data;
654 iov.iov_len = len;
655 uio.uio_iov = &iov;
656 uio.uio_iovcnt = 1;
657 uio.uio_resid = len;
658 uio.uio_offset = 0;
659 uio.uio_rw =
660 ur->ucr_request.bmRequestType & UT_READ ?
661 UIO_READ : UIO_WRITE;
662 uio.uio_vmspace = l->l_proc->p_vmspace;
663 ptr = malloc(len, M_TEMP, M_WAITOK);
664 if (uio.uio_rw == UIO_WRITE) {
665 error = uiomove(ptr, len, &uio);
666 if (error)
667 goto ret;
668 }
669 }
670 err = usbd_do_request_flags(sc->sc_bus->devices[addr],
671 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
672 USBD_DEFAULT_TIMEOUT);
673 if (err) {
674 error = EIO;
675 goto ret;
676 }
677 if (len > ur->ucr_actlen)
678 len = ur->ucr_actlen;
679 if (len != 0) {
680 if (uio.uio_rw == UIO_READ) {
681 error = uiomove(ptr, len, &uio);
682 if (error)
683 goto ret;
684 }
685 }
686 ret:
687 if (ptr)
688 free(ptr, M_TEMP);
689 return (error);
690 }
691
692 case USB_DEVICEINFO:
693 {
694 usbd_device_handle dev;
695 struct usb_device_info *di = (void *)data;
696 int addr = di->udi_addr;
697
698 if (addr < 1 || addr >= USB_MAX_DEVICES)
699 return EINVAL;
700 if ((dev = sc->sc_bus->devices[addr]) == NULL)
701 return ENXIO;
702 usbd_fill_deviceinfo(dev, di, 1);
703 break;
704 }
705
706 #ifdef COMPAT_30
707 case USB_DEVICEINFO_OLD:
708 {
709 usbd_device_handle dev;
710 struct usb_device_info_old *di = (void *)data;
711 int addr = di->udi_addr;
712
713 if (addr < 1 || addr >= USB_MAX_DEVICES)
714 return EINVAL;
715 if ((dev = sc->sc_bus->devices[addr]) == NULL)
716 return ENXIO;
717 usbd_fill_deviceinfo_old(dev, di, 1);
718 break;
719 }
720 #endif
721
722 case USB_DEVICESTATS:
723 *(struct usb_device_stats *)data = sc->sc_bus->stats;
724 break;
725
726 default:
727 return (EINVAL);
728 }
729 return (0);
730 }
731
732 int
733 usbpoll(dev_t dev, int events, struct lwp *l)
734 {
735 int revents, mask;
736
737 if (minor(dev) == USB_DEV_MINOR) {
738 revents = 0;
739 mask = POLLIN | POLLRDNORM;
740
741 mutex_enter(&usb_event_lock);
742 if (events & mask && usb_nevents > 0)
743 revents |= events & mask;
744 if (revents == 0 && events & mask)
745 selrecord(l, &usb_selevent);
746 mutex_exit(&usb_event_lock);
747
748 return (revents);
749 } else {
750 return (0);
751 }
752 }
753
754 static void
755 filt_usbrdetach(struct knote *kn)
756 {
757
758 mutex_enter(&usb_event_lock);
759 SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext);
760 mutex_exit(&usb_event_lock);
761 }
762
763 static int
764 filt_usbread(struct knote *kn, long hint)
765 {
766
767 if (usb_nevents == 0)
768 return (0);
769
770 kn->kn_data = sizeof(struct usb_event);
771 return (1);
772 }
773
774 static const struct filterops usbread_filtops =
775 { 1, NULL, filt_usbrdetach, filt_usbread };
776
777 int
778 usbkqfilter(dev_t dev, struct knote *kn)
779 {
780 struct klist *klist;
781
782 switch (kn->kn_filter) {
783 case EVFILT_READ:
784 if (minor(dev) != USB_DEV_MINOR)
785 return (1);
786 klist = &usb_selevent.sel_klist;
787 kn->kn_fop = &usbread_filtops;
788 break;
789
790 default:
791 return (EINVAL);
792 }
793
794 kn->kn_hook = NULL;
795
796 mutex_enter(&usb_event_lock);
797 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
798 mutex_exit(&usb_event_lock);
799
800 return (0);
801 }
802
803 /* Explore device tree from the root. */
804 Static void
805 usb_discover(struct usb_softc *sc)
806 {
807
808 KASSERT(sc->sc_bus->lock == NULL || mutex_owned(sc->sc_bus->lock));
809
810 DPRINTFN(2,("usb_discover\n"));
811 if (usb_noexplore > 1)
812 return;
813 /*
814 * We need mutual exclusion while traversing the device tree,
815 * but this is guaranteed since this function is only called
816 * from the event thread for the controller.
817 *
818 * Also, we now have sc_bus->lock held for MPSAFE controllers.
819 */
820 while (sc->sc_bus->needs_explore && !sc->sc_dying) {
821 sc->sc_bus->needs_explore = 0;
822 if (sc->sc_bus->lock)
823 mutex_exit(sc->sc_bus->lock);
824 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
825 if (sc->sc_bus->lock)
826 mutex_enter(sc->sc_bus->lock);
827 }
828 }
829
830 void
831 usb_needs_explore(usbd_device_handle dev)
832 {
833 DPRINTFN(2,("usb_needs_explore\n"));
834 if (dev->bus->lock)
835 mutex_enter(dev->bus->lock);
836 dev->bus->needs_explore = 1;
837 if (dev->bus->lock) {
838 cv_signal(&dev->bus->needs_explore_cv);
839 mutex_exit(dev->bus->lock);
840 } else
841 wakeup(&dev->bus->needs_explore); /* XXXSMP ok */
842 }
843
844 void
845 usb_needs_reattach(usbd_device_handle dev)
846 {
847 DPRINTFN(2,("usb_needs_reattach\n"));
848 if (dev->bus->lock)
849 mutex_enter(dev->bus->lock);
850 dev->powersrc->reattach = 1;
851 dev->bus->needs_explore = 1;
852 if (dev->bus->lock) {
853 cv_signal(&dev->bus->needs_explore_cv);
854 mutex_exit(dev->bus->lock);
855 } else
856 wakeup(&dev->bus->needs_explore); /* XXXSMP ok */
857 }
858
859 /* Called at with usb_event_lock held. */
860 int
861 usb_get_next_event(struct usb_event *ue)
862 {
863 struct usb_event_q *ueq;
864
865 KASSERT(mutex_owned(&usb_event_lock));
866
867 if (usb_nevents <= 0)
868 return (0);
869 ueq = SIMPLEQ_FIRST(&usb_events);
870 #ifdef DIAGNOSTIC
871 if (ueq == NULL) {
872 printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
873 usb_nevents = 0;
874 return (0);
875 }
876 #endif
877 if (ue)
878 *ue = ueq->ue;
879 SIMPLEQ_REMOVE_HEAD(&usb_events, next);
880 usb_free_event((struct usb_event *)(void *)ueq);
881 usb_nevents--;
882 return (1);
883 }
884
885 void
886 usbd_add_dev_event(int type, usbd_device_handle udev)
887 {
888 struct usb_event *ue = usb_alloc_event();
889
890 usbd_fill_deviceinfo(udev, &ue->u.ue_device, USB_EVENT_IS_ATTACH(type));
891 usb_add_event(type, ue);
892 }
893
894 void
895 usbd_add_drv_event(int type, usbd_device_handle udev, device_t dev)
896 {
897 struct usb_event *ue = usb_alloc_event();
898
899 ue->u.ue_driver.ue_cookie = udev->cookie;
900 strncpy(ue->u.ue_driver.ue_devname, device_xname(dev),
901 sizeof ue->u.ue_driver.ue_devname);
902 usb_add_event(type, ue);
903 }
904
905 Static struct usb_event *
906 usb_alloc_event(void)
907 {
908 /* Yes, this is right; we allocate enough so that we can use it later */
909 return malloc(sizeof(struct usb_event_q), M_USBDEV, M_WAITOK|M_ZERO);
910 }
911
912 Static void
913 usb_free_event(struct usb_event *uep)
914 {
915 free(uep, M_USBDEV);
916 }
917
918 Static void
919 usb_add_event(int type, struct usb_event *uep)
920 {
921 struct usb_event_q *ueq;
922 struct timeval thetime;
923
924 microtime(&thetime);
925 /* Don't want to wait here with usb_event_lock held */
926 ueq = (struct usb_event_q *)(void *)uep;
927 ueq->ue = *uep;
928 ueq->ue.ue_type = type;
929 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
930
931 mutex_enter(&usb_event_lock);
932 if (++usb_nevents >= USB_MAX_EVENTS) {
933 /* Too many queued events, drop an old one. */
934 DPRINTFN(-1,("usb: event dropped\n"));
935 (void)usb_get_next_event(0);
936 }
937 SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
938 cv_signal(&usb_event_cv);
939 selnotify(&usb_selevent, 0, 0);
940 if (usb_async_proc != NULL) {
941 kpreempt_disable();
942 softint_schedule(usb_async_sih);
943 kpreempt_enable();
944 }
945 mutex_exit(&usb_event_lock);
946 }
947
948 Static void
949 usb_async_intr(void *cookie)
950 {
951 proc_t *proc;
952
953 mutex_enter(proc_lock);
954 if ((proc = usb_async_proc) != NULL)
955 psignal(proc, SIGIO);
956 mutex_exit(proc_lock);
957 }
958
959 Static void
960 usb_soft_intr(void *arg)
961 {
962 usbd_bus_handle bus = arg;
963
964 if (bus->lock)
965 mutex_enter(bus->lock);
966 (*bus->methods->soft_intr)(bus);
967 if (bus->lock)
968 mutex_exit(bus->lock);
969 }
970
971 void
972 usb_schedsoftintr(usbd_bus_handle bus)
973 {
974
975 DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
976
977 if (bus->use_polling) {
978 bus->methods->soft_intr(bus);
979 } else {
980 kpreempt_disable();
981 softint_schedule(bus->soft);
982 kpreempt_enable();
983 }
984 }
985
986 int
987 usb_activate(device_t self, enum devact act)
988 {
989 struct usb_softc *sc = device_private(self);
990
991 switch (act) {
992 case DVACT_DEACTIVATE:
993 sc->sc_dying = 1;
994 return 0;
995 default:
996 return EOPNOTSUPP;
997 }
998 }
999
1000 void
1001 usb_childdet(device_t self, device_t child)
1002 {
1003 int i;
1004 struct usb_softc *sc = device_private(self);
1005 struct usbd_device *dev;
1006
1007 if ((dev = sc->sc_port.device) == NULL || dev->subdevlen == 0)
1008 return;
1009
1010 for (i = 0; i < dev->subdevlen; i++)
1011 if (dev->subdevs[i] == child)
1012 dev->subdevs[i] = NULL;
1013 }
1014
1015 int
1016 usb_detach(device_t self, int flags)
1017 {
1018 struct usb_softc *sc = device_private(self);
1019 struct usb_event *ue;
1020 int rc;
1021
1022 DPRINTF(("usb_detach: start\n"));
1023
1024 /* Make all devices disconnect. */
1025 if (sc->sc_port.device != NULL &&
1026 (rc = usb_disconnect_port(&sc->sc_port, self, flags)) != 0)
1027 return rc;
1028
1029 pmf_device_deregister(self);
1030 /* Kill off event thread. */
1031 sc->sc_dying = 1;
1032 while (sc->sc_event_thread != NULL) {
1033 if (sc->sc_bus->lock) {
1034 mutex_enter(sc->sc_bus->lock);
1035 cv_signal(&sc->sc_bus->needs_explore_cv);
1036 cv_timedwait(&sc->sc_bus->needs_explore_cv,
1037 sc->sc_bus->lock, hz * 60);
1038 mutex_exit(sc->sc_bus->lock);
1039 } else {
1040 wakeup(&sc->sc_bus->needs_explore); /* XXXSMP ok */
1041 tsleep(sc, PWAIT, "usbdet", hz * 60); /* XXXSMP ok */
1042 }
1043 }
1044 DPRINTF(("usb_detach: event thread dead\n"));
1045
1046 if (sc->sc_bus->soft != NULL) {
1047 softint_disestablish(sc->sc_bus->soft);
1048 sc->sc_bus->soft = NULL;
1049 }
1050
1051 ue = usb_alloc_event();
1052 ue->u.ue_ctrlr.ue_bus = device_unit(self);
1053 usb_add_event(USB_EVENT_CTRLR_DETACH, ue);
1054
1055 cv_destroy(&sc->sc_bus->needs_explore_cv);
1056
1057 return (0);
1058 }
1059
1060 #ifdef COMPAT_30
1061 Static void
1062 usb_copy_old_devinfo(struct usb_device_info_old *uo,
1063 const struct usb_device_info *ue)
1064 {
1065 const unsigned char *p;
1066 unsigned char *q;
1067 int i, n;
1068
1069 uo->udi_bus = ue->udi_bus;
1070 uo->udi_addr = ue->udi_addr;
1071 uo->udi_cookie = ue->udi_cookie;
1072 for (i = 0, p = (const unsigned char *)ue->udi_product,
1073 q = (unsigned char *)uo->udi_product;
1074 *p && i < USB_MAX_STRING_LEN - 1; p++) {
1075 if (*p < 0x80)
1076 q[i++] = *p;
1077 else {
1078 q[i++] = '?';
1079 if ((*p & 0xe0) == 0xe0)
1080 p++;
1081 p++;
1082 }
1083 }
1084 q[i] = 0;
1085
1086 for (i = 0, p = ue->udi_vendor, q = uo->udi_vendor;
1087 *p && i < USB_MAX_STRING_LEN - 1; p++) {
1088 if (* p < 0x80)
1089 q[i++] = *p;
1090 else {
1091 q[i++] = '?';
1092 p++;
1093 if ((*p & 0xe0) == 0xe0)
1094 p++;
1095 }
1096 }
1097 q[i] = 0;
1098
1099 memcpy(uo->udi_release, ue->udi_release, sizeof(uo->udi_release));
1100
1101 uo->udi_productNo = ue->udi_productNo;
1102 uo->udi_vendorNo = ue->udi_vendorNo;
1103 uo->udi_releaseNo = ue->udi_releaseNo;
1104 uo->udi_class = ue->udi_class;
1105 uo->udi_subclass = ue->udi_subclass;
1106 uo->udi_protocol = ue->udi_protocol;
1107 uo->udi_config = ue->udi_config;
1108 uo->udi_speed = ue->udi_speed;
1109 uo->udi_power = ue->udi_power;
1110 uo->udi_nports = ue->udi_nports;
1111
1112 for (n=0; n<USB_MAX_DEVNAMES; n++)
1113 memcpy(uo->udi_devnames[n],
1114 ue->udi_devnames[n], USB_MAX_DEVNAMELEN);
1115 memcpy(uo->udi_ports, ue->udi_ports, sizeof(uo->udi_ports));
1116 }
1117 #endif
1118