uio_pci_generic.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* uio_pci_generic - generic UIO driver for PCI 2.3 devices
  3. *
  4. * Copyright (C) 2009 Red Hat, Inc.
  5. * Author: Michael S. Tsirkin <mst@redhat.com>
  6. *
  7. * Since the driver does not declare any device ids, you must allocate
  8. * id and bind the device to the driver yourself. For example:
  9. *
  10. * # echo "8086 10f5" > /sys/bus/pci/drivers/uio_pci_generic/new_id
  11. * # echo -n 0000:00:19.0 > /sys/bus/pci/drivers/e1000e/unbind
  12. * # echo -n 0000:00:19.0 > /sys/bus/pci/drivers/uio_pci_generic/bind
  13. * # ls -l /sys/bus/pci/devices/0000:00:19.0/driver
  14. * .../0000:00:19.0/driver -> ../../../bus/pci/drivers/uio_pci_generic
  15. *
  16. * Driver won't bind to devices which do not support the Interrupt Disable Bit
  17. * in the command register. All devices compliant to PCI 2.3 (circa 2002) and
  18. * all compliant PCI Express devices should support this bit.
  19. */
  20. #include <linux/device.h>
  21. #include <linux/module.h>
  22. #include <linux/pci.h>
  23. #include <linux/slab.h>
  24. #include <linux/uio_driver.h>
  25. #define DRIVER_VERSION "0.01.0"
  26. #define DRIVER_AUTHOR "Michael S. Tsirkin <mst@redhat.com>"
  27. #define DRIVER_DESC "Generic UIO driver for PCI 2.3 devices"
  28. struct uio_pci_generic_dev {
  29. struct uio_info info;
  30. struct pci_dev *pdev;
  31. };
  32. static inline struct uio_pci_generic_dev *
  33. to_uio_pci_generic_dev(struct uio_info *info)
  34. {
  35. return container_of(info, struct uio_pci_generic_dev, info);
  36. }
  37. static int release(struct uio_info *info, struct inode *inode)
  38. {
  39. struct uio_pci_generic_dev *gdev = to_uio_pci_generic_dev(info);
  40. /*
  41. * This driver is insecure when used with devices doing DMA, but some
  42.  * people (mis)use it with such devices.
  43.  * Let's at least make sure DMA isn't left enabled after the userspace
  44.  * driver closes the fd.
  45.  * Note that there's a non-zero chance doing this will wedge the device
  46.  * at least until reset.
  47. */
  48. pci_clear_master(gdev->pdev);
  49. return 0;
  50. }
  51. /* Interrupt handler. Read/modify/write the command register to disable
  52. * the interrupt. */
  53. static irqreturn_t irqhandler(int irq, struct uio_info *info)
  54. {
  55. struct uio_pci_generic_dev *gdev = to_uio_pci_generic_dev(info);
  56. if (!pci_check_and_mask_intx(gdev->pdev))
  57. return IRQ_NONE;
  58. /* UIO core will signal the user process. */
  59. return IRQ_HANDLED;
  60. }
  61. static int probe(struct pci_dev *pdev,
  62. const struct pci_device_id *id)
  63. {
  64. struct uio_pci_generic_dev *gdev;
  65. int err;
  66. err = pci_enable_device(pdev);
  67. if (err) {
  68. dev_err(&pdev->dev, "%s: pci_enable_device failed: %d\n",
  69. __func__, err);
  70. return err;
  71. }
  72. if (pdev->irq && !pci_intx_mask_supported(pdev)) {
  73. err = -ENODEV;
  74. goto err_verify;
  75. }
  76. gdev = kzalloc(sizeof(struct uio_pci_generic_dev), GFP_KERNEL);
  77. if (!gdev) {
  78. err = -ENOMEM;
  79. goto err_alloc;
  80. }
  81. gdev->info.name = "uio_pci_generic";
  82. gdev->info.version = DRIVER_VERSION;
  83. gdev->info.release = release;
  84. gdev->pdev = pdev;
  85. if (pdev->irq) {
  86. gdev->info.irq = pdev->irq;
  87. gdev->info.irq_flags = IRQF_SHARED;
  88. gdev->info.handler = irqhandler;
  89. } else {
  90. dev_warn(&pdev->dev, "No IRQ assigned to device: "
  91. "no support for interrupts?\n");
  92. }
  93. err = uio_register_device(&pdev->dev, &gdev->info);
  94. if (err)
  95. goto err_register;
  96. pci_set_drvdata(pdev, gdev);
  97. return 0;
  98. err_register:
  99. kfree(gdev);
  100. err_alloc:
  101. err_verify:
  102. pci_disable_device(pdev);
  103. return err;
  104. }
  105. static void remove(struct pci_dev *pdev)
  106. {
  107. struct uio_pci_generic_dev *gdev = pci_get_drvdata(pdev);
  108. uio_unregister_device(&gdev->info);
  109. pci_disable_device(pdev);
  110. kfree(gdev);
  111. }
  112. static struct pci_driver uio_pci_driver = {
  113. .name = "uio_pci_generic",
  114. .id_table = NULL, /* only dynamic id's */
  115. .probe = probe,
  116. .remove = remove,
  117. };
  118. module_pci_driver(uio_pci_driver);
  119. MODULE_VERSION(DRIVER_VERSION);
  120. MODULE_LICENSE("GPL v2");
  121. MODULE_AUTHOR(DRIVER_AUTHOR);
  122. MODULE_DESCRIPTION(DRIVER_DESC);