uhid.c revision 1.20 1 /* $NetBSD: uhid.c,v 1.20 1999/08/17 16:06:21 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 return (0);
246 }
247
248 int
249 uhid_detach(self, flags)
250 struct device *self;
251 int flags;
252 {
253 struct uhid_softc *sc = (struct uhid_softc *)self;
254 int maj, mn;
255 int s;
256
257 DPRINTF(("uhid_detach: sc=%p flags=%d\n", sc, flags));
258
259 sc->sc_dying = 1;
260 if (sc->sc_intrpipe)
261 usbd_abort_pipe(sc->sc_intrpipe);
262
263 if (sc->sc_state & UHID_OPEN) {
264 s = splusb();
265 if (--sc->sc_refcnt >= 0) {
266 /* Wake everyone */
267 wakeup(&sc->sc_q);
268 /* Wait for processes to go away. */
269 usb_detach_wait(&sc->sc_dev);
270 }
271 splx(s);
272 }
273
274 /* locate the major number */
275 for (maj = 0; maj < nchrdev; maj++)
276 if (cdevsw[maj].d_open == uhidopen)
277 break;
278
279 /* Nuke the vnodes for any open instances (calls close). */
280 mn = self->dv_unit;
281 vdevgone(maj, mn, mn, VCHR);
282
283 free(sc->sc_repdesc, M_USBDEV);
284
285 return (0);
286 }
287
288 void
289 uhid_intr(reqh, addr, status)
290 usbd_request_handle reqh;
291 usbd_private_handle addr;
292 usbd_status status;
293 {
294 struct uhid_softc *sc = addr;
295
296 DPRINTFN(5, ("uhid_intr: status=%d\n", status));
297 DPRINTFN(5, ("uhid_intr: data = %02x %02x %02x\n",
298 sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
299
300 if (status == USBD_CANCELLED)
301 return;
302
303 if (status != USBD_NORMAL_COMPLETION) {
304 DPRINTF(("uhid_intr: status=%d\n", status));
305 sc->sc_state |= UHID_NEEDCLEAR;
306 return;
307 }
308
309 (void) b_to_q(sc->sc_ibuf, sc->sc_isize, &sc->sc_q);
310
311 if (sc->sc_state & UHID_ASLP) {
312 sc->sc_state &= ~UHID_ASLP;
313 DPRINTFN(5, ("uhid_intr: waking %p\n", sc));
314 wakeup(&sc->sc_q);
315 }
316 selwakeup(&sc->sc_rsel);
317 }
318
319 int
320 uhidopen(dev, flag, mode, p)
321 dev_t dev;
322 int flag;
323 int mode;
324 struct proc *p;
325 {
326 usbd_status r;
327 USB_GET_SC_OPEN(uhid, UHIDUNIT(dev), sc);
328
329 DPRINTF(("uhidopen: sc=%p\n", sc));
330
331 if (sc->sc_dying)
332 return (ENXIO);
333
334 if (sc->sc_state & UHID_OPEN)
335 return (EBUSY);
336 sc->sc_state |= UHID_OPEN;
337
338 #if defined(__NetBSD__) || defined(__OpenBSD__)
339 if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) {
340 sc->sc_state &= ~UHID_OPEN;
341 return (ENOMEM);
342 }
343 #elif defined(__FreeBSD__)
344 clist_alloc_cblocks(&sc->sc_q, UHID_BSIZE, 0);
345 #endif
346
347 sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
348 sc->sc_obuf = malloc(sc->sc_osize, M_USBDEV, M_WAITOK);
349
350 /* Set up interrupt pipe. */
351 r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
352 USBD_SHORT_XFER_OK,
353 &sc->sc_intrpipe, sc, sc->sc_ibuf,
354 sc->sc_isize, uhid_intr);
355 if (r != USBD_NORMAL_COMPLETION) {
356 DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
357 "error=%d\n",r));
358 free(sc->sc_ibuf, M_USBDEV);
359 free(sc->sc_obuf, M_USBDEV);
360 sc->sc_state &= ~UHID_OPEN;
361 return (EIO);
362 }
363
364 sc->sc_state &= ~UHID_IMMED;
365
366 return (0);
367 }
368
369 int
370 uhidclose(dev, flag, mode, p)
371 dev_t dev;
372 int flag;
373 int mode;
374 struct proc *p;
375 {
376 USB_GET_SC(uhid, UHIDUNIT(dev), sc);
377
378 DPRINTF(("uhidclose: sc=%p\n", sc));
379
380 /* Disable interrupts. */
381 usbd_abort_pipe(sc->sc_intrpipe);
382 usbd_close_pipe(sc->sc_intrpipe);
383 sc->sc_intrpipe = 0;
384
385 #if defined(__NetBSD__) || defined(__OpenBSD__)
386 clfree(&sc->sc_q);
387 #elif defined(__FreeBSD__)
388 clist_free_cblocks(&sc->sc_q);
389 #endif
390
391 free(sc->sc_ibuf, M_USBDEV);
392 free(sc->sc_obuf, M_USBDEV);
393
394 sc->sc_state &= ~UHID_OPEN;
395
396 return (0);
397 }
398
399 int
400 uhid_do_read(sc, uio, flag)
401 struct uhid_softc *sc;
402 struct uio *uio;
403 int flag;
404 {
405 int s;
406 int error = 0;
407 size_t length;
408 u_char buffer[UHID_CHUNK];
409 usbd_status r;
410
411 DPRINTFN(1, ("uhidread\n"));
412 if (sc->sc_state & UHID_IMMED) {
413 DPRINTFN(1, ("uhidread immed\n"));
414
415 r = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
416 sc->sc_iid, buffer, sc->sc_isize);
417 if (r != USBD_NORMAL_COMPLETION)
418 return (EIO);
419 return (uiomove(buffer, sc->sc_isize, uio));
420 }
421
422 s = splusb();
423 while (sc->sc_q.c_cc == 0) {
424 if (flag & IO_NDELAY) {
425 splx(s);
426 return (EWOULDBLOCK);
427 }
428 sc->sc_state |= UHID_ASLP;
429 DPRINTFN(5, ("uhidread: sleep on %p\n", sc));
430 error = tsleep(&sc->sc_q, PZERO | PCATCH, "uhidrea", 0);
431 DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
432 if (sc->sc_dying)
433 error = EIO;
434 if (error) {
435 sc->sc_state &= ~UHID_ASLP;
436 break;
437 }
438 if (sc->sc_state & UHID_NEEDCLEAR) {
439 DPRINTFN(-1,("uhidread: clearing stall\n"));
440 sc->sc_state &= ~UHID_NEEDCLEAR;
441 usbd_clear_endpoint_stall(sc->sc_intrpipe);
442 }
443 }
444 splx(s);
445
446 /* Transfer as many chunks as possible. */
447 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
448 length = min(sc->sc_q.c_cc, uio->uio_resid);
449 if (length > sizeof(buffer))
450 length = sizeof(buffer);
451
452 /* Remove a small chunk from the input queue. */
453 (void) q_to_b(&sc->sc_q, buffer, length);
454 DPRINTFN(5, ("uhidread: got %d chars\n", length));
455
456 /* Copy the data to the user process. */
457 if ((error = uiomove(buffer, length, uio)) != 0)
458 break;
459 }
460
461 return (error);
462 }
463
464 int
465 uhidread(dev, uio, flag)
466 dev_t dev;
467 struct uio *uio;
468 int flag;
469 {
470 USB_GET_SC(uhid, UHIDUNIT(dev), sc);
471 int error;
472
473 sc->sc_refcnt++;
474 error = uhid_do_read(sc, uio, flag);
475 if (--sc->sc_refcnt < 0)
476 usb_detach_wakeup(&sc->sc_dev);
477 return (error);
478 }
479
480 int
481 uhid_do_write(sc, uio, flag)
482 struct uhid_softc *sc;
483 struct uio *uio;
484 int flag;
485 {
486 int error;
487 int size;
488 usbd_status r;
489
490 DPRINTFN(1, ("uhidwrite\n"));
491
492 if (sc->sc_dying)
493 return (EIO);
494
495 size = sc->sc_osize;
496 error = 0;
497 if (uio->uio_resid != size)
498 return (EINVAL);
499 error = uiomove(sc->sc_obuf, size, uio);
500 if (!error) {
501 if (sc->sc_oid)
502 r = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
503 sc->sc_obuf[0],
504 sc->sc_obuf+1, size-1);
505 else
506 r = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
507 0, sc->sc_obuf, size);
508 if (r != USBD_NORMAL_COMPLETION) {
509 error = EIO;
510 }
511 }
512
513 return (error);
514 }
515
516 int
517 uhidwrite(dev, uio, flag)
518 dev_t dev;
519 struct uio *uio;
520 int flag;
521 {
522 USB_GET_SC(uhid, UHIDUNIT(dev), sc);
523 int error;
524
525 sc->sc_refcnt++;
526 error = uhid_do_write(sc, uio, flag);
527 if (--sc->sc_refcnt < 0)
528 usb_detach_wakeup(&sc->sc_dev);
529 return (error);
530 }
531
532 int
533 uhid_do_ioctl(sc, cmd, addr, flag, p)
534 struct uhid_softc *sc;
535 u_long cmd;
536 caddr_t addr;
537 int flag;
538 struct proc *p;
539 {
540 struct usb_ctl_report_desc *rd;
541 struct usb_ctl_report *re;
542 int size, id;
543 usbd_status r;
544
545 DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
546
547 if (sc->sc_dying)
548 return (EIO);
549
550 switch (cmd) {
551 case FIONBIO:
552 /* All handled in the upper FS layer. */
553 break;
554
555 case USB_GET_REPORT_DESC:
556 rd = (struct usb_ctl_report_desc *)addr;
557 size = min(sc->sc_repdesc_size, sizeof rd->data);
558 rd->size = size;
559 memcpy(rd->data, sc->sc_repdesc, size);
560 break;
561
562 case USB_SET_IMMED:
563 if (*(int *)addr) {
564 /* XXX should read into ibuf, but does it matter */
565 r = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
566 sc->sc_iid, sc->sc_ibuf,
567 sc->sc_isize);
568 if (r != USBD_NORMAL_COMPLETION)
569 return (EOPNOTSUPP);
570
571 sc->sc_state |= UHID_IMMED;
572 } else
573 sc->sc_state &= ~UHID_IMMED;
574 break;
575
576 case USB_GET_REPORT:
577 re = (struct usb_ctl_report *)addr;
578 switch (re->report) {
579 case UHID_INPUT_REPORT:
580 size = sc->sc_isize;
581 id = sc->sc_iid;
582 break;
583 case UHID_OUTPUT_REPORT:
584 size = sc->sc_osize;
585 id = sc->sc_oid;
586 break;
587 case UHID_FEATURE_REPORT:
588 size = sc->sc_fsize;
589 id = sc->sc_fid;
590 break;
591 default:
592 return (EINVAL);
593 }
594 r = usbd_get_report(sc->sc_iface, re->report, id,
595 re->data, size);
596 if (r != USBD_NORMAL_COMPLETION)
597 return (EIO);
598 break;
599
600 default:
601 return (EINVAL);
602 }
603 return (0);
604 }
605
606 int
607 uhidioctl(dev, cmd, addr, flag, p)
608 dev_t dev;
609 u_long cmd;
610 caddr_t addr;
611 int flag;
612 struct proc *p;
613 {
614 USB_GET_SC(uhid, UHIDUNIT(dev), sc);
615 int error;
616
617 sc->sc_refcnt++;
618 error = uhid_do_ioctl(sc, cmd, addr, flag, p);
619 if (--sc->sc_refcnt < 0)
620 usb_detach_wakeup(&sc->sc_dev);
621 return (error);
622 }
623
624 int
625 uhidpoll(dev, events, p)
626 dev_t dev;
627 int events;
628 struct proc *p;
629 {
630 int revents = 0;
631 int s;
632 USB_GET_SC(uhid, UHIDUNIT(dev), sc);
633
634 if (sc->sc_dying)
635 return (EIO);
636
637 s = splusb();
638 if (events & (POLLOUT | POLLWRNORM))
639 revents |= events & (POLLOUT | POLLWRNORM);
640 if (events & (POLLIN | POLLRDNORM)) {
641 if (sc->sc_q.c_cc > 0)
642 revents |= events & (POLLIN | POLLRDNORM);
643 else
644 selrecord(p, &sc->sc_rsel);
645 }
646
647 splx(s);
648 return (revents);
649 }
650
651 #if defined(__FreeBSD__)
652 DEV_DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass,
653 uhid_cdevsw, usbd_driver_load, 0);
654 #endif
655