udbg.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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)(unsigned char c);
  18. unsigned char (*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 c, *p = buf;
  52. int i;
  53. if (!udbg_getc)
  54. return 0;
  55. for (i = 0; i < buflen; ++i) {
  56. do {
  57. c = udbg_getc();
  58. } while (c == 0x11 || c == 0x13);
  59. if (c == 0)
  60. break;
  61. *p++ = c;
  62. }
  63. return i;
  64. }
  65. #define UDBG_BUFSIZE 256
  66. void udbg_printf(const char *fmt, ...)
  67. {
  68. unsigned char buf[UDBG_BUFSIZE];
  69. va_list args;
  70. va_start(args, fmt);
  71. vsnprintf(buf, UDBG_BUFSIZE, fmt, args);
  72. udbg_puts(buf);
  73. va_end(args);
  74. }
  75. /*
  76. * Early boot console based on udbg
  77. */
  78. static void udbg_console_write(struct console *con, const char *s,
  79. unsigned int n)
  80. {
  81. udbg_write(s, n);
  82. }
  83. static struct console udbg_console = {
  84. .name = "udbg",
  85. .write = udbg_console_write,
  86. .flags = CON_PRINTBUFFER,
  87. .index = -1,
  88. };
  89. static int early_console_initialized;
  90. void __init disable_early_printk(void)
  91. {
  92. if (!early_console_initialized)
  93. return;
  94. unregister_console(&udbg_console);
  95. early_console_initialized = 0;
  96. }
  97. /* called by setup_system */
  98. void register_early_udbg_console(void)
  99. {
  100. early_console_initialized = 1;
  101. register_console(&udbg_console);
  102. }
  103. #if 0 /* if you want to use this as a regular output console */
  104. console_initcall(register_udbg_console);
  105. #endif