devres.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_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_free_irq - free an interrupt
  66. * @dev: device to free interrupt for
  67. * @irq: Interrupt line to free
  68. * @dev_id: Device identity to free
  69. *
  70. * Except for the extra @dev argument, this function takes the
  71. * same arguments and performs the same function as free_irq().
  72. * This function instead of free_irq() should be used to manually
  73. * free IRQs allocated with devm_request_irq().
  74. */
  75. void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id)
  76. {
  77. struct irq_devres match_data = { irq, dev_id };
  78. WARN_ON(devres_destroy(dev, devm_irq_release, devm_irq_match,
  79. &match_data));
  80. free_irq(irq, dev_id);
  81. }
  82. EXPORT_SYMBOL(devm_free_irq);