ssl.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2000, 2002 Jeff Dike ([email protected])
  4. */
  5. #include <linux/fs.h>
  6. #include <linux/tty.h>
  7. #include <linux/tty_driver.h>
  8. #include <linux/major.h>
  9. #include <linux/mm.h>
  10. #include <linux/init.h>
  11. #include <linux/console.h>
  12. #include <asm/termbits.h>
  13. #include <asm/irq.h>
  14. #include "chan.h"
  15. #include <init.h>
  16. #include <irq_user.h>
  17. #include "mconsole_kern.h"
  18. static const int ssl_version = 1;
  19. #define NR_PORTS 64
  20. static void ssl_announce(char *dev_name, int dev)
  21. {
  22. printk(KERN_INFO "Serial line %d assigned device '%s'\n", dev,
  23. dev_name);
  24. }
  25. /* Almost const, except that xterm_title may be changed in an initcall */
  26. static struct chan_opts opts = {
  27. .announce = ssl_announce,
  28. .xterm_title = "Serial Line #%d",
  29. .raw = 1,
  30. };
  31. static int ssl_config(char *str, char **error_out);
  32. static int ssl_get_config(char *dev, char *str, int size, char **error_out);
  33. static int ssl_remove(int n, char **error_out);
  34. /* Const, except for .mc.list */
  35. static struct line_driver driver = {
  36. .name = "UML serial line",
  37. .device_name = "ttyS",
  38. .major = TTY_MAJOR,
  39. .minor_start = 64,
  40. .type = TTY_DRIVER_TYPE_SERIAL,
  41. .subtype = 0,
  42. .read_irq_name = "ssl",
  43. .write_irq_name = "ssl-write",
  44. .mc = {
  45. .list = LIST_HEAD_INIT(driver.mc.list),
  46. .name = "ssl",
  47. .config = ssl_config,
  48. .get_config = ssl_get_config,
  49. .id = line_id,
  50. .remove = ssl_remove,
  51. },
  52. };
  53. /* The array is initialized by line_init, at initcall time. The
  54. * elements are locked individually as needed.
  55. */
  56. static char *conf[NR_PORTS];
  57. static char *def_conf = CONFIG_SSL_CHAN;
  58. static struct line serial_lines[NR_PORTS];
  59. static int ssl_config(char *str, char **error_out)
  60. {
  61. return line_config(serial_lines, ARRAY_SIZE(serial_lines), str, &opts,
  62. error_out);
  63. }
  64. static int ssl_get_config(char *dev, char *str, int size, char **error_out)
  65. {
  66. return line_get_config(dev, serial_lines, ARRAY_SIZE(serial_lines), str,
  67. size, error_out);
  68. }
  69. static int ssl_remove(int n, char **error_out)
  70. {
  71. return line_remove(serial_lines, ARRAY_SIZE(serial_lines), n,
  72. error_out);
  73. }
  74. static int ssl_install(struct tty_driver *driver, struct tty_struct *tty)
  75. {
  76. return line_install(driver, tty, &serial_lines[tty->index]);
  77. }
  78. static const struct tty_operations ssl_ops = {
  79. .open = line_open,
  80. .close = line_close,
  81. .write = line_write,
  82. .write_room = line_write_room,
  83. .chars_in_buffer = line_chars_in_buffer,
  84. .flush_buffer = line_flush_buffer,
  85. .flush_chars = line_flush_chars,
  86. .throttle = line_throttle,
  87. .unthrottle = line_unthrottle,
  88. .install = ssl_install,
  89. .hangup = line_hangup,
  90. };
  91. /* Changed by ssl_init and referenced by ssl_exit, which are both serialized
  92. * by being an initcall and exitcall, respectively.
  93. */
  94. static int ssl_init_done;
  95. static void ssl_console_write(struct console *c, const char *string,
  96. unsigned len)
  97. {
  98. struct line *line = &serial_lines[c->index];
  99. unsigned long flags;
  100. spin_lock_irqsave(&line->lock, flags);
  101. console_write_chan(line->chan_out, string, len);
  102. spin_unlock_irqrestore(&line->lock, flags);
  103. }
  104. static struct tty_driver *ssl_console_device(struct console *c, int *index)
  105. {
  106. *index = c->index;
  107. return driver.driver;
  108. }
  109. static int ssl_console_setup(struct console *co, char *options)
  110. {
  111. struct line *line = &serial_lines[co->index];
  112. return console_open_chan(line, co);
  113. }
  114. /* No locking for register_console call - relies on single-threaded initcalls */
  115. static struct console ssl_cons = {
  116. .name = "ttyS",
  117. .write = ssl_console_write,
  118. .device = ssl_console_device,
  119. .setup = ssl_console_setup,
  120. .flags = CON_PRINTBUFFER|CON_ANYTIME,
  121. .index = -1,
  122. };
  123. static int ssl_init(void)
  124. {
  125. char *new_title;
  126. int err;
  127. int i;
  128. printk(KERN_INFO "Initializing software serial port version %d\n",
  129. ssl_version);
  130. err = register_lines(&driver, &ssl_ops, serial_lines,
  131. ARRAY_SIZE(serial_lines));
  132. if (err)
  133. return err;
  134. new_title = add_xterm_umid(opts.xterm_title);
  135. if (new_title != NULL)
  136. opts.xterm_title = new_title;
  137. for (i = 0; i < NR_PORTS; i++) {
  138. char *error;
  139. char *s = conf[i];
  140. if (!s)
  141. s = def_conf;
  142. if (setup_one_line(serial_lines, i, s, &opts, &error))
  143. printk(KERN_ERR "setup_one_line failed for "
  144. "device %d : %s\n", i, error);
  145. }
  146. ssl_init_done = 1;
  147. register_console(&ssl_cons);
  148. return 0;
  149. }
  150. late_initcall(ssl_init);
  151. static void ssl_exit(void)
  152. {
  153. if (!ssl_init_done)
  154. return;
  155. close_lines(serial_lines, ARRAY_SIZE(serial_lines));
  156. }
  157. __uml_exitcall(ssl_exit);
  158. static int ssl_chan_setup(char *str)
  159. {
  160. line_setup(conf, NR_PORTS, &def_conf, str, "serial line");
  161. return 1;
  162. }
  163. __setup("ssl", ssl_chan_setup);
  164. __channel_help(ssl_chan_setup, "ssl");
  165. static int ssl_non_raw_setup(char *str)
  166. {
  167. opts.raw = 0;
  168. return 1;
  169. }
  170. __setup("ssl-non-raw", ssl_non_raw_setup);
  171. __channel_help(ssl_non_raw_setup, "set serial lines to non-raw mode");