Loading...
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
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
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"
30
31struct uio_pci_generic_dev {
32 struct uio_info info;
33 struct pci_dev *pdev;
34};
35
36static inline struct uio_pci_generic_dev *
37to_uio_pci_generic_dev(struct uio_info *info)
38{
39 return container_of(info, struct uio_pci_generic_dev, info);
40}
41
42static int release(struct uio_info *info, struct inode *inode)
43{
44 struct uio_pci_generic_dev *gdev = to_uio_pci_generic_dev(info);
45
46 /*
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.
53 */
54 pci_clear_master(gdev->pdev);
55 return 0;
56}
57
58/* Interrupt handler. Read/modify/write the command register to disable
59 * the interrupt. */
60static irqreturn_t irqhandler(int irq, struct uio_info *info)
61{
62 struct uio_pci_generic_dev *gdev = to_uio_pci_generic_dev(info);
63
64 if (!pci_check_and_mask_intx(gdev->pdev))
65 return IRQ_NONE;
66
67 /* UIO core will signal the user process. */
68 return IRQ_HANDLED;
69}
70
71static int probe(struct pci_dev *pdev,
72 const struct pci_device_id *id)
73{
74 struct uio_pci_generic_dev *gdev;
75 struct uio_mem *uiomem;
76 int err;
77 int i;
78
79 err = pcim_enable_device(pdev);
80 if (err) {
81 dev_err(&pdev->dev, "%s: pci_enable_device failed: %d\n",
82 __func__, err);
83 return err;
84 }
85
86 if (pdev->irq && !pci_intx_mask_supported(pdev))
87 return -ENODEV;
88
89 gdev = devm_kzalloc(&pdev->dev, sizeof(struct uio_pci_generic_dev), GFP_KERNEL);
90 if (!gdev)
91 return -ENOMEM;
92
93 gdev->info.name = "uio_pci_generic";
94 gdev->info.version = DRIVER_VERSION;
95 gdev->info.release = release;
96 gdev->pdev = pdev;
97 if (pdev->irq && (pdev->irq != IRQ_NOTCONNECTED)) {
98 gdev->info.irq = pdev->irq;
99 gdev->info.irq_flags = IRQF_SHARED;
100 gdev->info.handler = irqhandler;
101 } else {
102 dev_warn(&pdev->dev, "No IRQ assigned to device: "
103 "no support for interrupts?\n");
104 }
105
106 uiomem = &gdev->info.mem[0];
107 for (i = 0; i < MAX_UIO_MAPS; ++i) {
108 struct resource *r = &pdev->resource[i];
109
110 if (r->flags != (IORESOURCE_SIZEALIGN | IORESOURCE_MEM))
111 continue;
112
113 if (uiomem >= &gdev->info.mem[MAX_UIO_MAPS]) {
114 dev_warn(
115 &pdev->dev,
116 "device has more than " __stringify(
117 MAX_UIO_MAPS) " I/O memory resources.\n");
118 break;
119 }
120
121 uiomem->memtype = UIO_MEM_PHYS;
122 uiomem->addr = r->start & PAGE_MASK;
123 uiomem->offs = r->start & ~PAGE_MASK;
124 uiomem->size =
125 (uiomem->offs + resource_size(r) + PAGE_SIZE - 1) &
126 PAGE_MASK;
127 uiomem->name = r->name;
128 ++uiomem;
129 }
130
131 while (uiomem < &gdev->info.mem[MAX_UIO_MAPS]) {
132 uiomem->size = 0;
133 ++uiomem;
134 }
135
136 return devm_uio_register_device(&pdev->dev, &gdev->info);
137}
138
139static struct pci_driver uio_pci_driver = {
140 .name = "uio_pci_generic",
141 .id_table = NULL, /* only dynamic id's */
142 .probe = probe,
143};
144
145module_pci_driver(uio_pci_driver);
146MODULE_VERSION(DRIVER_VERSION);
147MODULE_LICENSE("GPL v2");
148MODULE_AUTHOR(DRIVER_AUTHOR);
149MODULE_DESCRIPTION(DRIVER_DESC);
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
22#include <linux/device.h>
23#include <linux/module.h>
24#include <linux/pci.h>
25#include <linux/slab.h>
26#include <linux/uio_driver.h>
27
28#define DRIVER_VERSION "0.01.0"
29#define DRIVER_AUTHOR "Michael S. Tsirkin <mst@redhat.com>"
30#define DRIVER_DESC "Generic UIO driver for PCI 2.3 devices"
31
32struct uio_pci_generic_dev {
33 struct uio_info info;
34 struct pci_dev *pdev;
35};
36
37static inline struct uio_pci_generic_dev *
38to_uio_pci_generic_dev(struct uio_info *info)
39{
40 return container_of(info, struct uio_pci_generic_dev, info);
41}
42
43/* Interrupt handler. Read/modify/write the command register to disable
44 * the interrupt. */
45static irqreturn_t irqhandler(int irq, struct uio_info *info)
46{
47 struct uio_pci_generic_dev *gdev = to_uio_pci_generic_dev(info);
48
49 if (!pci_check_and_mask_intx(gdev->pdev))
50 return IRQ_NONE;
51
52 /* UIO core will signal the user process. */
53 return IRQ_HANDLED;
54}
55
56static int probe(struct pci_dev *pdev,
57 const struct pci_device_id *id)
58{
59 struct uio_pci_generic_dev *gdev;
60 int err;
61
62 err = pci_enable_device(pdev);
63 if (err) {
64 dev_err(&pdev->dev, "%s: pci_enable_device failed: %d\n",
65 __func__, err);
66 return err;
67 }
68
69 if (!pdev->irq) {
70 dev_warn(&pdev->dev, "No IRQ assigned to device: "
71 "no support for interrupts?\n");
72 pci_disable_device(pdev);
73 return -ENODEV;
74 }
75
76 if (!pci_intx_mask_supported(pdev)) {
77 err = -ENODEV;
78 goto err_verify;
79 }
80
81 gdev = kzalloc(sizeof(struct uio_pci_generic_dev), GFP_KERNEL);
82 if (!gdev) {
83 err = -ENOMEM;
84 goto err_alloc;
85 }
86
87 gdev->info.name = "uio_pci_generic";
88 gdev->info.version = DRIVER_VERSION;
89 gdev->info.irq = pdev->irq;
90 gdev->info.irq_flags = IRQF_SHARED;
91 gdev->info.handler = irqhandler;
92 gdev->pdev = pdev;
93
94 err = uio_register_device(&pdev->dev, &gdev->info);
95 if (err)
96 goto err_register;
97 pci_set_drvdata(pdev, gdev);
98
99 return 0;
100err_register:
101 kfree(gdev);
102err_alloc:
103err_verify:
104 pci_disable_device(pdev);
105 return err;
106}
107
108static void remove(struct pci_dev *pdev)
109{
110 struct uio_pci_generic_dev *gdev = pci_get_drvdata(pdev);
111
112 uio_unregister_device(&gdev->info);
113 pci_disable_device(pdev);
114 kfree(gdev);
115}
116
117static struct pci_driver uio_pci_driver = {
118 .name = "uio_pci_generic",
119 .id_table = NULL, /* only dynamic id's */
120 .probe = probe,
121 .remove = remove,
122};
123
124module_pci_driver(uio_pci_driver);
125MODULE_VERSION(DRIVER_VERSION);
126MODULE_LICENSE("GPL v2");
127MODULE_AUTHOR(DRIVER_AUTHOR);
128MODULE_DESCRIPTION(DRIVER_DESC);