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