uhid.c revision 1.12 1 /* $NetBSD: uhid.c,v 1.12 1998/12/26 12:53:02 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 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #if defined(__NetBSD__)
46 #include <sys/device.h>
47 #include <sys/ioctl.h>
48 #elif defined(__FreeBSD__)
49 #include <sys/ioccom.h>
50 #include <sys/filio.h>
51 #include <sys/module.h>
52 #include <sys/bus.h>
53 #endif
54 #include <sys/device.h>
55 #include <sys/ioctl.h>
56 #include <sys/tty.h>
57 #include <sys/file.h>
58 #include <sys/select.h>
59 #include <sys/proc.h>
60 #include <sys/vnode.h>
61 #include <sys/poll.h>
62
63 #include <dev/usb/usb.h>
64 #include <dev/usb/usbhid.h>
65
66 #include <dev/usb/usbdi.h>
67 #include <dev/usb/usbdi_util.h>
68 #include <dev/usb/hid.h>
69 #include <dev/usb/usb_quirks.h>
70
71 #ifdef USB_DEBUG
72 #define DPRINTF(x) if (uhiddebug) printf x
73 #define DPRINTFN(n,x) if (uhiddebug>(n)) printf x
74 int uhiddebug = 0;
75 #else
76 #define DPRINTF(x)
77 #define DPRINTFN(n,x)
78 #endif
79
80 struct uhid_softc {
81 bdevice sc_dev; /* base device */
82 usbd_interface_handle sc_iface; /* interface */
83 usbd_pipe_handle sc_intrpipe; /* interrupt pipe */
84 int sc_ep_addr;
85
86 int sc_isize;
87 int sc_osize;
88 int sc_fsize;
89 u_int8_t sc_iid;
90 u_int8_t sc_oid;
91 u_int8_t sc_fid;
92
93 char *sc_ibuf;
94 char *sc_obuf;
95
96 void *sc_repdesc;
97 int sc_repdesc_size;
98
99 struct clist sc_q;
100 struct selinfo sc_rsel;
101 u_char sc_state; /* driver state */
102 #define UHID_OPEN 0x01 /* device is open */
103 #define UHID_ASLP 0x02 /* waiting for device data */
104 #define UHID_NEEDCLEAR 0x04 /* needs clearing endpoint stall */
105 #define UHID_IMMED 0x08 /* return read data immediately */
106 int sc_disconnected; /* device is gone */
107 };
108
109 #define UHIDUNIT(dev) (minor(dev))
110 #define UHID_CHUNK 128 /* chunk size for read */
111 #define UHID_BSIZE 1020 /* buffer size */
112
113 int uhidopen __P((dev_t, int, int, struct proc *));
114 int uhidclose __P((dev_t, int, int, struct proc *p));
115 int uhidread __P((dev_t, struct uio *uio, int));
116 int uhidwrite __P((dev_t, struct uio *uio, int));
117 int uhidioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
118 int uhidpoll __P((dev_t, int, struct proc *));
119 void uhid_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
120 void uhid_disco __P((void *));
121
122 USB_DECLARE_DRIVER(uhid);
123
124 USB_MATCH(uhid)
125 {
126 USB_MATCH_START(uhid, uaa);
127 usb_interface_descriptor_t *id;
128
129 if (!uaa->iface)
130 return (UMATCH_NONE);
131 id = usbd_get_interface_descriptor(uaa->iface);
132 if (!id || id->bInterfaceClass != UCLASS_HID)
133 return (UMATCH_NONE);
134 return (UMATCH_IFACECLASS_GENERIC);
135 }
136
137 USB_ATTACH(uhid)
138 {
139 USB_ATTACH_START(uhid, sc, uaa);
140 usbd_interface_handle iface = uaa->iface;
141 usb_interface_descriptor_t *id;
142 usb_endpoint_descriptor_t *ed;
143 int size;
144 void *desc;
145 usbd_status r;
146 char devinfo[1024];
147
148 sc->sc_disconnected = 1;
149 sc->sc_iface = iface;
150 id = usbd_get_interface_descriptor(iface);
151 usbd_devinfo(uaa->device, 0, devinfo);
152 USB_ATTACH_SETUP;
153 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
154 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
155
156 ed = usbd_interface2endpoint_descriptor(iface, 0);
157 if (!ed) {
158 printf("%s: could not read endpoint descriptor\n",
159 USBDEVNAME(sc->sc_dev));
160 USB_ATTACH_ERROR_RETURN;
161 }
162
163 DPRINTFN(10,("uhid_attach: bLength=%d bDescriptorType=%d "
164 "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
165 " bInterval=%d\n",
166 ed->bLength, ed->bDescriptorType,
167 ed->bEndpointAddress & UE_ADDR,
168 ed->bEndpointAddress & UE_IN ? "in" : "out",
169 ed->bmAttributes & UE_XFERTYPE,
170 UGETW(ed->wMaxPacketSize), ed->bInterval));
171
172 if ((ed->bEndpointAddress & UE_IN) != UE_IN ||
173 (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
174 printf("%s: unexpected endpoint\n", USBDEVNAME(sc->sc_dev));
175 USB_ATTACH_ERROR_RETURN;
176 }
177
178 sc->sc_ep_addr = ed->bEndpointAddress;
179 sc->sc_disconnected = 0;
180
181 r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_USB);
182 if (r != USBD_NORMAL_COMPLETION) {
183 printf("%s: no report descriptor\n", USBDEVNAME(sc->sc_dev));
184 USB_ATTACH_ERROR_RETURN;
185 }
186
187 (void)usbd_set_idle(iface, 0, 0);
188
189 sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
190 sc->sc_osize = hid_report_size(desc, size, hid_output, &sc->sc_oid);
191 sc->sc_fsize = hid_report_size(desc, size, hid_feature, &sc->sc_fid);
192
193 sc->sc_repdesc = desc;
194 sc->sc_repdesc_size = size;
195
196 USB_ATTACH_SUCCESS_RETURN;
197 }
198
199 #if defined(__FreeBSD__)
200 static int
201 uhid_detach(device_t self)
202 {
203 struct uhid_softc *sc = device_get_softc(self);
204 char *devinfo = (char *) device_get_desc(self);
205
206 if (devinfo) {
207 device_set_desc(self, NULL);
208 free(devinfo, M_USB);
209 }
210 return 0;
211 }
212 #endif
213
214 void
215 uhid_disco(p)
216 void *p;
217 {
218 struct uhid_softc *sc = p;
219
220 DPRINTF(("ums_hid: sc=%p\n", sc));
221 usbd_abort_pipe(sc->sc_intrpipe);
222 sc->sc_disconnected = 1;
223 }
224
225 void
226 uhid_intr(reqh, addr, status)
227 usbd_request_handle reqh;
228 usbd_private_handle addr;
229 usbd_status status;
230 {
231 struct uhid_softc *sc = addr;
232
233 DPRINTFN(5, ("uhid_intr: status=%d\n", status));
234 DPRINTFN(5, ("uhid_intr: data = %02x %02x %02x\n",
235 sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
236
237 if (status == USBD_CANCELLED)
238 return;
239
240 if (status != USBD_NORMAL_COMPLETION) {
241 DPRINTF(("uhid_intr: status=%d\n", status));
242 sc->sc_state |= UHID_NEEDCLEAR;
243 return;
244 }
245
246 (void) b_to_q(sc->sc_ibuf, sc->sc_isize, &sc->sc_q);
247
248 if (sc->sc_state & UHID_ASLP) {
249 sc->sc_state &= ~UHID_ASLP;
250 DPRINTFN(5, ("uhid_intr: waking %p\n", sc));
251 wakeup((caddr_t)sc);
252 }
253 selwakeup(&sc->sc_rsel);
254 }
255
256 int
257 uhidopen(dev, flag, mode, p)
258 dev_t dev;
259 int flag;
260 int mode;
261 struct proc *p;
262 {
263 usbd_status r;
264 USB_GET_SC_OPEN(uhid, UHIDUNIT(dev), sc);
265
266 if (!sc)
267 return (ENXIO);
268
269 DPRINTF(("uhidopen: sc=%p, disco=%d\n", sc, sc->sc_disconnected));
270
271 if (sc->sc_disconnected)
272 return (EIO);
273
274 if (sc->sc_state & UHID_OPEN)
275 return (EBUSY);
276
277 #if defined(__NetBSD__)
278 if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1)
279 return (ENOMEM);
280 #elif defined(__FreeBSD__)
281 clist_alloc_cblocks(&sc->sc_q, UHID_BSIZE, 0);
282 #endif
283
284 sc->sc_state |= UHID_OPEN;
285 sc->sc_state &= ~UHID_IMMED;
286
287 sc->sc_ibuf = malloc(sc->sc_isize, M_USB, M_WAITOK);
288 sc->sc_obuf = malloc(sc->sc_osize, M_USB, M_WAITOK);
289
290 /* Set up interrupt pipe. */
291 r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
292 USBD_SHORT_XFER_OK,
293 &sc->sc_intrpipe, sc, sc->sc_ibuf,
294 sc->sc_isize, uhid_intr);
295 if (r != USBD_NORMAL_COMPLETION) {
296 DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
297 "error=%d\n",r));
298 sc->sc_state &= ~UHID_OPEN;
299 return (EIO);
300 }
301 usbd_set_disco(sc->sc_intrpipe, uhid_disco, sc);
302
303 return (0);
304 }
305
306 int
307 uhidclose(dev, flag, mode, p)
308 dev_t dev;
309 int flag;
310 int mode;
311 struct proc *p;
312 {
313 USB_GET_SC(uhid, UHIDUNIT(dev), sc);
314
315 if (sc->sc_disconnected)
316 return (EIO);
317
318 DPRINTF(("uhidclose: sc=%p\n", sc));
319
320 /* Disable interrupts. */
321 usbd_abort_pipe(sc->sc_intrpipe);
322 usbd_close_pipe(sc->sc_intrpipe);
323
324 sc->sc_state &= ~UHID_OPEN;
325
326 #if defined(__NetBSD__)
327 clfree(&sc->sc_q);
328 #elif defined(__FreeBSD__)
329 clist_free_cblocks(&sc->sc_q);
330 #endif
331
332 free(sc->sc_ibuf, M_USB);
333 free(sc->sc_obuf, M_USB);
334
335 return (0);
336 }
337
338 int
339 uhidread(dev, uio, flag)
340 dev_t dev;
341 struct uio *uio;
342 int flag;
343 {
344 int s;
345 int error = 0;
346 size_t length;
347 u_char buffer[UHID_CHUNK];
348 usbd_status r;
349 USB_GET_SC(uhid, UHIDUNIT(dev), sc);
350
351 if (sc->sc_disconnected)
352 return (EIO);
353
354 DPRINTFN(1, ("uhidread\n"));
355 if (sc->sc_state & UHID_IMMED) {
356 DPRINTFN(1, ("uhidread immed\n"));
357
358 r = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
359 sc->sc_iid, sc->sc_ibuf, sc->sc_isize);
360 if (r != USBD_NORMAL_COMPLETION)
361 return (EIO);
362 return (uiomove(buffer, sc->sc_isize, uio));
363 }
364
365 s = splusb();
366 while (sc->sc_q.c_cc == 0) {
367 if (flag & IO_NDELAY) {
368 splx(s);
369 return EWOULDBLOCK;
370 }
371 sc->sc_state |= UHID_ASLP;
372 DPRINTFN(5, ("uhidread: sleep on %p\n", sc));
373 error = tsleep((caddr_t)sc, PZERO | PCATCH, "uhidrea", 0);
374 DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
375 if (error) {
376 sc->sc_state &= ~UHID_ASLP;
377 splx(s);
378 return (error);
379 }
380 if (sc->sc_state & UHID_NEEDCLEAR) {
381 DPRINTFN(-1,("uhidread: clearing stall\n"));
382 sc->sc_state &= ~UHID_NEEDCLEAR;
383 usbd_clear_endpoint_stall(sc->sc_intrpipe);
384 }
385 }
386 splx(s);
387
388 /* Transfer as many chunks as possible. */
389 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
390 length = min(sc->sc_q.c_cc, uio->uio_resid);
391 if (length > sizeof(buffer))
392 length = sizeof(buffer);
393
394 /* Remove a small chunk from the input queue. */
395 (void) q_to_b(&sc->sc_q, buffer, length);
396 DPRINTFN(5, ("uhidread: got %d chars\n", length));
397
398 /* Copy the data to the user process. */
399 if ((error = uiomove(buffer, length, uio)) != 0)
400 break;
401 }
402
403 return (error);
404 }
405
406 int
407 uhidwrite(dev, uio, flag)
408 dev_t dev;
409 struct uio *uio;
410 int flag;
411 {
412 int error;
413 int size;
414 usbd_status r;
415 USB_GET_SC(uhid, UHIDUNIT(dev), sc);
416
417 if (sc->sc_disconnected)
418 return (EIO);
419
420 DPRINTFN(1, ("uhidwrite\n"));
421
422 size = sc->sc_osize;
423 error = 0;
424 while (uio->uio_resid > 0) {
425 if (uio->uio_resid != size)
426 return (0);
427 if ((error = uiomove(sc->sc_obuf, size, uio)) != 0)
428 break;
429 if (sc->sc_oid)
430 r = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
431 sc->sc_obuf[0],
432 sc->sc_obuf+1, size-1);
433 else
434 r = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
435 0, sc->sc_obuf, size);
436 if (r != USBD_NORMAL_COMPLETION) {
437 error = EIO;
438 break;
439 }
440 }
441 return (error);
442 }
443
444 int
445 uhidioctl(dev, cmd, addr, flag, p)
446 dev_t dev;
447 u_long cmd;
448 caddr_t addr;
449 int flag;
450 struct proc *p;
451 {
452 struct usb_ctl_report_desc *rd;
453 struct usb_ctl_report *re;
454 int size, id;
455 usbd_status r;
456 USB_GET_SC(uhid, UHIDUNIT(dev), sc);
457
458 if (sc->sc_disconnected)
459 return (EIO);
460
461 DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
462 switch (cmd) {
463 case FIONBIO:
464 /* All handled in the upper FS layer. */
465 break;
466
467 case USB_GET_REPORT_DESC:
468 rd = (struct usb_ctl_report_desc *)addr;
469 size = min(sc->sc_repdesc_size, sizeof rd->data);
470 rd->size = size;
471 memcpy(rd->data, sc->sc_repdesc, size);
472 break;
473
474 case USB_SET_IMMED:
475 if (*(int *)addr) {
476 /* XXX should read into ibuf, but does it matter */
477 r = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
478 sc->sc_iid, sc->sc_ibuf,
479 sc->sc_isize);
480 if (r != USBD_NORMAL_COMPLETION)
481 return (EOPNOTSUPP);
482
483 sc->sc_state |= UHID_IMMED;
484 } else
485 sc->sc_state &= ~UHID_IMMED;
486 break;
487
488 case USB_GET_REPORT:
489 re = (struct usb_ctl_report *)addr;
490 switch (re->report) {
491 case UHID_INPUT_REPORT:
492 size = sc->sc_isize;
493 id = sc->sc_iid;
494 break;
495 case UHID_OUTPUT_REPORT:
496 size = sc->sc_osize;
497 id = sc->sc_oid;
498 break;
499 case UHID_FEATURE_REPORT:
500 size = sc->sc_fsize;
501 id = sc->sc_fid;
502 break;
503 default:
504 return (EINVAL);
505 }
506 r = usbd_get_report(sc->sc_iface, re->report, id,
507 re->data, size);
508 if (r != USBD_NORMAL_COMPLETION)
509 return (EIO);
510 break;
511
512 default:
513 return (EINVAL);
514 }
515 return (0);
516 }
517
518 int
519 uhidpoll(dev, events, p)
520 dev_t dev;
521 int events;
522 struct proc *p;
523 {
524 int revents = 0;
525 int s;
526 USB_GET_SC(uhid, UHIDUNIT(dev), sc);
527
528 if (sc->sc_disconnected)
529 return (EIO);
530
531 s = splusb();
532 if (events & (POLLOUT | POLLWRNORM))
533 revents |= events & (POLLOUT | POLLWRNORM);
534 if (events & (POLLIN | POLLRDNORM)) {
535 if (sc->sc_q.c_cc > 0)
536 revents |= events & (POLLIN | POLLRDNORM);
537 else
538 selrecord(p, &sc->sc_rsel);
539 }
540
541 splx(s);
542 return (revents);
543 }
544
545 #if defined(__FreeBSD__)
546 DRIVER_MODULE(uhid, usb, uhid_driver, uhid_devclass, usb_driver_load, 0);
547 #endif
548