Fabio Baltieri | 996a953 | 2012-12-18 18:50:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012, Fabio Baltieri <fabio.baltieri@gmail.com> |
Kurt Van Dijck | a1ef7bd | 2012-12-18 18:50:57 +0100 | [diff] [blame] | 3 | * Copyright 2012, Kurt Van Dijck <kurt.van.dijck@eia.be> |
Fabio Baltieri | 996a953 | 2012-12-18 18:50:55 +0100 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/device.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/netdevice.h> |
| 15 | #include <linux/can/dev.h> |
| 16 | |
| 17 | #include <linux/can/led.h> |
| 18 | |
| 19 | static unsigned long led_delay = 50; |
| 20 | module_param(led_delay, ulong, 0644); |
| 21 | MODULE_PARM_DESC(led_delay, |
| 22 | "blink delay time for activity leds (msecs, default: 50)."); |
| 23 | |
| 24 | /* Trigger a LED event in response to a CAN device event */ |
| 25 | void can_led_event(struct net_device *netdev, enum can_led_event event) |
| 26 | { |
| 27 | struct can_priv *priv = netdev_priv(netdev); |
| 28 | |
| 29 | switch (event) { |
| 30 | case CAN_LED_EVENT_OPEN: |
| 31 | led_trigger_event(priv->tx_led_trig, LED_FULL); |
| 32 | led_trigger_event(priv->rx_led_trig, LED_FULL); |
Yegor Yefremov | c54eb70 | 2015-03-16 09:38:13 +0100 | [diff] [blame] | 33 | led_trigger_event(priv->rxtx_led_trig, LED_FULL); |
Fabio Baltieri | 996a953 | 2012-12-18 18:50:55 +0100 | [diff] [blame] | 34 | break; |
| 35 | case CAN_LED_EVENT_STOP: |
| 36 | led_trigger_event(priv->tx_led_trig, LED_OFF); |
| 37 | led_trigger_event(priv->rx_led_trig, LED_OFF); |
Yegor Yefremov | c54eb70 | 2015-03-16 09:38:13 +0100 | [diff] [blame] | 38 | led_trigger_event(priv->rxtx_led_trig, LED_OFF); |
Fabio Baltieri | 996a953 | 2012-12-18 18:50:55 +0100 | [diff] [blame] | 39 | break; |
| 40 | case CAN_LED_EVENT_TX: |
Yegor Yefremov | c54eb70 | 2015-03-16 09:38:13 +0100 | [diff] [blame] | 41 | if (led_delay) { |
Fabio Baltieri | 996a953 | 2012-12-18 18:50:55 +0100 | [diff] [blame] | 42 | led_trigger_blink_oneshot(priv->tx_led_trig, |
| 43 | &led_delay, &led_delay, 1); |
Yegor Yefremov | c54eb70 | 2015-03-16 09:38:13 +0100 | [diff] [blame] | 44 | led_trigger_blink_oneshot(priv->rxtx_led_trig, |
| 45 | &led_delay, &led_delay, 1); |
| 46 | } |
Fabio Baltieri | 996a953 | 2012-12-18 18:50:55 +0100 | [diff] [blame] | 47 | break; |
| 48 | case CAN_LED_EVENT_RX: |
Yegor Yefremov | c54eb70 | 2015-03-16 09:38:13 +0100 | [diff] [blame] | 49 | if (led_delay) { |
Fabio Baltieri | 996a953 | 2012-12-18 18:50:55 +0100 | [diff] [blame] | 50 | led_trigger_blink_oneshot(priv->rx_led_trig, |
| 51 | &led_delay, &led_delay, 1); |
Yegor Yefremov | c54eb70 | 2015-03-16 09:38:13 +0100 | [diff] [blame] | 52 | led_trigger_blink_oneshot(priv->rxtx_led_trig, |
| 53 | &led_delay, &led_delay, 1); |
| 54 | } |
Fabio Baltieri | 996a953 | 2012-12-18 18:50:55 +0100 | [diff] [blame] | 55 | break; |
| 56 | } |
| 57 | } |
| 58 | EXPORT_SYMBOL_GPL(can_led_event); |
| 59 | |
| 60 | static void can_led_release(struct device *gendev, void *res) |
| 61 | { |
| 62 | struct can_priv *priv = netdev_priv(to_net_dev(gendev)); |
| 63 | |
| 64 | led_trigger_unregister_simple(priv->tx_led_trig); |
| 65 | led_trigger_unregister_simple(priv->rx_led_trig); |
Yegor Yefremov | c54eb70 | 2015-03-16 09:38:13 +0100 | [diff] [blame] | 66 | led_trigger_unregister_simple(priv->rxtx_led_trig); |
Fabio Baltieri | 996a953 | 2012-12-18 18:50:55 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /* Register CAN LED triggers for a CAN device |
| 70 | * |
| 71 | * This is normally called from a driver's probe function |
| 72 | */ |
| 73 | void devm_can_led_init(struct net_device *netdev) |
| 74 | { |
| 75 | struct can_priv *priv = netdev_priv(netdev); |
| 76 | void *res; |
| 77 | |
| 78 | res = devres_alloc(can_led_release, 0, GFP_KERNEL); |
| 79 | if (!res) { |
| 80 | netdev_err(netdev, "cannot register LED triggers\n"); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | snprintf(priv->tx_led_trig_name, sizeof(priv->tx_led_trig_name), |
| 85 | "%s-tx", netdev->name); |
| 86 | snprintf(priv->rx_led_trig_name, sizeof(priv->rx_led_trig_name), |
| 87 | "%s-rx", netdev->name); |
Yegor Yefremov | c54eb70 | 2015-03-16 09:38:13 +0100 | [diff] [blame] | 88 | snprintf(priv->rxtx_led_trig_name, sizeof(priv->rxtx_led_trig_name), |
| 89 | "%s-rxtx", netdev->name); |
Fabio Baltieri | 996a953 | 2012-12-18 18:50:55 +0100 | [diff] [blame] | 90 | |
| 91 | led_trigger_register_simple(priv->tx_led_trig_name, |
| 92 | &priv->tx_led_trig); |
| 93 | led_trigger_register_simple(priv->rx_led_trig_name, |
| 94 | &priv->rx_led_trig); |
Yegor Yefremov | c54eb70 | 2015-03-16 09:38:13 +0100 | [diff] [blame] | 95 | led_trigger_register_simple(priv->rxtx_led_trig_name, |
| 96 | &priv->rxtx_led_trig); |
Fabio Baltieri | 996a953 | 2012-12-18 18:50:55 +0100 | [diff] [blame] | 97 | |
| 98 | devres_add(&netdev->dev, res); |
| 99 | } |
| 100 | EXPORT_SYMBOL_GPL(devm_can_led_init); |
Kurt Van Dijck | a1ef7bd | 2012-12-18 18:50:57 +0100 | [diff] [blame] | 101 | |
| 102 | /* NETDEV rename notifier to rename the associated led triggers too */ |
| 103 | static int can_led_notifier(struct notifier_block *nb, unsigned long msg, |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 104 | void *ptr) |
Kurt Van Dijck | a1ef7bd | 2012-12-18 18:50:57 +0100 | [diff] [blame] | 105 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 106 | struct net_device *netdev = netdev_notifier_info_to_dev(ptr); |
Kurt Van Dijck | a1ef7bd | 2012-12-18 18:50:57 +0100 | [diff] [blame] | 107 | struct can_priv *priv = safe_candev_priv(netdev); |
| 108 | char name[CAN_LED_NAME_SZ]; |
| 109 | |
| 110 | if (!priv) |
| 111 | return NOTIFY_DONE; |
| 112 | |
Yegor Yefremov | c54eb70 | 2015-03-16 09:38:13 +0100 | [diff] [blame] | 113 | if (!priv->tx_led_trig || !priv->rx_led_trig || !priv->rxtx_led_trig) |
Oliver Hartkopp | 45fb4f8 | 2014-05-27 13:30:56 +0200 | [diff] [blame] | 114 | return NOTIFY_DONE; |
| 115 | |
Kurt Van Dijck | a1ef7bd | 2012-12-18 18:50:57 +0100 | [diff] [blame] | 116 | if (msg == NETDEV_CHANGENAME) { |
| 117 | snprintf(name, sizeof(name), "%s-tx", netdev->name); |
| 118 | led_trigger_rename_static(name, priv->tx_led_trig); |
| 119 | |
| 120 | snprintf(name, sizeof(name), "%s-rx", netdev->name); |
| 121 | led_trigger_rename_static(name, priv->rx_led_trig); |
Yegor Yefremov | c54eb70 | 2015-03-16 09:38:13 +0100 | [diff] [blame] | 122 | |
| 123 | snprintf(name, sizeof(name), "%s-rxtx", netdev->name); |
| 124 | led_trigger_rename_static(name, priv->rxtx_led_trig); |
Kurt Van Dijck | a1ef7bd | 2012-12-18 18:50:57 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | return NOTIFY_DONE; |
| 128 | } |
| 129 | |
| 130 | /* notifier block for netdevice event */ |
| 131 | static struct notifier_block can_netdev_notifier __read_mostly = { |
| 132 | .notifier_call = can_led_notifier, |
| 133 | }; |
| 134 | |
| 135 | int __init can_led_notifier_init(void) |
| 136 | { |
| 137 | return register_netdevice_notifier(&can_netdev_notifier); |
| 138 | } |
| 139 | |
| 140 | void __exit can_led_notifier_exit(void) |
| 141 | { |
| 142 | unregister_netdevice_notifier(&can_netdev_notifier); |
| 143 | } |