Home | History | Annotate | Line # | Download | only in usb
uhid.c revision 1.50
      1 /*	$NetBSD: uhid.c,v 1.50 2002/02/20 20:30:12 christos 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 (lennart (at) augustsson.net) 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/devclass/hid1_1.pdf
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: uhid.c,v 1.50 2002/02/20 20:30:12 christos Exp $");
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/kernel.h>
     50 #include <sys/malloc.h>
     51 #include <sys/signalvar.h>
     52 #include <sys/device.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/conf.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/usbdevs.h>
     66 #include <dev/usb/usbdi.h>
     67 #include <dev/usb/usbdi_util.h>
     68 #include <dev/usb/hid.h>
     69 #include <dev/usb/usb_quirks.h>
     70 
     71 #include <dev/usb/uhidev.h>
     72 
     73 #ifdef UHID_DEBUG
     74 #define DPRINTF(x)	if (uhiddebug) logprintf x
     75 #define DPRINTFN(n,x)	if (uhiddebug>(n)) logprintf x
     76 int	uhiddebug = 0;
     77 #else
     78 #define DPRINTF(x)
     79 #define DPRINTFN(n,x)
     80 #endif
     81 
     82 struct uhid_softc {
     83 	struct uhidev sc_hdev;
     84 
     85 	int sc_isize;
     86 	int sc_osize;
     87 	int sc_fsize;
     88 
     89 	u_char *sc_obuf;
     90 
     91 	struct clist sc_q;
     92 	struct selinfo sc_rsel;
     93 	usb_proc_ptr sc_async;	/* process that wants SIGIO */
     94 	u_char sc_state;	/* driver state */
     95 #define	UHID_ASLP	0x01	/* waiting for device data */
     96 #define UHID_IMMED	0x02	/* return read data immediately */
     97 
     98 	int sc_refcnt;
     99 	u_char sc_dying;
    100 };
    101 
    102 #define	UHIDUNIT(dev)	(minor(dev))
    103 #define	UHID_CHUNK	128	/* chunk size for read */
    104 #define	UHID_BSIZE	1020	/* buffer size */
    105 
    106 cdev_decl(uhid);
    107 
    108 Static void uhid_intr(struct uhidev *, void *, u_int len);
    109 
    110 Static int uhid_do_read(struct uhid_softc *, struct uio *uio, int);
    111 Static int uhid_do_write(struct uhid_softc *, struct uio *uio, int);
    112 Static int uhid_do_ioctl(struct uhid_softc*, u_long, caddr_t, int, usb_proc_ptr);
    113 
    114 USB_DECLARE_DRIVER(uhid);
    115 
    116 int
    117 uhid_match(struct device *parent, struct cfdata *match, void *aux)
    118 {
    119 	struct uhidev_attach_arg *uha = aux;
    120 
    121 	DPRINTF(("uhid_match: report=%d\n", uha->reportid));
    122 
    123 	if (uha->matchlvl)
    124 		return (uha->matchlvl);
    125 	return (UMATCH_IFACECLASS_GENERIC);
    126 }
    127 
    128 void
    129 uhid_attach(struct device *parent, struct device *self, void *aux)
    130 {
    131 	struct uhid_softc *sc = (struct uhid_softc *)self;
    132 	struct uhidev_attach_arg *uha = aux;
    133 	int size, repid;
    134 	void *desc;
    135 
    136 	sc->sc_hdev.sc_intr = uhid_intr;
    137 	sc->sc_hdev.sc_parent = uha->parent;
    138 	sc->sc_hdev.sc_report_id = uha->reportid;
    139 
    140 	uhidev_get_report_desc(uha->parent, &desc, &size);
    141 	repid = uha->reportid;
    142 	sc->sc_isize = hid_report_size(desc, size, hid_input,   repid);
    143 	sc->sc_osize = hid_report_size(desc, size, hid_output,  repid);
    144 	sc->sc_fsize = hid_report_size(desc, size, hid_feature, repid);
    145 
    146 	printf(": input=%d, output=%d, feature=%d\n",
    147 	       sc->sc_isize, sc->sc_osize, sc->sc_fsize);
    148 
    149 	USB_ATTACH_SUCCESS_RETURN;
    150 }
    151 
    152 int
    153 uhid_activate(device_ptr_t self, enum devact act)
    154 {
    155 	struct uhid_softc *sc = (struct uhid_softc *)self;
    156 
    157 	switch (act) {
    158 	case DVACT_ACTIVATE:
    159 		return (EOPNOTSUPP);
    160 		break;
    161 
    162 	case DVACT_DEACTIVATE:
    163 		sc->sc_dying = 1;
    164 		break;
    165 	}
    166 	return (0);
    167 }
    168 
    169 int
    170 uhid_detach(struct device *self, int flags)
    171 {
    172 	struct uhid_softc *sc = (struct uhid_softc *)self;
    173 	int s;
    174 	int maj, mn;
    175 
    176 	DPRINTF(("uhid_detach: sc=%p flags=%d\n", sc, flags));
    177 
    178 	sc->sc_dying = 1;
    179 
    180 	if (sc->sc_hdev.sc_state & UHIDEV_OPEN) {
    181 		s = splusb();
    182 		if (--sc->sc_refcnt >= 0) {
    183 			/* Wake everyone */
    184 			wakeup(&sc->sc_q);
    185 			/* Wait for processes to go away. */
    186 			usb_detach_wait(USBDEV(sc->sc_hdev.sc_dev));
    187 		}
    188 		splx(s);
    189 	}
    190 
    191 	/* locate the major number */
    192 	for (maj = 0; maj < nchrdev; maj++)
    193 		if (cdevsw[maj].d_open == uhidopen)
    194 			break;
    195 
    196 	/* Nuke the vnodes for any open instances (calls close). */
    197 	mn = self->dv_unit;
    198 	vdevgone(maj, mn, mn, VCHR);
    199 
    200 #if 0
    201 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH,
    202 			   sc->sc_hdev.sc_parent->sc_udev,
    203 			   USBDEV(sc->sc_hdev.sc_dev));
    204 #endif
    205 
    206 	return (0);
    207 }
    208 
    209 void
    210 uhid_intr(struct uhidev *addr, void *data, u_int len)
    211 {
    212 	struct uhid_softc *sc = (struct uhid_softc *)addr;
    213 
    214 #ifdef UHID_DEBUG
    215 	if (uhiddebug > 5) {
    216 		u_int32_t i;
    217 
    218 		DPRINTF(("uhid_intr: data ="));
    219 		for (i = 0; i < len; i++)
    220 			DPRINTF((" %02x", ((u_char*)data)[i]));
    221 		DPRINTF(("\n"));
    222 	}
    223 #endif
    224 
    225 	(void)b_to_q(data, len, &sc->sc_q);
    226 
    227 	if (sc->sc_state & UHID_ASLP) {
    228 		sc->sc_state &= ~UHID_ASLP;
    229 		DPRINTFN(5, ("uhid_intr: waking %p\n", &sc->sc_q));
    230 		wakeup(&sc->sc_q);
    231 	}
    232 	selwakeup(&sc->sc_rsel);
    233 	if (sc->sc_async != NULL) {
    234 		DPRINTFN(3, ("uhid_intr: sending SIGIO %p\n", sc->sc_async));
    235 		psignal(sc->sc_async, SIGIO);
    236 	}
    237 }
    238 
    239 int
    240 uhidopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
    241 {
    242 	struct uhid_softc *sc;
    243 	int error;
    244 
    245 	USB_GET_SC_OPEN(uhid, UHIDUNIT(dev), sc);
    246 
    247 	DPRINTF(("uhidopen: sc=%p\n", sc));
    248 
    249 	if (sc->sc_dying)
    250 		return (ENXIO);
    251 
    252 	error = uhidev_open(&sc->sc_hdev);
    253 	if (error)
    254 		return (error);
    255 
    256 	if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) {
    257 		uhidev_close(&sc->sc_hdev);
    258 		return (ENOMEM);
    259 	}
    260 	sc->sc_obuf = malloc(sc->sc_osize, M_USBDEV, M_WAITOK);
    261 	sc->sc_state &= ~UHID_IMMED;
    262 	sc->sc_async = NULL;
    263 
    264 	return (0);
    265 }
    266 
    267 int
    268 uhidclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
    269 {
    270 	struct uhid_softc *sc;
    271 
    272 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    273 
    274 	DPRINTF(("uhidclose: sc=%p\n", sc));
    275 
    276 	clfree(&sc->sc_q);
    277 	free(sc->sc_obuf, M_USBDEV);
    278 	sc->sc_async = NULL;
    279 	uhidev_close(&sc->sc_hdev);
    280 
    281 	return (0);
    282 }
    283 
    284 int
    285 uhid_do_read(struct uhid_softc *sc, struct uio *uio, int flag)
    286 {
    287 	int s;
    288 	int error = 0;
    289 	int extra;
    290 	size_t length;
    291 	u_char buffer[UHID_CHUNK];
    292 	usbd_status err;
    293 
    294 	DPRINTFN(1, ("uhidread\n"));
    295 	if (sc->sc_state & UHID_IMMED) {
    296 		DPRINTFN(1, ("uhidread immed\n"));
    297 		extra = sc->sc_hdev.sc_report_id != 0;
    298 		err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT,
    299 					buffer, sc->sc_isize + extra);
    300 		if (err)
    301 			return (EIO);
    302 		return (uiomove(buffer+extra, sc->sc_isize, uio));
    303 	}
    304 
    305 	s = splusb();
    306 	while (sc->sc_q.c_cc == 0) {
    307 		if (flag & IO_NDELAY) {
    308 			splx(s);
    309 			return (EWOULDBLOCK);
    310 		}
    311 		sc->sc_state |= UHID_ASLP;
    312 		DPRINTFN(5, ("uhidread: sleep on %p\n", &sc->sc_q));
    313 		error = tsleep(&sc->sc_q, PZERO | PCATCH, "uhidrea", 0);
    314 		DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
    315 		if (sc->sc_dying)
    316 			error = EIO;
    317 		if (error) {
    318 			sc->sc_state &= ~UHID_ASLP;
    319 			break;
    320 		}
    321 	}
    322 	splx(s);
    323 
    324 	/* Transfer as many chunks as possible. */
    325 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
    326 		length = min(sc->sc_q.c_cc, uio->uio_resid);
    327 		if (length > sizeof(buffer))
    328 			length = sizeof(buffer);
    329 
    330 		/* Remove a small chunk from the input queue. */
    331 		(void) q_to_b(&sc->sc_q, buffer, length);
    332 		DPRINTFN(5, ("uhidread: got %lu chars\n", (u_long)length));
    333 
    334 		/* Copy the data to the user process. */
    335 		if ((error = uiomove(buffer, length, uio)) != 0)
    336 			break;
    337 	}
    338 
    339 	return (error);
    340 }
    341 
    342 int
    343 uhidread(dev_t dev, struct uio *uio, int flag)
    344 {
    345 	struct uhid_softc *sc;
    346 	int error;
    347 
    348 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    349 
    350 	sc->sc_refcnt++;
    351 	error = uhid_do_read(sc, uio, flag);
    352 	if (--sc->sc_refcnt < 0)
    353 		usb_detach_wakeup(USBDEV(sc->sc_hdev.sc_dev));
    354 	return (error);
    355 }
    356 
    357 int
    358 uhid_do_write(struct uhid_softc *sc, struct uio *uio, int flag)
    359 {
    360 	int error;
    361 	int size;
    362 	usbd_status err;
    363 
    364 	DPRINTFN(1, ("uhidwrite\n"));
    365 
    366 	if (sc->sc_dying)
    367 		return (EIO);
    368 
    369 	size = sc->sc_osize;
    370 	error = 0;
    371 	if (uio->uio_resid != size)
    372 		return (EINVAL);
    373 	error = uiomove(sc->sc_obuf, size, uio);
    374 	if (!error) {
    375 		err = uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT,
    376 					sc->sc_obuf, size);
    377 		if (err)
    378 			error = EIO;
    379 	}
    380 
    381 	return (error);
    382 }
    383 
    384 int
    385 uhidwrite(dev_t dev, struct uio *uio, int flag)
    386 {
    387 	struct uhid_softc *sc;
    388 	int error;
    389 
    390 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    391 
    392 	sc->sc_refcnt++;
    393 	error = uhid_do_write(sc, uio, flag);
    394 	if (--sc->sc_refcnt < 0)
    395 		usb_detach_wakeup(USBDEV(sc->sc_hdev.sc_dev));
    396 	return (error);
    397 }
    398 
    399 int
    400 uhid_do_ioctl(struct uhid_softc *sc, u_long cmd, caddr_t addr,
    401 	      int flag, usb_proc_ptr p)
    402 {
    403 	struct usb_ctl_report_desc *rd;
    404 	struct usb_ctl_report *re;
    405 	u_char buffer[UHID_CHUNK];
    406 	int size, extra;
    407 	usbd_status err;
    408 	void *desc;
    409 
    410 	DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
    411 
    412 	if (sc->sc_dying)
    413 		return (EIO);
    414 
    415 	switch (cmd) {
    416 	case FIONBIO:
    417 		/* All handled in the upper FS layer. */
    418 		break;
    419 
    420 	case FIOASYNC:
    421 		if (*(int *)addr) {
    422 			if (sc->sc_async != NULL)
    423 				return (EBUSY);
    424 			sc->sc_async = p;
    425 			DPRINTF(("uhid_do_ioctl: FIOASYNC %p\n", p));
    426 		} else
    427 			sc->sc_async = NULL;
    428 		break;
    429 
    430 	/* XXX this is not the most general solution. */
    431 	case TIOCSPGRP:
    432 		if (sc->sc_async == NULL)
    433 			return (EINVAL);
    434 		if (*(int *)addr != sc->sc_async->p_pgid)
    435 			return (EPERM);
    436 		break;
    437 
    438 	case USB_GET_REPORT_DESC:
    439 		uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size);
    440 		rd = (struct usb_ctl_report_desc *)addr;
    441 		size = min(size, sizeof rd->ucrd_data);
    442 		rd->ucrd_size = size;
    443 		memcpy(rd->ucrd_data, desc, size);
    444 		break;
    445 
    446 	case USB_SET_IMMED:
    447 		if (*(int *)addr) {
    448 			extra = sc->sc_hdev.sc_report_id != 0;
    449 			err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT,
    450 						buffer, sc->sc_isize + extra);
    451 			if (err)
    452 				return (EOPNOTSUPP);
    453 
    454 			sc->sc_state |=  UHID_IMMED;
    455 		} else
    456 			sc->sc_state &= ~UHID_IMMED;
    457 		break;
    458 
    459 	case USB_GET_REPORT:
    460 		re = (struct usb_ctl_report *)addr;
    461 		switch (re->ucr_report) {
    462 		case UHID_INPUT_REPORT:
    463 			size = sc->sc_isize;
    464 			break;
    465 		case UHID_OUTPUT_REPORT:
    466 			size = sc->sc_osize;
    467 			break;
    468 		case UHID_FEATURE_REPORT:
    469 			size = sc->sc_fsize;
    470 			break;
    471 		default:
    472 			return (EINVAL);
    473 		}
    474 		extra = sc->sc_hdev.sc_report_id != 0;
    475 		err = uhidev_get_report(&sc->sc_hdev, re->ucr_report,
    476 		    re->ucr_data, size + extra);
    477 		if (extra)
    478 			memcpy(re->ucr_data, re->ucr_data+1, size);
    479 		if (err)
    480 			return (EIO);
    481 		break;
    482 
    483 	case USB_SET_REPORT:
    484 		re = (struct usb_ctl_report *)addr;
    485 		switch (re->ucr_report) {
    486 		case UHID_INPUT_REPORT:
    487 			size = sc->sc_isize;
    488 			break;
    489 		case UHID_OUTPUT_REPORT:
    490 			size = sc->sc_osize;
    491 			break;
    492 		case UHID_FEATURE_REPORT:
    493 			size = sc->sc_fsize;
    494 			break;
    495 		default:
    496 			return (EINVAL);
    497 		}
    498 		err = uhidev_set_report(&sc->sc_hdev, re->ucr_report,
    499 		    re->ucr_data, size);
    500 		if (err)
    501 			return (EIO);
    502 		break;
    503 
    504 	case USB_GET_REPORT_ID:
    505 		*(int *)addr = sc->sc_hdev.sc_report_id;
    506 		break;
    507 
    508 	default:
    509 		return (EINVAL);
    510 	}
    511 	return (0);
    512 }
    513 
    514 int
    515 uhidioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, usb_proc_ptr p)
    516 {
    517 	struct uhid_softc *sc;
    518 	int error;
    519 
    520 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    521 
    522 	sc->sc_refcnt++;
    523 	error = uhid_do_ioctl(sc, cmd, addr, flag, p);
    524 	if (--sc->sc_refcnt < 0)
    525 		usb_detach_wakeup(USBDEV(sc->sc_hdev.sc_dev));
    526 	return (error);
    527 }
    528 
    529 int
    530 uhidpoll(dev_t dev, int events, usb_proc_ptr p)
    531 {
    532 	struct uhid_softc *sc;
    533 	int revents = 0;
    534 	int s;
    535 
    536 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    537 
    538 	if (sc->sc_dying)
    539 		return (EIO);
    540 
    541 	s = splusb();
    542 	if (events & (POLLOUT | POLLWRNORM))
    543 		revents |= events & (POLLOUT | POLLWRNORM);
    544 	if (events & (POLLIN | POLLRDNORM)) {
    545 		if (sc->sc_q.c_cc > 0)
    546 			revents |= events & (POLLIN | POLLRDNORM);
    547 		else
    548 			selrecord(p, &sc->sc_rsel);
    549 	}
    550 
    551 	splx(s);
    552 	return (revents);
    553 }
    554