mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / tty / sysrq.c
blob3b9b80856c1b36105ac67f4356ab107fd10f04f7
1 /*
2 * Linux Magic System Request Key Hacks
4 * (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
5 * based on ideas by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>
7 * (c) 2000 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
8 * overhauled to use key registration
9 * based upon discusions in irc://irc.openprojects.net/#kernelnewbies
11 * Copyright (c) 2010 Dmitry Torokhov
12 * Input handler conversion
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/sched.h>
18 #include <linux/sched/rt.h>
19 #include <linux/interrupt.h>
20 #include <linux/mm.h>
21 #include <linux/fs.h>
22 #include <linux/mount.h>
23 #include <linux/kdev_t.h>
24 #include <linux/major.h>
25 #include <linux/reboot.h>
26 #include <linux/sysrq.h>
27 #include <linux/kbd_kern.h>
28 #include <linux/proc_fs.h>
29 #include <linux/nmi.h>
30 #include <linux/quotaops.h>
31 #include <linux/perf_event.h>
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/suspend.h>
35 #include <linux/writeback.h>
36 #include <linux/swap.h>
37 #include <linux/spinlock.h>
38 #include <linux/vt_kern.h>
39 #include <linux/workqueue.h>
40 #include <linux/hrtimer.h>
41 #include <linux/oom.h>
42 #include <linux/slab.h>
43 #include <linux/input.h>
44 #include <linux/uaccess.h>
45 #include <linux/moduleparam.h>
46 #include <linux/jiffies.h>
47 #include <linux/syscalls.h>
48 #include <linux/of.h>
50 #include <asm/ptrace.h>
51 #include <asm/irq_regs.h>
53 /* Whether we react on sysrq keys or just ignore them */
54 static int __read_mostly sysrq_enabled = SYSRQ_DEFAULT_ENABLE;
55 static bool __read_mostly sysrq_always_enabled;
57 static bool sysrq_on(void)
59 return sysrq_enabled || sysrq_always_enabled;
63 * A value of 1 means 'all', other nonzero values are an op mask:
65 static bool sysrq_on_mask(int mask)
67 return sysrq_always_enabled ||
68 sysrq_enabled == 1 ||
69 (sysrq_enabled & mask);
72 static int __init sysrq_always_enabled_setup(char *str)
74 sysrq_always_enabled = true;
75 pr_info("sysrq always enabled.\n");
77 return 1;
80 __setup("sysrq_always_enabled", sysrq_always_enabled_setup);
83 static void sysrq_handle_loglevel(int key)
85 int i;
87 i = key - '0';
88 console_loglevel = 7;
89 printk("Loglevel set to %d\n", i);
90 console_loglevel = i;
92 static struct sysrq_key_op sysrq_loglevel_op = {
93 .handler = sysrq_handle_loglevel,
94 .help_msg = "loglevel(0-9)",
95 .action_msg = "Changing Loglevel",
96 .enable_mask = SYSRQ_ENABLE_LOG,
99 #ifdef CONFIG_VT
100 static void sysrq_handle_SAK(int key)
102 struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work;
103 schedule_work(SAK_work);
105 static struct sysrq_key_op sysrq_SAK_op = {
106 .handler = sysrq_handle_SAK,
107 .help_msg = "sak(k)",
108 .action_msg = "SAK",
109 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
111 #else
112 #define sysrq_SAK_op (*(struct sysrq_key_op *)NULL)
113 #endif
115 #ifdef CONFIG_VT
116 static void sysrq_handle_unraw(int key)
118 vt_reset_unicode(fg_console);
121 static struct sysrq_key_op sysrq_unraw_op = {
122 .handler = sysrq_handle_unraw,
123 .help_msg = "unraw(r)",
124 .action_msg = "Keyboard mode set to system default",
125 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
127 #else
128 #define sysrq_unraw_op (*(struct sysrq_key_op *)NULL)
129 #endif /* CONFIG_VT */
131 static void sysrq_handle_crash(int key)
133 char *killer = NULL;
135 panic_on_oops = 1; /* force panic */
136 wmb();
137 *killer = 1;
139 static struct sysrq_key_op sysrq_crash_op = {
140 .handler = sysrq_handle_crash,
141 .help_msg = "crash(c)",
142 .action_msg = "Trigger a crash",
143 .enable_mask = SYSRQ_ENABLE_DUMP,
146 static void sysrq_handle_reboot(int key)
148 lockdep_off();
149 local_irq_enable();
150 emergency_restart();
152 static struct sysrq_key_op sysrq_reboot_op = {
153 .handler = sysrq_handle_reboot,
154 .help_msg = "reboot(b)",
155 .action_msg = "Resetting",
156 .enable_mask = SYSRQ_ENABLE_BOOT,
159 static void sysrq_handle_sync(int key)
161 emergency_sync();
163 static struct sysrq_key_op sysrq_sync_op = {
164 .handler = sysrq_handle_sync,
165 .help_msg = "sync(s)",
166 .action_msg = "Emergency Sync",
167 .enable_mask = SYSRQ_ENABLE_SYNC,
170 static void sysrq_handle_show_timers(int key)
172 sysrq_timer_list_show();
175 static struct sysrq_key_op sysrq_show_timers_op = {
176 .handler = sysrq_handle_show_timers,
177 .help_msg = "show-all-timers(q)",
178 .action_msg = "Show clockevent devices & pending hrtimers (no others)",
181 static void sysrq_handle_mountro(int key)
183 emergency_remount();
185 static struct sysrq_key_op sysrq_mountro_op = {
186 .handler = sysrq_handle_mountro,
187 .help_msg = "unmount(u)",
188 .action_msg = "Emergency Remount R/O",
189 .enable_mask = SYSRQ_ENABLE_REMOUNT,
192 #ifdef CONFIG_LOCKDEP
193 static void sysrq_handle_showlocks(int key)
195 debug_show_all_locks();
198 static struct sysrq_key_op sysrq_showlocks_op = {
199 .handler = sysrq_handle_showlocks,
200 .help_msg = "show-all-locks(d)",
201 .action_msg = "Show Locks Held",
203 #else
204 #define sysrq_showlocks_op (*(struct sysrq_key_op *)NULL)
205 #endif
207 #ifdef CONFIG_SMP
208 static DEFINE_SPINLOCK(show_lock);
210 static void showacpu(void *dummy)
212 unsigned long flags;
214 /* Idle CPUs have no interesting backtrace. */
215 if (idle_cpu(smp_processor_id()))
216 return;
218 spin_lock_irqsave(&show_lock, flags);
219 printk(KERN_INFO "CPU%d:\n", smp_processor_id());
220 show_stack(NULL, NULL);
221 spin_unlock_irqrestore(&show_lock, flags);
224 static void sysrq_showregs_othercpus(struct work_struct *dummy)
226 smp_call_function(showacpu, NULL, 0);
229 static DECLARE_WORK(sysrq_showallcpus, sysrq_showregs_othercpus);
231 static void sysrq_handle_showallcpus(int key)
234 * Fall back to the workqueue based printing if the
235 * backtrace printing did not succeed or the
236 * architecture has no support for it:
238 if (!trigger_all_cpu_backtrace()) {
239 struct pt_regs *regs = get_irq_regs();
241 if (regs) {
242 printk(KERN_INFO "CPU%d:\n", smp_processor_id());
243 show_regs(regs);
245 schedule_work(&sysrq_showallcpus);
249 static struct sysrq_key_op sysrq_showallcpus_op = {
250 .handler = sysrq_handle_showallcpus,
251 .help_msg = "show-backtrace-all-active-cpus(l)",
252 .action_msg = "Show backtrace of all active CPUs",
253 .enable_mask = SYSRQ_ENABLE_DUMP,
255 #endif
257 static void sysrq_handle_showregs(int key)
259 struct pt_regs *regs = get_irq_regs();
260 if (regs)
261 show_regs(regs);
262 perf_event_print_debug();
264 static struct sysrq_key_op sysrq_showregs_op = {
265 .handler = sysrq_handle_showregs,
266 .help_msg = "show-registers(p)",
267 .action_msg = "Show Regs",
268 .enable_mask = SYSRQ_ENABLE_DUMP,
271 static void sysrq_handle_showstate(int key)
273 show_state();
275 static struct sysrq_key_op sysrq_showstate_op = {
276 .handler = sysrq_handle_showstate,
277 .help_msg = "show-task-states(t)",
278 .action_msg = "Show State",
279 .enable_mask = SYSRQ_ENABLE_DUMP,
282 static void sysrq_handle_showstate_blocked(int key)
284 show_state_filter(TASK_UNINTERRUPTIBLE);
286 static struct sysrq_key_op sysrq_showstate_blocked_op = {
287 .handler = sysrq_handle_showstate_blocked,
288 .help_msg = "show-blocked-tasks(w)",
289 .action_msg = "Show Blocked State",
290 .enable_mask = SYSRQ_ENABLE_DUMP,
293 #ifdef CONFIG_TRACING
294 #include <linux/ftrace.h>
296 static void sysrq_ftrace_dump(int key)
298 ftrace_dump(DUMP_ALL);
300 static struct sysrq_key_op sysrq_ftrace_dump_op = {
301 .handler = sysrq_ftrace_dump,
302 .help_msg = "dump-ftrace-buffer(z)",
303 .action_msg = "Dump ftrace buffer",
304 .enable_mask = SYSRQ_ENABLE_DUMP,
306 #else
307 #define sysrq_ftrace_dump_op (*(struct sysrq_key_op *)NULL)
308 #endif
310 static void sysrq_handle_showmem(int key)
312 show_mem(0);
314 static struct sysrq_key_op sysrq_showmem_op = {
315 .handler = sysrq_handle_showmem,
316 .help_msg = "show-memory-usage(m)",
317 .action_msg = "Show Memory",
318 .enable_mask = SYSRQ_ENABLE_DUMP,
322 * Signal sysrq helper function. Sends a signal to all user processes.
324 static void send_sig_all(int sig)
326 struct task_struct *p;
328 read_lock(&tasklist_lock);
329 for_each_process(p) {
330 if (p->flags & PF_KTHREAD)
331 continue;
332 if (is_global_init(p))
333 continue;
335 do_send_sig_info(sig, SEND_SIG_FORCED, p, true);
337 read_unlock(&tasklist_lock);
340 static void sysrq_handle_term(int key)
342 send_sig_all(SIGTERM);
343 console_loglevel = 8;
345 static struct sysrq_key_op sysrq_term_op = {
346 .handler = sysrq_handle_term,
347 .help_msg = "terminate-all-tasks(e)",
348 .action_msg = "Terminate All Tasks",
349 .enable_mask = SYSRQ_ENABLE_SIGNAL,
352 static void moom_callback(struct work_struct *ignored)
354 out_of_memory(node_zonelist(first_online_node, GFP_KERNEL), GFP_KERNEL,
355 0, NULL, true);
358 static DECLARE_WORK(moom_work, moom_callback);
360 static void sysrq_handle_moom(int key)
362 schedule_work(&moom_work);
364 static struct sysrq_key_op sysrq_moom_op = {
365 .handler = sysrq_handle_moom,
366 .help_msg = "memory-full-oom-kill(f)",
367 .action_msg = "Manual OOM execution",
368 .enable_mask = SYSRQ_ENABLE_SIGNAL,
371 #ifdef CONFIG_BLOCK
372 static void sysrq_handle_thaw(int key)
374 emergency_thaw_all();
376 static struct sysrq_key_op sysrq_thaw_op = {
377 .handler = sysrq_handle_thaw,
378 .help_msg = "thaw-filesystems(j)",
379 .action_msg = "Emergency Thaw of all frozen filesystems",
380 .enable_mask = SYSRQ_ENABLE_SIGNAL,
382 #endif
384 static void sysrq_handle_kill(int key)
386 send_sig_all(SIGKILL);
387 console_loglevel = 8;
389 static struct sysrq_key_op sysrq_kill_op = {
390 .handler = sysrq_handle_kill,
391 .help_msg = "kill-all-tasks(i)",
392 .action_msg = "Kill All Tasks",
393 .enable_mask = SYSRQ_ENABLE_SIGNAL,
396 static void sysrq_handle_unrt(int key)
398 normalize_rt_tasks();
400 static struct sysrq_key_op sysrq_unrt_op = {
401 .handler = sysrq_handle_unrt,
402 .help_msg = "nice-all-RT-tasks(n)",
403 .action_msg = "Nice All RT Tasks",
404 .enable_mask = SYSRQ_ENABLE_RTNICE,
407 /* Key Operations table and lock */
408 static DEFINE_SPINLOCK(sysrq_key_table_lock);
410 static struct sysrq_key_op *sysrq_key_table[36] = {
411 &sysrq_loglevel_op, /* 0 */
412 &sysrq_loglevel_op, /* 1 */
413 &sysrq_loglevel_op, /* 2 */
414 &sysrq_loglevel_op, /* 3 */
415 &sysrq_loglevel_op, /* 4 */
416 &sysrq_loglevel_op, /* 5 */
417 &sysrq_loglevel_op, /* 6 */
418 &sysrq_loglevel_op, /* 7 */
419 &sysrq_loglevel_op, /* 8 */
420 &sysrq_loglevel_op, /* 9 */
423 * a: Don't use for system provided sysrqs, it is handled specially on
424 * sparc and will never arrive.
426 NULL, /* a */
427 &sysrq_reboot_op, /* b */
428 &sysrq_crash_op, /* c & ibm_emac driver debug */
429 &sysrq_showlocks_op, /* d */
430 &sysrq_term_op, /* e */
431 &sysrq_moom_op, /* f */
432 /* g: May be registered for the kernel debugger */
433 NULL, /* g */
434 NULL, /* h - reserved for help */
435 &sysrq_kill_op, /* i */
436 #ifdef CONFIG_BLOCK
437 &sysrq_thaw_op, /* j */
438 #else
439 NULL, /* j */
440 #endif
441 &sysrq_SAK_op, /* k */
442 #ifdef CONFIG_SMP
443 &sysrq_showallcpus_op, /* l */
444 #else
445 NULL, /* l */
446 #endif
447 &sysrq_showmem_op, /* m */
448 &sysrq_unrt_op, /* n */
449 /* o: This will often be registered as 'Off' at init time */
450 NULL, /* o */
451 &sysrq_showregs_op, /* p */
452 &sysrq_show_timers_op, /* q */
453 &sysrq_unraw_op, /* r */
454 &sysrq_sync_op, /* s */
455 &sysrq_showstate_op, /* t */
456 &sysrq_mountro_op, /* u */
457 /* v: May be registered for frame buffer console restore */
458 NULL, /* v */
459 &sysrq_showstate_blocked_op, /* w */
460 /* x: May be registered on ppc/powerpc for xmon */
461 /* x: May be registered on sparc64 for global PMU dump */
462 NULL, /* x */
463 /* y: May be registered on sparc64 for global register dump */
464 NULL, /* y */
465 &sysrq_ftrace_dump_op, /* z */
468 /* key2index calculation, -1 on invalid index */
469 static int sysrq_key_table_key2index(int key)
471 int retval;
473 if ((key >= '0') && (key <= '9'))
474 retval = key - '0';
475 else if ((key >= 'a') && (key <= 'z'))
476 retval = key + 10 - 'a';
477 else
478 retval = -1;
479 return retval;
483 * get and put functions for the table, exposed to modules.
485 struct sysrq_key_op *__sysrq_get_key_op(int key)
487 struct sysrq_key_op *op_p = NULL;
488 int i;
490 i = sysrq_key_table_key2index(key);
491 if (i != -1)
492 op_p = sysrq_key_table[i];
494 return op_p;
497 static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
499 int i = sysrq_key_table_key2index(key);
501 if (i != -1)
502 sysrq_key_table[i] = op_p;
505 void __handle_sysrq(int key, bool check_mask)
507 struct sysrq_key_op *op_p;
508 int orig_log_level;
509 int i;
510 unsigned long flags;
512 spin_lock_irqsave(&sysrq_key_table_lock, flags);
514 * Raise the apparent loglevel to maximum so that the sysrq header
515 * is shown to provide the user with positive feedback. We do not
516 * simply emit this at KERN_EMERG as that would change message
517 * routing in the consumers of /proc/kmsg.
519 orig_log_level = console_loglevel;
520 console_loglevel = 7;
521 printk(KERN_INFO "SysRq : ");
523 op_p = __sysrq_get_key_op(key);
524 if (op_p) {
526 * Should we check for enabled operations (/proc/sysrq-trigger
527 * should not) and is the invoked operation enabled?
529 if (!check_mask || sysrq_on_mask(op_p->enable_mask)) {
530 printk("%s\n", op_p->action_msg);
531 console_loglevel = orig_log_level;
532 op_p->handler(key);
533 } else {
534 printk("This sysrq operation is disabled.\n");
536 } else {
537 printk("HELP : ");
538 /* Only print the help msg once per handler */
539 for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
540 if (sysrq_key_table[i]) {
541 int j;
543 for (j = 0; sysrq_key_table[i] !=
544 sysrq_key_table[j]; j++)
546 if (j != i)
547 continue;
548 printk("%s ", sysrq_key_table[i]->help_msg);
551 printk("\n");
552 console_loglevel = orig_log_level;
554 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
557 void handle_sysrq(int key)
559 if (sysrq_on())
560 __handle_sysrq(key, true);
562 EXPORT_SYMBOL(handle_sysrq);
564 #ifdef CONFIG_INPUT
565 static int sysrq_reset_downtime_ms;
567 /* Simple translation table for the SysRq keys */
568 static const unsigned char sysrq_xlate[KEY_CNT] =
569 "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
570 "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
571 "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
572 "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
573 "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
574 "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
575 "\r\000/"; /* 0x60 - 0x6f */
577 struct sysrq_state {
578 struct input_handle handle;
579 struct work_struct reinject_work;
580 unsigned long key_down[BITS_TO_LONGS(KEY_CNT)];
581 unsigned int alt;
582 unsigned int alt_use;
583 bool active;
584 bool need_reinject;
585 bool reinjecting;
587 /* reset sequence handling */
588 bool reset_canceled;
589 bool reset_requested;
590 unsigned long reset_keybit[BITS_TO_LONGS(KEY_CNT)];
591 int reset_seq_len;
592 int reset_seq_cnt;
593 int reset_seq_version;
594 struct timer_list keyreset_timer;
597 #define SYSRQ_KEY_RESET_MAX 20 /* Should be plenty */
598 static unsigned short sysrq_reset_seq[SYSRQ_KEY_RESET_MAX];
599 static unsigned int sysrq_reset_seq_len;
600 static unsigned int sysrq_reset_seq_version = 1;
602 static void sysrq_parse_reset_sequence(struct sysrq_state *state)
604 int i;
605 unsigned short key;
607 state->reset_seq_cnt = 0;
609 for (i = 0; i < sysrq_reset_seq_len; i++) {
610 key = sysrq_reset_seq[i];
612 if (key == KEY_RESERVED || key > KEY_MAX)
613 break;
615 __set_bit(key, state->reset_keybit);
616 state->reset_seq_len++;
618 if (test_bit(key, state->key_down))
619 state->reset_seq_cnt++;
622 /* Disable reset until old keys are not released */
623 state->reset_canceled = state->reset_seq_cnt != 0;
625 state->reset_seq_version = sysrq_reset_seq_version;
628 static void sysrq_do_reset(unsigned long _state)
630 struct sysrq_state *state = (struct sysrq_state *) _state;
632 state->reset_requested = true;
634 sys_sync();
635 kernel_restart(NULL);
638 static void sysrq_handle_reset_request(struct sysrq_state *state)
640 if (state->reset_requested)
641 __handle_sysrq(sysrq_xlate[KEY_B], false);
643 if (sysrq_reset_downtime_ms)
644 mod_timer(&state->keyreset_timer,
645 jiffies + msecs_to_jiffies(sysrq_reset_downtime_ms));
646 else
647 sysrq_do_reset((unsigned long)state);
650 static void sysrq_detect_reset_sequence(struct sysrq_state *state,
651 unsigned int code, int value)
653 if (!test_bit(code, state->reset_keybit)) {
655 * Pressing any key _not_ in reset sequence cancels
656 * the reset sequence. Also cancelling the timer in
657 * case additional keys were pressed after a reset
658 * has been requested.
660 if (value && state->reset_seq_cnt) {
661 state->reset_canceled = true;
662 del_timer(&state->keyreset_timer);
664 } else if (value == 0) {
666 * Key release - all keys in the reset sequence need
667 * to be pressed and held for the reset timeout
668 * to hold.
670 del_timer(&state->keyreset_timer);
672 if (--state->reset_seq_cnt == 0)
673 state->reset_canceled = false;
674 } else if (value == 1) {
675 /* key press, not autorepeat */
676 if (++state->reset_seq_cnt == state->reset_seq_len &&
677 !state->reset_canceled) {
678 sysrq_handle_reset_request(state);
683 #ifdef CONFIG_OF
684 static void sysrq_of_get_keyreset_config(void)
686 u32 key;
687 struct device_node *np;
688 struct property *prop;
689 const __be32 *p;
691 np = of_find_node_by_path("/chosen/linux,sysrq-reset-seq");
692 if (!np) {
693 pr_debug("No sysrq node found");
694 return;
697 /* Reset in case a __weak definition was present */
698 sysrq_reset_seq_len = 0;
700 of_property_for_each_u32(np, "keyset", prop, p, key) {
701 if (key == KEY_RESERVED || key > KEY_MAX ||
702 sysrq_reset_seq_len == SYSRQ_KEY_RESET_MAX)
703 break;
705 sysrq_reset_seq[sysrq_reset_seq_len++] = (unsigned short)key;
708 /* Get reset timeout if any. */
709 of_property_read_u32(np, "timeout-ms", &sysrq_reset_downtime_ms);
711 #else
712 static void sysrq_of_get_keyreset_config(void)
715 #endif
717 static void sysrq_reinject_alt_sysrq(struct work_struct *work)
719 struct sysrq_state *sysrq =
720 container_of(work, struct sysrq_state, reinject_work);
721 struct input_handle *handle = &sysrq->handle;
722 unsigned int alt_code = sysrq->alt_use;
724 if (sysrq->need_reinject) {
725 /* we do not want the assignment to be reordered */
726 sysrq->reinjecting = true;
727 mb();
729 /* Simulate press and release of Alt + SysRq */
730 input_inject_event(handle, EV_KEY, alt_code, 1);
731 input_inject_event(handle, EV_KEY, KEY_SYSRQ, 1);
732 input_inject_event(handle, EV_SYN, SYN_REPORT, 1);
734 input_inject_event(handle, EV_KEY, KEY_SYSRQ, 0);
735 input_inject_event(handle, EV_KEY, alt_code, 0);
736 input_inject_event(handle, EV_SYN, SYN_REPORT, 1);
738 mb();
739 sysrq->reinjecting = false;
743 static bool sysrq_handle_keypress(struct sysrq_state *sysrq,
744 unsigned int code, int value)
746 bool was_active = sysrq->active;
747 bool suppress;
749 switch (code) {
751 case KEY_LEFTALT:
752 case KEY_RIGHTALT:
753 if (!value) {
754 /* One of ALTs is being released */
755 if (sysrq->active && code == sysrq->alt_use)
756 sysrq->active = false;
758 sysrq->alt = KEY_RESERVED;
760 } else if (value != 2) {
761 sysrq->alt = code;
762 sysrq->need_reinject = false;
764 break;
766 case KEY_SYSRQ:
767 if (value == 1 && sysrq->alt != KEY_RESERVED) {
768 sysrq->active = true;
769 sysrq->alt_use = sysrq->alt;
771 * If nothing else will be pressed we'll need
772 * to re-inject Alt-SysRq keysroke.
774 sysrq->need_reinject = true;
778 * Pretend that sysrq was never pressed at all. This
779 * is needed to properly handle KGDB which will try
780 * to release all keys after exiting debugger. If we
781 * do not clear key bit it KGDB will end up sending
782 * release events for Alt and SysRq, potentially
783 * triggering print screen function.
785 if (sysrq->active)
786 clear_bit(KEY_SYSRQ, sysrq->handle.dev->key);
788 break;
790 default:
791 if (sysrq->active && value && value != 2) {
792 sysrq->need_reinject = false;
793 __handle_sysrq(sysrq_xlate[code], true);
795 break;
798 suppress = sysrq->active;
800 if (!sysrq->active) {
803 * See if reset sequence has changed since the last time.
805 if (sysrq->reset_seq_version != sysrq_reset_seq_version)
806 sysrq_parse_reset_sequence(sysrq);
809 * If we are not suppressing key presses keep track of
810 * keyboard state so we can release keys that have been
811 * pressed before entering SysRq mode.
813 if (value)
814 set_bit(code, sysrq->key_down);
815 else
816 clear_bit(code, sysrq->key_down);
818 if (was_active)
819 schedule_work(&sysrq->reinject_work);
821 /* Check for reset sequence */
822 sysrq_detect_reset_sequence(sysrq, code, value);
824 } else if (value == 0 && test_and_clear_bit(code, sysrq->key_down)) {
826 * Pass on release events for keys that was pressed before
827 * entering SysRq mode.
829 suppress = false;
832 return suppress;
835 static bool sysrq_filter(struct input_handle *handle,
836 unsigned int type, unsigned int code, int value)
838 struct sysrq_state *sysrq = handle->private;
839 bool suppress;
842 * Do not filter anything if we are in the process of re-injecting
843 * Alt+SysRq combination.
845 if (sysrq->reinjecting)
846 return false;
848 switch (type) {
850 case EV_SYN:
851 suppress = false;
852 break;
854 case EV_KEY:
855 suppress = sysrq_handle_keypress(sysrq, code, value);
856 break;
858 default:
859 suppress = sysrq->active;
860 break;
863 return suppress;
866 static int sysrq_connect(struct input_handler *handler,
867 struct input_dev *dev,
868 const struct input_device_id *id)
870 struct sysrq_state *sysrq;
871 int error;
873 sysrq = kzalloc(sizeof(struct sysrq_state), GFP_KERNEL);
874 if (!sysrq)
875 return -ENOMEM;
877 INIT_WORK(&sysrq->reinject_work, sysrq_reinject_alt_sysrq);
879 sysrq->handle.dev = dev;
880 sysrq->handle.handler = handler;
881 sysrq->handle.name = "sysrq";
882 sysrq->handle.private = sysrq;
883 setup_timer(&sysrq->keyreset_timer,
884 sysrq_do_reset, (unsigned long)sysrq);
886 error = input_register_handle(&sysrq->handle);
887 if (error) {
888 pr_err("Failed to register input sysrq handler, error %d\n",
889 error);
890 goto err_free;
893 error = input_open_device(&sysrq->handle);
894 if (error) {
895 pr_err("Failed to open input device, error %d\n", error);
896 goto err_unregister;
899 return 0;
901 err_unregister:
902 input_unregister_handle(&sysrq->handle);
903 err_free:
904 kfree(sysrq);
905 return error;
908 static void sysrq_disconnect(struct input_handle *handle)
910 struct sysrq_state *sysrq = handle->private;
912 input_close_device(handle);
913 cancel_work_sync(&sysrq->reinject_work);
914 del_timer_sync(&sysrq->keyreset_timer);
915 input_unregister_handle(handle);
916 kfree(sysrq);
920 * We are matching on KEY_LEFTALT instead of KEY_SYSRQ because not all
921 * keyboards have SysRq key predefined and so user may add it to keymap
922 * later, but we expect all such keyboards to have left alt.
924 static const struct input_device_id sysrq_ids[] = {
926 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
927 INPUT_DEVICE_ID_MATCH_KEYBIT,
928 .evbit = { BIT_MASK(EV_KEY) },
929 .keybit = { BIT_MASK(KEY_LEFTALT) },
931 { },
934 static struct input_handler sysrq_handler = {
935 .filter = sysrq_filter,
936 .connect = sysrq_connect,
937 .disconnect = sysrq_disconnect,
938 .name = "sysrq",
939 .id_table = sysrq_ids,
942 static bool sysrq_handler_registered;
944 static inline void sysrq_register_handler(void)
946 int error;
948 sysrq_of_get_keyreset_config();
950 error = input_register_handler(&sysrq_handler);
951 if (error)
952 pr_err("Failed to register input handler, error %d", error);
953 else
954 sysrq_handler_registered = true;
957 static inline void sysrq_unregister_handler(void)
959 if (sysrq_handler_registered) {
960 input_unregister_handler(&sysrq_handler);
961 sysrq_handler_registered = false;
965 static int sysrq_reset_seq_param_set(const char *buffer,
966 const struct kernel_param *kp)
968 unsigned long val;
969 int error;
971 error = kstrtoul(buffer, 0, &val);
972 if (error < 0)
973 return error;
975 if (val > KEY_MAX)
976 return -EINVAL;
978 *((unsigned short *)kp->arg) = val;
979 sysrq_reset_seq_version++;
981 return 0;
984 static struct kernel_param_ops param_ops_sysrq_reset_seq = {
985 .get = param_get_ushort,
986 .set = sysrq_reset_seq_param_set,
989 #define param_check_sysrq_reset_seq(name, p) \
990 __param_check(name, p, unsigned short)
992 module_param_array_named(reset_seq, sysrq_reset_seq, sysrq_reset_seq,
993 &sysrq_reset_seq_len, 0644);
995 module_param_named(sysrq_downtime_ms, sysrq_reset_downtime_ms, int, 0644);
997 #else
999 static inline void sysrq_register_handler(void)
1003 static inline void sysrq_unregister_handler(void)
1007 #endif /* CONFIG_INPUT */
1009 int sysrq_toggle_support(int enable_mask)
1011 bool was_enabled = sysrq_on();
1013 sysrq_enabled = enable_mask;
1015 if (was_enabled != sysrq_on()) {
1016 if (sysrq_on())
1017 sysrq_register_handler();
1018 else
1019 sysrq_unregister_handler();
1022 return 0;
1025 static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
1026 struct sysrq_key_op *remove_op_p)
1028 int retval;
1029 unsigned long flags;
1031 spin_lock_irqsave(&sysrq_key_table_lock, flags);
1032 if (__sysrq_get_key_op(key) == remove_op_p) {
1033 __sysrq_put_key_op(key, insert_op_p);
1034 retval = 0;
1035 } else {
1036 retval = -1;
1038 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
1039 return retval;
1042 int register_sysrq_key(int key, struct sysrq_key_op *op_p)
1044 return __sysrq_swap_key_ops(key, op_p, NULL);
1046 EXPORT_SYMBOL(register_sysrq_key);
1048 int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
1050 return __sysrq_swap_key_ops(key, NULL, op_p);
1052 EXPORT_SYMBOL(unregister_sysrq_key);
1054 #ifdef CONFIG_PROC_FS
1056 * writing 'C' to /proc/sysrq-trigger is like sysrq-C
1058 static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
1059 size_t count, loff_t *ppos)
1061 if (count) {
1062 char c;
1064 if (get_user(c, buf))
1065 return -EFAULT;
1066 __handle_sysrq(c, false);
1069 return count;
1072 static const struct file_operations proc_sysrq_trigger_operations = {
1073 .write = write_sysrq_trigger,
1074 .llseek = noop_llseek,
1077 static void sysrq_init_procfs(void)
1079 if (!proc_create("sysrq-trigger", S_IWUSR, NULL,
1080 &proc_sysrq_trigger_operations))
1081 pr_err("Failed to register proc interface\n");
1084 #else
1086 static inline void sysrq_init_procfs(void)
1090 #endif /* CONFIG_PROC_FS */
1092 static int __init sysrq_init(void)
1094 sysrq_init_procfs();
1096 if (sysrq_on())
1097 sysrq_register_handler();
1099 return 0;
1101 module_init(sysrq_init);