blob: 2c89ce5c9ab6c555ee0cf0710429fe5026d90fc1 [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 {
33 struct tty_struct *tty;
34 struct timer_list timer;
35 spinlock_t lock;
Jiri Slabyee024d42012-03-05 14:51:58 +010036} srmcons_singleton;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38typedef union _srmcons_result {
39 struct {
40 unsigned long c :61;
41 unsigned long status :3;
42 } bits;
43 long as_long;
44} srmcons_result;
45
46/* called with callback_lock held */
47static int
48srmcons_do_receive_chars(struct tty_struct *tty)
49{
50 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) {
56 tty_insert_flip_char(tty, (char)result.bits.c, 0);
57 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;
71 unsigned long flags;
72 int incr = 10;
73
74 local_irq_save(flags);
75 if (spin_trylock(&srmcons_callback_lock)) {
76 if (!srmcons_do_receive_chars(srmconsp->tty))
77 incr = 100;
78 spin_unlock(&srmcons_callback_lock);
79 }
80
81 spin_lock(&srmconsp->lock);
Jiri Slaby5e88e6c2012-03-05 14:51:57 +010082 if (srmconsp->tty)
83 mod_timer(&srmconsp->timer, jiffies + incr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 spin_unlock(&srmconsp->lock);
85
86 local_irq_restore(flags);
87}
88
89/* called with callback_lock held */
90static int
91srmcons_do_write(struct tty_struct *tty, const char *buf, int count)
92{
93 static char str_cr[1] = "\r";
94 long c, remaining = count;
95 srmcons_result result;
96 char *cur;
97 int need_cr;
98
99 for (cur = (char *)buf; remaining > 0; ) {
100 need_cr = 0;
101 /*
102 * Break it up into reasonable size chunks to allow a chance
103 * for input to get in
104 */
105 for (c = 0; c < min_t(long, 128L, remaining) && !need_cr; c++)
106 if (cur[c] == '\n')
107 need_cr = 1;
108
109 while (c > 0) {
110 result.as_long = callback_puts(0, cur, c);
111 c -= result.bits.c;
112 remaining -= result.bits.c;
113 cur += result.bits.c;
114
115 /*
116 * Check for pending input iff a tty was provided
117 */
118 if (tty)
119 srmcons_do_receive_chars(tty);
120 }
121
122 while (need_cr) {
123 result.as_long = callback_puts(0, str_cr, 1);
124 if (result.bits.c > 0)
125 need_cr = 0;
126 }
127 }
128 return count;
129}
130
131static int
132srmcons_write(struct tty_struct *tty,
133 const unsigned char *buf, int count)
134{
135 unsigned long flags;
136
137 spin_lock_irqsave(&srmcons_callback_lock, flags);
138 srmcons_do_write(tty, (const char *) buf, count);
139 spin_unlock_irqrestore(&srmcons_callback_lock, flags);
140
141 return count;
142}
143
144static int
145srmcons_write_room(struct tty_struct *tty)
146{
147 return 512;
148}
149
150static int
151srmcons_chars_in_buffer(struct tty_struct *tty)
152{
153 return 0;
154}
155
156static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157srmcons_open(struct tty_struct *tty, struct file *filp)
158{
Jiri Slabyee024d42012-03-05 14:51:58 +0100159 struct srmcons_private *srmconsp = &srmcons_singleton;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 spin_lock_irqsave(&srmconsp->lock, flags);
163
164 if (!srmconsp->tty) {
165 tty->driver_data = srmconsp;
166
167 srmconsp->tty = tty;
Jiri Slaby5e88e6c2012-03-05 14:51:57 +0100168 mod_timer(&srmconsp->timer, jiffies + 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170
171 spin_unlock_irqrestore(&srmconsp->lock, flags);
172
173 return 0;
174}
175
176static void
177srmcons_close(struct tty_struct *tty, struct file *filp)
178{
179 struct srmcons_private *srmconsp = tty->driver_data;
180 unsigned long flags;
181
182 spin_lock_irqsave(&srmconsp->lock, flags);
183
184 if (tty->count == 1) {
185 srmconsp->tty = NULL;
186 del_timer(&srmconsp->timer);
187 }
188
189 spin_unlock_irqrestore(&srmconsp->lock, flags);
190}
191
192
193static struct tty_driver *srmcons_driver;
194
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700195static const struct tty_operations srmcons_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 .open = srmcons_open,
197 .close = srmcons_close,
198 .write = srmcons_write,
199 .write_room = srmcons_write_room,
200 .chars_in_buffer= srmcons_chars_in_buffer,
201};
202
203static int __init
204srmcons_init(void)
205{
Jiri Slabyee024d42012-03-05 14:51:58 +0100206 spin_lock_init(&srmcons_singleton.lock);
207 setup_timer(&srmcons_singleton.timer, srmcons_receive_chars,
208 (unsigned long)&srmcons_singleton);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (srm_is_registered_console) {
210 struct tty_driver *driver;
211 int err;
212
213 driver = alloc_tty_driver(MAX_SRM_CONSOLE_DEVICES);
214 if (!driver)
215 return -ENOMEM;
216 driver->driver_name = "srm";
217 driver->name = "srm";
218 driver->major = 0; /* dynamic */
219 driver->minor_start = 0;
220 driver->type = TTY_DRIVER_TYPE_SYSTEM;
221 driver->subtype = SYSTEM_TYPE_SYSCONS;
222 driver->init_termios = tty_std_termios;
223 tty_set_operations(driver, &srmcons_ops);
224 err = tty_register_driver(driver);
225 if (err) {
226 put_tty_driver(driver);
227 return err;
228 }
229 srmcons_driver = driver;
230 }
231
232 return -ENODEV;
233}
234
235module_init(srmcons_init);
236
237
238/*
239 * The console driver
240 */
241static void
242srm_console_write(struct console *co, const char *s, unsigned count)
243{
244 unsigned long flags;
245
246 spin_lock_irqsave(&srmcons_callback_lock, flags);
247 srmcons_do_write(NULL, s, count);
248 spin_unlock_irqrestore(&srmcons_callback_lock, flags);
249}
250
251static struct tty_driver *
252srm_console_device(struct console *co, int *index)
253{
254 *index = co->index;
255 return srmcons_driver;
256}
257
Sam Ravnborgebaf4fc2007-07-15 23:38:37 -0700258static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259srm_console_setup(struct console *co, char *options)
260{
261 return 0;
262}
263
264static struct console srmcons = {
265 .name = "srm",
266 .write = srm_console_write,
267 .device = srm_console_device,
268 .setup = srm_console_setup,
Gerd Hoffmann69331af2007-05-08 00:26:49 -0700269 .flags = CON_PRINTBUFFER | CON_BOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 .index = -1,
271};
272
273void __init
274register_srm_console(void)
275{
276 if (!srm_is_registered_console) {
277 callback_open_console();
278 register_console(&srmcons);
279 srm_is_registered_console = 1;
280 }
281}
282
283void __init
284unregister_srm_console(void)
285{
286 if (srm_is_registered_console) {
287 callback_close_console();
288 unregister_console(&srmcons);
289 srm_is_registered_console = 0;
290 }
291}