blob: 7218c754505bf99a4a9a1888398d32d1b8aa849d [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"
15#include "user_util.h"
16#include "kern.h"
17#include "irq_user.h"
18#include "sigio.h"
19#include "line.h"
20#include "os.h"
21
Paolo 'Blaisorblade' Giarrussofac97ae2005-09-22 21:44:22 -070022/* XXX: could well be moved to somewhere else, if needed. */
23static int my_printf(const char * fmt, ...)
24 __attribute__ ((format (printf, 1, 2)));
Jeff Dike84ddaa82005-05-20 13:59:12 -070025
Paolo 'Blaisorblade' Giarrussofac97ae2005-09-22 21:44:22 -070026static int my_printf(const char * fmt, ...)
27{
28 /* Yes, can be called on atomic context.*/
29 char *buf = kmalloc(4096, GFP_ATOMIC);
30 va_list args;
31 int r;
32
33 if (!buf) {
34 /* We print directly fmt.
35 * Yes, yes, yes, feel free to complain. */
36 r = strlen(fmt);
37 } else {
38 va_start(args, fmt);
39 r = vsprintf(buf, fmt, args);
40 va_end(args);
41 fmt = buf;
42 }
43
44 if (r)
45 r = os_write_file(1, fmt, r);
46 return r;
47
48}
49
50#ifdef CONFIG_NOCONFIG_CHAN
51/* Despite its name, there's no added trailing newline. */
52static int my_puts(const char * buf)
53{
54 return os_write_file(1, buf, strlen(buf));
55}
Jeff Dike84ddaa82005-05-20 13:59:12 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static void *not_configged_init(char *str, int device, struct chan_opts *opts)
58{
Paolo 'Blaisorblade' Giarrussofac97ae2005-09-22 21:44:22 -070059 my_puts("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 NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
64static int not_configged_open(int input, int output, int primary, void *data,
65 char **dev_out)
66{
Paolo 'Blaisorblade' Giarrussofac97ae2005-09-22 21:44:22 -070067 my_puts("Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 "UML\n");
Jeff Diked50084a2006-01-06 00:18:50 -080069 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
72static void not_configged_close(int fd, void *data)
73{
Paolo 'Blaisorblade' Giarrussofac97ae2005-09-22 21:44:22 -070074 my_puts("Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 "UML\n");
76}
77
78static int not_configged_read(int fd, char *c_out, void *data)
79{
Paolo 'Blaisorblade' Giarrussofac97ae2005-09-22 21:44:22 -070080 my_puts("Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 "UML\n");
Jeff Diked50084a2006-01-06 00:18:50 -080082 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
85static int not_configged_write(int fd, const char *buf, int len, void *data)
86{
Paolo 'Blaisorblade' Giarrussofac97ae2005-09-22 21:44:22 -070087 my_puts("Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 "UML\n");
Jeff Diked50084a2006-01-06 00:18:50 -080089 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Paolo 'Blaisorblade' Giarrusso55c033c2005-11-13 16:07:11 -080092static int not_configged_console_write(int fd, const char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Paolo 'Blaisorblade' Giarrussofac97ae2005-09-22 21:44:22 -070094 my_puts("Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 "UML\n");
Jeff Diked50084a2006-01-06 00:18:50 -080096 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
99static int not_configged_window_size(int fd, void *data, unsigned short *rows,
100 unsigned short *cols)
101{
Paolo 'Blaisorblade' Giarrussofac97ae2005-09-22 21:44:22 -0700102 my_puts("Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 "UML\n");
Jeff Diked50084a2006-01-06 00:18:50 -0800104 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107static void not_configged_free(void *data)
108{
Paolo 'Blaisorblade' Giarrussofac97ae2005-09-22 21:44:22 -0700109 my_puts("Using a channel type which is configured out of "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 "UML\n");
111}
112
113static struct chan_ops not_configged_ops = {
114 .init = not_configged_init,
115 .open = not_configged_open,
116 .close = not_configged_close,
117 .read = not_configged_read,
118 .write = not_configged_write,
119 .console_write = not_configged_console_write,
120 .window_size = not_configged_window_size,
121 .free = not_configged_free,
122 .winch = 0,
123};
124#endif /* CONFIG_NOCONFIG_CHAN */
125
126void generic_close(int fd, void *unused)
127{
128 os_close_file(fd);
129}
130
131int generic_read(int fd, char *c_out, void *unused)
132{
133 int n;
134
135 n = os_read_file(fd, c_out, sizeof(*c_out));
136
137 if(n == -EAGAIN)
Jeff Diked50084a2006-01-06 00:18:50 -0800138 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 else if(n == 0)
Jeff Diked50084a2006-01-06 00:18:50 -0800140 return -EIO;
141 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144/* XXX Trivial wrapper around os_write_file */
145
146int generic_write(int fd, const char *buf, int n, void *unused)
147{
Jeff Diked50084a2006-01-06 00:18:50 -0800148 return os_write_file(fd, buf, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151int generic_window_size(int fd, void *unused, unsigned short *rows_out,
152 unsigned short *cols_out)
153{
154 int rows, cols;
155 int ret;
156
157 ret = os_window_size(fd, &rows, &cols);
158 if(ret < 0)
Jeff Diked50084a2006-01-06 00:18:50 -0800159 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 ret = ((*rows_out != rows) || (*cols_out != cols));
162
163 *rows_out = rows;
164 *cols_out = cols;
165
Jeff Diked50084a2006-01-06 00:18:50 -0800166 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169void generic_free(void *data)
170{
171 kfree(data);
172}
173
174static void tty_receive_char(struct tty_struct *tty, char ch)
175{
176 if(tty == NULL) return;
177
178 if(I_IXON(tty) && !I_IXOFF(tty) && !tty->raw) {
179 if(ch == STOP_CHAR(tty)){
180 stop_tty(tty);
181 return;
182 }
183 else if(ch == START_CHAR(tty)){
184 start_tty(tty);
185 return;
186 }
187 }
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 tty_insert_flip_char(tty, ch, TTY_NORMAL);
190}
191
Jeff Diked50084a2006-01-06 00:18:50 -0800192static int open_one_chan(struct chan *chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 int fd;
195
Jeff Diked50084a2006-01-06 00:18:50 -0800196 if(chan->opened)
197 return 0;
198
199 if(chan->ops->open == NULL)
200 fd = 0;
201 else fd = (*chan->ops->open)(chan->input, chan->output, chan->primary,
202 chan->data, &chan->dev);
203 if(fd < 0)
204 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 chan->fd = fd;
206
207 chan->opened = 1;
Jeff Diked50084a2006-01-06 00:18:50 -0800208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211int open_chan(struct list_head *chans)
212{
213 struct list_head *ele;
214 struct chan *chan;
215 int ret, err = 0;
216
217 list_for_each(ele, chans){
218 chan = list_entry(ele, struct chan, list);
Jeff Diked50084a2006-01-06 00:18:50 -0800219 ret = open_one_chan(chan);
220 if(chan->primary)
221 err = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
Jeff Diked50084a2006-01-06 00:18:50 -0800223 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
226void chan_enable_winch(struct list_head *chans, struct tty_struct *tty)
227{
228 struct list_head *ele;
229 struct chan *chan;
230
231 list_for_each(ele, chans){
232 chan = list_entry(ele, struct chan, list);
233 if(chan->primary && chan->output && chan->ops->winch){
234 register_winch(chan->fd, tty);
235 return;
236 }
237 }
238}
239
Jeff Dike165dc592006-01-06 00:18:57 -0800240void enable_chan(struct line *line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 struct list_head *ele;
243 struct chan *chan;
244
Jeff Dike165dc592006-01-06 00:18:57 -0800245 list_for_each(ele, &line->chan_list){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 chan = list_entry(ele, struct chan, list);
Jeff Dike165dc592006-01-06 00:18:57 -0800247 if(open_one_chan(chan))
248 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Jeff Dike165dc592006-01-06 00:18:57 -0800250 if(chan->enabled)
251 continue;
252 line_setup_irq(chan->fd, chan->input, chan->output, line,
253 chan);
254 chan->enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256}
257
Jeff Dike165dc592006-01-06 00:18:57 -0800258static LIST_HEAD(irqs_to_free);
259
260void free_irqs(void)
261{
262 struct chan *chan;
263
264 while(!list_empty(&irqs_to_free)){
265 chan = list_entry(irqs_to_free.next, struct chan, free_list);
266 list_del(&chan->free_list);
267
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}
275
276static void close_one_chan(struct chan *chan, int delay_free_irq)
277{
278 if(!chan->opened)
279 return;
280
281 if(delay_free_irq){
282 list_add(&chan->free_list, &irqs_to_free);
283 }
284 else {
285 if(chan->input)
286 free_irq(chan->line->driver->read_irq, chan);
287 if(chan->output)
288 free_irq(chan->line->driver->write_irq, chan);
289 chan->enabled = 0;
290 }
291 if(chan->ops->close != NULL)
292 (*chan->ops->close)(chan->fd, chan->data);
293
294 chan->opened = 0;
295 chan->fd = -1;
296}
297
298void close_chan(struct list_head *chans, int delay_free_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 struct chan *chan;
301
302 /* Close in reverse order as open in case more than one of them
303 * refers to the same device and they save and restore that device's
304 * state. Then, the first one opened will have the original state,
305 * so it must be the last closed.
306 */
307 list_for_each_entry_reverse(chan, chans, list) {
Jeff Dike165dc592006-01-06 00:18:57 -0800308 close_one_chan(chan, delay_free_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310}
311
Jeff Dikee4dcee82006-01-06 00:18:58 -0800312void deactivate_chan(struct list_head *chans, int irq)
313{
314 struct list_head *ele;
315
316 struct chan *chan;
317 list_for_each(ele, chans) {
318 chan = list_entry(ele, struct chan, list);
319
320 if(chan->enabled && chan->input)
321 deactivate_fd(chan->fd, irq);
322 }
323}
324
325void reactivate_chan(struct list_head *chans, int irq)
326{
327 struct list_head *ele;
328 struct chan *chan;
329
330 list_for_each(ele, chans) {
331 chan = list_entry(ele, struct chan, list);
332
333 if(chan->enabled && chan->input)
334 reactivate_fd(chan->fd, irq);
335 }
336}
337
Jeff Diked50084a2006-01-06 00:18:50 -0800338int write_chan(struct list_head *chans, const char *buf, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 int write_irq)
340{
341 struct list_head *ele;
342 struct chan *chan = NULL;
343 int n, ret = 0;
344
345 list_for_each(ele, chans) {
346 chan = list_entry(ele, struct chan, list);
347 if (!chan->output || (chan->ops->write == NULL))
348 continue;
349 n = chan->ops->write(chan->fd, buf, len, chan->data);
350 if (chan->primary) {
351 ret = n;
352 if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
353 reactivate_fd(chan->fd, write_irq);
354 }
355 }
Jeff Diked50084a2006-01-06 00:18:50 -0800356 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
359int console_write_chan(struct list_head *chans, const char *buf, int len)
360{
361 struct list_head *ele;
362 struct chan *chan;
363 int n, ret = 0;
364
365 list_for_each(ele, chans){
366 chan = list_entry(ele, struct chan, list);
367 if(!chan->output || (chan->ops->console_write == NULL))
368 continue;
Paolo 'Blaisorblade' Giarrusso55c033c2005-11-13 16:07:11 -0800369 n = chan->ops->console_write(chan->fd, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if(chan->primary) ret = n;
371 }
Jeff Diked50084a2006-01-06 00:18:50 -0800372 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Jeff Diked50084a2006-01-06 00:18:50 -0800375int console_open_chan(struct line *line, struct console *co,
376 struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Jeff Dike1f801712006-01-06 00:18:55 -0800378 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Jeff Dike1f801712006-01-06 00:18:55 -0800380 err = open_chan(&line->chan_list);
381 if(err)
382 return err;
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 printk("Console initialized on /dev/%s%d\n",co->name,co->index);
385 return 0;
386}
387
388int chan_window_size(struct list_head *chans, unsigned short *rows_out,
389 unsigned short *cols_out)
390{
391 struct list_head *ele;
392 struct chan *chan;
393
394 list_for_each(ele, chans){
395 chan = list_entry(ele, struct chan, list);
396 if(chan->primary){
Jeff Diked50084a2006-01-06 00:18:50 -0800397 if(chan->ops->window_size == NULL)
398 return 0;
399 return chan->ops->window_size(chan->fd, chan->data,
400 rows_out, cols_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402 }
Jeff Diked50084a2006-01-06 00:18:50 -0800403 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
Paolo 'Blaisorblade' Giarrusso42947cb2006-02-01 03:06:29 -0800406static void free_one_chan(struct chan *chan, int delay_free_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 list_del(&chan->list);
Jeff Dike165dc592006-01-06 00:18:57 -0800409
410 close_one_chan(chan, delay_free_irq);
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if(chan->ops->free != NULL)
413 (*chan->ops->free)(chan->data);
Jeff Dike165dc592006-01-06 00:18:57 -0800414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
416 kfree(chan);
417}
418
Paolo 'Blaisorblade' Giarrusso42947cb2006-02-01 03:06:29 -0800419static void free_chan(struct list_head *chans, int delay_free_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 struct list_head *ele, *next;
422 struct chan *chan;
423
424 list_for_each_safe(ele, next, chans){
425 chan = list_entry(ele, struct chan, list);
Jeff Dike165dc592006-01-06 00:18:57 -0800426 free_one_chan(chan, delay_free_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428}
429
430static int one_chan_config_string(struct chan *chan, char *str, int size,
431 char **error_out)
432{
433 int n = 0;
434
435 if(chan == NULL){
436 CONFIG_CHUNK(str, size, n, "none", 1);
Jeff Diked50084a2006-01-06 00:18:50 -0800437 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439
440 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
441
442 if(chan->dev == NULL){
443 CONFIG_CHUNK(str, size, n, "", 1);
Jeff Diked50084a2006-01-06 00:18:50 -0800444 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446
447 CONFIG_CHUNK(str, size, n, ":", 0);
448 CONFIG_CHUNK(str, size, n, chan->dev, 0);
449
Jeff Diked50084a2006-01-06 00:18:50 -0800450 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Jeff Diked50084a2006-01-06 00:18:50 -0800453static int chan_pair_config_string(struct chan *in, struct chan *out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 char *str, int size, char **error_out)
455{
456 int n;
457
458 n = one_chan_config_string(in, str, size, error_out);
459 str += n;
460 size -= n;
461
462 if(in == out){
463 CONFIG_CHUNK(str, size, n, "", 1);
Jeff Diked50084a2006-01-06 00:18:50 -0800464 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466
467 CONFIG_CHUNK(str, size, n, ",", 1);
468 n = one_chan_config_string(out, str, size, error_out);
469 str += n;
470 size -= n;
471 CONFIG_CHUNK(str, size, n, "", 1);
472
Jeff Diked50084a2006-01-06 00:18:50 -0800473 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Jeff Diked50084a2006-01-06 00:18:50 -0800476int chan_config_string(struct list_head *chans, char *str, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 char **error_out)
478{
479 struct list_head *ele;
480 struct chan *chan, *in = NULL, *out = NULL;
481
482 list_for_each(ele, chans){
483 chan = list_entry(ele, struct chan, list);
484 if(!chan->primary)
485 continue;
486 if(chan->input)
487 in = chan;
488 if(chan->output)
489 out = chan;
490 }
491
Jeff Diked50084a2006-01-06 00:18:50 -0800492 return chan_pair_config_string(in, out, str, size, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
495struct chan_type {
496 char *key;
497 struct chan_ops *ops;
498};
499
Paolo 'Blaisorblade' Giarrusso42947cb2006-02-01 03:06:29 -0800500static struct chan_type chan_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 { "fd", &fd_ops },
502
503#ifdef CONFIG_NULL_CHAN
504 { "null", &null_ops },
505#else
506 { "null", &not_configged_ops },
507#endif
508
509#ifdef CONFIG_PORT_CHAN
510 { "port", &port_ops },
511#else
512 { "port", &not_configged_ops },
513#endif
514
515#ifdef CONFIG_PTY_CHAN
516 { "pty", &pty_ops },
517 { "pts", &pts_ops },
518#else
519 { "pty", &not_configged_ops },
520 { "pts", &not_configged_ops },
521#endif
522
523#ifdef CONFIG_TTY_CHAN
524 { "tty", &tty_ops },
525#else
526 { "tty", &not_configged_ops },
527#endif
528
529#ifdef CONFIG_XTERM_CHAN
530 { "xterm", &xterm_ops },
531#else
532 { "xterm", &not_configged_ops },
533#endif
534};
535
Jeff Dike165dc592006-01-06 00:18:57 -0800536static struct chan *parse_chan(struct line *line, char *str, int device,
537 struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 struct chan_type *entry;
540 struct chan_ops *ops;
541 struct chan *chan;
542 void *data;
543 int i;
544
545 ops = NULL;
546 data = NULL;
547 for(i = 0; i < sizeof(chan_table)/sizeof(chan_table[0]); i++){
548 entry = &chan_table[i];
549 if(!strncmp(str, entry->key, strlen(entry->key))){
550 ops = entry->ops;
551 str += strlen(entry->key);
552 break;
553 }
554 }
555 if(ops == NULL){
Paolo 'Blaisorblade' Giarrussofac97ae2005-09-22 21:44:22 -0700556 my_printf("parse_chan couldn't parse \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 str);
Jeff Diked50084a2006-01-06 00:18:50 -0800558 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
Jeff Diked50084a2006-01-06 00:18:50 -0800560 if(ops->init == NULL)
561 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 data = (*ops->init)(str, device, opts);
Jeff Diked50084a2006-01-06 00:18:50 -0800563 if(data == NULL)
564 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Paolo 'Blaisorblade' Giarrusso79ae2cb2005-09-22 21:44:21 -0700566 chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
Jeff Diked50084a2006-01-06 00:18:50 -0800567 if(chan == NULL)
568 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
Jeff Dike165dc592006-01-06 00:18:57 -0800570 .free_list =
571 LIST_HEAD_INIT(chan->free_list),
572 .line = line,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 .primary = 1,
574 .input = 0,
575 .output = 0,
576 .opened = 0,
Jeff Dike165dc592006-01-06 00:18:57 -0800577 .enabled = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 .fd = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 .ops = ops,
580 .data = data });
Jeff Diked50084a2006-01-06 00:18:50 -0800581 return chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Jeff Dike165dc592006-01-06 00:18:57 -0800584int parse_chan_pair(char *str, struct line *line, int device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 struct chan_opts *opts)
586{
Jeff Dike165dc592006-01-06 00:18:57 -0800587 struct list_head *chans = &line->chan_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 struct chan *new, *chan;
589 char *in, *out;
590
591 if(!list_empty(chans)){
592 chan = list_entry(chans->next, struct chan, list);
Jeff Dike165dc592006-01-06 00:18:57 -0800593 free_chan(chans, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 INIT_LIST_HEAD(chans);
595 }
596
597 out = strchr(str, ',');
598 if(out != NULL){
599 in = str;
600 *out = '\0';
601 out++;
Jeff Dike165dc592006-01-06 00:18:57 -0800602 new = parse_chan(line, in, device, opts);
Jeff Diked50084a2006-01-06 00:18:50 -0800603 if(new == NULL)
604 return -1;
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 new->input = 1;
607 list_add(&new->list, chans);
608
Jeff Dike165dc592006-01-06 00:18:57 -0800609 new = parse_chan(line, out, device, opts);
Jeff Diked50084a2006-01-06 00:18:50 -0800610 if(new == NULL)
611 return -1;
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 list_add(&new->list, chans);
614 new->output = 1;
615 }
616 else {
Jeff Dike165dc592006-01-06 00:18:57 -0800617 new = parse_chan(line, str, device, opts);
Jeff Diked50084a2006-01-06 00:18:50 -0800618 if(new == NULL)
619 return -1;
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 list_add(&new->list, chans);
622 new->input = 1;
623 new->output = 1;
624 }
Jeff Diked50084a2006-01-06 00:18:50 -0800625 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628int chan_out_fd(struct list_head *chans)
629{
630 struct list_head *ele;
631 struct chan *chan;
632
633 list_for_each(ele, chans){
634 chan = list_entry(ele, struct chan, list);
635 if(chan->primary && chan->output)
Jeff Diked50084a2006-01-06 00:18:50 -0800636 return chan->fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
Jeff Diked50084a2006-01-06 00:18:50 -0800638 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
641void chan_interrupt(struct list_head *chans, struct work_struct *task,
642 struct tty_struct *tty, int irq)
643{
644 struct list_head *ele, *next;
645 struct chan *chan;
646 int err;
647 char c;
648
649 list_for_each_safe(ele, next, chans){
650 chan = list_entry(ele, struct chan, list);
651 if(!chan->input || (chan->ops->read == NULL)) continue;
652 do {
Alan Cox33f0f882006-01-09 20:54:13 -0800653 if (tty && !tty_buffer_request_room(tty, 1)) {
Jeff Dike9159c9d2006-01-06 00:18:58 -0800654 schedule_delayed_work(task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 goto out;
656 }
657 err = chan->ops->read(chan->fd, &c, chan->data);
658 if(err > 0)
659 tty_receive_char(tty, c);
660 } while(err > 0);
661
662 if(err == 0) reactivate_fd(chan->fd, irq);
663 if(err == -EIO){
664 if(chan->primary){
665 if(tty != NULL)
666 tty_hangup(tty);
Jeff Dike165dc592006-01-06 00:18:57 -0800667 close_chan(chans, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return;
669 }
Jeff Dike165dc592006-01-06 00:18:57 -0800670 else close_one_chan(chan, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672 }
673 out:
674 if(tty) tty_flip_buffer_push(tty);
675}