add b43
[openwrt.git] / package / b43 / src / leds.c
1 /*
2
3   Broadcom B43 wireless driver
4   LED control
5
6   Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
7   Copyright (c) 2005 Stefano Brivio <st3@riseup.net>
8   Copyright (c) 2005-2007 Michael Buesch <mb@bu3sch.de>
9   Copyright (c) 2005 Danny van Dyk <kugelfang@gentoo.org>
10   Copyright (c) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
11
12   This program is free software; you can redistribute it and/or modify
13   it under the terms of the GNU General Public License as published by
14   the Free Software Foundation; either version 2 of the License, or
15   (at your option) any later version.
16
17   This program is distributed in the hope that it will be useful,
18   but WITHOUT ANY WARRANTY; without even the implied warranty of
19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20   GNU General Public License for more details.
21
22   You should have received a copy of the GNU General Public License
23   along with this program; see the file COPYING.  If not, write to
24   the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
25   Boston, MA 02110-1301, USA.
26
27 */
28
29 #include "b43.h"
30 #include "leds.h"
31
32 static void b43_led_turn_on(struct b43_wldev *dev, u8 led_index,
33                             bool activelow)
34 {
35         struct b43_wl *wl = dev->wl;
36         unsigned long flags;
37         u16 ctl;
38
39         spin_lock_irqsave(&wl->leds_lock, flags);
40         ctl = b43_read16(dev, B43_MMIO_GPIO_CONTROL);
41         if (activelow)
42                 ctl &= ~(1 << led_index);
43         else
44                 ctl |= (1 << led_index);
45         b43_write16(dev, B43_MMIO_GPIO_CONTROL, ctl);
46         spin_unlock_irqrestore(&wl->leds_lock, flags);
47 }
48
49 static void b43_led_turn_off(struct b43_wldev *dev, u8 led_index,
50                              bool activelow)
51 {
52         struct b43_wl *wl = dev->wl;
53         unsigned long flags;
54         u16 ctl;
55
56         spin_lock_irqsave(&wl->leds_lock, flags);
57         ctl = b43_read16(dev, B43_MMIO_GPIO_CONTROL);
58         if (activelow)
59                 ctl |= (1 << led_index);
60         else
61                 ctl &= ~(1 << led_index);
62         b43_write16(dev, B43_MMIO_GPIO_CONTROL, ctl);
63         spin_unlock_irqrestore(&wl->leds_lock, flags);
64 }
65
66 /* Callback from the LED subsystem. */
67 static void b43_led_brightness_set(struct led_classdev *led_dev,
68                                    enum led_brightness brightness)
69 {
70         struct b43_led *led = container_of(led_dev, struct b43_led, led_dev);
71         struct b43_wldev *dev = led->dev;
72         bool radio_enabled;
73
74         /* Checking the radio-enabled status here is slightly racy,
75          * but we want to avoid the locking overhead and we don't care
76          * whether the LED has the wrong state for a second. */
77         radio_enabled = (dev->phy.radio_on && dev->radio_hw_enable);
78
79         if (brightness == LED_OFF || !radio_enabled)
80                 b43_led_turn_off(dev, led->index, led->activelow);
81         else
82                 b43_led_turn_on(dev, led->index, led->activelow);
83 }
84
85 static int b43_register_led(struct b43_wldev *dev, struct b43_led *led,
86                             const char *name, char *default_trigger,
87                             u8 led_index, bool activelow)
88 {
89         int err;
90
91         b43_led_turn_off(dev, led_index, activelow);
92         if (led->dev)
93                 return -EEXIST;
94         if (!default_trigger)
95                 return -EINVAL;
96         led->dev = dev;
97         led->index = led_index;
98         led->activelow = activelow;
99         strncpy(led->name, name, sizeof(led->name));
100
101         led->led_dev.name = led->name;
102         led->led_dev.default_trigger = default_trigger;
103         led->led_dev.brightness_set = b43_led_brightness_set;
104
105         err = led_classdev_register(dev->dev->dev, &led->led_dev);
106         if (err) {
107                 b43warn(dev->wl, "LEDs: Failed to register %s\n", name);
108                 led->dev = NULL;
109                 return err;
110         }
111         return 0;
112 }
113
114 static void b43_unregister_led(struct b43_led *led)
115 {
116         if (!led->dev)
117                 return;
118         led_classdev_unregister(&led->led_dev);
119         b43_led_turn_off(led->dev, led->index, led->activelow);
120         led->dev = NULL;
121 }
122
123 static void b43_map_led(struct b43_wldev *dev,
124                         u8 led_index,
125                         enum b43_led_behaviour behaviour,
126                         bool activelow)
127 {
128         struct ieee80211_hw *hw = dev->wl->hw;
129         char name[B43_LED_MAX_NAME_LEN + 1];
130
131         /* Map the b43 specific LED behaviour value to the
132          * generic LED triggers. */
133         switch (behaviour) {
134         case B43_LED_INACTIVE:
135                 break;
136         case B43_LED_OFF:
137                 b43_led_turn_off(dev, led_index, activelow);
138                 break;
139         case B43_LED_ON:
140                 b43_led_turn_on(dev, led_index, activelow);
141                 break;
142         case B43_LED_ACTIVITY:
143         case B43_LED_TRANSFER:
144         case B43_LED_APTRANSFER:
145                 snprintf(name, sizeof(name),
146                          "b43-%s:tx", wiphy_name(hw->wiphy));
147                 b43_register_led(dev, &dev->led_tx, name,
148                                  ieee80211_get_tx_led_name(hw),
149                                  led_index, activelow);
150                 snprintf(name, sizeof(name),
151                          "b43-%s:rx", wiphy_name(hw->wiphy));
152                 b43_register_led(dev, &dev->led_rx, name,
153                                  ieee80211_get_rx_led_name(hw),
154                                  led_index, activelow);
155                 break;
156         case B43_LED_RADIO_ALL:
157         case B43_LED_RADIO_A:
158         case B43_LED_RADIO_B:
159         case B43_LED_MODE_BG:
160                 snprintf(name, sizeof(name),
161                          "b43-%s:radio", wiphy_name(hw->wiphy));
162                 b43_register_led(dev, &dev->led_radio, name,
163                                  b43_rfkill_led_name(dev),
164                                  led_index, activelow);
165                 break;
166         case B43_LED_WEIRD:
167         case B43_LED_ASSOC:
168                 snprintf(name, sizeof(name),
169                          "b43-%s:assoc", wiphy_name(hw->wiphy));
170                 b43_register_led(dev, &dev->led_assoc, name,
171                                  ieee80211_get_assoc_led_name(hw),
172                                  led_index, activelow);
173                 break;
174         default:
175                 b43warn(dev->wl, "LEDs: Unknown behaviour 0x%02X\n",
176                         behaviour);
177                 break;
178         }
179 }
180
181 void b43_leds_init(struct b43_wldev *dev)
182 {
183         struct ssb_bus *bus = dev->dev->bus;
184         u8 sprom[4];
185         int i;
186         enum b43_led_behaviour behaviour;
187         bool activelow;
188
189         sprom[0] = bus->sprom.r1.gpio0;
190         sprom[1] = bus->sprom.r1.gpio1;
191         sprom[2] = bus->sprom.r1.gpio2;
192         sprom[3] = bus->sprom.r1.gpio3;
193
194         for (i = 0; i < 4; i++) {
195                 if (sprom[i] == 0xFF) {
196                         /* There is no LED information in the SPROM
197                          * for this LED. Hardcode it here. */
198                         activelow = 0;
199                         switch (i) {
200                         case 0:
201                                 behaviour = B43_LED_ACTIVITY;
202                                 activelow = 1;
203                                 if (bus->boardinfo.vendor == PCI_VENDOR_ID_COMPAQ)
204                                         behaviour = B43_LED_RADIO_ALL;
205                                 break;
206                         case 1:
207                                 behaviour = B43_LED_RADIO_B;
208                                 if (bus->boardinfo.vendor == PCI_VENDOR_ID_ASUSTEK)
209                                         behaviour = B43_LED_ASSOC;
210                                 break;
211                         case 2:
212                                 behaviour = B43_LED_RADIO_A;
213                                 break;
214                         case 3:
215                                 behaviour = B43_LED_OFF;
216                                 break;
217                         default:
218                                 B43_WARN_ON(1);
219                                 return;
220                         }
221                 } else {
222                         behaviour = sprom[i] & B43_LED_BEHAVIOUR;
223                         activelow = !!(sprom[i] & B43_LED_ACTIVELOW);
224                 }
225                 b43_map_led(dev, i, behaviour, activelow);
226         }
227 }
228
229 void b43_leds_exit(struct b43_wldev *dev)
230 {
231         b43_unregister_led(&dev->led_tx);
232         b43_unregister_led(&dev->led_rx);
233         b43_unregister_led(&dev->led_assoc);
234 }