ums.c revision 1.26 1 /* $NetBSD: ums.c,v 1.26 1999/08/14 14:49:32 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) logprintf x
82 #define DPRINTFN(n,x) if (umsdebug>(n)) logprintf 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
112 int flags; /* device configuration */
113 #define UMS_Z 0x01 /* z direction available */
114 int nbuttons;
115 #define MAX_BUTTONS 7 /* chosen because sc_buttons is u_char */
116
117 char revz; /* Z-axis is reversed */
118
119 #if defined(__NetBSD__)
120 u_char sc_buttons; /* mouse button status */
121 struct device *sc_wsmousedev;
122 #elif defined(__FreeBSD__)
123 u_char qbuf[QUEUE_BUFSIZE];
124 u_char dummy[100]; /* XXX just for safety and for now */
125 int qcount, qhead, qtail;
126 mousehw_t hw;
127 mousemode_t mode;
128 mousestatus_t status;
129
130 int state;
131 # define UMS_ASLEEP 0x01 /* readFromDevice is waiting */
132 # define UMS_SELECT 0x02 /* select is waiting */
133 struct selinfo rsel; /* process waiting in select */
134 #endif
135 };
136
137 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
138 #define MOUSE_FLAGS (HIO_RELATIVE)
139
140 void ums_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
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_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 sc->revz = (usbd_get_quirks(uaa->device)->uq_flags & UQ_MS_REVZ) != 0;
248
249 r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
250 if (r != USBD_NORMAL_COMPLETION)
251 USB_ATTACH_ERROR_RETURN;
252
253 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
254 hid_input, &sc->sc_loc_x, &flags)) {
255 printf("%s: mouse has no X report\n", USBDEVNAME(sc->sc_dev));
256 USB_ATTACH_ERROR_RETURN;
257 }
258 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
259 printf("%s: X report 0x%04x not supported\n",
260 USBDEVNAME(sc->sc_dev), flags);
261 USB_ATTACH_ERROR_RETURN;
262 }
263
264 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
265 hid_input, &sc->sc_loc_y, &flags)) {
266 printf("%s: mouse has no Y report\n", USBDEVNAME(sc->sc_dev));
267 USB_ATTACH_ERROR_RETURN;
268 }
269 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
270 printf("%s: Y report 0x%04x not supported\n",
271 USBDEVNAME(sc->sc_dev), flags);
272 USB_ATTACH_ERROR_RETURN;
273 }
274
275 /* Try to guess the Z activator: first check Z, then WHEEL. */
276 if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
277 hid_input, &sc->sc_loc_z, &flags) ||
278 hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
279 hid_input, &sc->sc_loc_z, &flags)) {
280 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
281 sc->sc_loc_z.size = 0; /* Bad Z coord, ignore it */
282 } else {
283 sc->flags |= UMS_Z;
284 }
285 }
286
287 /* figure out the number of buttons */
288 for (i = 1; i <= MAX_BUTTONS; i++)
289 if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
290 hid_input, &loc_btn, 0))
291 break;
292 sc->nbuttons = i - 1;
293 sc->sc_loc_btn = malloc(sizeof(struct hid_location)*sc->nbuttons,
294 M_USBDEV, M_NOWAIT);
295 if (!sc->sc_loc_btn) {
296 printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
297 USB_ATTACH_ERROR_RETURN;
298 }
299
300 printf("%s: %d buttons%s\n", USBDEVNAME(sc->sc_dev),
301 sc->nbuttons, sc->flags & UMS_Z ? " and Z dir." : "");
302
303 for (i = 1; i <= sc->nbuttons; i++)
304 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
305 hid_input, &sc->sc_loc_btn[i-1], 0);
306
307 sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
308 sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_NOWAIT);
309 if (!sc->sc_ibuf) {
310 printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
311 free(sc->sc_loc_btn, M_USBDEV);
312 USB_ATTACH_ERROR_RETURN;
313 }
314
315 sc->sc_ep_addr = ed->bEndpointAddress;
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 int
374 ums_activate(self, act)
375 struct device *self;
376 enum devact act;
377 {
378 return (0);
379 }
380
381 int
382 ums_detach(self, flags)
383 struct device *self;
384 int flags;
385 {
386 struct ums_softc *sc = (struct ums_softc *)self;
387 int rv = 0;
388
389 DPRINTF(("ums_detach: sc=%p flags=%d\n", sc, flags));
390 /* No need to do reference counting of ums, wsmouse has all the goo. */
391 if (sc->sc_wsmousedev)
392 rv = config_detach(sc->sc_wsmousedev, flags);
393 if (rv == 0) {
394 free(sc->sc_loc_btn, M_USBDEV);
395 free(sc->sc_ibuf, M_USBDEV);
396 }
397 return (rv);
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 int s;
412
413 #if defined(__NetBSD__)
414 #define UMS_BUT(i) ((i) == 1 || (i) == 2 ? 3 - (i) : i)
415 #elif defined(__FreeBSD__)
416 #define UMS_BUT(i) (i)
417 #endif
418
419 DPRINTFN(5, ("ums_intr: sc=%p status=%d\n", sc, status));
420 DPRINTFN(5, ("ums_intr: data = %02x %02x %02x\n",
421 sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
422
423 if (status == USBD_CANCELLED)
424 return;
425
426 if (status != USBD_NORMAL_COMPLETION) {
427 DPRINTF(("ums_intr: status=%d\n", status));
428 usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
429 return;
430 }
431
432 ibuf = sc->sc_ibuf;
433 if (sc->sc_iid) {
434 if (*ibuf++ != sc->sc_iid)
435 return;
436 }
437 dx = hid_get_data(ibuf, &sc->sc_loc_x);
438 dy = -hid_get_data(ibuf, &sc->sc_loc_y);
439 dz = hid_get_data(ibuf, &sc->sc_loc_z);
440 if (sc->revz)
441 dz = -dz;
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 s = spltty();
453 wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz);
454 splx(s);
455 }
456 #elif defined(__FreeBSD__)
457 if (dx || dy || dz || buttons != sc->status.button) {
458 DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d buttons:0x%x\n",
459 dx, dy, dz, buttons));
460
461 sc->status.button = buttons;
462 sc->status.dx += dx;
463 sc->status.dy += dy;
464 sc->status.dz += dz;
465
466 /* Discard data in case of full buffer */
467 if (sc->qcount == sizeof(sc->qbuf)) {
468 DPRINTF(("Buffer full, discarded packet"));
469 return;
470 }
471
472 sc->qbuf[sc->qhead] = MOUSE_PS2_SYNC;
473 if (dx < 0)
474 sc->qbuf[sc->qhead] |= MOUSE_PS2_XNEG;
475 if (dx > 255 || dx < -255)
476 sc->qbuf[sc->qhead] |= MOUSE_PS2_XOVERFLOW;
477 if (dy < 0)
478 sc->qbuf[sc->qhead] |= MOUSE_PS2_YNEG;
479 if (dy > 255 || dy < -255)
480 sc->qbuf[sc->qhead] |= MOUSE_PS2_YOVERFLOW;
481 sc->qbuf[sc->qhead++] |= buttons;
482 sc->qbuf[sc->qhead++] = dx;
483 sc->qbuf[sc->qhead++] = dy;
484 sc->qcount += 3;
485 if (sc->flags & UMS_Z) {
486 sc->qbuf[sc->qhead++] = dz;
487 sc->qcount++;
488 }
489 #ifdef USB_DEBUG
490 if (sc->qhead > sizeof(sc->qbuf))
491 DPRINTF(("Buffer overrun! %d %d\n",
492 sc->qhead, sizeof(sc->qbuf)));
493 #endif
494 /* wrap round at end of buffer */
495 if (sc->qhead >= sizeof(sc->qbuf))
496 sc->qhead = 0;
497
498 /* someone waiting for data */
499 if (sc->state & UMS_ASLEEP)
500 wakeup(sc);
501 /* wake up any pending selects */
502 selwakeup(&sc->rsel);
503 sc->state &= ~UMS_SELECT;
504 #endif
505 }
506 }
507
508 static int
509 ums_enable(v)
510 void *v;
511 {
512 struct ums_softc *sc = v;
513
514 usbd_status r;
515
516 DPRINTFN(1,("ums_enable: sc=%p\n", sc));
517 if (sc->sc_enabled)
518 return EBUSY;
519
520 sc->sc_enabled = 1;
521 #if defined(__NetBSD__)
522 sc->sc_buttons = 0;
523 #elif defined(__FreeBSD__)
524 sc->qcount = 0;
525 sc->qhead = sc->qtail = 0;
526 #ifdef USB_DEBUG
527 if (sizeof(sc->qbuf) % 4 || sizeof(sc->qbuf) % 3) {
528 DPRINTF(("Buffer size not divisible by 3 or 4\n"));
529 return ENXIO;
530 }
531 #endif
532 sc->status.flags = 0;
533 sc->status.button = sc->status.obutton = 0;
534 sc->status.dx = sc->status.dy = sc->status.dz = 0;
535 #endif
536
537 /* Set up interrupt pipe. */
538 r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
539 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
540 sc->sc_ibuf, sc->sc_isize, ums_intr);
541 if (r != USBD_NORMAL_COMPLETION) {
542 DPRINTF(("ums_enable: usbd_open_pipe_intr failed, error=%d\n",
543 r));
544 sc->sc_enabled = 0;
545 return (EIO);
546 }
547 return (0);
548 }
549
550 static void
551 ums_disable(v)
552 void *v;
553 {
554 struct ums_softc *sc = v;
555
556 DPRINTFN(1,("ums_disable: sc=%p\n", sc));
557 #ifdef DIAGNOSTIC
558 if (!sc->sc_enabled) {
559 printf("ums_disable: not enabled\n");
560 return;
561 }
562 #endif
563
564 /* Disable interrupts. */
565 usbd_abort_pipe(sc->sc_intrpipe);
566 usbd_close_pipe(sc->sc_intrpipe);
567
568 sc->sc_enabled = 0;
569
570 #if defined(USBVERBOSE) && defined(__FreeBSD__)
571 if (sc->qcount != 0)
572 DPRINTF(("Discarded %d bytes in queue\n", sc->qcount));
573 #endif
574 }
575
576 #if defined(__NetBSD__)
577 static int
578 ums_ioctl(v, cmd, data, flag, p)
579 void *v;
580 u_long cmd;
581 caddr_t data;
582 int flag;
583 struct proc *p;
584
585 {
586 switch (cmd) {
587 case WSMOUSEIO_GTYPE:
588 *(u_int *)data = WSMOUSE_TYPE_USB;
589 return (0);
590 }
591
592 return (-1);
593 }
594
595 #elif defined(__FreeBSD__)
596 static int
597 ums_open(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 open"));
603 return EINVAL;
604 }
605
606 return ums_enable(sc);
607 }
608
609 static int
610 ums_close(dev_t dev, int flag, int fmt, struct proc *p)
611 {
612 struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
613
614 if (!sc) {
615 DPRINTF(("sc not found at close"));
616 return EINVAL;
617 }
618
619 if (sc->sc_enabled)
620 ums_disable(sc);
621 return 0;
622 }
623
624 static int
625 ums_read(dev_t dev, struct uio *uio, int flag)
626 {
627 struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
628 int s;
629 char buf[sizeof(sc->qbuf)];
630 int l = 0;
631 int error;
632
633 if (!sc || !sc->sc_enabled) {
634 DPRINTF(("sc not found at read"));
635 return EINVAL;
636 }
637
638 s = splusb();
639 while (sc->qcount == 0 ) {
640 /* NWH XXX non blocking I/O ??
641 if (non blocking I/O ) {
642 splx(s);
643 return EWOULDBLOCK;
644 } else {
645 */
646 sc->state |= UMS_ASLEEP;
647 error = tsleep(sc, PZERO | PCATCH, "umsrea", 0);
648 sc->state &= ~UMS_ASLEEP;
649 if (error) {
650 splx(s);
651 return error;
652 }
653 }
654
655 while ((sc->qcount > 0) && (uio->uio_resid > 0)) {
656 l = (sc->qcount < uio->uio_resid? sc->qcount:uio->uio_resid);
657 if (l > sizeof(buf))
658 l = sizeof(buf);
659 if (l > sizeof(sc->qbuf) - sc->qtail) /* transfer till end of buf */
660 l = sizeof(sc->qbuf) - sc->qtail;
661
662 splx(s);
663 uiomove(&sc->qbuf[sc->qtail], l, uio);
664 s = splusb();
665
666 if ( sc->qcount - l < 0 ) {
667 DPRINTF(("qcount below 0, count=%d l=%d\n", sc->qcount, l));
668 sc->qcount = l;
669 }
670 sc->qcount -= l; /* remove the bytes from the buffer */
671 sc->qtail = (sc->qtail + l) % sizeof(sc->qbuf);
672 }
673 splx(s);
674
675 return 0;
676 }
677
678 static int
679 ums_poll(dev_t dev, int events, struct proc *p)
680 {
681 struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
682 int revents = 0;
683 int s;
684
685 if (!sc) {
686 DPRINTF(("sc not found at poll"));
687 return 0; /* just to make sure */
688 }
689
690 s = splusb();
691 if (events & (POLLIN | POLLRDNORM))
692 if (sc->qcount) {
693 revents = events & (POLLIN | POLLRDNORM);
694 } else {
695 sc->state |= UMS_SELECT;
696 selrecord(p, &sc->rsel);
697 }
698 splx(s);
699
700 return revents;
701 }
702
703 int
704 ums_ioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
705 {
706 struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
707 int error = 0;
708 int s;
709
710 if (!sc) {
711 DPRINTF(("sc not found at ioctl"));
712 return (ENOENT);
713 }
714
715 switch(cmd) {
716 case MOUSE_GETHWINFO:
717 *(mousehw_t *)addr = sc->hw;
718 break;
719 case MOUSE_GETMODE:
720 *(mousemode_t *)addr = sc->mode;
721 break;
722 case MOUSE_GETLEVEL:
723 *(int *)addr = sc->mode.level;
724 break;
725 case MOUSE_GETSTATUS: {
726 mousestatus_t *status = (mousestatus_t *) addr;
727
728 s = splusb();
729 *status = sc->status;
730 sc->status.obutton = sc->status.button;
731 sc->status.button = 0;
732 sc->status.dx = sc->status.dy = sc->status.dz = 0;
733 splx(s);
734
735 if (status->dx || status->dy || status->dz)
736 status->flags |= MOUSE_POSCHANGED;
737 if (status->button != status->obutton)
738 status->flags |= MOUSE_BUTTONSCHANGED;
739 break;
740 }
741 default:
742 error = ENOTTY;
743 }
744
745 return error;
746 }
747 #endif
748
749 #if defined(__FreeBSD__)
750 CDEV_DRIVER_MODULE(ums, usb, ums_driver, ums_devclass,
751 UMS_CDEV_MAJOR, ums_cdevsw, usbd_driver_load, 0);
752 #endif
753