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