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