4 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer as
12 * the first lines of this file unmodified.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * $FreeBSD: src/sys/dev/syscons/sysmouse.c,v 1.2.2.2 2001/07/16 05:21:24 yokota Exp $
31 /* MPSAFE NOTE: This file uses the tty_token mostly for the linesw access and
32 * for the internal mouse structures. The latter ones could be protected
35 #include "opt_syscons.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/thread2.h>
46 #include <machine/console.h>
47 #include <sys/mouse.h>
51 #ifndef SC_NO_SYSMOUSE
53 #define SC_MOUSE 128 /* minor number */
55 static d_open_t smopen;
56 static d_close_t smclose;
57 static d_ioctl_t smioctl;
59 static struct dev_ops sm_ops = {
60 { "sysmouse", 0, D_TTY },
65 .d_kqfilter = ttykqfilter,
70 static struct tty *sysmouse_tty;
71 static int mouse_level; /* sysmouse protocol level */
72 static mousestatus_t mouse_status;
74 static void smstart(struct tty *tp);
75 static int smparam(struct tty *tp, struct termios *t);
78 smopen(struct dev_open_args *ap)
80 cdev_t dev = ap->a_head.a_dev;
84 DPRINTF(5, ("smopen: dev:%d,%d, vty:%d\n",
85 major(dev), minor(dev), SC_VTY(dev)));
88 if (SC_VTY(dev) != SC_MOUSE)
92 lwkt_gettoken(&tty_token);
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;
104 tp->t_cflag = TTYDEF_CFLAG;
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 && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
109 lwkt_reltoken(&tty_token);
113 ret = (*linesw[tp->t_line].l_open)(dev, tp);
114 lwkt_reltoken(&tty_token);
119 smclose(struct dev_close_args *ap)
121 cdev_t dev = ap->a_head.a_dev;
126 lwkt_gettoken(&tty_token);
128 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
130 lwkt_reltoken(&tty_token);
137 smstart(struct tty *tp)
143 lwkt_gettoken(&tty_token);
144 if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
145 tp->t_state |= TS_BUSY;
148 q_to_b(rbp, buf, PCBURST);
149 tp->t_state &= ~TS_BUSY;
152 lwkt_reltoken(&tty_token);
157 smparam(struct tty *tp, struct termios *t)
159 lwkt_gettoken(&tty_token);
160 tp->t_ispeed = t->c_ispeed;
161 tp->t_ospeed = t->c_ospeed;
162 tp->t_cflag = t->c_cflag;
163 lwkt_reltoken(&tty_token);
168 smioctl(struct dev_ioctl_args *ap)
170 cdev_t dev = ap->a_head.a_dev;
176 lwkt_gettoken(&tty_token);
180 case MOUSE_GETHWINFO: /* get device information */
181 hw = (mousehw_t *)ap->a_data;
182 hw->buttons = 10; /* XXX unknown */
183 hw->iftype = MOUSE_IF_SYSMOUSE;
184 hw->type = MOUSE_MOUSE;
185 hw->model = MOUSE_MODEL_GENERIC;
187 lwkt_reltoken(&tty_token);
190 case MOUSE_GETMODE: /* get protocol/mode */
191 mode = (mousemode_t *)ap->a_data;
192 mode->level = mouse_level;
193 switch (mode->level) {
194 case 0: /* emulate MouseSystems protocol */
195 mode->protocol = MOUSE_PROTO_MSC;
196 mode->rate = -1; /* unknown */
197 mode->resolution = -1; /* unknown */
198 mode->accelfactor = 0; /* disabled */
199 mode->packetsize = MOUSE_MSC_PACKETSIZE;
200 mode->syncmask[0] = MOUSE_MSC_SYNCMASK;
201 mode->syncmask[1] = MOUSE_MSC_SYNC;
204 case 1: /* sysmouse protocol */
205 mode->protocol = MOUSE_PROTO_SYSMOUSE;
207 mode->resolution = -1;
208 mode->accelfactor = 0;
209 mode->packetsize = MOUSE_SYS_PACKETSIZE;
210 mode->syncmask[0] = MOUSE_SYS_SYNCMASK;
211 mode->syncmask[1] = MOUSE_SYS_SYNC;
214 lwkt_reltoken(&tty_token);
217 case MOUSE_SETMODE: /* set protocol/mode */
218 mode = (mousemode_t *)ap->a_data;
219 if (mode->level == -1)
220 ; /* don't change the current setting */
221 else if ((mode->level < 0) || (mode->level > 1)) {
222 lwkt_reltoken(&tty_token);
225 mouse_level = mode->level;
227 lwkt_reltoken(&tty_token);
230 case MOUSE_GETLEVEL: /* get operation level */
231 *(int *)ap->a_data = mouse_level;
232 lwkt_reltoken(&tty_token);
235 case MOUSE_SETLEVEL: /* set operation level */
236 if ((*(int *)ap->a_data < 0) || (*(int *)ap->a_data > 1)) {
237 lwkt_reltoken(&tty_token);
240 mouse_level = *(int *)ap->a_data;
241 lwkt_reltoken(&tty_token);
244 case MOUSE_GETSTATUS: /* get accumulated mouse events */
246 *(mousestatus_t *)ap->a_data = mouse_status;
247 mouse_status.flags = 0;
248 mouse_status.obutton = mouse_status.button;
253 lwkt_reltoken(&tty_token);
257 case MOUSE_GETVARS: /* get internal mouse variables */
258 case MOUSE_SETVARS: /* set internal mouse variables */
259 lwkt_reltoken(&tty_token);
263 case MOUSE_READSTATE: /* read status from the device */
264 case MOUSE_READDATA: /* read data from the device */
265 lwkt_reltoken(&tty_token);
269 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data, ap->a_fflag, ap->a_cred);
270 if (error != ENOIOCTL) {
271 lwkt_reltoken(&tty_token);
274 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
275 if (error != ENOIOCTL) {
276 lwkt_reltoken(&tty_token);
279 lwkt_reltoken(&tty_token);
284 sm_attach_mouse(void *unused)
288 dev = make_dev(&sm_ops, SC_MOUSE, UID_ROOT, GID_WHEEL,
290 /* sysmouse doesn't have scr_stat */
293 SYSINIT(sysmouse, SI_SUB_DRIVERS, SI_ORDER_ANY, sm_attach_mouse, NULL);
296 sysmouse_event(mouse_info_t *info)
298 /* MOUSE_BUTTON?DOWN -> MOUSE_MSC_BUTTON?UP */
299 static int butmap[8] = {
300 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
301 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
302 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
304 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
313 lwkt_gettoken(&tty_token);
314 switch (info->operation) {
316 mouse_status.button = info->u.data.buttons;
318 case MOUSE_MOTION_EVENT:
323 case MOUSE_BUTTON_EVENT:
325 if (info->u.event.value > 0)
326 mouse_status.button |= info->u.event.id;
328 mouse_status.button &= ~info->u.event.id;
331 lwkt_reltoken(&tty_token);
335 mouse_status.dx += x;
336 mouse_status.dy += y;
337 mouse_status.dz += z;
338 mouse_status.flags |= ((x || y || z) ? MOUSE_POSCHANGED : 0)
339 | (mouse_status.obutton ^ mouse_status.button);
340 if (mouse_status.flags == 0) {
341 lwkt_reltoken(&tty_token);
345 if ((sysmouse_tty == NULL) || !(sysmouse_tty->t_state & TS_ISOPEN)) {
346 lwkt_reltoken(&tty_token);
347 return mouse_status.flags;
350 /* the first five bytes are compatible with MouseSystems' */
351 buf[0] = MOUSE_MSC_SYNC
352 | butmap[mouse_status.button & MOUSE_STDBUTTONS];
353 x = imax(imin(x, 255), -256);
356 y = -imax(imin(y, 255), -256);
359 for (i = 0; i < MOUSE_MSC_PACKETSIZE; ++i)
360 (*linesw[sysmouse_tty->t_line].l_rint)(buf[i], sysmouse_tty);
361 if (mouse_level >= 1) {
363 z = imax(imin(z, 127), -128);
364 buf[5] = (z >> 1) & 0x7f;
365 buf[6] = (z - (z >> 1)) & 0x7f;
367 buf[7] = (~mouse_status.button >> 3) & 0x7f;
368 for (i = MOUSE_MSC_PACKETSIZE; i < MOUSE_SYS_PACKETSIZE; ++i)
369 (*linesw[sysmouse_tty->t_line].l_rint)(buf[i],
373 lwkt_reltoken(&tty_token);
374 return mouse_status.flags;
377 #endif /* !SC_NO_SYSMOUSE */