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