wsmouse.c revision 1.40 1 /* $NetBSD: wsmouse.c,v 1.40 2006/02/07 09:13:02 jmmv 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.40 2006/02/07 09:13:02 jmmv 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 struct callout 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, caddr_t,
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, caddr_t, 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,
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, void *aux)
237 {
238 return (1);
239 }
240
241 void
242 wsmouse_attach(struct device *parent, struct device *self, void *aux)
243 {
244 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
245 struct wsmousedev_attach_args *ap = aux;
246 #if NWSMUX > 0
247 int mux, error;
248 #endif
249
250 sc->sc_accessops = ap->accessops;
251 sc->sc_accesscookie = ap->accesscookie;
252
253 /* Initialize button repeating. */
254 memset(&sc->sc_repeat, 0, sizeof(sc->sc_repeat));
255 sc->sc_repeat_button = -1;
256 sc->sc_repeat_delay = 0;
257 callout_init(&sc->sc_repeat_callout);
258
259 #if NWSMUX > 0
260 sc->sc_base.me_ops = &wsmouse_srcops;
261 mux = sc->sc_base.me_dv.dv_cfdata->wsmousedevcf_mux;
262 if (mux >= 0) {
263 error = wsmux_attach_sc(wsmux_getmux(mux), &sc->sc_base);
264 if (error)
265 printf(" attach error=%d", error);
266 else
267 printf(" mux %d", mux);
268 }
269 #else
270 if (sc->sc_base.me_dv.dv_cfdata->wsmousedevcf_mux >= 0)
271 printf(" (mux ignored)");
272 #endif
273
274 printf("\n");
275 }
276
277 int
278 wsmouse_activate(struct device *self, enum devact act)
279 {
280 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
281
282 if (act == DVACT_DEACTIVATE)
283 sc->sc_dying = 1;
284 return (0);
285 }
286
287 /*
288 * Detach a mouse. To keep track of users of the softc we keep
289 * a reference count that's incremented while inside, e.g., read.
290 * If the mouse is active and the reference count is > 0 (0 is the
291 * normal state) we post an event and then wait for the process
292 * that had the reference to wake us up again. Then we blow away the
293 * vnode and return (which will deallocate the softc).
294 */
295 int
296 wsmouse_detach(struct device *self, int flags)
297 {
298 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
299 struct wseventvar *evar;
300 int maj, mn;
301 int s;
302
303 #if NWSMUX > 0
304 /* Tell parent mux we're leaving. */
305 if (sc->sc_base.me_parent != NULL) {
306 DPRINTF(("wsmouse_detach:\n"));
307 wsmux_detach_sc(&sc->sc_base);
308 }
309 #endif
310
311 /* If we're open ... */
312 evar = sc->sc_base.me_evp;
313 if (evar != NULL && evar->io != NULL) {
314 s = spltty();
315 if (--sc->sc_refcnt >= 0) {
316 struct wscons_event event;
317
318 /* Wake everyone by generating a dummy event. */
319 event.type = 0;
320 event.value = 0;
321 if (wsevent_inject(evar, &event, 1) != 0)
322 wsevent_wakeup(evar);
323
324 /* Wait for processes to go away. */
325 if (tsleep(sc, PZERO, "wsmdet", hz * 60))
326 printf("wsmouse_detach: %s didn't detach\n",
327 sc->sc_base.me_dv.dv_xname);
328 }
329 splx(s);
330 }
331
332 /* locate the major number */
333 maj = cdevsw_lookup_major(&wsmouse_cdevsw);
334
335 /* Nuke the vnodes for any open instances (calls close). */
336 mn = self->dv_unit;
337 vdevgone(maj, mn, mn, VCHR);
338
339 return (0);
340 }
341
342 void
343 wsmouse_input_xyzw(struct device *wsmousedev, u_int btns /* 0 is up */,
344 int x, int y, int z, int w, u_int flags)
345 {
346 struct wsmouse_softc *sc = (struct wsmouse_softc *)wsmousedev;
347 struct wseventvar *evar;
348 int mb, ub, d, nevents;
349 struct wscons_event events[4 + sizeof(d) * 8];
350
351 /*
352 * Discard input if not open.
353 */
354 evar = sc->sc_base.me_evp;
355 if (evar == NULL)
356 return;
357
358 #ifdef DIAGNOSTIC
359 if (evar->q == NULL) {
360 printf("wsmouse_input: evar->q=NULL\n");
361 return;
362 }
363 #endif
364
365 #if NWSMUX > 0
366 DPRINTFN(5,("wsmouse_input: %s mux=%p, evar=%p\n",
367 sc->sc_base.me_dv.dv_xname, sc->sc_base.me_parent, evar));
368 #endif
369
370 sc->sc_mb = btns;
371 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_X))
372 sc->sc_dx += x;
373 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_Y))
374 sc->sc_dy += y;
375 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_Z))
376 sc->sc_dz += z;
377 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_W))
378 sc->sc_dw += w;
379
380 /*
381 * We have at least one event (mouse button, delta-X, or
382 * delta-Y; possibly all three, and possibly three separate
383 * button events). Deliver these events until we are out
384 * of changes or out of room. As events get delivered,
385 * mark them `unchanged'.
386 */
387 ub = sc->sc_ub;
388 nevents = 0;
389
390 if (flags & WSMOUSE_INPUT_ABSOLUTE_X) {
391 if (sc->sc_x != x) {
392 events[nevents].type = WSCONS_EVENT_MOUSE_ABSOLUTE_X;
393 events[nevents].value = x;
394 nevents++;
395 }
396 } else {
397 if (sc->sc_dx) {
398 events[nevents].type = WSCONS_EVENT_MOUSE_DELTA_X;
399 events[nevents].value = sc->sc_dx;
400 nevents++;
401 }
402 }
403 if (flags & WSMOUSE_INPUT_ABSOLUTE_Y) {
404 if (sc->sc_y != y) {
405 events[nevents].type = WSCONS_EVENT_MOUSE_ABSOLUTE_Y;
406 events[nevents].value = y;
407 nevents++;
408 }
409 } else {
410 if (sc->sc_dy) {
411 events[nevents].type = WSCONS_EVENT_MOUSE_DELTA_Y;
412 events[nevents].value = sc->sc_dy;
413 nevents++;
414 }
415 }
416 if (flags & WSMOUSE_INPUT_ABSOLUTE_Z) {
417 if (sc->sc_z != z) {
418 events[nevents].type = WSCONS_EVENT_MOUSE_ABSOLUTE_Z;
419 events[nevents].value = z;
420 nevents++;
421 }
422 } else {
423 if (sc->sc_dz) {
424 events[nevents].type = WSCONS_EVENT_MOUSE_DELTA_Z;
425 events[nevents].value = sc->sc_dz;
426 nevents++;
427 }
428 }
429 if (flags & WSMOUSE_INPUT_ABSOLUTE_W) {
430 if (sc->sc_w != w) {
431 events[nevents].type = WSCONS_EVENT_MOUSE_ABSOLUTE_W;
432 events[nevents].value = w;
433 nevents++;
434 }
435 } else {
436 if (sc->sc_dw) {
437 events[nevents].type = WSCONS_EVENT_MOUSE_DELTA_W;
438 events[nevents].value = sc->sc_dw;
439 nevents++;
440 }
441 }
442
443 mb = sc->sc_mb;
444 while ((d = mb ^ ub) != 0) {
445 int btnno;
446
447 /*
448 * Cancel button repeating if button status changed.
449 */
450 if (sc->sc_repeat_button != -1) {
451 KASSERT(sc->sc_repeat_button >= 0);
452 KASSERT(sc->sc_repeat.wr_buttons &
453 (1 << sc->sc_repeat_button));
454 ub &= ~(1 << sc->sc_repeat_button);
455 sc->sc_repeat_button = -1;
456 callout_stop(&sc->sc_repeat_callout);
457 }
458
459 /*
460 * Mouse button change. Find the first change and drop
461 * it into the event queue.
462 */
463 btnno = ffs(d) - 1;
464 KASSERT(btnno >= 0);
465
466 events[nevents].type =
467 (mb & d) ? WSCONS_EVENT_MOUSE_DOWN : WSCONS_EVENT_MOUSE_UP;
468 events[nevents].value = btnno;
469 nevents++;
470
471 ub ^= (1 << btnno);
472
473 /*
474 * Program button repeating if configured for this button.
475 */
476 if ((mb & d) && (sc->sc_repeat.wr_buttons & (1 << btnno)) &&
477 sc->sc_repeat.wr_delay_first > 0) {
478 sc->sc_repeat_button = btnno;
479 sc->sc_repeat_delay = sc->sc_repeat.wr_delay_first;
480 callout_reset(&sc->sc_repeat_callout,
481 (hz * sc->sc_repeat_delay) / 1000, wsmouse_repeat,
482 sc);
483 }
484 }
485
486 KASSERT(nevents > 0 &&
487 nevents <= sizeof(events) / sizeof(struct wscons_event));
488
489 if (wsevent_inject(evar, events, nevents) == 0) {
490 /* All events were correctly injected into the queue.
491 * Synchronize the mouse's status with what the user
492 * has received. */
493 sc->sc_x = x; sc->sc_dx = 0;
494 sc->sc_y = y; sc->sc_dy = 0;
495 sc->sc_z = z; sc->sc_dz = 0;
496 sc->sc_w = w; sc->sc_dw = 0;
497 sc->sc_ub = ub;
498 #if NWSMUX > 0
499 DPRINTFN(5,("wsmouse_input: %s wakeup evar=%p\n",
500 sc->sc_base.me_dv.dv_xname, evar));
501 #endif
502 }
503 }
504
505 static void
506 wsmouse_repeat(void *v)
507 {
508 int oldspl;
509 unsigned int newdelay;
510 struct wsmouse_softc *sc;
511 struct wscons_event events[2];
512
513 oldspl = spltty();
514 sc = (struct wsmouse_softc *)v;
515
516 if (sc->sc_repeat_button == -1) {
517 /* Race condition: a "button up" event came in when
518 * this function was already called but did not do
519 * spltty() yet. */
520 splx(oldspl);
521 return;
522 }
523 KASSERT(sc->sc_repeat_button >= 0);
524
525 KASSERT(sc->sc_repeat.wr_buttons & (1 << sc->sc_repeat_button));
526
527 newdelay = sc->sc_repeat_delay;
528
529 events[0].type = WSCONS_EVENT_MOUSE_UP;
530 events[0].value = sc->sc_repeat_button;
531 events[1].type = WSCONS_EVENT_MOUSE_DOWN;
532 events[1].value = sc->sc_repeat_button;
533
534 if (wsevent_inject(sc->sc_base.me_evp, events, 2) == 0) {
535 sc->sc_ub = 1 << sc->sc_repeat_button;
536
537 if (newdelay - sc->sc_repeat.wr_delay_decrement <
538 sc->sc_repeat.wr_delay_minimum)
539 newdelay = sc->sc_repeat.wr_delay_minimum;
540 else if (newdelay > sc->sc_repeat.wr_delay_minimum)
541 newdelay -= sc->sc_repeat.wr_delay_decrement;
542 KASSERT(newdelay >= sc->sc_repeat.wr_delay_minimum &&
543 newdelay <= sc->sc_repeat.wr_delay_first);
544 }
545
546 /*
547 * Reprogram the repeating event.
548 */
549 sc->sc_repeat_delay = newdelay;
550 callout_reset(&sc->sc_repeat_callout, (hz * newdelay) / 1000,
551 wsmouse_repeat, sc);
552
553 splx(oldspl);
554 }
555
556 int
557 wsmouseopen(dev_t dev, int flags, int mode, struct lwp *l)
558 {
559 struct wsmouse_softc *sc;
560 struct wseventvar *evar;
561 int error, unit;
562
563 unit = minor(dev);
564 if (unit >= wsmouse_cd.cd_ndevs || /* make sure it was attached */
565 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
566 return (ENXIO);
567
568 #if NWSMUX > 0
569 DPRINTF(("wsmouseopen: %s mux=%p p=%p\n", sc->sc_base.me_dv.dv_xname,
570 sc->sc_base.me_parent, l));
571 #endif
572
573 if (sc->sc_dying)
574 return (EIO);
575
576 if ((flags & (FREAD | FWRITE)) == FWRITE)
577 return (0); /* always allow open for write
578 so ioctl() is possible. */
579
580 if (sc->sc_base.me_evp != NULL)
581 return (EBUSY);
582
583 evar = &sc->sc_base.me_evar;
584 wsevent_init(evar, l->l_proc);
585 sc->sc_base.me_evp = evar;
586
587 error = wsmousedoopen(sc, evar);
588 if (error) {
589 DPRINTF(("wsmouseopen: %s open failed\n",
590 sc->sc_base.me_dv.dv_xname));
591 sc->sc_base.me_evp = NULL;
592 wsevent_fini(evar);
593 }
594 return (error);
595 }
596
597 int
598 wsmouseclose(dev_t dev, int flags, int mode, struct lwp *l)
599 {
600 struct wsmouse_softc *sc =
601 (struct wsmouse_softc *)wsmouse_cd.cd_devs[minor(dev)];
602 struct wseventvar *evar = sc->sc_base.me_evp;
603
604 if (evar == NULL)
605 /* not open for read */
606 return (0);
607 sc->sc_base.me_evp = NULL;
608 (*sc->sc_accessops->disable)(sc->sc_accesscookie);
609 wsevent_fini(evar);
610
611 return (0);
612 }
613
614 int
615 wsmousedoopen(struct wsmouse_softc *sc, struct wseventvar *evp)
616 {
617 sc->sc_base.me_evp = evp;
618 sc->sc_x = INVALID_X;
619 sc->sc_y = INVALID_Y;
620 sc->sc_z = INVALID_Z;
621
622 /* Stop button repeating when messing with the device. */
623 if (sc->sc_repeat_button != -1) {
624 KASSERT(sc->sc_repeat_button >= 0);
625 sc->sc_repeat_button = -1;
626 callout_stop(&sc->sc_repeat_callout);
627 }
628
629 /* enable the device, and punt if that's not possible */
630 return (*sc->sc_accessops->enable)(sc->sc_accesscookie);
631 }
632
633 int
634 wsmouseread(dev_t dev, struct uio *uio, int flags)
635 {
636 struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
637 int error;
638
639 if (sc->sc_dying)
640 return (EIO);
641
642 #ifdef DIAGNOSTIC
643 if (sc->sc_base.me_evp == NULL) {
644 printf("wsmouseread: evp == NULL\n");
645 return (EINVAL);
646 }
647 #endif
648
649 sc->sc_refcnt++;
650 error = wsevent_read(sc->sc_base.me_evp, uio, flags);
651 if (--sc->sc_refcnt < 0) {
652 wakeup(sc);
653 error = EIO;
654 }
655 return (error);
656 }
657
658 int
659 wsmouseioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
660 {
661 return (wsmousedoioctl(wsmouse_cd.cd_devs[minor(dev)],
662 cmd, data, flag, l));
663 }
664
665 /* A wrapper around the ioctl() workhorse to make reference counting easy. */
666 int
667 wsmousedoioctl(struct device *dv, u_long cmd, caddr_t data, int flag,
668 struct lwp *l)
669 {
670 struct wsmouse_softc *sc = (struct wsmouse_softc *)dv;
671 int error;
672
673 sc->sc_refcnt++;
674 error = wsmouse_do_ioctl(sc, cmd, data, flag, l);
675 if (--sc->sc_refcnt < 0)
676 wakeup(sc);
677 return (error);
678 }
679
680 int
681 wsmouse_do_ioctl(struct wsmouse_softc *sc, u_long cmd, caddr_t data,
682 int flag, struct lwp *l)
683 {
684 int error;
685 struct wsmouse_repeat *wr;
686
687 if (sc->sc_dying)
688 return (EIO);
689
690 /*
691 * Try the generic ioctls that the wsmouse interface supports.
692 */
693 switch (cmd) {
694 case FIONBIO: /* we will remove this someday (soon???) */
695 return (0);
696
697 case FIOASYNC:
698 if (sc->sc_base.me_evp == NULL)
699 return (EINVAL);
700 sc->sc_base.me_evp->async = *(int *)data != 0;
701 return (0);
702
703 case FIOSETOWN:
704 if (sc->sc_base.me_evp == NULL)
705 return (EINVAL);
706 if (-*(int *)data != sc->sc_base.me_evp->io->p_pgid
707 && *(int *)data != sc->sc_base.me_evp->io->p_pid)
708 return (EPERM);
709 return (0);
710
711 case TIOCSPGRP:
712 if (sc->sc_base.me_evp == NULL)
713 return (EINVAL);
714 if (*(int *)data != sc->sc_base.me_evp->io->p_pgid)
715 return (EPERM);
716 return (0);
717 }
718
719 /*
720 * Try the wsmouse specific ioctls.
721 */
722 switch (cmd) {
723 case WSMOUSEIO_GETREPEAT:
724 wr = (struct wsmouse_repeat *)data;
725 memcpy(wr, &sc->sc_repeat, sizeof(sc->sc_repeat));
726 return 0;
727
728 case WSMOUSEIO_SETREPEAT:
729 if ((flag & FWRITE) == 0)
730 return EACCES;
731
732 /* Validate input data. */
733 wr = (struct wsmouse_repeat *)data;
734 if (wr->wr_delay_first != 0 &&
735 (wr->wr_delay_first < wr->wr_delay_decrement ||
736 wr->wr_delay_first < wr->wr_delay_minimum ||
737 wr->wr_delay_first < wr->wr_delay_minimum +
738 wr->wr_delay_decrement))
739 return EINVAL;
740
741 /* Stop current repeating and set new data. */
742 sc->sc_repeat_button = -1;
743 callout_stop(&sc->sc_repeat_callout);
744 memcpy(&sc->sc_repeat, wr, sizeof(sc->sc_repeat));
745
746 return 0;
747 }
748
749 /*
750 * Try the mouse driver for WSMOUSEIO ioctls. It returns -1
751 * if it didn't recognize the request.
752 */
753 error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd,
754 data, flag, l);
755 return (error); /* may be EPASSTHROUGH */
756 }
757
758 int
759 wsmousepoll(dev_t dev, int events, struct lwp *l)
760 {
761 struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
762
763 if (sc->sc_base.me_evp == NULL)
764 return (POLLERR);
765 return (wsevent_poll(sc->sc_base.me_evp, events, l));
766 }
767
768 int
769 wsmousekqfilter(dev_t dev, struct knote *kn)
770 {
771 struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
772
773 if (sc->sc_base.me_evp == NULL)
774 return (1);
775 return (wsevent_kqfilter(sc->sc_base.me_evp, kn));
776 }
777
778 #if NWSMUX > 0
779 int
780 wsmouse_mux_open(struct wsevsrc *me, struct wseventvar *evp)
781 {
782 struct wsmouse_softc *sc = (struct wsmouse_softc *)me;
783
784 if (sc->sc_base.me_evp != NULL)
785 return (EBUSY);
786
787 return wsmousedoopen(sc, evp);
788 }
789
790 int
791 wsmouse_mux_close(struct wsevsrc *me)
792 {
793 struct wsmouse_softc *sc = (struct wsmouse_softc *)me;
794
795 sc->sc_base.me_evp = NULL;
796 (*sc->sc_accessops->disable)(sc->sc_accesscookie);
797
798 return (0);
799 }
800
801 int
802 wsmouse_add_mux(int unit, struct wsmux_softc *muxsc)
803 {
804 struct wsmouse_softc *sc;
805
806 if (unit < 0 || unit >= wsmouse_cd.cd_ndevs ||
807 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
808 return (ENXIO);
809
810 if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL)
811 return (EBUSY);
812
813 return (wsmux_attach_sc(muxsc, &sc->sc_base));
814 }
815 #endif /* NWSMUX > 0 */
816