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