udbg_adb.c 5.0 KB

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