]> Pileus Git - ~andy/linux/blob - arch/alpha/kernel/srmcons.c
ALPHA: srmcons, fix racy singleton structure
[~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 } srmcons_singleton;
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_open(struct tty_struct *tty, struct file *filp)
158 {
159         struct srmcons_private *srmconsp = &srmcons_singleton;
160         unsigned long flags;
161
162         spin_lock_irqsave(&srmconsp->lock, flags);
163
164         if (!srmconsp->tty) {
165                 tty->driver_data = srmconsp;
166
167                 srmconsp->tty = tty;
168                 mod_timer(&srmconsp->timer, jiffies + 10);
169         }
170
171         spin_unlock_irqrestore(&srmconsp->lock, flags);
172
173         return 0;
174 }
175
176 static void
177 srmcons_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
193 static struct tty_driver *srmcons_driver;
194
195 static const struct tty_operations srmcons_ops = {
196         .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
203 static int __init
204 srmcons_init(void)
205 {
206         spin_lock_init(&srmcons_singleton.lock);
207         setup_timer(&srmcons_singleton.timer, srmcons_receive_chars,
208                         (unsigned long)&srmcons_singleton);
209         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
235 module_init(srmcons_init);
236
237 \f
238 /*
239  * The console driver
240  */
241 static void
242 srm_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
251 static struct tty_driver *
252 srm_console_device(struct console *co, int *index)
253 {
254         *index = co->index;
255         return srmcons_driver;
256 }
257
258 static int
259 srm_console_setup(struct console *co, char *options)
260 {
261         return 0;
262 }
263
264 static struct console srmcons = {
265         .name           = "srm",
266         .write          = srm_console_write,
267         .device         = srm_console_device,
268         .setup          = srm_console_setup,
269         .flags          = CON_PRINTBUFFER | CON_BOOT,
270         .index          = -1,
271 };
272
273 void __init
274 register_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
283 void __init
284 unregister_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 }