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