led.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
  3. * Copyright (c) 2004-2005 Atheros Communications, Inc.
  4. * Copyright (c) 2007 Jiri Slaby <[email protected]>
  5. * Copyright (c) 2009 Bob Copeland <[email protected]>
  6. *
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer,
  14. * without modification.
  15. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  16. * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
  17. * redistribution must be conditioned upon including a substantially
  18. * similar Disclaimer requirement for further binary redistribution.
  19. * 3. Neither the names of the above-listed copyright holders nor the names
  20. * of any contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * Alternatively, this software may be distributed under the terms of the
  24. * GNU General Public License ("GPL") version 2 as published by the Free
  25. * Software Foundation.
  26. *
  27. * NO WARRANTY
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  30. * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
  31. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  32. * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
  33. * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  34. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  35. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  36. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  37. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  38. * THE POSSIBILITY OF SUCH DAMAGES.
  39. *
  40. */
  41. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  42. #include <linux/pci.h>
  43. #include "ath5k.h"
  44. #define ATH_SDEVICE(subv, subd) \
  45. .vendor = PCI_ANY_ID, .device = PCI_ANY_ID, \
  46. .subvendor = (subv), .subdevice = (subd)
  47. #define ATH_LED(pin, polarity) .driver_data = (((pin) << 8) | (polarity))
  48. #define ATH_PIN(data) ((data) >> 8)
  49. #define ATH_POLARITY(data) ((data) & 0xff)
  50. /* Devices we match on for LED config info (typically laptops) */
  51. static const struct pci_device_id ath5k_led_devices[] = {
  52. /* AR5211 */
  53. { PCI_VDEVICE(ATHEROS, PCI_DEVICE_ID_ATHEROS_AR5211), ATH_LED(0, 0) },
  54. /* HP Compaq nc6xx, nc4000, nx6000 */
  55. { ATH_SDEVICE(PCI_VENDOR_ID_COMPAQ, PCI_ANY_ID), ATH_LED(1, 1) },
  56. /* Acer Aspire One A150 ([email protected]) */
  57. { ATH_SDEVICE(PCI_VENDOR_ID_FOXCONN, 0xe008), ATH_LED(3, 0) },
  58. /* Acer Aspire One AO531h AO751h ([email protected]) */
  59. { ATH_SDEVICE(PCI_VENDOR_ID_FOXCONN, 0xe00d), ATH_LED(3, 0) },
  60. /* Acer Ferrari 5000 ([email protected]) */
  61. { ATH_SDEVICE(PCI_VENDOR_ID_AMBIT, 0x0422), ATH_LED(1, 1) },
  62. /* E-machines E510 ([email protected]) */
  63. { ATH_SDEVICE(PCI_VENDOR_ID_AMBIT, 0x0428), ATH_LED(3, 0) },
  64. /* BenQ Joybook R55v ([email protected]) */
  65. { ATH_SDEVICE(PCI_VENDOR_ID_QMI, 0x0100), ATH_LED(1, 0) },
  66. /* Acer Extensa 5620z ([email protected]) */
  67. { ATH_SDEVICE(PCI_VENDOR_ID_QMI, 0x0105), ATH_LED(3, 0) },
  68. /* Fukato Datacask Jupiter 1014a ([email protected]) */
  69. { ATH_SDEVICE(PCI_VENDOR_ID_AZWAVE, 0x1026), ATH_LED(3, 0) },
  70. /* IBM ThinkPad AR5BXB6 ([email protected]) */
  71. { ATH_SDEVICE(PCI_VENDOR_ID_IBM, 0x058a), ATH_LED(1, 0) },
  72. /* HP Compaq CQ60-206US ([email protected]) */
  73. { ATH_SDEVICE(PCI_VENDOR_ID_HP, 0x0137a), ATH_LED(3, 1) },
  74. /* HP Compaq C700 ([email protected]) */
  75. { ATH_SDEVICE(PCI_VENDOR_ID_HP, 0x0137b), ATH_LED(3, 0) },
  76. /* LiteOn AR5BXB63 ([email protected]) */
  77. { ATH_SDEVICE(PCI_VENDOR_ID_ATHEROS, 0x3067), ATH_LED(3, 0) },
  78. /* IBM-specific AR5212 (all others) */
  79. { PCI_VDEVICE(ATHEROS, PCI_DEVICE_ID_ATHEROS_AR5212_IBM), ATH_LED(0, 0) },
  80. /* Dell Vostro A860 ([email protected]) */
  81. { ATH_SDEVICE(PCI_VENDOR_ID_QMI, 0x0112), ATH_LED(3, 0) },
  82. { }
  83. };
  84. void ath5k_led_enable(struct ath5k_hw *ah)
  85. {
  86. if (IS_ENABLED(CONFIG_MAC80211_LEDS) &&
  87. test_bit(ATH_STAT_LEDSOFT, ah->status)) {
  88. ath5k_hw_set_gpio_output(ah, ah->led_pin);
  89. ath5k_led_off(ah);
  90. }
  91. }
  92. static void ath5k_led_on(struct ath5k_hw *ah)
  93. {
  94. if (!test_bit(ATH_STAT_LEDSOFT, ah->status))
  95. return;
  96. ath5k_hw_set_gpio(ah, ah->led_pin, ah->led_on);
  97. }
  98. void ath5k_led_off(struct ath5k_hw *ah)
  99. {
  100. if (!IS_ENABLED(CONFIG_MAC80211_LEDS) ||
  101. !test_bit(ATH_STAT_LEDSOFT, ah->status))
  102. return;
  103. ath5k_hw_set_gpio(ah, ah->led_pin, !ah->led_on);
  104. }
  105. static void
  106. ath5k_led_brightness_set(struct led_classdev *led_dev,
  107. enum led_brightness brightness)
  108. {
  109. struct ath5k_led *led = container_of(led_dev, struct ath5k_led,
  110. led_dev);
  111. if (brightness == LED_OFF)
  112. ath5k_led_off(led->ah);
  113. else
  114. ath5k_led_on(led->ah);
  115. }
  116. static int
  117. ath5k_register_led(struct ath5k_hw *ah, struct ath5k_led *led,
  118. const char *name, const char *trigger)
  119. {
  120. int err;
  121. led->ah = ah;
  122. strncpy(led->name, name, sizeof(led->name));
  123. led->name[sizeof(led->name)-1] = 0;
  124. led->led_dev.name = led->name;
  125. led->led_dev.default_trigger = trigger;
  126. led->led_dev.brightness_set = ath5k_led_brightness_set;
  127. err = led_classdev_register(ah->dev, &led->led_dev);
  128. if (err) {
  129. ATH5K_WARN(ah, "could not register LED %s\n", name);
  130. led->ah = NULL;
  131. }
  132. return err;
  133. }
  134. static void
  135. ath5k_unregister_led(struct ath5k_led *led)
  136. {
  137. if (!IS_ENABLED(CONFIG_MAC80211_LEDS) || !led->ah)
  138. return;
  139. led_classdev_unregister(&led->led_dev);
  140. ath5k_led_off(led->ah);
  141. led->ah = NULL;
  142. }
  143. void ath5k_unregister_leds(struct ath5k_hw *ah)
  144. {
  145. ath5k_unregister_led(&ah->rx_led);
  146. ath5k_unregister_led(&ah->tx_led);
  147. }
  148. int ath5k_init_leds(struct ath5k_hw *ah)
  149. {
  150. int ret = 0;
  151. struct ieee80211_hw *hw = ah->hw;
  152. #ifndef CONFIG_ATH5K_AHB
  153. struct pci_dev *pdev = ah->pdev;
  154. #endif
  155. char name[ATH5K_LED_MAX_NAME_LEN + 1];
  156. const struct pci_device_id *match;
  157. if (!IS_ENABLED(CONFIG_MAC80211_LEDS) || !ah->pdev)
  158. return 0;
  159. #ifdef CONFIG_ATH5K_AHB
  160. match = NULL;
  161. #else
  162. match = pci_match_id(&ath5k_led_devices[0], pdev);
  163. #endif
  164. if (match) {
  165. __set_bit(ATH_STAT_LEDSOFT, ah->status);
  166. ah->led_pin = ATH_PIN(match->driver_data);
  167. ah->led_on = ATH_POLARITY(match->driver_data);
  168. }
  169. if (!test_bit(ATH_STAT_LEDSOFT, ah->status))
  170. goto out;
  171. ath5k_led_enable(ah);
  172. snprintf(name, sizeof(name), "ath5k-%s::rx", wiphy_name(hw->wiphy));
  173. ret = ath5k_register_led(ah, &ah->rx_led, name,
  174. ieee80211_get_rx_led_name(hw));
  175. if (ret)
  176. goto out;
  177. snprintf(name, sizeof(name), "ath5k-%s::tx", wiphy_name(hw->wiphy));
  178. ret = ath5k_register_led(ah, &ah->tx_led, name,
  179. ieee80211_get_tx_led_name(hw));
  180. out:
  181. return ret;
  182. }