blob: cd13b91b9ff6a9702126282fb6e9dbc7fc0f9f90 [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
Jeff Diked50084a2006-01-06 00:18:50 -0800189 if((tty->flip.flag_buf_ptr == NULL) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 (tty->flip.char_buf_ptr == NULL))
191 return;
192 tty_insert_flip_char(tty, ch, TTY_NORMAL);
193}
194
Jeff Diked50084a2006-01-06 00:18:50 -0800195static int open_one_chan(struct chan *chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 int fd;
198
Jeff Diked50084a2006-01-06 00:18:50 -0800199 if(chan->opened)
200 return 0;
201
202 if(chan->ops->open == NULL)
203 fd = 0;
204 else fd = (*chan->ops->open)(chan->input, chan->output, chan->primary,
205 chan->data, &chan->dev);
206 if(fd < 0)
207 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 chan->fd = fd;
209
210 chan->opened = 1;
Jeff Diked50084a2006-01-06 00:18:50 -0800211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214int open_chan(struct list_head *chans)
215{
216 struct list_head *ele;
217 struct chan *chan;
218 int ret, err = 0;
219
220 list_for_each(ele, chans){
221 chan = list_entry(ele, struct chan, list);
Jeff Diked50084a2006-01-06 00:18:50 -0800222 ret = open_one_chan(chan);
223 if(chan->primary)
224 err = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
Jeff Diked50084a2006-01-06 00:18:50 -0800226 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
229void chan_enable_winch(struct list_head *chans, struct tty_struct *tty)
230{
231 struct list_head *ele;
232 struct chan *chan;
233
234 list_for_each(ele, chans){
235 chan = list_entry(ele, struct chan, list);
236 if(chan->primary && chan->output && chan->ops->winch){
237 register_winch(chan->fd, tty);
238 return;
239 }
240 }
241}
242
Jeff Dike165dc592006-01-06 00:18:57 -0800243void enable_chan(struct line *line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 struct list_head *ele;
246 struct chan *chan;
247
Jeff Dike165dc592006-01-06 00:18:57 -0800248 list_for_each(ele, &line->chan_list){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 chan = list_entry(ele, struct chan, list);
Jeff Dike165dc592006-01-06 00:18:57 -0800250 if(open_one_chan(chan))
251 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Jeff Dike165dc592006-01-06 00:18:57 -0800253 if(chan->enabled)
254 continue;
255 line_setup_irq(chan->fd, chan->input, chan->output, line,
256 chan);
257 chan->enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259}
260
Jeff Dike165dc592006-01-06 00:18:57 -0800261static LIST_HEAD(irqs_to_free);
262
263void free_irqs(void)
264{
265 struct chan *chan;
266
267 while(!list_empty(&irqs_to_free)){
268 chan = list_entry(irqs_to_free.next, struct chan, free_list);
269 list_del(&chan->free_list);
270
271 if(chan->input)
272 free_irq(chan->line->driver->read_irq, chan);
273 if(chan->output)
274 free_irq(chan->line->driver->write_irq, chan);
275 chan->enabled = 0;
276 }
277}
278
279static void close_one_chan(struct chan *chan, int delay_free_irq)
280{
281 if(!chan->opened)
282 return;
283
284 if(delay_free_irq){
285 list_add(&chan->free_list, &irqs_to_free);
286 }
287 else {
288 if(chan->input)
289 free_irq(chan->line->driver->read_irq, chan);
290 if(chan->output)
291 free_irq(chan->line->driver->write_irq, chan);
292 chan->enabled = 0;
293 }
294 if(chan->ops->close != NULL)
295 (*chan->ops->close)(chan->fd, chan->data);
296
297 chan->opened = 0;
298 chan->fd = -1;
299}
300
301void close_chan(struct list_head *chans, int delay_free_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 struct chan *chan;
304
305 /* Close in reverse order as open in case more than one of them
306 * refers to the same device and they save and restore that device's
307 * state. Then, the first one opened will have the original state,
308 * so it must be the last closed.
309 */
310 list_for_each_entry_reverse(chan, chans, list) {
Jeff Dike165dc592006-01-06 00:18:57 -0800311 close_one_chan(chan, delay_free_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313}
314
Jeff Dikee4dcee82006-01-06 00:18:58 -0800315void deactivate_chan(struct list_head *chans, int irq)
316{
317 struct list_head *ele;
318
319 struct chan *chan;
320 list_for_each(ele, chans) {
321 chan = list_entry(ele, struct chan, list);
322
323 if(chan->enabled && chan->input)
324 deactivate_fd(chan->fd, irq);
325 }
326}
327
328void reactivate_chan(struct list_head *chans, int irq)
329{
330 struct list_head *ele;
331 struct chan *chan;
332
333 list_for_each(ele, chans) {
334 chan = list_entry(ele, struct chan, list);
335
336 if(chan->enabled && chan->input)
337 reactivate_fd(chan->fd, irq);
338 }
339}
340
Jeff Diked50084a2006-01-06 00:18:50 -0800341int write_chan(struct list_head *chans, const char *buf, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 int write_irq)
343{
344 struct list_head *ele;
345 struct chan *chan = NULL;
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->write == NULL))
351 continue;
352 n = chan->ops->write(chan->fd, buf, len, chan->data);
353 if (chan->primary) {
354 ret = n;
355 if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
356 reactivate_fd(chan->fd, write_irq);
357 }
358 }
Jeff Diked50084a2006-01-06 00:18:50 -0800359 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
362int console_write_chan(struct list_head *chans, const char *buf, int len)
363{
364 struct list_head *ele;
365 struct chan *chan;
366 int n, ret = 0;
367
368 list_for_each(ele, chans){
369 chan = list_entry(ele, struct chan, list);
370 if(!chan->output || (chan->ops->console_write == NULL))
371 continue;
Paolo 'Blaisorblade' Giarrusso55c033c2005-11-13 16:07:11 -0800372 n = chan->ops->console_write(chan->fd, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if(chan->primary) ret = n;
374 }
Jeff Diked50084a2006-01-06 00:18:50 -0800375 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
Jeff Diked50084a2006-01-06 00:18:50 -0800378int console_open_chan(struct line *line, struct console *co,
379 struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Jeff Dike1f801712006-01-06 00:18:55 -0800381 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Jeff Dike1f801712006-01-06 00:18:55 -0800383 err = open_chan(&line->chan_list);
384 if(err)
385 return err;
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 printk("Console initialized on /dev/%s%d\n",co->name,co->index);
388 return 0;
389}
390
391int chan_window_size(struct list_head *chans, unsigned short *rows_out,
392 unsigned short *cols_out)
393{
394 struct list_head *ele;
395 struct chan *chan;
396
397 list_for_each(ele, chans){
398 chan = list_entry(ele, struct chan, list);
399 if(chan->primary){
Jeff Diked50084a2006-01-06 00:18:50 -0800400 if(chan->ops->window_size == NULL)
401 return 0;
402 return chan->ops->window_size(chan->fd, chan->data,
403 rows_out, cols_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405 }
Jeff Diked50084a2006-01-06 00:18:50 -0800406 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Jeff Dike165dc592006-01-06 00:18:57 -0800409void free_one_chan(struct chan *chan, int delay_free_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 list_del(&chan->list);
Jeff Dike165dc592006-01-06 00:18:57 -0800412
413 close_one_chan(chan, delay_free_irq);
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if(chan->ops->free != NULL)
416 (*chan->ops->free)(chan->data);
Jeff Dike165dc592006-01-06 00:18:57 -0800417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
419 kfree(chan);
420}
421
Jeff Dike165dc592006-01-06 00:18:57 -0800422void free_chan(struct list_head *chans, int delay_free_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 struct list_head *ele, *next;
425 struct chan *chan;
426
427 list_for_each_safe(ele, next, chans){
428 chan = list_entry(ele, struct chan, list);
Jeff Dike165dc592006-01-06 00:18:57 -0800429 free_one_chan(chan, delay_free_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431}
432
433static int one_chan_config_string(struct chan *chan, char *str, int size,
434 char **error_out)
435{
436 int n = 0;
437
438 if(chan == NULL){
439 CONFIG_CHUNK(str, size, n, "none", 1);
Jeff Diked50084a2006-01-06 00:18:50 -0800440 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442
443 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
444
445 if(chan->dev == NULL){
446 CONFIG_CHUNK(str, size, n, "", 1);
Jeff Diked50084a2006-01-06 00:18:50 -0800447 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449
450 CONFIG_CHUNK(str, size, n, ":", 0);
451 CONFIG_CHUNK(str, size, n, chan->dev, 0);
452
Jeff Diked50084a2006-01-06 00:18:50 -0800453 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Jeff Diked50084a2006-01-06 00:18:50 -0800456static int chan_pair_config_string(struct chan *in, struct chan *out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 char *str, int size, char **error_out)
458{
459 int n;
460
461 n = one_chan_config_string(in, str, size, error_out);
462 str += n;
463 size -= n;
464
465 if(in == out){
466 CONFIG_CHUNK(str, size, n, "", 1);
Jeff Diked50084a2006-01-06 00:18:50 -0800467 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469
470 CONFIG_CHUNK(str, size, n, ",", 1);
471 n = one_chan_config_string(out, str, size, error_out);
472 str += n;
473 size -= n;
474 CONFIG_CHUNK(str, size, n, "", 1);
475
Jeff Diked50084a2006-01-06 00:18:50 -0800476 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Jeff Diked50084a2006-01-06 00:18:50 -0800479int chan_config_string(struct list_head *chans, char *str, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 char **error_out)
481{
482 struct list_head *ele;
483 struct chan *chan, *in = NULL, *out = NULL;
484
485 list_for_each(ele, chans){
486 chan = list_entry(ele, struct chan, list);
487 if(!chan->primary)
488 continue;
489 if(chan->input)
490 in = chan;
491 if(chan->output)
492 out = chan;
493 }
494
Jeff Diked50084a2006-01-06 00:18:50 -0800495 return chan_pair_config_string(in, out, str, size, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498struct chan_type {
499 char *key;
500 struct chan_ops *ops;
501};
502
503struct chan_type chan_table[] = {
504 { "fd", &fd_ops },
505
506#ifdef CONFIG_NULL_CHAN
507 { "null", &null_ops },
508#else
509 { "null", &not_configged_ops },
510#endif
511
512#ifdef CONFIG_PORT_CHAN
513 { "port", &port_ops },
514#else
515 { "port", &not_configged_ops },
516#endif
517
518#ifdef CONFIG_PTY_CHAN
519 { "pty", &pty_ops },
520 { "pts", &pts_ops },
521#else
522 { "pty", &not_configged_ops },
523 { "pts", &not_configged_ops },
524#endif
525
526#ifdef CONFIG_TTY_CHAN
527 { "tty", &tty_ops },
528#else
529 { "tty", &not_configged_ops },
530#endif
531
532#ifdef CONFIG_XTERM_CHAN
533 { "xterm", &xterm_ops },
534#else
535 { "xterm", &not_configged_ops },
536#endif
537};
538
Jeff Dike165dc592006-01-06 00:18:57 -0800539static struct chan *parse_chan(struct line *line, char *str, int device,
540 struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct chan_type *entry;
543 struct chan_ops *ops;
544 struct chan *chan;
545 void *data;
546 int i;
547
548 ops = NULL;
549 data = NULL;
550 for(i = 0; i < sizeof(chan_table)/sizeof(chan_table[0]); i++){
551 entry = &chan_table[i];
552 if(!strncmp(str, entry->key, strlen(entry->key))){
553 ops = entry->ops;
554 str += strlen(entry->key);
555 break;
556 }
557 }
558 if(ops == NULL){
Paolo 'Blaisorblade' Giarrussofac97ae2005-09-22 21:44:22 -0700559 my_printf("parse_chan couldn't parse \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 str);
Jeff Diked50084a2006-01-06 00:18:50 -0800561 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
Jeff Diked50084a2006-01-06 00:18:50 -0800563 if(ops->init == NULL)
564 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 data = (*ops->init)(str, device, opts);
Jeff Diked50084a2006-01-06 00:18:50 -0800566 if(data == NULL)
567 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Paolo 'Blaisorblade' Giarrusso79ae2cb2005-09-22 21:44:21 -0700569 chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
Jeff Diked50084a2006-01-06 00:18:50 -0800570 if(chan == NULL)
571 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
Jeff Dike165dc592006-01-06 00:18:57 -0800573 .free_list =
574 LIST_HEAD_INIT(chan->free_list),
575 .line = line,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 .primary = 1,
577 .input = 0,
578 .output = 0,
579 .opened = 0,
Jeff Dike165dc592006-01-06 00:18:57 -0800580 .enabled = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 .fd = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 .ops = ops,
583 .data = data });
Jeff Diked50084a2006-01-06 00:18:50 -0800584 return chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Jeff Dike165dc592006-01-06 00:18:57 -0800587int parse_chan_pair(char *str, struct line *line, int device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 struct chan_opts *opts)
589{
Jeff Dike165dc592006-01-06 00:18:57 -0800590 struct list_head *chans = &line->chan_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 struct chan *new, *chan;
592 char *in, *out;
593
594 if(!list_empty(chans)){
595 chan = list_entry(chans->next, struct chan, list);
Jeff Dike165dc592006-01-06 00:18:57 -0800596 free_chan(chans, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 INIT_LIST_HEAD(chans);
598 }
599
600 out = strchr(str, ',');
601 if(out != NULL){
602 in = str;
603 *out = '\0';
604 out++;
Jeff Dike165dc592006-01-06 00:18:57 -0800605 new = parse_chan(line, in, device, opts);
Jeff Diked50084a2006-01-06 00:18:50 -0800606 if(new == NULL)
607 return -1;
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 new->input = 1;
610 list_add(&new->list, chans);
611
Jeff Dike165dc592006-01-06 00:18:57 -0800612 new = parse_chan(line, out, device, opts);
Jeff Diked50084a2006-01-06 00:18:50 -0800613 if(new == NULL)
614 return -1;
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 list_add(&new->list, chans);
617 new->output = 1;
618 }
619 else {
Jeff Dike165dc592006-01-06 00:18:57 -0800620 new = parse_chan(line, str, device, opts);
Jeff Diked50084a2006-01-06 00:18:50 -0800621 if(new == NULL)
622 return -1;
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 list_add(&new->list, chans);
625 new->input = 1;
626 new->output = 1;
627 }
Jeff Diked50084a2006-01-06 00:18:50 -0800628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631int chan_out_fd(struct list_head *chans)
632{
633 struct list_head *ele;
634 struct chan *chan;
635
636 list_for_each(ele, chans){
637 chan = list_entry(ele, struct chan, list);
638 if(chan->primary && chan->output)
Jeff Diked50084a2006-01-06 00:18:50 -0800639 return chan->fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
Jeff Diked50084a2006-01-06 00:18:50 -0800641 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644void chan_interrupt(struct list_head *chans, struct work_struct *task,
645 struct tty_struct *tty, int irq)
646{
647 struct list_head *ele, *next;
648 struct chan *chan;
649 int err;
650 char c;
651
652 list_for_each_safe(ele, next, chans){
653 chan = list_entry(ele, struct chan, list);
654 if(!chan->input || (chan->ops->read == NULL)) continue;
655 do {
Jeff Diked50084a2006-01-06 00:18:50 -0800656 if((tty != NULL) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 (tty->flip.count >= TTY_FLIPBUF_SIZE)){
Jeff Dike9159c9d2006-01-06 00:18:58 -0800658 schedule_delayed_work(task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 goto out;
660 }
661 err = chan->ops->read(chan->fd, &c, chan->data);
662 if(err > 0)
663 tty_receive_char(tty, c);
664 } while(err > 0);
665
666 if(err == 0) reactivate_fd(chan->fd, irq);
667 if(err == -EIO){
668 if(chan->primary){
669 if(tty != NULL)
670 tty_hangup(tty);
Jeff Dike165dc592006-01-06 00:18:57 -0800671 close_chan(chans, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return;
673 }
Jeff Dike165dc592006-01-06 00:18:57 -0800674 else close_one_chan(chan, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676 }
677 out:
678 if(tty) tty_flip_buffer_push(tty);
679}