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