WorkQueue: Fix up arch-specific work items where possible
[deliverable/linux.git] / arch / um / drivers / chan_kern.c
CommitLineData
165dc591 1/*
1da177e4
LT
2 * 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
fac97ae0
PBG
22/* XXX: could well be moved to somewhere else, if needed. */
23static int my_printf(const char * fmt, ...)
24 __attribute__ ((format (printf, 1, 2)));
84ddaa8c 25
fac97ae0
PBG
26static 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}
84ddaa8c 56
1da177e4
LT
57static void *not_configged_init(char *str, int device, struct chan_opts *opts)
58{
fac97ae0 59 my_puts("Using a channel type which is configured out of "
1da177e4 60 "UML\n");
d50084a2 61 return NULL;
1da177e4
LT
62}
63
64static int not_configged_open(int input, int output, int primary, void *data,
65 char **dev_out)
66{
fac97ae0 67 my_puts("Using a channel type which is configured out of "
1da177e4 68 "UML\n");
d50084a2 69 return -ENODEV;
1da177e4
LT
70}
71
72static void not_configged_close(int fd, void *data)
73{
fac97ae0 74 my_puts("Using a channel type which is configured out of "
1da177e4
LT
75 "UML\n");
76}
77
78static int not_configged_read(int fd, char *c_out, void *data)
79{
fac97ae0 80 my_puts("Using a channel type which is configured out of "
1da177e4 81 "UML\n");
d50084a2 82 return -EIO;
1da177e4
LT
83}
84
85static int not_configged_write(int fd, const char *buf, int len, void *data)
86{
fac97ae0 87 my_puts("Using a channel type which is configured out of "
1da177e4 88 "UML\n");
d50084a2 89 return -EIO;
1da177e4
LT
90}
91
55c033c1 92static int not_configged_console_write(int fd, const char *buf, int len)
1da177e4 93{
fac97ae0 94 my_puts("Using a channel type which is configured out of "
1da177e4 95 "UML\n");
d50084a2 96 return -EIO;
1da177e4
LT
97}
98
99static int not_configged_window_size(int fd, void *data, unsigned short *rows,
100 unsigned short *cols)
101{
fac97ae0 102 my_puts("Using a channel type which is configured out of "
1da177e4 103 "UML\n");
d50084a2 104 return -ENODEV;
1da177e4
LT
105}
106
107static void not_configged_free(void *data)
108{
fac97ae0 109 my_puts("Using a channel type which is configured out of "
1da177e4
LT
110 "UML\n");
111}
112
5e7672ec 113static const struct chan_ops not_configged_ops = {
1da177e4
LT
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)
d50084a2 138 return 0;
1da177e4 139 else if(n == 0)
d50084a2
JD
140 return -EIO;
141 return n;
1da177e4
LT
142}
143
144/* XXX Trivial wrapper around os_write_file */
145
146int generic_write(int fd, const char *buf, int n, void *unused)
147{
d50084a2 148 return os_write_file(fd, buf, n);
1da177e4
LT
149}
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)
d50084a2 159 return ret;
1da177e4
LT
160
161 ret = ((*rows_out != rows) || (*cols_out != cols));
162
163 *rows_out = rows;
164 *cols_out = cols;
165
d50084a2 166 return ret;
1da177e4
LT
167}
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
1da177e4
LT
189 tty_insert_flip_char(tty, ch, TTY_NORMAL);
190}
191
d50084a2 192static int open_one_chan(struct chan *chan)
1da177e4
LT
193{
194 int fd;
195
d50084a2
JD
196 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;
1da177e4
LT
205 chan->fd = fd;
206
207 chan->opened = 1;
d50084a2 208 return 0;
1da177e4
LT
209}
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);
d50084a2
JD
219 ret = open_one_chan(chan);
220 if(chan->primary)
221 err = ret;
1da177e4 222 }
d50084a2 223 return err;
1da177e4
LT
224}
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
165dc591 240void enable_chan(struct line *line)
1da177e4
LT
241{
242 struct list_head *ele;
243 struct chan *chan;
244
165dc591 245 list_for_each(ele, &line->chan_list){
1da177e4 246 chan = list_entry(ele, struct chan, list);
165dc591
JD
247 if(open_one_chan(chan))
248 continue;
249
250 if(chan->enabled)
251 continue;
252 line_setup_irq(chan->fd, chan->input, chan->output, line,
253 chan);
254 chan->enabled = 1;
255 }
256}
257
258static 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;
1da177e4 280
165dc591
JD
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;
1da177e4 290 }
165dc591
JD
291 if(chan->ops->close != NULL)
292 (*chan->ops->close)(chan->fd, chan->data);
293
294 chan->opened = 0;
295 chan->fd = -1;
1da177e4
LT
296}
297
165dc591 298void close_chan(struct list_head *chans, int delay_free_irq)
1da177e4
LT
299{
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) {
165dc591 308 close_one_chan(chan, delay_free_irq);
1da177e4
LT
309 }
310}
311
e4dcee80
JD
312void 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
d50084a2 338int write_chan(struct list_head *chans, const char *buf, int len,
1da177e4
LT
339 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 }
d50084a2 356 return ret;
1da177e4
LT
357}
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;
55c033c1 369 n = chan->ops->console_write(chan->fd, buf, len);
1da177e4
LT
370 if(chan->primary) ret = n;
371 }
d50084a2 372 return ret;
1da177e4
LT
373}
374
d50084a2 375int console_open_chan(struct line *line, struct console *co,
5e7672ec 376 const struct chan_opts *opts)
1da177e4 377{
1f80171e
JD
378 int err;
379
380 err = open_chan(&line->chan_list);
381 if(err)
382 return err;
1da177e4 383
1da177e4
LT
384 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){
d50084a2
JD
397 if(chan->ops->window_size == NULL)
398 return 0;
399 return chan->ops->window_size(chan->fd, chan->data,
400 rows_out, cols_out);
1da177e4
LT
401 }
402 }
d50084a2 403 return 0;
1da177e4
LT
404}
405
42947cb9 406static void free_one_chan(struct chan *chan, int delay_free_irq)
1da177e4
LT
407{
408 list_del(&chan->list);
165dc591
JD
409
410 close_one_chan(chan, delay_free_irq);
411
1da177e4
LT
412 if(chan->ops->free != NULL)
413 (*chan->ops->free)(chan->data);
165dc591 414
1da177e4
LT
415 if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
416 kfree(chan);
417}
418
42947cb9 419static void free_chan(struct list_head *chans, int delay_free_irq)
1da177e4
LT
420{
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);
165dc591 426 free_one_chan(chan, delay_free_irq);
1da177e4
LT
427 }
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);
d50084a2 437 return n;
1da177e4
LT
438 }
439
440 CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
441
442 if(chan->dev == NULL){
443 CONFIG_CHUNK(str, size, n, "", 1);
d50084a2 444 return n;
1da177e4
LT
445 }
446
447 CONFIG_CHUNK(str, size, n, ":", 0);
448 CONFIG_CHUNK(str, size, n, chan->dev, 0);
449
d50084a2 450 return n;
1da177e4
LT
451}
452
d50084a2 453static int chan_pair_config_string(struct chan *in, struct chan *out,
1da177e4
LT
454 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);
d50084a2 464 return n;
1da177e4
LT
465 }
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
d50084a2 473 return n;
1da177e4
LT
474}
475
d50084a2 476int chan_config_string(struct list_head *chans, char *str, int size,
1da177e4
LT
477 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
d50084a2 492 return chan_pair_config_string(in, out, str, size, error_out);
1da177e4
LT
493}
494
495struct chan_type {
496 char *key;
5e7672ec 497 const struct chan_ops *ops;
1da177e4
LT
498};
499
5e7672ec 500static const struct chan_type chan_table[] = {
1da177e4
LT
501 { "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
165dc591 536static struct chan *parse_chan(struct line *line, char *str, int device,
5e7672ec 537 const struct chan_opts *opts)
1da177e4 538{
5e7672ec
JD
539 const struct chan_type *entry;
540 const struct chan_ops *ops;
1da177e4
LT
541 struct chan *chan;
542 void *data;
543 int i;
544
545 ops = NULL;
546 data = NULL;
91b165c0 547 for(i = 0; i < ARRAY_SIZE(chan_table); i++){
1da177e4
LT
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){
fac97ae0 556 my_printf("parse_chan couldn't parse \"%s\"\n",
1da177e4 557 str);
d50084a2 558 return NULL;
1da177e4 559 }
d50084a2
JD
560 if(ops->init == NULL)
561 return NULL;
1da177e4 562 data = (*ops->init)(str, device, opts);
d50084a2
JD
563 if(data == NULL)
564 return NULL;
1da177e4 565
79ae2cb8 566 chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
d50084a2
JD
567 if(chan == NULL)
568 return NULL;
1da177e4 569 *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
165dc591
JD
570 .free_list =
571 LIST_HEAD_INIT(chan->free_list),
572 .line = line,
1da177e4
LT
573 .primary = 1,
574 .input = 0,
575 .output = 0,
576 .opened = 0,
165dc591 577 .enabled = 0,
1da177e4 578 .fd = -1,
1da177e4
LT
579 .ops = ops,
580 .data = data });
d50084a2 581 return chan;
1da177e4
LT
582}
583
165dc591 584int parse_chan_pair(char *str, struct line *line, int device,
5e7672ec 585 const struct chan_opts *opts)
1da177e4 586{
165dc591 587 struct list_head *chans = &line->chan_list;
1da177e4
LT
588 struct chan *new, *chan;
589 char *in, *out;
590
591 if(!list_empty(chans)){
592 chan = list_entry(chans->next, struct chan, list);
165dc591 593 free_chan(chans, 0);
1da177e4
LT
594 INIT_LIST_HEAD(chans);
595 }
596
597 out = strchr(str, ',');
598 if(out != NULL){
599 in = str;
600 *out = '\0';
601 out++;
165dc591 602 new = parse_chan(line, in, device, opts);
d50084a2
JD
603 if(new == NULL)
604 return -1;
605
1da177e4
LT
606 new->input = 1;
607 list_add(&new->list, chans);
608
165dc591 609 new = parse_chan(line, out, device, opts);
d50084a2
JD
610 if(new == NULL)
611 return -1;
612
1da177e4
LT
613 list_add(&new->list, chans);
614 new->output = 1;
615 }
616 else {
165dc591 617 new = parse_chan(line, str, device, opts);
d50084a2
JD
618 if(new == NULL)
619 return -1;
620
1da177e4
LT
621 list_add(&new->list, chans);
622 new->input = 1;
623 new->output = 1;
624 }
d50084a2 625 return 0;
1da177e4
LT
626}
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)
d50084a2 636 return chan->fd;
1da177e4 637 }
d50084a2 638 return -1;
1da177e4
LT
639}
640
6d5aefb8 641void chan_interrupt(struct list_head *chans, struct delayed_work *task,
1da177e4
LT
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 {
33f0f88f 653 if (tty && !tty_buffer_request_room(tty, 1)) {
9159c9df 654 schedule_delayed_work(task, 1);
1da177e4
LT
655 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);
165dc591 667 close_chan(chans, 1);
1da177e4
LT
668 return;
669 }
165dc591 670 else close_one_chan(chan, 1);
1da177e4
LT
671 }
672 }
673 out:
674 if(tty) tty_flip_buffer_push(tty);
675}
This page took 0.209011 seconds and 5 git commands to generate.