led.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2012, Fabio Baltieri <fabio.baltieri@gmail.com>
  4. * Copyright 2012, Kurt Van Dijck <kurt.van.dijck@eia.be>
  5. */
  6. #include <linux/module.h>
  7. #include <linux/device.h>
  8. #include <linux/kernel.h>
  9. #include <linux/slab.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/can/dev.h>
  12. #include <linux/can/led.h>
  13. static unsigned long led_delay = 50;
  14. module_param(led_delay, ulong, 0644);
  15. MODULE_PARM_DESC(led_delay,
  16. "blink delay time for activity leds (msecs, default: 50).");
  17. /* Trigger a LED event in response to a CAN device event */
  18. void can_led_event(struct net_device *netdev, enum can_led_event event)
  19. {
  20. struct can_priv *priv = netdev_priv(netdev);
  21. switch (event) {
  22. case CAN_LED_EVENT_OPEN:
  23. led_trigger_event(priv->tx_led_trig, LED_FULL);
  24. led_trigger_event(priv->rx_led_trig, LED_FULL);
  25. led_trigger_event(priv->rxtx_led_trig, LED_FULL);
  26. break;
  27. case CAN_LED_EVENT_STOP:
  28. led_trigger_event(priv->tx_led_trig, LED_OFF);
  29. led_trigger_event(priv->rx_led_trig, LED_OFF);
  30. led_trigger_event(priv->rxtx_led_trig, LED_OFF);
  31. break;
  32. case CAN_LED_EVENT_TX:
  33. if (led_delay) {
  34. led_trigger_blink_oneshot(priv->tx_led_trig,
  35. &led_delay, &led_delay, 1);
  36. led_trigger_blink_oneshot(priv->rxtx_led_trig,
  37. &led_delay, &led_delay, 1);
  38. }
  39. break;
  40. case CAN_LED_EVENT_RX:
  41. if (led_delay) {
  42. led_trigger_blink_oneshot(priv->rx_led_trig,
  43. &led_delay, &led_delay, 1);
  44. led_trigger_blink_oneshot(priv->rxtx_led_trig,
  45. &led_delay, &led_delay, 1);
  46. }
  47. break;
  48. }
  49. }
  50. EXPORT_SYMBOL_GPL(can_led_event);
  51. static void can_led_release(struct device *gendev, void *res)
  52. {
  53. struct can_priv *priv = netdev_priv(to_net_dev(gendev));
  54. led_trigger_unregister_simple(priv->tx_led_trig);
  55. led_trigger_unregister_simple(priv->rx_led_trig);
  56. led_trigger_unregister_simple(priv->rxtx_led_trig);
  57. }
  58. /* Register CAN LED triggers for a CAN device
  59. *
  60. * This is normally called from a driver's probe function
  61. */
  62. void devm_can_led_init(struct net_device *netdev)
  63. {
  64. struct can_priv *priv = netdev_priv(netdev);
  65. void *res;
  66. res = devres_alloc(can_led_release, 0, GFP_KERNEL);
  67. if (!res) {
  68. netdev_err(netdev, "cannot register LED triggers\n");
  69. return;
  70. }
  71. snprintf(priv->tx_led_trig_name, sizeof(priv->tx_led_trig_name),
  72. "%s-tx", netdev->name);
  73. snprintf(priv->rx_led_trig_name, sizeof(priv->rx_led_trig_name),
  74. "%s-rx", netdev->name);
  75. snprintf(priv->rxtx_led_trig_name, sizeof(priv->rxtx_led_trig_name),
  76. "%s-rxtx", netdev->name);
  77. led_trigger_register_simple(priv->tx_led_trig_name,
  78. &priv->tx_led_trig);
  79. led_trigger_register_simple(priv->rx_led_trig_name,
  80. &priv->rx_led_trig);
  81. led_trigger_register_simple(priv->rxtx_led_trig_name,
  82. &priv->rxtx_led_trig);
  83. devres_add(&netdev->dev, res);
  84. }
  85. EXPORT_SYMBOL_GPL(devm_can_led_init);
  86. /* NETDEV rename notifier to rename the associated led triggers too */
  87. static int can_led_notifier(struct notifier_block *nb, unsigned long msg,
  88. void *ptr)
  89. {
  90. struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
  91. struct can_priv *priv = safe_candev_priv(netdev);
  92. char name[CAN_LED_NAME_SZ];
  93. if (!priv)
  94. return NOTIFY_DONE;
  95. if (!priv->tx_led_trig || !priv->rx_led_trig || !priv->rxtx_led_trig)
  96. return NOTIFY_DONE;
  97. if (msg == NETDEV_CHANGENAME) {
  98. snprintf(name, sizeof(name), "%s-tx", netdev->name);
  99. led_trigger_rename_static(name, priv->tx_led_trig);
  100. snprintf(name, sizeof(name), "%s-rx", netdev->name);
  101. led_trigger_rename_static(name, priv->rx_led_trig);
  102. snprintf(name, sizeof(name), "%s-rxtx", netdev->name);
  103. led_trigger_rename_static(name, priv->rxtx_led_trig);
  104. }
  105. return NOTIFY_DONE;
  106. }
  107. /* notifier block for netdevice event */
  108. static struct notifier_block can_netdev_notifier __read_mostly = {
  109. .notifier_call = can_led_notifier,
  110. };
  111. int __init can_led_notifier_init(void)
  112. {
  113. return register_netdevice_notifier(&can_netdev_notifier);
  114. }
  115. void __exit can_led_notifier_exit(void)
  116. {
  117. unregister_netdevice_notifier(&can_netdev_notifier);
  118. }