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