xref: /netbsd-src/sys/arch/amiga/dev/ms.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: ms.c,v 1.36 2009/03/14 15:36:01 dsl 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.36 2009/03/14 15:36:01 dsl 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(struct device *, struct device *, void *);
89 int msmatch(struct device *, struct cfdata *, 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 	struct device *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 device sc_dev;		/* base device */
115 	struct ms_port sc_ports[MS_NPORTS];
116 };
117 
118 CFATTACH_DECL(ms, sizeof(struct ms_softc),
119     msmatch, msattach, NULL, NULL);
120 
121 void msintr(void *);
122 void ms_enable(struct ms_port *);
123 void ms_disable(struct ms_port *);
124 
125 extern struct cfdriver ms_cd;
126 
127 dev_type_open(msopen);
128 dev_type_close(msclose);
129 dev_type_read(msread);
130 dev_type_ioctl(msioctl);
131 dev_type_poll(mspoll);
132 dev_type_kqfilter(mskqfilter);
133 
134 const struct cdevsw ms_cdevsw = {
135 	msopen, msclose, msread, nowrite, msioctl,
136 	nostop, notty, mspoll, nommap, mskqfilter,
137 };
138 
139 #define	MS_UNIT(d)	((minor(d) & ~0x1) >> 1)
140 #define	MS_PORT(d)	(minor(d) & 0x1)
141 
142 /*
143  * Given a dev_t, return a pointer to the port's hardware state.
144  * Assumes the unit to be valid, so do *not* use this in msopen().
145  */
146 #define	MS_DEV2MSPORT(d) \
147     (&(((struct ms_softc *)getsoftc(ms_cd, MS_UNIT(d)))->sc_ports[MS_PORT(d)]))
148 
149 #if NWSMOUSE > 0
150 /*
151  * Callbacks for wscons.
152  */
153 static int ms_wscons_enable(void *);
154 static int ms_wscons_ioctl(void *, u_long, void *, int, struct lwp *);
155 static void ms_wscons_disable(void *);
156 
157 static struct wsmouse_accessops ms_wscons_accessops = {
158 	ms_wscons_enable,
159 	ms_wscons_ioctl,
160 	ms_wscons_disable
161 };
162 #endif
163 
164 int
165 msmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
166 {
167 	static int ms_matched = 0;
168 
169 	/* Allow only one instance. */
170 	if (!matchname((char *)auxp, "ms") || ms_matched)
171 		return 0;
172 
173 	ms_matched = 1;
174 	return 1;
175 }
176 
177 void
178 msattach(struct device *pdp, struct device *dp, void *auxp)
179 {
180 #if NWSMOUSE > 0
181 	struct wsmousedev_attach_args waa;
182 #endif
183 	struct ms_softc *sc = (void *) dp;
184 	int i;
185 
186 	printf("\n");
187 	for (i = 0; i < MS_NPORTS; i++) {
188 		sc->sc_ports[i].ms_portno = i;
189 		callout_init(&sc->sc_ports[i].ms_intr_ch, 0);
190 #if NWSMOUSE > 0
191 		waa.accessops = &ms_wscons_accessops;
192 		waa.accesscookie = &sc->sc_ports[i];
193 
194 		sc->sc_ports[i].ms_wsenabled = 0;
195 		sc->sc_ports[i].ms_wsmousedev =
196 		    config_found(dp, &waa, wsmousedevprint);
197 #endif
198 	}
199 }
200 
201 /*
202  * Amiga mice are hooked up to one of the two "game" ports, where
203  * the main mouse is usually on the first port, and port 2 can
204  * be used by a joystick. Nevertheless, we support two mouse
205  * devices, /dev/mouse0 and /dev/mouse1 (with a link of /dev/mouse to
206  * the device that represents the port of the mouse in use).
207  */
208 
209 /*
210  * enable scanner, called when someone opens the port.
211  */
212 void
213 ms_enable(struct ms_port *ms)
214 {
215 
216 	/*
217 	 * use this as flag to the "interrupt" to tell it when to
218 	 * shut off (when it's reset to 0).
219 	 */
220 	ms->ms_ready = 1;
221 
222 	callout_reset(&ms->ms_intr_ch, 2, msintr, ms);
223 }
224 
225 /*
226  * disable scanner. Just set ms_ready to 0, and after the next
227  * timeout taken, no further timeouts will be initiated.
228  */
229 void
230 ms_disable(struct ms_port *ms)
231 {
232 	int s;
233 
234 	s = splhigh ();
235 	ms->ms_ready = 0;
236 	/*
237 	 * sync with the interrupt
238 	 */
239 	tsleep(ms, PZERO - 1, "mouse-disable", 0);
240 	splx(s);
241 }
242 
243 
244 /*
245  * we're emulating a mousesystems serial mouse here..
246  */
247 void
248 msintr(void *arg)
249 {
250 	static const char to_one[] = { 1, 2, 2, 4, 4, 4, 4 };
251 	static const int to_id[] = { MS_RIGHT, MS_MIDDLE, 0, MS_LEFT };
252 	struct ms_port *ms = arg;
253 	struct firm_event *fe;
254 	int mb, ub, d, get, put, any, port;
255 	u_char pra, *horc, *verc;
256 	u_short pot, count;
257 	short dx, dy;
258 
259 	port = ms->ms_portno;
260 
261 	horc = ((u_char *) &count) + 1;
262 	verc = (u_char *) &count;
263 
264 	/*
265 	 * first read the three buttons.
266 	 */
267 	pot  = custom.potgor;
268 	pra  = ciaa.pra;
269 	pot >>= port == 0 ? 8 : 12;	/* contains right and middle button */
270 	pra >>= port == 0 ? 6 : 7;	/* contains left button */
271 	mb = (pot & 4) / 4 + (pot & 1) * 2 + (pra & 1) * 4;
272 	mb ^= 0x07;
273 
274 	/*
275 	 * read current values of counter registers
276 	 */
277 	if (port == 0)
278 		count = custom.joy0dat;
279 	else
280 		count = custom.joy1dat;
281 
282 	/*
283 	 * take care of wraparound
284 	 */
285 	dx = *horc - ms->ms_horc;
286 	if (dx < -127)
287 		dx += 255;
288 	else if (dx > 127)
289 		dx -= 255;
290 	dy = *verc - ms->ms_verc;
291 	if (dy < -127)
292 		dy += 255;
293 	else if (dy > 127)
294 		dy -= 255;
295 
296 	/*
297 	 * remember current values for next scan
298 	 */
299 	ms->ms_horc = *horc;
300 	ms->ms_verc = *verc;
301 
302 	ms->ms_dx = dx;
303 	ms->ms_dy = dy;
304 	ms->ms_mb = mb;
305 
306 #if NWSMOUSE > 0
307 	/*
308 	 * If we have attached wsmouse and we are not opened
309 	 * directly then pass events to wscons.
310 	 */
311 	if (ms->ms_wsmousedev && ms->ms_wsenabled)
312 	{
313 		int buttons = 0;
314 
315 		if (mb & 4)
316 			buttons |= 1;
317 		if (mb & 2)
318 			buttons |= 2;
319 		if (mb & 1)
320 			buttons |= 4;
321 
322 		wsmouse_input(ms->ms_wsmousedev,
323 			      buttons,
324 			      dx, -dy, 0, 0,
325 			      WSMOUSE_INPUT_DELTA);
326 
327 	} else
328 #endif
329 	if (dx || dy || ms->ms_ub != ms->ms_mb) {
330 		/*
331 		 * We have at least one event (mouse button, delta-X, or
332 		 * delta-Y; possibly all three, and possibly three separate
333 		 * button events).  Deliver these events until we are out of
334 		 * changes or out of room.  As events get delivered, mark them
335 		 * `unchanged'.
336 		 */
337 		any = 0;
338 		get = ms->ms_events.ev_get;
339 		put = ms->ms_events.ev_put;
340 		fe = &ms->ms_events.ev_q[put];
341 
342 		mb = ms->ms_mb;
343 		ub = ms->ms_ub;
344 		while ((d = mb ^ ub) != 0) {
345 			/*
346 			 * Mouse button change.  Convert up to three changes
347 			 * to the `first' change, and drop it into the event
348 			 * queue.
349 			 */
350 			if ((++put) % EV_QSIZE == get) {
351 				put--;
352 				goto out;
353 			}
354 
355 			d = to_one[d - 1];	/* from 1..7 to {1,2,4} */
356 			fe->id = to_id[d - 1];	/* from {1,2,4} to ID */
357 			fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
358 			firm_gettime(fe);
359 			fe++;
360 
361 			if (put >= EV_QSIZE) {
362 				put = 0;
363 				fe = &ms->ms_events.ev_q[0];
364 			}
365 			any = 1;
366 
367 			ub ^= d;
368 		}
369 		if (ms->ms_dx) {
370 			if ((++put) % EV_QSIZE == get) {
371 				put--;
372 				goto out;
373 			}
374 
375 			fe->id = LOC_X_DELTA;
376 			fe->value = ms->ms_dx;
377 			firm_gettime(fe);
378 			fe++;
379 
380 			if (put >= EV_QSIZE) {
381 				put = 0;
382 				fe = &ms->ms_events.ev_q[0];
383 			}
384 			any = 1;
385 
386 			ms->ms_dx = 0;
387 		}
388 		if (ms->ms_dy) {
389 			if ((++put) % EV_QSIZE == get) {
390 				put--;
391 				goto out;
392 			}
393 
394 			fe->id = LOC_Y_DELTA;
395 			fe->value = ms->ms_dy;
396 			firm_gettime(fe);
397 			fe++;
398 
399 			if (put >= EV_QSIZE) {
400 				put = 0;
401 				fe = &ms->ms_events.ev_q[0];
402 			}
403 			any = 1;
404 
405 			ms->ms_dy = 0;
406 		}
407 out:
408 		if (any) {
409 			ms->ms_ub = ub;
410 			ms->ms_events.ev_put = put;
411 			EV_WAKEUP(&ms->ms_events);
412 		}
413 	}
414 
415 	/*
416 	 * reschedule handler, or if terminating,
417 	 * handshake with ms_disable
418 	 */
419 	if (ms->ms_ready)
420 		callout_reset(&ms->ms_intr_ch, 2, msintr, ms);
421 	else
422 		wakeup(ms);
423 }
424 
425 int
426 msopen(dev_t dev, int flags, int mode, struct lwp *l)
427 {
428 	struct ms_softc *sc;
429 	struct ms_port *ms;
430 	int unit, port;
431 
432 	unit = MS_UNIT(dev);
433 	sc = (struct ms_softc *)getsoftc(ms_cd, unit);
434 
435 	if (sc == NULL)
436 		return(EXDEV);
437 
438 	port = MS_PORT(dev);
439 	ms = &sc->sc_ports[port];
440 
441 	if (ms->ms_events.ev_io)
442 		return(EBUSY);
443 
444 #if NWSMOUSE > 0
445 	/* don't allow opening when sending events to wsmouse */
446 	if (ms->ms_wsenabled)
447 		return EBUSY;
448 #endif
449 	/* initialize potgo bits for mouse mode */
450 	custom.potgo = custom.potgor | (0xf00 << (port * 4));
451 
452 	ms->ms_events.ev_io = l->l_proc;
453 	ev_init(&ms->ms_events);	/* may cause sleep */
454 	ms_enable(ms);
455 	return(0);
456 }
457 
458 int
459 msclose(dev_t dev, int flags, int mode, struct lwp *l)
460 {
461 	struct ms_port *ms;
462 
463 	ms = MS_DEV2MSPORT(dev);
464 
465 	ms_disable(ms);
466 	ev_fini(&ms->ms_events);
467 	ms->ms_events.ev_io = NULL;
468 	return(0);
469 }
470 
471 int
472 msread(dev_t dev, struct uio *uio, int flags)
473 {
474 	struct ms_port *ms;
475 
476 	ms = MS_DEV2MSPORT(dev);
477 
478 	return(ev_read(&ms->ms_events, uio, flags));
479 }
480 
481 int
482 msioctl(dev_t dev, u_long cmd, register void *data, int flag,
483         struct lwp *l)
484 {
485 	struct ms_port *ms;
486 
487 	ms = MS_DEV2MSPORT(dev);
488 
489 	switch (cmd) {
490 	case FIONBIO:		/* we will remove this someday (soon???) */
491 		return(0);
492 	case FIOASYNC:
493 		ms->ms_events.ev_async = *(int *)data != 0;
494 		return(0);
495 	case FIOSETOWN:
496 		if (-*(int *)data != ms->ms_events.ev_io->p_pgid
497 		    && *(int *)data != ms->ms_events.ev_io->p_pid)
498 			return(EPERM);
499 		return(0);
500 	case TIOCSPGRP:
501 		if (*(int *)data != ms->ms_events.ev_io->p_pgid)
502 			return(EPERM);
503 		return(0);
504 	case VUIDGFORMAT:	/* we only do firm_events */
505 		*(int *)data = VUID_FIRM_EVENT;
506 		return(0);
507 	case VUIDSFORMAT:
508 		if (*(int *)data != VUID_FIRM_EVENT)
509 			return(EINVAL);
510 		return(0);
511 	}
512 	return(ENOTTY);
513 }
514 
515 int
516 mspoll(dev_t dev, int events, struct lwp *l)
517 {
518 	struct ms_port *ms;
519 
520 	ms = MS_DEV2MSPORT(dev);
521 
522 	return(ev_poll(&ms->ms_events, events, l));
523 }
524 
525 int
526 mskqfilter(dev_t dev, struct knote *kn)
527 {
528 	struct ms_port *ms;
529 
530 	ms = MS_DEV2MSPORT(dev);
531 
532 	return (ev_kqfilter(&ms->ms_events, kn));
533 }
534 
535 #if NWSMOUSE > 0
536 
537 static int
538 ms_wscons_ioctl(void *cookie, u_long cmd, void *data, int flag,
539 		struct lwp *l)
540 {
541 	switch(cmd) {
542 	case WSMOUSEIO_GTYPE:
543 		*(u_int*)data = WSMOUSE_TYPE_AMIGA;
544 		return (0);
545 	}
546 
547 	return -1;
548 }
549 
550 static int
551 ms_wscons_enable(void *cookie)
552 {
553 	struct ms_port *port = cookie;
554 
555 	/* somebody reading events from us directly? */
556 	if (port->ms_events.ev_io)
557 		return EBUSY;
558 
559 	port->ms_wsenabled = 1;
560 	ms_enable(port);
561 
562 	return 0;
563 }
564 
565 static void
566 ms_wscons_disable(void *cookie)
567 {
568 	struct ms_port *port = cookie;
569 
570 	if (port->ms_wsenabled)
571 		ms_disable(port);
572 	port->ms_wsenabled = 0;
573 }
574 
575 #endif
576 
577