ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / 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/config.h>
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/console.h>
12 #include <linux/delay.h>
13 #include <linux/mm.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <linux/timer.h>
17 #include <linux/tty.h>
18 #include <linux/tty_driver.h>
19 #include <linux/tty_flip.h>
20
21 #include <asm/console.h>
22 #include <asm/uaccess.h>
23
24
25 static spinlock_t srmcons_callback_lock = SPIN_LOCK_UNLOCKED;
26 static int srm_is_registered_console = 0;
27
28 /* 
29  * The TTY driver
30  */
31 #define MAX_SRM_CONSOLE_DEVICES 1       /* only support 1 console device */
32
33 struct srmcons_private {
34         struct tty_struct *tty;
35         struct timer_list timer;
36         spinlock_t lock;
37 };
38
39 typedef union _srmcons_result {
40         struct {
41                 unsigned long c :61;
42                 unsigned long status :3;
43         } bits;
44         long as_long;
45 } srmcons_result;
46
47 /* called with callback_lock held */
48 static int
49 srmcons_do_receive_chars(struct tty_struct *tty)
50 {
51         srmcons_result result;
52         int count = 0, loops = 0;
53
54         do {
55                 result.as_long = callback_getc(0);
56                 if (result.bits.status < 2) {
57                         tty_insert_flip_char(tty, (char)result.bits.c, 0);
58                         count++;
59                 }
60         } while((result.bits.status & 1) && (++loops < 10));
61
62         if (count)
63                 tty_schedule_flip(tty);
64
65         return count;
66 }
67
68 static void
69 srmcons_receive_chars(unsigned long data)
70 {
71         struct srmcons_private *srmconsp = (struct srmcons_private *)data;
72         unsigned long flags;
73         int incr = 10;
74
75         local_irq_save(flags);
76         if (spin_trylock(&srmcons_callback_lock)) {
77                 if (!srmcons_do_receive_chars(srmconsp->tty))
78                         incr = 100;
79                 spin_unlock(&srmcons_callback_lock);
80         } 
81
82         spin_lock(&srmconsp->lock);
83         if (srmconsp->tty) {
84                 srmconsp->timer.expires = jiffies + incr;
85                 add_timer(&srmconsp->timer);
86         }
87         spin_unlock(&srmconsp->lock);
88
89         local_irq_restore(flags);
90 }
91
92 /* called with callback_lock held */
93 static int
94 srmcons_do_write(struct tty_struct *tty, const unsigned char *buf, int count)
95 {
96         unsigned char *str_cr = "\r";
97         long c, remaining = count;
98         srmcons_result result;
99         unsigned char *cur;
100         int need_cr;
101
102         for (cur = (unsigned char *)buf; remaining > 0; ) {
103                 need_cr = 0;
104                 /* 
105                  * Break it up into reasonable size chunks to allow a chance
106                  * for input to get in
107                  */
108                 for (c = 0; c < min_t(long, 128L, remaining) && !need_cr; c++)
109                         if (cur[c] == '\n')
110                                 need_cr = 1;
111                 
112                 while (c > 0) {
113                         result.as_long = callback_puts(0, cur, c);
114                         c -= result.bits.c;
115                         remaining -= result.bits.c;
116                         cur += result.bits.c;
117
118                         /*
119                          * Check for pending input iff a tty was provided
120                          */
121                         if (tty)
122                                 srmcons_do_receive_chars(tty);
123                 }
124
125                 while (need_cr) {
126                         result.as_long = callback_puts(0, str_cr, 1);
127                         if (result.bits.c > 0)
128                                 need_cr = 0;
129                 }
130         }
131         return count;
132 }
133
134 static int
135 srmcons_write(struct tty_struct *tty, int from_user,
136               const unsigned char *buf, int count)
137 {
138         unsigned long flags;
139
140         if (from_user) {
141                 unsigned char tmp[512];
142                 int ret = 0;
143                 size_t c;
144
145                 while ((c = count) > 0) {
146                         if (c > sizeof(tmp))
147                                 c = sizeof(tmp);
148                         
149                         c -= copy_from_user(tmp, buf, c);
150
151                         if (!c) { 
152                                 printk("%s: EFAULT (count %d)\n",
153                                        __FUNCTION__, count);
154                                 return -EFAULT;
155                         }
156
157                         spin_lock_irqsave(&srmcons_callback_lock, flags);
158                         srmcons_do_write(tty, tmp, c);
159                         spin_unlock_irqrestore(&srmcons_callback_lock, flags);
160
161                         buf += c;
162                         count -= c;
163                         ret += c;
164                 }
165
166                 return ret;
167         }
168
169         spin_lock_irqsave(&srmcons_callback_lock, flags);
170         srmcons_do_write(tty, buf, count);
171         spin_unlock_irqrestore(&srmcons_callback_lock, flags);
172
173         return count;
174 }
175
176 static int
177 srmcons_write_room(struct tty_struct *tty)
178 {
179         return 512;
180 }
181
182 static int
183 srmcons_chars_in_buffer(struct tty_struct *tty)
184 {
185         return 0;
186 }
187
188 static int
189 srmcons_get_private_struct(struct srmcons_private **ps)
190 {
191         static struct srmcons_private *srmconsp = NULL;
192         static spinlock_t srmconsp_lock = SPIN_LOCK_UNLOCKED;
193         unsigned long flags;
194         int retval = 0;
195
196         spin_lock_irqsave(&srmconsp_lock, flags);
197
198         do {
199                 if (srmconsp != NULL) {
200                         *ps = srmconsp;
201                         break;
202                 }
203
204                 srmconsp = kmalloc(sizeof(*srmconsp), GFP_KERNEL);
205                 if (srmconsp == NULL) {
206                         retval = -ENOMEM;
207                         break;
208                 }
209
210                 srmconsp->tty = NULL;
211                 srmconsp->lock = SPIN_LOCK_UNLOCKED;
212                 init_timer(&srmconsp->timer);
213
214                 *ps = srmconsp;
215         } while(0);
216
217         spin_unlock_irqrestore(&srmconsp_lock, flags);
218
219         return retval;
220 }
221
222 static int
223 srmcons_open(struct tty_struct *tty, struct file *filp)
224 {
225         struct srmcons_private *srmconsp;
226         unsigned long flags;
227         int retval;
228
229         retval = srmcons_get_private_struct(&srmconsp);
230         if (retval)
231                 return retval;
232
233         spin_lock_irqsave(&srmconsp->lock, flags);
234
235         if (!srmconsp->tty) {
236                 tty->driver_data = srmconsp;
237
238                 srmconsp->tty = tty;
239                 srmconsp->timer.function = srmcons_receive_chars;
240                 srmconsp->timer.data = (unsigned long)srmconsp;
241                 srmconsp->timer.expires = jiffies + 10;
242                 add_timer(&srmconsp->timer);
243         }
244
245         spin_unlock_irqrestore(&srmconsp->lock, flags);
246
247         return 0;
248 }
249
250 static void
251 srmcons_close(struct tty_struct *tty, struct file *filp)
252 {
253         struct srmcons_private *srmconsp = tty->driver_data;
254         unsigned long flags;
255
256         spin_lock_irqsave(&srmconsp->lock, flags);
257
258         if (tty->count == 1) {
259                 srmconsp->tty = NULL;
260                 del_timer(&srmconsp->timer);
261         }
262
263         spin_unlock_irqrestore(&srmconsp->lock, flags);
264 }
265
266
267 static struct tty_driver *srmcons_driver;
268
269 static struct tty_operations srmcons_ops = {
270         .open           = srmcons_open,
271         .close          = srmcons_close,
272         .write          = srmcons_write,
273         .write_room     = srmcons_write_room,
274         .chars_in_buffer= srmcons_chars_in_buffer,
275 };
276
277 static int __init
278 srmcons_init(void)
279 {
280         if (srm_is_registered_console) {
281                 struct tty_driver *driver;
282                 int err;
283
284                 driver = alloc_tty_driver(MAX_SRM_CONSOLE_DEVICES);
285                 if (!driver)
286                         return -ENOMEM;
287                 driver->driver_name = "srm";
288                 driver->name = "srm";
289                 driver->major = 0;      /* dynamic */
290                 driver->minor_start = 0;
291                 driver->type = TTY_DRIVER_TYPE_SYSTEM;
292                 driver->subtype = SYSTEM_TYPE_SYSCONS;
293                 driver->init_termios = tty_std_termios;
294                 tty_set_operations(driver, &srmcons_ops);
295                 err = tty_register_driver(driver);
296                 if (err) {
297                         put_tty_driver(driver);
298                         return err;
299                 }
300                 srmcons_driver = driver;
301         }
302
303         return -ENODEV;
304 }
305
306 module_init(srmcons_init);
307
308 \f
309 /*
310  * The console driver
311  */
312 static void
313 srm_console_write(struct console *co, const char *s, unsigned count)
314 {
315         unsigned long flags;
316
317         spin_lock_irqsave(&srmcons_callback_lock, flags);
318         srmcons_do_write(NULL, s, count);
319         spin_unlock_irqrestore(&srmcons_callback_lock, flags);
320 }
321
322 static struct tty_driver *
323 srm_console_device(struct console *co, int *index)
324 {
325         *index = co->index;
326         return srmcons_driver;
327 }
328
329 static int __init
330 srm_console_setup(struct console *co, char *options)
331 {
332         return 0;
333 }
334
335 static struct console srmcons = {
336         .name           = "srm",
337         .write          = srm_console_write,
338         .device         = srm_console_device,
339         .setup          = srm_console_setup,
340         .flags          = CON_PRINTBUFFER,
341         .index          = -1,
342 };
343
344 void __init
345 register_srm_console(void)
346 {
347         if (!srm_is_registered_console) {
348                 callback_open_console();
349                 register_console(&srmcons);
350                 srm_is_registered_console = 1;
351         }
352 }
353
354 void __init
355 unregister_srm_console(void)
356 {
357         if (srm_is_registered_console) {
358                 callback_close_console();
359                 unregister_console(&srmcons);
360                 srm_is_registered_console = 0;
361         }
362 }