srmcons.c 5.9 KB

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