leds.c 6.4 KB

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