blob: 368d3e97dfd9a85a918c5455fb63390f3f0ab197 [file] [log] [blame]
Jeff Dike165dc592006-01-06 00:18:57 -08001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6#include <linux/stddef.h>
7#include <linux/kernel.h>
8#include <linux/list.h>
9#include <linux/slab.h>
10#include <linux/tty.h>
11#include <linux/string.h>
12#include <linux/tty_flip.h>
13#include <asm/irq.h>
14#include "chan_kern.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "kern.h"
16#include "irq_user.h"
17#include "sigio.h"
18#include "line.h"
19#include "os.h"
20
Paolo 'Blaisorblade' Giarrussofac97ae2005-09-22 21:44:22 -070021#ifdef CONFIG_NOCONFIG_CHAN
Jeff Dikef28169d2007-02-10 01:43:53 -080022static void *not_configged_init(char *str, int device,
23 const struct chan_opts *opts)
Paolo 'Blaisorblade' Giarrussofac97ae2005-09-22 21:44:22 -070024{
Jeff Dikef28169d2007-02-10 01:43:53 -080025 printk("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 NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028}
29
30static int not_configged_open(int input, int output, int primary, void *data,
31 char **dev_out)
32{
Jeff Dikef28169d2007-02-10 01:43:53 -080033 printk("Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 "UML\n");
Jeff Diked50084a2006-01-06 00:18:50 -080035 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
38static void not_configged_close(int fd, void *data)
39{
Jeff Dikef28169d2007-02-10 01:43:53 -080040 printk("Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 "UML\n");
42}
43
44static int not_configged_read(int fd, char *c_out, void *data)
45{
Jeff Dikef28169d2007-02-10 01:43:53 -080046 printk("Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 "UML\n");
Jeff Diked50084a2006-01-06 00:18:50 -080048 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
51static int not_configged_write(int fd, const char *buf, int len, void *data)
52{
Jeff Dikef28169d2007-02-10 01:43:53 -080053 printk("Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 "UML\n");
Jeff Diked50084a2006-01-06 00:18:50 -080055 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
Paolo 'Blaisorblade' Giarrusso55c033c2005-11-13 16:07:11 -080058static int not_configged_console_write(int fd, const char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Jeff Dikef28169d2007-02-10 01:43:53 -080060 printk("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 -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
65static int not_configged_window_size(int fd, void *data, unsigned short *rows,
66 unsigned short *cols)
67{
Jeff Dikef28169d2007-02-10 01:43:53 -080068 printk("Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 "UML\n");
Jeff Diked50084a2006-01-06 00:18:50 -080070 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73static void not_configged_free(void *data)
74{
Jeff Dikef28169d2007-02-10 01:43:53 -080075 printk("Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 "UML\n");
77}
78
Jeff Dike5e7672e2006-09-27 01:50:33 -070079static const struct chan_ops not_configged_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 .init = not_configged_init,
81 .open = not_configged_open,
82 .close = not_configged_close,
83 .read = not_configged_read,
84 .write = not_configged_write,
85 .console_write = not_configged_console_write,
86 .window_size = not_configged_window_size,
87 .free = not_configged_free,
88 .winch = 0,
89};
90#endif /* CONFIG_NOCONFIG_CHAN */
91
92void generic_close(int fd, void *unused)
93{
94 os_close_file(fd);
95}
96
97int generic_read(int fd, char *c_out, void *unused)
98{
99 int n;
100
Jeff Dikea6ea4cc2007-05-06 14:51:43 -0700101 n = os_read_file(fd, c_out, sizeof(*c_out));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 if(n == -EAGAIN)
Jeff Diked50084a2006-01-06 00:18:50 -0800104 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 else if(n == 0)
Jeff Diked50084a2006-01-06 00:18:50 -0800106 return -EIO;
107 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
110/* XXX Trivial wrapper around os_write_file */
111
112int generic_write(int fd, const char *buf, int n, void *unused)
113{
Jeff Dikea6ea4cc2007-05-06 14:51:43 -0700114 return os_write_file(fd, buf, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117int generic_window_size(int fd, void *unused, unsigned short *rows_out,
118 unsigned short *cols_out)
119{
120 int rows, cols;
121 int ret;
122
123 ret = os_window_size(fd, &rows, &cols);
124 if(ret < 0)
Jeff Diked50084a2006-01-06 00:18:50 -0800125 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 ret = ((*rows_out != rows) || (*cols_out != cols));
128
129 *rows_out = rows;
130 *cols_out = cols;
131
Jeff Diked50084a2006-01-06 00:18:50 -0800132 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
135void generic_free(void *data)
136{
137 kfree(data);
138}
139
140static void tty_receive_char(struct tty_struct *tty, char ch)
141{
142 if(tty == NULL) return;
143
144 if(I_IXON(tty) && !I_IXOFF(tty) && !tty->raw) {
145 if(ch == STOP_CHAR(tty)){
146 stop_tty(tty);
147 return;
148 }
149 else if(ch == START_CHAR(tty)){
150 start_tty(tty);
151 return;
152 }
153 }
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 tty_insert_flip_char(tty, ch, TTY_NORMAL);
156}
157
Jeff Diked50084a2006-01-06 00:18:50 -0800158static int open_one_chan(struct chan *chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 int fd;
161
Jeff Diked50084a2006-01-06 00:18:50 -0800162 if(chan->opened)
163 return 0;
164
165 if(chan->ops->open == NULL)
166 fd = 0;
167 else fd = (*chan->ops->open)(chan->input, chan->output, chan->primary,
168 chan->data, &chan->dev);
169 if(fd < 0)
170 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 chan->fd = fd;
172
173 chan->opened = 1;
Jeff Diked50084a2006-01-06 00:18:50 -0800174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
177int open_chan(struct list_head *chans)
178{
179 struct list_head *ele;
180 struct chan *chan;
181 int ret, err = 0;
182
183 list_for_each(ele, chans){
184 chan = list_entry(ele, struct chan, list);
Jeff Diked50084a2006-01-06 00:18:50 -0800185 ret = open_one_chan(chan);
186 if(chan->primary)
187 err = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
Jeff Diked50084a2006-01-06 00:18:50 -0800189 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
192void chan_enable_winch(struct list_head *chans, struct tty_struct *tty)
193{
194 struct list_head *ele;
195 struct chan *chan;
196
197 list_for_each(ele, chans){
198 chan = list_entry(ele, struct chan, list);
199 if(chan->primary && chan->output && chan->ops->winch){
200 register_winch(chan->fd, tty);
201 return;
202 }
203 }
204}
205
Jeff Diked14ad812007-07-15 23:38:54 -0700206int enable_chan(struct line *line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 struct list_head *ele;
209 struct chan *chan;
Jeff Diked14ad812007-07-15 23:38:54 -0700210 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Jeff Dike165dc592006-01-06 00:18:57 -0800212 list_for_each(ele, &line->chan_list){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 chan = list_entry(ele, struct chan, list);
Jeff Diked14ad812007-07-15 23:38:54 -0700214 err = open_one_chan(chan);
215 if (err) {
216 if (chan->primary)
217 goto out_close;
218
Jeff Dike165dc592006-01-06 00:18:57 -0800219 continue;
Jeff Diked14ad812007-07-15 23:38:54 -0700220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Jeff Dike165dc592006-01-06 00:18:57 -0800222 if(chan->enabled)
223 continue;
Jeff Diked14ad812007-07-15 23:38:54 -0700224 err = line_setup_irq(chan->fd, chan->input, chan->output, line,
225 chan);
226 if (err)
227 goto out_close;
228
Jeff Dike165dc592006-01-06 00:18:57 -0800229 chan->enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
Jeff Diked14ad812007-07-15 23:38:54 -0700231
232 return 0;
233
234 out_close:
235 close_chan(&line->chan_list, 0);
236 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Jeff Dike190c3e42007-02-10 01:43:55 -0800239/* Items are added in IRQ context, when free_irq can't be called, and
240 * removed in process context, when it can.
241 * This handles interrupt sources which disappear, and which need to
242 * be permanently disabled. This is discovered in IRQ context, but
243 * the freeing of the IRQ must be done later.
244 */
245static DEFINE_SPINLOCK(irqs_to_free_lock);
Jeff Dike165dc592006-01-06 00:18:57 -0800246static LIST_HEAD(irqs_to_free);
247
248void free_irqs(void)
249{
250 struct chan *chan;
Jeff Dike190c3e42007-02-10 01:43:55 -0800251 LIST_HEAD(list);
252 struct list_head *ele;
Jeff Dike30762122007-03-29 01:20:30 -0700253 unsigned long flags;
Jeff Dike165dc592006-01-06 00:18:57 -0800254
Jeff Dike30762122007-03-29 01:20:30 -0700255 spin_lock_irqsave(&irqs_to_free_lock, flags);
Jeff Dike190c3e42007-02-10 01:43:55 -0800256 list_splice_init(&irqs_to_free, &list);
Jeff Dike30762122007-03-29 01:20:30 -0700257 spin_unlock_irqrestore(&irqs_to_free_lock, flags);
Jeff Dike190c3e42007-02-10 01:43:55 -0800258
259 list_for_each(ele, &list){
260 chan = list_entry(ele, struct chan, free_list);
Jeff Dike165dc592006-01-06 00:18:57 -0800261
262 if(chan->input)
263 free_irq(chan->line->driver->read_irq, chan);
264 if(chan->output)
265 free_irq(chan->line->driver->write_irq, chan);
266 chan->enabled = 0;
267 }
268}
269
270static void close_one_chan(struct chan *chan, int delay_free_irq)
271{
Jeff Dike30762122007-03-29 01:20:30 -0700272 unsigned long flags;
273
Jeff Dike165dc592006-01-06 00:18:57 -0800274 if(!chan->opened)
275 return;
276
277 if(delay_free_irq){
Jeff Dike30762122007-03-29 01:20:30 -0700278 spin_lock_irqsave(&irqs_to_free_lock, flags);
Jeff Dike165dc592006-01-06 00:18:57 -0800279 list_add(&chan->free_list, &irqs_to_free);
Jeff Dike30762122007-03-29 01:20:30 -0700280 spin_unlock_irqrestore(&irqs_to_free_lock, flags);
Jeff Dike165dc592006-01-06 00:18:57 -0800281 }
282 else {
283 if(chan->input)
284 free_irq(chan->line->driver->read_irq, chan);
285 if(chan->output)
286 free_irq(chan->line->driver->write_irq, chan);
287 chan->enabled = 0;
288 }
289 if(chan->ops->close != NULL)
290 (*chan->ops->close)(chan->fd, chan->data);
291
292 chan->opened = 0;
293 chan->fd = -1;
294}
295
296void close_chan(struct list_head *chans, int delay_free_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 struct chan *chan;
299
300 /* Close in reverse order as open in case more than one of them
301 * refers to the same device and they save and restore that device's
302 * state. Then, the first one opened will have the original state,
303 * so it must be the last closed.
304 */
305 list_for_each_entry_reverse(chan, chans, list) {
Jeff Dike165dc592006-01-06 00:18:57 -0800306 close_one_chan(chan, delay_free_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308}
309
Jeff Dikee4dcee82006-01-06 00:18:58 -0800310void deactivate_chan(struct list_head *chans, int irq)
311{
312 struct list_head *ele;
313
314 struct chan *chan;
315 list_for_each(ele, chans) {
316 chan = list_entry(ele, struct chan, list);
317
318 if(chan->enabled && chan->input)
319 deactivate_fd(chan->fd, irq);
320 }
321}
322
323void reactivate_chan(struct list_head *chans, int irq)
324{
325 struct list_head *ele;
326 struct chan *chan;
327
328 list_for_each(ele, chans) {
329 chan = list_entry(ele, struct chan, list);
330
331 if(chan->enabled && chan->input)
332 reactivate_fd(chan->fd, irq);
333 }
334}
335
Jeff Diked50084a2006-01-06 00:18:50 -0800336int write_chan(struct list_head *chans, const char *buf, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 int write_irq)
338{
339 struct list_head *ele;
340 struct chan *chan = NULL;
341 int n, ret = 0;
342
343 list_for_each(ele, chans) {
344 chan = list_entry(ele, struct chan, list);
345 if (!chan->output || (chan->ops->write == NULL))
346 continue;
347 n = chan->ops->write(chan->fd, buf, len, chan->data);
348 if (chan->primary) {
349 ret = n;
350 if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
351 reactivate_fd(chan->fd, write_irq);
352 }
353 }
Jeff Diked50084a2006-01-06 00:18:50 -0800354 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357int console_write_chan(struct list_head *chans, const char *buf, int len)
358{
359 struct list_head *ele;
360 struct chan *chan;
361 int n, ret = 0;
362
363 list_for_each(ele, chans){
364 chan = list_entry(ele, struct chan, list);
365 if(!chan->output || (chan->ops->console_write == NULL))
366 continue;
Paolo 'Blaisorblade' Giarrusso55c033c2005-11-13 16:07:11 -0800367 n = chan->ops->console_write(chan->fd, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if(chan->primary) ret = n;
369 }
Jeff Diked50084a2006-01-06 00:18:50 -0800370 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Jeff Dikea52f3622007-02-10 01:44:06 -0800373int console_open_chan(struct line *line, struct console *co)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Jeff Dike1f801712006-01-06 00:18:55 -0800375 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Jeff Dike1f801712006-01-06 00:18:55 -0800377 err = open_chan(&line->chan_list);
378 if(err)
379 return err;
380
Jeff Dikea52f3622007-02-10 01:44:06 -0800381 printk("Console initialized on /dev/%s%d\n", co->name, co->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return 0;
383}
384
385int chan_window_size(struct list_head *chans, unsigned short *rows_out,
386 unsigned short *cols_out)
387{
388 struct list_head *ele;
389 struct chan *chan;
390
391 list_for_each(ele, chans){
392 chan = list_entry(ele, struct chan, list);
393 if(chan->primary){
Jeff Diked50084a2006-01-06 00:18:50 -0800394 if(chan->ops->window_size == NULL)
395 return 0;
396 return chan->ops->window_size(chan->fd, chan->data,
397 rows_out, cols_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399 }
Jeff Diked50084a2006-01-06 00:18:50 -0800400 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Paolo 'Blaisorblade' Giarrusso42947cb2006-02-01 03:06:29 -0800403static void free_one_chan(struct chan *chan, int delay_free_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 list_del(&chan->list);
Jeff Dike165dc592006-01-06 00:18:57 -0800406
407 close_one_chan(chan, delay_free_irq);
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if(chan->ops->free != NULL)
410 (*chan->ops->free)(chan->data);
Jeff Dike165dc592006-01-06 00:18:57 -0800411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
413 kfree(chan);
414}
415
Paolo 'Blaisorblade' Giarrusso42947cb2006-02-01 03:06:29 -0800416static void free_chan(struct list_head *chans, int delay_free_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 struct list_head *ele, *next;
419 struct chan *chan;
420
421 list_for_each_safe(ele, next, chans){
422 chan = list_entry(ele, struct chan, list);
Jeff Dike165dc592006-01-06 00:18:57 -0800423 free_one_chan(chan, delay_free_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425}
426
427static int one_chan_config_string(struct chan *chan, char *str, int size,
428 char **error_out)
429{
430 int n = 0;
431
432 if(chan == NULL){
433 CONFIG_CHUNK(str, size, n, "none", 1);
Jeff Diked50084a2006-01-06 00:18:50 -0800434 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436
437 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
438
439 if(chan->dev == NULL){
440 CONFIG_CHUNK(str, size, n, "", 1);
Jeff Diked50084a2006-01-06 00:18:50 -0800441 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443
444 CONFIG_CHUNK(str, size, n, ":", 0);
445 CONFIG_CHUNK(str, size, n, chan->dev, 0);
446
Jeff Diked50084a2006-01-06 00:18:50 -0800447 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Jeff Diked50084a2006-01-06 00:18:50 -0800450static int chan_pair_config_string(struct chan *in, struct chan *out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 char *str, int size, char **error_out)
452{
453 int n;
454
455 n = one_chan_config_string(in, str, size, error_out);
456 str += n;
457 size -= n;
458
459 if(in == out){
460 CONFIG_CHUNK(str, size, n, "", 1);
Jeff Diked50084a2006-01-06 00:18:50 -0800461 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463
464 CONFIG_CHUNK(str, size, n, ",", 1);
465 n = one_chan_config_string(out, str, size, error_out);
466 str += n;
467 size -= n;
468 CONFIG_CHUNK(str, size, n, "", 1);
469
Jeff Diked50084a2006-01-06 00:18:50 -0800470 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Jeff Diked50084a2006-01-06 00:18:50 -0800473int chan_config_string(struct list_head *chans, char *str, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 char **error_out)
475{
476 struct list_head *ele;
477 struct chan *chan, *in = NULL, *out = NULL;
478
479 list_for_each(ele, chans){
480 chan = list_entry(ele, struct chan, list);
481 if(!chan->primary)
482 continue;
483 if(chan->input)
484 in = chan;
485 if(chan->output)
486 out = chan;
487 }
488
Jeff Diked50084a2006-01-06 00:18:50 -0800489 return chan_pair_config_string(in, out, str, size, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492struct chan_type {
493 char *key;
Jeff Dike5e7672e2006-09-27 01:50:33 -0700494 const struct chan_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495};
496
Jeff Dike5e7672e2006-09-27 01:50:33 -0700497static const struct chan_type chan_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 { "fd", &fd_ops },
499
500#ifdef CONFIG_NULL_CHAN
501 { "null", &null_ops },
502#else
503 { "null", &not_configged_ops },
504#endif
505
506#ifdef CONFIG_PORT_CHAN
507 { "port", &port_ops },
508#else
509 { "port", &not_configged_ops },
510#endif
511
512#ifdef CONFIG_PTY_CHAN
513 { "pty", &pty_ops },
514 { "pts", &pts_ops },
515#else
516 { "pty", &not_configged_ops },
517 { "pts", &not_configged_ops },
518#endif
519
520#ifdef CONFIG_TTY_CHAN
521 { "tty", &tty_ops },
522#else
523 { "tty", &not_configged_ops },
524#endif
525
526#ifdef CONFIG_XTERM_CHAN
527 { "xterm", &xterm_ops },
528#else
529 { "xterm", &not_configged_ops },
530#endif
531};
532
Jeff Dike165dc592006-01-06 00:18:57 -0800533static struct chan *parse_chan(struct line *line, char *str, int device,
Jeff Dikef28169d2007-02-10 01:43:53 -0800534 const struct chan_opts *opts, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Jeff Dike5e7672e2006-09-27 01:50:33 -0700536 const struct chan_type *entry;
537 const struct chan_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 struct chan *chan;
539 void *data;
540 int i;
541
542 ops = NULL;
543 data = NULL;
Jeff Dike91b165c2006-09-25 23:33:00 -0700544 for(i = 0; i < ARRAY_SIZE(chan_table); i++){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 entry = &chan_table[i];
546 if(!strncmp(str, entry->key, strlen(entry->key))){
547 ops = entry->ops;
548 str += strlen(entry->key);
549 break;
550 }
551 }
552 if(ops == NULL){
Jeff Dikef28169d2007-02-10 01:43:53 -0800553 *error_out = "No match for configured backends";
Jeff Diked50084a2006-01-06 00:18:50 -0800554 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 data = (*ops->init)(str, device, opts);
Jeff Dikef28169d2007-02-10 01:43:53 -0800558 if(data == NULL){
559 *error_out = "Configuration failed";
Jeff Diked50084a2006-01-06 00:18:50 -0800560 return NULL;
Jeff Dikef28169d2007-02-10 01:43:53 -0800561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Paolo 'Blaisorblade' Giarrusso79ae2cb2005-09-22 21:44:21 -0700563 chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
Jeff Dikef28169d2007-02-10 01:43:53 -0800564 if(chan == NULL){
565 *error_out = "Memory allocation failed";
Jeff Diked50084a2006-01-06 00:18:50 -0800566 return NULL;
Jeff Dikef28169d2007-02-10 01:43:53 -0800567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
Jeff Dike165dc592006-01-06 00:18:57 -0800569 .free_list =
570 LIST_HEAD_INIT(chan->free_list),
571 .line = line,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 .primary = 1,
573 .input = 0,
574 .output = 0,
575 .opened = 0,
Jeff Dike165dc592006-01-06 00:18:57 -0800576 .enabled = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 .fd = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 .ops = ops,
579 .data = data });
Jeff Diked50084a2006-01-06 00:18:50 -0800580 return chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
Jeff Dike165dc592006-01-06 00:18:57 -0800583int parse_chan_pair(char *str, struct line *line, int device,
Jeff Dikef28169d2007-02-10 01:43:53 -0800584 const struct chan_opts *opts, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Jeff Dike165dc592006-01-06 00:18:57 -0800586 struct list_head *chans = &line->chan_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 struct chan *new, *chan;
588 char *in, *out;
589
590 if(!list_empty(chans)){
591 chan = list_entry(chans->next, struct chan, list);
Jeff Dike165dc592006-01-06 00:18:57 -0800592 free_chan(chans, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 INIT_LIST_HEAD(chans);
594 }
595
596 out = strchr(str, ',');
597 if(out != NULL){
598 in = str;
599 *out = '\0';
600 out++;
Jeff Dikef28169d2007-02-10 01:43:53 -0800601 new = parse_chan(line, in, device, opts, error_out);
Jeff Diked50084a2006-01-06 00:18:50 -0800602 if(new == NULL)
603 return -1;
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 new->input = 1;
606 list_add(&new->list, chans);
607
Jeff Dikef28169d2007-02-10 01:43:53 -0800608 new = parse_chan(line, out, device, opts, error_out);
Jeff Diked50084a2006-01-06 00:18:50 -0800609 if(new == NULL)
610 return -1;
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 list_add(&new->list, chans);
613 new->output = 1;
614 }
615 else {
Jeff Dikef28169d2007-02-10 01:43:53 -0800616 new = parse_chan(line, str, device, opts, error_out);
Jeff Diked50084a2006-01-06 00:18:50 -0800617 if(new == NULL)
618 return -1;
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 list_add(&new->list, chans);
621 new->input = 1;
622 new->output = 1;
623 }
Jeff Diked50084a2006-01-06 00:18:50 -0800624 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
627int chan_out_fd(struct list_head *chans)
628{
629 struct list_head *ele;
630 struct chan *chan;
631
632 list_for_each(ele, chans){
633 chan = list_entry(ele, struct chan, list);
634 if(chan->primary && chan->output)
Jeff Diked50084a2006-01-06 00:18:50 -0800635 return chan->fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
Jeff Diked50084a2006-01-06 00:18:50 -0800637 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
David Howells6d5aefb2006-12-05 19:36:26 +0000640void chan_interrupt(struct list_head *chans, struct delayed_work *task,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct tty_struct *tty, int irq)
642{
643 struct list_head *ele, *next;
644 struct chan *chan;
645 int err;
646 char c;
647
648 list_for_each_safe(ele, next, chans){
649 chan = list_entry(ele, struct chan, list);
650 if(!chan->input || (chan->ops->read == NULL)) continue;
651 do {
Alan Cox33f0f882006-01-09 20:54:13 -0800652 if (tty && !tty_buffer_request_room(tty, 1)) {
Jeff Dike9159c9d2006-01-06 00:18:58 -0800653 schedule_delayed_work(task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 goto out;
655 }
656 err = chan->ops->read(chan->fd, &c, chan->data);
657 if(err > 0)
658 tty_receive_char(tty, c);
659 } while(err > 0);
660
661 if(err == 0) reactivate_fd(chan->fd, irq);
662 if(err == -EIO){
663 if(chan->primary){
664 if(tty != NULL)
665 tty_hangup(tty);
Jeff Dike165dc592006-01-06 00:18:57 -0800666 close_chan(chans, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return;
668 }
Jeff Dike165dc592006-01-06 00:18:57 -0800669 else close_one_chan(chan, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671 }
672 out:
673 if(tty) tty_flip_buffer_push(tty);
674}