mac80211_if.c 42.8 KB
Newer Older
1 2
/*
 * Copyright (c) 2010 Broadcom Corporation
3
 * Copyright (c) 2013 Hauke Mehrtens <hauke@hauke-m.de>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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.
 */

#define __UNDEF_NO_VERSION__
19
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 21 22 23 24

#include <linux/etherdevice.h>
#include <linux/sched.h>
#include <linux/firmware.h>
#include <linux/interrupt.h>
25
#include <linux/module.h>
26
#include <linux/bcma/bcma.h>
27 28 29 30 31 32 33 34 35 36
#include <net/mac80211.h>
#include <defs.h>
#include "phy/phy_int.h"
#include "d11.h"
#include "channel.h"
#include "scb.h"
#include "pub.h"
#include "ucode_loader.h"
#include "mac80211_if.h"
#include "main.h"
37
#include "debug.h"
P
Piotr Haber 已提交
38
#include "led.h"
39 40

#define N_TX_QUEUES	4 /* #tx queues on mac80211<->driver interface */
41
#define BRCMS_FLUSH_TIMEOUT	500 /* msec */
42 43 44 45 46 47 48

/* Flags we support */
#define MAC_FILTERS (FIF_PROMISC_IN_BSS | \
	FIF_ALLMULTI | \
	FIF_FCSFAIL | \
	FIF_CONTROL | \
	FIF_OTHER_BSS | \
49 50
	FIF_BCN_PRBRESP_PROMISC | \
	FIF_PSPOLL)
51 52 53 54 55 56 57 58 59 60 61 62 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

#define CHAN2GHZ(channel, freqency, chflags)  { \
	.band = IEEE80211_BAND_2GHZ, \
	.center_freq = (freqency), \
	.hw_value = (channel), \
	.flags = chflags, \
	.max_antenna_gain = 0, \
	.max_power = 19, \
}

#define CHAN5GHZ(channel, chflags)  { \
	.band = IEEE80211_BAND_5GHZ, \
	.center_freq = 5000 + 5*(channel), \
	.hw_value = (channel), \
	.flags = chflags, \
	.max_antenna_gain = 0, \
	.max_power = 21, \
}

#define RATE(rate100m, _flags) { \
	.bitrate = (rate100m), \
	.flags = (_flags), \
	.hw_value = (rate100m / 5), \
}

struct firmware_hdr {
	__le32 offset;
	__le32 len;
	__le32 idx;
};

static const char * const brcms_firmwares[MAX_FW_IMAGES] = {
	"brcm/bcm43xx",
	NULL
};

static int n_adapters_found;

MODULE_AUTHOR("Broadcom Corporation");
MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN driver.");
MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN cards");
MODULE_LICENSE("Dual BSD/GPL");
93 94 95
/* This needs to be adjusted when brcms_firmwares changes */
MODULE_FIRMWARE("brcm/bcm43xx-0.fw");
MODULE_FIRMWARE("brcm/bcm43xx_hdr-0.fw");
96

97 98
/* recognized BCMA Core IDs */
static struct bcma_device_id brcms_coreid_table[] = {
99
	BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 17, BCMA_ANY_CLASS),
100 101
	BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 23, BCMA_ANY_CLASS),
	BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 24, BCMA_ANY_CLASS),
102 103 104
	BCMA_CORETABLE_END
};
MODULE_DEVICE_TABLE(bcma, brcms_coreid_table);
105

106 107 108 109 110 111 112 113
#if defined(CONFIG_BRCMDBG)
/*
 * Module parameter for setting the debug message level. Available
 * flags are specified by the BRCM_DL_* macros in
 * drivers/net/wireless/brcm80211/include/defs.h.
 */
module_param_named(debug, brcm_msg_level, uint, S_IRUGO | S_IWUSR);
#endif
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

static struct ieee80211_channel brcms_2ghz_chantable[] = {
	CHAN2GHZ(1, 2412, IEEE80211_CHAN_NO_HT40MINUS),
	CHAN2GHZ(2, 2417, IEEE80211_CHAN_NO_HT40MINUS),
	CHAN2GHZ(3, 2422, IEEE80211_CHAN_NO_HT40MINUS),
	CHAN2GHZ(4, 2427, IEEE80211_CHAN_NO_HT40MINUS),
	CHAN2GHZ(5, 2432, 0),
	CHAN2GHZ(6, 2437, 0),
	CHAN2GHZ(7, 2442, 0),
	CHAN2GHZ(8, 2447, IEEE80211_CHAN_NO_HT40PLUS),
	CHAN2GHZ(9, 2452, IEEE80211_CHAN_NO_HT40PLUS),
	CHAN2GHZ(10, 2457, IEEE80211_CHAN_NO_HT40PLUS),
	CHAN2GHZ(11, 2462, IEEE80211_CHAN_NO_HT40PLUS),
	CHAN2GHZ(12, 2467,
		 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_IBSS |
		 IEEE80211_CHAN_NO_HT40PLUS),
	CHAN2GHZ(13, 2472,
		 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_IBSS |
		 IEEE80211_CHAN_NO_HT40PLUS),
	CHAN2GHZ(14, 2484,
		 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_IBSS |
135 136
		 IEEE80211_CHAN_NO_HT40PLUS | IEEE80211_CHAN_NO_HT40MINUS |
		 IEEE80211_CHAN_NO_OFDM)
137 138 139 140 141 142 143 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 184 185 186 187 188 189 190 191 192 193 194 195 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
};

static struct ieee80211_channel brcms_5ghz_nphy_chantable[] = {
	/* UNII-1 */
	CHAN5GHZ(36, IEEE80211_CHAN_NO_HT40MINUS),
	CHAN5GHZ(40, IEEE80211_CHAN_NO_HT40PLUS),
	CHAN5GHZ(44, IEEE80211_CHAN_NO_HT40MINUS),
	CHAN5GHZ(48, IEEE80211_CHAN_NO_HT40PLUS),
	/* UNII-2 */
	CHAN5GHZ(52,
		 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
		 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
	CHAN5GHZ(56,
		 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
		 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
	CHAN5GHZ(60,
		 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
		 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
	CHAN5GHZ(64,
		 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
		 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
	/* MID */
	CHAN5GHZ(100,
		 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
		 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
	CHAN5GHZ(104,
		 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
		 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
	CHAN5GHZ(108,
		 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
		 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
	CHAN5GHZ(112,
		 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
		 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
	CHAN5GHZ(116,
		 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
		 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
	CHAN5GHZ(120,
		 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
		 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
	CHAN5GHZ(124,
		 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
		 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
	CHAN5GHZ(128,
		 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
		 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
	CHAN5GHZ(132,
		 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
		 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
	CHAN5GHZ(136,
		 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
		 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
	CHAN5GHZ(140,
		 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
		 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS |
		 IEEE80211_CHAN_NO_HT40MINUS),
	/* UNII-3 */
	CHAN5GHZ(149, IEEE80211_CHAN_NO_HT40MINUS),
	CHAN5GHZ(153, IEEE80211_CHAN_NO_HT40PLUS),
	CHAN5GHZ(157, IEEE80211_CHAN_NO_HT40MINUS),
	CHAN5GHZ(161, IEEE80211_CHAN_NO_HT40PLUS),
	CHAN5GHZ(165, IEEE80211_CHAN_NO_HT40PLUS | IEEE80211_CHAN_NO_HT40MINUS)
};

/*
 * The rate table is used for both 2.4G and 5G rates. The
 * latter being a subset as it does not support CCK rates.
 */
static struct ieee80211_rate legacy_ratetable[] = {
	RATE(10, 0),
	RATE(20, IEEE80211_RATE_SHORT_PREAMBLE),
	RATE(55, IEEE80211_RATE_SHORT_PREAMBLE),
	RATE(110, IEEE80211_RATE_SHORT_PREAMBLE),
	RATE(60, 0),
	RATE(90, 0),
	RATE(120, 0),
	RATE(180, 0),
	RATE(240, 0),
	RATE(360, 0),
	RATE(480, 0),
	RATE(540, 0),
};

static const struct ieee80211_supported_band brcms_band_2GHz_nphy_template = {
	.band = IEEE80211_BAND_2GHZ,
	.channels = brcms_2ghz_chantable,
	.n_channels = ARRAY_SIZE(brcms_2ghz_chantable),
	.bitrates = legacy_ratetable,
	.n_bitrates = ARRAY_SIZE(legacy_ratetable),
	.ht_cap = {
		   /* from include/linux/ieee80211.h */
		   .cap = IEEE80211_HT_CAP_GRN_FLD |
229
			  IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40,
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
		   .ht_supported = true,
		   .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K,
		   .ampdu_density = AMPDU_DEF_MPDU_DENSITY,
		   .mcs = {
			   /* placeholders for now */
			   .rx_mask = {0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0},
			   .rx_highest = cpu_to_le16(500),
			   .tx_params = IEEE80211_HT_MCS_TX_DEFINED}
		   }
};

static const struct ieee80211_supported_band brcms_band_5GHz_nphy_template = {
	.band = IEEE80211_BAND_5GHZ,
	.channels = brcms_5ghz_nphy_chantable,
	.n_channels = ARRAY_SIZE(brcms_5ghz_nphy_chantable),
	.bitrates = legacy_ratetable + BRCMS_LEGACY_5G_RATE_OFFSET,
	.n_bitrates = ARRAY_SIZE(legacy_ratetable) -
			BRCMS_LEGACY_5G_RATE_OFFSET,
	.ht_cap = {
		   .cap = IEEE80211_HT_CAP_GRN_FLD | IEEE80211_HT_CAP_SGI_20 |
250
			  IEEE80211_HT_CAP_SGI_40,
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
		   .ht_supported = true,
		   .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K,
		   .ampdu_density = AMPDU_DEF_MPDU_DENSITY,
		   .mcs = {
			   /* placeholders for now */
			   .rx_mask = {0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0},
			   .rx_highest = cpu_to_le16(500),
			   .tx_params = IEEE80211_HT_MCS_TX_DEFINED}
		   }
};

/* flags the given rate in rateset as requested */
static void brcms_set_basic_rate(struct brcm_rateset *rs, u16 rate, bool is_br)
{
	u32 i;

	for (i = 0; i < rs->count; i++) {
		if (rate != (rs->rates[i] & 0x7f))
			continue;

		if (is_br)
			rs->rates[i] |= BRCMS_RATE_FLAG;
		else
			rs->rates[i] &= BRCMS_RATE_MASK;
		return;
	}
}

279 280 281
static void brcms_ops_tx(struct ieee80211_hw *hw,
			 struct ieee80211_tx_control *control,
			 struct sk_buff *skb)
282 283
{
	struct brcms_info *wl = hw->priv;
284
	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
285 286 287

	spin_lock_bh(&wl->lock);
	if (!wl->pub->up) {
288
		brcms_err(wl->wlc->hw->d11core, "ops->tx called while down\n");
289 290 291
		kfree_skb(skb);
		goto done;
	}
292 293
	if (brcms_c_sendpkt_mac80211(wl->wlc, skb, hw))
		tx_info->rate_driver_data[0] = control->sta;
294 295 296 297 298 299 300 301
 done:
	spin_unlock_bh(&wl->lock);
}

static int brcms_ops_start(struct ieee80211_hw *hw)
{
	struct brcms_info *wl = hw->priv;
	bool blocked;
302
	int err;
303 304 305 306 307 308 309 310

	ieee80211_wake_queues(hw);
	spin_lock_bh(&wl->lock);
	blocked = brcms_rfkill_set_hw_state(wl);
	spin_unlock_bh(&wl->lock);
	if (!blocked)
		wiphy_rfkill_stop_polling(wl->pub->ieee_hw->wiphy);

311
	spin_lock_bh(&wl->lock);
312 313 314
	/* avoid acknowledging frames before a non-monitor device is added */
	wl->mute_tx = true;

315
	if (!wl->pub->up)
316 317 318 319
		if (!blocked)
			err = brcms_up(wl);
		else
			err = -ERFKILL;
320 321 322 323 324
	else
		err = -ENODEV;
	spin_unlock_bh(&wl->lock);

	if (err != 0)
325 326
		brcms_err(wl->wlc->hw->d11core, "%s: brcms_up() returned %d\n",
			  __func__, err);
327
	return err;
328 329 330 331
}

static void brcms_ops_stop(struct ieee80211_hw *hw)
{
332 333 334
	struct brcms_info *wl = hw->priv;
	int status;

335
	ieee80211_stop_queues(hw);
336 337 338 339 340

	if (wl->wlc == NULL)
		return;

	spin_lock_bh(&wl->lock);
341
	status = brcms_c_chipmatch(wl->wlc->hw->d11core);
342 343
	spin_unlock_bh(&wl->lock);
	if (!status) {
344
		brcms_err(wl->wlc->hw->d11core,
345 346 347 348 349 350 351 352
			  "wl: brcms_ops_stop: chipmatch failed\n");
		return;
	}

	/* put driver in down state */
	spin_lock_bh(&wl->lock);
	brcms_down(wl);
	spin_unlock_bh(&wl->lock);
353 354 355 356 357
}

static int
brcms_ops_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
{
358 359
	struct brcms_info *wl = hw->priv;

360
	/* Just STA for now */
361
	if (vif->type != NL80211_IFTYPE_STATION) {
362 363 364
		brcms_err(wl->wlc->hw->d11core,
			  "%s: Attempt to add type %d, only STA for now\n",
			  __func__, vif->type);
365 366 367
		return -EOPNOTSUPP;
	}

368
	spin_lock_bh(&wl->lock);
369 370
	wl->mute_tx = false;
	brcms_c_mute(wl->wlc, false);
371 372
	if (vif->type == NL80211_IFTYPE_STATION)
		brcms_c_start_station(wl->wlc, vif->addr);
373
	spin_unlock_bh(&wl->lock);
374

375
	return 0;
376 377 378 379 380 381 382 383 384 385 386
}

static void
brcms_ops_remove_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
{
}

static int brcms_ops_config(struct ieee80211_hw *hw, u32 changed)
{
	struct ieee80211_conf *conf = &hw->conf;
	struct brcms_info *wl = hw->priv;
387
	struct bcma_device *core = wl->wlc->hw->d11core;
388 389 390 391 392 393 394 395 396
	int err = 0;
	int new_int;

	spin_lock_bh(&wl->lock);
	if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
		brcms_c_set_beacon_listen_interval(wl->wlc,
						   conf->listen_interval);
	}
	if (changed & IEEE80211_CONF_CHANGE_MONITOR)
397 398 399
		brcms_dbg_info(core, "%s: change monitor mode: %s\n",
			       __func__, conf->flags & IEEE80211_CONF_MONITOR ?
			       "true" : "false");
400
	if (changed & IEEE80211_CONF_CHANGE_PS)
401
		brcms_err(core, "%s: change power-save mode: %s (implement)\n",
402 403 404 405 406 407
			  __func__, conf->flags & IEEE80211_CONF_PS ?
			  "true" : "false");

	if (changed & IEEE80211_CONF_CHANGE_POWER) {
		err = brcms_c_set_tx_power(wl->wlc, conf->power_level);
		if (err < 0) {
408
			brcms_err(core, "%s: Error setting power_level\n",
409 410 411 412 413
				  __func__);
			goto config_out;
		}
		new_int = brcms_c_get_tx_power(wl->wlc);
		if (new_int != conf->power_level)
414 415 416
			brcms_err(core,
				  "%s: Power level req != actual, %d %d\n",
				  __func__, conf->power_level,
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
				  new_int);
	}
	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
		if (conf->channel_type == NL80211_CHAN_HT20 ||
		    conf->channel_type == NL80211_CHAN_NO_HT)
			err = brcms_c_set_channel(wl->wlc,
						  conf->channel->hw_value);
		else
			err = -ENOTSUPP;
	}
	if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
		err = brcms_c_set_rate_limit(wl->wlc,
					     conf->short_frame_max_tx_count,
					     conf->long_frame_max_tx_count);

 config_out:
	spin_unlock_bh(&wl->lock);
	return err;
}

static void
brcms_ops_bss_info_changed(struct ieee80211_hw *hw,
			struct ieee80211_vif *vif,
			struct ieee80211_bss_conf *info, u32 changed)
{
	struct brcms_info *wl = hw->priv;
443
	struct bcma_device *core = wl->wlc->hw->d11core;
444 445 446 447 448

	if (changed & BSS_CHANGED_ASSOC) {
		/* association status changed (associated/disassociated)
		 * also implies a change in the AID.
		 */
449
		brcms_err(core, "%s: %s: %sassociated\n", KBUILD_MODNAME,
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 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
			  __func__, info->assoc ? "" : "dis");
		spin_lock_bh(&wl->lock);
		brcms_c_associate_upd(wl->wlc, info->assoc);
		spin_unlock_bh(&wl->lock);
	}
	if (changed & BSS_CHANGED_ERP_SLOT) {
		s8 val;

		/* slot timing changed */
		if (info->use_short_slot)
			val = 1;
		else
			val = 0;
		spin_lock_bh(&wl->lock);
		brcms_c_set_shortslot_override(wl->wlc, val);
		spin_unlock_bh(&wl->lock);
	}

	if (changed & BSS_CHANGED_HT) {
		/* 802.11n parameters changed */
		u16 mode = info->ht_operation_mode;

		spin_lock_bh(&wl->lock);
		brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_CFG,
			mode & IEEE80211_HT_OP_MODE_PROTECTION);
		brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_NONGF,
			mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
		brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_OBSS,
			mode & IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT);
		spin_unlock_bh(&wl->lock);
	}
	if (changed & BSS_CHANGED_BASIC_RATES) {
		struct ieee80211_supported_band *bi;
		u32 br_mask, i;
		u16 rate;
		struct brcm_rateset rs;
		int error;

		/* retrieve the current rates */
		spin_lock_bh(&wl->lock);
		brcms_c_get_current_rateset(wl->wlc, &rs);
		spin_unlock_bh(&wl->lock);

		br_mask = info->basic_rates;
		bi = hw->wiphy->bands[brcms_c_get_curband(wl->wlc)];
		for (i = 0; i < bi->n_bitrates; i++) {
			/* convert to internal rate value */
			rate = (bi->bitrates[i].bitrate << 1) / 10;

			/* set/clear basic rate flag */
			brcms_set_basic_rate(&rs, rate, br_mask & 1);
			br_mask >>= 1;
		}

		/* update the rate set */
		spin_lock_bh(&wl->lock);
		error = brcms_c_set_rateset(wl->wlc, &rs);
		spin_unlock_bh(&wl->lock);
		if (error)
509
			brcms_err(core, "changing basic rates failed: %d\n",
510 511 512 513 514 515 516 517 518 519 520 521 522 523
				  error);
	}
	if (changed & BSS_CHANGED_BEACON_INT) {
		/* Beacon interval changed */
		spin_lock_bh(&wl->lock);
		brcms_c_set_beacon_period(wl->wlc, info->beacon_int);
		spin_unlock_bh(&wl->lock);
	}
	if (changed & BSS_CHANGED_BSSID) {
		/* BSSID changed, for whatever reason (IBSS and managed mode) */
		spin_lock_bh(&wl->lock);
		brcms_c_set_addrmatch(wl->wlc, RCM_BSSID_OFFSET, info->bssid);
		spin_unlock_bh(&wl->lock);
	}
524 525 526 527 528 529
	if (changed & BSS_CHANGED_SSID) {
		/* BSSID changed, for whatever reason (IBSS and managed mode) */
		spin_lock_bh(&wl->lock);
		brcms_c_set_ssid(wl->wlc, info->ssid, info->ssid_len);
		spin_unlock_bh(&wl->lock);
	}
530
	if (changed & BSS_CHANGED_BEACON) {
531
		/* Beacon data changed, retrieve new beacon (beaconing modes) */
532 533 534 535 536 537 538 539 540
		struct sk_buff *beacon;
		u16 tim_offset = 0;

		spin_lock_bh(&wl->lock);
		beacon = ieee80211_beacon_get_tim(hw, vif, &tim_offset, NULL);
		brcms_c_set_new_beacon(wl->wlc, beacon, tim_offset,
				       info->dtim_period);
		spin_unlock_bh(&wl->lock);
	}
541

542 543 544 545 546 547 548 549 550
	if (changed & BSS_CHANGED_AP_PROBE_RESP) {
		struct sk_buff *probe_resp;

		spin_lock_bh(&wl->lock);
		probe_resp = ieee80211_proberesp_get(hw, vif);
		brcms_c_set_new_probe_resp(wl->wlc, probe_resp);
		spin_unlock_bh(&wl->lock);
	}

551 552
	if (changed & BSS_CHANGED_BEACON_ENABLED) {
		/* Beaconing should be enabled/disabled (beaconing modes) */
553
		brcms_err(core, "%s: Beacon enabled: %s\n", __func__,
554
			  info->enable_beacon ? "true" : "false");
555 556 557 558 559 560
		if (info->enable_beacon &&
		    hw->wiphy->flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) {
			brcms_c_enable_probe_resp(wl->wlc, true);
		} else {
			brcms_c_enable_probe_resp(wl->wlc, false);
		}
561 562 563 564
	}

	if (changed & BSS_CHANGED_CQM) {
		/* Connection quality monitor config changed */
565
		brcms_err(core, "%s: cqm change: threshold %d, hys %d "
566 567 568 569 570 571
			  " (implement)\n", __func__, info->cqm_rssi_thold,
			  info->cqm_rssi_hyst);
	}

	if (changed & BSS_CHANGED_IBSS) {
		/* IBSS join status changed */
572 573
		brcms_err(core, "%s: IBSS joined: %s (implement)\n",
			  __func__, info->ibss_joined ? "true" : "false");
574 575 576 577
	}

	if (changed & BSS_CHANGED_ARP_FILTER) {
		/* Hardware ARP filter address list or state changed */
578 579
		brcms_err(core, "%s: arp filtering: %d addresses"
			  " (implement)\n", __func__, info->arp_addr_cnt);
580 581 582 583 584 585 586
	}

	if (changed & BSS_CHANGED_QOS) {
		/*
		 * QoS for this association was enabled/disabled.
		 * Note that it is only ever disabled for station mode.
		 */
587 588
		brcms_err(core, "%s: qos enabled: %s (implement)\n",
			  __func__, info->qos ? "true" : "false");
589 590 591 592 593 594 595 596 597 598
	}
	return;
}

static void
brcms_ops_configure_filter(struct ieee80211_hw *hw,
			unsigned int changed_flags,
			unsigned int *total_flags, u64 multicast)
{
	struct brcms_info *wl = hw->priv;
599
	struct bcma_device *core = wl->wlc->hw->d11core;
600 601 602

	changed_flags &= MAC_FILTERS;
	*total_flags &= MAC_FILTERS;
603

604
	if (changed_flags & FIF_PROMISC_IN_BSS)
605
		brcms_dbg_info(core, "FIF_PROMISC_IN_BSS\n");
606
	if (changed_flags & FIF_ALLMULTI)
607
		brcms_dbg_info(core, "FIF_ALLMULTI\n");
608
	if (changed_flags & FIF_FCSFAIL)
609
		brcms_dbg_info(core, "FIF_FCSFAIL\n");
610
	if (changed_flags & FIF_CONTROL)
611
		brcms_dbg_info(core, "FIF_CONTROL\n");
612
	if (changed_flags & FIF_OTHER_BSS)
613
		brcms_dbg_info(core, "FIF_OTHER_BSS\n");
614
	if (changed_flags & FIF_PSPOLL)
615
		brcms_dbg_info(core, "FIF_PSPOLL\n");
616
	if (changed_flags & FIF_BCN_PRBRESP_PROMISC)
617
		brcms_dbg_info(core, "FIF_BCN_PRBRESP_PROMISC\n");
618 619 620 621

	spin_lock_bh(&wl->lock);
	brcms_c_mac_promisc(wl->wlc, *total_flags);
	spin_unlock_bh(&wl->lock);
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
	return;
}

static void brcms_ops_sw_scan_start(struct ieee80211_hw *hw)
{
	struct brcms_info *wl = hw->priv;
	spin_lock_bh(&wl->lock);
	brcms_c_scan_start(wl->wlc);
	spin_unlock_bh(&wl->lock);
	return;
}

static void brcms_ops_sw_scan_complete(struct ieee80211_hw *hw)
{
	struct brcms_info *wl = hw->priv;
	spin_lock_bh(&wl->lock);
	brcms_c_scan_stop(wl->wlc);
	spin_unlock_bh(&wl->lock);
	return;
}

static int
brcms_ops_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
		  const struct ieee80211_tx_queue_params *params)
{
	struct brcms_info *wl = hw->priv;

	spin_lock_bh(&wl->lock);
	brcms_c_wme_setparams(wl->wlc, queue, params, true);
	spin_unlock_bh(&wl->lock);

	return 0;
}

static int
brcms_ops_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
	       struct ieee80211_sta *sta)
{
	struct brcms_info *wl = hw->priv;
	struct scb *scb = &wl->wlc->pri_scb;

	brcms_c_init_scb(scb);

	wl->pub->global_ampdu = &(scb->scb_ampdu);
	wl->pub->global_ampdu->scb = scb;
	wl->pub->global_ampdu->max_pdu = 16;

	/*
	 * minstrel_ht initiates addBA on our behalf by calling
	 * ieee80211_start_tx_ba_session()
	 */
	return 0;
}

static int
brcms_ops_ampdu_action(struct ieee80211_hw *hw,
		    struct ieee80211_vif *vif,
		    enum ieee80211_ampdu_mlme_action action,
		    struct ieee80211_sta *sta, u16 tid, u16 *ssn,
		    u8 buf_size)
{
	struct brcms_info *wl = hw->priv;
	struct scb *scb = &wl->wlc->pri_scb;
	int status;

	if (WARN_ON(scb->magic != SCB_MAGIC))
		return -EIDRM;
	switch (action) {
	case IEEE80211_AMPDU_RX_START:
		break;
	case IEEE80211_AMPDU_RX_STOP:
		break;
	case IEEE80211_AMPDU_TX_START:
		spin_lock_bh(&wl->lock);
		status = brcms_c_aggregatable(wl->wlc, tid);
		spin_unlock_bh(&wl->lock);
		if (!status) {
699 700
			brcms_err(wl->wlc->hw->d11core,
				  "START: tid %d is not agg\'able\n", tid);
701 702 703 704 705
			return -EINVAL;
		}
		ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
		break;

706 707 708
	case IEEE80211_AMPDU_TX_STOP_CONT:
	case IEEE80211_AMPDU_TX_STOP_FLUSH:
	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
		spin_lock_bh(&wl->lock);
		brcms_c_ampdu_flush(wl->wlc, sta, tid);
		spin_unlock_bh(&wl->lock);
		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
		break;
	case IEEE80211_AMPDU_TX_OPERATIONAL:
		/*
		 * BA window size from ADDBA response ('buf_size') defines how
		 * many outstanding MPDUs are allowed for the BA stream by
		 * recipient and traffic class. 'ampdu_factor' gives maximum
		 * AMPDU size.
		 */
		spin_lock_bh(&wl->lock);
		brcms_c_ampdu_tx_operational(wl->wlc, tid, buf_size,
			(1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
			 sta->ht_cap.ampdu_factor)) - 1);
		spin_unlock_bh(&wl->lock);
		/* Power save wakeup */
		break;
	default:
729 730
		brcms_err(wl->wlc->hw->d11core,
			  "%s: Invalid command, ignoring\n", __func__);
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
	}

	return 0;
}

static void brcms_ops_rfkill_poll(struct ieee80211_hw *hw)
{
	struct brcms_info *wl = hw->priv;
	bool blocked;

	spin_lock_bh(&wl->lock);
	blocked = brcms_c_check_radio_disabled(wl->wlc);
	spin_unlock_bh(&wl->lock);

	wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, blocked);
}

748 749 750 751 752 753 754 755 756 757
static bool brcms_tx_flush_completed(struct brcms_info *wl)
{
	bool result;

	spin_lock_bh(&wl->lock);
	result = brcms_c_tx_flush_completed(wl->wlc);
	spin_unlock_bh(&wl->lock);
	return result;
}

758
static void brcms_ops_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
759 760
{
	struct brcms_info *wl = hw->priv;
761
	int ret;
762 763 764

	no_printk("%s: drop = %s\n", __func__, drop ? "true" : "false");

765 766 767 768 769 770
	ret = wait_event_timeout(wl->tx_flush_wq,
				 brcms_tx_flush_completed(wl),
				 msecs_to_jiffies(BRCMS_FLUSH_TIMEOUT));

	brcms_dbg_mac80211(wl->wlc->hw->d11core,
			   "ret=%d\n", jiffies_to_msecs(ret));
771 772
}

773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
static u64 brcms_ops_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
{
	struct brcms_info *wl = hw->priv;
	u64 tsf;

	spin_lock_bh(&wl->lock);
	tsf = brcms_c_tsf_get(wl->wlc);
	spin_unlock_bh(&wl->lock);

	return tsf;
}

static void brcms_ops_set_tsf(struct ieee80211_hw *hw,
			   struct ieee80211_vif *vif, u64 tsf)
{
	struct brcms_info *wl = hw->priv;

	spin_lock_bh(&wl->lock);
	brcms_c_tsf_set(wl->wlc, tsf);
	spin_unlock_bh(&wl->lock);
}

795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
static const struct ieee80211_ops brcms_ops = {
	.tx = brcms_ops_tx,
	.start = brcms_ops_start,
	.stop = brcms_ops_stop,
	.add_interface = brcms_ops_add_interface,
	.remove_interface = brcms_ops_remove_interface,
	.config = brcms_ops_config,
	.bss_info_changed = brcms_ops_bss_info_changed,
	.configure_filter = brcms_ops_configure_filter,
	.sw_scan_start = brcms_ops_sw_scan_start,
	.sw_scan_complete = brcms_ops_sw_scan_complete,
	.conf_tx = brcms_ops_conf_tx,
	.sta_add = brcms_ops_sta_add,
	.ampdu_action = brcms_ops_ampdu_action,
	.rfkill_poll = brcms_ops_rfkill_poll,
	.flush = brcms_ops_flush,
811 812
	.get_tsf = brcms_ops_get_tsf,
	.set_tsf = brcms_ops_set_tsf,
813 814 815 816 817 818 819 820 821 822 823 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
};

void brcms_dpc(unsigned long data)
{
	struct brcms_info *wl;

	wl = (struct brcms_info *) data;

	spin_lock_bh(&wl->lock);

	/* call the common second level interrupt handler */
	if (wl->pub->up) {
		if (wl->resched) {
			unsigned long flags;

			spin_lock_irqsave(&wl->isr_lock, flags);
			brcms_c_intrsupd(wl->wlc);
			spin_unlock_irqrestore(&wl->isr_lock, flags);
		}

		wl->resched = brcms_c_dpc(wl->wlc, true);
	}

	/* brcms_c_dpc() may bring the driver down */
	if (!wl->pub->up)
		goto done;

	/* re-schedule dpc */
	if (wl->resched)
		tasklet_schedule(&wl->tasklet);
	else
		/* re-enable interrupts */
		brcms_intrson(wl);

 done:
	spin_unlock_bh(&wl->lock);
849
	wake_up(&wl->tx_flush_wq);
850 851 852 853 854 855
}

/*
 * Precondition: Since this function is called in brcms_pci_probe() context,
 * no locking is required.
 */
856
static int brcms_request_fw(struct brcms_info *wl, struct bcma_device *pdev)
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
{
	int status;
	struct device *device = &pdev->dev;
	char fw_name[100];
	int i;

	memset(&wl->fw, 0, sizeof(struct brcms_firmware));
	for (i = 0; i < MAX_FW_IMAGES; i++) {
		if (brcms_firmwares[i] == NULL)
			break;
		sprintf(fw_name, "%s-%d.fw", brcms_firmwares[i],
			UCODE_LOADER_API_VER);
		status = request_firmware(&wl->fw.fw_bin[i], fw_name, device);
		if (status) {
			wiphy_err(wl->wiphy, "%s: fail to load firmware %s\n",
				  KBUILD_MODNAME, fw_name);
			return status;
		}
		sprintf(fw_name, "%s_hdr-%d.fw", brcms_firmwares[i],
			UCODE_LOADER_API_VER);
		status = request_firmware(&wl->fw.fw_hdr[i], fw_name, device);
		if (status) {
			wiphy_err(wl->wiphy, "%s: fail to load firmware %s\n",
				  KBUILD_MODNAME, fw_name);
			return status;
		}
		wl->fw.hdr_num_entries[i] =
		    wl->fw.fw_hdr[i]->size / (sizeof(struct firmware_hdr));
	}
	wl->fw.fw_cnt = i;
	return brcms_ucode_data_init(wl, &wl->ucode);
}

/*
 * Precondition: Since this function is called in brcms_pci_probe() context,
 * no locking is required.
 */
static void brcms_release_fw(struct brcms_info *wl)
{
	int i;
	for (i = 0; i < MAX_FW_IMAGES; i++) {
		release_firmware(wl->fw.fw_bin[i]);
		release_firmware(wl->fw.fw_hdr[i]);
	}
}

/**
 * This function frees the WL per-device resources.
 *
 * This function frees resources owned by the WL device pointed to
 * by the wl parameter.
 *
 * precondition: can both be called locked and unlocked
 *
 */
static void brcms_free(struct brcms_info *wl)
{
	struct brcms_timer *t, *next;

	/* free ucode data */
	if (wl->fw.fw_cnt)
		brcms_ucode_data_free(&wl->ucode);
	if (wl->irq)
		free_irq(wl->irq, wl);

	/* kill dpc */
	tasklet_kill(&wl->tasklet);

P
Piotr Haber 已提交
925 926
	if (wl->pub) {
		brcms_debugfs_detach(wl->pub);
927
		brcms_c_module_unregister(wl->pub, "linux", wl);
P
Piotr Haber 已提交
928
	}
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945

	/* free common resources */
	if (wl->wlc) {
		brcms_c_detach(wl->wlc);
		wl->wlc = NULL;
		wl->pub = NULL;
	}

	/* virtual interface deletion is deferred so we cannot spinwait */

	/* wait for all pending callbacks to complete */
	while (atomic_read(&wl->callbacks) > 0)
		schedule();

	/* free timers */
	for (t = wl->timers; t; t = next) {
		next = t->next;
J
Joe Perches 已提交
946
#ifdef DEBUG
947 948 949 950 951 952 953
		kfree(t->name);
#endif
		kfree(t);
	}
}

/*
954
* called from both kernel as from this kernel module (error flow on attach)
955 956
* precondition: perimeter lock is not acquired.
*/
957
static void brcms_remove(struct bcma_device *pdev)
958
{
959
	struct ieee80211_hw *hw = bcma_get_drvdata(pdev);
960
	struct brcms_info *wl = hw->priv;
961 962

	if (wl->wlc) {
P
Piotr Haber 已提交
963
		brcms_led_unregister(wl);
964 965 966 967 968 969 970
		wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, false);
		wiphy_rfkill_stop_polling(wl->pub->ieee_hw->wiphy);
		ieee80211_unregister_hw(hw);
	}

	brcms_free(wl);

971
	bcma_set_drvdata(pdev, NULL);
972 973 974 975 976 977
	ieee80211_free_hw(hw);
}

static irqreturn_t brcms_isr(int irq, void *dev_id)
{
	struct brcms_info *wl;
P
Piotr Haber 已提交
978
	irqreturn_t ret = IRQ_NONE;
979 980 981 982 983 984

	wl = (struct brcms_info *) dev_id;

	spin_lock(&wl->isr_lock);

	/* call common first level interrupt handler */
P
Piotr Haber 已提交
985 986 987 988
	if (brcms_c_isr(wl->wlc)) {
		/* schedule second level handler */
		tasklet_schedule(&wl->tasklet);
		ret = IRQ_HANDLED;
989 990 991 992
	}

	spin_unlock(&wl->isr_lock);

P
Piotr Haber 已提交
993
	return ret;
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
}

/*
 * is called in brcms_pci_probe() context, therefore no locking required.
 */
static int ieee_hw_rate_init(struct ieee80211_hw *hw)
{
	struct brcms_info *wl = hw->priv;
	struct brcms_c_info *wlc = wl->wlc;
	struct ieee80211_supported_band *band;
	int has_5g = 0;
	u16 phy_type;

	hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
	hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;

	phy_type = brcms_c_get_phy_type(wl->wlc, 0);
	if (phy_type == PHY_TYPE_N || phy_type == PHY_TYPE_LCN) {
		band = &wlc->bandstate[BAND_2G_INDEX]->band;
		*band = brcms_band_2GHz_nphy_template;
		if (phy_type == PHY_TYPE_LCN) {
			/* Single stream */
			band->ht_cap.mcs.rx_mask[1] = 0;
1017
			band->ht_cap.mcs.rx_highest = cpu_to_le16(72);
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
		}
		hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
	} else {
		return -EPERM;
	}

	/* Assume all bands use the same phy.  True for 11n devices. */
	if (wl->pub->_nbands > 1) {
		has_5g++;
		if (phy_type == PHY_TYPE_N || phy_type == PHY_TYPE_LCN) {
			band = &wlc->bandstate[BAND_5G_INDEX]->band;
			*band = brcms_band_5GHz_nphy_template;
			hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
		} else {
			return -EPERM;
		}
	}
	return 0;
}

/*
 * is called in brcms_pci_probe() context, therefore no locking required.
 */
static int ieee_hw_init(struct ieee80211_hw *hw)
{
	hw->flags = IEEE80211_HW_SIGNAL_DBM
	    /* | IEEE80211_HW_CONNECTION_MONITOR  What is this? */
	    | IEEE80211_HW_REPORTS_TX_ACK_STATUS
	    | IEEE80211_HW_AMPDU_AGGREGATION;

	hw->extra_tx_headroom = brcms_c_get_header_len();
	hw->queues = N_TX_QUEUES;
	hw->max_rates = 2;	/* Primary rate and 1 fallback rate */

	/* channel change time is dependent on chip and band  */
	hw->channel_change_time = 7 * 1000;
	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);

1056 1057 1058 1059 1060 1061
	/*
	 * deactivate sending probe responses by ucude, because this will
	 * cause problems when WPS is used.
	 *
	 * hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
	 */
1062

1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
	hw->rate_control_algorithm = "minstrel_ht";

	hw->sta_data_size = 0;
	return ieee_hw_rate_init(hw);
}

/**
 * attach to the WL device.
 *
 * Attach to the WL device identified by vendor and device parameters.
 * regs is a host accessible memory address pointing to WL device registers.
 *
 * brcms_attach is not defined as static because in the case where no bus
 * is defined, wl_attach will never be called, and thus, gcc will issue
 * a warning that this function is defined but not used if we declare
 * it as static.
 *
 *
1081
 * is called in brcms_bcma_probe() context, therefore no locking required.
1082
 */
1083
static struct brcms_info *brcms_attach(struct bcma_device *pdev)
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
{
	struct brcms_info *wl = NULL;
	int unit, err;
	struct ieee80211_hw *hw;
	u8 perm[ETH_ALEN];

	unit = n_adapters_found;
	err = 0;

	if (unit < 0)
		return NULL;

	/* allocate private info */
1097
	hw = bcma_get_drvdata(pdev);
1098 1099 1100 1101 1102 1103 1104 1105
	if (hw != NULL)
		wl = hw->priv;
	if (WARN_ON(hw == NULL) || WARN_ON(wl == NULL))
		return NULL;
	wl->wiphy = hw->wiphy;

	atomic_set(&wl->callbacks, 0);

1106 1107
	init_waitqueue_head(&wl->tx_flush_wq);

1108 1109 1110 1111 1112 1113 1114
	/* setup the bottom half handler */
	tasklet_init(&wl->tasklet, brcms_dpc, (unsigned long) wl);

	spin_lock_init(&wl->lock);
	spin_lock_init(&wl->isr_lock);

	/* prepare ucode */
1115
	if (brcms_request_fw(wl, pdev) < 0) {
1116 1117 1118
		wiphy_err(wl->wiphy, "%s: Failed to find firmware usually in "
			  "%s\n", KBUILD_MODNAME, "/lib/firmware/brcm");
		brcms_release_fw(wl);
1119
		brcms_remove(pdev);
1120 1121 1122 1123
		return NULL;
	}

	/* common load-time initialization */
1124
	wl->wlc = brcms_c_attach((void *)wl, pdev, unit, false, &err);
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
	brcms_release_fw(wl);
	if (!wl->wlc) {
		wiphy_err(wl->wiphy, "%s: attach() failed with code %d\n",
			  KBUILD_MODNAME, err);
		goto fail;
	}
	wl->pub = brcms_c_pub(wl->wlc);

	wl->pub->ieee_hw = hw;

	/* register our interrupt handler */
1136
	if (request_irq(pdev->irq, brcms_isr,
1137
			IRQF_SHARED, KBUILD_MODNAME, wl)) {
1138 1139 1140
		wiphy_err(wl->wiphy, "wl%d: request_irq() failed\n", unit);
		goto fail;
	}
1141
	wl->irq = pdev->irq;
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151

	/* register module */
	brcms_c_module_register(wl->pub, "linux", wl, NULL);

	if (ieee_hw_init(hw)) {
		wiphy_err(wl->wiphy, "wl%d: %s: ieee_hw_init failed!\n", unit,
			  __func__);
		goto fail;
	}

1152 1153
	brcms_c_regd_init(wl->wlc);

1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
	memcpy(perm, &wl->pub->cur_etheraddr, ETH_ALEN);
	if (WARN_ON(!is_valid_ether_addr(perm)))
		goto fail;
	SET_IEEE80211_PERM_ADDR(hw, perm);

	err = ieee80211_register_hw(hw);
	if (err)
		wiphy_err(wl->wiphy, "%s: ieee80211_register_hw failed, status"
			  "%d\n", __func__, err);

1164 1165 1166
	if (wl->pub->srom_ccode[0] &&
	    regulatory_hint(wl->wiphy, wl->pub->srom_ccode))
		wiphy_err(wl->wiphy, "%s: regulatory hint failed\n", __func__);
1167

P
Piotr Haber 已提交
1168 1169
	brcms_debugfs_attach(wl->pub);
	brcms_debugfs_create_files(wl->pub);
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
	n_adapters_found++;
	return wl;

fail:
	brcms_free(wl);
	return NULL;
}



/**
 * determines if a device is a WL device, and if so, attaches it.
 *
 * This function determines if a device pointed to by pdev is a WL device,
 * and if so, performs a brcms_attach() on it.
 *
 * Perimeter lock is initialized in the course of this function.
 */
1188
static int brcms_bcma_probe(struct bcma_device *pdev)
1189 1190 1191 1192
{
	struct brcms_info *wl;
	struct ieee80211_hw *hw;

1193 1194
	dev_info(&pdev->dev, "mfg %x core %x rev %d class %d irq %d\n",
		 pdev->id.manuf, pdev->id.id, pdev->id.rev, pdev->id.class,
1195
		 pdev->irq);
1196

1197 1198
	if ((pdev->id.manuf != BCMA_MANUF_BCM) ||
	    (pdev->id.id != BCMA_CORE_80211))
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
		return -ENODEV;

	hw = ieee80211_alloc_hw(sizeof(struct brcms_info), &brcms_ops);
	if (!hw) {
		pr_err("%s: ieee80211_alloc_hw failed\n", __func__);
		return -ENOMEM;
	}

	SET_IEEE80211_DEV(hw, &pdev->dev);

1209
	bcma_set_drvdata(pdev, hw);
1210 1211 1212

	memset(hw->priv, 0, sizeof(*wl));

1213
	wl = brcms_attach(pdev);
1214
	if (!wl) {
1215
		pr_err("%s: brcms_attach failed!\n", __func__);
1216 1217
		return -ENODEV;
	}
P
Piotr Haber 已提交
1218 1219
	brcms_led_register(wl);

1220 1221 1222
	return 0;
}

1223
static int brcms_suspend(struct bcma_device *pdev)
1224 1225 1226 1227
{
	struct brcms_info *wl;
	struct ieee80211_hw *hw;

1228
	hw = bcma_get_drvdata(pdev);
1229 1230
	wl = hw->priv;
	if (!wl) {
1231 1232
		pr_err("%s: %s: no driver private struct!\n", KBUILD_MODNAME,
		       __func__);
1233 1234 1235 1236 1237 1238 1239 1240
		return -ENODEV;
	}

	/* only need to flag hw is down for proper resume */
	spin_lock_bh(&wl->lock);
	wl->pub->hw_up = false;
	spin_unlock_bh(&wl->lock);

1241
	brcms_dbg_info(wl->wlc->hw->d11core, "brcms_suspend ok\n");
1242

1243 1244 1245 1246 1247
	return 0;
}

static int brcms_resume(struct bcma_device *pdev)
{
1248
	return 0;
1249 1250
}

1251
static struct bcma_driver brcms_bcma_driver = {
1252
	.name     = KBUILD_MODNAME,
1253
	.probe    = brcms_bcma_probe,
1254 1255
	.suspend  = brcms_suspend,
	.resume   = brcms_resume,
1256
	.remove   = brcms_remove,
1257
	.id_table = brcms_coreid_table,
1258 1259 1260
};

/**
1261
 * This is the main entry point for the brcmsmac driver.
1262
 *
1263 1264 1265
 * This function is scheduled upon module initialization and
 * does the driver registration, which result in brcms_bcma_probe()
 * call resulting in the driver bringup.
1266
 */
1267
static void brcms_driver_init(struct work_struct *work)
1268
{
1269 1270 1271 1272 1273 1274 1275 1276
	int error;

	error = bcma_driver_register(&brcms_bcma_driver);
	if (error)
		pr_err("%s: register returned %d\n", __func__, error);
}

static DECLARE_WORK(brcms_driver_work, brcms_driver_init);
1277

1278 1279
static int __init brcms_module_init(void)
{
P
Piotr Haber 已提交
1280
	brcms_debugfs_init();
1281 1282
	if (!schedule_work(&brcms_driver_work))
		return -EBUSY;
1283

1284
	return 0;
1285 1286 1287
}

/**
1288
 * This function unloads the brcmsmac driver from the system.
1289
 *
1290
 * This function unconditionally unloads the brcmsmac driver module from the
1291 1292 1293 1294 1295
 * system.
 *
 */
static void __exit brcms_module_exit(void)
{
1296
	cancel_work_sync(&brcms_driver_work);
1297
	bcma_driver_unregister(&brcms_bcma_driver);
P
Piotr Haber 已提交
1298
	brcms_debugfs_exit();
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
}

module_init(brcms_module_init);
module_exit(brcms_module_exit);

/*
 * precondition: perimeter lock has been acquired
 */
void brcms_txflowcontrol(struct brcms_info *wl, struct brcms_if *wlif,
			 bool state, int prio)
{
1310
	brcms_err(wl->wlc->hw->d11core, "Shouldn't be here %s\n", __func__);
1311 1312 1313 1314 1315 1316 1317
}

/*
 * precondition: perimeter lock has been acquired
 */
void brcms_init(struct brcms_info *wl)
{
1318 1319
	brcms_dbg_info(wl->wlc->hw->d11core, "Initializing wl%d\n",
		       wl->pub->unit);
1320
	brcms_reset(wl);
1321
	brcms_c_init(wl->wlc, wl->mute_tx);
1322 1323 1324 1325 1326 1327 1328
}

/*
 * precondition: perimeter lock has been acquired
 */
uint brcms_reset(struct brcms_info *wl)
{
1329
	brcms_dbg_info(wl->wlc->hw->d11core, "Resetting wl%d\n", wl->pub->unit);
1330 1331 1332
	brcms_c_reset(wl->wlc);

	/* dpc will not be rescheduled */
1333
	wl->resched = false;
1334

1335 1336 1337
	/* inform publicly that interface is down */
	wl->pub->up = false;

1338 1339 1340
	return 0;
}

1341 1342
void brcms_fatal_error(struct brcms_info *wl)
{
1343
	brcms_err(wl->wlc->hw->d11core, "wl%d: fatal error, reinitializing\n",
1344 1345 1346 1347 1348
		  wl->wlc->pub->unit);
	brcms_reset(wl);
	ieee80211_restart_hw(wl->pub->ieee_hw);
}

1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
/*
 * These are interrupt on/off entry points. Disable interrupts
 * during interrupt state transition.
 */
void brcms_intrson(struct brcms_info *wl)
{
	unsigned long flags;

	spin_lock_irqsave(&wl->isr_lock, flags);
	brcms_c_intrson(wl->wlc);
	spin_unlock_irqrestore(&wl->isr_lock, flags);
}

u32 brcms_intrsoff(struct brcms_info *wl)
{
	unsigned long flags;
	u32 status;

	spin_lock_irqsave(&wl->isr_lock, flags);
	status = brcms_c_intrsoff(wl->wlc);
	spin_unlock_irqrestore(&wl->isr_lock, flags);
	return status;
}

void brcms_intrsrestore(struct brcms_info *wl, u32 macintmask)
{
	unsigned long flags;

	spin_lock_irqsave(&wl->isr_lock, flags);
	brcms_c_intrsrestore(wl->wlc, macintmask);
	spin_unlock_irqrestore(&wl->isr_lock, flags);
}

/*
 * precondition: perimeter lock has been acquired
 */
int brcms_up(struct brcms_info *wl)
{
	int error = 0;

	if (wl->pub->up)
		return 0;

	error = brcms_c_up(wl->wlc);

	return error;
}

/*
 * precondition: perimeter lock has been acquired
 */
void brcms_down(struct brcms_info *wl)
{
	uint callbacks, ret_val = 0;

	/* call common down function */
	ret_val = brcms_c_down(wl->wlc);
	callbacks = atomic_read(&wl->callbacks) - ret_val;

	/* wait for down callbacks to complete */
	spin_unlock_bh(&wl->lock);

	/* For HIGH_only driver, it's important to actually schedule other work,
	 * not just spin wait since everything runs at schedule level
	 */
	SPINWAIT((atomic_read(&wl->callbacks) > callbacks), 100 * 1000);

	spin_lock_bh(&wl->lock);
}

/*
* precondition: perimeter lock is not acquired
 */
1422
static void _brcms_timer(struct work_struct *work)
1423
{
1424 1425 1426
	struct brcms_timer *t = container_of(work, struct brcms_timer,
					     dly_wrk.work);

1427 1428 1429 1430 1431
	spin_lock_bh(&t->wl->lock);

	if (t->set) {
		if (t->periodic) {
			atomic_inc(&t->wl->callbacks);
1432 1433 1434 1435
			ieee80211_queue_delayed_work(t->wl->pub->ieee_hw,
						     &t->dly_wrk,
						     msecs_to_jiffies(t->ms));
		} else {
1436
			t->set = false;
1437
		}
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462

		t->fn(t->arg);
	}

	atomic_dec(&t->wl->callbacks);

	spin_unlock_bh(&t->wl->lock);
}

/*
 * Adds a timer to the list. Caller supplies a timer function.
 * Is called from wlc.
 *
 * precondition: perimeter lock has been acquired
 */
struct brcms_timer *brcms_init_timer(struct brcms_info *wl,
				     void (*fn) (void *arg),
				     void *arg, const char *name)
{
	struct brcms_timer *t;

	t = kzalloc(sizeof(struct brcms_timer), GFP_ATOMIC);
	if (!t)
		return NULL;

1463
	INIT_DELAYED_WORK(&t->dly_wrk, _brcms_timer);
1464 1465 1466 1467 1468 1469
	t->wl = wl;
	t->fn = fn;
	t->arg = arg;
	t->next = wl->timers;
	wl->timers = t;

J
Joe Perches 已提交
1470
#ifdef DEBUG
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
	t->name = kmalloc(strlen(name) + 1, GFP_ATOMIC);
	if (t->name)
		strcpy(t->name, name);
#endif

	return t;
}

/*
 * adds only the kernel timer since it's going to be more accurate
 * as well as it's easier to make it periodic
 *
 * precondition: perimeter lock has been acquired
 */
1485
void brcms_add_timer(struct brcms_timer *t, uint ms, int periodic)
1486
{
1487 1488
	struct ieee80211_hw *hw = t->wl->pub->ieee_hw;

J
Joe Perches 已提交
1489
#ifdef DEBUG
1490
	if (t->set)
1491 1492 1493
		brcms_dbg_info(t->wl->wlc->hw->d11core,
			       "%s: Already set. Name: %s, per %d\n",
			       __func__, t->name, periodic);
1494 1495 1496
#endif
	t->ms = ms;
	t->periodic = (bool) periodic;
1497 1498 1499 1500
	if (!t->set) {
		t->set = true;
		atomic_inc(&t->wl->callbacks);
	}
1501 1502

	ieee80211_queue_delayed_work(hw, &t->dly_wrk, msecs_to_jiffies(ms));
1503 1504 1505 1506 1507 1508 1509
}

/*
 * return true if timer successfully deleted, false if still pending
 *
 * precondition: perimeter lock has been acquired
 */
1510
bool brcms_del_timer(struct brcms_timer *t)
1511 1512 1513
{
	if (t->set) {
		t->set = false;
1514
		if (!cancel_delayed_work(&t->dly_wrk))
1515 1516
			return false;

1517
		atomic_dec(&t->wl->callbacks);
1518 1519 1520 1521 1522 1523 1524 1525
	}

	return true;
}

/*
 * precondition: perimeter lock has been acquired
 */
1526
void brcms_free_timer(struct brcms_timer *t)
1527
{
1528
	struct brcms_info *wl = t->wl;
1529 1530 1531
	struct brcms_timer *tmp;

	/* delete the timer in case it is active */
1532
	brcms_del_timer(t);
1533 1534 1535

	if (wl->timers == t) {
		wl->timers = wl->timers->next;
J
Joe Perches 已提交
1536
#ifdef DEBUG
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
		kfree(t->name);
#endif
		kfree(t);
		return;

	}

	tmp = wl->timers;
	while (tmp) {
		if (tmp->next == t) {
			tmp->next = t->next;
J
Joe Perches 已提交
1548
#ifdef DEBUG
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
			kfree(t->name);
#endif
			kfree(t);
			return;
		}
		tmp = tmp->next;
	}

}

/*
 * precondition: perimeter lock has been acquired
 */
int brcms_ucode_init_buf(struct brcms_info *wl, void **pbuf, u32 idx)
{
	int i, entry;
	const u8 *pdata;
	struct firmware_hdr *hdr;
	for (i = 0; i < wl->fw.fw_cnt; i++) {
		hdr = (struct firmware_hdr *)wl->fw.fw_hdr[i]->data;
		for (entry = 0; entry < wl->fw.hdr_num_entries[i];
		     entry++, hdr++) {
			u32 len = le32_to_cpu(hdr->len);
			if (le32_to_cpu(hdr->idx) == idx) {
				pdata = wl->fw.fw_bin[i]->data +
					le32_to_cpu(hdr->offset);
1575
				*pbuf = kmemdup(pdata, len, GFP_ATOMIC);
1576 1577 1578 1579 1580 1581 1582
				if (*pbuf == NULL)
					goto fail;

				return 0;
			}
		}
	}
1583 1584
	brcms_err(wl->wlc->hw->d11core,
		  "ERROR: ucode buf tag:%d can not be found!\n", idx);
1585 1586 1587 1588 1589 1590
	*pbuf = NULL;
fail:
	return -ENODATA;
}

/*
1591
 * Precondition: Since this function is called in brcms_bcma_probe() context,
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
 * no locking is required.
 */
int brcms_ucode_init_uint(struct brcms_info *wl, size_t *n_bytes, u32 idx)
{
	int i, entry;
	const u8 *pdata;
	struct firmware_hdr *hdr;
	for (i = 0; i < wl->fw.fw_cnt; i++) {
		hdr = (struct firmware_hdr *)wl->fw.fw_hdr[i]->data;
		for (entry = 0; entry < wl->fw.hdr_num_entries[i];
		     entry++, hdr++) {
			if (le32_to_cpu(hdr->idx) == idx) {
				pdata = wl->fw.fw_bin[i]->data +
					le32_to_cpu(hdr->offset);
				if (le32_to_cpu(hdr->len) != 4) {
1607
					brcms_err(wl->wlc->hw->d11core,
1608 1609 1610 1611 1612 1613 1614 1615
						  "ERROR: fw hdr len\n");
					return -ENOMSG;
				}
				*n_bytes = le32_to_cpu(*((__le32 *) pdata));
				return 0;
			}
		}
	}
1616 1617
	brcms_err(wl->wlc->hw->d11core,
		  "ERROR: ucode tag:%d can not be found!\n", idx);
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
	return -ENOMSG;
}

/*
 * precondition: can both be called locked and unlocked
 */
void brcms_ucode_free_buf(void *p)
{
	kfree(p);
}

/*
 * checks validity of all firmware images loaded from user space
 *
1632
 * Precondition: Since this function is called in brcms_bcma_probe() context,
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
 * no locking is required.
 */
int brcms_check_firmwares(struct brcms_info *wl)
{
	int i;
	int entry;
	int rc = 0;
	const struct firmware *fw;
	const struct firmware *fw_hdr;
	struct firmware_hdr *ucode_hdr;
	for (i = 0; i < MAX_FW_IMAGES && rc == 0; i++) {
		fw =  wl->fw.fw_bin[i];
		fw_hdr = wl->fw.fw_hdr[i];
		if (fw == NULL && fw_hdr == NULL) {
			break;
		} else if (fw == NULL || fw_hdr == NULL) {
			wiphy_err(wl->wiphy, "%s: invalid bin/hdr fw\n",
				  __func__);
			rc = -EBADF;
		} else if (fw_hdr->size % sizeof(struct firmware_hdr)) {
			wiphy_err(wl->wiphy, "%s: non integral fw hdr file "
				"size %zu/%zu\n", __func__, fw_hdr->size,
				sizeof(struct firmware_hdr));
			rc = -EBADF;
		} else if (fw->size < MIN_FW_SIZE || fw->size > MAX_FW_SIZE) {
1658 1659
			wiphy_err(wl->wiphy, "%s: out of bounds fw file size %zu\n",
				  __func__, fw->size);
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698
			rc = -EBADF;
		} else {
			/* check if ucode section overruns firmware image */
			ucode_hdr = (struct firmware_hdr *)fw_hdr->data;
			for (entry = 0; entry < wl->fw.hdr_num_entries[i] &&
			     !rc; entry++, ucode_hdr++) {
				if (le32_to_cpu(ucode_hdr->offset) +
				    le32_to_cpu(ucode_hdr->len) >
				    fw->size) {
					wiphy_err(wl->wiphy,
						  "%s: conflicting bin/hdr\n",
						  __func__);
					rc = -EBADF;
				}
			}
		}
	}
	if (rc == 0 && wl->fw.fw_cnt != i) {
		wiphy_err(wl->wiphy, "%s: invalid fw_cnt=%d\n", __func__,
			wl->fw.fw_cnt);
		rc = -EBADF;
	}
	return rc;
}

/*
 * precondition: perimeter lock has been acquired
 */
bool brcms_rfkill_set_hw_state(struct brcms_info *wl)
{
	bool blocked = brcms_c_check_radio_disabled(wl->wlc);

	spin_unlock_bh(&wl->lock);
	wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, blocked);
	if (blocked)
		wiphy_rfkill_start_polling(wl->pub->ieee_hw->wiphy);
	spin_lock_bh(&wl->lock);
	return blocked;
}