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