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