8 #include <tpl_drv_common.h>
9 #include <tpl_drv_keyboard.h>
13 #define KB_BUFFER_SIZE 1024
16 int KB_Install(char **Arguments);
18 void KB_AddBuffer(char ch);
19 Uint64 KB_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Dest);
21 int KB_IOCtl(tVFS_Node *Node, int Id, void *Data);
24 MODULE_DEFINE(0, 0x0100, PS2Keybard, KB_Install, NULL, NULL);
25 tDevFS_Driver gKB_DevInfo = {
34 tKeybardCallback gKB_Callback = NULL;
35 Uint8 **gpKB_Map = gpKBDUS;
36 Uint8 gbaKB_States[256];
37 int gbKB_ShiftState = 0;
38 int gbKB_CapsState = 0;
40 int giKB_KeyLayer = 0;
41 //Uint64 giKB_ReadBase = 0;
42 Uint8 gaKB_Buffer[KB_BUFFER_SIZE]; //!< Keyboard Ring Buffer
43 volatile int giKB_InsertPoint = 0; //!< Writing location marker
44 volatile int giKB_ReadPoint = 0; //!< Reading location marker
45 volatile int giKB_InUse = 0; //!< Lock marker
49 * \fn int KB_Install(char **Arguments)
51 int KB_Install(char **Arguments)
53 IRQ_AddHandler(1, KB_IRQHandler);
54 DevFS_AddDevice( &gKB_DevInfo );
59 * \fn void KB_IRQHandler()
60 * \brief Called on a keyboard IRQ
68 //if( inportb(0x64) & 0x20 ) return;
70 scancode = inb(0x60); // Read from the keyboard's data buffer
73 if(scancode == 0xFA) {
74 //kb_lastChar = KB_ACK;
79 if(scancode == 0xE0) {
84 if(scancode == 0xE1) {
104 ch = gpKB_Map[giKB_KeyLayer][scancode];
105 keyNum = giKB_KeyLayer * 256 + scancode;
106 // Check for unknown key
107 if(!ch && !gbKB_KeyUp)
108 Warning("UNK %i %x", giKB_KeyLayer, scancode);
117 gbaKB_States[ keyNum ] = 0; // Unset key state flag
119 if( !gbaKB_States[KEY_LSHIFT] && !gbaKB_States[KEY_RSHIFT] )
122 KB_AddBuffer(KEY_KEYUP);
128 // Set the bit relating to the key
129 gbaKB_States[keyNum] = 1;
130 if(ch == KEY_LSHIFT || ch == KEY_RSHIFT)
133 // Check for Caps Lock
134 if(ch == KEY_CAPSLOCK) {
135 gbKB_CapsState = !gbKB_CapsState;
139 // Ignore Non-Printable Characters
140 if(ch == 0 || ch & 0x80) return;
143 if(gbKB_ShiftState ^ gbKB_CapsState)
147 case '`': ch = '~'; break;
148 case '1': ch = '!'; break;
149 case '2': ch = '@'; break;
150 case '3': ch = '#'; break;
151 case '4': ch = '$'; break;
152 case '5': ch = '%'; break;
153 case '6': ch = '^'; break;
154 case '7': ch = '&'; break;
155 case '8': ch = '*'; break;
156 case '9': ch = '('; break;
157 case '0': ch = ')'; break;
158 case '-': ch = '_'; break;
159 case '=': ch = '+'; break;
160 case '[': ch = '{'; break;
161 case ']': ch = '}'; break;
162 case '\\': ch = '|'; break;
163 case ';': ch = ':'; break;
164 case '\'': ch = '"'; break;
165 case ',': ch = '<'; break;
166 case '.': ch = '>'; break;
167 case '/': ch = '?'; break;
169 if('a' <= ch && ch <= 'z')
175 // --- Check for Kernel Magic Combos
176 if(gbaKB_States[KEY_LSHIFT] && gbaKB_States[KEY_RSHIFT])
180 case 'D': __asm__ __volatile__ ("xchg %bx, %bx"); break;
184 if(gKB_Callback) gKB_Callback(ch);
188 * \fn void KB_AddBuffer(char ch)
189 * \brief Add to the keyboard ring buffer
191 void KB_AddBuffer(char ch)
194 gaKB_Buffer[ giKB_InsertPoint++ ] = ch;
196 if( giKB_InsertPoint == KB_BUFFER_SIZE ) giKB_InsertPoint = 0;
197 // - Force increment read pointer
198 if( giKB_InsertPoint == giKB_ReadPoint ) giKB_ReadPoint ++;
202 * \fn Uint64 KB_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Dest)
203 * \brief Read from the ring buffer
205 * \param Offset Unused (Character Device)
206 * \param Length Number of bytes to read
207 * \param Dest Destination
209 Uint64 KB_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Dest)
214 if(giKB_InUse) return -1;
217 while(giKB_ReadPoint != giKB_InsertPoint && pos < Length)
219 dstbuf[pos++] = gaKB_Buffer[ giKB_ReadPoint++ ];
220 if( giKB_ReadPoint == KB_BUFFER_SIZE ) giKB_InsertPoint = 0;
229 * \fn void KB_UpdateLEDs()
230 * \brief Updates the status of the keyboard LEDs
236 leds = (gbKB_CapsState ? 4 : 0);
238 while( inb(0x64) & 2 ); // Wait for bit 2 to unset
239 outb(0x60, 0xED); // Send update command
241 while( inb(0x64) & 2 ); // Wait for bit 2 to unset
246 * \fn int KB_IOCtl(tVFS_Node *Node, int Id, void *Data)
247 * \brief Calls an IOCtl Command
249 int KB_IOCtl(tVFS_Node *Node, int Id, void *Data)
253 case DRV_IOCTL_TYPE: return DRV_TYPE_KEYBOARD;
254 case DRV_IOCTL_IDENT: memcpy(Data, "KB\0\0", 4); return 1;
255 case DRV_IOCTL_VERSION: return 0x100;
256 case DRV_IOCTL_LOOKUP: return 0;
258 // Sets the Keyboard Callback
259 case KB_IOCTL_SETCALLBACK:
261 if((Uint)Data < KERNEL_BASE) return 0;
262 // Can only be set once
263 if(gKB_Callback != NULL) return 0;