uhid.c revision 1.21 1 /* $NetBSD: uhid.c,v 1.21 1999/08/23 22:55:14 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 bdevice 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 ed->bEndpointAddress & UE_IN ? "in" : "out",
206 ed->bmAttributes & UE_XFERTYPE,
207 UGETW(ed->wMaxPacketSize), ed->bInterval));
208
209 if ((ed->bEndpointAddress & UE_IN) != UE_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 struct device *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 struct device *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(&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 usbd_status r;
338 USB_GET_SC_OPEN(uhid, UHIDUNIT(dev), sc);
339
340 DPRINTF(("uhidopen: sc=%p\n", sc));
341
342 if (sc->sc_dying)
343 return (ENXIO);
344
345 if (sc->sc_state & UHID_OPEN)
346 return (EBUSY);
347 sc->sc_state |= UHID_OPEN;
348
349 #if defined(__NetBSD__) || defined(__OpenBSD__)
350 if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) {
351 sc->sc_state &= ~UHID_OPEN;
352 return (ENOMEM);
353 }
354 #elif defined(__FreeBSD__)
355 clist_alloc_cblocks(&sc->sc_q, UHID_BSIZE, 0);
356 #endif
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 USB_GET_SC(uhid, UHIDUNIT(dev), sc);
388
389 DPRINTF(("uhidclose: sc=%p\n", sc));
390
391 /* Disable interrupts. */
392 usbd_abort_pipe(sc->sc_intrpipe);
393 usbd_close_pipe(sc->sc_intrpipe);
394 sc->sc_intrpipe = 0;
395
396 #if defined(__NetBSD__) || defined(__OpenBSD__)
397 clfree(&sc->sc_q);
398 #elif defined(__FreeBSD__)
399 clist_free_cblocks(&sc->sc_q);
400 #endif
401
402 free(sc->sc_ibuf, M_USBDEV);
403 free(sc->sc_obuf, M_USBDEV);
404
405 sc->sc_state &= ~UHID_OPEN;
406
407 return (0);
408 }
409
410 int
411 uhid_do_read(sc, uio, flag)
412 struct uhid_softc *sc;
413 struct uio *uio;
414 int flag;
415 {
416 int s;
417 int error = 0;
418 size_t length;
419 u_char buffer[UHID_CHUNK];
420 usbd_status r;
421
422 DPRINTFN(1, ("uhidread\n"));
423 if (sc->sc_state & UHID_IMMED) {
424 DPRINTFN(1, ("uhidread immed\n"));
425
426 r = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
427 sc->sc_iid, buffer, sc->sc_isize);
428 if (r != USBD_NORMAL_COMPLETION)
429 return (EIO);
430 return (uiomove(buffer, sc->sc_isize, uio));
431 }
432
433 s = splusb();
434 while (sc->sc_q.c_cc == 0) {
435 if (flag & IO_NDELAY) {
436 splx(s);
437 return (EWOULDBLOCK);
438 }
439 sc->sc_state |= UHID_ASLP;
440 DPRINTFN(5, ("uhidread: sleep on %p\n", sc));
441 error = tsleep(&sc->sc_q, PZERO | PCATCH, "uhidrea", 0);
442 DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
443 if (sc->sc_dying)
444 error = EIO;
445 if (error) {
446 sc->sc_state &= ~UHID_ASLP;
447 break;
448 }
449 if (sc->sc_state & UHID_NEEDCLEAR) {
450 DPRINTFN(-1,("uhidread: clearing stall\n"));
451 sc->sc_state &= ~UHID_NEEDCLEAR;
452 usbd_clear_endpoint_stall(sc->sc_intrpipe);
453 }
454 }
455 splx(s);
456
457 /* Transfer as many chunks as possible. */
458 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
459 length = min(sc->sc_q.c_cc, uio->uio_resid);
460 if (length > sizeof(buffer))
461 length = sizeof(buffer);
462
463 /* Remove a small chunk from the input queue. */
464 (void) q_to_b(&sc->sc_q, buffer, length);
465 DPRINTFN(5, ("uhidread: got %d chars\n", length));
466
467 /* Copy the data to the user process. */
468 if ((error = uiomove(buffer, length, uio)) != 0)
469 break;
470 }
471
472 return (error);
473 }
474
475 int
476 uhidread(dev, uio, flag)
477 dev_t dev;
478 struct uio *uio;
479 int flag;
480 {
481 USB_GET_SC(uhid, UHIDUNIT(dev), sc);
482 int error;
483
484 sc->sc_refcnt++;
485 error = uhid_do_read(sc, uio, flag);
486 if (--sc->sc_refcnt < 0)
487 usb_detach_wakeup(&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 USB_GET_SC(uhid, UHIDUNIT(dev), sc);
534 int error;
535
536 sc->sc_refcnt++;
537 error = uhid_do_write(sc, uio, flag);
538 if (--sc->sc_refcnt < 0)
539 usb_detach_wakeup(&sc->sc_dev);
540 return (error);
541 }
542
543 int
544 uhid_do_ioctl(sc, cmd, addr, flag, p)
545 struct uhid_softc *sc;
546 u_long cmd;
547 caddr_t addr;
548 int flag;
549 struct proc *p;
550 {
551 struct usb_ctl_report_desc *rd;
552 struct usb_ctl_report *re;
553 int size, id;
554 usbd_status r;
555
556 DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
557
558 if (sc->sc_dying)
559 return (EIO);
560
561 switch (cmd) {
562 case FIONBIO:
563 /* All handled in the upper FS layer. */
564 break;
565
566 case USB_GET_REPORT_DESC:
567 rd = (struct usb_ctl_report_desc *)addr;
568 size = min(sc->sc_repdesc_size, sizeof rd->data);
569 rd->size = size;
570 memcpy(rd->data, sc->sc_repdesc, size);
571 break;
572
573 case USB_SET_IMMED:
574 if (*(int *)addr) {
575 /* XXX should read into ibuf, but does it matter */
576 r = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
577 sc->sc_iid, sc->sc_ibuf,
578 sc->sc_isize);
579 if (r != USBD_NORMAL_COMPLETION)
580 return (EOPNOTSUPP);
581
582 sc->sc_state |= UHID_IMMED;
583 } else
584 sc->sc_state &= ~UHID_IMMED;
585 break;
586
587 case USB_GET_REPORT:
588 re = (struct usb_ctl_report *)addr;
589 switch (re->report) {
590 case UHID_INPUT_REPORT:
591 size = sc->sc_isize;
592 id = sc->sc_iid;
593 break;
594 case UHID_OUTPUT_REPORT:
595 size = sc->sc_osize;
596 id = sc->sc_oid;
597 break;
598 case UHID_FEATURE_REPORT:
599 size = sc->sc_fsize;
600 id = sc->sc_fid;
601 break;
602 default:
603 return (EINVAL);
604 }
605 r = usbd_get_report(sc->sc_iface, re->report, id,
606 re->data, size);
607 if (r != USBD_NORMAL_COMPLETION)
608 return (EIO);
609 break;
610
611 default:
612 return (EINVAL);
613 }
614 return (0);
615 }
616
617 int
618 uhidioctl(dev, cmd, addr, flag, p)
619 dev_t dev;
620 u_long cmd;
621 caddr_t addr;
622 int flag;
623 struct proc *p;
624 {
625 USB_GET_SC(uhid, UHIDUNIT(dev), sc);
626 int error;
627
628 sc->sc_refcnt++;
629 error = uhid_do_ioctl(sc, cmd, addr, flag, p);
630 if (--sc->sc_refcnt < 0)
631 usb_detach_wakeup(&sc->sc_dev);
632 return (error);
633 }
634
635 int
636 uhidpoll(dev, events, p)
637 dev_t dev;
638 int events;
639 struct proc *p;
640 {
641 int revents = 0;
642 int s;
643 USB_GET_SC(uhid, UHIDUNIT(dev), sc);
644
645 if (sc->sc_dying)
646 return (EIO);
647
648 s = splusb();
649 if (events & (POLLOUT | POLLWRNORM))
650 revents |= events & (POLLOUT | POLLWRNORM);
651 if (events & (POLLIN | POLLRDNORM)) {
652 if (sc->sc_q.c_cc > 0)
653 revents |= events & (POLLIN | POLLRDNORM);
654 else
655 selrecord(p, &sc->sc_rsel);
656 }
657
658 splx(s);
659 return (revents);
660 }
661
662 #if defined(__FreeBSD__)
663 DEV_DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass,
664 uhid_cdevsw, usbd_driver_load, 0);
665 #endif
666