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