2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
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.9 2005/06/11 00:26:45 dillon Exp $
30 #include "opt_syscons.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/thread2.h>
40 #include <machine/console.h>
41 #include <machine/mouse.h>
45 #ifndef SC_NO_SYSMOUSE
47 #define CDEV_MAJOR 12 /* major number, shared with syscons */
48 #define SC_MOUSE 128 /* minor number */
50 static d_open_t smopen;
51 static d_close_t smclose;
52 static d_ioctl_t smioctl;
54 static struct cdevsw sm_cdevsw = {
55 /* name */ "sysmouse",
68 /* strategy */ nostrategy,
74 static struct tty *sysmouse_tty;
75 static int mouse_level; /* sysmouse protocol level */
76 static mousestatus_t mouse_status;
78 static void smstart(struct tty *tp);
79 static int smparam(struct tty *tp, struct termios *t);
82 smopen(dev_t dev, int flag, int mode, struct thread *td)
86 DPRINTF(5, ("smopen: dev:%d,%d, vty:%d\n",
87 major(dev), minor(dev), SC_VTY(dev)));
90 if (SC_VTY(dev) != SC_MOUSE)
94 tp = dev->si_tty = ttymalloc(dev->si_tty);
95 if (!(tp->t_state & TS_ISOPEN)) {
97 tp->t_oproc = smstart;
98 tp->t_param = smparam;
99 tp->t_stop = nottystop;
102 tp->t_iflag = TTYDEF_IFLAG;
103 tp->t_oflag = TTYDEF_OFLAG;
104 tp->t_cflag = TTYDEF_CFLAG;
105 tp->t_lflag = TTYDEF_LFLAG;
106 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
107 smparam(tp, &tp->t_termios);
108 (*linesw[tp->t_line].l_modem)(tp, 1);
109 } else if (tp->t_state & TS_XCLUDE && suser(td)) {
113 return (*linesw[tp->t_line].l_open)(dev, tp);
117 smclose(dev_t dev, int flag, int mode, struct thread *td)
124 (*linesw[tp->t_line].l_close)(tp, flag);
132 smstart(struct tty *tp)
138 if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
139 tp->t_state |= TS_BUSY;
142 q_to_b(rbp, buf, PCBURST);
143 tp->t_state &= ~TS_BUSY;
150 smparam(struct tty *tp, struct termios *t)
152 tp->t_ispeed = t->c_ispeed;
153 tp->t_ospeed = t->c_ospeed;
154 tp->t_cflag = t->c_cflag;
159 smioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
169 case MOUSE_GETHWINFO: /* get device information */
170 hw = (mousehw_t *)data;
171 hw->buttons = 10; /* XXX unknown */
172 hw->iftype = MOUSE_IF_SYSMOUSE;
173 hw->type = MOUSE_MOUSE;
174 hw->model = MOUSE_MODEL_GENERIC;
178 case MOUSE_GETMODE: /* get protocol/mode */
179 mode = (mousemode_t *)data;
180 mode->level = mouse_level;
181 switch (mode->level) {
182 case 0: /* emulate MouseSystems protocol */
183 mode->protocol = MOUSE_PROTO_MSC;
184 mode->rate = -1; /* unknown */
185 mode->resolution = -1; /* unknown */
186 mode->accelfactor = 0; /* disabled */
187 mode->packetsize = MOUSE_MSC_PACKETSIZE;
188 mode->syncmask[0] = MOUSE_MSC_SYNCMASK;
189 mode->syncmask[1] = MOUSE_MSC_SYNC;
192 case 1: /* sysmouse protocol */
193 mode->protocol = MOUSE_PROTO_SYSMOUSE;
195 mode->resolution = -1;
196 mode->accelfactor = 0;
197 mode->packetsize = MOUSE_SYS_PACKETSIZE;
198 mode->syncmask[0] = MOUSE_SYS_SYNCMASK;
199 mode->syncmask[1] = MOUSE_SYS_SYNC;
204 case MOUSE_SETMODE: /* set protocol/mode */
205 mode = (mousemode_t *)data;
206 if (mode->level == -1)
207 ; /* don't change the current setting */
208 else if ((mode->level < 0) || (mode->level > 1))
211 mouse_level = mode->level;
214 case MOUSE_GETLEVEL: /* get operation level */
215 *(int *)data = mouse_level;
218 case MOUSE_SETLEVEL: /* set operation level */
219 if ((*(int *)data < 0) || (*(int *)data > 1))
221 mouse_level = *(int *)data;
224 case MOUSE_GETSTATUS: /* get accumulated mouse events */
226 *(mousestatus_t *)data = mouse_status;
227 mouse_status.flags = 0;
228 mouse_status.obutton = mouse_status.button;
236 case MOUSE_GETVARS: /* get internal mouse variables */
237 case MOUSE_SETVARS: /* set internal mouse variables */
241 case MOUSE_READSTATE: /* read status from the device */
242 case MOUSE_READDATA: /* read data from the device */
246 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td);
247 if (error != ENOIOCTL)
249 error = ttioctl(tp, cmd, data, flag);
250 if (error != ENOIOCTL)
256 sm_attach_mouse(void *unused)
260 cdevsw_add(&sm_cdevsw, -1, SC_MOUSE);
261 dev = make_dev(&sm_cdevsw, SC_MOUSE, UID_ROOT, GID_WHEEL, 0600,
263 /* sysmouse doesn't have scr_stat */
266 SYSINIT(sysmouse, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR,
267 sm_attach_mouse, NULL)
270 sysmouse_event(mouse_info_t *info)
272 /* MOUSE_BUTTON?DOWN -> MOUSE_MSC_BUTTON?UP */
273 static int butmap[8] = {
274 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
275 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
276 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
278 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
287 switch (info->operation) {
289 mouse_status.button = info->u.data.buttons;
291 case MOUSE_MOTION_EVENT:
296 case MOUSE_BUTTON_EVENT:
298 if (info->u.event.value > 0)
299 mouse_status.button |= info->u.event.id;
301 mouse_status.button &= ~info->u.event.id;
307 mouse_status.dx += x;
308 mouse_status.dy += y;
309 mouse_status.dz += z;
310 mouse_status.flags |= ((x || y || z) ? MOUSE_POSCHANGED : 0)
311 | (mouse_status.obutton ^ mouse_status.button);
312 if (mouse_status.flags == 0)
315 if ((sysmouse_tty == NULL) || !(sysmouse_tty->t_state & TS_ISOPEN))
316 return mouse_status.flags;
318 /* the first five bytes are compatible with MouseSystems' */
319 buf[0] = MOUSE_MSC_SYNC
320 | butmap[mouse_status.button & MOUSE_STDBUTTONS];
321 x = imax(imin(x, 255), -256);
324 y = -imax(imin(y, 255), -256);
327 for (i = 0; i < MOUSE_MSC_PACKETSIZE; ++i)
328 (*linesw[sysmouse_tty->t_line].l_rint)(buf[i], sysmouse_tty);
329 if (mouse_level >= 1) {
331 z = imax(imin(z, 127), -128);
332 buf[5] = (z >> 1) & 0x7f;
333 buf[6] = (z - (z >> 1)) & 0x7f;
335 buf[7] = (~mouse_status.button >> 3) & 0x7f;
336 for (i = MOUSE_MSC_PACKETSIZE; i < MOUSE_SYS_PACKETSIZE; ++i)
337 (*linesw[sysmouse_tty->t_line].l_rint)(buf[i],
341 return mouse_status.flags;
344 #endif /* !SC_NO_SYSMOUSE */