rtl8187_leds.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 "rtl8187_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->mode == 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->mode == 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 = hw->priv;
  99. if (brightness == LED_OFF) {
  100. ieee80211_queue_delayed_work(hw, &priv->led_off, 0);
  101. /* The LED is off for 1/20 sec so that it just blinks. */
  102. ieee80211_queue_delayed_work(hw, &priv->led_on, HZ / 20);
  103. } else
  104. ieee80211_queue_delayed_work(hw, &priv->led_on, 0);
  105. }
  106. static int rtl8187_register_led(struct ieee80211_hw *dev,
  107. struct rtl8187_led *led, const char *name,
  108. const char *default_trigger, u8 ledpin)
  109. {
  110. int err;
  111. struct rtl8187_priv *priv = dev->priv;
  112. if (led->dev)
  113. return -EEXIST;
  114. if (!default_trigger)
  115. return -EINVAL;
  116. led->dev = dev;
  117. led->ledpin = ledpin;
  118. strncpy(led->name, name, sizeof(led->name));
  119. led->led_dev.name = led->name;
  120. led->led_dev.default_trigger = default_trigger;
  121. led->led_dev.brightness_set = rtl8187_led_brightness_set;
  122. err = led_classdev_register(&priv->udev->dev, &led->led_dev);
  123. if (err) {
  124. printk(KERN_INFO "LEDs: Failed to register %s\n", name);
  125. led->dev = NULL;
  126. return err;
  127. }
  128. return 0;
  129. }
  130. static void rtl8187_unregister_led(struct rtl8187_led *led)
  131. {
  132. led_classdev_unregister(&led->led_dev);
  133. led->dev = NULL;
  134. }
  135. void rtl8187_leds_init(struct ieee80211_hw *dev, u16 custid)
  136. {
  137. struct rtl8187_priv *priv = dev->priv;
  138. char name[RTL8187_LED_MAX_NAME_LEN + 1];
  139. u8 ledpin;
  140. int err;
  141. /* According to the vendor driver, the LED operation depends on the
  142. * customer ID encoded in the EEPROM
  143. */
  144. printk(KERN_INFO "rtl8187: Customer ID is 0x%02X\n", custid);
  145. switch (custid) {
  146. case EEPROM_CID_RSVD0:
  147. case EEPROM_CID_RSVD1:
  148. case EEPROM_CID_SERCOMM_PS:
  149. case EEPROM_CID_QMI:
  150. case EEPROM_CID_DELL:
  151. case EEPROM_CID_TOSHIBA:
  152. ledpin = LED_PIN_GPIO0;
  153. break;
  154. case EEPROM_CID_ALPHA0:
  155. ledpin = LED_PIN_LED0;
  156. break;
  157. case EEPROM_CID_HW:
  158. ledpin = LED_PIN_HW;
  159. break;
  160. default:
  161. ledpin = LED_PIN_GPIO0;
  162. }
  163. INIT_DELAYED_WORK(&priv->led_on, led_turn_on);
  164. INIT_DELAYED_WORK(&priv->led_off, led_turn_off);
  165. snprintf(name, sizeof(name),
  166. "rtl8187-%s::tx", wiphy_name(dev->wiphy));
  167. err = rtl8187_register_led(dev, &priv->led_tx, name,
  168. ieee80211_get_tx_led_name(dev), ledpin);
  169. if (err)
  170. goto error;
  171. snprintf(name, sizeof(name),
  172. "rtl8187-%s::rx", wiphy_name(dev->wiphy));
  173. err = rtl8187_register_led(dev, &priv->led_rx, name,
  174. ieee80211_get_rx_led_name(dev), ledpin);
  175. if (!err) {
  176. ieee80211_queue_delayed_work(dev, &priv->led_on, 0);
  177. return;
  178. }
  179. /* registration of RX LED failed - unregister TX */
  180. rtl8187_unregister_led(&priv->led_tx);
  181. error:
  182. /* If registration of either failed, cancel delayed work */
  183. cancel_delayed_work_sync(&priv->led_off);
  184. cancel_delayed_work_sync(&priv->led_on);
  185. }
  186. void rtl8187_leds_exit(struct ieee80211_hw *dev)
  187. {
  188. struct rtl8187_priv *priv = dev->priv;
  189. /* turn the LED off before exiting */
  190. ieee80211_queue_delayed_work(dev, &priv->led_off, 0);
  191. rtl8187_unregister_led(&priv->led_rx);
  192. rtl8187_unregister_led(&priv->led_tx);
  193. cancel_delayed_work_sync(&priv->led_off);
  194. cancel_delayed_work_sync(&priv->led_on);
  195. }
  196. #endif /* def CONFIG_RTL8187_LED */