srmcons.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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_schedule_flip(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 int
  122. srmcons_write_room(struct tty_struct *tty)
  123. {
  124. return 512;
  125. }
  126. static int
  127. srmcons_chars_in_buffer(struct tty_struct *tty)
  128. {
  129. return 0;
  130. }
  131. static int
  132. srmcons_open(struct tty_struct *tty, struct file *filp)
  133. {
  134. struct srmcons_private *srmconsp = &srmcons_singleton;
  135. struct tty_port *port = &srmconsp->port;
  136. unsigned long flags;
  137. spin_lock_irqsave(&port->lock, flags);
  138. if (!port->tty) {
  139. tty->driver_data = srmconsp;
  140. tty->port = port;
  141. port->tty = tty; /* XXX proper refcounting */
  142. mod_timer(&srmconsp->timer, jiffies + 10);
  143. }
  144. spin_unlock_irqrestore(&port->lock, flags);
  145. return 0;
  146. }
  147. static void
  148. srmcons_close(struct tty_struct *tty, struct file *filp)
  149. {
  150. struct srmcons_private *srmconsp = tty->driver_data;
  151. struct tty_port *port = &srmconsp->port;
  152. unsigned long flags;
  153. spin_lock_irqsave(&port->lock, flags);
  154. if (tty->count == 1) {
  155. port->tty = NULL;
  156. del_timer(&srmconsp->timer);
  157. }
  158. spin_unlock_irqrestore(&port->lock, flags);
  159. }
  160. static struct tty_driver *srmcons_driver;
  161. static const struct tty_operations srmcons_ops = {
  162. .open = srmcons_open,
  163. .close = srmcons_close,
  164. .write = srmcons_write,
  165. .write_room = srmcons_write_room,
  166. .chars_in_buffer= srmcons_chars_in_buffer,
  167. };
  168. static int __init
  169. srmcons_init(void)
  170. {
  171. timer_setup(&srmcons_singleton.timer, srmcons_receive_chars, 0);
  172. if (srm_is_registered_console) {
  173. struct tty_driver *driver;
  174. int err;
  175. driver = alloc_tty_driver(MAX_SRM_CONSOLE_DEVICES);
  176. if (!driver)
  177. return -ENOMEM;
  178. tty_port_init(&srmcons_singleton.port);
  179. driver->driver_name = "srm";
  180. driver->name = "srm";
  181. driver->major = 0; /* dynamic */
  182. driver->minor_start = 0;
  183. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  184. driver->subtype = SYSTEM_TYPE_SYSCONS;
  185. driver->init_termios = tty_std_termios;
  186. tty_set_operations(driver, &srmcons_ops);
  187. tty_port_link_device(&srmcons_singleton.port, driver, 0);
  188. err = tty_register_driver(driver);
  189. if (err) {
  190. put_tty_driver(driver);
  191. tty_port_destroy(&srmcons_singleton.port);
  192. return err;
  193. }
  194. srmcons_driver = driver;
  195. }
  196. return -ENODEV;
  197. }
  198. device_initcall(srmcons_init);
  199. /*
  200. * The console driver
  201. */
  202. static void
  203. srm_console_write(struct console *co, const char *s, unsigned count)
  204. {
  205. unsigned long flags;
  206. spin_lock_irqsave(&srmcons_callback_lock, flags);
  207. srmcons_do_write(NULL, s, count);
  208. spin_unlock_irqrestore(&srmcons_callback_lock, flags);
  209. }
  210. static struct tty_driver *
  211. srm_console_device(struct console *co, int *index)
  212. {
  213. *index = co->index;
  214. return srmcons_driver;
  215. }
  216. static int
  217. srm_console_setup(struct console *co, char *options)
  218. {
  219. return 0;
  220. }
  221. static struct console srmcons = {
  222. .name = "srm",
  223. .write = srm_console_write,
  224. .device = srm_console_device,
  225. .setup = srm_console_setup,
  226. .flags = CON_PRINTBUFFER | CON_BOOT,
  227. .index = -1,
  228. };
  229. void __init
  230. register_srm_console(void)
  231. {
  232. if (!srm_is_registered_console) {
  233. callback_open_console();
  234. register_console(&srmcons);
  235. srm_is_registered_console = 1;
  236. }
  237. }
  238. void __init
  239. unregister_srm_console(void)
  240. {
  241. if (srm_is_registered_console) {
  242. callback_close_console();
  243. unregister_console(&srmcons);
  244. srm_is_registered_console = 0;
  245. }
  246. }