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 $
29 * $DragonFly: src/sys/dev/misc/syscons/sysmouse.c,v 1.12 2006/09/10 01:26:35 dillon Exp $
32 /* MPSAFE NOTE: This file uses the tty_token mostly for the linesw access and
33 * for the internal mouse structures. The latter ones could be protected
36 #include "opt_syscons.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/thread2.h>
47 #include <machine/console.h>
48 #include <machine/mouse.h>
52 #ifndef SC_NO_SYSMOUSE
54 #define CDEV_MAJOR 12 /* major number, shared with syscons */
55 #define SC_MOUSE 128 /* minor number */
57 static d_open_t smopen;
58 static d_close_t smclose;
59 static d_ioctl_t smioctl;
61 static struct dev_ops sm_ops = {
62 { "sysmouse", CDEV_MAJOR, D_TTY },
67 .d_kqfilter = ttykqfilter,
72 static struct tty *sysmouse_tty;
73 static int mouse_level; /* sysmouse protocol level */
74 static mousestatus_t mouse_status;
76 static void smstart(struct tty *tp);
77 static int smparam(struct tty *tp, struct termios *t);
80 smopen(struct dev_open_args *ap)
82 cdev_t dev = ap->a_head.a_dev;
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 lwkt_gettoken(&tty_token);
95 tp = dev->si_tty = ttymalloc(dev->si_tty);
96 if (!(tp->t_state & TS_ISOPEN)) {
98 tp->t_oproc = smstart;
99 tp->t_param = smparam;
100 tp->t_stop = nottystop;
106 tp->t_cflag = TTYDEF_CFLAG;
107 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
108 smparam(tp, &tp->t_termios);
109 (*linesw[tp->t_line].l_modem)(tp, 1);
110 } else if (tp->t_state & TS_XCLUDE && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
111 lwkt_reltoken(&tty_token);
115 ret = (*linesw[tp->t_line].l_open)(dev, tp);
116 lwkt_reltoken(&tty_token);
121 smclose(struct dev_close_args *ap)
123 cdev_t dev = ap->a_head.a_dev;
128 lwkt_gettoken(&tty_token);
130 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
132 lwkt_reltoken(&tty_token);
139 smstart(struct tty *tp)
145 lwkt_gettoken(&tty_token);
146 if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
147 tp->t_state |= TS_BUSY;
150 q_to_b(rbp, buf, PCBURST);
151 tp->t_state &= ~TS_BUSY;
154 lwkt_reltoken(&tty_token);
159 smparam(struct tty *tp, struct termios *t)
161 lwkt_gettoken(&tty_token);
162 tp->t_ispeed = t->c_ispeed;
163 tp->t_ospeed = t->c_ospeed;
164 tp->t_cflag = t->c_cflag;
165 lwkt_reltoken(&tty_token);
170 smioctl(struct dev_ioctl_args *ap)
172 cdev_t dev = ap->a_head.a_dev;
178 lwkt_gettoken(&tty_token);
182 case MOUSE_GETHWINFO: /* get device information */
183 hw = (mousehw_t *)ap->a_data;
184 hw->buttons = 10; /* XXX unknown */
185 hw->iftype = MOUSE_IF_SYSMOUSE;
186 hw->type = MOUSE_MOUSE;
187 hw->model = MOUSE_MODEL_GENERIC;
189 lwkt_reltoken(&tty_token);
192 case MOUSE_GETMODE: /* get protocol/mode */
193 mode = (mousemode_t *)ap->a_data;
194 mode->level = mouse_level;
195 switch (mode->level) {
196 case 0: /* emulate MouseSystems protocol */
197 mode->protocol = MOUSE_PROTO_MSC;
198 mode->rate = -1; /* unknown */
199 mode->resolution = -1; /* unknown */
200 mode->accelfactor = 0; /* disabled */
201 mode->packetsize = MOUSE_MSC_PACKETSIZE;
202 mode->syncmask[0] = MOUSE_MSC_SYNCMASK;
203 mode->syncmask[1] = MOUSE_MSC_SYNC;
206 case 1: /* sysmouse protocol */
207 mode->protocol = MOUSE_PROTO_SYSMOUSE;
209 mode->resolution = -1;
210 mode->accelfactor = 0;
211 mode->packetsize = MOUSE_SYS_PACKETSIZE;
212 mode->syncmask[0] = MOUSE_SYS_SYNCMASK;
213 mode->syncmask[1] = MOUSE_SYS_SYNC;
216 lwkt_reltoken(&tty_token);
219 case MOUSE_SETMODE: /* set protocol/mode */
220 mode = (mousemode_t *)ap->a_data;
221 if (mode->level == -1)
222 ; /* don't change the current setting */
223 else if ((mode->level < 0) || (mode->level > 1)) {
224 lwkt_reltoken(&tty_token);
227 mouse_level = mode->level;
229 lwkt_reltoken(&tty_token);
232 case MOUSE_GETLEVEL: /* get operation level */
233 *(int *)ap->a_data = mouse_level;
234 lwkt_reltoken(&tty_token);
237 case MOUSE_SETLEVEL: /* set operation level */
238 if ((*(int *)ap->a_data < 0) || (*(int *)ap->a_data > 1)) {
239 lwkt_reltoken(&tty_token);
242 mouse_level = *(int *)ap->a_data;
243 lwkt_reltoken(&tty_token);
246 case MOUSE_GETSTATUS: /* get accumulated mouse events */
248 *(mousestatus_t *)ap->a_data = mouse_status;
249 mouse_status.flags = 0;
250 mouse_status.obutton = mouse_status.button;
255 lwkt_reltoken(&tty_token);
259 case MOUSE_GETVARS: /* get internal mouse variables */
260 case MOUSE_SETVARS: /* set internal mouse variables */
261 lwkt_reltoken(&tty_token);
265 case MOUSE_READSTATE: /* read status from the device */
266 case MOUSE_READDATA: /* read data from the device */
267 lwkt_reltoken(&tty_token);
271 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data, ap->a_fflag, ap->a_cred);
272 if (error != ENOIOCTL) {
273 lwkt_reltoken(&tty_token);
276 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
277 if (error != ENOIOCTL) {
278 lwkt_reltoken(&tty_token);
281 lwkt_reltoken(&tty_token);
286 sm_attach_mouse(void *unused)
290 dev = make_dev(&sm_ops, SC_MOUSE, UID_ROOT, GID_WHEEL,
292 /* sysmouse doesn't have scr_stat */
295 SYSINIT(sysmouse, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR,
296 sm_attach_mouse, NULL)
299 sysmouse_event(mouse_info_t *info)
301 /* MOUSE_BUTTON?DOWN -> MOUSE_MSC_BUTTON?UP */
302 static int butmap[8] = {
303 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
304 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
305 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
307 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
316 lwkt_gettoken(&tty_token);
317 switch (info->operation) {
319 mouse_status.button = info->u.data.buttons;
321 case MOUSE_MOTION_EVENT:
326 case MOUSE_BUTTON_EVENT:
328 if (info->u.event.value > 0)
329 mouse_status.button |= info->u.event.id;
331 mouse_status.button &= ~info->u.event.id;
334 lwkt_reltoken(&tty_token);
338 mouse_status.dx += x;
339 mouse_status.dy += y;
340 mouse_status.dz += z;
341 mouse_status.flags |= ((x || y || z) ? MOUSE_POSCHANGED : 0)
342 | (mouse_status.obutton ^ mouse_status.button);
343 if (mouse_status.flags == 0) {
344 lwkt_reltoken(&tty_token);
348 if ((sysmouse_tty == NULL) || !(sysmouse_tty->t_state & TS_ISOPEN)) {
349 lwkt_reltoken(&tty_token);
350 return mouse_status.flags;
353 /* the first five bytes are compatible with MouseSystems' */
354 buf[0] = MOUSE_MSC_SYNC
355 | butmap[mouse_status.button & MOUSE_STDBUTTONS];
356 x = imax(imin(x, 255), -256);
359 y = -imax(imin(y, 255), -256);
362 for (i = 0; i < MOUSE_MSC_PACKETSIZE; ++i)
363 (*linesw[sysmouse_tty->t_line].l_rint)(buf[i], sysmouse_tty);
364 if (mouse_level >= 1) {
366 z = imax(imin(z, 127), -128);
367 buf[5] = (z >> 1) & 0x7f;
368 buf[6] = (z - (z >> 1)) & 0x7f;
370 buf[7] = (~mouse_status.button >> 3) & 0x7f;
371 for (i = MOUSE_MSC_PACKETSIZE; i < MOUSE_SYS_PACKETSIZE; ++i)
372 (*linesw[sysmouse_tty->t_line].l_rint)(buf[i],
376 lwkt_reltoken(&tty_token);
377 return mouse_status.flags;
380 #endif /* !SC_NO_SYSMOUSE */