blob: 41eae2e8fb652c07e1ace961ba5517a4a37f5266 [file] [log] [blame]
Alex Dewardbddf422019-08-25 10:49:16 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Copyright (C) 2000, 2002 Jeff Dike (jdike@karaya.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
Al Viro37185b32012-10-08 03:27:32 +01006#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>
Al Viro510c72a32011-08-18 20:08:29 +010015#include "chan.h"
Al Viro37185b32012-10-08 03:27:32 +010016#include <init.h>
17#include <irq_user.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "mconsole_kern.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Jeff Dike5e7672e2006-09-27 01:50:33 -070020static const int ssl_version = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#define NR_PORTS 64
23
Paolo 'Blaisorblade' Giarrusso42947cb2006-02-01 03:06:29 -080024static void ssl_announce(char *dev_name, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
26 printk(KERN_INFO "Serial line %d assigned device '%s'\n", dev,
27 dev_name);
28}
29
Jeff Dikea52f3622007-02-10 01:44:06 -080030/* Almost const, except that xterm_title may be changed in an initcall */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static struct chan_opts opts = {
32 .announce = ssl_announce,
33 .xterm_title = "Serial Line #%d",
34 .raw = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070035};
36
Jeff Dikef28169d2007-02-10 01:43:53 -080037static int ssl_config(char *str, char **error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static int ssl_get_config(char *dev, char *str, int size, char **error_out);
Jeff Dikef28169d2007-02-10 01:43:53 -080039static int ssl_remove(int n, char **error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Jeff Diked5c9ffc2007-02-10 01:44:08 -080041
42/* Const, except for .mc.list */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static struct line_driver driver = {
44 .name = "UML serial line",
45 .device_name = "ttyS",
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 .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",
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 .mc = {
Jeff Dike84f48d42007-02-10 01:44:01 -080055 .list = LIST_HEAD_INIT(driver.mc.list),
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 .name = "ssl",
57 .config = ssl_config,
58 .get_config = ssl_get_config,
Jeff Diked50084a2006-01-06 00:18:50 -080059 .id = line_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 .remove = ssl_remove,
61 },
62};
63
Jeff Diked5c9ffc2007-02-10 01:44:08 -080064/* The array is initialized by line_init, at initcall time. The
65 * elements are locked individually as needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 */
Al Viro43574c12011-09-09 17:25:00 -040067static char *conf[NR_PORTS];
68static char *def_conf = CONFIG_SSL_CHAN;
69static struct line serial_lines[NR_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Jeff Dikef28169d2007-02-10 01:43:53 -080071static int ssl_config(char *str, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Jeff Dikef28169d2007-02-10 01:43:53 -080073 return line_config(serial_lines, ARRAY_SIZE(serial_lines), str, &opts,
74 error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
77static int ssl_get_config(char *dev, char *str, int size, char **error_out)
78{
Jeff Dike0834cc72006-01-06 00:18:51 -080079 return line_get_config(dev, serial_lines, ARRAY_SIZE(serial_lines), str,
80 size, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Jeff Dikef28169d2007-02-10 01:43:53 -080083static int ssl_remove(int n, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Jeff Dikef28169d2007-02-10 01:43:53 -080085 return line_remove(serial_lines, ARRAY_SIZE(serial_lines), n,
86 error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Richard Weinberger79e02732012-06-04 21:57:24 +020089static int ssl_install(struct tty_driver *driver, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Richard Weinberger79e02732012-06-04 21:57:24 +020091 return line_install(driver, tty, &serial_lines[tty->index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Jeff Dike5e7672e2006-09-27 01:50:33 -070094static const struct tty_operations ssl_ops = {
Richard Weinberger79e02732012-06-04 21:57:24 +020095 .open = line_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 .close = line_close,
97 .write = line_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 .write_room = line_write_room,
99 .chars_in_buffer = line_chars_in_buffer,
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700100 .flush_buffer = line_flush_buffer,
101 .flush_chars = line_flush_chars,
Jeff Dikee4dcee82006-01-06 00:18:58 -0800102 .throttle = line_throttle,
103 .unthrottle = line_unthrottle,
Richard Weinberger79e02732012-06-04 21:57:24 +0200104 .install = ssl_install,
Richard Weinberger79e02732012-06-04 21:57:24 +0200105 .hangup = line_hangup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
108/* Changed by ssl_init and referenced by ssl_exit, which are both serialized
109 * by being an initcall and exitcall, respectively.
110 */
111static int ssl_init_done = 0;
112
113static void ssl_console_write(struct console *c, const char *string,
114 unsigned len)
115{
116 struct line *line = &serial_lines[c->index];
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700117 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700119 spin_lock_irqsave(&line->lock, flags);
Al Virobed5e392011-09-08 10:49:34 -0400120 console_write_chan(line->chan_out, string, len);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700121 spin_unlock_irqrestore(&line->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124static struct tty_driver *ssl_console_device(struct console *c, int *index)
125{
126 *index = c->index;
Al Virocfe6b7c2011-09-09 19:45:42 -0400127 return driver.driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130static int ssl_console_setup(struct console *co, char *options)
131{
132 struct line *line = &serial_lines[co->index];
133
Jeff Dikea52f3622007-02-10 01:44:06 -0800134 return console_open_chan(line, co);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Jeff Diked5c9ffc2007-02-10 01:44:08 -0800137/* No locking for register_console call - relies on single-threaded initcalls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static struct console ssl_cons = {
139 .name = "ttyS",
140 .write = ssl_console_write,
141 .device = ssl_console_device,
142 .setup = ssl_console_setup,
Paolo 'Blaisorblade' Giarrussob5337882007-03-07 20:41:11 -0800143 .flags = CON_PRINTBUFFER|CON_ANYTIME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 .index = -1,
145};
146
Paolo 'Blaisorblade' Giarrusso42947cb2006-02-01 03:06:29 -0800147static int ssl_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 char *new_title;
Al Virocfe6b7c2011-09-09 19:45:42 -0400150 int err;
Al Viro43574c12011-09-09 17:25:00 -0400151 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Jeff Diked50084a2006-01-06 00:18:50 -0800153 printk(KERN_INFO "Initializing software serial port version %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 ssl_version);
Al Viro43574c12011-09-09 17:25:00 -0400155
Al Virocfe6b7c2011-09-09 19:45:42 -0400156 err = register_lines(&driver, &ssl_ops, serial_lines,
Jeff Diked5c9ffc2007-02-10 01:44:08 -0800157 ARRAY_SIZE(serial_lines));
Al Virocfe6b7c2011-09-09 19:45:42 -0400158 if (err)
159 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 new_title = add_xterm_umid(opts.xterm_title);
162 if (new_title != NULL)
163 opts.xterm_title = new_title;
164
Al Viro04292b22011-09-09 20:07:05 -0400165 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 }
Davide Brini57ac8952007-05-06 14:51:16 -0700174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 ssl_init_done = 1;
176 register_console(&ssl_cons);
Jeff Diked50084a2006-01-06 00:18:50 -0800177 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179late_initcall(ssl_init);
180
181static void ssl_exit(void)
182{
183 if (!ssl_init_done)
184 return;
Jeff Dike0834cc72006-01-06 00:18:51 -0800185 close_lines(serial_lines, ARRAY_SIZE(serial_lines));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187__uml_exitcall(ssl_exit);
188
189static int ssl_chan_setup(char *str)
190{
Al Viro43574c12011-09-09 17:25:00 -0400191 line_setup(conf, NR_PORTS, &def_conf, str, "serial line");
Jeff Dikef28169d2007-02-10 01:43:53 -0800192 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195__setup("ssl", ssl_chan_setup);
196__channel_help(ssl_chan_setup, "ssl");
Johannes Bergf1dc67d2019-05-25 00:08:43 +0200197
198static 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");