Sync usage with man page.
[netbsd-mini2440.git] / sys / dev / usb / usb.c
blobe66239989608b65939db232e4eae27c9afa92494
1 /* $NetBSD: usb.c,v 1.119 2009/11/12 20:11:35 dyoung Exp $ */
3 /*
4 * Copyright (c) 1998, 2002, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net) at
9 * Carlstedt Research & Technology.
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.
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.
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/
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.119 2009/11/12 20:11:35 dyoung Exp $");
42 #include "opt_compat_netbsd.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/device.h>
49 #include <sys/kthread.h>
50 #include <sys/proc.h>
51 #include <sys/conf.h>
52 #include <sys/fcntl.h>
53 #include <sys/poll.h>
54 #include <sys/select.h>
55 #include <sys/vnode.h>
56 #include <sys/signalvar.h>
57 #include <sys/intr.h>
59 #include <dev/usb/usb.h>
60 #include <dev/usb/usbdi.h>
61 #include <dev/usb/usbdi_util.h>
63 #define USB_DEV_MINOR 255
65 #include <sys/bus.h>
67 #include <dev/usb/usbdivar.h>
68 #include <dev/usb/usb_quirks.h>
70 #ifdef USB_DEBUG
71 #define DPRINTF(x) if (usbdebug) logprintf x
72 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
73 int usbdebug = 0;
75 * 0 - do usual exploration
76 * 1 - do not use timeout exploration
77 * >1 - do no exploration
79 int usb_noexplore = 0;
80 #else
81 #define DPRINTF(x)
82 #define DPRINTFN(n,x)
83 #endif
85 struct usb_softc {
86 #if 0
87 USBBASEDEVICE sc_dev; /* base device */
88 #endif
89 usbd_bus_handle sc_bus; /* USB controller */
90 struct usbd_port sc_port; /* dummy port for root hub */
92 struct lwp *sc_event_thread;
94 char sc_dying;
97 struct usb_taskq {
98 TAILQ_HEAD(, usb_task) tasks;
99 struct lwp *task_thread_lwp;
100 const char *name;
101 int taskcreated; /* task thread exists. */
104 static struct usb_taskq usb_taskq[USB_NUM_TASKQS];
106 dev_type_open(usbopen);
107 dev_type_close(usbclose);
108 dev_type_read(usbread);
109 dev_type_ioctl(usbioctl);
110 dev_type_poll(usbpoll);
111 dev_type_kqfilter(usbkqfilter);
113 const struct cdevsw usb_cdevsw = {
114 usbopen, usbclose, usbread, nowrite, usbioctl,
115 nostop, notty, usbpoll, nommap, usbkqfilter, D_OTHER,
118 Static void usb_discover(struct usb_softc *);
119 Static void usb_create_event_thread(device_t);
120 Static void usb_event_thread(void *);
121 Static void usb_task_thread(void *);
123 #define USB_MAX_EVENTS 100
124 struct usb_event_q {
125 struct usb_event ue;
126 SIMPLEQ_ENTRY(usb_event_q) next;
128 Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
129 SIMPLEQ_HEAD_INITIALIZER(usb_events);
130 Static int usb_nevents = 0;
131 Static struct selinfo usb_selevent;
132 Static usb_proc_ptr usb_async_proc; /* process that wants USB SIGIO */
133 Static void *usb_async_sih;
134 Static int usb_dev_open = 0;
135 Static struct usb_event *usb_alloc_event(void);
136 Static void usb_free_event(struct usb_event *);
137 Static void usb_add_event(int, struct usb_event *);
138 Static int usb_get_next_event(struct usb_event *);
139 Static void usb_async_intr(void *);
141 #ifdef COMPAT_30
142 Static void usb_copy_old_devinfo(struct usb_device_info_old *, const struct usb_device_info *);
143 #endif
145 Static const char *usbrev_str[] = USBREV_STR;
147 static int usb_match(device_t, cfdata_t, void *);
148 static void usb_attach(device_t, device_t, void *);
149 static int usb_detach(device_t, int);
150 static int usb_activate(device_t, enum devact);
151 static void usb_childdet(device_t, device_t);
152 static void usb_doattach(device_t);
154 extern struct cfdriver usb_cd;
156 CFATTACH_DECL3_NEW(usb, sizeof(struct usb_softc),
157 usb_match, usb_attach, usb_detach, usb_activate, NULL, usb_childdet,
158 DVF_DETACH_SHUTDOWN);
161 usb_match(device_t parent, cfdata_t match, void *aux)
163 DPRINTF(("usbd_match\n"));
164 return (UMATCH_GENERIC);
167 void
168 usb_attach(device_t parent, device_t self, void *aux)
170 struct usb_softc *sc = device_private(self);
171 int usbrev;
173 sc->sc_bus = aux;
174 usbrev = sc->sc_bus->usbrev;
176 aprint_naive("\n");
177 aprint_normal(": USB revision %s", usbrev_str[usbrev]);
178 switch (usbrev) {
179 case USBREV_1_0:
180 case USBREV_1_1:
181 case USBREV_2_0:
182 break;
183 default:
184 aprint_error(", not supported\n");
185 sc->sc_dying = 1;
186 USB_ATTACH_ERROR_RETURN;
188 aprint_normal("\n");
190 config_interrupts(self, usb_doattach);
193 static void
194 usb_doattach(device_t self)
196 static bool usb_selevent_init; /* XXX */
197 struct usb_softc *sc = device_private(self);
198 usbd_device_handle dev;
199 usbd_status err;
200 int speed;
201 struct usb_event *ue;
203 if (!usb_selevent_init) {
204 selinit(&usb_selevent);
205 usb_selevent_init = true;
207 DPRINTF(("usbd_doattach\n"));
209 sc->sc_bus->usbctl = self;
210 sc->sc_port.power = USB_MAX_POWER;
212 switch (sc->sc_bus->usbrev) {
213 case USBREV_1_0:
214 case USBREV_1_1:
215 speed = USB_SPEED_FULL;
216 break;
217 case USBREV_2_0:
218 speed = USB_SPEED_HIGH;
219 break;
220 default:
221 panic("usb_doattach");
224 ue = usb_alloc_event();
225 ue->u.ue_ctrlr.ue_bus = device_unit(self);
226 usb_add_event(USB_EVENT_CTRLR_ATTACH, ue);
228 #ifdef USB_USE_SOFTINTR
229 /* XXX we should have our own level */
230 sc->sc_bus->soft = softint_establish(SOFTINT_NET,
231 sc->sc_bus->methods->soft_intr, sc->sc_bus);
232 if (sc->sc_bus->soft == NULL) {
233 aprint_error("%s: can't register softintr\n",
234 device_xname(self));
235 sc->sc_dying = 1;
236 USB_ATTACH_ERROR_RETURN;
238 #endif
240 err = usbd_new_device(self, sc->sc_bus, 0, speed, 0,
241 &sc->sc_port);
242 if (!err) {
243 dev = sc->sc_port.device;
244 if (dev->hub == NULL) {
245 sc->sc_dying = 1;
246 aprint_error("%s: root device is not a hub\n",
247 device_xname(self));
248 USB_ATTACH_ERROR_RETURN;
250 sc->sc_bus->root_hub = dev;
251 #if 1
253 * Turning this code off will delay attachment of USB devices
254 * until the USB event thread is running, which means that
255 * the keyboard will not work until after cold boot.
257 if (cold && (device_cfdata(self)->cf_flags & 1))
258 dev->hub->explore(sc->sc_bus->root_hub);
259 #endif
260 } else {
261 aprint_error("%s: root hub problem, error=%d\n",
262 device_xname(self), err);
263 sc->sc_dying = 1;
266 config_pending_incr();
267 usb_create_event_thread(self);
269 if (!pmf_device_register(self, NULL, NULL))
270 aprint_error_dev(self, "couldn't establish power handler\n");
272 usb_async_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
273 usb_async_intr, NULL);
275 USB_ATTACH_SUCCESS_RETURN;
278 static const char *taskq_names[] = USB_TASKQ_NAMES;
280 void
281 usb_create_event_thread(device_t self)
283 struct usb_softc *sc = device_private(self);
284 struct usb_taskq *taskq;
285 int i;
287 if (usb_kthread_create1(PRI_NONE, 0, NULL, usb_event_thread, sc,
288 &sc->sc_event_thread, "%s", device_xname(self))) {
289 printf("%s: unable to create event thread for\n",
290 device_xname(self));
291 panic("usb_create_event_thread");
293 for (i = 0; i < USB_NUM_TASKQS; i++) {
294 taskq = &usb_taskq[i];
296 if (taskq->taskcreated)
297 continue;
299 TAILQ_INIT(&taskq->tasks);
300 taskq->taskcreated = 1;
301 taskq->name = taskq_names[i];
302 if (usb_kthread_create1(PRI_NONE, 0, NULL, usb_task_thread,
303 taskq, &taskq->task_thread_lwp, taskq->name)) {
304 printf("unable to create task thread: %s\n", taskq->name);
305 panic("usb_create_event_thread task");
311 * Add a task to be performed by the task thread. This function can be
312 * called from any context and the task will be executed in a process
313 * context ASAP.
315 void
316 usb_add_task(usbd_device_handle dev, struct usb_task *task, int queue)
318 struct usb_taskq *taskq;
319 int s;
321 taskq = &usb_taskq[queue];
322 s = splusb();
323 if (task->queue == -1) {
324 DPRINTFN(2,("usb_add_task: task=%p\n", task));
325 TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
326 task->queue = queue;
327 } else {
328 DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
330 wakeup(&taskq->tasks);
331 splx(s);
334 void
335 usb_rem_task(usbd_device_handle dev, struct usb_task *task)
337 struct usb_taskq *taskq;
338 int s;
340 taskq = &usb_taskq[task->queue];
341 s = splusb();
342 if (task->queue != -1) {
343 TAILQ_REMOVE(&taskq->tasks, task, next);
344 task->queue = -1;
346 splx(s);
349 void
350 usb_event_thread(void *arg)
352 struct usb_softc *sc = arg;
354 DPRINTF(("usb_event_thread: start\n"));
357 * In case this controller is a companion controller to an
358 * EHCI controller we need to wait until the EHCI controller
359 * has grabbed the port.
360 * XXX It would be nicer to do this with a tsleep(), but I don't
361 * know how to synchronize the creation of the threads so it
362 * will work.
364 usb_delay_ms(sc->sc_bus, 500);
366 /* Make sure first discover does something. */
367 sc->sc_bus->needs_explore = 1;
368 usb_discover(sc);
369 config_pending_decr();
371 while (!sc->sc_dying) {
372 #ifdef USB_DEBUG
373 if (usb_noexplore < 2)
374 #endif
375 usb_discover(sc);
376 #ifdef USB_DEBUG
377 (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
378 usb_noexplore ? 0 : hz * 60);
379 #else
380 (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
381 hz * 60);
382 #endif
383 DPRINTFN(2,("usb_event_thread: woke up\n"));
385 sc->sc_event_thread = NULL;
387 /* In case parent is waiting for us to exit. */
388 wakeup(sc);
390 DPRINTF(("usb_event_thread: exit\n"));
391 kthread_exit(0);
394 void
395 usb_task_thread(void *arg)
397 struct usb_task *task;
398 struct usb_taskq *taskq;
399 int s;
401 taskq = arg;
402 DPRINTF(("usb_task_thread: start taskq %s\n", taskq->name));
404 s = splusb();
405 for (;;) {
406 task = TAILQ_FIRST(&taskq->tasks);
407 if (task == NULL) {
408 tsleep(&taskq->tasks, PWAIT, "usbtsk", 0);
409 task = TAILQ_FIRST(&taskq->tasks);
411 DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
412 if (task != NULL) {
413 TAILQ_REMOVE(&taskq->tasks, task, next);
414 task->queue = -1;
415 splx(s);
416 task->fun(task->arg);
417 s = splusb();
423 usbctlprint(void *aux, const char *pnp)
425 /* only "usb"es can attach to host controllers */
426 if (pnp)
427 aprint_normal("usb at %s", pnp);
429 return (UNCONF);
433 usbopen(dev_t dev, int flag, int mode, struct lwp *l)
435 int unit = minor(dev);
436 struct usb_softc *sc;
438 if (unit == USB_DEV_MINOR) {
439 if (usb_dev_open)
440 return (EBUSY);
441 usb_dev_open = 1;
442 mutex_enter(proc_lock);
443 usb_async_proc = 0;
444 mutex_exit(proc_lock);
445 return (0);
448 sc = device_lookup_private(&usb_cd, unit);
449 if (!sc)
450 return (ENXIO);
452 if (sc->sc_dying)
453 return (EIO);
455 return (0);
459 usbread(dev_t dev, struct uio *uio, int flag)
461 struct usb_event *ue;
462 #ifdef COMPAT_30
463 struct usb_event_old *ueo = NULL; /* XXXGCC */
464 #endif
465 int s, error, n, useold;
467 if (minor(dev) != USB_DEV_MINOR)
468 return (ENXIO);
470 useold = 0;
471 switch (uio->uio_resid) {
472 #ifdef COMPAT_30
473 case sizeof(struct usb_event_old):
474 ueo = malloc(sizeof(struct usb_event_old), M_USBDEV,
475 M_WAITOK|M_ZERO);
476 useold = 1;
477 /* FALLTHRU */
478 #endif
479 case sizeof(struct usb_event):
480 ue = usb_alloc_event();
481 break;
482 default:
483 return (EINVAL);
486 error = 0;
487 s = splusb();
488 for (;;) {
489 n = usb_get_next_event(ue);
490 if (n != 0)
491 break;
492 if (flag & IO_NDELAY) {
493 error = EWOULDBLOCK;
494 break;
496 error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
497 if (error)
498 break;
500 splx(s);
501 if (!error) {
502 #ifdef COMPAT_30
503 if (useold) { /* copy fields to old struct */
504 ueo->ue_type = ue->ue_type;
505 memcpy(&ueo->ue_time, &ue->ue_time,
506 sizeof(struct timespec));
507 switch (ue->ue_type) {
508 case USB_EVENT_DEVICE_ATTACH:
509 case USB_EVENT_DEVICE_DETACH:
510 usb_copy_old_devinfo(&ueo->u.ue_device, &ue->u.ue_device);
511 break;
513 case USB_EVENT_CTRLR_ATTACH:
514 case USB_EVENT_CTRLR_DETACH:
515 ueo->u.ue_ctrlr.ue_bus=ue->u.ue_ctrlr.ue_bus;
516 break;
518 case USB_EVENT_DRIVER_ATTACH:
519 case USB_EVENT_DRIVER_DETACH:
520 ueo->u.ue_driver.ue_cookie=ue->u.ue_driver.ue_cookie;
521 memcpy(ueo->u.ue_driver.ue_devname,
522 ue->u.ue_driver.ue_devname,
523 sizeof(ue->u.ue_driver.ue_devname));
524 break;
525 default:
529 error = uiomove((void *)ueo, sizeof *ueo, uio);
530 } else
531 #endif
532 error = uiomove((void *)ue, sizeof *ue, uio);
534 usb_free_event(ue);
535 #ifdef COMPAT_30
536 if (useold)
537 free(ueo, M_USBDEV);
538 #endif
540 return (error);
544 usbclose(dev_t dev, int flag, int mode,
545 struct lwp *l)
547 int unit = minor(dev);
549 if (unit == USB_DEV_MINOR) {
550 mutex_enter(proc_lock);
551 usb_async_proc = 0;
552 mutex_exit(proc_lock);
553 usb_dev_open = 0;
556 return (0);
560 usbioctl(dev_t devt, u_long cmd, void *data, int flag, struct lwp *l)
562 struct usb_softc *sc;
563 int unit = minor(devt);
565 if (unit == USB_DEV_MINOR) {
566 switch (cmd) {
567 case FIONBIO:
568 /* All handled in the upper FS layer. */
569 return (0);
571 case FIOASYNC:
572 mutex_enter(proc_lock);
573 if (*(int *)data)
574 usb_async_proc = l->l_proc;
575 else
576 usb_async_proc = 0;
577 mutex_exit(proc_lock);
578 return (0);
580 default:
581 return (EINVAL);
585 sc = device_lookup_private(&usb_cd, unit);
587 if (sc->sc_dying)
588 return (EIO);
590 switch (cmd) {
591 #ifdef USB_DEBUG
592 case USB_SETDEBUG:
593 if (!(flag & FWRITE))
594 return (EBADF);
595 usbdebug = ((*(int *)data) & 0x000000ff);
596 break;
597 #endif /* USB_DEBUG */
598 case USB_REQUEST:
600 struct usb_ctl_request *ur = (void *)data;
601 int len = UGETW(ur->ucr_request.wLength);
602 struct iovec iov;
603 struct uio uio;
604 void *ptr = 0;
605 int addr = ur->ucr_addr;
606 usbd_status err;
607 int error = 0;
609 if (!(flag & FWRITE))
610 return (EBADF);
612 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
613 if (len < 0 || len > 32768)
614 return (EINVAL);
615 if (addr < 0 || addr >= USB_MAX_DEVICES ||
616 sc->sc_bus->devices[addr] == 0)
617 return (EINVAL);
618 if (len != 0) {
619 iov.iov_base = (void *)ur->ucr_data;
620 iov.iov_len = len;
621 uio.uio_iov = &iov;
622 uio.uio_iovcnt = 1;
623 uio.uio_resid = len;
624 uio.uio_offset = 0;
625 uio.uio_rw =
626 ur->ucr_request.bmRequestType & UT_READ ?
627 UIO_READ : UIO_WRITE;
628 uio.uio_vmspace = l->l_proc->p_vmspace;
629 ptr = malloc(len, M_TEMP, M_WAITOK);
630 if (uio.uio_rw == UIO_WRITE) {
631 error = uiomove(ptr, len, &uio);
632 if (error)
633 goto ret;
636 err = usbd_do_request_flags(sc->sc_bus->devices[addr],
637 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
638 USBD_DEFAULT_TIMEOUT);
639 if (err) {
640 error = EIO;
641 goto ret;
643 if (len > ur->ucr_actlen)
644 len = ur->ucr_actlen;
645 if (len != 0) {
646 if (uio.uio_rw == UIO_READ) {
647 error = uiomove(ptr, len, &uio);
648 if (error)
649 goto ret;
652 ret:
653 if (ptr)
654 free(ptr, M_TEMP);
655 return (error);
658 case USB_DEVICEINFO:
660 usbd_device_handle dev;
661 struct usb_device_info *di = (void *)data;
662 int addr = di->udi_addr;
664 if (addr < 1 || addr >= USB_MAX_DEVICES)
665 return EINVAL;
666 if ((dev = sc->sc_bus->devices[addr]) == NULL)
667 return ENXIO;
668 usbd_fill_deviceinfo(dev, di, 1);
669 break;
672 #ifdef COMPAT_30
673 case USB_DEVICEINFO_OLD:
675 usbd_device_handle dev;
676 struct usb_device_info_old *di = (void *)data;
677 int addr = di->udi_addr;
679 if (addr < 1 || addr >= USB_MAX_DEVICES)
680 return EINVAL;
681 if ((dev = sc->sc_bus->devices[addr]) == NULL)
682 return ENXIO;
683 usbd_fill_deviceinfo_old(dev, di, 1);
684 break;
686 #endif
688 case USB_DEVICESTATS:
689 *(struct usb_device_stats *)data = sc->sc_bus->stats;
690 break;
692 default:
693 return (EINVAL);
695 return (0);
699 usbpoll(dev_t dev, int events, struct lwp *l)
701 int revents, mask, s;
703 if (minor(dev) == USB_DEV_MINOR) {
704 revents = 0;
705 mask = POLLIN | POLLRDNORM;
707 s = splusb();
708 if (events & mask && usb_nevents > 0)
709 revents |= events & mask;
710 if (revents == 0 && events & mask)
711 selrecord(l, &usb_selevent);
712 splx(s);
714 return (revents);
715 } else {
716 return (0);
720 static void
721 filt_usbrdetach(struct knote *kn)
723 int s;
725 s = splusb();
726 SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext);
727 splx(s);
730 static int
731 filt_usbread(struct knote *kn, long hint)
734 if (usb_nevents == 0)
735 return (0);
737 kn->kn_data = sizeof(struct usb_event);
738 return (1);
741 static const struct filterops usbread_filtops =
742 { 1, NULL, filt_usbrdetach, filt_usbread };
745 usbkqfilter(dev_t dev, struct knote *kn)
747 struct klist *klist;
748 int s;
750 switch (kn->kn_filter) {
751 case EVFILT_READ:
752 if (minor(dev) != USB_DEV_MINOR)
753 return (1);
754 klist = &usb_selevent.sel_klist;
755 kn->kn_fop = &usbread_filtops;
756 break;
758 default:
759 return (EINVAL);
762 kn->kn_hook = NULL;
764 s = splusb();
765 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
766 splx(s);
768 return (0);
771 /* Explore device tree from the root. */
772 Static void
773 usb_discover(struct usb_softc *sc)
776 DPRINTFN(2,("usb_discover\n"));
777 #ifdef USB_DEBUG
778 if (usb_noexplore > 1)
779 return;
780 #endif
782 * We need mutual exclusion while traversing the device tree,
783 * but this is guaranteed since this function is only called
784 * from the event thread for the controller.
786 while (sc->sc_bus->needs_explore && !sc->sc_dying) {
787 sc->sc_bus->needs_explore = 0;
788 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
792 void
793 usb_needs_explore(usbd_device_handle dev)
795 DPRINTFN(2,("usb_needs_explore\n"));
796 dev->bus->needs_explore = 1;
797 wakeup(&dev->bus->needs_explore);
800 void
801 usb_needs_reattach(usbd_device_handle dev)
803 DPRINTFN(2,("usb_needs_reattach\n"));
804 dev->powersrc->reattach = 1;
805 dev->bus->needs_explore = 1;
806 wakeup(&dev->bus->needs_explore);
809 /* Called at splusb() */
811 usb_get_next_event(struct usb_event *ue)
813 struct usb_event_q *ueq;
815 if (usb_nevents <= 0)
816 return (0);
817 ueq = SIMPLEQ_FIRST(&usb_events);
818 #ifdef DIAGNOSTIC
819 if (ueq == NULL) {
820 printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
821 usb_nevents = 0;
822 return (0);
824 #endif
825 if (ue)
826 *ue = ueq->ue;
827 SIMPLEQ_REMOVE_HEAD(&usb_events, next);
828 usb_free_event((struct usb_event *)(void *)ueq);
829 usb_nevents--;
830 return (1);
833 void
834 usbd_add_dev_event(int type, usbd_device_handle udev)
836 struct usb_event *ue = usb_alloc_event();
838 usbd_fill_deviceinfo(udev, &ue->u.ue_device, USB_EVENT_IS_ATTACH(type));
839 usb_add_event(type, ue);
842 void
843 usbd_add_drv_event(int type, usbd_device_handle udev, device_t dev)
845 struct usb_event *ue = usb_alloc_event();
847 ue->u.ue_driver.ue_cookie = udev->cookie;
848 strncpy(ue->u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
849 sizeof ue->u.ue_driver.ue_devname);
850 usb_add_event(type, ue);
853 Static struct usb_event *
854 usb_alloc_event(void)
856 /* Yes, this is right; we allocate enough so that we can use it later */
857 return malloc(sizeof(struct usb_event_q), M_USBDEV, M_WAITOK|M_ZERO);
860 Static void
861 usb_free_event(struct usb_event *uep)
863 free(uep, M_USBDEV);
866 Static void
867 usb_add_event(int type, struct usb_event *uep)
869 struct usb_event_q *ueq;
870 struct timeval thetime;
871 int s;
873 microtime(&thetime);
874 /* Don't want to wait here inside splusb() */
875 ueq = (struct usb_event_q *)(void *)uep;
876 ueq->ue = *uep;
877 ueq->ue.ue_type = type;
878 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
880 s = splusb();
881 if (++usb_nevents >= USB_MAX_EVENTS) {
882 /* Too many queued events, drop an old one. */
883 DPRINTFN(-1,("usb: event dropped\n"));
884 (void)usb_get_next_event(0);
886 SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
887 wakeup(&usb_events);
888 selnotify(&usb_selevent, 0, 0);
889 if (usb_async_proc != NULL) {
890 softint_schedule(usb_async_sih);
892 splx(s);
895 Static void
896 usb_async_intr(void *cookie)
898 proc_t *proc;
900 mutex_enter(proc_lock);
901 if ((proc = usb_async_proc) != NULL)
902 psignal(proc, SIGIO);
903 mutex_exit(proc_lock);
906 void
907 usb_schedsoftintr(usbd_bus_handle bus)
909 DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
910 #ifdef USB_USE_SOFTINTR
911 if (bus->use_polling) {
912 bus->methods->soft_intr(bus);
913 } else {
914 softint_schedule(bus->soft);
916 #else
917 bus->methods->soft_intr(bus);
918 #endif /* USB_USE_SOFTINTR */
922 usb_activate(device_t self, enum devact act)
924 struct usb_softc *sc = device_private(self);
926 switch (act) {
927 case DVACT_DEACTIVATE:
928 sc->sc_dying = 1;
929 return 0;
930 default:
931 return EOPNOTSUPP;
935 void
936 usb_childdet(device_t self, device_t child)
938 int i;
939 struct usb_softc *sc = device_private(self);
940 struct usbd_device *dev;
942 if ((dev = sc->sc_port.device) == NULL || dev->subdevlen == 0)
943 return;
945 for (i = 0; i < dev->subdevlen; i++)
946 if (dev->subdevs[i] == child)
947 dev->subdevs[i] = NULL;
951 usb_detach(device_t self, int flags)
953 struct usb_softc *sc = device_private(self);
954 struct usb_event *ue;
955 int rc;
957 DPRINTF(("usb_detach: start\n"));
959 /* Make all devices disconnect. */
960 if (sc->sc_port.device != NULL &&
961 (rc = usb_disconnect_port(&sc->sc_port, self, flags)) != 0)
962 return rc;
964 pmf_device_deregister(self);
965 /* Kill off event thread. */
966 sc->sc_dying = 1;
967 while (sc->sc_event_thread != NULL) {
968 wakeup(&sc->sc_bus->needs_explore);
969 tsleep(sc, PWAIT, "usbdet", hz * 60);
971 DPRINTF(("usb_detach: event thread dead\n"));
973 #ifdef USB_USE_SOFTINTR
974 if (sc->sc_bus->soft != NULL) {
975 softint_disestablish(sc->sc_bus->soft);
976 sc->sc_bus->soft = NULL;
978 #endif
980 ue = usb_alloc_event();
981 ue->u.ue_ctrlr.ue_bus = device_unit(self);
982 usb_add_event(USB_EVENT_CTRLR_DETACH, ue);
984 return (0);
987 #ifdef COMPAT_30
988 Static void
989 usb_copy_old_devinfo(struct usb_device_info_old *uo,
990 const struct usb_device_info *ue)
992 const unsigned char *p;
993 unsigned char *q;
994 int i, n;
996 uo->udi_bus = ue->udi_bus;
997 uo->udi_addr = ue->udi_addr;
998 uo->udi_cookie = ue->udi_cookie;
999 for (i = 0, p = (const unsigned char *)ue->udi_product,
1000 q = (unsigned char *)uo->udi_product;
1001 *p && i < USB_MAX_STRING_LEN - 1; p++) {
1002 if (*p < 0x80)
1003 q[i++] = *p;
1004 else {
1005 q[i++] = '?';
1006 if ((*p & 0xe0) == 0xe0)
1007 p++;
1008 p++;
1011 q[i] = 0;
1013 for (i = 0, p = ue->udi_vendor, q = uo->udi_vendor;
1014 *p && i < USB_MAX_STRING_LEN - 1; p++) {
1015 if (* p < 0x80)
1016 q[i++] = *p;
1017 else {
1018 q[i++] = '?';
1019 p++;
1020 if ((*p & 0xe0) == 0xe0)
1021 p++;
1024 q[i] = 0;
1026 memcpy(uo->udi_release, ue->udi_release, sizeof(uo->udi_release));
1028 uo->udi_productNo = ue->udi_productNo;
1029 uo->udi_vendorNo = ue->udi_vendorNo;
1030 uo->udi_releaseNo = ue->udi_releaseNo;
1031 uo->udi_class = ue->udi_class;
1032 uo->udi_subclass = ue->udi_subclass;
1033 uo->udi_protocol = ue->udi_protocol;
1034 uo->udi_config = ue->udi_config;
1035 uo->udi_speed = ue->udi_speed;
1036 uo->udi_power = ue->udi_power;
1037 uo->udi_nports = ue->udi_nports;
1039 for (n=0; n<USB_MAX_DEVNAMES; n++)
1040 memcpy(uo->udi_devnames[n],
1041 ue->udi_devnames[n], USB_MAX_DEVNAMELEN);
1042 memcpy(uo->udi_ports, ue->udi_ports, sizeof(uo->udi_ports));
1044 #endif