udbg.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * polling mode stateless debugging stuff, originally for NS16550 Serial Ports
  3. *
  4. * c 2001 PPC 64 Team, IBM Corp
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <stdarg.h>
  12. #include <linux/config.h>
  13. #include <linux/types.h>
  14. #include <linux/sched.h>
  15. #include <linux/console.h>
  16. #include <asm/processor.h>
  17. void (*udbg_putc)(char c);
  18. int (*udbg_getc)(void);
  19. int (*udbg_getc_poll)(void);
  20. /* udbg library, used by xmon et al */
  21. void udbg_puts(const char *s)
  22. {
  23. if (udbg_putc) {
  24. char c;
  25. if (s && *s != '\0') {
  26. while ((c = *s++) != '\0')
  27. udbg_putc(c);
  28. }
  29. }
  30. #if 0
  31. else {
  32. printk("%s", s);
  33. }
  34. #endif
  35. }
  36. int udbg_write(const char *s, int n)
  37. {
  38. int remain = n;
  39. char c;
  40. if (!udbg_putc)
  41. return 0;
  42. if (s && *s != '\0') {
  43. while (((c = *s++) != '\0') && (remain-- > 0)) {
  44. udbg_putc(c);
  45. }
  46. }
  47. return n - remain;
  48. }
  49. int udbg_read(char *buf, int buflen)
  50. {
  51. char *p = buf;
  52. int i, c;
  53. if (!udbg_getc)
  54. return 0;
  55. for (i = 0; i < buflen; ++i) {
  56. do {
  57. c = udbg_getc();
  58. if (c == -1 && i == 0)
  59. return -1;
  60. } while (c == 0x11 || c == 0x13);
  61. if (c == 0 || c == -1)
  62. break;
  63. *p++ = c;
  64. }
  65. return i;
  66. }
  67. #define UDBG_BUFSIZE 256
  68. void udbg_printf(const char *fmt, ...)
  69. {
  70. char buf[UDBG_BUFSIZE];
  71. va_list args;
  72. va_start(args, fmt);
  73. vsnprintf(buf, UDBG_BUFSIZE, fmt, args);
  74. udbg_puts(buf);
  75. va_end(args);
  76. }
  77. void __init udbg_progress(char *s, unsigned short hex)
  78. {
  79. udbg_puts(s);
  80. udbg_puts("\n");
  81. }
  82. /*
  83. * Early boot console based on udbg
  84. */
  85. static void udbg_console_write(struct console *con, const char *s,
  86. unsigned int n)
  87. {
  88. udbg_write(s, n);
  89. }
  90. static struct console udbg_console = {
  91. .name = "udbg",
  92. .write = udbg_console_write,
  93. .flags = CON_PRINTBUFFER | CON_ENABLED,
  94. .index = -1,
  95. };
  96. static int early_console_initialized;
  97. void __init disable_early_printk(void)
  98. {
  99. #if 1
  100. if (!early_console_initialized)
  101. return;
  102. unregister_console(&udbg_console);
  103. early_console_initialized = 0;
  104. #endif
  105. }
  106. /* called by setup_system */
  107. void register_early_udbg_console(void)
  108. {
  109. if (early_console_initialized)
  110. return;
  111. early_console_initialized = 1;
  112. register_console(&udbg_console);
  113. }
  114. #if 0 /* if you want to use this as a regular output console */
  115. console_initcall(register_udbg_console);
  116. #endif