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: This file uses the tty_token mostly for the linesw access and 32 * for the internal mouse structures. The latter ones could be protected 33 * by a local lock. 34 */ 35 #include "opt_syscons.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/conf.h> 40 #include <sys/proc.h> 41 #include <sys/priv.h> 42 #include <sys/tty.h> 43 #include <sys/kernel.h> 44 #include <sys/thread2.h> 45 46 #include <machine/console.h> 47 #include <sys/mouse.h> 48 49 #include "syscons.h" 50 51 #ifndef SC_NO_SYSMOUSE 52 53 #define SC_MOUSE 128 /* minor number */ 54 55 static d_open_t smopen; 56 static d_close_t smclose; 57 static d_ioctl_t smioctl; 58 59 static struct dev_ops sm_ops = { 60 { "sysmouse", 0, D_TTY | D_MPSAFE }, 61 .d_open = smopen, 62 .d_close = smclose, 63 .d_read = ttyread, 64 .d_ioctl = smioctl, 65 .d_kqfilter = ttykqfilter, 66 .d_revoke = ttyrevoke 67 }; 68 69 /* local variables */ 70 static struct tty *sysmouse_tty; 71 static int mouse_level; /* sysmouse protocol level */ 72 static mousestatus_t mouse_status; 73 74 static void smstart(struct tty *tp); 75 static int smparam(struct tty *tp, struct termios *t); 76 77 static int 78 smopen(struct dev_open_args *ap) 79 { 80 cdev_t dev = ap->a_head.a_dev; 81 struct tty *tp; 82 int ret; 83 84 DPRINTF(5, ("smopen: dev:%d,%d, vty:%d\n", 85 major(dev), minor(dev), SC_VTY(dev))); 86 87 #if 0 88 if (SC_VTY(dev) != SC_MOUSE) 89 return ENXIO; 90 #endif 91 92 lwkt_gettoken(&tty_token); 93 tp = dev->si_tty = ttymalloc(dev->si_tty); 94 if (!(tp->t_state & TS_ISOPEN)) { 95 sysmouse_tty = tp; 96 tp->t_oproc = smstart; 97 tp->t_param = smparam; 98 tp->t_stop = nottystop; 99 tp->t_dev = dev; 100 ttychars(tp); 101 tp->t_iflag = 0; 102 tp->t_oflag = 0; 103 tp->t_lflag = 0; 104 tp->t_cflag = TTYDEF_CFLAG; 105 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 106 smparam(tp, &tp->t_termios); 107 (*linesw[tp->t_line].l_modem)(tp, 1); 108 } else if (tp->t_state & TS_XCLUDE && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) { 109 lwkt_reltoken(&tty_token); 110 return EBUSY; 111 } 112 113 ret = (*linesw[tp->t_line].l_open)(dev, tp); 114 lwkt_reltoken(&tty_token); 115 return ret; 116 } 117 118 static int 119 smclose(struct dev_close_args *ap) 120 { 121 cdev_t dev = ap->a_head.a_dev; 122 struct tty *tp; 123 124 tp = dev->si_tty; 125 crit_enter(); 126 lwkt_gettoken(&tty_token); 127 mouse_level = 0; 128 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag); 129 ttyclose(tp); 130 lwkt_reltoken(&tty_token); 131 crit_exit(); 132 133 return 0; 134 } 135 136 static void 137 smstart(struct tty *tp) 138 { 139 struct clist *rbp; 140 u_char buf[PCBURST]; 141 142 crit_enter(); 143 lwkt_gettoken(&tty_token); 144 if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) { 145 tp->t_state |= TS_BUSY; 146 rbp = &tp->t_outq; 147 while (rbp->c_cc) 148 q_to_b(rbp, buf, PCBURST); 149 tp->t_state &= ~TS_BUSY; 150 ttwwakeup(tp); 151 } 152 lwkt_reltoken(&tty_token); 153 crit_exit(); 154 } 155 156 static int 157 smparam(struct tty *tp, struct termios *t) 158 { 159 lwkt_gettoken(&tty_token); 160 tp->t_ispeed = t->c_ispeed; 161 tp->t_ospeed = t->c_ospeed; 162 tp->t_cflag = t->c_cflag; 163 lwkt_reltoken(&tty_token); 164 return 0; 165 } 166 167 static int 168 smioctl(struct dev_ioctl_args *ap) 169 { 170 cdev_t dev = ap->a_head.a_dev; 171 struct tty *tp; 172 mousehw_t *hw; 173 mousemode_t *mode; 174 int error; 175 176 lwkt_gettoken(&tty_token); 177 tp = dev->si_tty; 178 179 switch (ap->a_cmd) { 180 case MOUSE_GETHWINFO: /* get device information */ 181 hw = (mousehw_t *)ap->a_data; 182 hw->buttons = 10; /* XXX unknown */ 183 hw->iftype = MOUSE_IF_SYSMOUSE; 184 hw->type = MOUSE_MOUSE; 185 hw->model = MOUSE_MODEL_GENERIC; 186 hw->hwid = 0; 187 lwkt_reltoken(&tty_token); 188 return 0; 189 190 case MOUSE_GETMODE: /* get protocol/mode */ 191 mode = (mousemode_t *)ap->a_data; 192 mode->level = mouse_level; 193 switch (mode->level) { 194 case 0: /* emulate MouseSystems protocol */ 195 mode->protocol = MOUSE_PROTO_MSC; 196 mode->rate = -1; /* unknown */ 197 mode->resolution = -1; /* unknown */ 198 mode->accelfactor = 0; /* disabled */ 199 mode->packetsize = MOUSE_MSC_PACKETSIZE; 200 mode->syncmask[0] = MOUSE_MSC_SYNCMASK; 201 mode->syncmask[1] = MOUSE_MSC_SYNC; 202 break; 203 204 case 1: /* sysmouse protocol */ 205 mode->protocol = MOUSE_PROTO_SYSMOUSE; 206 mode->rate = -1; 207 mode->resolution = -1; 208 mode->accelfactor = 0; 209 mode->packetsize = MOUSE_SYS_PACKETSIZE; 210 mode->syncmask[0] = MOUSE_SYS_SYNCMASK; 211 mode->syncmask[1] = MOUSE_SYS_SYNC; 212 break; 213 } 214 lwkt_reltoken(&tty_token); 215 return 0; 216 217 case MOUSE_SETMODE: /* set protocol/mode */ 218 mode = (mousemode_t *)ap->a_data; 219 if (mode->level == -1) 220 ; /* don't change the current setting */ 221 else if ((mode->level < 0) || (mode->level > 1)) { 222 lwkt_reltoken(&tty_token); 223 return EINVAL; 224 } else { 225 mouse_level = mode->level; 226 } 227 lwkt_reltoken(&tty_token); 228 return 0; 229 230 case MOUSE_GETLEVEL: /* get operation level */ 231 *(int *)ap->a_data = mouse_level; 232 lwkt_reltoken(&tty_token); 233 return 0; 234 235 case MOUSE_SETLEVEL: /* set operation level */ 236 if ((*(int *)ap->a_data < 0) || (*(int *)ap->a_data > 1)) { 237 lwkt_reltoken(&tty_token); 238 return EINVAL; 239 } 240 mouse_level = *(int *)ap->a_data; 241 lwkt_reltoken(&tty_token); 242 return 0; 243 244 case MOUSE_GETSTATUS: /* get accumulated mouse events */ 245 crit_enter(); 246 *(mousestatus_t *)ap->a_data = mouse_status; 247 mouse_status.flags = 0; 248 mouse_status.obutton = mouse_status.button; 249 mouse_status.dx = 0; 250 mouse_status.dy = 0; 251 mouse_status.dz = 0; 252 crit_exit(); 253 lwkt_reltoken(&tty_token); 254 return 0; 255 256 #if 0 /* notyet */ 257 case MOUSE_GETVARS: /* get internal mouse variables */ 258 case MOUSE_SETVARS: /* set internal mouse variables */ 259 lwkt_reltoken(&tty_token); 260 return ENODEV; 261 #endif 262 263 case MOUSE_READSTATE: /* read status from the device */ 264 case MOUSE_READDATA: /* read data from the device */ 265 lwkt_reltoken(&tty_token); 266 return ENODEV; 267 } 268 269 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data, ap->a_fflag, ap->a_cred); 270 if (error != ENOIOCTL) { 271 lwkt_reltoken(&tty_token); 272 return error; 273 } 274 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag); 275 if (error != ENOIOCTL) { 276 lwkt_reltoken(&tty_token); 277 return error; 278 } 279 lwkt_reltoken(&tty_token); 280 return ENOTTY; 281 } 282 283 static void 284 sm_attach_mouse(void *unused) 285 { 286 cdev_t dev; 287 288 dev = make_dev(&sm_ops, SC_MOUSE, UID_ROOT, GID_WHEEL, 289 0600, "sysmouse"); 290 /* sysmouse doesn't have scr_stat */ 291 } 292 293 SYSINIT(sysmouse, SI_SUB_DRIVERS, SI_ORDER_ANY, sm_attach_mouse, NULL); 294 295 int 296 sysmouse_event(mouse_info_t *info) 297 { 298 /* MOUSE_BUTTON?DOWN -> MOUSE_MSC_BUTTON?UP */ 299 static int butmap[8] = { 300 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP, 301 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP, 302 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP, 303 MOUSE_MSC_BUTTON3UP, 304 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP, 305 MOUSE_MSC_BUTTON2UP, 306 MOUSE_MSC_BUTTON1UP, 307 0, 308 }; 309 u_char buf[8]; 310 int x, y, z; 311 int i; 312 313 lwkt_gettoken(&tty_token); 314 switch (info->operation) { 315 case MOUSE_ACTION: 316 mouse_status.button = info->u.data.buttons; 317 /* FALL THROUGH */ 318 case MOUSE_MOTION_EVENT: 319 x = info->u.data.x; 320 y = info->u.data.y; 321 z = info->u.data.z; 322 break; 323 case MOUSE_BUTTON_EVENT: 324 x = y = z = 0; 325 if (info->u.event.value > 0) 326 mouse_status.button |= info->u.event.id; 327 else 328 mouse_status.button &= ~info->u.event.id; 329 break; 330 default: 331 lwkt_reltoken(&tty_token); 332 return 0; 333 } 334 335 mouse_status.dx += x; 336 mouse_status.dy += y; 337 mouse_status.dz += z; 338 mouse_status.flags |= ((x || y || z) ? MOUSE_POSCHANGED : 0) 339 | (mouse_status.obutton ^ mouse_status.button); 340 if (mouse_status.flags == 0) { 341 lwkt_reltoken(&tty_token); 342 return 0; 343 } 344 345 if ((sysmouse_tty == NULL) || !(sysmouse_tty->t_state & TS_ISOPEN)) { 346 lwkt_reltoken(&tty_token); 347 return mouse_status.flags; 348 } 349 350 /* the first five bytes are compatible with MouseSystems' */ 351 buf[0] = MOUSE_MSC_SYNC 352 | butmap[mouse_status.button & MOUSE_STDBUTTONS]; 353 x = imax(imin(x, 255), -256); 354 buf[1] = x >> 1; 355 buf[3] = x - buf[1]; 356 y = -imax(imin(y, 255), -256); 357 buf[2] = y >> 1; 358 buf[4] = y - buf[2]; 359 for (i = 0; i < MOUSE_MSC_PACKETSIZE; ++i) 360 (*linesw[sysmouse_tty->t_line].l_rint)(buf[i], sysmouse_tty); 361 if (mouse_level >= 1) { 362 /* extended part */ 363 z = imax(imin(z, 127), -128); 364 buf[5] = (z >> 1) & 0x7f; 365 buf[6] = (z - (z >> 1)) & 0x7f; 366 /* buttons 4-10 */ 367 buf[7] = (~mouse_status.button >> 3) & 0x7f; 368 for (i = MOUSE_MSC_PACKETSIZE; i < MOUSE_SYS_PACKETSIZE; ++i) 369 (*linesw[sysmouse_tty->t_line].l_rint)(buf[i], 370 sysmouse_tty); 371 } 372 373 lwkt_reltoken(&tty_token); 374 return mouse_status.flags; 375 } 376 377 #endif /* !SC_NO_SYSMOUSE */ 378