keyboard.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "keyboard.h"
  2. #include "../interrupt/apic/apic.h"
  3. #include "../../mm/mm.h"
  4. #include "../../mm/slab.h"
  5. #include "../../common/printk.h"
  6. static struct keyboard_input_buffer *kb_buf_ptr = NULL;
  7. static int shift_l, shift_r, ctrl_l, ctrl_r, alt_l, alt_r;
  8. struct apic_IO_APIC_RTE_entry entry;
  9. hardware_intr_controller keyboard_intr_controller =
  10. {
  11. .enable = apic_ioapic_enable,
  12. .disable = apic_ioapic_disable,
  13. .install = apic_ioapic_install,
  14. .uninstall = apic_ioapic_uninstall,
  15. .ack = apic_ioapic_edge_ack,
  16. };
  17. /**
  18. * @brief 键盘中断处理函数(中断上半部)
  19. * 将数据存入缓冲区
  20. * @param irq_num 中断向量号
  21. * @param param 参数
  22. * @param regs 寄存器信息
  23. */
  24. void keyboard_handler(ul irq_num, ul param, struct pt_regs *regs)
  25. {
  26. // 读取键盘输入的信息
  27. unsigned x = io_in8(0x60);
  28. printk_color(ORANGE, BLACK, "key_pressed:%02x\n", x);
  29. // 当头指针越过界时,恢复指向数组头部
  30. if (kb_buf_ptr->ptr_head == kb_buf_ptr + keyboard_buffer_size)
  31. kb_buf_ptr->ptr_head = kb_buf_ptr->buffer;
  32. if (kb_buf_ptr->count >= keyboard_buffer_size)
  33. {
  34. kwarn("Keyboard input buffer is full.");
  35. return;
  36. }
  37. *kb_buf_ptr->ptr_head = x;
  38. ++(kb_buf_ptr->count);
  39. ++(kb_buf_ptr->ptr_head);
  40. }
  41. /**
  42. * @brief 初始化键盘驱动程序的函数
  43. *
  44. */
  45. void keyboard_init()
  46. {
  47. // ======= 初始化键盘循环队列缓冲区 ===========
  48. // 申请键盘循环队列缓冲区的内存
  49. kb_buf_ptr = (struct keyboard_input_buffer *)kmalloc(sizeof(struct keyboard_input_buffer), 0);
  50. kb_buf_ptr->ptr_head = kb_buf_ptr;
  51. kb_buf_ptr->ptr_tail = kb_buf_ptr;
  52. kb_buf_ptr->count = 0;
  53. memset(kb_buf_ptr->buffer, 0, keyboard_buffer_size);
  54. // ======== 初始化中断RTE entry ==========
  55. entry.vector = 0x21; // 设置中断向量号
  56. entry.deliver_mode = IO_APIC_FIXED; // 投递模式:混合
  57. entry.dest_mode = DEST_PHYSICAL; // 物理模式投递中断
  58. entry.deliver_status = IDLE;
  59. entry.trigger_mode = EDGE_TRIGGER; // 设置边沿触发
  60. entry.polarity = POLARITY_HIGH; // 高电平触发
  61. entry.remote_IRR = IRR_RESET;
  62. entry.mask = MASKED;
  63. entry.reserved = 0;
  64. entry.destination.physical.reserved1 = 0;
  65. entry.destination.physical.reserved2 = 0;
  66. entry.destination.physical.phy_dest = 0; // 设置投递到BSP处理器
  67. // ======== 初始化键盘控制器,写入配置值 =========
  68. wait_keyboard_write();
  69. io_out8(PORT_KEYBOARD_CONTROL, KEYBOARD_COMMAND_WRITE);
  70. wait_keyboard_write();
  71. io_out8(PORT_KEYBOARD_DATA, KEYBOARD_PARAM_INIT);
  72. wait_keyboard_write();
  73. // 执行一百万次nop,等待键盘控制器把命令执行完毕
  74. for (int i = 0; i < 1000; ++i)
  75. for (int j = 0; j < 1000; ++j)
  76. nop();
  77. shift_l = 0;
  78. shift_r = 0;
  79. ctrl_l = 0;
  80. ctrl_r = 0;
  81. alt_l = 0;
  82. alt_r = 0;
  83. // 注册中断处理程序
  84. irq_register(0x21, &entry, &keyboard_handler, (ul)kb_buf_ptr, &keyboard_intr_controller, "ps/2 keyboard");
  85. }
  86. /**
  87. * @brief 键盘驱动卸载函数
  88. *
  89. */
  90. void keyboard_exit()
  91. {
  92. irq_unregister(0x21);
  93. kfree((ul *)kb_buf_ptr);
  94. }