ums.c revision 1.23 1 /* $NetBSD: ums.c,v 1.23 1999/05/09 15:10:31 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 int s;
415
416 #if defined(__NetBSD__)
417 #define UMS_BUT(i) ((i) == 1 || (i) == 2 ? 3 - (i) : i)
418 #elif defined(__FreeBSD__)
419 #define UMS_BUT(i) (i)
420 #endif
421
422 DPRINTFN(5, ("ums_intr: sc=%p status=%d\n", sc, status));
423 DPRINTFN(5, ("ums_intr: data = %02x %02x %02x\n",
424 sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
425
426 if (status == USBD_CANCELLED)
427 return;
428
429 if (status != USBD_NORMAL_COMPLETION) {
430 DPRINTF(("ums_intr: status=%d\n", status));
431 usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
432 return;
433 }
434
435 ibuf = sc->sc_ibuf;
436 if (sc->sc_iid) {
437 if (*ibuf++ != sc->sc_iid)
438 return;
439 }
440 dx = hid_get_data(ibuf, &sc->sc_loc_x);
441 dy = -hid_get_data(ibuf, &sc->sc_loc_y);
442 dz = hid_get_data(ibuf, &sc->sc_loc_z);
443 for (i = 0; i < sc->nbuttons; i++)
444 if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
445 buttons |= (1 << UMS_BUT(i));
446
447 #if defined(__NetBSD__)
448 if (dx || dy || dz || buttons != sc->sc_buttons) {
449 DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d buttons:0x%x\n",
450 dx, dy, dz, buttons));
451 sc->sc_buttons = buttons;
452 if (sc->sc_wsmousedev) {
453 s = spltty();
454 wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz);
455 splx(s);
456 }
457 #elif defined(__FreeBSD__)
458 if (dx || dy || dz || buttons != sc->status.button) {
459 DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d buttons:0x%x\n",
460 dx, dy, dz, buttons));
461
462 sc->status.button = buttons;
463 sc->status.dx += dx;
464 sc->status.dy += dy;
465 sc->status.dz += dz;
466
467 /* Discard data in case of full buffer */
468 if (sc->qcount == sizeof(sc->qbuf)) {
469 DPRINTF(("Buffer full, discarded packet"));
470 return;
471 }
472
473 sc->qbuf[sc->qhead] = MOUSE_PS2_SYNC;
474 if (dx < 0)
475 sc->qbuf[sc->qhead] |= MOUSE_PS2_XNEG;
476 if (dx > 255 || dx < -255)
477 sc->qbuf[sc->qhead] |= MOUSE_PS2_XOVERFLOW;
478 if (dy < 0)
479 sc->qbuf[sc->qhead] |= MOUSE_PS2_YNEG;
480 if (dy > 255 || dy < -255)
481 sc->qbuf[sc->qhead] |= MOUSE_PS2_YOVERFLOW;
482 sc->qbuf[sc->qhead++] |= buttons;
483 sc->qbuf[sc->qhead++] = dx;
484 sc->qbuf[sc->qhead++] = dy;
485 sc->qcount += 3;
486 if (sc->flags & UMS_Z) {
487 sc->qbuf[sc->qhead++] = dz;
488 sc->qcount++;
489 }
490 #ifdef USB_DEBUG
491 if (sc->qhead > sizeof(sc->qbuf))
492 DPRINTF(("Buffer overrun! %d %d\n",
493 sc->qhead, sizeof(sc->qbuf)));
494 #endif
495 /* wrap round at end of buffer */
496 if (sc->qhead >= sizeof(sc->qbuf))
497 sc->qhead = 0;
498
499 /* someone waiting for data */
500 if (sc->state & UMS_ASLEEP)
501 wakeup(sc);
502 /* wake up any pending selects */
503 selwakeup(&sc->rsel);
504 sc->state &= ~UMS_SELECT;
505 #endif
506 }
507 }
508
509
510 static int
511 ums_enable(v)
512 void *v;
513 {
514 struct ums_softc *sc = v;
515
516 usbd_status r;
517
518 if (sc->sc_enabled)
519 return EBUSY;
520
521 sc->sc_enabled = 1;
522 #if defined(__NetBSD__)
523 sc->sc_buttons = 0;
524 #elif defined(__FreeBSD__)
525 sc->qcount = 0;
526 sc->qhead = sc->qtail = 0;
527 #ifdef USB_DEBUG
528 if (sizeof(sc->qbuf) % 4 || sizeof(sc->qbuf) % 3) {
529 DPRINTF(("Buffer size not divisible by 3 or 4\n"));
530 return ENXIO;
531 }
532 #endif
533 sc->status.flags = 0;
534 sc->status.button = sc->status.obutton = 0;
535 sc->status.dx = sc->status.dy = sc->status.dz = 0;
536 #endif
537
538 /* Set up interrupt pipe. */
539 r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
540 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
541 sc->sc_ibuf, sc->sc_isize, ums_intr);
542 if (r != USBD_NORMAL_COMPLETION) {
543 DPRINTF(("ums_enable: usbd_open_pipe_intr failed, error=%d\n",
544 r));
545 sc->sc_enabled = 0;
546 return (EIO);
547 }
548 usbd_set_disco(sc->sc_intrpipe, ums_disco, sc);
549 return (0);
550 }
551
552 static void
553 ums_disable(v)
554 void *v;
555 {
556 struct ums_softc *sc = v;
557
558 /* Disable interrupts. */
559 usbd_abort_pipe(sc->sc_intrpipe);
560 usbd_close_pipe(sc->sc_intrpipe);
561
562 sc->sc_enabled = 0;
563
564 #if defined(USBVERBOSE) && defined(__FreeBSD__)
565 if (sc->qcount != 0)
566 DPRINTF(("Discarded %d bytes in queue\n", sc->qcount));
567 #endif
568 }
569
570 #if defined(__NetBSD__)
571 static int
572 ums_ioctl(v, cmd, data, flag, p)
573 void *v;
574 u_long cmd;
575 caddr_t data;
576 int flag;
577 struct proc *p;
578
579 {
580 switch (cmd) {
581 case WSMOUSEIO_GTYPE:
582 *(u_int *)data = WSMOUSE_TYPE_USB;
583 return (0);
584 }
585
586 return (-1);
587 }
588
589 #elif defined(__FreeBSD__)
590 static int
591 ums_open(dev_t dev, int flag, int fmt, struct proc *p)
592 {
593 struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
594
595 if (!sc) {
596 DPRINTF(("sc not found at open"));
597 return EINVAL;
598 }
599
600 return ums_enable(sc);
601 }
602
603 static int
604 ums_close(dev_t dev, int flag, int fmt, struct proc *p)
605 {
606 struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
607
608 if (!sc) {
609 DPRINTF(("sc not found at close"));
610 return EINVAL;
611 }
612
613 if (sc->sc_enabled)
614 ums_disable(sc);
615 return 0;
616 }
617
618 static int
619 ums_read(dev_t dev, struct uio *uio, int flag)
620 {
621 struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
622 int s;
623 char buf[sizeof(sc->qbuf)];
624 int l = 0;
625 int error;
626
627 if (!sc || !sc->sc_enabled) {
628 DPRINTF(("sc not found at read"));
629 return EINVAL;
630 }
631
632 s = splusb();
633 while (sc->qcount == 0 ) {
634 /* NWH XXX non blocking I/O ??
635 if (non blocking I/O ) {
636 splx(s);
637 return EWOULDBLOCK;
638 } else {
639 */
640 sc->state |= UMS_ASLEEP;
641 error = tsleep(sc, PZERO | PCATCH, "umsrea", 0);
642 sc->state &= ~UMS_ASLEEP;
643 if (error) {
644 splx(s);
645 return error;
646 }
647 }
648
649 while ((sc->qcount > 0) && (uio->uio_resid > 0)) {
650 l = (sc->qcount < uio->uio_resid? sc->qcount:uio->uio_resid);
651 if (l > sizeof(buf))
652 l = sizeof(buf);
653 if (l > sizeof(sc->qbuf) - sc->qtail) /* transfer till end of buf */
654 l = sizeof(sc->qbuf) - sc->qtail;
655
656 splx(s);
657 uiomove(&sc->qbuf[sc->qtail], l, uio);
658 s = splusb();
659
660 if ( sc->qcount - l < 0 ) {
661 DPRINTF(("qcount below 0, count=%d l=%d\n", sc->qcount, l));
662 sc->qcount = l;
663 }
664 sc->qcount -= l; /* remove the bytes from the buffer */
665 sc->qtail = (sc->qtail + l) % sizeof(sc->qbuf);
666 }
667 splx(s);
668
669 return 0;
670 }
671
672 static int
673 ums_poll(dev_t dev, int events, struct proc *p)
674 {
675 struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
676 int revents = 0;
677 int s;
678
679 if (!sc) {
680 DPRINTF(("sc not found at poll"));
681 return 0; /* just to make sure */
682 }
683
684 s = splusb();
685 if (events & (POLLIN | POLLRDNORM))
686 if (sc->qcount) {
687 revents = events & (POLLIN | POLLRDNORM);
688 } else {
689 sc->state |= UMS_SELECT;
690 selrecord(p, &sc->rsel);
691 }
692 splx(s);
693
694 return revents;
695 }
696
697 int
698 ums_ioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
699 {
700 struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
701 int error = 0;
702 int s;
703
704 if (!sc) {
705 DPRINTF(("sc not found at ioctl"));
706 return (ENOENT);
707 }
708
709 switch(cmd) {
710 case MOUSE_GETHWINFO:
711 *(mousehw_t *)addr = sc->hw;
712 break;
713 case MOUSE_GETMODE:
714 *(mousemode_t *)addr = sc->mode;
715 break;
716 case MOUSE_GETLEVEL:
717 *(int *)addr = sc->mode.level;
718 break;
719 case MOUSE_GETSTATUS: {
720 mousestatus_t *status = (mousestatus_t *) addr;
721
722 s = splusb();
723 *status = sc->status;
724 sc->status.obutton = sc->status.button;
725 sc->status.button = 0;
726 sc->status.dx = sc->status.dy = sc->status.dz = 0;
727 splx(s);
728
729 if (status->dx || status->dy || status->dz)
730 status->flags |= MOUSE_POSCHANGED;
731 if (status->button != status->obutton)
732 status->flags |= MOUSE_BUTTONSCHANGED;
733 break;
734 }
735 default:
736 error = ENOTTY;
737 }
738
739 return error;
740 }
741 #endif
742
743 #if defined(__FreeBSD__)
744 CDEV_DRIVER_MODULE(ums, usb, ums_driver, ums_devclass,
745 UMS_CDEV_MAJOR, ums_cdevsw, usbd_driver_load, 0);
746 #endif
747