wl_cfg80211.c 135 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * Copyright (c) 2010 Broadcom Corporation
 *
 * 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.
 */

/* Toplevel file. Relies on dhd_linux.c to send commands to the dongle. */

19
20
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

21
22
23
24
25
26
27
28
29
30
#include <linux/kernel.h>
#include <linux/if_arp.h>
#include <linux/sched.h>
#include <linux/kthread.h>
#include <linux/netdevice.h>
#include <linux/bitops.h>
#include <linux/etherdevice.h>
#include <linux/ieee80211.h>
#include <linux/uaccess.h>
#include <net/cfg80211.h>
31
#include <net/netlink.h>
32
33
34
35
36
37

#include <brcmu_utils.h>
#include <defs.h>
#include <brcmu_wifi.h>
#include "dhd.h"
#include "wl_cfg80211.h"
38
#include "fwil.h"
39

40
41
42
43
44
45
46
47
48
49
50
51
#define BRCMF_SCAN_IE_LEN_MAX		2048
#define BRCMF_PNO_VERSION		2
#define BRCMF_PNO_TIME			30
#define BRCMF_PNO_REPEAT		4
#define BRCMF_PNO_FREQ_EXPO_MAX		3
#define BRCMF_PNO_MAX_PFN_COUNT		16
#define BRCMF_PNO_ENABLE_ADAPTSCAN_BIT	6
#define BRCMF_PNO_HIDDEN_BIT		2
#define BRCMF_PNO_WPA_AUTH_ANY		0xFFFFFFFF
#define BRCMF_PNO_SCAN_COMPLETE		1
#define BRCMF_PNO_SCAN_INCOMPLETE	0

Hante Meuleman's avatar
Hante Meuleman committed
52
#define TLV_LEN_OFF			1	/* length offset */
53
#define TLV_HDR_LEN			2	/* header length */
Hante Meuleman's avatar
Hante Meuleman committed
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
#define TLV_BODY_OFF			2	/* body offset */
#define TLV_OUI_LEN			3	/* oui id length */
#define WPA_OUI				"\x00\x50\xF2"	/* WPA OUI */
#define WPA_OUI_TYPE			1
#define RSN_OUI				"\x00\x0F\xAC"	/* RSN OUI */
#define	WME_OUI_TYPE			2

#define VS_IE_FIXED_HDR_LEN		6
#define WPA_IE_VERSION_LEN		2
#define WPA_IE_MIN_OUI_LEN		4
#define WPA_IE_SUITE_COUNT_LEN		2

#define WPA_CIPHER_NONE			0	/* None */
#define WPA_CIPHER_WEP_40		1	/* WEP (40-bit) */
#define WPA_CIPHER_TKIP			2	/* TKIP: default for WPA */
#define WPA_CIPHER_AES_CCM		4	/* AES (CCM) */
#define WPA_CIPHER_WEP_104		5	/* WEP (104-bit) */

#define RSN_AKM_NONE			0	/* None (IBSS) */
#define RSN_AKM_UNSPECIFIED		1	/* Over 802.1x */
#define RSN_AKM_PSK			2	/* Pre-shared Key */
#define RSN_CAP_LEN			2	/* Length of RSN capabilities */
#define RSN_CAP_PTK_REPLAY_CNTR_MASK	0x000C

#define VNDR_IE_CMD_LEN			4	/* length of the set command
						 * string :"add", "del" (+ NUL)
						 */
#define VNDR_IE_COUNT_OFFSET		4
#define VNDR_IE_PKTFLAG_OFFSET		8
#define VNDR_IE_VSIE_OFFSET		12
#define VNDR_IE_HDR_SIZE		12
#define VNDR_IE_BEACON_FLAG		0x1
#define VNDR_IE_PRBRSP_FLAG		0x2
#define MAX_VNDR_IE_NUMBER		5

#define	DOT11_MGMT_HDR_LEN		24	/* d11 management header len */
#define	DOT11_BCN_PRB_FIXED_LEN		12	/* beacon/probe fixed length */
91

92
93
94
95
96
97
98
99
100
#define BRCMF_ASSOC_PARAMS_FIXED_SIZE \
	(sizeof(struct brcmf_assoc_params_le) - sizeof(u16))

static const u8 ether_bcast[ETH_ALEN] = {255, 255, 255, 255, 255, 255};

static u32 brcmf_dbg_level = WL_DBG_ERR;

static bool check_sys_up(struct wiphy *wiphy)
{
101
102
	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
	if (!test_bit(WL_STATUS_READY, &cfg->status)) {
103
		WL_INFO("device is not ready : status (%d)\n",
104
			(int)cfg->status);
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
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
279
280
281
282
283
284
		return false;
	}
	return true;
}

#define CHAN2G(_channel, _freq, _flags) {			\
	.band			= IEEE80211_BAND_2GHZ,		\
	.center_freq		= (_freq),			\
	.hw_value		= (_channel),			\
	.flags			= (_flags),			\
	.max_antenna_gain	= 0,				\
	.max_power		= 30,				\
}

#define CHAN5G(_channel, _flags) {				\
	.band			= IEEE80211_BAND_5GHZ,		\
	.center_freq		= 5000 + (5 * (_channel)),	\
	.hw_value		= (_channel),			\
	.flags			= (_flags),			\
	.max_antenna_gain	= 0,				\
	.max_power		= 30,				\
}

#define RATE_TO_BASE100KBPS(rate)   (((rate) * 10) / 2)
#define RATETAB_ENT(_rateid, _flags) \
	{                                                               \
		.bitrate        = RATE_TO_BASE100KBPS(_rateid),     \
		.hw_value       = (_rateid),                            \
		.flags          = (_flags),                             \
	}

static struct ieee80211_rate __wl_rates[] = {
	RATETAB_ENT(BRCM_RATE_1M, 0),
	RATETAB_ENT(BRCM_RATE_2M, IEEE80211_RATE_SHORT_PREAMBLE),
	RATETAB_ENT(BRCM_RATE_5M5, IEEE80211_RATE_SHORT_PREAMBLE),
	RATETAB_ENT(BRCM_RATE_11M, IEEE80211_RATE_SHORT_PREAMBLE),
	RATETAB_ENT(BRCM_RATE_6M, 0),
	RATETAB_ENT(BRCM_RATE_9M, 0),
	RATETAB_ENT(BRCM_RATE_12M, 0),
	RATETAB_ENT(BRCM_RATE_18M, 0),
	RATETAB_ENT(BRCM_RATE_24M, 0),
	RATETAB_ENT(BRCM_RATE_36M, 0),
	RATETAB_ENT(BRCM_RATE_48M, 0),
	RATETAB_ENT(BRCM_RATE_54M, 0),
};

#define wl_a_rates		(__wl_rates + 4)
#define wl_a_rates_size	8
#define wl_g_rates		(__wl_rates + 0)
#define wl_g_rates_size	12

static struct ieee80211_channel __wl_2ghz_channels[] = {
	CHAN2G(1, 2412, 0),
	CHAN2G(2, 2417, 0),
	CHAN2G(3, 2422, 0),
	CHAN2G(4, 2427, 0),
	CHAN2G(5, 2432, 0),
	CHAN2G(6, 2437, 0),
	CHAN2G(7, 2442, 0),
	CHAN2G(8, 2447, 0),
	CHAN2G(9, 2452, 0),
	CHAN2G(10, 2457, 0),
	CHAN2G(11, 2462, 0),
	CHAN2G(12, 2467, 0),
	CHAN2G(13, 2472, 0),
	CHAN2G(14, 2484, 0),
};

static struct ieee80211_channel __wl_5ghz_a_channels[] = {
	CHAN5G(34, 0), CHAN5G(36, 0),
	CHAN5G(38, 0), CHAN5G(40, 0),
	CHAN5G(42, 0), CHAN5G(44, 0),
	CHAN5G(46, 0), CHAN5G(48, 0),
	CHAN5G(52, 0), CHAN5G(56, 0),
	CHAN5G(60, 0), CHAN5G(64, 0),
	CHAN5G(100, 0), CHAN5G(104, 0),
	CHAN5G(108, 0), CHAN5G(112, 0),
	CHAN5G(116, 0), CHAN5G(120, 0),
	CHAN5G(124, 0), CHAN5G(128, 0),
	CHAN5G(132, 0), CHAN5G(136, 0),
	CHAN5G(140, 0), CHAN5G(149, 0),
	CHAN5G(153, 0), CHAN5G(157, 0),
	CHAN5G(161, 0), CHAN5G(165, 0),
	CHAN5G(184, 0), CHAN5G(188, 0),
	CHAN5G(192, 0), CHAN5G(196, 0),
	CHAN5G(200, 0), CHAN5G(204, 0),
	CHAN5G(208, 0), CHAN5G(212, 0),
	CHAN5G(216, 0),
};

static struct ieee80211_channel __wl_5ghz_n_channels[] = {
	CHAN5G(32, 0), CHAN5G(34, 0),
	CHAN5G(36, 0), CHAN5G(38, 0),
	CHAN5G(40, 0), CHAN5G(42, 0),
	CHAN5G(44, 0), CHAN5G(46, 0),
	CHAN5G(48, 0), CHAN5G(50, 0),
	CHAN5G(52, 0), CHAN5G(54, 0),
	CHAN5G(56, 0), CHAN5G(58, 0),
	CHAN5G(60, 0), CHAN5G(62, 0),
	CHAN5G(64, 0), CHAN5G(66, 0),
	CHAN5G(68, 0), CHAN5G(70, 0),
	CHAN5G(72, 0), CHAN5G(74, 0),
	CHAN5G(76, 0), CHAN5G(78, 0),
	CHAN5G(80, 0), CHAN5G(82, 0),
	CHAN5G(84, 0), CHAN5G(86, 0),
	CHAN5G(88, 0), CHAN5G(90, 0),
	CHAN5G(92, 0), CHAN5G(94, 0),
	CHAN5G(96, 0), CHAN5G(98, 0),
	CHAN5G(100, 0), CHAN5G(102, 0),
	CHAN5G(104, 0), CHAN5G(106, 0),
	CHAN5G(108, 0), CHAN5G(110, 0),
	CHAN5G(112, 0), CHAN5G(114, 0),
	CHAN5G(116, 0), CHAN5G(118, 0),
	CHAN5G(120, 0), CHAN5G(122, 0),
	CHAN5G(124, 0), CHAN5G(126, 0),
	CHAN5G(128, 0), CHAN5G(130, 0),
	CHAN5G(132, 0), CHAN5G(134, 0),
	CHAN5G(136, 0), CHAN5G(138, 0),
	CHAN5G(140, 0), CHAN5G(142, 0),
	CHAN5G(144, 0), CHAN5G(145, 0),
	CHAN5G(146, 0), CHAN5G(147, 0),
	CHAN5G(148, 0), CHAN5G(149, 0),
	CHAN5G(150, 0), CHAN5G(151, 0),
	CHAN5G(152, 0), CHAN5G(153, 0),
	CHAN5G(154, 0), CHAN5G(155, 0),
	CHAN5G(156, 0), CHAN5G(157, 0),
	CHAN5G(158, 0), CHAN5G(159, 0),
	CHAN5G(160, 0), CHAN5G(161, 0),
	CHAN5G(162, 0), CHAN5G(163, 0),
	CHAN5G(164, 0), CHAN5G(165, 0),
	CHAN5G(166, 0), CHAN5G(168, 0),
	CHAN5G(170, 0), CHAN5G(172, 0),
	CHAN5G(174, 0), CHAN5G(176, 0),
	CHAN5G(178, 0), CHAN5G(180, 0),
	CHAN5G(182, 0), CHAN5G(184, 0),
	CHAN5G(186, 0), CHAN5G(188, 0),
	CHAN5G(190, 0), CHAN5G(192, 0),
	CHAN5G(194, 0), CHAN5G(196, 0),
	CHAN5G(198, 0), CHAN5G(200, 0),
	CHAN5G(202, 0), CHAN5G(204, 0),
	CHAN5G(206, 0), CHAN5G(208, 0),
	CHAN5G(210, 0), CHAN5G(212, 0),
	CHAN5G(214, 0), CHAN5G(216, 0),
	CHAN5G(218, 0), CHAN5G(220, 0),
	CHAN5G(222, 0), CHAN5G(224, 0),
	CHAN5G(226, 0), CHAN5G(228, 0),
};

static struct ieee80211_supported_band __wl_band_2ghz = {
	.band = IEEE80211_BAND_2GHZ,
	.channels = __wl_2ghz_channels,
	.n_channels = ARRAY_SIZE(__wl_2ghz_channels),
	.bitrates = wl_g_rates,
	.n_bitrates = wl_g_rates_size,
};

static struct ieee80211_supported_band __wl_band_5ghz_a = {
	.band = IEEE80211_BAND_5GHZ,
	.channels = __wl_5ghz_a_channels,
	.n_channels = ARRAY_SIZE(__wl_5ghz_a_channels),
	.bitrates = wl_a_rates,
	.n_bitrates = wl_a_rates_size,
};

static struct ieee80211_supported_band __wl_band_5ghz_n = {
	.band = IEEE80211_BAND_5GHZ,
	.channels = __wl_5ghz_n_channels,
	.n_channels = ARRAY_SIZE(__wl_5ghz_n_channels),
	.bitrates = wl_a_rates,
	.n_bitrates = wl_a_rates_size,
};

static const u32 __wl_cipher_suites[] = {
	WLAN_CIPHER_SUITE_WEP40,
	WLAN_CIPHER_SUITE_WEP104,
	WLAN_CIPHER_SUITE_TKIP,
	WLAN_CIPHER_SUITE_CCMP,
	WLAN_CIPHER_SUITE_AES_CMAC,
};

285
286
287
288
289
290
291
/* tag_ID/length/value_buffer tuple */
struct brcmf_tlv {
	u8 id;
	u8 len;
	u8 data[1];
};

Hante Meuleman's avatar
Hante Meuleman committed
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* Vendor specific ie. id = 221, oui and type defines exact ie */
struct brcmf_vs_tlv {
	u8 id;
	u8 len;
	u8 oui[3];
	u8 oui_type;
};

struct parsed_vndr_ie_info {
	u8 *ie_ptr;
	u32 ie_len;	/* total length including id & length field */
	struct brcmf_vs_tlv vndrie;
};

struct parsed_vndr_ies {
	u32 count;
	struct parsed_vndr_ie_info ie_info[MAX_VNDR_IE_NUMBER];
};

311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/* Quarter dBm units to mW
 * Table starts at QDBM_OFFSET, so the first entry is mW for qdBm=153
 * Table is offset so the last entry is largest mW value that fits in
 * a u16.
 */

#define QDBM_OFFSET 153		/* Offset for first entry */
#define QDBM_TABLE_LEN 40	/* Table size */

/* Smallest mW value that will round up to the first table entry, QDBM_OFFSET.
 * Value is ( mW(QDBM_OFFSET - 1) + mW(QDBM_OFFSET) ) / 2
 */
#define QDBM_TABLE_LOW_BOUND 6493	/* Low bound */

/* Largest mW value that will round down to the last table entry,
 * QDBM_OFFSET + QDBM_TABLE_LEN-1.
 * Value is ( mW(QDBM_OFFSET + QDBM_TABLE_LEN - 1) +
 * mW(QDBM_OFFSET + QDBM_TABLE_LEN) ) / 2.
 */
#define QDBM_TABLE_HIGH_BOUND 64938	/* High bound */

static const u16 nqdBm_to_mW_map[QDBM_TABLE_LEN] = {
/* qdBm:	+0	+1	+2	+3	+4	+5	+6	+7 */
/* 153: */ 6683, 7079, 7499, 7943, 8414, 8913, 9441, 10000,
/* 161: */ 10593, 11220, 11885, 12589, 13335, 14125, 14962, 15849,
/* 169: */ 16788, 17783, 18836, 19953, 21135, 22387, 23714, 25119,
/* 177: */ 26607, 28184, 29854, 31623, 33497, 35481, 37584, 39811,
/* 185: */ 42170, 44668, 47315, 50119, 53088, 56234, 59566, 63096
};

static u16 brcmf_qdbm_to_mw(u8 qdbm)
{
	uint factor = 1;
	int idx = qdbm - QDBM_OFFSET;

	if (idx >= QDBM_TABLE_LEN)
		/* clamp to max u16 mW value */
		return 0xFFFF;

	/* scale the qdBm index up to the range of the table 0-40
	 * where an offset of 40 qdBm equals a factor of 10 mW.
	 */
	while (idx < 0) {
		idx += 40;
		factor *= 10;
	}

	/* return the mW value scaled down to the correct factor of 10,
	 * adding in factor/2 to get proper rounding.
	 */
	return (nqdBm_to_mW_map[idx] + factor / 2) / factor;
}

static u8 brcmf_mw_to_qdbm(u16 mw)
{
	u8 qdbm;
	int offset;
	uint mw_uint = mw;
	uint boundary;

	/* handle boundary case */
	if (mw_uint <= 1)
		return 0;

	offset = QDBM_OFFSET;

	/* move mw into the range of the table */
	while (mw_uint < QDBM_TABLE_LOW_BOUND) {
		mw_uint *= 10;
		offset -= 40;
	}

	for (qdbm = 0; qdbm < QDBM_TABLE_LEN - 1; qdbm++) {
		boundary = nqdBm_to_mW_map[qdbm] + (nqdBm_to_mW_map[qdbm + 1] -
						    nqdBm_to_mW_map[qdbm]) / 2;
		if (mw_uint < boundary)
			break;
	}

	qdbm += (u8) offset;

	return qdbm;
}

395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
static u16 channel_to_chanspec(struct ieee80211_channel *ch)
{
	u16 chanspec;

	chanspec = ieee80211_frequency_to_channel(ch->center_freq);
	chanspec &= WL_CHANSPEC_CHAN_MASK;

	if (ch->band == IEEE80211_BAND_2GHZ)
		chanspec |= WL_CHANSPEC_BAND_2G;
	else
		chanspec |= WL_CHANSPEC_BAND_5G;

	if (ch->flags & IEEE80211_CHAN_NO_HT40) {
		chanspec |= WL_CHANSPEC_BW_20;
		chanspec |= WL_CHANSPEC_CTL_SB_NONE;
	} else {
		chanspec |= WL_CHANSPEC_BW_40;
		if (ch->flags & IEEE80211_CHAN_NO_HT40PLUS)
			chanspec |= WL_CHANSPEC_CTL_SB_LOWER;
		else
			chanspec |= WL_CHANSPEC_CTL_SB_UPPER;
	}
	return chanspec;
}

420
421
422
423
424
425
426
427
428
429
430
431
432
433
static void convert_key_from_CPU(struct brcmf_wsec_key *key,
				 struct brcmf_wsec_key_le *key_le)
{
	key_le->index = cpu_to_le32(key->index);
	key_le->len = cpu_to_le32(key->len);
	key_le->algo = cpu_to_le32(key->algo);
	key_le->flags = cpu_to_le32(key->flags);
	key_le->rxiv.hi = cpu_to_le32(key->rxiv.hi);
	key_le->rxiv.lo = cpu_to_le16(key->rxiv.lo);
	key_le->iv_initialized = cpu_to_le32(key->iv_initialized);
	memcpy(key_le->data, key->data, sizeof(key->data));
	memcpy(key_le->ea, key->ea, sizeof(key->ea));
}

434
static int
435
send_key_to_dongle(struct brcmf_cfg80211_info *cfg, s32 bssidx,
436
		   struct net_device *ndev, struct brcmf_wsec_key *key)
437
438
439
440
441
{
	int err;
	struct brcmf_wsec_key_le key_le;

	convert_key_from_CPU(key, &key_le);
442

443
444
	brcmf_netdev_wait_pend8021x(ndev);

445
	err = brcmf_fil_bsscfg_data_set(ndev, "wsec_key", &key_le,
446
					sizeof(key_le));
447

448
	if (err)
449
		WL_ERR("wsec_key error (%d)\n", err);
450
451
452
453
454
455
456
457
	return err;
}

static s32
brcmf_cfg80211_change_iface(struct wiphy *wiphy, struct net_device *ndev,
			 enum nl80211_iftype type, u32 *flags,
			 struct vif_params *params)
{
458
	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
459
	s32 infra = 0;
Hante Meuleman's avatar
Hante Meuleman committed
460
	s32 ap = 0;
461
462
	s32 err = 0;

Hante Meuleman's avatar
Hante Meuleman committed
463
	WL_TRACE("Enter, ndev=%p, type=%d\n", ndev, type);
464
465
466
467
468
469
470
471

	switch (type) {
	case NL80211_IFTYPE_MONITOR:
	case NL80211_IFTYPE_WDS:
		WL_ERR("type (%d) : currently we do not support this type\n",
		       type);
		return -EOPNOTSUPP;
	case NL80211_IFTYPE_ADHOC:
472
		cfg->conf->mode = WL_MODE_IBSS;
473
474
475
		infra = 0;
		break;
	case NL80211_IFTYPE_STATION:
476
		cfg->conf->mode = WL_MODE_BSS;
477
478
		infra = 1;
		break;
Hante Meuleman's avatar
Hante Meuleman committed
479
	case NL80211_IFTYPE_AP:
480
		cfg->conf->mode = WL_MODE_AP;
Hante Meuleman's avatar
Hante Meuleman committed
481
482
		ap = 1;
		break;
483
484
485
486
487
	default:
		err = -EINVAL;
		goto done;
	}

Hante Meuleman's avatar
Hante Meuleman committed
488
	if (ap) {
489
490
491
492
493
		set_bit(WL_STATUS_AP_CREATING, &cfg->status);
		if (!cfg->ap_info)
			cfg->ap_info = kzalloc(sizeof(*cfg->ap_info),
					       GFP_KERNEL);
		if (!cfg->ap_info) {
Hante Meuleman's avatar
Hante Meuleman committed
494
495
496
497
			err = -ENOMEM;
			goto done;
		}
		WL_INFO("IF Type = AP\n");
498
	} else {
499
		err = brcmf_fil_cmd_int_set(ndev, BRCMF_C_SET_INFRA, infra);
Hante Meuleman's avatar
Hante Meuleman committed
500
501
502
503
504
505
		if (err) {
			WL_ERR("WLC_SET_INFRA error (%d)\n", err);
			err = -EAGAIN;
			goto done;
		}
		WL_INFO("IF Type = %s\n",
506
			(cfg->conf->mode == WL_MODE_IBSS) ?
Hante Meuleman's avatar
Hante Meuleman committed
507
			"Adhoc" : "Infra");
508
	}
Hante Meuleman's avatar
Hante Meuleman committed
509
	ndev->ieee80211_ptr->iftype = type;
510
511
512
513
514
515
516

done:
	WL_TRACE("Exit\n");

	return err;
}

517
518
519
520
521
/*
 * For now brcmf_find_bssidx will return 0. Once p2p gets implemented this
 * should return the ndev matching bssidx.
 */
static s32
522
brcmf_find_bssidx(struct brcmf_cfg80211_info *cfg, struct net_device *ndev)
523
524
525
526
{
	return 0;
}

527
528
529
static void brcmf_set_mpc(struct net_device *ndev, int mpc)
{
	s32 err = 0;
530
	struct brcmf_cfg80211_info *cfg = ndev_to_cfg(ndev);
531

532
	if (test_bit(WL_STATUS_READY, &cfg->status)) {
533
		err = brcmf_fil_iovar_int_set(ndev, "mpc", mpc);
534
535
536
537
538
539
540
541
		if (err) {
			WL_ERR("fail to set mpc\n");
			return;
		}
		WL_INFO("MPC : %d\n", mpc);
	}
}

542
543
static void brcmf_iscan_prep(struct brcmf_scan_params_le *params_le,
			     struct brcmf_ssid *ssid)
544
545
546
547
548
549
550
551
552
{
	memcpy(params_le->bssid, ether_bcast, ETH_ALEN);
	params_le->bss_type = DOT11_BSSTYPE_ANY;
	params_le->scan_type = 0;
	params_le->channel_num = 0;
	params_le->nprobes = cpu_to_le32(-1);
	params_le->active_time = cpu_to_le32(-1);
	params_le->passive_time = cpu_to_le32(-1);
	params_le->home_time = cpu_to_le32(-1);
553
554
555
556
	if (ssid && ssid->SSID_len) {
		params_le->ssid_le.SSID_len = cpu_to_le32(ssid->SSID_len);
		memcpy(&params_le->ssid_le.SSID, ssid->SSID, ssid->SSID_len);
	}
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
}

static s32
brcmf_run_iscan(struct brcmf_cfg80211_iscan_ctrl *iscan,
		struct brcmf_ssid *ssid, u16 action)
{
	s32 params_size = BRCMF_SCAN_PARAMS_FIXED_SIZE +
			  offsetof(struct brcmf_iscan_params_le, params_le);
	struct brcmf_iscan_params_le *params;
	s32 err = 0;

	if (ssid && ssid->SSID_len)
		params_size += sizeof(struct brcmf_ssid);
	params = kzalloc(params_size, GFP_KERNEL);
	if (!params)
		return -ENOMEM;
	BUG_ON(params_size >= BRCMF_DCMD_SMLEN);

575
	brcmf_iscan_prep(&params->params_le, ssid);
576
577
578
579
580

	params->version = cpu_to_le32(BRCMF_ISCAN_REQ_VERSION);
	params->action = cpu_to_le16(action);
	params->scan_duration = cpu_to_le16(0);

581
582
	err = brcmf_fil_iovar_data_set(iscan->ndev, "iscan", params,
				       params_size);
583
584
585
586
587
588
589
590
591
592
593
	if (err) {
		if (err == -EBUSY)
			WL_INFO("system busy : iscan canceled\n");
		else
			WL_ERR("error (%d)\n", err);
	}

	kfree(params);
	return err;
}

594
static s32 brcmf_do_iscan(struct brcmf_cfg80211_info *cfg)
595
{
596
597
	struct brcmf_cfg80211_iscan_ctrl *iscan = cfg_to_iscan(cfg);
	struct net_device *ndev = cfg_to_ndev(cfg);
598
	struct brcmf_ssid ssid;
599
	u32 passive_scan;
600
601
602
603
604
605
606
	s32 err = 0;

	/* Broadcast scan by default */
	memset(&ssid, 0, sizeof(ssid));

	iscan->state = WL_ISCAN_STATE_SCANING;

607
608
609
	passive_scan = cfg->active_scan ? 0 : 1;
	err = brcmf_fil_cmd_int_set(cfg_to_ndev(cfg),
				    BRCMF_C_SET_PASSIVE_SCAN, passive_scan);
610
611
612
613
614
	if (err) {
		WL_ERR("error (%d)\n", err);
		return err;
	}
	brcmf_set_mpc(ndev, 0);
615
	cfg->iscan_kickstart = true;
616
617
618
	err = brcmf_run_iscan(iscan, &ssid, BRCMF_SCAN_ACTION_START);
	if (err) {
		brcmf_set_mpc(ndev, 1);
619
		cfg->iscan_kickstart = false;
620
621
622
623
624
625
626
627
		return err;
	}
	mod_timer(&iscan->timer, jiffies + iscan->timer_ms * HZ / 1000);
	iscan->timer_on = 1;
	return err;
}

static s32
628
629
630
brcmf_cfg80211_iscan(struct wiphy *wiphy, struct net_device *ndev,
		     struct cfg80211_scan_request *request,
		     struct cfg80211_ssid *this_ssid)
631
{
632
	struct brcmf_cfg80211_info *cfg = ndev_to_cfg(ndev);
633
	struct cfg80211_ssid *ssids;
634
	struct brcmf_cfg80211_scan_req *sr = cfg->scan_req_int;
635
	u32 passive_scan;
636
637
638
639
640
	bool iscan_req;
	bool spec_scan;
	s32 err = 0;
	u32 SSID_len;

641
642
	if (test_bit(WL_STATUS_SCANNING, &cfg->status)) {
		WL_ERR("Scanning already : status (%lu)\n", cfg->status);
643
644
		return -EAGAIN;
	}
645
	if (test_bit(WL_STATUS_SCAN_ABORTING, &cfg->status)) {
646
		WL_ERR("Scanning being aborted : status (%lu)\n",
647
		       cfg->status);
648
649
		return -EAGAIN;
	}
650
	if (test_bit(WL_STATUS_CONNECTING, &cfg->status)) {
651
		WL_ERR("Connecting : status (%lu)\n",
652
		       cfg->status);
653
654
655
656
657
658
659
660
		return -EAGAIN;
	}

	iscan_req = false;
	spec_scan = false;
	if (request) {
		/* scan bss */
		ssids = request->ssids;
661
		if (cfg->iscan_on && (!ssids || !ssids->ssid_len))
662
663
664
665
666
667
668
			iscan_req = true;
	} else {
		/* scan in ibss */
		/* we don't do iscan in ibss */
		ssids = this_ssid;
	}

669
670
	cfg->scan_request = request;
	set_bit(WL_STATUS_SCANNING, &cfg->status);
671
	if (iscan_req) {
672
		err = brcmf_do_iscan(cfg);
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
		if (!err)
			return err;
		else
			goto scan_out;
	} else {
		WL_SCAN("ssid \"%s\", ssid_len (%d)\n",
		       ssids->ssid, ssids->ssid_len);
		memset(&sr->ssid_le, 0, sizeof(sr->ssid_le));
		SSID_len = min_t(u8, sizeof(sr->ssid_le.SSID), ssids->ssid_len);
		sr->ssid_le.SSID_len = cpu_to_le32(0);
		if (SSID_len) {
			memcpy(sr->ssid_le.SSID, ssids->ssid, SSID_len);
			sr->ssid_le.SSID_len = cpu_to_le32(SSID_len);
			spec_scan = true;
		} else {
			WL_SCAN("Broadcast scan\n");
		}

691
692
693
		passive_scan = cfg->active_scan ? 0 : 1;
		err = brcmf_fil_cmd_int_set(ndev, BRCMF_C_SET_PASSIVE_SCAN,
					    passive_scan);
694
695
696
697
698
		if (err) {
			WL_ERR("WLC_SET_PASSIVE_SCAN error (%d)\n", err);
			goto scan_out;
		}
		brcmf_set_mpc(ndev, 0);
699
700
		err = brcmf_fil_cmd_data_set(ndev, BRCMF_C_SCAN, &sr->ssid_le,
					     sizeof(sr->ssid_le));
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
		if (err) {
			if (err == -EBUSY)
				WL_INFO("system busy : scan for \"%s\" "
					"canceled\n", sr->ssid_le.SSID);
			else
				WL_ERR("WLC_SCAN error (%d)\n", err);

			brcmf_set_mpc(ndev, 1);
			goto scan_out;
		}
	}

	return 0;

scan_out:
716
717
	clear_bit(WL_STATUS_SCANNING, &cfg->status);
	cfg->scan_request = NULL;
718
719
720
	return err;
}

Hante Meuleman's avatar
Hante Meuleman committed
721
722
723
724
725
726
727
static void brcmf_escan_prep(struct brcmf_scan_params_le *params_le,
			     struct cfg80211_scan_request *request)
{
	u32 n_ssids;
	u32 n_channels;
	s32 i;
	s32 offset;
728
	u16 chanspec;
Hante Meuleman's avatar
Hante Meuleman committed
729
	char *ptr;
730
	struct brcmf_ssid_le ssid_le;
Hante Meuleman's avatar
Hante Meuleman committed
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751

	memcpy(params_le->bssid, ether_bcast, ETH_ALEN);
	params_le->bss_type = DOT11_BSSTYPE_ANY;
	params_le->scan_type = 0;
	params_le->channel_num = 0;
	params_le->nprobes = cpu_to_le32(-1);
	params_le->active_time = cpu_to_le32(-1);
	params_le->passive_time = cpu_to_le32(-1);
	params_le->home_time = cpu_to_le32(-1);
	memset(&params_le->ssid_le, 0, sizeof(params_le->ssid_le));

	/* if request is null exit so it will be all channel broadcast scan */
	if (!request)
		return;

	n_ssids = request->n_ssids;
	n_channels = request->n_channels;
	/* Copy channel array if applicable */
	WL_SCAN("### List of channelspecs to scan ### %d\n", n_channels);
	if (n_channels > 0) {
		for (i = 0; i < n_channels; i++) {
752
			chanspec = channel_to_chanspec(request->channels[i]);
Hante Meuleman's avatar
Hante Meuleman committed
753
			WL_SCAN("Chan : %d, Channel spec: %x\n",
754
				request->channels[i]->hw_value, chanspec);
755
			params_le->channel_list[i] = cpu_to_le16(chanspec);
Hante Meuleman's avatar
Hante Meuleman committed
756
757
758
759
760
761
762
763
764
765
766
767
		}
	} else {
		WL_SCAN("Scanning all channels\n");
	}
	/* Copy ssid array if applicable */
	WL_SCAN("### List of SSIDs to scan ### %d\n", n_ssids);
	if (n_ssids > 0) {
		offset = offsetof(struct brcmf_scan_params_le, channel_list) +
				n_channels * sizeof(u16);
		offset = roundup(offset, sizeof(u32));
		ptr = (char *)params_le + offset;
		for (i = 0; i < n_ssids; i++) {
768
769
770
771
772
773
			memset(&ssid_le, 0, sizeof(ssid_le));
			ssid_le.SSID_len =
					cpu_to_le32(request->ssids[i].ssid_len);
			memcpy(ssid_le.SSID, request->ssids[i].ssid,
			       request->ssids[i].ssid_len);
			if (!ssid_le.SSID_len)
Hante Meuleman's avatar
Hante Meuleman committed
774
775
776
				WL_SCAN("%d: Broadcast scan\n", i);
			else
				WL_SCAN("%d: scan for  %s size =%d\n", i,
777
778
779
					ssid_le.SSID, ssid_le.SSID_len);
			memcpy(ptr, &ssid_le, sizeof(ssid_le));
			ptr += sizeof(ssid_le);
Hante Meuleman's avatar
Hante Meuleman committed
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
		}
	} else {
		WL_SCAN("Broadcast scan %p\n", request->ssids);
		if ((request->ssids) && request->ssids->ssid_len) {
			WL_SCAN("SSID %s len=%d\n", params_le->ssid_le.SSID,
				request->ssids->ssid_len);
			params_le->ssid_le.SSID_len =
				cpu_to_le32(request->ssids->ssid_len);
			memcpy(&params_le->ssid_le.SSID, request->ssids->ssid,
				request->ssids->ssid_len);
		}
	}
	/* Adding mask to channel numbers */
	params_le->channel_num =
		cpu_to_le32((n_ssids << BRCMF_SCAN_PARAMS_NSSID_SHIFT) |
			(n_channels & BRCMF_SCAN_PARAMS_COUNT_MASK));
}

static s32
799
brcmf_notify_escan_complete(struct brcmf_cfg80211_info *cfg,
Hante Meuleman's avatar
Hante Meuleman committed
800
801
802
803
804
805
806
807
808
809
810
			    struct net_device *ndev,
			    bool aborted, bool fw_abort)
{
	struct brcmf_scan_params_le params_le;
	struct cfg80211_scan_request *scan_request;
	s32 err = 0;

	WL_SCAN("Enter\n");

	/* clear scan request, because the FW abort can cause a second call */
	/* to this functon and might cause a double cfg80211_scan_done      */
811
812
	scan_request = cfg->scan_request;
	cfg->scan_request = NULL;
Hante Meuleman's avatar
Hante Meuleman committed
813

814
815
	if (timer_pending(&cfg->escan_timeout))
		del_timer_sync(&cfg->escan_timeout);
Hante Meuleman's avatar
Hante Meuleman committed
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831

	if (fw_abort) {
		/* Do a scan abort to stop the driver's scan engine */
		WL_SCAN("ABORT scan in firmware\n");
		memset(&params_le, 0, sizeof(params_le));
		memcpy(params_le.bssid, ether_bcast, ETH_ALEN);
		params_le.bss_type = DOT11_BSSTYPE_ANY;
		params_le.scan_type = 0;
		params_le.channel_num = cpu_to_le32(1);
		params_le.nprobes = cpu_to_le32(1);
		params_le.active_time = cpu_to_le32(-1);
		params_le.passive_time = cpu_to_le32(-1);
		params_le.home_time = cpu_to_le32(-1);
		/* Scan is aborted by setting channel_list[0] to -1 */
		params_le.channel_list[0] = cpu_to_le16(-1);
		/* E-Scan (or anyother type) can be aborted by SCAN */
832
833
		err = brcmf_fil_cmd_data_set(ndev, BRCMF_C_SCAN, &params_le,
					     sizeof(params_le));
Hante Meuleman's avatar
Hante Meuleman committed
834
835
836
		if (err)
			WL_ERR("Scan abort  failed\n");
	}
837
838
839
840
	/*
	 * e-scan can be initiated by scheduled scan
	 * which takes precedence.
	 */
841
	if (cfg->sched_escan) {
842
		WL_SCAN("scheduled scan completed\n");
843
		cfg->sched_escan = false;
844
		if (!aborted)
845
			cfg80211_sched_scan_results(cfg_to_wiphy(cfg));
846
847
		brcmf_set_mpc(ndev, 1);
	} else if (scan_request) {
Hante Meuleman's avatar
Hante Meuleman committed
848
849
850
851
852
		WL_SCAN("ESCAN Completed scan: %s\n",
				aborted ? "Aborted" : "Done");
		cfg80211_scan_done(scan_request, aborted);
		brcmf_set_mpc(ndev, 1);
	}
853
	if (!test_and_clear_bit(WL_STATUS_SCANNING, &cfg->status)) {
Hante Meuleman's avatar
Hante Meuleman committed
854
855
856
857
858
859
860
861
		WL_ERR("Scan complete while device not scanning\n");
		return -EPERM;
	}

	return err;
}

static s32
862
brcmf_run_escan(struct brcmf_cfg80211_info *cfg, struct net_device *ndev,
Hante Meuleman's avatar
Hante Meuleman committed
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
		struct cfg80211_scan_request *request, u16 action)
{
	s32 params_size = BRCMF_SCAN_PARAMS_FIXED_SIZE +
			  offsetof(struct brcmf_escan_params_le, params_le);
	struct brcmf_escan_params_le *params;
	s32 err = 0;

	WL_SCAN("E-SCAN START\n");

	if (request != NULL) {
		/* Allocate space for populating ssids in struct */
		params_size += sizeof(u32) * ((request->n_channels + 1) / 2);

		/* Allocate space for populating ssids in struct */
		params_size += sizeof(struct brcmf_ssid) * request->n_ssids;
	}

	params = kzalloc(params_size, GFP_KERNEL);
	if (!params) {
		err = -ENOMEM;
		goto exit;
	}
	BUG_ON(params_size + sizeof("escan") >= BRCMF_DCMD_MEDLEN);
	brcmf_escan_prep(&params->params_le, request);
	params->version = cpu_to_le32(BRCMF_ESCAN_REQ_VERSION);
	params->action = cpu_to_le16(action);
	params->sync_id = cpu_to_le16(0x1234);

891
	err = brcmf_fil_iovar_data_set(ndev, "escan", params, params_size);
Hante Meuleman's avatar
Hante Meuleman committed
892
893
894
895
896
897
898
899
900
901
902
903
904
	if (err) {
		if (err == -EBUSY)
			WL_INFO("system busy : escan canceled\n");
		else
			WL_ERR("error (%d)\n", err);
	}

	kfree(params);
exit:
	return err;
}

static s32
905
brcmf_do_escan(struct brcmf_cfg80211_info *cfg, struct wiphy *wiphy,
Hante Meuleman's avatar
Hante Meuleman committed
906
907
908
	       struct net_device *ndev, struct cfg80211_scan_request *request)
{
	s32 err;
909
	u32 passive_scan;
Hante Meuleman's avatar
Hante Meuleman committed
910
911
912
	struct brcmf_scan_results *results;

	WL_SCAN("Enter\n");
913
914
915
	cfg->escan_info.ndev = ndev;
	cfg->escan_info.wiphy = wiphy;
	cfg->escan_info.escan_state = WL_ESCAN_STATE_SCANNING;
916
917
918
	passive_scan = cfg->active_scan ? 0 : 1;
	err = brcmf_fil_cmd_int_set(ndev, BRCMF_C_SET_PASSIVE_SCAN,
				    passive_scan);
Hante Meuleman's avatar
Hante Meuleman committed
919
920
921
922
923
	if (err) {
		WL_ERR("error (%d)\n", err);
		return err;
	}
	brcmf_set_mpc(ndev, 0);
924
	results = (struct brcmf_scan_results *)cfg->escan_info.escan_buf;
Hante Meuleman's avatar
Hante Meuleman committed
925
926
927
928
	results->version = 0;
	results->count = 0;
	results->buflen = WL_ESCAN_RESULTS_FIXED_SIZE;

929
	err = brcmf_run_escan(cfg, ndev, request, WL_ESCAN_ACTION_START);
Hante Meuleman's avatar
Hante Meuleman committed
930
931
932
933
934
935
936
937
938
939
	if (err)
		brcmf_set_mpc(ndev, 1);
	return err;
}

static s32
brcmf_cfg80211_escan(struct wiphy *wiphy, struct net_device *ndev,
		     struct cfg80211_scan_request *request,
		     struct cfg80211_ssid *this_ssid)
{
940
	struct brcmf_cfg80211_info *cfg = ndev_to_cfg(ndev);
Hante Meuleman's avatar
Hante Meuleman committed
941
	struct cfg80211_ssid *ssids;
942
	struct brcmf_cfg80211_scan_req *sr = cfg->scan_req_int;
943
	u32 passive_scan;
Hante Meuleman's avatar
Hante Meuleman committed
944
945
946
947
948
949
950
	bool escan_req;
	bool spec_scan;
	s32 err;
	u32 SSID_len;

	WL_SCAN("START ESCAN\n");

951
952
	if (test_bit(WL_STATUS_SCANNING, &cfg->status)) {
		WL_ERR("Scanning already : status (%lu)\n", cfg->status);
Hante Meuleman's avatar
Hante Meuleman committed
953
954
		return -EAGAIN;
	}
955
	if (test_bit(WL_STATUS_SCAN_ABORTING, &cfg->status)) {
Hante Meuleman's avatar
Hante Meuleman committed
956
		WL_ERR("Scanning being aborted : status (%lu)\n",
957
		       cfg->status);
Hante Meuleman's avatar
Hante Meuleman committed
958
959
		return -EAGAIN;
	}
960
	if (test_bit(WL_STATUS_CONNECTING, &cfg->status)) {
Hante Meuleman's avatar
Hante Meuleman committed
961
		WL_ERR("Connecting : status (%lu)\n",
962
		       cfg->status);
Hante Meuleman's avatar
Hante Meuleman committed
963
964
965
966
		return -EAGAIN;
	}

	/* Arm scan timeout timer */
967
	mod_timer(&cfg->escan_timeout, jiffies +
Hante Meuleman's avatar
Hante Meuleman committed
968
969
970
971
972
973
974
975
976
977
978
979
980
			WL_ESCAN_TIMER_INTERVAL_MS * HZ / 1000);

	escan_req = false;
	if (request) {
		/* scan bss */
		ssids = request->ssids;
		escan_req = true;
	} else {
		/* scan in ibss */
		/* we don't do escan in ibss */
		ssids = this_ssid;
	}

981
982
	cfg->scan_request = request;
	set_bit(WL_STATUS_SCANNING, &cfg->status);
Hante Meuleman's avatar
Hante Meuleman committed
983
	if (escan_req) {
984
		err = brcmf_do_escan(cfg, wiphy, ndev, request);
Hante Meuleman's avatar
Hante Meuleman committed
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
		if (!err)
			return err;
		else
			goto scan_out;
	} else {
		WL_SCAN("ssid \"%s\", ssid_len (%d)\n",
		       ssids->ssid, ssids->ssid_len);
		memset(&sr->ssid_le, 0, sizeof(sr->ssid_le));
		SSID_len = min_t(u8, sizeof(sr->ssid_le.SSID), ssids->ssid_len);
		sr->ssid_le.SSID_len = cpu_to_le32(0);
		spec_scan = false;
		if (SSID_len) {
			memcpy(sr->ssid_le.SSID, ssids->ssid, SSID_len);
			sr->ssid_le.SSID_len = cpu_to_le32(SSID_len);
			spec_scan = true;
		} else
For faster browsing, not all history is shown. View entire blame