summaryrefslogtreecommitdiff
path: root/arch/powerpc/platforms/powermac/udbg_adb.c
blob: 93e0a8372fbcff0326055bbacba21ae24b91dd8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// SPDX-License-Identifier: GPL-2.0
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/bitops.h>
#include <linux/ptrace.h>
#include <linux/adb.h>
#include <linux/pmu.h>
#include <linux/cuda.h>
#include <asm/machdep.h>
#include <asm/io.h>
#include <asm/page.h>
#include <asm/xmon.h>
#include <asm/bootx.h>
#include <asm/errno.h>
#include <asm/pmac_feature.h>
#include <asm/processor.h>
#include <asm/delay.h>
#include <asm/btext.h>
#include <asm/time.h>
#include <asm/udbg.h>

/*
 * This implementation is "special", it can "patch" the current
 * udbg implementation and work on top of it. It must thus be
 * initialized last
 */

static void (*udbg_adb_old_putc)(char c);
static int (*udbg_adb_old_getc)(void);
static int (*udbg_adb_old_getc_poll)(void);

static enum {
	input_adb_none,
	input_adb_pmu,
	input_adb_cuda,
} input_type = input_adb_none;

int xmon_wants_key, xmon_adb_keycode;

static inline void udbg_adb_poll(void)
{
#ifdef CONFIG_ADB_PMU
	if (input_type == input_adb_pmu)
		pmu_poll_adb();
#endif /* CONFIG_ADB_PMU */
#ifdef CONFIG_ADB_CUDA
	if (input_type == input_adb_cuda)
		cuda_poll();
#endif /* CONFIG_ADB_CUDA */
}

#ifdef CONFIG_BOOTX_TEXT

static int udbg_adb_use_btext;
static int xmon_adb_shiftstate;

static unsigned char xmon_keytab[128] =
	"asdfhgzxcv\000bqwer"				/* 0x00 - 0x0f */
	"yt123465=97-80]o"				/* 0x10 - 0x1f */
	"u[ip\rlj'k;\\,/nm."				/* 0x20 - 0x2f */
	"\t `\177\0\033\0\0\0\0\0\0\0\0\0\0"		/* 0x30 - 0x3f */
	"\0.\0*\0+\0\0\0\0\0/\r\0-\0"			/* 0x40 - 0x4f */
	"\0\0000123456789\0\0\0";			/* 0x50 - 0x5f */

static unsigned char xmon_shift_keytab[128] =
	"ASDFHGZXCV\000BQWER"				/* 0x00 - 0x0f */
	"YT!@#$^%+(&_*)}O"				/* 0x10 - 0x1f */
	"U{IP\rLJ\"K:|<?NM>"				/* 0x20 - 0x2f */
	"\t ~\177\0\033\0\0\0\0\0\0\0\0\0\0"		/* 0x30 - 0x3f */
	"\0.\0*\0+\0\0\0\0\0/\r\0-\0"			/* 0x40 - 0x4f */
	"\0\0000123456789\0\0\0";			/* 0x50 - 0x5f */

static int udbg_adb_local_getc(void)
{
	int k, t, on;

	xmon_wants_key = 1;
	for (;;) {
		xmon_adb_keycode = -1;
		t = 0;
		on = 0;
		k = -1;
		do {
			if (--t < 0) {
				on = 1 - on;
				btext_drawchar(on? 0xdb: 0x20);
				btext_drawchar('\b');
				t = 200000;
			}
			udbg_adb_poll();
			if (udbg_adb_old_getc_poll)
				k = udbg_adb_old_getc_poll();
		} while (k == -1 && xmon_adb_keycode == -1);
		if (on)
			btext_drawstring(" \b");
		if (k != -1)
			return k;
		k = xmon_adb_keycode;

		/* test for shift keys */
		if ((k & 0x7f) == 0x38 || (k & 0x7f) == 0x7b) {
			xmon_adb_shiftstate = (k & 0x80) == 0;
			continue;
		}
		if (k >= 0x80)
			continue;	/* ignore up transitions */
		k = (xmon_adb_shiftstate? xmon_shift_keytab: xmon_keytab)[k];
		if (k != 0)
			break;
	}
	xmon_wants_key = 0;
	return k;
}
#endif /* CONFIG_BOOTX_TEXT */

static int udbg_adb_getc(void)
{
#ifdef CONFIG_BOOTX_TEXT
	if (udbg_adb_use_btext && input_type != input_adb_none)
		return udbg_adb_local_getc();
#endif
	if (udbg_adb_old_getc)
		return udbg_adb_old_getc();
	return -1;
}

/* getc_poll() is not really used, unless you have the xmon-over modem
 * hack that doesn't quite concern us here, thus we just poll the low level
 * ADB driver to prevent it from timing out and call back the original poll
 * routine.
 */
static int udbg_adb_getc_poll(void)
{
	udbg_adb_poll();

	if (udbg_adb_old_getc_poll)
		return udbg_adb_old_getc_poll();
	return -1;
}

static void udbg_adb_putc(char c)
{
#ifdef CONFIG_BOOTX_TEXT
	if (udbg_adb_use_btext)
		btext_drawchar(c);
#endif
	if (udbg_adb_old_putc)
		return udbg_adb_old_putc(c);
}

void __init udbg_adb_init_early(void)
{
#ifdef CONFIG_BOOTX_TEXT
	if (btext_find_display(1) == 0) {
		udbg_adb_use_btext = 1;
		udbg_putc = udbg_adb_putc;
	}
#endif
}

int __init udbg_adb_init(int force_btext)
{
	struct device_node *np;

	/* Capture existing callbacks */
	udbg_adb_old_putc = udbg_putc;
	udbg_adb_old_getc = udbg_getc;
	udbg_adb_old_getc_poll = udbg_getc_poll;

	/* Check if our early init was already called */
	if (udbg_adb_old_putc == udbg_adb_putc)
		udbg_adb_old_putc = NULL;
#ifdef CONFIG_BOOTX_TEXT
	if (udbg_adb_old_putc == btext_drawchar)
		udbg_adb_old_putc = NULL;
#endif

	/* Set ours as output */
	udbg_putc = udbg_adb_putc;
	udbg_getc = udbg_adb_getc;
	udbg_getc_poll = udbg_adb_getc_poll;

#ifdef CONFIG_BOOTX_TEXT
	/* Check if we should use btext output */
	if (btext_find_display(force_btext) == 0)
		udbg_adb_use_btext = 1;
#endif

	/* See if there is a keyboard in the device tree with a parent
	 * of type "adb". If not, we return a failure, but we keep the
	 * bext output set for now
	 */
	for_each_node_by_name(np, "keyboard") {
		struct device_node *parent = of_get_parent(np);
		int found = of_node_is_type(parent, "adb");
		of_node_put(parent);
		if (found)
			break;
	}
	if (np == NULL)
		return -ENODEV;
	of_node_put(np);

#ifdef CONFIG_ADB_PMU
	if (find_via_pmu())
		input_type = input_adb_pmu;
#endif
#ifdef CONFIG_ADB_CUDA
	if (find_via_cuda())
		input_type = input_adb_cuda;
#endif

	/* Same as above: nothing found, keep btext set for output */
	if (input_type == input_adb_none)
		return -ENODEV;

	return 0;
}