Linux Audio

Check our new training course

Loading...
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
   4 * All rights reserved.
   5 */
   6
   7#include <linux/irq.h>
   8#include <linux/kthread.h>
   9#include <linux/firmware.h>
  10#include <linux/netdevice.h>
  11#include <linux/inetdevice.h>
  12
  13#include "cfg80211.h"
  14#include "wlan_cfg.h"
  15
  16#define WILC_MULTICAST_TABLE_SIZE	8
  17#define WILC_MAX_FW_VERSION_STR_SIZE	50
  18
  19/* latest API version supported */
  20#define WILC1000_API_VER		1
  21
  22#define WILC1000_FW_PREFIX		"atmel/wilc1000_wifi_firmware-"
  23#define __WILC1000_FW(api)		WILC1000_FW_PREFIX #api ".bin"
  24#define WILC1000_FW(api)		__WILC1000_FW(api)
  25
  26#define WILC3000_API_VER		1
  27
  28#define WILC3000_FW_PREFIX		"atmel/wilc3000_wifi_firmware-"
  29#define __WILC3000_FW(api)		WILC3000_FW_PREFIX #api ".bin"
  30#define WILC3000_FW(api)		__WILC3000_FW(api)
  31
  32static irqreturn_t isr_uh_routine(int irq, void *user_data)
  33{
  34	struct wilc *wilc = user_data;
  35
  36	if (wilc->close) {
  37		pr_err("Can't handle UH interrupt\n");
  38		return IRQ_HANDLED;
  39	}
  40	return IRQ_WAKE_THREAD;
  41}
  42
  43static irqreturn_t isr_bh_routine(int irq, void *userdata)
  44{
  45	struct wilc *wilc = userdata;
  46
  47	if (wilc->close) {
  48		pr_err("Can't handle BH interrupt\n");
  49		return IRQ_HANDLED;
  50	}
  51
  52	wilc_handle_isr(wilc);
  53
  54	return IRQ_HANDLED;
  55}
  56
  57static int init_irq(struct net_device *dev)
  58{
  59	struct wilc_vif *vif = netdev_priv(dev);
  60	struct wilc *wl = vif->wilc;
  61	int ret;
  62
  63	ret = request_threaded_irq(wl->dev_irq_num, isr_uh_routine,
  64				   isr_bh_routine,
  65				   IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  66				   dev->name, wl);
  67	if (ret) {
  68		netdev_err(dev, "Failed to request IRQ [%d]\n", ret);
  69		return ret;
  70	}
  71	netdev_dbg(dev, "IRQ request succeeded IRQ-NUM= %d\n", wl->dev_irq_num);
  72
  73	return 0;
  74}
  75
  76static void deinit_irq(struct net_device *dev)
  77{
  78	struct wilc_vif *vif = netdev_priv(dev);
  79	struct wilc *wilc = vif->wilc;
  80
  81	/* Deinitialize IRQ */
  82	if (wilc->dev_irq_num)
  83		free_irq(wilc->dev_irq_num, wilc);
  84}
  85
  86void wilc_mac_indicate(struct wilc *wilc)
  87{
  88	s8 status;
  89
  90	wilc_wlan_cfg_get_val(wilc, WID_STATUS, &status, 1);
  91	if (wilc->mac_status == WILC_MAC_STATUS_INIT) {
  92		wilc->mac_status = status;
  93		complete(&wilc->sync_event);
  94	} else {
  95		wilc->mac_status = status;
  96	}
  97}
  98
  99static struct net_device *get_if_handler(struct wilc *wilc, u8 *mac_header)
 100{
 101	struct net_device *ndev = NULL;
 102	struct wilc_vif *vif;
 103	struct ieee80211_hdr *h = (struct ieee80211_hdr *)mac_header;
 104
 105	wilc_for_each_vif(wilc, vif) {
 106		if (vif->iftype == WILC_STATION_MODE)
 107			if (ether_addr_equal_unaligned(h->addr2, vif->bssid)) {
 108				ndev = vif->ndev;
 109				goto out;
 110			}
 111		if (vif->iftype == WILC_AP_MODE)
 112			if (ether_addr_equal_unaligned(h->addr1, vif->bssid)) {
 113				ndev = vif->ndev;
 114				goto out;
 115			}
 116	}
 117out:
 118	return ndev;
 119}
 120
 121void wilc_wlan_set_bssid(struct net_device *wilc_netdev, const u8 *bssid,
 122			 u8 mode)
 123{
 124	struct wilc_vif *vif = netdev_priv(wilc_netdev);
 125
 126	if (bssid)
 127		ether_addr_copy(vif->bssid, bssid);
 128	else
 129		eth_zero_addr(vif->bssid);
 130
 131	vif->iftype = mode;
 132}
 133
 134int wilc_wlan_get_num_conn_ifcs(struct wilc *wilc)
 135{
 136	int srcu_idx;
 137	u8 ret_val = 0;
 138	struct wilc_vif *vif;
 139
 140	srcu_idx = srcu_read_lock(&wilc->srcu);
 141	wilc_for_each_vif(wilc, vif) {
 142		if (!is_zero_ether_addr(vif->bssid))
 143			ret_val++;
 144	}
 145	srcu_read_unlock(&wilc->srcu, srcu_idx);
 146	return ret_val;
 147}
 148
 149static void wilc_wake_tx_queues(struct wilc *wl)
 150{
 151	int srcu_idx;
 152	struct wilc_vif *ifc;
 153
 154	srcu_idx = srcu_read_lock(&wl->srcu);
 155	wilc_for_each_vif(wl, ifc) {
 156		if (ifc->mac_opened && netif_queue_stopped(ifc->ndev))
 157			netif_wake_queue(ifc->ndev);
 158	}
 159	srcu_read_unlock(&wl->srcu, srcu_idx);
 160}
 161
 162static int wilc_txq_task(void *vp)
 163{
 164	int ret;
 165	u32 txq_count;
 166	struct wilc *wl = vp;
 167
 168	complete(&wl->txq_thread_started);
 169	while (1) {
 170		if (wait_for_completion_interruptible(&wl->txq_event))
 171			continue;
 172		if (wl->close) {
 173			complete(&wl->txq_thread_started);
 174
 175			while (!kthread_should_stop())
 176				schedule();
 177			break;
 178		}
 179		do {
 180			ret = wilc_wlan_handle_txq(wl, &txq_count);
 181			if (txq_count < FLOW_CONTROL_LOWER_THRESHOLD) {
 182				wilc_wake_tx_queues(wl);
 183			}
 184			if (ret != WILC_VMM_ENTRY_FULL_RETRY)
 185				break;
 186			/* Back off TX task from sending packets for some time.
 187			 * msleep_interruptible will allow RX task to run and
 188			 * free buffers. TX task will be in TASK_INTERRUPTIBLE
 189			 * state which will put the thread back to CPU running
 190			 * queue when it's signaled even if the timeout isn't
 191			 * elapsed. This gives faster chance for reserved SK
 192			 * buffers to be free.
 193			 */
 194			msleep_interruptible(TX_BACKOFF_WEIGHT_MS);
 195		} while (!wl->close);
 196	}
 197	return 0;
 198}
 199
 200static int wilc_wlan_get_firmware(struct net_device *dev)
 201{
 202	struct wilc_vif *vif = netdev_priv(dev);
 203	struct wilc *wilc = vif->wilc;
 
 204	const struct firmware *wilc_fw;
 205	char *firmware;
 206	int ret;
 207
 208	if (is_wilc1000(wilc->chipid))
 209		firmware = WILC1000_FW(WILC1000_API_VER);
 210	else if (is_wilc3000(wilc->chipid))
 211		firmware = WILC3000_FW(WILC3000_API_VER);
 212	else
 213		return -EINVAL;
 214
 215	netdev_info(dev, "WILC%d loading firmware [%s]\n",
 216		    is_wilc1000(wilc->chipid) ? 1000 : 3000,
 217		    firmware);
 218
 219	ret = request_firmware(&wilc_fw, firmware, wilc->dev);
 
 220	if (ret != 0) {
 221		netdev_err(dev, "%s - firmware not available\n", firmware);
 
 222		return -EINVAL;
 223	}
 224	wilc->firmware = wilc_fw;
 225
 226	return 0;
 227}
 228
 229static int wilc_start_firmware(struct net_device *dev)
 230{
 231	struct wilc_vif *vif = netdev_priv(dev);
 232	struct wilc *wilc = vif->wilc;
 233	int ret = 0;
 234
 235	ret = wilc_wlan_start(wilc);
 236	if (ret)
 237		return ret;
 238
 239	if (!wait_for_completion_timeout(&wilc->sync_event,
 240					 msecs_to_jiffies(5000)))
 241		return -ETIME;
 242
 243	return 0;
 244}
 245
 246static int wilc_firmware_download(struct net_device *dev)
 247{
 248	struct wilc_vif *vif = netdev_priv(dev);
 249	struct wilc *wilc = vif->wilc;
 250	int ret = 0;
 251
 252	if (!wilc->firmware) {
 253		netdev_err(dev, "Firmware buffer is NULL\n");
 254		return -ENOBUFS;
 255	}
 256
 257	ret = wilc_wlan_firmware_download(wilc, wilc->firmware->data,
 258					  wilc->firmware->size);
 259	if (ret)
 260		return ret;
 261
 262	release_firmware(wilc->firmware);
 263	wilc->firmware = NULL;
 264
 265	netdev_dbg(dev, "Download Succeeded\n");
 266
 267	return 0;
 268}
 269
 270static int wilc_init_fw_config(struct net_device *dev, struct wilc_vif *vif)
 271{
 272	struct wilc_priv *priv = &vif->priv;
 273	struct host_if_drv *hif_drv;
 274	u8 b;
 275	u16 hw;
 276	u32 w;
 277
 278	netdev_dbg(dev, "Start configuring Firmware\n");
 279	hif_drv = (struct host_if_drv *)priv->hif_drv;
 280	netdev_dbg(dev, "Host = %p\n", hif_drv);
 281
 282	w = vif->iftype;
 283	cpu_to_le32s(&w);
 284	if (!wilc_wlan_cfg_set(vif, 1, WID_SET_OPERATION_MODE, (u8 *)&w, 4,
 285			       0, 0))
 286		goto fail;
 287
 288	b = WILC_FW_BSS_TYPE_INFRA;
 289	if (!wilc_wlan_cfg_set(vif, 0, WID_BSS_TYPE, &b, 1, 0, 0))
 290		goto fail;
 291
 292	b = WILC_FW_TX_RATE_AUTO;
 293	if (!wilc_wlan_cfg_set(vif, 0, WID_CURRENT_TX_RATE, &b, 1, 0, 0))
 294		goto fail;
 295
 296	b = WILC_FW_OPER_MODE_G_MIXED_11B_2;
 297	if (!wilc_wlan_cfg_set(vif, 0, WID_11G_OPERATING_MODE, &b, 1, 0, 0))
 298		goto fail;
 299
 300	b = WILC_FW_PREAMBLE_AUTO;
 301	if (!wilc_wlan_cfg_set(vif, 0, WID_PREAMBLE, &b, 1, 0, 0))
 302		goto fail;
 303
 304	b = WILC_FW_11N_PROT_AUTO;
 305	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_PROT_MECH, &b, 1, 0, 0))
 306		goto fail;
 307
 308	b = WILC_FW_ACTIVE_SCAN;
 309	if (!wilc_wlan_cfg_set(vif, 0, WID_SCAN_TYPE, &b, 1, 0, 0))
 310		goto fail;
 311
 312	b = WILC_FW_SITE_SURVEY_OFF;
 313	if (!wilc_wlan_cfg_set(vif, 0, WID_SITE_SURVEY, &b, 1, 0, 0))
 314		goto fail;
 315
 316	hw = 0xffff;
 317	cpu_to_le16s(&hw);
 318	if (!wilc_wlan_cfg_set(vif, 0, WID_RTS_THRESHOLD, (u8 *)&hw, 2, 0, 0))
 319		goto fail;
 320
 321	hw = 2346;
 322	cpu_to_le16s(&hw);
 323	if (!wilc_wlan_cfg_set(vif, 0, WID_FRAG_THRESHOLD, (u8 *)&hw, 2, 0, 0))
 324		goto fail;
 325
 326	b = 0;
 327	if (!wilc_wlan_cfg_set(vif, 0, WID_BCAST_SSID, &b, 1, 0, 0))
 328		goto fail;
 329
 330	b = 1;
 331	if (!wilc_wlan_cfg_set(vif, 0, WID_QOS_ENABLE, &b, 1, 0, 0))
 332		goto fail;
 333
 334	b = WILC_FW_NO_POWERSAVE;
 335	if (!wilc_wlan_cfg_set(vif, 0, WID_POWER_MANAGEMENT, &b, 1, 0, 0))
 336		goto fail;
 337
 338	b = WILC_FW_SEC_NO;
 339	if (!wilc_wlan_cfg_set(vif, 0, WID_11I_MODE, &b, 1, 0, 0))
 340		goto fail;
 341
 342	b = WILC_FW_AUTH_OPEN_SYSTEM;
 343	if (!wilc_wlan_cfg_set(vif, 0, WID_AUTH_TYPE, &b, 1, 0, 0))
 344		goto fail;
 345
 346	b = 3;
 347	if (!wilc_wlan_cfg_set(vif, 0, WID_LISTEN_INTERVAL, &b, 1, 0, 0))
 348		goto fail;
 349
 350	b = 3;
 351	if (!wilc_wlan_cfg_set(vif, 0, WID_DTIM_PERIOD, &b, 1, 0, 0))
 352		goto fail;
 353
 354	b = WILC_FW_ACK_POLICY_NORMAL;
 355	if (!wilc_wlan_cfg_set(vif, 0, WID_ACK_POLICY, &b, 1, 0, 0))
 356		goto fail;
 357
 358	b = 0;
 359	if (!wilc_wlan_cfg_set(vif, 0, WID_USER_CONTROL_ON_TX_POWER, &b, 1,
 360			       0, 0))
 361		goto fail;
 362
 363	b = 48;
 364	if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11A, &b, 1, 0, 0))
 365		goto fail;
 366
 367	b = 28;
 368	if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11B, &b, 1, 0, 0))
 369		goto fail;
 370
 371	hw = 100;
 372	cpu_to_le16s(&hw);
 373	if (!wilc_wlan_cfg_set(vif, 0, WID_BEACON_INTERVAL, (u8 *)&hw, 2, 0, 0))
 374		goto fail;
 375
 376	b = WILC_FW_REKEY_POLICY_DISABLE;
 377	if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_POLICY, &b, 1, 0, 0))
 378		goto fail;
 379
 380	w = 84600;
 381	cpu_to_le32s(&w);
 382	if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PERIOD, (u8 *)&w, 4, 0, 0))
 383		goto fail;
 384
 385	w = 500;
 386	cpu_to_le32s(&w);
 387	if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PACKET_COUNT, (u8 *)&w, 4, 0,
 388			       0))
 389		goto fail;
 390
 391	b = 1;
 392	if (!wilc_wlan_cfg_set(vif, 0, WID_SHORT_SLOT_ALLOWED, &b, 1, 0,
 393			       0))
 394		goto fail;
 395
 396	b = WILC_FW_ERP_PROT_SELF_CTS;
 397	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ERP_PROT_TYPE, &b, 1, 0, 0))
 398		goto fail;
 399
 400	b = 1;
 401	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ENABLE, &b, 1, 0, 0))
 402		goto fail;
 403
 404	b = WILC_FW_11N_OP_MODE_HT_MIXED;
 405	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OPERATING_MODE, &b, 1, 0, 0))
 406		goto fail;
 407
 408	b = 1;
 409	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_TXOP_PROT_DISABLE, &b, 1, 0, 0))
 410		goto fail;
 411
 412	b = WILC_FW_OBBS_NONHT_DETECT_PROTECT_REPORT;
 413	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OBSS_NONHT_DETECTION, &b, 1,
 414			       0, 0))
 415		goto fail;
 416
 417	b = WILC_FW_HT_PROT_RTS_CTS_NONHT;
 418	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_HT_PROT_TYPE, &b, 1, 0, 0))
 419		goto fail;
 420
 421	b = 0;
 422	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_RIFS_PROT_ENABLE, &b, 1, 0,
 423			       0))
 424		goto fail;
 425
 426	b = 7;
 427	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_CURRENT_TX_MCS, &b, 1, 0, 0))
 428		goto fail;
 429
 430	b = 1;
 431	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_IMMEDIATE_BA_ENABLED, &b, 1,
 432			       1, 0))
 433		goto fail;
 434
 435	return 0;
 436
 437fail:
 438	return -EINVAL;
 439}
 440
 441static void wlan_deinitialize_threads(struct net_device *dev)
 442{
 443	struct wilc_vif *vif = netdev_priv(dev);
 444	struct wilc *wl = vif->wilc;
 445
 446	wl->close = 1;
 447
 448	complete(&wl->txq_event);
 449
 450	if (wl->txq_thread) {
 451		kthread_stop(wl->txq_thread);
 452		wl->txq_thread = NULL;
 453	}
 454}
 455
 456static void wilc_wlan_deinitialize(struct net_device *dev)
 457{
 458	struct wilc_vif *vif = netdev_priv(dev);
 459	struct wilc *wl = vif->wilc;
 460
 461	if (!wl) {
 462		netdev_err(dev, "wl is NULL\n");
 463		return;
 464	}
 465
 466	if (wl->initialized) {
 467		netdev_info(dev, "Deinitializing wilc1000...\n");
 468
 469		if (!wl->dev_irq_num &&
 470		    wl->hif_func->disable_interrupt) {
 471			mutex_lock(&wl->hif_cs);
 472			wl->hif_func->disable_interrupt(wl);
 473			mutex_unlock(&wl->hif_cs);
 474		}
 475		complete(&wl->txq_event);
 476
 477		wlan_deinitialize_threads(dev);
 478		deinit_irq(dev);
 479
 480		wilc_wlan_stop(wl, vif);
 481		wilc_wlan_cleanup(dev);
 482
 483		wl->initialized = false;
 484
 485		netdev_dbg(dev, "wilc1000 deinitialization Done\n");
 486	} else {
 487		netdev_dbg(dev, "wilc1000 is not initialized\n");
 488	}
 489}
 490
 491static int wlan_initialize_threads(struct net_device *dev)
 492{
 493	struct wilc_vif *vif = netdev_priv(dev);
 494	struct wilc *wilc = vif->wilc;
 495
 496	wilc->txq_thread = kthread_run(wilc_txq_task, (void *)wilc,
 497				       "%s-tx", dev->name);
 498	if (IS_ERR(wilc->txq_thread)) {
 499		netdev_err(dev, "couldn't create TXQ thread\n");
 500		wilc->close = 1;
 501		return PTR_ERR(wilc->txq_thread);
 502	}
 503	wait_for_completion(&wilc->txq_thread_started);
 504
 505	return 0;
 506}
 507
 508static int wilc_wlan_initialize(struct net_device *dev, struct wilc_vif *vif)
 509{
 510	int ret = 0;
 511	struct wilc *wl = vif->wilc;
 512
 513	if (!wl->initialized) {
 514		wl->mac_status = WILC_MAC_STATUS_INIT;
 515		wl->close = 0;
 516
 517		ret = wilc_wlan_init(dev);
 518		if (ret)
 519			return ret;
 520
 521		ret = wlan_initialize_threads(dev);
 522		if (ret)
 523			goto fail_wilc_wlan;
 524
 525		if (wl->dev_irq_num && init_irq(dev)) {
 526			ret = -EIO;
 527			goto fail_threads;
 528		}
 529
 530		if (!wl->dev_irq_num &&
 531		    wl->hif_func->enable_interrupt &&
 532		    wl->hif_func->enable_interrupt(wl)) {
 533			ret = -EIO;
 534			goto fail_irq_init;
 535		}
 536
 537		ret = wilc_wlan_get_firmware(dev);
 538		if (ret)
 539			goto fail_irq_enable;
 540
 541		ret = wilc_firmware_download(dev);
 542		if (ret)
 543			goto fail_irq_enable;
 544
 545		ret = wilc_start_firmware(dev);
 546		if (ret)
 547			goto fail_irq_enable;
 548
 549		if (wilc_wlan_cfg_get(vif, 1, WID_FIRMWARE_VERSION, 1, 0)) {
 550			int size;
 551			char firmware_ver[WILC_MAX_FW_VERSION_STR_SIZE];
 552
 553			size = wilc_wlan_cfg_get_val(wl, WID_FIRMWARE_VERSION,
 554						     firmware_ver,
 555						     sizeof(firmware_ver));
 556			firmware_ver[size] = '\0';
 557			netdev_dbg(dev, "Firmware Ver = %s\n", firmware_ver);
 558		}
 559
 560		ret = wilc_init_fw_config(dev, vif);
 561		if (ret) {
 562			netdev_err(dev, "Failed to configure firmware\n");
 563			goto fail_fw_start;
 564		}
 565		wl->initialized = true;
 566		return 0;
 567
 568fail_fw_start:
 569		wilc_wlan_stop(wl, vif);
 570
 571fail_irq_enable:
 572		if (!wl->dev_irq_num &&
 573		    wl->hif_func->disable_interrupt)
 574			wl->hif_func->disable_interrupt(wl);
 575fail_irq_init:
 576		if (wl->dev_irq_num)
 577			deinit_irq(dev);
 578fail_threads:
 579		wlan_deinitialize_threads(dev);
 580fail_wilc_wlan:
 581		wilc_wlan_cleanup(dev);
 582		netdev_err(dev, "WLAN initialization FAILED\n");
 583	} else {
 584		netdev_dbg(dev, "wilc1000 already initialized\n");
 585	}
 586	return ret;
 587}
 588
 589static int mac_init_fn(struct net_device *ndev)
 590{
 591	netif_start_queue(ndev);
 592	netif_stop_queue(ndev);
 593
 594	return 0;
 595}
 596
 597static int wilc_mac_open(struct net_device *ndev)
 598{
 599	struct wilc_vif *vif = netdev_priv(ndev);
 600	struct wilc *wl = vif->wilc;
 601	int ret = 0;
 602	struct mgmt_frame_regs mgmt_regs = {};
 
 603
 604	if (!wl || !wl->dev) {
 605		netdev_err(ndev, "device not ready\n");
 606		return -ENODEV;
 607	}
 608
 609	netdev_dbg(ndev, "MAC OPEN[%p]\n", ndev);
 610
 611	ret = wilc_init_host_int(ndev);
 612	if (ret)
 613		return ret;
 614
 615	ret = wilc_wlan_initialize(ndev, vif);
 616	if (ret) {
 617		wilc_deinit_host_int(ndev);
 618		return ret;
 619	}
 620
 621	wilc_set_operation_mode(vif, wilc_get_vif_idx(vif), vif->iftype,
 622				vif->idx);
 623
 
 
 
 
 
 
 
 624	netdev_dbg(ndev, "Mac address: %pM\n", ndev->dev_addr);
 625	ret = wilc_set_mac_address(vif, ndev->dev_addr);
 626	if (ret) {
 627		netdev_err(ndev, "Failed to enforce MAC address in chip");
 628		wilc_deinit_host_int(ndev);
 629		if (!wl->open_ifcs)
 630			wilc_wlan_deinitialize(ndev);
 631		return ret;
 632	}
 633
 634	mgmt_regs.interface_stypes = vif->mgmt_reg_stypes;
 635	/* so we detect a change */
 636	vif->mgmt_reg_stypes = 0;
 637	wilc_update_mgmt_frame_registrations(vif->ndev->ieee80211_ptr->wiphy,
 638					     vif->ndev->ieee80211_ptr,
 639					     &mgmt_regs);
 640	netif_wake_queue(ndev);
 641	wl->open_ifcs++;
 642	vif->mac_opened = 1;
 643	return 0;
 644}
 645
 646static struct net_device_stats *mac_stats(struct net_device *dev)
 647{
 648	struct wilc_vif *vif = netdev_priv(dev);
 649
 650	return &vif->netstats;
 651}
 652
 653static int wilc_set_mac_addr(struct net_device *dev, void *p)
 654{
 655	int result;
 656	struct wilc_vif *vif = netdev_priv(dev);
 657	struct wilc *wilc = vif->wilc;
 658	struct sockaddr *addr = (struct sockaddr *)p;
 659	unsigned char mac_addr[ETH_ALEN];
 660	struct wilc_vif *tmp_vif;
 661	int srcu_idx;
 662
 663	if (!is_valid_ether_addr(addr->sa_data))
 664		return -EADDRNOTAVAIL;
 665
 666	if (!vif->mac_opened) {
 667		eth_commit_mac_addr_change(dev, p);
 668		return 0;
 669	}
 670
 671	/* Verify MAC Address is not already in use: */
 672
 673	srcu_idx = srcu_read_lock(&wilc->srcu);
 674	wilc_for_each_vif(wilc, tmp_vif) {
 675		wilc_get_mac_address(tmp_vif, mac_addr);
 676		if (ether_addr_equal(addr->sa_data, mac_addr)) {
 677			if (vif != tmp_vif) {
 678				srcu_read_unlock(&wilc->srcu, srcu_idx);
 679				return -EADDRNOTAVAIL;
 680			}
 681			srcu_read_unlock(&wilc->srcu, srcu_idx);
 682			return 0;
 683		}
 684	}
 685	srcu_read_unlock(&wilc->srcu, srcu_idx);
 686
 687	result = wilc_set_mac_address(vif, addr->sa_data);
 688	if (result)
 689		return result;
 690
 691	eth_commit_mac_addr_change(dev, p);
 692	return result;
 693}
 694
 695static void wilc_set_multicast_list(struct net_device *dev)
 696{
 697	struct netdev_hw_addr *ha;
 698	struct wilc_vif *vif = netdev_priv(dev);
 699	int i;
 700	u8 *mc_list;
 701	u8 *cur_mc;
 702
 703	if (dev->flags & IFF_PROMISC)
 704		return;
 705
 706	if (dev->flags & IFF_ALLMULTI ||
 707	    dev->mc.count > WILC_MULTICAST_TABLE_SIZE) {
 708		wilc_setup_multicast_filter(vif, 0, 0, NULL);
 709		return;
 710	}
 711
 712	if (dev->mc.count == 0) {
 713		wilc_setup_multicast_filter(vif, 1, 0, NULL);
 714		return;
 715	}
 716
 717	mc_list = kmalloc_array(dev->mc.count, ETH_ALEN, GFP_ATOMIC);
 718	if (!mc_list)
 719		return;
 720
 721	cur_mc = mc_list;
 722	i = 0;
 723	netdev_for_each_mc_addr(ha, dev) {
 724		memcpy(cur_mc, ha->addr, ETH_ALEN);
 725		netdev_dbg(dev, "Entry[%d]: %pM\n", i, cur_mc);
 726		i++;
 727		cur_mc += ETH_ALEN;
 728	}
 729
 730	if (wilc_setup_multicast_filter(vif, 1, dev->mc.count, mc_list))
 731		kfree(mc_list);
 732}
 733
 734static void wilc_tx_complete(void *priv, int status)
 735{
 736	struct tx_complete_data *pv_data = priv;
 737
 738	dev_kfree_skb(pv_data->skb);
 739	kfree(pv_data);
 740}
 741
 742netdev_tx_t wilc_mac_xmit(struct sk_buff *skb, struct net_device *ndev)
 743{
 744	struct wilc_vif *vif = netdev_priv(ndev);
 745	struct wilc *wilc = vif->wilc;
 746	struct tx_complete_data *tx_data = NULL;
 747	int queue_count;
 748
 749	if (skb->dev != ndev) {
 750		netdev_err(ndev, "Packet not destined to this device\n");
 751		dev_kfree_skb(skb);
 752		return NETDEV_TX_OK;
 753	}
 754
 755	tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
 756	if (!tx_data) {
 757		dev_kfree_skb(skb);
 758		netif_wake_queue(ndev);
 759		return NETDEV_TX_OK;
 760	}
 761
 762	tx_data->buff = skb->data;
 763	tx_data->size = skb->len;
 764	tx_data->skb  = skb;
 765
 766	vif->netstats.tx_packets++;
 767	vif->netstats.tx_bytes += tx_data->size;
 768	queue_count = wilc_wlan_txq_add_net_pkt(ndev, tx_data,
 769						tx_data->buff, tx_data->size,
 770						wilc_tx_complete);
 771
 772	if (queue_count > FLOW_CONTROL_UPPER_THRESHOLD) {
 773		int srcu_idx;
 774		struct wilc_vif *vif;
 775
 776		srcu_idx = srcu_read_lock(&wilc->srcu);
 777		wilc_for_each_vif(wilc, vif) {
 778			if (vif->mac_opened)
 779				netif_stop_queue(vif->ndev);
 780		}
 781		srcu_read_unlock(&wilc->srcu, srcu_idx);
 782	}
 783
 784	return NETDEV_TX_OK;
 785}
 786
 787static int wilc_mac_close(struct net_device *ndev)
 788{
 789	struct wilc_vif *vif = netdev_priv(ndev);
 790	struct wilc *wl = vif->wilc;
 791
 792	netdev_dbg(ndev, "Mac close\n");
 793
 794	if (wl->open_ifcs > 0)
 795		wl->open_ifcs--;
 796	else
 797		return 0;
 798
 799	if (vif->ndev) {
 800		netif_stop_queue(vif->ndev);
 801
 802		wilc_handle_disconnect(vif);
 803		wilc_deinit_host_int(vif->ndev);
 804	}
 805
 806	if (wl->open_ifcs == 0) {
 807		netdev_dbg(ndev, "Deinitializing wilc1000\n");
 808		wl->close = 1;
 809		wilc_wlan_deinitialize(ndev);
 810	}
 811
 812	vif->mac_opened = 0;
 813
 814	return 0;
 815}
 816
 817void wilc_frmw_to_host(struct wilc *wilc, u8 *buff, u32 size,
 818		       u32 pkt_offset)
 819{
 820	unsigned char *buff_to_send = NULL;
 821	struct net_device *wilc_netdev;
 822	unsigned int frame_len = 0;
 823	struct wilc_vif *vif;
 824	struct sk_buff *skb;
 825	int srcu_idx;
 826	int stats;
 827
 828	if (!wilc)
 829		return;
 830
 831	srcu_idx = srcu_read_lock(&wilc->srcu);
 832	wilc_netdev = get_if_handler(wilc, buff);
 833	if (!wilc_netdev)
 834		goto out;
 835
 836	buff += pkt_offset;
 837	vif = netdev_priv(wilc_netdev);
 838
 839	if (size > 0) {
 840		frame_len = size;
 841		buff_to_send = buff;
 842
 843		skb = dev_alloc_skb(frame_len);
 844		if (!skb)
 845			goto out;
 846
 847		skb->dev = wilc_netdev;
 848
 849		skb_put_data(skb, buff_to_send, frame_len);
 850
 851		skb->protocol = eth_type_trans(skb, wilc_netdev);
 852		vif->netstats.rx_packets++;
 853		vif->netstats.rx_bytes += frame_len;
 854		skb->ip_summed = CHECKSUM_UNNECESSARY;
 855		stats = netif_rx(skb);
 856		netdev_dbg(wilc_netdev, "netif_rx ret value is: %d\n", stats);
 857	}
 858out:
 859	srcu_read_unlock(&wilc->srcu, srcu_idx);
 860}
 861
 862void wilc_wfi_mgmt_rx(struct wilc *wilc, u8 *buff, u32 size, bool is_auth)
 863{
 864	int srcu_idx;
 865	struct wilc_vif *vif;
 866
 867	srcu_idx = srcu_read_lock(&wilc->srcu);
 868	wilc_for_each_vif(wilc, vif) {
 869		struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buff;
 870		u16 type = le16_to_cpup((__le16 *)buff);
 871		u32 type_bit = BIT(type >> 4);
 872		u32 auth_bit = BIT(IEEE80211_STYPE_AUTH >> 4);
 873
 874		if ((vif->mgmt_reg_stypes & auth_bit &&
 875		     ieee80211_is_auth(mgmt->frame_control)) &&
 876		    vif->iftype == WILC_STATION_MODE && is_auth) {
 877			wilc_wfi_mgmt_frame_rx(vif, buff, size);
 878			break;
 879		}
 880
 881		if (vif->priv.p2p_listen_state &&
 882		    vif->mgmt_reg_stypes & type_bit)
 883			wilc_wfi_p2p_rx(vif, buff, size);
 884
 885		if (vif->monitor_flag)
 886			wilc_wfi_monitor_rx(wilc->monitor_dev, buff, size);
 887	}
 888	srcu_read_unlock(&wilc->srcu, srcu_idx);
 889}
 890
 891static const struct net_device_ops wilc_netdev_ops = {
 892	.ndo_init = mac_init_fn,
 893	.ndo_open = wilc_mac_open,
 894	.ndo_stop = wilc_mac_close,
 895	.ndo_set_mac_address = wilc_set_mac_addr,
 896	.ndo_start_xmit = wilc_mac_xmit,
 897	.ndo_get_stats = mac_stats,
 898	.ndo_set_rx_mode  = wilc_set_multicast_list,
 899};
 900
 901void wilc_netdev_cleanup(struct wilc *wilc)
 902{
 903	struct wilc_vif *vif, *vif_tmp;
 904
 905	if (!wilc)
 906		return;
 907
 908	if (wilc->firmware) {
 909		release_firmware(wilc->firmware);
 910		wilc->firmware = NULL;
 911	}
 912
 913	list_for_each_entry_safe(vif, vif_tmp, &wilc->vif_list, list) {
 914		mutex_lock(&wilc->vif_mutex);
 915		list_del_rcu(&vif->list);
 916		wilc->vif_num--;
 917		mutex_unlock(&wilc->vif_mutex);
 918		synchronize_srcu(&wilc->srcu);
 919		if (vif->ndev)
 920			unregister_netdev(vif->ndev);
 921	}
 922
 923	wilc_wfi_deinit_mon_interface(wilc, false);
 924	destroy_workqueue(wilc->hif_workqueue);
 925
 926	wilc_wlan_cfg_deinit(wilc);
 927	wlan_deinit_locks(wilc);
 
 
 928}
 929EXPORT_SYMBOL_GPL(wilc_netdev_cleanup);
 930
 931static u8 wilc_get_available_idx(struct wilc *wl)
 932{
 933	int idx = 0;
 934	struct wilc_vif *vif;
 935	int srcu_idx;
 936
 937	srcu_idx = srcu_read_lock(&wl->srcu);
 938	wilc_for_each_vif(wl, vif) {
 939		if (vif->idx == 0)
 940			idx = 1;
 941		else
 942			idx = 0;
 943	}
 944	srcu_read_unlock(&wl->srcu, srcu_idx);
 945	return idx;
 946}
 947
 948struct wilc_vif *wilc_netdev_ifc_init(struct wilc *wl, const char *name,
 949				      int vif_type, enum nl80211_iftype type,
 950				      bool rtnl_locked)
 951{
 952	u8 mac_address[ETH_ALEN];
 953	struct net_device *ndev;
 954	struct wilc_vif *vif;
 955	int ret;
 956
 957	ndev = alloc_etherdev(sizeof(*vif));
 958	if (!ndev)
 959		return ERR_PTR(-ENOMEM);
 960
 961	vif = netdev_priv(ndev);
 962	ndev->ieee80211_ptr = &vif->priv.wdev;
 963	strcpy(ndev->name, name);
 964	vif->wilc = wl;
 965	vif->ndev = ndev;
 966	ndev->ml_priv = vif;
 967
 968	ndev->netdev_ops = &wilc_netdev_ops;
 969
 970	SET_NETDEV_DEV(ndev, wiphy_dev(wl->wiphy));
 971
 972	vif->priv.wdev.wiphy = wl->wiphy;
 973	vif->priv.wdev.netdev = ndev;
 974	vif->priv.wdev.iftype = type;
 975	vif->priv.dev = ndev;
 976
 977	ndev->needs_free_netdev = true;
 978	vif->iftype = vif_type;
 979	vif->idx = wilc_get_available_idx(wl);
 980	vif->mac_opened = 0;
 981
 982	memcpy(mac_address, wl->nv_mac_address, ETH_ALEN);
 983	/* WILC firmware uses locally administered MAC address for the
 984	 * second virtual interface (bit 1 of first byte set), but
 985	 * since it is possibly not loaded/running yet, reproduce this behavior
 986	 * in the driver during interface creation.
 987	 */
 988	if (vif->idx)
 989		mac_address[0] |= 0x2;
 990
 991	eth_hw_addr_set(vif->ndev, mac_address);
 992
 993	mutex_lock(&wl->vif_mutex);
 994	list_add_tail_rcu(&vif->list, &wl->vif_list);
 995	wl->vif_num += 1;
 996	mutex_unlock(&wl->vif_mutex);
 997	synchronize_srcu(&wl->srcu);
 998
 999	if (rtnl_locked)
1000		ret = cfg80211_register_netdevice(ndev);
1001	else
1002		ret = register_netdev(ndev);
1003
1004	if (ret) {
1005		ret = -EFAULT;
1006		goto error_remove_vif;
1007	}
1008
1009	return vif;
1010
1011error_remove_vif:
 
1012	mutex_lock(&wl->vif_mutex);
1013	list_del_rcu(&vif->list);
1014	wl->vif_num -= 1;
1015	mutex_unlock(&wl->vif_mutex);
1016	synchronize_srcu(&wl->srcu);
 
 
 
 
 
 
 
 
1017	free_netdev(ndev);
1018	return ERR_PTR(ret);
1019}
1020EXPORT_SYMBOL_GPL(wilc_netdev_ifc_init);
1021
1022MODULE_DESCRIPTION("Atmel WILC1000 core wireless driver");
1023MODULE_LICENSE("GPL");
1024MODULE_FIRMWARE(WILC1000_FW(WILC1000_API_VER));
1025MODULE_FIRMWARE(WILC3000_FW(WILC3000_API_VER));
v6.9.4
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
   4 * All rights reserved.
   5 */
   6
   7#include <linux/irq.h>
   8#include <linux/kthread.h>
   9#include <linux/firmware.h>
  10#include <linux/netdevice.h>
  11#include <linux/inetdevice.h>
  12
  13#include "cfg80211.h"
  14#include "wlan_cfg.h"
  15
  16#define WILC_MULTICAST_TABLE_SIZE	8
  17#define WILC_MAX_FW_VERSION_STR_SIZE	50
  18
  19/* latest API version supported */
  20#define WILC1000_API_VER		1
  21
  22#define WILC1000_FW_PREFIX		"atmel/wilc1000_wifi_firmware-"
  23#define __WILC1000_FW(api)		WILC1000_FW_PREFIX #api ".bin"
  24#define WILC1000_FW(api)		__WILC1000_FW(api)
  25
 
 
 
 
 
 
  26static irqreturn_t isr_uh_routine(int irq, void *user_data)
  27{
  28	struct wilc *wilc = user_data;
  29
  30	if (wilc->close) {
  31		pr_err("Can't handle UH interrupt\n");
  32		return IRQ_HANDLED;
  33	}
  34	return IRQ_WAKE_THREAD;
  35}
  36
  37static irqreturn_t isr_bh_routine(int irq, void *userdata)
  38{
  39	struct wilc *wilc = userdata;
  40
  41	if (wilc->close) {
  42		pr_err("Can't handle BH interrupt\n");
  43		return IRQ_HANDLED;
  44	}
  45
  46	wilc_handle_isr(wilc);
  47
  48	return IRQ_HANDLED;
  49}
  50
  51static int init_irq(struct net_device *dev)
  52{
  53	struct wilc_vif *vif = netdev_priv(dev);
  54	struct wilc *wl = vif->wilc;
  55	int ret;
  56
  57	ret = request_threaded_irq(wl->dev_irq_num, isr_uh_routine,
  58				   isr_bh_routine,
  59				   IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  60				   dev->name, wl);
  61	if (ret) {
  62		netdev_err(dev, "Failed to request IRQ [%d]\n", ret);
  63		return ret;
  64	}
  65	netdev_dbg(dev, "IRQ request succeeded IRQ-NUM= %d\n", wl->dev_irq_num);
  66
  67	return 0;
  68}
  69
  70static void deinit_irq(struct net_device *dev)
  71{
  72	struct wilc_vif *vif = netdev_priv(dev);
  73	struct wilc *wilc = vif->wilc;
  74
  75	/* Deinitialize IRQ */
  76	if (wilc->dev_irq_num)
  77		free_irq(wilc->dev_irq_num, wilc);
  78}
  79
  80void wilc_mac_indicate(struct wilc *wilc)
  81{
  82	s8 status;
  83
  84	wilc_wlan_cfg_get_val(wilc, WID_STATUS, &status, 1);
  85	if (wilc->mac_status == WILC_MAC_STATUS_INIT) {
  86		wilc->mac_status = status;
  87		complete(&wilc->sync_event);
  88	} else {
  89		wilc->mac_status = status;
  90	}
  91}
  92
  93static struct net_device *get_if_handler(struct wilc *wilc, u8 *mac_header)
  94{
  95	struct net_device *ndev = NULL;
  96	struct wilc_vif *vif;
  97	struct ieee80211_hdr *h = (struct ieee80211_hdr *)mac_header;
  98
  99	wilc_for_each_vif(wilc, vif) {
 100		if (vif->iftype == WILC_STATION_MODE)
 101			if (ether_addr_equal_unaligned(h->addr2, vif->bssid)) {
 102				ndev = vif->ndev;
 103				goto out;
 104			}
 105		if (vif->iftype == WILC_AP_MODE)
 106			if (ether_addr_equal_unaligned(h->addr1, vif->bssid)) {
 107				ndev = vif->ndev;
 108				goto out;
 109			}
 110	}
 111out:
 112	return ndev;
 113}
 114
 115void wilc_wlan_set_bssid(struct net_device *wilc_netdev, const u8 *bssid,
 116			 u8 mode)
 117{
 118	struct wilc_vif *vif = netdev_priv(wilc_netdev);
 119
 120	if (bssid)
 121		ether_addr_copy(vif->bssid, bssid);
 122	else
 123		eth_zero_addr(vif->bssid);
 124
 125	vif->iftype = mode;
 126}
 127
 128int wilc_wlan_get_num_conn_ifcs(struct wilc *wilc)
 129{
 130	int srcu_idx;
 131	u8 ret_val = 0;
 132	struct wilc_vif *vif;
 133
 134	srcu_idx = srcu_read_lock(&wilc->srcu);
 135	wilc_for_each_vif(wilc, vif) {
 136		if (!is_zero_ether_addr(vif->bssid))
 137			ret_val++;
 138	}
 139	srcu_read_unlock(&wilc->srcu, srcu_idx);
 140	return ret_val;
 141}
 142
 143static void wilc_wake_tx_queues(struct wilc *wl)
 144{
 145	int srcu_idx;
 146	struct wilc_vif *ifc;
 147
 148	srcu_idx = srcu_read_lock(&wl->srcu);
 149	wilc_for_each_vif(wl, ifc) {
 150		if (ifc->mac_opened && netif_queue_stopped(ifc->ndev))
 151			netif_wake_queue(ifc->ndev);
 152	}
 153	srcu_read_unlock(&wl->srcu, srcu_idx);
 154}
 155
 156static int wilc_txq_task(void *vp)
 157{
 158	int ret;
 159	u32 txq_count;
 160	struct wilc *wl = vp;
 161
 162	complete(&wl->txq_thread_started);
 163	while (1) {
 164		if (wait_for_completion_interruptible(&wl->txq_event))
 165			continue;
 166		if (wl->close) {
 167			complete(&wl->txq_thread_started);
 168
 169			while (!kthread_should_stop())
 170				schedule();
 171			break;
 172		}
 173		do {
 174			ret = wilc_wlan_handle_txq(wl, &txq_count);
 175			if (txq_count < FLOW_CONTROL_LOWER_THRESHOLD) {
 176				wilc_wake_tx_queues(wl);
 177			}
 178			if (ret != WILC_VMM_ENTRY_FULL_RETRY)
 179				break;
 180			/* Back off TX task from sending packets for some time.
 181			 * msleep_interruptible will allow RX task to run and
 182			 * free buffers. TX task will be in TASK_INTERRUPTIBLE
 183			 * state which will put the thread back to CPU running
 184			 * queue when it's signaled even if the timeout isn't
 185			 * elapsed. This gives faster chance for reserved SK
 186			 * buffers to be free.
 187			 */
 188			msleep_interruptible(TX_BACKOFF_WEIGHT_MS);
 189		} while (!wl->close);
 190	}
 191	return 0;
 192}
 193
 194static int wilc_wlan_get_firmware(struct net_device *dev)
 195{
 196	struct wilc_vif *vif = netdev_priv(dev);
 197	struct wilc *wilc = vif->wilc;
 198	int chip_id;
 199	const struct firmware *wilc_fw;
 
 200	int ret;
 201
 202	chip_id = wilc_get_chipid(wilc, false);
 
 
 
 
 
 203
 204	netdev_info(dev, "ChipID [%x] loading firmware [%s]\n", chip_id,
 205		    WILC1000_FW(WILC1000_API_VER));
 
 206
 207	ret = request_firmware(&wilc_fw, WILC1000_FW(WILC1000_API_VER),
 208			       wilc->dev);
 209	if (ret != 0) {
 210		netdev_err(dev, "%s - firmware not available\n",
 211			   WILC1000_FW(WILC1000_API_VER));
 212		return -EINVAL;
 213	}
 214	wilc->firmware = wilc_fw;
 215
 216	return 0;
 217}
 218
 219static int wilc_start_firmware(struct net_device *dev)
 220{
 221	struct wilc_vif *vif = netdev_priv(dev);
 222	struct wilc *wilc = vif->wilc;
 223	int ret = 0;
 224
 225	ret = wilc_wlan_start(wilc);
 226	if (ret)
 227		return ret;
 228
 229	if (!wait_for_completion_timeout(&wilc->sync_event,
 230					 msecs_to_jiffies(5000)))
 231		return -ETIME;
 232
 233	return 0;
 234}
 235
 236static int wilc1000_firmware_download(struct net_device *dev)
 237{
 238	struct wilc_vif *vif = netdev_priv(dev);
 239	struct wilc *wilc = vif->wilc;
 240	int ret = 0;
 241
 242	if (!wilc->firmware) {
 243		netdev_err(dev, "Firmware buffer is NULL\n");
 244		return -ENOBUFS;
 245	}
 246
 247	ret = wilc_wlan_firmware_download(wilc, wilc->firmware->data,
 248					  wilc->firmware->size);
 249	if (ret)
 250		return ret;
 251
 252	release_firmware(wilc->firmware);
 253	wilc->firmware = NULL;
 254
 255	netdev_dbg(dev, "Download Succeeded\n");
 256
 257	return 0;
 258}
 259
 260static int wilc_init_fw_config(struct net_device *dev, struct wilc_vif *vif)
 261{
 262	struct wilc_priv *priv = &vif->priv;
 263	struct host_if_drv *hif_drv;
 264	u8 b;
 265	u16 hw;
 266	u32 w;
 267
 268	netdev_dbg(dev, "Start configuring Firmware\n");
 269	hif_drv = (struct host_if_drv *)priv->hif_drv;
 270	netdev_dbg(dev, "Host = %p\n", hif_drv);
 271
 272	w = vif->iftype;
 273	cpu_to_le32s(&w);
 274	if (!wilc_wlan_cfg_set(vif, 1, WID_SET_OPERATION_MODE, (u8 *)&w, 4,
 275			       0, 0))
 276		goto fail;
 277
 278	b = WILC_FW_BSS_TYPE_INFRA;
 279	if (!wilc_wlan_cfg_set(vif, 0, WID_BSS_TYPE, &b, 1, 0, 0))
 280		goto fail;
 281
 282	b = WILC_FW_TX_RATE_AUTO;
 283	if (!wilc_wlan_cfg_set(vif, 0, WID_CURRENT_TX_RATE, &b, 1, 0, 0))
 284		goto fail;
 285
 286	b = WILC_FW_OPER_MODE_G_MIXED_11B_2;
 287	if (!wilc_wlan_cfg_set(vif, 0, WID_11G_OPERATING_MODE, &b, 1, 0, 0))
 288		goto fail;
 289
 290	b = WILC_FW_PREAMBLE_AUTO;
 291	if (!wilc_wlan_cfg_set(vif, 0, WID_PREAMBLE, &b, 1, 0, 0))
 292		goto fail;
 293
 294	b = WILC_FW_11N_PROT_AUTO;
 295	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_PROT_MECH, &b, 1, 0, 0))
 296		goto fail;
 297
 298	b = WILC_FW_ACTIVE_SCAN;
 299	if (!wilc_wlan_cfg_set(vif, 0, WID_SCAN_TYPE, &b, 1, 0, 0))
 300		goto fail;
 301
 302	b = WILC_FW_SITE_SURVEY_OFF;
 303	if (!wilc_wlan_cfg_set(vif, 0, WID_SITE_SURVEY, &b, 1, 0, 0))
 304		goto fail;
 305
 306	hw = 0xffff;
 307	cpu_to_le16s(&hw);
 308	if (!wilc_wlan_cfg_set(vif, 0, WID_RTS_THRESHOLD, (u8 *)&hw, 2, 0, 0))
 309		goto fail;
 310
 311	hw = 2346;
 312	cpu_to_le16s(&hw);
 313	if (!wilc_wlan_cfg_set(vif, 0, WID_FRAG_THRESHOLD, (u8 *)&hw, 2, 0, 0))
 314		goto fail;
 315
 316	b = 0;
 317	if (!wilc_wlan_cfg_set(vif, 0, WID_BCAST_SSID, &b, 1, 0, 0))
 318		goto fail;
 319
 320	b = 1;
 321	if (!wilc_wlan_cfg_set(vif, 0, WID_QOS_ENABLE, &b, 1, 0, 0))
 322		goto fail;
 323
 324	b = WILC_FW_NO_POWERSAVE;
 325	if (!wilc_wlan_cfg_set(vif, 0, WID_POWER_MANAGEMENT, &b, 1, 0, 0))
 326		goto fail;
 327
 328	b = WILC_FW_SEC_NO;
 329	if (!wilc_wlan_cfg_set(vif, 0, WID_11I_MODE, &b, 1, 0, 0))
 330		goto fail;
 331
 332	b = WILC_FW_AUTH_OPEN_SYSTEM;
 333	if (!wilc_wlan_cfg_set(vif, 0, WID_AUTH_TYPE, &b, 1, 0, 0))
 334		goto fail;
 335
 336	b = 3;
 337	if (!wilc_wlan_cfg_set(vif, 0, WID_LISTEN_INTERVAL, &b, 1, 0, 0))
 338		goto fail;
 339
 340	b = 3;
 341	if (!wilc_wlan_cfg_set(vif, 0, WID_DTIM_PERIOD, &b, 1, 0, 0))
 342		goto fail;
 343
 344	b = WILC_FW_ACK_POLICY_NORMAL;
 345	if (!wilc_wlan_cfg_set(vif, 0, WID_ACK_POLICY, &b, 1, 0, 0))
 346		goto fail;
 347
 348	b = 0;
 349	if (!wilc_wlan_cfg_set(vif, 0, WID_USER_CONTROL_ON_TX_POWER, &b, 1,
 350			       0, 0))
 351		goto fail;
 352
 353	b = 48;
 354	if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11A, &b, 1, 0, 0))
 355		goto fail;
 356
 357	b = 28;
 358	if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11B, &b, 1, 0, 0))
 359		goto fail;
 360
 361	hw = 100;
 362	cpu_to_le16s(&hw);
 363	if (!wilc_wlan_cfg_set(vif, 0, WID_BEACON_INTERVAL, (u8 *)&hw, 2, 0, 0))
 364		goto fail;
 365
 366	b = WILC_FW_REKEY_POLICY_DISABLE;
 367	if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_POLICY, &b, 1, 0, 0))
 368		goto fail;
 369
 370	w = 84600;
 371	cpu_to_le32s(&w);
 372	if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PERIOD, (u8 *)&w, 4, 0, 0))
 373		goto fail;
 374
 375	w = 500;
 376	cpu_to_le32s(&w);
 377	if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PACKET_COUNT, (u8 *)&w, 4, 0,
 378			       0))
 379		goto fail;
 380
 381	b = 1;
 382	if (!wilc_wlan_cfg_set(vif, 0, WID_SHORT_SLOT_ALLOWED, &b, 1, 0,
 383			       0))
 384		goto fail;
 385
 386	b = WILC_FW_ERP_PROT_SELF_CTS;
 387	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ERP_PROT_TYPE, &b, 1, 0, 0))
 388		goto fail;
 389
 390	b = 1;
 391	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ENABLE, &b, 1, 0, 0))
 392		goto fail;
 393
 394	b = WILC_FW_11N_OP_MODE_HT_MIXED;
 395	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OPERATING_MODE, &b, 1, 0, 0))
 396		goto fail;
 397
 398	b = 1;
 399	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_TXOP_PROT_DISABLE, &b, 1, 0, 0))
 400		goto fail;
 401
 402	b = WILC_FW_OBBS_NONHT_DETECT_PROTECT_REPORT;
 403	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OBSS_NONHT_DETECTION, &b, 1,
 404			       0, 0))
 405		goto fail;
 406
 407	b = WILC_FW_HT_PROT_RTS_CTS_NONHT;
 408	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_HT_PROT_TYPE, &b, 1, 0, 0))
 409		goto fail;
 410
 411	b = 0;
 412	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_RIFS_PROT_ENABLE, &b, 1, 0,
 413			       0))
 414		goto fail;
 415
 416	b = 7;
 417	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_CURRENT_TX_MCS, &b, 1, 0, 0))
 418		goto fail;
 419
 420	b = 1;
 421	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_IMMEDIATE_BA_ENABLED, &b, 1,
 422			       1, 0))
 423		goto fail;
 424
 425	return 0;
 426
 427fail:
 428	return -EINVAL;
 429}
 430
 431static void wlan_deinitialize_threads(struct net_device *dev)
 432{
 433	struct wilc_vif *vif = netdev_priv(dev);
 434	struct wilc *wl = vif->wilc;
 435
 436	wl->close = 1;
 437
 438	complete(&wl->txq_event);
 439
 440	if (wl->txq_thread) {
 441		kthread_stop(wl->txq_thread);
 442		wl->txq_thread = NULL;
 443	}
 444}
 445
 446static void wilc_wlan_deinitialize(struct net_device *dev)
 447{
 448	struct wilc_vif *vif = netdev_priv(dev);
 449	struct wilc *wl = vif->wilc;
 450
 451	if (!wl) {
 452		netdev_err(dev, "wl is NULL\n");
 453		return;
 454	}
 455
 456	if (wl->initialized) {
 457		netdev_info(dev, "Deinitializing wilc1000...\n");
 458
 459		if (!wl->dev_irq_num &&
 460		    wl->hif_func->disable_interrupt) {
 461			mutex_lock(&wl->hif_cs);
 462			wl->hif_func->disable_interrupt(wl);
 463			mutex_unlock(&wl->hif_cs);
 464		}
 465		complete(&wl->txq_event);
 466
 467		wlan_deinitialize_threads(dev);
 468		deinit_irq(dev);
 469
 470		wilc_wlan_stop(wl, vif);
 471		wilc_wlan_cleanup(dev);
 472
 473		wl->initialized = false;
 474
 475		netdev_dbg(dev, "wilc1000 deinitialization Done\n");
 476	} else {
 477		netdev_dbg(dev, "wilc1000 is not initialized\n");
 478	}
 479}
 480
 481static int wlan_initialize_threads(struct net_device *dev)
 482{
 483	struct wilc_vif *vif = netdev_priv(dev);
 484	struct wilc *wilc = vif->wilc;
 485
 486	wilc->txq_thread = kthread_run(wilc_txq_task, (void *)wilc,
 487				       "%s-tx", dev->name);
 488	if (IS_ERR(wilc->txq_thread)) {
 489		netdev_err(dev, "couldn't create TXQ thread\n");
 490		wilc->close = 1;
 491		return PTR_ERR(wilc->txq_thread);
 492	}
 493	wait_for_completion(&wilc->txq_thread_started);
 494
 495	return 0;
 496}
 497
 498static int wilc_wlan_initialize(struct net_device *dev, struct wilc_vif *vif)
 499{
 500	int ret = 0;
 501	struct wilc *wl = vif->wilc;
 502
 503	if (!wl->initialized) {
 504		wl->mac_status = WILC_MAC_STATUS_INIT;
 505		wl->close = 0;
 506
 507		ret = wilc_wlan_init(dev);
 508		if (ret)
 509			return ret;
 510
 511		ret = wlan_initialize_threads(dev);
 512		if (ret)
 513			goto fail_wilc_wlan;
 514
 515		if (wl->dev_irq_num && init_irq(dev)) {
 516			ret = -EIO;
 517			goto fail_threads;
 518		}
 519
 520		if (!wl->dev_irq_num &&
 521		    wl->hif_func->enable_interrupt &&
 522		    wl->hif_func->enable_interrupt(wl)) {
 523			ret = -EIO;
 524			goto fail_irq_init;
 525		}
 526
 527		ret = wilc_wlan_get_firmware(dev);
 528		if (ret)
 529			goto fail_irq_enable;
 530
 531		ret = wilc1000_firmware_download(dev);
 532		if (ret)
 533			goto fail_irq_enable;
 534
 535		ret = wilc_start_firmware(dev);
 536		if (ret)
 537			goto fail_irq_enable;
 538
 539		if (wilc_wlan_cfg_get(vif, 1, WID_FIRMWARE_VERSION, 1, 0)) {
 540			int size;
 541			char firmware_ver[WILC_MAX_FW_VERSION_STR_SIZE];
 542
 543			size = wilc_wlan_cfg_get_val(wl, WID_FIRMWARE_VERSION,
 544						     firmware_ver,
 545						     sizeof(firmware_ver));
 546			firmware_ver[size] = '\0';
 547			netdev_dbg(dev, "Firmware Ver = %s\n", firmware_ver);
 548		}
 549
 550		ret = wilc_init_fw_config(dev, vif);
 551		if (ret) {
 552			netdev_err(dev, "Failed to configure firmware\n");
 553			goto fail_fw_start;
 554		}
 555		wl->initialized = true;
 556		return 0;
 557
 558fail_fw_start:
 559		wilc_wlan_stop(wl, vif);
 560
 561fail_irq_enable:
 562		if (!wl->dev_irq_num &&
 563		    wl->hif_func->disable_interrupt)
 564			wl->hif_func->disable_interrupt(wl);
 565fail_irq_init:
 566		if (wl->dev_irq_num)
 567			deinit_irq(dev);
 568fail_threads:
 569		wlan_deinitialize_threads(dev);
 570fail_wilc_wlan:
 571		wilc_wlan_cleanup(dev);
 572		netdev_err(dev, "WLAN initialization FAILED\n");
 573	} else {
 574		netdev_dbg(dev, "wilc1000 already initialized\n");
 575	}
 576	return ret;
 577}
 578
 579static int mac_init_fn(struct net_device *ndev)
 580{
 581	netif_start_queue(ndev);
 582	netif_stop_queue(ndev);
 583
 584	return 0;
 585}
 586
 587static int wilc_mac_open(struct net_device *ndev)
 588{
 589	struct wilc_vif *vif = netdev_priv(ndev);
 590	struct wilc *wl = vif->wilc;
 591	int ret = 0;
 592	struct mgmt_frame_regs mgmt_regs = {};
 593	u8 addr[ETH_ALEN] __aligned(2);
 594
 595	if (!wl || !wl->dev) {
 596		netdev_err(ndev, "device not ready\n");
 597		return -ENODEV;
 598	}
 599
 600	netdev_dbg(ndev, "MAC OPEN[%p]\n", ndev);
 601
 602	ret = wilc_init_host_int(ndev);
 603	if (ret)
 604		return ret;
 605
 606	ret = wilc_wlan_initialize(ndev, vif);
 607	if (ret) {
 608		wilc_deinit_host_int(ndev);
 609		return ret;
 610	}
 611
 612	wilc_set_operation_mode(vif, wilc_get_vif_idx(vif), vif->iftype,
 613				vif->idx);
 614
 615	if (is_valid_ether_addr(ndev->dev_addr)) {
 616		ether_addr_copy(addr, ndev->dev_addr);
 617		wilc_set_mac_address(vif, addr);
 618	} else {
 619		wilc_get_mac_address(vif, addr);
 620		eth_hw_addr_set(ndev, addr);
 621	}
 622	netdev_dbg(ndev, "Mac address: %pM\n", ndev->dev_addr);
 623
 624	if (!is_valid_ether_addr(ndev->dev_addr)) {
 625		netdev_err(ndev, "Wrong MAC address\n");
 626		wilc_deinit_host_int(ndev);
 627		wilc_wlan_deinitialize(ndev);
 628		return -EINVAL;
 
 629	}
 630
 631	mgmt_regs.interface_stypes = vif->mgmt_reg_stypes;
 632	/* so we detect a change */
 633	vif->mgmt_reg_stypes = 0;
 634	wilc_update_mgmt_frame_registrations(vif->ndev->ieee80211_ptr->wiphy,
 635					     vif->ndev->ieee80211_ptr,
 636					     &mgmt_regs);
 637	netif_wake_queue(ndev);
 638	wl->open_ifcs++;
 639	vif->mac_opened = 1;
 640	return 0;
 641}
 642
 643static struct net_device_stats *mac_stats(struct net_device *dev)
 644{
 645	struct wilc_vif *vif = netdev_priv(dev);
 646
 647	return &vif->netstats;
 648}
 649
 650static int wilc_set_mac_addr(struct net_device *dev, void *p)
 651{
 652	int result;
 653	struct wilc_vif *vif = netdev_priv(dev);
 654	struct wilc *wilc = vif->wilc;
 655	struct sockaddr *addr = (struct sockaddr *)p;
 656	unsigned char mac_addr[ETH_ALEN];
 657	struct wilc_vif *tmp_vif;
 658	int srcu_idx;
 659
 660	if (!is_valid_ether_addr(addr->sa_data))
 661		return -EADDRNOTAVAIL;
 662
 663	if (!vif->mac_opened) {
 664		eth_commit_mac_addr_change(dev, p);
 665		return 0;
 666	}
 667
 668	/* Verify MAC Address is not already in use: */
 669
 670	srcu_idx = srcu_read_lock(&wilc->srcu);
 671	wilc_for_each_vif(wilc, tmp_vif) {
 672		wilc_get_mac_address(tmp_vif, mac_addr);
 673		if (ether_addr_equal(addr->sa_data, mac_addr)) {
 674			if (vif != tmp_vif) {
 675				srcu_read_unlock(&wilc->srcu, srcu_idx);
 676				return -EADDRNOTAVAIL;
 677			}
 678			srcu_read_unlock(&wilc->srcu, srcu_idx);
 679			return 0;
 680		}
 681	}
 682	srcu_read_unlock(&wilc->srcu, srcu_idx);
 683
 684	result = wilc_set_mac_address(vif, (u8 *)addr->sa_data);
 685	if (result)
 686		return result;
 687
 688	eth_commit_mac_addr_change(dev, p);
 689	return result;
 690}
 691
 692static void wilc_set_multicast_list(struct net_device *dev)
 693{
 694	struct netdev_hw_addr *ha;
 695	struct wilc_vif *vif = netdev_priv(dev);
 696	int i;
 697	u8 *mc_list;
 698	u8 *cur_mc;
 699
 700	if (dev->flags & IFF_PROMISC)
 701		return;
 702
 703	if (dev->flags & IFF_ALLMULTI ||
 704	    dev->mc.count > WILC_MULTICAST_TABLE_SIZE) {
 705		wilc_setup_multicast_filter(vif, 0, 0, NULL);
 706		return;
 707	}
 708
 709	if (dev->mc.count == 0) {
 710		wilc_setup_multicast_filter(vif, 1, 0, NULL);
 711		return;
 712	}
 713
 714	mc_list = kmalloc_array(dev->mc.count, ETH_ALEN, GFP_ATOMIC);
 715	if (!mc_list)
 716		return;
 717
 718	cur_mc = mc_list;
 719	i = 0;
 720	netdev_for_each_mc_addr(ha, dev) {
 721		memcpy(cur_mc, ha->addr, ETH_ALEN);
 722		netdev_dbg(dev, "Entry[%d]: %pM\n", i, cur_mc);
 723		i++;
 724		cur_mc += ETH_ALEN;
 725	}
 726
 727	if (wilc_setup_multicast_filter(vif, 1, dev->mc.count, mc_list))
 728		kfree(mc_list);
 729}
 730
 731static void wilc_tx_complete(void *priv, int status)
 732{
 733	struct tx_complete_data *pv_data = priv;
 734
 735	dev_kfree_skb(pv_data->skb);
 736	kfree(pv_data);
 737}
 738
 739netdev_tx_t wilc_mac_xmit(struct sk_buff *skb, struct net_device *ndev)
 740{
 741	struct wilc_vif *vif = netdev_priv(ndev);
 742	struct wilc *wilc = vif->wilc;
 743	struct tx_complete_data *tx_data = NULL;
 744	int queue_count;
 745
 746	if (skb->dev != ndev) {
 747		netdev_err(ndev, "Packet not destined to this device\n");
 748		dev_kfree_skb(skb);
 749		return NETDEV_TX_OK;
 750	}
 751
 752	tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
 753	if (!tx_data) {
 754		dev_kfree_skb(skb);
 755		netif_wake_queue(ndev);
 756		return NETDEV_TX_OK;
 757	}
 758
 759	tx_data->buff = skb->data;
 760	tx_data->size = skb->len;
 761	tx_data->skb  = skb;
 762
 763	vif->netstats.tx_packets++;
 764	vif->netstats.tx_bytes += tx_data->size;
 765	queue_count = wilc_wlan_txq_add_net_pkt(ndev, tx_data,
 766						tx_data->buff, tx_data->size,
 767						wilc_tx_complete);
 768
 769	if (queue_count > FLOW_CONTROL_UPPER_THRESHOLD) {
 770		int srcu_idx;
 771		struct wilc_vif *vif;
 772
 773		srcu_idx = srcu_read_lock(&wilc->srcu);
 774		wilc_for_each_vif(wilc, vif) {
 775			if (vif->mac_opened)
 776				netif_stop_queue(vif->ndev);
 777		}
 778		srcu_read_unlock(&wilc->srcu, srcu_idx);
 779	}
 780
 781	return NETDEV_TX_OK;
 782}
 783
 784static int wilc_mac_close(struct net_device *ndev)
 785{
 786	struct wilc_vif *vif = netdev_priv(ndev);
 787	struct wilc *wl = vif->wilc;
 788
 789	netdev_dbg(ndev, "Mac close\n");
 790
 791	if (wl->open_ifcs > 0)
 792		wl->open_ifcs--;
 793	else
 794		return 0;
 795
 796	if (vif->ndev) {
 797		netif_stop_queue(vif->ndev);
 798
 799		wilc_handle_disconnect(vif);
 800		wilc_deinit_host_int(vif->ndev);
 801	}
 802
 803	if (wl->open_ifcs == 0) {
 804		netdev_dbg(ndev, "Deinitializing wilc1000\n");
 805		wl->close = 1;
 806		wilc_wlan_deinitialize(ndev);
 807	}
 808
 809	vif->mac_opened = 0;
 810
 811	return 0;
 812}
 813
 814void wilc_frmw_to_host(struct wilc *wilc, u8 *buff, u32 size,
 815		       u32 pkt_offset)
 816{
 817	unsigned char *buff_to_send = NULL;
 818	struct net_device *wilc_netdev;
 819	unsigned int frame_len = 0;
 820	struct wilc_vif *vif;
 821	struct sk_buff *skb;
 822	int srcu_idx;
 823	int stats;
 824
 825	if (!wilc)
 826		return;
 827
 828	srcu_idx = srcu_read_lock(&wilc->srcu);
 829	wilc_netdev = get_if_handler(wilc, buff);
 830	if (!wilc_netdev)
 831		goto out;
 832
 833	buff += pkt_offset;
 834	vif = netdev_priv(wilc_netdev);
 835
 836	if (size > 0) {
 837		frame_len = size;
 838		buff_to_send = buff;
 839
 840		skb = dev_alloc_skb(frame_len);
 841		if (!skb)
 842			goto out;
 843
 844		skb->dev = wilc_netdev;
 845
 846		skb_put_data(skb, buff_to_send, frame_len);
 847
 848		skb->protocol = eth_type_trans(skb, wilc_netdev);
 849		vif->netstats.rx_packets++;
 850		vif->netstats.rx_bytes += frame_len;
 851		skb->ip_summed = CHECKSUM_UNNECESSARY;
 852		stats = netif_rx(skb);
 853		netdev_dbg(wilc_netdev, "netif_rx ret value is: %d\n", stats);
 854	}
 855out:
 856	srcu_read_unlock(&wilc->srcu, srcu_idx);
 857}
 858
 859void wilc_wfi_mgmt_rx(struct wilc *wilc, u8 *buff, u32 size, bool is_auth)
 860{
 861	int srcu_idx;
 862	struct wilc_vif *vif;
 863
 864	srcu_idx = srcu_read_lock(&wilc->srcu);
 865	wilc_for_each_vif(wilc, vif) {
 866		struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buff;
 867		u16 type = le16_to_cpup((__le16 *)buff);
 868		u32 type_bit = BIT(type >> 4);
 869		u32 auth_bit = BIT(IEEE80211_STYPE_AUTH >> 4);
 870
 871		if ((vif->mgmt_reg_stypes & auth_bit &&
 872		     ieee80211_is_auth(mgmt->frame_control)) &&
 873		    vif->iftype == WILC_STATION_MODE && is_auth) {
 874			wilc_wfi_mgmt_frame_rx(vif, buff, size);
 875			break;
 876		}
 877
 878		if (vif->priv.p2p_listen_state &&
 879		    vif->mgmt_reg_stypes & type_bit)
 880			wilc_wfi_p2p_rx(vif, buff, size);
 881
 882		if (vif->monitor_flag)
 883			wilc_wfi_monitor_rx(wilc->monitor_dev, buff, size);
 884	}
 885	srcu_read_unlock(&wilc->srcu, srcu_idx);
 886}
 887
 888static const struct net_device_ops wilc_netdev_ops = {
 889	.ndo_init = mac_init_fn,
 890	.ndo_open = wilc_mac_open,
 891	.ndo_stop = wilc_mac_close,
 892	.ndo_set_mac_address = wilc_set_mac_addr,
 893	.ndo_start_xmit = wilc_mac_xmit,
 894	.ndo_get_stats = mac_stats,
 895	.ndo_set_rx_mode  = wilc_set_multicast_list,
 896};
 897
 898void wilc_netdev_cleanup(struct wilc *wilc)
 899{
 900	struct wilc_vif *vif, *vif_tmp;
 901
 902	if (!wilc)
 903		return;
 904
 905	if (wilc->firmware) {
 906		release_firmware(wilc->firmware);
 907		wilc->firmware = NULL;
 908	}
 909
 910	list_for_each_entry_safe(vif, vif_tmp, &wilc->vif_list, list) {
 911		mutex_lock(&wilc->vif_mutex);
 912		list_del_rcu(&vif->list);
 913		wilc->vif_num--;
 914		mutex_unlock(&wilc->vif_mutex);
 915		synchronize_srcu(&wilc->srcu);
 916		if (vif->ndev)
 917			unregister_netdev(vif->ndev);
 918	}
 919
 920	wilc_wfi_deinit_mon_interface(wilc, false);
 921	destroy_workqueue(wilc->hif_workqueue);
 922
 923	wilc_wlan_cfg_deinit(wilc);
 924	wlan_deinit_locks(wilc);
 925	wiphy_unregister(wilc->wiphy);
 926	wiphy_free(wilc->wiphy);
 927}
 928EXPORT_SYMBOL_GPL(wilc_netdev_cleanup);
 929
 930static u8 wilc_get_available_idx(struct wilc *wl)
 931{
 932	int idx = 0;
 933	struct wilc_vif *vif;
 934	int srcu_idx;
 935
 936	srcu_idx = srcu_read_lock(&wl->srcu);
 937	wilc_for_each_vif(wl, vif) {
 938		if (vif->idx == 0)
 939			idx = 1;
 940		else
 941			idx = 0;
 942	}
 943	srcu_read_unlock(&wl->srcu, srcu_idx);
 944	return idx;
 945}
 946
 947struct wilc_vif *wilc_netdev_ifc_init(struct wilc *wl, const char *name,
 948				      int vif_type, enum nl80211_iftype type,
 949				      bool rtnl_locked)
 950{
 
 951	struct net_device *ndev;
 952	struct wilc_vif *vif;
 953	int ret;
 954
 955	ndev = alloc_etherdev(sizeof(*vif));
 956	if (!ndev)
 957		return ERR_PTR(-ENOMEM);
 958
 959	vif = netdev_priv(ndev);
 960	ndev->ieee80211_ptr = &vif->priv.wdev;
 961	strcpy(ndev->name, name);
 962	vif->wilc = wl;
 963	vif->ndev = ndev;
 964	ndev->ml_priv = vif;
 965
 966	ndev->netdev_ops = &wilc_netdev_ops;
 967
 968	SET_NETDEV_DEV(ndev, wiphy_dev(wl->wiphy));
 969
 970	vif->priv.wdev.wiphy = wl->wiphy;
 971	vif->priv.wdev.netdev = ndev;
 972	vif->priv.wdev.iftype = type;
 973	vif->priv.dev = ndev;
 974
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 975	if (rtnl_locked)
 976		ret = cfg80211_register_netdevice(ndev);
 977	else
 978		ret = register_netdev(ndev);
 979
 980	if (ret) {
 981		ret = -EFAULT;
 982		goto error;
 983	}
 984
 985	ndev->needs_free_netdev = true;
 986	vif->iftype = vif_type;
 987	vif->idx = wilc_get_available_idx(wl);
 988	vif->mac_opened = 0;
 989	mutex_lock(&wl->vif_mutex);
 990	list_add_tail_rcu(&vif->list, &wl->vif_list);
 991	wl->vif_num += 1;
 992	mutex_unlock(&wl->vif_mutex);
 993	synchronize_srcu(&wl->srcu);
 994
 995	return vif;
 996
 997error:
 998	if (rtnl_locked)
 999		cfg80211_unregister_netdevice(ndev);
1000	else
1001		unregister_netdev(ndev);
1002	free_netdev(ndev);
1003	return ERR_PTR(ret);
1004}
 
1005
1006MODULE_DESCRIPTION("Atmel WILC1000 core wireless driver");
1007MODULE_LICENSE("GPL");
1008MODULE_FIRMWARE(WILC1000_FW(WILC1000_API_VER));