leds.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2015, Heiner Kallweit <hkallweit1@gmail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <net/bluetooth/bluetooth.h>
  9. #include <net/bluetooth/hci_core.h>
  10. #include "leds.h"
  11. DEFINE_LED_TRIGGER(bt_power_led_trigger);
  12. struct hci_basic_led_trigger {
  13. struct led_trigger led_trigger;
  14. struct hci_dev *hdev;
  15. };
  16. #define to_hci_basic_led_trigger(arg) container_of(arg, \
  17. struct hci_basic_led_trigger, led_trigger)
  18. void hci_leds_update_powered(struct hci_dev *hdev, bool enabled)
  19. {
  20. if (hdev->power_led)
  21. led_trigger_event(hdev->power_led,
  22. enabled ? LED_FULL : LED_OFF);
  23. if (!enabled) {
  24. struct hci_dev *d;
  25. read_lock(&hci_dev_list_lock);
  26. list_for_each_entry(d, &hci_dev_list, list) {
  27. if (test_bit(HCI_UP, &d->flags))
  28. enabled = true;
  29. }
  30. read_unlock(&hci_dev_list_lock);
  31. }
  32. led_trigger_event(bt_power_led_trigger, enabled ? LED_FULL : LED_OFF);
  33. }
  34. static void power_activate(struct led_classdev *led_cdev)
  35. {
  36. struct hci_basic_led_trigger *htrig;
  37. bool powered;
  38. htrig = to_hci_basic_led_trigger(led_cdev->trigger);
  39. powered = test_bit(HCI_UP, &htrig->hdev->flags);
  40. led_trigger_event(led_cdev->trigger, powered ? LED_FULL : LED_OFF);
  41. }
  42. static struct led_trigger *led_allocate_basic(struct hci_dev *hdev,
  43. void (*activate)(struct led_classdev *led_cdev),
  44. const char *name)
  45. {
  46. struct hci_basic_led_trigger *htrig;
  47. htrig = devm_kzalloc(&hdev->dev, sizeof(*htrig), GFP_KERNEL);
  48. if (!htrig)
  49. return NULL;
  50. htrig->hdev = hdev;
  51. htrig->led_trigger.activate = activate;
  52. htrig->led_trigger.name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
  53. "%s-%s", hdev->name,
  54. name);
  55. if (!htrig->led_trigger.name)
  56. goto err_alloc;
  57. if (devm_led_trigger_register(&hdev->dev, &htrig->led_trigger))
  58. goto err_register;
  59. return &htrig->led_trigger;
  60. err_register:
  61. devm_kfree(&hdev->dev, (void *)htrig->led_trigger.name);
  62. err_alloc:
  63. devm_kfree(&hdev->dev, htrig);
  64. return NULL;
  65. }
  66. void hci_leds_init(struct hci_dev *hdev)
  67. {
  68. /* initialize power_led */
  69. hdev->power_led = led_allocate_basic(hdev, power_activate, "power");
  70. }
  71. void bt_leds_init(void)
  72. {
  73. led_trigger_register_simple("bluetooth-power", &bt_power_led_trigger);
  74. }
  75. void bt_leds_cleanup(void)
  76. {
  77. led_trigger_unregister_simple(bt_power_led_trigger);
  78. }