usb.c revision 1.51 1 /* $NetBSD: usb.c,v 1.51 2001/01/21 19:00:06 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 void
232 usb_add_task(usbd_device_handle dev, struct usb_task *task)
233 {
234 struct usb_softc *sc = dev->bus->usbctl;
235 int s;
236
237 s = splusb();
238 if (!task->onqueue) {
239 DPRINTFN(2,("usb_add_task: sc=%p task=%p\n", sc, task));
240 SIMPLEQ_INSERT_TAIL(&sc->sc_tasks, task, next);
241 task->onqueue = 1;
242 } else
243 DPRINTFN(3,("usb_add_task: sc=%p task=%p on q\n", sc, task));
244 wakeup(&sc->sc_tasks);
245 splx(s);
246 }
247
248 void
249 usb_event_thread(void *arg)
250 {
251 struct usb_softc *sc = arg;
252 struct usb_task *task;
253 int s;
254
255 DPRINTF(("usb_event_thread: start\n"));
256
257 /* Make sure first discover does something. */
258 sc->sc_bus->needs_explore = 1;
259 usb_discover(sc);
260 config_pending_decr();
261
262 while (!sc->sc_dying) {
263 s = splusb();
264 task = SIMPLEQ_FIRST(&sc->sc_tasks);
265 if (task == NULL) {
266 tsleep(&sc->sc_tasks, PWAIT, "usbevt", 0);
267 task = SIMPLEQ_FIRST(&sc->sc_tasks);
268 }
269 DPRINTFN(2,("usb_event_thread: woke up task=%p\n", task));
270 if (task != NULL && !sc->sc_dying) {
271 SIMPLEQ_REMOVE_HEAD(&sc->sc_tasks, task, next);
272 task->onqueue = 0;
273 splx(s);
274 task->fun(task->arg);
275 } else
276 splx(s);
277 }
278 sc->sc_event_thread = NULL;
279
280 /* In case parent is waiting for us to exit. */
281 wakeup(sc);
282
283 DPRINTF(("usb_event_thread: exit\n"));
284 kthread_exit(0);
285 }
286
287 int
288 usbctlprint(void *aux, const char *pnp)
289 {
290 /* only "usb"es can attach to host controllers */
291 if (pnp)
292 printf("usb at %s", pnp);
293
294 return (UNCONF);
295 }
296 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
297
298 int
299 usbopen(dev_t dev, int flag, int mode, struct proc *p)
300 {
301 int unit = minor(dev);
302 struct usb_softc *sc;
303
304 if (unit == USB_DEV_MINOR) {
305 if (usb_dev_open)
306 return (EBUSY);
307 usb_dev_open = 1;
308 usb_async_proc = 0;
309 return (0);
310 }
311
312 USB_GET_SC_OPEN(usb, unit, sc);
313
314 if (sc->sc_dying)
315 return (EIO);
316
317 return (0);
318 }
319
320 int
321 usbread(dev_t dev, struct uio *uio, int flag)
322 {
323 struct usb_event ue;
324 int s, error, n;
325
326 if (minor(dev) != USB_DEV_MINOR)
327 return (ENXIO);
328
329 if (uio->uio_resid != sizeof(struct usb_event))
330 return (EINVAL);
331
332 error = 0;
333 s = splusb();
334 for (;;) {
335 n = usb_get_next_event(&ue);
336 if (n != 0)
337 break;
338 if (flag & IO_NDELAY) {
339 error = EWOULDBLOCK;
340 break;
341 }
342 error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
343 if (error)
344 break;
345 }
346 splx(s);
347 if (!error)
348 error = uiomove((void *)&ue, uio->uio_resid, uio);
349
350 return (error);
351 }
352
353 int
354 usbclose(dev_t dev, int flag, int mode, struct proc *p)
355 {
356 int unit = minor(dev);
357
358 if (unit == USB_DEV_MINOR) {
359 usb_async_proc = 0;
360 usb_dev_open = 0;
361 }
362
363 return (0);
364 }
365
366 int
367 usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct proc *p)
368 {
369 struct usb_softc *sc;
370 int unit = minor(devt);
371
372 if (unit == USB_DEV_MINOR) {
373 switch (cmd) {
374 case FIONBIO:
375 /* All handled in the upper FS layer. */
376 return (0);
377
378 case FIOASYNC:
379 if (*(int *)data)
380 usb_async_proc = p;
381 else
382 usb_async_proc = 0;
383 return (0);
384
385 default:
386 return (EINVAL);
387 }
388 }
389
390 USB_GET_SC(usb, unit, sc);
391
392 if (sc->sc_dying)
393 return (EIO);
394
395 switch (cmd) {
396 #ifdef USB_DEBUG
397 case USB_SETDEBUG:
398 usbdebug = ((*(int *)data) & 0x000000ff);
399 #ifdef UHCI_DEBUG
400 uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
401 #endif
402 #ifdef OHCI_DEBUG
403 ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
404 #endif
405 break;
406 #endif
407 case USB_REQUEST:
408 {
409 struct usb_ctl_request *ur = (void *)data;
410 int len = UGETW(ur->request.wLength);
411 struct iovec iov;
412 struct uio uio;
413 void *ptr = 0;
414 int addr = ur->addr;
415 usbd_status err;
416 int error = 0;
417
418 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
419 if (len < 0 || len > 32768)
420 return (EINVAL);
421 if (addr < 0 || addr >= USB_MAX_DEVICES ||
422 sc->sc_bus->devices[addr] == 0)
423 return (EINVAL);
424 if (len != 0) {
425 iov.iov_base = (caddr_t)ur->data;
426 iov.iov_len = len;
427 uio.uio_iov = &iov;
428 uio.uio_iovcnt = 1;
429 uio.uio_resid = len;
430 uio.uio_offset = 0;
431 uio.uio_segflg = UIO_USERSPACE;
432 uio.uio_rw =
433 ur->request.bmRequestType & UT_READ ?
434 UIO_READ : UIO_WRITE;
435 uio.uio_procp = p;
436 ptr = malloc(len, M_TEMP, M_WAITOK);
437 if (uio.uio_rw == UIO_WRITE) {
438 error = uiomove(ptr, len, &uio);
439 if (error)
440 goto ret;
441 }
442 }
443 err = usbd_do_request_flags(sc->sc_bus->devices[addr],
444 &ur->request, ptr, ur->flags, &ur->actlen);
445 if (err) {
446 error = EIO;
447 goto ret;
448 }
449 if (len != 0) {
450 if (uio.uio_rw == UIO_READ) {
451 error = uiomove(ptr, len, &uio);
452 if (error)
453 goto ret;
454 }
455 }
456 ret:
457 if (ptr)
458 free(ptr, M_TEMP);
459 return (error);
460 }
461
462 case USB_DEVICEINFO:
463 {
464 struct usb_device_info *di = (void *)data;
465 int addr = di->addr;
466 usbd_device_handle dev;
467
468 if (addr < 1 || addr >= USB_MAX_DEVICES)
469 return (EINVAL);
470 dev = sc->sc_bus->devices[addr];
471 if (dev == NULL)
472 return (ENXIO);
473 usbd_fill_deviceinfo(dev, di, 1);
474 break;
475 }
476
477 case USB_DEVICESTATS:
478 *(struct usb_device_stats *)data = sc->sc_bus->stats;
479 break;
480
481 default:
482 return (EINVAL);
483 }
484 return (0);
485 }
486
487 int
488 usbpoll(dev_t dev, int events, struct proc *p)
489 {
490 int revents, mask, s;
491
492 if (minor(dev) == USB_DEV_MINOR) {
493 revents = 0;
494 mask = POLLIN | POLLRDNORM;
495
496 s = splusb();
497 if (events & mask && usb_nevents > 0)
498 revents |= events & mask;
499 if (revents == 0 && events & mask)
500 selrecord(p, &usb_selevent);
501 splx(s);
502
503 return (revents);
504 } else {
505 return (ENXIO);
506 }
507 }
508
509 /* Explore device tree from the root. */
510 Static void
511 usb_discover(void *v)
512 {
513 struct usb_softc *sc = v;
514
515 DPRINTFN(2,("usb_discover\n"));
516 #ifdef USB_DEBUG
517 if (usb_noexplore > 1)
518 return;
519 #endif
520 /*
521 * We need mutual exclusion while traversing the device tree,
522 * but this is guaranteed since this function is only called
523 * from the event thread for the controller.
524 */
525 while (sc->sc_bus->needs_explore && !sc->sc_dying) {
526 sc->sc_bus->needs_explore = 0;
527 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
528 }
529 }
530
531 void
532 usb_needs_explore(usbd_device_handle dev)
533 {
534 DPRINTFN(2,("usb_needs_explore\n"));
535 dev->bus->needs_explore = 1;
536 usb_add_task(dev, &dev->bus->usbctl->sc_exp_task);
537 }
538
539 /* Called at splusb() */
540 int
541 usb_get_next_event(struct usb_event *ue)
542 {
543 struct usb_event_q *ueq;
544
545 if (usb_nevents <= 0)
546 return (0);
547 ueq = SIMPLEQ_FIRST(&usb_events);
548 *ue = ueq->ue;
549 SIMPLEQ_REMOVE_HEAD(&usb_events, ueq, next);
550 free(ueq, M_USBDEV);
551 usb_nevents--;
552 return (1);
553 }
554
555 void
556 usbd_add_dev_event(int type, usbd_device_handle udev)
557 {
558 struct usb_event ue;
559
560 usbd_fill_deviceinfo(udev, &ue.u.ue_device, USB_EVENT_IS_ATTACH(type));
561 usb_add_event(type, &ue);
562 }
563
564 void
565 usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
566 {
567 struct usb_event ue;
568
569 ue.u.ue_driver.ue_cookie = udev->cookie;
570 strncpy(ue.u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
571 sizeof ue.u.ue_driver.ue_devname);
572 usb_add_event(type, &ue);
573 }
574
575 Static void
576 usb_add_event(int type, struct usb_event *uep)
577 {
578 struct usb_event_q *ueq;
579 struct usb_event ue;
580 struct timeval thetime;
581 int s;
582
583 microtime(&thetime);
584 /* Don't want to wait here inside splusb() */
585 ueq = malloc(sizeof *ueq, M_USBDEV, M_WAITOK);
586 ueq->ue = *uep;
587 ueq->ue.ue_type = type;
588 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
589
590 s = splusb();
591 if (++usb_nevents >= USB_MAX_EVENTS) {
592 /* Too many queued events, drop an old one. */
593 DPRINTFN(-1,("usb: event dropped\n"));
594 (void)usb_get_next_event(&ue);
595 }
596 SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
597 wakeup(&usb_events);
598 selwakeup(&usb_selevent);
599 if (usb_async_proc != NULL)
600 psignal(usb_async_proc, SIGIO);
601 splx(s);
602 }
603 void
604 usb_schedsoftintr(usbd_bus_handle bus)
605 {
606 #ifdef USB_USE_SOFTINTR
607 if (bus->use_polling) {
608 bus->methods->soft_intr(bus);
609 } else {
610 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
611 softintr_schedule(bus->soft);
612 #else
613 if (!callout_pending(&bus->softi))
614 callout_reset(&bus->softi, 0, bus->methods->soft_intr,
615 bus);
616 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
617 }
618 #else
619 bus->methods->soft_intr(bus);
620 #endif
621 }
622
623 int
624 usb_activate(device_ptr_t self, enum devact act)
625 {
626 struct usb_softc *sc = (struct usb_softc *)self;
627 usbd_device_handle dev = sc->sc_port.device;
628 int i, rv = 0;
629
630 switch (act) {
631 case DVACT_ACTIVATE:
632 return (EOPNOTSUPP);
633 break;
634
635 case DVACT_DEACTIVATE:
636 sc->sc_dying = 1;
637 if (dev && dev->cdesc && dev->subdevs) {
638 for (i = 0; dev->subdevs[i]; i++)
639 rv |= config_deactivate(dev->subdevs[i]);
640 }
641 break;
642 }
643 return (rv);
644 }
645
646 int
647 usb_detach(device_ptr_t self, int flags)
648 {
649 struct usb_softc *sc = (struct usb_softc *)self;
650 struct usb_event ue;
651
652 DPRINTF(("usb_detach: start\n"));
653
654 sc->sc_dying = 1;
655
656 /* Make all devices disconnect. */
657 if (sc->sc_port.device)
658 usb_disconnect_port(&sc->sc_port, self);
659
660 /* Kill off event thread. */
661 if (sc->sc_event_thread) {
662 wakeup(&sc->sc_tasks);
663 if (tsleep(sc, PWAIT, "usbdet", hz * 60))
664 printf("%s: event thread didn't die\n",
665 USBDEVNAME(sc->sc_dev));
666 DPRINTF(("usb_detach: event thread dead\n"));
667 }
668
669 usbd_finish();
670
671 #ifdef USB_USE_SOFTINTR
672 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
673 if (sc->sc_bus->soft != NULL) {
674 softintr_disestablish(sc->sc_bus->soft);
675 sc->sc_bus->soft = NULL;
676 }
677 #else
678 callout_stop(&sc->sc_bus->softi);
679 #endif
680 #endif
681
682 ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
683 usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
684
685 return (0);
686 }
687