xref: /freebsd/sys/dev/syscons/sysmouse.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
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  * $FreeBSD$
27  */
28 
29 #include "opt_syscons.h"
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/conf.h>
34 #include <sys/tty.h>
35 #include <sys/kernel.h>
36 #include <sys/consio.h>
37 #include <sys/mouse.h>
38 
39 #include <dev/syscons/syscons.h>
40 
41 #ifndef SC_NO_SYSMOUSE
42 
43 #define	CDEV_MAJOR	12		/* major number, shared with syscons */
44 #define SC_MOUSE 	128		/* minor number */
45 
46 static d_open_t		smopen;
47 static d_close_t	smclose;
48 static d_ioctl_t	smioctl;
49 
50 static struct cdevsw sm_cdevsw = {
51 	/* open */	smopen,
52 	/* close */	smclose,
53 	/* read */	ttyread,
54 	/* write */	nowrite,
55 	/* ioctl */	smioctl,
56 	/* poll */	ttypoll,
57 	/* mmap */	nommap,
58 	/* strategy */	nostrategy,
59 	/* name */	"sysmouse",
60 	/* maj */	CDEV_MAJOR,
61 	/* dump */	nodump,
62 	/* psize */	nopsize,
63 	/* flags */	D_TTY,
64 	/* bmaj */	-1
65 };
66 
67 /* local variables */
68 static struct tty	*sysmouse_tty;
69 static int		mouse_level;	/* sysmouse protocol level */
70 static mousestatus_t	mouse_status;
71 
72 static void		smstart(struct tty *tp);
73 static int		smparam(struct tty *tp, struct termios *t);
74 
75 static int
76 smopen(dev_t dev, int flag, int mode, struct proc *p)
77 {
78 	struct tty *tp;
79 
80 	DPRINTF(5, ("smopen: dev:%d,%d, vty:%d\n",
81 		major(dev), minor(dev), SC_VTY(dev)));
82 
83 #if 0
84 	if (SC_VTY(dev) != SC_MOUSE)
85 		return ENXIO;
86 #endif
87 
88 	tp = dev->si_tty = ttymalloc(dev->si_tty);
89 	if (!(tp->t_state & TS_ISOPEN)) {
90 		tp->t_oproc = smstart;
91 		tp->t_param = smparam;
92 		tp->t_stop = nottystop;
93 		tp->t_dev = dev;
94 		ttychars(tp);
95 		tp->t_iflag = TTYDEF_IFLAG;
96 		tp->t_oflag = TTYDEF_OFLAG;
97 		tp->t_cflag = TTYDEF_CFLAG;
98 		tp->t_lflag = TTYDEF_LFLAG;
99 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
100 		smparam(tp, &tp->t_termios);
101 		(*linesw[tp->t_line].l_modem)(tp, 1);
102 	} else if (tp->t_state & TS_XCLUDE && suser(p)) {
103 		return EBUSY;
104 	}
105 
106 	return (*linesw[tp->t_line].l_open)(dev, tp);
107 }
108 
109 static int
110 smclose(dev_t dev, int flag, int mode, struct proc *p)
111 {
112 	struct tty *tp;
113 	int s;
114 
115 	tp = dev->si_tty;
116 	s = spltty();
117 	mouse_level = 0;
118 	(*linesw[tp->t_line].l_close)(tp, flag);
119 	ttyclose(tp);
120 	splx(s);
121 
122 	return 0;
123 }
124 
125 static void
126 smstart(struct tty *tp)
127 {
128 	struct clist *rbp;
129 	u_char buf[PCBURST];
130 	int s;
131 
132 	s = spltty();
133 	if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
134 		tp->t_state |= TS_BUSY;
135 		rbp = &tp->t_outq;
136 		while (rbp->c_cc)
137 			q_to_b(rbp, buf, PCBURST);
138 		tp->t_state &= ~TS_BUSY;
139 		ttwwakeup(tp);
140 	}
141 	splx(s);
142 }
143 
144 static int
145 smparam(struct tty *tp, struct termios *t)
146 {
147 	tp->t_ispeed = t->c_ispeed;
148 	tp->t_ospeed = t->c_ospeed;
149 	tp->t_cflag = t->c_cflag;
150 	return 0;
151 }
152 
153 static int
154 smioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
155 {
156 	struct tty *tp;
157 	mousehw_t *hw;
158 	mousemode_t *mode;
159 	int error;
160 	int s;
161 
162 	tp = dev->si_tty;
163 	switch (cmd) {
164 
165 	case MOUSE_GETHWINFO:	/* get device information */
166 		hw = (mousehw_t *)data;
167 		hw->buttons = 10;		/* XXX unknown */
168 		hw->iftype = MOUSE_IF_SYSMOUSE;
169 		hw->type = MOUSE_MOUSE;
170 		hw->model = MOUSE_MODEL_GENERIC;
171 		hw->hwid = 0;
172 		return 0;
173 
174 	case MOUSE_GETMODE:	/* get protocol/mode */
175 		mode = (mousemode_t *)data;
176 		mode->level = mouse_level;
177 		switch (mode->level) {
178 		case 0: /* emulate MouseSystems protocol */
179 			mode->protocol = MOUSE_PROTO_MSC;
180 			mode->rate = -1;		/* unknown */
181 			mode->resolution = -1;	/* unknown */
182 			mode->accelfactor = 0;	/* disabled */
183 			mode->packetsize = MOUSE_MSC_PACKETSIZE;
184 			mode->syncmask[0] = MOUSE_MSC_SYNCMASK;
185 			mode->syncmask[1] = MOUSE_MSC_SYNC;
186 			break;
187 
188 		case 1: /* sysmouse protocol */
189 			mode->protocol = MOUSE_PROTO_SYSMOUSE;
190 			mode->rate = -1;
191 			mode->resolution = -1;
192 			mode->accelfactor = 0;
193 			mode->packetsize = MOUSE_SYS_PACKETSIZE;
194 			mode->syncmask[0] = MOUSE_SYS_SYNCMASK;
195 			mode->syncmask[1] = MOUSE_SYS_SYNC;
196 			break;
197 		}
198 		return 0;
199 
200 	case MOUSE_SETMODE:	/* set protocol/mode */
201 		mode = (mousemode_t *)data;
202 		if (mode->level == -1)
203 			; 	/* don't change the current setting */
204 		else if ((mode->level < 0) || (mode->level > 1))
205 			return EINVAL;
206 		else
207 			mouse_level = mode->level;
208 		return 0;
209 
210 	case MOUSE_GETLEVEL:	/* get operation level */
211 		*(int *)data = mouse_level;
212 		return 0;
213 
214 	case MOUSE_SETLEVEL:	/* set operation level */
215 		if ((*(int *)data  < 0) || (*(int *)data > 1))
216 			return EINVAL;
217 		mouse_level = *(int *)data;
218 		return 0;
219 
220 	case MOUSE_GETSTATUS:	/* get accumulated mouse events */
221 		s = spltty();
222 		*(mousestatus_t *)data = mouse_status;
223 		mouse_status.flags = 0;
224 		mouse_status.obutton = mouse_status.button;
225 		mouse_status.dx = 0;
226 		mouse_status.dy = 0;
227 		mouse_status.dz = 0;
228 		splx(s);
229 		return 0;
230 
231 #if notyet
232 	case MOUSE_GETVARS:	/* get internal mouse variables */
233 	case MOUSE_SETVARS:	/* set internal mouse variables */
234 		return ENODEV;
235 #endif
236 
237 	case MOUSE_READSTATE:	/* read status from the device */
238 	case MOUSE_READDATA:	/* read data from the device */
239 		return ENODEV;
240 	}
241 
242 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
243 	if (error != ENOIOCTL)
244 		return error;
245 	error = ttioctl(tp, cmd, data, flag);
246 	if (error != ENOIOCTL)
247 		return error;
248 	return ENOTTY;
249 }
250 
251 static void
252 sm_attach_mouse(void *unused)
253 {
254 	dev_t dev;
255 
256 	dev = make_dev(&sm_cdevsw, SC_MOUSE, UID_ROOT, GID_WHEEL, 0600,
257 		       "sysmouse");
258 	dev->si_tty = sysmouse_tty = ttymalloc(sysmouse_tty);
259 	/* sysmouse doesn't have scr_stat */
260 }
261 
262 SYSINIT(sysmouse, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR,
263 	sm_attach_mouse, NULL)
264 
265 int
266 sysmouse_event(mouse_info_t *info)
267 {
268 	/* MOUSE_BUTTON?DOWN -> MOUSE_MSC_BUTTON?UP */
269 	static int butmap[8] = {
270 	    MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
271 	    MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
272 	    MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
273 	    MOUSE_MSC_BUTTON3UP,
274 	    MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
275 	    MOUSE_MSC_BUTTON2UP,
276 	    MOUSE_MSC_BUTTON1UP,
277 	    0,
278 	};
279 	u_char buf[8];
280 	int x, y, z;
281 	int i;
282 
283 	switch (info->operation) {
284 	case MOUSE_ACTION:
285         	mouse_status.button = info->u.data.buttons;
286 		/* FALL THROUGH */
287 	case MOUSE_MOTION_EVENT:
288 		x = info->u.data.x;
289 		y = info->u.data.y;
290 		z = info->u.data.z;
291 		break;
292 	case MOUSE_BUTTON_EVENT:
293 		x = y = z = 0;
294 		if (info->u.event.value > 0)
295 			mouse_status.button |= info->u.event.id;
296 		else
297 			mouse_status.button &= ~info->u.event.id;
298 		break;
299 	default:
300 		return 0;
301 	}
302 
303 	mouse_status.dx += x;
304 	mouse_status.dy += y;
305 	mouse_status.dz += z;
306 	mouse_status.flags |= ((x || y || z) ? MOUSE_POSCHANGED : 0)
307 			      | (mouse_status.obutton ^ mouse_status.button);
308 	if (mouse_status.flags == 0)
309 		return 0;
310 
311 	if ((sysmouse_tty == NULL) || !(sysmouse_tty->t_state & TS_ISOPEN))
312 		return mouse_status.flags;
313 
314 	/* the first five bytes are compatible with MouseSystems' */
315 	buf[0] = MOUSE_MSC_SYNC
316 		 | butmap[mouse_status.button & MOUSE_STDBUTTONS];
317 	x = imax(imin(x, 255), -256);
318 	buf[1] = x >> 1;
319 	buf[3] = x - buf[1];
320 	y = -imax(imin(y, 255), -256);
321 	buf[2] = y >> 1;
322 	buf[4] = y - buf[2];
323 	for (i = 0; i < MOUSE_MSC_PACKETSIZE; ++i)
324 		(*linesw[sysmouse_tty->t_line].l_rint)(buf[i], sysmouse_tty);
325 	if (mouse_level >= 1) {
326 		/* extended part */
327         	z = imax(imin(z, 127), -128);
328         	buf[5] = (z >> 1) & 0x7f;
329         	buf[6] = (z - (z >> 1)) & 0x7f;
330         	/* buttons 4-10 */
331         	buf[7] = (~mouse_status.button >> 3) & 0x7f;
332         	for (i = MOUSE_MSC_PACKETSIZE; i < MOUSE_SYS_PACKETSIZE; ++i)
333 			(*linesw[sysmouse_tty->t_line].l_rint)(buf[i],
334 							       sysmouse_tty);
335 	}
336 
337 	return mouse_status.flags;
338 }
339 
340 #endif /* !SC_NO_SYSMOUSE */
341