ssl.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright (C) 2000, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  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 "line.h"
  15. #include "ssl.h"
  16. #include "chan_kern.h"
  17. #include "kern_util.h"
  18. #include "kern.h"
  19. #include "init.h"
  20. #include "irq_user.h"
  21. #include "mconsole_kern.h"
  22. static const int ssl_version = 1;
  23. /* Referenced only by tty_driver below - presumably it's locked correctly
  24. * by the tty driver.
  25. */
  26. static struct tty_driver *ssl_driver;
  27. #define NR_PORTS 64
  28. static void ssl_announce(char *dev_name, int dev)
  29. {
  30. printk(KERN_INFO "Serial line %d assigned device '%s'\n", dev,
  31. dev_name);
  32. }
  33. /* Almost const, except that xterm_title may be changed in an initcall */
  34. static struct chan_opts opts = {
  35. .announce = ssl_announce,
  36. .xterm_title = "Serial Line #%d",
  37. .raw = 1,
  38. };
  39. static int ssl_config(char *str, char **error_out);
  40. static int ssl_get_config(char *dev, char *str, int size, char **error_out);
  41. static int ssl_remove(int n, char **error_out);
  42. /* Const, except for .mc.list */
  43. static struct line_driver driver = {
  44. .name = "UML serial line",
  45. .device_name = "ttyS",
  46. .major = TTY_MAJOR,
  47. .minor_start = 64,
  48. .type = TTY_DRIVER_TYPE_SERIAL,
  49. .subtype = 0,
  50. .read_irq = SSL_IRQ,
  51. .read_irq_name = "ssl",
  52. .write_irq = SSL_WRITE_IRQ,
  53. .write_irq_name = "ssl-write",
  54. .mc = {
  55. .list = LIST_HEAD_INIT(driver.mc.list),
  56. .name = "ssl",
  57. .config = ssl_config,
  58. .get_config = ssl_get_config,
  59. .id = line_id,
  60. .remove = ssl_remove,
  61. },
  62. };
  63. /* The array is initialized by line_init, at initcall time. The
  64. * elements are locked individually as needed.
  65. */
  66. static struct line serial_lines[NR_PORTS] =
  67. { [0 ... NR_PORTS - 1] = LINE_INIT(CONFIG_SSL_CHAN, &driver) };
  68. static int ssl_config(char *str, char **error_out)
  69. {
  70. return line_config(serial_lines, ARRAY_SIZE(serial_lines), str, &opts,
  71. error_out);
  72. }
  73. static int ssl_get_config(char *dev, char *str, int size, char **error_out)
  74. {
  75. return line_get_config(dev, serial_lines, ARRAY_SIZE(serial_lines), str,
  76. size, error_out);
  77. }
  78. static int ssl_remove(int n, char **error_out)
  79. {
  80. return line_remove(serial_lines, ARRAY_SIZE(serial_lines), n,
  81. error_out);
  82. }
  83. static int ssl_open(struct tty_struct *tty, struct file *filp)
  84. {
  85. int err = line_open(serial_lines, tty);
  86. if (err)
  87. printk(KERN_ERR "Failed to open serial line %d, err = %d\n",
  88. tty->index, err);
  89. return err;
  90. }
  91. #if 0
  92. static void ssl_flush_buffer(struct tty_struct *tty)
  93. {
  94. return;
  95. }
  96. static void ssl_stop(struct tty_struct *tty)
  97. {
  98. printk(KERN_ERR "Someone should implement ssl_stop\n");
  99. }
  100. static void ssl_start(struct tty_struct *tty)
  101. {
  102. printk(KERN_ERR "Someone should implement ssl_start\n");
  103. }
  104. void ssl_hangup(struct tty_struct *tty)
  105. {
  106. }
  107. #endif
  108. static const struct tty_operations ssl_ops = {
  109. .open = ssl_open,
  110. .close = line_close,
  111. .write = line_write,
  112. .put_char = line_put_char,
  113. .write_room = line_write_room,
  114. .chars_in_buffer = line_chars_in_buffer,
  115. .flush_buffer = line_flush_buffer,
  116. .flush_chars = line_flush_chars,
  117. .set_termios = line_set_termios,
  118. .ioctl = line_ioctl,
  119. .throttle = line_throttle,
  120. .unthrottle = line_unthrottle,
  121. #if 0
  122. .stop = ssl_stop,
  123. .start = ssl_start,
  124. .hangup = ssl_hangup,
  125. #endif
  126. };
  127. /* Changed by ssl_init and referenced by ssl_exit, which are both serialized
  128. * by being an initcall and exitcall, respectively.
  129. */
  130. static int ssl_init_done = 0;
  131. static void ssl_console_write(struct console *c, const char *string,
  132. unsigned len)
  133. {
  134. struct line *line = &serial_lines[c->index];
  135. unsigned long flags;
  136. spin_lock_irqsave(&line->lock, flags);
  137. console_write_chan(&line->chan_list, string, len);
  138. spin_unlock_irqrestore(&line->lock, flags);
  139. }
  140. static struct tty_driver *ssl_console_device(struct console *c, int *index)
  141. {
  142. *index = c->index;
  143. return ssl_driver;
  144. }
  145. static int ssl_console_setup(struct console *co, char *options)
  146. {
  147. struct line *line = &serial_lines[co->index];
  148. return console_open_chan(line, co);
  149. }
  150. /* No locking for register_console call - relies on single-threaded initcalls */
  151. static struct console ssl_cons = {
  152. .name = "ttyS",
  153. .write = ssl_console_write,
  154. .device = ssl_console_device,
  155. .setup = ssl_console_setup,
  156. .flags = CON_PRINTBUFFER|CON_ANYTIME,
  157. .index = -1,
  158. };
  159. static int ssl_init(void)
  160. {
  161. char *new_title;
  162. printk(KERN_INFO "Initializing software serial port version %d\n",
  163. ssl_version);
  164. ssl_driver = register_lines(&driver, &ssl_ops, serial_lines,
  165. ARRAY_SIZE(serial_lines));
  166. new_title = add_xterm_umid(opts.xterm_title);
  167. if (new_title != NULL)
  168. opts.xterm_title = new_title;
  169. lines_init(serial_lines, ARRAY_SIZE(serial_lines), &opts);
  170. ssl_init_done = 1;
  171. register_console(&ssl_cons);
  172. return 0;
  173. }
  174. late_initcall(ssl_init);
  175. static void ssl_exit(void)
  176. {
  177. if (!ssl_init_done)
  178. return;
  179. close_lines(serial_lines, ARRAY_SIZE(serial_lines));
  180. }
  181. __uml_exitcall(ssl_exit);
  182. static int ssl_chan_setup(char *str)
  183. {
  184. char *error;
  185. int ret;
  186. ret = line_setup(serial_lines, ARRAY_SIZE(serial_lines), str, &error);
  187. if(ret < 0)
  188. printk(KERN_ERR "Failed to set up serial line with "
  189. "configuration string \"%s\" : %s\n", str, error);
  190. return 1;
  191. }
  192. __setup("ssl", ssl_chan_setup);
  193. __channel_help(ssl_chan_setup, "ssl");