Allow ARG_MAX execve string space even with a small stack limit
[wrt350n-kernel.git] / arch / um / drivers / chan_kern.c
blobdb3082b4da46573e7b7ddc427d5e4bbdbbdda366
1 /*
2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
3 * Licensed under the GPL
4 */
6 #include <linux/slab.h>
7 #include <linux/tty.h>
8 #include <linux/tty_flip.h>
9 #include "chan_kern.h"
10 #include "os.h"
12 #ifdef CONFIG_NOCONFIG_CHAN
13 static void *not_configged_init(char *str, int device,
14 const struct chan_opts *opts)
16 printk(KERN_ERR "Using a channel type which is configured out of "
17 "UML\n");
18 return NULL;
21 static int not_configged_open(int input, int output, int primary, void *data,
22 char **dev_out)
24 printk(KERN_ERR "Using a channel type which is configured out of "
25 "UML\n");
26 return -ENODEV;
29 static void not_configged_close(int fd, void *data)
31 printk(KERN_ERR "Using a channel type which is configured out of "
32 "UML\n");
35 static int not_configged_read(int fd, char *c_out, void *data)
37 printk(KERN_ERR "Using a channel type which is configured out of "
38 "UML\n");
39 return -EIO;
42 static int not_configged_write(int fd, const char *buf, int len, void *data)
44 printk(KERN_ERR "Using a channel type which is configured out of "
45 "UML\n");
46 return -EIO;
49 static int not_configged_console_write(int fd, const char *buf, int len)
51 printk(KERN_ERR "Using a channel type which is configured out of "
52 "UML\n");
53 return -EIO;
56 static int not_configged_window_size(int fd, void *data, unsigned short *rows,
57 unsigned short *cols)
59 printk(KERN_ERR "Using a channel type which is configured out of "
60 "UML\n");
61 return -ENODEV;
64 static void not_configged_free(void *data)
66 printk(KERN_ERR "Using a channel type which is configured out of "
67 "UML\n");
70 static const struct chan_ops not_configged_ops = {
71 .init = not_configged_init,
72 .open = not_configged_open,
73 .close = not_configged_close,
74 .read = not_configged_read,
75 .write = not_configged_write,
76 .console_write = not_configged_console_write,
77 .window_size = not_configged_window_size,
78 .free = not_configged_free,
79 .winch = 0,
81 #endif /* CONFIG_NOCONFIG_CHAN */
83 static void tty_receive_char(struct tty_struct *tty, char ch)
85 if (tty == NULL)
86 return;
88 if (I_IXON(tty) && !I_IXOFF(tty) && !tty->raw) {
89 if (ch == STOP_CHAR(tty)) {
90 stop_tty(tty);
91 return;
93 else if (ch == START_CHAR(tty)) {
94 start_tty(tty);
95 return;
99 tty_insert_flip_char(tty, ch, TTY_NORMAL);
102 static int open_one_chan(struct chan *chan)
104 int fd, err;
106 if (chan->opened)
107 return 0;
109 if (chan->ops->open == NULL)
110 fd = 0;
111 else fd = (*chan->ops->open)(chan->input, chan->output, chan->primary,
112 chan->data, &chan->dev);
113 if (fd < 0)
114 return fd;
116 err = os_set_fd_block(fd, 0);
117 if (err) {
118 (*chan->ops->close)(fd, chan->data);
119 return err;
122 chan->fd = fd;
124 chan->opened = 1;
125 return 0;
128 int open_chan(struct list_head *chans)
130 struct list_head *ele;
131 struct chan *chan;
132 int ret, err = 0;
134 list_for_each(ele, chans) {
135 chan = list_entry(ele, struct chan, list);
136 ret = open_one_chan(chan);
137 if (chan->primary)
138 err = ret;
140 return err;
143 void chan_enable_winch(struct list_head *chans, struct tty_struct *tty)
145 struct list_head *ele;
146 struct chan *chan;
148 list_for_each(ele, chans) {
149 chan = list_entry(ele, struct chan, list);
150 if (chan->primary && chan->output && chan->ops->winch) {
151 register_winch(chan->fd, tty);
152 return;
157 int enable_chan(struct line *line)
159 struct list_head *ele;
160 struct chan *chan;
161 int err;
163 list_for_each(ele, &line->chan_list) {
164 chan = list_entry(ele, struct chan, list);
165 err = open_one_chan(chan);
166 if (err) {
167 if (chan->primary)
168 goto out_close;
170 continue;
173 if (chan->enabled)
174 continue;
175 err = line_setup_irq(chan->fd, chan->input, chan->output, line,
176 chan);
177 if (err)
178 goto out_close;
180 chan->enabled = 1;
183 return 0;
185 out_close:
186 close_chan(&line->chan_list, 0);
187 return err;
190 /* Items are added in IRQ context, when free_irq can't be called, and
191 * removed in process context, when it can.
192 * This handles interrupt sources which disappear, and which need to
193 * be permanently disabled. This is discovered in IRQ context, but
194 * the freeing of the IRQ must be done later.
196 static DEFINE_SPINLOCK(irqs_to_free_lock);
197 static LIST_HEAD(irqs_to_free);
199 void free_irqs(void)
201 struct chan *chan;
202 LIST_HEAD(list);
203 struct list_head *ele;
204 unsigned long flags;
206 spin_lock_irqsave(&irqs_to_free_lock, flags);
207 list_splice_init(&irqs_to_free, &list);
208 spin_unlock_irqrestore(&irqs_to_free_lock, flags);
210 list_for_each(ele, &list) {
211 chan = list_entry(ele, struct chan, free_list);
213 if (chan->input)
214 free_irq(chan->line->driver->read_irq, chan);
215 if (chan->output)
216 free_irq(chan->line->driver->write_irq, chan);
217 chan->enabled = 0;
221 static void close_one_chan(struct chan *chan, int delay_free_irq)
223 unsigned long flags;
225 if (!chan->opened)
226 return;
228 if (delay_free_irq) {
229 spin_lock_irqsave(&irqs_to_free_lock, flags);
230 list_add(&chan->free_list, &irqs_to_free);
231 spin_unlock_irqrestore(&irqs_to_free_lock, flags);
233 else {
234 if (chan->input)
235 free_irq(chan->line->driver->read_irq, chan);
236 if (chan->output)
237 free_irq(chan->line->driver->write_irq, chan);
238 chan->enabled = 0;
240 if (chan->ops->close != NULL)
241 (*chan->ops->close)(chan->fd, chan->data);
243 chan->opened = 0;
244 chan->fd = -1;
247 void close_chan(struct list_head *chans, int delay_free_irq)
249 struct chan *chan;
251 /* Close in reverse order as open in case more than one of them
252 * refers to the same device and they save and restore that device's
253 * state. Then, the first one opened will have the original state,
254 * so it must be the last closed.
256 list_for_each_entry_reverse(chan, chans, list) {
257 close_one_chan(chan, delay_free_irq);
261 void deactivate_chan(struct list_head *chans, int irq)
263 struct list_head *ele;
265 struct chan *chan;
266 list_for_each(ele, chans) {
267 chan = list_entry(ele, struct chan, list);
269 if (chan->enabled && chan->input)
270 deactivate_fd(chan->fd, irq);
274 void reactivate_chan(struct list_head *chans, int irq)
276 struct list_head *ele;
277 struct chan *chan;
279 list_for_each(ele, chans) {
280 chan = list_entry(ele, struct chan, list);
282 if (chan->enabled && chan->input)
283 reactivate_fd(chan->fd, irq);
287 int write_chan(struct list_head *chans, const char *buf, int len,
288 int write_irq)
290 struct list_head *ele;
291 struct chan *chan = NULL;
292 int n, ret = 0;
294 if (len == 0)
295 return 0;
297 list_for_each(ele, chans) {
298 chan = list_entry(ele, struct chan, list);
299 if (!chan->output || (chan->ops->write == NULL))
300 continue;
302 n = chan->ops->write(chan->fd, buf, len, chan->data);
303 if (chan->primary) {
304 ret = n;
305 if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
306 reactivate_fd(chan->fd, write_irq);
309 return ret;
312 int console_write_chan(struct list_head *chans, const char *buf, int len)
314 struct list_head *ele;
315 struct chan *chan;
316 int n, ret = 0;
318 list_for_each(ele, chans) {
319 chan = list_entry(ele, struct chan, list);
320 if (!chan->output || (chan->ops->console_write == NULL))
321 continue;
323 n = chan->ops->console_write(chan->fd, buf, len);
324 if (chan->primary)
325 ret = n;
327 return ret;
330 int console_open_chan(struct line *line, struct console *co)
332 int err;
334 err = open_chan(&line->chan_list);
335 if (err)
336 return err;
338 printk(KERN_INFO "Console initialized on /dev/%s%d\n", co->name,
339 co->index);
340 return 0;
343 int chan_window_size(struct list_head *chans, unsigned short *rows_out,
344 unsigned short *cols_out)
346 struct list_head *ele;
347 struct chan *chan;
349 list_for_each(ele, chans) {
350 chan = list_entry(ele, struct chan, list);
351 if (chan->primary) {
352 if (chan->ops->window_size == NULL)
353 return 0;
354 return chan->ops->window_size(chan->fd, chan->data,
355 rows_out, cols_out);
358 return 0;
361 static void free_one_chan(struct chan *chan, int delay_free_irq)
363 list_del(&chan->list);
365 close_one_chan(chan, delay_free_irq);
367 if (chan->ops->free != NULL)
368 (*chan->ops->free)(chan->data);
370 if (chan->primary && chan->output)
371 ignore_sigio_fd(chan->fd);
372 kfree(chan);
375 static void free_chan(struct list_head *chans, int delay_free_irq)
377 struct list_head *ele, *next;
378 struct chan *chan;
380 list_for_each_safe(ele, next, chans) {
381 chan = list_entry(ele, struct chan, list);
382 free_one_chan(chan, delay_free_irq);
386 static int one_chan_config_string(struct chan *chan, char *str, int size,
387 char **error_out)
389 int n = 0;
391 if (chan == NULL) {
392 CONFIG_CHUNK(str, size, n, "none", 1);
393 return n;
396 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
398 if (chan->dev == NULL) {
399 CONFIG_CHUNK(str, size, n, "", 1);
400 return n;
403 CONFIG_CHUNK(str, size, n, ":", 0);
404 CONFIG_CHUNK(str, size, n, chan->dev, 0);
406 return n;
409 static int chan_pair_config_string(struct chan *in, struct chan *out,
410 char *str, int size, char **error_out)
412 int n;
414 n = one_chan_config_string(in, str, size, error_out);
415 str += n;
416 size -= n;
418 if (in == out) {
419 CONFIG_CHUNK(str, size, n, "", 1);
420 return n;
423 CONFIG_CHUNK(str, size, n, ",", 1);
424 n = one_chan_config_string(out, str, size, error_out);
425 str += n;
426 size -= n;
427 CONFIG_CHUNK(str, size, n, "", 1);
429 return n;
432 int chan_config_string(struct list_head *chans, char *str, int size,
433 char **error_out)
435 struct list_head *ele;
436 struct chan *chan, *in = NULL, *out = NULL;
438 list_for_each(ele, chans) {
439 chan = list_entry(ele, struct chan, list);
440 if (!chan->primary)
441 continue;
442 if (chan->input)
443 in = chan;
444 if (chan->output)
445 out = chan;
448 return chan_pair_config_string(in, out, str, size, error_out);
451 struct chan_type {
452 char *key;
453 const struct chan_ops *ops;
456 static const struct chan_type chan_table[] = {
457 { "fd", &fd_ops },
459 #ifdef CONFIG_NULL_CHAN
460 { "null", &null_ops },
461 #else
462 { "null", &not_configged_ops },
463 #endif
465 #ifdef CONFIG_PORT_CHAN
466 { "port", &port_ops },
467 #else
468 { "port", &not_configged_ops },
469 #endif
471 #ifdef CONFIG_PTY_CHAN
472 { "pty", &pty_ops },
473 { "pts", &pts_ops },
474 #else
475 { "pty", &not_configged_ops },
476 { "pts", &not_configged_ops },
477 #endif
479 #ifdef CONFIG_TTY_CHAN
480 { "tty", &tty_ops },
481 #else
482 { "tty", &not_configged_ops },
483 #endif
485 #ifdef CONFIG_XTERM_CHAN
486 { "xterm", &xterm_ops },
487 #else
488 { "xterm", &not_configged_ops },
489 #endif
492 static struct chan *parse_chan(struct line *line, char *str, int device,
493 const struct chan_opts *opts, char **error_out)
495 const struct chan_type *entry;
496 const struct chan_ops *ops;
497 struct chan *chan;
498 void *data;
499 int i;
501 ops = NULL;
502 data = NULL;
503 for(i = 0; i < ARRAY_SIZE(chan_table); i++) {
504 entry = &chan_table[i];
505 if (!strncmp(str, entry->key, strlen(entry->key))) {
506 ops = entry->ops;
507 str += strlen(entry->key);
508 break;
511 if (ops == NULL) {
512 *error_out = "No match for configured backends";
513 return NULL;
516 data = (*ops->init)(str, device, opts);
517 if (data == NULL) {
518 *error_out = "Configuration failed";
519 return NULL;
522 chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
523 if (chan == NULL) {
524 *error_out = "Memory allocation failed";
525 return NULL;
527 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
528 .free_list =
529 LIST_HEAD_INIT(chan->free_list),
530 .line = line,
531 .primary = 1,
532 .input = 0,
533 .output = 0,
534 .opened = 0,
535 .enabled = 0,
536 .fd = -1,
537 .ops = ops,
538 .data = data });
539 return chan;
542 int parse_chan_pair(char *str, struct line *line, int device,
543 const struct chan_opts *opts, char **error_out)
545 struct list_head *chans = &line->chan_list;
546 struct chan *new, *chan;
547 char *in, *out;
549 if (!list_empty(chans)) {
550 chan = list_entry(chans->next, struct chan, list);
551 free_chan(chans, 0);
552 INIT_LIST_HEAD(chans);
555 out = strchr(str, ',');
556 if (out != NULL) {
557 in = str;
558 *out = '\0';
559 out++;
560 new = parse_chan(line, in, device, opts, error_out);
561 if (new == NULL)
562 return -1;
564 new->input = 1;
565 list_add(&new->list, chans);
567 new = parse_chan(line, out, device, opts, error_out);
568 if (new == NULL)
569 return -1;
571 list_add(&new->list, chans);
572 new->output = 1;
574 else {
575 new = parse_chan(line, str, device, opts, error_out);
576 if (new == NULL)
577 return -1;
579 list_add(&new->list, chans);
580 new->input = 1;
581 new->output = 1;
583 return 0;
586 int chan_out_fd(struct list_head *chans)
588 struct list_head *ele;
589 struct chan *chan;
591 list_for_each(ele, chans) {
592 chan = list_entry(ele, struct chan, list);
593 if (chan->primary && chan->output)
594 return chan->fd;
596 return -1;
599 void chan_interrupt(struct list_head *chans, struct delayed_work *task,
600 struct tty_struct *tty, int irq)
602 struct list_head *ele, *next;
603 struct chan *chan;
604 int err;
605 char c;
607 list_for_each_safe(ele, next, chans) {
608 chan = list_entry(ele, struct chan, list);
609 if (!chan->input || (chan->ops->read == NULL))
610 continue;
611 do {
612 if (tty && !tty_buffer_request_room(tty, 1)) {
613 schedule_delayed_work(task, 1);
614 goto out;
616 err = chan->ops->read(chan->fd, &c, chan->data);
617 if (err > 0)
618 tty_receive_char(tty, c);
619 } while (err > 0);
621 if (err == 0)
622 reactivate_fd(chan->fd, irq);
623 if (err == -EIO) {
624 if (chan->primary) {
625 if (tty != NULL)
626 tty_hangup(tty);
627 close_chan(chans, 1);
628 return;
630 else close_one_chan(chan, 1);
633 out:
634 if (tty)
635 tty_flip_buffer_push(tty);