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