xref: /openbsd-src/sys/dev/usb/uhidev.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: uhidev.c,v 1.62 2014/07/12 18:48:52 tedu 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  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/signalvar.h>
43 #include <sys/device.h>
44 #include <sys/ioctl.h>
45 #include <sys/conf.h>
46 
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbhid.h>
49 
50 #include <dev/usb/usbdevs.h>
51 #include <dev/usb/usbdi.h>
52 #include <dev/usb/usbdi_util.h>
53 #include <dev/usb/hid.h>
54 #include <dev/usb/usb_quirks.h>
55 
56 #include <dev/usb/uhidev.h>
57 
58 #ifndef SMALL_KERNEL
59 /* Replacement report descriptors for devices shipped with broken ones */
60 #include <dev/usb/uhid_rdesc.h>
61 int uhidev_use_rdesc(struct uhidev_softc *, int, int, void **, int *);
62 #endif /* !SMALL_KERNEL */
63 
64 #define DEVNAME(sc)		((sc)->sc_dev.dv_xname)
65 
66 #ifdef UHIDEV_DEBUG
67 #define DPRINTF(x)	do { if (uhidevdebug) printf x; } while (0)
68 #define DPRINTFN(n,x)	do { if (uhidevdebug>(n)) printf x; } while (0)
69 int	uhidevdebug = 0;
70 #else
71 #define DPRINTF(x)
72 #define DPRINTFN(n,x)
73 #endif
74 
75 void uhidev_intr(struct usbd_xfer *, void *, usbd_status);
76 
77 int uhidev_maxrepid(void *buf, int len);
78 int uhidevprint(void *aux, const char *pnp);
79 int uhidevsubmatch(struct device *parent, void *cf, void *aux);
80 
81 int uhidev_match(struct device *, void *, void *);
82 void uhidev_attach(struct device *, struct device *, void *);
83 int uhidev_detach(struct device *, int);
84 int uhidev_activate(struct device *, int);
85 
86 struct cfdriver uhidev_cd = {
87 	NULL, "uhidev", DV_DULL
88 };
89 
90 const struct cfattach uhidev_ca = {
91 	sizeof(struct uhidev_softc), uhidev_match, uhidev_attach,
92 	uhidev_detach, uhidev_activate,
93 };
94 
95 int
96 uhidev_match(struct device *parent, void *match, void *aux)
97 {
98 	struct usb_attach_arg *uaa = aux;
99 	usb_interface_descriptor_t *id;
100 
101 	if (uaa->iface == NULL)
102 		return (UMATCH_NONE);
103 	id = usbd_get_interface_descriptor(uaa->iface);
104 	if (id == NULL)
105 		return (UMATCH_NONE);
106 #ifndef SMALL_KERNEL
107 	if (uaa->vendor == USB_VENDOR_MICROSOFT &&
108 	    uaa->product == USB_PRODUCT_MICROSOFT_XBOX360_CONTROLLER &&
109 	    id->bInterfaceNumber == 0)
110 		return (UMATCH_VENDOR_PRODUCT);
111 #endif /* !SMALL_KERNEL */
112 	if (id->bInterfaceClass != UICLASS_HID)
113 		return (UMATCH_NONE);
114 	if (usbd_get_quirks(uaa->device)->uq_flags & UQ_BAD_HID)
115 		return (UMATCH_NONE);
116 
117 	return (UMATCH_IFACECLASS_GENERIC);
118 }
119 
120 void
121 uhidev_attach(struct device *parent, struct device *self, void *aux)
122 {
123 	struct uhidev_softc *sc = (struct uhidev_softc *)self;
124 	struct usb_attach_arg *uaa = aux;
125 	usb_interface_descriptor_t *id;
126 	usb_endpoint_descriptor_t *ed;
127 	struct uhidev_attach_arg uha;
128 	int size, nrepid, repid, repsz;
129 	int i, repsizes[256];
130 	void *desc = NULL;
131 	struct device *dev;
132 
133 	sc->sc_udev = uaa->device;
134 	sc->sc_iface = uaa->iface;
135 	sc->sc_ifaceno = uaa->ifaceno;
136 	id = usbd_get_interface_descriptor(sc->sc_iface);
137 
138 	usbd_set_idle(sc->sc_udev, sc->sc_ifaceno, 0, 0);
139 
140 	sc->sc_iep_addr = sc->sc_oep_addr = -1;
141 	for (i = 0; i < id->bNumEndpoints; i++) {
142 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
143 		if (ed == NULL) {
144 			printf("%s: could not read endpoint descriptor\n",
145 			    DEVNAME(sc));
146 			return;
147 		}
148 
149 		DPRINTFN(10,("uhidev_attach: bLength=%d bDescriptorType=%d "
150 		    "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
151 		    " bInterval=%d\n",
152 		    ed->bLength, ed->bDescriptorType,
153 		    ed->bEndpointAddress & UE_ADDR,
154 		    UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
155 		    ed->bmAttributes & UE_XFERTYPE,
156 		    UGETW(ed->wMaxPacketSize), ed->bInterval));
157 
158 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
159 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
160 			sc->sc_iep_addr = ed->bEndpointAddress;
161 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
162 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
163 			sc->sc_oep_addr = ed->bEndpointAddress;
164 		} else {
165 			printf("%s: unexpected endpoint\n", DEVNAME(sc));
166 			return;
167 		}
168 	}
169 
170 	/*
171 	 * Check that we found an input interrupt endpoint.
172 	 * The output interrupt endpoint is optional
173 	 */
174 	if (sc->sc_iep_addr == -1) {
175 		printf("%s: no input interrupt endpoint\n", DEVNAME(sc));
176 		return;
177 	}
178 
179 #ifndef SMALL_KERNEL
180 	if (uhidev_use_rdesc(sc, uaa->vendor, uaa->product, &desc, &size))
181 		return;
182 #endif /* !SMALL_KERNEL */
183 
184 	if (desc == NULL) {
185 		struct usb_hid_descriptor *hid;
186 
187 		hid = usbd_get_hid_descriptor(sc->sc_udev, id);
188 		if (hid == NULL) {
189 			printf("%s: no HID descriptor\n", DEVNAME(sc));
190 			return;
191 		}
192 		size = UGETW(hid->descrs[0].wDescriptorLength);
193 		desc = malloc(size, M_USBDEV, M_NOWAIT);
194 		if (desc == NULL) {
195 			printf("%s: no memory\n", DEVNAME(sc));
196 			return;
197 		}
198 		if (usbd_get_report_descriptor(sc->sc_udev, sc->sc_ifaceno,
199 		    desc, size)) {
200 			printf("%s: no report descriptor\n", DEVNAME(sc));
201 			free(desc, M_USBDEV, 0);
202 			return;
203 		}
204 	}
205 
206 	sc->sc_repdesc = desc;
207 	sc->sc_repdesc_size = size;
208 
209 	nrepid = uhidev_maxrepid(desc, size);
210 	if (nrepid < 0)
211 		return;
212 	printf("%s: iclass %d/%d", DEVNAME(sc), id->bInterfaceClass,
213 	    id->bInterfaceSubClass);
214 	if (nrepid > 0)
215 		printf(", %d report id%s", nrepid, nrepid > 1 ? "s" : "");
216 	printf("\n");
217 	nrepid++;
218 	sc->sc_subdevs = malloc(nrepid * sizeof(struct device *),
219 	    M_USBDEV, M_NOWAIT | M_ZERO);
220 	if (sc->sc_subdevs == NULL) {
221 		printf("%s: no memory\n", DEVNAME(sc));
222 		return;
223 	}
224 	sc->sc_nrepid = nrepid;
225 	sc->sc_isize = 0;
226 
227 	for (repid = 0; repid < nrepid; repid++) {
228 		repsz = hid_report_size(desc, size, hid_input, repid);
229 		DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz));
230 		repsizes[repid] = repsz;
231 		if (repsz > sc->sc_isize)
232 			sc->sc_isize = repsz;
233 	}
234 	sc->sc_isize += (nrepid != 1);	/* one byte for the report ID */
235 	DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize));
236 
237 	uha.uaa = uaa;
238 	uha.parent = sc;
239 	uha.reportid = UHIDEV_CLAIM_ALLREPORTID;
240 
241 	/* Look for a driver claiming all report IDs first. */
242 	dev = config_found_sm(self, &uha, NULL, uhidevsubmatch);
243 	if (dev != NULL) {
244 		for (repid = 0; repid < nrepid; repid++)
245 			sc->sc_subdevs[repid] = (struct uhidev *)dev;
246 		return;
247 	}
248 
249 	for (repid = 0; repid < nrepid; repid++) {
250 		DPRINTF(("%s: try repid=%d\n", __func__, 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 			continue;
255 
256 		uha.reportid = repid;
257 		dev = config_found_sm(self, &uha, uhidevprint, uhidevsubmatch);
258 		sc->sc_subdevs[repid] = (struct uhidev *)dev;
259 	}
260 }
261 
262 #ifndef SMALL_KERNEL
263 int
264 uhidev_use_rdesc(struct uhidev_softc *sc, int vendor, int product,
265     void **descp, int *sizep)
266 {
267 	static uByte reportbuf[] = {2, 2, 2};
268 	const void *descptr = NULL;
269 	void *desc;
270 	int size;
271 
272 	if (vendor == USB_VENDOR_WACOM) {
273 
274 		/* The report descriptor for the Wacom Graphire is broken. */
275 		switch (product) {
276 		case USB_PRODUCT_WACOM_GRAPHIRE:
277 			size = sizeof(uhid_graphire_report_descr);
278 			descptr = uhid_graphire_report_descr;
279 			break;
280 		case USB_PRODUCT_WACOM_GRAPHIRE3_4X5:
281 		case USB_PRODUCT_WACOM_GRAPHIRE4_4X5:
282 			usbd_set_report(sc->sc_udev, sc->sc_ifaceno,
283 			    UHID_FEATURE_REPORT, 2, &reportbuf,
284 			    sizeof(reportbuf));
285 			size = sizeof(uhid_graphire3_4x5_report_descr);
286 			descptr = uhid_graphire3_4x5_report_descr;
287 			break;
288 		default:
289 			break;
290 		}
291 	} else if (vendor == USB_VENDOR_MICROSOFT &&
292 	    product == USB_PRODUCT_MICROSOFT_XBOX360_CONTROLLER) {
293 		/* The Xbox 360 gamepad has no report descriptor. */
294 		size = sizeof(uhid_xb360gp_report_descr);
295 		descptr = uhid_xb360gp_report_descr;
296 	}
297 
298 	if (descptr) {
299 		desc = malloc(size, M_USBDEV, M_NOWAIT);
300 		if (desc == NULL)
301 			return (ENOMEM);
302 
303 		memcpy(desc, descptr, size);
304 
305 		*descp = desc;
306 		*sizep = size;
307 	}
308 
309 	return (0);
310 }
311 #endif /* !SMALL_KERNEL */
312 
313 int
314 uhidev_maxrepid(void *buf, int len)
315 {
316 	struct hid_data *d;
317 	struct hid_item h;
318 	int maxid;
319 
320 	maxid = -1;
321 	h.report_ID = 0;
322 	for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
323 		if (h.report_ID > maxid)
324 			maxid = h.report_ID;
325 	hid_end_parse(d);
326 	return (maxid);
327 }
328 
329 int
330 uhidevprint(void *aux, const char *pnp)
331 {
332 	struct uhidev_attach_arg *uha = aux;
333 
334 	if (pnp)
335 		printf("uhid at %s", pnp);
336 	if (uha->reportid != 0 && uha->reportid != UHIDEV_CLAIM_ALLREPORTID)
337 		printf(" reportid %d", uha->reportid);
338 	return (UNCONF);
339 }
340 
341 int uhidevsubmatch(struct device *parent, void *match, void *aux)
342 {
343 	struct uhidev_attach_arg *uha = aux;
344         struct cfdata *cf = match;
345 
346 	if (cf->uhidevcf_reportid != UHIDEV_UNK_REPORTID &&
347 	    cf->uhidevcf_reportid != uha->reportid)
348 		return (0);
349 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
350 }
351 
352 int
353 uhidev_activate(struct device *self, int act)
354 {
355 	struct uhidev_softc *sc = (struct uhidev_softc *)self;
356 	int i, rv = 0, r;
357 
358 	switch (act) {
359 	case DVACT_DEACTIVATE:
360 		for (i = 0; i < sc->sc_nrepid; i++)
361 			if (sc->sc_subdevs[i] != NULL) {
362 				r = config_deactivate(
363 				    &sc->sc_subdevs[i]->sc_dev);
364 				if (r && r != EOPNOTSUPP)
365 					rv = r;
366 			}
367 		usbd_deactivate(sc->sc_udev);
368 		break;
369 	}
370 	return (rv);
371 }
372 
373 int
374 uhidev_detach(struct device *self, int flags)
375 {
376 	struct uhidev_softc *sc = (struct uhidev_softc *)self;
377 	int i, rv = 0;
378 
379 	DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
380 
381 	if (sc->sc_opipe != NULL) {
382 		usbd_abort_pipe(sc->sc_opipe);
383 		usbd_close_pipe(sc->sc_opipe);
384 		sc->sc_opipe = NULL;
385 	}
386 
387 	if (sc->sc_ipipe != NULL) {
388 		usbd_abort_pipe(sc->sc_ipipe);
389 		usbd_close_pipe(sc->sc_ipipe);
390 		sc->sc_ipipe = NULL;
391 	}
392 
393 	if (sc->sc_repdesc != NULL)
394 		free(sc->sc_repdesc, M_USBDEV, 0);
395 
396 	/*
397 	 * XXX Check if we have only one children claiming all the Report
398 	 * IDs, this is a hack since we need a dev -> Report ID mapping
399 	 * for uhidev_intr().
400 	 */
401 	if (sc->sc_nrepid > 1 && sc->sc_subdevs[0] != NULL &&
402 	    sc->sc_subdevs[0] == sc->sc_subdevs[1])
403 		return (config_detach(&sc->sc_subdevs[0]->sc_dev, flags));
404 
405 	for (i = 0; i < sc->sc_nrepid; i++) {
406 		if (sc->sc_subdevs[i] != NULL) {
407 			rv |= config_detach(&sc->sc_subdevs[i]->sc_dev, flags);
408 			sc->sc_subdevs[i] = NULL;
409 		}
410 	}
411 
412 	return (rv);
413 }
414 
415 void
416 uhidev_intr(struct usbd_xfer *xfer, void *addr, usbd_status status)
417 {
418 	struct uhidev_softc *sc = addr;
419 	struct uhidev *scd;
420 	u_char *p;
421 	u_int rep;
422 	u_int32_t cc;
423 
424 	if (usbd_is_dying(sc->sc_udev))
425 		return;
426 
427 	usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
428 
429 #ifdef UHIDEV_DEBUG
430 	if (uhidevdebug > 5) {
431 		u_int32_t i;
432 
433 		DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
434 		DPRINTF(("uhidev_intr: data ="));
435 		for (i = 0; i < cc; i++)
436 			DPRINTF((" %02x", sc->sc_ibuf[i]));
437 		DPRINTF(("\n"));
438 	}
439 #endif
440 
441 	if (status == USBD_CANCELLED || status == USBD_IOERROR)
442 		return;
443 
444 	if (status != USBD_NORMAL_COMPLETION) {
445 		DPRINTF(("%s: interrupt status=%d\n", DEVNAME(sc), status));
446 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
447 		return;
448 	}
449 
450 	p = sc->sc_ibuf;
451 	if (sc->sc_nrepid != 1)
452 		rep = *p++, cc--;
453 	else
454 		rep = 0;
455 	if (rep >= sc->sc_nrepid) {
456 		printf("uhidev_intr: bad repid %d\n", rep);
457 		return;
458 	}
459 	scd = sc->sc_subdevs[rep];
460 	DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n",
461 		    rep, scd, scd ? scd->sc_state : 0));
462 	if (scd == NULL || !(scd->sc_state & UHIDEV_OPEN))
463 		return;
464 
465 	scd->sc_intr(scd, p, cc);
466 }
467 
468 void
469 uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size)
470 {
471 	*desc = sc->sc_repdesc;
472 	*size = sc->sc_repdesc_size;
473 }
474 
475 int
476 uhidev_open(struct uhidev *scd)
477 {
478 	struct uhidev_softc *sc = scd->sc_parent;
479 	usbd_status err;
480 	int error;
481 
482 	DPRINTF(("uhidev_open: open pipe, state=%d refcnt=%d\n",
483 		 scd->sc_state, sc->sc_refcnt));
484 
485 	if (scd->sc_state & UHIDEV_OPEN)
486 		return (EBUSY);
487 	scd->sc_state |= UHIDEV_OPEN;
488 	if (sc->sc_refcnt++)
489 		return (0);
490 
491 	if (sc->sc_isize == 0)
492 		return (0);
493 
494 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
495 
496 	/* Set up input interrupt pipe. */
497 	DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize,
498 	    sc->sc_iep_addr));
499 
500 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_iep_addr,
501 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_ibuf,
502 		  sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL);
503 	if (err != USBD_NORMAL_COMPLETION) {
504 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
505 		    "error=%d\n", err));
506 		error = EIO;
507 		goto out1;
508 	}
509 
510 	DPRINTF(("uhidev_open: sc->sc_ipipe=%p\n", sc->sc_ipipe));
511 
512 	sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
513 	if (sc->sc_ixfer == NULL) {
514 		DPRINTF(("uhidev_open: couldn't allocate an xfer\n"));
515 		error = ENOMEM;
516 		goto out1; // xxxx
517 	}
518 
519 	/*
520 	 * Set up output interrupt pipe if an output interrupt endpoint
521 	 * exists.
522 	 */
523 	if (sc->sc_oep_addr != -1) {
524 		DPRINTF(("uhidev_open: oep=0x%02x\n", sc->sc_oep_addr));
525 
526 		err = usbd_open_pipe(sc->sc_iface, sc->sc_oep_addr,
527 		    0, &sc->sc_opipe);
528 
529 		if (err != USBD_NORMAL_COMPLETION) {
530 			DPRINTF(("uhidev_open: usbd_open_pipe failed, "
531 			    "error=%d\n", err));
532 			error = EIO;
533 			goto out2;
534 		}
535 		DPRINTF(("uhidev_open: sc->sc_opipe=%p\n", sc->sc_opipe));
536 
537 		sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
538 		if (sc->sc_oxfer == NULL) {
539 			DPRINTF(("uhidev_open: couldn't allocate an xfer\n"));
540 			error = ENOMEM;
541 			goto out3;
542 		}
543 
544 		sc->sc_owxfer = usbd_alloc_xfer(sc->sc_udev);
545 		if (sc->sc_owxfer == NULL) {
546 			DPRINTF(("uhidev_open: couldn't allocate owxfer\n"));
547 			error = ENOMEM;
548 			goto out3;
549 		}
550 	}
551 
552 	return (0);
553 
554 out3:
555 	/* Abort output pipe */
556 	usbd_close_pipe(sc->sc_opipe);
557 out2:
558 	/* Abort input pipe */
559 	usbd_close_pipe(sc->sc_ipipe);
560 out1:
561 	DPRINTF(("uhidev_open: failed in someway"));
562 	free(sc->sc_ibuf, M_USBDEV, 0);
563 	scd->sc_state &= ~UHIDEV_OPEN;
564 	sc->sc_refcnt = 0;
565 	sc->sc_ipipe = NULL;
566 	sc->sc_opipe = NULL;
567 	if (sc->sc_oxfer != NULL) {
568 		usbd_free_xfer(sc->sc_oxfer);
569 		sc->sc_oxfer = NULL;
570 	}
571 	if (sc->sc_owxfer != NULL) {
572 		usbd_free_xfer(sc->sc_owxfer);
573 		sc->sc_owxfer = NULL;
574 	}
575 	if (sc->sc_ixfer != NULL) {
576 		usbd_free_xfer(sc->sc_ixfer);
577 		sc->sc_ixfer = NULL;
578 	}
579 	return (error);
580 }
581 
582 void
583 uhidev_close(struct uhidev *scd)
584 {
585 	struct uhidev_softc *sc = scd->sc_parent;
586 
587 	if (!(scd->sc_state & UHIDEV_OPEN))
588 		return;
589 	scd->sc_state &= ~UHIDEV_OPEN;
590 	if (--sc->sc_refcnt)
591 		return;
592 	DPRINTF(("uhidev_close: close pipe\n"));
593 
594 	if (sc->sc_oxfer != NULL) {
595 		usbd_free_xfer(sc->sc_oxfer);
596 		sc->sc_oxfer = NULL;
597 	}
598 
599 	if (sc->sc_owxfer != NULL) {
600 		usbd_free_xfer(sc->sc_owxfer);
601 		sc->sc_owxfer = NULL;
602 	}
603 
604 	if (sc->sc_ixfer != NULL) {
605 		usbd_free_xfer(sc->sc_ixfer);
606 		sc->sc_ixfer = NULL;
607 	}
608 
609 	/* Disable interrupts. */
610 	if (sc->sc_opipe != NULL) {
611 		usbd_abort_pipe(sc->sc_opipe);
612 		usbd_close_pipe(sc->sc_opipe);
613 		sc->sc_opipe = NULL;
614 	}
615 
616 	if (sc->sc_ipipe != NULL) {
617 		usbd_abort_pipe(sc->sc_ipipe);
618 		usbd_close_pipe(sc->sc_ipipe);
619 		sc->sc_ipipe = NULL;
620 	}
621 
622 	if (sc->sc_ibuf != NULL) {
623 		free(sc->sc_ibuf, M_USBDEV, 0);
624 		sc->sc_ibuf = NULL;
625 	}
626 }
627 
628 usbd_status
629 uhidev_set_report(struct uhidev *scd, int type, int id, void *data, int len)
630 {
631 	struct uhidev_softc *sc = scd->sc_parent;
632 	char *buf;
633 	usbd_status retstat;
634 
635 	if (id == 0)
636 		return usbd_set_report(sc->sc_udev, sc->sc_ifaceno, type,
637 				       id, data, len);
638 
639 	buf = malloc(len + 1, M_TEMP, M_WAITOK);
640 	buf[0] = id;
641 	memcpy(buf+1, data, len);
642 
643 	retstat = usbd_set_report(sc->sc_udev, sc->sc_ifaceno, type,
644 				  id, buf, len + 1);
645 
646 	free(buf, M_TEMP, 0);
647 
648 	return retstat;
649 }
650 
651 usbd_status
652 uhidev_set_report_async(struct uhidev *scd, int type, int id, void *data,
653     int len)
654 {
655 	struct uhidev_softc *sc = scd->sc_parent;
656 	char *buf;
657 	usbd_status retstat;
658 
659 	if (id == 0)
660 		return usbd_set_report_async(sc->sc_udev, sc->sc_ifaceno, type,
661 					     id, data, len);
662 
663 	buf = malloc(len + 1, M_TEMP, M_NOWAIT);
664 	if (buf == NULL)
665 		return (USBD_NOMEM);
666 	buf[0] = id;
667 	memcpy(buf+1, data, len);
668 
669 	retstat = usbd_set_report_async(sc->sc_udev, sc->sc_ifaceno, type,
670 					id, buf, len + 1);
671 
672 	/*
673 	 * Since report requests are write-only it is safe to free
674 	 * the buffer right after submitting the transfer because
675 	 * it won't be used afterward.
676 	 */
677 	free(buf, M_TEMP, 0);
678 
679 	return retstat;
680 }
681 
682 usbd_status
683 uhidev_get_report(struct uhidev *scd, int type, int id, void *data, int len)
684 {
685 	struct uhidev_softc *sc = scd->sc_parent;
686 
687 	return usbd_get_report(sc->sc_udev, sc->sc_ifaceno, type,
688 			       id, data, len);
689 }
690 
691 usbd_status
692 uhidev_write(struct uhidev_softc *sc, void *data, int len)
693 {
694 	usbd_status error;
695 
696 	DPRINTF(("uhidev_write: data=%p, len=%d\n", data, len));
697 
698 	if (sc->sc_opipe == NULL)
699 		return USBD_INVAL;
700 
701 #ifdef UHIDEV_DEBUG
702 	if (uhidevdebug > 50) {
703 
704 		u_int32_t i;
705 		u_int8_t *d = data;
706 
707 		DPRINTF(("uhidev_write: data ="));
708 		for (i = 0; i < len; i++)
709 			DPRINTF((" %02x", d[i]));
710 		DPRINTF(("\n"));
711 	}
712 #endif
713 	usbd_setup_xfer(sc->sc_owxfer, sc->sc_opipe, 0, data, len,
714 	    USBD_SYNCHRONOUS | USBD_CATCH, 0, NULL);
715 	error = usbd_transfer(sc->sc_owxfer);
716 	if (error)
717 		usbd_clear_endpoint_stall(sc->sc_opipe);
718 
719 	return (error);
720 }
721 
722 int
723 uhidev_ioctl(struct uhidev *sc, u_long cmd, caddr_t addr, int flag,
724     struct proc *p)
725 {
726 	struct usb_ctl_report_desc *rd;
727 	struct usb_ctl_report *re;
728 	int size, extra;
729 	usbd_status err;
730 	void *desc;
731 
732 	switch (cmd) {
733 	case USB_GET_REPORT_DESC:
734 		uhidev_get_report_desc(sc->sc_parent, &desc, &size);
735 		rd = (struct usb_ctl_report_desc *)addr;
736 		size = min(size, sizeof rd->ucrd_data);
737 		rd->ucrd_size = size;
738 		memcpy(rd->ucrd_data, desc, size);
739 		break;
740 	case USB_GET_REPORT:
741 		re = (struct usb_ctl_report *)addr;
742 		switch (re->ucr_report) {
743 		case UHID_INPUT_REPORT:
744 			size = sc->sc_isize;
745 			break;
746 		case UHID_OUTPUT_REPORT:
747 			size = sc->sc_osize;
748 			break;
749 		case UHID_FEATURE_REPORT:
750 			size = sc->sc_fsize;
751 			break;
752 		default:
753 			return EINVAL;
754 		}
755 		extra = sc->sc_report_id != 0;
756 		err = uhidev_get_report(sc, re->ucr_report, sc->sc_report_id,
757 		    re->ucr_data, size + extra);
758 		if (extra)
759 			memcpy(re->ucr_data, re->ucr_data + 1, size);
760 		if (err)
761 			return EIO;
762 		break;
763 	case USB_SET_REPORT:
764 		re = (struct usb_ctl_report *)addr;
765 		switch (re->ucr_report) {
766 		case UHID_INPUT_REPORT:
767 			size = sc->sc_isize;
768 			break;
769 		case UHID_OUTPUT_REPORT:
770 			size = sc->sc_osize;
771 			break;
772 		case UHID_FEATURE_REPORT:
773 			size = sc->sc_fsize;
774 			break;
775 		default:
776 			return EINVAL;
777 		}
778 		err = uhidev_set_report(sc, re->ucr_report,
779 		    sc->sc_report_id, re->ucr_data, size);
780 		if (err)
781 			return EIO;
782 		break;
783 	case USB_GET_REPORT_ID:
784 		*(int *)addr = sc->sc_report_id;
785 		break;
786 	default:
787 		return -1;
788 	}
789 	return 0;
790 }
791