udbg_adb.c 5.0 KB

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