irq_parse.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. irq_t *new;
  36. if (!(new = irq_new(num)))
  37. return NULL;
  38. lub_list_add(irqs, new);
  39. return new;
  40. }
  41. static int irq_list_populate_pci(lub_list_t *irqs)
  42. {
  43. DIR *dir;
  44. DIR *msi;
  45. struct dirent *dent;
  46. struct dirent *ment;
  47. FILE *fd;
  48. char path[PATH_MAX];
  49. int num;
  50. /* Now we can parse PCI devices only */
  51. /* Get info from /sys/bus/pci/devices */
  52. dir = opendir(SYSFS_PCI_PATH);
  53. if (!dir)
  54. return -1;
  55. while((dent = readdir(dir))) {
  56. if (!strcmp(dent->d_name, ".") ||
  57. !strcmp(dent->d_name, ".."))
  58. continue;
  59. /* Search for MSI IRQs. Since linux-3.2 */
  60. sprintf(path, "%s/%s/msi_irqs", SYSFS_PCI_PATH, dent->d_name);
  61. if ((msi = opendir(path))) {
  62. while((ment = readdir(msi))) {
  63. if (!strcmp(ment->d_name, ".") ||
  64. !strcmp(ment->d_name, ".."))
  65. continue;
  66. num = strtol(ment->d_name, NULL, 10);
  67. if (!num)
  68. continue;
  69. irq_list_add(irqs, num);
  70. printf("MSI: %d\n", num);
  71. }
  72. closedir(msi);
  73. continue;
  74. }
  75. /* Try to get IRQ number from irq file */
  76. sprintf(path, "%s/%s/irq", SYSFS_PCI_PATH, dent->d_name);
  77. if (!(fd = fopen(path, "r")))
  78. continue;
  79. if (fscanf(fd, "%d", &num) < 0) {
  80. fclose(fd);
  81. continue;
  82. }
  83. fclose(fd);
  84. if (!num)
  85. continue;
  86. irq_list_add(irqs, num);
  87. printf("IRQ: %d\n", num);
  88. }
  89. closedir(dir);
  90. return 0;
  91. }
  92. int irq_list_populate(lub_list_t *irqs)
  93. {
  94. irq_list_populate_pci(irqs);
  95. // irq_list_populate_proc(irqs);
  96. return 0;
  97. }