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 /* XXX: could well be moved to somewhere else, if needed. */
23 static int my_printf(const char * fmt, ...)
24 __attribute__ ((format (printf, 1, 2)));
26 static int my_printf(const char * fmt, ...)
28 /* Yes, can be called on atomic context.*/
29 char *buf = kmalloc(4096, GFP_ATOMIC);
34 /* We print directly fmt.
35 * Yes, yes, yes, feel free to complain. */
39 r = vsprintf(buf, fmt, args);
45 r = os_write_file(1, fmt, r);
50 #ifdef CONFIG_NOCONFIG_CHAN
51 /* Despite its name, there's no added trailing newline. */
52 static int my_puts(const char * buf)
54 return os_write_file(1, buf, strlen(buf));
57 static void *not_configged_init(char *str, int device, struct chan_opts *opts)
59 my_puts("Using a channel type which is configured out of "
64 static int not_configged_open(int input, int output, int primary, void *data,
67 my_puts("Using a channel type which is configured out of "
72 static void not_configged_close(int fd, void *data)
74 my_puts("Using a channel type which is configured out of "
78 static int not_configged_read(int fd, char *c_out, void *data)
80 my_puts("Using a channel type which is configured out of "
85 static int not_configged_write(int fd, const char *buf, int len, void *data)
87 my_puts("Using a channel type which is configured out of "
92 static int not_configged_console_write(int fd, const char *buf, int len)
94 my_puts("Using a channel type which is configured out of "
99 static int not_configged_window_size(int fd, void *data, unsigned short *rows,
100 unsigned short *cols)
102 my_puts("Using a channel type which is configured out of "
107 static void not_configged_free(void *data)
109 my_puts("Using a channel type which is configured out of "
113 static struct chan_ops not_configged_ops = {
114 .init = not_configged_init,
115 .open = not_configged_open,
116 .close = not_configged_close,
117 .read = not_configged_read,
118 .write = not_configged_write,
119 .console_write = not_configged_console_write,
120 .window_size = not_configged_window_size,
121 .free = not_configged_free,
124 #endif /* CONFIG_NOCONFIG_CHAN */
126 void generic_close(int fd, void *unused)
131 int generic_read(int fd, char *c_out, void *unused)
135 n = os_read_file(fd, c_out, sizeof(*c_out));
144 /* XXX Trivial wrapper around os_write_file */
146 int generic_write(int fd, const char *buf, int n, void *unused)
148 return os_write_file(fd, buf, n);
151 int generic_window_size(int fd, void *unused, unsigned short *rows_out,
152 unsigned short *cols_out)
157 ret = os_window_size(fd, &rows, &cols);
161 ret = ((*rows_out != rows) || (*cols_out != cols));
169 void generic_free(void *data)
174 static void tty_receive_char(struct tty_struct *tty, char ch)
176 if(tty == NULL) return;
178 if(I_IXON(tty) && !I_IXOFF(tty) && !tty->raw) {
179 if(ch == STOP_CHAR(tty)){
183 else if(ch == START_CHAR(tty)){
189 if((tty->flip.flag_buf_ptr == NULL) ||
190 (tty->flip.char_buf_ptr == NULL))
192 tty_insert_flip_char(tty, ch, TTY_NORMAL);
195 static int open_one_chan(struct chan *chan)
202 if(chan->ops->open == NULL)
204 else fd = (*chan->ops->open)(chan->input, chan->output, chan->primary,
205 chan->data, &chan->dev);
214 int open_chan(struct list_head *chans)
216 struct list_head *ele;
220 list_for_each(ele, chans){
221 chan = list_entry(ele, struct chan, list);
222 ret = open_one_chan(chan);
229 void chan_enable_winch(struct list_head *chans, struct tty_struct *tty)
231 struct list_head *ele;
234 list_for_each(ele, chans){
235 chan = list_entry(ele, struct chan, list);
236 if(chan->primary && chan->output && chan->ops->winch){
237 register_winch(chan->fd, tty);
243 void enable_chan(struct line *line)
245 struct list_head *ele;
248 list_for_each(ele, &line->chan_list){
249 chan = list_entry(ele, struct chan, list);
250 if(open_one_chan(chan))
255 line_setup_irq(chan->fd, chan->input, chan->output, line,
261 static LIST_HEAD(irqs_to_free);
267 while(!list_empty(&irqs_to_free)){
268 chan = list_entry(irqs_to_free.next, struct chan, free_list);
269 list_del(&chan->free_list);
272 free_irq(chan->line->driver->read_irq, chan);
274 free_irq(chan->line->driver->write_irq, chan);
279 static void close_one_chan(struct chan *chan, int delay_free_irq)
285 list_add(&chan->free_list, &irqs_to_free);
289 free_irq(chan->line->driver->read_irq, chan);
291 free_irq(chan->line->driver->write_irq, chan);
294 if(chan->ops->close != NULL)
295 (*chan->ops->close)(chan->fd, chan->data);
301 void close_chan(struct list_head *chans, int delay_free_irq)
305 /* Close in reverse order as open in case more than one of them
306 * refers to the same device and they save and restore that device's
307 * state. Then, the first one opened will have the original state,
308 * so it must be the last closed.
310 list_for_each_entry_reverse(chan, chans, list) {
311 close_one_chan(chan, delay_free_irq);
315 void deactivate_chan(struct list_head *chans, int irq)
317 struct list_head *ele;
320 list_for_each(ele, chans) {
321 chan = list_entry(ele, struct chan, list);
323 if(chan->enabled && chan->input)
324 deactivate_fd(chan->fd, irq);
328 void reactivate_chan(struct list_head *chans, int irq)
330 struct list_head *ele;
333 list_for_each(ele, chans) {
334 chan = list_entry(ele, struct chan, list);
336 if(chan->enabled && chan->input)
337 reactivate_fd(chan->fd, irq);
341 int write_chan(struct list_head *chans, const char *buf, int len,
344 struct list_head *ele;
345 struct chan *chan = NULL;
348 list_for_each(ele, chans) {
349 chan = list_entry(ele, struct chan, list);
350 if (!chan->output || (chan->ops->write == NULL))
352 n = chan->ops->write(chan->fd, buf, len, chan->data);
355 if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
356 reactivate_fd(chan->fd, write_irq);
362 int console_write_chan(struct list_head *chans, const char *buf, int len)
364 struct list_head *ele;
368 list_for_each(ele, chans){
369 chan = list_entry(ele, struct chan, list);
370 if(!chan->output || (chan->ops->console_write == NULL))
372 n = chan->ops->console_write(chan->fd, buf, len);
373 if(chan->primary) ret = n;
378 int console_open_chan(struct line *line, struct console *co,
379 struct chan_opts *opts)
383 err = open_chan(&line->chan_list);
387 printk("Console initialized on /dev/%s%d\n",co->name,co->index);
391 int chan_window_size(struct list_head *chans, unsigned short *rows_out,
392 unsigned short *cols_out)
394 struct list_head *ele;
397 list_for_each(ele, chans){
398 chan = list_entry(ele, struct chan, list);
400 if(chan->ops->window_size == NULL)
402 return chan->ops->window_size(chan->fd, chan->data,
409 void free_one_chan(struct chan *chan, int delay_free_irq)
411 list_del(&chan->list);
413 close_one_chan(chan, delay_free_irq);
415 if(chan->ops->free != NULL)
416 (*chan->ops->free)(chan->data);
418 if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
422 void free_chan(struct list_head *chans, int delay_free_irq)
424 struct list_head *ele, *next;
427 list_for_each_safe(ele, next, chans){
428 chan = list_entry(ele, struct chan, list);
429 free_one_chan(chan, delay_free_irq);
433 static int one_chan_config_string(struct chan *chan, char *str, int size,
439 CONFIG_CHUNK(str, size, n, "none", 1);
443 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
445 if(chan->dev == NULL){
446 CONFIG_CHUNK(str, size, n, "", 1);
450 CONFIG_CHUNK(str, size, n, ":", 0);
451 CONFIG_CHUNK(str, size, n, chan->dev, 0);
456 static int chan_pair_config_string(struct chan *in, struct chan *out,
457 char *str, int size, char **error_out)
461 n = one_chan_config_string(in, str, size, error_out);
466 CONFIG_CHUNK(str, size, n, "", 1);
470 CONFIG_CHUNK(str, size, n, ",", 1);
471 n = one_chan_config_string(out, str, size, error_out);
474 CONFIG_CHUNK(str, size, n, "", 1);
479 int chan_config_string(struct list_head *chans, char *str, int size,
482 struct list_head *ele;
483 struct chan *chan, *in = NULL, *out = NULL;
485 list_for_each(ele, chans){
486 chan = list_entry(ele, struct chan, list);
495 return chan_pair_config_string(in, out, str, size, error_out);
500 struct chan_ops *ops;
503 struct chan_type chan_table[] = {
506 #ifdef CONFIG_NULL_CHAN
507 { "null", &null_ops },
509 { "null", ¬_configged_ops },
512 #ifdef CONFIG_PORT_CHAN
513 { "port", &port_ops },
515 { "port", ¬_configged_ops },
518 #ifdef CONFIG_PTY_CHAN
522 { "pty", ¬_configged_ops },
523 { "pts", ¬_configged_ops },
526 #ifdef CONFIG_TTY_CHAN
529 { "tty", ¬_configged_ops },
532 #ifdef CONFIG_XTERM_CHAN
533 { "xterm", &xterm_ops },
535 { "xterm", ¬_configged_ops },
539 static struct chan *parse_chan(struct line *line, char *str, int device,
540 struct chan_opts *opts)
542 struct chan_type *entry;
543 struct chan_ops *ops;
550 for(i = 0; i < sizeof(chan_table)/sizeof(chan_table[0]); i++){
551 entry = &chan_table[i];
552 if(!strncmp(str, entry->key, strlen(entry->key))){
554 str += strlen(entry->key);
559 my_printf("parse_chan couldn't parse \"%s\"\n",
563 if(ops->init == NULL)
565 data = (*ops->init)(str, device, opts);
569 chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
572 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
574 LIST_HEAD_INIT(chan->free_list),
587 int parse_chan_pair(char *str, struct line *line, int device,
588 struct chan_opts *opts)
590 struct list_head *chans = &line->chan_list;
591 struct chan *new, *chan;
594 if(!list_empty(chans)){
595 chan = list_entry(chans->next, struct chan, list);
597 INIT_LIST_HEAD(chans);
600 out = strchr(str, ',');
605 new = parse_chan(line, in, device, opts);
610 list_add(&new->list, chans);
612 new = parse_chan(line, out, device, opts);
616 list_add(&new->list, chans);
620 new = parse_chan(line, str, device, opts);
624 list_add(&new->list, chans);
631 int chan_out_fd(struct list_head *chans)
633 struct list_head *ele;
636 list_for_each(ele, chans){
637 chan = list_entry(ele, struct chan, list);
638 if(chan->primary && chan->output)
644 void chan_interrupt(struct list_head *chans, struct work_struct *task,
645 struct tty_struct *tty, int irq)
647 struct list_head *ele, *next;
652 list_for_each_safe(ele, next, chans){
653 chan = list_entry(ele, struct chan, list);
654 if(!chan->input || (chan->ops->read == NULL)) continue;
657 (tty->flip.count >= TTY_FLIPBUF_SIZE)){
658 schedule_delayed_work(task, 1);
661 err = chan->ops->read(chan->fd, &c, chan->data);
663 tty_receive_char(tty, c);
666 if(err == 0) reactivate_fd(chan->fd, irq);
671 close_chan(chans, 1);
674 else close_one_chan(chan, 1);
678 if(tty) tty_flip_buffer_push(tty);