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