1 // SPDX-License-Identifier: GPL-2.0
2 /* uio_pci_generic - generic UIO driver for PCI 2.3 devices
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Author: Michael S. Tsirkin <mst@redhat.com>
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:
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
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.
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>
27 #define DRIVER_VERSION "0.01.0"
28 #define DRIVER_AUTHOR "Michael S. Tsirkin <mst@redhat.com>"
29 #define DRIVER_DESC "Generic UIO driver for PCI 2.3 devices"
31 struct uio_pci_generic_dev {
36 static inline struct uio_pci_generic_dev *
37 to_uio_pci_generic_dev(struct uio_info *info)
39 return container_of(info, struct uio_pci_generic_dev, info);
42 static int release(struct uio_info *info, struct inode *inode)
44 struct uio_pci_generic_dev *gdev = to_uio_pci_generic_dev(info);
47 * This driver is insecure when used with devices doing DMA, but some
48 * people (mis)use it with such devices.
49 * Let's at least make sure DMA isn't left enabled after the userspace
50 * driver closes the fd.
51 * Note that there's a non-zero chance doing this will wedge the device
52 * at least until reset.
54 pci_clear_master(gdev->pdev);
58 /* Interrupt handler. Read/modify/write the command register to disable
60 static irqreturn_t irqhandler(int irq, struct uio_info *info)
62 struct uio_pci_generic_dev *gdev = to_uio_pci_generic_dev(info);
64 if (!pci_check_and_mask_intx(gdev->pdev))
67 /* UIO core will signal the user process. */
71 static int probe(struct pci_dev *pdev,
72 const struct pci_device_id *id)
74 struct uio_pci_generic_dev *gdev;
77 err = pcim_enable_device(pdev);
79 dev_err(&pdev->dev, "%s: pci_enable_device failed: %d\n",
84 if (pdev->irq && !pci_intx_mask_supported(pdev))
87 gdev = devm_kzalloc(&pdev->dev, sizeof(struct uio_pci_generic_dev), GFP_KERNEL);
91 gdev->info.name = "uio_pci_generic";
92 gdev->info.version = DRIVER_VERSION;
93 gdev->info.release = release;
96 gdev->info.irq = pdev->irq;
97 gdev->info.irq_flags = IRQF_SHARED;
98 gdev->info.handler = irqhandler;
100 dev_warn(&pdev->dev, "No IRQ assigned to device: "
101 "no support for interrupts?\n");
104 return devm_uio_register_device(&pdev->dev, &gdev->info);
107 static struct pci_driver uio_pci_driver = {
108 .name = "uio_pci_generic",
109 .id_table = NULL, /* only dynamic id's */
113 module_pci_driver(uio_pci_driver);
114 MODULE_VERSION(DRIVER_VERSION);
115 MODULE_LICENSE("GPL v2");
116 MODULE_AUTHOR(DRIVER_AUTHOR);
117 MODULE_DESCRIPTION(DRIVER_DESC);