leds.c 6.8 KB

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