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;
189 * \fn void KB_AddBuffer(char ch)
190 * \brief Add to the keyboard ring buffer
192 void KB_AddBuffer(char ch)
195 gaKB_Buffer[ giKB_InsertPoint++ ] = ch;
197 if( giKB_InsertPoint == KB_BUFFER_SIZE ) giKB_InsertPoint = 0;
198 // - Force increment read pointer
199 if( giKB_InsertPoint == giKB_ReadPoint ) giKB_ReadPoint ++;
203 * \fn Uint64 KB_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Dest)
204 * \brief Read from the ring buffer
206 * \param Offset Unused (Character Device)
207 * \param Length Number of bytes to read
208 * \param Dest Destination
210 Uint64 KB_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Dest)
215 if(giKB_InUse) return -1;
218 while(giKB_ReadPoint != giKB_InsertPoint && pos < Length)
220 dstbuf[pos++] = gaKB_Buffer[ giKB_ReadPoint++ ];
221 if( giKB_ReadPoint == KB_BUFFER_SIZE ) giKB_InsertPoint = 0;
230 * \fn void KB_UpdateLEDs()
231 * \brief Updates the status of the keyboard LEDs
237 leds = (gbKB_CapsState ? 4 : 0);
239 while( inb(0x64) & 2 ); // Wait for bit 2 to unset
240 outb(0x60, 0xED); // Send update command
242 while( inb(0x64) & 2 ); // Wait for bit 2 to unset
247 * \fn int KB_IOCtl(tVFS_Node *Node, int Id, void *Data)
248 * \brief Calls an IOCtl Command
250 int KB_IOCtl(tVFS_Node *Node, int Id, void *Data)
254 case DRV_IOCTL_TYPE: return DRV_TYPE_KEYBOARD;
255 case DRV_IOCTL_IDENT: memcpy(Data, "KB\0\0", 4); return 1;
256 case DRV_IOCTL_VERSION: return 0x100;
257 case DRV_IOCTL_LOOKUP: return 0;
259 // Sets the Keyboard Callback
260 case KB_IOCTL_SETCALLBACK:
262 if((Uint)Data < KERNEL_BASE) return 0;
263 // Can only be set once
264 if(gKB_Callback != NULL) return 0;