can: add tx/rx LED trigger support
PATCH v3 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
---
Hi all!
This is the v3 of my CAN LED trigger patch. It's a major refactoring of the v2 that was discussed about three months ago concluding with the idea that implementing the oneshot triggering code in the LED framework would be a better solution.
This is the old thread for reference:
http://marc.info/?l=linux-can&m=133521499002898&w=2
So, generic oneshot trigger code is now merged in mainline (see 5bb629c), and these are the changes in the v3:
- use the new led_trigger_blink_oneshot() function for LED triggering
- use kasprintf() and led_trigger_{un}register_simple for LED allocation
- add some usage note in the comments
The resulting code is quite simple now and - I think - a bit less intrusive. Still, I hope on some feedback from the community as I don't have that much hardware to test it - this version has been tested mainly on an x86 with a custom usb-can interface.
In 2/2 there is a sample implementation for the flexcan driver, which is basically unchanged from the old version.
Any comments?
Fabio
drivers/net/can/Kconfig | 12 ++++++ drivers/net/can/Makefile | 2 + drivers/net/can/led.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/can/dev.h | 8 ++++ include/linux/can/led.h | 38 +++++++++++++++++++ 5 files changed, 159 insertions(+) create mode 100644 drivers/net/can/led.c create mode 100644 include/linux/can/led.h
drivers/net/can/Kconfig +12 -0
@@ -54,6 +54,18 @@ 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_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
@@ -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 +99 -0
99 lines not on this page · raw message
include/linux/can/dev.h +8 -0
@@ -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,13 @@ struct can_priv {unsigned int echo_skb_max;struct sk_buff **echo_skb; + +#ifdef CONFIG_CAN_LEDS +struct led_trigger *tx_led_trig; +char *tx_led_trig_name; +struct led_trigger *rx_led_trig; +char *rx_led_trig_name; +#endif };/*
include/linux/can/led.h new file +38 -0
@@ -0,0 +1,38 @@ +/* +* Copyright 2012, Fabio Baltieri <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef CAN_LED_H +#define CAN_LED_H + +#include <linux/leds.h> + +enum can_led_event { +CAN_LED_EVENT_OPEN , +CAN_LED_EVENT_STOP , +CAN_LED_EVENT_TX , +CAN_LED_EVENT_RX , +}; + +#ifdef CONFIG_CAN_LEDS +void can_led_event (struct net_device *netdev,enum can_led_event event); +void can_led_init (struct net_device *netdev); +void can_led_exit (struct net_device *netdev); +#else +static inline void can_led_event (struct net_device *netdev, +enum can_led_event event) +{ +} +static inline void can_led_init (struct net_device *netdev) +{ +} +static inline void can_led_exit (struct net_device *netdev) +{ +} +#endif + +#endif
Signature · 1 line
1.7.11.rc1.9.gf623ca1.dirty
PATCH v3 2/2 can: flexcan: add LED trigger support
Add support for canbus activity led indicators on flexcan devices 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 file changed, 13 insertions(+)
drivers/net/can/flexcan.c +13 -0
@@ -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>
@@ -547,6 +548,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 ; }
@@ -635,6 +638,7 @@ static irqreturn_t flexcan_irq(int irq, void *dev_id)if (reg_iflag1 & (1 <<FLEXCAN_TX_BUF_ID )) { stats->tx_bytes +=can_get_echo_skb (dev,0 ); stats->tx_packets++; +can_led_event (dev,CAN_LED_EVENT_TX );flexcan_write ((1 <<FLEXCAN_TX_BUF_ID ), ®s->iflag1);netif_wake_queue (dev); }
@@ -844,6 +848,9 @@ static int flexcan_open(struct net_device *dev) err =flexcan_chip_start (dev);if (err) goto out_close; + +can_led_event (dev,CAN_LED_EVENT_OPEN ); +napi_enable (&priv->napi);netif_start_queue (dev);
@@ -872,6 +879,8 @@ static int flexcan_close(struct net_device *dev)close_candev (dev); +can_led_event (dev,CAN_LED_EVENT_STOP ); +return 0 ; }
@@ -1068,6 +1077,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);
@@ -1091,6 +1102,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.11.rc1.9.gf623ca1.dirty
Marc Kleine-Budde
maintainerQuoted 49 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
Kconfigoption.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_privstructure.Supported drivers should call
can_led_init(),can_led_exit()andcan_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 blinkCc Oliver Hartkopp <[email protected]>Cc Wolfgang Grandegger <[email protected]>Cc Marc Kleine-Budde <[email protected]>Signed-off-by Fabio Baltieri <[email protected]>---
Hi all!
This is the v3 of my CAN LED trigger patch. It's a major refactoring of the v2 that was discussed about three months ago concluding with the idea that implementing the oneshot triggering code in the LED framework would be a better solution.
This is the old thread for reference:
http://marc.info/?l=linux-can&m=133521499002898&w=2So, generic oneshot trigger code is now merged in mainline (see 5bb629c), and these are the changes in the v3:
- use the new
led_trigger_blink_oneshot()function for LED triggering
- usekasprintf()and led_trigger_{un}register_simple for LED allocation
- add some usage note in the commentsThe resulting code is quite simple now and - I think - a bit less intrusive. Still, I hope on some feedback from the community as I don't have that much hardware to test it - this version has been tested mainly on an x86 with a custom usb-can interface.
Quoted 5 lines
In 2/2 there is a sample implementation for the flexcan driver, which is basically unchanged from the old version.
Any comments?
Fabio
Looks quite clean now. Can you provide a devm implementation, too?
Marc
Quoted 124 lines
drivers/net/can/Kconfig | 12 ++++++ drivers/net/can/Makefile | 2 + drivers/net/can/led.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/can/dev.h | 8 ++++ include/linux/can/led.h | 38 +++++++++++++++++++ 5 files changed, 159 insertions(+) create mode 100644 drivers/net/can/led.c create mode 100644 include/linux/can/led.hdrivers/net/can/Kconfig +12
-0@@ -54,6 +54,18 @@ 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_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@@ -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 +72
-0@@ -0,0 +1,99 @@ +/* +* Copyright 2012, Fabio Baltieri <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/netdevice.h> +#include <linux/can/dev.h> + +#include <linux/can/led.h> + +static unsigned long led_delay =50 ; +module_param (led_delay, ulong,0644 ); +MODULE_PARM_DESC (led_delay, +"blink delay time for activity leds (msecs, default: 50)." ); + +/* +* Trigger a LED event in response to a CAN device event +*/ +void can_led_event (struct net_device *netdev,enum can_led_event event) +{ +struct can_priv *priv =netdev_priv (netdev); + +switch (event) { +case CAN_LED_EVENT_OPEN : +led_trigger_event (priv->tx_led_trig,LED_FULL ); +led_trigger_event (priv->rx_led_trig,LED_FULL ); +break ; +case CAN_LED_EVENT_STOP : +led_trigger_event (priv->tx_led_trig,LED_OFF ); +led_trigger_event (priv->rx_led_trig,LED_OFF ); +break ; +case CAN_LED_EVENT_TX : +if (led_delay) +led_trigger_blink_oneshot (priv->tx_led_trig, + &led_delay, &led_delay,1 ); +break ; +case CAN_LED_EVENT_RX : +if (led_delay) +led_trigger_blink_oneshot (priv->rx_led_trig, + &led_delay, &led_delay,1 ); +break ; + } +} +EXPORT_SYMBOL_GPL (can_led_event); + +/* +* Register CAN LED triggers for a CAN device +* +* This is normally called from a driver's probe function +*/ +void can_led_init (struct net_device *netdev) +{ +struct can_priv *priv =netdev_priv (netdev); + + priv->tx_led_trig_name =kasprintf (GFP_KERNEL ,"%s-tx" , netdev->name); +if (!priv->tx_led_trig_name) + goto tx_led_failed; + + priv->rx_led_trig_name =kasprintf (GFP_KERNEL ,"%s-rx" , netdev->name); +if (!priv->rx_led_trig_name) + goto rx_led_failed; + +led_trigger_register_simple (priv->tx_led_trig_name, + &priv->tx_led_trig); +led_trigger_register_simple (priv->rx_led_trig_name, + &priv->rx_led_trig);
drivers/net/can/led.c line 82
Quoted 10 lines
drivers/net/can/led.c new file +10
-0+ +return ; + +rx_led_failed: +kfree (priv->tx_led_trig_name); + priv->tx_led_trig_name =NULL ; +tx_led_failed: +return ; +} +EXPORT_SYMBOL_GPL (can_led_init);
Can you provide a devm implementation for can_led?
Quoted 88 lines
drivers/net/can/led.c new file +17
-0+ +/* +* Unregister CAN LED triggers for a CAN device +* +* This is normally called from a driver's remove function +*/ +void can_led_exit (struct net_device *netdev) +{ +struct can_priv *priv =netdev_priv (netdev); + +led_trigger_unregister_simple (priv->tx_led_trig); +led_trigger_unregister_simple (priv->rx_led_trig); + +kfree (priv->tx_led_trig_name); +kfree (priv->rx_led_trig_name); +} +EXPORT_SYMBOL_GPL (can_led_exit);include/linux/can/dev.h +8
-0@@ -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,13 @@ struct can_priv {unsigned int echo_skb_max;struct sk_buff **echo_skb; + +#ifdef CONFIG_CAN_LEDS +struct led_trigger *tx_led_trig; +char *tx_led_trig_name; +struct led_trigger *rx_led_trig; +char *rx_led_trig_name; +#endif };/* include/linux/can/led.h new file +38
-0@@ -0,0 +1,38 @@ +/* +* Copyright 2012, Fabio Baltieri <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef CAN_LED_H +#define CAN_LED_H + +#include <linux/leds.h> + +enum can_led_event { +CAN_LED_EVENT_OPEN , +CAN_LED_EVENT_STOP , +CAN_LED_EVENT_TX , +CAN_LED_EVENT_RX , +}; + +#ifdef CONFIG_CAN_LEDS +void can_led_event (struct net_device *netdev,enum can_led_event event); +void can_led_init (struct net_device *netdev); +void can_led_exit (struct net_device *netdev); +#else +static inline void can_led_event (struct net_device *netdev, +enum can_led_event event) +{ +} +static inline void can_led_init (struct net_device *netdev) +{ +} +static inline void can_led_exit (struct net_device *netdev) +{ +} +#endif + +#endif
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 |
Hi Marc,
Quoted 187 lines · Marc Kleine-Budde
Quoted 49 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
Kconfigoption.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_privstructure.Supported drivers should call
can_led_init(),can_led_exit()andcan_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 blinkCc Oliver Hartkopp <[email protected]>Cc Wolfgang Grandegger <[email protected]>Cc Marc Kleine-Budde <[email protected]>Signed-off-by Fabio Baltieri <[email protected]>---
Hi all!
This is the v3 of my CAN LED trigger patch. It's a major refactoring of the v2 that was discussed about three months ago concluding with the idea that implementing the oneshot triggering code in the LED framework would be a better solution.
This is the old thread for reference:
http://marc.info/?l=linux-can&m=133521499002898&w=2So, generic oneshot trigger code is now merged in mainline (see 5bb629c), and these are the changes in the v3:
- use the new
led_trigger_blink_oneshot()function for LED triggering
- usekasprintf()and led_trigger_{un}register_simple for LED allocation
- add some usage note in the commentsThe resulting code is quite simple now and - I think - a bit less intrusive. Still, I hope on some feedback from the community as I don't have that much hardware to test it - this version has been tested mainly on an x86 with a custom usb-can interface.
Quoted 5 lines
In 2/2 there is a sample implementation for the flexcan driver, which is basically unchanged from the old version.
Any comments?
FabioLooks quite clean now. Can you provide a devm implementation, too?
Marc
Quoted 128 lines
drivers/net/can/Kconfig | 12 ++++++ drivers/net/can/Makefile | 2 + drivers/net/can/led.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/can/dev.h | 8 ++++ include/linux/can/led.h | 38 +++++++++++++++++++ 5 files changed, 159 insertions(+) create mode 100644 drivers/net/can/led.c create mode 100644 include/linux/can/led.hdrivers/net/can/Kconfig +12
-0@@ -54,6 +54,18 @@ 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_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@@ -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 +76
-0@@ -0,0 +1,99 @@ +/* +* Copyright 2012, Fabio Baltieri <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/netdevice.h> +#include <linux/can/dev.h> + +#include <linux/can/led.h> + +static unsigned long led_delay =50 ; +module_param (led_delay, ulong,0644 ); +MODULE_PARM_DESC (led_delay, +"blink delay time for activity leds (msecs, default: 50)." ); + +/* +* Trigger a LED event in response to a CAN device event +*/ +void can_led_event (struct net_device *netdev,enum can_led_event event) +{ +struct can_priv *priv =netdev_priv (netdev); + +switch (event) { +case CAN_LED_EVENT_OPEN : +led_trigger_event (priv->tx_led_trig,LED_FULL ); +led_trigger_event (priv->rx_led_trig,LED_FULL ); +break ; +case CAN_LED_EVENT_STOP : +led_trigger_event (priv->tx_led_trig,LED_OFF ); +led_trigger_event (priv->rx_led_trig,LED_OFF ); +break ; +case CAN_LED_EVENT_TX : +if (led_delay) +led_trigger_blink_oneshot (priv->tx_led_trig, + &led_delay, &led_delay,1 ); +break ; +case CAN_LED_EVENT_RX : +if (led_delay) +led_trigger_blink_oneshot (priv->rx_led_trig, + &led_delay, &led_delay,1 ); +break ; + } +} +EXPORT_SYMBOL_GPL (can_led_event); + +/* +* Register CAN LED triggers for a CAN device +* +* This is normally called from a driver's probe function +*/ +void can_led_init (struct net_device *netdev) +{ +struct can_priv *priv =netdev_priv (netdev); + + priv->tx_led_trig_name =kasprintf (GFP_KERNEL ,"%s-tx" , netdev->name); +if (!priv->tx_led_trig_name) + goto tx_led_failed; + + priv->rx_led_trig_name =kasprintf (GFP_KERNEL ,"%s-rx" , netdev->name); +if (!priv->rx_led_trig_name) + goto rx_led_failed; + +led_trigger_register_simple (priv->tx_led_trig_name, + &priv->tx_led_trig); +led_trigger_register_simple (priv->rx_led_trig_name, + &priv->rx_led_trig); + +return ; + +rx_led_failed:
Quoted 8 lines
drivers/net/can/led.c line 82
Quoted 6 lines
drivers/net/can/led.c new file +6
-0+kfree (priv->tx_led_trig_name); + priv->tx_led_trig_name =NULL ; +tx_led_failed: +return ; +} +EXPORT_SYMBOL_GPL (can_led_init);Can you provide a devm implementation for
can_led?
Sounds reasonable, you mean like a devm_kasprintf implementation to remove kfree and unwinding code?
Fabio
Quoted 96 lines
Quoted 88 lines
drivers/net/can/led.c new file +17
-0+ +/* +* Unregister CAN LED triggers for a CAN device +* +* This is normally called from a driver's remove function +*/ +void can_led_exit (struct net_device *netdev) +{ +struct can_priv *priv =netdev_priv (netdev); + +led_trigger_unregister_simple (priv->tx_led_trig); +led_trigger_unregister_simple (priv->rx_led_trig); + +kfree (priv->tx_led_trig_name); +kfree (priv->rx_led_trig_name); +} +EXPORT_SYMBOL_GPL (can_led_exit);include/linux/can/dev.h +8
-0@@ -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,13 @@ struct can_priv {unsigned int echo_skb_max;struct sk_buff **echo_skb; + +#ifdef CONFIG_CAN_LEDS +struct led_trigger *tx_led_trig; +char *tx_led_trig_name; +struct led_trigger *rx_led_trig; +char *rx_led_trig_name; +#endif };/* include/linux/can/led.h new file +38
-0@@ -0,0 +1,38 @@ +/* +* Copyright 2012, Fabio Baltieri <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef CAN_LED_H +#define CAN_LED_H + +#include <linux/leds.h> + +enum can_led_event { +CAN_LED_EVENT_OPEN , +CAN_LED_EVENT_STOP , +CAN_LED_EVENT_TX , +CAN_LED_EVENT_RX , +}; + +#ifdef CONFIG_CAN_LEDS +void can_led_event (struct net_device *netdev,enum can_led_event event); +void can_led_init (struct net_device *netdev); +void can_led_exit (struct net_device *netdev); +#else +static inline void can_led_event (struct net_device *netdev, +enum can_led_event event) +{ +} +static inline void can_led_init (struct net_device *netdev) +{ +} +static inline void can_led_exit (struct net_device *netdev) +{ +} +#endif + +#endif 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 |
Marc Kleine-Budde
maintainerQuoted 199 lines · Fabio Baltieri
Hi Marc,
Quoted 187 lines · Marc Kleine-Budde
Quoted 49 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
Kconfigoption.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_privstructure.Supported drivers should call
can_led_init(),can_led_exit()andcan_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 blinkCc Oliver Hartkopp <[email protected]>Cc Wolfgang Grandegger <[email protected]>Cc Marc Kleine-Budde <[email protected]>Signed-off-by Fabio Baltieri <[email protected]>---
Hi all!
This is the v3 of my CAN LED trigger patch. It's a major refactoring of the v2 that was discussed about three months ago concluding with the idea that implementing the oneshot triggering code in the LED framework would be a better solution.
This is the old thread for reference:
http://marc.info/?l=linux-can&m=133521499002898&w=2So, generic oneshot trigger code is now merged in mainline (see 5bb629c), and these are the changes in the v3:
- use the new
led_trigger_blink_oneshot()function for LED triggering
- usekasprintf()and led_trigger_{un}register_simple for LED allocation
- add some usage note in the commentsThe resulting code is quite simple now and - I think - a bit less intrusive. Still, I hope on some feedback from the community as I don't have that much hardware to test it - this version has been tested mainly on an x86 with a custom usb-can interface.
Quoted 5 lines
In 2/2 there is a sample implementation for the flexcan driver, which is basically unchanged from the old version.
Any comments?
FabioLooks quite clean now. Can you provide a devm implementation, too?
Marc
Quoted 128 lines
drivers/net/can/Kconfig | 12 ++++++ drivers/net/can/Makefile | 2 + drivers/net/can/led.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/can/dev.h | 8 ++++ include/linux/can/led.h | 38 +++++++++++++++++++ 5 files changed, 159 insertions(+) create mode 100644 drivers/net/can/led.c create mode 100644 include/linux/can/led.hdrivers/net/can/Kconfig +12
-0@@ -54,6 +54,18 @@ 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_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@@ -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 +76
-0@@ -0,0 +1,99 @@ +/* +* Copyright 2012, Fabio Baltieri <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/netdevice.h> +#include <linux/can/dev.h> + +#include <linux/can/led.h> + +static unsigned long led_delay =50 ; +module_param (led_delay, ulong,0644 ); +MODULE_PARM_DESC (led_delay, +"blink delay time for activity leds (msecs, default: 50)." ); + +/* +* Trigger a LED event in response to a CAN device event +*/ +void can_led_event (struct net_device *netdev,enum can_led_event event) +{ +struct can_priv *priv =netdev_priv (netdev); + +switch (event) { +case CAN_LED_EVENT_OPEN : +led_trigger_event (priv->tx_led_trig,LED_FULL ); +led_trigger_event (priv->rx_led_trig,LED_FULL ); +break ; +case CAN_LED_EVENT_STOP : +led_trigger_event (priv->tx_led_trig,LED_OFF ); +led_trigger_event (priv->rx_led_trig,LED_OFF ); +break ; +case CAN_LED_EVENT_TX : +if (led_delay) +led_trigger_blink_oneshot (priv->tx_led_trig, + &led_delay, &led_delay,1 ); +break ; +case CAN_LED_EVENT_RX : +if (led_delay) +led_trigger_blink_oneshot (priv->rx_led_trig, + &led_delay, &led_delay,1 ); +break ; + } +} +EXPORT_SYMBOL_GPL (can_led_event); + +/* +* Register CAN LED triggers for a CAN device +* +* This is normally called from a driver's probe function +*/ +void can_led_init (struct net_device *netdev) +{ +struct can_priv *priv =netdev_priv (netdev); + + priv->tx_led_trig_name =kasprintf (GFP_KERNEL ,"%s-tx" , netdev->name); +if (!priv->tx_led_trig_name) + goto tx_led_failed; + + priv->rx_led_trig_name =kasprintf (GFP_KERNEL ,"%s-rx" , netdev->name); +if (!priv->rx_led_trig_name) + goto rx_led_failed; + +led_trigger_register_simple (priv->tx_led_trig_name, + &priv->tx_led_trig); +led_trigger_register_simple (priv->rx_led_trig_name, + &priv->rx_led_trig); + +return ; + +rx_led_failed:Quoted 8 lines
drivers/net/can/led.c line 82
Quoted 6 lines
drivers/net/can/led.c new file +6
-0+kfree (priv->tx_led_trig_name); + priv->tx_led_trig_name =NULL ; +tx_led_failed: +return ; +} +EXPORT_SYMBOL_GPL (can_led_init);Can you provide a devm implementation for
can_led?
Quoted 2 lines
Sounds reasonable, you mean like a
devm_kasprintfimplementation to remove kfree and unwinding code?
IMHO it would be sufficient if you implement the devm cleanup functions here.
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 |
Hi Fabio,
Quoted 34 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
Kconfigoption.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_privstructure.Supported drivers should call
can_led_init(),can_led_exit()andcan_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 blinkCc Oliver Hartkopp <[email protected]>Cc Wolfgang Grandegger <[email protected]>Cc Marc Kleine-Budde <[email protected]>Signed-off-by Fabio Baltieri <[email protected]>---
Hi all!
This is the v3 of my CAN LED trigger patch. It's a major refactoring of the v2 that was discussed about three months ago concluding with the idea that implementing the oneshot triggering code in the LED framework would be a better solution.
Quoted 5 lines
This is the old thread for reference:
http://marc.info/?l=linux-can&m=133521499002898&w=2So, generic oneshot trigger code is now merged in mainline (see 5bb629c), and these are the changes in the v3:
Nice, thanks for your patience (and not giving up).
Quoted 120 lines
- use the new
led_trigger_blink_oneshot()function for LED triggering
- usekasprintf()and led_trigger_{un}register_simple for LED allocation
- add some usage note in the commentsThe resulting code is quite simple now and - I think - a bit less intrusive. Still, I hope on some feedback from the community as I don't have that much hardware to test it - this version has been tested mainly on an x86 with a custom usb-can interface.
In 2/2 there is a sample implementation for the flexcan driver, which is basically unchanged from the old version.
Any comments?
Fabiodrivers/net/can/Kconfig | 12 ++++++ drivers/net/can/Makefile | 2 + drivers/net/can/led.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/can/dev.h | 8 ++++ include/linux/can/led.h | 38 +++++++++++++++++++ 5 files changed, 159 insertions(+) create mode 100644 drivers/net/can/led.c create mode 100644 include/linux/can/led.hdrivers/net/can/Kconfig +12
-0@@ -54,6 +54,18 @@ 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_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@@ -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 +53
-0@@ -0,0 +1,99 @@ +/* +* Copyright 2012, Fabio Baltieri <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/netdevice.h> +#include <linux/can/dev.h> + +#include <linux/can/led.h> + +static unsigned long led_delay =50 ; +module_param (led_delay, ulong,0644 ); +MODULE_PARM_DESC (led_delay, +"blink delay time for activity leds (msecs, default: 50)." ); + +/* +* Trigger a LED event in response to a CAN device event +*/ +void can_led_event (struct net_device *netdev,enum can_led_event event) +{ +struct can_priv *priv =netdev_priv (netdev); + +switch (event) { +case CAN_LED_EVENT_OPEN : +led_trigger_event (priv->tx_led_trig,LED_FULL ); +led_trigger_event (priv->rx_led_trig,LED_FULL ); +break ; +case CAN_LED_EVENT_STOP : +led_trigger_event (priv->tx_led_trig,LED_OFF ); +led_trigger_event (priv->rx_led_trig,LED_OFF ); +break ; +case CAN_LED_EVENT_TX : +if (led_delay) +led_trigger_blink_oneshot (priv->tx_led_trig, + &led_delay, &led_delay,1 ); +break ; +case CAN_LED_EVENT_RX : +if (led_delay) +led_trigger_blink_oneshot (priv->rx_led_trig, + &led_delay, &led_delay,1 ); +break ; + } +} +EXPORT_SYMBOL_GPL (can_led_event); + +/* +* Register CAN LED triggers for a CAN device
drivers/net/can/led.c line 63
Quoted 10 lines
drivers/net/can/led.c new file +10
-0+* +* This is normally called from a driver's probe function +*/ +void can_led_init (struct net_device *netdev) +{ +struct can_priv *priv =netdev_priv (netdev); + + priv->tx_led_trig_name =kasprintf (GFP_KERNEL ,"%s-tx" , netdev->name); +if (!priv->tx_led_trig_name) + goto tx_led_failed;
Just return here?
Quoted 6 lines
drivers/net/can/led.c new file +6
-0+ priv->rx_led_trig_name =kasprintf (GFP_KERNEL ,"%s-rx" , netdev->name); +if (!priv->rx_led_trig_name) + goto rx_led_failed; + +led_trigger_register_simple (priv->tx_led_trig_name, + &priv->tx_led_trig);
drivers/net/can/led.c line 80
Quoted 10 lines
drivers/net/can/led.c new file +10
-0+led_trigger_register_simple (priv->rx_led_trig_name, + &priv->rx_led_trig); + +return ; + +rx_led_failed: +kfree (priv->tx_led_trig_name); + priv->tx_led_trig_name =NULL ; +tx_led_failed: +return ;
In case of error the function returns silently. Is this by purpose? What happens if CAN_LEDS is enabled but no LEDs are assigned?
Quoted 32 lines
drivers/net/can/led.c new file +19
-0+} +EXPORT_SYMBOL_GPL (can_led_init); + +/* +* Unregister CAN LED triggers for a CAN device +* +* This is normally called from a driver's remove function +*/ +void can_led_exit (struct net_device *netdev) +{ +struct can_priv *priv =netdev_priv (netdev); + +led_trigger_unregister_simple (priv->tx_led_trig); +led_trigger_unregister_simple (priv->rx_led_trig); + +kfree (priv->tx_led_trig_name); +kfree (priv->rx_led_trig_name); +} +EXPORT_SYMBOL_GPL (can_led_exit);include/linux/can/dev.h +1
-0@@ -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
include/linux/can/dev.h struct can_priv { · line 62
Quoted 10 lines
include/linux/can/dev.h +7
-0@@ -52,6 +53,13 @@ struct can_priv {unsigned int echo_skb_max;struct sk_buff **echo_skb; + +#ifdef CONFIG_CAN_LEDS +struct led_trigger *tx_led_trig; +char *tx_led_trig_name; +struct led_trigger *rx_led_trig; +char *rx_led_trig_name; +#endif
Do we need to store the names?
Wolfgang.
Marc Kleine-Budde
maintainerQuoted 189 lines · Wolfgang Grandegger
Hi Fabio,
Quoted 34 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
Kconfigoption.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_privstructure.Supported drivers should call
can_led_init(),can_led_exit()andcan_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 blinkCc Oliver Hartkopp <[email protected]>Cc Wolfgang Grandegger <[email protected]>Cc Marc Kleine-Budde <[email protected]>Signed-off-by Fabio Baltieri <[email protected]>---
Hi all!
This is the v3 of my CAN LED trigger patch. It's a major refactoring of the v2 that was discussed about three months ago concluding with the idea that implementing the oneshot triggering code in the LED framework would be a better solution.
Quoted 5 lines
This is the old thread for reference:
http://marc.info/?l=linux-can&m=133521499002898&w=2So, generic oneshot trigger code is now merged in mainline (see 5bb629c), and these are the changes in the v3:
Nice, thanks for your patience (and not giving up).
Quoted 120 lines
- use the new
led_trigger_blink_oneshot()function for LED triggering
- usekasprintf()and led_trigger_{un}register_simple for LED allocation
- add some usage note in the commentsThe resulting code is quite simple now and - I think - a bit less intrusive. Still, I hope on some feedback from the community as I don't have that much hardware to test it - this version has been tested mainly on an x86 with a custom usb-can interface.
In 2/2 there is a sample implementation for the flexcan driver, which is basically unchanged from the old version.
Any comments?
Fabiodrivers/net/can/Kconfig | 12 ++++++ drivers/net/can/Makefile | 2 + drivers/net/can/led.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/can/dev.h | 8 ++++ include/linux/can/led.h | 38 +++++++++++++++++++ 5 files changed, 159 insertions(+) create mode 100644 drivers/net/can/led.c create mode 100644 include/linux/can/led.hdrivers/net/can/Kconfig +12
-0@@ -54,6 +54,18 @@ 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_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@@ -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 +53
-0@@ -0,0 +1,99 @@ +/* +* Copyright 2012, Fabio Baltieri <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/netdevice.h> +#include <linux/can/dev.h> + +#include <linux/can/led.h> + +static unsigned long led_delay =50 ; +module_param (led_delay, ulong,0644 ); +MODULE_PARM_DESC (led_delay, +"blink delay time for activity leds (msecs, default: 50)." ); + +/* +* Trigger a LED event in response to a CAN device event +*/ +void can_led_event (struct net_device *netdev,enum can_led_event event) +{ +struct can_priv *priv =netdev_priv (netdev); + +switch (event) { +case CAN_LED_EVENT_OPEN : +led_trigger_event (priv->tx_led_trig,LED_FULL ); +led_trigger_event (priv->rx_led_trig,LED_FULL ); +break ; +case CAN_LED_EVENT_STOP : +led_trigger_event (priv->tx_led_trig,LED_OFF ); +led_trigger_event (priv->rx_led_trig,LED_OFF ); +break ; +case CAN_LED_EVENT_TX : +if (led_delay) +led_trigger_blink_oneshot (priv->tx_led_trig, + &led_delay, &led_delay,1 ); +break ; +case CAN_LED_EVENT_RX : +if (led_delay) +led_trigger_blink_oneshot (priv->rx_led_trig, + &led_delay, &led_delay,1 ); +break ; + } +} +EXPORT_SYMBOL_GPL (can_led_event); + +/* +* Register CAN LED triggers for a CAN device drivers/net/can/led.c line 63
Quoted 10 lines
drivers/net/can/led.c new file +10
-0+* +* This is normally called from a driver's probe function +*/ +void can_led_init (struct net_device *netdev) +{ +struct can_priv *priv =netdev_priv (netdev); + + priv->tx_led_trig_name =kasprintf (GFP_KERNEL ,"%s-tx" , netdev->name); +if (!priv->tx_led_trig_name) + goto tx_led_failed;Just return here?
Quoted 11 lines
drivers/net/can/led.c new file +11
-0+ priv->rx_led_trig_name =kasprintf (GFP_KERNEL ,"%s-rx" , netdev->name); +if (!priv->rx_led_trig_name) + goto rx_led_failed; + +led_trigger_register_simple (priv->tx_led_trig_name, + &priv->tx_led_trig); +led_trigger_register_simple (priv->rx_led_trig_name, + &priv->rx_led_trig); + +return ; +
Quoted 8 lines
drivers/net/can/led.c line 80
Quoted 5 lines
drivers/net/can/led.c new file +5
-0+rx_led_failed: +kfree (priv->tx_led_trig_name); + priv->tx_led_trig_name =NULL ; +tx_led_failed: +return ;In case of error the function returns silently. Is this by purpose? What happens if
CAN_LEDSis enabled but no LEDs are assigned?
It's a bit strange, but led_trigger_register_simple() can fail silently, too. Or better it has no return value, but does a warning printk in case of an error.
Quoted 34 lines
Quoted 34 lines
drivers/net/can/led.c new file +19
-0+} +EXPORT_SYMBOL_GPL (can_led_init); + +/* +* Unregister CAN LED triggers for a CAN device +* +* This is normally called from a driver's remove function +*/ +void can_led_exit (struct net_device *netdev) +{ +struct can_priv *priv =netdev_priv (netdev); + +led_trigger_unregister_simple (priv->tx_led_trig); +led_trigger_unregister_simple (priv->rx_led_trig); + +kfree (priv->tx_led_trig_name); +kfree (priv->rx_led_trig_name); +} +EXPORT_SYMBOL_GPL (can_led_exit);include/linux/can/dev.h +1
-0@@ -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,13 @@ struct can_priv {
Quoted 11 lines
include/linux/can/dev.h struct can_priv { · line 62
Quoted 9 lines
include/linux/can/dev.h +7
-0unsigned int echo_skb_max;struct sk_buff **echo_skb; + +#ifdef CONFIG_CAN_LEDS +struct led_trigger *tx_led_trig; +char *tx_led_trig_name; +struct led_trigger *rx_led_trig; +char *rx_led_trig_name; +#endif Do we need to store the names?
Yes, Seems, so the name is not copied:
http://lxr.free-electrons.com/source/drivers/leds/led-triggers.c#L253
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 |
Quoted 6 lines · Marc Kleine-Budde
Quoted 6 lines
Quoted 6 lines
drivers/net/can/led.c new file +6
-0+/* +* Register CAN LED triggers for a CAN device +* +* This is normally called from a driver's probe function +*/ +void can_led_init (struct net_device *netdev)
Quoted 8 lines
Quoted 8 lines
drivers/net/can/led.c line 63
Quoted 6 lines
drivers/net/can/led.c new file +6
-0+{ +struct can_priv *priv =netdev_priv (netdev); + + priv->tx_led_trig_name =kasprintf (GFP_KERNEL ,"%s-tx" , netdev->name); +if (!priv->tx_led_trig_name) + goto tx_led_failed;Just return here?
Right.
Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
drivers/net/can/led.c new file +11
-0+ priv->rx_led_trig_name =kasprintf (GFP_KERNEL ,"%s-rx" , netdev->name); +if (!priv->rx_led_trig_name) + goto rx_led_failed; + +led_trigger_register_simple (priv->tx_led_trig_name, + &priv->tx_led_trig); +led_trigger_register_simple (priv->rx_led_trig_name, + &priv->rx_led_trig); + +return ; +Quoted 8 lines
drivers/net/can/led.c line 80
Quoted 5 lines
drivers/net/can/led.c new file +5
-0+rx_led_failed: +kfree (priv->tx_led_trig_name); + priv->tx_led_trig_name =NULL ; +tx_led_failed: +return ;In case of error the function returns silently. Is this by purpose? What happens if
CAN_LEDSis enabled but no LEDs are assigned?
Quoted 3 lines
It's a bit strange, but
led_trigger_register_simple()can fail silently, too. Or better it has no return value, but does a warning printk in case of an error.
Well, that's in line with the behviour of leds trigger registration in other subsystems out there (mac80211 and power_supply for instance) but now that you pointed it out, I agree that this is not really nice to the user. led_trigger_register_simple already has a printk to KERN_ERR, I may add another one in the error path (if we keep the kasprintf).
Quoted 47 lines
Quoted 35 lines
Quoted 35 lines
drivers/net/can/led.c new file +19
-0+} +EXPORT_SYMBOL_GPL (can_led_init); + +/* +* Unregister CAN LED triggers for a CAN device +* +* This is normally called from a driver's remove function +*/ +void can_led_exit (struct net_device *netdev) +{ +struct can_priv *priv =netdev_priv (netdev); + +led_trigger_unregister_simple (priv->tx_led_trig); +led_trigger_unregister_simple (priv->rx_led_trig); + +kfree (priv->tx_led_trig_name); +kfree (priv->rx_led_trig_name); +} +EXPORT_SYMBOL_GPL (can_led_exit);include/linux/can/dev.h +1
-0@@ -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,13 @@ struct can_priv {Quoted 11 lines
include/linux/can/dev.h struct can_priv { · line 62
Quoted 9 lines
include/linux/can/dev.h +7
-0unsigned int echo_skb_max;struct sk_buff **echo_skb; + +#ifdef CONFIG_CAN_LEDS +struct led_trigger *tx_led_trig; +char *tx_led_trig_name; +struct led_trigger *rx_led_trig; +char *rx_led_trig_name; +#endif Do we need to store the names?
Quoted 5 lines
Yes, Seems, so the name is not copied:
http://lxr.free-electrons.com/source/drivers/leds/led-triggers.c#L253
Marc
Actually we may try to exploit struct led_trigger to get back the pointers, but then we have to free the names before calling led_trigger_unregister, and that's going to be race against led_trigger_show(). Anyway, those pointers would go away using a devm-based allocation, so I'll keep that in mind.
Thanks,
Fabio
Quoted 34 lines · Marc Kleine-Budde
Quoted 34 lines
Quoted 33 lines
Quoted 21 lines
drivers/net/can/led.c new file +21
-0+/* +* Register CAN LED triggers for a CAN device +* +* This is normally called from a driver's probe function +*/ +void can_led_init (struct net_device *netdev) +{ +struct can_priv *priv =netdev_priv (netdev); + + priv->tx_led_trig_name =kasprintf (GFP_KERNEL ,"%s-tx" , netdev->name); +if (!priv->tx_led_trig_name) + goto tx_led_failed; + + priv->rx_led_trig_name =kasprintf (GFP_KERNEL ,"%s-rx" , netdev->name); +if (!priv->rx_led_trig_name) + goto rx_led_failed; + +led_trigger_register_simple (priv->tx_led_trig_name, + &priv->tx_led_trig); +led_trigger_register_simple (priv->rx_led_trig_name, + &priv->rx_led_trig);drivers/net/can/led.c line 82
Quoted 10 lines
drivers/net/can/led.c new file +10
-0+ +return ; + +rx_led_failed: +kfree (priv->tx_led_trig_name); + priv->tx_led_trig_name =NULL ; +tx_led_failed: +return ; +} +EXPORT_SYMBOL_GPL (can_led_init);Can you provide a devm implementation for
can_led?
Quoted 5 lines
Quoted 2 lines
Sounds reasonable, you mean like a
devm_kasprintfimplementation to remove kfree and unwinding code?IMHO it would be sufficient if you implement the devm cleanup functions here.
Uh - can you be more specific? I mean, are you suggesting to just convert the code to something like:
unsigned int len;
char *p;
len = snprintf(NULL, 0, "%s-tx", netdev->name); p = devm_kzalloc(&netdev->dev, len + 1, GFP_KERNEL); if (!p)
return -ENOMEM;sprintf(p, len + 1, "%s-tx", netdev->name);
or to implement something with devres_alloc() and a specific release function?
Fabio
Marc Kleine-Budde
maintainerOn 07/31/2012 01:57 PM, Fabio Baltieri wrote:
[...]
Quoted 11 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Can you provide a devm implementation for
can_led?Sounds reasonable, you mean like a
devm_kasprintfimplementation to remove kfree and unwinding code?IMHO it would be sufficient if you implement the devm cleanup functions here.
Uh - can you be more specific? I mean, are you suggesting to just convert the code to something like:
Quoted 8 lines
unsigned int len;
char *p;len =
snprintf(NULL, 0, "%s-tx", netdev->name); p =devm_kzalloc(&netdev->dev, len + 1, GFP_KERNEL); if (!p)
return-ENOMEM;sprintf(p, len + 1, "%s-tx", netdev->name);
This would work, if you just have to free both names, but...
Quoted 2 lines
or to implement something with
devres_alloc()and a specific release function?
...you have to call led_trigger_unregister_simple(), so we need a custom release function.
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 |
Quoted 37 lines · Wolfgang Grandegger
Hi Fabio,
Quoted 34 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
Kconfigoption.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_privstructure.Supported drivers should call
can_led_init(),can_led_exit()andcan_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 blinkCc Oliver Hartkopp <[email protected]>Cc Wolfgang Grandegger <[email protected]>Cc Marc Kleine-Budde <[email protected]>Signed-off-by Fabio Baltieri <[email protected]>---
Hi all!
This is the v3 of my CAN LED trigger patch. It's a major refactoring of the v2 that was discussed about three months ago concluding with the idea that implementing the oneshot triggering code in the LED framework would be a better solution.
Quoted 7 lines
Quoted 5 lines
This is the old thread for reference:
http://marc.info/?l=linux-can&m=133521499002898&w=2So, generic oneshot trigger code is now merged in mainline (see 5bb629c), and these are the changes in the v3:
Nice, thanks for your patience (and not giving up).
Thanks to you for the review... no intention of giving up, I'm learning a lot from this! ;-)
Fabio
PATCH v4 1/1 can: add tx/rx LED trigger support
In reply to Marc Kleine-BuddeThis 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() and can_led_event() as needed.
Cleanup is handled automatically by devres, so no *_exit function is 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
---
Hi!
So, here is the v4.
Changes against v3:
- add a netdev_err message in kasprintf error path
- implement a devres_alloc release function to de-allocate strings and trigger
- can_led_exit is now unneeded and gone
Is that what you had in mind Marc?
I like the devres implementation, as callers don't have to care about exit function anymore... still it looks like a bit of an hack to me.
Also, I made an initial version with the four pointers in a separate can_led struct to be allocated as a payload of devres_alloc - but I think this one looks cleaner and is more cache-friendly.
What you think about this?
Fabio
drivers/net/can/Kconfig | 12 ++++++ drivers/net/can/Makefile | 2 + drivers/net/can/led.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++ include/linux/can/dev.h | 8 ++++ include/linux/can/led.h | 34 ++++++++++++++++ 5 files changed, 158 insertions(+) create mode 100644 drivers/net/can/led.c create mode 100644 include/linux/can/led.h
drivers/net/can/Kconfig +12 -0
@@ -54,6 +54,18 @@ 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_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
@@ -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 +102 -0
102 lines not on this page · raw message
include/linux/can/dev.h +8 -0
@@ -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,13 @@ struct can_priv {unsigned int echo_skb_max;struct sk_buff **echo_skb; + +#ifdef CONFIG_CAN_LEDS +struct led_trigger *tx_led_trig; +char *tx_led_trig_name; +struct led_trigger *rx_led_trig; +char *rx_led_trig_name; +#endif };/*
include/linux/can/led.h new file +34 -0
@@ -0,0 +1,34 @@ +/* +* Copyright 2012, Fabio Baltieri <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef CAN_LED_H +#define CAN_LED_H + +#include <linux/leds.h> + +enum can_led_event { +CAN_LED_EVENT_OPEN , +CAN_LED_EVENT_STOP , +CAN_LED_EVENT_TX , +CAN_LED_EVENT_RX , +}; + +#ifdef CONFIG_CAN_LEDS +void can_led_event (struct net_device *netdev,enum can_led_event event); +void can_led_init (struct net_device *netdev); +#else +static inline void can_led_event (struct net_device *netdev, +enum can_led_event event) +{ +} +static inline void can_led_init (struct net_device *netdev) +{ +} +#endif + +#endif
Signature · 1 line
1.7.11.rc1.9.gf623ca1.dirty
Marc Kleine-Budde
maintainerQuoted 32 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
Kconfigoption.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_privstructure.Supported drivers should call
can_led_init()andcan_led_event()as needed.Cleanup is handled automatically by devres, so no
*_exitfunction is 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 blinkCc Oliver Hartkopp <[email protected]>Cc Wolfgang Grandegger <[email protected]>Cc Marc Kleine-Budde <[email protected]>Signed-off-by Fabio Baltieri <[email protected]>---
Hi!
Quoted 8 lines
So, here is the v4.
Changes against v3:
- add anetdev_errmessage in kasprintf error path
- implement adevres_allocrelease function to de-allocate strings and trigger
-can_led_exitis now unneeded and goneIs that what you had in mind Marc?
Yes, that's it. Can you add a devm_ prefix to can_led_init() please.
Quoted 2 lines
I like the devres implementation, as callers don't have to care about exit function anymore... still it looks like a bit of an hack to me.
No :)
Quoted 3 lines
Also, I made an initial version with the four pointers in a separate
can_ledstruct to be allocated as a payload ofdevres_alloc- but I think this one looks cleaner and is more cache-friendly.
If you allocate the 4 pointers in a separate struct you only save 3 points, as you need the pointer to the struct anyways.
Quoted 1 line
What you think about this?
Looks good. See comment inline,
Marc
Quoted 115 lines
drivers/net/can/Kconfig | 12 ++++++ drivers/net/can/Makefile | 2 + drivers/net/can/led.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++ include/linux/can/dev.h | 8 ++++ include/linux/can/led.h | 34 ++++++++++++++++ 5 files changed, 158 insertions(+) create mode 100644 drivers/net/can/led.c create mode 100644 include/linux/can/led.hdrivers/net/can/Kconfig +12
-0@@ -54,6 +54,18 @@ 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_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@@ -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 +64
-0@@ -0,0 +1,102 @@ +/* +* Copyright 2012, Fabio Baltieri <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#include <linux/module.h> +#include <linux/device.h> +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/netdevice.h> +#include <linux/can/dev.h> + +#include <linux/can/led.h> + +static unsigned long led_delay =50 ; +module_param (led_delay, ulong,0644 ); +MODULE_PARM_DESC (led_delay, +"blink delay time for activity leds (msecs, default: 50)." ); + +/* +* Trigger a LED event in response to a CAN device event +*/ +void can_led_event (struct net_device *netdev,enum can_led_event event) +{ +struct can_priv *priv =netdev_priv (netdev); + +switch (event) { +case CAN_LED_EVENT_OPEN : +led_trigger_event (priv->tx_led_trig,LED_FULL ); +led_trigger_event (priv->rx_led_trig,LED_FULL ); +break ; +case CAN_LED_EVENT_STOP : +led_trigger_event (priv->tx_led_trig,LED_OFF ); +led_trigger_event (priv->rx_led_trig,LED_OFF ); +break ; +case CAN_LED_EVENT_TX : +if (led_delay) +led_trigger_blink_oneshot (priv->tx_led_trig, + &led_delay, &led_delay,1 ); +break ; +case CAN_LED_EVENT_RX : +if (led_delay) +led_trigger_blink_oneshot (priv->rx_led_trig, + &led_delay, &led_delay,1 ); +break ; + } +} +EXPORT_SYMBOL_GPL (can_led_event); + +static void can_led_release (struct device *gendev,void *res) +{ +struct can_priv *priv =netdev_priv (to_net_dev (gendev)); + +led_trigger_unregister_simple (priv->tx_led_trig); +led_trigger_unregister_simple (priv->rx_led_trig); + +kfree (priv->tx_led_trig_name); +kfree (priv->rx_led_trig_name); +} + +/*
drivers/net/can/led.c line 74
Quoted 10 lines
drivers/net/can/led.c new file +10
-0+* Register CAN LED triggers for a CAN device +* +* This is normally called from a driver's probe function +*/ +void can_led_init (struct net_device *netdev) +{ +struct can_priv *priv =netdev_priv (netdev); +void *res; + + res =devres_alloc (can_led_release,0 ,GFP_KERNEL );
^
I'm not really sure if this is working. For example, pinctrl [1] allocates a double pointer here. The res pointer here and in can_led_release simply points to invalid memory. But as long as you don't dereference it, it should work.
[1] http://lxr.free-electrons.com/source/drivers/pinctrl/core.c#L862
Quoted 18 lines
drivers/net/can/led.c new file +18
-0+if (!res) + goto err_out; + + priv->tx_led_trig_name =kasprintf (GFP_KERNEL ,"%s-tx" , netdev->name); +if (!priv->tx_led_trig_name) + goto err_out; + + priv->rx_led_trig_name =kasprintf (GFP_KERNEL ,"%s-rx" , netdev->name); +if (!priv->rx_led_trig_name) + goto err_out_rx; + +led_trigger_register_simple (priv->tx_led_trig_name, + &priv->tx_led_trig); +led_trigger_register_simple (priv->rx_led_trig_name, + &priv->rx_led_trig); + +devres_add (&netdev->dev, res); +
drivers/net/can/led.c line 102
Quoted 10 lines
drivers/net/can/led.c new file +10
-0+return ; + +err_out_rx: +kfree (priv->tx_led_trig_name); + priv->tx_led_trig_name =NULL ; +err_out: +netdev_err (netdev,"cannot register LED triggers\n" ); +devres_free (res); +} +EXPORT_SYMBOL_GPL (can_led_init);
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 |
Marc Kleine-Budde
maintainerOn 08/01/2012 11:36 AM, Marc Kleine-Budde wrote:
[...]
Quoted 12 lines
drivers/net/can/led.c line 74
Quoted 11 lines
drivers/net/can/led.c new file +11
-0+/* +* Register CAN LED triggers for a CAN device +* +* This is normally called from a driver's probe function +*/ +void can_led_init (struct net_device *netdev) +{ +struct can_priv *priv =netdev_priv (netdev); +void *res; + + res =devres_alloc (can_led_release,0 ,GFP_KERNEL );^
Quoted 6 lines
I'm not really sure if this is working. For example, pinctrl [1] allocates a double pointer here. The res pointer here and in
can_led_releasesimply points to invalid memory. But as long as you don't dereference it, it should work.[1] http://lxr.free-electrons.com/source/drivers/pinctrl/core.c#L862
The pinctrl usecase if different, pinctrl needs that extra memory because they cannot get a reference to their pinctrl they have to put.
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 |
Quoted 35 lines · Marc Kleine-Budde
Quoted 34 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
Kconfigoption.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_privstructure.Supported drivers should call
can_led_init()andcan_led_event()as needed.Cleanup is handled automatically by devres, so no
*_exitfunction is 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 blinkCc Oliver Hartkopp <[email protected]>Cc Wolfgang Grandegger <[email protected]>Cc Marc Kleine-Budde <[email protected]>Signed-off-by Fabio Baltieri <[email protected]>---
Hi!
So, here is the v4.
Quoted 8 lines
Quoted 6 lines
Changes against v3:
- add anetdev_errmessage in kasprintf error path
- implement adevres_allocrelease function to de-allocate strings and trigger
-can_led_exitis now unneeded and goneIs that what you had in mind Marc?
Yes, that's it. Can you add a
devm_prefix tocan_led_init()please.
Okay.
Quoted 5 lines
Quoted 3 lines
I like the devres implementation, as callers don't have to care about exit function anymore... still it looks like a bit of an hack to me.
No :)
Fair enough :)
Quoted 25 lines
Quoted 3 lines
Also, I made an initial version with the four pointers in a separate
can_ledstruct to be allocated as a payload ofdevres_alloc- but I think this one looks cleaner and is more cache-friendly.If you allocate the 4 pointers in a separate struct you only save 3 points, as you need the pointer to the struct anyways.
Quoted 1 line
What you think about this?
Looks good. See comment inline,
Marc
drivers/net/can/led.c line 74
Quoted 11 lines
drivers/net/can/led.c new file +11
-0+/* +* Register CAN LED triggers for a CAN device +* +* This is normally called from a driver's probe function +*/ +void can_led_init (struct net_device *netdev) +{ +struct can_priv *priv =netdev_priv (netdev); +void *res; + + res =devres_alloc (can_led_release,0 ,GFP_KERNEL );^
Quoted 6 lines
I'm not really sure if this is working. For example, pinctrl [1] allocates a double pointer here. The res pointer here and in
can_led_releasesimply points to invalid memory. But as long as you don't dereference it, it should work.[1] http://lxr.free-electrons.com/source/drivers/pinctrl/core.c#L862
Actually that's also used by libata-core (not in lxr yet) and dma-mapping:
http://lxr.free-electrons.com/source/drivers/base/dma-mapping.c#L193
actually, res should point at the end of some internal devres structure, and is only used as return value in this case.
Of course, in this case the release function can only use the struct device pointer.
I've run some fail test and ftraced the whole thing and it seems to work pretty good! I'll send a v5 with the rename later.
Thanks,
Fabio
Marc Kleine-Budde
maintainerOn 08/01/2012 12:30 PM, Fabio Baltieri wrote:
[...]
Quoted 18 lines
Quoted 7 lines
drivers/net/can/led.c line 74
Quoted 6 lines
drivers/net/can/led.c new file +6
-0+void can_led_init (struct net_device *netdev) +{ +struct can_priv *priv =netdev_priv (netdev); +void *res; + + res =devres_alloc (can_led_release,0 ,GFP_KERNEL );^Quoted 6 lines
I'm not really sure if this is working. For example, pinctrl [1] allocates a double pointer here. The res pointer here and in
can_led_releasesimply points to invalid memory. But as long as you don't dereference it, it should work.[1] http://lxr.free-electrons.com/source/drivers/pinctrl/core.c#L862
Actually that's also used by libata-core (not in lxr yet) and dma-mapping:
http://lxr.free-electrons.com/source/drivers/base/dma-mapping.c#L193
Quoted 8 lines
actually, res should point at the end of some internal devres structure, and is only used as return value in this case.
Of course, in this case the release function can only use the struct device pointer.
I've run some fail test and ftraced the whole thing and it seems to work pretty good! I'll send a v5 with the rename later.
Thanks for checking. I just noticed, the pinctrl usecase is a bit different.
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 |
PATCH v5 1/2 can: add tx/rx LED trigger support
In reply to Marc Kleine-BuddeThis 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 devm_can_led_init() and can_led_event() as needed.
Cleanup is handled automatically by devres, so no *_exit function is 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,
so, v5, renamed can_led_init to devm_can_led_init like other devres based functions.
Also, I'm reposting implementation into flexcan in 2/2... I have a small patchset for others embedded can drivers to be tested which I can post in another thread if this patch is accepted.
Fabio
drivers/net/can/Kconfig | 12 ++++++ drivers/net/can/Makefile | 2 + drivers/net/can/led.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++ include/linux/can/dev.h | 8 ++++ include/linux/can/led.h | 34 ++++++++++++++++ 5 files changed, 158 insertions(+) create mode 100644 drivers/net/can/led.c create mode 100644 include/linux/can/led.h
drivers/net/can/Kconfig +12 -0
@@ -54,6 +54,18 @@ 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_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
@@ -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 +102 -0
102 lines not on this page · raw message
include/linux/can/dev.h +8 -0
@@ -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,13 @@ struct can_priv {unsigned int echo_skb_max;struct sk_buff **echo_skb; + +#ifdef CONFIG_CAN_LEDS +struct led_trigger *tx_led_trig; +char *tx_led_trig_name; +struct led_trigger *rx_led_trig; +char *rx_led_trig_name; +#endif };/*
include/linux/can/led.h new file +34 -0
@@ -0,0 +1,34 @@ +/* +* Copyright 2012, Fabio Baltieri <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef CAN_LED_H +#define CAN_LED_H + +#include <linux/leds.h> + +enum can_led_event { +CAN_LED_EVENT_OPEN , +CAN_LED_EVENT_STOP , +CAN_LED_EVENT_TX , +CAN_LED_EVENT_RX , +}; + +#ifdef CONFIG_CAN_LEDS +void can_led_event (struct net_device *netdev,enum can_led_event event); +void devm_can_led_init (struct net_device *netdev); +#else +static inline void can_led_event (struct net_device *netdev, +enum can_led_event event) +{ +} +static inline void devm_can_led_init (struct net_device *netdev) +{ +} +#endif + +#endif
Signature · 1 line
1.7.11.rc1.9.gf623ca1.dirty
PATCH v5 2/2 can: flexcan: add LED trigger support
Add support for canbus activity led indicators on flexcan devices 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 | 11 +++++++++++ 1 file changed, 11 insertions(+)
drivers/net/can/flexcan.c +11 -0
@@ -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>
@@ -547,6 +548,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 ; }
@@ -635,6 +638,7 @@ static irqreturn_t flexcan_irq(int irq, void *dev_id)if (reg_iflag1 & (1 <<FLEXCAN_TX_BUF_ID )) { stats->tx_bytes +=can_get_echo_skb (dev,0 ); stats->tx_packets++; +can_led_event (dev,CAN_LED_EVENT_TX );flexcan_write ((1 <<FLEXCAN_TX_BUF_ID ), ®s->iflag1);netif_wake_queue (dev); }
@@ -844,6 +848,9 @@ static int flexcan_open(struct net_device *dev) err =flexcan_chip_start (dev);if (err) goto out_close; + +can_led_event (dev,CAN_LED_EVENT_OPEN ); +napi_enable (&priv->napi);netif_start_queue (dev);
@@ -872,6 +879,8 @@ static int flexcan_close(struct net_device *dev)close_candev (dev); +can_led_event (dev,CAN_LED_EVENT_STOP ); +return 0 ; }
@@ -1068,6 +1077,8 @@ static int __devinit flexcan_probe(struct platform_device *pdev) goto failed_register; } +devm_can_led_init (dev); +dev_info (&pdev->dev,"device registered (reg_base=%p, irq=%d)\n" , priv->base, dev->irq);
Signature · 1 line
1.7.11.rc1.9.gf623ca1.dirty
Marc Kleine-Budde
maintainerRe: [PATCH can-next v5 2/2] can: flexcan: add LED trigger support
Quoted 36 lines · Fabio Baltieri
Add support for canbus activity led indicators on flexcan devices by calling appropriate can_led_* functions.
These are only enabled when
CONFIG_CAN_LEDSis Y, becomes no-op otherwise.Cc Oliver Hartkopp <[email protected]>Cc Wolfgang Grandegger <[email protected]>Cc Marc Kleine-Budde <[email protected]>Signed-off-by Fabio Baltieri <[email protected]>--- drivers/net/can/flexcan.c | 11 +++++++++++ 1 file changed, 11 insertions(+)drivers/net/can/flexcan.c +3
-0@@ -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> @@ -547,6 +548,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 ; }
drivers/net/can/flexcan.c static irqreturn_t flexcan_irq(int irq, void *dev_id) · line 641
Quoted 4 lines
drivers/net/can/flexcan.c +1
-0@@ -635,6 +638,7 @@ static irqreturn_t flexcan_irq(int irq, void *dev_id)if (reg_iflag1 & (1 <<FLEXCAN_TX_BUF_ID )) { stats->tx_bytes +=can_get_echo_skb (dev,0 ); stats->tx_packets++; +can_led_event (dev,CAN_LED_EVENT_TX );
Should the led blink on TX or TX completion interrupt?
Quoted 24 lines
drivers/net/can/flexcan.c +5
-0flexcan_write ((1 <<FLEXCAN_TX_BUF_ID ), ®s->iflag1);netif_wake_queue (dev); }@@ -844,6 +848,9 @@ static int flexcan_open(struct net_device *dev) err =flexcan_chip_start (dev);if (err) goto out_close; + +can_led_event (dev,CAN_LED_EVENT_OPEN ); +napi_enable (&priv->napi);netif_start_queue (dev);@@ -872,6 +879,8 @@ static int flexcan_close(struct net_device *dev)close_candev (dev); +can_led_event (dev,CAN_LED_EVENT_STOP ); +return 0 ; }
drivers/net/can/flexcan.c static int __devinit flexcan_probe(struct platform_device *pdev) · line 1084
Quoted 8 lines
drivers/net/can/flexcan.c +2
-0@@ -1068,6 +1077,8 @@ static int __devinit flexcan_probe(struct platform_device *pdev) goto failed_register; } +devm_can_led_init (dev); +dev_info (&pdev->dev,"device registered (reg_base=%p, irq=%d)\n" , priv->base, dev->irq);
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 |
Quoted 24 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
Kconfigoption.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_privstructure.Supported drivers should call
devm_can_led_init()andcan_led_event()as needed.Cleanup is handled automatically by devres, so no
*_exitfunction is 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
Quoted 4 lines
Cc Oliver Hartkopp <[email protected]>Cc Wolfgang Grandegger <[email protected]>Cc Marc Kleine-Budde <[email protected]>Signed-off-by Fabio Baltieri <[email protected]>
Applied to can-next/master.
But as net-next is still closed, feel free to send Acked-by or Tested-by. Or even code improvements.
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 |
Sorry for this potentially mangled mail from my webmail access ...
Fabio Baltieri <[email protected]> hat am 1. August 2012 um 13:49 geschrieben:
drivers/net/can/led.c line 78
Quoted 10 lines
drivers/net/can/led.c new file +10
-0+void devm_can_led_init (struct net_device *netdev) +{ +struct can_priv *priv =netdev_priv (netdev); +void *res; + + res =devres_alloc (can_led_release,0 ,GFP_KERNEL ); +if (!res) + goto err_out; + + priv->tx_led_trig_name =kasprintf (GFP_KERNEL ,"%s-tx" , netdev->name);
IMO putting a string with 8 or 9 bytes into a separate kmalloc memory sniplet is pretty oversized.
Ok - these functions provide to hide the complexitiy for allocating and storing strings, which is
definitely fine for path names and these kind of strings that are not known in length and probably
more than 100 bytes long.
But in this case i would suggest to allocate a fixed space in can_priv, as we know the string length
very good (IFNAMSZ + strlen("-tx")) and there's no reason to get all the overhead from three kmallocs
instead of one for that small memory allocations.
So i would suggest:
include/linux/can/dev.h struct can_priv { · line 59
Quoted 8 lines
include/linux/can/dev.h +4
-0@@ -52,6 +53,13 @@ struct can_priv {unsigned int echo_skb_max;struct sk_buff **echo_skb; + +#ifdef CONFIG_CAN_LEDS +struct led_trigger *tx_led_trig; +char *tx_led_trig_name;
char tx_led_trig_name[IFNAMSZ+4];
include/linux/can/dev.h struct can_priv { · line 61
Quoted 2 lines
include/linux/can/dev.h +2
-0+struct led_trigger *rx_led_trig; +char *rx_led_trig_name;
char rx_led_trig_name[IFNAMSZ+4];
include/linux/can/dev.h struct can_priv { · line 64
Quoted 3 lines
include/linux/can/dev.h +1
-0+#endif };
Just my two cents as i was in CC here :-)
Thanks for the cool LED support & best regards
Oliver
Marc Kleine-Budde
maintainerQuoted 43 lines · Oliver Hartkopp
Sorry for this potentially mangled mail from my webmail access ...
Fabio Baltieri <[email protected]> hat am 1. August 2012 um 13:49 geschrieben:
drivers/net/can/led.c line 78
Quoted 10 lines
drivers/net/can/led.c new file +10
-0+void devm_can_led_init (struct net_device *netdev) +{ +struct can_priv *priv =netdev_priv (netdev); +void *res; + + res =devres_alloc (can_led_release,0 ,GFP_KERNEL ); +if (!res) + goto err_out; + + priv->tx_led_trig_name =kasprintf (GFP_KERNEL ,"%s-tx" , netdev->name);IMO putting a string with 8 or 9 bytes into a separate kmalloc memory sniplet is pretty oversized.
Ok - these functions provide to hide the complexitiy for allocating and storing strings, which is
definitely fine for path names and these kind of strings that are not known in length and probably
more than 100 bytes long.But in this case i would suggest to allocate a fixed space in
can_priv, as we know the string length
very good (IFNAMSZ +strlen("-tx")) and there's no reason to get all the overhead from three kmallocs
instead of one for that small memory allocations.So i would suggest:
include/linux/can/dev.h struct can_priv { · line 59
Quoted 8 lines
include/linux/can/dev.h +4
-0@@ -52,6 +53,13 @@ struct can_priv {unsigned int echo_skb_max;struct sk_buff **echo_skb; + +#ifdef CONFIG_CAN_LEDS +struct led_trigger *tx_led_trig; +char *tx_led_trig_name;char tx_led_trig_name[IFNAMSZ+4];
Quoted 7 lines
include/linux/can/dev.h struct can_priv { · line 61
Quoted 2 lines
include/linux/can/dev.h +2
-0+struct led_trigger *rx_led_trig; +char *rx_led_trig_name;char rx_led_trig_name[IFNAMSZ+4];
Quoted 2 lines
include/linux/can/dev.h +1
-0+#endif };
Sounds reasonable. Please Introduce a #define for "IFNAMSZ + 4" and document where the 4 comes from. Send a v6 patch, I'll force update the git repo, after you have Oliver's Acked-by.
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 |
Re: [PATCH can-next v5 2/2] can: flexcan: add LED trigger support
In reply to Marc Kleine-BuddeOn Wed, Aug 01, 2012 at 01:53:22PM +0200, Marc Kleine-Budde wrote:
[...]
Quoted 7 lines
drivers/net/can/flexcan.c static irqreturn_t flexcan_irq(int irq, void *dev_id) · line 641
Quoted 5 lines
drivers/net/can/flexcan.c +1
-0@@ -635,6 +638,7 @@ static irqreturn_t flexcan_irq(int irq, void *dev_id)if (reg_iflag1 & (1 <<FLEXCAN_TX_BUF_ID )) { stats->tx_bytes +=can_get_echo_skb (dev,0 ); stats->tx_packets++; +can_led_event (dev,CAN_LED_EVENT_TX );Should the led blink on TX or TX completion interrupt?
I'd say on complention interrupt, together with can_get_echo_skb().
That was briefly discussed with Oliver in my first patch:
http://article.gmane.org/gmane.linux.can/1007
Fabio
Marc Kleine-Budde
maintainerRe: [PATCH can-next v5 2/2] can: flexcan: add LED trigger support
Quoted 11 lines · Fabio Baltieri
On Wed, Aug 01, 2012 at 01:53:22PM +0200, Marc Kleine-Budde wrote:
[...]Quoted 7 lines
drivers/net/can/flexcan.c static irqreturn_t flexcan_irq(int irq, void *dev_id) · line 641
Quoted 5 lines
drivers/net/can/flexcan.c +1
-0@@ -635,6 +638,7 @@ static irqreturn_t flexcan_irq(int irq, void *dev_id)if (reg_iflag1 & (1 <<FLEXCAN_TX_BUF_ID )) { stats->tx_bytes +=can_get_echo_skb (dev,0 ); stats->tx_packets++; +can_led_event (dev,CAN_LED_EVENT_TX );Should the led blink on TX or TX completion interrupt?
I'd say on complention interrupt, together with
can_get_echo_skb().
Yes, good that we already agreed on this :D
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 |
PATCH v6 1/1 can: add tx/rx LED trigger support
In reply to Marc Kleine-BuddeThis 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 devm_can_led_init() and can_led_event() as needed.
Cleanup is handled automatically by devres, so no *_exit function is 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
---
Hi all,
so, v6, change trigger names for fixed size allocation capped to (IFNAMSIZ + 4) and removed kasprintf as suggested by Oliver (thanks!).
This also has the side effect of reducing the error path to just one check to devres_alloc return value... nice!
I've put CAN_LED_NAME_SZ definition with the active function declaration, but used sizeof(priv->tx_led_trig_name) as snprintf length argument in the code, as it looks cleaner to me.
I'm not reposting the flexcan patch as it's not affected by the change.
Fabio
drivers/net/can/Kconfig | 12 +++++++ drivers/net/can/Makefile | 2 ++ drivers/net/can/led.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/can/dev.h | 8 +++++ include/linux/can/led.h | 42 +++++++++++++++++++++++ 5 files changed, 153 insertions(+) create mode 100644 drivers/net/can/led.c create mode 100644 include/linux/can/led.h
drivers/net/can/Kconfig +12 -0
@@ -54,6 +54,18 @@ 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_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
@@ -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 +89 -0
89 lines not on this page · raw message
include/linux/can/dev.h +8 -0
@@ -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,13 @@ struct can_priv {unsigned int echo_skb_max;struct sk_buff **echo_skb; + +#ifdef CONFIG_CAN_LEDS +struct led_trigger *tx_led_trig; +char tx_led_trig_name[CAN_LED_NAME_SZ ]; +struct led_trigger *rx_led_trig; +char rx_led_trig_name[CAN_LED_NAME_SZ ]; +#endif };/*
include/linux/can/led.h new file +42 -0
42 lines not on this page · raw message
Signature · 1 line
1.7.11.rc1.9.gf623ca1.dirty
Marc Kleine-Budde
maintainerQuoted 35 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
Kconfigoption.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_privstructure.Supported drivers should call
devm_can_led_init()andcan_led_event()as needed.Cleanup is handled automatically by devres, so no
*_exitfunction is 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 blinkCc Oliver Hartkopp <[email protected]>Cc Wolfgang Grandegger <[email protected]>Cc Marc Kleine-Budde <[email protected]>Signed-off-by Fabio Baltieri <[email protected]>---
Hi all,
so, v6, change trigger names for fixed size allocation capped to (IFNAMSIZ + 4) and removed kasprintf as suggested by Oliver (thanks!).
Quoted 8 lines
This also has the side effect of reducing the error path to just one check to
devres_allocreturn value... nice!I've put
CAN_LED_NAME_SZdefinition with the active function declaration, but usedsizeof(priv->tx_led_trig_name)as snprintf length argument in the code, as it looks cleaner to me.I'm not reposting the flexcan patch as it's not affected by the change.
Pushed to can-next/master, it even compiles now, as David has included some upstream branches.
I'm still taking Tested- and Acked-by for these patches.
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 |
Re: [PATCH can-next v5 2/2] can: flexcan: add LED trigger support
In reply to Fabio BaltieriQuoted 11 lines · Fabio Baltieri
Add support for canbus activity led indicators on flexcan devices by calling appropriate can_led_* functions.
These are only enabled when
CONFIG_CAN_LEDSis Y, becomes no-op otherwise.Cc Oliver Hartkopp <[email protected]>Cc Wolfgang Grandegger <[email protected]>Cc Marc Kleine-Budde <[email protected]>Signed-off-by Fabio Baltieri <[email protected]>---
Applied to can-next/master
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 |
Fabio Baltieri <[email protected]> hat am 1. August 2012 um 20:21 geschrieben:
Quoted 10 lines
so, v6, change trigger names for fixed size allocation capped to (IFNAMSIZ +
4)
and removed kasprintf as suggested by Oliver (thanks!).This also has the side effect of reducing the error path to just one check to
devres_allocreturn value... nice!I've put
CAN_LED_NAME_SZdefinition with the active function declaration, but usedsizeof(priv->tx_led_trig_name)as snprintf length argument in the code, as it looks cleaner to me.
Yes. Nice improvement.
Thanks Fabio!
Quoted 43 lines · Marc Kleine-Budde
Quoted 42 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
Kconfigoption.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_privstructure.Supported drivers should call
devm_can_led_init()andcan_led_event()as needed.Cleanup is handled automatically by devres, so no
*_exitfunction is 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 blinkCc Oliver Hartkopp <[email protected]>Cc Wolfgang Grandegger <[email protected]>Cc Marc Kleine-Budde <[email protected]>Signed-off-by Fabio Baltieri <[email protected]>---
Hi all,
so, v6, change trigger names for fixed size allocation capped to (IFNAMSIZ + 4) and removed kasprintf as suggested by Oliver (thanks!).
This also has the side effect of reducing the error path to just one check to
devres_allocreturn value... nice!I've put
CAN_LED_NAME_SZdefinition with the active function declaration, but usedsizeof(priv->tx_led_trig_name)as snprintf length argument in the code, as it looks cleaner to me.
Quoted 6 lines
Quoted 1 line
I'm not reposting the flexcan patch as it's not affected by the change.
Pushed to can-next/master, it even compiles now, as David has included some upstream branches.
I'm still taking Tested- and Acked-by for these patches.
Nice! So, I'll start preparing some patch for other embedded CAN controllers for test/review by developers who have access to the actual hardware.
In the meantime, thanks to everyone on the list for reviews and ideas!
Cheers!
Fabio
Hello,
I find the CAN led triggers an interesting thing.
And then, this scenario fell crossed my mind:
Imagine I do:
[insert CAN device: can0]
$ ip link set can0 name helga [insert another CAN device: again 'can0']
Registering 'can0-tx' led trigger will fail for the second CAN device, since that led trigger name is already reserved for CAN device 'helga'.
I'm not sure how to fix such.
If 'rx' & 'tx' may be combined, reusing the netdev name may be possible? Just wild thinking ...
Kind regards,
Kurt
Quoted 267 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
Kconfigoption.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_privstructure.Supported drivers should call
devm_can_led_init()andcan_led_event()as needed.Cleanup is handled automatically by devres, so no
*_exitfunction is 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 blinkCc Oliver Hartkopp <[email protected]>Cc Wolfgang Grandegger <[email protected]>Cc Marc Kleine-Budde <[email protected]>Signed-off-by Fabio Baltieri <[email protected]>---
Hi all,
so, v6, change trigger names for fixed size allocation capped to (IFNAMSIZ + 4) and removed kasprintf as suggested by Oliver (thanks!).
This also has the side effect of reducing the error path to just one check to
devres_allocreturn value... nice!I've put
CAN_LED_NAME_SZdefinition with the active function declaration, but usedsizeof(priv->tx_led_trig_name)as snprintf length argument in the code, as it looks cleaner to me.I'm not reposting the flexcan patch as it's not affected by the change.
Fabio
drivers/net/can/Kconfig | 12 +++++++ drivers/net/can/Makefile | 2 ++ drivers/net/can/led.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/can/dev.h | 8 +++++ include/linux/can/led.h | 42 +++++++++++++++++++++++ 5 files changed, 153 insertions(+) create mode 100644 drivers/net/can/led.c create mode 100644 include/linux/can/led.hdrivers/net/can/Kconfig +12
-0@@ -54,6 +54,18 @@ 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_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@@ -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 +89
-0@@ -0,0 +1,89 @@ +/* +* Copyright 2012, Fabio Baltieri <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#include <linux/module.h> +#include <linux/device.h> +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/netdevice.h> +#include <linux/can/dev.h> + +#include <linux/can/led.h> + +static unsigned long led_delay =50 ; +module_param (led_delay, ulong,0644 ); +MODULE_PARM_DESC (led_delay, +"blink delay time for activity leds (msecs, default: 50)." ); + +/* +* Trigger a LED event in response to a CAN device event +*/ +void can_led_event (struct net_device *netdev,enum can_led_event event) +{ +struct can_priv *priv =netdev_priv (netdev); + +switch (event) { +case CAN_LED_EVENT_OPEN : +led_trigger_event (priv->tx_led_trig,LED_FULL ); +led_trigger_event (priv->rx_led_trig,LED_FULL ); +break ; +case CAN_LED_EVENT_STOP : +led_trigger_event (priv->tx_led_trig,LED_OFF ); +led_trigger_event (priv->rx_led_trig,LED_OFF ); +break ; +case CAN_LED_EVENT_TX : +if (led_delay) +led_trigger_blink_oneshot (priv->tx_led_trig, + &led_delay, &led_delay,1 ); +break ; +case CAN_LED_EVENT_RX : +if (led_delay) +led_trigger_blink_oneshot (priv->rx_led_trig, + &led_delay, &led_delay,1 ); +break ; + } +} +EXPORT_SYMBOL_GPL (can_led_event); + +static void can_led_release (struct device *gendev,void *res) +{ +struct can_priv *priv =netdev_priv (to_net_dev (gendev)); + +led_trigger_unregister_simple (priv->tx_led_trig); +led_trigger_unregister_simple (priv->rx_led_trig); +} + +/* +* Register CAN LED triggers for a CAN device +* +* This is normally called from a driver's probe function +*/ +void devm_can_led_init (struct net_device *netdev) +{ +struct can_priv *priv =netdev_priv (netdev); +void *res; + + res =devres_alloc (can_led_release,0 ,GFP_KERNEL ); +if (!res) { +netdev_err (netdev,"cannot register LED triggers\n" ); +return ; + } + +snprintf (priv->tx_led_trig_name,sizeof (priv->tx_led_trig_name), +"%s-tx" , netdev->name); +snprintf (priv->rx_led_trig_name,sizeof (priv->rx_led_trig_name), +"%s-rx" , netdev->name); + +led_trigger_register_simple (priv->tx_led_trig_name, + &priv->tx_led_trig); +led_trigger_register_simple (priv->rx_led_trig_name, + &priv->rx_led_trig); + +devres_add (&netdev->dev, res); +} +EXPORT_SYMBOL_GPL (devm_can_led_init);include/linux/can/dev.h +8
-0@@ -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,13 @@ struct can_priv {unsigned int echo_skb_max;struct sk_buff **echo_skb; + +#ifdef CONFIG_CAN_LEDS +struct led_trigger *tx_led_trig; +char tx_led_trig_name[CAN_LED_NAME_SZ ]; +struct led_trigger *rx_led_trig; +char rx_led_trig_name[CAN_LED_NAME_SZ ]; +#endif };/* include/linux/can/led.h new file +42
-0@@ -0,0 +1,42 @@ +/* +* Copyright 2012, Fabio Baltieri <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef CAN_LED_H +#define CAN_LED_H + +#include <linux/if.h> +#include <linux/leds.h> + +enum can_led_event { +CAN_LED_EVENT_OPEN , +CAN_LED_EVENT_STOP , +CAN_LED_EVENT_TX , +CAN_LED_EVENT_RX , +}; + +#ifdef CONFIG_CAN_LEDS + +/* keep space for interface name + "-tx"/"-rx" suffix and null terminator */ +#define CAN_LED_NAME_SZ (IFNAMSIZ + 4) + +void can_led_event (struct net_device *netdev,enum can_led_event event); +void devm_can_led_init (struct net_device *netdev); + +#else + +static inline void can_led_event (struct net_device *netdev, +enum can_led_event event) +{ +} +static inline void devm_can_led_init (struct net_device *netdev) +{ +} + +#endif + +#endif Signature · 6 lines
1.7.11.rc1.9.gf623ca1.dirty -- To unsubscribe from this list: send the line "unsubscribe linux-can" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Signature · 5 lines
Kurt Van Dijck GRAMMER EiA ELECTRONICS http://www.eia.be [email protected] +32-38708534
Marc Kleine-Budde
maintainerQuoted 4 lines · Kurt Van Dijck
Hello,
I find the CAN led triggers an interesting thing.
Quoted 8 lines
And then, this scenario fell crossed my mind:
Imagine I do:
[insert CAN device: can0]$ ip link set can0 name helga [insert another CAN device: again 'can0']Registering 'can0-tx' led trigger will fail for the second CAN device, since that led trigger name is already reserved for CAN device 'helga'.
Good point.
Quoted 3 lines
I'm not sure how to fix such.
If 'rx' & 'tx' may be combined, reusing the netdev name may be possible? Just wild thinking ...
I think the device's name (not netdev) is unique in the system and cannot be changed.
On my device tree enabled mx28 I'm talking about the "80032000.can" in:
Quoted 7 lines
dmesg |grep flexcan
flexcan 80032000.can: device registered (reg_base=f5032000, irq=8) flexcan 80034000.can: device registered (reg_base=f5034000, irq=9)$ ls /sys/bus/platform/devices/*can* -d /sys/bus/platform/devices/80032000.can /sys/bus/platform/devices/80034000.can
regards, 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 |
Quoted 14 lines · Marc Kleine-Budde
Quoted 4 lines · Kurt Van Dijck
Hello,
I find the CAN led triggers an interesting thing.
Quoted 8 lines
And then, this scenario fell crossed my mind:
Imagine I do:
[insert CAN device: can0]$ ip link set can0 name helga [insert another CAN device: again 'can0']Registering 'can0-tx' led trigger will fail for the second CAN device, since that led trigger name is already reserved for CAN device 'helga'.
Quoted 8 lines
Good point.
Quoted 3 lines
I'm not sure how to fix such.
If 'rx' & 'tx' may be combined, reusing the netdev name may be possible? Just wild thinking ...I think the device's name (not netdev) is unique in the system and cannot be changed.
but may contain several netdev's ...
Quoted 2 lines
On my device tree enabled mx28 I'm talking about the "80032000.can" in:
You idea triggered another thougt: since control is put in device drivers, why putting the name in the generic can_dev struct?
A more flexible approach to assign names is the key to success here. The correct 'works in all conditions' approach is not yet in my sight :-(
Kurt
Quoted 9 lines
regards, 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 |
Signature · 5 lines
Kurt Van Dijck GRAMMER EiA ELECTRONICS http://www.eia.be [email protected] +32-38708534
Hello Kurt,
Quoted 12 lines · Kurt Van Dijck
Quoted 11 lines · Marc Kleine-Budde
Quoted 10 lines · Kurt Van Dijck
Hello,
I find the CAN led triggers an interesting thing.
And then, this scenario fell crossed my mind:
Imagine I do:
[insert CAN device: can0]$ ip link set can0 name helga [insert another CAN device: again 'can0']
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
Registering 'can0-tx' led trigger will fail for the second CAN device, since that led trigger name is already reserved for CAN device 'helga'.
Good point.
Yep, thanks for pointing that out!
Interface renaming was something I considered when I first wrote the code and I had the mac80211-led driver in mind, as that driver uses the phy name and not the netdev one for its triggers.
The reason why I did not care that much in the end is that on SoC based systems trigger-led association is made at probe time, based on data either from platform_data or devicetree, so I imagined that once the kernel is ported to the board and default triggers are set correctly at boot time, the userspace is free to rename CAN interfaces and nobody should notice... :^)
The thing I did not consider are hot-plug interfaces mixed with renaming, such as in the case you pointed out - it's probably not really common but still possible.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
I'm not sure how to fix such.
If 'rx' & 'tx' may be combined, reusing the netdev name may be possible? Just wild thinking ...I think the device's name (not netdev) is unique in the system and cannot be changed.
but may contain several netdev's ...
Ouch.
Quoted 6 lines
Quoted 3 lines
On my device tree enabled mx28 I'm talking about the "80032000.can" in:
You idea triggered another thougt: since control is put in device drivers, why putting the name in the generic
can_devstruct?
Why not? That makes the API easy.
Quoted 2 lines
A more flexible approach to assign names is the key to success here. The correct 'works in all conditions' approach is not yet in my sight :-(
Agreed.
What about using a combination of device name + an optional port index specified in devm_can_led_init()? (something like to platform_device names) Of course that would require changing the API for libraries like register_sja1000dev(), to add a port index.
Fabio
Quoted 49 lines · Fabio Baltieri
Hello Kurt,
Quoted 12 lines · Kurt Van Dijck
Quoted 11 lines · Marc Kleine-Budde
Quoted 10 lines · Kurt Van Dijck
Hello,
I find the CAN led triggers an interesting thing.
And then, this scenario fell crossed my mind:
Imagine I do:
[insert CAN device: can0]$ ip link set can0 name helga [insert another CAN device: again 'can0']Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
Registering 'can0-tx' led trigger will fail for the second CAN device, since that led trigger name is already reserved for CAN device 'helga'.
Good point.
Yep, thanks for pointing that out!
Interface renaming was something I considered when I first wrote the code and I had the mac80211-led driver in mind, as that driver uses the phy name and not the netdev one for its triggers.
The reason why I did not care that much in the end is that on SoC based systems trigger-led association is made at probe time, based on data either from
platform_dataor devicetree, so I imagined that once the kernel is ported to the board and default triggers are set correctly at boot time, the userspace is free to rename CAN interfaces and nobody should notice... :^)The thing I did not consider are hot-plug interfaces mixed with renaming, such as in the case you pointed out - it's probably not really common but still possible.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
I'm not sure how to fix such.
If 'rx' & 'tx' may be combined, reusing the netdev name may be possible? Just wild thinking ...I think the device's name (not netdev) is unique in the system and cannot be changed.
but may contain several netdev's ...
Ouch.
Quoted 2 lines
Quoted 2 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
On my device tree enabled mx28 I'm talking about the "80032000.can" in:
You idea triggered another thougt: since control is put in device drivers, why putting the name in the generic
can_devstruct?Why not? That makes the API easy.
The code is not next to the data?
Anyway, I said the above because I think the led names need a per-device approach, but the data is in common parts. That's where things start being complicated. I understand the common part now because you mainly addressed the SoC based drivers.
Quoted 16 lines
Quoted 3 lines
A more flexible approach to assign names is the key to success here. The correct 'works in all conditions' approach is not yet in my sight :-(
Agreed.
What about using a combination of device name + an optional port index specified in
devm_can_led_init()? (something like toplatform_devicenames) Of course that would require changing the API for libraries likeregister_sja1000dev(), to add a port index.Fabio
--
To unsubscribe from this list: send the line "unsubscribe linux-can" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Signature · 5 lines
Kurt Van Dijck GRAMMER EiA ELECTRONICS http://www.eia.be [email protected] +32-38708534
Marc Kleine-Budde
maintainerQuoted 40 lines · Fabio Baltieri
Hello Kurt,
Quoted 12 lines · Kurt Van Dijck
Quoted 11 lines · Marc Kleine-Budde
Quoted 10 lines · Kurt Van Dijck
Hello,
I find the CAN led triggers an interesting thing.
And then, this scenario fell crossed my mind:
Imagine I do:
[insert CAN device: can0]$ ip link set can0 name helga [insert another CAN device: again 'can0']Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
Registering 'can0-tx' led trigger will fail for the second CAN device, since that led trigger name is already reserved for CAN device 'helga'.
Good point.
Yep, thanks for pointing that out!
Interface renaming was something I considered when I first wrote the code and I had the mac80211-led driver in mind, as that driver uses the phy name and not the netdev one for its triggers.
The reason why I did not care that much in the end is that on SoC based systems trigger-led association is made at probe time, based on data either from
platform_dataor devicetree, so I imagined that once the kernel is ported to the board and default triggers are set correctly at boot time, the userspace is free to rename CAN interfaces and nobody should notice... :^)The thing I did not consider are hot-plug interfaces mixed with renaming, such as in the case you pointed out - it's probably not really common but still possible.
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
I'm not sure how to fix such.
If 'rx' & 'tx' may be combined, reusing the netdev name may be possible? Just wild thinking ...
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
I think the device's name (not netdev) is unique in the system and cannot be changed.
but may contain several netdev's ...
Ouch.
The net->ifindex is unique. But it's only an integer. Usually can0 has a ifindex != 0, so a simple can%d is contra productive here.
Some pointers to related code:
http://lxr.free-electrons.com/source/drivers/base/core.c#L1847
http://lxr.free-electrons.com/source/drivers/base/core.c#L73
http://lxr.free-electrons.com/source/include/linux/device.h#L695
comments?
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 |
Quoted 51 lines · Marc Kleine-Budde
Quoted 40 lines · Fabio Baltieri
Hello Kurt,
Quoted 12 lines · Kurt Van Dijck
Quoted 11 lines · Marc Kleine-Budde
Quoted 10 lines · Kurt Van Dijck
Hello,
I find the CAN led triggers an interesting thing.
And then, this scenario fell crossed my mind:
Imagine I do:
[insert CAN device: can0]$ ip link set can0 name helga [insert another CAN device: again 'can0']Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
Registering 'can0-tx' led trigger will fail for the second CAN device, since that led trigger name is already reserved for CAN device 'helga'.
Good point.
Yep, thanks for pointing that out!
Interface renaming was something I considered when I first wrote the code and I had the mac80211-led driver in mind, as that driver uses the phy name and not the netdev one for its triggers.
The reason why I did not care that much in the end is that on SoC based systems trigger-led association is made at probe time, based on data either from
platform_dataor devicetree, so I imagined that once the kernel is ported to the board and default triggers are set correctly at boot time, the userspace is free to rename CAN interfaces and nobody should notice... :^)The thing I did not consider are hot-plug interfaces mixed with renaming, such as in the case you pointed out - it's probably not really common but still possible.
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
I'm not sure how to fix such.
If 'rx' & 'tx' may be combined, reusing the netdev name may be possible? Just wild thinking ...Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
I think the device's name (not netdev) is unique in the system and cannot be changed.
but may contain several netdev's ...
Ouch.
The
net->ifindexis unique. But it's only an integer. Usually can0 has a ifindex != 0, so a simple can%d is contra productive here.
Quoted 6 lines
Some pointers to related code:
http://lxr.free-electrons.com/source/drivers/base/core.c#L1847
http://lxr.free-electrons.com/source/drivers/base/core.c#L73
http://lxr.free-electrons.com/source/include/linux/device.h#L695comments?
a very recent idea: something with netdevice notifiers and NETDEV_CHANGENAME ...
http://lxr.free-electrons.com/source/net/core/dev.c#L1030
you could: rename the trigger, or if we think it's usefull, block the netdev rename when its triggers are in use.
Kurt
Quoted 10 lines · Kurt Van Dijck
Quoted 9 lines · Marc Kleine-Budde
The
net->ifindexis unique. But it's only an integer. Usually can0 has a ifindex != 0, so a simple can%d is contra productive here.Some pointers to related code:
http://lxr.free-electrons.com/source/drivers/base/core.c#L1847
http://lxr.free-electrons.com/source/drivers/base/core.c#L73
http://lxr.free-electrons.com/source/include/linux/device.h#L695comments?
That would probabily makes really hard to choose the right default_trigger for led devices to get to the appropriate CAN LED in embedded systems, as trigger name would depend from other network devices and probing order (correct me if I'm wrong).
Something with device name would probaily be more appropriate here.
Quoted 6 lines
a very recent idea: something with netdevice notifiers and
NETDEV_CHANGENAME...
http://lxr.free-electrons.com/source/net/core/dev.c#L1030you could: rename the trigger, or if we think it's usefull, block the netdev rename when its triggers are in use.
Blocking the rename looks overkill to me, what about using device name with an optional "port id" appended to it? Sounds simpler...
Fabio
Quoted 20 lines · Fabio Baltieri
Quoted 10 lines · Kurt Van Dijck
Quoted 9 lines · Marc Kleine-Budde
The
net->ifindexis unique. But it's only an integer. Usually can0 has a ifindex != 0, so a simple can%d is contra productive here.Some pointers to related code:
http://lxr.free-electrons.com/source/drivers/base/core.c#L1847
http://lxr.free-electrons.com/source/drivers/base/core.c#L73
http://lxr.free-electrons.com/source/include/linux/device.h#L695comments?
That would probabily makes really hard to choose the right
default_triggerfor led devices to get to the appropriate CAN LED in embedded systems, as trigger name would depend from other network devices and probing order (correct me if I'm wrong).Something with device name would probaily be more appropriate here.
Quoted 1 line
Quoted 8 lines
Quoted 5 lines
a very recent idea: something with netdevice notifiers and
NETDEV_CHANGENAME...
http://lxr.free-electrons.com/source/net/core/dev.c#L1030you could: rename the trigger, or if we think it's usefull, block the netdev rename when its triggers are in use.
Blocking the rename looks overkill to me, what about using device name with an optional "port id" appended to it? Sounds simpler...
The name of the device can only be changed when the interface is down. Is it possible to put some scripting around it to detach and attach the leds to the interfaces on ifup/ifdown triggers?
Regards,
Oliver
Quoted 22 lines · Oliver Hartkopp
Quoted 20 lines · Fabio Baltieri
Quoted 10 lines · Kurt Van Dijck
Quoted 9 lines · Marc Kleine-Budde
The
net->ifindexis unique. But it's only an integer. Usually can0 has a ifindex != 0, so a simple can%d is contra productive here.Some pointers to related code:
http://lxr.free-electrons.com/source/drivers/base/core.c#L1847
http://lxr.free-electrons.com/source/drivers/base/core.c#L73
http://lxr.free-electrons.com/source/include/linux/device.h#L695comments?
That would probabily makes really hard to choose the right
default_triggerfor led devices to get to the appropriate CAN LED in embedded systems, as trigger name would depend from other network devices and probing order (correct me if I'm wrong).Something with device name would probaily be more appropriate here.
Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
a very recent idea: something with netdevice notifiers and
NETDEV_CHANGENAME...
http://lxr.free-electrons.com/source/net/core/dev.c#L1030you could: rename the trigger, or if we think it's usefull, block the netdev rename when its triggers are in use.
Blocking the rename looks overkill to me,
renaming a netdev _after_ first attaching led triggers looks stupid to me anyway.
Quoted 7 lines
Quoted 2 lines
what about using device name
with an optional "port id" appended to it? Sounds simpler...The name of the device can only be changed when the interface is down. Is it possible to put some scripting around it to detach and attach the leds to the interfaces on ifup/ifdown triggers?
Are the led triggers available for using while the netdev is down then?
Kurt
PATCH 1/1 can: rename LED trigger name on netdev renames
The LED trigger name for CAN devices is based on the initial CAN device name, but does never change. The LED trigger name is not guaranteed to be unique in case of hotplugging CAN devices.
This patch tries to address this problem by modifying the LED trigger name according to the CAN device name when the latter changes.
This patch is meant as illustration only.
In case of VCAN device rename, a segmentation fault will occur.
--- drivers/net/can/led.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+)
drivers/net/can/led.c +57 -0
@@ -12,6 +12,7 @@#include <linux/slab.h> #include <linux/netdevice.h> #include <linux/can/dev.h> +#include <linux/if_arp.h> #include <linux/can/led.h>
@@ -87,3 +88,59 @@ void devm_can_led_init(struct net_device *netdev)devres_add (&netdev->dev, res); }EXPORT_SYMBOL_GPL (devm_can_led_init); + +/* +* NETDEV rename notifier to rename the associated led triggers too +*/ +static int can_led_notifier (struct notifier_block *nb,unsigned long msg, +void *data) +{ +struct net_device *netdev = (struct net_device *)data; +struct can_priv *priv =netdev_priv (netdev); +int busy =0 ; + +if (!net_eq (dev_net (netdev), &init_net)) +return NOTIFY_DONE ; + +if (netdev->type !=ARPHRD_CAN ) +return NOTIFY_DONE ; + +if (msg !=NETDEV_CHANGENAME ) +return NOTIFY_DONE ; + +read_lock (&priv->tx_led_trig->leddev_list_lock); +if (!list_empty (&priv->tx_led_trig->led_cdevs)) + ++busy; +read_unlock (&priv->tx_led_trig->leddev_list_lock); +read_lock (&priv->rx_led_trig->leddev_list_lock); +if (!list_empty (&priv->rx_led_trig->led_cdevs)) + ++busy; +read_unlock (&priv->rx_led_trig->leddev_list_lock); + +if (busy) +return notifier_from_errno (-EBUSY ); + +snprintf (priv->tx_led_trig_name,sizeof (priv->tx_led_trig_name), +"%s-tx" , netdev->name); +snprintf (priv->rx_led_trig_name,sizeof (priv->rx_led_trig_name), +"%s-rx" , netdev->name); +return NOTIFY_DONE ; +} + +/* notifier block for netdevice event */ +static struct notifier_block can_netdev_notifier__read_mostly = { + .notifier_call = can_led_notifier, +}; + +static __init int can_led_init (void ) +{ +return register_netdevice_notifier (&can_netdev_notifier); +} + +static __exit void can_led_exit(void) +{ +unregister_netdevice_notifier (&can_netdev_notifier); +} + +module_init (can_led_init); +module_exit (can_led_exit);
Signature · 1 line
1.7.10.4
Quoted 38 lines · Kurt Van Dijck
Quoted 22 lines · Oliver Hartkopp
Quoted 20 lines · Fabio Baltieri
Quoted 10 lines · Kurt Van Dijck
Quoted 9 lines · Marc Kleine-Budde
The
net->ifindexis unique. But it's only an integer. Usually can0 has a ifindex != 0, so a simple can%d is contra productive here.Some pointers to related code:
http://lxr.free-electrons.com/source/drivers/base/core.c#L1847
http://lxr.free-electrons.com/source/drivers/base/core.c#L73
http://lxr.free-electrons.com/source/include/linux/device.h#L695comments?
That would probabily makes really hard to choose the right
default_triggerfor led devices to get to the appropriate CAN LED in embedded systems, as trigger name would depend from other network devices and probing order (correct me if I'm wrong).Something with device name would probaily be more appropriate here.
Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
a very recent idea: something with netdevice notifiers and
NETDEV_CHANGENAME...
http://lxr.free-electrons.com/source/net/core/dev.c#L1030you could: rename the trigger, or if we think it's usefull, block the netdev rename when its triggers are in use.
Blocking the rename looks overkill to me,
renaming a netdev _after_ first attaching led triggers looks stupid to me anyway.
Quoted 4 lines
Quoted 2 lines
what about using device name
with an optional "port id" appended to it? Sounds simpler...
Quoted 5 lines
Quoted 3 lines
The name of the device can only be changed when the interface is down. Is it possible to put some scripting around it to detach and attach the leds to the interfaces on ifup/ifdown triggers?
Are the led triggers available for using while the netdev is down then?
Sure! On embedded systems triggers are usually attached to actual LEDs at probe time using default_trigger field of struct led_classdev, and that can be specified both in machine files or in device tree.
See
http://lxr.free-electrons.com/source/arch/powerpc/boot/dts/mpc8315erdb.dts#L477
http://lxr.free-electrons.com/source/arch/arm/mach-pxa/tosa.c#L576
that's why the trigger name should be predictable, at least at boot/probe time.
The actual CAN LED trigger also reflects the actual state of the interface (off if down, on if up, blink on activity).
Fabio
Quoted 5 lines · Fabio Baltieri
Quoted 4 lines · Kurt Van Dijck
Quoted 3 lines · Oliver Hartkopp
On 03.09.2012 20:29, Fabio Baltieri wrote:
[...]
Quoted 9 lines
Quoted 5 lines
Quoted 3 lines
The name of the device can only be changed when the interface is down. Is it possible to put some scripting around it to detach and attach the leds to the interfaces on ifup/ifdown triggers?
Are the led triggers available for using while the netdev is down then?
Sure! On embedded systems triggers are usually attached to actual LEDs at probe time using
default_triggerfield of structled_classdev, and that can be specified both in machine files or in device tree.
I also think that led triggers should be available.
I asked the question because detach & attach leds to interfaces would indeed break that.
btw, I tried to send a patch tuesday (my first $ git send-email) using netdev notifiers: did you receive it, and what do you think of it?
Kurt
Hi Kurt,
Quoted 7 lines · Kurt Van Dijck
Quoted 1 line · Fabio Baltieri
On Tue, Sep 04, 2012 at 09:11:28AM +0200, Kurt Van Dijck wrote:
[...]
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
The name of the device can only be changed when the interface is down. Is it possible to put some scripting around it to detach and attach the leds to the interfaces on ifup/ifdown triggers?
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Are the led triggers available for using while the netdev is down then?
Sure! On embedded systems triggers are usually attached to actual LEDs at probe time using
default_triggerfield of structled_classdev, and that can be specified both in machine files or in device tree.I also think that led triggers should be available.
Right, that's why I think the only way is to use device name.
Quoted 2 lines
I asked the question because detach & attach leds to interfaces would indeed break that.
Sure? I think that the trigger would be set again on reattach, as default_trigger is checked both in led_cdev probe and trigger_register, see:
http://lxr.free-electrons.com/source/drivers/leds/led-triggers.c#L180
I'll try that tonight.
Quoted 2 lines
btw, I tried to send a patch tuesday (my first $ git send-email) using netdev notifiers: did you receive it, and what do you think of it?
Sure, I got it! I was planning to try that this weekend but I can give you some comments earlier tonight... sorry for the dealy!
Fabio
Signature · 1 line
Fabio Baltieri
Quoted 12 lines · Fabio Baltieri
Hi Kurt,
Quoted 9 lines · Kurt Van Dijck
Quoted 1 line · Fabio Baltieri
On Tue, Sep 04, 2012 at 09:11:28AM +0200, Kurt Van Dijck wrote:
[...]
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines
The name of the device can only be changed when the interface is down. Is it possible to put some scripting around it to detach and attach the leds to the interfaces on ifup/ifdown triggers?
Are the led triggers available for using while the netdev is down then?
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Sure! On embedded systems triggers are usually attached to actual LEDs at probe time using
default_triggerfield of structled_classdev, and that can be specified both in machine files or in device tree.I also think that led triggers should be available.
Right, that's why I think the only way is to use device name.
yes, but it has 2 disadvantages:
* inconvenient. I like 'can0-tx' much more than any device name (Sorry I can't get any real example, I'm not at home now). And for usecases where the actual device of the CAN iface is important, such setup requires udev to assign the proper iface names.
* extends existing function calls like sja1000
Quoted 11 lines
Quoted 3 lines
I asked the question because detach & attach leds to interfaces would indeed break that.
Sure? I think that the trigger would be set again on reattach, as
default_triggeris checked both inled_cdevprobe andtrigger_register, see:http://lxr.free-electrons.com/source/drivers/leds/led-triggers.c#L180
I'll try that tonight.
It looks even more inconvenient. It only works as expected when you don't change the trigger afterwards, but still it is possible (as should be), so the design of trigger is ... wrong.
example:
When you put another trigger to a led, and have a proper sequence of 'ip link set xxx up' and 'ip link set xxx down', you will end up with the default_trigger again.
I realize I'm looking at unusual scenario's.
Quoted 5 lines
Quoted 3 lines
btw, I tried to send a patch tuesday (my first $ git send-email) using netdev notifiers: did you receive it, and what do you think of it?
Sure, I got it!
Ok, well done for me then :-)
Quoted 2 lines
I was planning to try that this weekend but I can give you some comments earlier tonight... sorry for the dealy!
no hurry, I'm already satified that I got git setup right.
Kurt
Re: [PATCH] can: rename LED trigger name on netdev renames
In reply to Kurt Van DijckHi Kurt,
thanks for the patch!
Quoted 7 lines · Kurt Van Dijck
The LED trigger name for CAN devices is based on the initial CAN device name, but does never change. The LED trigger name is not guaranteed to be unique in case of hotplugging CAN devices.
This patch tries to address this problem by modifying the LED trigger name according to the CAN device name when the latter changes.
That's an ideal solution, and I like the notifier implementation, but I see a couple of problems with it.
Quoted 33 lines
This patch is meant as illustration only.
In case of VCAN device rename, a segmentation fault will occur.Signed-off-by Kurt Van Dijck <[email protected]>--- drivers/net/can/led.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+)drivers/net/can/led.c +10
-0@@ -12,6 +12,7 @@#include <linux/slab.h> #include <linux/netdevice.h> #include <linux/can/dev.h> +#include <linux/if_arp.h> #include <linux/can/led.h> @@ -87,3 +88,59 @@ void devm_can_led_init(struct net_device *netdev)devres_add (&netdev->dev, res); }EXPORT_SYMBOL_GPL (devm_can_led_init); + +/* +* NETDEV rename notifier to rename the associated led triggers too +*/ +static int can_led_notifier (struct notifier_block *nb,unsigned long msg, +void *data) +{ +struct net_device *netdev = (struct net_device *)data; +struct can_priv *priv =netdev_priv (netdev);
drivers/net/can/led.c void devm_can_led_init(struct net_device *netdev) · line 109
Quoted 10 lines
drivers/net/can/led.c +10
-0+int busy =0 ; + +if (!net_eq (dev_net (netdev), &init_net)) +return NOTIFY_DONE ; + +if (netdev->type !=ARPHRD_CAN ) +return NOTIFY_DONE ; + +if (msg !=NETDEV_CHANGENAME ) +return NOTIFY_DONE ;
That's the main problem, which I also got stuck into when I did my first can-led implementation. As LED structures are in netdev's private data, you can only use it if your driver is based on the can-dev API, and there are no way to be sure of that if you get outside driver's code itself.
This would give problems with vcan, slcan, and probabily other non-mainlined drivers.
Is there any way to register a notifier which fires only for devices registered with devm_can_led_init?
drivers/net/can/led.c void devm_can_led_init(struct net_device *netdev) · line 121
Quoted 11 lines
drivers/net/can/led.c +11
-0+read_lock (&priv->tx_led_trig->leddev_list_lock); +if (!list_empty (&priv->tx_led_trig->led_cdevs)) + ++busy; +read_unlock (&priv->tx_led_trig->leddev_list_lock); +read_lock (&priv->rx_led_trig->leddev_list_lock); +if (!list_empty (&priv->rx_led_trig->led_cdevs)) + ++busy; +read_unlock (&priv->rx_led_trig->leddev_list_lock); + +if (busy) +return notifier_from_errno (-EBUSY );
As we were discussing in the other thread, I don't think that this is necessary.
drivers/net/can/led.c void devm_can_led_init(struct net_device *netdev) · line 126
Quoted 4 lines
drivers/net/can/led.c +4
-0+snprintf (priv->tx_led_trig_name,sizeof (priv->tx_led_trig_name), +"%s-tx" , netdev->name); +snprintf (priv->rx_led_trig_name,sizeof (priv->rx_led_trig_name), +"%s-rx" , netdev->name);
I'm afraid that this is going to race against other code using trigger name, so that's probabily needs a down_write(&led_cdev->trigger_lock), which is a bit of a problem beacause that's static in led-triggers.c.
Quoted 10 lines
drivers/net/can/led.c +10
-0+return NOTIFY_DONE ; +} + +/* notifier block for netdevice event */ +static struct notifier_block can_netdev_notifier__read_mostly = { + .notifier_call = can_led_notifier, +}; + +static __init int can_led_init (void ) +{
drivers/net/can/led.c void devm_can_led_init(struct net_device *netdev) · line 146
Quoted 10 lines
drivers/net/can/led.c +10
-0+return register_netdevice_notifier (&can_netdev_notifier); +} + +static __exit void can_led_exit(void) +{ +unregister_netdevice_notifier (&can_netdev_notifier); +} + +module_init (can_led_init); +module_exit (can_led_exit);
This breaks the driver when built as a module, as led.c is actually linked together with can-dev. The solution is to get it on a module by its own or just to call init/exit functions from can_dev.
Besides those points I like the idea of renaming the trigger if we can find a clean way to do that, so I'll try to look at those point further in the weekend.
Fabio
Re: [PATCH] can: rename LED trigger name on netdev renames
Quoted 66 lines · Fabio Baltieri
Hi Kurt,
thanks for the patch!
Quoted 7 lines · Kurt Van Dijck
The LED trigger name for CAN devices is based on the initial CAN device name, but does never change. The LED trigger name is not guaranteed to be unique in case of hotplugging CAN devices.
This patch tries to address this problem by modifying the LED trigger name according to the CAN device name when the latter changes.
That's an ideal solution, and I like the notifier implementation, but I see a couple of problems with it.
Quoted 33 lines
This patch is meant as illustration only.
In case of VCAN device rename, a segmentation fault will occur.Signed-off-by Kurt Van Dijck <[email protected]>--- drivers/net/can/led.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+)drivers/net/can/led.c +10
-0@@ -12,6 +12,7 @@#include <linux/slab.h> #include <linux/netdevice.h> #include <linux/can/dev.h> +#include <linux/if_arp.h> #include <linux/can/led.h> @@ -87,3 +88,59 @@ void devm_can_led_init(struct net_device *netdev)devres_add (&netdev->dev, res); }EXPORT_SYMBOL_GPL (devm_can_led_init); + +/* +* NETDEV rename notifier to rename the associated led triggers too +*/ +static int can_led_notifier (struct notifier_block *nb,unsigned long msg, +void *data) +{ +struct net_device *netdev = (struct net_device *)data; +struct can_priv *priv =netdev_priv (netdev);drivers/net/can/led.c void devm_can_led_init(struct net_device *netdev) · line 109
Quoted 10 lines
drivers/net/can/led.c +10
-0+int busy =0 ; + +if (!net_eq (dev_net (netdev), &init_net)) +return NOTIFY_DONE ; + +if (netdev->type !=ARPHRD_CAN ) +return NOTIFY_DONE ; + +if (msg !=NETDEV_CHANGENAME ) +return NOTIFY_DONE ;That's the main problem, which I also got stuck into when I did my first can-led implementation. As LED structures are in netdev's private data, you can only use it if your driver is based on the can-dev API, and there are no way to be sure of that if you get outside driver's code itself.
Quoted 2 lines
This would give problems with vcan, slcan, and probabily other non-mainlined drivers.
Do you think, this is really a problem?
If a driver decides not to use the can-dev framework it has to implement own solutions or just adopt can-dev.
Regards,
Oliver
Quoted 4 lines
Is there any way to register a notifier which fires only for devices registered with
devm_can_led_init?
Re: [PATCH] can: rename LED trigger name on netdev renames
Quoted 26 lines · Oliver Hartkopp
Quoted 26 lines
Quoted 9 lines
drivers/net/can/led.c +9
-0+ +/* +* NETDEV rename notifier to rename the associated led triggers too +*/ +static int can_led_notifier (struct notifier_block *nb,unsigned long msg, +void *data) +{ +struct net_device *netdev = (struct net_device *)data; +struct can_priv *priv =netdev_priv (netdev);drivers/net/can/led.c void devm_can_led_init(struct net_device *netdev) · line 109
Quoted 10 lines
drivers/net/can/led.c +10
-0+int busy =0 ; + +if (!net_eq (dev_net (netdev), &init_net)) +return NOTIFY_DONE ; + +if (netdev->type !=ARPHRD_CAN ) +return NOTIFY_DONE ; + +if (msg !=NETDEV_CHANGENAME ) +return NOTIFY_DONE ;That's the main problem, which I also got stuck into when I did my first can-led implementation. As LED structures are in netdev's private data, you can only use it if your driver is based on the can-dev API, and there are no way to be sure of that if you get outside driver's code itself.
Quoted 8 lines
Quoted 2 lines
This would give problems with vcan, slcan, and probabily other non-mainlined drivers.
Do you think, this is really a problem?
If a driver decides not to use the can-dev framework it has to implement own solutions or just adopt can-dev.
Agreed, but this still means that we can't assume that netdev_priv(netdev) to a netdev where netdev->type == ARPHRD_CAN points to a struct can_priv, right?
Fabio
Quoted 9 lines · Kurt Van Dijck
Quoted 3 lines
Quoted 1 line
I also think that led triggers should be available.
Right, that's why I think the only way is to use device name.
yes, but it has 2 disadvantages:
* inconvenient. I like 'can0-tx' much more than any device name (Sorry I can't get any real example, I'm not at home now). And for usecases where the actual device of the CAN iface is important, such setup requires udev to assign the proper iface names.
Can't argue with that... I'm trying to see how it comes but names like "can-3-2:1.0-tx" doesn't looks that friendly to me...
Quoted 1 line
* extends existing function calls like sja1000
Indeed.
Quoted 11 lines
Quoted 10 lines
Quoted 2 lines
I asked the question because detach & attach leds to interfaces would indeed break that.
Sure? I think that the trigger would be set again on reattach, as
default_triggeris checked both inled_cdevprobe andtrigger_register, see:http://lxr.free-electrons.com/source/drivers/leds/led-triggers.c#L180
I'll try that tonight.
Quoted 8 lines
It looks even more inconvenient. It only works as expected when you don't change the trigger afterwards, but still it is possible (as should be), so the design of trigger is ... wrong.
example:
When you put another trigger to a led, and have a proper sequence of 'ip link set xxx up' and 'ip link set xxx down', you will end up with thedefault_triggeragain.
I realize I'm looking at unusual scenario's.
That's not correct, default trigger is going to be set only if there are no other trigger assigned to the LED, and only on led probe and trigger probe.
So, the LED framework is not going to change the trigger if you manually changed it to something else, and in any case default_trigger assignment only happens at probe/exit, not when interface is set up/down.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
btw, I tried to send a patch tuesday (my first $ git send-email) using netdev notifiers: did you receive it, and what do you think of it?
Sure, I got it!
Ok, well done for me then :-)
Quoted 2 lines
I was planning to try that this weekend but I can give you some comments earlier tonight... sorry for the dealy!
no hurry, I'm already satified that I got git setup right.
Way to go! :-)
Fabio
Quoted 5 lines · Fabio Baltieri
Quoted 4 lines · Kurt Van Dijck
Quoted 3 lines
Quoted 1 line
I also think that led triggers should be available.
Right, that's why I think the only way is to use device name.
Quoted 8 lines
Quoted 5 lines
yes, but it has 2 disadvantages:
* inconvenient. I like 'can0-tx' much more than any device name (Sorry I can't get any real example, I'm not at home now). And for usecases where the actual device of the CAN iface is important, such setup requires udev to assign the proper iface names.Can't argue with that... I'm trying to see how it comes but names like "can-3-2:1.0-tx" doesn't looks that friendly to me...
"can-3-2:1.0" is an iface name?
We must keep default iface names can%d. Only in the usecase that someone wants to name its ifaces according device names, he/she should manually adjust udev rules on his/her system ...
And if the iface is names "can-3-2:1.0", "can-3-2:1.0-tx" _is_ a friendly trigger name.
Quoted 25 lines
Quoted 2 lines
* extends existing function calls like sja1000
Indeed.
Quoted 11 lines
Quoted 10 lines
Quoted 2 lines
I asked the question because detach & attach leds to interfaces would indeed break that.
Sure? I think that the trigger would be set again on reattach, as
default_triggeris checked both inled_cdevprobe andtrigger_register, see:http://lxr.free-electrons.com/source/drivers/leds/led-triggers.c#L180
I'll try that tonight.
Quoted 8 lines
It looks even more inconvenient. It only works as expected when you don't change the trigger afterwards, but still it is possible (as should be), so the design of trigger is ... wrong.
example:
When you put another trigger to a led, and have a proper sequence of 'ip link set xxx up' and 'ip link set xxx down', you will end up with thedefault_triggeragain.
I realize I'm looking at unusual scenario's.
Quoted 7 lines
That's not correct, default trigger is going to be set only if there are no other trigger assigned to the LED, and only on led probe and trigger probe.
So, the LED framework is not going to change the trigger if you manually changed it to something else, and in any case
default_triggerassignment only happens at probe/exit, not when interface is set up/down.
I think you're also arguing against a register/unregister during iface up/down. We agree.
Kurt
Re: [PATCH] can: rename LED trigger name on netdev renames
In reply to Fabio BaltieriQuoted 21 lines · Fabio Baltieri
Quoted 20 lines · Oliver Hartkopp
Quoted 18 lines
drivers/net/can/led.c void devm_can_led_init(struct net_device *netdev) · line 99
Quoted 9 lines
drivers/net/can/led.c +9
-0+ +/* +* NETDEV rename notifier to rename the associated led triggers too +*/ +static int can_led_notifier (struct notifier_block *nb,unsigned long msg, +void *data) +{ +struct net_device *netdev = (struct net_device *)data; +struct can_priv *priv =netdev_priv (netdev);That's the main problem, which I also got stuck into when I did my first can-led implementation. As LED structures are in netdev's private data, you can only use it if your driver is based on the can-dev API, and there are no way to be sure of that if you get outside driver's code itself.
This would give problems with vcan, slcan, and probabily other non-mainlined drivers.
Quoted 8 lines
Quoted 4 lines
Do you think, this is really a problem?
If a driver decides not to use the can-dev framework it has to implement own solutions or just adopt can-dev.
Agreed, but this still means that we can't assume that
netdev_priv(netdev)to a netdev wherenetdev->type==ARPHRD_CANpoints to a structcan_priv, right?
It remains a problem for vcan & slcan.
I tend to start looking at how netlink deals with it.
I see 2 options:
* Invent a new flag for netdev->features, like NETIF_F_CANDEV, to be assigned at http://lxr.free-electrons.com/source/drivers/net/can/dev.c#L464
* test for the rtnl_link_ops:
We put a function can_priv_safe(netdev) in driver/net/can/dev.c that tests netdev->rtnl_link_ops == &can_link_ops, like in
http://lxr.free-electrons.com/source/drivers/net/can/dev.c#L751
and for candev based drivers, it returns a can_priv*, otherwise NULL. I'll prepare something early next week ...
Kind regards,
Kurt
Quoted 6 lines · Kurt Van Dijck
Quoted 3 lines · Fabio Baltieri
On Thu, Sep 06, 2012 at 05:11:58PM +0200, Kurt Van Dijck wrote:
Can't argue with that... I'm trying to see how it comes but names like "can-3-2:1.0-tx" doesn't looks that friendly to me..."can-3-2:1.0" is an iface name?
No, I was trying some combination using device name instead of netdev one (which remains can0), here 3-2:1.0 is the usb path, looks really bad to be used for dev name if netdev name is still can0 - no way of determine the link between the two.
Quoted 6 lines
We must keep default iface names can%d. Only in the usecase that someone wants to name its ifaces according device names, he/she should manually adjust udev rules on his/her system ...
And if the iface is names "can-3-2:1.0", "can-3-2:1.0-tx" _is_ a friendly trigger name.
Agreed.
Quoted 13 lines
Quoted 13 lines
Quoted 8 lines
It looks even more inconvenient. It only works as expected when you don't change the trigger afterwards, but still it is possible (as should be), so the design of trigger is ... wrong.
example:
When you put another trigger to a led, and have a proper sequence of 'ip link set xxx up' and 'ip link set xxx down', you will end up with thedefault_triggeragain.
I realize I'm looking at unusual scenario's.That's not correct, default trigger is going to be set only if there are no other trigger assigned to the LED, and only on led probe and trigger probe.
Quoted 6 lines
Quoted 3 lines
So, the LED framework is not going to change the trigger if you manually changed it to something else, and in any case
default_triggerassignment only happens at probe/exit, not when interface is set up/down.I think you're also arguing against a register/unregister during iface up/down. We agree.
Uh - yes! :-)
Fabio
PATCH v2 1/1 can: rename LED trigger name on netdev renames
In reply to Kurt Van DijckFrom: Kurt Van Dijck <[email protected]>
The LED trigger name for CAN devices is based on the initial CAN device name, but does never change. The LED trigger name is not guaranteed to be unique in case of hotplugging CAN devices.
This patch tries to address this problem by modifying the LED trigger name according to the CAN device name when the latter changes.
This patch is meant as illustration only.
In case of VCAN device rename, a segmentation fault will occur.
v1 - Kurt Van Dijck
v2 - Fabio Baltieri
- remove rename blocking if trigger is bound
- use led-subsystem function for the actual rename (still WiP)
- call init/exit functions from dev.c
---
Hi Kurt,
I like the can_priv_safe() idea, so I allowed myself to write a v2 of your patch (hope you are ok with that - let me know if you think I should just post a diff) to address the other issues I found and bring back the discussion.
So, that's what I changed:
- remove device rename blocking if trigger is bound: I don't think it's needed as we can just rename the trigger and everyone should be happy. Besides that I don't like the idea of using list lock outside led-triggers.
- use led_trigger_rename_static to rename the LED: the actual implementation is a locked strcpy to prevent races, I just posted the patch in the linux-leds mailing list, so this patch depends on wether that would be accepted.
- call init/exit functions from dev.c: that fixes builds as a module. I also renamed functions to can_led_notifier_{init,exit} to avoid confusion with devm_can_led_init.
- left a TODO marker for you for a candev-safe v3 version :-)
As this is starting to look like a jam of patches, I pushed my working branch, based upon Marc's can-next/led-trigger on:
git://github.com/fabiobaltieri/linux.git can-leds-devel
Let me know what you think of it.
Fabio
drivers/net/can/dev.c | 5 +++++ drivers/net/can/led.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ include/linux/can/led.h | 9 +++++++++ 3 files changed, 60 insertions(+)
drivers/net/can/dev.c +5 -0
@@ -25,6 +25,7 @@#include <linux/can.h> #include <linux/can/dev.h> #include <linux/can/netlink.h> +#include <linux/can/led.h> #include <net/rtnetlink.h> #define MOD_DESC "CAN device driver interface"
@@ -799,6 +800,8 @@ static __init int can_dev_init(void) {int err; +can_led_notifier_init (); + err =rtnl_link_register (&can_link_ops);if (!err) printk(KERN_INFO MOD_DESC "\n");
@@ -810,6 +813,8 @@ module_init(can_dev_init); static __exit void can_dev_exit(void) {rtnl_link_unregister (&can_link_ops); + +can_led_notifier_exit (); }module_exit (can_dev_exit);
drivers/net/can/led.c +46 -0
55 lines not on this page · raw message
include/linux/can/led.h +9 -0
@@ -26,6 +26,8 @@ enum can_led_event {void can_led_event (struct net_device *netdev,enum can_led_event event);void devm_can_led_init (struct net_device *netdev); +int __init can_led_notifier_init (void ); +void __exit can_led_notifier_exit(void);#else
@@ -36,6 +38,13 @@ static inline void can_led_event(struct net_device *netdev,static inline void devm_can_led_init (struct net_device *netdev) { } +static inline int can_led_notifier_init (void ) +{ +return 0 ; +} +static inline void can_led_notifier_exit (void ) +{ +}#endif
Signature · 1 line
1.7.11.rc1.9.gf623ca1.dirty
PATCH 1/1 can: export a safe netdev_priv wrapper for candev
can: export a safe netdev_priv wrapper for candev
In net_device notifier calls, it was impossible to determine if a CAN device is based on candev in a safe way. This patch adds such test in order to access candev storage from within those notifiers.
drivers/net/can/dev.c +12 -0
@@ -795,6 +795,18 @@ void unregister_candev(struct net_device *dev) }EXPORT_SYMBOL_GPL (unregister_candev); +/* +* Test if a network device is a candev based device +* and return the can_priv* if so. +*/ +struct can_priv *safe_candev_priv (struct net_device *dev) +{ +if ((dev->type !=ARPHRD_CAN ) || (dev->rtnl_link_ops != &can_link_ops)) +return NULL ; + +return netdev_priv (dev); +} +static __init int can_dev_init (void ) {int err;
include/linux/can/dev.h +3 -0
@@ -106,6 +106,9 @@ u8 can_len2dlc(u8 len);struct net_device *alloc_candev (int sizeof_priv,unsigned int echo_skb_max);void free_candev (struct net_device *dev); +/* a candev safe wrapper around netdev_priv */ +struct can_priv *safe_candev_priv (struct net_device *dev); +int open_candev (struct net_device *dev);void close_candev (struct net_device *dev);
PATCH v3 1/1 can: rename LED trigger name on netdev renames
In reply to Fabio Baltierican: rename LED trigger name on netdev renames
The LED trigger name for CAN devices is based on the initial CAN device name, but does never change. The LED trigger name is not guaranteed to be unique in case of hotplugging CAN devices.
This patch tries to address this problem by modifying the LED trigger name according to the CAN device name when the latter changes.
v1 - Kurt Van Dijck
v2 - Fabio Baltieri
- remove rename blocking if trigger is bound
- use led-subsystem function for the actual rename (still WiP)
- call init/exit functions from dev.c
v3 - Kurt Van Dijck
- safe operation for non-candev based devices (vcan, slcan) based on earlier patch
--
Fabio,
I followed all your contribution to this patch.
drivers/net/can/dev.c +7 -0
@@ -25,6 +25,7 @@#include <linux/can.h> #include <linux/can/dev.h> #include <linux/can/netlink.h> +#include <linux/can/led.h> #include <net/rtnetlink.h> #define MOD_DESC "CAN device driver interface"
@@ -811,6 +812,10 @@ static __init int can_dev_init(void) {int err; +can_led_notifier_init (); + +can_led_notifier_init (); + err =rtnl_link_register (&can_link_ops);if (!err) printk(KERN_INFO MOD_DESC "\n");
@@ -822,6 +827,8 @@ module_init(can_dev_init); static __exit void can_dev_exit(void) {rtnl_link_unregister (&can_link_ops); + +can_led_notifier_exit (); }module_exit (can_dev_exit);
drivers/net/can/led.c +37 -0
@@ -1,5 +1,6 @@/* * Copyright 2012, Fabio Baltieri <[email protected]> +* Copyright 2012, Kurt Van Dijck <[email protected]> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as
@@ -87,3 +88,39 @@ void devm_can_led_init(struct net_device *netdev)devres_add (&netdev->dev, res); }EXPORT_SYMBOL_GPL (devm_can_led_init); + +/* +* NETDEV rename notifier to rename the associated led triggers too +*/ +static int can_led_notifier (struct notifier_block *nb,unsigned long msg, +void *data) +{ +struct net_device *netdev = data; +struct can_priv *priv =safe_candev_priv (netdev); +char name[CAN_LED_NAME_SZ ]; + +if (msg ==NETDEV_CHANGENAME ) { +snprintf (name,sizeof (name),"%s-tx" , netdev->name); +led_trigger_rename_static (name, priv->tx_led_trig); + +snprintf (name,sizeof (name),"%s-rx" , netdev->name); +led_trigger_rename_static (name, priv->rx_led_trig); + } + +return NOTIFY_DONE ; +} + +/* notifier block for netdevice event */ +static struct notifier_block can_netdev_notifier__read_mostly = { + .notifier_call = can_led_notifier, +}; + +int __init can_led_notifier_init (void ) +{ +return register_netdevice_notifier (&can_netdev_notifier); +} + +void __exit can_led_notifier_exit(void) +{ +unregister_netdevice_notifier (&can_netdev_notifier); +}
include/linux/can/led.h +9 -0
@@ -26,6 +26,8 @@ enum can_led_event {void can_led_event (struct net_device *netdev,enum can_led_event event);void devm_can_led_init (struct net_device *netdev); +int __init can_led_notifier_init (void ); +void __exit can_led_notifier_exit(void);#else
@@ -36,6 +38,13 @@ static inline void can_led_event(struct net_device *netdev,static inline void devm_can_led_init (struct net_device *netdev) { } +static inline int can_led_notifier_init (void ) +{ +return 0 ; +} +static inline void can_led_notifier_exit (void ) +{ +}#endif
Re: [PATCH] can: export a safe netdev_priv wrapper for candev
In reply to Kurt Van DijckQuoted 15 lines · Kurt Van Dijck
can: export a safe
netdev_privwrapper for candevIn
net_devicenotifier calls, it was impossible to determine if a CAN device is based on candev in a safe way. This patch adds such test in order to access candev storage from within those notifiers.Signed-off-by Kurt Van Dijck <[email protected]>drivers/net/can/dev.c +0
-0@@ -795,6 +795,18 @@ void unregister_candev(struct net_device *dev) }
drivers/net/can/dev.c void unregister_candev(struct net_device *dev) · line 805
Quoted 10 lines
drivers/net/can/dev.c +8
-0EXPORT_SYMBOL_GPL (unregister_candev); +/* +* Test if a network device is a candev based device +* and return the can_priv* if so. +*/ +struct can_priv *safe_candev_priv (struct net_device *dev) +{ +if ((dev->type !=ARPHRD_CAN ) || (dev->rtnl_link_ops != &can_link_ops)) +return NULL ;
Nice idea!
Quoted 21 lines
drivers/net/can/dev.c +4
-0+ +return netdev_priv (dev); +} +static __init int can_dev_init (void ) {int err;include/linux/can/dev.h +3
-0@@ -106,6 +106,9 @@ u8 can_len2dlc(u8 len);struct net_device *alloc_candev (int sizeof_priv,unsigned int echo_skb_max);void free_candev (struct net_device *dev); +/* a candev safe wrapper around netdev_priv */ +struct can_priv *safe_candev_priv (struct net_device *dev); +int open_candev (struct net_device *dev);void close_candev (struct net_device *dev);
Re: [PATCH v3] can: rename LED trigger name on netdev renames
In reply to Kurt Van DijckQuoted 40 lines · Kurt Van Dijck
can: rename LED trigger name on netdev renames
The LED trigger name for CAN devices is based on the initial CAN device name, but does never change. The LED trigger name is not guaranteed to be unique in case of hotplugging CAN devices.
This patch tries to address this problem by modifying the LED trigger name according to the CAN device name when the latter changes.
v1 - Kurt Van Dijck
v2 - Fabio Baltieri
- remove rename blocking if trigger is bound
- use led-subsystem function for the actual rename (still WiP)
- call init/exit functions fromdev.c
v3 - Kurt Van Dijck
- safe operation for non-candev based devices (vcan, slcan) based on earlier patchSigned-off-by Kurt Van Dijck <[email protected]>Signed-off-by Fabio Baltieri <[email protected]>--
Fabio,
I followed all your contribution to this patch.
drivers/net/can/dev.c +1
-0@@ -25,6 +25,7 @@#include <linux/can.h> #include <linux/can/dev.h> #include <linux/can/netlink.h> +#include <linux/can/led.h> #include <net/rtnetlink.h> #define MOD_DESC "CAN device driver interface"
drivers/net/can/dev.c static __init int can_dev_init(void) · line 818
Quoted 7 lines
drivers/net/can/dev.c +4
-0@@ -811,6 +812,10 @@ static __init int can_dev_init(void) {int err; +can_led_notifier_init (); + +can_led_notifier_init (); +
two times?
Quoted 28 lines
drivers/net/can/dev.c +2
-0err =rtnl_link_register (&can_link_ops);if (!err) printk(KERN_INFO MOD_DESC "\n");@@ -822,6 +827,8 @@ module_init(can_dev_init); static __exit void can_dev_exit(void) {rtnl_link_unregister (&can_link_ops); + +can_led_notifier_exit (); }module_exit (can_dev_exit);drivers/net/can/led.c +2
-0@@ -1,5 +1,6 @@/* * Copyright 2012, Fabio Baltieri <[email protected]> +* Copyright 2012, Kurt Van Dijck <[email protected]> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as @@ -87,3 +88,39 @@ void devm_can_led_init(struct net_device *netdev)devres_add (&netdev->dev, res); }EXPORT_SYMBOL_GPL (devm_can_led_init); +
drivers/net/can/led.c void devm_can_led_init(struct net_device *netdev) · line 101
Quoted 10 lines
drivers/net/can/led.c +10
-0+/* +* NETDEV rename notifier to rename the associated led triggers too +*/ +static int can_led_notifier (struct notifier_block *nb,unsigned long msg, +void *data) +{ +struct net_device *netdev = data; +struct can_priv *priv =safe_candev_priv (netdev); +char name[CAN_LED_NAME_SZ ]; +
What about
if (!priv)
return NOTIFY_DONE;
:-)
You created that nice function but you are accessing priv->tx_led_trig below without checking the output of save_candev_priv() ...
Regards,
Oliver
Quoted 56 lines
drivers/net/can/led.c +25
-0+if (msg ==NETDEV_CHANGENAME ) { +snprintf (name,sizeof (name),"%s-tx" , netdev->name); +led_trigger_rename_static (name, priv->tx_led_trig); + +snprintf (name,sizeof (name),"%s-rx" , netdev->name); +led_trigger_rename_static (name, priv->rx_led_trig); + } + +return NOTIFY_DONE ; +} + +/* notifier block for netdevice event */ +static struct notifier_block can_netdev_notifier__read_mostly = { + .notifier_call = can_led_notifier, +}; + +int __init can_led_notifier_init (void ) +{ +return register_netdevice_notifier (&can_netdev_notifier); +} + +void __exit can_led_notifier_exit(void) +{ +unregister_netdevice_notifier (&can_netdev_notifier); +}include/linux/can/led.h +9
-1@@ -26,6 +26,8 @@ enum can_led_event {void can_led_event (struct net_device *netdev,enum can_led_event event);void devm_can_led_init (struct net_device *netdev); +int __init can_led_notifier_init (void ); +void __exit can_led_notifier_exit(void);#else @@ -36,6 +38,13 @@ static inline void can_led_event(struct net_device *netdev,static inline void devm_can_led_init (struct net_device *netdev) { } +static inline int can_led_notifier_init (void ) +{ +return 0 ; +} +static inline void can_led_notifier_exit (void ) +{ +}#endif --To unsubscribe from this list: send the line "unsubscribe linux-can" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH] can: export a safe netdev_priv wrapper for candev
In reply to Kurt Van DijckHi Kurt,
Quoted 19 lines · Kurt Van Dijck
can: export a safe
netdev_privwrapper for candevIn
net_devicenotifier calls, it was impossible to determine if a CAN device is based on candev in a safe way. This patch adds such test in order to access candev storage from within those notifiers.Signed-off-by Kurt Van Dijck <[email protected]>drivers/net/can/dev.c +2
-0@@ -795,6 +795,18 @@ void unregister_candev(struct net_device *dev) }EXPORT_SYMBOL_GPL (unregister_candev); +/* +* Test if a network device is a candev based device
drivers/net/can/dev.c void unregister_candev(struct net_device *dev) · line 809
Quoted 10 lines
drivers/net/can/dev.c +10
-0+* and return the can_priv* if so. +*/ +struct can_priv *safe_candev_priv (struct net_device *dev) +{ +if ((dev->type !=ARPHRD_CAN ) || (dev->rtnl_link_ops != &can_link_ops)) +return NULL ; + +return netdev_priv (dev); +} +
No EXPORT_SYMBOL_GPL here?
Looks good to me a part from that.
Fabio
Signature · 1 line
Fabio Baltieri
Re: [PATCH v3] can: rename LED trigger name on netdev renames
In reply to Oliver HartkoppQuoted 4 lines · Oliver Hartkopp
Quoted 3 lines · Kurt Van Dijck
Fabio,
I followed all your contribution to this patch.
Thanks!
Quoted 66 lines
Quoted 13 lines
drivers/net/can/dev.c +1
-0@@ -25,6 +25,7 @@#include <linux/can.h> #include <linux/can/dev.h> #include <linux/can/netlink.h> +#include <linux/can/led.h> #include <net/rtnetlink.h> #define MOD_DESC "CAN device driver interface"drivers/net/can/dev.c static __init int can_dev_init(void) · line 818
Quoted 7 lines
drivers/net/can/dev.c +4
-0@@ -811,6 +812,10 @@ static __init int can_dev_init(void) {int err; +can_led_notifier_init (); + +can_led_notifier_init (); +two times?
Quoted 28 lines
drivers/net/can/dev.c +2
-0err =rtnl_link_register (&can_link_ops);if (!err) printk(KERN_INFO MOD_DESC "\n");@@ -822,6 +827,8 @@ module_init(can_dev_init); static __exit void can_dev_exit(void) {rtnl_link_unregister (&can_link_ops); + +can_led_notifier_exit (); }module_exit (can_dev_exit);drivers/net/can/led.c +2
-0@@ -1,5 +1,6 @@/* * Copyright 2012, Fabio Baltieri <[email protected]> +* Copyright 2012, Kurt Van Dijck <[email protected]> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as @@ -87,3 +88,39 @@ void devm_can_led_init(struct net_device *netdev)devres_add (&netdev->dev, res); }EXPORT_SYMBOL_GPL (devm_can_led_init); +drivers/net/can/led.c void devm_can_led_init(struct net_device *netdev) · line 101
Quoted 10 lines
drivers/net/can/led.c +10
-0+/* +* NETDEV rename notifier to rename the associated led triggers too +*/ +static int can_led_notifier (struct notifier_block *nb,unsigned long msg, +void *data) +{ +struct net_device *netdev = data; +struct can_priv *priv =safe_candev_priv (netdev); +char name[CAN_LED_NAME_SZ ]; +What about
Quoted 7 lines
if (!priv)
return NOTIFY_DONE;:-)
You created that nice function but you are accessing
priv->tx_led_trigbelow without checking the output ofsave_candev_priv()...
Also, I noticed you removed the namespace check... wasn't it needed? (just asking, I have no idea).
Quoted 4 lines
drivers/net/can/led.c void devm_can_led_init(struct net_device *netdev) · line 105
Quoted 4 lines
drivers/net/can/led.c +4
-0+if (msg ==NETDEV_CHANGENAME ) { +snprintf (name,sizeof (name),"%s-tx" , netdev->name); +led_trigger_rename_static (name, priv->tx_led_trig); +
whitespaces here? (checkpatch)
Fabio
Quoted 47 lines
Quoted 47 lines
drivers/net/can/led.c +21
-0+snprintf (name,sizeof (name),"%s-rx" , netdev->name); +led_trigger_rename_static (name, priv->rx_led_trig); + } + +return NOTIFY_DONE ; +} + +/* notifier block for netdevice event */ +static struct notifier_block can_netdev_notifier__read_mostly = { + .notifier_call = can_led_notifier, +}; + +int __init can_led_notifier_init (void ) +{ +return register_netdevice_notifier (&can_netdev_notifier); +} + +void __exit can_led_notifier_exit(void) +{ +unregister_netdevice_notifier (&can_netdev_notifier); +}include/linux/can/led.h +9
-0@@ -26,6 +26,8 @@ enum can_led_event {void can_led_event (struct net_device *netdev,enum can_led_event event);void devm_can_led_init (struct net_device *netdev); +int __init can_led_notifier_init (void ); +void __exit can_led_notifier_exit(void);#else @@ -36,6 +38,13 @@ static inline void can_led_event(struct net_device *netdev,static inline void devm_can_led_init (struct net_device *netdev) { } +static inline int can_led_notifier_init (void ) +{ +return 0 ; +} +static inline void can_led_notifier_exit (void ) +{ +}#endif
Signature · 1 line
Fabio Baltieri
Re: [PATCH v3] can: rename LED trigger name on netdev renames
Quoted 79 lines · Fabio Baltieri
Quoted 4 lines · Oliver Hartkopp
Quoted 3 lines · Kurt Van Dijck
Fabio,
I followed all your contribution to this patch.
Thanks!
Quoted 71 lines
Quoted 13 lines
drivers/net/can/dev.c +1
-0@@ -25,6 +25,7 @@#include <linux/can.h> #include <linux/can/dev.h> #include <linux/can/netlink.h> +#include <linux/can/led.h> #include <net/rtnetlink.h> #define MOD_DESC "CAN device driver interface"drivers/net/can/dev.c static __init int can_dev_init(void) · line 818
Quoted 7 lines
drivers/net/can/dev.c +4
-0@@ -811,6 +812,10 @@ static __init int can_dev_init(void) {int err; +can_led_notifier_init (); + +can_led_notifier_init (); +two times?
Quoted 28 lines
drivers/net/can/dev.c +2
-0err =rtnl_link_register (&can_link_ops);if (!err) printk(KERN_INFO MOD_DESC "\n");@@ -822,6 +827,8 @@ module_init(can_dev_init); static __exit void can_dev_exit(void) {rtnl_link_unregister (&can_link_ops); + +can_led_notifier_exit (); }module_exit (can_dev_exit);drivers/net/can/led.c +2
-0@@ -1,5 +1,6 @@/* * Copyright 2012, Fabio Baltieri <[email protected]> +* Copyright 2012, Kurt Van Dijck <[email protected]> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as @@ -87,3 +88,39 @@ void devm_can_led_init(struct net_device *netdev)devres_add (&netdev->dev, res); }EXPORT_SYMBOL_GPL (devm_can_led_init); +drivers/net/can/led.c void devm_can_led_init(struct net_device *netdev) · line 101
Quoted 10 lines
drivers/net/can/led.c +10
-0+/* +* NETDEV rename notifier to rename the associated led triggers too +*/ +static int can_led_notifier (struct notifier_block *nb,unsigned long msg, +void *data) +{ +struct net_device *netdev = data; +struct can_priv *priv =safe_candev_priv (netdev); +char name[CAN_LED_NAME_SZ ]; +What about
if (!priv)
return NOTIFY_DONE;:-)
Quoted 7 lines
Quoted 2 lines
You created that nice function but you are accessing
priv->tx_led_trigbelow without checking the output ofsave_candev_priv()...Also, I noticed you removed the namespace check... wasn't it needed? (just asking, I have no idea).
Quoted 1 line
drivers/net/can/led.c void devm_can_led_init(struct net_device *netdev) · line 102
Quoted 1 line
drivers/net/can/led.c +1
-0+if (msg ==NETDEV_CHANGENAME ) {
I do have a second remark.
What about always using the notifiers to name the LED triggers?
Looking into
http://lxr.linux.no/#linux+v3.5.3/include/linux/netdevice.h#L1526
we have NETDEV_CHANGENAME but what about
NETDEV_REGISTER
???
When changing and updating the LED trigger name always inside the notifier, we should never run into any locking or race conditions ...
What do you think about this idea?
Regards,
Oliver
Quoted 56 lines
Quoted 3 lines
drivers/net/can/led.c void devm_can_led_init(struct net_device *netdev) · line 105
Quoted 3 lines
drivers/net/can/led.c +3
-0+snprintf (name,sizeof (name),"%s-tx" , netdev->name); +led_trigger_rename_static (name, priv->tx_led_trig); +whitespaces here? (checkpatch)
Fabio
Quoted 47 lines
Quoted 47 lines
drivers/net/can/led.c +21
-0+snprintf (name,sizeof (name),"%s-rx" , netdev->name); +led_trigger_rename_static (name, priv->rx_led_trig); + } + +return NOTIFY_DONE ; +} + +/* notifier block for netdevice event */ +static struct notifier_block can_netdev_notifier__read_mostly = { + .notifier_call = can_led_notifier, +}; + +int __init can_led_notifier_init (void ) +{ +return register_netdevice_notifier (&can_netdev_notifier); +} + +void __exit can_led_notifier_exit(void) +{ +unregister_netdevice_notifier (&can_netdev_notifier); +}include/linux/can/led.h +9
-0@@ -26,6 +26,8 @@ enum can_led_event {void can_led_event (struct net_device *netdev,enum can_led_event event);void devm_can_led_init (struct net_device *netdev); +int __init can_led_notifier_init (void ); +void __exit can_led_notifier_exit(void);#else @@ -36,6 +38,13 @@ static inline void can_led_event(struct net_device *netdev,static inline void devm_can_led_init (struct net_device *netdev) { } +static inline int can_led_notifier_init (void ) +{ +return 0 ; +} +static inline void can_led_notifier_exit (void ) +{ +}#endif
PATCH v2 1/1 can: export a safe netdev_priv wrapper for candev
In reply to Fabio Baltierican: export a safe netdev_priv wrapper for candev
In net_device notifier calls, it was impossible to determine if a CAN device is based on candev in a safe way. This patch adds such test in order to access candev storage from within those notifiers.
drivers/net/can/dev.c +13 -0
@@ -795,6 +795,19 @@ void unregister_candev(struct net_device *dev) }EXPORT_SYMBOL_GPL (unregister_candev); +/* +* Test if a network device is a candev based device +* and return the can_priv* if so. +*/ +struct can_priv *safe_candev_priv (struct net_device *dev) +{ +if ((dev->type !=ARPHRD_CAN ) || (dev->rtnl_link_ops != &can_link_ops)) +return NULL ; + +return netdev_priv (dev); +} +EXPORT_SYMBOL_GPL (safe_candev_priv); +static __init int can_dev_init (void ) {int err;
include/linux/can/dev.h +3 -0
@@ -106,6 +106,9 @@ u8 can_len2dlc(u8 len);struct net_device *alloc_candev (int sizeof_priv,unsigned int echo_skb_max);void free_candev (struct net_device *dev); +/* a candev safe wrapper around netdev_priv */ +struct can_priv *safe_candev_priv (struct net_device *dev); +int open_candev (struct net_device *dev);void close_candev (struct net_device *dev);
PATCH v4 1/1 can: rename LED trigger name on netdev renames
In reply to Fabio Baltierican: rename LED trigger name on netdev renames
The LED trigger name for CAN devices is based on the initial CAN device name, but does never change. The LED trigger name is not guaranteed to be unique in case of hotplugging CAN devices.
This patch tries to address this problem by modifying the LED trigger name according to the CAN device name when the latter changes.
v1 - Kurt Van Dijck
v2 - Fabio Baltieri
- remove rename blocking if trigger is bound
- use led-subsystem function for the actual rename (still WiP)
- call init/exit functions from dev.c
v3 - Kurt Van Dijck
- safe operation for non-candev based devices (vcan, slcan) based on earlier patch
v4 - Kurt Van Dijck
- trivial patch mistakes fixed
drivers/net/can/dev.c +5 -0
@@ -25,6 +25,7 @@#include <linux/can.h> #include <linux/can/dev.h> #include <linux/can/netlink.h> +#include <linux/can/led.h> #include <net/rtnetlink.h> #define MOD_DESC "CAN device driver interface"
@@ -812,6 +813,8 @@ static __init int can_dev_init(void) {int err; +can_led_notifier_init (); + err =rtnl_link_register (&can_link_ops);if (!err) printk(KERN_INFO MOD_DESC "\n");
@@ -823,6 +826,8 @@ module_init(can_dev_init); static __exit void can_dev_exit(void) {rtnl_link_unregister (&can_link_ops); + +can_led_notifier_exit (); }module_exit (can_dev_exit);
drivers/net/can/led.c +40 -0
@@ -1,5 +1,6 @@/* * Copyright 2012, Fabio Baltieri <[email protected]> +* Copyright 2012, Kurt Van Dijck <[email protected]> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as
@@ -87,3 +88,42 @@ void devm_can_led_init(struct net_device *netdev)devres_add (&netdev->dev, res); }EXPORT_SYMBOL_GPL (devm_can_led_init); + +/* +* NETDEV rename notifier to rename the associated led triggers too +*/ +static int can_led_notifier (struct notifier_block *nb,unsigned long msg, +void *data) +{ +struct net_device *netdev = data; +struct can_priv *priv =safe_candev_priv (netdev); +char name[CAN_LED_NAME_SZ ]; + +if (!priv) +return NOTIFY_DONE ; + +if (msg ==NETDEV_CHANGENAME ) { +snprintf (name,sizeof (name),"%s-tx" , netdev->name); +led_trigger_rename_static (name, priv->tx_led_trig); + +snprintf (name,sizeof (name),"%s-rx" , netdev->name); +led_trigger_rename_static (name, priv->rx_led_trig); + } + +return NOTIFY_DONE ; +} + +/* notifier block for netdevice event */ +static struct notifier_block can_netdev_notifier__read_mostly = { + .notifier_call = can_led_notifier, +}; + +int __init can_led_notifier_init (void ) +{ +return register_netdevice_notifier (&can_netdev_notifier); +} + +void __exit can_led_notifier_exit(void) +{ +unregister_netdevice_notifier (&can_netdev_notifier); +}
include/linux/can/led.h +9 -0
@@ -26,6 +26,8 @@ enum can_led_event {void can_led_event (struct net_device *netdev,enum can_led_event event);void devm_can_led_init (struct net_device *netdev); +int __init can_led_notifier_init (void ); +void __exit can_led_notifier_exit(void);#else
@@ -36,6 +38,13 @@ static inline void can_led_event(struct net_device *netdev,static inline void devm_can_led_init (struct net_device *netdev) { } +static inline int can_led_notifier_init (void ) +{ +return 0 ; +} +static inline void can_led_notifier_exit (void ) +{ +}#endif
Re: [PATCH v3] can: rename LED trigger name on netdev renames
In reply to Oliver HartkoppQuoted 8 lines · Oliver Hartkopp
Quoted 6 lines · Fabio Baltieri
Quoted 4 lines · Oliver Hartkopp
Quoted 3 lines · Kurt Van Dijck
Fabio,
I followed all your contribution to this patch.
Quoted 8 lines
Quoted 8 lines
Thanks!
Quoted 3 lines
You created that nice function but you are accessing
priv->tx_led_trigbelow without checking the output ofsave_candev_priv()...Also, I noticed you removed the namespace check... wasn't it needed? (just asking, I have no idea).
I don't actually know.
Anyone?
Quoted 14 lines
Quoted 2 lines
Quoted 2 lines
drivers/net/can/led.c void devm_can_led_init(struct net_device *netdev) · line 102
Quoted 2 lines
drivers/net/can/led.c +1
-0+if (msg ==NETDEV_CHANGENAME ) {I do have a second remark.
What about always using the notifiers to name the LED triggers?
Looking into
http://lxr.linux.no/#linux+v3.5.3/include/linux/netdevice.h#L1526
we have
NETDEV_CHANGENAMEbut what about
Quoted 9 lines
NETDEV_REGISTER???
When changing and updating the LED trigger name always inside the notifier, we should never run into any locking or race conditions ...
What do you think about this idea?
But the initial name must have been set properly already in that case...
Kurt
Re: [PATCH v3] can: rename LED trigger name on netdev renames
Quoted 17 lines · Kurt Van Dijck
Quoted 15 lines · Oliver Hartkopp
Quoted 1 line
Quoted 1 line
drivers/net/can/led.c void devm_can_led_init(struct net_device *netdev) · line 102
Quoted 1 line
drivers/net/can/led.c +1
-0+if (msg ==NETDEV_CHANGENAME ) {I do have a second remark.
What about always using the notifiers to name the LED triggers?
Looking into
http://lxr.linux.no/#linux+v3.5.3/include/linux/netdevice.h#L1526
we have
NETDEV_CHANGENAMEbut what about
NETDEV_REGISTER
Quoted 9 lines
Quoted 7 lines
???
When changing and updating the LED trigger name always inside the notifier, we should never run into any locking or race conditions ...
What do you think about this idea?
But the initial name must have been set properly already in that case...
Yes it is.
See register_netdevice()
http://lxr.linux.no/#linux+v3.5.3/net/core/dev.c#L5453
at the end NETDEV_REGISTER is notified:
http://lxr.linux.no/#linux+v3.5.3/net/core/dev.c#L5552
So it would be pretty elegant to create the LED trigger name in the notifier. This would allow to remove that code at the current init function too.
Regards,
Oliver
Re: [PATCH v3] can: rename LED trigger name on netdev renames
Hi Oliver,
Quoted 10 lines · Oliver Hartkopp
Quoted 1 line · Kurt Van Dijck
But the initial name must have been set properly already in that case...
Yes it is.
See
register_netdevice()
Quoted 6 lines
at the end
NETDEV_REGISTERis notified:http://lxr.linux.no/#linux+v3.5.3/net/core/dev.c#L5552
So it would be pretty elegant to create the LED trigger name in the notifier. This would allow to remove that code at the current init function too.
That would indeed be elegant, but in the current implementation triggers only works for supported drivers, while putting registration in the notifier would imply to use it also in unsupported ones, where you would get a trigger which never triggers.
Is that what you had in mind?
Of course that would simplify another bit trigger integration to the driver... if the target is to have support for all drivers that's a good thing, through I think that triggers are mostly useful on embedded drivers.
Cheers,
Fabio
Signature · 1 line
Fabio Baltieri
Re: [PATCH v3] can: rename LED trigger name on netdev renames
In reply to Kurt Van DijckQuoted 11 lines · Kurt Van Dijck
Quoted 9 lines · Oliver Hartkopp
Quoted 7 lines · Fabio Baltieri
Quoted 3 lines · Oliver Hartkopp
Quoted 2 lines · Kurt Van Dijck
Fabio,
Also, I noticed you removed the namespace check... wasn't it needed? (just asking, I have no idea).
Moreover, the function tests if the net_device has an associated can_priv. The namespace it's in is irrelevant then?
I think those tests may be removed in af_can too.
Kurt
Re: [PATCH v4] can: rename LED trigger name on netdev renames
In reply to Kurt Van DijckHi Kurt,
Quoted 10 lines · Kurt Van Dijck
can: rename LED trigger name on netdev renames
The LED trigger name for CAN devices is based on the initial CAN device name, but does never change. The LED trigger name is not guaranteed to be unique in case of hotplugging CAN devices.
This patch tries to address this problem by modifying the LED trigger name according to the CAN device name when the latter changes.
Quoted 10 lines
v1 - Kurt Van Dijck
v2 - Fabio Baltieri
- remove rename blocking if trigger is bound
- use led-subsystem function for the actual rename (still WiP)
- call init/exit functions fromdev.c
v3 - Kurt Van Dijck
- safe operation for non-candev based devices (vcan, slcan) based on earlier patch
v4 - Kurt Van Dijck
- trivial patch mistakes fixed
Well done, I'm testing the last patches (my can-leds-devel branch on github) and seems to work good (tested rename, disconnect-reconnect, vcan rename, rename from udev rule).
Now I think we just have to wait for Bryan for the LED rename patch.
Fabio
Signature · 1 line
Fabio Baltieri
Re: [PATCH v3] can: rename LED trigger name on netdev renames
In reply to Fabio BaltieriQuoted 17 lines · Fabio Baltieri
Hi Oliver,
Quoted 14 lines · Oliver Hartkopp
Quoted 1 line · Kurt Van Dijck
But the initial name must have been set properly already in that case...
Yes it is.
See
register_netdevice()http://lxr.linux.no/#linux+v3.5.3/net/core/dev.c#L5453
at the end
NETDEV_REGISTERis notified:
Quoted 7 lines
Quoted 2 lines
So it would be pretty elegant to create the LED trigger name in the notifier. This would allow to remove that code at the current init function too.
That would indeed be elegant, but in the current implementation triggers only works for supported drivers, while putting registration in the notifier would imply to use it also in unsupported ones, where you would get a trigger which never triggers.
I note here another problem: any candev rename causes the notifier to rename its led triggers, but some have it never registered.
Oliver, your approach is the better. It took a bit for me to see ... but this requires a bit of reverting in the drivers.
Kind regards,
Kurt