2 * drivers/s390/char/keyboard.c
3 * ebcdic keycode functions for s390 console drivers
6 * Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
10 #include <linux/module.h>
11 #include <linux/sched.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>
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.
53 kbd = kzalloc(sizeof(struct kbd_data), GFP_KERNEL);
56 kbd->key_maps = kzalloc(sizeof(key_maps), GFP_KERNEL);
59 for (i = 0; i < ARRAY_SIZE(key_maps); i++) {
62 kmalloc(sizeof(u_short)*NR_KEYS, GFP_KERNEL);
63 if (!kbd->key_maps[i])
65 memcpy(kbd->key_maps[i], key_maps[i],
66 sizeof(u_short)*NR_KEYS);
69 kbd->func_table = kzalloc(sizeof(func_table), GFP_KERNEL);
72 for (i = 0; i < ARRAY_SIZE(func_table); i++) {
74 len = strlen(func_table[i]) + 1;
75 kbd->func_table[i] = kmalloc(len, GFP_KERNEL);
76 if (!kbd->func_table[i])
78 memcpy(kbd->func_table[i], func_table[i], len);
82 kzalloc(sizeof(fn_handler_fn *) * NR_FN_HANDLER, GFP_KERNEL);
86 kmalloc(sizeof(struct kbdiacruc)*MAX_DIACR, GFP_KERNEL);
87 if (!kbd->accent_table)
89 memcpy(kbd->accent_table, accent_table,
90 sizeof(struct kbdiacruc)*MAX_DIACR);
91 kbd->accent_table_size = accent_table_size;
95 kfree(kbd->fn_handler);
97 for (i = 0; i < ARRAY_SIZE(func_table); i++)
98 kfree(kbd->func_table[i]);
99 kfree(kbd->func_table);
101 for (i = 0; i < ARRAY_SIZE(key_maps); i++)
102 kfree(kbd->key_maps[i]);
103 kfree(kbd->key_maps);
111 kbd_free(struct kbd_data *kbd)
115 kfree(kbd->accent_table);
116 kfree(kbd->fn_handler);
117 for (i = 0; i < ARRAY_SIZE(func_table); i++)
118 kfree(kbd->func_table[i]);
119 kfree(kbd->func_table);
120 for (i = 0; i < ARRAY_SIZE(key_maps); i++)
121 kfree(kbd->key_maps[i]);
122 kfree(kbd->key_maps);
127 * Generate ascii -> ebcdic translation table from kbd_data.
130 kbd_ascebc(struct kbd_data *kbd, unsigned char *ascebc)
132 unsigned short *keymap, keysym;
135 memset(ascebc, 0x40, 256);
136 for (i = 0; i < ARRAY_SIZE(key_maps); i++) {
137 keymap = kbd->key_maps[i];
140 for (j = 0; j < NR_KEYS; j++) {
141 k = ((i & 1) << 7) + j;
143 if (KTYP(keysym) == (KT_LATIN | 0xf0) ||
144 KTYP(keysym) == (KT_LETTER | 0xf0))
145 ascebc[KVAL(keysym)] = k;
146 else if (KTYP(keysym) == (KT_DEAD | 0xf0))
147 ascebc[ret_diacr[KVAL(keysym)]] = k;
154 * Generate ebcdic -> ascii translation table from kbd_data.
157 kbd_ebcasc(struct kbd_data *kbd, unsigned char *ebcasc)
159 unsigned short *keymap, keysym;
162 memset(ebcasc, ' ', 256);
163 for (i = 0; i < ARRAY_SIZE(key_maps); i++) {
164 keymap = kbd->key_maps[i];
167 for (j = 0; j < NR_KEYS; j++) {
169 k = ((i & 1) << 7) + j;
170 if (KTYP(keysym) == (KT_LATIN | 0xf0) ||
171 KTYP(keysym) == (KT_LETTER | 0xf0))
172 ebcasc[k] = KVAL(keysym);
173 else if (KTYP(keysym) == (KT_DEAD | 0xf0))
174 ebcasc[k] = ret_diacr[KVAL(keysym)];
181 * We have a combining character DIACR here, followed by the character CH.
182 * If the combination occurs in the table, return the corresponding value.
183 * Otherwise, if CH is a space or equals DIACR, return DIACR.
184 * Otherwise, conclude that DIACR was not combining after all,
185 * queue it and return CH.
188 handle_diacr(struct kbd_data *kbd, unsigned int ch)
195 for (i = 0; i < kbd->accent_table_size; i++) {
196 if (kbd->accent_table[i].diacr == d &&
197 kbd->accent_table[i].base == ch)
198 return kbd->accent_table[i].result;
201 if (ch == ' ' || ch == d)
204 kbd_put_queue(kbd->tty, d);
212 k_dead(struct kbd_data *kbd, unsigned char value)
214 value = ret_diacr[value];
215 kbd->diacr = (kbd->diacr ? handle_diacr(kbd, value) : value);
219 * Normal character handler.
222 k_self(struct kbd_data *kbd, unsigned char value)
225 value = handle_diacr(kbd, value);
226 kbd_put_queue(kbd->tty, value);
230 * Special key handlers
233 k_ignore(struct kbd_data *kbd, unsigned char value)
238 * Function key handler.
241 k_fn(struct kbd_data *kbd, unsigned char value)
243 if (kbd->func_table[value])
244 kbd_puts_queue(kbd->tty, kbd->func_table[value]);
248 k_spec(struct kbd_data *kbd, unsigned char value)
250 if (value >= NR_FN_HANDLER)
252 if (kbd->fn_handler[value])
253 kbd->fn_handler[value](kbd);
257 * Put utf8 character to tty flip buffer.
258 * UTF-8 is defined for words of up to 31 bits,
259 * but we need only 16 bits here
262 to_utf8(struct tty_struct *tty, ushort c)
266 kbd_put_queue(tty, c);
267 else if (c < 0x800) {
268 /* 110***** 10****** */
269 kbd_put_queue(tty, 0xc0 | (c >> 6));
270 kbd_put_queue(tty, 0x80 | (c & 0x3f));
272 /* 1110**** 10****** 10****** */
273 kbd_put_queue(tty, 0xe0 | (c >> 12));
274 kbd_put_queue(tty, 0x80 | ((c >> 6) & 0x3f));
275 kbd_put_queue(tty, 0x80 | (c & 0x3f));
283 kbd_keycode(struct kbd_data *kbd, unsigned int keycode)
285 unsigned short keysym;
286 unsigned char type, value;
288 if (!kbd || !kbd->tty)
292 keysym = kbd->key_maps[5][keycode - 384];
293 else if (keycode >= 256)
294 keysym = kbd->key_maps[4][keycode - 256];
295 else if (keycode >= 128)
296 keysym = kbd->key_maps[1][keycode - 128];
298 keysym = kbd->key_maps[0][keycode];
303 if (type == KT_LETTER)
305 value = KVAL(keysym);
306 #ifdef CONFIG_MAGIC_SYSRQ /* Handle the SysRq Hack */
308 if (kbd->sysrq == K(KT_LATIN, '-')) {
310 handle_sysrq(value, kbd->tty);
314 kbd->sysrq = K(KT_LATIN, '-');
317 /* Incomplete sysrq sequence. */
318 (*k_handler[KTYP(kbd->sysrq)])(kbd, KVAL(kbd->sysrq));
320 } else if ((type == KT_LATIN && value == '^') ||
321 (type == KT_DEAD && ret_diacr[value] == '^')) {
322 kbd->sysrq = K(type, value);
326 (*k_handler[type])(kbd, value);
328 to_utf8(kbd->tty, keysym);
335 do_kdsk_ioctl(struct kbd_data *kbd, struct kbentry __user *user_kbe,
339 ushort *key_map, val, ov;
341 if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
344 if (tmp.kb_index >= NR_KEYS)
347 #if MAX_NR_KEYMAPS < 256
348 if (tmp.kb_table >= MAX_NR_KEYMAPS)
354 key_map = kbd->key_maps[tmp.kb_table];
356 val = U(key_map[tmp.kb_index]);
357 if (KTYP(val) >= KBD_NR_TYPES)
360 val = (tmp.kb_index ? K_HOLE : K_NOSUCHMAP);
361 return put_user(val, &user_kbe->kb_value);
365 if (!tmp.kb_index && tmp.kb_value == K_NOSUCHMAP) {
366 /* disallocate map */
367 key_map = kbd->key_maps[tmp.kb_table];
369 kbd->key_maps[tmp.kb_table] = NULL;
375 if (KTYP(tmp.kb_value) >= KBD_NR_TYPES)
377 if (KVAL(tmp.kb_value) > kbd_max_vals[KTYP(tmp.kb_value)])
380 if (!(key_map = kbd->key_maps[tmp.kb_table])) {
383 key_map = kmalloc(sizeof(plain_map),
387 kbd->key_maps[tmp.kb_table] = key_map;
388 for (j = 0; j < NR_KEYS; j++)
389 key_map[j] = U(K_HOLE);
391 ov = U(key_map[tmp.kb_index]);
392 if (tmp.kb_value == ov)
393 break; /* nothing to do */
397 if (((ov == K_SAK) || (tmp.kb_value == K_SAK)) &&
398 !capable(CAP_SYS_ADMIN))
400 key_map[tmp.kb_index] = U(tmp.kb_value);
407 do_kdgkb_ioctl(struct kbd_data *kbd, struct kbsentry __user *u_kbs,
410 unsigned char kb_func;
414 /* Get u_kbs->kb_func. */
415 if (get_user(kb_func, &u_kbs->kb_func))
417 #if MAX_NR_FUNC < 256
418 if (kb_func >= MAX_NR_FUNC)
424 p = kbd->func_table[kb_func];
427 if (len >= sizeof(u_kbs->kb_string))
428 len = sizeof(u_kbs->kb_string) - 1;
429 if (copy_to_user(u_kbs->kb_string, p, len))
433 if (put_user('\0', u_kbs->kb_string + len))
439 len = strnlen_user(u_kbs->kb_string,
440 sizeof(u_kbs->kb_string) - 1);
443 if (len > sizeof(u_kbs->kb_string) - 1)
445 p = kmalloc(len + 1, GFP_KERNEL);
448 if (copy_from_user(p, u_kbs->kb_string, len)) {
453 kfree(kbd->func_table[kb_func]);
454 kbd->func_table[kb_func] = p;
461 kbd_ioctl(struct kbd_data *kbd, struct file *file,
462 unsigned int cmd, unsigned long arg)
467 argp = (void __user *)arg;
470 * To have permissions to do most of the vt ioctls, we either have
471 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
473 perm = current->signal->tty == kbd->tty || capable(CAP_SYS_TTY_CONFIG);
476 return put_user(KB_101, (char __user *)argp);
479 return do_kdsk_ioctl(kbd, argp, cmd, perm);
482 return do_kdgkb_ioctl(kbd, argp, cmd, perm);
485 struct kbdiacrs __user *a = argp;
486 struct kbdiacr diacr;
489 if (put_user(kbd->accent_table_size, &a->kb_cnt))
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)))
502 struct kbdiacrsuc __user *a = argp;
504 ct = kbd->accent_table_size;
505 if (put_user(ct, &a->kb_cnt))
507 if (copy_to_user(a->kbdiacruc, kbd->accent_table,
508 ct * sizeof(struct kbdiacruc)))
514 struct kbdiacrs __user *a = argp;
515 struct kbdiacr diacr;
520 if (get_user(ct, &a->kb_cnt))
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)))
528 kbd->accent_table[i].diacr = diacr.diacr;
529 kbd->accent_table[i].base = diacr.base;
530 kbd->accent_table[i].result = diacr.result;
536 struct kbdiacrsuc __user *a = argp;
540 if (get_user(ct, &a->kb_cnt))
544 kbd->accent_table_size = ct;
545 if (copy_from_user(kbd->accent_table, a->kbdiacruc,
546 ct * sizeof(struct kbdiacruc)))
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);