1 /* $NetBSD: ms.c,v 1.39 2014/03/16 05:20:29 dholland Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This software was developed by the Computer Systems Engineering group 8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9 * contributed to Berkeley. 10 * 11 * All advertising materials mentioning features or use of this software 12 * must display the following acknowledgement: 13 * This product includes software developed by the University of 14 * California, Lawrence Berkeley Laboratory. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 3. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * @(#)ms.c 8.1 (Berkeley) 6/11/93 41 */ 42 43 /* 44 * Mouse driver (/dev/mouse) 45 */ 46 47 /* 48 * Zilog Z8530 Dual UART driver (mouse interface) 49 * 50 * This is the "slave" driver that will be attached to 51 * the "zsc" driver for a Sun mouse. 52 */ 53 54 #include <sys/cdefs.h> 55 __KERNEL_RCSID(0, "$NetBSD: ms.c,v 1.39 2014/03/16 05:20:29 dholland Exp $"); 56 57 #include <sys/param.h> 58 #include <sys/systm.h> 59 #include <sys/conf.h> 60 #include <sys/device.h> 61 #include <sys/ioctl.h> 62 #include <sys/kernel.h> 63 #include <sys/proc.h> 64 #include <sys/signal.h> 65 #include <sys/signalvar.h> 66 #include <sys/time.h> 67 #include <sys/syslog.h> 68 #include <sys/select.h> 69 #include <sys/poll.h> 70 71 #include <machine/vuid_event.h> 72 73 #include <dev/ic/z8530reg.h> 74 #include <machine/z8530var.h> 75 #include <dev/sun/event_var.h> 76 #include <dev/sun/msvar.h> 77 78 #include <dev/wscons/wsconsio.h> 79 #include <dev/wscons/wsmousevar.h> 80 81 #include "ioconf.h" 82 #include "locators.h" 83 #include "wsmouse.h" 84 85 dev_type_open(msopen); 86 dev_type_close(msclose); 87 dev_type_read(msread); 88 dev_type_ioctl(msioctl); 89 dev_type_poll(mspoll); 90 dev_type_kqfilter(mskqfilter); 91 92 const struct cdevsw ms_cdevsw = { 93 .d_open = msopen, 94 .d_close = msclose, 95 .d_read = msread, 96 .d_write = nowrite, 97 .d_ioctl = msioctl, 98 .d_stop = nostop, 99 .d_tty = notty, 100 .d_poll = mspoll, 101 .d_mmap = nommap, 102 .d_kqfilter = mskqfilter, 103 .d_flag = D_OTHER 104 }; 105 106 /**************************************************************** 107 * Entry points for /dev/mouse 108 * (open,close,read,write,...) 109 ****************************************************************/ 110 111 int 112 msopen(dev_t dev, int flags, int mode, struct lwp *l) 113 { 114 struct ms_softc *ms; 115 116 ms = device_lookup_private(&ms_cd, minor(dev)); 117 if (ms == NULL) 118 return ENXIO; 119 120 /* This is an exclusive open device. */ 121 if (ms->ms_events.ev_io) 122 return EBUSY; 123 124 if (ms->ms_deviopen) { 125 int err; 126 err = (*ms->ms_deviopen)(ms->ms_dev, flags); 127 if (err) 128 return err; 129 } 130 ms->ms_events.ev_io = l->l_proc; 131 ev_init(&ms->ms_events); /* may cause sleep */ 132 133 ms->ms_ready = 1; /* start accepting events */ 134 return 0; 135 } 136 137 int 138 msclose(dev_t dev, int flags, int mode, struct lwp *l) 139 { 140 struct ms_softc *ms; 141 142 ms = device_lookup_private(&ms_cd, minor(dev)); 143 ms->ms_ready = 0; /* stop accepting events */ 144 ev_fini(&ms->ms_events); 145 146 ms->ms_events.ev_io = NULL; 147 if (ms->ms_deviclose) { 148 int err; 149 err = (*ms->ms_deviclose)(ms->ms_dev, flags); 150 if (err) 151 return err; 152 } 153 return 0; 154 } 155 156 int 157 msread(dev_t dev, struct uio *uio, int flags) 158 { 159 struct ms_softc *ms; 160 161 ms = device_lookup_private(&ms_cd, minor(dev)); 162 return ev_read(&ms->ms_events, uio, flags); 163 } 164 165 int 166 msioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 167 { 168 struct ms_softc *ms; 169 170 ms = device_lookup_private(&ms_cd, minor(dev)); 171 172 switch (cmd) { 173 174 case FIONBIO: /* we will remove this someday (soon???) */ 175 return 0; 176 177 case FIOASYNC: 178 ms->ms_events.ev_async = *(int *)data != 0; 179 return 0; 180 181 case FIOSETOWN: 182 if (-*(int *)data != ms->ms_events.ev_io->p_pgid 183 && *(int *)data != ms->ms_events.ev_io->p_pid) 184 return EPERM; 185 return 0; 186 187 case TIOCSPGRP: 188 if (*(int *)data != ms->ms_events.ev_io->p_pgid) 189 return EPERM; 190 return 0; 191 192 case VUIDGFORMAT: 193 /* we only do firm_events */ 194 *(int *)data = VUID_FIRM_EVENT; 195 return 0; 196 197 case VUIDSFORMAT: 198 if (*(int *)data != VUID_FIRM_EVENT) 199 return EINVAL; 200 return 0; 201 } 202 return ENOTTY; 203 } 204 205 int 206 mspoll(dev_t dev, int events, struct lwp *l) 207 { 208 struct ms_softc *ms; 209 210 ms = device_lookup_private(&ms_cd, minor(dev)); 211 return ev_poll(&ms->ms_events, events, l); 212 } 213 214 int 215 mskqfilter(dev_t dev, struct knote *kn) 216 { 217 struct ms_softc *ms; 218 219 ms = device_lookup_private(&ms_cd, minor(dev)); 220 return ev_kqfilter(&ms->ms_events, kn); 221 } 222 223 /**************************************************************** 224 * Middle layer (translator) 225 ****************************************************************/ 226 227 /* 228 * Called by our ms_softint() routine on input. 229 */ 230 void 231 ms_input(struct ms_softc *ms, int c) 232 { 233 struct firm_event *fe; 234 int mb, ub, d, get, put, any; 235 static const char to_one[] = { 1, 2, 2, 4, 4, 4, 4 }; 236 static const int to_id[] = { MS_RIGHT, MS_MIDDLE, 0, MS_LEFT }; 237 238 /* 239 * Discard input if not ready. Drop sync on parity or framing 240 * error; gain sync on button byte. 241 */ 242 if (ms->ms_ready == 0) 243 return; 244 if (c == -1) { 245 ms->ms_byteno = -1; 246 return; 247 } 248 if ((c & 0xb0) == 0x80) { /* if in 0x80..0x8f of 0xc0..0xcf */ 249 if (c & 8) { 250 ms->ms_byteno = 1; /* short form (3 bytes) */ 251 } else { 252 ms->ms_byteno = 0; /* long form (5 bytes) */ 253 } 254 } 255 256 /* 257 * Run the decode loop, adding to the current information. 258 * We add, rather than replace, deltas, so that if the event queue 259 * fills, we accumulate data for when it opens up again. 260 */ 261 switch (ms->ms_byteno) { 262 263 case -1: 264 return; 265 266 case 0: 267 /* buttons (long form) */ 268 ms->ms_byteno = 2; 269 ms->ms_mb = (~c) & 0x7; 270 return; 271 272 case 1: 273 /* buttons (short form) */ 274 ms->ms_byteno = 4; 275 ms->ms_mb = (~c) & 0x7; 276 return; 277 278 case 2: 279 /* first delta-x */ 280 ms->ms_byteno = 3; 281 ms->ms_dx += (char)c; 282 return; 283 284 case 3: 285 /* first delta-y */ 286 ms->ms_byteno = 4; 287 ms->ms_dy += (char)c; 288 return; 289 290 case 4: 291 /* second delta-x */ 292 ms->ms_byteno = 5; 293 ms->ms_dx += (char)c; 294 return; 295 296 case 5: 297 /* second delta-y */ 298 ms->ms_byteno = -1; /* wait for button-byte again */ 299 ms->ms_dy += (char)c; 300 break; 301 302 default: 303 panic("ms_rint"); 304 /* NOTREACHED */ 305 } 306 307 #if NWSMOUSE > 0 308 if (ms->ms_wsmousedev != NULL && ms->ms_ready == 2) { 309 mb = ((ms->ms_mb & 4) >> 2) | 310 (ms->ms_mb & 2) | 311 ((ms->ms_mb & 1) << 2); 312 wsmouse_input(ms->ms_wsmousedev, 313 mb, 314 ms->ms_dx, ms->ms_dy, 0, 0, 315 WSMOUSE_INPUT_DELTA); 316 ms->ms_dx = 0; 317 ms->ms_dy = 0; 318 return; 319 } 320 #endif 321 /* 322 * We have at least one event (mouse button, delta-X, or 323 * delta-Y; possibly all three, and possibly three separate 324 * button events). Deliver these events until we are out 325 * of changes or out of room. As events get delivered, 326 * mark them `unchanged'. 327 */ 328 any = 0; 329 get = ms->ms_events.ev_get; 330 put = ms->ms_events.ev_put; 331 fe = &ms->ms_events.ev_q[put]; 332 333 /* NEXT prepares to put the next event, backing off if necessary */ 334 #define NEXT \ 335 if ((++put) % EV_QSIZE == get) { \ 336 put--; \ 337 goto out; \ 338 } 339 /* ADVANCE completes the `put' of the event */ 340 #define ADVANCE \ 341 fe++; \ 342 if (put >= EV_QSIZE) { \ 343 put = 0; \ 344 fe = &ms->ms_events.ev_q[0]; \ 345 } \ 346 any = 1 347 348 mb = ms->ms_mb; 349 ub = ms->ms_ub; 350 while ((d = mb ^ ub) != 0) { 351 /* 352 * Mouse button change. Convert up to three changes 353 * to the `first' change, and drop it into the event queue. 354 */ 355 NEXT; 356 d = to_one[d - 1]; /* from 1..7 to {1,2,4} */ 357 fe->id = to_id[d - 1]; /* from {1,2,4} to ID */ 358 fe->value = mb & d ? VKEY_DOWN : VKEY_UP; 359 firm_gettime(fe); 360 ADVANCE; 361 ub ^= d; 362 } 363 if (ms->ms_dx) { 364 NEXT; 365 fe->id = LOC_X_DELTA; 366 fe->value = ms->ms_dx; 367 firm_gettime(fe); 368 ADVANCE; 369 ms->ms_dx = 0; 370 } 371 if (ms->ms_dy) { 372 NEXT; 373 fe->id = LOC_Y_DELTA; 374 fe->value = ms->ms_dy; 375 firm_gettime(fe); 376 ADVANCE; 377 ms->ms_dy = 0; 378 } 379 out: 380 if (any) { 381 ms->ms_ub = ub; 382 ms->ms_events.ev_put = put; 383 EV_WAKEUP(&ms->ms_events); 384 } 385 } 386