^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) * Copyright (C) 2000, 2002 Jeff Dike (jdike@karaya.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/tty_driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/major.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/console.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <asm/termbits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <asm/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "chan.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <irq_user.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "mconsole_kern.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static const int ssl_version = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define NR_PORTS 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static void ssl_announce(char *dev_name, int dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) printk(KERN_INFO "Serial line %d assigned device '%s'\n", dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) dev_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /* Almost const, except that xterm_title may be changed in an initcall */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static struct chan_opts opts = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) .announce = ssl_announce,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) .xterm_title = "Serial Line #%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) .raw = 1,
^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) static int ssl_config(char *str, char **error_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static int ssl_get_config(char *dev, char *str, int size, char **error_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static int ssl_remove(int n, char **error_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /* Const, except for .mc.list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static struct line_driver driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) .name = "UML serial line",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) .device_name = "ttyS",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) .major = TTY_MAJOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) .minor_start = 64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) .type = TTY_DRIVER_TYPE_SERIAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) .subtype = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) .read_irq = SSL_IRQ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) .read_irq_name = "ssl",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) .write_irq = SSL_WRITE_IRQ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) .write_irq_name = "ssl-write",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) .mc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) .list = LIST_HEAD_INIT(driver.mc.list),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) .name = "ssl",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) .config = ssl_config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) .get_config = ssl_get_config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) .id = line_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) .remove = ssl_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /* The array is initialized by line_init, at initcall time. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * elements are locked individually as needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static char *conf[NR_PORTS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static char *def_conf = CONFIG_SSL_CHAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static struct line serial_lines[NR_PORTS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static int ssl_config(char *str, char **error_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return line_config(serial_lines, ARRAY_SIZE(serial_lines), str, &opts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) error_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static int ssl_get_config(char *dev, char *str, int size, char **error_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return line_get_config(dev, serial_lines, ARRAY_SIZE(serial_lines), str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) size, error_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static int ssl_remove(int n, char **error_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return line_remove(serial_lines, ARRAY_SIZE(serial_lines), n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) error_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static int ssl_install(struct tty_driver *driver, struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return line_install(driver, tty, &serial_lines[tty->index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static const struct tty_operations ssl_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) .open = line_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) .close = line_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) .write = line_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) .write_room = line_write_room,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) .chars_in_buffer = line_chars_in_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) .flush_buffer = line_flush_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) .flush_chars = line_flush_chars,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) .set_termios = line_set_termios,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) .throttle = line_throttle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) .unthrottle = line_unthrottle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) .install = ssl_install,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) .hangup = line_hangup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /* Changed by ssl_init and referenced by ssl_exit, which are both serialized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * by being an initcall and exitcall, respectively.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static int ssl_init_done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static void ssl_console_write(struct console *c, const char *string,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) unsigned len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct line *line = &serial_lines[c->index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) spin_lock_irqsave(&line->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) console_write_chan(line->chan_out, string, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) spin_unlock_irqrestore(&line->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static struct tty_driver *ssl_console_device(struct console *c, int *index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) *index = c->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return driver.driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static int ssl_console_setup(struct console *co, char *options)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct line *line = &serial_lines[co->index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return console_open_chan(line, co);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /* No locking for register_console call - relies on single-threaded initcalls */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static struct console ssl_cons = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) .name = "ttyS",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) .write = ssl_console_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) .device = ssl_console_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) .setup = ssl_console_setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) .flags = CON_PRINTBUFFER|CON_ANYTIME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) .index = -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static int ssl_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) char *new_title;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) printk(KERN_INFO "Initializing software serial port version %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) ssl_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) err = register_lines(&driver, &ssl_ops, serial_lines,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) ARRAY_SIZE(serial_lines));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) new_title = add_xterm_umid(opts.xterm_title);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (new_title != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) opts.xterm_title = new_title;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) for (i = 0; i < NR_PORTS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) char *error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) char *s = conf[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (!s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) s = def_conf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (setup_one_line(serial_lines, i, s, &opts, &error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) printk(KERN_ERR "setup_one_line failed for "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) "device %d : %s\n", i, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) ssl_init_done = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) register_console(&ssl_cons);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) late_initcall(ssl_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static void ssl_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (!ssl_init_done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) close_lines(serial_lines, ARRAY_SIZE(serial_lines));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) __uml_exitcall(ssl_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static int ssl_chan_setup(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) line_setup(conf, NR_PORTS, &def_conf, str, "serial line");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) __setup("ssl", ssl_chan_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) __channel_help(ssl_chan_setup, "ssl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static int ssl_non_raw_setup(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) opts.raw = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) __setup("ssl-non-raw", ssl_non_raw_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) __channel_help(ssl_non_raw_setup, "set serial lines to non-raw mode");