1/*
2 * Copyright 2017, Data61, CSIRO (ABN 41 687 119 230)
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7/* Usage of the keyboard in this file is based on information from
8 * http://www.win.tue.nl/~aeb/linux/kbd/scancodes-11.html
9 */
10
11#include <assert.h>
12#include <stdio.h>
13#include <camkes.h>
14#include <camkes/io.h>
15
16#define KEYBOARD_STATUS_PORT    0x64 /* Control port. */
17#define KEYBOARD_DATA_PORT      0x60 /* Input/output port. */
18
19#define KEYBOARD_CCB_WRITE  0x60 /* Write it to the Control port before write a CCB. */
20#define KEYBOARD_CCB_READ   0x20 /* Write it to the Control port before read a CCB. */
21
22#define KEYBOARD_ENABLE     0xae /* Keyboard enable command. */
23#define KEYBOARD_DISABLE    0xad /* Keyboard disable command. */
24
25static ps_io_port_ops_t io_port_ops;
26
27static uint8_t port_in8(uint16_t port)
28{
29    uint32_t result = 0;
30    int error = ps_io_port_in(&io_port_ops, port, IOSIZE_8, &result);
31    if (error) {
32        return 0;
33    }
34    return (uint8_t) result;
35}
36
37static uint8_t read_status(void)
38{
39    char c;
40    c = port_in8(KEYBOARD_STATUS_PORT);
41
42    printf("Current Status: 0x%x\n", c);
43    return c;
44}
45
46static uint8_t read_scancode(void)
47{
48    return port_in8(KEYBOARD_DATA_PORT);
49}
50
51static int keyboard_enable(void)
52{
53    ps_io_port_out(&io_port_ops, KEYBOARD_STATUS_PORT, IOSIZE_8, KEYBOARD_ENABLE);
54    return 0;
55}
56
57void interrupt_handle(void)
58{
59    printf("Scancode: %u\n", read_scancode());
60    keypress_emit();
61    interrupt_acknowledge();
62}
63
64void kbd__init(void)
65{
66    int error = camkes_io_port_ops(&io_port_ops);
67    assert(!error);
68    read_status();
69    keyboard_enable();
70}
71
72int kbd_get_scancode(void)
73{
74    return read_scancode();
75}
76