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,
95 my_puts("Using a channel type which is configured out of "
100 static int not_configged_window_size(int fd, void *data, unsigned short *rows,
101 unsigned short *cols)
103 my_puts("Using a channel type which is configured out of "
108 static void not_configged_free(void *data)
110 my_puts("Using a channel type which is configured out of "
114 static struct chan_ops not_configged_ops = {
115 .init = not_configged_init,
116 .open = not_configged_open,
117 .close = not_configged_close,
118 .read = not_configged_read,
119 .write = not_configged_write,
120 .console_write = not_configged_console_write,
121 .window_size = not_configged_window_size,
122 .free = not_configged_free,
125 #endif /* CONFIG_NOCONFIG_CHAN */
127 void generic_close(int fd, void *unused)
132 int generic_read(int fd, char *c_out, void *unused)
136 n = os_read_file(fd, c_out, sizeof(*c_out));
145 /* XXX Trivial wrapper around os_write_file */
147 int generic_write(int fd, const char *buf, int n, void *unused)
149 return(os_write_file(fd, buf, n));
152 int generic_window_size(int fd, void *unused, unsigned short *rows_out,
153 unsigned short *cols_out)
158 ret = os_window_size(fd, &rows, &cols);
162 ret = ((*rows_out != rows) || (*cols_out != cols));
170 void generic_free(void *data)
175 static void tty_receive_char(struct tty_struct *tty, char ch)
177 if(tty == NULL) return;
179 if(I_IXON(tty) && !I_IXOFF(tty) && !tty->raw) {
180 if(ch == STOP_CHAR(tty)){
184 else if(ch == START_CHAR(tty)){
190 if((tty->flip.flag_buf_ptr == NULL) ||
191 (tty->flip.char_buf_ptr == NULL))
193 tty_insert_flip_char(tty, ch, TTY_NORMAL);
196 static int open_one_chan(struct chan *chan, int input, int output, int primary)
200 if(chan->opened) return(0);
201 if(chan->ops->open == NULL) fd = 0;
202 else fd = (*chan->ops->open)(input, output, primary, chan->data,
204 if(fd < 0) return(fd);
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, chan->input, chan->output,
221 if(chan->primary) err = ret;
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 list_head *chans, struct tty_struct *tty)
242 struct list_head *ele;
245 list_for_each(ele, chans){
246 chan = list_entry(ele, struct chan, list);
247 if(!chan->opened) continue;
249 line_setup_irq(chan->fd, chan->input, chan->output, tty);
253 void close_chan(struct list_head *chans)
257 /* Close in reverse order as open in case more than one of them
258 * refers to the same device and they save and restore that device's
259 * state. Then, the first one opened will have the original state,
260 * so it must be the last closed.
262 list_for_each_entry_reverse(chan, chans, list) {
263 if(!chan->opened) continue;
264 if(chan->ops->close != NULL)
265 (*chan->ops->close)(chan->fd, chan->data);
271 int write_chan(struct list_head *chans, const char *buf, int len,
274 struct list_head *ele;
275 struct chan *chan = NULL;
278 list_for_each(ele, chans) {
279 chan = list_entry(ele, struct chan, list);
280 if (!chan->output || (chan->ops->write == NULL))
282 n = chan->ops->write(chan->fd, buf, len, chan->data);
285 if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
286 reactivate_fd(chan->fd, write_irq);
292 int console_write_chan(struct list_head *chans, const char *buf, int len)
294 struct list_head *ele;
298 list_for_each(ele, chans){
299 chan = list_entry(ele, struct chan, list);
300 if(!chan->output || (chan->ops->console_write == NULL))
302 n = chan->ops->console_write(chan->fd, buf, len, chan->data);
303 if(chan->primary) ret = n;
308 int console_open_chan(struct line *line, struct console *co, struct chan_opts *opts)
310 if (!list_empty(&line->chan_list))
313 if (0 != parse_chan_pair(line->init_str, &line->chan_list,
314 line->init_pri, co->index, opts))
316 if (0 != open_chan(&line->chan_list))
318 printk("Console initialized on /dev/%s%d\n",co->name,co->index);
322 int chan_window_size(struct list_head *chans, unsigned short *rows_out,
323 unsigned short *cols_out)
325 struct list_head *ele;
328 list_for_each(ele, chans){
329 chan = list_entry(ele, struct chan, list);
331 if(chan->ops->window_size == NULL) return(0);
332 return(chan->ops->window_size(chan->fd, chan->data,
333 rows_out, cols_out));
339 void free_one_chan(struct chan *chan)
341 list_del(&chan->list);
342 if(chan->ops->free != NULL)
343 (*chan->ops->free)(chan->data);
344 free_irq_by_fd(chan->fd);
345 if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
349 void free_chan(struct list_head *chans)
351 struct list_head *ele, *next;
354 list_for_each_safe(ele, next, chans){
355 chan = list_entry(ele, struct chan, list);
360 static int one_chan_config_string(struct chan *chan, char *str, int size,
366 CONFIG_CHUNK(str, size, n, "none", 1);
370 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
372 if(chan->dev == NULL){
373 CONFIG_CHUNK(str, size, n, "", 1);
377 CONFIG_CHUNK(str, size, n, ":", 0);
378 CONFIG_CHUNK(str, size, n, chan->dev, 0);
383 static int chan_pair_config_string(struct chan *in, struct chan *out,
384 char *str, int size, char **error_out)
388 n = one_chan_config_string(in, str, size, error_out);
393 CONFIG_CHUNK(str, size, n, "", 1);
397 CONFIG_CHUNK(str, size, n, ",", 1);
398 n = one_chan_config_string(out, str, size, error_out);
401 CONFIG_CHUNK(str, size, n, "", 1);
406 int chan_config_string(struct list_head *chans, char *str, int size,
409 struct list_head *ele;
410 struct chan *chan, *in = NULL, *out = NULL;
412 list_for_each(ele, chans){
413 chan = list_entry(ele, struct chan, list);
422 return(chan_pair_config_string(in, out, str, size, error_out));
427 struct chan_ops *ops;
430 struct chan_type chan_table[] = {
433 #ifdef CONFIG_NULL_CHAN
434 { "null", &null_ops },
436 { "null", ¬_configged_ops },
439 #ifdef CONFIG_PORT_CHAN
440 { "port", &port_ops },
442 { "port", ¬_configged_ops },
445 #ifdef CONFIG_PTY_CHAN
449 { "pty", ¬_configged_ops },
450 { "pts", ¬_configged_ops },
453 #ifdef CONFIG_TTY_CHAN
456 { "tty", ¬_configged_ops },
459 #ifdef CONFIG_XTERM_CHAN
460 { "xterm", &xterm_ops },
462 { "xterm", ¬_configged_ops },
466 static struct chan *parse_chan(char *str, int pri, int device,
467 struct chan_opts *opts)
469 struct chan_type *entry;
470 struct chan_ops *ops;
477 for(i = 0; i < sizeof(chan_table)/sizeof(chan_table[0]); i++){
478 entry = &chan_table[i];
479 if(!strncmp(str, entry->key, strlen(entry->key))){
481 str += strlen(entry->key);
486 my_printf("parse_chan couldn't parse \"%s\"\n",
490 if(ops->init == NULL) return(NULL);
491 data = (*ops->init)(str, device, opts);
492 if(data == NULL) return(NULL);
494 chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
495 if(chan == NULL) return(NULL);
496 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
508 int parse_chan_pair(char *str, struct list_head *chans, int pri, int device,
509 struct chan_opts *opts)
511 struct chan *new, *chan;
514 if(!list_empty(chans)){
515 chan = list_entry(chans->next, struct chan, list);
516 if(chan->pri >= pri) return(0);
518 INIT_LIST_HEAD(chans);
521 out = strchr(str, ',');
526 new = parse_chan(in, pri, device, opts);
527 if(new == NULL) return(-1);
529 list_add(&new->list, chans);
531 new = parse_chan(out, pri, device, opts);
532 if(new == NULL) return(-1);
533 list_add(&new->list, chans);
537 new = parse_chan(str, pri, device, opts);
538 if(new == NULL) return(-1);
539 list_add(&new->list, chans);
546 int chan_out_fd(struct list_head *chans)
548 struct list_head *ele;
551 list_for_each(ele, chans){
552 chan = list_entry(ele, struct chan, list);
553 if(chan->primary && chan->output)
559 void chan_interrupt(struct list_head *chans, struct work_struct *task,
560 struct tty_struct *tty, int irq)
562 struct list_head *ele, *next;
567 list_for_each_safe(ele, next, chans){
568 chan = list_entry(ele, struct chan, list);
569 if(!chan->input || (chan->ops->read == NULL)) continue;
572 (tty->flip.count >= TTY_FLIPBUF_SIZE)){
576 err = chan->ops->read(chan->fd, &c, chan->data);
578 tty_receive_char(tty, c);
581 if(err == 0) reactivate_fd(chan->fd, irq);
586 line_disable(tty, irq);
592 if(chan->ops->close != NULL)
593 chan->ops->close(chan->fd, chan->data);
599 if(tty) tty_flip_buffer_push(tty);
603 * Overrides for Emacs so that we follow Linus's tabbing style.
604 * Emacs will notice this stuff at the end of the file and automatically
605 * adjust the settings for this buffer only. This must remain at the end
607 * ---------------------------------------------------------------------------
609 * c-file-style: "linux"