irq_parse.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 <ctype.h>
  11. #include "lub/list.h"
  12. #include "irq.h"
  13. #define STR(str) ( str ? str : "" )
  14. int irq_list_compare(const void *first, const void *second)
  15. {
  16. const irq_t *f = (const irq_t *)first;
  17. const irq_t *s = (const irq_t *)second;
  18. return (f->irq - s->irq);
  19. }
  20. static irq_t * irq_new(int num)
  21. {
  22. irq_t *new;
  23. if (!(new = malloc(sizeof(*new))))
  24. return NULL;
  25. new->irq = num;
  26. new->type = NULL;
  27. new->desc = NULL;
  28. new->refresh = 1;
  29. return new;
  30. }
  31. static void irq_free(irq_t *irq)
  32. {
  33. free(irq->type);
  34. free(irq->desc);
  35. free(irq);
  36. }
  37. static irq_t * irq_list_search(lub_list_t *irqs, unsigned int num)
  38. {
  39. lub_list_node_t *node;
  40. irq_t search;
  41. search.irq = num;
  42. node = lub_list_search(irqs, &search);
  43. if (!node)
  44. return NULL;
  45. return (irq_t *)lub_list_node__get_data(node);
  46. }
  47. static irq_t * irq_list_add(lub_list_t *irqs, unsigned int num)
  48. {
  49. lub_list_node_t *node;
  50. irq_t *new;
  51. irq_t search;
  52. search.irq = num;
  53. node = lub_list_search(irqs, &search);
  54. if (node) /* IRQ already exists. May be renew some fields later */
  55. return (irq_t *)lub_list_node__get_data(node);
  56. if (!(new = irq_new(num)))
  57. return NULL;
  58. lub_list_add(irqs, new);
  59. return new;
  60. }
  61. int irq_list_free(lub_list_t *irqs)
  62. {
  63. lub_list_node_t *iter;
  64. while ((iter = lub_list__get_head(irqs))) {
  65. irq_t *irq;
  66. irq = (irq_t *)lub_list_node__get_data(iter);
  67. irq_free(irq);
  68. lub_list_del(irqs, iter);
  69. lub_list_node_free(iter);
  70. }
  71. lub_list_free(irqs);
  72. return 0;
  73. }
  74. /* Show IRQ information */
  75. static void irq_show(irq_t *irq)
  76. {
  77. printf("IRQ %3d [%s] %s\n", irq->irq, STR(irq->type), STR(irq->desc));
  78. }
  79. /* Show IRQ list */
  80. static int irq_list_show(lub_list_t *irqs)
  81. {
  82. lub_list_node_t *iter;
  83. for (iter = lub_list_iterator_init(irqs); iter;
  84. iter = lub_list_iterator_next(iter)) {
  85. irq_t *irq;
  86. irq = (irq_t *)lub_list_node__get_data(iter);
  87. irq_show(irq);
  88. }
  89. return 0;
  90. }
  91. static int parse_local_cpus(lub_list_t *irqs, const char *sysfs_path,
  92. unsigned int num)
  93. {
  94. char path[PATH_MAX];
  95. FILE *fd;
  96. char *str = NULL;
  97. size_t sz;
  98. irq_t *irq;
  99. irq = irq_list_search(irqs, num);
  100. if (!irq)
  101. return -1;
  102. sprintf(path, "%s/%s/local_cpus", SYSFS_PCI_PATH, sysfs_path);
  103. if (!(fd = fopen(path, "r")))
  104. return -1;
  105. if (getline(&str, &sz, fd) < 0) {
  106. fclose(fd);
  107. return -1;
  108. }
  109. fclose(fd);
  110. /* printf("%d %s %s\n", num, str, sysfs_path);
  111. */ free(str);
  112. return 0;
  113. }
  114. static int scan_sysfs(lub_list_t *irqs)
  115. {
  116. DIR *dir;
  117. DIR *msi;
  118. struct dirent *dent;
  119. struct dirent *ment;
  120. FILE *fd;
  121. char path[PATH_MAX];
  122. int num;
  123. /* Now we can parse PCI devices only */
  124. /* Get info from /sys/bus/pci/devices */
  125. dir = opendir(SYSFS_PCI_PATH);
  126. if (!dir)
  127. return -1;
  128. while((dent = readdir(dir))) {
  129. if (!strcmp(dent->d_name, ".") ||
  130. !strcmp(dent->d_name, ".."))
  131. continue;
  132. /* Search for MSI IRQs. Since linux-3.2 */
  133. sprintf(path, "%s/%s/msi_irqs", SYSFS_PCI_PATH, dent->d_name);
  134. if ((msi = opendir(path))) {
  135. while((ment = readdir(msi))) {
  136. if (!strcmp(ment->d_name, ".") ||
  137. !strcmp(ment->d_name, ".."))
  138. continue;
  139. num = strtol(ment->d_name, NULL, 10);
  140. if (!num)
  141. continue;
  142. parse_local_cpus(irqs, dent->d_name, num);
  143. }
  144. closedir(msi);
  145. continue;
  146. }
  147. /* Try to get IRQ number from irq file */
  148. sprintf(path, "%s/%s/irq", SYSFS_PCI_PATH, dent->d_name);
  149. if (!(fd = fopen(path, "r")))
  150. continue;
  151. if (fscanf(fd, "%d", &num) < 0) {
  152. fclose(fd);
  153. continue;
  154. }
  155. fclose(fd);
  156. if (!num)
  157. continue;
  158. parse_local_cpus(irqs, dent->d_name, num);
  159. }
  160. closedir(dir);
  161. return 0;
  162. }
  163. /* Parse /proc/interrupts to get actual IRQ list */
  164. int irq_list_populate(lub_list_t *irqs)
  165. {
  166. FILE *fd;
  167. unsigned int num;
  168. char *str = NULL;
  169. size_t sz;
  170. irq_t *irq;
  171. lub_list_node_t *iter;
  172. if (!(fd = fopen(PROC_INTERRUPTS, "r")))
  173. return -1;
  174. while(getline(&str, &sz, fd) >= 0) {
  175. char *endptr, *tok;
  176. int new = 0;
  177. num = strtoul(str, &endptr, 10);
  178. if (endptr == str)
  179. continue;
  180. if (!(irq = irq_list_search(irqs, num))) {
  181. new = 1;
  182. irq = irq_list_add(irqs, num);
  183. }
  184. /* Find IRQ type - first non-digital and non-space */
  185. while (*endptr && !isalpha(*endptr))
  186. endptr++;
  187. tok = endptr; /* It will be IRQ type */
  188. while (*endptr && !isblank(*endptr))
  189. endptr++;
  190. free(irq->type);
  191. irq->type = strndup(tok, endptr - tok);
  192. /* Find IRQ devices list */
  193. while (*endptr && !isalpha(*endptr))
  194. endptr++;
  195. tok = endptr; /* It will be device list */
  196. while (*endptr && !iscntrl(*endptr))
  197. endptr++;
  198. free(irq->desc);
  199. irq->desc = strndup(tok, endptr - tok);
  200. /* Set refresh flag because IRQ was found */
  201. irq->refresh = 1;
  202. if (new) {
  203. printf("Add new ");
  204. irq_show(irq);
  205. }
  206. }
  207. free(str);
  208. fclose(fd);
  209. /* Remove disapeared IRQs */
  210. iter = lub_list_iterator_init(irqs);
  211. while(iter) {
  212. irq_t *irq;
  213. lub_list_node_t *old_iter;
  214. irq = (irq_t *)lub_list_node__get_data(iter);
  215. old_iter = iter;
  216. iter = lub_list_iterator_next(iter);
  217. if (!irq->refresh) {
  218. lub_list_del(irqs, old_iter);
  219. irq_free(irq);
  220. printf("Remove ");
  221. irq_show(irq);
  222. } else {
  223. /* Drop refresh flag for next iteration */
  224. irq->refresh = 0;
  225. }
  226. }
  227. /* Add IRQ info from sysfs */
  228. scan_sysfs(irqs);
  229. return 0;
  230. }