toaruos/kernel/devices/kbd.c

97 lines
1.7 KiB
C
Raw Normal View History

/* vim: tabstop=4 shiftwidth=4 noexpandtab
*
* Low-level keyboard interrupt driver.
*
2012-01-25 23:50:30 +04:00
* Creates a device file (keyboard_pipe) that can be read
* to retreive keyboard events.
*
2011-04-15 06:02:44 +04:00
* Part of the ToAruOS Kernel
2012-01-25 23:50:30 +04:00
* Copyright 2011-2012 Kevin Lange
*
2012-01-25 23:50:30 +04:00
* TODO: Move this to a server
* TODO: Better handling of function keys
*/
2011-01-16 21:45:51 +03:00
#include <system.h>
#include <logging.h>
#include <fs.h>
#include <pipe.h>
#include <process.h>
2011-01-16 21:45:51 +03:00
2011-10-23 04:32:03 +04:00
#define KEY_UP_MASK 0x80
#define KEY_CODE_MASK 0x7F
#define KEY_CTRL_MASK 0x40
#define KEY_DEVICE 0x60
#define KEY_PENDING 0x64
#define KEYBOARD_NOTICES 0
#define KEYBOARD_IRQ 1
2011-04-15 06:02:44 +04:00
/* A bit-map to store the keyboard states */
2011-01-22 06:48:17 +03:00
struct keyboard_states {
uint32_t shift : 1;
uint32_t alt : 1;
uint32_t ctrl : 1;
} keyboard_state;
typedef void (*keyboard_handler_t)(int scancode);
fs_node_t * keyboard_pipe;
2011-01-22 06:48:17 +03:00
2011-10-31 10:48:03 +04:00
extern uint8_t mouse_cycle;
2011-01-16 21:45:51 +03:00
void
keyboard_handler(
struct regs *r
) {
unsigned char scancode;
keyboard_wait();
2011-10-23 04:32:03 +04:00
scancode = inportb(KEY_DEVICE);
irq_ack(KEYBOARD_IRQ);
2011-10-31 10:48:03 +04:00
putch(scancode);
2011-01-16 21:45:51 +03:00
}
2012-01-25 23:50:30 +04:00
/*
* Install the keyboard driver and initialize the
* pipe device for userspace.
*/
2011-01-16 21:45:51 +03:00
void
keyboard_install() {
2011-12-27 05:23:58 +04:00
blog("Initializing PS/2 keyboard driver...");
LOG(INFO, "Initializing PS/2 keyboard driver");
2012-01-25 23:50:30 +04:00
/* Create a device pipe */
keyboard_pipe = make_pipe(128);
2012-02-17 00:31:40 +04:00
current_process->fds->entries[0] = keyboard_pipe;
2012-01-25 23:50:30 +04:00
/* Install the interrupt handler */
irq_install_handler(KEYBOARD_IRQ, keyboard_handler);
2011-12-27 05:23:58 +04:00
bfinish(0);
2011-01-16 21:45:51 +03:00
}
2012-01-25 23:50:30 +04:00
/*
* Wait on the keyboard.
*/
2011-01-16 21:45:51 +03:00
void
keyboard_wait() {
2011-10-23 04:32:03 +04:00
while(inportb(KEY_PENDING) & 2);
2011-01-16 21:45:51 +03:00
}
/*
2012-01-25 23:50:30 +04:00
* Add a character to the device buffer.
*/
void
putch(
unsigned char c
) {
uint8_t buf[2];
buf[0] = c;
buf[1] = '\0';
write_fs(keyboard_pipe, 0, 1, buf);
}