htc_drv_init.c 23.7 KB
Newer Older
S
Sujith 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright (c) 2010 Atheros Communications Inc.
 *
 * 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.
 */

#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");

27 28
int htc_modparam_nohwcrypt;
module_param_named(nohwcrypt, htc_modparam_nohwcrypt, int, 0444);
S
Sujith 已提交
29 30 31 32 33 34 35 36
MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption");

#define CHAN2G(_freq, _idx)  { \
	.center_freq = (_freq), \
	.hw_value = (_idx), \
	.max_power = 20, \
}

S
Sujith 已提交
37 38 39 40 41 42 43
#define CHAN5G(_freq, _idx) { \
	.band = IEEE80211_BAND_5GHZ, \
	.center_freq = (_freq), \
	.hw_value = (_idx), \
	.max_power = 20, \
}

44 45
#define ATH_HTC_BTCOEX_PRODUCT_ID "wb193"

S
Sujith 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
static struct ieee80211_channel ath9k_2ghz_channels[] = {
	CHAN2G(2412, 0), /* Channel 1 */
	CHAN2G(2417, 1), /* Channel 2 */
	CHAN2G(2422, 2), /* Channel 3 */
	CHAN2G(2427, 3), /* Channel 4 */
	CHAN2G(2432, 4), /* Channel 5 */
	CHAN2G(2437, 5), /* Channel 6 */
	CHAN2G(2442, 6), /* Channel 7 */
	CHAN2G(2447, 7), /* Channel 8 */
	CHAN2G(2452, 8), /* Channel 9 */
	CHAN2G(2457, 9), /* Channel 10 */
	CHAN2G(2462, 10), /* Channel 11 */
	CHAN2G(2467, 11), /* Channel 12 */
	CHAN2G(2472, 12), /* Channel 13 */
	CHAN2G(2484, 13), /* Channel 14 */
};

S
Sujith 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
static struct ieee80211_channel ath9k_5ghz_channels[] = {
	/* _We_ call this UNII 1 */
	CHAN5G(5180, 14), /* Channel 36 */
	CHAN5G(5200, 15), /* Channel 40 */
	CHAN5G(5220, 16), /* Channel 44 */
	CHAN5G(5240, 17), /* Channel 48 */
	/* _We_ call this UNII 2 */
	CHAN5G(5260, 18), /* Channel 52 */
	CHAN5G(5280, 19), /* Channel 56 */
	CHAN5G(5300, 20), /* Channel 60 */
	CHAN5G(5320, 21), /* Channel 64 */
	/* _We_ call this "Middle band" */
	CHAN5G(5500, 22), /* Channel 100 */
	CHAN5G(5520, 23), /* Channel 104 */
	CHAN5G(5540, 24), /* Channel 108 */
	CHAN5G(5560, 25), /* Channel 112 */
	CHAN5G(5580, 26), /* Channel 116 */
	CHAN5G(5600, 27), /* Channel 120 */
	CHAN5G(5620, 28), /* Channel 124 */
	CHAN5G(5640, 29), /* Channel 128 */
	CHAN5G(5660, 30), /* Channel 132 */
	CHAN5G(5680, 31), /* Channel 136 */
	CHAN5G(5700, 32), /* Channel 140 */
	/* _We_ call this UNII 3 */
	CHAN5G(5745, 33), /* Channel 149 */
	CHAN5G(5765, 34), /* Channel 153 */
	CHAN5G(5785, 35), /* Channel 157 */
	CHAN5G(5805, 36), /* Channel 161 */
	CHAN5G(5825, 37), /* Channel 165 */
};

S
Sujith 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
/* Atheros hardware rate code addition for short premble */
#define SHPCHECK(__hw_rate, __flags) \
	((__flags & IEEE80211_RATE_SHORT_PREAMBLE) ? (__hw_rate | 0x04) : 0)

#define RATE(_bitrate, _hw_rate, _flags) {		\
	.bitrate	= (_bitrate),			\
	.flags		= (_flags),			\
	.hw_value	= (_hw_rate),			\
	.hw_value_short = (SHPCHECK(_hw_rate, _flags))	\
}

static struct ieee80211_rate ath9k_legacy_rates[] = {
	RATE(10, 0x1b, 0),
	RATE(20, 0x1a, IEEE80211_RATE_SHORT_PREAMBLE), /* shortp : 0x1e */
	RATE(55, 0x19, IEEE80211_RATE_SHORT_PREAMBLE), /* shortp: 0x1d */
	RATE(110, 0x18, IEEE80211_RATE_SHORT_PREAMBLE), /* short: 0x1c */
	RATE(60, 0x0b, 0),
	RATE(90, 0x0f, 0),
	RATE(120, 0x0a, 0),
	RATE(180, 0x0e, 0),
	RATE(240, 0x09, 0),
	RATE(360, 0x0d, 0),
	RATE(480, 0x08, 0),
	RATE(540, 0x0c, 0),
};

static int ath9k_htc_wait_for_target(struct ath9k_htc_priv *priv)
{
	int time_left;

124 125 126 127 128
	if (atomic_read(&priv->htc->tgt_ready) > 0) {
		atomic_dec(&priv->htc->tgt_ready);
		return 0;
	}

S
Sujith 已提交
129 130 131 132 133 134 135
	/* 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;
	}

136 137
	atomic_dec(&priv->htc->tgt_ready);

S
Sujith 已提交
138 139 140 141 142
	return 0;
}

static void ath9k_deinit_priv(struct ath9k_htc_priv *priv)
{
143
	ath9k_htc_exit_debug(priv->ah);
S
Sujith 已提交
144 145 146 147 148 149 150 151 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
	ath9k_hw_deinit(priv->ah);
	tasklet_kill(&priv->wmi_tasklet);
	tasklet_kill(&priv->rx_tasklet);
	tasklet_kill(&priv->tx_tasklet);
	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);
	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);
}

184 185
static int ath9k_init_htc_services(struct ath9k_htc_priv *priv, u16 devid,
				   u32 drv_info)
S
Sujith 已提交
186 187 188 189 190 191 192 193 194
{
	int ret;

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

	/* Beacon */
S
Sujith 已提交
195
	ret = ath9k_htc_connect_svc(priv, WMI_BEACON_SVC, ath9k_htc_beaconep,
S
Sujith 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
				    &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;

243 244 245 246 247 248
	/*
	 * Setup required credits before initializing HTC.
	 * This is a bit hacky, but, since queuing is done in
	 * the HIF layer, shouldn't matter much.
	 */

249
	if (IS_AR7010_DEVICE(drv_info))
250
		priv->htc->credits = 45;
251
	else
252
		priv->htc->credits = 33;
253

S
Sujith 已提交
254 255 256 257
	ret = htc_init(priv->htc);
	if (ret)
		goto err;

258 259 260
	dev_info(priv->dev, "ath9k_htc: HTC initialized with %d credits\n",
		 priv->htc->credits);

S
Sujith 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
	return 0;

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

static int ath9k_reg_notifier(struct wiphy *wiphy,
			      struct regulatory_request *request)
{
	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
	struct ath9k_htc_priv *priv = hw->priv;

	return ath_reg_notifier_apply(wiphy, request,
				      ath9k_hw_regulatory(priv->ah));
}

278
static unsigned int ath9k_regread(void *hw_priv, u32 reg_offset)
S
Sujith 已提交
279 280 281 282 283 284 285 286 287 288 289 290
{
	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)) {
J
Joe Perches 已提交
291 292 293
		ath_dbg(common, ATH_DBG_WMI,
			"REGISTER READ FAILED: (0x%04x, %d)\n",
			reg_offset, r);
S
Sujith 已提交
294 295 296 297 298 299
		return -EIO;
	}

	return be32_to_cpu(val);
}

300
static void ath9k_regwrite_single(void *hw_priv, u32 val, u32 reg_offset)
S
Sujith 已提交
301 302 303 304
{
	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 已提交
305
	const __be32 buf[2] = {
S
Sujith 已提交
306 307 308 309 310 311 312 313 314 315
		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)) {
J
Joe Perches 已提交
316 317 318
		ath_dbg(common, ATH_DBG_WMI,
			"REGISTER WRITE FAILED:(0x%04x, %d)\n",
			reg_offset, r);
S
Sujith 已提交
319 320 321
	}
}

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
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;
	u32 rsp_status;
	int r;

	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. */
	if (priv->wmi->multi_write_idx == MAX_CMD_NUMBER) {
		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)) {
J
Joe Perches 已提交
348 349 350
			ath_dbg(common, ATH_DBG_WMI,
				"REGISTER WRITE FAILED, multi len: %d\n",
				priv->wmi->multi_write_idx);
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
		}
		priv->wmi->multi_write_idx = 0;
	}

	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;
	u32 rsp_status;
	int r;

387 388
	atomic_dec(&priv->wmi->mwrite_cnt);

389 390 391 392 393 394 395 396 397
	mutex_lock(&priv->wmi->multi_write_mutex);

	if (priv->wmi->multi_write_idx) {
		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)) {
J
Joe Perches 已提交
398 399 400
			ath_dbg(common, ATH_DBG_WMI,
				"REGISTER WRITE FAILED, multi len: %d\n",
				priv->wmi->multi_write_idx);
401 402 403 404 405 406 407
		}
		priv->wmi->multi_write_idx = 0;
	}

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

S
Sujith 已提交
408
static const struct ath_ops ath9k_common_ops = {
409 410 411 412
	.read = ath9k_regread,
	.write = ath9k_regwrite,
	.enable_write_buffer = ath9k_enable_regwrite_buffer,
	.write_flush = ath9k_regwrite_flush,
S
Sujith 已提交
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
};

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 已提交
440
	.ath_bus_type = ATH_USB,
S
Sujith 已提交
441 442 443 444 445 446 447
	.read_cachesize = ath_usb_read_cachesize,
	.eeprom_read = ath_usb_eeprom_read,
};

static void setup_ht_cap(struct ath9k_htc_priv *priv,
			 struct ieee80211_sta_ht_cap *ht_info)
{
448 449 450 451
	struct ath_common *common = ath9k_hw_common(priv->ah);
	u8 tx_streams, rx_streams;
	int i;

S
Sujith 已提交
452 453 454 455 456 457
	ht_info->ht_supported = true;
	ht_info->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
		       IEEE80211_HT_CAP_SM_PS |
		       IEEE80211_HT_CAP_SGI_40 |
		       IEEE80211_HT_CAP_DSSSCCK40;

458 459 460
	if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_SGI_20)
		ht_info->cap |= IEEE80211_HT_CAP_SGI_20;

S
Sujith 已提交
461 462
	ht_info->cap |= (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT);

S
Sujith 已提交
463 464 465 466
	ht_info->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
	ht_info->ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;

	memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
467 468 469 470 471

	/* ath9k_htc supports only 1 or 2 stream devices */
	tx_streams = ath9k_cmn_count_streams(common->tx_chainmask, 2);
	rx_streams = ath9k_cmn_count_streams(common->rx_chainmask, 2);

J
Joe Perches 已提交
472 473 474
	ath_dbg(common, ATH_DBG_CONFIG,
		"TX streams %d, RX streams: %d\n",
		tx_streams, rx_streams);
475 476 477 478 479 480 481 482 483 484

	if (tx_streams != rx_streams) {
		ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
		ht_info->mcs.tx_params |= ((tx_streams - 1) <<
					   IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
	}

	for (i = 0; i < rx_streams; i++)
		ht_info->mcs.rx_mask[i] = 0xff;

S
Sujith 已提交
485 486 487 488 489 490 491 492 493 494 495
	ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
}

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;

S
Sujith 已提交
496 497
	priv->beaconq = ath9k_hw_beaconq_setup(priv->ah);
	if (priv->beaconq == -1) {
498
		ath_err(common, "Unable to setup BEACON xmit queue\n");
S
Sujith 已提交
499 500 501 502 503
		goto err;
	}

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

508
	if (!ath9k_htc_txq_setup(priv, WME_AC_BE)) {
509
		ath_err(common, "Unable to setup xmit queue for BE traffic\n");
S
Sujith 已提交
510 511 512
		goto err;
	}

513
	if (!ath9k_htc_txq_setup(priv, WME_AC_BK)) {
514
		ath_err(common, "Unable to setup xmit queue for BK traffic\n");
S
Sujith 已提交
515 516
		goto err;
	}
517
	if (!ath9k_htc_txq_setup(priv, WME_AC_VI)) {
518
		ath_err(common, "Unable to setup xmit queue for VI traffic\n");
S
Sujith 已提交
519 520
		goto err;
	}
521
	if (!ath9k_htc_txq_setup(priv, WME_AC_VO)) {
522
		ath_err(common, "Unable to setup xmit queue for VO traffic\n");
S
Sujith 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
		goto err;
	}

	return 0;

err:
	return -EINVAL;
}

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

	/* Get the hardware key cache size. */
	common->keymax = priv->ah->caps.keycache_size;
	if (common->keymax > ATH_KEYMAX) {
J
Joe Perches 已提交
540 541 542
		ath_dbg(common, ATH_DBG_ANY,
			"Warning, using only %u entries in %u key cache\n",
			ATH_KEYMAX, common->keymax);
S
Sujith 已提交
543 544 545
		common->keymax = ATH_KEYMAX;
	}

546 547 548
	if (priv->ah->misc_mode & AR_PCU_MIC_NEW_LOC_ENA)
		common->crypt_caps |= ATH_CRYPT_CAP_MIC_COMBINED;

S
Sujith 已提交
549 550 551 552 553
	/*
	 * Reset the key cache since some parts do not
	 * reset the contents on initial power up.
	 */
	for (i = 0; i < common->keymax; i++)
554
		ath_hw_keyreset(common, (u16) i);
S
Sujith 已提交
555 556 557 558
}

static void ath9k_init_channels_rates(struct ath9k_htc_priv *priv)
{
559
	if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ) {
S
Sujith 已提交
560 561 562 563 564 565 566 567 568
		priv->sbands[IEEE80211_BAND_2GHZ].channels =
			ath9k_2ghz_channels;
		priv->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
		priv->sbands[IEEE80211_BAND_2GHZ].n_channels =
			ARRAY_SIZE(ath9k_2ghz_channels);
		priv->sbands[IEEE80211_BAND_2GHZ].bitrates = ath9k_legacy_rates;
		priv->sbands[IEEE80211_BAND_2GHZ].n_bitrates =
			ARRAY_SIZE(ath9k_legacy_rates);
	}
S
Sujith 已提交
569

570
	if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ) {
S
Sujith 已提交
571 572 573 574 575 576 577 578 579
		priv->sbands[IEEE80211_BAND_5GHZ].channels = ath9k_5ghz_channels;
		priv->sbands[IEEE80211_BAND_5GHZ].band = IEEE80211_BAND_5GHZ;
		priv->sbands[IEEE80211_BAND_5GHZ].n_channels =
			ARRAY_SIZE(ath9k_5ghz_channels);
		priv->sbands[IEEE80211_BAND_5GHZ].bitrates =
			ath9k_legacy_rates + 4;
		priv->sbands[IEEE80211_BAND_5GHZ].n_bitrates =
			ARRAY_SIZE(ath9k_legacy_rates) - 4;
	}
S
Sujith 已提交
580 581 582 583 584 585 586 587 588
}

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

	common->tx_chainmask = priv->ah->caps.tx_chainmask;
	common->rx_chainmask = priv->ah->caps.rx_chainmask;

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

S
Sujith 已提交
591
	priv->ah->opmode = NL80211_IFTYPE_STATION;
S
Sujith 已提交
592 593
}

594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
static void ath9k_init_btcoex(struct ath9k_htc_priv *priv)
{
	int qnum;

	switch (priv->ah->btcoex_hw.scheme) {
	case ATH_BTCOEX_CFG_NONE:
		break;
	case ATH_BTCOEX_CFG_3WIRE:
		priv->ah->btcoex_hw.btactive_gpio = 7;
		priv->ah->btcoex_hw.btpriority_gpio = 6;
		priv->ah->btcoex_hw.wlanactive_gpio = 8;
		priv->btcoex.bt_stomp_type = ATH_BTCOEX_STOMP_LOW;
		ath9k_hw_btcoex_init_3wire(priv->ah);
		ath_htc_init_btcoex_work(priv);
		qnum = priv->hwq_map[WME_AC_BE];
		ath9k_hw_init_btcoex_hw(priv->ah, qnum);
		break;
	default:
		WARN_ON(1);
		break;
	}
}

static int ath9k_init_priv(struct ath9k_htc_priv *priv,
618 619
			   u16 devid, char *product,
			   u32 drv_info)
S
Sujith 已提交
620 621 622 623 624 625 626 627 628 629 630 631 632
{
	struct ath_hw *ah = NULL;
	struct ath_common *common;
	int ret = 0, csz = 0;

	priv->op_flags |= OP_INVALID;

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

	ah->hw_version.devid = devid;
	ah->hw_version.subsysid = 0; /* FIXME */
633
	ah->hw_version.usbdev = drv_info;
F
Felix Fietkau 已提交
634
	ah->ah_flags |= AH_USE_EEPROM;
S
Sujith 已提交
635 636 637 638 639 640 641 642 643 644 645 646
	priv->ah = ah;

	common = ath9k_hw_common(ah);
	common->ops = &ath9k_common_ops;
	common->bus_ops = &ath9k_usb_bus_ops;
	common->ah = ah;
	common->hw = priv->hw;
	common->priv = priv;
	common->debug_mask = ath9k_debug;

	spin_lock_init(&priv->wmi->wmi_lock);
	spin_lock_init(&priv->beacon_lock);
S
Sujith 已提交
647
	spin_lock_init(&priv->tx_lock);
S
Sujith 已提交
648
	mutex_init(&priv->mutex);
649
	mutex_init(&priv->htc_pm_lock);
S
Sujith 已提交
650 651 652 653 654 655
	tasklet_init(&priv->wmi_tasklet, ath9k_wmi_tasklet,
		     (unsigned long)priv);
	tasklet_init(&priv->rx_tasklet, ath9k_rx_tasklet,
		     (unsigned long)priv);
	tasklet_init(&priv->tx_tasklet, ath9k_tx_tasklet, (unsigned long)priv);
	INIT_DELAYED_WORK(&priv->ath9k_ani_work, ath9k_ani_work);
656
	INIT_WORK(&priv->ps_work, ath9k_ps_work);
S
Sujith 已提交
657 658 659 660 661 662 663 664 665 666

	/*
	 * 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) {
667 668 669
		ath_err(common,
			"Unable to initialize hardware; initialization status: %d\n",
			ret);
S
Sujith 已提交
670 671 672
		goto err_hw;
	}

673
	ret = ath9k_htc_init_debug(ah);
S
Sujith 已提交
674
	if (ret) {
675
		ath_err(common, "Unable to create debugfs files\n");
S
Sujith 已提交
676 677 678 679 680 681 682 683 684 685 686
		goto err_debug;
	}

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

	ath9k_init_crypto(priv);
	ath9k_init_channels_rates(priv);
	ath9k_init_misc(priv);

687 688 689 690 691
	if (product && strncmp(product, ATH_HTC_BTCOEX_PRODUCT_ID, 5) == 0) {
		ah->btcoex_hw.scheme = ATH_BTCOEX_CFG_3WIRE;
		ath9k_init_btcoex(priv);
	}

S
Sujith 已提交
692 693 694
	return 0;

err_queues:
695
	ath9k_htc_exit_debug(ah);
S
Sujith 已提交
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
err_debug:
	ath9k_hw_deinit(ah);
err_hw:

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

	return ret;
}

static void ath9k_set_hw_capab(struct ath9k_htc_priv *priv,
			       struct ieee80211_hw *hw)
{
	struct ath_common *common = ath9k_hw_common(priv->ah);

	hw->flags = IEEE80211_HW_SIGNAL_DBM |
		IEEE80211_HW_AMPDU_AGGREGATION |
		IEEE80211_HW_SPECTRUM_MGMT |
S
Sujith 已提交
714
		IEEE80211_HW_HAS_RATE_CONTROL |
715 716
		IEEE80211_HW_RX_INCLUDES_FCS |
		IEEE80211_HW_SUPPORTS_PS |
717 718
		IEEE80211_HW_PS_NULLFUNC_STACK |
		IEEE80211_HW_NEED_DTIM_PERIOD;
S
Sujith 已提交
719 720 721 722 723

	hw->wiphy->interface_modes =
		BIT(NL80211_IFTYPE_STATION) |
		BIT(NL80211_IFTYPE_ADHOC);

724 725
	hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;

S
Sujith 已提交
726 727 728 729 730 731 732 733 734 735
	hw->queues = 4;
	hw->channel_change_time = 5000;
	hw->max_listen_interval = 10;
	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;

736
	if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ)
S
Sujith 已提交
737 738
		hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
			&priv->sbands[IEEE80211_BAND_2GHZ];
739
	if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ)
S
Sujith 已提交
740 741
		hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
			&priv->sbands[IEEE80211_BAND_5GHZ];
S
Sujith 已提交
742 743

	if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_HT) {
744
		if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ)
S
Sujith 已提交
745 746
			setup_ht_cap(priv,
				     &priv->sbands[IEEE80211_BAND_2GHZ].ht_cap);
747
		if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ)
S
Sujith 已提交
748 749
			setup_ht_cap(priv,
				     &priv->sbands[IEEE80211_BAND_5GHZ].ht_cap);
S
Sujith 已提交
750 751 752 753 754
	}

	SET_IEEE80211_PERM_ADDR(hw, common->macaddr);
}

755
static int ath9k_init_device(struct ath9k_htc_priv *priv,
756
			     u16 devid, char *product, u32 drv_info)
S
Sujith 已提交
757 758 759 760 761 762 763 764
{
	struct ieee80211_hw *hw = priv->hw;
	struct ath_common *common;
	struct ath_hw *ah;
	int error = 0;
	struct ath_regulatory *reg;

	/* Bring up device */
765
	error = ath9k_init_priv(priv, devid, product, drv_info);
S
Sujith 已提交
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
	if (error != 0)
		goto err_init;

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

	/* 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;

	/* 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;
	}

	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:
	ath9k_deinit_priv(priv);
err_init:
	return error;
}

int ath9k_htc_probe_device(struct htc_target *htc_handle, struct device *dev,
823
			   u16 devid, char *product, u32 drv_info)
S
Sujith 已提交
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
{
	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;
	}

850
	ret = ath9k_init_htc_services(priv, devid, drv_info);
S
Sujith 已提交
851 852 853
	if (ret)
		goto err_init;

854 855 856
	/* The device may have been unplugged earlier. */
	priv->op_flags &= ~OP_UNPLUGGED;

857
	ret = ath9k_init_device(priv, devid, product, drv_info);
S
Sujith 已提交
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
	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) {
873 874 875 876 877

		/* Check if the device has been yanked out. */
		if (hotunplug)
			htc_handle->drv_priv->op_flags |= OP_UNPLUGGED;

S
Sujith 已提交
878 879 880 881 882 883 884
		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 已提交
885 886 887 888 889 890

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

S
Sujith 已提交
891 892
int ath9k_htc_resume(struct htc_target *htc_handle)
{
893
	struct ath9k_htc_priv *priv = htc_handle->drv_priv;
S
Sujith 已提交
894 895
	int ret;

896
	ret = ath9k_htc_wait_for_target(priv);
S
Sujith 已提交
897 898 899
	if (ret)
		return ret;

900
	ret = ath9k_init_htc_services(priv, priv->ah->hw_version.devid,
901
				      priv->ah->hw_version.usbdev);
S
Sujith 已提交
902 903 904 905 906 907 908 909
	return ret;
}
#endif

static int __init ath9k_htc_init(void)
{
	int error;

910
	error = ath9k_htc_debug_create_root();
S
Sujith 已提交
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
	if (error < 0) {
		printk(KERN_ERR
			"ath9k_htc: Unable to create debugfs root: %d\n",
			error);
		goto err_dbg;
	}

	error = ath9k_hif_usb_init();
	if (error < 0) {
		printk(KERN_ERR
			"ath9k_htc: No USB devices found,"
			" driver not installed.\n");
		error = -ENODEV;
		goto err_usb;
	}

	return 0;

err_usb:
930
	ath9k_htc_debug_remove_root();
S
Sujith 已提交
931 932 933 934 935 936 937 938
err_dbg:
	return error;
}
module_init(ath9k_htc_init);

static void __exit ath9k_htc_exit(void)
{
	ath9k_hif_usb_exit();
939
	ath9k_htc_debug_remove_root();
S
Sujith 已提交
940 941 942
	printk(KERN_INFO "ath9k_htc: Driver unloaded\n");
}
module_exit(ath9k_htc_exit);