console.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * arch/xtensa/platforms/iss/console.c
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 2001-2005 Tensilica Inc.
  9. * Authors Christian Zankel, Joe Taylor
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/console.h>
  15. #include <linux/init.h>
  16. #include <linux/mm.h>
  17. #include <linux/major.h>
  18. #include <linux/param.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/serial.h>
  21. #include <linux/uaccess.h>
  22. #include <asm/irq.h>
  23. #include <platform/simcall.h>
  24. #include <linux/tty.h>
  25. #include <linux/tty_flip.h>
  26. #define SERIAL_MAX_NUM_LINES 1
  27. #define SERIAL_TIMER_VALUE (HZ / 10)
  28. static void rs_poll(struct timer_list *);
  29. static struct tty_driver *serial_driver;
  30. static struct tty_port serial_port;
  31. static DEFINE_TIMER(serial_timer, rs_poll);
  32. static int rs_open(struct tty_struct *tty, struct file * filp)
  33. {
  34. if (tty->count == 1)
  35. mod_timer(&serial_timer, jiffies + SERIAL_TIMER_VALUE);
  36. return 0;
  37. }
  38. static void rs_close(struct tty_struct *tty, struct file * filp)
  39. {
  40. if (tty->count == 1)
  41. del_timer_sync(&serial_timer);
  42. }
  43. static int rs_write(struct tty_struct * tty,
  44. const unsigned char *buf, int count)
  45. {
  46. /* see drivers/char/serialX.c to reference original version */
  47. simc_write(1, buf, count);
  48. return count;
  49. }
  50. static void rs_poll(struct timer_list *unused)
  51. {
  52. struct tty_port *port = &serial_port;
  53. int i = 0;
  54. int rd = 1;
  55. unsigned char c;
  56. while (simc_poll(0)) {
  57. rd = simc_read(0, &c, 1);
  58. if (rd <= 0)
  59. break;
  60. tty_insert_flip_char(port, c, TTY_NORMAL);
  61. i++;
  62. }
  63. if (i)
  64. tty_flip_buffer_push(port);
  65. if (rd)
  66. mod_timer(&serial_timer, jiffies + SERIAL_TIMER_VALUE);
  67. }
  68. static int rs_put_char(struct tty_struct *tty, unsigned char ch)
  69. {
  70. return rs_write(tty, &ch, 1);
  71. }
  72. static void rs_flush_chars(struct tty_struct *tty)
  73. {
  74. }
  75. static unsigned int rs_write_room(struct tty_struct *tty)
  76. {
  77. /* Let's say iss can always accept 2K characters.. */
  78. return 2 * 1024;
  79. }
  80. static void rs_hangup(struct tty_struct *tty)
  81. {
  82. /* Stub, once again.. */
  83. }
  84. static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
  85. {
  86. /* Stub, once again.. */
  87. }
  88. static int rs_proc_show(struct seq_file *m, void *v)
  89. {
  90. seq_printf(m, "serinfo:1.0 driver:0.1\n");
  91. return 0;
  92. }
  93. static const struct tty_operations serial_ops = {
  94. .open = rs_open,
  95. .close = rs_close,
  96. .write = rs_write,
  97. .put_char = rs_put_char,
  98. .flush_chars = rs_flush_chars,
  99. .write_room = rs_write_room,
  100. .hangup = rs_hangup,
  101. .wait_until_sent = rs_wait_until_sent,
  102. .proc_show = rs_proc_show,
  103. };
  104. static int __init rs_init(void)
  105. {
  106. struct tty_driver *driver;
  107. int ret;
  108. driver = tty_alloc_driver(SERIAL_MAX_NUM_LINES, TTY_DRIVER_REAL_RAW);
  109. if (IS_ERR(driver))
  110. return PTR_ERR(driver);
  111. tty_port_init(&serial_port);
  112. /* Initialize the tty_driver structure */
  113. driver->driver_name = "iss_serial";
  114. driver->name = "ttyS";
  115. driver->major = TTY_MAJOR;
  116. driver->minor_start = 64;
  117. driver->type = TTY_DRIVER_TYPE_SERIAL;
  118. driver->subtype = SERIAL_TYPE_NORMAL;
  119. driver->init_termios = tty_std_termios;
  120. driver->init_termios.c_cflag =
  121. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  122. tty_set_operations(driver, &serial_ops);
  123. tty_port_link_device(&serial_port, driver, 0);
  124. ret = tty_register_driver(driver);
  125. if (ret) {
  126. pr_err("Couldn't register serial driver\n");
  127. tty_driver_kref_put(driver);
  128. tty_port_destroy(&serial_port);
  129. return ret;
  130. }
  131. serial_driver = driver;
  132. return 0;
  133. }
  134. static __exit void rs_exit(void)
  135. {
  136. tty_unregister_driver(serial_driver);
  137. tty_driver_kref_put(serial_driver);
  138. tty_port_destroy(&serial_port);
  139. }
  140. /* We use `late_initcall' instead of just `__initcall' as a workaround for
  141. * the fact that (1) simcons_tty_init can't be called before tty_init,
  142. * (2) tty_init is called via `module_init', (3) if statically linked,
  143. * module_init == device_init, and (4) there's no ordering of init lists.
  144. * We can do this easily because simcons is always statically linked, but
  145. * other tty drivers that depend on tty_init and which must use
  146. * `module_init' to declare their init routines are likely to be broken.
  147. */
  148. late_initcall(rs_init);
  149. #ifdef CONFIG_SERIAL_CONSOLE
  150. static void iss_console_write(struct console *co, const char *s, unsigned count)
  151. {
  152. if (s && *s != 0) {
  153. int len = strlen(s);
  154. simc_write(1, s, count < len ? count : len);
  155. }
  156. }
  157. static struct tty_driver* iss_console_device(struct console *c, int *index)
  158. {
  159. *index = c->index;
  160. return serial_driver;
  161. }
  162. static struct console sercons = {
  163. .name = "ttyS",
  164. .write = iss_console_write,
  165. .device = iss_console_device,
  166. .flags = CON_PRINTBUFFER,
  167. .index = -1
  168. };
  169. static int __init iss_console_init(void)
  170. {
  171. register_console(&sercons);
  172. return 0;
  173. }
  174. console_initcall(iss_console_init);
  175. #endif /* CONFIG_SERIAL_CONSOLE */