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"
21 static void *not_configged_init(char *str, int device, struct chan_opts *opts)
23 printk(KERN_ERR "Using a channel type which is configured out of "
28 static int not_configged_open(int input, int output, int primary, void *data,
31 printk(KERN_ERR "Using a channel type which is configured out of "
36 static void not_configged_close(int fd, void *data)
38 printk(KERN_ERR "Using a channel type which is configured out of "
42 static int not_configged_read(int fd, char *c_out, void *data)
44 printk(KERN_ERR "Using a channel type which is configured out of "
49 static int not_configged_write(int fd, const char *buf, int len, void *data)
51 printk(KERN_ERR "Using a channel type which is configured out of "
56 static int not_configged_console_write(int fd, const char *buf, int len,
59 printk(KERN_ERR "Using a channel type which is configured out of "
64 static int not_configged_window_size(int fd, void *data, unsigned short *rows,
67 printk(KERN_ERR "Using a channel type which is configured out of "
72 static void not_configged_free(void *data)
74 printk(KERN_ERR "Using a channel type which is configured out of "
78 static struct chan_ops not_configged_ops = {
79 .init = not_configged_init,
80 .open = not_configged_open,
81 .close = not_configged_close,
82 .read = not_configged_read,
83 .write = not_configged_write,
84 .console_write = not_configged_console_write,
85 .window_size = not_configged_window_size,
86 .free = not_configged_free,
90 static void tty_receive_char(struct tty_struct *tty, char ch)
92 if(tty == NULL) return;
94 if(I_IXON(tty) && !I_IXOFF(tty) && !tty->raw) {
95 if(ch == STOP_CHAR(tty)){
99 else if(ch == START_CHAR(tty)){
105 if((tty->flip.flag_buf_ptr == NULL) ||
106 (tty->flip.char_buf_ptr == NULL))
108 tty_insert_flip_char(tty, ch, TTY_NORMAL);
111 static int open_one_chan(struct chan *chan, int input, int output, int primary)
115 if(chan->opened) return(0);
116 if(chan->ops->open == NULL) fd = 0;
117 else fd = (*chan->ops->open)(input, output, primary, chan->data,
119 if(fd < 0) return(fd);
126 int open_chan(struct list_head *chans)
128 struct list_head *ele;
132 list_for_each(ele, chans){
133 chan = list_entry(ele, struct chan, list);
134 ret = open_one_chan(chan, chan->input, chan->output,
136 if(chan->primary) err = ret;
141 void chan_enable_winch(struct list_head *chans, void *line)
143 struct list_head *ele;
146 list_for_each(ele, chans){
147 chan = list_entry(ele, struct chan, list);
148 if(chan->primary && chan->output && chan->ops->winch){
149 register_winch(chan->fd, line);
155 void enable_chan(struct list_head *chans, void *data)
157 struct list_head *ele;
160 list_for_each(ele, chans){
161 chan = list_entry(ele, struct chan, list);
162 if(!chan->opened) continue;
164 line_setup_irq(chan->fd, chan->input, chan->output, data);
168 void close_chan(struct list_head *chans)
170 struct list_head *ele;
173 /* Close in reverse order as open in case more than one of them
174 * refers to the same device and they save and restore that device's
175 * state. Then, the first one opened will have the original state,
176 * so it must be the last closed.
178 for(ele = chans->prev; ele != chans; ele = ele->prev){
179 chan = list_entry(ele, struct chan, list);
180 if(!chan->opened) continue;
181 if(chan->ops->close != NULL)
182 (*chan->ops->close)(chan->fd, chan->data);
188 int write_chan(struct list_head *chans, const char *buf, int len,
191 struct list_head *ele;
195 list_for_each(ele, chans){
196 chan = list_entry(ele, struct chan, list);
197 if(!chan->output || (chan->ops->write == NULL)) continue;
198 n = chan->ops->write(chan->fd, buf, len, chan->data);
201 if((ret == -EAGAIN) || ((ret >= 0) && (ret < len))){
202 reactivate_fd(chan->fd, write_irq);
203 if(ret == -EAGAIN) ret = 0;
210 int console_write_chan(struct list_head *chans, const char *buf, int len)
212 struct list_head *ele;
216 list_for_each(ele, chans){
217 chan = list_entry(ele, struct chan, list);
218 if(!chan->output || (chan->ops->console_write == NULL))
220 n = chan->ops->console_write(chan->fd, buf, len, chan->data);
221 if(chan->primary) ret = n;
226 int chan_window_size(struct list_head *chans, unsigned short *rows_out,
227 unsigned short *cols_out)
229 struct list_head *ele;
232 list_for_each(ele, chans){
233 chan = list_entry(ele, struct chan, list);
235 if(chan->ops->window_size == NULL) return(0);
236 return(chan->ops->window_size(chan->fd, chan->data,
237 rows_out, cols_out));
243 void free_one_chan(struct chan *chan)
245 list_del(&chan->list);
246 if(chan->ops->free != NULL)
247 (*chan->ops->free)(chan->data);
248 free_irq_by_fd(chan->fd);
249 if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
253 void free_chan(struct list_head *chans)
255 struct list_head *ele, *next;
258 list_for_each_safe(ele, next, chans){
259 chan = list_entry(ele, struct chan, list);
264 static int one_chan_config_string(struct chan *chan, char *str, int size,
270 CONFIG_CHUNK(str, size, n, "none", 1);
274 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
276 if(chan->dev == NULL){
277 CONFIG_CHUNK(str, size, n, "", 1);
281 CONFIG_CHUNK(str, size, n, ":", 0);
282 CONFIG_CHUNK(str, size, n, chan->dev, 0);
287 static int chan_pair_config_string(struct chan *in, struct chan *out,
288 char *str, int size, char **error_out)
292 n = one_chan_config_string(in, str, size, error_out);
297 CONFIG_CHUNK(str, size, n, "", 1);
301 CONFIG_CHUNK(str, size, n, ",", 1);
302 n = one_chan_config_string(out, str, size, error_out);
305 CONFIG_CHUNK(str, size, n, "", 1);
310 int chan_config_string(struct list_head *chans, char *str, int size,
313 struct list_head *ele;
314 struct chan *chan, *in = NULL, *out = NULL;
316 list_for_each(ele, chans){
317 chan = list_entry(ele, struct chan, list);
326 return(chan_pair_config_string(in, out, str, size, error_out));
331 struct chan_ops *ops;
334 struct chan_type chan_table[] = {
335 #ifdef CONFIG_FD_CHAN
338 { "fd", ¬_configged_ops },
341 #ifdef CONFIG_NULL_CHAN
342 { "null", &null_ops },
344 { "null", ¬_configged_ops },
347 #ifdef CONFIG_PORT_CHAN
348 { "port", &port_ops },
350 { "port", ¬_configged_ops },
353 #ifdef CONFIG_PTY_CHAN
357 { "pty", ¬_configged_ops },
358 { "pts", ¬_configged_ops },
361 #ifdef CONFIG_TTY_CHAN
364 { "tty", ¬_configged_ops },
367 #ifdef CONFIG_XTERM_CHAN
368 { "xterm", &xterm_ops },
370 { "xterm", ¬_configged_ops },
374 static struct chan *parse_chan(char *str, int pri, int device,
375 struct chan_opts *opts)
377 struct chan_type *entry;
378 struct chan_ops *ops;
385 for(i = 0; i < sizeof(chan_table)/sizeof(chan_table[0]); i++){
386 entry = &chan_table[i];
387 if(!strncmp(str, entry->key, strlen(entry->key))){
389 str += strlen(entry->key);
394 printk(KERN_ERR "parse_chan couldn't parse \"%s\"\n",
398 if(ops->init == NULL) return(NULL);
399 data = (*ops->init)(str, device, opts);
400 if(data == NULL) return(NULL);
402 chan = kmalloc(sizeof(*chan), GFP_KERNEL);
403 if(chan == NULL) return(NULL);
404 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
416 int parse_chan_pair(char *str, struct list_head *chans, int pri, int device,
417 struct chan_opts *opts)
419 struct chan *new, *chan;
422 if(!list_empty(chans)){
423 chan = list_entry(chans->next, struct chan, list);
424 if(chan->pri >= pri) return(0);
426 INIT_LIST_HEAD(chans);
429 out = strchr(str, ',');
434 new = parse_chan(in, pri, device, opts);
435 if(new == NULL) return(-1);
437 list_add(&new->list, chans);
439 new = parse_chan(out, pri, device, opts);
440 if(new == NULL) return(-1);
441 list_add(&new->list, chans);
445 new = parse_chan(str, pri, device, opts);
446 if(new == NULL) return(-1);
447 list_add(&new->list, chans);
454 int chan_out_fd(struct list_head *chans)
456 struct list_head *ele;
459 list_for_each(ele, chans){
460 chan = list_entry(ele, struct chan, list);
461 if(chan->primary && chan->output)
467 void chan_interrupt(struct list_head *chans, struct work_struct *task,
468 struct tty_struct *tty, int irq, void *dev)
470 struct list_head *ele, *next;
475 list_for_each_safe(ele, next, chans){
476 chan = list_entry(ele, struct chan, list);
477 if(!chan->input || (chan->ops->read == NULL)) continue;
480 (tty->flip.count >= TTY_FLIPBUF_SIZE)){
484 err = chan->ops->read(chan->fd, &c, chan->data);
486 tty_receive_char(tty, c);
489 if(err == 0) reactivate_fd(chan->fd, irq);
494 line_disable(dev, irq);
500 if(chan->ops->close != NULL)
501 chan->ops->close(chan->fd, chan->data);
507 if(tty) tty_flip_buffer_push(tty);
511 * Overrides for Emacs so that we follow Linus's tabbing style.
512 * Emacs will notice this stuff at the end of the file and automatically
513 * adjust the settings for this buffer only. This must remain at the end
515 * ---------------------------------------------------------------------------
517 * c-file-style: "linux"