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