blob: 25e1965df7ce6960440e2f51d2bf59c203a94f2d [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
WANG Cong3af7cb72008-04-28 02:13:55 -0700128static int open_chan(struct list_head *chans)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
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
Richard Weinberger47562272010-08-09 17:20:14 -0700213 if (chan->input && chan->enabled)
Jeff Dike165dc592006-01-06 00:18:57 -0800214 free_irq(chan->line->driver->read_irq, chan);
Richard Weinberger47562272010-08-09 17:20:14 -0700215 if (chan->output && chan->enabled)
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 {
Richard Weinberger47562272010-08-09 17:20:14 -0700234 if (chan->input && chan->enabled)
Jeff Dike165dc592006-01-06 00:18:57 -0800235 free_irq(chan->line->driver->read_irq, chan);
Richard Weinberger47562272010-08-09 17:20:14 -0700236 if (chan->output && chan->enabled)
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
Jeff Dikec59dbca2007-10-16 01:26:42 -0700294 if (len == 0)
295 return 0;
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 list_for_each(ele, chans) {
298 chan = list_entry(ele, struct chan, list);
299 if (!chan->output || (chan->ops->write == NULL))
300 continue;
Jeff Dikee99525f2007-10-16 01:26:41 -0700301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 n = chan->ops->write(chan->fd, buf, len, chan->data);
303 if (chan->primary) {
304 ret = n;
305 if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
306 reactivate_fd(chan->fd, write_irq);
307 }
308 }
Jeff Diked50084a2006-01-06 00:18:50 -0800309 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
312int console_write_chan(struct list_head *chans, const char *buf, int len)
313{
314 struct list_head *ele;
315 struct chan *chan;
316 int n, ret = 0;
317
Jeff Dikee99525f2007-10-16 01:26:41 -0700318 list_for_each(ele, chans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 chan = list_entry(ele, struct chan, list);
Jeff Dikee99525f2007-10-16 01:26:41 -0700320 if (!chan->output || (chan->ops->console_write == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 continue;
Jeff Dikee99525f2007-10-16 01:26:41 -0700322
Paolo 'Blaisorblade' Giarrusso55c033c2005-11-13 16:07:11 -0800323 n = chan->ops->console_write(chan->fd, buf, len);
Jeff Dikee99525f2007-10-16 01:26:41 -0700324 if (chan->primary)
325 ret = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
Jeff Diked50084a2006-01-06 00:18:50 -0800327 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Jeff Dikea52f3622007-02-10 01:44:06 -0800330int console_open_chan(struct line *line, struct console *co)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Jeff Dike1f801712006-01-06 00:18:55 -0800332 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Jeff Dike1f801712006-01-06 00:18:55 -0800334 err = open_chan(&line->chan_list);
Jeff Dikee99525f2007-10-16 01:26:41 -0700335 if (err)
Jeff Dike1f801712006-01-06 00:18:55 -0800336 return err;
337
Jeff Dikee99525f2007-10-16 01:26:41 -0700338 printk(KERN_INFO "Console initialized on /dev/%s%d\n", co->name,
339 co->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return 0;
341}
342
343int chan_window_size(struct list_head *chans, unsigned short *rows_out,
344 unsigned short *cols_out)
345{
346 struct list_head *ele;
347 struct chan *chan;
348
Jeff Dikee99525f2007-10-16 01:26:41 -0700349 list_for_each(ele, chans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 chan = list_entry(ele, struct chan, list);
Jeff Dikee99525f2007-10-16 01:26:41 -0700351 if (chan->primary) {
352 if (chan->ops->window_size == NULL)
Jeff Diked50084a2006-01-06 00:18:50 -0800353 return 0;
354 return chan->ops->window_size(chan->fd, chan->data,
355 rows_out, cols_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357 }
Jeff Diked50084a2006-01-06 00:18:50 -0800358 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
Paolo 'Blaisorblade' Giarrusso42947cb2006-02-01 03:06:29 -0800361static void free_one_chan(struct chan *chan, int delay_free_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 list_del(&chan->list);
Jeff Dike165dc592006-01-06 00:18:57 -0800364
365 close_one_chan(chan, delay_free_irq);
366
Jeff Dikee99525f2007-10-16 01:26:41 -0700367 if (chan->ops->free != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 (*chan->ops->free)(chan->data);
Jeff Dike165dc592006-01-06 00:18:57 -0800369
Jeff Dikee99525f2007-10-16 01:26:41 -0700370 if (chan->primary && chan->output)
371 ignore_sigio_fd(chan->fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 kfree(chan);
373}
374
Paolo 'Blaisorblade' Giarrusso42947cb2006-02-01 03:06:29 -0800375static void free_chan(struct list_head *chans, int delay_free_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 struct list_head *ele, *next;
378 struct chan *chan;
379
Jeff Dikee99525f2007-10-16 01:26:41 -0700380 list_for_each_safe(ele, next, chans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 chan = list_entry(ele, struct chan, list);
Jeff Dike165dc592006-01-06 00:18:57 -0800382 free_one_chan(chan, delay_free_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384}
385
386static int one_chan_config_string(struct chan *chan, char *str, int size,
387 char **error_out)
388{
389 int n = 0;
390
Jeff Dikee99525f2007-10-16 01:26:41 -0700391 if (chan == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 CONFIG_CHUNK(str, size, n, "none", 1);
Jeff Diked50084a2006-01-06 00:18:50 -0800393 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395
396 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
397
Jeff Dikee99525f2007-10-16 01:26:41 -0700398 if (chan->dev == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 CONFIG_CHUNK(str, size, n, "", 1);
Jeff Diked50084a2006-01-06 00:18:50 -0800400 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402
403 CONFIG_CHUNK(str, size, n, ":", 0);
404 CONFIG_CHUNK(str, size, n, chan->dev, 0);
405
Jeff Diked50084a2006-01-06 00:18:50 -0800406 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Jeff Diked50084a2006-01-06 00:18:50 -0800409static int chan_pair_config_string(struct chan *in, struct chan *out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 char *str, int size, char **error_out)
411{
412 int n;
413
414 n = one_chan_config_string(in, str, size, error_out);
415 str += n;
416 size -= n;
417
Jeff Dikee99525f2007-10-16 01:26:41 -0700418 if (in == out) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 CONFIG_CHUNK(str, size, n, "", 1);
Jeff Diked50084a2006-01-06 00:18:50 -0800420 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422
423 CONFIG_CHUNK(str, size, n, ",", 1);
424 n = one_chan_config_string(out, str, size, error_out);
425 str += n;
426 size -= n;
427 CONFIG_CHUNK(str, size, n, "", 1);
428
Jeff Diked50084a2006-01-06 00:18:50 -0800429 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Jeff Diked50084a2006-01-06 00:18:50 -0800432int chan_config_string(struct list_head *chans, char *str, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 char **error_out)
434{
435 struct list_head *ele;
436 struct chan *chan, *in = NULL, *out = NULL;
437
Jeff Dikee99525f2007-10-16 01:26:41 -0700438 list_for_each(ele, chans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 chan = list_entry(ele, struct chan, list);
Jeff Dikee99525f2007-10-16 01:26:41 -0700440 if (!chan->primary)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 continue;
Jeff Dikee99525f2007-10-16 01:26:41 -0700442 if (chan->input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 in = chan;
Jeff Dikee99525f2007-10-16 01:26:41 -0700444 if (chan->output)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 out = chan;
446 }
447
Jeff Diked50084a2006-01-06 00:18:50 -0800448 return chan_pair_config_string(in, out, str, size, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
451struct chan_type {
452 char *key;
Jeff Dike5e7672e2006-09-27 01:50:33 -0700453 const struct chan_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454};
455
Jeff Dike5e7672e2006-09-27 01:50:33 -0700456static const struct chan_type chan_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 { "fd", &fd_ops },
458
459#ifdef CONFIG_NULL_CHAN
460 { "null", &null_ops },
461#else
462 { "null", &not_configged_ops },
463#endif
464
465#ifdef CONFIG_PORT_CHAN
466 { "port", &port_ops },
467#else
468 { "port", &not_configged_ops },
469#endif
470
471#ifdef CONFIG_PTY_CHAN
472 { "pty", &pty_ops },
473 { "pts", &pts_ops },
474#else
475 { "pty", &not_configged_ops },
476 { "pts", &not_configged_ops },
477#endif
478
479#ifdef CONFIG_TTY_CHAN
480 { "tty", &tty_ops },
481#else
482 { "tty", &not_configged_ops },
483#endif
484
485#ifdef CONFIG_XTERM_CHAN
486 { "xterm", &xterm_ops },
487#else
488 { "xterm", &not_configged_ops },
489#endif
490};
491
Jeff Dike165dc592006-01-06 00:18:57 -0800492static struct chan *parse_chan(struct line *line, char *str, int device,
Jeff Dikef28169d2007-02-10 01:43:53 -0800493 const struct chan_opts *opts, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Jeff Dike5e7672e2006-09-27 01:50:33 -0700495 const struct chan_type *entry;
496 const struct chan_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 struct chan *chan;
498 void *data;
499 int i;
500
501 ops = NULL;
502 data = NULL;
Jeff Dikee99525f2007-10-16 01:26:41 -0700503 for(i = 0; i < ARRAY_SIZE(chan_table); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 entry = &chan_table[i];
Jeff Dikee99525f2007-10-16 01:26:41 -0700505 if (!strncmp(str, entry->key, strlen(entry->key))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 ops = entry->ops;
507 str += strlen(entry->key);
508 break;
509 }
510 }
Jeff Dikee99525f2007-10-16 01:26:41 -0700511 if (ops == NULL) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800512 *error_out = "No match for configured backends";
Jeff Diked50084a2006-01-06 00:18:50 -0800513 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 data = (*ops->init)(str, device, opts);
Jeff Dikee99525f2007-10-16 01:26:41 -0700517 if (data == NULL) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800518 *error_out = "Configuration failed";
Jeff Diked50084a2006-01-06 00:18:50 -0800519 return NULL;
Jeff Dikef28169d2007-02-10 01:43:53 -0800520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Paolo 'Blaisorblade' Giarrusso79ae2cb2005-09-22 21:44:21 -0700522 chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
Jeff Dikee99525f2007-10-16 01:26:41 -0700523 if (chan == NULL) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800524 *error_out = "Memory allocation failed";
Jeff Diked50084a2006-01-06 00:18:50 -0800525 return NULL;
Jeff Dikef28169d2007-02-10 01:43:53 -0800526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
Jeff Dike165dc592006-01-06 00:18:57 -0800528 .free_list =
529 LIST_HEAD_INIT(chan->free_list),
530 .line = line,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 .primary = 1,
532 .input = 0,
533 .output = 0,
534 .opened = 0,
Jeff Dike165dc592006-01-06 00:18:57 -0800535 .enabled = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 .fd = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 .ops = ops,
538 .data = data });
Jeff Diked50084a2006-01-06 00:18:50 -0800539 return chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Jeff Dike165dc592006-01-06 00:18:57 -0800542int parse_chan_pair(char *str, struct line *line, int device,
Jeff Dikef28169d2007-02-10 01:43:53 -0800543 const struct chan_opts *opts, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Jeff Dike165dc592006-01-06 00:18:57 -0800545 struct list_head *chans = &line->chan_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 struct chan *new, *chan;
547 char *in, *out;
548
Jeff Dikee99525f2007-10-16 01:26:41 -0700549 if (!list_empty(chans)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 chan = list_entry(chans->next, struct chan, list);
Jeff Dike165dc592006-01-06 00:18:57 -0800551 free_chan(chans, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 INIT_LIST_HEAD(chans);
553 }
554
555 out = strchr(str, ',');
Jeff Dikee99525f2007-10-16 01:26:41 -0700556 if (out != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 in = str;
558 *out = '\0';
559 out++;
Jeff Dikef28169d2007-02-10 01:43:53 -0800560 new = parse_chan(line, in, device, opts, error_out);
Jeff Dikee99525f2007-10-16 01:26:41 -0700561 if (new == NULL)
Jeff Diked50084a2006-01-06 00:18:50 -0800562 return -1;
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 new->input = 1;
565 list_add(&new->list, chans);
566
Jeff Dikef28169d2007-02-10 01:43:53 -0800567 new = parse_chan(line, out, device, opts, error_out);
Jeff Dikee99525f2007-10-16 01:26:41 -0700568 if (new == NULL)
Jeff Diked50084a2006-01-06 00:18:50 -0800569 return -1;
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 list_add(&new->list, chans);
572 new->output = 1;
573 }
574 else {
Jeff Dikef28169d2007-02-10 01:43:53 -0800575 new = parse_chan(line, str, device, opts, error_out);
Jeff Dikee99525f2007-10-16 01:26:41 -0700576 if (new == NULL)
Jeff Diked50084a2006-01-06 00:18:50 -0800577 return -1;
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 list_add(&new->list, chans);
580 new->input = 1;
581 new->output = 1;
582 }
Jeff Diked50084a2006-01-06 00:18:50 -0800583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
David Howells6d5aefb2006-12-05 19:36:26 +0000586void chan_interrupt(struct list_head *chans, struct delayed_work *task,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 struct tty_struct *tty, int irq)
588{
589 struct list_head *ele, *next;
590 struct chan *chan;
591 int err;
592 char c;
593
Jeff Dikee99525f2007-10-16 01:26:41 -0700594 list_for_each_safe(ele, next, chans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 chan = list_entry(ele, struct chan, list);
Jeff Dikee99525f2007-10-16 01:26:41 -0700596 if (!chan->input || (chan->ops->read == NULL))
597 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 do {
Alan Cox33f0f882006-01-09 20:54:13 -0800599 if (tty && !tty_buffer_request_room(tty, 1)) {
Jeff Dike9159c9d2006-01-06 00:18:58 -0800600 schedule_delayed_work(task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 goto out;
602 }
603 err = chan->ops->read(chan->fd, &c, chan->data);
Jeff Dikee99525f2007-10-16 01:26:41 -0700604 if (err > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 tty_receive_char(tty, c);
Jeff Dikee99525f2007-10-16 01:26:41 -0700606 } while (err > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Jeff Dikee99525f2007-10-16 01:26:41 -0700608 if (err == 0)
609 reactivate_fd(chan->fd, irq);
610 if (err == -EIO) {
611 if (chan->primary) {
612 if (tty != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 tty_hangup(tty);
Jeff Dike165dc592006-01-06 00:18:57 -0800614 close_chan(chans, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return;
616 }
Jeff Dike165dc592006-01-06 00:18:57 -0800617 else close_one_chan(chan, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619 }
620 out:
Jeff Dikee99525f2007-10-16 01:26:41 -0700621 if (tty)
622 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}