This source file includes following definitions.
- wsmousedevprint
- wsmouse_match
- wsmouse_attach
- wsmouse_activate
- wsmouse_detach
- wsmouse_input
- wsmouseopen
- wsmouseclose
- wsmousedoopen
- wsmouseread
- wsmouseioctl
- wsmousedoioctl
- wsmouse_do_ioctl
- wsmousepoll
- wsmouse_mux_open
- wsmouse_mux_close
- wsmouse_add_mux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 #ifndef SMALL_KERNEL
79 #define BURNER_SUPPORT
80 #endif
81
82 #include <sys/param.h>
83 #include <sys/conf.h>
84 #include <sys/ioctl.h>
85 #include <sys/fcntl.h>
86 #include <sys/kernel.h>
87 #include <sys/proc.h>
88 #include <sys/syslog.h>
89 #include <sys/systm.h>
90 #include <sys/tty.h>
91 #include <sys/signalvar.h>
92 #include <sys/device.h>
93 #include <sys/vnode.h>
94 #include <sys/poll.h>
95
96 #include <dev/wscons/wsconsio.h>
97 #include <dev/wscons/wsmousevar.h>
98 #include <dev/wscons/wseventvar.h>
99 #include <dev/wscons/wsdisplayvar.h>
100 #include <dev/rndvar.h>
101
102 #include "wsmux.h"
103 #include "wsdisplay.h"
104 #include "wskbd.h"
105
106 #include <dev/wscons/wsmuxvar.h>
107
108 #if defined(WSMUX_DEBUG) && NWSMUX > 0
109 #define DPRINTF(x) if (wsmuxdebug) printf x
110 #define DPRINTFN(n,x) if (wsmuxdebug > (n)) printf x
111 extern int wsmuxdebug;
112 #else
113 #define DPRINTF(x)
114 #define DPRINTFN(n,x)
115 #endif
116
117 #define INVALID_X INT_MAX
118 #define INVALID_Y INT_MAX
119 #define INVALID_Z INT_MAX
120 #define INVALID_W INT_MAX
121
122 struct wsmouse_softc {
123 struct wsevsrc sc_base;
124
125 const struct wsmouse_accessops *sc_accessops;
126 void *sc_accesscookie;
127
128 u_int sc_mb;
129 u_int sc_ub;
130 int sc_dx;
131 int sc_dy;
132 int sc_dz;
133 int sc_dw;
134 int sc_x;
135 int sc_y;
136 int sc_z;
137 int sc_w;
138
139 int sc_refcnt;
140 u_char sc_dying;
141 };
142
143 int wsmouse_match(struct device *, void *, void *);
144 void wsmouse_attach(struct device *, struct device *, void *);
145 int wsmouse_detach(struct device *, int);
146 int wsmouse_activate(struct device *, enum devact);
147
148 int wsmouse_do_ioctl(struct wsmouse_softc *, u_long, caddr_t,
149 int, struct proc *);
150
151 #if NWSMUX > 0
152 int wsmouse_mux_open(struct wsevsrc *, struct wseventvar *);
153 int wsmouse_mux_close(struct wsevsrc *);
154 #endif
155
156 int wsmousedoioctl(struct device *, u_long, caddr_t, int,
157 struct proc *);
158 int wsmousedoopen(struct wsmouse_softc *, struct wseventvar *);
159
160 struct cfdriver wsmouse_cd = {
161 NULL, "wsmouse", DV_TTY
162 };
163
164 struct cfattach wsmouse_ca = {
165 sizeof (struct wsmouse_softc), wsmouse_match, wsmouse_attach,
166 wsmouse_detach, wsmouse_activate
167 };
168
169 #if NWSMUX > 0
170 struct wssrcops wsmouse_srcops = {
171 WSMUX_MOUSE,
172 wsmouse_mux_open, wsmouse_mux_close, wsmousedoioctl, NULL, NULL
173 };
174 #endif
175
176
177
178
179 int
180 wsmousedevprint(void *aux, const char *pnp)
181 {
182
183 if (pnp)
184 printf("wsmouse at %s", pnp);
185 return (UNCONF);
186 }
187
188 int
189 wsmouse_match(struct device *parent, void *match, void *aux)
190 {
191 return (1);
192 }
193
194 void
195 wsmouse_attach(struct device *parent, struct device *self, void *aux)
196 {
197 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
198 struct wsmousedev_attach_args *ap = aux;
199 #if NWSMUX > 0
200 int mux, error;
201 #endif
202
203 sc->sc_accessops = ap->accessops;
204 sc->sc_accesscookie = ap->accesscookie;
205
206 #if NWSMUX > 0
207 sc->sc_base.me_ops = &wsmouse_srcops;
208 mux = sc->sc_base.me_dv.dv_cfdata->wsmousedevcf_mux;
209 if (mux >= 0) {
210 error = wsmux_attach_sc(wsmux_getmux(mux), &sc->sc_base);
211 if (error)
212 printf(" attach error=%d", error);
213 else
214 printf(" mux %d", mux);
215 }
216 #else
217 #if 0
218 if (sc->sc_base.me_dv.dv_cfdata->wsmousedevcf_mux >= 0)
219 printf(" (mux ignored)");
220 #endif
221 #endif
222
223 printf("\n");
224 }
225
226 int
227 wsmouse_activate(struct device *self, enum devact act)
228 {
229 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
230
231 if (act == DVACT_DEACTIVATE)
232 sc->sc_dying = 1;
233 return (0);
234 }
235
236
237
238
239
240
241
242
243
244 int
245 wsmouse_detach(struct device *self, int flags)
246 {
247 struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
248 struct wseventvar *evar;
249 int maj, mn;
250 int s;
251
252 #if NWSMUX > 0
253
254 if (sc->sc_base.me_parent != NULL) {
255 DPRINTF(("wsmouse_detach:\n"));
256 wsmux_detach_sc(&sc->sc_base);
257 }
258 #endif
259
260
261 evar = sc->sc_base.me_evp;
262 if (evar != NULL && evar->io != NULL) {
263 s = spltty();
264 if (--sc->sc_refcnt >= 0) {
265
266 if (++evar->put >= WSEVENT_QSIZE)
267 evar->put = 0;
268 WSEVENT_WAKEUP(evar);
269
270 if (tsleep(sc, PZERO, "wsmdet", hz * 60))
271 printf("wsmouse_detach: %s didn't detach\n",
272 sc->sc_base.me_dv.dv_xname);
273 }
274 splx(s);
275 }
276
277
278 for (maj = 0; maj < nchrdev; maj++)
279 if (cdevsw[maj].d_open == wsmouseopen)
280 break;
281
282
283 mn = self->dv_unit;
284 vdevgone(maj, mn, mn, VCHR);
285
286 return (0);
287 }
288
289 void
290 wsmouse_input(struct device *wsmousedev, u_int btns,
291 int x, int y, int z, int w, u_int flags)
292 {
293 struct wsmouse_softc *sc = (struct wsmouse_softc *)wsmousedev;
294 struct wscons_event *ev;
295 struct wseventvar *evar;
296 int mb, ub, d, get, put, any;
297
298 add_mouse_randomness(x ^ y ^ z ^ w ^ btns);
299
300
301
302
303 evar = sc->sc_base.me_evp;
304 if (evar == NULL)
305 return;
306
307 #ifdef DIAGNOSTIC
308 if (evar->q == NULL) {
309 printf("wsmouse_input: evar->q=NULL\n");
310 return;
311 }
312 #endif
313
314 #if NWSMUX > 0
315 DPRINTFN(5,("wsmouse_input: %s mux=%p, evar=%p\n",
316 sc->sc_base.me_dv.dv_xname, sc->sc_base.me_parent, evar));
317 #endif
318
319 sc->sc_mb = btns;
320 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_X))
321 sc->sc_dx += x;
322 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_Y))
323 sc->sc_dy += y;
324 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_Z))
325 sc->sc_dz += z;
326 if (!(flags & WSMOUSE_INPUT_ABSOLUTE_W))
327 sc->sc_dw += w;
328
329
330
331
332
333
334
335
336 ub = sc->sc_ub;
337 any = 0;
338 get = evar->get;
339 put = evar->put;
340 ev = &evar->q[put];
341
342
343 #define NEXT \
344 if ((++put) % WSEVENT_QSIZE == get) { \
345 put--; \
346 goto out; \
347 }
348
349 #define ADVANCE \
350 ev++; \
351 if (put >= WSEVENT_QSIZE) { \
352 put = 0; \
353 ev = &evar->q[0]; \
354 } \
355 any = 1
356
357 #define TIMESTAMP \
358 do { \
359 getnanotime(&ev->time); \
360 } while (0)
361
362 if (flags & WSMOUSE_INPUT_ABSOLUTE_X) {
363 if (sc->sc_x != x) {
364 NEXT;
365 ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_X;
366 ev->value = x;
367 TIMESTAMP;
368 ADVANCE;
369 sc->sc_x = x;
370 }
371 } else {
372 if (sc->sc_dx) {
373 NEXT;
374 ev->type = WSCONS_EVENT_MOUSE_DELTA_X;
375 ev->value = sc->sc_dx;
376 TIMESTAMP;
377 ADVANCE;
378 sc->sc_dx = 0;
379 }
380 }
381 if (flags & WSMOUSE_INPUT_ABSOLUTE_Y) {
382 if (sc->sc_y != y) {
383 NEXT;
384 ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_Y;
385 ev->value = y;
386 TIMESTAMP;
387 ADVANCE;
388 sc->sc_y = y;
389 }
390 } else {
391 if (sc->sc_dy) {
392 NEXT;
393 ev->type = WSCONS_EVENT_MOUSE_DELTA_Y;
394 ev->value = sc->sc_dy;
395 TIMESTAMP;
396 ADVANCE;
397 sc->sc_dy = 0;
398 }
399 }
400 if (flags & WSMOUSE_INPUT_ABSOLUTE_Z) {
401 if (sc->sc_z != z) {
402 NEXT;
403 ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_Z;
404 ev->value = z;
405 TIMESTAMP;
406 ADVANCE;
407 sc->sc_z = z;
408 }
409 } else {
410 if (sc->sc_dz) {
411 NEXT;
412 ev->type = WSCONS_EVENT_MOUSE_DELTA_Z;
413 ev->value = sc->sc_dz;
414 TIMESTAMP;
415 ADVANCE;
416 sc->sc_dz = 0;
417 }
418 }
419 if (flags & WSMOUSE_INPUT_ABSOLUTE_W) {
420 if (sc->sc_w != w) {
421 NEXT;
422 ev->type = WSCONS_EVENT_MOUSE_ABSOLUTE_W;
423 ev->value = w;
424 TIMESTAMP;
425 ADVANCE;
426 sc->sc_w = w;
427 }
428 } else {
429 if (sc->sc_dw) {
430 NEXT;
431 ev->type = WSCONS_EVENT_MOUSE_DELTA_W;
432 ev->value = sc->sc_dw;
433 TIMESTAMP;
434 ADVANCE;
435 sc->sc_dw = 0;
436 }
437 }
438
439 mb = sc->sc_mb;
440 while ((d = mb ^ ub) != 0) {
441
442
443
444
445 NEXT;
446 ev->value = ffs(d) - 1;
447
448 KASSERT(ev->value >= 0);
449
450 d = 1 << ev->value;
451 ev->type =
452 (mb & d) ? WSCONS_EVENT_MOUSE_DOWN : WSCONS_EVENT_MOUSE_UP;
453 TIMESTAMP;
454 ADVANCE;
455 ub ^= d;
456 }
457
458
459 if (flags & WSMOUSE_INPUT_WSMOUSED_CLOSE) {
460 NEXT;
461 ev->type = WSCONS_EVENT_WSMOUSED_CLOSE;
462 ev->value = 0;
463 TIMESTAMP;
464 ADVANCE;
465 }
466
467 #undef TIMESTAMP
468 #undef ADVANCE
469 #undef NEXT
470
471 out:
472 if (any) {
473 sc->sc_ub = ub;
474 evar->put = put;
475 WSEVENT_WAKEUP(evar);
476 #ifdef BURNER_SUPPORT
477
478 #endif
479 #if NWSMUX > 0
480 DPRINTFN(5,("wsmouse_input: %s wakeup evar=%p\n",
481 sc->sc_base.me_dv.dv_xname, evar));
482 #endif
483 }
484 }
485
486 int
487 wsmouseopen(dev_t dev, int flags, int mode, struct proc *p)
488 {
489 struct wsmouse_softc *sc;
490 struct wseventvar *evar;
491 int error, unit;
492
493 unit = minor(dev);
494 if (unit >= wsmouse_cd.cd_ndevs ||
495 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
496 return (ENXIO);
497
498 #if NWSMUX > 0
499 DPRINTF(("wsmouseopen: %s mux=%p p=%p\n", sc->sc_base.me_dv.dv_xname,
500 sc->sc_base.me_parent, p));
501 #endif
502
503 if (sc->sc_dying)
504 return (EIO);
505
506 if ((flags & (FREAD | FWRITE)) == FWRITE)
507 return (0);
508
509
510 if (sc->sc_base.me_evp != NULL)
511 return (EBUSY);
512
513 evar = &sc->sc_base.me_evar;
514 wsevent_init(evar);
515 evar->io = p;
516
517 error = wsmousedoopen(sc, evar);
518 if (error) {
519 DPRINTF(("wsmouseopen: %s open failed\n",
520 sc->sc_base.me_dv.dv_xname));
521 sc->sc_base.me_evp = NULL;
522 wsevent_fini(evar);
523 }
524 return (error);
525 }
526
527 int
528 wsmouseclose(dev_t dev, int flags, int mode, struct proc *p)
529 {
530 struct wsmouse_softc *sc =
531 (struct wsmouse_softc *)wsmouse_cd.cd_devs[minor(dev)];
532 struct wseventvar *evar = sc->sc_base.me_evp;
533
534 if ((flags & (FREAD | FWRITE)) == FWRITE)
535 return (0);
536
537 if (evar == NULL)
538
539 return (0);
540 sc->sc_base.me_evp = NULL;
541 (*sc->sc_accessops->disable)(sc->sc_accesscookie);
542 wsevent_fini(evar);
543
544 return (0);
545 }
546
547 int
548 wsmousedoopen(struct wsmouse_softc *sc, struct wseventvar *evp)
549 {
550 sc->sc_base.me_evp = evp;
551 sc->sc_x = INVALID_X;
552 sc->sc_y = INVALID_Y;
553 sc->sc_z = INVALID_Z;
554 sc->sc_w = INVALID_W;
555
556
557 return (*sc->sc_accessops->enable)(sc->sc_accesscookie);
558 }
559
560 int
561 wsmouseread(dev_t dev, struct uio *uio, int flags)
562 {
563 struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
564 int error;
565
566 if (sc->sc_dying)
567 return (EIO);
568
569 #ifdef DIAGNOSTIC
570 if (sc->sc_base.me_evp == NULL) {
571 printf("wsmouseread: evp == NULL\n");
572 return (EINVAL);
573 }
574 #endif
575
576 sc->sc_refcnt++;
577 error = wsevent_read(sc->sc_base.me_evp, uio, flags);
578 if (--sc->sc_refcnt < 0) {
579 wakeup(sc);
580 error = EIO;
581 }
582 return (error);
583 }
584
585 int
586 wsmouseioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
587 {
588 return (wsmousedoioctl(wsmouse_cd.cd_devs[minor(dev)],
589 cmd, data, flag, p));
590 }
591
592
593 int
594 wsmousedoioctl(struct device *dv, u_long cmd, caddr_t data, int flag,
595 struct proc *p)
596 {
597 struct wsmouse_softc *sc = (struct wsmouse_softc *)dv;
598 int error;
599
600 sc->sc_refcnt++;
601 error = wsmouse_do_ioctl(sc, cmd, data, flag, p);
602 if (--sc->sc_refcnt < 0)
603 wakeup(sc);
604 return (error);
605 }
606
607 int
608 wsmouse_do_ioctl(struct wsmouse_softc *sc, u_long cmd, caddr_t data, int flag,
609 struct proc *p)
610 {
611 int error;
612
613 if (sc->sc_dying)
614 return (EIO);
615
616
617
618
619
620 switch (cmd) {
621 case FIOASYNC:
622 case FIOSETOWN:
623 case TIOCSPGRP:
624 if ((flag & FWRITE) == 0)
625 return (EACCES);
626 }
627
628 switch (cmd) {
629 case FIONBIO:
630 return (0);
631
632 case FIOASYNC:
633 if (sc->sc_base.me_evp == NULL)
634 return (EINVAL);
635 sc->sc_base.me_evp->async = *(int *)data != 0;
636 return (0);
637
638 case FIOSETOWN:
639 if (sc->sc_base.me_evp == NULL)
640 return (EINVAL);
641 if (-*(int *)data != sc->sc_base.me_evp->io->p_pgid
642 && *(int *)data != sc->sc_base.me_evp->io->p_pid)
643 return (EPERM);
644 return (0);
645
646 case TIOCSPGRP:
647 if (sc->sc_base.me_evp == NULL)
648 return (EINVAL);
649 if (*(int *)data != sc->sc_base.me_evp->io->p_pgid)
650 return (EPERM);
651 return (0);
652 }
653
654
655
656
657
658 error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd,
659 data, flag, p);
660 return (error != -1 ? error : ENOTTY);
661 }
662
663 int
664 wsmousepoll(dev_t dev, int events, struct proc *p)
665 {
666 struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
667
668 if (sc->sc_base.me_evp == NULL)
669 return (POLLERR);
670 return (wsevent_poll(sc->sc_base.me_evp, events, p));
671 }
672
673 #if NWSMUX > 0
674 int
675 wsmouse_mux_open(struct wsevsrc *me, struct wseventvar *evp)
676 {
677 struct wsmouse_softc *sc = (struct wsmouse_softc *)me;
678
679 if (sc->sc_base.me_evp != NULL)
680 return (EBUSY);
681
682 return wsmousedoopen(sc, evp);
683 }
684
685 int
686 wsmouse_mux_close(struct wsevsrc *me)
687 {
688 struct wsmouse_softc *sc = (struct wsmouse_softc *)me;
689
690 sc->sc_base.me_evp = NULL;
691 (*sc->sc_accessops->disable)(sc->sc_accesscookie);
692
693 return (0);
694 }
695
696 int
697 wsmouse_add_mux(int unit, struct wsmux_softc *muxsc)
698 {
699 struct wsmouse_softc *sc;
700
701 if (unit < 0 || unit >= wsmouse_cd.cd_ndevs ||
702 (sc = wsmouse_cd.cd_devs[unit]) == NULL)
703 return (ENXIO);
704
705 if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL)
706 return (EBUSY);
707
708 return (wsmux_attach_sc(muxsc, &sc->sc_base));
709 }
710 #endif