irq.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdlib.h>
  6. #include <errno.h>
  7. #include <poll.h>
  8. #include <signal.h>
  9. #include <string.h>
  10. #include <irq_user.h>
  11. #include <os.h>
  12. #include <um_malloc.h>
  13. /*
  14. * Locked by irq_lock in arch/um/kernel/irq.c. Changed by os_create_pollfd
  15. * and os_free_irq_by_cb, which are called under irq_lock.
  16. */
  17. static struct pollfd *pollfds = NULL;
  18. static int pollfds_num = 0;
  19. static int pollfds_size = 0;
  20. int os_waiting_for_events(struct irq_fd *active_fds)
  21. {
  22. struct irq_fd *irq_fd;
  23. int i, n, err;
  24. n = poll(pollfds, pollfds_num, 0);
  25. if (n < 0) {
  26. err = -errno;
  27. if (errno != EINTR)
  28. printk(UM_KERN_ERR "os_waiting_for_events:"
  29. " poll returned %d, errno = %d\n", n, errno);
  30. return err;
  31. }
  32. if (n == 0)
  33. return 0;
  34. irq_fd = active_fds;
  35. for (i = 0; i < pollfds_num; i++) {
  36. if (pollfds[i].revents != 0) {
  37. irq_fd->current_events = pollfds[i].revents;
  38. pollfds[i].fd = -1;
  39. }
  40. irq_fd = irq_fd->next;
  41. }
  42. return n;
  43. }
  44. int os_create_pollfd(int fd, int events, void *tmp_pfd, int size_tmpfds)
  45. {
  46. if (pollfds_num == pollfds_size) {
  47. if (size_tmpfds <= pollfds_size * sizeof(pollfds[0])) {
  48. /* return min size needed for new pollfds area */
  49. return (pollfds_size + 1) * sizeof(pollfds[0]);
  50. }
  51. if (pollfds != NULL) {
  52. memcpy(tmp_pfd, pollfds,
  53. sizeof(pollfds[0]) * pollfds_size);
  54. /* remove old pollfds */
  55. kfree(pollfds);
  56. }
  57. pollfds = tmp_pfd;
  58. pollfds_size++;
  59. } else
  60. kfree(tmp_pfd); /* remove not used tmp_pfd */
  61. pollfds[pollfds_num] = ((struct pollfd) { .fd = fd,
  62. .events = events,
  63. .revents = 0 });
  64. pollfds_num++;
  65. return 0;
  66. }
  67. void os_free_irq_by_cb(int (*test)(struct irq_fd *, void *), void *arg,
  68. struct irq_fd *active_fds, struct irq_fd ***last_irq_ptr2)
  69. {
  70. struct irq_fd **prev;
  71. int i = 0;
  72. prev = &active_fds;
  73. while (*prev != NULL) {
  74. if ((*test)(*prev, arg)) {
  75. struct irq_fd *old_fd = *prev;
  76. if ((pollfds[i].fd != -1) &&
  77. (pollfds[i].fd != (*prev)->fd)) {
  78. printk(UM_KERN_ERR "os_free_irq_by_cb - "
  79. "mismatch between active_fds and "
  80. "pollfds, fd %d vs %d\n",
  81. (*prev)->fd, pollfds[i].fd);
  82. goto out;
  83. }
  84. pollfds_num--;
  85. /*
  86. * This moves the *whole* array after pollfds[i]
  87. * (though it doesn't spot as such)!
  88. */
  89. memmove(&pollfds[i], &pollfds[i + 1],
  90. (pollfds_num - i) * sizeof(pollfds[0]));
  91. if (*last_irq_ptr2 == &old_fd->next)
  92. *last_irq_ptr2 = prev;
  93. *prev = (*prev)->next;
  94. if (old_fd->type == IRQ_WRITE)
  95. ignore_sigio_fd(old_fd->fd);
  96. kfree(old_fd);
  97. continue;
  98. }
  99. prev = &(*prev)->next;
  100. i++;
  101. }
  102. out:
  103. return;
  104. }
  105. int os_get_pollfd(int i)
  106. {
  107. return pollfds[i].fd;
  108. }
  109. void os_set_pollfd(int i, int fd)
  110. {
  111. pollfds[i].fd = fd;
  112. }
  113. void os_set_ioignore(void)
  114. {
  115. signal(SIGIO, SIG_IGN);
  116. }