blob: c09dbdfa298a4c5f56fa4a091ed54034909996cf [file] [log] [blame]
Jeff Dike165dc592006-01-06 00:18:57 -08001/*
Jeff Dikee99525f2007-10-16 01:26:41 -07002 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/slab.h>
7#include <linux/tty.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include "chan_kern.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "os.h"
11
Paolo 'Blaisorblade' Giarrussofac97ae2005-09-22 21:44:22 -070012#ifdef CONFIG_NOCONFIG_CHAN
Jeff Dikef28169d2007-02-10 01:43:53 -080013static void *not_configged_init(char *str, int device,
14 const struct chan_opts *opts)
Paolo 'Blaisorblade' Giarrussofac97ae2005-09-22 21:44:22 -070015{
Jeff Dikee99525f2007-10-16 01:26:41 -070016 printk(KERN_ERR "Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 "UML\n");
Jeff Diked50084a2006-01-06 00:18:50 -080018 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070019}
20
21static int not_configged_open(int input, int output, int primary, void *data,
22 char **dev_out)
23{
Jeff Dikee99525f2007-10-16 01:26:41 -070024 printk(KERN_ERR "Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 "UML\n");
Jeff Diked50084a2006-01-06 00:18:50 -080026 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027}
28
29static void not_configged_close(int fd, void *data)
30{
Jeff Dikee99525f2007-10-16 01:26:41 -070031 printk(KERN_ERR "Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 "UML\n");
33}
34
35static int not_configged_read(int fd, char *c_out, void *data)
36{
Jeff Dikee99525f2007-10-16 01:26:41 -070037 printk(KERN_ERR "Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 "UML\n");
Jeff Diked50084a2006-01-06 00:18:50 -080039 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
42static int not_configged_write(int fd, const char *buf, int len, void *data)
43{
Jeff Dikee99525f2007-10-16 01:26:41 -070044 printk(KERN_ERR "Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 "UML\n");
Jeff Diked50084a2006-01-06 00:18:50 -080046 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
Paolo 'Blaisorblade' Giarrusso55c033c2005-11-13 16:07:11 -080049static int not_configged_console_write(int fd, const char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Jeff Dikee99525f2007-10-16 01:26:41 -070051 printk(KERN_ERR "Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 "UML\n");
Jeff Diked50084a2006-01-06 00:18:50 -080053 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
56static int not_configged_window_size(int fd, void *data, unsigned short *rows,
57 unsigned short *cols)
58{
Jeff Dikee99525f2007-10-16 01:26:41 -070059 printk(KERN_ERR "Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 "UML\n");
Jeff Diked50084a2006-01-06 00:18:50 -080061 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
64static void not_configged_free(void *data)
65{
Jeff Dikee99525f2007-10-16 01:26:41 -070066 printk(KERN_ERR "Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 "UML\n");
68}
69
Jeff Dike5e7672e2006-09-27 01:50:33 -070070static const struct chan_ops not_configged_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 .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,
79 .winch = 0,
80};
81#endif /* CONFIG_NOCONFIG_CHAN */
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static void tty_receive_char(struct tty_struct *tty, char ch)
84{
Jeff Dikee99525f2007-10-16 01:26:41 -070085 if (tty == NULL)
86 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Jeff Dikee99525f2007-10-16 01:26:41 -070088 if (I_IXON(tty) && !I_IXOFF(tty) && !tty->raw) {
89 if (ch == STOP_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 stop_tty(tty);
91 return;
92 }
Jeff Dikee99525f2007-10-16 01:26:41 -070093 else if (ch == START_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 start_tty(tty);
95 return;
96 }
97 }
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 tty_insert_flip_char(tty, ch, TTY_NORMAL);
100}
101
Jeff Diked50084a2006-01-06 00:18:50 -0800102static int open_one_chan(struct chan *chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Jeff Dike6676ae62007-07-31 00:37:44 -0700104 int fd, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Jeff Dikee99525f2007-10-16 01:26:41 -0700106 if (chan->opened)
Jeff Diked50084a2006-01-06 00:18:50 -0800107 return 0;
108
Jeff Dikee99525f2007-10-16 01:26:41 -0700109 if (chan->ops->open == NULL)
Jeff Diked50084a2006-01-06 00:18:50 -0800110 fd = 0;
111 else fd = (*chan->ops->open)(chan->input, chan->output, chan->primary,
112 chan->data, &chan->dev);
Jeff Dikee99525f2007-10-16 01:26:41 -0700113 if (fd < 0)
Jeff Diked50084a2006-01-06 00:18:50 -0800114 return fd;
Jeff Dike6676ae62007-07-31 00:37:44 -0700115
116 err = os_set_fd_block(fd, 0);
117 if (err) {
118 (*chan->ops->close)(fd, chan->data);
119 return err;
120 }
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 chan->fd = fd;
123
124 chan->opened = 1;
Jeff Diked50084a2006-01-06 00:18:50 -0800125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128int open_chan(struct list_head *chans)
129{
130 struct list_head *ele;
131 struct chan *chan;
132 int ret, err = 0;
133
Jeff Dikee99525f2007-10-16 01:26:41 -0700134 list_for_each(ele, chans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 chan = list_entry(ele, struct chan, list);
Jeff Diked50084a2006-01-06 00:18:50 -0800136 ret = open_one_chan(chan);
Jeff Dikee99525f2007-10-16 01:26:41 -0700137 if (chan->primary)
Jeff Diked50084a2006-01-06 00:18:50 -0800138 err = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
Jeff Diked50084a2006-01-06 00:18:50 -0800140 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143void chan_enable_winch(struct list_head *chans, struct tty_struct *tty)
144{
145 struct list_head *ele;
146 struct chan *chan;
147
Jeff Dikee99525f2007-10-16 01:26:41 -0700148 list_for_each(ele, chans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 chan = list_entry(ele, struct chan, list);
Jeff Dikee99525f2007-10-16 01:26:41 -0700150 if (chan->primary && chan->output && chan->ops->winch) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 register_winch(chan->fd, tty);
152 return;
153 }
154 }
155}
156
Jeff Diked14ad812007-07-15 23:38:54 -0700157int enable_chan(struct line *line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 struct list_head *ele;
160 struct chan *chan;
Jeff Diked14ad812007-07-15 23:38:54 -0700161 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Jeff Dikee99525f2007-10-16 01:26:41 -0700163 list_for_each(ele, &line->chan_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 chan = list_entry(ele, struct chan, list);
Jeff Diked14ad812007-07-15 23:38:54 -0700165 err = open_one_chan(chan);
166 if (err) {
167 if (chan->primary)
168 goto out_close;
169
Jeff Dike165dc592006-01-06 00:18:57 -0800170 continue;
Jeff Diked14ad812007-07-15 23:38:54 -0700171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Jeff Dikee99525f2007-10-16 01:26:41 -0700173 if (chan->enabled)
Jeff Dike165dc592006-01-06 00:18:57 -0800174 continue;
Jeff Diked14ad812007-07-15 23:38:54 -0700175 err = line_setup_irq(chan->fd, chan->input, chan->output, line,
176 chan);
177 if (err)
178 goto out_close;
179
Jeff Dike165dc592006-01-06 00:18:57 -0800180 chan->enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
Jeff Diked14ad812007-07-15 23:38:54 -0700182
183 return 0;
184
185 out_close:
186 close_chan(&line->chan_list, 0);
187 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Jeff Dike190c3e42007-02-10 01:43:55 -0800190/* 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.
195 */
196static DEFINE_SPINLOCK(irqs_to_free_lock);
Jeff Dike165dc592006-01-06 00:18:57 -0800197static LIST_HEAD(irqs_to_free);
198
199void free_irqs(void)
200{
201 struct chan *chan;
Jeff Dike190c3e42007-02-10 01:43:55 -0800202 LIST_HEAD(list);
203 struct list_head *ele;
Jeff Dike30762122007-03-29 01:20:30 -0700204 unsigned long flags;
Jeff Dike165dc592006-01-06 00:18:57 -0800205
Jeff Dike30762122007-03-29 01:20:30 -0700206 spin_lock_irqsave(&irqs_to_free_lock, flags);
Jeff Dike190c3e42007-02-10 01:43:55 -0800207 list_splice_init(&irqs_to_free, &list);
Jeff Dike30762122007-03-29 01:20:30 -0700208 spin_unlock_irqrestore(&irqs_to_free_lock, flags);
Jeff Dike190c3e42007-02-10 01:43:55 -0800209
Jeff Dikee99525f2007-10-16 01:26:41 -0700210 list_for_each(ele, &list) {
Jeff Dike190c3e42007-02-10 01:43:55 -0800211 chan = list_entry(ele, struct chan, free_list);
Jeff Dike165dc592006-01-06 00:18:57 -0800212
Jeff Dikee99525f2007-10-16 01:26:41 -0700213 if (chan->input)
Jeff Dike165dc592006-01-06 00:18:57 -0800214 free_irq(chan->line->driver->read_irq, chan);
Jeff Dikee99525f2007-10-16 01:26:41 -0700215 if (chan->output)
Jeff Dike165dc592006-01-06 00:18:57 -0800216 free_irq(chan->line->driver->write_irq, chan);
217 chan->enabled = 0;
218 }
219}
220
221static void close_one_chan(struct chan *chan, int delay_free_irq)
222{
Jeff Dike30762122007-03-29 01:20:30 -0700223 unsigned long flags;
224
Jeff Dikee99525f2007-10-16 01:26:41 -0700225 if (!chan->opened)
Jeff Dike165dc592006-01-06 00:18:57 -0800226 return;
227
Jeff Dikee99525f2007-10-16 01:26:41 -0700228 if (delay_free_irq) {
Jeff Dike30762122007-03-29 01:20:30 -0700229 spin_lock_irqsave(&irqs_to_free_lock, flags);
Jeff Dike165dc592006-01-06 00:18:57 -0800230 list_add(&chan->free_list, &irqs_to_free);
Jeff Dike30762122007-03-29 01:20:30 -0700231 spin_unlock_irqrestore(&irqs_to_free_lock, flags);
Jeff Dike165dc592006-01-06 00:18:57 -0800232 }
233 else {
Jeff Dikee99525f2007-10-16 01:26:41 -0700234 if (chan->input)
Jeff Dike165dc592006-01-06 00:18:57 -0800235 free_irq(chan->line->driver->read_irq, chan);
Jeff Dikee99525f2007-10-16 01:26:41 -0700236 if (chan->output)
Jeff Dike165dc592006-01-06 00:18:57 -0800237 free_irq(chan->line->driver->write_irq, chan);
238 chan->enabled = 0;
239 }
Jeff Dikee99525f2007-10-16 01:26:41 -0700240 if (chan->ops->close != NULL)
Jeff Dike165dc592006-01-06 00:18:57 -0800241 (*chan->ops->close)(chan->fd, chan->data);
242
243 chan->opened = 0;
244 chan->fd = -1;
245}
246
247void close_chan(struct list_head *chans, int delay_free_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct chan *chan;
250
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.
255 */
256 list_for_each_entry_reverse(chan, chans, list) {
Jeff Dike165dc592006-01-06 00:18:57 -0800257 close_one_chan(chan, delay_free_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259}
260
Jeff Dikee4dcee82006-01-06 00:18:58 -0800261void deactivate_chan(struct list_head *chans, int irq)
262{
263 struct list_head *ele;
264
265 struct chan *chan;
266 list_for_each(ele, chans) {
267 chan = list_entry(ele, struct chan, list);
268
Jeff Dikee99525f2007-10-16 01:26:41 -0700269 if (chan->enabled && chan->input)
Jeff Dikee4dcee82006-01-06 00:18:58 -0800270 deactivate_fd(chan->fd, irq);
271 }
272}
273
274void reactivate_chan(struct list_head *chans, int irq)
275{
276 struct list_head *ele;
277 struct chan *chan;
278
279 list_for_each(ele, chans) {
280 chan = list_entry(ele, struct chan, list);
281
Jeff Dikee99525f2007-10-16 01:26:41 -0700282 if (chan->enabled && chan->input)
Jeff Dikee4dcee82006-01-06 00:18:58 -0800283 reactivate_fd(chan->fd, irq);
284 }
285}
286
Jeff Diked50084a2006-01-06 00:18:50 -0800287int write_chan(struct list_head *chans, const char *buf, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 int write_irq)
289{
290 struct list_head *ele;
291 struct chan *chan = NULL;
292 int n, ret = 0;
293
294 list_for_each(ele, chans) {
295 chan = list_entry(ele, struct chan, list);
296 if (!chan->output || (chan->ops->write == NULL))
297 continue;
Jeff Dikee99525f2007-10-16 01:26:41 -0700298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 n = chan->ops->write(chan->fd, buf, len, chan->data);
300 if (chan->primary) {
301 ret = n;
302 if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
303 reactivate_fd(chan->fd, write_irq);
304 }
305 }
Jeff Diked50084a2006-01-06 00:18:50 -0800306 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309int console_write_chan(struct list_head *chans, const char *buf, int len)
310{
311 struct list_head *ele;
312 struct chan *chan;
313 int n, ret = 0;
314
Jeff Dikee99525f2007-10-16 01:26:41 -0700315 list_for_each(ele, chans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 chan = list_entry(ele, struct chan, list);
Jeff Dikee99525f2007-10-16 01:26:41 -0700317 if (!chan->output || (chan->ops->console_write == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 continue;
Jeff Dikee99525f2007-10-16 01:26:41 -0700319
Paolo 'Blaisorblade' Giarrusso55c033c2005-11-13 16:07:11 -0800320 n = chan->ops->console_write(chan->fd, buf, len);
Jeff Dikee99525f2007-10-16 01:26:41 -0700321 if (chan->primary)
322 ret = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
Jeff Diked50084a2006-01-06 00:18:50 -0800324 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Jeff Dikea52f3622007-02-10 01:44:06 -0800327int console_open_chan(struct line *line, struct console *co)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Jeff Dike1f801712006-01-06 00:18:55 -0800329 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Jeff Dike1f801712006-01-06 00:18:55 -0800331 err = open_chan(&line->chan_list);
Jeff Dikee99525f2007-10-16 01:26:41 -0700332 if (err)
Jeff Dike1f801712006-01-06 00:18:55 -0800333 return err;
334
Jeff Dikee99525f2007-10-16 01:26:41 -0700335 printk(KERN_INFO "Console initialized on /dev/%s%d\n", co->name,
336 co->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return 0;
338}
339
340int chan_window_size(struct list_head *chans, unsigned short *rows_out,
341 unsigned short *cols_out)
342{
343 struct list_head *ele;
344 struct chan *chan;
345
Jeff Dikee99525f2007-10-16 01:26:41 -0700346 list_for_each(ele, chans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 chan = list_entry(ele, struct chan, list);
Jeff Dikee99525f2007-10-16 01:26:41 -0700348 if (chan->primary) {
349 if (chan->ops->window_size == NULL)
Jeff Diked50084a2006-01-06 00:18:50 -0800350 return 0;
351 return chan->ops->window_size(chan->fd, chan->data,
352 rows_out, cols_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 }
Jeff Diked50084a2006-01-06 00:18:50 -0800355 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
Paolo 'Blaisorblade' Giarrusso42947cb2006-02-01 03:06:29 -0800358static void free_one_chan(struct chan *chan, int delay_free_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 list_del(&chan->list);
Jeff Dike165dc592006-01-06 00:18:57 -0800361
362 close_one_chan(chan, delay_free_irq);
363
Jeff Dikee99525f2007-10-16 01:26:41 -0700364 if (chan->ops->free != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 (*chan->ops->free)(chan->data);
Jeff Dike165dc592006-01-06 00:18:57 -0800366
Jeff Dikee99525f2007-10-16 01:26:41 -0700367 if (chan->primary && chan->output)
368 ignore_sigio_fd(chan->fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 kfree(chan);
370}
371
Paolo 'Blaisorblade' Giarrusso42947cb2006-02-01 03:06:29 -0800372static void free_chan(struct list_head *chans, int delay_free_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 struct list_head *ele, *next;
375 struct chan *chan;
376
Jeff Dikee99525f2007-10-16 01:26:41 -0700377 list_for_each_safe(ele, next, chans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 chan = list_entry(ele, struct chan, list);
Jeff Dike165dc592006-01-06 00:18:57 -0800379 free_one_chan(chan, delay_free_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381}
382
383static int one_chan_config_string(struct chan *chan, char *str, int size,
384 char **error_out)
385{
386 int n = 0;
387
Jeff Dikee99525f2007-10-16 01:26:41 -0700388 if (chan == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 CONFIG_CHUNK(str, size, n, "none", 1);
Jeff Diked50084a2006-01-06 00:18:50 -0800390 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
393 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
394
Jeff Dikee99525f2007-10-16 01:26:41 -0700395 if (chan->dev == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 CONFIG_CHUNK(str, size, n, "", 1);
Jeff Diked50084a2006-01-06 00:18:50 -0800397 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
400 CONFIG_CHUNK(str, size, n, ":", 0);
401 CONFIG_CHUNK(str, size, n, chan->dev, 0);
402
Jeff Diked50084a2006-01-06 00:18:50 -0800403 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
Jeff Diked50084a2006-01-06 00:18:50 -0800406static int chan_pair_config_string(struct chan *in, struct chan *out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 char *str, int size, char **error_out)
408{
409 int n;
410
411 n = one_chan_config_string(in, str, size, error_out);
412 str += n;
413 size -= n;
414
Jeff Dikee99525f2007-10-16 01:26:41 -0700415 if (in == out) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 CONFIG_CHUNK(str, size, n, "", 1);
Jeff Diked50084a2006-01-06 00:18:50 -0800417 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419
420 CONFIG_CHUNK(str, size, n, ",", 1);
421 n = one_chan_config_string(out, str, size, error_out);
422 str += n;
423 size -= n;
424 CONFIG_CHUNK(str, size, n, "", 1);
425
Jeff Diked50084a2006-01-06 00:18:50 -0800426 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Jeff Diked50084a2006-01-06 00:18:50 -0800429int chan_config_string(struct list_head *chans, char *str, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 char **error_out)
431{
432 struct list_head *ele;
433 struct chan *chan, *in = NULL, *out = NULL;
434
Jeff Dikee99525f2007-10-16 01:26:41 -0700435 list_for_each(ele, chans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 chan = list_entry(ele, struct chan, list);
Jeff Dikee99525f2007-10-16 01:26:41 -0700437 if (!chan->primary)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 continue;
Jeff Dikee99525f2007-10-16 01:26:41 -0700439 if (chan->input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 in = chan;
Jeff Dikee99525f2007-10-16 01:26:41 -0700441 if (chan->output)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 out = chan;
443 }
444
Jeff Diked50084a2006-01-06 00:18:50 -0800445 return chan_pair_config_string(in, out, str, size, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
448struct chan_type {
449 char *key;
Jeff Dike5e7672e2006-09-27 01:50:33 -0700450 const struct chan_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451};
452
Jeff Dike5e7672e2006-09-27 01:50:33 -0700453static const struct chan_type chan_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 { "fd", &fd_ops },
455
456#ifdef CONFIG_NULL_CHAN
457 { "null", &null_ops },
458#else
459 { "null", &not_configged_ops },
460#endif
461
462#ifdef CONFIG_PORT_CHAN
463 { "port", &port_ops },
464#else
465 { "port", &not_configged_ops },
466#endif
467
468#ifdef CONFIG_PTY_CHAN
469 { "pty", &pty_ops },
470 { "pts", &pts_ops },
471#else
472 { "pty", &not_configged_ops },
473 { "pts", &not_configged_ops },
474#endif
475
476#ifdef CONFIG_TTY_CHAN
477 { "tty", &tty_ops },
478#else
479 { "tty", &not_configged_ops },
480#endif
481
482#ifdef CONFIG_XTERM_CHAN
483 { "xterm", &xterm_ops },
484#else
485 { "xterm", &not_configged_ops },
486#endif
487};
488
Jeff Dike165dc592006-01-06 00:18:57 -0800489static struct chan *parse_chan(struct line *line, char *str, int device,
Jeff Dikef28169d2007-02-10 01:43:53 -0800490 const struct chan_opts *opts, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Jeff Dike5e7672e2006-09-27 01:50:33 -0700492 const struct chan_type *entry;
493 const struct chan_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 struct chan *chan;
495 void *data;
496 int i;
497
498 ops = NULL;
499 data = NULL;
Jeff Dikee99525f2007-10-16 01:26:41 -0700500 for(i = 0; i < ARRAY_SIZE(chan_table); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 entry = &chan_table[i];
Jeff Dikee99525f2007-10-16 01:26:41 -0700502 if (!strncmp(str, entry->key, strlen(entry->key))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 ops = entry->ops;
504 str += strlen(entry->key);
505 break;
506 }
507 }
Jeff Dikee99525f2007-10-16 01:26:41 -0700508 if (ops == NULL) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800509 *error_out = "No match for configured backends";
Jeff Diked50084a2006-01-06 00:18:50 -0800510 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 data = (*ops->init)(str, device, opts);
Jeff Dikee99525f2007-10-16 01:26:41 -0700514 if (data == NULL) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800515 *error_out = "Configuration failed";
Jeff Diked50084a2006-01-06 00:18:50 -0800516 return NULL;
Jeff Dikef28169d2007-02-10 01:43:53 -0800517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Paolo 'Blaisorblade' Giarrusso79ae2cb2005-09-22 21:44:21 -0700519 chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
Jeff Dikee99525f2007-10-16 01:26:41 -0700520 if (chan == NULL) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800521 *error_out = "Memory allocation failed";
Jeff Diked50084a2006-01-06 00:18:50 -0800522 return NULL;
Jeff Dikef28169d2007-02-10 01:43:53 -0800523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
Jeff Dike165dc592006-01-06 00:18:57 -0800525 .free_list =
526 LIST_HEAD_INIT(chan->free_list),
527 .line = line,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 .primary = 1,
529 .input = 0,
530 .output = 0,
531 .opened = 0,
Jeff Dike165dc592006-01-06 00:18:57 -0800532 .enabled = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 .fd = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 .ops = ops,
535 .data = data });
Jeff Diked50084a2006-01-06 00:18:50 -0800536 return chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Jeff Dike165dc592006-01-06 00:18:57 -0800539int parse_chan_pair(char *str, struct line *line, int device,
Jeff Dikef28169d2007-02-10 01:43:53 -0800540 const struct chan_opts *opts, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Jeff Dike165dc592006-01-06 00:18:57 -0800542 struct list_head *chans = &line->chan_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 struct chan *new, *chan;
544 char *in, *out;
545
Jeff Dikee99525f2007-10-16 01:26:41 -0700546 if (!list_empty(chans)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 chan = list_entry(chans->next, struct chan, list);
Jeff Dike165dc592006-01-06 00:18:57 -0800548 free_chan(chans, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 INIT_LIST_HEAD(chans);
550 }
551
552 out = strchr(str, ',');
Jeff Dikee99525f2007-10-16 01:26:41 -0700553 if (out != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 in = str;
555 *out = '\0';
556 out++;
Jeff Dikef28169d2007-02-10 01:43:53 -0800557 new = parse_chan(line, in, device, opts, error_out);
Jeff Dikee99525f2007-10-16 01:26:41 -0700558 if (new == NULL)
Jeff Diked50084a2006-01-06 00:18:50 -0800559 return -1;
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 new->input = 1;
562 list_add(&new->list, chans);
563
Jeff Dikef28169d2007-02-10 01:43:53 -0800564 new = parse_chan(line, out, device, opts, error_out);
Jeff Dikee99525f2007-10-16 01:26:41 -0700565 if (new == NULL)
Jeff Diked50084a2006-01-06 00:18:50 -0800566 return -1;
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 list_add(&new->list, chans);
569 new->output = 1;
570 }
571 else {
Jeff Dikef28169d2007-02-10 01:43:53 -0800572 new = parse_chan(line, str, device, opts, error_out);
Jeff Dikee99525f2007-10-16 01:26:41 -0700573 if (new == NULL)
Jeff Diked50084a2006-01-06 00:18:50 -0800574 return -1;
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 list_add(&new->list, chans);
577 new->input = 1;
578 new->output = 1;
579 }
Jeff Diked50084a2006-01-06 00:18:50 -0800580 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
583int chan_out_fd(struct list_head *chans)
584{
585 struct list_head *ele;
586 struct chan *chan;
587
Jeff Dikee99525f2007-10-16 01:26:41 -0700588 list_for_each(ele, chans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 chan = list_entry(ele, struct chan, list);
Jeff Dikee99525f2007-10-16 01:26:41 -0700590 if (chan->primary && chan->output)
Jeff Diked50084a2006-01-06 00:18:50 -0800591 return chan->fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
Jeff Diked50084a2006-01-06 00:18:50 -0800593 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
David Howells6d5aefb2006-12-05 19:36:26 +0000596void chan_interrupt(struct list_head *chans, struct delayed_work *task,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 struct tty_struct *tty, int irq)
598{
599 struct list_head *ele, *next;
600 struct chan *chan;
601 int err;
602 char c;
603
Jeff Dikee99525f2007-10-16 01:26:41 -0700604 list_for_each_safe(ele, next, chans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 chan = list_entry(ele, struct chan, list);
Jeff Dikee99525f2007-10-16 01:26:41 -0700606 if (!chan->input || (chan->ops->read == NULL))
607 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 do {
Alan Cox33f0f882006-01-09 20:54:13 -0800609 if (tty && !tty_buffer_request_room(tty, 1)) {
Jeff Dike9159c9d2006-01-06 00:18:58 -0800610 schedule_delayed_work(task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 goto out;
612 }
613 err = chan->ops->read(chan->fd, &c, chan->data);
Jeff Dikee99525f2007-10-16 01:26:41 -0700614 if (err > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 tty_receive_char(tty, c);
Jeff Dikee99525f2007-10-16 01:26:41 -0700616 } while (err > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Jeff Dikee99525f2007-10-16 01:26:41 -0700618 if (err == 0)
619 reactivate_fd(chan->fd, irq);
620 if (err == -EIO) {
621 if (chan->primary) {
622 if (tty != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 tty_hangup(tty);
Jeff Dike165dc592006-01-06 00:18:57 -0800624 close_chan(chans, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return;
626 }
Jeff Dike165dc592006-01-06 00:18:57 -0800627 else close_one_chan(chan, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629 }
630 out:
Jeff Dikee99525f2007-10-16 01:26:41 -0700631 if (tty)
632 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}