Nuke USB_GET_SC and USB_GET_SC_OPEN macros.
[dragonfly.git] / sys / dev / usbmisc / ums / ums.c
1 /*
2  * $FreeBSD: src/sys/dev/usb/ums.c,v 1.64 2003/11/09 09:17:22 tanimura Exp $
3  * $DragonFly: src/sys/dev/usbmisc/ums/ums.c,v 1.26 2007/07/02 06:43:31 hasso Exp $
4  */
5
6 /*
7  * Copyright (c) 1998 The NetBSD Foundation, Inc.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to The NetBSD Foundation
11  * by Lennart Augustsson (lennart@augustsson.net) at
12  * Carlstedt Research & Technology.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. All advertising materials mentioning features or use of this software
23  *    must display the following acknowledgement:
24  *        This product includes software developed by the NetBSD
25  *        Foundation, Inc. and its contributors.
26  * 4. Neither the name of The NetBSD Foundation nor the names of its
27  *    contributors may be used to endorse or promote products derived
28  *    from this software without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40  * POSSIBILITY OF SUCH DAMAGE.
41  */
42
43 /*
44  * HID spec: http://www.usb.org/developers/data/devclass/hid1_1.pdf
45  */
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/module.h>
52 #include <sys/bus.h>
53 #include <sys/ioccom.h>
54 #include <sys/conf.h>
55 #include <sys/tty.h>
56 #include <sys/file.h>
57 #include <sys/select.h>
58 #include <sys/vnode.h>
59 #include <sys/poll.h>
60 #include <sys/sysctl.h>
61 #include <sys/thread2.h>
62
63 #include <bus/usb/usb.h>
64 #include <bus/usb/usbhid.h>
65
66 #include <bus/usb/usbdi.h>
67 #include <bus/usb/usbdi_util.h>
68 #include <bus/usb/usbdevs.h>
69 #include <bus/usb/usb_quirks.h>
70 #include <bus/usb/hid.h>
71
72 #include <machine/mouse.h>
73
74 #ifdef USB_DEBUG
75 #define DPRINTF(x)      if (umsdebug) kprintf x
76 #define DPRINTFN(n,x)   if (umsdebug>(n)) kprintf x
77 int     umsdebug = 0;
78 SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW, 0, "USB ums");
79 SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RW,
80            &umsdebug, 0, "ums debug level");
81 #else
82 #define DPRINTF(x)
83 #define DPRINTFN(n,x)
84 #endif
85
86 #define UMSUNIT(s)      (minor(s)&0x1f)
87
88 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
89
90 #define QUEUE_BUFSIZE   400     /* MUST be divisible by 5 _and_ 8 */
91
92 struct ums_softc {
93         device_t sc_dev;                /* base device */
94         usbd_interface_handle sc_iface; /* interface */
95         usbd_pipe_handle sc_intrpipe;   /* interrupt pipe */
96         int sc_ep_addr;
97
98         u_char *sc_ibuf;
99         u_int8_t sc_iid;
100         int sc_isize;
101         struct hid_location sc_loc_x, sc_loc_y, sc_loc_z;
102         struct hid_location *sc_loc_btn;
103
104         struct callout sc_timeout;      /* for spurious button ups */
105
106         int sc_enabled;
107         int sc_disconnected;    /* device is gone */
108
109         int flags;              /* device configuration */
110 #define UMS_Z           0x01    /* z direction available */
111 #define UMS_SPUR_BUT_UP 0x02    /* spurious button up events */
112         int nbuttons;
113 #define MAX_BUTTONS     31      /* must not exceed size of sc_buttons */
114
115         u_char          qbuf[QUEUE_BUFSIZE];    /* must be divisable by 3&4 */
116         u_char          dummy[100];     /* XXX just for safety and for now */
117         int             qcount, qhead, qtail;
118         mousehw_t       hw;
119         mousemode_t     mode;
120         mousestatus_t   status;
121
122         int             state;
123 #         define        UMS_ASLEEP      0x01    /* readFromDevice is waiting */
124 #         define        UMS_SELECT      0x02    /* select is waiting */
125         struct selinfo  rsel;           /* process waiting in select */
126 };
127
128 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
129 #define MOUSE_FLAGS (HIO_RELATIVE)
130
131 static void ums_intr(usbd_xfer_handle xfer,
132                           usbd_private_handle priv, usbd_status status);
133
134 static void ums_add_to_queue(struct ums_softc *sc,
135                                 int dx, int dy, int dz, int buttons);
136 static void ums_add_to_queue_timeout(void *priv);
137
138 static int  ums_enable(void *);
139 static void ums_disable(void *);
140
141 static d_open_t  ums_open;
142 static d_close_t ums_close;
143 static d_read_t  ums_read;
144 static d_ioctl_t ums_ioctl;
145 static d_poll_t  ums_poll;
146
147 #define UMS_CDEV_MAJOR  111
148
149 static struct dev_ops ums_ops = {
150         { "ums", UMS_CDEV_MAJOR, 0 },
151         .d_open =       ums_open,
152         .d_close =      ums_close,
153         .d_read =       ums_read,
154         .d_ioctl =      ums_ioctl,
155         .d_poll =       ums_poll,
156 };
157
158 USB_DECLARE_DRIVER(ums);
159
160 static int
161 ums_match(device_t self)
162 {
163         struct usb_attach_arg *uaa = device_get_ivars(self);
164         usb_interface_descriptor_t *id;
165         int size, ret;
166         void *desc;
167         usbd_status err;
168
169         if (!uaa->iface)
170                 return (UMATCH_NONE);
171         id = usbd_get_interface_descriptor(uaa->iface);
172         if (!id || id->bInterfaceClass != UICLASS_HID)
173                 return (UMATCH_NONE);
174
175         err = usbd_read_report_desc(uaa->iface, &desc, &size, M_TEMP);
176         if (err)
177                 return (UMATCH_NONE);
178
179         if (hid_is_collection(desc, size,
180                               HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
181                 ret = UMATCH_IFACECLASS;
182         else
183                 ret = UMATCH_NONE;
184
185         kfree(desc, M_TEMP);
186         return (ret);
187 }
188
189 static int
190 ums_attach(device_t self)
191 {
192         struct ums_softc *sc = device_get_softc(self);
193         struct usb_attach_arg *uaa = device_get_ivars(self);
194         usbd_interface_handle iface = uaa->iface;
195         usb_interface_descriptor_t *id;
196         usb_endpoint_descriptor_t *ed;
197         int size;
198         void *desc;
199         usbd_status err;
200         char devinfo[1024];
201         u_int32_t flags;
202         int i;
203         struct hid_location loc_btn;
204
205         sc->sc_disconnected = 1;
206         sc->sc_iface = iface;
207         id = usbd_get_interface_descriptor(iface);
208         usbd_devinfo(uaa->device, 0, devinfo);
209         sc->sc_dev = self;
210         device_set_desc_copy(self, devinfo);
211         kprintf("%s: %s, iclass %d/%d\n", device_get_nameunit(sc->sc_dev),
212                devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
213         ed = usbd_interface2endpoint_descriptor(iface, 0);
214         if (!ed) {
215                 kprintf("%s: could not read endpoint descriptor\n",
216                        device_get_nameunit(sc->sc_dev));
217                 return ENXIO;
218         }
219
220         DPRINTFN(10,("ums_attach: bLength=%d bDescriptorType=%d "
221                      "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
222                      " bInterval=%d\n",
223                      ed->bLength, ed->bDescriptorType,
224                      UE_GET_ADDR(ed->bEndpointAddress),
225                      UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN ? "in":"out",
226                      UE_GET_XFERTYPE(ed->bmAttributes),
227                      UGETW(ed->wMaxPacketSize), ed->bInterval));
228
229         if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
230             UE_GET_XFERTYPE(ed->bmAttributes) != UE_INTERRUPT) {
231                 kprintf("%s: unexpected endpoint\n",
232                        device_get_nameunit(sc->sc_dev));
233                 return ENXIO;
234         }
235
236         err = usbd_read_report_desc(uaa->iface, &desc, &size, M_TEMP);
237         if (err)
238                 return ENXIO;
239
240         if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
241                        hid_input, &sc->sc_loc_x, &flags)) {
242                 kprintf("%s: mouse has no X report\n", device_get_nameunit(sc->sc_dev));
243                 return ENXIO;
244         }
245         if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
246                 kprintf("%s: X report 0x%04x not supported\n",
247                        device_get_nameunit(sc->sc_dev), flags);
248                 return ENXIO;
249         }
250
251         if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
252                        hid_input, &sc->sc_loc_y, &flags)) {
253                 kprintf("%s: mouse has no Y report\n", device_get_nameunit(sc->sc_dev));
254                 return ENXIO;
255         }
256         if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
257                 kprintf("%s: Y report 0x%04x not supported\n",
258                        device_get_nameunit(sc->sc_dev), flags);
259                 return ENXIO;
260         }
261
262         /* try to guess the Z activator: first check Z, then WHEEL */
263         if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
264                        hid_input, &sc->sc_loc_z, &flags) ||
265             hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
266                        hid_input, &sc->sc_loc_z, &flags)) {
267                 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
268                         sc->sc_loc_z.size = 0;  /* Bad Z coord, ignore it */
269                 } else {
270                         sc->flags |= UMS_Z;
271                 }
272         }
273
274         /* figure out the number of buttons */
275         for (i = 1; i <= MAX_BUTTONS; i++)
276                 if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
277                                 hid_input, &loc_btn, 0))
278                         break;
279         sc->nbuttons = i - 1;
280         sc->sc_loc_btn = kmalloc(sizeof(struct hid_location)*sc->nbuttons,
281                                 M_USBDEV, M_INTWAIT);
282
283         kprintf("%s: %d buttons%s\n", device_get_nameunit(sc->sc_dev),
284                sc->nbuttons, sc->flags & UMS_Z? " and Z dir." : "");
285
286         for (i = 1; i <= sc->nbuttons; i++)
287                 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
288                                 hid_input, &sc->sc_loc_btn[i-1], 0);
289
290         sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
291         sc->sc_ibuf = kmalloc(sc->sc_isize, M_USB, M_INTWAIT);
292         sc->sc_ep_addr = ed->bEndpointAddress;
293         sc->sc_disconnected = 0;
294         kfree(desc, M_TEMP);
295
296 #ifdef USB_DEBUG
297         DPRINTF(("ums_attach: sc=%p\n", sc));
298         DPRINTF(("ums_attach: X\t%d/%d\n",
299                  sc->sc_loc_x.pos, sc->sc_loc_x.size));
300         DPRINTF(("ums_attach: Y\t%d/%d\n",
301                  sc->sc_loc_y.pos, sc->sc_loc_y.size));
302         if (sc->flags & UMS_Z)
303                 DPRINTF(("ums_attach: Z\t%d/%d\n",
304                          sc->sc_loc_z.pos, sc->sc_loc_z.size));
305         for (i = 1; i <= sc->nbuttons; i++) {
306                 DPRINTF(("ums_attach: B%d\t%d/%d\n",
307                          i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));
308         }
309         DPRINTF(("ums_attach: size=%d, id=%d\n", sc->sc_isize, sc->sc_iid));
310 #endif
311
312         if (sc->nbuttons > MOUSE_MSC_MAXBUTTON)
313                 sc->hw.buttons = MOUSE_MSC_MAXBUTTON;
314         else
315                 sc->hw.buttons = sc->nbuttons;
316         sc->hw.iftype = MOUSE_IF_USB;
317         sc->hw.type = MOUSE_MOUSE;
318         sc->hw.model = MOUSE_MODEL_GENERIC;
319         sc->hw.hwid = 0;
320         sc->mode.protocol = MOUSE_PROTO_MSC;
321         sc->mode.rate = -1;
322         sc->mode.resolution = MOUSE_RES_UNKNOWN;
323         sc->mode.accelfactor = 0;
324         sc->mode.level = 0;
325         sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
326         sc->mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
327         sc->mode.syncmask[1] = MOUSE_MSC_SYNC;
328
329         sc->status.flags = 0;
330         sc->status.button = sc->status.obutton = 0;
331         sc->status.dx = sc->status.dy = sc->status.dz = 0;
332
333         dev_ops_add(&ums_ops, -1, device_get_unit(self));
334         make_dev(&ums_ops, device_get_unit(self),
335                 UID_ROOT, GID_OPERATOR,
336                 0644, "ums%d", device_get_unit(self));
337
338         if (usbd_get_quirks(uaa->device)->uq_flags & UQ_SPUR_BUT_UP) {
339                 DPRINTF(("%s: Spurious button up events\n",
340                         device_get_nameunit(sc->sc_dev)));
341                 sc->flags |= UMS_SPUR_BUT_UP;
342         }
343
344         return 0;
345 }
346
347
348 static int
349 ums_detach(device_t self)
350 {
351         struct ums_softc *sc = device_get_softc(self);
352
353         if (sc->sc_enabled)
354                 ums_disable(sc);
355
356         DPRINTF(("%s: disconnected\n", device_get_nameunit(self)));
357
358         kfree(sc->sc_loc_btn, M_USB);
359         kfree(sc->sc_ibuf, M_USB);
360
361         /* someone waiting for data */
362         /*
363          * XXX If we wakeup the process here, the device will be gone by
364          * the time the process gets a chance to notice. *_close and friends
365          * should be fixed to handle this case.
366          * Or we should do a delayed detach for this.
367          * Does this delay now force tsleep to exit with an error?
368          */
369         if (sc->state & UMS_ASLEEP) {
370                 sc->state &= ~UMS_ASLEEP;
371                 wakeup(sc);
372         }
373         if (sc->state & UMS_SELECT) {
374                 sc->state &= ~UMS_SELECT;
375                 selwakeuppri(&sc->rsel, 0);
376         }
377         dev_ops_remove(&ums_ops, -1, device_get_unit(self));
378
379         return 0;
380 }
381
382 void
383 ums_intr(usbd_xfer_handle xfer, usbd_private_handle addr,
384          usbd_status status)
385 {
386         struct ums_softc *sc = addr;
387         u_char *ibuf;
388         int dx, dy, dz;
389         u_char buttons = 0;
390         int i;
391
392 #define UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
393
394         DPRINTFN(5, ("ums_intr: sc=%p status=%d\n", sc, status));
395         DPRINTFN(5, ("ums_intr: data = %02x %02x %02x\n",
396                      sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
397
398         if (status == USBD_CANCELLED)
399                 return;
400
401         if (status != USBD_NORMAL_COMPLETION) {
402                 DPRINTF(("ums_intr: status=%d\n", status));
403                 if (status == USBD_STALLED)
404                     usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
405                 return;
406         }
407
408         ibuf = sc->sc_ibuf;
409         if (sc->sc_iid) {
410                 if (*ibuf++ != sc->sc_iid)
411                         return;
412         }
413
414         dx =  hid_get_data(ibuf, &sc->sc_loc_x);
415         dy = -hid_get_data(ibuf, &sc->sc_loc_y);
416         dz = -hid_get_data(ibuf, &sc->sc_loc_z);
417         for (i = 0; i < sc->nbuttons; i++)
418                 if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
419                         buttons |= (1 << UMS_BUT(i));
420
421         if (dx || dy || dz || (sc->flags & UMS_Z)
422             || buttons != sc->status.button) {
423                 DPRINTFN(5, ("ums_intr: x:%d y:%d z:%d buttons:0x%x\n",
424                         dx, dy, dz, buttons));
425
426                 sc->status.button = buttons;
427                 sc->status.dx += dx;
428                 sc->status.dy += dy;
429                 sc->status.dz += dz;
430
431                 /* Discard data in case of full buffer */
432                 if (sc->qcount == sizeof(sc->qbuf)) {
433                         DPRINTF(("Buffer full, discarded packet"));
434                         return;
435                 }
436
437                 /*
438                  * The Qtronix keyboard has a built in PS/2 port for a mouse.
439                  * The firmware once in a while posts a spurious button up
440                  * event. This event we ignore by doing a timeout for 50 msecs.
441                  * If we receive dx=dy=dz=buttons=0 before we add the event to
442                  * the queue.
443                  * In any other case we delete the timeout event.
444                  */
445                 if (sc->flags & UMS_SPUR_BUT_UP &&
446                     dx == 0 && dy == 0 && dz == 0 && buttons == 0) {
447                         callout_reset(&sc->sc_timeout, MS_TO_TICKS(50),
448                                     ums_add_to_queue_timeout, (void *) sc);
449                 } else {
450                         callout_stop(&sc->sc_timeout);
451                         ums_add_to_queue(sc, dx, dy, dz, buttons);
452                 }
453         }
454 }
455
456 static void
457 ums_add_to_queue_timeout(void *priv)
458 {
459         struct ums_softc *sc = priv;
460
461         crit_enter();
462         ums_add_to_queue(sc, 0, 0, 0, 0);
463         crit_exit();
464 }
465
466 static void
467 ums_add_to_queue(struct ums_softc *sc, int dx, int dy, int dz, int buttons)
468 {
469         /* Discard data in case of full buffer */
470         if (sc->qhead+sc->mode.packetsize > sizeof(sc->qbuf)) {
471                 DPRINTF(("Buffer full, discarded packet"));
472                 return;
473         }
474
475         if (dx >  254)          dx =  254;
476         if (dx < -256)          dx = -256;
477         if (dy >  254)          dy =  254;
478         if (dy < -256)          dy = -256;
479         if (dz >  126)          dz =  126;
480         if (dz < -128)          dz = -128;
481
482         sc->qbuf[sc->qhead] = sc->mode.syncmask[1];
483         sc->qbuf[sc->qhead] |= ~buttons & MOUSE_MSC_BUTTONS;
484         sc->qbuf[sc->qhead+1] = dx >> 1;
485         sc->qbuf[sc->qhead+2] = dy >> 1;
486         sc->qbuf[sc->qhead+3] = dx - (dx >> 1);
487         sc->qbuf[sc->qhead+4] = dy - (dy >> 1);
488
489         if (sc->mode.level == 1) {
490                 sc->qbuf[sc->qhead+5] = dz >> 1;
491                 sc->qbuf[sc->qhead+6] = dz - (dz >> 1);
492                 sc->qbuf[sc->qhead+7] = ((~buttons >> 3)
493                                          & MOUSE_SYS_EXTBUTTONS);
494         }
495
496         sc->qhead += sc->mode.packetsize;
497         sc->qcount += sc->mode.packetsize;
498         /* wrap round at end of buffer */
499         if (sc->qhead >= sizeof(sc->qbuf))
500                 sc->qhead = 0;
501
502         /* someone waiting for data */
503         if (sc->state & UMS_ASLEEP) {
504                 sc->state &= ~UMS_ASLEEP;
505                 wakeup(sc);
506         }
507         if (sc->state & UMS_SELECT) {
508                 sc->state &= ~UMS_SELECT;
509                 selwakeuppri(&sc->rsel, 0);
510         }
511 }
512
513 static int
514 ums_enable(void *v)
515 {
516         struct ums_softc *sc = v;
517
518         usbd_status err;
519
520         if (sc->sc_enabled)
521                 return EBUSY;
522
523         sc->sc_enabled = 1;
524         sc->qcount = 0;
525         sc->qhead = sc->qtail = 0;
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
530         callout_init(&sc->sc_timeout);
531
532         /* Set up interrupt pipe. */
533         err = 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                                 USBD_DEFAULT_INTERVAL);
537         if (err) {
538                 DPRINTF(("ums_enable: usbd_open_pipe_intr failed, error=%d\n",
539                          err));
540                 sc->sc_enabled = 0;
541                 return (EIO);
542         }
543         return (0);
544 }
545
546 static void
547 ums_disable(void *priv)
548 {
549         struct ums_softc *sc = priv;
550
551         callout_stop(&sc->sc_timeout);
552
553         /* Disable interrupts. */
554         usbd_abort_pipe(sc->sc_intrpipe);
555         usbd_close_pipe(sc->sc_intrpipe);
556
557         sc->sc_enabled = 0;
558
559         if (sc->qcount != 0)
560                 DPRINTF(("Discarded %d bytes in queue\n", sc->qcount));
561 }
562
563 static int
564 ums_open(struct dev_open_args *ap)
565 {
566         cdev_t dev = ap->a_head.a_dev;
567         struct ums_softc *sc;
568
569         sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
570         if (sc == NULL)
571                 return (ENXIO);
572
573         return ums_enable(sc);
574 }
575
576 static int
577 ums_close(struct dev_close_args *ap)
578 {
579         cdev_t dev = ap->a_head.a_dev;
580         struct ums_softc *sc;
581
582         sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
583
584         if (!sc)
585                 return 0;
586
587         if (sc->sc_enabled)
588                 ums_disable(sc);
589
590         return 0;
591 }
592
593 static int
594 ums_read(struct dev_read_args *ap)
595 {
596         cdev_t dev = ap->a_head.a_dev;
597         struct uio *uio = ap->a_uio;
598         struct ums_softc *sc;
599         char buf[sizeof(sc->qbuf)];
600         int l = 0;
601         int error;
602
603         sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
604
605         crit_enter();
606         if (!sc) {
607                 crit_exit();
608                 return EIO;
609         }
610
611         while (sc->qcount == 0 )  {
612                 if (ap->a_ioflag & IO_NDELAY) {         /* non-blocking I/O */
613                         crit_exit();
614                         return EWOULDBLOCK;
615                 }
616
617                 sc->state |= UMS_ASLEEP;        /* blocking I/O */
618                 error = tsleep(sc, PCATCH, "umsrea", 0);
619                 if (error) {
620                         crit_exit();
621                         return error;
622                 } else if (!sc->sc_enabled) {
623                         crit_exit();
624                         return EINTR;
625                 }
626                 /* check whether the device is still there */
627
628                 sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
629                 if (!sc) {
630                         crit_exit();
631                         return EIO;
632                 }
633         }
634
635         /*
636          * The writer process only extends qcount and qtail. We could copy
637          * them and use the copies to do the copying out of the queue.
638          */
639
640         while ((sc->qcount > 0) && (uio->uio_resid > 0)) {
641                 l = (sc->qcount < uio->uio_resid? sc->qcount:uio->uio_resid);
642                 if (l > sizeof(buf))
643                         l = sizeof(buf);
644                 if (l > sizeof(sc->qbuf) - sc->qtail)           /* transfer till end of buf */
645                         l = sizeof(sc->qbuf) - sc->qtail;
646
647                 crit_exit();
648                 uiomove(&sc->qbuf[sc->qtail], l, uio);
649                 crit_enter();
650
651                 if ( sc->qcount - l < 0 ) {
652                         DPRINTF(("qcount below 0, count=%d l=%d\n", sc->qcount, l));
653                         sc->qcount = l;
654                 }
655                 sc->qcount -= l;        /* remove the bytes from the buffer */
656                 sc->qtail = (sc->qtail + l) % sizeof(sc->qbuf);
657         }
658         crit_exit();
659
660         return 0;
661 }
662
663 static int
664 ums_poll(struct dev_poll_args *ap)
665 {
666         cdev_t dev = ap->a_head.a_dev;
667         struct ums_softc *sc;
668         int revents = 0;
669
670         sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
671
672         if (!sc) {
673                 ap->a_events = 0;
674                 return 0;
675         }
676
677         crit_enter();
678         if (ap->a_events & (POLLIN | POLLRDNORM)) {
679                 if (sc->qcount) {
680                         revents = ap->a_events & (POLLIN | POLLRDNORM);
681                 } else {
682                         sc->state |= UMS_SELECT;
683                         selrecord(curthread, &sc->rsel);
684                 }
685         }
686         crit_exit();
687         ap->a_events = revents;
688         return (0);
689 }
690
691 int
692 ums_ioctl(struct dev_ioctl_args *ap)
693 {
694         cdev_t dev = ap->a_head.a_dev;
695         struct ums_softc *sc;
696         int error = 0;
697         mousemode_t mode;
698
699         sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
700
701         if (!sc)
702                 return EIO;
703
704         switch(ap->a_cmd) {
705         case MOUSE_GETHWINFO:
706                 *(mousehw_t *)ap->a_data = sc->hw;
707                 break;
708         case MOUSE_GETMODE:
709                 *(mousemode_t *)ap->a_data = sc->mode;
710                 break;
711         case MOUSE_SETMODE:
712                 mode = *(mousemode_t *)ap->a_data;
713
714                 if (mode.level == -1)
715                         /* don't change the current setting */
716                         ;
717                 else if ((mode.level < 0) || (mode.level > 1))
718                         return (EINVAL);
719
720                 crit_enter();
721                 sc->mode.level = mode.level;
722
723                 if (sc->mode.level == 0) {
724                         if (sc->nbuttons > MOUSE_MSC_MAXBUTTON)
725                                 sc->hw.buttons = MOUSE_MSC_MAXBUTTON;
726                         else
727                                 sc->hw.buttons = sc->nbuttons;
728                         sc->mode.protocol = MOUSE_PROTO_MSC;
729                         sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
730                         sc->mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
731                         sc->mode.syncmask[1] = MOUSE_MSC_SYNC;
732                 } else if (sc->mode.level == 1) {
733                         if (sc->nbuttons > MOUSE_SYS_MAXBUTTON)
734                                 sc->hw.buttons = MOUSE_SYS_MAXBUTTON;
735                         else
736                                 sc->hw.buttons = sc->nbuttons;
737                         sc->mode.protocol = MOUSE_PROTO_SYSMOUSE;
738                         sc->mode.packetsize = MOUSE_SYS_PACKETSIZE;
739                         sc->mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
740                         sc->mode.syncmask[1] = MOUSE_SYS_SYNC;
741                 }
742
743                 bzero(sc->qbuf, sizeof(sc->qbuf));
744                 sc->qhead = sc->qtail = sc->qcount = 0;
745                 crit_exit();
746
747                 break;
748         case MOUSE_GETLEVEL:
749                 *(int *)ap->a_data = sc->mode.level;
750                 break;
751         case MOUSE_SETLEVEL:
752                 if (*(int *)ap->a_data < 0 || *(int *)ap->a_data > 1)
753                         return (EINVAL);
754
755                 crit_enter();
756                 sc->mode.level = *(int *)ap->a_data;
757
758                 if (sc->mode.level == 0) {
759                         if (sc->nbuttons > MOUSE_MSC_MAXBUTTON)
760                                 sc->hw.buttons = MOUSE_MSC_MAXBUTTON;
761                         else
762                                 sc->hw.buttons = sc->nbuttons;
763                         sc->mode.protocol = MOUSE_PROTO_MSC;
764                         sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
765                         sc->mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
766                         sc->mode.syncmask[1] = MOUSE_MSC_SYNC;
767                 } else if (sc->mode.level == 1) {
768                         if (sc->nbuttons > MOUSE_SYS_MAXBUTTON)
769                                 sc->hw.buttons = MOUSE_SYS_MAXBUTTON;
770                         else
771                                 sc->hw.buttons = sc->nbuttons;
772                         sc->mode.protocol = MOUSE_PROTO_SYSMOUSE;
773                         sc->mode.packetsize = MOUSE_SYS_PACKETSIZE;
774                         sc->mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
775                         sc->mode.syncmask[1] = MOUSE_SYS_SYNC;
776                 }
777
778                 bzero(sc->qbuf, sizeof(sc->qbuf));
779                 sc->qhead = sc->qtail = sc->qcount = 0;
780                 crit_exit();
781
782                 break;
783         case MOUSE_GETSTATUS: {
784                 mousestatus_t *status = (mousestatus_t *) ap->a_data;
785
786                 crit_enter();
787                 *status = sc->status;
788                 sc->status.obutton = sc->status.button;
789                 sc->status.button = 0;
790                 sc->status.dx = sc->status.dy = sc->status.dz = 0;
791                 crit_exit();
792
793                 if (status->dx || status->dy || status->dz)
794                         status->flags |= MOUSE_POSCHANGED;
795                 if (status->button != status->obutton)
796                         status->flags |= MOUSE_BUTTONSCHANGED;
797                 break;
798                 }
799         default:
800                 error = ENOTTY;
801         }
802
803         return error;
804 }
805
806 DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, usbd_driver_load, 0);