• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Written for linux by Johan Myreen as a translation from
3  * the assembly version by Linus (with diacriticals added)
4  *
5  * Some additional features added by Christoph Niemann (ChN), March 1993
6  *
7  * Loadable keymaps by Risto Kankkunen, May 1993
8  *
9  * Diacriticals redone & other small changes, aeb@cwi.nl, June 1993
10  * Added decr/incr_console, dynamic keymaps, Unicode support,
11  * dynamic function/string keys, led setting,  Sept 1994
12  * `Sticky' modifier keys, 951006.
13  *
14  * 11-11-96: SAK should now work in the raw mode (Martin Mares)
15  *
16  * Modified to provide 'generic' keyboard support by Hamish Macdonald
17  * Merge with the m68k keyboard driver and split-off of the PC low-level
18  * parts by Geert Uytterhoeven, May 1997
19  *
20  * 27-05-97: Added support for the Magic SysRq Key (Martin Mares)
21  * 30-07-98: Dead keys redone, aeb@cwi.nl.
22  * 21-08-02: Converted to input API, major cleanup. (Vojtech Pavlik)
23  */
24 
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 
27 #include <linux/consolemap.h>
28 #include <linux/module.h>
29 #include <linux/sched.h>
30 #include <linux/tty.h>
31 #include <linux/tty_flip.h>
32 #include <linux/mm.h>
33 #include <linux/string.h>
34 #include <linux/init.h>
35 #include <linux/slab.h>
36 
37 #include <linux/kbd_kern.h>
38 #include <linux/kbd_diacr.h>
39 #include <linux/vt_kern.h>
40 #include <linux/input.h>
41 #include <linux/reboot.h>
42 #include <linux/notifier.h>
43 #include <linux/jiffies.h>
44 #include <linux/uaccess.h>
45 
46 #include <asm/irq_regs.h>
47 
48 extern void ctrl_alt_del(void);
49 
50 /*
51  * Exported functions/variables
52  */
53 
54 #define KBD_DEFMODE ((1 << VC_REPEAT) | (1 << VC_META))
55 
56 #if defined(CONFIG_X86) || defined(CONFIG_PARISC)
57 #include <asm/kbdleds.h>
58 #else
kbd_defleds(void)59 static inline int kbd_defleds(void)
60 {
61 	return 0;
62 }
63 #endif
64 
65 #define KBD_DEFLOCK 0
66 
67 /*
68  * Handler Tables.
69  */
70 
71 #define K_HANDLERS\
72 	k_self,		k_fn,		k_spec,		k_pad,\
73 	k_dead,		k_cons,		k_cur,		k_shift,\
74 	k_meta,		k_ascii,	k_lock,		k_lowercase,\
75 	k_slock,	k_dead2,	k_brl,		k_ignore
76 
77 typedef void (k_handler_fn)(struct vc_data *vc, unsigned char value,
78 			    char up_flag);
79 static k_handler_fn K_HANDLERS;
80 static k_handler_fn *k_handler[16] = { K_HANDLERS };
81 
82 #define FN_HANDLERS\
83 	fn_null,	fn_enter,	fn_show_ptregs,	fn_show_mem,\
84 	fn_show_state,	fn_send_intr,	fn_lastcons,	fn_caps_toggle,\
85 	fn_num,		fn_hold,	fn_scroll_forw,	fn_scroll_back,\
86 	fn_boot_it,	fn_caps_on,	fn_compose,	fn_SAK,\
87 	fn_dec_console, fn_inc_console, fn_spawn_con,	fn_bare_num
88 
89 typedef void (fn_handler_fn)(struct vc_data *vc);
90 static fn_handler_fn FN_HANDLERS;
91 static fn_handler_fn *fn_handler[] = { FN_HANDLERS };
92 
93 /*
94  * Variables exported for vt_ioctl.c
95  */
96 
97 struct vt_spawn_console vt_spawn_con = {
98 	.lock = __SPIN_LOCK_UNLOCKED(vt_spawn_con.lock),
99 	.pid  = NULL,
100 	.sig  = 0,
101 };
102 
103 
104 /*
105  * Internal Data.
106  */
107 
108 static struct kbd_struct kbd_table[MAX_NR_CONSOLES];
109 static struct kbd_struct *kbd = kbd_table;
110 
111 /* maximum values each key_handler can handle */
112 static const int max_vals[] = {
113 	255, ARRAY_SIZE(func_table) - 1, ARRAY_SIZE(fn_handler) - 1, NR_PAD - 1,
114 	NR_DEAD - 1, 255, 3, NR_SHIFT - 1, 255, NR_ASCII - 1, NR_LOCK - 1,
115 	255, NR_LOCK - 1, 255, NR_BRL - 1
116 };
117 
118 static const int NR_TYPES = ARRAY_SIZE(max_vals);
119 
120 static struct input_handler kbd_handler;
121 static DEFINE_SPINLOCK(kbd_event_lock);
122 static DEFINE_SPINLOCK(led_lock);
123 static unsigned long key_down[BITS_TO_LONGS(KEY_CNT)];	/* keyboard key bitmap */
124 static unsigned char shift_down[NR_SHIFT];		/* shift state counters.. */
125 static bool dead_key_next;
126 static int npadch = -1;					/* -1 or number assembled on pad */
127 static unsigned int diacr;
128 static char rep;					/* flag telling character repeat */
129 
130 static int shift_state = 0;
131 
132 static unsigned char ledstate = 0xff;			/* undefined */
133 static unsigned char ledioctl;
134 
135 /*
136  * Notifier list for console keyboard events
137  */
138 static ATOMIC_NOTIFIER_HEAD(keyboard_notifier_list);
139 
register_keyboard_notifier(struct notifier_block * nb)140 int register_keyboard_notifier(struct notifier_block *nb)
141 {
142 	return atomic_notifier_chain_register(&keyboard_notifier_list, nb);
143 }
144 EXPORT_SYMBOL_GPL(register_keyboard_notifier);
145 
unregister_keyboard_notifier(struct notifier_block * nb)146 int unregister_keyboard_notifier(struct notifier_block *nb)
147 {
148 	return atomic_notifier_chain_unregister(&keyboard_notifier_list, nb);
149 }
150 EXPORT_SYMBOL_GPL(unregister_keyboard_notifier);
151 
152 /*
153  * Translation of scancodes to keycodes. We set them on only the first
154  * keyboard in the list that accepts the scancode and keycode.
155  * Explanation for not choosing the first attached keyboard anymore:
156  *  USB keyboards for example have two event devices: one for all "normal"
157  *  keys and one for extra function keys (like "volume up", "make coffee",
158  *  etc.). So this means that scancodes for the extra function keys won't
159  *  be valid for the first event device, but will be for the second.
160  */
161 
162 struct getset_keycode_data {
163 	struct input_keymap_entry ke;
164 	int error;
165 };
166 
getkeycode_helper(struct input_handle * handle,void * data)167 static int getkeycode_helper(struct input_handle *handle, void *data)
168 {
169 	struct getset_keycode_data *d = data;
170 
171 	d->error = input_get_keycode(handle->dev, &d->ke);
172 
173 	return d->error == 0; /* stop as soon as we successfully get one */
174 }
175 
getkeycode(unsigned int scancode)176 static int getkeycode(unsigned int scancode)
177 {
178 	struct getset_keycode_data d = {
179 		.ke	= {
180 			.flags		= 0,
181 			.len		= sizeof(scancode),
182 			.keycode	= 0,
183 		},
184 		.error	= -ENODEV,
185 	};
186 
187 	memcpy(d.ke.scancode, &scancode, sizeof(scancode));
188 
189 	input_handler_for_each_handle(&kbd_handler, &d, getkeycode_helper);
190 
191 	return d.error ?: d.ke.keycode;
192 }
193 
setkeycode_helper(struct input_handle * handle,void * data)194 static int setkeycode_helper(struct input_handle *handle, void *data)
195 {
196 	struct getset_keycode_data *d = data;
197 
198 	d->error = input_set_keycode(handle->dev, &d->ke);
199 
200 	return d->error == 0; /* stop as soon as we successfully set one */
201 }
202 
setkeycode(unsigned int scancode,unsigned int keycode)203 static int setkeycode(unsigned int scancode, unsigned int keycode)
204 {
205 	struct getset_keycode_data d = {
206 		.ke	= {
207 			.flags		= 0,
208 			.len		= sizeof(scancode),
209 			.keycode	= keycode,
210 		},
211 		.error	= -ENODEV,
212 	};
213 
214 	memcpy(d.ke.scancode, &scancode, sizeof(scancode));
215 
216 	input_handler_for_each_handle(&kbd_handler, &d, setkeycode_helper);
217 
218 	return d.error;
219 }
220 
221 /*
222  * Making beeps and bells. Note that we prefer beeps to bells, but when
223  * shutting the sound off we do both.
224  */
225 
kd_sound_helper(struct input_handle * handle,void * data)226 static int kd_sound_helper(struct input_handle *handle, void *data)
227 {
228 	unsigned int *hz = data;
229 	struct input_dev *dev = handle->dev;
230 
231 	if (test_bit(EV_SND, dev->evbit)) {
232 		if (test_bit(SND_TONE, dev->sndbit)) {
233 			input_inject_event(handle, EV_SND, SND_TONE, *hz);
234 			if (*hz)
235 				return 0;
236 		}
237 		if (test_bit(SND_BELL, dev->sndbit))
238 			input_inject_event(handle, EV_SND, SND_BELL, *hz ? 1 : 0);
239 	}
240 
241 	return 0;
242 }
243 
kd_nosound(unsigned long ignored)244 static void kd_nosound(unsigned long ignored)
245 {
246 	static unsigned int zero;
247 
248 	input_handler_for_each_handle(&kbd_handler, &zero, kd_sound_helper);
249 }
250 
251 static DEFINE_TIMER(kd_mksound_timer, kd_nosound, 0, 0);
252 
kd_mksound(unsigned int hz,unsigned int ticks)253 void kd_mksound(unsigned int hz, unsigned int ticks)
254 {
255 	del_timer_sync(&kd_mksound_timer);
256 
257 	input_handler_for_each_handle(&kbd_handler, &hz, kd_sound_helper);
258 
259 	if (hz && ticks)
260 		mod_timer(&kd_mksound_timer, jiffies + ticks);
261 }
262 EXPORT_SYMBOL(kd_mksound);
263 
264 /*
265  * Setting the keyboard rate.
266  */
267 
kbd_rate_helper(struct input_handle * handle,void * data)268 static int kbd_rate_helper(struct input_handle *handle, void *data)
269 {
270 	struct input_dev *dev = handle->dev;
271 	struct kbd_repeat *rpt = data;
272 
273 	if (test_bit(EV_REP, dev->evbit)) {
274 
275 		if (rpt[0].delay > 0)
276 			input_inject_event(handle,
277 					   EV_REP, REP_DELAY, rpt[0].delay);
278 		if (rpt[0].period > 0)
279 			input_inject_event(handle,
280 					   EV_REP, REP_PERIOD, rpt[0].period);
281 
282 		rpt[1].delay = dev->rep[REP_DELAY];
283 		rpt[1].period = dev->rep[REP_PERIOD];
284 	}
285 
286 	return 0;
287 }
288 
kbd_rate(struct kbd_repeat * rpt)289 int kbd_rate(struct kbd_repeat *rpt)
290 {
291 	struct kbd_repeat data[2] = { *rpt };
292 
293 	input_handler_for_each_handle(&kbd_handler, data, kbd_rate_helper);
294 	*rpt = data[1];	/* Copy currently used settings */
295 
296 	return 0;
297 }
298 
299 /*
300  * Helper Functions.
301  */
put_queue(struct vc_data * vc,int ch)302 static void put_queue(struct vc_data *vc, int ch)
303 {
304 	tty_insert_flip_char(&vc->port, ch, 0);
305 	tty_schedule_flip(&vc->port);
306 }
307 
puts_queue(struct vc_data * vc,char * cp)308 static void puts_queue(struct vc_data *vc, char *cp)
309 {
310 	while (*cp) {
311 		tty_insert_flip_char(&vc->port, *cp, 0);
312 		cp++;
313 	}
314 	tty_schedule_flip(&vc->port);
315 }
316 
applkey(struct vc_data * vc,int key,char mode)317 static void applkey(struct vc_data *vc, int key, char mode)
318 {
319 	static char buf[] = { 0x1b, 'O', 0x00, 0x00 };
320 
321 	buf[1] = (mode ? 'O' : '[');
322 	buf[2] = key;
323 	puts_queue(vc, buf);
324 }
325 
326 /*
327  * Many other routines do put_queue, but I think either
328  * they produce ASCII, or they produce some user-assigned
329  * string, and in both cases we might assume that it is
330  * in utf-8 already.
331  */
to_utf8(struct vc_data * vc,uint c)332 static void to_utf8(struct vc_data *vc, uint c)
333 {
334 	if (c < 0x80)
335 		/*  0******* */
336 		put_queue(vc, c);
337 	else if (c < 0x800) {
338 		/* 110***** 10****** */
339 		put_queue(vc, 0xc0 | (c >> 6));
340 		put_queue(vc, 0x80 | (c & 0x3f));
341 	} else if (c < 0x10000) {
342 		if (c >= 0xD800 && c < 0xE000)
343 			return;
344 		if (c == 0xFFFF)
345 			return;
346 		/* 1110**** 10****** 10****** */
347 		put_queue(vc, 0xe0 | (c >> 12));
348 		put_queue(vc, 0x80 | ((c >> 6) & 0x3f));
349 		put_queue(vc, 0x80 | (c & 0x3f));
350 	} else if (c < 0x110000) {
351 		/* 11110*** 10****** 10****** 10****** */
352 		put_queue(vc, 0xf0 | (c >> 18));
353 		put_queue(vc, 0x80 | ((c >> 12) & 0x3f));
354 		put_queue(vc, 0x80 | ((c >> 6) & 0x3f));
355 		put_queue(vc, 0x80 | (c & 0x3f));
356 	}
357 }
358 
359 /*
360  * Called after returning from RAW mode or when changing consoles - recompute
361  * shift_down[] and shift_state from key_down[] maybe called when keymap is
362  * undefined, so that shiftkey release is seen. The caller must hold the
363  * kbd_event_lock.
364  */
365 
do_compute_shiftstate(void)366 static void do_compute_shiftstate(void)
367 {
368 	unsigned int k, sym, val;
369 
370 	shift_state = 0;
371 	memset(shift_down, 0, sizeof(shift_down));
372 
373 	for_each_set_bit(k, key_down, min(NR_KEYS, KEY_CNT)) {
374 		sym = U(key_maps[0][k]);
375 		if (KTYP(sym) != KT_SHIFT && KTYP(sym) != KT_SLOCK)
376 			continue;
377 
378 		val = KVAL(sym);
379 		if (val == KVAL(K_CAPSSHIFT))
380 			val = KVAL(K_SHIFT);
381 
382 		shift_down[val]++;
383 		shift_state |= BIT(val);
384 	}
385 }
386 
387 /* We still have to export this method to vt.c */
compute_shiftstate(void)388 void compute_shiftstate(void)
389 {
390 	unsigned long flags;
391 	spin_lock_irqsave(&kbd_event_lock, flags);
392 	do_compute_shiftstate();
393 	spin_unlock_irqrestore(&kbd_event_lock, flags);
394 }
395 
396 /*
397  * We have a combining character DIACR here, followed by the character CH.
398  * If the combination occurs in the table, return the corresponding value.
399  * Otherwise, if CH is a space or equals DIACR, return DIACR.
400  * Otherwise, conclude that DIACR was not combining after all,
401  * queue it and return CH.
402  */
handle_diacr(struct vc_data * vc,unsigned int ch)403 static unsigned int handle_diacr(struct vc_data *vc, unsigned int ch)
404 {
405 	unsigned int d = diacr;
406 	unsigned int i;
407 
408 	diacr = 0;
409 
410 	if ((d & ~0xff) == BRL_UC_ROW) {
411 		if ((ch & ~0xff) == BRL_UC_ROW)
412 			return d | ch;
413 	} else {
414 		for (i = 0; i < accent_table_size; i++)
415 			if (accent_table[i].diacr == d && accent_table[i].base == ch)
416 				return accent_table[i].result;
417 	}
418 
419 	if (ch == ' ' || ch == (BRL_UC_ROW|0) || ch == d)
420 		return d;
421 
422 	if (kbd->kbdmode == VC_UNICODE)
423 		to_utf8(vc, d);
424 	else {
425 		int c = conv_uni_to_8bit(d);
426 		if (c != -1)
427 			put_queue(vc, c);
428 	}
429 
430 	return ch;
431 }
432 
433 /*
434  * Special function handlers
435  */
fn_enter(struct vc_data * vc)436 static void fn_enter(struct vc_data *vc)
437 {
438 	if (diacr) {
439 		if (kbd->kbdmode == VC_UNICODE)
440 			to_utf8(vc, diacr);
441 		else {
442 			int c = conv_uni_to_8bit(diacr);
443 			if (c != -1)
444 				put_queue(vc, c);
445 		}
446 		diacr = 0;
447 	}
448 
449 	put_queue(vc, 13);
450 	if (vc_kbd_mode(kbd, VC_CRLF))
451 		put_queue(vc, 10);
452 }
453 
fn_caps_toggle(struct vc_data * vc)454 static void fn_caps_toggle(struct vc_data *vc)
455 {
456 	if (rep)
457 		return;
458 
459 	chg_vc_kbd_led(kbd, VC_CAPSLOCK);
460 }
461 
fn_caps_on(struct vc_data * vc)462 static void fn_caps_on(struct vc_data *vc)
463 {
464 	if (rep)
465 		return;
466 
467 	set_vc_kbd_led(kbd, VC_CAPSLOCK);
468 }
469 
fn_show_ptregs(struct vc_data * vc)470 static void fn_show_ptregs(struct vc_data *vc)
471 {
472 	struct pt_regs *regs = get_irq_regs();
473 
474 	if (regs)
475 		show_regs(regs);
476 }
477 
fn_hold(struct vc_data * vc)478 static void fn_hold(struct vc_data *vc)
479 {
480 	struct tty_struct *tty = vc->port.tty;
481 
482 	if (rep || !tty)
483 		return;
484 
485 	/*
486 	 * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty);
487 	 * these routines are also activated by ^S/^Q.
488 	 * (And SCROLLOCK can also be set by the ioctl KDSKBLED.)
489 	 */
490 	if (tty->stopped)
491 		start_tty(tty);
492 	else
493 		stop_tty(tty);
494 }
495 
fn_num(struct vc_data * vc)496 static void fn_num(struct vc_data *vc)
497 {
498 	if (vc_kbd_mode(kbd, VC_APPLIC))
499 		applkey(vc, 'P', 1);
500 	else
501 		fn_bare_num(vc);
502 }
503 
504 /*
505  * Bind this to Shift-NumLock if you work in application keypad mode
506  * but want to be able to change the NumLock flag.
507  * Bind this to NumLock if you prefer that the NumLock key always
508  * changes the NumLock flag.
509  */
fn_bare_num(struct vc_data * vc)510 static void fn_bare_num(struct vc_data *vc)
511 {
512 	if (!rep)
513 		chg_vc_kbd_led(kbd, VC_NUMLOCK);
514 }
515 
fn_lastcons(struct vc_data * vc)516 static void fn_lastcons(struct vc_data *vc)
517 {
518 	/* switch to the last used console, ChN */
519 	set_console(last_console);
520 }
521 
fn_dec_console(struct vc_data * vc)522 static void fn_dec_console(struct vc_data *vc)
523 {
524 	int i, cur = fg_console;
525 
526 	/* Currently switching?  Queue this next switch relative to that. */
527 	if (want_console != -1)
528 		cur = want_console;
529 
530 	for (i = cur - 1; i != cur; i--) {
531 		if (i == -1)
532 			i = MAX_NR_CONSOLES - 1;
533 		if (vc_cons_allocated(i))
534 			break;
535 	}
536 	set_console(i);
537 }
538 
fn_inc_console(struct vc_data * vc)539 static void fn_inc_console(struct vc_data *vc)
540 {
541 	int i, cur = fg_console;
542 
543 	/* Currently switching?  Queue this next switch relative to that. */
544 	if (want_console != -1)
545 		cur = want_console;
546 
547 	for (i = cur+1; i != cur; i++) {
548 		if (i == MAX_NR_CONSOLES)
549 			i = 0;
550 		if (vc_cons_allocated(i))
551 			break;
552 	}
553 	set_console(i);
554 }
555 
fn_send_intr(struct vc_data * vc)556 static void fn_send_intr(struct vc_data *vc)
557 {
558 	tty_insert_flip_char(&vc->port, 0, TTY_BREAK);
559 	tty_schedule_flip(&vc->port);
560 }
561 
fn_scroll_forw(struct vc_data * vc)562 static void fn_scroll_forw(struct vc_data *vc)
563 {
564 	scrollfront(vc, 0);
565 }
566 
fn_scroll_back(struct vc_data * vc)567 static void fn_scroll_back(struct vc_data *vc)
568 {
569 	scrollback(vc, 0);
570 }
571 
fn_show_mem(struct vc_data * vc)572 static void fn_show_mem(struct vc_data *vc)
573 {
574 	show_mem(0);
575 }
576 
fn_show_state(struct vc_data * vc)577 static void fn_show_state(struct vc_data *vc)
578 {
579 	show_state();
580 }
581 
fn_boot_it(struct vc_data * vc)582 static void fn_boot_it(struct vc_data *vc)
583 {
584 	ctrl_alt_del();
585 }
586 
fn_compose(struct vc_data * vc)587 static void fn_compose(struct vc_data *vc)
588 {
589 	dead_key_next = true;
590 }
591 
fn_spawn_con(struct vc_data * vc)592 static void fn_spawn_con(struct vc_data *vc)
593 {
594 	spin_lock(&vt_spawn_con.lock);
595 	if (vt_spawn_con.pid)
596 		if (kill_pid(vt_spawn_con.pid, vt_spawn_con.sig, 1)) {
597 			put_pid(vt_spawn_con.pid);
598 			vt_spawn_con.pid = NULL;
599 		}
600 	spin_unlock(&vt_spawn_con.lock);
601 }
602 
fn_SAK(struct vc_data * vc)603 static void fn_SAK(struct vc_data *vc)
604 {
605 	struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work;
606 	schedule_work(SAK_work);
607 }
608 
fn_null(struct vc_data * vc)609 static void fn_null(struct vc_data *vc)
610 {
611 	do_compute_shiftstate();
612 }
613 
614 /*
615  * Special key handlers
616  */
k_ignore(struct vc_data * vc,unsigned char value,char up_flag)617 static void k_ignore(struct vc_data *vc, unsigned char value, char up_flag)
618 {
619 }
620 
k_spec(struct vc_data * vc,unsigned char value,char up_flag)621 static void k_spec(struct vc_data *vc, unsigned char value, char up_flag)
622 {
623 	if (up_flag)
624 		return;
625 	if (value >= ARRAY_SIZE(fn_handler))
626 		return;
627 	if ((kbd->kbdmode == VC_RAW ||
628 	     kbd->kbdmode == VC_MEDIUMRAW ||
629 	     kbd->kbdmode == VC_OFF) &&
630 	     value != KVAL(K_SAK))
631 		return;		/* SAK is allowed even in raw mode */
632 	fn_handler[value](vc);
633 }
634 
k_lowercase(struct vc_data * vc,unsigned char value,char up_flag)635 static void k_lowercase(struct vc_data *vc, unsigned char value, char up_flag)
636 {
637 	pr_err("k_lowercase was called - impossible\n");
638 }
639 
k_unicode(struct vc_data * vc,unsigned int value,char up_flag)640 static void k_unicode(struct vc_data *vc, unsigned int value, char up_flag)
641 {
642 	if (up_flag)
643 		return;		/* no action, if this is a key release */
644 
645 	if (diacr)
646 		value = handle_diacr(vc, value);
647 
648 	if (dead_key_next) {
649 		dead_key_next = false;
650 		diacr = value;
651 		return;
652 	}
653 	if (kbd->kbdmode == VC_UNICODE)
654 		to_utf8(vc, value);
655 	else {
656 		int c = conv_uni_to_8bit(value);
657 		if (c != -1)
658 			put_queue(vc, c);
659 	}
660 }
661 
662 /*
663  * Handle dead key. Note that we now may have several
664  * dead keys modifying the same character. Very useful
665  * for Vietnamese.
666  */
k_deadunicode(struct vc_data * vc,unsigned int value,char up_flag)667 static void k_deadunicode(struct vc_data *vc, unsigned int value, char up_flag)
668 {
669 	if (up_flag)
670 		return;
671 
672 	diacr = (diacr ? handle_diacr(vc, value) : value);
673 }
674 
k_self(struct vc_data * vc,unsigned char value,char up_flag)675 static void k_self(struct vc_data *vc, unsigned char value, char up_flag)
676 {
677 	k_unicode(vc, conv_8bit_to_uni(value), up_flag);
678 }
679 
k_dead2(struct vc_data * vc,unsigned char value,char up_flag)680 static void k_dead2(struct vc_data *vc, unsigned char value, char up_flag)
681 {
682 	k_deadunicode(vc, value, up_flag);
683 }
684 
685 /*
686  * Obsolete - for backwards compatibility only
687  */
k_dead(struct vc_data * vc,unsigned char value,char up_flag)688 static void k_dead(struct vc_data *vc, unsigned char value, char up_flag)
689 {
690 	static const unsigned char ret_diacr[NR_DEAD] = {'`', '\'', '^', '~', '"', ',' };
691 
692 	k_deadunicode(vc, ret_diacr[value], up_flag);
693 }
694 
k_cons(struct vc_data * vc,unsigned char value,char up_flag)695 static void k_cons(struct vc_data *vc, unsigned char value, char up_flag)
696 {
697 	if (up_flag)
698 		return;
699 
700 	set_console(value);
701 }
702 
k_fn(struct vc_data * vc,unsigned char value,char up_flag)703 static void k_fn(struct vc_data *vc, unsigned char value, char up_flag)
704 {
705 	if (up_flag)
706 		return;
707 
708 	if ((unsigned)value < ARRAY_SIZE(func_table)) {
709 		if (func_table[value])
710 			puts_queue(vc, func_table[value]);
711 	} else
712 		pr_err("k_fn called with value=%d\n", value);
713 }
714 
k_cur(struct vc_data * vc,unsigned char value,char up_flag)715 static void k_cur(struct vc_data *vc, unsigned char value, char up_flag)
716 {
717 	static const char cur_chars[] = "BDCA";
718 
719 	if (up_flag)
720 		return;
721 
722 	applkey(vc, cur_chars[value], vc_kbd_mode(kbd, VC_CKMODE));
723 }
724 
k_pad(struct vc_data * vc,unsigned char value,char up_flag)725 static void k_pad(struct vc_data *vc, unsigned char value, char up_flag)
726 {
727 	static const char pad_chars[] = "0123456789+-*/\015,.?()#";
728 	static const char app_map[] = "pqrstuvwxylSRQMnnmPQS";
729 
730 	if (up_flag)
731 		return;		/* no action, if this is a key release */
732 
733 	/* kludge... shift forces cursor/number keys */
734 	if (vc_kbd_mode(kbd, VC_APPLIC) && !shift_down[KG_SHIFT]) {
735 		applkey(vc, app_map[value], 1);
736 		return;
737 	}
738 
739 	if (!vc_kbd_led(kbd, VC_NUMLOCK)) {
740 
741 		switch (value) {
742 		case KVAL(K_PCOMMA):
743 		case KVAL(K_PDOT):
744 			k_fn(vc, KVAL(K_REMOVE), 0);
745 			return;
746 		case KVAL(K_P0):
747 			k_fn(vc, KVAL(K_INSERT), 0);
748 			return;
749 		case KVAL(K_P1):
750 			k_fn(vc, KVAL(K_SELECT), 0);
751 			return;
752 		case KVAL(K_P2):
753 			k_cur(vc, KVAL(K_DOWN), 0);
754 			return;
755 		case KVAL(K_P3):
756 			k_fn(vc, KVAL(K_PGDN), 0);
757 			return;
758 		case KVAL(K_P4):
759 			k_cur(vc, KVAL(K_LEFT), 0);
760 			return;
761 		case KVAL(K_P6):
762 			k_cur(vc, KVAL(K_RIGHT), 0);
763 			return;
764 		case KVAL(K_P7):
765 			k_fn(vc, KVAL(K_FIND), 0);
766 			return;
767 		case KVAL(K_P8):
768 			k_cur(vc, KVAL(K_UP), 0);
769 			return;
770 		case KVAL(K_P9):
771 			k_fn(vc, KVAL(K_PGUP), 0);
772 			return;
773 		case KVAL(K_P5):
774 			applkey(vc, 'G', vc_kbd_mode(kbd, VC_APPLIC));
775 			return;
776 		}
777 	}
778 
779 	put_queue(vc, pad_chars[value]);
780 	if (value == KVAL(K_PENTER) && vc_kbd_mode(kbd, VC_CRLF))
781 		put_queue(vc, 10);
782 }
783 
k_shift(struct vc_data * vc,unsigned char value,char up_flag)784 static void k_shift(struct vc_data *vc, unsigned char value, char up_flag)
785 {
786 	int old_state = shift_state;
787 
788 	if (rep)
789 		return;
790 	/*
791 	 * Mimic typewriter:
792 	 * a CapsShift key acts like Shift but undoes CapsLock
793 	 */
794 	if (value == KVAL(K_CAPSSHIFT)) {
795 		value = KVAL(K_SHIFT);
796 		if (!up_flag)
797 			clr_vc_kbd_led(kbd, VC_CAPSLOCK);
798 	}
799 
800 	if (up_flag) {
801 		/*
802 		 * handle the case that two shift or control
803 		 * keys are depressed simultaneously
804 		 */
805 		if (shift_down[value])
806 			shift_down[value]--;
807 	} else
808 		shift_down[value]++;
809 
810 	if (shift_down[value])
811 		shift_state |= (1 << value);
812 	else
813 		shift_state &= ~(1 << value);
814 
815 	/* kludge */
816 	if (up_flag && shift_state != old_state && npadch != -1) {
817 		if (kbd->kbdmode == VC_UNICODE)
818 			to_utf8(vc, npadch);
819 		else
820 			put_queue(vc, npadch & 0xff);
821 		npadch = -1;
822 	}
823 }
824 
k_meta(struct vc_data * vc,unsigned char value,char up_flag)825 static void k_meta(struct vc_data *vc, unsigned char value, char up_flag)
826 {
827 	if (up_flag)
828 		return;
829 
830 	if (vc_kbd_mode(kbd, VC_META)) {
831 		put_queue(vc, '\033');
832 		put_queue(vc, value);
833 	} else
834 		put_queue(vc, value | 0x80);
835 }
836 
k_ascii(struct vc_data * vc,unsigned char value,char up_flag)837 static void k_ascii(struct vc_data *vc, unsigned char value, char up_flag)
838 {
839 	int base;
840 
841 	if (up_flag)
842 		return;
843 
844 	if (value < 10) {
845 		/* decimal input of code, while Alt depressed */
846 		base = 10;
847 	} else {
848 		/* hexadecimal input of code, while AltGr depressed */
849 		value -= 10;
850 		base = 16;
851 	}
852 
853 	if (npadch == -1)
854 		npadch = value;
855 	else
856 		npadch = npadch * base + value;
857 }
858 
k_lock(struct vc_data * vc,unsigned char value,char up_flag)859 static void k_lock(struct vc_data *vc, unsigned char value, char up_flag)
860 {
861 	if (up_flag || rep)
862 		return;
863 
864 	chg_vc_kbd_lock(kbd, value);
865 }
866 
k_slock(struct vc_data * vc,unsigned char value,char up_flag)867 static void k_slock(struct vc_data *vc, unsigned char value, char up_flag)
868 {
869 	k_shift(vc, value, up_flag);
870 	if (up_flag || rep)
871 		return;
872 
873 	chg_vc_kbd_slock(kbd, value);
874 	/* try to make Alt, oops, AltGr and such work */
875 	if (!key_maps[kbd->lockstate ^ kbd->slockstate]) {
876 		kbd->slockstate = 0;
877 		chg_vc_kbd_slock(kbd, value);
878 	}
879 }
880 
881 /* by default, 300ms interval for combination release */
882 static unsigned brl_timeout = 300;
883 MODULE_PARM_DESC(brl_timeout, "Braille keys release delay in ms (0 for commit on first key release)");
884 module_param(brl_timeout, uint, 0644);
885 
886 static unsigned brl_nbchords = 1;
887 MODULE_PARM_DESC(brl_nbchords, "Number of chords that produce a braille pattern (0 for dead chords)");
888 module_param(brl_nbchords, uint, 0644);
889 
k_brlcommit(struct vc_data * vc,unsigned int pattern,char up_flag)890 static void k_brlcommit(struct vc_data *vc, unsigned int pattern, char up_flag)
891 {
892 	static unsigned long chords;
893 	static unsigned committed;
894 
895 	if (!brl_nbchords)
896 		k_deadunicode(vc, BRL_UC_ROW | pattern, up_flag);
897 	else {
898 		committed |= pattern;
899 		chords++;
900 		if (chords == brl_nbchords) {
901 			k_unicode(vc, BRL_UC_ROW | committed, up_flag);
902 			chords = 0;
903 			committed = 0;
904 		}
905 	}
906 }
907 
k_brl(struct vc_data * vc,unsigned char value,char up_flag)908 static void k_brl(struct vc_data *vc, unsigned char value, char up_flag)
909 {
910 	static unsigned pressed, committing;
911 	static unsigned long releasestart;
912 
913 	if (kbd->kbdmode != VC_UNICODE) {
914 		if (!up_flag)
915 			pr_warning("keyboard mode must be unicode for braille patterns\n");
916 		return;
917 	}
918 
919 	if (!value) {
920 		k_unicode(vc, BRL_UC_ROW, up_flag);
921 		return;
922 	}
923 
924 	if (value > 8)
925 		return;
926 
927 	if (!up_flag) {
928 		pressed |= 1 << (value - 1);
929 		if (!brl_timeout)
930 			committing = pressed;
931 	} else if (brl_timeout) {
932 		if (!committing ||
933 		    time_after(jiffies,
934 			       releasestart + msecs_to_jiffies(brl_timeout))) {
935 			committing = pressed;
936 			releasestart = jiffies;
937 		}
938 		pressed &= ~(1 << (value - 1));
939 		if (!pressed && committing) {
940 			k_brlcommit(vc, committing, 0);
941 			committing = 0;
942 		}
943 	} else {
944 		if (committing) {
945 			k_brlcommit(vc, committing, 0);
946 			committing = 0;
947 		}
948 		pressed &= ~(1 << (value - 1));
949 	}
950 }
951 
952 /*
953  * The leds display either (i) the status of NumLock, CapsLock, ScrollLock,
954  * or (ii) whatever pattern of lights people want to show using KDSETLED,
955  * or (iii) specified bits of specified words in kernel memory.
956  */
getledstate(void)957 static unsigned char getledstate(void)
958 {
959 	return ledstate;
960 }
961 
setledstate(struct kbd_struct * kb,unsigned int led)962 void setledstate(struct kbd_struct *kb, unsigned int led)
963 {
964         unsigned long flags;
965         spin_lock_irqsave(&led_lock, flags);
966 	if (!(led & ~7)) {
967 		ledioctl = led;
968 		kb->ledmode = LED_SHOW_IOCTL;
969 	} else
970 		kb->ledmode = LED_SHOW_FLAGS;
971 
972 	set_leds();
973 	spin_unlock_irqrestore(&led_lock, flags);
974 }
975 
getleds(void)976 static inline unsigned char getleds(void)
977 {
978 	struct kbd_struct *kb = kbd_table + fg_console;
979 
980 	if (kb->ledmode == LED_SHOW_IOCTL)
981 		return ledioctl;
982 
983 	return kb->ledflagstate;
984 }
985 
kbd_update_leds_helper(struct input_handle * handle,void * data)986 static int kbd_update_leds_helper(struct input_handle *handle, void *data)
987 {
988 	unsigned char leds = *(unsigned char *)data;
989 
990 	if (test_bit(EV_LED, handle->dev->evbit)) {
991 		input_inject_event(handle, EV_LED, LED_SCROLLL, !!(leds & 0x01));
992 		input_inject_event(handle, EV_LED, LED_NUML,    !!(leds & 0x02));
993 		input_inject_event(handle, EV_LED, LED_CAPSL,   !!(leds & 0x04));
994 		input_inject_event(handle, EV_SYN, SYN_REPORT, 0);
995 	}
996 
997 	return 0;
998 }
999 
1000 /**
1001  *	vt_get_leds	-	helper for braille console
1002  *	@console: console to read
1003  *	@flag: flag we want to check
1004  *
1005  *	Check the status of a keyboard led flag and report it back
1006  */
vt_get_leds(int console,int flag)1007 int vt_get_leds(int console, int flag)
1008 {
1009 	struct kbd_struct *kb = kbd_table + console;
1010 	int ret;
1011 	unsigned long flags;
1012 
1013 	spin_lock_irqsave(&led_lock, flags);
1014 	ret = vc_kbd_led(kb, flag);
1015 	spin_unlock_irqrestore(&led_lock, flags);
1016 
1017 	return ret;
1018 }
1019 EXPORT_SYMBOL_GPL(vt_get_leds);
1020 
1021 /**
1022  *	vt_set_led_state	-	set LED state of a console
1023  *	@console: console to set
1024  *	@leds: LED bits
1025  *
1026  *	Set the LEDs on a console. This is a wrapper for the VT layer
1027  *	so that we can keep kbd knowledge internal
1028  */
vt_set_led_state(int console,int leds)1029 void vt_set_led_state(int console, int leds)
1030 {
1031 	struct kbd_struct *kb = kbd_table + console;
1032 	setledstate(kb, leds);
1033 }
1034 
1035 /**
1036  *	vt_kbd_con_start	-	Keyboard side of console start
1037  *	@console: console
1038  *
1039  *	Handle console start. This is a wrapper for the VT layer
1040  *	so that we can keep kbd knowledge internal
1041  *
1042  *	FIXME: We eventually need to hold the kbd lock here to protect
1043  *	the LED updating. We can't do it yet because fn_hold calls stop_tty
1044  *	and start_tty under the kbd_event_lock, while normal tty paths
1045  *	don't hold the lock. We probably need to split out an LED lock
1046  *	but not during an -rc release!
1047  */
vt_kbd_con_start(int console)1048 void vt_kbd_con_start(int console)
1049 {
1050 	struct kbd_struct *kb = kbd_table + console;
1051 	unsigned long flags;
1052 	spin_lock_irqsave(&led_lock, flags);
1053 	clr_vc_kbd_led(kb, VC_SCROLLOCK);
1054 	set_leds();
1055 	spin_unlock_irqrestore(&led_lock, flags);
1056 }
1057 
1058 /**
1059  *	vt_kbd_con_stop		-	Keyboard side of console stop
1060  *	@console: console
1061  *
1062  *	Handle console stop. This is a wrapper for the VT layer
1063  *	so that we can keep kbd knowledge internal
1064  */
vt_kbd_con_stop(int console)1065 void vt_kbd_con_stop(int console)
1066 {
1067 	struct kbd_struct *kb = kbd_table + console;
1068 	unsigned long flags;
1069 	spin_lock_irqsave(&led_lock, flags);
1070 	set_vc_kbd_led(kb, VC_SCROLLOCK);
1071 	set_leds();
1072 	spin_unlock_irqrestore(&led_lock, flags);
1073 }
1074 
1075 /*
1076  * This is the tasklet that updates LED state on all keyboards
1077  * attached to the box. The reason we use tasklet is that we
1078  * need to handle the scenario when keyboard handler is not
1079  * registered yet but we already getting updates from the VT to
1080  * update led state.
1081  */
kbd_bh(unsigned long dummy)1082 static void kbd_bh(unsigned long dummy)
1083 {
1084 	unsigned char leds;
1085 	unsigned long flags;
1086 
1087 	spin_lock_irqsave(&led_lock, flags);
1088 	leds = getleds();
1089 	spin_unlock_irqrestore(&led_lock, flags);
1090 
1091 	if (leds != ledstate) {
1092 		input_handler_for_each_handle(&kbd_handler, &leds,
1093 					      kbd_update_leds_helper);
1094 		ledstate = leds;
1095 	}
1096 }
1097 
1098 DECLARE_TASKLET_DISABLED(keyboard_tasklet, kbd_bh, 0);
1099 
1100 #if defined(CONFIG_X86) || defined(CONFIG_IA64) || defined(CONFIG_ALPHA) ||\
1101     defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_SPARC) ||\
1102     defined(CONFIG_PARISC) || defined(CONFIG_SUPERH) ||\
1103     (defined(CONFIG_ARM) && defined(CONFIG_KEYBOARD_ATKBD) && !defined(CONFIG_ARCH_RPC)) ||\
1104     defined(CONFIG_AVR32)
1105 
1106 #define HW_RAW(dev) (test_bit(EV_MSC, dev->evbit) && test_bit(MSC_RAW, dev->mscbit) &&\
1107 			((dev)->id.bustype == BUS_I8042) && ((dev)->id.vendor == 0x0001) && ((dev)->id.product == 0x0001))
1108 
1109 static const unsigned short x86_keycodes[256] =
1110 	{ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
1111 	 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
1112 	 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
1113 	 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
1114 	 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
1115 	 80, 81, 82, 83, 84,118, 86, 87, 88,115,120,119,121,112,123, 92,
1116 	284,285,309,  0,312, 91,327,328,329,331,333,335,336,337,338,339,
1117 	367,288,302,304,350, 89,334,326,267,126,268,269,125,347,348,349,
1118 	360,261,262,263,268,376,100,101,321,316,373,286,289,102,351,355,
1119 	103,104,105,275,287,279,258,106,274,107,294,364,358,363,362,361,
1120 	291,108,381,281,290,272,292,305,280, 99,112,257,306,359,113,114,
1121 	264,117,271,374,379,265,266, 93, 94, 95, 85,259,375,260, 90,116,
1122 	377,109,111,277,278,282,283,295,296,297,299,300,301,293,303,307,
1123 	308,310,313,314,315,317,318,319,320,357,322,323,324,325,276,330,
1124 	332,340,365,342,343,344,345,346,356,270,341,368,369,370,371,372 };
1125 
1126 #ifdef CONFIG_SPARC
1127 static int sparc_l1_a_state;
1128 extern void sun_do_break(void);
1129 #endif
1130 
emulate_raw(struct vc_data * vc,unsigned int keycode,unsigned char up_flag)1131 static int emulate_raw(struct vc_data *vc, unsigned int keycode,
1132 		       unsigned char up_flag)
1133 {
1134 	int code;
1135 
1136 	switch (keycode) {
1137 
1138 	case KEY_PAUSE:
1139 		put_queue(vc, 0xe1);
1140 		put_queue(vc, 0x1d | up_flag);
1141 		put_queue(vc, 0x45 | up_flag);
1142 		break;
1143 
1144 	case KEY_HANGEUL:
1145 		if (!up_flag)
1146 			put_queue(vc, 0xf2);
1147 		break;
1148 
1149 	case KEY_HANJA:
1150 		if (!up_flag)
1151 			put_queue(vc, 0xf1);
1152 		break;
1153 
1154 	case KEY_SYSRQ:
1155 		/*
1156 		 * Real AT keyboards (that's what we're trying
1157 		 * to emulate here emit 0xe0 0x2a 0xe0 0x37 when
1158 		 * pressing PrtSc/SysRq alone, but simply 0x54
1159 		 * when pressing Alt+PrtSc/SysRq.
1160 		 */
1161 		if (test_bit(KEY_LEFTALT, key_down) ||
1162 		    test_bit(KEY_RIGHTALT, key_down)) {
1163 			put_queue(vc, 0x54 | up_flag);
1164 		} else {
1165 			put_queue(vc, 0xe0);
1166 			put_queue(vc, 0x2a | up_flag);
1167 			put_queue(vc, 0xe0);
1168 			put_queue(vc, 0x37 | up_flag);
1169 		}
1170 		break;
1171 
1172 	default:
1173 		if (keycode > 255)
1174 			return -1;
1175 
1176 		code = x86_keycodes[keycode];
1177 		if (!code)
1178 			return -1;
1179 
1180 		if (code & 0x100)
1181 			put_queue(vc, 0xe0);
1182 		put_queue(vc, (code & 0x7f) | up_flag);
1183 
1184 		break;
1185 	}
1186 
1187 	return 0;
1188 }
1189 
1190 #else
1191 
1192 #define HW_RAW(dev)	0
1193 
emulate_raw(struct vc_data * vc,unsigned int keycode,unsigned char up_flag)1194 static int emulate_raw(struct vc_data *vc, unsigned int keycode, unsigned char up_flag)
1195 {
1196 	if (keycode > 127)
1197 		return -1;
1198 
1199 	put_queue(vc, keycode | up_flag);
1200 	return 0;
1201 }
1202 #endif
1203 
kbd_rawcode(unsigned char data)1204 static void kbd_rawcode(unsigned char data)
1205 {
1206 	struct vc_data *vc = vc_cons[fg_console].d;
1207 
1208 	kbd = kbd_table + vc->vc_num;
1209 	if (kbd->kbdmode == VC_RAW)
1210 		put_queue(vc, data);
1211 }
1212 
kbd_keycode(unsigned int keycode,int down,int hw_raw)1213 static void kbd_keycode(unsigned int keycode, int down, int hw_raw)
1214 {
1215 	struct vc_data *vc = vc_cons[fg_console].d;
1216 	unsigned short keysym, *key_map;
1217 	unsigned char type;
1218 	bool raw_mode;
1219 	struct tty_struct *tty;
1220 	int shift_final;
1221 	struct keyboard_notifier_param param = { .vc = vc, .value = keycode, .down = down };
1222 	int rc;
1223 
1224 	tty = vc->port.tty;
1225 
1226 	if (tty && (!tty->driver_data)) {
1227 		/* No driver data? Strange. Okay we fix it then. */
1228 		tty->driver_data = vc;
1229 	}
1230 
1231 	kbd = kbd_table + vc->vc_num;
1232 
1233 #ifdef CONFIG_SPARC
1234 	if (keycode == KEY_STOP)
1235 		sparc_l1_a_state = down;
1236 #endif
1237 
1238 	rep = (down == 2);
1239 
1240 	raw_mode = (kbd->kbdmode == VC_RAW);
1241 	if (raw_mode && !hw_raw)
1242 		if (emulate_raw(vc, keycode, !down << 7))
1243 			if (keycode < BTN_MISC && printk_ratelimit())
1244 				pr_warning("can't emulate rawmode for keycode %d\n",
1245 					   keycode);
1246 
1247 #ifdef CONFIG_SPARC
1248 	if (keycode == KEY_A && sparc_l1_a_state) {
1249 		sparc_l1_a_state = false;
1250 		sun_do_break();
1251 	}
1252 #endif
1253 
1254 	if (kbd->kbdmode == VC_MEDIUMRAW) {
1255 		/*
1256 		 * This is extended medium raw mode, with keys above 127
1257 		 * encoded as 0, high 7 bits, low 7 bits, with the 0 bearing
1258 		 * the 'up' flag if needed. 0 is reserved, so this shouldn't
1259 		 * interfere with anything else. The two bytes after 0 will
1260 		 * always have the up flag set not to interfere with older
1261 		 * applications. This allows for 16384 different keycodes,
1262 		 * which should be enough.
1263 		 */
1264 		if (keycode < 128) {
1265 			put_queue(vc, keycode | (!down << 7));
1266 		} else {
1267 			put_queue(vc, !down << 7);
1268 			put_queue(vc, (keycode >> 7) | 0x80);
1269 			put_queue(vc, keycode | 0x80);
1270 		}
1271 		raw_mode = true;
1272 	}
1273 
1274 	if (down)
1275 		set_bit(keycode, key_down);
1276 	else
1277 		clear_bit(keycode, key_down);
1278 
1279 	if (rep &&
1280 	    (!vc_kbd_mode(kbd, VC_REPEAT) ||
1281 	     (tty && !L_ECHO(tty) && tty_chars_in_buffer(tty)))) {
1282 		/*
1283 		 * Don't repeat a key if the input buffers are not empty and the
1284 		 * characters get aren't echoed locally. This makes key repeat
1285 		 * usable with slow applications and under heavy loads.
1286 		 */
1287 		return;
1288 	}
1289 
1290 	param.shift = shift_final = (shift_state | kbd->slockstate) ^ kbd->lockstate;
1291 	param.ledstate = kbd->ledflagstate;
1292 	key_map = key_maps[shift_final];
1293 
1294 	rc = atomic_notifier_call_chain(&keyboard_notifier_list,
1295 					KBD_KEYCODE, &param);
1296 	if (rc == NOTIFY_STOP || !key_map) {
1297 		atomic_notifier_call_chain(&keyboard_notifier_list,
1298 					   KBD_UNBOUND_KEYCODE, &param);
1299 		do_compute_shiftstate();
1300 		kbd->slockstate = 0;
1301 		return;
1302 	}
1303 
1304 	if (keycode < NR_KEYS)
1305 		keysym = key_map[keycode];
1306 	else if (keycode >= KEY_BRL_DOT1 && keycode <= KEY_BRL_DOT8)
1307 		keysym = U(K(KT_BRL, keycode - KEY_BRL_DOT1 + 1));
1308 	else
1309 		return;
1310 
1311 	type = KTYP(keysym);
1312 
1313 	if (type < 0xf0) {
1314 		param.value = keysym;
1315 		rc = atomic_notifier_call_chain(&keyboard_notifier_list,
1316 						KBD_UNICODE, &param);
1317 		if (rc != NOTIFY_STOP)
1318 			if (down && !raw_mode)
1319 				to_utf8(vc, keysym);
1320 		return;
1321 	}
1322 
1323 	type -= 0xf0;
1324 
1325 	if (type == KT_LETTER) {
1326 		type = KT_LATIN;
1327 		if (vc_kbd_led(kbd, VC_CAPSLOCK)) {
1328 			key_map = key_maps[shift_final ^ (1 << KG_SHIFT)];
1329 			if (key_map)
1330 				keysym = key_map[keycode];
1331 		}
1332 	}
1333 
1334 	param.value = keysym;
1335 	rc = atomic_notifier_call_chain(&keyboard_notifier_list,
1336 					KBD_KEYSYM, &param);
1337 	if (rc == NOTIFY_STOP)
1338 		return;
1339 
1340 	if ((raw_mode || kbd->kbdmode == VC_OFF) && type != KT_SPEC && type != KT_SHIFT)
1341 		return;
1342 
1343 	(*k_handler[type])(vc, keysym & 0xff, !down);
1344 
1345 	param.ledstate = kbd->ledflagstate;
1346 	atomic_notifier_call_chain(&keyboard_notifier_list, KBD_POST_KEYSYM, &param);
1347 
1348 	if (type != KT_SLOCK)
1349 		kbd->slockstate = 0;
1350 }
1351 
kbd_event(struct input_handle * handle,unsigned int event_type,unsigned int event_code,int value)1352 static void kbd_event(struct input_handle *handle, unsigned int event_type,
1353 		      unsigned int event_code, int value)
1354 {
1355 	/* We are called with interrupts disabled, just take the lock */
1356 	spin_lock(&kbd_event_lock);
1357 
1358 	if (event_type == EV_MSC && event_code == MSC_RAW && HW_RAW(handle->dev))
1359 		kbd_rawcode(value);
1360 	if (event_type == EV_KEY)
1361 		kbd_keycode(event_code, value, HW_RAW(handle->dev));
1362 
1363 	spin_unlock(&kbd_event_lock);
1364 
1365 	tasklet_schedule(&keyboard_tasklet);
1366 	do_poke_blanked_console = 1;
1367 	schedule_console_callback();
1368 }
1369 
kbd_match(struct input_handler * handler,struct input_dev * dev)1370 static bool kbd_match(struct input_handler *handler, struct input_dev *dev)
1371 {
1372 	int i;
1373 
1374 	if (test_bit(EV_SND, dev->evbit))
1375 		return true;
1376 
1377 	if (test_bit(EV_KEY, dev->evbit)) {
1378 		for (i = KEY_RESERVED; i < BTN_MISC; i++)
1379 			if (test_bit(i, dev->keybit))
1380 				return true;
1381 		for (i = KEY_BRL_DOT1; i <= KEY_BRL_DOT10; i++)
1382 			if (test_bit(i, dev->keybit))
1383 				return true;
1384 	}
1385 
1386 	return false;
1387 }
1388 
1389 /*
1390  * When a keyboard (or other input device) is found, the kbd_connect
1391  * function is called. The function then looks at the device, and if it
1392  * likes it, it can open it and get events from it. In this (kbd_connect)
1393  * function, we should decide which VT to bind that keyboard to initially.
1394  */
kbd_connect(struct input_handler * handler,struct input_dev * dev,const struct input_device_id * id)1395 static int kbd_connect(struct input_handler *handler, struct input_dev *dev,
1396 			const struct input_device_id *id)
1397 {
1398 	struct input_handle *handle;
1399 	int error;
1400 
1401 	handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
1402 	if (!handle)
1403 		return -ENOMEM;
1404 
1405 	handle->dev = dev;
1406 	handle->handler = handler;
1407 	handle->name = "kbd";
1408 
1409 	error = input_register_handle(handle);
1410 	if (error)
1411 		goto err_free_handle;
1412 
1413 	error = input_open_device(handle);
1414 	if (error)
1415 		goto err_unregister_handle;
1416 
1417 	return 0;
1418 
1419  err_unregister_handle:
1420 	input_unregister_handle(handle);
1421  err_free_handle:
1422 	kfree(handle);
1423 	return error;
1424 }
1425 
kbd_disconnect(struct input_handle * handle)1426 static void kbd_disconnect(struct input_handle *handle)
1427 {
1428 	input_close_device(handle);
1429 	input_unregister_handle(handle);
1430 	kfree(handle);
1431 }
1432 
1433 /*
1434  * Start keyboard handler on the new keyboard by refreshing LED state to
1435  * match the rest of the system.
1436  */
kbd_start(struct input_handle * handle)1437 static void kbd_start(struct input_handle *handle)
1438 {
1439 	tasklet_disable(&keyboard_tasklet);
1440 
1441 	if (ledstate != 0xff)
1442 		kbd_update_leds_helper(handle, &ledstate);
1443 
1444 	tasklet_enable(&keyboard_tasklet);
1445 }
1446 
1447 static const struct input_device_id kbd_ids[] = {
1448 	{
1449 		.flags = INPUT_DEVICE_ID_MATCH_EVBIT,
1450 		.evbit = { BIT_MASK(EV_KEY) },
1451 	},
1452 
1453 	{
1454 		.flags = INPUT_DEVICE_ID_MATCH_EVBIT,
1455 		.evbit = { BIT_MASK(EV_SND) },
1456 	},
1457 
1458 	{ },    /* Terminating entry */
1459 };
1460 
1461 MODULE_DEVICE_TABLE(input, kbd_ids);
1462 
1463 static struct input_handler kbd_handler = {
1464 	.event		= kbd_event,
1465 	.match		= kbd_match,
1466 	.connect	= kbd_connect,
1467 	.disconnect	= kbd_disconnect,
1468 	.start		= kbd_start,
1469 	.name		= "kbd",
1470 	.id_table	= kbd_ids,
1471 };
1472 
kbd_init(void)1473 int __init kbd_init(void)
1474 {
1475 	int i;
1476 	int error;
1477 
1478 	for (i = 0; i < MAX_NR_CONSOLES; i++) {
1479 		kbd_table[i].ledflagstate = kbd_defleds();
1480 		kbd_table[i].default_ledflagstate = kbd_defleds();
1481 		kbd_table[i].ledmode = LED_SHOW_FLAGS;
1482 		kbd_table[i].lockstate = KBD_DEFLOCK;
1483 		kbd_table[i].slockstate = 0;
1484 		kbd_table[i].modeflags = KBD_DEFMODE;
1485 		kbd_table[i].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
1486 	}
1487 
1488 	error = input_register_handler(&kbd_handler);
1489 	if (error)
1490 		return error;
1491 
1492 	tasklet_enable(&keyboard_tasklet);
1493 	tasklet_schedule(&keyboard_tasklet);
1494 
1495 	return 0;
1496 }
1497 
1498 /* Ioctl support code */
1499 
1500 /**
1501  *	vt_do_diacrit		-	diacritical table updates
1502  *	@cmd: ioctl request
1503  *	@udp: pointer to user data for ioctl
1504  *	@perm: permissions check computed by caller
1505  *
1506  *	Update the diacritical tables atomically and safely. Lock them
1507  *	against simultaneous keypresses
1508  */
vt_do_diacrit(unsigned int cmd,void __user * udp,int perm)1509 int vt_do_diacrit(unsigned int cmd, void __user *udp, int perm)
1510 {
1511 	unsigned long flags;
1512 	int asize;
1513 	int ret = 0;
1514 
1515 	switch (cmd) {
1516 	case KDGKBDIACR:
1517 	{
1518 		struct kbdiacrs __user *a = udp;
1519 		struct kbdiacr *dia;
1520 		int i;
1521 
1522 		dia = kmalloc(MAX_DIACR * sizeof(struct kbdiacr),
1523 								GFP_KERNEL);
1524 		if (!dia)
1525 			return -ENOMEM;
1526 
1527 		/* Lock the diacriticals table, make a copy and then
1528 		   copy it after we unlock */
1529 		spin_lock_irqsave(&kbd_event_lock, flags);
1530 
1531 		asize = accent_table_size;
1532 		for (i = 0; i < asize; i++) {
1533 			dia[i].diacr = conv_uni_to_8bit(
1534 						accent_table[i].diacr);
1535 			dia[i].base = conv_uni_to_8bit(
1536 						accent_table[i].base);
1537 			dia[i].result = conv_uni_to_8bit(
1538 						accent_table[i].result);
1539 		}
1540 		spin_unlock_irqrestore(&kbd_event_lock, flags);
1541 
1542 		if (put_user(asize, &a->kb_cnt))
1543 			ret = -EFAULT;
1544 		else  if (copy_to_user(a->kbdiacr, dia,
1545 				asize * sizeof(struct kbdiacr)))
1546 			ret = -EFAULT;
1547 		kfree(dia);
1548 		return ret;
1549 	}
1550 	case KDGKBDIACRUC:
1551 	{
1552 		struct kbdiacrsuc __user *a = udp;
1553 		void *buf;
1554 
1555 		buf = kmalloc(MAX_DIACR * sizeof(struct kbdiacruc),
1556 								GFP_KERNEL);
1557 		if (buf == NULL)
1558 			return -ENOMEM;
1559 
1560 		/* Lock the diacriticals table, make a copy and then
1561 		   copy it after we unlock */
1562 		spin_lock_irqsave(&kbd_event_lock, flags);
1563 
1564 		asize = accent_table_size;
1565 		memcpy(buf, accent_table, asize * sizeof(struct kbdiacruc));
1566 
1567 		spin_unlock_irqrestore(&kbd_event_lock, flags);
1568 
1569 		if (put_user(asize, &a->kb_cnt))
1570 			ret = -EFAULT;
1571 		else if (copy_to_user(a->kbdiacruc, buf,
1572 				asize*sizeof(struct kbdiacruc)))
1573 			ret = -EFAULT;
1574 		kfree(buf);
1575 		return ret;
1576 	}
1577 
1578 	case KDSKBDIACR:
1579 	{
1580 		struct kbdiacrs __user *a = udp;
1581 		struct kbdiacr *dia = NULL;
1582 		unsigned int ct;
1583 		int i;
1584 
1585 		if (!perm)
1586 			return -EPERM;
1587 		if (get_user(ct, &a->kb_cnt))
1588 			return -EFAULT;
1589 		if (ct >= MAX_DIACR)
1590 			return -EINVAL;
1591 
1592 		if (ct) {
1593 			dia = kmalloc(sizeof(struct kbdiacr) * ct,
1594 								GFP_KERNEL);
1595 			if (!dia)
1596 				return -ENOMEM;
1597 
1598 			if (copy_from_user(dia, a->kbdiacr,
1599 					sizeof(struct kbdiacr) * ct)) {
1600 				kfree(dia);
1601 				return -EFAULT;
1602 			}
1603 		}
1604 
1605 		spin_lock_irqsave(&kbd_event_lock, flags);
1606 		accent_table_size = ct;
1607 		for (i = 0; i < ct; i++) {
1608 			accent_table[i].diacr =
1609 					conv_8bit_to_uni(dia[i].diacr);
1610 			accent_table[i].base =
1611 					conv_8bit_to_uni(dia[i].base);
1612 			accent_table[i].result =
1613 					conv_8bit_to_uni(dia[i].result);
1614 		}
1615 		spin_unlock_irqrestore(&kbd_event_lock, flags);
1616 		kfree(dia);
1617 		return 0;
1618 	}
1619 
1620 	case KDSKBDIACRUC:
1621 	{
1622 		struct kbdiacrsuc __user *a = udp;
1623 		unsigned int ct;
1624 		void *buf = NULL;
1625 
1626 		if (!perm)
1627 			return -EPERM;
1628 
1629 		if (get_user(ct, &a->kb_cnt))
1630 			return -EFAULT;
1631 
1632 		if (ct >= MAX_DIACR)
1633 			return -EINVAL;
1634 
1635 		if (ct) {
1636 			buf = kmalloc(ct * sizeof(struct kbdiacruc),
1637 								GFP_KERNEL);
1638 			if (buf == NULL)
1639 				return -ENOMEM;
1640 
1641 			if (copy_from_user(buf, a->kbdiacruc,
1642 					ct * sizeof(struct kbdiacruc))) {
1643 				kfree(buf);
1644 				return -EFAULT;
1645 			}
1646 		}
1647 		spin_lock_irqsave(&kbd_event_lock, flags);
1648 		if (ct)
1649 			memcpy(accent_table, buf,
1650 					ct * sizeof(struct kbdiacruc));
1651 		accent_table_size = ct;
1652 		spin_unlock_irqrestore(&kbd_event_lock, flags);
1653 		kfree(buf);
1654 		return 0;
1655 	}
1656 	}
1657 	return ret;
1658 }
1659 
1660 /**
1661  *	vt_do_kdskbmode		-	set keyboard mode ioctl
1662  *	@console: the console to use
1663  *	@arg: the requested mode
1664  *
1665  *	Update the keyboard mode bits while holding the correct locks.
1666  *	Return 0 for success or an error code.
1667  */
vt_do_kdskbmode(int console,unsigned int arg)1668 int vt_do_kdskbmode(int console, unsigned int arg)
1669 {
1670 	struct kbd_struct *kb = kbd_table + console;
1671 	int ret = 0;
1672 	unsigned long flags;
1673 
1674 	spin_lock_irqsave(&kbd_event_lock, flags);
1675 	switch(arg) {
1676 	case K_RAW:
1677 		kb->kbdmode = VC_RAW;
1678 		break;
1679 	case K_MEDIUMRAW:
1680 		kb->kbdmode = VC_MEDIUMRAW;
1681 		break;
1682 	case K_XLATE:
1683 		kb->kbdmode = VC_XLATE;
1684 		do_compute_shiftstate();
1685 		break;
1686 	case K_UNICODE:
1687 		kb->kbdmode = VC_UNICODE;
1688 		do_compute_shiftstate();
1689 		break;
1690 	case K_OFF:
1691 		kb->kbdmode = VC_OFF;
1692 		break;
1693 	default:
1694 		ret = -EINVAL;
1695 	}
1696 	spin_unlock_irqrestore(&kbd_event_lock, flags);
1697 	return ret;
1698 }
1699 
1700 /**
1701  *	vt_do_kdskbmeta		-	set keyboard meta state
1702  *	@console: the console to use
1703  *	@arg: the requested meta state
1704  *
1705  *	Update the keyboard meta bits while holding the correct locks.
1706  *	Return 0 for success or an error code.
1707  */
vt_do_kdskbmeta(int console,unsigned int arg)1708 int vt_do_kdskbmeta(int console, unsigned int arg)
1709 {
1710 	struct kbd_struct *kb = kbd_table + console;
1711 	int ret = 0;
1712 	unsigned long flags;
1713 
1714 	spin_lock_irqsave(&kbd_event_lock, flags);
1715 	switch(arg) {
1716 	case K_METABIT:
1717 		clr_vc_kbd_mode(kb, VC_META);
1718 		break;
1719 	case K_ESCPREFIX:
1720 		set_vc_kbd_mode(kb, VC_META);
1721 		break;
1722 	default:
1723 		ret = -EINVAL;
1724 	}
1725 	spin_unlock_irqrestore(&kbd_event_lock, flags);
1726 	return ret;
1727 }
1728 
vt_do_kbkeycode_ioctl(int cmd,struct kbkeycode __user * user_kbkc,int perm)1729 int vt_do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc,
1730 								int perm)
1731 {
1732 	struct kbkeycode tmp;
1733 	int kc = 0;
1734 
1735 	if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode)))
1736 		return -EFAULT;
1737 	switch (cmd) {
1738 	case KDGETKEYCODE:
1739 		kc = getkeycode(tmp.scancode);
1740 		if (kc >= 0)
1741 			kc = put_user(kc, &user_kbkc->keycode);
1742 		break;
1743 	case KDSETKEYCODE:
1744 		if (!perm)
1745 			return -EPERM;
1746 		kc = setkeycode(tmp.scancode, tmp.keycode);
1747 		break;
1748 	}
1749 	return kc;
1750 }
1751 
1752 #define i (tmp.kb_index)
1753 #define s (tmp.kb_table)
1754 #define v (tmp.kb_value)
1755 
vt_do_kdsk_ioctl(int cmd,struct kbentry __user * user_kbe,int perm,int console)1756 int vt_do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm,
1757 						int console)
1758 {
1759 	struct kbd_struct *kb = kbd_table + console;
1760 	struct kbentry tmp;
1761 	ushort *key_map, *new_map, val, ov;
1762 	unsigned long flags;
1763 
1764 	if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
1765 		return -EFAULT;
1766 
1767 	if (!capable(CAP_SYS_TTY_CONFIG))
1768 		perm = 0;
1769 
1770 	switch (cmd) {
1771 	case KDGKBENT:
1772 		/* Ensure another thread doesn't free it under us */
1773 		spin_lock_irqsave(&kbd_event_lock, flags);
1774 		key_map = key_maps[s];
1775 		if (key_map) {
1776 		    val = U(key_map[i]);
1777 		    if (kb->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES)
1778 			val = K_HOLE;
1779 		} else
1780 		    val = (i ? K_HOLE : K_NOSUCHMAP);
1781 		spin_unlock_irqrestore(&kbd_event_lock, flags);
1782 		return put_user(val, &user_kbe->kb_value);
1783 	case KDSKBENT:
1784 		if (!perm)
1785 			return -EPERM;
1786 		if (!i && v == K_NOSUCHMAP) {
1787 			spin_lock_irqsave(&kbd_event_lock, flags);
1788 			/* deallocate map */
1789 			key_map = key_maps[s];
1790 			if (s && key_map) {
1791 			    key_maps[s] = NULL;
1792 			    if (key_map[0] == U(K_ALLOCATED)) {
1793 					kfree(key_map);
1794 					keymap_count--;
1795 			    }
1796 			}
1797 			spin_unlock_irqrestore(&kbd_event_lock, flags);
1798 			break;
1799 		}
1800 
1801 		if (KTYP(v) < NR_TYPES) {
1802 		    if (KVAL(v) > max_vals[KTYP(v)])
1803 				return -EINVAL;
1804 		} else
1805 		    if (kb->kbdmode != VC_UNICODE)
1806 				return -EINVAL;
1807 
1808 		/* ++Geert: non-PC keyboards may generate keycode zero */
1809 #if !defined(__mc68000__) && !defined(__powerpc__)
1810 		/* assignment to entry 0 only tests validity of args */
1811 		if (!i)
1812 			break;
1813 #endif
1814 
1815 		new_map = kmalloc(sizeof(plain_map), GFP_KERNEL);
1816 		if (!new_map)
1817 			return -ENOMEM;
1818 		spin_lock_irqsave(&kbd_event_lock, flags);
1819 		key_map = key_maps[s];
1820 		if (key_map == NULL) {
1821 			int j;
1822 
1823 			if (keymap_count >= MAX_NR_OF_USER_KEYMAPS &&
1824 			    !capable(CAP_SYS_RESOURCE)) {
1825 				spin_unlock_irqrestore(&kbd_event_lock, flags);
1826 				kfree(new_map);
1827 				return -EPERM;
1828 			}
1829 			key_maps[s] = new_map;
1830 			key_map = new_map;
1831 			key_map[0] = U(K_ALLOCATED);
1832 			for (j = 1; j < NR_KEYS; j++)
1833 				key_map[j] = U(K_HOLE);
1834 			keymap_count++;
1835 		} else
1836 			kfree(new_map);
1837 
1838 		ov = U(key_map[i]);
1839 		if (v == ov)
1840 			goto out;
1841 		/*
1842 		 * Attention Key.
1843 		 */
1844 		if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN)) {
1845 			spin_unlock_irqrestore(&kbd_event_lock, flags);
1846 			return -EPERM;
1847 		}
1848 		key_map[i] = U(v);
1849 		if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT))
1850 			do_compute_shiftstate();
1851 out:
1852 		spin_unlock_irqrestore(&kbd_event_lock, flags);
1853 		break;
1854 	}
1855 	return 0;
1856 }
1857 #undef i
1858 #undef s
1859 #undef v
1860 
1861 /* FIXME: This one needs untangling and locking */
vt_do_kdgkb_ioctl(int cmd,struct kbsentry __user * user_kdgkb,int perm)1862 int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
1863 {
1864 	struct kbsentry *kbs;
1865 	char *p;
1866 	u_char *q;
1867 	u_char __user *up;
1868 	int sz;
1869 	int delta;
1870 	char *first_free, *fj, *fnw;
1871 	int i, j, k;
1872 	int ret;
1873 
1874 	if (!capable(CAP_SYS_TTY_CONFIG))
1875 		perm = 0;
1876 
1877 	kbs = kmalloc(sizeof(*kbs), GFP_KERNEL);
1878 	if (!kbs) {
1879 		ret = -ENOMEM;
1880 		goto reterr;
1881 	}
1882 
1883 	/* we mostly copy too much here (512bytes), but who cares ;) */
1884 	if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) {
1885 		ret = -EFAULT;
1886 		goto reterr;
1887 	}
1888 	kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0';
1889 	i = kbs->kb_func;
1890 
1891 	switch (cmd) {
1892 	case KDGKBSENT:
1893 		sz = sizeof(kbs->kb_string) - 1; /* sz should have been
1894 						  a struct member */
1895 		up = user_kdgkb->kb_string;
1896 		p = func_table[i];
1897 		if(p)
1898 			for ( ; *p && sz; p++, sz--)
1899 				if (put_user(*p, up++)) {
1900 					ret = -EFAULT;
1901 					goto reterr;
1902 				}
1903 		if (put_user('\0', up)) {
1904 			ret = -EFAULT;
1905 			goto reterr;
1906 		}
1907 		kfree(kbs);
1908 		return ((p && *p) ? -EOVERFLOW : 0);
1909 	case KDSKBSENT:
1910 		if (!perm) {
1911 			ret = -EPERM;
1912 			goto reterr;
1913 		}
1914 
1915 		q = func_table[i];
1916 		first_free = funcbufptr + (funcbufsize - funcbufleft);
1917 		for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++)
1918 			;
1919 		if (j < MAX_NR_FUNC)
1920 			fj = func_table[j];
1921 		else
1922 			fj = first_free;
1923 
1924 		delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string);
1925 		if (delta <= funcbufleft) { 	/* it fits in current buf */
1926 		    if (j < MAX_NR_FUNC) {
1927 			memmove(fj + delta, fj, first_free - fj);
1928 			for (k = j; k < MAX_NR_FUNC; k++)
1929 			    if (func_table[k])
1930 				func_table[k] += delta;
1931 		    }
1932 		    if (!q)
1933 		      func_table[i] = fj;
1934 		    funcbufleft -= delta;
1935 		} else {			/* allocate a larger buffer */
1936 		    sz = 256;
1937 		    while (sz < funcbufsize - funcbufleft + delta)
1938 		      sz <<= 1;
1939 		    fnw = kmalloc(sz, GFP_KERNEL);
1940 		    if(!fnw) {
1941 		      ret = -ENOMEM;
1942 		      goto reterr;
1943 		    }
1944 
1945 		    if (!q)
1946 		      func_table[i] = fj;
1947 		    if (fj > funcbufptr)
1948 			memmove(fnw, funcbufptr, fj - funcbufptr);
1949 		    for (k = 0; k < j; k++)
1950 		      if (func_table[k])
1951 			func_table[k] = fnw + (func_table[k] - funcbufptr);
1952 
1953 		    if (first_free > fj) {
1954 			memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj);
1955 			for (k = j; k < MAX_NR_FUNC; k++)
1956 			  if (func_table[k])
1957 			    func_table[k] = fnw + (func_table[k] - funcbufptr) + delta;
1958 		    }
1959 		    if (funcbufptr != func_buf)
1960 		      kfree(funcbufptr);
1961 		    funcbufptr = fnw;
1962 		    funcbufleft = funcbufleft - delta + sz - funcbufsize;
1963 		    funcbufsize = sz;
1964 		}
1965 		strcpy(func_table[i], kbs->kb_string);
1966 		break;
1967 	}
1968 	ret = 0;
1969 reterr:
1970 	kfree(kbs);
1971 	return ret;
1972 }
1973 
vt_do_kdskled(int console,int cmd,unsigned long arg,int perm)1974 int vt_do_kdskled(int console, int cmd, unsigned long arg, int perm)
1975 {
1976 	struct kbd_struct *kb = kbd_table + console;
1977         unsigned long flags;
1978 	unsigned char ucval;
1979 
1980         switch(cmd) {
1981 	/* the ioctls below read/set the flags usually shown in the leds */
1982 	/* don't use them - they will go away without warning */
1983 	case KDGKBLED:
1984                 spin_lock_irqsave(&kbd_event_lock, flags);
1985 		ucval = kb->ledflagstate | (kb->default_ledflagstate << 4);
1986                 spin_unlock_irqrestore(&kbd_event_lock, flags);
1987 		return put_user(ucval, (char __user *)arg);
1988 
1989 	case KDSKBLED:
1990 		if (!perm)
1991 			return -EPERM;
1992 		if (arg & ~0x77)
1993 			return -EINVAL;
1994                 spin_lock_irqsave(&led_lock, flags);
1995 		kb->ledflagstate = (arg & 7);
1996 		kb->default_ledflagstate = ((arg >> 4) & 7);
1997 		set_leds();
1998                 spin_unlock_irqrestore(&led_lock, flags);
1999 		return 0;
2000 
2001 	/* the ioctls below only set the lights, not the functions */
2002 	/* for those, see KDGKBLED and KDSKBLED above */
2003 	case KDGETLED:
2004 		ucval = getledstate();
2005 		return put_user(ucval, (char __user *)arg);
2006 
2007 	case KDSETLED:
2008 		if (!perm)
2009 			return -EPERM;
2010 		setledstate(kb, arg);
2011 		return 0;
2012         }
2013         return -ENOIOCTLCMD;
2014 }
2015 
vt_do_kdgkbmode(int console)2016 int vt_do_kdgkbmode(int console)
2017 {
2018 	struct kbd_struct *kb = kbd_table + console;
2019 	/* This is a spot read so needs no locking */
2020 	switch (kb->kbdmode) {
2021 	case VC_RAW:
2022 		return K_RAW;
2023 	case VC_MEDIUMRAW:
2024 		return K_MEDIUMRAW;
2025 	case VC_UNICODE:
2026 		return K_UNICODE;
2027 	case VC_OFF:
2028 		return K_OFF;
2029 	default:
2030 		return K_XLATE;
2031 	}
2032 }
2033 
2034 /**
2035  *	vt_do_kdgkbmeta		-	report meta status
2036  *	@console: console to report
2037  *
2038  *	Report the meta flag status of this console
2039  */
vt_do_kdgkbmeta(int console)2040 int vt_do_kdgkbmeta(int console)
2041 {
2042 	struct kbd_struct *kb = kbd_table + console;
2043         /* Again a spot read so no locking */
2044 	return vc_kbd_mode(kb, VC_META) ? K_ESCPREFIX : K_METABIT;
2045 }
2046 
2047 /**
2048  *	vt_reset_unicode	-	reset the unicode status
2049  *	@console: console being reset
2050  *
2051  *	Restore the unicode console state to its default
2052  */
vt_reset_unicode(int console)2053 void vt_reset_unicode(int console)
2054 {
2055 	unsigned long flags;
2056 
2057 	spin_lock_irqsave(&kbd_event_lock, flags);
2058 	kbd_table[console].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
2059 	spin_unlock_irqrestore(&kbd_event_lock, flags);
2060 }
2061 
2062 /**
2063  *	vt_get_shiftstate	-	shift bit state
2064  *
2065  *	Report the shift bits from the keyboard state. We have to export
2066  *	this to support some oddities in the vt layer.
2067  */
vt_get_shift_state(void)2068 int vt_get_shift_state(void)
2069 {
2070         /* Don't lock as this is a transient report */
2071         return shift_state;
2072 }
2073 
2074 /**
2075  *	vt_reset_keyboard	-	reset keyboard state
2076  *	@console: console to reset
2077  *
2078  *	Reset the keyboard bits for a console as part of a general console
2079  *	reset event
2080  */
vt_reset_keyboard(int console)2081 void vt_reset_keyboard(int console)
2082 {
2083 	struct kbd_struct *kb = kbd_table + console;
2084 	unsigned long flags;
2085 
2086 	spin_lock_irqsave(&kbd_event_lock, flags);
2087 	set_vc_kbd_mode(kb, VC_REPEAT);
2088 	clr_vc_kbd_mode(kb, VC_CKMODE);
2089 	clr_vc_kbd_mode(kb, VC_APPLIC);
2090 	clr_vc_kbd_mode(kb, VC_CRLF);
2091 	kb->lockstate = 0;
2092 	kb->slockstate = 0;
2093 	spin_lock(&led_lock);
2094 	kb->ledmode = LED_SHOW_FLAGS;
2095 	kb->ledflagstate = kb->default_ledflagstate;
2096 	spin_unlock(&led_lock);
2097 	/* do not do set_leds here because this causes an endless tasklet loop
2098 	   when the keyboard hasn't been initialized yet */
2099 	spin_unlock_irqrestore(&kbd_event_lock, flags);
2100 }
2101 
2102 /**
2103  *	vt_get_kbd_mode_bit	-	read keyboard status bits
2104  *	@console: console to read from
2105  *	@bit: mode bit to read
2106  *
2107  *	Report back a vt mode bit. We do this without locking so the
2108  *	caller must be sure that there are no synchronization needs
2109  */
2110 
vt_get_kbd_mode_bit(int console,int bit)2111 int vt_get_kbd_mode_bit(int console, int bit)
2112 {
2113 	struct kbd_struct *kb = kbd_table + console;
2114 	return vc_kbd_mode(kb, bit);
2115 }
2116 
2117 /**
2118  *	vt_set_kbd_mode_bit	-	read keyboard status bits
2119  *	@console: console to read from
2120  *	@bit: mode bit to read
2121  *
2122  *	Set a vt mode bit. We do this without locking so the
2123  *	caller must be sure that there are no synchronization needs
2124  */
2125 
vt_set_kbd_mode_bit(int console,int bit)2126 void vt_set_kbd_mode_bit(int console, int bit)
2127 {
2128 	struct kbd_struct *kb = kbd_table + console;
2129 	unsigned long flags;
2130 
2131 	spin_lock_irqsave(&kbd_event_lock, flags);
2132 	set_vc_kbd_mode(kb, bit);
2133 	spin_unlock_irqrestore(&kbd_event_lock, flags);
2134 }
2135 
2136 /**
2137  *	vt_clr_kbd_mode_bit	-	read keyboard status bits
2138  *	@console: console to read from
2139  *	@bit: mode bit to read
2140  *
2141  *	Report back a vt mode bit. We do this without locking so the
2142  *	caller must be sure that there are no synchronization needs
2143  */
2144 
vt_clr_kbd_mode_bit(int console,int bit)2145 void vt_clr_kbd_mode_bit(int console, int bit)
2146 {
2147 	struct kbd_struct *kb = kbd_table + console;
2148 	unsigned long flags;
2149 
2150 	spin_lock_irqsave(&kbd_event_lock, flags);
2151 	clr_vc_kbd_mode(kb, bit);
2152 	spin_unlock_irqrestore(&kbd_event_lock, flags);
2153 }
2154