linux/drivers/staging/wilc1000/wilc_netdev.c
<<
>>
Prefs
   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 "wilc_wfi_cfgoperations.h"
  14
  15#define WILC_MULTICAST_TABLE_SIZE       8
  16
  17static irqreturn_t isr_uh_routine(int irq, void *user_data)
  18{
  19        struct net_device *dev = user_data;
  20        struct wilc_vif *vif = netdev_priv(dev);
  21        struct wilc *wilc = vif->wilc;
  22
  23        if (wilc->close) {
  24                netdev_err(dev, "Can't handle UH interrupt\n");
  25                return IRQ_HANDLED;
  26        }
  27        return IRQ_WAKE_THREAD;
  28}
  29
  30static irqreturn_t isr_bh_routine(int irq, void *userdata)
  31{
  32        struct net_device *dev = userdata;
  33        struct wilc_vif *vif = netdev_priv(userdata);
  34        struct wilc *wilc = vif->wilc;
  35
  36        if (wilc->close) {
  37                netdev_err(dev, "Can't handle BH interrupt\n");
  38                return IRQ_HANDLED;
  39        }
  40
  41        wilc_handle_isr(wilc);
  42
  43        return IRQ_HANDLED;
  44}
  45
  46static int init_irq(struct net_device *dev)
  47{
  48        int ret = 0;
  49        struct wilc_vif *vif = netdev_priv(dev);
  50        struct wilc *wl = vif->wilc;
  51
  52        ret = gpiod_direction_input(wl->gpio_irq);
  53        if (ret) {
  54                netdev_err(dev, "could not obtain gpio for WILC_INTR\n");
  55                return ret;
  56        }
  57
  58        wl->dev_irq_num = gpiod_to_irq(wl->gpio_irq);
  59
  60        ret = request_threaded_irq(wl->dev_irq_num, isr_uh_routine,
  61                                   isr_bh_routine,
  62                                   IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  63                                   "WILC_IRQ", dev);
  64        if (ret < 0)
  65                netdev_err(dev, "Failed to request IRQ\n");
  66        else
  67                netdev_dbg(dev, "IRQ request succeeded IRQ-NUM= %d\n",
  68                           wl->dev_irq_num);
  69
  70        return ret;
  71}
  72
  73static void deinit_irq(struct net_device *dev)
  74{
  75        struct wilc_vif *vif = netdev_priv(dev);
  76        struct wilc *wilc = vif->wilc;
  77
  78        /* Deinitialize IRQ */
  79        if (wilc->dev_irq_num)
  80                free_irq(wilc->dev_irq_num, wilc);
  81}
  82
  83void wilc_mac_indicate(struct wilc *wilc)
  84{
  85        s8 status;
  86
  87        wilc_wlan_cfg_get_val(wilc, WID_STATUS, &status, 1);
  88        if (wilc->mac_status == WILC_MAC_STATUS_INIT) {
  89                wilc->mac_status = status;
  90                complete(&wilc->sync_event);
  91        } else {
  92                wilc->mac_status = status;
  93        }
  94}
  95
  96static struct net_device *get_if_handler(struct wilc *wilc, u8 *mac_header)
  97{
  98        u8 *bssid, *bssid1;
  99        int i = 0;
 100
 101        bssid = mac_header + 10;
 102        bssid1 = mac_header + 4;
 103
 104        for (i = 0; i < wilc->vif_num; i++) {
 105                if (wilc->vif[i]->mode == WILC_STATION_MODE)
 106                        if (ether_addr_equal_unaligned(bssid,
 107                                                       wilc->vif[i]->bssid))
 108                                return wilc->vif[i]->ndev;
 109                if (wilc->vif[i]->mode == WILC_AP_MODE)
 110                        if (ether_addr_equal_unaligned(bssid1,
 111                                                       wilc->vif[i]->bssid))
 112                                return wilc->vif[i]->ndev;
 113        }
 114
 115        return NULL;
 116}
 117
 118void wilc_wlan_set_bssid(struct net_device *wilc_netdev, u8 *bssid, u8 mode)
 119{
 120        struct wilc_vif *vif = netdev_priv(wilc_netdev);
 121
 122        if (bssid)
 123                ether_addr_copy(vif->bssid, bssid);
 124        else
 125                eth_zero_addr(vif->bssid);
 126
 127        vif->mode = mode;
 128}
 129
 130int wilc_wlan_get_num_conn_ifcs(struct wilc *wilc)
 131{
 132        u8 i = 0;
 133        u8 ret_val = 0;
 134
 135        for (i = 0; i < wilc->vif_num; i++)
 136                if (!is_zero_ether_addr(wilc->vif[i]->bssid))
 137                        ret_val++;
 138
 139        return ret_val;
 140}
 141
 142static int wilc_txq_task(void *vp)
 143{
 144        int ret;
 145        u32 txq_count;
 146        struct net_device *dev = vp;
 147        struct wilc_vif *vif = netdev_priv(dev);
 148        struct wilc *wl = vif->wilc;
 149
 150        complete(&wl->txq_thread_started);
 151        while (1) {
 152                wait_for_completion(&wl->txq_event);
 153
 154                if (wl->close) {
 155                        complete(&wl->txq_thread_started);
 156
 157                        while (!kthread_should_stop())
 158                                schedule();
 159                        break;
 160                }
 161                do {
 162                        ret = wilc_wlan_handle_txq(dev, &txq_count);
 163                        if (txq_count < FLOW_CONTROL_LOWER_THRESHOLD) {
 164                                if (wl->vif[0]->mac_opened &&
 165                                    netif_queue_stopped(wl->vif[0]->ndev))
 166                                        netif_wake_queue(wl->vif[0]->ndev);
 167                                if (wl->vif[1]->mac_opened &&
 168                                    netif_queue_stopped(wl->vif[1]->ndev))
 169                                        netif_wake_queue(wl->vif[1]->ndev);
 170                        }
 171                } while (ret == -ENOBUFS && !wl->close);
 172        }
 173        return 0;
 174}
 175
 176static int wilc_wlan_get_firmware(struct net_device *dev)
 177{
 178        struct wilc_vif *vif = netdev_priv(dev);
 179        struct wilc *wilc = vif->wilc;
 180        int chip_id, ret = 0;
 181        const struct firmware *wilc_firmware;
 182        char *firmware;
 183
 184        chip_id = wilc_get_chipid(wilc, false);
 185
 186        if (chip_id < 0x1003a0)
 187                firmware = FIRMWARE_1002;
 188        else
 189                firmware = FIRMWARE_1003;
 190
 191        netdev_info(dev, "loading firmware %s\n", firmware);
 192
 193        if (request_firmware(&wilc_firmware, firmware, wilc->dev) != 0) {
 194                netdev_err(dev, "%s - firmware not available\n", firmware);
 195                ret = -1;
 196                goto fail;
 197        }
 198        wilc->firmware = wilc_firmware;
 199
 200fail:
 201
 202        return ret;
 203}
 204
 205static int wilc_start_firmware(struct net_device *dev)
 206{
 207        struct wilc_vif *vif = netdev_priv(dev);
 208        struct wilc *wilc = vif->wilc;
 209        int ret = 0;
 210
 211        ret = wilc_wlan_start(wilc);
 212        if (ret < 0)
 213                return ret;
 214
 215        if (!wait_for_completion_timeout(&wilc->sync_event,
 216                                         msecs_to_jiffies(5000)))
 217                return -ETIME;
 218
 219        return 0;
 220}
 221
 222static int wilc1000_firmware_download(struct net_device *dev)
 223{
 224        struct wilc_vif *vif = netdev_priv(dev);
 225        struct wilc *wilc = vif->wilc;
 226        int ret = 0;
 227
 228        if (!wilc->firmware) {
 229                netdev_err(dev, "Firmware buffer is NULL\n");
 230                return -ENOBUFS;
 231        }
 232
 233        ret = wilc_wlan_firmware_download(wilc, wilc->firmware->data,
 234                                          wilc->firmware->size);
 235        if (ret < 0)
 236                return ret;
 237
 238        release_firmware(wilc->firmware);
 239        wilc->firmware = NULL;
 240
 241        netdev_dbg(dev, "Download Succeeded\n");
 242
 243        return 0;
 244}
 245
 246static int wilc_init_fw_config(struct net_device *dev, struct wilc_vif *vif)
 247{
 248        struct wilc_priv *priv;
 249        struct host_if_drv *hif_drv;
 250        u8 b;
 251        u16 hw;
 252        u32 w;
 253
 254        netdev_dbg(dev, "Start configuring Firmware\n");
 255        priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
 256        hif_drv = (struct host_if_drv *)priv->hif_drv;
 257        netdev_dbg(dev, "Host = %p\n", hif_drv);
 258
 259        w = vif->iftype;
 260        cpu_to_le32s(&w);
 261        if (!wilc_wlan_cfg_set(vif, 1, WID_SET_OPERATION_MODE, (u8 *)&w, 4,
 262                               0, 0))
 263                goto fail;
 264
 265        b = WILC_FW_BSS_TYPE_INFRA;
 266        if (!wilc_wlan_cfg_set(vif, 0, WID_BSS_TYPE, &b, 1, 0, 0))
 267                goto fail;
 268
 269        b = WILC_FW_TX_RATE_AUTO;
 270        if (!wilc_wlan_cfg_set(vif, 0, WID_CURRENT_TX_RATE, &b, 1, 0, 0))
 271                goto fail;
 272
 273        b = WILC_FW_OPER_MODE_G_MIXED_11B_2;
 274        if (!wilc_wlan_cfg_set(vif, 0, WID_11G_OPERATING_MODE, &b, 1, 0, 0))
 275                goto fail;
 276
 277        b = WILC_FW_PREAMBLE_SHORT;
 278        if (!wilc_wlan_cfg_set(vif, 0, WID_PREAMBLE, &b, 1, 0, 0))
 279                goto fail;
 280
 281        b = WILC_FW_11N_PROT_AUTO;
 282        if (!wilc_wlan_cfg_set(vif, 0, WID_11N_PROT_MECH, &b, 1, 0, 0))
 283                goto fail;
 284
 285        b = WILC_FW_ACTIVE_SCAN;
 286        if (!wilc_wlan_cfg_set(vif, 0, WID_SCAN_TYPE, &b, 1, 0, 0))
 287                goto fail;
 288
 289        b = WILC_FW_SITE_SURVEY_OFF;
 290        if (!wilc_wlan_cfg_set(vif, 0, WID_SITE_SURVEY, &b, 1, 0, 0))
 291                goto fail;
 292
 293        hw = 0xffff;
 294        cpu_to_le16s(&hw);
 295        if (!wilc_wlan_cfg_set(vif, 0, WID_RTS_THRESHOLD, (u8 *)&hw, 2, 0, 0))
 296                goto fail;
 297
 298        hw = 2346;
 299        cpu_to_le16s(&hw);
 300        if (!wilc_wlan_cfg_set(vif, 0, WID_FRAG_THRESHOLD, (u8 *)&hw, 2, 0, 0))
 301                goto fail;
 302
 303        b = 0;
 304        if (!wilc_wlan_cfg_set(vif, 0, WID_BCAST_SSID, &b, 1, 0, 0))
 305                goto fail;
 306
 307        b = 1;
 308        if (!wilc_wlan_cfg_set(vif, 0, WID_QOS_ENABLE, &b, 1, 0, 0))
 309                goto fail;
 310
 311        b = WILC_FW_NO_POWERSAVE;
 312        if (!wilc_wlan_cfg_set(vif, 0, WID_POWER_MANAGEMENT, &b, 1, 0, 0))
 313                goto fail;
 314
 315        b = WILC_FW_SEC_NO;
 316        if (!wilc_wlan_cfg_set(vif, 0, WID_11I_MODE, &b, 1, 0, 0))
 317                goto fail;
 318
 319        b = WILC_FW_AUTH_OPEN_SYSTEM;
 320        if (!wilc_wlan_cfg_set(vif, 0, WID_AUTH_TYPE, &b, 1, 0, 0))
 321                goto fail;
 322
 323        b = 3;
 324        if (!wilc_wlan_cfg_set(vif, 0, WID_LISTEN_INTERVAL, &b, 1, 0, 0))
 325                goto fail;
 326
 327        b = 3;
 328        if (!wilc_wlan_cfg_set(vif, 0, WID_DTIM_PERIOD, &b, 1, 0, 0))
 329                goto fail;
 330
 331        b = WILC_FW_ACK_POLICY_NORMAL;
 332        if (!wilc_wlan_cfg_set(vif, 0, WID_ACK_POLICY, &b, 1, 0, 0))
 333                goto fail;
 334
 335        b = 0;
 336        if (!wilc_wlan_cfg_set(vif, 0, WID_USER_CONTROL_ON_TX_POWER, &b, 1,
 337                               0, 0))
 338                goto fail;
 339
 340        b = 48;
 341        if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11A, &b, 1, 0, 0))
 342                goto fail;
 343
 344        b = 28;
 345        if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11B, &b, 1, 0, 0))
 346                goto fail;
 347
 348        hw = 100;
 349        cpu_to_le16s(&hw);
 350        if (!wilc_wlan_cfg_set(vif, 0, WID_BEACON_INTERVAL, (u8 *)&hw, 2, 0, 0))
 351                goto fail;
 352
 353        b = WILC_FW_REKEY_POLICY_DISABLE;
 354        if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_POLICY, &b, 1, 0, 0))
 355                goto fail;
 356
 357        w = 84600;
 358        cpu_to_le32s(&w);
 359        if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PERIOD, (u8 *)&w, 4, 0, 0))
 360                goto fail;
 361
 362        w = 500;
 363        cpu_to_le32s(&w);
 364        if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PACKET_COUNT, (u8 *)&w, 4, 0,
 365                               0))
 366                goto fail;
 367
 368        b = 1;
 369        if (!wilc_wlan_cfg_set(vif, 0, WID_SHORT_SLOT_ALLOWED, &b, 1, 0,
 370                               0))
 371                goto fail;
 372
 373        b = WILC_FW_ERP_PROT_SELF_CTS;
 374        if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ERP_PROT_TYPE, &b, 1, 0, 0))
 375                goto fail;
 376
 377        b = 1;
 378        if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ENABLE, &b, 1, 0, 0))
 379                goto fail;
 380
 381        b = WILC_FW_11N_OP_MODE_HT_MIXED;
 382        if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OPERATING_MODE, &b, 1, 0, 0))
 383                goto fail;
 384
 385        b = 1;
 386        if (!wilc_wlan_cfg_set(vif, 0, WID_11N_TXOP_PROT_DISABLE, &b, 1, 0, 0))
 387                goto fail;
 388
 389        b = WILC_FW_OBBS_NONHT_DETECT_PROTECT_REPORT;
 390        if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OBSS_NONHT_DETECTION, &b, 1,
 391                               0, 0))
 392                goto fail;
 393
 394        b = WILC_FW_HT_PROT_RTS_CTS_NONHT;
 395        if (!wilc_wlan_cfg_set(vif, 0, WID_11N_HT_PROT_TYPE, &b, 1, 0, 0))
 396                goto fail;
 397
 398        b = 0;
 399        if (!wilc_wlan_cfg_set(vif, 0, WID_11N_RIFS_PROT_ENABLE, &b, 1, 0,
 400                               0))
 401                goto fail;
 402
 403        b = 7;
 404        if (!wilc_wlan_cfg_set(vif, 0, WID_11N_CURRENT_TX_MCS, &b, 1, 0, 0))
 405                goto fail;
 406
 407        b = 1;
 408        if (!wilc_wlan_cfg_set(vif, 0, WID_11N_IMMEDIATE_BA_ENABLED, &b, 1,
 409                               1, 1))
 410                goto fail;
 411
 412        return 0;
 413
 414fail:
 415        return -1;
 416}
 417
 418static void wlan_deinit_locks(struct net_device *dev)
 419{
 420        struct wilc_vif *vif = netdev_priv(dev);
 421        struct wilc *wilc = vif->wilc;
 422
 423        mutex_destroy(&wilc->hif_cs);
 424        mutex_destroy(&wilc->rxq_cs);
 425        mutex_destroy(&wilc->cfg_cmd_lock);
 426        mutex_destroy(&wilc->txq_add_to_head_cs);
 427}
 428
 429static void wlan_deinitialize_threads(struct net_device *dev)
 430{
 431        struct wilc_vif *vif = netdev_priv(dev);
 432        struct wilc *wl = vif->wilc;
 433
 434        wl->close = 1;
 435
 436        complete(&wl->txq_event);
 437
 438        if (wl->txq_thread) {
 439                kthread_stop(wl->txq_thread);
 440                wl->txq_thread = NULL;
 441        }
 442}
 443
 444static void wilc_wlan_deinitialize(struct net_device *dev)
 445{
 446        struct wilc_vif *vif = netdev_priv(dev);
 447        struct wilc *wl = vif->wilc;
 448
 449        if (!wl) {
 450                netdev_err(dev, "wl is NULL\n");
 451                return;
 452        }
 453
 454        if (wl->initialized) {
 455                netdev_info(dev, "Deinitializing wilc1000...\n");
 456
 457                if (!wl->dev_irq_num &&
 458                    wl->hif_func->disable_interrupt) {
 459                        mutex_lock(&wl->hif_cs);
 460                        wl->hif_func->disable_interrupt(wl);
 461                        mutex_unlock(&wl->hif_cs);
 462                }
 463                complete(&wl->txq_event);
 464
 465                wlan_deinitialize_threads(dev);
 466                deinit_irq(dev);
 467
 468                wilc_wlan_stop(wl);
 469                wilc_wlan_cleanup(dev);
 470                wlan_deinit_locks(dev);
 471
 472                wl->initialized = false;
 473
 474                netdev_dbg(dev, "wilc1000 deinitialization Done\n");
 475        } else {
 476                netdev_dbg(dev, "wilc1000 is not initialized\n");
 477        }
 478}
 479
 480static void wlan_init_locks(struct net_device *dev)
 481{
 482        struct wilc_vif *vif = netdev_priv(dev);
 483        struct wilc *wl = vif->wilc;
 484
 485        mutex_init(&wl->hif_cs);
 486        mutex_init(&wl->rxq_cs);
 487        mutex_init(&wl->cfg_cmd_lock);
 488
 489        spin_lock_init(&wl->txq_spinlock);
 490        mutex_init(&wl->txq_add_to_head_cs);
 491
 492        init_completion(&wl->txq_event);
 493
 494        init_completion(&wl->cfg_event);
 495        init_completion(&wl->sync_event);
 496        init_completion(&wl->txq_thread_started);
 497}
 498
 499static int wlan_initialize_threads(struct net_device *dev)
 500{
 501        struct wilc_vif *vif = netdev_priv(dev);
 502        struct wilc *wilc = vif->wilc;
 503
 504        wilc->txq_thread = kthread_run(wilc_txq_task, (void *)dev,
 505                                       "K_TXQ_TASK");
 506        if (IS_ERR(wilc->txq_thread)) {
 507                netdev_err(dev, "couldn't create TXQ thread\n");
 508                wilc->close = 0;
 509                return PTR_ERR(wilc->txq_thread);
 510        }
 511        wait_for_completion(&wilc->txq_thread_started);
 512
 513        return 0;
 514}
 515
 516static int wilc_wlan_initialize(struct net_device *dev, struct wilc_vif *vif)
 517{
 518        int ret = 0;
 519        struct wilc *wl = vif->wilc;
 520
 521        if (!wl->initialized) {
 522                wl->mac_status = WILC_MAC_STATUS_INIT;
 523                wl->close = 0;
 524
 525                wlan_init_locks(dev);
 526
 527                ret = wilc_wlan_init(dev);
 528                if (ret < 0) {
 529                        ret = -EIO;
 530                        goto fail_locks;
 531                }
 532
 533                if (wl->gpio_irq && init_irq(dev)) {
 534                        ret = -EIO;
 535                        goto fail_locks;
 536                }
 537
 538                ret = wlan_initialize_threads(dev);
 539                if (ret < 0) {
 540                        ret = -EIO;
 541                        goto fail_wilc_wlan;
 542                }
 543
 544                if (!wl->dev_irq_num &&
 545                    wl->hif_func->enable_interrupt &&
 546                    wl->hif_func->enable_interrupt(wl)) {
 547                        ret = -EIO;
 548                        goto fail_irq_init;
 549                }
 550
 551                if (wilc_wlan_get_firmware(dev)) {
 552                        ret = -EIO;
 553                        goto fail_irq_enable;
 554                }
 555
 556                ret = wilc1000_firmware_download(dev);
 557                if (ret < 0) {
 558                        ret = -EIO;
 559                        goto fail_irq_enable;
 560                }
 561
 562                ret = wilc_start_firmware(dev);
 563                if (ret < 0) {
 564                        ret = -EIO;
 565                        goto fail_irq_enable;
 566                }
 567
 568                if (wilc_wlan_cfg_get(vif, 1, WID_FIRMWARE_VERSION, 1, 0)) {
 569                        int size;
 570                        char firmware_ver[20];
 571
 572                        size = wilc_wlan_cfg_get_val(wl, WID_FIRMWARE_VERSION,
 573                                                     firmware_ver,
 574                                                     sizeof(firmware_ver));
 575                        firmware_ver[size] = '\0';
 576                        netdev_dbg(dev, "Firmware Ver = %s\n", firmware_ver);
 577                }
 578                ret = wilc_init_fw_config(dev, vif);
 579
 580                if (ret < 0) {
 581                        netdev_err(dev, "Failed to configure firmware\n");
 582                        ret = -EIO;
 583                        goto fail_fw_start;
 584                }
 585
 586                wl->initialized = true;
 587                return 0;
 588
 589fail_fw_start:
 590                wilc_wlan_stop(wl);
 591
 592fail_irq_enable:
 593                if (!wl->dev_irq_num &&
 594                    wl->hif_func->disable_interrupt)
 595                        wl->hif_func->disable_interrupt(wl);
 596fail_irq_init:
 597                if (wl->dev_irq_num)
 598                        deinit_irq(dev);
 599
 600                wlan_deinitialize_threads(dev);
 601fail_wilc_wlan:
 602                wilc_wlan_cleanup(dev);
 603fail_locks:
 604                wlan_deinit_locks(dev);
 605                netdev_err(dev, "WLAN initialization FAILED\n");
 606        } else {
 607                netdev_dbg(dev, "wilc1000 already initialized\n");
 608        }
 609        return ret;
 610}
 611
 612static int mac_init_fn(struct net_device *ndev)
 613{
 614        netif_start_queue(ndev);
 615        netif_stop_queue(ndev);
 616
 617        return 0;
 618}
 619
 620static int wilc_mac_open(struct net_device *ndev)
 621{
 622        struct wilc_vif *vif = netdev_priv(ndev);
 623        struct wilc *wl = vif->wilc;
 624        struct wilc_priv *priv = wdev_priv(vif->ndev->ieee80211_ptr);
 625        unsigned char mac_add[ETH_ALEN] = {0};
 626        int ret = 0;
 627        int i = 0;
 628
 629        if (!wl || !wl->dev) {
 630                netdev_err(ndev, "device not ready\n");
 631                return -ENODEV;
 632        }
 633
 634        netdev_dbg(ndev, "MAC OPEN[%p]\n", ndev);
 635
 636        ret = wilc_init_host_int(ndev);
 637        if (ret < 0)
 638                return ret;
 639
 640        ret = wilc_wlan_initialize(ndev, vif);
 641        if (ret < 0) {
 642                wilc_deinit_host_int(ndev);
 643                return ret;
 644        }
 645
 646        for (i = 0; i < wl->vif_num; i++) {
 647                if (ndev == wl->vif[i]->ndev) {
 648                        wilc_set_wfi_drv_handler(vif, wilc_get_vif_idx(vif),
 649                                                 vif->iftype, vif->ifc_id);
 650                        wilc_set_operation_mode(vif, vif->iftype);
 651                        break;
 652                }
 653        }
 654
 655        wilc_get_mac_address(vif, mac_add);
 656        netdev_dbg(ndev, "Mac address: %pM\n", mac_add);
 657        memcpy(wl->vif[i]->src_addr, mac_add, ETH_ALEN);
 658        memcpy(ndev->dev_addr, wl->vif[i]->src_addr, ETH_ALEN);
 659
 660        if (!is_valid_ether_addr(ndev->dev_addr)) {
 661                netdev_err(ndev, "Wrong MAC address\n");
 662                wilc_deinit_host_int(ndev);
 663                wilc_wlan_deinitialize(ndev);
 664                return -EINVAL;
 665        }
 666
 667        wilc_mgmt_frame_register(vif->ndev->ieee80211_ptr->wiphy,
 668                                 vif->ndev->ieee80211_ptr,
 669                                 vif->frame_reg[0].type,
 670                                 vif->frame_reg[0].reg);
 671        wilc_mgmt_frame_register(vif->ndev->ieee80211_ptr->wiphy,
 672                                 vif->ndev->ieee80211_ptr,
 673                                 vif->frame_reg[1].type,
 674                                 vif->frame_reg[1].reg);
 675        netif_wake_queue(ndev);
 676        wl->open_ifcs++;
 677        priv->p2p.local_random = 0x01;
 678        vif->mac_opened = 1;
 679        return 0;
 680}
 681
 682static struct net_device_stats *mac_stats(struct net_device *dev)
 683{
 684        struct wilc_vif *vif = netdev_priv(dev);
 685
 686        return &vif->netstats;
 687}
 688
 689static void wilc_set_multicast_list(struct net_device *dev)
 690{
 691        struct netdev_hw_addr *ha;
 692        struct wilc_vif *vif = netdev_priv(dev);
 693        int i;
 694        u8 *mc_list;
 695        u8 *cur_mc;
 696
 697        if (dev->flags & IFF_PROMISC)
 698                return;
 699
 700        if (dev->flags & IFF_ALLMULTI ||
 701            dev->mc.count > WILC_MULTICAST_TABLE_SIZE) {
 702                wilc_setup_multicast_filter(vif, 0, 0, NULL);
 703                return;
 704        }
 705
 706        if (dev->mc.count == 0) {
 707                wilc_setup_multicast_filter(vif, 1, 0, NULL);
 708                return;
 709        }
 710
 711        mc_list = kmalloc_array(dev->mc.count, ETH_ALEN, GFP_ATOMIC);
 712        if (!mc_list)
 713                return;
 714
 715        cur_mc = mc_list;
 716        i = 0;
 717        netdev_for_each_mc_addr(ha, dev) {
 718                memcpy(cur_mc, ha->addr, ETH_ALEN);
 719                netdev_dbg(dev, "Entry[%d]: %pM\n", i, cur_mc);
 720                i++;
 721                cur_mc += ETH_ALEN;
 722        }
 723
 724        if (wilc_setup_multicast_filter(vif, 1, dev->mc.count, mc_list))
 725                kfree(mc_list);
 726}
 727
 728static void wilc_tx_complete(void *priv, int status)
 729{
 730        struct tx_complete_data *pv_data = priv;
 731
 732        dev_kfree_skb(pv_data->skb);
 733        kfree(pv_data);
 734}
 735
 736netdev_tx_t wilc_mac_xmit(struct sk_buff *skb, struct net_device *ndev)
 737{
 738        struct wilc_vif *vif = netdev_priv(ndev);
 739        struct wilc *wilc = vif->wilc;
 740        struct tx_complete_data *tx_data = NULL;
 741        int queue_count;
 742
 743        if (skb->dev != ndev) {
 744                netdev_err(ndev, "Packet not destined to this device\n");
 745                return 0;
 746        }
 747
 748        tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
 749        if (!tx_data) {
 750                dev_kfree_skb(skb);
 751                netif_wake_queue(ndev);
 752                return 0;
 753        }
 754
 755        tx_data->buff = skb->data;
 756        tx_data->size = skb->len;
 757        tx_data->skb  = skb;
 758
 759        vif->netstats.tx_packets++;
 760        vif->netstats.tx_bytes += tx_data->size;
 761        tx_data->bssid = wilc->vif[vif->idx]->bssid;
 762        queue_count = wilc_wlan_txq_add_net_pkt(ndev, (void *)tx_data,
 763                                                tx_data->buff, tx_data->size,
 764                                                wilc_tx_complete);
 765
 766        if (queue_count > FLOW_CONTROL_UPPER_THRESHOLD) {
 767                if (wilc->vif[0]->mac_opened)
 768                        netif_stop_queue(wilc->vif[0]->ndev);
 769                if (wilc->vif[1]->mac_opened)
 770                        netif_stop_queue(wilc->vif[1]->ndev);
 771        }
 772
 773        return 0;
 774}
 775
 776static int wilc_mac_close(struct net_device *ndev)
 777{
 778        struct wilc_vif *vif = netdev_priv(ndev);
 779        struct wilc *wl = vif->wilc;
 780
 781        netdev_dbg(ndev, "Mac close\n");
 782
 783        if (wl->open_ifcs > 0)
 784                wl->open_ifcs--;
 785        else
 786                return 0;
 787
 788        if (vif->ndev) {
 789                netif_stop_queue(vif->ndev);
 790
 791                wilc_deinit_host_int(vif->ndev);
 792        }
 793
 794        if (wl->open_ifcs == 0) {
 795                netdev_dbg(ndev, "Deinitializing wilc1000\n");
 796                wl->close = 1;
 797                wilc_wlan_deinitialize(ndev);
 798        }
 799
 800        vif->mac_opened = 0;
 801
 802        return 0;
 803}
 804
 805void wilc_frmw_to_host(struct wilc *wilc, u8 *buff, u32 size,
 806                       u32 pkt_offset)
 807{
 808        unsigned int frame_len = 0;
 809        int stats;
 810        unsigned char *buff_to_send = NULL;
 811        struct sk_buff *skb;
 812        struct net_device *wilc_netdev;
 813        struct wilc_vif *vif;
 814
 815        if (!wilc)
 816                return;
 817
 818        wilc_netdev = get_if_handler(wilc, buff);
 819        if (!wilc_netdev)
 820                return;
 821
 822        buff += pkt_offset;
 823        vif = netdev_priv(wilc_netdev);
 824
 825        if (size > 0) {
 826                frame_len = size;
 827                buff_to_send = buff;
 828
 829                skb = dev_alloc_skb(frame_len);
 830                if (!skb)
 831                        return;
 832
 833                skb->dev = wilc_netdev;
 834
 835                skb_put_data(skb, buff_to_send, frame_len);
 836
 837                skb->protocol = eth_type_trans(skb, wilc_netdev);
 838                vif->netstats.rx_packets++;
 839                vif->netstats.rx_bytes += frame_len;
 840                skb->ip_summed = CHECKSUM_UNNECESSARY;
 841                stats = netif_rx(skb);
 842                netdev_dbg(wilc_netdev, "netif_rx ret value is: %d\n", stats);
 843        }
 844}
 845
 846void wilc_wfi_mgmt_rx(struct wilc *wilc, u8 *buff, u32 size)
 847{
 848        int i = 0;
 849        struct wilc_vif *vif;
 850
 851        for (i = 0; i < wilc->vif_num; i++) {
 852                vif = netdev_priv(wilc->vif[i]->ndev);
 853                if (vif->monitor_flag) {
 854                        wilc_wfi_monitor_rx(wilc->monitor_dev, buff, size);
 855                        return;
 856                }
 857        }
 858
 859        vif = netdev_priv(wilc->vif[1]->ndev);
 860        if ((buff[0] == vif->frame_reg[0].type && vif->frame_reg[0].reg) ||
 861            (buff[0] == vif->frame_reg[1].type && vif->frame_reg[1].reg))
 862                wilc_wfi_p2p_rx(wilc->vif[1]->ndev, buff, size);
 863}
 864
 865static const struct net_device_ops wilc_netdev_ops = {
 866        .ndo_init = mac_init_fn,
 867        .ndo_open = wilc_mac_open,
 868        .ndo_stop = wilc_mac_close,
 869        .ndo_start_xmit = wilc_mac_xmit,
 870        .ndo_get_stats = mac_stats,
 871        .ndo_set_rx_mode  = wilc_set_multicast_list,
 872};
 873
 874static int dev_state_ev_handler(struct notifier_block *this,
 875                                unsigned long event, void *ptr)
 876{
 877        struct in_ifaddr *dev_iface = ptr;
 878        struct wilc_priv *priv;
 879        struct host_if_drv *hif_drv;
 880        struct net_device *dev;
 881        struct wilc_vif *vif;
 882
 883        if (!dev_iface || !dev_iface->ifa_dev || !dev_iface->ifa_dev->dev)
 884                return NOTIFY_DONE;
 885
 886        dev  = (struct net_device *)dev_iface->ifa_dev->dev;
 887        if (dev->netdev_ops != &wilc_netdev_ops)
 888                return NOTIFY_DONE;
 889
 890        if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
 891                return NOTIFY_DONE;
 892
 893        priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
 894        if (!priv)
 895                return NOTIFY_DONE;
 896
 897        hif_drv = (struct host_if_drv *)priv->hif_drv;
 898        vif = netdev_priv(dev);
 899        if (!vif || !hif_drv)
 900                return NOTIFY_DONE;
 901
 902        switch (event) {
 903        case NETDEV_UP:
 904                if (vif->iftype == WILC_STATION_MODE ||
 905                    vif->iftype == WILC_CLIENT_MODE) {
 906                        hif_drv->ifc_up = 1;
 907                        vif->obtaining_ip = false;
 908                        del_timer(&vif->during_ip_timer);
 909                }
 910
 911                if (vif->wilc->enable_ps)
 912                        wilc_set_power_mgmt(vif, 1, 0);
 913
 914                break;
 915
 916        case NETDEV_DOWN:
 917                if (vif->iftype == WILC_STATION_MODE ||
 918                    vif->iftype == WILC_CLIENT_MODE) {
 919                        hif_drv->ifc_up = 0;
 920                        vif->obtaining_ip = false;
 921                        wilc_set_power_mgmt(vif, 0, 0);
 922                }
 923
 924                wilc_resolve_disconnect_aberration(vif);
 925
 926                break;
 927
 928        default:
 929                break;
 930        }
 931
 932        return NOTIFY_DONE;
 933}
 934
 935static struct notifier_block g_dev_notifier = {
 936        .notifier_call = dev_state_ev_handler
 937};
 938
 939void wilc_netdev_cleanup(struct wilc *wilc)
 940{
 941        int i;
 942
 943        if (!wilc)
 944                return;
 945
 946        if (wilc->vif[0]->ndev || wilc->vif[1]->ndev)
 947                unregister_inetaddr_notifier(&g_dev_notifier);
 948
 949        if (wilc->firmware) {
 950                release_firmware(wilc->firmware);
 951                wilc->firmware = NULL;
 952        }
 953
 954        for (i = 0; i < WILC_NUM_CONCURRENT_IFC; i++) {
 955                if (wilc->vif[i] && wilc->vif[i]->ndev) {
 956                        unregister_netdev(wilc->vif[i]->ndev);
 957                        wilc_free_wiphy(wilc->vif[i]->ndev);
 958                        free_netdev(wilc->vif[i]->ndev);
 959                }
 960        }
 961
 962        wilc_wfi_deinit_mon_interface(wilc);
 963        flush_workqueue(wilc->hif_workqueue);
 964        destroy_workqueue(wilc->hif_workqueue);
 965        wilc_wlan_cfg_deinit(wilc);
 966        kfree(wilc->bus_data);
 967        kfree(wilc);
 968}
 969EXPORT_SYMBOL_GPL(wilc_netdev_cleanup);
 970
 971int wilc_netdev_init(struct wilc **wilc, struct device *dev, int io_type,
 972                     const struct wilc_hif_func *ops)
 973{
 974        int i, ret;
 975        struct wilc_vif *vif;
 976        struct net_device *ndev;
 977        struct wilc *wl;
 978
 979        wl = kzalloc(sizeof(*wl), GFP_KERNEL);
 980        if (!wl)
 981                return -ENOMEM;
 982
 983        ret = wilc_wlan_cfg_init(wl);
 984        if (ret)
 985                goto free_wl;
 986
 987        *wilc = wl;
 988        wl->io_type = io_type;
 989        wl->hif_func = ops;
 990        wl->enable_ps = true;
 991        wl->chip_ps_state = WILC_CHIP_WAKEDUP;
 992        INIT_LIST_HEAD(&wl->txq_head.list);
 993        INIT_LIST_HEAD(&wl->rxq_head.list);
 994
 995        wl->hif_workqueue = create_singlethread_workqueue("WILC_wq");
 996        if (!wl->hif_workqueue) {
 997                ret = -ENOMEM;
 998                goto free_cfg;
 999        }
1000
1001        register_inetaddr_notifier(&g_dev_notifier);
1002
1003        for (i = 0; i < WILC_NUM_CONCURRENT_IFC; i++) {
1004                struct wireless_dev *wdev;
1005
1006                ndev = alloc_etherdev(sizeof(struct wilc_vif));
1007                if (!ndev) {
1008                        ret = -ENOMEM;
1009                        goto free_ndev;
1010                }
1011
1012                vif = netdev_priv(ndev);
1013                memset(vif, 0, sizeof(struct wilc_vif));
1014
1015                if (i == 0) {
1016                        strcpy(ndev->name, "wlan%d");
1017                        vif->ifc_id = 1;
1018                } else {
1019                        strcpy(ndev->name, "p2p%d");
1020                        vif->ifc_id = 0;
1021                }
1022                vif->wilc = *wilc;
1023                vif->ndev = ndev;
1024                wl->vif[i] = vif;
1025                wl->vif_num = i + 1;
1026                vif->idx = i;
1027
1028                ndev->netdev_ops = &wilc_netdev_ops;
1029
1030                wdev = wilc_create_wiphy(ndev, dev);
1031                if (!wdev) {
1032                        netdev_err(ndev, "Can't register WILC Wiphy\n");
1033                        ret = -ENOMEM;
1034                        goto free_ndev;
1035                }
1036
1037                SET_NETDEV_DEV(ndev, dev);
1038
1039                vif->ndev->ieee80211_ptr = wdev;
1040                vif->ndev->ml_priv = vif;
1041                wdev->netdev = vif->ndev;
1042                vif->netstats.rx_packets = 0;
1043                vif->netstats.tx_packets = 0;
1044                vif->netstats.rx_bytes = 0;
1045                vif->netstats.tx_bytes = 0;
1046
1047                ret = register_netdev(ndev);
1048                if (ret)
1049                        goto free_ndev;
1050
1051                vif->iftype = WILC_STATION_MODE;
1052                vif->mac_opened = 0;
1053        }
1054
1055        return 0;
1056
1057free_ndev:
1058        for (; i >= 0; i--) {
1059                if (wl->vif[i]) {
1060                        if (wl->vif[i]->iftype == WILC_STATION_MODE)
1061                                unregister_netdev(wl->vif[i]->ndev);
1062
1063                        if (wl->vif[i]->ndev) {
1064                                wilc_free_wiphy(wl->vif[i]->ndev);
1065                                free_netdev(wl->vif[i]->ndev);
1066                        }
1067                }
1068        }
1069        unregister_inetaddr_notifier(&g_dev_notifier);
1070        destroy_workqueue(wl->hif_workqueue);
1071free_cfg:
1072        wilc_wlan_cfg_deinit(wl);
1073free_wl:
1074        kfree(wl);
1075        return ret;
1076}
1077EXPORT_SYMBOL_GPL(wilc_netdev_init);
1078
1079MODULE_LICENSE("GPL");
1080