srmcons.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * linux/arch/alpha/kernel/srmcons.c
  3. *
  4. * Callback based driver for SRM Console console device.
  5. * (TTY driver and console driver)
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/init.h>
  9. #include <linux/console.h>
  10. #include <linux/delay.h>
  11. #include <linux/mm.h>
  12. #include <linux/slab.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/timer.h>
  15. #include <linux/tty.h>
  16. #include <linux/tty_driver.h>
  17. #include <linux/tty_flip.h>
  18. #include <asm/console.h>
  19. #include <asm/uaccess.h>
  20. static DEFINE_SPINLOCK(srmcons_callback_lock);
  21. static int srm_is_registered_console = 0;
  22. /*
  23. * The TTY driver
  24. */
  25. #define MAX_SRM_CONSOLE_DEVICES 1 /* only support 1 console device */
  26. struct srmcons_private {
  27. struct tty_port port;
  28. struct timer_list timer;
  29. } srmcons_singleton;
  30. typedef union _srmcons_result {
  31. struct {
  32. unsigned long c :61;
  33. unsigned long status :3;
  34. } bits;
  35. long as_long;
  36. } srmcons_result;
  37. /* called with callback_lock held */
  38. static int
  39. srmcons_do_receive_chars(struct tty_port *port)
  40. {
  41. srmcons_result result;
  42. int count = 0, loops = 0;
  43. do {
  44. result.as_long = callback_getc(0);
  45. if (result.bits.status < 2) {
  46. tty_insert_flip_char(port, (char)result.bits.c, 0);
  47. count++;
  48. }
  49. } while((result.bits.status & 1) && (++loops < 10));
  50. if (count)
  51. tty_schedule_flip(port);
  52. return count;
  53. }
  54. static void
  55. srmcons_receive_chars(unsigned long data)
  56. {
  57. struct srmcons_private *srmconsp = (struct srmcons_private *)data;
  58. struct tty_port *port = &srmconsp->port;
  59. unsigned long flags;
  60. int incr = 10;
  61. local_irq_save(flags);
  62. if (spin_trylock(&srmcons_callback_lock)) {
  63. if (!srmcons_do_receive_chars(port))
  64. incr = 100;
  65. spin_unlock(&srmcons_callback_lock);
  66. }
  67. spin_lock(&port->lock);
  68. if (port->tty)
  69. mod_timer(&srmconsp->timer, jiffies + incr);
  70. spin_unlock(&port->lock);
  71. local_irq_restore(flags);
  72. }
  73. /* called with callback_lock held */
  74. static int
  75. srmcons_do_write(struct tty_port *port, const char *buf, int count)
  76. {
  77. static char str_cr[1] = "\r";
  78. long c, remaining = count;
  79. srmcons_result result;
  80. char *cur;
  81. int need_cr;
  82. for (cur = (char *)buf; remaining > 0; ) {
  83. need_cr = 0;
  84. /*
  85. * Break it up into reasonable size chunks to allow a chance
  86. * for input to get in
  87. */
  88. for (c = 0; c < min_t(long, 128L, remaining) && !need_cr; c++)
  89. if (cur[c] == '\n')
  90. need_cr = 1;
  91. while (c > 0) {
  92. result.as_long = callback_puts(0, cur, c);
  93. c -= result.bits.c;
  94. remaining -= result.bits.c;
  95. cur += result.bits.c;
  96. /*
  97. * Check for pending input iff a tty port was provided
  98. */
  99. if (port)
  100. srmcons_do_receive_chars(port);
  101. }
  102. while (need_cr) {
  103. result.as_long = callback_puts(0, str_cr, 1);
  104. if (result.bits.c > 0)
  105. need_cr = 0;
  106. }
  107. }
  108. return count;
  109. }
  110. static int
  111. srmcons_write(struct tty_struct *tty,
  112. const unsigned char *buf, int count)
  113. {
  114. unsigned long flags;
  115. spin_lock_irqsave(&srmcons_callback_lock, flags);
  116. srmcons_do_write(tty->port, (const char *) buf, count);
  117. spin_unlock_irqrestore(&srmcons_callback_lock, flags);
  118. return count;
  119. }
  120. static int
  121. srmcons_write_room(struct tty_struct *tty)
  122. {
  123. return 512;
  124. }
  125. static int
  126. srmcons_chars_in_buffer(struct tty_struct *tty)
  127. {
  128. return 0;
  129. }
  130. static int
  131. srmcons_open(struct tty_struct *tty, struct file *filp)
  132. {
  133. struct srmcons_private *srmconsp = &srmcons_singleton;
  134. struct tty_port *port = &srmconsp->port;
  135. unsigned long flags;
  136. spin_lock_irqsave(&port->lock, flags);
  137. if (!port->tty) {
  138. tty->driver_data = srmconsp;
  139. tty->port = port;
  140. port->tty = tty; /* XXX proper refcounting */
  141. mod_timer(&srmconsp->timer, jiffies + 10);
  142. }
  143. spin_unlock_irqrestore(&port->lock, flags);
  144. return 0;
  145. }
  146. static void
  147. srmcons_close(struct tty_struct *tty, struct file *filp)
  148. {
  149. struct srmcons_private *srmconsp = tty->driver_data;
  150. struct tty_port *port = &srmconsp->port;
  151. unsigned long flags;
  152. spin_lock_irqsave(&port->lock, flags);
  153. if (tty->count == 1) {
  154. port->tty = NULL;
  155. del_timer(&srmconsp->timer);
  156. }
  157. spin_unlock_irqrestore(&port->lock, flags);
  158. }
  159. static struct tty_driver *srmcons_driver;
  160. static const struct tty_operations srmcons_ops = {
  161. .open = srmcons_open,
  162. .close = srmcons_close,
  163. .write = srmcons_write,
  164. .write_room = srmcons_write_room,
  165. .chars_in_buffer= srmcons_chars_in_buffer,
  166. };
  167. static int __init
  168. srmcons_init(void)
  169. {
  170. setup_timer(&srmcons_singleton.timer, srmcons_receive_chars,
  171. (unsigned long)&srmcons_singleton);
  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. }