wsmouse.c revision 1.52.6.1 1 /* $NetBSD: wsmouse.c,v 1.52.6.1 2007/08/05 19:01:04 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julio M. Merino Vidal.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by Christopher G. Demetriou
53 * for the NetBSD Project.
54 * 4. The name of the author may not be used to endorse or promote products
55 * derived from this software without specific prior written permission
56 *
57 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
62 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
63 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
64 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
66 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 */
68
69 /*
70 * Copyright (c) 1992, 1993
71 * The Regents of the University of California. All rights reserved.
72 *
73 * This software was developed by the Computer Systems Engineering group
74 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
75 * contributed to Berkeley.
76 *
77 * All advertising materials mentioning features or use of this software
78 * must display the following acknowledgement:
79 * This product includes software developed by the University of
80 * California, Lawrence Berkeley Laboratory.
81 *
82 * Redistribution and use in source and binary forms, with or without
83 * modification, are permitted provided that the following conditions
84 * are met:
85 * 1. Redistributions of source code must retain the above copyright
86 * notice, this list of conditions and the following disclaimer.
87 * 2. Redistributions in binary form must reproduce the above copyright
88 * notice, this list of conditions and the following disclaimer in the
89 * documentation and/or other materials provided with the distribution.
90 * 3. Neither the name of the University nor the names of its contributors
91 * may be used to endorse or promote products derived from this software
92 * without specific prior written permission.
93 *
94 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
95 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
97 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
98 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
99 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
100 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
101 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
102 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
103 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
104 * SUCH DAMAGE.
105 *
106 * @(#)ms.c 8.1 (Berkeley) 6/11/93
107 */
108
109 /*
110 * Mouse driver.
111 */
112
113 #include <sys/cdefs.h>
114 __KERNEL_RCSID(0, "$NetBSD: wsmouse.c,v 1.52.6.1 2007/08/05 19:01:04 jmcneill Exp $");
115
116 #include "wsmouse.h"
117 #include "wsdisplay.h"
118 #include "wsmux.h"
119
120 #include <sys/param.h>
121 #include <sys/conf.h>
122 #include <sys/ioctl.h>
123 #include <sys/poll.h>
124 #include <sys/fcntl.h>
125 #include <sys/kernel.h>
126 #include <sys/proc.h>
127 #include <sys/syslog.h>
128 #include <sys/systm.h>
129 #include <sys/tty.h>
130 #include <sys/signalvar.h>
131 #include <sys/device.h>
132 #include <sys/vnode.h>
133 #include <sys/callout.h>
134 #include <sys/malloc.h>
135
136 #include <dev/wscons/wsconsio.h>
137 #include <dev/wscons/wsmousevar.h>
138 #include <dev/wscons/wseventvar.h>
139 #include <dev/wscons/wsmuxvar.h>
140
141 #if defined(WSMUX_DEBUG) && NWSMUX > 0
142 #define DPRINTF(x) if (wsmuxdebug) printf x
143 #define DPRINTFN(n,x) if (wsmuxdebug > (n)) printf x
144 extern int wsmuxdebug;
145 #else
146 #define DPRINTF(x)
147 #define DPRINTFN(n,x)
148 #endif
149
150 #define INVALID_X INT_MAX
151 #define INVALID_Y INT_MAX
152 #define INVALID_Z INT_MAX
153
154 struct wsmouse_softc {
155 struct wsevsrc sc_base;
156
157 const struct wsmouse_accessops *sc_accessops;
158 void *sc_accesscookie;
159
160 u_int sc_mb; /* mouse button state */
161 u_int sc_ub; /* user button state */
162 int sc_dx; /* delta-x */
163 int sc_dy; /* delta-y */
164 int sc_dz; /* delta-z */
165 int sc_dw; /* delta-w */
166 int sc_x; /* absolute-x */
167 int sc_y; /* absolute-y */
168 int sc_z; /* absolute-z */
169 int sc_w; /* absolute-w */
170
171 int sc_refcnt;
172 u_char sc_dying; /* device is being detached */
173
174 struct wsmouse_repeat sc_repeat;
175 int sc_repeat_button;
176 callout_t sc_repeat_callout;
177 unsigned int sc_repeat_delay;
178 };
179
180 static int wsmouse_match(struct device *, struct cfdata *, void *);
181 static void wsmouse_attach(struct device *, struct device *, void *);
182 static int wsmouse_detach(struct device *, int);
183 static int wsmouse_activate(struct device *, enum devact);
184
185 static int wsmouse_do_ioctl(struct wsmouse_softc *, u_long, void *,
186 int, struct lwp *);
187
188 #if NWSMUX > 0
189 static int wsmouse_mux_open(struct wsevsrc *, struct wseventvar *);
190 static int wsmouse_mux_close(struct wsevsrc *);
191 #endif
192
193 static int wsmousedoioctl(struct device *, u_long, void *, int, struct lwp *);
194
195 static int wsmousedoopen(struct wsmouse_softc *, struct wseventvar *);
196
197 CFATTACH_DECL(wsmouse, sizeof (struct wsmouse_softc),
198 wsmouse_match, wsmouse_attach, wsmouse_detach, wsmouse_activate);
199
200 static void wsmouse_repeat(void *v);
201
202 extern struct cfdriver wsmouse_cd;
203
204 dev_type_open(wsmouseopen);
205 dev_type_close(wsmouseclose);
206 dev_type_read(wsmouseread);
207 dev_type_ioctl(wsmouseioctl);
208 dev_type_poll(wsmousepoll);
209 dev_type_kqfilter(wsmousekqfilter);
210
211 const struct cdevsw wsmouse_cdevsw = {
212 wsmouseopen, wsmouseclose, wsmouseread, nowrite, wsmouseioctl,
213 nostop, notty, wsmousepoll, nommap, wsmousekqfilter, D_OTHER
214 };
215
216 #if NWSMUX > 0
217 struct wssrcops wsmouse_srcops = {
218 WSMUX_MOUSE,
219 wsmouse_mux_open, wsmouse_mux_close, wsmousedoioctl, NULL, NULL
220 };
221 #endif
222
223 /*
224 * Print function (for parent devices).
225 */
226 int
227 wsmousedevprint(void *aux, const char *pnp)
228 {
229
230 if (pnp)
231 aprint_normal("wsmouse at %s", pnp);
232 return (UNCONF);
233 }
234
235 int
236 wsmouse_match(struct device *parent, struct cfdata *match,
237 void *aux)
238 {
239 return (1);
240 }
241
242 void
243 wsmouse_attach(struct device *parent, struct device *self, void *aux)
244 {
245 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
246 struct wsmousedev_attach_args *ap = aux;
247 #if NWSMUX > 0
248 int mux, error;
249 #endif
250
251 sc->sc_accessops = ap->accessops;
252 sc->sc_accesscookie = ap->accesscookie;
253
254 /* Initialize button repeating. */
255 memset(&sc->sc_repeat, 0, sizeof(sc->sc_repeat));
256 sc->sc_repeat_button = -1;
257 sc->sc_repeat_delay = 0;
258 callout_init(&sc->sc_repeat_callout, 0);
259
260 #if NWSMUX > 0
261 sc->sc_base.me_ops = &wsmouse_srcops;
262 mux = device_cfdata(&sc->sc_base.me_dv)->wsmousedevcf_mux;
263 if (mux >= 0) {
264 error = wsmux_attach_sc(wsmux_getmux(mux), &sc->sc_base);
265 if (error)
266 printf(" attach error=%d", error);
267 else
268 printf(" mux %d", mux);
269 }
270 #else
271 if (device_cfdata(&sc->sc_base.me_dv)->wsmousedevcf_mux >= 0)
272 printf(" (mux ignored)");
273 #endif
274
275 printf("\n");
276
277 (void)pnp_register(self, pnp_generic_power);
278 }
279
280 int
281 wsmouse_activate(struct device *self, enum devact act)
282 {
283 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
284
285 if (act == DVACT_DEACTIVATE)
286 sc->sc_dying = 1;
287 return (0);
288 }
289
290 /*
291 * Detach a mouse. To keep track of users of the softc we keep
292 * a reference count that's incremented while inside, e.g., read.
293 * If the mouse is active and the reference count is > 0 (0 is the
294 * normal state) we post an event and then wait for the process
295 * that had the reference to wake us up again. Then we blow away the
296 * vnode and return (which will deallocate the softc).
297 */
298 int
299 wsmouse_detach(struct device *self, int flags)
300 {
301 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
302 struct wseventvar *evar;
303 int maj, mn;
304 int s;
305
306 #if NWSMUX > 0
307 /* Tell parent mux we're leaving. */
308 if (sc->sc_base.me_parent != NULL) {
309 DPRINTF(("wsmouse_detach:\n"));
310 wsmux_detach_sc(&sc->sc_base);
311 }
312 #endif
313
314 /* If we're open ... */
315 evar = sc->sc_base.me_evp;
316 if (evar != NULL && evar->io != NULL) {
317 s = spltty();
318 if (--sc->sc_refcnt >= 0) {
319 struct wscons_event event;
320
321 /* Wake everyone by generating a dummy event. */
322 event.type = 0;
323 event.value = 0;
324 if (wsevent_inject(evar, &event, 1) != 0)
325 wsevent_wakeup(evar);
326
327 /* Wait for processes to go away. */
328 if (tsleep(sc, PZERO, "wsmdet", hz * 60))
329 printf("wsmouse_detach: %s didn't detach\n",
330 sc->sc_base.me_dv.dv_xname);
331 }
332 splx(s);
333 }
334
335 /* locate the major number */
336 maj = cdevsw_lookup_major(&wsmouse_cdevsw);
337
338 /* Nuke the vnodes for any open instances (calls close). */
339 mn = device_unit(self);
340 vdevgone(maj, mn, mn, VCHR);
341
342 return (0);
343 }
344
345 void
346 wsmouse_input(struct device *wsmousedev, u_int btns /* 0 is up */,
347 int x, int y, int z, int w, u_int flags)
348 {
349 struct wsmouse_softc *sc = (struct wsmouse_softc *)wsmousedev;
350 struct wseventvar *evar;
351 int mb, ub, d, nevents;
352 /* one for each dimension (4) + a bit for each button */
353 struct wscons_event events[4 + sizeof(d) * 8];
354
355 /*
356 * Discard input if not open.
357 */
358 evar = sc->sc_base.me_evp;
359 if (evar == NULL)
360 return;
361
362 #ifdef DIAGNOSTIC
363 if (evar->q == NULL) {
364 printf("wsmouse_input: evar->q=NULL\n");
365 return;
366 }
367 #endif
368
369 #if NWSMUX > 0
370 DPRINTFN(5,("wsmouse_input: %s mux=%p, evar=%p\n",
371 sc->sc_base.me_dv.dv_xname, sc->sc_base.me_parent, evar));
372 #endif
373
374 sc->sc_mb = btns;
375 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_X))
376 sc->sc_dx += x;
377 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_Y))
378 sc->sc_dy += y;
379 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_Z))
380 sc->sc_dz += z;
381 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_W))
382 sc->sc_dw += w;
383
384 /*
385 * We have at least one event (mouse button, delta-X, or
386 * delta-Y; possibly all three, and possibly three separate
387 * button events). Deliver these events until we are out
388 * of changes or out of room. As events get delivered,
389 * mark them `unchanged'.
390 */
391 ub = sc->sc_ub;
392 nevents = 0;
393
394 if (flags & WSMOUSE_INPUT_ABSOLUTE_X) {
395 if (sc->sc_x != x) {
396 events[nevents].type = WSCONS_EVENT_MOUSE_ABSOLUTE_X;
397 events[nevents].value = x;
398 nevents++;
399 }
400 } else {
401 if (sc->sc_dx) {
402 events[nevents].type = WSCONS_EVENT_MOUSE_DELTA_X;
403 events[nevents].value = sc->sc_dx;
404 nevents++;
405 }
406 }
407 if (flags & WSMOUSE_INPUT_ABSOLUTE_Y) {
408 if (sc->sc_y != y) {
409 events[nevents].type = WSCONS_EVENT_MOUSE_ABSOLUTE_Y;
410 events[nevents].value = y;
411 nevents++;
412 }
413 } else {
414 if (sc->sc_dy) {
415 events[nevents].type = WSCONS_EVENT_MOUSE_DELTA_Y;
416 events[nevents].value = sc->sc_dy;
417 nevents++;
418 }
419 }
420 if (flags & WSMOUSE_INPUT_ABSOLUTE_Z) {
421 if (sc->sc_z != z) {
422 events[nevents].type = WSCONS_EVENT_MOUSE_ABSOLUTE_Z;
423 events[nevents].value = z;
424 nevents++;
425 }
426 } else {
427 if (sc->sc_dz) {
428 events[nevents].type = WSCONS_EVENT_MOUSE_DELTA_Z;
429 events[nevents].value = sc->sc_dz;
430 nevents++;
431 }
432 }
433 if (flags & WSMOUSE_INPUT_ABSOLUTE_W) {
434 if (sc->sc_w != w) {
435 events[nevents].type = WSCONS_EVENT_MOUSE_ABSOLUTE_W;
436 events[nevents].value = w;
437 nevents++;
438 }
439 } else {
440 if (sc->sc_dw) {
441 events[nevents].type = WSCONS_EVENT_MOUSE_DELTA_W;
442 events[nevents].value = sc->sc_dw;
443 nevents++;
444 }
445 }
446
447 mb = sc->sc_mb;
448 while ((d = mb ^ ub) != 0) {
449 int btnno;
450
451 /*
452 * Cancel button repeating if button status changed.
453 */
454 if (sc->sc_repeat_button != -1) {
455 KASSERT(sc->sc_repeat_button >= 0);
456 KASSERT(sc->sc_repeat.wr_buttons &
457 (1 << sc->sc_repeat_button));
458 ub &= ~(1 << sc->sc_repeat_button);
459 sc->sc_repeat_button = -1;
460 callout_stop(&sc->sc_repeat_callout);
461 }
462
463 /*
464 * Mouse button change. Find the first change and drop
465 * it into the event queue.
466 */
467 btnno = ffs(d) - 1;
468 KASSERT(btnno >= 0);
469
470 if (nevents >= sizeof(events) / sizeof(events[0])) {
471 printf("%s: Event queue full (button status mb=0x%x"
472 " ub=0x%x)\n", sc->sc_base.me_dv.dv_xname, mb, ub);
473 break;
474 }
475
476 events[nevents].type =
477 (mb & d) ? WSCONS_EVENT_MOUSE_DOWN : WSCONS_EVENT_MOUSE_UP;
478 events[nevents].value = btnno;
479 nevents++;
480
481 ub ^= (1 << btnno);
482
483 /*
484 * Program button repeating if configured for this button.
485 */
486 if ((mb & d) && (sc->sc_repeat.wr_buttons & (1 << btnno)) &&
487 sc->sc_repeat.wr_delay_first > 0) {
488 sc->sc_repeat_button = btnno;
489 sc->sc_repeat_delay = sc->sc_repeat.wr_delay_first;
490 callout_reset(&sc->sc_repeat_callout,
491 (hz * sc->sc_repeat_delay) / 1000, wsmouse_repeat,
492 sc);
493 }
494 }
495
496 if (nevents == 0 || wsevent_inject(evar, events, nevents) == 0) {
497 /* All events were correctly injected into the queue.
498 * Synchronize the mouse's status with what the user
499 * has received. */
500 sc->sc_x = x; sc->sc_dx = 0;
501 sc->sc_y = y; sc->sc_dy = 0;
502 sc->sc_z = z; sc->sc_dz = 0;
503 sc->sc_w = w; sc->sc_dw = 0;
504 sc->sc_ub = ub;
505 #if NWSMUX > 0
506 DPRINTFN(5,("wsmouse_input: %s wakeup evar=%p\n",
507 sc->sc_base.me_dv.dv_xname, evar));
508 #endif
509 }
510 }
511
512 static void
513 wsmouse_repeat(void *v)
514 {
515 int oldspl;
516 unsigned int newdelay;
517 struct wsmouse_softc *sc;
518 struct wscons_event events[2];
519
520 oldspl = spltty();
521 sc = (struct wsmouse_softc *)v;
522
523 if (sc->sc_repeat_button == -1) {
524 /* Race condition: a "button up" event came in when
525 * this function was already called but did not do
526 * spltty() yet. */
527 splx(oldspl);
528 return;
529 }
530 KASSERT(sc->sc_repeat_button >= 0);
531
532 KASSERT(sc->sc_repeat.wr_buttons & (1 << sc->sc_repeat_button));
533
534 newdelay = sc->sc_repeat_delay;
535
536 events[0].type = WSCONS_EVENT_MOUSE_UP;
537 events[0].value = sc->sc_repeat_button;
538 events[1].type = WSCONS_EVENT_MOUSE_DOWN;
539 events[1].value = sc->sc_repeat_button;
540
541 if (wsevent_inject(sc->sc_base.me_evp, events, 2) == 0) {
542 sc->sc_ub = 1 << sc->sc_repeat_button;
543
544 if (newdelay - sc->sc_repeat.wr_delay_decrement <
545 sc->sc_repeat.wr_delay_minimum)
546 newdelay = sc->sc_repeat.wr_delay_minimum;
547 else if (newdelay > sc->sc_repeat.wr_delay_minimum)
548 newdelay -= sc->sc_repeat.wr_delay_decrement;
549 KASSERT(newdelay >= sc->sc_repeat.wr_delay_minimum &&
550 newdelay <= sc->sc_repeat.wr_delay_first);
551 }
552
553 /*
554 * Reprogram the repeating event.
555 */
556 sc->sc_repeat_delay = newdelay;
557 callout_reset(&sc->sc_repeat_callout, (hz * newdelay) / 1000,
558 wsmouse_repeat, sc);
559
560 splx(oldspl);
561 }
562
563 int
564 wsmouseopen(dev_t dev, int flags, int mode, struct lwp *l)
565 {
566 struct wsmouse_softc *sc;
567 struct wseventvar *evar;
568 int error, unit;
569
570 unit = minor(dev);
571 if (unit >= wsmouse_cd.cd_ndevs || /* make sure it was attached */
572 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
573 return (ENXIO);
574
575 #if NWSMUX > 0
576 DPRINTF(("wsmouseopen: %s mux=%p p=%p\n", sc->sc_base.me_dv.dv_xname,
577 sc->sc_base.me_parent, l));
578 #endif
579
580 if (sc->sc_dying)
581 return (EIO);
582
583 if ((flags & (FREAD | FWRITE)) == FWRITE)
584 return (0); /* always allow open for write
585 so ioctl() is possible. */
586
587 if (sc->sc_base.me_evp != NULL)
588 return (EBUSY);
589
590 evar = &sc->sc_base.me_evar;
591 wsevent_init(evar, l->l_proc);
592 sc->sc_base.me_evp = evar;
593
594 error = wsmousedoopen(sc, evar);
595 if (error) {
596 DPRINTF(("wsmouseopen: %s open failed\n",
597 sc->sc_base.me_dv.dv_xname));
598 sc->sc_base.me_evp = NULL;
599 wsevent_fini(evar);
600 }
601 return (error);
602 }
603
604 int
605 wsmouseclose(dev_t dev, int flags, int mode,
606 struct lwp *l)
607 {
608 struct wsmouse_softc *sc =
609 (struct wsmouse_softc *)wsmouse_cd.cd_devs[minor(dev)];
610 struct wseventvar *evar = sc->sc_base.me_evp;
611
612 if (evar == NULL)
613 /* not open for read */
614 return (0);
615 sc->sc_base.me_evp = NULL;
616 (*sc->sc_accessops->disable)(sc->sc_accesscookie);
617 wsevent_fini(evar);
618
619 return (0);
620 }
621
622 int
623 wsmousedoopen(struct wsmouse_softc *sc, struct wseventvar *evp)
624 {
625 sc->sc_base.me_evp = evp;
626 sc->sc_x = INVALID_X;
627 sc->sc_y = INVALID_Y;
628 sc->sc_z = INVALID_Z;
629
630 /* Stop button repeating when messing with the device. */
631 if (sc->sc_repeat_button != -1) {
632 KASSERT(sc->sc_repeat_button >= 0);
633 sc->sc_repeat_button = -1;
634 callout_stop(&sc->sc_repeat_callout);
635 }
636
637 /* enable the device, and punt if that's not possible */
638 return (*sc->sc_accessops->enable)(sc->sc_accesscookie);
639 }
640
641 int
642 wsmouseread(dev_t dev, struct uio *uio, int flags)
643 {
644 struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
645 int error;
646
647 if (sc->sc_dying)
648 return (EIO);
649
650 #ifdef DIAGNOSTIC
651 if (sc->sc_base.me_evp == NULL) {
652 printf("wsmouseread: evp == NULL\n");
653 return (EINVAL);
654 }
655 #endif
656
657 sc->sc_refcnt++;
658 error = wsevent_read(sc->sc_base.me_evp, uio, flags);
659 if (--sc->sc_refcnt < 0) {
660 wakeup(sc);
661 error = EIO;
662 }
663 return (error);
664 }
665
666 int
667 wsmouseioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
668 {
669 return (wsmousedoioctl(wsmouse_cd.cd_devs[minor(dev)],
670 cmd, data, flag, l));
671 }
672
673 /* A wrapper around the ioctl() workhorse to make reference counting easy. */
674 int
675 wsmousedoioctl(struct device *dv, u_long cmd, void *data, int flag,
676 struct lwp *l)
677 {
678 struct wsmouse_softc *sc = (struct wsmouse_softc *)dv;
679 int error;
680
681 sc->sc_refcnt++;
682 error = wsmouse_do_ioctl(sc, cmd, data, flag, l);
683 if (--sc->sc_refcnt < 0)
684 wakeup(sc);
685 return (error);
686 }
687
688 int
689 wsmouse_do_ioctl(struct wsmouse_softc *sc, u_long cmd, void *data,
690 int flag, struct lwp *l)
691 {
692 int error;
693 struct wsmouse_repeat *wr;
694
695 if (sc->sc_dying)
696 return (EIO);
697
698 /*
699 * Try the generic ioctls that the wsmouse interface supports.
700 */
701 switch (cmd) {
702 case FIONBIO: /* we will remove this someday (soon???) */
703 return (0);
704
705 case FIOASYNC:
706 if (sc->sc_base.me_evp == NULL)
707 return (EINVAL);
708 sc->sc_base.me_evp->async = *(int *)data != 0;
709 return (0);
710
711 case FIOSETOWN:
712 if (sc->sc_base.me_evp == NULL)
713 return (EINVAL);
714 if (-*(int *)data != sc->sc_base.me_evp->io->p_pgid
715 && *(int *)data != sc->sc_base.me_evp->io->p_pid)
716 return (EPERM);
717 return (0);
718
719 case TIOCSPGRP:
720 if (sc->sc_base.me_evp == NULL)
721 return (EINVAL);
722 if (*(int *)data != sc->sc_base.me_evp->io->p_pgid)
723 return (EPERM);
724 return (0);
725 }
726
727 /*
728 * Try the wsmouse specific ioctls.
729 */
730 switch (cmd) {
731 case WSMOUSEIO_GETREPEAT:
732 wr = (struct wsmouse_repeat *)data;
733 memcpy(wr, &sc->sc_repeat, sizeof(sc->sc_repeat));
734 return 0;
735
736 case WSMOUSEIO_SETREPEAT:
737 if ((flag & FWRITE) == 0)
738 return EACCES;
739
740 /* Validate input data. */
741 wr = (struct wsmouse_repeat *)data;
742 if (wr->wr_delay_first != 0 &&
743 (wr->wr_delay_first < wr->wr_delay_decrement ||
744 wr->wr_delay_first < wr->wr_delay_minimum ||
745 wr->wr_delay_first < wr->wr_delay_minimum +
746 wr->wr_delay_decrement))
747 return EINVAL;
748
749 /* Stop current repeating and set new data. */
750 sc->sc_repeat_button = -1;
751 callout_stop(&sc->sc_repeat_callout);
752 memcpy(&sc->sc_repeat, wr, sizeof(sc->sc_repeat));
753
754 return 0;
755 }
756
757 /*
758 * Try the mouse driver for WSMOUSEIO ioctls. It returns -1
759 * if it didn't recognize the request.
760 */
761 error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd,
762 data, flag, l);
763 return (error); /* may be EPASSTHROUGH */
764 }
765
766 int
767 wsmousepoll(dev_t dev, int events, struct lwp *l)
768 {
769 struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
770
771 if (sc->sc_base.me_evp == NULL)
772 return (POLLERR);
773 return (wsevent_poll(sc->sc_base.me_evp, events, l));
774 }
775
776 int
777 wsmousekqfilter(dev_t dev, struct knote *kn)
778 {
779 struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
780
781 if (sc->sc_base.me_evp == NULL)
782 return (1);
783 return (wsevent_kqfilter(sc->sc_base.me_evp, kn));
784 }
785
786 #if NWSMUX > 0
787 int
788 wsmouse_mux_open(struct wsevsrc *me, struct wseventvar *evp)
789 {
790 struct wsmouse_softc *sc = (struct wsmouse_softc *)me;
791
792 if (sc->sc_base.me_evp != NULL)
793 return (EBUSY);
794
795 return wsmousedoopen(sc, evp);
796 }
797
798 int
799 wsmouse_mux_close(struct wsevsrc *me)
800 {
801 struct wsmouse_softc *sc = (struct wsmouse_softc *)me;
802
803 sc->sc_base.me_evp = NULL;
804 (*sc->sc_accessops->disable)(sc->sc_accesscookie);
805
806 return (0);
807 }
808
809 int
810 wsmouse_add_mux(int unit, struct wsmux_softc *muxsc)
811 {
812 struct wsmouse_softc *sc;
813
814 if (unit < 0 || unit >= wsmouse_cd.cd_ndevs ||
815 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
816 return (ENXIO);
817
818 if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL)
819 return (EBUSY);
820
821 return (wsmux_attach_sc(muxsc, &sc->sc_base));
822 }
823 #endif /* NWSMUX > 0 */
824