xref: /openbmc/linux/drivers/uio/uio_pci_generic.c (revision e3d786a3)
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 
31 struct uio_pci_generic_dev {
32 	struct uio_info info;
33 	struct pci_dev *pdev;
34 };
35 
36 static inline struct uio_pci_generic_dev *
37 to_uio_pci_generic_dev(struct uio_info *info)
38 {
39 	return container_of(info, struct uio_pci_generic_dev, info);
40 }
41 
42 /* Interrupt handler. Read/modify/write the command register to disable
43  * the interrupt. */
44 static irqreturn_t irqhandler(int irq, struct uio_info *info)
45 {
46 	struct uio_pci_generic_dev *gdev = to_uio_pci_generic_dev(info);
47 
48 	if (!pci_check_and_mask_intx(gdev->pdev))
49 		return IRQ_NONE;
50 
51 	/* UIO core will signal the user process. */
52 	return IRQ_HANDLED;
53 }
54 
55 static int probe(struct pci_dev *pdev,
56 			   const struct pci_device_id *id)
57 {
58 	struct uio_pci_generic_dev *gdev;
59 	int err;
60 
61 	err = pci_enable_device(pdev);
62 	if (err) {
63 		dev_err(&pdev->dev, "%s: pci_enable_device failed: %d\n",
64 			__func__, err);
65 		return err;
66 	}
67 
68 	if (pdev->irq && !pci_intx_mask_supported(pdev)) {
69 		err = -ENODEV;
70 		goto err_verify;
71 	}
72 
73 	gdev = kzalloc(sizeof(struct uio_pci_generic_dev), GFP_KERNEL);
74 	if (!gdev) {
75 		err = -ENOMEM;
76 		goto err_alloc;
77 	}
78 
79 	gdev->info.name = "uio_pci_generic";
80 	gdev->info.version = DRIVER_VERSION;
81 	gdev->pdev = pdev;
82 	if (pdev->irq) {
83 		gdev->info.irq = pdev->irq;
84 		gdev->info.irq_flags = IRQF_SHARED;
85 		gdev->info.handler = irqhandler;
86 	} else {
87 		dev_warn(&pdev->dev, "No IRQ assigned to device: "
88 			 "no support for interrupts?\n");
89 	}
90 
91 	err = uio_register_device(&pdev->dev, &gdev->info);
92 	if (err)
93 		goto err_register;
94 	pci_set_drvdata(pdev, gdev);
95 
96 	return 0;
97 err_register:
98 	kfree(gdev);
99 err_alloc:
100 err_verify:
101 	pci_disable_device(pdev);
102 	return err;
103 }
104 
105 static void remove(struct pci_dev *pdev)
106 {
107 	struct uio_pci_generic_dev *gdev = pci_get_drvdata(pdev);
108 
109 	uio_unregister_device(&gdev->info);
110 	pci_disable_device(pdev);
111 	kfree(gdev);
112 }
113 
114 static struct pci_driver uio_pci_driver = {
115 	.name = "uio_pci_generic",
116 	.id_table = NULL, /* only dynamic id's */
117 	.probe = probe,
118 	.remove = remove,
119 };
120 
121 module_pci_driver(uio_pci_driver);
122 MODULE_VERSION(DRIVER_VERSION);
123 MODULE_LICENSE("GPL v2");
124 MODULE_AUTHOR(DRIVER_AUTHOR);
125 MODULE_DESCRIPTION(DRIVER_DESC);
126