xref: /linux/drivers/s390/char/keyboard.c (revision c537b994505099b7197e7d3125b942ecbcc51eb6)
1 /*
2  *  drivers/s390/char/keyboard.c
3  *    ebcdic keycode functions for s390 console drivers
4  *
5  *  S390 version
6  *    Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
7  *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
8  */
9 
10 #include <linux/module.h>
11 #include <linux/sched.h>
12 #include <linux/sysrq.h>
13 
14 #include <linux/kbd_kern.h>
15 #include <linux/kbd_diacr.h>
16 #include <asm/uaccess.h>
17 
18 #include "keyboard.h"
19 
20 /*
21  * Handler Tables.
22  */
23 #define K_HANDLERS\
24 	k_self,		k_fn,		k_spec,		k_ignore,\
25 	k_dead,		k_ignore,	k_ignore,	k_ignore,\
26 	k_ignore,	k_ignore,	k_ignore,	k_ignore,\
27 	k_ignore,	k_ignore,	k_ignore,	k_ignore
28 
29 typedef void (k_handler_fn)(struct kbd_data *, unsigned char);
30 static k_handler_fn K_HANDLERS;
31 static k_handler_fn *k_handler[16] = { K_HANDLERS };
32 
33 /* maximum values each key_handler can handle */
34 static const int kbd_max_vals[] = {
35 	255, ARRAY_SIZE(func_table) - 1, NR_FN_HANDLER - 1, 0,
36 	NR_DEAD - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
37 };
38 static const int KBD_NR_TYPES = ARRAY_SIZE(kbd_max_vals);
39 
40 static unsigned char ret_diacr[NR_DEAD] = {
41 	'`', '\'', '^', '~', '"', ','
42 };
43 
44 /*
45  * Alloc/free of kbd_data structures.
46  */
47 struct kbd_data *
48 kbd_alloc(void) {
49 	struct kbd_data *kbd;
50 	int i, len;
51 
52 	kbd = kzalloc(sizeof(struct kbd_data), GFP_KERNEL);
53 	if (!kbd)
54 		goto out;
55 	kbd->key_maps = kzalloc(sizeof(key_maps), GFP_KERNEL);
56 	if (!kbd->key_maps)
57 		goto out_kbd;
58 	for (i = 0; i < ARRAY_SIZE(key_maps); i++) {
59 		if (key_maps[i]) {
60 			kbd->key_maps[i] =
61 				kmalloc(sizeof(u_short)*NR_KEYS, GFP_KERNEL);
62 			if (!kbd->key_maps[i])
63 				goto out_maps;
64 			memcpy(kbd->key_maps[i], key_maps[i],
65 			       sizeof(u_short)*NR_KEYS);
66 		}
67 	}
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 			len = strlen(func_table[i]) + 1;
74 			kbd->func_table[i] = kmalloc(len, GFP_KERNEL);
75 			if (!kbd->func_table[i])
76 				goto out_func;
77 			memcpy(kbd->func_table[i], func_table[i], len);
78 		}
79 	}
80 	kbd->fn_handler =
81 		kzalloc(sizeof(fn_handler_fn *) * NR_FN_HANDLER, GFP_KERNEL);
82 	if (!kbd->fn_handler)
83 		goto out_func;
84 	kbd->accent_table =
85 		kmalloc(sizeof(struct kbdiacr)*MAX_DIACR, GFP_KERNEL);
86 	if (!kbd->accent_table)
87 		goto out_fn_handler;
88 	memcpy(kbd->accent_table, accent_table,
89 	       sizeof(struct kbdiacr)*MAX_DIACR);
90 	kbd->accent_table_size = accent_table_size;
91 	return kbd;
92 
93 out_fn_handler:
94 	kfree(kbd->fn_handler);
95 out_func:
96 	for (i = 0; i < ARRAY_SIZE(func_table); i++)
97 		kfree(kbd->func_table[i]);
98 	kfree(kbd->func_table);
99 out_maps:
100 	for (i = 0; i < ARRAY_SIZE(key_maps); i++)
101 		kfree(kbd->key_maps[i]);
102 	kfree(kbd->key_maps);
103 out_kbd:
104 	kfree(kbd);
105 out:
106 	return NULL;
107 }
108 
109 void
110 kbd_free(struct kbd_data *kbd)
111 {
112 	int i;
113 
114 	kfree(kbd->accent_table);
115 	kfree(kbd->fn_handler);
116 	for (i = 0; i < ARRAY_SIZE(func_table); i++)
117 		kfree(kbd->func_table[i]);
118 	kfree(kbd->func_table);
119 	for (i = 0; i < ARRAY_SIZE(key_maps); i++)
120 		kfree(kbd->key_maps[i]);
121 	kfree(kbd->key_maps);
122 	kfree(kbd);
123 }
124 
125 /*
126  * Generate ascii -> ebcdic translation table from kbd_data.
127  */
128 void
129 kbd_ascebc(struct kbd_data *kbd, unsigned char *ascebc)
130 {
131 	unsigned short *keymap, keysym;
132 	int i, j, k;
133 
134 	memset(ascebc, 0x40, 256);
135 	for (i = 0; i < ARRAY_SIZE(key_maps); i++) {
136 		keymap = kbd->key_maps[i];
137 		if (!keymap)
138 			continue;
139 		for (j = 0; j < NR_KEYS; j++) {
140 			k = ((i & 1) << 7) + j;
141 			keysym = keymap[j];
142 			if (KTYP(keysym) == (KT_LATIN | 0xf0) ||
143 			    KTYP(keysym) == (KT_LETTER | 0xf0))
144 				ascebc[KVAL(keysym)] = k;
145 			else if (KTYP(keysym) == (KT_DEAD | 0xf0))
146 				ascebc[ret_diacr[KVAL(keysym)]] = k;
147 		}
148 	}
149 }
150 
151 #if 0
152 /*
153  * Generate ebcdic -> ascii translation table from kbd_data.
154  */
155 void
156 kbd_ebcasc(struct kbd_data *kbd, unsigned char *ebcasc)
157 {
158 	unsigned short *keymap, keysym;
159 	int i, j, k;
160 
161 	memset(ebcasc, ' ', 256);
162 	for (i = 0; i < ARRAY_SIZE(key_maps); i++) {
163 		keymap = kbd->key_maps[i];
164 		if (!keymap)
165 			continue;
166 		for (j = 0; j < NR_KEYS; j++) {
167 			keysym = keymap[j];
168 			k = ((i & 1) << 7) + j;
169 			if (KTYP(keysym) == (KT_LATIN | 0xf0) ||
170 			    KTYP(keysym) == (KT_LETTER | 0xf0))
171 				ebcasc[k] = KVAL(keysym);
172 			else if (KTYP(keysym) == (KT_DEAD | 0xf0))
173 				ebcasc[k] = ret_diacr[KVAL(keysym)];
174 		}
175 	}
176 }
177 #endif
178 
179 /*
180  * We have a combining character DIACR here, followed by the character CH.
181  * If the combination occurs in the table, return the corresponding value.
182  * Otherwise, if CH is a space or equals DIACR, return DIACR.
183  * Otherwise, conclude that DIACR was not combining after all,
184  * queue it and return CH.
185  */
186 static unsigned char
187 handle_diacr(struct kbd_data *kbd, unsigned char ch)
188 {
189 	int i, d;
190 
191 	d = kbd->diacr;
192 	kbd->diacr = 0;
193 
194 	for (i = 0; i < kbd->accent_table_size; i++) {
195 		if (kbd->accent_table[i].diacr == d &&
196 		    kbd->accent_table[i].base == ch)
197 			return kbd->accent_table[i].result;
198 	}
199 
200 	if (ch == ' ' || ch == d)
201 		return d;
202 
203 	kbd_put_queue(kbd->tty, d);
204 	return ch;
205 }
206 
207 /*
208  * Handle dead key.
209  */
210 static void
211 k_dead(struct kbd_data *kbd, unsigned char value)
212 {
213 	value = ret_diacr[value];
214 	kbd->diacr = (kbd->diacr ? handle_diacr(kbd, value) : value);
215 }
216 
217 /*
218  * Normal character handler.
219  */
220 static void
221 k_self(struct kbd_data *kbd, unsigned char value)
222 {
223 	if (kbd->diacr)
224 		value = handle_diacr(kbd, value);
225 	kbd_put_queue(kbd->tty, value);
226 }
227 
228 /*
229  * Special key handlers
230  */
231 static void
232 k_ignore(struct kbd_data *kbd, unsigned char value)
233 {
234 }
235 
236 /*
237  * Function key handler.
238  */
239 static void
240 k_fn(struct kbd_data *kbd, unsigned char value)
241 {
242 	if (kbd->func_table[value])
243 		kbd_puts_queue(kbd->tty, kbd->func_table[value]);
244 }
245 
246 static void
247 k_spec(struct kbd_data *kbd, unsigned char value)
248 {
249 	if (value >= NR_FN_HANDLER)
250 		return;
251 	if (kbd->fn_handler[value])
252 		kbd->fn_handler[value](kbd);
253 }
254 
255 /*
256  * Put utf8 character to tty flip buffer.
257  * UTF-8 is defined for words of up to 31 bits,
258  * but we need only 16 bits here
259  */
260 static void
261 to_utf8(struct tty_struct *tty, ushort c)
262 {
263 	if (c < 0x80)
264 		/*  0******* */
265 		kbd_put_queue(tty, c);
266 	else if (c < 0x800) {
267 		/* 110***** 10****** */
268 		kbd_put_queue(tty, 0xc0 | (c >> 6));
269 		kbd_put_queue(tty, 0x80 | (c & 0x3f));
270 	} else {
271 		/* 1110**** 10****** 10****** */
272 		kbd_put_queue(tty, 0xe0 | (c >> 12));
273 		kbd_put_queue(tty, 0x80 | ((c >> 6) & 0x3f));
274 		kbd_put_queue(tty, 0x80 | (c & 0x3f));
275 	}
276 }
277 
278 /*
279  * Process keycode.
280  */
281 void
282 kbd_keycode(struct kbd_data *kbd, unsigned int keycode)
283 {
284 	unsigned short keysym;
285 	unsigned char type, value;
286 
287 	if (!kbd || !kbd->tty)
288 		return;
289 
290 	if (keycode >= 384)
291 		keysym = kbd->key_maps[5][keycode - 384];
292 	else if (keycode >= 256)
293 		keysym = kbd->key_maps[4][keycode - 256];
294 	else if (keycode >= 128)
295 		keysym = kbd->key_maps[1][keycode - 128];
296 	else
297 		keysym = kbd->key_maps[0][keycode];
298 
299 	type = KTYP(keysym);
300 	if (type >= 0xf0) {
301 		type -= 0xf0;
302 		if (type == KT_LETTER)
303 			type = KT_LATIN;
304 		value = KVAL(keysym);
305 #ifdef CONFIG_MAGIC_SYSRQ	       /* Handle the SysRq Hack */
306 		if (kbd->sysrq) {
307 			if (kbd->sysrq == K(KT_LATIN, '-')) {
308 				kbd->sysrq = 0;
309 				handle_sysrq(value, kbd->tty);
310 				return;
311 			}
312 			if (value == '-') {
313 				kbd->sysrq = K(KT_LATIN, '-');
314 				return;
315 			}
316 			/* Incomplete sysrq sequence. */
317 			(*k_handler[KTYP(kbd->sysrq)])(kbd, KVAL(kbd->sysrq));
318 			kbd->sysrq = 0;
319 		} else if ((type == KT_LATIN && value == '^') ||
320 			   (type == KT_DEAD && ret_diacr[value] == '^')) {
321 			kbd->sysrq = K(type, value);
322 			return;
323 		}
324 #endif
325 		(*k_handler[type])(kbd, value);
326 	} else
327 		to_utf8(kbd->tty, keysym);
328 }
329 
330 /*
331  * Ioctl stuff.
332  */
333 static int
334 do_kdsk_ioctl(struct kbd_data *kbd, struct kbentry __user *user_kbe,
335 	      int cmd, int perm)
336 {
337 	struct kbentry tmp;
338 	ushort *key_map, val, ov;
339 
340 	if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
341 		return -EFAULT;
342 #if NR_KEYS < 256
343 	if (tmp.kb_index >= NR_KEYS)
344 		return -EINVAL;
345 #endif
346 #if MAX_NR_KEYMAPS < 256
347 	if (tmp.kb_table >= MAX_NR_KEYMAPS)
348 		return -EINVAL;
349 #endif
350 
351 	switch (cmd) {
352 	case KDGKBENT:
353 		key_map = kbd->key_maps[tmp.kb_table];
354 		if (key_map) {
355 		    val = U(key_map[tmp.kb_index]);
356 		    if (KTYP(val) >= KBD_NR_TYPES)
357 			val = K_HOLE;
358 		} else
359 		    val = (tmp.kb_index ? K_HOLE : K_NOSUCHMAP);
360 		return put_user(val, &user_kbe->kb_value);
361 	case KDSKBENT:
362 		if (!perm)
363 			return -EPERM;
364 		if (!tmp.kb_index && tmp.kb_value == K_NOSUCHMAP) {
365 			/* disallocate map */
366 			key_map = kbd->key_maps[tmp.kb_table];
367 			if (key_map) {
368 			    kbd->key_maps[tmp.kb_table] = NULL;
369 			    kfree(key_map);
370 			}
371 			break;
372 		}
373 
374 		if (KTYP(tmp.kb_value) >= KBD_NR_TYPES)
375 			return -EINVAL;
376 		if (KVAL(tmp.kb_value) > kbd_max_vals[KTYP(tmp.kb_value)])
377 			return -EINVAL;
378 
379 		if (!(key_map = kbd->key_maps[tmp.kb_table])) {
380 			int j;
381 
382 			key_map = kmalloc(sizeof(plain_map),
383 						     GFP_KERNEL);
384 			if (!key_map)
385 				return -ENOMEM;
386 			kbd->key_maps[tmp.kb_table] = key_map;
387 			for (j = 0; j < NR_KEYS; j++)
388 				key_map[j] = U(K_HOLE);
389 		}
390 		ov = U(key_map[tmp.kb_index]);
391 		if (tmp.kb_value == ov)
392 			break;	/* nothing to do */
393 		/*
394 		 * Attention Key.
395 		 */
396 		if (((ov == K_SAK) || (tmp.kb_value == K_SAK)) &&
397 		    !capable(CAP_SYS_ADMIN))
398 			return -EPERM;
399 		key_map[tmp.kb_index] = U(tmp.kb_value);
400 		break;
401 	}
402 	return 0;
403 }
404 
405 static int
406 do_kdgkb_ioctl(struct kbd_data *kbd, struct kbsentry __user *u_kbs,
407 	       int cmd, int perm)
408 {
409 	unsigned char kb_func;
410 	char *p;
411 	int len;
412 
413 	/* Get u_kbs->kb_func. */
414 	if (get_user(kb_func, &u_kbs->kb_func))
415 		return -EFAULT;
416 #if MAX_NR_FUNC < 256
417 	if (kb_func >= MAX_NR_FUNC)
418 		return -EINVAL;
419 #endif
420 
421 	switch (cmd) {
422 	case KDGKBSENT:
423 		p = kbd->func_table[kb_func];
424 		if (p) {
425 			len = strlen(p);
426 			if (len >= sizeof(u_kbs->kb_string))
427 				len = sizeof(u_kbs->kb_string) - 1;
428 			if (copy_to_user(u_kbs->kb_string, p, len))
429 				return -EFAULT;
430 		} else
431 			len = 0;
432 		if (put_user('\0', u_kbs->kb_string + len))
433 			return -EFAULT;
434 		break;
435 	case KDSKBSENT:
436 		if (!perm)
437 			return -EPERM;
438 		len = strnlen_user(u_kbs->kb_string,
439 				   sizeof(u_kbs->kb_string) - 1);
440 		if (!len)
441 			return -EFAULT;
442 		if (len > sizeof(u_kbs->kb_string) - 1)
443 			return -EINVAL;
444 		p = kmalloc(len + 1, GFP_KERNEL);
445 		if (!p)
446 			return -ENOMEM;
447 		if (copy_from_user(p, u_kbs->kb_string, len)) {
448 			kfree(p);
449 			return -EFAULT;
450 		}
451 		p[len] = 0;
452 		kfree(kbd->func_table[kb_func]);
453 		kbd->func_table[kb_func] = p;
454 		break;
455 	}
456 	return 0;
457 }
458 
459 int
460 kbd_ioctl(struct kbd_data *kbd, struct file *file,
461 	  unsigned int cmd, unsigned long arg)
462 {
463 	struct kbdiacrs __user *a;
464 	void __user *argp;
465 	int ct, perm;
466 
467 	argp = (void __user *)arg;
468 
469 	/*
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.
472 	 */
473 	perm = current->signal->tty == kbd->tty || capable(CAP_SYS_TTY_CONFIG);
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:
484 		a = argp;
485 
486 		if (put_user(kbd->accent_table_size, &a->kb_cnt))
487 			return -EFAULT;
488 		ct = kbd->accent_table_size;
489 		if (copy_to_user(a->kbdiacr, kbd->accent_table,
490 				 ct * sizeof(struct kbdiacr)))
491 			return -EFAULT;
492 		return 0;
493 	case KDSKBDIACR:
494 		a = argp;
495 		if (!perm)
496 			return -EPERM;
497 		if (get_user(ct, &a->kb_cnt))
498 			return -EFAULT;
499 		if (ct >= MAX_DIACR)
500 			return -EINVAL;
501 		kbd->accent_table_size = ct;
502 		if (copy_from_user(kbd->accent_table, a->kbdiacr,
503 				   ct * sizeof(struct kbdiacr)))
504 			return -EFAULT;
505 		return 0;
506 	default:
507 		return -ENOIOCTLCMD;
508 	}
509 }
510 
511 EXPORT_SYMBOL(kbd_ioctl);
512 EXPORT_SYMBOL(kbd_ascebc);
513 EXPORT_SYMBOL(kbd_free);
514 EXPORT_SYMBOL(kbd_alloc);
515 EXPORT_SYMBOL(kbd_keycode);
516