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
24 /* The printk's here are wrong because we are complaining that there is no
25 * output device, but printk is printing to that output device. The user will
26 * never see the error. printf would be better, except it can't run on a
27 * kernel stack because it will overflow it.
28 * Use printk for now since that will avoid crashing.
31 static void *not_configged_init(char *str
, int device
, struct chan_opts
*opts
)
33 printk(KERN_ERR
"Using a channel type which is configured out of "
38 static int not_configged_open(int input
, int output
, int primary
, void *data
,
41 printk(KERN_ERR
"Using a channel type which is configured out of "
46 static void not_configged_close(int fd
, void *data
)
48 printk(KERN_ERR
"Using a channel type which is configured out of "
52 static int not_configged_read(int fd
, char *c_out
, void *data
)
54 printk(KERN_ERR
"Using a channel type which is configured out of "
59 static int not_configged_write(int fd
, const char *buf
, int len
, void *data
)
61 printk(KERN_ERR
"Using a channel type which is configured out of "
66 static int not_configged_console_write(int fd
, const char *buf
, int len
,
69 printk(KERN_ERR
"Using a channel type which is configured out of "
74 static int not_configged_window_size(int fd
, void *data
, unsigned short *rows
,
77 printk(KERN_ERR
"Using a channel type which is configured out of "
82 static void not_configged_free(void *data
)
84 printf(KERN_ERR
"Using a channel type which is configured out of "
88 static struct chan_ops not_configged_ops
= {
89 .init
= not_configged_init
,
90 .open
= not_configged_open
,
91 .close
= not_configged_close
,
92 .read
= not_configged_read
,
93 .write
= not_configged_write
,
94 .console_write
= not_configged_console_write
,
95 .window_size
= not_configged_window_size
,
96 .free
= not_configged_free
,
99 #endif /* CONFIG_NOCONFIG_CHAN */
101 void generic_close(int fd
, void *unused
)
106 int generic_read(int fd
, char *c_out
, void *unused
)
110 n
= os_read_file(fd
, c_out
, sizeof(*c_out
));
119 /* XXX Trivial wrapper around os_write_file */
121 int generic_write(int fd
, const char *buf
, int n
, void *unused
)
123 return(os_write_file(fd
, buf
, n
));
126 int generic_window_size(int fd
, void *unused
, unsigned short *rows_out
,
127 unsigned short *cols_out
)
132 ret
= os_window_size(fd
, &rows
, &cols
);
136 ret
= ((*rows_out
!= rows
) || (*cols_out
!= cols
));
144 void generic_free(void *data
)
149 static void tty_receive_char(struct tty_struct
*tty
, char ch
)
151 if(tty
== NULL
) return;
153 if(I_IXON(tty
) && !I_IXOFF(tty
) && !tty
->raw
) {
154 if(ch
== STOP_CHAR(tty
)){
158 else if(ch
== START_CHAR(tty
)){
164 if((tty
->flip
.flag_buf_ptr
== NULL
) ||
165 (tty
->flip
.char_buf_ptr
== NULL
))
167 tty_insert_flip_char(tty
, ch
, TTY_NORMAL
);
170 static int open_one_chan(struct chan
*chan
, int input
, int output
, int primary
)
174 if(chan
->opened
) return(0);
175 if(chan
->ops
->open
== NULL
) fd
= 0;
176 else fd
= (*chan
->ops
->open
)(input
, output
, primary
, chan
->data
,
178 if(fd
< 0) return(fd
);
185 int open_chan(struct list_head
*chans
)
187 struct list_head
*ele
;
191 list_for_each(ele
, chans
){
192 chan
= list_entry(ele
, struct chan
, list
);
193 ret
= open_one_chan(chan
, chan
->input
, chan
->output
,
195 if(chan
->primary
) err
= ret
;
200 void chan_enable_winch(struct list_head
*chans
, struct tty_struct
*tty
)
202 struct list_head
*ele
;
205 list_for_each(ele
, chans
){
206 chan
= list_entry(ele
, struct chan
, list
);
207 if(chan
->primary
&& chan
->output
&& chan
->ops
->winch
){
208 register_winch(chan
->fd
, tty
);
214 void enable_chan(struct list_head
*chans
, struct tty_struct
*tty
)
216 struct list_head
*ele
;
219 list_for_each(ele
, chans
){
220 chan
= list_entry(ele
, struct chan
, list
);
221 if(!chan
->opened
) continue;
223 line_setup_irq(chan
->fd
, chan
->input
, chan
->output
, tty
);
227 void close_chan(struct list_head
*chans
)
231 /* Close in reverse order as open in case more than one of them
232 * refers to the same device and they save and restore that device's
233 * state. Then, the first one opened will have the original state,
234 * so it must be the last closed.
236 list_for_each_entry_reverse(chan
, chans
, list
) {
237 if(!chan
->opened
) continue;
238 if(chan
->ops
->close
!= NULL
)
239 (*chan
->ops
->close
)(chan
->fd
, chan
->data
);
245 int write_chan(struct list_head
*chans
, const char *buf
, int len
,
248 struct list_head
*ele
;
249 struct chan
*chan
= NULL
;
252 list_for_each(ele
, chans
) {
253 chan
= list_entry(ele
, struct chan
, list
);
254 if (!chan
->output
|| (chan
->ops
->write
== NULL
))
256 n
= chan
->ops
->write(chan
->fd
, buf
, len
, chan
->data
);
259 if ((ret
== -EAGAIN
) || ((ret
>= 0) && (ret
< len
)))
260 reactivate_fd(chan
->fd
, write_irq
);
266 int console_write_chan(struct list_head
*chans
, const char *buf
, int len
)
268 struct list_head
*ele
;
272 list_for_each(ele
, chans
){
273 chan
= list_entry(ele
, struct chan
, list
);
274 if(!chan
->output
|| (chan
->ops
->console_write
== NULL
))
276 n
= chan
->ops
->console_write(chan
->fd
, buf
, len
, chan
->data
);
277 if(chan
->primary
) ret
= n
;
282 int console_open_chan(struct line
*line
, struct console
*co
, struct chan_opts
*opts
)
284 if (!list_empty(&line
->chan_list
))
287 if (0 != parse_chan_pair(line
->init_str
, &line
->chan_list
,
288 line
->init_pri
, co
->index
, opts
))
290 if (0 != open_chan(&line
->chan_list
))
292 printk("Console initialized on /dev/%s%d\n",co
->name
,co
->index
);
296 int chan_window_size(struct list_head
*chans
, unsigned short *rows_out
,
297 unsigned short *cols_out
)
299 struct list_head
*ele
;
302 list_for_each(ele
, chans
){
303 chan
= list_entry(ele
, struct chan
, list
);
305 if(chan
->ops
->window_size
== NULL
) return(0);
306 return(chan
->ops
->window_size(chan
->fd
, chan
->data
,
307 rows_out
, cols_out
));
313 void free_one_chan(struct chan
*chan
)
315 list_del(&chan
->list
);
316 if(chan
->ops
->free
!= NULL
)
317 (*chan
->ops
->free
)(chan
->data
);
318 free_irq_by_fd(chan
->fd
);
319 if(chan
->primary
&& chan
->output
) ignore_sigio_fd(chan
->fd
);
323 void free_chan(struct list_head
*chans
)
325 struct list_head
*ele
, *next
;
328 list_for_each_safe(ele
, next
, chans
){
329 chan
= list_entry(ele
, struct chan
, list
);
334 static int one_chan_config_string(struct chan
*chan
, char *str
, int size
,
340 CONFIG_CHUNK(str
, size
, n
, "none", 1);
344 CONFIG_CHUNK(str
, size
, n
, chan
->ops
->type
, 0);
346 if(chan
->dev
== NULL
){
347 CONFIG_CHUNK(str
, size
, n
, "", 1);
351 CONFIG_CHUNK(str
, size
, n
, ":", 0);
352 CONFIG_CHUNK(str
, size
, n
, chan
->dev
, 0);
357 static int chan_pair_config_string(struct chan
*in
, struct chan
*out
,
358 char *str
, int size
, char **error_out
)
362 n
= one_chan_config_string(in
, str
, size
, error_out
);
367 CONFIG_CHUNK(str
, size
, n
, "", 1);
371 CONFIG_CHUNK(str
, size
, n
, ",", 1);
372 n
= one_chan_config_string(out
, str
, size
, error_out
);
375 CONFIG_CHUNK(str
, size
, n
, "", 1);
380 int chan_config_string(struct list_head
*chans
, char *str
, int size
,
383 struct list_head
*ele
;
384 struct chan
*chan
, *in
= NULL
, *out
= NULL
;
386 list_for_each(ele
, chans
){
387 chan
= list_entry(ele
, struct chan
, list
);
396 return(chan_pair_config_string(in
, out
, str
, size
, error_out
));
401 struct chan_ops
*ops
;
404 struct chan_type chan_table
[] = {
407 #ifdef CONFIG_NULL_CHAN
408 { "null", &null_ops
},
410 { "null", ¬_configged_ops
},
413 #ifdef CONFIG_PORT_CHAN
414 { "port", &port_ops
},
416 { "port", ¬_configged_ops
},
419 #ifdef CONFIG_PTY_CHAN
423 { "pty", ¬_configged_ops
},
424 { "pts", ¬_configged_ops
},
427 #ifdef CONFIG_TTY_CHAN
430 { "tty", ¬_configged_ops
},
433 #ifdef CONFIG_XTERM_CHAN
434 { "xterm", &xterm_ops
},
436 { "xterm", ¬_configged_ops
},
440 static struct chan
*parse_chan(char *str
, int pri
, int device
,
441 struct chan_opts
*opts
)
443 struct chan_type
*entry
;
444 struct chan_ops
*ops
;
451 for(i
= 0; i
< sizeof(chan_table
)/sizeof(chan_table
[0]); i
++){
452 entry
= &chan_table
[i
];
453 if(!strncmp(str
, entry
->key
, strlen(entry
->key
))){
455 str
+= strlen(entry
->key
);
460 printk(KERN_ERR
"parse_chan couldn't parse \"%s\"\n",
464 if(ops
->init
== NULL
) return(NULL
);
465 data
= (*ops
->init
)(str
, device
, opts
);
466 if(data
== NULL
) return(NULL
);
468 chan
= kmalloc(sizeof(*chan
), GFP_KERNEL
);
469 if(chan
== NULL
) return(NULL
);
470 *chan
= ((struct chan
) { .list
= LIST_HEAD_INIT(chan
->list
),
482 int parse_chan_pair(char *str
, struct list_head
*chans
, int pri
, int device
,
483 struct chan_opts
*opts
)
485 struct chan
*new, *chan
;
488 if(!list_empty(chans
)){
489 chan
= list_entry(chans
->next
, struct chan
, list
);
490 if(chan
->pri
>= pri
) return(0);
492 INIT_LIST_HEAD(chans
);
495 out
= strchr(str
, ',');
500 new = parse_chan(in
, pri
, device
, opts
);
501 if(new == NULL
) return(-1);
503 list_add(&new->list
, chans
);
505 new = parse_chan(out
, pri
, device
, opts
);
506 if(new == NULL
) return(-1);
507 list_add(&new->list
, chans
);
511 new = parse_chan(str
, pri
, device
, opts
);
512 if(new == NULL
) return(-1);
513 list_add(&new->list
, chans
);
520 int chan_out_fd(struct list_head
*chans
)
522 struct list_head
*ele
;
525 list_for_each(ele
, chans
){
526 chan
= list_entry(ele
, struct chan
, list
);
527 if(chan
->primary
&& chan
->output
)
533 void chan_interrupt(struct list_head
*chans
, struct work_struct
*task
,
534 struct tty_struct
*tty
, int irq
)
536 struct list_head
*ele
, *next
;
541 list_for_each_safe(ele
, next
, chans
){
542 chan
= list_entry(ele
, struct chan
, list
);
543 if(!chan
->input
|| (chan
->ops
->read
== NULL
)) continue;
546 (tty
->flip
.count
>= TTY_FLIPBUF_SIZE
)){
550 err
= chan
->ops
->read(chan
->fd
, &c
, chan
->data
);
552 tty_receive_char(tty
, c
);
555 if(err
== 0) reactivate_fd(chan
->fd
, irq
);
560 line_disable(tty
, irq
);
566 if(chan
->ops
->close
!= NULL
)
567 chan
->ops
->close(chan
->fd
, chan
->data
);
573 if(tty
) tty_flip_buffer_push(tty
);
577 * Overrides for Emacs so that we follow Linus's tabbing style.
578 * Emacs will notice this stuff at the end of the file and automatically
579 * adjust the settings for this buffer only. This must remain at the end
581 * ---------------------------------------------------------------------------
583 * c-file-style: "linux"