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