srmcons.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/arch/alpha/kernel/srmcons.c
  4. *
  5. * Callback based driver for SRM Console console device.
  6. * (TTY driver and console driver)
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/console.h>
  11. #include <linux/delay.h>
  12. #include <linux/mm.h>
  13. #include <linux/slab.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/timer.h>
  16. #include <linux/tty.h>
  17. #include <linux/tty_driver.h>
  18. #include <linux/tty_flip.h>
  19. #include <asm/console.h>
  20. #include <linux/uaccess.h>
  21. static DEFINE_SPINLOCK(srmcons_callback_lock);
  22. static int srm_is_registered_console = 0;
  23. /*
  24. * The TTY driver
  25. */
  26. #define MAX_SRM_CONSOLE_DEVICES 1 /* only support 1 console device */
  27. struct srmcons_private {
  28. struct tty_port port;
  29. struct timer_list timer;
  30. } srmcons_singleton;
  31. typedef union _srmcons_result {
  32. struct {
  33. unsigned long c :61;
  34. unsigned long status :3;
  35. } bits;
  36. long as_long;
  37. } srmcons_result;
  38. /* called with callback_lock held */
  39. static int
  40. srmcons_do_receive_chars(struct tty_port *port)
  41. {
  42. srmcons_result result;
  43. int count = 0, loops = 0;
  44. do {
  45. result.as_long = callback_getc(0);
  46. if (result.bits.status < 2) {
  47. tty_insert_flip_char(port, (char)result.bits.c, 0);
  48. count++;
  49. }
  50. } while((result.bits.status & 1) && (++loops < 10));
  51. if (count)
  52. tty_flip_buffer_push(port);
  53. return count;
  54. }
  55. static void
  56. srmcons_receive_chars(struct timer_list *t)
  57. {
  58. struct srmcons_private *srmconsp = from_timer(srmconsp, t, timer);
  59. struct tty_port *port = &srmconsp->port;
  60. unsigned long flags;
  61. int incr = 10;
  62. local_irq_save(flags);
  63. if (spin_trylock(&srmcons_callback_lock)) {
  64. if (!srmcons_do_receive_chars(port))
  65. incr = 100;
  66. spin_unlock(&srmcons_callback_lock);
  67. }
  68. spin_lock(&port->lock);
  69. if (port->tty)
  70. mod_timer(&srmconsp->timer, jiffies + incr);
  71. spin_unlock(&port->lock);
  72. local_irq_restore(flags);
  73. }
  74. /* called with callback_lock held */
  75. static int
  76. srmcons_do_write(struct tty_port *port, const char *buf, int count)
  77. {
  78. static char str_cr[1] = "\r";
  79. long c, remaining = count;
  80. srmcons_result result;
  81. char *cur;
  82. int need_cr;
  83. for (cur = (char *)buf; remaining > 0; ) {
  84. need_cr = 0;
  85. /*
  86. * Break it up into reasonable size chunks to allow a chance
  87. * for input to get in
  88. */
  89. for (c = 0; c < min_t(long, 128L, remaining) && !need_cr; c++)
  90. if (cur[c] == '\n')
  91. need_cr = 1;
  92. while (c > 0) {
  93. result.as_long = callback_puts(0, cur, c);
  94. c -= result.bits.c;
  95. remaining -= result.bits.c;
  96. cur += result.bits.c;
  97. /*
  98. * Check for pending input iff a tty port was provided
  99. */
  100. if (port)
  101. srmcons_do_receive_chars(port);
  102. }
  103. while (need_cr) {
  104. result.as_long = callback_puts(0, str_cr, 1);
  105. if (result.bits.c > 0)
  106. need_cr = 0;
  107. }
  108. }
  109. return count;
  110. }
  111. static int
  112. srmcons_write(struct tty_struct *tty,
  113. const unsigned char *buf, int count)
  114. {
  115. unsigned long flags;
  116. spin_lock_irqsave(&srmcons_callback_lock, flags);
  117. srmcons_do_write(tty->port, (const char *) buf, count);
  118. spin_unlock_irqrestore(&srmcons_callback_lock, flags);
  119. return count;
  120. }
  121. static unsigned int
  122. srmcons_write_room(struct tty_struct *tty)
  123. {
  124. return 512;
  125. }
  126. static int
  127. srmcons_open(struct tty_struct *tty, struct file *filp)
  128. {
  129. struct srmcons_private *srmconsp = &srmcons_singleton;
  130. struct tty_port *port = &srmconsp->port;
  131. unsigned long flags;
  132. spin_lock_irqsave(&port->lock, flags);
  133. if (!port->tty) {
  134. tty->driver_data = srmconsp;
  135. tty->port = port;
  136. port->tty = tty; /* XXX proper refcounting */
  137. mod_timer(&srmconsp->timer, jiffies + 10);
  138. }
  139. spin_unlock_irqrestore(&port->lock, flags);
  140. return 0;
  141. }
  142. static void
  143. srmcons_close(struct tty_struct *tty, struct file *filp)
  144. {
  145. struct srmcons_private *srmconsp = tty->driver_data;
  146. struct tty_port *port = &srmconsp->port;
  147. unsigned long flags;
  148. spin_lock_irqsave(&port->lock, flags);
  149. if (tty->count == 1) {
  150. port->tty = NULL;
  151. del_timer(&srmconsp->timer);
  152. }
  153. spin_unlock_irqrestore(&port->lock, flags);
  154. }
  155. static struct tty_driver *srmcons_driver;
  156. static const struct tty_operations srmcons_ops = {
  157. .open = srmcons_open,
  158. .close = srmcons_close,
  159. .write = srmcons_write,
  160. .write_room = srmcons_write_room,
  161. };
  162. static int __init
  163. srmcons_init(void)
  164. {
  165. timer_setup(&srmcons_singleton.timer, srmcons_receive_chars, 0);
  166. if (srm_is_registered_console) {
  167. struct tty_driver *driver;
  168. int err;
  169. driver = tty_alloc_driver(MAX_SRM_CONSOLE_DEVICES, 0);
  170. if (IS_ERR(driver))
  171. return PTR_ERR(driver);
  172. tty_port_init(&srmcons_singleton.port);
  173. driver->driver_name = "srm";
  174. driver->name = "srm";
  175. driver->major = 0; /* dynamic */
  176. driver->minor_start = 0;
  177. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  178. driver->subtype = SYSTEM_TYPE_SYSCONS;
  179. driver->init_termios = tty_std_termios;
  180. tty_set_operations(driver, &srmcons_ops);
  181. tty_port_link_device(&srmcons_singleton.port, driver, 0);
  182. err = tty_register_driver(driver);
  183. if (err) {
  184. tty_driver_kref_put(driver);
  185. tty_port_destroy(&srmcons_singleton.port);
  186. return err;
  187. }
  188. srmcons_driver = driver;
  189. }
  190. return -ENODEV;
  191. }
  192. device_initcall(srmcons_init);
  193. /*
  194. * The console driver
  195. */
  196. static void
  197. srm_console_write(struct console *co, const char *s, unsigned count)
  198. {
  199. unsigned long flags;
  200. spin_lock_irqsave(&srmcons_callback_lock, flags);
  201. srmcons_do_write(NULL, s, count);
  202. spin_unlock_irqrestore(&srmcons_callback_lock, flags);
  203. }
  204. static struct tty_driver *
  205. srm_console_device(struct console *co, int *index)
  206. {
  207. *index = co->index;
  208. return srmcons_driver;
  209. }
  210. static int
  211. srm_console_setup(struct console *co, char *options)
  212. {
  213. return 0;
  214. }
  215. static struct console srmcons = {
  216. .name = "srm",
  217. .write = srm_console_write,
  218. .device = srm_console_device,
  219. .setup = srm_console_setup,
  220. .flags = CON_PRINTBUFFER | CON_BOOT,
  221. .index = -1,
  222. };
  223. void __init
  224. register_srm_console(void)
  225. {
  226. if (!srm_is_registered_console) {
  227. callback_open_console();
  228. register_console(&srmcons);
  229. srm_is_registered_console = 1;
  230. }
  231. }
  232. void __init
  233. unregister_srm_console(void)
  234. {
  235. if (srm_is_registered_console) {
  236. callback_close_console();
  237. unregister_console(&srmcons);
  238. srm_is_registered_console = 0;
  239. }
  240. }