irq.c 3.3 KB

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