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 
2594a22fe10SSujith static void ath9k_regwrite_single(void *hw_priv, u32 val, u32 reg_offset)
260fb9987d0SSujith {
261fb9987d0SSujith 	struct ath_hw *ah = (struct ath_hw *) hw_priv;
262fb9987d0SSujith 	struct ath_common *common = ath9k_hw_common(ah);
263fb9987d0SSujith 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
26407b2fa5aSJoe Perches 	const __be32 buf[2] = {
265fb9987d0SSujith 		cpu_to_be32(reg_offset),
266fb9987d0SSujith 		cpu_to_be32(val),
267fb9987d0SSujith 	};
268fb9987d0SSujith 	int r;
269fb9987d0SSujith 
270fb9987d0SSujith 	r = ath9k_wmi_cmd(priv->wmi, WMI_REG_WRITE_CMDID,
271fb9987d0SSujith 			  (u8 *) &buf, sizeof(buf),
272fb9987d0SSujith 			  (u8 *) &val, sizeof(val),
273fb9987d0SSujith 			  100);
274fb9987d0SSujith 	if (unlikely(r)) {
275d2182b69SJoe Perches 		ath_dbg(common, WMI, "REGISTER WRITE FAILED:(0x%04x, %d)\n",
276fb9987d0SSujith 			reg_offset, r);
277fb9987d0SSujith 	}
278fb9987d0SSujith }
279fb9987d0SSujith 
2804a22fe10SSujith static void ath9k_regwrite_buffer(void *hw_priv, u32 val, u32 reg_offset)
2814a22fe10SSujith {
2824a22fe10SSujith 	struct ath_hw *ah = (struct ath_hw *) hw_priv;
2834a22fe10SSujith 	struct ath_common *common = ath9k_hw_common(ah);
2844a22fe10SSujith 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
2854a22fe10SSujith 	u32 rsp_status;
2864a22fe10SSujith 	int r;
2874a22fe10SSujith 
2884a22fe10SSujith 	mutex_lock(&priv->wmi->multi_write_mutex);
2894a22fe10SSujith 
2904a22fe10SSujith 	/* Store the register/value */
2914a22fe10SSujith 	priv->wmi->multi_write[priv->wmi->multi_write_idx].reg =
2924a22fe10SSujith 		cpu_to_be32(reg_offset);
2934a22fe10SSujith 	priv->wmi->multi_write[priv->wmi->multi_write_idx].val =
2944a22fe10SSujith 		cpu_to_be32(val);
2954a22fe10SSujith 
2964a22fe10SSujith 	priv->wmi->multi_write_idx++;
2974a22fe10SSujith 
2984a22fe10SSujith 	/* If the buffer is full, send it out. */
2994a22fe10SSujith 	if (priv->wmi->multi_write_idx == MAX_CMD_NUMBER) {
3004a22fe10SSujith 		r = ath9k_wmi_cmd(priv->wmi, WMI_REG_WRITE_CMDID,
3014a22fe10SSujith 			  (u8 *) &priv->wmi->multi_write,
3024a22fe10SSujith 			  sizeof(struct register_write) * priv->wmi->multi_write_idx,
3034a22fe10SSujith 			  (u8 *) &rsp_status, sizeof(rsp_status),
3044a22fe10SSujith 			  100);
3054a22fe10SSujith 		if (unlikely(r)) {
306d2182b69SJoe Perches 			ath_dbg(common, WMI,
3074a22fe10SSujith 				"REGISTER WRITE FAILED, multi len: %d\n",
3084a22fe10SSujith 				priv->wmi->multi_write_idx);
3094a22fe10SSujith 		}
3104a22fe10SSujith 		priv->wmi->multi_write_idx = 0;
3114a22fe10SSujith 	}
3124a22fe10SSujith 
3134a22fe10SSujith 	mutex_unlock(&priv->wmi->multi_write_mutex);
3144a22fe10SSujith }
3154a22fe10SSujith 
3164a22fe10SSujith static void ath9k_regwrite(void *hw_priv, u32 val, u32 reg_offset)
3174a22fe10SSujith {
3184a22fe10SSujith 	struct ath_hw *ah = (struct ath_hw *) hw_priv;
3194a22fe10SSujith 	struct ath_common *common = ath9k_hw_common(ah);
3204a22fe10SSujith 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
3214a22fe10SSujith 
3224a22fe10SSujith 	if (atomic_read(&priv->wmi->mwrite_cnt))
3234a22fe10SSujith 		ath9k_regwrite_buffer(hw_priv, val, reg_offset);
3244a22fe10SSujith 	else
3254a22fe10SSujith 		ath9k_regwrite_single(hw_priv, val, reg_offset);
3264a22fe10SSujith }
3274a22fe10SSujith 
3284a22fe10SSujith static void ath9k_enable_regwrite_buffer(void *hw_priv)
3294a22fe10SSujith {
3304a22fe10SSujith 	struct ath_hw *ah = (struct ath_hw *) hw_priv;
3314a22fe10SSujith 	struct ath_common *common = ath9k_hw_common(ah);
3324a22fe10SSujith 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
3334a22fe10SSujith 
3344a22fe10SSujith 	atomic_inc(&priv->wmi->mwrite_cnt);
3354a22fe10SSujith }
3364a22fe10SSujith 
3374a22fe10SSujith static void ath9k_regwrite_flush(void *hw_priv)
3384a22fe10SSujith {
3394a22fe10SSujith 	struct ath_hw *ah = (struct ath_hw *) hw_priv;
3404a22fe10SSujith 	struct ath_common *common = ath9k_hw_common(ah);
3414a22fe10SSujith 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
3424a22fe10SSujith 	u32 rsp_status;
3434a22fe10SSujith 	int r;
3444a22fe10SSujith 
345435c1610SFelix Fietkau 	atomic_dec(&priv->wmi->mwrite_cnt);
346435c1610SFelix Fietkau 
3474a22fe10SSujith 	mutex_lock(&priv->wmi->multi_write_mutex);
3484a22fe10SSujith 
3494a22fe10SSujith 	if (priv->wmi->multi_write_idx) {
3504a22fe10SSujith 		r = ath9k_wmi_cmd(priv->wmi, WMI_REG_WRITE_CMDID,
3514a22fe10SSujith 			  (u8 *) &priv->wmi->multi_write,
3524a22fe10SSujith 			  sizeof(struct register_write) * priv->wmi->multi_write_idx,
3534a22fe10SSujith 			  (u8 *) &rsp_status, sizeof(rsp_status),
3544a22fe10SSujith 			  100);
3554a22fe10SSujith 		if (unlikely(r)) {
356d2182b69SJoe Perches 			ath_dbg(common, WMI,
3574a22fe10SSujith 				"REGISTER WRITE FAILED, multi len: %d\n",
3584a22fe10SSujith 				priv->wmi->multi_write_idx);
3594a22fe10SSujith 		}
3604a22fe10SSujith 		priv->wmi->multi_write_idx = 0;
3614a22fe10SSujith 	}
3624a22fe10SSujith 
3634a22fe10SSujith 	mutex_unlock(&priv->wmi->multi_write_mutex);
3644a22fe10SSujith }
3654a22fe10SSujith 
366845e03c9SFelix Fietkau static u32 ath9k_reg_rmw(void *hw_priv, u32 reg_offset, u32 set, u32 clr)
367845e03c9SFelix Fietkau {
368845e03c9SFelix Fietkau 	u32 val;
369845e03c9SFelix Fietkau 
370845e03c9SFelix Fietkau 	val = ath9k_regread(hw_priv, reg_offset);
371845e03c9SFelix Fietkau 	val &= ~clr;
372845e03c9SFelix Fietkau 	val |= set;
373845e03c9SFelix Fietkau 	ath9k_regwrite(hw_priv, val, reg_offset);
374845e03c9SFelix Fietkau 	return val;
375845e03c9SFelix Fietkau }
376845e03c9SFelix Fietkau 
377fb9987d0SSujith static void ath_usb_read_cachesize(struct ath_common *common, int *csz)
378fb9987d0SSujith {
379fb9987d0SSujith 	*csz = L1_CACHE_BYTES >> 2;
380fb9987d0SSujith }
381fb9987d0SSujith 
382fb9987d0SSujith static bool ath_usb_eeprom_read(struct ath_common *common, u32 off, u16 *data)
383fb9987d0SSujith {
384fb9987d0SSujith 	struct ath_hw *ah = (struct ath_hw *) common->ah;
385fb9987d0SSujith 
386fb9987d0SSujith 	(void)REG_READ(ah, AR5416_EEPROM_OFFSET + (off << AR5416_EEPROM_S));
387fb9987d0SSujith 
388fb9987d0SSujith 	if (!ath9k_hw_wait(ah,
389fb9987d0SSujith 			   AR_EEPROM_STATUS_DATA,
390fb9987d0SSujith 			   AR_EEPROM_STATUS_DATA_BUSY |
391fb9987d0SSujith 			   AR_EEPROM_STATUS_DATA_PROT_ACCESS, 0,
392fb9987d0SSujith 			   AH_WAIT_TIMEOUT))
393fb9987d0SSujith 		return false;
394fb9987d0SSujith 
395fb9987d0SSujith 	*data = MS(REG_READ(ah, AR_EEPROM_STATUS_DATA),
396fb9987d0SSujith 		   AR_EEPROM_STATUS_DATA_VAL);
397fb9987d0SSujith 
398fb9987d0SSujith 	return true;
399fb9987d0SSujith }
400fb9987d0SSujith 
401fb9987d0SSujith static const struct ath_bus_ops ath9k_usb_bus_ops = {
402497ad9adSSujith 	.ath_bus_type = ATH_USB,
403fb9987d0SSujith 	.read_cachesize = ath_usb_read_cachesize,
404fb9987d0SSujith 	.eeprom_read = ath_usb_eeprom_read,
405fb9987d0SSujith };
406fb9987d0SSujith 
407fb9987d0SSujith static int ath9k_init_queues(struct ath9k_htc_priv *priv)
408fb9987d0SSujith {
409fb9987d0SSujith 	struct ath_common *common = ath9k_hw_common(priv->ah);
410fb9987d0SSujith 	int i;
411fb9987d0SSujith 
412fb9987d0SSujith 	for (i = 0; i < ARRAY_SIZE(priv->hwq_map); i++)
413fb9987d0SSujith 		priv->hwq_map[i] = -1;
414fb9987d0SSujith 
415ca74b83bSSujith 	priv->beaconq = ath9k_hw_beaconq_setup(priv->ah);
416ca74b83bSSujith 	if (priv->beaconq == -1) {
4173800276aSJoe Perches 		ath_err(common, "Unable to setup BEACON xmit queue\n");
418ca74b83bSSujith 		goto err;
419ca74b83bSSujith 	}
420ca74b83bSSujith 
421ca74b83bSSujith 	priv->cabq = ath9k_htc_cabq_setup(priv);
422ca74b83bSSujith 	if (priv->cabq == -1) {
4233800276aSJoe Perches 		ath_err(common, "Unable to setup CAB xmit queue\n");
424ca74b83bSSujith 		goto err;
425ca74b83bSSujith 	}
426ca74b83bSSujith 
427bea843c7SSujith Manoharan 	if (!ath9k_htc_txq_setup(priv, IEEE80211_AC_BE)) {
4283800276aSJoe Perches 		ath_err(common, "Unable to setup xmit queue for BE traffic\n");
429fb9987d0SSujith 		goto err;
430fb9987d0SSujith 	}
431fb9987d0SSujith 
432bea843c7SSujith Manoharan 	if (!ath9k_htc_txq_setup(priv, IEEE80211_AC_BK)) {
4333800276aSJoe Perches 		ath_err(common, "Unable to setup xmit queue for BK traffic\n");
434fb9987d0SSujith 		goto err;
435fb9987d0SSujith 	}
436bea843c7SSujith Manoharan 	if (!ath9k_htc_txq_setup(priv, IEEE80211_AC_VI)) {
4373800276aSJoe Perches 		ath_err(common, "Unable to setup xmit queue for VI traffic\n");
438fb9987d0SSujith 		goto err;
439fb9987d0SSujith 	}
440bea843c7SSujith Manoharan 	if (!ath9k_htc_txq_setup(priv, IEEE80211_AC_VO)) {
4413800276aSJoe Perches 		ath_err(common, "Unable to setup xmit queue for VO traffic\n");
442fb9987d0SSujith 		goto err;
443fb9987d0SSujith 	}
444fb9987d0SSujith 
445fb9987d0SSujith 	return 0;
446fb9987d0SSujith 
447fb9987d0SSujith err:
448fb9987d0SSujith 	return -EINVAL;
449fb9987d0SSujith }
450fb9987d0SSujith 
451fb9987d0SSujith static void ath9k_init_misc(struct ath9k_htc_priv *priv)
452fb9987d0SSujith {
453fb9987d0SSujith 	struct ath_common *common = ath9k_hw_common(priv->ah);
454fb9987d0SSujith 
455fb9987d0SSujith 	memcpy(common->bssidmask, ath_bcast_mac, ETH_ALEN);
456fb9987d0SSujith 
457e5ba18c6SOleksij Rempel 	common->last_rssi = ATH_RSSI_DUMMY_MARKER;
4589f01a84eSSujith 	priv->ah->opmode = NL80211_IFTYPE_STATION;
459fb9987d0SSujith }
460fb9987d0SSujith 
46121cb9879SVivek Natarajan static int ath9k_init_priv(struct ath9k_htc_priv *priv,
462fa6e15e0SRajkumar Manoharan 			   u16 devid, char *product,
463fa6e15e0SRajkumar Manoharan 			   u32 drv_info)
464fb9987d0SSujith {
465fb9987d0SSujith 	struct ath_hw *ah = NULL;
466fb9987d0SSujith 	struct ath_common *common;
467832f6a18SSujith Manoharan 	int i, ret = 0, csz = 0;
468fb9987d0SSujith 
469d8a2c51cSSujith Manoharan 	set_bit(OP_INVALID, &priv->op_flags);
470fb9987d0SSujith 
471fb9987d0SSujith 	ah = kzalloc(sizeof(struct ath_hw), GFP_KERNEL);
472fb9987d0SSujith 	if (!ah)
473fb9987d0SSujith 		return -ENOMEM;
474fb9987d0SSujith 
47531f023a1SOleksij Rempel 	ah->dev = priv->dev;
476fb9987d0SSujith 	ah->hw_version.devid = devid;
4770b5ead91SSujith Manoharan 	ah->hw_version.usbdev = drv_info;
478f8afa42bSFelix Fietkau 	ah->ah_flags |= AH_USE_EEPROM;
479f9f84e96SFelix Fietkau 	ah->reg_ops.read = ath9k_regread;
480f9f84e96SFelix Fietkau 	ah->reg_ops.multi_read = ath9k_multi_regread;
481f9f84e96SFelix Fietkau 	ah->reg_ops.write = ath9k_regwrite;
482f9f84e96SFelix Fietkau 	ah->reg_ops.enable_write_buffer = ath9k_enable_regwrite_buffer;
483f9f84e96SFelix Fietkau 	ah->reg_ops.write_flush = ath9k_regwrite_flush;
484845e03c9SFelix Fietkau 	ah->reg_ops.rmw = ath9k_reg_rmw;
485fb9987d0SSujith 	priv->ah = ah;
486fb9987d0SSujith 
487fb9987d0SSujith 	common = ath9k_hw_common(ah);
488f9f84e96SFelix Fietkau 	common->ops = &ah->reg_ops;
489fb9987d0SSujith 	common->bus_ops = &ath9k_usb_bus_ops;
490fb9987d0SSujith 	common->ah = ah;
491fb9987d0SSujith 	common->hw = priv->hw;
492fb9987d0SSujith 	common->priv = priv;
493fb9987d0SSujith 	common->debug_mask = ath9k_debug;
4947f34778eSMohammed Shafi Shajakhan 	common->btcoex_enabled = ath9k_htc_btcoex_enable == 1;
495fb9987d0SSujith 
496fb9987d0SSujith 	spin_lock_init(&priv->beacon_lock);
497658ef04fSSujith Manoharan 	spin_lock_init(&priv->tx.tx_lock);
498fb9987d0SSujith 	mutex_init(&priv->mutex);
499bde748a4SVivek Natarajan 	mutex_init(&priv->htc_pm_lock);
500fb9987d0SSujith 	tasklet_init(&priv->rx_tasklet, ath9k_rx_tasklet,
501fb9987d0SSujith 		     (unsigned long)priv);
50227876a29SSujith Manoharan 	tasklet_init(&priv->tx_failed_tasklet, ath9k_tx_failed_tasklet,
50373908674SSujith Manoharan 		     (unsigned long)priv);
504a236254cSSujith Manoharan 	INIT_DELAYED_WORK(&priv->ani_work, ath9k_htc_ani_work);
505bde748a4SVivek Natarajan 	INIT_WORK(&priv->ps_work, ath9k_ps_work);
50673908674SSujith Manoharan 	INIT_WORK(&priv->fatal_work, ath9k_fatal_work);
507859c3ca1SSujith Manoharan 	setup_timer(&priv->tx.cleanup_timer, ath9k_htc_tx_cleanup_timer,
508859c3ca1SSujith Manoharan 		    (unsigned long)priv);
509fb9987d0SSujith 
510fb9987d0SSujith 	/*
511fb9987d0SSujith 	 * Cache line size is used to size and align various
512fb9987d0SSujith 	 * structures used to communicate with the hardware.
513fb9987d0SSujith 	 */
514fb9987d0SSujith 	ath_read_cachesize(common, &csz);
515fb9987d0SSujith 	common->cachelsz = csz << 2; /* convert to bytes */
516fb9987d0SSujith 
517fb9987d0SSujith 	ret = ath9k_hw_init(ah);
518fb9987d0SSujith 	if (ret) {
5193800276aSJoe Perches 		ath_err(common,
5203800276aSJoe Perches 			"Unable to initialize hardware; initialization status: %d\n",
5213800276aSJoe Perches 			ret);
522fb9987d0SSujith 		goto err_hw;
523fb9987d0SSujith 	}
524fb9987d0SSujith 
525fb9987d0SSujith 	ret = ath9k_init_queues(priv);
526fb9987d0SSujith 	if (ret)
527fb9987d0SSujith 		goto err_queues;
528fb9987d0SSujith 
529832f6a18SSujith Manoharan 	for (i = 0; i < ATH9K_HTC_MAX_BCN_VIF; i++)
530832f6a18SSujith Manoharan 		priv->cur_beacon_conf.bslot[i] = NULL;
531832f6a18SSujith Manoharan 
53231f023a1SOleksij Rempel 	ath9k_cmn_init_channels_rates(common);
533f82b4bdeSRajkumar Manoharan 	ath9k_cmn_init_crypto(ah);
534fb9987d0SSujith 	ath9k_init_misc(priv);
535cee5341dSSujith Manoharan 	ath9k_htc_init_btcoex(priv, product);
53621cb9879SVivek Natarajan 
537fb9987d0SSujith 	return 0;
538fb9987d0SSujith 
539fb9987d0SSujith err_queues:
540fb9987d0SSujith 	ath9k_hw_deinit(ah);
541fb9987d0SSujith err_hw:
542fb9987d0SSujith 
543fb9987d0SSujith 	kfree(ah);
544fb9987d0SSujith 	priv->ah = NULL;
545fb9987d0SSujith 
546fb9987d0SSujith 	return ret;
547fb9987d0SSujith }
548fb9987d0SSujith 
5498b0b6be5SMohammed Shafi Shajakhan static const struct ieee80211_iface_limit if_limits[] = {
5508b0b6be5SMohammed Shafi Shajakhan 	{ .max = 2,	.types = BIT(NL80211_IFTYPE_STATION) |
5518b0b6be5SMohammed Shafi Shajakhan 				 BIT(NL80211_IFTYPE_P2P_CLIENT) },
5528b0b6be5SMohammed Shafi Shajakhan 	{ .max = 2,	.types = BIT(NL80211_IFTYPE_AP) |
5530c9acaa8SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
5540c9acaa8SThomas Pedersen 				 BIT(NL80211_IFTYPE_MESH_POINT) |
5550c9acaa8SThomas Pedersen #endif
5560c9acaa8SThomas Pedersen 				 BIT(NL80211_IFTYPE_P2P_GO) },
5578b0b6be5SMohammed Shafi Shajakhan };
5588b0b6be5SMohammed Shafi Shajakhan 
5598b0b6be5SMohammed Shafi Shajakhan static const struct ieee80211_iface_combination if_comb = {
5608b0b6be5SMohammed Shafi Shajakhan 	.limits = if_limits,
5618b0b6be5SMohammed Shafi Shajakhan 	.n_limits = ARRAY_SIZE(if_limits),
5628b0b6be5SMohammed Shafi Shajakhan 	.max_interfaces = 2,
5638b0b6be5SMohammed Shafi Shajakhan 	.num_different_channels = 1,
5648b0b6be5SMohammed Shafi Shajakhan };
5658b0b6be5SMohammed Shafi Shajakhan 
566fb9987d0SSujith static void ath9k_set_hw_capab(struct ath9k_htc_priv *priv,
567fb9987d0SSujith 			       struct ieee80211_hw *hw)
568fb9987d0SSujith {
569e9fb5888SOleksij Rempel 	struct ath_hw *ah = priv->ah;
570fb9987d0SSujith 	struct ath_common *common = ath9k_hw_common(priv->ah);
571156652bbSBen Greear 	struct base_eep_header *pBase;
572fb9987d0SSujith 
573fb9987d0SSujith 	hw->flags = IEEE80211_HW_SIGNAL_DBM |
574fb9987d0SSujith 		IEEE80211_HW_AMPDU_AGGREGATION |
575fb9987d0SSujith 		IEEE80211_HW_SPECTRUM_MGMT |
57632fbccafSSujith 		IEEE80211_HW_HAS_RATE_CONTROL |
577bde748a4SVivek Natarajan 		IEEE80211_HW_RX_INCLUDES_FCS |
5787d547eb4SSujith Manoharan 		IEEE80211_HW_PS_NULLFUNC_STACK |
5798ae2e12fSRajkumar Manoharan 		IEEE80211_HW_REPORTS_TX_ACK_STATUS |
580bd4a85eeSJavier Cardona 		IEEE80211_HW_MFP_CAPABLE |
5817d547eb4SSujith Manoharan 		IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
582fb9987d0SSujith 
5836bca610dSOleksij Rempel 	if (ath9k_ps_enable)
5846bca610dSOleksij Rempel 		hw->flags |= IEEE80211_HW_SUPPORTS_PS;
5856bca610dSOleksij Rempel 
586fb9987d0SSujith 	hw->wiphy->interface_modes =
587fb9987d0SSujith 		BIT(NL80211_IFTYPE_STATION) |
58809d5b94dSSujith Manoharan 		BIT(NL80211_IFTYPE_ADHOC) |
58909d5b94dSSujith Manoharan 		BIT(NL80211_IFTYPE_AP) |
59009d5b94dSSujith Manoharan 		BIT(NL80211_IFTYPE_P2P_GO) |
591594e65b6SJavier Cardona 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
592594e65b6SJavier Cardona 		BIT(NL80211_IFTYPE_MESH_POINT);
593fb9987d0SSujith 
5948b0b6be5SMohammed Shafi Shajakhan 	hw->wiphy->iface_combinations = &if_comb;
5958b0b6be5SMohammed Shafi Shajakhan 	hw->wiphy->n_iface_combinations = 1;
5968b0b6be5SMohammed Shafi Shajakhan 
597bde748a4SVivek Natarajan 	hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
598bde748a4SVivek Natarajan 
59981ddbb5cSJohannes Berg 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN |
60081ddbb5cSJohannes Berg 			    WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
601d7d312caSAntonio Quartulli 
602fb9987d0SSujith 	hw->queues = 4;
603fb1c078eSSujith Manoharan 	hw->max_listen_interval = 1;
6043a0593efSSujith Manoharan 
605fb9987d0SSujith 	hw->vif_data_size = sizeof(struct ath9k_htc_vif);
606fb9987d0SSujith 	hw->sta_data_size = sizeof(struct ath9k_htc_sta);
607fb9987d0SSujith 
608fb9987d0SSujith 	/* tx_frame_hdr is larger than tx_mgmt_hdr anyway */
609fb9987d0SSujith 	hw->extra_tx_headroom = sizeof(struct tx_frame_hdr) +
610fb9987d0SSujith 		sizeof(struct htc_frame_hdr) + 4;
611fb9987d0SSujith 
612d4659912SFelix Fietkau 	if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ)
613fb9987d0SSujith 		hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
61431f023a1SOleksij Rempel 			&common->sbands[IEEE80211_BAND_2GHZ];
615d4659912SFelix Fietkau 	if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ)
616ea46e644SSujith 		hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
61731f023a1SOleksij Rempel 			&common->sbands[IEEE80211_BAND_5GHZ];
618fb9987d0SSujith 
6196da2f4adSOleksij Rempel 	ath9k_cmn_reload_chainmask(ah);
620fb9987d0SSujith 
621156652bbSBen Greear 	pBase = ath9k_htc_get_eeprom_base(priv);
622156652bbSBen Greear 	if (pBase) {
623156652bbSBen Greear 		hw->wiphy->available_antennas_rx = pBase->rxMask;
624156652bbSBen Greear 		hw->wiphy->available_antennas_tx = pBase->txMask;
625156652bbSBen Greear 	}
626156652bbSBen Greear 
627fb9987d0SSujith 	SET_IEEE80211_PERM_ADDR(hw, common->macaddr);
628fb9987d0SSujith }
629fb9987d0SSujith 
63029bbfb24SSujith Manoharan static int ath9k_init_firmware_version(struct ath9k_htc_priv *priv)
63129bbfb24SSujith Manoharan {
63229bbfb24SSujith Manoharan 	struct ieee80211_hw *hw = priv->hw;
63329bbfb24SSujith Manoharan 	struct wmi_fw_version cmd_rsp;
63429bbfb24SSujith Manoharan 	int ret;
63529bbfb24SSujith Manoharan 
63629bbfb24SSujith Manoharan 	memset(&cmd_rsp, 0, sizeof(cmd_rsp));
63729bbfb24SSujith Manoharan 
63829bbfb24SSujith Manoharan 	WMI_CMD(WMI_GET_FW_VERSION);
63929bbfb24SSujith Manoharan 	if (ret)
64029bbfb24SSujith Manoharan 		return -EINVAL;
64129bbfb24SSujith Manoharan 
64229bbfb24SSujith Manoharan 	priv->fw_version_major = be16_to_cpu(cmd_rsp.major);
64329bbfb24SSujith Manoharan 	priv->fw_version_minor = be16_to_cpu(cmd_rsp.minor);
64429bbfb24SSujith Manoharan 
64581135548SJiri Pirko 	snprintf(hw->wiphy->fw_version, sizeof(hw->wiphy->fw_version), "%d.%d",
64629bbfb24SSujith Manoharan 		 priv->fw_version_major,
64729bbfb24SSujith Manoharan 		 priv->fw_version_minor);
64829bbfb24SSujith Manoharan 
64929bbfb24SSujith Manoharan 	dev_info(priv->dev, "ath9k_htc: FW Version: %d.%d\n",
65029bbfb24SSujith Manoharan 		 priv->fw_version_major,
65129bbfb24SSujith Manoharan 		 priv->fw_version_minor);
65229bbfb24SSujith Manoharan 
6533a0593efSSujith Manoharan 	/*
6543a0593efSSujith Manoharan 	 * Check if the available FW matches the driver's
6553a0593efSSujith Manoharan 	 * required version.
6563a0593efSSujith Manoharan 	 */
6573a0593efSSujith Manoharan 	if (priv->fw_version_major != MAJOR_VERSION_REQ ||
658319e7bd9SFelix Fietkau 	    priv->fw_version_minor < MINOR_VERSION_REQ) {
6593a0593efSSujith Manoharan 		dev_err(priv->dev, "ath9k_htc: Please upgrade to FW version %d.%d\n",
6603a0593efSSujith Manoharan 			MAJOR_VERSION_REQ, MINOR_VERSION_REQ);
6613a0593efSSujith Manoharan 		return -EINVAL;
6623a0593efSSujith Manoharan 	}
6633a0593efSSujith Manoharan 
66429bbfb24SSujith Manoharan 	return 0;
66529bbfb24SSujith Manoharan }
66629bbfb24SSujith Manoharan 
66721cb9879SVivek Natarajan static int ath9k_init_device(struct ath9k_htc_priv *priv,
668fa6e15e0SRajkumar Manoharan 			     u16 devid, char *product, u32 drv_info)
669fb9987d0SSujith {
670fb9987d0SSujith 	struct ieee80211_hw *hw = priv->hw;
671fb9987d0SSujith 	struct ath_common *common;
672fb9987d0SSujith 	struct ath_hw *ah;
673fb9987d0SSujith 	int error = 0;
674fb9987d0SSujith 	struct ath_regulatory *reg;
6753e3f1d19SSujith Manoharan 	char hw_name[64];
676fb9987d0SSujith 
677fb9987d0SSujith 	/* Bring up device */
678fa6e15e0SRajkumar Manoharan 	error = ath9k_init_priv(priv, devid, product, drv_info);
679fb9987d0SSujith 	if (error != 0)
680fb9987d0SSujith 		goto err_init;
681fb9987d0SSujith 
682fb9987d0SSujith 	ah = priv->ah;
683fb9987d0SSujith 	common = ath9k_hw_common(ah);
684fb9987d0SSujith 	ath9k_set_hw_capab(priv, hw);
685fb9987d0SSujith 
68629bbfb24SSujith Manoharan 	error = ath9k_init_firmware_version(priv);
68729bbfb24SSujith Manoharan 	if (error != 0)
68829bbfb24SSujith Manoharan 		goto err_fw;
68929bbfb24SSujith Manoharan 
690fb9987d0SSujith 	/* Initialize regulatory */
691fb9987d0SSujith 	error = ath_regd_init(&common->regulatory, priv->hw->wiphy,
692fb9987d0SSujith 			      ath9k_reg_notifier);
693fb9987d0SSujith 	if (error)
694fb9987d0SSujith 		goto err_regd;
695fb9987d0SSujith 
696fb9987d0SSujith 	reg = &common->regulatory;
697fb9987d0SSujith 
698fb9987d0SSujith 	/* Setup TX */
699fb9987d0SSujith 	error = ath9k_tx_init(priv);
700fb9987d0SSujith 	if (error != 0)
701fb9987d0SSujith 		goto err_tx;
702fb9987d0SSujith 
703fb9987d0SSujith 	/* Setup RX */
704fb9987d0SSujith 	error = ath9k_rx_init(priv);
705fb9987d0SSujith 	if (error != 0)
706fb9987d0SSujith 		goto err_rx;
707fb9987d0SSujith 
708dc2a87f5SOleksij Rempel 	ath9k_hw_disable(priv->ah);
709d244f21eSSujith Manoharan #ifdef CONFIG_MAC80211_LEDS
710d244f21eSSujith Manoharan 	/* must be initialized before ieee80211_register_hw */
711d244f21eSSujith Manoharan 	priv->led_cdev.default_trigger = ieee80211_create_tpt_led_trigger(priv->hw,
712d244f21eSSujith Manoharan 		IEEE80211_TPT_LEDTRIG_FL_RADIO, ath9k_htc_tpt_blink,
713d244f21eSSujith Manoharan 		ARRAY_SIZE(ath9k_htc_tpt_blink));
714d244f21eSSujith Manoharan #endif
715d244f21eSSujith Manoharan 
716fb9987d0SSujith 	/* Register with mac80211 */
717fb9987d0SSujith 	error = ieee80211_register_hw(hw);
718fb9987d0SSujith 	if (error)
719fb9987d0SSujith 		goto err_register;
720fb9987d0SSujith 
721fb9987d0SSujith 	/* Handle world regulatory */
722fb9987d0SSujith 	if (!ath_is_world_regd(reg)) {
723fb9987d0SSujith 		error = regulatory_hint(hw->wiphy, reg->alpha2);
724fb9987d0SSujith 		if (error)
725fb9987d0SSujith 			goto err_world;
726fb9987d0SSujith 	}
727fb9987d0SSujith 
728e5facc75SRajkumar Manoharan 	error = ath9k_htc_init_debug(priv->ah);
729e5facc75SRajkumar Manoharan 	if (error) {
730e5facc75SRajkumar Manoharan 		ath_err(common, "Unable to create debugfs files\n");
731e5facc75SRajkumar Manoharan 		goto err_world;
732e5facc75SRajkumar Manoharan 	}
733e5facc75SRajkumar Manoharan 
734d2182b69SJoe Perches 	ath_dbg(common, CONFIG,
735d2182b69SJoe Perches 		"WMI:%d, BCN:%d, CAB:%d, UAPSD:%d, MGMT:%d, BE:%d, BK:%d, VI:%d, VO:%d\n",
7363e3f1d19SSujith Manoharan 		priv->wmi_cmd_ep,
7373e3f1d19SSujith Manoharan 		priv->beacon_ep,
7383e3f1d19SSujith Manoharan 		priv->cab_ep,
7393e3f1d19SSujith Manoharan 		priv->uapsd_ep,
7403e3f1d19SSujith Manoharan 		priv->mgmt_ep,
7413e3f1d19SSujith Manoharan 		priv->data_be_ep,
7423e3f1d19SSujith Manoharan 		priv->data_bk_ep,
7433e3f1d19SSujith Manoharan 		priv->data_vi_ep,
7443e3f1d19SSujith Manoharan 		priv->data_vo_ep);
7453e3f1d19SSujith Manoharan 
7463e3f1d19SSujith Manoharan 	ath9k_hw_name(priv->ah, hw_name, sizeof(hw_name));
7473e3f1d19SSujith Manoharan 	wiphy_info(hw->wiphy, "%s\n", hw_name);
7483e3f1d19SSujith Manoharan 
749fb9987d0SSujith 	ath9k_init_leds(priv);
750fb9987d0SSujith 	ath9k_start_rfkill_poll(priv);
751fb9987d0SSujith 
752fb9987d0SSujith 	return 0;
753fb9987d0SSujith 
754fb9987d0SSujith err_world:
755fb9987d0SSujith 	ieee80211_unregister_hw(hw);
756fb9987d0SSujith err_register:
757fb9987d0SSujith 	ath9k_rx_cleanup(priv);
758fb9987d0SSujith err_rx:
759fb9987d0SSujith 	ath9k_tx_cleanup(priv);
760fb9987d0SSujith err_tx:
761fb9987d0SSujith 	/* Nothing */
762fb9987d0SSujith err_regd:
76329bbfb24SSujith Manoharan 	/* Nothing */
76429bbfb24SSujith Manoharan err_fw:
765fb9987d0SSujith 	ath9k_deinit_priv(priv);
766fb9987d0SSujith err_init:
767fb9987d0SSujith 	return error;
768fb9987d0SSujith }
769fb9987d0SSujith 
770fb9987d0SSujith int ath9k_htc_probe_device(struct htc_target *htc_handle, struct device *dev,
771fa6e15e0SRajkumar Manoharan 			   u16 devid, char *product, u32 drv_info)
772fb9987d0SSujith {
773fb9987d0SSujith 	struct ieee80211_hw *hw;
774fb9987d0SSujith 	struct ath9k_htc_priv *priv;
775fb9987d0SSujith 	int ret;
776fb9987d0SSujith 
777fb9987d0SSujith 	hw = ieee80211_alloc_hw(sizeof(struct ath9k_htc_priv), &ath9k_htc_ops);
778fb9987d0SSujith 	if (!hw)
779fb9987d0SSujith 		return -ENOMEM;
780fb9987d0SSujith 
781fb9987d0SSujith 	priv = hw->priv;
782fb9987d0SSujith 	priv->hw = hw;
783fb9987d0SSujith 	priv->htc = htc_handle;
784fb9987d0SSujith 	priv->dev = dev;
785fb9987d0SSujith 	htc_handle->drv_priv = priv;
786fb9987d0SSujith 	SET_IEEE80211_DEV(hw, priv->dev);
787fb9987d0SSujith 
788fb9987d0SSujith 	ret = ath9k_htc_wait_for_target(priv);
789fb9987d0SSujith 	if (ret)
790fb9987d0SSujith 		goto err_free;
791fb9987d0SSujith 
792fb9987d0SSujith 	priv->wmi = ath9k_init_wmi(priv);
793fb9987d0SSujith 	if (!priv->wmi) {
794fb9987d0SSujith 		ret = -EINVAL;
795fb9987d0SSujith 		goto err_free;
796fb9987d0SSujith 	}
797fb9987d0SSujith 
798fa6e15e0SRajkumar Manoharan 	ret = ath9k_init_htc_services(priv, devid, drv_info);
799fb9987d0SSujith 	if (ret)
800fb9987d0SSujith 		goto err_init;
801fb9987d0SSujith 
802fa6e15e0SRajkumar Manoharan 	ret = ath9k_init_device(priv, devid, product, drv_info);
803fb9987d0SSujith 	if (ret)
804fb9987d0SSujith 		goto err_init;
805fb9987d0SSujith 
806fb9987d0SSujith 	return 0;
807fb9987d0SSujith 
808fb9987d0SSujith err_init:
809fb9987d0SSujith 	ath9k_deinit_wmi(priv);
810fb9987d0SSujith err_free:
811fb9987d0SSujith 	ieee80211_free_hw(hw);
812fb9987d0SSujith 	return ret;
813fb9987d0SSujith }
814fb9987d0SSujith 
815fb9987d0SSujith void ath9k_htc_disconnect_device(struct htc_target *htc_handle, bool hotunplug)
816fb9987d0SSujith {
817fb9987d0SSujith 	if (htc_handle->drv_priv) {
818a3be14b7SSujith 
819a3be14b7SSujith 		/* Check if the device has been yanked out. */
820a3be14b7SSujith 		if (hotunplug)
82197dcec57SSujith Manoharan 			htc_handle->drv_priv->ah->ah_flags |= AH_UNPLUGGED;
822a3be14b7SSujith 
823fb9987d0SSujith 		ath9k_deinit_device(htc_handle->drv_priv);
824fb9987d0SSujith 		ath9k_deinit_wmi(htc_handle->drv_priv);
825fb9987d0SSujith 		ieee80211_free_hw(htc_handle->drv_priv->hw);
826fb9987d0SSujith 	}
827fb9987d0SSujith }
828fb9987d0SSujith 
829fb9987d0SSujith #ifdef CONFIG_PM
830f933ebedSSujith Manoharan 
831f933ebedSSujith Manoharan void ath9k_htc_suspend(struct htc_target *htc_handle)
832f933ebedSSujith Manoharan {
833f933ebedSSujith Manoharan 	ath9k_htc_setpower(htc_handle->drv_priv, ATH9K_PM_FULL_SLEEP);
834f933ebedSSujith Manoharan }
835f933ebedSSujith Manoharan 
836fb9987d0SSujith int ath9k_htc_resume(struct htc_target *htc_handle)
837fb9987d0SSujith {
838fa6e15e0SRajkumar Manoharan 	struct ath9k_htc_priv *priv = htc_handle->drv_priv;
839fb9987d0SSujith 	int ret;
840fb9987d0SSujith 
841fa6e15e0SRajkumar Manoharan 	ret = ath9k_htc_wait_for_target(priv);
842fb9987d0SSujith 	if (ret)
843fb9987d0SSujith 		return ret;
844fb9987d0SSujith 
845fa6e15e0SRajkumar Manoharan 	ret = ath9k_init_htc_services(priv, priv->ah->hw_version.devid,
8460b5ead91SSujith Manoharan 				      priv->ah->hw_version.usbdev);
8471e51acaaSOleksij Rempel 	ath9k_configure_leds(priv);
8481e51acaaSOleksij Rempel 
849fb9987d0SSujith 	return ret;
850fb9987d0SSujith }
851fb9987d0SSujith #endif
852fb9987d0SSujith 
853fb9987d0SSujith static int __init ath9k_htc_init(void)
854fb9987d0SSujith {
855e5facc75SRajkumar Manoharan 	if (ath9k_hif_usb_init() < 0) {
856516304b0SJoe Perches 		pr_err("No USB devices found, driver not installed\n");
857e5facc75SRajkumar Manoharan 		return -ENODEV;
858fb9987d0SSujith 	}
859fb9987d0SSujith 
860fb9987d0SSujith 	return 0;
861fb9987d0SSujith }
862fb9987d0SSujith module_init(ath9k_htc_init);
863fb9987d0SSujith 
864fb9987d0SSujith static void __exit ath9k_htc_exit(void)
865fb9987d0SSujith {
866fb9987d0SSujith 	ath9k_hif_usb_exit();
867516304b0SJoe Perches 	pr_info("Driver unloaded\n");
868fb9987d0SSujith }
869fb9987d0SSujith module_exit(ath9k_htc_exit);
870