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.5 2003/07/21 05:50:36 dillon Exp $
30 #include "opt_syscons.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <machine/console.h>
40 #include <machine/mouse.h>
42 #include <dev/syscons/syscons.h>
44 #ifndef SC_NO_SYSMOUSE
46 #define CDEV_MAJOR 12 /* major number, shared with syscons */
47 #define SC_MOUSE 128 /* minor number */
49 static d_open_t smopen;
50 static d_close_t smclose;
51 static d_ioctl_t smioctl;
53 static struct cdevsw sm_cdevsw = {
54 /* name */ "sysmouse",
67 /* strategy */ nostrategy,
73 static struct tty *sysmouse_tty;
74 static int mouse_level; /* sysmouse protocol level */
75 static mousestatus_t mouse_status;
77 static void smstart(struct tty *tp);
78 static int smparam(struct tty *tp, struct termios *t);
81 smopen(dev_t dev, int flag, int mode, struct thread *td)
85 DPRINTF(5, ("smopen: dev:%d,%d, vty:%d\n",
86 major(dev), minor(dev), SC_VTY(dev)));
89 if (SC_VTY(dev) != SC_MOUSE)
93 tp = dev->si_tty = ttymalloc(dev->si_tty);
94 if (!(tp->t_state & TS_ISOPEN)) {
96 tp->t_oproc = smstart;
97 tp->t_param = smparam;
98 tp->t_stop = nottystop;
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)) {
112 return (*linesw[tp->t_line].l_open)(dev, tp);
116 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)
139 if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
140 tp->t_state |= TS_BUSY;
143 q_to_b(rbp, buf, PCBURST);
144 tp->t_state &= ~TS_BUSY;
151 smparam(struct tty *tp, struct termios *t)
153 tp->t_ispeed = t->c_ispeed;
154 tp->t_ospeed = t->c_ospeed;
155 tp->t_cflag = t->c_cflag;
160 smioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
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;
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;
194 case 1: /* sysmouse protocol */
195 mode->protocol = MOUSE_PROTO_SYSMOUSE;
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;
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))
213 mouse_level = mode->level;
216 case MOUSE_GETLEVEL: /* get operation level */
217 *(int *)data = mouse_level;
220 case MOUSE_SETLEVEL: /* set operation level */
221 if ((*(int *)data < 0) || (*(int *)data > 1))
223 mouse_level = *(int *)data;
226 case MOUSE_GETSTATUS: /* get accumulated mouse events */
228 *(mousestatus_t *)data = mouse_status;
229 mouse_status.flags = 0;
230 mouse_status.obutton = mouse_status.button;
238 case MOUSE_GETVARS: /* get internal mouse variables */
239 case MOUSE_SETVARS: /* set internal mouse variables */
243 case MOUSE_READSTATE: /* read status from the device */
244 case MOUSE_READDATA: /* read data from the device */
248 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td);
249 if (error != ENOIOCTL)
251 error = ttioctl(tp, cmd, data, flag);
252 if (error != ENOIOCTL)
258 sm_attach_mouse(void *unused)
262 dev = make_dev(&sm_cdevsw, SC_MOUSE, UID_ROOT, GID_WHEEL, 0600,
264 /* sysmouse doesn't have scr_stat */
267 SYSINIT(sysmouse, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR,
268 sm_attach_mouse, NULL)
271 sysmouse_event(mouse_info_t *info)
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,
279 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
288 switch (info->operation) {
290 mouse_status.button = info->u.data.buttons;
292 case MOUSE_MOTION_EVENT:
297 case MOUSE_BUTTON_EVENT:
299 if (info->u.event.value > 0)
300 mouse_status.button |= info->u.event.id;
302 mouse_status.button &= ~info->u.event.id;
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)
316 if ((sysmouse_tty == NULL) || !(sysmouse_tty->t_state & TS_ISOPEN))
317 return mouse_status.flags;
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);
325 y = -imax(imin(y, 255), -256);
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) {
332 z = imax(imin(z, 127), -128);
333 buf[5] = (z >> 1) & 0x7f;
334 buf[6] = (z - (z >> 1)) & 0x7f;
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],
342 return mouse_status.flags;
345 #endif /* !SC_NO_SYSMOUSE */