leds.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Linux LED driver for RTL8187
  3. *
  4. * Copyright 2009 Larry Finger <Larry.Finger@lwfinger.net>
  5. *
  6. * Based on the LED handling in the r8187 driver, which is:
  7. * Copyright (c) Realtek Semiconductor Corp. All rights reserved.
  8. *
  9. * Thanks to Realtek for their support!
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #ifdef CONFIG_RTL8187_LEDS
  16. #include <net/mac80211.h>
  17. #include <linux/usb.h>
  18. #include <linux/eeprom_93cx6.h>
  19. #include "rtl8187.h"
  20. #include "leds.h"
  21. static void led_turn_on(struct work_struct *work)
  22. {
  23. /* As this routine does read/write operations on the hardware, it must
  24. * be run from a work queue.
  25. */
  26. u8 reg;
  27. struct rtl8187_priv *priv = container_of(work, struct rtl8187_priv,
  28. led_on.work);
  29. struct rtl8187_led *led = &priv->led_tx;
  30. /* Don't change the LED, when the device is down. */
  31. if (!priv->vif || priv->vif->type == NL80211_IFTYPE_UNSPECIFIED)
  32. return ;
  33. /* Skip if the LED is not registered. */
  34. if (!led->dev)
  35. return;
  36. mutex_lock(&priv->conf_mutex);
  37. switch (led->ledpin) {
  38. case LED_PIN_GPIO0:
  39. rtl818x_iowrite8(priv, &priv->map->GPIO0, 0x01);
  40. rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, 0x00);
  41. break;
  42. case LED_PIN_LED0:
  43. reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) & ~(1 << 4);
  44. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
  45. break;
  46. case LED_PIN_LED1:
  47. reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) & ~(1 << 5);
  48. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
  49. break;
  50. case LED_PIN_HW:
  51. default:
  52. break;
  53. }
  54. mutex_unlock(&priv->conf_mutex);
  55. }
  56. static void led_turn_off(struct work_struct *work)
  57. {
  58. /* As this routine does read/write operations on the hardware, it must
  59. * be run from a work queue.
  60. */
  61. u8 reg;
  62. struct rtl8187_priv *priv = container_of(work, struct rtl8187_priv,
  63. led_off.work);
  64. struct rtl8187_led *led = &priv->led_tx;
  65. /* Don't change the LED, when the device is down. */
  66. if (!priv->vif || priv->vif->type == NL80211_IFTYPE_UNSPECIFIED)
  67. return ;
  68. /* Skip if the LED is not registered. */
  69. if (!led->dev)
  70. return;
  71. mutex_lock(&priv->conf_mutex);
  72. switch (led->ledpin) {
  73. case LED_PIN_GPIO0:
  74. rtl818x_iowrite8(priv, &priv->map->GPIO0, 0x01);
  75. rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, 0x01);
  76. break;
  77. case LED_PIN_LED0:
  78. reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) | (1 << 4);
  79. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
  80. break;
  81. case LED_PIN_LED1:
  82. reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) | (1 << 5);
  83. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
  84. break;
  85. case LED_PIN_HW:
  86. default:
  87. break;
  88. }
  89. mutex_unlock(&priv->conf_mutex);
  90. }
  91. /* Callback from the LED subsystem. */
  92. static void rtl8187_led_brightness_set(struct led_classdev *led_dev,
  93. enum led_brightness brightness)
  94. {
  95. struct rtl8187_led *led = container_of(led_dev, struct rtl8187_led,
  96. led_dev);
  97. struct ieee80211_hw *hw = led->dev;
  98. struct rtl8187_priv *priv;
  99. static bool radio_on;
  100. if (!hw)
  101. return;
  102. priv = hw->priv;
  103. if (led->is_radio) {
  104. if (brightness == LED_FULL) {
  105. ieee80211_queue_delayed_work(hw, &priv->led_on, 0);
  106. radio_on = true;
  107. } else if (radio_on) {
  108. radio_on = false;
  109. cancel_delayed_work(&priv->led_on);
  110. ieee80211_queue_delayed_work(hw, &priv->led_off, 0);
  111. }
  112. } else if (radio_on) {
  113. if (brightness == LED_OFF) {
  114. ieee80211_queue_delayed_work(hw, &priv->led_off, 0);
  115. /* The LED is off for 1/20 sec - it just blinks. */
  116. ieee80211_queue_delayed_work(hw, &priv->led_on,
  117. HZ / 20);
  118. } else
  119. ieee80211_queue_delayed_work(hw, &priv->led_on, 0);
  120. }
  121. }
  122. static int rtl8187_register_led(struct ieee80211_hw *dev,
  123. struct rtl8187_led *led, const char *name,
  124. const char *default_trigger, u8 ledpin,
  125. bool is_radio)
  126. {
  127. int err;
  128. struct rtl8187_priv *priv = dev->priv;
  129. if (led->dev)
  130. return -EEXIST;
  131. if (!default_trigger)
  132. return -EINVAL;
  133. led->dev = dev;
  134. led->ledpin = ledpin;
  135. led->is_radio = is_radio;
  136. strlcpy(led->name, name, sizeof(led->name));
  137. led->led_dev.name = led->name;
  138. led->led_dev.default_trigger = default_trigger;
  139. led->led_dev.brightness_set = rtl8187_led_brightness_set;
  140. err = led_classdev_register(&priv->udev->dev, &led->led_dev);
  141. if (err) {
  142. printk(KERN_INFO "LEDs: Failed to register %s\n", name);
  143. led->dev = NULL;
  144. return err;
  145. }
  146. return 0;
  147. }
  148. static void rtl8187_unregister_led(struct rtl8187_led *led)
  149. {
  150. struct ieee80211_hw *hw = led->dev;
  151. struct rtl8187_priv *priv = hw->priv;
  152. led_classdev_unregister(&led->led_dev);
  153. flush_delayed_work(&priv->led_off);
  154. led->dev = NULL;
  155. }
  156. void rtl8187_leds_init(struct ieee80211_hw *dev, u16 custid)
  157. {
  158. struct rtl8187_priv *priv = dev->priv;
  159. char name[RTL8187_LED_MAX_NAME_LEN + 1];
  160. u8 ledpin;
  161. int err;
  162. /* According to the vendor driver, the LED operation depends on the
  163. * customer ID encoded in the EEPROM
  164. */
  165. printk(KERN_INFO "rtl8187: Customer ID is 0x%02X\n", custid);
  166. switch (custid) {
  167. case EEPROM_CID_RSVD0:
  168. case EEPROM_CID_RSVD1:
  169. case EEPROM_CID_SERCOMM_PS:
  170. case EEPROM_CID_QMI:
  171. case EEPROM_CID_DELL:
  172. case EEPROM_CID_TOSHIBA:
  173. ledpin = LED_PIN_GPIO0;
  174. break;
  175. case EEPROM_CID_ALPHA0:
  176. ledpin = LED_PIN_LED0;
  177. break;
  178. case EEPROM_CID_HW:
  179. ledpin = LED_PIN_HW;
  180. break;
  181. default:
  182. ledpin = LED_PIN_GPIO0;
  183. }
  184. INIT_DELAYED_WORK(&priv->led_on, led_turn_on);
  185. INIT_DELAYED_WORK(&priv->led_off, led_turn_off);
  186. snprintf(name, sizeof(name),
  187. "rtl8187-%s::radio", wiphy_name(dev->wiphy));
  188. err = rtl8187_register_led(dev, &priv->led_radio, name,
  189. ieee80211_get_radio_led_name(dev), ledpin, true);
  190. if (err)
  191. return;
  192. snprintf(name, sizeof(name),
  193. "rtl8187-%s::tx", wiphy_name(dev->wiphy));
  194. err = rtl8187_register_led(dev, &priv->led_tx, name,
  195. ieee80211_get_tx_led_name(dev), ledpin, false);
  196. if (err)
  197. goto err_tx;
  198. snprintf(name, sizeof(name),
  199. "rtl8187-%s::rx", wiphy_name(dev->wiphy));
  200. err = rtl8187_register_led(dev, &priv->led_rx, name,
  201. ieee80211_get_rx_led_name(dev), ledpin, false);
  202. if (!err)
  203. return;
  204. /* registration of RX LED failed - unregister */
  205. rtl8187_unregister_led(&priv->led_tx);
  206. err_tx:
  207. rtl8187_unregister_led(&priv->led_radio);
  208. }
  209. void rtl8187_leds_exit(struct ieee80211_hw *dev)
  210. {
  211. struct rtl8187_priv *priv = dev->priv;
  212. rtl8187_unregister_led(&priv->led_radio);
  213. rtl8187_unregister_led(&priv->led_rx);
  214. rtl8187_unregister_led(&priv->led_tx);
  215. cancel_delayed_work_sync(&priv->led_off);
  216. cancel_delayed_work_sync(&priv->led_on);
  217. }
  218. #endif /* def CONFIG_RTL8187_LEDS */