blob: 6d59a5023231f702956367065d45d6a114ae7811 [file] [log] [blame]
Heiner Kallweit6d5d2ee2016-01-08 19:28:58 +01001/*
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
9#include <net/bluetooth/bluetooth.h>
10#include <net/bluetooth/hci_core.h>
11
12#include "leds.h"
13
Marcel Holtmanne64c97b2016-07-21 14:12:41 +020014DEFINE_LED_TRIGGER(bt_power_led_trigger);
15
Heiner Kallweit6d5d2ee2016-01-08 19:28:58 +010016struct hci_basic_led_trigger {
17 struct led_trigger led_trigger;
18 struct hci_dev *hdev;
19};
20
21#define to_hci_basic_led_trigger(arg) container_of(arg, \
22 struct hci_basic_led_trigger, led_trigger)
23
24void hci_leds_update_powered(struct hci_dev *hdev, bool enabled)
25{
26 if (hdev->power_led)
27 led_trigger_event(hdev->power_led,
28 enabled ? LED_FULL : LED_OFF);
Marcel Holtmanne64c97b2016-07-21 14:12:41 +020029
30 if (!enabled) {
31 struct hci_dev *d;
32
33 read_lock(&hci_dev_list_lock);
34
35 list_for_each_entry(d, &hci_dev_list, list) {
36 if (test_bit(HCI_UP, &d->flags))
37 enabled = true;
38 }
39
40 read_unlock(&hci_dev_list_lock);
41 }
42
43 led_trigger_event(bt_power_led_trigger, enabled ? LED_FULL : LED_OFF);
Heiner Kallweit6d5d2ee2016-01-08 19:28:58 +010044}
45
Uwe Kleine-König2282e1252018-07-02 22:05:21 +020046static int power_activate(struct led_classdev *led_cdev)
Heiner Kallweit6d5d2ee2016-01-08 19:28:58 +010047{
48 struct hci_basic_led_trigger *htrig;
49 bool powered;
50
51 htrig = to_hci_basic_led_trigger(led_cdev->trigger);
52 powered = test_bit(HCI_UP, &htrig->hdev->flags);
53
54 led_trigger_event(led_cdev->trigger, powered ? LED_FULL : LED_OFF);
Uwe Kleine-König2282e1252018-07-02 22:05:21 +020055
56 return 0;
Heiner Kallweit6d5d2ee2016-01-08 19:28:58 +010057}
58
59static struct led_trigger *led_allocate_basic(struct hci_dev *hdev,
Uwe Kleine-König2282e1252018-07-02 22:05:21 +020060 int (*activate)(struct led_classdev *led_cdev),
Heiner Kallweit6d5d2ee2016-01-08 19:28:58 +010061 const char *name)
62{
63 struct hci_basic_led_trigger *htrig;
64
65 htrig = devm_kzalloc(&hdev->dev, sizeof(*htrig), GFP_KERNEL);
66 if (!htrig)
67 return NULL;
68
69 htrig->hdev = hdev;
70 htrig->led_trigger.activate = activate;
71 htrig->led_trigger.name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
72 "%s-%s", hdev->name,
73 name);
74 if (!htrig->led_trigger.name)
75 goto err_alloc;
76
Heiner Kallweitb6e402f2016-01-25 20:46:21 +010077 if (devm_led_trigger_register(&hdev->dev, &htrig->led_trigger))
Heiner Kallweit6d5d2ee2016-01-08 19:28:58 +010078 goto err_register;
79
80 return &htrig->led_trigger;
81
82err_register:
83 devm_kfree(&hdev->dev, (void *)htrig->led_trigger.name);
84err_alloc:
85 devm_kfree(&hdev->dev, htrig);
86 return NULL;
87}
88
89void hci_leds_init(struct hci_dev *hdev)
90{
91 /* initialize power_led */
92 hdev->power_led = led_allocate_basic(hdev, power_activate, "power");
93}
Marcel Holtmanne64c97b2016-07-21 14:12:41 +020094
95void bt_leds_init(void)
96{
97 led_trigger_register_simple("bluetooth-power", &bt_power_led_trigger);
98}
99
100void bt_leds_cleanup(void)
101{
102 led_trigger_unregister_simple(bt_power_led_trigger);
103}