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 /* XXX Trivial wrapper around os_write_file */
111 int generic_write(int fd, const char *buf, int n, void *unused)
113 return(os_write_file(fd, buf, n));
116 int generic_window_size(int fd, void *unused, unsigned short *rows_out,
117 unsigned short *cols_out)
122 ret = os_window_size(fd, &rows, &cols);
126 ret = ((*rows_out != rows) || (*cols_out != cols));
134 void generic_free(void *data)
139 static void tty_receive_char(struct tty_struct *tty, char ch)
141 if(tty == NULL) return;
143 if(I_IXON(tty) && !I_IXOFF(tty) && !tty->raw) {
144 if(ch == STOP_CHAR(tty)){
148 else if(ch == START_CHAR(tty)){
154 if((tty->flip.flag_buf_ptr == NULL) ||
155 (tty->flip.char_buf_ptr == NULL))
157 tty_insert_flip_char(tty, ch, TTY_NORMAL);
160 static int open_one_chan(struct chan *chan, int input, int output, int primary)
164 if(chan->opened) return(0);
165 if(chan->ops->open == NULL) fd = 0;
166 else fd = (*chan->ops->open)(input, output, primary, chan->data,
168 if(fd < 0) return(fd);
175 int open_chan(struct list_head *chans)
177 struct list_head *ele;
181 list_for_each(ele, chans){
182 chan = list_entry(ele, struct chan, list);
183 ret = open_one_chan(chan, chan->input, chan->output,
185 if(chan->primary) err = ret;
190 void chan_enable_winch(struct list_head *chans, void *line)
192 struct list_head *ele;
195 list_for_each(ele, chans){
196 chan = list_entry(ele, struct chan, list);
197 if(chan->primary && chan->output && chan->ops->winch){
198 register_winch(chan->fd, line);
204 void enable_chan(struct list_head *chans, void *data)
206 struct list_head *ele;
209 list_for_each(ele, chans){
210 chan = list_entry(ele, struct chan, list);
211 if(!chan->opened) continue;
213 line_setup_irq(chan->fd, chan->input, chan->output, data);
217 void close_chan(struct list_head *chans)
219 struct list_head *ele;
222 /* Close in reverse order as open in case more than one of them
223 * refers to the same device and they save and restore that device's
224 * state. Then, the first one opened will have the original state,
225 * so it must be the last closed.
227 for(ele = chans->prev; ele != chans; ele = ele->prev){
228 chan = list_entry(ele, struct chan, list);
229 if(!chan->opened) continue;
230 if(chan->ops->close != NULL)
231 (*chan->ops->close)(chan->fd, chan->data);
237 int write_chan(struct list_head *chans, const char *buf, int len,
240 struct list_head *ele;
244 list_for_each(ele, chans){
245 chan = list_entry(ele, struct chan, list);
246 if(!chan->output || (chan->ops->write == NULL)) continue;
247 n = chan->ops->write(chan->fd, buf, len, chan->data);
250 if((ret == -EAGAIN) || ((ret >= 0) && (ret < len))){
251 reactivate_fd(chan->fd, write_irq);
252 if(ret == -EAGAIN) ret = 0;
259 int console_write_chan(struct list_head *chans, const char *buf, int len)
261 struct list_head *ele;
265 list_for_each(ele, chans){
266 chan = list_entry(ele, struct chan, list);
267 if(!chan->output || (chan->ops->console_write == NULL))
269 n = chan->ops->console_write(chan->fd, buf, len, chan->data);
270 if(chan->primary) ret = n;
275 int chan_window_size(struct list_head *chans, unsigned short *rows_out,
276 unsigned short *cols_out)
278 struct list_head *ele;
281 list_for_each(ele, chans){
282 chan = list_entry(ele, struct chan, list);
284 if(chan->ops->window_size == NULL) return(0);
285 return(chan->ops->window_size(chan->fd, chan->data,
286 rows_out, cols_out));
292 void free_one_chan(struct chan *chan)
294 list_del(&chan->list);
295 if(chan->ops->free != NULL)
296 (*chan->ops->free)(chan->data);
297 free_irq_by_fd(chan->fd);
298 if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
302 void free_chan(struct list_head *chans)
304 struct list_head *ele, *next;
307 list_for_each_safe(ele, next, chans){
308 chan = list_entry(ele, struct chan, list);
313 static int one_chan_config_string(struct chan *chan, char *str, int size,
319 CONFIG_CHUNK(str, size, n, "none", 1);
323 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
325 if(chan->dev == NULL){
326 CONFIG_CHUNK(str, size, n, "", 1);
330 CONFIG_CHUNK(str, size, n, ":", 0);
331 CONFIG_CHUNK(str, size, n, chan->dev, 0);
336 static int chan_pair_config_string(struct chan *in, struct chan *out,
337 char *str, int size, char **error_out)
341 n = one_chan_config_string(in, str, size, error_out);
346 CONFIG_CHUNK(str, size, n, "", 1);
350 CONFIG_CHUNK(str, size, n, ",", 1);
351 n = one_chan_config_string(out, str, size, error_out);
354 CONFIG_CHUNK(str, size, n, "", 1);
359 int chan_config_string(struct list_head *chans, char *str, int size,
362 struct list_head *ele;
363 struct chan *chan, *in = NULL, *out = NULL;
365 list_for_each(ele, chans){
366 chan = list_entry(ele, struct chan, list);
375 return(chan_pair_config_string(in, out, str, size, error_out));
380 struct chan_ops *ops;
383 struct chan_type chan_table[] = {
384 #ifdef CONFIG_FD_CHAN
387 { "fd", ¬_configged_ops },
390 #ifdef CONFIG_NULL_CHAN
391 { "null", &null_ops },
393 { "null", ¬_configged_ops },
396 #ifdef CONFIG_PORT_CHAN
397 { "port", &port_ops },
399 { "port", ¬_configged_ops },
402 #ifdef CONFIG_PTY_CHAN
406 { "pty", ¬_configged_ops },
407 { "pts", ¬_configged_ops },
410 #ifdef CONFIG_TTY_CHAN
413 { "tty", ¬_configged_ops },
416 #ifdef CONFIG_XTERM_CHAN
417 { "xterm", &xterm_ops },
419 { "xterm", ¬_configged_ops },
423 static struct chan *parse_chan(char *str, int pri, int device,
424 struct chan_opts *opts)
426 struct chan_type *entry;
427 struct chan_ops *ops;
434 for(i = 0; i < sizeof(chan_table)/sizeof(chan_table[0]); i++){
435 entry = &chan_table[i];
436 if(!strncmp(str, entry->key, strlen(entry->key))){
438 str += strlen(entry->key);
443 printk(KERN_ERR "parse_chan couldn't parse \"%s\"\n",
447 if(ops->init == NULL) return(NULL);
448 data = (*ops->init)(str, device, opts);
449 if(data == NULL) return(NULL);
451 chan = kmalloc(sizeof(*chan), GFP_KERNEL);
452 if(chan == NULL) return(NULL);
453 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
465 int parse_chan_pair(char *str, struct list_head *chans, int pri, int device,
466 struct chan_opts *opts)
468 struct chan *new, *chan;
471 if(!list_empty(chans)){
472 chan = list_entry(chans->next, struct chan, list);
473 if(chan->pri >= pri) return(0);
475 INIT_LIST_HEAD(chans);
478 out = strchr(str, ',');
483 new = parse_chan(in, pri, device, opts);
484 if(new == NULL) return(-1);
486 list_add(&new->list, chans);
488 new = parse_chan(out, pri, device, opts);
489 if(new == NULL) return(-1);
490 list_add(&new->list, chans);
494 new = parse_chan(str, pri, device, opts);
495 if(new == NULL) return(-1);
496 list_add(&new->list, chans);
503 int chan_out_fd(struct list_head *chans)
505 struct list_head *ele;
508 list_for_each(ele, chans){
509 chan = list_entry(ele, struct chan, list);
510 if(chan->primary && chan->output)
516 void chan_interrupt(struct list_head *chans, struct work_struct *task,
517 struct tty_struct *tty, int irq, void *dev)
519 struct list_head *ele, *next;
524 list_for_each_safe(ele, next, chans){
525 chan = list_entry(ele, struct chan, list);
526 if(!chan->input || (chan->ops->read == NULL)) continue;
529 (tty->flip.count >= TTY_FLIPBUF_SIZE)){
533 err = chan->ops->read(chan->fd, &c, chan->data);
535 tty_receive_char(tty, c);
538 if(err == 0) reactivate_fd(chan->fd, irq);
543 line_disable(dev, irq);
549 if(chan->ops->close != NULL)
550 chan->ops->close(chan->fd, chan->data);
556 if(tty) tty_flip_buffer_push(tty);
560 * Overrides for Emacs so that we follow Linus's tabbing style.
561 * Emacs will notice this stuff at the end of the file and automatically
562 * adjust the settings for this buffer only. This must remain at the end
564 * ---------------------------------------------------------------------------
566 * c-file-style: "linux"