uhidev.c revision 1.24.2.1 1 /* $NetBSD: uhidev.c,v 1.24.2.1 2007/01/23 18:45:56 tron 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.24.2.1 2007/01/23 18:45:56 tron 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:
178 case USB_PRODUCT_WACOM_GRAPHIRE3_6X8:
179 case USB_PRODUCT_WACOM_GRAPHIRE4_4X5: /* The 6x8 too? */
180 /*
181 * The Graphire3 needs 0x0202 to be written to
182 * feature report ID 2 before it'll start
183 * returning digitizer data.
184 */
185 usbd_set_report(uaa->iface, UHID_FEATURE_REPORT, 2,
186 &reportbuf, sizeof reportbuf);
187
188 size = sizeof uhid_graphire3_4x5_report_descr;
189 descptr = uhid_graphire3_4x5_report_descr;
190 break;
191 default:
192 /* Keep descriptor */
193 break;
194 }
195 }
196
197 if (descptr) {
198 desc = malloc(size, M_USBDEV, M_NOWAIT);
199 if (desc == NULL)
200 err = USBD_NOMEM;
201 else {
202 err = USBD_NORMAL_COMPLETION;
203 memcpy(desc, descptr, size);
204 }
205 } else {
206 desc = NULL;
207 err = usbd_read_report_desc(uaa->iface, &desc, &size,
208 M_USBDEV);
209 }
210 if (err) {
211 printf("%s: no report descriptor\n", USBDEVNAME(sc->sc_dev));
212 sc->sc_dying = 1;
213 USB_ATTACH_ERROR_RETURN;
214 }
215
216 sc->sc_repdesc = desc;
217 sc->sc_repdesc_size = size;
218
219 uha.uaa = uaa;
220 nrepid = uhidev_maxrepid(desc, size);
221 if (nrepid < 0)
222 USB_ATTACH_SUCCESS_RETURN;
223 if (nrepid > 0)
224 printf("%s: %d report ids\n", USBDEVNAME(sc->sc_dev), nrepid);
225 nrepid++;
226 sc->sc_subdevs = malloc(nrepid * sizeof(device_ptr_t),
227 M_USBDEV, M_NOWAIT | M_ZERO);
228 if (sc->sc_subdevs == NULL) {
229 printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
230 USB_ATTACH_ERROR_RETURN;
231 }
232 sc->sc_nrepid = nrepid;
233 sc->sc_isize = 0;
234
235 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
236 USBDEV(sc->sc_dev));
237
238 for (repid = 0; repid < nrepid; repid++) {
239 repsz = hid_report_size(desc, size, hid_input, repid);
240 DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz));
241 repsizes[repid] = repsz;
242 if (repsz > 0) {
243 if (repsz > sc->sc_isize)
244 sc->sc_isize = repsz;
245 }
246 }
247 sc->sc_isize += nrepid != 1; /* space for report ID */
248 DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize));
249
250 uha.parent = sc;
251 for (repid = 0; repid < nrepid; repid++) {
252 DPRINTF(("uhidev_match: try repid=%d\n", repid));
253 if (hid_report_size(desc, size, hid_input, repid) == 0 &&
254 hid_report_size(desc, size, hid_output, repid) == 0 &&
255 hid_report_size(desc, size, hid_feature, repid) == 0) {
256 ; /* already NULL in sc->sc_subdevs[repid] */
257 } else {
258 uha.reportid = repid;
259 ldesc->len = 1;
260 ldesc->locs[UHIDBUSCF_REPORTID] = repid;
261
262 dev = (struct uhidev *)config_found_sm_loc(self,
263 "uhidbus", ldesc, &uha,
264 uhidevprint, uhidevsubmatch);
265 sc->sc_subdevs[repid] = dev;
266 if (dev != NULL) {
267 dev->sc_in_rep_size = repsizes[repid];
268 #ifdef DIAGNOSTIC
269 DPRINTF(("uhidev_match: repid=%d dev=%p\n",
270 repid, dev));
271 if (dev->sc_intr == NULL) {
272 printf("%s: sc_intr == NULL\n",
273 USBDEVNAME(sc->sc_dev));
274 USB_ATTACH_ERROR_RETURN;
275 }
276 #endif
277 #if NRND > 0
278 rnd_attach_source(&dev->rnd_source,
279 USBDEVNAME(dev->sc_dev),
280 RND_TYPE_TTY, 0);
281 #endif
282 }
283 }
284 }
285
286 USB_ATTACH_SUCCESS_RETURN;
287 }
288
289 int
290 uhidev_maxrepid(void *buf, int len)
291 {
292 struct hid_data *d;
293 struct hid_item h;
294 int maxid;
295
296 maxid = -1;
297 h.report_ID = 0;
298 for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
299 if (h.report_ID > maxid)
300 maxid = h.report_ID;
301 hid_end_parse(d);
302 return (maxid);
303 }
304
305 int
306 uhidevprint(void *aux, const char *pnp)
307 {
308 struct uhidev_attach_arg *uha = aux;
309
310 if (pnp)
311 aprint_normal("uhid at %s", pnp);
312 if (uha->reportid != 0)
313 aprint_normal(" reportid %d", uha->reportid);
314 return (UNCONF);
315 }
316
317 int
318 uhidevsubmatch(struct device *parent, struct cfdata *cf,
319 const locdesc_t *ldesc, void *aux)
320 {
321 struct uhidev_attach_arg *uha = aux;
322
323 if (cf->cf_loc[UHIDBUSCF_REPORTID] != UHIDBUSCF_REPORTID_DEFAULT &&
324 cf->cf_loc[UHIDBUSCF_REPORTID] != ldesc->locs[UHIDBUSCF_REPORTID])
325 return (0);
326
327 if (cf->cf_loc[UHIDBUSCF_REPORTID] == ldesc->locs[UHIDBUSCF_REPORTID])
328 uha->matchlvl = UMATCH_VENDOR_PRODUCT;
329 else
330 uha->matchlvl = 0;
331 return (config_match(parent, cf, aux));
332 }
333
334 int
335 uhidev_activate(device_ptr_t self, enum devact act)
336 {
337 struct uhidev_softc *sc = (struct uhidev_softc *)self;
338 int i, rv;
339
340 switch (act) {
341 case DVACT_ACTIVATE:
342 return (EOPNOTSUPP);
343
344 case DVACT_DEACTIVATE:
345 rv = 0;
346 for (i = 0; i < sc->sc_nrepid; i++)
347 if (sc->sc_subdevs[i] != NULL)
348 rv |= config_deactivate(
349 &sc->sc_subdevs[i]->sc_dev);
350 sc->sc_dying = 1;
351 break;
352 default:
353 rv = 0;
354 break;
355 }
356 return (rv);
357 }
358
359 USB_DETACH(uhidev)
360 {
361 USB_DETACH_START(uhidev, sc);
362 int i, rv;
363
364 DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
365
366 sc->sc_dying = 1;
367 if (sc->sc_intrpipe != NULL)
368 usbd_abort_pipe(sc->sc_intrpipe);
369
370 if (sc->sc_repdesc != NULL)
371 free(sc->sc_repdesc, M_USBDEV);
372
373 rv = 0;
374 for (i = 0; i < sc->sc_nrepid; i++) {
375 if (sc->sc_subdevs[i] != NULL) {
376 #if NRND > 0
377 rnd_detach_source(&sc->sc_subdevs[i]->rnd_source);
378 #endif
379 rv |= config_detach(&sc->sc_subdevs[i]->sc_dev, flags);
380 sc->sc_subdevs[i] = NULL;
381 }
382 }
383
384 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
385 USBDEV(sc->sc_dev));
386
387 return (rv);
388 }
389
390 void
391 uhidev_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
392 {
393 struct uhidev_softc *sc = addr;
394 struct uhidev *scd;
395 u_char *p;
396 u_int rep;
397 u_int32_t cc;
398
399 usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
400
401 #ifdef UHIDEV_DEBUG
402 if (uhidevdebug > 5) {
403 u_int32_t i;
404
405 DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
406 DPRINTF(("uhidev_intr: data ="));
407 for (i = 0; i < cc; i++)
408 DPRINTF((" %02x", sc->sc_ibuf[i]));
409 DPRINTF(("\n"));
410 }
411 #endif
412
413 if (status == USBD_CANCELLED)
414 return;
415
416 if (status != USBD_NORMAL_COMPLETION) {
417 DPRINTF(("%s: interrupt status=%d\n", USBDEVNAME(sc->sc_dev),
418 status));
419 usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
420 return;
421 }
422
423 p = sc->sc_ibuf;
424 if (sc->sc_nrepid != 1)
425 rep = *p++, cc--;
426 else
427 rep = 0;
428 if (rep >= sc->sc_nrepid) {
429 printf("uhidev_intr: bad repid %d\n", rep);
430 return;
431 }
432 scd = sc->sc_subdevs[rep];
433 DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n",
434 rep, scd, scd ? scd->sc_state : 0));
435 if (scd == NULL || !(scd->sc_state & UHIDEV_OPEN))
436 return;
437 if (scd->sc_in_rep_size != cc) {
438 printf("%s: bad input length %d != %d\n",
439 USBDEVNAME(sc->sc_dev), scd->sc_in_rep_size, cc);
440 return;
441 }
442 #if NRND > 0
443 rnd_add_uint32(&scd->rnd_source, (uintptr_t)(sc->sc_ibuf));
444 #endif
445 scd->sc_intr(scd, p, cc);
446 }
447
448 void
449 uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size)
450 {
451 *desc = sc->sc_repdesc;
452 *size = sc->sc_repdesc_size;
453 }
454
455 int
456 uhidev_open(struct uhidev *scd)
457 {
458 struct uhidev_softc *sc = scd->sc_parent;
459 usbd_status err;
460
461 DPRINTF(("uhidev_open: open pipe, state=%d refcnt=%d\n",
462 scd->sc_state, sc->sc_refcnt));
463
464 if (scd->sc_state & UHIDEV_OPEN)
465 return (EBUSY);
466 scd->sc_state |= UHIDEV_OPEN;
467 if (sc->sc_refcnt++)
468 return (0);
469
470 if (sc->sc_isize == 0)
471 return (0);
472
473 sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
474
475 /* Set up interrupt pipe. */
476 DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize,
477 sc->sc_ep_addr));
478 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
479 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc, sc->sc_ibuf,
480 sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL);
481 if (err) {
482 DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
483 "error=%d\n",err));
484 free(sc->sc_ibuf, M_USBDEV);
485 scd->sc_state &= ~UHIDEV_OPEN;
486 sc->sc_refcnt = 0;
487 sc->sc_intrpipe = NULL;
488 return (EIO);
489 }
490 return (0);
491 }
492
493 void
494 uhidev_close(struct uhidev *scd)
495 {
496 struct uhidev_softc *sc = scd->sc_parent;
497
498 if (!(scd->sc_state & UHIDEV_OPEN))
499 return;
500 scd->sc_state &= ~UHIDEV_OPEN;
501 if (--sc->sc_refcnt)
502 return;
503 DPRINTF(("uhidev_close: close pipe\n"));
504
505 /* Disable interrupts. */
506 if (sc->sc_intrpipe != NULL) {
507 usbd_abort_pipe(sc->sc_intrpipe);
508 usbd_close_pipe(sc->sc_intrpipe);
509 sc->sc_intrpipe = NULL;
510 }
511
512 if (sc->sc_ibuf != NULL) {
513 free(sc->sc_ibuf, M_USBDEV);
514 sc->sc_ibuf = NULL;
515 }
516 }
517
518 usbd_status
519 uhidev_set_report(struct uhidev *scd, int type, void *data, int len)
520 {
521 char *buf;
522 usbd_status retstat;
523
524 if (scd->sc_report_id == 0)
525 return usbd_set_report(scd->sc_parent->sc_iface, type,
526 scd->sc_report_id, data, len);
527
528 buf = malloc(len + 1, M_TEMP, M_WAITOK);
529 buf[0] = scd->sc_report_id;
530 memcpy(buf+1, data, len);
531
532 retstat = usbd_set_report(scd->sc_parent->sc_iface, type,
533 scd->sc_report_id, buf, len + 1);
534
535 free(buf, M_TEMP);
536
537 return retstat;
538 }
539
540 void
541 uhidev_set_report_async(struct uhidev *scd, int type, void *data, int len)
542 {
543 /* XXX */
544 char buf[100];
545 if (scd->sc_report_id) {
546 buf[0] = scd->sc_report_id;
547 memcpy(buf+1, data, len);
548 len++;
549 data = buf;
550 }
551
552 usbd_set_report_async(scd->sc_parent->sc_iface, type,
553 scd->sc_report_id, data, len);
554 }
555
556 usbd_status
557 uhidev_get_report(struct uhidev *scd, int type, void *data, int len)
558 {
559 return usbd_get_report(scd->sc_parent->sc_iface, type,
560 scd->sc_report_id, data, len);
561 }
562