xref: /freebsd/sys/dev/vt/vt_sysmouse.c (revision 7cc42f6d)
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  * Copyright (c) 2009 The FreeBSD Foundation
8  * All rights reserved.
9  *
10  * This software was developed by Ed Schouten under sponsorship from the
11  * FreeBSD Foundation.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include "opt_evdev.h"
39 
40 #include <sys/param.h>
41 #include <sys/condvar.h>
42 #include <sys/consio.h>
43 #include <sys/fcntl.h>
44 #include <sys/filio.h>
45 #include <sys/lock.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/mutex.h>
49 #include <sys/poll.h>
50 #include <sys/random.h>
51 #include <sys/selinfo.h>
52 #include <sys/sigio.h>
53 #include <sys/signalvar.h>
54 #include <sys/systm.h>
55 #include <sys/uio.h>
56 
57 #include <dev/vt/vt.h>
58 
59 #ifdef EVDEV_SUPPORT
60 #include <dev/evdev/input.h>
61 #include <dev/evdev/evdev.h>
62 #endif
63 
64 static d_open_t		sysmouse_open;
65 static d_close_t	sysmouse_close;
66 static d_read_t		sysmouse_read;
67 static d_ioctl_t	sysmouse_ioctl;
68 static d_poll_t		sysmouse_poll;
69 
70 static struct cdevsw sysmouse_cdevsw = {
71 	.d_version	= D_VERSION,
72 	.d_open		= sysmouse_open,
73 	.d_close	= sysmouse_close,
74 	.d_read		= sysmouse_read,
75 	.d_ioctl	= sysmouse_ioctl,
76 	.d_poll		= sysmouse_poll,
77 	.d_name		= "sysmouse",
78 };
79 
80 static struct mtx	 sysmouse_lock;
81 static struct cv	 sysmouse_sleep;
82 static struct selinfo	 sysmouse_bufpoll;
83 
84 static int		 sysmouse_level;
85 static mousestatus_t	 sysmouse_status;
86 static int		 sysmouse_flags;
87 #define	SM_ASYNC	0x1
88 static struct sigio	*sysmouse_sigio;
89 
90 #define	SYSMOUSE_MAXFRAMES	250	/* 2 KB */
91 static MALLOC_DEFINE(M_SYSMOUSE, "sysmouse", "sysmouse device");
92 static unsigned char	*sysmouse_buffer;
93 static unsigned int	 sysmouse_start, sysmouse_length;
94 
95 #ifdef EVDEV_SUPPORT
96 static struct evdev_dev	*sysmouse_evdev;
97 
98 static void
99 sysmouse_evdev_init(void)
100 {
101 	int i;
102 
103 	sysmouse_evdev = evdev_alloc();
104 	evdev_set_name(sysmouse_evdev, "System mouse");
105 	evdev_set_phys(sysmouse_evdev, "sysmouse");
106 	evdev_set_id(sysmouse_evdev, BUS_VIRTUAL, 0, 0, 0);
107 	evdev_support_prop(sysmouse_evdev, INPUT_PROP_POINTER);
108 	evdev_support_event(sysmouse_evdev, EV_SYN);
109 	evdev_support_event(sysmouse_evdev, EV_REL);
110 	evdev_support_event(sysmouse_evdev, EV_KEY);
111 	evdev_support_rel(sysmouse_evdev, REL_X);
112 	evdev_support_rel(sysmouse_evdev, REL_Y);
113 	evdev_support_rel(sysmouse_evdev, REL_WHEEL);
114 	evdev_support_rel(sysmouse_evdev, REL_HWHEEL);
115 	for (i = 0; i < 8; i++)
116 		evdev_support_key(sysmouse_evdev, BTN_MOUSE + i);
117 	if (evdev_register(sysmouse_evdev)) {
118 		evdev_free(sysmouse_evdev);
119 		sysmouse_evdev = NULL;
120 	}
121 }
122 
123 static void
124 sysmouse_evdev_store(int x, int y, int z, int buttons)
125 {
126 
127 	if (sysmouse_evdev == NULL || !(evdev_rcpt_mask & EVDEV_RCPT_SYSMOUSE))
128 		return;
129 
130 	evdev_push_event(sysmouse_evdev, EV_REL, REL_X, x);
131 	evdev_push_event(sysmouse_evdev, EV_REL, REL_Y, y);
132 	switch (evdev_sysmouse_t_axis) {
133 	case EVDEV_SYSMOUSE_T_AXIS_PSM:
134 		switch (z) {
135 		case 1:
136 		case -1:
137 			evdev_push_rel(sysmouse_evdev, REL_WHEEL, -z);
138 			break;
139 		case 2:
140 		case -2:
141 			evdev_push_rel(sysmouse_evdev, REL_HWHEEL, z / 2);
142 			break;
143 		}
144 		break;
145 	case EVDEV_SYSMOUSE_T_AXIS_UMS:
146 		if (buttons & (1 << 6))
147 			evdev_push_rel(sysmouse_evdev, REL_HWHEEL, 1);
148 		else if (buttons & (1 << 5))
149 			evdev_push_rel(sysmouse_evdev, REL_HWHEEL, -1);
150 		buttons &= ~((1 << 5)|(1 << 6));
151 		/* PASSTHROUGH */
152 	case EVDEV_SYSMOUSE_T_AXIS_NONE:
153 	default:
154 		evdev_push_rel(sysmouse_evdev, REL_WHEEL, -z);
155 	}
156 	evdev_push_mouse_btn(sysmouse_evdev, buttons);
157 	evdev_sync(sysmouse_evdev);
158 }
159 #endif
160 
161 static int
162 sysmouse_buf_read(struct uio *uio, unsigned int length)
163 {
164 	unsigned char buf[MOUSE_SYS_PACKETSIZE];
165 	int error;
166 
167 	if (sysmouse_buffer == NULL)
168 		return (ENXIO);
169 	else if (sysmouse_length == 0)
170 		return (EWOULDBLOCK);
171 
172 	memcpy(buf, sysmouse_buffer +
173 	    sysmouse_start * MOUSE_SYS_PACKETSIZE, MOUSE_SYS_PACKETSIZE);
174 	sysmouse_start = (sysmouse_start + 1) % SYSMOUSE_MAXFRAMES;
175 	sysmouse_length--;
176 
177 	mtx_unlock(&sysmouse_lock);
178 	error = uiomove(buf, length, uio);
179 	mtx_lock(&sysmouse_lock);
180 
181 	return (error);
182 }
183 
184 static void
185 sysmouse_buf_store(const unsigned char buf[MOUSE_SYS_PACKETSIZE])
186 {
187 	unsigned int idx;
188 
189 	if (sysmouse_buffer == NULL || sysmouse_length == SYSMOUSE_MAXFRAMES)
190 		return;
191 
192 	idx = (sysmouse_start + sysmouse_length) % SYSMOUSE_MAXFRAMES;
193 	memcpy(sysmouse_buffer + idx * MOUSE_SYS_PACKETSIZE, buf,
194 	    MOUSE_SYS_PACKETSIZE);
195 	sysmouse_length++;
196 	cv_broadcast(&sysmouse_sleep);
197 	selwakeup(&sysmouse_bufpoll);
198 	if (sysmouse_flags & SM_ASYNC && sysmouse_sigio != NULL)
199 		pgsigio(&sysmouse_sigio, SIGIO, 0);
200 }
201 
202 void
203 sysmouse_process_event(mouse_info_t *mi)
204 {
205 	/* MOUSE_BUTTON?DOWN -> MOUSE_MSC_BUTTON?UP */
206 	static const int buttonmap[8] = {
207 	    MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
208 	    MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
209 	    MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
210 	    MOUSE_MSC_BUTTON3UP,
211 	    MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
212 	    MOUSE_MSC_BUTTON2UP,
213 	    MOUSE_MSC_BUTTON1UP,
214 	    0,
215 	};
216 	unsigned char buf[MOUSE_SYS_PACKETSIZE];
217 	int x, y, iy, z;
218 
219 	random_harvest_queue(mi, sizeof *mi, RANDOM_MOUSE);
220 
221 	mtx_lock(&sysmouse_lock);
222 	switch (mi->operation) {
223 	case MOUSE_ACTION:
224 		sysmouse_status.button = mi->u.data.buttons;
225 		/* FALLTHROUGH */
226 	case MOUSE_MOTION_EVENT:
227 		x = mi->u.data.x;
228 		y = mi->u.data.y;
229 		z = mi->u.data.z;
230 		break;
231 	case MOUSE_BUTTON_EVENT:
232 		x = y = z = 0;
233 		if (mi->u.event.value > 0)
234 			sysmouse_status.button |= mi->u.event.id;
235 		else
236 			sysmouse_status.button &= ~mi->u.event.id;
237 		break;
238 	default:
239 		goto done;
240 	}
241 
242 	sysmouse_status.dx += x;
243 	sysmouse_status.dy += y;
244 	sysmouse_status.dz += z;
245 	sysmouse_status.flags |= ((x || y || z) ? MOUSE_POSCHANGED : 0) |
246 	    (sysmouse_status.obutton ^ sysmouse_status.button);
247 	if (sysmouse_status.flags == 0)
248 		goto done;
249 
250 #ifdef EVDEV_SUPPORT
251 	sysmouse_evdev_store(x, y, z, sysmouse_status.button);
252 #endif
253 
254 	/* The first five bytes are compatible with MouseSystems. */
255 	buf[0] = MOUSE_MSC_SYNC |
256 	    buttonmap[sysmouse_status.button & MOUSE_STDBUTTONS];
257 	x = imax(imin(x, 255), -256);
258 	buf[1] = x >> 1;
259 	buf[3] = x - buf[1];
260 	iy = -imax(imin(y, 255), -256);
261 	buf[2] = iy >> 1;
262 	buf[4] = iy - buf[2];
263 	/* Extended part. */
264         z = imax(imin(z, 127), -128);
265         buf[5] = (z >> 1) & 0x7f;
266         buf[6] = (z - (z >> 1)) & 0x7f;
267         /* Buttons 4-10. */
268         buf[7] = (~sysmouse_status.button >> 3) & 0x7f;
269 
270 	sysmouse_buf_store(buf);
271 
272 #ifndef SC_NO_CUTPASTE
273 	mtx_unlock(&sysmouse_lock);
274 	vt_mouse_event(mi->operation, x, y, mi->u.event.id, mi->u.event.value,
275 	    sysmouse_level);
276 	return;
277 #endif
278 
279 done:	mtx_unlock(&sysmouse_lock);
280 }
281 
282 static int
283 sysmouse_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
284 {
285 	void *buf;
286 
287 	buf = malloc(MOUSE_SYS_PACKETSIZE * SYSMOUSE_MAXFRAMES,
288 	    M_SYSMOUSE, M_WAITOK);
289 	mtx_lock(&sysmouse_lock);
290 	if (sysmouse_buffer == NULL) {
291 		sysmouse_buffer = buf;
292 		sysmouse_start = sysmouse_length = 0;
293 		sysmouse_level = 0;
294 	} else {
295 		free(buf, M_SYSMOUSE);
296 	}
297 	mtx_unlock(&sysmouse_lock);
298 
299 	return (0);
300 }
301 
302 static int
303 sysmouse_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
304 {
305 
306 	mtx_lock(&sysmouse_lock);
307 	free(sysmouse_buffer, M_SYSMOUSE);
308 	sysmouse_buffer = NULL;
309 	sysmouse_level = 0;
310 	mtx_unlock(&sysmouse_lock);
311 
312 	return (0);
313 }
314 
315 static int
316 sysmouse_read(struct cdev *dev, struct uio *uio, int ioflag)
317 {
318 	unsigned int length;
319 	ssize_t oresid;
320 	int error = 0;
321 
322 	oresid = uio->uio_resid;
323 
324 	mtx_lock(&sysmouse_lock);
325 	length = sysmouse_level >= 1 ? MOUSE_SYS_PACKETSIZE :
326 	    MOUSE_MSC_PACKETSIZE;
327 
328 	while (uio->uio_resid >= length) {
329 		error = sysmouse_buf_read(uio, length);
330 		if (error == 0) {
331 			/* Process the next frame. */
332 			continue;
333 		} else if (error != EWOULDBLOCK) {
334 			/* Error (e.g. EFAULT). */
335 			break;
336 		} else {
337 			/* Block. */
338 			if (oresid != uio->uio_resid || ioflag & O_NONBLOCK)
339 				break;
340 			error = cv_wait_sig(&sysmouse_sleep, &sysmouse_lock);
341 			if (error != 0)
342 				break;
343 		}
344 	}
345 	mtx_unlock(&sysmouse_lock);
346 
347 	return (error);
348 }
349 
350 static int
351 sysmouse_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
352     struct thread *td)
353 {
354 
355 	switch (cmd) {
356 	case FIOASYNC:
357 		mtx_lock(&sysmouse_lock);
358 		if (*(int *)data)
359 			sysmouse_flags |= SM_ASYNC;
360 		else
361 			sysmouse_flags &= ~SM_ASYNC;
362 		mtx_unlock(&sysmouse_lock);
363 		return (0);
364 	case FIONBIO:
365 		return (0);
366 	case FIOGETOWN:
367 		*(int *)data = fgetown(&sysmouse_sigio);
368 		return (0);
369 	case FIOSETOWN:
370 		return (fsetown(*(int *)data, &sysmouse_sigio));
371 	case MOUSE_GETHWINFO: {
372 		mousehw_t *hw = (mousehw_t *)data;
373 
374 		hw->buttons = 10;
375 		hw->iftype = MOUSE_IF_SYSMOUSE;
376 		hw->type = MOUSE_MOUSE;
377 		hw->model = MOUSE_MODEL_GENERIC;
378 		hw->hwid = 0;
379 
380 		return (0);
381 	}
382 	case MOUSE_GETLEVEL:
383 		*(int *)data = sysmouse_level;
384 		return (0);
385 	case MOUSE_GETMODE: {
386 		mousemode_t *mode = (mousemode_t *)data;
387 
388 		mode->rate = -1;
389 		mode->resolution = -1;
390 		mode->accelfactor = 0;
391 		mode->level = sysmouse_level;
392 
393 		switch (mode->level) {
394 		case 0:
395 			mode->protocol = MOUSE_PROTO_MSC;
396 			mode->packetsize = MOUSE_MSC_PACKETSIZE;
397 			mode->syncmask[0] = MOUSE_MSC_SYNCMASK;
398 			mode->syncmask[1] = MOUSE_MSC_SYNC;
399 			break;
400 		case 1:
401 			mode->protocol = MOUSE_PROTO_SYSMOUSE;
402 			mode->packetsize = MOUSE_SYS_PACKETSIZE;
403 			mode->syncmask[0] = MOUSE_SYS_SYNCMASK;
404 			mode->syncmask[1] = MOUSE_SYS_SYNC;
405 			break;
406 		}
407 
408 		return (0);
409 	}
410 	case MOUSE_GETSTATUS:
411 		mtx_lock(&sysmouse_lock);
412 		*(mousestatus_t *)data = sysmouse_status;
413 
414 		sysmouse_status.flags = 0;
415 		sysmouse_status.obutton = sysmouse_status.button;
416 		sysmouse_status.dx = 0;
417 		sysmouse_status.dy = 0;
418 		sysmouse_status.dz = 0;
419 		mtx_unlock(&sysmouse_lock);
420 
421 		return (0);
422 	case MOUSE_SETLEVEL: {
423 		int level;
424 
425 		level = *(int *)data;
426 		if (level != 0 && level != 1)
427 			return (EINVAL);
428 
429 		sysmouse_level = level;
430 		return (0);
431 	}
432 	case MOUSE_SETMODE: {
433 		mousemode_t *mode = (mousemode_t *)data;
434 
435 		switch (mode->level) {
436 		case -1:
437 			/* Do nothing. */
438 			break;
439 		case 0:
440 		case 1:
441 			sysmouse_level = mode->level;
442 			break;
443 		default:
444 			return (EINVAL);
445 		}
446 
447 		return (0);
448 	}
449 	case MOUSE_MOUSECHAR:
450 		return (0);
451 	default:
452 #ifdef VT_SYSMOUSE_DEBUG
453 		printf("sysmouse: unknown ioctl: %c:%lx\n",
454 		    (char)IOCGROUP(cmd), IOCBASECMD(cmd));
455 #endif
456 		return (ENOIOCTL);
457 	}
458 }
459 
460 static int
461 sysmouse_poll(struct cdev *dev, int events, struct thread *td)
462 {
463 	int revents = 0;
464 
465 	mtx_lock(&sysmouse_lock);
466 	if (events & (POLLIN|POLLRDNORM)) {
467 		if (sysmouse_length > 0)
468 			revents = events & (POLLIN|POLLRDNORM);
469 		else
470 			selrecord(td, &sysmouse_bufpoll);
471 	}
472 	mtx_unlock(&sysmouse_lock);
473 
474 	return (revents);
475 }
476 
477 static void
478 sysmouse_drvinit(void *unused)
479 {
480 
481 	if (!vty_enabled(VTY_VT))
482 		return;
483 	mtx_init(&sysmouse_lock, "sysmouse", NULL, MTX_DEF);
484 	cv_init(&sysmouse_sleep, "sysmrd");
485 	make_dev(&sysmouse_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
486 	    "sysmouse");
487 #ifdef EVDEV_SUPPORT
488 	sysmouse_evdev_init();
489 #endif
490 }
491 
492 SYSINIT(sysmouse, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, sysmouse_drvinit, NULL);
493