File Index
Files and directories
Sign in
FILE

cfg.c

File preview with syntax highlighting.

linux-6.19.14/drivers/net/wireless/marvell/libertas/cfg.c C
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Implement cfg80211 ("iw") support.
   4 *
   5 * Copyright (C) 2009 M&N Solutions GmbH, 61191 Rosbach, Germany
   6 * Holger Schurig <hs4233@mail.mn-solutions.de>
   7 *
   8 */
   9
  10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11
  12#include <linux/hardirq.h>
  13#include <linux/sched.h>
  14#include <linux/wait.h>
  15#include <linux/slab.h>
  16#include <linux/ieee80211.h>
  17#include <net/cfg80211.h>
  18#include <linux/unaligned.h>
  19
  20#include "decl.h"
  21#include "cfg.h"
  22#include "cmd.h"
  23#include "mesh.h"
  24
  25
  26#define CHAN2G(_channel, _freq, _flags) {        \
  27	.band             = NL80211_BAND_2GHZ, \
  28	.center_freq      = (_freq),             \
  29	.hw_value         = (_channel),          \
  30	.flags            = (_flags),            \
  31	.max_antenna_gain = 0,                   \
  32	.max_power        = 30,                  \
  33}
  34
  35static struct ieee80211_channel lbs_2ghz_channels[] = {
  36	CHAN2G(1,  2412, 0),
  37	CHAN2G(2,  2417, 0),
  38	CHAN2G(3,  2422, 0),
  39	CHAN2G(4,  2427, 0),
  40	CHAN2G(5,  2432, 0),
  41	CHAN2G(6,  2437, 0),
  42	CHAN2G(7,  2442, 0),
  43	CHAN2G(8,  2447, 0),
  44	CHAN2G(9,  2452, 0),
  45	CHAN2G(10, 2457, 0),
  46	CHAN2G(11, 2462, 0),
  47	CHAN2G(12, 2467, 0),
  48	CHAN2G(13, 2472, 0),
  49	CHAN2G(14, 2484, 0),
  50};
  51
  52#define RATETAB_ENT(_rate, _hw_value, _flags) { \
  53	.bitrate  = (_rate),                    \
  54	.hw_value = (_hw_value),                \
  55	.flags    = (_flags),                   \
  56}
  57
  58
  59/* Table 6 in section 3.2.1.1 */
  60static struct ieee80211_rate lbs_rates[] = {
  61	RATETAB_ENT(10,  0,  0),
  62	RATETAB_ENT(20,  1,  0),
  63	RATETAB_ENT(55,  2,  0),
  64	RATETAB_ENT(110, 3,  0),
  65	RATETAB_ENT(60,  9,  0),
  66	RATETAB_ENT(90,  6,  0),
  67	RATETAB_ENT(120, 7,  0),
  68	RATETAB_ENT(180, 8,  0),
  69	RATETAB_ENT(240, 9,  0),
  70	RATETAB_ENT(360, 10, 0),
  71	RATETAB_ENT(480, 11, 0),
  72	RATETAB_ENT(540, 12, 0),
  73};
  74
  75static struct ieee80211_supported_band lbs_band_2ghz = {
  76	.channels = lbs_2ghz_channels,
  77	.n_channels = ARRAY_SIZE(lbs_2ghz_channels),
  78	.bitrates = lbs_rates,
  79	.n_bitrates = ARRAY_SIZE(lbs_rates),
  80};
  81
  82
  83static const u32 cipher_suites[] = {
  84	WLAN_CIPHER_SUITE_WEP40,
  85	WLAN_CIPHER_SUITE_WEP104,
  86	WLAN_CIPHER_SUITE_TKIP,
  87	WLAN_CIPHER_SUITE_CCMP,
  88};
  89
  90/* Time to stay on the channel */
  91#define LBS_DWELL_PASSIVE 100
  92#define LBS_DWELL_ACTIVE  40
  93
  94
  95/***************************************************************************
  96 * Misc utility functions
  97 *
  98 * TLVs are Marvell specific. They are very similar to IEs, they have the
  99 * same structure: type, length, data*. The only difference: for IEs, the
 100 * type and length are u8, but for TLVs they're __le16.
 101 */
 102
 103/*
 104 * Convert NL80211's auth_type to the one from Libertas, see chapter 5.9.1
 105 * in the firmware spec
 106 */
 107static int lbs_auth_to_authtype(enum nl80211_auth_type auth_type)
 108{
 109	int ret = -ENOTSUPP;
 110
 111	switch (auth_type) {
 112	case NL80211_AUTHTYPE_OPEN_SYSTEM:
 113	case NL80211_AUTHTYPE_SHARED_KEY:
 114		ret = auth_type;
 115		break;
 116	case NL80211_AUTHTYPE_AUTOMATIC:
 117		ret = NL80211_AUTHTYPE_OPEN_SYSTEM;
 118		break;
 119	case NL80211_AUTHTYPE_NETWORK_EAP:
 120		ret = 0x80;
 121		break;
 122	default:
 123		/* silence compiler */
 124		break;
 125	}
 126	return ret;
 127}
 128
 129
 130/*
 131 * Various firmware commands need the list of supported rates, but with
 132 * the hight-bit set for basic rates
 133 */
 134static int lbs_add_rates(u8 *rates)
 135{
 136	size_t i;
 137
 138	for (i = 0; i < ARRAY_SIZE(lbs_rates); i++) {
 139		u8 rate = lbs_rates[i].bitrate / 5;
 140		if (rate == 0x02 || rate == 0x04 ||
 141		    rate == 0x0b || rate == 0x16)
 142			rate |= 0x80;
 143		rates[i] = rate;
 144	}
 145	return ARRAY_SIZE(lbs_rates);
 146}
 147
 148
 149/***************************************************************************
 150 * TLV utility functions
 151 *
 152 * TLVs are Marvell specific. They are very similar to IEs, they have the
 153 * same structure: type, length, data*. The only difference: for IEs, the
 154 * type and length are u8, but for TLVs they're __le16.
 155 */
 156
 157
 158/*
 159 * Add ssid TLV
 160 */
 161#define LBS_MAX_SSID_TLV_SIZE			\
 162	(sizeof(struct mrvl_ie_header)		\
 163	 + IEEE80211_MAX_SSID_LEN)
 164
 165static int lbs_add_ssid_tlv(u8 *tlv, const u8 *ssid, int ssid_len)
 166{
 167	struct mrvl_ie_ssid_param_set *ssid_tlv = (void *)tlv;
 168
 169	/*
 170	 * TLV-ID SSID  00 00
 171	 * length       06 00
 172	 * ssid         4d 4e 54 45 53 54
 173	 */
 174	ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
 175	ssid_tlv->header.len = cpu_to_le16(ssid_len);
 176	memcpy(ssid_tlv->ssid, ssid, ssid_len);
 177	return sizeof(ssid_tlv->header) + ssid_len;
 178}
 179
 180
 181/*
 182 * Add channel list TLV (section 8.4.2)
 183 *
 184 * Actual channel data comes from priv->wdev->wiphy->channels.
 185 */
 186#define LBS_MAX_CHANNEL_LIST_TLV_SIZE					\
 187	(sizeof(struct mrvl_ie_header)					\
 188	 + (LBS_SCAN_BEFORE_NAP * sizeof(struct chanscanparamset)))
 189
 190static int lbs_add_channel_list_tlv(struct lbs_private *priv, u8 *tlv,
 191				    int last_channel, int active_scan)
 192{
 193	int chanscanparamsize = sizeof(struct chanscanparamset) *
 194		(last_channel - priv->scan_channel);
 195
 196	struct mrvl_ie_header *header = (void *) tlv;
 197
 198	/*
 199	 * TLV-ID CHANLIST  01 01
 200	 * length           0e 00
 201	 * channel          00 01 00 00 00 64 00
 202	 *   radio type     00
 203	 *   channel           01
 204	 *   scan type            00
 205	 *   min scan time           00 00
 206	 *   max scan time                 64 00
 207	 * channel 2        00 02 00 00 00 64 00
 208	 *
 209	 */
 210
 211	header->type = cpu_to_le16(TLV_TYPE_CHANLIST);
 212	header->len  = cpu_to_le16(chanscanparamsize);
 213	tlv += sizeof(struct mrvl_ie_header);
 214
 215	/* lbs_deb_scan("scan: channels %d to %d\n", priv->scan_channel,
 216		     last_channel); */
 217	memset(tlv, 0, chanscanparamsize);
 218
 219	while (priv->scan_channel < last_channel) {
 220		struct chanscanparamset *param = (void *) tlv;
 221
 222		param->radiotype = CMD_SCAN_RADIO_TYPE_BG;
 223		param->channumber =
 224			priv->scan_req->channels[priv->scan_channel]->hw_value;
 225		if (active_scan) {
 226			param->maxscantime = cpu_to_le16(LBS_DWELL_ACTIVE);
 227		} else {
 228			param->chanscanmode.passivescan = 1;
 229			param->maxscantime = cpu_to_le16(LBS_DWELL_PASSIVE);
 230		}
 231		tlv += sizeof(struct chanscanparamset);
 232		priv->scan_channel++;
 233	}
 234	return sizeof(struct mrvl_ie_header) + chanscanparamsize;
 235}
 236
 237
 238/*
 239 * Add rates TLV
 240 *
 241 * The rates are in lbs_bg_rates[], but for the 802.11b
 242 * rates the high bit is set. We add this TLV only because
 243 * there's a firmware which otherwise doesn't report all
 244 * APs in range.
 245 */
 246#define LBS_MAX_RATES_TLV_SIZE			\
 247	(sizeof(struct mrvl_ie_header)		\
 248	 + (ARRAY_SIZE(lbs_rates)))
 249
 250/* Adds a TLV with all rates the hardware supports */
 251static int lbs_add_supported_rates_tlv(u8 *tlv)
 252{
 253	size_t i;
 254	struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
 255
 256	/*
 257	 * TLV-ID RATES  01 00
 258	 * length        0e 00
 259	 * rates         82 84 8b 96 0c 12 18 24 30 48 60 6c
 260	 */
 261	rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
 262	tlv += sizeof(rate_tlv->header);
 263	i = lbs_add_rates(tlv);
 264	tlv += i;
 265	rate_tlv->header.len = cpu_to_le16(i);
 266	return sizeof(rate_tlv->header) + i;
 267}
 268
 269/* Add common rates from a TLV and return the new end of the TLV */
 270static u8 *
 271add_ie_rates(u8 *tlv, const u8 *ie, int *nrates)
 272{
 273	int hw, ap, ap_max = ie[1];
 274	u8 hw_rate;
 275
 276	if (ap_max > MAX_RATES) {
 277		lbs_deb_assoc("invalid rates\n");
 278		return tlv;
 279	}
 280	/* Advance past IE header */
 281	ie += 2;
 282
 283	lbs_deb_hex(LBS_DEB_ASSOC, "AP IE Rates", (u8 *) ie, ap_max);
 284
 285	for (hw = 0; hw < ARRAY_SIZE(lbs_rates); hw++) {
 286		hw_rate = lbs_rates[hw].bitrate / 5;
 287		for (ap = 0; ap < ap_max; ap++) {
 288			if (hw_rate == (ie[ap] & 0x7f)) {
 289				*tlv++ = ie[ap];
 290				*nrates = *nrates + 1;
 291			}
 292		}
 293	}
 294	return tlv;
 295}
 296
 297/*
 298 * Adds a TLV with all rates the hardware *and* BSS supports.
 299 */
 300static int lbs_add_common_rates_tlv(u8 *tlv, struct cfg80211_bss *bss)
 301{
 302	struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
 303	const u8 *rates_eid, *ext_rates_eid;
 304	int n = 0;
 305
 306	rcu_read_lock();
 307	rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
 308	ext_rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
 309
 310	/*
 311	 * 01 00                   TLV_TYPE_RATES
 312	 * 04 00                   len
 313	 * 82 84 8b 96             rates
 314	 */
 315	rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
 316	tlv += sizeof(rate_tlv->header);
 317
 318	/* Add basic rates */
 319	if (rates_eid) {
 320		tlv = add_ie_rates(tlv, rates_eid, &n);
 321
 322		/* Add extended rates, if any */
 323		if (ext_rates_eid)
 324			tlv = add_ie_rates(tlv, ext_rates_eid, &n);
 325	} else {
 326		lbs_deb_assoc("assoc: bss had no basic rate IE\n");
 327		/* Fallback: add basic 802.11b rates */
 328		*tlv++ = 0x82;
 329		*tlv++ = 0x84;
 330		*tlv++ = 0x8b;
 331		*tlv++ = 0x96;
 332		n = 4;
 333	}
 334	rcu_read_unlock();
 335
 336	rate_tlv->header.len = cpu_to_le16(n);
 337	return sizeof(rate_tlv->header) + n;
 338}
 339
 340
 341/*
 342 * Add auth type TLV.
 343 *
 344 * This is only needed for newer firmware (V9 and up).
 345 */
 346#define LBS_MAX_AUTH_TYPE_TLV_SIZE \
 347	sizeof(struct mrvl_ie_auth_type)
 348
 349static int lbs_add_auth_type_tlv(u8 *tlv, enum nl80211_auth_type auth_type)
 350{
 351	struct mrvl_ie_auth_type *auth = (void *) tlv;
 352
 353	/*
 354	 * 1f 01  TLV_TYPE_AUTH_TYPE
 355	 * 01 00  len
 356	 * 01     auth type
 357	 */
 358	auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
 359	auth->header.len = cpu_to_le16(sizeof(*auth)-sizeof(auth->header));
 360	auth->auth = cpu_to_le16(lbs_auth_to_authtype(auth_type));
 361	return sizeof(*auth);
 362}
 363
 364
 365/*
 366 * Add channel (phy ds) TLV
 367 */
 368#define LBS_MAX_CHANNEL_TLV_SIZE \
 369	sizeof(struct mrvl_ie_header)
 370
 371static int lbs_add_channel_tlv(u8 *tlv, u8 channel)
 372{
 373	struct mrvl_ie_ds_param_set *ds = (void *) tlv;
 374
 375	/*
 376	 * 03 00  TLV_TYPE_PHY_DS
 377	 * 01 00  len
 378	 * 06     channel
 379	 */
 380	ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
 381	ds->header.len = cpu_to_le16(sizeof(*ds)-sizeof(ds->header));
 382	ds->channel = channel;
 383	return sizeof(*ds);
 384}
 385
 386
 387/*
 388 * Add (empty) CF param TLV of the form:
 389 */
 390#define LBS_MAX_CF_PARAM_TLV_SIZE		\
 391	sizeof(struct mrvl_ie_header)
 392
 393static int lbs_add_cf_param_tlv(u8 *tlv)
 394{
 395	struct mrvl_ie_cf_param_set *cf = (void *)tlv;
 396
 397	/*
 398	 * 04 00  TLV_TYPE_CF
 399	 * 06 00  len
 400	 * 00     cfpcnt
 401	 * 00     cfpperiod
 402	 * 00 00  cfpmaxduration
 403	 * 00 00  cfpdurationremaining
 404	 */
 405	cf->header.type = cpu_to_le16(TLV_TYPE_CF);
 406	cf->header.len = cpu_to_le16(sizeof(*cf)-sizeof(cf->header));
 407	return sizeof(*cf);
 408}
 409
 410/*
 411 * Add WPA TLV
 412 */
 413#define LBS_MAX_WPA_TLV_SIZE			\
 414	(sizeof(struct mrvl_ie_header)		\
 415	 + 128 /* TODO: I guessed the size */)
 416
 417static int lbs_add_wpa_tlv(u8 *tlv, const u8 *ie, u8 ie_len)
 418{
 419	struct mrvl_ie_data *wpatlv = (struct mrvl_ie_data *)tlv;
 420	const struct element *wpaie;
 421
 422	/* Find the first RSN or WPA IE to use */
 423	wpaie = cfg80211_find_elem(WLAN_EID_RSN, ie, ie_len);
 424	if (!wpaie)
 425		wpaie = cfg80211_find_vendor_elem(WLAN_OUI_MICROSOFT,
 426						  WLAN_OUI_TYPE_MICROSOFT_WPA,
 427						  ie, ie_len);
 428	if (!wpaie || wpaie->datalen > 128)
 429		return 0;
 430
 431	/*
 432	 * Convert the found IE to a TLV. IEs use u8 for the header,
 433	 *   u8      type
 434	 *   u8      len
 435	 *   u8[]    data
 436	 * but TLVs use __le16 instead:
 437	 *   __le16  type
 438	 *   __le16  len
 439	 *   u8[]    data
 440	 */
 441	wpatlv->header.type = cpu_to_le16(wpaie->id);
 442	wpatlv->header.len = cpu_to_le16(wpaie->datalen);
 443	memcpy(wpatlv->data, wpaie->data, wpaie->datalen);
 444
 445	/* Return the total number of bytes added to the TLV buffer */
 446	return sizeof(struct mrvl_ie_header) + wpaie->datalen;
 447}
 448
 449/* Add WPS enrollee TLV
 450 */
 451#define LBS_MAX_WPS_ENROLLEE_TLV_SIZE		\
 452	(sizeof(struct mrvl_ie_header)		\
 453	 + 256)
 454
 455static int lbs_add_wps_enrollee_tlv(u8 *tlv, const u8 *ie, size_t ie_len)
 456{
 457	struct mrvl_ie_data *wpstlv = (struct mrvl_ie_data *)tlv;
 458	const struct element *wpsie;
 459
 460	/* Look for a WPS IE and add it to the probe request */
 461	wpsie = cfg80211_find_vendor_elem(WLAN_OUI_MICROSOFT,
 462					  WLAN_OUI_TYPE_MICROSOFT_WPS,
 463					  ie, ie_len);
 464	if (!wpsie)
 465		return 0;
 466
 467	/* Convert the WPS IE to a TLV. The IE looks like this:
 468	 *   u8      type (WLAN_EID_VENDOR_SPECIFIC)
 469	 *   u8      len
 470	 *   u8[]    data
 471	 * but the TLV will look like this instead:
 472	 *   __le16  type (TLV_TYPE_WPS_ENROLLEE)
 473	 *   __le16  len
 474	 *   u8[]    data
 475	 */
 476	wpstlv->header.type = cpu_to_le16(TLV_TYPE_WPS_ENROLLEE);
 477	wpstlv->header.len = cpu_to_le16(wpsie->datalen);
 478	memcpy(wpstlv->data, wpsie->data, wpsie->datalen);
 479
 480	/* Return the total number of bytes added to the TLV buffer */
 481	return sizeof(struct mrvl_ie_header) + wpsie->datalen;
 482}
 483
 484/*
 485 * Set Channel
 486 */
 487
 488static int lbs_cfg_set_monitor_channel(struct wiphy *wiphy,
 489				       struct net_device *dev,
 490				       struct cfg80211_chan_def *chandef)
 491{
 492	struct lbs_private *priv = wiphy_priv(wiphy);
 493	int ret = -ENOTSUPP;
 494
 495	if (cfg80211_get_chandef_type(chandef) != NL80211_CHAN_NO_HT)
 496		goto out;
 497
 498	ret = lbs_set_channel(priv, chandef->chan->hw_value);
 499
 500 out:
 501	return ret;
 502}
 503
 504static int lbs_cfg_set_mesh_channel(struct wiphy *wiphy,
 505				    struct net_device *netdev,
 506				    struct ieee80211_channel *channel)
 507{
 508	struct lbs_private *priv = wiphy_priv(wiphy);
 509	int ret = -ENOTSUPP;
 510
 511	if (netdev != priv->mesh_dev)
 512		goto out;
 513
 514	ret = lbs_mesh_set_channel(priv, channel->hw_value);
 515
 516 out:
 517	return ret;
 518}
 519
 520
 521
 522/*
 523 * Scanning
 524 */
 525
 526/*
 527 * When scanning, the firmware doesn't send a nul packet with the power-safe
 528 * bit to the AP. So we cannot stay away from our current channel too long,
 529 * otherwise we loose data. So take a "nap" while scanning every other
 530 * while.
 531 */
 532#define LBS_SCAN_BEFORE_NAP 4
 533
 534
 535/*
 536 * When the firmware reports back a scan-result, it gives us an "u8 rssi",
 537 * which isn't really an RSSI, as it becomes larger when moving away from
 538 * the AP. Anyway, we need to convert that into mBm.
 539 */
 540#define LBS_SCAN_RSSI_TO_MBM(rssi) \
 541	((-(int)rssi + 3)*100)
 542
 543static int lbs_ret_scan(struct lbs_private *priv, unsigned long dummy,
 544	struct cmd_header *resp)
 545{
 546	struct cfg80211_bss *bss;
 547	struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
 548	int bsssize;
 549	const u8 *pos;
 550	const u8 *tsfdesc;
 551	int tsfsize;
 552	int i;
 553	int ret = -EILSEQ;
 554
 555	bsssize = get_unaligned_le16(&scanresp->bssdescriptsize);
 556
 557	lbs_deb_scan("scan response: %d BSSs (%d bytes); resp size %d bytes\n",
 558			scanresp->nr_sets, bsssize, le16_to_cpu(resp->size));
 559
 560	if (scanresp->nr_sets == 0) {
 561		ret = 0;
 562		goto done;
 563	}
 564
 565	/*
 566	 * The general layout of the scan response is described in chapter
 567	 * 5.7.1. Basically we have a common part, then any number of BSS
 568	 * descriptor sections. Finally we have section with the same number
 569	 * of TSFs.
 570	 *
 571	 * cmd_ds_802_11_scan_rsp
 572	 *   cmd_header
 573	 *   pos_size
 574	 *   nr_sets
 575	 *   bssdesc 1
 576	 *     bssid
 577	 *     rssi
 578	 *     timestamp
 579	 *     intvl
 580	 *     capa
 581	 *     IEs
 582	 *   bssdesc 2
 583	 *   bssdesc n
 584	 *   MrvlIEtypes_TsfFimestamp_t
 585	 *     TSF for BSS 1
 586	 *     TSF for BSS 2
 587	 *     TSF for BSS n
 588	 */
 589
 590	pos = scanresp->bssdesc_and_tlvbuffer;
 591
 592	lbs_deb_hex(LBS_DEB_SCAN, "SCAN_RSP", scanresp->bssdesc_and_tlvbuffer,
 593		    bsssize);
 594
 595	tsfdesc = pos + bsssize;
 596	tsfsize = 4 + 8 * scanresp->nr_sets;
 597	lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TSF", (u8 *) tsfdesc, tsfsize);
 598
 599	/* Validity check: we expect a Marvell-Local TLV */
 600	i = get_unaligned_le16(tsfdesc);
 601	tsfdesc += 2;
 602	if (i != TLV_TYPE_TSFTIMESTAMP) {
 603		lbs_deb_scan("scan response: invalid TSF Timestamp %d\n", i);
 604		goto done;
 605	}
 606
 607	/*
 608	 * Validity check: the TLV holds TSF values with 8 bytes each, so
 609	 * the size in the TLV must match the nr_sets value
 610	 */
 611	i = get_unaligned_le16(tsfdesc);
 612	tsfdesc += 2;
 613	if (i / 8 != scanresp->nr_sets) {
 614		lbs_deb_scan("scan response: invalid number of TSF timestamp "
 615			     "sets (expected %d got %d)\n", scanresp->nr_sets,
 616			     i / 8);
 617		goto done;
 618	}
 619
 620	for (i = 0; i < scanresp->nr_sets; i++) {
 621		const u8 *bssid;
 622		const u8 *ie;
 623		int left;
 624		int ielen;
 625		int rssi;
 626		u16 intvl;
 627		u16 capa;
 628		int chan_no = -1;
 629		const u8 *ssid = NULL;
 630		u8 ssid_len = 0;
 631
 632		int len = get_unaligned_le16(pos);
 633		pos += 2;
 634
 635		/* BSSID */
 636		bssid = pos;
 637		pos += ETH_ALEN;
 638		/* RSSI */
 639		rssi = *pos++;
 640		/* Packet time stamp */
 641		pos += 8;
 642		/* Beacon interval */
 643		intvl = get_unaligned_le16(pos);
 644		pos += 2;
 645		/* Capabilities */
 646		capa = get_unaligned_le16(pos);
 647		pos += 2;
 648
 649		/* To find out the channel, we must parse the IEs */
 650		ie = pos;
 651		/*
 652		 * 6+1+8+2+2: size of BSSID, RSSI, time stamp, beacon
 653		 * interval, capabilities
 654		 */
 655		ielen = left = len - (6 + 1 + 8 + 2 + 2);
 656		while (left >= 2) {
 657			u8 id, elen;
 658			id = *pos++;
 659			elen = *pos++;
 660			left -= 2;
 661			if (elen > left) {
 662				lbs_deb_scan("scan response: invalid IE fmt\n");
 663				goto done;
 664			}
 665
 666			if (id == WLAN_EID_DS_PARAMS)
 667				chan_no = *pos;
 668			if (id == WLAN_EID_SSID) {
 669				ssid = pos;
 670				ssid_len = elen;
 671			}
 672			left -= elen;
 673			pos += elen;
 674		}
 675
 676		/* No channel, no luck */
 677		if (chan_no != -1) {
 678			struct wiphy *wiphy = priv->wdev->wiphy;
 679			int freq = ieee80211_channel_to_frequency(chan_no,
 680							NL80211_BAND_2GHZ);
 681			struct ieee80211_channel *channel =
 682				ieee80211_get_channel(wiphy, freq);
 683
 684			lbs_deb_scan("scan: %pM, capa %04x, chan %2d, %*pE, %d dBm\n",
 685				     bssid, capa, chan_no, ssid_len, ssid,
 686				     LBS_SCAN_RSSI_TO_MBM(rssi)/100);
 687
 688			if (channel &&
 689			    !(channel->flags & IEEE80211_CHAN_DISABLED)) {
 690				bss = cfg80211_inform_bss(wiphy, channel,
 691					CFG80211_BSS_FTYPE_UNKNOWN,
 692					bssid, get_unaligned_le64(tsfdesc),
 693					capa, intvl, ie, ielen,
 694					LBS_SCAN_RSSI_TO_MBM(rssi),
 695					GFP_KERNEL);
 696				cfg80211_put_bss(wiphy, bss);
 697			}
 698		} else
 699			lbs_deb_scan("scan response: missing BSS channel IE\n");
 700
 701		tsfdesc += 8;
 702	}
 703	ret = 0;
 704
 705 done:
 706	return ret;
 707}
 708
 709
 710/*
 711 * Our scan command contains a TLV, consisting of a SSID TLV, a channel list
 712 * TLV, a rates TLV, and an optional WPS IE. Determine the maximum size of them:
 713 */
 714#define LBS_SCAN_MAX_CMD_SIZE			\
 715	(sizeof(struct cmd_ds_802_11_scan)	\
 716	 + LBS_MAX_SSID_TLV_SIZE		\
 717	 + LBS_MAX_CHANNEL_LIST_TLV_SIZE	\
 718	 + LBS_MAX_RATES_TLV_SIZE		\
 719	 + LBS_MAX_WPS_ENROLLEE_TLV_SIZE)
 720
 721/*
 722 * Assumes priv->scan_req is initialized and valid
 723 * Assumes priv->scan_channel is initialized
 724 */
 725static void lbs_scan_worker(struct work_struct *work)
 726{
 727	struct lbs_private *priv =
 728		container_of(work, struct lbs_private, scan_work.work);
 729	struct cmd_ds_802_11_scan *scan_cmd;
 730	u8 *tlv; /* pointer into our current, growing TLV storage area */
 731	int last_channel;
 732	int running, carrier;
 733
 734	scan_cmd = kzalloc(LBS_SCAN_MAX_CMD_SIZE, GFP_KERNEL);
 735	if (scan_cmd == NULL)
 736		return;
 737
 738	/* prepare fixed part of scan command */
 739	scan_cmd->bsstype = CMD_BSS_TYPE_ANY;
 740
 741	/* stop network while we're away from our main channel */
 742	running = !netif_queue_stopped(priv->dev);
 743	carrier = netif_carrier_ok(priv->dev);
 744	if (running)
 745		netif_stop_queue(priv->dev);
 746	if (carrier)
 747		netif_carrier_off(priv->dev);
 748
 749	/* prepare fixed part of scan command */
 750	tlv = scan_cmd->tlvbuffer;
 751
 752	/* add SSID TLV */
 753	if (priv->scan_req->n_ssids && priv->scan_req->ssids[0].ssid_len > 0)
 754		tlv += lbs_add_ssid_tlv(tlv,
 755					priv->scan_req->ssids[0].ssid,
 756					priv->scan_req->ssids[0].ssid_len);
 757
 758	/* add channel TLVs */
 759	last_channel = priv->scan_channel + LBS_SCAN_BEFORE_NAP;
 760	if (last_channel > priv->scan_req->n_channels)
 761		last_channel = priv->scan_req->n_channels;
 762	tlv += lbs_add_channel_list_tlv(priv, tlv, last_channel,
 763		priv->scan_req->n_ssids);
 764
 765	/* add rates TLV */
 766	tlv += lbs_add_supported_rates_tlv(tlv);
 767
 768	/* add optional WPS enrollee TLV */
 769	if (priv->scan_req->ie && priv->scan_req->ie_len)
 770		tlv += lbs_add_wps_enrollee_tlv(tlv, priv->scan_req->ie,
 771						priv->scan_req->ie_len);
 772
 773	if (priv->scan_channel < priv->scan_req->n_channels) {
 774		cancel_delayed_work(&priv->scan_work);
 775		if (netif_running(priv->dev))
 776			queue_delayed_work(priv->work_thread, &priv->scan_work,
 777				msecs_to_jiffies(300));
 778	}
 779
 780	/* This is the final data we are about to send */
 781	scan_cmd->hdr.size = cpu_to_le16(tlv - (u8 *)scan_cmd);
 782	lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
 783		    sizeof(*scan_cmd));
 784	lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
 785		    tlv - scan_cmd->tlvbuffer);
 786
 787	__lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
 788		le16_to_cpu(scan_cmd->hdr.size),
 789		lbs_ret_scan, 0);
 790
 791	if (priv->scan_channel >= priv->scan_req->n_channels) {
 792		/* Mark scan done */
 793		cancel_delayed_work(&priv->scan_work);
 794		lbs_scan_done(priv);
 795	}
 796
 797	/* Restart network */
 798	if (carrier)
 799		netif_carrier_on(priv->dev);
 800	if (running && !priv->tx_pending_len)
 801		netif_wake_queue(priv->dev);
 802
 803	kfree(scan_cmd);
 804
 805	/* Wake up anything waiting on scan completion */
 806	if (priv->scan_req == NULL) {
 807		lbs_deb_scan("scan: waking up waiters\n");
 808		wake_up_all(&priv->scan_q);
 809	}
 810}
 811
 812static void _internal_start_scan(struct lbs_private *priv, bool internal,
 813	struct cfg80211_scan_request *request)
 814{
 815	lbs_deb_scan("scan: ssids %d, channels %d, ie_len %zd\n",
 816		request->n_ssids, request->n_channels, request->ie_len);
 817
 818	priv->scan_channel = 0;
 819	priv->scan_req = request;
 820	priv->internal_scan = internal;
 821
 822	queue_delayed_work(priv->work_thread, &priv->scan_work,
 823		msecs_to_jiffies(50));
 824}
 825
 826/*
 827 * Clean up priv->scan_req.  Should be used to handle the allocation details.
 828 */
 829void lbs_scan_done(struct lbs_private *priv)
 830{
 831	WARN_ON(!priv->scan_req);
 832
 833	if (priv->internal_scan) {
 834		kfree(priv->scan_req);
 835	} else {
 836		struct cfg80211_scan_info info = {
 837			.aborted = false,
 838		};
 839
 840		cfg80211_scan_done(priv->scan_req, &info);
 841	}
 842
 843	priv->scan_req = NULL;
 844}
 845
 846static int lbs_cfg_scan(struct wiphy *wiphy,
 847	struct cfg80211_scan_request *request)
 848{
 849	struct lbs_private *priv = wiphy_priv(wiphy);
 850	int ret = 0;
 851
 852	if (priv->scan_req || delayed_work_pending(&priv->scan_work)) {
 853		/* old scan request not yet processed */
 854		ret = -EAGAIN;
 855		goto out;
 856	}
 857
 858	_internal_start_scan(priv, false, request);
 859
 860	if (priv->surpriseremoved)
 861		ret = -EIO;
 862
 863 out:
 864	return ret;
 865}
 866
 867
 868
 869
 870/*
 871 * Events
 872 */
 873
 874void lbs_send_disconnect_notification(struct lbs_private *priv,
 875				      bool locally_generated)
 876{
 877	cfg80211_disconnected(priv->dev, 0, NULL, 0, locally_generated,
 878			      GFP_KERNEL);
 879}
 880
 881void lbs_send_mic_failureevent(struct lbs_private *priv, u32 event)
 882{
 883	cfg80211_michael_mic_failure(priv->dev,
 884		priv->assoc_bss,
 885		event == MACREG_INT_CODE_MIC_ERR_MULTICAST ?
 886			NL80211_KEYTYPE_GROUP :
 887			NL80211_KEYTYPE_PAIRWISE,
 888		-1,
 889		NULL,
 890		GFP_KERNEL);
 891}
 892
 893
 894
 895
 896/*
 897 * Connect/disconnect
 898 */
 899
 900
 901/*
 902 * This removes all WEP keys
 903 */
 904static int lbs_remove_wep_keys(struct lbs_private *priv)
 905{
 906	struct cmd_ds_802_11_set_wep cmd;
 907	int ret;
 908
 909	memset(&cmd, 0, sizeof(cmd));
 910	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
 911	cmd.keyindex = cpu_to_le16(priv->wep_tx_key);
 912	cmd.action = cpu_to_le16(CMD_ACT_REMOVE);
 913
 914	ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
 915
 916	return ret;
 917}
 918
 919/*
 920 * Set WEP keys
 921 */
 922static int lbs_set_wep_keys(struct lbs_private *priv)
 923{
 924	struct cmd_ds_802_11_set_wep cmd;
 925	int i;
 926	int ret;
 927
 928	/*
 929	 * command         13 00
 930	 * size            50 00
 931	 * sequence        xx xx
 932	 * result          00 00
 933	 * action          02 00     ACT_ADD
 934	 * transmit key    00 00
 935	 * type for key 1  01        WEP40
 936	 * type for key 2  00
 937	 * type for key 3  00
 938	 * type for key 4  00
 939	 * key 1           39 39 39 39 39 00 00 00
 940	 *                 00 00 00 00 00 00 00 00
 941	 * key 2           00 00 00 00 00 00 00 00
 942	 *                 00 00 00 00 00 00 00 00
 943	 * key 3           00 00 00 00 00 00 00 00
 944	 *                 00 00 00 00 00 00 00 00
 945	 * key 4           00 00 00 00 00 00 00 00
 946	 */
 947	if (priv->wep_key_len[0] || priv->wep_key_len[1] ||
 948	    priv->wep_key_len[2] || priv->wep_key_len[3]) {
 949		/* Only set wep keys if we have at least one of them */
 950		memset(&cmd, 0, sizeof(cmd));
 951		cmd.hdr.size = cpu_to_le16(sizeof(cmd));
 952		cmd.keyindex = cpu_to_le16(priv->wep_tx_key);
 953		cmd.action = cpu_to_le16(CMD_ACT_ADD);
 954
 955		for (i = 0; i < 4; i++) {
 956			switch (priv->wep_key_len[i]) {
 957			case WLAN_KEY_LEN_WEP40:
 958				cmd.keytype[i] = CMD_TYPE_WEP_40_BIT;
 959				break;
 960			case WLAN_KEY_LEN_WEP104:
 961				cmd.keytype[i] = CMD_TYPE_WEP_104_BIT;
 962				break;
 963			default:
 964				cmd.keytype[i] = 0;
 965				break;
 966			}
 967			memcpy(cmd.keymaterial[i], priv->wep_key[i],
 968			       priv->wep_key_len[i]);
 969		}
 970
 971		ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
 972	} else {
 973		/* Otherwise remove all wep keys */
 974		ret = lbs_remove_wep_keys(priv);
 975	}
 976
 977	return ret;
 978}
 979
 980
 981/*
 982 * Enable/Disable RSN status
 983 */
 984static int lbs_enable_rsn(struct lbs_private *priv, int enable)
 985{
 986	struct cmd_ds_802_11_enable_rsn cmd;
 987	int ret;
 988
 989	/*
 990	 * cmd       2f 00
 991	 * size      0c 00
 992	 * sequence  xx xx
 993	 * result    00 00
 994	 * action    01 00    ACT_SET
 995	 * enable    01 00
 996	 */
 997	memset(&cmd, 0, sizeof(cmd));
 998	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
 999	cmd.action = cpu_to_le16(CMD_ACT_SET);
1000	cmd.enable = cpu_to_le16(enable);
1001
1002	ret = lbs_cmd_with_response(priv, CMD_802_11_ENABLE_RSN, &cmd);
1003
1004	return ret;
1005}
1006
1007
1008/*
1009 * Set WPA/WPA key material
1010 */
1011
1012/*
1013 * like "struct cmd_ds_802_11_key_material", but with cmd_header. Once we
1014 * get rid of WEXT, this should go into host.h
1015 */
1016
1017struct cmd_key_material {
1018	struct cmd_header hdr;
1019
1020	__le16 action;
1021	struct MrvlIEtype_keyParamSet param;
1022} __packed;
1023
1024static int lbs_set_key_material(struct lbs_private *priv,
1025				int key_type, int key_info,
1026				const u8 *key, u16 key_len)
1027{
1028	struct cmd_key_material cmd;
1029	int ret;
1030
1031	/*
1032	 * Example for WPA (TKIP):
1033	 *
1034	 * cmd       5e 00
1035	 * size      34 00
1036	 * sequence  xx xx
1037	 * result    00 00
1038	 * action    01 00
1039	 * TLV type  00 01    key param
1040	 * length    00 26
1041	 * key type  01 00    TKIP
1042	 * key info  06 00    UNICAST | ENABLED
1043	 * key len   20 00
1044	 * key       32 bytes
1045	 */
1046	memset(&cmd, 0, sizeof(cmd));
1047	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1048	cmd.action = cpu_to_le16(CMD_ACT_SET);
1049	cmd.param.type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
1050	cmd.param.length = cpu_to_le16(sizeof(cmd.param) - 4);
1051	cmd.param.keytypeid = cpu_to_le16(key_type);
1052	cmd.param.keyinfo = cpu_to_le16(key_info);
1053	cmd.param.keylen = cpu_to_le16(key_len);
1054	if (key && key_len)
1055		memcpy(cmd.param.key, key, key_len);
1056
1057	ret = lbs_cmd_with_response(priv, CMD_802_11_KEY_MATERIAL, &cmd);
1058
1059	return ret;
1060}
1061
1062
1063/*
1064 * Sets the auth type (open, shared, etc) in the firmware. That
1065 * we use CMD_802_11_AUTHENTICATE is misleading, this firmware
1066 * command doesn't send an authentication frame at all, it just
1067 * stores the auth_type.
1068 */
1069static int lbs_set_authtype(struct lbs_private *priv,
1070			    struct cfg80211_connect_params *sme)
1071{
1072	struct cmd_ds_802_11_authenticate cmd;
1073	int ret;
1074
1075	/*
1076	 * cmd        11 00
1077	 * size       19 00
1078	 * sequence   xx xx
1079	 * result     00 00
1080	 * BSS id     00 13 19 80 da 30
1081	 * auth type  00
1082	 * reserved   00 00 00 00 00 00 00 00 00 00
1083	 */
1084	memset(&cmd, 0, sizeof(cmd));
1085	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1086	if (sme->bssid)
1087		memcpy(cmd.bssid, sme->bssid, ETH_ALEN);
1088	/* convert auth_type */
1089	ret = lbs_auth_to_authtype(sme->auth_type);
1090	if (ret < 0)
1091		goto done;
1092
1093	cmd.authtype = ret;
1094	ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
1095
1096 done:
1097	return ret;
1098}
1099
1100
1101/*
1102 * Create association request
1103 */
1104#define LBS_ASSOC_MAX_CMD_SIZE                     \
1105	(sizeof(struct cmd_ds_802_11_associate)    \
1106	 + LBS_MAX_SSID_TLV_SIZE                   \
1107	 + LBS_MAX_CHANNEL_TLV_SIZE                \
1108	 + LBS_MAX_CF_PARAM_TLV_SIZE               \
1109	 + LBS_MAX_AUTH_TYPE_TLV_SIZE              \
1110	 + LBS_MAX_WPA_TLV_SIZE)
1111
1112static int lbs_associate(struct lbs_private *priv,
1113		struct cfg80211_bss *bss,
1114		struct cfg80211_connect_params *sme)
1115{
1116	struct cmd_ds_802_11_associate_response *resp;
1117	struct cmd_ds_802_11_associate *cmd = kzalloc(LBS_ASSOC_MAX_CMD_SIZE,
1118						      GFP_KERNEL);
1119	const u8 *ssid_eid;
1120	size_t len, resp_ie_len;
1121	int status;
1122	int ret;
1123	u8 *pos;
1124	u8 *tmp;
1125
1126	if (!cmd) {
1127		ret = -ENOMEM;
1128		goto done;
1129	}
1130	pos = &cmd->iebuf[0];
1131
1132	/*
1133	 * cmd              50 00
1134	 * length           34 00
1135	 * sequence         xx xx
1136	 * result           00 00
1137	 * BSS id           00 13 19 80 da 30
1138	 * capabilities     11 00
1139	 * listen interval  0a 00
1140	 * beacon interval  00 00
1141	 * DTIM period      00
1142	 * TLVs             xx   (up to 512 bytes)
1143	 */
1144	cmd->hdr.command = cpu_to_le16(CMD_802_11_ASSOCIATE);
1145
1146	/* Fill in static fields */
1147	memcpy(cmd->bssid, bss->bssid, ETH_ALEN);
1148	cmd->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
1149	cmd->capability = cpu_to_le16(bss->capability);
1150
1151	/* add SSID TLV */
1152	rcu_read_lock();
1153	ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
1154	if (ssid_eid) {
1155		u32 ssid_len = min(ssid_eid[1], IEEE80211_MAX_SSID_LEN);
1156
1157		pos += lbs_add_ssid_tlv(pos, ssid_eid + 2, ssid_len);
1158	} else {
1159		lbs_deb_assoc("no SSID\n");
1160	}
1161	rcu_read_unlock();
1162
1163	/* add DS param TLV */
1164	if (bss->channel)
1165		pos += lbs_add_channel_tlv(pos, bss->channel->hw_value);
1166	else
1167		lbs_deb_assoc("no channel\n");
1168
1169	/* add (empty) CF param TLV */
1170	pos += lbs_add_cf_param_tlv(pos);
1171
1172	/* add rates TLV */
1173	tmp = pos + 4; /* skip Marvell IE header */
1174	pos += lbs_add_common_rates_tlv(pos, bss);
1175	lbs_deb_hex(LBS_DEB_ASSOC, "Common Rates", tmp, pos - tmp);
1176
1177	/* add auth type TLV */
1178	if (MRVL_FW_MAJOR_REV(priv->fwrelease) >= 9)
1179		pos += lbs_add_auth_type_tlv(pos, sme->auth_type);
1180
1181	/* add WPA/WPA2 TLV */
1182	if (sme->ie && sme->ie_len)
1183		pos += lbs_add_wpa_tlv(pos, sme->ie, sme->ie_len);
1184
1185	len = sizeof(*cmd) + (u16)(pos - (u8 *) &cmd->iebuf);
1186	cmd->hdr.size = cpu_to_le16(len);
1187
1188	lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_CMD", (u8 *) cmd,
1189			le16_to_cpu(cmd->hdr.size));
1190
1191	/* store for later use */
1192	memcpy(priv->assoc_bss, bss->bssid, ETH_ALEN);
1193
1194	ret = lbs_cmd_with_response(priv, CMD_802_11_ASSOCIATE, cmd);
1195	if (ret)
1196		goto done;
1197
1198	/* generate connect message to cfg80211 */
1199
1200	resp = (void *) cmd; /* recast for easier field access */
1201	status = le16_to_cpu(resp->statuscode);
1202
1203	/* Older FW versions map the IEEE 802.11 Status Code in the association
1204	 * response to the following values returned in resp->statuscode:
1205	 *
1206	 *    IEEE Status Code                Marvell Status Code
1207	 *    0                       ->      0x0000 ASSOC_RESULT_SUCCESS
1208	 *    13                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
1209	 *    14                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
1210	 *    15                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
1211	 *    16                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
1212	 *    others                  ->      0x0003 ASSOC_RESULT_REFUSED
1213	 *
1214	 * Other response codes:
1215	 *    0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
1216	 *    0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
1217	 *                                    association response from the AP)
1218	 */
1219	if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8) {
1220		switch (status) {
1221		case 0:
1222			break;
1223		case 1:
1224			lbs_deb_assoc("invalid association parameters\n");
1225			status = WLAN_STATUS_CAPS_UNSUPPORTED;
1226			break;
1227		case 2:
1228			lbs_deb_assoc("timer expired while waiting for AP\n");
1229			status = WLAN_STATUS_AUTH_TIMEOUT;
1230			break;
1231		case 3:
1232			lbs_deb_assoc("association refused by AP\n");
1233			status = WLAN_STATUS_ASSOC_DENIED_UNSPEC;
1234			break;
1235		case 4:
1236			lbs_deb_assoc("authentication refused by AP\n");
1237			status = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
1238			break;
1239		default:
1240			lbs_deb_assoc("association failure %d\n", status);
1241			/* v5 OLPC firmware does return the AP status code if
1242			 * it's not one of the values above.  Let that through.
1243			 */
1244			break;
1245		}
1246	}
1247
1248	lbs_deb_assoc("status %d, statuscode 0x%04x, capability 0x%04x, "
1249		      "aid 0x%04x\n", status, le16_to_cpu(resp->statuscode),
1250		      le16_to_cpu(resp->capability), le16_to_cpu(resp->aid));
1251
1252	resp_ie_len = le16_to_cpu(resp->hdr.size)
1253		- sizeof(resp->hdr)
1254		- 6;
1255	cfg80211_connect_result(priv->dev,
1256				priv->assoc_bss,
1257				sme->ie, sme->ie_len,
1258				resp->iebuf, resp_ie_len,
1259				status,
1260				GFP_KERNEL);
1261
1262	if (status == 0) {
1263		/* TODO: get rid of priv->connect_status */
1264		priv->connect_status = LBS_CONNECTED;
1265		netif_carrier_on(priv->dev);
1266		if (!priv->tx_pending_len)
1267			netif_tx_wake_all_queues(priv->dev);
1268	}
1269
1270	kfree(cmd);
1271done:
1272	return ret;
1273}
1274
1275static struct cfg80211_scan_request *
1276_new_connect_scan_req(struct wiphy *wiphy, struct cfg80211_connect_params *sme)
1277{
1278	struct cfg80211_scan_request *creq = NULL;
1279	int i, n_channels = ieee80211_get_num_supported_channels(wiphy);
1280	enum nl80211_band band;
1281
1282	creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
1283		       n_channels * sizeof(void *),
1284		       GFP_ATOMIC);
1285	if (!creq)
1286		return NULL;
1287
1288	/* SSIDs come after channels */
1289	creq->ssids = (void *)&creq->channels[n_channels];
1290	creq->n_channels = n_channels;
1291	creq->n_ssids = 1;
1292
1293	/* Scan all available channels */
1294	i = 0;
1295	for (band = 0; band < NUM_NL80211_BANDS; band++) {
1296		int j;
1297
1298		if (!wiphy->bands[band])
1299			continue;
1300
1301		for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
1302			/* ignore disabled channels */
1303			if (wiphy->bands[band]->channels[j].flags &
1304						IEEE80211_CHAN_DISABLED)
1305				continue;
1306
1307			creq->channels[i] = &wiphy->bands[band]->channels[j];
1308			i++;
1309		}
1310	}
1311	if (i) {
1312		/* Set real number of channels specified in creq->channels[] */
1313		creq->n_channels = i;
1314
1315		/* Scan for the SSID we're going to connect to */
1316		memcpy(creq->ssids[0].ssid, sme->ssid, sme->ssid_len);
1317		creq->ssids[0].ssid_len = sme->ssid_len;
1318	} else {
1319		/* No channels found... */
1320		kfree(creq);
1321		creq = NULL;
1322	}
1323
1324	return creq;
1325}
1326
1327static int lbs_cfg_connect(struct wiphy *wiphy, struct net_device *dev,
1328			   struct cfg80211_connect_params *sme)
1329{
1330	struct lbs_private *priv = wiphy_priv(wiphy);
1331	struct cfg80211_bss *bss = NULL;
1332	int ret = 0;
1333	u8 preamble = RADIO_PREAMBLE_SHORT;
1334
1335	if (dev == priv->mesh_dev)
1336		return -EOPNOTSUPP;
1337
1338	if (!sme->bssid) {
1339		struct cfg80211_scan_request *creq;
1340
1341		/*
1342		 * Scan for the requested network after waiting for existing
1343		 * scans to finish.
1344		 */
1345		lbs_deb_assoc("assoc: waiting for existing scans\n");
1346		wait_event_interruptible_timeout(priv->scan_q,
1347						 (priv->scan_req == NULL),
1348						 (15 * HZ));
1349
1350		creq = _new_connect_scan_req(wiphy, sme);
1351		if (!creq) {
1352			ret = -EINVAL;
1353			goto done;
1354		}
1355
1356		lbs_deb_assoc("assoc: scanning for compatible AP\n");
1357		_internal_start_scan(priv, true, creq);
1358
1359		lbs_deb_assoc("assoc: waiting for scan to complete\n");
1360		wait_event_interruptible_timeout(priv->scan_q,
1361						 (priv->scan_req == NULL),
1362						 (15 * HZ));
1363		lbs_deb_assoc("assoc: scanning completed\n");
1364	}
1365
1366	/* Find the BSS we want using available scan results */
1367	bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
1368		sme->ssid, sme->ssid_len, IEEE80211_BSS_TYPE_ESS,
1369		IEEE80211_PRIVACY_ANY);
1370	if (!bss) {
1371		wiphy_err(wiphy, "assoc: bss %pM not in scan results\n",
1372			  sme->bssid);
1373		ret = -ENOENT;
1374		goto done;
1375	}
1376	lbs_deb_assoc("trying %pM\n", bss->bssid);
1377	lbs_deb_assoc("cipher 0x%x, key index %d, key len %d\n",
1378		      sme->crypto.cipher_group,
1379		      sme->key_idx, sme->key_len);
1380
1381	/* As this is a new connection, clear locally stored WEP keys */
1382	priv->wep_tx_key = 0;
1383	memset(priv->wep_key, 0, sizeof(priv->wep_key));
1384	memset(priv->wep_key_len, 0, sizeof(priv->wep_key_len));
1385
1386	/* set/remove WEP keys */
1387	switch (sme->crypto.cipher_group) {
1388	case WLAN_CIPHER_SUITE_WEP40:
1389	case WLAN_CIPHER_SUITE_WEP104:
1390		/* Store provided WEP keys in priv-> */
1391		priv->wep_tx_key = sme->key_idx;
1392		priv->wep_key_len[sme->key_idx] = sme->key_len;
1393		memcpy(priv->wep_key[sme->key_idx], sme->key, sme->key_len);
1394		/* Set WEP keys and WEP mode */
1395		lbs_set_wep_keys(priv);
1396		priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
1397		lbs_set_mac_control(priv);
1398		/* No RSN mode for WEP */
1399		lbs_enable_rsn(priv, 0);
1400		break;
1401	case 0: /* there's no WLAN_CIPHER_SUITE_NONE definition */
1402		/*
1403		 * If we don't have no WEP, no WPA and no WPA2,
1404		 * we remove all keys like in the WPA/WPA2 setup,
1405		 * we just don't set RSN.
1406		 *
1407		 * Therefore: fall-through
1408		 */
1409	case WLAN_CIPHER_SUITE_TKIP:
1410	case WLAN_CIPHER_SUITE_CCMP:
1411		/* Remove WEP keys and WEP mode */
1412		lbs_remove_wep_keys(priv);
1413		priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
1414		lbs_set_mac_control(priv);
1415
1416		/* clear the WPA/WPA2 keys */
1417		lbs_set_key_material(priv,
1418			KEY_TYPE_ID_WEP, /* doesn't matter */
1419			KEY_INFO_WPA_UNICAST,
1420			NULL, 0);
1421		lbs_set_key_material(priv,
1422			KEY_TYPE_ID_WEP, /* doesn't matter */
1423			KEY_INFO_WPA_MCAST,
1424			NULL, 0);
1425		/* RSN mode for WPA/WPA2 */
1426		lbs_enable_rsn(priv, sme->crypto.cipher_group != 0);
1427		break;
1428	default:
1429		wiphy_err(wiphy, "unsupported cipher group 0x%x\n",
1430			  sme->crypto.cipher_group);
1431		ret = -ENOTSUPP;
1432		goto done;
1433	}
1434
1435	ret = lbs_set_authtype(priv, sme);
1436	if (ret == -ENOTSUPP) {
1437		wiphy_err(wiphy, "unsupported authtype 0x%x\n", sme->auth_type);
1438		goto done;
1439	}
1440
1441	lbs_set_radio(priv, preamble, 1);
1442
1443	/* Do the actual association */
1444	ret = lbs_associate(priv, bss, sme);
1445
1446 done:
1447	if (bss)
1448		cfg80211_put_bss(wiphy, bss);
1449	return ret;
1450}
1451
1452int lbs_disconnect(struct lbs_private *priv, u16 reason)
1453{
1454	struct cmd_ds_802_11_deauthenticate cmd;
1455	int ret;
1456
1457	memset(&cmd, 0, sizeof(cmd));
1458	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1459	/* Mildly ugly to use a locally store my own BSSID ... */
1460	memcpy(cmd.macaddr, &priv->assoc_bss, ETH_ALEN);
1461	cmd.reasoncode = cpu_to_le16(reason);
1462
1463	ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
1464	if (ret)
1465		return ret;
1466
1467	cfg80211_disconnected(priv->dev,
1468			reason,
1469			NULL, 0, true,
1470			GFP_KERNEL);
1471	priv->connect_status = LBS_DISCONNECTED;
1472
1473	return 0;
1474}
1475
1476static int lbs_cfg_disconnect(struct wiphy *wiphy, struct net_device *dev,
1477	u16 reason_code)
1478{
1479	struct lbs_private *priv = wiphy_priv(wiphy);
1480
1481	if (dev == priv->mesh_dev)
1482		return -EOPNOTSUPP;
1483
1484	/* store for lbs_cfg_ret_disconnect() */
1485	priv->disassoc_reason = reason_code;
1486
1487	return lbs_disconnect(priv, reason_code);
1488}
1489
1490static int lbs_cfg_set_default_key(struct wiphy *wiphy,
1491				   struct net_device *netdev, int link_id,
1492				   u8 key_index, bool unicast,
1493				   bool multicast)
1494{
1495	struct lbs_private *priv = wiphy_priv(wiphy);
1496
1497	if (netdev == priv->mesh_dev)
1498		return -EOPNOTSUPP;
1499
1500	if (key_index != priv->wep_tx_key) {
1501		lbs_deb_assoc("set_default_key: to %d\n", key_index);
1502		priv->wep_tx_key = key_index;
1503		lbs_set_wep_keys(priv);
1504	}
1505
1506	return 0;
1507}
1508
1509
1510static int lbs_cfg_add_key(struct wiphy *wiphy, struct net_device *netdev,
1511			   int link_id, u8 idx, bool pairwise,
1512			   const u8 *mac_addr, struct key_params *params)
1513{
1514	struct lbs_private *priv = wiphy_priv(wiphy);
1515	u16 key_info;
1516	u16 key_type;
1517	int ret = 0;
1518
1519	if (netdev == priv->mesh_dev)
1520		return -EOPNOTSUPP;
1521
1522	lbs_deb_assoc("add_key: cipher 0x%x, mac_addr %pM\n",
1523		      params->cipher, mac_addr);
1524	lbs_deb_assoc("add_key: key index %d, key len %d\n",
1525		      idx, params->key_len);
1526	if (params->key_len)
1527		lbs_deb_hex(LBS_DEB_CFG80211, "KEY",
1528			    params->key, params->key_len);
1529
1530	lbs_deb_assoc("add_key: seq len %d\n", params->seq_len);
1531	if (params->seq_len)
1532		lbs_deb_hex(LBS_DEB_CFG80211, "SEQ",
1533			    params->seq, params->seq_len);
1534
1535	switch (params->cipher) {
1536	case WLAN_CIPHER_SUITE_WEP40:
1537	case WLAN_CIPHER_SUITE_WEP104:
1538		/* actually compare if something has changed ... */
1539		if ((priv->wep_key_len[idx] != params->key_len) ||
1540			memcmp(priv->wep_key[idx],
1541			       params->key, params->key_len) != 0) {
1542			priv->wep_key_len[idx] = params->key_len;
1543			memcpy(priv->wep_key[idx],
1544			       params->key, params->key_len);
1545			lbs_set_wep_keys(priv);
1546		}
1547		break;
1548	case WLAN_CIPHER_SUITE_TKIP:
1549	case WLAN_CIPHER_SUITE_CCMP:
1550		key_info = KEY_INFO_WPA_ENABLED | ((idx == 0)
1551						   ? KEY_INFO_WPA_UNICAST
1552						   : KEY_INFO_WPA_MCAST);
1553		key_type = (params->cipher == WLAN_CIPHER_SUITE_TKIP)
1554			? KEY_TYPE_ID_TKIP
1555			: KEY_TYPE_ID_AES;
1556		lbs_set_key_material(priv,
1557				     key_type,
1558				     key_info,
1559				     params->key, params->key_len);
1560		break;
1561	default:
1562		wiphy_err(wiphy, "unhandled cipher 0x%x\n", params->cipher);
1563		ret = -ENOTSUPP;
1564		break;
1565	}
1566
1567	return ret;
1568}
1569
1570
1571static int lbs_cfg_del_key(struct wiphy *wiphy, struct net_device *netdev,
1572			   int link_id, u8 key_index, bool pairwise,
1573			   const u8 *mac_addr)
1574{
1575
1576	lbs_deb_assoc("del_key: key_idx %d, mac_addr %pM\n",
1577		      key_index, mac_addr);
1578
1579#ifdef TODO
1580	struct lbs_private *priv = wiphy_priv(wiphy);
1581	/*
1582	 * I think can keep this a NO-OP, because:
1583
1584	 * - we clear all keys whenever we do lbs_cfg_connect() anyway
1585	 * - neither "iw" nor "wpa_supplicant" won't call this during
1586	 *   an ongoing connection
1587	 * - TODO: but I have to check if this is still true when
1588	 *   I set the AP to periodic re-keying
1589	 * - we've not kzallec() something when we've added a key at
1590	 *   lbs_cfg_connect() or lbs_cfg_add_key().
1591	 *
1592	 * This causes lbs_cfg_del_key() only called at disconnect time,
1593	 * where we'd just waste time deleting a key that is not going
1594	 * to be used anyway.
1595	 */
1596	if (key_index < 3 && priv->wep_key_len[key_index]) {
1597		priv->wep_key_len[key_index] = 0;
1598		lbs_set_wep_keys(priv);
1599	}
1600#endif
1601
1602	return 0;
1603}
1604
1605
1606/*
1607 * Get station
1608 */
1609
1610static int lbs_cfg_get_station(struct wiphy *wiphy, struct net_device *dev,
1611			       const u8 *mac, struct station_info *sinfo)
1612{
1613	struct lbs_private *priv = wiphy_priv(wiphy);
1614	s8 signal, noise;
1615	int ret;
1616	size_t i;
1617
1618	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
1619			 BIT_ULL(NL80211_STA_INFO_TX_PACKETS) |
1620			 BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
1621			 BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
1622	sinfo->tx_bytes = priv->dev->stats.tx_bytes;
1623	sinfo->tx_packets = priv->dev->stats.tx_packets;
1624	sinfo->rx_bytes = priv->dev->stats.rx_bytes;
1625	sinfo->rx_packets = priv->dev->stats.rx_packets;
1626
1627	/* Get current RSSI */
1628	ret = lbs_get_rssi(priv, &signal, &noise);
1629	if (ret == 0) {
1630		sinfo->signal = signal;
1631		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
1632	}
1633
1634	/* Convert priv->cur_rate from hw_value to NL80211 value */
1635	for (i = 0; i < ARRAY_SIZE(lbs_rates); i++) {
1636		if (priv->cur_rate == lbs_rates[i].hw_value) {
1637			sinfo->txrate.legacy = lbs_rates[i].bitrate;
1638			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1639			break;
1640		}
1641	}
1642
1643	return 0;
1644}
1645
1646
1647
1648
1649/*
1650 * Change interface
1651 */
1652
1653static int lbs_change_intf(struct wiphy *wiphy, struct net_device *dev,
1654	enum nl80211_iftype type,
1655	       struct vif_params *params)
1656{
1657	struct lbs_private *priv = wiphy_priv(wiphy);
1658	int ret = 0;
1659
1660	if (dev == priv->mesh_dev)
1661		return -EOPNOTSUPP;
1662
1663	switch (type) {
1664	case NL80211_IFTYPE_MONITOR:
1665	case NL80211_IFTYPE_STATION:
1666	case NL80211_IFTYPE_ADHOC:
1667		break;
1668	default:
1669		return -EOPNOTSUPP;
1670	}
1671
1672	if (priv->iface_running)
1673		ret = lbs_set_iface_type(priv, type);
1674
1675	if (!ret)
1676		priv->wdev->iftype = type;
1677
1678	return ret;
1679}
1680
1681
1682
1683/*
1684 * IBSS (Ad-Hoc)
1685 */
1686
1687/*
1688 * The firmware needs the following bits masked out of the beacon-derived
1689 * capability field when associating/joining to a BSS:
1690 *  9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
1691 */
1692#define CAPINFO_MASK (~(0xda00))
1693
1694
1695static void lbs_join_post(struct lbs_private *priv,
1696			  struct cfg80211_ibss_params *params,
1697			  u8 *bssid, u16 capability)
1698{
1699	u8 fake_ie[2 + IEEE80211_MAX_SSID_LEN + /* ssid */
1700		   2 + 4 +                      /* basic rates */
1701		   2 + 1 +                      /* DS parameter */
1702		   2 + 2 +                      /* atim */
1703		   2 + 8];                      /* extended rates */
1704	u8 *fake = fake_ie;
1705	struct cfg80211_bss *bss;
1706
1707	/*
1708	 * For cfg80211_inform_bss, we'll need a fake IE, as we can't get
1709	 * the real IE from the firmware. So we fabricate a fake IE based on
1710	 * what the firmware actually sends (sniffed with wireshark).
1711	 */
1712	/* Fake SSID IE */
1713	*fake++ = WLAN_EID_SSID;
1714	*fake++ = params->ssid_len;
1715	memcpy(fake, params->ssid, params->ssid_len);
1716	fake += params->ssid_len;
1717	/* Fake supported basic rates IE */
1718	*fake++ = WLAN_EID_SUPP_RATES;
1719	*fake++ = 4;
1720	*fake++ = 0x82;
1721	*fake++ = 0x84;
1722	*fake++ = 0x8b;
1723	*fake++ = 0x96;
1724	/* Fake DS channel IE */
1725	*fake++ = WLAN_EID_DS_PARAMS;
1726	*fake++ = 1;
1727	*fake++ = params->chandef.chan->hw_value;
1728	/* Fake IBSS params IE */
1729	*fake++ = WLAN_EID_IBSS_PARAMS;
1730	*fake++ = 2;
1731	*fake++ = 0; /* ATIM=0 */
1732	*fake++ = 0;
1733	/* Fake extended rates IE, TODO: don't add this for 802.11b only,
1734	 * but I don't know how this could be checked */
1735	*fake++ = WLAN_EID_EXT_SUPP_RATES;
1736	*fake++ = 8;
1737	*fake++ = 0x0c;
1738	*fake++ = 0x12;
1739	*fake++ = 0x18;
1740	*fake++ = 0x24;
1741	*fake++ = 0x30;
1742	*fake++ = 0x48;
1743	*fake++ = 0x60;
1744	*fake++ = 0x6c;
1745	lbs_deb_hex(LBS_DEB_CFG80211, "IE", fake_ie, fake - fake_ie);
1746
1747	bss = cfg80211_inform_bss(priv->wdev->wiphy,
1748				  params->chandef.chan,
1749				  CFG80211_BSS_FTYPE_UNKNOWN,
1750				  bssid,
1751				  0,
1752				  capability,
1753				  params->beacon_interval,
1754				  fake_ie, fake - fake_ie,
1755				  0, GFP_KERNEL);
1756	cfg80211_put_bss(priv->wdev->wiphy, bss);
1757
1758	cfg80211_ibss_joined(priv->dev, bssid, params->chandef.chan,
1759			     GFP_KERNEL);
1760
1761	/* TODO: consider doing this at MACREG_INT_CODE_LINK_SENSED time */
1762	priv->connect_status = LBS_CONNECTED;
1763	netif_carrier_on(priv->dev);
1764	if (!priv->tx_pending_len)
1765		netif_wake_queue(priv->dev);
1766}
1767
1768static int lbs_ibss_join_existing(struct lbs_private *priv,
1769	struct cfg80211_ibss_params *params,
1770	struct cfg80211_bss *bss)
1771{
1772	const u8 *rates_eid;
1773	struct cmd_ds_802_11_ad_hoc_join cmd;
1774	u8 preamble = RADIO_PREAMBLE_SHORT;
1775	int ret = 0;
1776	int hw, i;
1777	u8 rates_max;
1778	u8 *rates;
1779
1780	/* TODO: set preamble based on scan result */
1781	ret = lbs_set_radio(priv, preamble, 1);
1782	if (ret)
1783		goto out;
1784
1785	/*
1786	 * Example CMD_802_11_AD_HOC_JOIN command:
1787	 *
1788	 * command         2c 00         CMD_802_11_AD_HOC_JOIN
1789	 * size            65 00
1790	 * sequence        xx xx
1791	 * result          00 00
1792	 * bssid           02 27 27 97 2f 96
1793	 * ssid            49 42 53 53 00 00 00 00
1794	 *                 00 00 00 00 00 00 00 00
1795	 *                 00 00 00 00 00 00 00 00
1796	 *                 00 00 00 00 00 00 00 00
1797	 * type            02            CMD_BSS_TYPE_IBSS
1798	 * beacon period   64 00
1799	 * dtim period     00
1800	 * timestamp       00 00 00 00 00 00 00 00
1801	 * localtime       00 00 00 00 00 00 00 00
1802	 * IE DS           03
1803	 * IE DS len       01
1804	 * IE DS channel   01
1805	 * reserveed       00 00 00 00
1806	 * IE IBSS         06
1807	 * IE IBSS len     02
1808	 * IE IBSS atim    00 00
1809	 * reserved        00 00 00 00
1810	 * capability      02 00
1811	 * rates           82 84 8b 96 0c 12 18 24 30 48 60 6c 00
1812	 * fail timeout    ff 00
1813	 * probe delay     00 00
1814	 */
1815	memset(&cmd, 0, sizeof(cmd));
1816	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1817
1818	memcpy(cmd.bss.bssid, bss->bssid, ETH_ALEN);
1819	memcpy(cmd.bss.ssid, params->ssid, params->ssid_len);
1820	cmd.bss.type = CMD_BSS_TYPE_IBSS;
1821	cmd.bss.beaconperiod = cpu_to_le16(params->beacon_interval);
1822	cmd.bss.ds.header.id = WLAN_EID_DS_PARAMS;
1823	cmd.bss.ds.header.len = 1;
1824	cmd.bss.ds.channel = params->chandef.chan->hw_value;
1825	cmd.bss.ibss.header.id = WLAN_EID_IBSS_PARAMS;
1826	cmd.bss.ibss.header.len = 2;
1827	cmd.bss.ibss.atimwindow = 0;
1828	cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
1829
1830	/* set rates to the intersection of our rates and the rates in the
1831	   bss */
1832	rcu_read_lock();
1833	rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1834	if (!rates_eid) {
1835		lbs_add_rates(cmd.bss.rates);
1836	} else {
1837		rates_max = rates_eid[1];
1838		if (rates_max > MAX_RATES) {
1839			lbs_deb_join("invalid rates");
1840			rcu_read_unlock();
1841			ret = -EINVAL;
1842			goto out;
1843		}
1844		rates = cmd.bss.rates;
1845		for (hw = 0; hw < ARRAY_SIZE(lbs_rates); hw++) {
1846			u8 hw_rate = lbs_rates[hw].bitrate / 5;
1847			for (i = 0; i < rates_max; i++) {
1848				if (hw_rate == (rates_eid[i+2] & 0x7f)) {
1849					u8 rate = rates_eid[i+2];
1850					if (rate == 0x02 || rate == 0x04 ||
1851					    rate == 0x0b || rate == 0x16)
1852						rate |= 0x80;
1853					*rates++ = rate;
1854				}
1855			}
1856		}
1857	}
1858	rcu_read_unlock();
1859
1860	/* Only v8 and below support setting this */
1861	if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8) {
1862		cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
1863		cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1864	}
1865	ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
1866	if (ret)
1867		goto out;
1868
1869	/*
1870	 * This is a sample response to CMD_802_11_AD_HOC_JOIN:
1871	 *
1872	 * response        2c 80
1873	 * size            09 00
1874	 * sequence        xx xx
1875	 * result          00 00
1876	 * reserved        00
1877	 */
1878	lbs_join_post(priv, params, bss->bssid, bss->capability);
1879
1880 out:
1881	return ret;
1882}
1883
1884
1885
1886static int lbs_ibss_start_new(struct lbs_private *priv,
1887	struct cfg80211_ibss_params *params)
1888{
1889	struct cmd_ds_802_11_ad_hoc_start cmd;
1890	struct cmd_ds_802_11_ad_hoc_result *resp =
1891		(struct cmd_ds_802_11_ad_hoc_result *) &cmd;
1892	u8 preamble = RADIO_PREAMBLE_SHORT;
1893	int ret = 0;
1894	u16 capability;
1895
1896	ret = lbs_set_radio(priv, preamble, 1);
1897	if (ret)
1898		goto out;
1899
1900	/*
1901	 * Example CMD_802_11_AD_HOC_START command:
1902	 *
1903	 * command         2b 00         CMD_802_11_AD_HOC_START
1904	 * size            b1 00
1905	 * sequence        xx xx
1906	 * result          00 00
1907	 * ssid            54 45 53 54 00 00 00 00
1908	 *                 00 00 00 00 00 00 00 00
1909	 *                 00 00 00 00 00 00 00 00
1910	 *                 00 00 00 00 00 00 00 00
1911	 * bss type        02
1912	 * beacon period   64 00
1913	 * dtim period     00
1914	 * IE IBSS         06
1915	 * IE IBSS len     02
1916	 * IE IBSS atim    00 00
1917	 * reserved        00 00 00 00
1918	 * IE DS           03
1919	 * IE DS len       01
1920	 * IE DS channel   01
1921	 * reserved        00 00 00 00
1922	 * probe delay     00 00
1923	 * capability      02 00
1924	 * rates           82 84 8b 96   (basic rates with have bit 7 set)
1925	 *                 0c 12 18 24 30 48 60 6c
1926	 * padding         100 bytes
1927	 */
1928	memset(&cmd, 0, sizeof(cmd));
1929	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1930	memcpy(cmd.ssid, params->ssid, params->ssid_len);
1931	cmd.bsstype = CMD_BSS_TYPE_IBSS;
1932	cmd.beaconperiod = cpu_to_le16(params->beacon_interval);
1933	cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
1934	cmd.ibss.header.len = 2;
1935	cmd.ibss.atimwindow = 0;
1936	cmd.ds.header.id = WLAN_EID_DS_PARAMS;
1937	cmd.ds.header.len = 1;
1938	cmd.ds.channel = params->chandef.chan->hw_value;
1939	/* Only v8 and below support setting probe delay */
1940	if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8)
1941		cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1942	/* TODO: mix in WLAN_CAPABILITY_PRIVACY */
1943	capability = WLAN_CAPABILITY_IBSS;
1944	cmd.capability = cpu_to_le16(capability);
1945	lbs_add_rates(cmd.rates);
1946
1947
1948	ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
1949	if (ret)
1950		goto out;
1951
1952	/*
1953	 * This is a sample response to CMD_802_11_AD_HOC_JOIN:
1954	 *
1955	 * response        2b 80
1956	 * size            14 00
1957	 * sequence        xx xx
1958	 * result          00 00
1959	 * reserved        00
1960	 * bssid           02 2b 7b 0f 86 0e
1961	 */
1962	lbs_join_post(priv, params, resp->bssid, capability);
1963
1964 out:
1965	return ret;
1966}
1967
1968
1969static int lbs_join_ibss(struct wiphy *wiphy, struct net_device *dev,
1970		struct cfg80211_ibss_params *params)
1971{
1972	struct lbs_private *priv = wiphy_priv(wiphy);
1973	int ret = 0;
1974	struct cfg80211_bss *bss;
1975
1976	if (dev == priv->mesh_dev)
1977		return -EOPNOTSUPP;
1978
1979	if (!params->chandef.chan) {
1980		ret = -ENOTSUPP;
1981		goto out;
1982	}
1983
1984	ret = lbs_set_channel(priv, params->chandef.chan->hw_value);
1985	if (ret)
1986		goto out;
1987
1988	/* Search if someone is beaconing. This assumes that the
1989	 * bss list is populated already */
1990	bss = cfg80211_get_bss(wiphy, params->chandef.chan, params->bssid,
1991		params->ssid, params->ssid_len,
1992		IEEE80211_BSS_TYPE_IBSS, IEEE80211_PRIVACY_ANY);
1993
1994	if (bss) {
1995		ret = lbs_ibss_join_existing(priv, params, bss);
1996		cfg80211_put_bss(wiphy, bss);
1997	} else
1998		ret = lbs_ibss_start_new(priv, params);
1999
2000
2001 out:
2002	return ret;
2003}
2004
2005
2006static int lbs_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2007{
2008	struct lbs_private *priv = wiphy_priv(wiphy);
2009	struct cmd_ds_802_11_ad_hoc_stop cmd;
2010	int ret = 0;
2011
2012	if (dev == priv->mesh_dev)
2013		return -EOPNOTSUPP;
2014
2015	memset(&cmd, 0, sizeof(cmd));
2016	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
2017	ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
2018
2019	/* TODO: consider doing this at MACREG_INT_CODE_ADHOC_BCN_LOST time */
2020	lbs_mac_event_disconnected(priv, true);
2021
2022	return ret;
2023}
2024
2025
2026
2027static int lbs_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
2028			      bool enabled, int timeout)
2029{
2030	struct lbs_private *priv = wiphy_priv(wiphy);
2031
2032	if  (!(priv->fwcapinfo & FW_CAPINFO_PS)) {
2033		if (!enabled)
2034			return 0;
2035		else
2036			return -EINVAL;
2037	}
2038	/* firmware does not work well with too long latency with power saving
2039	 * enabled, so do not enable it if there is only polling, no
2040	 * interrupts (like in some sdio hosts which can only
2041	 * poll for sdio irqs)
2042	 */
2043	if  (priv->is_polling) {
2044		if (!enabled)
2045			return 0;
2046		else
2047			return -EINVAL;
2048	}
2049	if (!enabled) {
2050		priv->psmode = LBS802_11POWERMODECAM;
2051		if (priv->psstate != PS_STATE_FULL_POWER)
2052			lbs_set_ps_mode(priv,
2053					PS_MODE_ACTION_EXIT_PS,
2054					true);
2055		return 0;
2056	}
2057	if (priv->psmode != LBS802_11POWERMODECAM)
2058		return 0;
2059	priv->psmode = LBS802_11POWERMODEMAX_PSP;
2060	if (priv->connect_status == LBS_CONNECTED)
2061		lbs_set_ps_mode(priv, PS_MODE_ACTION_ENTER_PS, true);
2062	return 0;
2063}
2064
2065/*
2066 * Initialization
2067 */
2068
2069static const struct cfg80211_ops lbs_cfg80211_ops = {
2070	.set_monitor_channel = lbs_cfg_set_monitor_channel,
2071	.libertas_set_mesh_channel = lbs_cfg_set_mesh_channel,
2072	.scan = lbs_cfg_scan,
2073	.connect = lbs_cfg_connect,
2074	.disconnect = lbs_cfg_disconnect,
2075	.add_key = lbs_cfg_add_key,
2076	.del_key = lbs_cfg_del_key,
2077	.set_default_key = lbs_cfg_set_default_key,
2078	.get_station = lbs_cfg_get_station,
2079	.change_virtual_intf = lbs_change_intf,
2080	.join_ibss = lbs_join_ibss,
2081	.leave_ibss = lbs_leave_ibss,
2082	.set_power_mgmt = lbs_set_power_mgmt,
2083};
2084
2085
2086/*
2087 * At this time lbs_private *priv doesn't even exist, so we just allocate
2088 * memory and don't initialize the wiphy further. This is postponed until we
2089 * can talk to the firmware and happens at registration time in
2090 * lbs_cfg_wiphy_register().
2091 */
2092struct wireless_dev *lbs_cfg_alloc(struct device *dev)
2093{
2094	int ret = 0;
2095	struct wireless_dev *wdev;
2096
2097	wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
2098	if (!wdev)
2099		return ERR_PTR(-ENOMEM);
2100
2101	wdev->wiphy = wiphy_new(&lbs_cfg80211_ops, sizeof(struct lbs_private));
2102	if (!wdev->wiphy) {
2103		dev_err(dev, "cannot allocate wiphy\n");
2104		ret = -ENOMEM;
2105		goto err_wiphy_new;
2106	}
2107
2108	return wdev;
2109
2110 err_wiphy_new:
2111	kfree(wdev);
2112	return ERR_PTR(ret);
2113}
2114
2115
2116static void lbs_cfg_set_regulatory_hint(struct lbs_private *priv)
2117{
2118	struct region_code_mapping {
2119		const char *cn;
2120		int code;
2121	};
2122
2123	/* Section 5.17.2 */
2124	static const struct region_code_mapping regmap[] = {
2125		{"US ", 0x10}, /* US FCC */
2126		{"CA ", 0x20}, /* Canada */
2127		{"EU ", 0x30}, /* ETSI   */
2128		{"ES ", 0x31}, /* Spain  */
2129		{"FR ", 0x32}, /* France */
2130		{"JP ", 0x40}, /* Japan  */
2131	};
2132	size_t i;
2133
2134	for (i = 0; i < ARRAY_SIZE(regmap); i++)
2135		if (regmap[i].code == priv->regioncode) {
2136			regulatory_hint(priv->wdev->wiphy, regmap[i].cn);
2137			break;
2138		}
2139}
2140
2141static void lbs_reg_notifier(struct wiphy *wiphy,
2142			     struct regulatory_request *request)
2143{
2144	struct lbs_private *priv = wiphy_priv(wiphy);
2145
2146	memcpy(priv->country_code, request->alpha2, sizeof(request->alpha2));
2147	if (lbs_iface_active(priv))
2148		lbs_set_11d_domain_info(priv);
2149}
2150
2151/*
2152 * This function gets called after lbs_setup_firmware() determined the
2153 * firmware capabilities. So we can setup the wiphy according to our
2154 * hardware/firmware.
2155 */
2156int lbs_cfg_register(struct lbs_private *priv)
2157{
2158	struct wireless_dev *wdev = priv->wdev;
2159	int ret;
2160
2161	wdev->wiphy->max_scan_ssids = 1;
2162	wdev->wiphy->max_scan_ie_len = 256;
2163	wdev->wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
2164
2165	wdev->wiphy->interface_modes =
2166			BIT(NL80211_IFTYPE_STATION) |
2167			BIT(NL80211_IFTYPE_ADHOC);
2168	if (lbs_rtap_supported(priv))
2169		wdev->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
2170	if (lbs_mesh_activated(priv))
2171		wdev->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MESH_POINT);
2172
2173	wdev->wiphy->bands[NL80211_BAND_2GHZ] = &lbs_band_2ghz;
2174
2175	/*
2176	 * We could check priv->fwcapinfo && FW_CAPINFO_WPA, but I have
2177	 * never seen a firmware without WPA
2178	 */
2179	wdev->wiphy->cipher_suites = cipher_suites;
2180	wdev->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
2181	wdev->wiphy->reg_notifier = lbs_reg_notifier;
2182
2183	ret = wiphy_register(wdev->wiphy);
2184	if (ret < 0)
2185		pr_err("cannot register wiphy device\n");
2186
2187	priv->wiphy_registered = true;
2188
2189	ret = register_netdev(priv->dev);
2190	if (ret)
2191		pr_err("cannot register network device\n");
2192
2193	INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
2194
2195	lbs_cfg_set_regulatory_hint(priv);
2196
2197	return ret;
2198}
2199
2200void lbs_scan_deinit(struct lbs_private *priv)
2201{
2202	cancel_delayed_work_sync(&priv->scan_work);
2203}
2204
2205
2206void lbs_cfg_free(struct lbs_private *priv)
2207{
2208	struct wireless_dev *wdev = priv->wdev;
2209
2210	if (!wdev)
2211		return;
2212
2213	if (priv->wiphy_registered)
2214		wiphy_unregister(wdev->wiphy);
2215
2216	if (wdev->wiphy)
2217		wiphy_free(wdev->wiphy);
2218
2219	kfree(wdev);
2220}
~/ Read-only file preview.