2 DOS interrupt-based keyboard driver.
3 Copyright (C) 2013 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with the program. If not, see <http://www.gnu.org/licenses/>
42 #define PIC1_CMD_PORT 0x20
43 #define OCW2_EOI (1 << 5)
46 #define INTERRUPT __interrupt __far
48 #define DONE_INIT (prev_handler)
49 static void (INTERRUPT *prev_handler)();
55 #define DONE_INIT prev_intr.pm_offset
56 static _go32_dpmi_seginfo intr, prev_intr;
59 static void INTERRUPT kbintr();
62 static struct kb_event evbuf[BUFSZ];
63 static int buf_ridx, buf_widx;
66 static unsigned int num_pressed;
67 unsigned char keystate[256];
69 #define ADVANCE(x) ((x) = ((x) + 1) & (BUFSZ - 1))
71 int kb_init(int bufsz)
74 fprintf(stderr, "keyboard driver already initialized!\n");
78 buf_ridx = buf_widx = 0;
81 memset(keystate, 0, sizeof keystate);
84 /* set our interrupt handler */
87 prev_handler = _dos_getvect(KB_INTR);
88 _dos_setvect(KB_INTR, kbintr);
91 _go32_dpmi_get_protected_mode_interrupt_vector(KB_INTR, &prev_intr);
92 intr.pm_offset = (intptr_t)kbintr;
93 intr.pm_selector = _go32_my_cs();
94 _go32_dpmi_allocate_iret_wrapper(&intr);
95 _go32_dpmi_set_protected_mode_interrupt_vector(KB_INTR, &intr);
102 void kb_shutdown(void)
108 /* restore the original interrupt handler */
111 _dos_setvect(KB_INTR, prev_handler);
114 _go32_dpmi_set_protected_mode_interrupt_vector(KB_INTR, &prev_intr);
115 _go32_dpmi_free_iret_wrapper(&intr);
120 int kb_isdown(int key)
127 return keystate[KB_LALT] + keystate[KB_RALT];
130 return keystate[KB_LCTRL] + keystate[KB_RCTRL];
136 return keystate[key];
147 #define halt() asm volatile("sti\n\thlt\n\t")
151 int kb_event(struct kb_event *ev)
158 while(buf_ridx == buf_widx) {
164 *ev = evbuf[buf_ridx];
168 return ev->press ? ev->key : (ev->key | 0x100);
171 static void INTERRUPT kbintr()
189 if(num_pressed > 0) {
199 key = scantbl_ext[code];
204 c = (keystate[KB_LSHIFT] | keystate[KB_RSHIFT]) ? scantbl_shift[code] : key;
207 ev = evbuf + buf_widx;
209 if(buf_widx == buf_ridx) {
217 /* and update keystate table */
218 keystate[key] = press;
221 outp(PIC1_CMD_PORT, OCW2_EOI); /* send end-of-interrupt */