xref: /openbsd-src/sys/dev/usb/uhidev.c (revision 07aaebcc98a187b2d036aa04587ca3cdb2f94ff2)
1 /*	$OpenBSD: uhidev.c,v 1.16 2006/06/23 06:27:11 miod Exp $	*/
2 /*	$NetBSD: uhidev.c,v 1.14 2003/03/11 16:44:00 augustss Exp $	*/
3 
4 /*
5  * Copyright (c) 2001 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) at
10  * Carlstedt Research & Technology.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *        This product includes software developed by the NetBSD
23  *        Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 /*
42  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
43  */
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/signalvar.h>
50 #include <sys/device.h>
51 #include <sys/ioctl.h>
52 #include <sys/conf.h>
53 
54 #include <dev/usb/usb.h>
55 #include <dev/usb/usbhid.h>
56 
57 #include <dev/usb/usbdevs.h>
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdi_util.h>
60 #include <dev/usb/hid.h>
61 #include <dev/usb/usb_quirks.h>
62 
63 #include <dev/usb/uhidev.h>
64 
65 /* Report descriptor for broken Wacom Graphire */
66 #include <dev/usb/ugraphire_rdesc.h>
67 
68 #ifdef UHIDEV_DEBUG
69 #define DPRINTF(x)	do { if (uhidevdebug) logprintf x; } while (0)
70 #define DPRINTFN(n,x)	do { if (uhidevdebug>(n)) logprintf x; } while (0)
71 int	uhidevdebug = 0;
72 #else
73 #define DPRINTF(x)
74 #define DPRINTFN(n,x)
75 #endif
76 
77 Static void uhidev_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
78 
79 Static int uhidev_maxrepid(void *buf, int len);
80 Static int uhidevprint(void *aux, const char *pnp);
81 #if defined(__NetBSD__)
82 Static int uhidevsubmatch(struct device *parent, struct cfdata *cf, void *aux);
83 #else
84 Static int uhidevsubmatch(struct device *parent, void *cf, void *aux);
85 #endif
86 
87 USB_DECLARE_DRIVER(uhidev);
88 
89 USB_MATCH(uhidev)
90 {
91 	USB_MATCH_START(uhidev, uaa);
92 	usb_interface_descriptor_t *id;
93 
94 	if (uaa->iface == NULL)
95 		return (UMATCH_NONE);
96 	id = usbd_get_interface_descriptor(uaa->iface);
97 	if (id == NULL || id->bInterfaceClass != UICLASS_HID)
98 		return (UMATCH_NONE);
99 	if (usbd_get_quirks(uaa->device)->uq_flags & UQ_BAD_HID)
100 		return (UMATCH_NONE);
101 	if (uaa->matchlvl)
102 		return (uaa->matchlvl);
103 
104 #ifdef __macppc__
105 	/*
106 	 * Some Apple laptops have USB phantom devices which match
107 	 * the ADB devices.  We want to ignore them to avoid
108 	 * confusing users, as the real hardware underneath is adb
109 	 * and has already attached.
110 	 */
111 	if (uaa->vendor == USB_VENDOR_APPLE &&
112 	    uaa->product == USB_PRODUCT_APPLE_ADB)
113 		return (UMATCH_NONE);
114 #endif
115 
116 	return (UMATCH_IFACECLASS_GENERIC);
117 }
118 
119 USB_ATTACH(uhidev)
120 {
121 	USB_ATTACH_START(uhidev, sc, uaa);
122 	usbd_interface_handle iface = uaa->iface;
123 	usb_interface_descriptor_t *id;
124 	usb_endpoint_descriptor_t *ed;
125 	struct uhidev_attach_arg uha;
126 	struct uhidev *dev;
127 	int size, nrepid, repid, repsz;
128 	int repsizes[256];
129 	void *desc;
130 	const void *descptr;
131 	usbd_status err;
132 	char *devinfop;
133 
134 	sc->sc_udev = uaa->device;
135 	sc->sc_iface = iface;
136 	id = usbd_get_interface_descriptor(iface);
137 
138 	devinfop = usbd_devinfo_alloc(uaa->device, 0);
139 	USB_ATTACH_SETUP;
140 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
141 	       devinfop, id->bInterfaceClass, id->bInterfaceSubClass);
142 	usbd_devinfo_free(devinfop);
143 
144 	(void)usbd_set_idle(iface, 0, 0);
145 #if 0
146 
147 	qflags = usbd_get_quirks(sc->sc_udev)->uq_flags;
148 	if ((qflags & UQ_NO_SET_PROTO) == 0 &&
149 	    id->bInterfaceSubClass != UISUBCLASS_BOOT)
150 		(void)usbd_set_protocol(iface, 1);
151 #endif
152 
153 	ed = usbd_interface2endpoint_descriptor(iface, 0);
154 	if (ed == NULL) {
155 		printf("%s: could not read endpoint descriptor\n",
156 		       USBDEVNAME(sc->sc_dev));
157 		sc->sc_dying = 1;
158 		USB_ATTACH_ERROR_RETURN;
159 	}
160 
161 	DPRINTFN(10,("uhidev_attach: bLength=%d bDescriptorType=%d "
162 		     "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
163 		     " bInterval=%d\n",
164 		     ed->bLength, ed->bDescriptorType,
165 		     ed->bEndpointAddress & UE_ADDR,
166 		     UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
167 		     ed->bmAttributes & UE_XFERTYPE,
168 		     UGETW(ed->wMaxPacketSize), ed->bInterval));
169 
170 	if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
171 	    (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
172 		printf("%s: unexpected endpoint\n", USBDEVNAME(sc->sc_dev));
173 		sc->sc_dying = 1;
174 		USB_ATTACH_ERROR_RETURN;
175 	}
176 
177 	sc->sc_ep_addr = ed->bEndpointAddress;
178 
179 	/* XXX need to extend this */
180 	descptr = NULL;
181 	if (uaa->vendor == USB_VENDOR_WACOM) {
182 		static uByte reportbuf[] = {2, 2, 2};
183 
184 		/* The report descriptor for the Wacom Graphire is broken. */
185 		switch (uaa->product) {
186 		case USB_PRODUCT_WACOM_GRAPHIRE:
187 			size = sizeof uhid_graphire_report_descr;
188 			descptr = uhid_graphire_report_descr;
189 			break;
190 		case USB_PRODUCT_WACOM_GRAPHIRE3_4X5:
191 		case USB_PRODUCT_WACOM_GRAPHIRE4_4X5:
192 			usbd_set_report(uaa->iface, UHID_FEATURE_REPORT, 2,
193 			    &reportbuf, sizeof reportbuf);
194 			size = sizeof uhid_graphire3_4x5_report_descr;
195 			descptr = uhid_graphire3_4x5_report_descr;
196 			break;
197 		default:
198 			/* Keep descriptor */
199 			break;
200 		}
201 	}
202 
203 	if (descptr) {
204 		desc = malloc(size, M_USBDEV, M_NOWAIT);
205 		if (desc == NULL)
206 			err = USBD_NOMEM;
207 		else {
208 			err = USBD_NORMAL_COMPLETION;
209 			memcpy(desc, descptr, size);
210 		}
211 	} else {
212 		desc = NULL;
213 		err = usbd_read_report_desc(uaa->iface, &desc, &size, M_USBDEV);
214 	}
215 	if (err) {
216 		printf("%s: no report descriptor\n", USBDEVNAME(sc->sc_dev));
217 		sc->sc_dying = 1;
218 		USB_ATTACH_ERROR_RETURN;
219 	}
220 
221 	sc->sc_repdesc = desc;
222 	sc->sc_repdesc_size = size;
223 
224 	uha.uaa = uaa;
225 	nrepid = uhidev_maxrepid(desc, size);
226 	if (nrepid < 0)
227 		USB_ATTACH_SUCCESS_RETURN;
228 	if (nrepid > 0)
229 		printf("%s: %d report ids\n", USBDEVNAME(sc->sc_dev), nrepid);
230 	nrepid++;
231 	sc->sc_subdevs = malloc(nrepid * sizeof(device_ptr_t),
232 				M_USBDEV, M_NOWAIT);
233 	bzero(sc->sc_subdevs, nrepid * sizeof(device_ptr_t));
234 	if (sc->sc_subdevs == NULL) {
235 		printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
236 		USB_ATTACH_ERROR_RETURN;
237 	}
238 	sc->sc_nrepid = nrepid;
239 	sc->sc_isize = 0;
240 
241 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
242 			   USBDEV(sc->sc_dev));
243 
244 	for (repid = 0; repid < nrepid; repid++) {
245 		repsz = hid_report_size(desc, size, hid_input, repid);
246 		DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz));
247 		repsizes[repid] = repsz;
248 		if (repsz > 0) {
249 			if (repsz > sc->sc_isize)
250 				sc->sc_isize = repsz;
251 		}
252 	}
253 	sc->sc_isize += nrepid != 1;	/* space for report ID */
254 	DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize));
255 
256 	uha.parent = sc;
257 	for (repid = 0; repid < nrepid; repid++) {
258 		DPRINTF(("uhidev_match: try repid=%d\n", repid));
259 		if (hid_report_size(desc, size, hid_input, repid) == 0 &&
260 		    hid_report_size(desc, size, hid_output, repid) == 0 &&
261 		    hid_report_size(desc, size, hid_feature, repid) == 0) {
262 			;	/* already NULL in sc->sc_subdevs[repid] */
263 		} else {
264 			uha.reportid = repid;
265 			dev = (struct uhidev *)config_found_sm(self, &uha,
266 			                           uhidevprint, uhidevsubmatch);
267 			sc->sc_subdevs[repid] = dev;
268 			if (dev != NULL) {
269 				dev->sc_in_rep_size = repsizes[repid];
270 #ifdef DIAGNOSTIC
271 				DPRINTF(("uhidev_match: repid=%d dev=%p\n",
272 					 repid, dev));
273 				if (dev->sc_intr == NULL) {
274 					printf("%s: sc_intr == NULL\n",
275 					       USBDEVNAME(sc->sc_dev));
276 					USB_ATTACH_ERROR_RETURN;
277 				}
278 #endif
279 			}
280 		}
281 	}
282 
283 	USB_ATTACH_SUCCESS_RETURN;
284 }
285 
286 int
287 uhidev_maxrepid(void *buf, int len)
288 {
289 	struct hid_data *d;
290 	struct hid_item h;
291 	int maxid;
292 
293 	maxid = -1;
294 	h.report_ID = 0;
295 	for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
296 		if (h.report_ID > maxid)
297 			maxid = h.report_ID;
298 	hid_end_parse(d);
299 	return (maxid);
300 }
301 
302 int
303 uhidevprint(void *aux, const char *pnp)
304 {
305 	struct uhidev_attach_arg *uha = aux;
306 
307 	if (pnp)
308 		printf("uhid at %s", pnp);
309 	if (uha->reportid != 0)
310 		printf(" reportid %d", uha->reportid);
311 	return (UNCONF);
312 }
313 
314 #if defined(__NetBSD__)
315 Static int uhidevsubmatch(struct device *parent, struct cfdata *cf, void *aux)
316 #else
317 Static int uhidevsubmatch(struct device *parent, void *match, void *aux)
318 #endif
319 {
320 	struct uhidev_attach_arg *uha = aux;
321 #if defined(__OpenBSD__)
322         struct cfdata *cf = match;
323 #endif
324 
325 	if (cf->uhidevcf_reportid != UHIDEV_UNK_REPORTID &&
326 	    cf->uhidevcf_reportid != uha->reportid)
327 		return (0);
328 	if (cf->uhidevcf_reportid == uha->reportid)
329 		uha->matchlvl = UMATCH_VENDOR_PRODUCT;
330 	else
331 		uha->matchlvl = 0;
332 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
333 }
334 
335 int
336 uhidev_activate(device_ptr_t self, enum devact act)
337 {
338 	struct uhidev_softc *sc = (struct uhidev_softc *)self;
339 	int i, rv = 0;
340 
341 	switch (act) {
342 	case DVACT_ACTIVATE:
343 		break;
344 
345 	case DVACT_DEACTIVATE:
346 		for (i = 0; i < sc->sc_nrepid; i++)
347 			if (sc->sc_subdevs[i] != NULL)
348 				rv |= config_deactivate(
349 					&sc->sc_subdevs[i]->sc_dev);
350 		sc->sc_dying = 1;
351 		break;
352 	}
353 	return (rv);
354 }
355 
356 USB_DETACH(uhidev)
357 {
358 	USB_DETACH_START(uhidev, sc);
359 	int i, rv;
360 
361 	DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
362 
363 	sc->sc_dying = 1;
364 	if (sc->sc_intrpipe != NULL)
365 		usbd_abort_pipe(sc->sc_intrpipe);
366 
367 	if (sc->sc_repdesc != NULL)
368 		free(sc->sc_repdesc, M_USBDEV);
369 
370 	rv = 0;
371 	for (i = 0; i < sc->sc_nrepid; i++) {
372 		if (sc->sc_subdevs[i] != NULL) {
373 			rv |= config_detach(&sc->sc_subdevs[i]->sc_dev, flags);
374 			sc->sc_subdevs[i] = NULL;
375 		}
376 	}
377 
378 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
379 			   USBDEV(sc->sc_dev));
380 
381 	return (rv);
382 }
383 
384 void
385 uhidev_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
386 {
387 	struct uhidev_softc *sc = addr;
388 	struct uhidev *scd;
389 	u_char *p;
390 	u_int rep;
391 	u_int32_t cc;
392 
393 	usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
394 
395 #ifdef UHIDEV_DEBUG
396 	if (uhidevdebug > 5) {
397 		u_int32_t i;
398 
399 		DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
400 		DPRINTF(("uhidev_intr: data ="));
401 		for (i = 0; i < cc; i++)
402 			DPRINTF((" %02x", sc->sc_ibuf[i]));
403 		DPRINTF(("\n"));
404 	}
405 #endif
406 
407 	if (status == USBD_CANCELLED)
408 		return;
409 
410 	if (status != USBD_NORMAL_COMPLETION) {
411 		DPRINTF(("%s: interrupt status=%d\n", USBDEVNAME(sc->sc_dev),
412 			 status));
413 		usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
414 		return;
415 	}
416 
417 	p = sc->sc_ibuf;
418 	if (sc->sc_nrepid != 1)
419 		rep = *p++, cc--;
420 	else
421 		rep = 0;
422 	if (rep >= sc->sc_nrepid) {
423 		printf("uhidev_intr: bad repid %d\n", rep);
424 		return;
425 	}
426 	scd = sc->sc_subdevs[rep];
427 	DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n",
428 		    rep, scd, scd ? scd->sc_state : 0));
429 	if (scd == NULL || !(scd->sc_state & UHIDEV_OPEN))
430 		return;
431 #ifdef DIAGNOSTIC
432 	if (scd->sc_in_rep_size != cc)
433 		printf("%s: bad input length %d != %d\n",USBDEVNAME(sc->sc_dev),
434 		       scd->sc_in_rep_size, cc);
435 #endif
436 	scd->sc_intr(scd, p, cc);
437 }
438 
439 void
440 uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size)
441 {
442 	*desc = sc->sc_repdesc;
443 	*size = sc->sc_repdesc_size;
444 }
445 
446 int
447 uhidev_open(struct uhidev *scd)
448 {
449 	struct uhidev_softc *sc = scd->sc_parent;
450 	usbd_status err;
451 
452 	DPRINTF(("uhidev_open: open pipe, state=%d refcnt=%d\n",
453 		 scd->sc_state, sc->sc_refcnt));
454 
455 	if (scd->sc_state & UHIDEV_OPEN)
456 		return (EBUSY);
457 	scd->sc_state |= UHIDEV_OPEN;
458 	if (sc->sc_refcnt++)
459 		return (0);
460 
461 	if (sc->sc_isize == 0)
462 		return (0);
463 
464 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
465 
466 	/* Set up interrupt pipe. */
467 	DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize,
468 		 sc->sc_ep_addr));
469 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
470 		  USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc, sc->sc_ibuf,
471 		  sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL);
472 	if (err) {
473 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
474 			 "error=%d\n",err));
475 		free(sc->sc_ibuf, M_USBDEV);
476 		scd->sc_state &= ~UHIDEV_OPEN;
477 		sc->sc_refcnt = 0;
478 		sc->sc_intrpipe = NULL;
479 		return (EIO);
480 	}
481 	return (0);
482 }
483 
484 void
485 uhidev_close(struct uhidev *scd)
486 {
487 	struct uhidev_softc *sc = scd->sc_parent;
488 
489 	if (!(scd->sc_state & UHIDEV_OPEN))
490 		return;
491 	scd->sc_state &= ~UHIDEV_OPEN;
492 	if (--sc->sc_refcnt)
493 		return;
494 	DPRINTF(("uhidev_close: close pipe\n"));
495 
496 	/* Disable interrupts. */
497 	if (sc->sc_intrpipe != NULL) {
498 		usbd_abort_pipe(sc->sc_intrpipe);
499 		usbd_close_pipe(sc->sc_intrpipe);
500 		sc->sc_intrpipe = NULL;
501 	}
502 
503 	if (sc->sc_ibuf != NULL) {
504 		free(sc->sc_ibuf, M_USBDEV);
505 		sc->sc_ibuf = NULL;
506 	}
507 }
508 
509 usbd_status
510 uhidev_set_report(struct uhidev *scd, int type, void *data, int len)
511 {
512 	char *buf;
513 	usbd_status retstat;
514 
515 	if (scd->sc_report_id == 0)
516 		return usbd_set_report(scd->sc_parent->sc_iface, type,
517 				       scd->sc_report_id, data, len);
518 
519 	buf = malloc(len + 1, M_TEMP, M_WAITOK);
520 	buf[0] = scd->sc_report_id;
521 	memcpy(buf+1, data, len);
522 
523 	retstat = usbd_set_report(scd->sc_parent->sc_iface, type,
524 				  scd->sc_report_id, data, len + 1);
525 
526 	free(buf, M_TEMP);
527 
528 	return retstat;
529 }
530 
531 void
532 uhidev_set_report_async(struct uhidev *scd, int type, void *data, int len)
533 {
534 	/* XXX */
535 	char buf[100];
536 	if (scd->sc_report_id) {
537 		buf[0] = scd->sc_report_id;
538 		memcpy(buf+1, data, len);
539 		len++;
540 		data = buf;
541 	}
542 
543 	usbd_set_report_async(scd->sc_parent->sc_iface, type,
544 			      scd->sc_report_id, data, len);
545 }
546 
547 usbd_status
548 uhidev_get_report(struct uhidev *scd, int type, void *data, int len)
549 {
550 	return usbd_get_report(scd->sc_parent->sc_iface, type,
551 			       scd->sc_report_id, data, len);
552 }
553