stdio_console.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 "user_util.h"
  25. #include "kern_util.h"
  26. #include "irq_user.h"
  27. #include "mconsole_kern.h"
  28. #include "init.h"
  29. #define MAX_TTYS (16)
  30. /* ----------------------------------------------------------------------------- */
  31. /* Referenced only by tty_driver below - presumably it's locked correctly
  32. * by the tty driver.
  33. */
  34. static struct tty_driver *console_driver;
  35. void stdio_announce(char *dev_name, int dev)
  36. {
  37. printk(KERN_INFO "Virtual console %d assigned device '%s'\n", dev,
  38. dev_name);
  39. }
  40. static struct chan_opts opts = {
  41. .announce = stdio_announce,
  42. .xterm_title = "Virtual Console #%d",
  43. .raw = 1,
  44. .tramp_stack = 0,
  45. .in_kernel = 1,
  46. };
  47. static int con_config(char *str);
  48. static int con_get_config(char *dev, char *str, int size, char **error_out);
  49. static int con_remove(int n);
  50. static struct line_driver driver = {
  51. .name = "UML console",
  52. .device_name = "tty",
  53. .major = TTY_MAJOR,
  54. .minor_start = 0,
  55. .type = TTY_DRIVER_TYPE_CONSOLE,
  56. .subtype = SYSTEM_TYPE_CONSOLE,
  57. .read_irq = CONSOLE_IRQ,
  58. .read_irq_name = "console",
  59. .write_irq = CONSOLE_WRITE_IRQ,
  60. .write_irq_name = "console-write",
  61. .symlink_from = "ttys",
  62. .symlink_to = "vc",
  63. .mc = {
  64. .name = "con",
  65. .config = con_config,
  66. .get_config = con_get_config,
  67. .id = line_id,
  68. .remove = con_remove,
  69. },
  70. };
  71. static struct lines console_lines = LINES_INIT(MAX_TTYS);
  72. /* The array is initialized by line_init, which is an initcall. The
  73. * individual elements are protected by individual semaphores.
  74. */
  75. struct line vts[MAX_TTYS] = { LINE_INIT(CONFIG_CON_ZERO_CHAN, &driver),
  76. [ 1 ... MAX_TTYS - 1 ] =
  77. LINE_INIT(CONFIG_CON_CHAN, &driver) };
  78. static int con_config(char *str)
  79. {
  80. return line_config(vts, ARRAY_SIZE(vts), str, &opts);
  81. }
  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. static int con_remove(int n)
  87. {
  88. return line_remove(vts, ARRAY_SIZE(vts), n);
  89. }
  90. static int con_open(struct tty_struct *tty, struct file *filp)
  91. {
  92. return line_open(vts, tty);
  93. }
  94. /* Set in an initcall, checked in an exitcall */
  95. static int con_init_done = 0;
  96. static const struct tty_operations console_ops = {
  97. .open = con_open,
  98. .close = line_close,
  99. .write = line_write,
  100. .put_char = line_put_char,
  101. .write_room = line_write_room,
  102. .chars_in_buffer = line_chars_in_buffer,
  103. .flush_buffer = line_flush_buffer,
  104. .flush_chars = line_flush_chars,
  105. .set_termios = line_set_termios,
  106. .ioctl = line_ioctl,
  107. .throttle = line_throttle,
  108. .unthrottle = line_unthrottle,
  109. };
  110. static void uml_console_write(struct console *console, const char *string,
  111. unsigned len)
  112. {
  113. struct line *line = &vts[console->index];
  114. unsigned long flags;
  115. spin_lock_irqsave(&line->lock, flags);
  116. console_write_chan(&line->chan_list, string, len);
  117. spin_unlock_irqrestore(&line->lock, flags);
  118. }
  119. static struct tty_driver *uml_console_device(struct console *c, int *index)
  120. {
  121. *index = c->index;
  122. return console_driver;
  123. }
  124. static int uml_console_setup(struct console *co, char *options)
  125. {
  126. struct line *line = &vts[co->index];
  127. return console_open_chan(line, co, &opts);
  128. }
  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,
  135. .index = -1,
  136. .data = &vts,
  137. };
  138. int stdio_init(void)
  139. {
  140. char *new_title;
  141. console_driver = line_register_devfs(&console_lines, &driver,
  142. &console_ops, vts,
  143. ARRAY_SIZE(vts));
  144. if (console_driver == NULL)
  145. return -1;
  146. printk(KERN_INFO "Initialized stdio console driver\n");
  147. lines_init(vts, ARRAY_SIZE(vts), &opts);
  148. new_title = add_xterm_umid(opts.xterm_title);
  149. if(new_title != NULL)
  150. opts.xterm_title = new_title;
  151. con_init_done = 1;
  152. register_console(&stdiocons);
  153. return 0;
  154. }
  155. late_initcall(stdio_init);
  156. static void console_exit(void)
  157. {
  158. if (!con_init_done)
  159. return;
  160. close_lines(vts, ARRAY_SIZE(vts));
  161. }
  162. __uml_exitcall(console_exit);
  163. static int console_chan_setup(char *str)
  164. {
  165. return line_setup(vts, ARRAY_SIZE(vts), str);
  166. }
  167. __setup("con", console_chan_setup);
  168. __channel_help(console_chan_setup, "con");