summaryrefslogtreecommitdiff
path: root/drivers/char/dcc_tty.c
blob: a787accdcb14b2556bcb59ea5693c1d04e198307 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* drivers/char/dcc_tty.c
 *
 * Copyright (C) 2007 Google, Inc.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/delay.h>
#include <linux/console.h>
#include <linux/hrtimer.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>

MODULE_DESCRIPTION("DCC TTY Driver");
MODULE_LICENSE("GPL");
MODULE_VERSION("1.0");

static spinlock_t g_dcc_tty_lock = SPIN_LOCK_UNLOCKED;
static struct hrtimer g_dcc_timer;
static char g_dcc_buffer[16];
static int g_dcc_buffer_head;
static int g_dcc_buffer_count;
static unsigned g_dcc_write_delay_usecs = 1;
static struct tty_driver *g_dcc_tty_driver;
static struct tty_struct *g_dcc_tty;
static int g_dcc_tty_open_count;

static void dcc_poll_locked(void)
{
	char ch;
	int rch;
	int written;

	while (g_dcc_buffer_count) {
		ch = g_dcc_buffer[g_dcc_buffer_head];
		asm(
			"mrc 14, 0, r15, c0, c1, 0\n"
			"mcrcc 14, 0, %1, c0, c5, 0\n"
			"movcc %0, #1\n"
			"movcs %0, #0\n"
			: "=r" (written)
			: "r" (ch)
		);
		if (written) {
			if (ch == '\n')
				g_dcc_buffer[g_dcc_buffer_head] = '\r';
			else {
				g_dcc_buffer_head = (g_dcc_buffer_head + 1) % ARRAY_SIZE(g_dcc_buffer);
				g_dcc_buffer_count--;
				if (g_dcc_tty)
					tty_wakeup(g_dcc_tty);
			}
			g_dcc_write_delay_usecs = 1;
		} else {
			if (g_dcc_write_delay_usecs > 0x100)
				break;
			g_dcc_write_delay_usecs <<= 1;
			udelay(g_dcc_write_delay_usecs);
		}
	}

	if (g_dcc_tty && !test_bit(TTY_THROTTLED, &g_dcc_tty->flags)) {
		asm(
			"mrc 14, 0, %0, c0, c1, 0\n"
			"tst %0, #(1 << 30)\n"
			"moveq %0, #-1\n"
			"mrcne 14, 0, %0, c0, c5, 0\n"
			: "=r" (rch)
		);
		if (rch >= 0) {
			ch = rch;
			tty_insert_flip_string(g_dcc_tty, &ch, 1);
			tty_flip_buffer_push(g_dcc_tty);
		}
	}


	if (g_dcc_buffer_count)
		hrtimer_start(&g_dcc_timer, ktime_set(0, g_dcc_write_delay_usecs * NSEC_PER_USEC), HRTIMER_MODE_REL);
	else
		hrtimer_start(&g_dcc_timer, ktime_set(0, 20 * NSEC_PER_MSEC), HRTIMER_MODE_REL);
}

static int dcc_tty_open(struct tty_struct * tty, struct file * filp)
{
	int ret;
	unsigned long irq_flags;

	spin_lock_irqsave(&g_dcc_tty_lock, irq_flags);
	if (g_dcc_tty == NULL || g_dcc_tty == tty) {
		g_dcc_tty = tty;
		g_dcc_tty_open_count++;
		ret = 0;
	} else
		ret = -EBUSY;
	spin_unlock_irqrestore(&g_dcc_tty_lock, irq_flags);

	printk("dcc_tty_open, tty %p, f_flags %x, returned %d\n", tty, filp->f_flags, ret);

	return ret;
}

static void dcc_tty_close(struct tty_struct * tty, struct file * filp)
{
	printk("dcc_tty_close, tty %p, f_flags %x\n", tty, filp->f_flags);
	if (g_dcc_tty == tty) {
		if (--g_dcc_tty_open_count == 0)
			g_dcc_tty = NULL;
	}
}

static int dcc_write(const unsigned char *buf_start, int count)
{
	const unsigned char *buf = buf_start;
	unsigned long irq_flags;
	int copy_len;
	int space_left;
	int tail;

	if (count < 1)
		return 0;

	spin_lock_irqsave(&g_dcc_tty_lock, irq_flags);
	do {
		tail = (g_dcc_buffer_head + g_dcc_buffer_count) % ARRAY_SIZE(g_dcc_buffer);
		copy_len = ARRAY_SIZE(g_dcc_buffer) - tail;
		space_left = ARRAY_SIZE(g_dcc_buffer) - g_dcc_buffer_count;
		if (copy_len > space_left)
			copy_len = space_left;
		if (copy_len > count)
			copy_len = count;
		memcpy(&g_dcc_buffer[tail], buf, copy_len);
		g_dcc_buffer_count += copy_len;
		buf += copy_len;
		count -= copy_len;
		if (copy_len < count && copy_len < space_left) {
			space_left -= copy_len;
			copy_len = count;
			if (copy_len > space_left) {
				copy_len = space_left;
			}
			memcpy(g_dcc_buffer, buf, copy_len);
			buf += copy_len;
			count -= copy_len;
			g_dcc_buffer_count += copy_len;
		}
		dcc_poll_locked();
		space_left = ARRAY_SIZE(g_dcc_buffer) - g_dcc_buffer_count;
	} while(count && space_left);
	spin_unlock_irqrestore(&g_dcc_tty_lock, irq_flags);
	return buf - buf_start;
}

static int dcc_tty_write(struct tty_struct * tty, const unsigned char *buf, int count)
{
	int ret;
	/* printk("dcc_tty_write %p, %d\n", buf, count); */
	ret = dcc_write(buf, count);
	if (ret != count)
		printk("dcc_tty_write %p, %d, returned %d\n", buf, count, ret);
	return ret;
}

static int dcc_tty_write_room(struct tty_struct *tty)
{
	int space_left;
	unsigned long irq_flags;

	spin_lock_irqsave(&g_dcc_tty_lock, irq_flags);
	space_left = ARRAY_SIZE(g_dcc_buffer) - g_dcc_buffer_count;
	spin_unlock_irqrestore(&g_dcc_tty_lock, irq_flags);
	return space_left;
}

static int dcc_tty_chars_in_buffer(struct tty_struct *tty)
{
	int ret;
	asm(
		"mrc 14, 0, %0, c0, c1, 0\n"
		"mov %0, %0, LSR #30\n"
		"and %0, %0, #1\n"
		: "=r" (ret)
	);
	return ret;
}

static void dcc_tty_unthrottle(struct tty_struct * tty)
{
	unsigned long irq_flags;

	spin_lock_irqsave(&g_dcc_tty_lock, irq_flags);
	dcc_poll_locked();
	spin_unlock_irqrestore(&g_dcc_tty_lock, irq_flags);
}

static enum hrtimer_restart dcc_tty_timer_func(struct hrtimer *timer)
{
	unsigned long irq_flags;

	spin_lock_irqsave(&g_dcc_tty_lock, irq_flags);
	dcc_poll_locked();
	spin_unlock_irqrestore(&g_dcc_tty_lock, irq_flags);
	return HRTIMER_NORESTART;
}

void dcc_console_write(struct console *co, const char *b, unsigned count)
{
#if 1
	dcc_write(b, count);
#else
	/* blocking printk */
	while (count > 0) {
		int written;
		written = dcc_write(b, count);
		if (written) {
			b += written;
			count -= written;
		}
	}
#endif
}

static struct tty_driver *dcc_console_device(struct console *c, int *index)
{
	*index = 0;
	return g_dcc_tty_driver;
}

static int __init dcc_console_setup(struct console *co, char *options)
{
	if (co->index != 0)
		return -ENODEV;
	return 0;
}


static struct console dcc_console =
{
	.name		= "ttyDCC",
	.write		= dcc_console_write,
	.device		= dcc_console_device,
	.setup		= dcc_console_setup,
	.flags		= CON_PRINTBUFFER,
	.index		= -1,
};

static struct tty_operations dcc_tty_ops = {
	.open = dcc_tty_open,
	.close = dcc_tty_close,
	.write = dcc_tty_write,
	.write_room = dcc_tty_write_room,
	.chars_in_buffer = dcc_tty_chars_in_buffer,
	.unthrottle = dcc_tty_unthrottle,
};

static int __init dcc_tty_init(void)
{
	int ret;

	hrtimer_init(&g_dcc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
	g_dcc_timer.function = dcc_tty_timer_func;

	g_dcc_tty_driver = alloc_tty_driver(1);
	if (!g_dcc_tty_driver) {
		printk(KERN_ERR "dcc_tty_probe: alloc_tty_driver failed\n");
		ret = -ENOMEM;
		goto err_alloc_tty_driver_failed;
	}
	g_dcc_tty_driver->owner = THIS_MODULE;
	g_dcc_tty_driver->driver_name = "dcc";
	g_dcc_tty_driver->name = "ttyDCC";
	g_dcc_tty_driver->major = 0; // auto assign
	g_dcc_tty_driver->minor_start = 0;
	g_dcc_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
	g_dcc_tty_driver->subtype = SERIAL_TYPE_NORMAL;
	g_dcc_tty_driver->init_termios = tty_std_termios;
	g_dcc_tty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
	tty_set_operations(g_dcc_tty_driver, &dcc_tty_ops);
	ret = tty_register_driver(g_dcc_tty_driver);
	if (ret) {
		printk(KERN_ERR "dcc_tty_probe: tty_register_driver failed, %d\n", ret);
		goto err_tty_register_driver_failed;
	}
	tty_register_device(g_dcc_tty_driver, 0, NULL);

	register_console(&dcc_console);
	hrtimer_start(&g_dcc_timer, ktime_set(0, 0), HRTIMER_MODE_REL);

	return 0;

err_tty_register_driver_failed:
	put_tty_driver(g_dcc_tty_driver);
	g_dcc_tty_driver = NULL;
err_alloc_tty_driver_failed:
	return ret;
}

static void  __exit dcc_tty_exit(void)
{
	int ret;

	tty_unregister_device(g_dcc_tty_driver, 0);
	ret = tty_unregister_driver(g_dcc_tty_driver);
	if (ret < 0) {
		printk(KERN_ERR "dcc_tty_remove: tty_unregister_driver failed, %d\n", ret);
	} else {
		put_tty_driver(g_dcc_tty_driver);
	}
	g_dcc_tty_driver = NULL;
}

module_init(dcc_tty_init);
module_exit(dcc_tty_exit);