serial.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Generic serial console support
  3. *
  4. * Author: Mark A. Greer <mgreer@mvista.com>
  5. *
  6. * Code in serial_edit_cmdline() copied from <file:arch/ppc/boot/simple/misc.c>
  7. * and was written by Matt Porter <mporter@kernel.crashing.org>.
  8. *
  9. * 2001,2006 (c) MontaVista Software, Inc. This file is licensed under
  10. * the terms of the GNU General Public License version 2. This program
  11. * is licensed "as is" without any warranty of any kind, whether express
  12. * or implied.
  13. */
  14. #include <stdarg.h>
  15. #include <stddef.h>
  16. #include "types.h"
  17. #include "string.h"
  18. #include "stdio.h"
  19. #include "io.h"
  20. #include "ops.h"
  21. extern void udelay(long delay);
  22. static int serial_open(void)
  23. {
  24. struct serial_console_data *scdp = console_ops.data;
  25. return scdp->open();
  26. }
  27. static void serial_write(char *buf, int len)
  28. {
  29. struct serial_console_data *scdp = console_ops.data;
  30. while (*buf != '\0')
  31. scdp->putc(*buf++);
  32. }
  33. static void serial_edit_cmdline(char *buf, int len)
  34. {
  35. int timer = 0, count;
  36. char ch, *cp;
  37. struct serial_console_data *scdp = console_ops.data;
  38. cp = buf;
  39. count = strlen(buf);
  40. cp = &buf[count];
  41. count++;
  42. while (timer++ < 5*1000) {
  43. if (scdp->tstc()) {
  44. while (((ch = scdp->getc()) != '\n') && (ch != '\r')) {
  45. /* Test for backspace/delete */
  46. if ((ch == '\b') || (ch == '\177')) {
  47. if (cp != buf) {
  48. cp--;
  49. count--;
  50. printf("\b \b");
  51. }
  52. /* Test for ^x/^u (and wipe the line) */
  53. } else if ((ch == '\030') || (ch == '\025')) {
  54. while (cp != buf) {
  55. cp--;
  56. count--;
  57. printf("\b \b");
  58. }
  59. } else if (count < len) {
  60. *cp++ = ch;
  61. count++;
  62. scdp->putc(ch);
  63. }
  64. }
  65. break; /* Exit 'timer' loop */
  66. }
  67. udelay(1000); /* 1 msec */
  68. }
  69. *cp = 0;
  70. }
  71. static void serial_close(void)
  72. {
  73. struct serial_console_data *scdp = console_ops.data;
  74. if (scdp->close)
  75. scdp->close();
  76. }
  77. static void *serial_get_stdout_devp(void)
  78. {
  79. void *devp;
  80. char devtype[MAX_PROP_LEN];
  81. char path[MAX_PATH_LEN];
  82. devp = finddevice("/chosen");
  83. if (devp == NULL)
  84. goto err_out;
  85. if (getprop(devp, "linux,stdout-path", path, MAX_PATH_LEN) > 0) {
  86. devp = finddevice(path);
  87. if (devp == NULL)
  88. goto err_out;
  89. if ((getprop(devp, "device_type", devtype, sizeof(devtype)) > 0)
  90. && !strcmp(devtype, "serial"))
  91. return devp;
  92. }
  93. err_out:
  94. return NULL;
  95. }
  96. static struct serial_console_data serial_cd;
  97. /* Node's "compatible" property determines which serial driver to use */
  98. int serial_console_init(void)
  99. {
  100. void *devp;
  101. int rc = -1;
  102. char compat[MAX_PROP_LEN];
  103. devp = serial_get_stdout_devp();
  104. if (devp == NULL)
  105. goto err_out;
  106. if (getprop(devp, "compatible", compat, sizeof(compat)) < 0)
  107. goto err_out;
  108. if (!strcmp(compat, "ns16550"))
  109. rc = ns16550_console_init(devp, &serial_cd);
  110. /* Add other serial console driver calls here */
  111. if (!rc) {
  112. console_ops.open = serial_open;
  113. console_ops.write = serial_write;
  114. console_ops.edit_cmdline = serial_edit_cmdline;
  115. console_ops.close = serial_close;
  116. console_ops.data = &serial_cd;
  117. return 0;
  118. }
  119. err_out:
  120. return -1;
  121. }