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