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>
39 #include <sys/consio.h>
40 #include <sys/fcntl.h>
41 #include <sys/filio.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/mouse.h>
46 #include <sys/random.h>
47 #include <sys/selinfo.h>
48 #include <sys/sigio.h>
49 #include <sys/signalvar.h>
50 #include <sys/systm.h>
53 #include <dev/vt/vt.h>
55 static d_open_t sysmouse_open;
56 static d_close_t sysmouse_close;
57 static d_read_t sysmouse_read;
58 static d_ioctl_t sysmouse_ioctl;
59 static d_poll_t sysmouse_poll;
61 static struct cdevsw sysmouse_cdevsw = {
62 .d_version = D_VERSION,
63 .d_open = sysmouse_open,
64 .d_close = sysmouse_close,
65 .d_read = sysmouse_read,
66 .d_ioctl = sysmouse_ioctl,
67 .d_poll = sysmouse_poll,
71 static struct mtx sysmouse_lock;
72 static struct cv sysmouse_sleep;
73 static struct selinfo sysmouse_bufpoll;
75 static int sysmouse_level;
76 static mousestatus_t sysmouse_status;
77 static int sysmouse_flags;
79 static struct sigio *sysmouse_sigio;
81 #define SYSMOUSE_MAXFRAMES 250 /* 2 KB */
82 static MALLOC_DEFINE(M_SYSMOUSE, "sysmouse", "sysmouse device");
83 static unsigned char *sysmouse_buffer;
84 static unsigned int sysmouse_start, sysmouse_length;
87 sysmouse_buf_read(struct uio *uio, unsigned int length)
89 unsigned char buf[MOUSE_SYS_PACKETSIZE];
92 if (sysmouse_buffer == NULL)
94 else if (sysmouse_length == 0)
97 memcpy(buf, sysmouse_buffer +
98 sysmouse_start * MOUSE_SYS_PACKETSIZE, MOUSE_SYS_PACKETSIZE);
99 sysmouse_start = (sysmouse_start + 1) % SYSMOUSE_MAXFRAMES;
102 mtx_unlock(&sysmouse_lock);
103 error = uiomove(buf, length, uio);
104 mtx_lock(&sysmouse_lock);
110 sysmouse_buf_store(const unsigned char buf[MOUSE_SYS_PACKETSIZE])
114 if (sysmouse_buffer == NULL || sysmouse_length == SYSMOUSE_MAXFRAMES)
117 idx = (sysmouse_start + sysmouse_length) % SYSMOUSE_MAXFRAMES;
118 memcpy(sysmouse_buffer + idx * MOUSE_SYS_PACKETSIZE, buf,
119 MOUSE_SYS_PACKETSIZE);
121 cv_broadcast(&sysmouse_sleep);
122 selwakeup(&sysmouse_bufpoll);
123 if (sysmouse_flags & SM_ASYNC && sysmouse_sigio != NULL)
124 pgsigio(&sysmouse_sigio, SIGIO, 0);
128 sysmouse_process_event(mouse_info_t *mi)
130 /* MOUSE_BUTTON?DOWN -> MOUSE_MSC_BUTTON?UP */
131 static const int buttonmap[8] = {
132 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
133 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
134 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
136 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
141 unsigned char buf[MOUSE_SYS_PACKETSIZE];
144 random_harvest(mi, sizeof *mi, 2, RANDOM_MOUSE);
146 mtx_lock(&sysmouse_lock);
147 switch (mi->operation) {
149 sysmouse_status.button = mi->u.data.buttons;
151 case MOUSE_MOTION_EVENT:
156 case MOUSE_BUTTON_EVENT:
158 if (mi->u.event.value > 0)
159 sysmouse_status.button |= mi->u.event.id;
161 sysmouse_status.button &= ~mi->u.event.id;
167 sysmouse_status.dx += x;
168 sysmouse_status.dy += y;
169 sysmouse_status.dz += z;
170 sysmouse_status.flags |= ((x || y || z) ? MOUSE_POSCHANGED : 0) |
171 (sysmouse_status.obutton ^ sysmouse_status.button);
172 if (sysmouse_status.flags == 0)
175 /* The first five bytes are compatible with MouseSystems. */
176 buf[0] = MOUSE_MSC_SYNC |
177 buttonmap[sysmouse_status.button & MOUSE_STDBUTTONS];
178 x = imax(imin(x, 255), -256);
181 y = -imax(imin(y, 255), -256);
185 z = imax(imin(z, 127), -128);
186 buf[5] = (z >> 1) & 0x7f;
187 buf[6] = (z - (z >> 1)) & 0x7f;
189 buf[7] = (~sysmouse_status.button >> 3) & 0x7f;
191 sysmouse_buf_store(buf);
192 done: mtx_unlock(&sysmouse_lock);
196 sysmouse_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
200 buf = malloc(MOUSE_SYS_PACKETSIZE * SYSMOUSE_MAXFRAMES,
201 M_SYSMOUSE, M_WAITOK);
202 mtx_lock(&sysmouse_lock);
203 if (sysmouse_buffer == NULL) {
204 sysmouse_buffer = buf;
205 sysmouse_start = sysmouse_length = 0;
208 free(buf, M_SYSMOUSE);
210 mtx_unlock(&sysmouse_lock);
216 sysmouse_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
219 mtx_lock(&sysmouse_lock);
220 free(sysmouse_buffer, M_SYSMOUSE);
221 sysmouse_buffer = NULL;
222 mtx_unlock(&sysmouse_lock);
228 sysmouse_read(struct cdev *dev, struct uio *uio, int ioflag)
234 oresid = uio->uio_resid;
236 mtx_lock(&sysmouse_lock);
237 length = sysmouse_level >= 1 ? MOUSE_SYS_PACKETSIZE :
238 MOUSE_MSC_PACKETSIZE;
240 while (uio->uio_resid >= length) {
241 error = sysmouse_buf_read(uio, length);
243 /* Process the next frame. */
245 } else if (error != EWOULDBLOCK) {
246 /* Error (e.g. EFAULT). */
250 if (oresid != uio->uio_resid || ioflag & O_NONBLOCK)
252 error = cv_wait_sig(&sysmouse_sleep, &sysmouse_lock);
257 mtx_unlock(&sysmouse_lock);
263 sysmouse_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
269 mtx_lock(&sysmouse_lock);
271 sysmouse_flags |= SM_ASYNC;
273 sysmouse_flags &= ~SM_ASYNC;
274 mtx_unlock(&sysmouse_lock);
279 *(int *)data = fgetown(&sysmouse_sigio);
282 return (fsetown(*(int *)data, &sysmouse_sigio));
283 case MOUSE_GETHWINFO: {
284 mousehw_t *hw = (mousehw_t *)data;
287 hw->iftype = MOUSE_IF_SYSMOUSE;
288 hw->type = MOUSE_MOUSE;
289 hw->model = MOUSE_MODEL_GENERIC;
295 *(int *)data = sysmouse_level;
297 case MOUSE_GETMODE: {
298 mousemode_t *mode = (mousemode_t *)data;
301 mode->resolution = -1;
302 mode->accelfactor = 0;
303 mode->level = sysmouse_level;
305 switch (mode->level) {
307 mode->protocol = MOUSE_PROTO_MSC;
308 mode->packetsize = MOUSE_MSC_PACKETSIZE;
309 mode->syncmask[0] = MOUSE_MSC_SYNCMASK;
310 mode->syncmask[1] = MOUSE_MSC_SYNC;
313 mode->protocol = MOUSE_PROTO_SYSMOUSE;
314 mode->packetsize = MOUSE_SYS_PACKETSIZE;
315 mode->syncmask[0] = MOUSE_SYS_SYNCMASK;
316 mode->syncmask[1] = MOUSE_SYS_SYNC;
322 case MOUSE_GETSTATUS:
323 mtx_lock(&sysmouse_lock);
324 *(mousestatus_t *)data = sysmouse_status;
326 sysmouse_status.flags = 0;
327 sysmouse_status.obutton = sysmouse_status.button;
328 sysmouse_status.dx = 0;
329 sysmouse_status.dy = 0;
330 sysmouse_status.dz = 0;
331 mtx_unlock(&sysmouse_lock);
334 case MOUSE_SETLEVEL: {
337 level = *(int *)data;
338 if (level != 0 && level != 1)
341 sysmouse_level = level;
344 case MOUSE_SETMODE: {
345 mousemode_t *mode = (mousemode_t *)data;
347 switch (mode->level) {
353 sysmouse_level = mode->level;
362 printf("sysmouse: unknown ioctl: %c:%lx\n",
363 (char)IOCGROUP(cmd), IOCBASECMD(cmd));
369 sysmouse_poll(struct cdev *dev, int events, struct thread *td)
373 mtx_lock(&sysmouse_lock);
374 if (events & (POLLIN|POLLRDNORM)) {
375 if (sysmouse_length > 0)
376 revents = events & (POLLIN|POLLRDNORM);
378 selrecord(td, &sysmouse_bufpoll);
380 mtx_unlock(&sysmouse_lock);
386 sysmouse_drvinit(void *unused)
389 mtx_init(&sysmouse_lock, "sysmouse", NULL, MTX_DEF);
390 cv_init(&sysmouse_sleep, "sysmrd");
391 make_dev(&sysmouse_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
395 SYSINIT(sysmouse, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, sysmouse_drvinit, NULL);