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