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 #ifdef CONFIG_NOCONFIG_CHAN
23 static void *not_configged_init(char *str, int device, struct chan_opts *opts)
25 printk(KERN_ERR "Using a channel type which is configured out of "
30 static int not_configged_open(int input, int output, int primary, void *data,
33 printk(KERN_ERR "Using a channel type which is configured out of "
38 static void not_configged_close(int fd, void *data)
40 printk(KERN_ERR "Using a channel type which is configured out of "
44 static int not_configged_read(int fd, char *c_out, void *data)
46 printk(KERN_ERR "Using a channel type which is configured out of "
51 static int not_configged_write(int fd, const char *buf, int len, void *data)
53 printk(KERN_ERR "Using a channel type which is configured out of "
58 static int not_configged_console_write(int fd, const char *buf, int len,
61 printk(KERN_ERR "Using a channel type which is configured out of "
66 static int not_configged_window_size(int fd, void *data, unsigned short *rows,
69 printk(KERN_ERR "Using a channel type which is configured out of "
74 static void not_configged_free(void *data)
76 printk(KERN_ERR "Using a channel type which is configured out of "
80 static struct chan_ops not_configged_ops = {
81 .init = not_configged_init,
82 .open = not_configged_open,
83 .close = not_configged_close,
84 .read = not_configged_read,
85 .write = not_configged_write,
86 .console_write = not_configged_console_write,
87 .window_size = not_configged_window_size,
88 .free = not_configged_free,
91 #endif /* CONFIG_NOCONFIG_CHAN */
93 void generic_close(int fd, void *unused)
98 int generic_read(int fd, char *c_out, void *unused)
102 n = os_read_file(fd, c_out, sizeof(*c_out));
111 /* XXX Trivial wrapper around os_write_file */
113 int generic_write(int fd, const char *buf, int n, void *unused)
115 return(os_write_file(fd, buf, n));
118 int generic_window_size(int fd, void *unused, unsigned short *rows_out,
119 unsigned short *cols_out)
124 ret = os_window_size(fd, &rows, &cols);
128 ret = ((*rows_out != rows) || (*cols_out != cols));
136 void generic_free(void *data)
141 void generic_close(int fd, void *unused)
146 int generic_read(int fd, char *c_out, void *unused)
150 n = os_read_file(fd, c_out, sizeof(*c_out));
159 /* XXX Trivial wrapper around os_write_file */
161 int generic_write(int fd, const char *buf, int n, void *unused)
163 return(os_write_file(fd, buf, n));
166 int generic_window_size(int fd, void *unused, unsigned short *rows_out,
167 unsigned short *cols_out)
172 ret = os_window_size(fd, &rows, &cols);
176 ret = ((*rows_out != rows) || (*cols_out != cols));
184 void generic_free(void *data)
189 static void tty_receive_char(struct tty_struct *tty, char ch)
191 if(tty == NULL) return;
193 if(I_IXON(tty) && !I_IXOFF(tty) && !tty->raw) {
194 if(ch == STOP_CHAR(tty)){
198 else if(ch == START_CHAR(tty)){
204 if((tty->flip.flag_buf_ptr == NULL) ||
205 (tty->flip.char_buf_ptr == NULL))
207 tty_insert_flip_char(tty, ch, TTY_NORMAL);
210 static int open_one_chan(struct chan *chan, int input, int output, int primary)
214 if(chan->opened) return(0);
215 if(chan->ops->open == NULL) fd = 0;
216 else fd = (*chan->ops->open)(input, output, primary, chan->data,
218 if(fd < 0) return(fd);
225 int open_chan(struct list_head *chans)
227 struct list_head *ele;
231 list_for_each(ele, chans){
232 chan = list_entry(ele, struct chan, list);
233 ret = open_one_chan(chan, chan->input, chan->output,
235 if(chan->primary) err = ret;
240 void chan_enable_winch(struct list_head *chans, void *line)
242 struct list_head *ele;
245 list_for_each(ele, chans){
246 chan = list_entry(ele, struct chan, list);
247 if(chan->primary && chan->output && chan->ops->winch){
248 register_winch(chan->fd, line);
254 void enable_chan(struct list_head *chans, void *data)
256 struct list_head *ele;
259 list_for_each(ele, chans){
260 chan = list_entry(ele, struct chan, list);
261 if(!chan->opened) continue;
263 line_setup_irq(chan->fd, chan->input, chan->output, data);
267 void close_chan(struct list_head *chans)
269 struct list_head *ele;
272 /* Close in reverse order as open in case more than one of them
273 * refers to the same device and they save and restore that device's
274 * state. Then, the first one opened will have the original state,
275 * so it must be the last closed.
277 for(ele = chans->prev; ele != chans; ele = ele->prev){
278 chan = list_entry(ele, struct chan, list);
279 if(!chan->opened) continue;
280 if(chan->ops->close != NULL)
281 (*chan->ops->close)(chan->fd, chan->data);
287 int write_chan(struct list_head *chans, const char *buf, int len,
290 struct list_head *ele;
294 list_for_each(ele, chans){
295 chan = list_entry(ele, struct chan, list);
296 if(!chan->output || (chan->ops->write == NULL)) continue;
297 n = chan->ops->write(chan->fd, buf, len, chan->data);
300 if((ret == -EAGAIN) || ((ret >= 0) && (ret < len))){
301 reactivate_fd(chan->fd, write_irq);
302 if(ret == -EAGAIN) ret = 0;
309 int console_write_chan(struct list_head *chans, const char *buf, int len)
311 struct list_head *ele;
315 list_for_each(ele, chans){
316 chan = list_entry(ele, struct chan, list);
317 if(!chan->output || (chan->ops->console_write == NULL))
319 n = chan->ops->console_write(chan->fd, buf, len, chan->data);
320 if(chan->primary) ret = n;
325 int chan_window_size(struct list_head *chans, unsigned short *rows_out,
326 unsigned short *cols_out)
328 struct list_head *ele;
331 list_for_each(ele, chans){
332 chan = list_entry(ele, struct chan, list);
334 if(chan->ops->window_size == NULL) return(0);
335 return(chan->ops->window_size(chan->fd, chan->data,
336 rows_out, cols_out));
342 void free_one_chan(struct chan *chan)
344 list_del(&chan->list);
345 if(chan->ops->free != NULL)
346 (*chan->ops->free)(chan->data);
347 free_irq_by_fd(chan->fd);
348 if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
352 void free_chan(struct list_head *chans)
354 struct list_head *ele, *next;
357 list_for_each_safe(ele, next, chans){
358 chan = list_entry(ele, struct chan, list);
363 static int one_chan_config_string(struct chan *chan, char *str, int size,
369 CONFIG_CHUNK(str, size, n, "none", 1);
373 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
375 if(chan->dev == NULL){
376 CONFIG_CHUNK(str, size, n, "", 1);
380 CONFIG_CHUNK(str, size, n, ":", 0);
381 CONFIG_CHUNK(str, size, n, chan->dev, 0);
386 static int chan_pair_config_string(struct chan *in, struct chan *out,
387 char *str, int size, char **error_out)
391 n = one_chan_config_string(in, str, size, error_out);
396 CONFIG_CHUNK(str, size, n, "", 1);
400 CONFIG_CHUNK(str, size, n, ",", 1);
401 n = one_chan_config_string(out, str, size, error_out);
404 CONFIG_CHUNK(str, size, n, "", 1);
409 int chan_config_string(struct list_head *chans, char *str, int size,
412 struct list_head *ele;
413 struct chan *chan, *in = NULL, *out = NULL;
415 list_for_each(ele, chans){
416 chan = list_entry(ele, struct chan, list);
425 return(chan_pair_config_string(in, out, str, size, error_out));
430 struct chan_ops *ops;
433 struct chan_type chan_table[] = {
434 #ifdef CONFIG_FD_CHAN
437 { "fd", ¬_configged_ops },
440 #ifdef CONFIG_NULL_CHAN
441 { "null", &null_ops },
443 { "null", ¬_configged_ops },
446 #ifdef CONFIG_PORT_CHAN
447 { "port", &port_ops },
449 { "port", ¬_configged_ops },
452 #ifdef CONFIG_PTY_CHAN
456 { "pty", ¬_configged_ops },
457 { "pts", ¬_configged_ops },
460 #ifdef CONFIG_TTY_CHAN
463 { "tty", ¬_configged_ops },
466 #ifdef CONFIG_XTERM_CHAN
467 { "xterm", &xterm_ops },
469 { "xterm", ¬_configged_ops },
473 static struct chan *parse_chan(char *str, int pri, int device,
474 struct chan_opts *opts)
476 struct chan_type *entry;
477 struct chan_ops *ops;
484 for(i = 0; i < sizeof(chan_table)/sizeof(chan_table[0]); i++){
485 entry = &chan_table[i];
486 if(!strncmp(str, entry->key, strlen(entry->key))){
488 str += strlen(entry->key);
493 printk(KERN_ERR "parse_chan couldn't parse \"%s\"\n",
497 if(ops->init == NULL) return(NULL);
498 data = (*ops->init)(str, device, opts);
499 if(data == NULL) return(NULL);
501 chan = kmalloc(sizeof(*chan), GFP_KERNEL);
502 if(chan == NULL) return(NULL);
503 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
515 int parse_chan_pair(char *str, struct list_head *chans, int pri, int device,
516 struct chan_opts *opts)
518 struct chan *new, *chan;
521 if(!list_empty(chans)){
522 chan = list_entry(chans->next, struct chan, list);
523 if(chan->pri >= pri) return(0);
525 INIT_LIST_HEAD(chans);
528 out = strchr(str, ',');
533 new = parse_chan(in, pri, device, opts);
534 if(new == NULL) return(-1);
536 list_add(&new->list, chans);
538 new = parse_chan(out, pri, device, opts);
539 if(new == NULL) return(-1);
540 list_add(&new->list, chans);
544 new = parse_chan(str, pri, device, opts);
545 if(new == NULL) return(-1);
546 list_add(&new->list, chans);
553 int chan_out_fd(struct list_head *chans)
555 struct list_head *ele;
558 list_for_each(ele, chans){
559 chan = list_entry(ele, struct chan, list);
560 if(chan->primary && chan->output)
566 void chan_interrupt(struct list_head *chans, struct work_struct *task,
567 struct tty_struct *tty, int irq, void *dev)
569 struct list_head *ele, *next;
574 list_for_each_safe(ele, next, chans){
575 chan = list_entry(ele, struct chan, list);
576 if(!chan->input || (chan->ops->read == NULL)) continue;
579 (tty->flip.count >= TTY_FLIPBUF_SIZE)){
583 err = chan->ops->read(chan->fd, &c, chan->data);
585 tty_receive_char(tty, c);
588 if(err == 0) reactivate_fd(chan->fd, irq);
593 line_disable(dev, irq);
599 if(chan->ops->close != NULL)
600 chan->ops->close(chan->fd, chan->data);
606 if(tty) tty_flip_buffer_push(tty);
610 * Overrides for Emacs so that we follow Linus's tabbing style.
611 * Emacs will notice this stuff at the end of the file and automatically
612 * adjust the settings for this buffer only. This must remain at the end
614 * ---------------------------------------------------------------------------
616 * c-file-style: "linux"