2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "opt_evdev.h"
34 #include "opt_syscons.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/serial.h>
41 #include <sys/ttydefaults.h>
42 #include <sys/kernel.h>
44 #include <sys/consio.h>
45 #include <sys/mouse.h>
47 #include <dev/syscons/syscons.h>
50 #include <dev/evdev/input.h>
51 #include <dev/evdev/evdev.h>
54 #ifndef SC_NO_SYSMOUSE
57 static struct tty *sysmouse_tty;
58 static int mouse_level; /* sysmouse protocol level */
59 static mousestatus_t mouse_status;
62 static struct evdev_dev *sysmouse_evdev;
65 smdev_evdev_init(void)
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;
90 smdev_evdev_write(int x, int y, int z, int buttons)
93 if (sysmouse_evdev == NULL || !(evdev_rcpt_mask & EVDEV_RCPT_SYSMOUSE))
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:
103 evdev_push_rel(sysmouse_evdev, REL_WHEEL, -z);
107 evdev_push_rel(sysmouse_evdev, REL_HWHEEL, z / 2);
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));
118 case EVDEV_SYSMOUSE_T_AXIS_NONE:
120 evdev_push_rel(sysmouse_evdev, REL_WHEEL, -z);
122 evdev_push_mouse_btn(sysmouse_evdev, buttons);
123 evdev_sync(sysmouse_evdev);
128 smdev_close(struct tty *tp)
134 smdev_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
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;
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;
164 case 1: /* sysmouse protocol */
165 mode->protocol = MOUSE_PROTO_SYSMOUSE;
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;
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))
183 mouse_level = mode->level;
186 case MOUSE_GETLEVEL: /* get operation level */
187 *(int *)data = mouse_level;
190 case MOUSE_SETLEVEL: /* set operation level */
191 if ((*(int *)data < 0) || (*(int *)data > 1))
193 mouse_level = *(int *)data;
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;
205 case MOUSE_READSTATE: /* read status from the device */
206 case MOUSE_READDATA: /* read data from the device */
214 smdev_param(struct tty *tp, struct termios *t)
218 * Set the output baud rate to zero. The mouse device supports
219 * no output, so we don't want to waste buffers.
221 t->c_ispeed = TTYDEF_SPEED;
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,
235 sm_attach_mouse(void *unused)
237 if (!vty_enabled(VTY_SC))
239 sysmouse_tty = tty_alloc(&smdev_ttydevsw, NULL);
240 tty_makedev(sysmouse_tty, NULL, "sysmouse");
246 SYSINIT(sysmouse, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, sm_attach_mouse, NULL);
249 sysmouse_event(mouse_info_t *info)
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,
257 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
266 tty_lock(sysmouse_tty);
268 switch (info->operation) {
270 mouse_status.button = info->u.data.buttons;
272 case MOUSE_MOTION_EVENT:
277 case MOUSE_BUTTON_EVENT:
279 if (info->u.event.value > 0)
280 mouse_status.button |= info->u.event.id;
282 mouse_status.button &= ~info->u.event.id;
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;
298 smdev_evdev_write(x, y, z, mouse_status.button);
301 if (!tty_opened(sysmouse_tty))
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);
310 y = -imax(imin(y, 255), -256);
313 for (i = 0; i < MOUSE_MSC_PACKETSIZE; ++i)
314 ttydisc_rint(sysmouse_tty, buf[i], 0);
315 if (mouse_level >= 1) {
317 z = imax(imin(z, 127), -128);
318 buf[5] = (z >> 1) & 0x7f;
319 buf[6] = (z - (z >> 1)) & 0x7f;
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);
325 ttydisc_rint_done(sysmouse_tty);
327 done: tty_unlock(sysmouse_tty);
331 #endif /* !SC_NO_SYSMOUSE */