linux-leds.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Lechner <david@lechnology.com>
To: Jacek Anaszewski <j.anaszewski@samsung.com>,
	Richard Purdie <rpurdie@rpsys.net>
Cc: David Lechner <david@lechnology.com>,
	linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] leds: uleds: make max_brightness configurable
Date: Fri, 11 Nov 2016 11:35:29 -0600	[thread overview]
Message-ID: <1478885730-23190-1-git-send-email-david@lechnology.com> (raw)

This adds support for configuring max_brightness when setting up a
userspace LED device. It also requires changing the brightness value
returned from a single byte to an integer. Since the uleds driver has
not made it into the mainline kernel yet, this is not breaking userspace.

Signed-off-by: David Lechner <david@lechnology.com>
---
 drivers/leds/uleds.c       | 16 +++++++++++-----
 include/uapi/linux/uleds.h |  1 +
 tools/leds/uledmon.c       |  5 +++--
 3 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/drivers/leds/uleds.c b/drivers/leds/uleds.c
index 2bbc73a..5e9e8a1 100644
--- a/drivers/leds/uleds.c
+++ b/drivers/leds/uleds.c
@@ -39,7 +39,7 @@ struct uleds_device {
 	struct mutex		mutex;
 	enum uleds_state	state;
 	wait_queue_head_t	waitq;
-	unsigned char		brightness;
+	int			brightness;
 	bool			new_data;
 };
 
@@ -67,7 +67,6 @@ static int uleds_open(struct inode *inode, struct file *file)
 		return -ENOMEM;
 
 	udev->led_cdev.name = udev->user_dev.name;
-	udev->led_cdev.max_brightness = LED_FULL;
 	udev->led_cdev.brightness_set = uleds_brightness_set;
 
 	mutex_init(&udev->mutex);
@@ -117,6 +116,12 @@ static ssize_t uleds_write(struct file *file, const char __user *buffer,
 		goto out;
 	}
 
+	if (udev->user_dev.max_brightness <= 0) {
+		ret = -EINVAL;
+		goto out;
+	}
+	udev->led_cdev.max_brightness = udev->user_dev.max_brightness;
+
 	ret = devm_led_classdev_register(uleds_misc.this_device,
 					 &udev->led_cdev);
 	if (ret < 0)
@@ -138,7 +143,7 @@ static ssize_t uleds_read(struct file *file, char __user *buffer, size_t count,
 	struct uleds_device *udev = file->private_data;
 	ssize_t retval;
 
-	if (count == 0)
+	if (count < sizeof(udev->brightness))
 		return 0;
 
 	do {
@@ -151,9 +156,10 @@ static ssize_t uleds_read(struct file *file, char __user *buffer, size_t count,
 		} else if (!udev->new_data && (file->f_flags & O_NONBLOCK)) {
 			retval = -EAGAIN;
 		} else if (udev->new_data) {
-			retval = copy_to_user(buffer, &udev->brightness, 1);
+			retval = copy_to_user(buffer, &udev->brightness,
+					      sizeof(udev->brightness));
 			udev->new_data = false;
-			retval = 1;
+			retval = sizeof(udev->brightness);
 		}
 
 		mutex_unlock(&udev->mutex);
diff --git a/include/uapi/linux/uleds.h b/include/uapi/linux/uleds.h
index 2545b79..3788d5e 100644
--- a/include/uapi/linux/uleds.h
+++ b/include/uapi/linux/uleds.h
@@ -18,6 +18,7 @@
 
 struct uleds_user_dev {
 	char name[LEDS_MAX_NAME_SIZE];
+	int max_brightness;
 };
 
 #endif /* _UAPI__ULEDS_H_ */
diff --git a/tools/leds/uledmon.c b/tools/leds/uledmon.c
index 44d8634..163da85 100644
--- a/tools/leds/uledmon.c
+++ b/tools/leds/uledmon.c
@@ -22,7 +22,7 @@ int main(int argc, char const *argv[])
 {
 	struct uleds_user_dev uleds_dev;
 	int fd, ret;
-	unsigned char brightness;
+	int brightness;
 	struct timespec ts;
 
 	if (argc != 2) {
@@ -31,6 +31,7 @@ int main(int argc, char const *argv[])
 	}
 
 	strncpy(uleds_dev.name, argv[1], LEDS_MAX_NAME_SIZE);
+	uleds_dev.max_brightness = 100;
 
 	fd = open("/dev/uleds", O_RDWR);
 	if (fd == -1) {
@@ -46,7 +47,7 @@ int main(int argc, char const *argv[])
 	}
 
 	while (1) {
-		ret = read(fd, &brightness, 1);
+		ret = read(fd, &brightness, sizeof(brightness));
 		if (ret == -1) {
 			perror("Failed to read from /dev/uleds");
 			close(fd);
-- 
2.7.4

             reply	other threads:[~2016-11-11 17:35 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-11 17:35 David Lechner [this message]
2016-11-13 12:29 ` [PATCH] leds: uleds: make max_brightness configurable Jacek Anaszewski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1478885730-23190-1-git-send-email-david@lechnology.com \
    --to=david@lechnology.com \
    --cc=j.anaszewski@samsung.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=rpurdie@rpsys.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).