console.c 6.4 KB

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