1 /*
2  * Copyright (c) 2008-2011 Atheros Communications Inc.
3  *
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.
7  *
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.
15  */
16 
17 #include "ath9k.h"
18 
19 /********************************/
20 /*	 LED functions		*/
21 /********************************/
22 
23 #ifdef CONFIG_MAC80211_LEDS
24 
25 static void ath_fill_led_pin(struct ath_softc *sc)
26 {
27 	struct ath_hw *ah = sc->sc_ah;
28 
29 	/* Set default led pin if invalid */
30 	if (ah->led_pin < 0) {
31 		if (AR_SREV_9287(ah))
32 			ah->led_pin = ATH_LED_PIN_9287;
33 		else if (AR_SREV_9485(ah))
34 			ah->led_pin = ATH_LED_PIN_9485;
35 		else if (AR_SREV_9300(ah))
36 			ah->led_pin = ATH_LED_PIN_9300;
37 		else if (AR_SREV_9462(ah) || AR_SREV_9565(ah))
38 			ah->led_pin = ATH_LED_PIN_9462;
39 		else
40 			ah->led_pin = ATH_LED_PIN_DEF;
41 	}
42 
43 	/* Configure gpio for output */
44 	ath9k_hw_gpio_request_out(ah, ah->led_pin, "ath9k-led",
45 				  AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
46 
47 	/* LED off, active low */
48 	ath9k_hw_set_gpio(ah, ah->led_pin, ah->config.led_active_high ? 0 : 1);
49 }
50 
51 static void ath_led_brightness(struct led_classdev *led_cdev,
52 			       enum led_brightness brightness)
53 {
54 	struct ath_softc *sc = container_of(led_cdev, struct ath_softc, led_cdev);
55 	u32 val = (brightness == LED_OFF);
56 
57 	if (sc->sc_ah->config.led_active_high)
58 		val = !val;
59 
60 	ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, val);
61 }
62 
63 void ath_deinit_leds(struct ath_softc *sc)
64 {
65 	if (!sc->led_registered)
66 		return;
67 
68 	ath_led_brightness(&sc->led_cdev, LED_OFF);
69 	led_classdev_unregister(&sc->led_cdev);
70 
71 	ath9k_hw_gpio_free(sc->sc_ah, sc->sc_ah->led_pin);
72 }
73 
74 void ath_init_leds(struct ath_softc *sc)
75 {
76 	int ret;
77 
78 	if (AR_SREV_9100(sc->sc_ah))
79 		return;
80 
81 	ath_fill_led_pin(sc);
82 
83 	if (!ath9k_led_blink)
84 		sc->led_cdev.default_trigger =
85 			ieee80211_get_radio_led_name(sc->hw);
86 
87 	snprintf(sc->led_name, sizeof(sc->led_name),
88 		"ath9k-%s", wiphy_name(sc->hw->wiphy));
89 	sc->led_cdev.name = sc->led_name;
90 	sc->led_cdev.brightness_set = ath_led_brightness;
91 
92 	ret = led_classdev_register(wiphy_dev(sc->hw->wiphy), &sc->led_cdev);
93 	if (ret < 0)
94 		return;
95 
96 	sc->led_registered = true;
97 }
98 #endif
99 
100 /*******************/
101 /*	Rfkill	   */
102 /*******************/
103 
104 static bool ath_is_rfkill_set(struct ath_softc *sc)
105 {
106 	struct ath_hw *ah = sc->sc_ah;
107 	bool is_blocked;
108 
109 	ath9k_ps_wakeup(sc);
110 	is_blocked = ath9k_hw_gpio_get(ah, ah->rfkill_gpio) ==
111 				  ah->rfkill_polarity;
112 	ath9k_ps_restore(sc);
113 
114 	return is_blocked;
115 }
116 
117 void ath9k_rfkill_poll_state(struct ieee80211_hw *hw)
118 {
119 	struct ath_softc *sc = hw->priv;
120 	bool blocked = !!ath_is_rfkill_set(sc);
121 
122 	wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
123 }
124 
125 void ath_start_rfkill_poll(struct ath_softc *sc)
126 {
127 	struct ath_hw *ah = sc->sc_ah;
128 
129 	if (ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
130 		wiphy_rfkill_start_polling(sc->hw->wiphy);
131 }
132 
133 #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
134 
135 /******************/
136 /*     BTCOEX     */
137 /******************/
138 
139 /*
140  * Detects if there is any priority bt traffic
141  */
142 static void ath_detect_bt_priority(struct ath_softc *sc)
143 {
144 	struct ath_btcoex *btcoex = &sc->btcoex;
145 	struct ath_hw *ah = sc->sc_ah;
146 
147 	if (ath9k_hw_gpio_get(sc->sc_ah, ah->btcoex_hw.btpriority_gpio))
148 		btcoex->bt_priority_cnt++;
149 
150 	if (time_after(jiffies, btcoex->bt_priority_time +
151 			msecs_to_jiffies(ATH_BT_PRIORITY_TIME_THRESHOLD))) {
152 		clear_bit(BT_OP_PRIORITY_DETECTED, &btcoex->op_flags);
153 		clear_bit(BT_OP_SCAN, &btcoex->op_flags);
154 		/* Detect if colocated bt started scanning */
155 		if (btcoex->bt_priority_cnt >= ATH_BT_CNT_SCAN_THRESHOLD) {
156 			ath_dbg(ath9k_hw_common(sc->sc_ah), BTCOEX,
157 				"BT scan detected\n");
158 			set_bit(BT_OP_PRIORITY_DETECTED, &btcoex->op_flags);
159 			set_bit(BT_OP_SCAN, &btcoex->op_flags);
160 		} else if (btcoex->bt_priority_cnt >= ATH_BT_CNT_THRESHOLD) {
161 			ath_dbg(ath9k_hw_common(sc->sc_ah), BTCOEX,
162 				"BT priority traffic detected\n");
163 			set_bit(BT_OP_PRIORITY_DETECTED, &btcoex->op_flags);
164 		}
165 
166 		btcoex->bt_priority_cnt = 0;
167 		btcoex->bt_priority_time = jiffies;
168 	}
169 }
170 
171 static void ath_mci_ftp_adjust(struct ath_softc *sc)
172 {
173 	struct ath_btcoex *btcoex = &sc->btcoex;
174 	struct ath_mci_profile *mci = &btcoex->mci;
175 	struct ath_hw *ah = sc->sc_ah;
176 
177 	if (btcoex->bt_wait_time > ATH_BTCOEX_RX_WAIT_TIME) {
178 		if (ar9003_mci_state(ah, MCI_STATE_NEED_FTP_STOMP) &&
179 		    (mci->num_pan || mci->num_other_acl))
180 			ah->btcoex_hw.mci.stomp_ftp =
181 				(sc->rx.num_pkts < ATH_BTCOEX_STOMP_FTP_THRESH);
182 		else
183 			ah->btcoex_hw.mci.stomp_ftp = false;
184 		btcoex->bt_wait_time = 0;
185 		sc->rx.num_pkts = 0;
186 	}
187 }
188 
189 /*
190  * This is the master bt coex timer which runs for every
191  * 45ms, bt traffic will be given priority during 55% of this
192  * period while wlan gets remaining 45%
193  */
194 static void ath_btcoex_period_timer(struct timer_list *t)
195 {
196 	struct ath_softc *sc = timer_container_of(sc, t, btcoex.period_timer);
197 	struct ath_hw *ah = sc->sc_ah;
198 	struct ath_btcoex *btcoex = &sc->btcoex;
199 	enum ath_stomp_type stomp_type;
200 	u32 timer_period;
201 	unsigned long flags;
202 
203 	spin_lock_irqsave(&sc->sc_pm_lock, flags);
204 	if (sc->sc_ah->power_mode == ATH9K_PM_NETWORK_SLEEP) {
205 		btcoex->bt_wait_time += btcoex->btcoex_period;
206 		spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
207 		goto skip_hw_wakeup;
208 	}
209 	spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
210 
211 	ath9k_ps_wakeup(sc);
212 	spin_lock_bh(&btcoex->btcoex_lock);
213 
214 	if (ah->caps.hw_caps & ATH9K_HW_CAP_MCI) {
215 		ath9k_mci_update_rssi(sc);
216 		ath_mci_ftp_adjust(sc);
217 	}
218 
219 	if (!(ah->caps.hw_caps & ATH9K_HW_CAP_MCI))
220 		ath_detect_bt_priority(sc);
221 
222 	stomp_type = btcoex->bt_stomp_type;
223 	timer_period = btcoex->btcoex_no_stomp;
224 
225 	if (!(ah->caps.hw_caps & ATH9K_HW_CAP_MCI)) {
226 		if (test_bit(BT_OP_SCAN, &btcoex->op_flags)) {
227 			stomp_type = ATH_BTCOEX_STOMP_ALL;
228 			timer_period = btcoex->btscan_no_stomp;
229 		}
230 	} else if (btcoex->stomp_audio >= 5) {
231 		stomp_type = ATH_BTCOEX_STOMP_AUDIO;
232 		btcoex->stomp_audio = 0;
233 	}
234 
235 	ath9k_hw_btcoex_bt_stomp(ah, stomp_type);
236 	ath9k_hw_btcoex_enable(ah);
237 
238 	spin_unlock_bh(&btcoex->btcoex_lock);
239 
240 	if (btcoex->btcoex_period != btcoex->btcoex_no_stomp)
241 		mod_timer(&btcoex->no_stomp_timer,
242 			 jiffies + msecs_to_jiffies(timer_period));
243 
244 	ath9k_ps_restore(sc);
245 
246 skip_hw_wakeup:
247 	mod_timer(&btcoex->period_timer,
248 		  jiffies + msecs_to_jiffies(btcoex->btcoex_period));
249 }
250 
251 /*
252  * Generic tsf based hw timer which configures weight
253  * registers to time slice between wlan and bt traffic
254  */
255 static void ath_btcoex_no_stomp_timer(struct timer_list *t)
256 {
257 	struct ath_softc *sc = timer_container_of(sc, t,
258 						  btcoex.no_stomp_timer);
259 	struct ath_hw *ah = sc->sc_ah;
260 	struct ath_btcoex *btcoex = &sc->btcoex;
261 
262 	ath9k_ps_wakeup(sc);
263 	spin_lock_bh(&btcoex->btcoex_lock);
264 
265 	if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_LOW ||
266 	    (!(ah->caps.hw_caps & ATH9K_HW_CAP_MCI) &&
267 	     test_bit(BT_OP_SCAN, &btcoex->op_flags)))
268 		ath9k_hw_btcoex_bt_stomp(ah, ATH_BTCOEX_STOMP_NONE);
269 	else if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_ALL)
270 		ath9k_hw_btcoex_bt_stomp(ah, ATH_BTCOEX_STOMP_LOW);
271 
272 	ath9k_hw_btcoex_enable(ah);
273 	spin_unlock_bh(&btcoex->btcoex_lock);
274 	ath9k_ps_restore(sc);
275 }
276 
277 static void ath_init_btcoex_timer(struct ath_softc *sc)
278 {
279 	struct ath_btcoex *btcoex = &sc->btcoex;
280 
281 	btcoex->btcoex_period = ATH_BTCOEX_DEF_BT_PERIOD;
282 	btcoex->btcoex_no_stomp = (100 - ATH_BTCOEX_DEF_DUTY_CYCLE) *
283 		btcoex->btcoex_period / 100;
284 	btcoex->btscan_no_stomp = (100 - ATH_BTCOEX_BTSCAN_DUTY_CYCLE) *
285 				   btcoex->btcoex_period / 100;
286 	btcoex->bt_stomp_type = ATH_BTCOEX_STOMP_LOW;
287 
288 	timer_setup(&btcoex->period_timer, ath_btcoex_period_timer, 0);
289 	timer_setup(&btcoex->no_stomp_timer, ath_btcoex_no_stomp_timer, 0);
290 
291 	spin_lock_init(&btcoex->btcoex_lock);
292 }
293 
294 /*
295  * (Re)start btcoex timers
296  */
297 void ath9k_btcoex_timer_resume(struct ath_softc *sc)
298 {
299 	struct ath_btcoex *btcoex = &sc->btcoex;
300 	struct ath_hw *ah = sc->sc_ah;
301 
302 	if (ath9k_hw_get_btcoex_scheme(ah) != ATH_BTCOEX_CFG_3WIRE &&
303 	    ath9k_hw_get_btcoex_scheme(ah) != ATH_BTCOEX_CFG_MCI)
304 		return;
305 
306 	ath_dbg(ath9k_hw_common(ah), BTCOEX, "Starting btcoex timers\n");
307 
308 	/* make sure duty cycle timer is also stopped when resuming */
309 	timer_delete_sync(&btcoex->no_stomp_timer);
310 
311 	btcoex->bt_priority_cnt = 0;
312 	btcoex->bt_priority_time = jiffies;
313 	clear_bit(BT_OP_PRIORITY_DETECTED, &btcoex->op_flags);
314 	clear_bit(BT_OP_SCAN, &btcoex->op_flags);
315 
316 	mod_timer(&btcoex->period_timer, jiffies);
317 }
318 
319 /*
320  * Pause btcoex timer and bt duty cycle timer
321  */
322 void ath9k_btcoex_timer_pause(struct ath_softc *sc)
323 {
324 	struct ath_btcoex *btcoex = &sc->btcoex;
325 	struct ath_hw *ah = sc->sc_ah;
326 
327 	if (ath9k_hw_get_btcoex_scheme(ah) != ATH_BTCOEX_CFG_3WIRE &&
328 	    ath9k_hw_get_btcoex_scheme(ah) != ATH_BTCOEX_CFG_MCI)
329 		return;
330 
331 	ath_dbg(ath9k_hw_common(ah), BTCOEX, "Stopping btcoex timers\n");
332 
333 	timer_delete_sync(&btcoex->period_timer);
334 	timer_delete_sync(&btcoex->no_stomp_timer);
335 }
336 
337 void ath9k_btcoex_stop_gen_timer(struct ath_softc *sc)
338 {
339 	struct ath_btcoex *btcoex = &sc->btcoex;
340 
341 	timer_delete_sync(&btcoex->no_stomp_timer);
342 }
343 
344 u16 ath9k_btcoex_aggr_limit(struct ath_softc *sc, u32 max_4ms_framelen)
345 {
346 	struct ath_btcoex *btcoex = &sc->btcoex;
347 	struct ath_mci_profile *mci = &sc->btcoex.mci;
348 	u16 aggr_limit = 0;
349 
350 	if ((sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_MCI) && mci->aggr_limit)
351 		aggr_limit = (max_4ms_framelen * mci->aggr_limit) >> 4;
352 	else if (test_bit(BT_OP_PRIORITY_DETECTED, &btcoex->op_flags))
353 		aggr_limit = min((max_4ms_framelen * 3) / 8,
354 				 (u32)ATH_AMPDU_LIMIT_MAX);
355 
356 	return aggr_limit;
357 }
358 
359 void ath9k_btcoex_handle_interrupt(struct ath_softc *sc, u32 status)
360 {
361 	if (status & ATH9K_INT_MCI)
362 		ath_mci_intr(sc);
363 }
364 
365 void ath9k_start_btcoex(struct ath_softc *sc)
366 {
367 	struct ath_hw *ah = sc->sc_ah;
368 
369 	if (ah->btcoex_hw.enabled ||
370 	    ath9k_hw_get_btcoex_scheme(ah) == ATH_BTCOEX_CFG_NONE)
371 		return;
372 
373 	if (!(ah->caps.hw_caps & ATH9K_HW_CAP_MCI))
374 		ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
375 					   AR_STOMP_LOW_WLAN_WGHT, 0);
376 	else
377 		ath9k_hw_btcoex_set_weight(ah, 0, 0,
378 					   ATH_BTCOEX_STOMP_NONE);
379 	ath9k_hw_btcoex_enable(ah);
380 	ath9k_btcoex_timer_resume(sc);
381 }
382 
383 void ath9k_stop_btcoex(struct ath_softc *sc)
384 {
385 	struct ath_hw *ah = sc->sc_ah;
386 
387 	if (!ah->btcoex_hw.enabled ||
388 	    ath9k_hw_get_btcoex_scheme(ah) == ATH_BTCOEX_CFG_NONE)
389 		return;
390 
391 	ath9k_btcoex_timer_pause(sc);
392 	ath9k_hw_btcoex_disable(ah);
393 
394 	if (ah->caps.hw_caps & ATH9K_HW_CAP_MCI)
395 		ath_mci_flush_profile(&sc->btcoex.mci);
396 }
397 
398 void ath9k_deinit_btcoex(struct ath_softc *sc)
399 {
400 	struct ath_hw *ah = sc->sc_ah;
401 
402 	if (ath9k_hw_mci_is_enabled(ah))
403 		ath_mci_cleanup(sc);
404 	else {
405 		enum ath_btcoex_scheme scheme = ath9k_hw_get_btcoex_scheme(ah);
406 
407 		if (scheme == ATH_BTCOEX_CFG_2WIRE ||
408 		    scheme == ATH_BTCOEX_CFG_3WIRE)
409 			ath9k_hw_btcoex_deinit(sc->sc_ah);
410 	}
411 }
412 
413 int ath9k_init_btcoex(struct ath_softc *sc)
414 {
415 	struct ath_txq *txq;
416 	struct ath_hw *ah = sc->sc_ah;
417 	int r;
418 
419 	ath9k_hw_btcoex_init_scheme(ah);
420 
421 	switch (ath9k_hw_get_btcoex_scheme(sc->sc_ah)) {
422 	case ATH_BTCOEX_CFG_NONE:
423 		break;
424 	case ATH_BTCOEX_CFG_2WIRE:
425 		ath9k_hw_btcoex_init_2wire(sc->sc_ah);
426 		break;
427 	case ATH_BTCOEX_CFG_3WIRE:
428 		ath9k_hw_btcoex_init_3wire(sc->sc_ah);
429 		ath_init_btcoex_timer(sc);
430 		txq = sc->tx.txq_map[IEEE80211_AC_BE];
431 		ath9k_hw_init_btcoex_hw(sc->sc_ah, txq->axq_qnum);
432 		break;
433 	case ATH_BTCOEX_CFG_MCI:
434 		ath_init_btcoex_timer(sc);
435 
436 		sc->btcoex.duty_cycle = ATH_BTCOEX_DEF_DUTY_CYCLE;
437 		INIT_LIST_HEAD(&sc->btcoex.mci.info);
438 		ath9k_hw_btcoex_init_mci(ah);
439 
440 		r = ath_mci_setup(sc);
441 		if (r)
442 			return r;
443 
444 		break;
445 	default:
446 		WARN_ON(1);
447 		break;
448 	}
449 
450 	return 0;
451 }
452 
453 static int ath9k_dump_mci_btcoex(struct ath_softc *sc, u8 *buf, u32 size)
454 {
455 	struct ath_btcoex *btcoex = &sc->btcoex;
456 	struct ath_mci_profile *mci = &btcoex->mci;
457 	struct ath_hw *ah = sc->sc_ah;
458 	struct ath_btcoex_hw *btcoex_hw = &ah->btcoex_hw;
459 	u32 len = 0;
460 	int i;
461 
462 	ATH_DUMP_BTCOEX("Total BT profiles", NUM_PROF(mci));
463 	ATH_DUMP_BTCOEX("MGMT", mci->num_mgmt);
464 	ATH_DUMP_BTCOEX("SCO", mci->num_sco);
465 	ATH_DUMP_BTCOEX("A2DP", mci->num_a2dp);
466 	ATH_DUMP_BTCOEX("HID", mci->num_hid);
467 	ATH_DUMP_BTCOEX("PAN", mci->num_pan);
468 	ATH_DUMP_BTCOEX("ACL", mci->num_other_acl);
469 	ATH_DUMP_BTCOEX("BDR", mci->num_bdr);
470 	ATH_DUMP_BTCOEX("Aggr. Limit", mci->aggr_limit);
471 	ATH_DUMP_BTCOEX("Stomp Type", btcoex->bt_stomp_type);
472 	ATH_DUMP_BTCOEX("BTCoex Period (msec)", btcoex->btcoex_period);
473 	ATH_DUMP_BTCOEX("Duty Cycle", btcoex->duty_cycle);
474 	ATH_DUMP_BTCOEX("BT Wait time", btcoex->bt_wait_time);
475 	ATH_DUMP_BTCOEX("Concurrent Tx", btcoex_hw->mci.concur_tx);
476 	ATH_DUMP_BTCOEX("Concurrent RSSI cnt", btcoex->rssi_count);
477 
478 	len += scnprintf(buf + len, size - len, "BT Weights: ");
479 	for (i = 0; i < AR9300_NUM_BT_WEIGHTS; i++)
480 		len += scnprintf(buf + len, size - len, "%08x ",
481 				 btcoex_hw->bt_weight[i]);
482 	len += scnprintf(buf + len, size - len, "\n");
483 	len += scnprintf(buf + len, size - len, "WLAN Weights: ");
484 	for (i = 0; i < AR9300_NUM_BT_WEIGHTS; i++)
485 		len += scnprintf(buf + len, size - len, "%08x ",
486 				 btcoex_hw->wlan_weight[i]);
487 	len += scnprintf(buf + len, size - len, "\n");
488 	len += scnprintf(buf + len, size - len, "Tx Priorities: ");
489 	for (i = 0; i < ATH_BTCOEX_STOMP_MAX; i++)
490 		len += scnprintf(buf + len, size - len, "%08x ",
491 				btcoex_hw->tx_prio[i]);
492 
493 	len += scnprintf(buf + len, size - len, "\n");
494 
495 	return len;
496 }
497 
498 static int ath9k_dump_legacy_btcoex(struct ath_softc *sc, u8 *buf, u32 size)
499 {
500 
501 	struct ath_btcoex *btcoex = &sc->btcoex;
502 	u32 len = 0;
503 
504 	ATH_DUMP_BTCOEX("Stomp Type", btcoex->bt_stomp_type);
505 	ATH_DUMP_BTCOEX("BTCoex Period (msec)", btcoex->btcoex_period);
506 	ATH_DUMP_BTCOEX("Duty Cycle", btcoex->duty_cycle);
507 	ATH_DUMP_BTCOEX("BT Wait time", btcoex->bt_wait_time);
508 
509 	return len;
510 }
511 
512 int ath9k_dump_btcoex(struct ath_softc *sc, u8 *buf, u32 size)
513 {
514 	if (ath9k_hw_mci_is_enabled(sc->sc_ah))
515 		return ath9k_dump_mci_btcoex(sc, buf, size);
516 	else
517 		return ath9k_dump_legacy_btcoex(sc, buf, size);
518 }
519 
520 #endif /* CONFIG_ATH9K_BTCOEX_SUPPORT */
521