1 /*
2  * Copyright (c) 2010-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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 
19 #include "htc.h"
20 
21 MODULE_AUTHOR("Atheros Communications");
22 MODULE_LICENSE("Dual BSD/GPL");
23 MODULE_DESCRIPTION("Atheros driver 802.11n HTC based wireless devices");
24 
25 static unsigned int ath9k_debug = ATH_DBG_DEFAULT;
26 module_param_named(debug, ath9k_debug, uint, 0);
27 MODULE_PARM_DESC(debug, "Debugging mask");
28 
29 int htc_modparam_nohwcrypt;
30 module_param_named(nohwcrypt, htc_modparam_nohwcrypt, int, 0444);
31 MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption");
32 
33 static int ath9k_htc_btcoex_enable;
34 module_param_named(btcoex_enable, ath9k_htc_btcoex_enable, int, 0444);
35 MODULE_PARM_DESC(btcoex_enable, "Enable wifi-BT coexistence");
36 
37 static int ath9k_ps_enable;
38 module_param_named(ps_enable, ath9k_ps_enable, int, 0444);
39 MODULE_PARM_DESC(ps_enable, "Enable WLAN PowerSave");
40 
41 #ifdef CONFIG_MAC80211_LEDS
42 int ath9k_htc_led_blink = 1;
43 module_param_named(blink, ath9k_htc_led_blink, int, 0444);
44 MODULE_PARM_DESC(blink, "Enable LED blink on activity");
45 
46 static const struct ieee80211_tpt_blink ath9k_htc_tpt_blink[] = {
47 	{ .throughput = 0 * 1024, .blink_time = 334 },
48 	{ .throughput = 1 * 1024, .blink_time = 260 },
49 	{ .throughput = 5 * 1024, .blink_time = 220 },
50 	{ .throughput = 10 * 1024, .blink_time = 190 },
51 	{ .throughput = 20 * 1024, .blink_time = 170 },
52 	{ .throughput = 50 * 1024, .blink_time = 150 },
53 	{ .throughput = 70 * 1024, .blink_time = 130 },
54 	{ .throughput = 100 * 1024, .blink_time = 110 },
55 	{ .throughput = 200 * 1024, .blink_time = 80 },
56 	{ .throughput = 300 * 1024, .blink_time = 50 },
57 };
58 #endif
59 
60 static void ath9k_htc_op_ps_wakeup(struct ath_common *common)
61 {
62 	ath9k_htc_ps_wakeup((struct ath9k_htc_priv *) common->priv);
63 }
64 
65 static void ath9k_htc_op_ps_restore(struct ath_common *common)
66 {
67 	ath9k_htc_ps_restore((struct ath9k_htc_priv *) common->priv);
68 }
69 
70 static struct ath_ps_ops ath9k_htc_ps_ops = {
71 	.wakeup = ath9k_htc_op_ps_wakeup,
72 	.restore = ath9k_htc_op_ps_restore,
73 };
74 
75 static int ath9k_htc_wait_for_target(struct ath9k_htc_priv *priv)
76 {
77 	int time_left;
78 
79 	if (atomic_read(&priv->htc->tgt_ready) > 0) {
80 		atomic_dec(&priv->htc->tgt_ready);
81 		return 0;
82 	}
83 
84 	/* Firmware can take up to 50ms to get ready, to be safe use 1 second */
85 	time_left = wait_for_completion_timeout(&priv->htc->target_wait, HZ);
86 	if (!time_left) {
87 		dev_err(priv->dev, "ath9k_htc: Target is unresponsive\n");
88 		return -ETIMEDOUT;
89 	}
90 
91 	atomic_dec(&priv->htc->tgt_ready);
92 
93 	return 0;
94 }
95 
96 static void ath9k_deinit_priv(struct ath9k_htc_priv *priv)
97 {
98 	ath9k_hw_deinit(priv->ah);
99 	kfree(priv->ah);
100 	priv->ah = NULL;
101 }
102 
103 static void ath9k_deinit_device(struct ath9k_htc_priv *priv)
104 {
105 	struct ieee80211_hw *hw = priv->hw;
106 
107 	wiphy_rfkill_stop_polling(hw->wiphy);
108 	ath9k_deinit_leds(priv);
109 	ath9k_htc_deinit_debug(priv);
110 	ieee80211_unregister_hw(hw);
111 	ath9k_rx_cleanup(priv);
112 	ath9k_tx_cleanup(priv);
113 	ath9k_deinit_priv(priv);
114 }
115 
116 static inline int ath9k_htc_connect_svc(struct ath9k_htc_priv *priv,
117 					u16 service_id,
118 					void (*tx) (void *,
119 						    struct sk_buff *,
120 						    enum htc_endpoint_id,
121 						    bool txok),
122 					enum htc_endpoint_id *ep_id)
123 {
124 	struct htc_service_connreq req;
125 
126 	memset(&req, 0, sizeof(struct htc_service_connreq));
127 
128 	req.service_id = service_id;
129 	req.ep_callbacks.priv = priv;
130 	req.ep_callbacks.rx = ath9k_htc_rxep;
131 	req.ep_callbacks.tx = tx;
132 
133 	return htc_connect_service(priv->htc, &req, ep_id);
134 }
135 
136 static int ath9k_init_htc_services(struct ath9k_htc_priv *priv, u16 devid,
137 				   u32 drv_info)
138 {
139 	int ret;
140 
141 	/* WMI CMD*/
142 	ret = ath9k_wmi_connect(priv->htc, priv->wmi, &priv->wmi_cmd_ep);
143 	if (ret)
144 		goto err;
145 
146 	/* Beacon */
147 	ret = ath9k_htc_connect_svc(priv, WMI_BEACON_SVC, ath9k_htc_beaconep,
148 				    &priv->beacon_ep);
149 	if (ret)
150 		goto err;
151 
152 	/* CAB */
153 	ret = ath9k_htc_connect_svc(priv, WMI_CAB_SVC, ath9k_htc_txep,
154 				    &priv->cab_ep);
155 	if (ret)
156 		goto err;
157 
158 
159 	/* UAPSD */
160 	ret = ath9k_htc_connect_svc(priv, WMI_UAPSD_SVC, ath9k_htc_txep,
161 				    &priv->uapsd_ep);
162 	if (ret)
163 		goto err;
164 
165 	/* MGMT */
166 	ret = ath9k_htc_connect_svc(priv, WMI_MGMT_SVC, ath9k_htc_txep,
167 				    &priv->mgmt_ep);
168 	if (ret)
169 		goto err;
170 
171 	/* DATA BE */
172 	ret = ath9k_htc_connect_svc(priv, WMI_DATA_BE_SVC, ath9k_htc_txep,
173 				    &priv->data_be_ep);
174 	if (ret)
175 		goto err;
176 
177 	/* DATA BK */
178 	ret = ath9k_htc_connect_svc(priv, WMI_DATA_BK_SVC, ath9k_htc_txep,
179 				    &priv->data_bk_ep);
180 	if (ret)
181 		goto err;
182 
183 	/* DATA VI */
184 	ret = ath9k_htc_connect_svc(priv, WMI_DATA_VI_SVC, ath9k_htc_txep,
185 				    &priv->data_vi_ep);
186 	if (ret)
187 		goto err;
188 
189 	/* DATA VO */
190 	ret = ath9k_htc_connect_svc(priv, WMI_DATA_VO_SVC, ath9k_htc_txep,
191 				    &priv->data_vo_ep);
192 	if (ret)
193 		goto err;
194 
195 	/*
196 	 * Setup required credits before initializing HTC.
197 	 * This is a bit hacky, but, since queuing is done in
198 	 * the HIF layer, shouldn't matter much.
199 	 */
200 
201 	if (IS_AR7010_DEVICE(drv_info))
202 		priv->htc->credits = 45;
203 	else
204 		priv->htc->credits = 33;
205 
206 	ret = htc_init(priv->htc);
207 	if (ret)
208 		goto err;
209 
210 	dev_info(priv->dev, "ath9k_htc: HTC initialized with %d credits\n",
211 		 priv->htc->credits);
212 
213 	return 0;
214 
215 err:
216 	dev_err(priv->dev, "ath9k_htc: Unable to initialize HTC services\n");
217 	return ret;
218 }
219 
220 static void ath9k_reg_notifier(struct wiphy *wiphy,
221 			       struct regulatory_request *request)
222 {
223 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
224 	struct ath9k_htc_priv *priv = hw->priv;
225 
226 	ath_reg_notifier_apply(wiphy, request,
227 			       ath9k_hw_regulatory(priv->ah));
228 }
229 
230 static unsigned int ath9k_regread(void *hw_priv, u32 reg_offset)
231 {
232 	struct ath_hw *ah = (struct ath_hw *) hw_priv;
233 	struct ath_common *common = ath9k_hw_common(ah);
234 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
235 	__be32 val, reg = cpu_to_be32(reg_offset);
236 	int r;
237 
238 	r = ath9k_wmi_cmd(priv->wmi, WMI_REG_READ_CMDID,
239 			  (u8 *) &reg, sizeof(reg),
240 			  (u8 *) &val, sizeof(val),
241 			  100);
242 	if (unlikely(r)) {
243 		ath_dbg(common, WMI, "REGISTER READ FAILED: (0x%04x, %d)\n",
244 			reg_offset, r);
245 		return -EIO;
246 	}
247 
248 	return be32_to_cpu(val);
249 }
250 
251 static void ath9k_multi_regread(void *hw_priv, u32 *addr,
252 				u32 *val, u16 count)
253 {
254 	struct ath_hw *ah = (struct ath_hw *) hw_priv;
255 	struct ath_common *common = ath9k_hw_common(ah);
256 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
257 	__be32 tmpaddr[8];
258 	__be32 tmpval[8];
259 	int i, ret;
260 
261        for (i = 0; i < count; i++) {
262 	       tmpaddr[i] = cpu_to_be32(addr[i]);
263        }
264 
265        ret = ath9k_wmi_cmd(priv->wmi, WMI_REG_READ_CMDID,
266 			   (u8 *)tmpaddr , sizeof(u32) * count,
267 			   (u8 *)tmpval, sizeof(u32) * count,
268 			   100);
269 	if (unlikely(ret)) {
270 		ath_dbg(common, WMI,
271 			"Multiple REGISTER READ FAILED (count: %d)\n", count);
272 	}
273 
274        for (i = 0; i < count; i++) {
275 	       val[i] = be32_to_cpu(tmpval[i]);
276        }
277 }
278 
279 static void ath9k_regwrite_multi(struct ath_common *common)
280 {
281 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
282 	u32 rsp_status;
283 	int r;
284 
285 	r = ath9k_wmi_cmd(priv->wmi, WMI_REG_WRITE_CMDID,
286 			  (u8 *) &priv->wmi->multi_write,
287 			  sizeof(struct register_write) * priv->wmi->multi_write_idx,
288 			  (u8 *) &rsp_status, sizeof(rsp_status),
289 			  100);
290 	if (unlikely(r)) {
291 		ath_dbg(common, WMI,
292 			"REGISTER WRITE FAILED, multi len: %d\n",
293 			priv->wmi->multi_write_idx);
294 	}
295 	priv->wmi->multi_write_idx = 0;
296 }
297 
298 static void ath9k_regwrite_single(void *hw_priv, u32 val, u32 reg_offset)
299 {
300 	struct ath_hw *ah = (struct ath_hw *) hw_priv;
301 	struct ath_common *common = ath9k_hw_common(ah);
302 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
303 	const __be32 buf[2] = {
304 		cpu_to_be32(reg_offset),
305 		cpu_to_be32(val),
306 	};
307 	int r;
308 
309 	r = ath9k_wmi_cmd(priv->wmi, WMI_REG_WRITE_CMDID,
310 			  (u8 *) &buf, sizeof(buf),
311 			  (u8 *) &val, sizeof(val),
312 			  100);
313 	if (unlikely(r)) {
314 		ath_dbg(common, WMI, "REGISTER WRITE FAILED:(0x%04x, %d)\n",
315 			reg_offset, r);
316 	}
317 }
318 
319 static void ath9k_regwrite_buffer(void *hw_priv, u32 val, u32 reg_offset)
320 {
321 	struct ath_hw *ah = (struct ath_hw *) hw_priv;
322 	struct ath_common *common = ath9k_hw_common(ah);
323 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
324 
325 	mutex_lock(&priv->wmi->multi_write_mutex);
326 
327 	/* Store the register/value */
328 	priv->wmi->multi_write[priv->wmi->multi_write_idx].reg =
329 		cpu_to_be32(reg_offset);
330 	priv->wmi->multi_write[priv->wmi->multi_write_idx].val =
331 		cpu_to_be32(val);
332 
333 	priv->wmi->multi_write_idx++;
334 
335 	/* If the buffer is full, send it out. */
336 	if (priv->wmi->multi_write_idx == MAX_CMD_NUMBER)
337 		ath9k_regwrite_multi(common);
338 
339 	mutex_unlock(&priv->wmi->multi_write_mutex);
340 }
341 
342 static void ath9k_regwrite(void *hw_priv, u32 val, u32 reg_offset)
343 {
344 	struct ath_hw *ah = (struct ath_hw *) hw_priv;
345 	struct ath_common *common = ath9k_hw_common(ah);
346 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
347 
348 	if (atomic_read(&priv->wmi->mwrite_cnt))
349 		ath9k_regwrite_buffer(hw_priv, val, reg_offset);
350 	else
351 		ath9k_regwrite_single(hw_priv, val, reg_offset);
352 }
353 
354 static void ath9k_enable_regwrite_buffer(void *hw_priv)
355 {
356 	struct ath_hw *ah = (struct ath_hw *) hw_priv;
357 	struct ath_common *common = ath9k_hw_common(ah);
358 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
359 
360 	atomic_inc(&priv->wmi->mwrite_cnt);
361 }
362 
363 static void ath9k_regwrite_flush(void *hw_priv)
364 {
365 	struct ath_hw *ah = (struct ath_hw *) hw_priv;
366 	struct ath_common *common = ath9k_hw_common(ah);
367 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
368 
369 	atomic_dec(&priv->wmi->mwrite_cnt);
370 
371 	mutex_lock(&priv->wmi->multi_write_mutex);
372 
373 	if (priv->wmi->multi_write_idx)
374 		ath9k_regwrite_multi(common);
375 
376 	mutex_unlock(&priv->wmi->multi_write_mutex);
377 }
378 
379 static u32 ath9k_reg_rmw(void *hw_priv, u32 reg_offset, u32 set, u32 clr)
380 {
381 	u32 val;
382 
383 	val = ath9k_regread(hw_priv, reg_offset);
384 	val &= ~clr;
385 	val |= set;
386 	ath9k_regwrite(hw_priv, val, reg_offset);
387 	return val;
388 }
389 
390 static void ath_usb_read_cachesize(struct ath_common *common, int *csz)
391 {
392 	*csz = L1_CACHE_BYTES >> 2;
393 }
394 
395 static bool ath_usb_eeprom_read(struct ath_common *common, u32 off, u16 *data)
396 {
397 	struct ath_hw *ah = (struct ath_hw *) common->ah;
398 
399 	(void)REG_READ(ah, AR5416_EEPROM_OFFSET + (off << AR5416_EEPROM_S));
400 
401 	if (!ath9k_hw_wait(ah,
402 			   AR_EEPROM_STATUS_DATA,
403 			   AR_EEPROM_STATUS_DATA_BUSY |
404 			   AR_EEPROM_STATUS_DATA_PROT_ACCESS, 0,
405 			   AH_WAIT_TIMEOUT))
406 		return false;
407 
408 	*data = MS(REG_READ(ah, AR_EEPROM_STATUS_DATA),
409 		   AR_EEPROM_STATUS_DATA_VAL);
410 
411 	return true;
412 }
413 
414 static const struct ath_bus_ops ath9k_usb_bus_ops = {
415 	.ath_bus_type = ATH_USB,
416 	.read_cachesize = ath_usb_read_cachesize,
417 	.eeprom_read = ath_usb_eeprom_read,
418 };
419 
420 static int ath9k_init_queues(struct ath9k_htc_priv *priv)
421 {
422 	struct ath_common *common = ath9k_hw_common(priv->ah);
423 	int i;
424 
425 	for (i = 0; i < ARRAY_SIZE(priv->hwq_map); i++)
426 		priv->hwq_map[i] = -1;
427 
428 	priv->beacon.beaconq = ath9k_hw_beaconq_setup(priv->ah);
429 	if (priv->beacon.beaconq == -1) {
430 		ath_err(common, "Unable to setup BEACON xmit queue\n");
431 		goto err;
432 	}
433 
434 	priv->cabq = ath9k_htc_cabq_setup(priv);
435 	if (priv->cabq == -1) {
436 		ath_err(common, "Unable to setup CAB xmit queue\n");
437 		goto err;
438 	}
439 
440 	if (!ath9k_htc_txq_setup(priv, IEEE80211_AC_BE)) {
441 		ath_err(common, "Unable to setup xmit queue for BE traffic\n");
442 		goto err;
443 	}
444 
445 	if (!ath9k_htc_txq_setup(priv, IEEE80211_AC_BK)) {
446 		ath_err(common, "Unable to setup xmit queue for BK traffic\n");
447 		goto err;
448 	}
449 	if (!ath9k_htc_txq_setup(priv, IEEE80211_AC_VI)) {
450 		ath_err(common, "Unable to setup xmit queue for VI traffic\n");
451 		goto err;
452 	}
453 	if (!ath9k_htc_txq_setup(priv, IEEE80211_AC_VO)) {
454 		ath_err(common, "Unable to setup xmit queue for VO traffic\n");
455 		goto err;
456 	}
457 
458 	return 0;
459 
460 err:
461 	return -EINVAL;
462 }
463 
464 static void ath9k_init_misc(struct ath9k_htc_priv *priv)
465 {
466 	struct ath_common *common = ath9k_hw_common(priv->ah);
467 
468 	memcpy(common->bssidmask, ath_bcast_mac, ETH_ALEN);
469 
470 	common->last_rssi = ATH_RSSI_DUMMY_MARKER;
471 	priv->ah->opmode = NL80211_IFTYPE_STATION;
472 
473 	priv->spec_priv.ah = priv->ah;
474 	priv->spec_priv.spec_config.enabled = 0;
475 	priv->spec_priv.spec_config.short_repeat = false;
476 	priv->spec_priv.spec_config.count = 8;
477 	priv->spec_priv.spec_config.endless = false;
478 	priv->spec_priv.spec_config.period = 0x12;
479 	priv->spec_priv.spec_config.fft_period = 0x02;
480 }
481 
482 static int ath9k_init_priv(struct ath9k_htc_priv *priv,
483 			   u16 devid, char *product,
484 			   u32 drv_info)
485 {
486 	struct ath_hw *ah = NULL;
487 	struct ath_common *common;
488 	int i, ret = 0, csz = 0;
489 
490 	ah = kzalloc(sizeof(struct ath_hw), GFP_KERNEL);
491 	if (!ah)
492 		return -ENOMEM;
493 
494 	ah->dev = priv->dev;
495 	ah->hw = priv->hw;
496 	ah->hw_version.devid = devid;
497 	ah->hw_version.usbdev = drv_info;
498 	ah->ah_flags |= AH_USE_EEPROM;
499 	ah->reg_ops.read = ath9k_regread;
500 	ah->reg_ops.multi_read = ath9k_multi_regread;
501 	ah->reg_ops.write = ath9k_regwrite;
502 	ah->reg_ops.enable_write_buffer = ath9k_enable_regwrite_buffer;
503 	ah->reg_ops.write_flush = ath9k_regwrite_flush;
504 	ah->reg_ops.rmw = ath9k_reg_rmw;
505 	priv->ah = ah;
506 
507 	common = ath9k_hw_common(ah);
508 	common->ops = &ah->reg_ops;
509 	common->ps_ops = &ath9k_htc_ps_ops;
510 	common->bus_ops = &ath9k_usb_bus_ops;
511 	common->ah = ah;
512 	common->hw = priv->hw;
513 	common->priv = priv;
514 	common->debug_mask = ath9k_debug;
515 	common->btcoex_enabled = ath9k_htc_btcoex_enable == 1;
516 	set_bit(ATH_OP_INVALID, &common->op_flags);
517 
518 	spin_lock_init(&priv->beacon_lock);
519 	spin_lock_init(&priv->tx.tx_lock);
520 	mutex_init(&priv->mutex);
521 	mutex_init(&priv->htc_pm_lock);
522 	tasklet_init(&priv->rx_tasklet, ath9k_rx_tasklet,
523 		     (unsigned long)priv);
524 	tasklet_init(&priv->tx_failed_tasklet, ath9k_tx_failed_tasklet,
525 		     (unsigned long)priv);
526 	INIT_DELAYED_WORK(&priv->ani_work, ath9k_htc_ani_work);
527 	INIT_WORK(&priv->ps_work, ath9k_ps_work);
528 	INIT_WORK(&priv->fatal_work, ath9k_fatal_work);
529 	setup_timer(&priv->tx.cleanup_timer, ath9k_htc_tx_cleanup_timer,
530 		    (unsigned long)priv);
531 
532 	/*
533 	 * Cache line size is used to size and align various
534 	 * structures used to communicate with the hardware.
535 	 */
536 	ath_read_cachesize(common, &csz);
537 	common->cachelsz = csz << 2; /* convert to bytes */
538 
539 	ret = ath9k_hw_init(ah);
540 	if (ret) {
541 		ath_err(common,
542 			"Unable to initialize hardware; initialization status: %d\n",
543 			ret);
544 		goto err_hw;
545 	}
546 
547 	ret = ath9k_init_queues(priv);
548 	if (ret)
549 		goto err_queues;
550 
551 	for (i = 0; i < ATH9K_HTC_MAX_BCN_VIF; i++)
552 		priv->beacon.bslot[i] = NULL;
553 	priv->beacon.slottime = ATH9K_SLOT_TIME_9;
554 
555 	ath9k_cmn_init_channels_rates(common);
556 	ath9k_cmn_init_crypto(ah);
557 	ath9k_init_misc(priv);
558 	ath9k_htc_init_btcoex(priv, product);
559 
560 	return 0;
561 
562 err_queues:
563 	ath9k_hw_deinit(ah);
564 err_hw:
565 
566 	kfree(ah);
567 	priv->ah = NULL;
568 
569 	return ret;
570 }
571 
572 static const struct ieee80211_iface_limit if_limits[] = {
573 	{ .max = 2,	.types = BIT(NL80211_IFTYPE_STATION) |
574 				 BIT(NL80211_IFTYPE_P2P_CLIENT) },
575 	{ .max = 2,	.types = BIT(NL80211_IFTYPE_AP) |
576 #ifdef CONFIG_MAC80211_MESH
577 				 BIT(NL80211_IFTYPE_MESH_POINT) |
578 #endif
579 				 BIT(NL80211_IFTYPE_P2P_GO) },
580 };
581 
582 static const struct ieee80211_iface_combination if_comb = {
583 	.limits = if_limits,
584 	.n_limits = ARRAY_SIZE(if_limits),
585 	.max_interfaces = 2,
586 	.num_different_channels = 1,
587 };
588 
589 static void ath9k_set_hw_capab(struct ath9k_htc_priv *priv,
590 			       struct ieee80211_hw *hw)
591 {
592 	struct ath_hw *ah = priv->ah;
593 	struct ath_common *common = ath9k_hw_common(priv->ah);
594 	struct base_eep_header *pBase;
595 
596 	hw->flags = IEEE80211_HW_SIGNAL_DBM |
597 		IEEE80211_HW_AMPDU_AGGREGATION |
598 		IEEE80211_HW_SPECTRUM_MGMT |
599 		IEEE80211_HW_HAS_RATE_CONTROL |
600 		IEEE80211_HW_RX_INCLUDES_FCS |
601 		IEEE80211_HW_PS_NULLFUNC_STACK |
602 		IEEE80211_HW_REPORTS_TX_ACK_STATUS |
603 		IEEE80211_HW_MFP_CAPABLE |
604 		IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
605 
606 	if (ath9k_ps_enable)
607 		hw->flags |= IEEE80211_HW_SUPPORTS_PS;
608 
609 	hw->wiphy->interface_modes =
610 		BIT(NL80211_IFTYPE_STATION) |
611 		BIT(NL80211_IFTYPE_ADHOC) |
612 		BIT(NL80211_IFTYPE_AP) |
613 		BIT(NL80211_IFTYPE_P2P_GO) |
614 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
615 		BIT(NL80211_IFTYPE_MESH_POINT);
616 
617 	hw->wiphy->iface_combinations = &if_comb;
618 	hw->wiphy->n_iface_combinations = 1;
619 
620 	hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
621 
622 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN |
623 			    WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
624 
625 	hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
626 
627 	hw->queues = 4;
628 	hw->max_listen_interval = 1;
629 
630 	hw->vif_data_size = sizeof(struct ath9k_htc_vif);
631 	hw->sta_data_size = sizeof(struct ath9k_htc_sta);
632 
633 	/* tx_frame_hdr is larger than tx_mgmt_hdr anyway */
634 	hw->extra_tx_headroom = sizeof(struct tx_frame_hdr) +
635 		sizeof(struct htc_frame_hdr) + 4;
636 
637 	if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ)
638 		hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
639 			&common->sbands[IEEE80211_BAND_2GHZ];
640 	if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ)
641 		hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
642 			&common->sbands[IEEE80211_BAND_5GHZ];
643 
644 	ath9k_cmn_reload_chainmask(ah);
645 
646 	pBase = ath9k_htc_get_eeprom_base(priv);
647 	if (pBase) {
648 		hw->wiphy->available_antennas_rx = pBase->rxMask;
649 		hw->wiphy->available_antennas_tx = pBase->txMask;
650 	}
651 
652 	SET_IEEE80211_PERM_ADDR(hw, common->macaddr);
653 }
654 
655 static int ath9k_init_firmware_version(struct ath9k_htc_priv *priv)
656 {
657 	struct ieee80211_hw *hw = priv->hw;
658 	struct wmi_fw_version cmd_rsp;
659 	int ret;
660 
661 	memset(&cmd_rsp, 0, sizeof(cmd_rsp));
662 
663 	WMI_CMD(WMI_GET_FW_VERSION);
664 	if (ret)
665 		return -EINVAL;
666 
667 	priv->fw_version_major = be16_to_cpu(cmd_rsp.major);
668 	priv->fw_version_minor = be16_to_cpu(cmd_rsp.minor);
669 
670 	snprintf(hw->wiphy->fw_version, sizeof(hw->wiphy->fw_version), "%d.%d",
671 		 priv->fw_version_major,
672 		 priv->fw_version_minor);
673 
674 	dev_info(priv->dev, "ath9k_htc: FW Version: %d.%d\n",
675 		 priv->fw_version_major,
676 		 priv->fw_version_minor);
677 
678 	/*
679 	 * Check if the available FW matches the driver's
680 	 * required version.
681 	 */
682 	if (priv->fw_version_major != MAJOR_VERSION_REQ ||
683 	    priv->fw_version_minor < MINOR_VERSION_REQ) {
684 		dev_err(priv->dev, "ath9k_htc: Please upgrade to FW version %d.%d\n",
685 			MAJOR_VERSION_REQ, MINOR_VERSION_REQ);
686 		return -EINVAL;
687 	}
688 
689 	return 0;
690 }
691 
692 static int ath9k_init_device(struct ath9k_htc_priv *priv,
693 			     u16 devid, char *product, u32 drv_info)
694 {
695 	struct ieee80211_hw *hw = priv->hw;
696 	struct ath_common *common;
697 	struct ath_hw *ah;
698 	int error = 0;
699 	struct ath_regulatory *reg;
700 	char hw_name[64];
701 
702 	/* Bring up device */
703 	error = ath9k_init_priv(priv, devid, product, drv_info);
704 	if (error != 0)
705 		goto err_init;
706 
707 	ah = priv->ah;
708 	common = ath9k_hw_common(ah);
709 	ath9k_set_hw_capab(priv, hw);
710 
711 	error = ath9k_init_firmware_version(priv);
712 	if (error != 0)
713 		goto err_fw;
714 
715 	/* Initialize regulatory */
716 	error = ath_regd_init(&common->regulatory, priv->hw->wiphy,
717 			      ath9k_reg_notifier);
718 	if (error)
719 		goto err_regd;
720 
721 	reg = &common->regulatory;
722 
723 	/* Setup TX */
724 	error = ath9k_tx_init(priv);
725 	if (error != 0)
726 		goto err_tx;
727 
728 	/* Setup RX */
729 	error = ath9k_rx_init(priv);
730 	if (error != 0)
731 		goto err_rx;
732 
733 	ath9k_hw_disable(priv->ah);
734 #ifdef CONFIG_MAC80211_LEDS
735 	/* must be initialized before ieee80211_register_hw */
736 	priv->led_cdev.default_trigger = ieee80211_create_tpt_led_trigger(priv->hw,
737 		IEEE80211_TPT_LEDTRIG_FL_RADIO, ath9k_htc_tpt_blink,
738 		ARRAY_SIZE(ath9k_htc_tpt_blink));
739 #endif
740 
741 	/* Register with mac80211 */
742 	error = ieee80211_register_hw(hw);
743 	if (error)
744 		goto err_register;
745 
746 	/* Handle world regulatory */
747 	if (!ath_is_world_regd(reg)) {
748 		error = regulatory_hint(hw->wiphy, reg->alpha2);
749 		if (error)
750 			goto err_world;
751 	}
752 
753 	error = ath9k_htc_init_debug(priv->ah);
754 	if (error) {
755 		ath_err(common, "Unable to create debugfs files\n");
756 		goto err_world;
757 	}
758 
759 	ath_dbg(common, CONFIG,
760 		"WMI:%d, BCN:%d, CAB:%d, UAPSD:%d, MGMT:%d, BE:%d, BK:%d, VI:%d, VO:%d\n",
761 		priv->wmi_cmd_ep,
762 		priv->beacon_ep,
763 		priv->cab_ep,
764 		priv->uapsd_ep,
765 		priv->mgmt_ep,
766 		priv->data_be_ep,
767 		priv->data_bk_ep,
768 		priv->data_vi_ep,
769 		priv->data_vo_ep);
770 
771 	ath9k_hw_name(priv->ah, hw_name, sizeof(hw_name));
772 	wiphy_info(hw->wiphy, "%s\n", hw_name);
773 
774 	ath9k_init_leds(priv);
775 	ath9k_start_rfkill_poll(priv);
776 
777 	return 0;
778 
779 err_world:
780 	ieee80211_unregister_hw(hw);
781 err_register:
782 	ath9k_rx_cleanup(priv);
783 err_rx:
784 	ath9k_tx_cleanup(priv);
785 err_tx:
786 	/* Nothing */
787 err_regd:
788 	/* Nothing */
789 err_fw:
790 	ath9k_deinit_priv(priv);
791 err_init:
792 	return error;
793 }
794 
795 int ath9k_htc_probe_device(struct htc_target *htc_handle, struct device *dev,
796 			   u16 devid, char *product, u32 drv_info)
797 {
798 	struct ieee80211_hw *hw;
799 	struct ath9k_htc_priv *priv;
800 	int ret;
801 
802 	hw = ieee80211_alloc_hw(sizeof(struct ath9k_htc_priv), &ath9k_htc_ops);
803 	if (!hw)
804 		return -ENOMEM;
805 
806 	priv = hw->priv;
807 	priv->hw = hw;
808 	priv->htc = htc_handle;
809 	priv->dev = dev;
810 	htc_handle->drv_priv = priv;
811 	SET_IEEE80211_DEV(hw, priv->dev);
812 
813 	ret = ath9k_htc_wait_for_target(priv);
814 	if (ret)
815 		goto err_free;
816 
817 	priv->wmi = ath9k_init_wmi(priv);
818 	if (!priv->wmi) {
819 		ret = -EINVAL;
820 		goto err_free;
821 	}
822 
823 	ret = ath9k_init_htc_services(priv, devid, drv_info);
824 	if (ret)
825 		goto err_init;
826 
827 	ret = ath9k_init_device(priv, devid, product, drv_info);
828 	if (ret)
829 		goto err_init;
830 
831 	return 0;
832 
833 err_init:
834 	ath9k_deinit_wmi(priv);
835 err_free:
836 	ieee80211_free_hw(hw);
837 	return ret;
838 }
839 
840 void ath9k_htc_disconnect_device(struct htc_target *htc_handle, bool hotunplug)
841 {
842 	if (htc_handle->drv_priv) {
843 
844 		/* Check if the device has been yanked out. */
845 		if (hotunplug)
846 			htc_handle->drv_priv->ah->ah_flags |= AH_UNPLUGGED;
847 
848 		ath9k_deinit_device(htc_handle->drv_priv);
849 		ath9k_deinit_wmi(htc_handle->drv_priv);
850 		ieee80211_free_hw(htc_handle->drv_priv->hw);
851 	}
852 }
853 
854 #ifdef CONFIG_PM
855 
856 void ath9k_htc_suspend(struct htc_target *htc_handle)
857 {
858 	ath9k_htc_setpower(htc_handle->drv_priv, ATH9K_PM_FULL_SLEEP);
859 }
860 
861 int ath9k_htc_resume(struct htc_target *htc_handle)
862 {
863 	struct ath9k_htc_priv *priv = htc_handle->drv_priv;
864 	int ret;
865 
866 	ret = ath9k_htc_wait_for_target(priv);
867 	if (ret)
868 		return ret;
869 
870 	ret = ath9k_init_htc_services(priv, priv->ah->hw_version.devid,
871 				      priv->ah->hw_version.usbdev);
872 	ath9k_configure_leds(priv);
873 
874 	return ret;
875 }
876 #endif
877 
878 static int __init ath9k_htc_init(void)
879 {
880 	if (ath9k_hif_usb_init() < 0) {
881 		pr_err("No USB devices found, driver not installed\n");
882 		return -ENODEV;
883 	}
884 
885 	return 0;
886 }
887 module_init(ath9k_htc_init);
888 
889 static void __exit ath9k_htc_exit(void)
890 {
891 	ath9k_hif_usb_exit();
892 	pr_info("Driver unloaded\n");
893 }
894 module_exit(ath9k_htc_exit);
895