1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
|
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "io.h"
#include "../port/error.h"
/*
* TODO
* - shift key should modify right button with non-serial mice
* + intellimouse implementation
* - acceleration for all mouse types
* + spurious interrupt 7 after probing for ps2 mouse for the first time...?
* - test with ms busmouse
* - test with logitech serial mouse
*/
/*
* mouse types
*/
enum
{
Mouseother,
Mouseserial,
MousePS2,
Mousebus,
Mouseintelli,
Mousemsbus,
};
static int mousetype;
static int mouseswap;
static int mouseport; /* port for serial mice, irq for bus mice */
static int mousesubtype;
static int accelerated;
static QLock mouselock;
static int msbusmousedetect(void);
static int busmousedetect(void);
static void mousectl(char *buf);
static void mouseprobe(char *buf, int len);
static void mousestatus(char *buf, int len);
enum{
Qdir,
Qmousectl,
Qmouseprobe,
};
static
Dirtab mousetab[]={
"mousectl", {Qmousectl, 0}, 0, 0600,
"mouseprobe", {Qmouseprobe, 0}, 0, 0400,
};
static Chan*
mouseattach(char* spec)
{
return devattach('m', spec);
}
static int
mousewalk(Chan* c, char* name)
{
return devwalk(c, name, mousetab, nelem(mousetab), devgen);
}
static void
mousestat(Chan* c, char* db)
{
devstat(c, db, mousetab, nelem(mousetab), devgen);
}
static Chan*
mouseopen(Chan* c, int omode)
{
return devopen(c, omode, mousetab, nelem(mousetab), devgen);
}
static void
mouseclose(Chan* c)
{
USED(c);
}
static long
mouseread(Chan* c, void* a, long n, vlong offset)
{
char buf[64];
USED(offset);
switch(c->qid.path & ~CHDIR){
case Qdir:
return devdirread(c, a, n, mousetab, nelem(mousetab), devgen);
case Qmousectl:
qlock(&mouselock);
mousestatus(buf, sizeof(buf));
qunlock(&mouselock);
n = readstr(offset, a, n, buf);
break;
case Qmouseprobe:
if (mousetype)
error(Emouseset);
mouseprobe(buf, sizeof(buf));
n = readstr(offset, a, n, buf);
break;
default:
n=0;
break;
}
return n;
}
static long
mousewrite(Chan* c, void *a, long n, vlong)
{
char buf[64];
if ((c->qid.path & ~CHDIR) != Qmousectl)
error(Ebadusefd);
if (n >= sizeof(buf))
n = sizeof(buf) - 1;
strncpy(buf, a, n);
buf[n] = 0;
qlock(&mouselock);
if (waserror()) {
qunlock(&mouselock);
nexterror();
}
mousectl(buf);
poperror();
qunlock(&mouselock);
return n;
}
static void
track(int b, int dx, int dy)
{
static uchar map[8] = {0,4,2,6,1,5,3,7};
if (mouseswap)
b = map[b&7];
mousetrack(b, dx, dy);
}
static void
setintellimouse(void)
{
i8042auxcmd(0xF3); /* set sample */
i8042auxcmd(0xC8);
i8042auxcmd(0xF3); /* set sample */
i8042auxcmd(0x64);
i8042auxcmd(0xF3); /* set sample */
i8042auxcmd(0x50);
}
/*
* check for an Intellimouse.
* this is only used when we know there's an 8042 aux device
*/
static int
intellimousedetect(void)
{
int id;
setintellimouse();
/* check whether the mouse is now in extended mode */
id = i8042auxcmdval(0xf2); /* identify device */
if (id != 3) {
/*
* set back to standard sample rate (100 per sec)
*/
i8042auxcmd(0xf3);
i8042auxcmd(0x64);
return 0;
}
return 1;
}
static void
mouseprobe(char *buf, int len)
{
USED(len);
/*
* bus mice are easiest, so probe them first
*/
if (busmousedetect())
sprint(buf, "bus\n");
else if (msbusmousedetect())
sprint(buf, "msbus\n");
else if (i8042auxdetect()) {
if (intellimousedetect())
sprint(buf, "ps2intellimouse\n");
else
sprint(buf, "ps2\n");
}
else
*buf = 0;
}
static void
mousestatus(char *buf, int len)
{
char *s;
USED(len);
s = buf;
switch (mousetype) {
case Mouseserial:
if (mousesubtype)
s += sprint(s, "serial %d %c\n", mouseport, mousesubtype);
else
s += sprint(s, "serial %d\n", mouseport);
break;
case MousePS2:
s += sprint(s, "ps2\n");
break;
case Mousebus:
s += sprint(s, "bus %d\n", mouseport);
break;
case Mouseintelli:
s += sprint(s, "intelli\n");
break;
case Mousemsbus:
s += sprint(s, "msbus %d\n", mouseport);
break;
default:
case Mouseother:
s += sprint(s, "unknown\n");
break;
}
if (accelerated)
s += sprint(s, "accelerated\n");
if (mouseswap)
sprint(s, "swap\n");
}
/*
* Logitech 5 byte packed binary mouse format, 8 bit bytes
*
* shift & right button is the same as middle button (for 2 button mice)
*/
static int
logitechmouseputc(Queue *q, int c)
{
static short msg[5];
static int nb;
static uchar b[] = {0, 4, 2, 6, 1, 5, 3, 7, 0, 2, 2, 6, 1, 5, 3, 7};
int dx, dy, newbuttons;
int mouseshifted;
USED(q);
if((c&0xF0) == 0x80)
nb=0;
msg[nb] = c;
if(c & 0x80)
msg[nb] |= ~0xFF; /* sign extend */
if(++nb == 5){
mouseshifted = 0; /* XXX should be from keyboard shift key */
newbuttons = b[((msg[0]&7)^7) | (mouseshifted ? 8 : 0)];
dx = msg[1]+msg[3];
dy = -(msg[2]+msg[4]);
track(newbuttons, dx, dy);
nb = 0;
}
return 0;
}
/*
* microsoft 3 button, 7 bit bytes
*
* byte 0 - 1 L R Y7 Y6 X7 X6
* byte 1 - 0 X5 X4 X3 X2 X1 X0
* byte 2 - 0 Y5 Y4 Y3 Y2 Y1 Y0
* byte 3 - 0 M x x x x x (optional)
*
* shift & right button is the same as middle button (for 2 button mice)
*/
static int
m3mouseputc(Queue*, int c)
{
static uchar msg[3];
static int nb;
static int middle;
static uchar b[] = { 0, 4, 1, 5, 0, 2, 1, 5 };
short x;
int dx, dy, buttons;
/*
* check bit 6 for consistency
*/
if(nb==0){
if((c&0x40) == 0){
/* an extra byte gets sent for the middle button */
if(c & 0x1c)
return 0;
middle = (c&0x20) ? 2 : 0;
buttons = (mouse.b & ~2) | middle;
track(buttons, 0, 0);
return 0;
}
}
msg[nb] = c&0x3f;
if(++nb == 3){
nb = 0;
buttons = middle | b[(msg[0]>>4)&3];
x = (msg[0]&0x3)<<14;
dx = (x>>8) | msg[1];
x = (msg[0]&0xc)<<12;
dy = (x>>8) | msg[2];
track(buttons, dx, dy);
}
return 0;
}
static void
serialmouse(int port, char *type, int setspeed)
{
int (*putc)(Queue *, int) = 0;
char pn[KNAMELEN];
if(mousetype)
error(Emouseset);
if(port >= 2 || port < 0)
error(Ebadarg);
if (type == 0)
putc = logitechmouseputc;
else if (*type == 'M')
putc = m3mouseputc;
else
error(Ebadarg);
snprint(pn, sizeof(pn), "%d", port);
i8250mouse(pn, putc, setspeed);
mousetype = Mouseserial;
mouseport = port;
mousesubtype = (type && *type == 'M') ? 'M' : 0;
}
/*
* ps/2 mouse message is three bytes
*
* byte 0 - 0 0 SDY SDX 1 M R L
* byte 1 - DX
* byte 2 - DY
*
* shift & left button is the same as middle button
*/
static void
ps2mouseputc(int c, int shift)
{
static short msg[3];
static int nb;
static uchar b[] = {0, 1, 4, 5, 2, 3, 6, 7, 0, 1, 2, 5, 2, 3, 6, 7 };
int buttons, dx, dy;
/*
* check byte 0 for consistency
*/
if(nb==0 && (c&0xc8)!=0x08)
return;
msg[nb] = c;
if(++nb == 3){
nb = 0;
if(msg[0] & 0x10)
msg[1] |= 0xFF00;
if(msg[0] & 0x20)
msg[2] |= 0xFF00;
buttons = b[(msg[0]&7) | (shift ? 8 : 0)];
dx = msg[1];
dy = -msg[2];
track(buttons, dx, dy);
}
return;
}
/*
* set up a ps2 mouse
*/
static void
ps2mouse(void)
{
if(mousetype)
error(Emouseset);
i8042auxenable(ps2mouseputc);
/* make mouse streaming, enabled */
i8042auxcmd(0xEA);
i8042auxcmd(0xF4);
mousetype = MousePS2;
}
/* logitech bus mouse ports and commands */
enum {
/* ports */
BMdatap = 0x23c,
BMsigp = 0x23d,
BMctlp = 0x23e,
BMintrp = 0x23e,
BMconfigp = 0x23f,
/* commands */
BMintron = 0x0,
BMintroff = 0x10,
BMrxlo = 0x80,
BMrxhi = 0xa0,
BMrylo = 0xc0,
BMryhi = 0xe0,
BMconfig = 0x91,
BMdefault = 0x90,
BMsigval = 0xa5
};
static void
busmouseintr(Ureg *, void *)
{
char dx, dy;
uchar b;
static uchar oldb;
static Lock intrlock;
ilock(&intrlock);
outb(BMintrp, BMintroff);
outb(BMctlp, BMrxlo);
dx = inb(BMdatap) & 0xf;
outb(BMctlp, BMrxhi);
dx |= (inb(BMdatap) & 0xf) << 4;
outb(BMctlp, BMrylo);
dy = inb(BMdatap) & 0xf;
outb(BMctlp, BMryhi);
b = inb(BMdatap);
dy |= (b & 0xf) << 4;
b = ~(b >> 5) & 7;
if (dx || dy || b != oldb) {
oldb = b;
track((b>>2)|(b&0x02)|((b&0x01)<<2), dx, dy);
}
iunlock(&intrlock);
outb(BMintrp, BMintron);
}
static int
busmousedetect(void)
{
outb(BMconfigp, BMconfig);
outb(BMsigp, BMsigval);
delay(2);
if (inb(BMsigp) != BMsigval)
return 0;
outb(BMconfigp, BMdefault);
return 1;
}
/*
* set up a logitech bus mouse
*/
static void
busmouse(int irq)
{
if (mousetype)
error(Emouseset);
if (!busmousedetect())
error(Enodev);
intrenable(irq >= 0 ? irq+VectorPIC : VectorBUSMOUSE, busmouseintr, 0, BUSUNKNOWN);
outb(BMintrp, BMintron);
mousetype = Mousebus;
mouseport = irq >= 0 ? irq : VectorBUSMOUSE-VectorPIC;
}
/* microsoft bus mouse ports and commands */
enum {
MBMdatap= 0x23d,
MBMsigp= 0x23e,
MBMctlp= 0x23c,
MBMconfigp= 0x23f,
MBMintron= 0x11,
MBMintroff= 0x10,
MBMrbuttons= 0x00,
MBMrx= 0x01,
MBMry= 0x02,
MBMstart= 0x80,
MBMcmd= 0x07,
};
static void
msbusmouseintr(Ureg *, void *)
{
char dx, dy;
uchar b;
static uchar oldb;
static Lock intrlock;
ilock(&intrlock);
outb(MBMctlp, MBMcmd);
outb(MBMdatap, inb(MBMdatap)|0x20);
outb(MBMctlp, MBMrx);
dx = inb(MBMdatap);
outb(MBMctlp, MBMry);
dy = inb(MBMdatap);
outb(MBMctlp, MBMrbuttons);
b = inb(MBMdatap) & 0x7;
outb(MBMctlp, MBMcmd);
outb(MBMdatap, inb(MBMdatap)&0xdf);
if (dx != 0 || dy != 0 || b != oldb) {
oldb = b;
/* XXX this is almost certainly wrong */
track((b>>2)|(b&0x02)|((b&0x01)<<2), dx, dy);
}
iunlock(&intrlock);
}
static int
msbusmousedetect(void)
{
if (inb(MBMsigp) == 0xde) {
int v, i;
delay(1);
v = inb(MBMsigp);
delay(1);
for (i = 0; i < 4; i++) {
if (inb(MBMsigp) != 0xde)
break;
delay(1);
if (inb(MBMsigp) != v)
break;
delay(1);
}
if (i == 4) {
outb(MBMctlp, MBMcmd);
return 1;
}
}
return 0;
}
static void
msbusmouse(int irq)
{
if (mousetype)
error(Emouseset);
if (!msbusmousedetect())
error(Enodev);
mousetype = Mousemsbus;
mouseport = irq >= 0 ? irq : VectorBUSMOUSE-VectorPIC;
intrenable(irq >= 0 ? irq+VectorPIC : VectorBUSMOUSE, msbusmouseintr, 0, BUSUNKNOWN);
outb(MBMdatap, MBMintron);
}
static void
mousectl(char *buf)
{
int nf, x;
char *field[10];
nf = getfields(buf, field, 10, 1, " \t\n");
if (nf < 1)
return;
if(strncmp(field[0], "serial", 6) == 0){
switch(nf){
/* the difference between these two cases is intriguing - wrtp */
case 1:
serialmouse(atoi(field[0]+6), 0, 1);
break;
case 2:
serialmouse(atoi(field[1]), 0, 0);
break;
case 3:
default:
serialmouse(atoi(field[1]), field[2], 0);
break;
}
} else if(strcmp(field[0], "ps2") == 0){
ps2mouse();
} else if (strcmp(field[0], "ps2intellimouse") == 0) {
ps2mouse();
setintellimouse();
} else if (strncmp(field[0], "bus", 3) == 0 || strncmp(field[0], "msbus", 5) == 0) {
int irq, isms;
isms = (field[0][0] == 'm');
if (nf == 1)
irq = atoi(field[0] + (isms ? 5 : 3));
else
irq = atoi(field[1]);
if (irq < 1)
irq = -1;
if (isms)
msbusmouse(irq);
else
busmouse(irq);
} else if(strcmp(field[0], "accelerated") == 0){
switch(mousetype){
case MousePS2:
x = splhi();
i8042auxcmd(0xE7);
splx(x);
accelerated = 1;
break;
}
} else if(strcmp(field[0], "linear") == 0){
switch(mousetype){
case MousePS2:
x = splhi();
i8042auxcmd(0xE6);
splx(x);
accelerated = 0;
break;
}
} else if(strcmp(field[0], "res") == 0){
int n,m;
switch(nf){
default:
n = 0x02;
m = 0x23;
break;
case 2:
n = atoi(field[1])&0x3;
m = 0x7;
break;
case 3:
n = atoi(field[1])&0x3;
m = atoi(field[2])&0x7;
break;
}
switch(mousetype){
case MousePS2:
x = splhi();
i8042auxcmd(0xE8);
i8042auxcmd(n);
i8042auxcmd(0x5A);
i8042auxcmd(0x30|m);
i8042auxcmd(0x5A);
i8042auxcmd(0x20|(m>>1));
splx(x);
break;
}
} else if(strcmp(field[0], "swap") == 0)
mouseswap ^= 1;
}
Dev mousedevtab = { /* defaults in dev.c */
'm',
"mouse",
devreset, /* devreset */
devinit, /* devinit */
mouseattach,
devdetach,
devclone, /* devclone */
mousewalk,
mousestat,
mouseopen,
devcreate, /* devcreate */
mouseclose,
mouseread,
devbread, /* devbread */
mousewrite,
devbwrite, /* devbwrite */
devremove, /* devremove */
devwstat, /* devwstat */
};
|