New: mac80211-based bcm43xx driver from the wireless-dev tree
[openwrt.git] / package / bcm43xx-mac80211 / src / bcm43xx / bcm43xx_leds.c
1 /*
2
3 Broadcom BCM43xx wireless driver
4
5 Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
6 Stefano Brivio <st3@riseup.net>
7 Michael Buesch <mb@bu3sch.de>
8 Danny van Dyk <kugelfang@gentoo.org>
9 Andreas Jaggi <andreas.jaggi@waterwave.ch>
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; see the file COPYING. If not, write to
23 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
24 Boston, MA 02110-1301, USA.
25
26 */
27
28 #include "bcm43xx_leds.h"
29 #include "bcm43xx.h"
30 #include "bcm43xx_main.h"
31
32 static void bcm43xx_led_changestate(struct bcm43xx_led *led)
33 {
34 struct bcm43xx_wldev *dev = led->dev;
35 const int index = bcm43xx_led_index(led);
36 const u16 mask = (1 << index);
37 u16 ledctl;
38
39 assert(index >= 0 && index < BCM43xx_NR_LEDS);
40 assert(led->blink_interval);
41 ledctl = bcm43xx_read16(dev, BCM43xx_MMIO_GPIO_CONTROL);
42 ledctl = (ledctl & mask) ? (ledctl & ~mask) : (ledctl | mask);
43 bcm43xx_write16(dev, BCM43xx_MMIO_GPIO_CONTROL, ledctl);
44 }
45
46 static void bcm43xx_led_blink(unsigned long d)
47 {
48 struct bcm43xx_led *led = (struct bcm43xx_led *)d;
49 struct bcm43xx_wldev *dev = led->dev;
50 unsigned long flags;
51
52 spin_lock_irqsave(&dev->wl->leds_lock, flags);
53 if (led->blink_interval) {
54 bcm43xx_led_changestate(led);
55 mod_timer(&led->blink_timer, jiffies + led->blink_interval);
56 }
57 spin_unlock_irqrestore(&dev->wl->leds_lock, flags);
58 }
59
60 static void bcm43xx_led_blink_start(struct bcm43xx_led *led,
61 unsigned long interval)
62 {
63 if (led->blink_interval)
64 return;
65 led->blink_interval = interval;
66 bcm43xx_led_changestate(led);
67 led->blink_timer.expires = jiffies + interval;
68 add_timer(&led->blink_timer);
69 }
70
71 static void bcm43xx_led_blink_stop(struct bcm43xx_led *led, int sync)
72 {
73 struct bcm43xx_wldev *dev = led->dev;
74 const int index = bcm43xx_led_index(led);
75 u16 ledctl;
76
77 if (!led->blink_interval)
78 return;
79 if (unlikely(sync))
80 del_timer_sync(&led->blink_timer);
81 else
82 del_timer(&led->blink_timer);
83 led->blink_interval = 0;
84
85 /* Make sure the LED is turned off. */
86 assert(index >= 0 && index < BCM43xx_NR_LEDS);
87 ledctl = bcm43xx_read16(dev, BCM43xx_MMIO_GPIO_CONTROL);
88 if (led->activelow)
89 ledctl |= (1 << index);
90 else
91 ledctl &= ~(1 << index);
92 bcm43xx_write16(dev, BCM43xx_MMIO_GPIO_CONTROL, ledctl);
93 }
94
95 static void bcm43xx_led_init_hardcoded(struct bcm43xx_wldev *dev,
96 struct bcm43xx_led *led,
97 int led_index)
98 {
99 struct ssb_bus *bus = dev->dev->bus;
100
101 /* This function is called, if the behaviour (and activelow)
102 * information for a LED is missing in the SPROM.
103 * We hardcode the behaviour values for various devices here.
104 * Note that the BCM43xx_LED_TEST_XXX behaviour values can
105 * be used to figure out which led is mapped to which index.
106 */
107
108 switch (led_index) {
109 case 0:
110 led->behaviour = BCM43xx_LED_ACTIVITY;
111 led->activelow = 1;
112 if (bus->board_vendor == PCI_VENDOR_ID_COMPAQ)
113 led->behaviour = BCM43xx_LED_RADIO_ALL;
114 break;
115 case 1:
116 led->behaviour = BCM43xx_LED_RADIO_B;
117 if (bus->board_vendor == PCI_VENDOR_ID_ASUSTEK)
118 led->behaviour = BCM43xx_LED_ASSOC;
119 break;
120 case 2:
121 led->behaviour = BCM43xx_LED_RADIO_A;
122 break;
123 case 3:
124 led->behaviour = BCM43xx_LED_OFF;
125 break;
126 default:
127 assert(0);
128 }
129 }
130
131 int bcm43xx_leds_init(struct bcm43xx_wldev *dev)
132 {
133 struct bcm43xx_led *led;
134 u8 sprom[4];
135 int i;
136
137 sprom[0] = dev->dev->bus->sprom.r1.gpio0;
138 sprom[1] = dev->dev->bus->sprom.r1.gpio1;
139 sprom[2] = dev->dev->bus->sprom.r1.gpio2;
140 sprom[3] = dev->dev->bus->sprom.r1.gpio3;
141
142 for (i = 0; i < BCM43xx_NR_LEDS; i++) {
143 led = &(dev->leds[i]);
144 led->dev = dev;
145 setup_timer(&led->blink_timer,
146 bcm43xx_led_blink,
147 (unsigned long)led);
148
149 if (sprom[i] == 0xFF) {
150 bcm43xx_led_init_hardcoded(dev, led, i);
151 } else {
152 led->behaviour = sprom[i] & BCM43xx_LED_BEHAVIOUR;
153 led->activelow = !!(sprom[i] & BCM43xx_LED_ACTIVELOW);
154 }
155 }
156
157 return 0;
158 }
159
160 void bcm43xx_leds_exit(struct bcm43xx_wldev *dev)
161 {
162 struct bcm43xx_led *led;
163 int i;
164
165 for (i = 0; i < BCM43xx_NR_LEDS; i++) {
166 led = &(dev->leds[i]);
167 bcm43xx_led_blink_stop(led, 1);
168 }
169 bcm43xx_leds_switch_all(dev, 0);
170 }
171
172 void bcm43xx_leds_update(struct bcm43xx_wldev *dev, int activity)
173 {
174 struct bcm43xx_led *led;
175 struct bcm43xx_phy *phy = &dev->phy;
176 const int transferring = (jiffies - dev->stats.last_tx) < BCM43xx_LED_XFER_THRES;
177 int i, turn_on;
178 unsigned long interval = 0;
179 u16 ledctl;
180 unsigned long flags;
181
182 spin_lock_irqsave(&dev->wl->leds_lock, flags);
183 ledctl = bcm43xx_read16(dev, BCM43xx_MMIO_GPIO_CONTROL);
184 for (i = 0; i < BCM43xx_NR_LEDS; i++) {
185 led = &(dev->leds[i]);
186
187 turn_on = 0;
188 switch (led->behaviour) {
189 case BCM43xx_LED_INACTIVE:
190 continue;
191 case BCM43xx_LED_OFF:
192 break;
193 case BCM43xx_LED_ON:
194 turn_on = 1;
195 break;
196 case BCM43xx_LED_ACTIVITY:
197 turn_on = activity;
198 break;
199 case BCM43xx_LED_RADIO_ALL:
200 turn_on = phy->radio_on && bcm43xx_is_hw_radio_enabled(dev);
201 break;
202 case BCM43xx_LED_RADIO_A:
203 turn_on = (phy->radio_on && bcm43xx_is_hw_radio_enabled(dev)
204 && phy->type == BCM43xx_PHYTYPE_A);
205 break;
206 case BCM43xx_LED_RADIO_B:
207 turn_on = (phy->radio_on && bcm43xx_is_hw_radio_enabled(dev) &&
208 (phy->type == BCM43xx_PHYTYPE_B ||
209 phy->type == BCM43xx_PHYTYPE_G));
210 break;
211 case BCM43xx_LED_MODE_BG:
212 if (phy->type == BCM43xx_PHYTYPE_G && bcm43xx_is_hw_radio_enabled(dev) &&
213 1/*FIXME: using G rates.*/)
214 turn_on = 1;
215 break;
216 case BCM43xx_LED_TRANSFER:
217 if (transferring)
218 bcm43xx_led_blink_start(led, BCM43xx_LEDBLINK_MEDIUM);
219 else
220 bcm43xx_led_blink_stop(led, 0);
221 continue;
222 case BCM43xx_LED_APTRANSFER:
223 if (bcm43xx_is_mode(dev->wl, IEEE80211_IF_TYPE_AP)) {
224 if (transferring) {
225 interval = BCM43xx_LEDBLINK_FAST;
226 turn_on = 1;
227 }
228 } else {
229 turn_on = 1;
230 if (0/*TODO: not assoc*/)
231 interval = BCM43xx_LEDBLINK_SLOW;
232 else if (transferring)
233 interval = BCM43xx_LEDBLINK_FAST;
234 else
235 turn_on = 0;
236 }
237 if (turn_on)
238 bcm43xx_led_blink_start(led, interval);
239 else
240 bcm43xx_led_blink_stop(led, 0);
241 continue;
242 case BCM43xx_LED_WEIRD:
243 //TODO
244 break;
245 case BCM43xx_LED_ASSOC:
246 if (1/*dev->softmac->associated*/)
247 turn_on = 1;
248 break;
249 #ifdef CONFIG_BCM43XX_DEBUG
250 case BCM43xx_LED_TEST_BLINKSLOW:
251 bcm43xx_led_blink_start(led, BCM43xx_LEDBLINK_SLOW);
252 continue;
253 case BCM43xx_LED_TEST_BLINKMEDIUM:
254 bcm43xx_led_blink_start(led, BCM43xx_LEDBLINK_MEDIUM);
255 continue;
256 case BCM43xx_LED_TEST_BLINKFAST:
257 bcm43xx_led_blink_start(led, BCM43xx_LEDBLINK_FAST);
258 continue;
259 #endif /* CONFIG_BCM43XX_DEBUG */
260 default:
261 assert(0);
262 };
263
264 if (led->activelow)
265 turn_on = !turn_on;
266 if (turn_on)
267 ledctl |= (1 << i);
268 else
269 ledctl &= ~(1 << i);
270 }
271 bcm43xx_write16(dev, BCM43xx_MMIO_GPIO_CONTROL, ledctl);
272 spin_unlock_irqrestore(&dev->wl->leds_lock, flags);
273 }
274
275 void bcm43xx_leds_switch_all(struct bcm43xx_wldev *dev, int on)
276 {
277 struct bcm43xx_led *led;
278 u16 ledctl;
279 int i;
280 int bit_on;
281 unsigned long flags;
282
283 spin_lock_irqsave(&dev->wl->leds_lock, flags);
284 ledctl = bcm43xx_read16(dev, BCM43xx_MMIO_GPIO_CONTROL);
285 for (i = 0; i < BCM43xx_NR_LEDS; i++) {
286 led = &(dev->leds[i]);
287 if (led->behaviour == BCM43xx_LED_INACTIVE)
288 continue;
289 if (on)
290 bit_on = led->activelow ? 0 : 1;
291 else
292 bit_on = led->activelow ? 1 : 0;
293 if (bit_on)
294 ledctl |= (1 << i);
295 else
296 ledctl &= ~(1 << i);
297 }
298 bcm43xx_write16(dev, BCM43xx_MMIO_GPIO_CONTROL, ledctl);
299 spin_unlock_irqrestore(&dev->wl->leds_lock, flags);
300 }
This page took 0.054694 seconds and 5 git commands to generate.