PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / s390 / char / keyboard.c
blob01463b052ae740aa56cd6ea816102a34961ed668
1 /*
2 * ebcdic keycode functions for s390 console drivers
4 * S390 version
5 * Copyright IBM Corp. 2003
6 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
7 */
9 #include <linux/module.h>
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/sysrq.h>
14 #include <linux/consolemap.h>
15 #include <linux/kbd_kern.h>
16 #include <linux/kbd_diacr.h>
17 #include <asm/uaccess.h>
19 #include "keyboard.h"
22 * Handler Tables.
24 #define K_HANDLERS\
25 k_self, k_fn, k_spec, k_ignore,\
26 k_dead, k_ignore, k_ignore, k_ignore,\
27 k_ignore, k_ignore, k_ignore, k_ignore,\
28 k_ignore, k_ignore, k_ignore, k_ignore
30 typedef void (k_handler_fn)(struct kbd_data *, unsigned char);
31 static k_handler_fn K_HANDLERS;
32 static k_handler_fn *k_handler[16] = { K_HANDLERS };
34 /* maximum values each key_handler can handle */
35 static const int kbd_max_vals[] = {
36 255, ARRAY_SIZE(func_table) - 1, NR_FN_HANDLER - 1, 0,
37 NR_DEAD - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
39 static const int KBD_NR_TYPES = ARRAY_SIZE(kbd_max_vals);
41 static unsigned char ret_diacr[NR_DEAD] = {
42 '`', '\'', '^', '~', '"', ','
46 * Alloc/free of kbd_data structures.
48 struct kbd_data *
49 kbd_alloc(void) {
50 struct kbd_data *kbd;
51 int i;
53 kbd = kzalloc(sizeof(struct kbd_data), GFP_KERNEL);
54 if (!kbd)
55 goto out;
56 kbd->key_maps = kzalloc(sizeof(key_maps), GFP_KERNEL);
57 if (!kbd->key_maps)
58 goto out_kbd;
59 for (i = 0; i < ARRAY_SIZE(key_maps); i++) {
60 if (key_maps[i]) {
61 kbd->key_maps[i] = kmemdup(key_maps[i],
62 sizeof(u_short) * NR_KEYS,
63 GFP_KERNEL);
64 if (!kbd->key_maps[i])
65 goto out_maps;
68 kbd->func_table = kzalloc(sizeof(func_table), GFP_KERNEL);
69 if (!kbd->func_table)
70 goto out_maps;
71 for (i = 0; i < ARRAY_SIZE(func_table); i++) {
72 if (func_table[i]) {
73 kbd->func_table[i] = kstrdup(func_table[i],
74 GFP_KERNEL);
75 if (!kbd->func_table[i])
76 goto out_func;
79 kbd->fn_handler =
80 kzalloc(sizeof(fn_handler_fn *) * NR_FN_HANDLER, GFP_KERNEL);
81 if (!kbd->fn_handler)
82 goto out_func;
83 kbd->accent_table = kmemdup(accent_table,
84 sizeof(struct kbdiacruc) * MAX_DIACR,
85 GFP_KERNEL);
86 if (!kbd->accent_table)
87 goto out_fn_handler;
88 kbd->accent_table_size = accent_table_size;
89 return kbd;
91 out_fn_handler:
92 kfree(kbd->fn_handler);
93 out_func:
94 for (i = 0; i < ARRAY_SIZE(func_table); i++)
95 kfree(kbd->func_table[i]);
96 kfree(kbd->func_table);
97 out_maps:
98 for (i = 0; i < ARRAY_SIZE(key_maps); i++)
99 kfree(kbd->key_maps[i]);
100 kfree(kbd->key_maps);
101 out_kbd:
102 kfree(kbd);
103 out:
104 return NULL;
107 void
108 kbd_free(struct kbd_data *kbd)
110 int i;
112 kfree(kbd->accent_table);
113 kfree(kbd->fn_handler);
114 for (i = 0; i < ARRAY_SIZE(func_table); i++)
115 kfree(kbd->func_table[i]);
116 kfree(kbd->func_table);
117 for (i = 0; i < ARRAY_SIZE(key_maps); i++)
118 kfree(kbd->key_maps[i]);
119 kfree(kbd->key_maps);
120 kfree(kbd);
124 * Generate ascii -> ebcdic translation table from kbd_data.
126 void
127 kbd_ascebc(struct kbd_data *kbd, unsigned char *ascebc)
129 unsigned short *keymap, keysym;
130 int i, j, k;
132 memset(ascebc, 0x40, 256);
133 for (i = 0; i < ARRAY_SIZE(key_maps); i++) {
134 keymap = kbd->key_maps[i];
135 if (!keymap)
136 continue;
137 for (j = 0; j < NR_KEYS; j++) {
138 k = ((i & 1) << 7) + j;
139 keysym = keymap[j];
140 if (KTYP(keysym) == (KT_LATIN | 0xf0) ||
141 KTYP(keysym) == (KT_LETTER | 0xf0))
142 ascebc[KVAL(keysym)] = k;
143 else if (KTYP(keysym) == (KT_DEAD | 0xf0))
144 ascebc[ret_diacr[KVAL(keysym)]] = k;
149 #if 0
151 * Generate ebcdic -> ascii translation table from kbd_data.
153 void
154 kbd_ebcasc(struct kbd_data *kbd, unsigned char *ebcasc)
156 unsigned short *keymap, keysym;
157 int i, j, k;
159 memset(ebcasc, ' ', 256);
160 for (i = 0; i < ARRAY_SIZE(key_maps); i++) {
161 keymap = kbd->key_maps[i];
162 if (!keymap)
163 continue;
164 for (j = 0; j < NR_KEYS; j++) {
165 keysym = keymap[j];
166 k = ((i & 1) << 7) + j;
167 if (KTYP(keysym) == (KT_LATIN | 0xf0) ||
168 KTYP(keysym) == (KT_LETTER | 0xf0))
169 ebcasc[k] = KVAL(keysym);
170 else if (KTYP(keysym) == (KT_DEAD | 0xf0))
171 ebcasc[k] = ret_diacr[KVAL(keysym)];
175 #endif
178 * We have a combining character DIACR here, followed by the character CH.
179 * If the combination occurs in the table, return the corresponding value.
180 * Otherwise, if CH is a space or equals DIACR, return DIACR.
181 * Otherwise, conclude that DIACR was not combining after all,
182 * queue it and return CH.
184 static unsigned int
185 handle_diacr(struct kbd_data *kbd, unsigned int ch)
187 int i, d;
189 d = kbd->diacr;
190 kbd->diacr = 0;
192 for (i = 0; i < kbd->accent_table_size; i++) {
193 if (kbd->accent_table[i].diacr == d &&
194 kbd->accent_table[i].base == ch)
195 return kbd->accent_table[i].result;
198 if (ch == ' ' || ch == d)
199 return d;
201 kbd_put_queue(kbd->port, d);
202 return ch;
206 * Handle dead key.
208 static void
209 k_dead(struct kbd_data *kbd, unsigned char value)
211 value = ret_diacr[value];
212 kbd->diacr = (kbd->diacr ? handle_diacr(kbd, value) : value);
216 * Normal character handler.
218 static void
219 k_self(struct kbd_data *kbd, unsigned char value)
221 if (kbd->diacr)
222 value = handle_diacr(kbd, value);
223 kbd_put_queue(kbd->port, value);
227 * Special key handlers
229 static void
230 k_ignore(struct kbd_data *kbd, unsigned char value)
235 * Function key handler.
237 static void
238 k_fn(struct kbd_data *kbd, unsigned char value)
240 if (kbd->func_table[value])
241 kbd_puts_queue(kbd->port, kbd->func_table[value]);
244 static void
245 k_spec(struct kbd_data *kbd, unsigned char value)
247 if (value >= NR_FN_HANDLER)
248 return;
249 if (kbd->fn_handler[value])
250 kbd->fn_handler[value](kbd);
254 * Put utf8 character to tty flip buffer.
255 * UTF-8 is defined for words of up to 31 bits,
256 * but we need only 16 bits here
258 static void
259 to_utf8(struct tty_port *port, ushort c)
261 if (c < 0x80)
262 /* 0******* */
263 kbd_put_queue(port, c);
264 else if (c < 0x800) {
265 /* 110***** 10****** */
266 kbd_put_queue(port, 0xc0 | (c >> 6));
267 kbd_put_queue(port, 0x80 | (c & 0x3f));
268 } else {
269 /* 1110**** 10****** 10****** */
270 kbd_put_queue(port, 0xe0 | (c >> 12));
271 kbd_put_queue(port, 0x80 | ((c >> 6) & 0x3f));
272 kbd_put_queue(port, 0x80 | (c & 0x3f));
277 * Process keycode.
279 void
280 kbd_keycode(struct kbd_data *kbd, unsigned int keycode)
282 unsigned short keysym;
283 unsigned char type, value;
285 if (!kbd)
286 return;
288 if (keycode >= 384)
289 keysym = kbd->key_maps[5][keycode - 384];
290 else if (keycode >= 256)
291 keysym = kbd->key_maps[4][keycode - 256];
292 else if (keycode >= 128)
293 keysym = kbd->key_maps[1][keycode - 128];
294 else
295 keysym = kbd->key_maps[0][keycode];
297 type = KTYP(keysym);
298 if (type >= 0xf0) {
299 type -= 0xf0;
300 if (type == KT_LETTER)
301 type = KT_LATIN;
302 value = KVAL(keysym);
303 #ifdef CONFIG_MAGIC_SYSRQ /* Handle the SysRq Hack */
304 if (kbd->sysrq) {
305 if (kbd->sysrq == K(KT_LATIN, '-')) {
306 kbd->sysrq = 0;
307 handle_sysrq(value);
308 return;
310 if (value == '-') {
311 kbd->sysrq = K(KT_LATIN, '-');
312 return;
314 /* Incomplete sysrq sequence. */
315 (*k_handler[KTYP(kbd->sysrq)])(kbd, KVAL(kbd->sysrq));
316 kbd->sysrq = 0;
317 } else if ((type == KT_LATIN && value == '^') ||
318 (type == KT_DEAD && ret_diacr[value] == '^')) {
319 kbd->sysrq = K(type, value);
320 return;
322 #endif
323 (*k_handler[type])(kbd, value);
324 } else
325 to_utf8(kbd->port, keysym);
329 * Ioctl stuff.
331 static int
332 do_kdsk_ioctl(struct kbd_data *kbd, struct kbentry __user *user_kbe,
333 int cmd, int perm)
335 struct kbentry tmp;
336 ushort *key_map, val, ov;
338 if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
339 return -EFAULT;
340 #if NR_KEYS < 256
341 if (tmp.kb_index >= NR_KEYS)
342 return -EINVAL;
343 #endif
344 #if MAX_NR_KEYMAPS < 256
345 if (tmp.kb_table >= MAX_NR_KEYMAPS)
346 return -EINVAL;
347 #endif
349 switch (cmd) {
350 case KDGKBENT:
351 key_map = kbd->key_maps[tmp.kb_table];
352 if (key_map) {
353 val = U(key_map[tmp.kb_index]);
354 if (KTYP(val) >= KBD_NR_TYPES)
355 val = K_HOLE;
356 } else
357 val = (tmp.kb_index ? K_HOLE : K_NOSUCHMAP);
358 return put_user(val, &user_kbe->kb_value);
359 case KDSKBENT:
360 if (!perm)
361 return -EPERM;
362 if (!tmp.kb_index && tmp.kb_value == K_NOSUCHMAP) {
363 /* disallocate map */
364 key_map = kbd->key_maps[tmp.kb_table];
365 if (key_map) {
366 kbd->key_maps[tmp.kb_table] = NULL;
367 kfree(key_map);
369 break;
372 if (KTYP(tmp.kb_value) >= KBD_NR_TYPES)
373 return -EINVAL;
374 if (KVAL(tmp.kb_value) > kbd_max_vals[KTYP(tmp.kb_value)])
375 return -EINVAL;
377 if (!(key_map = kbd->key_maps[tmp.kb_table])) {
378 int j;
380 key_map = kmalloc(sizeof(plain_map),
381 GFP_KERNEL);
382 if (!key_map)
383 return -ENOMEM;
384 kbd->key_maps[tmp.kb_table] = key_map;
385 for (j = 0; j < NR_KEYS; j++)
386 key_map[j] = U(K_HOLE);
388 ov = U(key_map[tmp.kb_index]);
389 if (tmp.kb_value == ov)
390 break; /* nothing to do */
392 * Attention Key.
394 if (((ov == K_SAK) || (tmp.kb_value == K_SAK)) &&
395 !capable(CAP_SYS_ADMIN))
396 return -EPERM;
397 key_map[tmp.kb_index] = U(tmp.kb_value);
398 break;
400 return 0;
403 static int
404 do_kdgkb_ioctl(struct kbd_data *kbd, struct kbsentry __user *u_kbs,
405 int cmd, int perm)
407 unsigned char kb_func;
408 char *p;
409 int len;
411 /* Get u_kbs->kb_func. */
412 if (get_user(kb_func, &u_kbs->kb_func))
413 return -EFAULT;
414 #if MAX_NR_FUNC < 256
415 if (kb_func >= MAX_NR_FUNC)
416 return -EINVAL;
417 #endif
419 switch (cmd) {
420 case KDGKBSENT:
421 p = kbd->func_table[kb_func];
422 if (p) {
423 len = strlen(p);
424 if (len >= sizeof(u_kbs->kb_string))
425 len = sizeof(u_kbs->kb_string) - 1;
426 if (copy_to_user(u_kbs->kb_string, p, len))
427 return -EFAULT;
428 } else
429 len = 0;
430 if (put_user('\0', u_kbs->kb_string + len))
431 return -EFAULT;
432 break;
433 case KDSKBSENT:
434 if (!perm)
435 return -EPERM;
436 len = strnlen_user(u_kbs->kb_string,
437 sizeof(u_kbs->kb_string) - 1);
438 if (!len)
439 return -EFAULT;
440 if (len > sizeof(u_kbs->kb_string) - 1)
441 return -EINVAL;
442 p = kmalloc(len + 1, GFP_KERNEL);
443 if (!p)
444 return -ENOMEM;
445 if (copy_from_user(p, u_kbs->kb_string, len)) {
446 kfree(p);
447 return -EFAULT;
449 p[len] = 0;
450 kfree(kbd->func_table[kb_func]);
451 kbd->func_table[kb_func] = p;
452 break;
454 return 0;
457 int kbd_ioctl(struct kbd_data *kbd, unsigned int cmd, unsigned long arg)
459 struct tty_struct *tty;
460 void __user *argp;
461 unsigned int ct;
462 int perm;
464 argp = (void __user *)arg;
467 * To have permissions to do most of the vt ioctls, we either have
468 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
470 tty = tty_port_tty_get(kbd->port);
471 /* FIXME this test is pretty racy */
472 perm = current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG);
473 tty_kref_put(tty);
474 switch (cmd) {
475 case KDGKBTYPE:
476 return put_user(KB_101, (char __user *)argp);
477 case KDGKBENT:
478 case KDSKBENT:
479 return do_kdsk_ioctl(kbd, argp, cmd, perm);
480 case KDGKBSENT:
481 case KDSKBSENT:
482 return do_kdgkb_ioctl(kbd, argp, cmd, perm);
483 case KDGKBDIACR:
485 struct kbdiacrs __user *a = argp;
486 struct kbdiacr diacr;
487 int i;
489 if (put_user(kbd->accent_table_size, &a->kb_cnt))
490 return -EFAULT;
491 for (i = 0; i < kbd->accent_table_size; i++) {
492 diacr.diacr = kbd->accent_table[i].diacr;
493 diacr.base = kbd->accent_table[i].base;
494 diacr.result = kbd->accent_table[i].result;
495 if (copy_to_user(a->kbdiacr + i, &diacr, sizeof(struct kbdiacr)))
496 return -EFAULT;
498 return 0;
500 case KDGKBDIACRUC:
502 struct kbdiacrsuc __user *a = argp;
504 ct = kbd->accent_table_size;
505 if (put_user(ct, &a->kb_cnt))
506 return -EFAULT;
507 if (copy_to_user(a->kbdiacruc, kbd->accent_table,
508 ct * sizeof(struct kbdiacruc)))
509 return -EFAULT;
510 return 0;
512 case KDSKBDIACR:
514 struct kbdiacrs __user *a = argp;
515 struct kbdiacr diacr;
516 int i;
518 if (!perm)
519 return -EPERM;
520 if (get_user(ct, &a->kb_cnt))
521 return -EFAULT;
522 if (ct >= MAX_DIACR)
523 return -EINVAL;
524 kbd->accent_table_size = ct;
525 for (i = 0; i < ct; i++) {
526 if (copy_from_user(&diacr, a->kbdiacr + i, sizeof(struct kbdiacr)))
527 return -EFAULT;
528 kbd->accent_table[i].diacr = diacr.diacr;
529 kbd->accent_table[i].base = diacr.base;
530 kbd->accent_table[i].result = diacr.result;
532 return 0;
534 case KDSKBDIACRUC:
536 struct kbdiacrsuc __user *a = argp;
538 if (!perm)
539 return -EPERM;
540 if (get_user(ct, &a->kb_cnt))
541 return -EFAULT;
542 if (ct >= MAX_DIACR)
543 return -EINVAL;
544 kbd->accent_table_size = ct;
545 if (copy_from_user(kbd->accent_table, a->kbdiacruc,
546 ct * sizeof(struct kbdiacruc)))
547 return -EFAULT;
548 return 0;
550 default:
551 return -ENOIOCTLCMD;
555 EXPORT_SYMBOL(kbd_ioctl);
556 EXPORT_SYMBOL(kbd_ascebc);
557 EXPORT_SYMBOL(kbd_free);
558 EXPORT_SYMBOL(kbd_alloc);
559 EXPORT_SYMBOL(kbd_keycode);