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 static LIST_HEAD(irqs_to_free);
231 while(!list_empty(&irqs_to_free)){
232 chan = list_entry(irqs_to_free.next, struct chan, free_list);
233 list_del(&chan->free_list);
236 free_irq(chan->line->driver->read_irq, chan);
238 free_irq(chan->line->driver->write_irq, chan);
243 static void close_one_chan(struct chan *chan, int delay_free_irq)
249 list_add(&chan->free_list, &irqs_to_free);
253 free_irq(chan->line->driver->read_irq, chan);
255 free_irq(chan->line->driver->write_irq, chan);
258 if(chan->ops->close != NULL)
259 (*chan->ops->close)(chan->fd, chan->data);
265 void close_chan(struct list_head *chans, int delay_free_irq)
269 /* Close in reverse order as open in case more than one of them
270 * refers to the same device and they save and restore that device's
271 * state. Then, the first one opened will have the original state,
272 * so it must be the last closed.
274 list_for_each_entry_reverse(chan, chans, list) {
275 close_one_chan(chan, delay_free_irq);
279 void deactivate_chan(struct list_head *chans, int irq)
281 struct list_head *ele;
284 list_for_each(ele, chans) {
285 chan = list_entry(ele, struct chan, list);
287 if(chan->enabled && chan->input)
288 deactivate_fd(chan->fd, irq);
292 void reactivate_chan(struct list_head *chans, int irq)
294 struct list_head *ele;
297 list_for_each(ele, chans) {
298 chan = list_entry(ele, struct chan, list);
300 if(chan->enabled && chan->input)
301 reactivate_fd(chan->fd, irq);
305 int write_chan(struct list_head *chans, const char *buf, int len,
308 struct list_head *ele;
309 struct chan *chan = NULL;
312 list_for_each(ele, chans) {
313 chan = list_entry(ele, struct chan, list);
314 if (!chan->output || (chan->ops->write == NULL))
316 n = chan->ops->write(chan->fd, buf, len, chan->data);
319 if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
320 reactivate_fd(chan->fd, write_irq);
326 int console_write_chan(struct list_head *chans, const char *buf, int len)
328 struct list_head *ele;
332 list_for_each(ele, chans){
333 chan = list_entry(ele, struct chan, list);
334 if(!chan->output || (chan->ops->console_write == NULL))
336 n = chan->ops->console_write(chan->fd, buf, len);
337 if(chan->primary) ret = n;
342 int console_open_chan(struct line *line, struct console *co,
343 const struct chan_opts *opts)
347 err = open_chan(&line->chan_list);
351 printk("Console initialized on /dev/%s%d\n",co->name,co->index);
355 int chan_window_size(struct list_head *chans, unsigned short *rows_out,
356 unsigned short *cols_out)
358 struct list_head *ele;
361 list_for_each(ele, chans){
362 chan = list_entry(ele, struct chan, list);
364 if(chan->ops->window_size == NULL)
366 return chan->ops->window_size(chan->fd, chan->data,
373 static void free_one_chan(struct chan *chan, int delay_free_irq)
375 list_del(&chan->list);
377 close_one_chan(chan, delay_free_irq);
379 if(chan->ops->free != NULL)
380 (*chan->ops->free)(chan->data);
382 if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
386 static void free_chan(struct list_head *chans, int delay_free_irq)
388 struct list_head *ele, *next;
391 list_for_each_safe(ele, next, chans){
392 chan = list_entry(ele, struct chan, list);
393 free_one_chan(chan, delay_free_irq);
397 static int one_chan_config_string(struct chan *chan, char *str, int size,
403 CONFIG_CHUNK(str, size, n, "none", 1);
407 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
409 if(chan->dev == NULL){
410 CONFIG_CHUNK(str, size, n, "", 1);
414 CONFIG_CHUNK(str, size, n, ":", 0);
415 CONFIG_CHUNK(str, size, n, chan->dev, 0);
420 static int chan_pair_config_string(struct chan *in, struct chan *out,
421 char *str, int size, char **error_out)
425 n = one_chan_config_string(in, str, size, error_out);
430 CONFIG_CHUNK(str, size, n, "", 1);
434 CONFIG_CHUNK(str, size, n, ",", 1);
435 n = one_chan_config_string(out, str, size, error_out);
438 CONFIG_CHUNK(str, size, n, "", 1);
443 int chan_config_string(struct list_head *chans, char *str, int size,
446 struct list_head *ele;
447 struct chan *chan, *in = NULL, *out = NULL;
449 list_for_each(ele, chans){
450 chan = list_entry(ele, struct chan, list);
459 return chan_pair_config_string(in, out, str, size, error_out);
464 const struct chan_ops *ops;
467 static const struct chan_type chan_table[] = {
470 #ifdef CONFIG_NULL_CHAN
471 { "null", &null_ops },
473 { "null", ¬_configged_ops },
476 #ifdef CONFIG_PORT_CHAN
477 { "port", &port_ops },
479 { "port", ¬_configged_ops },
482 #ifdef CONFIG_PTY_CHAN
486 { "pty", ¬_configged_ops },
487 { "pts", ¬_configged_ops },
490 #ifdef CONFIG_TTY_CHAN
493 { "tty", ¬_configged_ops },
496 #ifdef CONFIG_XTERM_CHAN
497 { "xterm", &xterm_ops },
499 { "xterm", ¬_configged_ops },
503 static struct chan *parse_chan(struct line *line, char *str, int device,
504 const struct chan_opts *opts, char **error_out)
506 const struct chan_type *entry;
507 const struct chan_ops *ops;
514 for(i = 0; i < ARRAY_SIZE(chan_table); i++){
515 entry = &chan_table[i];
516 if(!strncmp(str, entry->key, strlen(entry->key))){
518 str += strlen(entry->key);
523 *error_out = "No match for configured backends";
527 data = (*ops->init)(str, device, opts);
529 *error_out = "Configuration failed";
533 chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
535 *error_out = "Memory allocation failed";
538 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
540 LIST_HEAD_INIT(chan->free_list),
553 int parse_chan_pair(char *str, struct line *line, int device,
554 const struct chan_opts *opts, char **error_out)
556 struct list_head *chans = &line->chan_list;
557 struct chan *new, *chan;
560 if(!list_empty(chans)){
561 chan = list_entry(chans->next, struct chan, list);
563 INIT_LIST_HEAD(chans);
566 out = strchr(str, ',');
571 new = parse_chan(line, in, device, opts, error_out);
576 list_add(&new->list, chans);
578 new = parse_chan(line, out, device, opts, error_out);
582 list_add(&new->list, chans);
586 new = parse_chan(line, str, device, opts, error_out);
590 list_add(&new->list, chans);
597 int chan_out_fd(struct list_head *chans)
599 struct list_head *ele;
602 list_for_each(ele, chans){
603 chan = list_entry(ele, struct chan, list);
604 if(chan->primary && chan->output)
610 void chan_interrupt(struct list_head *chans, struct delayed_work *task,
611 struct tty_struct *tty, int irq)
613 struct list_head *ele, *next;
618 list_for_each_safe(ele, next, chans){
619 chan = list_entry(ele, struct chan, list);
620 if(!chan->input || (chan->ops->read == NULL)) continue;
622 if (tty && !tty_buffer_request_room(tty, 1)) {
623 schedule_delayed_work(task, 1);
626 err = chan->ops->read(chan->fd, &c, chan->data);
628 tty_receive_char(tty, c);
631 if(err == 0) reactivate_fd(chan->fd, irq);
636 close_chan(chans, 1);
639 else close_one_chan(chan, 1);
643 if(tty) tty_flip_buffer_push(tty);