blob: 45e248c2f43c7c1c8c618aea0eb41980fd28a046 [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"
Richard Weinbergerfa7a0442012-04-17 22:37:13 +020011#include "irq_kern.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Paolo 'Blaisorblade' Giarrussofac97ae2005-09-22 21:44:22 -070013#ifdef CONFIG_NOCONFIG_CHAN
Jeff Dikef28169d2007-02-10 01:43:53 -080014static void *not_configged_init(char *str, int device,
15 const struct chan_opts *opts)
Paolo 'Blaisorblade' Giarrussofac97ae2005-09-22 21:44:22 -070016{
Jeff Dikee99525f2007-10-16 01:26:41 -070017 printk(KERN_ERR "Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 "UML\n");
Jeff Diked50084a2006-01-06 00:18:50 -080019 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070020}
21
22static int not_configged_open(int input, int output, int primary, void *data,
23 char **dev_out)
24{
Jeff Dikee99525f2007-10-16 01:26:41 -070025 printk(KERN_ERR "Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 "UML\n");
Jeff Diked50084a2006-01-06 00:18:50 -080027 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028}
29
30static void not_configged_close(int fd, void *data)
31{
Jeff Dikee99525f2007-10-16 01:26:41 -070032 printk(KERN_ERR "Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 "UML\n");
34}
35
36static int not_configged_read(int fd, char *c_out, void *data)
37{
Jeff Dikee99525f2007-10-16 01:26:41 -070038 printk(KERN_ERR "Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 "UML\n");
Jeff Diked50084a2006-01-06 00:18:50 -080040 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
43static int not_configged_write(int fd, const char *buf, int len, void *data)
44{
Jeff Dikee99525f2007-10-16 01:26:41 -070045 printk(KERN_ERR "Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 "UML\n");
Jeff Diked50084a2006-01-06 00:18:50 -080047 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
Paolo 'Blaisorblade' Giarrusso55c033c2005-11-13 16:07:11 -080050static int not_configged_console_write(int fd, const char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Jeff Dikee99525f2007-10-16 01:26:41 -070052 printk(KERN_ERR "Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 "UML\n");
Jeff Diked50084a2006-01-06 00:18:50 -080054 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
57static int not_configged_window_size(int fd, void *data, unsigned short *rows,
58 unsigned short *cols)
59{
Jeff Dikee99525f2007-10-16 01:26:41 -070060 printk(KERN_ERR "Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 "UML\n");
Jeff Diked50084a2006-01-06 00:18:50 -080062 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
65static void not_configged_free(void *data)
66{
Jeff Dikee99525f2007-10-16 01:26:41 -070067 printk(KERN_ERR "Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 "UML\n");
69}
70
Jeff Dike5e7672e2006-09-27 01:50:33 -070071static const struct chan_ops not_configged_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 .init = not_configged_init,
73 .open = not_configged_open,
74 .close = not_configged_close,
75 .read = not_configged_read,
76 .write = not_configged_write,
77 .console_write = not_configged_console_write,
78 .window_size = not_configged_window_size,
79 .free = not_configged_free,
80 .winch = 0,
81};
82#endif /* CONFIG_NOCONFIG_CHAN */
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static void tty_receive_char(struct tty_struct *tty, char ch)
85{
Jeff Dikee99525f2007-10-16 01:26:41 -070086 if (tty == NULL)
87 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Jeff Dikee99525f2007-10-16 01:26:41 -070089 if (I_IXON(tty) && !I_IXOFF(tty) && !tty->raw) {
90 if (ch == STOP_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 stop_tty(tty);
92 return;
93 }
Jeff Dikee99525f2007-10-16 01:26:41 -070094 else if (ch == START_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 start_tty(tty);
96 return;
97 }
98 }
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 tty_insert_flip_char(tty, ch, TTY_NORMAL);
101}
102
Jeff Diked50084a2006-01-06 00:18:50 -0800103static int open_one_chan(struct chan *chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Jeff Dike6676ae62007-07-31 00:37:44 -0700105 int fd, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Jeff Dikee99525f2007-10-16 01:26:41 -0700107 if (chan->opened)
Jeff Diked50084a2006-01-06 00:18:50 -0800108 return 0;
109
Jeff Dikee99525f2007-10-16 01:26:41 -0700110 if (chan->ops->open == NULL)
Jeff Diked50084a2006-01-06 00:18:50 -0800111 fd = 0;
112 else fd = (*chan->ops->open)(chan->input, chan->output, chan->primary,
113 chan->data, &chan->dev);
Jeff Dikee99525f2007-10-16 01:26:41 -0700114 if (fd < 0)
Jeff Diked50084a2006-01-06 00:18:50 -0800115 return fd;
Jeff Dike6676ae62007-07-31 00:37:44 -0700116
117 err = os_set_fd_block(fd, 0);
118 if (err) {
119 (*chan->ops->close)(fd, chan->data);
120 return err;
121 }
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 chan->fd = fd;
124
125 chan->opened = 1;
Jeff Diked50084a2006-01-06 00:18:50 -0800126 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
WANG Cong3af7cb72008-04-28 02:13:55 -0700129static int open_chan(struct list_head *chans)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 struct list_head *ele;
132 struct chan *chan;
133 int ret, err = 0;
134
Jeff Dikee99525f2007-10-16 01:26:41 -0700135 list_for_each(ele, chans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 chan = list_entry(ele, struct chan, list);
Jeff Diked50084a2006-01-06 00:18:50 -0800137 ret = open_one_chan(chan);
Jeff Dikee99525f2007-10-16 01:26:41 -0700138 if (chan->primary)
Jeff Diked50084a2006-01-06 00:18:50 -0800139 err = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
Jeff Diked50084a2006-01-06 00:18:50 -0800141 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
Al Virobed5e392011-09-08 10:49:34 -0400144void chan_enable_winch(struct chan *chan, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Al Virobed5e392011-09-08 10:49:34 -0400146 if (chan && chan->primary && chan->ops->winch)
147 register_winch(chan->fd, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Al Viro0fcd7192011-09-10 08:17:04 -0400150static void line_timer_cb(struct work_struct *work)
151{
152 struct line *line = container_of(work, struct line, task.work);
153
154 if (!line->throttled)
155 chan_interrupt(line, line->tty, line->driver->read_irq);
156}
157
Jeff Diked14ad812007-07-15 23:38:54 -0700158int enable_chan(struct line *line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 struct list_head *ele;
161 struct chan *chan;
Jeff Diked14ad812007-07-15 23:38:54 -0700162 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Al Viro0fcd7192011-09-10 08:17:04 -0400164 INIT_DELAYED_WORK(&line->task, line_timer_cb);
165
Jeff Dikee99525f2007-10-16 01:26:41 -0700166 list_for_each(ele, &line->chan_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 chan = list_entry(ele, struct chan, list);
Jeff Diked14ad812007-07-15 23:38:54 -0700168 err = open_one_chan(chan);
169 if (err) {
170 if (chan->primary)
171 goto out_close;
172
Jeff Dike165dc592006-01-06 00:18:57 -0800173 continue;
Jeff Diked14ad812007-07-15 23:38:54 -0700174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Jeff Dikee99525f2007-10-16 01:26:41 -0700176 if (chan->enabled)
Jeff Dike165dc592006-01-06 00:18:57 -0800177 continue;
Jeff Diked14ad812007-07-15 23:38:54 -0700178 err = line_setup_irq(chan->fd, chan->input, chan->output, line,
179 chan);
180 if (err)
181 goto out_close;
182
Jeff Dike165dc592006-01-06 00:18:57 -0800183 chan->enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
Jeff Diked14ad812007-07-15 23:38:54 -0700185
186 return 0;
187
188 out_close:
Al Viro10c890c02011-09-10 08:39:18 -0400189 close_chan(line);
Jeff Diked14ad812007-07-15 23:38:54 -0700190 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Jeff Dike190c3e42007-02-10 01:43:55 -0800193/* Items are added in IRQ context, when free_irq can't be called, and
194 * removed in process context, when it can.
195 * This handles interrupt sources which disappear, and which need to
196 * be permanently disabled. This is discovered in IRQ context, but
197 * the freeing of the IRQ must be done later.
198 */
199static DEFINE_SPINLOCK(irqs_to_free_lock);
Jeff Dike165dc592006-01-06 00:18:57 -0800200static LIST_HEAD(irqs_to_free);
201
202void free_irqs(void)
203{
204 struct chan *chan;
Jeff Dike190c3e42007-02-10 01:43:55 -0800205 LIST_HEAD(list);
206 struct list_head *ele;
Jeff Dike30762122007-03-29 01:20:30 -0700207 unsigned long flags;
Jeff Dike165dc592006-01-06 00:18:57 -0800208
Jeff Dike30762122007-03-29 01:20:30 -0700209 spin_lock_irqsave(&irqs_to_free_lock, flags);
Jeff Dike190c3e42007-02-10 01:43:55 -0800210 list_splice_init(&irqs_to_free, &list);
Jeff Dike30762122007-03-29 01:20:30 -0700211 spin_unlock_irqrestore(&irqs_to_free_lock, flags);
Jeff Dike190c3e42007-02-10 01:43:55 -0800212
Jeff Dikee99525f2007-10-16 01:26:41 -0700213 list_for_each(ele, &list) {
Jeff Dike190c3e42007-02-10 01:43:55 -0800214 chan = list_entry(ele, struct chan, free_list);
Jeff Dike165dc592006-01-06 00:18:57 -0800215
Richard Weinberger47562272010-08-09 17:20:14 -0700216 if (chan->input && chan->enabled)
Richard Weinbergerfa7a0442012-04-17 22:37:13 +0200217 um_free_irq(chan->line->driver->read_irq, chan);
Richard Weinberger47562272010-08-09 17:20:14 -0700218 if (chan->output && chan->enabled)
Richard Weinbergerfa7a0442012-04-17 22:37:13 +0200219 um_free_irq(chan->line->driver->write_irq, chan);
Jeff Dike165dc592006-01-06 00:18:57 -0800220 chan->enabled = 0;
221 }
222}
223
224static void close_one_chan(struct chan *chan, int delay_free_irq)
225{
Jeff Dike30762122007-03-29 01:20:30 -0700226 unsigned long flags;
227
Jeff Dikee99525f2007-10-16 01:26:41 -0700228 if (!chan->opened)
Jeff Dike165dc592006-01-06 00:18:57 -0800229 return;
230
Jeff Dikee99525f2007-10-16 01:26:41 -0700231 if (delay_free_irq) {
Jeff Dike30762122007-03-29 01:20:30 -0700232 spin_lock_irqsave(&irqs_to_free_lock, flags);
Jeff Dike165dc592006-01-06 00:18:57 -0800233 list_add(&chan->free_list, &irqs_to_free);
Jeff Dike30762122007-03-29 01:20:30 -0700234 spin_unlock_irqrestore(&irqs_to_free_lock, flags);
Jeff Dike165dc592006-01-06 00:18:57 -0800235 }
236 else {
Richard Weinberger47562272010-08-09 17:20:14 -0700237 if (chan->input && chan->enabled)
Richard Weinbergerfa7a0442012-04-17 22:37:13 +0200238 um_free_irq(chan->line->driver->read_irq, chan);
Richard Weinberger47562272010-08-09 17:20:14 -0700239 if (chan->output && chan->enabled)
Richard Weinbergerfa7a0442012-04-17 22:37:13 +0200240 um_free_irq(chan->line->driver->write_irq, chan);
Jeff Dike165dc592006-01-06 00:18:57 -0800241 chan->enabled = 0;
242 }
Jeff Dikee99525f2007-10-16 01:26:41 -0700243 if (chan->ops->close != NULL)
Jeff Dike165dc592006-01-06 00:18:57 -0800244 (*chan->ops->close)(chan->fd, chan->data);
245
246 chan->opened = 0;
247 chan->fd = -1;
248}
249
Al Viro10c890c02011-09-10 08:39:18 -0400250void close_chan(struct line *line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 struct chan *chan;
253
254 /* Close in reverse order as open in case more than one of them
255 * refers to the same device and they save and restore that device's
256 * state. Then, the first one opened will have the original state,
257 * so it must be the last closed.
258 */
Al Viro10c890c02011-09-10 08:39:18 -0400259 list_for_each_entry_reverse(chan, &line->chan_list, list) {
260 close_one_chan(chan, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262}
263
Al Virobed5e392011-09-08 10:49:34 -0400264void deactivate_chan(struct chan *chan, int irq)
Jeff Dikee4dcee82006-01-06 00:18:58 -0800265{
Al Virobed5e392011-09-08 10:49:34 -0400266 if (chan && chan->enabled)
267 deactivate_fd(chan->fd, irq);
Jeff Dikee4dcee82006-01-06 00:18:58 -0800268}
269
Al Virobed5e392011-09-08 10:49:34 -0400270void reactivate_chan(struct chan *chan, int irq)
Jeff Dikee4dcee82006-01-06 00:18:58 -0800271{
Al Virobed5e392011-09-08 10:49:34 -0400272 if (chan && chan->enabled)
273 reactivate_fd(chan->fd, irq);
Jeff Dikee4dcee82006-01-06 00:18:58 -0800274}
275
Al Virobed5e392011-09-08 10:49:34 -0400276int write_chan(struct chan *chan, const char *buf, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 int write_irq)
278{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 int n, ret = 0;
280
Al Virobed5e392011-09-08 10:49:34 -0400281 if (len == 0 || !chan || !chan->ops->write)
Jeff Dikec59dbca2007-10-16 01:26:42 -0700282 return 0;
283
Al Virobed5e392011-09-08 10:49:34 -0400284 n = chan->ops->write(chan->fd, buf, len, chan->data);
285 if (chan->primary) {
286 ret = n;
287 if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
288 reactivate_fd(chan->fd, write_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
Jeff Diked50084a2006-01-06 00:18:50 -0800290 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Al Virobed5e392011-09-08 10:49:34 -0400293int console_write_chan(struct chan *chan, const char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 int n, ret = 0;
296
Al Virobed5e392011-09-08 10:49:34 -0400297 if (!chan || !chan->ops->console_write)
298 return 0;
Jeff Dikee99525f2007-10-16 01:26:41 -0700299
Al Virobed5e392011-09-08 10:49:34 -0400300 n = chan->ops->console_write(chan->fd, buf, len);
301 if (chan->primary)
302 ret = n;
Jeff Diked50084a2006-01-06 00:18:50 -0800303 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Jeff Dikea52f3622007-02-10 01:44:06 -0800306int console_open_chan(struct line *line, struct console *co)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Jeff Dike1f801712006-01-06 00:18:55 -0800308 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Jeff Dike1f801712006-01-06 00:18:55 -0800310 err = open_chan(&line->chan_list);
Jeff Dikee99525f2007-10-16 01:26:41 -0700311 if (err)
Jeff Dike1f801712006-01-06 00:18:55 -0800312 return err;
313
Jeff Dikee99525f2007-10-16 01:26:41 -0700314 printk(KERN_INFO "Console initialized on /dev/%s%d\n", co->name,
315 co->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return 0;
317}
318
Al Virobed5e392011-09-08 10:49:34 -0400319int chan_window_size(struct line *line, unsigned short *rows_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 unsigned short *cols_out)
321{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 struct chan *chan;
323
Al Virobed5e392011-09-08 10:49:34 -0400324 chan = line->chan_in;
325 if (chan && chan->primary) {
326 if (chan->ops->window_size == NULL)
327 return 0;
328 return chan->ops->window_size(chan->fd, chan->data,
329 rows_out, cols_out);
330 }
331 chan = line->chan_out;
332 if (chan && chan->primary) {
333 if (chan->ops->window_size == NULL)
334 return 0;
335 return chan->ops->window_size(chan->fd, chan->data,
336 rows_out, cols_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
Jeff Diked50084a2006-01-06 00:18:50 -0800338 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
Al Viro772bd0a2011-08-18 20:12:39 +0100341static void free_one_chan(struct chan *chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 list_del(&chan->list);
Jeff Dike165dc592006-01-06 00:18:57 -0800344
Al Viro772bd0a2011-08-18 20:12:39 +0100345 close_one_chan(chan, 0);
Jeff Dike165dc592006-01-06 00:18:57 -0800346
Jeff Dikee99525f2007-10-16 01:26:41 -0700347 if (chan->ops->free != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 (*chan->ops->free)(chan->data);
Jeff Dike165dc592006-01-06 00:18:57 -0800349
Jeff Dikee99525f2007-10-16 01:26:41 -0700350 if (chan->primary && chan->output)
351 ignore_sigio_fd(chan->fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 kfree(chan);
353}
354
Al Viro772bd0a2011-08-18 20:12:39 +0100355static void free_chan(struct list_head *chans)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 struct list_head *ele, *next;
358 struct chan *chan;
359
Jeff Dikee99525f2007-10-16 01:26:41 -0700360 list_for_each_safe(ele, next, chans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 chan = list_entry(ele, struct chan, list);
Al Viro772bd0a2011-08-18 20:12:39 +0100362 free_one_chan(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364}
365
366static int one_chan_config_string(struct chan *chan, char *str, int size,
367 char **error_out)
368{
369 int n = 0;
370
Jeff Dikee99525f2007-10-16 01:26:41 -0700371 if (chan == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 CONFIG_CHUNK(str, size, n, "none", 1);
Jeff Diked50084a2006-01-06 00:18:50 -0800373 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
376 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
377
Jeff Dikee99525f2007-10-16 01:26:41 -0700378 if (chan->dev == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 CONFIG_CHUNK(str, size, n, "", 1);
Jeff Diked50084a2006-01-06 00:18:50 -0800380 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382
383 CONFIG_CHUNK(str, size, n, ":", 0);
384 CONFIG_CHUNK(str, size, n, chan->dev, 0);
385
Jeff Diked50084a2006-01-06 00:18:50 -0800386 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
Jeff Diked50084a2006-01-06 00:18:50 -0800389static int chan_pair_config_string(struct chan *in, struct chan *out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 char *str, int size, char **error_out)
391{
392 int n;
393
394 n = one_chan_config_string(in, str, size, error_out);
395 str += n;
396 size -= n;
397
Jeff Dikee99525f2007-10-16 01:26:41 -0700398 if (in == out) {
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, ",", 1);
404 n = one_chan_config_string(out, str, size, error_out);
405 str += n;
406 size -= n;
407 CONFIG_CHUNK(str, size, n, "", 1);
408
Jeff Diked50084a2006-01-06 00:18:50 -0800409 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
Al Virobed5e392011-09-08 10:49:34 -0400412int chan_config_string(struct line *line, char *str, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 char **error_out)
414{
Al Virobed5e392011-09-08 10:49:34 -0400415 struct chan *in = line->chan_in, *out = line->chan_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Al Virobed5e392011-09-08 10:49:34 -0400417 if (in && !in->primary)
418 in = NULL;
419 if (out && !out->primary)
420 out = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Jeff Diked50084a2006-01-06 00:18:50 -0800422 return chan_pair_config_string(in, out, str, size, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
425struct chan_type {
426 char *key;
Jeff Dike5e7672e2006-09-27 01:50:33 -0700427 const struct chan_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428};
429
Jeff Dike5e7672e2006-09-27 01:50:33 -0700430static const struct chan_type chan_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 { "fd", &fd_ops },
432
433#ifdef CONFIG_NULL_CHAN
434 { "null", &null_ops },
435#else
436 { "null", &not_configged_ops },
437#endif
438
439#ifdef CONFIG_PORT_CHAN
440 { "port", &port_ops },
441#else
442 { "port", &not_configged_ops },
443#endif
444
445#ifdef CONFIG_PTY_CHAN
446 { "pty", &pty_ops },
447 { "pts", &pts_ops },
448#else
449 { "pty", &not_configged_ops },
450 { "pts", &not_configged_ops },
451#endif
452
453#ifdef CONFIG_TTY_CHAN
454 { "tty", &tty_ops },
455#else
456 { "tty", &not_configged_ops },
457#endif
458
459#ifdef CONFIG_XTERM_CHAN
460 { "xterm", &xterm_ops },
461#else
462 { "xterm", &not_configged_ops },
463#endif
464};
465
Jeff Dike165dc592006-01-06 00:18:57 -0800466static struct chan *parse_chan(struct line *line, char *str, int device,
Jeff Dikef28169d2007-02-10 01:43:53 -0800467 const struct chan_opts *opts, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Jeff Dike5e7672e2006-09-27 01:50:33 -0700469 const struct chan_type *entry;
470 const struct chan_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 struct chan *chan;
472 void *data;
473 int i;
474
475 ops = NULL;
476 data = NULL;
Jeff Dikee99525f2007-10-16 01:26:41 -0700477 for(i = 0; i < ARRAY_SIZE(chan_table); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 entry = &chan_table[i];
Jeff Dikee99525f2007-10-16 01:26:41 -0700479 if (!strncmp(str, entry->key, strlen(entry->key))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 ops = entry->ops;
481 str += strlen(entry->key);
482 break;
483 }
484 }
Jeff Dikee99525f2007-10-16 01:26:41 -0700485 if (ops == NULL) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800486 *error_out = "No match for configured backends";
Jeff Diked50084a2006-01-06 00:18:50 -0800487 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 data = (*ops->init)(str, device, opts);
Jeff Dikee99525f2007-10-16 01:26:41 -0700491 if (data == NULL) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800492 *error_out = "Configuration failed";
Jeff Diked50084a2006-01-06 00:18:50 -0800493 return NULL;
Jeff Dikef28169d2007-02-10 01:43:53 -0800494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Paolo 'Blaisorblade' Giarrusso79ae2cb2005-09-22 21:44:21 -0700496 chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
Jeff Dikee99525f2007-10-16 01:26:41 -0700497 if (chan == NULL) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800498 *error_out = "Memory allocation failed";
Jeff Diked50084a2006-01-06 00:18:50 -0800499 return NULL;
Jeff Dikef28169d2007-02-10 01:43:53 -0800500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
Jeff Dike165dc592006-01-06 00:18:57 -0800502 .free_list =
503 LIST_HEAD_INIT(chan->free_list),
504 .line = line,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 .primary = 1,
506 .input = 0,
507 .output = 0,
508 .opened = 0,
Jeff Dike165dc592006-01-06 00:18:57 -0800509 .enabled = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 .fd = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 .ops = ops,
512 .data = data });
Jeff Diked50084a2006-01-06 00:18:50 -0800513 return chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Jeff Dike165dc592006-01-06 00:18:57 -0800516int parse_chan_pair(char *str, struct line *line, int device,
Jeff Dikef28169d2007-02-10 01:43:53 -0800517 const struct chan_opts *opts, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Jeff Dike165dc592006-01-06 00:18:57 -0800519 struct list_head *chans = &line->chan_list;
Richard Weinbergerf1c93e42011-07-25 17:12:55 -0700520 struct chan *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 char *in, *out;
522
Jeff Dikee99525f2007-10-16 01:26:41 -0700523 if (!list_empty(chans)) {
Al Viroee485072011-09-08 07:07:26 -0400524 line->chan_in = line->chan_out = NULL;
Al Viro772bd0a2011-08-18 20:12:39 +0100525 free_chan(chans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 INIT_LIST_HEAD(chans);
527 }
528
Al Viro31efceb2011-09-09 19:14:02 -0400529 if (!str)
530 return 0;
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 out = strchr(str, ',');
Jeff Dikee99525f2007-10-16 01:26:41 -0700533 if (out != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 in = str;
535 *out = '\0';
536 out++;
Jeff Dikef28169d2007-02-10 01:43:53 -0800537 new = parse_chan(line, in, device, opts, error_out);
Jeff Dikee99525f2007-10-16 01:26:41 -0700538 if (new == NULL)
Jeff Diked50084a2006-01-06 00:18:50 -0800539 return -1;
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 new->input = 1;
542 list_add(&new->list, chans);
Al Viroee485072011-09-08 07:07:26 -0400543 line->chan_in = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Jeff Dikef28169d2007-02-10 01:43:53 -0800545 new = parse_chan(line, out, device, opts, error_out);
Jeff Dikee99525f2007-10-16 01:26:41 -0700546 if (new == NULL)
Jeff Diked50084a2006-01-06 00:18:50 -0800547 return -1;
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 list_add(&new->list, chans);
550 new->output = 1;
Al Viroee485072011-09-08 07:07:26 -0400551 line->chan_out = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553 else {
Jeff Dikef28169d2007-02-10 01:43:53 -0800554 new = parse_chan(line, str, device, opts, error_out);
Jeff Dikee99525f2007-10-16 01:26:41 -0700555 if (new == NULL)
Jeff Diked50084a2006-01-06 00:18:50 -0800556 return -1;
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 list_add(&new->list, chans);
559 new->input = 1;
560 new->output = 1;
Al Viroee485072011-09-08 07:07:26 -0400561 line->chan_in = line->chan_out = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
Jeff Diked50084a2006-01-06 00:18:50 -0800563 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Al Viro0fcd7192011-09-10 08:17:04 -0400566void chan_interrupt(struct line *line, struct tty_struct *tty, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Al Virobed5e392011-09-08 10:49:34 -0400568 struct chan *chan = line->chan_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 int err;
570 char c;
571
Al Virobed5e392011-09-08 10:49:34 -0400572 if (!chan || !chan->ops->read)
573 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Al Virobed5e392011-09-08 10:49:34 -0400575 do {
576 if (tty && !tty_buffer_request_room(tty, 1)) {
Al Viro0fcd7192011-09-10 08:17:04 -0400577 schedule_delayed_work(&line->task, 1);
Al Virobed5e392011-09-08 10:49:34 -0400578 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
Al Virobed5e392011-09-08 10:49:34 -0400580 err = chan->ops->read(chan->fd, &c, chan->data);
581 if (err > 0)
582 tty_receive_char(tty, c);
583 } while (err > 0);
584
585 if (err == 0)
586 reactivate_fd(chan->fd, irq);
587 if (err == -EIO) {
588 if (chan->primary) {
589 if (tty != NULL)
590 tty_hangup(tty);
Al Viro10c890c02011-09-10 08:39:18 -0400591 if (line->chan_out != chan)
592 close_one_chan(line->chan_out, 1);
Al Virobed5e392011-09-08 10:49:34 -0400593 }
Al Viro10c890c02011-09-10 08:39:18 -0400594 close_one_chan(chan, 1);
595 if (chan->primary)
596 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598 out:
Jeff Dikee99525f2007-10-16 01:26:41 -0700599 if (tty)
600 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}