summaryrefslogtreecommitdiffstats
path: root/target/linux/goldfish/patches-2.6.30/0122--ARM-goldfish-tty-Adding-tty-driver-for-goldfish.patch
blob: e8767e6bdda4701be3292e636287b232742eeafd (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
From 4ff5a10b94c0c41088c8bbfa5be1ebab3822371b Mon Sep 17 00:00:00 2001
From: =?utf-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?= <arve@google.com>
Date: Fri, 29 Jun 2007 21:41:20 -0700
Subject: [PATCH 122/134] [ARM] goldfish: tty: Adding tty driver for goldfish.
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Mike A. Chan <mikechan@google.com>
Signed-off-by: Arve Hjønnevåg <arve@android.com>
---
 drivers/char/Kconfig        |    6 +
 drivers/char/Makefile       |    1 +
 drivers/char/goldfish_tty.c |  323 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 330 insertions(+), 0 deletions(-)
 create mode 100644 drivers/char/goldfish_tty.c

--- a/drivers/char/Kconfig
+++ b/drivers/char/Kconfig
@@ -1106,6 +1106,12 @@ config DEVPORT
 	depends on ISA || PCI
 	default y
 
+config GOLDFISH_TTY
+	tristate "Goldfish TTY Driver"
+	default n
+	help
+	  TTY driver for Goldfish Virtual Platform.
+
 source "drivers/s390/char/Kconfig"
 
 endmenu
--- a/drivers/char/Makefile
+++ b/drivers/char/Makefile
@@ -98,6 +98,7 @@ obj-$(CONFIG_GPIO_DEVICE)	+= gpio_dev.o
 obj-$(CONFIG_GPIO_VR41XX)	+= vr41xx_giu.o
 obj-$(CONFIG_GPIO_TB0219)	+= tb0219.o
 obj-$(CONFIG_TELCLOCK)		+= tlclk.o
+obj-$(CONFIG_GOLDFISH_TTY)	+= goldfish_tty.o
 
 obj-$(CONFIG_MWAVE)		+= mwave/
 obj-$(CONFIG_AGP)		+= agp/
--- /dev/null
+++ b/drivers/char/goldfish_tty.c
@@ -0,0 +1,323 @@
+/* drivers/char/goldfish_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/console.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+#include <linux/tty.h>
+#include <linux/tty_flip.h>
+
+#include <mach/hardware.h>
+#include <asm/io.h>
+
+enum {
+	GOLDFISH_TTY_PUT_CHAR       = 0x00,
+	GOLDFISH_TTY_BYTES_READY    = 0x04,
+	GOLDFISH_TTY_CMD            = 0x08,
+
+	GOLDFISH_TTY_DATA_PTR       = 0x10,
+	GOLDFISH_TTY_DATA_LEN       = 0x14,
+
+	GOLDFISH_TTY_CMD_INT_DISABLE    = 0,
+	GOLDFISH_TTY_CMD_INT_ENABLE     = 1,
+	GOLDFISH_TTY_CMD_WRITE_BUFFER   = 2,
+	GOLDFISH_TTY_CMD_READ_BUFFER    = 3,
+};
+
+struct goldfish_tty {
+	spinlock_t lock;
+	uint32_t base;
+	uint32_t irq;
+	int opencount;
+	struct tty_struct *tty;
+	struct console console;
+};
+
+static DEFINE_MUTEX(goldfish_tty_lock);
+static struct tty_driver *goldfish_tty_driver;
+static uint32_t goldfish_tty_line_count = 8;
+static uint32_t goldfish_tty_current_line_count;
+static struct goldfish_tty *goldfish_ttys;
+
+static void goldfish_tty_do_write(int line, const char *buf, unsigned count)
+{
+	unsigned long irq_flags;
+	struct goldfish_tty *qtty = &goldfish_ttys[line];
+	uint32_t base = qtty->base;
+	spin_lock_irqsave(&qtty->lock, irq_flags);
+	writel(buf, base + GOLDFISH_TTY_DATA_PTR);
+	writel(count, base + GOLDFISH_TTY_DATA_LEN);
+	writel(GOLDFISH_TTY_CMD_WRITE_BUFFER, base + GOLDFISH_TTY_CMD);
+	spin_unlock_irqrestore(&qtty->lock, irq_flags);
+}
+
+static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
+{
+	struct platform_device *pdev = dev_id;
+	struct goldfish_tty *qtty = &goldfish_ttys[pdev->id];
+	uint32_t base = qtty->base;
+	unsigned long irq_flags;
+	unsigned char *buf;
+	uint32_t count;
+
+	count = readl(base + GOLDFISH_TTY_BYTES_READY);
+	if(count == 0) {
+		return IRQ_NONE;
+	}
+	count = tty_prepare_flip_string(qtty->tty, &buf, count);
+	spin_lock_irqsave(&qtty->lock, irq_flags);
+	writel(buf, base + GOLDFISH_TTY_DATA_PTR);
+	writel(count, base + GOLDFISH_TTY_DATA_LEN);
+	writel(GOLDFISH_TTY_CMD_READ_BUFFER, base + GOLDFISH_TTY_CMD);
+	spin_unlock_irqrestore(&qtty->lock, irq_flags);
+	tty_schedule_flip(qtty->tty);
+	return IRQ_HANDLED;
+}
+
+static int goldfish_tty_open(struct tty_struct * tty, struct file * filp)
+{
+	int ret;
+	struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
+
+	mutex_lock(&goldfish_tty_lock);
+	if(qtty->tty == NULL || qtty->tty == tty) {
+		if(qtty->opencount++ == 0) {
+			qtty->tty = tty;
+			writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_CMD);
+		}
+		ret = 0;
+	}
+	else
+		ret = -EBUSY;
+	mutex_unlock(&goldfish_tty_lock);
+	return ret;
+}
+
+static void goldfish_tty_close(struct tty_struct * tty, struct file * filp)
+{
+	struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
+
+	mutex_lock(&goldfish_tty_lock);
+	if(qtty->tty == tty) {
+		if(--qtty->opencount == 0) {
+			writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_CMD);
+			qtty->tty = NULL;
+		}
+	}
+	mutex_unlock(&goldfish_tty_lock);
+}
+
+static int goldfish_tty_write(struct tty_struct * tty, const unsigned char *buf, int count)
+{
+	goldfish_tty_do_write(tty->index, buf, count);
+	return count;
+}
+
+static int goldfish_tty_write_room(struct tty_struct *tty)
+{
+	return 0x10000;
+}
+
+static int goldfish_tty_chars_in_buffer(struct tty_struct *tty)
+{
+	struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
+	uint32_t base = qtty->base;
+	return readl(base + GOLDFISH_TTY_BYTES_READY);
+}
+
+static void goldfish_tty_console_write(struct console *co, const char *b, unsigned count)
+{
+	goldfish_tty_do_write(co->index, b, count);
+}
+
+static struct tty_driver *goldfish_tty_console_device(struct console *c, int *index)
+{
+	*index = c->index;
+	return goldfish_tty_driver;
+}
+
+static int goldfish_tty_console_setup(struct console *co, char *options)
+{
+	if((unsigned)co->index > goldfish_tty_line_count)
+		return -ENODEV;
+	if(goldfish_ttys[co->index].base == 0)
+		return -ENODEV;
+	return 0;
+}
+
+static struct tty_operations goldfish_tty_ops = {
+	.open = goldfish_tty_open,
+	.close = goldfish_tty_close,
+	.write = goldfish_tty_write,
+	.write_room = goldfish_tty_write_room,
+	.chars_in_buffer = goldfish_tty_chars_in_buffer,
+};
+
+static int __devinit goldfish_tty_create_driver(void)
+{
+	int ret;
+	struct tty_driver *tty;
+
+	goldfish_ttys = kzalloc(sizeof(*goldfish_ttys) * goldfish_tty_line_count, GFP_KERNEL);
+	if(goldfish_ttys == NULL) {
+		ret = -ENOMEM;
+		goto err_alloc_goldfish_ttys_failed;
+	}
+
+	tty = alloc_tty_driver(goldfish_tty_line_count);
+	if(tty == NULL) {
+		ret = -ENOMEM;
+		goto err_alloc_tty_driver_failed;
+	}
+	tty->driver_name = "goldfish";
+	tty->name = "ttyS";
+	tty->type = TTY_DRIVER_TYPE_SERIAL;
+	tty->subtype = SERIAL_TYPE_NORMAL;
+	tty->init_termios = tty_std_termios;
+	tty->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
+	tty_set_operations(tty, &goldfish_tty_ops);
+	ret = tty_register_driver(tty);
+	if(ret)
+		goto err_tty_register_driver_failed;
+
+	goldfish_tty_driver = tty;
+	return 0;
+
+err_tty_register_driver_failed:
+	put_tty_driver(tty);
+err_alloc_tty_driver_failed:
+	kfree(goldfish_ttys);
+	goldfish_ttys = NULL;
+err_alloc_goldfish_ttys_failed:
+	return ret;
+}
+
+static void goldfish_tty_delete_driver(void)
+{
+	tty_unregister_driver(goldfish_tty_driver);
+	put_tty_driver(goldfish_tty_driver);
+	goldfish_tty_driver = NULL;
+	kfree(goldfish_ttys);
+	goldfish_ttys = NULL;
+}
+
+static int __devinit goldfish_tty_probe(struct platform_device *pdev)
+{
+	int ret;
+	int i;
+	struct resource *r;
+	struct device *ttydev;
+	uint32_t base;
+	uint32_t irq;
+
+	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if(r == NULL)
+		return -EINVAL;
+	base = IO_ADDRESS(r->start - IO_START);
+	r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
+	if(r == NULL)
+		return -EINVAL;
+	irq = r->start;
+
+	if(pdev->id >= goldfish_tty_line_count)
+		return -EINVAL;
+
+	mutex_lock(&goldfish_tty_lock);
+	if(goldfish_tty_current_line_count == 0) {
+		ret = goldfish_tty_create_driver();
+		if(ret)
+			goto err_create_driver_failed;
+	}
+	goldfish_tty_current_line_count++;
+
+	spin_lock_init(&goldfish_ttys[pdev->id].lock);
+	goldfish_ttys[pdev->id].base = base;
+	goldfish_ttys[pdev->id].irq = irq;
+
+	writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_CMD);
+
+	ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED, "goldfish_tty", pdev);
+	if(ret)
+		goto err_request_irq_failed;
+
+
+	ttydev = tty_register_device(goldfish_tty_driver, pdev->id, NULL);
+	if(IS_ERR(ttydev)) {
+		ret = PTR_ERR(ttydev);
+		goto err_tty_register_device_failed;
+	}
+
+	strcpy(goldfish_ttys[pdev->id].console.name, "ttyS");
+	goldfish_ttys[pdev->id].console.write		= goldfish_tty_console_write;
+	goldfish_ttys[pdev->id].console.device		= goldfish_tty_console_device;
+	goldfish_ttys[pdev->id].console.setup		= goldfish_tty_console_setup;
+	goldfish_ttys[pdev->id].console.flags		= CON_PRINTBUFFER;
+	goldfish_ttys[pdev->id].console.index		= pdev->id;
+	register_console(&goldfish_ttys[pdev->id].console);
+
+
+	mutex_unlock(&goldfish_tty_lock);
+
+	return 0;
+
+	tty_unregister_device(goldfish_tty_driver, i);
+err_tty_register_device_failed:
+	free_irq(irq, pdev);
+err_request_irq_failed:
+	goldfish_tty_current_line_count--;
+	if(goldfish_tty_current_line_count == 0) {
+		goldfish_tty_delete_driver();
+	}
+err_create_driver_failed:
+	mutex_unlock(&goldfish_tty_lock);
+	return ret;
+}
+
+static int __devexit goldfish_tty_remove(struct platform_device *pdev)
+{
+	mutex_lock(&goldfish_tty_lock);
+	unregister_console(&goldfish_ttys[pdev->id].console);
+	tty_unregister_device(goldfish_tty_driver, pdev->id);
+	goldfish_ttys[pdev->id].base = 0;
+	free_irq(goldfish_ttys[pdev->id].irq, pdev);
+	goldfish_tty_current_line_count--;
+	if(goldfish_tty_current_line_count == 0) {
+		goldfish_tty_delete_driver();
+	}
+	mutex_unlock(&goldfish_tty_lock);
+	return 0;
+}
+
+static struct platform_driver goldfish_tty_platform_driver = {
+	.probe = goldfish_tty_probe,
+	.remove = __devexit_p(goldfish_tty_remove),
+	.driver = {
+		.name = "goldfish_tty"
+	}
+};
+
+static int __init goldfish_tty_init(void)
+{
+	return platform_driver_register(&goldfish_tty_platform_driver);
+}
+
+static void __exit goldfish_tty_exit(void)
+{
+	platform_driver_unregister(&goldfish_tty_platform_driver);
+}
+
+module_init(goldfish_tty_init);
+module_exit(goldfish_tty_exit);