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