leds.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. Broadcom B43legacy wireless driver
  3. Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
  4. Stefano Brivio <st3@riseup.net>
  5. Michael Buesch <mb@bu3sch.de>
  6. Danny van Dyk <kugelfang@gentoo.org>
  7. Andreas Jaggi <andreas.jaggi@waterwave.ch>
  8. Copyright (c) 2007 Larry Finger <Larry.Finger@lwfinger.net>
  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 "leds.h"
  23. #include "b43legacy.h"
  24. #include "main.h"
  25. static void b43legacy_led_changestate(struct b43legacy_led *led)
  26. {
  27. struct b43legacy_wldev *dev = led->dev;
  28. const int index = led->index;
  29. u16 ledctl;
  30. B43legacy_WARN_ON(!(index >= 0 && index < B43legacy_NR_LEDS));
  31. B43legacy_WARN_ON(!led->blink_interval);
  32. ledctl = b43legacy_read16(dev, B43legacy_MMIO_GPIO_CONTROL);
  33. ledctl ^= (1 << index);
  34. b43legacy_write16(dev, B43legacy_MMIO_GPIO_CONTROL, ledctl);
  35. }
  36. static void b43legacy_led_blink(unsigned long d)
  37. {
  38. struct b43legacy_led *led = (struct b43legacy_led *)d;
  39. struct b43legacy_wldev *dev = led->dev;
  40. unsigned long flags;
  41. spin_lock_irqsave(&dev->wl->leds_lock, flags);
  42. if (led->blink_interval) {
  43. b43legacy_led_changestate(led);
  44. mod_timer(&led->blink_timer, jiffies + led->blink_interval);
  45. }
  46. spin_unlock_irqrestore(&dev->wl->leds_lock, flags);
  47. }
  48. static void b43legacy_led_blink_start(struct b43legacy_led *led,
  49. unsigned long interval)
  50. {
  51. if (led->blink_interval)
  52. return;
  53. led->blink_interval = interval;
  54. b43legacy_led_changestate(led);
  55. led->blink_timer.expires = jiffies + interval;
  56. add_timer(&led->blink_timer);
  57. }
  58. static void b43legacy_led_blink_stop(struct b43legacy_led *led, int sync)
  59. {
  60. struct b43legacy_wldev *dev = led->dev;
  61. const int index = led->index;
  62. u16 ledctl;
  63. if (!led->blink_interval)
  64. return;
  65. if (unlikely(sync))
  66. del_timer_sync(&led->blink_timer);
  67. else
  68. del_timer(&led->blink_timer);
  69. led->blink_interval = 0;
  70. /* Make sure the LED is turned off. */
  71. B43legacy_WARN_ON(!(index >= 0 && index < B43legacy_NR_LEDS));
  72. ledctl = b43legacy_read16(dev, B43legacy_MMIO_GPIO_CONTROL);
  73. if (led->activelow)
  74. ledctl |= (1 << index);
  75. else
  76. ledctl &= ~(1 << index);
  77. b43legacy_write16(dev, B43legacy_MMIO_GPIO_CONTROL, ledctl);
  78. }
  79. static void b43legacy_led_init_hardcoded(struct b43legacy_wldev *dev,
  80. struct b43legacy_led *led,
  81. int led_index)
  82. {
  83. struct ssb_bus *bus = dev->dev->bus;
  84. /* This function is called, if the behaviour (and activelow)
  85. * information for a LED is missing in the SPROM.
  86. * We hardcode the behaviour values for various devices here.
  87. * Note that the B43legacy_LED_TEST_XXX behaviour values can
  88. * be used to figure out which led is mapped to which index.
  89. */
  90. switch (led_index) {
  91. case 0:
  92. led->behaviour = B43legacy_LED_ACTIVITY;
  93. led->activelow = 1;
  94. if (bus->boardinfo.vendor == PCI_VENDOR_ID_COMPAQ)
  95. led->behaviour = B43legacy_LED_RADIO_ALL;
  96. break;
  97. case 1:
  98. led->behaviour = B43legacy_LED_RADIO_B;
  99. if (bus->boardinfo.vendor == PCI_VENDOR_ID_ASUSTEK)
  100. led->behaviour = B43legacy_LED_ASSOC;
  101. break;
  102. case 2:
  103. led->behaviour = B43legacy_LED_RADIO_A;
  104. break;
  105. case 3:
  106. led->behaviour = B43legacy_LED_OFF;
  107. break;
  108. default:
  109. B43legacy_BUG_ON(1);
  110. }
  111. }
  112. int b43legacy_leds_init(struct b43legacy_wldev *dev)
  113. {
  114. struct b43legacy_led *led;
  115. u8 sprom[4];
  116. int i;
  117. sprom[0] = dev->dev->bus->sprom.r1.gpio0;
  118. sprom[1] = dev->dev->bus->sprom.r1.gpio1;
  119. sprom[2] = dev->dev->bus->sprom.r1.gpio2;
  120. sprom[3] = dev->dev->bus->sprom.r1.gpio3;
  121. for (i = 0; i < B43legacy_NR_LEDS; i++) {
  122. led = &(dev->leds[i]);
  123. led->index = i;
  124. led->dev = dev;
  125. setup_timer(&led->blink_timer,
  126. b43legacy_led_blink,
  127. (unsigned long)led);
  128. if (sprom[i] == 0xFF)
  129. b43legacy_led_init_hardcoded(dev, led, i);
  130. else {
  131. led->behaviour = sprom[i] & B43legacy_LED_BEHAVIOUR;
  132. led->activelow = !!(sprom[i] &
  133. B43legacy_LED_ACTIVELOW);
  134. }
  135. }
  136. return 0;
  137. }
  138. void b43legacy_leds_exit(struct b43legacy_wldev *dev)
  139. {
  140. struct b43legacy_led *led;
  141. int i;
  142. for (i = 0; i < B43legacy_NR_LEDS; i++) {
  143. led = &(dev->leds[i]);
  144. b43legacy_led_blink_stop(led, 1);
  145. }
  146. b43legacy_leds_switch_all(dev, 0);
  147. }
  148. void b43legacy_leds_update(struct b43legacy_wldev *dev, int activity)
  149. {
  150. struct b43legacy_led *led;
  151. struct b43legacy_phy *phy = &dev->phy;
  152. const int transferring = (jiffies - dev->stats.last_tx)
  153. < B43legacy_LED_XFER_THRES;
  154. int i;
  155. int turn_on;
  156. unsigned long interval = 0;
  157. u16 ledctl;
  158. unsigned long flags;
  159. bool radio_enabled = (phy->radio_on && dev->radio_hw_enable);
  160. spin_lock_irqsave(&dev->wl->leds_lock, flags);
  161. ledctl = b43legacy_read16(dev, B43legacy_MMIO_GPIO_CONTROL);
  162. for (i = 0; i < B43legacy_NR_LEDS; i++) {
  163. led = &(dev->leds[i]);
  164. turn_on = 0;
  165. switch (led->behaviour) {
  166. case B43legacy_LED_INACTIVE:
  167. continue;
  168. case B43legacy_LED_OFF:
  169. break;
  170. case B43legacy_LED_ON:
  171. turn_on = 1;
  172. break;
  173. case B43legacy_LED_ACTIVITY:
  174. turn_on = activity;
  175. break;
  176. case B43legacy_LED_RADIO_ALL:
  177. turn_on = radio_enabled;
  178. break;
  179. case B43legacy_LED_RADIO_A:
  180. break;
  181. case B43legacy_LED_RADIO_B:
  182. turn_on = radio_enabled;
  183. break;
  184. case B43legacy_LED_MODE_BG:
  185. if (phy->type == B43legacy_PHYTYPE_G && radio_enabled)
  186. turn_on = 1;
  187. break;
  188. case B43legacy_LED_TRANSFER:
  189. if (transferring)
  190. b43legacy_led_blink_start(led,
  191. B43legacy_LEDBLINK_MEDIUM);
  192. else
  193. b43legacy_led_blink_stop(led, 0);
  194. continue;
  195. case B43legacy_LED_APTRANSFER:
  196. if (b43legacy_is_mode(dev->wl,
  197. IEEE80211_IF_TYPE_AP)) {
  198. if (transferring) {
  199. interval = B43legacy_LEDBLINK_FAST;
  200. turn_on = 1;
  201. }
  202. } else {
  203. turn_on = 1;
  204. if (transferring)
  205. interval = B43legacy_LEDBLINK_FAST;
  206. else
  207. turn_on = 0;
  208. }
  209. if (turn_on)
  210. b43legacy_led_blink_start(led, interval);
  211. else
  212. b43legacy_led_blink_stop(led, 0);
  213. continue;
  214. case B43legacy_LED_WEIRD:
  215. break;
  216. case B43legacy_LED_ASSOC:
  217. turn_on = 1;
  218. #ifdef CONFIG_B43LEGACY_DEBUG
  219. case B43legacy_LED_TEST_BLINKSLOW:
  220. b43legacy_led_blink_start(led, B43legacy_LEDBLINK_SLOW);
  221. continue;
  222. case B43legacy_LED_TEST_BLINKMEDIUM:
  223. b43legacy_led_blink_start(led,
  224. B43legacy_LEDBLINK_MEDIUM);
  225. continue;
  226. case B43legacy_LED_TEST_BLINKFAST:
  227. b43legacy_led_blink_start(led, B43legacy_LEDBLINK_FAST);
  228. continue;
  229. #endif /* CONFIG_B43LEGACY_DEBUG */
  230. default:
  231. B43legacy_BUG_ON(1);
  232. };
  233. if (led->activelow)
  234. turn_on = !turn_on;
  235. if (turn_on)
  236. ledctl |= (1 << i);
  237. else
  238. ledctl &= ~(1 << i);
  239. }
  240. b43legacy_write16(dev, B43legacy_MMIO_GPIO_CONTROL, ledctl);
  241. spin_unlock_irqrestore(&dev->wl->leds_lock, flags);
  242. }
  243. void b43legacy_leds_switch_all(struct b43legacy_wldev *dev, int on)
  244. {
  245. struct b43legacy_led *led;
  246. u16 ledctl;
  247. int i;
  248. int bit_on;
  249. unsigned long flags;
  250. spin_lock_irqsave(&dev->wl->leds_lock, flags);
  251. ledctl = b43legacy_read16(dev, B43legacy_MMIO_GPIO_CONTROL);
  252. for (i = 0; i < B43legacy_NR_LEDS; i++) {
  253. led = &(dev->leds[i]);
  254. if (led->behaviour == B43legacy_LED_INACTIVE)
  255. continue;
  256. if (on)
  257. bit_on = led->activelow ? 0 : 1;
  258. else
  259. bit_on = led->activelow ? 1 : 0;
  260. if (bit_on)
  261. ledctl |= (1 << i);
  262. else
  263. ledctl &= ~(1 << i);
  264. }
  265. b43legacy_write16(dev, B43legacy_MMIO_GPIO_CONTROL, ledctl);
  266. spin_unlock_irqrestore(&dev->wl->leds_lock, flags);
  267. }