blob: 455a0c743f73ed4f163c2069aaa0aafcc0880ba2 [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);
38 u16 ledctl;
39
40 assert(index >= 0 && index < BCM43xx_NR_LEDS);
41 assert(led->blink_interval);
42 ledctl = bcm43xx_read16(bcm, BCM43xx_MMIO_GPIO_CONTROL);
43 __change_bit(index, (unsigned long *)(&ledctl));
44 bcm43xx_write16(bcm, BCM43xx_MMIO_GPIO_CONTROL, ledctl);
45}
46
47static void bcm43xx_led_blink(unsigned long d)
48{
49 struct bcm43xx_led *led = (struct bcm43xx_led *)d;
50 struct bcm43xx_private *bcm = led->bcm;
51 unsigned long flags;
52
53 spin_lock_irqsave(&bcm->lock, flags);
54 if (led->blink_interval) {
55 bcm43xx_led_changestate(led);
56 mod_timer(&led->blink_timer, jiffies + led->blink_interval);
57 }
58 spin_unlock_irqrestore(&bcm->lock, flags);
59}
60
61static void bcm43xx_led_blink_start(struct bcm43xx_led *led,
62 unsigned long interval)
63{
64 led->blink_interval = interval;
65 bcm43xx_led_changestate(led);
66 led->blink_timer.expires = jiffies + interval;
67 add_timer(&led->blink_timer);
68}
69
70static void bcm43xx_led_blink_stop(struct bcm43xx_led *led, int sync)
71{
72 struct bcm43xx_private *bcm = led->bcm;
73 const int index = bcm43xx_led_index(led);
74 u16 ledctl;
75
76 if (!led->blink_interval)
77 return;
78 if (unlikely(sync))
79 del_timer_sync(&led->blink_timer);
80 else
81 del_timer(&led->blink_timer);
82 led->blink_interval = 0;
83
84 /* Make sure the LED is turned off. */
85 assert(index >= 0 && index < BCM43xx_NR_LEDS);
86 ledctl = bcm43xx_read16(bcm, BCM43xx_MMIO_GPIO_CONTROL);
87 if (led->activelow)
88 ledctl |= (1 << index);
89 else
90 ledctl &= ~(1 << index);
91 bcm43xx_write16(bcm, BCM43xx_MMIO_GPIO_CONTROL, ledctl);
92}
93
94int bcm43xx_leds_init(struct bcm43xx_private *bcm)
95{
96 struct bcm43xx_led *led;
97 u8 sprom[4];
98 int i;
99
100 sprom[0] = bcm->sprom.wl0gpio0;
101 sprom[1] = bcm->sprom.wl0gpio1;
102 sprom[2] = bcm->sprom.wl0gpio2;
103 sprom[3] = bcm->sprom.wl0gpio3;
104
105 for (i = 0; i < BCM43xx_NR_LEDS; i++) {
106 led = &(bcm->leds[i]);
107 led->bcm = bcm;
108 init_timer(&led->blink_timer);
109 led->blink_timer.data = (unsigned long)led;
110 led->blink_timer.function = bcm43xx_led_blink;
111
112 if (sprom[i] == 0xFF) {
113 /* SPROM information not set. */
114 switch (i) {
115 case 0:
116 if (bcm->board_vendor == PCI_VENDOR_ID_COMPAQ)
117 led->behaviour = BCM43xx_LED_RADIO_ALL;
118 else
119 led->behaviour = BCM43xx_LED_ACTIVITY;
120 break;
121 case 1:
122 led->behaviour = BCM43xx_LED_RADIO_B;
123 break;
124 case 2:
125 led->behaviour = BCM43xx_LED_RADIO_A;
126 break;
127 case 3:
128 led->behaviour = BCM43xx_LED_OFF;
129 break;
130 default:
131 assert(0);
132 }
133 } else {
134 led->behaviour = sprom[i] & BCM43xx_LED_BEHAVIOUR;
135 led->activelow = !!(sprom[i] & BCM43xx_LED_ACTIVELOW);
136 }
137 }
138
139 return 0;
140}
141
142void bcm43xx_leds_exit(struct bcm43xx_private *bcm)
143{
144 struct bcm43xx_led *led;
145 int i;
146
147 for (i = 0; i < BCM43xx_NR_LEDS; i++) {
148 led = &(bcm->leds[i]);
149 bcm43xx_led_blink_stop(led, 1);
150 }
151 bcm43xx_leds_turn_off(bcm);
152}
153
154void bcm43xx_leds_update(struct bcm43xx_private *bcm, int activity)
155{
156 struct bcm43xx_led *led;
157 struct bcm43xx_radioinfo *radio = bcm->current_core->radio;
158 struct bcm43xx_phyinfo *phy = bcm->current_core->phy;
159 const int transferring = (jiffies - bcm->stats.last_tx) < BCM43xx_LED_XFER_THRES;
160 int i, turn_on = 0;
161 unsigned long interval = 0;
162 u16 ledctl;
163
164 ledctl = bcm43xx_read16(bcm, BCM43xx_MMIO_GPIO_CONTROL);
165 for (i = 0; i < BCM43xx_NR_LEDS; i++) {
166 led = &(bcm->leds[i]);
167 if (led->behaviour == BCM43xx_LED_INACTIVE)
168 continue;
169
170 switch (led->behaviour) {
171 case BCM43xx_LED_OFF:
172 turn_on = 0;
173 break;
174 case BCM43xx_LED_ON:
175 turn_on = 1;
176 break;
177 case BCM43xx_LED_ACTIVITY:
178 turn_on = activity;
179 break;
180 case BCM43xx_LED_RADIO_ALL:
181 turn_on = radio->enabled;
182 break;
183 case BCM43xx_LED_RADIO_A:
184 turn_on = (radio->enabled && phy->type == BCM43xx_PHYTYPE_A);
185 break;
186 case BCM43xx_LED_RADIO_B:
187 turn_on = (radio->enabled &&
188 (phy->type == BCM43xx_PHYTYPE_B ||
189 phy->type == BCM43xx_PHYTYPE_G));
190 break;
191 case BCM43xx_LED_MODE_BG:
192 turn_on = 0;
193 if (phy->type == BCM43xx_PHYTYPE_G &&
194 1/*FIXME: using G rates.*/)
195 turn_on = 1;
196 break;
197 case BCM43xx_LED_TRANSFER:
198 if (transferring)
199 bcm43xx_led_blink_start(led, BCM43xx_LEDBLINK_MEDIUM);
200 else
201 bcm43xx_led_blink_stop(led, 0);
202 continue;
203 case BCM43xx_LED_APTRANSFER:
204 if (bcm->ieee->iw_mode == IW_MODE_MASTER) {
205 if (transferring) {
206 interval = BCM43xx_LEDBLINK_FAST;
207 turn_on = 1;
208 }
209 } else {
210 turn_on = 1;
211 if (0/*TODO: not assoc*/)
212 interval = BCM43xx_LEDBLINK_SLOW;
213 else if (transferring)
214 interval = BCM43xx_LEDBLINK_FAST;
215 else
216 turn_on = 0;
217 }
218 if (turn_on)
219 bcm43xx_led_blink_start(led, interval);
220 else
221 bcm43xx_led_blink_stop(led, 0);
222 continue;
223 case BCM43xx_LED_WEIRD:
224 //TODO
225 turn_on = 0;
226 break;
227 case BCM43xx_LED_ASSOC:
228 if (1/*TODO: associated*/)
229 turn_on = 1;
230 break;
231 default:
232 assert(0);
233 };
234
235 if (led->activelow)
236 turn_on = !turn_on;
237 if (turn_on)
238 ledctl |= (1 << i);
239 else
240 ledctl &= ~(1 << i);
241 }
242 bcm43xx_write16(bcm, BCM43xx_MMIO_GPIO_CONTROL, ledctl);
243}
244
245void bcm43xx_leds_turn_off(struct bcm43xx_private *bcm)
246{
247 struct bcm43xx_led *led;
248 u16 ledctl = 0;
249 int i;
250
251 for (i = 0; i < BCM43xx_NR_LEDS; i++) {
252 led = &(bcm->leds[i]);
253 if (led->behaviour == BCM43xx_LED_INACTIVE)
254 continue;
255 if (led->activelow)
256 ledctl |= (1 << i);
257 }
258 bcm43xx_write16(bcm, BCM43xx_MMIO_GPIO_CONTROL, ledctl);
259}
260
261/* vim: set ts=8 sw=8 sts=8: */