1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
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 #include "opt_evdev.h"
37
38 #include <sys/param.h>
39 #include <sys/condvar.h>
40 #include <sys/consio.h>
41 #include <sys/fcntl.h>
42 #include <sys/filio.h>
43 #include <sys/lock.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/mutex.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
sysmouse_evdev_init(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
sysmouse_evdev_store(int x,int y,int z,int buttons)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
sysmouse_buf_read(struct uio * uio,unsigned int length)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
sysmouse_buf_store(const unsigned char buf[MOUSE_SYS_PACKETSIZE])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
sysmouse_process_event(mouse_info_t * mi)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 if (evdev_is_grabbed(sysmouse_evdev))
251 goto done;
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
sysmouse_open(struct cdev * dev,int oflags,int devtype,struct thread * td)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
sysmouse_close(struct cdev * dev,int fflag,int devtype,struct thread * td)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
sysmouse_read(struct cdev * dev,struct uio * uio,int ioflag)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
sysmouse_ioctl(struct cdev * dev,u_long cmd,caddr_t data,int flag,struct thread * td)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
sysmouse_poll(struct cdev * dev,int events,struct thread * td)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
sysmouse_drvinit(void * unused)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