uhidev.c revision 1.7 1 /* $NetBSD: uhidev.c,v 1.7 2002/09/23 05:51:21 simonb 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
298 case DVACT_DEACTIVATE:
299 rv = 0;
300 for (i = 0; i < sc->sc_nrepid; i++)
301 if (sc->sc_subdevs[i] != NULL)
302 rv |= config_deactivate(
303 &sc->sc_subdevs[i]->sc_dev);
304 sc->sc_dying = 1;
305 break;
306 }
307 return (rv);
308 }
309
310 USB_DETACH(uhidev)
311 {
312 USB_DETACH_START(uhidev, sc);
313 int i, rv;
314
315 DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
316
317 sc->sc_dying = 1;
318 if (sc->sc_intrpipe != NULL)
319 usbd_abort_pipe(sc->sc_intrpipe);
320
321 if (sc->sc_repdesc != NULL)
322 free(sc->sc_repdesc, M_USBDEV);
323
324 rv = 0;
325 for (i = 0; i < sc->sc_nrepid; i++) {
326 if (sc->sc_subdevs[i] != NULL) {
327 rv |= config_detach(&sc->sc_subdevs[i]->sc_dev, flags);
328 sc->sc_subdevs[i] = NULL;
329 }
330 }
331
332 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
333 USBDEV(sc->sc_dev));
334
335 return (rv);
336 }
337
338 void
339 uhidev_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
340 {
341 struct uhidev_softc *sc = addr;
342 struct uhidev *scd;
343 u_char *p;
344 u_int rep;
345 u_int32_t cc;
346
347 usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
348
349 #ifdef UHIDEV_DEBUG
350 if (uhidevdebug > 5) {
351 u_int32_t i;
352
353 DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
354 DPRINTF(("uhidev_intr: data ="));
355 for (i = 0; i < cc; i++)
356 DPRINTF((" %02x", sc->sc_ibuf[i]));
357 DPRINTF(("\n"));
358 }
359 #endif
360
361 if (status == USBD_CANCELLED)
362 return;
363
364 if (status != USBD_NORMAL_COMPLETION) {
365 DPRINTF(("%s: interrupt status=%d\n", USBDEVNAME(sc->sc_dev),
366 status));
367 usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
368 return;
369 }
370
371 p = sc->sc_ibuf;
372 if (sc->sc_nrepid != 1)
373 rep = *p++, cc--;
374 else
375 rep = 0;
376 if (rep >= sc->sc_nrepid) {
377 printf("uhidev_intr: bad repid %d\n", rep);
378 return;
379 }
380 scd = sc->sc_subdevs[rep];
381 DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n",
382 rep, scd, scd ? scd->sc_state : 0));
383 if (scd == NULL || !(scd->sc_state & UHIDEV_OPEN))
384 return;
385 #ifdef DIAGNOSTIC
386 if (scd->sc_in_rep_size != cc)
387 printf("%s: bad input length %d != %d\n",USBDEVNAME(sc->sc_dev),
388 scd->sc_in_rep_size, cc);
389 #endif
390 scd->sc_intr(scd, p, cc);
391 }
392
393 void
394 uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size)
395 {
396 *desc = sc->sc_repdesc;
397 *size = sc->sc_repdesc_size;
398 }
399
400 int
401 uhidev_open(struct uhidev *scd)
402 {
403 struct uhidev_softc *sc = scd->sc_parent;
404 usbd_status err;
405
406 DPRINTF(("uhidev_open: open pipe, state=%d refcnt=%d\n",
407 scd->sc_state, sc->sc_refcnt));
408
409 if (scd->sc_state & UHIDEV_OPEN)
410 return (EBUSY);
411 scd->sc_state |= UHIDEV_OPEN;
412 if (sc->sc_refcnt++)
413 return (0);
414
415 if (sc->sc_isize == 0)
416 return (0);
417
418 sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
419
420 /* Set up interrupt pipe. */
421 DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize,
422 sc->sc_ep_addr));
423 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
424 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc, sc->sc_ibuf,
425 sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL);
426 if (err) {
427 DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
428 "error=%d\n",err));
429 free(sc->sc_ibuf, M_USBDEV);
430 scd->sc_state &= ~UHIDEV_OPEN;
431 sc->sc_refcnt = 0;
432 sc->sc_intrpipe = NULL;
433 return (EIO);
434 }
435 return (0);
436 }
437
438 void
439 uhidev_close(struct uhidev *scd)
440 {
441 struct uhidev_softc *sc = scd->sc_parent;
442
443 if (!(scd->sc_state & UHIDEV_OPEN))
444 return;
445 scd->sc_state &= ~UHIDEV_OPEN;
446 if (--sc->sc_refcnt)
447 return;
448 DPRINTF(("uhidev_close: close pipe\n"));
449
450 /* Disable interrupts. */
451 if (sc->sc_intrpipe != NULL) {
452 usbd_abort_pipe(sc->sc_intrpipe);
453 usbd_close_pipe(sc->sc_intrpipe);
454 sc->sc_intrpipe = NULL;
455 }
456
457 if (sc->sc_ibuf != NULL) {
458 free(sc->sc_ibuf, M_USBDEV);
459 sc->sc_ibuf = NULL;
460 }
461 }
462
463 usbd_status
464 uhidev_set_report(struct uhidev *scd, int type, void *data, int len)
465 {
466 /* XXX */
467 char buf[100];
468 if (scd->sc_report_id) {
469 buf[0] = scd->sc_report_id;
470 memcpy(buf+1, data, len);
471 len++;
472 data = buf;
473 }
474
475 return usbd_set_report(scd->sc_parent->sc_iface, type,
476 scd->sc_report_id, data, len);
477 }
478
479 void
480 uhidev_set_report_async(struct uhidev *scd, int type, void *data, int len)
481 {
482 /* XXX */
483 char buf[100];
484 if (scd->sc_report_id) {
485 buf[0] = scd->sc_report_id;
486 memcpy(buf+1, data, len);
487 len++;
488 data = buf;
489 }
490
491 usbd_set_report_async(scd->sc_parent->sc_iface, type,
492 scd->sc_report_id, data, len);
493 }
494
495 usbd_status
496 uhidev_get_report(struct uhidev *scd, int type, void *data, int len)
497 {
498 return usbd_get_report(scd->sc_parent->sc_iface, type,
499 scd->sc_report_id, data, len);
500 }
501