uml: tidy kern_util.h
[deliverable/linux.git] / arch / um / os-Linux / irq.c
1 /*
2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <signal.h>
10 #include <string.h>
11 #include <sys/poll.h>
12 #include <sys/types.h>
13 #include <sys/time.h>
14 #include "user.h"
15 #include "process.h"
16 #include "sigio.h"
17 #include "irq_user.h"
18 #include "os.h"
19 #include "um_malloc.h"
20
21 /*
22 * Locked by irq_lock in arch/um/kernel/irq.c. Changed by os_create_pollfd
23 * and os_free_irq_by_cb, which are called under irq_lock.
24 */
25 static struct pollfd *pollfds = NULL;
26 static int pollfds_num = 0;
27 static int pollfds_size = 0;
28
29 int os_waiting_for_events(struct irq_fd *active_fds)
30 {
31 struct irq_fd *irq_fd;
32 int i, n, err;
33
34 n = poll(pollfds, pollfds_num, 0);
35 if (n < 0) {
36 err = -errno;
37 if (errno != EINTR)
38 printk("sigio_handler: os_waiting_for_events:"
39 " poll returned %d, errno = %d\n", n, errno);
40 return err;
41 }
42
43 if (n == 0)
44 return 0;
45
46 irq_fd = active_fds;
47
48 for (i = 0; i < pollfds_num; i++) {
49 if (pollfds[i].revents != 0) {
50 irq_fd->current_events = pollfds[i].revents;
51 pollfds[i].fd = -1;
52 }
53 irq_fd = irq_fd->next;
54 }
55 return n;
56 }
57
58 int os_create_pollfd(int fd, int events, void *tmp_pfd, int size_tmpfds)
59 {
60 if (pollfds_num == pollfds_size) {
61 if (size_tmpfds <= pollfds_size * sizeof(pollfds[0])) {
62 /* return min size needed for new pollfds area */
63 return (pollfds_size + 1) * sizeof(pollfds[0]);
64 }
65
66 if (pollfds != NULL) {
67 memcpy(tmp_pfd, pollfds,
68 sizeof(pollfds[0]) * pollfds_size);
69 /* remove old pollfds */
70 kfree(pollfds);
71 }
72 pollfds = tmp_pfd;
73 pollfds_size++;
74 } else
75 kfree(tmp_pfd); /* remove not used tmp_pfd */
76
77 pollfds[pollfds_num] = ((struct pollfd) { .fd = fd,
78 .events = events,
79 .revents = 0 });
80 pollfds_num++;
81
82 return 0;
83 }
84
85 void os_free_irq_by_cb(int (*test)(struct irq_fd *, void *), void *arg,
86 struct irq_fd *active_fds, struct irq_fd ***last_irq_ptr2)
87 {
88 struct irq_fd **prev;
89 int i = 0;
90
91 prev = &active_fds;
92 while (*prev != NULL) {
93 if ((*test)(*prev, arg)) {
94 struct irq_fd *old_fd = *prev;
95 if ((pollfds[i].fd != -1) &&
96 (pollfds[i].fd != (*prev)->fd)) {
97 printk("os_free_irq_by_cb - mismatch between "
98 "active_fds and pollfds, fd %d vs %d\n",
99 (*prev)->fd, pollfds[i].fd);
100 goto out;
101 }
102
103 pollfds_num--;
104
105 /* This moves the *whole* array after pollfds[i]
106 * (though it doesn't spot as such)!
107 */
108 memmove(&pollfds[i], &pollfds[i + 1],
109 (pollfds_num - i) * sizeof(pollfds[0]));
110 if(*last_irq_ptr2 == &old_fd->next)
111 *last_irq_ptr2 = prev;
112
113 *prev = (*prev)->next;
114 if(old_fd->type == IRQ_WRITE)
115 ignore_sigio_fd(old_fd->fd);
116 kfree(old_fd);
117 continue;
118 }
119 prev = &(*prev)->next;
120 i++;
121 }
122 out:
123 return;
124 }
125
126 int os_get_pollfd(int i)
127 {
128 return pollfds[i].fd;
129 }
130
131 void os_set_pollfd(int i, int fd)
132 {
133 pollfds[i].fd = fd;
134 }
135
136 void os_set_ioignore(void)
137 {
138 signal(SIGIO, SIG_IGN);
139 }
140
141 void init_irq_signals(int on_sigstack)
142 {
143 int flags;
144
145 flags = on_sigstack ? SA_ONSTACK : 0;
146
147 set_handler(SIGIO, (__sighandler_t) sig_handler, flags | SA_RESTART,
148 SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
149 signal(SIGWINCH, SIG_IGN);
150 }
This page took 0.036952 seconds and 5 git commands to generate.