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 38 #include <machine/console.h> 39 #include <machine/mouse.h> 40 41 #include <dev/syscons/syscons.h> 42 43 #ifndef SC_NO_SYSMOUSE 44 45 #define CDEV_MAJOR 12 /* major number, shared with syscons */ 46 #define SC_MOUSE 128 /* minor number */ 47 48 static d_open_t smopen; 49 static d_close_t smclose; 50 static d_ioctl_t smioctl; 51 52 static struct cdevsw sm_cdevsw = { 53 /* open */ smopen, 54 /* close */ smclose, 55 /* read */ ttyread, 56 /* write */ nowrite, 57 /* ioctl */ smioctl, 58 /* poll */ ttypoll, 59 /* mmap */ nommap, 60 /* strategy */ nostrategy, 61 /* name */ "sysmouse", 62 /* maj */ CDEV_MAJOR, 63 /* dump */ nodump, 64 /* psize */ nopsize, 65 /* flags */ D_TTY, 66 /* bmaj */ -1 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(dev_t dev, int flag, int mode, struct proc *p) 79 { 80 struct tty *tp; 81 82 DPRINTF(5, ("smopen: dev:%d,%d, vty:%d\n", 83 major(dev), minor(dev), SC_VTY(dev))); 84 85 #if 0 86 if (SC_VTY(dev) != SC_MOUSE) 87 return ENXIO; 88 #endif 89 90 tp = dev->si_tty = ttymalloc(dev->si_tty); 91 if (!(tp->t_state & TS_ISOPEN)) { 92 tp->t_oproc = smstart; 93 tp->t_param = smparam; 94 tp->t_stop = nottystop; 95 tp->t_dev = dev; 96 ttychars(tp); 97 tp->t_iflag = TTYDEF_IFLAG; 98 tp->t_oflag = TTYDEF_OFLAG; 99 tp->t_cflag = TTYDEF_CFLAG; 100 tp->t_lflag = TTYDEF_LFLAG; 101 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 102 smparam(tp, &tp->t_termios); 103 (*linesw[tp->t_line].l_modem)(tp, 1); 104 } else if (tp->t_state & TS_XCLUDE && suser(p)) { 105 return EBUSY; 106 } 107 108 return (*linesw[tp->t_line].l_open)(dev, tp); 109 } 110 111 static int 112 smclose(dev_t dev, int flag, int mode, struct proc *p) 113 { 114 struct tty *tp; 115 int s; 116 117 tp = dev->si_tty; 118 s = spltty(); 119 mouse_level = 0; 120 (*linesw[tp->t_line].l_close)(tp, flag); 121 ttyclose(tp); 122 splx(s); 123 124 return 0; 125 } 126 127 static void 128 smstart(struct tty *tp) 129 { 130 struct clist *rbp; 131 u_char buf[PCBURST]; 132 int s; 133 134 s = spltty(); 135 if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) { 136 tp->t_state |= TS_BUSY; 137 rbp = &tp->t_outq; 138 while (rbp->c_cc) 139 q_to_b(rbp, buf, PCBURST); 140 tp->t_state &= ~TS_BUSY; 141 ttwwakeup(tp); 142 } 143 splx(s); 144 } 145 146 static int 147 smparam(struct tty *tp, struct termios *t) 148 { 149 tp->t_ispeed = t->c_ispeed; 150 tp->t_ospeed = t->c_ospeed; 151 tp->t_cflag = t->c_cflag; 152 return 0; 153 } 154 155 static int 156 smioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 157 { 158 struct tty *tp; 159 mousehw_t *hw; 160 mousemode_t *mode; 161 int error; 162 int s; 163 164 tp = dev->si_tty; 165 switch (cmd) { 166 167 case MOUSE_GETHWINFO: /* get device information */ 168 hw = (mousehw_t *)data; 169 hw->buttons = 10; /* XXX unknown */ 170 hw->iftype = MOUSE_IF_SYSMOUSE; 171 hw->type = MOUSE_MOUSE; 172 hw->model = MOUSE_MODEL_GENERIC; 173 hw->hwid = 0; 174 return 0; 175 176 case MOUSE_GETMODE: /* get protocol/mode */ 177 mode = (mousemode_t *)data; 178 mode->level = mouse_level; 179 switch (mode->level) { 180 case 0: /* emulate MouseSystems protocol */ 181 mode->protocol = MOUSE_PROTO_MSC; 182 mode->rate = -1; /* unknown */ 183 mode->resolution = -1; /* unknown */ 184 mode->accelfactor = 0; /* disabled */ 185 mode->packetsize = MOUSE_MSC_PACKETSIZE; 186 mode->syncmask[0] = MOUSE_MSC_SYNCMASK; 187 mode->syncmask[1] = MOUSE_MSC_SYNC; 188 break; 189 190 case 1: /* sysmouse protocol */ 191 mode->protocol = MOUSE_PROTO_SYSMOUSE; 192 mode->rate = -1; 193 mode->resolution = -1; 194 mode->accelfactor = 0; 195 mode->packetsize = MOUSE_SYS_PACKETSIZE; 196 mode->syncmask[0] = MOUSE_SYS_SYNCMASK; 197 mode->syncmask[1] = MOUSE_SYS_SYNC; 198 break; 199 } 200 return 0; 201 202 case MOUSE_SETMODE: /* set protocol/mode */ 203 mode = (mousemode_t *)data; 204 if ((mode->level < 0) || (mode->level > 1)) 205 return EINVAL; 206 mouse_level = mode->level; 207 return 0; 208 209 case MOUSE_GETLEVEL: /* get operation level */ 210 *(int *)data = mouse_level; 211 return 0; 212 213 case MOUSE_SETLEVEL: /* set operation level */ 214 if ((*(int *)data < 0) || (*(int *)data > 1)) 215 return EINVAL; 216 mouse_level = *(int *)data; 217 return 0; 218 219 case MOUSE_GETSTATUS: /* get accumulated mouse events */ 220 s = spltty(); 221 *(mousestatus_t *)data = mouse_status; 222 mouse_status.flags = 0; 223 mouse_status.obutton = mouse_status.button; 224 mouse_status.dx = 0; 225 mouse_status.dy = 0; 226 mouse_status.dz = 0; 227 splx(s); 228 return 0; 229 230 #if notyet 231 case MOUSE_GETVARS: /* get internal mouse variables */ 232 case MOUSE_SETVARS: /* set internal mouse variables */ 233 return ENODEV; 234 #endif 235 236 case MOUSE_READSTATE: /* read status from the device */ 237 case MOUSE_READDATA: /* read data from the device */ 238 return ENODEV; 239 } 240 241 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); 242 if (error != ENOIOCTL) 243 return error; 244 error = ttioctl(tp, cmd, data, flag); 245 if (error != ENOIOCTL) 246 return error; 247 return ENOTTY; 248 } 249 250 static void 251 sm_attach_mouse(void *unused) 252 { 253 dev_t dev; 254 255 dev = make_dev(&sm_cdevsw, SC_MOUSE, UID_ROOT, GID_WHEEL, 0600, 256 "sysmouse"); 257 dev->si_tty = sysmouse_tty = ttymalloc(sysmouse_tty); 258 /* sysmouse doesn't have scr_stat */ 259 } 260 261 SYSINIT(sysmouse, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR, 262 sm_attach_mouse, NULL) 263 264 int 265 sysmouse_event(mouse_info_t *info) 266 { 267 /* MOUSE_BUTTON?DOWN -> MOUSE_MSC_BUTTON?UP */ 268 static int butmap[8] = { 269 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP, 270 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP, 271 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP, 272 MOUSE_MSC_BUTTON3UP, 273 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP, 274 MOUSE_MSC_BUTTON2UP, 275 MOUSE_MSC_BUTTON1UP, 276 0, 277 }; 278 u_char buf[8]; 279 int x, y, z; 280 int i; 281 282 switch (info->operation) { 283 case MOUSE_ACTION: 284 mouse_status.button = info->u.data.buttons; 285 /* FALL THROUGH */ 286 case MOUSE_MOTION_EVENT: 287 x = info->u.data.x; 288 y = info->u.data.y; 289 z = info->u.data.z; 290 break; 291 case MOUSE_BUTTON_EVENT: 292 x = y = z = 0; 293 if (info->u.event.value > 0) 294 mouse_status.button |= info->u.event.id; 295 else 296 mouse_status.button &= ~info->u.event.id; 297 break; 298 default: 299 return 0; 300 } 301 302 mouse_status.dx += x; 303 mouse_status.dy += y; 304 mouse_status.dz += z; 305 mouse_status.flags |= ((x || y || z) ? MOUSE_POSCHANGED : 0) 306 | (mouse_status.obutton ^ mouse_status.button); 307 if (mouse_status.flags == 0) 308 return 0; 309 310 if ((sysmouse_tty == NULL) || !(sysmouse_tty->t_state & TS_ISOPEN)) 311 return mouse_status.flags; 312 313 /* the first five bytes are compatible with MouseSystems' */ 314 buf[0] = MOUSE_MSC_SYNC 315 | butmap[mouse_status.button & MOUSE_STDBUTTONS]; 316 x = imax(imin(x, 255), -256); 317 buf[1] = x >> 1; 318 buf[3] = x - buf[1]; 319 y = -imax(imin(y, 255), -256); 320 buf[2] = y >> 1; 321 buf[4] = y - buf[2]; 322 for (i = 0; i < MOUSE_MSC_PACKETSIZE; ++i) 323 (*linesw[sysmouse_tty->t_line].l_rint)(buf[i], sysmouse_tty); 324 if (mouse_level >= 1) { 325 /* extended part */ 326 z = imax(imin(z, 127), -128); 327 buf[5] = (z >> 1) & 0x7f; 328 buf[6] = (z - (z >> 1)) & 0x7f; 329 /* buttons 4-10 */ 330 buf[7] = (~mouse_status.button >> 3) & 0x7f; 331 for (i = MOUSE_MSC_PACKETSIZE; i < MOUSE_SYS_PACKETSIZE; ++i) 332 (*linesw[sysmouse_tty->t_line].l_rint)(buf[i], 333 sysmouse_tty); 334 } 335 336 return mouse_status.flags; 337 } 338 339 #endif /* !SC_NO_SYSMOUSE */ 340