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#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#include <linux/leds.h>
37
38#include <linux/kbd_kern.h>
39#include <linux/kbd_diacr.h>
40#include <linux/vt_kern.h>
41#include <linux/input.h>
42#include <linux/reboot.h>
43#include <linux/notifier.h>
44#include <linux/jiffies.h>
45#include <linux/uaccess.h>
46
47#include <asm/irq_regs.h>
48
49extern void ctrl_alt_del(void);
50
51
52
53
54
55#define KBD_DEFMODE ((1 << VC_REPEAT) | (1 << VC_META))
56
57#if defined(CONFIG_X86) || defined(CONFIG_PARISC)
58#include <asm/kbdleds.h>
59#else
60static inline int kbd_defleds(void)
61{
62 return 0;
63}
64#endif
65
66#define KBD_DEFLOCK 0
67
68
69
70
71
72#define K_HANDLERS\
73 k_self, k_fn, k_spec, k_pad,\
74 k_dead, k_cons, k_cur, k_shift,\
75 k_meta, k_ascii, k_lock, k_lowercase,\
76 k_slock, k_dead2, k_brl, k_ignore
77
78typedef void (k_handler_fn)(struct vc_data *vc, unsigned char value,
79 char up_flag);
80static k_handler_fn K_HANDLERS;
81static k_handler_fn *k_handler[16] = { K_HANDLERS };
82
83#define FN_HANDLERS\
84 fn_null, fn_enter, fn_show_ptregs, fn_show_mem,\
85 fn_show_state, fn_send_intr, fn_lastcons, fn_caps_toggle,\
86 fn_num, fn_hold, fn_scroll_forw, fn_scroll_back,\
87 fn_boot_it, fn_caps_on, fn_compose, fn_SAK,\
88 fn_dec_console, fn_inc_console, fn_spawn_con, fn_bare_num
89
90typedef void (fn_handler_fn)(struct vc_data *vc);
91static fn_handler_fn FN_HANDLERS;
92static fn_handler_fn *fn_handler[] = { FN_HANDLERS };
93
94
95
96
97
98struct vt_spawn_console vt_spawn_con = {
99 .lock = __SPIN_LOCK_UNLOCKED(vt_spawn_con.lock),
100 .pid = NULL,
101 .sig = 0,
102};
103
104
105
106
107
108
109static struct kbd_struct kbd_table[MAX_NR_CONSOLES];
110static struct kbd_struct *kbd = kbd_table;
111
112
113static const int max_vals[] = {
114 255, ARRAY_SIZE(func_table) - 1, ARRAY_SIZE(fn_handler) - 1, NR_PAD - 1,
115 NR_DEAD - 1, 255, 3, NR_SHIFT - 1, 255, NR_ASCII - 1, NR_LOCK - 1,
116 255, NR_LOCK - 1, 255, NR_BRL - 1
117};
118
119static const int NR_TYPES = ARRAY_SIZE(max_vals);
120
121static struct input_handler kbd_handler;
122static DEFINE_SPINLOCK(kbd_event_lock);
123static DEFINE_SPINLOCK(led_lock);
124static unsigned long key_down[BITS_TO_LONGS(KEY_CNT)];
125static unsigned char shift_down[NR_SHIFT];
126static bool dead_key_next;
127static int npadch = -1;
128static unsigned int diacr;
129static char rep;
130
131static int shift_state = 0;
132
133static unsigned int ledstate = -1U;
134static unsigned char ledioctl;
135
136static struct ledptr {
137 unsigned int *addr;
138 unsigned int mask;
139 unsigned char valid:1;
140} ledptrs[3];
141
142
143
144
145static ATOMIC_NOTIFIER_HEAD(keyboard_notifier_list);
146
147int register_keyboard_notifier(struct notifier_block *nb)
148{
149 return atomic_notifier_chain_register(&keyboard_notifier_list, nb);
150}
151EXPORT_SYMBOL_GPL(register_keyboard_notifier);
152
153int unregister_keyboard_notifier(struct notifier_block *nb)
154{
155 return atomic_notifier_chain_unregister(&keyboard_notifier_list, nb);
156}
157EXPORT_SYMBOL_GPL(unregister_keyboard_notifier);
158
159
160
161
162
163
164
165
166
167
168
169struct getset_keycode_data {
170 struct input_keymap_entry ke;
171 int error;
172};
173
174static int getkeycode_helper(struct input_handle *handle, void *data)
175{
176 struct getset_keycode_data *d = data;
177
178 d->error = input_get_keycode(handle->dev, &d->ke);
179
180 return d->error == 0;
181}
182
183static int getkeycode(unsigned int scancode)
184{
185 struct getset_keycode_data d = {
186 .ke = {
187 .flags = 0,
188 .len = sizeof(scancode),
189 .keycode = 0,
190 },
191 .error = -ENODEV,
192 };
193
194 memcpy(d.ke.scancode, &scancode, sizeof(scancode));
195
196 input_handler_for_each_handle(&kbd_handler, &d, getkeycode_helper);
197
198 return d.error ?: d.ke.keycode;
199}
200
201static int setkeycode_helper(struct input_handle *handle, void *data)
202{
203 struct getset_keycode_data *d = data;
204
205 d->error = input_set_keycode(handle->dev, &d->ke);
206
207 return d->error == 0;
208}
209
210static int setkeycode(unsigned int scancode, unsigned int keycode)
211{
212 struct getset_keycode_data d = {
213 .ke = {
214 .flags = 0,
215 .len = sizeof(scancode),
216 .keycode = keycode,
217 },
218 .error = -ENODEV,
219 };
220
221 memcpy(d.ke.scancode, &scancode, sizeof(scancode));
222
223 input_handler_for_each_handle(&kbd_handler, &d, setkeycode_helper);
224
225 return d.error;
226}
227
228
229
230
231
232
233static int kd_sound_helper(struct input_handle *handle, void *data)
234{
235 unsigned int *hz = data;
236 struct input_dev *dev = handle->dev;
237
238 if (test_bit(EV_SND, dev->evbit)) {
239 if (test_bit(SND_TONE, dev->sndbit)) {
240 input_inject_event(handle, EV_SND, SND_TONE, *hz);
241 if (*hz)
242 return 0;
243 }
244 if (test_bit(SND_BELL, dev->sndbit))
245 input_inject_event(handle, EV_SND, SND_BELL, *hz ? 1 : 0);
246 }
247
248 return 0;
249}
250
251static void kd_nosound(unsigned long ignored)
252{
253 static unsigned int zero;
254
255 input_handler_for_each_handle(&kbd_handler, &zero, kd_sound_helper);
256}
257
258static DEFINE_TIMER(kd_mksound_timer, kd_nosound, 0, 0);
259
260void kd_mksound(unsigned int hz, unsigned int ticks)
261{
262 del_timer_sync(&kd_mksound_timer);
263
264 input_handler_for_each_handle(&kbd_handler, &hz, kd_sound_helper);
265
266 if (hz && ticks)
267 mod_timer(&kd_mksound_timer, jiffies + ticks);
268}
269EXPORT_SYMBOL(kd_mksound);
270
271
272
273
274
275static int kbd_rate_helper(struct input_handle *handle, void *data)
276{
277 struct input_dev *dev = handle->dev;
278 struct kbd_repeat *rep = data;
279
280 if (test_bit(EV_REP, dev->evbit)) {
281
282 if (rep[0].delay > 0)
283 input_inject_event(handle,
284 EV_REP, REP_DELAY, rep[0].delay);
285 if (rep[0].period > 0)
286 input_inject_event(handle,
287 EV_REP, REP_PERIOD, rep[0].period);
288
289 rep[1].delay = dev->rep[REP_DELAY];
290 rep[1].period = dev->rep[REP_PERIOD];
291 }
292
293 return 0;
294}
295
296int kbd_rate(struct kbd_repeat *rep)
297{
298 struct kbd_repeat data[2] = { *rep };
299
300 input_handler_for_each_handle(&kbd_handler, data, kbd_rate_helper);
301 *rep = data[1];
302
303 return 0;
304}
305
306
307
308
309static void put_queue(struct vc_data *vc, int ch)
310{
311 tty_insert_flip_char(&vc->port, ch, 0);
312 tty_schedule_flip(&vc->port);
313}
314
315static void puts_queue(struct vc_data *vc, char *cp)
316{
317 while (*cp) {
318 tty_insert_flip_char(&vc->port, *cp, 0);
319 cp++;
320 }
321 tty_schedule_flip(&vc->port);
322}
323
324static void applkey(struct vc_data *vc, int key, char mode)
325{
326 static char buf[] = { 0x1b, 'O', 0x00, 0x00 };
327
328 buf[1] = (mode ? 'O' : '[');
329 buf[2] = key;
330 puts_queue(vc, buf);
331}
332
333
334
335
336
337
338
339static void to_utf8(struct vc_data *vc, uint c)
340{
341 if (c < 0x80)
342
343 put_queue(vc, c);
344 else if (c < 0x800) {
345
346 put_queue(vc, 0xc0 | (c >> 6));
347 put_queue(vc, 0x80 | (c & 0x3f));
348 } else if (c < 0x10000) {
349 if (c >= 0xD800 && c < 0xE000)
350 return;
351 if (c == 0xFFFF)
352 return;
353
354 put_queue(vc, 0xe0 | (c >> 12));
355 put_queue(vc, 0x80 | ((c >> 6) & 0x3f));
356 put_queue(vc, 0x80 | (c & 0x3f));
357 } else if (c < 0x110000) {
358
359 put_queue(vc, 0xf0 | (c >> 18));
360 put_queue(vc, 0x80 | ((c >> 12) & 0x3f));
361 put_queue(vc, 0x80 | ((c >> 6) & 0x3f));
362 put_queue(vc, 0x80 | (c & 0x3f));
363 }
364}
365
366
367
368
369
370
371
372
373static void do_compute_shiftstate(void)
374{
375 unsigned int i, j, k, sym, val;
376
377 shift_state = 0;
378 memset(shift_down, 0, sizeof(shift_down));
379
380 for (i = 0; i < ARRAY_SIZE(key_down); i++) {
381
382 if (!key_down[i])
383 continue;
384
385 k = i * BITS_PER_LONG;
386
387 for (j = 0; j < BITS_PER_LONG; j++, k++) {
388
389 if (!test_bit(k, key_down))
390 continue;
391
392 sym = U(key_maps[0][k]);
393 if (KTYP(sym) != KT_SHIFT && KTYP(sym) != KT_SLOCK)
394 continue;
395
396 val = KVAL(sym);
397 if (val == KVAL(K_CAPSSHIFT))
398 val = KVAL(K_SHIFT);
399
400 shift_down[val]++;
401 shift_state |= (1 << val);
402 }
403 }
404}
405
406
407void compute_shiftstate(void)
408{
409 unsigned long flags;
410 spin_lock_irqsave(&kbd_event_lock, flags);
411 do_compute_shiftstate();
412 spin_unlock_irqrestore(&kbd_event_lock, flags);
413}
414
415
416
417
418
419
420
421
422static unsigned int handle_diacr(struct vc_data *vc, unsigned int ch)
423{
424 unsigned int d = diacr;
425 unsigned int i;
426
427 diacr = 0;
428
429 if ((d & ~0xff) == BRL_UC_ROW) {
430 if ((ch & ~0xff) == BRL_UC_ROW)
431 return d | ch;
432 } else {
433 for (i = 0; i < accent_table_size; i++)
434 if (accent_table[i].diacr == d && accent_table[i].base == ch)
435 return accent_table[i].result;
436 }
437
438 if (ch == ' ' || ch == (BRL_UC_ROW|0) || ch == d)
439 return d;
440
441 if (kbd->kbdmode == VC_UNICODE)
442 to_utf8(vc, d);
443 else {
444 int c = conv_uni_to_8bit(d);
445 if (c != -1)
446 put_queue(vc, c);
447 }
448
449 return ch;
450}
451
452
453
454
455static void fn_enter(struct vc_data *vc)
456{
457 if (diacr) {
458 if (kbd->kbdmode == VC_UNICODE)
459 to_utf8(vc, diacr);
460 else {
461 int c = conv_uni_to_8bit(diacr);
462 if (c != -1)
463 put_queue(vc, c);
464 }
465 diacr = 0;
466 }
467
468 put_queue(vc, 13);
469 if (vc_kbd_mode(kbd, VC_CRLF))
470 put_queue(vc, 10);
471}
472
473static void fn_caps_toggle(struct vc_data *vc)
474{
475 if (rep)
476 return;
477
478 chg_vc_kbd_led(kbd, VC_CAPSLOCK);
479}
480
481static void fn_caps_on(struct vc_data *vc)
482{
483 if (rep)
484 return;
485
486 set_vc_kbd_led(kbd, VC_CAPSLOCK);
487}
488
489static void fn_show_ptregs(struct vc_data *vc)
490{
491 struct pt_regs *regs = get_irq_regs();
492
493 if (regs)
494 show_regs(regs);
495}
496
497static void fn_hold(struct vc_data *vc)
498{
499 struct tty_struct *tty = vc->port.tty;
500
501 if (rep || !tty)
502 return;
503
504
505
506
507
508
509 if (tty->stopped)
510 start_tty(tty);
511 else
512 stop_tty(tty);
513}
514
515static void fn_num(struct vc_data *vc)
516{
517 if (vc_kbd_mode(kbd, VC_APPLIC))
518 applkey(vc, 'P', 1);
519 else
520 fn_bare_num(vc);
521}
522
523
524
525
526
527
528
529static void fn_bare_num(struct vc_data *vc)
530{
531 if (!rep)
532 chg_vc_kbd_led(kbd, VC_NUMLOCK);
533}
534
535static void fn_lastcons(struct vc_data *vc)
536{
537
538 set_console(last_console);
539}
540
541static void fn_dec_console(struct vc_data *vc)
542{
543 int i, cur = fg_console;
544
545
546 if (want_console != -1)
547 cur = want_console;
548
549 for (i = cur - 1; i != cur; i--) {
550 if (i == -1)
551 i = MAX_NR_CONSOLES - 1;
552 if (vc_cons_allocated(i))
553 break;
554 }
555 set_console(i);
556}
557
558static void fn_inc_console(struct vc_data *vc)
559{
560 int i, cur = fg_console;
561
562
563 if (want_console != -1)
564 cur = want_console;
565
566 for (i = cur+1; i != cur; i++) {
567 if (i == MAX_NR_CONSOLES)
568 i = 0;
569 if (vc_cons_allocated(i))
570 break;
571 }
572 set_console(i);
573}
574
575static void fn_send_intr(struct vc_data *vc)
576{
577 tty_insert_flip_char(&vc->port, 0, TTY_BREAK);
578 tty_schedule_flip(&vc->port);
579}
580
581static void fn_scroll_forw(struct vc_data *vc)
582{
583 scrollfront(vc, 0);
584}
585
586static void fn_scroll_back(struct vc_data *vc)
587{
588 scrollback(vc, 0);
589}
590
591static void fn_show_mem(struct vc_data *vc)
592{
593 show_mem(0);
594}
595
596static void fn_show_state(struct vc_data *vc)
597{
598 show_state();
599}
600
601static void fn_boot_it(struct vc_data *vc)
602{
603 ctrl_alt_del();
604}
605
606static void fn_compose(struct vc_data *vc)
607{
608 dead_key_next = true;
609}
610
611static void fn_spawn_con(struct vc_data *vc)
612{
613 spin_lock(&vt_spawn_con.lock);
614 if (vt_spawn_con.pid)
615 if (kill_pid(vt_spawn_con.pid, vt_spawn_con.sig, 1)) {
616 put_pid(vt_spawn_con.pid);
617 vt_spawn_con.pid = NULL;
618 }
619 spin_unlock(&vt_spawn_con.lock);
620}
621
622static void fn_SAK(struct vc_data *vc)
623{
624 struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work;
625 schedule_work(SAK_work);
626}
627
628static void fn_null(struct vc_data *vc)
629{
630 do_compute_shiftstate();
631}
632
633
634
635
636static void k_ignore(struct vc_data *vc, unsigned char value, char up_flag)
637{
638}
639
640static void k_spec(struct vc_data *vc, unsigned char value, char up_flag)
641{
642 if (up_flag)
643 return;
644 if (value >= ARRAY_SIZE(fn_handler))
645 return;
646 if ((kbd->kbdmode == VC_RAW ||
647 kbd->kbdmode == VC_MEDIUMRAW ||
648 kbd->kbdmode == VC_OFF) &&
649 value != KVAL(K_SAK))
650 return;
651 fn_handler[value](vc);
652}
653
654static void k_lowercase(struct vc_data *vc, unsigned char value, char up_flag)
655{
656 pr_err("k_lowercase was called - impossible\n");
657}
658
659static void k_unicode(struct vc_data *vc, unsigned int value, char up_flag)
660{
661 if (up_flag)
662 return;
663
664 if (diacr)
665 value = handle_diacr(vc, value);
666
667 if (dead_key_next) {
668 dead_key_next = false;
669 diacr = value;
670 return;
671 }
672 if (kbd->kbdmode == VC_UNICODE)
673 to_utf8(vc, value);
674 else {
675 int c = conv_uni_to_8bit(value);
676 if (c != -1)
677 put_queue(vc, c);
678 }
679}
680
681
682
683
684
685
686static void k_deadunicode(struct vc_data *vc, unsigned int value, char up_flag)
687{
688 if (up_flag)
689 return;
690
691 diacr = (diacr ? handle_diacr(vc, value) : value);
692}
693
694static void k_self(struct vc_data *vc, unsigned char value, char up_flag)
695{
696 k_unicode(vc, conv_8bit_to_uni(value), up_flag);
697}
698
699static void k_dead2(struct vc_data *vc, unsigned char value, char up_flag)
700{
701 k_deadunicode(vc, value, up_flag);
702}
703
704
705
706
707static void k_dead(struct vc_data *vc, unsigned char value, char up_flag)
708{
709 static const unsigned char ret_diacr[NR_DEAD] = {'`', '\'', '^', '~', '"', ',' };
710
711 k_deadunicode(vc, ret_diacr[value], up_flag);
712}
713
714static void k_cons(struct vc_data *vc, unsigned char value, char up_flag)
715{
716 if (up_flag)
717 return;
718
719 set_console(value);
720}
721
722static void k_fn(struct vc_data *vc, unsigned char value, char up_flag)
723{
724 if (up_flag)
725 return;
726
727 if ((unsigned)value < ARRAY_SIZE(func_table)) {
728 if (func_table[value])
729 puts_queue(vc, func_table[value]);
730 } else
731 pr_err("k_fn called with value=%d\n", value);
732}
733
734static void k_cur(struct vc_data *vc, unsigned char value, char up_flag)
735{
736 static const char cur_chars[] = "BDCA";
737
738 if (up_flag)
739 return;
740
741 applkey(vc, cur_chars[value], vc_kbd_mode(kbd, VC_CKMODE));
742}
743
744static void k_pad(struct vc_data *vc, unsigned char value, char up_flag)
745{
746 static const char pad_chars[] = "0123456789+-*/\015,.?()#";
747 static const char app_map[] = "pqrstuvwxylSRQMnnmPQS";
748
749 if (up_flag)
750 return;
751
752
753 if (vc_kbd_mode(kbd, VC_APPLIC) && !shift_down[KG_SHIFT]) {
754 applkey(vc, app_map[value], 1);
755 return;
756 }
757
758 if (!vc_kbd_led(kbd, VC_NUMLOCK)) {
759
760 switch (value) {
761 case KVAL(K_PCOMMA):
762 case KVAL(K_PDOT):
763 k_fn(vc, KVAL(K_REMOVE), 0);
764 return;
765 case KVAL(K_P0):
766 k_fn(vc, KVAL(K_INSERT), 0);
767 return;
768 case KVAL(K_P1):
769 k_fn(vc, KVAL(K_SELECT), 0);
770 return;
771 case KVAL(K_P2):
772 k_cur(vc, KVAL(K_DOWN), 0);
773 return;
774 case KVAL(K_P3):
775 k_fn(vc, KVAL(K_PGDN), 0);
776 return;
777 case KVAL(K_P4):
778 k_cur(vc, KVAL(K_LEFT), 0);
779 return;
780 case KVAL(K_P6):
781 k_cur(vc, KVAL(K_RIGHT), 0);
782 return;
783 case KVAL(K_P7):
784 k_fn(vc, KVAL(K_FIND), 0);
785 return;
786 case KVAL(K_P8):
787 k_cur(vc, KVAL(K_UP), 0);
788 return;
789 case KVAL(K_P9):
790 k_fn(vc, KVAL(K_PGUP), 0);
791 return;
792 case KVAL(K_P5):
793 applkey(vc, 'G', vc_kbd_mode(kbd, VC_APPLIC));
794 return;
795 }
796 }
797
798 put_queue(vc, pad_chars[value]);
799 if (value == KVAL(K_PENTER) && vc_kbd_mode(kbd, VC_CRLF))
800 put_queue(vc, 10);
801}
802
803static void k_shift(struct vc_data *vc, unsigned char value, char up_flag)
804{
805 int old_state = shift_state;
806
807 if (rep)
808 return;
809
810
811
812
813 if (value == KVAL(K_CAPSSHIFT)) {
814 value = KVAL(K_SHIFT);
815 if (!up_flag)
816 clr_vc_kbd_led(kbd, VC_CAPSLOCK);
817 }
818
819 if (up_flag) {
820
821
822
823
824 if (shift_down[value])
825 shift_down[value]--;
826 } else
827 shift_down[value]++;
828
829 if (shift_down[value])
830 shift_state |= (1 << value);
831 else
832 shift_state &= ~(1 << value);
833
834
835 if (up_flag && shift_state != old_state && npadch != -1) {
836 if (kbd->kbdmode == VC_UNICODE)
837 to_utf8(vc, npadch);
838 else
839 put_queue(vc, npadch & 0xff);
840 npadch = -1;
841 }
842}
843
844static void k_meta(struct vc_data *vc, unsigned char value, char up_flag)
845{
846 if (up_flag)
847 return;
848
849 if (vc_kbd_mode(kbd, VC_META)) {
850 put_queue(vc, '\033');
851 put_queue(vc, value);
852 } else
853 put_queue(vc, value | 0x80);
854}
855
856static void k_ascii(struct vc_data *vc, unsigned char value, char up_flag)
857{
858 int base;
859
860 if (up_flag)
861 return;
862
863 if (value < 10) {
864
865 base = 10;
866 } else {
867
868 value -= 10;
869 base = 16;
870 }
871
872 if (npadch == -1)
873 npadch = value;
874 else
875 npadch = npadch * base + value;
876}
877
878static void k_lock(struct vc_data *vc, unsigned char value, char up_flag)
879{
880 if (up_flag || rep)
881 return;
882
883 chg_vc_kbd_lock(kbd, value);
884}
885
886static void k_slock(struct vc_data *vc, unsigned char value, char up_flag)
887{
888 k_shift(vc, value, up_flag);
889 if (up_flag || rep)
890 return;
891
892 chg_vc_kbd_slock(kbd, value);
893
894 if (!key_maps[kbd->lockstate ^ kbd->slockstate]) {
895 kbd->slockstate = 0;
896 chg_vc_kbd_slock(kbd, value);
897 }
898}
899
900
901static unsigned brl_timeout = 300;
902MODULE_PARM_DESC(brl_timeout, "Braille keys release delay in ms (0 for commit on first key release)");
903module_param(brl_timeout, uint, 0644);
904
905static unsigned brl_nbchords = 1;
906MODULE_PARM_DESC(brl_nbchords, "Number of chords that produce a braille pattern (0 for dead chords)");
907module_param(brl_nbchords, uint, 0644);
908
909static void k_brlcommit(struct vc_data *vc, unsigned int pattern, char up_flag)
910{
911 static unsigned long chords;
912 static unsigned committed;
913
914 if (!brl_nbchords)
915 k_deadunicode(vc, BRL_UC_ROW | pattern, up_flag);
916 else {
917 committed |= pattern;
918 chords++;
919 if (chords == brl_nbchords) {
920 k_unicode(vc, BRL_UC_ROW | committed, up_flag);
921 chords = 0;
922 committed = 0;
923 }
924 }
925}
926
927static void k_brl(struct vc_data *vc, unsigned char value, char up_flag)
928{
929 static unsigned pressed, committing;
930 static unsigned long releasestart;
931
932 if (kbd->kbdmode != VC_UNICODE) {
933 if (!up_flag)
934 pr_warning("keyboard mode must be unicode for braille patterns\n");
935 return;
936 }
937
938 if (!value) {
939 k_unicode(vc, BRL_UC_ROW, up_flag);
940 return;
941 }
942
943 if (value > 8)
944 return;
945
946 if (!up_flag) {
947 pressed |= 1 << (value - 1);
948 if (!brl_timeout)
949 committing = pressed;
950 } else if (brl_timeout) {
951 if (!committing ||
952 time_after(jiffies,
953 releasestart + msecs_to_jiffies(brl_timeout))) {
954 committing = pressed;
955 releasestart = jiffies;
956 }
957 pressed &= ~(1 << (value - 1));
958 if (!pressed && committing) {
959 k_brlcommit(vc, committing, 0);
960 committing = 0;
961 }
962 } else {
963 if (committing) {
964 k_brlcommit(vc, committing, 0);
965 committing = 0;
966 }
967 pressed &= ~(1 << (value - 1));
968 }
969}
970
971#if IS_ENABLED(CONFIG_INPUT_LEDS) && IS_ENABLED(CONFIG_LEDS_TRIGGERS)
972
973struct kbd_led_trigger {
974 struct led_trigger trigger;
975 unsigned int mask;
976};
977
978static void kbd_led_trigger_activate(struct led_classdev *cdev)
979{
980 struct kbd_led_trigger *trigger =
981 container_of(cdev->trigger, struct kbd_led_trigger, trigger);
982
983 tasklet_disable(&keyboard_tasklet);
984 if (ledstate != -1U)
985 led_trigger_event(&trigger->trigger,
986 ledstate & trigger->mask ?
987 LED_FULL : LED_OFF);
988 tasklet_enable(&keyboard_tasklet);
989}
990
991#define KBD_LED_TRIGGER(_led_bit, _name) { \
992 .trigger = { \
993 .name = _name, \
994 .activate = kbd_led_trigger_activate, \
995 }, \
996 .mask = BIT(_led_bit), \
997 }
998
999#define KBD_LOCKSTATE_TRIGGER(_led_bit, _name) \
1000 KBD_LED_TRIGGER((_led_bit) + 8, _name)
1001
1002static struct kbd_led_trigger kbd_led_triggers[] = {
1003 KBD_LED_TRIGGER(VC_SCROLLOCK, "kbd-scrolllock"),
1004 KBD_LED_TRIGGER(VC_NUMLOCK, "kbd-numlock"),
1005 KBD_LED_TRIGGER(VC_CAPSLOCK, "kbd-capslock"),
1006 KBD_LED_TRIGGER(VC_KANALOCK, "kbd-kanalock"),
1007
1008 KBD_LOCKSTATE_TRIGGER(VC_SHIFTLOCK, "kbd-shiftlock"),
1009 KBD_LOCKSTATE_TRIGGER(VC_ALTGRLOCK, "kbd-altgrlock"),
1010 KBD_LOCKSTATE_TRIGGER(VC_CTRLLOCK, "kbd-ctrllock"),
1011 KBD_LOCKSTATE_TRIGGER(VC_ALTLOCK, "kbd-altlock"),
1012 KBD_LOCKSTATE_TRIGGER(VC_SHIFTLLOCK, "kbd-shiftllock"),
1013 KBD_LOCKSTATE_TRIGGER(VC_SHIFTRLOCK, "kbd-shiftrlock"),
1014 KBD_LOCKSTATE_TRIGGER(VC_CTRLLLOCK, "kbd-ctrlllock"),
1015 KBD_LOCKSTATE_TRIGGER(VC_CTRLRLOCK, "kbd-ctrlrlock"),
1016};
1017
1018static void kbd_propagate_led_state(unsigned int old_state,
1019 unsigned int new_state)
1020{
1021 struct kbd_led_trigger *trigger;
1022 unsigned int changed = old_state ^ new_state;
1023 int i;
1024
1025 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); i++) {
1026 trigger = &kbd_led_triggers[i];
1027
1028 if (changed & trigger->mask)
1029 led_trigger_event(&trigger->trigger,
1030 new_state & trigger->mask ?
1031 LED_FULL : LED_OFF);
1032 }
1033}
1034
1035static int kbd_update_leds_helper(struct input_handle *handle, void *data)
1036{
1037 unsigned int led_state = *(unsigned int *)data;
1038
1039 if (test_bit(EV_LED, handle->dev->evbit))
1040 kbd_propagate_led_state(~led_state, led_state);
1041
1042 return 0;
1043}
1044
1045static void kbd_init_leds(void)
1046{
1047 int error;
1048 int i;
1049
1050 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); i++) {
1051 error = led_trigger_register(&kbd_led_triggers[i].trigger);
1052 if (error)
1053 pr_err("error %d while registering trigger %s\n",
1054 error, kbd_led_triggers[i].trigger.name);
1055 }
1056}
1057
1058#else
1059
1060static int kbd_update_leds_helper(struct input_handle *handle, void *data)
1061{
1062 unsigned int leds = *(unsigned int *)data;
1063
1064 if (test_bit(EV_LED, handle->dev->evbit)) {
1065 input_inject_event(handle, EV_LED, LED_SCROLLL, !!(leds & 0x01));
1066 input_inject_event(handle, EV_LED, LED_NUML, !!(leds & 0x02));
1067 input_inject_event(handle, EV_LED, LED_CAPSL, !!(leds & 0x04));
1068 input_inject_event(handle, EV_SYN, SYN_REPORT, 0);
1069 }
1070
1071 return 0;
1072}
1073
1074static void kbd_propagate_led_state(unsigned int old_state,
1075 unsigned int new_state)
1076{
1077 input_handler_for_each_handle(&kbd_handler, &new_state,
1078 kbd_update_leds_helper);
1079}
1080
1081static void kbd_init_leds(void)
1082{
1083}
1084
1085#endif
1086
1087
1088
1089
1090
1091
1092static unsigned char getledstate(void)
1093{
1094 return ledstate & 0xff;
1095}
1096
1097void setledstate(struct kbd_struct *kbd, unsigned int led)
1098{
1099 unsigned long flags;
1100 spin_lock_irqsave(&led_lock, flags);
1101 if (!(led & ~7)) {
1102 ledioctl = led;
1103 kbd->ledmode = LED_SHOW_IOCTL;
1104 } else
1105 kbd->ledmode = LED_SHOW_FLAGS;
1106
1107 set_leds();
1108 spin_unlock_irqrestore(&led_lock, flags);
1109}
1110
1111static inline unsigned char getleds(void)
1112{
1113 struct kbd_struct *kbd = kbd_table + fg_console;
1114 unsigned char leds;
1115 int i;
1116
1117 if (kbd->ledmode == LED_SHOW_IOCTL)
1118 return ledioctl;
1119
1120 leds = kbd->ledflagstate;
1121
1122 if (kbd->ledmode == LED_SHOW_MEM) {
1123 for (i = 0; i < 3; i++)
1124 if (ledptrs[i].valid) {
1125 if (*ledptrs[i].addr & ledptrs[i].mask)
1126 leds |= (1 << i);
1127 else
1128 leds &= ~(1 << i);
1129 }
1130 }
1131 return leds;
1132}
1133
1134
1135
1136
1137
1138
1139
1140
1141int vt_get_leds(int console, int flag)
1142{
1143 struct kbd_struct * kbd = kbd_table + console;
1144 int ret;
1145 unsigned long flags;
1146
1147 spin_lock_irqsave(&led_lock, flags);
1148 ret = vc_kbd_led(kbd, flag);
1149 spin_unlock_irqrestore(&led_lock, flags);
1150
1151 return ret;
1152}
1153EXPORT_SYMBOL_GPL(vt_get_leds);
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163void vt_set_led_state(int console, int leds)
1164{
1165 struct kbd_struct * kbd = kbd_table + console;
1166 setledstate(kbd, leds);
1167}
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182void vt_kbd_con_start(int console)
1183{
1184 struct kbd_struct * kbd = kbd_table + console;
1185 unsigned long flags;
1186 spin_lock_irqsave(&led_lock, flags);
1187 clr_vc_kbd_led(kbd, VC_SCROLLOCK);
1188 set_leds();
1189 spin_unlock_irqrestore(&led_lock, flags);
1190}
1191
1192
1193
1194
1195
1196
1197
1198
1199void vt_kbd_con_stop(int console)
1200{
1201 struct kbd_struct * kbd = kbd_table + console;
1202 unsigned long flags;
1203 spin_lock_irqsave(&led_lock, flags);
1204 set_vc_kbd_led(kbd, VC_SCROLLOCK);
1205 set_leds();
1206 spin_unlock_irqrestore(&led_lock, flags);
1207}
1208
1209
1210
1211
1212
1213
1214
1215static void kbd_bh(unsigned long dummy)
1216{
1217 unsigned int leds;
1218 unsigned long flags;
1219
1220 spin_lock_irqsave(&led_lock, flags);
1221 leds = getleds();
1222 leds |= (unsigned int)kbd->lockstate << 8;
1223 spin_unlock_irqrestore(&led_lock, flags);
1224
1225 if (leds != ledstate) {
1226 kbd_propagate_led_state(ledstate, leds);
1227 ledstate = leds;
1228 }
1229}
1230
1231DECLARE_TASKLET_DISABLED(keyboard_tasklet, kbd_bh, 0);
1232
1233#if defined(CONFIG_X86) || defined(CONFIG_IA64) || defined(CONFIG_ALPHA) ||\
1234 defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_SPARC) ||\
1235 defined(CONFIG_PARISC) || defined(CONFIG_SUPERH) ||\
1236 (defined(CONFIG_ARM) && defined(CONFIG_KEYBOARD_ATKBD) && !defined(CONFIG_ARCH_RPC)) ||\
1237 defined(CONFIG_AVR32)
1238
1239#define HW_RAW(dev) (test_bit(EV_MSC, dev->evbit) && test_bit(MSC_RAW, dev->mscbit) &&\
1240 ((dev)->id.bustype == BUS_I8042) && ((dev)->id.vendor == 0x0001) && ((dev)->id.product == 0x0001))
1241
1242static const unsigned short x86_keycodes[256] =
1243 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
1244 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
1245 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
1246 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
1247 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
1248 80, 81, 82, 83, 84,118, 86, 87, 88,115,120,119,121,112,123, 92,
1249 284,285,309, 0,312, 91,327,328,329,331,333,335,336,337,338,339,
1250 367,288,302,304,350, 89,334,326,267,126,268,269,125,347,348,349,
1251 360,261,262,263,268,376,100,101,321,316,373,286,289,102,351,355,
1252 103,104,105,275,287,279,258,106,274,107,294,364,358,363,362,361,
1253 291,108,381,281,290,272,292,305,280, 99,112,257,306,359,113,114,
1254 264,117,271,374,379,265,266, 93, 94, 95, 85,259,375,260, 90,116,
1255 377,109,111,277,278,282,283,295,296,297,299,300,301,293,303,307,
1256 308,310,313,314,315,317,318,319,320,357,322,323,324,325,276,330,
1257 332,340,365,342,343,344,345,346,356,270,341,368,369,370,371,372 };
1258
1259#ifdef CONFIG_SPARC
1260static int sparc_l1_a_state;
1261extern void sun_do_break(void);
1262#endif
1263
1264static int emulate_raw(struct vc_data *vc, unsigned int keycode,
1265 unsigned char up_flag)
1266{
1267 int code;
1268
1269 switch (keycode) {
1270
1271 case KEY_PAUSE:
1272 put_queue(vc, 0xe1);
1273 put_queue(vc, 0x1d | up_flag);
1274 put_queue(vc, 0x45 | up_flag);
1275 break;
1276
1277 case KEY_HANGEUL:
1278 if (!up_flag)
1279 put_queue(vc, 0xf2);
1280 break;
1281
1282 case KEY_HANJA:
1283 if (!up_flag)
1284 put_queue(vc, 0xf1);
1285 break;
1286
1287 case KEY_SYSRQ:
1288
1289
1290
1291
1292
1293
1294 if (test_bit(KEY_LEFTALT, key_down) ||
1295 test_bit(KEY_RIGHTALT, key_down)) {
1296 put_queue(vc, 0x54 | up_flag);
1297 } else {
1298 put_queue(vc, 0xe0);
1299 put_queue(vc, 0x2a | up_flag);
1300 put_queue(vc, 0xe0);
1301 put_queue(vc, 0x37 | up_flag);
1302 }
1303 break;
1304
1305 default:
1306 if (keycode > 255)
1307 return -1;
1308
1309 code = x86_keycodes[keycode];
1310 if (!code)
1311 return -1;
1312
1313 if (code & 0x100)
1314 put_queue(vc, 0xe0);
1315 put_queue(vc, (code & 0x7f) | up_flag);
1316
1317 break;
1318 }
1319
1320 return 0;
1321}
1322
1323#else
1324
1325#define HW_RAW(dev) 0
1326
1327static int emulate_raw(struct vc_data *vc, unsigned int keycode, unsigned char up_flag)
1328{
1329 if (keycode > 127)
1330 return -1;
1331
1332 put_queue(vc, keycode | up_flag);
1333 return 0;
1334}
1335#endif
1336
1337static void kbd_rawcode(unsigned char data)
1338{
1339 struct vc_data *vc = vc_cons[fg_console].d;
1340
1341 kbd = kbd_table + vc->vc_num;
1342 if (kbd->kbdmode == VC_RAW)
1343 put_queue(vc, data);
1344}
1345
1346static void kbd_keycode(unsigned int keycode, int down, int hw_raw)
1347{
1348 struct vc_data *vc = vc_cons[fg_console].d;
1349 unsigned short keysym, *key_map;
1350 unsigned char type;
1351 bool raw_mode;
1352 struct tty_struct *tty;
1353 int shift_final;
1354 struct keyboard_notifier_param param = { .vc = vc, .value = keycode, .down = down };
1355 int rc;
1356
1357 tty = vc->port.tty;
1358
1359 if (tty && (!tty->driver_data)) {
1360
1361 tty->driver_data = vc;
1362 }
1363
1364 kbd = kbd_table + vc->vc_num;
1365
1366#ifdef CONFIG_SPARC
1367 if (keycode == KEY_STOP)
1368 sparc_l1_a_state = down;
1369#endif
1370
1371 rep = (down == 2);
1372
1373 raw_mode = (kbd->kbdmode == VC_RAW);
1374 if (raw_mode && !hw_raw)
1375 if (emulate_raw(vc, keycode, !down << 7))
1376 if (keycode < BTN_MISC && printk_ratelimit())
1377 pr_warning("can't emulate rawmode for keycode %d\n",
1378 keycode);
1379
1380#ifdef CONFIG_SPARC
1381 if (keycode == KEY_A && sparc_l1_a_state) {
1382 sparc_l1_a_state = false;
1383 sun_do_break();
1384 }
1385#endif
1386
1387 if (kbd->kbdmode == VC_MEDIUMRAW) {
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397 if (keycode < 128) {
1398 put_queue(vc, keycode | (!down << 7));
1399 } else {
1400 put_queue(vc, !down << 7);
1401 put_queue(vc, (keycode >> 7) | 0x80);
1402 put_queue(vc, keycode | 0x80);
1403 }
1404 raw_mode = true;
1405 }
1406
1407 if (down)
1408 set_bit(keycode, key_down);
1409 else
1410 clear_bit(keycode, key_down);
1411
1412 if (rep &&
1413 (!vc_kbd_mode(kbd, VC_REPEAT) ||
1414 (tty && !L_ECHO(tty) && tty_chars_in_buffer(tty)))) {
1415
1416
1417
1418
1419
1420 return;
1421 }
1422
1423 param.shift = shift_final = (shift_state | kbd->slockstate) ^ kbd->lockstate;
1424 param.ledstate = kbd->ledflagstate;
1425 key_map = key_maps[shift_final];
1426
1427 rc = atomic_notifier_call_chain(&keyboard_notifier_list,
1428 KBD_KEYCODE, ¶m);
1429 if (rc == NOTIFY_STOP || !key_map) {
1430 atomic_notifier_call_chain(&keyboard_notifier_list,
1431 KBD_UNBOUND_KEYCODE, ¶m);
1432 do_compute_shiftstate();
1433 kbd->slockstate = 0;
1434 return;
1435 }
1436
1437 if (keycode < NR_KEYS)
1438 keysym = key_map[keycode];
1439 else if (keycode >= KEY_BRL_DOT1 && keycode <= KEY_BRL_DOT8)
1440 keysym = U(K(KT_BRL, keycode - KEY_BRL_DOT1 + 1));
1441 else
1442 return;
1443
1444 type = KTYP(keysym);
1445
1446 if (type < 0xf0) {
1447 param.value = keysym;
1448 rc = atomic_notifier_call_chain(&keyboard_notifier_list,
1449 KBD_UNICODE, ¶m);
1450 if (rc != NOTIFY_STOP)
1451 if (down && !raw_mode)
1452 to_utf8(vc, keysym);
1453 return;
1454 }
1455
1456 type -= 0xf0;
1457
1458 if (type == KT_LETTER) {
1459 type = KT_LATIN;
1460 if (vc_kbd_led(kbd, VC_CAPSLOCK)) {
1461 key_map = key_maps[shift_final ^ (1 << KG_SHIFT)];
1462 if (key_map)
1463 keysym = key_map[keycode];
1464 }
1465 }
1466
1467 param.value = keysym;
1468 rc = atomic_notifier_call_chain(&keyboard_notifier_list,
1469 KBD_KEYSYM, ¶m);
1470 if (rc == NOTIFY_STOP)
1471 return;
1472
1473 if ((raw_mode || kbd->kbdmode == VC_OFF) && type != KT_SPEC && type != KT_SHIFT)
1474 return;
1475
1476 (*k_handler[type])(vc, keysym & 0xff, !down);
1477
1478 param.ledstate = kbd->ledflagstate;
1479 atomic_notifier_call_chain(&keyboard_notifier_list, KBD_POST_KEYSYM, ¶m);
1480
1481 if (type != KT_SLOCK)
1482 kbd->slockstate = 0;
1483}
1484
1485static void kbd_event(struct input_handle *handle, unsigned int event_type,
1486 unsigned int event_code, int value)
1487{
1488
1489 spin_lock(&kbd_event_lock);
1490
1491 if (event_type == EV_MSC && event_code == MSC_RAW && HW_RAW(handle->dev))
1492 kbd_rawcode(value);
1493 if (event_type == EV_KEY)
1494 kbd_keycode(event_code, value, HW_RAW(handle->dev));
1495
1496 spin_unlock(&kbd_event_lock);
1497
1498 tasklet_schedule(&keyboard_tasklet);
1499 do_poke_blanked_console = 1;
1500 schedule_console_callback();
1501}
1502
1503static bool kbd_match(struct input_handler *handler, struct input_dev *dev)
1504{
1505 int i;
1506
1507 if (test_bit(EV_SND, dev->evbit))
1508 return true;
1509
1510 if (test_bit(EV_KEY, dev->evbit)) {
1511 for (i = KEY_RESERVED; i < BTN_MISC; i++)
1512 if (test_bit(i, dev->keybit))
1513 return true;
1514 for (i = KEY_BRL_DOT1; i <= KEY_BRL_DOT10; i++)
1515 if (test_bit(i, dev->keybit))
1516 return true;
1517 }
1518
1519 return false;
1520}
1521
1522
1523
1524
1525
1526
1527
1528static int kbd_connect(struct input_handler *handler, struct input_dev *dev,
1529 const struct input_device_id *id)
1530{
1531 struct input_handle *handle;
1532 int error;
1533
1534 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
1535 if (!handle)
1536 return -ENOMEM;
1537
1538 handle->dev = dev;
1539 handle->handler = handler;
1540 handle->name = "kbd";
1541
1542 error = input_register_handle(handle);
1543 if (error)
1544 goto err_free_handle;
1545
1546 error = input_open_device(handle);
1547 if (error)
1548 goto err_unregister_handle;
1549
1550 return 0;
1551
1552 err_unregister_handle:
1553 input_unregister_handle(handle);
1554 err_free_handle:
1555 kfree(handle);
1556 return error;
1557}
1558
1559static void kbd_disconnect(struct input_handle *handle)
1560{
1561 input_close_device(handle);
1562 input_unregister_handle(handle);
1563 kfree(handle);
1564}
1565
1566
1567
1568
1569
1570static void kbd_start(struct input_handle *handle)
1571{
1572 tasklet_disable(&keyboard_tasklet);
1573
1574 if (ledstate != -1U)
1575 kbd_update_leds_helper(handle, &ledstate);
1576
1577 tasklet_enable(&keyboard_tasklet);
1578}
1579
1580static const struct input_device_id kbd_ids[] = {
1581 {
1582 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
1583 .evbit = { BIT_MASK(EV_KEY) },
1584 },
1585
1586 {
1587 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
1588 .evbit = { BIT_MASK(EV_SND) },
1589 },
1590
1591 { },
1592};
1593
1594MODULE_DEVICE_TABLE(input, kbd_ids);
1595
1596static struct input_handler kbd_handler = {
1597 .event = kbd_event,
1598 .match = kbd_match,
1599 .connect = kbd_connect,
1600 .disconnect = kbd_disconnect,
1601 .start = kbd_start,
1602 .name = "kbd",
1603 .id_table = kbd_ids,
1604};
1605
1606int __init kbd_init(void)
1607{
1608 int i;
1609 int error;
1610
1611 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1612 kbd_table[i].ledflagstate = kbd_defleds();
1613 kbd_table[i].default_ledflagstate = kbd_defleds();
1614 kbd_table[i].ledmode = LED_SHOW_FLAGS;
1615 kbd_table[i].lockstate = KBD_DEFLOCK;
1616 kbd_table[i].slockstate = 0;
1617 kbd_table[i].modeflags = KBD_DEFMODE;
1618 kbd_table[i].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
1619 }
1620
1621 kbd_init_leds();
1622
1623 error = input_register_handler(&kbd_handler);
1624 if (error)
1625 return error;
1626
1627 tasklet_enable(&keyboard_tasklet);
1628 tasklet_schedule(&keyboard_tasklet);
1629
1630 return 0;
1631}
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644int vt_do_diacrit(unsigned int cmd, void __user *up, int perm)
1645{
1646 struct kbdiacrs __user *a = up;
1647 unsigned long flags;
1648 int asize;
1649 int ret = 0;
1650
1651 switch (cmd) {
1652 case KDGKBDIACR:
1653 {
1654 struct kbdiacr *diacr;
1655 int i;
1656
1657 diacr = kmalloc(MAX_DIACR * sizeof(struct kbdiacr),
1658 GFP_KERNEL);
1659 if (diacr == NULL)
1660 return -ENOMEM;
1661
1662
1663
1664 spin_lock_irqsave(&kbd_event_lock, flags);
1665
1666 asize = accent_table_size;
1667 for (i = 0; i < asize; i++) {
1668 diacr[i].diacr = conv_uni_to_8bit(
1669 accent_table[i].diacr);
1670 diacr[i].base = conv_uni_to_8bit(
1671 accent_table[i].base);
1672 diacr[i].result = conv_uni_to_8bit(
1673 accent_table[i].result);
1674 }
1675 spin_unlock_irqrestore(&kbd_event_lock, flags);
1676
1677 if (put_user(asize, &a->kb_cnt))
1678 ret = -EFAULT;
1679 else if (copy_to_user(a->kbdiacr, diacr,
1680 asize * sizeof(struct kbdiacr)))
1681 ret = -EFAULT;
1682 kfree(diacr);
1683 return ret;
1684 }
1685 case KDGKBDIACRUC:
1686 {
1687 struct kbdiacrsuc __user *a = up;
1688 void *buf;
1689
1690 buf = kmalloc(MAX_DIACR * sizeof(struct kbdiacruc),
1691 GFP_KERNEL);
1692 if (buf == NULL)
1693 return -ENOMEM;
1694
1695
1696
1697 spin_lock_irqsave(&kbd_event_lock, flags);
1698
1699 asize = accent_table_size;
1700 memcpy(buf, accent_table, asize * sizeof(struct kbdiacruc));
1701
1702 spin_unlock_irqrestore(&kbd_event_lock, flags);
1703
1704 if (put_user(asize, &a->kb_cnt))
1705 ret = -EFAULT;
1706 else if (copy_to_user(a->kbdiacruc, buf,
1707 asize*sizeof(struct kbdiacruc)))
1708 ret = -EFAULT;
1709 kfree(buf);
1710 return ret;
1711 }
1712
1713 case KDSKBDIACR:
1714 {
1715 struct kbdiacrs __user *a = up;
1716 struct kbdiacr *diacr = NULL;
1717 unsigned int ct;
1718 int i;
1719
1720 if (!perm)
1721 return -EPERM;
1722 if (get_user(ct, &a->kb_cnt))
1723 return -EFAULT;
1724 if (ct >= MAX_DIACR)
1725 return -EINVAL;
1726
1727 if (ct) {
1728 diacr = kmalloc(sizeof(struct kbdiacr) * ct,
1729 GFP_KERNEL);
1730 if (diacr == NULL)
1731 return -ENOMEM;
1732
1733 if (copy_from_user(diacr, a->kbdiacr,
1734 sizeof(struct kbdiacr) * ct)) {
1735 kfree(diacr);
1736 return -EFAULT;
1737 }
1738 }
1739
1740 spin_lock_irqsave(&kbd_event_lock, flags);
1741 accent_table_size = ct;
1742 for (i = 0; i < ct; i++) {
1743 accent_table[i].diacr =
1744 conv_8bit_to_uni(diacr[i].diacr);
1745 accent_table[i].base =
1746 conv_8bit_to_uni(diacr[i].base);
1747 accent_table[i].result =
1748 conv_8bit_to_uni(diacr[i].result);
1749 }
1750 spin_unlock_irqrestore(&kbd_event_lock, flags);
1751 kfree(diacr);
1752 return 0;
1753 }
1754
1755 case KDSKBDIACRUC:
1756 {
1757 struct kbdiacrsuc __user *a = up;
1758 unsigned int ct;
1759 void *buf = NULL;
1760
1761 if (!perm)
1762 return -EPERM;
1763
1764 if (get_user(ct, &a->kb_cnt))
1765 return -EFAULT;
1766
1767 if (ct >= MAX_DIACR)
1768 return -EINVAL;
1769
1770 if (ct) {
1771 buf = kmalloc(ct * sizeof(struct kbdiacruc),
1772 GFP_KERNEL);
1773 if (buf == NULL)
1774 return -ENOMEM;
1775
1776 if (copy_from_user(buf, a->kbdiacruc,
1777 ct * sizeof(struct kbdiacruc))) {
1778 kfree(buf);
1779 return -EFAULT;
1780 }
1781 }
1782 spin_lock_irqsave(&kbd_event_lock, flags);
1783 if (ct)
1784 memcpy(accent_table, buf,
1785 ct * sizeof(struct kbdiacruc));
1786 accent_table_size = ct;
1787 spin_unlock_irqrestore(&kbd_event_lock, flags);
1788 kfree(buf);
1789 return 0;
1790 }
1791 }
1792 return ret;
1793}
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803int vt_do_kdskbmode(int console, unsigned int arg)
1804{
1805 struct kbd_struct * kbd = kbd_table + console;
1806 int ret = 0;
1807 unsigned long flags;
1808
1809 spin_lock_irqsave(&kbd_event_lock, flags);
1810 switch(arg) {
1811 case K_RAW:
1812 kbd->kbdmode = VC_RAW;
1813 break;
1814 case K_MEDIUMRAW:
1815 kbd->kbdmode = VC_MEDIUMRAW;
1816 break;
1817 case K_XLATE:
1818 kbd->kbdmode = VC_XLATE;
1819 do_compute_shiftstate();
1820 break;
1821 case K_UNICODE:
1822 kbd->kbdmode = VC_UNICODE;
1823 do_compute_shiftstate();
1824 break;
1825 case K_OFF:
1826 kbd->kbdmode = VC_OFF;
1827 break;
1828 default:
1829 ret = -EINVAL;
1830 }
1831 spin_unlock_irqrestore(&kbd_event_lock, flags);
1832 return ret;
1833}
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843int vt_do_kdskbmeta(int console, unsigned int arg)
1844{
1845 struct kbd_struct * kbd = kbd_table + console;
1846 int ret = 0;
1847 unsigned long flags;
1848
1849 spin_lock_irqsave(&kbd_event_lock, flags);
1850 switch(arg) {
1851 case K_METABIT:
1852 clr_vc_kbd_mode(kbd, VC_META);
1853 break;
1854 case K_ESCPREFIX:
1855 set_vc_kbd_mode(kbd, VC_META);
1856 break;
1857 default:
1858 ret = -EINVAL;
1859 }
1860 spin_unlock_irqrestore(&kbd_event_lock, flags);
1861 return ret;
1862}
1863
1864int vt_do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc,
1865 int perm)
1866{
1867 struct kbkeycode tmp;
1868 int kc = 0;
1869
1870 if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode)))
1871 return -EFAULT;
1872 switch (cmd) {
1873 case KDGETKEYCODE:
1874 kc = getkeycode(tmp.scancode);
1875 if (kc >= 0)
1876 kc = put_user(kc, &user_kbkc->keycode);
1877 break;
1878 case KDSETKEYCODE:
1879 if (!perm)
1880 return -EPERM;
1881 kc = setkeycode(tmp.scancode, tmp.keycode);
1882 break;
1883 }
1884 return kc;
1885}
1886
1887#define i (tmp.kb_index)
1888#define s (tmp.kb_table)
1889#define v (tmp.kb_value)
1890
1891int vt_do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm,
1892 int console)
1893{
1894 struct kbd_struct * kbd = kbd_table + console;
1895 struct kbentry tmp;
1896 ushort *key_map, *new_map, val, ov;
1897 unsigned long flags;
1898
1899 if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
1900 return -EFAULT;
1901
1902 if (!capable(CAP_SYS_TTY_CONFIG))
1903 perm = 0;
1904
1905 switch (cmd) {
1906 case KDGKBENT:
1907
1908 spin_lock_irqsave(&kbd_event_lock, flags);
1909 key_map = key_maps[s];
1910 if (key_map) {
1911 val = U(key_map[i]);
1912 if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES)
1913 val = K_HOLE;
1914 } else
1915 val = (i ? K_HOLE : K_NOSUCHMAP);
1916 spin_unlock_irqrestore(&kbd_event_lock, flags);
1917 return put_user(val, &user_kbe->kb_value);
1918 case KDSKBENT:
1919 if (!perm)
1920 return -EPERM;
1921 if (!i && v == K_NOSUCHMAP) {
1922 spin_lock_irqsave(&kbd_event_lock, flags);
1923
1924 key_map = key_maps[s];
1925 if (s && key_map) {
1926 key_maps[s] = NULL;
1927 if (key_map[0] == U(K_ALLOCATED)) {
1928 kfree(key_map);
1929 keymap_count--;
1930 }
1931 }
1932 spin_unlock_irqrestore(&kbd_event_lock, flags);
1933 break;
1934 }
1935
1936 if (KTYP(v) < NR_TYPES) {
1937 if (KVAL(v) > max_vals[KTYP(v)])
1938 return -EINVAL;
1939 } else
1940 if (kbd->kbdmode != VC_UNICODE)
1941 return -EINVAL;
1942
1943
1944#if !defined(__mc68000__) && !defined(__powerpc__)
1945
1946 if (!i)
1947 break;
1948#endif
1949
1950 new_map = kmalloc(sizeof(plain_map), GFP_KERNEL);
1951 if (!new_map)
1952 return -ENOMEM;
1953 spin_lock_irqsave(&kbd_event_lock, flags);
1954 key_map = key_maps[s];
1955 if (key_map == NULL) {
1956 int j;
1957
1958 if (keymap_count >= MAX_NR_OF_USER_KEYMAPS &&
1959 !capable(CAP_SYS_RESOURCE)) {
1960 spin_unlock_irqrestore(&kbd_event_lock, flags);
1961 kfree(new_map);
1962 return -EPERM;
1963 }
1964 key_maps[s] = new_map;
1965 key_map = new_map;
1966 key_map[0] = U(K_ALLOCATED);
1967 for (j = 1; j < NR_KEYS; j++)
1968 key_map[j] = U(K_HOLE);
1969 keymap_count++;
1970 } else
1971 kfree(new_map);
1972
1973 ov = U(key_map[i]);
1974 if (v == ov)
1975 goto out;
1976
1977
1978
1979 if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN)) {
1980 spin_unlock_irqrestore(&kbd_event_lock, flags);
1981 return -EPERM;
1982 }
1983 key_map[i] = U(v);
1984 if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT))
1985 do_compute_shiftstate();
1986out:
1987 spin_unlock_irqrestore(&kbd_event_lock, flags);
1988 break;
1989 }
1990 return 0;
1991}
1992#undef i
1993#undef s
1994#undef v
1995
1996
1997int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
1998{
1999 struct kbsentry *kbs;
2000 char *p;
2001 u_char *q;
2002 u_char __user *up;
2003 int sz;
2004 int delta;
2005 char *first_free, *fj, *fnw;
2006 int i, j, k;
2007 int ret;
2008
2009 if (!capable(CAP_SYS_TTY_CONFIG))
2010 perm = 0;
2011
2012 kbs = kmalloc(sizeof(*kbs), GFP_KERNEL);
2013 if (!kbs) {
2014 ret = -ENOMEM;
2015 goto reterr;
2016 }
2017
2018
2019 if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) {
2020 ret = -EFAULT;
2021 goto reterr;
2022 }
2023 kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0';
2024 i = kbs->kb_func;
2025
2026 switch (cmd) {
2027 case KDGKBSENT:
2028 sz = sizeof(kbs->kb_string) - 1;
2029
2030 up = user_kdgkb->kb_string;
2031 p = func_table[i];
2032 if(p)
2033 for ( ; *p && sz; p++, sz--)
2034 if (put_user(*p, up++)) {
2035 ret = -EFAULT;
2036 goto reterr;
2037 }
2038 if (put_user('\0', up)) {
2039 ret = -EFAULT;
2040 goto reterr;
2041 }
2042 kfree(kbs);
2043 return ((p && *p) ? -EOVERFLOW : 0);
2044 case KDSKBSENT:
2045 if (!perm) {
2046 ret = -EPERM;
2047 goto reterr;
2048 }
2049
2050 q = func_table[i];
2051 first_free = funcbufptr + (funcbufsize - funcbufleft);
2052 for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++)
2053 ;
2054 if (j < MAX_NR_FUNC)
2055 fj = func_table[j];
2056 else
2057 fj = first_free;
2058
2059 delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string);
2060 if (delta <= funcbufleft) {
2061 if (j < MAX_NR_FUNC) {
2062 memmove(fj + delta, fj, first_free - fj);
2063 for (k = j; k < MAX_NR_FUNC; k++)
2064 if (func_table[k])
2065 func_table[k] += delta;
2066 }
2067 if (!q)
2068 func_table[i] = fj;
2069 funcbufleft -= delta;
2070 } else {
2071 sz = 256;
2072 while (sz < funcbufsize - funcbufleft + delta)
2073 sz <<= 1;
2074 fnw = kmalloc(sz, GFP_KERNEL);
2075 if(!fnw) {
2076 ret = -ENOMEM;
2077 goto reterr;
2078 }
2079
2080 if (!q)
2081 func_table[i] = fj;
2082 if (fj > funcbufptr)
2083 memmove(fnw, funcbufptr, fj - funcbufptr);
2084 for (k = 0; k < j; k++)
2085 if (func_table[k])
2086 func_table[k] = fnw + (func_table[k] - funcbufptr);
2087
2088 if (first_free > fj) {
2089 memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj);
2090 for (k = j; k < MAX_NR_FUNC; k++)
2091 if (func_table[k])
2092 func_table[k] = fnw + (func_table[k] - funcbufptr) + delta;
2093 }
2094 if (funcbufptr != func_buf)
2095 kfree(funcbufptr);
2096 funcbufptr = fnw;
2097 funcbufleft = funcbufleft - delta + sz - funcbufsize;
2098 funcbufsize = sz;
2099 }
2100 strcpy(func_table[i], kbs->kb_string);
2101 break;
2102 }
2103 ret = 0;
2104reterr:
2105 kfree(kbs);
2106 return ret;
2107}
2108
2109int vt_do_kdskled(int console, int cmd, unsigned long arg, int perm)
2110{
2111 struct kbd_struct * kbd = kbd_table + console;
2112 unsigned long flags;
2113 unsigned char ucval;
2114
2115 switch(cmd) {
2116
2117
2118 case KDGKBLED:
2119 spin_lock_irqsave(&kbd_event_lock, flags);
2120 ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4);
2121 spin_unlock_irqrestore(&kbd_event_lock, flags);
2122 return put_user(ucval, (char __user *)arg);
2123
2124 case KDSKBLED:
2125 if (!perm)
2126 return -EPERM;
2127 if (arg & ~0x77)
2128 return -EINVAL;
2129 spin_lock_irqsave(&led_lock, flags);
2130 kbd->ledflagstate = (arg & 7);
2131 kbd->default_ledflagstate = ((arg >> 4) & 7);
2132 set_leds();
2133 spin_unlock_irqrestore(&led_lock, flags);
2134 return 0;
2135
2136
2137
2138 case KDGETLED:
2139 ucval = getledstate();
2140 return put_user(ucval, (char __user *)arg);
2141
2142 case KDSETLED:
2143 if (!perm)
2144 return -EPERM;
2145 setledstate(kbd, arg);
2146 return 0;
2147 }
2148 return -ENOIOCTLCMD;
2149}
2150
2151int vt_do_kdgkbmode(int console)
2152{
2153 struct kbd_struct * kbd = kbd_table + console;
2154
2155 switch (kbd->kbdmode) {
2156 case VC_RAW:
2157 return K_RAW;
2158 case VC_MEDIUMRAW:
2159 return K_MEDIUMRAW;
2160 case VC_UNICODE:
2161 return K_UNICODE;
2162 case VC_OFF:
2163 return K_OFF;
2164 default:
2165 return K_XLATE;
2166 }
2167}
2168
2169
2170
2171
2172
2173
2174
2175int vt_do_kdgkbmeta(int console)
2176{
2177 struct kbd_struct * kbd = kbd_table + console;
2178
2179 return vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT;
2180}
2181
2182
2183
2184
2185
2186
2187
2188void vt_reset_unicode(int console)
2189{
2190 unsigned long flags;
2191
2192 spin_lock_irqsave(&kbd_event_lock, flags);
2193 kbd_table[console].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
2194 spin_unlock_irqrestore(&kbd_event_lock, flags);
2195}
2196
2197
2198
2199
2200
2201
2202
2203int vt_get_shift_state(void)
2204{
2205
2206 return shift_state;
2207}
2208
2209
2210
2211
2212
2213
2214
2215
2216void vt_reset_keyboard(int console)
2217{
2218 struct kbd_struct * kbd = kbd_table + console;
2219 unsigned long flags;
2220
2221 spin_lock_irqsave(&kbd_event_lock, flags);
2222 set_vc_kbd_mode(kbd, VC_REPEAT);
2223 clr_vc_kbd_mode(kbd, VC_CKMODE);
2224 clr_vc_kbd_mode(kbd, VC_APPLIC);
2225 clr_vc_kbd_mode(kbd, VC_CRLF);
2226 kbd->lockstate = 0;
2227 kbd->slockstate = 0;
2228 spin_lock(&led_lock);
2229 kbd->ledmode = LED_SHOW_FLAGS;
2230 kbd->ledflagstate = kbd->default_ledflagstate;
2231 spin_unlock(&led_lock);
2232
2233
2234 spin_unlock_irqrestore(&kbd_event_lock, flags);
2235}
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246int vt_get_kbd_mode_bit(int console, int bit)
2247{
2248 struct kbd_struct * kbd = kbd_table + console;
2249 return vc_kbd_mode(kbd, bit);
2250}
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261void vt_set_kbd_mode_bit(int console, int bit)
2262{
2263 struct kbd_struct * kbd = kbd_table + console;
2264 unsigned long flags;
2265
2266 spin_lock_irqsave(&kbd_event_lock, flags);
2267 set_vc_kbd_mode(kbd, bit);
2268 spin_unlock_irqrestore(&kbd_event_lock, flags);
2269}
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280void vt_clr_kbd_mode_bit(int console, int bit)
2281{
2282 struct kbd_struct * kbd = kbd_table + console;
2283 unsigned long flags;
2284
2285 spin_lock_irqsave(&kbd_event_lock, flags);
2286 clr_vc_kbd_mode(kbd, bit);
2287 spin_unlock_irqrestore(&kbd_event_lock, flags);
2288}
2289