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
|
From c7a4c4448632af43383818654dd79d2251b27e3e Mon Sep 17 00:00:00 2001
From: John Ogness <john.ogness@linutronix.de>
Date: Fri, 15 Feb 2019 11:04:56 +0106
Subject: [PATCH 4/7] hello: add led toggling via chardev read/write
Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
hello.c | 28 +++++++++++++++++++++++++++-
1 file changed, 27 insertions(+), 1 deletion(-)
diff --git a/hello.c b/hello.c
index 58a64e5..f664121 100644
--- a/hello.c
+++ b/hello.c
@@ -61,11 +61,37 @@ static ssize_t hello_read(struct file *f, char __user *u, size_t s, loff_t *l)
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;
- dev_info(hello->dev, "%s: size=%zu offset=%llu\n", __func__, s, *l);
+ 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;
}
--
2.11.0
|