linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [RFC] User-defined leds
@ 2016-07-25 18:36 David Lechner
  2016-07-25 18:45 ` Marcel Holtmann
  2016-07-26  7:38 ` Jacek Anaszewski
  0 siblings, 2 replies; 7+ messages in thread
From: David Lechner @ 2016-07-25 18:36 UTC (permalink / raw)
  To: linux-arm-kernel

short version: I have a use case for leds where I want to be able to use 
the triggers in the leds subsystem without having a physical hardware led.

long version: I am working on a program to make one embedded system 
(http://fatcatlab.com/product/evb/) compatible with another 
(http://mindstorms.lego.com). One has physical red/green LEDs, that use 
the gpio leds driver to control them. The other system does not have 
physical leds. However, it does have a color screen. So, my idea is
to create virtual LEDs on the screen that emulate the physical LEDs on 
the other device.

I would like to make a userspace program that works the same on both 
devices. If the leds were simple on-off, then it would of course be 
simpler to make the virtual leds completely in userspace. However, we 
are currently using other triggers (disk activity/heatbeat/etc.) with 
the leds. I would like for the virtual LEDs to be able to use these 
triggers as well.


Proposed implementation:

My thought is to create a new module that can be used to create 
user-defined leds using configfs. Below is some sample code that I have 
been experimenting with. Be aware, it does not quite match the 
description I am about to give.

In configfs you simply create a new node for each leds and assign the 
name. We could have separate attributes for devicename, color and 
function to enforce the naming convention or we could just allow 
arbitrary names.

Once the leds class devices are configured in configfs and exported, it 
will create the usual leds class device in sysfs as well as a character 
device that can be polled by the userspace program. The character device 
would work a bit like evdev where any time the brightness is changed, it 
spits out an event on the character device.

It would probably be simplest to have a single character device for all 
virtual leds. In this case the leds device name or some other id would 
need to be part of the event structure.

Any thoughts? Does this seem like a sane thing to do? Or maybe there is 
a different way to accomplish the same thing using existing drivers?



---

/*
  * User-defined LEDs
  *
  * Copyright (C) 2016 David Lechner <david@lechnology.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.

  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  * kind, whether express or implied; without even the implied warranty
  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  */

/*
  * Note: The comment block below is used to generate docs on the ev3dev 
website.
  * Use kramdown (markdown) syntax. Use a '.' as a placeholder when 
blank lines
  * or leading whitespace is important for the markdown syntax.
  */

/**
  * DOC: website
  *
  * User-defined LEDs
  *
  * TODO
  */

#include <linux/device.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/uaccess.h>

#include "user_led.h"

#define DEVICE_NAME "user_led"

static int major_dev_num;

#define to_user_led(_dev) container_of(_dev, struct user_led, led_cdev)

static void user_led_brightness_set(struct led_classdev *led_cdev,
				    enum led_brightness brightness)
{
	struct user_led *led = to_user_led(led_cdev);

	led->led_cdev.brightness = brightness;
	/* TODO: add support for poll() and notify here */
}

int user_led_register(struct user_led *led, struct device *parent)
{
	int err;

	if (WARN_ON(!parent))
		return -EINVAL;

	memset(&led->led_cdev, 0, sizeof(led->led_cdev));
	led->led_cdev.name = led->name;
	led->led_cdev.max_brightness = LED_FULL;
	led->led_cdev.brightness_set = user_led_brightness_set;

	err = led_classdev_register(parent, &led->led_cdev);
	if (err < 0)
		return err;

	memset(&led->cdev, 0, sizeof(led->cdev));
	/* TODO: register character device here */

	dev_info(parent, "Registered '%s'\n", led->name);

	return 0;
}
EXPORT_SYMBOL_GPL(user_led_register);

void user_led_unregister(struct user_led *led)
{
	struct device *parent = led->led_cdev.dev->parent;

	led_classdev_unregister(&led->led_cdev);
	dev_info(parent, "Unregistered '%s'\n", led->name);
}
EXPORT_SYMBOL_GPL(user_led_unregister);

static ssize_t user_led_read(struct file *file, char __user *buffer,
			     size_t count, loff_t *ptr)
{
	struct user_led *led = file->private_data;

	if (!count)
		return 0;

	count = min_t(size_t, count, sizeof(enum led_brightness));
	count -= copy_to_user(buffer, &led->led_cdev.brightness, count);

	return count;
}

static int user_led_open(struct inode *inode, struct file *file)
{
	struct user_led *led =
		container_of(inode->i_cdev, struct user_led, cdev);

	file->private_data = led;

	return 0;
}

static int user_led_release(struct inode *inode, struct file *file)
{
	file->private_data = NULL;

	return 0;
}

static const struct file_operations user_led_fops = {
	.read		= user_led_read,
	.open		= user_led_open,
	.release	= user_led_release,
};

static int __init user_led_init(void)
{
	major_dev_num = register_chrdev(0, DEVICE_NAME, &user_led_fops);
	if (major_dev_num < 0) {
		pr_err("Failed to register user_led chrdev\n");
		return major_dev_num;
	}

	return 0;
}
module_init(user_led_init);

static void __exit user_led_exit(void)
{
	unregister_chrdev(major_dev_num, DEVICE_NAME);
}
module_exit(user_led_exit);

MODULE_DESCRIPTION("User-defined LEGO led device class");
MODULE_AUTHOR("David Lechner <david@lechnology.com>");
MODULE_LICENSE("GPL");

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [RFC] User-defined leds
  2016-07-25 18:36 [RFC] User-defined leds David Lechner
@ 2016-07-25 18:45 ` Marcel Holtmann
  2016-07-26  7:38   ` Jacek Anaszewski
  2016-08-05 19:51   ` Pavel Machek
  2016-07-26  7:38 ` Jacek Anaszewski
  1 sibling, 2 replies; 7+ messages in thread
From: Marcel Holtmann @ 2016-07-25 18:45 UTC (permalink / raw)
  To: linux-arm-kernel

Hi David,

> short version: I have a use case for leds where I want to be able to use the triggers in the leds subsystem without having a physical hardware led.
> 
> long version: I am working on a program to make one embedded system (http://fatcatlab.com/product/evb/) compatible with another (http://mindstorms.lego.com). One has physical red/green LEDs, that use the gpio leds driver to control them. The other system does not have physical leds. However, it does have a color screen. So, my idea is
> to create virtual LEDs on the screen that emulate the physical LEDs on the other device.
> 
> I would like to make a userspace program that works the same on both devices. If the leds were simple on-off, then it would of course be simpler to make the virtual leds completely in userspace. However, we are currently using other triggers (disk activity/heatbeat/etc.) with the leds. I would like for the virtual LEDs to be able to use these triggers as well.

this is funny since I have just written a ledsim.c using debugfs to emulate a LED and read out its state via a simple cat command. I found that useful for testing triggers in the Bluetooth subsystem and see if they behave correctly without bothering to run this on real hardware.

> Proposed implementation:
> 
> My thought is to create a new module that can be used to create user-defined leds using configfs. Below is some sample code that I have been experimenting with. Be aware, it does not quite match the description I am about to give.
> 
> In configfs you simply create a new node for each leds and assign the name. We could have separate attributes for devicename, color and function to enforce the naming convention or we could just allow arbitrary names.
> 
> Once the leds class devices are configured in configfs and exported, it will create the usual leds class device in sysfs as well as a character device that can be polled by the userspace program. The character device would work a bit like evdev where any time the brightness is changed, it spits out an event on the character device.
> 
> It would probably be simplest to have a single character device for all virtual leds. In this case the leds device name or some other id would need to be part of the event structure.
> 
> Any thoughts? Does this seem like a sane thing to do? Or maybe there is a different way to accomplish the same thing using existing drivers?

So I used debugfs since I was lazy and only needed it for testing. However doing something similar to what /dev/uinput, /dev/uhid, /dev/rfkill, /dev/vhci etc. are doing seems to be reasonable here. What I like with these userspace driven interfaces is that the process has to keep the character device open. Once it closes it, then the kernel removes the data structures. So I would vote for creating a /dev/uled.

However creating /dev/uled would lead to using a dedicated tool to drive it. Which might be useful anyway since it could be easily extended with some easy to use functionality for testing purposes. It would also be simple to create unit test integration.

For my test of ledsim.c I opted for debugfs and cat just so that I can do it really easily. I can share my code if that is interesting to people.

Regards

Marcel

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [RFC] User-defined leds
  2016-07-25 18:36 [RFC] User-defined leds David Lechner
  2016-07-25 18:45 ` Marcel Holtmann
@ 2016-07-26  7:38 ` Jacek Anaszewski
  1 sibling, 0 replies; 7+ messages in thread
From: Jacek Anaszewski @ 2016-07-26  7:38 UTC (permalink / raw)
  To: linux-arm-kernel

Hi David,

On 07/25/2016 08:36 PM, David Lechner wrote:
> short version: I have a use case for leds where I want to be able to use
> the triggers in the leds subsystem without having a physical hardware led.
>
> long version: I am working on a program to make one embedded system
> (http://fatcatlab.com/product/evb/) compatible with another
> (http://mindstorms.lego.com). One has physical red/green LEDs, that use
> the gpio leds driver to control them. The other system does not have
> physical leds. However, it does have a color screen. So, my idea is
> to create virtual LEDs on the screen that emulate the physical LEDs on
> the other device.
>
> I would like to make a userspace program that works the same on both
> devices. If the leds were simple on-off, then it would of course be
> simpler to make the virtual leds completely in userspace. However, we
> are currently using other triggers (disk activity/heatbeat/etc.) with
> the leds. I would like for the virtual LEDs to be able to use these
> triggers as well.
>
>
> Proposed implementation:
>
> My thought is to create a new module that can be used to create
> user-defined leds using configfs. Below is some sample code that I have
> been experimenting with. Be aware, it does not quite match the
> description I am about to give.
>
> In configfs you simply create a new node for each leds and assign the
> name. We could have separate attributes for devicename, color and
> function to enforce the naming convention or we could just allow
> arbitrary names.
>
> Once the leds class devices are configured in configfs and exported, it
> will create the usual leds class device in sysfs as well as a character
> device that can be polled by the userspace program. The character device
> would work a bit like evdev where any time the brightness is changed, it
> spits out an event on the character device.
>
> It would probably be simplest to have a single character device for all
> virtual leds. In this case the leds device name or some other id would
> need to be part of the event structure.
>
> Any thoughts? Does this seem like a sane thing to do? Or maybe there is
> a different way to accomplish the same thing using existing drivers?

The idea sounds reasonable. Please also compare the solutions spotted
by Marcel. After that please submit the patch officially, along with
the sample user space application.

-- 
Best regards,
Jacek Anaszewski

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [RFC] User-defined leds
  2016-07-25 18:45 ` Marcel Holtmann
@ 2016-07-26  7:38   ` Jacek Anaszewski
  2016-08-05 19:51   ` Pavel Machek
  1 sibling, 0 replies; 7+ messages in thread
From: Jacek Anaszewski @ 2016-07-26  7:38 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Marcel,

On 07/25/2016 08:45 PM, Marcel Holtmann wrote:
> Hi David,
>
>> short version: I have a use case for leds where I want to be able to use the triggers in the leds subsystem without having a physical hardware led.
>>
>> long version: I am working on a program to make one embedded system (http://fatcatlab.com/product/evb/) compatible with another (http://mindstorms.lego.com). One has physical red/green LEDs, that use the gpio leds driver to control them. The other system does not have physical leds. However, it does have a color screen. So, my idea is
>> to create virtual LEDs on the screen that emulate the physical LEDs on the other device.
>>
>> I would like to make a userspace program that works the same on both devices. If the leds were simple on-off, then it would of course be simpler to make the virtual leds completely in userspace. However, we are currently using other triggers (disk activity/heatbeat/etc.) with the leds. I would like for the virtual LEDs to be able to use these triggers as well.
>
> this is funny since I have just written a ledsim.c using debugfs to emulate a LED and read out its state via a simple cat command. I found that useful for testing triggers in the Bluetooth subsystem and see if they behave correctly without bothering to run this on real hardware.
>
>> Proposed implementation:
>>
>> My thought is to create a new module that can be used to create user-defined leds using configfs. Below is some sample code that I have been experimenting with. Be aware, it does not quite match the description I am about to give.
>>
>> In configfs you simply create a new node for each leds and assign the name. We could have separate attributes for devicename, color and function to enforce the naming convention or we could just allow arbitrary names.
>>
>> Once the leds class devices are configured in configfs and exported, it will create the usual leds class device in sysfs as well as a character device that can be polled by the userspace program. The character device would work a bit like evdev where any time the brightness is changed, it spits out an event on the character device.
>>
>> It would probably be simplest to have a single character device for all virtual leds. In this case the leds device name or some other id would need to be part of the event structure.
>>
>> Any thoughts? Does this seem like a sane thing to do? Or maybe there is a different way to accomplish the same thing using existing drivers?
>
> So I used debugfs since I was lazy and only needed it for testing. However doing something similar to what /dev/uinput, /dev/uhid, /dev/rfkill, /dev/vhci etc. are doing seems to be reasonable here. What I like with these userspace driven interfaces is that the process has to keep the character device open. Once it closes it, then the kernel removes the data structures. So I would vote for creating a /dev/uled.
>
> However creating /dev/uled would lead to using a dedicated tool to drive it. Which might be useful anyway since it could be easily extended with some easy to use functionality for testing purposes. It would also be simple to create unit test integration.
>
> For my test of ledsim.c I opted for debugfs and cat just so that I can do it really easily. I can share my code if that is interesting to people.

Yes, it would be nice to see your code, it is always beneficial
to compare other approaches.

-- 
Best regards,
Jacek Anaszewski

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [RFC] User-defined leds
  2016-07-25 18:45 ` Marcel Holtmann
  2016-07-26  7:38   ` Jacek Anaszewski
@ 2016-08-05 19:51   ` Pavel Machek
  2016-08-05 19:54     ` David Lechner
  1 sibling, 1 reply; 7+ messages in thread
From: Pavel Machek @ 2016-08-05 19:51 UTC (permalink / raw)
  To: linux-arm-kernel

Hi!

> > short version: I have a use case for leds where I want to be able to use the triggers in the leds subsystem without having a physical hardware led.
> > 
> > long version: I am working on a program to make one embedded system (http://fatcatlab.com/product/evb/) compatible with another (http://mindstorms.lego.com). One has physical red/green LEDs, that use the gpio leds driver to control them. The other system does not have physical leds. However, it does have a color screen. So, my idea is
> > to create virtual LEDs on the screen that emulate the physical LEDs on the other device.
> > 
> > I would like to make a userspace program that works the same on both devices. If the leds were simple on-off, then it would of course be simpler to make the virtual leds completely in userspace. However, we are currently using other triggers (disk activity/heatbeat/etc.) with the leds. I would like for the virtual LEDs to be able to use these triggers as well.
> 
> this is funny since I have just written a ledsim.c using debugfs to emulate a LED and read out its state via a simple cat command. I found that useful for testing triggers in the Bluetooth subsystem and see if they behave correctly without bothering to run this on real hardware.
> 

Could the device tree be used to bind LED driver to otherwise unused
gpio?

								Pavel
-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [RFC] User-defined leds
  2016-08-05 19:51   ` Pavel Machek
@ 2016-08-05 19:54     ` David Lechner
  2016-08-05 19:58       ` Pavel Machek
  0 siblings, 1 reply; 7+ messages in thread
From: David Lechner @ 2016-08-05 19:54 UTC (permalink / raw)
  To: linux-arm-kernel

On 08/05/2016 02:51 PM, Pavel Machek wrote:
>
> Could the device tree be used to bind LED driver to otherwise unused
> gpio?
>
> 								Pavel
>

There is already a leds-gpio driver that does this.

https://www.kernel.org/doc/Documentation/devicetree/bindings/leds/leds-gpio.txt

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [RFC] User-defined leds
  2016-08-05 19:54     ` David Lechner
@ 2016-08-05 19:58       ` Pavel Machek
  0 siblings, 0 replies; 7+ messages in thread
From: Pavel Machek @ 2016-08-05 19:58 UTC (permalink / raw)
  To: linux-arm-kernel

On Fri 2016-08-05 14:54:33, David Lechner wrote:
> On 08/05/2016 02:51 PM, Pavel Machek wrote:
> >
> >Could the device tree be used to bind LED driver to otherwise unused
> >gpio?
> 
> There is already a leds-gpio driver that does this.
> 
> https://www.kernel.org/doc/Documentation/devicetree/bindings/leds/leds-gpio.txt

Yeah, I know, that's why I suggested it :-).

What might be missing: simple way to attach leds-gpio driver to
hardware on non-devicetree machines, and perhaps teaching leds-gpio
special gpio value "no gpio" meaning ... that hardware is not actually
updated.

									Pavel
-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2016-08-05 19:58 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-07-25 18:36 [RFC] User-defined leds David Lechner
2016-07-25 18:45 ` Marcel Holtmann
2016-07-26  7:38   ` Jacek Anaszewski
2016-08-05 19:51   ` Pavel Machek
2016-08-05 19:54     ` David Lechner
2016-08-05 19:58       ` Pavel Machek
2016-07-26  7:38 ` Jacek Anaszewski

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).