vserver 1.9.5.x5
[linux-2.6.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 #ifdef CONFIG_NOCONFIG_CHAN
23 static void *not_configged_init(char *str, int device, struct chan_opts *opts)
24 {
25         printk(KERN_ERR "Using a channel type which is configured out of "
26                "UML\n");
27         return(NULL);
28 }
29
30 static int not_configged_open(int input, int output, int primary, void *data,
31                               char **dev_out)
32 {
33         printk(KERN_ERR "Using a channel type which is configured out of "
34                "UML\n");
35         return(-ENODEV);
36 }
37
38 static void not_configged_close(int fd, void *data)
39 {
40         printk(KERN_ERR "Using a channel type which is configured out of "
41                "UML\n");
42 }
43
44 static int not_configged_read(int fd, char *c_out, void *data)
45 {
46         printk(KERN_ERR "Using a channel type which is configured out of "
47                "UML\n");
48         return(-EIO);
49 }
50
51 static int not_configged_write(int fd, const char *buf, int len, void *data)
52 {
53         printk(KERN_ERR "Using a channel type which is configured out of "
54                "UML\n");
55         return(-EIO);
56 }
57
58 static int not_configged_console_write(int fd, const char *buf, int len,
59                                        void *data)
60 {
61         printk(KERN_ERR "Using a channel type which is configured out of "
62                "UML\n");
63         return(-EIO);
64 }
65
66 static int not_configged_window_size(int fd, void *data, unsigned short *rows,
67                                      unsigned short *cols)
68 {
69         printk(KERN_ERR "Using a channel type which is configured out of "
70                "UML\n");
71         return(-ENODEV);
72 }
73
74 static void not_configged_free(void *data)
75 {
76         printk(KERN_ERR "Using a channel type which is configured out of "
77                "UML\n");
78 }
79
80 static struct chan_ops not_configged_ops = {
81         .init           = not_configged_init,
82         .open           = not_configged_open,
83         .close          = not_configged_close,
84         .read           = not_configged_read,
85         .write          = not_configged_write,
86         .console_write  = not_configged_console_write,
87         .window_size    = not_configged_window_size,
88         .free           = not_configged_free,
89         .winch          = 0,
90 };
91 #endif /* CONFIG_NOCONFIG_CHAN */
92
93 void generic_close(int fd, void *unused)
94 {
95         os_close_file(fd);
96 }
97
98 int generic_read(int fd, char *c_out, void *unused)
99 {
100         int n;
101
102         n = os_read_file(fd, c_out, sizeof(*c_out));
103
104         if(n == -EAGAIN)
105                 return(0);
106         else if(n == 0)
107                 return(-EIO);
108         return(n);
109 }
110
111 /* XXX Trivial wrapper around os_write_file */
112
113 int generic_write(int fd, const char *buf, int n, void *unused)
114 {
115         return(os_write_file(fd, buf, n));
116 }
117
118 int generic_window_size(int fd, void *unused, unsigned short *rows_out,
119                         unsigned short *cols_out)
120 {
121         int rows, cols;
122         int ret;
123
124         ret = os_window_size(fd, &rows, &cols);
125         if(ret < 0)
126                 return(ret);
127
128         ret = ((*rows_out != rows) || (*cols_out != cols));
129
130         *rows_out = rows;
131         *cols_out = cols;
132
133         return(ret);
134 }
135
136 void generic_free(void *data)
137 {
138         kfree(data);
139 }
140
141 static void tty_receive_char(struct tty_struct *tty, char ch)
142 {
143         if(tty == NULL) return;
144
145         if(I_IXON(tty) && !I_IXOFF(tty) && !tty->raw) {
146                 if(ch == STOP_CHAR(tty)){
147                         stop_tty(tty);
148                         return;
149                 }
150                 else if(ch == START_CHAR(tty)){
151                         start_tty(tty);
152                         return;
153                 }
154         }
155
156         if((tty->flip.flag_buf_ptr == NULL) || 
157            (tty->flip.char_buf_ptr == NULL))
158                 return;
159         tty_insert_flip_char(tty, ch, TTY_NORMAL);
160 }
161
162 static int open_one_chan(struct chan *chan, int input, int output, int primary)
163 {
164         int fd;
165
166         if(chan->opened) return(0);
167         if(chan->ops->open == NULL) fd = 0;
168         else fd = (*chan->ops->open)(input, output, primary, chan->data,
169                                      &chan->dev);
170         if(fd < 0) return(fd);
171         chan->fd = fd;
172
173         chan->opened = 1;
174         return(0);
175 }
176
177 int open_chan(struct list_head *chans)
178 {
179         struct list_head *ele;
180         struct chan *chan;
181         int ret, err = 0;
182
183         list_for_each(ele, chans){
184                 chan = list_entry(ele, struct chan, list);
185                 ret = open_one_chan(chan, chan->input, chan->output,
186                                     chan->primary);
187                 if(chan->primary) err = ret;
188         }
189         return(err);
190 }
191
192 void chan_enable_winch(struct list_head *chans, struct tty_struct *tty)
193 {
194         struct list_head *ele;
195         struct chan *chan;
196
197         list_for_each(ele, chans){
198                 chan = list_entry(ele, struct chan, list);
199                 if(chan->primary && chan->output && chan->ops->winch){
200                         register_winch(chan->fd, tty);
201                         return;
202                 }
203         }
204 }
205
206 void enable_chan(struct list_head *chans, struct tty_struct *tty)
207 {
208         struct list_head *ele;
209         struct chan *chan;
210
211         list_for_each(ele, chans){
212                 chan = list_entry(ele, struct chan, list);
213                 if(!chan->opened) continue;
214
215                 line_setup_irq(chan->fd, chan->input, chan->output, tty);
216         }
217 }
218
219 void close_chan(struct list_head *chans)
220 {
221         struct list_head *ele;
222         struct chan *chan;
223
224         /* Close in reverse order as open in case more than one of them
225          * refers to the same device and they save and restore that device's
226          * state.  Then, the first one opened will have the original state,
227          * so it must be the last closed.
228          */
229         for(ele = chans->prev; ele != chans; ele = ele->prev){
230                 chan = list_entry(ele, struct chan, list);
231                 if(!chan->opened) continue;
232                 if(chan->ops->close != NULL)
233                         (*chan->ops->close)(chan->fd, chan->data);
234                 chan->opened = 0;
235                 chan->fd = -1;
236         }
237 }
238
239 int write_chan(struct list_head *chans, const char *buf, int len, 
240                int write_irq)
241 {
242         struct list_head *ele;
243         struct chan *chan = NULL;
244         int n, ret = 0;
245
246         list_for_each(ele, chans) {
247                 chan = list_entry(ele, struct chan, list);
248                 if (!chan->output || (chan->ops->write == NULL))
249                         continue;
250                 n = chan->ops->write(chan->fd, buf, len, chan->data);
251                 if (chan->primary) {
252                         ret = n;
253                         if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len))){
254                                 reactivate_fd(chan->fd, write_irq);
255                                 if (ret == -EAGAIN)
256                                         ret = 0;
257                         }
258                 }
259         }
260         return(ret);
261 }
262
263 int console_write_chan(struct list_head *chans, const char *buf, int len)
264 {
265         struct list_head *ele;
266         struct chan *chan;
267         int n, ret = 0;
268
269         list_for_each(ele, chans){
270                 chan = list_entry(ele, struct chan, list);
271                 if(!chan->output || (chan->ops->console_write == NULL))
272                         continue;
273                 n = chan->ops->console_write(chan->fd, buf, len, chan->data);
274                 if(chan->primary) ret = n;
275         }
276         return(ret);
277 }
278
279 int console_open_chan(struct line *line, struct console *co, struct chan_opts *opts)
280 {
281         if (!list_empty(&line->chan_list))
282                 return 0;
283
284         if (0 != parse_chan_pair(line->init_str, &line->chan_list,
285                                  line->init_pri, co->index, opts))
286                 return -1;
287         if (0 != open_chan(&line->chan_list))
288                 return -1;
289         printk("Console initialized on /dev/%s%d\n",co->name,co->index);
290         return 0;
291 }
292
293 int chan_window_size(struct list_head *chans, unsigned short *rows_out,
294                       unsigned short *cols_out)
295 {
296         struct list_head *ele;
297         struct chan *chan;
298
299         list_for_each(ele, chans){
300                 chan = list_entry(ele, struct chan, list);
301                 if(chan->primary){
302                         if(chan->ops->window_size == NULL) return(0);
303                         return(chan->ops->window_size(chan->fd, chan->data,
304                                                       rows_out, cols_out));
305                 }
306         }
307         return(0);
308 }
309
310 void free_one_chan(struct chan *chan)
311 {
312         list_del(&chan->list);
313         if(chan->ops->free != NULL)
314                 (*chan->ops->free)(chan->data);
315         free_irq_by_fd(chan->fd);
316         if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
317         kfree(chan);
318 }
319
320 void free_chan(struct list_head *chans)
321 {
322         struct list_head *ele, *next;
323         struct chan *chan;
324
325         list_for_each_safe(ele, next, chans){
326                 chan = list_entry(ele, struct chan, list);
327                 free_one_chan(chan);
328         }
329 }
330
331 static int one_chan_config_string(struct chan *chan, char *str, int size,
332                                   char **error_out)
333 {
334         int n = 0;
335
336         if(chan == NULL){
337                 CONFIG_CHUNK(str, size, n, "none", 1);
338                 return(n);
339         }
340
341         CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
342
343         if(chan->dev == NULL){
344                 CONFIG_CHUNK(str, size, n, "", 1);
345                 return(n);
346         }
347
348         CONFIG_CHUNK(str, size, n, ":", 0);
349         CONFIG_CHUNK(str, size, n, chan->dev, 0);
350
351         return(n);
352 }
353
354 static int chan_pair_config_string(struct chan *in, struct chan *out, 
355                                    char *str, int size, char **error_out)
356 {
357         int n;
358
359         n = one_chan_config_string(in, str, size, error_out);
360         str += n;
361         size -= n;
362
363         if(in == out){
364                 CONFIG_CHUNK(str, size, n, "", 1);
365                 return(n);
366         }
367
368         CONFIG_CHUNK(str, size, n, ",", 1);
369         n = one_chan_config_string(out, str, size, error_out);
370         str += n;
371         size -= n;
372         CONFIG_CHUNK(str, size, n, "", 1);
373
374         return(n);
375 }
376
377 int chan_config_string(struct list_head *chans, char *str, int size, 
378                        char **error_out)
379 {
380         struct list_head *ele;
381         struct chan *chan, *in = NULL, *out = NULL;
382
383         list_for_each(ele, chans){
384                 chan = list_entry(ele, struct chan, list);
385                 if(!chan->primary)
386                         continue;
387                 if(chan->input)
388                         in = chan;
389                 if(chan->output)
390                         out = chan;
391         }
392
393         return(chan_pair_config_string(in, out, str, size, error_out));
394 }
395
396 struct chan_type {
397         char *key;
398         struct chan_ops *ops;
399 };
400
401 struct chan_type chan_table[] = {
402         { "fd", &fd_ops },
403
404 #ifdef CONFIG_NULL_CHAN
405         { "null", &null_ops },
406 #else
407         { "null", &not_configged_ops },
408 #endif
409
410 #ifdef CONFIG_PORT_CHAN
411         { "port", &port_ops },
412 #else
413         { "port", &not_configged_ops },
414 #endif
415
416 #ifdef CONFIG_PTY_CHAN
417         { "pty", &pty_ops },
418         { "pts", &pts_ops },
419 #else
420         { "pty", &not_configged_ops },
421         { "pts", &not_configged_ops },
422 #endif
423
424 #ifdef CONFIG_TTY_CHAN
425         { "tty", &tty_ops },
426 #else
427         { "tty", &not_configged_ops },
428 #endif
429
430 #ifdef CONFIG_XTERM_CHAN
431         { "xterm", &xterm_ops },
432 #else
433         { "xterm", &not_configged_ops },
434 #endif
435 };
436
437 static struct chan *parse_chan(char *str, int pri, int device, 
438                                struct chan_opts *opts)
439 {
440         struct chan_type *entry;
441         struct chan_ops *ops;
442         struct chan *chan;
443         void *data;
444         int i;
445
446         ops = NULL;
447         data = NULL;
448         for(i = 0; i < sizeof(chan_table)/sizeof(chan_table[0]); i++){
449                 entry = &chan_table[i];
450                 if(!strncmp(str, entry->key, strlen(entry->key))){
451                         ops = entry->ops;
452                         str += strlen(entry->key);
453                         break;
454                 }
455         }
456         if(ops == NULL){
457                 printk(KERN_ERR "parse_chan couldn't parse \"%s\"\n", 
458                        str);
459                 return(NULL);
460         }
461         if(ops->init == NULL) return(NULL); 
462         data = (*ops->init)(str, device, opts);
463         if(data == NULL) return(NULL);
464
465         chan = kmalloc(sizeof(*chan), GFP_KERNEL);
466         if(chan == NULL) return(NULL);
467         *chan = ((struct chan) { .list          = LIST_HEAD_INIT(chan->list),
468                                  .primary       = 1,
469                                  .input         = 0,
470                                  .output        = 0,
471                                  .opened        = 0,
472                                  .fd            = -1,
473                                  .pri           = pri,
474                                  .ops           = ops,
475                                  .data          = data });
476         return(chan);
477 }
478
479 int parse_chan_pair(char *str, struct list_head *chans, int pri, int device,
480                     struct chan_opts *opts)
481 {
482         struct chan *new, *chan;
483         char *in, *out;
484
485         if(!list_empty(chans)){
486                 chan = list_entry(chans->next, struct chan, list);
487                 if(chan->pri >= pri) return(0);
488                 free_chan(chans);
489                 INIT_LIST_HEAD(chans);
490         }
491
492         out = strchr(str, ',');
493         if(out != NULL){
494                 in = str;
495                 *out = '\0';
496                 out++;
497                 new = parse_chan(in, pri, device, opts);
498                 if(new == NULL) return(-1);
499                 new->input = 1;
500                 list_add(&new->list, chans);
501
502                 new = parse_chan(out, pri, device, opts);
503                 if(new == NULL) return(-1);
504                 list_add(&new->list, chans);
505                 new->output = 1;
506         }
507         else {
508                 new = parse_chan(str, pri, device, opts);
509                 if(new == NULL) return(-1);
510                 list_add(&new->list, chans);
511                 new->input = 1;
512                 new->output = 1;
513         }
514         return(0);
515 }
516
517 int chan_out_fd(struct list_head *chans)
518 {
519         struct list_head *ele;
520         struct chan *chan;
521
522         list_for_each(ele, chans){
523                 chan = list_entry(ele, struct chan, list);
524                 if(chan->primary && chan->output)
525                         return(chan->fd);
526         }
527         return(-1);
528 }
529
530 void chan_interrupt(struct list_head *chans, struct work_struct *task,
531                     struct tty_struct *tty, int irq)
532 {
533         struct list_head *ele, *next;
534         struct chan *chan;
535         int err;
536         char c;
537
538         list_for_each_safe(ele, next, chans){
539                 chan = list_entry(ele, struct chan, list);
540                 if(!chan->input || (chan->ops->read == NULL)) continue;
541                 do {
542                         if((tty != NULL) && 
543                            (tty->flip.count >= TTY_FLIPBUF_SIZE)){
544                                 schedule_work(task);
545                                 goto out;
546                         }
547                         err = chan->ops->read(chan->fd, &c, chan->data);
548                         if(err > 0)
549                                 tty_receive_char(tty, c);
550                 } while(err > 0);
551
552                 if(err == 0) reactivate_fd(chan->fd, irq);
553                 if(err == -EIO){
554                         if(chan->primary){
555                                 if(tty != NULL)
556                                         tty_hangup(tty);
557                                 line_disable(tty, irq);
558                                 close_chan(chans);
559                                 free_chan(chans);
560                                 return;
561                         }
562                         else {
563                                 if(chan->ops->close != NULL)
564                                         chan->ops->close(chan->fd, chan->data);
565                                 free_one_chan(chan);
566                         }
567                 }
568         }
569  out:
570         if(tty) tty_flip_buffer_push(tty);
571 }
572
573 /*
574  * Overrides for Emacs so that we follow Linus's tabbing style.
575  * Emacs will notice this stuff at the end of the file and automatically
576  * adjust the settings for this buffer only.  This must remain at the end
577  * of the file.
578  * ---------------------------------------------------------------------------
579  * Local variables:
580  * c-file-style: "linux"
581  * End:
582  */