console.c 6.8 KB

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