8 #include <tpl_drv_common.h>
9 #include <tpl_drv_keyboard.h>
13 #define KB_BUFFER_SIZE 1024
19 int KB_Install(char **Arguments);
21 void KB_AddBuffer(char ch);
22 Uint64 KB_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Dest);
24 int KB_IOCtl(tVFS_Node *Node, int Id, void *Data);
27 MODULE_DEFINE(0, 0x0100, PS2Keyboard, KB_Install, NULL, NULL);
28 tDevFS_Driver gKB_DevInfo = {
37 tKeybardCallback gKB_Callback = NULL;
38 Uint32 **gpKB_Map = gpKBDUS;
39 Uint8 gbaKB_States[3][256];
40 int gbKB_ShiftState = 0;
41 int gbKB_CapsState = 0;
43 int giKB_KeyLayer = 0;
45 int gbKB_MagicState = 0;
47 //Uint64 giKB_ReadBase = 0;
48 //Uint32 gaKB_Buffer[KB_BUFFER_SIZE]; //!< Keyboard Ring Buffer
49 //volatile int giKB_InsertPoint = 0; //!< Writing location marker
50 //volatile int giKB_ReadPoint = 0; //!< Reading location marker
51 //volatile int giKB_InUse = 0; //!< Lock marker
55 * \fn int KB_Install(char **Arguments)
57 int KB_Install(char **Arguments)
59 IRQ_AddHandler(1, KB_IRQHandler);
60 DevFS_AddDevice( &gKB_DevInfo );
65 * \fn void KB_IRQHandler()
66 * \brief Called on a keyboard IRQ
74 //if( inportb(0x64) & 0x20 ) return;
76 scancode = inb(0x60); // Read from the keyboard's data buffer
79 if(scancode == 0xFA) {
80 // Oh man! This is anachic (I'm leaving it here to represent the
81 // mess that acess once was
82 //kb_lastChar = KB_ACK;
87 if(scancode == 0xE0) {
92 if(scancode == 0xE1) {
112 ch = gpKB_Map[giKB_KeyLayer][scancode];
113 //keyNum = giKB_KeyLayer * 256 + scancode;
114 // Check for unknown key
115 if(!ch && !gbKB_KeyUp)
116 Warning("UNK %i %x", giKB_KeyLayer, scancode);
122 gbaKB_States[giKB_KeyLayer][scancode] = 0; // Unset key state flag
125 if(ch == KEY_LCTRL) gbKB_MagicState &= ~1;
126 if(ch == KEY_LALT) gbKB_MagicState &= ~2;
129 if(ch == KEY_LSHIFT) gbKB_ShiftState &= ~1;
130 if(ch == KEY_RSHIFT) gbKB_ShiftState &= ~2;
133 if(ch != 0 && gKB_Callback)
134 gKB_Callback( ch & 0x80000000 );
141 // Set the bit relating to the key
142 gbaKB_States[giKB_KeyLayer][scancode] = 1;
143 // Set shift key bits
144 if(ch == KEY_LSHIFT) gbKB_ShiftState |= 1;
145 if(ch == KEY_RSHIFT) gbKB_ShiftState |= 2;
147 // Check for Caps Lock
148 if(ch == KEY_CAPSLOCK) {
149 gbKB_CapsState = !gbKB_CapsState;
156 // Ignore Non-Printable Characters
159 // --- Check for Kernel Magic Combos
161 if(ch == KEY_LCTRL) gbKB_MagicState |= 1;
162 if(ch == KEY_LALT) gbKB_MagicState |= 2;
163 if(gbKB_MagicState == 3)
167 case 'd': __asm__ __volatile__ ("xchg %bx, %bx"); break;
168 case 'p': Threads_Dump(); break;
174 // - Darn ugly hacks !(!x) means (bool)x
175 if( !(!gbKB_ShiftState) ^ gbKB_CapsState)
180 case '`': ch = '~'; break;
181 case '1': ch = '!'; break;
182 case '2': ch = '@'; break;
183 case '3': ch = '#'; break;
184 case '4': ch = '$'; break;
185 case '5': ch = '%'; break;
186 case '6': ch = '^'; break;
187 case '7': ch = '&'; break;
188 case '8': ch = '*'; break;
189 case '9': ch = '('; break;
190 case '0': ch = ')'; break;
191 case '-': ch = '_'; break;
192 case '=': ch = '+'; break;
193 case '[': ch = '{'; break;
194 case ']': ch = '}'; break;
195 case '\\': ch = '|'; break;
196 case ';': ch = ':'; break;
197 case '\'': ch = '"'; break;
198 case ',': ch = '<'; break;
199 case '.': ch = '>'; break;
200 case '/': ch = '?'; break;
202 if('a' <= ch && ch <= 'z')
208 if(gKB_Callback && ch != 0) gKB_Callback(ch);
212 * \fn void KB_UpdateLEDs()
213 * \brief Updates the status of the keyboard LEDs
219 leds = (gbKB_CapsState ? 4 : 0);
221 while( inb(0x64) & 2 ); // Wait for bit 2 to unset
222 outb(0x60, 0xED); // Send update command
224 while( inb(0x64) & 2 ); // Wait for bit 2 to unset
229 * \fn int KB_IOCtl(tVFS_Node *Node, int Id, void *Data)
230 * \brief Calls an IOCtl Command
232 int KB_IOCtl(tVFS_Node *Node, int Id, void *Data)
236 case DRV_IOCTL_TYPE: return DRV_TYPE_KEYBOARD;
237 case DRV_IOCTL_IDENT: memcpy(Data, "KB\0\0", 4); return 1;
238 case DRV_IOCTL_VERSION: return 0x100;
239 case DRV_IOCTL_LOOKUP: return 0;
241 // Sets the Keyboard Callback
242 case KB_IOCTL_SETCALLBACK:
244 if((Uint)Data < KERNEL_BASE) return 0;
245 // Can only be set once
246 if(gKB_Callback != NULL) return 0;