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.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_syscons.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/consio.h>
40 #include <sys/mouse.h>
42 #include <dev/syscons/syscons.h>
44 #ifndef SC_NO_SYSMOUSE
46 #define SC_MOUSE 128 /* minor number */
48 static d_open_t smopen;
49 static d_close_t smclose;
50 static d_ioctl_t smioctl;
52 static struct cdevsw sm_cdevsw = {
53 .d_version = D_VERSION,
58 .d_flags = D_TTY | D_NEEDGIANT,
62 static struct tty *sysmouse_tty;
63 static int mouse_level; /* sysmouse protocol level */
64 static mousestatus_t mouse_status;
66 static void smstart(struct tty *tp);
67 static int smparam(struct tty *tp, struct termios *t);
70 smopen(struct cdev *dev, int flag, int mode, struct thread *td)
74 DPRINTF(5, ("smopen: dev:%s, vty:%d\n",
75 devtoname(dev), SC_VTY(dev)));
78 if (SC_VTY(dev) != SC_MOUSE)
83 if (!(tp->t_state & TS_ISOPEN)) {
84 ttyinitmode(tp, 0, 0);
85 smparam(tp, &tp->t_termios);
87 } else if (tp->t_state & TS_XCLUDE &&
88 priv_check(td, PRIV_TTY_EXCLUSIVE)) {
92 return ttyld_open(tp, dev);
96 smclose(struct cdev *dev, int flag, int mode, struct thread *td)
104 ttyld_close(tp, flag);
112 smstart(struct tty *tp)
119 if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
120 tp->t_state |= TS_BUSY;
123 q_to_b(rbp, buf, PCBURST);
124 tp->t_state &= ~TS_BUSY;
131 smparam(struct tty *tp, struct termios *t)
133 tp->t_ispeed = t->c_ispeed;
134 tp->t_ospeed = t->c_ospeed;
135 tp->t_cflag = t->c_cflag;
140 smioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
150 case MOUSE_GETHWINFO: /* get device information */
151 hw = (mousehw_t *)data;
152 hw->buttons = 10; /* XXX unknown */
153 hw->iftype = MOUSE_IF_SYSMOUSE;
154 hw->type = MOUSE_MOUSE;
155 hw->model = MOUSE_MODEL_GENERIC;
159 case MOUSE_GETMODE: /* get protocol/mode */
160 mode = (mousemode_t *)data;
161 mode->level = mouse_level;
162 switch (mode->level) {
163 case 0: /* emulate MouseSystems protocol */
164 mode->protocol = MOUSE_PROTO_MSC;
165 mode->rate = -1; /* unknown */
166 mode->resolution = -1; /* unknown */
167 mode->accelfactor = 0; /* disabled */
168 mode->packetsize = MOUSE_MSC_PACKETSIZE;
169 mode->syncmask[0] = MOUSE_MSC_SYNCMASK;
170 mode->syncmask[1] = MOUSE_MSC_SYNC;
173 case 1: /* sysmouse protocol */
174 mode->protocol = MOUSE_PROTO_SYSMOUSE;
176 mode->resolution = -1;
177 mode->accelfactor = 0;
178 mode->packetsize = MOUSE_SYS_PACKETSIZE;
179 mode->syncmask[0] = MOUSE_SYS_SYNCMASK;
180 mode->syncmask[1] = MOUSE_SYS_SYNC;
185 case MOUSE_SETMODE: /* set protocol/mode */
186 mode = (mousemode_t *)data;
187 if (mode->level == -1)
188 ; /* don't change the current setting */
189 else if ((mode->level < 0) || (mode->level > 1))
192 mouse_level = mode->level;
195 case MOUSE_GETLEVEL: /* get operation level */
196 *(int *)data = mouse_level;
199 case MOUSE_SETLEVEL: /* set operation level */
200 if ((*(int *)data < 0) || (*(int *)data > 1))
202 mouse_level = *(int *)data;
205 case MOUSE_GETSTATUS: /* get accumulated mouse events */
207 *(mousestatus_t *)data = mouse_status;
208 mouse_status.flags = 0;
209 mouse_status.obutton = mouse_status.button;
217 case MOUSE_GETVARS: /* get internal mouse variables */
218 case MOUSE_SETVARS: /* set internal mouse variables */
222 case MOUSE_READSTATE: /* read status from the device */
223 case MOUSE_READDATA: /* read data from the device */
227 return(ttyioctl(dev, cmd, data, flag, td));
231 sm_attach_mouse(void *unused)
236 dev = make_dev(&sm_cdevsw, SC_MOUSE, UID_ROOT, GID_WHEEL, 0600,
238 dev->si_tty = tp = ttyalloc();
239 tp->t_oproc = smstart;
240 tp->t_param = smparam;
241 tp->t_stop = nottystop;
245 /* sysmouse doesn't have scr_stat */
248 SYSINIT(sysmouse, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, sm_attach_mouse, NULL)
251 sysmouse_event(mouse_info_t *info)
253 /* MOUSE_BUTTON?DOWN -> MOUSE_MSC_BUTTON?UP */
254 static int butmap[8] = {
255 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
256 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
257 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
259 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
268 switch (info->operation) {
270 mouse_status.button = info->u.data.buttons;
272 case MOUSE_MOTION_EVENT:
277 case MOUSE_BUTTON_EVENT:
279 if (info->u.event.value > 0)
280 mouse_status.button |= info->u.event.id;
282 mouse_status.button &= ~info->u.event.id;
288 mouse_status.dx += x;
289 mouse_status.dy += y;
290 mouse_status.dz += z;
291 mouse_status.flags |= ((x || y || z) ? MOUSE_POSCHANGED : 0)
292 | (mouse_status.obutton ^ mouse_status.button);
293 if (mouse_status.flags == 0)
296 if ((sysmouse_tty == NULL) || !(sysmouse_tty->t_state & TS_ISOPEN))
297 return mouse_status.flags;
299 /* the first five bytes are compatible with MouseSystems' */
300 buf[0] = MOUSE_MSC_SYNC
301 | butmap[mouse_status.button & MOUSE_STDBUTTONS];
302 x = imax(imin(x, 255), -256);
305 y = -imax(imin(y, 255), -256);
308 for (i = 0; i < MOUSE_MSC_PACKETSIZE; ++i)
309 ttyld_rint(sysmouse_tty, buf[i]);
310 if (mouse_level >= 1) {
312 z = imax(imin(z, 127), -128);
313 buf[5] = (z >> 1) & 0x7f;
314 buf[6] = (z - (z >> 1)) & 0x7f;
316 buf[7] = (~mouse_status.button >> 3) & 0x7f;
317 for (i = MOUSE_MSC_PACKETSIZE; i < MOUSE_SYS_PACKETSIZE; ++i)
318 ttyld_rint(sysmouse_tty, buf[i]);
321 return mouse_status.flags;
324 #endif /* !SC_NO_SYSMOUSE */