blob: e4bfcfe8550ba91cc7515c3d3bd46bd7e39ba325 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2000, 2001 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6#include "linux/config.h"
7#include "linux/posix_types.h"
8#include "linux/tty.h"
9#include "linux/tty_flip.h"
10#include "linux/types.h"
11#include "linux/major.h"
12#include "linux/kdev_t.h"
13#include "linux/console.h"
14#include "linux/string.h"
15#include "linux/sched.h"
16#include "linux/list.h"
17#include "linux/init.h"
18#include "linux/interrupt.h"
19#include "linux/slab.h"
20#include "linux/hardirq.h"
21#include "asm/current.h"
22#include "asm/irq.h"
23#include "stdio_console.h"
24#include "line.h"
25#include "chan_kern.h"
26#include "user_util.h"
27#include "kern_util.h"
28#include "irq_user.h"
29#include "mconsole_kern.h"
30#include "init.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#define MAX_TTYS (16)
33
34/* ----------------------------------------------------------------------------- */
35
36/* Referenced only by tty_driver below - presumably it's locked correctly
37 * by the tty driver.
38 */
39
40static struct tty_driver *console_driver;
41
42void stdio_announce(char *dev_name, int dev)
43{
44 printk(KERN_INFO "Virtual console %d assigned device '%s'\n", dev,
45 dev_name);
46}
47
48static struct chan_opts opts = {
49 .announce = stdio_announce,
50 .xterm_title = "Virtual Console #%d",
51 .raw = 1,
52 .tramp_stack = 0,
53 .in_kernel = 1,
54};
55
56static int con_config(char *str);
57static int con_get_config(char *dev, char *str, int size, char **error_out);
Jeff Dike29d56cf2005-06-25 14:55:25 -070058static int con_remove(int n);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60static struct line_driver driver = {
61 .name = "UML console",
62 .device_name = "tty",
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 .major = TTY_MAJOR,
64 .minor_start = 0,
65 .type = TTY_DRIVER_TYPE_CONSOLE,
66 .subtype = SYSTEM_TYPE_CONSOLE,
67 .read_irq = CONSOLE_IRQ,
68 .read_irq_name = "console",
69 .write_irq = CONSOLE_WRITE_IRQ,
70 .write_irq_name = "console-write",
71 .symlink_from = "ttys",
72 .symlink_to = "vc",
73 .mc = {
74 .name = "con",
75 .config = con_config,
76 .get_config = con_get_config,
Jeff Diked50084a2006-01-06 00:18:50 -080077 .id = line_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 .remove = con_remove,
79 },
80};
81
82static struct lines console_lines = LINES_INIT(MAX_TTYS);
83
84/* The array is initialized by line_init, which is an initcall. The
85 * individual elements are protected by individual semaphores.
86 */
87struct line vts[MAX_TTYS] = { LINE_INIT(CONFIG_CON_ZERO_CHAN, &driver),
Jeff Diked50084a2006-01-06 00:18:50 -080088 [ 1 ... MAX_TTYS - 1 ] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 LINE_INIT(CONFIG_CON_CHAN, &driver) };
90
91static int con_config(char *str)
92{
Jeff Dike1f801712006-01-06 00:18:55 -080093 return line_config(vts, ARRAY_SIZE(vts), str, &opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
96static int con_get_config(char *dev, char *str, int size, char **error_out)
97{
Jeff Dike0834cc72006-01-06 00:18:51 -080098 return line_get_config(dev, vts, ARRAY_SIZE(vts), str, size, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
Jeff Dike29d56cf2005-06-25 14:55:25 -0700101static int con_remove(int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Jeff Dike0834cc72006-01-06 00:18:51 -0800103 return line_remove(vts, ARRAY_SIZE(vts), n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106static int con_open(struct tty_struct *tty, struct file *filp)
107{
Jeff Dike1f801712006-01-06 00:18:55 -0800108 return line_open(vts, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Jeff Dike730760e2006-09-29 01:58:50 -0700111/* Set in an initcall, checked in an exitcall */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static int con_init_done = 0;
113
Jeff Dike5e7672e2006-09-27 01:50:33 -0700114static const struct tty_operations console_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .open = con_open,
116 .close = line_close,
117 .write = line_write,
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700118 .put_char = line_put_char,
Jeff Diked50084a2006-01-06 00:18:50 -0800119 .write_room = line_write_room,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 .chars_in_buffer = line_chars_in_buffer,
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700121 .flush_buffer = line_flush_buffer,
122 .flush_chars = line_flush_chars,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 .set_termios = line_set_termios,
124 .ioctl = line_ioctl,
Jeff Dikee4dcee82006-01-06 00:18:58 -0800125 .throttle = line_throttle,
126 .unthrottle = line_unthrottle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
129static void uml_console_write(struct console *console, const char *string,
Jeff Diked50084a2006-01-06 00:18:50 -0800130 unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct line *line = &vts[console->index];
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700133 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700135 spin_lock_irqsave(&line->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 console_write_chan(&line->chan_list, string, len);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700137 spin_unlock_irqrestore(&line->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140static struct tty_driver *uml_console_device(struct console *c, int *index)
141{
142 *index = c->index;
143 return console_driver;
144}
145
146static int uml_console_setup(struct console *co, char *options)
147{
148 struct line *line = &vts[co->index];
149
Jeff Diked50084a2006-01-06 00:18:50 -0800150 return console_open_chan(line, co, &opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153static struct console stdiocons = {
154 .name = "tty",
155 .write = uml_console_write,
156 .device = uml_console_device,
157 .setup = uml_console_setup,
158 .flags = CON_PRINTBUFFER,
159 .index = -1,
Jeff Diked50084a2006-01-06 00:18:50 -0800160 .data = &vts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
162
163int stdio_init(void)
164{
165 char *new_title;
166
167 console_driver = line_register_devfs(&console_lines, &driver,
168 &console_ops, vts,
169 ARRAY_SIZE(vts));
Jeff Diked50084a2006-01-06 00:18:50 -0800170 if (console_driver == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return -1;
172 printk(KERN_INFO "Initialized stdio console driver\n");
173
Jeff Dike1f801712006-01-06 00:18:55 -0800174 lines_init(vts, ARRAY_SIZE(vts), &opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 new_title = add_xterm_umid(opts.xterm_title);
177 if(new_title != NULL)
178 opts.xterm_title = new_title;
179
180 con_init_done = 1;
181 register_console(&stdiocons);
Jeff Diked50084a2006-01-06 00:18:50 -0800182 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184late_initcall(stdio_init);
185
186static void console_exit(void)
187{
188 if (!con_init_done)
189 return;
Jeff Dike0834cc72006-01-06 00:18:51 -0800190 close_lines(vts, ARRAY_SIZE(vts));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192__uml_exitcall(console_exit);
193
194static int console_chan_setup(char *str)
195{
Jeff Diked571cd12006-01-06 00:18:53 -0800196 return line_setup(vts, ARRAY_SIZE(vts), str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198__setup("con", console_chan_setup);
199__channel_help(console_chan_setup, "con");