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;
240 spin_lock_irq(&irqs_to_free_lock);
241 list_splice_init(&irqs_to_free, &list);
242 INIT_LIST_HEAD(&irqs_to_free);
243 spin_unlock_irq(&irqs_to_free_lock);
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)
262 spin_lock_irq(&irqs_to_free_lock);
263 list_add(&chan->free_list, &irqs_to_free);
264 spin_unlock_irq(&irqs_to_free_lock);
268 free_irq(chan->line->driver->read_irq, chan);
270 free_irq(chan->line->driver->write_irq, chan);
273 if(chan->ops->close != NULL)
274 (*chan->ops->close)(chan->fd, chan->data);
280 void close_chan(struct list_head *chans, int delay_free_irq)
284 /* Close in reverse order as open in case more than one of them
285 * refers to the same device and they save and restore that device's
286 * state. Then, the first one opened will have the original state,
287 * so it must be the last closed.
289 list_for_each_entry_reverse(chan, chans, list) {
290 close_one_chan(chan, delay_free_irq);
294 void deactivate_chan(struct list_head *chans, int irq)
296 struct list_head *ele;
299 list_for_each(ele, chans) {
300 chan = list_entry(ele, struct chan, list);
302 if(chan->enabled && chan->input)
303 deactivate_fd(chan->fd, irq);
307 void reactivate_chan(struct list_head *chans, int irq)
309 struct list_head *ele;
312 list_for_each(ele, chans) {
313 chan = list_entry(ele, struct chan, list);
315 if(chan->enabled && chan->input)
316 reactivate_fd(chan->fd, irq);
320 int write_chan(struct list_head *chans, const char *buf, int len,
323 struct list_head *ele;
324 struct chan *chan = NULL;
327 list_for_each(ele, chans) {
328 chan = list_entry(ele, struct chan, list);
329 if (!chan->output || (chan->ops->write == NULL))
331 n = chan->ops->write(chan->fd, buf, len, chan->data);
334 if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
335 reactivate_fd(chan->fd, write_irq);
341 int console_write_chan(struct list_head *chans, const char *buf, int len)
343 struct list_head *ele;
347 list_for_each(ele, chans){
348 chan = list_entry(ele, struct chan, list);
349 if(!chan->output || (chan->ops->console_write == NULL))
351 n = chan->ops->console_write(chan->fd, buf, len);
352 if(chan->primary) ret = n;
357 int console_open_chan(struct line *line, struct console *co)
361 err = open_chan(&line->chan_list);
365 printk("Console initialized on /dev/%s%d\n", co->name, co->index);
369 int chan_window_size(struct list_head *chans, unsigned short *rows_out,
370 unsigned short *cols_out)
372 struct list_head *ele;
375 list_for_each(ele, chans){
376 chan = list_entry(ele, struct chan, list);
378 if(chan->ops->window_size == NULL)
380 return chan->ops->window_size(chan->fd, chan->data,
387 static void free_one_chan(struct chan *chan, int delay_free_irq)
389 list_del(&chan->list);
391 close_one_chan(chan, delay_free_irq);
393 if(chan->ops->free != NULL)
394 (*chan->ops->free)(chan->data);
396 if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
400 static void free_chan(struct list_head *chans, int delay_free_irq)
402 struct list_head *ele, *next;
405 list_for_each_safe(ele, next, chans){
406 chan = list_entry(ele, struct chan, list);
407 free_one_chan(chan, delay_free_irq);
411 static int one_chan_config_string(struct chan *chan, char *str, int size,
417 CONFIG_CHUNK(str, size, n, "none", 1);
421 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
423 if(chan->dev == NULL){
424 CONFIG_CHUNK(str, size, n, "", 1);
428 CONFIG_CHUNK(str, size, n, ":", 0);
429 CONFIG_CHUNK(str, size, n, chan->dev, 0);
434 static int chan_pair_config_string(struct chan *in, struct chan *out,
435 char *str, int size, char **error_out)
439 n = one_chan_config_string(in, str, size, error_out);
444 CONFIG_CHUNK(str, size, n, "", 1);
448 CONFIG_CHUNK(str, size, n, ",", 1);
449 n = one_chan_config_string(out, str, size, error_out);
452 CONFIG_CHUNK(str, size, n, "", 1);
457 int chan_config_string(struct list_head *chans, char *str, int size,
460 struct list_head *ele;
461 struct chan *chan, *in = NULL, *out = NULL;
463 list_for_each(ele, chans){
464 chan = list_entry(ele, struct chan, list);
473 return chan_pair_config_string(in, out, str, size, error_out);
478 const struct chan_ops *ops;
481 static const struct chan_type chan_table[] = {
484 #ifdef CONFIG_NULL_CHAN
485 { "null", &null_ops },
487 { "null", ¬_configged_ops },
490 #ifdef CONFIG_PORT_CHAN
491 { "port", &port_ops },
493 { "port", ¬_configged_ops },
496 #ifdef CONFIG_PTY_CHAN
500 { "pty", ¬_configged_ops },
501 { "pts", ¬_configged_ops },
504 #ifdef CONFIG_TTY_CHAN
507 { "tty", ¬_configged_ops },
510 #ifdef CONFIG_XTERM_CHAN
511 { "xterm", &xterm_ops },
513 { "xterm", ¬_configged_ops },
517 static struct chan *parse_chan(struct line *line, char *str, int device,
518 const struct chan_opts *opts, char **error_out)
520 const struct chan_type *entry;
521 const struct chan_ops *ops;
528 for(i = 0; i < ARRAY_SIZE(chan_table); i++){
529 entry = &chan_table[i];
530 if(!strncmp(str, entry->key, strlen(entry->key))){
532 str += strlen(entry->key);
537 *error_out = "No match for configured backends";
541 data = (*ops->init)(str, device, opts);
543 *error_out = "Configuration failed";
547 chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
549 *error_out = "Memory allocation failed";
552 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
554 LIST_HEAD_INIT(chan->free_list),
567 int parse_chan_pair(char *str, struct line *line, int device,
568 const struct chan_opts *opts, char **error_out)
570 struct list_head *chans = &line->chan_list;
571 struct chan *new, *chan;
574 if(!list_empty(chans)){
575 chan = list_entry(chans->next, struct chan, list);
577 INIT_LIST_HEAD(chans);
580 out = strchr(str, ',');
585 new = parse_chan(line, in, device, opts, error_out);
590 list_add(&new->list, chans);
592 new = parse_chan(line, out, device, opts, error_out);
596 list_add(&new->list, chans);
600 new = parse_chan(line, str, device, opts, error_out);
604 list_add(&new->list, chans);
611 int chan_out_fd(struct list_head *chans)
613 struct list_head *ele;
616 list_for_each(ele, chans){
617 chan = list_entry(ele, struct chan, list);
618 if(chan->primary && chan->output)
624 void chan_interrupt(struct list_head *chans, struct delayed_work *task,
625 struct tty_struct *tty, int irq)
627 struct list_head *ele, *next;
632 list_for_each_safe(ele, next, chans){
633 chan = list_entry(ele, struct chan, list);
634 if(!chan->input || (chan->ops->read == NULL)) continue;
636 if (tty && !tty_buffer_request_room(tty, 1)) {
637 schedule_delayed_work(task, 1);
640 err = chan->ops->read(chan->fd, &c, chan->data);
642 tty_receive_char(tty, c);
645 if(err == 0) reactivate_fd(chan->fd, irq);
650 close_chan(chans, 1);
653 else close_one_chan(chan, 1);
657 if(tty) tty_flip_buffer_push(tty);