xref: /openbmc/linux/arch/um/drivers/xterm_kern.c (revision 17bfcd6a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4  */
5 
6 #include <linux/slab.h>
7 #include <linux/completion.h>
8 #include <linux/irqreturn.h>
9 #include <asm/irq.h>
10 #include <irq_kern.h>
11 #include <os.h>
12 #include "xterm.h"
13 
14 struct xterm_wait {
15 	struct completion ready;
16 	int fd;
17 	int pid;
18 	int new_fd;
19 };
20 
21 static irqreturn_t xterm_interrupt(int irq, void *data)
22 {
23 	struct xterm_wait *xterm = data;
24 	int fd;
25 
26 	fd = os_rcv_fd(xterm->fd, &xterm->pid);
27 	if (fd == -EAGAIN)
28 		return IRQ_NONE;
29 
30 	xterm->new_fd = fd;
31 	complete(&xterm->ready);
32 
33 	return IRQ_HANDLED;
34 }
35 
36 int xterm_fd(int socket, int *pid_out)
37 {
38 	struct xterm_wait *data;
39 	int err, ret;
40 
41 	data = kmalloc(sizeof(*data), GFP_KERNEL);
42 	if (data == NULL) {
43 		printk(KERN_ERR "xterm_fd : failed to allocate xterm_wait\n");
44 		return -ENOMEM;
45 	}
46 
47 	/* This is a locked semaphore... */
48 	*data = ((struct xterm_wait) { .fd 		= socket,
49 				       .pid 		= -1,
50 				       .new_fd	 	= -1 });
51 	init_completion(&data->ready);
52 
53 	err = um_request_irq(XTERM_IRQ, socket, IRQ_READ, xterm_interrupt,
54 			     IRQF_SHARED, "xterm", data);
55 	if (err < 0) {
56 		printk(KERN_ERR "xterm_fd : failed to get IRQ for xterm, "
57 		       "err = %d\n",  err);
58 		ret = err;
59 		goto out;
60 	}
61 
62 	/* ... so here we wait for an xterm interrupt.
63 	 *
64 	 * XXX Note, if the xterm doesn't work for some reason (eg. DISPLAY
65 	 * isn't set) this will hang... */
66 	wait_for_completion(&data->ready);
67 
68 	um_free_irq(XTERM_IRQ, data);
69 
70 	ret = data->new_fd;
71 	*pid_out = data->pid;
72  out:
73 	kfree(data);
74 
75 	return ret;
76 }
77