devres.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. #include "internals.h"
  7. /*
  8. * Device resource management aware IRQ request/free implementation.
  9. */
  10. struct irq_devres {
  11. unsigned int irq;
  12. void *dev_id;
  13. };
  14. static void devm_irq_release(struct device *dev, void *res)
  15. {
  16. struct irq_devres *this = res;
  17. free_irq(this->irq, this->dev_id);
  18. }
  19. static int devm_irq_match(struct device *dev, void *res, void *data)
  20. {
  21. struct irq_devres *this = res, *match = data;
  22. return this->irq == match->irq && this->dev_id == match->dev_id;
  23. }
  24. /**
  25. * devm_request_threaded_irq - allocate an interrupt line for a managed device
  26. * @dev: device to request interrupt for
  27. * @irq: Interrupt line to allocate
  28. * @handler: Function to be called when the IRQ occurs
  29. * @thread_fn: function to be called in a threaded interrupt context. NULL
  30. * for devices which handle everything in @handler
  31. * @irqflags: Interrupt type flags
  32. * @devname: An ascii name for the claiming device, dev_name(dev) if NULL
  33. * @dev_id: A cookie passed back to the handler function
  34. *
  35. * Except for the extra @dev argument, this function takes the
  36. * same arguments and performs the same function as
  37. * request_threaded_irq(). IRQs requested with this function will be
  38. * automatically freed on driver detach.
  39. *
  40. * If an IRQ allocated with this function needs to be freed
  41. * separately, devm_free_irq() must be used.
  42. */
  43. int devm_request_threaded_irq(struct device *dev, unsigned int irq,
  44. irq_handler_t handler, irq_handler_t thread_fn,
  45. unsigned long irqflags, const char *devname,
  46. void *dev_id)
  47. {
  48. struct irq_devres *dr;
  49. int rc;
  50. dr = devres_alloc(devm_irq_release, sizeof(struct irq_devres),
  51. GFP_KERNEL);
  52. if (!dr)
  53. return -ENOMEM;
  54. if (!devname)
  55. devname = dev_name(dev);
  56. rc = request_threaded_irq(irq, handler, thread_fn, irqflags, devname,
  57. dev_id);
  58. if (rc) {
  59. devres_free(dr);
  60. return rc;
  61. }
  62. dr->irq = irq;
  63. dr->dev_id = dev_id;
  64. devres_add(dev, dr);
  65. return 0;
  66. }
  67. EXPORT_SYMBOL(devm_request_threaded_irq);
  68. /**
  69. * devm_request_any_context_irq - allocate an interrupt line for a managed device
  70. * @dev: device to request interrupt for
  71. * @irq: Interrupt line to allocate
  72. * @handler: Function to be called when the IRQ occurs
  73. * @thread_fn: function to be called in a threaded interrupt context. NULL
  74. * for devices which handle everything in @handler
  75. * @irqflags: Interrupt type flags
  76. * @devname: An ascii name for the claiming device, dev_name(dev) if NULL
  77. * @dev_id: A cookie passed back to the handler function
  78. *
  79. * Except for the extra @dev argument, this function takes the
  80. * same arguments and performs the same function as
  81. * request_any_context_irq(). IRQs requested with this function will be
  82. * automatically freed on driver detach.
  83. *
  84. * If an IRQ allocated with this function needs to be freed
  85. * separately, devm_free_irq() must be used.
  86. */
  87. int devm_request_any_context_irq(struct device *dev, unsigned int irq,
  88. irq_handler_t handler, unsigned long irqflags,
  89. const char *devname, void *dev_id)
  90. {
  91. struct irq_devres *dr;
  92. int rc;
  93. dr = devres_alloc(devm_irq_release, sizeof(struct irq_devres),
  94. GFP_KERNEL);
  95. if (!dr)
  96. return -ENOMEM;
  97. if (!devname)
  98. devname = dev_name(dev);
  99. rc = request_any_context_irq(irq, handler, irqflags, devname, dev_id);
  100. if (rc < 0) {
  101. devres_free(dr);
  102. return rc;
  103. }
  104. dr->irq = irq;
  105. dr->dev_id = dev_id;
  106. devres_add(dev, dr);
  107. return rc;
  108. }
  109. EXPORT_SYMBOL(devm_request_any_context_irq);
  110. /**
  111. * devm_free_irq - free an interrupt
  112. * @dev: device to free interrupt for
  113. * @irq: Interrupt line to free
  114. * @dev_id: Device identity to free
  115. *
  116. * Except for the extra @dev argument, this function takes the
  117. * same arguments and performs the same function as free_irq().
  118. * This function instead of free_irq() should be used to manually
  119. * free IRQs allocated with devm_request_irq().
  120. */
  121. void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id)
  122. {
  123. struct irq_devres match_data = { irq, dev_id };
  124. WARN_ON(devres_destroy(dev, devm_irq_release, devm_irq_match,
  125. &match_data));
  126. free_irq(irq, dev_id);
  127. }
  128. EXPORT_SYMBOL(devm_free_irq);
  129. struct irq_desc_devres {
  130. unsigned int from;
  131. unsigned int cnt;
  132. };
  133. static void devm_irq_desc_release(struct device *dev, void *res)
  134. {
  135. struct irq_desc_devres *this = res;
  136. irq_free_descs(this->from, this->cnt);
  137. }
  138. /**
  139. * __devm_irq_alloc_descs - Allocate and initialize a range of irq descriptors
  140. * for a managed device
  141. * @dev: Device to allocate the descriptors for
  142. * @irq: Allocate for specific irq number if irq >= 0
  143. * @from: Start the search from this irq number
  144. * @cnt: Number of consecutive irqs to allocate
  145. * @node: Preferred node on which the irq descriptor should be allocated
  146. * @owner: Owning module (can be NULL)
  147. * @affinity: Optional pointer to an affinity mask array of size @cnt
  148. * which hints where the irq descriptors should be allocated
  149. * and which default affinities to use
  150. *
  151. * Returns the first irq number or error code.
  152. *
  153. * Note: Use the provided wrappers (devm_irq_alloc_desc*) for simplicity.
  154. */
  155. int __devm_irq_alloc_descs(struct device *dev, int irq, unsigned int from,
  156. unsigned int cnt, int node, struct module *owner,
  157. const struct cpumask *affinity)
  158. {
  159. struct irq_desc_devres *dr;
  160. int base;
  161. dr = devres_alloc(devm_irq_desc_release, sizeof(*dr), GFP_KERNEL);
  162. if (!dr)
  163. return -ENOMEM;
  164. base = __irq_alloc_descs(irq, from, cnt, node, owner, affinity);
  165. if (base < 0) {
  166. devres_free(dr);
  167. return base;
  168. }
  169. dr->from = base;
  170. dr->cnt = cnt;
  171. devres_add(dev, dr);
  172. return base;
  173. }
  174. EXPORT_SYMBOL_GPL(__devm_irq_alloc_descs);
  175. #ifdef CONFIG_GENERIC_IRQ_CHIP
  176. /**
  177. * devm_irq_alloc_generic_chip - Allocate and initialize a generic chip
  178. * for a managed device
  179. * @dev: Device to allocate the generic chip for
  180. * @name: Name of the irq chip
  181. * @num_ct: Number of irq_chip_type instances associated with this
  182. * @irq_base: Interrupt base nr for this chip
  183. * @reg_base: Register base address (virtual)
  184. * @handler: Default flow handler associated with this chip
  185. *
  186. * Returns an initialized irq_chip_generic structure. The chip defaults
  187. * to the primary (index 0) irq_chip_type and @handler
  188. */
  189. struct irq_chip_generic *
  190. devm_irq_alloc_generic_chip(struct device *dev, const char *name, int num_ct,
  191. unsigned int irq_base, void __iomem *reg_base,
  192. irq_flow_handler_t handler)
  193. {
  194. struct irq_chip_generic *gc;
  195. unsigned long sz = sizeof(*gc) + num_ct * sizeof(struct irq_chip_type);
  196. gc = devm_kzalloc(dev, sz, GFP_KERNEL);
  197. if (gc)
  198. irq_init_generic_chip(gc, name, num_ct,
  199. irq_base, reg_base, handler);
  200. return gc;
  201. }
  202. EXPORT_SYMBOL_GPL(devm_irq_alloc_generic_chip);
  203. struct irq_generic_chip_devres {
  204. struct irq_chip_generic *gc;
  205. u32 msk;
  206. unsigned int clr;
  207. unsigned int set;
  208. };
  209. static void devm_irq_remove_generic_chip(struct device *dev, void *res)
  210. {
  211. struct irq_generic_chip_devres *this = res;
  212. irq_remove_generic_chip(this->gc, this->msk, this->clr, this->set);
  213. }
  214. /**
  215. * devm_irq_setup_generic_chip - Setup a range of interrupts with a generic
  216. * chip for a managed device
  217. *
  218. * @dev: Device to setup the generic chip for
  219. * @gc: Generic irq chip holding all data
  220. * @msk: Bitmask holding the irqs to initialize relative to gc->irq_base
  221. * @flags: Flags for initialization
  222. * @clr: IRQ_* bits to clear
  223. * @set: IRQ_* bits to set
  224. *
  225. * Set up max. 32 interrupts starting from gc->irq_base. Note, this
  226. * initializes all interrupts to the primary irq_chip_type and its
  227. * associated handler.
  228. */
  229. int devm_irq_setup_generic_chip(struct device *dev, struct irq_chip_generic *gc,
  230. u32 msk, enum irq_gc_flags flags,
  231. unsigned int clr, unsigned int set)
  232. {
  233. struct irq_generic_chip_devres *dr;
  234. dr = devres_alloc(devm_irq_remove_generic_chip,
  235. sizeof(*dr), GFP_KERNEL);
  236. if (!dr)
  237. return -ENOMEM;
  238. irq_setup_generic_chip(gc, msk, flags, clr, set);
  239. dr->gc = gc;
  240. dr->msk = msk;
  241. dr->clr = clr;
  242. dr->set = set;
  243. devres_add(dev, dr);
  244. return 0;
  245. }
  246. EXPORT_SYMBOL_GPL(devm_irq_setup_generic_chip);
  247. #endif /* CONFIG_GENERIC_IRQ_CHIP */