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