blob: c3f90c8563d915ee4175afe6ca8cd6a4a41627fb [file] [log] [blame]
John W. Linvillef2223132006-01-23 16:59:58 -05001/*
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 <mbuesch@freenet.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
31#include <asm/bitops.h>
32
33
34static void bcm43xx_led_changestate(struct bcm43xx_led *led)
35{
36 struct bcm43xx_private *bcm = led->bcm;
37 const int index = bcm43xx_led_index(led);
Michael Bueschdcfd7202006-02-12 20:25:55 +010038 const u16 mask = (1 << index);
John W. Linvillef2223132006-01-23 16:59:58 -050039 u16 ledctl;
40
41 assert(index >= 0 && index < BCM43xx_NR_LEDS);
42 assert(led->blink_interval);
43 ledctl = bcm43xx_read16(bcm, BCM43xx_MMIO_GPIO_CONTROL);
Michael Bueschdcfd7202006-02-12 20:25:55 +010044 ledctl = (ledctl & mask) ? (ledctl & ~mask) : (ledctl | mask);
John W. Linvillef2223132006-01-23 16:59:58 -050045 bcm43xx_write16(bcm, BCM43xx_MMIO_GPIO_CONTROL, ledctl);
46}
47
48static void bcm43xx_led_blink(unsigned long d)
49{
50 struct bcm43xx_led *led = (struct bcm43xx_led *)d;
51 struct bcm43xx_private *bcm = led->bcm;
52 unsigned long flags;
53
Michael Bueschefa6a372006-06-27 21:38:40 +020054 spin_lock_irqsave(&bcm->leds_lock, flags);
John W. Linvillef2223132006-01-23 16:59:58 -050055 if (led->blink_interval) {
56 bcm43xx_led_changestate(led);
57 mod_timer(&led->blink_timer, jiffies + led->blink_interval);
58 }
Michael Bueschefa6a372006-06-27 21:38:40 +020059 spin_unlock_irqrestore(&bcm->leds_lock, flags);
John W. Linvillef2223132006-01-23 16:59:58 -050060}
61
62static void bcm43xx_led_blink_start(struct bcm43xx_led *led,
63 unsigned long interval)
64{
Michael Bueschdcfd7202006-02-12 20:25:55 +010065 if (led->blink_interval)
66 return;
John W. Linvillef2223132006-01-23 16:59:58 -050067 led->blink_interval = interval;
68 bcm43xx_led_changestate(led);
69 led->blink_timer.expires = jiffies + interval;
70 add_timer(&led->blink_timer);
71}
72
73static void bcm43xx_led_blink_stop(struct bcm43xx_led *led, int sync)
74{
75 struct bcm43xx_private *bcm = led->bcm;
76 const int index = bcm43xx_led_index(led);
77 u16 ledctl;
78
79 if (!led->blink_interval)
80 return;
81 if (unlikely(sync))
82 del_timer_sync(&led->blink_timer);
83 else
84 del_timer(&led->blink_timer);
85 led->blink_interval = 0;
86
87 /* Make sure the LED is turned off. */
88 assert(index >= 0 && index < BCM43xx_NR_LEDS);
89 ledctl = bcm43xx_read16(bcm, BCM43xx_MMIO_GPIO_CONTROL);
90 if (led->activelow)
91 ledctl |= (1 << index);
92 else
93 ledctl &= ~(1 << index);
94 bcm43xx_write16(bcm, BCM43xx_MMIO_GPIO_CONTROL, ledctl);
95}
96
Michael Bueschdcfd7202006-02-12 20:25:55 +010097static void bcm43xx_led_init_hardcoded(struct bcm43xx_private *bcm,
98 struct bcm43xx_led *led,
99 int led_index)
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 if (bcm->board_vendor == PCI_VENDOR_ID_COMPAQ)
112 led->behaviour = BCM43xx_LED_RADIO_ALL;
113 break;
114 case 1:
115 led->behaviour = BCM43xx_LED_RADIO_B;
116 if (bcm->board_vendor == PCI_VENDOR_ID_ASUSTEK)
117 led->behaviour = BCM43xx_LED_ASSOC;
118 break;
119 case 2:
120 led->behaviour = BCM43xx_LED_RADIO_A;
121 break;
122 case 3:
123 led->behaviour = BCM43xx_LED_OFF;
124 break;
125 default:
126 assert(0);
127 }
128}
129
John W. Linvillef2223132006-01-23 16:59:58 -0500130int bcm43xx_leds_init(struct bcm43xx_private *bcm)
131{
132 struct bcm43xx_led *led;
133 u8 sprom[4];
134 int i;
135
136 sprom[0] = bcm->sprom.wl0gpio0;
137 sprom[1] = bcm->sprom.wl0gpio1;
138 sprom[2] = bcm->sprom.wl0gpio2;
139 sprom[3] = bcm->sprom.wl0gpio3;
140
141 for (i = 0; i < BCM43xx_NR_LEDS; i++) {
142 led = &(bcm->leds[i]);
143 led->bcm = bcm;
Michael Bueschdcfd7202006-02-12 20:25:55 +0100144 setup_timer(&led->blink_timer,
145 bcm43xx_led_blink,
146 (unsigned long)led);
John W. Linvillef2223132006-01-23 16:59:58 -0500147
148 if (sprom[i] == 0xFF) {
Michael Bueschdcfd7202006-02-12 20:25:55 +0100149 bcm43xx_led_init_hardcoded(bcm, led, i);
John W. Linvillef2223132006-01-23 16:59:58 -0500150 } else {
151 led->behaviour = sprom[i] & BCM43xx_LED_BEHAVIOUR;
152 led->activelow = !!(sprom[i] & BCM43xx_LED_ACTIVELOW);
153 }
154 }
155
156 return 0;
157}
158
159void bcm43xx_leds_exit(struct bcm43xx_private *bcm)
160{
161 struct bcm43xx_led *led;
162 int i;
163
164 for (i = 0; i < BCM43xx_NR_LEDS; i++) {
165 led = &(bcm->leds[i]);
166 bcm43xx_led_blink_stop(led, 1);
167 }
Michael Buesch714eece2006-03-18 21:28:46 +0100168 bcm43xx_leds_switch_all(bcm, 0);
John W. Linvillef2223132006-01-23 16:59:58 -0500169}
170
171void bcm43xx_leds_update(struct bcm43xx_private *bcm, int activity)
172{
173 struct bcm43xx_led *led;
Michael Buesche9357c02006-03-13 19:27:34 +0100174 struct bcm43xx_radioinfo *radio = bcm43xx_current_radio(bcm);
175 struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -0500176 const int transferring = (jiffies - bcm->stats.last_tx) < BCM43xx_LED_XFER_THRES;
Michael Bueschdcfd7202006-02-12 20:25:55 +0100177 int i, turn_on;
John W. Linvillef2223132006-01-23 16:59:58 -0500178 unsigned long interval = 0;
179 u16 ledctl;
Michael Bueschefa6a372006-06-27 21:38:40 +0200180 unsigned long flags;
John W. Linvillef2223132006-01-23 16:59:58 -0500181
Michael Bueschefa6a372006-06-27 21:38:40 +0200182 spin_lock_irqsave(&bcm->leds_lock, flags);
John W. Linvillef2223132006-01-23 16:59:58 -0500183 ledctl = bcm43xx_read16(bcm, BCM43xx_MMIO_GPIO_CONTROL);
184 for (i = 0; i < BCM43xx_NR_LEDS; i++) {
185 led = &(bcm->leds[i]);
John W. Linvillef2223132006-01-23 16:59:58 -0500186
Michael Bueschdcfd7202006-02-12 20:25:55 +0100187 turn_on = 0;
John W. Linvillef2223132006-01-23 16:59:58 -0500188 switch (led->behaviour) {
Michael Bueschdcfd7202006-02-12 20:25:55 +0100189 case BCM43xx_LED_INACTIVE:
190 continue;
John W. Linvillef2223132006-01-23 16:59:58 -0500191 case BCM43xx_LED_OFF:
John W. Linvillef2223132006-01-23 16:59:58 -0500192 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 = radio->enabled;
201 break;
202 case BCM43xx_LED_RADIO_A:
203 turn_on = (radio->enabled && phy->type == BCM43xx_PHYTYPE_A);
204 break;
205 case BCM43xx_LED_RADIO_B:
206 turn_on = (radio->enabled &&
207 (phy->type == BCM43xx_PHYTYPE_B ||
208 phy->type == BCM43xx_PHYTYPE_G));
209 break;
210 case BCM43xx_LED_MODE_BG:
John W. Linvillef2223132006-01-23 16:59:58 -0500211 if (phy->type == BCM43xx_PHYTYPE_G &&
212 1/*FIXME: using G rates.*/)
213 turn_on = 1;
214 break;
215 case BCM43xx_LED_TRANSFER:
216 if (transferring)
217 bcm43xx_led_blink_start(led, BCM43xx_LEDBLINK_MEDIUM);
218 else
219 bcm43xx_led_blink_stop(led, 0);
220 continue;
221 case BCM43xx_LED_APTRANSFER:
222 if (bcm->ieee->iw_mode == IW_MODE_MASTER) {
223 if (transferring) {
224 interval = BCM43xx_LEDBLINK_FAST;
225 turn_on = 1;
226 }
227 } else {
228 turn_on = 1;
229 if (0/*TODO: not assoc*/)
230 interval = BCM43xx_LEDBLINK_SLOW;
231 else if (transferring)
232 interval = BCM43xx_LEDBLINK_FAST;
233 else
234 turn_on = 0;
235 }
236 if (turn_on)
237 bcm43xx_led_blink_start(led, interval);
238 else
239 bcm43xx_led_blink_stop(led, 0);
240 continue;
241 case BCM43xx_LED_WEIRD:
242 //TODO
John W. Linvillef2223132006-01-23 16:59:58 -0500243 break;
244 case BCM43xx_LED_ASSOC:
Michael Bueschdcfd7202006-02-12 20:25:55 +0100245 if (bcm->softmac->associated)
John W. Linvillef2223132006-01-23 16:59:58 -0500246 turn_on = 1;
247 break;
Michael Bueschdcfd7202006-02-12 20:25:55 +0100248#ifdef CONFIG_BCM43XX_DEBUG
249 case BCM43xx_LED_TEST_BLINKSLOW:
250 bcm43xx_led_blink_start(led, BCM43xx_LEDBLINK_SLOW);
251 continue;
252 case BCM43xx_LED_TEST_BLINKMEDIUM:
253 bcm43xx_led_blink_start(led, BCM43xx_LEDBLINK_MEDIUM);
254 continue;
255 case BCM43xx_LED_TEST_BLINKFAST:
256 bcm43xx_led_blink_start(led, BCM43xx_LEDBLINK_FAST);
257 continue;
258#endif /* CONFIG_BCM43XX_DEBUG */
John W. Linvillef2223132006-01-23 16:59:58 -0500259 default:
260 assert(0);
261 };
262
263 if (led->activelow)
264 turn_on = !turn_on;
265 if (turn_on)
266 ledctl |= (1 << i);
267 else
268 ledctl &= ~(1 << i);
269 }
270 bcm43xx_write16(bcm, BCM43xx_MMIO_GPIO_CONTROL, ledctl);
Michael Bueschefa6a372006-06-27 21:38:40 +0200271 spin_unlock_irqrestore(&bcm->leds_lock, flags);
John W. Linvillef2223132006-01-23 16:59:58 -0500272}
273
Michael Buesch714eece2006-03-18 21:28:46 +0100274void bcm43xx_leds_switch_all(struct bcm43xx_private *bcm, int on)
John W. Linvillef2223132006-01-23 16:59:58 -0500275{
276 struct bcm43xx_led *led;
Michael Buesch714eece2006-03-18 21:28:46 +0100277 u16 ledctl;
John W. Linvillef2223132006-01-23 16:59:58 -0500278 int i;
Michael Buesch714eece2006-03-18 21:28:46 +0100279 int bit_on;
Michael Bueschefa6a372006-06-27 21:38:40 +0200280 unsigned long flags;
John W. Linvillef2223132006-01-23 16:59:58 -0500281
Michael Bueschefa6a372006-06-27 21:38:40 +0200282 spin_lock_irqsave(&bcm->leds_lock, flags);
Michael Buesch714eece2006-03-18 21:28:46 +0100283 ledctl = bcm43xx_read16(bcm, BCM43xx_MMIO_GPIO_CONTROL);
John W. Linvillef2223132006-01-23 16:59:58 -0500284 for (i = 0; i < BCM43xx_NR_LEDS; i++) {
285 led = &(bcm->leds[i]);
286 if (led->behaviour == BCM43xx_LED_INACTIVE)
287 continue;
Michael Buesch714eece2006-03-18 21:28:46 +0100288 if (on)
289 bit_on = led->activelow ? 0 : 1;
290 else
291 bit_on = led->activelow ? 1 : 0;
292 if (bit_on)
John W. Linvillef2223132006-01-23 16:59:58 -0500293 ledctl |= (1 << i);
Michael Buesch714eece2006-03-18 21:28:46 +0100294 else
295 ledctl &= ~(1 << i);
John W. Linvillef2223132006-01-23 16:59:58 -0500296 }
297 bcm43xx_write16(bcm, BCM43xx_MMIO_GPIO_CONTROL, ledctl);
Michael Bueschefa6a372006-06-27 21:38:40 +0200298 spin_unlock_irqrestore(&bcm->leds_lock, flags);
John W. Linvillef2223132006-01-23 16:59:58 -0500299}