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