Bugfixing and testing TCP stack
[tpg/acess2.git] / Kernel / drv / kb.c
1 /*
2  * Acess2
3  * PS2 Keyboard Driver
4  */
5 #include <acess.h>
6 #include <modules.h>
7 #include <fs_devfs.h>
8 #include <tpl_drv_common.h>
9 #include <tpl_drv_keyboard.h>
10 #include "kb_kbdus.h"
11
12 // === CONSTANTS ===
13 #define KB_BUFFER_SIZE  1024
14
15 // === IMPORTS ===
16 void    Threads_Dump();
17
18 // === PROTOTYPES ===
19  int    KB_Install(char **Arguments);
20 void    KB_IRQHandler();
21 void    KB_AddBuffer(char ch);
22 Uint64  KB_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Dest);
23 void    KB_UpdateLEDs();
24  int    KB_IOCtl(tVFS_Node *Node, int Id, void *Data);
25
26 // === GLOBALS ===
27 MODULE_DEFINE(0, 0x0100, PS2Keyboard, KB_Install, NULL, NULL);
28 tDevFS_Driver   gKB_DevInfo = {
29         NULL, "PS2Keyboard",
30         {
31         .NumACLs = 0,
32         .Size = 0,
33         //.Read = KB_Read,
34         .IOCtl = KB_IOCtl
35         }
36 };
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;
42  int    gbKB_KeyUp = 0;
43  int    giKB_KeyLayer = 0;
44 #if USE_KERNEL_MAGIC
45  int    gbKB_MagicState = 0;
46 #endif
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
52
53 // === CODE ===
54 /**
55  * \fn int KB_Install(char **Arguments)
56  */
57 int KB_Install(char **Arguments)
58 {
59         IRQ_AddHandler(1, KB_IRQHandler);
60         DevFS_AddDevice( &gKB_DevInfo );
61         //Log("KB_Install: Installed");
62         return 1;
63 }
64
65 /**
66  * \fn void KB_IRQHandler()
67  * \brief Called on a keyboard IRQ
68  */
69 void KB_IRQHandler()
70 {
71         Uint8   scancode;
72         Uint32  ch;
73         // int  keyNum;
74
75         //if( inportb(0x64) & 0x20 )    return;
76
77         scancode = inb(0x60); // Read from the keyboard's data buffer
78
79         //Log("KB_IRQHandler: scancode = 0x%02x", scancode);
80
81         // Ignore ACKs
82         if(scancode == 0xFA) {
83                 // Oh man! This is anachic (I'm leaving it here to represent the
84                 // mess that Acess once was)
85                 //kb_lastChar = KB_ACK;
86                 return;
87         }
88
89         // Layer +1
90         if(scancode == 0xE0) {
91                 giKB_KeyLayer = 1;
92                 return;
93         }
94         // Layer +2
95         if(scancode == 0xE1) {
96                 giKB_KeyLayer = 2;
97                 return;
98         }
99
100         #if KB_ALT_SCANCODES
101         if(scancode == 0xF0)
102         {
103                 gbKB_KeyUp = 1;
104                 return;
105         }
106         #else
107         if(scancode & 0x80)
108         {
109                 scancode &= 0x7F;
110                 gbKB_KeyUp = 1;
111         }
112         #endif
113
114         // Translate
115         ch = gpKB_Map[giKB_KeyLayer][scancode];
116         //keyNum = giKB_KeyLayer * 256 + scancode;
117         // Check for unknown key
118         if(!ch && !gbKB_KeyUp)
119                 Warning("UNK %i %x", giKB_KeyLayer, scancode);
120
121         // Key Up?
122         if (gbKB_KeyUp)
123         {
124                 gbKB_KeyUp = 0;
125                 gbaKB_States[giKB_KeyLayer][scancode] = 0;      // Unset key state flag
126
127                 #if USE_KERNEL_MAGIC
128                 if(ch == KEY_LCTRL)     gbKB_MagicState &= ~1;
129                 if(ch == KEY_LALT)      gbKB_MagicState &= ~2;
130                 #endif
131
132                 if(ch == KEY_LSHIFT)    gbKB_ShiftState &= ~1;
133                 if(ch == KEY_RSHIFT)    gbKB_ShiftState &= ~2;
134
135                 // Call callback
136                 if(ch != 0 && gKB_Callback)
137                         gKB_Callback( ch & 0x80000000 );
138
139                 // Reset Layer
140                 giKB_KeyLayer = 0;
141                 return;
142         }
143
144         // Set the bit relating to the key
145         gbaKB_States[giKB_KeyLayer][scancode] = 1;
146         // Set shift key bits
147         if(ch == KEY_LSHIFT)    gbKB_ShiftState |= 1;
148         if(ch == KEY_RSHIFT)    gbKB_ShiftState |= 2;
149
150         // Check for Caps Lock
151         if(ch == KEY_CAPSLOCK) {
152                 gbKB_CapsState = !gbKB_CapsState;
153                 KB_UpdateLEDs();
154         }
155
156         // Reset Layer
157         giKB_KeyLayer = 0;
158
159         // Ignore Non-Printable Characters
160         if(ch == 0)             return;
161
162         // --- Check for Kernel Magic Combos
163         #if USE_KERNEL_MAGIC
164         if(ch == KEY_LCTRL)     gbKB_MagicState |= 1;
165         if(ch == KEY_LALT)      gbKB_MagicState |= 2;
166         if(gbKB_MagicState == 3)
167         {
168                 switch(ch)
169                 {
170                 case 'd':       __asm__ __volatile__ ("xchg %bx, %bx"); break;
171                 case 'p':       Threads_Dump(); break;
172                 }
173         }
174         #endif
175
176         // Is shift pressed
177         // - Darn ugly hacks !(!x) means (bool)x
178         if( !(!gbKB_ShiftState) ^ gbKB_CapsState)
179         {
180                 switch(ch)
181                 {
182                 case 0: break;
183                 case '`':       ch = '~';       break;
184                 case '1':       ch = '!';       break;
185                 case '2':       ch = '@';       break;
186                 case '3':       ch = '#';       break;
187                 case '4':       ch = '$';       break;
188                 case '5':       ch = '%';       break;
189                 case '6':       ch = '^';       break;
190                 case '7':       ch = '&';       break;
191                 case '8':       ch = '*';       break;
192                 case '9':       ch = '(';       break;
193                 case '0':       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;
201                 case ',':       ch = '<';       break;
202                 case '.':       ch = '>';       break;
203                 case '/':       ch = '?';       break;
204                 default:
205                         if('a' <= ch && ch <= 'z')
206                                 ch -= 0x20;
207                         break;
208                 }
209         }
210
211         if(gKB_Callback && ch != 0)     gKB_Callback(ch);
212 }
213
214 /**
215  * \fn void KB_UpdateLEDs()
216  * \brief Updates the status of the keyboard LEDs
217  */
218 void KB_UpdateLEDs()
219 {
220         Uint8   leds;
221
222         leds = (gbKB_CapsState ? 4 : 0);
223
224         while( inb(0x64) & 2 ); // Wait for bit 2 to unset
225         outb(0x60, 0xED);       // Send update command
226
227         while( inb(0x64) & 2 ); // Wait for bit 2 to unset
228         outb(0x60, leds);
229 }
230
231 /**
232  * \fn int KB_IOCtl(tVFS_Node *Node, int Id, void *Data)
233  * \brief Calls an IOCtl Command
234  */
235 int KB_IOCtl(tVFS_Node *Node, int Id, void *Data)
236 {
237         switch(Id)
238         {
239         case DRV_IOCTL_TYPE:    return DRV_TYPE_KEYBOARD;
240         case DRV_IOCTL_IDENT:   memcpy(Data, "KB\0\0", 4);      return 1;
241         case DRV_IOCTL_VERSION: return 0x100;
242         case DRV_IOCTL_LOOKUP:  return 0;
243
244         // Sets the Keyboard Callback
245         case KB_IOCTL_SETCALLBACK:
246                 // Sanity Check
247                 if((Uint)Data < KERNEL_BASE)    return 0;
248                 // Can only be set once
249                 if(gKB_Callback != NULL)        return 0;
250                 // Set Callback
251                 gKB_Callback = Data;
252                 return 1;
253
254         default:
255                 return 0;
256         }
257 }

UCC git Repository :: git.ucc.asn.au