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