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