Skip to content

Commit f6cafaf

Browse files
committed
Move PS2 controller driver out of keyboard
1 parent 6c07680 commit f6cafaf

File tree

15 files changed

+302
-127
lines changed

15 files changed

+302
-127
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ include $(SRC_KERNEL)/syscall/Makefile.mk
149149
include $(SRC_DRIVERS)/disk/Makefile.mk
150150
include $(SRC_DRIVERS)/keyboard/Makefile.mk
151151
include $(SRC_DRIVERS)/pic/Makefile.mk
152+
include $(SRC_DRIVERS)/ps2/Makefile.mk
152153
include $(SRC_DRIVERS)/display/Makefile.mk
153154
include $(SRC_DRIVERS)/display/vga/Makefile.mk
154155

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ fork() | TicTacToe game
3131
| ![gif](https://user-images.githubusercontent.com/9819066/132988756-de1b7770-430d-40df-a7c9-593cd752b20a.gif) |
3232

3333

34-
3534
### Boot OS
3635

3736
#### How to get boot image?

include/fuzzy/drivers/port.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
#define PORT_PIT_DATA2 0x42
1313
#define PORT_PIT_CMD 0x43
1414

15+
#define PORT_PS2_DATA 0x60
16+
#define PORT_PS2_CMD 0x64
17+
#define PORT_PS2_STATUS 0x64
18+
19+
1520
static inline void outb(uint16_t port, uint8_t data) {
1621
__asm__ volatile(
1722
"outb %0, %1 \n"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
4+
void ps2_keyboard_init();
5+
6+
char ps2_keyboard_get_key_pressed_blocking();
7+
char ps2_keyboard_get_key_pressed_poll();
8+
int ps2_keyboard_get_kbhit();

include/fuzzy/drivers/ps2/ps2.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include <stddef.h>
4+
5+
void ps2_init();
6+
7+
uint8_t ps2_read_data();
8+
void ps2_write_port1(uint8_t byte);
9+
void ps2_controller_wait_for_empty_input();
10+
void ps2_controller_wait_for_full_output();
11+
12+
void interrupt_register_0x21_0x2C_irq1_ir12_keyboard_mouse();
13+
void irq1_handler();
14+
void irq12_handler();
15+

include/fuzzy/kernel/interrupts/interrupts.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
#define IDT_IRQ_OFFSET 0x20
1111

1212
// Used by /usr/lib/process.c
13-
#define IDT_IRQ0_PIC (IDT_IRQ_OFFSET+0)
13+
#define IDT_IRQ0_PIC (IDT_IRQ_OFFSET+0)
14+
#define IDT_IRQ1_KEYBOARD (IDT_IRQ_OFFSET+1)
15+
#define IDT_IRQ12_MOUSE (IDT_IRQ_OFFSET+12)
1416
// Used by /usr/lib/sys/syscall.asm
1517
#define IDT_SYSCALL 0x32
1618

src/drivers/keyboard/keyboard.c

Lines changed: 3 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -183,128 +183,11 @@ int keyboard_get_kbhit() {
183183
}
184184

185185
void keyboard_init() {
186-
unsigned char original_colors = get_color_fgbg();
187-
sleep_mini(3000000);
188-
189186
unsigned char out;
190-
// disable PS/2 first port
191-
ps2_controller_send_command(0XAD, 0);
192-
// disable PS/2 second port
193-
ps2_controller_send_command(0XA7, 0);
194-
// flush output buffer
195-
wait_for_status_flag(STATUS_OUTPUT_BUFFER, 0);
196-
197-
out = ps2_controller_send_command(0X20, 1);
198-
out = out&(~0b1000011);
199-
int dual_ps2_controller = 0;
200-
if (out & (0b10000)) {
201-
dual_ps2_controller = 1;
202-
}
203-
print_log("Init: Maybe dual PS2 Controller", dual_ps2_controller);
204-
ps2_controller_send_command_with_data(0x60, out, 0);
205-
206-
207-
out = ps2_controller_send_command(0XAA, 1);
208-
if (out != 0x55) {
209-
PANIC(out, "PS2 controller self test failed.");
210-
}
211-
212-
if(dual_ps2_controller) {
213-
ps2_controller_send_command(0XA8, 0);
214-
out = ps2_controller_send_command(0X20, 1);
215-
if (!(out & (0b10000))) {
216-
dual_ps2_controller = 0;
217-
print_log("Init: Enabling PS2 port failed, thus disabling dual_ps2_controller");
218-
} else {
219-
// disabling second port again.
220-
ps2_controller_send_command(0xA7, 0);
221-
}
222-
}
223-
224-
out = ps2_controller_send_command(0XAB, 1);
225-
if (out != 0x00) {
226-
PANIC(out, "PS2 first port test failed.");
227-
}
228-
if(dual_ps2_controller) {
229-
out = ps2_controller_send_command(0XA9, 1);
230-
if (out != 0x00) {
231-
PANIC(out, "PS2 second port test failed.");
232-
}
233-
}
234-
235-
print_log("Init: Dual PS2 Controller: %d", dual_ps2_controller);
236-
237-
// enable first port
238-
ps2_controller_send_command(0xAE, 0);
239-
if(dual_ps2_controller) {
240-
// enable second port
241-
ps2_controller_send_command(0xA8, 0);
242-
}
243-
187+
// disable scanning
244188
out = write_to_ps2_first_port(0xF5, 1);
245189
if (out != 0xFA) {
246-
PANIC(out, "disable scanning failed.");
247-
}
248-
249-
// enable interrupts
250-
out = ps2_controller_send_command(0X20, 1);
251-
out |= 0b11;
252-
ps2_controller_send_command_with_data(0x60, out, 0);
253-
254-
255-
// reset device
256-
out = write_to_ps2_first_port(0xFF, 1);
257-
if (out != 0xFA) {
258-
PANIC(out, "reset ps/2 first failed");
259-
}
260-
out = read_data_reply();
261-
if (out != 0xAA) {
262-
PANIC(out, "reset ps/2 first port, keyboard self test failed");
263-
}
264-
265-
if(dual_ps2_controller) {
266-
// reset second port
267-
out = write_to_ps2_second_port(0xFF, 1);
268-
if (out != 0xFA) {
269-
PANIC(out, "reset ps/2 second failed");
270-
}
271-
out = read_data_reply();
272-
if (out != 0xAA) {
273-
PANIC(out, "reset ps/2 second port, mouse self test failed");
274-
}
275-
}
276-
277-
// detect device type
278-
out = write_to_ps2_first_port(0xF2, 1);
279-
if (out != 0xFA) {
280-
PANIC(out, "device identity failed.");
281-
}
282-
unsigned char dev_d0 = read_data_reply();
283-
unsigned char dev_d1 = read_data_reply();
284-
285-
// Testing for device 0xAB 0x83: MF2 keyboard
286-
if(dev_d0 != 0xAB) {
287-
PANIC(dev_d0, "testing for device id 0xAB 0x83, found xx 0x83.");
288-
}
289-
if(dev_d1 != 0x83) {
290-
PANIC(dev_d1, "testing for device id 0xAB 0x83, found 0xAB xx.");
291-
}
292-
293-
// Caps
294-
write_to_ps2_first_port(0xED, 0);
295-
out = write_to_ps2_first_port(7, 1);
296-
if (out != 0xFA) {
297-
PANIC(out, "caps failed");
298-
}
299-
// get scan code
300-
write_to_ps2_first_port(0xF0, 0);
301-
out = write_to_ps2_first_port(0, 1);
302-
if (out != 0xFA) {
303-
PANIC(out, "failed to get scan code");
304-
} else {
305-
// Not yet working
306-
// out = read_data_reply();
307-
// PANIC(out, "got get scan code");
190+
PANIC(out, "disable scan failed");
308191
}
309192

310193
// set scan code
@@ -320,6 +203,5 @@ void keyboard_init() {
320203
PANIC(out, "scan failed");
321204
}
322205
keyboard_scanner_init();
323-
set_color_fgbg(original_colors);
324206
print_log("[keyboard init] done.");
325-
}
207+
}

src/drivers/ps2/Makefile.mk

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
$(SELF_BUILD_DIR)/%.o: $(SELF_SRC_DIR)/%.c $(BUILD_USR_INCLUDE_ALL)
2+
mkdir -p $(dir $@)
3+
$(KERNEL_CC) -c -o $@ \
4+
$<
5+
6+
$(SELF_BUILD_DIR)/%_asm.o: $(SELF_SRC_DIR)/%.asm
7+
mkdir -p $(dir $@)
8+
nasm -o $@ -f elf32 $<
9+
10+
$(SELF_BUILD_DIR)/libps2: $(SELF_BUILD_ALL_C) $(SELF_BUILD_ALL_ASM)
11+
ar rc $@ $^
12+

src/drivers/ps2/keyboard.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <fuzzy/drivers/ps2/ps2.h>
2+
#include <fuzzy/drivers/ps2/keyboard.h>
3+
4+
#include <drivers/keyboard/keyboard.h>
5+
6+
void ps2_keyboard_init() {
7+
keyboard_init();
8+
}
9+
10+
char ps2_keyboard_get_key_pressed_blocking() {
11+
return keyboard_get_key_pressed_blocking();
12+
}
13+
14+
char ps2_keyboard_get_key_pressed_poll() {
15+
return keyboard_get_key_pressed_poll();
16+
}
17+
18+
int ps2_keyboard_get_kbhit() {
19+
return keyboard_get_kbhit();
20+
}

src/drivers/ps2/ps2.asm

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[BITS 32]
2+
3+
global irq1_handler_low
4+
global irq12_handler_low
5+
6+
[SECTION .text]
7+
8+
irq1_handler_low:
9+
; TODO: send EOI
10+
iret
11+
12+
irq12_handler_low:
13+
; TODO: send EOI
14+
iret

0 commit comments

Comments
 (0)