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