blob: e8f9957bfbf6bad39a17812298a194f4f39b882e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Dike63920f42007-07-15 23:38:52 -07002 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Jeff Dike63920f42007-07-15 23:38:52 -07006#include <linux/slab.h>
7#include <linux/completion.h>
8#include <linux/irqreturn.h>
9#include <asm/irq.h>
Al Viro37185b32012-10-08 03:27:32 +010010#include <irq_kern.h>
11#include <os.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13struct xterm_wait {
14 struct completion ready;
15 int fd;
16 int pid;
17 int new_fd;
18};
19
Al Viro7bea96f2006-10-08 22:49:34 +010020static irqreturn_t xterm_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
22 struct xterm_wait *xterm = data;
23 int fd;
24
25 fd = os_rcv_fd(xterm->fd, &xterm->pid);
Jeff Dike63920f42007-07-15 23:38:52 -070026 if (fd == -EAGAIN)
27 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29 xterm->new_fd = fd;
30 complete(&xterm->ready);
Jeff Dike63920f42007-07-15 23:38:52 -070031
32 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033}
34
35int xterm_fd(int socket, int *pid_out)
36{
37 struct xterm_wait *data;
38 int err, ret;
39
40 data = kmalloc(sizeof(*data), GFP_KERNEL);
Jeff Dike63920f42007-07-15 23:38:52 -070041 if (data == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 printk(KERN_ERR "xterm_fd : failed to allocate xterm_wait\n");
Jeff Dike63920f42007-07-15 23:38:52 -070043 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 }
45
46 /* This is a locked semaphore... */
Jeff Dike63920f42007-07-15 23:38:52 -070047 *data = ((struct xterm_wait) { .fd = socket,
48 .pid = -1,
49 .new_fd = -1 });
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 init_completion(&data->ready);
51
Jeff Dike63920f42007-07-15 23:38:52 -070052 err = um_request_irq(XTERM_IRQ, socket, IRQ_READ, xterm_interrupt,
Theodore Ts'oaab94462012-07-17 14:18:23 -040053 IRQF_SHARED, "xterm", data);
Jeff Dike63920f42007-07-15 23:38:52 -070054 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 printk(KERN_ERR "xterm_fd : failed to get IRQ for xterm, "
56 "err = %d\n", err);
57 ret = err;
58 goto out;
59 }
60
61 /* ... so here we wait for an xterm interrupt.
62 *
63 * XXX Note, if the xterm doesn't work for some reason (eg. DISPLAY
64 * isn't set) this will hang... */
65 wait_for_completion(&data->ready);
66
Richard Weinbergerfa7a0442012-04-17 22:37:13 +020067 um_free_irq(XTERM_IRQ, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 ret = data->new_fd;
70 *pid_out = data->pid;
71 out:
72 kfree(data);
73
Jeff Dike63920f42007-07-15 23:38:52 -070074 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}