leds.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. Broadcom B43 wireless driver
  3. LED control
  4. Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
  5. Copyright (c) 2005 Stefano Brivio <stefano.brivio@polimi.it>
  6. Copyright (c) 2005-2007 Michael Buesch <mb@bu3sch.de>
  7. Copyright (c) 2005 Danny van Dyk <kugelfang@gentoo.org>
  8. Copyright (c) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; see the file COPYING. If not, write to
  19. the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  20. Boston, MA 02110-1301, USA.
  21. */
  22. #include "b43.h"
  23. #include "leds.h"
  24. #include "rfkill.h"
  25. static void b43_led_turn_on(struct b43_wldev *dev, u8 led_index,
  26. bool activelow)
  27. {
  28. struct b43_wl *wl = dev->wl;
  29. unsigned long flags;
  30. u16 ctl;
  31. spin_lock_irqsave(&wl->leds_lock, flags);
  32. ctl = b43_read16(dev, B43_MMIO_GPIO_CONTROL);
  33. if (activelow)
  34. ctl &= ~(1 << led_index);
  35. else
  36. ctl |= (1 << led_index);
  37. b43_write16(dev, B43_MMIO_GPIO_CONTROL, ctl);
  38. spin_unlock_irqrestore(&wl->leds_lock, flags);
  39. }
  40. static void b43_led_turn_off(struct b43_wldev *dev, u8 led_index,
  41. bool activelow)
  42. {
  43. struct b43_wl *wl = dev->wl;
  44. unsigned long flags;
  45. u16 ctl;
  46. spin_lock_irqsave(&wl->leds_lock, flags);
  47. ctl = b43_read16(dev, B43_MMIO_GPIO_CONTROL);
  48. if (activelow)
  49. ctl |= (1 << led_index);
  50. else
  51. ctl &= ~(1 << led_index);
  52. b43_write16(dev, B43_MMIO_GPIO_CONTROL, ctl);
  53. spin_unlock_irqrestore(&wl->leds_lock, flags);
  54. }
  55. /* Callback from the LED subsystem. */
  56. static void b43_led_brightness_set(struct led_classdev *led_dev,
  57. enum led_brightness brightness)
  58. {
  59. struct b43_led *led = container_of(led_dev, struct b43_led, led_dev);
  60. struct b43_wldev *dev = led->dev;
  61. bool radio_enabled;
  62. if (unlikely(b43_status(dev) < B43_STAT_INITIALIZED))
  63. return;
  64. /* Checking the radio-enabled status here is slightly racy,
  65. * but we want to avoid the locking overhead and we don't care
  66. * whether the LED has the wrong state for a second. */
  67. radio_enabled = (dev->phy.radio_on && dev->radio_hw_enable);
  68. if (brightness == LED_OFF || !radio_enabled)
  69. b43_led_turn_off(dev, led->index, led->activelow);
  70. else
  71. b43_led_turn_on(dev, led->index, led->activelow);
  72. }
  73. static int b43_register_led(struct b43_wldev *dev, struct b43_led *led,
  74. const char *name, const char *default_trigger,
  75. u8 led_index, bool activelow)
  76. {
  77. int err;
  78. b43_led_turn_off(dev, led_index, activelow);
  79. if (led->dev)
  80. return -EEXIST;
  81. if (!default_trigger)
  82. return -EINVAL;
  83. led->dev = dev;
  84. led->index = led_index;
  85. led->activelow = activelow;
  86. strncpy(led->name, name, sizeof(led->name));
  87. led->led_dev.name = led->name;
  88. led->led_dev.default_trigger = default_trigger;
  89. led->led_dev.brightness_set = b43_led_brightness_set;
  90. err = led_classdev_register(dev->dev->dev, &led->led_dev);
  91. if (err) {
  92. b43warn(dev->wl, "LEDs: Failed to register %s\n", name);
  93. led->dev = NULL;
  94. return err;
  95. }
  96. return 0;
  97. }
  98. static void b43_unregister_led(struct b43_led *led)
  99. {
  100. if (!led->dev)
  101. return;
  102. led_classdev_unregister(&led->led_dev);
  103. b43_led_turn_off(led->dev, led->index, led->activelow);
  104. led->dev = NULL;
  105. }
  106. static void b43_map_led(struct b43_wldev *dev,
  107. u8 led_index,
  108. enum b43_led_behaviour behaviour,
  109. bool activelow)
  110. {
  111. struct ieee80211_hw *hw = dev->wl->hw;
  112. char name[B43_LED_MAX_NAME_LEN + 1];
  113. /* Map the b43 specific LED behaviour value to the
  114. * generic LED triggers. */
  115. switch (behaviour) {
  116. case B43_LED_INACTIVE:
  117. break;
  118. case B43_LED_OFF:
  119. b43_led_turn_off(dev, led_index, activelow);
  120. break;
  121. case B43_LED_ON:
  122. b43_led_turn_on(dev, led_index, activelow);
  123. break;
  124. case B43_LED_ACTIVITY:
  125. case B43_LED_TRANSFER:
  126. case B43_LED_APTRANSFER:
  127. snprintf(name, sizeof(name),
  128. "b43-%s::tx", wiphy_name(hw->wiphy));
  129. b43_register_led(dev, &dev->led_tx, name,
  130. ieee80211_get_tx_led_name(hw),
  131. led_index, activelow);
  132. snprintf(name, sizeof(name),
  133. "b43-%s::rx", wiphy_name(hw->wiphy));
  134. b43_register_led(dev, &dev->led_rx, name,
  135. ieee80211_get_rx_led_name(hw),
  136. led_index, activelow);
  137. break;
  138. case B43_LED_RADIO_ALL:
  139. case B43_LED_RADIO_A:
  140. case B43_LED_RADIO_B:
  141. case B43_LED_MODE_BG:
  142. snprintf(name, sizeof(name),
  143. "b43-%s::radio", wiphy_name(hw->wiphy));
  144. b43_register_led(dev, &dev->led_radio, name,
  145. ieee80211_get_radio_led_name(hw),
  146. led_index, activelow);
  147. /* Sync the RF-kill LED state with radio and switch states. */
  148. if (dev->phy.radio_on && b43_is_hw_radio_enabled(dev))
  149. b43_led_turn_on(dev, led_index, activelow);
  150. break;
  151. case B43_LED_WEIRD:
  152. case B43_LED_ASSOC:
  153. snprintf(name, sizeof(name),
  154. "b43-%s::assoc", wiphy_name(hw->wiphy));
  155. b43_register_led(dev, &dev->led_assoc, name,
  156. ieee80211_get_assoc_led_name(hw),
  157. led_index, activelow);
  158. break;
  159. default:
  160. b43warn(dev->wl, "LEDs: Unknown behaviour 0x%02X\n",
  161. behaviour);
  162. break;
  163. }
  164. }
  165. void b43_leds_init(struct b43_wldev *dev)
  166. {
  167. struct ssb_bus *bus = dev->dev->bus;
  168. u8 sprom[4];
  169. int i;
  170. enum b43_led_behaviour behaviour;
  171. bool activelow;
  172. sprom[0] = bus->sprom.gpio0;
  173. sprom[1] = bus->sprom.gpio1;
  174. sprom[2] = bus->sprom.gpio2;
  175. sprom[3] = bus->sprom.gpio3;
  176. for (i = 0; i < 4; i++) {
  177. if (sprom[i] == 0xFF) {
  178. /* There is no LED information in the SPROM
  179. * for this LED. Hardcode it here. */
  180. activelow = 0;
  181. switch (i) {
  182. case 0:
  183. behaviour = B43_LED_ACTIVITY;
  184. activelow = 1;
  185. if (bus->boardinfo.vendor == PCI_VENDOR_ID_COMPAQ)
  186. behaviour = B43_LED_RADIO_ALL;
  187. break;
  188. case 1:
  189. behaviour = B43_LED_RADIO_B;
  190. if (bus->boardinfo.vendor == PCI_VENDOR_ID_ASUSTEK)
  191. behaviour = B43_LED_ASSOC;
  192. break;
  193. case 2:
  194. behaviour = B43_LED_RADIO_A;
  195. break;
  196. case 3:
  197. behaviour = B43_LED_OFF;
  198. break;
  199. default:
  200. B43_WARN_ON(1);
  201. return;
  202. }
  203. } else {
  204. behaviour = sprom[i] & B43_LED_BEHAVIOUR;
  205. activelow = !!(sprom[i] & B43_LED_ACTIVELOW);
  206. }
  207. b43_map_led(dev, i, behaviour, activelow);
  208. }
  209. }
  210. void b43_leds_exit(struct b43_wldev *dev)
  211. {
  212. b43_unregister_led(&dev->led_tx);
  213. b43_unregister_led(&dev->led_rx);
  214. b43_unregister_led(&dev->led_assoc);
  215. b43_unregister_led(&dev->led_radio);
  216. }