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