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, PS2Keybard, KB_Install, NULL, NULL);
28 tDevFS_Driver gKB_DevInfo = {
37 tKeybardCallback gKB_Callback = NULL;
38 Uint8 **gpKB_Map = gpKBDUS;
39 Uint8 gbaKB_States[256];
40 int gbKB_ShiftState = 0;
41 int gbKB_CapsState = 0;
43 int giKB_KeyLayer = 0;
44 //Uint64 giKB_ReadBase = 0;
45 Uint8 gaKB_Buffer[KB_BUFFER_SIZE]; //!< Keyboard Ring Buffer
46 volatile int giKB_InsertPoint = 0; //!< Writing location marker
47 volatile int giKB_ReadPoint = 0; //!< Reading location marker
48 volatile int giKB_InUse = 0; //!< Lock marker
52 * \fn int KB_Install(char **Arguments)
54 int KB_Install(char **Arguments)
56 IRQ_AddHandler(1, KB_IRQHandler);
57 DevFS_AddDevice( &gKB_DevInfo );
62 * \fn void KB_IRQHandler()
63 * \brief Called on a keyboard IRQ
71 //if( inportb(0x64) & 0x20 ) return;
73 scancode = inb(0x60); // Read from the keyboard's data buffer
76 if(scancode == 0xFA) {
77 //kb_lastChar = KB_ACK;
82 if(scancode == 0xE0) {
87 if(scancode == 0xE1) {
107 ch = gpKB_Map[giKB_KeyLayer][scancode];
108 keyNum = giKB_KeyLayer * 256 + scancode;
109 // Check for unknown key
110 if(!ch && !gbKB_KeyUp)
111 Warning("UNK %i %x", giKB_KeyLayer, scancode);
120 gbaKB_States[ keyNum ] = 0; // Unset key state flag
122 if( !gbaKB_States[KEY_LSHIFT] && !gbaKB_States[KEY_RSHIFT] )
125 KB_AddBuffer(KEY_KEYUP);
131 // Set the bit relating to the key
132 gbaKB_States[keyNum] = 1;
133 if(ch == KEY_LSHIFT || ch == KEY_RSHIFT)
136 // Check for Caps Lock
137 if(ch == KEY_CAPSLOCK) {
138 gbKB_CapsState = !gbKB_CapsState;
142 // Ignore Non-Printable Characters
143 if(ch == 0 || ch & 0x80) return;
146 // --- Check for Kernel Magic Combos
147 if(gbaKB_States[KEY_LCTRL])// && gbaKB_States[KEY_LALT])
149 LOG("ch = 0x%02x", ch);
152 case 'd': __asm__ __volatile__ ("xchg %bx, %bx"); break;
153 case 'p': Threads_Dump(); break;
158 if(gbKB_ShiftState ^ gbKB_CapsState)
162 case '`': ch = '~'; break;
163 case '1': ch = '!'; break;
164 case '2': ch = '@'; break;
165 case '3': ch = '#'; break;
166 case '4': ch = '$'; break;
167 case '5': ch = '%'; break;
168 case '6': ch = '^'; break;
169 case '7': ch = '&'; break;
170 case '8': ch = '*'; break;
171 case '9': ch = '('; break;
172 case '0': ch = ')'; break;
173 case '-': ch = '_'; break;
174 case '=': ch = '+'; break;
175 case '[': ch = '{'; break;
176 case ']': ch = '}'; break;
177 case '\\': ch = '|'; break;
178 case ';': ch = ':'; break;
179 case '\'': ch = '"'; break;
180 case ',': ch = '<'; break;
181 case '.': ch = '>'; break;
182 case '/': ch = '?'; break;
184 if('a' <= ch && ch <= 'z')
190 if(gKB_Callback) gKB_Callback(ch);
194 * \fn void KB_AddBuffer(char ch)
195 * \brief Add to the keyboard ring buffer
197 void KB_AddBuffer(char ch)
200 gaKB_Buffer[ giKB_InsertPoint++ ] = ch;
202 if( giKB_InsertPoint == KB_BUFFER_SIZE ) giKB_InsertPoint = 0;
203 // - Force increment read pointer
204 if( giKB_InsertPoint == giKB_ReadPoint ) giKB_ReadPoint ++;
208 * \fn Uint64 KB_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Dest)
209 * \brief Read from the ring buffer
211 * \param Offset Unused (Character Device)
212 * \param Length Number of bytes to read
213 * \param Dest Destination
215 Uint64 KB_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Dest)
220 if(giKB_InUse) return -1;
223 while(giKB_ReadPoint != giKB_InsertPoint && pos < Length)
225 dstbuf[pos++] = gaKB_Buffer[ giKB_ReadPoint++ ];
226 if( giKB_ReadPoint == KB_BUFFER_SIZE ) giKB_InsertPoint = 0;
235 * \fn void KB_UpdateLEDs()
236 * \brief Updates the status of the keyboard LEDs
242 leds = (gbKB_CapsState ? 4 : 0);
244 while( inb(0x64) & 2 ); // Wait for bit 2 to unset
245 outb(0x60, 0xED); // Send update command
247 while( inb(0x64) & 2 ); // Wait for bit 2 to unset
252 * \fn int KB_IOCtl(tVFS_Node *Node, int Id, void *Data)
253 * \brief Calls an IOCtl Command
255 int KB_IOCtl(tVFS_Node *Node, int Id, void *Data)
259 case DRV_IOCTL_TYPE: return DRV_TYPE_KEYBOARD;
260 case DRV_IOCTL_IDENT: memcpy(Data, "KB\0\0", 4); return 1;
261 case DRV_IOCTL_VERSION: return 0x100;
262 case DRV_IOCTL_LOOKUP: return 0;
264 // Sets the Keyboard Callback
265 case KB_IOCTL_SETCALLBACK:
267 if((Uint)Data < KERNEL_BASE) return 0;
268 // Can only be set once
269 if(gKB_Callback != NULL) return 0;