Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 2 | * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 6 | #include <linux/slab.h> |
| 7 | #include <linux/completion.h> |
| 8 | #include <linux/irqreturn.h> |
| 9 | #include <asm/irq.h> |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 10 | #include <irq_kern.h> |
| 11 | #include <os.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
| 13 | struct xterm_wait { |
| 14 | struct completion ready; |
| 15 | int fd; |
| 16 | int pid; |
| 17 | int new_fd; |
| 18 | }; |
| 19 | |
Al Viro | 7bea96f | 2006-10-08 22:49:34 +0100 | [diff] [blame] | 20 | static irqreturn_t xterm_interrupt(int irq, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | { |
| 22 | struct xterm_wait *xterm = data; |
| 23 | int fd; |
| 24 | |
| 25 | fd = os_rcv_fd(xterm->fd, &xterm->pid); |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 26 | if (fd == -EAGAIN) |
| 27 | return IRQ_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | xterm->new_fd = fd; |
| 30 | complete(&xterm->ready); |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 31 | |
| 32 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | int 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 Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 41 | if (data == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | printk(KERN_ERR "xterm_fd : failed to allocate xterm_wait\n"); |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 43 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /* This is a locked semaphore... */ |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 47 | *data = ((struct xterm_wait) { .fd = socket, |
| 48 | .pid = -1, |
| 49 | .new_fd = -1 }); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | init_completion(&data->ready); |
| 51 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 52 | err = um_request_irq(XTERM_IRQ, socket, IRQ_READ, xterm_interrupt, |
Theodore Ts'o | aab9446 | 2012-07-17 14:18:23 -0400 | [diff] [blame] | 53 | IRQF_SHARED, "xterm", data); |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 54 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | 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 Weinberger | fa7a044 | 2012-04-17 22:37:13 +0200 | [diff] [blame] | 67 | um_free_irq(XTERM_IRQ, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | ret = data->new_fd; |
| 70 | *pid_out = data->pid; |
| 71 | out: |
| 72 | kfree(data); |
| 73 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 74 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | } |