ps2kbd.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. keyboard_pipe->flags = FS_CHARDEVICE;
  49. vfs_mount("/dev/kbd", keyboard_pipe);
  50. /* Install the interrupt handler */
  51. irq_install_handler(KEY_IRQ, keyboard_handler, "ps2 kbd");
  52. return 0;
  53. }
  54. static void keyboard_reset_ps2(void) {
  55. uint8_t tmp = inportb(0x61);
  56. outportb(0x61, tmp | 0x80);
  57. outportb(0x61, tmp & 0x7F);
  58. inportb(KEY_DEVICE);
  59. }
  60. static int keyboard_uninstall(void) {
  61. /* TODO */
  62. return 0;
  63. }
  64. MODULE_DEF(ps2kbd, keyboard_install, keyboard_uninstall);