a8a8a4cc7b3e6544895501536ec5ff2565da5e16
[librecmc/librecmc.git] / package / kernel / mac80211 / patches / 530-ath9k_extra_leds.patch
1 --- a/drivers/net/wireless/ath/ath9k/ath9k.h
2 +++ b/drivers/net/wireless/ath/ath9k/ath9k.h
3 @@ -816,6 +816,9 @@ static inline int ath9k_dump_btcoex(stru
4  #ifdef CPTCFG_MAC80211_LEDS
5  void ath_init_leds(struct ath_softc *sc);
6  void ath_deinit_leds(struct ath_softc *sc);
7 +int ath_create_gpio_led(struct ath_softc *sc, int gpio, const char *name,
8 +                       const char *trigger, bool active_low);
9 +
10  #else
11  static inline void ath_init_leds(struct ath_softc *sc)
12  {
13 @@ -952,6 +955,13 @@ void ath_ant_comb_scan(struct ath_softc
14  
15  #define ATH9K_NUM_CHANCTX  2 /* supports 2 operating channels */
16  
17 +struct ath_led {
18 +       struct list_head list;
19 +       struct ath_softc *sc;
20 +       const struct gpio_led *gpio;
21 +       struct led_classdev cdev;
22 +};
23 +
24  struct ath_softc {
25         struct ieee80211_hw *hw;
26         struct device *dev;
27 @@ -1004,9 +1014,8 @@ struct ath_softc {
28         spinlock_t chan_lock;
29  
30  #ifdef CPTCFG_MAC80211_LEDS
31 -       bool led_registered;
32 -       char led_name[32];
33 -       struct led_classdev led_cdev;
34 +       const char *led_default_trigger;
35 +       struct list_head leds;
36  #endif
37  
38  #ifdef CPTCFG_ATH9K_DEBUGFS
39 --- a/drivers/net/wireless/ath/ath9k/gpio.c
40 +++ b/drivers/net/wireless/ath/ath9k/gpio.c
41 @@ -22,7 +22,7 @@
42  
43  #ifdef CPTCFG_MAC80211_LEDS
44  
45 -void ath_fill_led_pin(struct ath_softc *sc)
46 +static void ath_fill_led_pin(struct ath_softc *sc)
47  {
48         struct ath_hw *ah = sc->sc_ah;
49  
50 @@ -39,61 +39,111 @@ void ath_fill_led_pin(struct ath_softc *
51                 else
52                         ah->led_pin = ATH_LED_PIN_DEF;
53         }
54 +}
55 +
56 +static void ath_led_brightness(struct led_classdev *led_cdev,
57 +                              enum led_brightness brightness)
58 +{
59 +       struct ath_led *led = container_of(led_cdev, struct ath_led, cdev);
60 +       struct ath_softc *sc = led->sc;
61 +
62 +       ath9k_ps_wakeup(sc);
63 +       ath9k_hw_set_gpio(sc->sc_ah, led->gpio->gpio,
64 +                         (brightness != LED_OFF) ^ led->gpio->active_low);
65 +       ath9k_ps_restore(sc);
66 +}
67 +
68 +static int ath_add_led(struct ath_softc *sc, struct ath_led *led)
69 +{
70 +       const struct gpio_led *gpio = led->gpio;
71 +       int ret;
72 +
73 +       led->cdev.name = gpio->name;
74 +       led->cdev.default_trigger = gpio->default_trigger;
75 +       led->cdev.brightness_set = ath_led_brightness;
76 +
77 +       ret = led_classdev_register(wiphy_dev(sc->hw->wiphy), &led->cdev);
78 +       if (ret < 0)
79 +               return ret;
80 +
81 +       led->sc = sc;
82 +       list_add(&led->list, &sc->leds);
83  
84         /* Configure gpio for output */
85 -       ath9k_hw_gpio_request_out(ah, ah->led_pin, "ath9k-led",
86 +       ath9k_hw_gpio_request_out(sc->sc_ah, gpio->gpio, gpio->name,
87                                   AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
88  
89 -       /* LED off, active low */
90 -       ath9k_hw_set_gpio(ah, ah->led_pin, ah->config.led_active_high ? 0 : 1);
91 +       /* LED off */
92 +       ath9k_hw_set_gpio(sc->sc_ah, gpio->gpio, gpio->active_low);
93 +
94 +       return 0;
95  }
96  
97 -static void ath_led_brightness(struct led_classdev *led_cdev,
98 -                              enum led_brightness brightness)
99 +int ath_create_gpio_led(struct ath_softc *sc, int gpio_num, const char *name,
100 +                       const char *trigger, bool active_low)
101  {
102 -       struct ath_softc *sc = container_of(led_cdev, struct ath_softc, led_cdev);
103 -       u32 val = (brightness == LED_OFF);
104 +       struct ath_led *led;
105 +       struct gpio_led *gpio;
106 +       char *_name;
107 +       int ret;
108  
109 -       if (sc->sc_ah->config.led_active_high)
110 -               val = !val;
111 +       led = kzalloc(sizeof(*led) + sizeof(*gpio) + strlen(name) + 1,
112 +                     GFP_KERNEL);
113 +       if (!led)
114 +               return -ENOMEM;
115  
116 -       ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, val);
117 +       led->gpio = gpio = (struct gpio_led *) (led + 1);
118 +       _name = (char *) (led->gpio + 1);
119 +
120 +       strcpy(_name, name);
121 +       gpio->name = _name;
122 +       gpio->gpio = gpio_num;
123 +       gpio->active_low = active_low;
124 +       gpio->default_trigger = trigger;
125 +
126 +       ret = ath_add_led(sc, led);
127 +       if (unlikely(ret < 0))
128 +               kfree(led);
129 +
130 +       return ret;
131  }
132  
133  void ath_deinit_leds(struct ath_softc *sc)
134  {
135 -       if (!sc->led_registered)
136 -               return;
137 -
138 -       ath_led_brightness(&sc->led_cdev, LED_OFF);
139 -       led_classdev_unregister(&sc->led_cdev);
140 +       struct ath_led *led;
141  
142 -       ath9k_hw_gpio_free(sc->sc_ah, sc->sc_ah->led_pin);
143 +       while (!list_empty(&sc->leds)) {
144 +               led = list_first_entry(&sc->leds, struct ath_led, list);
145 +               list_del(&led->list);
146 +               ath_led_brightness(&led->cdev, LED_OFF);
147 +               led_classdev_unregister(&led->cdev);
148 +               ath9k_hw_gpio_free(sc->sc_ah, led->gpio->gpio);
149 +               kfree(led);
150 +       }
151  }
152  
153  void ath_init_leds(struct ath_softc *sc)
154  {
155 -       int ret;
156 +       char led_name[32];
157 +       const char *trigger;
158 +
159 +       INIT_LIST_HEAD(&sc->leds);
160  
161         if (AR_SREV_9100(sc->sc_ah))
162                 return;
163  
164         ath_fill_led_pin(sc);
165  
166 -       if (!ath9k_led_blink)
167 -               sc->led_cdev.default_trigger =
168 -                       ieee80211_get_radio_led_name(sc->hw);
169 -
170 -       snprintf(sc->led_name, sizeof(sc->led_name),
171 -               "ath9k-%s", wiphy_name(sc->hw->wiphy));
172 -       sc->led_cdev.name = sc->led_name;
173 -       sc->led_cdev.brightness_set = ath_led_brightness;
174 +       snprintf(led_name, sizeof(led_name), "ath9k-%s",
175 +                wiphy_name(sc->hw->wiphy));
176  
177 -       ret = led_classdev_register(wiphy_dev(sc->hw->wiphy), &sc->led_cdev);
178 -       if (ret < 0)
179 -               return;
180 +       if (ath9k_led_blink)
181 +               trigger = sc->led_default_trigger;
182 +       else
183 +               trigger = ieee80211_get_radio_led_name(sc->hw);
184  
185 -       sc->led_registered = true;
186 +       ath_create_gpio_led(sc, sc->sc_ah->led_pin, led_name, trigger,
187 +                          !sc->sc_ah->config.led_active_high);
188  }
189  #endif
190  
191 --- a/drivers/net/wireless/ath/ath9k/init.c
192 +++ b/drivers/net/wireless/ath/ath9k/init.c
193 @@ -942,7 +942,7 @@ int ath9k_init_device(u16 devid, struct
194  
195  #ifdef CPTCFG_MAC80211_LEDS
196         /* must be initialized before ieee80211_register_hw */
197 -       sc->led_cdev.default_trigger = ieee80211_create_tpt_led_trigger(sc->hw,
198 +       sc->led_default_trigger = ieee80211_create_tpt_led_trigger(sc->hw,
199                 IEEE80211_TPT_LEDTRIG_FL_RADIO, ath9k_tpt_blink,
200                 ARRAY_SIZE(ath9k_tpt_blink));
201  #endif
202 --- a/drivers/net/wireless/ath/ath9k/debug.c
203 +++ b/drivers/net/wireless/ath/ath9k/debug.c
204 @@ -1411,6 +1411,61 @@ static const struct file_operations fops
205         .llseek = default_llseek,
206  };
207  
208 +#ifdef CONFIG_MAC80211_LEDS
209 +
210 +static ssize_t write_file_gpio_led(struct file *file, const char __user *ubuf,
211 +                                  size_t count, loff_t *ppos)
212 +{
213 +       struct ath_softc *sc = file->private_data;
214 +       char buf[32], *str, *name, *c;
215 +       ssize_t len;
216 +       unsigned int gpio;
217 +       bool active_low = false;
218 +
219 +       len = min(count, sizeof(buf) - 1);
220 +       if (copy_from_user(buf, ubuf, len))
221 +               return -EFAULT;
222 +
223 +       buf[len] = '\0';
224 +       name = strchr(buf, ',');
225 +       if (!name)
226 +               return -EINVAL;
227 +
228 +       *(name++) = 0;
229 +       if (!*name)
230 +               return -EINVAL;
231 +
232 +       c = strchr(name, '\n');
233 +       if (c)
234 +               *c = 0;
235 +
236 +       str = buf;
237 +       if (*str == '!') {
238 +               str++;
239 +               active_low = true;
240 +       }
241 +
242 +       if (kstrtouint(str, 0, &gpio) < 0)
243 +               return -EINVAL;
244 +
245 +       if (gpio >= sc->sc_ah->caps.num_gpio_pins)
246 +               return -EINVAL;
247 +
248 +       if (ath_create_gpio_led(sc, gpio, name, NULL, active_low) < 0)
249 +               return -EINVAL;
250 +
251 +       return count;
252 +}
253 +
254 +static const struct file_operations fops_gpio_led = {
255 +       .write = write_file_gpio_led,
256 +       .open = simple_open,
257 +       .owner = THIS_MODULE,
258 +       .llseek = default_llseek,
259 +};
260 +
261 +#endif
262 +
263  
264  int ath9k_init_debug(struct ath_hw *ah)
265  {
266 @@ -1435,6 +1490,10 @@ int ath9k_init_debug(struct ath_hw *ah)
267                             &fops_eeprom);
268         debugfs_create_file("chanbw", S_IRUSR | S_IWUSR, sc->debug.debugfs_phy,
269                             sc, &fops_chanbw);
270 +#ifdef CONFIG_MAC80211_LEDS
271 +       debugfs_create_file("gpio_led", S_IWUSR,
272 +                          sc->debug.debugfs_phy, sc, &fops_gpio_led);
273 +#endif
274         debugfs_create_devm_seqfile(sc->dev, "dma", sc->debug.debugfs_phy,
275                                     read_file_dma);
276         debugfs_create_devm_seqfile(sc->dev, "interrupt", sc->debug.debugfs_phy,