srmcons.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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_struct *tty)
  40. {
  41. struct tty_port *port = tty->port;
  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(tty);
  53. return count;
  54. }
  55. static void
  56. srmcons_receive_chars(unsigned long data)
  57. {
  58. struct srmcons_private *srmconsp = (struct srmcons_private *)data;
  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->tty))
  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_struct *tty, 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 was provided
  99. */
  100. if (tty)
  101. srmcons_do_receive_chars(tty);
  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, (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. setup_timer(&srmcons_singleton.timer, srmcons_receive_chars,
  172. (unsigned long)&srmcons_singleton);
  173. if (srm_is_registered_console) {
  174. struct tty_driver *driver;
  175. int err;
  176. driver = alloc_tty_driver(MAX_SRM_CONSOLE_DEVICES);
  177. if (!driver)
  178. return -ENOMEM;
  179. tty_port_init(&srmcons_singleton.port);
  180. driver->driver_name = "srm";
  181. driver->name = "srm";
  182. driver->major = 0; /* dynamic */
  183. driver->minor_start = 0;
  184. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  185. driver->subtype = SYSTEM_TYPE_SYSCONS;
  186. driver->init_termios = tty_std_termios;
  187. tty_set_operations(driver, &srmcons_ops);
  188. tty_port_link_device(&srmcons_singleton.port, driver, 0);
  189. err = tty_register_driver(driver);
  190. if (err) {
  191. put_tty_driver(driver);
  192. tty_port_destroy(&srmcons_singleton.port);
  193. return err;
  194. }
  195. srmcons_driver = driver;
  196. }
  197. return -ENODEV;
  198. }
  199. module_init(srmcons_init);
  200. /*
  201. * The console driver
  202. */
  203. static void
  204. srm_console_write(struct console *co, const char *s, unsigned count)
  205. {
  206. unsigned long flags;
  207. spin_lock_irqsave(&srmcons_callback_lock, flags);
  208. srmcons_do_write(NULL, s, count);
  209. spin_unlock_irqrestore(&srmcons_callback_lock, flags);
  210. }
  211. static struct tty_driver *
  212. srm_console_device(struct console *co, int *index)
  213. {
  214. *index = co->index;
  215. return srmcons_driver;
  216. }
  217. static int
  218. srm_console_setup(struct console *co, char *options)
  219. {
  220. return 0;
  221. }
  222. static struct console srmcons = {
  223. .name = "srm",
  224. .write = srm_console_write,
  225. .device = srm_console_device,
  226. .setup = srm_console_setup,
  227. .flags = CON_PRINTBUFFER | CON_BOOT,
  228. .index = -1,
  229. };
  230. void __init
  231. register_srm_console(void)
  232. {
  233. if (!srm_is_registered_console) {
  234. callback_open_console();
  235. register_console(&srmcons);
  236. srm_is_registered_console = 1;
  237. }
  238. }
  239. void __init
  240. unregister_srm_console(void)
  241. {
  242. if (srm_is_registered_console) {
  243. callback_close_console();
  244. unregister_console(&srmcons);
  245. srm_is_registered_console = 0;
  246. }
  247. }