rt2x00leds.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
  3. <http://rt2x00.serialmonkey.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the
  14. Free Software Foundation, Inc.,
  15. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. /*
  18. Module: rt2x00lib
  19. Abstract: rt2x00 led specific routines.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include "rt2x00.h"
  24. #include "rt2x00lib.h"
  25. void rt2x00leds_led_quality(struct rt2x00_dev *rt2x00dev, int rssi)
  26. {
  27. struct rt2x00_led *led = &rt2x00dev->led_qual;
  28. unsigned int brightness;
  29. if ((led->type != LED_TYPE_QUALITY) || !(led->flags & LED_REGISTERED))
  30. return;
  31. /*
  32. * Led handling requires a positive value for the rssi,
  33. * to do that correctly we need to add the correction.
  34. */
  35. rssi += rt2x00dev->rssi_offset;
  36. /*
  37. * Get the rssi level, this is used to convert the rssi
  38. * to a LED value inside the range LED_OFF - LED_FULL.
  39. */
  40. if (rssi <= 30)
  41. rssi = 0;
  42. else if (rssi <= 39)
  43. rssi = 1;
  44. else if (rssi <= 49)
  45. rssi = 2;
  46. else if (rssi <= 53)
  47. rssi = 3;
  48. else if (rssi <= 63)
  49. rssi = 4;
  50. else
  51. rssi = 5;
  52. /*
  53. * Note that we must _not_ send LED_OFF since the driver
  54. * is going to calculate the value and might use it in a
  55. * division.
  56. */
  57. brightness = ((LED_FULL / 6) * rssi) + 1;
  58. if (brightness != led->led_dev.brightness) {
  59. led->led_dev.brightness_set(&led->led_dev, brightness);
  60. led->led_dev.brightness = brightness;
  61. }
  62. }
  63. static void rt2x00led_led_simple(struct rt2x00_led *led, bool enabled)
  64. {
  65. unsigned int brightness = enabled ? LED_FULL : LED_OFF;
  66. if (!(led->flags & LED_REGISTERED))
  67. return;
  68. led->led_dev.brightness_set(&led->led_dev, brightness);
  69. led->led_dev.brightness = brightness;
  70. }
  71. void rt2x00led_led_activity(struct rt2x00_dev *rt2x00dev, bool enabled)
  72. {
  73. if (rt2x00dev->led_qual.type == LED_TYPE_ACTIVITY)
  74. rt2x00led_led_simple(&rt2x00dev->led_qual, enabled);
  75. }
  76. void rt2x00leds_led_assoc(struct rt2x00_dev *rt2x00dev, bool enabled)
  77. {
  78. if (rt2x00dev->led_assoc.type == LED_TYPE_ASSOC)
  79. rt2x00led_led_simple(&rt2x00dev->led_assoc, enabled);
  80. }
  81. void rt2x00leds_led_radio(struct rt2x00_dev *rt2x00dev, bool enabled)
  82. {
  83. if (rt2x00dev->led_radio.type == LED_TYPE_RADIO)
  84. rt2x00led_led_simple(&rt2x00dev->led_radio, enabled);
  85. }
  86. static int rt2x00leds_register_led(struct rt2x00_dev *rt2x00dev,
  87. struct rt2x00_led *led,
  88. const char *name)
  89. {
  90. struct device *device = wiphy_dev(rt2x00dev->hw->wiphy);
  91. int retval;
  92. led->led_dev.name = name;
  93. led->led_dev.brightness = LED_OFF;
  94. retval = led_classdev_register(device, &led->led_dev);
  95. if (retval) {
  96. ERROR(rt2x00dev, "Failed to register led handler.\n");
  97. return retval;
  98. }
  99. led->flags |= LED_REGISTERED;
  100. return 0;
  101. }
  102. void rt2x00leds_register(struct rt2x00_dev *rt2x00dev)
  103. {
  104. char name[36];
  105. int retval;
  106. unsigned long on_period;
  107. unsigned long off_period;
  108. const char *phy_name = wiphy_name(rt2x00dev->hw->wiphy);
  109. if (rt2x00dev->led_radio.flags & LED_INITIALIZED) {
  110. snprintf(name, sizeof(name), "%s-%s::radio",
  111. rt2x00dev->ops->name, phy_name);
  112. retval = rt2x00leds_register_led(rt2x00dev,
  113. &rt2x00dev->led_radio,
  114. name);
  115. if (retval)
  116. goto exit_fail;
  117. }
  118. if (rt2x00dev->led_assoc.flags & LED_INITIALIZED) {
  119. snprintf(name, sizeof(name), "%s-%s::assoc",
  120. rt2x00dev->ops->name, phy_name);
  121. retval = rt2x00leds_register_led(rt2x00dev,
  122. &rt2x00dev->led_assoc,
  123. name);
  124. if (retval)
  125. goto exit_fail;
  126. }
  127. if (rt2x00dev->led_qual.flags & LED_INITIALIZED) {
  128. snprintf(name, sizeof(name), "%s-%s::quality",
  129. rt2x00dev->ops->name, phy_name);
  130. retval = rt2x00leds_register_led(rt2x00dev,
  131. &rt2x00dev->led_qual,
  132. name);
  133. if (retval)
  134. goto exit_fail;
  135. }
  136. /*
  137. * Initialize blink time to default value:
  138. * On period: 70ms
  139. * Off period: 30ms
  140. */
  141. if (rt2x00dev->led_radio.led_dev.blink_set) {
  142. on_period = 70;
  143. off_period = 30;
  144. rt2x00dev->led_radio.led_dev.blink_set(
  145. &rt2x00dev->led_radio.led_dev, &on_period, &off_period);
  146. }
  147. return;
  148. exit_fail:
  149. rt2x00leds_unregister(rt2x00dev);
  150. }
  151. static void rt2x00leds_unregister_led(struct rt2x00_led *led)
  152. {
  153. led_classdev_unregister(&led->led_dev);
  154. /*
  155. * This might look weird, but when we are unregistering while
  156. * suspended the led is already off, and since we haven't
  157. * fully resumed yet, access to the device might not be
  158. * possible yet.
  159. */
  160. if (!(led->led_dev.flags & LED_SUSPENDED))
  161. led->led_dev.brightness_set(&led->led_dev, LED_OFF);
  162. led->flags &= ~LED_REGISTERED;
  163. }
  164. void rt2x00leds_unregister(struct rt2x00_dev *rt2x00dev)
  165. {
  166. if (rt2x00dev->led_qual.flags & LED_REGISTERED)
  167. rt2x00leds_unregister_led(&rt2x00dev->led_qual);
  168. if (rt2x00dev->led_assoc.flags & LED_REGISTERED)
  169. rt2x00leds_unregister_led(&rt2x00dev->led_assoc);
  170. if (rt2x00dev->led_radio.flags & LED_REGISTERED)
  171. rt2x00leds_unregister_led(&rt2x00dev->led_radio);
  172. }
  173. static inline void rt2x00leds_suspend_led(struct rt2x00_led *led)
  174. {
  175. led_classdev_suspend(&led->led_dev);
  176. /* This shouldn't be needed, but just to be safe */
  177. led->led_dev.brightness_set(&led->led_dev, LED_OFF);
  178. led->led_dev.brightness = LED_OFF;
  179. }
  180. void rt2x00leds_suspend(struct rt2x00_dev *rt2x00dev)
  181. {
  182. if (rt2x00dev->led_qual.flags & LED_REGISTERED)
  183. rt2x00leds_suspend_led(&rt2x00dev->led_qual);
  184. if (rt2x00dev->led_assoc.flags & LED_REGISTERED)
  185. rt2x00leds_suspend_led(&rt2x00dev->led_assoc);
  186. if (rt2x00dev->led_radio.flags & LED_REGISTERED)
  187. rt2x00leds_suspend_led(&rt2x00dev->led_radio);
  188. }
  189. static inline void rt2x00leds_resume_led(struct rt2x00_led *led)
  190. {
  191. led_classdev_resume(&led->led_dev);
  192. /* Device might have enabled the LEDS during resume */
  193. led->led_dev.brightness_set(&led->led_dev, LED_OFF);
  194. led->led_dev.brightness = LED_OFF;
  195. }
  196. void rt2x00leds_resume(struct rt2x00_dev *rt2x00dev)
  197. {
  198. if (rt2x00dev->led_radio.flags & LED_REGISTERED)
  199. rt2x00leds_resume_led(&rt2x00dev->led_radio);
  200. if (rt2x00dev->led_assoc.flags & LED_REGISTERED)
  201. rt2x00leds_resume_led(&rt2x00dev->led_assoc);
  202. if (rt2x00dev->led_qual.flags & LED_REGISTERED)
  203. rt2x00leds_resume_led(&rt2x00dev->led_qual);
  204. }