xref: /openbmc/linux/arch/um/drivers/ssl.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1dbddf429SAlex Dewar // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Copyright (C) 2000, 2002 Jeff Dike (jdike@karaya.com)
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
637185b33SAl Viro #include <linux/fs.h>
737185b33SAl Viro #include <linux/tty.h>
837185b33SAl Viro #include <linux/tty_driver.h>
937185b33SAl Viro #include <linux/major.h>
1037185b33SAl Viro #include <linux/mm.h>
1137185b33SAl Viro #include <linux/init.h>
1237185b33SAl Viro #include <linux/console.h>
1337185b33SAl Viro #include <asm/termbits.h>
1437185b33SAl Viro #include <asm/irq.h>
15510c72a3SAl Viro #include "chan.h"
1637185b33SAl Viro #include <init.h>
1737185b33SAl Viro #include <irq_user.h>
181da177e4SLinus Torvalds #include "mconsole_kern.h"
191da177e4SLinus Torvalds 
205e7672ecSJeff Dike static const int ssl_version = 1;
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #define NR_PORTS 64
231da177e4SLinus Torvalds 
ssl_announce(char * dev_name,int dev)2442947cb9SPaolo 'Blaisorblade' Giarrusso static void ssl_announce(char *dev_name, int dev)
251da177e4SLinus Torvalds {
261da177e4SLinus Torvalds 	printk(KERN_INFO "Serial line %d assigned device '%s'\n", dev,
271da177e4SLinus Torvalds 	       dev_name);
281da177e4SLinus Torvalds }
291da177e4SLinus Torvalds 
30a52f362fSJeff Dike /* Almost const, except that xterm_title may be changed in an initcall */
311da177e4SLinus Torvalds static struct chan_opts opts = {
321da177e4SLinus Torvalds 	.announce 	= ssl_announce,
331da177e4SLinus Torvalds 	.xterm_title	= "Serial Line #%d",
341da177e4SLinus Torvalds 	.raw		= 1,
351da177e4SLinus Torvalds };
361da177e4SLinus Torvalds 
37f28169d2SJeff Dike static int ssl_config(char *str, char **error_out);
381da177e4SLinus Torvalds static int ssl_get_config(char *dev, char *str, int size, char **error_out);
39f28169d2SJeff Dike static int ssl_remove(int n, char **error_out);
401da177e4SLinus Torvalds 
41d5c9ffc6SJeff Dike 
42d5c9ffc6SJeff Dike /* Const, except for .mc.list */
431da177e4SLinus Torvalds static struct line_driver driver = {
441da177e4SLinus Torvalds 	.name 			= "UML serial line",
451da177e4SLinus Torvalds 	.device_name 		= "ttyS",
461da177e4SLinus Torvalds 	.major 			= TTY_MAJOR,
471da177e4SLinus Torvalds 	.minor_start 		= 64,
481da177e4SLinus Torvalds 	.type 		 	= TTY_DRIVER_TYPE_SERIAL,
491da177e4SLinus Torvalds 	.subtype 	 	= 0,
501da177e4SLinus Torvalds 	.read_irq_name 		= "ssl",
511da177e4SLinus Torvalds 	.write_irq_name 	= "ssl-write",
521da177e4SLinus Torvalds 	.mc  = {
5384f48d4fSJeff Dike 		.list		= LIST_HEAD_INIT(driver.mc.list),
541da177e4SLinus Torvalds 		.name  		= "ssl",
551da177e4SLinus Torvalds 		.config 	= ssl_config,
561da177e4SLinus Torvalds 		.get_config 	= ssl_get_config,
5729d56cfeSJeff Dike 		.id		= line_id,
581da177e4SLinus Torvalds 		.remove 	= ssl_remove,
591da177e4SLinus Torvalds 	},
601da177e4SLinus Torvalds };
611da177e4SLinus Torvalds 
62d5c9ffc6SJeff Dike /* The array is initialized by line_init, at initcall time.  The
63d5c9ffc6SJeff Dike  * elements are locked individually as needed.
641da177e4SLinus Torvalds  */
6543574c1aSAl Viro static char *conf[NR_PORTS];
6643574c1aSAl Viro static char *def_conf = CONFIG_SSL_CHAN;
6743574c1aSAl Viro static struct line serial_lines[NR_PORTS];
681da177e4SLinus Torvalds 
ssl_config(char * str,char ** error_out)69f28169d2SJeff Dike static int ssl_config(char *str, char **error_out)
701da177e4SLinus Torvalds {
71f28169d2SJeff Dike 	return line_config(serial_lines, ARRAY_SIZE(serial_lines), str, &opts,
72f28169d2SJeff Dike 			   error_out);
731da177e4SLinus Torvalds }
741da177e4SLinus Torvalds 
ssl_get_config(char * dev,char * str,int size,char ** error_out)751da177e4SLinus Torvalds static int ssl_get_config(char *dev, char *str, int size, char **error_out)
761da177e4SLinus Torvalds {
770834cc77SJeff Dike 	return line_get_config(dev, serial_lines, ARRAY_SIZE(serial_lines), str,
780834cc77SJeff Dike 			       size, error_out);
791da177e4SLinus Torvalds }
801da177e4SLinus Torvalds 
ssl_remove(int n,char ** error_out)81f28169d2SJeff Dike static int ssl_remove(int n, char **error_out)
821da177e4SLinus Torvalds {
83f28169d2SJeff Dike 	return line_remove(serial_lines, ARRAY_SIZE(serial_lines), n,
84f28169d2SJeff Dike 			   error_out);
851da177e4SLinus Torvalds }
861da177e4SLinus Torvalds 
ssl_install(struct tty_driver * driver,struct tty_struct * tty)8779e0273dSRichard Weinberger static int ssl_install(struct tty_driver *driver, struct tty_struct *tty)
881da177e4SLinus Torvalds {
8979e0273dSRichard Weinberger 	return line_install(driver, tty, &serial_lines[tty->index]);
901da177e4SLinus Torvalds }
911da177e4SLinus Torvalds 
925e7672ecSJeff Dike static const struct tty_operations ssl_ops = {
9379e0273dSRichard Weinberger 	.open 	 		= line_open,
941da177e4SLinus Torvalds 	.close 	 		= line_close,
951da177e4SLinus Torvalds 	.write 	 		= line_write,
961da177e4SLinus Torvalds 	.write_room		= line_write_room,
971da177e4SLinus Torvalds 	.chars_in_buffer 	= line_chars_in_buffer,
98b97b77ccSPaolo 'Blaisorblade' Giarrusso 	.flush_buffer 		= line_flush_buffer,
99b97b77ccSPaolo 'Blaisorblade' Giarrusso 	.flush_chars 		= line_flush_chars,
100e4dcee80SJeff Dike 	.throttle 		= line_throttle,
101e4dcee80SJeff Dike 	.unthrottle 		= line_unthrottle,
10279e0273dSRichard Weinberger 	.install		= ssl_install,
10379e0273dSRichard Weinberger 	.hangup			= line_hangup,
1041da177e4SLinus Torvalds };
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds /* Changed by ssl_init and referenced by ssl_exit, which are both serialized
1071da177e4SLinus Torvalds  * by being an initcall and exitcall, respectively.
1081da177e4SLinus Torvalds  */
109*4dc5a328SXin Gao static int ssl_init_done;
1101da177e4SLinus Torvalds 
ssl_console_write(struct console * c,const char * string,unsigned len)1111da177e4SLinus Torvalds static void ssl_console_write(struct console *c, const char *string,
1121da177e4SLinus Torvalds 			      unsigned len)
1131da177e4SLinus Torvalds {
1141da177e4SLinus Torvalds 	struct line *line = &serial_lines[c->index];
115b97b77ccSPaolo 'Blaisorblade' Giarrusso 	unsigned long flags;
1161da177e4SLinus Torvalds 
117b97b77ccSPaolo 'Blaisorblade' Giarrusso 	spin_lock_irqsave(&line->lock, flags);
118bed5e39cSAl Viro 	console_write_chan(line->chan_out, string, len);
119b97b77ccSPaolo 'Blaisorblade' Giarrusso 	spin_unlock_irqrestore(&line->lock, flags);
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds 
ssl_console_device(struct console * c,int * index)1221da177e4SLinus Torvalds static struct tty_driver *ssl_console_device(struct console *c, int *index)
1231da177e4SLinus Torvalds {
1241da177e4SLinus Torvalds 	*index = c->index;
125cfe6b7c7SAl Viro 	return driver.driver;
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
ssl_console_setup(struct console * co,char * options)1281da177e4SLinus Torvalds static int ssl_console_setup(struct console *co, char *options)
1291da177e4SLinus Torvalds {
1301da177e4SLinus Torvalds 	struct line *line = &serial_lines[co->index];
1311da177e4SLinus Torvalds 
132a52f362fSJeff Dike 	return console_open_chan(line, co);
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds 
135d5c9ffc6SJeff Dike /* No locking for register_console call - relies on single-threaded initcalls */
1361da177e4SLinus Torvalds static struct console ssl_cons = {
1371da177e4SLinus Torvalds 	.name		= "ttyS",
1381da177e4SLinus Torvalds 	.write		= ssl_console_write,
1391da177e4SLinus Torvalds 	.device		= ssl_console_device,
1401da177e4SLinus Torvalds 	.setup		= ssl_console_setup,
141b5337885SPaolo 'Blaisorblade' Giarrusso 	.flags		= CON_PRINTBUFFER|CON_ANYTIME,
1421da177e4SLinus Torvalds 	.index		= -1,
1431da177e4SLinus Torvalds };
1441da177e4SLinus Torvalds 
ssl_init(void)14542947cb9SPaolo 'Blaisorblade' Giarrusso static int ssl_init(void)
1461da177e4SLinus Torvalds {
1471da177e4SLinus Torvalds 	char *new_title;
148cfe6b7c7SAl Viro 	int err;
14943574c1aSAl Viro 	int i;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	printk(KERN_INFO "Initializing software serial port version %d\n",
1521da177e4SLinus Torvalds 	       ssl_version);
15343574c1aSAl Viro 
154cfe6b7c7SAl Viro 	err = register_lines(&driver, &ssl_ops, serial_lines,
155d50084a2SJeff Dike 				    ARRAY_SIZE(serial_lines));
156cfe6b7c7SAl Viro 	if (err)
157cfe6b7c7SAl Viro 		return err;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	new_title = add_xterm_umid(opts.xterm_title);
1601da177e4SLinus Torvalds 	if (new_title != NULL)
1611da177e4SLinus Torvalds 		opts.xterm_title = new_title;
1621da177e4SLinus Torvalds 
16304292b2cSAl Viro 	for (i = 0; i < NR_PORTS; i++) {
16404292b2cSAl Viro 		char *error;
16504292b2cSAl Viro 		char *s = conf[i];
16604292b2cSAl Viro 		if (!s)
16704292b2cSAl Viro 			s = def_conf;
16804292b2cSAl Viro 		if (setup_one_line(serial_lines, i, s, &opts, &error))
16904292b2cSAl Viro 			printk(KERN_ERR "setup_one_line failed for "
17004292b2cSAl Viro 			       "device %d : %s\n", i, error);
17104292b2cSAl Viro 	}
17257ac895aSDavide Brini 
1731da177e4SLinus Torvalds 	ssl_init_done = 1;
1741da177e4SLinus Torvalds 	register_console(&ssl_cons);
175d50084a2SJeff Dike 	return 0;
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds late_initcall(ssl_init);
1781da177e4SLinus Torvalds 
ssl_exit(void)1791da177e4SLinus Torvalds static void ssl_exit(void)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	if (!ssl_init_done)
1821da177e4SLinus Torvalds 		return;
1830834cc77SJeff Dike 	close_lines(serial_lines, ARRAY_SIZE(serial_lines));
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds __uml_exitcall(ssl_exit);
1861da177e4SLinus Torvalds 
ssl_chan_setup(char * str)1871da177e4SLinus Torvalds static int ssl_chan_setup(char *str)
1881da177e4SLinus Torvalds {
18943574c1aSAl Viro 	line_setup(conf, NR_PORTS, &def_conf, str, "serial line");
190f28169d2SJeff Dike 	return 1;
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds __setup("ssl", ssl_chan_setup);
1941da177e4SLinus Torvalds __channel_help(ssl_chan_setup, "ssl");
195f1dc67d0SJohannes Berg 
ssl_non_raw_setup(char * str)196f1dc67d0SJohannes Berg static int ssl_non_raw_setup(char *str)
197f1dc67d0SJohannes Berg {
198f1dc67d0SJohannes Berg 	opts.raw = 0;
199f1dc67d0SJohannes Berg 	return 1;
200f1dc67d0SJohannes Berg }
201f1dc67d0SJohannes Berg __setup("ssl-non-raw", ssl_non_raw_setup);
202f1dc67d0SJohannes Berg __channel_help(ssl_non_raw_setup, "set serial lines to non-raw mode");
203