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