]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/pc/mouse.c
mouse: disable ps2 hwaccel by default, drop inconsitent packet start byte
[plan9front.git] / sys / src / 9 / pc / mouse.c
1 #include "u.h"
2 #include "../port/lib.h"
3 #include "mem.h"
4 #include "dat.h"
5 #include "fns.h"
6 #include "../port/error.h"
7 #include "io.h"
8
9 #define Image   IMAGE
10 #include <draw.h>
11 #include <memdraw.h>
12 #include <cursor.h>
13 #include "screen.h"
14
15 /*
16  *  mouse types
17  */
18 enum
19 {
20         Mouseother=     0,
21         Mouseserial=    1,
22         MousePS2=       2,
23 };
24
25 static QLock mousectlqlock;
26 static int mousetype;
27 static int intellimouse;
28 static int packetsize;
29 static int resolution;
30 static int accelerated;
31 static int mousehwaccel;
32 static char mouseport[5];
33
34 enum
35 {
36         CMaccelerated,
37         CMhwaccel,
38         CMintellimouse,
39         CMlinear,
40         CMps2,
41         CMps2intellimouse,
42         CMres,
43         CMreset,
44         CMserial,
45 };
46
47 static Cmdtab mousectlmsg[] =
48 {
49         CMaccelerated,          "accelerated",          0,
50         CMhwaccel,              "hwaccel",              2,
51         CMintellimouse,         "intellimouse",         1,
52         CMlinear,               "linear",               1,
53         CMps2,                  "ps2",                  1,
54         CMps2intellimouse,      "ps2intellimouse",      1,
55         CMres,                  "res",                  0,
56         CMreset,                "reset",                1,
57         CMserial,               "serial",               0,
58 };
59
60 /*
61  *  ps/2 mouse message is three bytes
62  *
63  *      byte 0 -        0 0 SDY SDX 1 M R L
64  *      byte 1 -        DX
65  *      byte 2 -        DY
66  *
67  *  shift & right button is the same as middle button
68  *
69  * Intellimouse and AccuPoint with extra buttons deliver
70  *      byte 3 -        00 or 01 or FF according to extra button state.
71  * extra buttons are mapped in this code to buttons 4 and 5.
72  * AccuPoint generates repeated events for these buttons;
73 *  it and Intellimouse generate 'down' events only, so
74  * user-level code is required to generate button 'up' events
75  * if they are needed by the application.
76  * Also on laptops with AccuPoint AND external mouse, the
77  * controller may deliver 3 or 4 bytes according to the type
78  * of the external mouse; code must adapt.
79  *
80  * On the NEC Versa series (and perhaps others?) we seem to
81  * lose a byte from the packet every once in a while, which
82  * means we lose where we are in the instruction stream.
83  * To resynchronize, if we get a byte more than two seconds
84  * after the previous byte, we assume it's the first in a packet.
85  */
86 static void
87 ps2mouseputc(int c, int shift)
88 {
89         static short msg[4];
90         static int nb;
91         static uchar b[] = {0, 1, 4, 5, 2, 3, 6, 7, 0, 1, 2, 3, 2, 3, 6, 7 };
92         static ulong lasttick;
93         ulong m;
94         int buttons, dx, dy;
95
96         /*
97          * Resynchronize in stream with timing; see comment above.
98          */
99         m = MACHP(0)->ticks;
100         if(TK2SEC(m - lasttick) > 2)
101                 nb = 0;
102         lasttick = m;
103
104         /* 
105          *  check byte 0 for consistency
106          */
107         if(nb==0 && (c&0xc8)!=0x08){
108                 if(intellimouse && (c==0x00 || c==0x01 || c==0xFF)){
109                         /* last byte of 4-byte packet */
110                         packetsize = 4;
111                 }
112                 return;
113         }
114
115         msg[nb] = c;
116         if(++nb == packetsize){
117                 nb = 0;
118                 if(msg[0] & 0x10)
119                         msg[1] |= 0xFF00;
120                 if(msg[0] & 0x20)
121                         msg[2] |= 0xFF00;
122
123                 buttons = b[(msg[0]&7) | (shift ? 8 : 0)];
124                 if(intellimouse && packetsize==4){
125                         if((msg[3]&0xc8) == 0x08){
126                                 /* first byte of 3-byte packet */
127                                 packetsize = 3;
128                                 msg[0] = msg[3];
129                                 nb = 1;
130                                 /* fall through to emit previous packet */
131                         }else{
132                                 /* The AccuPoint on the Toshiba 34[48]0CT
133                                  * encodes extra buttons as 4 and 5. They repeat
134                                  * and don't release, however, so user-level
135                                  * timing code is required. Furthermore,
136                                  * intellimice with 3buttons + scroll give a
137                                  * two's complement number in the lower 4 bits
138                                  * (bit 4 is sign extension) that describes
139                                  * the amount the scroll wheel has moved during
140                                  * the last sample. Here we use only the sign to
141                                  * decide whether the wheel is moving up or down
142                                  * and generate a single button 4 or 5 click
143                                  * accordingly.
144                                  */
145                                 if((msg[3] >> 3) & 1) 
146                                         buttons |= 1<<3;
147                                 else if(msg[3] & 0x7) 
148                                         buttons |= 1<<4;
149                         }
150                 }
151                 dx = msg[1];
152                 dy = -msg[2];
153                 mousetrack(dx, dy, buttons, TK2MS(MACHP(0)->ticks));
154         }
155         return;
156 }
157
158 /*
159  *  set up a ps2 mouse
160  */
161 static void
162 ps2mouse(void)
163 {
164         if(mousetype == MousePS2)
165                 return;
166
167         i8042auxenable(ps2mouseputc);
168         /* make mouse streaming, enabled */
169         i8042auxcmd(0xEA);
170         i8042auxcmd(0xF4);
171
172         mousetype = MousePS2;
173         packetsize = 3;
174         mousehwaccel = 0;
175 }
176
177 /*
178  * The PS/2 Trackpoint multiplexor on the IBM Thinkpad T23 ignores
179  * acceleration commands.  It is supposed to pass them on
180  * to the attached device, but my Logitech mouse is simply
181  * not behaving any differently.  For such devices, we allow
182  * the user to use "hwaccel off" to tell us to back off to
183  * software acceleration even if we're using the PS/2 port.
184  * (Serial mice are always software accelerated.)
185  * For more information on the Thinkpad multiplexor, see
186  * http://wwwcssrv.almaden.ibm.com/trackpoint/
187  */
188 static void
189 setaccelerated(int x)
190 {
191         accelerated = x;
192         if(mousehwaccel){
193                 switch(mousetype){
194                 case MousePS2:
195                         i8042auxcmd(0xE7);
196                         return;
197                 }
198         }
199         mouseaccelerate(x);
200 }
201
202 static void
203 setlinear(void)
204 {
205         accelerated = 0;
206         if(mousehwaccel){
207                 switch(mousetype){
208                 case MousePS2:
209                         i8042auxcmd(0xE6);
210                         return;
211                 }
212         }
213         mouseaccelerate(0);
214 }
215
216 static void
217 setres(int n)
218 {
219         resolution = n;
220         switch(mousetype){
221         case MousePS2:
222                 i8042auxcmd(0xE8);
223                 i8042auxcmd(n);
224                 break;
225         }
226 }
227
228 static void
229 setintellimouse(void)
230 {
231         intellimouse = 1;
232         packetsize = 4;
233         switch(mousetype){
234         case MousePS2:
235                 i8042auxcmd(0xF3);      /* set sample */
236                 i8042auxcmd(0xC8);
237                 i8042auxcmd(0xF3);      /* set sample */
238                 i8042auxcmd(0x64);
239                 i8042auxcmd(0xF3);      /* set sample */
240                 i8042auxcmd(0x50);
241                 break;
242         case Mouseserial:
243                 i8250setmouseputc(mouseport, m5mouseputc);
244                 break;
245         }
246 }
247
248 static void
249 resetmouse(void)
250 {
251         packetsize = 3;
252         switch(mousetype){
253         case MousePS2:
254                 i8042auxcmd(0xF6);
255                 i8042auxcmd(0xEA);      /* streaming */
256                 i8042auxcmd(0xE8);      /* set resolution */
257                 i8042auxcmd(3);
258                 i8042auxcmd(0xF4);      /* enabled */
259                 break;
260         }
261 }
262
263 void
264 mousectl(Cmdbuf *cb)
265 {
266         Cmdtab *ct;
267
268         qlock(&mousectlqlock);
269         if(waserror()){
270                 qunlock(&mousectlqlock);
271                 nexterror();
272         }
273
274         ct = lookupcmd(cb, mousectlmsg, nelem(mousectlmsg));
275         switch(ct->index){
276         case CMaccelerated:
277                 setaccelerated(cb->nf == 1 ? 1 : atoi(cb->f[1]));
278                 break;
279         case CMintellimouse:
280                 setintellimouse();
281                 break;
282         case CMlinear:
283                 setlinear();
284                 break;
285         case CMps2:
286                 intellimouse = 0;
287                 ps2mouse();
288                 break;
289         case CMps2intellimouse:
290                 ps2mouse();
291                 setintellimouse();
292                 break;
293         case CMres:
294                 if(cb->nf >= 2)
295                         setres(atoi(cb->f[1]));
296                 else
297                         setres(1);
298                 break;
299         case CMreset:
300                 resetmouse();
301                 if(accelerated)
302                         setaccelerated(accelerated);
303                 if(resolution)
304                         setres(resolution);
305                 if(intellimouse)
306                         setintellimouse();
307                 break;
308         case CMserial:
309                 if(mousetype == Mouseserial)
310                         error(Emouseset);
311
312                 if(cb->nf > 2){
313                         if(strcmp(cb->f[2], "M") == 0)
314                                 i8250mouse(cb->f[1], m3mouseputc, 0);
315                         else if(strcmp(cb->f[2], "MI") == 0)
316                                 i8250mouse(cb->f[1], m5mouseputc, 0);
317                         else
318                                 i8250mouse(cb->f[1], mouseputc, cb->nf == 1);
319                 } else
320                         i8250mouse(cb->f[1], mouseputc, cb->nf == 1);
321
322                 mousetype = Mouseserial;
323                 strncpy(mouseport, cb->f[1], sizeof(mouseport)-1);
324                 packetsize = 3;
325                 break;
326         case CMhwaccel:
327                 if(strcmp(cb->f[1], "on")==0)
328                         mousehwaccel = 1;
329                 else if(strcmp(cb->f[1], "off")==0)
330                         mousehwaccel = 0;
331                 else
332                         cmderror(cb, "bad mouse control message");
333         }
334
335         qunlock(&mousectlqlock);
336         poperror();
337 }