ms.c revision 1.23 1 /* $NetBSD: ms.c,v 1.23 2009/03/14 15:36:03 dsl Exp $ */
2
3 /*
4 * Copyright (c) 1995 Leo Weppelman.
5 * All rights reserved.
6 *
7 * based on:
8 *
9 * Copyright (c) 1992, 1993
10 * The Regents of the University of California. All rights reserved.
11 *
12 * This software was developed by the Computer Systems Engineering group
13 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
14 * contributed to Berkeley.
15 *
16 * All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Lawrence Berkeley Laboratory.
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 * 3. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
44 *
45 * @(#)ms.c 8.1 (Berkeley) 6/11/93
46 *
47 * Header: ms.c,v 1.5 92/11/26 01:28:47 torek Exp (LBL)
48 */
49
50 /*
51 * Mouse driver.
52 */
53
54 #include <sys/cdefs.h>
55 __KERNEL_RCSID(0, "$NetBSD: ms.c,v 1.23 2009/03/14 15:36:03 dsl Exp $");
56
57 #include <sys/param.h>
58 #include <sys/conf.h>
59 #include <sys/ioctl.h>
60 #include <sys/kernel.h>
61 #include <sys/proc.h>
62 #include <sys/systm.h>
63 #include <sys/callout.h>
64 #include <sys/tty.h>
65 #include <sys/signalvar.h>
66
67 #include <machine/msioctl.h>
68 #include <atari/dev/event_var.h>
69 #include <atari/dev/vuid_event.h>
70 #include <atari/dev/kbdvar.h>
71 #include <atari/dev/msvar.h>
72
73 #include "mouse.h"
74 #if NMOUSE > 0
75
76 /* there's really no more physical ports on an atari. */
77 #if NMOUSE > 1
78 #undef NMOUSE
79 #define NMOUSE 1
80 #endif
81
82 typedef void (*FPV)(void *);
83
84 static struct ms_softc ms_softc[NMOUSE];
85
86 dev_type_open(msopen);
87 dev_type_close(msclose);
88 dev_type_read(msread);
89 dev_type_ioctl(msioctl);
90 dev_type_poll(mspoll);
91 dev_type_kqfilter(mskqfilter);
92
93 const struct cdevsw ms_cdevsw = {
94 msopen, msclose, msread, nowrite, msioctl,
95 nostop, notty, mspoll, nommap, mskqfilter,
96 };
97
98 static void ms_3b_delay(struct ms_softc *);
99
100 int
101 mouseattach(int cnt)
102 {
103 printf("1 mouse configured\n");
104 ms_softc[0].ms_emul3b = 1;
105 callout_init(&ms_softc[0].ms_delay_ch, 0);
106 return(NMOUSE);
107 }
108
109 static void
110 ms_3b_delay(struct ms_softc *ms)
111 {
112 REL_MOUSE rel_ms;
113
114 rel_ms.id = TIMEOUT_ID;
115 rel_ms.dx = rel_ms.dy = 0;
116 mouse_soft(&rel_ms, sizeof(rel_ms), KBD_TIMEO_PKG);
117 }
118 /*
119 * Note that we are called from the keyboard software interrupt!
120 */
121 void
122 mouse_soft(rel_ms, size, type)
123 REL_MOUSE *rel_ms;
124 int size, type;
125 {
126 struct ms_softc *ms = &ms_softc[0];
127 struct firm_event *fe, *fe2;
128 REL_MOUSE fake_mouse;
129 int get, put;
130 int sps;
131 u_char mbut, bmask;
132 int flush_buttons;
133
134 if (ms->ms_events.ev_io == NULL)
135 return;
136
137 switch (type) {
138 case KBD_JOY1_PKG:
139 /*
140 * Ignore if in emulation mode
141 */
142 if (ms->ms_emul3b)
143 return;
144
145 /*
146 * There are some mice that have their middle button
147 * wired to the 'up' bit of joystick 1....
148 * Simulate a mouse packet with dx = dy = 0, the middle
149 * button state set by UP and the other buttons unchanged.
150 * Flush all button changes.
151 */
152 flush_buttons = 1;
153 fake_mouse.id = (rel_ms->dx & 1 ? 4 : 0) | (ms->ms_buttons & 3);
154 fake_mouse.dx = fake_mouse.dy = 0;
155 rel_ms = &fake_mouse;
156 break;
157 case KBD_TIMEO_PKG:
158 /*
159 * Timeout package. No button changes and no movement.
160 * Flush all button changes.
161 */
162 flush_buttons = 1;
163 fake_mouse.id = ms->ms_buttons;
164 fake_mouse.dx = fake_mouse.dy = 0;
165 rel_ms = &fake_mouse;
166 break;
167 case KBD_RMS_PKG:
168 /*
169 * Normal mouse package. Always copy the middle button
170 * status. The emulation code decides if button changes
171 * must be flushed.
172 */
173 rel_ms->id = (ms->ms_buttons & 4) | (rel_ms->id & 3);
174 flush_buttons = (ms->ms_emul3b) ? 0 : 1;
175 break;
176 default:
177 return;
178 }
179
180 sps = splev();
181 get = ms->ms_events.ev_get;
182 put = ms->ms_events.ev_put;
183 fe = &ms->ms_events.ev_q[put];
184
185 if ((type != KBD_TIMEO_PKG) && ms->ms_emul3b && ms->ms_bq_idx)
186 callout_stop(&ms->ms_delay_ch);
187
188 /*
189 * Button states are encoded in the lower 3 bits of 'id'
190 */
191 if (!(mbut = (rel_ms->id ^ ms->ms_buttons)) && (put != get)) {
192 /*
193 * Compact dx/dy messages. Always generate an event when
194 * a button is pressed or the event queue is empty.
195 */
196 ms->ms_dx += rel_ms->dx;
197 ms->ms_dy += rel_ms->dy;
198 goto out;
199 }
200 rel_ms->dx += ms->ms_dx;
201 rel_ms->dy += ms->ms_dy;
202 ms->ms_dx = ms->ms_dy = 0;
203
204 /*
205 * Output location events _before_ button events ie. make sure
206 * the button is pressed at the correct location.
207 */
208 if (rel_ms->dx) {
209 if ((++put) % EV_QSIZE == get) {
210 put--;
211 goto out;
212 }
213 fe->id = LOC_X_DELTA;
214 fe->value = rel_ms->dx;
215 firm_gettime(fe);
216 if (put >= EV_QSIZE) {
217 put = 0;
218 fe = &ms->ms_events.ev_q[0];
219 }
220 else fe++;
221 }
222 if (rel_ms->dy) {
223 if ((++put) % EV_QSIZE == get) {
224 put--;
225 goto out;
226 }
227 fe->id = LOC_Y_DELTA;
228 fe->value = rel_ms->dy;
229 firm_gettime(fe);
230 if (put >= EV_QSIZE) {
231 put = 0;
232 fe = &ms->ms_events.ev_q[0];
233 }
234 else fe++;
235 }
236 if (mbut && (type != KBD_TIMEO_PKG)) {
237 for (bmask = 1; bmask < 0x08; bmask <<= 1) {
238 if (!(mbut & bmask))
239 continue;
240 fe2 = &ms->ms_bq[ms->ms_bq_idx++];
241 if (bmask == 1)
242 fe2->id = MS_RIGHT;
243 else if (bmask == 2)
244 fe2->id = MS_LEFT;
245 else fe2->id = MS_MIDDLE;
246 fe2->value = rel_ms->id & bmask ? VKEY_DOWN : VKEY_UP;
247 firm_gettime(fe2);
248 }
249 }
250
251 /*
252 * Handle 3rd button emulation.
253 */
254 if (ms->ms_emul3b && ms->ms_bq_idx && (type != KBD_TIMEO_PKG)) {
255 /*
256 * If the middle button is pressed, any change to
257 * one of the other buttons releases all.
258 */
259 if ((ms->ms_buttons & 4) && (mbut & 3)) {
260 ms->ms_bq[0].id = MS_MIDDLE;
261 ms->ms_bq_idx = 1;
262 rel_ms->id = 0;
263 flush_buttons = 1;
264 goto out;
265 }
266 if (ms->ms_bq_idx == 2) {
267 if (ms->ms_bq[0].value == ms->ms_bq[1].value) {
268 /* Must be 2 button presses! */
269 ms->ms_bq[0].id = MS_MIDDLE;
270 ms->ms_bq_idx = 1;
271 rel_ms->id = 7;
272 }
273 }
274 else if (ms->ms_bq[0].value == VKEY_DOWN) {
275 callout_reset(&ms->ms_delay_ch, 10,
276 (FPV)ms_3b_delay, (void *)ms);
277 goto out;
278 }
279 flush_buttons = 1;
280 }
281 out:
282 if (flush_buttons) {
283 int i;
284
285 for (i = 0; i < ms->ms_bq_idx; i++) {
286 if ((++put) % EV_QSIZE == get) {
287 ms->ms_bq_idx = 0;
288 put--;
289 goto out;
290 }
291 *fe = ms->ms_bq[i];
292 if (put >= EV_QSIZE) {
293 put = 0;
294 fe = &ms->ms_events.ev_q[0];
295 }
296 else fe++;
297 }
298 ms->ms_bq_idx = 0;
299 }
300 ms->ms_events.ev_put = put;
301 ms->ms_buttons = rel_ms->id;
302 splx(sps);
303 EV_WAKEUP(&ms->ms_events);
304 }
305
306 int
307 msopen(dev, flags, mode, l)
308 dev_t dev;
309 int flags, mode;
310 struct lwp *l;
311 {
312 u_char report_ms_joy[] = { 0x14, 0x08 };
313 struct ms_softc *ms;
314 int unit;
315
316 unit = minor(dev);
317 ms = &ms_softc[unit];
318
319 if (unit >= NMOUSE)
320 return(EXDEV);
321
322 if (ms->ms_events.ev_io)
323 return(EBUSY);
324
325 ms->ms_events.ev_io = l->l_proc;
326 ms->ms_dx = ms->ms_dy = 0;
327 ms->ms_buttons = 0;
328 ms->ms_bq[0].id = ms->ms_bq[1].id = 0;
329 ms->ms_bq_idx = 0;
330 ev_init(&ms->ms_events); /* may cause sleep */
331
332 /*
333 * Enable mouse reporting.
334 */
335 kbd_write(report_ms_joy, sizeof(report_ms_joy));
336 return(0);
337 }
338
339 int
340 msclose(dev, flags, mode, l)
341 dev_t dev;
342 int flags, mode;
343 struct lwp *l;
344 {
345 u_char disable_ms_joy[] = { 0x12, 0x1a };
346 int unit;
347 struct ms_softc *ms;
348
349 unit = minor (dev);
350 ms = &ms_softc[unit];
351
352 /*
353 * Turn off mouse interrogation.
354 */
355 kbd_write(disable_ms_joy, sizeof(disable_ms_joy));
356 ev_fini(&ms->ms_events);
357 ms->ms_events.ev_io = NULL;
358 return(0);
359 }
360
361 int
362 msread(dev_t dev, struct uio *uio, int flags)
363 {
364 struct ms_softc *ms;
365
366 ms = &ms_softc[minor(dev)];
367 return(ev_read(&ms->ms_events, uio, flags));
368 }
369
370 int
371 msioctl(dev_t dev, u_long cmd, register void * data, int flag, struct lwp *l)
372 {
373 struct ms_softc *ms;
374 int unit;
375
376 unit = minor(dev);
377 ms = &ms_softc[unit];
378
379 switch (cmd) {
380 case MIOCS3B_EMUL:
381 ms->ms_emul3b = (*(int *)data != 0) ? 1 : 0;
382 return (0);
383 case MIOCG3B_EMUL:
384 *(int *)data = ms->ms_emul3b;
385 return (0);
386 case FIONBIO: /* we will remove this someday (soon???) */
387 return(0);
388 case FIOASYNC:
389 ms->ms_events.ev_async = *(int *)data != 0;
390 return(0);
391 case FIOSETOWN:
392 if (-*(int *)data != ms->ms_events.ev_io->p_pgid
393 && *(int *)data != ms->ms_events.ev_io->p_pid)
394 return(EPERM);
395 return(0);
396 case TIOCSPGRP:
397 if (*(int *)data != ms->ms_events.ev_io->p_pgid)
398 return(EPERM);
399 return(0);
400 case VUIDGFORMAT: /* we only do firm_events */
401 *(int *)data = VUID_FIRM_EVENT;
402 return(0);
403 case VUIDSFORMAT:
404 if (*(int *)data != VUID_FIRM_EVENT)
405 return(EINVAL);
406 return(0);
407 }
408 return(ENOTTY);
409 }
410
411 int
412 mspoll(dev_t dev, int events, struct lwp *l)
413 {
414 struct ms_softc *ms;
415
416 ms = &ms_softc[minor(dev)];
417 return(ev_poll(&ms->ms_events, events, l));
418 }
419
420 int
421 mskqfilter(dev_t dev, struct knote *kn)
422 {
423 struct ms_softc *ms;
424
425 ms = &ms_softc[minor(dev)];
426 return (ev_kqfilter(&ms->ms_events, kn));
427 }
428 #endif /* NMOUSE > 0 */
429