blob: f1fdf178bcb4cc0658376f3b5668b66ce1b520f3 [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;
36};
37
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
157srmcons_get_private_struct(struct srmcons_private **ps)
158{
159 static struct srmcons_private *srmconsp = NULL;
160 static DEFINE_SPINLOCK(srmconsp_lock);
161 unsigned long flags;
162 int retval = 0;
163
164 if (srmconsp == NULL) {
Andrew Morton2af0bc92007-05-06 14:50:39 -0700165 srmconsp = kmalloc(sizeof(*srmconsp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 spin_lock_irqsave(&srmconsp_lock, flags);
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (srmconsp == NULL)
169 retval = -ENOMEM;
170 else {
171 srmconsp->tty = NULL;
172 spin_lock_init(&srmconsp->lock);
Jiri Slaby5e88e6c2012-03-05 14:51:57 +0100173 setup_timer(&srmconsp->timer, srmcons_receive_chars,
174 (unsigned long)srmconsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176
177 spin_unlock_irqrestore(&srmconsp_lock, flags);
178 }
179
180 *ps = srmconsp;
181 return retval;
182}
183
184static int
185srmcons_open(struct tty_struct *tty, struct file *filp)
186{
187 struct srmcons_private *srmconsp;
188 unsigned long flags;
189 int retval;
190
191 retval = srmcons_get_private_struct(&srmconsp);
192 if (retval)
193 return retval;
194
195 spin_lock_irqsave(&srmconsp->lock, flags);
196
197 if (!srmconsp->tty) {
198 tty->driver_data = srmconsp;
199
200 srmconsp->tty = tty;
Jiri Slaby5e88e6c2012-03-05 14:51:57 +0100201 mod_timer(&srmconsp->timer, jiffies + 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203
204 spin_unlock_irqrestore(&srmconsp->lock, flags);
205
206 return 0;
207}
208
209static void
210srmcons_close(struct tty_struct *tty, struct file *filp)
211{
212 struct srmcons_private *srmconsp = tty->driver_data;
213 unsigned long flags;
214
215 spin_lock_irqsave(&srmconsp->lock, flags);
216
217 if (tty->count == 1) {
218 srmconsp->tty = NULL;
219 del_timer(&srmconsp->timer);
220 }
221
222 spin_unlock_irqrestore(&srmconsp->lock, flags);
223}
224
225
226static struct tty_driver *srmcons_driver;
227
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700228static const struct tty_operations srmcons_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 .open = srmcons_open,
230 .close = srmcons_close,
231 .write = srmcons_write,
232 .write_room = srmcons_write_room,
233 .chars_in_buffer= srmcons_chars_in_buffer,
234};
235
236static int __init
237srmcons_init(void)
238{
239 if (srm_is_registered_console) {
240 struct tty_driver *driver;
241 int err;
242
243 driver = alloc_tty_driver(MAX_SRM_CONSOLE_DEVICES);
244 if (!driver)
245 return -ENOMEM;
246 driver->driver_name = "srm";
247 driver->name = "srm";
248 driver->major = 0; /* dynamic */
249 driver->minor_start = 0;
250 driver->type = TTY_DRIVER_TYPE_SYSTEM;
251 driver->subtype = SYSTEM_TYPE_SYSCONS;
252 driver->init_termios = tty_std_termios;
253 tty_set_operations(driver, &srmcons_ops);
254 err = tty_register_driver(driver);
255 if (err) {
256 put_tty_driver(driver);
257 return err;
258 }
259 srmcons_driver = driver;
260 }
261
262 return -ENODEV;
263}
264
265module_init(srmcons_init);
266
267
268/*
269 * The console driver
270 */
271static void
272srm_console_write(struct console *co, const char *s, unsigned count)
273{
274 unsigned long flags;
275
276 spin_lock_irqsave(&srmcons_callback_lock, flags);
277 srmcons_do_write(NULL, s, count);
278 spin_unlock_irqrestore(&srmcons_callback_lock, flags);
279}
280
281static struct tty_driver *
282srm_console_device(struct console *co, int *index)
283{
284 *index = co->index;
285 return srmcons_driver;
286}
287
Sam Ravnborgebaf4fc2007-07-15 23:38:37 -0700288static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289srm_console_setup(struct console *co, char *options)
290{
291 return 0;
292}
293
294static struct console srmcons = {
295 .name = "srm",
296 .write = srm_console_write,
297 .device = srm_console_device,
298 .setup = srm_console_setup,
Gerd Hoffmann69331af2007-05-08 00:26:49 -0700299 .flags = CON_PRINTBUFFER | CON_BOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 .index = -1,
301};
302
303void __init
304register_srm_console(void)
305{
306 if (!srm_is_registered_console) {
307 callback_open_console();
308 register_console(&srmcons);
309 srm_is_registered_console = 1;
310 }
311}
312
313void __init
314unregister_srm_console(void)
315{
316 if (srm_is_registered_console) {
317 callback_close_console();
318 unregister_console(&srmcons);
319 srm_is_registered_console = 0;
320 }
321}