1dbddf429SAlex Dewar // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
363920f47SJeff Dike * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
41da177e4SLinus Torvalds */
51da177e4SLinus Torvalds
663920f47SJeff Dike #include <linux/slab.h>
763920f47SJeff Dike #include <linux/completion.h>
863920f47SJeff Dike #include <linux/irqreturn.h>
963920f47SJeff Dike #include <asm/irq.h>
1037185b33SAl Viro #include <irq_kern.h>
1137185b33SAl Viro #include <os.h>
12ab7ca2ebSNick Desaulniers #include "xterm.h"
131da177e4SLinus Torvalds
141da177e4SLinus Torvalds struct xterm_wait {
151da177e4SLinus Torvalds struct completion ready;
161da177e4SLinus Torvalds int fd;
171da177e4SLinus Torvalds int pid;
181da177e4SLinus Torvalds int new_fd;
191da177e4SLinus Torvalds };
201da177e4SLinus Torvalds
xterm_interrupt(int irq,void * data)217bea96fdSAl Viro static irqreturn_t xterm_interrupt(int irq, void *data)
221da177e4SLinus Torvalds {
231da177e4SLinus Torvalds struct xterm_wait *xterm = data;
24*5cde6096SJohannes Berg int fd = -1, n_fds = 1;
25*5cde6096SJohannes Berg ssize_t ret;
261da177e4SLinus Torvalds
27*5cde6096SJohannes Berg ret = os_rcv_fd_msg(xterm->fd, &fd, n_fds,
28*5cde6096SJohannes Berg &xterm->pid, sizeof(xterm->pid));
29*5cde6096SJohannes Berg if (ret == -EAGAIN)
3063920f47SJeff Dike return IRQ_NONE;
311da177e4SLinus Torvalds
32*5cde6096SJohannes Berg if (ret < 0)
33*5cde6096SJohannes Berg fd = ret;
34*5cde6096SJohannes Berg else if (ret != sizeof(xterm->pid))
35*5cde6096SJohannes Berg fd = -EMSGSIZE;
36*5cde6096SJohannes Berg
371da177e4SLinus Torvalds xterm->new_fd = fd;
381da177e4SLinus Torvalds complete(&xterm->ready);
3963920f47SJeff Dike
4063920f47SJeff Dike return IRQ_HANDLED;
411da177e4SLinus Torvalds }
421da177e4SLinus Torvalds
xterm_fd(int socket,int * pid_out)431da177e4SLinus Torvalds int xterm_fd(int socket, int *pid_out)
441da177e4SLinus Torvalds {
451da177e4SLinus Torvalds struct xterm_wait *data;
461da177e4SLinus Torvalds int err, ret;
471da177e4SLinus Torvalds
481da177e4SLinus Torvalds data = kmalloc(sizeof(*data), GFP_KERNEL);
491da177e4SLinus Torvalds if (data == NULL) {
501da177e4SLinus Torvalds printk(KERN_ERR "xterm_fd : failed to allocate xterm_wait\n");
5163920f47SJeff Dike return -ENOMEM;
521da177e4SLinus Torvalds }
531da177e4SLinus Torvalds
541da177e4SLinus Torvalds /* This is a locked semaphore... */
5563920f47SJeff Dike *data = ((struct xterm_wait) { .fd = socket,
561da177e4SLinus Torvalds .pid = -1,
571da177e4SLinus Torvalds .new_fd = -1 });
581da177e4SLinus Torvalds init_completion(&data->ready);
591da177e4SLinus Torvalds
601da177e4SLinus Torvalds err = um_request_irq(XTERM_IRQ, socket, IRQ_READ, xterm_interrupt,
61aab94460STheodore Ts'o IRQF_SHARED, "xterm", data);
6236d46a59SJohannes Berg if (err < 0) {
631da177e4SLinus Torvalds printk(KERN_ERR "xterm_fd : failed to get IRQ for xterm, "
641da177e4SLinus Torvalds "err = %d\n", err);
651da177e4SLinus Torvalds ret = err;
661da177e4SLinus Torvalds goto out;
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds
691da177e4SLinus Torvalds /* ... so here we wait for an xterm interrupt.
701da177e4SLinus Torvalds *
711da177e4SLinus Torvalds * XXX Note, if the xterm doesn't work for some reason (eg. DISPLAY
721da177e4SLinus Torvalds * isn't set) this will hang... */
731da177e4SLinus Torvalds wait_for_completion(&data->ready);
741da177e4SLinus Torvalds
75fa7a0449SRichard Weinberger um_free_irq(XTERM_IRQ, data);
761da177e4SLinus Torvalds
771da177e4SLinus Torvalds ret = data->new_fd;
781da177e4SLinus Torvalds *pid_out = data->pid;
791da177e4SLinus Torvalds out:
801da177e4SLinus Torvalds kfree(data);
811da177e4SLinus Torvalds
8263920f47SJeff Dike return ret;
831da177e4SLinus Torvalds }
84