Linux Audio

Check our new training course

Loading...
v3.1
   1/*
   2 * Marvell Wireless LAN device driver: major functions
   3 *
   4 * Copyright (C) 2011, Marvell International Ltd.
   5 *
   6 * This software file (the "File") is distributed by Marvell International
   7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
   8 * (the "License").  You may use, redistribute and/or modify this File in
   9 * accordance with the terms and conditions of the License, a copy of which
  10 * is available by writing to the Free Software Foundation, Inc.,
  11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
  12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  13 *
  14 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
  15 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  16 * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
  17 * this warranty disclaimer.
  18 */
  19
  20#include "main.h"
  21#include "wmm.h"
  22#include "cfg80211.h"
  23#include "11n.h"
  24
  25#define VERSION	"1.0"
  26
  27const char driver_version[] = "mwifiex " VERSION " (%s) ";
 
 
  28
  29static struct mwifiex_bss_attr mwifiex_bss_sta[] = {
  30	{MWIFIEX_BSS_TYPE_STA, MWIFIEX_DATA_FRAME_TYPE_ETH_II, true, 0, 0},
  31};
 
 
 
  32
  33static int drv_mode = DRV_MODE_STA;
 
  34
  35/* Supported drv_mode table */
  36static struct mwifiex_drv_mode mwifiex_drv_mode_tbl[] = {
  37	{
  38		.drv_mode = DRV_MODE_STA,
  39		.intf_num = ARRAY_SIZE(mwifiex_bss_sta),
  40		.bss_attr = mwifiex_bss_sta,
  41	},
  42};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  43
  44/*
  45 * This function registers the device and performs all the necessary
  46 * initializations.
  47 *
  48 * The following initialization operations are performed -
  49 *      - Allocate adapter structure
  50 *      - Save interface specific operations table in adapter
  51 *      - Call interface specific initialization routine
  52 *      - Allocate private structures
  53 *      - Set default adapter structure parameters
  54 *      - Initialize locks
  55 *
  56 * In case of any errors during inittialization, this function also ensures
  57 * proper cleanup before exiting.
  58 */
  59static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
  60			    struct mwifiex_drv_mode *drv_mode_ptr,
  61			    void **padapter)
  62{
  63	struct mwifiex_adapter *adapter;
  64	int i;
  65
  66	adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
  67	if (!adapter)
  68		return -ENOMEM;
  69
  70	*padapter = adapter;
  71	adapter->card = card;
  72
  73	/* Save interface specific operations in adapter */
  74	memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
  75
  76	/* card specific initialization has been deferred until now .. */
  77	if (adapter->if_ops.init_if(adapter))
  78		goto error;
 
  79
  80	adapter->priv_num = 0;
  81	for (i = 0; i < drv_mode_ptr->intf_num; i++) {
  82		adapter->priv[i] = NULL;
  83
  84		if (!drv_mode_ptr->bss_attr[i].active)
  85			continue;
  86
 
  87		/* Allocate memory for private structure */
  88		adapter->priv[i] = kzalloc(sizeof(struct mwifiex_private),
  89				GFP_KERNEL);
  90		if (!adapter->priv[i]) {
  91			dev_err(adapter->dev, "%s: failed to alloc priv[%d]\n",
  92			       __func__, i);
  93			goto error;
  94		}
  95
  96		adapter->priv_num++;
  97		adapter->priv[i]->adapter = adapter;
  98		/* Save bss_type, frame_type & bss_priority */
  99		adapter->priv[i]->bss_type = drv_mode_ptr->bss_attr[i].bss_type;
 100		adapter->priv[i]->frame_type =
 101					drv_mode_ptr->bss_attr[i].frame_type;
 102		adapter->priv[i]->bss_priority =
 103					drv_mode_ptr->bss_attr[i].bss_priority;
 104
 105		if (drv_mode_ptr->bss_attr[i].bss_type == MWIFIEX_BSS_TYPE_STA)
 106			adapter->priv[i]->bss_role = MWIFIEX_BSS_ROLE_STA;
 107		else if (drv_mode_ptr->bss_attr[i].bss_type ==
 108							MWIFIEX_BSS_TYPE_UAP)
 109			adapter->priv[i]->bss_role = MWIFIEX_BSS_ROLE_UAP;
 110
 111		/* Save bss_index & bss_num */
 112		adapter->priv[i]->bss_index = i;
 113		adapter->priv[i]->bss_num = drv_mode_ptr->bss_attr[i].bss_num;
 114	}
 115	adapter->drv_mode = drv_mode_ptr;
 116
 117	if (mwifiex_init_lock_list(adapter))
 118		goto error;
 
 
 
 119
 120	init_timer(&adapter->cmd_timer);
 121	adapter->cmd_timer.function = mwifiex_cmd_timeout_func;
 122	adapter->cmd_timer.data = (unsigned long) adapter;
 123
 124	return 0;
 125
 126error:
 127	dev_dbg(adapter->dev, "info: leave mwifiex_register with error\n");
 128
 129	mwifiex_free_lock_list(adapter);
 130	for (i = 0; i < drv_mode_ptr->intf_num; i++)
 131		kfree(adapter->priv[i]);
 
 132	kfree(adapter);
 133
 134	return -1;
 135}
 136
 137/*
 138 * This function unregisters the device and performs all the necessary
 139 * cleanups.
 140 *
 141 * The following cleanup operations are performed -
 142 *      - Free the timers
 143 *      - Free beacon buffers
 144 *      - Free private structures
 145 *      - Free adapter structure
 146 */
 147static int mwifiex_unregister(struct mwifiex_adapter *adapter)
 148{
 149	s32 i;
 150
 151	del_timer(&adapter->cmd_timer);
 
 
 
 152
 153	/* Free private structures */
 154	for (i = 0; i < adapter->priv_num; i++) {
 155		if (adapter->priv[i]) {
 156			mwifiex_free_curr_bcn(adapter->priv[i]);
 
 157			kfree(adapter->priv[i]);
 158		}
 159	}
 160
 161	kfree(adapter);
 162	return 0;
 163}
 164
 165/*
 166 * The main process.
 167 *
 168 * This function is the main procedure of the driver and handles various driver
 169 * operations. It runs in a loop and provides the core functionalities.
 170 *
 171 * The main responsibilities of this function are -
 172 *      - Ensure concurrency control
 173 *      - Handle pending interrupts and call interrupt handlers
 174 *      - Wake up the card if required
 175 *      - Handle command responses and call response handlers
 176 *      - Handle events and call event handlers
 177 *      - Execute pending commands
 178 *      - Transmit pending data packets
 179 */
 180int mwifiex_main_process(struct mwifiex_adapter *adapter)
 181{
 182	int ret = 0;
 183	unsigned long flags;
 
 184
 185	spin_lock_irqsave(&adapter->main_proc_lock, flags);
 186
 187	/* Check if already processing */
 188	if (adapter->mwifiex_processing) {
 189		spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
 190		goto exit_main_proc;
 191	} else {
 192		adapter->mwifiex_processing = true;
 193		spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
 194	}
 195process_start:
 196	do {
 197		if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
 198		    (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
 199			break;
 200
 201		/* Handle pending interrupt if any */
 202		if (adapter->int_status) {
 203			if (adapter->hs_activated)
 204				mwifiex_process_hs_config(adapter);
 205			adapter->if_ops.process_int_status(adapter);
 
 206		}
 207
 208		/* Need to wake up the card ? */
 209		if ((adapter->ps_state == PS_STATE_SLEEP) &&
 210		    (adapter->pm_wakeup_card_req &&
 211		     !adapter->pm_wakeup_fw_try) &&
 212		    (is_command_pending(adapter)
 213		     || !mwifiex_wmm_lists_empty(adapter))) {
 214			adapter->pm_wakeup_fw_try = true;
 215			adapter->if_ops.wakeup(adapter);
 216			continue;
 217		}
 
 218		if (IS_CARD_RX_RCVD(adapter)) {
 219			adapter->pm_wakeup_fw_try = false;
 220			if (adapter->ps_state == PS_STATE_SLEEP)
 221				adapter->ps_state = PS_STATE_AWAKE;
 222		} else {
 223			/* We have tried to wakeup the card already */
 224			if (adapter->pm_wakeup_fw_try)
 225				break;
 226			if (adapter->ps_state != PS_STATE_AWAKE ||
 227			    adapter->tx_lock_flag)
 228				break;
 229
 230			if (adapter->scan_processing || adapter->data_sent
 231			    || mwifiex_wmm_lists_empty(adapter)) {
 232				if (adapter->cmd_sent || adapter->curr_cmd
 233				    || (!is_command_pending(adapter)))
 
 234					break;
 235			}
 236		}
 237
 
 
 
 
 
 
 
 
 
 
 
 238		/* Check for Cmd Resp */
 239		if (adapter->cmd_resp_received) {
 240			adapter->cmd_resp_received = false;
 241			mwifiex_process_cmdresp(adapter);
 242
 243			/* call mwifiex back when init_fw is done */
 244			if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
 245				adapter->hw_status = MWIFIEX_HW_STATUS_READY;
 246				mwifiex_init_fw_complete(adapter);
 247			}
 248		}
 249
 250		/* Check for event */
 251		if (adapter->event_received) {
 252			adapter->event_received = false;
 253			mwifiex_process_event(adapter);
 254		}
 255
 256		/* Check if we need to confirm Sleep Request
 257		   received previously */
 258		if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
 259			if (!adapter->cmd_sent && !adapter->curr_cmd)
 260				mwifiex_check_ps_cond(adapter);
 261		}
 262
 263		/* * The ps_state may have been changed during processing of
 264		 * Sleep Request event.
 265		 */
 266		if ((adapter->ps_state == PS_STATE_SLEEP)
 267		    || (adapter->ps_state == PS_STATE_PRE_SLEEP)
 268		    || (adapter->ps_state == PS_STATE_SLEEP_CFM)
 269		    || adapter->tx_lock_flag)
 270			continue;
 271
 272		if (!adapter->cmd_sent && !adapter->curr_cmd) {
 273			if (mwifiex_exec_next_cmd(adapter) == -1) {
 274				ret = -1;
 275				break;
 276			}
 277		}
 278
 279		if (!adapter->scan_processing && !adapter->data_sent &&
 280		    !mwifiex_wmm_lists_empty(adapter)) {
 281			mwifiex_wmm_process_tx(adapter);
 282			if (adapter->hs_activated) {
 283				adapter->is_hs_configured = false;
 284				mwifiex_hs_activated_event
 285					(mwifiex_get_priv
 286					 (adapter, MWIFIEX_BSS_ROLE_ANY),
 287					 false);
 288			}
 289		}
 290
 291		if (adapter->delay_null_pkt && !adapter->cmd_sent &&
 292		    !adapter->curr_cmd && !is_command_pending(adapter)
 293		    && mwifiex_wmm_lists_empty(adapter)) {
 294			if (!mwifiex_send_null_packet
 295			    (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
 296			     MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
 297			     MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
 298				adapter->delay_null_pkt = false;
 299				adapter->ps_state = PS_STATE_SLEEP;
 300			}
 301			break;
 302		}
 303	} while (true);
 304
 305	if ((adapter->int_status) || IS_CARD_RX_RCVD(adapter))
 
 
 306		goto process_start;
 
 307
 308	spin_lock_irqsave(&adapter->main_proc_lock, flags);
 309	adapter->mwifiex_processing = false;
 310	spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
 311
 312exit_main_proc:
 313	if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
 314		mwifiex_shutdown_drv(adapter);
 315	return ret;
 316}
 317
 318/*
 319 * This function initializes the software.
 320 *
 321 * The main work includes allocating and initializing the adapter structure
 322 * and initializing the private structures.
 323 */
 324static int
 325mwifiex_init_sw(void *card, struct mwifiex_if_ops *if_ops, void **padapter)
 326{
 327	int i;
 328	struct mwifiex_drv_mode *drv_mode_ptr;
 329
 330	/* find mwifiex_drv_mode entry from mwifiex_drv_mode_tbl */
 331	drv_mode_ptr = NULL;
 332	for (i = 0; i < ARRAY_SIZE(mwifiex_drv_mode_tbl); i++) {
 333		if (mwifiex_drv_mode_tbl[i].drv_mode == drv_mode) {
 334			drv_mode_ptr = &mwifiex_drv_mode_tbl[i];
 335			break;
 336		}
 337	}
 338
 339	if (!drv_mode_ptr) {
 340		pr_err("invalid drv_mode=%d\n", drv_mode);
 341		return -1;
 342	}
 343
 344	if (mwifiex_register(card, if_ops, drv_mode_ptr, padapter))
 345		return -1;
 346
 347	return 0;
 348}
 349
 350/*
 351 * This function frees the adapter structure.
 352 *
 353 * Additionally, this closes the netlink socket, frees the timers
 354 * and private structures.
 355 */
 356static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
 357{
 358	if (!adapter) {
 359		pr_err("%s: adapter is NULL\n", __func__);
 360		return;
 361	}
 362
 363	mwifiex_unregister(adapter);
 364	pr_debug("info: %s: free adapter\n", __func__);
 365}
 366
 367/*
 368 * This function initializes the hardware and firmware.
 
 
 
 
 
 
 
 
 
 
 
 369 *
 370 * The main initialization steps followed are -
 371 *      - Download the correct firmware to card
 372 *      - Allocate and initialize the adapter structure
 373 *      - Initialize the private structures
 374 *      - Issue the init commands to firmware
 375 */
 376static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
 377{
 378	int ret, err;
 
 
 
 379	struct mwifiex_fw_image fw;
 
 
 
 
 
 
 
 
 
 380
 381	memset(&fw, 0, sizeof(struct mwifiex_fw_image));
 382
 383	err = request_firmware(&adapter->firmware, adapter->fw_name,
 384			       adapter->dev);
 385	if (err < 0) {
 386		dev_err(adapter->dev, "request_firmware() returned"
 387				" error code %#x\n", err);
 388		ret = -1;
 389		goto done;
 390	}
 391	fw.fw_buf = (u8 *) adapter->firmware->data;
 392	fw.fw_len = adapter->firmware->size;
 393
 394	ret = mwifiex_dnld_fw(adapter, &fw);
 
 
 
 395	if (ret == -1)
 396		goto done;
 397
 398	dev_notice(adapter->dev, "WLAN FW is active\n");
 399
 
 
 
 
 
 
 
 
 
 
 
 
 
 400	adapter->init_wait_q_woken = false;
 401	ret = mwifiex_init_fw(adapter);
 402	if (ret == -1) {
 403		goto done;
 404	} else if (!ret) {
 405		adapter->hw_status = MWIFIEX_HW_STATUS_READY;
 406		goto done;
 407	}
 408	/* Wait for mwifiex_init to complete */
 409	wait_event_interruptible(adapter->init_wait_q,
 410				 adapter->init_wait_q_woken);
 411	if (adapter->hw_status != MWIFIEX_HW_STATUS_READY) {
 412		ret = -1;
 413		goto done;
 
 
 
 
 414	}
 415	ret = 0;
 416
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 417done:
 418	if (adapter->firmware)
 
 
 
 
 419		release_firmware(adapter->firmware);
 420	if (ret)
 421		ret = -1;
 422	return ret;
 
 
 
 
 423}
 424
 425/*
 426 * This function fills a driver buffer.
 427 *
 428 * The function associates a given SKB with the provided driver buffer
 429 * and also updates some of the SKB parameters, including IP header,
 430 * priority and timestamp.
 431 */
 432static void
 433mwifiex_fill_buffer(struct sk_buff *skb)
 434{
 435	struct ethhdr *eth;
 436	struct iphdr *iph;
 437	struct timeval tv;
 438	u8 tid = 0;
 439
 440	eth = (struct ethhdr *) skb->data;
 441	switch (eth->h_proto) {
 442	case __constant_htons(ETH_P_IP):
 443		iph = ip_hdr(skb);
 444		tid = IPTOS_PREC(iph->tos);
 445		pr_debug("data: packet type ETH_P_IP: %04x, tid=%#x prio=%#x\n",
 446		       eth->h_proto, tid, skb->priority);
 447		break;
 448	case __constant_htons(ETH_P_ARP):
 449		pr_debug("data: ARP packet: %04x\n", eth->h_proto);
 450	default:
 451		break;
 452	}
 453/* Offset for TOS field in the IP header */
 454#define IPTOS_OFFSET 5
 455	tid = (tid >> IPTOS_OFFSET);
 456	skb->priority = tid;
 457	/* Record the current time the packet was queued; used to
 458	   determine the amount of time the packet was queued in
 459	   the driver before it was sent to the firmware.
 460	   The delay is then sent along with the packet to the
 461	   firmware for aggregate delay calculation for stats and
 462	   MSDU lifetime expiry.
 463	 */
 464	do_gettimeofday(&tv);
 465	skb->tstamp = timeval_to_ktime(tv);
 466}
 467
 468/*
 469 * CFG802.11 network device handler for open.
 470 *
 471 * Starts the data queue.
 472 */
 473static int
 474mwifiex_open(struct net_device *dev)
 475{
 476	netif_start_queue(dev);
 477	return 0;
 478}
 479
 480/*
 481 * CFG802.11 network device handler for close.
 482 */
 483static int
 484mwifiex_close(struct net_device *dev)
 485{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 486	return 0;
 487}
 488
 489/*
 490 * CFG802.11 network device handler for data transmission.
 491 */
 492static int
 493mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
 494{
 495	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
 496	struct sk_buff *new_skb;
 497	struct mwifiex_txinfo *tx_info;
 
 498
 499	dev_dbg(priv->adapter->dev, "data: %lu BSS(%d): Data <= kernel\n",
 500				jiffies, priv->bss_index);
 501
 502	if (priv->adapter->surprise_removed) {
 503		kfree_skb(skb);
 504		priv->stats.tx_dropped++;
 505		return 0;
 506	}
 507	if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
 508		dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
 509		kfree_skb(skb);
 510		priv->stats.tx_dropped++;
 511		return 0;
 512	}
 513	if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
 514		dev_dbg(priv->adapter->dev,
 515			"data: Tx: insufficient skb headroom %d\n",
 516		       skb_headroom(skb));
 517		/* Insufficient skb headroom - allocate a new skb */
 518		new_skb =
 519			skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
 520		if (unlikely(!new_skb)) {
 521			dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
 522			kfree_skb(skb);
 523			priv->stats.tx_dropped++;
 524			return 0;
 525		}
 526		kfree_skb(skb);
 527		skb = new_skb;
 528		dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
 529				skb_headroom(skb));
 530	}
 531
 532	tx_info = MWIFIEX_SKB_TXCB(skb);
 533	tx_info->bss_index = priv->bss_index;
 534	mwifiex_fill_buffer(skb);
 535
 536	mwifiex_wmm_add_buf_txqueue(priv->adapter, skb);
 537	atomic_inc(&priv->adapter->tx_pending);
 538
 539	if (atomic_read(&priv->adapter->tx_pending) >= MAX_TX_PENDING) {
 540		netif_stop_queue(priv->netdev);
 541		dev->trans_start = jiffies;
 542	}
 
 
 
 
 
 543
 544	queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
 545
 546	return 0;
 547}
 548
 549/*
 550 * CFG802.11 network device handler for setting MAC address.
 551 */
 552static int
 553mwifiex_set_mac_address(struct net_device *dev, void *addr)
 554{
 555	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
 556	struct sockaddr *hw_addr = addr;
 557	int ret;
 558
 559	memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
 560
 561	/* Send request to firmware */
 562	ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
 563				    HostCmd_ACT_GEN_SET, 0, NULL);
 564
 565	if (!ret)
 566		memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
 567	else
 568		dev_err(priv->adapter->dev, "set mac address failed: ret=%d"
 569					    "\n", ret);
 570
 571	memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
 572
 573	return ret;
 574}
 575
 576/*
 577 * CFG802.11 network device handler for setting multicast list.
 578 */
 579static void mwifiex_set_multicast_list(struct net_device *dev)
 580{
 581	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
 582	struct mwifiex_multicast_list mcast_list;
 583
 584	if (dev->flags & IFF_PROMISC) {
 585		mcast_list.mode = MWIFIEX_PROMISC_MODE;
 586	} else if (dev->flags & IFF_ALLMULTI ||
 587		   netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
 588		mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
 589	} else {
 590		mcast_list.mode = MWIFIEX_MULTICAST_MODE;
 591		if (netdev_mc_count(dev))
 592			mcast_list.num_multicast_addr =
 593				mwifiex_copy_mcast_addr(&mcast_list, dev);
 594	}
 595	mwifiex_request_set_multicast_list(priv, &mcast_list);
 596}
 597
 598/*
 599 * CFG802.11 network device handler for transmission timeout.
 600 */
 601static void
 602mwifiex_tx_timeout(struct net_device *dev)
 603{
 604	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
 605
 606	dev_err(priv->adapter->dev, "%lu : Tx timeout, bss_index=%d\n",
 607				jiffies, priv->bss_index);
 608	dev->trans_start = jiffies;
 609	priv->num_tx_timeout++;
 
 
 
 
 
 
 
 
 
 
 
 
 610}
 611
 612/*
 613 * CFG802.11 network device handler for statistics retrieval.
 614 */
 615static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
 616{
 617	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
 618
 619	return &priv->stats;
 620}
 621
 
 
 
 
 
 
 
 
 622/* Network device handlers */
 623static const struct net_device_ops mwifiex_netdev_ops = {
 624	.ndo_open = mwifiex_open,
 625	.ndo_stop = mwifiex_close,
 626	.ndo_start_xmit = mwifiex_hard_start_xmit,
 627	.ndo_set_mac_address = mwifiex_set_mac_address,
 628	.ndo_tx_timeout = mwifiex_tx_timeout,
 629	.ndo_get_stats = mwifiex_get_stats,
 630	.ndo_set_multicast_list = mwifiex_set_multicast_list,
 
 631};
 632
 633/*
 634 * This function initializes the private structure parameters.
 635 *
 636 * The following wait queues are initialized -
 637 *      - IOCTL wait queue
 638 *      - Command wait queue
 639 *      - Statistics wait queue
 640 *
 641 * ...and the following default parameters are set -
 642 *      - Current key index     : Set to 0
 643 *      - Rate index            : Set to auto
 644 *      - Media connected       : Set to disconnected
 645 *      - Adhoc link sensed     : Set to false
 646 *      - Nick name             : Set to null
 647 *      - Number of Tx timeout  : Set to 0
 648 *      - Device address        : Set to current address
 649 *
 650 * In addition, the CFG80211 work queue is also created.
 651 */
 652static void
 653mwifiex_init_priv_params(struct mwifiex_private *priv, struct net_device *dev)
 654{
 655	dev->netdev_ops = &mwifiex_netdev_ops;
 
 656	/* Initialize private structure */
 657	priv->current_key_index = 0;
 658	priv->media_connected = false;
 659	memset(&priv->nick_name, 0, sizeof(priv->nick_name));
 
 
 
 
 
 
 660	priv->num_tx_timeout = 0;
 661	priv->workqueue = create_singlethread_workqueue("cfg80211_wq");
 662	INIT_WORK(&priv->cfg_workqueue, mwifiex_cfg80211_results);
 663	memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
 664}
 665
 666/*
 667 * This function adds a new logical interface.
 668 *
 669 * It allocates, initializes and registers the interface by performing
 670 * the following opearations -
 671 *      - Allocate a new net device structure
 672 *      - Assign device name
 673 *      - Register the new device with CFG80211 subsystem
 674 *      - Initialize semaphore and private structure
 675 *      - Register the new device with kernel
 676 *      - Create the complete debug FS structure if configured
 677 */
 678static struct mwifiex_private *mwifiex_add_interface(
 679			struct mwifiex_adapter *adapter,
 680			u8 bss_index, u8 bss_type)
 681{
 682	struct net_device *dev;
 683	struct mwifiex_private *priv;
 684	void *mdev_priv;
 685
 686	dev = alloc_netdev_mq(sizeof(struct mwifiex_private *), "mlan%d",
 687			      ether_setup, 1);
 688	if (!dev) {
 689		dev_err(adapter->dev, "no memory available for netdevice\n");
 690		goto error;
 691	}
 692
 693	if (mwifiex_register_cfg80211(dev, adapter->priv[bss_index]->curr_addr,
 694				      adapter->priv[bss_index]) != 0) {
 695		dev_err(adapter->dev, "cannot register netdevice with cfg80211\n");
 696		goto error;
 697	}
 698	/* Save the priv pointer in netdev */
 699	priv = adapter->priv[bss_index];
 700	mdev_priv = netdev_priv(dev);
 701	*((unsigned long *) mdev_priv) = (unsigned long) priv;
 702
 703	priv->netdev = dev;
 704
 705	sema_init(&priv->async_sem, 1);
 706	priv->scan_pending_on_block = false;
 707
 708	mwifiex_init_priv_params(priv, dev);
 709
 710	SET_NETDEV_DEV(dev, adapter->dev);
 711
 712	/* Register network device */
 713	if (register_netdev(dev)) {
 714		dev_err(adapter->dev, "cannot register virtual network device\n");
 715		goto error;
 716	}
 717
 718	dev_dbg(adapter->dev, "info: %s: Marvell 802.11 Adapter\n", dev->name);
 719#ifdef CONFIG_DEBUG_FS
 720	mwifiex_dev_debugfs_init(priv);
 721#endif
 722	return priv;
 723error:
 724	if (dev)
 725		free_netdev(dev);
 726	return NULL;
 727}
 728
 729/*
 730 * This function removes a logical interface.
 731 *
 732 * It deregisters, resets and frees the interface by performing
 733 * the following operations -
 734 *      - Disconnect the device if connected, send wireless event to
 735 *        notify applications.
 736 *      - Remove the debug FS structure if configured
 737 *      - Unregister the device from kernel
 738 *      - Free the net device structure
 739 *      - Cancel all works and destroy work queue
 740 *      - Unregister and free the wireless device from CFG80211 subsystem
 741 */
 742static void
 743mwifiex_remove_interface(struct mwifiex_adapter *adapter, u8 bss_index)
 744{
 745	struct net_device *dev;
 746	struct mwifiex_private *priv = adapter->priv[bss_index];
 747
 748	if (!priv)
 749		return;
 750	dev = priv->netdev;
 751
 752	if (priv->media_connected)
 753		priv->media_connected = false;
 754
 755#ifdef CONFIG_DEBUG_FS
 756	mwifiex_dev_debugfs_remove(priv);
 757#endif
 758	/* Last reference is our one */
 759	dev_dbg(adapter->dev, "info: %s: refcnt = %d\n",
 760				dev->name, netdev_refcnt_read(dev));
 761
 762	if (dev->reg_state == NETREG_REGISTERED)
 763		unregister_netdev(dev);
 764
 765	/* Clear the priv in adapter */
 766	priv->netdev = NULL;
 767	if (dev)
 768		free_netdev(dev);
 769
 770	cancel_work_sync(&priv->cfg_workqueue);
 771	flush_workqueue(priv->workqueue);
 772	destroy_workqueue(priv->workqueue);
 773	wiphy_unregister(priv->wdev->wiphy);
 774	wiphy_free(priv->wdev->wiphy);
 775	kfree(priv->wdev);
 776}
 777
 778/*
 779 * This function check if command is pending.
 780 */
 781int is_command_pending(struct mwifiex_adapter *adapter)
 782{
 783	unsigned long flags;
 784	int is_cmd_pend_q_empty;
 785
 786	spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
 787	is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
 788	spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
 789
 790	return !is_cmd_pend_q_empty;
 791}
 792
 793/*
 794 * This function returns the correct private structure pointer based
 795 * upon the BSS number.
 796 */
 797struct mwifiex_private *
 798mwifiex_bss_index_to_priv(struct mwifiex_adapter *adapter, u8 bss_index)
 799{
 800	if (!adapter || (bss_index >= adapter->priv_num))
 801		return NULL;
 802	return adapter->priv[bss_index];
 803}
 804
 805/*
 806 * This is the main work queue function.
 807 *
 808 * It handles the main process, which in turn handles the complete
 809 * driver operations.
 810 */
 811static void mwifiex_main_work_queue(struct work_struct *work)
 812{
 813	struct mwifiex_adapter *adapter =
 814		container_of(work, struct mwifiex_adapter, main_work);
 815
 816	if (adapter->surprise_removed)
 817		return;
 818	mwifiex_main_process(adapter);
 819}
 820
 821/*
 822 * This function cancels all works in the queue and destroys
 823 * the main workqueue.
 824 */
 825static void
 826mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
 827{
 828	flush_workqueue(adapter->workqueue);
 829	destroy_workqueue(adapter->workqueue);
 830	adapter->workqueue = NULL;
 831}
 832
 833/*
 834 * This function adds the card.
 835 *
 836 * This function follows the following major steps to set up the device -
 837 *      - Initialize software. This includes probing the card, registering
 838 *        the interface operations table, and allocating/initializing the
 839 *        adapter structure
 840 *      - Set up the netlink socket
 841 *      - Create and start the main work queue
 842 *      - Register the device
 843 *      - Initialize firmware and hardware
 844 *      - Add logical interfaces
 845 */
 846int
 847mwifiex_add_card(void *card, struct semaphore *sem,
 848		 struct mwifiex_if_ops *if_ops)
 849{
 850	int i;
 851	struct mwifiex_adapter *adapter;
 852
 853	if (down_interruptible(sem))
 854		goto exit_sem_err;
 855
 856	if (mwifiex_init_sw(card, if_ops, (void **)&adapter)) {
 857		pr_err("%s: software init failed\n", __func__);
 858		goto err_init_sw;
 859	}
 860
 
 
 
 861	adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
 862	adapter->surprise_removed = false;
 863	init_waitqueue_head(&adapter->init_wait_q);
 864	adapter->is_suspended = false;
 865	adapter->hs_activated = false;
 866	init_waitqueue_head(&adapter->hs_activate_wait_q);
 867	adapter->cmd_wait_q_required = false;
 868	init_waitqueue_head(&adapter->cmd_wait_q.wait);
 869	adapter->cmd_wait_q.condition = false;
 870	adapter->cmd_wait_q.status = 0;
 
 871
 872	adapter->workqueue = create_workqueue("MWIFIEX_WORK_QUEUE");
 
 
 873	if (!adapter->workqueue)
 874		goto err_kmalloc;
 875
 876	INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
 877
 878	/* Register the device. Fill up the private data structure with relevant
 879	   information from the card and request for the required IRQ. */
 880	if (adapter->if_ops.register_dev(adapter)) {
 881		pr_err("%s: failed to register mwifiex device\n", __func__);
 882		goto err_registerdev;
 883	}
 884
 885	if (mwifiex_init_hw_fw(adapter)) {
 886		pr_err("%s: firmware init failed\n", __func__);
 887		goto err_init_fw;
 888	}
 889
 890	/* Add interfaces */
 891	for (i = 0; i < adapter->drv_mode->intf_num; i++) {
 892		if (!mwifiex_add_interface(adapter, i,
 893				adapter->drv_mode->bss_attr[i].bss_type)) {
 894			goto err_add_intf;
 895		}
 896	}
 897
 898	up(sem);
 899
 900	return 0;
 901
 902err_add_intf:
 903	for (i = 0; i < adapter->priv_num; i++)
 904		mwifiex_remove_interface(adapter, i);
 905err_init_fw:
 906	pr_debug("info: %s: unregister device\n", __func__);
 907	adapter->if_ops.unregister_dev(adapter);
 908err_registerdev:
 909	adapter->surprise_removed = true;
 910	mwifiex_terminate_workqueue(adapter);
 911err_kmalloc:
 912	if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
 913	    (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
 914		pr_debug("info: %s: shutdown mwifiex\n", __func__);
 915		adapter->init_wait_q_woken = false;
 916
 917		if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
 918			wait_event_interruptible(adapter->init_wait_q,
 919						 adapter->init_wait_q_woken);
 920	}
 921
 
 
 
 922	mwifiex_free_adapter(adapter);
 923
 924err_init_sw:
 925	up(sem);
 926
 927exit_sem_err:
 928	return -1;
 929}
 930EXPORT_SYMBOL_GPL(mwifiex_add_card);
 931
 932/*
 933 * This function removes the card.
 934 *
 935 * This function follows the following major steps to remove the device -
 936 *      - Stop data traffic
 937 *      - Shutdown firmware
 938 *      - Remove the logical interfaces
 939 *      - Terminate the work queue
 940 *      - Unregister the device
 941 *      - Free the adapter structure
 942 */
 943int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
 944{
 945	struct mwifiex_private *priv = NULL;
 946	int i;
 947
 948	if (down_interruptible(sem))
 949		goto exit_sem_err;
 950
 951	if (!adapter)
 952		goto exit_remove;
 953
 
 
 
 
 
 954	adapter->surprise_removed = true;
 955
 956	/* Stop data */
 957	for (i = 0; i < adapter->priv_num; i++) {
 958		priv = adapter->priv[i];
 959		if (priv) {
 960			if (!netif_queue_stopped(priv->netdev))
 961				netif_stop_queue(priv->netdev);
 962			if (netif_carrier_ok(priv->netdev))
 963				netif_carrier_off(priv->netdev);
 964		}
 965	}
 966
 967	dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
 968	adapter->init_wait_q_woken = false;
 969
 970	if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
 971		wait_event_interruptible(adapter->init_wait_q,
 972					 adapter->init_wait_q_woken);
 973	dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
 974	if (atomic_read(&adapter->rx_pending) ||
 975	    atomic_read(&adapter->tx_pending) ||
 976	    atomic_read(&adapter->cmd_pending)) {
 977		dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
 978		       "cmd_pending=%d\n",
 979		       atomic_read(&adapter->rx_pending),
 980		       atomic_read(&adapter->tx_pending),
 981		       atomic_read(&adapter->cmd_pending));
 982	}
 983
 984	/* Remove interface */
 985	for (i = 0; i < adapter->priv_num; i++)
 986		mwifiex_remove_interface(adapter, i);
 
 
 
 
 
 
 
 
 
 
 
 987
 988	mwifiex_terminate_workqueue(adapter);
 989
 990	/* Unregister device */
 991	dev_dbg(adapter->dev, "info: unregister device\n");
 992	adapter->if_ops.unregister_dev(adapter);
 
 993	/* Free adapter structure */
 994	dev_dbg(adapter->dev, "info: free adapter\n");
 995	mwifiex_free_adapter(adapter);
 996
 997exit_remove:
 998	up(sem);
 999exit_sem_err:
1000	return 0;
1001}
1002EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1003
1004/*
1005 * This function initializes the module.
1006 *
1007 * The debug FS is also initialized if configured.
1008 */
1009static int
1010mwifiex_init_module(void)
1011{
1012#ifdef CONFIG_DEBUG_FS
1013	mwifiex_debugfs_init();
1014#endif
1015	return 0;
1016}
1017
1018/*
1019 * This function cleans up the module.
1020 *
1021 * The debug FS is removed if available.
1022 */
1023static void
1024mwifiex_cleanup_module(void)
1025{
1026#ifdef CONFIG_DEBUG_FS
1027	mwifiex_debugfs_remove();
1028#endif
1029}
1030
1031module_init(mwifiex_init_module);
1032module_exit(mwifiex_cleanup_module);
1033
1034MODULE_AUTHOR("Marvell International Ltd.");
1035MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1036MODULE_VERSION(VERSION);
1037MODULE_LICENSE("GPL v2");
v3.15
   1/*
   2 * Marvell Wireless LAN device driver: major functions
   3 *
   4 * Copyright (C) 2011, Marvell International Ltd.
   5 *
   6 * This software file (the "File") is distributed by Marvell International
   7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
   8 * (the "License").  You may use, redistribute and/or modify this File in
   9 * accordance with the terms and conditions of the License, a copy of which
  10 * is available by writing to the Free Software Foundation, Inc.,
  11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
  12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  13 *
  14 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
  15 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  16 * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
  17 * this warranty disclaimer.
  18 */
  19
  20#include "main.h"
  21#include "wmm.h"
  22#include "cfg80211.h"
  23#include "11n.h"
  24
  25#define VERSION	"1.0"
  26
  27const char driver_version[] = "mwifiex " VERSION " (%s) ";
  28static char *cal_data_cfg;
  29module_param(cal_data_cfg, charp, 0);
  30
  31static void scan_delay_timer_fn(unsigned long data)
  32{
  33	struct mwifiex_private *priv = (struct mwifiex_private *)data;
  34	struct mwifiex_adapter *adapter = priv->adapter;
  35	struct cmd_ctrl_node *cmd_node, *tmp_node;
  36	unsigned long flags;
  37
  38	if (adapter->surprise_removed)
  39		return;
  40
  41	if (adapter->scan_delay_cnt == MWIFIEX_MAX_SCAN_DELAY_CNT ||
  42	    !adapter->scan_processing) {
  43		/*
  44		 * Abort scan operation by cancelling all pending scan
  45		 * commands
  46		 */
  47		spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
  48		list_for_each_entry_safe(cmd_node, tmp_node,
  49					 &adapter->scan_pending_q, list) {
  50			list_del(&cmd_node->list);
  51			mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  52		}
  53		spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
  54
  55		spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  56		adapter->scan_processing = false;
  57		adapter->scan_delay_cnt = 0;
  58		adapter->empty_tx_q_cnt = 0;
  59		spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  60
  61		if (priv->scan_request) {
  62			dev_dbg(adapter->dev, "info: aborting scan\n");
  63			cfg80211_scan_done(priv->scan_request, 1);
  64			priv->scan_request = NULL;
  65		} else {
  66			priv->scan_aborting = false;
  67			dev_dbg(adapter->dev, "info: scan already aborted\n");
  68		}
  69		goto done;
  70	}
  71
  72	if (!atomic_read(&priv->adapter->is_tx_received)) {
  73		adapter->empty_tx_q_cnt++;
  74		if (adapter->empty_tx_q_cnt == MWIFIEX_MAX_EMPTY_TX_Q_CNT) {
  75			/*
  76			 * No Tx traffic for 200msec. Get scan command from
  77			 * scan pending queue and put to cmd pending queue to
  78			 * resume scan operation
  79			 */
  80			adapter->scan_delay_cnt = 0;
  81			adapter->empty_tx_q_cnt = 0;
  82			spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
  83			cmd_node = list_first_entry(&adapter->scan_pending_q,
  84						    struct cmd_ctrl_node, list);
  85			list_del(&cmd_node->list);
  86			spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
  87					       flags);
  88
  89			mwifiex_insert_cmd_to_pending_q(adapter, cmd_node,
  90							true);
  91			queue_work(adapter->workqueue, &adapter->main_work);
  92			goto done;
  93		}
  94	} else {
  95		adapter->empty_tx_q_cnt = 0;
  96	}
  97
  98	/* Delay scan operation further by 20msec */
  99	mod_timer(&priv->scan_delay_timer, jiffies +
 100		  msecs_to_jiffies(MWIFIEX_SCAN_DELAY_MSEC));
 101	adapter->scan_delay_cnt++;
 102
 103done:
 104	if (atomic_read(&priv->adapter->is_tx_received))
 105		atomic_set(&priv->adapter->is_tx_received, false);
 106
 107	return;
 108}
 109
 110/*
 111 * This function registers the device and performs all the necessary
 112 * initializations.
 113 *
 114 * The following initialization operations are performed -
 115 *      - Allocate adapter structure
 116 *      - Save interface specific operations table in adapter
 117 *      - Call interface specific initialization routine
 118 *      - Allocate private structures
 119 *      - Set default adapter structure parameters
 120 *      - Initialize locks
 121 *
 122 * In case of any errors during inittialization, this function also ensures
 123 * proper cleanup before exiting.
 124 */
 125static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
 
 126			    void **padapter)
 127{
 128	struct mwifiex_adapter *adapter;
 129	int i;
 130
 131	adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
 132	if (!adapter)
 133		return -ENOMEM;
 134
 135	*padapter = adapter;
 136	adapter->card = card;
 137
 138	/* Save interface specific operations in adapter */
 139	memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
 140
 141	/* card specific initialization has been deferred until now .. */
 142	if (adapter->if_ops.init_if)
 143		if (adapter->if_ops.init_if(adapter))
 144			goto error;
 145
 146	adapter->priv_num = 0;
 
 
 
 
 
 147
 148	for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
 149		/* Allocate memory for private structure */
 150		adapter->priv[i] =
 151			kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
 152		if (!adapter->priv[i])
 
 
 153			goto error;
 
 154
 
 155		adapter->priv[i]->adapter = adapter;
 156		adapter->priv_num++;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 157
 158		setup_timer(&adapter->priv[i]->scan_delay_timer,
 159			    scan_delay_timer_fn,
 160			    (unsigned long)adapter->priv[i]);
 161	}
 162	mwifiex_init_lock_list(adapter);
 163
 164	init_timer(&adapter->cmd_timer);
 165	adapter->cmd_timer.function = mwifiex_cmd_timeout_func;
 166	adapter->cmd_timer.data = (unsigned long) adapter;
 167
 168	return 0;
 169
 170error:
 171	dev_dbg(adapter->dev, "info: leave mwifiex_register with error\n");
 172
 173	for (i = 0; i < adapter->priv_num; i++)
 
 174		kfree(adapter->priv[i]);
 175
 176	kfree(adapter);
 177
 178	return -1;
 179}
 180
 181/*
 182 * This function unregisters the device and performs all the necessary
 183 * cleanups.
 184 *
 185 * The following cleanup operations are performed -
 186 *      - Free the timers
 187 *      - Free beacon buffers
 188 *      - Free private structures
 189 *      - Free adapter structure
 190 */
 191static int mwifiex_unregister(struct mwifiex_adapter *adapter)
 192{
 193	s32 i;
 194
 195	if (adapter->if_ops.cleanup_if)
 196		adapter->if_ops.cleanup_if(adapter);
 197
 198	del_timer_sync(&adapter->cmd_timer);
 199
 200	/* Free private structures */
 201	for (i = 0; i < adapter->priv_num; i++) {
 202		if (adapter->priv[i]) {
 203			mwifiex_free_curr_bcn(adapter->priv[i]);
 204			del_timer_sync(&adapter->priv[i]->scan_delay_timer);
 205			kfree(adapter->priv[i]);
 206		}
 207	}
 208
 209	kfree(adapter);
 210	return 0;
 211}
 212
 213/*
 214 * The main process.
 215 *
 216 * This function is the main procedure of the driver and handles various driver
 217 * operations. It runs in a loop and provides the core functionalities.
 218 *
 219 * The main responsibilities of this function are -
 220 *      - Ensure concurrency control
 221 *      - Handle pending interrupts and call interrupt handlers
 222 *      - Wake up the card if required
 223 *      - Handle command responses and call response handlers
 224 *      - Handle events and call event handlers
 225 *      - Execute pending commands
 226 *      - Transmit pending data packets
 227 */
 228int mwifiex_main_process(struct mwifiex_adapter *adapter)
 229{
 230	int ret = 0;
 231	unsigned long flags;
 232	struct sk_buff *skb;
 233
 234	spin_lock_irqsave(&adapter->main_proc_lock, flags);
 235
 236	/* Check if already processing */
 237	if (adapter->mwifiex_processing) {
 238		spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
 239		goto exit_main_proc;
 240	} else {
 241		adapter->mwifiex_processing = true;
 242		spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
 243	}
 244process_start:
 245	do {
 246		if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
 247		    (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
 248			break;
 249
 250		/* Handle pending interrupt if any */
 251		if (adapter->int_status) {
 252			if (adapter->hs_activated)
 253				mwifiex_process_hs_config(adapter);
 254			if (adapter->if_ops.process_int_status)
 255				adapter->if_ops.process_int_status(adapter);
 256		}
 257
 258		/* Need to wake up the card ? */
 259		if ((adapter->ps_state == PS_STATE_SLEEP) &&
 260		    (adapter->pm_wakeup_card_req &&
 261		     !adapter->pm_wakeup_fw_try) &&
 262		    (is_command_pending(adapter) ||
 263		     !mwifiex_wmm_lists_empty(adapter))) {
 264			adapter->pm_wakeup_fw_try = true;
 265			adapter->if_ops.wakeup(adapter);
 266			continue;
 267		}
 268
 269		if (IS_CARD_RX_RCVD(adapter)) {
 270			adapter->pm_wakeup_fw_try = false;
 271			if (adapter->ps_state == PS_STATE_SLEEP)
 272				adapter->ps_state = PS_STATE_AWAKE;
 273		} else {
 274			/* We have tried to wakeup the card already */
 275			if (adapter->pm_wakeup_fw_try)
 276				break;
 277			if (adapter->ps_state != PS_STATE_AWAKE ||
 278			    adapter->tx_lock_flag)
 279				break;
 280
 281			if ((adapter->scan_processing &&
 282			     !adapter->scan_delay_cnt) || adapter->data_sent ||
 283			    mwifiex_wmm_lists_empty(adapter)) {
 284				if (adapter->cmd_sent || adapter->curr_cmd ||
 285				    (!is_command_pending(adapter)))
 286					break;
 287			}
 288		}
 289
 290		/* Check Rx data for USB */
 291		if (adapter->iface_type == MWIFIEX_USB)
 292			while ((skb = skb_dequeue(&adapter->usb_rx_data_q)))
 293				mwifiex_handle_rx_packet(adapter, skb);
 294
 295		/* Check for event */
 296		if (adapter->event_received) {
 297			adapter->event_received = false;
 298			mwifiex_process_event(adapter);
 299		}
 300
 301		/* Check for Cmd Resp */
 302		if (adapter->cmd_resp_received) {
 303			adapter->cmd_resp_received = false;
 304			mwifiex_process_cmdresp(adapter);
 305
 306			/* call mwifiex back when init_fw is done */
 307			if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
 308				adapter->hw_status = MWIFIEX_HW_STATUS_READY;
 309				mwifiex_init_fw_complete(adapter);
 310			}
 311		}
 312
 
 
 
 
 
 
 313		/* Check if we need to confirm Sleep Request
 314		   received previously */
 315		if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
 316			if (!adapter->cmd_sent && !adapter->curr_cmd)
 317				mwifiex_check_ps_cond(adapter);
 318		}
 319
 320		/* * The ps_state may have been changed during processing of
 321		 * Sleep Request event.
 322		 */
 323		if ((adapter->ps_state == PS_STATE_SLEEP) ||
 324		    (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
 325		    (adapter->ps_state == PS_STATE_SLEEP_CFM) ||
 326		    adapter->tx_lock_flag)
 327			continue;
 328
 329		if (!adapter->cmd_sent && !adapter->curr_cmd) {
 330			if (mwifiex_exec_next_cmd(adapter) == -1) {
 331				ret = -1;
 332				break;
 333			}
 334		}
 335
 336		if ((!adapter->scan_processing || adapter->scan_delay_cnt) &&
 337		    !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter)) {
 338			mwifiex_wmm_process_tx(adapter);
 339			if (adapter->hs_activated) {
 340				adapter->is_hs_configured = false;
 341				mwifiex_hs_activated_event
 342					(mwifiex_get_priv
 343					 (adapter, MWIFIEX_BSS_ROLE_ANY),
 344					 false);
 345			}
 346		}
 347
 348		if (adapter->delay_null_pkt && !adapter->cmd_sent &&
 349		    !adapter->curr_cmd && !is_command_pending(adapter) &&
 350		    mwifiex_wmm_lists_empty(adapter)) {
 351			if (!mwifiex_send_null_packet
 352			    (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
 353			     MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
 354			     MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
 355				adapter->delay_null_pkt = false;
 356				adapter->ps_state = PS_STATE_SLEEP;
 357			}
 358			break;
 359		}
 360	} while (true);
 361
 362	spin_lock_irqsave(&adapter->main_proc_lock, flags);
 363	if ((adapter->int_status) || IS_CARD_RX_RCVD(adapter)) {
 364		spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
 365		goto process_start;
 366	}
 367
 
 368	adapter->mwifiex_processing = false;
 369	spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
 370
 371exit_main_proc:
 372	if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
 373		mwifiex_shutdown_drv(adapter);
 374	return ret;
 375}
 376EXPORT_SYMBOL_GPL(mwifiex_main_process);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 377
 378/*
 379 * This function frees the adapter structure.
 380 *
 381 * Additionally, this closes the netlink socket, frees the timers
 382 * and private structures.
 383 */
 384static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
 385{
 386	if (!adapter) {
 387		pr_err("%s: adapter is NULL\n", __func__);
 388		return;
 389	}
 390
 391	mwifiex_unregister(adapter);
 392	pr_debug("info: %s: free adapter\n", __func__);
 393}
 394
 395/*
 396 * This function cancels all works in the queue and destroys
 397 * the main workqueue.
 398 */
 399static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
 400{
 401	flush_workqueue(adapter->workqueue);
 402	destroy_workqueue(adapter->workqueue);
 403	adapter->workqueue = NULL;
 404}
 405
 406/*
 407 * This function gets firmware and initializes it.
 408 *
 409 * The main initialization steps followed are -
 410 *      - Download the correct firmware to card
 
 
 411 *      - Issue the init commands to firmware
 412 */
 413static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
 414{
 415	int ret;
 416	char fmt[64];
 417	struct mwifiex_private *priv;
 418	struct mwifiex_adapter *adapter = context;
 419	struct mwifiex_fw_image fw;
 420	struct semaphore *sem = adapter->card_sem;
 421	bool init_failed = false;
 422	struct wireless_dev *wdev;
 423
 424	if (!firmware) {
 425		dev_err(adapter->dev,
 426			"Failed to get firmware %s\n", adapter->fw_name);
 427		goto err_dnld_fw;
 428	}
 429
 430	memset(&fw, 0, sizeof(struct mwifiex_fw_image));
 431	adapter->firmware = firmware;
 
 
 
 
 
 
 
 
 432	fw.fw_buf = (u8 *) adapter->firmware->data;
 433	fw.fw_len = adapter->firmware->size;
 434
 435	if (adapter->if_ops.dnld_fw)
 436		ret = adapter->if_ops.dnld_fw(adapter, &fw);
 437	else
 438		ret = mwifiex_dnld_fw(adapter, &fw);
 439	if (ret == -1)
 440		goto err_dnld_fw;
 441
 442	dev_notice(adapter->dev, "WLAN FW is active\n");
 443
 444	if (cal_data_cfg) {
 445		if ((request_firmware(&adapter->cal_data, cal_data_cfg,
 446				      adapter->dev)) < 0)
 447			dev_err(adapter->dev,
 448				"Cal data request_firmware() failed\n");
 449	}
 450
 451	/* enable host interrupt after fw dnld is successful */
 452	if (adapter->if_ops.enable_int) {
 453		if (adapter->if_ops.enable_int(adapter))
 454			goto err_dnld_fw;
 455	}
 456
 457	adapter->init_wait_q_woken = false;
 458	ret = mwifiex_init_fw(adapter);
 459	if (ret == -1) {
 460		goto err_init_fw;
 461	} else if (!ret) {
 462		adapter->hw_status = MWIFIEX_HW_STATUS_READY;
 463		goto done;
 464	}
 465	/* Wait for mwifiex_init to complete */
 466	wait_event_interruptible(adapter->init_wait_q,
 467				 adapter->init_wait_q_woken);
 468	if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
 469		goto err_init_fw;
 470
 471	priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
 472	if (mwifiex_register_cfg80211(adapter)) {
 473		dev_err(adapter->dev, "cannot register with cfg80211\n");
 474		goto err_init_fw;
 475	}
 
 476
 477	rtnl_lock();
 478	/* Create station interface by default */
 479	wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d",
 480					NL80211_IFTYPE_STATION, NULL, NULL);
 481	if (IS_ERR(wdev)) {
 482		dev_err(adapter->dev, "cannot create default STA interface\n");
 483		rtnl_unlock();
 484		goto err_add_intf;
 485	}
 486	rtnl_unlock();
 487
 488	mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
 489	dev_notice(adapter->dev, "driver_version = %s\n", fmt);
 490	goto done;
 491
 492err_add_intf:
 493	wiphy_unregister(adapter->wiphy);
 494	wiphy_free(adapter->wiphy);
 495err_init_fw:
 496	if (adapter->if_ops.disable_int)
 497		adapter->if_ops.disable_int(adapter);
 498err_dnld_fw:
 499	pr_debug("info: %s: unregister device\n", __func__);
 500	if (adapter->if_ops.unregister_dev)
 501		adapter->if_ops.unregister_dev(adapter);
 502
 503	if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
 504	    (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
 505		pr_debug("info: %s: shutdown mwifiex\n", __func__);
 506		adapter->init_wait_q_woken = false;
 507
 508		if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
 509			wait_event_interruptible(adapter->init_wait_q,
 510						 adapter->init_wait_q_woken);
 511	}
 512	adapter->surprise_removed = true;
 513	mwifiex_terminate_workqueue(adapter);
 514	init_failed = true;
 515done:
 516	if (adapter->cal_data) {
 517		release_firmware(adapter->cal_data);
 518		adapter->cal_data = NULL;
 519	}
 520	if (adapter->firmware) {
 521		release_firmware(adapter->firmware);
 522		adapter->firmware = NULL;
 523	}
 524	complete(&adapter->fw_load);
 525	if (init_failed)
 526		mwifiex_free_adapter(adapter);
 527	up(sem);
 528	return;
 529}
 530
 531/*
 532 * This function initializes the hardware and gets firmware.
 
 
 
 
 533 */
 534static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
 
 535{
 536	int ret;
 
 
 
 537
 538	init_completion(&adapter->fw_load);
 539	ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
 540				      adapter->dev, GFP_KERNEL, adapter,
 541				      mwifiex_fw_dpc);
 542	if (ret < 0)
 543		dev_err(adapter->dev,
 544			"request_firmware_nowait() returned error %d\n", ret);
 545	return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 546}
 547
 548/*
 549 * CFG802.11 network device handler for open.
 550 *
 551 * Starts the data queue.
 552 */
 553static int
 554mwifiex_open(struct net_device *dev)
 555{
 556	netif_tx_start_all_queues(dev);
 557	return 0;
 558}
 559
 560/*
 561 * CFG802.11 network device handler for close.
 562 */
 563static int
 564mwifiex_close(struct net_device *dev)
 565{
 566	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
 567
 568	if (priv->scan_request) {
 569		dev_dbg(priv->adapter->dev, "aborting scan on ndo_stop\n");
 570		cfg80211_scan_done(priv->scan_request, 1);
 571		priv->scan_request = NULL;
 572		priv->scan_aborting = true;
 573	}
 574
 575	return 0;
 576}
 577
 578/*
 579 * Add buffer into wmm tx queue and queue work to transmit it.
 580 */
 581int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
 582{
 583	struct netdev_queue *txq;
 584	int index = mwifiex_1d_to_wmm_queue[skb->priority];
 585
 586	if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
 587		txq = netdev_get_tx_queue(priv->netdev, index);
 588		if (!netif_tx_queue_stopped(txq)) {
 589			netif_tx_stop_queue(txq);
 590			dev_dbg(priv->adapter->dev, "stop queue: %d\n", index);
 591		}
 592	}
 593
 594	atomic_inc(&priv->adapter->tx_pending);
 595	mwifiex_wmm_add_buf_txqueue(priv, skb);
 596
 597	if (priv->adapter->scan_delay_cnt)
 598		atomic_set(&priv->adapter->is_tx_received, true);
 599
 600	queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
 601
 602	return 0;
 603}
 604
 605/*
 606 * CFG802.11 network device handler for data transmission.
 607 */
 608static int
 609mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
 610{
 611	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
 612	struct sk_buff *new_skb;
 613	struct mwifiex_txinfo *tx_info;
 614	struct timeval tv;
 615
 616	dev_dbg(priv->adapter->dev, "data: %lu BSS(%d-%d): Data <= kernel\n",
 617		jiffies, priv->bss_type, priv->bss_num);
 618
 619	if (priv->adapter->surprise_removed) {
 620		kfree_skb(skb);
 621		priv->stats.tx_dropped++;
 622		return 0;
 623	}
 624	if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
 625		dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
 626		kfree_skb(skb);
 627		priv->stats.tx_dropped++;
 628		return 0;
 629	}
 630	if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
 631		dev_dbg(priv->adapter->dev,
 632			"data: Tx: insufficient skb headroom %d\n",
 633			skb_headroom(skb));
 634		/* Insufficient skb headroom - allocate a new skb */
 635		new_skb =
 636			skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
 637		if (unlikely(!new_skb)) {
 638			dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
 639			kfree_skb(skb);
 640			priv->stats.tx_dropped++;
 641			return 0;
 642		}
 643		kfree_skb(skb);
 644		skb = new_skb;
 645		dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
 646			skb_headroom(skb));
 647	}
 648
 649	tx_info = MWIFIEX_SKB_TXCB(skb);
 650	tx_info->bss_num = priv->bss_num;
 651	tx_info->bss_type = priv->bss_type;
 652	tx_info->pkt_len = skb->len;
 
 
 653
 654	/* Record the current time the packet was queued; used to
 655	 * determine the amount of time the packet was queued in
 656	 * the driver before it was sent to the firmware.
 657	 * The delay is then sent along with the packet to the
 658	 * firmware for aggregate delay calculation for stats and
 659	 * MSDU lifetime expiry.
 660	 */
 661	do_gettimeofday(&tv);
 662	skb->tstamp = timeval_to_ktime(tv);
 663
 664	mwifiex_queue_tx_pkt(priv, skb);
 665
 666	return 0;
 667}
 668
 669/*
 670 * CFG802.11 network device handler for setting MAC address.
 671 */
 672static int
 673mwifiex_set_mac_address(struct net_device *dev, void *addr)
 674{
 675	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
 676	struct sockaddr *hw_addr = addr;
 677	int ret;
 678
 679	memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
 680
 681	/* Send request to firmware */
 682	ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
 683			       HostCmd_ACT_GEN_SET, 0, NULL, true);
 684
 685	if (!ret)
 686		memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
 687	else
 688		dev_err(priv->adapter->dev,
 689			"set mac address failed: ret=%d\n", ret);
 690
 691	memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
 692
 693	return ret;
 694}
 695
 696/*
 697 * CFG802.11 network device handler for setting multicast list.
 698 */
 699static void mwifiex_set_multicast_list(struct net_device *dev)
 700{
 701	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
 702	struct mwifiex_multicast_list mcast_list;
 703
 704	if (dev->flags & IFF_PROMISC) {
 705		mcast_list.mode = MWIFIEX_PROMISC_MODE;
 706	} else if (dev->flags & IFF_ALLMULTI ||
 707		   netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
 708		mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
 709	} else {
 710		mcast_list.mode = MWIFIEX_MULTICAST_MODE;
 711		mcast_list.num_multicast_addr =
 712			mwifiex_copy_mcast_addr(&mcast_list, dev);
 
 713	}
 714	mwifiex_request_set_multicast_list(priv, &mcast_list);
 715}
 716
 717/*
 718 * CFG802.11 network device handler for transmission timeout.
 719 */
 720static void
 721mwifiex_tx_timeout(struct net_device *dev)
 722{
 723	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
 724
 
 
 
 725	priv->num_tx_timeout++;
 726	priv->tx_timeout_cnt++;
 727	dev_err(priv->adapter->dev,
 728		"%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
 729		jiffies, priv->tx_timeout_cnt, priv->bss_type, priv->bss_num);
 730	mwifiex_set_trans_start(dev);
 731
 732	if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
 733	    priv->adapter->if_ops.card_reset) {
 734		dev_err(priv->adapter->dev,
 735			"tx_timeout_cnt exceeds threshold. Triggering card reset!\n");
 736		priv->adapter->if_ops.card_reset(priv->adapter);
 737	}
 738}
 739
 740/*
 741 * CFG802.11 network device handler for statistics retrieval.
 742 */
 743static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
 744{
 745	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
 746
 747	return &priv->stats;
 748}
 749
 750static u16
 751mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
 752				void *accel_priv, select_queue_fallback_t fallback)
 753{
 754	skb->priority = cfg80211_classify8021d(skb, NULL);
 755	return mwifiex_1d_to_wmm_queue[skb->priority];
 756}
 757
 758/* Network device handlers */
 759static const struct net_device_ops mwifiex_netdev_ops = {
 760	.ndo_open = mwifiex_open,
 761	.ndo_stop = mwifiex_close,
 762	.ndo_start_xmit = mwifiex_hard_start_xmit,
 763	.ndo_set_mac_address = mwifiex_set_mac_address,
 764	.ndo_tx_timeout = mwifiex_tx_timeout,
 765	.ndo_get_stats = mwifiex_get_stats,
 766	.ndo_set_rx_mode = mwifiex_set_multicast_list,
 767	.ndo_select_queue = mwifiex_netdev_select_wmm_queue,
 768};
 769
 770/*
 771 * This function initializes the private structure parameters.
 772 *
 773 * The following wait queues are initialized -
 774 *      - IOCTL wait queue
 775 *      - Command wait queue
 776 *      - Statistics wait queue
 777 *
 778 * ...and the following default parameters are set -
 779 *      - Current key index     : Set to 0
 780 *      - Rate index            : Set to auto
 781 *      - Media connected       : Set to disconnected
 782 *      - Adhoc link sensed     : Set to false
 783 *      - Nick name             : Set to null
 784 *      - Number of Tx timeout  : Set to 0
 785 *      - Device address        : Set to current address
 786 *
 787 * In addition, the CFG80211 work queue is also created.
 788 */
 789void mwifiex_init_priv_params(struct mwifiex_private *priv,
 790						struct net_device *dev)
 791{
 792	dev->netdev_ops = &mwifiex_netdev_ops;
 793	dev->destructor = free_netdev;
 794	/* Initialize private structure */
 795	priv->current_key_index = 0;
 796	priv->media_connected = false;
 797	memset(&priv->nick_name, 0, sizeof(priv->nick_name));
 798	memset(priv->mgmt_ie, 0,
 799	       sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
 800	priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
 801	priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
 802	priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
 803	priv->rsn_idx = MWIFIEX_AUTO_IDX_MASK;
 804	priv->num_tx_timeout = 0;
 
 
 805	memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
 806}
 807
 808/*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 809 * This function check if command is pending.
 810 */
 811int is_command_pending(struct mwifiex_adapter *adapter)
 812{
 813	unsigned long flags;
 814	int is_cmd_pend_q_empty;
 815
 816	spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
 817	is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
 818	spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
 819
 820	return !is_cmd_pend_q_empty;
 821}
 822
 823/*
 
 
 
 
 
 
 
 
 
 
 
 
 824 * This is the main work queue function.
 825 *
 826 * It handles the main process, which in turn handles the complete
 827 * driver operations.
 828 */
 829static void mwifiex_main_work_queue(struct work_struct *work)
 830{
 831	struct mwifiex_adapter *adapter =
 832		container_of(work, struct mwifiex_adapter, main_work);
 833
 834	if (adapter->surprise_removed)
 835		return;
 836	mwifiex_main_process(adapter);
 837}
 838
 839/*
 
 
 
 
 
 
 
 
 
 
 
 
 840 * This function adds the card.
 841 *
 842 * This function follows the following major steps to set up the device -
 843 *      - Initialize software. This includes probing the card, registering
 844 *        the interface operations table, and allocating/initializing the
 845 *        adapter structure
 846 *      - Set up the netlink socket
 847 *      - Create and start the main work queue
 848 *      - Register the device
 849 *      - Initialize firmware and hardware
 850 *      - Add logical interfaces
 851 */
 852int
 853mwifiex_add_card(void *card, struct semaphore *sem,
 854		 struct mwifiex_if_ops *if_ops, u8 iface_type)
 855{
 
 856	struct mwifiex_adapter *adapter;
 857
 858	if (down_interruptible(sem))
 859		goto exit_sem_err;
 860
 861	if (mwifiex_register(card, if_ops, (void **)&adapter)) {
 862		pr_err("%s: software init failed\n", __func__);
 863		goto err_init_sw;
 864	}
 865
 866	adapter->iface_type = iface_type;
 867	adapter->card_sem = sem;
 868
 869	adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
 870	adapter->surprise_removed = false;
 871	init_waitqueue_head(&adapter->init_wait_q);
 872	adapter->is_suspended = false;
 873	adapter->hs_activated = false;
 874	init_waitqueue_head(&adapter->hs_activate_wait_q);
 
 875	init_waitqueue_head(&adapter->cmd_wait_q.wait);
 
 876	adapter->cmd_wait_q.status = 0;
 877	adapter->scan_wait_q_woken = false;
 878
 879	adapter->workqueue =
 880		alloc_workqueue("MWIFIEX_WORK_QUEUE",
 881				WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
 882	if (!adapter->workqueue)
 883		goto err_kmalloc;
 884
 885	INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
 886
 887	/* Register the device. Fill up the private data structure with relevant
 888	   information from the card. */
 889	if (adapter->if_ops.register_dev(adapter)) {
 890		pr_err("%s: failed to register mwifiex device\n", __func__);
 891		goto err_registerdev;
 892	}
 893
 894	if (mwifiex_init_hw_fw(adapter)) {
 895		pr_err("%s: firmware init failed\n", __func__);
 896		goto err_init_fw;
 897	}
 898
 
 
 
 
 
 
 
 
 
 
 899	return 0;
 900
 
 
 
 901err_init_fw:
 902	pr_debug("info: %s: unregister device\n", __func__);
 903	if (adapter->if_ops.unregister_dev)
 904		adapter->if_ops.unregister_dev(adapter);
 
 
 
 905	if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
 906	    (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
 907		pr_debug("info: %s: shutdown mwifiex\n", __func__);
 908		adapter->init_wait_q_woken = false;
 909
 910		if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
 911			wait_event_interruptible(adapter->init_wait_q,
 912						 adapter->init_wait_q_woken);
 913	}
 914err_registerdev:
 915	adapter->surprise_removed = true;
 916	mwifiex_terminate_workqueue(adapter);
 917err_kmalloc:
 918	mwifiex_free_adapter(adapter);
 919
 920err_init_sw:
 921	up(sem);
 922
 923exit_sem_err:
 924	return -1;
 925}
 926EXPORT_SYMBOL_GPL(mwifiex_add_card);
 927
 928/*
 929 * This function removes the card.
 930 *
 931 * This function follows the following major steps to remove the device -
 932 *      - Stop data traffic
 933 *      - Shutdown firmware
 934 *      - Remove the logical interfaces
 935 *      - Terminate the work queue
 936 *      - Unregister the device
 937 *      - Free the adapter structure
 938 */
 939int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
 940{
 941	struct mwifiex_private *priv = NULL;
 942	int i;
 943
 944	if (down_interruptible(sem))
 945		goto exit_sem_err;
 946
 947	if (!adapter)
 948		goto exit_remove;
 949
 950	/* We can no longer handle interrupts once we start doing the teardown
 951	 * below. */
 952	if (adapter->if_ops.disable_int)
 953		adapter->if_ops.disable_int(adapter);
 954
 955	adapter->surprise_removed = true;
 956
 957	/* Stop data */
 958	for (i = 0; i < adapter->priv_num; i++) {
 959		priv = adapter->priv[i];
 960		if (priv && priv->netdev) {
 961			mwifiex_stop_net_dev_queue(priv->netdev, adapter);
 
 962			if (netif_carrier_ok(priv->netdev))
 963				netif_carrier_off(priv->netdev);
 964		}
 965	}
 966
 967	dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
 968	adapter->init_wait_q_woken = false;
 969
 970	if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
 971		wait_event_interruptible(adapter->init_wait_q,
 972					 adapter->init_wait_q_woken);
 973	dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
 974	if (atomic_read(&adapter->rx_pending) ||
 975	    atomic_read(&adapter->tx_pending) ||
 976	    atomic_read(&adapter->cmd_pending)) {
 977		dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
 978		       "cmd_pending=%d\n",
 979		       atomic_read(&adapter->rx_pending),
 980		       atomic_read(&adapter->tx_pending),
 981		       atomic_read(&adapter->cmd_pending));
 982	}
 983
 984	for (i = 0; i < adapter->priv_num; i++) {
 985		priv = adapter->priv[i];
 986
 987		if (!priv)
 988			continue;
 989
 990		rtnl_lock();
 991		if (priv->wdev && priv->netdev)
 992			mwifiex_del_virtual_intf(adapter->wiphy, priv->wdev);
 993		rtnl_unlock();
 994	}
 995
 996	wiphy_unregister(adapter->wiphy);
 997	wiphy_free(adapter->wiphy);
 998
 999	mwifiex_terminate_workqueue(adapter);
1000
1001	/* Unregister device */
1002	dev_dbg(adapter->dev, "info: unregister device\n");
1003	if (adapter->if_ops.unregister_dev)
1004		adapter->if_ops.unregister_dev(adapter);
1005	/* Free adapter structure */
1006	dev_dbg(adapter->dev, "info: free adapter\n");
1007	mwifiex_free_adapter(adapter);
1008
1009exit_remove:
1010	up(sem);
1011exit_sem_err:
1012	return 0;
1013}
1014EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1015
1016/*
1017 * This function initializes the module.
1018 *
1019 * The debug FS is also initialized if configured.
1020 */
1021static int
1022mwifiex_init_module(void)
1023{
1024#ifdef CONFIG_DEBUG_FS
1025	mwifiex_debugfs_init();
1026#endif
1027	return 0;
1028}
1029
1030/*
1031 * This function cleans up the module.
1032 *
1033 * The debug FS is removed if available.
1034 */
1035static void
1036mwifiex_cleanup_module(void)
1037{
1038#ifdef CONFIG_DEBUG_FS
1039	mwifiex_debugfs_remove();
1040#endif
1041}
1042
1043module_init(mwifiex_init_module);
1044module_exit(mwifiex_cleanup_module);
1045
1046MODULE_AUTHOR("Marvell International Ltd.");
1047MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1048MODULE_VERSION(VERSION);
1049MODULE_LICENSE("GPL v2");