irq.c 2.8 KB
Newer Older
1
/*
J
Jeff Dike 已提交
2
 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 4 5 6 7
 * Licensed under the GPL
 */

#include <stdlib.h>
#include <errno.h>
J
Jeff Dike 已提交
8
#include <poll.h>
9 10
#include <signal.h>
#include <string.h>
11 12 13
#include <irq_user.h>
#include <os.h>
#include <um_malloc.h>
14

J
Jeff Dike 已提交
15 16 17 18
/*
 * Locked by irq_lock in arch/um/kernel/irq.c.  Changed by os_create_pollfd
 * and os_free_irq_by_cb, which are called under irq_lock.
 */
19 20 21 22 23 24 25 26 27 28
static struct pollfd *pollfds = NULL;
static int pollfds_num = 0;
static int pollfds_size = 0;

int os_waiting_for_events(struct irq_fd *active_fds)
{
	struct irq_fd *irq_fd;
	int i, n, err;

	n = poll(pollfds, pollfds_num, 0);
29
	if (n < 0) {
30
		err = -errno;
31
		if (errno != EINTR)
J
Jeff Dike 已提交
32
			printk(UM_KERN_ERR "os_waiting_for_events:"
33 34 35 36
			       " poll returned %d, errno = %d\n", n, errno);
		return err;
	}

37
	if (n == 0)
38 39 40 41
		return 0;

	irq_fd = active_fds;

42 43
	for (i = 0; i < pollfds_num; i++) {
		if (pollfds[i].revents != 0) {
44 45 46 47 48 49 50 51 52 53 54 55 56
			irq_fd->current_events = pollfds[i].revents;
			pollfds[i].fd = -1;
		}
		irq_fd = irq_fd->next;
	}
	return n;
}

int os_create_pollfd(int fd, int events, void *tmp_pfd, int size_tmpfds)
{
	if (pollfds_num == pollfds_size) {
		if (size_tmpfds <= pollfds_size * sizeof(pollfds[0])) {
			/* return min size needed for new pollfds area */
J
Jeff Dike 已提交
57
			return (pollfds_size + 1) * sizeof(pollfds[0]);
58 59
		}

60
		if (pollfds != NULL) {
61 62 63 64 65 66 67
			memcpy(tmp_pfd, pollfds,
			       sizeof(pollfds[0]) * pollfds_size);
			/* remove old pollfds */
			kfree(pollfds);
		}
		pollfds = tmp_pfd;
		pollfds_size++;
68 69
	} else
		kfree(tmp_pfd);	/* remove not used tmp_pfd */
70

71 72 73
	pollfds[pollfds_num] = ((struct pollfd) { .fd		= fd,
						  .events	= events,
						  .revents	= 0 });
74 75
	pollfds_num++;

76
	return 0;
77 78 79 80 81 82 83 84 85
}

void os_free_irq_by_cb(int (*test)(struct irq_fd *, void *), void *arg,
		struct irq_fd *active_fds, struct irq_fd ***last_irq_ptr2)
{
	struct irq_fd **prev;
	int i = 0;

	prev = &active_fds;
86 87
	while (*prev != NULL) {
		if ((*test)(*prev, arg)) {
88
			struct irq_fd *old_fd = *prev;
89 90
			if ((pollfds[i].fd != -1) &&
			    (pollfds[i].fd != (*prev)->fd)) {
J
Jeff Dike 已提交
91 92 93
				printk(UM_KERN_ERR "os_free_irq_by_cb - "
				       "mismatch between active_fds and "
				       "pollfds, fd %d vs %d\n",
94 95 96 97 98 99
				       (*prev)->fd, pollfds[i].fd);
				goto out;
			}

			pollfds_num--;

J
Jeff Dike 已提交
100 101
			/*
			 * This moves the *whole* array after pollfds[i]
102 103 104 105
			 * (though it doesn't spot as such)!
			 */
			memmove(&pollfds[i], &pollfds[i + 1],
			       (pollfds_num - i) * sizeof(pollfds[0]));
J
Jeff Dike 已提交
106
			if (*last_irq_ptr2 == &old_fd->next)
107 108 109
				*last_irq_ptr2 = prev;

			*prev = (*prev)->next;
J
Jeff Dike 已提交
110
			if (old_fd->type == IRQ_WRITE)
111 112 113 114 115 116 117 118 119 120 121 122 123
				ignore_sigio_fd(old_fd->fd);
			kfree(old_fd);
			continue;
		}
		prev = &(*prev)->next;
		i++;
	}
 out:
	return;
}

int os_get_pollfd(int i)
{
124
	return pollfds[i].fd;
125 126 127 128 129 130 131 132 133
}

void os_set_pollfd(int i, int fd)
{
	pollfds[i].fd = fd;
}

void os_set_ioignore(void)
{
134
	signal(SIGIO, SIG_IGN);
135
}