Advertisement
Guest User

ps2 driver

a guest
Jan 2nd, 2025
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.03 KB | Source Code | 0 0
  1. extern void irq_ack(int);
  2.  
  3. typedef enum ps2_mouse_commands {
  4.   MOUSE_PORT = 0x60,
  5.   MOUSE_STATUS = 0x64,
  6.   MOUSE_ABIT = 0x02,
  7.   MOUSE_BBIT = 0x01,
  8.   MOUSE_WRITE = 0xD4,
  9.   MOUSE_F_BIT = 0x20,
  10.   MOUSE_V_BIT = 0x08
  11. } ps2_mouse_command_t;
  12.  
  13. typedef enum ps2_mouse_flags {
  14.   MOUSE_LEFT = 1 << 0,
  15.   MOUSE_RIGHT = 1 << 1,
  16.   MOUSE_MIDDLE = 1 << 2,
  17.   MOUSE_ALWAYS_ONE = 1 << 3,
  18.   MOUSE_X_SIGN_BIT = 1 << 4,
  19.   MOUSE_Y_SIGN_BIT = 1 << 5,
  20.   MOUSE_X_OVERFLOW = 1 << 6,
  21.   MOUSE_Y_OVERFLOW = 1 << 7,
  22. } ps2_mouse_flag_t;
  23.  
  24. static void mouse_wait(unsigned char a_type) {
  25.   uint32_t timeout = 100000;
  26.   if (!a_type) {
  27.     while (--timeout) {
  28.       if ((inb(MOUSE_STATUS) & MOUSE_BBIT) == 1) {
  29.         return;
  30.       }
  31.     }
  32.     return;
  33.   } else {
  34.     while (--timeout) {
  35.       if (!((inb(MOUSE_STATUS) & MOUSE_ABIT))) {
  36.         return;
  37.       }
  38.     }
  39.     return;
  40.   }
  41. }
  42.  
  43. static void mouse_write(uint8_t command) {
  44.   mouse_wait(1);
  45.   outb(0x64, 0xD4);
  46.   mouse_wait(1);
  47.   outb(0x60, command);
  48. }
  49.  
  50. static uint8_t mouse_read() {
  51.   mouse_wait(0);
  52.   return inb(0x60);
  53. }
  54.  
  55. void ps2_mouse_irq(void*) {
  56.     kprintf("------------------------------- MOUSE\n");
  57.     uint8_t status = inb(MOUSE_STATUS);
  58.  
  59.     while (status & MOUSE_BBIT)
  60.     {
  61.         char mouse_in = inb(MOUSE_PORT);
  62.  
  63.        
  64.  
  65.         status = inb(MOUSE_STATUS);
  66.     }
  67.     irq_ack(12);
  68. }
  69.  
  70. extern void irq_set_handler(uint8_t num, void (*handler)(void*));
  71.  
  72. void ps2_keyboard_reset() {
  73.     uint8_t tmp = inb(0x61);
  74.     outb(0x61, tmp | 0x80);
  75.     outb(0x61, tmp & 0x7F);
  76.     inb(0x60);
  77. }
  78.  
  79. bool ps2_mouse_init(void) {
  80.     uint8_t _status;
  81.     asm("cli");
  82.     mouse_wait(1);
  83.     outb(MOUSE_STATUS, 0xA8);
  84.     mouse_wait(1);
  85.     outb(MOUSE_STATUS, 0x20);
  86.     mouse_wait(0);
  87.     _status = inb(0x60) | 2;
  88.     mouse_wait(1);
  89.     outb(MOUSE_STATUS, 0x60);
  90.     mouse_wait(1);
  91.     outb(MOUSE_PORT, _status);
  92.     mouse_write(0xF6);
  93.     mouse_read();
  94.     mouse_write(0xF4);
  95.     mouse_read();
  96.  
  97.     irq_set_handler(12, ps2_mouse_irq);
  98.  
  99.     //ps2_keyboard_reset();
  100.     asm("sti");
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement