1 /* $NetBSD: ms.c,v 1.43 2022/08/01 01:32:15 rin Exp $ */
2
3 /*
4 * based on:
5 *
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * This software was developed by the Computer Systems Engineering group
10 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
11 * contributed to Berkeley.
12 *
13 * All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Lawrence Berkeley Laboratory.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 * @(#)ms.c 8.1 (Berkeley) 6/11/93
43 *
44 * Header: ms.c,v 1.5 92/11/26 01:28:47 torek Exp (LBL)
45 */
46
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: ms.c,v 1.43 2022/08/01 01:32:15 rin Exp $");
49
50 /*
51 * Mouse driver.
52 *
53 * wscons aware. Attaches two wsmouse devices, one for each port.
54 * Also still exports its own device entry points so it is possible
55 * to open this and read firm_events.
56 * The events go only to one place at a time:
57 * - When somebody has opened a ms device directly wsmouse cannot be activated.
58 * (when wsmouse is opened it calls ms_enable to activate)
59 * - When feeding events to wsmouse open of ms device will fail.
60 */
61
62 #include "wsmouse.h"
63
64 #include <sys/param.h>
65 #include <sys/device.h>
66 #include <sys/ioctl.h>
67 #include <sys/kernel.h>
68 #include <sys/proc.h>
69 #include <sys/syslog.h>
70 #include <sys/systm.h>
71 #include <sys/callout.h>
72 #include <sys/tty.h>
73 #include <sys/signalvar.h>
74 #include <sys/conf.h>
75
76 #include <amiga/dev/event_var.h>
77 #include <amiga/dev/vuid_event.h>
78
79 #include <amiga/amiga/custom.h>
80 #include <amiga/amiga/cia.h>
81 #include <amiga/amiga/device.h>
82
83 #if NWSMOUSE > 0
84 #include <dev/wscons/wsmousevar.h>
85 #include <dev/wscons/wsconsio.h>
86 #endif
87
88 void msattach(device_t, device_t, void *);
89 int msmatch(device_t, cfdata_t, void *);
90
91 /* per-port state */
92 struct ms_port {
93 int ms_portno; /* which hardware port, for msintr() */
94
95 struct callout ms_intr_ch;
96
97 u_char ms_horc; /* horizontal counter on last scan */
98 u_char ms_verc; /* vertical counter on last scan */
99 char ms_mb; /* mouse button state */
100 char ms_ub; /* user button state */
101 int ms_dx; /* delta-x */
102 int ms_dy; /* delta-y */
103 volatile int ms_ready; /* event queue is ready */
104 struct evvar ms_events; /* event queue state */
105 #if NWSMOUSE > 0
106 device_t ms_wsmousedev; /* wsmouse device */
107 int ms_wsenabled; /* feeding events to wscons */
108 #endif
109 };
110
111 #define MS_NPORTS 2
112
113 struct ms_softc {
114 struct ms_port sc_ports[MS_NPORTS];
115 };
116
117 CFATTACH_DECL_NEW(ms, sizeof(struct ms_softc),
118 msmatch, msattach, NULL, NULL);
119
120 void msintr(void *);
121 void ms_enable(struct ms_port *);
122 void ms_disable(struct ms_port *);
123
124 extern struct cfdriver ms_cd;
125
126 dev_type_open(msopen);
127 dev_type_close(msclose);
128 dev_type_read(msread);
129 dev_type_ioctl(msioctl);
130 dev_type_poll(mspoll);
131 dev_type_kqfilter(mskqfilter);
132
133 const struct cdevsw ms_cdevsw = {
134 .d_open = msopen,
135 .d_close = msclose,
136 .d_read = msread,
137 .d_write = nowrite,
138 .d_ioctl = msioctl,
139 .d_stop = nostop,
140 .d_tty = notty,
141 .d_poll = mspoll,
142 .d_mmap = nommap,
143 .d_kqfilter = mskqfilter,
144 .d_discard = nodiscard,
145 .d_flag = 0
146 };
147
148 #define MS_UNIT(d) ((minor(d) & ~0x1) >> 1)
149 #define MS_PORT(d) (minor(d) & 0x1)
150
151 /*
152 * Given a dev_t, return a pointer to the port's hardware state.
153 * Assumes the unit to be valid, so do *not* use this in msopen().
154 */
155 #define MS_DEV2MSPORT(d) \
156 (&(((struct ms_softc *)getsoftc(ms_cd, MS_UNIT(d)))->sc_ports[MS_PORT(d)]))
157
158 #if NWSMOUSE > 0
159 /*
160 * Callbacks for wscons.
161 */
162 static int ms_wscons_enable(void *);
163 static int ms_wscons_ioctl(void *, u_long, void *, int, struct lwp *);
164 static void ms_wscons_disable(void *);
165
166 static struct wsmouse_accessops ms_wscons_accessops = {
167 ms_wscons_enable,
168 ms_wscons_ioctl,
169 ms_wscons_disable
170 };
171 #endif
172
173 int
msmatch(device_t parent,cfdata_t cf,void * aux)174 msmatch(device_t parent, cfdata_t cf, void *aux)
175 {
176 static int ms_matched = 0;
177
178 /* Allow only one instance. */
179 if (!matchname((char *)aux, "ms") || ms_matched)
180 return 0;
181
182 ms_matched = 1;
183 return 1;
184 }
185
186 void
msattach(device_t parent,device_t self,void * aux)187 msattach(device_t parent, device_t self, void *aux)
188 {
189 #if NWSMOUSE > 0
190 struct wsmousedev_attach_args waa;
191 #endif
192 struct ms_softc *sc = device_private(self);
193 int i;
194
195 printf("\n");
196 for (i = 0; i < MS_NPORTS; i++) {
197 sc->sc_ports[i].ms_portno = i;
198 callout_init(&sc->sc_ports[i].ms_intr_ch, 0);
199 #if NWSMOUSE > 0
200 waa.accessops = &ms_wscons_accessops;
201 waa.accesscookie = &sc->sc_ports[i];
202
203 sc->sc_ports[i].ms_wsenabled = 0;
204 sc->sc_ports[i].ms_wsmousedev =
205 config_found(self, &waa, wsmousedevprint,
206 CFARGS(.iattr = "wsmousedev"));
207 #endif
208 }
209 }
210
211 /*
212 * Amiga mice are hooked up to one of the two "game" ports, where
213 * the main mouse is usually on the first port, and port 2 can
214 * be used by a joystick. Nevertheless, we support two mouse
215 * devices, /dev/mouse0 and /dev/mouse1 (with a link of /dev/mouse to
216 * the device that represents the port of the mouse in use).
217 */
218
219 /*
220 * enable scanner, called when someone opens the port.
221 */
222 void
ms_enable(struct ms_port * ms)223 ms_enable(struct ms_port *ms)
224 {
225
226 /*
227 * use this as flag to the "interrupt" to tell it when to
228 * shut off (when it's reset to 0).
229 */
230 ms->ms_ready = 1;
231
232 callout_reset(&ms->ms_intr_ch, 2, msintr, ms);
233 }
234
235 /*
236 * disable scanner. Just set ms_ready to 0, and after the next
237 * timeout taken, no further timeouts will be initiated.
238 */
239 void
ms_disable(struct ms_port * ms)240 ms_disable(struct ms_port *ms)
241 {
242 int s;
243
244 s = splhigh ();
245 ms->ms_ready = 0;
246 /*
247 * sync with the interrupt
248 */
249 tsleep(ms, PZERO - 1, "mouse-disable", 0);
250 splx(s);
251 }
252
253
254 /*
255 * we're emulating a mousesystems serial mouse here..
256 */
257 void
msintr(void * arg)258 msintr(void *arg)
259 {
260 static const char to_one[] = { 1, 2, 2, 4, 4, 4, 4 };
261 static const int to_id[] = { MS_RIGHT, MS_MIDDLE, 0, MS_LEFT };
262 struct ms_port *ms = arg;
263 struct firm_event *fe;
264 int mb, ub, d, get, put, any, port;
265 u_char pra, *horc, *verc;
266 u_short pot, count;
267 short dx, dy;
268
269 port = ms->ms_portno;
270
271 horc = ((u_char *) &count) + 1;
272 verc = (u_char *) &count;
273
274 /*
275 * first read the three buttons.
276 */
277 pot = custom.potgor;
278 pra = ciaa.pra;
279 pot >>= port == 0 ? 8 : 12; /* contains right and middle button */
280 pra >>= port == 0 ? 6 : 7; /* contains left button */
281 mb = (pot & 4) / 4 + (pot & 1) * 2 + (pra & 1) * 4;
282 mb ^= 0x07;
283
284 /*
285 * read current values of counter registers
286 */
287 if (port == 0)
288 count = custom.joy0dat;
289 else
290 count = custom.joy1dat;
291
292 /*
293 * take care of wraparound
294 */
295 dx = *horc - ms->ms_horc;
296 if (dx < -127)
297 dx += 255;
298 else if (dx > 127)
299 dx -= 255;
300 dy = *verc - ms->ms_verc;
301 if (dy < -127)
302 dy += 255;
303 else if (dy > 127)
304 dy -= 255;
305
306 /*
307 * remember current values for next scan
308 */
309 ms->ms_horc = *horc;
310 ms->ms_verc = *verc;
311
312 ms->ms_dx = dx;
313 ms->ms_dy = dy;
314 ms->ms_mb = mb;
315
316 #if NWSMOUSE > 0
317 /*
318 * If we have attached wsmouse and we are not opened
319 * directly then pass events to wscons.
320 */
321 if (ms->ms_wsmousedev && ms->ms_wsenabled)
322 {
323 int buttons = 0;
324
325 if (mb & 4)
326 buttons |= 1;
327 if (mb & 2)
328 buttons |= 2;
329 if (mb & 1)
330 buttons |= 4;
331
332 wsmouse_input(ms->ms_wsmousedev,
333 buttons,
334 dx, -dy, 0, 0,
335 WSMOUSE_INPUT_DELTA);
336
337 } else
338 #endif
339 if (dx || dy || ms->ms_ub != ms->ms_mb) {
340 /*
341 * We have at least one event (mouse button, delta-X, or
342 * delta-Y; possibly all three, and possibly three separate
343 * button events). Deliver these events until we are out of
344 * changes or out of room. As events get delivered, mark them
345 * `unchanged'.
346 */
347 any = 0;
348 get = ms->ms_events.ev_get;
349 put = ms->ms_events.ev_put;
350 fe = &ms->ms_events.ev_q[put];
351
352 mb = ms->ms_mb;
353 ub = ms->ms_ub;
354 while ((d = mb ^ ub) != 0) {
355 /*
356 * Mouse button change. Convert up to three changes
357 * to the `first' change, and drop it into the event
358 * queue.
359 */
360 if ((++put) % EV_QSIZE == get) {
361 put--;
362 goto out;
363 }
364
365 d = to_one[d - 1]; /* from 1..7 to {1,2,4} */
366 fe->id = to_id[d - 1]; /* from {1,2,4} to ID */
367 fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
368 firm_gettime(fe);
369 fe++;
370
371 if (put >= EV_QSIZE) {
372 put = 0;
373 fe = &ms->ms_events.ev_q[0];
374 }
375 any = 1;
376
377 ub ^= d;
378 }
379 if (ms->ms_dx) {
380 if ((++put) % EV_QSIZE == get) {
381 put--;
382 goto out;
383 }
384
385 fe->id = LOC_X_DELTA;
386 fe->value = ms->ms_dx;
387 firm_gettime(fe);
388 fe++;
389
390 if (put >= EV_QSIZE) {
391 put = 0;
392 fe = &ms->ms_events.ev_q[0];
393 }
394 any = 1;
395
396 ms->ms_dx = 0;
397 }
398 if (ms->ms_dy) {
399 if ((++put) % EV_QSIZE == get) {
400 put--;
401 goto out;
402 }
403
404 fe->id = LOC_Y_DELTA;
405 fe->value = ms->ms_dy;
406 firm_gettime(fe);
407 fe++;
408
409 if (put >= EV_QSIZE) {
410 put = 0;
411 fe = &ms->ms_events.ev_q[0];
412 }
413 any = 1;
414
415 ms->ms_dy = 0;
416 }
417 out:
418 if (any) {
419 ms->ms_ub = ub;
420 ms->ms_events.ev_put = put;
421 EV_WAKEUP(&ms->ms_events);
422 }
423 }
424
425 /*
426 * reschedule handler, or if terminating,
427 * handshake with ms_disable
428 */
429 if (ms->ms_ready)
430 callout_reset(&ms->ms_intr_ch, 2, msintr, ms);
431 else
432 wakeup(ms);
433 }
434
435 int
msopen(dev_t dev,int flags,int mode,struct lwp * l)436 msopen(dev_t dev, int flags, int mode, struct lwp *l)
437 {
438 struct ms_softc *sc;
439 struct ms_port *ms;
440 int unit, port;
441
442 unit = MS_UNIT(dev);
443 sc = (struct ms_softc *)getsoftc(ms_cd, unit);
444
445 if (sc == NULL)
446 return(EXDEV);
447
448 port = MS_PORT(dev);
449 ms = &sc->sc_ports[port];
450
451 if (ms->ms_events.ev_io)
452 return(EBUSY);
453
454 #if NWSMOUSE > 0
455 /* don't allow opening when sending events to wsmouse */
456 if (ms->ms_wsenabled)
457 return EBUSY;
458 #endif
459 /* initialize potgo bits for mouse mode */
460 custom.potgo = custom.potgor | (0xf00 << (port * 4));
461
462 ms->ms_events.ev_io = l->l_proc;
463 ev_init(&ms->ms_events); /* may cause sleep */
464 ms_enable(ms);
465 return(0);
466 }
467
468 int
msclose(dev_t dev,int flags,int mode,struct lwp * l)469 msclose(dev_t dev, int flags, int mode, struct lwp *l)
470 {
471 struct ms_port *ms;
472
473 ms = MS_DEV2MSPORT(dev);
474
475 ms_disable(ms);
476 ev_fini(&ms->ms_events);
477 ms->ms_events.ev_io = NULL;
478 return(0);
479 }
480
481 int
msread(dev_t dev,struct uio * uio,int flags)482 msread(dev_t dev, struct uio *uio, int flags)
483 {
484 struct ms_port *ms;
485
486 ms = MS_DEV2MSPORT(dev);
487
488 return(ev_read(&ms->ms_events, uio, flags));
489 }
490
491 int
msioctl(dev_t dev,u_long cmd,register void * data,int flag,struct lwp * l)492 msioctl(dev_t dev, u_long cmd, register void *data, int flag,
493 struct lwp *l)
494 {
495 struct ms_port *ms;
496
497 ms = MS_DEV2MSPORT(dev);
498
499 switch (cmd) {
500 case FIONBIO: /* we will remove this someday (soon???) */
501 return(0);
502 case FIOASYNC:
503 ms->ms_events.ev_async = *(int *)data != 0;
504 return(0);
505 case FIOSETOWN:
506 if (-*(int *)data != ms->ms_events.ev_io->p_pgid
507 && *(int *)data != ms->ms_events.ev_io->p_pid)
508 return(EPERM);
509 return(0);
510 case TIOCSPGRP:
511 if (*(int *)data != ms->ms_events.ev_io->p_pgid)
512 return(EPERM);
513 return(0);
514 case VUIDGFORMAT: /* we only do firm_events */
515 *(int *)data = VUID_FIRM_EVENT;
516 return(0);
517 case VUIDSFORMAT:
518 if (*(int *)data != VUID_FIRM_EVENT)
519 return(EINVAL);
520 return(0);
521 }
522 return(ENOTTY);
523 }
524
525 int
mspoll(dev_t dev,int events,struct lwp * l)526 mspoll(dev_t dev, int events, struct lwp *l)
527 {
528 struct ms_port *ms;
529
530 ms = MS_DEV2MSPORT(dev);
531
532 return(ev_poll(&ms->ms_events, events, l));
533 }
534
535 int
mskqfilter(dev_t dev,struct knote * kn)536 mskqfilter(dev_t dev, struct knote *kn)
537 {
538 struct ms_port *ms;
539
540 ms = MS_DEV2MSPORT(dev);
541
542 return (ev_kqfilter(&ms->ms_events, kn));
543 }
544
545 #if NWSMOUSE > 0
546
547 static int
ms_wscons_ioctl(void * cookie,u_long cmd,void * data,int flag,struct lwp * l)548 ms_wscons_ioctl(void *cookie, u_long cmd, void *data, int flag,
549 struct lwp *l)
550 {
551 switch(cmd) {
552 case WSMOUSEIO_GTYPE:
553 *(u_int*)data = WSMOUSE_TYPE_AMIGA;
554 return (0);
555 }
556
557 return EPASSTHROUGH;
558 }
559
560 static int
ms_wscons_enable(void * cookie)561 ms_wscons_enable(void *cookie)
562 {
563 struct ms_port *port = cookie;
564
565 /* somebody reading events from us directly? */
566 if (port->ms_events.ev_io)
567 return EBUSY;
568
569 port->ms_wsenabled = 1;
570 ms_enable(port);
571
572 return 0;
573 }
574
575 static void
ms_wscons_disable(void * cookie)576 ms_wscons_disable(void *cookie)
577 {
578 struct ms_port *port = cookie;
579
580 if (port->ms_wsenabled)
581 ms_disable(port);
582 port->ms_wsenabled = 0;
583 }
584
585 #endif
586
587