8 #include <tpl_drv_common.h>
9 #include <tpl_drv_keyboard.h>
13 #define KB_BUFFER_SIZE 1024
14 #define USE_KERNEL_MAGIC 1
20 int KB_Install(char **Arguments);
22 void KB_AddBuffer(char ch);
23 Uint64 KB_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Dest);
25 int KB_IOCtl(tVFS_Node *Node, int Id, void *Data);
28 MODULE_DEFINE(0, 0x0100, PS2Keyboard, KB_Install, NULL, NULL);
29 tDevFS_Driver gKB_DevInfo = {
38 tKeybardCallback gKB_Callback = NULL;
39 Uint32 **gpKB_Map = gpKBDUS;
40 Uint8 gbaKB_States[3][256];
41 int gbKB_ShiftState = 0;
42 int gbKB_CapsState = 0;
44 int giKB_KeyLayer = 0;
46 int gbKB_MagicState = 0;
48 //Uint64 giKB_ReadBase = 0;
49 //Uint32 gaKB_Buffer[KB_BUFFER_SIZE]; //!< Keyboard Ring Buffer
50 //volatile int giKB_InsertPoint = 0; //!< Writing location marker
51 //volatile int giKB_ReadPoint = 0; //!< Reading location marker
52 //volatile int giKB_InUse = 0; //!< Lock marker
56 * \fn int KB_Install(char **Arguments)
58 int KB_Install(char **Arguments)
62 // Attempt to get around a strange bug in Bochs/Qemu by toggling
63 // the controller on and off
65 outb(0x61, temp | 0x80);
66 outb(0x61, temp & 0x7F);
67 inb(0x60); // Clear keyboard buffer
69 IRQ_AddHandler(1, KB_IRQHandler);
70 DevFS_AddDevice( &gKB_DevInfo );
71 //Log("KB_Install: Installed");
76 * \fn void KB_IRQHandler()
77 * \brief Called on a keyboard IRQ
85 // Check port 0x64 to tell if this is from the aux port
86 if( inb(0x64) & 0x20 ) return;
88 scancode = inb(0x60); // Read from the keyboard's data buffer
89 //Log_Debug("Keyboard", "scancode = %02x", scancode);
91 //Log("KB_IRQHandler: scancode = 0x%02x", scancode);
94 if(scancode == 0xFA) {
95 // Oh man! This is anachic (I'm leaving it here to represent the
96 // mess that Acess once was)
97 //kb_lastChar = KB_ACK;
102 if(scancode == 0xE0) {
107 if(scancode == 0xE1) {
127 ch = gpKB_Map[giKB_KeyLayer][scancode];
128 //keyNum = giKB_KeyLayer * 256 + scancode;
129 // Check for unknown key
130 if(!ch && !gbKB_KeyUp)
131 Log_Warning("Keyboard", "UNK %i %x", giKB_KeyLayer, scancode);
137 gbaKB_States[giKB_KeyLayer][scancode] = 0; // Unset key state flag
140 if(ch == KEY_LCTRL) gbKB_MagicState &= ~1;
141 if(ch == KEY_LALT) gbKB_MagicState &= ~2;
144 if(ch == KEY_LSHIFT) gbKB_ShiftState &= ~1;
145 if(ch == KEY_RSHIFT) gbKB_ShiftState &= ~2;
148 if(ch != 0 && gKB_Callback)
149 gKB_Callback( ch & 0x80000000 );
156 // Set the bit relating to the key
157 gbaKB_States[giKB_KeyLayer][scancode] = 1;
158 // Set shift key bits
159 if(ch == KEY_LSHIFT) gbKB_ShiftState |= 1;
160 if(ch == KEY_RSHIFT) gbKB_ShiftState |= 2;
162 // Check for Caps Lock
163 if(ch == KEY_CAPSLOCK) {
164 gbKB_CapsState = !gbKB_CapsState;
171 // Ignore Non-Printable Characters
174 // --- Check for Kernel Magic Combos
176 if(ch == KEY_LCTRL) {
177 gbKB_MagicState |= 1;
178 //Log_Log("Keyboard", "Kernel Magic LCTRL Down\n");
181 gbKB_MagicState |= 2;
182 //Log_Log("Keyboard", "Kernel Magic LALT Down\n");
184 if(gbKB_MagicState == 3)
188 case 'd': __asm__ __volatile__ ("xchg %bx, %bx"); break;
189 case 'p': Threads_Dump(); break;
195 // - Darn ugly hacks !(!x) means (bool)x
196 if( !(!gbKB_ShiftState) ^ gbKB_CapsState)
201 case '`': ch = '~'; break;
202 case '1': ch = '!'; break;
203 case '2': ch = '@'; break;
204 case '3': ch = '#'; break;
205 case '4': ch = '$'; break;
206 case '5': ch = '%'; break;
207 case '6': ch = '^'; break;
208 case '7': ch = '&'; break;
209 case '8': ch = '*'; break;
210 case '9': ch = '('; break;
211 case '0': ch = ')'; break;
212 case '-': ch = '_'; break;
213 case '=': ch = '+'; break;
214 case '[': ch = '{'; break;
215 case ']': ch = '}'; break;
216 case '\\': ch = '|'; break;
217 case ';': ch = ':'; break;
218 case '\'': ch = '"'; break;
219 case ',': ch = '<'; break;
220 case '.': ch = '>'; break;
221 case '/': ch = '?'; break;
223 if('a' <= ch && ch <= 'z')
229 if(gKB_Callback && ch != 0) gKB_Callback(ch);
233 * \fn void KB_UpdateLEDs()
234 * \brief Updates the status of the keyboard LEDs
240 leds = (gbKB_CapsState ? 4 : 0);
242 while( inb(0x64) & 2 ); // Wait for bit 2 to unset
243 outb(0x60, 0xED); // Send update command
245 while( inb(0x64) & 2 ); // Wait for bit 2 to unset
250 * \fn int KB_IOCtl(tVFS_Node *Node, int Id, void *Data)
251 * \brief Calls an IOCtl Command
253 int KB_IOCtl(tVFS_Node *Node, int Id, void *Data)
257 case DRV_IOCTL_TYPE: return DRV_TYPE_KEYBOARD;
258 case DRV_IOCTL_IDENT: memcpy(Data, "KB\0\0", 4); return 1;
259 case DRV_IOCTL_VERSION: return 0x100;
260 case DRV_IOCTL_LOOKUP: return 0;
262 // Sets the Keyboard Callback
263 case KB_IOCTL_SETCALLBACK:
265 if((Uint)Data < KERNEL_BASE) return 0;
266 // Can only be set once
267 if(gKB_Callback != NULL) return 0;