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