blob: 21b57a66e809ae9168ef4ba06fb715c158db3c40 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/alpha/kernel/srmcons.c
3 *
4 * Callback based driver for SRM Console console device.
5 * (TTY driver and console driver)
6 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/kernel.h>
9#include <linux/init.h>
10#include <linux/console.h>
11#include <linux/delay.h>
12#include <linux/mm.h>
13#include <linux/slab.h>
14#include <linux/spinlock.h>
15#include <linux/timer.h>
16#include <linux/tty.h>
17#include <linux/tty_driver.h>
18#include <linux/tty_flip.h>
19
20#include <asm/console.h>
21#include <asm/uaccess.h>
22
23
24static DEFINE_SPINLOCK(srmcons_callback_lock);
25static int srm_is_registered_console = 0;
26
27/*
28 * The TTY driver
29 */
30#define MAX_SRM_CONSOLE_DEVICES 1 /* only support 1 console device */
31
32struct srmcons_private {
Jiri Slaby54089d42012-03-05 14:51:59 +010033 struct tty_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 struct timer_list timer;
Jiri Slabyee024d42012-03-05 14:51:58 +010035} srmcons_singleton;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37typedef union _srmcons_result {
38 struct {
39 unsigned long c :61;
40 unsigned long status :3;
41 } bits;
42 long as_long;
43} srmcons_result;
44
45/* called with callback_lock held */
46static int
47srmcons_do_receive_chars(struct tty_struct *tty)
48{
Jiri Slaby92a19f92013-01-03 15:53:03 +010049 struct tty_port *port = tty->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 srmcons_result result;
51 int count = 0, loops = 0;
52
53 do {
54 result.as_long = callback_getc(0);
55 if (result.bits.status < 2) {
Jiri Slaby92a19f92013-01-03 15:53:03 +010056 tty_insert_flip_char(port, (char)result.bits.c, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 count++;
58 }
59 } while((result.bits.status & 1) && (++loops < 10));
60
61 if (count)
62 tty_schedule_flip(tty);
63
64 return count;
65}
66
67static void
68srmcons_receive_chars(unsigned long data)
69{
70 struct srmcons_private *srmconsp = (struct srmcons_private *)data;
Jiri Slaby54089d42012-03-05 14:51:59 +010071 struct tty_port *port = &srmconsp->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 unsigned long flags;
73 int incr = 10;
74
75 local_irq_save(flags);
76 if (spin_trylock(&srmcons_callback_lock)) {
Jiri Slaby54089d42012-03-05 14:51:59 +010077 if (!srmcons_do_receive_chars(port->tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 incr = 100;
79 spin_unlock(&srmcons_callback_lock);
80 }
81
Jiri Slaby54089d42012-03-05 14:51:59 +010082 spin_lock(&port->lock);
83 if (port->tty)
Jiri Slaby5e88e6c2012-03-05 14:51:57 +010084 mod_timer(&srmconsp->timer, jiffies + incr);
Jiri Slaby54089d42012-03-05 14:51:59 +010085 spin_unlock(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 local_irq_restore(flags);
88}
89
90/* called with callback_lock held */
91static int
92srmcons_do_write(struct tty_struct *tty, const char *buf, int count)
93{
94 static char str_cr[1] = "\r";
95 long c, remaining = count;
96 srmcons_result result;
97 char *cur;
98 int need_cr;
99
100 for (cur = (char *)buf; remaining > 0; ) {
101 need_cr = 0;
102 /*
103 * Break it up into reasonable size chunks to allow a chance
104 * for input to get in
105 */
106 for (c = 0; c < min_t(long, 128L, remaining) && !need_cr; c++)
107 if (cur[c] == '\n')
108 need_cr = 1;
109
110 while (c > 0) {
111 result.as_long = callback_puts(0, cur, c);
112 c -= result.bits.c;
113 remaining -= result.bits.c;
114 cur += result.bits.c;
115
116 /*
117 * Check for pending input iff a tty was provided
118 */
119 if (tty)
120 srmcons_do_receive_chars(tty);
121 }
122
123 while (need_cr) {
124 result.as_long = callback_puts(0, str_cr, 1);
125 if (result.bits.c > 0)
126 need_cr = 0;
127 }
128 }
129 return count;
130}
131
132static int
133srmcons_write(struct tty_struct *tty,
134 const unsigned char *buf, int count)
135{
136 unsigned long flags;
137
138 spin_lock_irqsave(&srmcons_callback_lock, flags);
139 srmcons_do_write(tty, (const char *) buf, count);
140 spin_unlock_irqrestore(&srmcons_callback_lock, flags);
141
142 return count;
143}
144
145static int
146srmcons_write_room(struct tty_struct *tty)
147{
148 return 512;
149}
150
151static int
152srmcons_chars_in_buffer(struct tty_struct *tty)
153{
154 return 0;
155}
156
157static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158srmcons_open(struct tty_struct *tty, struct file *filp)
159{
Jiri Slabyee024d42012-03-05 14:51:58 +0100160 struct srmcons_private *srmconsp = &srmcons_singleton;
Jiri Slaby54089d42012-03-05 14:51:59 +0100161 struct tty_port *port = &srmconsp->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Jiri Slaby54089d42012-03-05 14:51:59 +0100164 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Jiri Slaby54089d42012-03-05 14:51:59 +0100166 if (!port->tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 tty->driver_data = srmconsp;
Jiri Slaby54089d42012-03-05 14:51:59 +0100168 tty->port = port;
169 port->tty = tty; /* XXX proper refcounting */
Jiri Slaby5e88e6c2012-03-05 14:51:57 +0100170 mod_timer(&srmconsp->timer, jiffies + 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172
Jiri Slaby54089d42012-03-05 14:51:59 +0100173 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 return 0;
176}
177
178static void
179srmcons_close(struct tty_struct *tty, struct file *filp)
180{
181 struct srmcons_private *srmconsp = tty->driver_data;
Jiri Slaby54089d42012-03-05 14:51:59 +0100182 struct tty_port *port = &srmconsp->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 unsigned long flags;
184
Jiri Slaby54089d42012-03-05 14:51:59 +0100185 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 if (tty->count == 1) {
Jiri Slaby54089d42012-03-05 14:51:59 +0100188 port->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 del_timer(&srmconsp->timer);
190 }
191
Jiri Slaby54089d42012-03-05 14:51:59 +0100192 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195
196static struct tty_driver *srmcons_driver;
197
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700198static const struct tty_operations srmcons_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 .open = srmcons_open,
200 .close = srmcons_close,
201 .write = srmcons_write,
202 .write_room = srmcons_write_room,
203 .chars_in_buffer= srmcons_chars_in_buffer,
204};
205
206static int __init
207srmcons_init(void)
208{
Jiri Slabyee024d42012-03-05 14:51:58 +0100209 setup_timer(&srmcons_singleton.timer, srmcons_receive_chars,
210 (unsigned long)&srmcons_singleton);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (srm_is_registered_console) {
212 struct tty_driver *driver;
213 int err;
214
215 driver = alloc_tty_driver(MAX_SRM_CONSOLE_DEVICES);
216 if (!driver)
217 return -ENOMEM;
Jiri Slaby191c5f12012-11-15 09:49:56 +0100218
219 tty_port_init(&srmcons_singleton.port);
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 driver->driver_name = "srm";
222 driver->name = "srm";
223 driver->major = 0; /* dynamic */
224 driver->minor_start = 0;
225 driver->type = TTY_DRIVER_TYPE_SYSTEM;
226 driver->subtype = SYSTEM_TYPE_SYSCONS;
227 driver->init_termios = tty_std_termios;
228 tty_set_operations(driver, &srmcons_ops);
Jiri Slabyb19e2ca2012-08-07 21:47:51 +0200229 tty_port_link_device(&srmcons_singleton.port, driver, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 err = tty_register_driver(driver);
231 if (err) {
232 put_tty_driver(driver);
Jiri Slaby191c5f12012-11-15 09:49:56 +0100233 tty_port_destroy(&srmcons_singleton.port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return err;
235 }
236 srmcons_driver = driver;
237 }
238
239 return -ENODEV;
240}
241
242module_init(srmcons_init);
243
244
245/*
246 * The console driver
247 */
248static void
249srm_console_write(struct console *co, const char *s, unsigned count)
250{
251 unsigned long flags;
252
253 spin_lock_irqsave(&srmcons_callback_lock, flags);
254 srmcons_do_write(NULL, s, count);
255 spin_unlock_irqrestore(&srmcons_callback_lock, flags);
256}
257
258static struct tty_driver *
259srm_console_device(struct console *co, int *index)
260{
261 *index = co->index;
262 return srmcons_driver;
263}
264
Sam Ravnborgebaf4fc2007-07-15 23:38:37 -0700265static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266srm_console_setup(struct console *co, char *options)
267{
268 return 0;
269}
270
271static struct console srmcons = {
272 .name = "srm",
273 .write = srm_console_write,
274 .device = srm_console_device,
275 .setup = srm_console_setup,
Gerd Hoffmann69331af2007-05-08 00:26:49 -0700276 .flags = CON_PRINTBUFFER | CON_BOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 .index = -1,
278};
279
280void __init
281register_srm_console(void)
282{
283 if (!srm_is_registered_console) {
284 callback_open_console();
285 register_console(&srmcons);
286 srm_is_registered_console = 1;
287 }
288}
289
290void __init
291unregister_srm_console(void)
292{
293 if (srm_is_registered_console) {
294 callback_close_console();
295 unregister_console(&srmcons);
296 srm_is_registered_console = 0;
297 }
298}