main.c 39.5 KB
Newer Older
1 2 3 4 5 6
/**
  * This file contains the major functions in WLAN
  * driver. It includes init, exit, open, close and main
  * thread etc..
  */

7
#include <linux/moduleparam.h>
8 9 10 11 12
#include <linux/delay.h>
#include <linux/freezer.h>
#include <linux/etherdevice.h>
#include <linux/netdevice.h>
#include <linux/if_arp.h>
13
#include <linux/kthread.h>
14 15

#include <net/iw_handler.h>
16
#include <net/ieee80211.h>
17 18 19 20 21 22 23

#include "host.h"
#include "decl.h"
#include "dev.h"
#include "wext.h"
#include "debugfs.h"
#include "assoc.h"
24
#include "join.h"
25

26
#define DRIVER_RELEASE_VERSION "323.p0"
27
const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
28 29 30 31 32
#ifdef  DEBUG
    "-dbg"
#endif
    "";

33 34

/* Module parameters */
35 36 37
unsigned int lbs_debug;
EXPORT_SYMBOL_GPL(lbs_debug);
module_param_named(libertas_debug, lbs_debug, int, 0644);
38 39


40 41 42 43 44
#define LBS_TX_PWR_DEFAULT		20	/*100mW */
#define LBS_TX_PWR_US_DEFAULT		20	/*100mW */
#define LBS_TX_PWR_JP_DEFAULT		16	/*50mW */
#define LBS_TX_PWR_FR_DEFAULT		20	/*100mW */
#define LBS_TX_PWR_EMEA_DEFAULT	20	/*100mW */
45 46 47 48

/* Format { channel, frequency (MHz), maxtxpower } */
/* band: 'B/G', region: USA FCC/Canada IC */
static struct chan_freq_power channel_freq_power_US_BG[] = {
49 50 51 52 53 54 55 56 57 58 59
	{1, 2412, LBS_TX_PWR_US_DEFAULT},
	{2, 2417, LBS_TX_PWR_US_DEFAULT},
	{3, 2422, LBS_TX_PWR_US_DEFAULT},
	{4, 2427, LBS_TX_PWR_US_DEFAULT},
	{5, 2432, LBS_TX_PWR_US_DEFAULT},
	{6, 2437, LBS_TX_PWR_US_DEFAULT},
	{7, 2442, LBS_TX_PWR_US_DEFAULT},
	{8, 2447, LBS_TX_PWR_US_DEFAULT},
	{9, 2452, LBS_TX_PWR_US_DEFAULT},
	{10, 2457, LBS_TX_PWR_US_DEFAULT},
	{11, 2462, LBS_TX_PWR_US_DEFAULT}
60 61 62 63
};

/* band: 'B/G', region: Europe ETSI */
static struct chan_freq_power channel_freq_power_EU_BG[] = {
64 65 66 67 68 69 70 71 72 73 74 75 76
	{1, 2412, LBS_TX_PWR_EMEA_DEFAULT},
	{2, 2417, LBS_TX_PWR_EMEA_DEFAULT},
	{3, 2422, LBS_TX_PWR_EMEA_DEFAULT},
	{4, 2427, LBS_TX_PWR_EMEA_DEFAULT},
	{5, 2432, LBS_TX_PWR_EMEA_DEFAULT},
	{6, 2437, LBS_TX_PWR_EMEA_DEFAULT},
	{7, 2442, LBS_TX_PWR_EMEA_DEFAULT},
	{8, 2447, LBS_TX_PWR_EMEA_DEFAULT},
	{9, 2452, LBS_TX_PWR_EMEA_DEFAULT},
	{10, 2457, LBS_TX_PWR_EMEA_DEFAULT},
	{11, 2462, LBS_TX_PWR_EMEA_DEFAULT},
	{12, 2467, LBS_TX_PWR_EMEA_DEFAULT},
	{13, 2472, LBS_TX_PWR_EMEA_DEFAULT}
77 78 79 80
};

/* band: 'B/G', region: Spain */
static struct chan_freq_power channel_freq_power_SPN_BG[] = {
81 82
	{10, 2457, LBS_TX_PWR_DEFAULT},
	{11, 2462, LBS_TX_PWR_DEFAULT}
83 84 85 86
};

/* band: 'B/G', region: France */
static struct chan_freq_power channel_freq_power_FR_BG[] = {
87 88 89 90
	{10, 2457, LBS_TX_PWR_FR_DEFAULT},
	{11, 2462, LBS_TX_PWR_FR_DEFAULT},
	{12, 2467, LBS_TX_PWR_FR_DEFAULT},
	{13, 2472, LBS_TX_PWR_FR_DEFAULT}
91 92 93 94
};

/* band: 'B/G', region: Japan */
static struct chan_freq_power channel_freq_power_JPN_BG[] = {
95 96 97 98 99 100 101 102 103 104 105 106 107 108
	{1, 2412, LBS_TX_PWR_JP_DEFAULT},
	{2, 2417, LBS_TX_PWR_JP_DEFAULT},
	{3, 2422, LBS_TX_PWR_JP_DEFAULT},
	{4, 2427, LBS_TX_PWR_JP_DEFAULT},
	{5, 2432, LBS_TX_PWR_JP_DEFAULT},
	{6, 2437, LBS_TX_PWR_JP_DEFAULT},
	{7, 2442, LBS_TX_PWR_JP_DEFAULT},
	{8, 2447, LBS_TX_PWR_JP_DEFAULT},
	{9, 2452, LBS_TX_PWR_JP_DEFAULT},
	{10, 2457, LBS_TX_PWR_JP_DEFAULT},
	{11, 2462, LBS_TX_PWR_JP_DEFAULT},
	{12, 2467, LBS_TX_PWR_JP_DEFAULT},
	{13, 2472, LBS_TX_PWR_JP_DEFAULT},
	{14, 2484, LBS_TX_PWR_JP_DEFAULT}
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
};

/**
 * the structure for channel, frequency and power
 */
struct region_cfp_table {
	u8 region;
	struct chan_freq_power *cfp_BG;
	int cfp_no_BG;
};

/**
 * the structure for the mapping between region and CFP
 */
static struct region_cfp_table region_cfp_table[] = {
	{0x10,			/*US FCC */
	 channel_freq_power_US_BG,
126
	 ARRAY_SIZE(channel_freq_power_US_BG),
127 128 129 130
	 }
	,
	{0x20,			/*CANADA IC */
	 channel_freq_power_US_BG,
131
	 ARRAY_SIZE(channel_freq_power_US_BG),
132 133 134
	 }
	,
	{0x30, /*EU*/ channel_freq_power_EU_BG,
135
	 ARRAY_SIZE(channel_freq_power_EU_BG),
136 137 138
	 }
	,
	{0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
139
	 ARRAY_SIZE(channel_freq_power_SPN_BG),
140 141 142
	 }
	,
	{0x32, /*FRANCE*/ channel_freq_power_FR_BG,
143
	 ARRAY_SIZE(channel_freq_power_FR_BG),
144 145 146
	 }
	,
	{0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
147
	 ARRAY_SIZE(channel_freq_power_JPN_BG),
148 149 150 151 152 153
	 }
	,
/*Add new region here */
};

/**
154
 * the table to keep region code
155
 */
156
u16 lbs_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
157
    { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
158 159

/**
160
 * 802.11b/g supported bitrates (in 500Kb/s units)
161
 */
162
u8 lbs_bg_rates[MAX_RATES] =
163 164
    { 0x02, 0x04, 0x0b, 0x16, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
0x00, 0x00 };
165 166

/**
167 168 169
 * FW rate table.  FW refers to rates by their index in this table, not by the
 * rate value itself.  Values of 0x00 are
 * reserved positions.
170
 */
171 172 173 174
static u8 fw_data_rates[MAX_RATES] =
    { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
      0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
};
175 176

/**
177 178 179 180
 *  @brief use index to get the data rate
 *
 *  @param idx                The index of data rate
 *  @return 	   		data rate or 0
181
 */
182
u32 lbs_fw_index_to_data_rate(u8 idx)
183 184 185 186 187 188 189 190 191 192 193 194
{
	if (idx >= sizeof(fw_data_rates))
		idx = 0;
	return fw_data_rates[idx];
}

/**
 *  @brief use rate to get the index
 *
 *  @param rate                 data rate
 *  @return 	   		index or 0
 */
195
u8 lbs_data_rate_to_fw_index(u32 rate)
196 197 198 199 200 201 202 203 204 205 206 207
{
	u8 i;

	if (!rate)
		return 0;

	for (i = 0; i < sizeof(fw_data_rates); i++) {
		if (rate == fw_data_rates[i])
			return i;
	}
	return 0;
}
208 209 210 211 212 213

/**
 * Attributes exported through sysfs
 */

/**
214
 * @brief Get function for sysfs attribute anycast_mask
215
 */
216
static ssize_t lbs_anycast_get(struct device *dev,
D
Dan Williams 已提交
217 218
		struct device_attribute *attr, char * buf)
{
219 220 221
	struct cmd_ds_mesh_access mesh_access;

	memset(&mesh_access, 0, sizeof(mesh_access));
222
	lbs_prepare_and_send_command(to_net_dev(dev)->priv,
223 224 225
			CMD_MESH_ACCESS,
			CMD_ACT_MESH_GET_ANYCAST,
			CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
226

227
	return snprintf(buf, 12, "0x%X\n", le32_to_cpu(mesh_access.data[0]));
228 229 230
}

/**
231
 * @brief Set function for sysfs attribute anycast_mask
232
 */
233
static ssize_t lbs_anycast_set(struct device *dev,
D
Dan Williams 已提交
234 235
		struct device_attribute *attr, const char * buf, size_t count)
{
236
	struct cmd_ds_mesh_access mesh_access;
237
	uint32_t datum;
238 239

	memset(&mesh_access, 0, sizeof(mesh_access));
240
	sscanf(buf, "%x", &datum);
241 242
	mesh_access.data[0] = cpu_to_le32(datum);

243
	lbs_prepare_and_send_command((to_net_dev(dev))->priv,
244 245 246
			CMD_MESH_ACCESS,
			CMD_ACT_MESH_SET_ANYCAST,
			CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
247 248 249
	return strlen(buf);
}

250 251
int lbs_add_rtap(struct lbs_private *priv);
void lbs_remove_rtap(struct lbs_private *priv);
252 253 254 255

/**
 * Get function for sysfs attribute rtap
 */
256
static ssize_t lbs_rtap_get(struct device *dev,
257 258
		struct device_attribute *attr, char * buf)
{
259 260 261
	struct lbs_private *priv = (struct lbs_private *)
		(to_net_dev(dev))->priv;
	struct lbs_adapter *adapter = priv->adapter;
262 263 264 265 266 267
	return snprintf(buf, 5, "0x%X\n", adapter->monitormode);
}

/**
 *  Set function for sysfs attribute rtap
 */
268
static ssize_t lbs_rtap_set(struct device *dev,
269 270 271
		struct device_attribute *attr, const char * buf, size_t count)
{
	int monitor_mode;
272 273 274
	struct lbs_private *priv = (struct lbs_private *)
		(to_net_dev(dev))->priv;
	struct lbs_adapter *adapter = priv->adapter;
275 276

	sscanf(buf, "%x", &monitor_mode);
277
	if (monitor_mode != LBS_MONITOR_OFF) {
278 279
		if(adapter->monitormode == monitor_mode)
			return strlen(buf);
280
		if (adapter->monitormode == LBS_MONITOR_OFF) {
281
			if (adapter->mode == IW_MODE_INFRA)
282
				lbs_send_deauthentication(priv);
283
			else if (adapter->mode == IW_MODE_ADHOC)
284 285
				lbs_stop_adhoc_network(priv);
			lbs_add_rtap(priv);
286 287 288 289 290
		}
		adapter->monitormode = monitor_mode;
	}

	else {
291
		if (adapter->monitormode == LBS_MONITOR_OFF)
292
			return strlen(buf);
293 294
		adapter->monitormode = LBS_MONITOR_OFF;
		lbs_remove_rtap(priv);
295 296 297 298
		netif_wake_queue(priv->dev);
		netif_wake_queue(priv->mesh_dev);
	}

299
	lbs_prepare_and_send_command(priv,
300 301 302 303 304 305
			CMD_802_11_MONITOR_MODE, CMD_ACT_SET,
			CMD_OPTION_WAITFORRSP, 0, &adapter->monitormode);
	return strlen(buf);
}

/**
306
 * lbs_rtap attribute to be exported per mshX interface
307 308
 * through sysfs (/sys/class/net/mshX/libertas-rtap)
 */
309 310
static DEVICE_ATTR(lbs_rtap, 0644, lbs_rtap_get,
		lbs_rtap_set );
311

312
/**
313 314
 * anycast_mask attribute to be exported per mshX interface
 * through sysfs (/sys/class/net/mshX/anycast_mask)
315
 */
316
static DEVICE_ATTR(anycast_mask, 0644, lbs_anycast_get, lbs_anycast_set);
317

318
static ssize_t lbs_autostart_enabled_get(struct device *dev,
319 320 321 322 323
		struct device_attribute *attr, char * buf)
{
	struct cmd_ds_mesh_access mesh_access;

	memset(&mesh_access, 0, sizeof(mesh_access));
324
	lbs_prepare_and_send_command(to_net_dev(dev)->priv,
325 326 327 328 329 330 331
			CMD_MESH_ACCESS,
			CMD_ACT_MESH_GET_AUTOSTART_ENABLED,
			CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);

	return sprintf(buf, "%d\n", le32_to_cpu(mesh_access.data[0]));
}

332
static ssize_t lbs_autostart_enabled_set(struct device *dev,
333 334 335 336
		struct device_attribute *attr, const char * buf, size_t count)
{
	struct cmd_ds_mesh_access mesh_access;
	uint32_t datum;
337
	struct lbs_private *priv = (to_net_dev(dev))->priv;
338
	int ret;
339 340 341 342 343

	memset(&mesh_access, 0, sizeof(mesh_access));
	sscanf(buf, "%d", &datum);
	mesh_access.data[0] = cpu_to_le32(datum);

344
	ret = lbs_prepare_and_send_command(priv,
345 346 347
			CMD_MESH_ACCESS,
			CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
			CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
348 349 350
	if (ret == 0)
		priv->mesh_autostart_enabled = datum ? 1 : 0;

351 352 353 354
	return strlen(buf);
}

static DEVICE_ATTR(autostart_enabled, 0644,
355
		lbs_autostart_enabled_get, lbs_autostart_enabled_set);
356

357
static struct attribute *lbs_mesh_sysfs_entries[] = {
358 359 360 361 362
	&dev_attr_anycast_mask.attr,
	&dev_attr_autostart_enabled.attr,
	NULL,
};

363 364
static struct attribute_group lbs_mesh_attr_group = {
	.attrs = lbs_mesh_sysfs_entries,
365 366
};

367 368 369 370 371 372 373 374 375 376 377
/**
 *  @brief Check if the device can be open and wait if necessary.
 *
 *  @param dev     A pointer to net_device structure
 *  @return 	   0
 *
 * For USB adapter, on some systems the device open handler will be
 * called before FW ready. Use the following flag check and wait
 * function to work around the issue.
 *
 */
378 379
static int pre_open_check(struct net_device *dev)
{
380 381
	struct lbs_private *priv = (struct lbs_private *) dev->priv;
	struct lbs_adapter *adapter = priv->adapter;
382 383 384 385 386 387 388
	int i = 0;

	while (!adapter->fw_ready && i < 20) {
		i++;
		msleep_interruptible(100);
	}
	if (!adapter->fw_ready) {
389
		lbs_pr_err("firmware not ready\n");
390 391 392 393 394 395 396 397 398 399 400 401
		return -1;
	}

	return 0;
}

/**
 *  @brief This function opens the device
 *
 *  @param dev     A pointer to net_device structure
 *  @return 	   0
 */
402
static int lbs_dev_open(struct net_device *dev)
403
{
404 405
	struct lbs_private *priv = (struct lbs_private *) dev->priv;
	struct lbs_adapter *adapter = priv->adapter;
406

407
	lbs_deb_enter(LBS_DEB_NET);
408 409 410

	priv->open = 1;

411
	if (adapter->connect_status == LBS_CONNECTED)
412
		netif_carrier_on(priv->dev);
413
	else
414
		netif_carrier_off(priv->dev);
415 416 417 418 419

	if (priv->mesh_dev) {
		if (adapter->mesh_connect_status == LBS_CONNECTED)
			netif_carrier_on(priv->mesh_dev);
		else
420
			netif_carrier_off(priv->mesh_dev);
421
	}
422

423
	lbs_deb_leave(LBS_DEB_NET);
424 425 426 427 428 429 430 431
	return 0;
}
/**
 *  @brief This function opens the mshX interface
 *
 *  @param dev     A pointer to net_device structure
 *  @return 	   0
 */
432
static int lbs_mesh_open(struct net_device *dev)
433
{
434
	struct lbs_private *priv = (struct lbs_private *) dev->priv ;
435

436
	if (pre_open_check(dev) == -1)
437 438
		return -1;
	priv->mesh_open = 1 ;
439
	netif_wake_queue(priv->mesh_dev);
440 441 442 443 444

	priv->adapter->mesh_connect_status = LBS_CONNECTED;

	netif_carrier_on(priv->mesh_dev);
	netif_wake_queue(priv->mesh_dev);
445
	if (priv->infra_open == 0)
446
		return lbs_dev_open(priv->dev) ;
447 448 449 450 451 452 453 454 455
	return 0;
}

/**
 *  @brief This function opens the ethX interface
 *
 *  @param dev     A pointer to net_device structure
 *  @return 	   0
 */
456
static int lbs_open(struct net_device *dev)
457
{
458
	struct lbs_private *priv = (struct lbs_private *) dev->priv ;
459 460 461 462

	if(pre_open_check(dev) == -1)
		return -1;
	priv->infra_open = 1 ;
463
	netif_wake_queue(priv->dev);
464
	if (priv->open == 0)
465
		return lbs_dev_open(priv->dev) ;
466 467 468
	return 0;
}

469
static int lbs_dev_close(struct net_device *dev)
470
{
471
	struct lbs_private *priv = dev->priv;
472

473
	lbs_deb_enter(LBS_DEB_NET);
474

475
	netif_carrier_off(priv->dev);
476 477
	priv->open = 0;

478
	lbs_deb_leave(LBS_DEB_NET);
479 480 481 482 483 484 485 486 487
	return 0;
}

/**
 *  @brief This function closes the mshX interface
 *
 *  @param dev     A pointer to net_device structure
 *  @return 	   0
 */
488
static int lbs_mesh_close(struct net_device *dev)
489
{
490
	struct lbs_private *priv = (struct lbs_private *) (dev->priv);
491 492 493 494

	priv->mesh_open = 0;
	netif_stop_queue(priv->mesh_dev);
	if (priv->infra_open == 0)
495
		return lbs_dev_close(dev);
496 497 498 499 500 501 502 503 504 505
	else
		return 0;
}

/**
 *  @brief This function closes the ethX interface
 *
 *  @param dev     A pointer to net_device structure
 *  @return 	   0
 */
506
static int lbs_close(struct net_device *dev)
507
{
508
	struct lbs_private *priv = (struct lbs_private *) dev->priv;
509

510
	netif_stop_queue(dev);
511 512
	priv->infra_open = 0;
	if (priv->mesh_open == 0)
513
		return lbs_dev_close(dev);
514 515 516 517 518
	else
		return 0;
}


519
static int lbs_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
520 521
{
	int ret = 0;
522
	struct lbs_private *priv = dev->priv;
523

524
	lbs_deb_enter(LBS_DEB_TX);
525

526
	if (priv->dnld_sent || priv->adapter->TxLockFlag) {
527 528 529 530
		priv->stats.tx_dropped++;
		goto done;
	}

531
	netif_stop_queue(priv->dev);
532 533
	if (priv->mesh_dev)
		netif_stop_queue(priv->mesh_dev);
534

535
	if (lbs_process_tx(priv, skb) == 0)
536 537
		dev->trans_start = jiffies;
done:
538
	lbs_deb_leave_args(LBS_DEB_TX, "ret %d", ret);
539 540 541 542
	return ret;
}

/**
543
 * @brief Mark mesh packets and handover them to lbs_hard_start_xmit
544 545
 *
 */
546
static int lbs_mesh_pre_start_xmit(struct sk_buff *skb,
547
		struct net_device *dev)
548
{
549
	struct lbs_private *priv = dev->priv;
550 551 552
	int ret;

	lbs_deb_enter(LBS_DEB_MESH);
553
	if (priv->adapter->monitormode != LBS_MONITOR_OFF) {
554 555 556
		netif_stop_queue(dev);
		return -EOPNOTSUPP;
	}
557

558 559
	SET_MESH_FRAME(skb);

560
	ret = lbs_hard_start_xmit(skb, priv->mesh_dev);
561 562
	lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
	return ret;
563 564 565
}

/**
566
 * @brief Mark non-mesh packets and handover them to lbs_hard_start_xmit
567 568
 *
 */
569
static int lbs_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
570
{
571
	struct lbs_private *priv = dev->priv;
572 573
	int ret;

574
	lbs_deb_enter(LBS_DEB_TX);
575

576
	if (priv->adapter->monitormode != LBS_MONITOR_OFF) {
577 578 579 580
		netif_stop_queue(dev);
		return -EOPNOTSUPP;
	}

581
	UNSET_MESH_FRAME(skb);
582

583
	ret = lbs_hard_start_xmit(skb, dev);
584
	lbs_deb_leave_args(LBS_DEB_TX, "ret %d", ret);
585
	return ret;
586 587
}

588
static void lbs_tx_timeout(struct net_device *dev)
589
{
590
	struct lbs_private *priv = (struct lbs_private *) dev->priv;
591

592
	lbs_deb_enter(LBS_DEB_TX);
593

594
	lbs_pr_err("tx watch dog timeout\n");
595

596
	priv->dnld_sent = DNLD_RES_RECEIVED;
597 598 599
	dev->trans_start = jiffies;

	if (priv->adapter->currenttxskb) {
600
		if (priv->adapter->monitormode != LBS_MONITOR_OFF) {
601 602 603
			/* If we are here, we have not received feedback from
			   the previous packet.  Assume TX_FAIL and move on. */
			priv->adapter->eventcause = 0x01000000;
604
			lbs_send_tx_feedback(priv);
605
		} else
606
			wake_up_interruptible(&priv->waitq);
607 608 609 610 611 612
	} else if (dev == priv->dev) {
		if (priv->adapter->connect_status == LBS_CONNECTED)
			netif_wake_queue(priv->dev);

	} else if (dev == priv->mesh_dev) {
		if (priv->adapter->mesh_connect_status == LBS_CONNECTED)
613
			netif_wake_queue(priv->mesh_dev);
614
	}
615

616
	lbs_deb_leave(LBS_DEB_TX);
617 618
}

619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
void lbs_host_to_card_done(struct lbs_private *priv)
{
	struct lbs_adapter *adapter = priv->adapter;

	priv->dnld_sent = DNLD_RES_RECEIVED;

	/* Wake main thread if commands are pending */
	if (!adapter->cur_cmd)
		wake_up_interruptible(&priv->waitq);

	if (priv->dev && adapter->connect_status == LBS_CONNECTED)
		netif_wake_queue(priv->dev);

	if (priv->mesh_dev && adapter->mesh_connect_status == LBS_CONNECTED)
		netif_wake_queue(priv->mesh_dev);
}
EXPORT_SYMBOL_GPL(lbs_host_to_card_done);

637 638 639
/**
 *  @brief This function returns the network statistics
 *
640
 *  @param dev     A pointer to struct lbs_private structure
641 642
 *  @return 	   A pointer to net_device_stats structure
 */
643
static struct net_device_stats *lbs_get_stats(struct net_device *dev)
644
{
645
	struct lbs_private *priv = (struct lbs_private *) dev->priv;
646 647 648 649

	return &priv->stats;
}

650
static int lbs_set_mac_address(struct net_device *dev, void *addr)
651 652
{
	int ret = 0;
653 654
	struct lbs_private *priv = (struct lbs_private *) dev->priv;
	struct lbs_adapter *adapter = priv->adapter;
655 656
	struct sockaddr *phwaddr = addr;

657
	lbs_deb_enter(LBS_DEB_NET);
658

659 660 661
	/* In case it was called from the mesh device */
	dev = priv->dev ;

662 663 664
	memset(adapter->current_addr, 0, ETH_ALEN);

	/* dev->dev_addr is 8 bytes */
665
	lbs_deb_hex(LBS_DEB_NET, "dev->dev_addr", dev->dev_addr, ETH_ALEN);
666

667
	lbs_deb_hex(LBS_DEB_NET, "addr", phwaddr->sa_data, ETH_ALEN);
668 669
	memcpy(adapter->current_addr, phwaddr->sa_data, ETH_ALEN);

670
	ret = lbs_prepare_and_send_command(priv, CMD_802_11_MAC_ADDRESS,
671 672
				    CMD_ACT_SET,
				    CMD_OPTION_WAITFORRSP, 0, NULL);
673 674

	if (ret) {
675
		lbs_deb_net("set MAC address failed\n");
676 677 678 679
		ret = -1;
		goto done;
	}

680
	lbs_deb_hex(LBS_DEB_NET, "adapter->macaddr", adapter->current_addr, ETH_ALEN);
681
	memcpy(dev->dev_addr, adapter->current_addr, ETH_ALEN);
682 683
	if (priv->mesh_dev)
		memcpy(priv->mesh_dev->dev_addr, adapter->current_addr, ETH_ALEN);
684 685

done:
686
	lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
687 688 689
	return ret;
}

690
static int lbs_copy_multicast_address(struct lbs_adapter *adapter,
691 692 693 694 695 696 697 698 699 700 701 702 703 704
				     struct net_device *dev)
{
	int i = 0;
	struct dev_mc_list *mcptr = dev->mc_list;

	for (i = 0; i < dev->mc_count; i++) {
		memcpy(&adapter->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
		mcptr = mcptr->next;
	}

	return i;

}

705
static void lbs_set_multicast_list(struct net_device *dev)
706
{
707 708
	struct lbs_private *priv = dev->priv;
	struct lbs_adapter *adapter = priv->adapter;
709
	int oldpacketfilter;
710
	DECLARE_MAC_BUF(mac);
711

712
	lbs_deb_enter(LBS_DEB_NET);
713 714 715 716

	oldpacketfilter = adapter->currentpacketfilter;

	if (dev->flags & IFF_PROMISC) {
717
		lbs_deb_net("enable promiscuous mode\n");
718
		adapter->currentpacketfilter |=
719
		    CMD_ACT_MAC_PROMISCUOUS_ENABLE;
720
		adapter->currentpacketfilter &=
721 722
		    ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
		      CMD_ACT_MAC_MULTICAST_ENABLE);
723 724 725
	} else {
		/* Multicast */
		adapter->currentpacketfilter &=
726
		    ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
727 728 729

		if (dev->flags & IFF_ALLMULTI || dev->mc_count >
		    MRVDRV_MAX_MULTICAST_LIST_SIZE) {
730
			lbs_deb_net( "enabling all multicast\n");
731
			adapter->currentpacketfilter |=
732
			    CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
733
			adapter->currentpacketfilter &=
734
			    ~CMD_ACT_MAC_MULTICAST_ENABLE;
735 736
		} else {
			adapter->currentpacketfilter &=
737
			    ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
738 739

			if (!dev->mc_count) {
740 741
				lbs_deb_net("no multicast addresses, "
				       "disabling multicast\n");
742
				adapter->currentpacketfilter &=
743
				    ~CMD_ACT_MAC_MULTICAST_ENABLE;
744 745 746 747
			} else {
				int i;

				adapter->currentpacketfilter |=
748
				    CMD_ACT_MAC_MULTICAST_ENABLE;
749 750

				adapter->nr_of_multicastmacaddr =
751
				    lbs_copy_multicast_address(adapter, dev);
752

753
				lbs_deb_net("multicast addresses: %d\n",
754 755 756
				       dev->mc_count);

				for (i = 0; i < dev->mc_count; i++) {
757 758 759
					lbs_deb_net("Multicast address %d:%s\n",
					       i, print_mac(mac,
					       adapter->multicastlist[i]));
760
				}
761
				/* send multicast addresses to firmware */
762
				lbs_prepare_and_send_command(priv,
763 764
						      CMD_MAC_MULTICAST_ADR,
						      CMD_ACT_SET, 0, 0,
765 766 767 768 769 770
						      NULL);
			}
		}
	}

	if (adapter->currentpacketfilter != oldpacketfilter) {
771
		lbs_set_mac_packet_filter(priv);
772 773
	}

774
	lbs_deb_leave(LBS_DEB_NET);
775 776 777
}

/**
778
 *  @brief This function handles the major jobs in the LBS driver.
779 780
 *  It handles all events generated by firmware, RX data received
 *  from firmware and TX data sent from kernel.
781
 *
782
 *  @param data    A pointer to lbs_thread structure
783 784
 *  @return 	   0
 */
785
static int lbs_thread(void *data)
786
{
787
	struct net_device *dev = data;
788 789
	struct lbs_private *priv = dev->priv;
	struct lbs_adapter *adapter = priv->adapter;
790 791 792
	wait_queue_t wait;
	u8 ireg = 0;

793
	lbs_deb_enter(LBS_DEB_THREAD);
794 795 796

	init_waitqueue_entry(&wait, current);

797
	set_freezable();
798
	for (;;) {
799
		lbs_deb_thread( "main-thread 111: intcounter=%d "
800 801
		       "currenttxskb=%p dnld_sent=%d\n",
		       adapter->intcounter,
802
		       adapter->currenttxskb, priv->dnld_sent);
803

804
		add_wait_queue(&priv->waitq, &wait);
805 806 807 808
		set_current_state(TASK_INTERRUPTIBLE);
		spin_lock_irq(&adapter->driver_lock);
		if ((adapter->psstate == PS_STATE_SLEEP) ||
		    (!adapter->intcounter
809
		     && (priv->dnld_sent || adapter->cur_cmd ||
810
			 list_empty(&adapter->cmdpendingq)))) {
811
			lbs_deb_thread(
812 813 814 815 816 817 818 819
			       "main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
			       adapter->connect_status, adapter->intcounter,
			       adapter->psmode, adapter->psstate);
			spin_unlock_irq(&adapter->driver_lock);
			schedule();
		} else
			spin_unlock_irq(&adapter->driver_lock);

820
		lbs_deb_thread(
821 822
		       "main-thread 222 (waking up): intcounter=%d currenttxskb=%p "
		       "dnld_sent=%d\n", adapter->intcounter,
823
		       adapter->currenttxskb, priv->dnld_sent);
824 825

		set_current_state(TASK_RUNNING);
826
		remove_wait_queue(&priv->waitq, &wait);
827 828
		try_to_freeze();

829
		lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p "
830 831
		       "dnld_sent=%d\n",
		       adapter->intcounter,
832
		       adapter->currenttxskb, priv->dnld_sent);
833 834 835

		if (kthread_should_stop()
		    || adapter->surpriseremoved) {
836
			lbs_deb_thread(
837 838 839 840 841 842 843 844 845 846
			       "main-thread: break from main thread: surpriseremoved=0x%x\n",
			       adapter->surpriseremoved);
			break;
		}


		spin_lock_irq(&adapter->driver_lock);
		if (adapter->intcounter) {
			u8 int_status;
			adapter->intcounter = 0;
847
			int_status = priv->hw_get_int_status(priv, &ireg);
848 849

			if (int_status) {
850
				lbs_deb_thread(
851 852 853 854 855 856 857
				       "main-thread: reading HOST_INT_STATUS_REG failed\n");
				spin_unlock_irq(&adapter->driver_lock);
				continue;
			}
			adapter->hisregcpy |= ireg;
		}

858
		lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p "
859 860
		       "dnld_sent=%d\n",
		       adapter->intcounter,
861
		       adapter->currenttxskb, priv->dnld_sent);
862 863

		/* command response? */
864
		if (adapter->hisregcpy & MRVDRV_CMD_UPLD_RDY) {
865
			lbs_deb_thread("main-thread: cmd response ready\n");
866

867
			adapter->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
868
			spin_unlock_irq(&adapter->driver_lock);
869
			lbs_process_rx_command(priv);
870 871 872 873
			spin_lock_irq(&adapter->driver_lock);
		}

		/* Any Card Event */
874
		if (adapter->hisregcpy & MRVDRV_CARDEVENT) {
875
			lbs_deb_thread("main-thread: Card Event Activity\n");
876

877
			adapter->hisregcpy &= ~MRVDRV_CARDEVENT;
878

879
			if (priv->hw_read_event_cause(priv)) {
880
				lbs_pr_alert(
881
				       "main-thread: hw_read_event_cause failed\n");
882 883 884 885
				spin_unlock_irq(&adapter->driver_lock);
				continue;
			}
			spin_unlock_irq(&adapter->driver_lock);
886
			lbs_process_event(priv);
887 888 889 890 891
		} else
			spin_unlock_irq(&adapter->driver_lock);

		/* Check if we need to confirm Sleep Request received previously */
		if (adapter->psstate == PS_STATE_PRE_SLEEP) {
892
			if (!priv->dnld_sent && !adapter->cur_cmd) {
893
				if (adapter->connect_status ==
894
				    LBS_CONNECTED) {
895
					lbs_deb_thread(
896 897 898 899
					       "main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p "
					       "dnld_sent=%d cur_cmd=%p, confirm now\n",
					       adapter->intcounter,
					       adapter->currenttxskb,
900
					       priv->dnld_sent,
901 902
					       adapter->cur_cmd);

903
					lbs_ps_confirm_sleep(priv,
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
						       (u16) adapter->psmode);
				} else {
					/* workaround for firmware sending
					 * deauth/linkloss event immediately
					 * after sleep request, remove this
					 * after firmware fixes it
					 */
					adapter->psstate = PS_STATE_AWAKE;
					lbs_pr_alert(
					       "main-thread: ignore PS_SleepConfirm in non-connected state\n");
				}
			}
		}

		/* The PS state is changed during processing of Sleep Request
		 * event above
		 */
		if ((priv->adapter->psstate == PS_STATE_SLEEP) ||
		    (priv->adapter->psstate == PS_STATE_PRE_SLEEP))
			continue;

		/* Execute the next command */
926
		if (!priv->dnld_sent && !priv->adapter->cur_cmd)
927
			lbs_execute_next_command(priv);
928 929

		/* Wake-up command waiters which can't sleep in
930
		 * lbs_prepare_and_send_command
931
		 */
932
		if (!list_empty(&adapter->cmdpendingq))
933 934
			wake_up_all(&adapter->cmd_pending);

935
		lbs_tx_runqueue(priv);
936 937 938 939 940
	}

	del_timer(&adapter->command_timer);
	wake_up_all(&adapter->cmd_pending);

941
	lbs_deb_leave(LBS_DEB_THREAD);
942 943 944
	return 0;
}

H
Holger Schurig 已提交
945 946 947 948 949
/**
 *  @brief This function downloads firmware image, gets
 *  HW spec from firmware and set basic parameters to
 *  firmware.
 *
950
 *  @param priv    A pointer to struct lbs_private structure
H
Holger Schurig 已提交
951 952
 *  @return 	   0 or -1
 */
953
static int lbs_setup_firmware(struct lbs_private *priv)
H
Holger Schurig 已提交
954 955
{
	int ret = -1;
956
	struct lbs_adapter *adapter = priv->adapter;
957
	struct cmd_ds_mesh_access mesh_access;
H
Holger Schurig 已提交
958 959 960 961 962 963 964 965

	lbs_deb_enter(LBS_DEB_FW);

	/*
	 * Read MAC address from HW
	 */
	memset(adapter->current_addr, 0xff, ETH_ALEN);

966
	ret = lbs_prepare_and_send_command(priv, CMD_GET_HW_SPEC,
H
Holger Schurig 已提交
967 968 969 970 971 972 973
				    0, CMD_OPTION_WAITFORRSP, 0, NULL);

	if (ret) {
		ret = -1;
		goto done;
	}

974
	lbs_set_mac_packet_filter(priv);
H
Holger Schurig 已提交
975 976

	/* Get the supported Data rates */
977
	ret = lbs_prepare_and_send_command(priv, CMD_802_11_DATA_RATE,
H
Holger Schurig 已提交
978 979 980 981 982 983 984 985
				    CMD_ACT_GET_TX_RATE,
				    CMD_OPTION_WAITFORRSP, 0, NULL);

	if (ret) {
		ret = -1;
		goto done;
	}

986 987 988 989
	/* Disable mesh autostart */
	if (priv->mesh_dev) {
		memset(&mesh_access, 0, sizeof(mesh_access));
		mesh_access.data[0] = cpu_to_le32(0);
990
		ret = lbs_prepare_and_send_command(priv,
991 992 993 994 995 996 997 998 999 1000
				CMD_MESH_ACCESS,
				CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
				CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
		if (ret) {
			ret = -1;
			goto done;
		}
		priv->mesh_autostart_enabled = 0;
	}

H
Holger Schurig 已提交
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
	ret = 0;
done:
	lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
	return ret;
}

/**
 *  This function handles the timeout of command sending.
 *  It will re-send the same command again.
 */
static void command_timer_fn(unsigned long data)
{
1013 1014
	struct lbs_private *priv = (struct lbs_private *)data;
	struct lbs_adapter *adapter = priv->adapter;
H
Holger Schurig 已提交
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
	struct cmd_ctrl_node *ptempnode;
	struct cmd_ds_command *cmd;
	unsigned long flags;

	ptempnode = adapter->cur_cmd;
	if (ptempnode == NULL) {
		lbs_deb_fw("ptempnode empty\n");
		return;
	}

	cmd = (struct cmd_ds_command *)ptempnode->bufvirtualaddr;
	if (!cmd) {
		lbs_deb_fw("cmd is NULL\n");
		return;
	}

	lbs_deb_fw("command_timer_fn fired, cmd %x\n", cmd->command);

	if (!adapter->fw_ready)
		return;

	spin_lock_irqsave(&adapter->driver_lock, flags);
	adapter->cur_cmd = NULL;
	spin_unlock_irqrestore(&adapter->driver_lock, flags);

	lbs_deb_fw("re-sending same command because of timeout\n");
1041
	lbs_queue_cmd(adapter, ptempnode, 0);
H
Holger Schurig 已提交
1042 1043 1044 1045 1046 1047

	wake_up_interruptible(&priv->waitq);

	return;
}

1048
static int lbs_init_adapter(struct lbs_private *priv)
1049
{
1050
	struct lbs_adapter *adapter = priv->adapter;
H
Holger Schurig 已提交
1051
	size_t bufsize;
1052
	int i, ret = 0;
H
Holger Schurig 已提交
1053 1054 1055 1056 1057 1058

	/* Allocate buffer to store the BSSID list */
	bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
	adapter->networks = kzalloc(bufsize, GFP_KERNEL);
	if (!adapter->networks) {
		lbs_pr_err("Out of memory allocating beacons\n");
1059 1060
		ret = -1;
		goto out;
H
Holger Schurig 已提交
1061 1062
	}

1063 1064 1065 1066 1067 1068 1069
	/* Initialize scan result lists */
	INIT_LIST_HEAD(&adapter->network_free_list);
	INIT_LIST_HEAD(&adapter->network_list);
	for (i = 0; i < MAX_NETWORK_COUNT; i++) {
		list_add_tail(&adapter->networks[i].list,
			      &adapter->network_free_list);
	}
H
Holger Schurig 已提交
1070

1071 1072
	adapter->lbs_ps_confirm_sleep.seqnum = cpu_to_le16(++adapter->seqnum);
	adapter->lbs_ps_confirm_sleep.command =
H
Holger Schurig 已提交
1073
	    cpu_to_le16(CMD_802_11_PS_MODE);
1074
	adapter->lbs_ps_confirm_sleep.size =
H
Holger Schurig 已提交
1075
	    cpu_to_le16(sizeof(struct PS_CMD_ConfirmSleep));
1076
	adapter->lbs_ps_confirm_sleep.action =
H
Holger Schurig 已提交
1077 1078 1079 1080
	    cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);

	memset(adapter->current_addr, 0xff, ETH_ALEN);

1081
	adapter->connect_status = LBS_DISCONNECTED;
1082
	adapter->mesh_connect_status = LBS_DISCONNECTED;
H
Holger Schurig 已提交
1083 1084 1085
	adapter->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
	adapter->mode = IW_MODE_INFRA;
	adapter->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
1086
	adapter->currentpacketfilter = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
H
Holger Schurig 已提交
1087 1088 1089
	adapter->radioon = RADIO_ON;
	adapter->auto_rate = 1;
	adapter->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
1090
	adapter->psmode = LBS802_11POWERMODECAM;
H
Holger Schurig 已提交
1091 1092
	adapter->psstate = PS_STATE_FULL_POWER;

1093
	mutex_init(&adapter->lock);
H
Holger Schurig 已提交
1094 1095 1096 1097 1098

	memset(&adapter->tx_queue_ps, 0, NR_TX_QUEUE*sizeof(struct sk_buff*));
	adapter->tx_queue_idx = 0;
	spin_lock_init(&adapter->txqueue_lock);

1099 1100
	setup_timer(&adapter->command_timer, command_timer_fn,
	            (unsigned long)priv);
H
Holger Schurig 已提交
1101

1102 1103
	INIT_LIST_HEAD(&adapter->cmdfreeq);
	INIT_LIST_HEAD(&adapter->cmdpendingq);
H
Holger Schurig 已提交
1104

1105 1106
	spin_lock_init(&adapter->driver_lock);
	init_waitqueue_head(&adapter->cmd_pending);
H
Holger Schurig 已提交
1107

1108
	/* Allocate the command buffers */
1109
	if (lbs_allocate_cmd_buffer(priv)) {
1110 1111 1112
		lbs_pr_err("Out of memory allocating command buffers\n");
		ret = -1;
	}
H
Holger Schurig 已提交
1113

1114 1115 1116
out:
	return ret;
}
H
Holger Schurig 已提交
1117

1118
static void lbs_free_adapter(struct lbs_private *priv)
1119
{
1120
	struct lbs_adapter *adapter = priv->adapter;
H
Holger Schurig 已提交
1121

1122 1123 1124
	if (!adapter) {
		lbs_deb_fw("why double free adapter?\n");
		return;
H
Holger Schurig 已提交
1125 1126
	}

1127
	lbs_deb_fw("free command buffer\n");
1128
	lbs_free_cmd_buffer(priv);
H
Holger Schurig 已提交
1129

1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
	lbs_deb_fw("free command_timer\n");
	del_timer(&adapter->command_timer);

	lbs_deb_fw("free scan results table\n");
	kfree(adapter->networks);
	adapter->networks = NULL;

	/* Free the adapter object itself */
	lbs_deb_fw("free adapter\n");
	kfree(adapter);
	priv->adapter = NULL;
H
Holger Schurig 已提交
1141 1142
}

1143 1144
/**
 * @brief This function adds the card. it will probe the
1145
 * card, allocate the lbs_priv and initialize the device.
1146 1147
 *
 *  @param card    A pointer to card
1148
 *  @return 	   A pointer to struct lbs_private structure
1149
 */
1150
struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
1151 1152
{
	struct net_device *dev = NULL;
1153
	struct lbs_private *priv = NULL;
1154

1155
	lbs_deb_enter(LBS_DEB_NET);
1156 1157

	/* Allocate an Ethernet device and register it */
1158 1159
	dev = alloc_etherdev(sizeof(struct lbs_private));
	if (!dev) {
1160
		lbs_pr_err("init ethX device failed\n");
1161
		goto done;
1162
	}
1163
	priv = dev->priv;
1164

1165 1166 1167 1168
	/* allocate buffer for struct lbs_adapter */
	priv->adapter = kzalloc(sizeof(struct lbs_adapter), GFP_KERNEL);
	if (!priv->adapter) {
		lbs_pr_err("allocate buffer for struct lbs_adapter failed\n");
1169
		goto err_kzalloc;
1170 1171
	}

1172
	if (lbs_init_adapter(priv)) {
1173 1174 1175 1176
		lbs_pr_err("failed to initialize adapter structure.\n");
		goto err_init_adapter;
	}

1177 1178
	priv->dev = dev;
	priv->card = card;
1179 1180
	priv->mesh_open = 0;
	priv->infra_open = 0;
1181
	priv->hotplug_device = dmdev;
1182 1183

	/* Setup the OS Interface to our functions */
1184 1185 1186 1187 1188 1189
	dev->open = lbs_open;
	dev->hard_start_xmit = lbs_pre_start_xmit;
	dev->stop = lbs_close;
	dev->set_mac_address = lbs_set_mac_address;
	dev->tx_timeout = lbs_tx_timeout;
	dev->get_stats = lbs_get_stats;
1190
	dev->watchdog_timeo = 5 * HZ;
1191
	dev->ethtool_ops = &lbs_ethtool_ops;
1192
#ifdef	WIRELESS_EXT
1193
	dev->wireless_handlers = (struct iw_handler_def *)&lbs_handler_def;
1194 1195
#endif
	dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
1196
	dev->set_multicast_list = lbs_set_multicast_list;
1197

1198 1199
	SET_NETDEV_DEV(dev, dmdev);

1200
	priv->rtap_net_dev = NULL;
1201
	if (device_create_file(dmdev, &dev_attr_lbs_rtap))
1202 1203 1204 1205
		goto err_init_adapter;

	lbs_deb_thread("Starting main thread...\n");
	init_waitqueue_head(&priv->waitq);
1206
	priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
1207 1208 1209 1210 1211
	if (IS_ERR(priv->main_thread)) {
		lbs_deb_thread("Error creating main thread.\n");
		goto err_kthread_run;
	}

1212 1213 1214 1215
	priv->work_thread = create_singlethread_workqueue("lbs_worker");
	INIT_DELAYED_WORK(&priv->assoc_work, lbs_association_worker);
	INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
	INIT_WORK(&priv->sync_channel, lbs_sync_channel);
1216

1217 1218
	goto done;

1219
err_kthread_run:
1220
	device_remove_file(dmdev, &dev_attr_lbs_rtap);
1221 1222

err_init_adapter:
1223
	lbs_free_adapter(priv);
1224

1225 1226
err_kzalloc:
	free_netdev(dev);
1227
	priv = NULL;
1228

1229 1230 1231 1232
done:
	lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
	return priv;
}
1233
EXPORT_SYMBOL_GPL(lbs_add_card);
1234

1235

1236
int lbs_remove_card(struct lbs_private *priv)
1237
{
1238
	struct lbs_adapter *adapter = priv->adapter;
1239
	struct net_device *dev = priv->dev;
1240
	union iwreq_data wrqu;
1241 1242

	lbs_deb_enter(LBS_DEB_MAIN);
1243

1244
	lbs_remove_rtap(priv);
1245

1246
	dev = priv->dev;
1247
	device_remove_file(priv->hotplug_device, &dev_attr_lbs_rtap);
1248

1249 1250 1251
	cancel_delayed_work(&priv->scan_work);
	cancel_delayed_work(&priv->assoc_work);
	destroy_workqueue(priv->work_thread);
1252

1253 1254 1255
	if (adapter->psmode == LBS802_11POWERMODEMAX_PSP) {
		adapter->psmode = LBS802_11POWERMODECAM;
		lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
1256 1257
	}

1258 1259 1260 1261 1262 1263 1264 1265
	memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
	wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);

	/* Stop the thread servicing the interrupts */
	adapter->surpriseremoved = 1;
	kthread_stop(priv->main_thread);

1266
	lbs_free_adapter(priv);
1267 1268 1269 1270 1271 1272 1273

	priv->dev = NULL;
	free_netdev(dev);

	lbs_deb_leave(LBS_DEB_MAIN);
	return 0;
}
1274
EXPORT_SYMBOL_GPL(lbs_remove_card);
1275 1276


1277
int lbs_start_card(struct lbs_private *priv)
1278 1279 1280 1281 1282 1283 1284
{
	struct net_device *dev = priv->dev;
	int ret = -1;

	lbs_deb_enter(LBS_DEB_MAIN);

	/* poke the firmware */
1285
	ret = lbs_setup_firmware(priv);
1286 1287 1288 1289
	if (ret)
		goto done;

	/* init 802.11d */
1290
	lbs_init_11d(priv);
1291 1292

	if (register_netdev(dev)) {
1293
		lbs_pr_err("cannot register ethX device\n");
1294
		goto done;
1295 1296
	}

1297
	lbs_debugfs_init_one(priv, dev);
1298

1299 1300
	lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);

1301
	ret = 0;
1302

1303
done:
1304 1305 1306
	lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
	return ret;
}
1307
EXPORT_SYMBOL_GPL(lbs_start_card);
1308 1309


1310
int lbs_stop_card(struct lbs_private *priv)
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
{
	struct net_device *dev = priv->dev;
	int ret = -1;
	struct cmd_ctrl_node *cmdnode;
	unsigned long flags;

	lbs_deb_enter(LBS_DEB_MAIN);

	netif_stop_queue(priv->dev);
	netif_carrier_off(priv->dev);

1322
	lbs_debugfs_remove_one(priv);
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334

	/* Flush pending command nodes */
	spin_lock_irqsave(&priv->adapter->driver_lock, flags);
	list_for_each_entry(cmdnode, &priv->adapter->cmdpendingq, list) {
		cmdnode->cmdwaitqwoken = 1;
		wake_up_interruptible(&cmdnode->cmdwait_q);
	}
	spin_unlock_irqrestore(&priv->adapter->driver_lock, flags);

	unregister_netdev(dev);

	lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1335
	return ret;
1336
}
1337
EXPORT_SYMBOL_GPL(lbs_stop_card);
1338

1339

1340 1341 1342
/**
 * @brief This function adds mshX interface
 *
1343
 *  @param priv    A pointer to the struct lbs_private structure
1344 1345
 *  @return 	   0 if successful, -X otherwise
 */
1346
int lbs_add_mesh(struct lbs_private *priv, struct device *dev)
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
{
	struct net_device *mesh_dev = NULL;
	int ret = 0;

	lbs_deb_enter(LBS_DEB_MESH);

	/* Allocate a virtual mesh device */
	if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
		lbs_deb_mesh("init mshX device failed\n");
		ret = -ENOMEM;
		goto done;
	}
	mesh_dev->priv = priv;
	priv->mesh_dev = mesh_dev;

1362 1363 1364 1365 1366 1367
	mesh_dev->open = lbs_mesh_open;
	mesh_dev->hard_start_xmit = lbs_mesh_pre_start_xmit;
	mesh_dev->stop = lbs_mesh_close;
	mesh_dev->get_stats = lbs_get_stats;
	mesh_dev->set_mac_address = lbs_set_mac_address;
	mesh_dev->ethtool_ops = &lbs_ethtool_ops;
1368 1369
	memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
			sizeof(priv->dev->dev_addr));
1370

1371 1372
	SET_NETDEV_DEV(priv->mesh_dev, dev);

1373
#ifdef	WIRELESS_EXT
1374
	mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
1375 1376 1377 1378 1379 1380 1381 1382
#endif
	/* Register virtual mesh interface */
	ret = register_netdev(mesh_dev);
	if (ret) {
		lbs_pr_err("cannot register mshX virtual interface\n");
		goto err_free;
	}

1383
	ret = sysfs_create_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
	if (ret)
		goto err_unregister;

	/* Everything successful */
	ret = 0;
	goto done;

err_unregister:
	unregister_netdev(mesh_dev);

err_free:
	free_netdev(mesh_dev);

done:
	lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
	return ret;
}
1401
EXPORT_SYMBOL_GPL(lbs_add_mesh);
1402

1403

1404
void lbs_remove_mesh(struct lbs_private *priv)
1405 1406 1407
{
	struct net_device *mesh_dev;

1408
	lbs_deb_enter(LBS_DEB_MAIN);
1409 1410 1411 1412 1413 1414 1415

	if (!priv)
		goto out;

	mesh_dev = priv->mesh_dev;

	netif_stop_queue(mesh_dev);
1416
	netif_carrier_off(priv->mesh_dev);
1417

1418
	sysfs_remove_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
1419 1420 1421 1422 1423 1424
	unregister_netdev(mesh_dev);

	priv->mesh_dev = NULL ;
	free_netdev(mesh_dev);

out:
1425
	lbs_deb_leave(LBS_DEB_MAIN);
1426
}
1427
EXPORT_SYMBOL_GPL(lbs_remove_mesh);
1428

1429 1430 1431 1432 1433 1434 1435 1436 1437
/**
 *  @brief This function finds the CFP in
 *  region_cfp_table based on region and band parameter.
 *
 *  @param region  The region code
 *  @param band	   The band
 *  @param cfp_no  A pointer to CFP number
 *  @return 	   A pointer to CFP
 */
1438
struct chan_freq_power *lbs_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
1439 1440 1441
{
	int i, end;

1442
	lbs_deb_enter(LBS_DEB_MAIN);
1443

1444
	end = ARRAY_SIZE(region_cfp_table);
1445 1446

	for (i = 0; i < end ; i++) {
1447
		lbs_deb_main("region_cfp_table[i].region=%d\n",
1448 1449 1450
			region_cfp_table[i].region);
		if (region_cfp_table[i].region == region) {
			*cfp_no = region_cfp_table[i].cfp_no_BG;
1451
			lbs_deb_leave(LBS_DEB_MAIN);
1452 1453 1454 1455
			return region_cfp_table[i].cfp_BG;
		}
	}

1456
	lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
1457 1458 1459
	return NULL;
}

1460
int lbs_set_regiontable(struct lbs_private *priv, u8 region, u8 band)
1461
{
1462
	struct lbs_adapter *adapter = priv->adapter;
1463
	int ret = 0;
1464 1465 1466 1467 1468
	int i = 0;

	struct chan_freq_power *cfp;
	int cfp_no;

1469
	lbs_deb_enter(LBS_DEB_MAIN);
1470 1471 1472 1473

	memset(adapter->region_channel, 0, sizeof(adapter->region_channel));

	{
1474
		cfp = lbs_get_region_cfp_table(region, band, &cfp_no);
1475 1476 1477 1478
		if (cfp != NULL) {
			adapter->region_channel[i].nrcfp = cfp_no;
			adapter->region_channel[i].CFP = cfp;
		} else {
1479
			lbs_deb_main("wrong region code %#x in band B/G\n",
1480
			       region);
1481 1482
			ret = -1;
			goto out;
1483 1484 1485 1486 1487 1488
		}
		adapter->region_channel[i].valid = 1;
		adapter->region_channel[i].region = region;
		adapter->region_channel[i].band = band;
		i++;
	}
1489 1490 1491
out:
	lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
	return ret;
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
}

/**
 *  @brief This function handles the interrupt. it will change PS
 *  state if applicable. it will wake up main_thread to handle
 *  the interrupt event as well.
 *
 *  @param dev     A pointer to net_device structure
 *  @return 	   n/a
 */
1502
void lbs_interrupt(struct net_device *dev)
1503
{
1504
	struct lbs_private *priv = dev->priv;
1505

1506
	lbs_deb_enter(LBS_DEB_THREAD);
1507

1508
	lbs_deb_thread("lbs_interrupt: intcounter=%d\n",
1509 1510 1511 1512 1513 1514 1515
	       priv->adapter->intcounter);

	priv->adapter->intcounter++;

	if (priv->adapter->psstate == PS_STATE_SLEEP) {
		priv->adapter->psstate = PS_STATE_AWAKE;
		netif_wake_queue(dev);
1516 1517
		if (priv->mesh_dev)
			netif_wake_queue(priv->mesh_dev);
1518 1519
	}

1520
	wake_up_interruptible(&priv->waitq);
1521

1522
	lbs_deb_leave(LBS_DEB_THREAD);
1523
}
1524
EXPORT_SYMBOL_GPL(lbs_interrupt);
1525

1526
int lbs_reset_device(struct lbs_private *priv)
1527 1528 1529 1530
{
	int ret;

	lbs_deb_enter(LBS_DEB_MAIN);
1531
	ret = lbs_prepare_and_send_command(priv, CMD_802_11_RESET,
1532 1533 1534 1535 1536 1537
				    CMD_ACT_HALT, 0, 0, NULL);
	msleep_interruptible(10);

	lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
	return ret;
}
1538
EXPORT_SYMBOL_GPL(lbs_reset_device);
1539

1540
static int __init lbs_init_module(void)
1541
{
1542
	lbs_deb_enter(LBS_DEB_MAIN);
1543
	lbs_debugfs_init();
1544 1545
	lbs_deb_leave(LBS_DEB_MAIN);
	return 0;
1546 1547
}

1548
static void __exit lbs_exit_module(void)
1549
{
1550
	lbs_deb_enter(LBS_DEB_MAIN);
1551

1552
	lbs_debugfs_remove();
1553

1554
	lbs_deb_leave(LBS_DEB_MAIN);
1555 1556
}

1557 1558 1559 1560
/*
 * rtap interface support fuctions
 */

1561
static int lbs_rtap_open(struct net_device *dev)
1562 1563 1564 1565 1566 1567
{
        netif_carrier_off(dev);
        netif_stop_queue(dev);
        return 0;
}

1568
static int lbs_rtap_stop(struct net_device *dev)
1569 1570 1571 1572
{
        return 0;
}

1573
static int lbs_rtap_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1574 1575 1576 1577 1578
{
        netif_stop_queue(dev);
        return -EOPNOTSUPP;
}

1579
static struct net_device_stats *lbs_rtap_get_stats(struct net_device *dev)
1580
{
1581
	struct lbs_private *priv = dev->priv;
1582 1583 1584 1585
	return &priv->ieee->stats;
}


1586
void lbs_remove_rtap(struct lbs_private *priv)
1587 1588 1589 1590 1591 1592 1593 1594
{
	if (priv->rtap_net_dev == NULL)
		return;
	unregister_netdev(priv->rtap_net_dev);
	free_ieee80211(priv->rtap_net_dev);
	priv->rtap_net_dev = NULL;
}

1595
int lbs_add_rtap(struct lbs_private *priv)
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
{
	int rc = 0;

	if (priv->rtap_net_dev)
		return -EPERM;

	priv->rtap_net_dev = alloc_ieee80211(0);
	if (priv->rtap_net_dev == NULL)
		return -ENOMEM;


	priv->ieee = netdev_priv(priv->rtap_net_dev);

	strcpy(priv->rtap_net_dev->name, "rtap%d");

	priv->rtap_net_dev->type = ARPHRD_IEEE80211_RADIOTAP;
1612 1613 1614 1615 1616
	priv->rtap_net_dev->open = lbs_rtap_open;
	priv->rtap_net_dev->stop = lbs_rtap_stop;
	priv->rtap_net_dev->get_stats = lbs_rtap_get_stats;
	priv->rtap_net_dev->hard_start_xmit = lbs_rtap_hard_start_xmit;
	priv->rtap_net_dev->set_multicast_list = lbs_set_multicast_list;
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
	priv->rtap_net_dev->priv = priv;

	priv->ieee->iw_mode = IW_MODE_MONITOR;

	rc = register_netdev(priv->rtap_net_dev);
	if (rc) {
		free_ieee80211(priv->rtap_net_dev);
		priv->rtap_net_dev = NULL;
		return rc;
	}

	return 0;
}


1632 1633
module_init(lbs_init_module);
module_exit(lbs_exit_module);
1634

1635
MODULE_DESCRIPTION("Libertas WLAN Driver Library");
1636 1637
MODULE_AUTHOR("Marvell International Ltd.");
MODULE_LICENSE("GPL");