irq_parse.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. static int irqs_populate_pci(lub_list_t *irqs)
  13. {
  14. DIR *dir;
  15. DIR *msi;
  16. struct dirent *dent;
  17. struct dirent *ment;
  18. FILE *fd;
  19. char path[PATH_MAX];
  20. int num;
  21. /* Now we can parse PCI devices only */
  22. /* Get info from /sys/bus/pci/devices */
  23. dir = opendir(SYSFS_PCI_PATH);
  24. if (!dir)
  25. return -1;
  26. while((dent = readdir(dir))) {
  27. if (!strcmp(dent->d_name, ".") ||
  28. !strcmp(dent->d_name, ".."))
  29. continue;
  30. /* Search for MSI IRQs. Since linux-3.2 */
  31. sprintf(path, "%s/%s/msi_irqs", SYSFS_PCI_PATH, dent->d_name);
  32. if ((msi = opendir(path))) {
  33. while((ment = readdir(msi))) {
  34. if (!strcmp(ment->d_name, ".") ||
  35. !strcmp(ment->d_name, ".."))
  36. continue;
  37. num = strtol(ment->d_name, NULL, 10);
  38. if (!num)
  39. continue;
  40. printf("MSI: %d\n", num);
  41. }
  42. closedir(msi);
  43. continue;
  44. }
  45. /* Try to get IRQ number from irq file */
  46. sprintf(path, "%s/%s/irq", SYSFS_PCI_PATH, dent->d_name);
  47. if (!(fd = fopen(path, "r")))
  48. continue;
  49. if (fscanf(fd, "%d", &num) < 0) {
  50. fclose(fd);
  51. continue;
  52. }
  53. fclose(fd);
  54. if (!num)
  55. continue;
  56. printf("IRQ: %d\n", num);
  57. }
  58. closedir(dir);
  59. return 0;
  60. }
  61. int irqs_populate(lub_list_t *irqs)
  62. {
  63. irqs_populate_pci(irqs);
  64. // irqs_populate_proc(irqs);
  65. return 0;
  66. }