1 | /* |
---|
2 | * Rosimildo da Silva: rdasilva@connecttel.com |
---|
3 | */ |
---|
4 | |
---|
5 | #include <limits.h> |
---|
6 | #include <sys/types.h> |
---|
7 | #include <rtems/keyboard.h> |
---|
8 | #include "i386kbd.h" |
---|
9 | #include <rtems/kd.h> |
---|
10 | #include <bsp.h> |
---|
11 | #include <bsp/bootcard.h> |
---|
12 | |
---|
13 | #define SIZE(x) (sizeof(x)/sizeof((x)[0])) |
---|
14 | |
---|
15 | #ifndef KBD_DEFMODE |
---|
16 | #define KBD_DEFMODE ((1 << VC_REPEAT) | (1 << VC_META)) |
---|
17 | #endif |
---|
18 | |
---|
19 | #ifndef KBD_DEFLEDS |
---|
20 | /* |
---|
21 | * Some laptops take the 789uiojklm,. keys as number pad when NumLock |
---|
22 | * is on. This seems a good reason to start with NumLock off. |
---|
23 | */ |
---|
24 | #define KBD_DEFLEDS 0 |
---|
25 | #endif |
---|
26 | |
---|
27 | #ifndef KBD_DEFLOCK |
---|
28 | #define KBD_DEFLOCK 0 |
---|
29 | #endif |
---|
30 | |
---|
31 | static int set_bit(int nr, unsigned long * addr) |
---|
32 | { |
---|
33 | int mask; |
---|
34 | int retval; |
---|
35 | rtems_interrupt_level level; |
---|
36 | |
---|
37 | addr += nr >> 5; |
---|
38 | mask = 1 << (nr & 0x1f); |
---|
39 | rtems_interrupt_disable(level); |
---|
40 | retval = (mask & *addr) != 0; |
---|
41 | *addr |= mask; |
---|
42 | rtems_interrupt_enable(level); |
---|
43 | return retval; |
---|
44 | } |
---|
45 | |
---|
46 | static int clear_bit(int nr, unsigned long * addr) |
---|
47 | { |
---|
48 | int mask; |
---|
49 | int retval; |
---|
50 | rtems_interrupt_level level; |
---|
51 | |
---|
52 | addr += nr >> 5; |
---|
53 | mask = 1 << (nr & 0x1f); |
---|
54 | rtems_interrupt_disable(level); |
---|
55 | retval = (mask & *addr) != 0; |
---|
56 | *addr &= ~mask; |
---|
57 | rtems_interrupt_enable(level); |
---|
58 | return retval; |
---|
59 | } |
---|
60 | |
---|
61 | static int test_bit(int nr, unsigned long * addr) |
---|
62 | { |
---|
63 | int mask; |
---|
64 | |
---|
65 | addr += nr >> 5; |
---|
66 | mask = 1 << (nr & 0x1f); |
---|
67 | return ((mask & *addr) != 0); |
---|
68 | } |
---|
69 | |
---|
70 | #define test_and_set_bit(x,y) set_bit(x,y) |
---|
71 | #define test_and_clear_bit(x,y) clear_bit(x,y) |
---|
72 | |
---|
73 | /* |
---|
74 | * global state includes the following, and various static variables |
---|
75 | * in this module: prev_scancode, shift_state, diacr, npadch, dead_key_next. |
---|
76 | * (last_console is now a global variable) |
---|
77 | */ |
---|
78 | #define BITS_PER_LONG (sizeof(long)*CHAR_BIT) |
---|
79 | |
---|
80 | /* shift state counters.. */ |
---|
81 | static unsigned char k_down[NR_SHIFT] = {0, }; |
---|
82 | /* keyboard key bitmap */ |
---|
83 | static unsigned long key_down[256/BITS_PER_LONG] = { 0, }; |
---|
84 | |
---|
85 | static int dead_key_next = 0; |
---|
86 | /* |
---|
87 | * In order to retrieve the shift_state (for the mouse server), either |
---|
88 | * the variable must be global, or a new procedure must be created to |
---|
89 | * return the value. I chose the former way. |
---|
90 | */ |
---|
91 | int shift_state = 0; |
---|
92 | static int npadch = -1; /* -1 or number assembled on pad */ |
---|
93 | static unsigned char diacr = 0; |
---|
94 | static char rep = 0; /* flag telling character repeat */ |
---|
95 | |
---|
96 | /* default console for RTEMS */ |
---|
97 | static int fg_console = 0; |
---|
98 | |
---|
99 | struct kbd_struct kbd_table[MAX_NR_CONSOLES]; |
---|
100 | static struct kbd_struct * kbd = kbd_table; |
---|
101 | |
---|
102 | void compute_shiftstate(void); |
---|
103 | |
---|
104 | typedef void (*k_hand)(unsigned char value, char up_flag); |
---|
105 | typedef void (k_handfn)(unsigned char value, char up_flag); |
---|
106 | |
---|
107 | static k_handfn |
---|
108 | do_self, do_fn, do_spec, do_pad, do_dead, do_cons, do_cur, do_shift, |
---|
109 | do_meta, do_ascii, do_lock, do_lowercase, do_slock, do_dead2, |
---|
110 | do_ignore; |
---|
111 | |
---|
112 | static k_hand key_handler[16] = { |
---|
113 | do_self, do_fn, do_spec, do_pad, do_dead, do_cons, do_cur, do_shift, |
---|
114 | do_meta, do_ascii, do_lock, do_lowercase, do_slock, do_dead2, |
---|
115 | do_ignore, do_ignore |
---|
116 | }; |
---|
117 | |
---|
118 | /* Key types processed even in raw modes */ |
---|
119 | |
---|
120 | #define TYPES_ALLOWED_IN_RAW_MODE ((1 << KT_SPEC) | (1 << KT_SHIFT)) |
---|
121 | |
---|
122 | typedef void (*void_fnp)(void); |
---|
123 | typedef void (void_fn)(void); |
---|
124 | |
---|
125 | static void show_mem(void) |
---|
126 | { |
---|
127 | } |
---|
128 | static void show_state(void) |
---|
129 | { |
---|
130 | } |
---|
131 | |
---|
132 | static void_fn do_null, enter, show_ptregs, send_intr, lastcons, caps_toggle, |
---|
133 | num, hold, scroll_forw, scroll_back, caps_on, compose, |
---|
134 | SAK, decr_console, incr_console, spawn_console, bare_num; |
---|
135 | |
---|
136 | static void_fnp spec_fn_table[] = { |
---|
137 | do_null, enter, show_ptregs, show_mem, |
---|
138 | show_state, send_intr, lastcons, caps_toggle, |
---|
139 | num, hold, scroll_forw, scroll_back, |
---|
140 | bsp_reset, caps_on, compose, SAK, |
---|
141 | decr_console, incr_console, spawn_console, bare_num |
---|
142 | }; |
---|
143 | |
---|
144 | #define SPECIALS_ALLOWED_IN_RAW_MODE (1 << KVAL(K_SAK)) |
---|
145 | |
---|
146 | /* maximum values each key_handler can handle */ |
---|
147 | const int max_vals[] = { |
---|
148 | 255, SIZE(func_table) - 1, SIZE(spec_fn_table) - 1, NR_PAD - 1, |
---|
149 | NR_DEAD - 1, 255, 3, NR_SHIFT - 1, |
---|
150 | 255, NR_ASCII - 1, NR_LOCK - 1, 255, |
---|
151 | NR_LOCK - 1, 255 |
---|
152 | }; |
---|
153 | |
---|
154 | const int NR_TYPES = SIZE(max_vals); |
---|
155 | |
---|
156 | /* N.B. drivers/macintosh/mac_keyb.c needs to call put_queue */ |
---|
157 | static void put_queue(int); |
---|
158 | static unsigned char handle_diacr(unsigned char); |
---|
159 | |
---|
160 | #ifdef CONFIG_MAGIC_SYSRQ |
---|
161 | static int sysrq_pressed; |
---|
162 | #endif |
---|
163 | |
---|
164 | /* |
---|
165 | * Many other routines do put_queue, but I think either |
---|
166 | * they produce ASCII, or they produce some user-assigned |
---|
167 | * string, and in both cases we might assume that it is |
---|
168 | * in utf-8 already. |
---|
169 | */ |
---|
170 | static void to_utf8(ushort c) |
---|
171 | { |
---|
172 | if (c < 0x80) |
---|
173 | put_queue(c); /* 0******* */ |
---|
174 | else if (c < 0x800) { |
---|
175 | put_queue(0xc0 | (c >> 6)); /* 110***** 10****** */ |
---|
176 | put_queue(0x80 | (c & 0x3f)); |
---|
177 | } else { |
---|
178 | put_queue(0xe0 | (c >> 12)); /* 1110**** 10****** 10****** */ |
---|
179 | put_queue(0x80 | ((c >> 6) & 0x3f)); |
---|
180 | put_queue(0x80 | (c & 0x3f)); |
---|
181 | } |
---|
182 | /* UTF-8 is defined for words of up to 31 bits, |
---|
183 | but we need only 16 bits here */ |
---|
184 | } |
---|
185 | |
---|
186 | /* |
---|
187 | * Translation of escaped scancodes to keycodes. |
---|
188 | * This is now user-settable (for machines were it makes sense). |
---|
189 | */ |
---|
190 | |
---|
191 | int setkeycode(unsigned int scancode, unsigned int keycode) |
---|
192 | { |
---|
193 | return kbd_setkeycode(scancode, keycode); |
---|
194 | } |
---|
195 | |
---|
196 | int getkeycode(unsigned int scancode) |
---|
197 | { |
---|
198 | return kbd_getkeycode(scancode); |
---|
199 | } |
---|
200 | |
---|
201 | void handle_scancode(unsigned char scancode, int down) |
---|
202 | { |
---|
203 | unsigned char keycode; |
---|
204 | char up_flag = down ? 0 : 0200; |
---|
205 | char raw_mode; |
---|
206 | |
---|
207 | mark_bh(CONSOLE_BH); |
---|
208 | |
---|
209 | #if 0 |
---|
210 | tty = ttytab? ttytab[fg_console]: NULL; |
---|
211 | if (tty && (!tty->driver_data)) { |
---|
212 | /* |
---|
213 | * We touch the tty structure via the the ttytab array |
---|
214 | * without knowing whether or not tty is open, which |
---|
215 | * is inherently dangerous. We currently rely on that |
---|
216 | * fact that console_open sets tty->driver_data when |
---|
217 | * it opens it, and clears it when it closes it. |
---|
218 | */ |
---|
219 | tty = NULL; |
---|
220 | } |
---|
221 | #endif |
---|
222 | |
---|
223 | kbd = kbd_table + fg_console; |
---|
224 | if ((raw_mode = (kbd->kbdmode == VC_RAW))) { |
---|
225 | put_queue(scancode | up_flag); |
---|
226 | /* we do not return yet, because we want to maintain |
---|
227 | the key_down array, so that we have the correct |
---|
228 | values when finishing RAW mode or when changing VT's */ |
---|
229 | } |
---|
230 | |
---|
231 | /* |
---|
232 | * Convert scancode to keycode |
---|
233 | */ |
---|
234 | if (!kbd_translate(scancode, &keycode, raw_mode)) |
---|
235 | return; |
---|
236 | |
---|
237 | /* |
---|
238 | * At this point the variable `keycode' contains the keycode. |
---|
239 | * Note: the keycode must not be 0 (++Geert: on m68k 0 is valid). |
---|
240 | * We keep track of the up/down status of the key, and |
---|
241 | * return the keycode if in MEDIUMRAW mode. |
---|
242 | */ |
---|
243 | |
---|
244 | if (up_flag) { |
---|
245 | rep = 0; |
---|
246 | if(!test_and_clear_bit(keycode, key_down)) |
---|
247 | up_flag = kbd_unexpected_up(keycode); |
---|
248 | } else |
---|
249 | rep = test_and_set_bit(keycode, key_down); |
---|
250 | |
---|
251 | #ifdef CONFIG_MAGIC_SYSRQ /* Handle the SysRq Hack */ |
---|
252 | if (keycode == SYSRQ_KEY) { |
---|
253 | sysrq_pressed = !up_flag; |
---|
254 | return; |
---|
255 | } else if (sysrq_pressed) { |
---|
256 | if (!up_flag && sysrq_enabled) |
---|
257 | handle_sysrq(kbd_sysrq_xlate[keycode], kbd_pt_regs, kbd, tty); |
---|
258 | return; |
---|
259 | } |
---|
260 | #endif |
---|
261 | |
---|
262 | if (kbd->kbdmode == VC_MEDIUMRAW) { |
---|
263 | /* soon keycodes will require more than one byte */ |
---|
264 | put_queue(keycode + up_flag); |
---|
265 | raw_mode = 1; /* Most key classes will be ignored */ |
---|
266 | } |
---|
267 | /* |
---|
268 | * Small change in philosophy: earlier we defined repetition by |
---|
269 | * rep = keycode == prev_keycode; |
---|
270 | * prev_keycode = keycode; |
---|
271 | * but now by the fact that the depressed key was down already. |
---|
272 | * Does this ever make a difference? Yes. |
---|
273 | */ |
---|
274 | |
---|
275 | /* |
---|
276 | * Repeat a key only if the input buffers are empty or the |
---|
277 | * characters get echoed locally. This makes key repeat usable |
---|
278 | * with slow applications and under heavy loads. |
---|
279 | */ |
---|
280 | if (!rep || vc_kbd_mode(kbd,VC_REPEAT) ) { |
---|
281 | /* |
---|
282 | || (vc_kbd_mode(kbd,VC_REPEAT) && tty && |
---|
283 | (L_ECHO(tty) || (tty->driver.chars_in_buffer(tty) == 0)))) { |
---|
284 | */ |
---|
285 | u_short keysym; |
---|
286 | u_char type; |
---|
287 | |
---|
288 | /* the XOR below used to be an OR */ |
---|
289 | int shift_final = shift_state ^ kbd->lockstate ^ kbd->slockstate; |
---|
290 | ushort *key_map = key_maps[shift_final]; |
---|
291 | |
---|
292 | if (key_map != NULL) { |
---|
293 | keysym = key_map[keycode]; |
---|
294 | type = KTYP(keysym); |
---|
295 | |
---|
296 | if (type >= 0xf0) { |
---|
297 | type -= 0xf0; |
---|
298 | if (raw_mode && ! (TYPES_ALLOWED_IN_RAW_MODE & (1 << type))) |
---|
299 | return; |
---|
300 | if (type == KT_LETTER) { |
---|
301 | type = KT_LATIN; |
---|
302 | if (vc_kbd_led(kbd, VC_CAPSLOCK)) { |
---|
303 | key_map = key_maps[shift_final ^ (1<<KG_SHIFT)]; |
---|
304 | if (key_map) |
---|
305 | keysym = key_map[keycode]; |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | (*key_handler[type])(keysym & 0xff, up_flag); |
---|
310 | |
---|
311 | if (type != KT_SLOCK) |
---|
312 | kbd->slockstate = 0; |
---|
313 | |
---|
314 | } else { |
---|
315 | /* maybe only if (kbd->kbdmode == VC_UNICODE) ? */ |
---|
316 | if (!up_flag && !raw_mode) |
---|
317 | to_utf8(keysym); |
---|
318 | } |
---|
319 | } else { |
---|
320 | /* maybe beep? */ |
---|
321 | /* we have at least to update shift_state */ |
---|
322 | #if 1 /* how? two almost equivalent choices follow */ |
---|
323 | compute_shiftstate(); |
---|
324 | #else |
---|
325 | keysym = U(plain_map[keycode]); |
---|
326 | type = KTYP(keysym); |
---|
327 | if (type == KT_SHIFT) |
---|
328 | (*key_handler[type])(keysym & 0xff, up_flag); |
---|
329 | #endif |
---|
330 | } |
---|
331 | } |
---|
332 | } |
---|
333 | |
---|
334 | static void ( *driver_input_handler_kbd )( void *, unsigned short, unsigned long ) = 0; |
---|
335 | /* |
---|
336 | */ |
---|
337 | void kbd_set_driver_handler( |
---|
338 | void ( *handler )( void *, unsigned short, unsigned long ) |
---|
339 | ) |
---|
340 | { |
---|
341 | driver_input_handler_kbd = handler; |
---|
342 | } |
---|
343 | |
---|
344 | static void put_queue(int ch) |
---|
345 | { |
---|
346 | if ( driver_input_handler_kbd ) { |
---|
347 | driver_input_handler_kbd( ( void *)kbd, (unsigned short)ch, 0 ); |
---|
348 | } else { |
---|
349 | add_to_queue( ch ); |
---|
350 | } |
---|
351 | } |
---|
352 | |
---|
353 | static void puts_queue(char *cp) |
---|
354 | { |
---|
355 | while (*cp) { |
---|
356 | put_queue( *cp ); |
---|
357 | cp++; |
---|
358 | } |
---|
359 | } |
---|
360 | |
---|
361 | static void applkey(int key, char mode) |
---|
362 | { |
---|
363 | static char buf[] = { 0x1b, 'O', 0x00, 0x00 }; |
---|
364 | |
---|
365 | buf[1] = (mode ? 'O' : '['); |
---|
366 | buf[2] = key; |
---|
367 | puts_queue(buf); |
---|
368 | } |
---|
369 | |
---|
370 | static void enter(void) |
---|
371 | { |
---|
372 | if (diacr) { |
---|
373 | put_queue(diacr); |
---|
374 | diacr = 0; |
---|
375 | } |
---|
376 | put_queue(13); |
---|
377 | |
---|
378 | if (vc_kbd_mode(kbd,VC_CRLF)) |
---|
379 | put_queue(10); |
---|
380 | } |
---|
381 | |
---|
382 | static void caps_toggle(void) |
---|
383 | { |
---|
384 | if (rep) |
---|
385 | return; |
---|
386 | chg_vc_kbd_led(kbd, VC_CAPSLOCK); |
---|
387 | } |
---|
388 | |
---|
389 | static void caps_on(void) |
---|
390 | { |
---|
391 | if (rep) |
---|
392 | return; |
---|
393 | set_vc_kbd_led(kbd, VC_CAPSLOCK); |
---|
394 | } |
---|
395 | |
---|
396 | static void show_ptregs(void) |
---|
397 | { |
---|
398 | } |
---|
399 | |
---|
400 | static void hold(void) |
---|
401 | { |
---|
402 | if (rep ) |
---|
403 | return; |
---|
404 | chg_vc_kbd_led(kbd, VC_SCROLLOCK ); |
---|
405 | } |
---|
406 | |
---|
407 | static void num(void) |
---|
408 | { |
---|
409 | if (vc_kbd_mode(kbd,VC_APPLIC)) |
---|
410 | applkey('P', 1); |
---|
411 | else |
---|
412 | bare_num(); |
---|
413 | } |
---|
414 | |
---|
415 | /* |
---|
416 | * Bind this to Shift-NumLock if you work in application keypad mode |
---|
417 | * but want to be able to change the NumLock flag. |
---|
418 | * Bind this to NumLock if you prefer that the NumLock key always |
---|
419 | * changes the NumLock flag. |
---|
420 | */ |
---|
421 | static void bare_num(void) |
---|
422 | { |
---|
423 | if (!rep) |
---|
424 | chg_vc_kbd_led(kbd,VC_NUMLOCK); |
---|
425 | } |
---|
426 | |
---|
427 | static void lastcons(void) |
---|
428 | { |
---|
429 | } |
---|
430 | |
---|
431 | static void decr_console(void) |
---|
432 | { |
---|
433 | } |
---|
434 | |
---|
435 | static void incr_console(void) |
---|
436 | { |
---|
437 | } |
---|
438 | |
---|
439 | static void send_intr(void) |
---|
440 | { |
---|
441 | } |
---|
442 | |
---|
443 | static void scroll_forw(void) |
---|
444 | { |
---|
445 | } |
---|
446 | |
---|
447 | static void scroll_back(void) |
---|
448 | { |
---|
449 | } |
---|
450 | |
---|
451 | static void compose(void) |
---|
452 | { |
---|
453 | dead_key_next = 1; |
---|
454 | } |
---|
455 | |
---|
456 | int spawnpid, spawnsig; |
---|
457 | |
---|
458 | static void spawn_console(void) |
---|
459 | { |
---|
460 | } |
---|
461 | |
---|
462 | static void SAK(void) |
---|
463 | { |
---|
464 | } |
---|
465 | |
---|
466 | static void do_ignore(unsigned char value, char up_flag) |
---|
467 | { |
---|
468 | } |
---|
469 | |
---|
470 | static void do_null() |
---|
471 | { |
---|
472 | compute_shiftstate(); |
---|
473 | } |
---|
474 | |
---|
475 | static void do_spec(unsigned char value, char up_flag) |
---|
476 | { |
---|
477 | if (up_flag) |
---|
478 | return; |
---|
479 | if (value >= SIZE(spec_fn_table)) |
---|
480 | return; |
---|
481 | |
---|
482 | if ((kbd->kbdmode == VC_RAW || kbd->kbdmode == VC_MEDIUMRAW) && |
---|
483 | !(SPECIALS_ALLOWED_IN_RAW_MODE & (1 << value))) |
---|
484 | return; |
---|
485 | |
---|
486 | spec_fn_table[value](); |
---|
487 | } |
---|
488 | |
---|
489 | static void do_lowercase(unsigned char value, char up_flag) |
---|
490 | { |
---|
491 | } |
---|
492 | |
---|
493 | static void do_self(unsigned char value, char up_flag) |
---|
494 | { |
---|
495 | if (up_flag) |
---|
496 | return; /* no action, if this is a key release */ |
---|
497 | |
---|
498 | if (diacr) |
---|
499 | value = handle_diacr(value); |
---|
500 | |
---|
501 | if (dead_key_next) { |
---|
502 | dead_key_next = 0; |
---|
503 | diacr = value; |
---|
504 | return; |
---|
505 | } |
---|
506 | put_queue(value); |
---|
507 | } |
---|
508 | |
---|
509 | #define A_GRAVE '`' |
---|
510 | #define A_ACUTE '\'' |
---|
511 | #define A_CFLEX '^' |
---|
512 | #define A_TILDE '~' |
---|
513 | #define A_DIAER '"' |
---|
514 | #define A_CEDIL ',' |
---|
515 | static unsigned char ret_diacr[NR_DEAD] = |
---|
516 | {A_GRAVE, A_ACUTE, A_CFLEX, A_TILDE, A_DIAER, A_CEDIL }; |
---|
517 | |
---|
518 | /* Obsolete - for backwards compatibility only */ |
---|
519 | static void do_dead(unsigned char value, char up_flag) |
---|
520 | { |
---|
521 | value = ret_diacr[value]; |
---|
522 | printk( " do_dead( %X ) ", value ); |
---|
523 | do_dead2(value,up_flag); |
---|
524 | } |
---|
525 | |
---|
526 | /* |
---|
527 | * Handle dead key. Note that we now may have several |
---|
528 | * dead keys modifying the same character. Very useful |
---|
529 | * for Vietnamese. |
---|
530 | */ |
---|
531 | static void do_dead2(unsigned char value, char up_flag) |
---|
532 | { |
---|
533 | if (up_flag) |
---|
534 | return; |
---|
535 | diacr = (diacr ? handle_diacr(value) : value); |
---|
536 | } |
---|
537 | |
---|
538 | /* |
---|
539 | * We have a combining character DIACR here, followed by the character CH. |
---|
540 | * If the combination occurs in the table, return the corresponding value. |
---|
541 | * Otherwise, if CH is a space or equals DIACR, return DIACR. |
---|
542 | * Otherwise, conclude that DIACR was not combining after all, |
---|
543 | * queue it and return CH. |
---|
544 | */ |
---|
545 | unsigned char handle_diacr(unsigned char ch) |
---|
546 | { |
---|
547 | int d = diacr; |
---|
548 | int i; |
---|
549 | |
---|
550 | diacr = 0; |
---|
551 | |
---|
552 | for (i = 0; i < accent_table_size; i++) { |
---|
553 | if (accent_table[i].diacr == d && accent_table[i].base == ch) |
---|
554 | return accent_table[i].result; |
---|
555 | } |
---|
556 | if (ch == ' ' || ch == d) |
---|
557 | return d; |
---|
558 | |
---|
559 | put_queue(d); |
---|
560 | return ch; |
---|
561 | } |
---|
562 | |
---|
563 | static void do_cons(unsigned char value, char up_flag) |
---|
564 | { |
---|
565 | if (up_flag) |
---|
566 | return; |
---|
567 | } |
---|
568 | |
---|
569 | static void do_fn(unsigned char value, char up_flag) |
---|
570 | { |
---|
571 | if (up_flag) |
---|
572 | return; |
---|
573 | |
---|
574 | if (value < SIZE(func_table)) { |
---|
575 | if (func_table[value]) |
---|
576 | puts_queue(func_table[value]); |
---|
577 | } else |
---|
578 | printk( "do_fn called with value=%d\n", value); |
---|
579 | } |
---|
580 | |
---|
581 | static void do_pad(unsigned char value, char up_flag) |
---|
582 | { |
---|
583 | static const char *pad_chars = "0123456789+-*/\015,.?()"; |
---|
584 | static const char *app_map = "pqrstuvwxylSRQMnnmPQ"; |
---|
585 | |
---|
586 | if (up_flag) |
---|
587 | return; /* no action, if this is a key release */ |
---|
588 | |
---|
589 | /* kludge... shift forces cursor/number keys */ |
---|
590 | if (vc_kbd_mode(kbd,VC_APPLIC) && !k_down[KG_SHIFT]) { |
---|
591 | applkey(app_map[value], 1); |
---|
592 | return; |
---|
593 | } |
---|
594 | if (!vc_kbd_led(kbd,VC_NUMLOCK)) |
---|
595 | switch (value) { |
---|
596 | case KVAL(K_PCOMMA): |
---|
597 | case KVAL(K_PDOT): |
---|
598 | do_fn(KVAL(K_REMOVE), 0); |
---|
599 | return; |
---|
600 | case KVAL(K_P0): |
---|
601 | do_fn(KVAL(K_INSERT), 0); |
---|
602 | return; |
---|
603 | case KVAL(K_P1): |
---|
604 | do_fn(KVAL(K_SELECT), 0); |
---|
605 | return; |
---|
606 | case KVAL(K_P2): |
---|
607 | do_cur(KVAL(K_DOWN), 0); |
---|
608 | return; |
---|
609 | case KVAL(K_P3): |
---|
610 | do_fn(KVAL(K_PGDN), 0); |
---|
611 | return; |
---|
612 | case KVAL(K_P4): |
---|
613 | do_cur(KVAL(K_LEFT), 0); |
---|
614 | return; |
---|
615 | case KVAL(K_P6): |
---|
616 | do_cur(KVAL(K_RIGHT), 0); |
---|
617 | return; |
---|
618 | case KVAL(K_P7): |
---|
619 | do_fn(KVAL(K_FIND), 0); |
---|
620 | return; |
---|
621 | case KVAL(K_P8): |
---|
622 | do_cur(KVAL(K_UP), 0); |
---|
623 | return; |
---|
624 | case KVAL(K_P9): |
---|
625 | do_fn(KVAL(K_PGUP), 0); |
---|
626 | return; |
---|
627 | case KVAL(K_P5): |
---|
628 | applkey('G', vc_kbd_mode(kbd, VC_APPLIC)); |
---|
629 | return; |
---|
630 | } |
---|
631 | |
---|
632 | put_queue(pad_chars[value]); |
---|
633 | |
---|
634 | if (value == KVAL(K_PENTER) && vc_kbd_mode(kbd, VC_CRLF)) |
---|
635 | put_queue(10); |
---|
636 | |
---|
637 | } |
---|
638 | |
---|
639 | static void do_cur(unsigned char value, char up_flag) |
---|
640 | { |
---|
641 | static const char *cur_chars = "BDCA"; |
---|
642 | if (up_flag) |
---|
643 | return; |
---|
644 | |
---|
645 | applkey(cur_chars[value], vc_kbd_mode(kbd,VC_CKMODE)); |
---|
646 | } |
---|
647 | |
---|
648 | static void do_shift(unsigned char value, char up_flag) |
---|
649 | { |
---|
650 | int old_state = shift_state; |
---|
651 | |
---|
652 | if (rep) |
---|
653 | return; |
---|
654 | |
---|
655 | /* Mimic typewriter: |
---|
656 | a CapsShift key acts like Shift but undoes CapsLock */ |
---|
657 | if (value == KVAL(K_CAPSSHIFT)) { |
---|
658 | value = KVAL(K_SHIFT); |
---|
659 | if (!up_flag) |
---|
660 | clr_vc_kbd_led(kbd, VC_CAPSLOCK); |
---|
661 | } |
---|
662 | |
---|
663 | if (up_flag) { |
---|
664 | /* handle the case that two shift or control |
---|
665 | keys are depressed simultaneously */ |
---|
666 | if (k_down[value]) |
---|
667 | k_down[value]--; |
---|
668 | } else |
---|
669 | k_down[value]++; |
---|
670 | |
---|
671 | if (k_down[value]) |
---|
672 | shift_state |= (1 << value); |
---|
673 | else |
---|
674 | shift_state &= ~ (1 << value); |
---|
675 | |
---|
676 | /* kludge */ |
---|
677 | if (up_flag && shift_state != old_state && npadch != -1) { |
---|
678 | if (kbd->kbdmode == VC_UNICODE) |
---|
679 | to_utf8(npadch & 0xffff); |
---|
680 | else |
---|
681 | put_queue(npadch & 0xff); |
---|
682 | npadch = -1; |
---|
683 | } |
---|
684 | } |
---|
685 | |
---|
686 | /* called after returning from RAW mode or when changing consoles - |
---|
687 | recompute k_down[] and shift_state from key_down[] */ |
---|
688 | /* maybe called when keymap is undefined, so that shiftkey release is seen */ |
---|
689 | void compute_shiftstate(void) |
---|
690 | { |
---|
691 | int i, j, k, sym, val; |
---|
692 | |
---|
693 | shift_state = 0; |
---|
694 | for(i=0; i < SIZE(k_down); i++) |
---|
695 | k_down[i] = 0; |
---|
696 | |
---|
697 | for(i=0; i < SIZE(key_down); i++) |
---|
698 | if(key_down[i]) { /* skip this word if not a single bit on */ |
---|
699 | k = i*BITS_PER_LONG; |
---|
700 | for(j=0; j<BITS_PER_LONG; j++,k++) |
---|
701 | if(test_bit(k, key_down)) { |
---|
702 | sym = U(plain_map[k]); |
---|
703 | if(KTYP(sym) == KT_SHIFT) { |
---|
704 | val = KVAL(sym); |
---|
705 | if (val == KVAL(K_CAPSSHIFT)) |
---|
706 | val = KVAL(K_SHIFT); |
---|
707 | k_down[val]++; |
---|
708 | shift_state |= (1<<val); |
---|
709 | } |
---|
710 | } |
---|
711 | } |
---|
712 | } |
---|
713 | |
---|
714 | static void do_meta(unsigned char value, char up_flag) |
---|
715 | { |
---|
716 | if (up_flag) |
---|
717 | return; |
---|
718 | |
---|
719 | if (vc_kbd_mode(kbd, VC_META)) { |
---|
720 | put_queue('\033'); |
---|
721 | put_queue(value); |
---|
722 | } else |
---|
723 | put_queue(value | 0x80); |
---|
724 | } |
---|
725 | |
---|
726 | static void do_ascii(unsigned char value, char up_flag) |
---|
727 | { |
---|
728 | int base; |
---|
729 | |
---|
730 | if (up_flag) |
---|
731 | return; |
---|
732 | |
---|
733 | if (value < 10) /* decimal input of code, while Alt depressed */ |
---|
734 | base = 10; |
---|
735 | else { /* hexadecimal input of code, while AltGr depressed */ |
---|
736 | value -= 10; |
---|
737 | base = 16; |
---|
738 | } |
---|
739 | |
---|
740 | if (npadch == -1) |
---|
741 | npadch = value; |
---|
742 | else |
---|
743 | npadch = npadch * base + value; |
---|
744 | } |
---|
745 | |
---|
746 | static void do_lock(unsigned char value, char up_flag) |
---|
747 | { |
---|
748 | if (up_flag || rep) |
---|
749 | return; |
---|
750 | chg_vc_kbd_lock(kbd, value); |
---|
751 | } |
---|
752 | |
---|
753 | static void do_slock(unsigned char value, char up_flag) |
---|
754 | { |
---|
755 | if (up_flag || rep) |
---|
756 | return; |
---|
757 | |
---|
758 | chg_vc_kbd_slock(kbd, value); |
---|
759 | } |
---|
760 | |
---|
761 | /* |
---|
762 | * The leds display either (i) the status of NumLock, CapsLock, ScrollLock, |
---|
763 | * or (ii) whatever pattern of lights people want to show using KDSETLED, |
---|
764 | * or (iii) specified bits of specified words in kernel memory. |
---|
765 | */ |
---|
766 | |
---|
767 | static unsigned char ledstate = 0xff; /* undefined */ |
---|
768 | static unsigned char ledioctl; |
---|
769 | |
---|
770 | unsigned char getledstate(void) { |
---|
771 | return ledstate; |
---|
772 | } |
---|
773 | |
---|
774 | void setledstate(struct kbd_struct *kbd, unsigned int led) { |
---|
775 | if (!(led & ~7)) { |
---|
776 | ledioctl = led; |
---|
777 | kbd->ledmode = LED_SHOW_IOCTL; |
---|
778 | } else |
---|
779 | ; |
---|
780 | kbd->ledmode = LED_SHOW_FLAGS; |
---|
781 | set_leds(); |
---|
782 | } |
---|
783 | |
---|
784 | static struct ledptr { |
---|
785 | unsigned int *addr; |
---|
786 | unsigned int mask; |
---|
787 | unsigned char valid:1; |
---|
788 | } ledptrs[3]; |
---|
789 | |
---|
790 | void register_leds( |
---|
791 | int console, |
---|
792 | unsigned int led, |
---|
793 | unsigned int *addr, |
---|
794 | unsigned int mask |
---|
795 | ) |
---|
796 | { |
---|
797 | struct kbd_struct *kbd = kbd_table + console; |
---|
798 | |
---|
799 | if (led < 3) { |
---|
800 | ledptrs[led].addr = addr; |
---|
801 | ledptrs[led].mask = mask; |
---|
802 | ledptrs[led].valid = 1; |
---|
803 | kbd->ledmode = LED_SHOW_MEM; |
---|
804 | } else |
---|
805 | kbd->ledmode = LED_SHOW_FLAGS; |
---|
806 | } |
---|
807 | |
---|
808 | static inline unsigned char getleds(void) |
---|
809 | { |
---|
810 | |
---|
811 | struct kbd_struct *kbd = kbd_table + fg_console; |
---|
812 | |
---|
813 | unsigned char leds; |
---|
814 | |
---|
815 | if (kbd->ledmode == LED_SHOW_IOCTL) |
---|
816 | return ledioctl; |
---|
817 | leds = kbd->ledflagstate; |
---|
818 | if (kbd->ledmode == LED_SHOW_MEM) { |
---|
819 | if (ledptrs[0].valid) { |
---|
820 | if (*ledptrs[0].addr & ledptrs[0].mask) |
---|
821 | leds |= 1; |
---|
822 | else |
---|
823 | leds &= ~1; |
---|
824 | } |
---|
825 | if (ledptrs[1].valid) { |
---|
826 | if (*ledptrs[1].addr & ledptrs[1].mask) |
---|
827 | leds |= 2; |
---|
828 | else |
---|
829 | leds &= ~2; |
---|
830 | } |
---|
831 | if (ledptrs[2].valid) { |
---|
832 | if (*ledptrs[2].addr & ledptrs[2].mask) |
---|
833 | leds |= 4; |
---|
834 | else |
---|
835 | leds &= ~4; |
---|
836 | } |
---|
837 | } |
---|
838 | return leds; |
---|
839 | } |
---|
840 | |
---|
841 | /* |
---|
842 | * This routine is the bottom half of the keyboard interrupt |
---|
843 | * routine, and runs with all interrupts enabled. It does |
---|
844 | * console changing, led setting and copy_to_cooked, which can |
---|
845 | * take a reasonably long time. |
---|
846 | * |
---|
847 | * Aside from timing (which isn't really that important for |
---|
848 | * keyboard interrupts as they happen often), using the software |
---|
849 | * interrupt routines for this thing allows us to easily mask |
---|
850 | * this when we don't want any of the above to happen. Not yet |
---|
851 | * used, but this allows for easy and efficient race-condition |
---|
852 | * prevention later on. |
---|
853 | */ |
---|
854 | static void kbd_bh(void) |
---|
855 | { |
---|
856 | unsigned char leds = getleds(); |
---|
857 | if (leds != ledstate) { |
---|
858 | ledstate = leds; |
---|
859 | kbd_leds(leds); |
---|
860 | } |
---|
861 | } |
---|
862 | |
---|
863 | void set_leds(void) |
---|
864 | { |
---|
865 | kbd_bh(); |
---|
866 | } |
---|
867 | |
---|
868 | int kbd_init(void) |
---|
869 | { |
---|
870 | |
---|
871 | int i; |
---|
872 | struct kbd_struct kbd0; |
---|
873 | kbd0.ledflagstate = kbd0.default_ledflagstate = KBD_DEFLEDS; |
---|
874 | kbd0.ledmode = LED_SHOW_MEM; |
---|
875 | kbd0.lockstate = KBD_DEFLOCK; |
---|
876 | kbd0.slockstate = 0; |
---|
877 | kbd0.modeflags = KBD_DEFMODE; |
---|
878 | kbd0.kbdmode = VC_XLATE; |
---|
879 | |
---|
880 | for (i = 0 ; i < MAX_NR_CONSOLES ; i++) |
---|
881 | kbd_table[i] = kbd0; |
---|
882 | |
---|
883 | kbd_init_hw(); |
---|
884 | mark_bh(KEYBOARD_BH); |
---|
885 | return 0; |
---|
886 | } |
---|