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