Skip to contentSkip to conversation

can: add tx/rx LED trigger support

RFC PATCH Superseded Fabio Baltieri 5 participants 24 messages Apr 23, 2012 – May 4, 2012 CAN network drivers

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 1/2 can: add tx/rx LED trigger support

This patch implements the functions to add two LED triggers, named <ifname>-tx and <ifname>-rx, to a canbus device driver.

Triggers are called from specific handlers by each CAN device driver and can be disabled altogether with a Kconfig option.

The implementation keeps the LED on when the interface is UP and blinks the LED on network activity at a configurable rate.

This only supports can-dev based drivers, as it uses some support field in the can_priv structure.

Supported drivers should call can_led_init(), can_led_exit() and can_led_event() as needed.

Supported events are:
- CAN_LED_EVENT_OPEN: turn on tx/rx LEDs
- CAN_LED_EVENT_STOP: turn off tx/rx LEDs
- CAN_LED_EVENT_TX: trigger tx LED blink
- CAN_LED_EVENT_RX: trigger tx LED blink

---

Hello again!

So, this is the v2 of my can-led patch. I reworked the whole code based on the v1 and all the comments from the list (thanks guys!).

This version takes a different approach and is just an implementation of three functions - can_led_{init,exit,event} - to easily add tx/rx led trigger support to a netlink-based canbus driver. An implementation on flexcan is on the 2/2.

Working principle:

- the driver register the triggers on probe, free on exit and add events for open, close, tx, rx where appropriate
- both LEDs stay off when device is down, on when device is up
- each LED blinks for led_delay off + led_delay on on tx/rx event using a kernel timer (well, that's actually the opposite to keep critical path cleaner but don't tell anyone, k?).
- new tx/rx packets don't retrigger the timer until the blink sequence is over, so that on heavy traffic the LED keeps blinking at constate rate.

Notes:

- I considered putting all structures in can_priv to skip a couple of kzallocs as suggested by Oliver, but those pointers are actually useful to check if triggers registered properly, so the code now uses an internal structure with timer, state and name, and add just two pointers into struct can_dev.

- The blink timer is implemented as:
* keep on for delay_time
* turn off and retrigger the timer
* turn back on after delay
so that the hot-path only contains a switch-case, some checks and a mod_timer.

This is tested on an x86 with a custom usb-can interface and on a flexcan-based Freescale ARM board. I'll post some patch to implement support in other drivers if anyone is interested into testing this one.

Any comments?

Regards,
Fabio

 drivers/net/can/Kconfig  |   13 ++++
 drivers/net/can/Makefile |    2 +
 drivers/net/can/led.c    |  143 ++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/can/dev.h  |    6 ++
 include/linux/can/led.h  |   46 +++++++++++++++
 5 files changed, 210 insertions(+), 0 deletions(-)
 create mode 100644 drivers/net/can/led.c
 create mode 100644 include/linux/can/led.h
drivers/net/can/Kconfig +13 -0
index bb709fd..74d6bfb 100644
@@ -54,6 +54,19 @@ config CAN_CALC_BITTIMING
 	  arguments "tq", "prop_seg", "phase_seg1", "phase_seg2" and "sjw".
 	  If unsure, say Y.
 
+config CAN_LEDS
+	bool "Enable LED triggers for Netlink based drivers"
+	depends on CAN
+	depends on CAN_DEV
+	depends on LEDS_CLASS
+	select LEDS_TRIGGERS
+	---help---
+	  This option adds two LED triggers for packet receive and transmit
+	  events on each supported CAN device.
+
+	  Say Y here if you are working on a system with led-class supported
+	  LEDs and you want to use them as canbus activity indicators.
+
 config CAN_AT91
 	tristate "Atmel AT91 onchip CAN controller"
 	depends on CAN_DEV && (ARCH_AT91SAM9263 || ARCH_AT91SAM9X5)
drivers/net/can/Makefile +2 -0
index 938be37..24ee98b 100644
@@ -8,6 +8,8 @@ obj-$(CONFIG_CAN_SLCAN)		+= slcan.o
 obj-$(CONFIG_CAN_DEV)		+= can-dev.o
 can-dev-y			:= dev.o
 
+can-dev-$(CONFIG_CAN_LEDS)	+= led.o
+
 obj-y				+= usb/
 obj-y				+= softing/
 
drivers/net/can/led.c new file +143 -0

143 lines not on this page · raw message

include/linux/can/dev.h +6 -0
index 5d2efe7..a5a70bd 100644
@@ -16,6 +16,7 @@
 #include <linux/can.h>
 #include <linux/can/netlink.h>
 #include <linux/can/error.h>
+#include <linux/can/led.h>
 
 /*
  * CAN mode
@@ -52,6 +53,11 @@ struct can_priv {
 
 	unsigned int echo_skb_max;
 	struct sk_buff **echo_skb;
+
+#ifdef CONFIG_CAN_LEDS
+	struct can_led_data *tx_led_data;
+	struct can_led_data *rx_led_data;
+#endif
 };
 
 /*
include/linux/can/led.h new file +46 -0

46 lines not on this page · raw message

Signature · 1 line
1.7.5.1
#1 lore raw

RFC PATCH v2 2/2 can: flexcan: add LED trigger support

Add support for canbus activity led indicators on flexcan interfaces by calling appropriate can_led_* functions.

These are only enabled when CONFIG_CAN_LEDS is Y, becomes no-op otherwise.

---
 drivers/net/can/flexcan.c |   13 +++++++++++++
 1 files changed, 13 insertions(+), 0 deletions(-)
drivers/net/can/flexcan.c +13 -0
index 1efb083..0e55c4c 100644
@@ -23,6 +23,7 @@
 #include <linux/can.h>
 #include <linux/can/dev.h>
 #include <linux/can/error.h>
+#include <linux/can/led.h>
 #include <linux/can/platform/flexcan.h>
 #include <linux/clk.h>
 #include <linux/delay.h>
@@ -524,6 +525,8 @@ static int flexcan_read_frame(struct net_device *dev)
 	stats->rx_packets++;
 	stats->rx_bytes += cf->can_dlc;
 
+	can_led_event(dev, CAN_LED_EVENT_RX);
+
 	return 1;
 }
 
@@ -614,6 +617,8 @@ static irqreturn_t flexcan_irq(int irq, void *dev_id)
 		stats->tx_packets++;
 		flexcan_write((1 << FLEXCAN_TX_BUF_ID), &regs->iflag1);
 		netif_wake_queue(dev);
+
+		can_led_event(dev, CAN_LED_EVENT_TX);
 	}
 
 	return IRQ_HANDLED;
@@ -813,6 +818,8 @@ static int flexcan_open(struct net_device *dev)
 	if (err)
 		goto out_close;
 
+	can_led_event(dev, CAN_LED_EVENT_OPEN);
+
 	/* start chip and queuing */
 	err = flexcan_chip_start(dev);
 	if (err)
@@ -838,6 +845,8 @@ static int flexcan_close(struct net_device *dev)
 	napi_disable(&priv->napi);
 	flexcan_chip_stop(dev);
 
+	can_led_event(dev, CAN_LED_EVENT_STOP);
+
 	free_irq(dev->irq, dev);
 	clk_disable_unprepare(priv->clk);
 
@@ -1004,6 +1013,8 @@ static int __devinit flexcan_probe(struct platform_device *pdev)
 		goto failed_register;
 	}
 
+	can_led_init(dev);
+
 	dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n",
 		 priv->base, dev->irq);
 
@@ -1028,6 +1039,8 @@ static int __devexit flexcan_remove(struct platform_device *pdev)
 	struct flexcan_priv *priv = netdev_priv(dev);
 	struct resource *mem;
 
+	can_led_exit(dev);
+
 	unregister_flexcandev(dev);
 	platform_set_drvdata(pdev, NULL);
 	iounmap(priv->base);
Signature · 1 line
1.7.5.1
#2 lore raw
In reply to Fabio Baltieri
Quoted 2 lines · Fabio Baltieri

This patch implements the functions to add two LED triggers, named <ifname>-tx and <ifname>-rx, to a canbus device driver.

Hello Fabio,

to me it looks really good now. I understand everything of the idea at first sight which is a good indicator for simple code ;-)

The only remark i currently have is ...

drivers/net/can/Kconfig config CAN_CALC_BITTIMING · line 59

Quoted 12 lines
drivers/net/can/Kconfig +3 -0
index bb709fd..74d6bfb 100644
@@ -54,6 +54,19 @@ config CAN_CALC_BITTIMING
 	  arguments "tq", "prop_seg", "phase_seg1", "phase_seg2" and "sjw".
 	  If unsure, say Y.
 
+config CAN_LEDS
+	bool "Enable LED triggers for Netlink based drivers"
+	depends on CAN

CAN_DEV and this entire Kconfig content itself depends on CAN (see at the top of this Kconfig) so you can omit that line.

drivers/net/can/Kconfig config CAN_CALC_BITTIMING · line 72

Quoted 13 lines
drivers/net/can/Kconfig +10 -0
+	depends on CAN_DEV
+	depends on LEDS_CLASS
+	select LEDS_TRIGGERS
+	---help---
+	  This option adds two LED triggers for packet receive and transmit
+	  events on each supported CAN device.
+
+	  Say Y here if you are working on a system with led-class supported
+	  LEDs and you want to use them as canbus activity indicators.
+
 config CAN_AT91
 	tristate "Atmel AT91 onchip CAN controller"
 	depends on CAN_DEV && (ARCH_AT91SAM9263 || ARCH_AT91SAM9X5)

Regards,
Oliver

#3 lore raw
Quoted 7 lines · Oliver Hartkopp
Quoted 2 lines · Fabio Baltieri

This patch implements the functions to add two LED triggers, named <ifname>-tx and <ifname>-rx, to a canbus device driver.

Hello Fabio,

Hi Oliver,

Quoted 2 lines

to me it looks really good now. I understand everything of the idea at first sight which is a good indicator for simple code ;-)

Thanks!

Quoted 11 lines

The only remark i currently have is ...

Quoted 9 lines
drivers/net/can/Kconfig +0 -0
index bb709fd..74d6bfb 100644
@@ -54,6 +54,19 @@ config CAN_CALC_BITTIMING
 	  arguments "tq", "prop_seg", "phase_seg1", "phase_seg2" and "sjw".
 	  If unsure, say Y.
 
Quoted 7 lines

drivers/net/can/Kconfig config CAN_CALC_BITTIMING · line 59

Quoted 3 lines
drivers/net/can/Kconfig +3 -0
+config CAN_LEDS
+	bool "Enable LED triggers for Netlink based drivers"
+	depends on CAN

CAN_DEV and this entire Kconfig content itself depends on CAN (see at the top of this Kconfig) so you can omit that line.

You're right, that was a leftover from the v1.

Removed for v3.

Regards,
Fabio

#4 lore raw
In reply to Fabio Baltieri

Hi Fabio,

Quoted 58 lines · Fabio Baltieri

This patch implements the functions to add two LED triggers, named <ifname>-tx and <ifname>-rx, to a canbus device driver.

Triggers are called from specific handlers by each CAN device driver and can be disabled altogether with a Kconfig option.

The implementation keeps the LED on when the interface is UP and blinks the LED on network activity at a configurable rate.

This only supports can-dev based drivers, as it uses some support field in the can_priv structure.

Supported drivers should call can_led_init(), can_led_exit() and can_led_event() as needed.

Supported events are:
- CAN_LED_EVENT_OPEN: turn on tx/rx LEDs
- CAN_LED_EVENT_STOP: turn off tx/rx LEDs
- CAN_LED_EVENT_TX: trigger tx LED blink
- CAN_LED_EVENT_RX: trigger tx LED blink

---

Hello again!

So, this is the v2 of my can-led patch. I reworked the whole code based on the v1 and all the comments from the list (thanks guys!).

This version takes a different approach and is just an implementation of three functions - can_led_{init,exit,event} - to easily add tx/rx led trigger support to a netlink-based canbus driver. An implementation on flexcan is on the 2/2.

Working principle:

- the driver register the triggers on probe, free on exit and add events for open, close, tx, rx where appropriate
- both LEDs stay off when device is down, on when device is up
- each LED blinks for led_delay off + led_delay on on tx/rx event using a kernel timer (well, that's actually the opposite to keep critical path cleaner but don't tell anyone, k?).
- new tx/rx packets don't retrigger the timer until the blink sequence is over, so that on heavy traffic the LED keeps blinking at constate rate.

Notes:

- I considered putting all structures in can_priv to skip a couple of kzallocs as suggested by Oliver, but those pointers are actually useful to check if triggers registered properly, so the code now uses an internal structure with timer, state and name, and add just two pointers into struct can_dev.

- The blink timer is implemented as:
* keep on for delay_time
* turn off and retrigger the timer
* turn back on after delay
so that the hot-path only contains a switch-case, some checks and a mod_timer.

Quoted 5 lines

This is tested on an x86 with a custom usb-can interface and on a flexcan-based Freescale ARM board. I'll post some patch to implement support in other drivers if anyone is interested into testing this one.

Any comments?

I still think that the blinking support should go to the timer class to avoid duplicated code. Any good reason against? Apart from that the patches look good.

Wolfgang.

#5 lore raw
Quoted 10 lines · Wolfgang Grandegger
Quoted 10 lines

- The blink timer is implemented as:
* keep on for delay_time
* turn off and retrigger the timer
* turn back on after delay
so that the hot-path only contains a switch-case, some checks and a mod_timer.

This is tested on an x86 with a custom usb-can interface and on a flexcan-based Freescale ARM board. I'll post some patch to implement support in other drivers if anyone is interested into testing this one.

Quoted 5 lines
Quoted 1 line

Any comments?

I still think that the blinking support should go to the timer class to avoid duplicated code. Any good reason against? Apart from that the patches look good.

Hello Wolfgang,

what exactly do you mean with "timer class" ??

To me the current implementation looks like standard jiffie-based timer usage.

IMO hrtimers are not needed here - or what are you thinking of?

Regards,
Oliver

#6 lore raw
Quoted 20 lines · Oliver Hartkopp
Quoted 10 lines · Wolfgang Grandegger
Quoted 10 lines

- The blink timer is implemented as:
* keep on for delay_time
* turn off and retrigger the timer
* turn back on after delay
so that the hot-path only contains a switch-case, some checks and a mod_timer.

This is tested on an x86 with a custom usb-can interface and on a flexcan-based Freescale ARM board. I'll post some patch to implement support in other drivers if anyone is interested into testing this one.

Quoted 5 lines
Quoted 1 line

Any comments?

I still think that the blinking support should go to the timer class to avoid duplicated code. Any good reason against? Apart from that the patches look good.

Quoted 7 lines

Hello Wolfgang,

what exactly do you mean with "timer class" ??

To me the current implementation looks like standard jiffie-based timer usage.

IMO hrtimers are not needed here - or what are you thinking of?

I mean that the code doing that kind of blinking should go to the timer class as generic function/feature, which other drivers could then use as well, similar to led_trigger_event or led_trigger_blink. Should not be a big deal, I think. See also my previous mail:

http://marc.info/?l=linux-can&m=133425656510148&w=2

Wolfgang.

#7 lore raw
Quoted 27 lines · Wolfgang Grandegger
Quoted 26 lines · Oliver Hartkopp
Quoted 10 lines · Wolfgang Grandegger
Quoted 10 lines

- The blink timer is implemented as:
* keep on for delay_time
* turn off and retrigger the timer
* turn back on after delay
so that the hot-path only contains a switch-case, some checks and a mod_timer.

This is tested on an x86 with a custom usb-can interface and on a flexcan-based Freescale ARM board. I'll post some patch to implement support in other drivers if anyone is interested into testing this one.

Quoted 5 lines
Quoted 1 line

Any comments?

I still think that the blinking support should go to the timer class to avoid duplicated code. Any good reason against? Apart from that the patches look good.

Hello Wolfgang,

what exactly do you mean with "timer class" ??

To me the current implementation looks like standard jiffie-based timer usage.

Quoted 9 lines
Quoted 1 line

IMO hrtimers are not needed here - or what are you thinking of?

I mean that the code doing that kind of blinking should go to the timer class as generic function/feature, which other drivers could then use as well, similar to led_trigger_event or led_trigger_blink. Should not be a big deal, I think. See also my previous mail:

http://marc.info/?l=linux-can&m=133425656510148&w=2

Hm - as i see from the two examples there, the LEDs have a different behaviour. E.g. on which use-case specific events they blink or set the LEDs to fixed values.

I can't see any real benefit for these LED users to implement a generic LED blinking framework. IMO using timers and the led_trigger_event stuff is the appropriate abstraction level.

If you look into other networking drivers (e.g. the wireless) - they do it with delayed_work:

http://lxr.linux.no/#linux+v3.3.1/drivers/net/wireless/ath/carl9170/led.c#L105

And everyone defines a specific struct like this one:

http://lxr.linux.no/#linux+v3.3.1/drivers/net/wireless/ath/carl9170/carl9170.h#L205

I think it's an enormous effort to unify all the LED users ...

Regards,
Oliver

#8 lore raw
Quoted 40 lines · Oliver Hartkopp
Quoted 27 lines · Wolfgang Grandegger
Quoted 26 lines · Oliver Hartkopp
Quoted 10 lines · Wolfgang Grandegger
Quoted 10 lines

- The blink timer is implemented as:
* keep on for delay_time
* turn off and retrigger the timer
* turn back on after delay
so that the hot-path only contains a switch-case, some checks and a mod_timer.

This is tested on an x86 with a custom usb-can interface and on a flexcan-based Freescale ARM board. I'll post some patch to implement support in other drivers if anyone is interested into testing this one.

Quoted 5 lines
Quoted 1 line

Any comments?

I still think that the blinking support should go to the timer class to avoid duplicated code. Any good reason against? Apart from that the patches look good.

Hello Wolfgang,

what exactly do you mean with "timer class" ??

To me the current implementation looks like standard jiffie-based timer usage.

Quoted 9 lines
Quoted 1 line

IMO hrtimers are not needed here - or what are you thinking of?

I mean that the code doing that kind of blinking should go to the timer class as generic function/feature, which other drivers could then use as well, similar to led_trigger_event or led_trigger_blink. Should not be a big deal, I think. See also my previous mail:

http://marc.info/?l=linux-can&m=133425656510148&w=2

Quoted 7 lines

Hm - as i see from the two examples there, the LEDs have a different behaviour. E.g. on which use-case specific events they blink or set the LEDs to fixed values.

I can't see any real benefit for these LED users to implement a generic LED blinking framework. IMO using timers and the led_trigger_event stuff is the appropriate abstraction level.

It's not the task of the CAN driver to let LEDs blink! Or what should the LED class then be good for?

Quoted 10 lines

If you look into other networking drivers (e.g. the wireless) - they do it with delayed_work:

http://lxr.linux.no/#linux+v3.3.1/drivers/net/wireless/ath/carl9170/led.c#L105

And everyone defines a specific struct like this one:

http://lxr.linux.no/#linux+v3.3.1/drivers/net/wireless/ath/carl9170/carl9170.h#L205

I think it's an enormous effort to unify all the LED users ...

Yes, they do that because there is no generic support. I believe that the "one-shot-blinking" is useful in general which new drivers may use. I did not ask for adapting old drivers.

Wolfgang.

#9 lore raw
In reply to Wolfgang Grandegger

Hi Wolfgang,

Quoted 5 lines · Wolfgang Grandegger

I still think that the blinking support should go to the timer class to avoid duplicated code. Any good reason against? Apart from that the patches look good.

Wolfgang.

I can see you point and I considered your note about adding the one-shot-blink function to the led-class framework (sorry for not mentioning it in my first post). Still, I ended up with this code for a couple of reasons:

- I think that the led_blink_set function is primarily used to configure leds with hardware blinking (like i2c led drivers). While it would be possible to extend the function to get one-shot behavior and always fallback on software blink, I think that that's out of the purpose of the led-class, which should just translate on-off requests to underlaying hardware.

- I think that different drivers may want to obtain different on-off behavior depending on the application. For example in the ide-disk case the user expects to see a steady-on LED on constant activity, and that's how it's implemented, while in this case the on-if-up keep-blinking-on-activity off-if-down makes much more sense. So I think that even if a generic blink function were available, people would still be using custom functions because they want to fine tune the behavior for the application. Also, maybe a function too generic may impact on performance in critical paths.

- in this case, it looks to me like the implementation is as optimized as it can be, in the sense that the hot-path does really only some essential check and engage the timer and the timer function itself is really short. Also the final blinking effect is nice IMO :-)

Regards,
Fabio

#10 lore raw

Hi Fabio,

Quoted 14 lines · Fabio Baltieri

Hi Wolfgang,

Quoted 5 lines · Wolfgang Grandegger

I still think that the blinking support should go to the timer class to avoid duplicated code. Any good reason against? Apart from that the patches look good.

Wolfgang.

I can see you point and I considered your note about adding the one-shot-blink function to the led-class framework (sorry for not mentioning it in my first post). Still, I ended up with this code for a couple of reasons:

Quoted 6 lines

- I think that the led_blink_set function is primarily used to configure leds with hardware blinking (like i2c led drivers). While it would be possible to extend the function to get one-shot behavior and always fallback on software blink, I think that that's out of the purpose of the led-class, which should just translate on-off requests to underlaying hardware.

Why, there is already led_tigger_event() and led_trigger_blink(). Why should led_trigger_blink_once() then do not make sense?

Quoted 9 lines

- I think that different drivers may want to obtain different on-off behavior depending on the application. For example in the ide-disk case the user expects to see a steady-on LED on constant activity, and that's how it's implemented, while in this case the on-if-up keep-blinking-on-activity off-if-down makes much more sense. So I think that even if a generic blink function were available, people would still be using custom functions because they want to fine tune the behavior for the application. Also, maybe a function too generic may impact on performance in critical paths.

The blinking could be specified with led_trigger_blink_once().

Quoted 4 lines

- in this case, it looks to me like the implementation is as optimized as it can be, in the sense that the hot-path does really only some essential check and engage the timer and the timer function itself is really short. Also the final blinking effect is nice IMO :-)

For me it still does make sense to provide a generic led_trigger_blink_once support. But well, go ahead if I'm the only one with that opinion.

Wolfgang.

#11 lore raw
Quoted 39 lines · Wolfgang Grandegger

Hi Fabio,

Quoted 14 lines · Fabio Baltieri

Hi Wolfgang,

Quoted 5 lines · Wolfgang Grandegger

I still think that the blinking support should go to the timer class to avoid duplicated code. Any good reason against? Apart from that the patches look good.

Wolfgang.

I can see you point and I considered your note about adding the one-shot-blink function to the led-class framework (sorry for not mentioning it in my first post). Still, I ended up with this code for a couple of reasons:

Quoted 6 lines

- I think that the led_blink_set function is primarily used to configure leds with hardware blinking (like i2c led drivers). While it would be possible to extend the function to get one-shot behavior and always fallback on software blink, I think that that's out of the purpose of the led-class, which should just translate on-off requests to underlaying hardware.

Why, there is already led_tigger_event() and led_trigger_blink(). Why should led_trigger_blink_once() then do not make sense?

Quoted 9 lines

- I think that different drivers may want to obtain different on-off behavior depending on the application. For example in the ide-disk case the user expects to see a steady-on LED on constant activity, and that's how it's implemented, while in this case the on-if-up keep-blinking-on-activity off-if-down makes much more sense. So I think that even if a generic blink function were available, people would still be using custom functions because they want to fine tune the behavior for the application. Also, maybe a function too generic may impact on performance in critical paths.

The blinking could be specified with led_trigger_blink_once().

Quoted 8 lines
Quoted 4 lines

- in this case, it looks to me like the implementation is as optimized as it can be, in the sense that the hot-path does really only some essential check and engage the timer and the timer function itself is really short. Also the final blinking effect is nice IMO :-)

For me it still does make sense to provide a generic led_trigger_blink_once support. But well, go ahead if I'm the only one with that opinion.

+1
At least start a discussion on the LED list (I don't know which list that is).

Marc

Signature · 4 lines
Pengutronix e.K.                  | Marc Kleine-Budde           |
Industrial Linux Solutions        | Phone: +49-231-2826-924     |
Vertretung West/Dortmund          | Fax:   +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |
#12 lore raw
Quoted 45 lines · Marc Kleine-Budde
Quoted 44 lines · Wolfgang Grandegger

Hi Fabio,

Quoted 14 lines · Fabio Baltieri

Hi Wolfgang,

Quoted 5 lines · Wolfgang Grandegger

I still think that the blinking support should go to the timer class to avoid duplicated code. Any good reason against? Apart from that the patches look good.

Wolfgang.

I can see you point and I considered your note about adding the one-shot-blink function to the led-class framework (sorry for not mentioning it in my first post). Still, I ended up with this code for a couple of reasons:

Quoted 6 lines

- I think that the led_blink_set function is primarily used to configure leds with hardware blinking (like i2c led drivers). While it would be possible to extend the function to get one-shot behavior and always fallback on software blink, I think that that's out of the purpose of the led-class, which should just translate on-off requests to underlaying hardware.

Why, there is already led_tigger_event() and led_trigger_blink(). Why should led_trigger_blink_once() then do not make sense?

Quoted 9 lines

- I think that different drivers may want to obtain different on-off behavior depending on the application. For example in the ide-disk case the user expects to see a steady-on LED on constant activity, and that's how it's implemented, while in this case the on-if-up keep-blinking-on-activity off-if-down makes much more sense. So I think that even if a generic blink function were available, people would still be using custom functions because they want to fine tune the behavior for the application. Also, maybe a function too generic may impact on performance in critical paths.

The blinking could be specified with led_trigger_blink_once().

Quoted 4 lines

- in this case, it looks to me like the implementation is as optimized as it can be, in the sense that the hot-path does really only some essential check and engage the timer and the timer function itself is really short. Also the final blinking effect is nice IMO :-)

Quoted 7 lines
Quoted 3 lines

For me it still does make sense to provide a generic led_trigger_blink_once support. But well, go ahead if I'm the only one with that opinion.

+1
At least start a discussion on the LED list (I don't know which list that is).

The MAINAINERS file does not list a mailing list, therefore the LKML should be used, with a CC to Richard Purdie, I think. While checking if LED support is discussed, I found related mails. One shot timer LED support seems to be a frequently discussed issue:

http://marc.info/?t=133489487700001&r=1&w=2
http://marc.info/?l=linux-kernel&m=133331013422467&w=4

As usual, it's better to send a patch than to ask a (general) questions. I think we would get rather quick response.

Wolfgang.

#13 lore raw

Hello everyone,

I'm back on the activity-trigger subject.

Quoted 18 lines · Wolfgang Grandegger
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines · Wolfgang Grandegger
Quoted 4 lines

I still think that the blinking support should go to the timer class to avoid duplicated code. Any good reason against? Apart from that the patches look good.

Quoted 7 lines
Quoted 3 lines

For me it still does make sense to provide a generic led_trigger_blink_once support. But well, go ahead if I'm the only one with that opinion.

+1
At least start a discussion on the LED list (I don't know which list that is).

The MAINAINERS file does not list a mailing list, therefore the LKML should be used, with a CC to Richard Purdie, I think. While checking if LED support is discussed, I found related mails. One shot timer LED support seems to be a frequently discussed issue:

Quoted 5 lines

http://marc.info/?t=133489487700001&r=1&w=2
http://marc.info/?l=linux-kernel&m=133331013422467&w=4

As usual, it's better to send a patch than to ask a (general) questions. I think we would get rather quick response.

I spent last couple of days trying to come up with a generic one-shot blink patch clean enough to get some chance of integration, but what I ended up with was these considerations in favor of framework-specific code:

The only clean way I see to do that is to implement the function as an extension of the actual soft-blink code.

That functions are used as fallback if no hw-blink is available and are just blindly toggling values on-off, so they have to be modified quite heavily to add necessary state information to support the behaviour of the actual code (like when a trigger is added for an already up interface) and the feature required by other usage cases (like the trigger currently discussed in the list - which is going in the way of a trigger-specific implemetation anyway). This would involve adding flags and cases in both trigger and timer code.

So, I'm taking a break on this and trying to feed this argument: why the socketcan code would benefit from a specific implementation rather than a generic one?

Because overall code is *much* more simple and clean!

- the timer function body is just 12 lines of code, whithout special cases, that's faster to execute and easy to understand (and debug).

- the event function for TX and RX paths is just some checks and a mod_timer, without list iterations or locking, and as it's going to be called in hard-irq context, I think this is a *strong* plus for the implementation (the most important thing here is to get out of the way as fast as possible, right? A generic implementation would only make latencies worse here).

So, my point, do you still think that this feature would benefit of a generic triggering implementation?

(of course, I'm posting a v3 with just Oliver's Kconfig notes if you agree with my point :-) )

Regards,
Fabio

#14 lore raw
Quoted 62 lines · Fabio Baltieri

Hello everyone,

I'm back on the activity-trigger subject.

Quoted 18 lines · Wolfgang Grandegger
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines · Wolfgang Grandegger
Quoted 4 lines

I still think that the blinking support should go to the timer class to avoid duplicated code. Any good reason against? Apart from that the patches look good.

Quoted 7 lines
Quoted 3 lines

For me it still does make sense to provide a generic led_trigger_blink_once support. But well, go ahead if I'm the only one with that opinion.

+1
At least start a discussion on the LED list (I don't know which list that is).

The MAINAINERS file does not list a mailing list, therefore the LKML should be used, with a CC to Richard Purdie, I think. While checking if LED support is discussed, I found related mails. One shot timer LED support seems to be a frequently discussed issue:

Quoted 5 lines

http://marc.info/?t=133489487700001&r=1&w=2
http://marc.info/?l=linux-kernel&m=133331013422467&w=4

As usual, it's better to send a patch than to ask a (general) questions. I think we would get rather quick response.

I spent last couple of days trying to come up with a generic one-shot blink patch clean enough to get some chance of integration, but what I ended up with was these considerations in favor of framework-specific code:

The only clean way I see to do that is to implement the function as an extension of the actual soft-blink code.

That functions are used as fallback if no hw-blink is available and are just blindly toggling values on-off, so they have to be modified quite heavily to add necessary state information to support the behaviour of the actual code (like when a trigger is added for an already up interface) and the feature required by other usage cases (like the trigger currently discussed in the list - which is going in the way of a trigger-specific implemetation anyway). This would involve adding flags and cases in both trigger and timer code.

So, I'm taking a break on this and trying to feed this argument: why the socketcan code would benefit from a specific implementation rather than a generic one?

Because overall code is *much* more simple and clean!

- the timer function body is just 12 lines of code, whithout special cases, that's faster to execute and easy to understand (and debug).

- the event function for TX and RX paths is just some checks and a mod_timer, without list iterations or locking, and as it's going to be called in hard-irq context, I think this is a *strong* plus for the implementation (the most important thing here is to get out of the way as fast as possible, right? A generic implementation would only make latencies worse here).

Quoted 6 lines

So, my point, do you still think that this feature would benefit of a generic triggering implementation?

(of course, I'm posting a v3 with just Oliver's Kconfig notes if you agree with my point :-) )

Hi Fabio,

as the former implementation was fine to me there's no need to come up with additional remarks for me ... btw. i think your implementation is clean, simple and efficient. Therefore i would vote for it too.

I don't know if we should guide anyone to implement a generic led blinking framework covering all the (existing) cases in the kernel. Adding your patches would probably make the creation of such a framework more challenging to a volunteer in terms of creating a solution which is that simple and efficient.

just my two cents ...

Regards,
Oliver

#15 lore raw
In reply to Fabio Baltieri

Hi Fabio,

Quoted 13 lines · Fabio Baltieri

Hello everyone,

I'm back on the activity-trigger subject.

Quoted 8 lines · Wolfgang Grandegger
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines · Wolfgang Grandegger
Quoted 4 lines

I still think that the blinking support should go to the timer class to avoid duplicated code. Any good reason against? Apart from that the patches look good.

For me it still does make sense to provide a generic led_trigger_blink_once support. But well, go ahead if I'm the only one with that opinion.

You mean it does not make sense because it's difficult to implement? At least it sounds like.

Quoted 15 lines
Quoted 9 lines
Quoted 3 lines

+1
At least start a discussion on the LED list (I don't know which list that is).

The MAINAINERS file does not list a mailing list, therefore the LKML should be used, with a CC to Richard Purdie, I think. While checking if LED support is discussed, I found related mails. One shot timer LED support seems to be a frequently discussed issue:

Quoted 5 lines

http://marc.info/?t=133489487700001&r=1&w=2
http://marc.info/?l=linux-kernel&m=133331013422467&w=4

As usual, it's better to send a patch than to ask a (general) questions. I think we would get rather quick response.

Quoted 7 lines

I spent last couple of days trying to come up with a generic one-shot blink patch clean enough to get some chance of integration, but what I ended up with was these considerations in favor of framework-specific code:

The only clean way I see to do that is to implement the function as an extension of the actual soft-blink code.

Yes, hardware support seems not to make sense.

Quoted 9 lines

That functions are used as fallback if no hw-blink is available and are just blindly toggling values on-off, so they have to be modified quite heavily to add necessary state information to support the behaviour of the actual code (like when a trigger is added for an already up interface) and the feature required by other usage cases (like the trigger currently discussed in the list - which is going in the way of a trigger-specific implemetation anyway). This would involve adding flags and cases in both trigger and timer code.

Quoted 3 lines

So, I'm taking a break on this and trying to feed this argument: why the socketcan code would benefit from a specific implementation rather than a generic one?

The kernel will benefit from a generic implementation, I believe.

Quoted 4 lines

Because overall code is *much* more simple and clean!

- the timer function body is just 12 lines of code, whithout special cases, that's faster to execute and easy to understand (and debug).

That's also true for a LED class based implementation.

Quoted 6 lines

- the event function for TX and RX paths is just some checks and a mod_timer, without list iterations or locking, and as it's going to be called in hard-irq context, I think this is a *strong* plus for the implementation (the most important thing here is to get out of the way as fast as possible, right? A generic implementation would only make latencies worse here).

I don't think so, at least not for a simple software driven one-shot blinking similar to yours.

Quoted 2 lines

So, my point, do you still think that this feature would benefit of a generic triggering implementation?

Yes. What I would do is to prepare a simple software driven one-shot blinking similar to your existing code and similar to led_trigger_blink. Send the patch to the LKML and see how the community and the maintainer react. I think there is no hurry to get LED blinking supported by the mainline kernel.

Wolfgang.

#16 lore raw
In reply to Fabio Baltieri
Quoted 3 lines · Fabio Baltieri

This patch implements the functions to add two LED triggers, named <ifname>-tx and <ifname>-rx, to a canbus device driver.

[...]

If I understand well, this function is subject to maximal optimization.

drivers/net/can/led.c line 41

Quoted 3 lines
drivers/net/can/led.c new file +3 -0
+void can_led_event(struct net_device *netdev, enum can_led_event event)
+{
+	struct can_priv *priv = netdev_priv(netdev);

If the assignment is postponed to withing the differenct cases, thereby repeating this statement 3 times (not 4!), both for RX & TX path a single assignment could be saved.
For stack usage, I'm not an expert, but I suspect a little less stack use. I'm not sure wether it's worth the pain of extra lines of code. I tried to illustrate my point here:

Quoted 30 lines

+++ struct can_led_data *tx_led, *rx_led;

drivers/net/can/led.c new file +29 -0
+
+	switch (event) {
+	case CAN_LED_EVENT_OPEN:
+++		tx_led = priv->tx_led_data;
+		if (likely(tx_led))
+			led_trigger_event(&tx_led->trig, LED_FULL);
+++		rx_led = priv->rx_led_data;
+		if (likely(rx_led))
+			led_trigger_event(&rx_led->trig, LED_FULL);
+		break;
+	case CAN_LED_EVENT_STOP:
+++		tx_led = priv->tx_led_data;
+		if (likely(tx_led)) {
+			del_timer_sync(&tx_led->timer);
+			led_trigger_event(&tx_led->trig, LED_OFF);
+		}
+++		rx_led = priv->rx_led_data;
+		if (likely(rx_led)) {
+			del_timer_sync(&rx_led->timer);
+			led_trigger_event(&rx_led->trig, LED_OFF);
+		}
+		break;
+	case CAN_LED_EVENT_TX:
+++		tx_led = priv->tx_led_data;
+		if (led_delay && likely(tx_led) &&
+		    !timer_pending(&tx_led->timer))
+			mod_timer(&tx_led->timer,
+				jiffies + msecs_to_jiffies(led_delay));
+		break;

drivers/net/can/led.c line 76

Quoted 10 lines
drivers/net/can/led.c new file +10 -0
+	case CAN_LED_EVENT_RX:
+++		rx_led = priv->rx_led_data;
+		if (led_delay && likely(rx_led) &&
+		    !timer_pending(&rx_led->timer))
+			mod_timer(&rx_led->timer,
+				jiffies + msecs_to_jiffies(led_delay));
+		break;
+	}
+}
+EXPORT_SYMBOL_GPL(can_led_event);

Apart from this tiny question, the patch looks very clean, as Wolfgang already mentioned.

Kind regards,
Kurt

#17 lore raw
Quoted 5 lines · Kurt Van Dijck
Quoted 3 lines · Fabio Baltieri

This patch implements the functions to add two LED triggers, named <ifname>-tx and <ifname>-rx, to a canbus device driver.

[...]

Hello Kurt,

Quoted 10 lines

If I understand well, this function is subject to maximal optimization.

drivers/net/can/led.c line 41

Quoted 3 lines
drivers/net/can/led.c new file +3 -0
+void can_led_event(struct net_device *netdev, enum can_led_event event)
+{
+	struct can_priv *priv = netdev_priv(netdev);

If the assignment is postponed to withing the differenct cases, thereby repeating this statement 3 times (not 4!), both for RX & TX path a single assignment could be saved.
For stack usage, I'm not an expert, but I suspect a little less stack use. I'm not sure wether it's worth the pain of extra lines of code. I tried to illustrate my point here:

I'm looking at the disassembled function for both cases on ARM. Your version uses some less instructions but I think the optimizer is crunching the whoole thing in some funny way and I'm not sure if it's going to make a difference at all in the end.

Do you know if there are any other part of the kernel which are using similar optimiziations?

Quoted 35 lines
Quoted 35 lines

+++ struct can_led_data *tx_led, *rx_led;

drivers/net/can/led.c new file +34 -0
+
+	switch (event) {
+	case CAN_LED_EVENT_OPEN:
+++		tx_led = priv->tx_led_data;
+		if (likely(tx_led))
+			led_trigger_event(&tx_led->trig, LED_FULL);
+++		rx_led = priv->rx_led_data;
+		if (likely(rx_led))
+			led_trigger_event(&rx_led->trig, LED_FULL);
+		break;
+	case CAN_LED_EVENT_STOP:
+++		tx_led = priv->tx_led_data;
+		if (likely(tx_led)) {
+			del_timer_sync(&tx_led->timer);
+			led_trigger_event(&tx_led->trig, LED_OFF);
+		}
+++		rx_led = priv->rx_led_data;
+		if (likely(rx_led)) {
+			del_timer_sync(&rx_led->timer);
+			led_trigger_event(&rx_led->trig, LED_OFF);
+		}
+		break;
+	case CAN_LED_EVENT_TX:
+++		tx_led = priv->tx_led_data;
+		if (led_delay && likely(tx_led) &&
+		    !timer_pending(&tx_led->timer))
+			mod_timer(&tx_led->timer,
+				jiffies + msecs_to_jiffies(led_delay));
+		break;
+	case CAN_LED_EVENT_RX:
+++		rx_led = priv->rx_led_data;
+		if (led_delay && likely(rx_led) &&
+		    !timer_pending(&rx_led->timer))
+			mod_timer(&rx_led->timer,
Quoted 8 lines

drivers/net/can/led.c line 76

Quoted 5 lines
drivers/net/can/led.c new file +5 -0
+				jiffies + msecs_to_jiffies(led_delay));
+		break;
+	}
+}
+EXPORT_SYMBOL_GPL(can_led_event);

Apart from this tiny question, the patch looks very clean, as Wolfgang already mentioned.

Thanks.

Regards,
Fabio

#18 lore raw
Quoted 20 lines · Fabio Baltieri
Quoted 5 lines · Kurt Van Dijck
Quoted 3 lines · Fabio Baltieri

This patch implements the functions to add two LED triggers, named <ifname>-tx and <ifname>-rx, to a canbus device driver.

[...]

Hello Kurt,

Quoted 10 lines

If I understand well, this function is subject to maximal optimization.

drivers/net/can/led.c line 41

Quoted 3 lines
drivers/net/can/led.c new file +3 -0
+void can_led_event(struct net_device *netdev, enum can_led_event event)
+{
+	struct can_priv *priv = netdev_priv(netdev);

If the assignment is postponed to withing the differenct cases, thereby repeating this statement 3 times (not 4!), both for RX & TX path a single assignment could be saved.
For stack usage, I'm not an expert, but I suspect a little less stack use. I'm not sure wether it's worth the pain of extra lines of code. I tried to illustrate my point here:

Quoted 4 lines

I'm looking at the disassembled function for both cases on ARM. Your version uses some less instructions but I think the optimizer is crunching the whoole thing in some funny way and I'm not sure if it's going to make a difference at all in the end.

If the benefit cannot be proven, I'd stick to the original, as the code is much cleaner :-)

Quoted 2 lines

Do you know if there are any other part of the kernel which are using similar optimiziations?

No, I'm not an expert on code optimization ...

Kurt

#19 lore raw
In reply to Fabio Baltieri

drivers/net/can/led.c line 65

Quoted 10 lines · Fabio Baltieri

This patch implements the functions to add two LED triggers, named <ifname>-tx and <ifname>-rx, to a canbus device driver.

drivers/net/can/led.c new file +5 -0
+void can_led_event(struct net_device *netdev, enum can_led_event event)
 
+	case CAN_LED_EVENT_TX:
+		if (led_delay && likely(tx_led) &&
+		    !timer_pending(&tx_led->timer))
+			mod_timer(&tx_led->timer,

I think that the latter 2 statements need threadsafe protection _OR_ strict serialization from the caller (much more likely). I don't think the code needs change, but it might be worth mentioning this serialization requirement in the header.

Kind regards,
Kurt

#20 lore raw
Quoted 7 lines · Kurt Van Dijck
Quoted 5 lines · Fabio Baltieri

This patch implements the functions to add two LED triggers, named <ifname>-tx and <ifname>-rx, to a canbus device driver.

+void can_led_event(struct net_device *netdev, enum can_led_event event)

Quoted 8 lines

drivers/net/can/led.c line 65

Quoted 4 lines
drivers/net/can/led.c new file +4 -0
+	case CAN_LED_EVENT_TX:
+		if (led_delay && likely(tx_led) &&
+		    !timer_pending(&tx_led->timer))
+			mod_timer(&tx_led->timer,

I think that the latter 2 statements need threadsafe protection _OR_ strict serialization from the caller (much more likely). I don't think the code needs change, but it might be worth mentioning this serialization requirement in the header.

Sure? Each device is going to have individual timer structures, and each timer_list is going to be accessed only from the event function and by the timer handler itself for retriggering.

Also, mod_timer() by itself should be safe.

http://lxr.free-electrons.com/source/kernel/timer.c#L833

Regards,
Fabio

#21 lore raw
Quoted 17 lines · Fabio Baltieri
Quoted 7 lines · Kurt Van Dijck
Quoted 5 lines · Fabio Baltieri

This patch implements the functions to add two LED triggers, named <ifname>-tx and <ifname>-rx, to a canbus device driver.

+void can_led_event(struct net_device *netdev, enum can_led_event event)

Quoted 8 lines

drivers/net/can/led.c line 65

Quoted 4 lines
drivers/net/can/led.c new file +4 -0
+	case CAN_LED_EVENT_TX:
+		if (led_delay && likely(tx_led) &&
+		    !timer_pending(&tx_led->timer))
+			mod_timer(&tx_led->timer,

I think that the latter 2 statements need threadsafe protection _OR_ strict serialization from the caller (much more likely). I don't think the code needs change, but it might be worth mentioning this serialization requirement in the header.

Quoted 5 lines

Sure? Each device is going to have individual timer structures, and each timer_list is going to be accessed only from the event function and by the timer handler itself for retriggering.

Also, mod_timer() by itself should be safe.

_between_ timer_pending() & mod_timer(), which are both
safe I assume, an interrupt may happen.
In such case, it's important that can_led_event(CAN_LED_EVENT_TX)
is only called from either some IRQ function, or the network subsystem's softirq, but NOT from both places.
Any good design will be compliant to this.
Since this depends on actual drivers, I think it may be good
to avoid bad implimentations by mentioning this driver
requirement in the header ...

Kurt

#22 lore raw
Quoted 22 lines · Kurt Van Dijck
Quoted 21 lines · Fabio Baltieri
Quoted 7 lines · Kurt Van Dijck
Quoted 5 lines · Fabio Baltieri

This patch implements the functions to add two LED triggers, named <ifname>-tx and <ifname>-rx, to a canbus device driver.

+void can_led_event(struct net_device *netdev, enum can_led_event event)

Quoted 8 lines

drivers/net/can/led.c line 65

Quoted 4 lines
drivers/net/can/led.c new file +4 -0
+	case CAN_LED_EVENT_TX:
+		if (led_delay && likely(tx_led) &&
+		    !timer_pending(&tx_led->timer))
+			mod_timer(&tx_led->timer,

I think that the latter 2 statements need threadsafe protection _OR_ strict serialization from the caller (much more likely). I don't think the code needs change, but it might be worth mentioning this serialization requirement in the header.

Sure? Each device is going to have individual timer structures, and each timer_list is going to be accessed only from the event function and by the timer handler itself for retriggering.

Quoted 7 lines
Quoted 1 line

Also, mod_timer() by itself should be safe.

_between_ timer_pending() & mod_timer(), which are both
safe I assume, an interrupt may happen.
In such case, it's important that can_led_event(CAN_LED_EVENT_TX)
is only called from either some IRQ function, or the network subsystem's softirq, but NOT from both places.

Ok - but I can't think of a case where you would call this one outside hardirq/softirq context. Either way, I think that the only thing which could happen without locks is to schedule the timer two times, which as I see from mod_timer implementation is already optimized out, so I think that no locking is needed on this one. Am I missing something?

Regards,
Fabio

#23 lore raw
Quoted 31 lines · Fabio Baltieri
Quoted 22 lines · Kurt Van Dijck
Quoted 21 lines · Fabio Baltieri
Quoted 7 lines · Kurt Van Dijck
Quoted 5 lines · Fabio Baltieri

This patch implements the functions to add two LED triggers, named <ifname>-tx and <ifname>-rx, to a canbus device driver.

+void can_led_event(struct net_device *netdev, enum can_led_event event)

Quoted 8 lines

drivers/net/can/led.c line 65

Quoted 4 lines
drivers/net/can/led.c new file +4 -0
+	case CAN_LED_EVENT_TX:
+		if (led_delay && likely(tx_led) &&
+		    !timer_pending(&tx_led->timer))
+			mod_timer(&tx_led->timer,

I think that the latter 2 statements need threadsafe protection _OR_ strict serialization from the caller (much more likely). I don't think the code needs change, but it might be worth mentioning this serialization requirement in the header.

Sure? Each device is going to have individual timer structures, and each timer_list is going to be accessed only from the event function and by the timer handler itself for retriggering.

Quoted 7 lines
Quoted 1 line

Also, mod_timer() by itself should be safe.

_between_ timer_pending() & mod_timer(), which are both
safe I assume, an interrupt may happen.
In such case, it's important that can_led_event(CAN_LED_EVENT_TX)
is only called from either some IRQ function, or the network subsystem's softirq, but NOT from both places.

Quoted 5 lines

Ok - but I can't think of a case where you would call this one outside hardirq/softirq context. Either way, I think that the only thing which could happen without locks is to schedule the timer two times, which as I see from mod_timer implementation is already optimized out, so I think that no locking is needed on this one.

I neither think that locking is needed, for reasons you also mentioned. I did think the proposed way of using can_led_event() is worth describing in the header, so that it's clear that no locking is needed. But maybe I'm mistaken here...

Quoted 1 line

Am I missing something?

no.

Kind regards,
Kurt