console.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * arch/xtensa/platform-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/slab.h>
  17. #include <linux/mm.h>
  18. #include <linux/major.h>
  19. #include <linux/param.h>
  20. #include <linux/serial.h>
  21. #include <linux/serialP.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/irq.h>
  24. #include <asm/platform/simcall.h>
  25. #include <linux/tty.h>
  26. #include <linux/tty_flip.h>
  27. #ifdef SERIAL_INLINE
  28. #define _INLINE_ inline
  29. #endif
  30. #define SERIAL_MAX_NUM_LINES 1
  31. #define SERIAL_TIMER_VALUE (20 * HZ)
  32. static struct tty_driver *serial_driver;
  33. static struct timer_list serial_timer;
  34. static DEFINE_SPINLOCK(timer_lock);
  35. int errno;
  36. static int __simc (int a, int b, int c, int d, int e, int f)
  37. {
  38. int ret;
  39. __asm__ __volatile__ ("simcall\n"
  40. "mov %0, a2\n"
  41. "mov %1, a3\n" : "=a" (ret), "=a" (errno)
  42. : : "a2", "a3");
  43. return ret;
  44. }
  45. static char *serial_version = "0.1";
  46. static char *serial_name = "ISS serial driver";
  47. /*
  48. * This routine is called whenever a serial port is opened. It
  49. * enables interrupts for a serial port, linking in its async structure into
  50. * the IRQ chain. It also performs the serial-specific
  51. * initialization for the tty structure.
  52. */
  53. static void rs_poll(unsigned long);
  54. static int rs_open(struct tty_struct *tty, struct file * filp)
  55. {
  56. int line = tty->index;
  57. if ((line < 0) || (line >= SERIAL_MAX_NUM_LINES))
  58. return -ENODEV;
  59. spin_lock(&timer_lock);
  60. if (tty->count == 1) {
  61. init_timer(&serial_timer);
  62. serial_timer.data = (unsigned long) tty;
  63. serial_timer.function = rs_poll;
  64. mod_timer(&serial_timer, jiffies + SERIAL_TIMER_VALUE);
  65. }
  66. spin_unlock(&timer_lock);
  67. return 0;
  68. }
  69. /*
  70. * ------------------------------------------------------------
  71. * iss_serial_close()
  72. *
  73. * This routine is called when the serial port gets closed. First, we
  74. * wait for the last remaining data to be sent. Then, we unlink its
  75. * async structure from the interrupt chain if necessary, and we free
  76. * that IRQ if nothing is left in the chain.
  77. * ------------------------------------------------------------
  78. */
  79. static void rs_close(struct tty_struct *tty, struct file * filp)
  80. {
  81. spin_lock(&timer_lock);
  82. if (tty->count == 1)
  83. del_timer_sync(&serial_timer);
  84. spin_unlock(&timer_lock);
  85. }
  86. static int rs_write(struct tty_struct * tty,
  87. const unsigned char *buf, int count)
  88. {
  89. /* see drivers/char/serialX.c to reference original version */
  90. __simc (SYS_write, 1, (unsigned long)buf, count, 0, 0);
  91. return count;
  92. }
  93. static void rs_poll(unsigned long priv)
  94. {
  95. struct tty_struct* tty = (struct tty_struct*) priv;
  96. struct timeval tv = { .tv_sec = 0, .tv_usec = 0 };
  97. int i = 0;
  98. unsigned char c;
  99. spin_lock(&timer_lock);
  100. while (__simc(SYS_select_one, 0, XTISS_SELECT_ONE_READ, (int)&tv,0,0)){
  101. __simc (SYS_read, 0, (unsigned long)&c, 1, 0, 0);
  102. tty_insert_flip_char(tty, c, TTY_NORMAL);
  103. i++;
  104. }
  105. if (i)
  106. tty_flip_buffer_push(tty);
  107. mod_timer(&serial_timer, jiffies + SERIAL_TIMER_VALUE);
  108. spin_unlock(&timer_lock);
  109. }
  110. static void rs_put_char(struct tty_struct *tty, unsigned char ch)
  111. {
  112. char buf[2];
  113. if (!tty)
  114. return;
  115. buf[0] = ch;
  116. buf[1] = '\0'; /* Is this NULL necessary? */
  117. __simc (SYS_write, 1, (unsigned long) buf, 1, 0, 0);
  118. }
  119. static void rs_flush_chars(struct tty_struct *tty)
  120. {
  121. }
  122. static int rs_write_room(struct tty_struct *tty)
  123. {
  124. /* Let's say iss can always accept 2K characters.. */
  125. return 2 * 1024;
  126. }
  127. static int rs_chars_in_buffer(struct tty_struct *tty)
  128. {
  129. /* the iss doesn't buffer characters */
  130. return 0;
  131. }
  132. static void rs_hangup(struct tty_struct *tty)
  133. {
  134. /* Stub, once again.. */
  135. }
  136. static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
  137. {
  138. /* Stub, once again.. */
  139. }
  140. static int rs_read_proc(char *page, char **start, off_t off, int count,
  141. int *eof, void *data)
  142. {
  143. int len = 0;
  144. off_t begin = 0;
  145. len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
  146. *eof = 1;
  147. if (off >= len + begin)
  148. return 0;
  149. *start = page + (off - begin);
  150. return ((count < begin + len - off) ? count : begin + len - off);
  151. }
  152. static struct tty_operations serial_ops = {
  153. .open = rs_open,
  154. .close = rs_close,
  155. .write = rs_write,
  156. .put_char = rs_put_char,
  157. .flush_chars = rs_flush_chars,
  158. .write_room = rs_write_room,
  159. .chars_in_buffer = rs_chars_in_buffer,
  160. .hangup = rs_hangup,
  161. .wait_until_sent = rs_wait_until_sent,
  162. .read_proc = rs_read_proc
  163. };
  164. int __init rs_init(void)
  165. {
  166. serial_driver = alloc_tty_driver(1);
  167. printk ("%s %s\n", serial_name, serial_version);
  168. /* Initialize the tty_driver structure */
  169. serial_driver->owner = THIS_MODULE;
  170. serial_driver->driver_name = "iss_serial";
  171. serial_driver->name = "ttyS";
  172. serial_driver->major = TTY_MAJOR;
  173. serial_driver->minor_start = 64;
  174. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  175. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  176. serial_driver->init_termios = tty_std_termios;
  177. serial_driver->init_termios.c_cflag =
  178. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  179. serial_driver->flags = TTY_DRIVER_REAL_RAW;
  180. tty_set_operations(serial_driver, &serial_ops);
  181. if (tty_register_driver(serial_driver))
  182. panic("Couldn't register serial driver\n");
  183. return 0;
  184. }
  185. static __exit void rs_exit(void)
  186. {
  187. int error;
  188. if ((error = tty_unregister_driver(serial_driver)))
  189. printk("ISS_SERIAL: failed to unregister serial driver (%d)\n",
  190. error);
  191. put_tty_driver(serial_driver);
  192. }
  193. /* We use `late_initcall' instead of just `__initcall' as a workaround for
  194. * the fact that (1) simcons_tty_init can't be called before tty_init,
  195. * (2) tty_init is called via `module_init', (3) if statically linked,
  196. * module_init == device_init, and (4) there's no ordering of init lists.
  197. * We can do this easily because simcons is always statically linked, but
  198. * other tty drivers that depend on tty_init and which must use
  199. * `module_init' to declare their init routines are likely to be broken.
  200. */
  201. late_initcall(rs_init);
  202. #ifdef CONFIG_SERIAL_CONSOLE
  203. static void iss_console_write(struct console *co, const char *s, unsigned count)
  204. {
  205. int len = strlen(s);
  206. if (s != 0 && *s != 0)
  207. __simc (SYS_write, 1, (unsigned long)s,
  208. count < len ? count : len,0,0);
  209. }
  210. static struct tty_driver* iss_console_device(struct console *c, int *index)
  211. {
  212. *index = c->index;
  213. return serial_driver;
  214. }
  215. static struct console sercons = {
  216. .name = "ttyS",
  217. .write = iss_console_write,
  218. .device = iss_console_device,
  219. .flags = CON_PRINTBUFFER,
  220. .index = -1
  221. };
  222. static int __init iss_console_init(void)
  223. {
  224. register_console(&sercons);
  225. return 0;
  226. }
  227. console_initcall(iss_console_init);
  228. #endif /* CONFIG_SERIAL_CONSOLE */