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