8 #include <api_drv_common.h>
9 #include <api_drv_keyboard.h>
13 #define KB_BUFFER_SIZE 1024
14 #define USE_KERNEL_MAGIC 1
17 extern void Threads_ToggleTrace(int TID);
18 extern void Threads_Dump(void);
19 extern void Heap_Stats(void);
22 int KB_Install(char **Arguments);
23 void KB_HandleScancode(Uint8 scancode);
24 void KB_UpdateLEDs(void);
25 int KB_IOCtl(tVFS_Node *Node, int Id, void *Data);
28 tDevFS_Driver gKB_DevInfo = {
36 tKeybardCallback gKB_Callback = NULL;
37 Uint32 **gpKB_Map = gpKBDUS;
38 Uint8 gbaKB_States[3][256];
39 int gbKB_ShiftState = 0;
40 int gbKB_CapsState = 0;
42 int giKB_KeyLayer = 0;
44 int gbKB_MagicState = 0;
45 int giKB_MagicAddress = 0;
46 int giKB_MagicAddressPos = 0;
51 * \brief Install the keyboard driver
53 int KB_Install(char **Arguments)
55 DevFS_AddDevice( &gKB_DevInfo );
56 //Log("KB_Install: Installed");
61 * \brief Called on a keyboard IRQ
62 * \param IRQNum IRQ number (unused)
64 void KB_HandleScancode(Uint8 scancode)
67 int bCaseSwitch = (gbKB_ShiftState != 0) != (gbKB_CapsState != 0);
69 //Log_Debug("Keyboard", "scancode = %02x", scancode);
72 if(scancode == 0xFA) {
73 // Oh man! This is anarchic (I'm leaving it here to represent
74 // the mess that Acess once was)
75 //kb_lastChar = KB_ACK;
80 if(scancode == 0xE0) {
85 if(scancode == 0xE1) {
105 gKB_Callback( (giKB_KeyLayer << 8) | scancode | KEY_ACTION_RAWSYM );
108 ch = gpKB_Map[giKB_KeyLayer*2+bCaseSwitch][scancode];
109 // - Unknown characters in the shift layer fall through to lower
110 if(bCaseSwitch && ch == 0)
111 ch = gpKB_Map[giKB_KeyLayer*2][scancode];
112 // Check for unknown key
116 Log_Warning("Keyboard", "UNK %i %x", giKB_KeyLayer, scancode);
118 // Can pass through to ensure each raw message has a up/down with it
125 gbaKB_States[giKB_KeyLayer][scancode] = 0; // Unset key state flag
128 if(ch == KEY_LCTRL) gbKB_MagicState &= ~1;
129 if(ch == KEY_LALT) gbKB_MagicState &= ~2;
132 if(ch == KEY_LSHIFT) gbKB_ShiftState &= ~1;
133 if(ch == KEY_RSHIFT) gbKB_ShiftState &= ~2;
136 if(gKB_Callback) gKB_Callback( ch | KEY_ACTION_RELEASE );
144 if( gbaKB_States[giKB_KeyLayer][scancode] == 1 )
146 if(gKB_Callback) gKB_Callback(ch | KEY_ACTION_REFIRE);
151 // Set the bit relating to the key
152 gbaKB_States[giKB_KeyLayer][scancode] = 1;
153 // Set shift key bits
154 if(ch == KEY_LSHIFT) gbKB_ShiftState |= 1;
155 if(ch == KEY_RSHIFT) gbKB_ShiftState |= 2;
157 // Check for Caps Lock
158 if(ch == KEY_CAPSLOCK) {
159 gbKB_CapsState = !gbKB_CapsState;
163 // --- Check for Kernel Magic Combos
165 if(ch == KEY_LCTRL) {
166 gbKB_MagicState |= 1;
167 //Log_Log("Keyboard", "Kernel Magic LCTRL Down\n");
170 gbKB_MagicState |= 2;
171 //Log_Log("Keyboard", "Kernel Magic LALT Down\n");
173 if(gbKB_MagicState == 3)
177 case '0': case '1': case '2': case '3':
178 case '4': case '5': case '6': case '7':
179 case '8': case '9': case 'a': case 'b':
180 case 'c': case 'd': case 'e': case 'f':
182 char str[4] = {'0', 'x', ch, 0};
183 if(giKB_MagicAddressPos == BITS/4) return;
184 giKB_MagicAddress |= atoi(str) << giKB_MagicAddressPos;
185 giKB_MagicAddressPos ++;
189 // Instruction Tracing
191 Log("Toggle instruction tracing on %i\n", giKB_MagicAddress);
192 Threads_ToggleTrace( giKB_MagicAddress );
193 giKB_MagicAddress = 0; giKB_MagicAddressPos = 0;
197 case 'p': Threads_Dump(); return;
199 case 'h': Heap_Stats(); return;
207 gKB_Callback(ch | KEY_ACTION_PRESS);
214 * \fn void KB_UpdateLEDs(void)
215 * \brief Updates the status of the keyboard LEDs
217 void KB_UpdateLEDs(void)
221 leds = (gbKB_CapsState ? 4 : 0);
224 Log_Warning("Keyboard", "TODO: Update LEDs");
227 static const char *csaIOCTL_NAMES[] = {DRV_IOCTLNAMES, DRV_KEYBAORD_IOCTLNAMES, NULL};
230 * \fn int KB_IOCtl(tVFS_Node *Node, int Id, void *Data)
231 * \brief Calls an IOCtl Command
233 int KB_IOCtl(tVFS_Node *Node, int Id, void *Data)
237 BASE_IOCTLS(DRV_TYPE_KEYBOARD, "KB", 0x100, csaIOCTL_NAMES);
239 // Sets the Keyboard Callback
240 case KB_IOCTL_SETCALLBACK:
242 if((Uint)Data < KERNEL_BASE) return 0;
243 // Can only be set once
244 if(gKB_Callback != NULL) return 0;