ps2kbd.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2011-2018 K. Lange
  5. *
  6. * Low-level keyboard interrupt driver.
  7. *
  8. * Creates a device file (keyboard_pipe) that can be read
  9. * to retreive keyboard events.
  10. *
  11. */
  12. #include <kernel/system.h>
  13. #include <kernel/logging.h>
  14. #include <kernel/fs.h>
  15. #include <kernel/pipe.h>
  16. #include <kernel/process.h>
  17. #include <kernel/module.h>
  18. #define KEY_DEVICE 0x60
  19. #define KEY_PENDING 0x64
  20. #define KEY_IRQ 1
  21. static fs_node_t * keyboard_pipe;
  22. /*
  23. * Wait on the keyboard.
  24. */
  25. static void keyboard_wait(void) {
  26. while(inportb(KEY_PENDING) & 2);
  27. }
  28. /*
  29. * Keyboard interrupt callback
  30. */
  31. static int keyboard_handler(struct regs *r) {
  32. unsigned char scancode;
  33. if (inportb(KEY_PENDING) & 0x01) {
  34. scancode = inportb(KEY_DEVICE);
  35. write_fs(keyboard_pipe, 0, 1, (uint8_t []){scancode});
  36. }
  37. irq_ack(KEY_IRQ);
  38. return 1;
  39. }
  40. /*
  41. * Install the keyboard driver and initialize the
  42. * pipe device for userspace.
  43. */
  44. static int keyboard_install(void) {
  45. debug_print(NOTICE, "Initializing PS/2 keyboard driver");
  46. /* Create a device pipe */
  47. keyboard_pipe = make_pipe(128);
  48. current_process->fds->entries[0] = keyboard_pipe;
  49. keyboard_pipe->flags = FS_CHARDEVICE;
  50. vfs_mount("/dev/kbd", keyboard_pipe);
  51. /* Install the interrupt handler */
  52. irq_install_handler(KEY_IRQ, keyboard_handler, "ps2 kbd");
  53. return 0;
  54. }
  55. static void keyboard_reset_ps2(void) {
  56. uint8_t tmp = inportb(0x61);
  57. outportb(0x61, tmp | 0x80);
  58. outportb(0x61, tmp & 0x7F);
  59. inportb(KEY_DEVICE);
  60. }
  61. static int keyboard_uninstall(void) {
  62. /* TODO */
  63. return 0;
  64. }
  65. MODULE_DEF(ps2kbd, keyboard_install, keyboard_uninstall);