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 static void tty_receive_char(struct tty_struct
*tty
, char ch
)
143 if(tty
== NULL
) return;
145 if(I_IXON(tty
) && !I_IXOFF(tty
) && !tty
->raw
) {
146 if(ch
== STOP_CHAR(tty
)){
150 else if(ch
== START_CHAR(tty
)){
156 if((tty
->flip
.flag_buf_ptr
== NULL
) ||
157 (tty
->flip
.char_buf_ptr
== NULL
))
159 tty_insert_flip_char(tty
, ch
, TTY_NORMAL
);
162 static int open_one_chan(struct chan
*chan
, int input
, int output
, int primary
)
166 if(chan
->opened
) return(0);
167 if(chan
->ops
->open
== NULL
) fd
= 0;
168 else fd
= (*chan
->ops
->open
)(input
, output
, primary
, chan
->data
,
170 if(fd
< 0) return(fd
);
177 int open_chan(struct list_head
*chans
)
179 struct list_head
*ele
;
183 list_for_each(ele
, chans
){
184 chan
= list_entry(ele
, struct chan
, list
);
185 ret
= open_one_chan(chan
, chan
->input
, chan
->output
,
187 if(chan
->primary
) err
= ret
;
192 void chan_enable_winch(struct list_head
*chans
, void *line
)
194 struct list_head
*ele
;
197 list_for_each(ele
, chans
){
198 chan
= list_entry(ele
, struct chan
, list
);
199 if(chan
->primary
&& chan
->output
&& chan
->ops
->winch
){
200 register_winch(chan
->fd
, line
);
206 void enable_chan(struct list_head
*chans
, void *data
)
208 struct list_head
*ele
;
211 list_for_each(ele
, chans
){
212 chan
= list_entry(ele
, struct chan
, list
);
213 if(!chan
->opened
) continue;
215 line_setup_irq(chan
->fd
, chan
->input
, chan
->output
, data
);
219 void close_chan(struct list_head
*chans
)
221 struct list_head
*ele
;
224 /* Close in reverse order as open in case more than one of them
225 * refers to the same device and they save and restore that device's
226 * state. Then, the first one opened will have the original state,
227 * so it must be the last closed.
229 for(ele
= chans
->prev
; ele
!= chans
; ele
= ele
->prev
){
230 chan
= list_entry(ele
, struct chan
, list
);
231 if(!chan
->opened
) continue;
232 if(chan
->ops
->close
!= NULL
)
233 (*chan
->ops
->close
)(chan
->fd
, chan
->data
);
239 int write_chan(struct list_head
*chans
, const char *buf
, int len
,
242 struct list_head
*ele
;
246 list_for_each(ele
, chans
){
247 chan
= list_entry(ele
, struct chan
, list
);
248 if(!chan
->output
|| (chan
->ops
->write
== NULL
)) continue;
249 n
= chan
->ops
->write(chan
->fd
, buf
, len
, chan
->data
);
252 if((ret
== -EAGAIN
) || ((ret
>= 0) && (ret
< len
))){
253 reactivate_fd(chan
->fd
, write_irq
);
254 if(ret
== -EAGAIN
) ret
= 0;
261 int console_write_chan(struct list_head
*chans
, const char *buf
, int len
)
263 struct list_head
*ele
;
267 list_for_each(ele
, chans
){
268 chan
= list_entry(ele
, struct chan
, list
);
269 if(!chan
->output
|| (chan
->ops
->console_write
== NULL
))
271 n
= chan
->ops
->console_write(chan
->fd
, buf
, len
, chan
->data
);
272 if(chan
->primary
) ret
= n
;
277 int chan_window_size(struct list_head
*chans
, unsigned short *rows_out
,
278 unsigned short *cols_out
)
280 struct list_head
*ele
;
283 list_for_each(ele
, chans
){
284 chan
= list_entry(ele
, struct chan
, list
);
286 if(chan
->ops
->window_size
== NULL
) return(0);
287 return(chan
->ops
->window_size(chan
->fd
, chan
->data
,
288 rows_out
, cols_out
));
294 void free_one_chan(struct chan
*chan
)
296 list_del(&chan
->list
);
297 if(chan
->ops
->free
!= NULL
)
298 (*chan
->ops
->free
)(chan
->data
);
299 free_irq_by_fd(chan
->fd
);
300 if(chan
->primary
&& chan
->output
) ignore_sigio_fd(chan
->fd
);
304 void free_chan(struct list_head
*chans
)
306 struct list_head
*ele
, *next
;
309 list_for_each_safe(ele
, next
, chans
){
310 chan
= list_entry(ele
, struct chan
, list
);
315 static int one_chan_config_string(struct chan
*chan
, char *str
, int size
,
321 CONFIG_CHUNK(str
, size
, n
, "none", 1);
325 CONFIG_CHUNK(str
, size
, n
, chan
->ops
->type
, 0);
327 if(chan
->dev
== NULL
){
328 CONFIG_CHUNK(str
, size
, n
, "", 1);
332 CONFIG_CHUNK(str
, size
, n
, ":", 0);
333 CONFIG_CHUNK(str
, size
, n
, chan
->dev
, 0);
338 static int chan_pair_config_string(struct chan
*in
, struct chan
*out
,
339 char *str
, int size
, char **error_out
)
343 n
= one_chan_config_string(in
, str
, size
, error_out
);
348 CONFIG_CHUNK(str
, size
, n
, "", 1);
352 CONFIG_CHUNK(str
, size
, n
, ",", 1);
353 n
= one_chan_config_string(out
, str
, size
, error_out
);
356 CONFIG_CHUNK(str
, size
, n
, "", 1);
361 int chan_config_string(struct list_head
*chans
, char *str
, int size
,
364 struct list_head
*ele
;
365 struct chan
*chan
, *in
= NULL
, *out
= NULL
;
367 list_for_each(ele
, chans
){
368 chan
= list_entry(ele
, struct chan
, list
);
377 return(chan_pair_config_string(in
, out
, str
, size
, error_out
));
382 struct chan_ops
*ops
;
385 struct chan_type chan_table
[] = {
386 #ifdef CONFIG_FD_CHAN
389 { "fd", ¬_configged_ops
},
392 #ifdef CONFIG_NULL_CHAN
393 { "null", &null_ops
},
395 { "null", ¬_configged_ops
},
398 #ifdef CONFIG_PORT_CHAN
399 { "port", &port_ops
},
401 { "port", ¬_configged_ops
},
404 #ifdef CONFIG_PTY_CHAN
408 { "pty", ¬_configged_ops
},
409 { "pts", ¬_configged_ops
},
412 #ifdef CONFIG_TTY_CHAN
415 { "tty", ¬_configged_ops
},
418 #ifdef CONFIG_XTERM_CHAN
419 { "xterm", &xterm_ops
},
421 { "xterm", ¬_configged_ops
},
425 static struct chan
*parse_chan(char *str
, int pri
, int device
,
426 struct chan_opts
*opts
)
428 struct chan_type
*entry
;
429 struct chan_ops
*ops
;
436 for(i
= 0; i
< sizeof(chan_table
)/sizeof(chan_table
[0]); i
++){
437 entry
= &chan_table
[i
];
438 if(!strncmp(str
, entry
->key
, strlen(entry
->key
))){
440 str
+= strlen(entry
->key
);
445 printk(KERN_ERR
"parse_chan couldn't parse \"%s\"\n",
449 if(ops
->init
== NULL
) return(NULL
);
450 data
= (*ops
->init
)(str
, device
, opts
);
451 if(data
== NULL
) return(NULL
);
453 chan
= kmalloc(sizeof(*chan
), GFP_KERNEL
);
454 if(chan
== NULL
) return(NULL
);
455 *chan
= ((struct chan
) { .list
= LIST_HEAD_INIT(chan
->list
),
467 int parse_chan_pair(char *str
, struct list_head
*chans
, int pri
, int device
,
468 struct chan_opts
*opts
)
470 struct chan
*new, *chan
;
473 if(!list_empty(chans
)){
474 chan
= list_entry(chans
->next
, struct chan
, list
);
475 if(chan
->pri
>= pri
) return(0);
477 INIT_LIST_HEAD(chans
);
480 out
= strchr(str
, ',');
485 new = parse_chan(in
, pri
, device
, opts
);
486 if(new == NULL
) return(-1);
488 list_add(&new->list
, chans
);
490 new = parse_chan(out
, pri
, device
, opts
);
491 if(new == NULL
) return(-1);
492 list_add(&new->list
, chans
);
496 new = parse_chan(str
, pri
, device
, opts
);
497 if(new == NULL
) return(-1);
498 list_add(&new->list
, chans
);
505 int chan_out_fd(struct list_head
*chans
)
507 struct list_head
*ele
;
510 list_for_each(ele
, chans
){
511 chan
= list_entry(ele
, struct chan
, list
);
512 if(chan
->primary
&& chan
->output
)
518 void chan_interrupt(struct list_head
*chans
, struct work_struct
*task
,
519 struct tty_struct
*tty
, int irq
, void *dev
)
521 struct list_head
*ele
, *next
;
526 list_for_each_safe(ele
, next
, chans
){
527 chan
= list_entry(ele
, struct chan
, list
);
528 if(!chan
->input
|| (chan
->ops
->read
== NULL
)) continue;
531 (tty
->flip
.count
>= TTY_FLIPBUF_SIZE
)){
535 err
= chan
->ops
->read(chan
->fd
, &c
, chan
->data
);
537 tty_receive_char(tty
, c
);
540 if(err
== 0) reactivate_fd(chan
->fd
, irq
);
545 line_disable(dev
, irq
);
551 if(chan
->ops
->close
!= NULL
)
552 chan
->ops
->close(chan
->fd
, chan
->data
);
558 if(tty
) tty_flip_buffer_push(tty
);
562 * Overrides for Emacs so that we follow Linus's tabbing style.
563 * Emacs will notice this stuff at the end of the file and automatically
564 * adjust the settings for this buffer only. This must remain at the end
566 * ---------------------------------------------------------------------------
568 * c-file-style: "linux"