cfg80211: fix SME connect
[linux-block.git] / drivers / net / wireless / b43 / leds.c
CommitLineData
e4d6b795
MB
1/*
2
3 Broadcom B43 wireless driver
21954c36 4 LED control
e4d6b795
MB
5
6 Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
1f21ad2a 7 Copyright (c) 2005 Stefano Brivio <stefano.brivio@polimi.it>
21954c36
MB
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>
e4d6b795
MB
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"
f41f3f37 31#include "rfkill.h"
e4d6b795 32
e4d6b795 33
21954c36
MB
34static void b43_led_turn_on(struct b43_wldev *dev, u8 led_index,
35 bool activelow)
e4d6b795 36{
21954c36 37 struct b43_wl *wl = dev->wl;
e4d6b795 38 unsigned long flags;
21954c36 39 u16 ctl;
e4d6b795 40
21954c36
MB
41 spin_lock_irqsave(&wl->leds_lock, flags);
42 ctl = b43_read16(dev, B43_MMIO_GPIO_CONTROL);
43 if (activelow)
44 ctl &= ~(1 << led_index);
45 else
46 ctl |= (1 << led_index);
47 b43_write16(dev, B43_MMIO_GPIO_CONTROL, ctl);
48 spin_unlock_irqrestore(&wl->leds_lock, flags);
e4d6b795
MB
49}
50
21954c36
MB
51static void b43_led_turn_off(struct b43_wldev *dev, u8 led_index,
52 bool activelow)
e4d6b795 53{
21954c36
MB
54 struct b43_wl *wl = dev->wl;
55 unsigned long flags;
56 u16 ctl;
57
58 spin_lock_irqsave(&wl->leds_lock, flags);
59 ctl = b43_read16(dev, B43_MMIO_GPIO_CONTROL);
60 if (activelow)
61 ctl |= (1 << led_index);
62 else
63 ctl &= ~(1 << led_index);
64 b43_write16(dev, B43_MMIO_GPIO_CONTROL, ctl);
65 spin_unlock_irqrestore(&wl->leds_lock, flags);
e4d6b795
MB
66}
67
21954c36
MB
68/* Callback from the LED subsystem. */
69static void b43_led_brightness_set(struct led_classdev *led_dev,
70 enum led_brightness brightness)
e4d6b795 71{
21954c36 72 struct b43_led *led = container_of(led_dev, struct b43_led, led_dev);
e4d6b795 73 struct b43_wldev *dev = led->dev;
21954c36 74 bool radio_enabled;
e4d6b795 75
7b3abfc8
MB
76 if (unlikely(b43_status(dev) < B43_STAT_INITIALIZED))
77 return;
78
21954c36
MB
79 /* Checking the radio-enabled status here is slightly racy,
80 * but we want to avoid the locking overhead and we don't care
81 * whether the LED has the wrong state for a second. */
82 radio_enabled = (dev->phy.radio_on && dev->radio_hw_enable);
e4d6b795 83
21954c36
MB
84 if (brightness == LED_OFF || !radio_enabled)
85 b43_led_turn_off(dev, led->index, led->activelow);
e4d6b795 86 else
21954c36 87 b43_led_turn_on(dev, led->index, led->activelow);
e4d6b795
MB
88}
89
21954c36 90static int b43_register_led(struct b43_wldev *dev, struct b43_led *led,
19d337df 91 const char *name, const char *default_trigger,
21954c36 92 u8 led_index, bool activelow)
e4d6b795 93{
21954c36
MB
94 int err;
95
96 b43_led_turn_off(dev, led_index, activelow);
97 if (led->dev)
98 return -EEXIST;
99 if (!default_trigger)
100 return -EINVAL;
101 led->dev = dev;
102 led->index = led_index;
103 led->activelow = activelow;
104 strncpy(led->name, name, sizeof(led->name));
105
106 led->led_dev.name = led->name;
107 led->led_dev.default_trigger = default_trigger;
108 led->led_dev.brightness_set = b43_led_brightness_set;
109
110 err = led_classdev_register(dev->dev->dev, &led->led_dev);
111 if (err) {
112 b43warn(dev->wl, "LEDs: Failed to register %s\n", name);
113 led->dev = NULL;
114 return err;
115 }
116 return 0;
117}
118
119static void b43_unregister_led(struct b43_led *led)
120{
121 if (!led->dev)
122 return;
b844eba2 123 led_classdev_unregister(&led->led_dev);
21954c36
MB
124 b43_led_turn_off(led->dev, led->index, led->activelow);
125 led->dev = NULL;
126}
e4d6b795 127
21954c36
MB
128static void b43_map_led(struct b43_wldev *dev,
129 u8 led_index,
130 enum b43_led_behaviour behaviour,
131 bool activelow)
132{
133 struct ieee80211_hw *hw = dev->wl->hw;
134 char name[B43_LED_MAX_NAME_LEN + 1];
e4d6b795 135
21954c36
MB
136 /* Map the b43 specific LED behaviour value to the
137 * generic LED triggers. */
138 switch (behaviour) {
139 case B43_LED_INACTIVE:
140 break;
141 case B43_LED_OFF:
142 b43_led_turn_off(dev, led_index, activelow);
e4d6b795 143 break;
21954c36
MB
144 case B43_LED_ON:
145 b43_led_turn_on(dev, led_index, activelow);
e4d6b795 146 break;
21954c36
MB
147 case B43_LED_ACTIVITY:
148 case B43_LED_TRANSFER:
149 case B43_LED_APTRANSFER:
150 snprintf(name, sizeof(name),
6c152bee 151 "b43-%s::tx", wiphy_name(hw->wiphy));
21954c36
MB
152 b43_register_led(dev, &dev->led_tx, name,
153 ieee80211_get_tx_led_name(hw),
154 led_index, activelow);
155 snprintf(name, sizeof(name),
6c152bee 156 "b43-%s::rx", wiphy_name(hw->wiphy));
21954c36
MB
157 b43_register_led(dev, &dev->led_rx, name,
158 ieee80211_get_rx_led_name(hw),
159 led_index, activelow);
e4d6b795 160 break;
21954c36
MB
161 case B43_LED_RADIO_ALL:
162 case B43_LED_RADIO_A:
163 case B43_LED_RADIO_B:
164 case B43_LED_MODE_BG:
8e9f7529 165 snprintf(name, sizeof(name),
6c152bee 166 "b43-%s::radio", wiphy_name(hw->wiphy));
8e9f7529 167 b43_register_led(dev, &dev->led_radio, name,
f41f3f37 168 ieee80211_get_radio_led_name(hw),
8e9f7529 169 led_index, activelow);
f41f3f37
JB
170 /* Sync the RF-kill LED state with radio and switch states. */
171 if (dev->phy.radio_on && b43_is_hw_radio_enabled(dev))
1a8d1227 172 b43_led_turn_on(dev, led_index, activelow);
8e9f7529 173 break;
21954c36
MB
174 case B43_LED_WEIRD:
175 case B43_LED_ASSOC:
176 snprintf(name, sizeof(name),
6c152bee 177 "b43-%s::assoc", wiphy_name(hw->wiphy));
21954c36
MB
178 b43_register_led(dev, &dev->led_assoc, name,
179 ieee80211_get_assoc_led_name(hw),
180 led_index, activelow);
e4d6b795
MB
181 break;
182 default:
21954c36
MB
183 b43warn(dev->wl, "LEDs: Unknown behaviour 0x%02X\n",
184 behaviour);
185 break;
e4d6b795
MB
186 }
187}
188
21954c36 189void b43_leds_init(struct b43_wldev *dev)
e4d6b795 190{
21954c36 191 struct ssb_bus *bus = dev->dev->bus;
e4d6b795
MB
192 u8 sprom[4];
193 int i;
21954c36
MB
194 enum b43_led_behaviour behaviour;
195 bool activelow;
e4d6b795 196
95de2841
LF
197 sprom[0] = bus->sprom.gpio0;
198 sprom[1] = bus->sprom.gpio1;
199 sprom[2] = bus->sprom.gpio2;
200 sprom[3] = bus->sprom.gpio3;
e4d6b795 201
21954c36 202 for (i = 0; i < 4; i++) {
e4d6b795 203 if (sprom[i] == 0xFF) {
21954c36
MB
204 /* There is no LED information in the SPROM
205 * for this LED. Hardcode it here. */
206 activelow = 0;
207 switch (i) {
208 case 0:
209 behaviour = B43_LED_ACTIVITY;
210 activelow = 1;
211 if (bus->boardinfo.vendor == PCI_VENDOR_ID_COMPAQ)
212 behaviour = B43_LED_RADIO_ALL;
213 break;
214 case 1:
215 behaviour = B43_LED_RADIO_B;
216 if (bus->boardinfo.vendor == PCI_VENDOR_ID_ASUSTEK)
217 behaviour = B43_LED_ASSOC;
218 break;
219 case 2:
220 behaviour = B43_LED_RADIO_A;
221 break;
222 case 3:
223 behaviour = B43_LED_OFF;
224 break;
225 default:
226 B43_WARN_ON(1);
227 return;
228 }
e4d6b795 229 } else {
21954c36
MB
230 behaviour = sprom[i] & B43_LED_BEHAVIOUR;
231 activelow = !!(sprom[i] & B43_LED_ACTIVELOW);
e4d6b795 232 }
21954c36 233 b43_map_led(dev, i, behaviour, activelow);
e4d6b795 234 }
e4d6b795
MB
235}
236
237void b43_leds_exit(struct b43_wldev *dev)
238{
21954c36
MB
239 b43_unregister_led(&dev->led_tx);
240 b43_unregister_led(&dev->led_rx);
241 b43_unregister_led(&dev->led_assoc);
1a8d1227 242 b43_unregister_led(&dev->led_radio);
e4d6b795 243}