Home | History | Annotate | Line # | Download | only in usb
usb.c revision 1.15
      1 /*	$NetBSD: usb.c,v 1.15 1999/08/02 19:36:48 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__)
     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 #include <sys/conf.h>
     59 #endif
     60 #include <sys/poll.h>
     61 #include <sys/proc.h>
     62 #include <sys/select.h>
     63 
     64 #include <dev/usb/usb.h>
     65 #include <dev/usb/usbdi.h>
     66 #include <dev/usb/usbdi_util.h>
     67 
     68 #if defined(__FreeBSD__)
     69 MALLOC_DEFINE(M_USB, "USB", "USB");
     70 MALLOC_DEFINE(M_USBDEV, "USBdev", "USB device");
     71 MALLOC_DEFINE(M_USBHC, "USBHC", "USB host controller");
     72 
     73 #include "usb_if.h"
     74 #endif /* defined(__FreeBSD__) */
     75 
     76 #include <dev/usb/usbdivar.h>
     77 #include <dev/usb/usb_quirks.h>
     78 
     79 #ifdef USB_DEBUG
     80 #define DPRINTF(x)	if (usbdebug) printf x
     81 #define DPRINTFN(n,x)	if (usbdebug>(n)) printf x
     82 int	usbdebug = 0;
     83 int	uhcidebug;
     84 int	ohcidebug;
     85 #else
     86 #define DPRINTF(x)
     87 #define DPRINTFN(n,x)
     88 #endif
     89 
     90 #define USBUNIT(dev) (minor(dev))
     91 
     92 struct usb_softc {
     93 	bdevice sc_dev;			/* base device */
     94 	usbd_bus_handle sc_bus;		/* USB controller */
     95 	struct usbd_port sc_port;	/* dummy port for root hub */
     96 	char sc_running;
     97 	char sc_exploring;
     98 	struct selinfo sc_consel;	/* waiting for connect change */
     99 	int shutdown;
    100 	struct proc *event_thread;
    101 };
    102 
    103 #if defined(__NetBSD__)
    104 int usbopen __P((dev_t, int, int, struct proc *));
    105 int usbclose __P((dev_t, int, int, struct proc *));
    106 int usbioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
    107 int usbpoll __P((dev_t, int, struct proc *));
    108 
    109 #elif defined(__FreeBSD__)
    110 d_open_t  usbopen;
    111 d_close_t usbclose;
    112 d_ioctl_t usbioctl;
    113 int usbpoll __P((dev_t, int, struct proc *));
    114 
    115 struct cdevsw usb_cdevsw = {
    116 	usbopen,     usbclose,    noread,         nowrite,
    117 	usbioctl,    nullstop,    nullreset,      nodevtotty,
    118 	usbpoll,     nommap,      nostrat,
    119 	"usb",        NULL,   -1
    120 };
    121 #endif
    122 
    123 usbd_status usb_discover __P((struct usb_softc *));
    124 void	usb_create_event_thread __P((void *));
    125 void	usb_event_thread __P((void *));
    126 
    127 USB_DECLARE_DRIVER_INIT(usb, DEVMETHOD(bus_print_child, usbd_print_child));
    128 
    129 USB_MATCH(usb)
    130 {
    131 	DPRINTF(("usbd_match\n"));
    132 	return (UMATCH_GENERIC);
    133 }
    134 
    135 USB_ATTACH(usb)
    136 {
    137 #if defined(__NetBSD__)
    138 	struct usb_softc *sc = (struct usb_softc *)self;
    139 #elif defined(__FreeBSD__)
    140 	struct usb_softc *sc = device_get_softc(self);
    141 	void *aux = device_get_ivars(self);
    142 #endif
    143 	usbd_device_handle dev;
    144 	usbd_status r;
    145 
    146 #if defined(__NetBSD__)
    147 	printf("\n");
    148 #elif defined(__FreeBSD__)
    149 	sc->sc_dev = self;
    150 #endif
    151 
    152 	DPRINTF(("usbd_attach\n"));
    153 	usbd_init();
    154 	sc->sc_bus = aux;
    155 	sc->sc_bus->usbctl = sc;
    156 	sc->sc_running = 1;
    157 	sc->sc_bus->use_polling = 1;
    158 	sc->sc_port.power = USB_MAX_POWER;
    159 	r = usbd_new_device(&sc->sc_dev, sc->sc_bus, 0,0,0, &sc->sc_port);
    160 
    161 	if (r == USBD_NORMAL_COMPLETION) {
    162 		dev = sc->sc_port.device;
    163 		if (!dev->hub) {
    164 			sc->sc_running = 0;
    165 			printf("%s: root device is not a hub\n",
    166 			       USBDEVNAME(sc->sc_dev));
    167 			USB_ATTACH_ERROR_RETURN;
    168 		}
    169 		sc->sc_bus->root_hub = dev;
    170 		dev->hub->explore(sc->sc_bus->root_hub);
    171 	} else {
    172 		printf("%s: root hub problem, error=%d\n",
    173 		       USBDEVNAME(sc->sc_dev), r);
    174 		sc->sc_running = 0;
    175 	}
    176 	sc->sc_bus->use_polling = 0;
    177 
    178 	kthread_create(usb_create_event_thread, sc);
    179 
    180 	USB_ATTACH_SUCCESS_RETURN;
    181 }
    182 
    183 void
    184 usb_create_event_thread(arg)
    185 	void *arg;
    186 {
    187 	struct usb_softc *sc = arg;
    188 
    189 	if (kthread_create1(usb_event_thread, sc, &sc->event_thread,
    190 			   "%s", sc->sc_dev.dv_xname)) {
    191 		printf("%s: unable to create event thread for\n",
    192 		       sc->sc_dev.dv_xname);
    193 		panic("usb_create_event_thread");
    194 	}
    195 }
    196 
    197 void
    198 usb_event_thread(arg)
    199 	void *arg;
    200 {
    201 	struct usb_softc *sc = arg;
    202 
    203 	while (!sc->shutdown) {
    204 		(void)tsleep(&sc->sc_bus->needs_explore,
    205 			     PWAIT, "usbevt", hz*30);
    206 		DPRINTFN(2,("usb_event_thread: woke up\n"));
    207 		usb_discover(sc);
    208 	}
    209 	sc->event_thread = 0;
    210 
    211 	/* In case parent is waiting for us to exit. */
    212 	wakeup(sc);
    213 
    214 	kthread_exit(0);
    215 }
    216 
    217 #if defined(__NetBSD__)
    218 int
    219 usbctlprint(aux, pnp)
    220 	void *aux;
    221 	const char *pnp;
    222 {
    223 	/* only "usb"es can attach to host controllers */
    224 	if (pnp)
    225 		printf("usb at %s", pnp);
    226 
    227 	return (UNCONF);
    228 }
    229 #endif
    230 
    231 int
    232 usbopen(dev, flag, mode, p)
    233 	dev_t dev;
    234 	int flag, mode;
    235 	struct proc *p;
    236 {
    237 	USB_GET_SC_OPEN(usb, USBUNIT(dev), sc);
    238 
    239 	if (sc == 0 || !sc->sc_running)
    240 		return (ENXIO);
    241 
    242 	return (0);
    243 }
    244 
    245 int
    246 usbclose(dev, flag, mode, p)
    247 	dev_t dev;
    248 	int flag, mode;
    249 	struct proc *p;
    250 {
    251 	return (0);
    252 }
    253 
    254 int
    255 usbioctl(dev, cmd, data, flag, p)
    256 	dev_t dev;
    257 	u_long cmd;
    258 	caddr_t data;
    259 	int flag;
    260 	struct proc *p;
    261 {
    262 	USB_GET_SC(usb, USBUNIT(dev), sc);
    263 
    264 	if (sc == 0 || !sc->sc_running)
    265 		return (ENXIO);
    266 	switch (cmd) {
    267 #ifdef USB_DEBUG
    268 	case USB_SETDEBUG:
    269 		usbdebug = uhcidebug = ohcidebug = *(int *)data;
    270 		break;
    271 #endif
    272 #if 0
    273 	case USB_DISCOVER:
    274 		usb_discover(sc);
    275 		break;
    276 #endif
    277 	case USB_REQUEST:
    278 	{
    279 		struct usb_ctl_request *ur = (void *)data;
    280 		int len = UGETW(ur->request.wLength);
    281 		struct iovec iov;
    282 		struct uio uio;
    283 		void *ptr = 0;
    284 		int addr = ur->addr;
    285 		usbd_status r;
    286 		int error = 0;
    287 
    288 		DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
    289 		if (len < 0 || len > 32768)
    290 			return (EINVAL);
    291 		if (addr < 0 || addr >= USB_MAX_DEVICES ||
    292 		    sc->sc_bus->devices[addr] == 0)
    293 			return (EINVAL);
    294 		if (len != 0) {
    295 			iov.iov_base = (caddr_t)ur->data;
    296 			iov.iov_len = len;
    297 			uio.uio_iov = &iov;
    298 			uio.uio_iovcnt = 1;
    299 			uio.uio_resid = len;
    300 			uio.uio_offset = 0;
    301 			uio.uio_segflg = UIO_USERSPACE;
    302 			uio.uio_rw =
    303 				ur->request.bmRequestType & UT_READ ?
    304 				UIO_READ : UIO_WRITE;
    305 			uio.uio_procp = p;
    306 			ptr = malloc(len, M_TEMP, M_WAITOK);
    307 			if (uio.uio_rw == UIO_WRITE) {
    308 				error = uiomove(ptr, len, &uio);
    309 				if (error)
    310 					goto ret;
    311 			}
    312 		}
    313 		r = usbd_do_request_flags(sc->sc_bus->devices[addr],
    314 					  &ur->request, ptr,
    315 					  ur->flags, &ur->actlen);
    316 		if (r != USBD_NORMAL_COMPLETION) {
    317 			error = EIO;
    318 			goto ret;
    319 		}
    320 		if (len != 0) {
    321 			if (uio.uio_rw == UIO_READ) {
    322 				error = uiomove(ptr, len, &uio);
    323 				if (error)
    324 					goto ret;
    325 			}
    326 		}
    327 	ret:
    328 		if (ptr)
    329 			free(ptr, M_TEMP);
    330 		return (error);
    331 	}
    332 
    333 	case USB_DEVICEINFO:
    334 	{
    335 		struct usb_device_info *di = (void *)data;
    336 		int addr = di->addr;
    337 		usbd_device_handle dev;
    338 
    339 		if (addr < 1 || addr >= USB_MAX_DEVICES)
    340 			return (EINVAL);
    341 		dev = sc->sc_bus->devices[addr];
    342 		if (dev == 0)
    343 			return (ENXIO);
    344 		usbd_fill_deviceinfo(dev, di);
    345 		break;
    346 	}
    347 
    348 	case USB_DEVICESTATS:
    349 		*(struct usb_device_stats *)data = sc->sc_bus->stats;
    350 		break;
    351 
    352 	default:
    353 		return (ENXIO);
    354 	}
    355 	return (0);
    356 }
    357 
    358 int
    359 usbpoll(dev, events, p)
    360 	dev_t dev;
    361 	int events;
    362 	struct proc *p;
    363 {
    364 	int revents, s;
    365 	USB_GET_SC(usb, USBUNIT(dev), sc);
    366 
    367 	DPRINTFN(2, ("usbpoll: sc=%p events=0x%x\n", sc, events));
    368 	s = splusb();
    369 	revents = 0;
    370 	if (events & (POLLOUT | POLLWRNORM))
    371 		if (sc->sc_bus->needs_explore)
    372 			revents |= events & (POLLOUT | POLLWRNORM);
    373 	DPRINTFN(2, ("usbpoll: revents=0x%x\n", revents));
    374 	if (revents == 0) {
    375 		if (events & (POLLOUT | POLLWRNORM)) {
    376 			DPRINTFN(2, ("usbpoll: selrecord\n"));
    377 			selrecord(p, &sc->sc_consel);
    378 		}
    379 	}
    380 	splx(s);
    381 	return (revents);
    382 }
    383 
    384 #if 0
    385 int
    386 usb_bus_count()
    387 {
    388 	int i, n;
    389 
    390 	for (i = n = 0; i < usb_cd.cd_ndevs; i++)
    391 		if (usb_cd.cd_devs[i])
    392 			n++;
    393 	return (n);
    394 }
    395 #endif
    396 
    397 #if 0
    398 usbd_status
    399 usb_get_bus_handle(n, h)
    400 	int n;
    401 	usbd_bus_handle *h;
    402 {
    403 	int i;
    404 
    405 	for (i = 0; i < usb_cd.cd_ndevs; i++)
    406 		if (usb_cd.cd_devs[i] && n-- == 0) {
    407 			*h = usb_cd.cd_devs[i];
    408 			return (USBD_NORMAL_COMPLETION);
    409 		}
    410 	return (USBD_INVAL);
    411 }
    412 #endif
    413 
    414 usbd_status
    415 usb_discover(sc)
    416 	struct usb_softc *sc;
    417 {
    418 	int s;
    419 
    420 	/* Explore device tree from the root */
    421 	/* We need mutual exclusion while traversing the device tree. */
    422 	do {
    423 		s = splusb();
    424 		while (sc->sc_exploring)
    425 			tsleep(&sc->sc_exploring, PRIBIO, "usbdis", 0);
    426 		sc->sc_exploring = 1;
    427 		sc->sc_bus->needs_explore = 0;
    428 		splx(s);
    429 
    430 		sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
    431 
    432 		s = splusb();
    433 		sc->sc_exploring = 0;
    434 		wakeup(&sc->sc_exploring);
    435 		splx(s);
    436 	} while (sc->sc_bus->needs_explore);
    437 	return (USBD_NORMAL_COMPLETION);
    438 }
    439 
    440 void
    441 usb_needs_explore(bus)
    442 	usbd_bus_handle bus;
    443 {
    444 	bus->needs_explore = 1;
    445 	selwakeup(&bus->usbctl->sc_consel);
    446 	wakeup(&bus->needs_explore);
    447 }
    448 
    449 int
    450 usb_activate(self, act)
    451 	struct device *self;
    452 	enum devact act;
    453 {
    454 	panic("usb_activate\n");
    455 	return (0);
    456 }
    457 
    458 int
    459 usb_detach(self, flags)
    460 	struct device  *self;
    461 	int flags;
    462 {
    463 	panic("usb_detach\n");
    464 	return (0);
    465 }
    466 
    467 #if defined(__FreeBSD__)
    468 DRIVER_MODULE(usb, root, usb_driver, usb_devclass, 0, 0);
    469 #endif
    470