srmcons.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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_struct *tty;
  28. struct timer_list timer;
  29. spinlock_t lock;
  30. };
  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_struct *tty)
  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(tty, (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. 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(srmconsp->tty))
  64. incr = 100;
  65. spin_unlock(&srmcons_callback_lock);
  66. }
  67. spin_lock(&srmconsp->lock);
  68. if (srmconsp->tty) {
  69. srmconsp->timer.expires = jiffies + incr;
  70. add_timer(&srmconsp->timer);
  71. }
  72. spin_unlock(&srmconsp->lock);
  73. local_irq_restore(flags);
  74. }
  75. /* called with callback_lock held */
  76. static int
  77. srmcons_do_write(struct tty_struct *tty, const char *buf, int count)
  78. {
  79. static char str_cr[1] = "\r";
  80. long c, remaining = count;
  81. srmcons_result result;
  82. char *cur;
  83. int need_cr;
  84. for (cur = (char *)buf; remaining > 0; ) {
  85. need_cr = 0;
  86. /*
  87. * Break it up into reasonable size chunks to allow a chance
  88. * for input to get in
  89. */
  90. for (c = 0; c < min_t(long, 128L, remaining) && !need_cr; c++)
  91. if (cur[c] == '\n')
  92. need_cr = 1;
  93. while (c > 0) {
  94. result.as_long = callback_puts(0, cur, c);
  95. c -= result.bits.c;
  96. remaining -= result.bits.c;
  97. cur += result.bits.c;
  98. /*
  99. * Check for pending input iff a tty was provided
  100. */
  101. if (tty)
  102. srmcons_do_receive_chars(tty);
  103. }
  104. while (need_cr) {
  105. result.as_long = callback_puts(0, str_cr, 1);
  106. if (result.bits.c > 0)
  107. need_cr = 0;
  108. }
  109. }
  110. return count;
  111. }
  112. static int
  113. srmcons_write(struct tty_struct *tty,
  114. const unsigned char *buf, int count)
  115. {
  116. unsigned long flags;
  117. spin_lock_irqsave(&srmcons_callback_lock, flags);
  118. srmcons_do_write(tty, (const char *) buf, count);
  119. spin_unlock_irqrestore(&srmcons_callback_lock, flags);
  120. return count;
  121. }
  122. static int
  123. srmcons_write_room(struct tty_struct *tty)
  124. {
  125. return 512;
  126. }
  127. static int
  128. srmcons_chars_in_buffer(struct tty_struct *tty)
  129. {
  130. return 0;
  131. }
  132. static int
  133. srmcons_get_private_struct(struct srmcons_private **ps)
  134. {
  135. static struct srmcons_private *srmconsp = NULL;
  136. static DEFINE_SPINLOCK(srmconsp_lock);
  137. unsigned long flags;
  138. int retval = 0;
  139. if (srmconsp == NULL) {
  140. srmconsp = kmalloc(sizeof(*srmconsp), GFP_KERNEL);
  141. spin_lock_irqsave(&srmconsp_lock, flags);
  142. if (srmconsp == NULL)
  143. retval = -ENOMEM;
  144. else {
  145. srmconsp->tty = NULL;
  146. spin_lock_init(&srmconsp->lock);
  147. init_timer(&srmconsp->timer);
  148. }
  149. spin_unlock_irqrestore(&srmconsp_lock, flags);
  150. }
  151. *ps = srmconsp;
  152. return retval;
  153. }
  154. static int
  155. srmcons_open(struct tty_struct *tty, struct file *filp)
  156. {
  157. struct srmcons_private *srmconsp;
  158. unsigned long flags;
  159. int retval;
  160. retval = srmcons_get_private_struct(&srmconsp);
  161. if (retval)
  162. return retval;
  163. spin_lock_irqsave(&srmconsp->lock, flags);
  164. if (!srmconsp->tty) {
  165. tty->driver_data = srmconsp;
  166. srmconsp->tty = tty;
  167. srmconsp->timer.function = srmcons_receive_chars;
  168. srmconsp->timer.data = (unsigned long)srmconsp;
  169. srmconsp->timer.expires = jiffies + 10;
  170. add_timer(&srmconsp->timer);
  171. }
  172. spin_unlock_irqrestore(&srmconsp->lock, flags);
  173. return 0;
  174. }
  175. static void
  176. srmcons_close(struct tty_struct *tty, struct file *filp)
  177. {
  178. struct srmcons_private *srmconsp = tty->driver_data;
  179. unsigned long flags;
  180. spin_lock_irqsave(&srmconsp->lock, flags);
  181. if (tty->count == 1) {
  182. srmconsp->tty = NULL;
  183. del_timer(&srmconsp->timer);
  184. }
  185. spin_unlock_irqrestore(&srmconsp->lock, flags);
  186. }
  187. static struct tty_driver *srmcons_driver;
  188. static const struct tty_operations srmcons_ops = {
  189. .open = srmcons_open,
  190. .close = srmcons_close,
  191. .write = srmcons_write,
  192. .write_room = srmcons_write_room,
  193. .chars_in_buffer= srmcons_chars_in_buffer,
  194. };
  195. static int __init
  196. srmcons_init(void)
  197. {
  198. if (srm_is_registered_console) {
  199. struct tty_driver *driver;
  200. int err;
  201. driver = alloc_tty_driver(MAX_SRM_CONSOLE_DEVICES);
  202. if (!driver)
  203. return -ENOMEM;
  204. driver->driver_name = "srm";
  205. driver->name = "srm";
  206. driver->major = 0; /* dynamic */
  207. driver->minor_start = 0;
  208. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  209. driver->subtype = SYSTEM_TYPE_SYSCONS;
  210. driver->init_termios = tty_std_termios;
  211. tty_set_operations(driver, &srmcons_ops);
  212. err = tty_register_driver(driver);
  213. if (err) {
  214. put_tty_driver(driver);
  215. return err;
  216. }
  217. srmcons_driver = driver;
  218. }
  219. return -ENODEV;
  220. }
  221. module_init(srmcons_init);
  222. /*
  223. * The console driver
  224. */
  225. static void
  226. srm_console_write(struct console *co, const char *s, unsigned count)
  227. {
  228. unsigned long flags;
  229. spin_lock_irqsave(&srmcons_callback_lock, flags);
  230. srmcons_do_write(NULL, s, count);
  231. spin_unlock_irqrestore(&srmcons_callback_lock, flags);
  232. }
  233. static struct tty_driver *
  234. srm_console_device(struct console *co, int *index)
  235. {
  236. *index = co->index;
  237. return srmcons_driver;
  238. }
  239. static int
  240. srm_console_setup(struct console *co, char *options)
  241. {
  242. return 0;
  243. }
  244. static struct console srmcons = {
  245. .name = "srm",
  246. .write = srm_console_write,
  247. .device = srm_console_device,
  248. .setup = srm_console_setup,
  249. .flags = CON_PRINTBUFFER | CON_BOOT,
  250. .index = -1,
  251. };
  252. void __init
  253. register_srm_console(void)
  254. {
  255. if (!srm_is_registered_console) {
  256. callback_open_console();
  257. register_console(&srmcons);
  258. srm_is_registered_console = 1;
  259. }
  260. }
  261. void __init
  262. unregister_srm_console(void)
  263. {
  264. if (srm_is_registered_console) {
  265. callback_close_console();
  266. unregister_console(&srmcons);
  267. srm_is_registered_console = 0;
  268. }
  269. }