led.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Common code for mac80211 Prism54 drivers
  3. *
  4. * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
  5. * Copyright (c) 2007-2009, Christian Lamparter <chunkeey@web.de>
  6. * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
  7. *
  8. * Based on:
  9. * - the islsm (softmac prism54) driver, which is:
  10. * Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
  11. * - stlc45xx driver
  12. * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/firmware.h>
  19. #include <linux/etherdevice.h>
  20. #include <net/mac80211.h>
  21. #ifdef CONFIG_P54_LEDS
  22. #include <linux/leds.h>
  23. #endif /* CONFIG_P54_LEDS */
  24. #include "p54.h"
  25. #include "lmac.h"
  26. static void p54_update_leds(struct work_struct *work)
  27. {
  28. struct p54_common *priv = container_of(work, struct p54_common,
  29. led_work.work);
  30. int err, i, tmp, blink_delay = 400;
  31. bool rerun = false;
  32. /* Don't toggle the LED, when the device is down. */
  33. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
  34. return ;
  35. for (i = 0; i < ARRAY_SIZE(priv->leds); i++)
  36. if (priv->leds[i].toggled) {
  37. priv->softled_state |= BIT(i);
  38. tmp = 70 + 200 / (priv->leds[i].toggled);
  39. if (tmp < blink_delay)
  40. blink_delay = tmp;
  41. if (priv->leds[i].led_dev.brightness == LED_OFF)
  42. rerun = true;
  43. priv->leds[i].toggled =
  44. !!priv->leds[i].led_dev.brightness;
  45. } else
  46. priv->softled_state &= ~BIT(i);
  47. err = p54_set_leds(priv);
  48. if (err && net_ratelimit())
  49. wiphy_err(priv->hw->wiphy,
  50. "failed to update LEDs (%d).\n", err);
  51. if (rerun)
  52. ieee80211_queue_delayed_work(priv->hw, &priv->led_work,
  53. msecs_to_jiffies(blink_delay));
  54. }
  55. static void p54_led_brightness_set(struct led_classdev *led_dev,
  56. enum led_brightness brightness)
  57. {
  58. struct p54_led_dev *led = container_of(led_dev, struct p54_led_dev,
  59. led_dev);
  60. struct ieee80211_hw *dev = led->hw_dev;
  61. struct p54_common *priv = dev->priv;
  62. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
  63. return ;
  64. if ((brightness) && (led->registered)) {
  65. led->toggled++;
  66. ieee80211_queue_delayed_work(priv->hw, &priv->led_work, HZ/10);
  67. }
  68. }
  69. static int p54_register_led(struct p54_common *priv,
  70. unsigned int led_index,
  71. char *name, const char *trigger)
  72. {
  73. struct p54_led_dev *led = &priv->leds[led_index];
  74. int err;
  75. if (led->registered)
  76. return -EEXIST;
  77. snprintf(led->name, sizeof(led->name), "p54-%s::%s",
  78. wiphy_name(priv->hw->wiphy), name);
  79. led->hw_dev = priv->hw;
  80. led->index = led_index;
  81. led->led_dev.name = led->name;
  82. led->led_dev.default_trigger = trigger;
  83. led->led_dev.brightness_set = p54_led_brightness_set;
  84. err = led_classdev_register(wiphy_dev(priv->hw->wiphy), &led->led_dev);
  85. if (err)
  86. wiphy_err(priv->hw->wiphy,
  87. "Failed to register %s LED.\n", name);
  88. else
  89. led->registered = 1;
  90. return err;
  91. }
  92. int p54_init_leds(struct p54_common *priv)
  93. {
  94. int err;
  95. /*
  96. * TODO:
  97. * Figure out if the EEPROM contains some hints about the number
  98. * of available/programmable LEDs of the device.
  99. */
  100. INIT_DELAYED_WORK(&priv->led_work, p54_update_leds);
  101. err = p54_register_led(priv, 0, "assoc",
  102. ieee80211_get_assoc_led_name(priv->hw));
  103. if (err)
  104. return err;
  105. err = p54_register_led(priv, 1, "tx",
  106. ieee80211_get_tx_led_name(priv->hw));
  107. if (err)
  108. return err;
  109. err = p54_register_led(priv, 2, "rx",
  110. ieee80211_get_rx_led_name(priv->hw));
  111. if (err)
  112. return err;
  113. err = p54_register_led(priv, 3, "radio",
  114. ieee80211_get_radio_led_name(priv->hw));
  115. if (err)
  116. return err;
  117. err = p54_set_leds(priv);
  118. return err;
  119. }
  120. void p54_unregister_leds(struct p54_common *priv)
  121. {
  122. int i;
  123. for (i = 0; i < ARRAY_SIZE(priv->leds); i++) {
  124. if (priv->leds[i].registered) {
  125. priv->leds[i].registered = false;
  126. priv->leds[i].toggled = 0;
  127. led_classdev_unregister(&priv->leds[i].led_dev);
  128. }
  129. }
  130. cancel_delayed_work_sync(&priv->led_work);
  131. }