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