kernel - Remove D_KQFILTER flag
[dragonfly.git] / sys / dev / usbmisc / ums / ums.c
CommitLineData
1550dfd9
MD
1/*
2 * $FreeBSD: src/sys/dev/usb/ums.c,v 1.64 2003/11/09 09:17:22 tanimura Exp $
74781d8f 3 * $DragonFly: src/sys/dev/usbmisc/ums/ums.c,v 1.31 2008/08/14 20:55:54 hasso Exp $
1550dfd9 4 */
984263bc
MD
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/*
1550dfd9 44 * HID spec: http://www.usb.org/developers/data/devclass/hid1_1.pdf
984263bc
MD
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>
984263bc
MD
53#include <sys/conf.h>
54#include <sys/tty.h>
55#include <sys/file.h>
984263bc 56#include <sys/vnode.h>
96bcde04 57#include <sys/event.h>
984263bc 58#include <sys/sysctl.h>
4e01b467 59#include <sys/thread2.h>
984263bc 60
1f2de5d4
MD
61#include <bus/usb/usb.h>
62#include <bus/usb/usbhid.h>
984263bc 63
1f2de5d4
MD
64#include <bus/usb/usbdi.h>
65#include <bus/usb/usbdi_util.h>
1f2de5d4
MD
66#include <bus/usb/usb_quirks.h>
67#include <bus/usb/hid.h>
984263bc
MD
68
69#include <machine/mouse.h>
70
71#ifdef USB_DEBUG
fc1ef497
HT
72#define DPRINTF(x) if (umsdebug) kprintf x
73#define DPRINTFN(n,x) if (umsdebug>(n)) kprintf x
984263bc
MD
74int umsdebug = 0;
75SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW, 0, "USB ums");
76SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RW,
77 &umsdebug, 0, "ums debug level");
78#else
79#define DPRINTF(x)
80#define DPRINTFN(n,x)
81#endif
82
83#define UMSUNIT(s) (minor(s)&0x1f)
84
1550dfd9 85#define MS_TO_TICKS(ms) ((ms) * hz / 1000)
984263bc
MD
86
87#define QUEUE_BUFSIZE 400 /* MUST be divisible by 5 _and_ 8 */
88
89struct ums_softc {
90 device_t sc_dev; /* base device */
91 usbd_interface_handle sc_iface; /* interface */
92 usbd_pipe_handle sc_intrpipe; /* interrupt pipe */
93 int sc_ep_addr;
94
95 u_char *sc_ibuf;
96 u_int8_t sc_iid;
97 int sc_isize;
98 struct hid_location sc_loc_x, sc_loc_y, sc_loc_z;
99 struct hid_location *sc_loc_btn;
100
8c64e381 101 struct callout sc_timeout; /* for spurious button ups */
984263bc
MD
102
103 int sc_enabled;
104 int sc_disconnected; /* device is gone */
105
106 int flags; /* device configuration */
107#define UMS_Z 0x01 /* z direction available */
108#define UMS_SPUR_BUT_UP 0x02 /* spurious button up events */
109 int nbuttons;
bc5a1c0b 110#define MAX_BUTTONS 31 /* must not exceed size of sc_buttons */
984263bc
MD
111
112 u_char qbuf[QUEUE_BUFSIZE]; /* must be divisable by 3&4 */
113 u_char dummy[100]; /* XXX just for safety and for now */
114 int qcount, qhead, qtail;
115 mousehw_t hw;
116 mousemode_t mode;
117 mousestatus_t status;
118
119 int state;
120# define UMS_ASLEEP 0x01 /* readFromDevice is waiting */
5b22f1a7 121 struct kqinfo rkq; /* process waiting in select/poll/kq */
984263bc
MD
122};
123
124#define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
125#define MOUSE_FLAGS (HIO_RELATIVE)
126
6ed427ca 127static void ums_intr(usbd_xfer_handle xfer,
984263bc
MD
128 usbd_private_handle priv, usbd_status status);
129
6ed427ca 130static void ums_add_to_queue(struct ums_softc *sc,
984263bc 131 int dx, int dy, int dz, int buttons);
6ed427ca 132static void ums_add_to_queue_timeout(void *priv);
984263bc 133
6ed427ca
HT
134static int ums_enable(void *);
135static void ums_disable(void *);
984263bc 136
6ed427ca
HT
137static d_open_t ums_open;
138static d_close_t ums_close;
139static d_read_t ums_read;
140static d_ioctl_t ums_ioctl;
96bcde04
SG
141static d_kqfilter_t ums_kqfilter;
142
143static void ums_filt_detach(struct knote *);
144static int ums_filt(struct knote *, long);
984263bc
MD
145
146#define UMS_CDEV_MAJOR 111
147
6ed427ca 148static struct dev_ops ums_ops = {
d4b8aec4 149 { "ums", UMS_CDEV_MAJOR, 0 },
fef8985e
MD
150 .d_open = ums_open,
151 .d_close = ums_close,
152 .d_read = ums_read,
153 .d_ioctl = ums_ioctl,
96bcde04 154 .d_kqfilter = ums_kqfilter
984263bc
MD
155};
156
61b69189
HT
157static device_probe_t ums_match;
158static device_attach_t ums_attach;
159static device_detach_t ums_detach;
160
161static devclass_t ums_devclass;
162
163static kobj_method_t ums_methods[] = {
164 DEVMETHOD(device_probe, ums_match),
165 DEVMETHOD(device_attach, ums_attach),
166 DEVMETHOD(device_detach, ums_detach),
167 {0,0}
168};
169
170static driver_t ums_driver = {
171 "ums",
172 ums_methods,
173 sizeof(struct ums_softc)
174};
175
176MODULE_DEPEND(ums, usb, 1, 1, 1);
984263bc 177
e785a5d9
HT
178static int
179ums_match(device_t self)
984263bc 180{
e785a5d9 181 struct usb_attach_arg *uaa = device_get_ivars(self);
984263bc
MD
182 usb_interface_descriptor_t *id;
183 int size, ret;
184 void *desc;
185 usbd_status err;
1550dfd9 186
984263bc
MD
187 if (!uaa->iface)
188 return (UMATCH_NONE);
189 id = usbd_get_interface_descriptor(uaa->iface);
190 if (!id || id->bInterfaceClass != UICLASS_HID)
191 return (UMATCH_NONE);
192
1550dfd9 193 err = usbd_read_report_desc(uaa->iface, &desc, &size, M_TEMP);
984263bc
MD
194 if (err)
195 return (UMATCH_NONE);
196
1550dfd9 197 if (hid_is_collection(desc, size,
984263bc
MD
198 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
199 ret = UMATCH_IFACECLASS;
200 else
201 ret = UMATCH_NONE;
202
efda3bd0 203 kfree(desc, M_TEMP);
984263bc
MD
204 return (ret);
205}
206
e785a5d9
HT
207static int
208ums_attach(device_t self)
984263bc 209{
e785a5d9
HT
210 struct ums_softc *sc = device_get_softc(self);
211 struct usb_attach_arg *uaa = device_get_ivars(self);
984263bc 212 usbd_interface_handle iface = uaa->iface;
984263bc
MD
213 usb_endpoint_descriptor_t *ed;
214 int size;
215 void *desc;
216 usbd_status err;
984263bc
MD
217 u_int32_t flags;
218 int i;
219 struct hid_location loc_btn;
1550dfd9 220
984263bc
MD
221 sc->sc_disconnected = 1;
222 sc->sc_iface = iface;
e785a5d9 223 sc->sc_dev = self;
984263bc
MD
224 ed = usbd_interface2endpoint_descriptor(iface, 0);
225 if (!ed) {
e3869ec7 226 kprintf("%s: could not read endpoint descriptor\n",
6ed427ca 227 device_get_nameunit(sc->sc_dev));
e785a5d9 228 return ENXIO;
984263bc
MD
229 }
230
231 DPRINTFN(10,("ums_attach: bLength=%d bDescriptorType=%d "
232 "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
233 " bInterval=%d\n",
1550dfd9 234 ed->bLength, ed->bDescriptorType,
984263bc
MD
235 UE_GET_ADDR(ed->bEndpointAddress),
236 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN ? "in":"out",
237 UE_GET_XFERTYPE(ed->bmAttributes),
238 UGETW(ed->wMaxPacketSize), ed->bInterval));
239
240 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
241 UE_GET_XFERTYPE(ed->bmAttributes) != UE_INTERRUPT) {
e3869ec7 242 kprintf("%s: unexpected endpoint\n",
6ed427ca 243 device_get_nameunit(sc->sc_dev));
e785a5d9 244 return ENXIO;
984263bc
MD
245 }
246
1550dfd9 247 err = usbd_read_report_desc(uaa->iface, &desc, &size, M_TEMP);
984263bc 248 if (err)
e785a5d9 249 return ENXIO;
984263bc
MD
250
251 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
252 hid_input, &sc->sc_loc_x, &flags)) {
6ed427ca 253 kprintf("%s: mouse has no X report\n", device_get_nameunit(sc->sc_dev));
e785a5d9 254 return ENXIO;
984263bc
MD
255 }
256 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
e3869ec7 257 kprintf("%s: X report 0x%04x not supported\n",
6ed427ca 258 device_get_nameunit(sc->sc_dev), flags);
e785a5d9 259 return ENXIO;
984263bc
MD
260 }
261
262 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
263 hid_input, &sc->sc_loc_y, &flags)) {
6ed427ca 264 kprintf("%s: mouse has no Y report\n", device_get_nameunit(sc->sc_dev));
e785a5d9 265 return ENXIO;
984263bc
MD
266 }
267 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
e3869ec7 268 kprintf("%s: Y report 0x%04x not supported\n",
6ed427ca 269 device_get_nameunit(sc->sc_dev), flags);
e785a5d9 270 return ENXIO;
984263bc
MD
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 */
286 for (i = 1; i <= MAX_BUTTONS; 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;
77652cad 291 sc->sc_loc_btn = kmalloc(sizeof(struct hid_location)*sc->nbuttons,
ad86f6b6 292 M_USBDEV, M_INTWAIT);
984263bc 293
6ed427ca 294 kprintf("%s: %d buttons%s\n", device_get_nameunit(sc->sc_dev),
984263bc
MD
295 sc->nbuttons, sc->flags & UMS_Z? " and Z dir." : "");
296
297 for (i = 1; i <= sc->nbuttons; i++)
298 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
299 hid_input, &sc->sc_loc_btn[i-1], 0);
300
301 sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
efda3bd0 302 sc->sc_ibuf = kmalloc(sc->sc_isize, M_USB, M_INTWAIT);
984263bc
MD
303 sc->sc_ep_addr = ed->bEndpointAddress;
304 sc->sc_disconnected = 0;
efda3bd0 305 kfree(desc, M_TEMP);
984263bc
MD
306
307#ifdef USB_DEBUG
308 DPRINTF(("ums_attach: sc=%p\n", sc));
1550dfd9 309 DPRINTF(("ums_attach: X\t%d/%d\n",
984263bc 310 sc->sc_loc_x.pos, sc->sc_loc_x.size));
1550dfd9 311 DPRINTF(("ums_attach: Y\t%d/%d\n",
984263bc
MD
312 sc->sc_loc_y.pos, sc->sc_loc_y.size));
313 if (sc->flags & UMS_Z)
1550dfd9 314 DPRINTF(("ums_attach: Z\t%d/%d\n",
984263bc
MD
315 sc->sc_loc_z.pos, sc->sc_loc_z.size));
316 for (i = 1; i <= sc->nbuttons; i++) {
317 DPRINTF(("ums_attach: B%d\t%d/%d\n",
318 i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));
319 }
320 DPRINTF(("ums_attach: size=%d, id=%d\n", sc->sc_isize, sc->sc_iid));
321#endif
322
323 if (sc->nbuttons > MOUSE_MSC_MAXBUTTON)
324 sc->hw.buttons = MOUSE_MSC_MAXBUTTON;
325 else
326 sc->hw.buttons = sc->nbuttons;
327 sc->hw.iftype = MOUSE_IF_USB;
328 sc->hw.type = MOUSE_MOUSE;
329 sc->hw.model = MOUSE_MODEL_GENERIC;
330 sc->hw.hwid = 0;
331 sc->mode.protocol = MOUSE_PROTO_MSC;
332 sc->mode.rate = -1;
333 sc->mode.resolution = MOUSE_RES_UNKNOWN;
334 sc->mode.accelfactor = 0;
335 sc->mode.level = 0;
336 sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
337 sc->mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
338 sc->mode.syncmask[1] = MOUSE_MSC_SYNC;
339
340 sc->status.flags = 0;
341 sc->status.button = sc->status.obutton = 0;
342 sc->status.dx = sc->status.dy = sc->status.dz = 0;
343
fef8985e 344 make_dev(&ums_ops, device_get_unit(self),
3e82b46c
MD
345 UID_ROOT, GID_OPERATOR,
346 0644, "ums%d", device_get_unit(self));
984263bc
MD
347
348 if (usbd_get_quirks(uaa->device)->uq_flags & UQ_SPUR_BUT_UP) {
349 DPRINTF(("%s: Spurious button up events\n",
6ed427ca 350 device_get_nameunit(sc->sc_dev)));
984263bc
MD
351 sc->flags |= UMS_SPUR_BUT_UP;
352 }
353
e785a5d9 354 return 0;
984263bc
MD
355}
356
357
6ed427ca 358static int
984263bc
MD
359ums_detach(device_t self)
360{
361 struct ums_softc *sc = device_get_softc(self);
984263bc
MD
362
363 if (sc->sc_enabled)
364 ums_disable(sc);
365
6ed427ca 366 DPRINTF(("%s: disconnected\n", device_get_nameunit(self)));
984263bc 367
efda3bd0
MD
368 kfree(sc->sc_loc_btn, M_USB);
369 kfree(sc->sc_ibuf, M_USB);
984263bc 370
984263bc
MD
371 /* someone waiting for data */
372 /*
373 * XXX If we wakeup the process here, the device will be gone by
374 * the time the process gets a chance to notice. *_close and friends
375 * should be fixed to handle this case.
376 * Or we should do a delayed detach for this.
377 * Does this delay now force tsleep to exit with an error?
378 */
379 if (sc->state & UMS_ASLEEP) {
380 sc->state &= ~UMS_ASLEEP;
381 wakeup(sc);
382 }
5b22f1a7 383 KNOTE(&sc->rkq.ki_note, 0);
0f538c08 384
cd29885a 385 dev_ops_remove_minor(&ums_ops, /*-1, */device_get_unit(self));
984263bc
MD
386
387 return 0;
388}
389
390void
c436375a
SW
391ums_intr(usbd_xfer_handle xfer, usbd_private_handle addr,
392 usbd_status status)
984263bc
MD
393{
394 struct ums_softc *sc = addr;
395 u_char *ibuf;
396 int dx, dy, dz;
397 u_char buttons = 0;
398 int i;
399
400#define UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
401
402 DPRINTFN(5, ("ums_intr: sc=%p status=%d\n", sc, status));
403 DPRINTFN(5, ("ums_intr: data = %02x %02x %02x\n",
404 sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
405
406 if (status == USBD_CANCELLED)
407 return;
408
409 if (status != USBD_NORMAL_COMPLETION) {
410 DPRINTF(("ums_intr: status=%d\n", status));
1550dfd9
MD
411 if (status == USBD_STALLED)
412 usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
984263bc
MD
413 return;
414 }
415
416 ibuf = sc->sc_ibuf;
417 if (sc->sc_iid) {
418 if (*ibuf++ != sc->sc_iid)
419 return;
420 }
421
422 dx = hid_get_data(ibuf, &sc->sc_loc_x);
423 dy = -hid_get_data(ibuf, &sc->sc_loc_y);
424 dz = -hid_get_data(ibuf, &sc->sc_loc_z);
425 for (i = 0; i < sc->nbuttons; i++)
426 if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
427 buttons |= (1 << UMS_BUT(i));
428
429 if (dx || dy || dz || (sc->flags & UMS_Z)
430 || buttons != sc->status.button) {
431 DPRINTFN(5, ("ums_intr: x:%d y:%d z:%d buttons:0x%x\n",
432 dx, dy, dz, buttons));
433
434 sc->status.button = buttons;
435 sc->status.dx += dx;
436 sc->status.dy += dy;
437 sc->status.dz += dz;
438
439 /* Discard data in case of full buffer */
440 if (sc->qcount == sizeof(sc->qbuf)) {
441 DPRINTF(("Buffer full, discarded packet"));
442 return;
443 }
444
445 /*
446 * The Qtronix keyboard has a built in PS/2 port for a mouse.
447 * The firmware once in a while posts a spurious button up
448 * event. This event we ignore by doing a timeout for 50 msecs.
449 * If we receive dx=dy=dz=buttons=0 before we add the event to
450 * the queue.
451 * In any other case we delete the timeout event.
452 */
453 if (sc->flags & UMS_SPUR_BUT_UP &&
454 dx == 0 && dy == 0 && dz == 0 && buttons == 0) {
8c64e381 455 callout_reset(&sc->sc_timeout, MS_TO_TICKS(50),
1550dfd9 456 ums_add_to_queue_timeout, (void *) sc);
984263bc 457 } else {
8c64e381 458 callout_stop(&sc->sc_timeout);
984263bc
MD
459 ums_add_to_queue(sc, dx, dy, dz, buttons);
460 }
461 }
462}
463
6ed427ca 464static void
984263bc
MD
465ums_add_to_queue_timeout(void *priv)
466{
467 struct ums_softc *sc = priv;
984263bc 468
4e01b467 469 crit_enter();
984263bc 470 ums_add_to_queue(sc, 0, 0, 0, 0);
4e01b467 471 crit_exit();
984263bc
MD
472}
473
6ed427ca 474static void
984263bc
MD
475ums_add_to_queue(struct ums_softc *sc, int dx, int dy, int dz, int buttons)
476{
477 /* Discard data in case of full buffer */
478 if (sc->qhead+sc->mode.packetsize > sizeof(sc->qbuf)) {
479 DPRINTF(("Buffer full, discarded packet"));
480 return;
481 }
482
483 if (dx > 254) dx = 254;
484 if (dx < -256) dx = -256;
485 if (dy > 254) dy = 254;
486 if (dy < -256) dy = -256;
487 if (dz > 126) dz = 126;
488 if (dz < -128) dz = -128;
489
490 sc->qbuf[sc->qhead] = sc->mode.syncmask[1];
491 sc->qbuf[sc->qhead] |= ~buttons & MOUSE_MSC_BUTTONS;
492 sc->qbuf[sc->qhead+1] = dx >> 1;
493 sc->qbuf[sc->qhead+2] = dy >> 1;
494 sc->qbuf[sc->qhead+3] = dx - (dx >> 1);
495 sc->qbuf[sc->qhead+4] = dy - (dy >> 1);
496
497 if (sc->mode.level == 1) {
498 sc->qbuf[sc->qhead+5] = dz >> 1;
499 sc->qbuf[sc->qhead+6] = dz - (dz >> 1);
500 sc->qbuf[sc->qhead+7] = ((~buttons >> 3)
501 & MOUSE_SYS_EXTBUTTONS);
502 }
503
504 sc->qhead += sc->mode.packetsize;
505 sc->qcount += sc->mode.packetsize;
506 /* wrap round at end of buffer */
507 if (sc->qhead >= sizeof(sc->qbuf))
508 sc->qhead = 0;
509
510 /* someone waiting for data */
511 if (sc->state & UMS_ASLEEP) {
512 sc->state &= ~UMS_ASLEEP;
513 wakeup(sc);
514 }
5b22f1a7 515 KNOTE(&sc->rkq.ki_note, 0);
984263bc 516}
c436375a 517
6ed427ca 518static int
c436375a 519ums_enable(void *v)
984263bc
MD
520{
521 struct ums_softc *sc = v;
522
523 usbd_status err;
524
525 if (sc->sc_enabled)
526 return EBUSY;
527
528 sc->sc_enabled = 1;
529 sc->qcount = 0;
530 sc->qhead = sc->qtail = 0;
531 sc->status.flags = 0;
532 sc->status.button = sc->status.obutton = 0;
533 sc->status.dx = sc->status.dy = sc->status.dz = 0;
534
8c64e381 535 callout_init(&sc->sc_timeout);
984263bc
MD
536
537 /* Set up interrupt pipe. */
1550dfd9
MD
538 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
539 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
984263bc
MD
540 sc->sc_ibuf, sc->sc_isize, ums_intr,
541 USBD_DEFAULT_INTERVAL);
542 if (err) {
543 DPRINTF(("ums_enable: usbd_open_pipe_intr failed, error=%d\n",
544 err));
545 sc->sc_enabled = 0;
546 return (EIO);
547 }
548 return (0);
549}
550
6ed427ca 551static void
c436375a 552ums_disable(void *priv)
984263bc
MD
553{
554 struct ums_softc *sc = priv;
555
8c64e381 556 callout_stop(&sc->sc_timeout);
984263bc
MD
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 (sc->qcount != 0)
565 DPRINTF(("Discarded %d bytes in queue\n", sc->qcount));
566}
567
6ed427ca 568static int
fef8985e 569ums_open(struct dev_open_args *ap)
984263bc 570{
b13267a5 571 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
572 struct ums_softc *sc;
573
1e089e7e
HT
574 sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
575 if (sc == NULL)
576 return (ENXIO);
984263bc
MD
577
578 return ums_enable(sc);
579}
580
6ed427ca 581static int
fef8985e 582ums_close(struct dev_close_args *ap)
984263bc 583{
b13267a5 584 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
585 struct ums_softc *sc;
586
1e089e7e 587 sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
984263bc
MD
588
589 if (!sc)
590 return 0;
591
592 if (sc->sc_enabled)
593 ums_disable(sc);
594
595 return 0;
596}
597
6ed427ca 598static int
fef8985e 599ums_read(struct dev_read_args *ap)
984263bc 600{
b13267a5 601 cdev_t dev = ap->a_head.a_dev;
fef8985e 602 struct uio *uio = ap->a_uio;
984263bc 603 struct ums_softc *sc;
984263bc
MD
604 char buf[sizeof(sc->qbuf)];
605 int l = 0;
606 int error;
607
1e089e7e 608 sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
984263bc 609
4e01b467 610 crit_enter();
984263bc 611 if (!sc) {
4e01b467 612 crit_exit();
984263bc
MD
613 return EIO;
614 }
615
616 while (sc->qcount == 0 ) {
fef8985e 617 if (ap->a_ioflag & IO_NDELAY) { /* non-blocking I/O */
4e01b467 618 crit_exit();
984263bc
MD
619 return EWOULDBLOCK;
620 }
1550dfd9 621
984263bc 622 sc->state |= UMS_ASLEEP; /* blocking I/O */
377d4740 623 error = tsleep(sc, PCATCH, "umsrea", 0);
984263bc 624 if (error) {
4e01b467 625 crit_exit();
984263bc
MD
626 return error;
627 } else if (!sc->sc_enabled) {
4e01b467 628 crit_exit();
984263bc
MD
629 return EINTR;
630 }
631 /* check whether the device is still there */
632
633 sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
634 if (!sc) {
4e01b467 635 crit_exit();
984263bc
MD
636 return EIO;
637 }
638 }
639
640 /*
4e01b467
MD
641 * The writer process only extends qcount and qtail. We could copy
642 * them and use the copies to do the copying out of the queue.
984263bc
MD
643 */
644
645 while ((sc->qcount > 0) && (uio->uio_resid > 0)) {
646 l = (sc->qcount < uio->uio_resid? sc->qcount:uio->uio_resid);
647 if (l > sizeof(buf))
648 l = sizeof(buf);
649 if (l > sizeof(sc->qbuf) - sc->qtail) /* transfer till end of buf */
650 l = sizeof(sc->qbuf) - sc->qtail;
651
4e01b467 652 crit_exit();
984263bc 653 uiomove(&sc->qbuf[sc->qtail], l, uio);
4e01b467 654 crit_enter();
984263bc
MD
655
656 if ( sc->qcount - l < 0 ) {
657 DPRINTF(("qcount below 0, count=%d l=%d\n", sc->qcount, l));
658 sc->qcount = l;
659 }
660 sc->qcount -= l; /* remove the bytes from the buffer */
661 sc->qtail = (sc->qtail + l) % sizeof(sc->qbuf);
662 }
4e01b467 663 crit_exit();
984263bc
MD
664
665 return 0;
666}
667
96bcde04 668static struct filterops ums_filtops =
4c91dbc9 669 { FILTEROP_ISFD, NULL, ums_filt_detach, ums_filt };
96bcde04
SG
670
671static int
672ums_kqfilter(struct dev_kqfilter_args *ap)
673{
674 cdev_t dev = ap->a_head.a_dev;
675 struct knote *kn = ap->a_kn;
676 struct ums_softc *sc;
677 struct klist *klist;
678
679 ap->a_result = 0;
680
681 switch (kn->kn_filter) {
682 case EVFILT_READ:
683 sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
684 kn->kn_fop = &ums_filtops;
685 kn->kn_hook = (caddr_t)sc;
686 break;
687 default:
b287d649 688 ap->a_result = EOPNOTSUPP;
96bcde04
SG
689 return (0);
690 }
691
5b22f1a7
SG
692 klist = &sc->rkq.ki_note;
693 knote_insert(klist, kn);
96bcde04
SG
694
695 return (0);
696}
697
698static void
699ums_filt_detach(struct knote *kn)
700{
701 struct ums_softc *sc = (struct ums_softc *)kn->kn_hook;
702 struct klist *klist;
703
5b22f1a7
SG
704 klist = &sc->rkq.ki_note;
705 knote_remove(klist, kn);
96bcde04
SG
706}
707
708static int
709ums_filt(struct knote *kn, long hint)
710{
711 struct ums_softc *sc = (struct ums_softc *)kn->kn_hook;
712 int ready = 0;
713
714 crit_enter();
715 if (sc->qcount)
716 ready = 1;
717 crit_exit();
718
719 return (ready);
720}
721
984263bc 722int
fef8985e 723ums_ioctl(struct dev_ioctl_args *ap)
984263bc 724{
b13267a5 725 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
726 struct ums_softc *sc;
727 int error = 0;
984263bc
MD
728 mousemode_t mode;
729
1e089e7e 730 sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
984263bc
MD
731
732 if (!sc)
733 return EIO;
734
fef8985e 735 switch(ap->a_cmd) {
984263bc 736 case MOUSE_GETHWINFO:
fef8985e 737 *(mousehw_t *)ap->a_data = sc->hw;
984263bc
MD
738 break;
739 case MOUSE_GETMODE:
fef8985e 740 *(mousemode_t *)ap->a_data = sc->mode;
984263bc
MD
741 break;
742 case MOUSE_SETMODE:
fef8985e 743 mode = *(mousemode_t *)ap->a_data;
984263bc
MD
744
745 if (mode.level == -1)
746 /* don't change the current setting */
747 ;
748 else if ((mode.level < 0) || (mode.level > 1))
749 return (EINVAL);
750
4e01b467 751 crit_enter();
984263bc
MD
752 sc->mode.level = mode.level;
753
754 if (sc->mode.level == 0) {
755 if (sc->nbuttons > MOUSE_MSC_MAXBUTTON)
756 sc->hw.buttons = MOUSE_MSC_MAXBUTTON;
757 else
758 sc->hw.buttons = sc->nbuttons;
759 sc->mode.protocol = MOUSE_PROTO_MSC;
760 sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
761 sc->mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
762 sc->mode.syncmask[1] = MOUSE_MSC_SYNC;
763 } else if (sc->mode.level == 1) {
764 if (sc->nbuttons > MOUSE_SYS_MAXBUTTON)
765 sc->hw.buttons = MOUSE_SYS_MAXBUTTON;
766 else
767 sc->hw.buttons = sc->nbuttons;
768 sc->mode.protocol = MOUSE_PROTO_SYSMOUSE;
769 sc->mode.packetsize = MOUSE_SYS_PACKETSIZE;
770 sc->mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
771 sc->mode.syncmask[1] = MOUSE_SYS_SYNC;
772 }
773
774 bzero(sc->qbuf, sizeof(sc->qbuf));
775 sc->qhead = sc->qtail = sc->qcount = 0;
4e01b467 776 crit_exit();
984263bc
MD
777
778 break;
779 case MOUSE_GETLEVEL:
fef8985e 780 *(int *)ap->a_data = sc->mode.level;
984263bc
MD
781 break;
782 case MOUSE_SETLEVEL:
fef8985e 783 if (*(int *)ap->a_data < 0 || *(int *)ap->a_data > 1)
984263bc
MD
784 return (EINVAL);
785
4e01b467 786 crit_enter();
fef8985e 787 sc->mode.level = *(int *)ap->a_data;
984263bc
MD
788
789 if (sc->mode.level == 0) {
790 if (sc->nbuttons > MOUSE_MSC_MAXBUTTON)
791 sc->hw.buttons = MOUSE_MSC_MAXBUTTON;
792 else
793 sc->hw.buttons = sc->nbuttons;
794 sc->mode.protocol = MOUSE_PROTO_MSC;
795 sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
796 sc->mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
797 sc->mode.syncmask[1] = MOUSE_MSC_SYNC;
798 } else if (sc->mode.level == 1) {
799 if (sc->nbuttons > MOUSE_SYS_MAXBUTTON)
800 sc->hw.buttons = MOUSE_SYS_MAXBUTTON;
801 else
802 sc->hw.buttons = sc->nbuttons;
803 sc->mode.protocol = MOUSE_PROTO_SYSMOUSE;
804 sc->mode.packetsize = MOUSE_SYS_PACKETSIZE;
805 sc->mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
806 sc->mode.syncmask[1] = MOUSE_SYS_SYNC;
807 }
808
809 bzero(sc->qbuf, sizeof(sc->qbuf));
810 sc->qhead = sc->qtail = sc->qcount = 0;
4e01b467 811 crit_exit();
984263bc
MD
812
813 break;
814 case MOUSE_GETSTATUS: {
fef8985e 815 mousestatus_t *status = (mousestatus_t *) ap->a_data;
984263bc 816
4e01b467 817 crit_enter();
984263bc
MD
818 *status = sc->status;
819 sc->status.obutton = sc->status.button;
820 sc->status.button = 0;
821 sc->status.dx = sc->status.dy = sc->status.dz = 0;
4e01b467 822 crit_exit();
984263bc
MD
823
824 if (status->dx || status->dy || status->dz)
825 status->flags |= MOUSE_POSCHANGED;
826 if (status->button != status->obutton)
827 status->flags |= MOUSE_BUTTONSCHANGED;
828 break;
829 }
830 default:
831 error = ENOTTY;
832 }
833
834 return error;
835}
836
837DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, usbd_driver_load, 0);