Skip to contentSkip to conversation

Adds support for PHY LEDs with offload triggers

RFC PATCH Superseded Ansuel Smith 5 participants 38 messages Nov 9, 2021 – Nov 10, 2021

Subsystems (7)

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Ansuel Smith

author maintainer
#0 lore raw

v3 cover Adds support for PHY LEDs with offload triggers

This is another attempt in adding support for PHY LEDs. Most of the times Switch/PHY have connected multiple LEDs that are controlled by HW based on some rules/event. Currently we lack any support for a generic way to control the HW part and normally we either never implement the feature or only add control for brightness or hw blink.

This is based on Marek idea of providing some API to cled but use a different implementation that in theory should be more generilized.

The current idea is:
- LED driver implement 5 API (hw_control_status/start/stop/configure). They are used to put the LED in offload mode and to configure the various trigger.
- We have hardware triggers that are used to expose to userspace the supported offload triggers and set the offload mode on trigger activation.
- We can also have triggers that both support hardware and software mode.
- The LED driver will declare each supported hardware trigger and communicate with the trigger all the supported blink modes that will be available by sysfs.
- The LED driver will set how the hardware mode is activated/disabled and will set a configure function to set each supported triggers. This function provide 5 different mode enable/disable/read/supported/zero that will enable or disable the offload trigger, read the current status, check if supported or reset any active blink mode.
- On hardware trigger activation, only the hardware mode is enabled but the blink modes are not configured. This means that the LED will run in hardware mode but will have the default rules/event set by the device by default. To change this the user will have to operate via userspace (or we can consider adding another binding in the dts to declare also a default trigger configuration)

Each LED driver will have to declare explicit support for the offload trigger (or return not supported error code) as we pass a flag that the LED driver will elaborate and understand what is referring to (based on the current active trigger).

I posted a user for this new implementation that will benefit from this and will add a big feature to it. Currently qca8k can have up to 3 LEDs connected to each PHY port and we have some device that have only one of them connected and the default configuration won't work for that.

I also posted the netdev trigger expanded with the hardware support.

More polish is required but this is just to understand if I'm taking the correct path with this implementation.

v3:
- Rework start/stop as Andrew asked.
- Introduce more logic to permit a trigger to run in hardware mode.
- Add additional patch with netdev hardware support.
- Use test_bit API to check flag passed to hw_control_configure.
- Added a new cmd to hw_control_configure to reset any active blink_mode.
- Refactor all the patches to follow this new implementation. v2:
- Fix spelling mistake (sorry)
- Drop patch 02 "permit to declare supported offload triggers". Change the logic, now the LED driver declare support for them using the configure_offload with the cmd TRIGGER_SUPPORTED.
- Rework code to follow this new implementation.
- Update Documentation to better describe how this offload implementation work.

Ansuel Smith (8):
leds: add support for hardware driven LEDs
leds: add function to configure hardware controlled LED
leds: trigger: netdev: drop NETDEV_LED_MODE_LINKUP from mode
leds: trigger: netdev: rename and expose NETDEV trigger enum modes
leds: trigger: netdev: add hardware control support
leds: trigger: add hardware-phy-activity trigger
net: dsa: qca8k: add LEDs support
dt-bindings: net: dsa: qca8k: add LEDs definition example

 .../devicetree/bindings/net/dsa/qca8k.yaml    |  20 +
 Documentation/leds/leds-class.rst             |  53 +++
 drivers/leds/Kconfig                          |  11 +
 drivers/leds/led-class.c                      |  27 ++
 drivers/leds/led-triggers.c                   |  29 ++
 drivers/leds/trigger/Kconfig                  |  28 ++
 drivers/leds/trigger/Makefile                 |   1 +
 .../trigger/ledtrig-hardware-phy-activity.c   | 171 +++++++
 drivers/leds/trigger/ledtrig-netdev.c         | 108 +++--
 drivers/net/dsa/Kconfig                       |   9 +
 drivers/net/dsa/Makefile                      |   1 +
 drivers/net/dsa/qca8k-leds.c                  | 429 ++++++++++++++++++
 drivers/net/dsa/qca8k.c                       |   8 +-
 drivers/net/dsa/qca8k.h                       |  65 +++
 include/linux/leds.h                          | 115 ++++-
 15 files changed, 1045 insertions(+), 30 deletions(-)
 create mode 100644 drivers/leds/trigger/ledtrig-hardware-phy-activity.c
 create mode 100644 drivers/net/dsa/qca8k-leds.c
Signature · 1 line
2.32.0
author maintainer
#1 lore raw

RFC PATCH v3 1/8 leds: add support for hardware driven LEDs

Some LEDs can be driven by hardware (for example a LED connected to an ethernet PHY or an ethernet switch can be configured to blink on activity on the network, which in software is done by the netdev trigger).

To do such offloading, LED driver must support this and a supported trigger must be used.

LED driver should declare the correct blink_mode supported and should set the blink_mode parameter to one of HARDWARE_CONTROLLED or SOFTWARE_HARDWARE_CONTROLLED.
The trigger will check this option and fail to activate if the blink_mode is not supported. By default if a LED driver doesn't declare blink_mode, SOFTWARE_CONTROLLED is assumed.

The LED must implement 3 main API:
- trigger_offload_status(): This asks the LED driver if offload mode is enabled or not.
Triggers will check if the offload mode is supported and will be activated accordingly. If the trigger can't run in software mode, return -EOPNOTSUPP as the blinking can't be simulated by software.
- trigger_offload_start(): This will simply enable the offload mode for the LED.
With this not declared and trigger_offload_status() returning true, it's assumed that the LED is always in offload mode.
- trigger_offload_stop(): This will simply disable the offload mode for the LED.
With this not declared and trigger_offload_status() returning true, it's assumed that the LED is always in offload mode. It's advised to the driver to put the LED in the old state but this is not enforcerd and putting the LED off is also accepted.

With HARDWARE_CONTROLLED blink_mode trigger_offload_status/start/stop is optional and any software only trigger will reject activation as the LED supports only hardware mode.

An additional config CONFIG_LEDS_HARDWARE_CONTROL is added to add support for LEDs that can be controlled by hardware.

---
 Documentation/leds/leds-class.rst | 28 ++++++++++++++++++
 drivers/leds/Kconfig              | 11 ++++++++
 drivers/leds/led-class.c          | 27 ++++++++++++++++++
 drivers/leds/led-triggers.c       | 29 +++++++++++++++++++
 include/linux/leds.h              | 47 ++++++++++++++++++++++++++++++-
 5 files changed, 141 insertions(+), 1 deletion(-)
Documentation/leds/leds-class.rst +28 -0
index cd155ead8703..645940b78d81 100644
@@ -169,6 +169,34 @@ Setting the brightness to zero with brightness_set() callback function
 should completely turn off the LED and cancel the previously programmed
 hardware blinking function, if any.
 
+Hardware driven LEDs
+===================================
+
+Some LEDs can be driven by hardware (for example a LED connected to
+an ethernet PHY or an ethernet switch can be configured to blink on activity on
+the network, which in software is done by the netdev trigger).
+
+To do such offloading, LED driver must support this and a supported trigger must
+be used.
+
+LED driver should declare the correct blink_mode supported and should set the
+blink_mode parameter to one of HARDWARE_CONTROLLED or SOFTWARE_HARDWARE_CONTROLLED.
+The trigger will check this option and fail to activate if the blink_mode is not
+supported. By default if a LED driver doesn't declare blink_mode, SOFTWARE_CONTROLLED
+is assumed.
+
+The LED must implement 3 main API:
+- hw_control_status(): This asks the LED driver if hardware mode is enabled
+    or not. Triggers will check if the hardware mode is active and will try
+    to offload their triggers if supported by the driver.
+- hw_control_start(): This will simply enable the hardware mode for the LED.
+- hw_control_stop(): This will simply disable the hardware mode for the LED.
+    It's advised to the driver to put the LED in the old state but this is not
+    enforcerd and putting the LED off is also accepted.
+
+With HARDWARE_CONTROLLED blink_mode hw_control_status/start/stop is optional
+and any software only trigger will reject activation as the LED supports only
+hardware mode.
 
 Known Issues
 ============
drivers/leds/Kconfig +11 -0
index ed800f5da7d8..bd2b19cc77ec 100644
@@ -49,6 +49,17 @@ config LEDS_BRIGHTNESS_HW_CHANGED
 
 	  See Documentation/ABI/testing/sysfs-class-led for details.
 
+config LEDS_HARDWARE_CONTROL
+	bool "LED Hardware Control support"
+	help
+	  This option enabled Hardware control support used by leds that
+	  can be driven in hardware by using supported triggers.
+
+	  Hardware blink modes will be exposed by sysfs class in
+	  /sys/class/leds based on the trigger currently active.
+
+	  If unsure, say Y.
+
 comment "LED drivers"
 
 config LEDS_88PM860X
drivers/leds/led-class.c +27 -0
index f4bb02f6e042..1df782ecac66 100644
@@ -164,6 +164,27 @@ static void led_remove_brightness_hw_changed(struct led_classdev *led_cdev)
 }
 #endif
 
+#ifdef CONFIG_LEDS_HARDWARE_CONTROL
+static int led_classdev_check_blink_mode_functions(struct led_classdev *led_cdev)
+{
+	int mode = led_cdev->blink_mode;
+
+	if (mode == SOFTWARE_HARDWARE_CONTROLLED &&
+	    (!led_cdev->hw_control_status ||
+	    !led_cdev->hw_control_start ||
+	    !led_cdev->hw_control_stop))
+		return -EINVAL;
+
+	if (mode == SOFTWARE_CONTROLLED &&
+	    (led_cdev->hw_control_status ||
+	    led_cdev->hw_control_start ||
+	    led_cdev->hw_control_stop))
+		return -EINVAL;
+
+	return 0;
+}
+#endif
+
 /**
  * led_classdev_suspend - suspend an led_classdev.
  * @led_cdev: the led_classdev to suspend.
@@ -367,6 +388,12 @@ int led_classdev_register_ext(struct device *parent,
 	if (ret < 0)
 		return ret;
 
+#ifdef CONFIG_LEDS_HARDWARE_CONTROL
+	ret = led_classdev_check_blink_mode_functions(led_cdev);
+	if (ret < 0)
+		return ret;
+#endif
+
 	mutex_init(&led_cdev->led_access);
 	mutex_lock(&led_cdev->led_access);
 	led_cdev->dev = device_create_with_groups(leds_class, parent, 0,
drivers/leds/led-triggers.c +29 -0
index 072491d3e17b..693c5d0fa980 100644
@@ -154,6 +154,27 @@ ssize_t led_trigger_read(struct file *filp, struct kobject *kobj,
 }
 EXPORT_SYMBOL_GPL(led_trigger_read);
 
+static bool led_trigger_is_supported(struct led_classdev *led_cdev,
+				     struct led_trigger *trigger)
+{
+	switch (led_cdev->blink_mode) {
+	case SOFTWARE_CONTROLLED:
+		if (trigger->supported_blink_modes == HARDWARE_ONLY)
+			return 0;
+		break;
+	case HARDWARE_CONTROLLED:
+		if (trigger->supported_blink_modes == SOFTWARE_ONLY)
+			return 0;
+		break;
+	case SOFTWARE_HARDWARE_CONTROLLED:
+		break;
+	default:
+		return 0;
+	}
+
+	return 1;
+}
+
 /* Caller must ensure led_cdev->trigger_lock held */
 int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig)
 {
@@ -179,6 +200,10 @@ int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig)
 
 		cancel_work_sync(&led_cdev->set_brightness_work);
 		led_stop_software_blink(led_cdev);
+		/* Disable hardware mode on trigger change if supported */
+		if (led_cdev->blink_mode != SOFTWARE_CONTROLLED &&
+		    led_cdev->hw_control_status(led_cdev))
+			led_cdev->hw_control_stop(led_cdev);
 		if (led_cdev->trigger->deactivate)
 			led_cdev->trigger->deactivate(led_cdev);
 		device_remove_groups(led_cdev->dev, led_cdev->trigger->groups);
@@ -188,6 +213,10 @@ int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig)
 		led_set_brightness(led_cdev, LED_OFF);
 	}
 	if (trig) {
+		/* Make sure the trigger support the LED blink mode */
+		if (!led_trigger_is_supported(led_cdev, trig))
+			return -EINVAL;
+
 		spin_lock(&trig->leddev_list_lock);
 		list_add_tail_rcu(&led_cdev->trig_list, &trig->led_cdevs);
 		spin_unlock(&trig->leddev_list_lock);
include/linux/leds.h +46 -1

77 lines not on this page · raw message

Signature · 1 line
2.32.0
reviewer
#2 lore raw

Re: [RFC PATCH v3 1/8] leds: add support for hardware driven LEDs

Quoted 15 lines · Ansuel Smith
Documentation/leds/leds-class.rst +7 -0
index cd155ead8703..645940b78d81 100644
@@ -169,6 +169,34 @@ Setting the brightness to zero with brightness_set() callback function
  should completely turn off the LED and cancel the previously programmed
  hardware blinking function, if any.
 
+Hardware driven LEDs
+===================================
+
+Some LEDs can be driven by hardware (for example a LED connected to
+an ethernet PHY or an ethernet switch can be configured to blink on activity on
+the network, which in software is done by the netdev trigger).
+

Documentation/leds/leds-class.rst Setting the brightness to zero with brightness_set() callback function · line 188

Quoted 10 lines
Documentation/leds/leds-class.rst +10 -0
+To do such offloading, LED driver must support this and a supported trigger must
+be used.
+
+LED driver should declare the correct blink_mode supported and should set the
+blink_mode parameter to one of HARDWARE_CONTROLLED or SOFTWARE_HARDWARE_CONTROLLED.
+The trigger will check this option and fail to activate if the blink_mode is not
+supported. By default if a LED driver doesn't declare blink_mode, SOFTWARE_CONTROLLED
+is assumed.
+
+The LED must implement 3 main API:

APIs:

Documentation/leds/leds-class.rst Setting the brightness to zero with brightness_set() callback function · line 194

Quoted 6 lines
Documentation/leds/leds-class.rst +6 -0
+- hw_control_status(): This asks the LED driver if hardware mode is enabled
+    or not. Triggers will check if the hardware mode is active and will try
+    to offload their triggers if supported by the driver.
+- hw_control_start(): This will simply enable the hardware mode for the LED.
+- hw_control_stop(): This will simply disable the hardware mode for the LED.
+    It's advised to the driver to put the LED in the old state but this is not

for the driver

Documentation/leds/leds-class.rst Setting the brightness to zero with brightness_set() callback function · line 195

Quoted 1 line
Documentation/leds/leds-class.rst +1 -0
+    enforcerd and putting the LED off is also accepted.

enforced

Quoted 12 lines
Documentation/leds/leds-class.rst +4 -0
+
+With HARDWARE_CONTROLLED blink_mode hw_control_status/start/stop is optional
+and any software only trigger will reject activation as the LED supports only
+hardware mode.
 
  Known Issues
  ============
drivers/leds/Kconfig +0 -0
index ed800f5da7d8..bd2b19cc77ec 100644

drivers/leds/Kconfig config LEDS_BRIGHTNESS_HW_CHANGED · line 55

Quoted 7 lines
drivers/leds/Kconfig +4 -0
@@ -49,6 +49,17 @@ config LEDS_BRIGHTNESS_HW_CHANGED
 
  	  See Documentation/ABI/testing/sysfs-class-led for details.
 
+config LEDS_HARDWARE_CONTROL
+	bool "LED Hardware Control support"
+	help
+	  This option enabled Hardware control support used by leds that

enables LEDs

Quoted 6 lines
drivers/leds/Kconfig +6 -0
+	  can be driven in hardware by using supported triggers.
+
+	  Hardware blink modes will be exposed by sysfs class in
+	  /sys/class/leds based on the trigger currently active.
+
+	  If unsure, say Y.
Signature · 1 line
~Randy
#3 lore raw

Re: [RFC PATCH v3 1/8] leds: add support for hardware driven LEDs

In reply to Ansuel Smith
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Ansuel,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on net/master]
[also build test ERROR on linus/master next-20211109]
[cannot apply to pavel-leds/for-next robh/for-next net-next/master v5.15] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Ansuel-Smith/Adds-support-for-PHY-LEDs-with-offload-triggers/20211109-102721
base:   https://git.kernel.org/pub/scm/linux/kernel/git/davem/net.git c45231a7668d6b632534f692b10592ea375b55b0
config: nios2-defconfig (attached as .config)
compiler: nios2-linux-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/3c1a17a4c47130a1b03b57bc11b643bcb62d088c
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Ansuel-Smith/Adds-support-for-PHY-LEDs-with-offload-triggers/20211109-102721 git checkout 3c1a17a4c47130a1b03b57bc11b643bcb62d088c
# save the attached .config to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=nios2 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

drivers/leds/led-triggers.c: In function 'led_trigger_set':

>> drivers/leds/led-triggers.c:205:29: error: 'struct led_classdev' has no member named 'hw_control_status'
     205 |                     led_cdev->hw_control_status(led_cdev))
         |                             ^~
>> drivers/leds/led-triggers.c:206:33: error: 'struct led_classdev' has no member named 'hw_control_stop'
     206 |                         led_cdev->hw_control_stop(led_cdev);
         |                                 ^~

vim +205 drivers/leds/led-triggers.c

177
178 /* Caller must ensure led_cdev->trigger_lock held */
179 int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig)
180 {
181 char *event = NULL;
182 char *envp[2];
183 const char *name;
184 int ret;
185
186 if (!led_cdev->trigger && !trig)
187 return 0;
188
189 name = trig ? trig->name : "none";
190 event = kasprintf(GFP_KERNEL, "TRIGGER=%s", name);
191
192 /* Remove any existing trigger */
193 if (led_cdev->trigger) {
194 spin_lock(&led_cdev->trigger->leddev_list_lock);
195 list_del_rcu(&led_cdev->trig_list);
196 spin_unlock(&led_cdev->trigger->leddev_list_lock);
197
198 /* ensure it's no longer visible on the led_cdevs list */
199 synchronize_rcu();
200
201 cancel_work_sync(&led_cdev->set_brightness_work);
202 led_stop_software_blink(led_cdev);
203 /* Disable hardware mode on trigger change if supported */
204 if (led_cdev->blink_mode != SOFTWARE_CONTROLLED &&

 > 205			    led_cdev->hw_control_status(led_cdev))
 > 206				led_cdev->hw_control_stop(led_cdev);

207 if (led_cdev->trigger->deactivate)
208 led_cdev->trigger->deactivate(led_cdev);
209 device_remove_groups(led_cdev->dev, led_cdev->trigger->groups);
210 led_cdev->trigger = NULL;
211 led_cdev->trigger_data = NULL;
212 led_cdev->activated = false;
213 led_set_brightness(led_cdev, LED_OFF);
214 }
215 if (trig) {
216 /* Make sure the trigger support the LED blink mode */
217 if (!led_trigger_is_supported(led_cdev, trig))
218 return -EINVAL;
219
220 spin_lock(&trig->leddev_list_lock);
221 list_add_tail_rcu(&led_cdev->trig_list, &trig->led_cdevs);
222 spin_unlock(&trig->leddev_list_lock);
223 led_cdev->trigger = trig;
224
225 if (trig->activate)
226 ret = trig->activate(led_cdev);
227 else
228 ret = 0;
229
230 if (ret)
231 goto err_activate;
232
233 ret = device_add_groups(led_cdev->dev, trig->groups);
234 if (ret) {
235 dev_err(led_cdev->dev, "Failed to add trigger attributes\n");
236 goto err_add_groups;
237 }
238 }
239
240 if (event) {
241 envp[0] = event;
242 envp[1] = NULL;
243 if (kobject_uevent_env(&led_cdev->dev->kobj, KOBJ_CHANGE, envp))
244 dev_err(led_cdev->dev,
245 "%s: Error sending uevent\n", __func__);
246 kfree(event);
247 }
248
249 return 0;
250
251 err_add_groups:
252
253 if (trig->deactivate)
254 trig->deactivate(led_cdev);
255 err_activate:
256
257 spin_lock(&led_cdev->trigger->leddev_list_lock);
258 list_del_rcu(&led_cdev->trig_list);
259 spin_unlock(&led_cdev->trigger->leddev_list_lock);
260 synchronize_rcu();
261 led_cdev->trigger = NULL;
262 led_cdev->trigger_data = NULL;
263 led_set_brightness(led_cdev, LED_OFF);
264 kfree(event);
265
266 return ret;
267 }
268 EXPORT_SYMBOL_GPL(led_trigger_set);
269

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

maintainer
#4 lore raw

Re: [RFC PATCH v3 1/8] leds: add support for hardware driven LEDs

In reply to Ansuel Smith
Quoted 14 lines · Ansuel Smith

Some LEDs can be driven by hardware (for example a LED connected to an ethernet PHY or an ethernet switch can be configured to blink on activity on the network, which in software is done by the netdev trigger).

To do such offloading, LED driver must support this and a supported trigger must be used.

LED driver should declare the correct blink_mode supported and should set the blink_mode parameter to one of HARDWARE_CONTROLLED or SOFTWARE_HARDWARE_CONTROLLED.
The trigger will check this option and fail to activate if the blink_mode is not supported. By default if a LED driver doesn't declare blink_mode, SOFTWARE_CONTROLLED is assumed.

Quoted 6 lines

The LED must implement 3 main API:
- trigger_offload_status(): This asks the LED driver if offload mode is enabled or not.
Triggers will check if the offload mode is supported and will be activated accordingly. If the trigger can't run in software mode, return -EOPNOTSUPP as the blinking can't be simulated by software.

I don't understand this last part. The LED controller is not implementing software mode, other than providing a method to manually turn the LED on and off. And there is a well defined call for that. If that call is a NULL, it is clear it is not implemented. There is no need to ask the driver.

Andrew

author maintainer
#5 lore raw

Re: [RFC PATCH v3 1/8] leds: add support for hardware driven LEDs

Quoted 22 lines · Andrew Lunn
Quoted 14 lines · Ansuel Smith

Some LEDs can be driven by hardware (for example a LED connected to an ethernet PHY or an ethernet switch can be configured to blink on activity on the network, which in software is done by the netdev trigger).

To do such offloading, LED driver must support this and a supported trigger must be used.

LED driver should declare the correct blink_mode supported and should set the blink_mode parameter to one of HARDWARE_CONTROLLED or SOFTWARE_HARDWARE_CONTROLLED.
The trigger will check this option and fail to activate if the blink_mode is not supported. By default if a LED driver doesn't declare blink_mode, SOFTWARE_CONTROLLED is assumed.

Quoted 6 lines

The LED must implement 3 main API:
- trigger_offload_status(): This asks the LED driver if offload mode is enabled or not.
Triggers will check if the offload mode is supported and will be activated accordingly. If the trigger can't run in software mode, return -EOPNOTSUPP as the blinking can't be simulated by software.

Quoted 7 lines

I don't understand this last part. The LED controller is not implementing software mode, other than providing a method to manually turn the LED on and off. And there is a well defined call for that. If that call is a NULL, it is clear it is not implemented. There is no need to ask the driver.

Andrew

You are right I have to remove the last part as it doesn't make sense. We already use blink_mode. Will remove in v4.

Signature · 1 line
	Ansuel
author maintainer
#6 lore raw

RFC PATCH v3 2/8 leds: add function to configure hardware controlled LED

In reply to Ansuel Smith

Add hw_control_configure helper to configure how the LED should work in hardware mode. The function require to support the particular trigger and will use the passed flag to elaborate the data and apply the correct configuration. This function will then be used by the trigger to request and update hardware configuration.

---
 Documentation/leds/leds-class.rst | 25 ++++++++++++++++++++
 include/linux/leds.h              | 39 +++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+)
Documentation/leds/leds-class.rst +25 -0
index 645940b78d81..efd2f68c46a7 100644
@@ -198,6 +198,31 @@ With HARDWARE_CONTROLLED blink_mode hw_control_status/start/stop is optional
 and any software only trigger will reject activation as the LED supports only
 hardware mode.
 
+A trigger once he declared support for hardware controlled blinks, will use the function
+hw_control_configure() provided by the driver to check support for a particular blink mode.
+This function passes as the first argument (flag) a u32 flag.
+The second argument (cmd) of hw_control_configure() method can be used to do various
+operations for the specific blink mode. We currently support ENABLE, DISABLE, READ, ZERO
+and SUPPORTED to enable, disable, read the state of the blink mode, ask the LED
+driver if it does supports the specific blink mode and to reset any blink mode active.
+
+In ENABLE/DISABLE hw_control_configure() should configure the LED to enable/disable the
+requested blink mode (flag).
+In READ hw_control_configure() should return 0 or 1 based on the status of the requested
+blink mode (flag).
+In SUPPORTED hw_control_configure() should return 0 or 1 if the LED driver supports the
+requested blink mode (flags) or not.
+In ZERO hw_control_configure() should return 0 with success operation or error.
+
+The unsigned long flag is specific to the trigger and change across them. It's in the LED
+driver interest know how to elaborate this flag and to declare support for a
+particular trigger. For this exact reason explicit support for the specific
+trigger is mandatory or the driver returns -EOPNOTSUPP if asked to enter offload mode
+with a not supported trigger.
+If the driver returns -EOPNOTSUPP on hw_control_configure(), the trigger activation will
+fail as the driver doesn't support that specific offload trigger or doesn't know
+how to handle the provided flags.
+
 Known Issues
 ============
 
include/linux/leds.h +39 -0
index cf0c6005c297..00bc4d6ed7ca 100644
@@ -73,6 +73,16 @@ enum led_blink_modes {
 	SOFTWARE_HARDWARE_CONTROLLED,
 };
 
+#ifdef CONFIG_LEDS_HARDWARE_CONTROL
+enum blink_mode_cmd {
+	BLINK_MODE_ENABLE, /* Enable the hardware blink mode */
+	BLINK_MODE_DISABLE, /* Disable the hardware blink mode */
+	BLINK_MODE_READ, /* Read the status of the hardware blink mode */
+	BLINK_MODE_SUPPORTED, /* Ask the driver if the hardware blink mode is supported */
+	BLINK_MODE_ZERO, /* Disable any hardware blink active */
+};
+#endif
+
 struct led_classdev {
 	const char		*name;
 	unsigned int brightness;
@@ -185,6 +195,17 @@ struct led_classdev {
 	 * the old status but that is not mandatory and also putting it off is accepted.
 	 */
 	int			(*hw_control_stop)(struct led_classdev *led_cdev);
+	/* This will be used to configure the various blink modes LED support in hardware
+	 * mode.
+	 * The LED driver require to support the active trigger and will elaborate the
+	 * unsigned long flag and do the operation based on the provided cmd.
+	 * Current operation are enable,disable,supported and status.
+	 * A trigger will use this to enable or disable the asked blink mode, check the
+	 * status of the blink mode or ask if the blink mode can run in hardware mode.
+	 */
+	int			(*hw_control_configure)(struct led_classdev *led_cdev,
+							unsigned long flag,
+							enum blink_mode_cmd cmd);
 #endif
 #endif
 
@@ -454,6 +475,24 @@ static inline void *led_get_trigger_data(struct led_classdev *led_cdev)
 	return led_cdev->trigger_data;
 }
 
+#ifdef CONFIG_LEDS_HARDWARE_CONTROL
+static inline bool led_trigger_blink_mode_is_supported(struct led_classdev *led_cdev,
+						       unsigned long flag)
+{
+	int ret;
+
+	/* Sanity check: make sure led support hw mode */
+	if (led_cdev->blink_mode == SOFTWARE_CONTROLLED)
+		return false;
+
+	ret = led_cdev->hw_control_configure(led_cdev, flag, BLINK_MODE_SUPPORTED);
+	if (ret > 0)
+		return true;
+
+	return false;
+}
+#endif
+
 /**
  * led_trigger_rename_static - rename a trigger
  * @name: the new trigger name
Signature · 1 line
2.32.0
#7 lore raw

Re: [RFC PATCH v3 2/8] leds: add function to configure hardware controlled LED

Hello Ansuel,

Quoted 55 lines · On Tue, 9 Nov 2021 03:26:02 +0100 Ansuel Smith <[email protected]> wrote:

Add hw_control_configure helper to configure how the LED should work in hardware mode. The function require to support the particular trigger and will use the passed flag to elaborate the data and apply the correct configuration. This function will then be used by the trigger to request and update hardware configuration.

---
 Documentation/leds/leds-class.rst | 25 ++++++++++++++++++++
 include/linux/leds.h              | 39 +++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+)
Documentation/leds/leds-class.rst +25 -0
index 645940b78d81..efd2f68c46a7 100644
@@ -198,6 +198,31 @@ With HARDWARE_CONTROLLED blink_mode hw_control_status/start/stop is optional
 and any software only trigger will reject activation as the LED supports only
 hardware mode.
 
+A trigger once he declared support for hardware controlled blinks, will use the function
+hw_control_configure() provided by the driver to check support for a particular blink mode.
+This function passes as the first argument (flag) a u32 flag.
+The second argument (cmd) of hw_control_configure() method can be used to do various
+operations for the specific blink mode. We currently support ENABLE, DISABLE, READ, ZERO
+and SUPPORTED to enable, disable, read the state of the blink mode, ask the LED
+driver if it does supports the specific blink mode and to reset any blink mode active.
+
+In ENABLE/DISABLE hw_control_configure() should configure the LED to enable/disable the
+requested blink mode (flag).
+In READ hw_control_configure() should return 0 or 1 based on the status of the requested
+blink mode (flag).
+In SUPPORTED hw_control_configure() should return 0 or 1 if the LED driver supports the
+requested blink mode (flags) or not.
+In ZERO hw_control_configure() should return 0 with success operation or error.
+
+The unsigned long flag is specific to the trigger and change across them. It's in the LED
+driver interest know how to elaborate this flag and to declare support for a
+particular trigger. For this exact reason explicit support for the specific
+trigger is mandatory or the driver returns -EOPNOTSUPP if asked to enter offload mode
+with a not supported trigger.
+If the driver returns -EOPNOTSUPP on hw_control_configure(), the trigger activation will
+fail as the driver doesn't support that specific offload trigger or doesn't know
+how to handle the provided flags.
+
 Known Issues
 ============
 
include/linux/leds.h +0 -0
index cf0c6005c297..00bc4d6ed7ca 100644
@@ -73,6 +73,16 @@ enum led_blink_modes {
 	SOFTWARE_HARDWARE_CONTROLLED,
 };

include/linux/leds.h enum led_blink_modes { · line 84

Quoted 10 lines
include/linux/leds.h +9 -0
 
+#ifdef CONFIG_LEDS_HARDWARE_CONTROL
+enum blink_mode_cmd {
+	BLINK_MODE_ENABLE, /* Enable the hardware blink mode */
+	BLINK_MODE_DISABLE, /* Disable the hardware blink mode */
+	BLINK_MODE_READ, /* Read the status of the hardware blink mode */
+	BLINK_MODE_SUPPORTED, /* Ask the driver if the hardware blink mode is supported */
+	BLINK_MODE_ZERO, /* Disable any hardware blink active */
+};
+#endif

this is a strange proposal for the API.

Anyway, led_classdev already has the blink_set() method, which is documented as
/*
* Activate hardware accelerated blink, delays are in milliseconds
* and if both are zero then a sensible default should be chosen.
* The call should adjust the timings in that case and if it can't
* match the values specified exactly.
* Deactivate blinking again when the brightness is set to LED_OFF
* via the brightness_set() callback.
*/
int (*blink_set)(struct led_classdev *led_cdev,
unsigned long *delay_on, unsigned long *delay_off);

So we already have a method to set hardware blkinking, we don't need another one.

Marek

author maintainer
#8 lore raw

Re: [RFC PATCH v3 2/8] leds: add function to configure hardware controlled LED

Quoted 86 lines · Marek Behún

Hello Ansuel,

Quoted 55 lines · On Tue, 9 Nov 2021 03:26:02 +0100 Ansuel Smith <[email protected]> wrote:

Add hw_control_configure helper to configure how the LED should work in hardware mode. The function require to support the particular trigger and will use the passed flag to elaborate the data and apply the correct configuration. This function will then be used by the trigger to request and update hardware configuration.

---
 Documentation/leds/leds-class.rst | 25 ++++++++++++++++++++
 include/linux/leds.h              | 39 +++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+)
Documentation/leds/leds-class.rst +25 -0
index 645940b78d81..efd2f68c46a7 100644
@@ -198,6 +198,31 @@ With HARDWARE_CONTROLLED blink_mode hw_control_status/start/stop is optional
 and any software only trigger will reject activation as the LED supports only
 hardware mode.
 
+A trigger once he declared support for hardware controlled blinks, will use the function
+hw_control_configure() provided by the driver to check support for a particular blink mode.
+This function passes as the first argument (flag) a u32 flag.
+The second argument (cmd) of hw_control_configure() method can be used to do various
+operations for the specific blink mode. We currently support ENABLE, DISABLE, READ, ZERO
+and SUPPORTED to enable, disable, read the state of the blink mode, ask the LED
+driver if it does supports the specific blink mode and to reset any blink mode active.
+
+In ENABLE/DISABLE hw_control_configure() should configure the LED to enable/disable the
+requested blink mode (flag).
+In READ hw_control_configure() should return 0 or 1 based on the status of the requested
+blink mode (flag).
+In SUPPORTED hw_control_configure() should return 0 or 1 if the LED driver supports the
+requested blink mode (flags) or not.
+In ZERO hw_control_configure() should return 0 with success operation or error.
+
+The unsigned long flag is specific to the trigger and change across them. It's in the LED
+driver interest know how to elaborate this flag and to declare support for a
+particular trigger. For this exact reason explicit support for the specific
+trigger is mandatory or the driver returns -EOPNOTSUPP if asked to enter offload mode
+with a not supported trigger.
+If the driver returns -EOPNOTSUPP on hw_control_configure(), the trigger activation will
+fail as the driver doesn't support that specific offload trigger or doesn't know
+how to handle the provided flags.
+
 Known Issues
 ============
 
include/linux/leds.h +0 -0
index cf0c6005c297..00bc4d6ed7ca 100644
@@ -73,6 +73,16 @@ enum led_blink_modes {
 	SOFTWARE_HARDWARE_CONTROLLED,
 };

include/linux/leds.h enum led_blink_modes { · line 84

Quoted 10 lines
include/linux/leds.h +9 -0
 
+#ifdef CONFIG_LEDS_HARDWARE_CONTROL
+enum blink_mode_cmd {
+	BLINK_MODE_ENABLE, /* Enable the hardware blink mode */
+	BLINK_MODE_DISABLE, /* Disable the hardware blink mode */
+	BLINK_MODE_READ, /* Read the status of the hardware blink mode */
+	BLINK_MODE_SUPPORTED, /* Ask the driver if the hardware blink mode is supported */
+	BLINK_MODE_ZERO, /* Disable any hardware blink active */
+};
+#endif

this is a strange proposal for the API.

Anyway, led_classdev already has the blink_set() method, which is documented as
/*
* Activate hardware accelerated blink, delays are in milliseconds
* and if both are zero then a sensible default should be chosen.
* The call should adjust the timings in that case and if it can't
* match the values specified exactly.
* Deactivate blinking again when the brightness is set to LED_OFF
* via the brightness_set() callback.
*/
int (*blink_set)(struct led_classdev *led_cdev,
unsigned long *delay_on, unsigned long *delay_off);

Quoted 4 lines

So we already have a method to set hardware blkinking, we don't need another one.

Marek

But that is about hardware blink, not a LED controlled by hardware based on some rules/modes.
Doesn't really match the use for the hardware control. Blink_set makes the LED blink contantly at the declared delay. The blink_mode_cmd are used to request stuff to a LED in hardware mode.

Doesn't seem correct to change/enhance the blink_set function with something that would do something completely different.

Signature · 1 line
	Ansuel
maintainer
#9 lore raw

Re: [RFC PATCH v3 2/8] leds: add function to configure hardware controlled LED

Quoted 30 lines
Quoted 25 lines

include/linux/leds.h enum led_blink_modes { · line 84

Quoted 9 lines
include/linux/leds.h +9 -0
+#ifdef CONFIG_LEDS_HARDWARE_CONTROL
+enum blink_mode_cmd {
+	BLINK_MODE_ENABLE, /* Enable the hardware blink mode */
+	BLINK_MODE_DISABLE, /* Disable the hardware blink mode */
+	BLINK_MODE_READ, /* Read the status of the hardware blink mode */
+	BLINK_MODE_SUPPORTED, /* Ask the driver if the hardware blink mode is supported */
+	BLINK_MODE_ZERO, /* Disable any hardware blink active */
+};
+#endif

this is a strange proposal for the API.

Anyway, led_classdev already has the blink_set() method, which is documented as
/*
* Activate hardware accelerated blink, delays are in milliseconds
* and if both are zero then a sensible default should be chosen.
* The call should adjust the timings in that case and if it can't
* match the values specified exactly.
* Deactivate blinking again when the brightness is set to LED_OFF
* via the brightness_set() callback.
*/
int (*blink_set)(struct led_classdev *led_cdev,
unsigned long *delay_on, unsigned long *delay_off);

Quoted 4 lines

So we already have a method to set hardware blkinking, we don't need another one.

Marek

Quoted 8 lines

But that is about hardware blink, not a LED controlled by hardware based on some rules/modes.
Doesn't really match the use for the hardware control. Blink_set makes the LED blink contantly at the declared delay. The blink_mode_cmd are used to request stuff to a LED in hardware mode.

Doesn't seem correct to change/enhance the blink_set function with something that would do something completely different.

Humm. I can see merits for both.

What i like about reusing blink_set() is that it is well understood. There is a defined sysfs API for it. ledtrig-oneshot.c also uses it, for a non-repeating blink. So i think that also fits the PHY LED use case.

Andrew

author maintainer
#10 lore raw

Re: [RFC PATCH v3 2/8] leds: add function to configure hardware controlled LED

Quoted 39 lines · Andrew Lunn
Quoted 30 lines
Quoted 25 lines

include/linux/leds.h enum led_blink_modes { · line 84

Quoted 9 lines
include/linux/leds.h +9 -0
+#ifdef CONFIG_LEDS_HARDWARE_CONTROL
+enum blink_mode_cmd {
+	BLINK_MODE_ENABLE, /* Enable the hardware blink mode */
+	BLINK_MODE_DISABLE, /* Disable the hardware blink mode */
+	BLINK_MODE_READ, /* Read the status of the hardware blink mode */
+	BLINK_MODE_SUPPORTED, /* Ask the driver if the hardware blink mode is supported */
+	BLINK_MODE_ZERO, /* Disable any hardware blink active */
+};
+#endif

this is a strange proposal for the API.

Anyway, led_classdev already has the blink_set() method, which is documented as
/*
* Activate hardware accelerated blink, delays are in milliseconds
* and if both are zero then a sensible default should be chosen.
* The call should adjust the timings in that case and if it can't
* match the values specified exactly.
* Deactivate blinking again when the brightness is set to LED_OFF
* via the brightness_set() callback.
*/
int (*blink_set)(struct led_classdev *led_cdev,
unsigned long *delay_on, unsigned long *delay_off);

Quoted 4 lines

So we already have a method to set hardware blkinking, we don't need another one.

Marek

Quoted 8 lines

But that is about hardware blink, not a LED controlled by hardware based on some rules/modes.
Doesn't really match the use for the hardware control. Blink_set makes the LED blink contantly at the declared delay. The blink_mode_cmd are used to request stuff to a LED in hardware mode.

Doesn't seem correct to change/enhance the blink_set function with something that would do something completely different.

Quoted 8 lines

Humm. I can see merits for both.

What i like about reusing blink_set() is that it is well understood. There is a defined sysfs API for it. ledtrig-oneshot.c also uses it, for a non-repeating blink. So i think that also fits the PHY LED use case.

Andrew

If we should reuse blink_set to control hw blink I need to understand 2 thing.

The idea to implement the function hw_control_configure was to provide the triggers some way to configure the various blink_mode. (and by using the cmd enum provide different info based on the return value)

The advised path from Marek is to make the changes in the trigger_data and the LED driver will then use that to configure blink mode.

I need to call an example to explain my concern:
qca8k switch. Can both run in hardware mode and software mode (by controlling the reg to trigger manual blink) and also there is an extra mode to blink permanently at 4hz.

Now someone would declare the brightness_set to control the led manually and blink_set (for the permanent 4hz blink feature) So that's where my idea comes about introducting another function and the fact that it wouldn't match that well with blink_set with some LED.

I mean if we really want to use blink_set also for this(hw_control), we can consider adding a bool to understand when hw_control is active or not. So blink_set can be used for both feature.

Is that acceptable?

Also if we want to use blink_set I think we will have to drop all the cmd mess and remove some error handling. Don't like that but if that's what is needed for the feature, it's ok for me.

Signature · 1 line
	Ansuel
maintainer
#11 lore raw

Re: [RFC PATCH v3 2/8] leds: add function to configure hardware controlled LED

Quoted 15 lines

If we should reuse blink_set to control hw blink I need to understand 2 thing.

The idea to implement the function hw_control_configure was to provide the triggers some way to configure the various blink_mode. (and by using the cmd enum provide different info based on the return value)

The advised path from Marek is to make the changes in the trigger_data and the LED driver will then use that to configure blink mode.

I need to call an example to explain my concern:
qca8k switch. Can both run in hardware mode and software mode (by controlling the reg to trigger manual blink) and also there is an extra mode to blink permanently at 4hz.

Quoted 8 lines

Now someone would declare the brightness_set to control the led manually and blink_set (for the permanent 4hz blink feature) So that's where my idea comes about introducting another function and the fact that it wouldn't match that well with blink_set with some LED.

I mean if we really want to use blink_set also for this(hw_control), we can consider adding a bool to understand when hw_control is active or not. So blink_set can be used for both feature.

I don't see why we need the bool. The driver should know that speeds it supports. If asked to do something it cannot do in the current mode it should return either -EINVAL, or maybe -EOPNOTSUPP. Depending on how to the trigger works, we might want -EOPNOTSUPP when in a hardware offload mode, which gets returned to user space. If we are in a software blinking mode -EINVAL, so that the trigger does the blinking in software.

Andrew

#12 lore raw

Re: [RFC PATCH v3 2/8] leds: add function to configure hardware controlled LED

In reply to Ansuel Smith
Quoted 87 lines · On Tue, 9 Nov 2021 15:22:53 +0100 Ansuel Smith <[email protected]> wrote:
Quoted 86 lines · Marek Behún

Hello Ansuel,

Quoted 55 lines · On Tue, 9 Nov 2021 03:26:02 +0100 Ansuel Smith <[email protected]> wrote:

Add hw_control_configure helper to configure how the LED should work in hardware mode. The function require to support the particular trigger and will use the passed flag to elaborate the data and apply the correct configuration. This function will then be used by the trigger to request and update hardware configuration.

---
 Documentation/leds/leds-class.rst | 25 ++++++++++++++++++++
 include/linux/leds.h              | 39 +++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+)
Documentation/leds/leds-class.rst +25 -0
index 645940b78d81..efd2f68c46a7 100644
@@ -198,6 +198,31 @@ With HARDWARE_CONTROLLED blink_mode hw_control_status/start/stop is optional
 and any software only trigger will reject activation as the LED supports only
 hardware mode.
 
+A trigger once he declared support for hardware controlled blinks, will use the function
+hw_control_configure() provided by the driver to check support for a particular blink mode.
+This function passes as the first argument (flag) a u32 flag.
+The second argument (cmd) of hw_control_configure() method can be used to do various
+operations for the specific blink mode. We currently support ENABLE, DISABLE, READ, ZERO
+and SUPPORTED to enable, disable, read the state of the blink mode, ask the LED
+driver if it does supports the specific blink mode and to reset any blink mode active.
+
+In ENABLE/DISABLE hw_control_configure() should configure the LED to enable/disable the
+requested blink mode (flag).
+In READ hw_control_configure() should return 0 or 1 based on the status of the requested
+blink mode (flag).
+In SUPPORTED hw_control_configure() should return 0 or 1 if the LED driver supports the
+requested blink mode (flags) or not.
+In ZERO hw_control_configure() should return 0 with success operation or error.
+
+The unsigned long flag is specific to the trigger and change across them. It's in the LED
+driver interest know how to elaborate this flag and to declare support for a
+particular trigger. For this exact reason explicit support for the specific
+trigger is mandatory or the driver returns -EOPNOTSUPP if asked to enter offload mode
+with a not supported trigger.
+If the driver returns -EOPNOTSUPP on hw_control_configure(), the trigger activation will
+fail as the driver doesn't support that specific offload trigger or doesn't know
+how to handle the provided flags.
+
 Known Issues
 ============
 
include/linux/leds.h +0 -0
index cf0c6005c297..00bc4d6ed7ca 100644
@@ -73,6 +73,16 @@ enum led_blink_modes {
 	SOFTWARE_HARDWARE_CONTROLLED,
 };

include/linux/leds.h enum led_blink_modes { · line 84

Quoted 10 lines
include/linux/leds.h +9 -0
 
+#ifdef CONFIG_LEDS_HARDWARE_CONTROL
+enum blink_mode_cmd {
+	BLINK_MODE_ENABLE, /* Enable the hardware blink mode */
+	BLINK_MODE_DISABLE, /* Disable the hardware blink mode */
+	BLINK_MODE_READ, /* Read the status of the hardware blink mode */
+	BLINK_MODE_SUPPORTED, /* Ask the driver if the hardware blink mode is supported */
+	BLINK_MODE_ZERO, /* Disable any hardware blink active */
+};
+#endif  

this is a strange proposal for the API.

Anyway, led_classdev already has the blink_set() method, which is documented as
/*
* Activate hardware accelerated blink, delays are in milliseconds
* and if both are zero then a sensible default should be chosen.
* The call should adjust the timings in that case and if it can't
* match the values specified exactly.
* Deactivate blinking again when the brightness is set to LED_OFF
* via the brightness_set() callback.
*/
int (*blink_set)(struct led_classdev *led_cdev,
unsigned long *delay_on, unsigned long *delay_off);

Quoted 8 lines
Quoted 4 lines

So we already have a method to set hardware blkinking, we don't need another one.

Marek

But that is about hardware blink, not a LED controlled by hardware based on some rules/modes.
Doesn't really match the use for the hardware control.

I think there is a miscommunication, because I don't quite understand what you are trying to say here.

How is "hardware blink" different from "a LED controlled by hardware based on some rules/modes", when it is used for blinking ?

If the hardware, any hardware, supports blinking with different frequencies, it should implement the blink_set() method.

Quoted 6 lines

Blink_set makes the LED blink contantly at the declared delay. The blink_mode_cmd are used to request stuff to a LED in hardware mode.

Doesn't seem correct to change/enhance the blink_set function with something that would do something completely different.

reviewer
#13 lore raw

Re: [RFC PATCH v3 2/8] leds: add function to configure hardware controlled LED

In reply to Ansuel Smith

Documentation/leds/leds-class.rst With HARDWARE_CONTROLLED blink_mode hw_control_status/start/stop is optional · line 201

Quoted 9 lines · Ansuel Smith
Documentation/leds/leds-class.rst +1 -0
index 645940b78d81..efd2f68c46a7 100644
@@ -198,6 +198,31 @@ With HARDWARE_CONTROLLED blink_mode hw_control_status/start/stop is optional
  and any software only trigger will reject activation as the LED supports only
  hardware mode.
 
+A trigger once he declared support for hardware controlled blinks, will use the function

Maybe this:
Once a trigger has declared support for hardware-controller blinks, it will us the function

Quoted 6 lines
Documentation/leds/leds-class.rst +6 -0
+hw_control_configure() provided by the driver to check support for a particular blink mode.
+This function passes as the first argument (flag) a u32 flag.
+The second argument (cmd) of hw_control_configure() method can be used to do various
+operations for the specific blink mode. We currently support ENABLE, DISABLE, READ, ZERO
+and SUPPORTED to enable, disable, read the state of the blink mode, ask the LED
+driver if it does supports the specific blink mode and to reset any blink mode active.

Documentation/leds/leds-class.rst With HARDWARE_CONTROLLED blink_mode hw_control_status/start/stop is optional · line 217

Quoted 10 lines
Documentation/leds/leds-class.rst +10 -0
+
+In ENABLE/DISABLE hw_control_configure() should configure the LED to enable/disable the
+requested blink mode (flag).
+In READ hw_control_configure() should return 0 or 1 based on the status of the requested
+blink mode (flag).
+In SUPPORTED hw_control_configure() should return 0 or 1 if the LED driver supports the
+requested blink mode (flags) or not.
+In ZERO hw_control_configure() should return 0 with success operation or error.
+
+The unsigned long flag is specific to the trigger and change across them. It's in the LED

changes

Documentation/leds/leds-class.rst With HARDWARE_CONTROLLED blink_mode hw_control_status/start/stop is optional · line 218

Quoted 1 line
Documentation/leds/leds-class.rst +1 -0
+driver interest know how to elaborate this flag and to declare support for a

driver's interest to know how to

Quoted 6 lines
Documentation/leds/leds-class.rst +6 -0
+particular trigger. For this exact reason explicit support for the specific
+trigger is mandatory or the driver returns -EOPNOTSUPP if asked to enter offload mode
+with a not supported trigger.
+If the driver returns -EOPNOTSUPP on hw_control_configure(), the trigger activation will
+fail as the driver doesn't support that specific offload trigger or doesn't know
+how to handle the provided flags.
Signature · 1 line
~Randy
author maintainer
#14 lore raw

RFC PATCH v3 3/8 leds: trigger: netdev: drop NETDEV_LED_MODE_LINKUP from mode

In reply to Ansuel Smith

Drop NETDEV_LED_MODE_LINKUP from mode list and convert to a simple bool that will be true or false based on the carrier link. No functional change intended.

---
 drivers/leds/trigger/ledtrig-netdev.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/leds/trigger/ledtrig-netdev.c +7 -9
index d5e774d83021..66a81cc9b64d 100644
@@ -50,10 +50,10 @@ struct led_netdev_data {
 	unsigned int last_activity;
 
 	unsigned long mode;
+	bool carrier_link_up;
 #define NETDEV_LED_LINK	0
 #define NETDEV_LED_TX	1
 #define NETDEV_LED_RX	2
-#define NETDEV_LED_MODE_LINKUP	3
 };
 
 enum netdev_led_attr {
@@ -73,9 +73,9 @@ static void set_baseline_state(struct led_netdev_data *trigger_data)
 	if (!led_cdev->blink_brightness)
 		led_cdev->blink_brightness = led_cdev->max_brightness;
 
-	if (!test_bit(NETDEV_LED_MODE_LINKUP, &trigger_data->mode))
+	if (!trigger_data->carrier_link_up) {
 		led_set_brightness(led_cdev, LED_OFF);
-	else {
+	} else {
 		if (test_bit(NETDEV_LED_LINK, &trigger_data->mode))
 			led_set_brightness(led_cdev,
 					   led_cdev->blink_brightness);
@@ -131,10 +131,9 @@ static ssize_t device_name_store(struct device *dev,
 		trigger_data->net_dev =
 		    dev_get_by_name(&init_net, trigger_data->device_name);
 
-	clear_bit(NETDEV_LED_MODE_LINKUP, &trigger_data->mode);
+	trigger_data->carrier_link_up = false;
 	if (trigger_data->net_dev != NULL)
-		if (netif_carrier_ok(trigger_data->net_dev))
-			set_bit(NETDEV_LED_MODE_LINKUP, &trigger_data->mode);
+		trigger_data->carrier_link_up = netif_carrier_ok(trigger_data->net_dev);
 
 	trigger_data->last_activity = 0;
 
@@ -315,7 +314,7 @@ static int netdev_trig_notify(struct notifier_block *nb,
 
 	spin_lock_bh(&trigger_data->lock);
 
-	clear_bit(NETDEV_LED_MODE_LINKUP, &trigger_data->mode);
+	trigger_data->carrier_link_up = false;
 	switch (evt) {
 	case NETDEV_CHANGENAME:
 	case NETDEV_REGISTER:
@@ -330,8 +329,7 @@ static int netdev_trig_notify(struct notifier_block *nb,
 		break;
 	case NETDEV_UP:
 	case NETDEV_CHANGE:
-		if (netif_carrier_ok(dev))
-			set_bit(NETDEV_LED_MODE_LINKUP, &trigger_data->mode);
+		trigger_data->carrier_link_up = netif_carrier_ok(dev);
 		break;
 	}
 
Signature · 1 line
2.32.0
#15 lore raw

Re: [RFC PATCH v3 3/8] leds: trigger: netdev: drop NETDEV_LED_MODE_LINKUP from mode

Quoted 3 lines · On Tue, 9 Nov 2021 03:26:03 +0100 Ansuel Smith <[email protected]> wrote:

Drop NETDEV_LED_MODE_LINKUP from mode list and convert to a simple bool that will be true or false based on the carrier link. No functional change intended.

The last time I tried this, I did it for all the fields that are now in the bitmap, and I was told that the bitmap guarantees atomic access, so it should be used...

But why do you needs this? I guess I will see in another patch.

Marek

author maintainer
#16 lore raw

Re: [RFC PATCH v3 3/8] leds: trigger: netdev: drop NETDEV_LED_MODE_LINKUP from mode

Quoted 7 lines · Marek Behún
Quoted 3 lines · On Tue, 9 Nov 2021 03:26:03 +0100 Ansuel Smith <[email protected]> wrote:

Drop NETDEV_LED_MODE_LINKUP from mode list and convert to a simple bool that will be true or false based on the carrier link. No functional change intended.

Quoted 6 lines

The last time I tried this, I did it for all the fields that are now in the bitmap, and I was told that the bitmap guarantees atomic access, so it should be used...

But why do you needs this? I guess I will see in another patch.

The link_up seems something internal to the netdev trigger and not something strictly related to the blink modes. Why put a status in the mode variable?

Quoted 1 line

Marek

Signature · 1 line
	Ansuel
maintainer
#17 lore raw

Re: [RFC PATCH v3 3/8] leds: trigger: netdev: drop NETDEV_LED_MODE_LINKUP from mode

In reply to Marek Behún
Quoted 7 lines · Marek Behún
Quoted 3 lines · On Tue, 9 Nov 2021 03:26:03 +0100 Ansuel Smith <[email protected]> wrote:

Drop NETDEV_LED_MODE_LINKUP from mode list and convert to a simple bool that will be true or false based on the carrier link. No functional change intended.

Quoted 5 lines

The last time I tried this, I did it for all the fields that are now in the bitmap, and I was told that the bitmap guarantees atomic access, so it should be used...

But why do you needs this? I guess I will see in another patch.

I agree with Marek here. The commit message says what you have done, which is not very useful, i can read the patch. What it should include is why you have made this change. The why is very important in the commit message.

Andrew

author maintainer
#18 lore raw

RFC PATCH v3 4/8 leds: trigger: netdev: rename and expose NETDEV trigger enum modes

In reply to Ansuel Smith

Rename NETDEV trigger enum modes to a more simbolic name and move them in leds.h to make them accessible by any user.

---
 drivers/leds/trigger/ledtrig-netdev.c | 31 ++++++++++++---------------
 include/linux/leds.h                  |  7 ++++++
 2 files changed, 21 insertions(+), 17 deletions(-)
drivers/leds/trigger/ledtrig-netdev.c +14 -17
index 66a81cc9b64d..0a3c0b54dbb9 100644
@@ -51,9 +51,6 @@ struct led_netdev_data {
 
 	unsigned long mode;
 	bool carrier_link_up;
-#define NETDEV_LED_LINK	0
-#define NETDEV_LED_TX	1
-#define NETDEV_LED_RX	2
 };
 
 enum netdev_led_attr {
@@ -76,7 +73,7 @@ static void set_baseline_state(struct led_netdev_data *trigger_data)
 	if (!trigger_data->carrier_link_up) {
 		led_set_brightness(led_cdev, LED_OFF);
 	} else {
-		if (test_bit(NETDEV_LED_LINK, &trigger_data->mode))
+		if (test_bit(TRIGGER_NETDEV_LINK, &trigger_data->mode))
 			led_set_brightness(led_cdev,
 					   led_cdev->blink_brightness);
 		else
@@ -85,8 +82,8 @@ static void set_baseline_state(struct led_netdev_data *trigger_data)
 		/* If we are looking for RX/TX start periodically
 		 * checking stats
 		 */
-		if (test_bit(NETDEV_LED_TX, &trigger_data->mode) ||
-		    test_bit(NETDEV_LED_RX, &trigger_data->mode))
+		if (test_bit(TRIGGER_NETDEV_TX, &trigger_data->mode) ||
+		    test_bit(TRIGGER_NETDEV_RX, &trigger_data->mode))
 			schedule_delayed_work(&trigger_data->work, 0);
 	}
 }
@@ -153,13 +150,13 @@ static ssize_t netdev_led_attr_show(struct device *dev, char *buf,
 
 	switch (attr) {
 	case NETDEV_ATTR_LINK:
-		bit = NETDEV_LED_LINK;
+		bit = TRIGGER_NETDEV_LINK;
 		break;
 	case NETDEV_ATTR_TX:
-		bit = NETDEV_LED_TX;
+		bit = TRIGGER_NETDEV_TX;
 		break;
 	case NETDEV_ATTR_RX:
-		bit = NETDEV_LED_RX;
+		bit = TRIGGER_NETDEV_RX;
 		break;
 	default:
 		return -EINVAL;
@@ -182,13 +179,13 @@ static ssize_t netdev_led_attr_store(struct device *dev, const char *buf,
 
 	switch (attr) {
 	case NETDEV_ATTR_LINK:
-		bit = NETDEV_LED_LINK;
+		bit = TRIGGER_NETDEV_LINK;
 		break;
 	case NETDEV_ATTR_TX:
-		bit = NETDEV_LED_TX;
+		bit = TRIGGER_NETDEV_TX;
 		break;
 	case NETDEV_ATTR_RX:
-		bit = NETDEV_LED_RX;
+		bit = TRIGGER_NETDEV_RX;
 		break;
 	default:
 		return -EINVAL;
@@ -358,21 +355,21 @@ static void netdev_trig_work(struct work_struct *work)
 	}
 
 	/* If we are not looking for RX/TX then return  */
-	if (!test_bit(NETDEV_LED_TX, &trigger_data->mode) &&
-	    !test_bit(NETDEV_LED_RX, &trigger_data->mode))
+	if (!test_bit(TRIGGER_NETDEV_TX, &trigger_data->mode) &&
+	    !test_bit(TRIGGER_NETDEV_RX, &trigger_data->mode))
 		return;
 
 	dev_stats = dev_get_stats(trigger_data->net_dev, &temp);
 	new_activity =
-	    (test_bit(NETDEV_LED_TX, &trigger_data->mode) ?
+	    (test_bit(TRIGGER_NETDEV_TX, &trigger_data->mode) ?
 		dev_stats->tx_packets : 0) +
-	    (test_bit(NETDEV_LED_RX, &trigger_data->mode) ?
+	    (test_bit(TRIGGER_NETDEV_RX, &trigger_data->mode) ?
 		dev_stats->rx_packets : 0);
 
 	if (trigger_data->last_activity != new_activity) {
 		led_stop_software_blink(trigger_data->led_cdev);
 
-		invert = test_bit(NETDEV_LED_LINK, &trigger_data->mode);
+		invert = test_bit(TRIGGER_NETDEV_LINK, &trigger_data->mode);
 		interval = jiffies_to_msecs(
 				atomic_read(&trigger_data->interval));
 		/* base state is ON (link present) */
include/linux/leds.h +7 -0
index 00bc4d6ed7ca..087debe6716d 100644
@@ -548,6 +548,13 @@ static inline void *led_get_trigger_data(struct led_classdev *led_cdev)
 
 #endif /* CONFIG_LEDS_TRIGGERS */
 
+/* Trigger specific enum */
+enum led_trigger_netdev_modes {
+	TRIGGER_NETDEV_LINK,
+	TRIGGER_NETDEV_TX,
+	TRIGGER_NETDEV_RX,
+};
+
 /* Trigger specific functions */
 #ifdef CONFIG_LEDS_TRIGGER_DISK
 void ledtrig_disk_activity(bool write);
Signature · 1 line
2.32.0
maintainer
#19 lore raw

Re: [RFC PATCH v3 4/8] leds: trigger: netdev: rename and expose NETDEV trigger enum modes

Quoted 1 line · Ansuel Smith

Rename NETDEV trigger enum modes to a more simbolic name and move them

symbolic. Randy is slipping :-)

Quoted 1 line

in leds.h to make them accessible by any user.

any user? I would be more specific than that. Other triggers dealing with netdev states?

include/linux/leds.h static inline void *led_get_trigger_data(struct led_classdev *led_cdev) · line 551

Quoted 6 lines

+++ b/include/linux/leds.h

include/linux/leds.h +1 -0
@@ -548,6 +548,13 @@ static inline void *led_get_trigger_data(struct led_classdev *led_cdev)
 
 #endif /* CONFIG_LEDS_TRIGGERS */
 
+/* Trigger specific enum */

You probably want netdev in the comment above. Things could get interesting if other ledtrig-*.c started using them.

Quoted 12 lines
include/linux/leds.h +6 -0
+enum led_trigger_netdev_modes {
+	TRIGGER_NETDEV_LINK,
+	TRIGGER_NETDEV_TX,
+	TRIGGER_NETDEV_RX,
+};
+
 /* Trigger specific functions */
 #ifdef CONFIG_LEDS_TRIGGER_DISK
 void ledtrig_disk_activity(bool write);
Signature · 1 line
2.32.0
author maintainer
#20 lore raw

Re: [RFC PATCH v3 4/8] leds: trigger: netdev: rename and expose NETDEV trigger enum modes

Quoted 10 lines · Andrew Lunn
Quoted 1 line · Ansuel Smith

Rename NETDEV trigger enum modes to a more simbolic name and move them

symbolic. Randy is slipping :-)

Quoted 1 line

in leds.h to make them accessible by any user.

any user? I would be more specific than that. Other triggers dealing with netdev states?

Ok will be more specific. A LED driver require to explicitly support the trigger to run in hardware mode. The LED driver will take the trigger_data and elaborate his struct to parse all the option (blink_mode bitmap, interval)

So the user would be a LED driver that adds support for that specific trigger. That is also the reason we need to export them.

Quoted 22 lines

include/linux/leds.h static inline void *led_get_trigger_data(struct led_classdev *led_cdev) · line 551

Quoted 6 lines

+++ b/include/linux/leds.h

include/linux/leds.h +1 -0
@@ -548,6 +548,13 @@ static inline void *led_get_trigger_data(struct led_classdev *led_cdev)
 
 #endif /* CONFIG_LEDS_TRIGGERS */
 
+/* Trigger specific enum */

You probably want netdev in the comment above. Things could get interesting if other ledtrig-*.c started using them.

Quoted 12 lines
include/linux/leds.h +6 -0
+enum led_trigger_netdev_modes {
+	TRIGGER_NETDEV_LINK,
+	TRIGGER_NETDEV_TX,
+	TRIGGER_NETDEV_RX,
+};
+
 /* Trigger specific functions */
 #ifdef CONFIG_LEDS_TRIGGER_DISK
 void ledtrig_disk_activity(bool write);
Signature · 1 line
2.32.0
Signature · 1 line
	Ansuel
maintainer
#21 lore raw

Re: [RFC PATCH v3 4/8] leds: trigger: netdev: rename and expose NETDEV trigger enum modes

Quoted 12 lines · Ansuel Smith
Quoted 10 lines · Andrew Lunn
Quoted 1 line · Ansuel Smith

Rename NETDEV trigger enum modes to a more simbolic name and move them

symbolic. Randy is slipping :-)

Quoted 1 line

in leds.h to make them accessible by any user.

any user? I would be more specific than that. Other triggers dealing with netdev states?

Quoted 7 lines

Ok will be more specific. A LED driver require to explicitly support the trigger to run in hardware mode. The LED driver will take the trigger_data and elaborate his struct to parse all the option (blink_mode bitmap, interval)

So the user would be a LED driver that adds support for that specific trigger. That is also the reason we need to export them.

Say i have a SATA controller where i can configure it to blink on reads, or writes, or both. It also needs to understand these netdev modes? I was meaning you need to narrow the comment down to a trigger which has something to do with netdev. I assume other sorts of hardware offloads will appear in the future, once the generic infrastructure is there.

Andrew

author maintainer
#22 lore raw

RFC PATCH v3 5/8 leds: trigger: netdev: add hardware control support

In reply to Ansuel Smith

Add hardware control support for the Netdev trigger.
The trigger on config change will check if the requested trigger can set to blink mode using LED hardware mode and if every blink mode is supported, the trigger will enable hardware mode with the requested configuration. If there is at least one trigger that is not supported and can't run in hardware mode, then software mode will be used instead.

---
 drivers/leds/trigger/ledtrig-netdev.c | 61 ++++++++++++++++++++++++++-
 1 file changed, 60 insertions(+), 1 deletion(-)
drivers/leds/trigger/ledtrig-netdev.c +60 -1
index 0a3c0b54dbb9..28d9def2fbd0 100644
@@ -37,6 +37,7 @@
  */
 
 struct led_netdev_data {
+	bool hw_mode_supported;
 	spinlock_t lock;
 
 	struct delayed_work work;
@@ -61,9 +62,52 @@ enum netdev_led_attr {
 
 static void set_baseline_state(struct led_netdev_data *trigger_data)
 {
+	bool can_offload;
 	int current_brightness;
+	u32 hw_blink_mode_supported;
 	struct led_classdev *led_cdev = trigger_data->led_cdev;
 
+	if (trigger_data->hw_mode_supported) {
+		if (test_bit(TRIGGER_NETDEV_LINK, &trigger_data->mode) &&
+		    led_trigger_blink_mode_is_supported(led_cdev, TRIGGER_NETDEV_LINK))
+			hw_blink_mode_supported |= TRIGGER_NETDEV_LINK;
+		if (test_bit(TRIGGER_NETDEV_TX, &trigger_data->mode) &&
+		    led_trigger_blink_mode_is_supported(led_cdev, TRIGGER_NETDEV_TX))
+			hw_blink_mode_supported |= TRIGGER_NETDEV_TX;
+		if (test_bit(TRIGGER_NETDEV_RX, &trigger_data->mode) &&
+		    led_trigger_blink_mode_is_supported(led_cdev, TRIGGER_NETDEV_RX))
+			hw_blink_mode_supported |= TRIGGER_NETDEV_RX;
+
+		/* All the requested blink mode can be triggered by hardware.
+		 * Put it in hardware mode.
+		 */
+		if (hw_blink_mode_supported == trigger_data->mode)
+			can_offload = true;
+
+		if (can_offload) {
+			/* We are refreshing the blink modes. Reset them */
+			led_cdev->hw_control_configure(led_cdev, TRIGGER_NETDEV_LINK,
+						       BLINK_MODE_ZERO);
+
+			if (test_bit(TRIGGER_NETDEV_LINK, &trigger_data->mode))
+				led_cdev->hw_control_configure(led_cdev, TRIGGER_NETDEV_LINK,
+							       BLINK_MODE_ENABLE);
+			if (test_bit(TRIGGER_NETDEV_TX, &trigger_data->mode))
+				led_cdev->hw_control_configure(led_cdev, TRIGGER_NETDEV_TX,
+							       BLINK_MODE_ENABLE);
+			if (test_bit(TRIGGER_NETDEV_RX, &trigger_data->mode))
+				led_cdev->hw_control_configure(led_cdev, TRIGGER_NETDEV_RX,
+							       BLINK_MODE_ENABLE);
+			led_cdev->hw_control_start(led_cdev);
+
+			return;
+		}
+	}
+
+	/* If LED supports only hardware mode then skip any software handling */
+	if (led_cdev->blink_mode == HARDWARE_CONTROLLED)
+		return;
+
 	current_brightness = led_cdev->brightness;
 	if (current_brightness)
 		led_cdev->blink_brightness = current_brightness;
@@ -407,13 +451,27 @@ static int netdev_trig_activate(struct led_classdev *led_cdev)
 	trigger_data->mode = 0;
 	atomic_set(&trigger_data->interval, msecs_to_jiffies(50));
 	trigger_data->last_activity = 0;
+	if (led_cdev->blink_mode != SOFTWARE_CONTROLLED) {
+		trigger_data->hw_mode_supported = true;
+
+		/* With hw mode enabled reset any rule set by default */
+		if (led_cdev->hw_control_status(led_cdev)) {
+			rc = led_cdev->hw_control_configure(led_cdev, TRIGGER_NETDEV_LINK,
+							    BLINK_MODE_ZERO);
+			if (rc)
+				goto err;
+		}
+	}
 
 	led_set_trigger_data(led_cdev, trigger_data);
 
 	rc = register_netdevice_notifier(&trigger_data->notifier);
 	if (rc)
-		kfree(trigger_data);
+		goto err;
 
+	return 0;
+err:
+	kfree(trigger_data);
 	return rc;
 }
 
@@ -433,6 +491,7 @@ static void netdev_trig_deactivate(struct led_classdev *led_cdev)
 
 static struct led_trigger netdev_led_trigger = {
 	.name = "netdev",
+	.supported_blink_modes = SOFTWARE_HARDWARE,
 	.activate = netdev_trig_activate,
 	.deactivate = netdev_trig_deactivate,
 	.groups = netdev_trig_groups,
Signature · 1 line
2.32.0
#23 lore raw

Re: [RFC PATCH v3 5/8] leds: trigger: netdev: add hardware control support

Quoted 55 lines · On Tue, 9 Nov 2021 03:26:05 +0100 Ansuel Smith <[email protected]> wrote:

Add hardware control support for the Netdev trigger.
The trigger on config change will check if the requested trigger can set to blink mode using LED hardware mode and if every blink mode is supported, the trigger will enable hardware mode with the requested configuration. If there is at least one trigger that is not supported and can't run in hardware mode, then software mode will be used instead.

---
 drivers/leds/trigger/ledtrig-netdev.c | 61 ++++++++++++++++++++++++++-
 1 file changed, 60 insertions(+), 1 deletion(-)
drivers/leds/trigger/ledtrig-netdev.c +25 -0
index 0a3c0b54dbb9..28d9def2fbd0 100644
@@ -37,6 +37,7 @@
  */
 
 struct led_netdev_data {
+	bool hw_mode_supported;
 	spinlock_t lock;
 
 	struct delayed_work work;
@@ -61,9 +62,52 @@ enum netdev_led_attr {
 
 static void set_baseline_state(struct led_netdev_data *trigger_data)
 {
+	bool can_offload;
 	int current_brightness;
+	u32 hw_blink_mode_supported;
 	struct led_classdev *led_cdev = trigger_data->led_cdev;
 
+	if (trigger_data->hw_mode_supported) {
+		if (test_bit(TRIGGER_NETDEV_LINK, &trigger_data->mode) &&
+		    led_trigger_blink_mode_is_supported(led_cdev, TRIGGER_NETDEV_LINK))
+			hw_blink_mode_supported |= TRIGGER_NETDEV_LINK;
+		if (test_bit(TRIGGER_NETDEV_TX, &trigger_data->mode) &&
+		    led_trigger_blink_mode_is_supported(led_cdev, TRIGGER_NETDEV_TX))
+			hw_blink_mode_supported |= TRIGGER_NETDEV_TX;
+		if (test_bit(TRIGGER_NETDEV_RX, &trigger_data->mode) &&
+		    led_trigger_blink_mode_is_supported(led_cdev, TRIGGER_NETDEV_RX))
+			hw_blink_mode_supported |= TRIGGER_NETDEV_RX;
+
+		/* All the requested blink mode can be triggered by hardware.
+		 * Put it in hardware mode.
+		 */
+		if (hw_blink_mode_supported == trigger_data->mode)
+			can_offload = true;
+
+		if (can_offload) {
+			/* We are refreshing the blink modes. Reset them */
+			led_cdev->hw_control_configure(led_cdev, TRIGGER_NETDEV_LINK,
+						       BLINK_MODE_ZERO);
+

drivers/leds/trigger/ledtrig-netdev.c enum netdev_led_attr { · line 101

Quoted 10 lines
drivers/leds/trigger/ledtrig-netdev.c +10 -0
+			if (test_bit(TRIGGER_NETDEV_LINK, &trigger_data->mode))
+				led_cdev->hw_control_configure(led_cdev, TRIGGER_NETDEV_LINK,
+							       BLINK_MODE_ENABLE);
+			if (test_bit(TRIGGER_NETDEV_TX, &trigger_data->mode))
+				led_cdev->hw_control_configure(led_cdev, TRIGGER_NETDEV_TX,
+							       BLINK_MODE_ENABLE);
+			if (test_bit(TRIGGER_NETDEV_RX, &trigger_data->mode))
+				led_cdev->hw_control_configure(led_cdev, TRIGGER_NETDEV_RX,
+							       BLINK_MODE_ENABLE);
+			led_cdev->hw_control_start(led_cdev);

Please nooo :)
This is exactly what I wanted to avoid, this logic should be in the LED driver itself.

Can you please at least read my last proposal?

patch 2
https://lore.kernel.org/linux-leds/[email protected]/
adds the trigger_offload() method. This may need to get changed to trigger_offload_start() and trigger_offload_stop(), as per Andrew's request.

patch 3
https://lore.kernel.org/linux-leds/[email protected]/
moves the whole struct led_netdev_data to global include file include/linux/ledtrig-netdev.h

patch 4
https://lore.kernel.org/linux-leds/[email protected]/
makes netdev trigger to try to call the trigger_offload() method.

So after patch 4, netdev trigger calls trigger_offload() methods and passes itself into it.

Example implementation is then in patch 10 of the series
https://lore.kernel.org/linux-leds/[email protected]/
Look at omnia_led_trig_offload() function.

The benefit of this API is that it is flexible - all existing triggers an be theoretically offloaded to HW (if there is HW that supports it) without change to this API, and with minimal changes to the sw implementations of the triggers.

Could you please at least try it?

I am willing to work with you on this. We can make a conference call tomorrow, if you are able to.

Marek

author maintainer
#24 lore raw

Re: [RFC PATCH v3 5/8] leds: trigger: netdev: add hardware control support

Quoted 64 lines · Marek Behún
Quoted 61 lines · On Tue, 9 Nov 2021 03:26:05 +0100 Ansuel Smith <[email protected]> wrote:

Add hardware control support for the Netdev trigger.
The trigger on config change will check if the requested trigger can set to blink mode using LED hardware mode and if every blink mode is supported, the trigger will enable hardware mode with the requested configuration. If there is at least one trigger that is not supported and can't run in hardware mode, then software mode will be used instead.

---
 drivers/leds/trigger/ledtrig-netdev.c | 61 ++++++++++++++++++++++++++-
 1 file changed, 60 insertions(+), 1 deletion(-)
drivers/leds/trigger/ledtrig-netdev.c +31 -0
index 0a3c0b54dbb9..28d9def2fbd0 100644
@@ -37,6 +37,7 @@
  */
 
 struct led_netdev_data {
+	bool hw_mode_supported;
 	spinlock_t lock;
 
 	struct delayed_work work;
@@ -61,9 +62,52 @@ enum netdev_led_attr {
 
 static void set_baseline_state(struct led_netdev_data *trigger_data)
 {
+	bool can_offload;
 	int current_brightness;
+	u32 hw_blink_mode_supported;
 	struct led_classdev *led_cdev = trigger_data->led_cdev;
 
+	if (trigger_data->hw_mode_supported) {
+		if (test_bit(TRIGGER_NETDEV_LINK, &trigger_data->mode) &&
+		    led_trigger_blink_mode_is_supported(led_cdev, TRIGGER_NETDEV_LINK))
+			hw_blink_mode_supported |= TRIGGER_NETDEV_LINK;
+		if (test_bit(TRIGGER_NETDEV_TX, &trigger_data->mode) &&
+		    led_trigger_blink_mode_is_supported(led_cdev, TRIGGER_NETDEV_TX))
+			hw_blink_mode_supported |= TRIGGER_NETDEV_TX;
+		if (test_bit(TRIGGER_NETDEV_RX, &trigger_data->mode) &&
+		    led_trigger_blink_mode_is_supported(led_cdev, TRIGGER_NETDEV_RX))
+			hw_blink_mode_supported |= TRIGGER_NETDEV_RX;
+
+		/* All the requested blink mode can be triggered by hardware.
+		 * Put it in hardware mode.
+		 */
+		if (hw_blink_mode_supported == trigger_data->mode)
+			can_offload = true;
+
+		if (can_offload) {
+			/* We are refreshing the blink modes. Reset them */
+			led_cdev->hw_control_configure(led_cdev, TRIGGER_NETDEV_LINK,
+						       BLINK_MODE_ZERO);
+
+			if (test_bit(TRIGGER_NETDEV_LINK, &trigger_data->mode))
+				led_cdev->hw_control_configure(led_cdev, TRIGGER_NETDEV_LINK,
+							       BLINK_MODE_ENABLE);
+			if (test_bit(TRIGGER_NETDEV_TX, &trigger_data->mode))
+				led_cdev->hw_control_configure(led_cdev, TRIGGER_NETDEV_TX,
+							       BLINK_MODE_ENABLE);
Quoted 9 lines

drivers/leds/trigger/ledtrig-netdev.c enum netdev_led_attr { · line 101

Quoted 4 lines
drivers/leds/trigger/ledtrig-netdev.c +4 -0
+			if (test_bit(TRIGGER_NETDEV_RX, &trigger_data->mode))
+				led_cdev->hw_control_configure(led_cdev, TRIGGER_NETDEV_RX,
+							       BLINK_MODE_ENABLE);
+			led_cdev->hw_control_start(led_cdev);

Please nooo :)
This is exactly what I wanted to avoid, this logic should be in the LED driver itself.

Don't know about this. Honestly I think a better way would be to make the LED driver very stupid (current way) and just leave to the trigger how to handle the different case.
My idea is that LED driver should declare feature and do operation and then the trigger should elaborate them.

Quoted 2 lines

Can you please at least read my last proposal?

About this sorry but I lost track of it. Thanks for posting it here.

Quoted 18 lines

patch 2
https://lore.kernel.org/linux-leds/[email protected]/
adds the trigger_offload() method. This may need to get changed to trigger_offload_start() and trigger_offload_stop(), as per Andrew's request.

patch 3
https://lore.kernel.org/linux-leds/[email protected]/
moves the whole struct led_netdev_data to global include file include/linux/ledtrig-netdev.h

patch 4
https://lore.kernel.org/linux-leds/[email protected]/
makes netdev trigger to try to call the trigger_offload() method.

So after patch 4, netdev trigger calls trigger_offload() methods and passes itself into it.

Quoted 8 lines

Example implementation is then in patch 10 of the series
https://lore.kernel.org/linux-leds/[email protected]/
Look at omnia_led_trig_offload() function.

The benefit of this API is that it is flexible - all existing triggers an be theoretically offloaded to HW (if there is HW that supports it) without change to this API, and with minimal changes to the sw implementations of the triggers.

Looking at the patchset the main problem with my implementation is that a LED driver would benefits by having more data as it can also configure other stuff. So I see using the logic of using directly the trigger data instead of some values correct.
What I still don't like is the fact that moving the entire logic to the LED driver doesn't seems correct.
We can mix the 2 logic and create an hybrid.
- I would still have some type of direct control from a trigger. (We reduce the current cmd enum to something like APPLY, ZERO, SUPPORTED) just so the hardware configure part is not a big blackbox that do thing and we doesn't really know what happen on any error.
- On the LED driver part, it will use the data from the trigger and report what it was asked.

That would change the LED trigger transaction to.
1. Set the bitmap in the trigger data
2. Send supported command (the LED driver do his check and return a bool)
3. Send apply command (the LED driver actually apply the config)

Also the trigger will ZERO any blink configuration on init. (to handle LEDs that configure some blink mode by default in hardware mode)

The first question would be why have all these extra modes when we can just use one function like hw_control_setup. Well some other trigger would benefit from having more control and we would get better error handling by separating the main operation (supported and apply)

What do you think? This should be even more flexible than before and would also remove the flag problem (or better saying moving it to exporting the entire trigger struct)

Also about exporting struct. Should we really create another include or should we just put it in the generic led include?

Quoted 3 lines

Could you please at least try it?

If you are fine with the idea I have in mind, I will start working on v4 ASAP.

Quoted 3 lines

I am willing to work with you on this. We can make a conference call tomorrow, if you are able to.

A bit busy so I work on this on very late night but I think the way we are comunicating is fine. Lets continue like that.

Quoted 1 line

Marek

Signature · 1 line
	Ansuel
author maintainer
#25 lore raw

RFC PATCH v3 6/8 leds: trigger: add hardware-phy-activity trigger

In reply to Ansuel Smith

Add Hardware Only Trigger for PHY Activity. This special trigger is used to configure and expose the different HW trigger that are provided by the PHY. Each blink mode can be configured by sysfs and on trigger activation the hardware mode is enabled.

This currently implement these hw triggers:
- blink_tx: Blink LED on tx packet receive
- blink_rx: Blink LED on rx packet receive
- keep_link_10m: Keep LED on with 10m link speed
- keep_link_100m: Keep LED on with 100m link speed
- kepp_link_1000m: Keep LED on with 1000m link speed
- keep_half_duplex: Keep LED on with half duplex link
- keep_full_duplex: Keep LED on with full duplex link
- option_linkup_over: Ignore blink tx/rx with link keep not active
- option_power_on_reset: Keep LED on with switch reset
- option_blink_2hz: Set blink speed at 2hz for every blink event
- option_blink_4hz: Set blink speed at 4hz for every blink event
- option_blink_8hz: Set blink speed at 8hz for every blink event
- option_blink_auto: Set blink speed at 2hz for 10m link speed,

      4hz for 100m and 8hz for 1000m

The trigger will check if the LED driver support the various blink modes and will expose the blink modes in sysfs.
It will finally enable hw mode for the LED without configuring any rule. This mean that the LED will blink/follow whatever offload trigger is active by default and the user needs to manually configure the desired offload triggers using sysfs.
A flag is passed to configure_offload with the related rule from this trigger to active or disable.
It's in the led driver interest the detection and knowing how to elaborate the passed flags and should report -EOPNOTSUPP otherwise.

The different hw triggers are exposed in the led sysfs dir under the offload-phy-activity subdir.

---
 drivers/leds/trigger/Kconfig                  |  28 +++
 drivers/leds/trigger/Makefile                 |   1 +
 .../trigger/ledtrig-hardware-phy-activity.c   | 171 ++++++++++++++++++
 include/linux/leds.h                          |  22 +++
 4 files changed, 222 insertions(+)
 create mode 100644 drivers/leds/trigger/ledtrig-hardware-phy-activity.c
drivers/leds/trigger/Kconfig +28 -0
index dc6816d36d06..b947b238be3f 100644
@@ -154,4 +154,32 @@ config LEDS_TRIGGER_TTY
 
 	  When build as a module this driver will be called ledtrig-tty.
 
+config LEDS_TRIGGER_HARDWARE_PHY_ACTIVITY
+	tristate "LED Trigger for PHY Activity for Hardware Controlled LED"
+	depends on LEDS_HARDWARE_CONTROL
+	help
+	  This allows LEDs to be configured to run by hardware and offloaded
+	  based on some rules. The LED will blink or be on based on the PHY
+	  Activity for example on packet receive or based on the link speed.
+
+	  The current supported offload triggers are:
+	  - blink_tx: Blink LED on tx packet receive
+	  - blink_rx: Blink LED on rx packet receive
+	  - keep_link_10m: Keep LED on with 10m link speed
+	  - keep_link_100m: Keep LED on with 100m link speed
+	  - keep_link_1000m: Keep LED on with 1000m link speed
+	  - keep_half_duplex: Keep LED on with half duplex link
+	  - keep_full_duplex: Keep LED on with full duplex link
+	  - option_linkup_over: Blink rules are ignored with absent link
+	  - option_power_on_reset: Power ON Led on Switch/PHY reset
+	  - option_blink_2hz: Set blink speed at 2hz for every blink event
+	  - option_blink_4hz: Set blink speed at 4hz for every blink event
+	  - option_blink_8hz: Set blink speed at 8hz for every blink event
+
+	  These blink modes are present in the LED sysfs dir under
+	  hardware-phy-activity if supported by the LED driver.
+
+	  This trigger can be used only by LEDs that supports Hardware mode
+
+
 endif # LEDS_TRIGGERS
drivers/leds/trigger/Makefile +1 -0
index 25c4db97cdd4..f5d0d0057d2b 100644
@@ -16,3 +16,4 @@ obj-$(CONFIG_LEDS_TRIGGER_NETDEV)	+= ledtrig-netdev.o
 obj-$(CONFIG_LEDS_TRIGGER_PATTERN)	+= ledtrig-pattern.o
 obj-$(CONFIG_LEDS_TRIGGER_AUDIO)	+= ledtrig-audio.o
 obj-$(CONFIG_LEDS_TRIGGER_TTY)		+= ledtrig-tty.o
+obj-$(CONFIG_LEDS_TRIGGER_HARDWARE_PHY_ACTIVITY) += ledtrig-hardware-phy-activity.o
drivers/leds/trigger/ledtrig-hardware-phy-activity.c new file +171 -0

171 lines not on this page · raw message

include/linux/leds.h +22 -0
index 087debe6716d..3d2e06db6839 100644
@@ -555,6 +555,28 @@ enum led_trigger_netdev_modes {
 	TRIGGER_NETDEV_RX,
 };
 
+#ifdef CONFIG_LEDS_TRIGGER_HARDWARE_PHY_ACTIVITY
+/* 3 main category for offload trigger:
+ * - blink: the led will blink on trigger
+ * - keep: the led will be kept on with condition
+ * - option: a configuration value internal to the led on how offload works
+ */
+enum hardware_phy_activity {
+	BLINK_TX, /* Blink on TX traffic */
+	BLINK_RX, /* Blink on RX traffic */
+	KEEP_LINK_10M, /* LED on with 10M link */
+	KEEP_LINK_100M, /* LED on with 100M link */
+	KEEP_LINK_1000M, /* LED on with 1000M link */
+	KEEP_HALF_DUPLEX, /* LED on with Half-Duplex link */
+	KEEP_FULL_DUPLEX, /* LED on with Full-Duplex link */
+	OPTION_LINKUP_OVER, /* LED will be on with KEEP blink mode and blink on BLINK traffic */
+	OPTION_POWER_ON_RESET, /* LED will be on Switch reset */
+	OPTION_BLINK_2HZ, /* LED will blink with any offload_trigger at 2Hz */
+	OPTION_BLINK_4HZ, /* LED will blink with any offload_trigger at 4Hz */
+	OPTION_BLINK_8HZ, /* LED will blink with any offload_trigger at 8Hz */
+};
+#endif
+
 /* Trigger specific functions */
 #ifdef CONFIG_LEDS_TRIGGER_DISK
 void ledtrig_disk_activity(bool write);
Signature · 1 line
2.32.0
#26 lore raw

Re: [RFC PATCH v3 6/8] leds: trigger: add hardware-phy-activity trigger

Quoted 134 lines · On Tue, 9 Nov 2021 03:26:06 +0100 Ansuel Smith <[email protected]> wrote:

Add Hardware Only Trigger for PHY Activity. This special trigger is used to configure and expose the different HW trigger that are provided by the PHY. Each blink mode can be configured by sysfs and on trigger activation the hardware mode is enabled.

This currently implement these hw triggers:
- blink_tx: Blink LED on tx packet receive
- blink_rx: Blink LED on rx packet receive
- keep_link_10m: Keep LED on with 10m link speed
- keep_link_100m: Keep LED on with 100m link speed
- kepp_link_1000m: Keep LED on with 1000m link speed
- keep_half_duplex: Keep LED on with half duplex link
- keep_full_duplex: Keep LED on with full duplex link
- option_linkup_over: Ignore blink tx/rx with link keep not active
- option_power_on_reset: Keep LED on with switch reset
- option_blink_2hz: Set blink speed at 2hz for every blink event
- option_blink_4hz: Set blink speed at 4hz for every blink event
- option_blink_8hz: Set blink speed at 8hz for every blink event
- option_blink_auto: Set blink speed at 2hz for 10m link speed,

      4hz for 100m and 8hz for 1000m

The trigger will check if the LED driver support the various blink modes and will expose the blink modes in sysfs.
It will finally enable hw mode for the LED without configuring any rule. This mean that the LED will blink/follow whatever offload trigger is active by default and the user needs to manually configure the desired offload triggers using sysfs.
A flag is passed to configure_offload with the related rule from this trigger to active or disable.
It's in the led driver interest the detection and knowing how to elaborate the passed flags and should report -EOPNOTSUPP otherwise.

The different hw triggers are exposed in the led sysfs dir under the offload-phy-activity subdir.

---
 drivers/leds/trigger/Kconfig                  |  28 +++
 drivers/leds/trigger/Makefile                 |   1 +
 .../trigger/ledtrig-hardware-phy-activity.c   | 171 ++++++++++++++++++
 include/linux/leds.h                          |  22 +++
 4 files changed, 222 insertions(+)
 create mode 100644 drivers/leds/trigger/ledtrig-hardware-phy-activity.c
drivers/leds/trigger/Kconfig +28 -0
index dc6816d36d06..b947b238be3f 100644
@@ -154,4 +154,32 @@ config LEDS_TRIGGER_TTY
 
 	  When build as a module this driver will be called ledtrig-tty.
 
+config LEDS_TRIGGER_HARDWARE_PHY_ACTIVITY
+	tristate "LED Trigger for PHY Activity for Hardware Controlled LED"
+	depends on LEDS_HARDWARE_CONTROL
+	help
+	  This allows LEDs to be configured to run by hardware and offloaded
+	  based on some rules. The LED will blink or be on based on the PHY
+	  Activity for example on packet receive or based on the link speed.
+
+	  The current supported offload triggers are:
+	  - blink_tx: Blink LED on tx packet receive
+	  - blink_rx: Blink LED on rx packet receive
+	  - keep_link_10m: Keep LED on with 10m link speed
+	  - keep_link_100m: Keep LED on with 100m link speed
+	  - keep_link_1000m: Keep LED on with 1000m link speed
+	  - keep_half_duplex: Keep LED on with half duplex link
+	  - keep_full_duplex: Keep LED on with full duplex link
+	  - option_linkup_over: Blink rules are ignored with absent link
+	  - option_power_on_reset: Power ON Led on Switch/PHY reset
+	  - option_blink_2hz: Set blink speed at 2hz for every blink event
+	  - option_blink_4hz: Set blink speed at 4hz for every blink event
+	  - option_blink_8hz: Set blink speed at 8hz for every blink event
+
+	  These blink modes are present in the LED sysfs dir under
+	  hardware-phy-activity if supported by the LED driver.
+
+	  This trigger can be used only by LEDs that supports Hardware mode
+
+
 endif # LEDS_TRIGGERS
drivers/leds/trigger/Makefile +1 -0
index 25c4db97cdd4..f5d0d0057d2b 100644
@@ -16,3 +16,4 @@ obj-$(CONFIG_LEDS_TRIGGER_NETDEV)	+= ledtrig-netdev.o
 obj-$(CONFIG_LEDS_TRIGGER_PATTERN)	+= ledtrig-pattern.o
 obj-$(CONFIG_LEDS_TRIGGER_AUDIO)	+= ledtrig-audio.o
 obj-$(CONFIG_LEDS_TRIGGER_TTY)		+= ledtrig-tty.o
+obj-$(CONFIG_LEDS_TRIGGER_HARDWARE_PHY_ACTIVITY) += ledtrig-hardware-phy-activity.o
drivers/leds/trigger/ledtrig-hardware-phy-activity.c new file +38 -0
new file mode 100644
index 000000000000..7fd0557fe878
@@ -0,0 +1,171 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/leds.h>
+#include <linux/slab.h>
+
+#define PHY_ACTIVITY_MAX_TRIGGERS 12
+
+#define DEFINE_OFFLOAD_TRIGGER(trigger_name, trigger) \
+	static ssize_t trigger_name##_show(struct device *dev, \
+				struct device_attribute *attr, char *buf) \
+	{ \
+		struct led_classdev *led_cdev = led_trigger_get_led(dev); \
+		int val; \
+		val = led_cdev->hw_control_configure(led_cdev, trigger, BLINK_MODE_READ); \
+		return sprintf(buf, "%d\n", val ? 1 : 0); \
+	} \
+	static ssize_t trigger_name##_store(struct device *dev, \
+					struct device_attribute *attr, \
+					const char *buf, size_t size) \
+	{ \
+		struct led_classdev *led_cdev = led_trigger_get_led(dev); \
+		unsigned long state; \
+		int cmd, ret; \
+		ret = kstrtoul(buf, 0, &state); \
+		if (ret) \
+			return ret; \
+		cmd = !!state ? BLINK_MODE_ENABLE : BLINK_MODE_DISABLE; \
+		/* Update the configuration with every change */ \
+		led_cdev->hw_control_configure(led_cdev, trigger, cmd); \
+		return size; \
+	} \
+	DEVICE_ATTR_RW(trigger_name)
+
+/* Expose sysfs for every blink to be configurable from userspace */
+DEFINE_OFFLOAD_TRIGGER(blink_tx, BLINK_TX);
+DEFINE_OFFLOAD_TRIGGER(blink_rx, BLINK_RX);

drivers/leds/trigger/ledtrig-hardware-phy-activity.c line 48

Quoted 10 lines
drivers/leds/trigger/ledtrig-hardware-phy-activity.c new file +10 -0
+DEFINE_OFFLOAD_TRIGGER(keep_link_10m, KEEP_LINK_10M);
+DEFINE_OFFLOAD_TRIGGER(keep_link_100m, KEEP_LINK_100M);
+DEFINE_OFFLOAD_TRIGGER(keep_link_1000m, KEEP_LINK_1000M);
+DEFINE_OFFLOAD_TRIGGER(keep_half_duplex, KEEP_HALF_DUPLEX);
+DEFINE_OFFLOAD_TRIGGER(keep_full_duplex, KEEP_FULL_DUPLEX);
+DEFINE_OFFLOAD_TRIGGER(option_linkup_over, OPTION_LINKUP_OVER);
+DEFINE_OFFLOAD_TRIGGER(option_power_on_reset, OPTION_POWER_ON_RESET);
+DEFINE_OFFLOAD_TRIGGER(option_blink_2hz, OPTION_BLINK_2HZ);
+DEFINE_OFFLOAD_TRIGGER(option_blink_4hz, OPTION_BLINK_4HZ);
+DEFINE_OFFLOAD_TRIGGER(option_blink_8hz, OPTION_BLINK_8HZ);

This is very strange. Is option_blink_2hz a trigger on itself? Or just an option for another trigger? It seems that it is an option, so that I can set something like
blink_tx,option_blink_2hz
and the LED will blink on tx activity with frequency 2 Hz... If that is so, I think you are misnaming your macros or something, since you are defining option_blink_2hz as a trigger with
DEFINE_OFFLOAD_TRIGGER

Marek

maintainer
#27 lore raw

Re: [RFC PATCH v3 6/8] leds: trigger: add hardware-phy-activity trigger

Quoted 6 lines

drivers/leds/trigger/ledtrig-hardware-phy-activity.c line 41

Quoted 6 lines
drivers/leds/trigger/ledtrig-hardware-phy-activity.c new file +6 -0
+/* Expose sysfs for every blink to be configurable from userspace */
+DEFINE_OFFLOAD_TRIGGER(blink_tx, BLINK_TX);
+DEFINE_OFFLOAD_TRIGGER(blink_rx, BLINK_RX);
+DEFINE_OFFLOAD_TRIGGER(keep_link_10m, KEEP_LINK_10M);
+DEFINE_OFFLOAD_TRIGGER(keep_link_100m, KEEP_LINK_100M);
+DEFINE_OFFLOAD_TRIGGER(keep_link_1000m, KEEP_LINK_1000M);

You might get warnings about CamelCase, but i suggest keep_link_10M, keep_link_100M and keep_link_1000M. These are megabits, not millibits.

Quoted 2 lines

drivers/leds/trigger/ledtrig-hardware-phy-activity.c line 43

Quoted 2 lines
drivers/leds/trigger/ledtrig-hardware-phy-activity.c new file +2 -0
+DEFINE_OFFLOAD_TRIGGER(keep_half_duplex, KEEP_HALF_DUPLEX);
+DEFINE_OFFLOAD_TRIGGER(keep_full_duplex, KEEP_FULL_DUPLEX);

What does keep mean in this context?

Quoted 6 lines

drivers/leds/trigger/ledtrig-hardware-phy-activity.c line 48

Quoted 5 lines
drivers/leds/trigger/ledtrig-hardware-phy-activity.c new file +5 -0
+DEFINE_OFFLOAD_TRIGGER(option_linkup_over, OPTION_LINKUP_OVER);
+DEFINE_OFFLOAD_TRIGGER(option_power_on_reset, OPTION_POWER_ON_RESET);
+DEFINE_OFFLOAD_TRIGGER(option_blink_2hz, OPTION_BLINK_2HZ);
+DEFINE_OFFLOAD_TRIGGER(option_blink_4hz, OPTION_BLINK_4HZ);
+DEFINE_OFFLOAD_TRIGGER(option_blink_8hz, OPTION_BLINK_8HZ);
Quoted 8 lines

This is very strange. Is option_blink_2hz a trigger on itself? Or just an option for another trigger? It seems that it is an option, so that I can set something like
blink_tx,option_blink_2hz
and the LED will blink on tx activity with frequency 2 Hz... If that is so, I think you are misnaming your macros or something, since you are defining option_blink_2hz as a trigger with
DEFINE_OFFLOAD_TRIGGER

Yes, i already said this needs handling differently. The 2Hz, 4Hz and 8Hz naturally fit the delay_on, delay_of sysfs attributes.

Andrew

author maintainer
#28 lore raw

Re: [RFC PATCH v3 6/8] leds: trigger: add hardware-phy-activity trigger

Quoted 10 lines · Andrew Lunn
Quoted 6 lines

drivers/leds/trigger/ledtrig-hardware-phy-activity.c line 41

Quoted 6 lines
drivers/leds/trigger/ledtrig-hardware-phy-activity.c new file +6 -0
+/* Expose sysfs for every blink to be configurable from userspace */
+DEFINE_OFFLOAD_TRIGGER(blink_tx, BLINK_TX);
+DEFINE_OFFLOAD_TRIGGER(blink_rx, BLINK_RX);
+DEFINE_OFFLOAD_TRIGGER(keep_link_10m, KEEP_LINK_10M);
+DEFINE_OFFLOAD_TRIGGER(keep_link_100m, KEEP_LINK_100M);
+DEFINE_OFFLOAD_TRIGGER(keep_link_1000m, KEEP_LINK_1000M);

You might get warnings about CamelCase, but i suggest keep_link_10M, keep_link_100M and keep_link_1000M. These are megabits, not millibits.

Quoted 5 lines
Quoted 2 lines

drivers/leds/trigger/ledtrig-hardware-phy-activity.c line 43

Quoted 2 lines
drivers/leds/trigger/ledtrig-hardware-phy-activity.c new file +2 -0
+DEFINE_OFFLOAD_TRIGGER(keep_half_duplex, KEEP_HALF_DUPLEX);
+DEFINE_OFFLOAD_TRIGGER(keep_full_duplex, KEEP_FULL_DUPLEX);

What does keep mean in this context?

LED is turned on but doesn't blink. Hint for a better name?

Quoted 15 lines
Quoted 6 lines

drivers/leds/trigger/ledtrig-hardware-phy-activity.c line 48

Quoted 5 lines
drivers/leds/trigger/ledtrig-hardware-phy-activity.c new file +5 -0
+DEFINE_OFFLOAD_TRIGGER(option_linkup_over, OPTION_LINKUP_OVER);
+DEFINE_OFFLOAD_TRIGGER(option_power_on_reset, OPTION_POWER_ON_RESET);
+DEFINE_OFFLOAD_TRIGGER(option_blink_2hz, OPTION_BLINK_2HZ);
+DEFINE_OFFLOAD_TRIGGER(option_blink_4hz, OPTION_BLINK_4HZ);
+DEFINE_OFFLOAD_TRIGGER(option_blink_8hz, OPTION_BLINK_8HZ);
Quoted 8 lines

This is very strange. Is option_blink_2hz a trigger on itself? Or just an option for another trigger? It seems that it is an option, so that I can set something like
blink_tx,option_blink_2hz
and the LED will blink on tx activity with frequency 2 Hz... If that is so, I think you are misnaming your macros or something, since you are defining option_blink_2hz as a trigger with
DEFINE_OFFLOAD_TRIGGER

Quoted 4 lines

Yes, i already said this needs handling differently. The 2Hz, 4Hz and 8Hz naturally fit the delay_on, delay_of sysfs attributes.

Andrew

You are totally right. I tought the blink control was something specific that only some PHY had, but considering we have more than 1 that supports a variant of it, I can see how it should be handled separately. I assume even more have this kind of customizzation.

Signature · 1 line
	Ansuel
maintainer
#29 lore raw

Re: [RFC PATCH v3 6/8] leds: trigger: add hardware-phy-activity trigger

Quoted 11 lines · Ansuel Smith
Quoted 10 lines · Andrew Lunn
Quoted 6 lines

drivers/leds/trigger/ledtrig-hardware-phy-activity.c line 41

Quoted 6 lines
drivers/leds/trigger/ledtrig-hardware-phy-activity.c new file +6 -0
+/* Expose sysfs for every blink to be configurable from userspace */
+DEFINE_OFFLOAD_TRIGGER(blink_tx, BLINK_TX);
+DEFINE_OFFLOAD_TRIGGER(blink_rx, BLINK_RX);
+DEFINE_OFFLOAD_TRIGGER(keep_link_10m, KEEP_LINK_10M);
+DEFINE_OFFLOAD_TRIGGER(keep_link_100m, KEEP_LINK_100M);
+DEFINE_OFFLOAD_TRIGGER(keep_link_1000m, KEEP_LINK_1000M);

You might get warnings about CamelCase, but i suggest keep_link_10M, keep_link_100M and keep_link_1000M. These are megabits, not millibits.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

drivers/leds/trigger/ledtrig-hardware-phy-activity.c line 43

Quoted 2 lines
drivers/leds/trigger/ledtrig-hardware-phy-activity.c new file +2 -0
+DEFINE_OFFLOAD_TRIGGER(keep_half_duplex, KEEP_HALF_DUPLEX);
+DEFINE_OFFLOAD_TRIGGER(keep_full_duplex, KEEP_FULL_DUPLEX);

What does keep mean in this context?

LED is turned on but doesn't blink. Hint for a better name?

I would just drop the keep. You have blink_ as a prefix for those modes that blink.

Andrew

reviewer
#30 lore raw

Re: [RFC PATCH v3 6/8] leds: trigger: add hardware-phy-activity trigger

In reply to Ansuel Smith
Quoted 5 lines · Ansuel Smith
drivers/leds/trigger/Kconfig +0 -0
index dc6816d36d06..b947b238be3f 100644

drivers/leds/trigger/Kconfig config LEDS_TRIGGER_TTY · line 162

Quoted 9 lines
drivers/leds/trigger/Kconfig +6 -0
@@ -154,4 +154,32 @@ config LEDS_TRIGGER_TTY
 
  	  When build as a module this driver will be called ledtrig-tty.
 
+config LEDS_TRIGGER_HARDWARE_PHY_ACTIVITY
+	tristate "LED Trigger for PHY Activity for Hardware Controlled LED"
+	depends on LEDS_HARDWARE_CONTROL
+	help
+	  This allows LEDs to be configured to run by hardware and offloaded
+	  based on some rules. The LED will blink or be on based on the PHY

or be "on" based on the PHY

drivers/leds/trigger/Kconfig config LEDS_TRIGGER_TTY · line 163

Quoted 1 line
drivers/leds/trigger/Kconfig +1 -0
+	  Activity for example on packet receive or based on the link speed.

activity

Quoted 9 lines
drivers/leds/trigger/Kconfig +9 -0
+
+	  The current supported offload triggers are:
+	  - blink_tx: Blink LED on tx packet receive
+	  - blink_rx: Blink LED on rx packet receive
+	  - keep_link_10m: Keep LED on with 10m link speed
+	  - keep_link_100m: Keep LED on with 100m link speed
+	  - keep_link_1000m: Keep LED on with 1000m link speed
+	  - keep_half_duplex: Keep LED on with half duplex link
+	  - keep_full_duplex: Keep LED on with full duplex link

drivers/leds/trigger/Kconfig config LEDS_TRIGGER_TTY · line 182

Quoted 10 lines
drivers/leds/trigger/Kconfig +10 -0
+	  - option_linkup_over: Blink rules are ignored with absent link
+	  - option_power_on_reset: Power ON Led on Switch/PHY reset
+	  - option_blink_2hz: Set blink speed at 2hz for every blink event
+	  - option_blink_4hz: Set blink speed at 4hz for every blink event
+	  - option_blink_8hz: Set blink speed at 8hz for every blink event
+
+	  These blink modes are present in the LED sysfs dir under
+	  hardware-phy-activity if supported by the LED driver.
+
+	  This trigger can be used only by LEDs that supports Hardware mode

support Hardware mode.

Ansuel, do you read and consider these comments?
It's difficult to tell if you do or not.

thanks.

Signature · 1 line
~Randy
author maintainer
#31 lore raw

Re: [RFC PATCH v3 6/8] leds: trigger: add hardware-phy-activity trigger

Quoted 43 lines · Randy Dunlap

drivers/leds/trigger/Kconfig config LEDS_TRIGGER_TTY · line 160

Quoted 12 lines · Ansuel Smith
drivers/leds/trigger/Kconfig +6 -0
index dc6816d36d06..b947b238be3f 100644
@@ -154,4 +154,32 @@ config LEDS_TRIGGER_TTY
  	  When build as a module this driver will be called ledtrig-tty.
+config LEDS_TRIGGER_HARDWARE_PHY_ACTIVITY
+	tristate "LED Trigger for PHY Activity for Hardware Controlled LED"
+	depends on LEDS_HARDWARE_CONTROL
+	help
+	  This allows LEDs to be configured to run by hardware and offloaded
+	  based on some rules. The LED will blink or be on based on the PHY

or be "on" based on the PHY

drivers/leds/trigger/Kconfig config LEDS_TRIGGER_TTY · line 163

Quoted 1 line
drivers/leds/trigger/Kconfig +1 -0
+	  Activity for example on packet receive or based on the link speed.

activity

Quoted 9 lines
drivers/leds/trigger/Kconfig +9 -0
+
+	  The current supported offload triggers are:
+	  - blink_tx: Blink LED on tx packet receive
+	  - blink_rx: Blink LED on rx packet receive
+	  - keep_link_10m: Keep LED on with 10m link speed
+	  - keep_link_100m: Keep LED on with 100m link speed
+	  - keep_link_1000m: Keep LED on with 1000m link speed
+	  - keep_half_duplex: Keep LED on with half duplex link
+	  - keep_full_duplex: Keep LED on with full duplex link

drivers/leds/trigger/Kconfig config LEDS_TRIGGER_TTY · line 182

Quoted 10 lines
drivers/leds/trigger/Kconfig +10 -0
+	  - option_linkup_over: Blink rules are ignored with absent link
+	  - option_power_on_reset: Power ON Led on Switch/PHY reset
+	  - option_blink_2hz: Set blink speed at 2hz for every blink event
+	  - option_blink_4hz: Set blink speed at 4hz for every blink event
+	  - option_blink_8hz: Set blink speed at 8hz for every blink event
+
+	  These blink modes are present in the LED sysfs dir under
+	  hardware-phy-activity if supported by the LED driver.
+
+	  This trigger can be used only by LEDs that supports Hardware mode

support Hardware mode.

Quoted 3 lines

Ansuel, do you read and consider these comments?
It's difficult to tell if you do or not.

Yes and with every new version I'm fixing the errors.
Just we are changing implementation many times and more errors comes up. Thanks a lot for the comments and sorry if I'm not answering them.

Quoted 3 lines

thanks.

Signature · 1 line
~Randy
Signature · 1 line
	Ansuel
maintainer
#32 lore raw

Re: [RFC PATCH v3 6/8] leds: trigger: add hardware-phy-activity trigger

In reply to Ansuel Smith
Quoted 15 lines
drivers/leds/trigger/ledtrig-hardware-phy-activity.c new file +15 -0
+#define DEFINE_OFFLOAD_TRIGGER(trigger_name, trigger) \
+	static ssize_t trigger_name##_show(struct device *dev, \
+				struct device_attribute *attr, char *buf) \
+	{ \
+		struct led_classdev *led_cdev = led_trigger_get_led(dev); \
+		int val; \
+		val = led_cdev->hw_control_configure(led_cdev, trigger, BLINK_MODE_READ); \
+		return sprintf(buf, "%d\n", val ? 1 : 0); \
+	} \
+	static ssize_t trigger_name##_store(struct device *dev, \
+					struct device_attribute *attr, \
+					const char *buf, size_t size) \
+	{ \
+		struct led_classdev *led_cdev = led_trigger_get_led(dev); \
+		unsigned long state; \

drivers/leds/trigger/ledtrig-hardware-phy-activity.c line 34

Quoted 10 lines
drivers/leds/trigger/ledtrig-hardware-phy-activity.c new file +10 -0
+		int cmd, ret; \
+		ret = kstrtoul(buf, 0, &state); \
+		if (ret) \
+			return ret; \
+		cmd = !!state ? BLINK_MODE_ENABLE : BLINK_MODE_DISABLE; \
+		/* Update the configuration with every change */ \
+		led_cdev->hw_control_configure(led_cdev, trigger, cmd); \
+		return size; \
+	} \
+	DEVICE_ATTR_RW(trigger_name)

These are pretty big macro magic functions. And there is little actual macro in them. So make them simple functions which call helpers

	static ssize_t trigger_name##_show(struct device *dev, \
				struct device_attribute *attr, char *buf) \
	{ \
		return trigger_generic_store(dev, attr, buf, size, trigger); \
	} \
	static ssize_t trigger_name##_store(struct device *dev, \
					struct device_attribute *attr, \
					const char *buf, size_t size) \
	{ \
		return trigger_generic_store(dev, attr, buf, size, trigger); \
	} \
Quoted 5 lines
drivers/leds/trigger/ledtrig-hardware-phy-activity.c new file +5 -0
+/* The attrs will be placed dynamically based on the supported triggers */
+static struct attribute *phy_activity_attrs[PHY_ACTIVITY_MAX_TRIGGERS + 1];
+
+static int offload_phy_activity_activate(struct led_classdev *led_cdev)
+{

drivers/leds/trigger/ledtrig-hardware-phy-activity.c line 64

Quoted 10 lines
drivers/leds/trigger/ledtrig-hardware-phy-activity.c new file +10 -0
+	u32 checked_list = 0;
+	int i, trigger, ret;
+
+	/* Scan the supported offload triggers and expose them in sysfs if supported */
+	for (trigger = 0, i = 0; trigger < PHY_ACTIVITY_MAX_TRIGGERS; trigger++) {
+		if (!(checked_list & BLINK_TX) &&
+		    led_trigger_blink_mode_is_supported(led_cdev, BLINK_TX)) {
+			phy_activity_attrs[i++] = &dev_attr_blink_tx.attr;
+			checked_list |= BLINK_TX;
+		}

Please re-write this using tables, rather than all this repeated code.

Andrew

maintainer
#33 lore raw

Re: [RFC PATCH v3 6/8] leds: trigger: add hardware-phy-activity trigger

In reply to Ansuel Smith

drivers/leds/trigger/ledtrig-hardware-phy-activity.c line 51

Quoted 2 lines
drivers/leds/trigger/ledtrig-hardware-phy-activity.c new file +2 -0
+/* The attrs will be placed dynamically based on the supported triggers */
+static struct attribute *phy_activity_attrs[PHY_ACTIVITY_MAX_TRIGGERS + 1];

This cannot be global. I have boards with a mixture of different PHYs and switches. Each will have their own collection of supported modes. And some PHYs have different modes per LED.

Andrew

author maintainer
#34 lore raw

RFC PATCH v3 7/8 net: dsa: qca8k: add LEDs support

In reply to Ansuel Smith

Add LEDs support for qca8k Switch Family. This will provide the LEDs hardware API to permit the PHY LED to support hardware mode. Each port have at least 3 LEDs and they can HW blink, set on/off or follow blink modes configured with the LED in hardware mode.. This adds support for 2 hardware trigger netdev and hardware-phy-activity.

---
 drivers/net/dsa/Kconfig      |   9 +
 drivers/net/dsa/Makefile     |   1 +
 drivers/net/dsa/qca8k-leds.c | 429 +++++++++++++++++++++++++++++++++++
 drivers/net/dsa/qca8k.c      |   8 +-
 drivers/net/dsa/qca8k.h      |  65 ++++++
 5 files changed, 510 insertions(+), 2 deletions(-)
 create mode 100644 drivers/net/dsa/qca8k-leds.c
drivers/net/dsa/Kconfig +9 -0
index 7b1457a6e327..89e36f3a8195 100644
@@ -67,6 +67,15 @@ config NET_DSA_QCA8K
 	  This enables support for the Qualcomm Atheros QCA8K Ethernet
 	  switch chips.
 
+config NET_DSA_QCA8K_LEDS_SUPPORT
+	tristate "Qualcomm Atheros QCA8K Ethernet switch family LEDs support"
+	select NET_DSA_QCA8K
+	select LEDS_OFFLOAD_TRIGGERS
+	help
+	  Thsi enabled support for LEDs present on the Qualcomm Atheros
+	  QCA8K Ethernet switch chips. This require the LEDs offload
+	  triggers support as it can run in offload mode.
+
 config NET_DSA_REALTEK_SMI
 	tristate "Realtek SMI Ethernet switch family support"
 	select NET_DSA_TAG_RTL4_A
drivers/net/dsa/Makefile +1 -0
index 8da1569a34e6..fb1e7d0cf126 100644
@@ -9,6 +9,7 @@ obj-$(CONFIG_NET_DSA_LANTIQ_GSWIP) += lantiq_gswip.o
 obj-$(CONFIG_NET_DSA_MT7530)	+= mt7530.o
 obj-$(CONFIG_NET_DSA_MV88E6060) += mv88e6060.o
 obj-$(CONFIG_NET_DSA_QCA8K)	+= qca8k.o
+obj-$(CONFIG_NET_DSA_QCA8K_LEDS_SUPPORT) += qca8k-leds.o
 obj-$(CONFIG_NET_DSA_REALTEK_SMI) += realtek-smi.o
 realtek-smi-objs		:= realtek-smi-core.o rtl8366.o rtl8366rb.o rtl8365mb.o
 obj-$(CONFIG_NET_DSA_SMSC_LAN9303) += lan9303-core.o
drivers/net/dsa/qca8k-leds.c new file +429 -0

429 lines not on this page · raw message

drivers/net/dsa/qca8k.c +6 -2
index a429c9750add..10b2b47b2156 100644
@@ -148,7 +148,7 @@ qca8k_set_page(struct mii_bus *bus, u16 page)
 	return 0;
 }
 
-static int
+int
 qca8k_read(struct qca8k_priv *priv, u32 reg, u32 *val)
 {
 	struct mii_bus *bus = priv->bus;
@@ -192,7 +192,7 @@ qca8k_write(struct qca8k_priv *priv, u32 reg, u32 val)
 	return ret;
 }
 
-static int
+int
 qca8k_rmw(struct qca8k_priv *priv, u32 reg, u32 mask, u32 write_val)
 {
 	struct mii_bus *bus = priv->bus;
@@ -1109,6 +1109,10 @@ qca8k_setup(struct dsa_switch *ds)
 	if (ret)
 		return ret;
 
+	ret = qca8k_setup_led_ctrl(priv);
+	if (ret)
+		return ret;
+
 	/* Make sure MAC06 is disabled */
 	ret = qca8k_reg_clear(priv, QCA8K_REG_PORT0_PAD_CTRL,
 			      QCA8K_PORT0_PAD_MAC06_EXCHANGE_EN);
drivers/net/dsa/qca8k.h +65 -0

93 lines not on this page · raw message

Signature · 1 line
2.32.0
reviewer
#35 lore raw

Re: [RFC PATCH v3 7/8] net: dsa: qca8k: add LEDs support

Quoted 5 lines · Ansuel Smith
drivers/net/dsa/Kconfig +0 -0
index 7b1457a6e327..89e36f3a8195 100644

drivers/net/dsa/Kconfig config NET_DSA_QCA8K · line 75

Quoted 9 lines
drivers/net/dsa/Kconfig +6 -0
@@ -67,6 +67,15 @@ config NET_DSA_QCA8K
  	  This enables support for the Qualcomm Atheros QCA8K Ethernet
  	  switch chips.
 
+config NET_DSA_QCA8K_LEDS_SUPPORT
+	tristate "Qualcomm Atheros QCA8K Ethernet switch family LEDs support"
+	select NET_DSA_QCA8K
+	select LEDS_OFFLOAD_TRIGGERS
+	help
+	  Thsi enabled support for LEDs present on the Qualcomm Atheros

This enables

drivers/net/dsa/Kconfig config NET_DSA_QCA8K · line 76

Quoted 1 line
drivers/net/dsa/Kconfig +1 -0
+	  QCA8K Ethernet switch chips. This require the LEDs offload

requires

Quoted 1 line
drivers/net/dsa/Kconfig +1 -0
+	  triggers support as it can run in offload mode.
Signature · 1 line
~Randy
maintainer
#36 lore raw

Re: [RFC PATCH v3 7/8] net: dsa: qca8k: add LEDs support

In reply to Ansuel Smith

Review

Quoted 2 lines
(quoted diff) +2 -0
+static int
+qca8k_parse_netdev(unsigned long rules, u32 *offload_trigger, u32 *mask)

This is a rather oddly named function, given that it is not actually passed a netdev. netdev has a very well defined meaning in the network stack, struct net_device.

Andrew

author maintainer
#37 lore raw

RFC PATCH v3 8/8 dt-bindings: net: dsa: qca8k: add LEDs definition example

In reply to Ansuel Smith

Add LEDs definition example for qca8k using the offload trigger as the default trigger and add all the supported offload triggers by the switch.

---
 .../devicetree/bindings/net/dsa/qca8k.yaml    | 20 +++++++++++++++++++
 1 file changed, 20 insertions(+)
Documentation/devicetree/bindings/net/dsa/qca8k.yaml +20 -0
index 48de0ace265d..106d95adc1e8 100644
@@ -64,6 +64,8 @@ properties:
                  internal mdio access is used.
                  With the legacy mapping the reg corresponding to the internal
                  mdio is the switch reg with an offset of -1.
+                 Each phy have at least 3 LEDs connected and can be declared
+                 using the standard LEDs structure.
 
     properties:
       '#address-cells':
@@ -340,6 +342,24 @@ examples:
 
                 internal_phy_port1: ethernet-phy@0 {
                     reg = <0>;
+
+                    leds {
+                        led@0 {
+                            reg = <0>;
+                            color = <LED_COLOR_ID_WHITE>;
+                            function = LED_FUNCTION_LAN;
+                            function-enumerator = <1>;
+                            linux,default-trigger = "offload-phy-activity";
+                        };
+
+                        led@1 {
+                            reg = <1>;
+                            color = <LED_COLOR_ID_AMBER>;
+                            function = LED_FUNCTION_LAN;
+                            function-enumerator = <1>;
+                            linux,default-trigger = "offload-phy-activity";
+                        };
+                    };
                 };
 
                 internal_phy_port2: ethernet-phy@1 {
Signature · 1 line
2.32.0