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 printf(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 printf(KERN_ERR
"Using a channel type which is configured out of "
38 static void not_configged_close(int fd
, void *data
)
40 printf(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 printf(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 printf(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 printf(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 printf(KERN_ERR
"Using a channel type which is configured out of "
74 static void not_configged_free(void *data
)
76 printf(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
, struct tty_struct
*tty
)
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
, tty
);
206 void enable_chan(struct list_head
*chans
, struct tty_struct
*tty
)
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
, tty
);
219 void close_chan(struct list_head
*chans
)
223 /* Close in reverse order as open in case more than one of them
224 * refers to the same device and they save and restore that device's
225 * state. Then, the first one opened will have the original state,
226 * so it must be the last closed.
228 list_for_each_entry_reverse(chan
, chans
, 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
;
241 struct chan
*chan
= NULL
;
244 list_for_each(ele
, chans
) {
245 chan
= list_entry(ele
, struct chan
, list
);
246 if (!chan
->output
|| (chan
->ops
->write
== NULL
))
248 n
= chan
->ops
->write(chan
->fd
, buf
, len
, chan
->data
);
251 if ((ret
== -EAGAIN
) || ((ret
>= 0) && (ret
< len
)))
252 reactivate_fd(chan
->fd
, write_irq
);
258 int console_write_chan(struct list_head
*chans
, const char *buf
, int len
)
260 struct list_head
*ele
;
264 list_for_each(ele
, chans
){
265 chan
= list_entry(ele
, struct chan
, list
);
266 if(!chan
->output
|| (chan
->ops
->console_write
== NULL
))
268 n
= chan
->ops
->console_write(chan
->fd
, buf
, len
, chan
->data
);
269 if(chan
->primary
) ret
= n
;
274 int console_open_chan(struct line
*line
, struct console
*co
, struct chan_opts
*opts
)
276 if (!list_empty(&line
->chan_list
))
279 if (0 != parse_chan_pair(line
->init_str
, &line
->chan_list
,
280 line
->init_pri
, co
->index
, opts
))
282 if (0 != open_chan(&line
->chan_list
))
284 printk("Console initialized on /dev/%s%d\n",co
->name
,co
->index
);
288 int chan_window_size(struct list_head
*chans
, unsigned short *rows_out
,
289 unsigned short *cols_out
)
291 struct list_head
*ele
;
294 list_for_each(ele
, chans
){
295 chan
= list_entry(ele
, struct chan
, list
);
297 if(chan
->ops
->window_size
== NULL
) return(0);
298 return(chan
->ops
->window_size(chan
->fd
, chan
->data
,
299 rows_out
, cols_out
));
305 void free_one_chan(struct chan
*chan
)
307 list_del(&chan
->list
);
308 if(chan
->ops
->free
!= NULL
)
309 (*chan
->ops
->free
)(chan
->data
);
310 free_irq_by_fd(chan
->fd
);
311 if(chan
->primary
&& chan
->output
) ignore_sigio_fd(chan
->fd
);
315 void free_chan(struct list_head
*chans
)
317 struct list_head
*ele
, *next
;
320 list_for_each_safe(ele
, next
, chans
){
321 chan
= list_entry(ele
, struct chan
, list
);
326 static int one_chan_config_string(struct chan
*chan
, char *str
, int size
,
332 CONFIG_CHUNK(str
, size
, n
, "none", 1);
336 CONFIG_CHUNK(str
, size
, n
, chan
->ops
->type
, 0);
338 if(chan
->dev
== NULL
){
339 CONFIG_CHUNK(str
, size
, n
, "", 1);
343 CONFIG_CHUNK(str
, size
, n
, ":", 0);
344 CONFIG_CHUNK(str
, size
, n
, chan
->dev
, 0);
349 static int chan_pair_config_string(struct chan
*in
, struct chan
*out
,
350 char *str
, int size
, char **error_out
)
354 n
= one_chan_config_string(in
, str
, size
, error_out
);
359 CONFIG_CHUNK(str
, size
, n
, "", 1);
363 CONFIG_CHUNK(str
, size
, n
, ",", 1);
364 n
= one_chan_config_string(out
, str
, size
, error_out
);
367 CONFIG_CHUNK(str
, size
, n
, "", 1);
372 int chan_config_string(struct list_head
*chans
, char *str
, int size
,
375 struct list_head
*ele
;
376 struct chan
*chan
, *in
= NULL
, *out
= NULL
;
378 list_for_each(ele
, chans
){
379 chan
= list_entry(ele
, struct chan
, list
);
388 return(chan_pair_config_string(in
, out
, str
, size
, error_out
));
393 struct chan_ops
*ops
;
396 struct chan_type chan_table
[] = {
399 #ifdef CONFIG_NULL_CHAN
400 { "null", &null_ops
},
402 { "null", ¬_configged_ops
},
405 #ifdef CONFIG_PORT_CHAN
406 { "port", &port_ops
},
408 { "port", ¬_configged_ops
},
411 #ifdef CONFIG_PTY_CHAN
415 { "pty", ¬_configged_ops
},
416 { "pts", ¬_configged_ops
},
419 #ifdef CONFIG_TTY_CHAN
422 { "tty", ¬_configged_ops
},
425 #ifdef CONFIG_XTERM_CHAN
426 { "xterm", &xterm_ops
},
428 { "xterm", ¬_configged_ops
},
432 static struct chan
*parse_chan(char *str
, int pri
, int device
,
433 struct chan_opts
*opts
)
435 struct chan_type
*entry
;
436 struct chan_ops
*ops
;
443 for(i
= 0; i
< sizeof(chan_table
)/sizeof(chan_table
[0]); i
++){
444 entry
= &chan_table
[i
];
445 if(!strncmp(str
, entry
->key
, strlen(entry
->key
))){
447 str
+= strlen(entry
->key
);
452 printk(KERN_ERR
"parse_chan couldn't parse \"%s\"\n",
456 if(ops
->init
== NULL
) return(NULL
);
457 data
= (*ops
->init
)(str
, device
, opts
);
458 if(data
== NULL
) return(NULL
);
460 chan
= kmalloc(sizeof(*chan
), GFP_KERNEL
);
461 if(chan
== NULL
) return(NULL
);
462 *chan
= ((struct chan
) { .list
= LIST_HEAD_INIT(chan
->list
),
474 int parse_chan_pair(char *str
, struct list_head
*chans
, int pri
, int device
,
475 struct chan_opts
*opts
)
477 struct chan
*new, *chan
;
480 if(!list_empty(chans
)){
481 chan
= list_entry(chans
->next
, struct chan
, list
);
482 if(chan
->pri
>= pri
) return(0);
484 INIT_LIST_HEAD(chans
);
487 out
= strchr(str
, ',');
492 new = parse_chan(in
, pri
, device
, opts
);
493 if(new == NULL
) return(-1);
495 list_add(&new->list
, chans
);
497 new = parse_chan(out
, pri
, device
, opts
);
498 if(new == NULL
) return(-1);
499 list_add(&new->list
, chans
);
503 new = parse_chan(str
, pri
, device
, opts
);
504 if(new == NULL
) return(-1);
505 list_add(&new->list
, chans
);
512 int chan_out_fd(struct list_head
*chans
)
514 struct list_head
*ele
;
517 list_for_each(ele
, chans
){
518 chan
= list_entry(ele
, struct chan
, list
);
519 if(chan
->primary
&& chan
->output
)
525 void chan_interrupt(struct list_head
*chans
, struct work_struct
*task
,
526 struct tty_struct
*tty
, int irq
)
528 struct list_head
*ele
, *next
;
533 list_for_each_safe(ele
, next
, chans
){
534 chan
= list_entry(ele
, struct chan
, list
);
535 if(!chan
->input
|| (chan
->ops
->read
== NULL
)) continue;
538 (tty
->flip
.count
>= TTY_FLIPBUF_SIZE
)){
542 err
= chan
->ops
->read(chan
->fd
, &c
, chan
->data
);
544 tty_receive_char(tty
, c
);
547 if(err
== 0) reactivate_fd(chan
->fd
, irq
);
552 line_disable(tty
, irq
);
558 if(chan
->ops
->close
!= NULL
)
559 chan
->ops
->close(chan
->fd
, chan
->data
);
565 if(tty
) tty_flip_buffer_push(tty
);
569 * Overrides for Emacs so that we follow Linus's tabbing style.
570 * Emacs will notice this stuff at the end of the file and automatically
571 * adjust the settings for this buffer only. This must remain at the end
573 * ---------------------------------------------------------------------------
575 * c-file-style: "linux"