console.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. timer_delete_sync(&serial_timer);
  42. }
  43. static ssize_t rs_write(struct tty_struct * tty, const u8 *buf, size_t count)
  44. {
  45. /* see drivers/char/serialX.c to reference original version */
  46. simc_write(1, buf, count);
  47. return count;
  48. }
  49. static void rs_poll(struct timer_list *unused)
  50. {
  51. struct tty_port *port = &serial_port;
  52. int i = 0;
  53. int rd = 1;
  54. u8 c;
  55. while (simc_poll(0)) {
  56. rd = simc_read(0, &c, 1);
  57. if (rd <= 0)
  58. break;
  59. tty_insert_flip_char(port, c, TTY_NORMAL);
  60. i++;
  61. }
  62. if (i)
  63. tty_flip_buffer_push(port);
  64. if (rd)
  65. mod_timer(&serial_timer, jiffies + SERIAL_TIMER_VALUE);
  66. }
  67. static unsigned int rs_write_room(struct tty_struct *tty)
  68. {
  69. /* Let's say iss can always accept 2K characters.. */
  70. return 2 * 1024;
  71. }
  72. static int rs_proc_show(struct seq_file *m, void *v)
  73. {
  74. seq_printf(m, "serinfo:1.0 driver:0.1\n");
  75. return 0;
  76. }
  77. static const struct tty_operations serial_ops = {
  78. .open = rs_open,
  79. .close = rs_close,
  80. .write = rs_write,
  81. .write_room = rs_write_room,
  82. .proc_show = rs_proc_show,
  83. };
  84. static int __init rs_init(void)
  85. {
  86. struct tty_driver *driver;
  87. int ret;
  88. driver = tty_alloc_driver(SERIAL_MAX_NUM_LINES, TTY_DRIVER_REAL_RAW);
  89. if (IS_ERR(driver))
  90. return PTR_ERR(driver);
  91. tty_port_init(&serial_port);
  92. /* Initialize the tty_driver structure */
  93. driver->driver_name = "iss_serial";
  94. driver->name = "ttyS";
  95. driver->major = TTY_MAJOR;
  96. driver->minor_start = 64;
  97. driver->type = TTY_DRIVER_TYPE_SERIAL;
  98. driver->subtype = SERIAL_TYPE_NORMAL;
  99. driver->init_termios = tty_std_termios;
  100. driver->init_termios.c_cflag =
  101. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  102. tty_set_operations(driver, &serial_ops);
  103. tty_port_link_device(&serial_port, driver, 0);
  104. ret = tty_register_driver(driver);
  105. if (ret) {
  106. pr_err("Couldn't register serial driver\n");
  107. tty_driver_kref_put(driver);
  108. tty_port_destroy(&serial_port);
  109. return ret;
  110. }
  111. serial_driver = driver;
  112. return 0;
  113. }
  114. static __exit void rs_exit(void)
  115. {
  116. tty_unregister_driver(serial_driver);
  117. tty_driver_kref_put(serial_driver);
  118. tty_port_destroy(&serial_port);
  119. }
  120. /* We use `late_initcall' instead of just `__initcall' as a workaround for
  121. * the fact that (1) simcons_tty_init can't be called before tty_init,
  122. * (2) tty_init is called via `module_init', (3) if statically linked,
  123. * module_init == device_init, and (4) there's no ordering of init lists.
  124. * We can do this easily because simcons is always statically linked, but
  125. * other tty drivers that depend on tty_init and which must use
  126. * `module_init' to declare their init routines are likely to be broken.
  127. */
  128. late_initcall(rs_init);
  129. #ifdef CONFIG_SERIAL_CONSOLE
  130. static void iss_console_write(struct console *co, const char *s, unsigned count)
  131. {
  132. if (s && *s != 0)
  133. simc_write(1, s, min(count, strlen(s)));
  134. }
  135. static struct tty_driver* iss_console_device(struct console *c, int *index)
  136. {
  137. *index = c->index;
  138. return serial_driver;
  139. }
  140. static struct console sercons = {
  141. .name = "ttyS",
  142. .write = iss_console_write,
  143. .device = iss_console_device,
  144. .flags = CON_PRINTBUFFER,
  145. .index = -1
  146. };
  147. static int __init iss_console_init(void)
  148. {
  149. register_console(&sercons);
  150. return 0;
  151. }
  152. console_initcall(iss_console_init);
  153. #endif /* CONFIG_SERIAL_CONSOLE */