htc_drv_init.c 26.2 KB
Newer Older
S
Sujith 已提交
1
/*
2
 * Copyright (c) 2010-2011 Atheros Communications Inc.
S
Sujith 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

17 18
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

S
Sujith 已提交
19 20 21 22 23 24 25 26 27 28
#include "htc.h"

MODULE_AUTHOR("Atheros Communications");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_DESCRIPTION("Atheros driver 802.11n HTC based wireless devices");

static unsigned int ath9k_debug = ATH_DBG_DEFAULT;
module_param_named(debug, ath9k_debug, uint, 0);
MODULE_PARM_DESC(debug, "Debugging mask");

29 30
int htc_modparam_nohwcrypt;
module_param_named(nohwcrypt, htc_modparam_nohwcrypt, int, 0444);
S
Sujith 已提交
31 32
MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption");

33 34 35 36
static int ath9k_htc_btcoex_enable;
module_param_named(btcoex_enable, ath9k_htc_btcoex_enable, int, 0444);
MODULE_PARM_DESC(btcoex_enable, "Enable wifi-BT coexistence");

37 38 39 40
static int ath9k_ps_enable;
module_param_named(ps_enable, ath9k_ps_enable, int, 0444);
MODULE_PARM_DESC(ps_enable, "Enable WLAN PowerSave");

41 42 43 44
int htc_use_dev_fw = 0;
module_param_named(use_dev_fw, htc_use_dev_fw, int, 0444);
MODULE_PARM_DESC(use_dev_fw, "Use development FW version");

45
#ifdef CONFIG_MAC80211_LEDS
46 47
int ath9k_htc_led_blink = 1;
module_param_named(blink, ath9k_htc_led_blink, int, 0444);
48 49
MODULE_PARM_DESC(blink, "Enable LED blink on activity");

50 51 52 53 54 55 56 57 58 59 60 61 62 63
static const struct ieee80211_tpt_blink ath9k_htc_tpt_blink[] = {
	{ .throughput = 0 * 1024, .blink_time = 334 },
	{ .throughput = 1 * 1024, .blink_time = 260 },
	{ .throughput = 5 * 1024, .blink_time = 220 },
	{ .throughput = 10 * 1024, .blink_time = 190 },
	{ .throughput = 20 * 1024, .blink_time = 170 },
	{ .throughput = 50 * 1024, .blink_time = 150 },
	{ .throughput = 70 * 1024, .blink_time = 130 },
	{ .throughput = 100 * 1024, .blink_time = 110 },
	{ .throughput = 200 * 1024, .blink_time = 80 },
	{ .throughput = 300 * 1024, .blink_time = 50 },
};
#endif

64
static void ath9k_htc_op_ps_wakeup(struct ath_common *common)
65 66 67 68
{
	ath9k_htc_ps_wakeup((struct ath9k_htc_priv *) common->priv);
}

69
static void ath9k_htc_op_ps_restore(struct ath_common *common)
70 71 72 73
{
	ath9k_htc_ps_restore((struct ath9k_htc_priv *) common->priv);
}

74
static const struct ath_ps_ops ath9k_htc_ps_ops = {
75 76 77 78
	.wakeup = ath9k_htc_op_ps_wakeup,
	.restore = ath9k_htc_op_ps_restore,
};

S
Sujith 已提交
79 80
static int ath9k_htc_wait_for_target(struct ath9k_htc_priv *priv)
{
81
	unsigned long time_left;
S
Sujith 已提交
82

83 84 85 86 87
	if (atomic_read(&priv->htc->tgt_ready) > 0) {
		atomic_dec(&priv->htc->tgt_ready);
		return 0;
	}

S
Sujith 已提交
88 89 90 91 92 93 94
	/* Firmware can take up to 50ms to get ready, to be safe use 1 second */
	time_left = wait_for_completion_timeout(&priv->htc->target_wait, HZ);
	if (!time_left) {
		dev_err(priv->dev, "ath9k_htc: Target is unresponsive\n");
		return -ETIMEDOUT;
	}

95 96
	atomic_dec(&priv->htc->tgt_ready);

S
Sujith 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	return 0;
}

static void ath9k_deinit_priv(struct ath9k_htc_priv *priv)
{
	ath9k_hw_deinit(priv->ah);
	kfree(priv->ah);
	priv->ah = NULL;
}

static void ath9k_deinit_device(struct ath9k_htc_priv *priv)
{
	struct ieee80211_hw *hw = priv->hw;

	wiphy_rfkill_stop_polling(hw->wiphy);
	ath9k_deinit_leds(priv);
113
	ath9k_htc_deinit_debug(priv);
S
Sujith 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
	ieee80211_unregister_hw(hw);
	ath9k_rx_cleanup(priv);
	ath9k_tx_cleanup(priv);
	ath9k_deinit_priv(priv);
}

static inline int ath9k_htc_connect_svc(struct ath9k_htc_priv *priv,
					u16 service_id,
					void (*tx) (void *,
						    struct sk_buff *,
						    enum htc_endpoint_id,
						    bool txok),
					enum htc_endpoint_id *ep_id)
{
	struct htc_service_connreq req;

	memset(&req, 0, sizeof(struct htc_service_connreq));

	req.service_id = service_id;
	req.ep_callbacks.priv = priv;
	req.ep_callbacks.rx = ath9k_htc_rxep;
	req.ep_callbacks.tx = tx;

	return htc_connect_service(priv->htc, &req, ep_id);
}

140 141
static int ath9k_init_htc_services(struct ath9k_htc_priv *priv, u16 devid,
				   u32 drv_info)
S
Sujith 已提交
142 143 144 145 146 147 148 149 150
{
	int ret;

	/* WMI CMD*/
	ret = ath9k_wmi_connect(priv->htc, priv->wmi, &priv->wmi_cmd_ep);
	if (ret)
		goto err;

	/* Beacon */
S
Sujith 已提交
151
	ret = ath9k_htc_connect_svc(priv, WMI_BEACON_SVC, ath9k_htc_beaconep,
S
Sujith 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
				    &priv->beacon_ep);
	if (ret)
		goto err;

	/* CAB */
	ret = ath9k_htc_connect_svc(priv, WMI_CAB_SVC, ath9k_htc_txep,
				    &priv->cab_ep);
	if (ret)
		goto err;


	/* UAPSD */
	ret = ath9k_htc_connect_svc(priv, WMI_UAPSD_SVC, ath9k_htc_txep,
				    &priv->uapsd_ep);
	if (ret)
		goto err;

	/* MGMT */
	ret = ath9k_htc_connect_svc(priv, WMI_MGMT_SVC, ath9k_htc_txep,
				    &priv->mgmt_ep);
	if (ret)
		goto err;

	/* DATA BE */
	ret = ath9k_htc_connect_svc(priv, WMI_DATA_BE_SVC, ath9k_htc_txep,
				    &priv->data_be_ep);
	if (ret)
		goto err;

	/* DATA BK */
	ret = ath9k_htc_connect_svc(priv, WMI_DATA_BK_SVC, ath9k_htc_txep,
				    &priv->data_bk_ep);
	if (ret)
		goto err;

	/* DATA VI */
	ret = ath9k_htc_connect_svc(priv, WMI_DATA_VI_SVC, ath9k_htc_txep,
				    &priv->data_vi_ep);
	if (ret)
		goto err;

	/* DATA VO */
	ret = ath9k_htc_connect_svc(priv, WMI_DATA_VO_SVC, ath9k_htc_txep,
				    &priv->data_vo_ep);
	if (ret)
		goto err;

199 200 201 202 203 204
	/*
	 * Setup required credits before initializing HTC.
	 * This is a bit hacky, but, since queuing is done in
	 * the HIF layer, shouldn't matter much.
	 */

205
	if (IS_AR7010_DEVICE(drv_info))
206
		priv->htc->credits = 45;
207
	else
208
		priv->htc->credits = 33;
209

S
Sujith 已提交
210 211 212 213
	ret = htc_init(priv->htc);
	if (ret)
		goto err;

214 215 216
	dev_info(priv->dev, "ath9k_htc: HTC initialized with %d credits\n",
		 priv->htc->credits);

S
Sujith 已提交
217 218 219 220 221 222 223
	return 0;

err:
	dev_err(priv->dev, "ath9k_htc: Unable to initialize HTC services\n");
	return ret;
}

224 225
static void ath9k_reg_notifier(struct wiphy *wiphy,
			       struct regulatory_request *request)
S
Sujith 已提交
226 227 228 229
{
	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
	struct ath9k_htc_priv *priv = hw->priv;

230 231
	ath_reg_notifier_apply(wiphy, request,
			       ath9k_hw_regulatory(priv->ah));
S
Sujith 已提交
232 233
}

234
static unsigned int ath9k_regread(void *hw_priv, u32 reg_offset)
S
Sujith 已提交
235 236 237 238 239 240 241 242 243 244 245 246
{
	struct ath_hw *ah = (struct ath_hw *) hw_priv;
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
	__be32 val, reg = cpu_to_be32(reg_offset);
	int r;

	r = ath9k_wmi_cmd(priv->wmi, WMI_REG_READ_CMDID,
			  (u8 *) &reg, sizeof(reg),
			  (u8 *) &val, sizeof(val),
			  100);
	if (unlikely(r)) {
247
		ath_dbg(common, WMI, "REGISTER READ FAILED: (0x%04x, %d)\n",
J
Joe Perches 已提交
248
			reg_offset, r);
S
Sujith 已提交
249 250 251 252 253 254
		return -EIO;
	}

	return be32_to_cpu(val);
}

255 256 257 258 259 260 261 262 263 264
static void ath9k_multi_regread(void *hw_priv, u32 *addr,
				u32 *val, u16 count)
{
	struct ath_hw *ah = (struct ath_hw *) hw_priv;
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
	__be32 tmpaddr[8];
	__be32 tmpval[8];
	int i, ret;

265 266 267
	for (i = 0; i < count; i++) {
		tmpaddr[i] = cpu_to_be32(addr[i]);
	}
268

269
	ret = ath9k_wmi_cmd(priv->wmi, WMI_REG_READ_CMDID,
270 271 272 273
			   (u8 *)tmpaddr , sizeof(u32) * count,
			   (u8 *)tmpval, sizeof(u32) * count,
			   100);
	if (unlikely(ret)) {
274
		ath_dbg(common, WMI,
275 276 277
			"Multiple REGISTER READ FAILED (count: %d)\n", count);
	}

278 279 280
	for (i = 0; i < count; i++) {
		val[i] = be32_to_cpu(tmpval[i]);
	}
281 282
}

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
static void ath9k_regwrite_multi(struct ath_common *common)
{
	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
	u32 rsp_status;
	int r;

	r = ath9k_wmi_cmd(priv->wmi, WMI_REG_WRITE_CMDID,
			  (u8 *) &priv->wmi->multi_write,
			  sizeof(struct register_write) * priv->wmi->multi_write_idx,
			  (u8 *) &rsp_status, sizeof(rsp_status),
			  100);
	if (unlikely(r)) {
		ath_dbg(common, WMI,
			"REGISTER WRITE FAILED, multi len: %d\n",
			priv->wmi->multi_write_idx);
	}
	priv->wmi->multi_write_idx = 0;
}

302
static void ath9k_regwrite_single(void *hw_priv, u32 val, u32 reg_offset)
S
Sujith 已提交
303 304 305 306
{
	struct ath_hw *ah = (struct ath_hw *) hw_priv;
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
J
Joe Perches 已提交
307
	const __be32 buf[2] = {
S
Sujith 已提交
308 309 310 311 312 313 314 315 316 317
		cpu_to_be32(reg_offset),
		cpu_to_be32(val),
	};
	int r;

	r = ath9k_wmi_cmd(priv->wmi, WMI_REG_WRITE_CMDID,
			  (u8 *) &buf, sizeof(buf),
			  (u8 *) &val, sizeof(val),
			  100);
	if (unlikely(r)) {
318
		ath_dbg(common, WMI, "REGISTER WRITE FAILED:(0x%04x, %d)\n",
J
Joe Perches 已提交
319
			reg_offset, r);
S
Sujith 已提交
320 321 322
	}
}

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
static void ath9k_regwrite_buffer(void *hw_priv, u32 val, u32 reg_offset)
{
	struct ath_hw *ah = (struct ath_hw *) hw_priv;
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;

	mutex_lock(&priv->wmi->multi_write_mutex);

	/* Store the register/value */
	priv->wmi->multi_write[priv->wmi->multi_write_idx].reg =
		cpu_to_be32(reg_offset);
	priv->wmi->multi_write[priv->wmi->multi_write_idx].val =
		cpu_to_be32(val);

	priv->wmi->multi_write_idx++;

	/* If the buffer is full, send it out. */
340 341
	if (priv->wmi->multi_write_idx == MAX_CMD_NUMBER)
		ath9k_regwrite_multi(common);
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372

	mutex_unlock(&priv->wmi->multi_write_mutex);
}

static void ath9k_regwrite(void *hw_priv, u32 val, u32 reg_offset)
{
	struct ath_hw *ah = (struct ath_hw *) hw_priv;
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;

	if (atomic_read(&priv->wmi->mwrite_cnt))
		ath9k_regwrite_buffer(hw_priv, val, reg_offset);
	else
		ath9k_regwrite_single(hw_priv, val, reg_offset);
}

static void ath9k_enable_regwrite_buffer(void *hw_priv)
{
	struct ath_hw *ah = (struct ath_hw *) hw_priv;
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;

	atomic_inc(&priv->wmi->mwrite_cnt);
}

static void ath9k_regwrite_flush(void *hw_priv)
{
	struct ath_hw *ah = (struct ath_hw *) hw_priv;
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;

373 374
	atomic_dec(&priv->wmi->mwrite_cnt);

375 376
	mutex_lock(&priv->wmi->multi_write_mutex);

377 378
	if (priv->wmi->multi_write_idx)
		ath9k_regwrite_multi(common);
379 380 381 382

	mutex_unlock(&priv->wmi->multi_write_mutex);
}

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
static void ath9k_reg_rmw_buffer(void *hw_priv,
				 u32 reg_offset, u32 set, u32 clr)
{
	struct ath_hw *ah = (struct ath_hw *) hw_priv;
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
	u32 rsp_status;
	int r;

	mutex_lock(&priv->wmi->multi_rmw_mutex);

	/* Store the register/value */
	priv->wmi->multi_rmw[priv->wmi->multi_rmw_idx].reg =
		cpu_to_be32(reg_offset);
	priv->wmi->multi_rmw[priv->wmi->multi_rmw_idx].set =
		cpu_to_be32(set);
	priv->wmi->multi_rmw[priv->wmi->multi_rmw_idx].clr =
		cpu_to_be32(clr);

	priv->wmi->multi_rmw_idx++;

	/* If the buffer is full, send it out. */
	if (priv->wmi->multi_rmw_idx == MAX_RMW_CMD_NUMBER) {
		r = ath9k_wmi_cmd(priv->wmi, WMI_REG_RMW_CMDID,
			  (u8 *) &priv->wmi->multi_rmw,
			  sizeof(struct register_write) * priv->wmi->multi_rmw_idx,
			  (u8 *) &rsp_status, sizeof(rsp_status),
			  100);
		if (unlikely(r)) {
			ath_dbg(common, WMI,
				"REGISTER RMW FAILED, multi len: %d\n",
				priv->wmi->multi_rmw_idx);
		}
		priv->wmi->multi_rmw_idx = 0;
	}

	mutex_unlock(&priv->wmi->multi_rmw_mutex);
}

static void ath9k_reg_rmw_flush(void *hw_priv)
423
{
424 425 426 427 428 429 430 431 432 433
	struct ath_hw *ah = (struct ath_hw *) hw_priv;
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
	u32 rsp_status;
	int r;

	if (test_bit(HTC_FWFLAG_NO_RMW, &priv->fw_flags))
		return;

	atomic_dec(&priv->wmi->m_rmw_cnt);
434

435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
	mutex_lock(&priv->wmi->multi_rmw_mutex);

	if (priv->wmi->multi_rmw_idx) {
		r = ath9k_wmi_cmd(priv->wmi, WMI_REG_RMW_CMDID,
			  (u8 *) &priv->wmi->multi_rmw,
			  sizeof(struct register_rmw) * priv->wmi->multi_rmw_idx,
			  (u8 *) &rsp_status, sizeof(rsp_status),
			  100);
		if (unlikely(r)) {
			ath_dbg(common, WMI,
				"REGISTER RMW FAILED, multi len: %d\n",
				priv->wmi->multi_rmw_idx);
		}
		priv->wmi->multi_rmw_idx = 0;
	}

	mutex_unlock(&priv->wmi->multi_rmw_mutex);
}

static void ath9k_enable_rmw_buffer(void *hw_priv)
{
	struct ath_hw *ah = (struct ath_hw *) hw_priv;
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;

	if (test_bit(HTC_FWFLAG_NO_RMW, &priv->fw_flags))
		return;

	atomic_inc(&priv->wmi->m_rmw_cnt);
}

static u32 ath9k_reg_rmw_single(void *hw_priv,
				 u32 reg_offset, u32 set, u32 clr)
{
	struct ath_hw *ah = (struct ath_hw *) hw_priv;
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
	struct register_rmw buf, buf_ret;
	int ret;
	u32 val = 0;

	buf.reg = cpu_to_be32(reg_offset);
	buf.set = cpu_to_be32(set);
	buf.clr = cpu_to_be32(clr);

	ret = ath9k_wmi_cmd(priv->wmi, WMI_REG_RMW_CMDID,
			  (u8 *) &buf, sizeof(buf),
			  (u8 *) &buf_ret, sizeof(buf_ret),
			  100);
	if (unlikely(ret)) {
		ath_dbg(common, WMI, "REGISTER RMW FAILED:(0x%04x, %d)\n",
			reg_offset, ret);
	}
488 489 490
	return val;
}

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
static u32 ath9k_reg_rmw(void *hw_priv, u32 reg_offset, u32 set, u32 clr)
{
	struct ath_hw *ah = (struct ath_hw *) hw_priv;
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;

	if (test_bit(HTC_FWFLAG_NO_RMW, &priv->fw_flags)) {
		u32 val;

		val = REG_READ(ah, reg_offset);
		val &= ~clr;
		val |= set;
		REG_WRITE(ah, reg_offset, val);

		return 0;
	}

	if (atomic_read(&priv->wmi->m_rmw_cnt))
		ath9k_reg_rmw_buffer(hw_priv, reg_offset, set, clr);
	else
		ath9k_reg_rmw_single(hw_priv, reg_offset, set, clr);

	return 0;
}

S
Sujith 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
static void ath_usb_read_cachesize(struct ath_common *common, int *csz)
{
	*csz = L1_CACHE_BYTES >> 2;
}

static bool ath_usb_eeprom_read(struct ath_common *common, u32 off, u16 *data)
{
	struct ath_hw *ah = (struct ath_hw *) common->ah;

	(void)REG_READ(ah, AR5416_EEPROM_OFFSET + (off << AR5416_EEPROM_S));

	if (!ath9k_hw_wait(ah,
			   AR_EEPROM_STATUS_DATA,
			   AR_EEPROM_STATUS_DATA_BUSY |
			   AR_EEPROM_STATUS_DATA_PROT_ACCESS, 0,
			   AH_WAIT_TIMEOUT))
		return false;

	*data = MS(REG_READ(ah, AR_EEPROM_STATUS_DATA),
		   AR_EEPROM_STATUS_DATA_VAL);

	return true;
}

static const struct ath_bus_ops ath9k_usb_bus_ops = {
S
Sujith 已提交
541
	.ath_bus_type = ATH_USB,
S
Sujith 已提交
542 543 544 545 546 547 548 549 550 551 552 553
	.read_cachesize = ath_usb_read_cachesize,
	.eeprom_read = ath_usb_eeprom_read,
};

static int ath9k_init_queues(struct ath9k_htc_priv *priv)
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
	int i;

	for (i = 0; i < ARRAY_SIZE(priv->hwq_map); i++)
		priv->hwq_map[i] = -1;

554 555
	priv->beacon.beaconq = ath9k_hw_beaconq_setup(priv->ah);
	if (priv->beacon.beaconq == -1) {
556
		ath_err(common, "Unable to setup BEACON xmit queue\n");
S
Sujith 已提交
557 558 559 560 561
		goto err;
	}

	priv->cabq = ath9k_htc_cabq_setup(priv);
	if (priv->cabq == -1) {
562
		ath_err(common, "Unable to setup CAB xmit queue\n");
S
Sujith 已提交
563 564 565
		goto err;
	}

566
	if (!ath9k_htc_txq_setup(priv, IEEE80211_AC_BE)) {
567
		ath_err(common, "Unable to setup xmit queue for BE traffic\n");
S
Sujith 已提交
568 569 570
		goto err;
	}

571
	if (!ath9k_htc_txq_setup(priv, IEEE80211_AC_BK)) {
572
		ath_err(common, "Unable to setup xmit queue for BK traffic\n");
S
Sujith 已提交
573 574
		goto err;
	}
575
	if (!ath9k_htc_txq_setup(priv, IEEE80211_AC_VI)) {
576
		ath_err(common, "Unable to setup xmit queue for VI traffic\n");
S
Sujith 已提交
577 578
		goto err;
	}
579
	if (!ath9k_htc_txq_setup(priv, IEEE80211_AC_VO)) {
580
		ath_err(common, "Unable to setup xmit queue for VO traffic\n");
S
Sujith 已提交
581 582 583 584 585 586 587 588 589 590 591 592 593
		goto err;
	}

	return 0;

err:
	return -EINVAL;
}

static void ath9k_init_misc(struct ath9k_htc_priv *priv)
{
	struct ath_common *common = ath9k_hw_common(priv->ah);

594
	memcpy(common->bssidmask, ath_bcast_mac, ETH_ALEN);
S
Sujith 已提交
595

596
	common->last_rssi = ATH_RSSI_DUMMY_MARKER;
S
Sujith 已提交
597
	priv->ah->opmode = NL80211_IFTYPE_STATION;
598 599 600

	priv->spec_priv.ah = priv->ah;
	priv->spec_priv.spec_config.enabled = 0;
601
	priv->spec_priv.spec_config.short_repeat = true;
602 603 604 605
	priv->spec_priv.spec_config.count = 8;
	priv->spec_priv.spec_config.endless = false;
	priv->spec_priv.spec_config.period = 0x12;
	priv->spec_priv.spec_config.fft_period = 0x02;
S
Sujith 已提交
606 607
}

608
static int ath9k_init_priv(struct ath9k_htc_priv *priv,
609 610
			   u16 devid, char *product,
			   u32 drv_info)
S
Sujith 已提交
611 612 613
{
	struct ath_hw *ah = NULL;
	struct ath_common *common;
S
Sujith Manoharan 已提交
614
	int i, ret = 0, csz = 0;
S
Sujith 已提交
615 616 617 618 619

	ah = kzalloc(sizeof(struct ath_hw), GFP_KERNEL);
	if (!ah)
		return -ENOMEM;

620
	ah->dev = priv->dev;
621
	ah->hw = priv->hw;
S
Sujith 已提交
622
	ah->hw_version.devid = devid;
623
	ah->hw_version.usbdev = drv_info;
F
Felix Fietkau 已提交
624
	ah->ah_flags |= AH_USE_EEPROM;
625 626 627 628 629
	ah->reg_ops.read = ath9k_regread;
	ah->reg_ops.multi_read = ath9k_multi_regread;
	ah->reg_ops.write = ath9k_regwrite;
	ah->reg_ops.enable_write_buffer = ath9k_enable_regwrite_buffer;
	ah->reg_ops.write_flush = ath9k_regwrite_flush;
630 631
	ah->reg_ops.enable_rmw_buffer = ath9k_enable_rmw_buffer;
	ah->reg_ops.rmw_flush = ath9k_reg_rmw_flush;
632
	ah->reg_ops.rmw = ath9k_reg_rmw;
S
Sujith 已提交
633 634 635
	priv->ah = ah;

	common = ath9k_hw_common(ah);
636
	common->ops = &ah->reg_ops;
637
	common->ps_ops = &ath9k_htc_ps_ops;
S
Sujith 已提交
638 639 640 641 642
	common->bus_ops = &ath9k_usb_bus_ops;
	common->ah = ah;
	common->hw = priv->hw;
	common->priv = priv;
	common->debug_mask = ath9k_debug;
643
	common->btcoex_enabled = ath9k_htc_btcoex_enable == 1;
O
Oleksij Rempel 已提交
644
	set_bit(ATH_OP_INVALID, &common->op_flags);
S
Sujith 已提交
645 646

	spin_lock_init(&priv->beacon_lock);
647
	spin_lock_init(&priv->tx.tx_lock);
S
Sujith 已提交
648
	mutex_init(&priv->mutex);
649
	mutex_init(&priv->htc_pm_lock);
S
Sujith 已提交
650 651
	tasklet_init(&priv->rx_tasklet, ath9k_rx_tasklet,
		     (unsigned long)priv);
652
	tasklet_init(&priv->tx_failed_tasklet, ath9k_tx_failed_tasklet,
653
		     (unsigned long)priv);
654
	INIT_DELAYED_WORK(&priv->ani_work, ath9k_htc_ani_work);
655
	INIT_WORK(&priv->ps_work, ath9k_ps_work);
656
	INIT_WORK(&priv->fatal_work, ath9k_fatal_work);
657 658
	setup_timer(&priv->tx.cleanup_timer, ath9k_htc_tx_cleanup_timer,
		    (unsigned long)priv);
S
Sujith 已提交
659 660 661 662 663 664 665 666 667 668

	/*
	 * Cache line size is used to size and align various
	 * structures used to communicate with the hardware.
	 */
	ath_read_cachesize(common, &csz);
	common->cachelsz = csz << 2; /* convert to bytes */

	ret = ath9k_hw_init(ah);
	if (ret) {
669 670 671
		ath_err(common,
			"Unable to initialize hardware; initialization status: %d\n",
			ret);
S
Sujith 已提交
672 673 674 675 676 677 678
		goto err_hw;
	}

	ret = ath9k_init_queues(priv);
	if (ret)
		goto err_queues;

S
Sujith Manoharan 已提交
679
	for (i = 0; i < ATH9K_HTC_MAX_BCN_VIF; i++)
680
		priv->beacon.bslot[i] = NULL;
681
	priv->beacon.slottime = 9;
S
Sujith Manoharan 已提交
682

683
	ath9k_cmn_init_channels_rates(common);
684
	ath9k_cmn_init_crypto(ah);
S
Sujith 已提交
685
	ath9k_init_misc(priv);
686
	ath9k_htc_init_btcoex(priv, product);
687

S
Sujith 已提交
688 689 690 691 692 693 694 695 696 697 698 699
	return 0;

err_queues:
	ath9k_hw_deinit(ah);
err_hw:

	kfree(ah);
	priv->ah = NULL;

	return ret;
}

700 701 702 703
static const struct ieee80211_iface_limit if_limits[] = {
	{ .max = 2,	.types = BIT(NL80211_IFTYPE_STATION) |
				 BIT(NL80211_IFTYPE_P2P_CLIENT) },
	{ .max = 2,	.types = BIT(NL80211_IFTYPE_AP) |
704 705 706 707
#ifdef CONFIG_MAC80211_MESH
				 BIT(NL80211_IFTYPE_MESH_POINT) |
#endif
				 BIT(NL80211_IFTYPE_P2P_GO) },
708 709 710 711 712 713 714 715 716
};

static const struct ieee80211_iface_combination if_comb = {
	.limits = if_limits,
	.n_limits = ARRAY_SIZE(if_limits),
	.max_interfaces = 2,
	.num_different_channels = 1,
};

S
Sujith 已提交
717 718 719
static void ath9k_set_hw_capab(struct ath9k_htc_priv *priv,
			       struct ieee80211_hw *hw)
{
720
	struct ath_hw *ah = priv->ah;
S
Sujith 已提交
721
	struct ath_common *common = ath9k_hw_common(priv->ah);
722
	struct base_eep_header *pBase;
S
Sujith 已提交
723

724 725 726 727 728 729 730 731 732
	ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
	ieee80211_hw_set(hw, MFP_CAPABLE);
	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
	ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
	ieee80211_hw_set(hw, RX_INCLUDES_FCS);
	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
	ieee80211_hw_set(hw, SPECTRUM_MGMT);
	ieee80211_hw_set(hw, SIGNAL_DBM);
	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
S
Sujith 已提交
733

734
	if (ath9k_ps_enable)
735
		ieee80211_hw_set(hw, SUPPORTS_PS);
736

S
Sujith 已提交
737 738
	hw->wiphy->interface_modes =
		BIT(NL80211_IFTYPE_STATION) |
739 740 741
		BIT(NL80211_IFTYPE_ADHOC) |
		BIT(NL80211_IFTYPE_AP) |
		BIT(NL80211_IFTYPE_P2P_GO) |
742
		BIT(NL80211_IFTYPE_P2P_CLIENT) |
J
Jan Kaisrlik 已提交
743 744
		BIT(NL80211_IFTYPE_MESH_POINT) |
		BIT(NL80211_IFTYPE_OCB);
S
Sujith 已提交
745

746 747 748
	hw->wiphy->iface_combinations = &if_comb;
	hw->wiphy->n_iface_combinations = 1;

749 750
	hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;

751
	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN |
752 753
			    WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
			    WIPHY_FLAG_HAS_CHANNEL_SWITCH;
754

755 756
	hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;

S
Sujith 已提交
757
	hw->queues = 4;
758
	hw->max_listen_interval = 1;
759

S
Sujith 已提交
760 761 762 763 764 765 766
	hw->vif_data_size = sizeof(struct ath9k_htc_vif);
	hw->sta_data_size = sizeof(struct ath9k_htc_sta);

	/* tx_frame_hdr is larger than tx_mgmt_hdr anyway */
	hw->extra_tx_headroom = sizeof(struct tx_frame_hdr) +
		sizeof(struct htc_frame_hdr) + 4;

767
	if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ)
768 769
		hw->wiphy->bands[NL80211_BAND_2GHZ] =
			&common->sbands[NL80211_BAND_2GHZ];
770
	if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ)
771 772
		hw->wiphy->bands[NL80211_BAND_5GHZ] =
			&common->sbands[NL80211_BAND_5GHZ];
S
Sujith 已提交
773

774
	ath9k_cmn_reload_chainmask(ah);
S
Sujith 已提交
775

776 777 778 779 780 781
	pBase = ath9k_htc_get_eeprom_base(priv);
	if (pBase) {
		hw->wiphy->available_antennas_rx = pBase->rxMask;
		hw->wiphy->available_antennas_tx = pBase->txMask;
	}

S
Sujith 已提交
782
	SET_IEEE80211_PERM_ADDR(hw, common->macaddr);
783 784

	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
S
Sujith 已提交
785 786
}

787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
static int ath9k_init_firmware_version(struct ath9k_htc_priv *priv)
{
	struct ieee80211_hw *hw = priv->hw;
	struct wmi_fw_version cmd_rsp;
	int ret;

	memset(&cmd_rsp, 0, sizeof(cmd_rsp));

	WMI_CMD(WMI_GET_FW_VERSION);
	if (ret)
		return -EINVAL;

	priv->fw_version_major = be16_to_cpu(cmd_rsp.major);
	priv->fw_version_minor = be16_to_cpu(cmd_rsp.minor);

802
	snprintf(hw->wiphy->fw_version, sizeof(hw->wiphy->fw_version), "%d.%d",
803 804 805 806 807 808 809
		 priv->fw_version_major,
		 priv->fw_version_minor);

	dev_info(priv->dev, "ath9k_htc: FW Version: %d.%d\n",
		 priv->fw_version_major,
		 priv->fw_version_minor);

810 811 812 813 814
	/*
	 * Check if the available FW matches the driver's
	 * required version.
	 */
	if (priv->fw_version_major != MAJOR_VERSION_REQ ||
815
	    priv->fw_version_minor < MINOR_VERSION_REQ) {
816 817 818 819 820
		dev_err(priv->dev, "ath9k_htc: Please upgrade to FW version %d.%d\n",
			MAJOR_VERSION_REQ, MINOR_VERSION_REQ);
		return -EINVAL;
	}

821 822 823 824 825 826
	if (priv->fw_version_major == 1 && priv->fw_version_minor < 4)
		set_bit(HTC_FWFLAG_NO_RMW, &priv->fw_flags);

	dev_info(priv->dev, "FW RMW support: %s\n",
		test_bit(HTC_FWFLAG_NO_RMW, &priv->fw_flags) ? "Off" : "On");

827 828 829
	return 0;
}

830
static int ath9k_init_device(struct ath9k_htc_priv *priv,
831
			     u16 devid, char *product, u32 drv_info)
S
Sujith 已提交
832 833 834 835 836 837
{
	struct ieee80211_hw *hw = priv->hw;
	struct ath_common *common;
	struct ath_hw *ah;
	int error = 0;
	struct ath_regulatory *reg;
838
	char hw_name[64];
S
Sujith 已提交
839 840

	/* Bring up device */
841
	error = ath9k_init_priv(priv, devid, product, drv_info);
S
Sujith 已提交
842 843 844 845 846 847 848
	if (error != 0)
		goto err_init;

	ah = priv->ah;
	common = ath9k_hw_common(ah);
	ath9k_set_hw_capab(priv, hw);

849 850 851 852
	error = ath9k_init_firmware_version(priv);
	if (error != 0)
		goto err_fw;

S
Sujith 已提交
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
	/* Initialize regulatory */
	error = ath_regd_init(&common->regulatory, priv->hw->wiphy,
			      ath9k_reg_notifier);
	if (error)
		goto err_regd;

	reg = &common->regulatory;

	/* Setup TX */
	error = ath9k_tx_init(priv);
	if (error != 0)
		goto err_tx;

	/* Setup RX */
	error = ath9k_rx_init(priv);
	if (error != 0)
		goto err_rx;

871
	ath9k_hw_disable(priv->ah);
872 873 874 875 876 877 878
#ifdef CONFIG_MAC80211_LEDS
	/* must be initialized before ieee80211_register_hw */
	priv->led_cdev.default_trigger = ieee80211_create_tpt_led_trigger(priv->hw,
		IEEE80211_TPT_LEDTRIG_FL_RADIO, ath9k_htc_tpt_blink,
		ARRAY_SIZE(ath9k_htc_tpt_blink));
#endif

S
Sujith 已提交
879 880 881 882 883 884 885 886 887 888 889 890
	/* Register with mac80211 */
	error = ieee80211_register_hw(hw);
	if (error)
		goto err_register;

	/* Handle world regulatory */
	if (!ath_is_world_regd(reg)) {
		error = regulatory_hint(hw->wiphy, reg->alpha2);
		if (error)
			goto err_world;
	}

891 892 893 894 895 896
	error = ath9k_htc_init_debug(priv->ah);
	if (error) {
		ath_err(common, "Unable to create debugfs files\n");
		goto err_world;
	}

897 898
	ath_dbg(common, CONFIG,
		"WMI:%d, BCN:%d, CAB:%d, UAPSD:%d, MGMT:%d, BE:%d, BK:%d, VI:%d, VO:%d\n",
899 900 901 902 903 904 905 906 907 908 909 910 911
		priv->wmi_cmd_ep,
		priv->beacon_ep,
		priv->cab_ep,
		priv->uapsd_ep,
		priv->mgmt_ep,
		priv->data_be_ep,
		priv->data_bk_ep,
		priv->data_vi_ep,
		priv->data_vo_ep);

	ath9k_hw_name(priv->ah, hw_name, sizeof(hw_name));
	wiphy_info(hw->wiphy, "%s\n", hw_name);

S
Sujith 已提交
912 913 914 915 916 917 918 919 920 921 922 923 924 925
	ath9k_init_leds(priv);
	ath9k_start_rfkill_poll(priv);

	return 0;

err_world:
	ieee80211_unregister_hw(hw);
err_register:
	ath9k_rx_cleanup(priv);
err_rx:
	ath9k_tx_cleanup(priv);
err_tx:
	/* Nothing */
err_regd:
926 927
	/* Nothing */
err_fw:
S
Sujith 已提交
928 929 930 931 932 933
	ath9k_deinit_priv(priv);
err_init:
	return error;
}

int ath9k_htc_probe_device(struct htc_target *htc_handle, struct device *dev,
934
			   u16 devid, char *product, u32 drv_info)
S
Sujith 已提交
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
{
	struct ieee80211_hw *hw;
	struct ath9k_htc_priv *priv;
	int ret;

	hw = ieee80211_alloc_hw(sizeof(struct ath9k_htc_priv), &ath9k_htc_ops);
	if (!hw)
		return -ENOMEM;

	priv = hw->priv;
	priv->hw = hw;
	priv->htc = htc_handle;
	priv->dev = dev;
	htc_handle->drv_priv = priv;
	SET_IEEE80211_DEV(hw, priv->dev);

	ret = ath9k_htc_wait_for_target(priv);
	if (ret)
		goto err_free;

	priv->wmi = ath9k_init_wmi(priv);
	if (!priv->wmi) {
		ret = -EINVAL;
		goto err_free;
	}

961
	ret = ath9k_init_htc_services(priv, devid, drv_info);
S
Sujith 已提交
962 963 964
	if (ret)
		goto err_init;

965
	ret = ath9k_init_device(priv, devid, product, drv_info);
S
Sujith 已提交
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
	if (ret)
		goto err_init;

	return 0;

err_init:
	ath9k_deinit_wmi(priv);
err_free:
	ieee80211_free_hw(hw);
	return ret;
}

void ath9k_htc_disconnect_device(struct htc_target *htc_handle, bool hotunplug)
{
	if (htc_handle->drv_priv) {
981 982 983

		/* Check if the device has been yanked out. */
		if (hotunplug)
984
			htc_handle->drv_priv->ah->ah_flags |= AH_UNPLUGGED;
985

S
Sujith 已提交
986 987 988 989 990 991 992
		ath9k_deinit_device(htc_handle->drv_priv);
		ath9k_deinit_wmi(htc_handle->drv_priv);
		ieee80211_free_hw(htc_handle->drv_priv->hw);
	}
}

#ifdef CONFIG_PM
S
Sujith Manoharan 已提交
993 994 995 996 997 998

void ath9k_htc_suspend(struct htc_target *htc_handle)
{
	ath9k_htc_setpower(htc_handle->drv_priv, ATH9K_PM_FULL_SLEEP);
}

S
Sujith 已提交
999 1000
int ath9k_htc_resume(struct htc_target *htc_handle)
{
1001
	struct ath9k_htc_priv *priv = htc_handle->drv_priv;
S
Sujith 已提交
1002 1003
	int ret;

1004
	ret = ath9k_htc_wait_for_target(priv);
S
Sujith 已提交
1005 1006 1007
	if (ret)
		return ret;

1008
	ret = ath9k_init_htc_services(priv, priv->ah->hw_version.devid,
1009
				      priv->ah->hw_version.usbdev);
1010 1011
	ath9k_configure_leds(priv);

S
Sujith 已提交
1012 1013 1014 1015 1016 1017
	return ret;
}
#endif

static int __init ath9k_htc_init(void)
{
1018
	if (ath9k_hif_usb_init() < 0) {
1019
		pr_err("No USB devices found, driver not installed\n");
1020
		return -ENODEV;
S
Sujith 已提交
1021 1022 1023 1024 1025 1026 1027 1028 1029
	}

	return 0;
}
module_init(ath9k_htc_init);

static void __exit ath9k_htc_exit(void)
{
	ath9k_hif_usb_exit();
1030
	pr_info("Driver unloaded\n");
S
Sujith 已提交
1031 1032
}
module_exit(ath9k_htc_exit);