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"
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 "
30 static int not_configged_open(int input, int output, int primary, void *data,
33 printk("Using a channel type which is configured out of "
38 static void not_configged_close(int fd, void *data)
40 printk("Using a channel type which is configured out of "
44 static int not_configged_read(int fd, char *c_out, void *data)
46 printk("Using a channel type which is configured out of "
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 "
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 "
65 static int not_configged_window_size(int fd, void *data, unsigned short *rows,
68 printk("Using a channel type which is configured out of "
73 static void not_configged_free(void *data)
75 printk("Using a channel type which is configured out of "
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,
90 #endif /* CONFIG_NOCONFIG_CHAN */
92 void generic_close(int fd, void *unused)
97 int generic_read(int fd, char *c_out, void *unused)
101 n = os_read_file(fd, c_out, sizeof(*c_out));
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)
123 ret = os_window_size(fd, &rows, &cols);
127 ret = ((*rows_out != rows) || (*cols_out != cols));
135 void generic_free(void *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)){
149 else if(ch == START_CHAR(tty)){
155 tty_insert_flip_char(tty, ch, TTY_NORMAL);
158 static int open_one_chan(struct chan *chan)
165 if(chan->ops->open == NULL)
167 else fd = (*chan->ops->open)(chan->input, chan->output, chan->primary,
168 chan->data, &chan->dev);
172 err = os_set_fd_block(fd, 0);
174 (*chan->ops->close)(fd, chan->data);
184 int open_chan(struct list_head *chans)
186 struct list_head *ele;
190 list_for_each(ele, chans){
191 chan = list_entry(ele, struct chan, list);
192 ret = open_one_chan(chan);
199 void chan_enable_winch(struct list_head *chans, struct tty_struct *tty)
201 struct list_head *ele;
204 list_for_each(ele, chans){
205 chan = list_entry(ele, struct chan, list);
206 if(chan->primary && chan->output && chan->ops->winch){
207 register_winch(chan->fd, tty);
213 int enable_chan(struct line *line)
215 struct list_head *ele;
219 list_for_each(ele, &line->chan_list){
220 chan = list_entry(ele, struct chan, list);
221 err = open_one_chan(chan);
231 err = line_setup_irq(chan->fd, chan->input, chan->output, line,
242 close_chan(&line->chan_list, 0);
246 /* Items are added in IRQ context, when free_irq can't be called, and
247 * removed in process context, when it can.
248 * This handles interrupt sources which disappear, and which need to
249 * be permanently disabled. This is discovered in IRQ context, but
250 * the freeing of the IRQ must be done later.
252 static DEFINE_SPINLOCK(irqs_to_free_lock);
253 static LIST_HEAD(irqs_to_free);
259 struct list_head *ele;
262 spin_lock_irqsave(&irqs_to_free_lock, flags);
263 list_splice_init(&irqs_to_free, &list);
264 spin_unlock_irqrestore(&irqs_to_free_lock, flags);
266 list_for_each(ele, &list){
267 chan = list_entry(ele, struct chan, free_list);
270 free_irq(chan->line->driver->read_irq, chan);
272 free_irq(chan->line->driver->write_irq, chan);
277 static void close_one_chan(struct chan *chan, int delay_free_irq)
285 spin_lock_irqsave(&irqs_to_free_lock, flags);
286 list_add(&chan->free_list, &irqs_to_free);
287 spin_unlock_irqrestore(&irqs_to_free_lock, flags);
291 free_irq(chan->line->driver->read_irq, chan);
293 free_irq(chan->line->driver->write_irq, chan);
296 if(chan->ops->close != NULL)
297 (*chan->ops->close)(chan->fd, chan->data);
303 void close_chan(struct list_head *chans, int delay_free_irq)
307 /* Close in reverse order as open in case more than one of them
308 * refers to the same device and they save and restore that device's
309 * state. Then, the first one opened will have the original state,
310 * so it must be the last closed.
312 list_for_each_entry_reverse(chan, chans, list) {
313 close_one_chan(chan, delay_free_irq);
317 void deactivate_chan(struct list_head *chans, int irq)
319 struct list_head *ele;
322 list_for_each(ele, chans) {
323 chan = list_entry(ele, struct chan, list);
325 if(chan->enabled && chan->input)
326 deactivate_fd(chan->fd, irq);
330 void reactivate_chan(struct list_head *chans, int irq)
332 struct list_head *ele;
335 list_for_each(ele, chans) {
336 chan = list_entry(ele, struct chan, list);
338 if(chan->enabled && chan->input)
339 reactivate_fd(chan->fd, irq);
343 int write_chan(struct list_head *chans, const char *buf, int len,
346 struct list_head *ele;
347 struct chan *chan = NULL;
350 list_for_each(ele, chans) {
351 chan = list_entry(ele, struct chan, list);
352 if (!chan->output || (chan->ops->write == NULL))
354 n = chan->ops->write(chan->fd, buf, len, chan->data);
357 if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
358 reactivate_fd(chan->fd, write_irq);
364 int console_write_chan(struct list_head *chans, const char *buf, int len)
366 struct list_head *ele;
370 list_for_each(ele, chans){
371 chan = list_entry(ele, struct chan, list);
372 if(!chan->output || (chan->ops->console_write == NULL))
374 n = chan->ops->console_write(chan->fd, buf, len);
375 if(chan->primary) ret = n;
380 int console_open_chan(struct line *line, struct console *co)
384 err = open_chan(&line->chan_list);
388 printk("Console initialized on /dev/%s%d\n", co->name, co->index);
392 int chan_window_size(struct list_head *chans, unsigned short *rows_out,
393 unsigned short *cols_out)
395 struct list_head *ele;
398 list_for_each(ele, chans){
399 chan = list_entry(ele, struct chan, list);
401 if(chan->ops->window_size == NULL)
403 return chan->ops->window_size(chan->fd, chan->data,
410 static void free_one_chan(struct chan *chan, int delay_free_irq)
412 list_del(&chan->list);
414 close_one_chan(chan, delay_free_irq);
416 if(chan->ops->free != NULL)
417 (*chan->ops->free)(chan->data);
419 if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
423 static void free_chan(struct list_head *chans, int delay_free_irq)
425 struct list_head *ele, *next;
428 list_for_each_safe(ele, next, chans){
429 chan = list_entry(ele, struct chan, list);
430 free_one_chan(chan, delay_free_irq);
434 static int one_chan_config_string(struct chan *chan, char *str, int size,
440 CONFIG_CHUNK(str, size, n, "none", 1);
444 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
446 if(chan->dev == NULL){
447 CONFIG_CHUNK(str, size, n, "", 1);
451 CONFIG_CHUNK(str, size, n, ":", 0);
452 CONFIG_CHUNK(str, size, n, chan->dev, 0);
457 static int chan_pair_config_string(struct chan *in, struct chan *out,
458 char *str, int size, char **error_out)
462 n = one_chan_config_string(in, str, size, error_out);
467 CONFIG_CHUNK(str, size, n, "", 1);
471 CONFIG_CHUNK(str, size, n, ",", 1);
472 n = one_chan_config_string(out, str, size, error_out);
475 CONFIG_CHUNK(str, size, n, "", 1);
480 int chan_config_string(struct list_head *chans, char *str, int size,
483 struct list_head *ele;
484 struct chan *chan, *in = NULL, *out = NULL;
486 list_for_each(ele, chans){
487 chan = list_entry(ele, struct chan, list);
496 return chan_pair_config_string(in, out, str, size, error_out);
501 const struct chan_ops *ops;
504 static const struct chan_type chan_table[] = {
507 #ifdef CONFIG_NULL_CHAN
508 { "null", &null_ops },
510 { "null", ¬_configged_ops },
513 #ifdef CONFIG_PORT_CHAN
514 { "port", &port_ops },
516 { "port", ¬_configged_ops },
519 #ifdef CONFIG_PTY_CHAN
523 { "pty", ¬_configged_ops },
524 { "pts", ¬_configged_ops },
527 #ifdef CONFIG_TTY_CHAN
530 { "tty", ¬_configged_ops },
533 #ifdef CONFIG_XTERM_CHAN
534 { "xterm", &xterm_ops },
536 { "xterm", ¬_configged_ops },
540 static struct chan *parse_chan(struct line *line, char *str, int device,
541 const struct chan_opts *opts, char **error_out)
543 const struct chan_type *entry;
544 const struct chan_ops *ops;
551 for(i = 0; i < ARRAY_SIZE(chan_table); i++){
552 entry = &chan_table[i];
553 if(!strncmp(str, entry->key, strlen(entry->key))){
555 str += strlen(entry->key);
560 *error_out = "No match for configured backends";
564 data = (*ops->init)(str, device, opts);
566 *error_out = "Configuration failed";
570 chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
572 *error_out = "Memory allocation failed";
575 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
577 LIST_HEAD_INIT(chan->free_list),
590 int parse_chan_pair(char *str, struct line *line, int device,
591 const struct chan_opts *opts, char **error_out)
593 struct list_head *chans = &line->chan_list;
594 struct chan *new, *chan;
597 if(!list_empty(chans)){
598 chan = list_entry(chans->next, struct chan, list);
600 INIT_LIST_HEAD(chans);
603 out = strchr(str, ',');
608 new = parse_chan(line, in, device, opts, error_out);
613 list_add(&new->list, chans);
615 new = parse_chan(line, out, device, opts, error_out);
619 list_add(&new->list, chans);
623 new = parse_chan(line, str, device, opts, error_out);
627 list_add(&new->list, chans);
634 int chan_out_fd(struct list_head *chans)
636 struct list_head *ele;
639 list_for_each(ele, chans){
640 chan = list_entry(ele, struct chan, list);
641 if(chan->primary && chan->output)
647 void chan_interrupt(struct list_head *chans, struct delayed_work *task,
648 struct tty_struct *tty, int irq)
650 struct list_head *ele, *next;
655 list_for_each_safe(ele, next, chans){
656 chan = list_entry(ele, struct chan, list);
657 if(!chan->input || (chan->ops->read == NULL)) continue;
659 if (tty && !tty_buffer_request_room(tty, 1)) {
660 schedule_delayed_work(task, 1);
663 err = chan->ops->read(chan->fd, &c, chan->data);
665 tty_receive_char(tty, c);
668 if(err == 0) reactivate_fd(chan->fd, irq);
673 close_chan(chans, 1);
676 else close_one_chan(chan, 1);
680 if(tty) tty_flip_buffer_push(tty);