Home | History | Annotate | Line # | Download | only in usb
uhidev.c revision 1.19
      1 /*	$NetBSD: uhidev.c,v 1.19 2004/01/04 08:35:52 jdolecek Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 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/devclass_docs/HID1_11.pdf
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: uhidev.c,v 1.19 2004/01/04 08:35:52 jdolecek 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 
     56 #include <dev/usb/usb.h>
     57 #include <dev/usb/usbhid.h>
     58 
     59 #include <dev/usb/usbdevs.h>
     60 #include <dev/usb/usbdi.h>
     61 #include <dev/usb/usbdi_util.h>
     62 #include <dev/usb/hid.h>
     63 #include <dev/usb/usb_quirks.h>
     64 
     65 #include <dev/usb/uhidev.h>
     66 
     67 /* Report descriptor for broken Wacom Graphire */
     68 #include <dev/usb/ugraphire_rdesc.h>
     69 
     70 #ifdef UHIDEV_DEBUG
     71 #define DPRINTF(x)	if (uhidevdebug) logprintf x
     72 #define DPRINTFN(n,x)	if (uhidevdebug>(n)) logprintf x
     73 int	uhidevdebug = 0;
     74 #else
     75 #define DPRINTF(x)
     76 #define DPRINTFN(n,x)
     77 #endif
     78 
     79 Static void uhidev_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
     80 
     81 Static int uhidev_maxrepid(void *buf, int len);
     82 Static int uhidevprint(void *aux, const char *pnp);
     83 Static int uhidevsubmatch(struct device *parent, struct cfdata *cf, void *aux);
     84 
     85 USB_DECLARE_DRIVER(uhidev);
     86 
     87 USB_MATCH(uhidev)
     88 {
     89 	USB_MATCH_START(uhidev, uaa);
     90 	usb_interface_descriptor_t *id;
     91 
     92 	if (uaa->iface == NULL)
     93 		return (UMATCH_NONE);
     94 	id = usbd_get_interface_descriptor(uaa->iface);
     95 	if (id == NULL || id->bInterfaceClass != UICLASS_HID)
     96 		return (UMATCH_NONE);
     97 	if (uaa->matchlvl)
     98 		return (uaa->matchlvl);
     99 	return (UMATCH_IFACECLASS_GENERIC);
    100 }
    101 
    102 USB_ATTACH(uhidev)
    103 {
    104 	USB_ATTACH_START(uhidev, sc, uaa);
    105 	usbd_interface_handle iface = uaa->iface;
    106 	usb_interface_descriptor_t *id;
    107 	usb_endpoint_descriptor_t *ed;
    108 	struct uhidev_attach_arg uha;
    109 	struct uhidev *dev;
    110 	int size, nrepid, repid, repsz;
    111 	int repsizes[256];
    112 	void *desc;
    113 	const void *descptr = NULL;
    114 	usbd_status err;
    115 	char devinfo[1024];
    116 
    117 	sc->sc_udev = uaa->device;
    118 	sc->sc_iface = iface;
    119 	id = usbd_get_interface_descriptor(iface);
    120 	usbd_devinfo(uaa->device, 0, devinfo);
    121 	USB_ATTACH_SETUP;
    122 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    123 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
    124 
    125 	(void)usbd_set_idle(iface, 0, 0);
    126 #if 0
    127 
    128 	qflags = usbd_get_quirks(sc->sc_udev)->uq_flags;
    129 	if ((qflags & UQ_NO_SET_PROTO) == 0 &&
    130 	    id->bInterfaceSubClass != UISUBCLASS_BOOT)
    131 		(void)usbd_set_protocol(iface, 1);
    132 #endif
    133 
    134 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    135 	if (ed == NULL) {
    136 		printf("%s: could not read endpoint descriptor\n",
    137 		       USBDEVNAME(sc->sc_dev));
    138 		sc->sc_dying = 1;
    139 		USB_ATTACH_ERROR_RETURN;
    140 	}
    141 
    142 	DPRINTFN(10,("uhidev_attach: bLength=%d bDescriptorType=%d "
    143 		     "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
    144 		     " bInterval=%d\n",
    145 		     ed->bLength, ed->bDescriptorType,
    146 		     ed->bEndpointAddress & UE_ADDR,
    147 		     UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
    148 		     ed->bmAttributes & UE_XFERTYPE,
    149 		     UGETW(ed->wMaxPacketSize), ed->bInterval));
    150 
    151 	if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
    152 	    (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    153 		printf("%s: unexpected endpoint\n", USBDEVNAME(sc->sc_dev));
    154 		sc->sc_dying = 1;
    155 		USB_ATTACH_ERROR_RETURN;
    156 	}
    157 
    158 	sc->sc_ep_addr = ed->bEndpointAddress;
    159 
    160 	/* XXX need to extend this */
    161 	if (uaa->vendor == USB_VENDOR_WACOM /* &&
    162 	    uaa->revision == 0x???? */) { /* XXX should use revision */
    163 		char reportbuf[] = {2, 2, 2};
    164 
    165 		/* The report descriptor for the Wacom Graphire is broken. */
    166 		switch (uaa->product) {
    167 		case USB_PRODUCT_WACOM_GRAPHIRE:
    168 			size = sizeof uhid_graphire_report_descr;
    169 			descptr = uhid_graphire_report_descr;
    170 			break;
    171 
    172 		case USB_PRODUCT_WACOM_GRAPHIRE3_4X5: /* The 6x8 too? */
    173 			/*
    174 			 * The Graphire3 needs 0x0202 to be written to
    175 			 * feature report ID 2 before it'll start
    176 			 * returning digitizer data.
    177 			 */
    178 			usbd_set_report(uaa->iface, UHID_FEATURE_REPORT, 2,
    179 			    &reportbuf, sizeof(reportbuf));
    180 
    181 			size = sizeof uhid_graphire3_4x5_report_descr;
    182 			descptr = uhid_graphire3_4x5_report_descr;
    183 			break;
    184 		}
    185 
    186 	}
    187 
    188 	if (descptr) {
    189 		desc = malloc(size, M_USBDEV, M_NOWAIT);
    190 		if (desc == NULL)
    191 			err = USBD_NOMEM;
    192 		else {
    193 			err = USBD_NORMAL_COMPLETION;
    194 			memcpy(desc, descptr, size);
    195 		}
    196 	} else {
    197 		desc = NULL;
    198 		err = usbd_read_report_desc(uaa->iface, &desc, &size,
    199 		    M_USBDEV);
    200 	}
    201 	if (err) {
    202 		printf("%s: no report descriptor\n", USBDEVNAME(sc->sc_dev));
    203 		sc->sc_dying = 1;
    204 		USB_ATTACH_ERROR_RETURN;
    205 	}
    206 
    207 	sc->sc_repdesc = desc;
    208 	sc->sc_repdesc_size = size;
    209 
    210 	uha.uaa = uaa;
    211 	nrepid = uhidev_maxrepid(desc, size);
    212 	if (nrepid < 0)
    213 		USB_ATTACH_SUCCESS_RETURN;
    214 	if (nrepid > 0)
    215 		printf("%s: %d report ids\n", USBDEVNAME(sc->sc_dev), nrepid);
    216 	nrepid++;
    217 	sc->sc_subdevs = malloc(nrepid * sizeof(device_ptr_t),
    218 				M_USBDEV, M_NOWAIT | M_ZERO);
    219 	if (sc->sc_subdevs == NULL) {
    220 		printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
    221 		USB_ATTACH_ERROR_RETURN;
    222 	}
    223 	sc->sc_nrepid = nrepid;
    224 	sc->sc_isize = 0;
    225 
    226 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    227 			   USBDEV(sc->sc_dev));
    228 
    229 	for (repid = 0; repid < nrepid; repid++) {
    230 		repsz = hid_report_size(desc, size, hid_input, repid);
    231 		DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz));
    232 		repsizes[repid] = repsz;
    233 		if (repsz > 0) {
    234 			if (repsz > sc->sc_isize)
    235 				sc->sc_isize = repsz;
    236 		}
    237 	}
    238 	sc->sc_isize += nrepid != 1;	/* space for report ID */
    239 	DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize));
    240 
    241 	uha.parent = sc;
    242 	for (repid = 0; repid < nrepid; repid++) {
    243 		DPRINTF(("uhidev_match: try repid=%d\n", repid));
    244 		if (hid_report_size(desc, size, hid_input, repid) == 0 &&
    245 		    hid_report_size(desc, size, hid_output, repid) == 0 &&
    246 		    hid_report_size(desc, size, hid_feature, repid) == 0) {
    247 			;	/* already NULL in sc->sc_subdevs[repid] */
    248 		} else {
    249 			uha.reportid = repid;
    250 			dev = (struct uhidev *)config_found_sm(self, &uha,
    251 			                           uhidevprint, uhidevsubmatch);
    252 			sc->sc_subdevs[repid] = dev;
    253 			if (dev != NULL) {
    254 				dev->sc_in_rep_size = repsizes[repid];
    255 #ifdef DIAGNOSTIC
    256 				DPRINTF(("uhidev_match: repid=%d dev=%p\n",
    257 					 repid, dev));
    258 				if (dev->sc_intr == NULL) {
    259 					printf("%s: sc_intr == NULL\n",
    260 					       USBDEVNAME(sc->sc_dev));
    261 					USB_ATTACH_ERROR_RETURN;
    262 				}
    263 #endif
    264 #if NRND > 0
    265 				rnd_attach_source(&dev->rnd_source,
    266 						  USBDEVNAME(dev->sc_dev),
    267 						  RND_TYPE_TTY, 0);
    268 #endif
    269 			}
    270 		}
    271 	}
    272 
    273 	USB_ATTACH_SUCCESS_RETURN;
    274 }
    275 
    276 int
    277 uhidev_maxrepid(void *buf, int len)
    278 {
    279 	struct hid_data *d;
    280 	struct hid_item h;
    281 	int maxid;
    282 
    283 	maxid = -1;
    284 	h.report_ID = 0;
    285 	for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
    286 		if (h.report_ID > maxid)
    287 			maxid = h.report_ID;
    288 	hid_end_parse(d);
    289 	return (maxid);
    290 }
    291 
    292 int
    293 uhidevprint(void *aux, const char *pnp)
    294 {
    295 	struct uhidev_attach_arg *uha = aux;
    296 
    297 	if (pnp)
    298 		aprint_normal("uhid at %s", pnp);
    299 	if (uha->reportid != 0)
    300 		aprint_normal(" reportid %d", uha->reportid);
    301 	return (UNCONF);
    302 }
    303 
    304 int
    305 uhidevsubmatch(struct device *parent, struct cfdata *cf, void *aux)
    306 {
    307 	struct uhidev_attach_arg *uha = aux;
    308 
    309 	if (cf->uhidevcf_reportid != UHIDEV_UNK_REPORTID &&
    310 	    cf->uhidevcf_reportid != uha->reportid)
    311 		return (0);
    312 	if (cf->uhidevcf_reportid == uha->reportid)
    313 		uha->matchlvl = UMATCH_VENDOR_PRODUCT;
    314 	else
    315 		uha->matchlvl = 0;
    316 	return (config_match(parent, cf, aux));
    317 }
    318 
    319 int
    320 uhidev_activate(device_ptr_t self, enum devact act)
    321 {
    322 	struct uhidev_softc *sc = (struct uhidev_softc *)self;
    323 	int i, rv;
    324 
    325 	switch (act) {
    326 	case DVACT_ACTIVATE:
    327 		return (EOPNOTSUPP);
    328 
    329 	case DVACT_DEACTIVATE:
    330 		rv = 0;
    331 		for (i = 0; i < sc->sc_nrepid; i++)
    332 			if (sc->sc_subdevs[i] != NULL)
    333 				rv |= config_deactivate(
    334 					&sc->sc_subdevs[i]->sc_dev);
    335 		sc->sc_dying = 1;
    336 		break;
    337 	default:
    338 		rv = 0;
    339 		break;
    340 	}
    341 	return (rv);
    342 }
    343 
    344 USB_DETACH(uhidev)
    345 {
    346 	USB_DETACH_START(uhidev, sc);
    347 	int i, rv;
    348 
    349 	DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
    350 
    351 	sc->sc_dying = 1;
    352 	if (sc->sc_intrpipe != NULL)
    353 		usbd_abort_pipe(sc->sc_intrpipe);
    354 
    355 	if (sc->sc_repdesc != NULL)
    356 		free(sc->sc_repdesc, M_USBDEV);
    357 
    358 	rv = 0;
    359 	for (i = 0; i < sc->sc_nrepid; i++) {
    360 		if (sc->sc_subdevs[i] != NULL) {
    361 #if NRND > 0
    362 			rnd_detach_source(&sc->sc_subdevs[i]->rnd_source);
    363 #endif
    364 			rv |= config_detach(&sc->sc_subdevs[i]->sc_dev, flags);
    365 			sc->sc_subdevs[i] = NULL;
    366 		}
    367 	}
    368 
    369 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    370 			   USBDEV(sc->sc_dev));
    371 
    372 	return (rv);
    373 }
    374 
    375 void
    376 uhidev_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
    377 {
    378 	struct uhidev_softc *sc = addr;
    379 	struct uhidev *scd;
    380 	u_char *p;
    381 	u_int rep;
    382 	u_int32_t cc;
    383 
    384 	usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
    385 
    386 #ifdef UHIDEV_DEBUG
    387 	if (uhidevdebug > 5) {
    388 		u_int32_t i;
    389 
    390 		DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
    391 		DPRINTF(("uhidev_intr: data ="));
    392 		for (i = 0; i < cc; i++)
    393 			DPRINTF((" %02x", sc->sc_ibuf[i]));
    394 		DPRINTF(("\n"));
    395 	}
    396 #endif
    397 
    398 	if (status == USBD_CANCELLED)
    399 		return;
    400 
    401 	if (status != USBD_NORMAL_COMPLETION) {
    402 		DPRINTF(("%s: interrupt status=%d\n", USBDEVNAME(sc->sc_dev),
    403 			 status));
    404 		usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
    405 		return;
    406 	}
    407 
    408 	p = sc->sc_ibuf;
    409 	if (sc->sc_nrepid != 1)
    410 		rep = *p++, cc--;
    411 	else
    412 		rep = 0;
    413 	if (rep >= sc->sc_nrepid) {
    414 		printf("uhidev_intr: bad repid %d\n", rep);
    415 		return;
    416 	}
    417 	scd = sc->sc_subdevs[rep];
    418 	DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n",
    419 		    rep, scd, scd ? scd->sc_state : 0));
    420 	if (scd == NULL || !(scd->sc_state & UHIDEV_OPEN))
    421 		return;
    422 #ifdef DIAGNOSTIC
    423 	if (scd->sc_in_rep_size != cc)
    424 		printf("%s: bad input length %d != %d\n",USBDEVNAME(sc->sc_dev),
    425 		       scd->sc_in_rep_size, cc);
    426 #endif
    427 #if NRND > 0
    428 	rnd_add_uint32(&scd->rnd_source, (uintptr_t)(sc->sc_ibuf));
    429 #endif
    430 	scd->sc_intr(scd, p, cc);
    431 }
    432 
    433 void
    434 uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size)
    435 {
    436 	*desc = sc->sc_repdesc;
    437 	*size = sc->sc_repdesc_size;
    438 }
    439 
    440 int
    441 uhidev_open(struct uhidev *scd)
    442 {
    443 	struct uhidev_softc *sc = scd->sc_parent;
    444 	usbd_status err;
    445 
    446 	DPRINTF(("uhidev_open: open pipe, state=%d refcnt=%d\n",
    447 		 scd->sc_state, sc->sc_refcnt));
    448 
    449 	if (scd->sc_state & UHIDEV_OPEN)
    450 		return (EBUSY);
    451 	scd->sc_state |= UHIDEV_OPEN;
    452 	if (sc->sc_refcnt++)
    453 		return (0);
    454 
    455 	if (sc->sc_isize == 0)
    456 		return (0);
    457 
    458 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    459 
    460 	/* Set up interrupt pipe. */
    461 	DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize,
    462 		 sc->sc_ep_addr));
    463 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
    464 		  USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc, sc->sc_ibuf,
    465 		  sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL);
    466 	if (err) {
    467 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
    468 			 "error=%d\n",err));
    469 		free(sc->sc_ibuf, M_USBDEV);
    470 		scd->sc_state &= ~UHIDEV_OPEN;
    471 		sc->sc_refcnt = 0;
    472 		sc->sc_intrpipe = NULL;
    473 		return (EIO);
    474 	}
    475 	return (0);
    476 }
    477 
    478 void
    479 uhidev_close(struct uhidev *scd)
    480 {
    481 	struct uhidev_softc *sc = scd->sc_parent;
    482 
    483 	if (!(scd->sc_state & UHIDEV_OPEN))
    484 		return;
    485 	scd->sc_state &= ~UHIDEV_OPEN;
    486 	if (--sc->sc_refcnt)
    487 		return;
    488 	DPRINTF(("uhidev_close: close pipe\n"));
    489 
    490 	/* Disable interrupts. */
    491 	if (sc->sc_intrpipe != NULL) {
    492 		usbd_abort_pipe(sc->sc_intrpipe);
    493 		usbd_close_pipe(sc->sc_intrpipe);
    494 		sc->sc_intrpipe = NULL;
    495 	}
    496 
    497 	if (sc->sc_ibuf != NULL) {
    498 		free(sc->sc_ibuf, M_USBDEV);
    499 		sc->sc_ibuf = NULL;
    500 	}
    501 }
    502 
    503 usbd_status
    504 uhidev_set_report(struct uhidev *scd, int type, void *data, int len)
    505 {
    506 	char *buf;
    507 	usbd_status retstat;
    508 
    509 	if (scd->sc_report_id == 0)
    510 		return usbd_set_report(scd->sc_parent->sc_iface, type,
    511 				       scd->sc_report_id, data, len);
    512 
    513 	buf = malloc(len + 1, M_TEMP, M_WAITOK);
    514 	buf[0] = scd->sc_report_id;
    515 	memcpy(buf+1, data, len);
    516 
    517 	retstat = usbd_set_report(scd->sc_parent->sc_iface, type,
    518 				  scd->sc_report_id, buf, len + 1);
    519 
    520 	free(buf, M_TEMP);
    521 
    522 	return retstat;
    523 }
    524 
    525 void
    526 uhidev_set_report_async(struct uhidev *scd, int type, void *data, int len)
    527 {
    528 	/* XXX */
    529 	char buf[100];
    530 	if (scd->sc_report_id) {
    531 		buf[0] = scd->sc_report_id;
    532 		memcpy(buf+1, data, len);
    533 		len++;
    534 		data = buf;
    535 	}
    536 
    537 	usbd_set_report_async(scd->sc_parent->sc_iface, type,
    538 			      scd->sc_report_id, data, len);
    539 }
    540 
    541 usbd_status
    542 uhidev_get_report(struct uhidev *scd, int type, void *data, int len)
    543 {
    544 	return usbd_get_report(scd->sc_parent->sc_iface, type,
    545 			       scd->sc_report_id, data, len);
    546 }
    547