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