Commit | Line | Data |
---|---|---|
e4d6b795 MB |
1 | /* |
2 | ||
3 | Broadcom B43 wireless driver | |
21954c36 | 4 | LED control |
e4d6b795 MB |
5 | |
6 | Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>, | |
21954c36 MB |
7 | 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> | |
e4d6b795 MB |
11 | |
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" | |
e4d6b795 | 31 | |
e4d6b795 | 32 | |
21954c36 MB |
33 | static void b43_led_turn_on(struct b43_wldev *dev, u8 led_index, |
34 | bool activelow) | |
e4d6b795 | 35 | { |
21954c36 | 36 | struct b43_wl *wl = dev->wl; |
e4d6b795 | 37 | unsigned long flags; |
21954c36 | 38 | u16 ctl; |
e4d6b795 | 39 | |
21954c36 MB |
40 | 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); | |
e4d6b795 MB |
48 | } |
49 | ||
21954c36 MB |
50 | static void b43_led_turn_off(struct b43_wldev *dev, u8 led_index, |
51 | bool activelow) | |
e4d6b795 | 52 | { |
21954c36 MB |
53 | 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); | |
e4d6b795 MB |
65 | } |
66 | ||
21954c36 MB |
67 | /* Callback from the LED subsystem. */ |
68 | static void b43_led_brightness_set(struct led_classdev *led_dev, | |
69 | enum led_brightness brightness) | |
e4d6b795 | 70 | { |
21954c36 | 71 | struct b43_led *led = container_of(led_dev, struct b43_led, led_dev); |
e4d6b795 | 72 | struct b43_wldev *dev = led->dev; |
21954c36 | 73 | bool radio_enabled; |
e4d6b795 | 74 | |
21954c36 MB |
75 | /* 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); | |
e4d6b795 | 79 | |
21954c36 MB |
80 | if (brightness == LED_OFF || !radio_enabled) |
81 | b43_led_turn_off(dev, led->index, led->activelow); | |
e4d6b795 | 82 | else |
21954c36 | 83 | b43_led_turn_on(dev, led->index, led->activelow); |
e4d6b795 MB |
84 | } |
85 | ||
21954c36 MB |
86 | static 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) | |
e4d6b795 | 89 | { |
21954c36 MB |
90 | int err; |
91 | ||
92 | 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)); | |
101 | ||
102 | 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 | ||
115 | static 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 | } | |
e4d6b795 | 123 | |
21954c36 MB |
124 | static 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]; | |
e4d6b795 | 131 | |
21954c36 MB |
132 | /* Map the b43 specific LED behaviour value to the |
133 | * generic LED triggers. */ | |
134 | switch (behaviour) { | |
135 | case B43_LED_INACTIVE: | |
136 | break; | |
137 | case B43_LED_OFF: | |
138 | b43_led_turn_off(dev, led_index, activelow); | |
e4d6b795 | 139 | break; |
21954c36 MB |
140 | case B43_LED_ON: |
141 | b43_led_turn_on(dev, led_index, activelow); | |
e4d6b795 | 142 | break; |
21954c36 MB |
143 | 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); | |
e4d6b795 | 156 | break; |
21954c36 MB |
157 | case B43_LED_RADIO_ALL: |
158 | case B43_LED_RADIO_A: | |
159 | case B43_LED_RADIO_B: | |
160 | case B43_LED_MODE_BG: | |
8e9f7529 MB |
161 | 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); | |
1a8d1227 LF |
166 | /* Sync the RF-kill LED state with the switch state. */ |
167 | if (dev->radio_hw_enable) | |
168 | b43_led_turn_on(dev, led_index, activelow); | |
8e9f7529 | 169 | break; |
21954c36 MB |
170 | case B43_LED_WEIRD: |
171 | case B43_LED_ASSOC: | |
172 | snprintf(name, sizeof(name), | |
173 | "b43-%s:assoc", wiphy_name(hw->wiphy)); | |
174 | b43_register_led(dev, &dev->led_assoc, name, | |
175 | ieee80211_get_assoc_led_name(hw), | |
176 | led_index, activelow); | |
e4d6b795 MB |
177 | break; |
178 | default: | |
21954c36 MB |
179 | b43warn(dev->wl, "LEDs: Unknown behaviour 0x%02X\n", |
180 | behaviour); | |
181 | break; | |
e4d6b795 MB |
182 | } |
183 | } | |
184 | ||
21954c36 | 185 | void b43_leds_init(struct b43_wldev *dev) |
e4d6b795 | 186 | { |
21954c36 | 187 | struct ssb_bus *bus = dev->dev->bus; |
e4d6b795 MB |
188 | u8 sprom[4]; |
189 | int i; | |
21954c36 MB |
190 | enum b43_led_behaviour behaviour; |
191 | bool activelow; | |
e4d6b795 | 192 | |
21954c36 MB |
193 | sprom[0] = bus->sprom.r1.gpio0; |
194 | sprom[1] = bus->sprom.r1.gpio1; | |
195 | sprom[2] = bus->sprom.r1.gpio2; | |
196 | sprom[3] = bus->sprom.r1.gpio3; | |
e4d6b795 | 197 | |
21954c36 | 198 | for (i = 0; i < 4; i++) { |
e4d6b795 | 199 | if (sprom[i] == 0xFF) { |
21954c36 MB |
200 | /* There is no LED information in the SPROM |
201 | * for this LED. Hardcode it here. */ | |
202 | activelow = 0; | |
203 | switch (i) { | |
204 | case 0: | |
205 | behaviour = B43_LED_ACTIVITY; | |
206 | activelow = 1; | |
207 | if (bus->boardinfo.vendor == PCI_VENDOR_ID_COMPAQ) | |
208 | behaviour = B43_LED_RADIO_ALL; | |
209 | break; | |
210 | case 1: | |
211 | behaviour = B43_LED_RADIO_B; | |
212 | if (bus->boardinfo.vendor == PCI_VENDOR_ID_ASUSTEK) | |
213 | behaviour = B43_LED_ASSOC; | |
214 | break; | |
215 | case 2: | |
216 | behaviour = B43_LED_RADIO_A; | |
217 | break; | |
218 | case 3: | |
219 | behaviour = B43_LED_OFF; | |
220 | break; | |
221 | default: | |
222 | B43_WARN_ON(1); | |
223 | return; | |
224 | } | |
e4d6b795 | 225 | } else { |
21954c36 MB |
226 | behaviour = sprom[i] & B43_LED_BEHAVIOUR; |
227 | activelow = !!(sprom[i] & B43_LED_ACTIVELOW); | |
e4d6b795 | 228 | } |
21954c36 | 229 | b43_map_led(dev, i, behaviour, activelow); |
e4d6b795 | 230 | } |
e4d6b795 MB |
231 | } |
232 | ||
233 | void b43_leds_exit(struct b43_wldev *dev) | |
234 | { | |
21954c36 MB |
235 | b43_unregister_led(&dev->led_tx); |
236 | b43_unregister_led(&dev->led_rx); | |
237 | b43_unregister_led(&dev->led_assoc); | |
1a8d1227 | 238 | b43_unregister_led(&dev->led_radio); |
e4d6b795 | 239 | } |