wsmouse.c revision 1.9 1 /* $NetBSD: wsmouse.c,v 1.9 1999/08/04 11:26:04 augustss Exp $ */
2
3 /*
4 * Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christopher G. Demetriou
17 * for the NetBSD Project.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 static const char _copyright[] __attribute__ ((unused)) =
34 "Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.";
35 static const char _rcsid[] __attribute__ ((unused)) =
36 "$NetBSD: wsmouse.c,v 1.9 1999/08/04 11:26:04 augustss Exp $";
37
38 /*
39 * Copyright (c) 1992, 1993
40 * The Regents of the University of California. All rights reserved.
41 *
42 * This software was developed by the Computer Systems Engineering group
43 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
44 * contributed to Berkeley.
45 *
46 * All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Lawrence Berkeley Laboratory.
50 *
51 * Redistribution and use in source and binary forms, with or without
52 * modification, are permitted provided that the following conditions
53 * are met:
54 * 1. Redistributions of source code must retain the above copyright
55 * notice, this list of conditions and the following disclaimer.
56 * 2. Redistributions in binary form must reproduce the above copyright
57 * notice, this list of conditions and the following disclaimer in the
58 * documentation and/or other materials provided with the distribution.
59 * 3. All advertising materials mentioning features or use of this software
60 * must display the following acknowledgement:
61 * This product includes software developed by the University of
62 * California, Berkeley and its contributors.
63 * 4. Neither the name of the University nor the names of its contributors
64 * may be used to endorse or promote products derived from this software
65 * without specific prior written permission.
66 *
67 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
68 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
69 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
70 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
71 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
72 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
73 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
74 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
75 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
76 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77 * SUCH DAMAGE.
78 *
79 * @(#)ms.c 8.1 (Berkeley) 6/11/93
80 */
81
82 /*
83 * Mouse driver.
84 */
85
86 #include <sys/param.h>
87 #include <sys/conf.h>
88 #include <sys/ioctl.h>
89 #include <sys/fcntl.h>
90 #include <sys/kernel.h>
91 #include <sys/proc.h>
92 #include <sys/syslog.h>
93 #include <sys/systm.h>
94 #include <sys/tty.h>
95 #include <sys/signalvar.h>
96 #include <sys/device.h>
97 #include <sys/vnode.h>
98
99 #include <dev/wscons/wsconsio.h>
100 #include <dev/wscons/wsmousevar.h>
101 #include <dev/wscons/wseventvar.h>
102
103 #include "wsmouse.h"
104 #include "wsmux.h"
105 #include "wsdisplay.h"
106 #include "wskbd.h"
107
108 #if NWSMUX > 0
109 #include <dev/wscons/wsmuxvar.h>
110 #endif
111
112 struct wsmouse_softc {
113 struct device sc_dv;
114
115 const struct wsmouse_accessops *sc_accessops;
116 void *sc_accesscookie;
117
118 int sc_ready; /* accepting events */
119 struct wseventvar sc_events; /* event queue state */
120
121 u_int sc_mb; /* mouse button state */
122 u_int sc_ub; /* user button state */
123 int sc_dx; /* delta-x */
124 int sc_dy; /* delta-y */
125 int sc_dz; /* delta-z */
126
127 int sc_refcnt;
128 u_char sc_dying; /* device is being detached */
129
130 #if NWSMUX > 0
131 struct wsmux_softc *sc_mux;
132 #endif
133 };
134
135 int wsmouse_match __P((struct device *, struct cfdata *, void *));
136 void wsmouse_attach __P((struct device *, struct device *, void *));
137 int wsmouse_detach __P((struct device *, int));
138 int wsmouse_activate __P((struct device *, enum devact));
139
140 int wsmouse_do_ioctl __P((struct wsmouse_softc *, u_long, caddr_t,
141 int, struct proc *));
142
143 int wsmousedoclose __P((struct device *, int, int, struct proc *));
144 int wsmousedoioctl __P((struct device *, u_long, caddr_t, int,
145 struct proc *));
146
147 struct cfattach wsmouse_ca = {
148 sizeof (struct wsmouse_softc), wsmouse_match, wsmouse_attach,
149 wsmouse_detach, wsmouse_activate
150 };
151
152 #if NWSMOUSE > 0
153 extern struct cfdriver wsmouse_cd;
154 #endif /* NWSMOUSE > 0 */
155
156 cdev_decl(wsmouse);
157
158 #if NWSMUX > 0
159 struct wsmuxops wsmouse_muxops = {
160 wsmouseopen, wsmousedoclose, wsmousedoioctl, 0, 0
161 };
162 #endif
163
164 /*
165 * Print function (for parent devices).
166 */
167 int
168 wsmousedevprint(aux, pnp)
169 void *aux;
170 const char *pnp;
171 {
172
173 if (pnp)
174 printf("wsmouse at %s", pnp);
175 return (UNCONF);
176 }
177
178 int
179 wsmouse_match(parent, match, aux)
180 struct device *parent;
181 struct cfdata *match;
182 void *aux;
183 {
184 return (1);
185 }
186
187 void
188 wsmouse_attach(parent, self, aux)
189 struct device *parent, *self;
190 void *aux;
191 {
192 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
193 struct wsmousedev_attach_args *ap = aux;
194 #if NWSMUX > 0
195 int mux;
196 #endif
197
198 sc->sc_accessops = ap->accessops;
199 sc->sc_accesscookie = ap->accesscookie;
200 sc->sc_ready = 0; /* sanity */
201
202 #if NWSMUX > 0
203 mux = sc->sc_dv.dv_cfdata->wsmousedevcf_mux;
204 if (mux != WSMOUSEDEVCF_MUX_DEFAULT) {
205 wsmux_attach(mux, WSMUX_MOUSE, &sc->sc_dv, &sc->sc_events,
206 &sc->sc_mux, &wsmouse_muxops);
207 printf(" mux %d", mux);
208 }
209 #endif
210
211 printf("\n");
212 }
213
214 int
215 wsmouse_activate(self, act)
216 struct device *self;
217 enum devact act;
218 {
219 /* XXX should we do something more? */
220 return (0);
221 }
222
223 /*
224 * Detach a mouse. To keep track of users of the softc we keep
225 * a reference count that's incremented while inside, e.g., read.
226 * If the mouse is active and the reference count is > 0 (0 is the
227 * normal state) we post an event and then wait for the process
228 * that had the reference to wake us up again. Then we blow away the
229 * vnode and return (which will deallocate the softc).
230 */
231 int
232 wsmouse_detach(self, flags)
233 struct device *self;
234 int flags;
235 {
236 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
237 struct wseventvar *evar;
238 int maj, mn;
239 int s;
240 #if NWSMUX > 0
241 int mux;
242 #endif
243
244 sc->sc_dying = 1;
245
246 #if NWSMUX > 0
247 mux = sc->sc_dv.dv_cfdata->wsmousedevcf_mux;
248 if (mux != WSMOUSEDEVCF_MUX_DEFAULT)
249 wsmux_detach(mux, &sc->sc_dv);
250 #endif
251
252 evar = &sc->sc_events;
253 if (evar->io) {
254 s = spltty();
255 if (--sc->sc_refcnt >= 0) {
256 /* Wake everyone by generating a dummy event. */
257 if (++evar->put >= WSEVENT_QSIZE)
258 evar->put = 0;
259 WSEVENT_WAKEUP(evar);
260 /* Wait for processes to go away. */
261 if (tsleep(sc, PZERO, "wsmdet", hz * 60))
262 printf("wsmouse_detach: %s didn't detach\n",
263 sc->sc_dv.dv_xname);
264 }
265 splx(s);
266 }
267
268 /* locate the major number */
269 for (maj = 0; maj < nchrdev; maj++)
270 if (cdevsw[maj].d_open == wsmouseopen)
271 break;
272
273 /* Nuke the vnodes for any open instances (calls close). */
274 mn = self->dv_unit;
275 vdevgone(maj, mn, mn, VCHR);
276
277 return (0);
278 }
279
280 void
281 wsmouse_input(wsmousedev, btns, dx, dy, dz)
282 struct device *wsmousedev;
283 u_int btns; /* 0 is up */
284 int dx, dy, dz;
285 {
286 struct wsmouse_softc *sc = (struct wsmouse_softc *)wsmousedev;
287 struct wscons_event *ev;
288 struct wseventvar *evar;
289 int mb, ub, d, get, put, any;
290
291 /*
292 * Discard input if not ready.
293 */
294 if (sc->sc_ready == 0)
295 return;
296
297 #if NWSMUX > 0
298 if (sc->sc_mux)
299 evar = &sc->sc_mux->sc_events;
300 else
301 #endif
302 evar = &sc->sc_events;
303
304 sc->sc_mb = btns;
305 sc->sc_dx += dx;
306 sc->sc_dy += dy;
307 sc->sc_dz += dz;
308
309 /*
310 * We have at least one event (mouse button, delta-X, or
311 * delta-Y; possibly all three, and possibly three separate
312 * button events). Deliver these events until we are out
313 * of changes or out of room. As events get delivered,
314 * mark them `unchanged'.
315 */
316 any = 0;
317 get = evar->get;
318 put = evar->put;
319 ev = &evar->q[put];
320
321 /* NEXT prepares to put the next event, backing off if necessary */
322 #define NEXT \
323 if ((++put) % WSEVENT_QSIZE == get) { \
324 put--; \
325 goto out; \
326 }
327 /* ADVANCE completes the `put' of the event */
328 #define ADVANCE \
329 ev++; \
330 if (put >= WSEVENT_QSIZE) { \
331 put = 0; \
332 ev = &evar->q[0]; \
333 } \
334 any = 1
335 /* TIMESTAMP sets `time' field of the event to the current time */
336 #define TIMESTAMP \
337 do { \
338 int s; \
339 s = splhigh(); \
340 TIMEVAL_TO_TIMESPEC(&time, &ev->time); \
341 splx(s); \
342 } while (0)
343
344 mb = sc->sc_mb;
345 ub = sc->sc_ub;
346 while ((d = mb ^ ub) != 0) {
347 /*
348 * Mouse button change. Find the first change and drop
349 * it into the event queue.
350 */
351 NEXT;
352 ev->value = ffs(d) - 1;
353
354 KASSERT(ev->value >= 0);
355
356 d = 1 << ev->value;
357 ev->type =
358 (mb & d) ? WSCONS_EVENT_MOUSE_DOWN : WSCONS_EVENT_MOUSE_UP;
359 TIMESTAMP;
360 ADVANCE;
361 ub ^= d;
362 }
363 if (sc->sc_dx) {
364 NEXT;
365 ev->type = WSCONS_EVENT_MOUSE_DELTA_X;
366 ev->value = sc->sc_dx;
367 TIMESTAMP;
368 ADVANCE;
369 sc->sc_dx = 0;
370 }
371 if (sc->sc_dy) {
372 NEXT;
373 ev->type = WSCONS_EVENT_MOUSE_DELTA_Y;
374 ev->value = sc->sc_dy;
375 TIMESTAMP;
376 ADVANCE;
377 sc->sc_dy = 0;
378 }
379 if (sc->sc_dz) {
380 NEXT;
381 ev->type = WSCONS_EVENT_MOUSE_DELTA_Z;
382 ev->value = sc->sc_dz;
383 TIMESTAMP;
384 ADVANCE;
385 sc->sc_dz = 0;
386 }
387 out:
388 if (any) {
389 sc->sc_ub = ub;
390 evar->put = put;
391 WSEVENT_WAKEUP(evar);
392 }
393 }
394
395 int
396 wsmouseopen(dev, flags, mode, p)
397 dev_t dev;
398 int flags, mode;
399 struct proc *p;
400 {
401 #if NWSMOUSE > 0
402 struct wsmouse_softc *sc;
403 int error, unit;
404
405 unit = minor(dev);
406 if (unit >= wsmouse_cd.cd_ndevs || /* make sure it was attached */
407 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
408 return (ENXIO);
409
410 if (sc->sc_dying)
411 return (EIO);
412
413 if ((flags & (FREAD | FWRITE)) == FWRITE)
414 return (0); /* always allow open for write
415 so ioctl() is possible. */
416
417 #if NWSMUX > 0
418 if (sc->sc_mux)
419 return (EBUSY);
420 #endif
421
422 if (sc->sc_events.io) /* and that it's not in use */
423 return (EBUSY);
424
425 sc->sc_events.io = p;
426 wsevent_init(&sc->sc_events); /* may cause sleep */
427
428 sc->sc_ready = 1; /* start accepting events */
429
430 /* enable the device, and punt if that's not possible */
431 error = (*sc->sc_accessops->enable)(sc->sc_accesscookie);
432 if (error) {
433 sc->sc_ready = 0; /* stop accepting events */
434 wsevent_fini(&sc->sc_events);
435 sc->sc_events.io = NULL;
436 return (error);
437 }
438
439 return (0);
440 #else
441 return (ENXIO);
442 #endif /* NWSMOUSE > 0 */
443 }
444
445 int
446 wsmouseclose(dev, flags, mode, p)
447 dev_t dev;
448 int flags, mode;
449 struct proc *p;
450 {
451 #if NWSMOUSE > 0
452 return (wsmousedoclose(wsmouse_cd.cd_devs[minor(dev)],
453 flags, mode, p));
454 #else
455 return (ENXIO);
456 #endif /* NWSMOUSE > 0 */
457 }
458
459 #if NWSMOUSE > 0
460 int
461 wsmousedoclose(dv, flags, mode, p)
462 struct device *dv;
463 int flags, mode;
464 struct proc *p;
465 {
466 struct wsmouse_softc *sc = (struct wsmouse_softc *)dv;
467
468 if ((flags & (FREAD | FWRITE)) == FWRITE)
469 return (0); /* see wsmouseopen() */
470
471 (*sc->sc_accessops->disable)(sc->sc_accesscookie);
472
473 sc->sc_ready = 0; /* stop accepting events */
474 wsevent_fini(&sc->sc_events);
475 sc->sc_events.io = NULL;
476 return (0);
477 }
478 #endif /* NWSMOUSE > 0 */
479
480 int
481 wsmouseread(dev, uio, flags)
482 dev_t dev;
483 struct uio *uio;
484 int flags;
485 {
486 #if NWSMOUSE > 0
487 struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
488 int error;
489
490 if (sc->sc_dying)
491 return (EIO);
492
493 sc->sc_refcnt++;
494 error = wsevent_read(&sc->sc_events, uio, flags);
495 if (--sc->sc_refcnt < 0) {
496 wakeup(sc);
497 error = EIO;
498 }
499 return (error);
500 #else
501 return (ENXIO);
502 #endif /* NWSMOUSE > 0 */
503 }
504
505 int
506 wsmouseioctl(dev, cmd, data, flag, p)
507 dev_t dev;
508 u_long cmd;
509 caddr_t data;
510 int flag;
511 struct proc *p;
512 {
513 #if NWSMOUSE > 0
514 return (wsmousedoioctl(wsmouse_cd.cd_devs[minor(dev)],
515 cmd, data, flag, p));
516 #else
517 return (ENXIO);
518 #endif /* NWSMOUSE > 0 */
519 }
520
521 #if NWSMOUSE > 0
522 /* A wrapper around the ioctl() workhorse to make reference counting easy. */
523 int
524 wsmousedoioctl(dv, cmd, data, flag, p)
525 struct device *dv;
526 u_long cmd;
527 caddr_t data;
528 int flag;
529 struct proc *p;
530 {
531 struct wsmouse_softc *sc = (struct wsmouse_softc *)dv;
532 int error;
533
534 sc->sc_refcnt++;
535 error = wsmouse_do_ioctl(sc, cmd, data, flag, p);
536 if (--sc->sc_refcnt < 0)
537 wakeup(sc);
538 return (error);
539 }
540
541 int
542 wsmouse_do_ioctl(sc, cmd, data, flag, p)
543 struct wsmouse_softc *sc;
544 u_long cmd;
545 caddr_t data;
546 int flag;
547 struct proc *p;
548 {
549 int error;
550
551 if (sc->sc_dying)
552 return (EIO);
553
554 /*
555 * Try the generic ioctls that the wsmouse interface supports.
556 */
557 switch (cmd) {
558 case FIONBIO: /* we will remove this someday (soon???) */
559 return (0);
560
561 case FIOASYNC:
562 sc->sc_events.async = *(int *)data != 0;
563 return (0);
564
565 case TIOCSPGRP:
566 if (*(int *)data != sc->sc_events.io->p_pgid)
567 return (EPERM);
568 return (0);
569 }
570
571 /*
572 * Try the mouse driver for WSMOUSEIO ioctls. It returns -1
573 * if it didn't recognize the request.
574 */
575 error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd,
576 data, flag, p);
577 return (error != -1 ? error : ENOTTY);
578 }
579 #endif /* NWSMOUSE > 0 */
580
581 int
582 wsmousepoll(dev, events, p)
583 dev_t dev;
584 int events;
585 struct proc *p;
586 {
587 #if NWSMOUSE > 0
588 struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
589
590 return (wsevent_poll(&sc->sc_events, events, p));
591 #else
592 return (0);
593 #endif /* NWSMOUSE > 0 */
594 }
595
596 #if NWSMUX > 0
597 int
598 wsmouse_add_mux(unit, muxsc)
599 int unit;
600 struct wsmux_softc *muxsc;
601 {
602 struct wsmouse_softc *sc;
603
604 if (unit < 0 || unit >= wsmouse_cd.cd_ndevs ||
605 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
606 return (ENXIO);
607
608 if (sc->sc_mux || sc->sc_events.io)
609 return (EBUSY);
610
611 return (wsmux_attach_sc(muxsc, WSMUX_KBD, &sc->sc_dv, &sc->sc_events,
612 &sc->sc_mux, &wsmouse_muxops));
613 }
614
615 int
616 wsmouse_rem_mux(unit, muxsc)
617 int unit;
618 struct wsmux_softc *muxsc;
619 {
620 struct wsmouse_softc *sc;
621
622 if (unit < 0 || unit >= wsmouse_cd.cd_ndevs ||
623 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
624 return (ENXIO);
625
626 return (wsmux_detach_sc(muxsc, &sc->sc_dv));
627 }
628
629 #endif
630