xref: /freebsd/sys/dev/syscons/sysmouse.c (revision 4f52dfbb)
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 #ifdef notyet
206 	case MOUSE_GETVARS:	/* get internal mouse variables */
207 	case MOUSE_SETVARS:	/* set internal mouse variables */
208 		return ENODEV;
209 #endif
210 
211 	case MOUSE_READSTATE:	/* read status from the device */
212 	case MOUSE_READDATA:	/* read data from the device */
213 		return ENODEV;
214 	}
215 
216 	return (ENOIOCTL);
217 }
218 
219 static int
220 smdev_param(struct tty *tp, struct termios *t)
221 {
222 
223 	/*
224 	 * Set the output baud rate to zero. The mouse device supports
225 	 * no output, so we don't want to waste buffers.
226 	 */
227 	t->c_ispeed = TTYDEF_SPEED;
228 	t->c_ospeed = B0;
229 
230 	return (0);
231 }
232 
233 static struct ttydevsw smdev_ttydevsw = {
234 	.tsw_flags	= TF_NOPREFIX,
235 	.tsw_close	= smdev_close,
236 	.tsw_ioctl	= smdev_ioctl,
237 	.tsw_param	= smdev_param,
238 };
239 
240 static void
241 sm_attach_mouse(void *unused)
242 {
243 	if (!vty_enabled(VTY_SC))
244 		return;
245 	sysmouse_tty = tty_alloc(&smdev_ttydevsw, NULL);
246 	tty_makedev(sysmouse_tty, NULL, "sysmouse");
247 #ifdef EVDEV_SUPPORT
248 	smdev_evdev_init();
249 #endif
250 }
251 
252 SYSINIT(sysmouse, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, sm_attach_mouse, NULL);
253 
254 int
255 sysmouse_event(mouse_info_t *info)
256 {
257 	/* MOUSE_BUTTON?DOWN -> MOUSE_MSC_BUTTON?UP */
258 	static int butmap[8] = {
259 	    MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
260 	    MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
261 	    MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
262 	    MOUSE_MSC_BUTTON3UP,
263 	    MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
264 	    MOUSE_MSC_BUTTON2UP,
265 	    MOUSE_MSC_BUTTON1UP,
266 	    0,
267 	};
268 	u_char buf[8];
269 	int x, y, z;
270 	int i, flags = 0;
271 
272 	tty_lock(sysmouse_tty);
273 
274 	switch (info->operation) {
275 	case MOUSE_ACTION:
276         	mouse_status.button = info->u.data.buttons;
277 		/* FALL THROUGH */
278 	case MOUSE_MOTION_EVENT:
279 		x = info->u.data.x;
280 		y = info->u.data.y;
281 		z = info->u.data.z;
282 		break;
283 	case MOUSE_BUTTON_EVENT:
284 		x = y = z = 0;
285 		if (info->u.event.value > 0)
286 			mouse_status.button |= info->u.event.id;
287 		else
288 			mouse_status.button &= ~info->u.event.id;
289 		break;
290 	default:
291 		goto done;
292 	}
293 
294 	mouse_status.dx += x;
295 	mouse_status.dy += y;
296 	mouse_status.dz += z;
297 	mouse_status.flags |= ((x || y || z) ? MOUSE_POSCHANGED : 0)
298 			      | (mouse_status.obutton ^ mouse_status.button);
299 	flags = mouse_status.flags;
300 	if (flags == 0)
301 		goto done;
302 
303 #ifdef EVDEV_SUPPORT
304 	smdev_evdev_write(x, y, z, mouse_status.button);
305 #endif
306 
307 	if (!tty_opened(sysmouse_tty))
308 		goto done;
309 
310 	/* the first five bytes are compatible with MouseSystems' */
311 	buf[0] = MOUSE_MSC_SYNC
312 		 | butmap[mouse_status.button & MOUSE_STDBUTTONS];
313 	x = imax(imin(x, 255), -256);
314 	buf[1] = x >> 1;
315 	buf[3] = x - buf[1];
316 	y = -imax(imin(y, 255), -256);
317 	buf[2] = y >> 1;
318 	buf[4] = y - buf[2];
319 	for (i = 0; i < MOUSE_MSC_PACKETSIZE; ++i)
320 		ttydisc_rint(sysmouse_tty, buf[i], 0);
321 	if (mouse_level >= 1) {
322 		/* extended part */
323         	z = imax(imin(z, 127), -128);
324         	buf[5] = (z >> 1) & 0x7f;
325         	buf[6] = (z - (z >> 1)) & 0x7f;
326         	/* buttons 4-10 */
327         	buf[7] = (~mouse_status.button >> 3) & 0x7f;
328         	for (i = MOUSE_MSC_PACKETSIZE; i < MOUSE_SYS_PACKETSIZE; ++i)
329 			ttydisc_rint(sysmouse_tty, buf[i], 0);
330 	}
331 	ttydisc_rint_done(sysmouse_tty);
332 
333 done:	tty_unlock(sysmouse_tty);
334 	return (flags);
335 }
336 
337 #endif /* !SC_NO_SYSMOUSE */
338