Home | History | Annotate | Line # | Download | only in usb
uhid.c revision 1.13
      1 /*	$NetBSD: uhid.c,v 1.13 1999/01/07 02:22:51 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 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/kernel.h>
     44 #include <sys/malloc.h>
     45 #if defined(__NetBSD__)
     46 #include <sys/ioctl.h>
     47 #elif defined(__FreeBSD__)
     48 #include <sys/ioccom.h>
     49 #include <sys/filio.h>
     50 #include <sys/module.h>
     51 #include <sys/bus.h>
     52 #endif
     53 #include <sys/device.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/tty.h>
     56 #include <sys/file.h>
     57 #include <sys/select.h>
     58 #include <sys/proc.h>
     59 #include <sys/vnode.h>
     60 #include <sys/poll.h>
     61 
     62 #include <dev/usb/usb.h>
     63 #include <dev/usb/usbhid.h>
     64 
     65 #include <dev/usb/usbdi.h>
     66 #include <dev/usb/usbdi_util.h>
     67 #include <dev/usb/hid.h>
     68 #include <dev/usb/usb_quirks.h>
     69 
     70 #ifdef USB_DEBUG
     71 #define DPRINTF(x)	if (uhiddebug) printf x
     72 #define DPRINTFN(n,x)	if (uhiddebug>(n)) printf x
     73 int	uhiddebug = 0;
     74 #else
     75 #define DPRINTF(x)
     76 #define DPRINTFN(n,x)
     77 #endif
     78 
     79 struct uhid_softc {
     80 	bdevice sc_dev;		/* base device */
     81 	usbd_interface_handle sc_iface;	/* interface */
     82 	usbd_pipe_handle sc_intrpipe;	/* interrupt pipe */
     83 	int sc_ep_addr;
     84 
     85 	int sc_isize;
     86 	int sc_osize;
     87 	int sc_fsize;
     88 	u_int8_t sc_iid;
     89 	u_int8_t sc_oid;
     90 	u_int8_t sc_fid;
     91 
     92 	char *sc_ibuf;
     93 	char *sc_obuf;
     94 
     95 	void *sc_repdesc;
     96 	int sc_repdesc_size;
     97 
     98 	struct clist sc_q;
     99 	struct selinfo sc_rsel;
    100 	u_char sc_state;	/* driver state */
    101 #define	UHID_OPEN	0x01	/* device is open */
    102 #define	UHID_ASLP	0x02	/* waiting for device data */
    103 #define UHID_NEEDCLEAR	0x04	/* needs clearing endpoint stall */
    104 #define UHID_IMMED	0x08	/* return read data immediately */
    105 	int sc_disconnected;	/* device is gone */
    106 };
    107 
    108 #define	UHIDUNIT(dev)	(minor(dev))
    109 #define	UHID_CHUNK	128	/* chunk size for read */
    110 #define	UHID_BSIZE	1020	/* buffer size */
    111 
    112 int uhidopen __P((dev_t, int, int, struct proc *));
    113 int uhidclose __P((dev_t, int, int, struct proc *p));
    114 int uhidread __P((dev_t, struct uio *uio, int));
    115 int uhidwrite __P((dev_t, struct uio *uio, int));
    116 int uhidioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
    117 int uhidpoll __P((dev_t, int, struct proc *));
    118 void uhid_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
    119 void uhid_disco __P((void *));
    120 
    121 USB_DECLARE_DRIVER(uhid);
    122 
    123 USB_MATCH(uhid)
    124 {
    125 	USB_MATCH_START(uhid, uaa);
    126 	usb_interface_descriptor_t *id;
    127 
    128 	if (!uaa->iface)
    129 		return (UMATCH_NONE);
    130 	id = usbd_get_interface_descriptor(uaa->iface);
    131 	if (!id || id->bInterfaceClass != UCLASS_HID)
    132 		return (UMATCH_NONE);
    133 	return (UMATCH_IFACECLASS_GENERIC);
    134 }
    135 
    136 USB_ATTACH(uhid)
    137 {
    138 	USB_ATTACH_START(uhid, sc, uaa);
    139 	usbd_interface_handle iface = uaa->iface;
    140 	usb_interface_descriptor_t *id;
    141 	usb_endpoint_descriptor_t *ed;
    142 	int size;
    143 	void *desc;
    144 	usbd_status r;
    145 	char devinfo[1024];
    146 
    147 	sc->sc_disconnected = 1;
    148 	sc->sc_iface = iface;
    149 	id = usbd_get_interface_descriptor(iface);
    150 	usbd_devinfo(uaa->device, 0, devinfo);
    151 	USB_ATTACH_SETUP;
    152 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    153 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
    154 
    155 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    156 	if (!ed) {
    157 		printf("%s: could not read endpoint descriptor\n",
    158 		       USBDEVNAME(sc->sc_dev));
    159 		USB_ATTACH_ERROR_RETURN;
    160 	}
    161 
    162 	DPRINTFN(10,("uhid_attach: bLength=%d bDescriptorType=%d "
    163 		     "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
    164 		     " bInterval=%d\n",
    165 		     ed->bLength, ed->bDescriptorType,
    166 		     ed->bEndpointAddress & UE_ADDR,
    167 		     ed->bEndpointAddress & UE_IN ? "in" : "out",
    168 		     ed->bmAttributes & UE_XFERTYPE,
    169 		     UGETW(ed->wMaxPacketSize), ed->bInterval));
    170 
    171 	if ((ed->bEndpointAddress & UE_IN) != UE_IN ||
    172 	    (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    173 		printf("%s: unexpected endpoint\n", USBDEVNAME(sc->sc_dev));
    174 		USB_ATTACH_ERROR_RETURN;
    175 	}
    176 
    177 	sc->sc_ep_addr = ed->bEndpointAddress;
    178 	sc->sc_disconnected = 0;
    179 
    180 	r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_USB);
    181 	if (r != USBD_NORMAL_COMPLETION) {
    182 		printf("%s: no report descriptor\n", USBDEVNAME(sc->sc_dev));
    183 		USB_ATTACH_ERROR_RETURN;
    184 	}
    185 
    186 	(void)usbd_set_idle(iface, 0, 0);
    187 
    188 	sc->sc_isize = hid_report_size(desc, size, hid_input,   &sc->sc_iid);
    189 	sc->sc_osize = hid_report_size(desc, size, hid_output,  &sc->sc_oid);
    190 	sc->sc_fsize = hid_report_size(desc, size, hid_feature, &sc->sc_fid);
    191 
    192 	sc->sc_repdesc = desc;
    193 	sc->sc_repdesc_size = size;
    194 
    195 	USB_ATTACH_SUCCESS_RETURN;
    196 }
    197 
    198 #if defined(__FreeBSD__)
    199 static int
    200 uhid_detach(device_t self)
    201 {
    202         struct uhid_softc *sc = device_get_softc(self);
    203 	char *devinfo = (char *) device_get_desc(self);
    204 
    205 	if (devinfo) {
    206 		device_set_desc(self, NULL);
    207 		free(devinfo, M_USB);
    208 	}
    209 	return 0;
    210 }
    211 #endif
    212 
    213 void
    214 uhid_disco(p)
    215 	void *p;
    216 {
    217 	struct uhid_softc *sc = p;
    218 
    219 	DPRINTF(("ums_hid: sc=%p\n", sc));
    220 	usbd_abort_pipe(sc->sc_intrpipe);
    221 	sc->sc_disconnected = 1;
    222 }
    223 
    224 void
    225 uhid_intr(reqh, addr, status)
    226 	usbd_request_handle reqh;
    227 	usbd_private_handle addr;
    228 	usbd_status status;
    229 {
    230 	struct uhid_softc *sc = addr;
    231 
    232 	DPRINTFN(5, ("uhid_intr: status=%d\n", status));
    233 	DPRINTFN(5, ("uhid_intr: data = %02x %02x %02x\n",
    234 		     sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
    235 
    236 	if (status == USBD_CANCELLED)
    237 		return;
    238 
    239 	if (status != USBD_NORMAL_COMPLETION) {
    240 		DPRINTF(("uhid_intr: status=%d\n", status));
    241 		sc->sc_state |= UHID_NEEDCLEAR;
    242 		return;
    243 	}
    244 
    245 	(void) b_to_q(sc->sc_ibuf, sc->sc_isize, &sc->sc_q);
    246 
    247 	if (sc->sc_state & UHID_ASLP) {
    248 		sc->sc_state &= ~UHID_ASLP;
    249 		DPRINTFN(5, ("uhid_intr: waking %p\n", sc));
    250 		wakeup((caddr_t)sc);
    251 	}
    252 	selwakeup(&sc->sc_rsel);
    253 }
    254 
    255 int
    256 uhidopen(dev, flag, mode, p)
    257 	dev_t dev;
    258 	int flag;
    259 	int mode;
    260 	struct proc *p;
    261 {
    262 	usbd_status r;
    263 	USB_GET_SC_OPEN(uhid, UHIDUNIT(dev), sc);
    264 
    265 	if (!sc)
    266 		return (ENXIO);
    267 
    268 	DPRINTF(("uhidopen: sc=%p, disco=%d\n", sc, sc->sc_disconnected));
    269 
    270 	if (sc->sc_disconnected)
    271 		return (EIO);
    272 
    273 	if (sc->sc_state & UHID_OPEN)
    274 		return (EBUSY);
    275 
    276 #if defined(__NetBSD__)
    277 	if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1)
    278 		return (ENOMEM);
    279 #elif defined(__FreeBSD__)
    280 	clist_alloc_cblocks(&sc->sc_q, UHID_BSIZE, 0);
    281 #endif
    282 
    283 	sc->sc_state |= UHID_OPEN;
    284 	sc->sc_state &= ~UHID_IMMED;
    285 
    286 	sc->sc_ibuf = malloc(sc->sc_isize, M_USB, M_WAITOK);
    287 	sc->sc_obuf = malloc(sc->sc_osize, M_USB, M_WAITOK);
    288 
    289 	/* Set up interrupt pipe. */
    290 	r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
    291 				USBD_SHORT_XFER_OK,
    292 				&sc->sc_intrpipe, sc, sc->sc_ibuf,
    293 				sc->sc_isize, uhid_intr);
    294 	if (r != USBD_NORMAL_COMPLETION) {
    295 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
    296 			 "error=%d\n",r));
    297 		sc->sc_state &= ~UHID_OPEN;
    298 		return (EIO);
    299 	}
    300 	usbd_set_disco(sc->sc_intrpipe, uhid_disco, sc);
    301 
    302 	return (0);
    303 }
    304 
    305 int
    306 uhidclose(dev, flag, mode, p)
    307 	dev_t dev;
    308 	int flag;
    309 	int mode;
    310 	struct proc *p;
    311 {
    312 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    313 
    314 	if (sc->sc_disconnected)
    315 		return (EIO);
    316 
    317 	DPRINTF(("uhidclose: sc=%p\n", sc));
    318 
    319 	/* Disable interrupts. */
    320 	usbd_abort_pipe(sc->sc_intrpipe);
    321 	usbd_close_pipe(sc->sc_intrpipe);
    322 
    323 	sc->sc_state &= ~UHID_OPEN;
    324 
    325 #if defined(__NetBSD__)
    326 	clfree(&sc->sc_q);
    327 #elif defined(__FreeBSD__)
    328 	clist_free_cblocks(&sc->sc_q);
    329 #endif
    330 
    331 	free(sc->sc_ibuf, M_USB);
    332 	free(sc->sc_obuf, M_USB);
    333 
    334 	return (0);
    335 }
    336 
    337 int
    338 uhidread(dev, uio, flag)
    339 	dev_t dev;
    340 	struct uio *uio;
    341 	int flag;
    342 {
    343 	int s;
    344 	int error = 0;
    345 	size_t length;
    346 	u_char buffer[UHID_CHUNK];
    347 	usbd_status r;
    348 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    349 
    350 	if (sc->sc_disconnected)
    351 		return (EIO);
    352 
    353 	DPRINTFN(1, ("uhidread\n"));
    354 	if (sc->sc_state & UHID_IMMED) {
    355 		DPRINTFN(1, ("uhidread immed\n"));
    356 
    357 		r = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
    358 				    sc->sc_iid, sc->sc_ibuf, sc->sc_isize);
    359 		if (r != USBD_NORMAL_COMPLETION)
    360 			return (EIO);
    361 		return (uiomove(buffer, sc->sc_isize, uio));
    362 	}
    363 
    364 	s = splusb();
    365 	while (sc->sc_q.c_cc == 0) {
    366 		if (flag & IO_NDELAY) {
    367 			splx(s);
    368 			return EWOULDBLOCK;
    369 		}
    370 		sc->sc_state |= UHID_ASLP;
    371 		DPRINTFN(5, ("uhidread: sleep on %p\n", sc));
    372 		error = tsleep((caddr_t)sc, PZERO | PCATCH, "uhidrea", 0);
    373 		DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
    374 		if (error) {
    375 			sc->sc_state &= ~UHID_ASLP;
    376 			splx(s);
    377 			return (error);
    378 		}
    379 		if (sc->sc_state & UHID_NEEDCLEAR) {
    380 			DPRINTFN(-1,("uhidread: clearing stall\n"));
    381 			sc->sc_state &= ~UHID_NEEDCLEAR;
    382 			usbd_clear_endpoint_stall(sc->sc_intrpipe);
    383 		}
    384 	}
    385 	splx(s);
    386 
    387 	/* Transfer as many chunks as possible. */
    388 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
    389 		length = min(sc->sc_q.c_cc, uio->uio_resid);
    390 		if (length > sizeof(buffer))
    391 			length = sizeof(buffer);
    392 
    393 		/* Remove a small chunk from the input queue. */
    394 		(void) q_to_b(&sc->sc_q, buffer, length);
    395 		DPRINTFN(5, ("uhidread: got %d chars\n", length));
    396 
    397 		/* Copy the data to the user process. */
    398 		if ((error = uiomove(buffer, length, uio)) != 0)
    399 			break;
    400 	}
    401 
    402 	return (error);
    403 }
    404 
    405 int
    406 uhidwrite(dev, uio, flag)
    407 	dev_t dev;
    408 	struct uio *uio;
    409 	int flag;
    410 {
    411 	int error;
    412 	int size;
    413 	usbd_status r;
    414 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    415 
    416 	if (sc->sc_disconnected)
    417 		return (EIO);
    418 
    419 	DPRINTFN(1, ("uhidwrite\n"));
    420 
    421 	size = sc->sc_osize;
    422 	error = 0;
    423 	while (uio->uio_resid > 0) {
    424 		if (uio->uio_resid != size)
    425 			return (0);
    426 		if ((error = uiomove(sc->sc_obuf, size, uio)) != 0)
    427 			break;
    428 		if (sc->sc_oid)
    429 			r = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
    430 					    sc->sc_obuf[0],
    431 					    sc->sc_obuf+1, size-1);
    432 		else
    433 			r = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
    434 					    0, sc->sc_obuf, size);
    435 		if (r != USBD_NORMAL_COMPLETION) {
    436 			error = EIO;
    437 			break;
    438 		}
    439 	}
    440 	return (error);
    441 }
    442 
    443 int
    444 uhidioctl(dev, cmd, addr, flag, p)
    445 	dev_t dev;
    446 	u_long cmd;
    447 	caddr_t addr;
    448 	int flag;
    449 	struct proc *p;
    450 {
    451 	struct usb_ctl_report_desc *rd;
    452 	struct usb_ctl_report *re;
    453 	int size, id;
    454 	usbd_status r;
    455 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    456 
    457 	if (sc->sc_disconnected)
    458 		return (EIO);
    459 
    460 	DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
    461 	switch (cmd) {
    462 	case FIONBIO:
    463 		/* All handled in the upper FS layer. */
    464 		break;
    465 
    466 	case USB_GET_REPORT_DESC:
    467 		rd = (struct usb_ctl_report_desc *)addr;
    468 		size = min(sc->sc_repdesc_size, sizeof rd->data);
    469 		rd->size = size;
    470 		memcpy(rd->data, sc->sc_repdesc, size);
    471 		break;
    472 
    473 	case USB_SET_IMMED:
    474 		if (*(int *)addr) {
    475 			/* XXX should read into ibuf, but does it matter */
    476 			r = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
    477 					    sc->sc_iid, sc->sc_ibuf,
    478 					    sc->sc_isize);
    479 			if (r != USBD_NORMAL_COMPLETION)
    480 				return (EOPNOTSUPP);
    481 
    482 			sc->sc_state |=  UHID_IMMED;
    483 		} else
    484 			sc->sc_state &= ~UHID_IMMED;
    485 		break;
    486 
    487 	case USB_GET_REPORT:
    488 		re = (struct usb_ctl_report *)addr;
    489 		switch (re->report) {
    490 		case UHID_INPUT_REPORT:
    491 			size = sc->sc_isize;
    492 			id = sc->sc_iid;
    493 			break;
    494 		case UHID_OUTPUT_REPORT:
    495 			size = sc->sc_osize;
    496 			id = sc->sc_oid;
    497 			break;
    498 		case UHID_FEATURE_REPORT:
    499 			size = sc->sc_fsize;
    500 			id = sc->sc_fid;
    501 			break;
    502 		default:
    503 			return (EINVAL);
    504 		}
    505 		r = usbd_get_report(sc->sc_iface, re->report, id,
    506 				    re->data, size);
    507 		if (r != USBD_NORMAL_COMPLETION)
    508 			return (EIO);
    509 		break;
    510 
    511 	default:
    512 		return (EINVAL);
    513 	}
    514 	return (0);
    515 }
    516 
    517 int
    518 uhidpoll(dev, events, p)
    519 	dev_t dev;
    520 	int events;
    521 	struct proc *p;
    522 {
    523 	int revents = 0;
    524 	int s;
    525 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    526 
    527 	if (sc->sc_disconnected)
    528 		return (EIO);
    529 
    530 	s = splusb();
    531 	if (events & (POLLOUT | POLLWRNORM))
    532 		revents |= events & (POLLOUT | POLLWRNORM);
    533 	if (events & (POLLIN | POLLRDNORM)) {
    534 		if (sc->sc_q.c_cc > 0)
    535 			revents |= events & (POLLIN | POLLRDNORM);
    536 		else
    537 			selrecord(p, &sc->sc_rsel);
    538 	}
    539 
    540 	splx(s);
    541 	return (revents);
    542 }
    543 
    544 #if defined(__FreeBSD__)
    545 DRIVER_MODULE(uhid, usb, uhid_driver, uhid_devclass, usb_driver_load, 0);
    546 #endif
    547