1 /* $NetBSD: ms.c,v 1.28 2022/06/26 06:25:09 tsutsui 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.28 2022/06/26 06:25:09 tsutsui 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 static dev_type_open(msopen); 87 static dev_type_close(msclose); 88 static dev_type_read(msread); 89 static dev_type_ioctl(msioctl); 90 static dev_type_poll(mspoll); 91 static dev_type_kqfilter(mskqfilter); 92 93 const struct cdevsw ms_cdevsw = { 94 .d_open = msopen, 95 .d_close = msclose, 96 .d_read = msread, 97 .d_write = nowrite, 98 .d_ioctl = msioctl, 99 .d_stop = nostop, 100 .d_tty = notty, 101 .d_poll = mspoll, 102 .d_mmap = nommap, 103 .d_kqfilter = mskqfilter, 104 .d_discard = nodiscard, 105 .d_flag = 0 106 }; 107 108 static void ms_3b_delay(struct ms_softc *); 109 110 int 111 mouseattach(int cnt) 112 { 113 114 printf("1 mouse configured\n"); 115 ms_softc[0].ms_emul3b = 1; 116 callout_init(&ms_softc[0].ms_delay_ch, 0); 117 118 return NMOUSE; 119 } 120 121 static void 122 ms_3b_delay(struct ms_softc *ms) 123 { 124 REL_MOUSE rel_ms; 125 126 rel_ms.id = TIMEOUT_ID; 127 rel_ms.dx = rel_ms.dy = 0; 128 mouse_soft(&rel_ms, sizeof(rel_ms), KBD_TIMEO_PKG); 129 } 130 /* 131 * Note that we are called from the keyboard software interrupt! 132 */ 133 void 134 mouse_soft(REL_MOUSE *rel_ms, int size, int type) 135 { 136 struct ms_softc *ms = &ms_softc[0]; 137 struct firm_event *fe, *fe2; 138 REL_MOUSE fake_mouse; 139 int get, put; 140 int s; 141 uint8_t mbut, bmask; 142 int flush_buttons; 143 144 if (ms->ms_events.ev_io == NULL) 145 return; 146 147 switch (type) { 148 case KBD_JOY1_PKG: 149 /* 150 * Ignore if in emulation mode 151 */ 152 if (ms->ms_emul3b) 153 return; 154 155 /* 156 * There are some mice that have their middle button 157 * wired to the 'up' bit of joystick 1.... 158 * Simulate a mouse packet with dx = dy = 0, the middle 159 * button state set by UP and the other buttons unchanged. 160 * Flush all button changes. 161 */ 162 flush_buttons = 1; 163 fake_mouse.id = ((rel_ms->dx & 0x01) != 0 ? 0x04 : 0x00) | 164 (ms->ms_buttons & 0x03); 165 fake_mouse.dx = fake_mouse.dy = 0; 166 rel_ms = &fake_mouse; 167 break; 168 case KBD_TIMEO_PKG: 169 /* 170 * Timeout package. No button changes and no movement. 171 * Flush all button changes. 172 */ 173 flush_buttons = 1; 174 fake_mouse.id = ms->ms_buttons; 175 fake_mouse.dx = fake_mouse.dy = 0; 176 rel_ms = &fake_mouse; 177 break; 178 case KBD_RMS_PKG: 179 /* 180 * Normal mouse package. Always copy the middle button 181 * status. The emulation code decides if button changes 182 * must be flushed. 183 */ 184 rel_ms->id = (ms->ms_buttons & 0x04) | (rel_ms->id & 0x03); 185 flush_buttons = ms->ms_emul3b ? 0 : 1; 186 break; 187 default: 188 return; 189 } 190 191 s = splev(); 192 get = ms->ms_events.ev_get; 193 put = ms->ms_events.ev_put; 194 fe = &ms->ms_events.ev_q[put]; 195 196 if ((type != KBD_TIMEO_PKG) && ms->ms_emul3b && ms->ms_bq_idx != 0) 197 callout_stop(&ms->ms_delay_ch); 198 199 /* 200 * Button states are encoded in the lower 3 bits of 'id' 201 */ 202 if ((mbut = (rel_ms->id ^ ms->ms_buttons)) == 0 && (put != get)) { 203 /* 204 * Compact dx/dy messages. Always generate an event when 205 * a button is pressed or the event queue is empty. 206 */ 207 ms->ms_dx += rel_ms->dx; 208 ms->ms_dy += rel_ms->dy; 209 goto out; 210 } 211 rel_ms->dx += ms->ms_dx; 212 rel_ms->dy += ms->ms_dy; 213 ms->ms_dx = ms->ms_dy = 0; 214 215 /* 216 * Output location events _before_ button events ie. make sure 217 * the button is pressed at the correct location. 218 */ 219 if (rel_ms->dx != 0) { 220 if ((++put) % EV_QSIZE == get) { 221 put--; 222 goto out; 223 } 224 fe->id = LOC_X_DELTA; 225 fe->value = rel_ms->dx; 226 firm_gettime(fe); 227 if (put >= EV_QSIZE) { 228 put = 0; 229 fe = &ms->ms_events.ev_q[0]; 230 } else { 231 fe++; 232 } 233 } 234 if (rel_ms->dy != 0) { 235 if ((++put) % EV_QSIZE == get) { 236 put--; 237 goto out; 238 } 239 fe->id = LOC_Y_DELTA; 240 fe->value = rel_ms->dy; 241 firm_gettime(fe); 242 if (put >= EV_QSIZE) { 243 put = 0; 244 fe = &ms->ms_events.ev_q[0]; 245 } else { 246 fe++; 247 } 248 } 249 if (mbut != 0 && (type != KBD_TIMEO_PKG)) { 250 for (bmask = 0x01; bmask < 0x08; bmask <<= 1) { 251 if ((mbut & bmask) == 0) 252 continue; 253 fe2 = &ms->ms_bq[ms->ms_bq_idx++]; 254 if (bmask == 0x01) 255 fe2->id = MS_RIGHT; 256 else if (bmask == 0x02) 257 fe2->id = MS_LEFT; 258 else 259 fe2->id = MS_MIDDLE; 260 fe2->value = 261 (rel_ms->id & bmask) != 0 ? VKEY_DOWN : VKEY_UP; 262 firm_gettime(fe2); 263 } 264 } 265 266 /* 267 * Handle 3rd button emulation. 268 */ 269 if (ms->ms_emul3b && ms->ms_bq_idx != 0 && (type != KBD_TIMEO_PKG)) { 270 /* 271 * If the middle button is pressed, any change to 272 * one of the other buttons releases all. 273 */ 274 if ((ms->ms_buttons & 0x04) != 0 && (mbut & 0x03) != 0) { 275 ms->ms_bq[0].id = MS_MIDDLE; 276 ms->ms_bq_idx = 1; 277 rel_ms->id = 0; 278 flush_buttons = 1; 279 goto out; 280 } 281 if (ms->ms_bq_idx == 2) { 282 if (ms->ms_bq[0].value == ms->ms_bq[1].value) { 283 /* Must be 2 button presses! */ 284 ms->ms_bq[0].id = MS_MIDDLE; 285 ms->ms_bq_idx = 1; 286 rel_ms->id = 7; 287 } 288 } 289 else if (ms->ms_bq[0].value == VKEY_DOWN) { 290 callout_reset(&ms->ms_delay_ch, 10, 291 (FPV)ms_3b_delay, (void *)ms); 292 goto out; 293 } 294 flush_buttons = 1; 295 } 296 out: 297 if (flush_buttons) { 298 int i; 299 300 for (i = 0; i < ms->ms_bq_idx; i++) { 301 if ((++put) % EV_QSIZE == get) { 302 ms->ms_bq_idx = 0; 303 put--; 304 goto out; 305 } 306 *fe = ms->ms_bq[i]; 307 if (put >= EV_QSIZE) { 308 put = 0; 309 fe = &ms->ms_events.ev_q[0]; 310 } else { 311 fe++; 312 } 313 } 314 ms->ms_bq_idx = 0; 315 } 316 ms->ms_events.ev_put = put; 317 ms->ms_buttons = rel_ms->id; 318 splx(s); 319 EV_WAKEUP(&ms->ms_events); 320 } 321 322 static int 323 msopen(dev_t dev, int flags, int mode, struct lwp *l) 324 { 325 uint8_t report_ms_joy[] = { 0x14, 0x08 }; 326 struct ms_softc *ms; 327 int unit; 328 329 unit = minor(dev); 330 ms = &ms_softc[unit]; 331 332 if (unit >= NMOUSE) 333 return EXDEV; 334 335 if (ms->ms_events.ev_io) 336 return EBUSY; 337 338 ms->ms_events.ev_io = l->l_proc; 339 ms->ms_dx = ms->ms_dy = 0; 340 ms->ms_buttons = 0; 341 ms->ms_bq[0].id = ms->ms_bq[1].id = 0; 342 ms->ms_bq_idx = 0; 343 ev_init(&ms->ms_events); /* may cause sleep */ 344 345 /* 346 * Enable mouse reporting. 347 */ 348 kbd_write(report_ms_joy, sizeof(report_ms_joy)); 349 return 0; 350 } 351 352 static int 353 msclose(dev_t dev, int flags, int mode, struct lwp *l) 354 { 355 uint8_t disable_ms_joy[] = { 0x12, 0x1a }; 356 int unit; 357 struct ms_softc *ms; 358 359 unit = minor(dev); 360 ms = &ms_softc[unit]; 361 362 /* 363 * Turn off mouse interrogation. 364 */ 365 kbd_write(disable_ms_joy, sizeof(disable_ms_joy)); 366 ev_fini(&ms->ms_events); 367 ms->ms_events.ev_io = NULL; 368 return 0; 369 } 370 371 static int 372 msread(dev_t dev, struct uio *uio, int flags) 373 { 374 struct ms_softc *ms; 375 376 ms = &ms_softc[minor(dev)]; 377 return ev_read(&ms->ms_events, uio, flags); 378 } 379 380 static int 381 msioctl(dev_t dev, u_long cmd, register void * data, int flag, struct lwp *l) 382 { 383 struct ms_softc *ms; 384 int unit; 385 386 unit = minor(dev); 387 ms = &ms_softc[unit]; 388 389 switch (cmd) { 390 case MIOCS3B_EMUL: 391 ms->ms_emul3b = (*(int *)data != 0) ? 1 : 0; 392 return 0; 393 case MIOCG3B_EMUL: 394 *(int *)data = ms->ms_emul3b; 395 return 0; 396 case FIONBIO: /* we will remove this someday (soon???) */ 397 return 0; 398 case FIOASYNC: 399 ms->ms_events.ev_async = *(int *)data != 0; 400 return 0; 401 case FIOSETOWN: 402 if (-*(int *)data != ms->ms_events.ev_io->p_pgid && 403 *(int *)data != ms->ms_events.ev_io->p_pid) 404 return EPERM; 405 return 0; 406 case TIOCSPGRP: 407 if (*(int *)data != ms->ms_events.ev_io->p_pgid) 408 return EPERM; 409 return 0; 410 case VUIDGFORMAT: /* we only do firm_events */ 411 *(int *)data = VUID_FIRM_EVENT; 412 return 0; 413 case VUIDSFORMAT: 414 if (*(int *)data != VUID_FIRM_EVENT) 415 return EINVAL; 416 return 0; 417 } 418 return ENOTTY; 419 } 420 421 static int 422 mspoll(dev_t dev, int events, struct lwp *l) 423 { 424 struct ms_softc *ms; 425 426 ms = &ms_softc[minor(dev)]; 427 return ev_poll(&ms->ms_events, events, l); 428 } 429 430 static int 431 mskqfilter(dev_t dev, struct knote *kn) 432 { 433 struct ms_softc *ms; 434 435 ms = &ms_softc[minor(dev)]; 436 return ev_kqfilter(&ms->ms_events, kn); 437 } 438 #endif /* NMOUSE > 0 */ 439