1/*
2 *  Unified handling of special chars.
3 *
4 *    Copyright IBM Corp. 2001
5 *    Author(s): Fritz Elfert <felfert@millenux.com> <elfert@de.ibm.com>
6 *
7 */
8
9#include <linux/stddef.h>
10#include <asm/errno.h>
11#include <linux/sysrq.h>
12#include <linux/ctype.h>
13
14#include "ctrlchar.h"
15
16#ifdef CONFIG_MAGIC_SYSRQ
17static int ctrlchar_sysrq_key;
18
19static void
20ctrlchar_handle_sysrq(struct work_struct *work)
21{
22	handle_sysrq(ctrlchar_sysrq_key);
23}
24
25static DECLARE_WORK(ctrlchar_work, ctrlchar_handle_sysrq);
26#endif
27
28
29/**
30 * Check for special chars at start of input.
31 *
32 * @param buf Console input buffer.
33 * @param len Length of valid data in buffer.
34 * @param tty The tty struct for this console.
35 * @return CTRLCHAR_NONE, if nothing matched,
36 *         CTRLCHAR_SYSRQ, if sysrq was encountered
37 *         otherwise char to be inserted logically or'ed
38 *         with CTRLCHAR_CTRL
39 */
40unsigned int
41ctrlchar_handle(const unsigned char *buf, int len, struct tty_struct *tty)
42{
43	if ((len < 2) || (len > 3))
44		return CTRLCHAR_NONE;
45
46	/* hat is 0xb1 in codepage 037 (US etc.) and thus */
47	/* converted to 0x5e in ascii ('^') */
48	if ((buf[0] != '^') && (buf[0] != '\252'))
49		return CTRLCHAR_NONE;
50
51#ifdef CONFIG_MAGIC_SYSRQ
52	/* racy */
53	if (len == 3 && buf[1] == '-') {
54		ctrlchar_sysrq_key = buf[2];
55		schedule_work(&ctrlchar_work);
56		return CTRLCHAR_SYSRQ;
57	}
58#endif
59
60	if (len != 2)
61		return CTRLCHAR_NONE;
62
63	switch (tolower(buf[1])) {
64	case 'c':
65		return INTR_CHAR(tty) | CTRLCHAR_CTRL;
66	case 'd':
67		return EOF_CHAR(tty)  | CTRLCHAR_CTRL;
68	case 'z':
69		return SUSP_CHAR(tty) | CTRLCHAR_CTRL;
70	}
71	return CTRLCHAR_NONE;
72}
73