2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * Copyright (c) 2009 The FreeBSD Foundation
8 * This software was developed by Ed Schouten under sponsorship from the
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/condvar.h>
38 #include <sys/consio.h>
39 #include <sys/fcntl.h>
40 #include <sys/filio.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
44 #include <sys/random.h>
45 #include <sys/selinfo.h>
46 #include <sys/sigio.h>
47 #include <sys/signalvar.h>
48 #include <sys/systm.h>
51 #include <dev/vt/vt.h>
53 static d_open_t sysmouse_open;
54 static d_close_t sysmouse_close;
55 static d_read_t sysmouse_read;
56 static d_ioctl_t sysmouse_ioctl;
57 static d_poll_t sysmouse_poll;
59 static struct cdevsw sysmouse_cdevsw = {
60 .d_version = D_VERSION,
61 .d_open = sysmouse_open,
62 .d_close = sysmouse_close,
63 .d_read = sysmouse_read,
64 .d_ioctl = sysmouse_ioctl,
65 .d_poll = sysmouse_poll,
69 static struct mtx sysmouse_lock;
70 static struct cv sysmouse_sleep;
71 static struct selinfo sysmouse_bufpoll;
73 static int sysmouse_level;
74 static mousestatus_t sysmouse_status;
75 static int sysmouse_flags;
77 static struct sigio *sysmouse_sigio;
79 #define SYSMOUSE_MAXFRAMES 250 /* 2 KB */
80 static MALLOC_DEFINE(M_SYSMOUSE, "sysmouse", "sysmouse device");
81 static unsigned char *sysmouse_buffer;
82 static unsigned int sysmouse_start, sysmouse_length;
85 sysmouse_buf_read(struct uio *uio, unsigned int length)
87 unsigned char buf[MOUSE_SYS_PACKETSIZE];
90 if (sysmouse_buffer == NULL)
92 else if (sysmouse_length == 0)
95 memcpy(buf, sysmouse_buffer +
96 sysmouse_start * MOUSE_SYS_PACKETSIZE, MOUSE_SYS_PACKETSIZE);
97 sysmouse_start = (sysmouse_start + 1) % SYSMOUSE_MAXFRAMES;
100 mtx_unlock(&sysmouse_lock);
101 error = uiomove(buf, length, uio);
102 mtx_lock(&sysmouse_lock);
108 sysmouse_buf_store(const unsigned char buf[MOUSE_SYS_PACKETSIZE])
112 if (sysmouse_buffer == NULL || sysmouse_length == SYSMOUSE_MAXFRAMES)
115 idx = (sysmouse_start + sysmouse_length) % SYSMOUSE_MAXFRAMES;
116 memcpy(sysmouse_buffer + idx * MOUSE_SYS_PACKETSIZE, buf,
117 MOUSE_SYS_PACKETSIZE);
119 cv_broadcast(&sysmouse_sleep);
120 selwakeup(&sysmouse_bufpoll);
121 if (sysmouse_flags & SM_ASYNC && sysmouse_sigio != NULL)
122 pgsigio(&sysmouse_sigio, SIGIO, 0);
126 sysmouse_process_event(mouse_info_t *mi)
128 /* MOUSE_BUTTON?DOWN -> MOUSE_MSC_BUTTON?UP */
129 static const int buttonmap[8] = {
130 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
131 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
132 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
134 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
139 unsigned char buf[MOUSE_SYS_PACKETSIZE];
142 random_harvest(mi, sizeof *mi, 2, RANDOM_MOUSE);
144 mtx_lock(&sysmouse_lock);
145 switch (mi->operation) {
147 sysmouse_status.button = mi->u.data.buttons;
149 case MOUSE_MOTION_EVENT:
154 case MOUSE_BUTTON_EVENT:
156 if (mi->u.event.value > 0)
157 sysmouse_status.button |= mi->u.event.id;
159 sysmouse_status.button &= ~mi->u.event.id;
165 sysmouse_status.dx += x;
166 sysmouse_status.dy += y;
167 sysmouse_status.dz += z;
168 sysmouse_status.flags |= ((x || y || z) ? MOUSE_POSCHANGED : 0) |
169 (sysmouse_status.obutton ^ sysmouse_status.button);
170 if (sysmouse_status.flags == 0)
174 /* The first five bytes are compatible with MouseSystems. */
175 buf[0] = MOUSE_MSC_SYNC |
176 buttonmap[sysmouse_status.button & MOUSE_STDBUTTONS];
177 x = imax(imin(x, 255), -256);
180 iy = -imax(imin(y, 255), -256);
182 buf[4] = iy - buf[2];
184 z = imax(imin(z, 127), -128);
185 buf[5] = (z >> 1) & 0x7f;
186 buf[6] = (z - (z >> 1)) & 0x7f;
188 buf[7] = (~sysmouse_status.button >> 3) & 0x7f;
190 sysmouse_buf_store(buf);
192 #ifndef SC_NO_CUTPASTE
193 mtx_unlock(&sysmouse_lock);
194 vt_mouse_event(mi->operation, x, y, mi->u.event.id, mi->u.event.value,
199 done: mtx_unlock(&sysmouse_lock);
203 sysmouse_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
207 buf = malloc(MOUSE_SYS_PACKETSIZE * SYSMOUSE_MAXFRAMES,
208 M_SYSMOUSE, M_WAITOK);
209 mtx_lock(&sysmouse_lock);
210 if (sysmouse_buffer == NULL) {
211 sysmouse_buffer = buf;
212 sysmouse_start = sysmouse_length = 0;
215 free(buf, M_SYSMOUSE);
217 mtx_unlock(&sysmouse_lock);
223 sysmouse_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
226 mtx_lock(&sysmouse_lock);
227 free(sysmouse_buffer, M_SYSMOUSE);
228 sysmouse_buffer = NULL;
230 mtx_unlock(&sysmouse_lock);
236 sysmouse_read(struct cdev *dev, struct uio *uio, int ioflag)
242 oresid = uio->uio_resid;
244 mtx_lock(&sysmouse_lock);
245 length = sysmouse_level >= 1 ? MOUSE_SYS_PACKETSIZE :
246 MOUSE_MSC_PACKETSIZE;
248 while (uio->uio_resid >= length) {
249 error = sysmouse_buf_read(uio, length);
251 /* Process the next frame. */
253 } else if (error != EWOULDBLOCK) {
254 /* Error (e.g. EFAULT). */
258 if (oresid != uio->uio_resid || ioflag & O_NONBLOCK)
260 error = cv_wait_sig(&sysmouse_sleep, &sysmouse_lock);
265 mtx_unlock(&sysmouse_lock);
271 sysmouse_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
277 mtx_lock(&sysmouse_lock);
279 sysmouse_flags |= SM_ASYNC;
281 sysmouse_flags &= ~SM_ASYNC;
282 mtx_unlock(&sysmouse_lock);
287 *(int *)data = fgetown(&sysmouse_sigio);
290 return (fsetown(*(int *)data, &sysmouse_sigio));
291 case MOUSE_GETHWINFO: {
292 mousehw_t *hw = (mousehw_t *)data;
295 hw->iftype = MOUSE_IF_SYSMOUSE;
296 hw->type = MOUSE_MOUSE;
297 hw->model = MOUSE_MODEL_GENERIC;
303 *(int *)data = sysmouse_level;
305 case MOUSE_GETMODE: {
306 mousemode_t *mode = (mousemode_t *)data;
309 mode->resolution = -1;
310 mode->accelfactor = 0;
311 mode->level = sysmouse_level;
313 switch (mode->level) {
315 mode->protocol = MOUSE_PROTO_MSC;
316 mode->packetsize = MOUSE_MSC_PACKETSIZE;
317 mode->syncmask[0] = MOUSE_MSC_SYNCMASK;
318 mode->syncmask[1] = MOUSE_MSC_SYNC;
321 mode->protocol = MOUSE_PROTO_SYSMOUSE;
322 mode->packetsize = MOUSE_SYS_PACKETSIZE;
323 mode->syncmask[0] = MOUSE_SYS_SYNCMASK;
324 mode->syncmask[1] = MOUSE_SYS_SYNC;
330 case MOUSE_GETSTATUS:
331 mtx_lock(&sysmouse_lock);
332 *(mousestatus_t *)data = sysmouse_status;
334 sysmouse_status.flags = 0;
335 sysmouse_status.obutton = sysmouse_status.button;
336 sysmouse_status.dx = 0;
337 sysmouse_status.dy = 0;
338 sysmouse_status.dz = 0;
339 mtx_unlock(&sysmouse_lock);
342 case MOUSE_SETLEVEL: {
345 level = *(int *)data;
346 if (level != 0 && level != 1)
349 sysmouse_level = level;
352 case MOUSE_SETMODE: {
353 mousemode_t *mode = (mousemode_t *)data;
355 switch (mode->level) {
361 sysmouse_level = mode->level;
369 case MOUSE_MOUSECHAR:
372 #ifdef VT_SYSMOUSE_DEBUG
373 printf("sysmouse: unknown ioctl: %c:%lx\n",
374 (char)IOCGROUP(cmd), IOCBASECMD(cmd));
381 sysmouse_poll(struct cdev *dev, int events, struct thread *td)
385 mtx_lock(&sysmouse_lock);
386 if (events & (POLLIN|POLLRDNORM)) {
387 if (sysmouse_length > 0)
388 revents = events & (POLLIN|POLLRDNORM);
390 selrecord(td, &sysmouse_bufpoll);
392 mtx_unlock(&sysmouse_lock);
398 sysmouse_drvinit(void *unused)
401 if (!vty_enabled(VTY_VT))
403 mtx_init(&sysmouse_lock, "sysmouse", NULL, MTX_DEF);
404 cv_init(&sysmouse_sleep, "sysmrd");
405 make_dev(&sysmouse_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
409 SYSINIT(sysmouse, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, sysmouse_drvinit, NULL);