allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / arch / um / drivers / chan_kern.c
blob3aa351611763cbcf625f5cc6a09b4f4aad41ea5f
1 /*
2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
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 "kern.h"
16 #include "irq_user.h"
17 #include "sigio.h"
18 #include "line.h"
19 #include "os.h"
21 #ifdef CONFIG_NOCONFIG_CHAN
22 static void *not_configged_init(char *str, int device,
23 const struct chan_opts *opts)
25 printk("Using a channel type which is configured out of "
26 "UML\n");
27 return NULL;
30 static int not_configged_open(int input, int output, int primary, void *data,
31 char **dev_out)
33 printk("Using a channel type which is configured out of "
34 "UML\n");
35 return -ENODEV;
38 static void not_configged_close(int fd, void *data)
40 printk("Using a channel type which is configured out of "
41 "UML\n");
44 static int not_configged_read(int fd, char *c_out, void *data)
46 printk("Using a channel type which is configured out of "
47 "UML\n");
48 return -EIO;
51 static int not_configged_write(int fd, const char *buf, int len, void *data)
53 printk("Using a channel type which is configured out of "
54 "UML\n");
55 return -EIO;
58 static int not_configged_console_write(int fd, const char *buf, int len)
60 printk("Using a channel type which is configured out of "
61 "UML\n");
62 return -EIO;
65 static int not_configged_window_size(int fd, void *data, unsigned short *rows,
66 unsigned short *cols)
68 printk("Using a channel type which is configured out of "
69 "UML\n");
70 return -ENODEV;
73 static void not_configged_free(void *data)
75 printk("Using a channel type which is configured out of "
76 "UML\n");
79 static const 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,
90 #endif /* CONFIG_NOCONFIG_CHAN */
92 void generic_close(int fd, void *unused)
94 os_close_file(fd);
97 int generic_read(int fd, char *c_out, void *unused)
99 int n;
101 n = os_read_file(fd, c_out, sizeof(*c_out));
103 if(n == -EAGAIN)
104 return 0;
105 else if(n == 0)
106 return -EIO;
107 return n;
110 /* XXX Trivial wrapper around os_write_file */
112 int generic_write(int fd, const char *buf, int n, void *unused)
114 return os_write_file(fd, buf, n);
117 int generic_window_size(int fd, void *unused, unsigned short *rows_out,
118 unsigned short *cols_out)
120 int rows, cols;
121 int ret;
123 ret = os_window_size(fd, &rows, &cols);
124 if(ret < 0)
125 return ret;
127 ret = ((*rows_out != rows) || (*cols_out != cols));
129 *rows_out = rows;
130 *cols_out = cols;
132 return ret;
135 void generic_free(void *data)
137 kfree(data);
140 static void tty_receive_char(struct tty_struct *tty, char ch)
142 if(tty == NULL) return;
144 if(I_IXON(tty) && !I_IXOFF(tty) && !tty->raw) {
145 if(ch == STOP_CHAR(tty)){
146 stop_tty(tty);
147 return;
149 else if(ch == START_CHAR(tty)){
150 start_tty(tty);
151 return;
155 tty_insert_flip_char(tty, ch, TTY_NORMAL);
158 static int open_one_chan(struct chan *chan)
160 int fd;
162 if(chan->opened)
163 return 0;
165 if(chan->ops->open == NULL)
166 fd = 0;
167 else fd = (*chan->ops->open)(chan->input, chan->output, chan->primary,
168 chan->data, &chan->dev);
169 if(fd < 0)
170 return fd;
171 chan->fd = fd;
173 chan->opened = 1;
174 return 0;
177 int open_chan(struct list_head *chans)
179 struct list_head *ele;
180 struct chan *chan;
181 int ret, err = 0;
183 list_for_each(ele, chans){
184 chan = list_entry(ele, struct chan, list);
185 ret = open_one_chan(chan);
186 if(chan->primary)
187 err = ret;
189 return err;
192 void chan_enable_winch(struct list_head *chans, struct tty_struct *tty)
194 struct list_head *ele;
195 struct chan *chan;
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;
206 void enable_chan(struct line *line)
208 struct list_head *ele;
209 struct chan *chan;
211 list_for_each(ele, &line->chan_list){
212 chan = list_entry(ele, struct chan, list);
213 if(open_one_chan(chan))
214 continue;
216 if(chan->enabled)
217 continue;
218 line_setup_irq(chan->fd, chan->input, chan->output, line,
219 chan);
220 chan->enabled = 1;
224 /* Items are added in IRQ context, when free_irq can't be called, and
225 * removed in process context, when it can.
226 * This handles interrupt sources which disappear, and which need to
227 * be permanently disabled. This is discovered in IRQ context, but
228 * the freeing of the IRQ must be done later.
230 static DEFINE_SPINLOCK(irqs_to_free_lock);
231 static LIST_HEAD(irqs_to_free);
233 void free_irqs(void)
235 struct chan *chan;
236 LIST_HEAD(list);
237 struct list_head *ele;
238 unsigned long flags;
240 spin_lock_irqsave(&irqs_to_free_lock, flags);
241 list_splice_init(&irqs_to_free, &list);
242 spin_unlock_irqrestore(&irqs_to_free_lock, flags);
244 list_for_each(ele, &list){
245 chan = list_entry(ele, struct chan, free_list);
247 if(chan->input)
248 free_irq(chan->line->driver->read_irq, chan);
249 if(chan->output)
250 free_irq(chan->line->driver->write_irq, chan);
251 chan->enabled = 0;
255 static void close_one_chan(struct chan *chan, int delay_free_irq)
257 unsigned long flags;
259 if(!chan->opened)
260 return;
262 if(delay_free_irq){
263 spin_lock_irqsave(&irqs_to_free_lock, flags);
264 list_add(&chan->free_list, &irqs_to_free);
265 spin_unlock_irqrestore(&irqs_to_free_lock, flags);
267 else {
268 if(chan->input)
269 free_irq(chan->line->driver->read_irq, chan);
270 if(chan->output)
271 free_irq(chan->line->driver->write_irq, chan);
272 chan->enabled = 0;
274 if(chan->ops->close != NULL)
275 (*chan->ops->close)(chan->fd, chan->data);
277 chan->opened = 0;
278 chan->fd = -1;
281 void close_chan(struct list_head *chans, int delay_free_irq)
283 struct chan *chan;
285 /* Close in reverse order as open in case more than one of them
286 * refers to the same device and they save and restore that device's
287 * state. Then, the first one opened will have the original state,
288 * so it must be the last closed.
290 list_for_each_entry_reverse(chan, chans, list) {
291 close_one_chan(chan, delay_free_irq);
295 void deactivate_chan(struct list_head *chans, int irq)
297 struct list_head *ele;
299 struct chan *chan;
300 list_for_each(ele, chans) {
301 chan = list_entry(ele, struct chan, list);
303 if(chan->enabled && chan->input)
304 deactivate_fd(chan->fd, irq);
308 void reactivate_chan(struct list_head *chans, int irq)
310 struct list_head *ele;
311 struct chan *chan;
313 list_for_each(ele, chans) {
314 chan = list_entry(ele, struct chan, list);
316 if(chan->enabled && chan->input)
317 reactivate_fd(chan->fd, irq);
321 int write_chan(struct list_head *chans, const char *buf, int len,
322 int write_irq)
324 struct list_head *ele;
325 struct chan *chan = NULL;
326 int n, ret = 0;
328 list_for_each(ele, chans) {
329 chan = list_entry(ele, struct chan, list);
330 if (!chan->output || (chan->ops->write == NULL))
331 continue;
332 n = chan->ops->write(chan->fd, buf, len, chan->data);
333 if (chan->primary) {
334 ret = n;
335 if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
336 reactivate_fd(chan->fd, write_irq);
339 return ret;
342 int console_write_chan(struct list_head *chans, const char *buf, int len)
344 struct list_head *ele;
345 struct chan *chan;
346 int n, ret = 0;
348 list_for_each(ele, chans){
349 chan = list_entry(ele, struct chan, list);
350 if(!chan->output || (chan->ops->console_write == NULL))
351 continue;
352 n = chan->ops->console_write(chan->fd, buf, len);
353 if(chan->primary) ret = n;
355 return ret;
358 int console_open_chan(struct line *line, struct console *co)
360 int err;
362 err = open_chan(&line->chan_list);
363 if(err)
364 return err;
366 printk("Console initialized on /dev/%s%d\n", co->name, co->index);
367 return 0;
370 int chan_window_size(struct list_head *chans, unsigned short *rows_out,
371 unsigned short *cols_out)
373 struct list_head *ele;
374 struct chan *chan;
376 list_for_each(ele, chans){
377 chan = list_entry(ele, struct chan, list);
378 if(chan->primary){
379 if(chan->ops->window_size == NULL)
380 return 0;
381 return chan->ops->window_size(chan->fd, chan->data,
382 rows_out, cols_out);
385 return 0;
388 static void free_one_chan(struct chan *chan, int delay_free_irq)
390 list_del(&chan->list);
392 close_one_chan(chan, delay_free_irq);
394 if(chan->ops->free != NULL)
395 (*chan->ops->free)(chan->data);
397 if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
398 kfree(chan);
401 static void free_chan(struct list_head *chans, int delay_free_irq)
403 struct list_head *ele, *next;
404 struct chan *chan;
406 list_for_each_safe(ele, next, chans){
407 chan = list_entry(ele, struct chan, list);
408 free_one_chan(chan, delay_free_irq);
412 static int one_chan_config_string(struct chan *chan, char *str, int size,
413 char **error_out)
415 int n = 0;
417 if(chan == NULL){
418 CONFIG_CHUNK(str, size, n, "none", 1);
419 return n;
422 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
424 if(chan->dev == NULL){
425 CONFIG_CHUNK(str, size, n, "", 1);
426 return n;
429 CONFIG_CHUNK(str, size, n, ":", 0);
430 CONFIG_CHUNK(str, size, n, chan->dev, 0);
432 return n;
435 static int chan_pair_config_string(struct chan *in, struct chan *out,
436 char *str, int size, char **error_out)
438 int n;
440 n = one_chan_config_string(in, str, size, error_out);
441 str += n;
442 size -= n;
444 if(in == out){
445 CONFIG_CHUNK(str, size, n, "", 1);
446 return n;
449 CONFIG_CHUNK(str, size, n, ",", 1);
450 n = one_chan_config_string(out, str, size, error_out);
451 str += n;
452 size -= n;
453 CONFIG_CHUNK(str, size, n, "", 1);
455 return n;
458 int chan_config_string(struct list_head *chans, char *str, int size,
459 char **error_out)
461 struct list_head *ele;
462 struct chan *chan, *in = NULL, *out = NULL;
464 list_for_each(ele, chans){
465 chan = list_entry(ele, struct chan, list);
466 if(!chan->primary)
467 continue;
468 if(chan->input)
469 in = chan;
470 if(chan->output)
471 out = chan;
474 return chan_pair_config_string(in, out, str, size, error_out);
477 struct chan_type {
478 char *key;
479 const struct chan_ops *ops;
482 static const struct chan_type chan_table[] = {
483 { "fd", &fd_ops },
485 #ifdef CONFIG_NULL_CHAN
486 { "null", &null_ops },
487 #else
488 { "null", &not_configged_ops },
489 #endif
491 #ifdef CONFIG_PORT_CHAN
492 { "port", &port_ops },
493 #else
494 { "port", &not_configged_ops },
495 #endif
497 #ifdef CONFIG_PTY_CHAN
498 { "pty", &pty_ops },
499 { "pts", &pts_ops },
500 #else
501 { "pty", &not_configged_ops },
502 { "pts", &not_configged_ops },
503 #endif
505 #ifdef CONFIG_TTY_CHAN
506 { "tty", &tty_ops },
507 #else
508 { "tty", &not_configged_ops },
509 #endif
511 #ifdef CONFIG_XTERM_CHAN
512 { "xterm", &xterm_ops },
513 #else
514 { "xterm", &not_configged_ops },
515 #endif
518 static struct chan *parse_chan(struct line *line, char *str, int device,
519 const struct chan_opts *opts, char **error_out)
521 const struct chan_type *entry;
522 const struct chan_ops *ops;
523 struct chan *chan;
524 void *data;
525 int i;
527 ops = NULL;
528 data = NULL;
529 for(i = 0; i < ARRAY_SIZE(chan_table); i++){
530 entry = &chan_table[i];
531 if(!strncmp(str, entry->key, strlen(entry->key))){
532 ops = entry->ops;
533 str += strlen(entry->key);
534 break;
537 if(ops == NULL){
538 *error_out = "No match for configured backends";
539 return NULL;
542 data = (*ops->init)(str, device, opts);
543 if(data == NULL){
544 *error_out = "Configuration failed";
545 return NULL;
548 chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
549 if(chan == NULL){
550 *error_out = "Memory allocation failed";
551 return NULL;
553 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
554 .free_list =
555 LIST_HEAD_INIT(chan->free_list),
556 .line = line,
557 .primary = 1,
558 .input = 0,
559 .output = 0,
560 .opened = 0,
561 .enabled = 0,
562 .fd = -1,
563 .ops = ops,
564 .data = data });
565 return chan;
568 int parse_chan_pair(char *str, struct line *line, int device,
569 const struct chan_opts *opts, char **error_out)
571 struct list_head *chans = &line->chan_list;
572 struct chan *new, *chan;
573 char *in, *out;
575 if(!list_empty(chans)){
576 chan = list_entry(chans->next, struct chan, list);
577 free_chan(chans, 0);
578 INIT_LIST_HEAD(chans);
581 out = strchr(str, ',');
582 if(out != NULL){
583 in = str;
584 *out = '\0';
585 out++;
586 new = parse_chan(line, in, device, opts, error_out);
587 if(new == NULL)
588 return -1;
590 new->input = 1;
591 list_add(&new->list, chans);
593 new = parse_chan(line, out, device, opts, error_out);
594 if(new == NULL)
595 return -1;
597 list_add(&new->list, chans);
598 new->output = 1;
600 else {
601 new = parse_chan(line, str, device, opts, error_out);
602 if(new == NULL)
603 return -1;
605 list_add(&new->list, chans);
606 new->input = 1;
607 new->output = 1;
609 return 0;
612 int chan_out_fd(struct list_head *chans)
614 struct list_head *ele;
615 struct chan *chan;
617 list_for_each(ele, chans){
618 chan = list_entry(ele, struct chan, list);
619 if(chan->primary && chan->output)
620 return chan->fd;
622 return -1;
625 void chan_interrupt(struct list_head *chans, struct delayed_work *task,
626 struct tty_struct *tty, int irq)
628 struct list_head *ele, *next;
629 struct chan *chan;
630 int err;
631 char c;
633 list_for_each_safe(ele, next, chans){
634 chan = list_entry(ele, struct chan, list);
635 if(!chan->input || (chan->ops->read == NULL)) continue;
636 do {
637 if (tty && !tty_buffer_request_room(tty, 1)) {
638 schedule_delayed_work(task, 1);
639 goto out;
641 err = chan->ops->read(chan->fd, &c, chan->data);
642 if(err > 0)
643 tty_receive_char(tty, c);
644 } while(err > 0);
646 if(err == 0) reactivate_fd(chan->fd, irq);
647 if(err == -EIO){
648 if(chan->primary){
649 if(tty != NULL)
650 tty_hangup(tty);
651 close_chan(chans, 1);
652 return;
654 else close_one_chan(chan, 1);
657 out:
658 if(tty) tty_flip_buffer_push(tty);