Epoll based IRQ controller
[linux-2.6-block.git] / arch / um / drivers / chan_kern.c
CommitLineData
165dc591 1/*
e99525f9 2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
1da177e4
LT
3 * Licensed under the GPL
4 */
5
1da177e4
LT
6#include <linux/slab.h>
7#include <linux/tty.h>
1da177e4 8#include <linux/tty_flip.h>
510c72a3 9#include "chan.h"
37185b33
AV
10#include <os.h>
11#include <irq_kern.h>
1da177e4 12
fac97ae0 13#ifdef CONFIG_NOCONFIG_CHAN
f28169d2
JD
14static void *not_configged_init(char *str, int device,
15 const struct chan_opts *opts)
fac97ae0 16{
e99525f9 17 printk(KERN_ERR "Using a channel type which is configured out of "
1da177e4 18 "UML\n");
d50084a2 19 return NULL;
1da177e4
LT
20}
21
22static int not_configged_open(int input, int output, int primary, void *data,
23 char **dev_out)
24{
e99525f9 25 printk(KERN_ERR "Using a channel type which is configured out of "
1da177e4 26 "UML\n");
d50084a2 27 return -ENODEV;
1da177e4
LT
28}
29
30static void not_configged_close(int fd, void *data)
31{
e99525f9 32 printk(KERN_ERR "Using a channel type which is configured out of "
1da177e4
LT
33 "UML\n");
34}
35
36static int not_configged_read(int fd, char *c_out, void *data)
37{
e99525f9 38 printk(KERN_ERR "Using a channel type which is configured out of "
1da177e4 39 "UML\n");
d50084a2 40 return -EIO;
1da177e4
LT
41}
42
43static int not_configged_write(int fd, const char *buf, int len, void *data)
44{
e99525f9 45 printk(KERN_ERR "Using a channel type which is configured out of "
1da177e4 46 "UML\n");
d50084a2 47 return -EIO;
1da177e4
LT
48}
49
55c033c1 50static int not_configged_console_write(int fd, const char *buf, int len)
1da177e4 51{
e99525f9 52 printk(KERN_ERR "Using a channel type which is configured out of "
1da177e4 53 "UML\n");
d50084a2 54 return -EIO;
1da177e4
LT
55}
56
57static int not_configged_window_size(int fd, void *data, unsigned short *rows,
58 unsigned short *cols)
59{
e99525f9 60 printk(KERN_ERR "Using a channel type which is configured out of "
1da177e4 61 "UML\n");
d50084a2 62 return -ENODEV;
1da177e4
LT
63}
64
65static void not_configged_free(void *data)
66{
e99525f9 67 printk(KERN_ERR "Using a channel type which is configured out of "
1da177e4
LT
68 "UML\n");
69}
70
5e7672ec 71static const struct chan_ops not_configged_ops = {
1da177e4
LT
72 .init = not_configged_init,
73 .open = not_configged_open,
74 .close = not_configged_close,
75 .read = not_configged_read,
76 .write = not_configged_write,
77 .console_write = not_configged_console_write,
78 .window_size = not_configged_window_size,
79 .free = not_configged_free,
80 .winch = 0,
81};
82#endif /* CONFIG_NOCONFIG_CHAN */
83
d50084a2 84static int open_one_chan(struct chan *chan)
1da177e4 85{
6676ae62 86 int fd, err;
1da177e4 87
e99525f9 88 if (chan->opened)
d50084a2
JD
89 return 0;
90
e99525f9 91 if (chan->ops->open == NULL)
d50084a2
JD
92 fd = 0;
93 else fd = (*chan->ops->open)(chan->input, chan->output, chan->primary,
94 chan->data, &chan->dev);
e99525f9 95 if (fd < 0)
d50084a2 96 return fd;
6676ae62
JD
97
98 err = os_set_fd_block(fd, 0);
99 if (err) {
100 (*chan->ops->close)(fd, chan->data);
101 return err;
102 }
103
1da177e4
LT
104 chan->fd = fd;
105
106 chan->opened = 1;
d50084a2 107 return 0;
1da177e4
LT
108}
109
3af7cb7b 110static int open_chan(struct list_head *chans)
1da177e4
LT
111{
112 struct list_head *ele;
113 struct chan *chan;
114 int ret, err = 0;
115
e99525f9 116 list_for_each(ele, chans) {
1da177e4 117 chan = list_entry(ele, struct chan, list);
d50084a2 118 ret = open_one_chan(chan);
e99525f9 119 if (chan->primary)
d50084a2 120 err = ret;
1da177e4 121 }
d50084a2 122 return err;
1da177e4
LT
123}
124
2116bda6 125void chan_enable_winch(struct chan *chan, struct tty_port *port)
1da177e4 126{
bed5e39c 127 if (chan && chan->primary && chan->ops->winch)
2116bda6 128 register_winch(chan->fd, port);
1da177e4
LT
129}
130
0fcd7199
AV
131static void line_timer_cb(struct work_struct *work)
132{
133 struct line *line = container_of(work, struct line, task.work);
134
135 if (!line->throttled)
2e124b4a 136 chan_interrupt(line, line->driver->read_irq);
0fcd7199
AV
137}
138
d14ad81f 139int enable_chan(struct line *line)
1da177e4
LT
140{
141 struct list_head *ele;
142 struct chan *chan;
d14ad81f 143 int err;
1da177e4 144
0fcd7199
AV
145 INIT_DELAYED_WORK(&line->task, line_timer_cb);
146
e99525f9 147 list_for_each(ele, &line->chan_list) {
1da177e4 148 chan = list_entry(ele, struct chan, list);
d14ad81f
JD
149 err = open_one_chan(chan);
150 if (err) {
151 if (chan->primary)
152 goto out_close;
153
165dc591 154 continue;
d14ad81f 155 }
165dc591 156
e99525f9 157 if (chan->enabled)
165dc591 158 continue;
d14ad81f
JD
159 err = line_setup_irq(chan->fd, chan->input, chan->output, line,
160 chan);
161 if (err)
162 goto out_close;
163
165dc591
JD
164 chan->enabled = 1;
165 }
d14ad81f
JD
166
167 return 0;
168
169 out_close:
10c890c0 170 close_chan(line);
d14ad81f 171 return err;
165dc591
JD
172}
173
165dc591
JD
174static void close_one_chan(struct chan *chan, int delay_free_irq)
175{
e99525f9 176 if (!chan->opened)
165dc591 177 return;
1da177e4 178
ff6a1798
AI
179 /* we can safely call free now - it will be marked
180 * as free and freed once the IRQ stopped processing
181 */
182 if (chan->input && chan->enabled)
183 um_free_irq(chan->line->driver->read_irq, chan);
184 if (chan->output && chan->enabled)
185 um_free_irq(chan->line->driver->write_irq, chan);
186 chan->enabled = 0;
e99525f9 187 if (chan->ops->close != NULL)
165dc591
JD
188 (*chan->ops->close)(chan->fd, chan->data);
189
190 chan->opened = 0;
191 chan->fd = -1;
1da177e4
LT
192}
193
10c890c0 194void close_chan(struct line *line)
1da177e4
LT
195{
196 struct chan *chan;
197
198 /* Close in reverse order as open in case more than one of them
199 * refers to the same device and they save and restore that device's
200 * state. Then, the first one opened will have the original state,
201 * so it must be the last closed.
202 */
10c890c0
AV
203 list_for_each_entry_reverse(chan, &line->chan_list, list) {
204 close_one_chan(chan, 0);
1da177e4
LT
205 }
206}
207
bed5e39c 208void deactivate_chan(struct chan *chan, int irq)
e4dcee80 209{
bed5e39c
AV
210 if (chan && chan->enabled)
211 deactivate_fd(chan->fd, irq);
e4dcee80
JD
212}
213
bed5e39c 214void reactivate_chan(struct chan *chan, int irq)
e4dcee80 215{
bed5e39c
AV
216 if (chan && chan->enabled)
217 reactivate_fd(chan->fd, irq);
e4dcee80
JD
218}
219
bed5e39c 220int write_chan(struct chan *chan, const char *buf, int len,
1da177e4
LT
221 int write_irq)
222{
1da177e4
LT
223 int n, ret = 0;
224
bed5e39c 225 if (len == 0 || !chan || !chan->ops->write)
c59dbcad
JD
226 return 0;
227
bed5e39c
AV
228 n = chan->ops->write(chan->fd, buf, len, chan->data);
229 if (chan->primary) {
230 ret = n;
231 if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
232 reactivate_fd(chan->fd, write_irq);
1da177e4 233 }
d50084a2 234 return ret;
1da177e4
LT
235}
236
bed5e39c 237int console_write_chan(struct chan *chan, const char *buf, int len)
1da177e4 238{
1da177e4
LT
239 int n, ret = 0;
240
bed5e39c
AV
241 if (!chan || !chan->ops->console_write)
242 return 0;
e99525f9 243
bed5e39c
AV
244 n = chan->ops->console_write(chan->fd, buf, len);
245 if (chan->primary)
246 ret = n;
d50084a2 247 return ret;
1da177e4
LT
248}
249
a52f362f 250int console_open_chan(struct line *line, struct console *co)
1da177e4 251{
1f80171e
JD
252 int err;
253
254 err = open_chan(&line->chan_list);
e99525f9 255 if (err)
1f80171e 256 return err;
1da177e4 257
e99525f9
JD
258 printk(KERN_INFO "Console initialized on /dev/%s%d\n", co->name,
259 co->index);
1da177e4
LT
260 return 0;
261}
262
bed5e39c 263int chan_window_size(struct line *line, unsigned short *rows_out,
1da177e4
LT
264 unsigned short *cols_out)
265{
1da177e4
LT
266 struct chan *chan;
267
bed5e39c
AV
268 chan = line->chan_in;
269 if (chan && chan->primary) {
270 if (chan->ops->window_size == NULL)
271 return 0;
272 return chan->ops->window_size(chan->fd, chan->data,
273 rows_out, cols_out);
274 }
275 chan = line->chan_out;
276 if (chan && chan->primary) {
277 if (chan->ops->window_size == NULL)
278 return 0;
279 return chan->ops->window_size(chan->fd, chan->data,
280 rows_out, cols_out);
1da177e4 281 }
d50084a2 282 return 0;
1da177e4
LT
283}
284
772bd0a5 285static void free_one_chan(struct chan *chan)
1da177e4
LT
286{
287 list_del(&chan->list);
165dc591 288
772bd0a5 289 close_one_chan(chan, 0);
165dc591 290
e99525f9 291 if (chan->ops->free != NULL)
1da177e4 292 (*chan->ops->free)(chan->data);
165dc591 293
e99525f9
JD
294 if (chan->primary && chan->output)
295 ignore_sigio_fd(chan->fd);
1da177e4
LT
296 kfree(chan);
297}
298
772bd0a5 299static void free_chan(struct list_head *chans)
1da177e4
LT
300{
301 struct list_head *ele, *next;
302 struct chan *chan;
303
e99525f9 304 list_for_each_safe(ele, next, chans) {
1da177e4 305 chan = list_entry(ele, struct chan, list);
772bd0a5 306 free_one_chan(chan);
1da177e4
LT
307 }
308}
309
310static int one_chan_config_string(struct chan *chan, char *str, int size,
311 char **error_out)
312{
313 int n = 0;
314
e99525f9 315 if (chan == NULL) {
1da177e4 316 CONFIG_CHUNK(str, size, n, "none", 1);
d50084a2 317 return n;
1da177e4
LT
318 }
319
320 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
321
e99525f9 322 if (chan->dev == NULL) {
1da177e4 323 CONFIG_CHUNK(str, size, n, "", 1);
d50084a2 324 return n;
1da177e4
LT
325 }
326
327 CONFIG_CHUNK(str, size, n, ":", 0);
328 CONFIG_CHUNK(str, size, n, chan->dev, 0);
329
d50084a2 330 return n;
1da177e4
LT
331}
332
d50084a2 333static int chan_pair_config_string(struct chan *in, struct chan *out,
1da177e4
LT
334 char *str, int size, char **error_out)
335{
336 int n;
337
338 n = one_chan_config_string(in, str, size, error_out);
339 str += n;
340 size -= n;
341
e99525f9 342 if (in == out) {
1da177e4 343 CONFIG_CHUNK(str, size, n, "", 1);
d50084a2 344 return n;
1da177e4
LT
345 }
346
347 CONFIG_CHUNK(str, size, n, ",", 1);
348 n = one_chan_config_string(out, str, size, error_out);
349 str += n;
350 size -= n;
351 CONFIG_CHUNK(str, size, n, "", 1);
352
d50084a2 353 return n;
1da177e4
LT
354}
355
bed5e39c 356int chan_config_string(struct line *line, char *str, int size,
1da177e4
LT
357 char **error_out)
358{
bed5e39c 359 struct chan *in = line->chan_in, *out = line->chan_out;
1da177e4 360
bed5e39c
AV
361 if (in && !in->primary)
362 in = NULL;
363 if (out && !out->primary)
364 out = NULL;
1da177e4 365
d50084a2 366 return chan_pair_config_string(in, out, str, size, error_out);
1da177e4
LT
367}
368
369struct chan_type {
370 char *key;
5e7672ec 371 const struct chan_ops *ops;
1da177e4
LT
372};
373
5e7672ec 374static const struct chan_type chan_table[] = {
1da177e4
LT
375 { "fd", &fd_ops },
376
377#ifdef CONFIG_NULL_CHAN
378 { "null", &null_ops },
379#else
380 { "null", &not_configged_ops },
381#endif
382
383#ifdef CONFIG_PORT_CHAN
384 { "port", &port_ops },
385#else
386 { "port", &not_configged_ops },
387#endif
388
389#ifdef CONFIG_PTY_CHAN
390 { "pty", &pty_ops },
391 { "pts", &pts_ops },
392#else
393 { "pty", &not_configged_ops },
394 { "pts", &not_configged_ops },
395#endif
396
397#ifdef CONFIG_TTY_CHAN
398 { "tty", &tty_ops },
399#else
400 { "tty", &not_configged_ops },
401#endif
402
403#ifdef CONFIG_XTERM_CHAN
404 { "xterm", &xterm_ops },
405#else
406 { "xterm", &not_configged_ops },
407#endif
408};
409
165dc591 410static struct chan *parse_chan(struct line *line, char *str, int device,
f28169d2 411 const struct chan_opts *opts, char **error_out)
1da177e4 412{
5e7672ec
JD
413 const struct chan_type *entry;
414 const struct chan_ops *ops;
1da177e4
LT
415 struct chan *chan;
416 void *data;
417 int i;
418
419 ops = NULL;
420 data = NULL;
e99525f9 421 for(i = 0; i < ARRAY_SIZE(chan_table); i++) {
1da177e4 422 entry = &chan_table[i];
e99525f9 423 if (!strncmp(str, entry->key, strlen(entry->key))) {
1da177e4
LT
424 ops = entry->ops;
425 str += strlen(entry->key);
426 break;
427 }
428 }
e99525f9 429 if (ops == NULL) {
f28169d2 430 *error_out = "No match for configured backends";
d50084a2 431 return NULL;
1da177e4 432 }
f28169d2 433
1da177e4 434 data = (*ops->init)(str, device, opts);
e99525f9 435 if (data == NULL) {
f28169d2 436 *error_out = "Configuration failed";
d50084a2 437 return NULL;
f28169d2 438 }
1da177e4 439
79ae2cb8 440 chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
e99525f9 441 if (chan == NULL) {
f28169d2 442 *error_out = "Memory allocation failed";
d50084a2 443 return NULL;
f28169d2 444 }
1da177e4 445 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
165dc591
JD
446 .free_list =
447 LIST_HEAD_INIT(chan->free_list),
448 .line = line,
1da177e4
LT
449 .primary = 1,
450 .input = 0,
451 .output = 0,
452 .opened = 0,
165dc591 453 .enabled = 0,
1da177e4 454 .fd = -1,
1da177e4
LT
455 .ops = ops,
456 .data = data });
d50084a2 457 return chan;
1da177e4
LT
458}
459
165dc591 460int parse_chan_pair(char *str, struct line *line, int device,
f28169d2 461 const struct chan_opts *opts, char **error_out)
1da177e4 462{
165dc591 463 struct list_head *chans = &line->chan_list;
f1c93e49 464 struct chan *new;
1da177e4
LT
465 char *in, *out;
466
e99525f9 467 if (!list_empty(chans)) {
ee485070 468 line->chan_in = line->chan_out = NULL;
772bd0a5 469 free_chan(chans);
1da177e4
LT
470 INIT_LIST_HEAD(chans);
471 }
472
31efcebb
AV
473 if (!str)
474 return 0;
475
1da177e4 476 out = strchr(str, ',');
e99525f9 477 if (out != NULL) {
1da177e4
LT
478 in = str;
479 *out = '\0';
480 out++;
f28169d2 481 new = parse_chan(line, in, device, opts, error_out);
e99525f9 482 if (new == NULL)
d50084a2
JD
483 return -1;
484
1da177e4
LT
485 new->input = 1;
486 list_add(&new->list, chans);
ee485070 487 line->chan_in = new;
1da177e4 488
f28169d2 489 new = parse_chan(line, out, device, opts, error_out);
e99525f9 490 if (new == NULL)
d50084a2
JD
491 return -1;
492
1da177e4
LT
493 list_add(&new->list, chans);
494 new->output = 1;
ee485070 495 line->chan_out = new;
1da177e4
LT
496 }
497 else {
f28169d2 498 new = parse_chan(line, str, device, opts, error_out);
e99525f9 499 if (new == NULL)
d50084a2
JD
500 return -1;
501
1da177e4
LT
502 list_add(&new->list, chans);
503 new->input = 1;
504 new->output = 1;
ee485070 505 line->chan_in = line->chan_out = new;
1da177e4 506 }
d50084a2 507 return 0;
1da177e4
LT
508}
509
2e124b4a 510void chan_interrupt(struct line *line, int irq)
1da177e4 511{
227434f8 512 struct tty_port *port = &line->port;
bed5e39c 513 struct chan *chan = line->chan_in;
1da177e4
LT
514 int err;
515 char c;
516
bed5e39c
AV
517 if (!chan || !chan->ops->read)
518 goto out;
519
520 do {
227434f8 521 if (!tty_buffer_request_room(port, 1)) {
0fcd7199 522 schedule_delayed_work(&line->task, 1);
bed5e39c
AV
523 goto out;
524 }
525 err = chan->ops->read(chan->fd, &c, chan->data);
526 if (err > 0)
92a19f9c 527 tty_insert_flip_char(port, c, TTY_NORMAL);
bed5e39c
AV
528 } while (err > 0);
529
530 if (err == 0)
531 reactivate_fd(chan->fd, irq);
532 if (err == -EIO) {
533 if (chan->primary) {
aa27a094 534 tty_port_tty_hangup(&line->port, false);
10c890c0
AV
535 if (line->chan_out != chan)
536 close_one_chan(line->chan_out, 1);
1da177e4 537 }
10c890c0
AV
538 close_one_chan(chan, 1);
539 if (chan->primary)
540 return;
1da177e4
LT
541 }
542 out:
2e124b4a 543 tty_flip_buffer_push(port);
1da177e4 544}