ums.c revision 1.22 1 /* $NetBSD: ums.c,v 1.22 1999/01/12 22:06:48 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__)
49 #include <sys/device.h>
50 #include <sys/ioctl.h>
51 #elif defined(__FreeBSD__)
52 #include <sys/module.h>
53 #include <sys/bus.h>
54 #include <sys/ioccom.h>
55 #include <sys/conf.h>
56 #endif
57 #include <sys/tty.h>
58 #include <sys/file.h>
59 #include <sys/select.h>
60 #include <sys/proc.h>
61 #include <sys/vnode.h>
62 #include <sys/poll.h>
63
64 #include <dev/usb/usb.h>
65 #include <dev/usb/usbhid.h>
66
67 #include <dev/usb/usbdi.h>
68 #include <dev/usb/usbdi_util.h>
69 #include <dev/usb/usbdevs.h>
70 #include <dev/usb/usb_quirks.h>
71 #include <dev/usb/hid.h>
72
73 #if defined(__NetBSD__)
74 #include <dev/wscons/wsconsio.h>
75 #include <dev/wscons/wsmousevar.h>
76 #elif defined(__FreeBSD__)
77 #include <machine/mouse.h>
78 #endif
79
80 #ifdef USB_DEBUG
81 #define DPRINTF(x) if (umsdebug) printf x
82 #define DPRINTFN(n,x) if (umsdebug>(n)) printf x
83 int umsdebug = 0;
84 #else
85 #define DPRINTF(x)
86 #define DPRINTFN(n,x)
87 #endif
88
89 #define UMSUNIT(s) (minor(s)&0x1f)
90
91 #define PS2LBUTMASK x01
92 #define PS2RBUTMASK x02
93 #define PS2MBUTMASK x04
94 #define PS2BUTMASK 0x0f
95
96 #define QUEUE_BUFSIZE 240 /* MUST be divisible by 3 _and_ 4 */
97
98 struct ums_softc {
99 bdevice sc_dev; /* base device */
100 usbd_interface_handle sc_iface; /* interface */
101 usbd_pipe_handle sc_intrpipe; /* interrupt pipe */
102 int sc_ep_addr;
103
104 u_char *sc_ibuf;
105 u_int8_t sc_iid;
106 int sc_isize;
107 struct hid_location sc_loc_x, sc_loc_y, sc_loc_z;
108 struct hid_location *sc_loc_btn;
109
110 int sc_enabled;
111 int sc_disconnected; /* device is gone */
112
113 int flags; /* device configuration */
114 #define UMS_Z 0x01 /* z direction available */
115 int nbuttons;
116 #define MAX_BUTTONS 7 /* chosen because sc_buttons is u_char */
117
118 #if defined(__NetBSD__)
119 u_char sc_buttons; /* mouse button status */
120 struct device *sc_wsmousedev;
121 #elif defined(__FreeBSD__)
122 u_char qbuf[QUEUE_BUFSIZE];
123 u_char dummy[100]; /* XXX just for safety and for now */
124 int qcount, qhead, qtail;
125 mousehw_t hw;
126 mousemode_t mode;
127 mousestatus_t status;
128
129 int state;
130 # define UMS_ASLEEP 0x01 /* readFromDevice is waiting */
131 # define UMS_SELECT 0x02 /* select is waiting */
132 struct selinfo rsel; /* process waiting in select */
133 #endif
134 };
135
136 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
137 #define MOUSE_FLAGS (HIO_RELATIVE)
138
139 void ums_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
140 void ums_disco __P((void *));
141
142 static int ums_enable __P((void *));
143 static void ums_disable __P((void *));
144
145 #if defined(__NetBSD__)
146 static int ums_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
147
148 const struct wsmouse_accessops ums_accessops = {
149 ums_enable,
150 ums_ioctl,
151 ums_disable,
152 };
153
154 #elif defined(__FreeBSD__)
155 static d_open_t ums_open;
156 static d_close_t ums_close;
157 static d_read_t ums_read;
158 static d_ioctl_t ums_ioctl;
159 static d_poll_t ums_poll;
160
161 #define UMS_CDEV_MAJOR 111
162
163 static struct cdevsw ums_cdevsw = {
164 ums_open, ums_close, ums_read, nowrite,
165 ums_ioctl, nostop, nullreset, nodevtotty,
166 ums_poll, nommap,
167 NULL, "ums", NULL, -1
168 };
169 #endif
170
171 USB_DECLARE_DRIVER(ums);
172
173 USB_MATCH(ums)
174 {
175 USB_MATCH_START(ums, uaa);
176 usb_interface_descriptor_t *id;
177 int size, ret;
178 void *desc;
179 usbd_status r;
180
181 if (!uaa->iface)
182 return (UMATCH_NONE);
183 id = usbd_get_interface_descriptor(uaa->iface);
184 if (!id || id->bInterfaceClass != UCLASS_HID)
185 return (UMATCH_NONE);
186
187 r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
188 if (r != USBD_NORMAL_COMPLETION)
189 return (UMATCH_NONE);
190
191 if (hid_is_collection(desc, size,
192 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
193 ret = UMATCH_IFACECLASS;
194 else
195 ret = UMATCH_NONE;
196
197 free(desc, M_TEMP);
198 return (ret);
199 }
200
201 USB_ATTACH(ums)
202 {
203 USB_ATTACH_START(ums, sc, uaa);
204 usbd_interface_handle iface = uaa->iface;
205 usb_interface_descriptor_t *id;
206 usb_endpoint_descriptor_t *ed;
207 #if defined(__NetBSD__)
208 struct wsmousedev_attach_args a;
209 #endif
210 int size;
211 void *desc;
212 usbd_status r;
213 char devinfo[1024];
214 u_int32_t flags;
215 int i;
216 struct hid_location loc_btn;
217
218 sc->sc_disconnected = 1;
219 sc->sc_iface = iface;
220 id = usbd_get_interface_descriptor(iface);
221 usbd_devinfo(uaa->device, 0, devinfo);
222 USB_ATTACH_SETUP;
223 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
224 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
225 ed = usbd_interface2endpoint_descriptor(iface, 0);
226 if (!ed) {
227 printf("%s: could not read endpoint descriptor\n",
228 USBDEVNAME(sc->sc_dev));
229 USB_ATTACH_ERROR_RETURN;
230 }
231
232 DPRINTFN(10,("ums_attach: bLength=%d bDescriptorType=%d "
233 "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
234 " bInterval=%d\n",
235 ed->bLength, ed->bDescriptorType,
236 ed->bEndpointAddress & UE_ADDR,
237 ed->bEndpointAddress & UE_IN ? "in" : "out",
238 ed->bmAttributes & UE_XFERTYPE,
239 UGETW(ed->wMaxPacketSize), ed->bInterval));
240
241 if ((ed->bEndpointAddress & UE_IN) != UE_IN ||
242 (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
243 printf("%s: unexpected endpoint\n",
244 USBDEVNAME(sc->sc_dev));
245 USB_ATTACH_ERROR_RETURN;
246 }
247
248 r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
249 if (r != USBD_NORMAL_COMPLETION)
250 USB_ATTACH_ERROR_RETURN;
251
252 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
253 hid_input, &sc->sc_loc_x, &flags)) {
254 printf("%s: mouse has no X report\n", USBDEVNAME(sc->sc_dev));
255 USB_ATTACH_ERROR_RETURN;
256 }
257 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
258 printf("%s: X report 0x%04x not supported\n",
259 USBDEVNAME(sc->sc_dev), flags);
260 USB_ATTACH_ERROR_RETURN;
261 }
262
263 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
264 hid_input, &sc->sc_loc_y, &flags)) {
265 printf("%s: mouse has no Y report\n", USBDEVNAME(sc->sc_dev));
266 USB_ATTACH_ERROR_RETURN;
267 }
268 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
269 printf("%s: Y report 0x%04x not supported\n",
270 USBDEVNAME(sc->sc_dev), flags);
271 USB_ATTACH_ERROR_RETURN;
272 }
273
274 /* Try to guess the Z activator: first check Z, then WHEEL. */
275 if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
276 hid_input, &sc->sc_loc_z, &flags) ||
277 hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
278 hid_input, &sc->sc_loc_z, &flags)) {
279 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
280 sc->sc_loc_z.size = 0; /* Bad Z coord, ignore it */
281 } else {
282 sc->flags |= UMS_Z;
283 }
284 }
285
286 /* figure out the number of buttons */
287 for (i = 1; i <= MAX_BUTTONS; i++)
288 if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
289 hid_input, &loc_btn, 0))
290 break;
291 sc->nbuttons = i - 1;
292 sc->sc_loc_btn = malloc(sizeof(struct hid_location)*sc->nbuttons,
293 M_USBDEV, M_NOWAIT);
294 if (!sc->sc_loc_btn) {
295 printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
296 USB_ATTACH_ERROR_RETURN;
297 }
298
299 printf("%s: %d buttons%s\n", USBDEVNAME(sc->sc_dev),
300 sc->nbuttons, sc->flags & UMS_Z ? " and Z dir." : "");
301
302 for (i = 1; i <= sc->nbuttons; i++)
303 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
304 hid_input, &sc->sc_loc_btn[i-1], 0);
305
306 sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
307 sc->sc_ibuf = malloc(sc->sc_isize, M_USB, M_NOWAIT);
308 if (!sc->sc_ibuf) {
309 printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
310 free(sc->sc_loc_btn, M_USB);
311 USB_ATTACH_ERROR_RETURN;
312 }
313
314 sc->sc_ep_addr = ed->bEndpointAddress;
315 sc->sc_disconnected = 0;
316 free(desc, M_TEMP);
317
318 #ifdef USB_DEBUG
319 DPRINTF(("ums_attach: sc=%p\n", sc));
320 DPRINTF(("ums_attach: X\t%d/%d\n",
321 sc->sc_loc_x.pos, sc->sc_loc_x.size));
322 DPRINTF(("ums_attach: Y\t%d/%d\n",
323 sc->sc_loc_x.pos, sc->sc_loc_x.size));
324 if (sc->flags & UMS_Z)
325 DPRINTF(("ums_attach: Z\t%d/%d\n",
326 sc->sc_loc_z.pos, sc->sc_loc_z.size));
327 for (i = 1; i <= sc->nbuttons; i++) {
328 DPRINTF(("ums_attach: B%d\t%d/%d\n",
329 i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));
330 }
331 DPRINTF(("ums_attach: size=%d, id=%d\n", sc->sc_isize, sc->sc_iid));
332 #endif
333
334 #if defined(__NetBSD__)
335 a.accessops = &ums_accessops;
336 a.accesscookie = sc;
337
338 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
339 #elif defined(__FreeBSD__)
340 sc->hw.buttons = 2; /* XXX hw&mode values are bogus */
341 sc->hw.iftype = MOUSE_IF_PS2;
342 sc->hw.type = MOUSE_MOUSE;
343 if (sc->flags & UMS_Z)
344 sc->hw.model = MOUSE_MODEL_INTELLI;
345 else
346 sc->hw.model = MOUSE_MODEL_GENERIC;
347 sc->hw.hwid = 0;
348 sc->mode.protocol = MOUSE_PROTO_PS2;
349 sc->mode.rate = -1;
350 sc->mode.resolution = MOUSE_RES_DEFAULT;
351 sc->mode.accelfactor = 1;
352 sc->mode.level = 0;
353 if (sc->flags & UMS_Z) {
354 sc->mode.packetsize = MOUSE_INTELLI_PACKETSIZE;
355 sc->mode.syncmask[0] = 0xc8;
356 } else {
357 sc->mode.packetsize = MOUSE_PS2_PACKETSIZE;
358 sc->mode.syncmask[0] = 0xc0;
359 }
360 sc->mode.syncmask[1] = 0;
361
362 sc->status.flags = 0;
363 sc->status.button = sc->status.obutton = 0;
364 sc->status.dx = sc->status.dy = sc->status.dz = 0;
365
366 sc->rsel.si_flags = 0;
367 sc->rsel.si_pid = 0;
368 #endif
369
370 USB_ATTACH_SUCCESS_RETURN;
371 }
372
373
374 #if defined(__FreeBSD__)
375 static int
376 ums_detach(device_t self)
377 {
378 struct ums_softc *sc = device_get_softc(self);
379 char *devinfo = (char *) device_get_desc(self);
380
381 if (devinfo) {
382 device_set_desc(self, NULL);
383 free(devinfo, M_USB);
384 }
385 free(sc->sc_loc_btn, M_USB);
386 free(sc->sc_ibuf, M_USB);
387
388 return 0;
389 }
390 #endif
391
392 void
393 ums_disco(p)
394 void *p;
395 {
396 struct ums_softc *sc = p;
397
398 DPRINTF(("ums_disco: sc=%p\n", sc));
399 usbd_abort_pipe(sc->sc_intrpipe);
400 sc->sc_disconnected = 1;
401 }
402
403 void
404 ums_intr(reqh, addr, status)
405 usbd_request_handle reqh;
406 usbd_private_handle addr;
407 usbd_status status;
408 {
409 struct ums_softc *sc = addr;
410 u_char *ibuf;
411 int dx, dy, dz;
412 u_char buttons = 0;
413 int i;
414
415 #if defined(__NetBSD__)
416 #define UMS_BUT(i) ((i) == 1 || (i) == 2 ? 3 - (i) : i)
417 #elif defined(__FreeBSD__)
418 #define UMS_BUT(i) (i)
419 #endif
420
421 DPRINTFN(5, ("ums_intr: sc=%p status=%d\n", sc, status));
422 DPRINTFN(5, ("ums_intr: data = %02x %02x %02x\n",
423 sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
424
425 if (status == USBD_CANCELLED)
426 return;
427
428 if (status != USBD_NORMAL_COMPLETION) {
429 DPRINTF(("ums_intr: status=%d\n", status));
430 usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
431 return;
432 }
433
434 ibuf = sc->sc_ibuf;
435 if (sc->sc_iid) {
436 if (*ibuf++ != sc->sc_iid)
437 return;
438 }
439 dx = hid_get_data(ibuf, &sc->sc_loc_x);
440 dy = -hid_get_data(ibuf, &sc->sc_loc_y);
441 dz = hid_get_data(ibuf, &sc->sc_loc_z);
442 for (i = 0; i < sc->nbuttons; i++)
443 if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
444 buttons |= (1 << UMS_BUT(i));
445
446 #if defined(__NetBSD__)
447 if (dx || dy || dz || buttons != sc->sc_buttons) {
448 DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d buttons:0x%x\n",
449 dx, dy, dz, buttons));
450 sc->sc_buttons = buttons;
451 if (sc->sc_wsmousedev)
452 wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz);
453 #elif defined(__FreeBSD__)
454 if (dx || dy || dz || buttons != sc->status.button) {
455 DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d buttons:0x%x\n",
456 dx, dy, dz, buttons));
457
458 sc->status.button = buttons;
459 sc->status.dx += dx;
460 sc->status.dy += dy;
461 sc->status.dz += dz;
462
463 /* Discard data in case of full buffer */
464 if (sc->qcount == sizeof(sc->qbuf)) {
465 DPRINTF(("Buffer full, discarded packet"));
466 return;
467 }
468
469 sc->qbuf[sc->qhead] = MOUSE_PS2_SYNC;
470 if (dx < 0)
471 sc->qbuf[sc->qhead] |= MOUSE_PS2_XNEG;
472 if (dx > 255 || dx < -255)
473 sc->qbuf[sc->qhead] |= MOUSE_PS2_XOVERFLOW;
474 if (dy < 0)
475 sc->qbuf[sc->qhead] |= MOUSE_PS2_YNEG;
476 if (dy > 255 || dy < -255)
477 sc->qbuf[sc->qhead] |= MOUSE_PS2_YOVERFLOW;
478 sc->qbuf[sc->qhead++] |= buttons;
479 sc->qbuf[sc->qhead++] = dx;
480 sc->qbuf[sc->qhead++] = dy;
481 sc->qcount += 3;
482 if (sc->flags & UMS_Z) {
483 sc->qbuf[sc->qhead++] = dz;
484 sc->qcount++;
485 }
486 #ifdef USB_DEBUG
487 if (sc->qhead > sizeof(sc->qbuf))
488 DPRINTF(("Buffer overrun! %d %d\n",
489 sc->qhead, sizeof(sc->qbuf)));
490 #endif
491 /* wrap round at end of buffer */
492 if (sc->qhead >= sizeof(sc->qbuf))
493 sc->qhead = 0;
494
495 /* someone waiting for data */
496 if (sc->state & UMS_ASLEEP)
497 wakeup(sc);
498 /* wake up any pending selects */
499 selwakeup(&sc->rsel);
500 sc->state &= ~UMS_SELECT;
501 #endif
502 }
503 }
504
505
506 static int
507 ums_enable(v)
508 void *v;
509 {
510 struct ums_softc *sc = v;
511
512 usbd_status r;
513
514 if (sc->sc_enabled)
515 return EBUSY;
516
517 sc->sc_enabled = 1;
518 #if defined(__NetBSD__)
519 sc->sc_buttons = 0;
520 #elif defined(__FreeBSD__)
521 sc->qcount = 0;
522 sc->qhead = sc->qtail = 0;
523 #ifdef USB_DEBUG
524 if (sizeof(sc->qbuf) % 4 || sizeof(sc->qbuf) % 3) {
525 DPRINTF(("Buffer size not divisible by 3 or 4\n"));
526 return ENXIO;
527 }
528 #endif
529 sc->status.flags = 0;
530 sc->status.button = sc->status.obutton = 0;
531 sc->status.dx = sc->status.dy = sc->status.dz = 0;
532 #endif
533
534 /* Set up interrupt pipe. */
535 r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
536 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
537 sc->sc_ibuf, sc->sc_isize, ums_intr);
538 if (r != USBD_NORMAL_COMPLETION) {
539 DPRINTF(("ums_enable: usbd_open_pipe_intr failed, error=%d\n",
540 r));
541 sc->sc_enabled = 0;
542 return (EIO);
543 }
544 usbd_set_disco(sc->sc_intrpipe, ums_disco, sc);
545 return (0);
546 }
547
548 static void
549 ums_disable(v)
550 void *v;
551 {
552 struct ums_softc *sc = v;
553
554 /* Disable interrupts. */
555 usbd_abort_pipe(sc->sc_intrpipe);
556 usbd_close_pipe(sc->sc_intrpipe);
557
558 sc->sc_enabled = 0;
559
560 #if defined(USBVERBOSE) && defined(__FreeBSD__)
561 if (sc->qcount != 0)
562 DPRINTF(("Discarded %d bytes in queue\n", sc->qcount));
563 #endif
564 }
565
566 #if defined(__NetBSD__)
567 static int
568 ums_ioctl(v, cmd, data, flag, p)
569 void *v;
570 u_long cmd;
571 caddr_t data;
572 int flag;
573 struct proc *p;
574
575 {
576 switch (cmd) {
577 case WSMOUSEIO_GTYPE:
578 *(u_int *)data = WSMOUSE_TYPE_USB;
579 return (0);
580 }
581
582 return (-1);
583 }
584
585 #elif defined(__FreeBSD__)
586 static int
587 ums_open(dev_t dev, int flag, int fmt, struct proc *p)
588 {
589 struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
590
591 if (!sc) {
592 DPRINTF(("sc not found at open"));
593 return EINVAL;
594 }
595
596 return ums_enable(sc);
597 }
598
599 static int
600 ums_close(dev_t dev, int flag, int fmt, struct proc *p)
601 {
602 struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
603
604 if (!sc) {
605 DPRINTF(("sc not found at close"));
606 return EINVAL;
607 }
608
609 if (sc->sc_enabled)
610 ums_disable(sc);
611 return 0;
612 }
613
614 static int
615 ums_read(dev_t dev, struct uio *uio, int flag)
616 {
617 struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
618 int s;
619 char buf[sizeof(sc->qbuf)];
620 int l = 0;
621 int error;
622
623 if (!sc || !sc->sc_enabled) {
624 DPRINTF(("sc not found at read"));
625 return EINVAL;
626 }
627
628 s = splusb();
629 while (sc->qcount == 0 ) {
630 /* NWH XXX non blocking I/O ??
631 if (non blocking I/O ) {
632 splx(s);
633 return EWOULDBLOCK;
634 } else {
635 */
636 sc->state |= UMS_ASLEEP;
637 error = tsleep(sc, PZERO | PCATCH, "umsrea", 0);
638 sc->state &= ~UMS_ASLEEP;
639 if (error) {
640 splx(s);
641 return error;
642 }
643 }
644
645 while ((sc->qcount > 0) && (uio->uio_resid > 0)) {
646 l = (sc->qcount < uio->uio_resid? sc->qcount:uio->uio_resid);
647 if (l > sizeof(buf))
648 l = sizeof(buf);
649 if (l > sizeof(sc->qbuf) - sc->qtail) /* transfer till end of buf */
650 l = sizeof(sc->qbuf) - sc->qtail;
651
652 splx(s);
653 uiomove(&sc->qbuf[sc->qtail], l, uio);
654 s = splusb();
655
656 if ( sc->qcount - l < 0 ) {
657 DPRINTF(("qcount below 0, count=%d l=%d\n", sc->qcount, l));
658 sc->qcount = l;
659 }
660 sc->qcount -= l; /* remove the bytes from the buffer */
661 sc->qtail = (sc->qtail + l) % sizeof(sc->qbuf);
662 }
663 splx(s);
664
665 return 0;
666 }
667
668 static int
669 ums_poll(dev_t dev, int events, struct proc *p)
670 {
671 struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
672 int revents = 0;
673 int s;
674
675 if (!sc) {
676 DPRINTF(("sc not found at poll"));
677 return 0; /* just to make sure */
678 }
679
680 s = splusb();
681 if (events & (POLLIN | POLLRDNORM))
682 if (sc->qcount) {
683 revents = events & (POLLIN | POLLRDNORM);
684 } else {
685 sc->state |= UMS_SELECT;
686 selrecord(p, &sc->rsel);
687 }
688 splx(s);
689
690 return revents;
691 }
692
693 int
694 ums_ioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
695 {
696 struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
697 int error = 0;
698 int s;
699
700 if (!sc) {
701 DPRINTF(("sc not found at ioctl"));
702 return (ENOENT);
703 }
704
705 switch(cmd) {
706 case MOUSE_GETHWINFO:
707 *(mousehw_t *)addr = sc->hw;
708 break;
709 case MOUSE_GETMODE:
710 *(mousemode_t *)addr = sc->mode;
711 break;
712 case MOUSE_GETLEVEL:
713 *(int *)addr = sc->mode.level;
714 break;
715 case MOUSE_GETSTATUS: {
716 mousestatus_t *status = (mousestatus_t *) addr;
717
718 s = splusb();
719 *status = sc->status;
720 sc->status.obutton = sc->status.button;
721 sc->status.button = 0;
722 sc->status.dx = sc->status.dy = sc->status.dz = 0;
723 splx(s);
724
725 if (status->dx || status->dy || status->dz)
726 status->flags |= MOUSE_POSCHANGED;
727 if (status->button != status->obutton)
728 status->flags |= MOUSE_BUTTONSCHANGED;
729 break;
730 }
731 default:
732 error = ENOTTY;
733 }
734
735 return error;
736 }
737 #endif
738
739 #if defined(__FreeBSD__)
740 CDEV_DRIVER_MODULE(ums, usb, ums_driver, ums_devclass,
741 UMS_CDEV_MAJOR, ums_cdevsw, usbd_driver_load, 0);
742 #endif
743