leds.c 6.9 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 <m@bues.ch>
  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 "b43legacy.h"
  23. #include "leds.h"
  24. #include "rfkill.h"
  25. static void b43legacy_led_turn_on(struct b43legacy_wldev *dev, u8 led_index,
  26. bool activelow)
  27. {
  28. struct b43legacy_wl *wl = dev->wl;
  29. unsigned long flags;
  30. u16 ctl;
  31. spin_lock_irqsave(&wl->leds_lock, flags);
  32. ctl = b43legacy_read16(dev, B43legacy_MMIO_GPIO_CONTROL);
  33. if (activelow)
  34. ctl &= ~(1 << led_index);
  35. else
  36. ctl |= (1 << led_index);
  37. b43legacy_write16(dev, B43legacy_MMIO_GPIO_CONTROL, ctl);
  38. spin_unlock_irqrestore(&wl->leds_lock, flags);
  39. }
  40. static void b43legacy_led_turn_off(struct b43legacy_wldev *dev, u8 led_index,
  41. bool activelow)
  42. {
  43. struct b43legacy_wl *wl = dev->wl;
  44. unsigned long flags;
  45. u16 ctl;
  46. spin_lock_irqsave(&wl->leds_lock, flags);
  47. ctl = b43legacy_read16(dev, B43legacy_MMIO_GPIO_CONTROL);
  48. if (activelow)
  49. ctl |= (1 << led_index);
  50. else
  51. ctl &= ~(1 << led_index);
  52. b43legacy_write16(dev, B43legacy_MMIO_GPIO_CONTROL, ctl);
  53. spin_unlock_irqrestore(&wl->leds_lock, flags);
  54. }
  55. /* Callback from the LED subsystem. */
  56. static void b43legacy_led_brightness_set(struct led_classdev *led_dev,
  57. enum led_brightness brightness)
  58. {
  59. struct b43legacy_led *led = container_of(led_dev, struct b43legacy_led,
  60. led_dev);
  61. struct b43legacy_wldev *dev = led->dev;
  62. bool radio_enabled;
  63. /* Checking the radio-enabled status here is slightly racy,
  64. * but we want to avoid the locking overhead and we don't care
  65. * whether the LED has the wrong state for a second. */
  66. radio_enabled = (dev->phy.radio_on && dev->radio_hw_enable);
  67. if (brightness == LED_OFF || !radio_enabled)
  68. b43legacy_led_turn_off(dev, led->index, led->activelow);
  69. else
  70. b43legacy_led_turn_on(dev, led->index, led->activelow);
  71. }
  72. static int b43legacy_register_led(struct b43legacy_wldev *dev,
  73. struct b43legacy_led *led,
  74. const char *name,
  75. const char *default_trigger,
  76. u8 led_index, bool activelow)
  77. {
  78. int err;
  79. b43legacy_led_turn_off(dev, led_index, activelow);
  80. if (led->dev)
  81. return -EEXIST;
  82. if (!default_trigger)
  83. return -EINVAL;
  84. led->dev = dev;
  85. led->index = led_index;
  86. led->activelow = activelow;
  87. strncpy(led->name, name, sizeof(led->name));
  88. led->led_dev.name = led->name;
  89. led->led_dev.default_trigger = default_trigger;
  90. led->led_dev.brightness_set = b43legacy_led_brightness_set;
  91. err = led_classdev_register(dev->dev->dev, &led->led_dev);
  92. if (err) {
  93. b43legacywarn(dev->wl, "LEDs: Failed to register %s\n", name);
  94. led->dev = NULL;
  95. return err;
  96. }
  97. return 0;
  98. }
  99. static void b43legacy_unregister_led(struct b43legacy_led *led)
  100. {
  101. if (!led->dev)
  102. return;
  103. led_classdev_unregister(&led->led_dev);
  104. b43legacy_led_turn_off(led->dev, led->index, led->activelow);
  105. led->dev = NULL;
  106. }
  107. static void b43legacy_map_led(struct b43legacy_wldev *dev,
  108. u8 led_index,
  109. enum b43legacy_led_behaviour behaviour,
  110. bool activelow)
  111. {
  112. struct ieee80211_hw *hw = dev->wl->hw;
  113. char name[B43legacy_LED_MAX_NAME_LEN + 1];
  114. /* Map the b43 specific LED behaviour value to the
  115. * generic LED triggers. */
  116. switch (behaviour) {
  117. case B43legacy_LED_INACTIVE:
  118. break;
  119. case B43legacy_LED_OFF:
  120. b43legacy_led_turn_off(dev, led_index, activelow);
  121. break;
  122. case B43legacy_LED_ON:
  123. b43legacy_led_turn_on(dev, led_index, activelow);
  124. break;
  125. case B43legacy_LED_ACTIVITY:
  126. case B43legacy_LED_TRANSFER:
  127. case B43legacy_LED_APTRANSFER:
  128. snprintf(name, sizeof(name),
  129. "b43legacy-%s::tx", wiphy_name(hw->wiphy));
  130. b43legacy_register_led(dev, &dev->led_tx, name,
  131. ieee80211_get_tx_led_name(hw),
  132. led_index, activelow);
  133. snprintf(name, sizeof(name),
  134. "b43legacy-%s::rx", wiphy_name(hw->wiphy));
  135. b43legacy_register_led(dev, &dev->led_rx, name,
  136. ieee80211_get_rx_led_name(hw),
  137. led_index, activelow);
  138. break;
  139. case B43legacy_LED_RADIO_ALL:
  140. case B43legacy_LED_RADIO_A:
  141. case B43legacy_LED_RADIO_B:
  142. case B43legacy_LED_MODE_BG:
  143. snprintf(name, sizeof(name),
  144. "b43legacy-%s::radio", wiphy_name(hw->wiphy));
  145. b43legacy_register_led(dev, &dev->led_radio, name,
  146. ieee80211_get_radio_led_name(hw),
  147. led_index, activelow);
  148. /* Sync the RF-kill LED state with radio and switch states. */
  149. if (dev->phy.radio_on && b43legacy_is_hw_radio_enabled(dev))
  150. b43legacy_led_turn_on(dev, led_index, activelow);
  151. break;
  152. case B43legacy_LED_WEIRD:
  153. case B43legacy_LED_ASSOC:
  154. snprintf(name, sizeof(name),
  155. "b43legacy-%s::assoc", wiphy_name(hw->wiphy));
  156. b43legacy_register_led(dev, &dev->led_assoc, name,
  157. ieee80211_get_assoc_led_name(hw),
  158. led_index, activelow);
  159. break;
  160. default:
  161. b43legacywarn(dev->wl, "LEDs: Unknown behaviour 0x%02X\n",
  162. behaviour);
  163. break;
  164. }
  165. }
  166. void b43legacy_leds_init(struct b43legacy_wldev *dev)
  167. {
  168. struct ssb_bus *bus = dev->dev->bus;
  169. u8 sprom[4];
  170. int i;
  171. enum b43legacy_led_behaviour behaviour;
  172. bool activelow;
  173. sprom[0] = bus->sprom.gpio0;
  174. sprom[1] = bus->sprom.gpio1;
  175. sprom[2] = bus->sprom.gpio2;
  176. sprom[3] = bus->sprom.gpio3;
  177. for (i = 0; i < 4; i++) {
  178. if (sprom[i] == 0xFF) {
  179. /* There is no LED information in the SPROM
  180. * for this LED. Hardcode it here. */
  181. activelow = false;
  182. switch (i) {
  183. case 0:
  184. behaviour = B43legacy_LED_ACTIVITY;
  185. activelow = true;
  186. if (bus->boardinfo.vendor == PCI_VENDOR_ID_COMPAQ)
  187. behaviour = B43legacy_LED_RADIO_ALL;
  188. break;
  189. case 1:
  190. behaviour = B43legacy_LED_RADIO_B;
  191. if (bus->boardinfo.vendor == PCI_VENDOR_ID_ASUSTEK)
  192. behaviour = B43legacy_LED_ASSOC;
  193. break;
  194. case 2:
  195. behaviour = B43legacy_LED_RADIO_A;
  196. break;
  197. case 3:
  198. behaviour = B43legacy_LED_OFF;
  199. break;
  200. default:
  201. B43legacy_WARN_ON(1);
  202. return;
  203. }
  204. } else {
  205. behaviour = sprom[i] & B43legacy_LED_BEHAVIOUR;
  206. activelow = !!(sprom[i] & B43legacy_LED_ACTIVELOW);
  207. }
  208. b43legacy_map_led(dev, i, behaviour, activelow);
  209. }
  210. }
  211. void b43legacy_leds_exit(struct b43legacy_wldev *dev)
  212. {
  213. b43legacy_unregister_led(&dev->led_tx);
  214. b43legacy_unregister_led(&dev->led_rx);
  215. b43legacy_unregister_led(&dev->led_assoc);
  216. b43legacy_unregister_led(&dev->led_radio);
  217. }