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