usb.c revision 1.52 1 /* $NetBSD: usb.c,v 1.52 2001/01/21 19:00:29 augustss Exp $ */
2
3 /*
4 * Copyright (c) 1998 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/data/ and
43 * http://www.usb.org/developers/index.html .
44 */
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/device.h>
51 #include <sys/kthread.h>
52 #include <sys/proc.h>
53 #include <sys/conf.h>
54 #include <sys/poll.h>
55 #include <sys/select.h>
56 #include <sys/vnode.h>
57 #include <sys/signalvar.h>
58
59 #include <dev/usb/usb.h>
60 #include <dev/usb/usbdi.h>
61 #include <dev/usb/usbdi_util.h>
62
63 #define USB_DEV_MINOR 255
64
65 #include <machine/bus.h>
66
67 #include <dev/usb/usbdivar.h>
68 #include <dev/usb/usb_quirks.h>
69
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;
74 #ifdef UHCI_DEBUG
75 int uhcidebug;
76 #endif
77 #ifdef OHCI_DEBUG
78 int ohcidebug;
79 #endif
80 /*
81 * 0 - do usual exploration
82 * 1 - do not use timeout exploration
83 * >1 - do no exploration
84 */
85 int usb_noexplore = 0;
86 #else
87 #define DPRINTF(x)
88 #define DPRINTFN(n,x)
89 #endif
90
91 struct usb_softc {
92 USBBASEDEVICE sc_dev; /* base device */
93 usbd_bus_handle sc_bus; /* USB controller */
94 struct usbd_port sc_port; /* dummy port for root hub */
95
96 SIMPLEQ_HEAD(, usb_task) sc_tasks;
97 struct proc *sc_event_thread;
98
99 struct usb_task sc_exp_task;
100
101 char sc_dying;
102 };
103
104 cdev_decl(usb);
105
106 Static void usb_discover(void *);
107 Static void usb_create_event_thread(void *);
108 Static void usb_event_thread(void *);
109
110 #define USB_MAX_EVENTS 100
111 struct usb_event_q {
112 struct usb_event ue;
113 SIMPLEQ_ENTRY(usb_event_q) next;
114 };
115 Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
116 SIMPLEQ_HEAD_INITIALIZER(usb_events);
117 Static int usb_nevents = 0;
118 Static struct selinfo usb_selevent;
119 Static struct proc *usb_async_proc; /* process that wants USB SIGIO */
120 Static int usb_dev_open = 0;
121 Static void usb_add_event(int, struct usb_event *);
122
123 Static int usb_get_next_event(struct usb_event *);
124
125 Static const char *usbrev_str[] = USBREV_STR;
126
127 USB_DECLARE_DRIVER(usb);
128
129 USB_MATCH(usb)
130 {
131 DPRINTF(("usbd_match\n"));
132 return (UMATCH_GENERIC);
133 }
134
135 USB_ATTACH(usb)
136 {
137 struct usb_softc *sc = (struct usb_softc *)self;
138 usbd_device_handle dev;
139 usbd_status err;
140 int usbrev;
141 struct usb_event ue;
142
143 DPRINTF(("usbd_attach\n"));
144
145 usbd_init();
146 sc->sc_bus = aux;
147 sc->sc_bus->usbctl = sc;
148 sc->sc_port.power = USB_MAX_POWER;
149 SIMPLEQ_INIT(&sc->sc_tasks);
150
151 sc->sc_exp_task.fun = usb_discover;
152 sc->sc_exp_task.arg = sc;
153
154 usbrev = sc->sc_bus->usbrev;
155 printf(": USB revision %s", usbrev_str[usbrev]);
156 if (usbrev != USBREV_1_0 && usbrev != USBREV_1_1) {
157 printf(", not supported\n");
158 USB_ATTACH_ERROR_RETURN;
159 }
160 printf("\n");
161
162 /* Make sure not to use tsleep() if we are cold booting. */
163 if (cold)
164 sc->sc_bus->use_polling++;
165
166 ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
167 usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue);
168
169 #ifdef USB_USE_SOFTINTR
170 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
171 /* XXX we should have our own level */
172 sc->sc_bus->soft = softintr_establish(IPL_SOFTNET,
173 sc->sc_bus->methods->soft_intr, sc->sc_bus);
174 if (sc->sc_bus->soft == NULL) {
175 printf("%s: can't register softintr\n", USBDEVNAME(sc->sc_dev));
176 sc->sc_dying = 1;
177 }
178 #else
179 callout_init(&sc->sc_bus->softi);
180 #endif
181 #endif
182
183 err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, 0, 0,
184 &sc->sc_port);
185 if (!err) {
186 dev = sc->sc_port.device;
187 if (dev->hub == NULL) {
188 sc->sc_dying = 1;
189 printf("%s: root device is not a hub\n",
190 USBDEVNAME(sc->sc_dev));
191 USB_ATTACH_ERROR_RETURN;
192 }
193 sc->sc_bus->root_hub = dev;
194 #if 1
195 /*
196 * Turning this code off will delay attachment of USB devices
197 * until the USB event thread is running, which means that
198 * the keyboard will not work until after cold boot.
199 */
200 if (cold && (sc->sc_dev.dv_cfdata->cf_flags & 1))
201 dev->hub->explore(sc->sc_bus->root_hub);
202 #endif
203 } else {
204 printf("%s: root hub problem, error=%d\n",
205 USBDEVNAME(sc->sc_dev), err);
206 sc->sc_dying = 1;
207 }
208 if (cold)
209 sc->sc_bus->use_polling--;
210
211 config_pending_incr();
212 usb_kthread_create(usb_create_event_thread, sc);
213
214 USB_ATTACH_SUCCESS_RETURN;
215 }
216
217 #if defined(__NetBSD__) || defined(__OpenBSD__)
218 void
219 usb_create_event_thread(void *arg)
220 {
221 struct usb_softc *sc = arg;
222
223 if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
224 "%s", sc->sc_dev.dv_xname)) {
225 printf("%s: unable to create event thread for\n",
226 sc->sc_dev.dv_xname);
227 panic("usb_create_event_thread");
228 }
229 }
230
231 /*
232 * Add a task to be performed by the event thread. This function can be
233 * called from any context and the task will be executed in a process
234 * context ASAP.
235 */
236 void
237 usb_add_task(usbd_device_handle dev, struct usb_task *task)
238 {
239 struct usb_softc *sc = dev->bus->usbctl;
240 int s;
241
242 s = splusb();
243 if (!task->onqueue) {
244 DPRINTFN(2,("usb_add_task: sc=%p task=%p\n", sc, task));
245 SIMPLEQ_INSERT_TAIL(&sc->sc_tasks, task, next);
246 task->onqueue = 1;
247 } else
248 DPRINTFN(3,("usb_add_task: sc=%p task=%p on q\n", sc, task));
249 wakeup(&sc->sc_tasks);
250 splx(s);
251 }
252
253 void
254 usb_event_thread(void *arg)
255 {
256 struct usb_softc *sc = arg;
257 struct usb_task *task;
258 int s;
259
260 DPRINTF(("usb_event_thread: start\n"));
261
262 /* Make sure first discover does something. */
263 sc->sc_bus->needs_explore = 1;
264 usb_discover(sc);
265 config_pending_decr();
266
267 while (!sc->sc_dying) {
268 s = splusb();
269 task = SIMPLEQ_FIRST(&sc->sc_tasks);
270 if (task == NULL) {
271 tsleep(&sc->sc_tasks, PWAIT, "usbevt", 0);
272 task = SIMPLEQ_FIRST(&sc->sc_tasks);
273 }
274 DPRINTFN(2,("usb_event_thread: woke up task=%p\n", task));
275 if (task != NULL && !sc->sc_dying) {
276 SIMPLEQ_REMOVE_HEAD(&sc->sc_tasks, task, next);
277 task->onqueue = 0;
278 splx(s);
279 task->fun(task->arg);
280 } else
281 splx(s);
282 }
283 sc->sc_event_thread = NULL;
284
285 /* In case parent is waiting for us to exit. */
286 wakeup(sc);
287
288 DPRINTF(("usb_event_thread: exit\n"));
289 kthread_exit(0);
290 }
291
292 int
293 usbctlprint(void *aux, const char *pnp)
294 {
295 /* only "usb"es can attach to host controllers */
296 if (pnp)
297 printf("usb at %s", pnp);
298
299 return (UNCONF);
300 }
301 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
302
303 int
304 usbopen(dev_t dev, int flag, int mode, struct proc *p)
305 {
306 int unit = minor(dev);
307 struct usb_softc *sc;
308
309 if (unit == USB_DEV_MINOR) {
310 if (usb_dev_open)
311 return (EBUSY);
312 usb_dev_open = 1;
313 usb_async_proc = 0;
314 return (0);
315 }
316
317 USB_GET_SC_OPEN(usb, unit, sc);
318
319 if (sc->sc_dying)
320 return (EIO);
321
322 return (0);
323 }
324
325 int
326 usbread(dev_t dev, struct uio *uio, int flag)
327 {
328 struct usb_event ue;
329 int s, error, n;
330
331 if (minor(dev) != USB_DEV_MINOR)
332 return (ENXIO);
333
334 if (uio->uio_resid != sizeof(struct usb_event))
335 return (EINVAL);
336
337 error = 0;
338 s = splusb();
339 for (;;) {
340 n = usb_get_next_event(&ue);
341 if (n != 0)
342 break;
343 if (flag & IO_NDELAY) {
344 error = EWOULDBLOCK;
345 break;
346 }
347 error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
348 if (error)
349 break;
350 }
351 splx(s);
352 if (!error)
353 error = uiomove((void *)&ue, uio->uio_resid, uio);
354
355 return (error);
356 }
357
358 int
359 usbclose(dev_t dev, int flag, int mode, struct proc *p)
360 {
361 int unit = minor(dev);
362
363 if (unit == USB_DEV_MINOR) {
364 usb_async_proc = 0;
365 usb_dev_open = 0;
366 }
367
368 return (0);
369 }
370
371 int
372 usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct proc *p)
373 {
374 struct usb_softc *sc;
375 int unit = minor(devt);
376
377 if (unit == USB_DEV_MINOR) {
378 switch (cmd) {
379 case FIONBIO:
380 /* All handled in the upper FS layer. */
381 return (0);
382
383 case FIOASYNC:
384 if (*(int *)data)
385 usb_async_proc = p;
386 else
387 usb_async_proc = 0;
388 return (0);
389
390 default:
391 return (EINVAL);
392 }
393 }
394
395 USB_GET_SC(usb, unit, sc);
396
397 if (sc->sc_dying)
398 return (EIO);
399
400 switch (cmd) {
401 #ifdef USB_DEBUG
402 case USB_SETDEBUG:
403 usbdebug = ((*(int *)data) & 0x000000ff);
404 #ifdef UHCI_DEBUG
405 uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
406 #endif
407 #ifdef OHCI_DEBUG
408 ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
409 #endif
410 break;
411 #endif
412 case USB_REQUEST:
413 {
414 struct usb_ctl_request *ur = (void *)data;
415 int len = UGETW(ur->request.wLength);
416 struct iovec iov;
417 struct uio uio;
418 void *ptr = 0;
419 int addr = ur->addr;
420 usbd_status err;
421 int error = 0;
422
423 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
424 if (len < 0 || len > 32768)
425 return (EINVAL);
426 if (addr < 0 || addr >= USB_MAX_DEVICES ||
427 sc->sc_bus->devices[addr] == 0)
428 return (EINVAL);
429 if (len != 0) {
430 iov.iov_base = (caddr_t)ur->data;
431 iov.iov_len = len;
432 uio.uio_iov = &iov;
433 uio.uio_iovcnt = 1;
434 uio.uio_resid = len;
435 uio.uio_offset = 0;
436 uio.uio_segflg = UIO_USERSPACE;
437 uio.uio_rw =
438 ur->request.bmRequestType & UT_READ ?
439 UIO_READ : UIO_WRITE;
440 uio.uio_procp = p;
441 ptr = malloc(len, M_TEMP, M_WAITOK);
442 if (uio.uio_rw == UIO_WRITE) {
443 error = uiomove(ptr, len, &uio);
444 if (error)
445 goto ret;
446 }
447 }
448 err = usbd_do_request_flags(sc->sc_bus->devices[addr],
449 &ur->request, ptr, ur->flags, &ur->actlen);
450 if (err) {
451 error = EIO;
452 goto ret;
453 }
454 if (len != 0) {
455 if (uio.uio_rw == UIO_READ) {
456 error = uiomove(ptr, len, &uio);
457 if (error)
458 goto ret;
459 }
460 }
461 ret:
462 if (ptr)
463 free(ptr, M_TEMP);
464 return (error);
465 }
466
467 case USB_DEVICEINFO:
468 {
469 struct usb_device_info *di = (void *)data;
470 int addr = di->addr;
471 usbd_device_handle dev;
472
473 if (addr < 1 || addr >= USB_MAX_DEVICES)
474 return (EINVAL);
475 dev = sc->sc_bus->devices[addr];
476 if (dev == NULL)
477 return (ENXIO);
478 usbd_fill_deviceinfo(dev, di, 1);
479 break;
480 }
481
482 case USB_DEVICESTATS:
483 *(struct usb_device_stats *)data = sc->sc_bus->stats;
484 break;
485
486 default:
487 return (EINVAL);
488 }
489 return (0);
490 }
491
492 int
493 usbpoll(dev_t dev, int events, struct proc *p)
494 {
495 int revents, mask, s;
496
497 if (minor(dev) == USB_DEV_MINOR) {
498 revents = 0;
499 mask = POLLIN | POLLRDNORM;
500
501 s = splusb();
502 if (events & mask && usb_nevents > 0)
503 revents |= events & mask;
504 if (revents == 0 && events & mask)
505 selrecord(p, &usb_selevent);
506 splx(s);
507
508 return (revents);
509 } else {
510 return (ENXIO);
511 }
512 }
513
514 /* Explore device tree from the root. */
515 Static void
516 usb_discover(void *v)
517 {
518 struct usb_softc *sc = v;
519
520 DPRINTFN(2,("usb_discover\n"));
521 #ifdef USB_DEBUG
522 if (usb_noexplore > 1)
523 return;
524 #endif
525 /*
526 * We need mutual exclusion while traversing the device tree,
527 * but this is guaranteed since this function is only called
528 * from the event thread for the controller.
529 */
530 while (sc->sc_bus->needs_explore && !sc->sc_dying) {
531 sc->sc_bus->needs_explore = 0;
532 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
533 }
534 }
535
536 void
537 usb_needs_explore(usbd_device_handle dev)
538 {
539 DPRINTFN(2,("usb_needs_explore\n"));
540 dev->bus->needs_explore = 1;
541 usb_add_task(dev, &dev->bus->usbctl->sc_exp_task);
542 }
543
544 /* Called at splusb() */
545 int
546 usb_get_next_event(struct usb_event *ue)
547 {
548 struct usb_event_q *ueq;
549
550 if (usb_nevents <= 0)
551 return (0);
552 ueq = SIMPLEQ_FIRST(&usb_events);
553 *ue = ueq->ue;
554 SIMPLEQ_REMOVE_HEAD(&usb_events, ueq, next);
555 free(ueq, M_USBDEV);
556 usb_nevents--;
557 return (1);
558 }
559
560 void
561 usbd_add_dev_event(int type, usbd_device_handle udev)
562 {
563 struct usb_event ue;
564
565 usbd_fill_deviceinfo(udev, &ue.u.ue_device, USB_EVENT_IS_ATTACH(type));
566 usb_add_event(type, &ue);
567 }
568
569 void
570 usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
571 {
572 struct usb_event ue;
573
574 ue.u.ue_driver.ue_cookie = udev->cookie;
575 strncpy(ue.u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
576 sizeof ue.u.ue_driver.ue_devname);
577 usb_add_event(type, &ue);
578 }
579
580 Static void
581 usb_add_event(int type, struct usb_event *uep)
582 {
583 struct usb_event_q *ueq;
584 struct usb_event ue;
585 struct timeval thetime;
586 int s;
587
588 microtime(&thetime);
589 /* Don't want to wait here inside splusb() */
590 ueq = malloc(sizeof *ueq, M_USBDEV, M_WAITOK);
591 ueq->ue = *uep;
592 ueq->ue.ue_type = type;
593 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
594
595 s = splusb();
596 if (++usb_nevents >= USB_MAX_EVENTS) {
597 /* Too many queued events, drop an old one. */
598 DPRINTFN(-1,("usb: event dropped\n"));
599 (void)usb_get_next_event(&ue);
600 }
601 SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
602 wakeup(&usb_events);
603 selwakeup(&usb_selevent);
604 if (usb_async_proc != NULL)
605 psignal(usb_async_proc, SIGIO);
606 splx(s);
607 }
608 void
609 usb_schedsoftintr(usbd_bus_handle bus)
610 {
611 #ifdef USB_USE_SOFTINTR
612 if (bus->use_polling) {
613 bus->methods->soft_intr(bus);
614 } else {
615 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
616 softintr_schedule(bus->soft);
617 #else
618 if (!callout_pending(&bus->softi))
619 callout_reset(&bus->softi, 0, bus->methods->soft_intr,
620 bus);
621 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
622 }
623 #else
624 bus->methods->soft_intr(bus);
625 #endif
626 }
627
628 int
629 usb_activate(device_ptr_t self, enum devact act)
630 {
631 struct usb_softc *sc = (struct usb_softc *)self;
632 usbd_device_handle dev = sc->sc_port.device;
633 int i, rv = 0;
634
635 switch (act) {
636 case DVACT_ACTIVATE:
637 return (EOPNOTSUPP);
638 break;
639
640 case DVACT_DEACTIVATE:
641 sc->sc_dying = 1;
642 if (dev && dev->cdesc && dev->subdevs) {
643 for (i = 0; dev->subdevs[i]; i++)
644 rv |= config_deactivate(dev->subdevs[i]);
645 }
646 break;
647 }
648 return (rv);
649 }
650
651 int
652 usb_detach(device_ptr_t self, int flags)
653 {
654 struct usb_softc *sc = (struct usb_softc *)self;
655 struct usb_event ue;
656
657 DPRINTF(("usb_detach: start\n"));
658
659 sc->sc_dying = 1;
660
661 /* Make all devices disconnect. */
662 if (sc->sc_port.device)
663 usb_disconnect_port(&sc->sc_port, self);
664
665 /* Kill off event thread. */
666 if (sc->sc_event_thread) {
667 wakeup(&sc->sc_tasks);
668 if (tsleep(sc, PWAIT, "usbdet", hz * 60))
669 printf("%s: event thread didn't die\n",
670 USBDEVNAME(sc->sc_dev));
671 DPRINTF(("usb_detach: event thread dead\n"));
672 }
673
674 usbd_finish();
675
676 #ifdef USB_USE_SOFTINTR
677 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
678 if (sc->sc_bus->soft != NULL) {
679 softintr_disestablish(sc->sc_bus->soft);
680 sc->sc_bus->soft = NULL;
681 }
682 #else
683 callout_stop(&sc->sc_bus->softi);
684 #endif
685 #endif
686
687 ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
688 usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
689
690 return (0);
691 }
692