2 * ebcdic keycode functions for s390 console drivers
5 * Copyright IBM Corp. 2003
6 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
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>
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++) {
61 kbd->key_maps[i] = kmemdup(key_maps[i],
62 sizeof(u_short) * NR_KEYS,
64 if (!kbd->key_maps[i])
68 kbd->func_table = kzalloc(sizeof(func_table), GFP_KERNEL);
71 for (i = 0; i < ARRAY_SIZE(func_table); i++) {
73 kbd->func_table[i] = kstrdup(func_table[i],
75 if (!kbd->func_table[i])
80 kzalloc(sizeof(fn_handler_fn *) * NR_FN_HANDLER, GFP_KERNEL);
83 kbd->accent_table = kmemdup(accent_table,
84 sizeof(struct kbdiacruc) * MAX_DIACR,
86 if (!kbd->accent_table)
88 kbd->accent_table_size = accent_table_size;
92 kfree(kbd->fn_handler);
94 for (i = 0; i < ARRAY_SIZE(func_table); i++)
95 kfree(kbd->func_table[i]);
96 kfree(kbd->func_table);
98 for (i = 0; i < ARRAY_SIZE(key_maps); i++)
99 kfree(kbd->key_maps[i]);
100 kfree(kbd->key_maps);
108 kbd_free(struct kbd_data *kbd)
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);
124 * Generate ascii -> ebcdic translation table from kbd_data.
127 kbd_ascebc(struct kbd_data *kbd, unsigned char *ascebc)
129 unsigned short *keymap, keysym;
132 memset(ascebc, 0x40, 256);
133 for (i = 0; i < ARRAY_SIZE(key_maps); i++) {
134 keymap = kbd->key_maps[i];
137 for (j = 0; j < NR_KEYS; j++) {
138 k = ((i & 1) << 7) + 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;
151 * Generate ebcdic -> ascii translation table from kbd_data.
154 kbd_ebcasc(struct kbd_data *kbd, unsigned char *ebcasc)
156 unsigned short *keymap, keysym;
159 memset(ebcasc, ' ', 256);
160 for (i = 0; i < ARRAY_SIZE(key_maps); i++) {
161 keymap = kbd->key_maps[i];
164 for (j = 0; j < NR_KEYS; 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)];
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.
185 handle_diacr(struct kbd_data *kbd, unsigned int ch)
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)
201 kbd_put_queue(kbd->port, d);
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.
219 k_self(struct kbd_data *kbd, unsigned char value)
222 value = handle_diacr(kbd, value);
223 kbd_put_queue(kbd->port, value);
227 * Special key handlers
230 k_ignore(struct kbd_data *kbd, unsigned char value)
235 * Function key handler.
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]);
245 k_spec(struct kbd_data *kbd, unsigned char value)
247 if (value >= NR_FN_HANDLER)
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
259 to_utf8(struct tty_port *port, ushort c)
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));
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));
280 kbd_keycode(struct kbd_data *kbd, unsigned int keycode)
282 unsigned short keysym;
283 unsigned char type, value;
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];
295 keysym = kbd->key_maps[0][keycode];
300 if (type == KT_LETTER)
302 value = KVAL(keysym);
303 #ifdef CONFIG_MAGIC_SYSRQ /* Handle the SysRq Hack */
305 if (kbd->sysrq == K(KT_LATIN, '-')) {
311 kbd->sysrq = K(KT_LATIN, '-');
314 /* Incomplete sysrq sequence. */
315 (*k_handler[KTYP(kbd->sysrq)])(kbd, KVAL(kbd->sysrq));
317 } else if ((type == KT_LATIN && value == '^') ||
318 (type == KT_DEAD && ret_diacr[value] == '^')) {
319 kbd->sysrq = K(type, value);
323 (*k_handler[type])(kbd, value);
325 to_utf8(kbd->port, keysym);
332 do_kdsk_ioctl(struct kbd_data *kbd, struct kbentry __user *user_kbe,
336 ushort *key_map, val, ov;
338 if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
341 if (tmp.kb_index >= NR_KEYS)
344 #if MAX_NR_KEYMAPS < 256
345 if (tmp.kb_table >= MAX_NR_KEYMAPS)
351 key_map = kbd->key_maps[tmp.kb_table];
353 val = U(key_map[tmp.kb_index]);
354 if (KTYP(val) >= KBD_NR_TYPES)
357 val = (tmp.kb_index ? K_HOLE : K_NOSUCHMAP);
358 return put_user(val, &user_kbe->kb_value);
362 if (!tmp.kb_index && tmp.kb_value == K_NOSUCHMAP) {
363 /* disallocate map */
364 key_map = kbd->key_maps[tmp.kb_table];
366 kbd->key_maps[tmp.kb_table] = NULL;
372 if (KTYP(tmp.kb_value) >= KBD_NR_TYPES)
374 if (KVAL(tmp.kb_value) > kbd_max_vals[KTYP(tmp.kb_value)])
377 if (!(key_map = kbd->key_maps[tmp.kb_table])) {
380 key_map = kmalloc(sizeof(plain_map),
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 */
394 if (((ov == K_SAK) || (tmp.kb_value == K_SAK)) &&
395 !capable(CAP_SYS_ADMIN))
397 key_map[tmp.kb_index] = U(tmp.kb_value);
404 do_kdgkb_ioctl(struct kbd_data *kbd, struct kbsentry __user *u_kbs,
407 unsigned char kb_func;
411 /* Get u_kbs->kb_func. */
412 if (get_user(kb_func, &u_kbs->kb_func))
414 #if MAX_NR_FUNC < 256
415 if (kb_func >= MAX_NR_FUNC)
421 p = kbd->func_table[kb_func];
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))
430 if (put_user('\0', u_kbs->kb_string + len))
436 len = strnlen_user(u_kbs->kb_string, sizeof(u_kbs->kb_string));
439 if (len > sizeof(u_kbs->kb_string))
441 p = kmalloc(len, GFP_KERNEL);
444 if (copy_from_user(p, u_kbs->kb_string, len)) {
449 * Make sure the string is terminated by 0. User could have
450 * modified it between us running strnlen_user() and copying it.
453 kfree(kbd->func_table[kb_func]);
454 kbd->func_table[kb_func] = p;
460 int kbd_ioctl(struct kbd_data *kbd, unsigned int cmd, unsigned long arg)
462 struct tty_struct *tty;
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 tty = tty_port_tty_get(kbd->port);
474 /* FIXME this test is pretty racy */
475 perm = current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG);
479 return put_user(KB_101, (char __user *)argp);
482 return do_kdsk_ioctl(kbd, argp, cmd, perm);
485 return do_kdgkb_ioctl(kbd, argp, cmd, perm);
488 struct kbdiacrs __user *a = argp;
489 struct kbdiacr diacr;
492 if (put_user(kbd->accent_table_size, &a->kb_cnt))
494 for (i = 0; i < kbd->accent_table_size; i++) {
495 diacr.diacr = kbd->accent_table[i].diacr;
496 diacr.base = kbd->accent_table[i].base;
497 diacr.result = kbd->accent_table[i].result;
498 if (copy_to_user(a->kbdiacr + i, &diacr, sizeof(struct kbdiacr)))
505 struct kbdiacrsuc __user *a = argp;
507 ct = kbd->accent_table_size;
508 if (put_user(ct, &a->kb_cnt))
510 if (copy_to_user(a->kbdiacruc, kbd->accent_table,
511 ct * sizeof(struct kbdiacruc)))
517 struct kbdiacrs __user *a = argp;
518 struct kbdiacr diacr;
523 if (get_user(ct, &a->kb_cnt))
527 kbd->accent_table_size = ct;
528 for (i = 0; i < ct; i++) {
529 if (copy_from_user(&diacr, a->kbdiacr + i, sizeof(struct kbdiacr)))
531 kbd->accent_table[i].diacr = diacr.diacr;
532 kbd->accent_table[i].base = diacr.base;
533 kbd->accent_table[i].result = diacr.result;
539 struct kbdiacrsuc __user *a = argp;
543 if (get_user(ct, &a->kb_cnt))
547 kbd->accent_table_size = ct;
548 if (copy_from_user(kbd->accent_table, a->kbdiacruc,
549 ct * sizeof(struct kbdiacruc)))
558 EXPORT_SYMBOL(kbd_ioctl);
559 EXPORT_SYMBOL(kbd_ascebc);
560 EXPORT_SYMBOL(kbd_free);
561 EXPORT_SYMBOL(kbd_alloc);
562 EXPORT_SYMBOL(kbd_keycode);