keyboard.c 36.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Written for linux by Johan Myreen as a translation from
 * the assembly version by Linus (with diacriticals added)
 *
 * Some additional features added by Christoph Niemann (ChN), March 1993
 *
 * Loadable keymaps by Risto Kankkunen, May 1993
 *
 * Diacriticals redone & other small changes, aeb@cwi.nl, June 1993
 * Added decr/incr_console, dynamic keymaps, Unicode support,
 * dynamic function/string keys, led setting,  Sept 1994
 * `Sticky' modifier keys, 951006.
 *
 * 11-11-96: SAK should now work in the raw mode (Martin Mares)
15
 *
L
Linus Torvalds 已提交
16 17 18 19 20 21 22 23 24
 * Modified to provide 'generic' keyboard support by Hamish Macdonald
 * Merge with the m68k keyboard driver and split-off of the PC low-level
 * parts by Geert Uytterhoeven, May 1997
 *
 * 27-05-97: Added support for the Magic SysRq Key (Martin Mares)
 * 30-07-98: Dead keys redone, aeb@cwi.nl.
 * 21-08-02: Converted to input API, major cleanup. (Vojtech Pavlik)
 */

25 26
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

J
Jan Engelhardt 已提交
27
#include <linux/consolemap.h>
L
Linus Torvalds 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/slab.h>

#include <linux/kbd_kern.h>
#include <linux/kbd_diacr.h>
#include <linux/vt_kern.h>
#include <linux/input.h>
A
Adrian Bunk 已提交
41
#include <linux/reboot.h>
42
#include <linux/notifier.h>
43
#include <linux/jiffies.h>
L
Linus Torvalds 已提交
44

45 46
#include <asm/irq_regs.h>

L
Linus Torvalds 已提交
47 48 49 50 51 52 53 54 55 56 57
extern void ctrl_alt_del(void);

/*
 * Exported functions/variables
 */

#define KBD_DEFMODE ((1 << VC_REPEAT) | (1 << VC_META))

/*
 * Some laptops take the 789uiojklm,. keys as number pad when NumLock is on.
 * This seems a good reason to start with NumLock off. On HIL keyboards
58
 * of PARISC machines however there is no NumLock key and everyone expects the keypad
L
Linus Torvalds 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
 * to be used for numbers.
 */

#if defined(CONFIG_PARISC) && (defined(CONFIG_KEYBOARD_HIL) || defined(CONFIG_KEYBOARD_HIL_OLD))
#define KBD_DEFLEDS (1 << VC_NUMLOCK)
#else
#define KBD_DEFLEDS 0
#endif

#define KBD_DEFLOCK 0

void compute_shiftstate(void);

/*
 * Handler Tables.
 */

#define K_HANDLERS\
	k_self,		k_fn,		k_spec,		k_pad,\
	k_dead,		k_cons,		k_cur,		k_shift,\
	k_meta,		k_ascii,	k_lock,		k_lowercase,\
80
	k_slock,	k_dead2,	k_brl,		k_ignore
L
Linus Torvalds 已提交
81

82
typedef void (k_handler_fn)(struct vc_data *vc, unsigned char value,
83
			    char up_flag);
L
Linus Torvalds 已提交
84
static k_handler_fn K_HANDLERS;
85
static k_handler_fn *k_handler[16] = { K_HANDLERS };
L
Linus Torvalds 已提交
86 87

#define FN_HANDLERS\
88 89 90 91 92
	fn_null,	fn_enter,	fn_show_ptregs,	fn_show_mem,\
	fn_show_state,	fn_send_intr,	fn_lastcons,	fn_caps_toggle,\
	fn_num,		fn_hold,	fn_scroll_forw,	fn_scroll_back,\
	fn_boot_it,	fn_caps_on,	fn_compose,	fn_SAK,\
	fn_dec_console, fn_inc_console, fn_spawn_con,	fn_bare_num
L
Linus Torvalds 已提交
93

94
typedef void (fn_handler_fn)(struct vc_data *vc);
L
Linus Torvalds 已提交
95 96 97 98 99 100 101 102 103 104 105
static fn_handler_fn FN_HANDLERS;
static fn_handler_fn *fn_handler[] = { FN_HANDLERS };

/*
 * Variables exported for vt_ioctl.c
 */

/* maximum values each key_handler can handle */
const int max_vals[] = {
	255, ARRAY_SIZE(func_table) - 1, ARRAY_SIZE(fn_handler) - 1, NR_PAD - 1,
	NR_DEAD - 1, 255, 3, NR_SHIFT - 1, 255, NR_ASCII - 1, NR_LOCK - 1,
106
	255, NR_LOCK - 1, 255, NR_BRL - 1
L
Linus Torvalds 已提交
107 108 109 110 111
};

const int NR_TYPES = ARRAY_SIZE(max_vals);

struct kbd_struct kbd_table[MAX_NR_CONSOLES];
112
EXPORT_SYMBOL_GPL(kbd_table);
L
Linus Torvalds 已提交
113 114
static struct kbd_struct *kbd = kbd_table;

115
struct vt_spawn_console vt_spawn_con = {
116
	.lock = __SPIN_LOCK_UNLOCKED(vt_spawn_con.lock),
117 118 119
	.pid  = NULL,
	.sig  = 0,
};
L
Linus Torvalds 已提交
120 121 122 123 124 125 126 127 128 129 130 131

/*
 * Variables exported for vt.c
 */

int shift_state = 0;

/*
 * Internal Data.
 */

static struct input_handler kbd_handler;
132
static DEFINE_SPINLOCK(kbd_event_lock);
133
static unsigned long key_down[BITS_TO_LONGS(KEY_CNT)];	/* keyboard key bitmap */
L
Linus Torvalds 已提交
134
static unsigned char shift_down[NR_SHIFT];		/* shift state counters.. */
135
static bool dead_key_next;
L
Linus Torvalds 已提交
136
static int npadch = -1;					/* -1 or number assembled on pad */
137
static unsigned int diacr;
L
Linus Torvalds 已提交
138 139 140 141 142 143 144 145 146 147 148
static char rep;					/* flag telling character repeat */

static unsigned char ledstate = 0xff;			/* undefined */
static unsigned char ledioctl;

static struct ledptr {
	unsigned int *addr;
	unsigned int mask;
	unsigned char valid:1;
} ledptrs[3];

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
/*
 * Notifier list for console keyboard events
 */
static ATOMIC_NOTIFIER_HEAD(keyboard_notifier_list);

int register_keyboard_notifier(struct notifier_block *nb)
{
	return atomic_notifier_chain_register(&keyboard_notifier_list, nb);
}
EXPORT_SYMBOL_GPL(register_keyboard_notifier);

int unregister_keyboard_notifier(struct notifier_block *nb)
{
	return atomic_notifier_chain_unregister(&keyboard_notifier_list, nb);
}
EXPORT_SYMBOL_GPL(unregister_keyboard_notifier);

L
Linus Torvalds 已提交
166
/*
167 168 169 170 171 172 173
 * Translation of scancodes to keycodes. We set them on only the first
 * keyboard in the list that accepts the scancode and keycode.
 * Explanation for not choosing the first attached keyboard anymore:
 *  USB keyboards for example have two event devices: one for all "normal"
 *  keys and one for extra function keys (like "volume up", "make coffee",
 *  etc.). So this means that scancodes for the extra function keys won't
 *  be valid for the first event device, but will be for the second.
L
Linus Torvalds 已提交
174
 */
175 176

struct getset_keycode_data {
177
	struct input_keymap_entry ke;
178 179 180 181 182 183 184
	int error;
};

static int getkeycode_helper(struct input_handle *handle, void *data)
{
	struct getset_keycode_data *d = data;

185
	d->error = input_get_keycode(handle->dev, &d->ke);
186 187 188 189

	return d->error == 0; /* stop as soon as we successfully get one */
}

L
Linus Torvalds 已提交
190 191
int getkeycode(unsigned int scancode)
{
192 193 194 195 196 197 198 199 200 201
	struct getset_keycode_data d = {
		.ke	= {
			.flags		= 0,
			.len		= sizeof(scancode),
			.keycode	= 0,
		},
		.error	= -ENODEV,
	};

	memcpy(d.ke.scancode, &scancode, sizeof(scancode));
202

203
	input_handler_for_each_handle(&kbd_handler, &d, getkeycode_helper);
L
Linus Torvalds 已提交
204

205
	return d.error ?: d.ke.keycode;
206 207 208 209 210 211
}

static int setkeycode_helper(struct input_handle *handle, void *data)
{
	struct getset_keycode_data *d = data;

212
	d->error = input_set_keycode(handle->dev, &d->ke);
213 214

	return d->error == 0; /* stop as soon as we successfully set one */
L
Linus Torvalds 已提交
215 216 217 218
}

int setkeycode(unsigned int scancode, unsigned int keycode)
{
219 220 221 222 223 224 225 226 227 228
	struct getset_keycode_data d = {
		.ke	= {
			.flags		= 0,
			.len		= sizeof(scancode),
			.keycode	= keycode,
		},
		.error	= -ENODEV,
	};

	memcpy(d.ke.scancode, &scancode, sizeof(scancode));
229

230
	input_handler_for_each_handle(&kbd_handler, &d, setkeycode_helper);
L
Linus Torvalds 已提交
231

232
	return d.error;
L
Linus Torvalds 已提交
233 234 235
}

/*
236 237
 * Making beeps and bells. Note that we prefer beeps to bells, but when
 * shutting the sound off we do both.
L
Linus Torvalds 已提交
238
 */
239 240

static int kd_sound_helper(struct input_handle *handle, void *data)
L
Linus Torvalds 已提交
241
{
242 243
	unsigned int *hz = data;
	struct input_dev *dev = handle->dev;
L
Linus Torvalds 已提交
244

245
	if (test_bit(EV_SND, dev->evbit)) {
246
		if (test_bit(SND_TONE, dev->sndbit)) {
247
			input_inject_event(handle, EV_SND, SND_TONE, *hz);
248 249 250 251
			if (*hz)
				return 0;
		}
		if (test_bit(SND_BELL, dev->sndbit))
252
			input_inject_event(handle, EV_SND, SND_BELL, *hz ? 1 : 0);
L
Linus Torvalds 已提交
253
	}
254 255 256 257 258 259 260 261 262

	return 0;
}

static void kd_nosound(unsigned long ignored)
{
	static unsigned int zero;

	input_handler_for_each_handle(&kbd_handler, &zero, kd_sound_helper);
L
Linus Torvalds 已提交
263 264
}

265
static DEFINE_TIMER(kd_mksound_timer, kd_nosound, 0, 0);
L
Linus Torvalds 已提交
266 267 268

void kd_mksound(unsigned int hz, unsigned int ticks)
{
269
	del_timer_sync(&kd_mksound_timer);
L
Linus Torvalds 已提交
270

271
	input_handler_for_each_handle(&kbd_handler, &hz, kd_sound_helper);
L
Linus Torvalds 已提交
272

273 274
	if (hz && ticks)
		mod_timer(&kd_mksound_timer, jiffies + ticks);
L
Linus Torvalds 已提交
275
}
276
EXPORT_SYMBOL(kd_mksound);
L
Linus Torvalds 已提交
277 278 279 280 281

/*
 * Setting the keyboard rate.
 */

282
static int kbd_rate_helper(struct input_handle *handle, void *data)
L
Linus Torvalds 已提交
283
{
284 285 286 287 288 289 290 291 292 293 294 295 296 297
	struct input_dev *dev = handle->dev;
	struct kbd_repeat *rep = data;

	if (test_bit(EV_REP, dev->evbit)) {

		if (rep[0].delay > 0)
			input_inject_event(handle,
					   EV_REP, REP_DELAY, rep[0].delay);
		if (rep[0].period > 0)
			input_inject_event(handle,
					   EV_REP, REP_PERIOD, rep[0].period);

		rep[1].delay = dev->rep[REP_DELAY];
		rep[1].period = dev->rep[REP_PERIOD];
L
Linus Torvalds 已提交
298
	}
299 300 301 302 303 304 305 306 307 308 309

	return 0;
}

int kbd_rate(struct kbd_repeat *rep)
{
	struct kbd_repeat data[2] = { *rep };

	input_handler_for_each_handle(&kbd_handler, data, kbd_rate_helper);
	*rep = data[1];	/* Copy currently used settings */

L
Linus Torvalds 已提交
310 311 312 313 314 315 316 317
	return 0;
}

/*
 * Helper Functions.
 */
static void put_queue(struct vc_data *vc, int ch)
{
318
	struct tty_struct *tty = vc->port.tty;
L
Linus Torvalds 已提交
319 320 321 322 323 324 325 326 327

	if (tty) {
		tty_insert_flip_char(tty, ch, 0);
		con_schedule_flip(tty);
	}
}

static void puts_queue(struct vc_data *vc, char *cp)
{
328
	struct tty_struct *tty = vc->port.tty;
L
Linus Torvalds 已提交
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352

	if (!tty)
		return;

	while (*cp) {
		tty_insert_flip_char(tty, *cp, 0);
		cp++;
	}
	con_schedule_flip(tty);
}

static void applkey(struct vc_data *vc, int key, char mode)
{
	static char buf[] = { 0x1b, 'O', 0x00, 0x00 };

	buf[1] = (mode ? 'O' : '[');
	buf[2] = key;
	puts_queue(vc, buf);
}

/*
 * Many other routines do put_queue, but I think either
 * they produce ASCII, or they produce some user-assigned
 * string, and in both cases we might assume that it is
J
Jan Engelhardt 已提交
353
 * in utf-8 already.
L
Linus Torvalds 已提交
354
 */
J
Jan Engelhardt 已提交
355
static void to_utf8(struct vc_data *vc, uint c)
L
Linus Torvalds 已提交
356 357 358 359
{
	if (c < 0x80)
		/*  0******* */
		put_queue(vc, c);
360
	else if (c < 0x800) {
L
Linus Torvalds 已提交
361
		/* 110***** 10****** */
362
		put_queue(vc, 0xc0 | (c >> 6));
L
Linus Torvalds 已提交
363
		put_queue(vc, 0x80 | (c & 0x3f));
364 365
	} else if (c < 0x10000) {
		if (c >= 0xD800 && c < 0xE000)
J
Jan Engelhardt 已提交
366 367 368
			return;
		if (c == 0xFFFF)
			return;
L
Linus Torvalds 已提交
369 370 371 372
		/* 1110**** 10****** 10****** */
		put_queue(vc, 0xe0 | (c >> 12));
		put_queue(vc, 0x80 | ((c >> 6) & 0x3f));
		put_queue(vc, 0x80 | (c & 0x3f));
373
	} else if (c < 0x110000) {
J
Jan Engelhardt 已提交
374 375 376 377 378
		/* 11110*** 10****** 10****** 10****** */
		put_queue(vc, 0xf0 | (c >> 18));
		put_queue(vc, 0x80 | ((c >> 12) & 0x3f));
		put_queue(vc, 0x80 | ((c >> 6) & 0x3f));
		put_queue(vc, 0x80 | (c & 0x3f));
379
	}
L
Linus Torvalds 已提交
380 381
}

382
/*
L
Linus Torvalds 已提交
383 384 385 386 387 388 389 390 391 392
 * Called after returning from RAW mode or when changing consoles - recompute
 * shift_down[] and shift_state from key_down[] maybe called when keymap is
 * undefined, so that shiftkey release is seen
 */
void compute_shiftstate(void)
{
	unsigned int i, j, k, sym, val;

	shift_state = 0;
	memset(shift_down, 0, sizeof(shift_down));
393

L
Linus Torvalds 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
	for (i = 0; i < ARRAY_SIZE(key_down); i++) {

		if (!key_down[i])
			continue;

		k = i * BITS_PER_LONG;

		for (j = 0; j < BITS_PER_LONG; j++, k++) {

			if (!test_bit(k, key_down))
				continue;

			sym = U(key_maps[0][k]);
			if (KTYP(sym) != KT_SHIFT && KTYP(sym) != KT_SLOCK)
				continue;

			val = KVAL(sym);
			if (val == KVAL(K_CAPSSHIFT))
				val = KVAL(K_SHIFT);

			shift_down[val]++;
			shift_state |= (1 << val);
		}
	}
}

/*
 * We have a combining character DIACR here, followed by the character CH.
 * If the combination occurs in the table, return the corresponding value.
 * Otherwise, if CH is a space or equals DIACR, return DIACR.
 * Otherwise, conclude that DIACR was not combining after all,
 * queue it and return CH.
 */
427
static unsigned int handle_diacr(struct vc_data *vc, unsigned int ch)
L
Linus Torvalds 已提交
428
{
429
	unsigned int d = diacr;
L
Linus Torvalds 已提交
430 431 432 433
	unsigned int i;

	diacr = 0;

434 435 436 437 438 439 440
	if ((d & ~0xff) == BRL_UC_ROW) {
		if ((ch & ~0xff) == BRL_UC_ROW)
			return d | ch;
	} else {
		for (i = 0; i < accent_table_size; i++)
			if (accent_table[i].diacr == d && accent_table[i].base == ch)
				return accent_table[i].result;
L
Linus Torvalds 已提交
441 442
	}

443
	if (ch == ' ' || ch == (BRL_UC_ROW|0) || ch == d)
L
Linus Torvalds 已提交
444 445
		return d;

446
	if (kbd->kbdmode == VC_UNICODE)
S
Samuel Thibault 已提交
447 448 449 450 451 452
		to_utf8(vc, d);
	else {
		int c = conv_uni_to_8bit(d);
		if (c != -1)
			put_queue(vc, c);
	}
453

L
Linus Torvalds 已提交
454 455 456 457 458 459
	return ch;
}

/*
 * Special function handlers
 */
460
static void fn_enter(struct vc_data *vc)
L
Linus Torvalds 已提交
461 462
{
	if (diacr) {
463
		if (kbd->kbdmode == VC_UNICODE)
S
Samuel Thibault 已提交
464 465 466 467 468 469
			to_utf8(vc, diacr);
		else {
			int c = conv_uni_to_8bit(diacr);
			if (c != -1)
				put_queue(vc, c);
		}
L
Linus Torvalds 已提交
470 471
		diacr = 0;
	}
472

L
Linus Torvalds 已提交
473 474 475 476 477
	put_queue(vc, 13);
	if (vc_kbd_mode(kbd, VC_CRLF))
		put_queue(vc, 10);
}

478
static void fn_caps_toggle(struct vc_data *vc)
L
Linus Torvalds 已提交
479 480 481
{
	if (rep)
		return;
482

L
Linus Torvalds 已提交
483 484 485
	chg_vc_kbd_led(kbd, VC_CAPSLOCK);
}

486
static void fn_caps_on(struct vc_data *vc)
L
Linus Torvalds 已提交
487 488 489
{
	if (rep)
		return;
490

L
Linus Torvalds 已提交
491 492 493
	set_vc_kbd_led(kbd, VC_CAPSLOCK);
}

494
static void fn_show_ptregs(struct vc_data *vc)
L
Linus Torvalds 已提交
495
{
496
	struct pt_regs *regs = get_irq_regs();
497

L
Linus Torvalds 已提交
498 499 500 501
	if (regs)
		show_regs(regs);
}

502
static void fn_hold(struct vc_data *vc)
L
Linus Torvalds 已提交
503
{
504
	struct tty_struct *tty = vc->port.tty;
L
Linus Torvalds 已提交
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519

	if (rep || !tty)
		return;

	/*
	 * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty);
	 * these routines are also activated by ^S/^Q.
	 * (And SCROLLOCK can also be set by the ioctl KDSKBLED.)
	 */
	if (tty->stopped)
		start_tty(tty);
	else
		stop_tty(tty);
}

520
static void fn_num(struct vc_data *vc)
L
Linus Torvalds 已提交
521
{
522
	if (vc_kbd_mode(kbd, VC_APPLIC))
L
Linus Torvalds 已提交
523 524
		applkey(vc, 'P', 1);
	else
525
		fn_bare_num(vc);
L
Linus Torvalds 已提交
526 527 528 529 530 531 532 533
}

/*
 * Bind this to Shift-NumLock if you work in application keypad mode
 * but want to be able to change the NumLock flag.
 * Bind this to NumLock if you prefer that the NumLock key always
 * changes the NumLock flag.
 */
534
static void fn_bare_num(struct vc_data *vc)
L
Linus Torvalds 已提交
535 536 537 538 539
{
	if (!rep)
		chg_vc_kbd_led(kbd, VC_NUMLOCK);
}

540
static void fn_lastcons(struct vc_data *vc)
L
Linus Torvalds 已提交
541 542 543 544 545
{
	/* switch to the last used console, ChN */
	set_console(last_console);
}

546
static void fn_dec_console(struct vc_data *vc)
L
Linus Torvalds 已提交
547 548 549 550 551 552 553
{
	int i, cur = fg_console;

	/* Currently switching?  Queue this next switch relative to that. */
	if (want_console != -1)
		cur = want_console;

554
	for (i = cur - 1; i != cur; i--) {
L
Linus Torvalds 已提交
555
		if (i == -1)
556
			i = MAX_NR_CONSOLES - 1;
L
Linus Torvalds 已提交
557 558 559 560 561 562
		if (vc_cons_allocated(i))
			break;
	}
	set_console(i);
}

563
static void fn_inc_console(struct vc_data *vc)
L
Linus Torvalds 已提交
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
{
	int i, cur = fg_console;

	/* Currently switching?  Queue this next switch relative to that. */
	if (want_console != -1)
		cur = want_console;

	for (i = cur+1; i != cur; i++) {
		if (i == MAX_NR_CONSOLES)
			i = 0;
		if (vc_cons_allocated(i))
			break;
	}
	set_console(i);
}

580
static void fn_send_intr(struct vc_data *vc)
L
Linus Torvalds 已提交
581
{
582
	struct tty_struct *tty = vc->port.tty;
L
Linus Torvalds 已提交
583 584 585 586 587 588 589

	if (!tty)
		return;
	tty_insert_flip_char(tty, 0, TTY_BREAK);
	con_schedule_flip(tty);
}

590
static void fn_scroll_forw(struct vc_data *vc)
L
Linus Torvalds 已提交
591 592 593 594
{
	scrollfront(vc, 0);
}

595
static void fn_scroll_back(struct vc_data *vc)
L
Linus Torvalds 已提交
596 597 598 599
{
	scrollback(vc, 0);
}

600
static void fn_show_mem(struct vc_data *vc)
L
Linus Torvalds 已提交
601
{
602
	show_mem(0);
L
Linus Torvalds 已提交
603 604
}

605
static void fn_show_state(struct vc_data *vc)
L
Linus Torvalds 已提交
606 607 608 609
{
	show_state();
}

610
static void fn_boot_it(struct vc_data *vc)
L
Linus Torvalds 已提交
611 612 613 614
{
	ctrl_alt_del();
}

615
static void fn_compose(struct vc_data *vc)
L
Linus Torvalds 已提交
616
{
617
	dead_key_next = true;
L
Linus Torvalds 已提交
618 619
}

620
static void fn_spawn_con(struct vc_data *vc)
L
Linus Torvalds 已提交
621
{
622 623 624 625 626 627 628
	spin_lock(&vt_spawn_con.lock);
	if (vt_spawn_con.pid)
		if (kill_pid(vt_spawn_con.pid, vt_spawn_con.sig, 1)) {
			put_pid(vt_spawn_con.pid);
			vt_spawn_con.pid = NULL;
		}
	spin_unlock(&vt_spawn_con.lock);
L
Linus Torvalds 已提交
629 630
}

631
static void fn_SAK(struct vc_data *vc)
L
Linus Torvalds 已提交
632
{
633 634
	struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work;
	schedule_work(SAK_work);
L
Linus Torvalds 已提交
635 636
}

637
static void fn_null(struct vc_data *vc)
L
Linus Torvalds 已提交
638 639 640 641 642 643 644
{
	compute_shiftstate();
}

/*
 * Special key handlers
 */
645
static void k_ignore(struct vc_data *vc, unsigned char value, char up_flag)
L
Linus Torvalds 已提交
646 647 648
{
}

649
static void k_spec(struct vc_data *vc, unsigned char value, char up_flag)
L
Linus Torvalds 已提交
650 651 652 653 654
{
	if (up_flag)
		return;
	if (value >= ARRAY_SIZE(fn_handler))
		return;
655
	if ((kbd->kbdmode == VC_RAW ||
656 657
	     kbd->kbdmode == VC_MEDIUMRAW ||
	     kbd->kbdmode == VC_OFF) &&
L
Linus Torvalds 已提交
658 659
	     value != KVAL(K_SAK))
		return;		/* SAK is allowed even in raw mode */
660
	fn_handler[value](vc);
L
Linus Torvalds 已提交
661 662
}

663
static void k_lowercase(struct vc_data *vc, unsigned char value, char up_flag)
L
Linus Torvalds 已提交
664
{
665
	pr_err("k_lowercase was called - impossible\n");
L
Linus Torvalds 已提交
666 667
}

668
static void k_unicode(struct vc_data *vc, unsigned int value, char up_flag)
L
Linus Torvalds 已提交
669 670 671 672 673 674 675 676
{
	if (up_flag)
		return;		/* no action, if this is a key release */

	if (diacr)
		value = handle_diacr(vc, value);

	if (dead_key_next) {
677
		dead_key_next = false;
L
Linus Torvalds 已提交
678 679 680
		diacr = value;
		return;
	}
681
	if (kbd->kbdmode == VC_UNICODE)
S
Samuel Thibault 已提交
682 683 684 685 686 687
		to_utf8(vc, value);
	else {
		int c = conv_uni_to_8bit(value);
		if (c != -1)
			put_queue(vc, c);
	}
L
Linus Torvalds 已提交
688 689 690 691 692 693 694
}

/*
 * Handle dead key. Note that we now may have several
 * dead keys modifying the same character. Very useful
 * for Vietnamese.
 */
695
static void k_deadunicode(struct vc_data *vc, unsigned int value, char up_flag)
L
Linus Torvalds 已提交
696 697 698
{
	if (up_flag)
		return;
699

L
Linus Torvalds 已提交
700 701 702
	diacr = (diacr ? handle_diacr(vc, value) : value);
}

703
static void k_self(struct vc_data *vc, unsigned char value, char up_flag)
704
{
705
	k_unicode(vc, conv_8bit_to_uni(value), up_flag);
706 707
}

708
static void k_dead2(struct vc_data *vc, unsigned char value, char up_flag)
709
{
710
	k_deadunicode(vc, value, up_flag);
711 712
}

L
Linus Torvalds 已提交
713 714 715
/*
 * Obsolete - for backwards compatibility only
 */
716
static void k_dead(struct vc_data *vc, unsigned char value, char up_flag)
L
Linus Torvalds 已提交
717
{
718
	static const unsigned char ret_diacr[NR_DEAD] = {'`', '\'', '^', '~', '"', ',' };
719 720

	k_deadunicode(vc, ret_diacr[value], up_flag);
L
Linus Torvalds 已提交
721 722
}

723
static void k_cons(struct vc_data *vc, unsigned char value, char up_flag)
L
Linus Torvalds 已提交
724 725 726
{
	if (up_flag)
		return;
727

L
Linus Torvalds 已提交
728 729 730
	set_console(value);
}

731
static void k_fn(struct vc_data *vc, unsigned char value, char up_flag)
L
Linus Torvalds 已提交
732 733 734
{
	if (up_flag)
		return;
735 736

	if ((unsigned)value < ARRAY_SIZE(func_table)) {
L
Linus Torvalds 已提交
737 738 739
		if (func_table[value])
			puts_queue(vc, func_table[value]);
	} else
740
		pr_err("k_fn called with value=%d\n", value);
L
Linus Torvalds 已提交
741 742
}

743
static void k_cur(struct vc_data *vc, unsigned char value, char up_flag)
L
Linus Torvalds 已提交
744
{
745
	static const char cur_chars[] = "BDCA";
L
Linus Torvalds 已提交
746 747 748

	if (up_flag)
		return;
749

L
Linus Torvalds 已提交
750 751 752
	applkey(vc, cur_chars[value], vc_kbd_mode(kbd, VC_CKMODE));
}

753
static void k_pad(struct vc_data *vc, unsigned char value, char up_flag)
L
Linus Torvalds 已提交
754
{
755 756
	static const char pad_chars[] = "0123456789+-*/\015,.?()#";
	static const char app_map[] = "pqrstuvwxylSRQMnnmPQS";
L
Linus Torvalds 已提交
757 758 759 760 761 762 763 764 765 766

	if (up_flag)
		return;		/* no action, if this is a key release */

	/* kludge... shift forces cursor/number keys */
	if (vc_kbd_mode(kbd, VC_APPLIC) && !shift_down[KG_SHIFT]) {
		applkey(vc, app_map[value], 1);
		return;
	}

767 768
	if (!vc_kbd_led(kbd, VC_NUMLOCK)) {

L
Linus Torvalds 已提交
769
		switch (value) {
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
		case KVAL(K_PCOMMA):
		case KVAL(K_PDOT):
			k_fn(vc, KVAL(K_REMOVE), 0);
			return;
		case KVAL(K_P0):
			k_fn(vc, KVAL(K_INSERT), 0);
			return;
		case KVAL(K_P1):
			k_fn(vc, KVAL(K_SELECT), 0);
			return;
		case KVAL(K_P2):
			k_cur(vc, KVAL(K_DOWN), 0);
			return;
		case KVAL(K_P3):
			k_fn(vc, KVAL(K_PGDN), 0);
			return;
		case KVAL(K_P4):
			k_cur(vc, KVAL(K_LEFT), 0);
			return;
		case KVAL(K_P6):
			k_cur(vc, KVAL(K_RIGHT), 0);
			return;
		case KVAL(K_P7):
			k_fn(vc, KVAL(K_FIND), 0);
			return;
		case KVAL(K_P8):
			k_cur(vc, KVAL(K_UP), 0);
			return;
		case KVAL(K_P9):
			k_fn(vc, KVAL(K_PGUP), 0);
			return;
		case KVAL(K_P5):
			applkey(vc, 'G', vc_kbd_mode(kbd, VC_APPLIC));
			return;
L
Linus Torvalds 已提交
804
		}
805
	}
L
Linus Torvalds 已提交
806 807 808 809 810 811

	put_queue(vc, pad_chars[value]);
	if (value == KVAL(K_PENTER) && vc_kbd_mode(kbd, VC_CRLF))
		put_queue(vc, 10);
}

812
static void k_shift(struct vc_data *vc, unsigned char value, char up_flag)
L
Linus Torvalds 已提交
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
{
	int old_state = shift_state;

	if (rep)
		return;
	/*
	 * Mimic typewriter:
	 * a CapsShift key acts like Shift but undoes CapsLock
	 */
	if (value == KVAL(K_CAPSSHIFT)) {
		value = KVAL(K_SHIFT);
		if (!up_flag)
			clr_vc_kbd_led(kbd, VC_CAPSLOCK);
	}

	if (up_flag) {
		/*
		 * handle the case that two shift or control
		 * keys are depressed simultaneously
		 */
		if (shift_down[value])
			shift_down[value]--;
	} else
		shift_down[value]++;

	if (shift_down[value])
		shift_state |= (1 << value);
	else
		shift_state &= ~(1 << value);

	/* kludge */
	if (up_flag && shift_state != old_state && npadch != -1) {
		if (kbd->kbdmode == VC_UNICODE)
J
Jan Engelhardt 已提交
846
			to_utf8(vc, npadch);
L
Linus Torvalds 已提交
847 848 849 850 851 852
		else
			put_queue(vc, npadch & 0xff);
		npadch = -1;
	}
}

853
static void k_meta(struct vc_data *vc, unsigned char value, char up_flag)
L
Linus Torvalds 已提交
854 855 856 857 858 859 860 861 862 863 864
{
	if (up_flag)
		return;

	if (vc_kbd_mode(kbd, VC_META)) {
		put_queue(vc, '\033');
		put_queue(vc, value);
	} else
		put_queue(vc, value | 0x80);
}

865
static void k_ascii(struct vc_data *vc, unsigned char value, char up_flag)
L
Linus Torvalds 已提交
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
{
	int base;

	if (up_flag)
		return;

	if (value < 10) {
		/* decimal input of code, while Alt depressed */
		base = 10;
	} else {
		/* hexadecimal input of code, while AltGr depressed */
		value -= 10;
		base = 16;
	}

	if (npadch == -1)
		npadch = value;
	else
		npadch = npadch * base + value;
}

887
static void k_lock(struct vc_data *vc, unsigned char value, char up_flag)
L
Linus Torvalds 已提交
888 889 890
{
	if (up_flag || rep)
		return;
891

L
Linus Torvalds 已提交
892 893 894
	chg_vc_kbd_lock(kbd, value);
}

895
static void k_slock(struct vc_data *vc, unsigned char value, char up_flag)
L
Linus Torvalds 已提交
896
{
897
	k_shift(vc, value, up_flag);
L
Linus Torvalds 已提交
898 899
	if (up_flag || rep)
		return;
900

L
Linus Torvalds 已提交
901 902 903 904 905 906 907 908
	chg_vc_kbd_slock(kbd, value);
	/* try to make Alt, oops, AltGr and such work */
	if (!key_maps[kbd->lockstate ^ kbd->slockstate]) {
		kbd->slockstate = 0;
		chg_vc_kbd_slock(kbd, value);
	}
}

909
/* by default, 300ms interval for combination release */
910 911 912 913 914 915 916 917
static unsigned brl_timeout = 300;
MODULE_PARM_DESC(brl_timeout, "Braille keys release delay in ms (0 for commit on first key release)");
module_param(brl_timeout, uint, 0644);

static unsigned brl_nbchords = 1;
MODULE_PARM_DESC(brl_nbchords, "Number of chords that produce a braille pattern (0 for dead chords)");
module_param(brl_nbchords, uint, 0644);

918
static void k_brlcommit(struct vc_data *vc, unsigned int pattern, char up_flag)
919 920 921 922 923
{
	static unsigned long chords;
	static unsigned committed;

	if (!brl_nbchords)
924
		k_deadunicode(vc, BRL_UC_ROW | pattern, up_flag);
925 926 927 928
	else {
		committed |= pattern;
		chords++;
		if (chords == brl_nbchords) {
929
			k_unicode(vc, BRL_UC_ROW | committed, up_flag);
930 931 932 933 934 935
			chords = 0;
			committed = 0;
		}
	}
}

936
static void k_brl(struct vc_data *vc, unsigned char value, char up_flag)
937
{
938
	static unsigned pressed, committing;
939 940 941 942
	static unsigned long releasestart;

	if (kbd->kbdmode != VC_UNICODE) {
		if (!up_flag)
943
			pr_warning("keyboard mode must be unicode for braille patterns\n");
944 945 946 947
		return;
	}

	if (!value) {
948
		k_unicode(vc, BRL_UC_ROW, up_flag);
949 950 951 952 953 954
		return;
	}

	if (value > 8)
		return;

955
	if (!up_flag) {
956 957 958
		pressed |= 1 << (value - 1);
		if (!brl_timeout)
			committing = pressed;
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
	} else if (brl_timeout) {
		if (!committing ||
		    time_after(jiffies,
			       releasestart + msecs_to_jiffies(brl_timeout))) {
			committing = pressed;
			releasestart = jiffies;
		}
		pressed &= ~(1 << (value - 1));
		if (!pressed && committing) {
			k_brlcommit(vc, committing, 0);
			committing = 0;
		}
	} else {
		if (committing) {
			k_brlcommit(vc, committing, 0);
			committing = 0;
		}
		pressed &= ~(1 << (value - 1));
977 978 979
	}
}

L
Linus Torvalds 已提交
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
/*
 * The leds display either (i) the status of NumLock, CapsLock, ScrollLock,
 * or (ii) whatever pattern of lights people want to show using KDSETLED,
 * or (iii) specified bits of specified words in kernel memory.
 */
unsigned char getledstate(void)
{
	return ledstate;
}

void setledstate(struct kbd_struct *kbd, unsigned int led)
{
	if (!(led & ~7)) {
		ledioctl = led;
		kbd->ledmode = LED_SHOW_IOCTL;
	} else
		kbd->ledmode = LED_SHOW_FLAGS;
997

L
Linus Torvalds 已提交
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
	set_leds();
}

static inline unsigned char getleds(void)
{
	struct kbd_struct *kbd = kbd_table + fg_console;
	unsigned char leds;
	int i;

	if (kbd->ledmode == LED_SHOW_IOCTL)
		return ledioctl;

	leds = kbd->ledflagstate;

	if (kbd->ledmode == LED_SHOW_MEM) {
		for (i = 0; i < 3; i++)
			if (ledptrs[i].valid) {
				if (*ledptrs[i].addr & ledptrs[i].mask)
					leds |= (1 << i);
				else
					leds &= ~(1 << i);
			}
	}
	return leds;
}

1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
static int kbd_update_leds_helper(struct input_handle *handle, void *data)
{
	unsigned char leds = *(unsigned char *)data;

	if (test_bit(EV_LED, handle->dev->evbit)) {
		input_inject_event(handle, EV_LED, LED_SCROLLL, !!(leds & 0x01));
		input_inject_event(handle, EV_LED, LED_NUML,    !!(leds & 0x02));
		input_inject_event(handle, EV_LED, LED_CAPSL,   !!(leds & 0x04));
		input_inject_event(handle, EV_SYN, SYN_REPORT, 0);
	}

	return 0;
}

L
Linus Torvalds 已提交
1038
/*
1039 1040 1041 1042 1043
 * This is the tasklet that updates LED state on all keyboards
 * attached to the box. The reason we use tasklet is that we
 * need to handle the scenario when keyboard handler is not
 * registered yet but we already getting updates form VT to
 * update led state.
L
Linus Torvalds 已提交
1044 1045 1046 1047 1048 1049
 */
static void kbd_bh(unsigned long dummy)
{
	unsigned char leds = getleds();

	if (leds != ledstate) {
1050 1051 1052
		input_handler_for_each_handle(&kbd_handler, &leds,
					      kbd_update_leds_helper);
		ledstate = leds;
L
Linus Torvalds 已提交
1053 1054 1055 1056 1057 1058
	}
}

DECLARE_TASKLET_DISABLED(keyboard_tasklet, kbd_bh, 0);

#if defined(CONFIG_X86) || defined(CONFIG_IA64) || defined(CONFIG_ALPHA) ||\
1059 1060
    defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_SPARC) ||\
    defined(CONFIG_PARISC) || defined(CONFIG_SUPERH) ||\
1061 1062
    (defined(CONFIG_ARM) && defined(CONFIG_KEYBOARD_ATKBD) && !defined(CONFIG_ARCH_RPC)) ||\
    defined(CONFIG_AVR32)
L
Linus Torvalds 已提交
1063 1064 1065 1066

#define HW_RAW(dev) (test_bit(EV_MSC, dev->evbit) && test_bit(MSC_RAW, dev->mscbit) &&\
			((dev)->id.bustype == BUS_I8042) && ((dev)->id.vendor == 0x0001) && ((dev)->id.product == 0x0001))

1067
static const unsigned short x86_keycodes[256] =
L
Linus Torvalds 已提交
1068 1069 1070 1071 1072 1073
	{ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
	 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
	 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
	 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
	 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
	 80, 81, 82, 83, 84,118, 86, 87, 88,115,120,119,121,112,123, 92,
1074
	284,285,309,  0,312, 91,327,328,329,331,333,335,336,337,338,339,
L
Linus Torvalds 已提交
1075 1076
	367,288,302,304,350, 89,334,326,267,126,268,269,125,347,348,349,
	360,261,262,263,268,376,100,101,321,316,373,286,289,102,351,355,
1077 1078
	103,104,105,275,287,279,258,106,274,107,294,364,358,363,362,361,
	291,108,381,281,290,272,292,305,280, 99,112,257,306,359,113,114,
L
Linus Torvalds 已提交
1079 1080 1081 1082 1083
	264,117,271,374,379,265,266, 93, 94, 95, 85,259,375,260, 90,116,
	377,109,111,277,278,282,283,295,296,297,299,300,301,293,303,307,
	308,310,313,314,315,317,318,319,320,357,322,323,324,325,276,330,
	332,340,365,342,343,344,345,346,356,270,341,368,369,370,371,372 };

1084
#ifdef CONFIG_SPARC
1085
static int sparc_l1_a_state;
L
Linus Torvalds 已提交
1086 1087 1088
extern void sun_do_break(void);
#endif

1089
static int emulate_raw(struct vc_data *vc, unsigned int keycode,
L
Linus Torvalds 已提交
1090 1091
		       unsigned char up_flag)
{
1092
	int code;
L
Linus Torvalds 已提交
1093 1094

	switch (keycode) {
1095

1096 1097 1098 1099 1100
	case KEY_PAUSE:
		put_queue(vc, 0xe1);
		put_queue(vc, 0x1d | up_flag);
		put_queue(vc, 0x45 | up_flag);
		break;
1101

1102 1103 1104 1105
	case KEY_HANGEUL:
		if (!up_flag)
			put_queue(vc, 0xf2);
		break;
L
Linus Torvalds 已提交
1106

1107 1108 1109 1110
	case KEY_HANJA:
		if (!up_flag)
			put_queue(vc, 0xf1);
		break;
1111

1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
	case KEY_SYSRQ:
		/*
		 * Real AT keyboards (that's what we're trying
		 * to emulate here emit 0xe0 0x2a 0xe0 0x37 when
		 * pressing PrtSc/SysRq alone, but simply 0x54
		 * when pressing Alt+PrtSc/SysRq.
		 */
		if (test_bit(KEY_LEFTALT, key_down) ||
		    test_bit(KEY_RIGHTALT, key_down)) {
			put_queue(vc, 0x54 | up_flag);
		} else {
			put_queue(vc, 0xe0);
			put_queue(vc, 0x2a | up_flag);
			put_queue(vc, 0xe0);
			put_queue(vc, 0x37 | up_flag);
		}
		break;
L
Linus Torvalds 已提交
1129

1130 1131 1132
	default:
		if (keycode > 255)
			return -1;
L
Linus Torvalds 已提交
1133

1134 1135 1136
		code = x86_keycodes[keycode];
		if (!code)
			return -1;
L
Linus Torvalds 已提交
1137

1138 1139 1140 1141 1142
		if (code & 0x100)
			put_queue(vc, 0xe0);
		put_queue(vc, (code & 0x7f) | up_flag);

		break;
L
Linus Torvalds 已提交
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
	}

	return 0;
}

#else

#define HW_RAW(dev)	0

static int emulate_raw(struct vc_data *vc, unsigned int keycode, unsigned char up_flag)
{
	if (keycode > 127)
		return -1;

	put_queue(vc, keycode | up_flag);
	return 0;
}
#endif

static void kbd_rawcode(unsigned char data)
{
	struct vc_data *vc = vc_cons[fg_console].d;
1165

1166
	kbd = kbd_table + vc->vc_num;
L
Linus Torvalds 已提交
1167 1168 1169 1170
	if (kbd->kbdmode == VC_RAW)
		put_queue(vc, data);
}

1171
static void kbd_keycode(unsigned int keycode, int down, int hw_raw)
L
Linus Torvalds 已提交
1172 1173 1174
{
	struct vc_data *vc = vc_cons[fg_console].d;
	unsigned short keysym, *key_map;
1175 1176
	unsigned char type;
	bool raw_mode;
L
Linus Torvalds 已提交
1177 1178
	struct tty_struct *tty;
	int shift_final;
1179
	struct keyboard_notifier_param param = { .vc = vc, .value = keycode, .down = down };
1180
	int rc;
L
Linus Torvalds 已提交
1181

1182
	tty = vc->port.tty;
L
Linus Torvalds 已提交
1183 1184 1185 1186 1187 1188

	if (tty && (!tty->driver_data)) {
		/* No driver data? Strange. Okay we fix it then. */
		tty->driver_data = vc;
	}

1189
	kbd = kbd_table + vc->vc_num;
L
Linus Torvalds 已提交
1190

1191
#ifdef CONFIG_SPARC
L
Linus Torvalds 已提交
1192 1193 1194 1195 1196 1197
	if (keycode == KEY_STOP)
		sparc_l1_a_state = down;
#endif

	rep = (down == 2);

1198 1199
	raw_mode = (kbd->kbdmode == VC_RAW);
	if (raw_mode && !hw_raw)
L
Linus Torvalds 已提交
1200
		if (emulate_raw(vc, keycode, !down << 7))
1201
			if (keycode < BTN_MISC && printk_ratelimit())
1202 1203
				pr_warning("can't emulate rawmode for keycode %d\n",
					   keycode);
L
Linus Torvalds 已提交
1204

1205
#ifdef CONFIG_SPARC
L
Linus Torvalds 已提交
1206
	if (keycode == KEY_A && sparc_l1_a_state) {
1207
		sparc_l1_a_state = false;
L
Linus Torvalds 已提交
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
		sun_do_break();
	}
#endif

	if (kbd->kbdmode == VC_MEDIUMRAW) {
		/*
		 * This is extended medium raw mode, with keys above 127
		 * encoded as 0, high 7 bits, low 7 bits, with the 0 bearing
		 * the 'up' flag if needed. 0 is reserved, so this shouldn't
		 * interfere with anything else. The two bytes after 0 will
		 * always have the up flag set not to interfere with older
		 * applications. This allows for 16384 different keycodes,
		 * which should be enough.
		 */
		if (keycode < 128) {
			put_queue(vc, keycode | (!down << 7));
		} else {
			put_queue(vc, !down << 7);
			put_queue(vc, (keycode >> 7) | 0x80);
			put_queue(vc, keycode | 0x80);
		}
1229
		raw_mode = true;
L
Linus Torvalds 已提交
1230 1231 1232 1233 1234 1235 1236
	}

	if (down)
		set_bit(keycode, key_down);
	else
		clear_bit(keycode, key_down);

1237 1238
	if (rep &&
	    (!vc_kbd_mode(kbd, VC_REPEAT) ||
A
Alan Cox 已提交
1239
	     (tty && !L_ECHO(tty) && tty_chars_in_buffer(tty)))) {
L
Linus Torvalds 已提交
1240 1241
		/*
		 * Don't repeat a key if the input buffers are not empty and the
1242
		 * characters get aren't echoed locally. This makes key repeat
L
Linus Torvalds 已提交
1243 1244 1245 1246 1247
		 * usable with slow applications and under heavy loads.
		 */
		return;
	}

1248
	param.shift = shift_final = (shift_state | kbd->slockstate) ^ kbd->lockstate;
1249
	param.ledstate = kbd->ledflagstate;
L
Linus Torvalds 已提交
1250 1251
	key_map = key_maps[shift_final];

1252 1253 1254 1255 1256
	rc = atomic_notifier_call_chain(&keyboard_notifier_list,
					KBD_KEYCODE, &param);
	if (rc == NOTIFY_STOP || !key_map) {
		atomic_notifier_call_chain(&keyboard_notifier_list,
					   KBD_UNBOUND_KEYCODE, &param);
L
Linus Torvalds 已提交
1257 1258 1259 1260 1261
		compute_shiftstate();
		kbd->slockstate = 0;
		return;
	}

1262
	if (keycode < NR_KEYS)
1263
		keysym = key_map[keycode];
1264 1265 1266 1267
	else if (keycode >= KEY_BRL_DOT1 && keycode <= KEY_BRL_DOT8)
		keysym = U(K(KT_BRL, keycode - KEY_BRL_DOT1 + 1));
	else
		return;
L
Linus Torvalds 已提交
1268 1269 1270 1271

	type = KTYP(keysym);

	if (type < 0xf0) {
1272
		param.value = keysym;
1273 1274 1275 1276 1277
		rc = atomic_notifier_call_chain(&keyboard_notifier_list,
						KBD_UNICODE, &param);
		if (rc != NOTIFY_STOP)
			if (down && !raw_mode)
				to_utf8(vc, keysym);
L
Linus Torvalds 已提交
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
		return;
	}

	type -= 0xf0;

	if (type == KT_LETTER) {
		type = KT_LATIN;
		if (vc_kbd_led(kbd, VC_CAPSLOCK)) {
			key_map = key_maps[shift_final ^ (1 << KG_SHIFT)];
			if (key_map)
				keysym = key_map[keycode];
		}
	}
1291

1292 1293 1294 1295
	param.value = keysym;
	rc = atomic_notifier_call_chain(&keyboard_notifier_list,
					KBD_KEYSYM, &param);
	if (rc == NOTIFY_STOP)
1296 1297
		return;

1298
	if ((raw_mode || kbd->kbdmode == VC_OFF) && type != KT_SPEC && type != KT_SHIFT)
1299
		return;
L
Linus Torvalds 已提交
1300

1301
	(*k_handler[type])(vc, keysym & 0xff, !down);
L
Linus Torvalds 已提交
1302

1303
	param.ledstate = kbd->ledflagstate;
1304 1305
	atomic_notifier_call_chain(&keyboard_notifier_list, KBD_POST_KEYSYM, &param);

L
Linus Torvalds 已提交
1306 1307 1308 1309
	if (type != KT_SLOCK)
		kbd->slockstate = 0;
}

1310
static void kbd_event(struct input_handle *handle, unsigned int event_type,
L
Linus Torvalds 已提交
1311 1312
		      unsigned int event_code, int value)
{
1313 1314 1315
	/* We are called with interrupts disabled, just take the lock */
	spin_lock(&kbd_event_lock);

L
Linus Torvalds 已提交
1316 1317 1318
	if (event_type == EV_MSC && event_code == MSC_RAW && HW_RAW(handle->dev))
		kbd_rawcode(value);
	if (event_type == EV_KEY)
1319
		kbd_keycode(event_code, value, HW_RAW(handle->dev));
1320 1321 1322

	spin_unlock(&kbd_event_lock);

L
Linus Torvalds 已提交
1323 1324 1325 1326 1327
	tasklet_schedule(&keyboard_tasklet);
	do_poke_blanked_console = 1;
	schedule_console_callback();
}

1328 1329 1330 1331 1332 1333 1334
static bool kbd_match(struct input_handler *handler, struct input_dev *dev)
{
	int i;

	if (test_bit(EV_SND, dev->evbit))
		return true;

1335
	if (test_bit(EV_KEY, dev->evbit)) {
1336 1337 1338
		for (i = KEY_RESERVED; i < BTN_MISC; i++)
			if (test_bit(i, dev->keybit))
				return true;
1339 1340 1341 1342
		for (i = KEY_BRL_DOT1; i <= KEY_BRL_DOT10; i++)
			if (test_bit(i, dev->keybit))
				return true;
	}
1343 1344 1345 1346

	return false;
}

L
Linus Torvalds 已提交
1347 1348 1349 1350 1351 1352
/*
 * When a keyboard (or other input device) is found, the kbd_connect
 * function is called. The function then looks at the device, and if it
 * likes it, it can open it and get events from it. In this (kbd_connect)
 * function, we should decide which VT to bind that keyboard to initially.
 */
1353 1354
static int kbd_connect(struct input_handler *handler, struct input_dev *dev,
			const struct input_device_id *id)
L
Linus Torvalds 已提交
1355 1356
{
	struct input_handle *handle;
1357
	int error;
L
Linus Torvalds 已提交
1358

1359 1360
	handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
	if (!handle)
1361
		return -ENOMEM;
L
Linus Torvalds 已提交
1362 1363 1364

	handle->dev = dev;
	handle->handler = handler;
1365
	handle->name = "kbd";
L
Linus Torvalds 已提交
1366

1367 1368 1369
	error = input_register_handle(handle);
	if (error)
		goto err_free_handle;
L
Linus Torvalds 已提交
1370

1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
	error = input_open_device(handle);
	if (error)
		goto err_unregister_handle;

	return 0;

 err_unregister_handle:
	input_unregister_handle(handle);
 err_free_handle:
	kfree(handle);
	return error;
L
Linus Torvalds 已提交
1382 1383 1384 1385 1386
}

static void kbd_disconnect(struct input_handle *handle)
{
	input_close_device(handle);
1387
	input_unregister_handle(handle);
L
Linus Torvalds 已提交
1388 1389 1390
	kfree(handle);
}

1391 1392 1393 1394 1395 1396 1397
/*
 * Start keyboard handler on the new keyboard by refreshing LED state to
 * match the rest of the system.
 */
static void kbd_start(struct input_handle *handle)
{
	tasklet_disable(&keyboard_tasklet);
1398 1399 1400 1401

	if (ledstate != 0xff)
		kbd_update_leds_helper(handle, &ledstate);

1402 1403 1404
	tasklet_enable(&keyboard_tasklet);
}

D
Dmitry Torokhov 已提交
1405
static const struct input_device_id kbd_ids[] = {
L
Linus Torvalds 已提交
1406 1407
	{
                .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
1408
                .evbit = { BIT_MASK(EV_KEY) },
L
Linus Torvalds 已提交
1409
        },
1410

L
Linus Torvalds 已提交
1411 1412
	{
                .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
1413
                .evbit = { BIT_MASK(EV_SND) },
1414
        },
L
Linus Torvalds 已提交
1415 1416 1417 1418 1419 1420 1421 1422

	{ },    /* Terminating entry */
};

MODULE_DEVICE_TABLE(input, kbd_ids);

static struct input_handler kbd_handler = {
	.event		= kbd_event,
1423
	.match		= kbd_match,
L
Linus Torvalds 已提交
1424 1425
	.connect	= kbd_connect,
	.disconnect	= kbd_disconnect,
1426
	.start		= kbd_start,
L
Linus Torvalds 已提交
1427 1428 1429 1430 1431 1432 1433
	.name		= "kbd",
	.id_table	= kbd_ids,
};

int __init kbd_init(void)
{
	int i;
1434
	int error;
L
Linus Torvalds 已提交
1435

1436 1437 1438 1439 1440 1441 1442
        for (i = 0; i < MAX_NR_CONSOLES; i++) {
		kbd_table[i].ledflagstate = KBD_DEFLEDS;
		kbd_table[i].default_ledflagstate = KBD_DEFLEDS;
		kbd_table[i].ledmode = LED_SHOW_FLAGS;
		kbd_table[i].lockstate = KBD_DEFLOCK;
		kbd_table[i].slockstate = 0;
		kbd_table[i].modeflags = KBD_DEFMODE;
B
Bill Nottingham 已提交
1443
		kbd_table[i].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
1444
	}
L
Linus Torvalds 已提交
1445

1446 1447 1448
	error = input_register_handler(&kbd_handler);
	if (error)
		return error;
L
Linus Torvalds 已提交
1449 1450 1451 1452 1453 1454

	tasklet_enable(&keyboard_tasklet);
	tasklet_schedule(&keyboard_tasklet);

	return 0;
}
A
Alan Cox 已提交
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616

/* Ioctl support code */

/**
 *	vt_do_diacrit		-	diacritical table updates
 *	@cmd: ioctl request
 *	@up: pointer to user data for ioctl
 *	@perm: permissions check computed by caller
 *
 *	Update the diacritical tables atomically and safely. Lock them
 *	against simultaneous keypresses
 */
int vt_do_diacrit(unsigned int cmd, void __user *up, int perm)
{
	struct kbdiacrs __user *a = up;
	unsigned long flags;
	int asize;
	int ret = 0;

	switch (cmd) {
	case KDGKBDIACR:
	{
		struct kbdiacr *diacr;
		int i;

		diacr = kmalloc(MAX_DIACR * sizeof(struct kbdiacr),
								GFP_KERNEL);
		if (diacr == NULL)
			return -ENOMEM;

		/* Lock the diacriticals table, make a copy and then
		   copy it after we unlock */
		spin_lock_irqsave(&kbd_event_lock, flags);

		asize = accent_table_size;
		for (i = 0; i < asize; i++) {
			diacr[i].diacr = conv_uni_to_8bit(
						accent_table[i].diacr);
			diacr[i].base = conv_uni_to_8bit(
						accent_table[i].base);
			diacr[i].result = conv_uni_to_8bit(
						accent_table[i].result);
		}
		spin_unlock_irqrestore(&kbd_event_lock, flags);

		if (put_user(asize, &a->kb_cnt))
			ret = -EFAULT;
		else  if (copy_to_user(a->kbdiacr, diacr,
				asize * sizeof(struct kbdiacr)))
			ret = -EFAULT;
		kfree(diacr);
		return ret;
	}
	case KDGKBDIACRUC:
	{
		struct kbdiacrsuc __user *a = up;
		void *buf;

		buf = kmalloc(MAX_DIACR * sizeof(struct kbdiacruc),
								GFP_KERNEL);
		if (buf == NULL)
			return -ENOMEM;

		/* Lock the diacriticals table, make a copy and then
		   copy it after we unlock */
		spin_lock_irqsave(&kbd_event_lock, flags);

		asize = accent_table_size;
		memcpy(buf, accent_table, asize * sizeof(struct kbdiacruc));

		spin_unlock_irqrestore(&kbd_event_lock, flags);

		if (put_user(asize, &a->kb_cnt))
			ret = -EFAULT;
		else if (copy_to_user(a->kbdiacruc, buf,
				asize*sizeof(struct kbdiacruc)))
			ret = -EFAULT;
		kfree(buf);
		return ret;
	}

	case KDSKBDIACR:
	{
		struct kbdiacrs __user *a = up;
		struct kbdiacr *diacr = NULL;
		unsigned int ct;
		int i;

		if (!perm)
			return -EPERM;
		if (get_user(ct, &a->kb_cnt))
			return -EFAULT;
		if (ct >= MAX_DIACR)
			return -EINVAL;

		if (ct) {
			diacr = kmalloc(sizeof(struct kbdiacr) * ct,
								GFP_KERNEL);
			if (diacr == NULL)
				return -ENOMEM;

			if (copy_from_user(diacr, a->kbdiacr,
					sizeof(struct kbdiacr) * ct)) {
				kfree(diacr);
				return -EFAULT;
			}
		}

		spin_lock_irqsave(&kbd_event_lock, flags);
		accent_table_size = ct;
		for (i = 0; i < ct; i++) {
			accent_table[i].diacr =
					conv_8bit_to_uni(diacr[i].diacr);
			accent_table[i].base =
					conv_8bit_to_uni(diacr[i].base);
			accent_table[i].result =
					conv_8bit_to_uni(diacr[i].result);
		}
		spin_unlock_irqrestore(&kbd_event_lock, flags);
		kfree(diacr);
		return 0;
	}

	case KDSKBDIACRUC:
	{
		struct kbdiacrsuc __user *a = up;
		unsigned int ct;
		void *buf = NULL;

		if (!perm)
			return -EPERM;

		if (get_user(ct, &a->kb_cnt))
			return -EFAULT;

		if (ct >= MAX_DIACR)
			return -EINVAL;

		if (ct) {
			buf = kmalloc(ct * sizeof(struct kbdiacruc),
								GFP_KERNEL);
			if (buf == NULL)
				return -ENOMEM;

			if (copy_from_user(buf, a->kbdiacruc,
					ct * sizeof(struct kbdiacruc))) {
				kfree(buf);
				return -EFAULT;
			}
		} 
		spin_lock_irqsave(&kbd_event_lock, flags);
		if (ct)
			memcpy(accent_table, buf,
					ct * sizeof(struct kbdiacruc));
		accent_table_size = ct;
		spin_unlock_irqrestore(&kbd_event_lock, flags);
		kfree(buf);
		return 0;
	}
	}
	return ret;
}