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 const 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 tty_insert_flip_char(tty, ch, TTY_NORMAL);
192 static int open_one_chan(struct chan *chan)
199 if(chan->ops->open == NULL)
201 else fd = (*chan->ops->open)(chan->input, chan->output, chan->primary,
202 chan->data, &chan->dev);
211 int open_chan(struct list_head *chans)
213 struct list_head *ele;
217 list_for_each(ele, chans){
218 chan = list_entry(ele, struct chan, list);
219 ret = open_one_chan(chan);
226 void chan_enable_winch(struct list_head *chans, struct tty_struct *tty)
228 struct list_head *ele;
231 list_for_each(ele, chans){
232 chan = list_entry(ele, struct chan, list);
233 if(chan->primary && chan->output && chan->ops->winch){
234 register_winch(chan->fd, tty);
240 void enable_chan(struct line *line)
242 struct list_head *ele;
245 list_for_each(ele, &line->chan_list){
246 chan = list_entry(ele, struct chan, list);
247 if(open_one_chan(chan))
252 line_setup_irq(chan->fd, chan->input, chan->output, line,
258 static LIST_HEAD(irqs_to_free);
264 while(!list_empty(&irqs_to_free)){
265 chan = list_entry(irqs_to_free.next, struct chan, free_list);
266 list_del(&chan->free_list);
269 free_irq(chan->line->driver->read_irq, chan);
271 free_irq(chan->line->driver->write_irq, chan);
276 static void close_one_chan(struct chan *chan, int delay_free_irq)
282 list_add(&chan->free_list, &irqs_to_free);
286 free_irq(chan->line->driver->read_irq, chan);
288 free_irq(chan->line->driver->write_irq, chan);
291 if(chan->ops->close != NULL)
292 (*chan->ops->close)(chan->fd, chan->data);
298 void close_chan(struct list_head *chans, int delay_free_irq)
302 /* Close in reverse order as open in case more than one of them
303 * refers to the same device and they save and restore that device's
304 * state. Then, the first one opened will have the original state,
305 * so it must be the last closed.
307 list_for_each_entry_reverse(chan, chans, list) {
308 close_one_chan(chan, delay_free_irq);
312 void deactivate_chan(struct list_head *chans, int irq)
314 struct list_head *ele;
317 list_for_each(ele, chans) {
318 chan = list_entry(ele, struct chan, list);
320 if(chan->enabled && chan->input)
321 deactivate_fd(chan->fd, irq);
325 void reactivate_chan(struct list_head *chans, int irq)
327 struct list_head *ele;
330 list_for_each(ele, chans) {
331 chan = list_entry(ele, struct chan, list);
333 if(chan->enabled && chan->input)
334 reactivate_fd(chan->fd, irq);
338 int write_chan(struct list_head *chans, const char *buf, int len,
341 struct list_head *ele;
342 struct chan *chan = NULL;
345 list_for_each(ele, chans) {
346 chan = list_entry(ele, struct chan, list);
347 if (!chan->output || (chan->ops->write == NULL))
349 n = chan->ops->write(chan->fd, buf, len, chan->data);
352 if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
353 reactivate_fd(chan->fd, write_irq);
359 int console_write_chan(struct list_head *chans, const char *buf, int len)
361 struct list_head *ele;
365 list_for_each(ele, chans){
366 chan = list_entry(ele, struct chan, list);
367 if(!chan->output || (chan->ops->console_write == NULL))
369 n = chan->ops->console_write(chan->fd, buf, len);
370 if(chan->primary) ret = n;
375 int console_open_chan(struct line *line, struct console *co,
376 const struct chan_opts *opts)
380 err = open_chan(&line->chan_list);
384 printk("Console initialized on /dev/%s%d\n",co->name,co->index);
388 int chan_window_size(struct list_head *chans, unsigned short *rows_out,
389 unsigned short *cols_out)
391 struct list_head *ele;
394 list_for_each(ele, chans){
395 chan = list_entry(ele, struct chan, list);
397 if(chan->ops->window_size == NULL)
399 return chan->ops->window_size(chan->fd, chan->data,
406 static void free_one_chan(struct chan *chan, int delay_free_irq)
408 list_del(&chan->list);
410 close_one_chan(chan, delay_free_irq);
412 if(chan->ops->free != NULL)
413 (*chan->ops->free)(chan->data);
415 if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
419 static void free_chan(struct list_head *chans, int delay_free_irq)
421 struct list_head *ele, *next;
424 list_for_each_safe(ele, next, chans){
425 chan = list_entry(ele, struct chan, list);
426 free_one_chan(chan, delay_free_irq);
430 static int one_chan_config_string(struct chan *chan, char *str, int size,
436 CONFIG_CHUNK(str, size, n, "none", 1);
440 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
442 if(chan->dev == NULL){
443 CONFIG_CHUNK(str, size, n, "", 1);
447 CONFIG_CHUNK(str, size, n, ":", 0);
448 CONFIG_CHUNK(str, size, n, chan->dev, 0);
453 static int chan_pair_config_string(struct chan *in, struct chan *out,
454 char *str, int size, char **error_out)
458 n = one_chan_config_string(in, str, size, error_out);
463 CONFIG_CHUNK(str, size, n, "", 1);
467 CONFIG_CHUNK(str, size, n, ",", 1);
468 n = one_chan_config_string(out, str, size, error_out);
471 CONFIG_CHUNK(str, size, n, "", 1);
476 int chan_config_string(struct list_head *chans, char *str, int size,
479 struct list_head *ele;
480 struct chan *chan, *in = NULL, *out = NULL;
482 list_for_each(ele, chans){
483 chan = list_entry(ele, struct chan, list);
492 return chan_pair_config_string(in, out, str, size, error_out);
497 const struct chan_ops *ops;
500 static const struct chan_type chan_table[] = {
503 #ifdef CONFIG_NULL_CHAN
504 { "null", &null_ops },
506 { "null", ¬_configged_ops },
509 #ifdef CONFIG_PORT_CHAN
510 { "port", &port_ops },
512 { "port", ¬_configged_ops },
515 #ifdef CONFIG_PTY_CHAN
519 { "pty", ¬_configged_ops },
520 { "pts", ¬_configged_ops },
523 #ifdef CONFIG_TTY_CHAN
526 { "tty", ¬_configged_ops },
529 #ifdef CONFIG_XTERM_CHAN
530 { "xterm", &xterm_ops },
532 { "xterm", ¬_configged_ops },
536 static struct chan *parse_chan(struct line *line, char *str, int device,
537 const struct chan_opts *opts)
539 const struct chan_type *entry;
540 const struct chan_ops *ops;
547 for(i = 0; i < ARRAY_SIZE(chan_table); i++){
548 entry = &chan_table[i];
549 if(!strncmp(str, entry->key, strlen(entry->key))){
551 str += strlen(entry->key);
556 my_printf("parse_chan couldn't parse \"%s\"\n",
560 if(ops->init == NULL)
562 data = (*ops->init)(str, device, opts);
566 chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
569 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
571 LIST_HEAD_INIT(chan->free_list),
584 int parse_chan_pair(char *str, struct line *line, int device,
585 const struct chan_opts *opts)
587 struct list_head *chans = &line->chan_list;
588 struct chan *new, *chan;
591 if(!list_empty(chans)){
592 chan = list_entry(chans->next, struct chan, list);
594 INIT_LIST_HEAD(chans);
597 out = strchr(str, ',');
602 new = parse_chan(line, in, device, opts);
607 list_add(&new->list, chans);
609 new = parse_chan(line, out, device, opts);
613 list_add(&new->list, chans);
617 new = parse_chan(line, str, device, opts);
621 list_add(&new->list, chans);
628 int chan_out_fd(struct list_head *chans)
630 struct list_head *ele;
633 list_for_each(ele, chans){
634 chan = list_entry(ele, struct chan, list);
635 if(chan->primary && chan->output)
641 void chan_interrupt(struct list_head *chans, struct work_struct *task,
642 struct tty_struct *tty, int irq)
644 struct list_head *ele, *next;
649 list_for_each_safe(ele, next, chans){
650 chan = list_entry(ele, struct chan, list);
651 if(!chan->input || (chan->ops->read == NULL)) continue;
653 if (tty && !tty_buffer_request_room(tty, 1)) {
654 schedule_delayed_work(task, 1);
657 err = chan->ops->read(chan->fd, &c, chan->data);
659 tty_receive_char(tty, c);
662 if(err == 0) reactivate_fd(chan->fd, irq);
667 close_chan(chans, 1);
670 else close_one_chan(chan, 1);
674 if(tty) tty_flip_buffer_push(tty);