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