blob: ca4c7ebfd0aa592141021d779f1d49feac0f33a2 [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>
Al Viro510c72a32011-08-18 20:08:29 +01009#include "chan.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
Al Virobed5e392011-09-08 10:49:34 -0400143void chan_enable_winch(struct chan *chan, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Al Virobed5e392011-09-08 10:49:34 -0400145 if (chan && chan->primary && chan->ops->winch)
146 register_winch(chan->fd, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Al Viro0fcd7192011-09-10 08:17:04 -0400149static void line_timer_cb(struct work_struct *work)
150{
151 struct line *line = container_of(work, struct line, task.work);
152
153 if (!line->throttled)
154 chan_interrupt(line, line->tty, line->driver->read_irq);
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
Al Viro0fcd7192011-09-10 08:17:04 -0400163 INIT_DELAYED_WORK(&line->task, line_timer_cb);
164
Jeff Dikee99525f2007-10-16 01:26:41 -0700165 list_for_each(ele, &line->chan_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 chan = list_entry(ele, struct chan, list);
Jeff Diked14ad812007-07-15 23:38:54 -0700167 err = open_one_chan(chan);
168 if (err) {
169 if (chan->primary)
170 goto out_close;
171
Jeff Dike165dc592006-01-06 00:18:57 -0800172 continue;
Jeff Diked14ad812007-07-15 23:38:54 -0700173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Jeff Dikee99525f2007-10-16 01:26:41 -0700175 if (chan->enabled)
Jeff Dike165dc592006-01-06 00:18:57 -0800176 continue;
Jeff Diked14ad812007-07-15 23:38:54 -0700177 err = line_setup_irq(chan->fd, chan->input, chan->output, line,
178 chan);
179 if (err)
180 goto out_close;
181
Jeff Dike165dc592006-01-06 00:18:57 -0800182 chan->enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
Jeff Diked14ad812007-07-15 23:38:54 -0700184
185 return 0;
186
187 out_close:
Al Viro10c890c02011-09-10 08:39:18 -0400188 close_chan(line);
Jeff Diked14ad812007-07-15 23:38:54 -0700189 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Jeff Dike190c3e42007-02-10 01:43:55 -0800192/* Items are added in IRQ context, when free_irq can't be called, and
193 * removed in process context, when it can.
194 * This handles interrupt sources which disappear, and which need to
195 * be permanently disabled. This is discovered in IRQ context, but
196 * the freeing of the IRQ must be done later.
197 */
198static DEFINE_SPINLOCK(irqs_to_free_lock);
Jeff Dike165dc592006-01-06 00:18:57 -0800199static LIST_HEAD(irqs_to_free);
200
201void free_irqs(void)
202{
203 struct chan *chan;
Jeff Dike190c3e42007-02-10 01:43:55 -0800204 LIST_HEAD(list);
205 struct list_head *ele;
Jeff Dike30762122007-03-29 01:20:30 -0700206 unsigned long flags;
Jeff Dike165dc592006-01-06 00:18:57 -0800207
Jeff Dike30762122007-03-29 01:20:30 -0700208 spin_lock_irqsave(&irqs_to_free_lock, flags);
Jeff Dike190c3e42007-02-10 01:43:55 -0800209 list_splice_init(&irqs_to_free, &list);
Jeff Dike30762122007-03-29 01:20:30 -0700210 spin_unlock_irqrestore(&irqs_to_free_lock, flags);
Jeff Dike190c3e42007-02-10 01:43:55 -0800211
Jeff Dikee99525f2007-10-16 01:26:41 -0700212 list_for_each(ele, &list) {
Jeff Dike190c3e42007-02-10 01:43:55 -0800213 chan = list_entry(ele, struct chan, free_list);
Jeff Dike165dc592006-01-06 00:18:57 -0800214
Richard Weinberger47562272010-08-09 17:20:14 -0700215 if (chan->input && chan->enabled)
Jeff Dike165dc592006-01-06 00:18:57 -0800216 free_irq(chan->line->driver->read_irq, chan);
Richard Weinberger47562272010-08-09 17:20:14 -0700217 if (chan->output && chan->enabled)
Jeff Dike165dc592006-01-06 00:18:57 -0800218 free_irq(chan->line->driver->write_irq, chan);
219 chan->enabled = 0;
220 }
221}
222
223static void close_one_chan(struct chan *chan, int delay_free_irq)
224{
Jeff Dike30762122007-03-29 01:20:30 -0700225 unsigned long flags;
226
Jeff Dikee99525f2007-10-16 01:26:41 -0700227 if (!chan->opened)
Jeff Dike165dc592006-01-06 00:18:57 -0800228 return;
229
Jeff Dikee99525f2007-10-16 01:26:41 -0700230 if (delay_free_irq) {
Jeff Dike30762122007-03-29 01:20:30 -0700231 spin_lock_irqsave(&irqs_to_free_lock, flags);
Jeff Dike165dc592006-01-06 00:18:57 -0800232 list_add(&chan->free_list, &irqs_to_free);
Jeff Dike30762122007-03-29 01:20:30 -0700233 spin_unlock_irqrestore(&irqs_to_free_lock, flags);
Jeff Dike165dc592006-01-06 00:18:57 -0800234 }
235 else {
Richard Weinberger47562272010-08-09 17:20:14 -0700236 if (chan->input && chan->enabled)
Jeff Dike165dc592006-01-06 00:18:57 -0800237 free_irq(chan->line->driver->read_irq, chan);
Richard Weinberger47562272010-08-09 17:20:14 -0700238 if (chan->output && chan->enabled)
Jeff Dike165dc592006-01-06 00:18:57 -0800239 free_irq(chan->line->driver->write_irq, chan);
240 chan->enabled = 0;
241 }
Jeff Dikee99525f2007-10-16 01:26:41 -0700242 if (chan->ops->close != NULL)
Jeff Dike165dc592006-01-06 00:18:57 -0800243 (*chan->ops->close)(chan->fd, chan->data);
244
245 chan->opened = 0;
246 chan->fd = -1;
247}
248
Al Viro10c890c02011-09-10 08:39:18 -0400249void close_chan(struct line *line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 struct chan *chan;
252
253 /* Close in reverse order as open in case more than one of them
254 * refers to the same device and they save and restore that device's
255 * state. Then, the first one opened will have the original state,
256 * so it must be the last closed.
257 */
Al Viro10c890c02011-09-10 08:39:18 -0400258 list_for_each_entry_reverse(chan, &line->chan_list, list) {
259 close_one_chan(chan, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261}
262
Al Virobed5e392011-09-08 10:49:34 -0400263void deactivate_chan(struct chan *chan, int irq)
Jeff Dikee4dcee82006-01-06 00:18:58 -0800264{
Al Virobed5e392011-09-08 10:49:34 -0400265 if (chan && chan->enabled)
266 deactivate_fd(chan->fd, irq);
Jeff Dikee4dcee82006-01-06 00:18:58 -0800267}
268
Al Virobed5e392011-09-08 10:49:34 -0400269void reactivate_chan(struct chan *chan, int irq)
Jeff Dikee4dcee82006-01-06 00:18:58 -0800270{
Al Virobed5e392011-09-08 10:49:34 -0400271 if (chan && chan->enabled)
272 reactivate_fd(chan->fd, irq);
Jeff Dikee4dcee82006-01-06 00:18:58 -0800273}
274
Al Virobed5e392011-09-08 10:49:34 -0400275int write_chan(struct chan *chan, const char *buf, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 int write_irq)
277{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 int n, ret = 0;
279
Al Virobed5e392011-09-08 10:49:34 -0400280 if (len == 0 || !chan || !chan->ops->write)
Jeff Dikec59dbca2007-10-16 01:26:42 -0700281 return 0;
282
Al Virobed5e392011-09-08 10:49:34 -0400283 n = chan->ops->write(chan->fd, buf, len, chan->data);
284 if (chan->primary) {
285 ret = n;
286 if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
287 reactivate_fd(chan->fd, write_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
Jeff Diked50084a2006-01-06 00:18:50 -0800289 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Al Virobed5e392011-09-08 10:49:34 -0400292int console_write_chan(struct chan *chan, const char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 int n, ret = 0;
295
Al Virobed5e392011-09-08 10:49:34 -0400296 if (!chan || !chan->ops->console_write)
297 return 0;
Jeff Dikee99525f2007-10-16 01:26:41 -0700298
Al Virobed5e392011-09-08 10:49:34 -0400299 n = chan->ops->console_write(chan->fd, buf, len);
300 if (chan->primary)
301 ret = n;
Jeff Diked50084a2006-01-06 00:18:50 -0800302 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Jeff Dikea52f3622007-02-10 01:44:06 -0800305int console_open_chan(struct line *line, struct console *co)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Jeff Dike1f801712006-01-06 00:18:55 -0800307 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Jeff Dike1f801712006-01-06 00:18:55 -0800309 err = open_chan(&line->chan_list);
Jeff Dikee99525f2007-10-16 01:26:41 -0700310 if (err)
Jeff Dike1f801712006-01-06 00:18:55 -0800311 return err;
312
Jeff Dikee99525f2007-10-16 01:26:41 -0700313 printk(KERN_INFO "Console initialized on /dev/%s%d\n", co->name,
314 co->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return 0;
316}
317
Al Virobed5e392011-09-08 10:49:34 -0400318int chan_window_size(struct line *line, unsigned short *rows_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 unsigned short *cols_out)
320{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 struct chan *chan;
322
Al Virobed5e392011-09-08 10:49:34 -0400323 chan = line->chan_in;
324 if (chan && chan->primary) {
325 if (chan->ops->window_size == NULL)
326 return 0;
327 return chan->ops->window_size(chan->fd, chan->data,
328 rows_out, cols_out);
329 }
330 chan = line->chan_out;
331 if (chan && chan->primary) {
332 if (chan->ops->window_size == NULL)
333 return 0;
334 return chan->ops->window_size(chan->fd, chan->data,
335 rows_out, cols_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Jeff Diked50084a2006-01-06 00:18:50 -0800337 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Al Viro772bd0a2011-08-18 20:12:39 +0100340static void free_one_chan(struct chan *chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 list_del(&chan->list);
Jeff Dike165dc592006-01-06 00:18:57 -0800343
Al Viro772bd0a2011-08-18 20:12:39 +0100344 close_one_chan(chan, 0);
Jeff Dike165dc592006-01-06 00:18:57 -0800345
Jeff Dikee99525f2007-10-16 01:26:41 -0700346 if (chan->ops->free != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 (*chan->ops->free)(chan->data);
Jeff Dike165dc592006-01-06 00:18:57 -0800348
Jeff Dikee99525f2007-10-16 01:26:41 -0700349 if (chan->primary && chan->output)
350 ignore_sigio_fd(chan->fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 kfree(chan);
352}
353
Al Viro772bd0a2011-08-18 20:12:39 +0100354static void free_chan(struct list_head *chans)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 struct list_head *ele, *next;
357 struct chan *chan;
358
Jeff Dikee99525f2007-10-16 01:26:41 -0700359 list_for_each_safe(ele, next, chans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 chan = list_entry(ele, struct chan, list);
Al Viro772bd0a2011-08-18 20:12:39 +0100361 free_one_chan(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363}
364
365static int one_chan_config_string(struct chan *chan, char *str, int size,
366 char **error_out)
367{
368 int n = 0;
369
Jeff Dikee99525f2007-10-16 01:26:41 -0700370 if (chan == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 CONFIG_CHUNK(str, size, n, "none", 1);
Jeff Diked50084a2006-01-06 00:18:50 -0800372 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374
375 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
376
Jeff Dikee99525f2007-10-16 01:26:41 -0700377 if (chan->dev == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 CONFIG_CHUNK(str, size, n, "", 1);
Jeff Diked50084a2006-01-06 00:18:50 -0800379 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381
382 CONFIG_CHUNK(str, size, n, ":", 0);
383 CONFIG_CHUNK(str, size, n, chan->dev, 0);
384
Jeff Diked50084a2006-01-06 00:18:50 -0800385 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Jeff Diked50084a2006-01-06 00:18:50 -0800388static int chan_pair_config_string(struct chan *in, struct chan *out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 char *str, int size, char **error_out)
390{
391 int n;
392
393 n = one_chan_config_string(in, str, size, error_out);
394 str += n;
395 size -= n;
396
Jeff Dikee99525f2007-10-16 01:26:41 -0700397 if (in == out) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 CONFIG_CHUNK(str, size, n, "", 1);
Jeff Diked50084a2006-01-06 00:18:50 -0800399 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401
402 CONFIG_CHUNK(str, size, n, ",", 1);
403 n = one_chan_config_string(out, str, size, error_out);
404 str += n;
405 size -= n;
406 CONFIG_CHUNK(str, size, n, "", 1);
407
Jeff Diked50084a2006-01-06 00:18:50 -0800408 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
Al Virobed5e392011-09-08 10:49:34 -0400411int chan_config_string(struct line *line, char *str, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 char **error_out)
413{
Al Virobed5e392011-09-08 10:49:34 -0400414 struct chan *in = line->chan_in, *out = line->chan_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Al Virobed5e392011-09-08 10:49:34 -0400416 if (in && !in->primary)
417 in = NULL;
418 if (out && !out->primary)
419 out = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Jeff Diked50084a2006-01-06 00:18:50 -0800421 return chan_pair_config_string(in, out, str, size, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
424struct chan_type {
425 char *key;
Jeff Dike5e7672e2006-09-27 01:50:33 -0700426 const struct chan_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427};
428
Jeff Dike5e7672e2006-09-27 01:50:33 -0700429static const struct chan_type chan_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 { "fd", &fd_ops },
431
432#ifdef CONFIG_NULL_CHAN
433 { "null", &null_ops },
434#else
435 { "null", &not_configged_ops },
436#endif
437
438#ifdef CONFIG_PORT_CHAN
439 { "port", &port_ops },
440#else
441 { "port", &not_configged_ops },
442#endif
443
444#ifdef CONFIG_PTY_CHAN
445 { "pty", &pty_ops },
446 { "pts", &pts_ops },
447#else
448 { "pty", &not_configged_ops },
449 { "pts", &not_configged_ops },
450#endif
451
452#ifdef CONFIG_TTY_CHAN
453 { "tty", &tty_ops },
454#else
455 { "tty", &not_configged_ops },
456#endif
457
458#ifdef CONFIG_XTERM_CHAN
459 { "xterm", &xterm_ops },
460#else
461 { "xterm", &not_configged_ops },
462#endif
463};
464
Jeff Dike165dc592006-01-06 00:18:57 -0800465static struct chan *parse_chan(struct line *line, char *str, int device,
Jeff Dikef28169d2007-02-10 01:43:53 -0800466 const struct chan_opts *opts, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Jeff Dike5e7672e2006-09-27 01:50:33 -0700468 const struct chan_type *entry;
469 const struct chan_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 struct chan *chan;
471 void *data;
472 int i;
473
474 ops = NULL;
475 data = NULL;
Jeff Dikee99525f2007-10-16 01:26:41 -0700476 for(i = 0; i < ARRAY_SIZE(chan_table); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 entry = &chan_table[i];
Jeff Dikee99525f2007-10-16 01:26:41 -0700478 if (!strncmp(str, entry->key, strlen(entry->key))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 ops = entry->ops;
480 str += strlen(entry->key);
481 break;
482 }
483 }
Jeff Dikee99525f2007-10-16 01:26:41 -0700484 if (ops == NULL) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800485 *error_out = "No match for configured backends";
Jeff Diked50084a2006-01-06 00:18:50 -0800486 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 data = (*ops->init)(str, device, opts);
Jeff Dikee99525f2007-10-16 01:26:41 -0700490 if (data == NULL) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800491 *error_out = "Configuration failed";
Jeff Diked50084a2006-01-06 00:18:50 -0800492 return NULL;
Jeff Dikef28169d2007-02-10 01:43:53 -0800493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Paolo 'Blaisorblade' Giarrusso79ae2cb2005-09-22 21:44:21 -0700495 chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
Jeff Dikee99525f2007-10-16 01:26:41 -0700496 if (chan == NULL) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800497 *error_out = "Memory allocation failed";
Jeff Diked50084a2006-01-06 00:18:50 -0800498 return NULL;
Jeff Dikef28169d2007-02-10 01:43:53 -0800499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
Jeff Dike165dc592006-01-06 00:18:57 -0800501 .free_list =
502 LIST_HEAD_INIT(chan->free_list),
503 .line = line,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 .primary = 1,
505 .input = 0,
506 .output = 0,
507 .opened = 0,
Jeff Dike165dc592006-01-06 00:18:57 -0800508 .enabled = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 .fd = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 .ops = ops,
511 .data = data });
Jeff Diked50084a2006-01-06 00:18:50 -0800512 return chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Jeff Dike165dc592006-01-06 00:18:57 -0800515int parse_chan_pair(char *str, struct line *line, int device,
Jeff Dikef28169d2007-02-10 01:43:53 -0800516 const struct chan_opts *opts, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Jeff Dike165dc592006-01-06 00:18:57 -0800518 struct list_head *chans = &line->chan_list;
Richard Weinbergerf1c93e42011-07-25 17:12:55 -0700519 struct chan *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 char *in, *out;
521
Jeff Dikee99525f2007-10-16 01:26:41 -0700522 if (!list_empty(chans)) {
Al Viroee485072011-09-08 07:07:26 -0400523 line->chan_in = line->chan_out = NULL;
Al Viro772bd0a2011-08-18 20:12:39 +0100524 free_chan(chans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 INIT_LIST_HEAD(chans);
526 }
527
Al Viro31efceb2011-09-09 19:14:02 -0400528 if (!str)
529 return 0;
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 out = strchr(str, ',');
Jeff Dikee99525f2007-10-16 01:26:41 -0700532 if (out != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 in = str;
534 *out = '\0';
535 out++;
Jeff Dikef28169d2007-02-10 01:43:53 -0800536 new = parse_chan(line, in, device, opts, error_out);
Jeff Dikee99525f2007-10-16 01:26:41 -0700537 if (new == NULL)
Jeff Diked50084a2006-01-06 00:18:50 -0800538 return -1;
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 new->input = 1;
541 list_add(&new->list, chans);
Al Viroee485072011-09-08 07:07:26 -0400542 line->chan_in = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Jeff Dikef28169d2007-02-10 01:43:53 -0800544 new = parse_chan(line, out, device, opts, error_out);
Jeff Dikee99525f2007-10-16 01:26:41 -0700545 if (new == NULL)
Jeff Diked50084a2006-01-06 00:18:50 -0800546 return -1;
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 list_add(&new->list, chans);
549 new->output = 1;
Al Viroee485072011-09-08 07:07:26 -0400550 line->chan_out = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552 else {
Jeff Dikef28169d2007-02-10 01:43:53 -0800553 new = parse_chan(line, str, device, opts, error_out);
Jeff Dikee99525f2007-10-16 01:26:41 -0700554 if (new == NULL)
Jeff Diked50084a2006-01-06 00:18:50 -0800555 return -1;
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 list_add(&new->list, chans);
558 new->input = 1;
559 new->output = 1;
Al Viroee485072011-09-08 07:07:26 -0400560 line->chan_in = line->chan_out = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
Jeff Diked50084a2006-01-06 00:18:50 -0800562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
Al Viro0fcd7192011-09-10 08:17:04 -0400565void chan_interrupt(struct line *line, struct tty_struct *tty, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Al Virobed5e392011-09-08 10:49:34 -0400567 struct chan *chan = line->chan_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 int err;
569 char c;
570
Al Virobed5e392011-09-08 10:49:34 -0400571 if (!chan || !chan->ops->read)
572 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Al Virobed5e392011-09-08 10:49:34 -0400574 do {
575 if (tty && !tty_buffer_request_room(tty, 1)) {
Al Viro0fcd7192011-09-10 08:17:04 -0400576 schedule_delayed_work(&line->task, 1);
Al Virobed5e392011-09-08 10:49:34 -0400577 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
Al Virobed5e392011-09-08 10:49:34 -0400579 err = chan->ops->read(chan->fd, &c, chan->data);
580 if (err > 0)
581 tty_receive_char(tty, c);
582 } while (err > 0);
583
584 if (err == 0)
585 reactivate_fd(chan->fd, irq);
586 if (err == -EIO) {
587 if (chan->primary) {
588 if (tty != NULL)
589 tty_hangup(tty);
Al Viro10c890c02011-09-10 08:39:18 -0400590 if (line->chan_out != chan)
591 close_one_chan(line->chan_out, 1);
Al Virobed5e392011-09-08 10:49:34 -0400592 }
Al Viro10c890c02011-09-10 08:39:18 -0400593 close_one_chan(chan, 1);
594 if (chan->primary)
595 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597 out:
Jeff Dikee99525f2007-10-16 01:26:41 -0700598 if (tty)
599 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}