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 static void *not_configged_init(char *str, int device, struct chan_opts *opts)
24 printk(KERN_ERR "Using a channel type which is configured out of "
29 static int not_configged_open(int input, int output, int primary, void *data,
32 printk(KERN_ERR "Using a channel type which is configured out of "
37 static void not_configged_close(int fd, void *data)
39 printk(KERN_ERR "Using a channel type which is configured out of "
43 static int not_configged_read(int fd, char *c_out, void *data)
45 printk(KERN_ERR "Using a channel type which is configured out of "
50 static int not_configged_write(int fd, const char *buf, int len, void *data)
52 printk(KERN_ERR "Using a channel type which is configured out of "
57 static int not_configged_console_write(int fd, const char *buf, int len,
60 printk(KERN_ERR "Using a channel type which is configured out of "
65 static int not_configged_window_size(int fd, void *data, unsigned short *rows,
68 printk(KERN_ERR "Using a channel type which is configured out of "
73 static void not_configged_free(void *data)
75 printk(KERN_ERR "Using a channel type which is configured out of "
79 static struct chan_ops not_configged_ops = {
80 .init = not_configged_init,
81 .open = not_configged_open,
82 .close = not_configged_close,
83 .read = not_configged_read,
84 .write = not_configged_write,
85 .console_write = not_configged_console_write,
86 .window_size = not_configged_window_size,
87 .free = not_configged_free,
91 void generic_close(int fd, void *unused)
96 int generic_read(int fd, char *c_out, void *unused)
100 n = os_read_file(fd, c_out, sizeof(*c_out));
109 int generic_write(int fd, const char *buf, int n, void *unused)
111 return(os_write_file(fd, buf, n));
114 int generic_window_size(int fd, void *unused, unsigned short *rows_out,
115 unsigned short *cols_out)
120 ret = os_window_size(fd, &rows, &cols);
124 ret = ((*rows_out != rows) || (*cols_out != cols));
132 void generic_free(void *data)
137 static void tty_receive_char(struct tty_struct *tty, char ch)
139 if(tty == NULL) return;
141 if(I_IXON(tty) && !I_IXOFF(tty) && !tty->raw) {
142 if(ch == STOP_CHAR(tty)){
146 else if(ch == START_CHAR(tty)){
152 if((tty->flip.flag_buf_ptr == NULL) ||
153 (tty->flip.char_buf_ptr == NULL))
155 tty_insert_flip_char(tty, ch, TTY_NORMAL);
158 static int open_one_chan(struct chan *chan, int input, int output, int primary)
162 if(chan->opened) return(0);
163 if(chan->ops->open == NULL) fd = 0;
164 else fd = (*chan->ops->open)(input, output, primary, chan->data,
166 if(fd < 0) return(fd);
173 int open_chan(struct list_head *chans)
175 struct list_head *ele;
179 list_for_each(ele, chans){
180 chan = list_entry(ele, struct chan, list);
181 ret = open_one_chan(chan, chan->input, chan->output,
183 if(chan->primary) err = ret;
188 void chan_enable_winch(struct list_head *chans, void *line)
190 struct list_head *ele;
193 list_for_each(ele, chans){
194 chan = list_entry(ele, struct chan, list);
195 if(chan->primary && chan->output && chan->ops->winch){
196 register_winch(chan->fd, line);
202 void enable_chan(struct list_head *chans, void *data)
204 struct list_head *ele;
207 list_for_each(ele, chans){
208 chan = list_entry(ele, struct chan, list);
209 if(!chan->opened) continue;
211 line_setup_irq(chan->fd, chan->input, chan->output, data);
215 void close_chan(struct list_head *chans)
217 struct list_head *ele;
220 /* Close in reverse order as open in case more than one of them
221 * refers to the same device and they save and restore that device's
222 * state. Then, the first one opened will have the original state,
223 * so it must be the last closed.
225 for(ele = chans->prev; ele != chans; ele = ele->prev){
226 chan = list_entry(ele, struct chan, list);
227 if(!chan->opened) continue;
228 if(chan->ops->close != NULL)
229 (*chan->ops->close)(chan->fd, chan->data);
235 int write_chan(struct list_head *chans, const char *buf, int len,
238 struct list_head *ele;
242 list_for_each(ele, chans){
243 chan = list_entry(ele, struct chan, list);
244 if(!chan->output || (chan->ops->write == NULL)) continue;
245 n = chan->ops->write(chan->fd, buf, len, chan->data);
248 if((ret == -EAGAIN) || ((ret >= 0) && (ret < len))){
249 reactivate_fd(chan->fd, write_irq);
250 if(ret == -EAGAIN) ret = 0;
257 int console_write_chan(struct list_head *chans, const char *buf, int len)
259 struct list_head *ele;
263 list_for_each(ele, chans){
264 chan = list_entry(ele, struct chan, list);
265 if(!chan->output || (chan->ops->console_write == NULL))
267 n = chan->ops->console_write(chan->fd, buf, len, chan->data);
268 if(chan->primary) ret = n;
273 int chan_window_size(struct list_head *chans, unsigned short *rows_out,
274 unsigned short *cols_out)
276 struct list_head *ele;
279 list_for_each(ele, chans){
280 chan = list_entry(ele, struct chan, list);
282 if(chan->ops->window_size == NULL) return(0);
283 return(chan->ops->window_size(chan->fd, chan->data,
284 rows_out, cols_out));
290 void free_one_chan(struct chan *chan)
292 list_del(&chan->list);
293 if(chan->ops->free != NULL)
294 (*chan->ops->free)(chan->data);
295 free_irq_by_fd(chan->fd);
296 if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
300 void free_chan(struct list_head *chans)
302 struct list_head *ele, *next;
305 list_for_each_safe(ele, next, chans){
306 chan = list_entry(ele, struct chan, list);
311 static int one_chan_config_string(struct chan *chan, char *str, int size,
317 CONFIG_CHUNK(str, size, n, "none", 1);
321 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
323 if(chan->dev == NULL){
324 CONFIG_CHUNK(str, size, n, "", 1);
328 CONFIG_CHUNK(str, size, n, ":", 0);
329 CONFIG_CHUNK(str, size, n, chan->dev, 0);
334 static int chan_pair_config_string(struct chan *in, struct chan *out,
335 char *str, int size, char **error_out)
339 n = one_chan_config_string(in, str, size, error_out);
344 CONFIG_CHUNK(str, size, n, "", 1);
348 CONFIG_CHUNK(str, size, n, ",", 1);
349 n = one_chan_config_string(out, str, size, error_out);
352 CONFIG_CHUNK(str, size, n, "", 1);
357 int chan_config_string(struct list_head *chans, char *str, int size,
360 struct list_head *ele;
361 struct chan *chan, *in = NULL, *out = NULL;
363 list_for_each(ele, chans){
364 chan = list_entry(ele, struct chan, list);
373 return(chan_pair_config_string(in, out, str, size, error_out));
378 struct chan_ops *ops;
381 struct chan_type chan_table[] = {
382 #ifdef CONFIG_FD_CHAN
385 { "fd", ¬_configged_ops },
388 #ifdef CONFIG_NULL_CHAN
389 { "null", &null_ops },
391 { "null", ¬_configged_ops },
394 #ifdef CONFIG_PORT_CHAN
395 { "port", &port_ops },
397 { "port", ¬_configged_ops },
400 #ifdef CONFIG_PTY_CHAN
404 { "pty", ¬_configged_ops },
405 { "pts", ¬_configged_ops },
408 #ifdef CONFIG_TTY_CHAN
411 { "tty", ¬_configged_ops },
414 #ifdef CONFIG_XTERM_CHAN
415 { "xterm", &xterm_ops },
417 { "xterm", ¬_configged_ops },
421 static struct chan *parse_chan(char *str, int pri, int device,
422 struct chan_opts *opts)
424 struct chan_type *entry;
425 struct chan_ops *ops;
432 for(i = 0; i < sizeof(chan_table)/sizeof(chan_table[0]); i++){
433 entry = &chan_table[i];
434 if(!strncmp(str, entry->key, strlen(entry->key))){
436 str += strlen(entry->key);
441 printk(KERN_ERR "parse_chan couldn't parse \"%s\"\n",
445 if(ops->init == NULL) return(NULL);
446 data = (*ops->init)(str, device, opts);
447 if(data == NULL) return(NULL);
449 chan = kmalloc(sizeof(*chan), GFP_KERNEL);
450 if(chan == NULL) return(NULL);
451 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
463 int parse_chan_pair(char *str, struct list_head *chans, int pri, int device,
464 struct chan_opts *opts)
466 struct chan *new, *chan;
469 if(!list_empty(chans)){
470 chan = list_entry(chans->next, struct chan, list);
471 if(chan->pri >= pri) return(0);
473 INIT_LIST_HEAD(chans);
476 out = strchr(str, ',');
481 new = parse_chan(in, pri, device, opts);
482 if(new == NULL) return(-1);
484 list_add(&new->list, chans);
486 new = parse_chan(out, pri, device, opts);
487 if(new == NULL) return(-1);
488 list_add(&new->list, chans);
492 new = parse_chan(str, pri, device, opts);
493 if(new == NULL) return(-1);
494 list_add(&new->list, chans);
501 int chan_out_fd(struct list_head *chans)
503 struct list_head *ele;
506 list_for_each(ele, chans){
507 chan = list_entry(ele, struct chan, list);
508 if(chan->primary && chan->output)
514 void chan_interrupt(struct list_head *chans, struct work_struct *task,
515 struct tty_struct *tty, int irq, void *dev)
517 struct list_head *ele, *next;
522 list_for_each_safe(ele, next, chans){
523 chan = list_entry(ele, struct chan, list);
524 if(!chan->input || (chan->ops->read == NULL)) continue;
527 (tty->flip.count >= TTY_FLIPBUF_SIZE)){
531 err = chan->ops->read(chan->fd, &c, chan->data);
533 tty_receive_char(tty, c);
536 if(err == 0) reactivate_fd(chan->fd, irq);
541 line_disable(dev, irq);
547 if(chan->ops->close != NULL)
548 chan->ops->close(chan->fd, chan->data);
554 if(tty) tty_flip_buffer_push(tty);
558 * Overrides for Emacs so that we follow Linus's tabbing style.
559 * Emacs will notice this stuff at the end of the file and automatically
560 * adjust the settings for this buffer only. This must remain at the end
562 * ---------------------------------------------------------------------------
564 * c-file-style: "linux"