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