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