2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
3 * Licensed under the GPL
6 #include <linux/slab.h>
8 #include <linux/tty_flip.h>
12 #ifdef CONFIG_NOCONFIG_CHAN
13 static void *not_configged_init(char *str
, int device
,
14 const struct chan_opts
*opts
)
16 printk(KERN_ERR
"Using a channel type which is configured out of "
21 static int not_configged_open(int input
, int output
, int primary
, void *data
,
24 printk(KERN_ERR
"Using a channel type which is configured out of "
29 static void not_configged_close(int fd
, void *data
)
31 printk(KERN_ERR
"Using a channel type which is configured out of "
35 static int not_configged_read(int fd
, char *c_out
, void *data
)
37 printk(KERN_ERR
"Using a channel type which is configured out of "
42 static int not_configged_write(int fd
, const char *buf
, int len
, void *data
)
44 printk(KERN_ERR
"Using a channel type which is configured out of "
49 static int not_configged_console_write(int fd
, const char *buf
, int len
)
51 printk(KERN_ERR
"Using a channel type which is configured out of "
56 static int not_configged_window_size(int fd
, void *data
, unsigned short *rows
,
59 printk(KERN_ERR
"Using a channel type which is configured out of "
64 static void not_configged_free(void *data
)
66 printk(KERN_ERR
"Using a channel type which is configured out of "
70 static const struct chan_ops not_configged_ops
= {
71 .init
= not_configged_init
,
72 .open
= not_configged_open
,
73 .close
= not_configged_close
,
74 .read
= not_configged_read
,
75 .write
= not_configged_write
,
76 .console_write
= not_configged_console_write
,
77 .window_size
= not_configged_window_size
,
78 .free
= not_configged_free
,
81 #endif /* CONFIG_NOCONFIG_CHAN */
83 static void tty_receive_char(struct tty_struct
*tty
, char ch
)
88 if (I_IXON(tty
) && !I_IXOFF(tty
) && !tty
->raw
) {
89 if (ch
== STOP_CHAR(tty
)) {
93 else if (ch
== START_CHAR(tty
)) {
99 tty_insert_flip_char(tty
, ch
, TTY_NORMAL
);
102 static int open_one_chan(struct chan
*chan
)
109 if (chan
->ops
->open
== NULL
)
111 else fd
= (*chan
->ops
->open
)(chan
->input
, chan
->output
, chan
->primary
,
112 chan
->data
, &chan
->dev
);
116 err
= os_set_fd_block(fd
, 0);
118 (*chan
->ops
->close
)(fd
, chan
->data
);
128 int open_chan(struct list_head
*chans
)
130 struct list_head
*ele
;
134 list_for_each(ele
, chans
) {
135 chan
= list_entry(ele
, struct chan
, list
);
136 ret
= open_one_chan(chan
);
143 void chan_enable_winch(struct list_head
*chans
, struct tty_struct
*tty
)
145 struct list_head
*ele
;
148 list_for_each(ele
, chans
) {
149 chan
= list_entry(ele
, struct chan
, list
);
150 if (chan
->primary
&& chan
->output
&& chan
->ops
->winch
) {
151 register_winch(chan
->fd
, tty
);
157 int enable_chan(struct line
*line
)
159 struct list_head
*ele
;
163 list_for_each(ele
, &line
->chan_list
) {
164 chan
= list_entry(ele
, struct chan
, list
);
165 err
= open_one_chan(chan
);
175 err
= line_setup_irq(chan
->fd
, chan
->input
, chan
->output
, line
,
186 close_chan(&line
->chan_list
, 0);
190 /* Items are added in IRQ context, when free_irq can't be called, and
191 * removed in process context, when it can.
192 * This handles interrupt sources which disappear, and which need to
193 * be permanently disabled. This is discovered in IRQ context, but
194 * the freeing of the IRQ must be done later.
196 static DEFINE_SPINLOCK(irqs_to_free_lock
);
197 static LIST_HEAD(irqs_to_free
);
203 struct list_head
*ele
;
206 spin_lock_irqsave(&irqs_to_free_lock
, flags
);
207 list_splice_init(&irqs_to_free
, &list
);
208 spin_unlock_irqrestore(&irqs_to_free_lock
, flags
);
210 list_for_each(ele
, &list
) {
211 chan
= list_entry(ele
, struct chan
, free_list
);
214 free_irq(chan
->line
->driver
->read_irq
, chan
);
216 free_irq(chan
->line
->driver
->write_irq
, chan
);
221 static void close_one_chan(struct chan
*chan
, int delay_free_irq
)
228 if (delay_free_irq
) {
229 spin_lock_irqsave(&irqs_to_free_lock
, flags
);
230 list_add(&chan
->free_list
, &irqs_to_free
);
231 spin_unlock_irqrestore(&irqs_to_free_lock
, flags
);
235 free_irq(chan
->line
->driver
->read_irq
, chan
);
237 free_irq(chan
->line
->driver
->write_irq
, chan
);
240 if (chan
->ops
->close
!= NULL
)
241 (*chan
->ops
->close
)(chan
->fd
, chan
->data
);
247 void close_chan(struct list_head
*chans
, int delay_free_irq
)
251 /* Close in reverse order as open in case more than one of them
252 * refers to the same device and they save and restore that device's
253 * state. Then, the first one opened will have the original state,
254 * so it must be the last closed.
256 list_for_each_entry_reverse(chan
, chans
, list
) {
257 close_one_chan(chan
, delay_free_irq
);
261 void deactivate_chan(struct list_head
*chans
, int irq
)
263 struct list_head
*ele
;
266 list_for_each(ele
, chans
) {
267 chan
= list_entry(ele
, struct chan
, list
);
269 if (chan
->enabled
&& chan
->input
)
270 deactivate_fd(chan
->fd
, irq
);
274 void reactivate_chan(struct list_head
*chans
, int irq
)
276 struct list_head
*ele
;
279 list_for_each(ele
, chans
) {
280 chan
= list_entry(ele
, struct chan
, list
);
282 if (chan
->enabled
&& chan
->input
)
283 reactivate_fd(chan
->fd
, irq
);
287 int write_chan(struct list_head
*chans
, const char *buf
, int len
,
290 struct list_head
*ele
;
291 struct chan
*chan
= NULL
;
294 list_for_each(ele
, chans
) {
295 chan
= list_entry(ele
, struct chan
, list
);
296 if (!chan
->output
|| (chan
->ops
->write
== NULL
))
299 n
= chan
->ops
->write(chan
->fd
, buf
, len
, chan
->data
);
302 if ((ret
== -EAGAIN
) || ((ret
>= 0) && (ret
< len
)))
303 reactivate_fd(chan
->fd
, write_irq
);
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
))
320 n
= chan
->ops
->console_write(chan
->fd
, buf
, len
);
327 int console_open_chan(struct line
*line
, struct console
*co
)
331 err
= open_chan(&line
->chan_list
);
335 printk(KERN_INFO
"Console initialized on /dev/%s%d\n", co
->name
,
340 int chan_window_size(struct list_head
*chans
, unsigned short *rows_out
,
341 unsigned short *cols_out
)
343 struct list_head
*ele
;
346 list_for_each(ele
, chans
) {
347 chan
= list_entry(ele
, struct chan
, list
);
349 if (chan
->ops
->window_size
== NULL
)
351 return chan
->ops
->window_size(chan
->fd
, chan
->data
,
358 static void free_one_chan(struct chan
*chan
, int delay_free_irq
)
360 list_del(&chan
->list
);
362 close_one_chan(chan
, delay_free_irq
);
364 if (chan
->ops
->free
!= NULL
)
365 (*chan
->ops
->free
)(chan
->data
);
367 if (chan
->primary
&& chan
->output
)
368 ignore_sigio_fd(chan
->fd
);
372 static void free_chan(struct list_head
*chans
, int delay_free_irq
)
374 struct list_head
*ele
, *next
;
377 list_for_each_safe(ele
, next
, chans
) {
378 chan
= list_entry(ele
, struct chan
, list
);
379 free_one_chan(chan
, delay_free_irq
);
383 static int one_chan_config_string(struct chan
*chan
, char *str
, int size
,
389 CONFIG_CHUNK(str
, size
, n
, "none", 1);
393 CONFIG_CHUNK(str
, size
, n
, chan
->ops
->type
, 0);
395 if (chan
->dev
== NULL
) {
396 CONFIG_CHUNK(str
, size
, n
, "", 1);
400 CONFIG_CHUNK(str
, size
, n
, ":", 0);
401 CONFIG_CHUNK(str
, size
, n
, chan
->dev
, 0);
406 static int chan_pair_config_string(struct chan
*in
, struct chan
*out
,
407 char *str
, int size
, char **error_out
)
411 n
= one_chan_config_string(in
, str
, size
, error_out
);
416 CONFIG_CHUNK(str
, size
, n
, "", 1);
420 CONFIG_CHUNK(str
, size
, n
, ",", 1);
421 n
= one_chan_config_string(out
, str
, size
, error_out
);
424 CONFIG_CHUNK(str
, size
, n
, "", 1);
429 int chan_config_string(struct list_head
*chans
, char *str
, int size
,
432 struct list_head
*ele
;
433 struct chan
*chan
, *in
= NULL
, *out
= NULL
;
435 list_for_each(ele
, chans
) {
436 chan
= list_entry(ele
, struct chan
, list
);
445 return chan_pair_config_string(in
, out
, str
, size
, error_out
);
450 const struct chan_ops
*ops
;
453 static const struct chan_type chan_table
[] = {
456 #ifdef CONFIG_NULL_CHAN
457 { "null", &null_ops
},
459 { "null", ¬_configged_ops
},
462 #ifdef CONFIG_PORT_CHAN
463 { "port", &port_ops
},
465 { "port", ¬_configged_ops
},
468 #ifdef CONFIG_PTY_CHAN
472 { "pty", ¬_configged_ops
},
473 { "pts", ¬_configged_ops
},
476 #ifdef CONFIG_TTY_CHAN
479 { "tty", ¬_configged_ops
},
482 #ifdef CONFIG_XTERM_CHAN
483 { "xterm", &xterm_ops
},
485 { "xterm", ¬_configged_ops
},
489 static struct chan
*parse_chan(struct line
*line
, char *str
, int device
,
490 const struct chan_opts
*opts
, char **error_out
)
492 const struct chan_type
*entry
;
493 const struct chan_ops
*ops
;
500 for(i
= 0; i
< ARRAY_SIZE(chan_table
); i
++) {
501 entry
= &chan_table
[i
];
502 if (!strncmp(str
, entry
->key
, strlen(entry
->key
))) {
504 str
+= strlen(entry
->key
);
509 *error_out
= "No match for configured backends";
513 data
= (*ops
->init
)(str
, device
, opts
);
515 *error_out
= "Configuration failed";
519 chan
= kmalloc(sizeof(*chan
), GFP_ATOMIC
);
521 *error_out
= "Memory allocation failed";
524 *chan
= ((struct chan
) { .list
= LIST_HEAD_INIT(chan
->list
),
526 LIST_HEAD_INIT(chan
->free_list
),
539 int parse_chan_pair(char *str
, struct line
*line
, int device
,
540 const struct chan_opts
*opts
, char **error_out
)
542 struct list_head
*chans
= &line
->chan_list
;
543 struct chan
*new, *chan
;
546 if (!list_empty(chans
)) {
547 chan
= list_entry(chans
->next
, struct chan
, list
);
549 INIT_LIST_HEAD(chans
);
552 out
= strchr(str
, ',');
557 new = parse_chan(line
, in
, device
, opts
, error_out
);
562 list_add(&new->list
, chans
);
564 new = parse_chan(line
, out
, device
, opts
, error_out
);
568 list_add(&new->list
, chans
);
572 new = parse_chan(line
, str
, device
, opts
, error_out
);
576 list_add(&new->list
, chans
);
583 int chan_out_fd(struct list_head
*chans
)
585 struct list_head
*ele
;
588 list_for_each(ele
, chans
) {
589 chan
= list_entry(ele
, struct chan
, list
);
590 if (chan
->primary
&& chan
->output
)
596 void chan_interrupt(struct list_head
*chans
, struct delayed_work
*task
,
597 struct tty_struct
*tty
, int irq
)
599 struct list_head
*ele
, *next
;
604 list_for_each_safe(ele
, next
, chans
) {
605 chan
= list_entry(ele
, struct chan
, list
);
606 if (!chan
->input
|| (chan
->ops
->read
== NULL
))
609 if (tty
&& !tty_buffer_request_room(tty
, 1)) {
610 schedule_delayed_work(task
, 1);
613 err
= chan
->ops
->read(chan
->fd
, &c
, chan
->data
);
615 tty_receive_char(tty
, c
);
619 reactivate_fd(chan
->fd
, irq
);
624 close_chan(chans
, 1);
627 else close_one_chan(chan
, 1);
632 tty_flip_buffer_push(tty
);