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