Home | History | Annotate | Line # | Download | only in usb
uhidev.c revision 1.21
      1 /*	$NetBSD: uhidev.c,v 1.21 2004/04/23 17:25:25 itojun 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.21 2004/04/23 17:25:25 itojun 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;
    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, sizeof(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 	descptr = NULL;
    162 	if (uaa->vendor == USB_VENDOR_WACOM) {
    163 		static uByte 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 		default:
    185 			/* Keep descriptor */
    186 			break;
    187 		}
    188 	}
    189 
    190 	if (descptr) {
    191 		desc = malloc(size, M_USBDEV, M_NOWAIT);
    192 		if (desc == NULL)
    193 			err = USBD_NOMEM;
    194 		else {
    195 			err = USBD_NORMAL_COMPLETION;
    196 			memcpy(desc, descptr, size);
    197 		}
    198 	} else {
    199 		desc = NULL;
    200 		err = usbd_read_report_desc(uaa->iface, &desc, &size,
    201 		    M_USBDEV);
    202 	}
    203 	if (err) {
    204 		printf("%s: no report descriptor\n", USBDEVNAME(sc->sc_dev));
    205 		sc->sc_dying = 1;
    206 		USB_ATTACH_ERROR_RETURN;
    207 	}
    208 
    209 	sc->sc_repdesc = desc;
    210 	sc->sc_repdesc_size = size;
    211 
    212 	uha.uaa = uaa;
    213 	nrepid = uhidev_maxrepid(desc, size);
    214 	if (nrepid < 0)
    215 		USB_ATTACH_SUCCESS_RETURN;
    216 	if (nrepid > 0)
    217 		printf("%s: %d report ids\n", USBDEVNAME(sc->sc_dev), nrepid);
    218 	nrepid++;
    219 	sc->sc_subdevs = malloc(nrepid * sizeof(device_ptr_t),
    220 				M_USBDEV, M_NOWAIT | M_ZERO);
    221 	if (sc->sc_subdevs == NULL) {
    222 		printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
    223 		USB_ATTACH_ERROR_RETURN;
    224 	}
    225 	sc->sc_nrepid = nrepid;
    226 	sc->sc_isize = 0;
    227 
    228 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    229 			   USBDEV(sc->sc_dev));
    230 
    231 	for (repid = 0; repid < nrepid; repid++) {
    232 		repsz = hid_report_size(desc, size, hid_input, repid);
    233 		DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz));
    234 		repsizes[repid] = repsz;
    235 		if (repsz > 0) {
    236 			if (repsz > sc->sc_isize)
    237 				sc->sc_isize = repsz;
    238 		}
    239 	}
    240 	sc->sc_isize += nrepid != 1;	/* space for report ID */
    241 	DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize));
    242 
    243 	uha.parent = sc;
    244 	for (repid = 0; repid < nrepid; repid++) {
    245 		DPRINTF(("uhidev_match: try repid=%d\n", repid));
    246 		if (hid_report_size(desc, size, hid_input, repid) == 0 &&
    247 		    hid_report_size(desc, size, hid_output, repid) == 0 &&
    248 		    hid_report_size(desc, size, hid_feature, repid) == 0) {
    249 			;	/* already NULL in sc->sc_subdevs[repid] */
    250 		} else {
    251 			uha.reportid = repid;
    252 			dev = (struct uhidev *)config_found_sm(self, &uha,
    253 			                           uhidevprint, uhidevsubmatch);
    254 			sc->sc_subdevs[repid] = dev;
    255 			if (dev != NULL) {
    256 				dev->sc_in_rep_size = repsizes[repid];
    257 #ifdef DIAGNOSTIC
    258 				DPRINTF(("uhidev_match: repid=%d dev=%p\n",
    259 					 repid, dev));
    260 				if (dev->sc_intr == NULL) {
    261 					printf("%s: sc_intr == NULL\n",
    262 					       USBDEVNAME(sc->sc_dev));
    263 					USB_ATTACH_ERROR_RETURN;
    264 				}
    265 #endif
    266 #if NRND > 0
    267 				rnd_attach_source(&dev->rnd_source,
    268 						  USBDEVNAME(dev->sc_dev),
    269 						  RND_TYPE_TTY, 0);
    270 #endif
    271 			}
    272 		}
    273 	}
    274 
    275 	USB_ATTACH_SUCCESS_RETURN;
    276 }
    277 
    278 int
    279 uhidev_maxrepid(void *buf, int len)
    280 {
    281 	struct hid_data *d;
    282 	struct hid_item h;
    283 	int maxid;
    284 
    285 	maxid = -1;
    286 	h.report_ID = 0;
    287 	for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
    288 		if (h.report_ID > maxid)
    289 			maxid = h.report_ID;
    290 	hid_end_parse(d);
    291 	return (maxid);
    292 }
    293 
    294 int
    295 uhidevprint(void *aux, const char *pnp)
    296 {
    297 	struct uhidev_attach_arg *uha = aux;
    298 
    299 	if (pnp)
    300 		aprint_normal("uhid at %s", pnp);
    301 	if (uha->reportid != 0)
    302 		aprint_normal(" reportid %d", uha->reportid);
    303 	return (UNCONF);
    304 }
    305 
    306 int
    307 uhidevsubmatch(struct device *parent, struct cfdata *cf, void *aux)
    308 {
    309 	struct uhidev_attach_arg *uha = aux;
    310 
    311 	if (cf->uhidevcf_reportid != UHIDEV_UNK_REPORTID &&
    312 	    cf->uhidevcf_reportid != uha->reportid)
    313 		return (0);
    314 	if (cf->uhidevcf_reportid == uha->reportid)
    315 		uha->matchlvl = UMATCH_VENDOR_PRODUCT;
    316 	else
    317 		uha->matchlvl = 0;
    318 	return (config_match(parent, cf, aux));
    319 }
    320 
    321 int
    322 uhidev_activate(device_ptr_t self, enum devact act)
    323 {
    324 	struct uhidev_softc *sc = (struct uhidev_softc *)self;
    325 	int i, rv;
    326 
    327 	switch (act) {
    328 	case DVACT_ACTIVATE:
    329 		return (EOPNOTSUPP);
    330 
    331 	case DVACT_DEACTIVATE:
    332 		rv = 0;
    333 		for (i = 0; i < sc->sc_nrepid; i++)
    334 			if (sc->sc_subdevs[i] != NULL)
    335 				rv |= config_deactivate(
    336 					&sc->sc_subdevs[i]->sc_dev);
    337 		sc->sc_dying = 1;
    338 		break;
    339 	default:
    340 		rv = 0;
    341 		break;
    342 	}
    343 	return (rv);
    344 }
    345 
    346 USB_DETACH(uhidev)
    347 {
    348 	USB_DETACH_START(uhidev, sc);
    349 	int i, rv;
    350 
    351 	DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
    352 
    353 	sc->sc_dying = 1;
    354 	if (sc->sc_intrpipe != NULL)
    355 		usbd_abort_pipe(sc->sc_intrpipe);
    356 
    357 	if (sc->sc_repdesc != NULL)
    358 		free(sc->sc_repdesc, M_USBDEV);
    359 
    360 	rv = 0;
    361 	for (i = 0; i < sc->sc_nrepid; i++) {
    362 		if (sc->sc_subdevs[i] != NULL) {
    363 #if NRND > 0
    364 			rnd_detach_source(&sc->sc_subdevs[i]->rnd_source);
    365 #endif
    366 			rv |= config_detach(&sc->sc_subdevs[i]->sc_dev, flags);
    367 			sc->sc_subdevs[i] = NULL;
    368 		}
    369 	}
    370 
    371 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    372 			   USBDEV(sc->sc_dev));
    373 
    374 	return (rv);
    375 }
    376 
    377 void
    378 uhidev_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
    379 {
    380 	struct uhidev_softc *sc = addr;
    381 	struct uhidev *scd;
    382 	u_char *p;
    383 	u_int rep;
    384 	u_int32_t cc;
    385 
    386 	usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
    387 
    388 #ifdef UHIDEV_DEBUG
    389 	if (uhidevdebug > 5) {
    390 		u_int32_t i;
    391 
    392 		DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
    393 		DPRINTF(("uhidev_intr: data ="));
    394 		for (i = 0; i < cc; i++)
    395 			DPRINTF((" %02x", sc->sc_ibuf[i]));
    396 		DPRINTF(("\n"));
    397 	}
    398 #endif
    399 
    400 	if (status == USBD_CANCELLED)
    401 		return;
    402 
    403 	if (status != USBD_NORMAL_COMPLETION) {
    404 		DPRINTF(("%s: interrupt status=%d\n", USBDEVNAME(sc->sc_dev),
    405 			 status));
    406 		usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
    407 		return;
    408 	}
    409 
    410 	p = sc->sc_ibuf;
    411 	if (sc->sc_nrepid != 1)
    412 		rep = *p++, cc--;
    413 	else
    414 		rep = 0;
    415 	if (rep >= sc->sc_nrepid) {
    416 		printf("uhidev_intr: bad repid %d\n", rep);
    417 		return;
    418 	}
    419 	scd = sc->sc_subdevs[rep];
    420 	DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n",
    421 		    rep, scd, scd ? scd->sc_state : 0));
    422 	if (scd == NULL || !(scd->sc_state & UHIDEV_OPEN))
    423 		return;
    424 #ifdef DIAGNOSTIC
    425 	if (scd->sc_in_rep_size != cc)
    426 		printf("%s: bad input length %d != %d\n",USBDEVNAME(sc->sc_dev),
    427 		       scd->sc_in_rep_size, cc);
    428 #endif
    429 #if NRND > 0
    430 	rnd_add_uint32(&scd->rnd_source, (uintptr_t)(sc->sc_ibuf));
    431 #endif
    432 	scd->sc_intr(scd, p, cc);
    433 }
    434 
    435 void
    436 uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size)
    437 {
    438 	*desc = sc->sc_repdesc;
    439 	*size = sc->sc_repdesc_size;
    440 }
    441 
    442 int
    443 uhidev_open(struct uhidev *scd)
    444 {
    445 	struct uhidev_softc *sc = scd->sc_parent;
    446 	usbd_status err;
    447 
    448 	DPRINTF(("uhidev_open: open pipe, state=%d refcnt=%d\n",
    449 		 scd->sc_state, sc->sc_refcnt));
    450 
    451 	if (scd->sc_state & UHIDEV_OPEN)
    452 		return (EBUSY);
    453 	scd->sc_state |= UHIDEV_OPEN;
    454 	if (sc->sc_refcnt++)
    455 		return (0);
    456 
    457 	if (sc->sc_isize == 0)
    458 		return (0);
    459 
    460 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    461 
    462 	/* Set up interrupt pipe. */
    463 	DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize,
    464 		 sc->sc_ep_addr));
    465 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
    466 		  USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc, sc->sc_ibuf,
    467 		  sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL);
    468 	if (err) {
    469 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
    470 			 "error=%d\n",err));
    471 		free(sc->sc_ibuf, M_USBDEV);
    472 		scd->sc_state &= ~UHIDEV_OPEN;
    473 		sc->sc_refcnt = 0;
    474 		sc->sc_intrpipe = NULL;
    475 		return (EIO);
    476 	}
    477 	return (0);
    478 }
    479 
    480 void
    481 uhidev_close(struct uhidev *scd)
    482 {
    483 	struct uhidev_softc *sc = scd->sc_parent;
    484 
    485 	if (!(scd->sc_state & UHIDEV_OPEN))
    486 		return;
    487 	scd->sc_state &= ~UHIDEV_OPEN;
    488 	if (--sc->sc_refcnt)
    489 		return;
    490 	DPRINTF(("uhidev_close: close pipe\n"));
    491 
    492 	/* Disable interrupts. */
    493 	if (sc->sc_intrpipe != NULL) {
    494 		usbd_abort_pipe(sc->sc_intrpipe);
    495 		usbd_close_pipe(sc->sc_intrpipe);
    496 		sc->sc_intrpipe = NULL;
    497 	}
    498 
    499 	if (sc->sc_ibuf != NULL) {
    500 		free(sc->sc_ibuf, M_USBDEV);
    501 		sc->sc_ibuf = NULL;
    502 	}
    503 }
    504 
    505 usbd_status
    506 uhidev_set_report(struct uhidev *scd, int type, void *data, int len)
    507 {
    508 	char *buf;
    509 	usbd_status retstat;
    510 
    511 	if (scd->sc_report_id == 0)
    512 		return usbd_set_report(scd->sc_parent->sc_iface, type,
    513 				       scd->sc_report_id, data, len);
    514 
    515 	buf = malloc(len + 1, M_TEMP, M_WAITOK);
    516 	buf[0] = scd->sc_report_id;
    517 	memcpy(buf+1, data, len);
    518 
    519 	retstat = usbd_set_report(scd->sc_parent->sc_iface, type,
    520 				  scd->sc_report_id, buf, len + 1);
    521 
    522 	free(buf, M_TEMP);
    523 
    524 	return retstat;
    525 }
    526 
    527 void
    528 uhidev_set_report_async(struct uhidev *scd, int type, void *data, int len)
    529 {
    530 	/* XXX */
    531 	char buf[100];
    532 	if (scd->sc_report_id) {
    533 		buf[0] = scd->sc_report_id;
    534 		memcpy(buf+1, data, len);
    535 		len++;
    536 		data = buf;
    537 	}
    538 
    539 	usbd_set_report_async(scd->sc_parent->sc_iface, type,
    540 			      scd->sc_report_id, data, len);
    541 }
    542 
    543 usbd_status
    544 uhidev_get_report(struct uhidev *scd, int type, void *data, int len)
    545 {
    546 	return usbd_get_report(scd->sc_parent->sc_iface, type,
    547 			       scd->sc_report_id, data, len);
    548 }
    549