summaryrefslogtreecommitdiff
path: root/schulung_tools/drivers/hellodriver/patches-leds/0001-hello-change-from-platform-to-pci-bus.patch
blob: 35dc8177c34e52b747b388197b955ac256193336 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
From 28357d61e0ec39851ca88a09b04ce876b8758448 Mon Sep 17 00:00:00 2001
From: John Ogness <john.ogness@linutronix.de>
Date: Fri, 15 Feb 2019 12:11:27 +0100
Subject: [PATCH 2/7] hello: change from platform to pci bus

Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
 hello.c | 89 +++++++++++++++++++++++++++++++----------------------------------
 1 file changed, 43 insertions(+), 46 deletions(-)

diff --git a/hello.c b/hello.c
index 7f78d06..db50302 100644
--- a/hello.c
+++ b/hello.c
@@ -5,9 +5,8 @@
 #include <linux/uaccess.h>
 #include <linux/slab.h>
 #include <linux/device.h>
-#include <linux/platform_device.h>
+#include <linux/pci.h>
 #include <linux/cdev.h>
-#include <linux/of.h>
 
 struct hello_dev {
 	struct device *dev;
@@ -77,7 +76,7 @@ static const struct file_operations hello_fops = {
 	.write = hello_write,
 };
 
-static int hello_probe(struct platform_device *pdev)
+static int hello_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 {
 	struct hello_dev *hello;
 	struct device *dev;
@@ -90,15 +89,7 @@ static int hello_probe(struct platform_device *pdev)
 		return -ENOMEM;
 	}
 
-#ifdef CONFIG_OF
-	ret = of_property_read_u32(pdev->dev.of_node, "index", &hello->minor);
-	if (ret < 0) {
-		dev_err(&pdev->dev, "no index specified\n");
-		goto err_out;
-	}
-#else
-	hello->minor = (unsigned)pdev->id;
-#endif
+	hello->minor = 1;
 
 	if (hello->minor >= HELLO_MAX_DEVICES) {
 		dev_err(&pdev->dev, "invalid index: %u\n", hello->minor);
@@ -107,7 +98,27 @@ static int hello_probe(struct platform_device *pdev)
 	}
 
 	hello->dev = &pdev->dev;
-	platform_set_drvdata(pdev, hello);
+	pci_set_drvdata(pdev, hello);
+
+	ret = pci_enable_device(pdev);
+	if (ret) {
+		dev_err(&pdev->dev, "failed to enable device\n");
+		goto err_drvdata;
+	}
+
+	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
+		dev_err(&pdev->dev, "failed to find base address\n");
+		ret = -ENODEV;
+		goto err_enable;
+	}
+
+	ret = pci_request_regions(pdev, "hello");
+	if (ret) {
+		dev_err(&pdev->dev, "failed to request resources\n");
+		goto err_enable;
+	}
+
+	pci_set_master(pdev);
 
 	devt = MKDEV(MAJOR(hello_devt), hello->minor);
 
@@ -116,7 +127,7 @@ static int hello_probe(struct platform_device *pdev)
 	ret = cdev_add(&hello->cdev, devt, 1);
 	if (ret != 0) {
 		dev_err(&pdev->dev, "cdev_add failed\n");
-		goto err_drvdata;
+		goto err_regions;
 	}
 
 	dev = device_create(hello_class, &pdev->dev, devt, hello,
@@ -133,43 +144,41 @@ static int hello_probe(struct platform_device *pdev)
 
 err_cdev:
 	cdev_del(&hello->cdev);
+err_regions:
+	pci_release_regions(pdev);
+err_enable:
+	pci_disable_device(pdev);
 err_drvdata:
-	platform_set_drvdata(pdev, NULL);
-err_out:
+	pci_set_drvdata(pdev, NULL);
 	return ret;
 }
 
-static int hello_remove(struct platform_device *pdev)
+static void hello_remove(struct pci_dev *pdev)
 {
-	struct hello_dev *hello = platform_get_drvdata(pdev);
+	struct hello_dev *hello = pci_get_drvdata(pdev);
 
 	device_destroy(hello_class, MKDEV(MAJOR(hello_devt), hello->minor));
 	cdev_del(&hello->cdev);
-	platform_set_drvdata(pdev, NULL);
+	pci_release_regions(pdev);
+	pci_disable_device(pdev);
+	pci_set_drvdata(pdev, NULL);
 
 	dev_info(&pdev->dev, "GOODBYE! I was hello device %d!\n", hello->minor);
-
-	return 0;
 }
 
-static const struct of_device_id hello_match[] = {
-	{ .compatible = "virtual,hello", },
+static const struct pci_device_id hello_id_table[] = {
+	{ 0x1af4, 0x1110, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 0 },
 	{ /* end of table */ }
 };
+MODULE_DEVICE_TABLE(pci, hello_id_table);
 
-static struct platform_driver hello_driver = {
-	.driver = {
-		.name = "hello",
-		.of_match_table = hello_match,
-	},
+static struct pci_driver hello_driver = {
+	.name = "hello",
+	.id_table = hello_id_table,
 	.probe = hello_probe,
 	.remove = hello_remove,
 };
 
-#ifndef CONFIG_OF
-static struct platform_device *pdevs[3];
-#endif
-
 static int __init hello_init(void)
 {
 	int ret;
@@ -187,16 +196,10 @@ static int __init hello_init(void)
 		goto err_region;
 	}
 
-	ret = platform_driver_register(&hello_driver);
+	ret = pci_register_driver(&hello_driver);
 	if (ret != 0)
 		goto err_class;
 
-#ifndef CONFIG_OF
-	pdevs[0] = platform_device_register_simple("hello", 1, NULL, 0);
-	pdevs[1] = platform_device_register_simple("hello", 3, NULL, 0);
-	pdevs[2] = platform_device_register_simple("hello", 5, NULL, 0);
-#endif
-
 	return 0;
 
 err_class:
@@ -210,13 +213,7 @@ static void __exit hello_exit(void)
 {
 	printk(KERN_INFO "%s\n", __func__);
 
-#ifndef CONFIG_OF
-	platform_device_unregister(pdevs[0]);
-	platform_device_unregister(pdevs[1]);
-	platform_device_unregister(pdevs[2]);
-#endif
-
-	platform_driver_unregister(&hello_driver);
+	pci_unregister_driver(&hello_driver);
 	class_destroy(hello_class);
 	unregister_chrdev_region(hello_devt, HELLO_MAX_DEVICES);
 }
-- 
2.11.0