about summary refs log tree commit diff stats
path: root/kernel.soso/keyboard.c
blob: 8bb7ed817b38b11fb14366522beb22e103265292 (plain) (blame)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "keyboard.h"
#include "isr.h"
#include "common.h"
#include "screen.h"
#include "ttydriver.h"
#include "fs.h"
#include "device.h"
#include "alloc.h"
#include "devfs.h"
#include "list.h"

static uint8* gKeyBuffer = NULL;
static uint32 gKeyBufferWriteIndex = 0;
static uint32 gKeyBufferReadIndex = 0;

#define KEYBUFFER_SIZE 128

static BOOL keyboard_open(File *file, uint32 flags);
static void keyboard_close(File *file);
static int32 keyboard_read(File *file, uint32 size, uint8 *buffer);
static int32 keyboard_ioctl(File *file, int32 request, void * argp);

typedef enum ReadMode {
    Blocking = 0,
    NonBlocking = 1
} ReadMode;

typedef struct Reader {
    uint32 readIndex;
    ReadMode readMode;
} Reader;

static List* gReaders = NULL;

static void handleKeyboardInterrupt(Registers *regs);

void initializeKeyboard() {
    Device device;
    memset((uint8*)&device, 0, sizeof(Device));
    strcpy(device.name, "keyboard");
    device.deviceType = FT_CharacterDevice;
    device.open = keyboard_open;
    device.close = keyboard_close;
    device.read = keyboard_read;
    device.ioctl = keyboard_ioctl;

    gKeyBuffer = kmalloc(KEYBUFFER_SIZE);
    memset((uint8*)gKeyBuffer, 0, KEYBUFFER_SIZE);

    gReaders = List_Create();

    registerDevice(&device);

    registerInterruptHandler(IRQ1, handleKeyboardInterrupt);
}

static BOOL keyboard_open(File *file, uint32 flags) {
    Reader* reader = (Reader*)kmalloc(sizeof(Reader));
    reader->readIndex = 0;
    reader->readMode = Blocking;

    if (gKeyBufferWriteIndex > 0) {
        reader->readIndex = gKeyBufferWriteIndex;
    }
    file->privateData = (void*)reader;

    List_Append(gReaders, file);

    return TRUE;
}

static void keyboard_close(File *file) {
    //printkf("keyboard_close\n");

    Reader* reader = (Reader*)file->privateData;

    kfree(reader);

    List_RemoveFirstOccurrence(gReaders, file);
}

static int32 keyboard_read(File *file, uint32 size, uint8 *buffer) {
    Reader* reader = (Reader*)file->privateData;

    uint32 readIndex = reader->readIndex;

    if (reader->readMode == Blocking) {
        while (readIndex == gKeyBufferWriteIndex) {
            file->thread->state = TS_WAITIO;
            file->thread->state_privateData = keyboard_read;
            enableInterrupts();
            halt();
        }
    }

    disableInterrupts();

    if (readIndex == gKeyBufferWriteIndex) {
        //non-blocking return here
        return -1;
    }

    buffer[0] = gKeyBuffer[readIndex];
    readIndex++;
    readIndex %= KEYBUFFER_SIZE;

    reader->readIndex = readIndex;

    return 1;
}

static int32 keyboard_ioctl(File *file, int32 request, void * argp) {
    Reader* reader = (Reader*)file->privateData;

    int cmd = (int)argp;

    switch (request) {
    case 0: //get
        *(int*)argp = (int)reader->readMode;
        return 0;
        break;
    case 1: //set
        if (cmd == 0) {
            reader->readMode = Blocking;

            return 0;
        }
        else if (cmd == 1) {
            reader->readMode = NonBlocking;
            return 0;
        }
        break;
    default:
        break;
    }

    return -1;
}

static void handleKeyboardInterrupt(Registers *regs) {
    uint8 scancode = 0;
    do {
        scancode = inb(0x64);
    } while ((scancode & 0x01) == 0);

    scancode = inb(0x60);

    gKeyBuffer[gKeyBufferWriteIndex] = scancode;
    gKeyBufferWriteIndex++;
    gKeyBufferWriteIndex %= KEYBUFFER_SIZE;

    //Wake readers
    List_Foreach(n, gReaders) {
        File* file = n->data;

        if (file->thread->state == TS_WAITIO) {
            if (file->thread->state_privateData == keyboard_read) {
                file->thread->state = TS_RUN;
                file->thread->state_privateData = NULL;
            }
        }
    }

    sendKeyInputToTTY(getActiveTTY(), scancode);
}