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