xref: /freebsd-src/sys/dev/syscons/sysmouse.c (revision ea906c4152774dff300bb26fbfc1e4188351c89a)
1 /*-
2  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer as
10  *    the first lines of this file unmodified.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "opt_syscons.h"
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/priv.h>
36 #include <sys/serial.h>
37 #include <sys/tty.h>
38 #include <sys/kernel.h>
39 #include <sys/consio.h>
40 #include <sys/mouse.h>
41 
42 #include <dev/syscons/syscons.h>
43 
44 #ifndef SC_NO_SYSMOUSE
45 
46 #define SC_MOUSE 	128		/* minor number */
47 
48 /* local variables */
49 static struct tty	*sysmouse_tty;
50 static int		mouse_level;	/* sysmouse protocol level */
51 static mousestatus_t	mouse_status;
52 
53 static void
54 smdev_close(struct tty *tp)
55 {
56 	mouse_level = 0;
57 }
58 
59 static int
60 smdev_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
61 {
62 	mousehw_t *hw;
63 	mousemode_t *mode;
64 	int s;
65 
66 	switch (cmd) {
67 
68 	case MOUSE_GETHWINFO:	/* get device information */
69 		hw = (mousehw_t *)data;
70 		hw->buttons = 10;		/* XXX unknown */
71 		hw->iftype = MOUSE_IF_SYSMOUSE;
72 		hw->type = MOUSE_MOUSE;
73 		hw->model = MOUSE_MODEL_GENERIC;
74 		hw->hwid = 0;
75 		return 0;
76 
77 	case MOUSE_GETMODE:	/* get protocol/mode */
78 		mode = (mousemode_t *)data;
79 		mode->level = mouse_level;
80 		switch (mode->level) {
81 		case 0: /* emulate MouseSystems protocol */
82 			mode->protocol = MOUSE_PROTO_MSC;
83 			mode->rate = -1;		/* unknown */
84 			mode->resolution = -1;	/* unknown */
85 			mode->accelfactor = 0;	/* disabled */
86 			mode->packetsize = MOUSE_MSC_PACKETSIZE;
87 			mode->syncmask[0] = MOUSE_MSC_SYNCMASK;
88 			mode->syncmask[1] = MOUSE_MSC_SYNC;
89 			break;
90 
91 		case 1: /* sysmouse protocol */
92 			mode->protocol = MOUSE_PROTO_SYSMOUSE;
93 			mode->rate = -1;
94 			mode->resolution = -1;
95 			mode->accelfactor = 0;
96 			mode->packetsize = MOUSE_SYS_PACKETSIZE;
97 			mode->syncmask[0] = MOUSE_SYS_SYNCMASK;
98 			mode->syncmask[1] = MOUSE_SYS_SYNC;
99 			break;
100 		}
101 		return 0;
102 
103 	case MOUSE_SETMODE:	/* set protocol/mode */
104 		mode = (mousemode_t *)data;
105 		if (mode->level == -1)
106 			; 	/* don't change the current setting */
107 		else if ((mode->level < 0) || (mode->level > 1))
108 			return EINVAL;
109 		else
110 			mouse_level = mode->level;
111 		return 0;
112 
113 	case MOUSE_GETLEVEL:	/* get operation level */
114 		*(int *)data = mouse_level;
115 		return 0;
116 
117 	case MOUSE_SETLEVEL:	/* set operation level */
118 		if ((*(int *)data  < 0) || (*(int *)data > 1))
119 			return EINVAL;
120 		mouse_level = *(int *)data;
121 		return 0;
122 
123 	case MOUSE_GETSTATUS:	/* get accumulated mouse events */
124 		s = spltty();
125 		*(mousestatus_t *)data = mouse_status;
126 		mouse_status.flags = 0;
127 		mouse_status.obutton = mouse_status.button;
128 		mouse_status.dx = 0;
129 		mouse_status.dy = 0;
130 		mouse_status.dz = 0;
131 		splx(s);
132 		return 0;
133 
134 #ifdef notyet
135 	case MOUSE_GETVARS:	/* get internal mouse variables */
136 	case MOUSE_SETVARS:	/* set internal mouse variables */
137 		return ENODEV;
138 #endif
139 
140 	case MOUSE_READSTATE:	/* read status from the device */
141 	case MOUSE_READDATA:	/* read data from the device */
142 		return ENODEV;
143 	}
144 
145 	return (ENOIOCTL);
146 }
147 
148 static int
149 smdev_param(struct tty *tp, struct termios *t)
150 {
151 
152 	/*
153 	 * Set the output baud rate to zero. The mouse device supports
154 	 * no output, so we don't want to waste buffers.
155 	 */
156 	t->c_ispeed = TTYDEF_SPEED_PSEUDO;
157 	t->c_ospeed = B0;
158 
159 	return (0);
160 }
161 
162 static struct ttydevsw smdev_ttydevsw = {
163 	.tsw_flags	= TF_NOPREFIX,
164 	.tsw_close	= smdev_close,
165 	.tsw_ioctl	= smdev_ioctl,
166 	.tsw_param	= smdev_param,
167 };
168 
169 static void
170 sm_attach_mouse(void *unused)
171 {
172 	sysmouse_tty = tty_alloc(&smdev_ttydevsw, NULL, &Giant);
173 	tty_makedev(sysmouse_tty, NULL, "sysmouse");
174 }
175 
176 SYSINIT(sysmouse, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, sm_attach_mouse, NULL);
177 
178 int
179 sysmouse_event(mouse_info_t *info)
180 {
181 	/* MOUSE_BUTTON?DOWN -> MOUSE_MSC_BUTTON?UP */
182 	static int butmap[8] = {
183 	    MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
184 	    MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
185 	    MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
186 	    MOUSE_MSC_BUTTON3UP,
187 	    MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
188 	    MOUSE_MSC_BUTTON2UP,
189 	    MOUSE_MSC_BUTTON1UP,
190 	    0,
191 	};
192 	u_char buf[8];
193 	int x, y, z;
194 	int i;
195 
196 	switch (info->operation) {
197 	case MOUSE_ACTION:
198         	mouse_status.button = info->u.data.buttons;
199 		/* FALL THROUGH */
200 	case MOUSE_MOTION_EVENT:
201 		x = info->u.data.x;
202 		y = info->u.data.y;
203 		z = info->u.data.z;
204 		break;
205 	case MOUSE_BUTTON_EVENT:
206 		x = y = z = 0;
207 		if (info->u.event.value > 0)
208 			mouse_status.button |= info->u.event.id;
209 		else
210 			mouse_status.button &= ~info->u.event.id;
211 		break;
212 	default:
213 		return 0;
214 	}
215 
216 	mouse_status.dx += x;
217 	mouse_status.dy += y;
218 	mouse_status.dz += z;
219 	mouse_status.flags |= ((x || y || z) ? MOUSE_POSCHANGED : 0)
220 			      | (mouse_status.obutton ^ mouse_status.button);
221 	if (mouse_status.flags == 0)
222 		return 0;
223 
224 	if ((sysmouse_tty == NULL) || !tty_opened(sysmouse_tty))
225 		return mouse_status.flags;
226 
227 	/* the first five bytes are compatible with MouseSystems' */
228 	buf[0] = MOUSE_MSC_SYNC
229 		 | butmap[mouse_status.button & MOUSE_STDBUTTONS];
230 	x = imax(imin(x, 255), -256);
231 	buf[1] = x >> 1;
232 	buf[3] = x - buf[1];
233 	y = -imax(imin(y, 255), -256);
234 	buf[2] = y >> 1;
235 	buf[4] = y - buf[2];
236 	for (i = 0; i < MOUSE_MSC_PACKETSIZE; ++i)
237 		ttydisc_rint(sysmouse_tty, buf[i], 0);
238 	if (mouse_level >= 1) {
239 		/* extended part */
240         	z = imax(imin(z, 127), -128);
241         	buf[5] = (z >> 1) & 0x7f;
242         	buf[6] = (z - (z >> 1)) & 0x7f;
243         	/* buttons 4-10 */
244         	buf[7] = (~mouse_status.button >> 3) & 0x7f;
245         	for (i = MOUSE_MSC_PACKETSIZE; i < MOUSE_SYS_PACKETSIZE; ++i)
246 			ttydisc_rint(sysmouse_tty, buf[i], 0);
247 	}
248 	ttydisc_rint_done(sysmouse_tty);
249 
250 	return mouse_status.flags;
251 }
252 
253 #endif /* !SC_NO_SYSMOUSE */
254