1 /*- 2 * (MPSAFE) 3 * 4 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer as 12 * the first lines of this file unmodified. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $FreeBSD: src/sys/dev/syscons/sysmouse.c,v 1.2.2.2 2001/07/16 05:21:24 yokota Exp $ 29 */ 30 31 /* MPSAFE NOTE: Take care with locking in sysmouse_event which is called 32 * from syscons. 33 */ 34 #include "opt_syscons.h" 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/conf.h> 39 #include <sys/device.h> 40 #include <sys/event.h> 41 #include <sys/uio.h> 42 #include <sys/priv.h> 43 #include <sys/vnode.h> 44 #include <sys/kernel.h> 45 #include <sys/thread2.h> 46 47 #include <machine/console.h> 48 #include <sys/mouse.h> 49 50 #include "syscons.h" 51 52 #ifndef SC_NO_SYSMOUSE 53 54 #define FIFO_SIZE 256 55 56 struct event_fifo { 57 mouse_info_t buf[FIFO_SIZE]; 58 int start; 59 int fill; 60 unsigned int dropped; 61 }; 62 63 struct sysmouse_state { 64 struct event_fifo *fifo; 65 int level; /* sysmouse protocol level */ 66 mousestatus_t syncstatus; 67 mousestatus_t readstatus; /* Only needed for button status */ 68 int opened; 69 struct lock sm_lock; 70 struct kqinfo rkq; 71 }; 72 73 static d_open_t smopen; 74 static d_close_t smclose; 75 static d_read_t smread; 76 static d_ioctl_t smioctl; 77 static d_kqfilter_t smkqfilter; 78 79 static struct dev_ops sm_ops = { 80 { "sysmouse", 0, D_MPSAFE }, 81 .d_open = smopen, 82 .d_close = smclose, 83 .d_read = smread, 84 .d_ioctl = smioctl, 85 .d_kqfilter = smkqfilter, 86 }; 87 88 /* local variables */ 89 static struct sysmouse_state mouse_state; 90 91 static int sysmouse_evtopkt(struct sysmouse_state *sc, mouse_info_t *info, 92 u_char *buf); 93 static void smqueue(struct sysmouse_state *sc, mouse_info_t *info); 94 static int pktlen(struct sysmouse_state *sc); 95 static void smfilter_detach(struct knote *); 96 static int smfilter(struct knote *, long); 97 static void smget(struct sysmouse_state *sc, mouse_info_t *info); 98 static void smpop(struct sysmouse_state *sc); 99 100 static int 101 pktlen(struct sysmouse_state *sc) 102 { 103 if (sc->level == 0) 104 return 5; 105 else 106 return 8; 107 } 108 109 static int 110 smopen(struct dev_open_args *ap) 111 { 112 cdev_t dev = ap->a_head.a_dev; 113 struct sysmouse_state *sc = &mouse_state; 114 int ret; 115 116 DPRINTF(5, ("smopen: dev:%d,%d\n", 117 major(dev), minor(dev))); 118 119 lockmgr(&sc->sm_lock, LK_EXCLUSIVE); 120 if (!sc->opened) { 121 sc->fifo = kmalloc(sizeof(struct event_fifo), 122 M_SYSCONS, M_WAITOK | M_ZERO); 123 sc->opened = 1; 124 bzero(&sc->readstatus, sizeof(sc->readstatus)); 125 bzero(&sc->syncstatus, sizeof(sc->syncstatus)); 126 ret = 0; 127 } else { 128 ret = EBUSY; 129 } 130 lockmgr(&sc->sm_lock, LK_RELEASE); 131 132 return ret; 133 } 134 135 static int 136 smclose(struct dev_close_args *ap) 137 { 138 struct sysmouse_state *sc = &mouse_state; 139 140 lockmgr(&sc->sm_lock, LK_EXCLUSIVE); 141 sc->opened = 0; 142 sc->level = 0; 143 kfree(sc->fifo, M_SYSCONS); 144 sc->fifo = NULL; 145 lockmgr(&sc->sm_lock, LK_RELEASE); 146 147 return 0; 148 } 149 150 static int 151 smread(struct dev_read_args *ap) 152 { 153 struct sysmouse_state *sc = &mouse_state; 154 mousestatus_t backupstatus; 155 mouse_info_t info; 156 u_char buf[8]; 157 struct uio *uio = ap->a_uio; 158 int error = 0, val, cnt = 0; 159 160 lockmgr(&sc->sm_lock, LK_EXCLUSIVE); 161 while (sc->fifo->fill <= 0) { 162 /* Buffer too small to fit a complete mouse packet */ 163 if (uio->uio_resid < pktlen(sc)) { 164 error = EIO; 165 goto done; 166 } 167 if (ap->a_ioflag & IO_NDELAY) { 168 error = EAGAIN; 169 goto done; 170 } 171 error = lksleep(sc, &sc->sm_lock, PCATCH, "smread", 0); 172 if (error == EINTR || error == ERESTART) { 173 goto done; 174 } 175 } 176 177 do { 178 /* Buffer too small to fit a complete mouse packet */ 179 if (uio->uio_resid < pktlen(sc)) { 180 error = EIO; 181 goto done; 182 } 183 smget(sc, &info); 184 backupstatus = sc->readstatus; 185 val = sysmouse_evtopkt(sc, &info, buf); 186 if (val > 0) { 187 error = uiomove(buf, val, uio); 188 if (error != 0) { 189 sc->readstatus = backupstatus; 190 goto done; 191 } 192 cnt++; 193 } 194 smpop(sc); 195 } while (sc->fifo->fill > 0); 196 197 done: 198 lockmgr(&sc->sm_lock, LK_RELEASE); 199 if (cnt > 0 && error != EFAULT) 200 return 0; 201 return error; 202 } 203 204 static int 205 smioctl(struct dev_ioctl_args *ap) 206 { 207 struct sysmouse_state *sc = &mouse_state; 208 mousehw_t *hw; 209 mousemode_t *mode; 210 211 switch (ap->a_cmd) { 212 case MOUSE_GETHWINFO: /* get device information */ 213 hw = (mousehw_t *)ap->a_data; 214 hw->buttons = 10; /* XXX unknown */ 215 hw->iftype = MOUSE_IF_SYSMOUSE; 216 hw->type = MOUSE_MOUSE; 217 hw->model = MOUSE_MODEL_GENERIC; 218 hw->hwid = 0; 219 return 0; 220 221 case MOUSE_GETMODE: /* get protocol/mode */ 222 mode = (mousemode_t *)ap->a_data; 223 lockmgr(&sc->sm_lock, LK_EXCLUSIVE); 224 mode->level = sc->level; 225 lockmgr(&sc->sm_lock, LK_RELEASE); 226 switch (mode->level) { 227 case 0: /* emulate MouseSystems protocol */ 228 mode->protocol = MOUSE_PROTO_MSC; 229 mode->rate = -1; /* unknown */ 230 mode->resolution = -1; /* unknown */ 231 mode->accelfactor = 0; /* disabled */ 232 mode->packetsize = MOUSE_MSC_PACKETSIZE; 233 mode->syncmask[0] = MOUSE_MSC_SYNCMASK; 234 mode->syncmask[1] = MOUSE_MSC_SYNC; 235 break; 236 237 case 1: /* sysmouse protocol */ 238 mode->protocol = MOUSE_PROTO_SYSMOUSE; 239 mode->rate = -1; 240 mode->resolution = -1; 241 mode->accelfactor = 0; 242 mode->packetsize = MOUSE_SYS_PACKETSIZE; 243 mode->syncmask[0] = MOUSE_SYS_SYNCMASK; 244 mode->syncmask[1] = MOUSE_SYS_SYNC; 245 break; 246 } 247 return 0; 248 249 case MOUSE_SETMODE: /* set protocol/mode */ 250 mode = (mousemode_t *)ap->a_data; 251 if (mode->level == -1) 252 ; /* don't change the current setting */ 253 else if ((mode->level < 0) || (mode->level > 1)) { 254 return EINVAL; 255 } else { 256 lockmgr(&sc->sm_lock, LK_EXCLUSIVE); 257 sc->level = mode->level; 258 lockmgr(&sc->sm_lock, LK_RELEASE); 259 } 260 return 0; 261 262 case MOUSE_GETLEVEL: /* get operation level */ 263 lockmgr(&sc->sm_lock, LK_EXCLUSIVE); 264 *(int *)ap->a_data = sc->level; 265 lockmgr(&sc->sm_lock, LK_RELEASE); 266 return 0; 267 268 case MOUSE_SETLEVEL: /* set operation level */ 269 if ((*(int *)ap->a_data < 0) || (*(int *)ap->a_data > 1)) { 270 return EINVAL; 271 } 272 lockmgr(&sc->sm_lock, LK_EXCLUSIVE); 273 sc->level = *(int *)ap->a_data; 274 lockmgr(&sc->sm_lock, LK_RELEASE); 275 return 0; 276 277 case MOUSE_GETSTATUS: /* get accumulated mouse events */ 278 lockmgr(&sc->sm_lock, LK_EXCLUSIVE); 279 *(mousestatus_t *)ap->a_data = sc->syncstatus; 280 sc->syncstatus.flags = 0; 281 sc->syncstatus.obutton = sc->syncstatus.button; 282 sc->syncstatus.dx = 0; 283 sc->syncstatus.dy = 0; 284 sc->syncstatus.dz = 0; 285 lockmgr(&sc->sm_lock, LK_RELEASE); 286 return 0; 287 288 #if 0 /* notyet */ 289 case MOUSE_GETVARS: /* get internal mouse variables */ 290 case MOUSE_SETVARS: /* set internal mouse variables */ 291 return ENODEV; 292 #endif 293 294 case MOUSE_READSTATE: /* read status from the device */ 295 case MOUSE_READDATA: /* read data from the device */ 296 return ENODEV; 297 } 298 299 return ENOTTY; 300 } 301 302 static struct filterops smfiltops = 303 { FILTEROP_MPSAFE | FILTEROP_ISFD, NULL, smfilter_detach, smfilter }; 304 305 static int 306 smkqfilter(struct dev_kqfilter_args *ap) 307 { 308 struct sysmouse_state *sc = &mouse_state; 309 struct knote *kn = ap->a_kn; 310 struct klist *klist; 311 312 ap->a_result = 0; 313 314 switch (kn->kn_filter) { 315 case EVFILT_READ: 316 kn->kn_fop = &smfiltops; 317 kn->kn_hook = (caddr_t)sc; 318 break; 319 default: 320 ap->a_result = EOPNOTSUPP; 321 return (0); 322 } 323 324 klist = &sc->rkq.ki_note; 325 knote_insert(klist, kn); 326 327 return (0); 328 } 329 330 static void 331 smfilter_detach(struct knote *kn) 332 { 333 struct sysmouse_state *sc = &mouse_state; 334 struct klist *klist; 335 336 klist = &sc->rkq.ki_note; 337 knote_remove(klist, kn); 338 } 339 340 static int 341 smfilter(struct knote *kn, long hint) 342 { 343 struct sysmouse_state *sc = &mouse_state; 344 int ready = 0; 345 346 lockmgr(&sc->sm_lock, LK_EXCLUSIVE); 347 if (sc->fifo->fill > 0) { 348 ready = 1; 349 kn->kn_data = 0; 350 } 351 lockmgr(&sc->sm_lock, LK_RELEASE); 352 353 return ready; 354 } 355 356 static void 357 smqueue(struct sysmouse_state *sc, mouse_info_t *info) 358 { 359 struct event_fifo *f = sc->fifo; 360 361 if (f->fill >= FIFO_SIZE) { 362 f->fill = FIFO_SIZE; 363 f->buf[f->start] = *info; 364 f->start = (f->start + 1) % FIFO_SIZE; 365 f->dropped++; 366 } else { 367 f->buf[(f->start + f->fill) % FIFO_SIZE] = *info; 368 f->fill++; 369 } 370 371 } 372 373 static void 374 smget(struct sysmouse_state *sc, mouse_info_t *info) 375 { 376 struct event_fifo *f = sc->fifo; 377 378 if (f->fill > 0) 379 *info = f->buf[f->start]; 380 } 381 382 static void 383 smpop(struct sysmouse_state *sc) 384 { 385 struct event_fifo *f = sc->fifo; 386 387 if (f->fill > 0) { 388 f->fill--; 389 f->start = (f->start + 1) % FIFO_SIZE; 390 } 391 } 392 393 static void 394 sm_attach_mouse(void *unused) 395 { 396 struct sysmouse_state *sc = &mouse_state; 397 cdev_t dev; 398 399 lockinit(&mouse_state.sm_lock, "sysmouse", 0, LK_CANRECURSE); 400 sc->fifo = NULL; 401 402 dev = make_dev(&sm_ops, 0, UID_ROOT, GID_WHEEL, 0600, "sysmouse"); 403 } 404 405 SYSINIT(sysmouse, SI_SUB_DRIVERS, SI_ORDER_ANY, sm_attach_mouse, NULL); 406 407 static int 408 sysmouse_updatestatus(mousestatus_t *status, mouse_info_t *info) 409 { 410 int x, y, z; 411 412 status->obutton = status->button; 413 414 switch (info->operation) { 415 case MOUSE_ACTION: 416 status->button = info->u.data.buttons; 417 /* FALL THROUGH */ 418 case MOUSE_MOTION_EVENT: 419 x = info->u.data.x; 420 y = info->u.data.y; 421 z = info->u.data.z; 422 break; 423 case MOUSE_BUTTON_EVENT: 424 x = y = z = 0; 425 if (info->u.event.value > 0) 426 status->button |= info->u.event.id; 427 else 428 status->button &= ~info->u.event.id; 429 break; 430 default: 431 return 0; 432 } 433 434 status->dx += x; 435 status->dy += y; 436 status->dz += z; 437 status->flags |= ((x || y || z) ? MOUSE_POSCHANGED : 0) 438 | (status->obutton ^ status->button); 439 440 return 1; 441 } 442 443 /* Requires buf to hold at least 8 bytes, returns number of bytes written */ 444 static int 445 sysmouse_evtopkt(struct sysmouse_state *sc, mouse_info_t *info, u_char *buf) 446 { 447 /* MOUSE_BUTTON?DOWN -> MOUSE_MSC_BUTTON?UP */ 448 static int butmap[8] = { 449 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP, 450 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP, 451 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP, 452 MOUSE_MSC_BUTTON3UP, 453 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP, 454 MOUSE_MSC_BUTTON2UP, 455 MOUSE_MSC_BUTTON1UP, 456 0, 457 }; 458 int x, y, z; 459 460 sc->readstatus.dx = 0; 461 sc->readstatus.dy = 0; 462 sc->readstatus.dz = 0; 463 sc->readstatus.flags = 0; 464 if (sysmouse_updatestatus(&sc->readstatus, info) == 0) 465 return 0; 466 467 /* We aren't using the sc->readstatus.dx/dy/dz values */ 468 469 if (sc->readstatus.flags == 0) 470 return 0; 471 472 x = (info->operation == MOUSE_BUTTON_EVENT ? 0 : info->u.data.x); 473 y = (info->operation == MOUSE_BUTTON_EVENT ? 0 : info->u.data.y); 474 z = (info->operation == MOUSE_BUTTON_EVENT ? 0 : info->u.data.z); 475 476 /* the first five bytes are compatible with MouseSystems' */ 477 buf[0] = MOUSE_MSC_SYNC 478 | butmap[sc->readstatus.button & MOUSE_STDBUTTONS]; 479 x = imax(imin(x, 255), -256); 480 buf[1] = x >> 1; 481 buf[3] = x - buf[1]; 482 y = -imax(imin(y, 255), -256); 483 buf[2] = y >> 1; 484 buf[4] = y - buf[2]; 485 if (sc->level >= 1) { 486 /* extended part */ 487 z = imax(imin(z, 127), -128); 488 buf[5] = (z >> 1) & 0x7f; 489 buf[6] = (z - (z >> 1)) & 0x7f; 490 /* buttons 4-10 */ 491 buf[7] = (~sc->readstatus.button >> 3) & 0x7f; 492 } 493 494 if (sc->level >= 1) 495 return 8; 496 497 return 5; 498 } 499 500 int 501 sysmouse_event(mouse_info_t *info) 502 { 503 struct sysmouse_state *sc = &mouse_state; 504 int ret; 505 506 lockmgr(&sc->sm_lock, LK_EXCLUSIVE); 507 ret = sysmouse_updatestatus(&sc->syncstatus, info); 508 if (ret != 0) 509 ret = sc->syncstatus.flags; 510 if (!sc->opened) { 511 lockmgr(&sc->sm_lock, LK_RELEASE); 512 return ret; 513 } 514 515 switch (info->operation) { 516 case MOUSE_ACTION: 517 case MOUSE_MOTION_EVENT: 518 case MOUSE_BUTTON_EVENT: 519 smqueue(sc, info); 520 lockmgr(&sc->sm_lock, LK_RELEASE); 521 wakeup(sc); 522 KNOTE(&sc->rkq.ki_note, 0); 523 break; 524 default: 525 lockmgr(&sc->sm_lock, LK_RELEASE); 526 break; 527 } 528 529 return ret; 530 } 531 532 #endif /* !SC_NO_SYSMOUSE */ 533