ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / um / kernel / irq_user.c
1 /* 
2  * Copyright (C) 2000 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 <fcntl.h>
10 #include <signal.h>
11 #include <string.h>
12 #include <sys/poll.h>
13 #include <sys/types.h>
14 #include <sys/time.h>
15 #include "user_util.h"
16 #include "kern_util.h"
17 #include "user.h"
18 #include "process.h"
19 #include "signal_user.h"
20 #include "sigio.h"
21 #include "irq_user.h"
22 #include "os.h"
23
24 struct irq_fd {
25         struct irq_fd *next;
26         void *id;
27         int fd;
28         int type;
29         int irq;
30         int pid;
31         int events;
32         int current_events;
33         int freed;
34 };
35
36 static struct irq_fd *active_fds = NULL;
37 static struct irq_fd **last_irq_ptr = &active_fds;
38
39 static struct pollfd *pollfds = NULL;
40 static int pollfds_num = 0;
41 static int pollfds_size = 0;
42
43 extern int io_count, intr_count;
44
45 void sigio_handler(int sig, union uml_pt_regs *regs)
46 {
47         struct irq_fd *irq_fd, *next;
48         int i, n;
49
50         if(smp_sigio_handler()) return;
51         while(1){
52                 if((n = poll(pollfds, pollfds_num, 0)) < 0){
53                         if(errno == EINTR) continue;
54                         printk("sigio_handler : poll returned %d, "
55                                "errno = %d\n", n, errno);
56                         break;
57                 }
58                 if(n == 0) break;
59
60                 irq_fd = active_fds;
61                 for(i = 0; i < pollfds_num; i++){
62                         if(pollfds[i].revents != 0){
63                                 irq_fd->current_events = pollfds[i].revents;
64                                 pollfds[i].fd = -1;
65                         }
66                         irq_fd = irq_fd->next;
67                 }
68
69                 for(irq_fd = active_fds; irq_fd != NULL; irq_fd = next){
70                         next = irq_fd->next;
71                         if(irq_fd->current_events != 0){
72                                 irq_fd->current_events = 0;
73                                 do_IRQ(irq_fd->irq, regs);
74
75                                 /* This is here because the next irq may be
76                                  * freed in the handler.  If a console goes
77                                  * away, both the read and write irqs will be
78                                  * freed.  After do_IRQ, ->next will point to
79                                  * a good IRQ.
80                                  * Irqs can't be freed inside their handlers,
81                                  * so the next best thing is to have them
82                                  * marked as needing freeing, so that they
83                                  * can be freed here.
84                                  */
85                                 next = irq_fd->next;
86                                 if(irq_fd->freed)
87                                         free_irq(irq_fd->irq, irq_fd->id);
88                         }
89                 }
90         }
91 }
92
93 int activate_ipi(int fd, int pid)
94 {
95         return(os_set_fd_async(fd, pid));
96 }
97
98 static void maybe_sigio_broken(int fd, int type)
99 {
100         if(isatty(fd)){
101                 if((type == IRQ_WRITE) && !pty_output_sigio){
102                         write_sigio_workaround();
103                         add_sigio_fd(fd, 0);
104                 }
105                 else if((type == IRQ_READ) && !pty_close_sigio){
106                         write_sigio_workaround();
107                         add_sigio_fd(fd, 1);                    
108                 }
109         }
110 }
111
112 int activate_fd(int irq, int fd, int type, void *dev_id)
113 {
114         struct pollfd *tmp_pfd;
115         struct irq_fd *new_fd, *irq_fd;
116         unsigned long flags;
117         int pid, events, err, n, size;
118
119         pid = os_getpid();
120         err = os_set_fd_async(fd, pid);
121         if(err < 0)
122                 goto out;
123
124         new_fd = um_kmalloc(sizeof(*new_fd));
125         err = -ENOMEM;
126         if(new_fd == NULL)
127                 goto out;
128
129         if(type == IRQ_READ) events = POLLIN | POLLPRI;
130         else events = POLLOUT;
131         *new_fd = ((struct irq_fd) { .next              = NULL,
132                                      .id                = dev_id,
133                                      .fd                = fd,
134                                      .type              = type,
135                                      .irq               = irq,
136                                      .pid               = pid,
137                                      .events            = events,
138                                      .current_events    = 0,
139                                      .freed             = 0  } );
140
141         /* Critical section - locked by a spinlock because this stuff can
142          * be changed from interrupt handlers.  The stuff above is done 
143          * outside the lock because it allocates memory.
144          */
145
146         /* Actually, it only looks like it can be called from interrupt
147          * context.  The culprit is reactivate_fd, which calls 
148          * maybe_sigio_broken, which calls write_sigio_workaround,
149          * which calls activate_fd.  However, write_sigio_workaround should
150          * only be called once, at boot time.  That would make it clear that
151          * this is called only from process context, and can be locked with
152          * a semaphore.
153          */
154         flags = irq_lock();
155         for(irq_fd = active_fds; irq_fd != NULL; irq_fd = irq_fd->next){
156                 if((irq_fd->fd == fd) && (irq_fd->type == type)){
157                         printk("Registering fd %d twice\n", fd);
158                         printk("Irqs : %d, %d\n", irq_fd->irq, irq);
159                         printk("Ids : 0x%x, 0x%x\n", irq_fd->id, dev_id);
160                         goto out_unlock;
161                 }
162         }
163
164         n = pollfds_num;
165         if(n == pollfds_size){
166                 while(1){
167                         /* Here we have to drop the lock in order to call 
168                          * kmalloc, which might sleep.  If something else
169                          * came in and changed the pollfds array, we free
170                          * the buffer and try again.
171                          */
172                         irq_unlock(flags);
173                         size = (pollfds_num + 1) * sizeof(pollfds[0]);
174                         tmp_pfd = um_kmalloc(size);
175                         flags = irq_lock();
176                         if(tmp_pfd == NULL)
177                                 goto out_unlock;
178                         if(n == pollfds_size)
179                                 break;
180                         kfree(tmp_pfd);
181                 }
182                 if(pollfds != NULL){
183                         memcpy(tmp_pfd, pollfds,
184                                sizeof(pollfds[0]) * pollfds_size);
185                         kfree(pollfds);
186                 }
187                 pollfds = tmp_pfd;
188                 pollfds_size++;
189         }
190
191         if(type == IRQ_WRITE) 
192                 fd = -1;
193
194         pollfds[pollfds_num] = ((struct pollfd) { .fd   = fd,
195                                                   .events       = events,
196                                                   .revents      = 0 });
197         pollfds_num++;
198
199         *last_irq_ptr = new_fd;
200         last_irq_ptr = &new_fd->next;
201
202         irq_unlock(flags);
203
204         /* This calls activate_fd, so it has to be outside the critical
205          * section.
206          */
207         maybe_sigio_broken(fd, type);
208
209         return(0);
210
211  out_unlock:
212         irq_unlock(flags);
213         kfree(new_fd);
214  out:
215         return(err);
216 }
217
218 static void free_irq_by_cb(int (*test)(struct irq_fd *, void *), void *arg)
219 {
220         struct irq_fd **prev;
221         unsigned long flags;
222         int i = 0;
223
224         flags = irq_lock();
225         prev = &active_fds;
226         while(*prev != NULL){
227                 if((*test)(*prev, arg)){
228                         struct irq_fd *old_fd = *prev;
229                         if((pollfds[i].fd != -1) && 
230                            (pollfds[i].fd != (*prev)->fd)){
231                                 printk("free_irq_by_cb - mismatch between "
232                                        "active_fds and pollfds, fd %d vs %d\n",
233                                        (*prev)->fd, pollfds[i].fd);
234                                 goto out;
235                         }
236                         memcpy(&pollfds[i], &pollfds[i + 1],
237                                (pollfds_num - i - 1) * sizeof(pollfds[0]));
238                         pollfds_num--;
239                         if(last_irq_ptr == &old_fd->next) 
240                                 last_irq_ptr = prev;
241                         *prev = (*prev)->next;
242                         if(old_fd->type == IRQ_WRITE) 
243                                 ignore_sigio_fd(old_fd->fd);
244                         kfree(old_fd);
245                         continue;
246                 }
247                 prev = &(*prev)->next;
248                 i++;
249         }
250  out:
251         irq_unlock(flags);
252 }
253
254 struct irq_and_dev {
255         int irq;
256         void *dev;
257 };
258
259 static int same_irq_and_dev(struct irq_fd *irq, void *d)
260 {
261         struct irq_and_dev *data = d;
262
263         return((irq->irq == data->irq) && (irq->id == data->dev));
264 }
265
266 void free_irq_by_irq_and_dev(int irq, void *dev)
267 {
268         struct irq_and_dev data = ((struct irq_and_dev) { .irq  = irq,
269                                                           .dev  = dev });
270
271         free_irq_by_cb(same_irq_and_dev, &data);
272 }
273
274 static int same_fd(struct irq_fd *irq, void *fd)
275 {
276         return(irq->fd == *((int *) fd));
277 }
278
279 void free_irq_by_fd(int fd)
280 {
281         free_irq_by_cb(same_fd, &fd);
282 }
283
284 static struct irq_fd *find_irq_by_fd(int fd, int irqnum, int *index_out)
285 {
286         struct irq_fd *irq;
287         int i = 0;
288
289         for(irq=active_fds; irq != NULL; irq = irq->next){
290                 if((irq->fd == fd) && (irq->irq == irqnum)) break;
291                 i++;
292         }
293         if(irq == NULL){
294                 printk("find_irq_by_fd doesn't have descriptor %d\n", fd);
295                 goto out;
296         }
297         if((pollfds[i].fd != -1) && (pollfds[i].fd != fd)){
298                 printk("find_irq_by_fd - mismatch between active_fds and "
299                        "pollfds, fd %d vs %d, need %d\n", irq->fd, 
300                        pollfds[i].fd, fd);
301                 irq = NULL;
302                 goto out;
303         }
304         *index_out = i;
305  out:
306         return(irq);
307 }
308
309 void free_irq_later(int irq, void *dev_id)
310 {
311         struct irq_fd *irq_fd;
312         unsigned long flags;
313
314         flags = irq_lock();
315         for(irq_fd = active_fds; irq_fd != NULL; irq_fd = irq_fd->next){
316                 if((irq_fd->irq == irq) && (irq_fd->id == dev_id))
317                         break;
318         }
319         if(irq_fd == NULL){
320                 printk("free_irq_later found no irq, irq = %d, "
321                        "dev_id = 0x%p\n", irq, dev_id);
322                 goto out;
323         }
324         irq_fd->freed = 1;
325  out:
326         irq_unlock(flags);
327 }
328
329 void reactivate_fd(int fd, int irqnum)
330 {
331         struct irq_fd *irq;
332         unsigned long flags;
333         int i;
334
335         flags = irq_lock();
336         irq = find_irq_by_fd(fd, irqnum, &i);
337         if(irq == NULL){
338                 irq_unlock(flags);
339                 return;
340         }
341
342         pollfds[i].fd = irq->fd;
343
344         irq_unlock(flags);
345
346         /* This calls activate_fd, so it has to be outside the critical
347          * section.
348          */
349         maybe_sigio_broken(fd, irq->type);
350 }
351
352 void deactivate_fd(int fd, int irqnum)
353 {
354         struct irq_fd *irq;
355         unsigned long flags;
356         int i;
357
358         flags = irq_lock();
359         irq = find_irq_by_fd(fd, irqnum, &i);
360         if(irq == NULL)
361                 goto out;
362         pollfds[i].fd = -1;
363  out:
364         irq_unlock(flags);
365 }
366
367 void forward_ipi(int fd, int pid)
368 {
369         if(fcntl(fd, F_SETOWN, pid) < 0){
370                 int save_errno = errno;
371                 if(fcntl(fd, F_GETOWN, 0) != pid){
372                         printk("forward_ipi: F_SETOWN failed, fd = %d, "
373                                "me = %d, target = %d, errno = %d\n", fd, 
374                                os_getpid(), pid, save_errno);
375                 }
376         }
377 }
378
379 void forward_interrupts(int pid)
380 {
381         struct irq_fd *irq;
382         unsigned long flags;
383
384         flags = irq_lock();
385         for(irq=active_fds;irq != NULL;irq = irq->next){
386                 if(fcntl(irq->fd, F_SETOWN, pid) < 0){
387                         int save_errno = errno;
388                         if(fcntl(irq->fd, F_GETOWN, 0) != pid){
389                                 /* XXX Just remove the irq rather than
390                                  * print out an infinite stream of these
391                                  */
392                                 printk("Failed to forward %d to pid %d, "
393                                        "errno = %d\n", irq->fd, pid, 
394                                        save_errno);
395                         }
396                 }
397                 irq->pid = pid;
398         }
399         irq_unlock(flags);
400 }
401
402 void init_irq_signals(int on_sigstack)
403 {
404         __sighandler_t h;
405         int flags;
406
407         flags = on_sigstack ? SA_ONSTACK : 0;
408         if(timer_irq_inited) h = (__sighandler_t) alarm_handler;
409         else h = boot_timer_handler;
410
411         set_handler(SIGVTALRM, h, flags | SA_RESTART, 
412                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, -1);
413         set_handler(SIGIO, (__sighandler_t) sig_handler, flags | SA_RESTART,
414                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
415         signal(SIGWINCH, SIG_IGN);
416 }
417
418 /*
419  * Overrides for Emacs so that we follow Linus's tabbing style.
420  * Emacs will notice this stuff at the end of the file and automatically
421  * adjust the settings for this buffer only.  This must remain at the end
422  * of the file.
423  * ---------------------------------------------------------------------------
424  * Local variables:
425  * c-file-style: "linux"
426  * End:
427  */