linux-leds.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Akinobu Mita <akinobu.mita@gmail.com>
To: linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Akinobu Mita <akinobu.mita@gmail.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	Jacek Anaszewski <jacek.anaszewski@gmail.com>,
	Pavel Machek <pavel@ucw.cz>, Dan Murphy <dmurphy@ti.com>
Subject: [PATCH -next 1/2] leds: add /sys/devices/virtual/led-trigger/
Date: Thu,  3 Oct 2019 00:13:00 +0900	[thread overview]
Message-ID: <1570029181-11102-2-git-send-email-akinobu.mita@gmail.com> (raw)
In-Reply-To: <1570029181-11102-1-git-send-email-akinobu.mita@gmail.com>

Reading /sys/class/leds/<led>/trigger returns all available LED triggers.
However, this violates the "one value per file" rule of sysfs.

This makes led_triggers "real" devices and provides an
/sys/devices/virtual/led-trigger/ directory that contains a sub-directoriy
for each LED trigger device. The name of the sub-directory matches the LED
trigger name.

We can find all available LED triggers by listing this directory contents.

Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "Rafael J. Wysocki" <rafael@kernel.org>
Cc: Jacek Anaszewski <jacek.anaszewski@gmail.com>
Cc: Pavel Machek <pavel@ucw.cz>
Cc: Dan Murphy <dmurphy@ti.com>
Signed-off-by: Akinobu Mita <akinobu.mita@gmail.com>
---
 .../ABI/testing/sysfs-devices-virtual-led-trigger  |  8 +++
 drivers/leds/led-triggers.c                        | 57 ++++++++++++++++++++++
 include/linux/leds.h                               |  3 ++
 3 files changed, 68 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-devices-virtual-led-trigger

diff --git a/Documentation/ABI/testing/sysfs-devices-virtual-led-trigger b/Documentation/ABI/testing/sysfs-devices-virtual-led-trigger
new file mode 100644
index 0000000..b8eb8f3
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-devices-virtual-led-trigger
@@ -0,0 +1,8 @@
+What:		/sys/devices/virtual/leds-trigger/
+Date:		September 2019
+KernelVersion:	5.5
+Contact:	linux-leds@vger.kernel.org
+Description:
+		This directory contains a sub-directoriy for each LED trigger
+		device. The name of the sub-directory matches the LED trigger
+		name.
diff --git a/drivers/leds/led-triggers.c b/drivers/leds/led-triggers.c
index 79e30d2..0b810cf 100644
--- a/drivers/leds/led-triggers.c
+++ b/drivers/leds/led-triggers.c
@@ -267,21 +267,76 @@ void led_trigger_rename_static(const char *name, struct led_trigger *trig)
 }
 EXPORT_SYMBOL_GPL(led_trigger_rename_static);
 
+struct ledtrig_device {
+	struct device dev;
+};
+
+static void ledtrig_device_release(struct device *dev)
+{
+	struct ledtrig_device *trig_dev =
+		container_of(dev, struct ledtrig_device, dev);
+
+	kfree(trig_dev);
+}
+
+static struct bus_type led_trigger_subsys = {
+	.name = "led-trigger",
+};
+
+static int led_trigger_subsys_init(void)
+{
+	static DEFINE_MUTEX(init_mutex);
+	static bool init_done;
+	int ret = 0;
+
+	mutex_lock(&init_mutex);
+	if (!init_done) {
+		ret = subsys_virtual_register(&led_trigger_subsys, NULL);
+		if (!ret)
+			init_done = true;
+	}
+	mutex_unlock(&init_mutex);
+
+	return ret;
+}
+
 /* LED Trigger Interface */
 
 int led_trigger_register(struct led_trigger *trig)
 {
 	struct led_classdev *led_cdev;
 	struct led_trigger *_trig;
+	struct ledtrig_device *trig_dev;
+	int ret;
 
 	rwlock_init(&trig->leddev_list_lock);
 	INIT_LIST_HEAD(&trig->led_cdevs);
 
+	ret = led_trigger_subsys_init();
+	if (ret)
+		return ret;
+	trig_dev = kzalloc(sizeof(*trig_dev), GFP_KERNEL);
+	if (!trig_dev)
+		return -ENOMEM;
+
+	trig_dev->dev.bus = &led_trigger_subsys;
+	trig_dev->dev.release = ledtrig_device_release;
+	dev_set_name(&trig_dev->dev, "%s", trig->name);
+
+	ret = device_register(&trig_dev->dev);
+	if (ret) {
+		put_device(&trig_dev->dev);
+		return ret;
+	}
+
+	trig->trig_dev = trig_dev;
+
 	down_write(&triggers_list_lock);
 	/* Make sure the trigger's name isn't already in use */
 	list_for_each_entry(_trig, &trigger_list, next_trig) {
 		if (!strcmp(_trig->name, trig->name)) {
 			up_write(&triggers_list_lock);
+			device_unregister(&trig_dev->dev);
 			return -EEXIST;
 		}
 	}
@@ -327,6 +382,8 @@ void led_trigger_unregister(struct led_trigger *trig)
 		up_write(&led_cdev->trigger_lock);
 	}
 	up_read(&leds_list_lock);
+
+	device_unregister(&trig->trig_dev->dev);
 }
 EXPORT_SYMBOL_GPL(led_trigger_unregister);
 
diff --git a/include/linux/leds.h b/include/linux/leds.h
index da78b27..d63c8e7 100644
--- a/include/linux/leds.h
+++ b/include/linux/leds.h
@@ -336,6 +336,8 @@ static inline bool led_sysfs_is_disabled(struct led_classdev *led_cdev)
 
 #define TRIG_NAME_MAX 50
 
+struct ledtrig_device;
+
 struct led_trigger {
 	/* Trigger Properties */
 	const char	 *name;
@@ -350,6 +352,7 @@ struct led_trigger {
 	struct list_head  next_trig;
 
 	const struct attribute_group **groups;
+	struct ledtrig_device *trig_dev;
 };
 
 /*
-- 
2.7.4


  reply	other threads:[~2019-10-02 15:13 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-02 15:12 [PATCH -next 0/2] leds: add substitutes for /sys/class/leds/<led>/trigger Akinobu Mita
2019-10-02 15:13 ` Akinobu Mita [this message]
2019-10-02 15:27   ` [PATCH -next 1/2] leds: add /sys/devices/virtual/led-trigger/ Greg Kroah-Hartman
2019-10-02 15:28   ` Greg Kroah-Hartman
2019-10-02 15:13 ` [PATCH -next 2/2] leds: add /sys/class/leds/<led>/current-trigger Akinobu Mita
2019-10-02 15:30   ` Greg Kroah-Hartman
2019-10-02 15:47   ` Dan Murphy
2019-10-02 17:46     ` Jacek Anaszewski
2019-10-02 17:57       ` Dan Murphy
2019-10-02 18:06         ` Pavel Machek
2019-10-02 18:03 ` [PATCH -next 0/2] leds: add substitutes for /sys/class/leds/<led>/trigger Pavel Machek

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=1570029181-11102-2-git-send-email-akinobu.mita@gmail.com \
    --to=akinobu.mita@gmail.com \
    --cc=dmurphy@ti.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jacek.anaszewski@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=pavel@ucw.cz \
    --cc=rafael@kernel.org \
    /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).