blob: 0348b975e81cb3a5a4d067a8f3b39c5d2db1fff5 [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"
Jeff Dikefee64d32008-02-04 22:31:02 -080012#include "kern_constants.h"
Jeff Dike63ae2a92006-03-27 01:14:30 -080013#include "os.h"
Jeff Dikefee64d32008-02-04 22:31:02 -080014#include "process.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070015#include "um_malloc.h"
Jeff Dikefee64d32008-02-04 22:31:02 -080016#include "user.h"
Jeff Dike63ae2a92006-03-27 01:14:30 -080017
Jeff Dikef2e62992007-02-10 01:44:23 -080018/*
19 * Locked by irq_lock in arch/um/kernel/irq.c. Changed by os_create_pollfd
20 * and os_free_irq_by_cb, which are called under irq_lock.
21 */
Jeff Dike63ae2a92006-03-27 01:14:30 -080022static struct pollfd *pollfds = NULL;
23static int pollfds_num = 0;
24static int pollfds_size = 0;
25
26int os_waiting_for_events(struct irq_fd *active_fds)
27{
28 struct irq_fd *irq_fd;
29 int i, n, err;
30
31 n = poll(pollfds, pollfds_num, 0);
Jesper Juhl191ef962006-05-01 12:15:57 -070032 if (n < 0) {
Jeff Dike63ae2a92006-03-27 01:14:30 -080033 err = -errno;
Jesper Juhl191ef962006-05-01 12:15:57 -070034 if (errno != EINTR)
Jeff Dikefee64d32008-02-04 22:31:02 -080035 printk(UM_KERN_ERR "os_waiting_for_events:"
Jeff Dike63ae2a92006-03-27 01:14:30 -080036 " poll returned %d, errno = %d\n", n, errno);
37 return err;
38 }
39
Jesper Juhl191ef962006-05-01 12:15:57 -070040 if (n == 0)
Jeff Dike63ae2a92006-03-27 01:14:30 -080041 return 0;
42
43 irq_fd = active_fds;
44
Jesper Juhl191ef962006-05-01 12:15:57 -070045 for (i = 0; i < pollfds_num; i++) {
46 if (pollfds[i].revents != 0) {
Jeff Dike63ae2a92006-03-27 01:14:30 -080047 irq_fd->current_events = pollfds[i].revents;
48 pollfds[i].fd = -1;
49 }
50 irq_fd = irq_fd->next;
51 }
52 return n;
53}
54
Jeff Dike63ae2a92006-03-27 01:14:30 -080055int os_create_pollfd(int fd, int events, void *tmp_pfd, int size_tmpfds)
56{
57 if (pollfds_num == pollfds_size) {
58 if (size_tmpfds <= pollfds_size * sizeof(pollfds[0])) {
59 /* return min size needed for new pollfds area */
Jeff Dikef2e62992007-02-10 01:44:23 -080060 return (pollfds_size + 1) * sizeof(pollfds[0]);
Jeff Dike63ae2a92006-03-27 01:14:30 -080061 }
62
Jesper Juhl191ef962006-05-01 12:15:57 -070063 if (pollfds != NULL) {
Jeff Dike63ae2a92006-03-27 01:14:30 -080064 memcpy(tmp_pfd, pollfds,
65 sizeof(pollfds[0]) * pollfds_size);
66 /* remove old pollfds */
67 kfree(pollfds);
68 }
69 pollfds = tmp_pfd;
70 pollfds_size++;
Jesper Juhl191ef962006-05-01 12:15:57 -070071 } else
72 kfree(tmp_pfd); /* remove not used tmp_pfd */
Jeff Dike63ae2a92006-03-27 01:14:30 -080073
Jesper Juhl191ef962006-05-01 12:15:57 -070074 pollfds[pollfds_num] = ((struct pollfd) { .fd = fd,
75 .events = events,
76 .revents = 0 });
Jeff Dike63ae2a92006-03-27 01:14:30 -080077 pollfds_num++;
78
Jesper Juhl191ef962006-05-01 12:15:57 -070079 return 0;
Jeff Dike63ae2a92006-03-27 01:14:30 -080080}
81
82void os_free_irq_by_cb(int (*test)(struct irq_fd *, void *), void *arg,
83 struct irq_fd *active_fds, struct irq_fd ***last_irq_ptr2)
84{
85 struct irq_fd **prev;
86 int i = 0;
87
88 prev = &active_fds;
Jesper Juhl191ef962006-05-01 12:15:57 -070089 while (*prev != NULL) {
90 if ((*test)(*prev, arg)) {
Jeff Dike63ae2a92006-03-27 01:14:30 -080091 struct irq_fd *old_fd = *prev;
Jesper Juhl191ef962006-05-01 12:15:57 -070092 if ((pollfds[i].fd != -1) &&
93 (pollfds[i].fd != (*prev)->fd)) {
Jeff Dikefee64d32008-02-04 22:31:02 -080094 printk(UM_KERN_ERR "os_free_irq_by_cb - "
95 "mismatch between active_fds and "
96 "pollfds, fd %d vs %d\n",
Jeff Dike63ae2a92006-03-27 01:14:30 -080097 (*prev)->fd, pollfds[i].fd);
98 goto out;
99 }
100
101 pollfds_num--;
102
Jeff Dikefee64d32008-02-04 22:31:02 -0800103 /*
104 * This moves the *whole* array after pollfds[i]
Jeff Dike63ae2a92006-03-27 01:14:30 -0800105 * (though it doesn't spot as such)!
106 */
Jeff Dike63ae2a92006-03-27 01:14:30 -0800107 memmove(&pollfds[i], &pollfds[i + 1],
108 (pollfds_num - i) * sizeof(pollfds[0]));
Jeff Dikefee64d32008-02-04 22:31:02 -0800109 if (*last_irq_ptr2 == &old_fd->next)
Jeff Dike63ae2a92006-03-27 01:14:30 -0800110 *last_irq_ptr2 = prev;
111
112 *prev = (*prev)->next;
Jeff Dikefee64d32008-02-04 22:31:02 -0800113 if (old_fd->type == IRQ_WRITE)
Jeff Dike63ae2a92006-03-27 01:14:30 -0800114 ignore_sigio_fd(old_fd->fd);
115 kfree(old_fd);
116 continue;
117 }
118 prev = &(*prev)->next;
119 i++;
120 }
121 out:
122 return;
123}
124
Jeff Dike63ae2a92006-03-27 01:14:30 -0800125int os_get_pollfd(int i)
126{
Jesper Juhl191ef962006-05-01 12:15:57 -0700127 return pollfds[i].fd;
Jeff Dike63ae2a92006-03-27 01:14:30 -0800128}
129
130void os_set_pollfd(int i, int fd)
131{
132 pollfds[i].fd = fd;
133}
134
135void os_set_ioignore(void)
136{
Jeff Dike4b84c692006-09-25 23:33:04 -0700137 signal(SIGIO, SIG_IGN);
Jeff Dike63ae2a92006-03-27 01:14:30 -0800138}