leds.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 "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. u16 ctl;
  29. ctl = b43_read16(dev, B43_MMIO_GPIO_CONTROL);
  30. if (activelow)
  31. ctl &= ~(1 << led_index);
  32. else
  33. ctl |= (1 << led_index);
  34. b43_write16(dev, B43_MMIO_GPIO_CONTROL, ctl);
  35. }
  36. static void b43_led_turn_off(struct b43_wldev *dev, u8 led_index,
  37. bool activelow)
  38. {
  39. u16 ctl;
  40. ctl = b43_read16(dev, B43_MMIO_GPIO_CONTROL);
  41. if (activelow)
  42. ctl |= (1 << led_index);
  43. else
  44. ctl &= ~(1 << led_index);
  45. b43_write16(dev, B43_MMIO_GPIO_CONTROL, ctl);
  46. }
  47. static void b43_led_update(struct b43_wldev *dev,
  48. struct b43_led *led)
  49. {
  50. bool radio_enabled;
  51. bool turn_on;
  52. if (!led->wl)
  53. return;
  54. radio_enabled = (dev->phy.radio_on && dev->radio_hw_enable);
  55. /* The led->state read is racy, but we don't care. In case we raced
  56. * with the brightness_set handler, we will be called again soon
  57. * to fixup our state. */
  58. if (radio_enabled)
  59. turn_on = atomic_read(&led->state) != LED_OFF;
  60. else
  61. turn_on = false;
  62. if (turn_on == led->hw_state)
  63. return;
  64. led->hw_state = turn_on;
  65. if (turn_on)
  66. b43_led_turn_on(dev, led->index, led->activelow);
  67. else
  68. b43_led_turn_off(dev, led->index, led->activelow);
  69. }
  70. static void b43_leds_work(struct work_struct *work)
  71. {
  72. struct b43_leds *leds = container_of(work, struct b43_leds, work);
  73. struct b43_wl *wl = container_of(leds, struct b43_wl, leds);
  74. struct b43_wldev *dev;
  75. mutex_lock(&wl->mutex);
  76. dev = wl->current_dev;
  77. if (unlikely(!dev || b43_status(dev) < B43_STAT_STARTED))
  78. goto out_unlock;
  79. b43_led_update(dev, &wl->leds.led_tx);
  80. b43_led_update(dev, &wl->leds.led_rx);
  81. b43_led_update(dev, &wl->leds.led_radio);
  82. b43_led_update(dev, &wl->leds.led_assoc);
  83. out_unlock:
  84. mutex_unlock(&wl->mutex);
  85. }
  86. /* Callback from the LED subsystem. */
  87. static void b43_led_brightness_set(struct led_classdev *led_dev,
  88. enum led_brightness brightness)
  89. {
  90. struct b43_led *led = container_of(led_dev, struct b43_led, led_dev);
  91. struct b43_wl *wl = led->wl;
  92. if (likely(!wl->leds.stop)) {
  93. atomic_set(&led->state, brightness);
  94. ieee80211_queue_work(wl->hw, &wl->leds.work);
  95. }
  96. }
  97. static int b43_register_led(struct b43_wldev *dev, struct b43_led *led,
  98. const char *name, const char *default_trigger,
  99. u8 led_index, bool activelow)
  100. {
  101. int err;
  102. if (led->wl)
  103. return -EEXIST;
  104. if (!default_trigger)
  105. return -EINVAL;
  106. led->wl = dev->wl;
  107. led->index = led_index;
  108. led->activelow = activelow;
  109. strlcpy(led->name, name, sizeof(led->name));
  110. atomic_set(&led->state, 0);
  111. led->led_dev.name = led->name;
  112. led->led_dev.default_trigger = default_trigger;
  113. led->led_dev.brightness_set = b43_led_brightness_set;
  114. err = led_classdev_register(dev->dev->dev, &led->led_dev);
  115. if (err) {
  116. b43warn(dev->wl, "LEDs: Failed to register %s\n", name);
  117. led->wl = NULL;
  118. return err;
  119. }
  120. return 0;
  121. }
  122. static void b43_unregister_led(struct b43_led *led)
  123. {
  124. if (!led->wl)
  125. return;
  126. led_classdev_unregister(&led->led_dev);
  127. led->wl = NULL;
  128. }
  129. static void b43_map_led(struct b43_wldev *dev,
  130. u8 led_index,
  131. enum b43_led_behaviour behaviour,
  132. bool activelow)
  133. {
  134. struct ieee80211_hw *hw = dev->wl->hw;
  135. char name[B43_LED_MAX_NAME_LEN + 1];
  136. /* Map the b43 specific LED behaviour value to the
  137. * generic LED triggers. */
  138. switch (behaviour) {
  139. case B43_LED_INACTIVE:
  140. case B43_LED_OFF:
  141. case B43_LED_ON:
  142. break;
  143. case B43_LED_ACTIVITY:
  144. case B43_LED_TRANSFER:
  145. case B43_LED_APTRANSFER:
  146. snprintf(name, sizeof(name),
  147. "b43-%s::tx", wiphy_name(hw->wiphy));
  148. b43_register_led(dev, &dev->wl->leds.led_tx, name,
  149. ieee80211_get_tx_led_name(hw),
  150. led_index, activelow);
  151. snprintf(name, sizeof(name),
  152. "b43-%s::rx", wiphy_name(hw->wiphy));
  153. b43_register_led(dev, &dev->wl->leds.led_rx, name,
  154. ieee80211_get_rx_led_name(hw),
  155. led_index, activelow);
  156. break;
  157. case B43_LED_RADIO_ALL:
  158. case B43_LED_RADIO_A:
  159. case B43_LED_RADIO_B:
  160. case B43_LED_MODE_BG:
  161. snprintf(name, sizeof(name),
  162. "b43-%s::radio", wiphy_name(hw->wiphy));
  163. b43_register_led(dev, &dev->wl->leds.led_radio, name,
  164. ieee80211_get_radio_led_name(hw),
  165. led_index, activelow);
  166. break;
  167. case B43_LED_WEIRD:
  168. case B43_LED_ASSOC:
  169. snprintf(name, sizeof(name),
  170. "b43-%s::assoc", wiphy_name(hw->wiphy));
  171. b43_register_led(dev, &dev->wl->leds.led_assoc, name,
  172. ieee80211_get_assoc_led_name(hw),
  173. led_index, activelow);
  174. break;
  175. default:
  176. b43warn(dev->wl, "LEDs: Unknown behaviour 0x%02X\n",
  177. behaviour);
  178. break;
  179. }
  180. }
  181. static void b43_led_get_sprominfo(struct b43_wldev *dev,
  182. unsigned int led_index,
  183. enum b43_led_behaviour *behaviour,
  184. bool *activelow)
  185. {
  186. u8 sprom[4];
  187. sprom[0] = dev->dev->bus_sprom->gpio0;
  188. sprom[1] = dev->dev->bus_sprom->gpio1;
  189. sprom[2] = dev->dev->bus_sprom->gpio2;
  190. sprom[3] = dev->dev->bus_sprom->gpio3;
  191. if ((sprom[0] & sprom[1] & sprom[2] & sprom[3]) == 0xff) {
  192. /* There is no LED information in the SPROM
  193. * for this LED. Hardcode it here. */
  194. *activelow = false;
  195. switch (led_index) {
  196. case 0:
  197. *behaviour = B43_LED_ACTIVITY;
  198. *activelow = true;
  199. if (dev->dev->board_vendor == PCI_VENDOR_ID_COMPAQ)
  200. *behaviour = B43_LED_RADIO_ALL;
  201. break;
  202. case 1:
  203. *behaviour = B43_LED_RADIO_B;
  204. if (dev->dev->board_vendor == PCI_VENDOR_ID_ASUSTEK)
  205. *behaviour = B43_LED_ASSOC;
  206. break;
  207. case 2:
  208. *behaviour = B43_LED_RADIO_A;
  209. break;
  210. case 3:
  211. *behaviour = B43_LED_OFF;
  212. break;
  213. default:
  214. *behaviour = B43_LED_OFF;
  215. B43_WARN_ON(1);
  216. return;
  217. }
  218. } else {
  219. /* keep LED disabled if no mapping is defined */
  220. if (sprom[led_index] == 0xff)
  221. *behaviour = B43_LED_OFF;
  222. else
  223. *behaviour = sprom[led_index] & B43_LED_BEHAVIOUR;
  224. *activelow = !!(sprom[led_index] & B43_LED_ACTIVELOW);
  225. }
  226. }
  227. void b43_leds_init(struct b43_wldev *dev)
  228. {
  229. struct b43_led *led;
  230. unsigned int i;
  231. enum b43_led_behaviour behaviour;
  232. bool activelow;
  233. /* Sync the RF-kill LED state (if we have one) with radio and switch states. */
  234. led = &dev->wl->leds.led_radio;
  235. if (led->wl) {
  236. if (dev->phy.radio_on && b43_is_hw_radio_enabled(dev)) {
  237. b43_led_turn_on(dev, led->index, led->activelow);
  238. led->hw_state = true;
  239. atomic_set(&led->state, 1);
  240. } else {
  241. b43_led_turn_off(dev, led->index, led->activelow);
  242. led->hw_state = false;
  243. atomic_set(&led->state, 0);
  244. }
  245. }
  246. /* Initialize TX/RX/ASSOC leds */
  247. led = &dev->wl->leds.led_tx;
  248. if (led->wl) {
  249. b43_led_turn_off(dev, led->index, led->activelow);
  250. led->hw_state = false;
  251. atomic_set(&led->state, 0);
  252. }
  253. led = &dev->wl->leds.led_rx;
  254. if (led->wl) {
  255. b43_led_turn_off(dev, led->index, led->activelow);
  256. led->hw_state = false;
  257. atomic_set(&led->state, 0);
  258. }
  259. led = &dev->wl->leds.led_assoc;
  260. if (led->wl) {
  261. b43_led_turn_off(dev, led->index, led->activelow);
  262. led->hw_state = false;
  263. atomic_set(&led->state, 0);
  264. }
  265. /* Initialize other LED states. */
  266. for (i = 0; i < B43_MAX_NR_LEDS; i++) {
  267. b43_led_get_sprominfo(dev, i, &behaviour, &activelow);
  268. switch (behaviour) {
  269. case B43_LED_OFF:
  270. b43_led_turn_off(dev, i, activelow);
  271. break;
  272. case B43_LED_ON:
  273. b43_led_turn_on(dev, i, activelow);
  274. break;
  275. default:
  276. /* Leave others as-is. */
  277. break;
  278. }
  279. }
  280. dev->wl->leds.stop = 0;
  281. }
  282. void b43_leds_exit(struct b43_wldev *dev)
  283. {
  284. struct b43_leds *leds = &dev->wl->leds;
  285. b43_led_turn_off(dev, leds->led_tx.index, leds->led_tx.activelow);
  286. b43_led_turn_off(dev, leds->led_rx.index, leds->led_rx.activelow);
  287. b43_led_turn_off(dev, leds->led_assoc.index, leds->led_assoc.activelow);
  288. b43_led_turn_off(dev, leds->led_radio.index, leds->led_radio.activelow);
  289. }
  290. void b43_leds_stop(struct b43_wldev *dev)
  291. {
  292. struct b43_leds *leds = &dev->wl->leds;
  293. leds->stop = 1;
  294. cancel_work_sync(&leds->work);
  295. }
  296. void b43_leds_register(struct b43_wldev *dev)
  297. {
  298. unsigned int i;
  299. enum b43_led_behaviour behaviour;
  300. bool activelow;
  301. INIT_WORK(&dev->wl->leds.work, b43_leds_work);
  302. /* Register the LEDs to the LED subsystem. */
  303. for (i = 0; i < B43_MAX_NR_LEDS; i++) {
  304. b43_led_get_sprominfo(dev, i, &behaviour, &activelow);
  305. b43_map_led(dev, i, behaviour, activelow);
  306. }
  307. }
  308. void b43_leds_unregister(struct b43_wl *wl)
  309. {
  310. struct b43_leds *leds = &wl->leds;
  311. b43_unregister_led(&leds->led_tx);
  312. b43_unregister_led(&leds->led_rx);
  313. b43_unregister_led(&leds->led_assoc);
  314. b43_unregister_led(&leds->led_radio);
  315. }