ath9k_htc: Fix AMPDU subframe handling
[linux-2.6.git] / drivers / net / wireless / ath / ath9k / htc_drv_gpio.c
blobd051a4263e0c7c24493503d2cc2111c6e6319574
1 /*
2 * Copyright (c) 2010 Atheros Communications Inc.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "htc.h"
19 /******************/
20 /* BTCOEX */
21 /******************/
24 * Detects if there is any priority bt traffic
26 static void ath_detect_bt_priority(struct ath9k_htc_priv *priv)
28 struct ath_btcoex *btcoex = &priv->btcoex;
29 struct ath_hw *ah = priv->ah;
31 if (ath9k_hw_gpio_get(ah, ah->btcoex_hw.btpriority_gpio))
32 btcoex->bt_priority_cnt++;
34 if (time_after(jiffies, btcoex->bt_priority_time +
35 msecs_to_jiffies(ATH_BT_PRIORITY_TIME_THRESHOLD))) {
36 priv->op_flags &= ~(OP_BT_PRIORITY_DETECTED | OP_BT_SCAN);
37 /* Detect if colocated bt started scanning */
38 if (btcoex->bt_priority_cnt >= ATH_BT_CNT_SCAN_THRESHOLD) {
39 ath_dbg(ath9k_hw_common(ah), ATH_DBG_BTCOEX,
40 "BT scan detected\n");
41 priv->op_flags |= (OP_BT_SCAN |
42 OP_BT_PRIORITY_DETECTED);
43 } else if (btcoex->bt_priority_cnt >= ATH_BT_CNT_THRESHOLD) {
44 ath_dbg(ath9k_hw_common(ah), ATH_DBG_BTCOEX,
45 "BT priority traffic detected\n");
46 priv->op_flags |= OP_BT_PRIORITY_DETECTED;
49 btcoex->bt_priority_cnt = 0;
50 btcoex->bt_priority_time = jiffies;
55 * This is the master bt coex work which runs for every
56 * 45ms, bt traffic will be given priority during 55% of this
57 * period while wlan gets remaining 45%
59 static void ath_btcoex_period_work(struct work_struct *work)
61 struct ath9k_htc_priv *priv = container_of(work, struct ath9k_htc_priv,
62 coex_period_work.work);
63 struct ath_btcoex *btcoex = &priv->btcoex;
64 struct ath_common *common = ath9k_hw_common(priv->ah);
65 u32 timer_period;
66 bool is_btscan;
67 int ret;
69 ath_detect_bt_priority(priv);
71 is_btscan = !!(priv->op_flags & OP_BT_SCAN);
73 ret = ath9k_htc_update_cap_target(priv,
74 !!(priv->op_flags & OP_BT_PRIORITY_DETECTED));
75 if (ret) {
76 ath_err(common, "Unable to set BTCOEX parameters\n");
77 return;
80 ath9k_cmn_btcoex_bt_stomp(common, is_btscan ? ATH_BTCOEX_STOMP_ALL :
81 btcoex->bt_stomp_type);
83 timer_period = is_btscan ? btcoex->btscan_no_stomp :
84 btcoex->btcoex_no_stomp;
85 ieee80211_queue_delayed_work(priv->hw, &priv->duty_cycle_work,
86 msecs_to_jiffies(timer_period));
87 ieee80211_queue_delayed_work(priv->hw, &priv->coex_period_work,
88 msecs_to_jiffies(btcoex->btcoex_period));
92 * Work to time slice between wlan and bt traffic and
93 * configure weight registers
95 static void ath_btcoex_duty_cycle_work(struct work_struct *work)
97 struct ath9k_htc_priv *priv = container_of(work, struct ath9k_htc_priv,
98 duty_cycle_work.work);
99 struct ath_hw *ah = priv->ah;
100 struct ath_btcoex *btcoex = &priv->btcoex;
101 struct ath_common *common = ath9k_hw_common(ah);
102 bool is_btscan = priv->op_flags & OP_BT_SCAN;
104 ath_dbg(common, ATH_DBG_BTCOEX,
105 "time slice work for bt and wlan\n");
107 if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_LOW || is_btscan)
108 ath9k_cmn_btcoex_bt_stomp(common, ATH_BTCOEX_STOMP_NONE);
109 else if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_ALL)
110 ath9k_cmn_btcoex_bt_stomp(common, ATH_BTCOEX_STOMP_LOW);
113 void ath_htc_init_btcoex_work(struct ath9k_htc_priv *priv)
115 struct ath_btcoex *btcoex = &priv->btcoex;
117 btcoex->btcoex_period = ATH_BTCOEX_DEF_BT_PERIOD;
118 btcoex->btcoex_no_stomp = (100 - ATH_BTCOEX_DEF_DUTY_CYCLE) *
119 btcoex->btcoex_period / 100;
120 btcoex->btscan_no_stomp = (100 - ATH_BTCOEX_BTSCAN_DUTY_CYCLE) *
121 btcoex->btcoex_period / 100;
122 INIT_DELAYED_WORK(&priv->coex_period_work, ath_btcoex_period_work);
123 INIT_DELAYED_WORK(&priv->duty_cycle_work, ath_btcoex_duty_cycle_work);
127 * (Re)start btcoex work
130 void ath_htc_resume_btcoex_work(struct ath9k_htc_priv *priv)
132 struct ath_btcoex *btcoex = &priv->btcoex;
133 struct ath_hw *ah = priv->ah;
135 ath_dbg(ath9k_hw_common(ah), ATH_DBG_BTCOEX, "Starting btcoex work\n");
137 btcoex->bt_priority_cnt = 0;
138 btcoex->bt_priority_time = jiffies;
139 priv->op_flags &= ~(OP_BT_PRIORITY_DETECTED | OP_BT_SCAN);
140 ieee80211_queue_delayed_work(priv->hw, &priv->coex_period_work, 0);
145 * Cancel btcoex and bt duty cycle work.
147 void ath_htc_cancel_btcoex_work(struct ath9k_htc_priv *priv)
149 cancel_delayed_work_sync(&priv->coex_period_work);
150 cancel_delayed_work_sync(&priv->duty_cycle_work);
153 /*******/
154 /* LED */
155 /*******/
157 static void ath9k_led_blink_work(struct work_struct *work)
159 struct ath9k_htc_priv *priv = container_of(work, struct ath9k_htc_priv,
160 ath9k_led_blink_work.work);
162 if (!(priv->op_flags & OP_LED_ASSOCIATED))
163 return;
165 if ((priv->led_on_duration == ATH_LED_ON_DURATION_IDLE) ||
166 (priv->led_off_duration == ATH_LED_OFF_DURATION_IDLE))
167 ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin, 0);
168 else
169 ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin,
170 (priv->op_flags & OP_LED_ON) ? 1 : 0);
172 ieee80211_queue_delayed_work(priv->hw,
173 &priv->ath9k_led_blink_work,
174 (priv->op_flags & OP_LED_ON) ?
175 msecs_to_jiffies(priv->led_off_duration) :
176 msecs_to_jiffies(priv->led_on_duration));
178 priv->led_on_duration = priv->led_on_cnt ?
179 max((ATH_LED_ON_DURATION_IDLE - priv->led_on_cnt), 25) :
180 ATH_LED_ON_DURATION_IDLE;
181 priv->led_off_duration = priv->led_off_cnt ?
182 max((ATH_LED_OFF_DURATION_IDLE - priv->led_off_cnt), 10) :
183 ATH_LED_OFF_DURATION_IDLE;
184 priv->led_on_cnt = priv->led_off_cnt = 0;
186 if (priv->op_flags & OP_LED_ON)
187 priv->op_flags &= ~OP_LED_ON;
188 else
189 priv->op_flags |= OP_LED_ON;
192 static void ath9k_led_brightness_work(struct work_struct *work)
194 struct ath_led *led = container_of(work, struct ath_led,
195 brightness_work.work);
196 struct ath9k_htc_priv *priv = led->priv;
198 switch (led->brightness) {
199 case LED_OFF:
200 if (led->led_type == ATH_LED_ASSOC ||
201 led->led_type == ATH_LED_RADIO) {
202 ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin,
203 (led->led_type == ATH_LED_RADIO));
204 priv->op_flags &= ~OP_LED_ASSOCIATED;
205 if (led->led_type == ATH_LED_RADIO)
206 priv->op_flags &= ~OP_LED_ON;
207 } else {
208 priv->led_off_cnt++;
210 break;
211 case LED_FULL:
212 if (led->led_type == ATH_LED_ASSOC) {
213 priv->op_flags |= OP_LED_ASSOCIATED;
214 ieee80211_queue_delayed_work(priv->hw,
215 &priv->ath9k_led_blink_work, 0);
216 } else if (led->led_type == ATH_LED_RADIO) {
217 ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin, 0);
218 priv->op_flags |= OP_LED_ON;
219 } else {
220 priv->led_on_cnt++;
222 break;
223 default:
224 break;
228 static void ath9k_led_brightness(struct led_classdev *led_cdev,
229 enum led_brightness brightness)
231 struct ath_led *led = container_of(led_cdev, struct ath_led, led_cdev);
232 struct ath9k_htc_priv *priv = led->priv;
234 led->brightness = brightness;
235 if (!(priv->op_flags & OP_LED_DEINIT))
236 ieee80211_queue_delayed_work(priv->hw,
237 &led->brightness_work, 0);
240 void ath9k_led_stop_brightness(struct ath9k_htc_priv *priv)
242 cancel_delayed_work_sync(&priv->radio_led.brightness_work);
243 cancel_delayed_work_sync(&priv->assoc_led.brightness_work);
244 cancel_delayed_work_sync(&priv->tx_led.brightness_work);
245 cancel_delayed_work_sync(&priv->rx_led.brightness_work);
248 static int ath9k_register_led(struct ath9k_htc_priv *priv, struct ath_led *led,
249 char *trigger)
251 int ret;
253 led->priv = priv;
254 led->led_cdev.name = led->name;
255 led->led_cdev.default_trigger = trigger;
256 led->led_cdev.brightness_set = ath9k_led_brightness;
258 ret = led_classdev_register(wiphy_dev(priv->hw->wiphy), &led->led_cdev);
259 if (ret)
260 ath_err(ath9k_hw_common(priv->ah),
261 "Failed to register led:%s", led->name);
262 else
263 led->registered = 1;
265 INIT_DELAYED_WORK(&led->brightness_work, ath9k_led_brightness_work);
267 return ret;
270 static void ath9k_unregister_led(struct ath_led *led)
272 if (led->registered) {
273 led_classdev_unregister(&led->led_cdev);
274 led->registered = 0;
278 void ath9k_deinit_leds(struct ath9k_htc_priv *priv)
280 priv->op_flags |= OP_LED_DEINIT;
281 ath9k_unregister_led(&priv->assoc_led);
282 priv->op_flags &= ~OP_LED_ASSOCIATED;
283 ath9k_unregister_led(&priv->tx_led);
284 ath9k_unregister_led(&priv->rx_led);
285 ath9k_unregister_led(&priv->radio_led);
288 void ath9k_init_leds(struct ath9k_htc_priv *priv)
290 char *trigger;
291 int ret;
293 if (AR_SREV_9287(priv->ah))
294 priv->ah->led_pin = ATH_LED_PIN_9287;
295 else if (AR_SREV_9271(priv->ah))
296 priv->ah->led_pin = ATH_LED_PIN_9271;
297 else if (AR_DEVID_7010(priv->ah))
298 priv->ah->led_pin = ATH_LED_PIN_7010;
299 else
300 priv->ah->led_pin = ATH_LED_PIN_DEF;
302 /* Configure gpio 1 for output */
303 ath9k_hw_cfg_output(priv->ah, priv->ah->led_pin,
304 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
305 /* LED off, active low */
306 ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin, 1);
308 INIT_DELAYED_WORK(&priv->ath9k_led_blink_work, ath9k_led_blink_work);
310 trigger = ieee80211_get_radio_led_name(priv->hw);
311 snprintf(priv->radio_led.name, sizeof(priv->radio_led.name),
312 "ath9k-%s::radio", wiphy_name(priv->hw->wiphy));
313 ret = ath9k_register_led(priv, &priv->radio_led, trigger);
314 priv->radio_led.led_type = ATH_LED_RADIO;
315 if (ret)
316 goto fail;
318 trigger = ieee80211_get_assoc_led_name(priv->hw);
319 snprintf(priv->assoc_led.name, sizeof(priv->assoc_led.name),
320 "ath9k-%s::assoc", wiphy_name(priv->hw->wiphy));
321 ret = ath9k_register_led(priv, &priv->assoc_led, trigger);
322 priv->assoc_led.led_type = ATH_LED_ASSOC;
323 if (ret)
324 goto fail;
326 trigger = ieee80211_get_tx_led_name(priv->hw);
327 snprintf(priv->tx_led.name, sizeof(priv->tx_led.name),
328 "ath9k-%s::tx", wiphy_name(priv->hw->wiphy));
329 ret = ath9k_register_led(priv, &priv->tx_led, trigger);
330 priv->tx_led.led_type = ATH_LED_TX;
331 if (ret)
332 goto fail;
334 trigger = ieee80211_get_rx_led_name(priv->hw);
335 snprintf(priv->rx_led.name, sizeof(priv->rx_led.name),
336 "ath9k-%s::rx", wiphy_name(priv->hw->wiphy));
337 ret = ath9k_register_led(priv, &priv->rx_led, trigger);
338 priv->rx_led.led_type = ATH_LED_RX;
339 if (ret)
340 goto fail;
342 priv->op_flags &= ~OP_LED_DEINIT;
344 return;
346 fail:
347 cancel_delayed_work_sync(&priv->ath9k_led_blink_work);
348 ath9k_deinit_leds(priv);
351 /*******************/
352 /* Rfkill */
353 /*******************/
355 static bool ath_is_rfkill_set(struct ath9k_htc_priv *priv)
357 return ath9k_hw_gpio_get(priv->ah, priv->ah->rfkill_gpio) ==
358 priv->ah->rfkill_polarity;
361 void ath9k_htc_rfkill_poll_state(struct ieee80211_hw *hw)
363 struct ath9k_htc_priv *priv = hw->priv;
364 bool blocked = !!ath_is_rfkill_set(priv);
366 wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
369 void ath9k_start_rfkill_poll(struct ath9k_htc_priv *priv)
371 if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
372 wiphy_rfkill_start_polling(priv->hw->wiphy);
375 void ath9k_htc_radio_enable(struct ieee80211_hw *hw)
377 struct ath9k_htc_priv *priv = hw->priv;
378 struct ath_hw *ah = priv->ah;
379 struct ath_common *common = ath9k_hw_common(ah);
380 int ret;
381 u8 cmd_rsp;
383 if (!ah->curchan)
384 ah->curchan = ath9k_cmn_get_curchannel(hw, ah);
386 /* Reset the HW */
387 ret = ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
388 if (ret) {
389 ath_err(common,
390 "Unable to reset hardware; reset status %d (freq %u MHz)\n",
391 ret, ah->curchan->channel);
394 ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
395 &priv->curtxpow);
397 /* Start RX */
398 WMI_CMD(WMI_START_RECV_CMDID);
399 ath9k_host_rx_init(priv);
401 /* Start TX */
402 htc_start(priv->htc);
403 spin_lock_bh(&priv->tx.tx_lock);
404 priv->tx.flags &= ~ATH9K_HTC_OP_TX_QUEUES_STOP;
405 spin_unlock_bh(&priv->tx.tx_lock);
406 ieee80211_wake_queues(hw);
408 WMI_CMD(WMI_ENABLE_INTR_CMDID);
410 /* Enable LED */
411 ath9k_hw_cfg_output(ah, ah->led_pin,
412 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
413 ath9k_hw_set_gpio(ah, ah->led_pin, 0);
416 void ath9k_htc_radio_disable(struct ieee80211_hw *hw)
418 struct ath9k_htc_priv *priv = hw->priv;
419 struct ath_hw *ah = priv->ah;
420 struct ath_common *common = ath9k_hw_common(ah);
421 int ret;
422 u8 cmd_rsp;
424 ath9k_htc_ps_wakeup(priv);
426 /* Disable LED */
427 ath9k_hw_set_gpio(ah, ah->led_pin, 1);
428 ath9k_hw_cfg_gpio_input(ah, ah->led_pin);
430 WMI_CMD(WMI_DISABLE_INTR_CMDID);
432 /* Stop TX */
433 ieee80211_stop_queues(hw);
434 ath9k_htc_tx_drain(priv);
435 WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
437 /* Stop RX */
438 WMI_CMD(WMI_STOP_RECV_CMDID);
440 /* Clear the WMI event queue */
441 ath9k_wmi_event_drain(priv);
444 * The MIB counters have to be disabled here,
445 * since the target doesn't do it.
447 ath9k_hw_disable_mib_counters(ah);
449 if (!ah->curchan)
450 ah->curchan = ath9k_cmn_get_curchannel(hw, ah);
452 /* Reset the HW */
453 ret = ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
454 if (ret) {
455 ath_err(common,
456 "Unable to reset hardware; reset status %d (freq %u MHz)\n",
457 ret, ah->curchan->channel);
460 /* Disable the PHY */
461 ath9k_hw_phy_disable(ah);
463 ath9k_htc_ps_restore(priv);
464 ath9k_htc_setpower(priv, ATH9K_PM_FULL_SLEEP);