xref: /dflybsd-src/sys/dev/misc/syscons/sysmouse.c (revision 70e491c09b1cb18f96facdbc91052080f2a455c3)
1 /*-
2  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
14  *
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.
25  *
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.12 2006/09/10 01:26:35 dillon Exp $
28  */
29 
30 #include "opt_syscons.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/conf.h>
35 #include <sys/proc.h>
36 #include <sys/priv.h>
37 #include <sys/tty.h>
38 #include <sys/kernel.h>
39 #include <sys/thread2.h>
40 
41 #include <machine/console.h>
42 #include <machine/mouse.h>
43 
44 #include "syscons.h"
45 
46 #ifndef SC_NO_SYSMOUSE
47 
48 #define	CDEV_MAJOR	12		/* major number, shared with syscons */
49 #define SC_MOUSE 	128		/* minor number */
50 
51 static d_open_t		smopen;
52 static d_close_t	smclose;
53 static d_ioctl_t	smioctl;
54 
55 static struct dev_ops sm_ops = {
56 	{ "sysmouse", CDEV_MAJOR, D_TTY | D_KQFILTER },
57 	.d_open =	smopen,
58 	.d_close =	smclose,
59 	.d_read =	ttyread,
60 	.d_ioctl =	smioctl,
61 	.d_poll =	ttypoll,
62 	.d_kqfilter =	ttykqfilter,
63 	.d_revoke =	ttyrevoke
64 };
65 
66 /* local variables */
67 static struct tty	*sysmouse_tty;
68 static int		mouse_level;	/* sysmouse protocol level */
69 static mousestatus_t	mouse_status;
70 
71 static void		smstart(struct tty *tp);
72 static int		smparam(struct tty *tp, struct termios *t);
73 
74 static int
75 smopen(struct dev_open_args *ap)
76 {
77 	cdev_t dev = ap->a_head.a_dev;
78 	struct tty *tp;
79 
80 	DPRINTF(5, ("smopen: dev:%d,%d, vty:%d\n",
81 		major(dev), minor(dev), SC_VTY(dev)));
82 
83 #if 0
84 	if (SC_VTY(dev) != SC_MOUSE)
85 		return ENXIO;
86 #endif
87 
88 	tp = dev->si_tty = ttymalloc(dev->si_tty);
89 	if (!(tp->t_state & TS_ISOPEN)) {
90 		sysmouse_tty = tp;
91 		tp->t_oproc = smstart;
92 		tp->t_param = smparam;
93 		tp->t_stop = nottystop;
94 		tp->t_dev = dev;
95 		ttychars(tp);
96 		tp->t_iflag = 0;
97 		tp->t_oflag = 0;
98 		tp->t_lflag = 0;
99 		tp->t_cflag = TTYDEF_CFLAG;
100 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
101 		smparam(tp, &tp->t_termios);
102 		(*linesw[tp->t_line].l_modem)(tp, 1);
103 	} else if (tp->t_state & TS_XCLUDE && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
104 		return EBUSY;
105 	}
106 
107 	return (*linesw[tp->t_line].l_open)(dev, tp);
108 }
109 
110 static int
111 smclose(struct dev_close_args *ap)
112 {
113 	cdev_t dev = ap->a_head.a_dev;
114 	struct tty *tp;
115 
116 	tp = dev->si_tty;
117 	crit_enter();
118 	mouse_level = 0;
119 	(*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
120 	ttyclose(tp);
121 	crit_exit();
122 
123 	return 0;
124 }
125 
126 static void
127 smstart(struct tty *tp)
128 {
129 	struct clist *rbp;
130 	u_char buf[PCBURST];
131 
132 	crit_enter();
133 	if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
134 		tp->t_state |= TS_BUSY;
135 		rbp = &tp->t_outq;
136 		while (rbp->c_cc)
137 			q_to_b(rbp, buf, PCBURST);
138 		tp->t_state &= ~TS_BUSY;
139 		ttwwakeup(tp);
140 	}
141 	crit_exit();
142 }
143 
144 static int
145 smparam(struct tty *tp, struct termios *t)
146 {
147 	tp->t_ispeed = t->c_ispeed;
148 	tp->t_ospeed = t->c_ospeed;
149 	tp->t_cflag = t->c_cflag;
150 	return 0;
151 }
152 
153 static int
154 smioctl(struct dev_ioctl_args *ap)
155 {
156 	cdev_t dev = ap->a_head.a_dev;
157 	struct tty *tp;
158 	mousehw_t *hw;
159 	mousemode_t *mode;
160 	int error;
161 
162 	tp = dev->si_tty;
163 
164 	switch (ap->a_cmd) {
165 	case MOUSE_GETHWINFO:	/* get device information */
166 		hw = (mousehw_t *)ap->a_data;
167 		hw->buttons = 10;		/* XXX unknown */
168 		hw->iftype = MOUSE_IF_SYSMOUSE;
169 		hw->type = MOUSE_MOUSE;
170 		hw->model = MOUSE_MODEL_GENERIC;
171 		hw->hwid = 0;
172 		return 0;
173 
174 	case MOUSE_GETMODE:	/* get protocol/mode */
175 		mode = (mousemode_t *)ap->a_data;
176 		mode->level = mouse_level;
177 		switch (mode->level) {
178 		case 0: /* emulate MouseSystems protocol */
179 			mode->protocol = MOUSE_PROTO_MSC;
180 			mode->rate = -1;		/* unknown */
181 			mode->resolution = -1;	/* unknown */
182 			mode->accelfactor = 0;	/* disabled */
183 			mode->packetsize = MOUSE_MSC_PACKETSIZE;
184 			mode->syncmask[0] = MOUSE_MSC_SYNCMASK;
185 			mode->syncmask[1] = MOUSE_MSC_SYNC;
186 			break;
187 
188 		case 1: /* sysmouse protocol */
189 			mode->protocol = MOUSE_PROTO_SYSMOUSE;
190 			mode->rate = -1;
191 			mode->resolution = -1;
192 			mode->accelfactor = 0;
193 			mode->packetsize = MOUSE_SYS_PACKETSIZE;
194 			mode->syncmask[0] = MOUSE_SYS_SYNCMASK;
195 			mode->syncmask[1] = MOUSE_SYS_SYNC;
196 			break;
197 		}
198 		return 0;
199 
200 	case MOUSE_SETMODE:	/* set protocol/mode */
201 		mode = (mousemode_t *)ap->a_data;
202 		if (mode->level == -1)
203 			; 	/* don't change the current setting */
204 		else if ((mode->level < 0) || (mode->level > 1))
205 			return EINVAL;
206 		else
207 			mouse_level = mode->level;
208 		return 0;
209 
210 	case MOUSE_GETLEVEL:	/* get operation level */
211 		*(int *)ap->a_data = mouse_level;
212 		return 0;
213 
214 	case MOUSE_SETLEVEL:	/* set operation level */
215 		if ((*(int *)ap->a_data  < 0) || (*(int *)ap->a_data > 1))
216 			return EINVAL;
217 		mouse_level = *(int *)ap->a_data;
218 		return 0;
219 
220 	case MOUSE_GETSTATUS:	/* get accumulated mouse events */
221 		crit_enter();
222 		*(mousestatus_t *)ap->a_data = mouse_status;
223 		mouse_status.flags = 0;
224 		mouse_status.obutton = mouse_status.button;
225 		mouse_status.dx = 0;
226 		mouse_status.dy = 0;
227 		mouse_status.dz = 0;
228 		crit_exit();
229 		return 0;
230 
231 #if notyet
232 	case MOUSE_GETVARS:	/* get internal mouse variables */
233 	case MOUSE_SETVARS:	/* set internal mouse variables */
234 		return ENODEV;
235 #endif
236 
237 	case MOUSE_READSTATE:	/* read status from the device */
238 	case MOUSE_READDATA:	/* read data from the device */
239 		return ENODEV;
240 	}
241 
242 	error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data, ap->a_fflag, ap->a_cred);
243 	if (error != ENOIOCTL)
244 		return error;
245 	error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
246 	if (error != ENOIOCTL)
247 		return error;
248 	return ENOTTY;
249 }
250 
251 static void
252 sm_attach_mouse(void *unused)
253 {
254 	cdev_t dev;
255 
256 	dev = make_dev(&sm_ops, SC_MOUSE, UID_ROOT, GID_WHEEL,
257 		       0600, "sysmouse");
258 	/* sysmouse doesn't have scr_stat */
259 }
260 
261 SYSINIT(sysmouse, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR,
262 	sm_attach_mouse, NULL)
263 
264 int
265 sysmouse_event(mouse_info_t *info)
266 {
267 	/* MOUSE_BUTTON?DOWN -> MOUSE_MSC_BUTTON?UP */
268 	static int butmap[8] = {
269 	    MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
270 	    MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
271 	    MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
272 	    MOUSE_MSC_BUTTON3UP,
273 	    MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
274 	    MOUSE_MSC_BUTTON2UP,
275 	    MOUSE_MSC_BUTTON1UP,
276 	    0,
277 	};
278 	u_char buf[8];
279 	int x, y, z;
280 	int i;
281 
282 	switch (info->operation) {
283 	case MOUSE_ACTION:
284         	mouse_status.button = info->u.data.buttons;
285 		/* FALL THROUGH */
286 	case MOUSE_MOTION_EVENT:
287 		x = info->u.data.x;
288 		y = info->u.data.y;
289 		z = info->u.data.z;
290 		break;
291 	case MOUSE_BUTTON_EVENT:
292 		x = y = z = 0;
293 		if (info->u.event.value > 0)
294 			mouse_status.button |= info->u.event.id;
295 		else
296 			mouse_status.button &= ~info->u.event.id;
297 		break;
298 	default:
299 		return 0;
300 	}
301 
302 	mouse_status.dx += x;
303 	mouse_status.dy += y;
304 	mouse_status.dz += z;
305 	mouse_status.flags |= ((x || y || z) ? MOUSE_POSCHANGED : 0)
306 			      | (mouse_status.obutton ^ mouse_status.button);
307 	if (mouse_status.flags == 0)
308 		return 0;
309 
310 	if ((sysmouse_tty == NULL) || !(sysmouse_tty->t_state & TS_ISOPEN))
311 		return mouse_status.flags;
312 
313 	/* the first five bytes are compatible with MouseSystems' */
314 	buf[0] = MOUSE_MSC_SYNC
315 		 | butmap[mouse_status.button & MOUSE_STDBUTTONS];
316 	x = imax(imin(x, 255), -256);
317 	buf[1] = x >> 1;
318 	buf[3] = x - buf[1];
319 	y = -imax(imin(y, 255), -256);
320 	buf[2] = y >> 1;
321 	buf[4] = y - buf[2];
322 	for (i = 0; i < MOUSE_MSC_PACKETSIZE; ++i)
323 		(*linesw[sysmouse_tty->t_line].l_rint)(buf[i], sysmouse_tty);
324 	if (mouse_level >= 1) {
325 		/* extended part */
326         	z = imax(imin(z, 127), -128);
327         	buf[5] = (z >> 1) & 0x7f;
328         	buf[6] = (z - (z >> 1)) & 0x7f;
329         	/* buttons 4-10 */
330         	buf[7] = (~mouse_status.button >> 3) & 0x7f;
331         	for (i = MOUSE_MSC_PACKETSIZE; i < MOUSE_SYS_PACKETSIZE; ++i)
332 			(*linesw[sysmouse_tty->t_line].l_rint)(buf[i],
333 							       sysmouse_tty);
334 	}
335 
336 	return mouse_status.flags;
337 }
338 
339 #endif /* !SC_NO_SYSMOUSE */
340