xref: /freebsd/sys/dev/syscons/sysmouse.c (revision 076ad2f836d5f49dc1375f1677335a48fe0d4b82)
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_evdev.h"
32 #include "opt_syscons.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/priv.h>
37 #include <sys/serial.h>
38 #include <sys/tty.h>
39 #include <sys/ttydefaults.h>
40 #include <sys/kernel.h>
41 #include <sys/cons.h>
42 #include <sys/consio.h>
43 #include <sys/mouse.h>
44 
45 #include <dev/syscons/syscons.h>
46 
47 #ifdef EVDEV_SUPPORT
48 #include <dev/evdev/input.h>
49 #include <dev/evdev/evdev.h>
50 #endif
51 
52 #ifndef SC_NO_SYSMOUSE
53 
54 /* local variables */
55 static struct tty	*sysmouse_tty;
56 static int		mouse_level;	/* sysmouse protocol level */
57 static mousestatus_t	mouse_status;
58 
59 #ifdef EVDEV_SUPPORT
60 static struct evdev_dev	*sysmouse_evdev;
61 
62 static void
63 smdev_evdev_init(void)
64 {
65 	int i;
66 
67 	sysmouse_evdev = evdev_alloc();
68 	evdev_set_name(sysmouse_evdev, "System mouse");
69 	evdev_set_phys(sysmouse_evdev, "sysmouse");
70 	evdev_set_id(sysmouse_evdev, BUS_VIRTUAL, 0, 0, 0);
71 	evdev_support_prop(sysmouse_evdev, INPUT_PROP_POINTER);
72 	evdev_support_event(sysmouse_evdev, EV_SYN);
73 	evdev_support_event(sysmouse_evdev, EV_REL);
74 	evdev_support_event(sysmouse_evdev, EV_KEY);
75 	evdev_support_rel(sysmouse_evdev, REL_X);
76 	evdev_support_rel(sysmouse_evdev, REL_Y);
77 	evdev_support_rel(sysmouse_evdev, REL_WHEEL);
78 	evdev_support_rel(sysmouse_evdev, REL_HWHEEL);
79 	for (i = 0; i < 8; i++)
80 		evdev_support_key(sysmouse_evdev, BTN_MOUSE + i);
81 	if (evdev_register(sysmouse_evdev)) {
82 		evdev_free(sysmouse_evdev);
83 		sysmouse_evdev = NULL;
84 	}
85 }
86 
87 static void
88 smdev_evdev_write(int x, int y, int z, int buttons)
89 {
90 
91 	if (sysmouse_evdev == NULL || !(evdev_rcpt_mask & EVDEV_RCPT_SYSMOUSE))
92 		return;
93 
94 	evdev_push_event(sysmouse_evdev, EV_REL, REL_X, x);
95 	evdev_push_event(sysmouse_evdev, EV_REL, REL_Y, y);
96 	switch (evdev_sysmouse_t_axis) {
97 	case EVDEV_SYSMOUSE_T_AXIS_PSM:
98 		switch (z) {
99 		case 1:
100 		case -1:
101 			evdev_push_rel(sysmouse_evdev, REL_WHEEL, -z);
102 			break;
103 		case 2:
104 		case -2:
105 			evdev_push_rel(sysmouse_evdev, REL_HWHEEL, z / 2);
106 			break;
107 		}
108 		break;
109 	case EVDEV_SYSMOUSE_T_AXIS_UMS:
110 		/* XXX: Edge triggering should be used here */
111 		if (buttons & (1 << 5))
112 			evdev_push_rel(sysmouse_evdev, REL_HWHEEL, 1);
113 		else if (buttons & (1 << 6))
114 			evdev_push_rel(sysmouse_evdev, REL_HWHEEL, -1);
115 		/* PASSTHROUGH */
116 	case EVDEV_SYSMOUSE_T_AXIS_NONE:
117 	default:
118 		evdev_push_rel(sysmouse_evdev, REL_WHEEL, -z);
119 	}
120 	evdev_push_mouse_btn(sysmouse_evdev, buttons);
121 	evdev_sync(sysmouse_evdev);
122 }
123 #endif
124 
125 static void
126 smdev_close(struct tty *tp)
127 {
128 	mouse_level = 0;
129 }
130 
131 static int
132 smdev_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
133 {
134 	mousehw_t *hw;
135 	mousemode_t *mode;
136 
137 	switch (cmd) {
138 
139 	case MOUSE_GETHWINFO:	/* get device information */
140 		hw = (mousehw_t *)data;
141 		hw->buttons = 10;		/* XXX unknown */
142 		hw->iftype = MOUSE_IF_SYSMOUSE;
143 		hw->type = MOUSE_MOUSE;
144 		hw->model = MOUSE_MODEL_GENERIC;
145 		hw->hwid = 0;
146 		return 0;
147 
148 	case MOUSE_GETMODE:	/* get protocol/mode */
149 		mode = (mousemode_t *)data;
150 		mode->level = mouse_level;
151 		switch (mode->level) {
152 		case 0: /* emulate MouseSystems protocol */
153 			mode->protocol = MOUSE_PROTO_MSC;
154 			mode->rate = -1;		/* unknown */
155 			mode->resolution = -1;	/* unknown */
156 			mode->accelfactor = 0;	/* disabled */
157 			mode->packetsize = MOUSE_MSC_PACKETSIZE;
158 			mode->syncmask[0] = MOUSE_MSC_SYNCMASK;
159 			mode->syncmask[1] = MOUSE_MSC_SYNC;
160 			break;
161 
162 		case 1: /* sysmouse protocol */
163 			mode->protocol = MOUSE_PROTO_SYSMOUSE;
164 			mode->rate = -1;
165 			mode->resolution = -1;
166 			mode->accelfactor = 0;
167 			mode->packetsize = MOUSE_SYS_PACKETSIZE;
168 			mode->syncmask[0] = MOUSE_SYS_SYNCMASK;
169 			mode->syncmask[1] = MOUSE_SYS_SYNC;
170 			break;
171 		}
172 		return 0;
173 
174 	case MOUSE_SETMODE:	/* set protocol/mode */
175 		mode = (mousemode_t *)data;
176 		if (mode->level == -1)
177 			; 	/* don't change the current setting */
178 		else if ((mode->level < 0) || (mode->level > 1))
179 			return EINVAL;
180 		else
181 			mouse_level = mode->level;
182 		return 0;
183 
184 	case MOUSE_GETLEVEL:	/* get operation level */
185 		*(int *)data = mouse_level;
186 		return 0;
187 
188 	case MOUSE_SETLEVEL:	/* set operation level */
189 		if ((*(int *)data  < 0) || (*(int *)data > 1))
190 			return EINVAL;
191 		mouse_level = *(int *)data;
192 		return 0;
193 
194 	case MOUSE_GETSTATUS:	/* get accumulated mouse events */
195 		*(mousestatus_t *)data = mouse_status;
196 		mouse_status.flags = 0;
197 		mouse_status.obutton = mouse_status.button;
198 		mouse_status.dx = 0;
199 		mouse_status.dy = 0;
200 		mouse_status.dz = 0;
201 		return 0;
202 
203 #ifdef notyet
204 	case MOUSE_GETVARS:	/* get internal mouse variables */
205 	case MOUSE_SETVARS:	/* set internal mouse variables */
206 		return ENODEV;
207 #endif
208 
209 	case MOUSE_READSTATE:	/* read status from the device */
210 	case MOUSE_READDATA:	/* read data from the device */
211 		return ENODEV;
212 	}
213 
214 	return (ENOIOCTL);
215 }
216 
217 static int
218 smdev_param(struct tty *tp, struct termios *t)
219 {
220 
221 	/*
222 	 * Set the output baud rate to zero. The mouse device supports
223 	 * no output, so we don't want to waste buffers.
224 	 */
225 	t->c_ispeed = TTYDEF_SPEED;
226 	t->c_ospeed = B0;
227 
228 	return (0);
229 }
230 
231 static struct ttydevsw smdev_ttydevsw = {
232 	.tsw_flags	= TF_NOPREFIX,
233 	.tsw_close	= smdev_close,
234 	.tsw_ioctl	= smdev_ioctl,
235 	.tsw_param	= smdev_param,
236 };
237 
238 static void
239 sm_attach_mouse(void *unused)
240 {
241 	if (!vty_enabled(VTY_SC))
242 		return;
243 	sysmouse_tty = tty_alloc(&smdev_ttydevsw, NULL);
244 	tty_makedev(sysmouse_tty, NULL, "sysmouse");
245 #ifdef EVDEV_SUPPORT
246 	smdev_evdev_init();
247 #endif
248 }
249 
250 SYSINIT(sysmouse, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, sm_attach_mouse, NULL);
251 
252 int
253 sysmouse_event(mouse_info_t *info)
254 {
255 	/* MOUSE_BUTTON?DOWN -> MOUSE_MSC_BUTTON?UP */
256 	static int butmap[8] = {
257 	    MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
258 	    MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
259 	    MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
260 	    MOUSE_MSC_BUTTON3UP,
261 	    MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
262 	    MOUSE_MSC_BUTTON2UP,
263 	    MOUSE_MSC_BUTTON1UP,
264 	    0,
265 	};
266 	u_char buf[8];
267 	int x, y, z;
268 	int i, flags = 0;
269 
270 	tty_lock(sysmouse_tty);
271 
272 	switch (info->operation) {
273 	case MOUSE_ACTION:
274         	mouse_status.button = info->u.data.buttons;
275 		/* FALL THROUGH */
276 	case MOUSE_MOTION_EVENT:
277 		x = info->u.data.x;
278 		y = info->u.data.y;
279 		z = info->u.data.z;
280 		break;
281 	case MOUSE_BUTTON_EVENT:
282 		x = y = z = 0;
283 		if (info->u.event.value > 0)
284 			mouse_status.button |= info->u.event.id;
285 		else
286 			mouse_status.button &= ~info->u.event.id;
287 		break;
288 	default:
289 		goto done;
290 	}
291 
292 	mouse_status.dx += x;
293 	mouse_status.dy += y;
294 	mouse_status.dz += z;
295 	mouse_status.flags |= ((x || y || z) ? MOUSE_POSCHANGED : 0)
296 			      | (mouse_status.obutton ^ mouse_status.button);
297 	flags = mouse_status.flags;
298 	if (flags == 0)
299 		goto done;
300 
301 #ifdef EVDEV_SUPPORT
302 	smdev_evdev_write(x, y, z, mouse_status.button);
303 #endif
304 
305 	if (!tty_opened(sysmouse_tty))
306 		goto done;
307 
308 	/* the first five bytes are compatible with MouseSystems' */
309 	buf[0] = MOUSE_MSC_SYNC
310 		 | butmap[mouse_status.button & MOUSE_STDBUTTONS];
311 	x = imax(imin(x, 255), -256);
312 	buf[1] = x >> 1;
313 	buf[3] = x - buf[1];
314 	y = -imax(imin(y, 255), -256);
315 	buf[2] = y >> 1;
316 	buf[4] = y - buf[2];
317 	for (i = 0; i < MOUSE_MSC_PACKETSIZE; ++i)
318 		ttydisc_rint(sysmouse_tty, buf[i], 0);
319 	if (mouse_level >= 1) {
320 		/* extended part */
321         	z = imax(imin(z, 127), -128);
322         	buf[5] = (z >> 1) & 0x7f;
323         	buf[6] = (z - (z >> 1)) & 0x7f;
324         	/* buttons 4-10 */
325         	buf[7] = (~mouse_status.button >> 3) & 0x7f;
326         	for (i = MOUSE_MSC_PACKETSIZE; i < MOUSE_SYS_PACKETSIZE; ++i)
327 			ttydisc_rint(sysmouse_tty, buf[i], 0);
328 	}
329 	ttydisc_rint_done(sysmouse_tty);
330 
331 done:	tty_unlock(sysmouse_tty);
332 	return (flags);
333 }
334 
335 #endif /* !SC_NO_SYSMOUSE */
336