aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2017-02-20 20:31:23 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2017-02-20 20:31:23 -0500
commit7aa7d608112baf63a0b1278955f9619427373807 (patch)
treeb6a7ed4233ddf3a6760bd72aed93d4b83b1f7c45 /tools
parent85adbcd54f0982040c8cc7a086f01554b8f64427 (diff)
parentfb3d769173d26268d7bf068094a599bb28b2ac63 (diff)
Merge tag 'leds_for_4.11' of git://git.kernel.org/pub/scm/linux/kernel/git/j.anaszewski/linux-leds
Pull LED updates from Jacek Anaszewski: "New features and improvements: - add new optional brightness_hw_changed attribute for the LEDs that may have their brightness level changed autonomously (outside of kernel control) by hardware / firmware. The attribute supports userspace notifications through POLLPRI events - add led_brightness_hw_mon tool that demonstrates how to use the aforementioned feature - add LED_ON enum for LEDs that can be only turned on/off, and don't allow setting other brightness levels - allow for adjusting heartbeat trigger blink brightness level Fixes and cleanups: - avoid harmless maybe-uninitialized warning in leds-ktd2692.c - add context to the existing example entries in common LED bindings to make the documentation more clear" * tag 'leds_for_4.11' of git://git.kernel.org/pub/scm/linux/kernel/git/j.anaszewski/linux-leds: leds: ledtrig-heartbeat: Make top brightness adjustable tools/leds: Add led_hw_brightness_mon program leds: class: Add new optional brightness_hw_changed attribute leds: ktd2692: avoid harmless maybe-uninitialized warning leds: add LED_ON brightness as boolean value DT: leds: Improve examples by adding some context
Diffstat (limited to 'tools')
-rw-r--r--tools/leds/Makefile4
-rw-r--r--tools/leds/led_hw_brightness_mon.c84
2 files changed, 86 insertions, 2 deletions
diff --git a/tools/leds/Makefile b/tools/leds/Makefile
index c03a79ebf9c8..078b666fd78b 100644
--- a/tools/leds/Makefile
+++ b/tools/leds/Makefile
@@ -3,11 +3,11 @@
3CC = $(CROSS_COMPILE)gcc 3CC = $(CROSS_COMPILE)gcc
4CFLAGS = -Wall -Wextra -g -I../../include/uapi 4CFLAGS = -Wall -Wextra -g -I../../include/uapi
5 5
6all: uledmon 6all: uledmon led_hw_brightness_mon
7%: %.c 7%: %.c
8 $(CC) $(CFLAGS) -o $@ $^ 8 $(CC) $(CFLAGS) -o $@ $^
9 9
10clean: 10clean:
11 $(RM) uledmon 11 $(RM) uledmon led_hw_brightness_mon
12 12
13.PHONY: all clean 13.PHONY: all clean
diff --git a/tools/leds/led_hw_brightness_mon.c b/tools/leds/led_hw_brightness_mon.c
new file mode 100644
index 000000000000..64642ccfe442
--- /dev/null
+++ b/tools/leds/led_hw_brightness_mon.c
@@ -0,0 +1,84 @@
1/*
2 * led_hw_brightness_mon.c
3 *
4 * This program monitors LED brightness level changes having its origin
5 * in hardware/firmware, i.e. outside of kernel control.
6 * A timestamp and brightness value is printed each time the brightness changes.
7 *
8 * Usage: led_hw_brightness_mon <device-name>
9 *
10 * <device-name> is the name of the LED class device to be monitored. Pressing
11 * CTRL+C will exit.
12 */
13
14#include <errno.h>
15#include <fcntl.h>
16#include <poll.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <time.h>
21#include <unistd.h>
22
23#include <linux/uleds.h>
24
25int main(int argc, char const *argv[])
26{
27 int fd, ret;
28 char brightness_file_path[LED_MAX_NAME_SIZE + 11];
29 struct pollfd pollfd;
30 struct timespec ts;
31 char buf[11];
32
33 if (argc != 2) {
34 fprintf(stderr, "Requires <device-name> argument\n");
35 return 1;
36 }
37
38 snprintf(brightness_file_path, LED_MAX_NAME_SIZE,
39 "/sys/class/leds/%s/brightness_hw_changed", argv[1]);
40
41 fd = open(brightness_file_path, O_RDONLY);
42 if (fd == -1) {
43 printf("Failed to open %s file\n", brightness_file_path);
44 return 1;
45 }
46
47 /*
48 * read may fail if no hw brightness change has occurred so far,
49 * but it is required to avoid spurious poll notifications in
50 * the opposite case.
51 */
52 read(fd, buf, sizeof(buf));
53
54 pollfd.fd = fd;
55 pollfd.events = POLLPRI;
56
57 while (1) {
58 ret = poll(&pollfd, 1, -1);
59 if (ret == -1) {
60 printf("Failed to poll %s file (%d)\n",
61 brightness_file_path, ret);
62 ret = 1;
63 break;
64 }
65
66 clock_gettime(CLOCK_MONOTONIC, &ts);
67
68 ret = read(fd, buf, sizeof(buf));
69 if (ret < 0)
70 break;
71
72 ret = lseek(pollfd.fd, 0, SEEK_SET);
73 if (ret < 0) {
74 printf("lseek failed (%d)\n", ret);
75 break;
76 }
77
78 printf("[%ld.%09ld] %d\n", ts.tv_sec, ts.tv_nsec, atoi(buf));
79 }
80
81 close(fd);
82
83 return ret;
84}