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