blob: 538d3117594bd4e4c77c65ae3abe5a0a11bdeb6f [file] [log] [blame]
Larry Finger75388ac2007-09-25 16:46:54 -07001/*
2
Larry Fingerba48f7b2007-10-12 23:04:51 -05003 Broadcom B43 wireless driver
4 LED control
Larry Finger75388ac2007-09-25 16:46:54 -07005
6 Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
Stefano Brivio1f21ad22007-11-06 22:49:20 +01007 Copyright (c) 2005 Stefano Brivio <stefano.brivio@polimi.it>
Larry Fingerba48f7b2007-10-12 23:04:51 -05008 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>
Larry Finger75388ac2007-09-25 16:46:54 -070011
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
Larry Finger75388ac2007-09-25 16:46:54 -070029#include "b43legacy.h"
Larry Fingerba48f7b2007-10-12 23:04:51 -050030#include "leds.h"
Larry Finger75388ac2007-09-25 16:46:54 -070031
Larry Fingerba48f7b2007-10-12 23:04:51 -050032
33static void b43legacy_led_turn_on(struct b43legacy_wldev *dev, u8 led_index,
34 bool activelow)
Larry Finger75388ac2007-09-25 16:46:54 -070035{
Larry Fingerba48f7b2007-10-12 23:04:51 -050036 struct b43legacy_wl *wl = dev->wl;
Larry Finger75388ac2007-09-25 16:46:54 -070037 unsigned long flags;
Larry Fingerba48f7b2007-10-12 23:04:51 -050038 u16 ctl;
Larry Finger75388ac2007-09-25 16:46:54 -070039
Larry Fingerba48f7b2007-10-12 23:04:51 -050040 spin_lock_irqsave(&wl->leds_lock, flags);
41 ctl = b43legacy_read16(dev, B43legacy_MMIO_GPIO_CONTROL);
42 if (activelow)
43 ctl &= ~(1 << led_index);
44 else
45 ctl |= (1 << led_index);
46 b43legacy_write16(dev, B43legacy_MMIO_GPIO_CONTROL, ctl);
47 spin_unlock_irqrestore(&wl->leds_lock, flags);
Larry Finger75388ac2007-09-25 16:46:54 -070048}
49
Larry Fingerba48f7b2007-10-12 23:04:51 -050050static void b43legacy_led_turn_off(struct b43legacy_wldev *dev, u8 led_index,
51 bool activelow)
Larry Finger75388ac2007-09-25 16:46:54 -070052{
Larry Fingerba48f7b2007-10-12 23:04:51 -050053 struct b43legacy_wl *wl = dev->wl;
54 unsigned long flags;
55 u16 ctl;
56
57 spin_lock_irqsave(&wl->leds_lock, flags);
58 ctl = b43legacy_read16(dev, B43legacy_MMIO_GPIO_CONTROL);
59 if (activelow)
60 ctl |= (1 << led_index);
61 else
62 ctl &= ~(1 << led_index);
63 b43legacy_write16(dev, B43legacy_MMIO_GPIO_CONTROL, ctl);
64 spin_unlock_irqrestore(&wl->leds_lock, flags);
Larry Finger75388ac2007-09-25 16:46:54 -070065}
66
Larry Fingerba48f7b2007-10-12 23:04:51 -050067/* Callback from the LED subsystem. */
68static void b43legacy_led_brightness_set(struct led_classdev *led_dev,
69 enum led_brightness brightness)
Larry Finger75388ac2007-09-25 16:46:54 -070070{
Larry Fingerba48f7b2007-10-12 23:04:51 -050071 struct b43legacy_led *led = container_of(led_dev, struct b43legacy_led,
72 led_dev);
Larry Finger75388ac2007-09-25 16:46:54 -070073 struct b43legacy_wldev *dev = led->dev;
Larry Fingerba48f7b2007-10-12 23:04:51 -050074 bool radio_enabled;
Larry Finger75388ac2007-09-25 16:46:54 -070075
Larry Fingerba48f7b2007-10-12 23:04:51 -050076 /* Checking the radio-enabled status here is slightly racy,
77 * but we want to avoid the locking overhead and we don't care
78 * whether the LED has the wrong state for a second. */
79 radio_enabled = (dev->phy.radio_on && dev->radio_hw_enable);
Larry Finger75388ac2007-09-25 16:46:54 -070080
Larry Fingerba48f7b2007-10-12 23:04:51 -050081 if (brightness == LED_OFF || !radio_enabled)
82 b43legacy_led_turn_off(dev, led->index, led->activelow);
Larry Finger75388ac2007-09-25 16:46:54 -070083 else
Larry Fingerba48f7b2007-10-12 23:04:51 -050084 b43legacy_led_turn_on(dev, led->index, led->activelow);
Larry Finger75388ac2007-09-25 16:46:54 -070085}
86
Larry Fingerba48f7b2007-10-12 23:04:51 -050087static int b43legacy_register_led(struct b43legacy_wldev *dev,
88 struct b43legacy_led *led,
Johannes Berg19d337d2009-06-02 13:01:37 +020089 const char *name,
90 const char *default_trigger,
Larry Fingerba48f7b2007-10-12 23:04:51 -050091 u8 led_index, bool activelow)
Larry Finger75388ac2007-09-25 16:46:54 -070092{
Larry Fingerba48f7b2007-10-12 23:04:51 -050093 int err;
Larry Finger75388ac2007-09-25 16:46:54 -070094
Larry Fingerba48f7b2007-10-12 23:04:51 -050095 b43legacy_led_turn_off(dev, led_index, activelow);
96 if (led->dev)
97 return -EEXIST;
98 if (!default_trigger)
99 return -EINVAL;
100 led->dev = dev;
101 led->index = led_index;
102 led->activelow = activelow;
103 strncpy(led->name, name, sizeof(led->name));
Larry Finger75388ac2007-09-25 16:46:54 -0700104
Larry Fingerba48f7b2007-10-12 23:04:51 -0500105 led->led_dev.name = led->name;
106 led->led_dev.default_trigger = default_trigger;
107 led->led_dev.brightness_set = b43legacy_led_brightness_set;
108
109 err = led_classdev_register(dev->dev->dev, &led->led_dev);
110 if (err) {
111 b43legacywarn(dev->wl, "LEDs: Failed to register %s\n", name);
112 led->dev = NULL;
113 return err;
114 }
115 return 0;
116}
117
118static void b43legacy_unregister_led(struct b43legacy_led *led)
119{
120 if (!led->dev)
121 return;
122 led_classdev_unregister(&led->led_dev);
123 b43legacy_led_turn_off(led->dev, led->index, led->activelow);
124 led->dev = NULL;
125}
126
127static void b43legacy_map_led(struct b43legacy_wldev *dev,
128 u8 led_index,
129 enum b43legacy_led_behaviour behaviour,
130 bool activelow)
131{
132 struct ieee80211_hw *hw = dev->wl->hw;
133 char name[B43legacy_LED_MAX_NAME_LEN + 1];
134
135 /* Map the b43 specific LED behaviour value to the
136 * generic LED triggers. */
137 switch (behaviour) {
138 case B43legacy_LED_INACTIVE:
Larry Finger75388ac2007-09-25 16:46:54 -0700139 break;
Larry Fingerba48f7b2007-10-12 23:04:51 -0500140 case B43legacy_LED_OFF:
141 b43legacy_led_turn_off(dev, led_index, activelow);
Larry Finger75388ac2007-09-25 16:46:54 -0700142 break;
Larry Fingerba48f7b2007-10-12 23:04:51 -0500143 case B43legacy_LED_ON:
144 b43legacy_led_turn_on(dev, led_index, activelow);
Larry Finger75388ac2007-09-25 16:46:54 -0700145 break;
Larry Fingerba48f7b2007-10-12 23:04:51 -0500146 case B43legacy_LED_ACTIVITY:
147 case B43legacy_LED_TRANSFER:
148 case B43legacy_LED_APTRANSFER:
149 snprintf(name, sizeof(name),
Danny Kukawkab157b5e2009-01-31 15:52:16 +0100150 "b43legacy-%s::tx", wiphy_name(hw->wiphy));
Larry Fingerba48f7b2007-10-12 23:04:51 -0500151 b43legacy_register_led(dev, &dev->led_tx, name,
152 ieee80211_get_tx_led_name(hw),
153 led_index, activelow);
154 snprintf(name, sizeof(name),
Danny Kukawkab157b5e2009-01-31 15:52:16 +0100155 "b43legacy-%s::rx", wiphy_name(hw->wiphy));
Larry Fingerba48f7b2007-10-12 23:04:51 -0500156 b43legacy_register_led(dev, &dev->led_rx, name,
157 ieee80211_get_rx_led_name(hw),
158 led_index, activelow);
159 break;
Larry Fingerba48f7b2007-10-12 23:04:51 -0500160 case B43legacy_LED_RADIO_ALL:
161 case B43legacy_LED_RADIO_A:
162 case B43legacy_LED_RADIO_B:
163 case B43legacy_LED_MODE_BG:
Larry Finger93bb7f32007-10-10 22:44:22 -0500164 snprintf(name, sizeof(name),
Danny Kukawkab157b5e2009-01-31 15:52:16 +0100165 "b43legacy-%s::radio", wiphy_name(hw->wiphy));
Larry Finger93bb7f32007-10-10 22:44:22 -0500166 b43legacy_register_led(dev, &dev->led_radio, name,
167 b43legacy_rfkill_led_name(dev),
168 led_index, activelow);
Larry Finger4ad36d72007-12-16 19:21:06 +0100169 /* Sync the RF-kill LED state with the switch state. */
170 if (dev->radio_hw_enable)
171 b43legacy_led_turn_on(dev, led_index, activelow);
Larry Finger93bb7f32007-10-10 22:44:22 -0500172 break;
Larry Fingerba48f7b2007-10-12 23:04:51 -0500173 case B43legacy_LED_WEIRD:
174 case B43legacy_LED_ASSOC:
175 snprintf(name, sizeof(name),
Danny Kukawkab157b5e2009-01-31 15:52:16 +0100176 "b43legacy-%s::assoc", wiphy_name(hw->wiphy));
Larry Fingerba48f7b2007-10-12 23:04:51 -0500177 b43legacy_register_led(dev, &dev->led_assoc, name,
178 ieee80211_get_assoc_led_name(hw),
179 led_index, activelow);
Larry Finger75388ac2007-09-25 16:46:54 -0700180 break;
181 default:
Larry Fingerba48f7b2007-10-12 23:04:51 -0500182 b43legacywarn(dev->wl, "LEDs: Unknown behaviour 0x%02X\n",
183 behaviour);
184 break;
Larry Finger75388ac2007-09-25 16:46:54 -0700185 }
186}
187
Larry Fingerba48f7b2007-10-12 23:04:51 -0500188void b43legacy_leds_init(struct b43legacy_wldev *dev)
Larry Finger75388ac2007-09-25 16:46:54 -0700189{
Larry Fingerba48f7b2007-10-12 23:04:51 -0500190 struct ssb_bus *bus = dev->dev->bus;
Larry Finger75388ac2007-09-25 16:46:54 -0700191 u8 sprom[4];
192 int i;
Larry Fingerba48f7b2007-10-12 23:04:51 -0500193 enum b43legacy_led_behaviour behaviour;
194 bool activelow;
Larry Finger75388ac2007-09-25 16:46:54 -0700195
Larry Finger7797aa32007-11-09 16:57:34 -0600196 sprom[0] = bus->sprom.gpio0;
197 sprom[1] = bus->sprom.gpio1;
198 sprom[2] = bus->sprom.gpio2;
199 sprom[3] = bus->sprom.gpio3;
Larry Finger75388ac2007-09-25 16:46:54 -0700200
Larry Fingerba48f7b2007-10-12 23:04:51 -0500201 for (i = 0; i < 4; i++) {
202 if (sprom[i] == 0xFF) {
203 /* There is no LED information in the SPROM
204 * for this LED. Hardcode it here. */
205 activelow = 0;
206 switch (i) {
207 case 0:
208 behaviour = B43legacy_LED_ACTIVITY;
209 activelow = 1;
210 if (bus->boardinfo.vendor == PCI_VENDOR_ID_COMPAQ)
211 behaviour = B43legacy_LED_RADIO_ALL;
212 break;
213 case 1:
214 behaviour = B43legacy_LED_RADIO_B;
215 if (bus->boardinfo.vendor == PCI_VENDOR_ID_ASUSTEK)
216 behaviour = B43legacy_LED_ASSOC;
217 break;
218 case 2:
219 behaviour = B43legacy_LED_RADIO_A;
220 break;
221 case 3:
222 behaviour = B43legacy_LED_OFF;
223 break;
224 default:
225 B43legacy_WARN_ON(1);
226 return;
227 }
228 } else {
229 behaviour = sprom[i] & B43legacy_LED_BEHAVIOUR;
230 activelow = !!(sprom[i] & B43legacy_LED_ACTIVELOW);
Larry Finger75388ac2007-09-25 16:46:54 -0700231 }
Larry Fingerba48f7b2007-10-12 23:04:51 -0500232 b43legacy_map_led(dev, i, behaviour, activelow);
Larry Finger75388ac2007-09-25 16:46:54 -0700233 }
Larry Finger75388ac2007-09-25 16:46:54 -0700234}
235
236void b43legacy_leds_exit(struct b43legacy_wldev *dev)
237{
Larry Fingerba48f7b2007-10-12 23:04:51 -0500238 b43legacy_unregister_led(&dev->led_tx);
239 b43legacy_unregister_led(&dev->led_rx);
240 b43legacy_unregister_led(&dev->led_assoc);
Larry Finger4ad36d72007-12-16 19:21:06 +0100241 b43legacy_unregister_led(&dev->led_radio);
Larry Finger75388ac2007-09-25 16:46:54 -0700242}