18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/slab.h>
78c2ecf20Sopenharmony_ci#include <linux/completion.h>
88c2ecf20Sopenharmony_ci#include <linux/irqreturn.h>
98c2ecf20Sopenharmony_ci#include <asm/irq.h>
108c2ecf20Sopenharmony_ci#include <irq_kern.h>
118c2ecf20Sopenharmony_ci#include <os.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistruct xterm_wait {
148c2ecf20Sopenharmony_ci	struct completion ready;
158c2ecf20Sopenharmony_ci	int fd;
168c2ecf20Sopenharmony_ci	int pid;
178c2ecf20Sopenharmony_ci	int new_fd;
188c2ecf20Sopenharmony_ci};
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic irqreturn_t xterm_interrupt(int irq, void *data)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	struct xterm_wait *xterm = data;
238c2ecf20Sopenharmony_ci	int fd;
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	fd = os_rcv_fd(xterm->fd, &xterm->pid);
268c2ecf20Sopenharmony_ci	if (fd == -EAGAIN)
278c2ecf20Sopenharmony_ci		return IRQ_NONE;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	xterm->new_fd = fd;
308c2ecf20Sopenharmony_ci	complete(&xterm->ready);
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
338c2ecf20Sopenharmony_ci}
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ciint xterm_fd(int socket, int *pid_out)
368c2ecf20Sopenharmony_ci{
378c2ecf20Sopenharmony_ci	struct xterm_wait *data;
388c2ecf20Sopenharmony_ci	int err, ret;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	data = kmalloc(sizeof(*data), GFP_KERNEL);
418c2ecf20Sopenharmony_ci	if (data == NULL) {
428c2ecf20Sopenharmony_ci		printk(KERN_ERR "xterm_fd : failed to allocate xterm_wait\n");
438c2ecf20Sopenharmony_ci		return -ENOMEM;
448c2ecf20Sopenharmony_ci	}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	/* This is a locked semaphore... */
478c2ecf20Sopenharmony_ci	*data = ((struct xterm_wait) { .fd 		= socket,
488c2ecf20Sopenharmony_ci				       .pid 		= -1,
498c2ecf20Sopenharmony_ci				       .new_fd	 	= -1 });
508c2ecf20Sopenharmony_ci	init_completion(&data->ready);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	err = um_request_irq(XTERM_IRQ, socket, IRQ_READ, xterm_interrupt,
538c2ecf20Sopenharmony_ci			     IRQF_SHARED, "xterm", data);
548c2ecf20Sopenharmony_ci	if (err) {
558c2ecf20Sopenharmony_ci		printk(KERN_ERR "xterm_fd : failed to get IRQ for xterm, "
568c2ecf20Sopenharmony_ci		       "err = %d\n",  err);
578c2ecf20Sopenharmony_ci		ret = err;
588c2ecf20Sopenharmony_ci		goto out;
598c2ecf20Sopenharmony_ci	}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	/* ... so here we wait for an xterm interrupt.
628c2ecf20Sopenharmony_ci	 *
638c2ecf20Sopenharmony_ci	 * XXX Note, if the xterm doesn't work for some reason (eg. DISPLAY
648c2ecf20Sopenharmony_ci	 * isn't set) this will hang... */
658c2ecf20Sopenharmony_ci	wait_for_completion(&data->ready);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	um_free_irq(XTERM_IRQ, data);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	ret = data->new_fd;
708c2ecf20Sopenharmony_ci	*pid_out = data->pid;
718c2ecf20Sopenharmony_ci out:
728c2ecf20Sopenharmony_ci	kfree(data);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	return ret;
758c2ecf20Sopenharmony_ci}
76