irq_parse.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* irq_parse.c
  2. * Parse IRQ-related files.
  3. */
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <dirent.h>
  9. #include <limits.h>
  10. #include "lub/list.h"
  11. #include "irq.h"
  12. int irq_list_compare(const void *first, const void *second)
  13. {
  14. const irq_t *f = (const irq_t *)first;
  15. const irq_t *s = (const irq_t *)second;
  16. return (f->irq - s->irq);
  17. }
  18. static irq_t * irq_new(int num)
  19. {
  20. irq_t *new;
  21. if (!(new = malloc(sizeof(*new))))
  22. return NULL;
  23. new->irq = num;
  24. new->desc = NULL;
  25. return new;
  26. }
  27. static void irq_free(irq_t *irq)
  28. {
  29. if (irq->desc)
  30. free(irq->desc);
  31. free(irq);
  32. }
  33. static irq_t * irq_list_add(lub_list_t *irqs, int num)
  34. {
  35. lub_list_node_t *node;
  36. irq_t *new;
  37. irq_t search;
  38. search.irq = num;
  39. node = lub_list_search(irqs, &search);
  40. if (node) /* IRQ already exists. May be renew some fields later */
  41. return (irq_t *)lub_list_node__get_data(node);
  42. if (!(new = irq_new(num)))
  43. return NULL;
  44. lub_list_add(irqs, new);
  45. return new;
  46. }
  47. static int irq_list_show(lub_list_t *irqs)
  48. {
  49. lub_list_node_t *iter;
  50. for (iter = lub_list_iterator_init(irqs); iter;
  51. iter = lub_list_iterator_next(iter)) {
  52. irq_t *irq;
  53. irq = (irq_t *)lub_list_node__get_data(iter);
  54. printf("IRQ %3d %s\n", irq->irq, irq->desc ? irq->desc : "");
  55. }
  56. return 0;
  57. }
  58. static int irq_list_populate_pci(lub_list_t *irqs)
  59. {
  60. DIR *dir;
  61. DIR *msi;
  62. struct dirent *dent;
  63. struct dirent *ment;
  64. FILE *fd;
  65. char path[PATH_MAX];
  66. int num;
  67. /* Now we can parse PCI devices only */
  68. /* Get info from /sys/bus/pci/devices */
  69. dir = opendir(SYSFS_PCI_PATH);
  70. if (!dir)
  71. return -1;
  72. while((dent = readdir(dir))) {
  73. if (!strcmp(dent->d_name, ".") ||
  74. !strcmp(dent->d_name, ".."))
  75. continue;
  76. /* Get enable flag */
  77. sprintf(path, "%s/%s/enable", SYSFS_PCI_PATH, dent->d_name);
  78. if ((fd = fopen(path, "r"))) {
  79. if (fscanf(fd, "%d", &num) > 0) {
  80. if (0 == num) {
  81. fclose(fd);
  82. continue;
  83. }
  84. }
  85. fclose(fd);
  86. }
  87. /* Search for MSI IRQs. Since linux-3.2 */
  88. sprintf(path, "%s/%s/msi_irqs", SYSFS_PCI_PATH, dent->d_name);
  89. if ((msi = opendir(path))) {
  90. while((ment = readdir(msi))) {
  91. if (!strcmp(ment->d_name, ".") ||
  92. !strcmp(ment->d_name, ".."))
  93. continue;
  94. num = strtol(ment->d_name, NULL, 10);
  95. if (!num)
  96. continue;
  97. irq_list_add(irqs, num);
  98. printf("MSI: %3d %s\n", num, dent->d_name);
  99. }
  100. closedir(msi);
  101. continue;
  102. }
  103. /* Try to get IRQ number from irq file */
  104. sprintf(path, "%s/%s/irq", SYSFS_PCI_PATH, dent->d_name);
  105. if (!(fd = fopen(path, "r")))
  106. continue;
  107. if (fscanf(fd, "%d", &num) < 0) {
  108. fclose(fd);
  109. continue;
  110. }
  111. fclose(fd);
  112. if (!num)
  113. continue;
  114. irq_list_add(irqs, num);
  115. printf("IRQ: %3d %s\n", num, dent->d_name);
  116. }
  117. closedir(dir);
  118. irq_list_show(irqs);
  119. return 0;
  120. }
  121. int irq_list_populate(lub_list_t *irqs)
  122. {
  123. irq_list_populate_pci(irqs);
  124. // irq_list_populate_proc(irqs);
  125. return 0;
  126. }