leds.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 int 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. return 0;
  42. }
  43. static struct led_trigger *led_allocate_basic(struct hci_dev *hdev,
  44. int (*activate)(struct led_classdev *led_cdev),
  45. const char *name)
  46. {
  47. struct hci_basic_led_trigger *htrig;
  48. htrig = devm_kzalloc(&hdev->dev, sizeof(*htrig), GFP_KERNEL);
  49. if (!htrig)
  50. return NULL;
  51. htrig->hdev = hdev;
  52. htrig->led_trigger.activate = activate;
  53. htrig->led_trigger.name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
  54. "%s-%s", hdev->name,
  55. name);
  56. if (!htrig->led_trigger.name)
  57. goto err_alloc;
  58. if (devm_led_trigger_register(&hdev->dev, &htrig->led_trigger))
  59. goto err_register;
  60. return &htrig->led_trigger;
  61. err_register:
  62. devm_kfree(&hdev->dev, (void *)htrig->led_trigger.name);
  63. err_alloc:
  64. devm_kfree(&hdev->dev, htrig);
  65. return NULL;
  66. }
  67. void hci_leds_init(struct hci_dev *hdev)
  68. {
  69. /* initialize power_led */
  70. hdev->power_led = led_allocate_basic(hdev, power_activate, "power");
  71. }
  72. void bt_leds_init(void)
  73. {
  74. led_trigger_register_simple("bluetooth-power", &bt_power_led_trigger);
  75. }
  76. void bt_leds_cleanup(void)
  77. {
  78. led_trigger_unregister_simple(bt_power_led_trigger);
  79. }