stdio_console.c 5.0 KB

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