2 * Linux LED driver for RTL8187
4 * Copyright 2009 Larry Finger <Larry.Finger@lwfinger.net>
6 * Based on the LED handling in the r8187 driver, which is:
7 * Copyright (c) Realtek Semiconductor Corp. All rights reserved.
9 * Thanks to Realtek for their support!
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.
16 #ifdef CONFIG_RTL8187_LEDS
18 #include <net/mac80211.h>
19 #include <linux/usb.h>
20 #include <linux/eeprom_93cx6.h>
23 #include "rtl8187_leds.h"
25 static void led_turn_on(struct work_struct *work)
27 /* As this routine does read/write operations on the hardware, it must
28 * be run from a work queue.
31 struct rtl8187_priv *priv = container_of(work, struct rtl8187_priv,
33 struct rtl8187_led *led = &priv->led_tx;
35 /* Don't change the LED, when the device is down. */
36 if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
39 /* Skip if the LED is not registered. */
42 mutex_lock(&priv->conf_mutex);
43 switch (led->ledpin) {
45 rtl818x_iowrite8(priv, &priv->map->GPIO0, 0x01);
46 rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, 0x00);
49 reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) & ~(1 << 4);
50 rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
53 reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) & ~(1 << 5);
54 rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
60 mutex_unlock(&priv->conf_mutex);
63 static void led_turn_off(struct work_struct *work)
65 /* As this routine does read/write operations on the hardware, it must
66 * be run from a work queue.
69 struct rtl8187_priv *priv = container_of(work, struct rtl8187_priv,
71 struct rtl8187_led *led = &priv->led_tx;
73 /* Don't change the LED, when the device is down. */
74 if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
77 /* Skip if the LED is not registered. */
80 mutex_lock(&priv->conf_mutex);
81 switch (led->ledpin) {
83 rtl818x_iowrite8(priv, &priv->map->GPIO0, 0x01);
84 rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, 0x01);
87 reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) | (1 << 4);
88 rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
91 reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) | (1 << 5);
92 rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
98 mutex_unlock(&priv->conf_mutex);
101 /* Callback from the LED subsystem. */
102 static void rtl8187_led_brightness_set(struct led_classdev *led_dev,
103 enum led_brightness brightness)
105 struct rtl8187_led *led = container_of(led_dev, struct rtl8187_led,
107 struct ieee80211_hw *hw = led->dev;
108 struct rtl8187_priv *priv;
109 static bool radio_on;
115 if (brightness == LED_FULL) {
116 ieee80211_queue_delayed_work(hw, &priv->led_on, 0);
118 } else if (radio_on) {
120 cancel_delayed_work_sync(&priv->led_on);
121 ieee80211_queue_delayed_work(hw, &priv->led_off, 0);
123 } else if (radio_on) {
124 if (brightness == LED_OFF) {
125 ieee80211_queue_delayed_work(hw, &priv->led_off, 0);
126 /* The LED is off for 1/20 sec - it just blinks. */
127 ieee80211_queue_delayed_work(hw, &priv->led_on,
130 ieee80211_queue_delayed_work(hw, &priv->led_on, 0);
134 static int rtl8187_register_led(struct ieee80211_hw *dev,
135 struct rtl8187_led *led, const char *name,
136 const char *default_trigger, u8 ledpin,
140 struct rtl8187_priv *priv = dev->priv;
144 if (!default_trigger)
147 led->ledpin = ledpin;
148 led->is_radio = is_radio;
149 strncpy(led->name, name, sizeof(led->name));
151 led->led_dev.name = led->name;
152 led->led_dev.default_trigger = default_trigger;
153 led->led_dev.brightness_set = rtl8187_led_brightness_set;
155 err = led_classdev_register(&priv->udev->dev, &led->led_dev);
157 printk(KERN_INFO "LEDs: Failed to register %s\n", name);
164 static void rtl8187_unregister_led(struct rtl8187_led *led)
166 struct ieee80211_hw *hw = led->dev;
167 struct rtl8187_priv *priv = hw->priv;
169 led_classdev_unregister(&led->led_dev);
170 flush_delayed_work(&priv->led_off);
174 void rtl8187_leds_init(struct ieee80211_hw *dev, u16 custid)
176 struct rtl8187_priv *priv = dev->priv;
177 char name[RTL8187_LED_MAX_NAME_LEN + 1];
181 /* According to the vendor driver, the LED operation depends on the
182 * customer ID encoded in the EEPROM
184 printk(KERN_INFO "rtl8187: Customer ID is 0x%02X\n", custid);
186 case EEPROM_CID_RSVD0:
187 case EEPROM_CID_RSVD1:
188 case EEPROM_CID_SERCOMM_PS:
190 case EEPROM_CID_DELL:
191 case EEPROM_CID_TOSHIBA:
192 ledpin = LED_PIN_GPIO0;
194 case EEPROM_CID_ALPHA0:
195 ledpin = LED_PIN_LED0;
201 ledpin = LED_PIN_GPIO0;
204 INIT_DELAYED_WORK(&priv->led_on, led_turn_on);
205 INIT_DELAYED_WORK(&priv->led_off, led_turn_off);
207 snprintf(name, sizeof(name),
208 "rtl8187-%s::radio", wiphy_name(dev->wiphy));
209 err = rtl8187_register_led(dev, &priv->led_radio, name,
210 ieee80211_get_radio_led_name(dev), ledpin, true);
214 snprintf(name, sizeof(name),
215 "rtl8187-%s::tx", wiphy_name(dev->wiphy));
216 err = rtl8187_register_led(dev, &priv->led_tx, name,
217 ieee80211_get_tx_led_name(dev), ledpin, false);
221 snprintf(name, sizeof(name),
222 "rtl8187-%s::rx", wiphy_name(dev->wiphy));
223 err = rtl8187_register_led(dev, &priv->led_rx, name,
224 ieee80211_get_rx_led_name(dev), ledpin, false);
228 /* registration of RX LED failed - unregister */
229 rtl8187_unregister_led(&priv->led_tx);
231 rtl8187_unregister_led(&priv->led_radio);
234 void rtl8187_leds_exit(struct ieee80211_hw *dev)
236 struct rtl8187_priv *priv = dev->priv;
238 rtl8187_unregister_led(&priv->led_radio);
239 rtl8187_unregister_led(&priv->led_rx);
240 rtl8187_unregister_led(&priv->led_tx);
241 cancel_delayed_work_sync(&priv->led_off);
242 cancel_delayed_work_sync(&priv->led_on);
244 #endif /* def CONFIG_RTL8187_LED */