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