udbg_adb.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include <linux/string.h>
  2. #include <linux/kernel.h>
  3. #include <linux/errno.h>
  4. #include <linux/bitops.h>
  5. #include <linux/ptrace.h>
  6. #include <linux/adb.h>
  7. #include <linux/pmu.h>
  8. #include <linux/cuda.h>
  9. #include <asm/machdep.h>
  10. #include <asm/io.h>
  11. #include <asm/page.h>
  12. #include <asm/xmon.h>
  13. #include <asm/prom.h>
  14. #include <asm/bootx.h>
  15. #include <asm/errno.h>
  16. #include <asm/pmac_feature.h>
  17. #include <asm/processor.h>
  18. #include <asm/delay.h>
  19. #include <asm/btext.h>
  20. #include <asm/time.h>
  21. #include <asm/udbg.h>
  22. /*
  23. * This implementation is "special", it can "patch" the current
  24. * udbg implementation and work on top of it. It must thus be
  25. * initialized last
  26. */
  27. static void (*udbg_adb_old_putc)(char c);
  28. static int (*udbg_adb_old_getc)(void);
  29. static int (*udbg_adb_old_getc_poll)(void);
  30. static enum {
  31. input_adb_none,
  32. input_adb_pmu,
  33. input_adb_cuda,
  34. } input_type = input_adb_none;
  35. int xmon_wants_key, xmon_adb_keycode;
  36. static inline void udbg_adb_poll(void)
  37. {
  38. #ifdef CONFIG_ADB_PMU
  39. if (input_type == input_adb_pmu)
  40. pmu_poll_adb();
  41. #endif /* CONFIG_ADB_PMU */
  42. #ifdef CONFIG_ADB_CUDA
  43. if (input_type == input_adb_cuda)
  44. cuda_poll();
  45. #endif /* CONFIG_ADB_CUDA */
  46. }
  47. #ifdef CONFIG_BOOTX_TEXT
  48. static int udbg_adb_use_btext;
  49. static int xmon_adb_shiftstate;
  50. static unsigned char xmon_keytab[128] =
  51. "asdfhgzxcv\000bqwer" /* 0x00 - 0x0f */
  52. "yt123465=97-80]o" /* 0x10 - 0x1f */
  53. "u[ip\rlj'k;\\,/nm." /* 0x20 - 0x2f */
  54. "\t `\177\0\033\0\0\0\0\0\0\0\0\0\0" /* 0x30 - 0x3f */
  55. "\0.\0*\0+\0\0\0\0\0/\r\0-\0" /* 0x40 - 0x4f */
  56. "\0\0000123456789\0\0\0"; /* 0x50 - 0x5f */
  57. static unsigned char xmon_shift_keytab[128] =
  58. "ASDFHGZXCV\000BQWER" /* 0x00 - 0x0f */
  59. "YT!@#$^%+(&_*)}O" /* 0x10 - 0x1f */
  60. "U{IP\rLJ\"K:|<?NM>" /* 0x20 - 0x2f */
  61. "\t ~\177\0\033\0\0\0\0\0\0\0\0\0\0" /* 0x30 - 0x3f */
  62. "\0.\0*\0+\0\0\0\0\0/\r\0-\0" /* 0x40 - 0x4f */
  63. "\0\0000123456789\0\0\0"; /* 0x50 - 0x5f */
  64. static int udbg_adb_local_getc(void)
  65. {
  66. int k, t, on;
  67. xmon_wants_key = 1;
  68. for (;;) {
  69. xmon_adb_keycode = -1;
  70. t = 0;
  71. on = 0;
  72. k = -1;
  73. do {
  74. if (--t < 0) {
  75. on = 1 - on;
  76. btext_drawchar(on? 0xdb: 0x20);
  77. btext_drawchar('\b');
  78. t = 200000;
  79. }
  80. udbg_adb_poll();
  81. if (udbg_adb_old_getc_poll)
  82. k = udbg_adb_old_getc_poll();
  83. } while (k == -1 && xmon_adb_keycode == -1);
  84. if (on)
  85. btext_drawstring(" \b");
  86. if (k != -1)
  87. return k;
  88. k = xmon_adb_keycode;
  89. /* test for shift keys */
  90. if ((k & 0x7f) == 0x38 || (k & 0x7f) == 0x7b) {
  91. xmon_adb_shiftstate = (k & 0x80) == 0;
  92. continue;
  93. }
  94. if (k >= 0x80)
  95. continue; /* ignore up transitions */
  96. k = (xmon_adb_shiftstate? xmon_shift_keytab: xmon_keytab)[k];
  97. if (k != 0)
  98. break;
  99. }
  100. xmon_wants_key = 0;
  101. return k;
  102. }
  103. #endif /* CONFIG_BOOTX_TEXT */
  104. static int udbg_adb_getc(void)
  105. {
  106. #ifdef CONFIG_BOOTX_TEXT
  107. if (udbg_adb_use_btext && input_type != input_adb_none)
  108. return udbg_adb_local_getc();
  109. #endif
  110. if (udbg_adb_old_getc)
  111. return udbg_adb_old_getc();
  112. return -1;
  113. }
  114. /* getc_poll() is not really used, unless you have the xmon-over modem
  115. * hack that doesn't quite concern us here, thus we just poll the low level
  116. * ADB driver to prevent it from timing out and call back the original poll
  117. * routine.
  118. */
  119. static int udbg_adb_getc_poll(void)
  120. {
  121. udbg_adb_poll();
  122. if (udbg_adb_old_getc_poll)
  123. return udbg_adb_old_getc_poll();
  124. return -1;
  125. }
  126. static void udbg_adb_putc(char c)
  127. {
  128. #ifdef CONFIG_BOOTX_TEXT
  129. if (udbg_adb_use_btext)
  130. btext_drawchar(c);
  131. #endif
  132. if (udbg_adb_old_putc)
  133. return udbg_adb_old_putc(c);
  134. }
  135. void __init udbg_adb_init_early(void)
  136. {
  137. #ifdef CONFIG_BOOTX_TEXT
  138. if (btext_find_display(1) == 0) {
  139. udbg_adb_use_btext = 1;
  140. udbg_putc = udbg_adb_putc;
  141. }
  142. #endif
  143. }
  144. int __init udbg_adb_init(int force_btext)
  145. {
  146. struct device_node *np;
  147. /* Capture existing callbacks */
  148. udbg_adb_old_putc = udbg_putc;
  149. udbg_adb_old_getc = udbg_getc;
  150. udbg_adb_old_getc_poll = udbg_getc_poll;
  151. /* Check if our early init was already called */
  152. if (udbg_adb_old_putc == udbg_adb_putc)
  153. udbg_adb_old_putc = NULL;
  154. #ifdef CONFIG_BOOTX_TEXT
  155. if (udbg_adb_old_putc == btext_drawchar)
  156. udbg_adb_old_putc = NULL;
  157. #endif
  158. /* Set ours as output */
  159. udbg_putc = udbg_adb_putc;
  160. udbg_getc = udbg_adb_getc;
  161. udbg_getc_poll = udbg_adb_getc_poll;
  162. #ifdef CONFIG_BOOTX_TEXT
  163. /* Check if we should use btext output */
  164. if (btext_find_display(force_btext) == 0)
  165. udbg_adb_use_btext = 1;
  166. #endif
  167. /* See if there is a keyboard in the device tree with a parent
  168. * of type "adb". If not, we return a failure, but we keep the
  169. * bext output set for now
  170. */
  171. for_each_node_by_name(np, "keyboard") {
  172. struct device_node *parent = of_get_parent(np);
  173. int found = (parent && strcmp(parent->type, "adb") == 0);
  174. of_node_put(parent);
  175. if (found)
  176. break;
  177. }
  178. if (np == NULL)
  179. return -ENODEV;
  180. of_node_put(np);
  181. #ifdef CONFIG_ADB_PMU
  182. if (find_via_pmu())
  183. input_type = input_adb_pmu;
  184. #endif
  185. #ifdef CONFIG_ADB_CUDA
  186. if (find_via_cuda())
  187. input_type = input_adb_cuda;
  188. #endif
  189. /* Same as above: nothing found, keep btext set for output */
  190. if (input_type == input_adb_none)
  191. return -ENODEV;
  192. return 0;
  193. }