This source file includes following definitions.
- to_uio_pci_generic_dev
- release
- irqhandler
- probe
- remove
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 static 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
48
49
50
51
52
53
54 pci_clear_master(gdev->pdev);
55 return 0;
56 }
57
58
59
60 static 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
68 return IRQ_HANDLED;
69 }
70
71 static int probe(struct pci_dev *pdev,
72 const struct pci_device_id *id)
73 {
74 struct uio_pci_generic_dev *gdev;
75 int err;
76
77 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
84 if (pdev->irq && !pci_intx_mask_supported(pdev)) {
85 err = -ENODEV;
86 goto err_verify;
87 }
88
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;
97 gdev->info.release = release;
98 gdev->pdev = pdev;
99 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 }
107
108 err = uio_register_device(&pdev->dev, &gdev->info);
109 if (err)
110 goto err_register;
111 pci_set_drvdata(pdev, gdev);
112
113 return 0;
114 err_register:
115 kfree(gdev);
116 err_alloc:
117 err_verify:
118 pci_disable_device(pdev);
119 return err;
120 }
121
122 static 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
131 static struct pci_driver uio_pci_driver = {
132 .name = "uio_pci_generic",
133 .id_table = NULL,
134 .probe = probe,
135 .remove = remove,
136 };
137
138 module_pci_driver(uio_pci_driver);
139 MODULE_VERSION(DRIVER_VERSION);
140 MODULE_LICENSE("GPL v2");
141 MODULE_AUTHOR(DRIVER_AUTHOR);
142 MODULE_DESCRIPTION(DRIVER_DESC);