]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/um/drivers/chan_kern.c
Merge branch 'sched-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-artful-kernel.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
190c3e45
JD
174/* Items are added in IRQ context, when free_irq can't be called, and
175 * removed in process context, when it can.
176 * This handles interrupt sources which disappear, and which need to
177 * be permanently disabled. This is discovered in IRQ context, but
178 * the freeing of the IRQ must be done later.
179 */
180static DEFINE_SPINLOCK(irqs_to_free_lock);
165dc591
JD
181static LIST_HEAD(irqs_to_free);
182
183void free_irqs(void)
184{
185 struct chan *chan;
190c3e45
JD
186 LIST_HEAD(list);
187 struct list_head *ele;
3076212f 188 unsigned long flags;
190c3e45 189
3076212f 190 spin_lock_irqsave(&irqs_to_free_lock, flags);
190c3e45 191 list_splice_init(&irqs_to_free, &list);
3076212f 192 spin_unlock_irqrestore(&irqs_to_free_lock, flags);
165dc591 193
e99525f9 194 list_for_each(ele, &list) {
190c3e45 195 chan = list_entry(ele, struct chan, free_list);
165dc591 196
47562277 197 if (chan->input && chan->enabled)
fa7a0449 198 um_free_irq(chan->line->driver->read_irq, chan);
47562277 199 if (chan->output && chan->enabled)
fa7a0449 200 um_free_irq(chan->line->driver->write_irq, chan);
165dc591
JD
201 chan->enabled = 0;
202 }
203}
204
205static void close_one_chan(struct chan *chan, int delay_free_irq)
206{
3076212f
JD
207 unsigned long flags;
208
e99525f9 209 if (!chan->opened)
165dc591 210 return;
1da177e4 211
e99525f9 212 if (delay_free_irq) {
3076212f 213 spin_lock_irqsave(&irqs_to_free_lock, flags);
165dc591 214 list_add(&chan->free_list, &irqs_to_free);
3076212f 215 spin_unlock_irqrestore(&irqs_to_free_lock, flags);
165dc591
JD
216 }
217 else {
47562277 218 if (chan->input && chan->enabled)
fa7a0449 219 um_free_irq(chan->line->driver->read_irq, chan);
47562277 220 if (chan->output && chan->enabled)
fa7a0449 221 um_free_irq(chan->line->driver->write_irq, chan);
165dc591 222 chan->enabled = 0;
1da177e4 223 }
e99525f9 224 if (chan->ops->close != NULL)
165dc591
JD
225 (*chan->ops->close)(chan->fd, chan->data);
226
227 chan->opened = 0;
228 chan->fd = -1;
1da177e4
LT
229}
230
10c890c0 231void close_chan(struct line *line)
1da177e4
LT
232{
233 struct chan *chan;
234
235 /* Close in reverse order as open in case more than one of them
236 * refers to the same device and they save and restore that device's
237 * state. Then, the first one opened will have the original state,
238 * so it must be the last closed.
239 */
10c890c0
AV
240 list_for_each_entry_reverse(chan, &line->chan_list, list) {
241 close_one_chan(chan, 0);
1da177e4
LT
242 }
243}
244
bed5e39c 245void deactivate_chan(struct chan *chan, int irq)
e4dcee80 246{
bed5e39c
AV
247 if (chan && chan->enabled)
248 deactivate_fd(chan->fd, irq);
e4dcee80
JD
249}
250
bed5e39c 251void reactivate_chan(struct chan *chan, int irq)
e4dcee80 252{
bed5e39c
AV
253 if (chan && chan->enabled)
254 reactivate_fd(chan->fd, irq);
e4dcee80
JD
255}
256
bed5e39c 257int write_chan(struct chan *chan, const char *buf, int len,
1da177e4
LT
258 int write_irq)
259{
1da177e4
LT
260 int n, ret = 0;
261
bed5e39c 262 if (len == 0 || !chan || !chan->ops->write)
c59dbcad
JD
263 return 0;
264
bed5e39c
AV
265 n = chan->ops->write(chan->fd, buf, len, chan->data);
266 if (chan->primary) {
267 ret = n;
268 if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
269 reactivate_fd(chan->fd, write_irq);
1da177e4 270 }
d50084a2 271 return ret;
1da177e4
LT
272}
273
bed5e39c 274int console_write_chan(struct chan *chan, const char *buf, int len)
1da177e4 275{
1da177e4
LT
276 int n, ret = 0;
277
bed5e39c
AV
278 if (!chan || !chan->ops->console_write)
279 return 0;
e99525f9 280
bed5e39c
AV
281 n = chan->ops->console_write(chan->fd, buf, len);
282 if (chan->primary)
283 ret = n;
d50084a2 284 return ret;
1da177e4
LT
285}
286
a52f362f 287int console_open_chan(struct line *line, struct console *co)
1da177e4 288{
1f80171e
JD
289 int err;
290
291 err = open_chan(&line->chan_list);
e99525f9 292 if (err)
1f80171e 293 return err;
1da177e4 294
e99525f9
JD
295 printk(KERN_INFO "Console initialized on /dev/%s%d\n", co->name,
296 co->index);
1da177e4
LT
297 return 0;
298}
299
bed5e39c 300int chan_window_size(struct line *line, unsigned short *rows_out,
1da177e4
LT
301 unsigned short *cols_out)
302{
1da177e4
LT
303 struct chan *chan;
304
bed5e39c
AV
305 chan = line->chan_in;
306 if (chan && chan->primary) {
307 if (chan->ops->window_size == NULL)
308 return 0;
309 return chan->ops->window_size(chan->fd, chan->data,
310 rows_out, cols_out);
311 }
312 chan = line->chan_out;
313 if (chan && chan->primary) {
314 if (chan->ops->window_size == NULL)
315 return 0;
316 return chan->ops->window_size(chan->fd, chan->data,
317 rows_out, cols_out);
1da177e4 318 }
d50084a2 319 return 0;
1da177e4
LT
320}
321
772bd0a5 322static void free_one_chan(struct chan *chan)
1da177e4
LT
323{
324 list_del(&chan->list);
165dc591 325
772bd0a5 326 close_one_chan(chan, 0);
165dc591 327
e99525f9 328 if (chan->ops->free != NULL)
1da177e4 329 (*chan->ops->free)(chan->data);
165dc591 330
e99525f9
JD
331 if (chan->primary && chan->output)
332 ignore_sigio_fd(chan->fd);
1da177e4
LT
333 kfree(chan);
334}
335
772bd0a5 336static void free_chan(struct list_head *chans)
1da177e4
LT
337{
338 struct list_head *ele, *next;
339 struct chan *chan;
340
e99525f9 341 list_for_each_safe(ele, next, chans) {
1da177e4 342 chan = list_entry(ele, struct chan, list);
772bd0a5 343 free_one_chan(chan);
1da177e4
LT
344 }
345}
346
347static int one_chan_config_string(struct chan *chan, char *str, int size,
348 char **error_out)
349{
350 int n = 0;
351
e99525f9 352 if (chan == NULL) {
1da177e4 353 CONFIG_CHUNK(str, size, n, "none", 1);
d50084a2 354 return n;
1da177e4
LT
355 }
356
357 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
358
e99525f9 359 if (chan->dev == NULL) {
1da177e4 360 CONFIG_CHUNK(str, size, n, "", 1);
d50084a2 361 return n;
1da177e4
LT
362 }
363
364 CONFIG_CHUNK(str, size, n, ":", 0);
365 CONFIG_CHUNK(str, size, n, chan->dev, 0);
366
d50084a2 367 return n;
1da177e4
LT
368}
369
d50084a2 370static int chan_pair_config_string(struct chan *in, struct chan *out,
1da177e4
LT
371 char *str, int size, char **error_out)
372{
373 int n;
374
375 n = one_chan_config_string(in, str, size, error_out);
376 str += n;
377 size -= n;
378
e99525f9 379 if (in == out) {
1da177e4 380 CONFIG_CHUNK(str, size, n, "", 1);
d50084a2 381 return n;
1da177e4
LT
382 }
383
384 CONFIG_CHUNK(str, size, n, ",", 1);
385 n = one_chan_config_string(out, str, size, error_out);
386 str += n;
387 size -= n;
388 CONFIG_CHUNK(str, size, n, "", 1);
389
d50084a2 390 return n;
1da177e4
LT
391}
392
bed5e39c 393int chan_config_string(struct line *line, char *str, int size,
1da177e4
LT
394 char **error_out)
395{
bed5e39c 396 struct chan *in = line->chan_in, *out = line->chan_out;
1da177e4 397
bed5e39c
AV
398 if (in && !in->primary)
399 in = NULL;
400 if (out && !out->primary)
401 out = NULL;
1da177e4 402
d50084a2 403 return chan_pair_config_string(in, out, str, size, error_out);
1da177e4
LT
404}
405
406struct chan_type {
407 char *key;
5e7672ec 408 const struct chan_ops *ops;
1da177e4
LT
409};
410
5e7672ec 411static const struct chan_type chan_table[] = {
1da177e4
LT
412 { "fd", &fd_ops },
413
414#ifdef CONFIG_NULL_CHAN
415 { "null", &null_ops },
416#else
417 { "null", &not_configged_ops },
418#endif
419
420#ifdef CONFIG_PORT_CHAN
421 { "port", &port_ops },
422#else
423 { "port", &not_configged_ops },
424#endif
425
426#ifdef CONFIG_PTY_CHAN
427 { "pty", &pty_ops },
428 { "pts", &pts_ops },
429#else
430 { "pty", &not_configged_ops },
431 { "pts", &not_configged_ops },
432#endif
433
434#ifdef CONFIG_TTY_CHAN
435 { "tty", &tty_ops },
436#else
437 { "tty", &not_configged_ops },
438#endif
439
440#ifdef CONFIG_XTERM_CHAN
441 { "xterm", &xterm_ops },
442#else
443 { "xterm", &not_configged_ops },
444#endif
445};
446
165dc591 447static struct chan *parse_chan(struct line *line, char *str, int device,
f28169d2 448 const struct chan_opts *opts, char **error_out)
1da177e4 449{
5e7672ec
JD
450 const struct chan_type *entry;
451 const struct chan_ops *ops;
1da177e4
LT
452 struct chan *chan;
453 void *data;
454 int i;
455
456 ops = NULL;
457 data = NULL;
e99525f9 458 for(i = 0; i < ARRAY_SIZE(chan_table); i++) {
1da177e4 459 entry = &chan_table[i];
e99525f9 460 if (!strncmp(str, entry->key, strlen(entry->key))) {
1da177e4
LT
461 ops = entry->ops;
462 str += strlen(entry->key);
463 break;
464 }
465 }
e99525f9 466 if (ops == NULL) {
f28169d2 467 *error_out = "No match for configured backends";
d50084a2 468 return NULL;
1da177e4 469 }
f28169d2 470
1da177e4 471 data = (*ops->init)(str, device, opts);
e99525f9 472 if (data == NULL) {
f28169d2 473 *error_out = "Configuration failed";
d50084a2 474 return NULL;
f28169d2 475 }
1da177e4 476
79ae2cb8 477 chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
e99525f9 478 if (chan == NULL) {
f28169d2 479 *error_out = "Memory allocation failed";
d50084a2 480 return NULL;
f28169d2 481 }
1da177e4 482 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
165dc591
JD
483 .free_list =
484 LIST_HEAD_INIT(chan->free_list),
485 .line = line,
1da177e4
LT
486 .primary = 1,
487 .input = 0,
488 .output = 0,
489 .opened = 0,
165dc591 490 .enabled = 0,
1da177e4 491 .fd = -1,
1da177e4
LT
492 .ops = ops,
493 .data = data });
d50084a2 494 return chan;
1da177e4
LT
495}
496
165dc591 497int parse_chan_pair(char *str, struct line *line, int device,
f28169d2 498 const struct chan_opts *opts, char **error_out)
1da177e4 499{
165dc591 500 struct list_head *chans = &line->chan_list;
f1c93e49 501 struct chan *new;
1da177e4
LT
502 char *in, *out;
503
e99525f9 504 if (!list_empty(chans)) {
ee485070 505 line->chan_in = line->chan_out = NULL;
772bd0a5 506 free_chan(chans);
1da177e4
LT
507 INIT_LIST_HEAD(chans);
508 }
509
31efcebb
AV
510 if (!str)
511 return 0;
512
1da177e4 513 out = strchr(str, ',');
e99525f9 514 if (out != NULL) {
1da177e4
LT
515 in = str;
516 *out = '\0';
517 out++;
f28169d2 518 new = parse_chan(line, in, device, opts, error_out);
e99525f9 519 if (new == NULL)
d50084a2
JD
520 return -1;
521
1da177e4
LT
522 new->input = 1;
523 list_add(&new->list, chans);
ee485070 524 line->chan_in = new;
1da177e4 525
f28169d2 526 new = parse_chan(line, out, device, opts, error_out);
e99525f9 527 if (new == NULL)
d50084a2
JD
528 return -1;
529
1da177e4
LT
530 list_add(&new->list, chans);
531 new->output = 1;
ee485070 532 line->chan_out = new;
1da177e4
LT
533 }
534 else {
f28169d2 535 new = parse_chan(line, str, device, opts, error_out);
e99525f9 536 if (new == NULL)
d50084a2
JD
537 return -1;
538
1da177e4
LT
539 list_add(&new->list, chans);
540 new->input = 1;
541 new->output = 1;
ee485070 542 line->chan_in = line->chan_out = new;
1da177e4 543 }
d50084a2 544 return 0;
1da177e4
LT
545}
546
2e124b4a 547void chan_interrupt(struct line *line, int irq)
1da177e4 548{
227434f8 549 struct tty_port *port = &line->port;
bed5e39c 550 struct chan *chan = line->chan_in;
1da177e4
LT
551 int err;
552 char c;
553
bed5e39c
AV
554 if (!chan || !chan->ops->read)
555 goto out;
556
557 do {
227434f8 558 if (!tty_buffer_request_room(port, 1)) {
0fcd7199 559 schedule_delayed_work(&line->task, 1);
bed5e39c
AV
560 goto out;
561 }
562 err = chan->ops->read(chan->fd, &c, chan->data);
563 if (err > 0)
92a19f9c 564 tty_insert_flip_char(port, c, TTY_NORMAL);
bed5e39c
AV
565 } while (err > 0);
566
567 if (err == 0)
568 reactivate_fd(chan->fd, irq);
569 if (err == -EIO) {
570 if (chan->primary) {
aa27a094 571 tty_port_tty_hangup(&line->port, false);
10c890c0
AV
572 if (line->chan_out != chan)
573 close_one_chan(line->chan_out, 1);
1da177e4 574 }
10c890c0
AV
575 close_one_chan(chan, 1);
576 if (chan->primary)
577 return;
1da177e4
LT
578 }
579 out:
2e124b4a 580 tty_flip_buffer_push(port);
1da177e4 581}