blob: 8de2deb176d716daf043e9c80afd1536043eb94b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Unified handling of special chars.
3 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Fritz Elfert <felfert@millenux.com> <elfert@de.ibm.com>
6 *
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#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
Andrew Mortone45ccc02006-12-08 15:53:52 +010020ctrlchar_handle_sysrq(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
Dmitry Torokhovf3353972010-08-17 21:15:47 -070022 handle_sysrq(ctrlchar_sysrq_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023}
24
Andrew Mortone45ccc02006-12-08 15:53:52 +010025static DECLARE_WORK(ctrlchar_work, ctrlchar_handle_sysrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#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];
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 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}