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, int input, int output, int primary)
199 if(chan->opened) return(0);
200 if(chan->ops->open == NULL) fd = 0;
201 else fd = (*chan->ops->open)(input, output, primary, chan->data,
203 if(fd < 0) return(fd);
210 int open_chan(struct list_head *chans)
212 struct list_head *ele;
216 list_for_each(ele, chans){
217 chan = list_entry(ele, struct chan, list);
218 ret = open_one_chan(chan, chan->input, chan->output,
220 if(chan->primary) err = ret;
225 void chan_enable_winch(struct list_head *chans, struct tty_struct *tty)
227 struct list_head *ele;
230 list_for_each(ele, chans){
231 chan = list_entry(ele, struct chan, list);
232 if(chan->primary && chan->output && chan->ops->winch){
233 register_winch(chan->fd, tty);
239 void enable_chan(struct list_head *chans, struct tty_struct *tty)
241 struct list_head *ele;
244 list_for_each(ele, chans){
245 chan = list_entry(ele, struct chan, list);
246 if(!chan->opened) continue;
248 line_setup_irq(chan->fd, chan->input, chan->output, tty);
252 void close_chan(struct list_head *chans)
256 /* Close in reverse order as open in case more than one of them
257 * refers to the same device and they save and restore that device's
258 * state. Then, the first one opened will have the original state,
259 * so it must be the last closed.
261 list_for_each_entry_reverse(chan, chans, list) {
262 if(!chan->opened) continue;
263 if(chan->ops->close != NULL)
264 (*chan->ops->close)(chan->fd, chan->data);
270 int write_chan(struct list_head *chans, const char *buf, int len,
273 struct list_head *ele;
274 struct chan *chan = NULL;
277 list_for_each(ele, chans) {
278 chan = list_entry(ele, struct chan, list);
279 if (!chan->output || (chan->ops->write == NULL))
281 n = chan->ops->write(chan->fd, buf, len, chan->data);
284 if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
285 reactivate_fd(chan->fd, write_irq);
291 int console_write_chan(struct list_head *chans, const char *buf, int len)
293 struct list_head *ele;
297 list_for_each(ele, chans){
298 chan = list_entry(ele, struct chan, list);
299 if(!chan->output || (chan->ops->console_write == NULL))
301 n = chan->ops->console_write(chan->fd, buf, len);
302 if(chan->primary) ret = n;
307 int console_open_chan(struct line *line, struct console *co, struct chan_opts *opts)
309 if (!list_empty(&line->chan_list))
312 if (0 != parse_chan_pair(line->init_str, &line->chan_list,
313 line->init_pri, co->index, opts))
315 if (0 != open_chan(&line->chan_list))
317 printk("Console initialized on /dev/%s%d\n",co->name,co->index);
321 int chan_window_size(struct list_head *chans, unsigned short *rows_out,
322 unsigned short *cols_out)
324 struct list_head *ele;
327 list_for_each(ele, chans){
328 chan = list_entry(ele, struct chan, list);
330 if(chan->ops->window_size == NULL) return(0);
331 return(chan->ops->window_size(chan->fd, chan->data,
332 rows_out, cols_out));
338 void free_one_chan(struct chan *chan)
340 list_del(&chan->list);
341 if(chan->ops->free != NULL)
342 (*chan->ops->free)(chan->data);
343 free_irq_by_fd(chan->fd);
344 if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
348 void free_chan(struct list_head *chans)
350 struct list_head *ele, *next;
353 list_for_each_safe(ele, next, chans){
354 chan = list_entry(ele, struct chan, list);
359 static int one_chan_config_string(struct chan *chan, char *str, int size,
365 CONFIG_CHUNK(str, size, n, "none", 1);
369 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
371 if(chan->dev == NULL){
372 CONFIG_CHUNK(str, size, n, "", 1);
376 CONFIG_CHUNK(str, size, n, ":", 0);
377 CONFIG_CHUNK(str, size, n, chan->dev, 0);
382 static int chan_pair_config_string(struct chan *in, struct chan *out,
383 char *str, int size, char **error_out)
387 n = one_chan_config_string(in, str, size, error_out);
392 CONFIG_CHUNK(str, size, n, "", 1);
396 CONFIG_CHUNK(str, size, n, ",", 1);
397 n = one_chan_config_string(out, str, size, error_out);
400 CONFIG_CHUNK(str, size, n, "", 1);
405 int chan_config_string(struct list_head *chans, char *str, int size,
408 struct list_head *ele;
409 struct chan *chan, *in = NULL, *out = NULL;
411 list_for_each(ele, chans){
412 chan = list_entry(ele, struct chan, list);
421 return(chan_pair_config_string(in, out, str, size, error_out));
426 struct chan_ops *ops;
429 struct chan_type chan_table[] = {
432 #ifdef CONFIG_NULL_CHAN
433 { "null", &null_ops },
435 { "null", ¬_configged_ops },
438 #ifdef CONFIG_PORT_CHAN
439 { "port", &port_ops },
441 { "port", ¬_configged_ops },
444 #ifdef CONFIG_PTY_CHAN
448 { "pty", ¬_configged_ops },
449 { "pts", ¬_configged_ops },
452 #ifdef CONFIG_TTY_CHAN
455 { "tty", ¬_configged_ops },
458 #ifdef CONFIG_XTERM_CHAN
459 { "xterm", &xterm_ops },
461 { "xterm", ¬_configged_ops },
465 static struct chan *parse_chan(char *str, int pri, int device,
466 struct chan_opts *opts)
468 struct chan_type *entry;
469 struct chan_ops *ops;
476 for(i = 0; i < sizeof(chan_table)/sizeof(chan_table[0]); i++){
477 entry = &chan_table[i];
478 if(!strncmp(str, entry->key, strlen(entry->key))){
480 str += strlen(entry->key);
485 my_printf("parse_chan couldn't parse \"%s\"\n",
489 if(ops->init == NULL) return(NULL);
490 data = (*ops->init)(str, device, opts);
491 if(data == NULL) return(NULL);
493 chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
494 if(chan == NULL) return(NULL);
495 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
507 int parse_chan_pair(char *str, struct list_head *chans, int pri, int device,
508 struct chan_opts *opts)
510 struct chan *new, *chan;
513 if(!list_empty(chans)){
514 chan = list_entry(chans->next, struct chan, list);
515 if(chan->pri >= pri) return(0);
517 INIT_LIST_HEAD(chans);
520 out = strchr(str, ',');
525 new = parse_chan(in, pri, device, opts);
526 if(new == NULL) return(-1);
528 list_add(&new->list, chans);
530 new = parse_chan(out, pri, device, opts);
531 if(new == NULL) return(-1);
532 list_add(&new->list, chans);
536 new = parse_chan(str, pri, device, opts);
537 if(new == NULL) return(-1);
538 list_add(&new->list, chans);
545 int chan_out_fd(struct list_head *chans)
547 struct list_head *ele;
550 list_for_each(ele, chans){
551 chan = list_entry(ele, struct chan, list);
552 if(chan->primary && chan->output)
558 void chan_interrupt(struct list_head *chans, struct work_struct *task,
559 struct tty_struct *tty, int irq)
561 struct list_head *ele, *next;
566 list_for_each_safe(ele, next, chans){
567 chan = list_entry(ele, struct chan, list);
568 if(!chan->input || (chan->ops->read == NULL)) continue;
571 (tty->flip.count >= TTY_FLIPBUF_SIZE)){
575 err = chan->ops->read(chan->fd, &c, chan->data);
577 tty_receive_char(tty, c);
580 if(err == 0) reactivate_fd(chan->fd, irq);
585 line_disable(tty, irq);
591 if(chan->ops->close != NULL)
592 chan->ops->close(chan->fd, chan->data);
598 if(tty) tty_flip_buffer_push(tty);
602 * Overrides for Emacs so that we follow Linus's tabbing style.
603 * Emacs will notice this stuff at the end of the file and automatically
604 * adjust the settings for this buffer only. This must remain at the end
606 * ---------------------------------------------------------------------------
608 * c-file-style: "linux"