-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps2.c
594 lines (511 loc) · 11.7 KB
/
ps2.c
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
extern unsigned char inb(unsigned short port);
extern unsigned int ind(unsigned short port);
extern unsigned short inw(unsigned short port);
extern void outb(unsigned short port, unsigned char val);
extern void outd(unsigned short port, unsigned int val);
extern void outw(unsigned short port, unsigned short val);
extern void printki(long n);
extern void printk(char* s);
extern void set_int(unsigned char inte, void* funptr);
extern void disable_int();
extern void enable_int();
extern void irq_mask(unsigned char irq, unsigned char mask);
extern void irq1_ps2_port1();
extern void irq1_ps2_port2();
void wait_for_in_clear()
{
while ((inb(0x64) & 0x2) > 0) {}
}
void wait_for_out_data()
{
int z = 0;
while ((inb(0x64) & 0x1) == 0) {
z++;
if (z == 10000) {
break;
}
}
}
void ps2wr2(unsigned char thing)
{
wait_for_in_clear();
outb(0x64, 0xd4);
wait_for_in_clear();
outb(0x60, thing);
int z = 0;
while ((inb(0x64) & 0x1) == 0) {
z++;
if (z == 10000) {
return;
}
}
//wait_for_out_data();
if (inb(0x60) != 0xfa) {
printk("ps/2 mouse fail: not 0xfa! (was writing ");
printki(thing);
printk(")\n");
}
}
void some_ps2_stuff()
{
// TODO: init usb (disable usb-ps/2 legacy support)
// TODO: ensure there actually is a PS/2 controller (acpi?)
printk("---\n");
printk("PS/2: setup...\n");
disable_int();
// disable ps/2 ports
//
// - send 0xad
outb(0x64, 0xad);
// - send 0xa7
outb(0x64, 0xa7);
// flush the output buffer
// - read a bunch
while ((inb(0x64) & 1) == 1)
{
inb(0x60);
}
/*printk("flushed ");
printki(i);
printk(" bytes from ps/2 buffer\n");*/
// set CCB
//printk("set ccb\n");
// (disable all IRQs & disable translation (clear bits 0, 1 and 6))
outb(0x64, 0x20);
unsigned char ccb = inb(0x60);
// disable irqs
ccb &= 0xfc;
// disable PS/2 port 1 translation (we don't do mode 1 keybs anyway)
ccb &= 0xbf;
// actually set
outb(0x64, 0x60);
outb(0x60, ccb);
char singlechannel = 0;
if ((ccb & 0x20) > 0) {
printk("PS/2: might be a two-port ps/2 controller\n");
} else {
printk("PS/2: can't be a two-port ps/2 controller\n");
singlechannel = 1;
}
// perform controller self test
// - send 0xaa
// - ensure response is 0x55
printk("PS/2: controller self test result: ");
outb(0x64, 0xaa);
unsigned char response = inb(0x60);
if (response == 0x55) {
printk("OK\n");
} else if (response == 0xfc) {
printk("Fail\n");
// TODO: do something here
} else {
printk("???\n");
// TODO: do something here
}
// determine if there are 2 channels
if (!singlechannel) {
//printk("second-chance test if this ps/2 controller has only 1 port\n");
// enable second ps/2 port & test if bit 5 of ccb is clear
outb(0x64, 0xa8);
outb(0x64, 0x20);
char ccb2 = inb(0x60);
if ((ccb2 & 0x20) > 0) {
printk("PS/2: this is actually a single channel controller\n");
singlechannel = 1;
}
}
// perform controller self-test
/*outb(0x64, 0xaa);
printk("ps/2 controller self test: ");
if (inb(0x60) == 0x55) {
printk("OK\n");
} else {
printk("fail\n");
}*/
// perform interface tests
// - send 0xab, check result
printk("PS/2: controller port 1 test: ");
outb(0x64, 0xab);
char port1result = inb(0x60);
if (port1result != 0x00) {
printki(port1result);
printk("\n");
} else {
printk("OK\n");
}
// - send 0xa9, check result
char port2result = 0;
if (!singlechannel) {
printk("PS/2: controller port 2 test: ");
outb(0x64, 0xa9);
port2result = inb(0x60);
if (port2result != 0x00) {
printki(port2result);
printk("\n");
} else {
printk("OK\n");
}
}
// enable ports again
// - enable port 1
if (port1result == 0) {
printk("PS/2: enable port 1\n");
outb(0x64, 0xae);
}
// - enable port 2
/*if (!singlechannel && (port2result == 0)) {
printk("PS/2: enable port 2\n");
outb(0x64, 0xa8);
}*/
// - enable interrupts by modding ccb
//printk("enabling interrupts on ps/2\n");
outb(0x64, 0x20);
unsigned char ccb2 = inb(0x60);
//printk("ccb was ");
//printki(ccb2);
//printk("\n");
if (port1result == 0) {
ccb2 |= 0x1;
}
if (!singlechannel && (port2result == 0)) {
ccb2 |= 0x2;
}
//printk("ccb is now going to be ");
//printki(ccb2);
//printk("\n");
outb(0x64, 0x60);
outb(0x60, ccb2);
// reset devices:
// - ensure 'input buffer' clear
printk("PS/2: resetting attached devices\n");
if (port1result == 0) {
//printk("wait for ps/2 'input buffer' clear\n");
int z = 0;
while ((inb(0x64) & 0x2) > 0) {
// ...
z++;
}
/*printki(z);
printk(" time(s) to clear\n");
printk("send reset to ps/2 port 1\n");*/
outb(0x60, 0xff); // send reset cmd to kbd
//printk("wait for ps/2 output buffer mark\n");
while ((inb(0x64) & 0x1) == 0) {
// ...
}
unsigned char resetres1;
while (1) {
resetres1 = inb(0x60);
printk("PS/2: port 1 device r&sst result: ");
if (resetres1 == 0xfa) {
if ((inb(0x64) & 0x1) == 1) {
resetres1 = inb(0x60);
}
}
if (resetres1 == 0xaa) {
printk("OK");
break;
} else if ((resetres1 == 0xfc) || (resetres1 == 0xfd)) {
printk("self-test failed");
break;
} else if (resetres1 == 0xfe) {
printk("(resend)");
// TODO: resend
} else {
printk("?? ");
printki(resetres1);
break;
}
}
printk("\n");
// detect ps/2 port 1 device
// - disable scanning
while (1) {
wait_for_in_clear();
outb(0x60, 0xf5);
wait_for_out_data();
if (inb(0x60) == 0xfa) {
break;
}
}
// - send identify 0xf2
while (1) {
wait_for_in_clear();
outb(0x60, 0xf2);
wait_for_out_data();
char rlybrk;
if (inb(0x60) == 0xfa) {
// todo: consider 0 byte case? meh...
wait_for_out_data();
unsigned char ps2id1;
unsigned char ps2id12;
ps2id1 = inb(0x60);
rlybrk = 1;
switch (ps2id1) {
case 0x00:
printk("ps/2 port 1: standard ps/2 mouse\n");
break;
case 0x03:
printk("ps/2 port 1: mouse with scroll wheel\n");
break;
case 0x04:
printk("ps/2 port 1: 5-button mouse\n");
break;
case 0xab:
ps2id12 = inb(0x60);
if ((ps2id12 == 0x41) || (ps2id12 == 0xc1)) {
printk("ps/2 port 1: mf2 kbd with translation enabled\n");
} else if (ps2id12 == 0x83) {
printk("ps/2 port 1: mf2 kbd\n");
} else {
rlybrk = 0;
}
break;
default:
rlybrk = 0;
break;
}
}
if (rlybrk == 1) {
break;
}
}
// enable scanning
outb(0x60, 0xf4);
wait_for_out_data();
inb(0x60);
// just get keys. temp...
/*printk("\nsay wait keys\n");
wait_for_in_clear();
outb(0x60, 0xf4);
//outb(0x60, 0xee);
//printk("ok... let's go!\n");
wait_for_out_data();
inb(0x60);*/
/*while (1) {
wait_for_out_data();
printki(inb(0x60));
printk(" ");
}*/
set_int(0x21, &irq1_ps2_port1);
irq_mask(1, 0);
}
wait_for_in_clear();
outb(0x64, 0xad);
// TODO; reset port 2 device.
if (!singlechannel && (port2result == 0)) {
// disable port 1
printk("ps/2 mouse disable port 1\n");
wait_for_in_clear();
outb(0x64, 0xad);
// ack read??? (OPTIONAL)
printk("ps/2 mouse ack read\n");
//wait_for_out_data();
//inb(0x60);
// 'compaq voodoo' to enable irq12
printk("ps/2 compaq voodoo\n");
wait_for_in_clear();
outb(0x64, 0x20);
wait_for_out_data();
unsigned char uuu = inb(0x60);
wait_for_in_clear();
outb(0x64, 0x60);
uuu = uuu & 0xdf;
uuu = uuu | 0x2;
wait_for_in_clear();
outb(0x60, uuu);
// enable port 2
wait_for_in_clear();
outb(0x64, 0xa8);
// restore defaults
printk("ps/2 mouse restore defaults\n");
ps2wr2(0xf6);
// set sample rate
printk("ps/2 sample rate\n");
ps2wr2(0xf3);
ps2wr2(200);
// set resolution
printk("ps/2 set resolution\n");
ps2wr2(0xe8);
ps2wr2(3);
printk("ps/2 set scaling 1:1\n");
ps2wr2(0xe6);
printk("ps/2 mouse enable\n");
ps2wr2(0xf4);
//printk("wait for ps/2 'input buffer' clear\n");
/*while ((inb(0x64) & 0x2) > 0) {
// ...
}
wait_for_in_clear();
outb(0x64, 0xd4);
wait_for_in_clear();
outb(0x60, 0xff); // send reset cmd to kbd
while ((inb(0x64) & 0x1) == 0) {
// ...
}
unsigned char resetres1;
while (1) {
resetres1 = inb(0x60);
printk("PS/2: port 2 device r&sst result: ");
if (resetres1 == 0xfa) {
if ((inb(0x64) & 0x1) == 1) {
resetres1 = inb(0x60);
}
}
if (resetres1 == 0xaa) {
printk("OK");
break;
} else if ((resetres1 == 0xfc) || (resetres1 == 0xfd)) {
printk("self-test failed");
break;
} else if (resetres1 == 0xfe) {
printk("(resend)");
// TODO: resend
} else {
printk("?? ");
printki(resetres1);
break;
}
}
printk("\n");
int xyzxyz = 0;
while (1) {
if ((inb(0x64) & 0x1) == 1) {
printk("magic ");
printki(inb(0x60));
printk("\n");
}
if (xyzxyz >= 1000) {
break;
}
xyzxyz++;
}
// detect ps/2 port 2 device
// - disable scanning
printk("PS/2 port 2 disable scanning\n");
while (1) {
wait_for_in_clear();
outb(0x64, 0xd4);
wait_for_in_clear();
outb(0x60, 0xf5);
wait_for_out_data();
if (inb(0x60) == 0xfa) {
break;
}
}
printk("PS/2 port 2 sending identify\n");
// - send identify 0xf2
unsigned char idret;
while (1) {
printk("waiting for in clear 0\n");
wait_for_in_clear();
outb(0x64, 0xd4);
printk("waiting for in clear 1\n");
wait_for_in_clear();
outb(0x60, 0xf2);
printk("waiting for out data\n");
wait_for_out_data();
char rlybrk;
idret = inb(0x60);
if (idret == 0xfa) {
// todo: consider 0 byte case? meh...
printk("getting next byte\n");
wait_for_out_data();
unsigned char ps2id1;
unsigned char ps2id12;
ps2id1 = inb(0x60);
// another 0xfa??? wtf vbox pls
if (ps2id1 == 0xfa) {
wait_for_out_data();
ps2id1 = inb(0x60);
}
rlybrk = 1;
switch (ps2id1) {
case 0x00:
printk("ps/2 port 2: standard ps/2 mouse\n");
break;
case 0x03:
printk("ps/2 port 2: mouse with scroll wheel\n");
break;
case 0x04:
printk("ps/2 port 2: 5-button mouse\n");
break;
case 0xab:
printk("getting next byte pls\n");
ps2id12 = inb(0x60);
if ((ps2id12 == 0x41) || (ps2id12 == 0xc1)) {
printk("ps/2 port 2: mf2 kbd with translation enabled\n");
} else if (ps2id12 == 0x83) {
printk("ps/2 port 2: mf2 kbd\n");
} else {
rlybrk = 0;
}
break;
default:
printk("ps2id1 ");
printki(ps2id1);
printk("\n");
rlybrk = 0;
break;
}
} else {
printk("got idret ");
printki(idret);
printk("\n");
break;
}
if (rlybrk == 1) {
break;
}
}
// tell mouse to use default settings (apparently)
wait_for_in_clear();
outb(0x64, 0xd4);
wait_for_in_clear();
outb(0x60, 0xf6);
wait_for_out_data();
printki(inb(0x60));
printk("\n");
// set stream mode (apparently)
wait_for_in_clear();
outb(0x64, 0xd4);
wait_for_in_clear();
outb(0x60, 0xea);
wait_for_out_data();
printki(inb(0x60));
printk("\n");*/
// enable scanning
/*printk("enabling scanning\n");
wait_for_in_clear();
outb(0x64, 0xd4);
wait_for_in_clear();
outb(0x60, 0xf4);
wait_for_out_data();
printk("enable scan response: ");
printki(inb(0x60));
printk("\n");*/
//printk("PS/2: TODO: set port 2 irq\n");
set_int(0x20 + 12, &irq1_ps2_port2);
// spam-a-lot
/*int yy;
for (yy = 0x20 + 5; yy < 0x40; yy++) {
set_int(yy, &irq1_ps2_port2);
}*/
//irq_mask(12, 0);
// set ps/2 controller output port???
//outb(0xd1, 0x46);
// re-enable port 1
wait_for_in_clear();
outb(0x64, 0xae);
// disable port 2 (TEMP, BECAUSE MOUSE DOESN'T WORK!!)
printk("disabling ps/2 mouse (vbox bug?)\n");
wait_for_in_clear();
outb(0x64, 0xa7);
/*while (1) {
wait_for_out_data();
printki(inb(0x60));
printk(" ");
}*/
}
enable_int();
}