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