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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
From 5f05842b160249563a7e4965aaa01998e562f487 Mon Sep 17 00:00:00 2001
From: John Ogness <john.ogness@linutronix.de>
Date: Fri, 15 Feb 2019 11:33:24 +0100
Subject: [PATCH 7/7] hello: remove chardev, use only leds subsystem
Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
hello-led.c | 38 -------------
hello.c | 181 +-----------------------------------------------------------
hello.h | 7 ---
3 files changed, 3 insertions(+), 223 deletions(-)
delete mode 100644 hello-led.c
delete mode 100644 hello.h
diff --git a/hello-led.c b/hello-led.c
deleted file mode 100644
index 43230cd..0000000
--- a/hello-led.c
+++ /dev/null
@@ -1,38 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <sys/ioctl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include "hello.h"
-
-int main(int argc, char *argv[])
-{
- int fd;
-
- if (argc != 3) {
- fprintf(stderr, "usage: %s <device> <led>\n", argv[0]);
- return 1;
- }
-
- fd = open(argv[1], O_RDWR);
- if (fd < 0) {
- fprintf(stderr, "open() failed: %s\n", strerror(errno));
- return 1;
- }
-
- if (strcmp(argv[2], "heartbeat") == 0)
- ioctl(fd, HELLO_IOCTL_TOGGLE_LED, 0);
- else if (strcmp(argv[2], "net") == 0)
- ioctl(fd, HELLO_IOCTL_TOGGLE_LED, 1);
- else if (strcmp(argv[2], "disk") == 0)
- ioctl(fd, HELLO_IOCTL_TOGGLE_LED, 2);
- else
- fprintf(stderr, "unknown led\n");
-
- close(fd);
-
- return 0;
-}
diff --git a/hello.c b/hello.c
index f9809f3..52df6d7 100644
--- a/hello.c
+++ b/hello.c
@@ -1,19 +1,12 @@
// SPDX-License-Identifier: GPL-2.0
#include <linux/init.h>
#include <linux/module.h>
-#include <linux/fs.h>
-#include <linux/uaccess.h>
-#include <linux/slab.h>
#include <linux/device.h>
#include <linux/pci.h>
-#include <linux/cdev.h>
#include <linux/leds.h>
-#include "hello.h"
struct hello_dev {
struct device *dev;
- struct cdev cdev;
- unsigned int minor;
void __iomem *mem;
struct led_classdev led_heartbeat;
@@ -21,115 +14,6 @@ struct hello_dev {
struct led_classdev led_disk;
};
-#define HELLO_MAX_DEVICES 10
-
-static struct class *hello_class;
-static dev_t hello_devt;
-
-static int hello_open(struct inode *node, struct file *f)
-{
- struct cdev *cd = node->i_cdev;
- struct hello_dev *hello = container_of(cd, struct hello_dev, cdev);
- dev_info(hello->dev, "%s\n", __func__);
- f->private_data = hello;
- return 0;
-}
-
-static int hello_release(struct inode *node, struct file *f)
-{
- struct hello_dev *hello = f->private_data;
- dev_info(hello->dev, "%s\n", __func__);
- f->private_data = NULL;
- return 0;
-}
-
-static ssize_t hello_read(struct file *f, char __user *u, size_t s, loff_t *l)
-{
- struct hello_dev *hello = f->private_data;
- char buf[10];
- size_t len;
-
- dev_info(hello->dev, "%s: size=%zu offset=%llu\n", __func__, s, *l);
-
- if (*l >= 40)
- return 0;
-
- len = snprintf(buf, sizeof(buf), "hello:%d\n", hello->minor);
-
- if (s < len)
- len = s;
-
- if (copy_to_user(u, buf, len) != 0)
- return -EFAULT;
-
- *l += len;
-
- return len;
-}
-
-static void toggle_led(struct hello_dev *hello, int offset)
-{
- if (readb(hello->mem + offset))
- writeb(0, hello->mem + offset);
- else
- writeb(0xff, hello->mem + offset);
-}
-
-static ssize_t hello_write(struct file *f, const char __user *u, size_t s,
- loff_t *l)
-{
- struct hello_dev *hello = f->private_data;
- size_t sz = s;
- char buf[32];
-
- if (sz >= sizeof(buf))
- sz = sizeof(buf) - 1;
- if (copy_from_user(buf, u, sz) != 0)
- return -EFAULT;
- buf[sz] = 0;
-
- dev_info(hello->dev, "%s: size=%zu offset=%llu buf=%s\n", __func__,
- s, *l, buf);
-
- if (strstarts(buf, "heartbeat"))
- toggle_led(hello, 0);
- else if (strstarts(buf, "net"))
- toggle_led(hello, 1);
- else if (strstarts(buf, "disk"))
- toggle_led(hello, 2);
-
- return s;
-}
-
-static long hello_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
-{
- struct hello_dev *hello = f->private_data;
- u32 val;
-
- dev_info(hello->dev, "%s: cmd=%u arg=%lu\n", __func__, cmd, arg);
-
- switch (cmd) {
- case HELLO_IOCTL_TOGGLE_LED:
- val = arg;
- if (val >= 0 && val <= 2)
- toggle_led(hello, val);
- break;
- default:
- return -EINVAL;
- }
-
- return 0;
-}
-
-static const struct file_operations hello_fops = {
- .owner = THIS_MODULE,
- .open = hello_open,
- .release = hello_release,
- .read = hello_read,
- .write = hello_write,
- .unlocked_ioctl = hello_ioctl,
-};
-
static void set_brightness(struct hello_dev *hello, int offset,
enum led_brightness b)
{
@@ -194,8 +78,6 @@ static enum led_brightness hello_led_disk_get(struct led_classdev *ldev)
static int hello_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
{
struct hello_dev *hello;
- struct device *dev;
- dev_t devt;
int ret;
hello = devm_kzalloc(&pdev->dev, sizeof(*hello), GFP_KERNEL);
@@ -204,14 +86,6 @@ static int hello_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
return -ENOMEM;
}
- hello->minor = 1;
-
- if (hello->minor >= HELLO_MAX_DEVICES) {
- dev_err(&pdev->dev, "invalid index: %u\n", hello->minor);
- ret = -EINVAL;
- goto err_out;
- }
-
hello->dev = &pdev->dev;
pci_set_drvdata(pdev, hello);
@@ -241,24 +115,6 @@ static int hello_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
goto err_regions;
}
- devt = MKDEV(MAJOR(hello_devt), hello->minor);
-
- cdev_init(&hello->cdev, &hello_fops);
- hello->cdev.owner = THIS_MODULE;
- ret = cdev_add(&hello->cdev, devt, 1);
- if (ret != 0) {
- dev_err(&pdev->dev, "cdev_add failed\n");
- goto err_iomap;
- }
-
- dev = device_create(hello_class, &pdev->dev, devt, hello,
- "hello%d", hello->minor);
- if (IS_ERR(dev)) {
- dev_err(&pdev->dev, "device_create failed\n");
- ret = PTR_ERR(dev);
- goto err_cdev;
- }
-
hello->led_heartbeat.name = "heartbeat";
hello->led_heartbeat.brightness = get_brightness(hello, 0);
hello->led_heartbeat.max_brightness = 1;
@@ -280,14 +136,10 @@ static int hello_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
hello->led_disk.brightness_get = hello_led_disk_get;
led_classdev_register(&pdev->dev, &hello->led_disk);
- dev_info(&pdev->dev, "HELLO! I am hello device %d!\n", hello->minor);
+ dev_info(&pdev->dev, "HELLO! I am hello device!\n");
return 0;
-err_cdev:
- cdev_del(&hello->cdev);
-err_iomap:
- pci_iounmap(pdev, hello->mem);
err_regions:
pci_release_regions(pdev);
err_enable:
@@ -304,14 +156,12 @@ static void hello_remove(struct pci_dev *pdev)
led_classdev_unregister(&hello->led_disk);
led_classdev_unregister(&hello->led_net);
led_classdev_unregister(&hello->led_heartbeat);
- device_destroy(hello_class, MKDEV(MAJOR(hello_devt), hello->minor));
- cdev_del(&hello->cdev);
pci_iounmap(pdev, hello->mem);
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);
+ dev_info(&pdev->dev, "GOODBYE! I was hello device!\n");
}
static const struct pci_device_id hello_id_table[] = {
@@ -329,32 +179,9 @@ static struct pci_driver hello_driver = {
static int __init hello_init(void)
{
- int ret;
-
printk(KERN_INFO "%s\n", __func__);
- ret = alloc_chrdev_region(&hello_devt, 0, HELLO_MAX_DEVICES, "hello");
- if (ret < 0)
- return ret;
-
- hello_class = class_create(THIS_MODULE, "hello");
- if (IS_ERR(hello_class)) {
- printk(KERN_ERR "%s: failed to create class\n", __func__);
- ret = PTR_ERR(hello_class);
- goto err_region;
- }
-
- ret = pci_register_driver(&hello_driver);
- if (ret != 0)
- goto err_class;
-
- return 0;
-
-err_class:
- class_destroy(hello_class);
-err_region:
- unregister_chrdev_region(hello_devt, HELLO_MAX_DEVICES);
- return ret;
+ return pci_register_driver(&hello_driver);
}
static void __exit hello_exit(void)
@@ -362,8 +189,6 @@ static void __exit hello_exit(void)
printk(KERN_INFO "%s\n", __func__);
pci_unregister_driver(&hello_driver);
- class_destroy(hello_class);
- unregister_chrdev_region(hello_devt, HELLO_MAX_DEVICES);
}
module_init(hello_init);
diff --git a/hello.h b/hello.h
deleted file mode 100644
index cb172d9..0000000
--- a/hello.h
+++ /dev/null
@@ -1,7 +0,0 @@
-#ifndef HELLO_H
-#define HELLO_H
-
-#define HELLO_BASE 0x9F
-#define HELLO_IOCTL_TOGGLE_LED _IOW(HELLO_BASE, 0x00, unsigned int)
-
-#endif /* HELLO_H */
--
2.11.0
|