#include "keyboard.h"
+#include "devfs.h"
#include "uart_console.h"
#include <stddef.h>
}
}
+/* --- Raw scancode ring buffer for /dev/kbd --- */
+
+#define SCAN_BUF_SIZE 256
+static volatile uint32_t scan_head = 0;
+static volatile uint32_t scan_tail = 0;
+static uint8_t scan_buf[SCAN_BUF_SIZE];
+static spinlock_t scan_lock = {0};
+
+static void scan_push(uint8_t sc) {
+ uint32_t next = (scan_head + 1U) % SCAN_BUF_SIZE;
+ if (next == scan_tail) {
+ scan_tail = (scan_tail + 1U) % SCAN_BUF_SIZE;
+ }
+ scan_buf[scan_head] = sc;
+ scan_head = next;
+}
+
+static void hal_scan_bridge(uint8_t scancode) {
+ uintptr_t flags = spin_lock_irqsave(&scan_lock);
+ scan_push(scancode);
+ spin_unlock_irqrestore(&scan_lock, flags);
+}
+
+static uint32_t kbd_dev_read(fs_node_t* node, uint32_t offset, uint32_t size, uint8_t* buffer) {
+ (void)node; (void)offset;
+ if (!buffer || size == 0) return 0;
+
+ uintptr_t flags = spin_lock_irqsave(&scan_lock);
+ uint32_t count = 0;
+ while (count < size && scan_tail != scan_head) {
+ buffer[count++] = scan_buf[scan_tail];
+ scan_tail = (scan_tail + 1U) % SCAN_BUF_SIZE;
+ }
+ spin_unlock_irqrestore(&scan_lock, flags);
+ return count;
+}
+
+static fs_node_t g_dev_kbd_node;
+
void keyboard_init(void) {
uart_print("[KBD] Initializing Keyboard Driver...\n");
spinlock_init(&kbd_lock);
+ spinlock_init(&scan_lock);
kbd_head = 0;
kbd_tail = 0;
+ scan_head = 0;
+ scan_tail = 0;
kbd_waiter = NULL;
hal_keyboard_init(hal_kbd_bridge);
+ hal_keyboard_set_scancode_cb(hal_scan_bridge);
+}
+
+void keyboard_register_devfs(void) {
+ memset(&g_dev_kbd_node, 0, sizeof(g_dev_kbd_node));
+ strcpy(g_dev_kbd_node.name, "kbd");
+ g_dev_kbd_node.flags = FS_CHARDEVICE;
+ g_dev_kbd_node.inode = 21;
+ g_dev_kbd_node.read = &kbd_dev_read;
+ devfs_register_device(&g_dev_kbd_node);
}
void keyboard_set_callback(keyboard_callback_t callback) {
#include "io.h"
static hal_keyboard_char_cb_t g_cb = 0;
+static hal_keyboard_scan_cb_t g_scan_cb = 0;
static char scancode_map[128] = {
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b',
if (status & 0x01) {
uint8_t scancode = inb(0x60);
+ /* Raw scancode callback (key press and release) */
+ if (g_scan_cb) {
+ g_scan_cb(scancode);
+ }
+
if (!(scancode & 0x80)) {
if (scancode < 128) {
char c = scancode_map[scancode];
g_cb = cb;
register_interrupt_handler(33, kbd_irq);
}
+
+void hal_keyboard_set_scancode_cb(hal_keyboard_scan_cb_t cb) {
+ g_scan_cb = cb;
+}
#else
void hal_keyboard_init(hal_keyboard_char_cb_t cb) {
(void)cb;
}
+
+void hal_keyboard_set_scancode_cb(hal_keyboard_scan_cb_t cb) {
+ (void)cb;
+}
#endif