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