irq.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCI IRQ handling code
  4. *
  5. * Copyright (c) 2008 James Bottomley <[email protected]>
  6. * Copyright (C) 2017 Christoph Hellwig.
  7. */
  8. #include <linux/device.h>
  9. #include <linux/kernel.h>
  10. #include <linux/export.h>
  11. #include <linux/pci.h>
  12. /**
  13. * pci_request_irq - allocate an interrupt line for a PCI device
  14. * @dev: PCI device to operate on
  15. * @nr: device-relative interrupt vector index (0-based).
  16. * @handler: Function to be called when the IRQ occurs.
  17. * Primary handler for threaded interrupts.
  18. * If NULL and thread_fn != NULL the default primary handler is
  19. * installed.
  20. * @thread_fn: Function called from the IRQ handler thread
  21. * If NULL, no IRQ thread is created
  22. * @dev_id: Cookie passed back to the handler function
  23. * @fmt: Printf-like format string naming the handler
  24. *
  25. * This call allocates interrupt resources and enables the interrupt line and
  26. * IRQ handling. From the point this call is made @handler and @thread_fn may
  27. * be invoked. All interrupts requested using this function might be shared.
  28. *
  29. * @dev_id must not be NULL and must be globally unique.
  30. */
  31. int pci_request_irq(struct pci_dev *dev, unsigned int nr, irq_handler_t handler,
  32. irq_handler_t thread_fn, void *dev_id, const char *fmt, ...)
  33. {
  34. va_list ap;
  35. int ret;
  36. char *devname;
  37. unsigned long irqflags = IRQF_SHARED;
  38. if (!handler)
  39. irqflags |= IRQF_ONESHOT;
  40. va_start(ap, fmt);
  41. devname = kvasprintf(GFP_KERNEL, fmt, ap);
  42. va_end(ap);
  43. if (!devname)
  44. return -ENOMEM;
  45. ret = request_threaded_irq(pci_irq_vector(dev, nr), handler, thread_fn,
  46. irqflags, devname, dev_id);
  47. if (ret)
  48. kfree(devname);
  49. return ret;
  50. }
  51. EXPORT_SYMBOL(pci_request_irq);
  52. /**
  53. * pci_free_irq - free an interrupt allocated with pci_request_irq
  54. * @dev: PCI device to operate on
  55. * @nr: device-relative interrupt vector index (0-based).
  56. * @dev_id: Device identity to free
  57. *
  58. * Remove an interrupt handler. The handler is removed and if the interrupt
  59. * line is no longer in use by any driver it is disabled. The caller must
  60. * ensure the interrupt is disabled on the device before calling this function.
  61. * The function does not return until any executing interrupts for this IRQ
  62. * have completed.
  63. *
  64. * This function must not be called from interrupt context.
  65. */
  66. void pci_free_irq(struct pci_dev *dev, unsigned int nr, void *dev_id)
  67. {
  68. kfree(free_irq(pci_irq_vector(dev, nr), dev_id));
  69. }
  70. EXPORT_SYMBOL(pci_free_irq);