第一套键盘扫描码的状态机 (#216)

第一套键盘扫描码的状态机
---------

Co-authored-by: guanjinquan <1666320330@qq.com>
Co-authored-by: longjin <longjin@RinGoTek.cn>
This commit is contained in:
Gou Ngai
2023-03-30 18:19:02 +08:00
committed by GitHub
parent 676b8ef62e
commit 5fb12ce447
9 changed files with 539 additions and 26 deletions

View File

@ -14,6 +14,7 @@ static struct kfifo_t kb_buf;
// 缓冲区等待队列
static wait_queue_node_t ps2_keyboard_wait_queue;
extern void ps2_keyboard_register(struct vfs_file_operations_t *);
extern void ps2_keyboard_parse_keycode(uint8_t input);
// 缓冲区读写锁
static spinlock_t ps2_kb_buf_rw_lock;
@ -143,7 +144,7 @@ struct vfs_file_operations_t ps2_keyboard_fops =
void ps2_keyboard_handler(ul irq_num, ul buf_vaddr, struct pt_regs *regs)
{
unsigned char x = io_in8(PORT_PS2_KEYBOARD_DATA);
ps2_keyboard_parse_keycode((uint8_t)x);
uint8_t count = kfifo_in((struct kfifo_t *)buf_vaddr, &x, sizeof(unsigned char));
if (count == 0)
{
@ -152,6 +153,7 @@ void ps2_keyboard_handler(ul irq_num, ul buf_vaddr, struct pt_regs *regs)
}
wait_queue_wakeup(&ps2_keyboard_wait_queue, PROC_UNINTERRUPTIBLE);
}
/**
* @brief 初始化键盘驱动程序的函数
@ -216,4 +218,5 @@ void ps2_keyboard_exit()
{
irq_unregister(PS2_KEYBOARD_INTR_VECTOR);
kfifo_free_alloc(&kb_buf);
}

View File

@ -2,14 +2,13 @@
#include <common/glib.h>
#define PS2_KEYBOARD_INTR_VECTOR 0x21 // 键盘的中断向量号
#define PS2_KEYBOARD_INTR_VECTOR 0x21 // 键盘的中断向量号
// 定义键盘循环队列缓冲区大小为100bytes
#define ps2_keyboard_buffer_size 8
#define KEYBOARD_CMD_RESET_BUFFER 1
#define PORT_PS2_KEYBOARD_DATA 0x60
#define PORT_PS2_KEYBOARD_STATUS 0x64
#define PORT_PS2_KEYBOARD_CONTROL 0x64
@ -30,11 +29,8 @@
#define wait_ps2_keyboard_read() while (io_in8(PORT_PS2_KEYBOARD_STATUS) & PS2_KEYBOARD_FLAG_OUTBUF_FULL)
// #define wait_ps2_keyboard_read() (1)
extern struct vfs_file_operations_t ps2_keyboard_fops;
/**
* @brief 初始化键盘驱动程序的函数
*
@ -46,16 +42,3 @@ void ps2_keyboard_init();
*
*/
void ps2_keyboard_exit();
/**
* @brief 解析键盘扫描码
*
*/
void ps2_keyboard_analyze_keycode();
/**
* @brief 从缓冲队列中获取键盘扫描码
* @return 键盘扫描码
* 若缓冲队列为空则返回-1
*/
int ps2_keyboard_get_scancode();

View File

@ -8,13 +8,18 @@ use crate::{
vfs::{core::generate_inode_id, file::FileMode, FileType, IndexNode, Metadata, PollStatus},
},
include::bindings::bindings::{vfs_file_operations_t, vfs_file_t, vfs_index_node_t},
libs::rwlock::RwLock,
time::TimeSpec, syscall::SystemError,
libs::{keyboard_parser::TypeOneFSM, rwlock::RwLock, spinlock::SpinLock},
syscall::SystemError,
time::TimeSpec,
};
#[derive(Debug)]
pub struct LockedPS2KeyBoardInode(RwLock<PS2KeyBoardInode>, AtomicI32); // self.1 用来记录有多少个文件打开了这个inode
lazy_static! {
static ref PS2_KEYBOARD_FSM: SpinLock<TypeOneFSM> = SpinLock::new(TypeOneFSM::new());
}
#[derive(Debug)]
pub struct PS2KeyBoardInode {
/// uuid 暂时不知道有什么用x
@ -122,7 +127,10 @@ impl IndexNode for LockedPS2KeyBoardInode {
return Ok(());
}
fn close(&self, _data: &mut crate::filesystem::vfs::FilePrivateData) -> Result<(), SystemError> {
fn close(
&self,
_data: &mut crate::filesystem::vfs::FilePrivateData,
) -> Result<(), SystemError> {
let prev_ref_count = self.1.fetch_sub(1, core::sync::atomic::Ordering::SeqCst);
if prev_ref_count == 1 {
// 最后一次关闭,需要释放
@ -167,3 +175,10 @@ impl IndexNode for LockedPS2KeyBoardInode {
return Err(SystemError::ENOTSUP);
}
}
#[allow(dead_code)]
#[no_mangle]
/// for test
pub extern "C" fn ps2_keyboard_parse_keycode(input: u8) {
PS2_KEYBOARD_FSM.lock().parse(input);
}