devres.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <linux/module.h>
  2. #include <linux/interrupt.h>
  3. #include <linux/device.h>
  4. #include <linux/gfp.h>
  5. /*
  6. * Device resource management aware IRQ request/free implementation.
  7. */
  8. struct irq_devres {
  9. unsigned int irq;
  10. void *dev_id;
  11. };
  12. static void devm_irq_release(struct device *dev, void *res)
  13. {
  14. struct irq_devres *this = res;
  15. free_irq(this->irq, this->dev_id);
  16. }
  17. static int devm_irq_match(struct device *dev, void *res, void *data)
  18. {
  19. struct irq_devres *this = res, *match = data;
  20. return this->irq == match->irq && this->dev_id == match->dev_id;
  21. }
  22. /**
  23. * devm_request_threaded_irq - allocate an interrupt line for a managed device
  24. * @dev: device to request interrupt for
  25. * @irq: Interrupt line to allocate
  26. * @handler: Function to be called when the IRQ occurs
  27. * @thread_fn: function to be called in a threaded interrupt context. NULL
  28. * for devices which handle everything in @handler
  29. * @irqflags: Interrupt type flags
  30. * @devname: An ascii name for the claiming device
  31. * @dev_id: A cookie passed back to the handler function
  32. *
  33. * Except for the extra @dev argument, this function takes the
  34. * same arguments and performs the same function as
  35. * request_threaded_irq(). IRQs requested with this function will be
  36. * automatically freed on driver detach.
  37. *
  38. * If an IRQ allocated with this function needs to be freed
  39. * separately, devm_free_irq() must be used.
  40. */
  41. int devm_request_threaded_irq(struct device *dev, unsigned int irq,
  42. irq_handler_t handler, irq_handler_t thread_fn,
  43. unsigned long irqflags, const char *devname,
  44. void *dev_id)
  45. {
  46. struct irq_devres *dr;
  47. int rc;
  48. dr = devres_alloc(devm_irq_release, sizeof(struct irq_devres),
  49. GFP_KERNEL);
  50. if (!dr)
  51. return -ENOMEM;
  52. rc = request_threaded_irq(irq, handler, thread_fn, irqflags, devname,
  53. dev_id);
  54. if (rc) {
  55. devres_free(dr);
  56. return rc;
  57. }
  58. dr->irq = irq;
  59. dr->dev_id = dev_id;
  60. devres_add(dev, dr);
  61. return 0;
  62. }
  63. EXPORT_SYMBOL(devm_request_threaded_irq);
  64. /**
  65. * devm_request_any_context_irq - allocate an interrupt line for a managed device
  66. * @dev: device to request interrupt for
  67. * @irq: Interrupt line to allocate
  68. * @handler: Function to be called when the IRQ occurs
  69. * @thread_fn: function to be called in a threaded interrupt context. NULL
  70. * for devices which handle everything in @handler
  71. * @irqflags: Interrupt type flags
  72. * @devname: An ascii name for the claiming device
  73. * @dev_id: A cookie passed back to the handler function
  74. *
  75. * Except for the extra @dev argument, this function takes the
  76. * same arguments and performs the same function as
  77. * request_any_context_irq(). IRQs requested with this function will be
  78. * automatically freed on driver detach.
  79. *
  80. * If an IRQ allocated with this function needs to be freed
  81. * separately, devm_free_irq() must be used.
  82. */
  83. int devm_request_any_context_irq(struct device *dev, unsigned int irq,
  84. irq_handler_t handler, unsigned long irqflags,
  85. const char *devname, void *dev_id)
  86. {
  87. struct irq_devres *dr;
  88. int rc;
  89. dr = devres_alloc(devm_irq_release, sizeof(struct irq_devres),
  90. GFP_KERNEL);
  91. if (!dr)
  92. return -ENOMEM;
  93. rc = request_any_context_irq(irq, handler, irqflags, devname, dev_id);
  94. if (rc < 0) {
  95. devres_free(dr);
  96. return rc;
  97. }
  98. dr->irq = irq;
  99. dr->dev_id = dev_id;
  100. devres_add(dev, dr);
  101. return rc;
  102. }
  103. EXPORT_SYMBOL(devm_request_any_context_irq);
  104. /**
  105. * devm_free_irq - free an interrupt
  106. * @dev: device to free interrupt for
  107. * @irq: Interrupt line to free
  108. * @dev_id: Device identity to free
  109. *
  110. * Except for the extra @dev argument, this function takes the
  111. * same arguments and performs the same function as free_irq().
  112. * This function instead of free_irq() should be used to manually
  113. * free IRQs allocated with devm_request_irq().
  114. */
  115. void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id)
  116. {
  117. struct irq_devres match_data = { irq, dev_id };
  118. WARN_ON(devres_destroy(dev, devm_irq_release, devm_irq_match,
  119. &match_data));
  120. free_irq(irq, dev_id);
  121. }
  122. EXPORT_SYMBOL(devm_free_irq);