uhidev.c revision 1.3.2.5 1 /* $NetBSD: uhidev.c,v 1.3.2.5 2002/10/18 02:44:33 nathanw 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 #if NRND > 0
239 rnd_attach_source(&dev->rnd_source,
240 USBDEVNAME(dev->sc_dev),
241 RND_TYPE_TTY, 0);
242 #endif
243 }
244 }
245 }
246
247 USB_ATTACH_SUCCESS_RETURN;
248 }
249
250 int
251 uhidev_maxrepid(void *buf, int len)
252 {
253 struct hid_data *d;
254 struct hid_item h;
255 int maxid;
256
257 maxid = -1;
258 h.report_ID = 0;
259 for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
260 if (h.report_ID > maxid)
261 maxid = h.report_ID;
262 hid_end_parse(d);
263 return (maxid);
264 }
265
266 int
267 uhidevprint(void *aux, const char *pnp)
268 {
269 struct uhidev_attach_arg *uha = aux;
270
271 if (pnp)
272 printf("uhid at %s", pnp);
273 if (uha->reportid != 0)
274 printf(" reportid %d", uha->reportid);
275 return (UNCONF);
276 }
277
278 int
279 uhidevsubmatch(struct device *parent, struct cfdata *cf, void *aux)
280 {
281 struct uhidev_attach_arg *uha = aux;
282
283 if (cf->uhidevcf_reportid != UHIDEV_UNK_REPORTID &&
284 cf->uhidevcf_reportid != uha->reportid)
285 return (0);
286 if (cf->uhidevcf_reportid == uha->reportid)
287 uha->matchlvl = UMATCH_VENDOR_PRODUCT;
288 else
289 uha->matchlvl = 0;
290 return (config_match(parent, cf, aux));
291 }
292
293 int
294 uhidev_activate(device_ptr_t self, enum devact act)
295 {
296 struct uhidev_softc *sc = (struct uhidev_softc *)self;
297 int i, rv;
298
299 switch (act) {
300 case DVACT_ACTIVATE:
301 return (EOPNOTSUPP);
302
303 case DVACT_DEACTIVATE:
304 rv = 0;
305 for (i = 0; i < sc->sc_nrepid; i++)
306 if (sc->sc_subdevs[i] != NULL)
307 rv |= config_deactivate(
308 &sc->sc_subdevs[i]->sc_dev);
309 sc->sc_dying = 1;
310 break;
311 }
312 return (rv);
313 }
314
315 USB_DETACH(uhidev)
316 {
317 USB_DETACH_START(uhidev, sc);
318 int i, rv;
319
320 DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
321
322 sc->sc_dying = 1;
323 if (sc->sc_intrpipe != NULL)
324 usbd_abort_pipe(sc->sc_intrpipe);
325
326 if (sc->sc_repdesc != NULL)
327 free(sc->sc_repdesc, M_USBDEV);
328
329 rv = 0;
330 for (i = 0; i < sc->sc_nrepid; i++) {
331 if (sc->sc_subdevs[i] != NULL) {
332 #if NRND > 0
333 rnd_detach_source(&sc->sc_subdevs[i]->rnd_source);
334 #endif
335 rv |= config_detach(&sc->sc_subdevs[i]->sc_dev, flags);
336 sc->sc_subdevs[i] = NULL;
337 }
338 }
339
340 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
341 USBDEV(sc->sc_dev));
342
343 return (rv);
344 }
345
346 void
347 uhidev_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
348 {
349 struct uhidev_softc *sc = addr;
350 struct uhidev *scd;
351 u_char *p;
352 u_int rep;
353 u_int32_t cc;
354
355 usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
356
357 #ifdef UHIDEV_DEBUG
358 if (uhidevdebug > 5) {
359 u_int32_t i;
360
361 DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
362 DPRINTF(("uhidev_intr: data ="));
363 for (i = 0; i < cc; i++)
364 DPRINTF((" %02x", sc->sc_ibuf[i]));
365 DPRINTF(("\n"));
366 }
367 #endif
368
369 if (status == USBD_CANCELLED)
370 return;
371
372 if (status != USBD_NORMAL_COMPLETION) {
373 DPRINTF(("%s: interrupt status=%d\n", USBDEVNAME(sc->sc_dev),
374 status));
375 usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
376 return;
377 }
378
379 p = sc->sc_ibuf;
380 if (sc->sc_nrepid != 1)
381 rep = *p++, cc--;
382 else
383 rep = 0;
384 if (rep >= sc->sc_nrepid) {
385 printf("uhidev_intr: bad repid %d\n", rep);
386 return;
387 }
388 scd = sc->sc_subdevs[rep];
389 DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n",
390 rep, scd, scd ? scd->sc_state : 0));
391 if (scd == NULL || !(scd->sc_state & UHIDEV_OPEN))
392 return;
393 #ifdef DIAGNOSTIC
394 if (scd->sc_in_rep_size != cc)
395 printf("%s: bad input length %d != %d\n",USBDEVNAME(sc->sc_dev),
396 scd->sc_in_rep_size, cc);
397 #endif
398 #if NRND > 0
399 rnd_add_uint32(&scd->rnd_source, (uintptr_t)(sc->sc_ibuf));
400 #endif
401 scd->sc_intr(scd, p, cc);
402 }
403
404 void
405 uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size)
406 {
407 *desc = sc->sc_repdesc;
408 *size = sc->sc_repdesc_size;
409 }
410
411 int
412 uhidev_open(struct uhidev *scd)
413 {
414 struct uhidev_softc *sc = scd->sc_parent;
415 usbd_status err;
416
417 DPRINTF(("uhidev_open: open pipe, state=%d refcnt=%d\n",
418 scd->sc_state, sc->sc_refcnt));
419
420 if (scd->sc_state & UHIDEV_OPEN)
421 return (EBUSY);
422 scd->sc_state |= UHIDEV_OPEN;
423 if (sc->sc_refcnt++)
424 return (0);
425
426 if (sc->sc_isize == 0)
427 return (0);
428
429 sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
430
431 /* Set up interrupt pipe. */
432 DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize,
433 sc->sc_ep_addr));
434 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
435 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc, sc->sc_ibuf,
436 sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL);
437 if (err) {
438 DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
439 "error=%d\n",err));
440 free(sc->sc_ibuf, M_USBDEV);
441 scd->sc_state &= ~UHIDEV_OPEN;
442 sc->sc_refcnt = 0;
443 sc->sc_intrpipe = NULL;
444 return (EIO);
445 }
446 return (0);
447 }
448
449 void
450 uhidev_close(struct uhidev *scd)
451 {
452 struct uhidev_softc *sc = scd->sc_parent;
453
454 if (!(scd->sc_state & UHIDEV_OPEN))
455 return;
456 scd->sc_state &= ~UHIDEV_OPEN;
457 if (--sc->sc_refcnt)
458 return;
459 DPRINTF(("uhidev_close: close pipe\n"));
460
461 /* Disable interrupts. */
462 if (sc->sc_intrpipe != NULL) {
463 usbd_abort_pipe(sc->sc_intrpipe);
464 usbd_close_pipe(sc->sc_intrpipe);
465 sc->sc_intrpipe = NULL;
466 }
467
468 if (sc->sc_ibuf != NULL) {
469 free(sc->sc_ibuf, M_USBDEV);
470 sc->sc_ibuf = NULL;
471 }
472 }
473
474 usbd_status
475 uhidev_set_report(struct uhidev *scd, int type, void *data, int len)
476 {
477 /* XXX */
478 char buf[100];
479 if (scd->sc_report_id) {
480 buf[0] = scd->sc_report_id;
481 memcpy(buf+1, data, len);
482 len++;
483 data = buf;
484 }
485
486 return usbd_set_report(scd->sc_parent->sc_iface, type,
487 scd->sc_report_id, data, len);
488 }
489
490 void
491 uhidev_set_report_async(struct uhidev *scd, int type, void *data, int len)
492 {
493 /* XXX */
494 char buf[100];
495 if (scd->sc_report_id) {
496 buf[0] = scd->sc_report_id;
497 memcpy(buf+1, data, len);
498 len++;
499 data = buf;
500 }
501
502 usbd_set_report_async(scd->sc_parent->sc_iface, type,
503 scd->sc_report_id, data, len);
504 }
505
506 usbd_status
507 uhidev_get_report(struct uhidev *scd, int type, void *data, int len)
508 {
509 return usbd_get_report(scd->sc_parent->sc_iface, type,
510 scd->sc_report_id, data, len);
511 }
512