main.c 39.3 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
}

/**
 *  @brief This function returns the network statistics
 *
622
 *  @param dev     A pointer to struct lbs_private structure
623 624
 *  @return 	   A pointer to net_device_stats structure
 */
625
static struct net_device_stats *lbs_get_stats(struct net_device *dev)
626
{
627
	struct lbs_private *priv = (struct lbs_private *) dev->priv;
628 629 630 631

	return &priv->stats;
}

632
static int lbs_set_mac_address(struct net_device *dev, void *addr)
633 634
{
	int ret = 0;
635 636
	struct lbs_private *priv = (struct lbs_private *) dev->priv;
	struct lbs_adapter *adapter = priv->adapter;
637 638
	struct sockaddr *phwaddr = addr;

639
	lbs_deb_enter(LBS_DEB_NET);
640

641 642 643
	/* In case it was called from the mesh device */
	dev = priv->dev ;

644 645 646
	memset(adapter->current_addr, 0, ETH_ALEN);

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

649
	lbs_deb_hex(LBS_DEB_NET, "addr", phwaddr->sa_data, ETH_ALEN);
650 651
	memcpy(adapter->current_addr, phwaddr->sa_data, ETH_ALEN);

652
	ret = lbs_prepare_and_send_command(priv, CMD_802_11_MAC_ADDRESS,
653 654
				    CMD_ACT_SET,
				    CMD_OPTION_WAITFORRSP, 0, NULL);
655 656

	if (ret) {
657
		lbs_deb_net("set MAC address failed\n");
658 659 660 661
		ret = -1;
		goto done;
	}

662
	lbs_deb_hex(LBS_DEB_NET, "adapter->macaddr", adapter->current_addr, ETH_ALEN);
663
	memcpy(dev->dev_addr, adapter->current_addr, ETH_ALEN);
664 665
	if (priv->mesh_dev)
		memcpy(priv->mesh_dev->dev_addr, adapter->current_addr, ETH_ALEN);
666 667

done:
668
	lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
669 670 671
	return ret;
}

672
static int lbs_copy_multicast_address(struct lbs_adapter *adapter,
673 674 675 676 677 678 679 680 681 682 683 684 685 686
				     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;

}

687
static void lbs_set_multicast_list(struct net_device *dev)
688
{
689 690
	struct lbs_private *priv = dev->priv;
	struct lbs_adapter *adapter = priv->adapter;
691
	int oldpacketfilter;
692
	DECLARE_MAC_BUF(mac);
693

694
	lbs_deb_enter(LBS_DEB_NET);
695 696 697 698

	oldpacketfilter = adapter->currentpacketfilter;

	if (dev->flags & IFF_PROMISC) {
699
		lbs_deb_net("enable promiscuous mode\n");
700
		adapter->currentpacketfilter |=
701
		    CMD_ACT_MAC_PROMISCUOUS_ENABLE;
702
		adapter->currentpacketfilter &=
703 704
		    ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
		      CMD_ACT_MAC_MULTICAST_ENABLE);
705 706 707
	} else {
		/* Multicast */
		adapter->currentpacketfilter &=
708
		    ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
709 710 711

		if (dev->flags & IFF_ALLMULTI || dev->mc_count >
		    MRVDRV_MAX_MULTICAST_LIST_SIZE) {
712
			lbs_deb_net( "enabling all multicast\n");
713
			adapter->currentpacketfilter |=
714
			    CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
715
			adapter->currentpacketfilter &=
716
			    ~CMD_ACT_MAC_MULTICAST_ENABLE;
717 718
		} else {
			adapter->currentpacketfilter &=
719
			    ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
720 721

			if (!dev->mc_count) {
722 723
				lbs_deb_net("no multicast addresses, "
				       "disabling multicast\n");
724
				adapter->currentpacketfilter &=
725
				    ~CMD_ACT_MAC_MULTICAST_ENABLE;
726 727 728 729
			} else {
				int i;

				adapter->currentpacketfilter |=
730
				    CMD_ACT_MAC_MULTICAST_ENABLE;
731 732

				adapter->nr_of_multicastmacaddr =
733
				    lbs_copy_multicast_address(adapter, dev);
734

735
				lbs_deb_net("multicast addresses: %d\n",
736 737 738
				       dev->mc_count);

				for (i = 0; i < dev->mc_count; i++) {
739 740 741
					lbs_deb_net("Multicast address %d:%s\n",
					       i, print_mac(mac,
					       adapter->multicastlist[i]));
742
				}
743
				/* send multicast addresses to firmware */
744
				lbs_prepare_and_send_command(priv,
745 746
						      CMD_MAC_MULTICAST_ADR,
						      CMD_ACT_SET, 0, 0,
747 748 749 750 751 752
						      NULL);
			}
		}
	}

	if (adapter->currentpacketfilter != oldpacketfilter) {
753
		lbs_set_mac_packet_filter(priv);
754 755
	}

756
	lbs_deb_leave(LBS_DEB_NET);
757 758 759
}

/**
760
 *  @brief This function handles the major jobs in the LBS driver.
761 762
 *  It handles all events generated by firmware, RX data received
 *  from firmware and TX data sent from kernel.
763
 *
764
 *  @param data    A pointer to lbs_thread structure
765 766
 *  @return 	   0
 */
767
static int lbs_thread(void *data)
768
{
769
	struct net_device *dev = data;
770 771
	struct lbs_private *priv = dev->priv;
	struct lbs_adapter *adapter = priv->adapter;
772 773 774
	wait_queue_t wait;
	u8 ireg = 0;

775
	lbs_deb_enter(LBS_DEB_THREAD);
776 777 778

	init_waitqueue_entry(&wait, current);

779
	set_freezable();
780
	for (;;) {
781
		lbs_deb_thread( "main-thread 111: intcounter=%d "
782 783
		       "currenttxskb=%p dnld_sent=%d\n",
		       adapter->intcounter,
784
		       adapter->currenttxskb, priv->dnld_sent);
785

786
		add_wait_queue(&priv->waitq, &wait);
787 788 789 790
		set_current_state(TASK_INTERRUPTIBLE);
		spin_lock_irq(&adapter->driver_lock);
		if ((adapter->psstate == PS_STATE_SLEEP) ||
		    (!adapter->intcounter
791
		     && (priv->dnld_sent || adapter->cur_cmd ||
792
			 list_empty(&adapter->cmdpendingq)))) {
793
			lbs_deb_thread(
794 795 796 797 798 799 800 801
			       "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);

802
		lbs_deb_thread(
803 804
		       "main-thread 222 (waking up): intcounter=%d currenttxskb=%p "
		       "dnld_sent=%d\n", adapter->intcounter,
805
		       adapter->currenttxskb, priv->dnld_sent);
806 807

		set_current_state(TASK_RUNNING);
808
		remove_wait_queue(&priv->waitq, &wait);
809 810
		try_to_freeze();

811
		lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p "
812 813
		       "dnld_sent=%d\n",
		       adapter->intcounter,
814
		       adapter->currenttxskb, priv->dnld_sent);
815 816 817

		if (kthread_should_stop()
		    || adapter->surpriseremoved) {
818
			lbs_deb_thread(
819 820 821 822 823 824 825 826 827 828
			       "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;
829
			int_status = priv->hw_get_int_status(priv, &ireg);
830 831

			if (int_status) {
832
				lbs_deb_thread(
833 834 835 836 837 838 839
				       "main-thread: reading HOST_INT_STATUS_REG failed\n");
				spin_unlock_irq(&adapter->driver_lock);
				continue;
			}
			adapter->hisregcpy |= ireg;
		}

840
		lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p "
841 842
		       "dnld_sent=%d\n",
		       adapter->intcounter,
843
		       adapter->currenttxskb, priv->dnld_sent);
844 845

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

849
			adapter->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
850
			spin_unlock_irq(&adapter->driver_lock);
851
			lbs_process_rx_command(priv);
852 853 854 855
			spin_lock_irq(&adapter->driver_lock);
		}

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

859
			adapter->hisregcpy &= ~MRVDRV_CARDEVENT;
860

861
			if (priv->hw_read_event_cause(priv)) {
862
				lbs_pr_alert(
863
				       "main-thread: hw_read_event_cause failed\n");
864 865 866 867
				spin_unlock_irq(&adapter->driver_lock);
				continue;
			}
			spin_unlock_irq(&adapter->driver_lock);
868
			lbs_process_event(priv);
869 870 871 872 873
		} else
			spin_unlock_irq(&adapter->driver_lock);

		/* Check if we need to confirm Sleep Request received previously */
		if (adapter->psstate == PS_STATE_PRE_SLEEP) {
874
			if (!priv->dnld_sent && !adapter->cur_cmd) {
875
				if (adapter->connect_status ==
876
				    LBS_CONNECTED) {
877
					lbs_deb_thread(
878 879 880 881
					       "main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p "
					       "dnld_sent=%d cur_cmd=%p, confirm now\n",
					       adapter->intcounter,
					       adapter->currenttxskb,
882
					       priv->dnld_sent,
883 884
					       adapter->cur_cmd);

885
					lbs_ps_confirm_sleep(priv,
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
						       (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 */
908
		if (!priv->dnld_sent && !priv->adapter->cur_cmd)
909
			lbs_execute_next_command(priv);
910 911

		/* Wake-up command waiters which can't sleep in
912
		 * lbs_prepare_and_send_command
913 914 915 916
		 */
		if (!adapter->nr_cmd_pending)
			wake_up_all(&adapter->cmd_pending);

917
		lbs_tx_runqueue(priv);
918 919 920 921 922 923
	}

	del_timer(&adapter->command_timer);
	adapter->nr_cmd_pending = 0;
	wake_up_all(&adapter->cmd_pending);

924
	lbs_deb_leave(LBS_DEB_THREAD);
925 926 927
	return 0;
}

H
Holger Schurig 已提交
928 929 930 931 932
/**
 *  @brief This function downloads firmware image, gets
 *  HW spec from firmware and set basic parameters to
 *  firmware.
 *
933
 *  @param priv    A pointer to struct lbs_private structure
H
Holger Schurig 已提交
934 935
 *  @return 	   0 or -1
 */
936
static int lbs_setup_firmware(struct lbs_private *priv)
H
Holger Schurig 已提交
937 938
{
	int ret = -1;
939
	struct lbs_adapter *adapter = priv->adapter;
940
	struct cmd_ds_mesh_access mesh_access;
H
Holger Schurig 已提交
941 942 943 944 945 946 947 948

	lbs_deb_enter(LBS_DEB_FW);

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

949
	ret = lbs_prepare_and_send_command(priv, CMD_GET_HW_SPEC,
H
Holger Schurig 已提交
950 951 952 953 954 955 956
				    0, CMD_OPTION_WAITFORRSP, 0, NULL);

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

957
	lbs_set_mac_packet_filter(priv);
H
Holger Schurig 已提交
958 959

	/* Get the supported Data rates */
960
	ret = lbs_prepare_and_send_command(priv, CMD_802_11_DATA_RATE,
H
Holger Schurig 已提交
961 962 963 964 965 966 967 968
				    CMD_ACT_GET_TX_RATE,
				    CMD_OPTION_WAITFORRSP, 0, NULL);

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

969 970 971 972
	/* Disable mesh autostart */
	if (priv->mesh_dev) {
		memset(&mesh_access, 0, sizeof(mesh_access));
		mesh_access.data[0] = cpu_to_le32(0);
973
		ret = lbs_prepare_and_send_command(priv,
974 975 976 977 978 979 980 981 982 983
				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;
	}

984
       /* Set the boot2 version in firmware */
985
       ret = lbs_prepare_and_send_command(priv, CMD_SET_BOOT2_VER,
986 987
                                   0, CMD_OPTION_WAITFORRSP, 0, NULL);

H
Holger Schurig 已提交
988 989 990 991 992 993 994 995 996 997 998 999
	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)
{
1000 1001
	struct lbs_private *priv = (struct lbs_private *)data;
	struct lbs_adapter *adapter = priv->adapter;
H
Holger Schurig 已提交
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
	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");
1028
	lbs_queue_cmd(adapter, ptempnode, 0);
H
Holger Schurig 已提交
1029 1030 1031 1032 1033 1034

	wake_up_interruptible(&priv->waitq);

	return;
}

1035
static int lbs_init_adapter(struct lbs_private *priv)
1036
{
1037
	struct lbs_adapter *adapter = priv->adapter;
H
Holger Schurig 已提交
1038
	size_t bufsize;
1039
	int i, ret = 0;
H
Holger Schurig 已提交
1040 1041 1042 1043 1044 1045

	/* 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");
1046 1047
		ret = -1;
		goto out;
H
Holger Schurig 已提交
1048 1049
	}

1050 1051 1052 1053 1054 1055 1056
	/* 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 已提交
1057

1058 1059
	adapter->lbs_ps_confirm_sleep.seqnum = cpu_to_le16(++adapter->seqnum);
	adapter->lbs_ps_confirm_sleep.command =
H
Holger Schurig 已提交
1060
	    cpu_to_le16(CMD_802_11_PS_MODE);
1061
	adapter->lbs_ps_confirm_sleep.size =
H
Holger Schurig 已提交
1062
	    cpu_to_le16(sizeof(struct PS_CMD_ConfirmSleep));
1063
	adapter->lbs_ps_confirm_sleep.action =
H
Holger Schurig 已提交
1064 1065 1066 1067
	    cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);

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

1068
	adapter->connect_status = LBS_DISCONNECTED;
1069
	adapter->mesh_connect_status = LBS_DISCONNECTED;
H
Holger Schurig 已提交
1070 1071 1072
	adapter->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
	adapter->mode = IW_MODE_INFRA;
	adapter->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
1073
	adapter->currentpacketfilter = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
H
Holger Schurig 已提交
1074 1075 1076
	adapter->radioon = RADIO_ON;
	adapter->auto_rate = 1;
	adapter->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
1077
	adapter->psmode = LBS802_11POWERMODECAM;
H
Holger Schurig 已提交
1078 1079
	adapter->psstate = PS_STATE_FULL_POWER;

1080
	mutex_init(&adapter->lock);
H
Holger Schurig 已提交
1081 1082 1083 1084 1085

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

1086 1087
	setup_timer(&adapter->command_timer, command_timer_fn,
	            (unsigned long)priv);
H
Holger Schurig 已提交
1088

1089 1090
	INIT_LIST_HEAD(&adapter->cmdfreeq);
	INIT_LIST_HEAD(&adapter->cmdpendingq);
H
Holger Schurig 已提交
1091

1092 1093 1094
	spin_lock_init(&adapter->driver_lock);
	init_waitqueue_head(&adapter->cmd_pending);
	adapter->nr_cmd_pending = 0;
H
Holger Schurig 已提交
1095

1096
	/* Allocate the command buffers */
1097
	if (lbs_allocate_cmd_buffer(priv)) {
1098 1099 1100
		lbs_pr_err("Out of memory allocating command buffers\n");
		ret = -1;
	}
H
Holger Schurig 已提交
1101

1102 1103 1104
out:
	return ret;
}
H
Holger Schurig 已提交
1105

1106
static void lbs_free_adapter(struct lbs_private *priv)
1107
{
1108
	struct lbs_adapter *adapter = priv->adapter;
H
Holger Schurig 已提交
1109

1110 1111 1112
	if (!adapter) {
		lbs_deb_fw("why double free adapter?\n");
		return;
H
Holger Schurig 已提交
1113 1114
	}

1115
	lbs_deb_fw("free command buffer\n");
1116
	lbs_free_cmd_buffer(priv);
H
Holger Schurig 已提交
1117

1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
	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 已提交
1129 1130
}

1131 1132
/**
 * @brief This function adds the card. it will probe the
1133
 * card, allocate the lbs_priv and initialize the device.
1134 1135
 *
 *  @param card    A pointer to card
1136
 *  @return 	   A pointer to struct lbs_private structure
1137
 */
1138
struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
1139 1140
{
	struct net_device *dev = NULL;
1141
	struct lbs_private *priv = NULL;
1142

1143
	lbs_deb_enter(LBS_DEB_NET);
1144 1145

	/* Allocate an Ethernet device and register it */
1146 1147
	dev = alloc_etherdev(sizeof(struct lbs_private));
	if (!dev) {
1148
		lbs_pr_err("init ethX device failed\n");
1149
		goto done;
1150
	}
1151
	priv = dev->priv;
1152

1153 1154 1155 1156
	/* 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");
1157
		goto err_kzalloc;
1158 1159
	}

1160
	if (lbs_init_adapter(priv)) {
1161 1162 1163 1164
		lbs_pr_err("failed to initialize adapter structure.\n");
		goto err_init_adapter;
	}

1165 1166
	priv->dev = dev;
	priv->card = card;
1167 1168
	priv->mesh_open = 0;
	priv->infra_open = 0;
1169
	priv->hotplug_device = dmdev;
1170 1171

	/* Setup the OS Interface to our functions */
1172 1173 1174 1175 1176 1177
	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;
1178
	dev->watchdog_timeo = 5 * HZ;
1179
	dev->ethtool_ops = &lbs_ethtool_ops;
1180
#ifdef	WIRELESS_EXT
1181
	dev->wireless_handlers = (struct iw_handler_def *)&lbs_handler_def;
1182 1183
#endif
	dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
1184
	dev->set_multicast_list = lbs_set_multicast_list;
1185

1186 1187
	SET_NETDEV_DEV(dev, dmdev);

1188
	priv->rtap_net_dev = NULL;
1189
	if (device_create_file(dmdev, &dev_attr_lbs_rtap))
1190 1191 1192 1193
		goto err_init_adapter;

	lbs_deb_thread("Starting main thread...\n");
	init_waitqueue_head(&priv->waitq);
1194
	priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
1195 1196 1197 1198 1199
	if (IS_ERR(priv->main_thread)) {
		lbs_deb_thread("Error creating main thread.\n");
		goto err_kthread_run;
	}

1200 1201 1202 1203
	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);
1204

1205 1206
	goto done;

1207
err_kthread_run:
1208
	device_remove_file(dmdev, &dev_attr_lbs_rtap);
1209 1210

err_init_adapter:
1211
	lbs_free_adapter(priv);
1212

1213 1214
err_kzalloc:
	free_netdev(dev);
1215
	priv = NULL;
1216

1217 1218 1219 1220
done:
	lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
	return priv;
}
1221
EXPORT_SYMBOL_GPL(lbs_add_card);
1222

1223

1224
int lbs_remove_card(struct lbs_private *priv)
1225
{
1226
	struct lbs_adapter *adapter = priv->adapter;
1227
	struct net_device *dev = priv->dev;
1228
	union iwreq_data wrqu;
1229 1230

	lbs_deb_enter(LBS_DEB_MAIN);
1231

1232
	lbs_remove_rtap(priv);
1233

1234
	dev = priv->dev;
1235
	device_remove_file(priv->hotplug_device, &dev_attr_lbs_rtap);
1236

1237 1238 1239
	cancel_delayed_work(&priv->scan_work);
	cancel_delayed_work(&priv->assoc_work);
	destroy_workqueue(priv->work_thread);
1240

1241 1242 1243
	if (adapter->psmode == LBS802_11POWERMODEMAX_PSP) {
		adapter->psmode = LBS802_11POWERMODECAM;
		lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
1244 1245
	}

1246 1247 1248 1249 1250 1251 1252 1253
	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);

1254
	lbs_free_adapter(priv);
1255 1256 1257 1258 1259 1260 1261

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

	lbs_deb_leave(LBS_DEB_MAIN);
	return 0;
}
1262
EXPORT_SYMBOL_GPL(lbs_remove_card);
1263 1264


1265
int lbs_start_card(struct lbs_private *priv)
1266 1267 1268 1269 1270 1271 1272
{
	struct net_device *dev = priv->dev;
	int ret = -1;

	lbs_deb_enter(LBS_DEB_MAIN);

	/* poke the firmware */
1273
	ret = lbs_setup_firmware(priv);
1274 1275 1276 1277
	if (ret)
		goto done;

	/* init 802.11d */
1278
	lbs_init_11d(priv);
1279 1280

	if (register_netdev(dev)) {
1281
		lbs_pr_err("cannot register ethX device\n");
1282
		goto done;
1283 1284
	}

1285
	lbs_debugfs_init_one(priv, dev);
1286

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

1289
	ret = 0;
1290

1291
done:
1292 1293 1294
	lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
	return ret;
}
1295
EXPORT_SYMBOL_GPL(lbs_start_card);
1296 1297


1298
int lbs_stop_card(struct lbs_private *priv)
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
{
	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);

1310
	lbs_debugfs_remove_one(priv);
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322

	/* 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);
1323
	return ret;
1324
}
1325
EXPORT_SYMBOL_GPL(lbs_stop_card);
1326

1327

1328 1329 1330
/**
 * @brief This function adds mshX interface
 *
1331
 *  @param priv    A pointer to the struct lbs_private structure
1332 1333
 *  @return 	   0 if successful, -X otherwise
 */
1334
int lbs_add_mesh(struct lbs_private *priv, struct device *dev)
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
{
	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;

1350 1351 1352 1353 1354 1355
	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;
1356 1357
	memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
			sizeof(priv->dev->dev_addr));
1358

1359 1360
	SET_NETDEV_DEV(priv->mesh_dev, dev);

1361
#ifdef	WIRELESS_EXT
1362
	mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
1363 1364 1365 1366 1367 1368 1369 1370
#endif
	/* Register virtual mesh interface */
	ret = register_netdev(mesh_dev);
	if (ret) {
		lbs_pr_err("cannot register mshX virtual interface\n");
		goto err_free;
	}

1371
	ret = sysfs_create_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
	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;
}
1389
EXPORT_SYMBOL_GPL(lbs_add_mesh);
1390

1391

1392
void lbs_remove_mesh(struct lbs_private *priv)
1393 1394 1395
{
	struct net_device *mesh_dev;

1396
	lbs_deb_enter(LBS_DEB_MAIN);
1397 1398 1399 1400 1401 1402 1403

	if (!priv)
		goto out;

	mesh_dev = priv->mesh_dev;

	netif_stop_queue(mesh_dev);
1404
	netif_carrier_off(priv->mesh_dev);
1405

1406
	sysfs_remove_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
1407 1408 1409 1410 1411 1412
	unregister_netdev(mesh_dev);

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

out:
1413
	lbs_deb_leave(LBS_DEB_MAIN);
1414
}
1415
EXPORT_SYMBOL_GPL(lbs_remove_mesh);
1416

1417 1418 1419 1420 1421 1422 1423 1424 1425
/**
 *  @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
 */
1426
struct chan_freq_power *lbs_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
1427 1428 1429
{
	int i, end;

1430
	lbs_deb_enter(LBS_DEB_MAIN);
1431

1432
	end = ARRAY_SIZE(region_cfp_table);
1433 1434

	for (i = 0; i < end ; i++) {
1435
		lbs_deb_main("region_cfp_table[i].region=%d\n",
1436 1437 1438
			region_cfp_table[i].region);
		if (region_cfp_table[i].region == region) {
			*cfp_no = region_cfp_table[i].cfp_no_BG;
1439
			lbs_deb_leave(LBS_DEB_MAIN);
1440 1441 1442 1443
			return region_cfp_table[i].cfp_BG;
		}
	}

1444
	lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
1445 1446 1447
	return NULL;
}

1448
int lbs_set_regiontable(struct lbs_private *priv, u8 region, u8 band)
1449
{
1450
	struct lbs_adapter *adapter = priv->adapter;
1451
	int ret = 0;
1452 1453 1454 1455 1456
	int i = 0;

	struct chan_freq_power *cfp;
	int cfp_no;

1457
	lbs_deb_enter(LBS_DEB_MAIN);
1458 1459 1460 1461

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

	{
1462
		cfp = lbs_get_region_cfp_table(region, band, &cfp_no);
1463 1464 1465 1466
		if (cfp != NULL) {
			adapter->region_channel[i].nrcfp = cfp_no;
			adapter->region_channel[i].CFP = cfp;
		} else {
1467
			lbs_deb_main("wrong region code %#x in band B/G\n",
1468
			       region);
1469 1470
			ret = -1;
			goto out;
1471 1472 1473 1474 1475 1476
		}
		adapter->region_channel[i].valid = 1;
		adapter->region_channel[i].region = region;
		adapter->region_channel[i].band = band;
		i++;
	}
1477 1478 1479
out:
	lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
	return ret;
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
}

/**
 *  @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
 */
1490
void lbs_interrupt(struct net_device *dev)
1491
{
1492
	struct lbs_private *priv = dev->priv;
1493

1494
	lbs_deb_enter(LBS_DEB_THREAD);
1495

1496
	lbs_deb_thread("lbs_interrupt: intcounter=%d\n",
1497 1498 1499 1500 1501 1502 1503
	       priv->adapter->intcounter);

	priv->adapter->intcounter++;

	if (priv->adapter->psstate == PS_STATE_SLEEP) {
		priv->adapter->psstate = PS_STATE_AWAKE;
		netif_wake_queue(dev);
1504 1505
		if (priv->mesh_dev)
			netif_wake_queue(priv->mesh_dev);
1506 1507
	}

1508
	wake_up_interruptible(&priv->waitq);
1509

1510
	lbs_deb_leave(LBS_DEB_THREAD);
1511
}
1512
EXPORT_SYMBOL_GPL(lbs_interrupt);
1513

1514
int lbs_reset_device(struct lbs_private *priv)
1515 1516 1517 1518
{
	int ret;

	lbs_deb_enter(LBS_DEB_MAIN);
1519
	ret = lbs_prepare_and_send_command(priv, CMD_802_11_RESET,
1520 1521 1522 1523 1524 1525
				    CMD_ACT_HALT, 0, 0, NULL);
	msleep_interruptible(10);

	lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
	return ret;
}
1526
EXPORT_SYMBOL_GPL(lbs_reset_device);
1527

1528
static int __init lbs_init_module(void)
1529
{
1530
	lbs_deb_enter(LBS_DEB_MAIN);
1531
	lbs_debugfs_init();
1532 1533
	lbs_deb_leave(LBS_DEB_MAIN);
	return 0;
1534 1535
}

1536
static void __exit lbs_exit_module(void)
1537
{
1538
	lbs_deb_enter(LBS_DEB_MAIN);
1539

1540
	lbs_debugfs_remove();
1541

1542
	lbs_deb_leave(LBS_DEB_MAIN);
1543 1544
}

1545 1546 1547 1548
/*
 * rtap interface support fuctions
 */

1549
static int lbs_rtap_open(struct net_device *dev)
1550 1551 1552 1553 1554 1555
{
        netif_carrier_off(dev);
        netif_stop_queue(dev);
        return 0;
}

1556
static int lbs_rtap_stop(struct net_device *dev)
1557 1558 1559 1560
{
        return 0;
}

1561
static int lbs_rtap_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1562 1563 1564 1565 1566
{
        netif_stop_queue(dev);
        return -EOPNOTSUPP;
}

1567
static struct net_device_stats *lbs_rtap_get_stats(struct net_device *dev)
1568
{
1569
	struct lbs_private *priv = dev->priv;
1570 1571 1572 1573
	return &priv->ieee->stats;
}


1574
void lbs_remove_rtap(struct lbs_private *priv)
1575 1576 1577 1578 1579 1580 1581 1582
{
	if (priv->rtap_net_dev == NULL)
		return;
	unregister_netdev(priv->rtap_net_dev);
	free_ieee80211(priv->rtap_net_dev);
	priv->rtap_net_dev = NULL;
}

1583
int lbs_add_rtap(struct lbs_private *priv)
1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
{
	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;
1600 1601 1602 1603 1604
	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;
1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
	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;
}


1620 1621
module_init(lbs_init_module);
module_exit(lbs_exit_module);
1622

1623
MODULE_DESCRIPTION("Libertas WLAN Driver Library");
1624 1625
MODULE_AUTHOR("Marvell International Ltd.");
MODULE_LICENSE("GPL");