aboutsummaryrefslogtreecommitdiff
path: root/devices/ps2_keyboard.c
blob: 6171d1812abd2558d596be2a0a93d3208e3b90de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "ps2_keyboard.h"
#include <stdint.h>
#include <stdio.h>
#include <sys/io.h>

const uint8_t comm_port = 0x64; // r  status register
                                //  w command register
const uint8_t comm_enable_first_ps2 = 0xae;
const uint8_t comm_read_ctrl_config = 0x20;
const uint8_t comm_write_ctrl_config = 0x60;
const uint8_t data_port = 0x60; // rw
const uint8_t data_enable_scanning = 0xf4;

const char scancodes[] = {'E', '1', '2', '3',  '4', '5', '6',  '7', '8', '9',  '0', '-', '=', '\b', 'T', 'q', 'w', 'e',
                          'r', 't', 'y', 'u',  'i', 'o', 'p',  '[', ']', '\n', 'C', 'a', 's', 'd',  'f', 'g', 'h', 'j',
                          'k', 'l', ';', '\'', '`', 'L', '\\', 'z', 'x', 'c',  'v', 'b', 'n', 'm',  ',', '.', '/', 'R'};

void
ps2_keyboard_init()
{
  // eat all previous keystrikes
  while (inb(comm_port) & 0x1) inb(data_port);
  outb(comm_port, comm_enable_first_ps2);
  outb(comm_port, comm_read_ctrl_config);
  const uint8_t conf = (inb(data_port) | 1) & ~0x10;
  outb(comm_port, comm_write_ctrl_config);
  outb(data_port, conf);
  outb(data_port, data_enable_scanning);
}

void
ps2_keyboard_irq_handler()
{
  const uint8_t key = inb(data_port);
  if (key >= 0x80) return;

  if (key < 0x37) printf("%c", scancodes[key - 1]);
  else
    printf("key pressed: %x\n", key);
}