| 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> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include "irq_kern.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, | 
| Thomas Gleixner | bd6aa65 | 2006-07-01 19:29:27 -0700 | [diff] [blame] | 53 | 			     IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | 			     "xterm", data); | 
| Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 55 | 	if (err) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | 	free_irq(XTERM_IRQ, data); | 
 | 69 |  | 
 | 70 | 	ret = data->new_fd; | 
 | 71 | 	*pid_out = data->pid; | 
 | 72 |  out: | 
 | 73 | 	kfree(data); | 
 | 74 |  | 
| Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 75 | 	return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |