1 #include "ps2_keyboard.h"
2 #include <driver/interrupt/apic/apic.h>
3 #include <mm/mm.h>
4 #include <mm/slab.h>
5 #include <common/printk.h>
6 #include <filesystem/vfs/VFS.h>
7 #include <common/wait_queue.h>
8 #include <common/spinlock.h>
9 #include <common/kfifo.h>
10
11 // 键盘输入缓冲区
12 static struct kfifo_t kb_buf;
13
14 // 缓冲区等待队列
15 static wait_queue_node_t ps2_keyboard_wait_queue;
16 extern void ps2_keyboard_register(struct vfs_file_operations_t *);
17
18 // 缓冲区读写锁
19 static spinlock_t ps2_kb_buf_rw_lock;
20
21 /**
22 * @brief 重置ps2键盘输入缓冲区
23 *
24 * @param kbp 缓冲区对象指针
25 */
ps2_keyboard_reset_buffer(struct kfifo_t * kbp)26 static void ps2_keyboard_reset_buffer(struct kfifo_t *kbp)
27 {
28 kfifo_reset(kbp);
29 }
30 struct apic_IO_APIC_RTE_entry entry;
31
32 hardware_intr_controller ps2_keyboard_intr_controller =
33 {
34 .enable = apic_ioapic_enable,
35 .disable = apic_ioapic_disable,
36 .install = apic_ioapic_install,
37 .uninstall = apic_ioapic_uninstall,
38 .ack = apic_ioapic_edge_ack,
39
40 };
41
42 /**
43 * @brief 打开键盘文件
44 *
45 * @param inode 所在的inode
46 * @param filp 文件指针
47 * @return long
48 */
ps2_keyboard_open(struct vfs_index_node_t * inode,struct vfs_file_t * filp)49 long ps2_keyboard_open(struct vfs_index_node_t *inode, struct vfs_file_t *filp)
50 {
51 ps2_keyboard_reset_buffer(&kb_buf);
52 return 0;
53 }
54
55 /**
56 * @brief 关闭键盘文件
57 *
58 * @param inode 所在的inode
59 * @param filp 文件指针
60 * @return long
61 */
ps2_keyboard_close(struct vfs_index_node_t * inode,struct vfs_file_t * filp)62 long ps2_keyboard_close(struct vfs_index_node_t *inode, struct vfs_file_t *filp)
63 {
64 ps2_keyboard_reset_buffer(&kb_buf);
65 return 0;
66 }
67
68 /**
69 * @brief 键盘io控制接口
70 *
71 * @param inode 所在的inode
72 * @param filp 键盘文件指针
73 * @param cmd 命令
74 * @param arg 参数
75 * @return long
76 */
ps2_keyboard_ioctl(struct vfs_index_node_t * inode,struct vfs_file_t * filp,uint64_t cmd,uint64_t arg)77 long ps2_keyboard_ioctl(struct vfs_index_node_t *inode, struct vfs_file_t *filp, uint64_t cmd, uint64_t arg)
78 {
79 switch (cmd)
80 {
81 case KEYBOARD_CMD_RESET_BUFFER:
82 ps2_keyboard_reset_buffer(&kb_buf);
83 break;
84
85 default:
86 break;
87 }
88 return 0;
89 }
90
91 /**
92 * @brief 读取键盘文件的操作接口
93 *
94 * @param filp 文件指针
95 * @param buf 输出缓冲区
96 * @param count 要读取的字节数
97 * @param position 读取的位置
98 * @return long 读取的字节数
99 */
ps2_keyboard_read(struct vfs_file_t * filp,char * buf,int64_t count,long * position)100 long ps2_keyboard_read(struct vfs_file_t *filp, char *buf, int64_t count, long *position)
101 {
102 // 缓冲区为空则等待
103 if (kfifo_empty(&kb_buf))
104 wait_queue_sleep_on(&ps2_keyboard_wait_queue);
105
106 count = (count > kb_buf.size) ? kb_buf.size : count;
107 return kfifo_out(&kb_buf, buf, count);
108 }
109
110 /**
111 * @brief 键盘文件写入接口(无作用,空)
112 *
113 * @param filp
114 * @param buf
115 * @param count
116 * @param position
117 * @return long
118 */
ps2_keyboard_write(struct vfs_file_t * filp,char * buf,int64_t count,long * position)119 long ps2_keyboard_write(struct vfs_file_t *filp, char *buf, int64_t count, long *position)
120 {
121 return 0;
122 }
123 /**
124 * @brief ps2键盘驱动的虚拟文件接口
125 *
126 */
127 struct vfs_file_operations_t ps2_keyboard_fops =
128 {
129 .open = ps2_keyboard_open,
130 .close = ps2_keyboard_close,
131 .ioctl = ps2_keyboard_ioctl,
132 .read = ps2_keyboard_read,
133 .write = ps2_keyboard_write,
134 };
135
136 /**
137 * @brief 键盘中断处理函数(中断上半部)
138 * 将数据存入缓冲区
139 * @param irq_num 中断向量号
140 * @param param 参数
141 * @param regs 寄存器信息
142 */
ps2_keyboard_handler(ul irq_num,ul buf_vaddr,struct pt_regs * regs)143 void ps2_keyboard_handler(ul irq_num, ul buf_vaddr, struct pt_regs *regs)
144 {
145 unsigned char x = io_in8(PORT_PS2_KEYBOARD_DATA);
146
147 uint8_t count = kfifo_in((struct kfifo_t *)buf_vaddr, &x, sizeof(unsigned char));
148 if (count == 0)
149 {
150 kwarn("ps2 keyboard buffer full.");
151 return;
152 }
153
154 wait_queue_wakeup(&ps2_keyboard_wait_queue, PROC_UNINTERRUPTIBLE);
155 }
156 /**
157 * @brief 初始化键盘驱动程序的函数
158 *
159 */
ps2_keyboard_init()160 void ps2_keyboard_init()
161 {
162
163 // ======= 初始化键盘循环队列缓冲区 ===========
164
165 // 初始化键盘循环队列缓冲区
166 kfifo_alloc(&kb_buf, ps2_keyboard_buffer_size, 0);
167
168 // ======== 初始化中断RTE entry ==========
169
170 entry.vector = PS2_KEYBOARD_INTR_VECTOR; // 设置中断向量号
171 entry.deliver_mode = IO_APIC_FIXED; // 投递模式:混合
172 entry.dest_mode = DEST_PHYSICAL; // 物理模式投递中断
173 entry.deliver_status = IDLE;
174 entry.trigger_mode = EDGE_TRIGGER; // 设置边沿触发
175 entry.polarity = POLARITY_HIGH; // 高电平触发
176 entry.remote_IRR = IRR_RESET;
177 entry.mask = MASKED;
178 entry.reserved = 0;
179
180 entry.destination.physical.reserved1 = 0;
181 entry.destination.physical.reserved2 = 0;
182 entry.destination.physical.phy_dest = 0; // 设置投递到BSP处理器
183
184 // ======== 初始化键盘控制器,写入配置值 =========
185 wait_ps2_keyboard_write();
186 io_out8(PORT_PS2_KEYBOARD_CONTROL, PS2_KEYBOARD_COMMAND_WRITE);
187 wait_ps2_keyboard_write();
188 io_out8(PORT_PS2_KEYBOARD_DATA, PS2_KEYBOARD_PARAM_INIT);
189 wait_ps2_keyboard_write();
190
191 // 执行一百万次nop,等待键盘控制器把命令执行完毕
192 for (int i = 0; i < 1000; ++i)
193 for (int j = 0; j < 1000; ++j)
194 nop();
195
196 wait_queue_init(&ps2_keyboard_wait_queue, NULL);
197 // 初始化键盘缓冲区的读写锁
198 spin_init(&ps2_kb_buf_rw_lock);
199
200 // 注册中断处理程序
201 irq_register(PS2_KEYBOARD_INTR_VECTOR, &entry, &ps2_keyboard_handler, (ul)&kb_buf, &ps2_keyboard_intr_controller, "ps/2 keyboard");
202
203 // 先读一下键盘的数据,防止由于在键盘初始化之前,由于按键被按下从而导致接收不到中断。
204 io_in8(PORT_PS2_KEYBOARD_DATA);
205 // 将设备挂载到devfs
206 ps2_keyboard_register(&ps2_keyboard_fops);
207
208 kinfo("ps/2 keyboard registered.");
209 }
210
211 /**
212 * @brief 键盘驱动卸载函数
213 *
214 */
ps2_keyboard_exit()215 void ps2_keyboard_exit()
216 {
217 irq_unregister(PS2_KEYBOARD_INTR_VECTOR);
218 kfifo_free_alloc(&kb_buf);
219 }
220