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