uhid.c revision 1.25 1 /* $NetBSD: uhid.c,v 1.25 1999/10/12 11:54:56 augustss Exp $ */
2
3 /*
4 * Copyright (c) 1998 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 (augustss (at) carlstedt.se) 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/usbhid10.pdf
42 */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #if defined(__NetBSD__) || defined(__OpenBSD__)
49 #include <sys/device.h>
50 #include <sys/ioctl.h>
51 #elif defined(__FreeBSD__)
52 #include <sys/ioccom.h>
53 #include <sys/filio.h>
54 #include <sys/module.h>
55 #include <sys/bus.h>
56 #include <sys/ioccom.h>
57 #endif
58 #include <sys/conf.h>
59 #include <sys/tty.h>
60 #include <sys/file.h>
61 #include <sys/select.h>
62 #include <sys/proc.h>
63 #include <sys/vnode.h>
64 #include <sys/poll.h>
65
66 #include <dev/usb/usb.h>
67 #include <dev/usb/usbhid.h>
68
69 #include <dev/usb/usbdi.h>
70 #include <dev/usb/usbdi_util.h>
71 #include <dev/usb/hid.h>
72 #include <dev/usb/usb_quirks.h>
73
74 #ifdef USB_DEBUG
75 #define DPRINTF(x) if (uhiddebug) logprintf x
76 #define DPRINTFN(n,x) if (uhiddebug>(n)) logprintf x
77 int uhiddebug = 0;
78 #else
79 #define DPRINTF(x)
80 #define DPRINTFN(n,x)
81 #endif
82
83 struct uhid_softc {
84 USBBASEDEVICE sc_dev; /* base device */
85 usbd_interface_handle sc_iface; /* interface */
86 usbd_pipe_handle sc_intrpipe; /* interrupt pipe */
87 int sc_ep_addr;
88
89 int sc_isize;
90 int sc_osize;
91 int sc_fsize;
92 u_int8_t sc_iid;
93 u_int8_t sc_oid;
94 u_int8_t sc_fid;
95
96 char *sc_ibuf;
97 char *sc_obuf;
98
99 void *sc_repdesc;
100 int sc_repdesc_size;
101
102 struct clist sc_q;
103 struct selinfo sc_rsel;
104 u_char sc_state; /* driver state */
105 #define UHID_OPEN 0x01 /* device is open */
106 #define UHID_ASLP 0x02 /* waiting for device data */
107 #define UHID_NEEDCLEAR 0x04 /* needs clearing endpoint stall */
108 #define UHID_IMMED 0x08 /* return read data immediately */
109
110 int sc_refcnt;
111 u_char sc_dying;
112 };
113
114 #define UHIDUNIT(dev) (minor(dev))
115 #define UHID_CHUNK 128 /* chunk size for read */
116 #define UHID_BSIZE 1020 /* buffer size */
117
118 #if defined(__NetBSD__) || defined(__OpenBSD__)
119 cdev_decl(uhid);
120 #elif defined(__FreeBSD__)
121 d_open_t uhidopen;
122 d_close_t uhidclose;
123 d_read_t uhidread;
124 d_write_t uhidwrite;
125 d_ioctl_t uhidioctl;
126 d_poll_t uhidpoll;
127
128 #define UHID_CDEV_MAJOR 122
129
130 static struct cdevsw uhid_cdevsw = {
131 /* open */ uhidopen,
132 /* close */ uhidclose,
133 /* read */ uhidread,
134 /* write */ uhidwrite,
135 /* ioctl */ uhidioctl,
136 /* stop */ nostop,
137 /* reset */ noreset,
138 /* devtotty */ nodevtotty,
139 /* poll */ uhidpoll,
140 /* mmap */ nommap,
141 /* strategy */ nostrategy,
142 /* name */ "uhid",
143 /* parms */ noparms,
144 /* maj */ UHID_CDEV_MAJOR,
145 /* dump */ nodump,
146 /* psize */ nopsize,
147 /* flags */ 0,
148 /* maxio */ 0,
149 /* bmaj */ -1
150 };
151 #endif
152
153 void uhid_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
154
155 int uhid_do_read __P((struct uhid_softc *, struct uio *uio, int));
156 int uhid_do_write __P((struct uhid_softc *, struct uio *uio, int));
157 int uhid_do_ioctl __P((struct uhid_softc *, u_long, caddr_t, int, struct proc *));
158
159 USB_DECLARE_DRIVER(uhid);
160
161 USB_MATCH(uhid)
162 {
163 USB_MATCH_START(uhid, uaa);
164 usb_interface_descriptor_t *id;
165
166 if (!uaa->iface)
167 return (UMATCH_NONE);
168 id = usbd_get_interface_descriptor(uaa->iface);
169 if (!id || id->bInterfaceClass != UCLASS_HID)
170 return (UMATCH_NONE);
171 return (UMATCH_IFACECLASS_GENERIC);
172 }
173
174 USB_ATTACH(uhid)
175 {
176 USB_ATTACH_START(uhid, sc, uaa);
177 usbd_interface_handle iface = uaa->iface;
178 usb_interface_descriptor_t *id;
179 usb_endpoint_descriptor_t *ed;
180 int size;
181 void *desc;
182 usbd_status r;
183 char devinfo[1024];
184
185 sc->sc_iface = iface;
186 id = usbd_get_interface_descriptor(iface);
187 usbd_devinfo(uaa->device, 0, devinfo);
188 USB_ATTACH_SETUP;
189 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
190 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
191
192 ed = usbd_interface2endpoint_descriptor(iface, 0);
193 if (!ed) {
194 printf("%s: could not read endpoint descriptor\n",
195 USBDEVNAME(sc->sc_dev));
196 sc->sc_dying = 1;
197 USB_ATTACH_ERROR_RETURN;
198 }
199
200 DPRINTFN(10,("uhid_attach: bLength=%d bDescriptorType=%d "
201 "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
202 " bInterval=%d\n",
203 ed->bLength, ed->bDescriptorType,
204 ed->bEndpointAddress & UE_ADDR,
205 UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
206 ed->bmAttributes & UE_XFERTYPE,
207 UGETW(ed->wMaxPacketSize), ed->bInterval));
208
209 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
210 (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
211 printf("%s: unexpected endpoint\n", USBDEVNAME(sc->sc_dev));
212 sc->sc_dying = 1;
213 USB_ATTACH_ERROR_RETURN;
214 }
215
216 sc->sc_ep_addr = ed->bEndpointAddress;
217
218 desc = 0;
219 r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_USBDEV);
220 if (r != USBD_NORMAL_COMPLETION) {
221 printf("%s: no report descriptor\n", USBDEVNAME(sc->sc_dev));
222 sc->sc_dying = 1;
223 if (desc)
224 free(desc, M_USBDEV);
225 USB_ATTACH_ERROR_RETURN;
226 }
227
228 (void)usbd_set_idle(iface, 0, 0);
229
230 sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
231 sc->sc_osize = hid_report_size(desc, size, hid_output, &sc->sc_oid);
232 sc->sc_fsize = hid_report_size(desc, size, hid_feature, &sc->sc_fid);
233
234 sc->sc_repdesc = desc;
235 sc->sc_repdesc_size = size;
236
237 USB_ATTACH_SUCCESS_RETURN;
238 }
239
240 int
241 uhid_activate(self, act)
242 device_ptr_t self;
243 enum devact act;
244 {
245 struct uhid_softc *sc = (struct uhid_softc *)self;
246
247 switch (act) {
248 case DVACT_ACTIVATE:
249 return (EOPNOTSUPP);
250 break;
251
252 case DVACT_DEACTIVATE:
253 sc->sc_dying = 1;
254 break;
255 }
256 return (0);
257 }
258
259 int
260 uhid_detach(self, flags)
261 device_ptr_t self;
262 int flags;
263 {
264 struct uhid_softc *sc = (struct uhid_softc *)self;
265 int maj, mn;
266 int s;
267
268 DPRINTF(("uhid_detach: sc=%p flags=%d\n", sc, flags));
269
270 sc->sc_dying = 1;
271 if (sc->sc_intrpipe)
272 usbd_abort_pipe(sc->sc_intrpipe);
273
274 if (sc->sc_state & UHID_OPEN) {
275 s = splusb();
276 if (--sc->sc_refcnt >= 0) {
277 /* Wake everyone */
278 wakeup(&sc->sc_q);
279 /* Wait for processes to go away. */
280 usb_detach_wait(USBDEV(sc->sc_dev));
281 }
282 splx(s);
283 }
284
285 /* locate the major number */
286 for (maj = 0; maj < nchrdev; maj++)
287 if (cdevsw[maj].d_open == uhidopen)
288 break;
289
290 /* Nuke the vnodes for any open instances (calls close). */
291 mn = self->dv_unit;
292 vdevgone(maj, mn, mn, VCHR);
293
294 free(sc->sc_repdesc, M_USBDEV);
295
296 return (0);
297 }
298
299 void
300 uhid_intr(reqh, addr, status)
301 usbd_request_handle reqh;
302 usbd_private_handle addr;
303 usbd_status status;
304 {
305 struct uhid_softc *sc = addr;
306
307 DPRINTFN(5, ("uhid_intr: status=%d\n", status));
308 DPRINTFN(5, ("uhid_intr: data = %02x %02x %02x\n",
309 sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
310
311 if (status == USBD_CANCELLED)
312 return;
313
314 if (status != USBD_NORMAL_COMPLETION) {
315 DPRINTF(("uhid_intr: status=%d\n", status));
316 sc->sc_state |= UHID_NEEDCLEAR;
317 return;
318 }
319
320 (void) b_to_q(sc->sc_ibuf, sc->sc_isize, &sc->sc_q);
321
322 if (sc->sc_state & UHID_ASLP) {
323 sc->sc_state &= ~UHID_ASLP;
324 DPRINTFN(5, ("uhid_intr: waking %p\n", sc));
325 wakeup(&sc->sc_q);
326 }
327 selwakeup(&sc->sc_rsel);
328 }
329
330 int
331 uhidopen(dev, flag, mode, p)
332 dev_t dev;
333 int flag;
334 int mode;
335 struct proc *p;
336 {
337 struct uhid_softc *sc;
338 usbd_status r;
339
340 USB_GET_SC_OPEN(uhid, UHIDUNIT(dev), sc);
341
342 DPRINTF(("uhidopen: sc=%p\n", sc));
343
344 if (sc->sc_dying)
345 return (ENXIO);
346
347 if (sc->sc_state & UHID_OPEN)
348 return (EBUSY);
349 sc->sc_state |= UHID_OPEN;
350
351 #if defined(__NetBSD__) || defined(__OpenBSD__)
352 if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) {
353 sc->sc_state &= ~UHID_OPEN;
354 return (ENOMEM);
355 }
356 #elif defined(__FreeBSD__)
357 clist_alloc_cblocks(&sc->sc_q, UHID_BSIZE, 0);
358 #endif
359
360 sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
361 sc->sc_obuf = malloc(sc->sc_osize, M_USBDEV, M_WAITOK);
362
363 /* Set up interrupt pipe. */
364 r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
365 USBD_SHORT_XFER_OK,
366 &sc->sc_intrpipe, sc, sc->sc_ibuf,
367 sc->sc_isize, uhid_intr);
368 if (r != USBD_NORMAL_COMPLETION) {
369 DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
370 "error=%d\n",r));
371 free(sc->sc_ibuf, M_USBDEV);
372 free(sc->sc_obuf, M_USBDEV);
373 sc->sc_state &= ~UHID_OPEN;
374 return (EIO);
375 }
376
377 sc->sc_state &= ~UHID_IMMED;
378
379 return (0);
380 }
381
382 int
383 uhidclose(dev, flag, mode, p)
384 dev_t dev;
385 int flag;
386 int mode;
387 struct proc *p;
388 {
389 struct uhid_softc *sc;
390
391 USB_GET_SC(uhid, UHIDUNIT(dev), sc);
392
393 DPRINTF(("uhidclose: sc=%p\n", sc));
394
395 /* Disable interrupts. */
396 usbd_abort_pipe(sc->sc_intrpipe);
397 usbd_close_pipe(sc->sc_intrpipe);
398 sc->sc_intrpipe = 0;
399
400 #if defined(__NetBSD__) || defined(__OpenBSD__)
401 clfree(&sc->sc_q);
402 #elif defined(__FreeBSD__)
403 clist_free_cblocks(&sc->sc_q);
404 #endif
405
406 free(sc->sc_ibuf, M_USBDEV);
407 free(sc->sc_obuf, M_USBDEV);
408
409 sc->sc_state &= ~UHID_OPEN;
410
411 return (0);
412 }
413
414 int
415 uhid_do_read(sc, uio, flag)
416 struct uhid_softc *sc;
417 struct uio *uio;
418 int flag;
419 {
420 int s;
421 int error = 0;
422 size_t length;
423 u_char buffer[UHID_CHUNK];
424 usbd_status r;
425
426 DPRINTFN(1, ("uhidread\n"));
427 if (sc->sc_state & UHID_IMMED) {
428 DPRINTFN(1, ("uhidread immed\n"));
429
430 r = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
431 sc->sc_iid, buffer, sc->sc_isize);
432 if (r != USBD_NORMAL_COMPLETION)
433 return (EIO);
434 return (uiomove(buffer, sc->sc_isize, uio));
435 }
436
437 s = splusb();
438 while (sc->sc_q.c_cc == 0) {
439 if (flag & IO_NDELAY) {
440 splx(s);
441 return (EWOULDBLOCK);
442 }
443 sc->sc_state |= UHID_ASLP;
444 DPRINTFN(5, ("uhidread: sleep on %p\n", sc));
445 error = tsleep(&sc->sc_q, PZERO | PCATCH, "uhidrea", 0);
446 DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
447 if (sc->sc_dying)
448 error = EIO;
449 if (error) {
450 sc->sc_state &= ~UHID_ASLP;
451 break;
452 }
453 if (sc->sc_state & UHID_NEEDCLEAR) {
454 DPRINTFN(-1,("uhidread: clearing stall\n"));
455 sc->sc_state &= ~UHID_NEEDCLEAR;
456 usbd_clear_endpoint_stall(sc->sc_intrpipe);
457 }
458 }
459 splx(s);
460
461 /* Transfer as many chunks as possible. */
462 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
463 length = min(sc->sc_q.c_cc, uio->uio_resid);
464 if (length > sizeof(buffer))
465 length = sizeof(buffer);
466
467 /* Remove a small chunk from the input queue. */
468 (void) q_to_b(&sc->sc_q, buffer, length);
469 DPRINTFN(5, ("uhidread: got %d chars\n", length));
470
471 /* Copy the data to the user process. */
472 if ((error = uiomove(buffer, length, uio)) != 0)
473 break;
474 }
475
476 return (error);
477 }
478
479 int
480 uhidread(dev, uio, flag)
481 dev_t dev;
482 struct uio *uio;
483 int flag;
484 {
485 struct uhid_softc *sc;
486 int error;
487
488 USB_GET_SC(uhid, UHIDUNIT(dev), sc);
489
490 sc->sc_refcnt++;
491 error = uhid_do_read(sc, uio, flag);
492 if (--sc->sc_refcnt < 0)
493 usb_detach_wakeup(USBDEV(sc->sc_dev));
494 return (error);
495 }
496
497 int
498 uhid_do_write(sc, uio, flag)
499 struct uhid_softc *sc;
500 struct uio *uio;
501 int flag;
502 {
503 int error;
504 int size;
505 usbd_status r;
506
507 DPRINTFN(1, ("uhidwrite\n"));
508
509 if (sc->sc_dying)
510 return (EIO);
511
512 size = sc->sc_osize;
513 error = 0;
514 if (uio->uio_resid != size)
515 return (EINVAL);
516 error = uiomove(sc->sc_obuf, size, uio);
517 if (!error) {
518 if (sc->sc_oid)
519 r = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
520 sc->sc_obuf[0],
521 sc->sc_obuf+1, size-1);
522 else
523 r = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
524 0, sc->sc_obuf, size);
525 if (r != USBD_NORMAL_COMPLETION) {
526 error = EIO;
527 }
528 }
529
530 return (error);
531 }
532
533 int
534 uhidwrite(dev, uio, flag)
535 dev_t dev;
536 struct uio *uio;
537 int flag;
538 {
539 struct uhid_softc *sc;
540 int error;
541
542 USB_GET_SC(uhid, UHIDUNIT(dev), sc);
543
544 sc->sc_refcnt++;
545 error = uhid_do_write(sc, uio, flag);
546 if (--sc->sc_refcnt < 0)
547 usb_detach_wakeup(USBDEV(sc->sc_dev));
548 return (error);
549 }
550
551 int
552 uhid_do_ioctl(sc, cmd, addr, flag, p)
553 struct uhid_softc *sc;
554 u_long cmd;
555 caddr_t addr;
556 int flag;
557 struct proc *p;
558 {
559 struct usb_ctl_report_desc *rd;
560 struct usb_ctl_report *re;
561 int size, id;
562 usbd_status r;
563
564 DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
565
566 if (sc->sc_dying)
567 return (EIO);
568
569 switch (cmd) {
570 case FIONBIO:
571 /* All handled in the upper FS layer. */
572 break;
573
574 case USB_GET_REPORT_DESC:
575 rd = (struct usb_ctl_report_desc *)addr;
576 size = min(sc->sc_repdesc_size, sizeof rd->data);
577 rd->size = size;
578 memcpy(rd->data, sc->sc_repdesc, size);
579 break;
580
581 case USB_SET_IMMED:
582 if (*(int *)addr) {
583 /* XXX should read into ibuf, but does it matter */
584 r = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
585 sc->sc_iid, sc->sc_ibuf,
586 sc->sc_isize);
587 if (r != USBD_NORMAL_COMPLETION)
588 return (EOPNOTSUPP);
589
590 sc->sc_state |= UHID_IMMED;
591 } else
592 sc->sc_state &= ~UHID_IMMED;
593 break;
594
595 case USB_GET_REPORT:
596 re = (struct usb_ctl_report *)addr;
597 switch (re->report) {
598 case UHID_INPUT_REPORT:
599 size = sc->sc_isize;
600 id = sc->sc_iid;
601 break;
602 case UHID_OUTPUT_REPORT:
603 size = sc->sc_osize;
604 id = sc->sc_oid;
605 break;
606 case UHID_FEATURE_REPORT:
607 size = sc->sc_fsize;
608 id = sc->sc_fid;
609 break;
610 default:
611 return (EINVAL);
612 }
613 r = usbd_get_report(sc->sc_iface, re->report, id,
614 re->data, size);
615 if (r != USBD_NORMAL_COMPLETION)
616 return (EIO);
617 break;
618
619 default:
620 return (EINVAL);
621 }
622 return (0);
623 }
624
625 int
626 uhidioctl(dev, cmd, addr, flag, p)
627 dev_t dev;
628 u_long cmd;
629 caddr_t addr;
630 int flag;
631 struct proc *p;
632 {
633 struct uhid_softc *sc;
634 int error;
635
636 USB_GET_SC(uhid, UHIDUNIT(dev), sc);
637
638 sc->sc_refcnt++;
639 error = uhid_do_ioctl(sc, cmd, addr, flag, p);
640 if (--sc->sc_refcnt < 0)
641 usb_detach_wakeup(USBDEV(sc->sc_dev));
642 return (error);
643 }
644
645 int
646 uhidpoll(dev, events, p)
647 dev_t dev;
648 int events;
649 struct proc *p;
650 {
651 struct uhid_softc *sc;
652 int revents = 0;
653 int s;
654
655 USB_GET_SC(uhid, UHIDUNIT(dev), sc);
656
657 if (sc->sc_dying)
658 return (EIO);
659
660 s = splusb();
661 if (events & (POLLOUT | POLLWRNORM))
662 revents |= events & (POLLOUT | POLLWRNORM);
663 if (events & (POLLIN | POLLRDNORM)) {
664 if (sc->sc_q.c_cc > 0)
665 revents |= events & (POLLIN | POLLRDNORM);
666 else
667 selrecord(p, &sc->sc_rsel);
668 }
669
670 splx(s);
671 return (revents);
672 }
673
674 #if defined(__FreeBSD__)
675 DEV_DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass,
676 uhid_cdevsw, usbd_driver_load, 0);
677 #endif
678