wsmouse.c revision 1.8 1 /* $NetBSD: wsmouse.c,v 1.8 1999/07/29 18:20:03 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.8 1999/07/29 18:20:03 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 || (NWSDISPLAY > 0 && NWSKBD > 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 || (NWSDISPLAY > 0 && NWSKBD > 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 struct wsmuxops wsmouse_muxops = {
159 wsmouseopen, wsmousedoclose, wsmousedoioctl, 0, 0
160 };
161
162 /*
163 * Print function (for parent devices).
164 */
165 int
166 wsmousedevprint(aux, pnp)
167 void *aux;
168 const char *pnp;
169 {
170
171 if (pnp)
172 printf("wsmouse at %s", pnp);
173 return (UNCONF);
174 }
175
176 int
177 wsmouse_match(parent, match, aux)
178 struct device *parent;
179 struct cfdata *match;
180 void *aux;
181 {
182 return (1);
183 }
184
185 void
186 wsmouse_attach(parent, self, aux)
187 struct device *parent, *self;
188 void *aux;
189 {
190 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
191 struct wsmousedev_attach_args *ap = aux;
192 #if NWSMUX > 0 || (NWSDISPLAY > 0 && NWSKBD > 0)
193 int mux;
194 #endif
195
196 sc->sc_accessops = ap->accessops;
197 sc->sc_accesscookie = ap->accesscookie;
198 sc->sc_ready = 0; /* sanity */
199
200 #if NWSMUX > 0 || (NWSDISPLAY > 0 && NWSKBD > 0)
201 mux = sc->sc_dv.dv_cfdata->wsmousedevcf_mux;
202 if (mux != WSMOUSEDEVCF_MUX_DEFAULT) {
203 wsmux_attach(mux, WSMUX_MOUSE, &sc->sc_dv, &sc->sc_events,
204 &sc->sc_mux, &wsmouse_muxops);
205 printf(" mux %d", mux);
206 }
207 #endif
208
209 printf("\n");
210 }
211
212 int
213 wsmouse_activate(self, act)
214 struct device *self;
215 enum devact act;
216 {
217 /* XXX should we do something more? */
218 return (0);
219 }
220
221 /*
222 * Detach a mouse. To keep track of users of the softc we keep
223 * a reference count that's incremented while inside, e.g., read.
224 * If the mouse is active and the reference count is > 0 (0 is the
225 * normal state) we post an event and then wait for the process
226 * that had the reference to wake us up again. Then we blow away the
227 * vnode and return (which will deallocate the softc).
228 */
229 int
230 wsmouse_detach(self, flags)
231 struct device *self;
232 int flags;
233 {
234 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
235 struct wseventvar *evar;
236 int maj, mn;
237 int s;
238 #if NWSMUX > 0 || (NWSDISPLAY > 0 && NWSKBD > 0)
239 int mux;
240 #endif
241
242 sc->sc_dying = 1;
243
244 #if NWSMUX > 0 || (NWSDISPLAY > 0 && NWSKBD > 0)
245 mux = sc->sc_dv.dv_cfdata->wsmousedevcf_mux;
246 if (mux != WSMOUSEDEVCF_MUX_DEFAULT)
247 wsmux_detach(mux, &sc->sc_dv);
248 #endif
249
250 evar = &sc->sc_events;
251 if (evar->io) {
252 s = spltty();
253 if (--sc->sc_refcnt >= 0) {
254 /* Wake everyone by generating a dummy event. */
255 if (++evar->put >= WSEVENT_QSIZE)
256 evar->put = 0;
257 WSEVENT_WAKEUP(evar);
258 /* Wait for processes to go away. */
259 if (tsleep(sc, PZERO, "wsmdet", hz * 60))
260 printf("wsmouse_detach: %s didn't detach\n",
261 sc->sc_dv.dv_xname);
262 }
263 splx(s);
264 }
265
266 /* locate the major number */
267 for (maj = 0; maj < nchrdev; maj++)
268 if (cdevsw[maj].d_open == wsmouseopen)
269 break;
270
271 /* Nuke the vnodes for any open instances (calls close). */
272 mn = self->dv_unit;
273 vdevgone(maj, mn, mn, VCHR);
274
275 return (0);
276 }
277
278 void
279 wsmouse_input(wsmousedev, btns, dx, dy, dz)
280 struct device *wsmousedev;
281 u_int btns; /* 0 is up */
282 int dx, dy, dz;
283 {
284 struct wsmouse_softc *sc = (struct wsmouse_softc *)wsmousedev;
285 struct wscons_event *ev;
286 struct wseventvar *evar;
287 int mb, ub, d, get, put, any;
288
289 /*
290 * Discard input if not ready.
291 */
292 if (sc->sc_ready == 0)
293 return;
294
295 #if NWSMUX > 0 || (NWSDISPLAY > 0 && NWSKBD > 0)
296 if (sc->sc_mux)
297 evar = &sc->sc_mux->sc_events;
298 else
299 #endif
300 evar = &sc->sc_events;
301
302 sc->sc_mb = btns;
303 sc->sc_dx += dx;
304 sc->sc_dy += dy;
305 sc->sc_dz += dz;
306
307 /*
308 * We have at least one event (mouse button, delta-X, or
309 * delta-Y; possibly all three, and possibly three separate
310 * button events). Deliver these events until we are out
311 * of changes or out of room. As events get delivered,
312 * mark them `unchanged'.
313 */
314 any = 0;
315 get = evar->get;
316 put = evar->put;
317 ev = &evar->q[put];
318
319 /* NEXT prepares to put the next event, backing off if necessary */
320 #define NEXT \
321 if ((++put) % WSEVENT_QSIZE == get) { \
322 put--; \
323 goto out; \
324 }
325 /* ADVANCE completes the `put' of the event */
326 #define ADVANCE \
327 ev++; \
328 if (put >= WSEVENT_QSIZE) { \
329 put = 0; \
330 ev = &evar->q[0]; \
331 } \
332 any = 1
333 /* TIMESTAMP sets `time' field of the event to the current time */
334 #define TIMESTAMP \
335 do { \
336 int s; \
337 s = splhigh(); \
338 TIMEVAL_TO_TIMESPEC(&time, &ev->time); \
339 splx(s); \
340 } while (0)
341
342 mb = sc->sc_mb;
343 ub = sc->sc_ub;
344 while ((d = mb ^ ub) != 0) {
345 /*
346 * Mouse button change. Find the first change and drop
347 * it into the event queue.
348 */
349 NEXT;
350 ev->value = ffs(d) - 1;
351
352 KASSERT(ev->value >= 0);
353
354 d = 1 << ev->value;
355 ev->type =
356 (mb & d) ? WSCONS_EVENT_MOUSE_DOWN : WSCONS_EVENT_MOUSE_UP;
357 TIMESTAMP;
358 ADVANCE;
359 ub ^= d;
360 }
361 if (sc->sc_dx) {
362 NEXT;
363 ev->type = WSCONS_EVENT_MOUSE_DELTA_X;
364 ev->value = sc->sc_dx;
365 TIMESTAMP;
366 ADVANCE;
367 sc->sc_dx = 0;
368 }
369 if (sc->sc_dy) {
370 NEXT;
371 ev->type = WSCONS_EVENT_MOUSE_DELTA_Y;
372 ev->value = sc->sc_dy;
373 TIMESTAMP;
374 ADVANCE;
375 sc->sc_dy = 0;
376 }
377 if (sc->sc_dz) {
378 NEXT;
379 ev->type = WSCONS_EVENT_MOUSE_DELTA_Z;
380 ev->value = sc->sc_dz;
381 TIMESTAMP;
382 ADVANCE;
383 sc->sc_dz = 0;
384 }
385 out:
386 if (any) {
387 sc->sc_ub = ub;
388 evar->put = put;
389 WSEVENT_WAKEUP(evar);
390 }
391 }
392
393 int
394 wsmouseopen(dev, flags, mode, p)
395 dev_t dev;
396 int flags, mode;
397 struct proc *p;
398 {
399 #if NWSMOUSE > 0
400 struct wsmouse_softc *sc;
401 int error, unit;
402
403 unit = minor(dev);
404 if (unit >= wsmouse_cd.cd_ndevs || /* make sure it was attached */
405 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
406 return (ENXIO);
407
408 if (sc->sc_dying)
409 return (EIO);
410
411 if ((flags & (FREAD | FWRITE)) == FWRITE)
412 return (0); /* always allow open for write
413 so ioctl() is possible. */
414
415 #if NWSMUX > 0 || (NWSDISPLAY > 0 && NWSKBD > 0)
416 if (sc->sc_mux)
417 return (EBUSY);
418 #endif
419
420 if (sc->sc_events.io) /* and that it's not in use */
421 return (EBUSY);
422
423 sc->sc_events.io = p;
424 wsevent_init(&sc->sc_events); /* may cause sleep */
425
426 sc->sc_ready = 1; /* start accepting events */
427
428 /* enable the device, and punt if that's not possible */
429 error = (*sc->sc_accessops->enable)(sc->sc_accesscookie);
430 if (error) {
431 sc->sc_ready = 0; /* stop accepting events */
432 wsevent_fini(&sc->sc_events);
433 sc->sc_events.io = NULL;
434 return (error);
435 }
436
437 return (0);
438 #else
439 return (ENXIO);
440 #endif /* NWSMOUSE > 0 */
441 }
442
443 int
444 wsmouseclose(dev, flags, mode, p)
445 dev_t dev;
446 int flags, mode;
447 struct proc *p;
448 {
449 #if NWSMOUSE > 0
450 return (wsmousedoclose(wsmouse_cd.cd_devs[minor(dev)],
451 flags, mode, p));
452 #else
453 return (ENXIO);
454 #endif /* NWSMOUSE > 0 */
455 }
456
457 #if NWSMOUSE > 0
458 int
459 wsmousedoclose(dv, flags, mode, p)
460 struct device *dv;
461 int flags, mode;
462 struct proc *p;
463 {
464 struct wsmouse_softc *sc = (struct wsmouse_softc *)dv;
465
466 if ((flags & (FREAD | FWRITE)) == FWRITE)
467 return (0); /* see wsmouseopen() */
468
469 (*sc->sc_accessops->disable)(sc->sc_accesscookie);
470
471 sc->sc_ready = 0; /* stop accepting events */
472 wsevent_fini(&sc->sc_events);
473 sc->sc_events.io = NULL;
474 return (0);
475 }
476 #endif /* NWSMOUSE > 0 */
477
478 int
479 wsmouseread(dev, uio, flags)
480 dev_t dev;
481 struct uio *uio;
482 int flags;
483 {
484 #if NWSMOUSE > 0
485 struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
486 int error;
487
488 if (sc->sc_dying)
489 return (EIO);
490
491 sc->sc_refcnt++;
492 error = wsevent_read(&sc->sc_events, uio, flags);
493 if (--sc->sc_refcnt < 0) {
494 wakeup(sc);
495 error = EIO;
496 }
497 return (error);
498 #else
499 return (ENXIO);
500 #endif /* NWSMOUSE > 0 */
501 }
502
503 int
504 wsmouseioctl(dev, cmd, data, flag, p)
505 dev_t dev;
506 u_long cmd;
507 caddr_t data;
508 int flag;
509 struct proc *p;
510 {
511 #if NWSMOUSE > 0
512 return (wsmousedoioctl(wsmouse_cd.cd_devs[minor(dev)],
513 cmd, data, flag, p));
514 #else
515 return (ENXIO);
516 #endif /* NWSMOUSE > 0 */
517 }
518
519 #if NWSMOUSE > 0
520 /* A wrapper around the ioctl() workhorse to make reference counting easy. */
521 int
522 wsmousedoioctl(dv, cmd, data, flag, p)
523 struct device *dv;
524 u_long cmd;
525 caddr_t data;
526 int flag;
527 struct proc *p;
528 {
529 struct wsmouse_softc *sc = (struct wsmouse_softc *)dv;
530 int error;
531
532 sc->sc_refcnt++;
533 error = wsmouse_do_ioctl(sc, cmd, data, flag, p);
534 if (--sc->sc_refcnt < 0)
535 wakeup(sc);
536 return (error);
537 }
538
539 int
540 wsmouse_do_ioctl(sc, cmd, data, flag, p)
541 struct wsmouse_softc *sc;
542 u_long cmd;
543 caddr_t data;
544 int flag;
545 struct proc *p;
546 {
547 int error;
548
549 if (sc->sc_dying)
550 return (EIO);
551
552 /*
553 * Try the generic ioctls that the wsmouse interface supports.
554 */
555 switch (cmd) {
556 case FIONBIO: /* we will remove this someday (soon???) */
557 return (0);
558
559 case FIOASYNC:
560 sc->sc_events.async = *(int *)data != 0;
561 return (0);
562
563 case TIOCSPGRP:
564 if (*(int *)data != sc->sc_events.io->p_pgid)
565 return (EPERM);
566 return (0);
567 }
568
569 /*
570 * Try the mouse driver for WSMOUSEIO ioctls. It returns -1
571 * if it didn't recognize the request.
572 */
573 error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd,
574 data, flag, p);
575 return (error != -1 ? error : ENOTTY);
576 }
577 #endif /* NWSMOUSE > 0 */
578
579 int
580 wsmousepoll(dev, events, p)
581 dev_t dev;
582 int events;
583 struct proc *p;
584 {
585 #if NWSMOUSE > 0
586 struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
587
588 return (wsevent_poll(&sc->sc_events, events, p));
589 #else
590 return (0);
591 #endif /* NWSMOUSE > 0 */
592 }
593
594 #if NWSMUX > 0 || (NWSDISPLAY > 0 && NWSKBD > 0)
595 int
596 wsmouse_add_mux(unit, muxsc)
597 int unit;
598 struct wsmux_softc *muxsc;
599 {
600 struct wsmouse_softc *sc;
601
602 if (unit < 0 || unit >= wsmouse_cd.cd_ndevs ||
603 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
604 return (ENXIO);
605
606 if (sc->sc_mux || sc->sc_events.io)
607 return (EBUSY);
608
609 return (wsmux_attach_sc(muxsc, WSMUX_KBD, &sc->sc_dv, &sc->sc_events,
610 &sc->sc_mux, &wsmouse_muxops));
611 }
612
613 int
614 wsmouse_rem_mux(unit, muxsc)
615 int unit;
616 struct wsmux_softc *muxsc;
617 {
618 struct wsmouse_softc *sc;
619
620 if (unit < 0 || unit >= wsmouse_cd.cd_ndevs ||
621 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
622 return (ENXIO);
623
624 return (wsmux_detach_sc(muxsc, &sc->sc_dv));
625 }
626
627 #endif
628