xref: /freebsd/sys/dev/syscons/sysmouse.c (revision 9a14aa017b21c292740c00ee098195cd46642730)
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/ttydefaults.h>
39 #include <sys/kernel.h>
40 #include <sys/consio.h>
41 #include <sys/mouse.h>
42 
43 #include <dev/syscons/syscons.h>
44 
45 #ifndef SC_NO_SYSMOUSE
46 
47 /* local variables */
48 static struct tty	*sysmouse_tty;
49 static int		mouse_level;	/* sysmouse protocol level */
50 static mousestatus_t	mouse_status;
51 
52 static void
53 smdev_close(struct tty *tp)
54 {
55 	mouse_level = 0;
56 }
57 
58 static int
59 smdev_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
60 {
61 	mousehw_t *hw;
62 	mousemode_t *mode;
63 
64 	switch (cmd) {
65 
66 	case MOUSE_GETHWINFO:	/* get device information */
67 		hw = (mousehw_t *)data;
68 		hw->buttons = 10;		/* XXX unknown */
69 		hw->iftype = MOUSE_IF_SYSMOUSE;
70 		hw->type = MOUSE_MOUSE;
71 		hw->model = MOUSE_MODEL_GENERIC;
72 		hw->hwid = 0;
73 		return 0;
74 
75 	case MOUSE_GETMODE:	/* get protocol/mode */
76 		mode = (mousemode_t *)data;
77 		mode->level = mouse_level;
78 		switch (mode->level) {
79 		case 0: /* emulate MouseSystems protocol */
80 			mode->protocol = MOUSE_PROTO_MSC;
81 			mode->rate = -1;		/* unknown */
82 			mode->resolution = -1;	/* unknown */
83 			mode->accelfactor = 0;	/* disabled */
84 			mode->packetsize = MOUSE_MSC_PACKETSIZE;
85 			mode->syncmask[0] = MOUSE_MSC_SYNCMASK;
86 			mode->syncmask[1] = MOUSE_MSC_SYNC;
87 			break;
88 
89 		case 1: /* sysmouse protocol */
90 			mode->protocol = MOUSE_PROTO_SYSMOUSE;
91 			mode->rate = -1;
92 			mode->resolution = -1;
93 			mode->accelfactor = 0;
94 			mode->packetsize = MOUSE_SYS_PACKETSIZE;
95 			mode->syncmask[0] = MOUSE_SYS_SYNCMASK;
96 			mode->syncmask[1] = MOUSE_SYS_SYNC;
97 			break;
98 		}
99 		return 0;
100 
101 	case MOUSE_SETMODE:	/* set protocol/mode */
102 		mode = (mousemode_t *)data;
103 		if (mode->level == -1)
104 			; 	/* don't change the current setting */
105 		else if ((mode->level < 0) || (mode->level > 1))
106 			return EINVAL;
107 		else
108 			mouse_level = mode->level;
109 		return 0;
110 
111 	case MOUSE_GETLEVEL:	/* get operation level */
112 		*(int *)data = mouse_level;
113 		return 0;
114 
115 	case MOUSE_SETLEVEL:	/* set operation level */
116 		if ((*(int *)data  < 0) || (*(int *)data > 1))
117 			return EINVAL;
118 		mouse_level = *(int *)data;
119 		return 0;
120 
121 	case MOUSE_GETSTATUS:	/* get accumulated mouse events */
122 		*(mousestatus_t *)data = mouse_status;
123 		mouse_status.flags = 0;
124 		mouse_status.obutton = mouse_status.button;
125 		mouse_status.dx = 0;
126 		mouse_status.dy = 0;
127 		mouse_status.dz = 0;
128 		return 0;
129 
130 #ifdef notyet
131 	case MOUSE_GETVARS:	/* get internal mouse variables */
132 	case MOUSE_SETVARS:	/* set internal mouse variables */
133 		return ENODEV;
134 #endif
135 
136 	case MOUSE_READSTATE:	/* read status from the device */
137 	case MOUSE_READDATA:	/* read data from the device */
138 		return ENODEV;
139 	}
140 
141 	return (ENOIOCTL);
142 }
143 
144 static int
145 smdev_param(struct tty *tp, struct termios *t)
146 {
147 
148 	/*
149 	 * Set the output baud rate to zero. The mouse device supports
150 	 * no output, so we don't want to waste buffers.
151 	 */
152 	t->c_ispeed = TTYDEF_SPEED;
153 	t->c_ospeed = B0;
154 
155 	return (0);
156 }
157 
158 static struct ttydevsw smdev_ttydevsw = {
159 	.tsw_flags	= TF_NOPREFIX,
160 	.tsw_close	= smdev_close,
161 	.tsw_ioctl	= smdev_ioctl,
162 	.tsw_param	= smdev_param,
163 };
164 
165 static void
166 sm_attach_mouse(void *unused)
167 {
168 	sysmouse_tty = tty_alloc(&smdev_ttydevsw, NULL);
169 	tty_makedev(sysmouse_tty, NULL, "sysmouse");
170 }
171 
172 SYSINIT(sysmouse, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, sm_attach_mouse, NULL);
173 
174 int
175 sysmouse_event(mouse_info_t *info)
176 {
177 	/* MOUSE_BUTTON?DOWN -> MOUSE_MSC_BUTTON?UP */
178 	static int butmap[8] = {
179 	    MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
180 	    MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
181 	    MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
182 	    MOUSE_MSC_BUTTON3UP,
183 	    MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
184 	    MOUSE_MSC_BUTTON2UP,
185 	    MOUSE_MSC_BUTTON1UP,
186 	    0,
187 	};
188 	u_char buf[8];
189 	int x, y, z;
190 	int i, flags = 0;
191 
192 	tty_lock(sysmouse_tty);
193 
194 	switch (info->operation) {
195 	case MOUSE_ACTION:
196         	mouse_status.button = info->u.data.buttons;
197 		/* FALL THROUGH */
198 	case MOUSE_MOTION_EVENT:
199 		x = info->u.data.x;
200 		y = info->u.data.y;
201 		z = info->u.data.z;
202 		break;
203 	case MOUSE_BUTTON_EVENT:
204 		x = y = z = 0;
205 		if (info->u.event.value > 0)
206 			mouse_status.button |= info->u.event.id;
207 		else
208 			mouse_status.button &= ~info->u.event.id;
209 		break;
210 	default:
211 		goto done;
212 	}
213 
214 	mouse_status.dx += x;
215 	mouse_status.dy += y;
216 	mouse_status.dz += z;
217 	mouse_status.flags |= ((x || y || z) ? MOUSE_POSCHANGED : 0)
218 			      | (mouse_status.obutton ^ mouse_status.button);
219 	flags = mouse_status.flags;
220 	if (flags == 0 || !tty_opened(sysmouse_tty))
221 		goto done;
222 
223 	/* the first five bytes are compatible with MouseSystems' */
224 	buf[0] = MOUSE_MSC_SYNC
225 		 | butmap[mouse_status.button & MOUSE_STDBUTTONS];
226 	x = imax(imin(x, 255), -256);
227 	buf[1] = x >> 1;
228 	buf[3] = x - buf[1];
229 	y = -imax(imin(y, 255), -256);
230 	buf[2] = y >> 1;
231 	buf[4] = y - buf[2];
232 	for (i = 0; i < MOUSE_MSC_PACKETSIZE; ++i)
233 		ttydisc_rint(sysmouse_tty, buf[i], 0);
234 	if (mouse_level >= 1) {
235 		/* extended part */
236         	z = imax(imin(z, 127), -128);
237         	buf[5] = (z >> 1) & 0x7f;
238         	buf[6] = (z - (z >> 1)) & 0x7f;
239         	/* buttons 4-10 */
240         	buf[7] = (~mouse_status.button >> 3) & 0x7f;
241         	for (i = MOUSE_MSC_PACKETSIZE; i < MOUSE_SYS_PACKETSIZE; ++i)
242 			ttydisc_rint(sysmouse_tty, buf[i], 0);
243 	}
244 	ttydisc_rint_done(sysmouse_tty);
245 
246 done:	tty_unlock(sysmouse_tty);
247 	return (flags);
248 }
249 
250 #endif /* !SC_NO_SYSMOUSE */
251