blob: dde5cbb2717876b59d4d1393eced78d6cddcf193 [file] [log] [blame]
Stephen Hemmingerbce5c2e2018-07-21 06:31:39 -07001// SPDX-License-Identifier: GPL-2.0
Michael S. Tsirkinccb86a62009-07-20 10:29:34 +03002/* 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 *
Michael S. Tsirkinccb86a62009-07-20 10:29:34 +03007 * 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Michael S. Tsirkinccb86a62009-07-20 10:29:34 +030025#include <linux/uio_driver.h>
Michael S. Tsirkinccb86a62009-07-20 10:29:34 +030026
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;
Michael S. Tsirkinccb86a62009-07-20 10:29:34 +030034};
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
Venkatesh Srinivas865a11f2019-01-23 11:27:51 +000042static 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
Michael S. Tsirkinccb86a62009-07-20 10:29:34 +030058/* 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);
Michael S. Tsirkinccb86a62009-07-20 10:29:34 +030063
Jan Kiszka2502dbd2011-11-04 09:46:01 +010064 if (!pci_check_and_mask_intx(gdev->pdev))
65 return IRQ_NONE;
Michael S. Tsirkinccb86a62009-07-20 10:29:34 +030066
67 /* UIO core will signal the user process. */
Jan Kiszka2502dbd2011-11-04 09:46:01 +010068 return IRQ_HANDLED;
Michael S. Tsirkinccb86a62009-07-20 10:29:34 +030069}
70
Bill Pembertonb17b75b2012-11-19 13:21:49 -050071static int probe(struct pci_dev *pdev,
Michael S. Tsirkinccb86a62009-07-20 10:29:34 +030072 const struct pci_device_id *id)
73{
74 struct uio_pci_generic_dev *gdev;
75 int err;
76
Michael S. Tsirkinccb86a62009-07-20 10:29:34 +030077 err = pci_enable_device(pdev);
78 if (err) {
79 dev_err(&pdev->dev, "%s: pci_enable_device failed: %d\n",
80 __func__, err);
81 return err;
82 }
83
Jim Harrisacec09e2017-05-02 07:20:59 -070084 if (pdev->irq && !pci_intx_mask_supported(pdev)) {
Jan Kiszka2502dbd2011-11-04 09:46:01 +010085 err = -ENODEV;
Michael S. Tsirkinccb86a62009-07-20 10:29:34 +030086 goto err_verify;
Jan Kiszka2502dbd2011-11-04 09:46:01 +010087 }
Michael S. Tsirkinccb86a62009-07-20 10:29:34 +030088
89 gdev = kzalloc(sizeof(struct uio_pci_generic_dev), GFP_KERNEL);
90 if (!gdev) {
91 err = -ENOMEM;
92 goto err_alloc;
93 }
94
95 gdev->info.name = "uio_pci_generic";
96 gdev->info.version = DRIVER_VERSION;
Venkatesh Srinivas865a11f2019-01-23 11:27:51 +000097 gdev->info.release = release;
Michael S. Tsirkinccb86a62009-07-20 10:29:34 +030098 gdev->pdev = pdev;
Jim Harrisacec09e2017-05-02 07:20:59 -070099 if (pdev->irq) {
100 gdev->info.irq = pdev->irq;
101 gdev->info.irq_flags = IRQF_SHARED;
102 gdev->info.handler = irqhandler;
103 } else {
104 dev_warn(&pdev->dev, "No IRQ assigned to device: "
105 "no support for interrupts?\n");
106 }
Michael S. Tsirkinccb86a62009-07-20 10:29:34 +0300107
Alexey Khoroshilovc4277e92014-12-06 00:28:01 +0300108 err = uio_register_device(&pdev->dev, &gdev->info);
109 if (err)
Michael S. Tsirkinccb86a62009-07-20 10:29:34 +0300110 goto err_register;
111 pci_set_drvdata(pdev, gdev);
112
113 return 0;
114err_register:
115 kfree(gdev);
116err_alloc:
117err_verify:
118 pci_disable_device(pdev);
119 return err;
120}
121
122static void remove(struct pci_dev *pdev)
123{
124 struct uio_pci_generic_dev *gdev = pci_get_drvdata(pdev);
125
126 uio_unregister_device(&gdev->info);
127 pci_disable_device(pdev);
128 kfree(gdev);
129}
130
Peter Huewecd437392013-05-21 22:47:44 +0200131static struct pci_driver uio_pci_driver = {
Michael S. Tsirkinccb86a62009-07-20 10:29:34 +0300132 .name = "uio_pci_generic",
133 .id_table = NULL, /* only dynamic id's */
134 .probe = probe,
135 .remove = remove,
136};
137
Peter Huewecd437392013-05-21 22:47:44 +0200138module_pci_driver(uio_pci_driver);
Michael S. Tsirkinccb86a62009-07-20 10:29:34 +0300139MODULE_VERSION(DRIVER_VERSION);
140MODULE_LICENSE("GPL v2");
141MODULE_AUTHOR(DRIVER_AUTHOR);
142MODULE_DESCRIPTION(DRIVER_DESC);