devres.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include <linux/module.h>
  2. #include <linux/interrupt.h>
  3. #include <linux/device.h>
  4. #include <linux/gfp.h>
  5. #include <linux/irq.h>
  6. /*
  7. * Device resource management aware IRQ request/free implementation.
  8. */
  9. struct irq_devres {
  10. unsigned int irq;
  11. void *dev_id;
  12. };
  13. static void devm_irq_release(struct device *dev, void *res)
  14. {
  15. struct irq_devres *this = res;
  16. free_irq(this->irq, this->dev_id);
  17. }
  18. static int devm_irq_match(struct device *dev, void *res, void *data)
  19. {
  20. struct irq_devres *this = res, *match = data;
  21. return this->irq == match->irq && this->dev_id == match->dev_id;
  22. }
  23. /**
  24. * devm_request_threaded_irq - allocate an interrupt line for a managed device
  25. * @dev: device to request interrupt for
  26. * @irq: Interrupt line to allocate
  27. * @handler: Function to be called when the IRQ occurs
  28. * @thread_fn: function to be called in a threaded interrupt context. NULL
  29. * for devices which handle everything in @handler
  30. * @irqflags: Interrupt type flags
  31. * @devname: An ascii name for the claiming device, dev_name(dev) if NULL
  32. * @dev_id: A cookie passed back to the handler function
  33. *
  34. * Except for the extra @dev argument, this function takes the
  35. * same arguments and performs the same function as
  36. * request_threaded_irq(). IRQs requested with this function will be
  37. * automatically freed on driver detach.
  38. *
  39. * If an IRQ allocated with this function needs to be freed
  40. * separately, devm_free_irq() must be used.
  41. */
  42. int devm_request_threaded_irq(struct device *dev, unsigned int irq,
  43. irq_handler_t handler, irq_handler_t thread_fn,
  44. unsigned long irqflags, const char *devname,
  45. void *dev_id)
  46. {
  47. struct irq_devres *dr;
  48. int rc;
  49. dr = devres_alloc(devm_irq_release, sizeof(struct irq_devres),
  50. GFP_KERNEL);
  51. if (!dr)
  52. return -ENOMEM;
  53. if (!devname)
  54. devname = dev_name(dev);
  55. rc = request_threaded_irq(irq, handler, thread_fn, irqflags, devname,
  56. dev_id);
  57. if (rc) {
  58. devres_free(dr);
  59. return rc;
  60. }
  61. dr->irq = irq;
  62. dr->dev_id = dev_id;
  63. devres_add(dev, dr);
  64. return 0;
  65. }
  66. EXPORT_SYMBOL(devm_request_threaded_irq);
  67. /**
  68. * devm_request_any_context_irq - allocate an interrupt line for a managed device
  69. * @dev: device to request interrupt for
  70. * @irq: Interrupt line to allocate
  71. * @handler: Function to be called when the IRQ occurs
  72. * @thread_fn: function to be called in a threaded interrupt context. NULL
  73. * for devices which handle everything in @handler
  74. * @irqflags: Interrupt type flags
  75. * @devname: An ascii name for the claiming device, dev_name(dev) if NULL
  76. * @dev_id: A cookie passed back to the handler function
  77. *
  78. * Except for the extra @dev argument, this function takes the
  79. * same arguments and performs the same function as
  80. * request_any_context_irq(). IRQs requested with this function will be
  81. * automatically freed on driver detach.
  82. *
  83. * If an IRQ allocated with this function needs to be freed
  84. * separately, devm_free_irq() must be used.
  85. */
  86. int devm_request_any_context_irq(struct device *dev, unsigned int irq,
  87. irq_handler_t handler, unsigned long irqflags,
  88. const char *devname, void *dev_id)
  89. {
  90. struct irq_devres *dr;
  91. int rc;
  92. dr = devres_alloc(devm_irq_release, sizeof(struct irq_devres),
  93. GFP_KERNEL);
  94. if (!dr)
  95. return -ENOMEM;
  96. if (!devname)
  97. devname = dev_name(dev);
  98. rc = request_any_context_irq(irq, handler, irqflags, devname, dev_id);
  99. if (rc < 0) {
  100. devres_free(dr);
  101. return rc;
  102. }
  103. dr->irq = irq;
  104. dr->dev_id = dev_id;
  105. devres_add(dev, dr);
  106. return rc;
  107. }
  108. EXPORT_SYMBOL(devm_request_any_context_irq);
  109. /**
  110. * devm_free_irq - free an interrupt
  111. * @dev: device to free interrupt for
  112. * @irq: Interrupt line to free
  113. * @dev_id: Device identity to free
  114. *
  115. * Except for the extra @dev argument, this function takes the
  116. * same arguments and performs the same function as free_irq().
  117. * This function instead of free_irq() should be used to manually
  118. * free IRQs allocated with devm_request_irq().
  119. */
  120. void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id)
  121. {
  122. struct irq_devres match_data = { irq, dev_id };
  123. WARN_ON(devres_destroy(dev, devm_irq_release, devm_irq_match,
  124. &match_data));
  125. free_irq(irq, dev_id);
  126. }
  127. EXPORT_SYMBOL(devm_free_irq);
  128. struct irq_desc_devres {
  129. unsigned int from;
  130. unsigned int cnt;
  131. };
  132. static void devm_irq_desc_release(struct device *dev, void *res)
  133. {
  134. struct irq_desc_devres *this = res;
  135. irq_free_descs(this->from, this->cnt);
  136. }
  137. /**
  138. * __devm_irq_alloc_descs - Allocate and initialize a range of irq descriptors
  139. * for a managed device
  140. * @dev: Device to allocate the descriptors for
  141. * @irq: Allocate for specific irq number if irq >= 0
  142. * @from: Start the search from this irq number
  143. * @cnt: Number of consecutive irqs to allocate
  144. * @node: Preferred node on which the irq descriptor should be allocated
  145. * @owner: Owning module (can be NULL)
  146. * @affinity: Optional pointer to an affinity mask array of size @cnt
  147. * which hints where the irq descriptors should be allocated
  148. * and which default affinities to use
  149. *
  150. * Returns the first irq number or error code.
  151. *
  152. * Note: Use the provided wrappers (devm_irq_alloc_desc*) for simplicity.
  153. */
  154. int __devm_irq_alloc_descs(struct device *dev, int irq, unsigned int from,
  155. unsigned int cnt, int node, struct module *owner,
  156. const struct cpumask *affinity)
  157. {
  158. struct irq_desc_devres *dr;
  159. int base;
  160. dr = devres_alloc(devm_irq_desc_release, sizeof(*dr), GFP_KERNEL);
  161. if (!dr)
  162. return -ENOMEM;
  163. base = __irq_alloc_descs(irq, from, cnt, node, owner, affinity);
  164. if (base < 0) {
  165. devres_free(dr);
  166. return base;
  167. }
  168. dr->from = base;
  169. dr->cnt = cnt;
  170. devres_add(dev, dr);
  171. return base;
  172. }
  173. EXPORT_SYMBOL_GPL(__devm_irq_alloc_descs);