irq_parse.c 1.6 KB

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