rt2x00leds.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 dev_name[16];
  105. char name[32];
  106. int retval;
  107. unsigned long on_period;
  108. unsigned long off_period;
  109. snprintf(dev_name, sizeof(dev_name), "%s-%s",
  110. rt2x00dev->ops->name, wiphy_name(rt2x00dev->hw->wiphy));
  111. if (rt2x00dev->led_radio.flags & LED_INITIALIZED) {
  112. snprintf(name, sizeof(name), "%s::radio", dev_name);
  113. retval = rt2x00leds_register_led(rt2x00dev,
  114. &rt2x00dev->led_radio,
  115. name);
  116. if (retval)
  117. goto exit_fail;
  118. }
  119. if (rt2x00dev->led_assoc.flags & LED_INITIALIZED) {
  120. snprintf(name, sizeof(name), "%s::assoc", dev_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::quality", dev_name);
  129. retval = rt2x00leds_register_led(rt2x00dev,
  130. &rt2x00dev->led_qual,
  131. name);
  132. if (retval)
  133. goto exit_fail;
  134. }
  135. /*
  136. * Initialize blink time to default value:
  137. * On period: 70ms
  138. * Off period: 30ms
  139. */
  140. if (rt2x00dev->led_radio.led_dev.blink_set) {
  141. on_period = 70;
  142. off_period = 30;
  143. rt2x00dev->led_radio.led_dev.blink_set(
  144. &rt2x00dev->led_radio.led_dev, &on_period, &off_period);
  145. }
  146. return;
  147. exit_fail:
  148. rt2x00leds_unregister(rt2x00dev);
  149. }
  150. static void rt2x00leds_unregister_led(struct rt2x00_led *led)
  151. {
  152. led_classdev_unregister(&led->led_dev);
  153. /*
  154. * This might look weird, but when we are unregistering while
  155. * suspended the led is already off, and since we haven't
  156. * fully resumed yet, access to the device might not be
  157. * possible yet.
  158. */
  159. if (!(led->led_dev.flags & LED_SUSPENDED))
  160. led->led_dev.brightness_set(&led->led_dev, LED_OFF);
  161. led->flags &= ~LED_REGISTERED;
  162. }
  163. void rt2x00leds_unregister(struct rt2x00_dev *rt2x00dev)
  164. {
  165. if (rt2x00dev->led_qual.flags & LED_REGISTERED)
  166. rt2x00leds_unregister_led(&rt2x00dev->led_qual);
  167. if (rt2x00dev->led_assoc.flags & LED_REGISTERED)
  168. rt2x00leds_unregister_led(&rt2x00dev->led_assoc);
  169. if (rt2x00dev->led_radio.flags & LED_REGISTERED)
  170. rt2x00leds_unregister_led(&rt2x00dev->led_radio);
  171. }
  172. static inline void rt2x00leds_suspend_led(struct rt2x00_led *led)
  173. {
  174. led_classdev_suspend(&led->led_dev);
  175. /* This shouldn't be needed, but just to be safe */
  176. led->led_dev.brightness_set(&led->led_dev, LED_OFF);
  177. led->led_dev.brightness = LED_OFF;
  178. }
  179. void rt2x00leds_suspend(struct rt2x00_dev *rt2x00dev)
  180. {
  181. if (rt2x00dev->led_qual.flags & LED_REGISTERED)
  182. rt2x00leds_suspend_led(&rt2x00dev->led_qual);
  183. if (rt2x00dev->led_assoc.flags & LED_REGISTERED)
  184. rt2x00leds_suspend_led(&rt2x00dev->led_assoc);
  185. if (rt2x00dev->led_radio.flags & LED_REGISTERED)
  186. rt2x00leds_suspend_led(&rt2x00dev->led_radio);
  187. }
  188. static inline void rt2x00leds_resume_led(struct rt2x00_led *led)
  189. {
  190. led_classdev_resume(&led->led_dev);
  191. /* Device might have enabled the LEDS during resume */
  192. led->led_dev.brightness_set(&led->led_dev, LED_OFF);
  193. led->led_dev.brightness = LED_OFF;
  194. }
  195. void rt2x00leds_resume(struct rt2x00_dev *rt2x00dev)
  196. {
  197. if (rt2x00dev->led_radio.flags & LED_REGISTERED)
  198. rt2x00leds_resume_led(&rt2x00dev->led_radio);
  199. if (rt2x00dev->led_assoc.flags & LED_REGISTERED)
  200. rt2x00leds_resume_led(&rt2x00dev->led_assoc);
  201. if (rt2x00dev->led_qual.flags & LED_REGISTERED)
  202. rt2x00leds_resume_led(&rt2x00dev->led_qual);
  203. }