blob: 9a49908b576c20c80c124854f04167081873cb7c [file] [log] [blame]
Jeff Dike63ae2a92006-03-27 01:14:30 -08001/*
Jeff Dikefee64d32008-02-04 22:31:02 -08002 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Jeff Dike63ae2a92006-03-27 01:14:30 -08003 * Licensed under the GPL
4 */
5
6#include <stdlib.h>
Jeff Dike63ae2a92006-03-27 01:14:30 -08007#include <errno.h>
Jeff Dikefee64d32008-02-04 22:31:02 -08008#include <poll.h>
Jeff Dike63ae2a92006-03-27 01:14:30 -08009#include <signal.h>
10#include <string.h>
Jeff Dike63ae2a92006-03-27 01:14:30 -080011#include "irq_user.h"
12#include "os.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070013#include "um_malloc.h"
Jeff Dike63ae2a92006-03-27 01:14:30 -080014
Jeff Dikef2e62992007-02-10 01:44:23 -080015/*
16 * Locked by irq_lock in arch/um/kernel/irq.c. Changed by os_create_pollfd
17 * and os_free_irq_by_cb, which are called under irq_lock.
18 */
Jeff Dike63ae2a92006-03-27 01:14:30 -080019static struct pollfd *pollfds = NULL;
20static int pollfds_num = 0;
21static int pollfds_size = 0;
22
23int os_waiting_for_events(struct irq_fd *active_fds)
24{
25 struct irq_fd *irq_fd;
26 int i, n, err;
27
28 n = poll(pollfds, pollfds_num, 0);
Jesper Juhl191ef962006-05-01 12:15:57 -070029 if (n < 0) {
Jeff Dike63ae2a92006-03-27 01:14:30 -080030 err = -errno;
Jesper Juhl191ef962006-05-01 12:15:57 -070031 if (errno != EINTR)
Jeff Dikefee64d32008-02-04 22:31:02 -080032 printk(UM_KERN_ERR "os_waiting_for_events:"
Jeff Dike63ae2a92006-03-27 01:14:30 -080033 " poll returned %d, errno = %d\n", n, errno);
34 return err;
35 }
36
Jesper Juhl191ef962006-05-01 12:15:57 -070037 if (n == 0)
Jeff Dike63ae2a92006-03-27 01:14:30 -080038 return 0;
39
40 irq_fd = active_fds;
41
Jesper Juhl191ef962006-05-01 12:15:57 -070042 for (i = 0; i < pollfds_num; i++) {
43 if (pollfds[i].revents != 0) {
Jeff Dike63ae2a92006-03-27 01:14:30 -080044 irq_fd->current_events = pollfds[i].revents;
45 pollfds[i].fd = -1;
46 }
47 irq_fd = irq_fd->next;
48 }
49 return n;
50}
51
Jeff Dike63ae2a92006-03-27 01:14:30 -080052int os_create_pollfd(int fd, int events, void *tmp_pfd, int size_tmpfds)
53{
54 if (pollfds_num == pollfds_size) {
55 if (size_tmpfds <= pollfds_size * sizeof(pollfds[0])) {
56 /* return min size needed for new pollfds area */
Jeff Dikef2e62992007-02-10 01:44:23 -080057 return (pollfds_size + 1) * sizeof(pollfds[0]);
Jeff Dike63ae2a92006-03-27 01:14:30 -080058 }
59
Jesper Juhl191ef962006-05-01 12:15:57 -070060 if (pollfds != NULL) {
Jeff Dike63ae2a92006-03-27 01:14:30 -080061 memcpy(tmp_pfd, pollfds,
62 sizeof(pollfds[0]) * pollfds_size);
63 /* remove old pollfds */
64 kfree(pollfds);
65 }
66 pollfds = tmp_pfd;
67 pollfds_size++;
Jesper Juhl191ef962006-05-01 12:15:57 -070068 } else
69 kfree(tmp_pfd); /* remove not used tmp_pfd */
Jeff Dike63ae2a92006-03-27 01:14:30 -080070
Jesper Juhl191ef962006-05-01 12:15:57 -070071 pollfds[pollfds_num] = ((struct pollfd) { .fd = fd,
72 .events = events,
73 .revents = 0 });
Jeff Dike63ae2a92006-03-27 01:14:30 -080074 pollfds_num++;
75
Jesper Juhl191ef962006-05-01 12:15:57 -070076 return 0;
Jeff Dike63ae2a92006-03-27 01:14:30 -080077}
78
79void os_free_irq_by_cb(int (*test)(struct irq_fd *, void *), void *arg,
80 struct irq_fd *active_fds, struct irq_fd ***last_irq_ptr2)
81{
82 struct irq_fd **prev;
83 int i = 0;
84
85 prev = &active_fds;
Jesper Juhl191ef962006-05-01 12:15:57 -070086 while (*prev != NULL) {
87 if ((*test)(*prev, arg)) {
Jeff Dike63ae2a92006-03-27 01:14:30 -080088 struct irq_fd *old_fd = *prev;
Jesper Juhl191ef962006-05-01 12:15:57 -070089 if ((pollfds[i].fd != -1) &&
90 (pollfds[i].fd != (*prev)->fd)) {
Jeff Dikefee64d32008-02-04 22:31:02 -080091 printk(UM_KERN_ERR "os_free_irq_by_cb - "
92 "mismatch between active_fds and "
93 "pollfds, fd %d vs %d\n",
Jeff Dike63ae2a92006-03-27 01:14:30 -080094 (*prev)->fd, pollfds[i].fd);
95 goto out;
96 }
97
98 pollfds_num--;
99
Jeff Dikefee64d32008-02-04 22:31:02 -0800100 /*
101 * This moves the *whole* array after pollfds[i]
Jeff Dike63ae2a92006-03-27 01:14:30 -0800102 * (though it doesn't spot as such)!
103 */
Jeff Dike63ae2a92006-03-27 01:14:30 -0800104 memmove(&pollfds[i], &pollfds[i + 1],
105 (pollfds_num - i) * sizeof(pollfds[0]));
Jeff Dikefee64d32008-02-04 22:31:02 -0800106 if (*last_irq_ptr2 == &old_fd->next)
Jeff Dike63ae2a92006-03-27 01:14:30 -0800107 *last_irq_ptr2 = prev;
108
109 *prev = (*prev)->next;
Jeff Dikefee64d32008-02-04 22:31:02 -0800110 if (old_fd->type == IRQ_WRITE)
Jeff Dike63ae2a92006-03-27 01:14:30 -0800111 ignore_sigio_fd(old_fd->fd);
112 kfree(old_fd);
113 continue;
114 }
115 prev = &(*prev)->next;
116 i++;
117 }
118 out:
119 return;
120}
121
Jeff Dike63ae2a92006-03-27 01:14:30 -0800122int os_get_pollfd(int i)
123{
Jesper Juhl191ef962006-05-01 12:15:57 -0700124 return pollfds[i].fd;
Jeff Dike63ae2a92006-03-27 01:14:30 -0800125}
126
127void os_set_pollfd(int i, int fd)
128{
129 pollfds[i].fd = fd;
130}
131
132void os_set_ioignore(void)
133{
Jeff Dike4b84c692006-09-25 23:33:04 -0700134 signal(SIGIO, SIG_IGN);
Jeff Dike63ae2a92006-03-27 01:14:30 -0800135}