]> Pileus Git - ~andy/linux/blob - arch/alpha/kernel/srmcons.c
ALPHA: srmcons, use timer functions
[~andy/linux] / arch / alpha / kernel / srmcons.c
1 /*
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
8 #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
24 static DEFINE_SPINLOCK(srmcons_callback_lock);
25 static 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
32 struct srmcons_private {
33         struct tty_struct *tty;
34         struct timer_list timer;
35         spinlock_t lock;
36 };
37
38 typedef 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 */
47 static int
48 srmcons_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
67 static void
68 srmcons_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);
82         if (srmconsp->tty)
83                 mod_timer(&srmconsp->timer, jiffies + incr);
84         spin_unlock(&srmconsp->lock);
85
86         local_irq_restore(flags);
87 }
88
89 /* called with callback_lock held */
90 static int
91 srmcons_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
131 static int
132 srmcons_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
144 static int
145 srmcons_write_room(struct tty_struct *tty)
146 {
147         return 512;
148 }
149
150 static int
151 srmcons_chars_in_buffer(struct tty_struct *tty)
152 {
153         return 0;
154 }
155
156 static int
157 srmcons_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) {
165                 srmconsp = kmalloc(sizeof(*srmconsp), GFP_KERNEL);
166                 spin_lock_irqsave(&srmconsp_lock, flags);
167
168                 if (srmconsp == NULL)
169                         retval = -ENOMEM;
170                 else {
171                         srmconsp->tty = NULL;
172                         spin_lock_init(&srmconsp->lock);
173                         setup_timer(&srmconsp->timer, srmcons_receive_chars,
174                                         (unsigned long)srmconsp);
175                 }
176
177                 spin_unlock_irqrestore(&srmconsp_lock, flags);
178         }
179
180         *ps = srmconsp;
181         return retval;
182 }
183
184 static int
185 srmcons_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;
201                 mod_timer(&srmconsp->timer, jiffies + 10);
202         }
203
204         spin_unlock_irqrestore(&srmconsp->lock, flags);
205
206         return 0;
207 }
208
209 static void
210 srmcons_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
226 static struct tty_driver *srmcons_driver;
227
228 static const struct tty_operations srmcons_ops = {
229         .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
236 static int __init
237 srmcons_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
265 module_init(srmcons_init);
266
267 \f
268 /*
269  * The console driver
270  */
271 static void
272 srm_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
281 static struct tty_driver *
282 srm_console_device(struct console *co, int *index)
283 {
284         *index = co->index;
285         return srmcons_driver;
286 }
287
288 static int
289 srm_console_setup(struct console *co, char *options)
290 {
291         return 0;
292 }
293
294 static struct console srmcons = {
295         .name           = "srm",
296         .write          = srm_console_write,
297         .device         = srm_console_device,
298         .setup          = srm_console_setup,
299         .flags          = CON_PRINTBUFFER | CON_BOOT,
300         .index          = -1,
301 };
302
303 void __init
304 register_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
313 void __init
314 unregister_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 }