blob: 19e588582c7c4d42ed22493afcdca825ca64e9c9 [file] [log] [blame]
Michael Buesche4d6b792007-09-18 15:39:42 -04001/*
2
3 Broadcom B43 wireless driver
Michael Buesch21954c32007-09-27 15:31:40 +02004 LED control
Michael Buesche4d6b792007-09-18 15:39:42 -04005
6 Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
Michael Buesch21954c32007-09-27 15:31:40 +02007 Copyright (c) 2005 Stefano Brivio <st3@riseup.net>
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>
Michael Buesche4d6b792007-09-18 15:39:42 -040011
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"
Michael Buesche4d6b792007-09-18 15:39:42 -040031
Michael Buesch21954c32007-09-27 15:31:40 +020032
33static void b43_led_turn_on(struct b43_wldev *dev, u8 led_index,
34 bool activelow)
Michael Buesche4d6b792007-09-18 15:39:42 -040035{
Michael Buesch21954c32007-09-27 15:31:40 +020036 struct b43_wl *wl = dev->wl;
Michael Buesche4d6b792007-09-18 15:39:42 -040037 unsigned long flags;
Michael Buesch21954c32007-09-27 15:31:40 +020038 u16 ctl;
Michael Buesche4d6b792007-09-18 15:39:42 -040039
Michael Buesch21954c32007-09-27 15:31:40 +020040 spin_lock_irqsave(&wl->leds_lock, flags);
41 ctl = b43_read16(dev, B43_MMIO_GPIO_CONTROL);
42 if (activelow)
43 ctl &= ~(1 << led_index);
44 else
45 ctl |= (1 << led_index);
46 b43_write16(dev, B43_MMIO_GPIO_CONTROL, ctl);
47 spin_unlock_irqrestore(&wl->leds_lock, flags);
Michael Buesche4d6b792007-09-18 15:39:42 -040048}
49
Michael Buesch21954c32007-09-27 15:31:40 +020050static void b43_led_turn_off(struct b43_wldev *dev, u8 led_index,
51 bool activelow)
Michael Buesche4d6b792007-09-18 15:39:42 -040052{
Michael Buesch21954c32007-09-27 15:31:40 +020053 struct b43_wl *wl = dev->wl;
54 unsigned long flags;
55 u16 ctl;
56
57 spin_lock_irqsave(&wl->leds_lock, flags);
58 ctl = b43_read16(dev, B43_MMIO_GPIO_CONTROL);
59 if (activelow)
60 ctl |= (1 << led_index);
61 else
62 ctl &= ~(1 << led_index);
63 b43_write16(dev, B43_MMIO_GPIO_CONTROL, ctl);
64 spin_unlock_irqrestore(&wl->leds_lock, flags);
Michael Buesche4d6b792007-09-18 15:39:42 -040065}
66
Michael Buesch21954c32007-09-27 15:31:40 +020067/* Callback from the LED subsystem. */
68static void b43_led_brightness_set(struct led_classdev *led_dev,
69 enum led_brightness brightness)
Michael Buesche4d6b792007-09-18 15:39:42 -040070{
Michael Buesch21954c32007-09-27 15:31:40 +020071 struct b43_led *led = container_of(led_dev, struct b43_led, led_dev);
Michael Buesche4d6b792007-09-18 15:39:42 -040072 struct b43_wldev *dev = led->dev;
Michael Buesch21954c32007-09-27 15:31:40 +020073 bool radio_enabled;
Michael Buesche4d6b792007-09-18 15:39:42 -040074
Michael Buesch21954c32007-09-27 15:31:40 +020075 /* Checking the radio-enabled status here is slightly racy,
76 * but we want to avoid the locking overhead and we don't care
77 * whether the LED has the wrong state for a second. */
78 radio_enabled = (dev->phy.radio_on && dev->radio_hw_enable);
Michael Buesche4d6b792007-09-18 15:39:42 -040079
Michael Buesch21954c32007-09-27 15:31:40 +020080 if (brightness == LED_OFF || !radio_enabled)
81 b43_led_turn_off(dev, led->index, led->activelow);
Michael Buesche4d6b792007-09-18 15:39:42 -040082 else
Michael Buesch21954c32007-09-27 15:31:40 +020083 b43_led_turn_on(dev, led->index, led->activelow);
Michael Buesche4d6b792007-09-18 15:39:42 -040084}
85
Michael Buesch21954c32007-09-27 15:31:40 +020086static int b43_register_led(struct b43_wldev *dev, struct b43_led *led,
87 const char *name, char *default_trigger,
88 u8 led_index, bool activelow)
Michael Buesche4d6b792007-09-18 15:39:42 -040089{
Michael Buesch21954c32007-09-27 15:31:40 +020090 int err;
Michael Buesche4d6b792007-09-18 15:39:42 -040091
Michael Buesch21954c32007-09-27 15:31:40 +020092 b43_led_turn_off(dev, led_index, activelow);
93 if (led->dev)
94 return -EEXIST;
95 if (!default_trigger)
96 return -EINVAL;
97 led->dev = dev;
98 led->index = led_index;
99 led->activelow = activelow;
100 strncpy(led->name, name, sizeof(led->name));
Michael Buesche4d6b792007-09-18 15:39:42 -0400101
Michael Buesch21954c32007-09-27 15:31:40 +0200102 led->led_dev.name = led->name;
103 led->led_dev.default_trigger = default_trigger;
104 led->led_dev.brightness_set = b43_led_brightness_set;
105
106 err = led_classdev_register(dev->dev->dev, &led->led_dev);
107 if (err) {
108 b43warn(dev->wl, "LEDs: Failed to register %s\n", name);
109 led->dev = NULL;
110 return err;
111 }
112 return 0;
113}
114
115static void b43_unregister_led(struct b43_led *led)
116{
117 if (!led->dev)
118 return;
119 led_classdev_unregister(&led->led_dev);
120 b43_led_turn_off(led->dev, led->index, led->activelow);
121 led->dev = NULL;
122}
123
124static void b43_map_led(struct b43_wldev *dev,
125 u8 led_index,
126 enum b43_led_behaviour behaviour,
127 bool activelow)
128{
129 struct ieee80211_hw *hw = dev->wl->hw;
130 char name[B43_LED_MAX_NAME_LEN + 1];
131
132 /* Map the b43 specific LED behaviour value to the
133 * generic LED triggers. */
134 switch (behaviour) {
135 case B43_LED_INACTIVE:
Michael Buesche4d6b792007-09-18 15:39:42 -0400136 break;
Michael Buesch21954c32007-09-27 15:31:40 +0200137 case B43_LED_OFF:
138 b43_led_turn_off(dev, led_index, activelow);
Michael Buesche4d6b792007-09-18 15:39:42 -0400139 break;
Michael Buesch21954c32007-09-27 15:31:40 +0200140 case B43_LED_ON:
141 b43_led_turn_on(dev, led_index, activelow);
Michael Buesche4d6b792007-09-18 15:39:42 -0400142 break;
Michael Buesch21954c32007-09-27 15:31:40 +0200143 case B43_LED_ACTIVITY:
144 case B43_LED_TRANSFER:
145 case B43_LED_APTRANSFER:
146 snprintf(name, sizeof(name),
147 "b43-%s:tx", wiphy_name(hw->wiphy));
148 b43_register_led(dev, &dev->led_tx, name,
149 ieee80211_get_tx_led_name(hw),
150 led_index, activelow);
151 snprintf(name, sizeof(name),
152 "b43-%s:rx", wiphy_name(hw->wiphy));
153 b43_register_led(dev, &dev->led_rx, name,
154 ieee80211_get_rx_led_name(hw),
155 led_index, activelow);
156 break;
Michael Buesch21954c32007-09-27 15:31:40 +0200157 case B43_LED_RADIO_ALL:
158 case B43_LED_RADIO_A:
159 case B43_LED_RADIO_B:
160 case B43_LED_MODE_BG:
Michael Buesch8e9f7522007-09-27 21:35:34 +0200161 snprintf(name, sizeof(name),
162 "b43-%s:radio", wiphy_name(hw->wiphy));
163 b43_register_led(dev, &dev->led_radio, name,
164 b43_rfkill_led_name(dev),
165 led_index, activelow);
166 break;
Michael Buesch21954c32007-09-27 15:31:40 +0200167 case B43_LED_WEIRD:
168 case B43_LED_ASSOC:
169 snprintf(name, sizeof(name),
170 "b43-%s:assoc", wiphy_name(hw->wiphy));
171 b43_register_led(dev, &dev->led_assoc, name,
172 ieee80211_get_assoc_led_name(hw),
173 led_index, activelow);
Michael Buesche4d6b792007-09-18 15:39:42 -0400174 break;
175 default:
Michael Buesch21954c32007-09-27 15:31:40 +0200176 b43warn(dev->wl, "LEDs: Unknown behaviour 0x%02X\n",
177 behaviour);
178 break;
Michael Buesche4d6b792007-09-18 15:39:42 -0400179 }
180}
181
Michael Buesch21954c32007-09-27 15:31:40 +0200182void b43_leds_init(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -0400183{
Michael Buesch21954c32007-09-27 15:31:40 +0200184 struct ssb_bus *bus = dev->dev->bus;
Michael Buesche4d6b792007-09-18 15:39:42 -0400185 u8 sprom[4];
186 int i;
Michael Buesch21954c32007-09-27 15:31:40 +0200187 enum b43_led_behaviour behaviour;
188 bool activelow;
Michael Buesche4d6b792007-09-18 15:39:42 -0400189
Michael Buesch21954c32007-09-27 15:31:40 +0200190 sprom[0] = bus->sprom.r1.gpio0;
191 sprom[1] = bus->sprom.r1.gpio1;
192 sprom[2] = bus->sprom.r1.gpio2;
193 sprom[3] = bus->sprom.r1.gpio3;
Michael Buesche4d6b792007-09-18 15:39:42 -0400194
Michael Buesch21954c32007-09-27 15:31:40 +0200195 for (i = 0; i < 4; i++) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400196 if (sprom[i] == 0xFF) {
Michael Buesch21954c32007-09-27 15:31:40 +0200197 /* There is no LED information in the SPROM
198 * for this LED. Hardcode it here. */
199 activelow = 0;
200 switch (i) {
201 case 0:
202 behaviour = B43_LED_ACTIVITY;
203 activelow = 1;
204 if (bus->boardinfo.vendor == PCI_VENDOR_ID_COMPAQ)
205 behaviour = B43_LED_RADIO_ALL;
206 break;
207 case 1:
208 behaviour = B43_LED_RADIO_B;
209 if (bus->boardinfo.vendor == PCI_VENDOR_ID_ASUSTEK)
210 behaviour = B43_LED_ASSOC;
211 break;
212 case 2:
213 behaviour = B43_LED_RADIO_A;
214 break;
215 case 3:
216 behaviour = B43_LED_OFF;
217 break;
218 default:
219 B43_WARN_ON(1);
220 return;
221 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400222 } else {
Michael Buesch21954c32007-09-27 15:31:40 +0200223 behaviour = sprom[i] & B43_LED_BEHAVIOUR;
224 activelow = !!(sprom[i] & B43_LED_ACTIVELOW);
Michael Buesche4d6b792007-09-18 15:39:42 -0400225 }
Michael Buesch21954c32007-09-27 15:31:40 +0200226 b43_map_led(dev, i, behaviour, activelow);
Michael Buesche4d6b792007-09-18 15:39:42 -0400227 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400228}
229
230void b43_leds_exit(struct b43_wldev *dev)
231{
Michael Buesch21954c32007-09-27 15:31:40 +0200232 b43_unregister_led(&dev->led_tx);
233 b43_unregister_led(&dev->led_rx);
234 b43_unregister_led(&dev->led_assoc);
Michael Buesche4d6b792007-09-18 15:39:42 -0400235}