[PATCH] uml: Fix flip_buf full handling
[pandora-kernel.git] / arch / um / drivers / chan_kern.c
1 /*
2  * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include <linux/stddef.h>
7 #include <linux/kernel.h>
8 #include <linux/list.h>
9 #include <linux/slab.h>
10 #include <linux/tty.h>
11 #include <linux/string.h>
12 #include <linux/tty_flip.h>
13 #include <asm/irq.h>
14 #include "chan_kern.h"
15 #include "user_util.h"
16 #include "kern.h"
17 #include "irq_user.h"
18 #include "sigio.h"
19 #include "line.h"
20 #include "os.h"
21
22 /* XXX: could well be moved to somewhere else, if needed. */
23 static int my_printf(const char * fmt, ...)
24         __attribute__ ((format (printf, 1, 2)));
25
26 static int my_printf(const char * fmt, ...)
27 {
28         /* Yes, can be called on atomic context.*/
29         char *buf = kmalloc(4096, GFP_ATOMIC);
30         va_list args;
31         int r;
32
33         if (!buf) {
34                 /* We print directly fmt.
35                  * Yes, yes, yes, feel free to complain. */
36                 r = strlen(fmt);
37         } else {
38                 va_start(args, fmt);
39                 r = vsprintf(buf, fmt, args);
40                 va_end(args);
41                 fmt = buf;
42         }
43
44         if (r)
45                 r = os_write_file(1, fmt, r);
46         return r;
47
48 }
49
50 #ifdef CONFIG_NOCONFIG_CHAN
51 /* Despite its name, there's no added trailing newline. */
52 static int my_puts(const char * buf)
53 {
54         return os_write_file(1, buf, strlen(buf));
55 }
56
57 static void *not_configged_init(char *str, int device, struct chan_opts *opts)
58 {
59         my_puts("Using a channel type which is configured out of "
60                "UML\n");
61         return NULL;
62 }
63
64 static int not_configged_open(int input, int output, int primary, void *data,
65                               char **dev_out)
66 {
67         my_puts("Using a channel type which is configured out of "
68                "UML\n");
69         return -ENODEV;
70 }
71
72 static void not_configged_close(int fd, void *data)
73 {
74         my_puts("Using a channel type which is configured out of "
75                "UML\n");
76 }
77
78 static int not_configged_read(int fd, char *c_out, void *data)
79 {
80         my_puts("Using a channel type which is configured out of "
81                "UML\n");
82         return -EIO;
83 }
84
85 static int not_configged_write(int fd, const char *buf, int len, void *data)
86 {
87         my_puts("Using a channel type which is configured out of "
88                "UML\n");
89         return -EIO;
90 }
91
92 static int not_configged_console_write(int fd, const char *buf, int len)
93 {
94         my_puts("Using a channel type which is configured out of "
95                "UML\n");
96         return -EIO;
97 }
98
99 static int not_configged_window_size(int fd, void *data, unsigned short *rows,
100                                      unsigned short *cols)
101 {
102         my_puts("Using a channel type which is configured out of "
103                "UML\n");
104         return -ENODEV;
105 }
106
107 static void not_configged_free(void *data)
108 {
109         my_puts("Using a channel type which is configured out of "
110                "UML\n");
111 }
112
113 static struct chan_ops not_configged_ops = {
114         .init           = not_configged_init,
115         .open           = not_configged_open,
116         .close          = not_configged_close,
117         .read           = not_configged_read,
118         .write          = not_configged_write,
119         .console_write  = not_configged_console_write,
120         .window_size    = not_configged_window_size,
121         .free           = not_configged_free,
122         .winch          = 0,
123 };
124 #endif /* CONFIG_NOCONFIG_CHAN */
125
126 void generic_close(int fd, void *unused)
127 {
128         os_close_file(fd);
129 }
130
131 int generic_read(int fd, char *c_out, void *unused)
132 {
133         int n;
134
135         n = os_read_file(fd, c_out, sizeof(*c_out));
136
137         if(n == -EAGAIN)
138                 return 0;
139         else if(n == 0)
140                 return -EIO;
141         return n;
142 }
143
144 /* XXX Trivial wrapper around os_write_file */
145
146 int generic_write(int fd, const char *buf, int n, void *unused)
147 {
148         return os_write_file(fd, buf, n);
149 }
150
151 int generic_window_size(int fd, void *unused, unsigned short *rows_out,
152                         unsigned short *cols_out)
153 {
154         int rows, cols;
155         int ret;
156
157         ret = os_window_size(fd, &rows, &cols);
158         if(ret < 0)
159                 return ret;
160
161         ret = ((*rows_out != rows) || (*cols_out != cols));
162
163         *rows_out = rows;
164         *cols_out = cols;
165
166         return ret;
167 }
168
169 void generic_free(void *data)
170 {
171         kfree(data);
172 }
173
174 static void tty_receive_char(struct tty_struct *tty, char ch)
175 {
176         if(tty == NULL) return;
177
178         if(I_IXON(tty) && !I_IXOFF(tty) && !tty->raw) {
179                 if(ch == STOP_CHAR(tty)){
180                         stop_tty(tty);
181                         return;
182                 }
183                 else if(ch == START_CHAR(tty)){
184                         start_tty(tty);
185                         return;
186                 }
187         }
188
189         if((tty->flip.flag_buf_ptr == NULL) ||
190            (tty->flip.char_buf_ptr == NULL))
191                 return;
192         tty_insert_flip_char(tty, ch, TTY_NORMAL);
193 }
194
195 static int open_one_chan(struct chan *chan)
196 {
197         int fd;
198
199         if(chan->opened)
200                 return 0;
201
202         if(chan->ops->open == NULL)
203                 fd = 0;
204         else fd = (*chan->ops->open)(chan->input, chan->output, chan->primary,
205                                      chan->data, &chan->dev);
206         if(fd < 0)
207                 return fd;
208         chan->fd = fd;
209
210         chan->opened = 1;
211         return 0;
212 }
213
214 int open_chan(struct list_head *chans)
215 {
216         struct list_head *ele;
217         struct chan *chan;
218         int ret, err = 0;
219
220         list_for_each(ele, chans){
221                 chan = list_entry(ele, struct chan, list);
222                 ret = open_one_chan(chan);
223                 if(chan->primary)
224                         err = ret;
225         }
226         return err;
227 }
228
229 void chan_enable_winch(struct list_head *chans, struct tty_struct *tty)
230 {
231         struct list_head *ele;
232         struct chan *chan;
233
234         list_for_each(ele, chans){
235                 chan = list_entry(ele, struct chan, list);
236                 if(chan->primary && chan->output && chan->ops->winch){
237                         register_winch(chan->fd, tty);
238                         return;
239                 }
240         }
241 }
242
243 void enable_chan(struct line *line)
244 {
245         struct list_head *ele;
246         struct chan *chan;
247
248         list_for_each(ele, &line->chan_list){
249                 chan = list_entry(ele, struct chan, list);
250                 if(open_one_chan(chan))
251                         continue;
252
253                 if(chan->enabled)
254                         continue;
255                 line_setup_irq(chan->fd, chan->input, chan->output, line,
256                                chan);
257                 chan->enabled = 1;
258         }
259 }
260
261 static LIST_HEAD(irqs_to_free);
262
263 void free_irqs(void)
264 {
265         struct chan *chan;
266
267         while(!list_empty(&irqs_to_free)){
268                 chan = list_entry(irqs_to_free.next, struct chan, free_list);
269                 list_del(&chan->free_list);
270
271                 if(chan->input)
272                         free_irq(chan->line->driver->read_irq, chan);
273                 if(chan->output)
274                         free_irq(chan->line->driver->write_irq, chan);
275                 chan->enabled = 0;
276         }
277 }
278
279 static void close_one_chan(struct chan *chan, int delay_free_irq)
280 {
281         if(!chan->opened)
282                 return;
283
284         if(delay_free_irq){
285                 list_add(&chan->free_list, &irqs_to_free);
286         }
287         else {
288                 if(chan->input)
289                         free_irq(chan->line->driver->read_irq, chan);
290                 if(chan->output)
291                         free_irq(chan->line->driver->write_irq, chan);
292                 chan->enabled = 0;
293         }
294         if(chan->ops->close != NULL)
295                 (*chan->ops->close)(chan->fd, chan->data);
296
297         chan->opened = 0;
298         chan->fd = -1;
299 }
300
301 void close_chan(struct list_head *chans, int delay_free_irq)
302 {
303         struct chan *chan;
304
305         /* Close in reverse order as open in case more than one of them
306          * refers to the same device and they save and restore that device's
307          * state.  Then, the first one opened will have the original state,
308          * so it must be the last closed.
309          */
310         list_for_each_entry_reverse(chan, chans, list) {
311                 close_one_chan(chan, delay_free_irq);
312         }
313 }
314
315 int write_chan(struct list_head *chans, const char *buf, int len,
316                int write_irq)
317 {
318         struct list_head *ele;
319         struct chan *chan = NULL;
320         int n, ret = 0;
321
322         list_for_each(ele, chans) {
323                 chan = list_entry(ele, struct chan, list);
324                 if (!chan->output || (chan->ops->write == NULL))
325                         continue;
326                 n = chan->ops->write(chan->fd, buf, len, chan->data);
327                 if (chan->primary) {
328                         ret = n;
329                         if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
330                                 reactivate_fd(chan->fd, write_irq);
331                 }
332         }
333         return ret;
334 }
335
336 int console_write_chan(struct list_head *chans, const char *buf, int len)
337 {
338         struct list_head *ele;
339         struct chan *chan;
340         int n, ret = 0;
341
342         list_for_each(ele, chans){
343                 chan = list_entry(ele, struct chan, list);
344                 if(!chan->output || (chan->ops->console_write == NULL))
345                         continue;
346                 n = chan->ops->console_write(chan->fd, buf, len);
347                 if(chan->primary) ret = n;
348         }
349         return ret;
350 }
351
352 int console_open_chan(struct line *line, struct console *co,
353                       struct chan_opts *opts)
354 {
355         int err;
356
357         err = open_chan(&line->chan_list);
358         if(err)
359                 return err;
360
361         printk("Console initialized on /dev/%s%d\n",co->name,co->index);
362         return 0;
363 }
364
365 int chan_window_size(struct list_head *chans, unsigned short *rows_out,
366                       unsigned short *cols_out)
367 {
368         struct list_head *ele;
369         struct chan *chan;
370
371         list_for_each(ele, chans){
372                 chan = list_entry(ele, struct chan, list);
373                 if(chan->primary){
374                         if(chan->ops->window_size == NULL)
375                                 return 0;
376                         return chan->ops->window_size(chan->fd, chan->data,
377                                                       rows_out, cols_out);
378                 }
379         }
380         return 0;
381 }
382
383 void free_one_chan(struct chan *chan, int delay_free_irq)
384 {
385         list_del(&chan->list);
386
387         close_one_chan(chan, delay_free_irq);
388
389         if(chan->ops->free != NULL)
390                 (*chan->ops->free)(chan->data);
391
392         if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
393         kfree(chan);
394 }
395
396 void free_chan(struct list_head *chans, int delay_free_irq)
397 {
398         struct list_head *ele, *next;
399         struct chan *chan;
400
401         list_for_each_safe(ele, next, chans){
402                 chan = list_entry(ele, struct chan, list);
403                 free_one_chan(chan, delay_free_irq);
404         }
405 }
406
407 static int one_chan_config_string(struct chan *chan, char *str, int size,
408                                   char **error_out)
409 {
410         int n = 0;
411
412         if(chan == NULL){
413                 CONFIG_CHUNK(str, size, n, "none", 1);
414                 return n;
415         }
416
417         CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
418
419         if(chan->dev == NULL){
420                 CONFIG_CHUNK(str, size, n, "", 1);
421                 return n;
422         }
423
424         CONFIG_CHUNK(str, size, n, ":", 0);
425         CONFIG_CHUNK(str, size, n, chan->dev, 0);
426
427         return n;
428 }
429
430 static int chan_pair_config_string(struct chan *in, struct chan *out,
431                                    char *str, int size, char **error_out)
432 {
433         int n;
434
435         n = one_chan_config_string(in, str, size, error_out);
436         str += n;
437         size -= n;
438
439         if(in == out){
440                 CONFIG_CHUNK(str, size, n, "", 1);
441                 return n;
442         }
443
444         CONFIG_CHUNK(str, size, n, ",", 1);
445         n = one_chan_config_string(out, str, size, error_out);
446         str += n;
447         size -= n;
448         CONFIG_CHUNK(str, size, n, "", 1);
449
450         return n;
451 }
452
453 int chan_config_string(struct list_head *chans, char *str, int size,
454                        char **error_out)
455 {
456         struct list_head *ele;
457         struct chan *chan, *in = NULL, *out = NULL;
458
459         list_for_each(ele, chans){
460                 chan = list_entry(ele, struct chan, list);
461                 if(!chan->primary)
462                         continue;
463                 if(chan->input)
464                         in = chan;
465                 if(chan->output)
466                         out = chan;
467         }
468
469         return chan_pair_config_string(in, out, str, size, error_out);
470 }
471
472 struct chan_type {
473         char *key;
474         struct chan_ops *ops;
475 };
476
477 struct chan_type chan_table[] = {
478         { "fd", &fd_ops },
479
480 #ifdef CONFIG_NULL_CHAN
481         { "null", &null_ops },
482 #else
483         { "null", &not_configged_ops },
484 #endif
485
486 #ifdef CONFIG_PORT_CHAN
487         { "port", &port_ops },
488 #else
489         { "port", &not_configged_ops },
490 #endif
491
492 #ifdef CONFIG_PTY_CHAN
493         { "pty", &pty_ops },
494         { "pts", &pts_ops },
495 #else
496         { "pty", &not_configged_ops },
497         { "pts", &not_configged_ops },
498 #endif
499
500 #ifdef CONFIG_TTY_CHAN
501         { "tty", &tty_ops },
502 #else
503         { "tty", &not_configged_ops },
504 #endif
505
506 #ifdef CONFIG_XTERM_CHAN
507         { "xterm", &xterm_ops },
508 #else
509         { "xterm", &not_configged_ops },
510 #endif
511 };
512
513 static struct chan *parse_chan(struct line *line, char *str, int device,
514                                struct chan_opts *opts)
515 {
516         struct chan_type *entry;
517         struct chan_ops *ops;
518         struct chan *chan;
519         void *data;
520         int i;
521
522         ops = NULL;
523         data = NULL;
524         for(i = 0; i < sizeof(chan_table)/sizeof(chan_table[0]); i++){
525                 entry = &chan_table[i];
526                 if(!strncmp(str, entry->key, strlen(entry->key))){
527                         ops = entry->ops;
528                         str += strlen(entry->key);
529                         break;
530                 }
531         }
532         if(ops == NULL){
533                 my_printf("parse_chan couldn't parse \"%s\"\n",
534                        str);
535                 return NULL;
536         }
537         if(ops->init == NULL)
538                 return NULL;
539         data = (*ops->init)(str, device, opts);
540         if(data == NULL)
541                 return NULL;
542
543         chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
544         if(chan == NULL)
545                 return NULL;
546         *chan = ((struct chan) { .list          = LIST_HEAD_INIT(chan->list),
547                                  .free_list     =
548                                         LIST_HEAD_INIT(chan->free_list),
549                                  .line          = line,
550                                  .primary       = 1,
551                                  .input         = 0,
552                                  .output        = 0,
553                                  .opened        = 0,
554                                  .enabled       = 0,
555                                  .fd            = -1,
556                                  .ops           = ops,
557                                  .data          = data });
558         return chan;
559 }
560
561 int parse_chan_pair(char *str, struct line *line, int device,
562                     struct chan_opts *opts)
563 {
564         struct list_head *chans = &line->chan_list;
565         struct chan *new, *chan;
566         char *in, *out;
567
568         if(!list_empty(chans)){
569                 chan = list_entry(chans->next, struct chan, list);
570                 free_chan(chans, 0);
571                 INIT_LIST_HEAD(chans);
572         }
573
574         out = strchr(str, ',');
575         if(out != NULL){
576                 in = str;
577                 *out = '\0';
578                 out++;
579                 new = parse_chan(line, in, device, opts);
580                 if(new == NULL)
581                         return -1;
582
583                 new->input = 1;
584                 list_add(&new->list, chans);
585
586                 new = parse_chan(line, out, device, opts);
587                 if(new == NULL)
588                         return -1;
589
590                 list_add(&new->list, chans);
591                 new->output = 1;
592         }
593         else {
594                 new = parse_chan(line, str, device, opts);
595                 if(new == NULL)
596                         return -1;
597
598                 list_add(&new->list, chans);
599                 new->input = 1;
600                 new->output = 1;
601         }
602         return 0;
603 }
604
605 int chan_out_fd(struct list_head *chans)
606 {
607         struct list_head *ele;
608         struct chan *chan;
609
610         list_for_each(ele, chans){
611                 chan = list_entry(ele, struct chan, list);
612                 if(chan->primary && chan->output)
613                         return chan->fd;
614         }
615         return -1;
616 }
617
618 void chan_interrupt(struct list_head *chans, struct work_struct *task,
619                     struct tty_struct *tty, int irq)
620 {
621         struct list_head *ele, *next;
622         struct chan *chan;
623         int err;
624         char c;
625
626         list_for_each_safe(ele, next, chans){
627                 chan = list_entry(ele, struct chan, list);
628                 if(!chan->input || (chan->ops->read == NULL)) continue;
629                 do {
630                         if((tty != NULL) &&
631                            (tty->flip.count >= TTY_FLIPBUF_SIZE)){
632                                 schedule_delayed_work(task, 1);
633                                 goto out;
634                         }
635                         err = chan->ops->read(chan->fd, &c, chan->data);
636                         if(err > 0)
637                                 tty_receive_char(tty, c);
638                 } while(err > 0);
639
640                 if(err == 0) reactivate_fd(chan->fd, irq);
641                 if(err == -EIO){
642                         if(chan->primary){
643                                 if(tty != NULL)
644                                         tty_hangup(tty);
645                                 close_chan(chans, 1);
646                                 return;
647                         }
648                         else close_one_chan(chan, 1);
649                 }
650         }
651  out:
652         if(tty) tty_flip_buffer_push(tty);
653 }