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