blob: 3aa351611763cbcf625f5cc6a09b4f4aad41ea5f [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 Dike165dc592006-01-06 00:18:57 -0800206void enable_chan(struct line *line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 struct list_head *ele;
209 struct chan *chan;
210
Jeff Dike165dc592006-01-06 00:18:57 -0800211 list_for_each(ele, &line->chan_list){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 chan = list_entry(ele, struct chan, list);
Jeff Dike165dc592006-01-06 00:18:57 -0800213 if(open_one_chan(chan))
214 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Jeff Dike165dc592006-01-06 00:18:57 -0800216 if(chan->enabled)
217 continue;
218 line_setup_irq(chan->fd, chan->input, chan->output, line,
219 chan);
220 chan->enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222}
223
Jeff Dike190c3e42007-02-10 01:43:55 -0800224/* Items are added in IRQ context, when free_irq can't be called, and
225 * removed in process context, when it can.
226 * This handles interrupt sources which disappear, and which need to
227 * be permanently disabled. This is discovered in IRQ context, but
228 * the freeing of the IRQ must be done later.
229 */
230static DEFINE_SPINLOCK(irqs_to_free_lock);
Jeff Dike165dc592006-01-06 00:18:57 -0800231static LIST_HEAD(irqs_to_free);
232
233void free_irqs(void)
234{
235 struct chan *chan;
Jeff Dike190c3e42007-02-10 01:43:55 -0800236 LIST_HEAD(list);
237 struct list_head *ele;
Jeff Dike30762122007-03-29 01:20:30 -0700238 unsigned long flags;
Jeff Dike165dc592006-01-06 00:18:57 -0800239
Jeff Dike30762122007-03-29 01:20:30 -0700240 spin_lock_irqsave(&irqs_to_free_lock, flags);
Jeff Dike190c3e42007-02-10 01:43:55 -0800241 list_splice_init(&irqs_to_free, &list);
Jeff Dike30762122007-03-29 01:20:30 -0700242 spin_unlock_irqrestore(&irqs_to_free_lock, flags);
Jeff Dike190c3e42007-02-10 01:43:55 -0800243
244 list_for_each(ele, &list){
245 chan = list_entry(ele, struct chan, free_list);
Jeff Dike165dc592006-01-06 00:18:57 -0800246
247 if(chan->input)
248 free_irq(chan->line->driver->read_irq, chan);
249 if(chan->output)
250 free_irq(chan->line->driver->write_irq, chan);
251 chan->enabled = 0;
252 }
253}
254
255static void close_one_chan(struct chan *chan, int delay_free_irq)
256{
Jeff Dike30762122007-03-29 01:20:30 -0700257 unsigned long flags;
258
Jeff Dike165dc592006-01-06 00:18:57 -0800259 if(!chan->opened)
260 return;
261
262 if(delay_free_irq){
Jeff Dike30762122007-03-29 01:20:30 -0700263 spin_lock_irqsave(&irqs_to_free_lock, flags);
Jeff Dike165dc592006-01-06 00:18:57 -0800264 list_add(&chan->free_list, &irqs_to_free);
Jeff Dike30762122007-03-29 01:20:30 -0700265 spin_unlock_irqrestore(&irqs_to_free_lock, flags);
Jeff Dike165dc592006-01-06 00:18:57 -0800266 }
267 else {
268 if(chan->input)
269 free_irq(chan->line->driver->read_irq, chan);
270 if(chan->output)
271 free_irq(chan->line->driver->write_irq, chan);
272 chan->enabled = 0;
273 }
274 if(chan->ops->close != NULL)
275 (*chan->ops->close)(chan->fd, chan->data);
276
277 chan->opened = 0;
278 chan->fd = -1;
279}
280
281void close_chan(struct list_head *chans, int delay_free_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 struct chan *chan;
284
285 /* Close in reverse order as open in case more than one of them
286 * refers to the same device and they save and restore that device's
287 * state. Then, the first one opened will have the original state,
288 * so it must be the last closed.
289 */
290 list_for_each_entry_reverse(chan, chans, list) {
Jeff Dike165dc592006-01-06 00:18:57 -0800291 close_one_chan(chan, delay_free_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293}
294
Jeff Dikee4dcee82006-01-06 00:18:58 -0800295void deactivate_chan(struct list_head *chans, int irq)
296{
297 struct list_head *ele;
298
299 struct chan *chan;
300 list_for_each(ele, chans) {
301 chan = list_entry(ele, struct chan, list);
302
303 if(chan->enabled && chan->input)
304 deactivate_fd(chan->fd, irq);
305 }
306}
307
308void reactivate_chan(struct list_head *chans, int irq)
309{
310 struct list_head *ele;
311 struct chan *chan;
312
313 list_for_each(ele, chans) {
314 chan = list_entry(ele, struct chan, list);
315
316 if(chan->enabled && chan->input)
317 reactivate_fd(chan->fd, irq);
318 }
319}
320
Jeff Diked50084a2006-01-06 00:18:50 -0800321int write_chan(struct list_head *chans, const char *buf, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 int write_irq)
323{
324 struct list_head *ele;
325 struct chan *chan = NULL;
326 int n, ret = 0;
327
328 list_for_each(ele, chans) {
329 chan = list_entry(ele, struct chan, list);
330 if (!chan->output || (chan->ops->write == NULL))
331 continue;
332 n = chan->ops->write(chan->fd, buf, len, chan->data);
333 if (chan->primary) {
334 ret = n;
335 if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
336 reactivate_fd(chan->fd, write_irq);
337 }
338 }
Jeff Diked50084a2006-01-06 00:18:50 -0800339 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
342int console_write_chan(struct list_head *chans, const char *buf, int len)
343{
344 struct list_head *ele;
345 struct chan *chan;
346 int n, ret = 0;
347
348 list_for_each(ele, chans){
349 chan = list_entry(ele, struct chan, list);
350 if(!chan->output || (chan->ops->console_write == NULL))
351 continue;
Paolo 'Blaisorblade' Giarrusso55c033c2005-11-13 16:07:11 -0800352 n = chan->ops->console_write(chan->fd, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if(chan->primary) ret = n;
354 }
Jeff Diked50084a2006-01-06 00:18:50 -0800355 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
Jeff Dikea52f3622007-02-10 01:44:06 -0800358int console_open_chan(struct line *line, struct console *co)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Jeff Dike1f801712006-01-06 00:18:55 -0800360 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Jeff Dike1f801712006-01-06 00:18:55 -0800362 err = open_chan(&line->chan_list);
363 if(err)
364 return err;
365
Jeff Dikea52f3622007-02-10 01:44:06 -0800366 printk("Console initialized on /dev/%s%d\n", co->name, co->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return 0;
368}
369
370int chan_window_size(struct list_head *chans, unsigned short *rows_out,
371 unsigned short *cols_out)
372{
373 struct list_head *ele;
374 struct chan *chan;
375
376 list_for_each(ele, chans){
377 chan = list_entry(ele, struct chan, list);
378 if(chan->primary){
Jeff Diked50084a2006-01-06 00:18:50 -0800379 if(chan->ops->window_size == NULL)
380 return 0;
381 return chan->ops->window_size(chan->fd, chan->data,
382 rows_out, cols_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384 }
Jeff Diked50084a2006-01-06 00:18:50 -0800385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Paolo 'Blaisorblade' Giarrusso42947cb2006-02-01 03:06:29 -0800388static void free_one_chan(struct chan *chan, int delay_free_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 list_del(&chan->list);
Jeff Dike165dc592006-01-06 00:18:57 -0800391
392 close_one_chan(chan, delay_free_irq);
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if(chan->ops->free != NULL)
395 (*chan->ops->free)(chan->data);
Jeff Dike165dc592006-01-06 00:18:57 -0800396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
398 kfree(chan);
399}
400
Paolo 'Blaisorblade' Giarrusso42947cb2006-02-01 03:06:29 -0800401static void free_chan(struct list_head *chans, int delay_free_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 struct list_head *ele, *next;
404 struct chan *chan;
405
406 list_for_each_safe(ele, next, chans){
407 chan = list_entry(ele, struct chan, list);
Jeff Dike165dc592006-01-06 00:18:57 -0800408 free_one_chan(chan, delay_free_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410}
411
412static int one_chan_config_string(struct chan *chan, char *str, int size,
413 char **error_out)
414{
415 int n = 0;
416
417 if(chan == NULL){
418 CONFIG_CHUNK(str, size, n, "none", 1);
Jeff Diked50084a2006-01-06 00:18:50 -0800419 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
422 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
423
424 if(chan->dev == NULL){
425 CONFIG_CHUNK(str, size, n, "", 1);
Jeff Diked50084a2006-01-06 00:18:50 -0800426 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428
429 CONFIG_CHUNK(str, size, n, ":", 0);
430 CONFIG_CHUNK(str, size, n, chan->dev, 0);
431
Jeff Diked50084a2006-01-06 00:18:50 -0800432 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Jeff Diked50084a2006-01-06 00:18:50 -0800435static int chan_pair_config_string(struct chan *in, struct chan *out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 char *str, int size, char **error_out)
437{
438 int n;
439
440 n = one_chan_config_string(in, str, size, error_out);
441 str += n;
442 size -= n;
443
444 if(in == out){
445 CONFIG_CHUNK(str, size, n, "", 1);
Jeff Diked50084a2006-01-06 00:18:50 -0800446 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448
449 CONFIG_CHUNK(str, size, n, ",", 1);
450 n = one_chan_config_string(out, str, size, error_out);
451 str += n;
452 size -= n;
453 CONFIG_CHUNK(str, size, n, "", 1);
454
Jeff Diked50084a2006-01-06 00:18:50 -0800455 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Jeff Diked50084a2006-01-06 00:18:50 -0800458int chan_config_string(struct list_head *chans, char *str, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 char **error_out)
460{
461 struct list_head *ele;
462 struct chan *chan, *in = NULL, *out = NULL;
463
464 list_for_each(ele, chans){
465 chan = list_entry(ele, struct chan, list);
466 if(!chan->primary)
467 continue;
468 if(chan->input)
469 in = chan;
470 if(chan->output)
471 out = chan;
472 }
473
Jeff Diked50084a2006-01-06 00:18:50 -0800474 return chan_pair_config_string(in, out, str, size, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477struct chan_type {
478 char *key;
Jeff Dike5e7672e2006-09-27 01:50:33 -0700479 const struct chan_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480};
481
Jeff Dike5e7672e2006-09-27 01:50:33 -0700482static const struct chan_type chan_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 { "fd", &fd_ops },
484
485#ifdef CONFIG_NULL_CHAN
486 { "null", &null_ops },
487#else
488 { "null", &not_configged_ops },
489#endif
490
491#ifdef CONFIG_PORT_CHAN
492 { "port", &port_ops },
493#else
494 { "port", &not_configged_ops },
495#endif
496
497#ifdef CONFIG_PTY_CHAN
498 { "pty", &pty_ops },
499 { "pts", &pts_ops },
500#else
501 { "pty", &not_configged_ops },
502 { "pts", &not_configged_ops },
503#endif
504
505#ifdef CONFIG_TTY_CHAN
506 { "tty", &tty_ops },
507#else
508 { "tty", &not_configged_ops },
509#endif
510
511#ifdef CONFIG_XTERM_CHAN
512 { "xterm", &xterm_ops },
513#else
514 { "xterm", &not_configged_ops },
515#endif
516};
517
Jeff Dike165dc592006-01-06 00:18:57 -0800518static struct chan *parse_chan(struct line *line, char *str, int device,
Jeff Dikef28169d2007-02-10 01:43:53 -0800519 const struct chan_opts *opts, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Jeff Dike5e7672e2006-09-27 01:50:33 -0700521 const struct chan_type *entry;
522 const struct chan_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 struct chan *chan;
524 void *data;
525 int i;
526
527 ops = NULL;
528 data = NULL;
Jeff Dike91b165c2006-09-25 23:33:00 -0700529 for(i = 0; i < ARRAY_SIZE(chan_table); i++){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 entry = &chan_table[i];
531 if(!strncmp(str, entry->key, strlen(entry->key))){
532 ops = entry->ops;
533 str += strlen(entry->key);
534 break;
535 }
536 }
537 if(ops == NULL){
Jeff Dikef28169d2007-02-10 01:43:53 -0800538 *error_out = "No match for configured backends";
Jeff Diked50084a2006-01-06 00:18:50 -0800539 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 data = (*ops->init)(str, device, opts);
Jeff Dikef28169d2007-02-10 01:43:53 -0800543 if(data == NULL){
544 *error_out = "Configuration failed";
Jeff Diked50084a2006-01-06 00:18:50 -0800545 return NULL;
Jeff Dikef28169d2007-02-10 01:43:53 -0800546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Paolo 'Blaisorblade' Giarrusso79ae2cb2005-09-22 21:44:21 -0700548 chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
Jeff Dikef28169d2007-02-10 01:43:53 -0800549 if(chan == NULL){
550 *error_out = "Memory allocation failed";
Jeff Diked50084a2006-01-06 00:18:50 -0800551 return NULL;
Jeff Dikef28169d2007-02-10 01:43:53 -0800552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
Jeff Dike165dc592006-01-06 00:18:57 -0800554 .free_list =
555 LIST_HEAD_INIT(chan->free_list),
556 .line = line,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 .primary = 1,
558 .input = 0,
559 .output = 0,
560 .opened = 0,
Jeff Dike165dc592006-01-06 00:18:57 -0800561 .enabled = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 .fd = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 .ops = ops,
564 .data = data });
Jeff Diked50084a2006-01-06 00:18:50 -0800565 return chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
Jeff Dike165dc592006-01-06 00:18:57 -0800568int parse_chan_pair(char *str, struct line *line, int device,
Jeff Dikef28169d2007-02-10 01:43:53 -0800569 const struct chan_opts *opts, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Jeff Dike165dc592006-01-06 00:18:57 -0800571 struct list_head *chans = &line->chan_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 struct chan *new, *chan;
573 char *in, *out;
574
575 if(!list_empty(chans)){
576 chan = list_entry(chans->next, struct chan, list);
Jeff Dike165dc592006-01-06 00:18:57 -0800577 free_chan(chans, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 INIT_LIST_HEAD(chans);
579 }
580
581 out = strchr(str, ',');
582 if(out != NULL){
583 in = str;
584 *out = '\0';
585 out++;
Jeff Dikef28169d2007-02-10 01:43:53 -0800586 new = parse_chan(line, in, device, opts, error_out);
Jeff Diked50084a2006-01-06 00:18:50 -0800587 if(new == NULL)
588 return -1;
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 new->input = 1;
591 list_add(&new->list, chans);
592
Jeff Dikef28169d2007-02-10 01:43:53 -0800593 new = parse_chan(line, out, device, opts, error_out);
Jeff Diked50084a2006-01-06 00:18:50 -0800594 if(new == NULL)
595 return -1;
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 list_add(&new->list, chans);
598 new->output = 1;
599 }
600 else {
Jeff Dikef28169d2007-02-10 01:43:53 -0800601 new = parse_chan(line, str, 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 list_add(&new->list, chans);
606 new->input = 1;
607 new->output = 1;
608 }
Jeff Diked50084a2006-01-06 00:18:50 -0800609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
612int chan_out_fd(struct list_head *chans)
613{
614 struct list_head *ele;
615 struct chan *chan;
616
617 list_for_each(ele, chans){
618 chan = list_entry(ele, struct chan, list);
619 if(chan->primary && chan->output)
Jeff Diked50084a2006-01-06 00:18:50 -0800620 return chan->fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
Jeff Diked50084a2006-01-06 00:18:50 -0800622 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
David Howells6d5aefb2006-12-05 19:36:26 +0000625void chan_interrupt(struct list_head *chans, struct delayed_work *task,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 struct tty_struct *tty, int irq)
627{
628 struct list_head *ele, *next;
629 struct chan *chan;
630 int err;
631 char c;
632
633 list_for_each_safe(ele, next, chans){
634 chan = list_entry(ele, struct chan, list);
635 if(!chan->input || (chan->ops->read == NULL)) continue;
636 do {
Alan Cox33f0f882006-01-09 20:54:13 -0800637 if (tty && !tty_buffer_request_room(tty, 1)) {
Jeff Dike9159c9d2006-01-06 00:18:58 -0800638 schedule_delayed_work(task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 goto out;
640 }
641 err = chan->ops->read(chan->fd, &c, chan->data);
642 if(err > 0)
643 tty_receive_char(tty, c);
644 } while(err > 0);
645
646 if(err == 0) reactivate_fd(chan->fd, irq);
647 if(err == -EIO){
648 if(chan->primary){
649 if(tty != NULL)
650 tty_hangup(tty);
Jeff Dike165dc592006-01-06 00:18:57 -0800651 close_chan(chans, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return;
653 }
Jeff Dike165dc592006-01-06 00:18:57 -0800654 else close_one_chan(chan, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656 }
657 out:
658 if(tty) tty_flip_buffer_push(tty);
659}