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