2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
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>
14 #include "chan_kern.h"
15 #include "user_util.h"
22 #ifdef CONFIG_NOCONFIG_CHAN
23 static void *not_configged_init(char *str, int device,
24 const struct chan_opts *opts)
26 printk("Using a channel type which is configured out of "
31 static int not_configged_open(int input, int output, int primary, void *data,
34 printk("Using a channel type which is configured out of "
39 static void not_configged_close(int fd, void *data)
41 printk("Using a channel type which is configured out of "
45 static int not_configged_read(int fd, char *c_out, void *data)
47 printk("Using a channel type which is configured out of "
52 static int not_configged_write(int fd, const char *buf, int len, void *data)
54 printk("Using a channel type which is configured out of "
59 static int not_configged_console_write(int fd, const char *buf, int len)
61 printk("Using a channel type which is configured out of "
66 static int not_configged_window_size(int fd, void *data, unsigned short *rows,
69 printk("Using a channel type which is configured out of "
74 static void not_configged_free(void *data)
76 printk("Using a channel type which is configured out of "
80 static const struct chan_ops not_configged_ops = {
81 .init = not_configged_init,
82 .open = not_configged_open,
83 .close = not_configged_close,
84 .read = not_configged_read,
85 .write = not_configged_write,
86 .console_write = not_configged_console_write,
87 .window_size = not_configged_window_size,
88 .free = not_configged_free,
91 #endif /* CONFIG_NOCONFIG_CHAN */
93 void generic_close(int fd, void *unused)
98 int generic_read(int fd, char *c_out, void *unused)
102 n = os_read_file(fd, c_out, sizeof(*c_out));
111 /* XXX Trivial wrapper around os_write_file */
113 int generic_write(int fd, const char *buf, int n, void *unused)
115 return os_write_file(fd, buf, n);
118 int generic_window_size(int fd, void *unused, unsigned short *rows_out,
119 unsigned short *cols_out)
124 ret = os_window_size(fd, &rows, &cols);
128 ret = ((*rows_out != rows) || (*cols_out != cols));
136 void generic_free(void *data)
141 static void tty_receive_char(struct tty_struct *tty, char ch)
143 if(tty == NULL) return;
145 if(I_IXON(tty) && !I_IXOFF(tty) && !tty->raw) {
146 if(ch == STOP_CHAR(tty)){
150 else if(ch == START_CHAR(tty)){
156 tty_insert_flip_char(tty, ch, TTY_NORMAL);
159 static int open_one_chan(struct chan *chan)
166 if(chan->ops->open == NULL)
168 else fd = (*chan->ops->open)(chan->input, chan->output, chan->primary,
169 chan->data, &chan->dev);
178 int open_chan(struct list_head *chans)
180 struct list_head *ele;
184 list_for_each(ele, chans){
185 chan = list_entry(ele, struct chan, list);
186 ret = open_one_chan(chan);
193 void chan_enable_winch(struct list_head *chans, struct tty_struct *tty)
195 struct list_head *ele;
198 list_for_each(ele, chans){
199 chan = list_entry(ele, struct chan, list);
200 if(chan->primary && chan->output && chan->ops->winch){
201 register_winch(chan->fd, tty);
207 void enable_chan(struct line *line)
209 struct list_head *ele;
212 list_for_each(ele, &line->chan_list){
213 chan = list_entry(ele, struct chan, list);
214 if(open_one_chan(chan))
219 line_setup_irq(chan->fd, chan->input, chan->output, line,
225 /* Items are added in IRQ context, when free_irq can't be called, and
226 * removed in process context, when it can.
227 * This handles interrupt sources which disappear, and which need to
228 * be permanently disabled. This is discovered in IRQ context, but
229 * the freeing of the IRQ must be done later.
231 static DEFINE_SPINLOCK(irqs_to_free_lock);
232 static LIST_HEAD(irqs_to_free);
238 struct list_head *ele;
241 spin_lock_irqsave(&irqs_to_free_lock, flags);
242 list_splice_init(&irqs_to_free, &list);
243 spin_unlock_irqrestore(&irqs_to_free_lock, flags);
245 list_for_each(ele, &list){
246 chan = list_entry(ele, struct chan, free_list);
249 free_irq(chan->line->driver->read_irq, chan);
251 free_irq(chan->line->driver->write_irq, chan);
256 static void close_one_chan(struct chan *chan, int delay_free_irq)
264 spin_lock_irqsave(&irqs_to_free_lock, flags);
265 list_add(&chan->free_list, &irqs_to_free);
266 spin_unlock_irqrestore(&irqs_to_free_lock, flags);
270 free_irq(chan->line->driver->read_irq, chan);
272 free_irq(chan->line->driver->write_irq, chan);
275 if(chan->ops->close != NULL)
276 (*chan->ops->close)(chan->fd, chan->data);
282 void close_chan(struct list_head *chans, int delay_free_irq)
286 /* Close in reverse order as open in case more than one of them
287 * refers to the same device and they save and restore that device's
288 * state. Then, the first one opened will have the original state,
289 * so it must be the last closed.
291 list_for_each_entry_reverse(chan, chans, list) {
292 close_one_chan(chan, delay_free_irq);
296 void deactivate_chan(struct list_head *chans, int irq)
298 struct list_head *ele;
301 list_for_each(ele, chans) {
302 chan = list_entry(ele, struct chan, list);
304 if(chan->enabled && chan->input)
305 deactivate_fd(chan->fd, irq);
309 void reactivate_chan(struct list_head *chans, int irq)
311 struct list_head *ele;
314 list_for_each(ele, chans) {
315 chan = list_entry(ele, struct chan, list);
317 if(chan->enabled && chan->input)
318 reactivate_fd(chan->fd, irq);
322 int write_chan(struct list_head *chans, const char *buf, int len,
325 struct list_head *ele;
326 struct chan *chan = NULL;
329 list_for_each(ele, chans) {
330 chan = list_entry(ele, struct chan, list);
331 if (!chan->output || (chan->ops->write == NULL))
333 n = chan->ops->write(chan->fd, buf, len, chan->data);
336 if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
337 reactivate_fd(chan->fd, write_irq);
343 int console_write_chan(struct list_head *chans, const char *buf, int len)
345 struct list_head *ele;
349 list_for_each(ele, chans){
350 chan = list_entry(ele, struct chan, list);
351 if(!chan->output || (chan->ops->console_write == NULL))
353 n = chan->ops->console_write(chan->fd, buf, len);
354 if(chan->primary) ret = n;
359 int console_open_chan(struct line *line, struct console *co)
363 err = open_chan(&line->chan_list);
367 printk("Console initialized on /dev/%s%d\n", co->name, co->index);
371 int chan_window_size(struct list_head *chans, unsigned short *rows_out,
372 unsigned short *cols_out)
374 struct list_head *ele;
377 list_for_each(ele, chans){
378 chan = list_entry(ele, struct chan, list);
380 if(chan->ops->window_size == NULL)
382 return chan->ops->window_size(chan->fd, chan->data,
389 static void free_one_chan(struct chan *chan, int delay_free_irq)
391 list_del(&chan->list);
393 close_one_chan(chan, delay_free_irq);
395 if(chan->ops->free != NULL)
396 (*chan->ops->free)(chan->data);
398 if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
402 static void free_chan(struct list_head *chans, int delay_free_irq)
404 struct list_head *ele, *next;
407 list_for_each_safe(ele, next, chans){
408 chan = list_entry(ele, struct chan, list);
409 free_one_chan(chan, delay_free_irq);
413 static int one_chan_config_string(struct chan *chan, char *str, int size,
419 CONFIG_CHUNK(str, size, n, "none", 1);
423 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
425 if(chan->dev == NULL){
426 CONFIG_CHUNK(str, size, n, "", 1);
430 CONFIG_CHUNK(str, size, n, ":", 0);
431 CONFIG_CHUNK(str, size, n, chan->dev, 0);
436 static int chan_pair_config_string(struct chan *in, struct chan *out,
437 char *str, int size, char **error_out)
441 n = one_chan_config_string(in, str, size, error_out);
446 CONFIG_CHUNK(str, size, n, "", 1);
450 CONFIG_CHUNK(str, size, n, ",", 1);
451 n = one_chan_config_string(out, str, size, error_out);
454 CONFIG_CHUNK(str, size, n, "", 1);
459 int chan_config_string(struct list_head *chans, char *str, int size,
462 struct list_head *ele;
463 struct chan *chan, *in = NULL, *out = NULL;
465 list_for_each(ele, chans){
466 chan = list_entry(ele, struct chan, list);
475 return chan_pair_config_string(in, out, str, size, error_out);
480 const struct chan_ops *ops;
483 static const struct chan_type chan_table[] = {
486 #ifdef CONFIG_NULL_CHAN
487 { "null", &null_ops },
489 { "null", ¬_configged_ops },
492 #ifdef CONFIG_PORT_CHAN
493 { "port", &port_ops },
495 { "port", ¬_configged_ops },
498 #ifdef CONFIG_PTY_CHAN
502 { "pty", ¬_configged_ops },
503 { "pts", ¬_configged_ops },
506 #ifdef CONFIG_TTY_CHAN
509 { "tty", ¬_configged_ops },
512 #ifdef CONFIG_XTERM_CHAN
513 { "xterm", &xterm_ops },
515 { "xterm", ¬_configged_ops },
519 static struct chan *parse_chan(struct line *line, char *str, int device,
520 const struct chan_opts *opts, char **error_out)
522 const struct chan_type *entry;
523 const struct chan_ops *ops;
530 for(i = 0; i < ARRAY_SIZE(chan_table); i++){
531 entry = &chan_table[i];
532 if(!strncmp(str, entry->key, strlen(entry->key))){
534 str += strlen(entry->key);
539 *error_out = "No match for configured backends";
543 data = (*ops->init)(str, device, opts);
545 *error_out = "Configuration failed";
549 chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
551 *error_out = "Memory allocation failed";
554 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
556 LIST_HEAD_INIT(chan->free_list),
569 int parse_chan_pair(char *str, struct line *line, int device,
570 const struct chan_opts *opts, char **error_out)
572 struct list_head *chans = &line->chan_list;
573 struct chan *new, *chan;
576 if(!list_empty(chans)){
577 chan = list_entry(chans->next, struct chan, list);
579 INIT_LIST_HEAD(chans);
582 out = strchr(str, ',');
587 new = parse_chan(line, in, device, opts, error_out);
592 list_add(&new->list, chans);
594 new = parse_chan(line, out, device, opts, error_out);
598 list_add(&new->list, chans);
602 new = parse_chan(line, str, device, opts, error_out);
606 list_add(&new->list, chans);
613 int chan_out_fd(struct list_head *chans)
615 struct list_head *ele;
618 list_for_each(ele, chans){
619 chan = list_entry(ele, struct chan, list);
620 if(chan->primary && chan->output)
626 void chan_interrupt(struct list_head *chans, struct delayed_work *task,
627 struct tty_struct *tty, int irq)
629 struct list_head *ele, *next;
634 list_for_each_safe(ele, next, chans){
635 chan = list_entry(ele, struct chan, list);
636 if(!chan->input || (chan->ops->read == NULL)) continue;
638 if (tty && !tty_buffer_request_room(tty, 1)) {
639 schedule_delayed_work(task, 1);
642 err = chan->ops->read(chan->fd, &c, chan->data);
644 tty_receive_char(tty, c);
647 if(err == 0) reactivate_fd(chan->fd, irq);
652 close_chan(chans, 1);
655 else close_one_chan(chan, 1);
659 if(tty) tty_flip_buffer_push(tty);