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