led.c 4.1 KB

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