uio_pci_generic.c 3.4 KB

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