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