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