ums.c revision 1.21 1 /* $NetBSD: ums.c,v 1.21 1999/01/10 19:13:15 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 USB_ATTACH_ERROR_RETURN;
296
297 printf("%s: %d buttons%s\n", USBDEVNAME(sc->sc_dev),
298 sc->nbuttons, (sc->flags & UMS_Z? " and Z dir." : ""));
299
300 for (i = 1; i <= sc->nbuttons; i++)
301 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
302 hid_input, &sc->sc_loc_btn[i-1], 0);
303
304 sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
305 sc->sc_ibuf = malloc(sc->sc_isize, M_USB, M_NOWAIT);
306 if (!sc->sc_ibuf) {
307 free(sc->sc_loc_btn, M_USB);
308 USB_ATTACH_ERROR_RETURN;
309 }
310
311 sc->sc_ep_addr = ed->bEndpointAddress;
312 sc->sc_disconnected = 0;
313 free(desc, M_TEMP);
314
315 #ifdef USB_DEBUG
316 DPRINTF(("ums_attach: sc=%p\n", sc));
317 DPRINTF(("ums_attach: X\t%d/%d\n",
318 sc->sc_loc_x.pos, sc->sc_loc_x.size));
319 DPRINTF(("ums_attach: Y\t%d/%d\n",
320 sc->sc_loc_x.pos, sc->sc_loc_x.size));
321 if (sc->flags & UMS_Z)
322 DPRINTF(("ums_attach: Z\t%d/%d\n",
323 sc->sc_loc_z.pos, sc->sc_loc_z.size));
324 for (i = 1; i <= sc->nbuttons; i++) {
325 DPRINTF(("ums_attach: B%d\t%d/%d\n",
326 i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));
327 }
328 DPRINTF(("ums_attach: size=%d, id=%d\n", sc->sc_isize, sc->sc_iid));
329 #endif
330
331 #if defined(__NetBSD__)
332 a.accessops = &ums_accessops;
333 a.accesscookie = sc;
334
335 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
336 #elif defined(__FreeBSD__)
337 sc->hw.buttons = 2; /* XXX hw&mode values are bogus */
338 sc->hw.iftype = MOUSE_IF_PS2;
339 sc->hw.type = MOUSE_MOUSE;
340 if (sc->flags & UMS_Z)
341 sc->hw.model = MOUSE_MODEL_INTELLI;
342 else
343 sc->hw.model = MOUSE_MODEL_GENERIC;
344 sc->hw.hwid = 0;
345 sc->mode.protocol = MOUSE_PROTO_PS2;
346 sc->mode.rate = -1;
347 sc->mode.resolution = MOUSE_RES_DEFAULT;
348 sc->mode.accelfactor = 1;
349 sc->mode.level = 0;
350 if (sc->flags & UMS_Z) {
351 sc->mode.packetsize = MOUSE_INTELLI_PACKETSIZE;
352 sc->mode.syncmask[0] = 0xc8;
353 } else {
354 sc->mode.packetsize = MOUSE_PS2_PACKETSIZE;
355 sc->mode.syncmask[0] = 0xc0;
356 }
357 sc->mode.syncmask[1] = 0;
358
359 sc->status.flags = 0;
360 sc->status.button = sc->status.obutton = 0;
361 sc->status.dx = sc->status.dy = sc->status.dz = 0;
362
363 sc->rsel.si_flags = 0;
364 sc->rsel.si_pid = 0;
365 #endif
366
367 USB_ATTACH_SUCCESS_RETURN;
368 }
369
370
371 #if defined(__FreeBSD__)
372 static int
373 ums_detach(device_t self)
374 {
375 struct ums_softc *sc = device_get_softc(self);
376 char *devinfo = (char *) device_get_desc(self);
377
378 if (devinfo) {
379 device_set_desc(self, NULL);
380 free(devinfo, M_USB);
381 }
382 free(sc->sc_loc_btn, M_USB);
383 free(sc->sc_ibuf, M_USB);
384
385 return 0;
386 }
387 #endif
388
389 void
390 ums_disco(p)
391 void *p;
392 {
393 struct ums_softc *sc = p;
394
395 DPRINTF(("ums_disco: sc=%p\n", sc));
396 usbd_abort_pipe(sc->sc_intrpipe);
397 sc->sc_disconnected = 1;
398 }
399
400 void
401 ums_intr(reqh, addr, status)
402 usbd_request_handle reqh;
403 usbd_private_handle addr;
404 usbd_status status;
405 {
406 struct ums_softc *sc = addr;
407 u_char *ibuf;
408 int dx, dy, dz;
409 u_char buttons = 0;
410 int i;
411
412 #if defined(__NetBSD__)
413 #define UMS_BUT(i) ((i) == 1 || (i) == 2 ? 3 - (i) : i)
414 #elif defined(__FreeBSD__)
415 #define UMS_BUT(i) (i)
416 #endif
417
418 DPRINTFN(5, ("ums_intr: sc=%p status=%d\n", sc, status));
419 DPRINTFN(5, ("ums_intr: data = %02x %02x %02x\n",
420 sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
421
422 if (status == USBD_CANCELLED)
423 return;
424
425 if (status != USBD_NORMAL_COMPLETION) {
426 DPRINTF(("ums_intr: status=%d\n", status));
427 usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
428 return;
429 }
430
431 ibuf = sc->sc_ibuf;
432 if (sc->sc_iid) {
433 if (*ibuf++ != sc->sc_iid)
434 return;
435 }
436 dx = hid_get_data(ibuf, &sc->sc_loc_x);
437 dy = -hid_get_data(ibuf, &sc->sc_loc_y);
438 dz = hid_get_data(ibuf, &sc->sc_loc_z);
439 for (i = 0; i < sc->nbuttons; i++)
440 if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
441 buttons |= (1 << UMS_BUT(i));
442
443 #if defined(__NetBSD__)
444 if (dx || dy || buttons != sc->sc_buttons) {
445 DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d buttons:0x%x\n",
446 dx, dy, dz, buttons));
447 sc->sc_buttons = buttons;
448 if (sc->sc_wsmousedev)
449 wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz);
450 #elif defined(__FreeBSD__)
451 if (dx || dy || buttons != sc->status.button) {
452 DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d buttons:0x%x\n",
453 dx, dy, dz, buttons));
454
455 sc->status.button = buttons;
456 sc->status.dx += dx;
457 sc->status.dy += dy;
458 sc->status.dz += dz;
459
460 /* Discard data in case of full buffer */
461 if (sc->qcount == sizeof(sc->qbuf)) {
462 DPRINTF(("Buffer full, discarded packet"));
463 return;
464 }
465
466 sc->qbuf[sc->qhead] = MOUSE_PS2_SYNC;
467 if (dx < 0)
468 sc->qbuf[sc->qhead] |= MOUSE_PS2_XNEG;
469 if (dx > 255 || dx < -255)
470 sc->qbuf[sc->qhead] |= MOUSE_PS2_XOVERFLOW;
471 if (dy < 0)
472 sc->qbuf[sc->qhead] |= MOUSE_PS2_YNEG;
473 if (dy > 255 || dy < -255)
474 sc->qbuf[sc->qhead] |= MOUSE_PS2_YOVERFLOW;
475 sc->qbuf[sc->qhead++] |= buttons;
476 sc->qbuf[sc->qhead++] = dx;
477 sc->qbuf[sc->qhead++] = dy;
478 sc->qcount += 3;
479 if (sc->flags & UMS_Z) {
480 sc->qbuf[sc->qhead++] = dz;
481 sc->qcount++;
482 }
483 #ifdef USB_DEBUG
484 if (sc->qhead > sizeof(sc->qbuf))
485 DPRINTF(("Buffer overrun! %d %d\n",
486 sc->qhead, sizeof(sc->qbuf)));
487 #endif
488 /* wrap round at end of buffer */
489 if (sc->qhead >= sizeof(sc->qbuf))
490 sc->qhead = 0;
491
492 /* someone waiting for data */
493 if (sc->state & UMS_ASLEEP)
494 wakeup(sc);
495 /* wake up any pending selects */
496 selwakeup(&sc->rsel);
497 sc->state &= ~UMS_SELECT;
498 #endif
499 }
500 }
501
502
503 static int
504 ums_enable(v)
505 void *v;
506 {
507 struct ums_softc *sc = v;
508
509 usbd_status r;
510
511 if (sc->sc_enabled)
512 return EBUSY;
513
514 sc->sc_enabled = 1;
515 #if defined(__NetBSD__)
516 sc->sc_buttons = 0;
517 #elif defined(__FreeBSD__)
518 sc->qcount = 0;
519 sc->qhead = sc->qtail = 0;
520 #ifdef USB_DEBUG
521 if (sizeof(sc->qbuf) % 4 || sizeof(sc->qbuf) % 3) {
522 DPRINTF(("Buffer size not divisible by 3 or 4\n"));
523 return ENXIO;
524 }
525 #endif
526 sc->status.flags = 0;
527 sc->status.button = sc->status.obutton = 0;
528 sc->status.dx = sc->status.dy = sc->status.dz = 0;
529 #endif
530
531 /* Set up interrupt pipe. */
532 r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
533 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
534 sc->sc_ibuf, sc->sc_isize, ums_intr);
535 if (r != USBD_NORMAL_COMPLETION) {
536 DPRINTF(("ums_enable: usbd_open_pipe_intr failed, error=%d\n",
537 r));
538 sc->sc_enabled = 0;
539 return (EIO);
540 }
541 usbd_set_disco(sc->sc_intrpipe, ums_disco, sc);
542 return (0);
543 }
544
545 static void
546 ums_disable(v)
547 void *v;
548 {
549 struct ums_softc *sc = v;
550
551 /* Disable interrupts. */
552 usbd_abort_pipe(sc->sc_intrpipe);
553 usbd_close_pipe(sc->sc_intrpipe);
554
555 sc->sc_enabled = 0;
556
557 #if defined(USBVERBOSE) && defined(__FreeBSD__)
558 if (sc->qcount != 0)
559 DPRINTF(("Discarded %d bytes in queue\n", sc->qcount));
560 #endif
561 }
562
563 #if defined(__NetBSD__)
564 static int
565 ums_ioctl(v, cmd, data, flag, p)
566 void *v;
567 u_long cmd;
568 caddr_t data;
569 int flag;
570 struct proc *p;
571
572 {
573 switch (cmd) {
574 case WSMOUSEIO_GTYPE:
575 *(u_int *)data = WSMOUSE_TYPE_USB;
576 return (0);
577 }
578
579 return (-1);
580 }
581
582 #elif defined(__FreeBSD__)
583 static int
584 ums_open(dev_t dev, int flag, int fmt, struct proc *p)
585 {
586 struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
587
588 if (!sc) {
589 DPRINTF(("sc not found at open"));
590 return EINVAL;
591 }
592
593 return ums_enable(sc);
594 }
595
596 static int
597 ums_close(dev_t dev, int flag, int fmt, struct proc *p)
598 {
599 struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
600
601 if (!sc) {
602 DPRINTF(("sc not found at close"));
603 return EINVAL;
604 }
605
606 if (sc->sc_enabled)
607 ums_disable(sc);
608 return 0;
609 }
610
611 static int
612 ums_read(dev_t dev, struct uio *uio, int flag)
613 {
614 struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
615 int s;
616 char buf[sizeof(sc->qbuf)];
617 int l = 0;
618 int error;
619
620 if (!sc || !sc->sc_enabled) {
621 DPRINTF(("sc not found at read"));
622 return EINVAL;
623 }
624
625 s = splusb();
626 while (sc->qcount == 0 ) {
627 /* NWH XXX non blocking I/O ??
628 if (non blocking I/O ) {
629 splx(s);
630 return EWOULDBLOCK;
631 } else {
632 */
633 sc->state |= UMS_ASLEEP;
634 error = tsleep(sc, PZERO | PCATCH, "umsrea", 0);
635 sc->state &= ~UMS_ASLEEP;
636 if (error) {
637 splx(s);
638 return error;
639 }
640 }
641
642 while ((sc->qcount > 0) && (uio->uio_resid > 0)) {
643 l = (sc->qcount < uio->uio_resid? sc->qcount:uio->uio_resid);
644 if (l > sizeof(buf))
645 l = sizeof(buf);
646 if (l > sizeof(sc->qbuf) - sc->qtail) /* transfer till end of buf */
647 l = sizeof(sc->qbuf) - sc->qtail;
648
649 splx(s);
650 uiomove(&sc->qbuf[sc->qtail], l, uio);
651 s = splusb();
652
653 if ( sc->qcount - l < 0 ) {
654 DPRINTF(("qcount below 0, count=%d l=%d\n", sc->qcount, l));
655 sc->qcount = l;
656 }
657 sc->qcount -= l; /* remove the bytes from the buffer */
658 sc->qtail = (sc->qtail + l) % sizeof(sc->qbuf);
659 }
660 splx(s);
661
662 return 0;
663 }
664
665 static int
666 ums_poll(dev_t dev, int events, struct proc *p)
667 {
668 struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
669 int revents = 0;
670 int s;
671
672 if (!sc) {
673 DPRINTF(("sc not found at poll"));
674 return 0; /* just to make sure */
675 }
676
677 s = splusb();
678 if (events & (POLLIN | POLLRDNORM))
679 if (sc->qcount) {
680 revents = events & (POLLIN | POLLRDNORM);
681 } else {
682 sc->state |= UMS_SELECT;
683 selrecord(p, &sc->rsel);
684 }
685 splx(s);
686
687 return revents;
688 }
689
690 int
691 ums_ioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
692 {
693 struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
694 int error = 0;
695 int s;
696
697 if (!sc) {
698 DPRINTF(("sc not found at ioctl"));
699 return (ENOENT);
700 }
701
702 switch(cmd) {
703 case MOUSE_GETHWINFO:
704 *(mousehw_t *)addr = sc->hw;
705 break;
706 case MOUSE_GETMODE:
707 *(mousemode_t *)addr = sc->mode;
708 break;
709 case MOUSE_GETLEVEL:
710 *(int *)addr = sc->mode.level;
711 break;
712 case MOUSE_GETSTATUS: {
713 mousestatus_t *status = (mousestatus_t *) addr;
714
715 s = splusb();
716 *status = sc->status;
717 sc->status.obutton = sc->status.button;
718 sc->status.button = 0;
719 sc->status.dx = sc->status.dy = sc->status.dz = 0;
720 splx(s);
721
722 if (status->dx || status->dy || status->dz)
723 status->flags |= MOUSE_POSCHANGED;
724 if (status->button != status->obutton)
725 status->flags |= MOUSE_BUTTONSCHANGED;
726 break;
727 }
728 default:
729 error = ENOTTY;
730 }
731
732 return error;
733 }
734 #endif
735
736 #if defined(__FreeBSD__)
737 CDEV_DRIVER_MODULE(ums, usb, ums_driver, ums_devclass,
738 UMS_CDEV_MAJOR, ums_cdevsw, usbd_driver_load, 0);
739 #endif
740