]>
git.dujemihanovic.xyz Git - nameless-os.git/blob - kernel/drivers/input/ps2.c
ab4be5c5bbcbb9338ed7a545f59dd9517471605f
5 #include <irq/interrupt.h>
7 static int was_released
= 0, is_caps
= 0;
11 uint8_t scancode
= inb(PS2_DATA_PORT
);
17 if (scancode
== 0xf0) {
19 uint8_t scancode
= inb(PS2_DATA_PORT
);
20 if (scancode
== 0x12) {
25 if (scancode
== 0x12) {
30 kprintc(scancodes
[scancode
], 0);
32 kprintc(scancodes
[scancode
] - ('a'-'A'), 0);
40 uint8_t ccb
, is_2channel
, port_1_test
, port_2_test
;
42 kprint("ps2: Begin initializing PS/2 controller\n", 0);
43 outb(PS2_CMD_STS_PORT
, PS2_CMD_PORT_2_DISABLE
);
45 outb(PS2_CMD_STS_PORT
, PS2_CMD_PORT_1_DISABLE
);
49 outb(PS2_CMD_STS_PORT
, PS2_CMD_READ_CCB
);
51 ccb
= inb(PS2_DATA_PORT
);
58 outb(PS2_CMD_STS_PORT
, PS2_CMD_WRITE_CCB
);
60 outb(PS2_DATA_PORT
, ccb
);
63 outb(PS2_CMD_STS_PORT
, PS2_CMD_CONTROLLER_TEST
);
65 if (inb(PS2_DATA_PORT
) != PS2_CONTROLLER_GOOD
) {
66 kprint("ps2: Controller self test failed, exiting!\n", 0);
71 outb(PS2_CMD_STS_PORT
, PS2_CMD_PORT_2_ENABLE
);
73 outb(PS2_CMD_STS_PORT
, PS2_CMD_READ_CCB
);
75 ccb
= inb(PS2_DATA_PORT
);
77 if (ccb
& PORT_2_CLK
!= PORT_2_CLK
) {
79 kprint("ps2: Controller is single-channel\n", 0);
82 kprint("ps2: Controller is dual-channel\n", 0);
84 outb(PS2_CMD_STS_PORT
, PS2_CMD_PORT_2_DISABLE
);
88 outb(PS2_CMD_STS_PORT
, PS2_CMD_PORT_1_TEST
);
91 if (inb(PS2_DATA_PORT
) != 0) {
93 kprint("ps2: Port 1 test failed!\n", 0);
95 kprint("ps2: No functional port, exiting!\n", 0);
98 } else port_1_test
= 1;
102 outb(PS2_CMD_STS_PORT
, PS2_CMD_PORT_2_TEST
);
105 if (inb(PS2_DATA_PORT
) != 0) {
107 kprint("ps2: Port 2 test failed!", 0);
109 kprint("ps2: No functional port, exiting!\n", 0);
112 } else port_2_test
= 1;
115 if (port_1_test
) { ps2_input_wait(); outb(PS2_CMD_STS_PORT
, PS2_CMD_PORT_1_ENABLE
); }
116 if (port_2_test
) { ps2_input_wait(); outb(PS2_CMD_STS_PORT
, PS2_CMD_PORT_2_ENABLE
); }
118 int dev_1_test
, dev_2_test
;
125 outb(PS2_DATA_PORT
, PS2_DEV_RESET
);
127 resp
= inb(PS2_DATA_PORT
);
128 } while (resp
== RESEND
);
130 if (resp
== SELF_TEST_BAD
|| resp
== SELF_TEST_BAD_2
) {
132 kprint("ps2: Port 1 device self test failed!\n", 0);
134 kprint("ps2: No functioning devices, exiting!\n", 0);
137 } else { dev_1_test
= 1; kprint("ps2: Port 1 device test successful\n", 0); }
148 outb(PS2_CMD_STS_PORT
, PS2_CMD_PORT_2_WRITE
);
150 outb(PS2_DATA_PORT
, PS2_DEV_RESET
);
152 resp
= inb(PS2_DATA_PORT
);
153 } while (resp
== RESEND
);
155 if (resp
== SELF_TEST_BAD
|| resp
== SELF_TEST_BAD_2
) {
157 kprint("ps2: Port 2 device self test failed!\n", 0);
159 kprint("ps2: No functioning devices, exiting!\n", 0);
162 } else { dev_2_test
= 1; kprint("ps2: Port 2 device test successful\n", 0); }
168 outb(PS2_CMD_STS_PORT
, PS2_CMD_READ_CCB
);
170 ccb
= inb(PS2_DATA_PORT
);
172 SET(ccb
, PORT_1_IRQ
);
175 outb(PS2_CMD_STS_PORT
, PS2_CMD_WRITE_CCB
);
177 outb(PS2_DATA_PORT
, ccb
);
179 if (dev_1_test
) register_interrupt(33, &ps2_keyb_handler
);
181 if (dev_2_test
&& dev_1_test
) return 1;
185 void ps2_output_wait()
188 status
= inb(PS2_CMD_STS_PORT
);
190 while (!IS_SET(status
, OUT_STATUS
)) status
= inb(PS2_CMD_STS_PORT
);
194 void ps2_input_wait()
197 status
= inb(PS2_CMD_STS_PORT
);
199 while (IS_SET(status
, IN_STATUS
)) status
= inb(PS2_CMD_STS_PORT
);