1fb9987d0SSujith /*
25b68138eSSujith Manoharan  * Copyright (c) 2010-2011 Atheros Communications Inc.
3fb9987d0SSujith  *
4fb9987d0SSujith  * Permission to use, copy, modify, and/or distribute this software for any
5fb9987d0SSujith  * purpose with or without fee is hereby granted, provided that the above
6fb9987d0SSujith  * copyright notice and this permission notice appear in all copies.
7fb9987d0SSujith  *
8fb9987d0SSujith  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9fb9987d0SSujith  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10fb9987d0SSujith  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11fb9987d0SSujith  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12fb9987d0SSujith  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13fb9987d0SSujith  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14fb9987d0SSujith  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15fb9987d0SSujith  */
16fb9987d0SSujith 
17516304b0SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18516304b0SJoe Perches 
19fb9987d0SSujith #include "htc.h"
20fb9987d0SSujith 
21fb9987d0SSujith MODULE_AUTHOR("Atheros Communications");
22fb9987d0SSujith MODULE_LICENSE("Dual BSD/GPL");
23fb9987d0SSujith MODULE_DESCRIPTION("Atheros driver 802.11n HTC based wireless devices");
24fb9987d0SSujith 
25fb9987d0SSujith static unsigned int ath9k_debug = ATH_DBG_DEFAULT;
26fb9987d0SSujith module_param_named(debug, ath9k_debug, uint, 0);
27fb9987d0SSujith MODULE_PARM_DESC(debug, "Debugging mask");
28fb9987d0SSujith 
29e1572c5eSSujith int htc_modparam_nohwcrypt;
30e1572c5eSSujith module_param_named(nohwcrypt, htc_modparam_nohwcrypt, int, 0444);
31fb9987d0SSujith MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption");
32fb9987d0SSujith 
337f34778eSMohammed Shafi Shajakhan static int ath9k_htc_btcoex_enable;
347f34778eSMohammed Shafi Shajakhan module_param_named(btcoex_enable, ath9k_htc_btcoex_enable, int, 0444);
357f34778eSMohammed Shafi Shajakhan MODULE_PARM_DESC(btcoex_enable, "Enable wifi-BT coexistence");
367f34778eSMohammed Shafi Shajakhan 
376bca610dSOleksij Rempel static int ath9k_ps_enable;
386bca610dSOleksij Rempel module_param_named(ps_enable, ath9k_ps_enable, int, 0444);
396bca610dSOleksij Rempel MODULE_PARM_DESC(ps_enable, "Enable WLAN PowerSave");
406bca610dSOleksij Rempel 
41d244f21eSSujith Manoharan #ifdef CONFIG_MAC80211_LEDS
42d244f21eSSujith Manoharan static const struct ieee80211_tpt_blink ath9k_htc_tpt_blink[] = {
43d244f21eSSujith Manoharan 	{ .throughput = 0 * 1024, .blink_time = 334 },
44d244f21eSSujith Manoharan 	{ .throughput = 1 * 1024, .blink_time = 260 },
45d244f21eSSujith Manoharan 	{ .throughput = 5 * 1024, .blink_time = 220 },
46d244f21eSSujith Manoharan 	{ .throughput = 10 * 1024, .blink_time = 190 },
47d244f21eSSujith Manoharan 	{ .throughput = 20 * 1024, .blink_time = 170 },
48d244f21eSSujith Manoharan 	{ .throughput = 50 * 1024, .blink_time = 150 },
49d244f21eSSujith Manoharan 	{ .throughput = 70 * 1024, .blink_time = 130 },
50d244f21eSSujith Manoharan 	{ .throughput = 100 * 1024, .blink_time = 110 },
51d244f21eSSujith Manoharan 	{ .throughput = 200 * 1024, .blink_time = 80 },
52d244f21eSSujith Manoharan 	{ .throughput = 300 * 1024, .blink_time = 50 },
53d244f21eSSujith Manoharan };
54d244f21eSSujith Manoharan #endif
55d244f21eSSujith Manoharan 
56fb9987d0SSujith static int ath9k_htc_wait_for_target(struct ath9k_htc_priv *priv)
57fb9987d0SSujith {
58fb9987d0SSujith 	int time_left;
59fb9987d0SSujith 
60d8c49ffbSSujith.Manoharan@atheros.com 	if (atomic_read(&priv->htc->tgt_ready) > 0) {
61d8c49ffbSSujith.Manoharan@atheros.com 		atomic_dec(&priv->htc->tgt_ready);
62d8c49ffbSSujith.Manoharan@atheros.com 		return 0;
63d8c49ffbSSujith.Manoharan@atheros.com 	}
64d8c49ffbSSujith.Manoharan@atheros.com 
65fb9987d0SSujith 	/* Firmware can take up to 50ms to get ready, to be safe use 1 second */
66fb9987d0SSujith 	time_left = wait_for_completion_timeout(&priv->htc->target_wait, HZ);
67fb9987d0SSujith 	if (!time_left) {
68fb9987d0SSujith 		dev_err(priv->dev, "ath9k_htc: Target is unresponsive\n");
69fb9987d0SSujith 		return -ETIMEDOUT;
70fb9987d0SSujith 	}
71fb9987d0SSujith 
72d8c49ffbSSujith.Manoharan@atheros.com 	atomic_dec(&priv->htc->tgt_ready);
73d8c49ffbSSujith.Manoharan@atheros.com 
74fb9987d0SSujith 	return 0;
75fb9987d0SSujith }
76fb9987d0SSujith 
77fb9987d0SSujith static void ath9k_deinit_priv(struct ath9k_htc_priv *priv)
78fb9987d0SSujith {
79fb9987d0SSujith 	ath9k_hw_deinit(priv->ah);
80fb9987d0SSujith 	kfree(priv->ah);
81fb9987d0SSujith 	priv->ah = NULL;
82fb9987d0SSujith }
83fb9987d0SSujith 
84fb9987d0SSujith static void ath9k_deinit_device(struct ath9k_htc_priv *priv)
85fb9987d0SSujith {
86fb9987d0SSujith 	struct ieee80211_hw *hw = priv->hw;
87fb9987d0SSujith 
88fb9987d0SSujith 	wiphy_rfkill_stop_polling(hw->wiphy);
89fb9987d0SSujith 	ath9k_deinit_leds(priv);
90fb9987d0SSujith 	ieee80211_unregister_hw(hw);
91fb9987d0SSujith 	ath9k_rx_cleanup(priv);
92fb9987d0SSujith 	ath9k_tx_cleanup(priv);
93fb9987d0SSujith 	ath9k_deinit_priv(priv);
94fb9987d0SSujith }
95fb9987d0SSujith 
96fb9987d0SSujith static inline int ath9k_htc_connect_svc(struct ath9k_htc_priv *priv,
97fb9987d0SSujith 					u16 service_id,
98fb9987d0SSujith 					void (*tx) (void *,
99fb9987d0SSujith 						    struct sk_buff *,
100fb9987d0SSujith 						    enum htc_endpoint_id,
101fb9987d0SSujith 						    bool txok),
102fb9987d0SSujith 					enum htc_endpoint_id *ep_id)
103fb9987d0SSujith {
104fb9987d0SSujith 	struct htc_service_connreq req;
105fb9987d0SSujith 
106fb9987d0SSujith 	memset(&req, 0, sizeof(struct htc_service_connreq));
107fb9987d0SSujith 
108fb9987d0SSujith 	req.service_id = service_id;
109fb9987d0SSujith 	req.ep_callbacks.priv = priv;
110fb9987d0SSujith 	req.ep_callbacks.rx = ath9k_htc_rxep;
111fb9987d0SSujith 	req.ep_callbacks.tx = tx;
112fb9987d0SSujith 
113fb9987d0SSujith 	return htc_connect_service(priv->htc, &req, ep_id);
114fb9987d0SSujith }
115fb9987d0SSujith 
116fa6e15e0SRajkumar Manoharan static int ath9k_init_htc_services(struct ath9k_htc_priv *priv, u16 devid,
117fa6e15e0SRajkumar Manoharan 				   u32 drv_info)
118fb9987d0SSujith {
119fb9987d0SSujith 	int ret;
120fb9987d0SSujith 
121fb9987d0SSujith 	/* WMI CMD*/
122fb9987d0SSujith 	ret = ath9k_wmi_connect(priv->htc, priv->wmi, &priv->wmi_cmd_ep);
123fb9987d0SSujith 	if (ret)
124fb9987d0SSujith 		goto err;
125fb9987d0SSujith 
126fb9987d0SSujith 	/* Beacon */
1279c6dda4eSSujith 	ret = ath9k_htc_connect_svc(priv, WMI_BEACON_SVC, ath9k_htc_beaconep,
128fb9987d0SSujith 				    &priv->beacon_ep);
129fb9987d0SSujith 	if (ret)
130fb9987d0SSujith 		goto err;
131fb9987d0SSujith 
132fb9987d0SSujith 	/* CAB */
133fb9987d0SSujith 	ret = ath9k_htc_connect_svc(priv, WMI_CAB_SVC, ath9k_htc_txep,
134fb9987d0SSujith 				    &priv->cab_ep);
135fb9987d0SSujith 	if (ret)
136fb9987d0SSujith 		goto err;
137fb9987d0SSujith 
138fb9987d0SSujith 
139fb9987d0SSujith 	/* UAPSD */
140fb9987d0SSujith 	ret = ath9k_htc_connect_svc(priv, WMI_UAPSD_SVC, ath9k_htc_txep,
141fb9987d0SSujith 				    &priv->uapsd_ep);
142fb9987d0SSujith 	if (ret)
143fb9987d0SSujith 		goto err;
144fb9987d0SSujith 
145fb9987d0SSujith 	/* MGMT */
146fb9987d0SSujith 	ret = ath9k_htc_connect_svc(priv, WMI_MGMT_SVC, ath9k_htc_txep,
147fb9987d0SSujith 				    &priv->mgmt_ep);
148fb9987d0SSujith 	if (ret)
149fb9987d0SSujith 		goto err;
150fb9987d0SSujith 
151fb9987d0SSujith 	/* DATA BE */
152fb9987d0SSujith 	ret = ath9k_htc_connect_svc(priv, WMI_DATA_BE_SVC, ath9k_htc_txep,
153fb9987d0SSujith 				    &priv->data_be_ep);
154fb9987d0SSujith 	if (ret)
155fb9987d0SSujith 		goto err;
156fb9987d0SSujith 
157fb9987d0SSujith 	/* DATA BK */
158fb9987d0SSujith 	ret = ath9k_htc_connect_svc(priv, WMI_DATA_BK_SVC, ath9k_htc_txep,
159fb9987d0SSujith 				    &priv->data_bk_ep);
160fb9987d0SSujith 	if (ret)
161fb9987d0SSujith 		goto err;
162fb9987d0SSujith 
163fb9987d0SSujith 	/* DATA VI */
164fb9987d0SSujith 	ret = ath9k_htc_connect_svc(priv, WMI_DATA_VI_SVC, ath9k_htc_txep,
165fb9987d0SSujith 				    &priv->data_vi_ep);
166fb9987d0SSujith 	if (ret)
167fb9987d0SSujith 		goto err;
168fb9987d0SSujith 
169fb9987d0SSujith 	/* DATA VO */
170fb9987d0SSujith 	ret = ath9k_htc_connect_svc(priv, WMI_DATA_VO_SVC, ath9k_htc_txep,
171fb9987d0SSujith 				    &priv->data_vo_ep);
172fb9987d0SSujith 	if (ret)
173fb9987d0SSujith 		goto err;
174fb9987d0SSujith 
1756267dc70SSujith 	/*
1766267dc70SSujith 	 * Setup required credits before initializing HTC.
1776267dc70SSujith 	 * This is a bit hacky, but, since queuing is done in
1786267dc70SSujith 	 * the HIF layer, shouldn't matter much.
1796267dc70SSujith 	 */
1806267dc70SSujith 
1810b5ead91SSujith Manoharan 	if (IS_AR7010_DEVICE(drv_info))
182d108e8b9SSujith Manoharan 		priv->htc->credits = 45;
183fa6e15e0SRajkumar Manoharan 	else
1844e63f768SSujith 		priv->htc->credits = 33;
1856267dc70SSujith 
186fb9987d0SSujith 	ret = htc_init(priv->htc);
187fb9987d0SSujith 	if (ret)
188fb9987d0SSujith 		goto err;
189fb9987d0SSujith 
1906267dc70SSujith 	dev_info(priv->dev, "ath9k_htc: HTC initialized with %d credits\n",
1916267dc70SSujith 		 priv->htc->credits);
1926267dc70SSujith 
193fb9987d0SSujith 	return 0;
194fb9987d0SSujith 
195fb9987d0SSujith err:
196fb9987d0SSujith 	dev_err(priv->dev, "ath9k_htc: Unable to initialize HTC services\n");
197fb9987d0SSujith 	return ret;
198fb9987d0SSujith }
199fb9987d0SSujith 
2000c0280bdSLuis R. Rodriguez static void ath9k_reg_notifier(struct wiphy *wiphy,
201fb9987d0SSujith 			       struct regulatory_request *request)
202fb9987d0SSujith {
203fb9987d0SSujith 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
204fb9987d0SSujith 	struct ath9k_htc_priv *priv = hw->priv;
205fb9987d0SSujith 
2060c0280bdSLuis R. Rodriguez 	ath_reg_notifier_apply(wiphy, request,
207fb9987d0SSujith 			       ath9k_hw_regulatory(priv->ah));
208fb9987d0SSujith }
209fb9987d0SSujith 
2104a22fe10SSujith static unsigned int ath9k_regread(void *hw_priv, u32 reg_offset)
211fb9987d0SSujith {
212fb9987d0SSujith 	struct ath_hw *ah = (struct ath_hw *) hw_priv;
213fb9987d0SSujith 	struct ath_common *common = ath9k_hw_common(ah);
214fb9987d0SSujith 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
215fb9987d0SSujith 	__be32 val, reg = cpu_to_be32(reg_offset);
216fb9987d0SSujith 	int r;
217fb9987d0SSujith 
218fb9987d0SSujith 	r = ath9k_wmi_cmd(priv->wmi, WMI_REG_READ_CMDID,
219fb9987d0SSujith 			  (u8 *) &reg, sizeof(reg),
220fb9987d0SSujith 			  (u8 *) &val, sizeof(val),
221fb9987d0SSujith 			  100);
222fb9987d0SSujith 	if (unlikely(r)) {
223d2182b69SJoe Perches 		ath_dbg(common, WMI, "REGISTER READ FAILED: (0x%04x, %d)\n",
224fb9987d0SSujith 			reg_offset, r);
225fb9987d0SSujith 		return -EIO;
226fb9987d0SSujith 	}
227fb9987d0SSujith 
228fb9987d0SSujith 	return be32_to_cpu(val);
229fb9987d0SSujith }
230fb9987d0SSujith 
23109a525d3SSujith Manoharan static void ath9k_multi_regread(void *hw_priv, u32 *addr,
23209a525d3SSujith Manoharan 				u32 *val, u16 count)
23309a525d3SSujith Manoharan {
23409a525d3SSujith Manoharan 	struct ath_hw *ah = (struct ath_hw *) hw_priv;
23509a525d3SSujith Manoharan 	struct ath_common *common = ath9k_hw_common(ah);
23609a525d3SSujith Manoharan 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
23709a525d3SSujith Manoharan 	__be32 tmpaddr[8];
23809a525d3SSujith Manoharan 	__be32 tmpval[8];
23909a525d3SSujith Manoharan 	int i, ret;
24009a525d3SSujith Manoharan 
24109a525d3SSujith Manoharan        for (i = 0; i < count; i++) {
24209a525d3SSujith Manoharan 	       tmpaddr[i] = cpu_to_be32(addr[i]);
24309a525d3SSujith Manoharan        }
24409a525d3SSujith Manoharan 
24509a525d3SSujith Manoharan        ret = ath9k_wmi_cmd(priv->wmi, WMI_REG_READ_CMDID,
24609a525d3SSujith Manoharan 			   (u8 *)tmpaddr , sizeof(u32) * count,
24709a525d3SSujith Manoharan 			   (u8 *)tmpval, sizeof(u32) * count,
24809a525d3SSujith Manoharan 			   100);
24909a525d3SSujith Manoharan 	if (unlikely(ret)) {
250d2182b69SJoe Perches 		ath_dbg(common, WMI,
25109a525d3SSujith Manoharan 			"Multiple REGISTER READ FAILED (count: %d)\n", count);
25209a525d3SSujith Manoharan 	}
25309a525d3SSujith Manoharan 
25409a525d3SSujith Manoharan        for (i = 0; i < count; i++) {
25509a525d3SSujith Manoharan 	       val[i] = be32_to_cpu(tmpval[i]);
25609a525d3SSujith Manoharan        }
25709a525d3SSujith Manoharan }
25809a525d3SSujith Manoharan 
2592259ba38SOleksij Rempel static void ath9k_regwrite_multi(struct ath_common *common)
2602259ba38SOleksij Rempel {
2612259ba38SOleksij Rempel 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
2622259ba38SOleksij Rempel 	u32 rsp_status;
2632259ba38SOleksij Rempel 	int r;
2642259ba38SOleksij Rempel 
2652259ba38SOleksij Rempel 	r = ath9k_wmi_cmd(priv->wmi, WMI_REG_WRITE_CMDID,
2662259ba38SOleksij Rempel 			  (u8 *) &priv->wmi->multi_write,
2672259ba38SOleksij Rempel 			  sizeof(struct register_write) * priv->wmi->multi_write_idx,
2682259ba38SOleksij Rempel 			  (u8 *) &rsp_status, sizeof(rsp_status),
2692259ba38SOleksij Rempel 			  100);
2702259ba38SOleksij Rempel 	if (unlikely(r)) {
2712259ba38SOleksij Rempel 		ath_dbg(common, WMI,
2722259ba38SOleksij Rempel 			"REGISTER WRITE FAILED, multi len: %d\n",
2732259ba38SOleksij Rempel 			priv->wmi->multi_write_idx);
2742259ba38SOleksij Rempel 	}
2752259ba38SOleksij Rempel 	priv->wmi->multi_write_idx = 0;
2762259ba38SOleksij Rempel }
2772259ba38SOleksij Rempel 
2784a22fe10SSujith static void ath9k_regwrite_single(void *hw_priv, u32 val, u32 reg_offset)
279fb9987d0SSujith {
280fb9987d0SSujith 	struct ath_hw *ah = (struct ath_hw *) hw_priv;
281fb9987d0SSujith 	struct ath_common *common = ath9k_hw_common(ah);
282fb9987d0SSujith 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
28307b2fa5aSJoe Perches 	const __be32 buf[2] = {
284fb9987d0SSujith 		cpu_to_be32(reg_offset),
285fb9987d0SSujith 		cpu_to_be32(val),
286fb9987d0SSujith 	};
287fb9987d0SSujith 	int r;
288fb9987d0SSujith 
289fb9987d0SSujith 	r = ath9k_wmi_cmd(priv->wmi, WMI_REG_WRITE_CMDID,
290fb9987d0SSujith 			  (u8 *) &buf, sizeof(buf),
291fb9987d0SSujith 			  (u8 *) &val, sizeof(val),
292fb9987d0SSujith 			  100);
293fb9987d0SSujith 	if (unlikely(r)) {
294d2182b69SJoe Perches 		ath_dbg(common, WMI, "REGISTER WRITE FAILED:(0x%04x, %d)\n",
295fb9987d0SSujith 			reg_offset, r);
296fb9987d0SSujith 	}
297fb9987d0SSujith }
298fb9987d0SSujith 
2994a22fe10SSujith static void ath9k_regwrite_buffer(void *hw_priv, u32 val, u32 reg_offset)
3004a22fe10SSujith {
3014a22fe10SSujith 	struct ath_hw *ah = (struct ath_hw *) hw_priv;
3024a22fe10SSujith 	struct ath_common *common = ath9k_hw_common(ah);
3034a22fe10SSujith 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
3044a22fe10SSujith 
3054a22fe10SSujith 	mutex_lock(&priv->wmi->multi_write_mutex);
3064a22fe10SSujith 
3074a22fe10SSujith 	/* Store the register/value */
3084a22fe10SSujith 	priv->wmi->multi_write[priv->wmi->multi_write_idx].reg =
3094a22fe10SSujith 		cpu_to_be32(reg_offset);
3104a22fe10SSujith 	priv->wmi->multi_write[priv->wmi->multi_write_idx].val =
3114a22fe10SSujith 		cpu_to_be32(val);
3124a22fe10SSujith 
3134a22fe10SSujith 	priv->wmi->multi_write_idx++;
3144a22fe10SSujith 
3154a22fe10SSujith 	/* If the buffer is full, send it out. */
3162259ba38SOleksij Rempel 	if (priv->wmi->multi_write_idx == MAX_CMD_NUMBER)
3172259ba38SOleksij Rempel 		ath9k_regwrite_multi(common);
3184a22fe10SSujith 
3194a22fe10SSujith 	mutex_unlock(&priv->wmi->multi_write_mutex);
3204a22fe10SSujith }
3214a22fe10SSujith 
3224a22fe10SSujith static void ath9k_regwrite(void *hw_priv, u32 val, u32 reg_offset)
3234a22fe10SSujith {
3244a22fe10SSujith 	struct ath_hw *ah = (struct ath_hw *) hw_priv;
3254a22fe10SSujith 	struct ath_common *common = ath9k_hw_common(ah);
3264a22fe10SSujith 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
3274a22fe10SSujith 
3284a22fe10SSujith 	if (atomic_read(&priv->wmi->mwrite_cnt))
3294a22fe10SSujith 		ath9k_regwrite_buffer(hw_priv, val, reg_offset);
3304a22fe10SSujith 	else
3314a22fe10SSujith 		ath9k_regwrite_single(hw_priv, val, reg_offset);
3324a22fe10SSujith }
3334a22fe10SSujith 
3344a22fe10SSujith static void ath9k_enable_regwrite_buffer(void *hw_priv)
3354a22fe10SSujith {
3364a22fe10SSujith 	struct ath_hw *ah = (struct ath_hw *) hw_priv;
3374a22fe10SSujith 	struct ath_common *common = ath9k_hw_common(ah);
3384a22fe10SSujith 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
3394a22fe10SSujith 
3404a22fe10SSujith 	atomic_inc(&priv->wmi->mwrite_cnt);
3414a22fe10SSujith }
3424a22fe10SSujith 
3434a22fe10SSujith static void ath9k_regwrite_flush(void *hw_priv)
3444a22fe10SSujith {
3454a22fe10SSujith 	struct ath_hw *ah = (struct ath_hw *) hw_priv;
3464a22fe10SSujith 	struct ath_common *common = ath9k_hw_common(ah);
3474a22fe10SSujith 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
3484a22fe10SSujith 
349435c1610SFelix Fietkau 	atomic_dec(&priv->wmi->mwrite_cnt);
350435c1610SFelix Fietkau 
3514a22fe10SSujith 	mutex_lock(&priv->wmi->multi_write_mutex);
3524a22fe10SSujith 
3532259ba38SOleksij Rempel 	if (priv->wmi->multi_write_idx)
3542259ba38SOleksij Rempel 		ath9k_regwrite_multi(common);
3554a22fe10SSujith 
3564a22fe10SSujith 	mutex_unlock(&priv->wmi->multi_write_mutex);
3574a22fe10SSujith }
3584a22fe10SSujith 
359845e03c9SFelix Fietkau static u32 ath9k_reg_rmw(void *hw_priv, u32 reg_offset, u32 set, u32 clr)
360845e03c9SFelix Fietkau {
361845e03c9SFelix Fietkau 	u32 val;
362845e03c9SFelix Fietkau 
363845e03c9SFelix Fietkau 	val = ath9k_regread(hw_priv, reg_offset);
364845e03c9SFelix Fietkau 	val &= ~clr;
365845e03c9SFelix Fietkau 	val |= set;
366845e03c9SFelix Fietkau 	ath9k_regwrite(hw_priv, val, reg_offset);
367845e03c9SFelix Fietkau 	return val;
368845e03c9SFelix Fietkau }
369845e03c9SFelix Fietkau 
370fb9987d0SSujith static void ath_usb_read_cachesize(struct ath_common *common, int *csz)
371fb9987d0SSujith {
372fb9987d0SSujith 	*csz = L1_CACHE_BYTES >> 2;
373fb9987d0SSujith }
374fb9987d0SSujith 
375fb9987d0SSujith static bool ath_usb_eeprom_read(struct ath_common *common, u32 off, u16 *data)
376fb9987d0SSujith {
377fb9987d0SSujith 	struct ath_hw *ah = (struct ath_hw *) common->ah;
378fb9987d0SSujith 
379fb9987d0SSujith 	(void)REG_READ(ah, AR5416_EEPROM_OFFSET + (off << AR5416_EEPROM_S));
380fb9987d0SSujith 
381fb9987d0SSujith 	if (!ath9k_hw_wait(ah,
382fb9987d0SSujith 			   AR_EEPROM_STATUS_DATA,
383fb9987d0SSujith 			   AR_EEPROM_STATUS_DATA_BUSY |
384fb9987d0SSujith 			   AR_EEPROM_STATUS_DATA_PROT_ACCESS, 0,
385fb9987d0SSujith 			   AH_WAIT_TIMEOUT))
386fb9987d0SSujith 		return false;
387fb9987d0SSujith 
388fb9987d0SSujith 	*data = MS(REG_READ(ah, AR_EEPROM_STATUS_DATA),
389fb9987d0SSujith 		   AR_EEPROM_STATUS_DATA_VAL);
390fb9987d0SSujith 
391fb9987d0SSujith 	return true;
392fb9987d0SSujith }
393fb9987d0SSujith 
394fb9987d0SSujith static const struct ath_bus_ops ath9k_usb_bus_ops = {
395497ad9adSSujith 	.ath_bus_type = ATH_USB,
396fb9987d0SSujith 	.read_cachesize = ath_usb_read_cachesize,
397fb9987d0SSujith 	.eeprom_read = ath_usb_eeprom_read,
398fb9987d0SSujith };
399fb9987d0SSujith 
400fb9987d0SSujith static int ath9k_init_queues(struct ath9k_htc_priv *priv)
401fb9987d0SSujith {
402fb9987d0SSujith 	struct ath_common *common = ath9k_hw_common(priv->ah);
403fb9987d0SSujith 	int i;
404fb9987d0SSujith 
405fb9987d0SSujith 	for (i = 0; i < ARRAY_SIZE(priv->hwq_map); i++)
406fb9987d0SSujith 		priv->hwq_map[i] = -1;
407fb9987d0SSujith 
408ca74b83bSSujith 	priv->beaconq = ath9k_hw_beaconq_setup(priv->ah);
409ca74b83bSSujith 	if (priv->beaconq == -1) {
4103800276aSJoe Perches 		ath_err(common, "Unable to setup BEACON xmit queue\n");
411ca74b83bSSujith 		goto err;
412ca74b83bSSujith 	}
413ca74b83bSSujith 
414ca74b83bSSujith 	priv->cabq = ath9k_htc_cabq_setup(priv);
415ca74b83bSSujith 	if (priv->cabq == -1) {
4163800276aSJoe Perches 		ath_err(common, "Unable to setup CAB xmit queue\n");
417ca74b83bSSujith 		goto err;
418ca74b83bSSujith 	}
419ca74b83bSSujith 
420bea843c7SSujith Manoharan 	if (!ath9k_htc_txq_setup(priv, IEEE80211_AC_BE)) {
4213800276aSJoe Perches 		ath_err(common, "Unable to setup xmit queue for BE traffic\n");
422fb9987d0SSujith 		goto err;
423fb9987d0SSujith 	}
424fb9987d0SSujith 
425bea843c7SSujith Manoharan 	if (!ath9k_htc_txq_setup(priv, IEEE80211_AC_BK)) {
4263800276aSJoe Perches 		ath_err(common, "Unable to setup xmit queue for BK traffic\n");
427fb9987d0SSujith 		goto err;
428fb9987d0SSujith 	}
429bea843c7SSujith Manoharan 	if (!ath9k_htc_txq_setup(priv, IEEE80211_AC_VI)) {
4303800276aSJoe Perches 		ath_err(common, "Unable to setup xmit queue for VI traffic\n");
431fb9987d0SSujith 		goto err;
432fb9987d0SSujith 	}
433bea843c7SSujith Manoharan 	if (!ath9k_htc_txq_setup(priv, IEEE80211_AC_VO)) {
4343800276aSJoe Perches 		ath_err(common, "Unable to setup xmit queue for VO traffic\n");
435fb9987d0SSujith 		goto err;
436fb9987d0SSujith 	}
437fb9987d0SSujith 
438fb9987d0SSujith 	return 0;
439fb9987d0SSujith 
440fb9987d0SSujith err:
441fb9987d0SSujith 	return -EINVAL;
442fb9987d0SSujith }
443fb9987d0SSujith 
444fb9987d0SSujith static void ath9k_init_misc(struct ath9k_htc_priv *priv)
445fb9987d0SSujith {
446fb9987d0SSujith 	struct ath_common *common = ath9k_hw_common(priv->ah);
447fb9987d0SSujith 
448fb9987d0SSujith 	memcpy(common->bssidmask, ath_bcast_mac, ETH_ALEN);
449fb9987d0SSujith 
450e5ba18c6SOleksij Rempel 	common->last_rssi = ATH_RSSI_DUMMY_MARKER;
4519f01a84eSSujith 	priv->ah->opmode = NL80211_IFTYPE_STATION;
452fb9987d0SSujith }
453fb9987d0SSujith 
45421cb9879SVivek Natarajan static int ath9k_init_priv(struct ath9k_htc_priv *priv,
455fa6e15e0SRajkumar Manoharan 			   u16 devid, char *product,
456fa6e15e0SRajkumar Manoharan 			   u32 drv_info)
457fb9987d0SSujith {
458fb9987d0SSujith 	struct ath_hw *ah = NULL;
459fb9987d0SSujith 	struct ath_common *common;
460832f6a18SSujith Manoharan 	int i, ret = 0, csz = 0;
461fb9987d0SSujith 
462d8a2c51cSSujith Manoharan 	set_bit(OP_INVALID, &priv->op_flags);
463fb9987d0SSujith 
464fb9987d0SSujith 	ah = kzalloc(sizeof(struct ath_hw), GFP_KERNEL);
465fb9987d0SSujith 	if (!ah)
466fb9987d0SSujith 		return -ENOMEM;
467fb9987d0SSujith 
46831f023a1SOleksij Rempel 	ah->dev = priv->dev;
469fb9987d0SSujith 	ah->hw_version.devid = devid;
4700b5ead91SSujith Manoharan 	ah->hw_version.usbdev = drv_info;
471f8afa42bSFelix Fietkau 	ah->ah_flags |= AH_USE_EEPROM;
472f9f84e96SFelix Fietkau 	ah->reg_ops.read = ath9k_regread;
473f9f84e96SFelix Fietkau 	ah->reg_ops.multi_read = ath9k_multi_regread;
474f9f84e96SFelix Fietkau 	ah->reg_ops.write = ath9k_regwrite;
475f9f84e96SFelix Fietkau 	ah->reg_ops.enable_write_buffer = ath9k_enable_regwrite_buffer;
476f9f84e96SFelix Fietkau 	ah->reg_ops.write_flush = ath9k_regwrite_flush;
477845e03c9SFelix Fietkau 	ah->reg_ops.rmw = ath9k_reg_rmw;
478fb9987d0SSujith 	priv->ah = ah;
479fb9987d0SSujith 
480fb9987d0SSujith 	common = ath9k_hw_common(ah);
481f9f84e96SFelix Fietkau 	common->ops = &ah->reg_ops;
482fb9987d0SSujith 	common->bus_ops = &ath9k_usb_bus_ops;
483fb9987d0SSujith 	common->ah = ah;
484fb9987d0SSujith 	common->hw = priv->hw;
485fb9987d0SSujith 	common->priv = priv;
486fb9987d0SSujith 	common->debug_mask = ath9k_debug;
4877f34778eSMohammed Shafi Shajakhan 	common->btcoex_enabled = ath9k_htc_btcoex_enable == 1;
488fb9987d0SSujith 
489fb9987d0SSujith 	spin_lock_init(&priv->beacon_lock);
490658ef04fSSujith Manoharan 	spin_lock_init(&priv->tx.tx_lock);
491fb9987d0SSujith 	mutex_init(&priv->mutex);
492bde748a4SVivek Natarajan 	mutex_init(&priv->htc_pm_lock);
493fb9987d0SSujith 	tasklet_init(&priv->rx_tasklet, ath9k_rx_tasklet,
494fb9987d0SSujith 		     (unsigned long)priv);
49527876a29SSujith Manoharan 	tasklet_init(&priv->tx_failed_tasklet, ath9k_tx_failed_tasklet,
49673908674SSujith Manoharan 		     (unsigned long)priv);
497a236254cSSujith Manoharan 	INIT_DELAYED_WORK(&priv->ani_work, ath9k_htc_ani_work);
498bde748a4SVivek Natarajan 	INIT_WORK(&priv->ps_work, ath9k_ps_work);
49973908674SSujith Manoharan 	INIT_WORK(&priv->fatal_work, ath9k_fatal_work);
500859c3ca1SSujith Manoharan 	setup_timer(&priv->tx.cleanup_timer, ath9k_htc_tx_cleanup_timer,
501859c3ca1SSujith Manoharan 		    (unsigned long)priv);
502fb9987d0SSujith 
503fb9987d0SSujith 	/*
504fb9987d0SSujith 	 * Cache line size is used to size and align various
505fb9987d0SSujith 	 * structures used to communicate with the hardware.
506fb9987d0SSujith 	 */
507fb9987d0SSujith 	ath_read_cachesize(common, &csz);
508fb9987d0SSujith 	common->cachelsz = csz << 2; /* convert to bytes */
509fb9987d0SSujith 
510fb9987d0SSujith 	ret = ath9k_hw_init(ah);
511fb9987d0SSujith 	if (ret) {
5123800276aSJoe Perches 		ath_err(common,
5133800276aSJoe Perches 			"Unable to initialize hardware; initialization status: %d\n",
5143800276aSJoe Perches 			ret);
515fb9987d0SSujith 		goto err_hw;
516fb9987d0SSujith 	}
517fb9987d0SSujith 
518fb9987d0SSujith 	ret = ath9k_init_queues(priv);
519fb9987d0SSujith 	if (ret)
520fb9987d0SSujith 		goto err_queues;
521fb9987d0SSujith 
522832f6a18SSujith Manoharan 	for (i = 0; i < ATH9K_HTC_MAX_BCN_VIF; i++)
523832f6a18SSujith Manoharan 		priv->cur_beacon_conf.bslot[i] = NULL;
524832f6a18SSujith Manoharan 
52531f023a1SOleksij Rempel 	ath9k_cmn_init_channels_rates(common);
526f82b4bdeSRajkumar Manoharan 	ath9k_cmn_init_crypto(ah);
527fb9987d0SSujith 	ath9k_init_misc(priv);
528cee5341dSSujith Manoharan 	ath9k_htc_init_btcoex(priv, product);
52921cb9879SVivek Natarajan 
530fb9987d0SSujith 	return 0;
531fb9987d0SSujith 
532fb9987d0SSujith err_queues:
533fb9987d0SSujith 	ath9k_hw_deinit(ah);
534fb9987d0SSujith err_hw:
535fb9987d0SSujith 
536fb9987d0SSujith 	kfree(ah);
537fb9987d0SSujith 	priv->ah = NULL;
538fb9987d0SSujith 
539fb9987d0SSujith 	return ret;
540fb9987d0SSujith }
541fb9987d0SSujith 
5428b0b6be5SMohammed Shafi Shajakhan static const struct ieee80211_iface_limit if_limits[] = {
5438b0b6be5SMohammed Shafi Shajakhan 	{ .max = 2,	.types = BIT(NL80211_IFTYPE_STATION) |
5448b0b6be5SMohammed Shafi Shajakhan 				 BIT(NL80211_IFTYPE_P2P_CLIENT) },
5458b0b6be5SMohammed Shafi Shajakhan 	{ .max = 2,	.types = BIT(NL80211_IFTYPE_AP) |
5460c9acaa8SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
5470c9acaa8SThomas Pedersen 				 BIT(NL80211_IFTYPE_MESH_POINT) |
5480c9acaa8SThomas Pedersen #endif
5490c9acaa8SThomas Pedersen 				 BIT(NL80211_IFTYPE_P2P_GO) },
5508b0b6be5SMohammed Shafi Shajakhan };
5518b0b6be5SMohammed Shafi Shajakhan 
5528b0b6be5SMohammed Shafi Shajakhan static const struct ieee80211_iface_combination if_comb = {
5538b0b6be5SMohammed Shafi Shajakhan 	.limits = if_limits,
5548b0b6be5SMohammed Shafi Shajakhan 	.n_limits = ARRAY_SIZE(if_limits),
5558b0b6be5SMohammed Shafi Shajakhan 	.max_interfaces = 2,
5568b0b6be5SMohammed Shafi Shajakhan 	.num_different_channels = 1,
5578b0b6be5SMohammed Shafi Shajakhan };
5588b0b6be5SMohammed Shafi Shajakhan 
559fb9987d0SSujith static void ath9k_set_hw_capab(struct ath9k_htc_priv *priv,
560fb9987d0SSujith 			       struct ieee80211_hw *hw)
561fb9987d0SSujith {
562e9fb5888SOleksij Rempel 	struct ath_hw *ah = priv->ah;
563fb9987d0SSujith 	struct ath_common *common = ath9k_hw_common(priv->ah);
564156652bbSBen Greear 	struct base_eep_header *pBase;
565fb9987d0SSujith 
566fb9987d0SSujith 	hw->flags = IEEE80211_HW_SIGNAL_DBM |
567fb9987d0SSujith 		IEEE80211_HW_AMPDU_AGGREGATION |
568fb9987d0SSujith 		IEEE80211_HW_SPECTRUM_MGMT |
56932fbccafSSujith 		IEEE80211_HW_HAS_RATE_CONTROL |
570bde748a4SVivek Natarajan 		IEEE80211_HW_RX_INCLUDES_FCS |
5717d547eb4SSujith Manoharan 		IEEE80211_HW_PS_NULLFUNC_STACK |
5728ae2e12fSRajkumar Manoharan 		IEEE80211_HW_REPORTS_TX_ACK_STATUS |
573bd4a85eeSJavier Cardona 		IEEE80211_HW_MFP_CAPABLE |
5747d547eb4SSujith Manoharan 		IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
575fb9987d0SSujith 
5766bca610dSOleksij Rempel 	if (ath9k_ps_enable)
5776bca610dSOleksij Rempel 		hw->flags |= IEEE80211_HW_SUPPORTS_PS;
5786bca610dSOleksij Rempel 
579fb9987d0SSujith 	hw->wiphy->interface_modes =
580fb9987d0SSujith 		BIT(NL80211_IFTYPE_STATION) |
58109d5b94dSSujith Manoharan 		BIT(NL80211_IFTYPE_ADHOC) |
58209d5b94dSSujith Manoharan 		BIT(NL80211_IFTYPE_AP) |
58309d5b94dSSujith Manoharan 		BIT(NL80211_IFTYPE_P2P_GO) |
584594e65b6SJavier Cardona 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
585594e65b6SJavier Cardona 		BIT(NL80211_IFTYPE_MESH_POINT);
586fb9987d0SSujith 
5878b0b6be5SMohammed Shafi Shajakhan 	hw->wiphy->iface_combinations = &if_comb;
5888b0b6be5SMohammed Shafi Shajakhan 	hw->wiphy->n_iface_combinations = 1;
5898b0b6be5SMohammed Shafi Shajakhan 
590bde748a4SVivek Natarajan 	hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
591bde748a4SVivek Natarajan 
59281ddbb5cSJohannes Berg 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN |
59381ddbb5cSJohannes Berg 			    WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
594d7d312caSAntonio Quartulli 
595fb9987d0SSujith 	hw->queues = 4;
596fb1c078eSSujith Manoharan 	hw->max_listen_interval = 1;
5973a0593efSSujith Manoharan 
598fb9987d0SSujith 	hw->vif_data_size = sizeof(struct ath9k_htc_vif);
599fb9987d0SSujith 	hw->sta_data_size = sizeof(struct ath9k_htc_sta);
600fb9987d0SSujith 
601fb9987d0SSujith 	/* tx_frame_hdr is larger than tx_mgmt_hdr anyway */
602fb9987d0SSujith 	hw->extra_tx_headroom = sizeof(struct tx_frame_hdr) +
603fb9987d0SSujith 		sizeof(struct htc_frame_hdr) + 4;
604fb9987d0SSujith 
605d4659912SFelix Fietkau 	if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ)
606fb9987d0SSujith 		hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
60731f023a1SOleksij Rempel 			&common->sbands[IEEE80211_BAND_2GHZ];
608d4659912SFelix Fietkau 	if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ)
609ea46e644SSujith 		hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
61031f023a1SOleksij Rempel 			&common->sbands[IEEE80211_BAND_5GHZ];
611fb9987d0SSujith 
6126da2f4adSOleksij Rempel 	ath9k_cmn_reload_chainmask(ah);
613fb9987d0SSujith 
614156652bbSBen Greear 	pBase = ath9k_htc_get_eeprom_base(priv);
615156652bbSBen Greear 	if (pBase) {
616156652bbSBen Greear 		hw->wiphy->available_antennas_rx = pBase->rxMask;
617156652bbSBen Greear 		hw->wiphy->available_antennas_tx = pBase->txMask;
618156652bbSBen Greear 	}
619156652bbSBen Greear 
620fb9987d0SSujith 	SET_IEEE80211_PERM_ADDR(hw, common->macaddr);
621fb9987d0SSujith }
622fb9987d0SSujith 
62329bbfb24SSujith Manoharan static int ath9k_init_firmware_version(struct ath9k_htc_priv *priv)
62429bbfb24SSujith Manoharan {
62529bbfb24SSujith Manoharan 	struct ieee80211_hw *hw = priv->hw;
62629bbfb24SSujith Manoharan 	struct wmi_fw_version cmd_rsp;
62729bbfb24SSujith Manoharan 	int ret;
62829bbfb24SSujith Manoharan 
62929bbfb24SSujith Manoharan 	memset(&cmd_rsp, 0, sizeof(cmd_rsp));
63029bbfb24SSujith Manoharan 
63129bbfb24SSujith Manoharan 	WMI_CMD(WMI_GET_FW_VERSION);
63229bbfb24SSujith Manoharan 	if (ret)
63329bbfb24SSujith Manoharan 		return -EINVAL;
63429bbfb24SSujith Manoharan 
63529bbfb24SSujith Manoharan 	priv->fw_version_major = be16_to_cpu(cmd_rsp.major);
63629bbfb24SSujith Manoharan 	priv->fw_version_minor = be16_to_cpu(cmd_rsp.minor);
63729bbfb24SSujith Manoharan 
63881135548SJiri Pirko 	snprintf(hw->wiphy->fw_version, sizeof(hw->wiphy->fw_version), "%d.%d",
63929bbfb24SSujith Manoharan 		 priv->fw_version_major,
64029bbfb24SSujith Manoharan 		 priv->fw_version_minor);
64129bbfb24SSujith Manoharan 
64229bbfb24SSujith Manoharan 	dev_info(priv->dev, "ath9k_htc: FW Version: %d.%d\n",
64329bbfb24SSujith Manoharan 		 priv->fw_version_major,
64429bbfb24SSujith Manoharan 		 priv->fw_version_minor);
64529bbfb24SSujith Manoharan 
6463a0593efSSujith Manoharan 	/*
6473a0593efSSujith Manoharan 	 * Check if the available FW matches the driver's
6483a0593efSSujith Manoharan 	 * required version.
6493a0593efSSujith Manoharan 	 */
6503a0593efSSujith Manoharan 	if (priv->fw_version_major != MAJOR_VERSION_REQ ||
651319e7bd9SFelix Fietkau 	    priv->fw_version_minor < MINOR_VERSION_REQ) {
6523a0593efSSujith Manoharan 		dev_err(priv->dev, "ath9k_htc: Please upgrade to FW version %d.%d\n",
6533a0593efSSujith Manoharan 			MAJOR_VERSION_REQ, MINOR_VERSION_REQ);
6543a0593efSSujith Manoharan 		return -EINVAL;
6553a0593efSSujith Manoharan 	}
6563a0593efSSujith Manoharan 
65729bbfb24SSujith Manoharan 	return 0;
65829bbfb24SSujith Manoharan }
65929bbfb24SSujith Manoharan 
66021cb9879SVivek Natarajan static int ath9k_init_device(struct ath9k_htc_priv *priv,
661fa6e15e0SRajkumar Manoharan 			     u16 devid, char *product, u32 drv_info)
662fb9987d0SSujith {
663fb9987d0SSujith 	struct ieee80211_hw *hw = priv->hw;
664fb9987d0SSujith 	struct ath_common *common;
665fb9987d0SSujith 	struct ath_hw *ah;
666fb9987d0SSujith 	int error = 0;
667fb9987d0SSujith 	struct ath_regulatory *reg;
6683e3f1d19SSujith Manoharan 	char hw_name[64];
669fb9987d0SSujith 
670fb9987d0SSujith 	/* Bring up device */
671fa6e15e0SRajkumar Manoharan 	error = ath9k_init_priv(priv, devid, product, drv_info);
672fb9987d0SSujith 	if (error != 0)
673fb9987d0SSujith 		goto err_init;
674fb9987d0SSujith 
675fb9987d0SSujith 	ah = priv->ah;
676fb9987d0SSujith 	common = ath9k_hw_common(ah);
677fb9987d0SSujith 	ath9k_set_hw_capab(priv, hw);
678fb9987d0SSujith 
67929bbfb24SSujith Manoharan 	error = ath9k_init_firmware_version(priv);
68029bbfb24SSujith Manoharan 	if (error != 0)
68129bbfb24SSujith Manoharan 		goto err_fw;
68229bbfb24SSujith Manoharan 
683fb9987d0SSujith 	/* Initialize regulatory */
684fb9987d0SSujith 	error = ath_regd_init(&common->regulatory, priv->hw->wiphy,
685fb9987d0SSujith 			      ath9k_reg_notifier);
686fb9987d0SSujith 	if (error)
687fb9987d0SSujith 		goto err_regd;
688fb9987d0SSujith 
689fb9987d0SSujith 	reg = &common->regulatory;
690fb9987d0SSujith 
691fb9987d0SSujith 	/* Setup TX */
692fb9987d0SSujith 	error = ath9k_tx_init(priv);
693fb9987d0SSujith 	if (error != 0)
694fb9987d0SSujith 		goto err_tx;
695fb9987d0SSujith 
696fb9987d0SSujith 	/* Setup RX */
697fb9987d0SSujith 	error = ath9k_rx_init(priv);
698fb9987d0SSujith 	if (error != 0)
699fb9987d0SSujith 		goto err_rx;
700fb9987d0SSujith 
701dc2a87f5SOleksij Rempel 	ath9k_hw_disable(priv->ah);
702d244f21eSSujith Manoharan #ifdef CONFIG_MAC80211_LEDS
703d244f21eSSujith Manoharan 	/* must be initialized before ieee80211_register_hw */
704d244f21eSSujith Manoharan 	priv->led_cdev.default_trigger = ieee80211_create_tpt_led_trigger(priv->hw,
705d244f21eSSujith Manoharan 		IEEE80211_TPT_LEDTRIG_FL_RADIO, ath9k_htc_tpt_blink,
706d244f21eSSujith Manoharan 		ARRAY_SIZE(ath9k_htc_tpt_blink));
707d244f21eSSujith Manoharan #endif
708d244f21eSSujith Manoharan 
709fb9987d0SSujith 	/* Register with mac80211 */
710fb9987d0SSujith 	error = ieee80211_register_hw(hw);
711fb9987d0SSujith 	if (error)
712fb9987d0SSujith 		goto err_register;
713fb9987d0SSujith 
714fb9987d0SSujith 	/* Handle world regulatory */
715fb9987d0SSujith 	if (!ath_is_world_regd(reg)) {
716fb9987d0SSujith 		error = regulatory_hint(hw->wiphy, reg->alpha2);
717fb9987d0SSujith 		if (error)
718fb9987d0SSujith 			goto err_world;
719fb9987d0SSujith 	}
720fb9987d0SSujith 
721e5facc75SRajkumar Manoharan 	error = ath9k_htc_init_debug(priv->ah);
722e5facc75SRajkumar Manoharan 	if (error) {
723e5facc75SRajkumar Manoharan 		ath_err(common, "Unable to create debugfs files\n");
724e5facc75SRajkumar Manoharan 		goto err_world;
725e5facc75SRajkumar Manoharan 	}
726e5facc75SRajkumar Manoharan 
727d2182b69SJoe Perches 	ath_dbg(common, CONFIG,
728d2182b69SJoe Perches 		"WMI:%d, BCN:%d, CAB:%d, UAPSD:%d, MGMT:%d, BE:%d, BK:%d, VI:%d, VO:%d\n",
7293e3f1d19SSujith Manoharan 		priv->wmi_cmd_ep,
7303e3f1d19SSujith Manoharan 		priv->beacon_ep,
7313e3f1d19SSujith Manoharan 		priv->cab_ep,
7323e3f1d19SSujith Manoharan 		priv->uapsd_ep,
7333e3f1d19SSujith Manoharan 		priv->mgmt_ep,
7343e3f1d19SSujith Manoharan 		priv->data_be_ep,
7353e3f1d19SSujith Manoharan 		priv->data_bk_ep,
7363e3f1d19SSujith Manoharan 		priv->data_vi_ep,
7373e3f1d19SSujith Manoharan 		priv->data_vo_ep);
7383e3f1d19SSujith Manoharan 
7393e3f1d19SSujith Manoharan 	ath9k_hw_name(priv->ah, hw_name, sizeof(hw_name));
7403e3f1d19SSujith Manoharan 	wiphy_info(hw->wiphy, "%s\n", hw_name);
7413e3f1d19SSujith Manoharan 
742fb9987d0SSujith 	ath9k_init_leds(priv);
743fb9987d0SSujith 	ath9k_start_rfkill_poll(priv);
744fb9987d0SSujith 
745fb9987d0SSujith 	return 0;
746fb9987d0SSujith 
747fb9987d0SSujith err_world:
748fb9987d0SSujith 	ieee80211_unregister_hw(hw);
749fb9987d0SSujith err_register:
750fb9987d0SSujith 	ath9k_rx_cleanup(priv);
751fb9987d0SSujith err_rx:
752fb9987d0SSujith 	ath9k_tx_cleanup(priv);
753fb9987d0SSujith err_tx:
754fb9987d0SSujith 	/* Nothing */
755fb9987d0SSujith err_regd:
75629bbfb24SSujith Manoharan 	/* Nothing */
75729bbfb24SSujith Manoharan err_fw:
758fb9987d0SSujith 	ath9k_deinit_priv(priv);
759fb9987d0SSujith err_init:
760fb9987d0SSujith 	return error;
761fb9987d0SSujith }
762fb9987d0SSujith 
763fb9987d0SSujith int ath9k_htc_probe_device(struct htc_target *htc_handle, struct device *dev,
764fa6e15e0SRajkumar Manoharan 			   u16 devid, char *product, u32 drv_info)
765fb9987d0SSujith {
766fb9987d0SSujith 	struct ieee80211_hw *hw;
767fb9987d0SSujith 	struct ath9k_htc_priv *priv;
768fb9987d0SSujith 	int ret;
769fb9987d0SSujith 
770fb9987d0SSujith 	hw = ieee80211_alloc_hw(sizeof(struct ath9k_htc_priv), &ath9k_htc_ops);
771fb9987d0SSujith 	if (!hw)
772fb9987d0SSujith 		return -ENOMEM;
773fb9987d0SSujith 
774fb9987d0SSujith 	priv = hw->priv;
775fb9987d0SSujith 	priv->hw = hw;
776fb9987d0SSujith 	priv->htc = htc_handle;
777fb9987d0SSujith 	priv->dev = dev;
778fb9987d0SSujith 	htc_handle->drv_priv = priv;
779fb9987d0SSujith 	SET_IEEE80211_DEV(hw, priv->dev);
780fb9987d0SSujith 
781fb9987d0SSujith 	ret = ath9k_htc_wait_for_target(priv);
782fb9987d0SSujith 	if (ret)
783fb9987d0SSujith 		goto err_free;
784fb9987d0SSujith 
785fb9987d0SSujith 	priv->wmi = ath9k_init_wmi(priv);
786fb9987d0SSujith 	if (!priv->wmi) {
787fb9987d0SSujith 		ret = -EINVAL;
788fb9987d0SSujith 		goto err_free;
789fb9987d0SSujith 	}
790fb9987d0SSujith 
791fa6e15e0SRajkumar Manoharan 	ret = ath9k_init_htc_services(priv, devid, drv_info);
792fb9987d0SSujith 	if (ret)
793fb9987d0SSujith 		goto err_init;
794fb9987d0SSujith 
795fa6e15e0SRajkumar Manoharan 	ret = ath9k_init_device(priv, devid, product, drv_info);
796fb9987d0SSujith 	if (ret)
797fb9987d0SSujith 		goto err_init;
798fb9987d0SSujith 
799fb9987d0SSujith 	return 0;
800fb9987d0SSujith 
801fb9987d0SSujith err_init:
802fb9987d0SSujith 	ath9k_deinit_wmi(priv);
803fb9987d0SSujith err_free:
804fb9987d0SSujith 	ieee80211_free_hw(hw);
805fb9987d0SSujith 	return ret;
806fb9987d0SSujith }
807fb9987d0SSujith 
808fb9987d0SSujith void ath9k_htc_disconnect_device(struct htc_target *htc_handle, bool hotunplug)
809fb9987d0SSujith {
810fb9987d0SSujith 	if (htc_handle->drv_priv) {
811a3be14b7SSujith 
812a3be14b7SSujith 		/* Check if the device has been yanked out. */
813a3be14b7SSujith 		if (hotunplug)
81497dcec57SSujith Manoharan 			htc_handle->drv_priv->ah->ah_flags |= AH_UNPLUGGED;
815a3be14b7SSujith 
816fb9987d0SSujith 		ath9k_deinit_device(htc_handle->drv_priv);
817fb9987d0SSujith 		ath9k_deinit_wmi(htc_handle->drv_priv);
818fb9987d0SSujith 		ieee80211_free_hw(htc_handle->drv_priv->hw);
819fb9987d0SSujith 	}
820fb9987d0SSujith }
821fb9987d0SSujith 
822fb9987d0SSujith #ifdef CONFIG_PM
823f933ebedSSujith Manoharan 
824f933ebedSSujith Manoharan void ath9k_htc_suspend(struct htc_target *htc_handle)
825f933ebedSSujith Manoharan {
826f933ebedSSujith Manoharan 	ath9k_htc_setpower(htc_handle->drv_priv, ATH9K_PM_FULL_SLEEP);
827f933ebedSSujith Manoharan }
828f933ebedSSujith Manoharan 
829fb9987d0SSujith int ath9k_htc_resume(struct htc_target *htc_handle)
830fb9987d0SSujith {
831fa6e15e0SRajkumar Manoharan 	struct ath9k_htc_priv *priv = htc_handle->drv_priv;
832fb9987d0SSujith 	int ret;
833fb9987d0SSujith 
834fa6e15e0SRajkumar Manoharan 	ret = ath9k_htc_wait_for_target(priv);
835fb9987d0SSujith 	if (ret)
836fb9987d0SSujith 		return ret;
837fb9987d0SSujith 
838fa6e15e0SRajkumar Manoharan 	ret = ath9k_init_htc_services(priv, priv->ah->hw_version.devid,
8390b5ead91SSujith Manoharan 				      priv->ah->hw_version.usbdev);
8401e51acaaSOleksij Rempel 	ath9k_configure_leds(priv);
8411e51acaaSOleksij Rempel 
842fb9987d0SSujith 	return ret;
843fb9987d0SSujith }
844fb9987d0SSujith #endif
845fb9987d0SSujith 
846fb9987d0SSujith static int __init ath9k_htc_init(void)
847fb9987d0SSujith {
848e5facc75SRajkumar Manoharan 	if (ath9k_hif_usb_init() < 0) {
849516304b0SJoe Perches 		pr_err("No USB devices found, driver not installed\n");
850e5facc75SRajkumar Manoharan 		return -ENODEV;
851fb9987d0SSujith 	}
852fb9987d0SSujith 
853fb9987d0SSujith 	return 0;
854fb9987d0SSujith }
855fb9987d0SSujith module_init(ath9k_htc_init);
856fb9987d0SSujith 
857fb9987d0SSujith static void __exit ath9k_htc_exit(void)
858fb9987d0SSujith {
859fb9987d0SSujith 	ath9k_hif_usb_exit();
860516304b0SJoe Perches 	pr_info("Driver unloaded\n");
861fb9987d0SSujith }
862fb9987d0SSujith module_exit(ath9k_htc_exit);
863