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