blob: a42468f0c6aed1160b91230037265f65e1ed665f [file] [log] [blame]
wdenkb983fa22004-01-16 00:30:56 +00001/***********************************************************************
2 *
3 * (C) Copyright 2004
4 * DENX Software Engineering
5 * Wolfgang Denk, wd@denx.de
6 * All rights reserved.
7 *
8 * Keyboard driver
9 *
10 ***********************************************************************/
11
12#include <common.h>
13
14#ifdef CONFIG_PS2KBD
15
16#include <devices.h>
17#include <keyboard.h>
18
19#undef KBG_DEBUG
20
21#ifdef KBG_DEBUG
22#define PRINTF(fmt,args...) printf (fmt ,##args)
23#else
24#define PRINTF(fmt,args...)
25#endif
26
27
28#define DEVNAME "kbd"
29
30#define LED_SCR 0x01 /* scroll lock led */
31#define LED_CAP 0x04 /* caps lock led */
32#define LED_NUM 0x02 /* num lock led */
33
34#define KBD_BUFFER_LEN 0x20 /* size of the keyboardbuffer */
35
36static volatile char kbd_buffer[KBD_BUFFER_LEN];
37static volatile int in_pointer = 0;
38static volatile int out_pointer = 0;
39
40static unsigned char leds = 0;
41static unsigned char num_lock = 0;
42static unsigned char caps_lock = 0;
43static unsigned char scroll_lock = 0;
44static unsigned char shift = 0;
45static unsigned char ctrl = 0;
46static unsigned char alt = 0;
47static unsigned char e0 = 0;
48
49/******************************************************************
50 * Queue handling
51 ******************************************************************/
52
53/* puts character in the queue and sets up the in and out pointer */
54static void kbd_put_queue(char data)
55{
56 if((in_pointer+1)==KBD_BUFFER_LEN) {
57 if(out_pointer==0) {
58 return; /* buffer full */
59 } else{
60 in_pointer=0;
61 }
62 } else {
63 if((in_pointer+1)==out_pointer)
64 return; /* buffer full */
65 in_pointer++;
66 }
67 kbd_buffer[in_pointer]=data;
68 return;
69}
70
71/* test if a character is in the queue */
72static int kbd_testc(void)
73{
74 if(in_pointer==out_pointer)
75 return(0); /* no data */
76 else
77 return(1);
78}
79
80/* gets the character from the queue */
81static int kbd_getc(void)
82{
83 char c;
84 while(in_pointer==out_pointer);
85 if((out_pointer+1)==KBD_BUFFER_LEN)
86 out_pointer=0;
87 else
88 out_pointer++;
89 c=kbd_buffer[out_pointer];
90 return (int)c;
91
92}
93
94/* Simple translation table for the keys */
95
96static unsigned char kbd_plain_xlate[] = {
97 0xff,0x1b, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=','\b','\t', /* 0x00 - 0x0f */
98 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']','\r',0xff, 'a', 's', /* 0x10 - 0x1f */
99 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';','\'', '`',0xff,'\\', 'z', 'x', 'c', 'v', /* 0x20 - 0x2f */
100 'b', 'n', 'm', ',', '.', '/',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
101 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
102 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
103 '\r',0xff,0xff
104 };
105
106static unsigned char kbd_shift_xlate[] = {
107 0xff,0x1b, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+','\b','\t', /* 0x00 - 0x0f */
108 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}','\r',0xff, 'A', 'S', /* 0x10 - 0x1f */
109 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',0xff, '|', 'Z', 'X', 'C', 'V', /* 0x20 - 0x2f */
110 'B', 'N', 'M', '<', '>', '?',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
111 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
112 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
113 '\r',0xff,0xff
114 };
115
116static unsigned char kbd_ctrl_xlate[] = {
117 0xff,0x1b, '1',0x00, '3', '4', '5',0x1E, '7', '8', '9', '0',0x1F, '=','\b','\t', /* 0x00 - 0x0f */
118 0x11,0x17,0x05,0x12,0x14,0x18,0x15,0x09,0x0f,0x10,0x1b,0x1d,'\n',0xff,0x01,0x13, /* 0x10 - 0x1f */
119 0x04,0x06,0x08,0x09,0x0a,0x0b,0x0c, ';','\'', '~',0x00,0x1c,0x1a,0x18,0x03,0x16, /* 0x20 - 0x2f */
120 0x02,0x0e,0x0d, '<', '>', '?',0xff,0xff,0xff,0x00,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
121 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
122 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
123 '\r',0xff,0xff
124 };
125
126
127void handle_scancode(unsigned char scancode)
128{
129 unsigned char keycode;
130
131 /* Convert scancode to keycode */
132 PRINTF("scancode %x\n",scancode);
133 if(scancode==0xe0) {
134 e0=1; /* special charakters */
135 return;
136 }
137 if(e0==1) {
138 e0=0; /* delete flag */
139 if(!( ((scancode&0x7F)==0x38)|| /* the right ctrl key */
140 ((scancode&0x7F)==0x1D)|| /* the right alt key */
141 ((scancode&0x7F)==0x35)|| /* the right '/' key */
142 ((scancode&0x7F)==0x1C) )) /* the right enter key */
143 /* we swallow unknown e0 codes */
144 return;
145 }
146 /* special cntrl keys */
147 switch(scancode) {
148 case 0x2A:
149 case 0x36: /* shift pressed */
150 shift=1;
151 return; /* do nothing else */
152 case 0xAA:
153 case 0xB6: /* shift released */
154 shift=0;
155 return; /* do nothing else */
156 case 0x38: /* alt pressed */
157 alt=1;
158 return; /* do nothing else */
159 case 0xB8: /* alt released */
160 alt=0;
161 return; /* do nothing else */
162 case 0x1d: /* ctrl pressed */
163 ctrl=1;
164 return; /* do nothing else */
165 case 0x9d: /* ctrl released */
166 ctrl=0;
167 return; /* do nothing else */
168 case 0x46: /* scrollock pressed */
169 scroll_lock=~scroll_lock;
170 if(scroll_lock==0)
171 leds&=~LED_SCR; /* switch LED off */
172 else
173 leds|=LED_SCR; /* switch on LED */
174 pckbd_leds(leds);
175 return; /* do nothing else */
176 case 0x3A: /* capslock pressed */
177 caps_lock=~caps_lock;
178 if(caps_lock==0)
179 leds&=~LED_CAP; /* switch caps_lock off */
180 else
181 leds|=LED_CAP; /* switch on LED */
182 pckbd_leds(leds);
183 return;
184 case 0x45: /* numlock pressed */
185 num_lock=~num_lock;
186 if(num_lock==0)
187 leds&=~LED_NUM; /* switch LED off */
188 else
189 leds|=LED_NUM; /* switch on LED */
190 pckbd_leds(leds);
191 return;
192 case 0xC6: /* scroll lock released */
193 case 0xC5: /* num lock released */
194 case 0xBA: /* caps lock released */
195 return; /* just swallow */
196 }
wdenk398eeff2004-01-21 20:46:28 +0000197#if 1
wdenkb983fa22004-01-16 00:30:56 +0000198 if((scancode&0x80)==0x80) /* key released */
199 return;
wdenkda55c6e2004-01-20 23:12:12 +0000200#else
201 if((scancode&0x80)==0x00) /* key pressed */
202 return;
203 scancode &= ~0x80;
204#endif
wdenkb983fa22004-01-16 00:30:56 +0000205 /* now, decide which table we need */
206 if(scancode > (sizeof(kbd_plain_xlate)/sizeof(kbd_plain_xlate[0]))) { /* scancode not in list */
207 PRINTF("unkown scancode %X\n",scancode);
208 return; /* swallow it */
209 }
210 /* setup plain code first */
211 keycode=kbd_plain_xlate[scancode];
212 if(caps_lock==1) { /* caps_lock is pressed, overwrite plain code */
213 if(scancode > (sizeof(kbd_shift_xlate)/sizeof(kbd_shift_xlate[0]))) { /* scancode not in list */
214 PRINTF("unkown caps-locked scancode %X\n",scancode);
215 return; /* swallow it */
216 }
217 keycode=kbd_shift_xlate[scancode];
218 if(keycode<'A') { /* we only want the alphas capital */
219 keycode=kbd_plain_xlate[scancode];
220 }
221 }
222 if(shift==1) { /* shift overwrites caps_lock */
223 if(scancode > (sizeof(kbd_shift_xlate)/sizeof(kbd_shift_xlate[0]))) { /* scancode not in list */
224 PRINTF("unkown shifted scancode %X\n",scancode);
225 return; /* swallow it */
226 }
227 keycode=kbd_shift_xlate[scancode];
228 }
229 if(ctrl==1) { /* ctrl overwrites caps_lock and shift */
230 if(scancode > (sizeof(kbd_ctrl_xlate)/sizeof(kbd_ctrl_xlate[0]))) { /* scancode not in list */
231 PRINTF("unkown ctrl scancode %X\n",scancode);
232 return; /* swallow it */
233 }
234 keycode=kbd_ctrl_xlate[scancode];
235 }
236 /* check if valid keycode */
237 if(keycode==0xff) {
238 PRINTF("unkown scancode %X\n",scancode);
239 return; /* swallow unknown codes */
240 }
241
242 kbd_put_queue(keycode);
243 PRINTF("%x\n",keycode);
244}
245
246/******************************************************************
247 * Init
248 ******************************************************************/
249
250#ifdef CFG_CONSOLE_OVERWRITE_ROUTINE
251extern int overwrite_console (void);
252#else
253int overwrite_console (void)
254{
255 return (0);
256}
257#endif
258
259int kbd_init (void)
260{
261 int error;
262 device_t kbddev ;
263 char *stdinname = getenv ("stdin");
264
265 if(kbd_init_hw()==-1)
266 return -1;
267 memset (&kbddev, 0, sizeof(kbddev));
268 strcpy(kbddev.name, DEVNAME);
269 kbddev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
270 kbddev.putc = NULL ;
271 kbddev.puts = NULL ;
272 kbddev.getc = kbd_getc ;
273 kbddev.tstc = kbd_testc ;
274
275 error = device_register (&kbddev);
276 if(error==0) {
277 /* check if this is the standard input device */
278 if(strcmp(stdinname,DEVNAME)==0) {
279 /* reassign the console */
280 if(overwrite_console()) {
281 return 1;
282 }
283 error=console_assign(stdin,DEVNAME);
284 if(error==0)
285 return 1;
286 else
287 return error;
288 }
289 return 1;
290 }
291 return error;
292}
293
294#endif /* CONFIG_PS2KBD */