1 /*- 2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer as 10 * the first lines of this file unmodified. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include "opt_syscons.h" 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/conf.h> 34 #include <sys/proc.h> 35 #include <sys/tty.h> 36 #include <sys/kernel.h> 37 #include <sys/consio.h> 38 #include <sys/mouse.h> 39 40 #include <dev/syscons/syscons.h> 41 42 #ifndef SC_NO_SYSMOUSE 43 44 #define CDEV_MAJOR 12 /* major number, shared with syscons */ 45 #define SC_MOUSE 128 /* minor number */ 46 47 static d_open_t smopen; 48 static d_close_t smclose; 49 static d_ioctl_t smioctl; 50 51 static struct cdevsw sm_cdevsw = { 52 /* open */ smopen, 53 /* close */ smclose, 54 /* read */ ttyread, 55 /* write */ nowrite, 56 /* ioctl */ smioctl, 57 /* poll */ ttypoll, 58 /* mmap */ nommap, 59 /* strategy */ nostrategy, 60 /* name */ "sysmouse", 61 /* maj */ CDEV_MAJOR, 62 /* dump */ nodump, 63 /* psize */ nopsize, 64 /* flags */ D_TTY, 65 /* bmaj */ -1 66 }; 67 68 /* local variables */ 69 static struct tty *sysmouse_tty; 70 static int mouse_level; /* sysmouse protocol level */ 71 static mousestatus_t mouse_status; 72 73 static void smstart(struct tty *tp); 74 static int smparam(struct tty *tp, struct termios *t); 75 76 static int 77 smopen(dev_t dev, int flag, int mode, struct proc *p) 78 { 79 struct tty *tp; 80 81 DPRINTF(5, ("smopen: dev:%d,%d, vty:%d\n", 82 major(dev), minor(dev), SC_VTY(dev))); 83 84 #if 0 85 if (SC_VTY(dev) != SC_MOUSE) 86 return ENXIO; 87 #endif 88 89 tp = dev->si_tty = ttymalloc(dev->si_tty); 90 if (!(tp->t_state & TS_ISOPEN)) { 91 tp->t_oproc = smstart; 92 tp->t_param = smparam; 93 tp->t_stop = nottystop; 94 tp->t_dev = dev; 95 ttychars(tp); 96 tp->t_iflag = TTYDEF_IFLAG; 97 tp->t_oflag = TTYDEF_OFLAG; 98 tp->t_cflag = TTYDEF_CFLAG; 99 tp->t_lflag = TTYDEF_LFLAG; 100 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 101 smparam(tp, &tp->t_termios); 102 (*linesw[tp->t_line].l_modem)(tp, 1); 103 } else if (tp->t_state & TS_XCLUDE && suser(p)) { 104 return EBUSY; 105 } 106 107 return (*linesw[tp->t_line].l_open)(dev, tp); 108 } 109 110 static int 111 smclose(dev_t dev, int flag, int mode, struct proc *p) 112 { 113 struct tty *tp; 114 int s; 115 116 tp = dev->si_tty; 117 s = spltty(); 118 mouse_level = 0; 119 (*linesw[tp->t_line].l_close)(tp, flag); 120 ttyclose(tp); 121 splx(s); 122 123 return 0; 124 } 125 126 static void 127 smstart(struct tty *tp) 128 { 129 struct clist *rbp; 130 u_char buf[PCBURST]; 131 int s; 132 133 s = spltty(); 134 if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) { 135 tp->t_state |= TS_BUSY; 136 rbp = &tp->t_outq; 137 while (rbp->c_cc) 138 q_to_b(rbp, buf, PCBURST); 139 tp->t_state &= ~TS_BUSY; 140 ttwwakeup(tp); 141 } 142 splx(s); 143 } 144 145 static int 146 smparam(struct tty *tp, struct termios *t) 147 { 148 tp->t_ispeed = t->c_ispeed; 149 tp->t_ospeed = t->c_ospeed; 150 tp->t_cflag = t->c_cflag; 151 return 0; 152 } 153 154 static int 155 smioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 156 { 157 struct tty *tp; 158 mousehw_t *hw; 159 mousemode_t *mode; 160 int error; 161 int s; 162 163 tp = dev->si_tty; 164 switch (cmd) { 165 166 case MOUSE_GETHWINFO: /* get device information */ 167 hw = (mousehw_t *)data; 168 hw->buttons = 10; /* XXX unknown */ 169 hw->iftype = MOUSE_IF_SYSMOUSE; 170 hw->type = MOUSE_MOUSE; 171 hw->model = MOUSE_MODEL_GENERIC; 172 hw->hwid = 0; 173 return 0; 174 175 case MOUSE_GETMODE: /* get protocol/mode */ 176 mode = (mousemode_t *)data; 177 mode->level = mouse_level; 178 switch (mode->level) { 179 case 0: /* emulate MouseSystems protocol */ 180 mode->protocol = MOUSE_PROTO_MSC; 181 mode->rate = -1; /* unknown */ 182 mode->resolution = -1; /* unknown */ 183 mode->accelfactor = 0; /* disabled */ 184 mode->packetsize = MOUSE_MSC_PACKETSIZE; 185 mode->syncmask[0] = MOUSE_MSC_SYNCMASK; 186 mode->syncmask[1] = MOUSE_MSC_SYNC; 187 break; 188 189 case 1: /* sysmouse protocol */ 190 mode->protocol = MOUSE_PROTO_SYSMOUSE; 191 mode->rate = -1; 192 mode->resolution = -1; 193 mode->accelfactor = 0; 194 mode->packetsize = MOUSE_SYS_PACKETSIZE; 195 mode->syncmask[0] = MOUSE_SYS_SYNCMASK; 196 mode->syncmask[1] = MOUSE_SYS_SYNC; 197 break; 198 } 199 return 0; 200 201 case MOUSE_SETMODE: /* set protocol/mode */ 202 mode = (mousemode_t *)data; 203 if (mode->level == -1) 204 ; /* don't change the current setting */ 205 else if ((mode->level < 0) || (mode->level > 1)) 206 return EINVAL; 207 else 208 mouse_level = mode->level; 209 return 0; 210 211 case MOUSE_GETLEVEL: /* get operation level */ 212 *(int *)data = mouse_level; 213 return 0; 214 215 case MOUSE_SETLEVEL: /* set operation level */ 216 if ((*(int *)data < 0) || (*(int *)data > 1)) 217 return EINVAL; 218 mouse_level = *(int *)data; 219 return 0; 220 221 case MOUSE_GETSTATUS: /* get accumulated mouse events */ 222 s = spltty(); 223 *(mousestatus_t *)data = mouse_status; 224 mouse_status.flags = 0; 225 mouse_status.obutton = mouse_status.button; 226 mouse_status.dx = 0; 227 mouse_status.dy = 0; 228 mouse_status.dz = 0; 229 splx(s); 230 return 0; 231 232 #if notyet 233 case MOUSE_GETVARS: /* get internal mouse variables */ 234 case MOUSE_SETVARS: /* set internal mouse variables */ 235 return ENODEV; 236 #endif 237 238 case MOUSE_READSTATE: /* read status from the device */ 239 case MOUSE_READDATA: /* read data from the device */ 240 return ENODEV; 241 } 242 243 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); 244 if (error != ENOIOCTL) 245 return error; 246 error = ttioctl(tp, cmd, data, flag); 247 if (error != ENOIOCTL) 248 return error; 249 return ENOTTY; 250 } 251 252 static void 253 sm_attach_mouse(void *unused) 254 { 255 dev_t dev; 256 257 dev = make_dev(&sm_cdevsw, SC_MOUSE, UID_ROOT, GID_WHEEL, 0600, 258 "sysmouse"); 259 dev->si_tty = sysmouse_tty = ttymalloc(sysmouse_tty); 260 /* sysmouse doesn't have scr_stat */ 261 } 262 263 SYSINIT(sysmouse, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR, 264 sm_attach_mouse, NULL) 265 266 int 267 sysmouse_event(mouse_info_t *info) 268 { 269 /* MOUSE_BUTTON?DOWN -> MOUSE_MSC_BUTTON?UP */ 270 static int butmap[8] = { 271 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP, 272 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP, 273 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP, 274 MOUSE_MSC_BUTTON3UP, 275 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP, 276 MOUSE_MSC_BUTTON2UP, 277 MOUSE_MSC_BUTTON1UP, 278 0, 279 }; 280 u_char buf[8]; 281 int x, y, z; 282 int i; 283 284 switch (info->operation) { 285 case MOUSE_ACTION: 286 mouse_status.button = info->u.data.buttons; 287 /* FALL THROUGH */ 288 case MOUSE_MOTION_EVENT: 289 x = info->u.data.x; 290 y = info->u.data.y; 291 z = info->u.data.z; 292 break; 293 case MOUSE_BUTTON_EVENT: 294 x = y = z = 0; 295 if (info->u.event.value > 0) 296 mouse_status.button |= info->u.event.id; 297 else 298 mouse_status.button &= ~info->u.event.id; 299 break; 300 default: 301 return 0; 302 } 303 304 mouse_status.dx += x; 305 mouse_status.dy += y; 306 mouse_status.dz += z; 307 mouse_status.flags |= ((x || y || z) ? MOUSE_POSCHANGED : 0) 308 | (mouse_status.obutton ^ mouse_status.button); 309 if (mouse_status.flags == 0) 310 return 0; 311 312 if ((sysmouse_tty == NULL) || !(sysmouse_tty->t_state & TS_ISOPEN)) 313 return mouse_status.flags; 314 315 /* the first five bytes are compatible with MouseSystems' */ 316 buf[0] = MOUSE_MSC_SYNC 317 | butmap[mouse_status.button & MOUSE_STDBUTTONS]; 318 x = imax(imin(x, 255), -256); 319 buf[1] = x >> 1; 320 buf[3] = x - buf[1]; 321 y = -imax(imin(y, 255), -256); 322 buf[2] = y >> 1; 323 buf[4] = y - buf[2]; 324 for (i = 0; i < MOUSE_MSC_PACKETSIZE; ++i) 325 (*linesw[sysmouse_tty->t_line].l_rint)(buf[i], sysmouse_tty); 326 if (mouse_level >= 1) { 327 /* extended part */ 328 z = imax(imin(z, 127), -128); 329 buf[5] = (z >> 1) & 0x7f; 330 buf[6] = (z - (z >> 1)) & 0x7f; 331 /* buttons 4-10 */ 332 buf[7] = (~mouse_status.button >> 3) & 0x7f; 333 for (i = MOUSE_MSC_PACKETSIZE; i < MOUSE_SYS_PACKETSIZE; ++i) 334 (*linesw[sysmouse_tty->t_line].l_rint)(buf[i], 335 sysmouse_tty); 336 } 337 338 return mouse_status.flags; 339 } 340 341 #endif /* !SC_NO_SYSMOUSE */ 342