linux/drivers/staging/brcm80211/brcmfmac/dhd_linux.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2010 Broadcom Corporation
   3 *
   4 * Permission to use, copy, modify, and/or distribute this software for any
   5 * purpose with or without fee is hereby granted, provided that the above
   6 * copyright notice and this permission notice appear in all copies.
   7 *
   8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15 */
  16
  17#ifdef CONFIG_WIFI_CONTROL_FUNC
  18#include <linux/platform_device.h>
  19#endif
  20#include <linux/init.h>
  21#include <linux/kernel.h>
  22#include <linux/kthread.h>
  23#include <linux/slab.h>
  24#include <linux/skbuff.h>
  25#include <linux/netdevice.h>
  26#include <linux/etherdevice.h>
  27#include <linux/mmc/sdio_func.h>
  28#include <linux/random.h>
  29#include <linux/spinlock.h>
  30#include <linux/ethtool.h>
  31#include <linux/fcntl.h>
  32#include <linux/fs.h>
  33#include <linux/uaccess.h>
  34#include <bcmdefs.h>
  35#include <bcmutils.h>
  36
  37#include <dngl_stats.h>
  38#include <dhd.h>
  39#include <dhd_bus.h>
  40#include <dhd_proto.h>
  41#include <dhd_dbg.h>
  42
  43#include <wl_cfg80211.h>
  44
  45#define EPI_VERSION_STR         "4.218.248.5"
  46#define ETH_P_BRCM                      0x886c
  47
  48#if defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC)
  49#include <linux/wifi_tiwlan.h>
  50
  51struct semaphore wifi_control_sem;
  52
  53struct dhd_bus *g_bus;
  54
  55static struct wifi_platform_data *wifi_control_data;
  56static struct resource *wifi_irqres;
  57
  58int wifi_get_irq_number(unsigned long *irq_flags_ptr)
  59{
  60        if (wifi_irqres) {
  61                *irq_flags_ptr = wifi_irqres->flags & IRQF_TRIGGER_MASK;
  62                return (int)wifi_irqres->start;
  63        }
  64#ifdef CUSTOM_OOB_GPIO_NUM
  65        return CUSTOM_OOB_GPIO_NUM;
  66#else
  67        return -1;
  68#endif
  69}
  70
  71int wifi_set_carddetect(int on)
  72{
  73        printk(KERN_ERR "%s = %d\n", __func__, on);
  74        if (wifi_control_data && wifi_control_data->set_carddetect)
  75                wifi_control_data->set_carddetect(on);
  76        return 0;
  77}
  78
  79int wifi_set_power(int on, unsigned long msec)
  80{
  81        printk(KERN_ERR "%s = %d\n", __func__, on);
  82        if (wifi_control_data && wifi_control_data->set_power)
  83                wifi_control_data->set_power(on);
  84        if (msec)
  85                mdelay(msec);
  86        return 0;
  87}
  88
  89int wifi_set_reset(int on, unsigned long msec)
  90{
  91        printk(KERN_ERR "%s = %d\n", __func__, on);
  92        if (wifi_control_data && wifi_control_data->set_reset)
  93                wifi_control_data->set_reset(on);
  94        if (msec)
  95                mdelay(msec);
  96        return 0;
  97}
  98
  99static int wifi_probe(struct platform_device *pdev)
 100{
 101        struct wifi_platform_data *wifi_ctrl =
 102            (struct wifi_platform_data *)(pdev->dev.platform_data);
 103
 104        printk(KERN_ERR "## %s\n", __func__);
 105        wifi_irqres =
 106            platform_get_resource_byname(pdev, IORESOURCE_IRQ,
 107                                         "bcm4329_wlan_irq");
 108        wifi_control_data = wifi_ctrl;
 109
 110        wifi_set_power(1, 0);   /* Power On */
 111        wifi_set_carddetect(1); /* CardDetect (0->1) */
 112
 113        up(&wifi_control_sem);
 114        return 0;
 115}
 116
 117static int wifi_remove(struct platform_device *pdev)
 118{
 119        struct wifi_platform_data *wifi_ctrl =
 120            (struct wifi_platform_data *)(pdev->dev.platform_data);
 121
 122        printk(KERN_ERR "## %s\n", __func__);
 123        wifi_control_data = wifi_ctrl;
 124
 125        wifi_set_carddetect(0); /* CardDetect (1->0) */
 126        wifi_set_power(0, 0);   /* Power Off */
 127
 128        up(&wifi_control_sem);
 129        return 0;
 130}
 131
 132static int wifi_suspend(struct platform_device *pdev, pm_message_t state)
 133{
 134        DHD_TRACE(("##> %s\n", __func__));
 135        return 0;
 136}
 137
 138static int wifi_resume(struct platform_device *pdev)
 139{
 140        DHD_TRACE(("##> %s\n", __func__));
 141        return 0;
 142}
 143
 144static struct platform_driver wifi_device = {
 145        .probe = wifi_probe,
 146        .remove = wifi_remove,
 147        .suspend = wifi_suspend,
 148        .resume = wifi_resume,
 149        .driver = {
 150                   .name = KBUILD_MODNAME,
 151                   }
 152};
 153
 154int wifi_add_dev(void)
 155{
 156        DHD_TRACE(("## Calling platform_driver_register\n"));
 157        return platform_driver_register(&wifi_device);
 158}
 159
 160void wifi_del_dev(void)
 161{
 162        DHD_TRACE(("## Unregister platform_driver_register\n"));
 163        platform_driver_unregister(&wifi_device);
 164}
 165#endif  /* defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC) */
 166
 167#if defined(CONFIG_PM_SLEEP)
 168#include <linux/suspend.h>
 169atomic_t dhd_mmc_suspend;
 170DECLARE_WAIT_QUEUE_HEAD(dhd_dpc_wait);
 171#endif  /*  defined(CONFIG_PM_SLEEP) */
 172
 173#if defined(OOB_INTR_ONLY)
 174extern void dhd_enable_oob_intr(struct dhd_bus *bus, bool enable);
 175#endif  /* defined(OOB_INTR_ONLY) */
 176
 177MODULE_AUTHOR("Broadcom Corporation");
 178MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN fullmac driver.");
 179MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN fullmac cards");
 180MODULE_LICENSE("Dual BSD/GPL");
 181
 182#define DRV_MODULE_NAME "brcmfmac"
 183
 184/* Linux wireless extension support */
 185#if defined(CONFIG_WIRELESS_EXT)
 186#include <wl_iw.h>
 187extern wl_iw_extra_params_t g_wl_iw_params;
 188#endif          /* defined(CONFIG_WIRELESS_EXT) */
 189
 190#if defined(CONFIG_HAS_EARLYSUSPEND)
 191#include <linux/earlysuspend.h>
 192extern int dhdcdc_set_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf,
 193                            uint len);
 194#endif          /* defined(CONFIG_HAS_EARLYSUSPEND) */
 195
 196#ifdef PKT_FILTER_SUPPORT
 197extern void dhd_pktfilter_offload_set(dhd_pub_t *dhd, char *arg);
 198extern void dhd_pktfilter_offload_enable(dhd_pub_t *dhd, char *arg, int enable,
 199                                         int master_mode);
 200#endif
 201
 202/* Interface control information */
 203typedef struct dhd_if {
 204        struct dhd_info *info;  /* back pointer to dhd_info */
 205        /* OS/stack specifics */
 206        struct net_device *net;
 207        struct net_device_stats stats;
 208        int idx;                /* iface idx in dongle */
 209        int state;              /* interface state */
 210        uint subunit;           /* subunit */
 211        u8 mac_addr[ETH_ALEN];  /* assigned MAC address */
 212        bool attached;          /* Delayed attachment when unset */
 213        bool txflowcontrol;     /* Per interface flow control indicator */
 214        char name[IFNAMSIZ];    /* linux interface name */
 215} dhd_if_t;
 216
 217/* Local private structure (extension of pub) */
 218typedef struct dhd_info {
 219#if defined(CONFIG_WIRELESS_EXT)
 220        wl_iw_t iw;             /* wireless extensions state (must be first) */
 221#endif                          /* defined(CONFIG_WIRELESS_EXT) */
 222
 223        dhd_pub_t pub;
 224
 225        /* OS/stack specifics */
 226        dhd_if_t *iflist[DHD_MAX_IFS];
 227
 228        struct semaphore proto_sem;
 229        wait_queue_head_t ioctl_resp_wait;
 230        struct timer_list timer;
 231        bool wd_timer_valid;
 232        struct tasklet_struct tasklet;
 233        spinlock_t sdlock;
 234        spinlock_t txqlock;
 235        /* Thread based operation */
 236        bool threads_only;
 237        struct semaphore sdsem;
 238        struct task_struct *watchdog_tsk;
 239        struct semaphore watchdog_sem;
 240        struct task_struct *dpc_tsk;
 241        struct semaphore dpc_sem;
 242
 243        /* Thread to issue ioctl for multicast */
 244        struct task_struct *sysioc_tsk;
 245        struct semaphore sysioc_sem;
 246        bool set_multicast;
 247        bool set_macaddress;
 248        u8 macvalue[ETH_ALEN];
 249        wait_queue_head_t ctrl_wait;
 250        atomic_t pend_8021x_cnt;
 251
 252#ifdef CONFIG_HAS_EARLYSUSPEND
 253        struct early_suspend early_suspend;
 254#endif                          /* CONFIG_HAS_EARLYSUSPEND */
 255} dhd_info_t;
 256
 257/* Definitions to provide path to the firmware and nvram
 258 * example nvram_path[MOD_PARAM_PATHLEN]="/projects/wlan/nvram.txt"
 259 */
 260char firmware_path[MOD_PARAM_PATHLEN];
 261char nvram_path[MOD_PARAM_PATHLEN];
 262
 263/* load firmware and/or nvram values from the filesystem */
 264module_param_string(firmware_path, firmware_path, MOD_PARAM_PATHLEN, 0);
 265module_param_string(nvram_path, nvram_path, MOD_PARAM_PATHLEN, 0);
 266
 267/* Error bits */
 268module_param(dhd_msg_level, int, 0);
 269
 270/* Spawn a thread for system ioctls (set mac, set mcast) */
 271uint dhd_sysioc = true;
 272module_param(dhd_sysioc, uint, 0);
 273
 274/* Watchdog interval */
 275uint dhd_watchdog_ms = 10;
 276module_param(dhd_watchdog_ms, uint, 0);
 277
 278#ifdef DHD_DEBUG
 279/* Console poll interval */
 280uint dhd_console_ms;
 281module_param(dhd_console_ms, uint, 0);
 282#endif                          /* DHD_DEBUG */
 283
 284/* ARP offload agent mode : Enable ARP Host Auto-Reply
 285and ARP Peer Auto-Reply */
 286uint dhd_arp_mode = 0xb;
 287module_param(dhd_arp_mode, uint, 0);
 288
 289/* ARP offload enable */
 290uint dhd_arp_enable = true;
 291module_param(dhd_arp_enable, uint, 0);
 292
 293/* Global Pkt filter enable control */
 294uint dhd_pkt_filter_enable = true;
 295module_param(dhd_pkt_filter_enable, uint, 0);
 296
 297/*  Pkt filter init setup */
 298uint dhd_pkt_filter_init;
 299module_param(dhd_pkt_filter_init, uint, 0);
 300
 301/* Pkt filter mode control */
 302uint dhd_master_mode = true;
 303module_param(dhd_master_mode, uint, 1);
 304
 305/* Watchdog thread priority, -1 to use kernel timer */
 306int dhd_watchdog_prio = 97;
 307module_param(dhd_watchdog_prio, int, 0);
 308
 309/* DPC thread priority, -1 to use tasklet */
 310int dhd_dpc_prio = 98;
 311module_param(dhd_dpc_prio, int, 0);
 312
 313/* DPC thread priority, -1 to use tasklet */
 314extern int dhd_dongle_memsize;
 315module_param(dhd_dongle_memsize, int, 0);
 316
 317/* Contorl fw roaming */
 318#ifdef CUSTOMER_HW2
 319uint dhd_roam;
 320#else
 321uint dhd_roam = 1;
 322#endif
 323
 324/* Control radio state */
 325uint dhd_radio_up = 1;
 326
 327/* Network inteface name */
 328char iface_name[IFNAMSIZ] = "wlan";
 329module_param_string(iface_name, iface_name, IFNAMSIZ, 0);
 330
 331/* The following are specific to the SDIO dongle */
 332
 333/* IOCTL response timeout */
 334int dhd_ioctl_timeout_msec = IOCTL_RESP_TIMEOUT;
 335
 336/* Idle timeout for backplane clock */
 337int dhd_idletime = DHD_IDLETIME_TICKS;
 338module_param(dhd_idletime, int, 0);
 339
 340/* Use polling */
 341uint dhd_poll = false;
 342module_param(dhd_poll, uint, 0);
 343
 344/* Use cfg80211 */
 345uint dhd_cfg80211 = true;
 346module_param(dhd_cfg80211, uint, 0);
 347
 348/* Use interrupts */
 349uint dhd_intr = true;
 350module_param(dhd_intr, uint, 0);
 351
 352/* SDIO Drive Strength (in milliamps) */
 353uint dhd_sdiod_drive_strength = 6;
 354module_param(dhd_sdiod_drive_strength, uint, 0);
 355
 356/* Tx/Rx bounds */
 357extern uint dhd_txbound;
 358extern uint dhd_rxbound;
 359module_param(dhd_txbound, uint, 0);
 360module_param(dhd_rxbound, uint, 0);
 361
 362/* Deferred transmits */
 363extern uint dhd_deferred_tx;
 364module_param(dhd_deferred_tx, uint, 0);
 365
 366#ifdef SDTEST
 367/* Echo packet generator (pkts/s) */
 368uint dhd_pktgen;
 369module_param(dhd_pktgen, uint, 0);
 370
 371/* Echo packet len (0 => sawtooth, max 2040) */
 372uint dhd_pktgen_len;
 373module_param(dhd_pktgen_len, uint, 0);
 374#endif
 375
 376#define FAVORITE_WIFI_CP        (!!dhd_cfg80211)
 377#define IS_CFG80211_FAVORITE() FAVORITE_WIFI_CP
 378#define DBG_CFG80211_GET() ((dhd_cfg80211 & WL_DBG_MASK) >> 1)
 379#define NO_FW_REQ() (dhd_cfg80211 & 0x80)
 380
 381/* Version string to report */
 382#ifdef DHD_DEBUG
 383#define DHD_COMPILED "\nCompiled in " SRCBASE
 384#else
 385#define DHD_COMPILED
 386#endif
 387
 388static void dhd_dpc(unsigned long data);
 389/* forward decl */
 390extern int dhd_wait_pend8021x(struct net_device *dev);
 391
 392#ifdef TOE
 393#ifndef BDC
 394#error TOE requires BDC
 395#endif                          /* !BDC */
 396static int dhd_toe_get(dhd_info_t *dhd, int idx, u32 *toe_ol);
 397static int dhd_toe_set(dhd_info_t *dhd, int idx, u32 toe_ol);
 398#endif                          /* TOE */
 399
 400static int dhd_wl_host_event(dhd_info_t *dhd, int *ifidx, void *pktdata,
 401                             wl_event_msg_t *event_ptr, void **data_ptr);
 402
 403#if defined(CONFIG_PM_SLEEP)
 404static int dhd_sleep_pm_callback(struct notifier_block *nfb,
 405                                 unsigned long action, void *ignored)
 406{
 407        switch (action) {
 408        case PM_HIBERNATION_PREPARE:
 409        case PM_SUSPEND_PREPARE:
 410                atomic_set(&dhd_mmc_suspend, true);
 411                return NOTIFY_OK;
 412        case PM_POST_HIBERNATION:
 413        case PM_POST_SUSPEND:
 414                atomic_set(&dhd_mmc_suspend, false);
 415                return NOTIFY_OK;
 416        }
 417        return 0;
 418}
 419
 420static struct notifier_block dhd_sleep_pm_notifier = {
 421        .notifier_call = dhd_sleep_pm_callback,
 422        .priority = 0
 423};
 424
 425extern int register_pm_notifier(struct notifier_block *nb);
 426extern int unregister_pm_notifier(struct notifier_block *nb);
 427#endif  /* defined(CONFIG_PM_SLEEP) */
 428        /* && defined(DHD_GPL) */
 429static void dhd_set_packet_filter(int value, dhd_pub_t *dhd)
 430{
 431#ifdef PKT_FILTER_SUPPORT
 432        DHD_TRACE(("%s: %d\n", __func__, value));
 433        /* 1 - Enable packet filter, only allow unicast packet to send up */
 434        /* 0 - Disable packet filter */
 435        if (dhd_pkt_filter_enable) {
 436                int i;
 437
 438                for (i = 0; i < dhd->pktfilter_count; i++) {
 439                        dhd_pktfilter_offload_set(dhd, dhd->pktfilter[i]);
 440                        dhd_pktfilter_offload_enable(dhd, dhd->pktfilter[i],
 441                                                     value, dhd_master_mode);
 442                }
 443        }
 444#endif
 445}
 446
 447#if defined(CONFIG_HAS_EARLYSUSPEND)
 448static int dhd_set_suspend(int value, dhd_pub_t *dhd)
 449{
 450        int power_mode = PM_MAX;
 451        /* wl_pkt_filter_enable_t       enable_parm; */
 452        char iovbuf[32];
 453        int bcn_li_dtim = 3;
 454#ifdef CUSTOMER_HW2
 455        uint roamvar = 1;
 456#endif                          /* CUSTOMER_HW2 */
 457
 458        DHD_TRACE(("%s: enter, value = %d in_suspend=%d\n",
 459                   __func__, value, dhd->in_suspend));
 460
 461        if (dhd && dhd->up) {
 462                if (value && dhd->in_suspend) {
 463
 464                        /* Kernel suspended */
 465                        DHD_TRACE(("%s: force extra Suspend setting\n",
 466                                   __func__));
 467
 468                        dhdcdc_set_ioctl(dhd, 0, WLC_SET_PM,
 469                                         (char *)&power_mode,
 470                                         sizeof(power_mode));
 471
 472                        /* Enable packet filter, only allow unicast
 473                                 packet to send up */
 474                        dhd_set_packet_filter(1, dhd);
 475
 476                        /* if dtim skip setup as default force it
 477                         * to wake each third dtim
 478                         * for better power saving.
 479                         * Note that side effect is chance to miss BC/MC
 480                         * packet
 481                         */
 482                        if ((dhd->dtim_skip == 0) || (dhd->dtim_skip == 1))
 483                                bcn_li_dtim = 3;
 484                        else
 485                                bcn_li_dtim = dhd->dtim_skip;
 486                        bcm_mkiovar("bcn_li_dtim", (char *)&bcn_li_dtim,
 487                                    4, iovbuf, sizeof(iovbuf));
 488                        dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf,
 489                                         sizeof(iovbuf));
 490#ifdef CUSTOMER_HW2
 491                        /* Disable build-in roaming to allowed \
 492                         * supplicant to take of romaing
 493                         */
 494                        bcm_mkiovar("roam_off", (char *)&roamvar, 4,
 495                                    iovbuf, sizeof(iovbuf));
 496                        dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf,
 497                                         sizeof(iovbuf));
 498#endif                          /* CUSTOMER_HW2 */
 499                } else {
 500
 501                        /* Kernel resumed  */
 502                        DHD_TRACE(("%s: Remove extra suspend setting\n",
 503                                   __func__));
 504
 505                        power_mode = PM_FAST;
 506                        dhdcdc_set_ioctl(dhd, 0, WLC_SET_PM,
 507                                         (char *)&power_mode,
 508                                         sizeof(power_mode));
 509
 510                        /* disable pkt filter */
 511                        dhd_set_packet_filter(0, dhd);
 512
 513                        /* restore pre-suspend setting for dtim_skip */
 514                        bcm_mkiovar("bcn_li_dtim", (char *)&dhd->dtim_skip,
 515                                    4, iovbuf, sizeof(iovbuf));
 516
 517                        dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf,
 518                                         sizeof(iovbuf));
 519#ifdef CUSTOMER_HW2
 520                        roamvar = 0;
 521                        bcm_mkiovar("roam_off", (char *)&roamvar, 4, iovbuf,
 522                                    sizeof(iovbuf));
 523                        dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf,
 524                                         sizeof(iovbuf));
 525#endif                          /* CUSTOMER_HW2 */
 526                }
 527        }
 528
 529        return 0;
 530}
 531
 532static void dhd_suspend_resume_helper(struct dhd_info *dhd, int val)
 533{
 534        dhd_pub_t *dhdp = &dhd->pub;
 535
 536        dhd_os_proto_block(dhdp);
 537        /* Set flag when early suspend was called */
 538        dhdp->in_suspend = val;
 539        if (!dhdp->suspend_disable_flag)
 540                dhd_set_suspend(val, dhdp);
 541        dhd_os_proto_unblock(dhdp);
 542}
 543
 544static void dhd_early_suspend(struct early_suspend *h)
 545{
 546        struct dhd_info *dhd = container_of(h, struct dhd_info, early_suspend);
 547
 548        DHD_TRACE(("%s: enter\n", __func__));
 549
 550        if (dhd)
 551                dhd_suspend_resume_helper(dhd, 1);
 552
 553}
 554
 555static void dhd_late_resume(struct early_suspend *h)
 556{
 557        struct dhd_info *dhd = container_of(h, struct dhd_info, early_suspend);
 558
 559        DHD_TRACE(("%s: enter\n", __func__));
 560
 561        if (dhd)
 562                dhd_suspend_resume_helper(dhd, 0);
 563}
 564#endif                          /* defined(CONFIG_HAS_EARLYSUSPEND) */
 565
 566/*
 567 * Generalized timeout mechanism.  Uses spin sleep with exponential
 568 * back-off until
 569 * the sleep time reaches one jiffy, then switches over to task delay.  Usage:
 570 *
 571 *      dhd_timeout_start(&tmo, usec);
 572 *      while (!dhd_timeout_expired(&tmo))
 573 *              if (poll_something())
 574 *                      break;
 575 *      if (dhd_timeout_expired(&tmo))
 576 *              fatal();
 577 */
 578
 579void dhd_timeout_start(dhd_timeout_t *tmo, uint usec)
 580{
 581        tmo->limit = usec;
 582        tmo->increment = 0;
 583        tmo->elapsed = 0;
 584        tmo->tick = 1000000 / HZ;
 585}
 586
 587int dhd_timeout_expired(dhd_timeout_t *tmo)
 588{
 589        /* Does nothing the first call */
 590        if (tmo->increment == 0) {
 591                tmo->increment = 1;
 592                return 0;
 593        }
 594
 595        if (tmo->elapsed >= tmo->limit)
 596                return 1;
 597
 598        /* Add the delay that's about to take place */
 599        tmo->elapsed += tmo->increment;
 600
 601        if (tmo->increment < tmo->tick) {
 602                udelay(tmo->increment);
 603                tmo->increment *= 2;
 604                if (tmo->increment > tmo->tick)
 605                        tmo->increment = tmo->tick;
 606        } else {
 607                wait_queue_head_t delay_wait;
 608                DECLARE_WAITQUEUE(wait, current);
 609                int pending;
 610                init_waitqueue_head(&delay_wait);
 611                add_wait_queue(&delay_wait, &wait);
 612                set_current_state(TASK_INTERRUPTIBLE);
 613                schedule_timeout(1);
 614                pending = signal_pending(current);
 615                remove_wait_queue(&delay_wait, &wait);
 616                set_current_state(TASK_RUNNING);
 617                if (pending)
 618                        return 1;       /* Interrupted */
 619        }
 620
 621        return 0;
 622}
 623
 624static int dhd_net2idx(dhd_info_t *dhd, struct net_device *net)
 625{
 626        int i = 0;
 627
 628        ASSERT(dhd);
 629        while (i < DHD_MAX_IFS) {
 630                if (dhd->iflist[i] && (dhd->iflist[i]->net == net))
 631                        return i;
 632                i++;
 633        }
 634
 635        return DHD_BAD_IF;
 636}
 637
 638int dhd_ifname2idx(dhd_info_t *dhd, char *name)
 639{
 640        int i = DHD_MAX_IFS;
 641
 642        ASSERT(dhd);
 643
 644        if (name == NULL || *name == '\0')
 645                return 0;
 646
 647        while (--i > 0)
 648                if (dhd->iflist[i]
 649                    && !strncmp(dhd->iflist[i]->name, name, IFNAMSIZ))
 650                        break;
 651
 652        DHD_TRACE(("%s: return idx %d for \"%s\"\n", __func__, i, name));
 653
 654        return i;               /* default - the primary interface */
 655}
 656
 657char *dhd_ifname(dhd_pub_t *dhdp, int ifidx)
 658{
 659        dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
 660
 661        ASSERT(dhd);
 662
 663        if (ifidx < 0 || ifidx >= DHD_MAX_IFS) {
 664                DHD_ERROR(("%s: ifidx %d out of range\n", __func__, ifidx));
 665                return "<if_bad>";
 666        }
 667
 668        if (dhd->iflist[ifidx] == NULL) {
 669                DHD_ERROR(("%s: null i/f %d\n", __func__, ifidx));
 670                return "<if_null>";
 671        }
 672
 673        if (dhd->iflist[ifidx]->net)
 674                return dhd->iflist[ifidx]->net->name;
 675
 676        return "<if_none>";
 677}
 678
 679static void _dhd_set_multicast_list(dhd_info_t *dhd, int ifidx)
 680{
 681        struct net_device *dev;
 682        struct netdev_hw_addr *ha;
 683        u32 allmulti, cnt;
 684
 685        wl_ioctl_t ioc;
 686        char *buf, *bufp;
 687        uint buflen;
 688        int ret;
 689
 690        ASSERT(dhd && dhd->iflist[ifidx]);
 691        dev = dhd->iflist[ifidx]->net;
 692        cnt = netdev_mc_count(dev);
 693
 694        /* Determine initial value of allmulti flag */
 695        allmulti = (dev->flags & IFF_ALLMULTI) ? true : false;
 696
 697        /* Send down the multicast list first. */
 698
 699        buflen = sizeof("mcast_list") + sizeof(cnt) + (cnt * ETH_ALEN);
 700        bufp = buf = kmalloc(buflen, GFP_ATOMIC);
 701        if (!bufp) {
 702                DHD_ERROR(("%s: out of memory for mcast_list, cnt %d\n",
 703                           dhd_ifname(&dhd->pub, ifidx), cnt));
 704                return;
 705        }
 706
 707        strcpy(bufp, "mcast_list");
 708        bufp += strlen("mcast_list") + 1;
 709
 710        cnt = cpu_to_le32(cnt);
 711        memcpy(bufp, &cnt, sizeof(cnt));
 712        bufp += sizeof(cnt);
 713
 714        netdev_for_each_mc_addr(ha, dev) {
 715                if (!cnt)
 716                        break;
 717                memcpy(bufp, ha->addr, ETH_ALEN);
 718                bufp += ETH_ALEN;
 719                cnt--;
 720        }
 721
 722        memset(&ioc, 0, sizeof(ioc));
 723        ioc.cmd = WLC_SET_VAR;
 724        ioc.buf = buf;
 725        ioc.len = buflen;
 726        ioc.set = true;
 727
 728        ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
 729        if (ret < 0) {
 730                DHD_ERROR(("%s: set mcast_list failed, cnt %d\n",
 731                           dhd_ifname(&dhd->pub, ifidx), cnt));
 732                allmulti = cnt ? true : allmulti;
 733        }
 734
 735        kfree(buf);
 736
 737        /* Now send the allmulti setting.  This is based on the setting in the
 738         * net_device flags, but might be modified above to be turned on if we
 739         * were trying to set some addresses and dongle rejected it...
 740         */
 741
 742        buflen = sizeof("allmulti") + sizeof(allmulti);
 743        buf = kmalloc(buflen, GFP_ATOMIC);
 744        if (!buf) {
 745                DHD_ERROR(("%s: out of memory for allmulti\n",
 746                           dhd_ifname(&dhd->pub, ifidx)));
 747                return;
 748        }
 749        allmulti = cpu_to_le32(allmulti);
 750
 751        if (!bcm_mkiovar
 752            ("allmulti", (void *)&allmulti, sizeof(allmulti), buf, buflen)) {
 753                DHD_ERROR(("%s: mkiovar failed for allmulti, datalen %d "
 754                        "buflen %u\n", dhd_ifname(&dhd->pub, ifidx),
 755                        (int)sizeof(allmulti), buflen));
 756                kfree(buf);
 757                return;
 758        }
 759
 760        memset(&ioc, 0, sizeof(ioc));
 761        ioc.cmd = WLC_SET_VAR;
 762        ioc.buf = buf;
 763        ioc.len = buflen;
 764        ioc.set = true;
 765
 766        ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
 767        if (ret < 0) {
 768                DHD_ERROR(("%s: set allmulti %d failed\n",
 769                           dhd_ifname(&dhd->pub, ifidx),
 770                           le32_to_cpu(allmulti)));
 771        }
 772
 773        kfree(buf);
 774
 775        /* Finally, pick up the PROMISC flag as well, like the NIC
 776                 driver does */
 777
 778        allmulti = (dev->flags & IFF_PROMISC) ? true : false;
 779        allmulti = cpu_to_le32(allmulti);
 780
 781        memset(&ioc, 0, sizeof(ioc));
 782        ioc.cmd = WLC_SET_PROMISC;
 783        ioc.buf = &allmulti;
 784        ioc.len = sizeof(allmulti);
 785        ioc.set = true;
 786
 787        ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
 788        if (ret < 0) {
 789                DHD_ERROR(("%s: set promisc %d failed\n",
 790                           dhd_ifname(&dhd->pub, ifidx),
 791                           le32_to_cpu(allmulti)));
 792        }
 793}
 794
 795static int
 796_dhd_set_mac_address(dhd_info_t *dhd, int ifidx, u8 *addr)
 797{
 798        char buf[32];
 799        wl_ioctl_t ioc;
 800        int ret;
 801
 802        DHD_TRACE(("%s enter\n", __func__));
 803        if (!bcm_mkiovar
 804            ("cur_etheraddr", (char *)addr, ETH_ALEN, buf, 32)) {
 805                DHD_ERROR(("%s: mkiovar failed for cur_etheraddr\n",
 806                           dhd_ifname(&dhd->pub, ifidx)));
 807                return -1;
 808        }
 809        memset(&ioc, 0, sizeof(ioc));
 810        ioc.cmd = WLC_SET_VAR;
 811        ioc.buf = buf;
 812        ioc.len = 32;
 813        ioc.set = true;
 814
 815        ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
 816        if (ret < 0) {
 817                DHD_ERROR(("%s: set cur_etheraddr failed\n",
 818                           dhd_ifname(&dhd->pub, ifidx)));
 819        } else {
 820                memcpy(dhd->iflist[ifidx]->net->dev_addr, addr, ETH_ALEN);
 821        }
 822
 823        return ret;
 824}
 825
 826#ifdef SOFTAP
 827extern struct net_device *ap_net_dev;
 828#endif
 829
 830static void dhd_op_if(dhd_if_t *ifp)
 831{
 832        dhd_info_t *dhd;
 833        int ret = 0, err = 0;
 834
 835        ASSERT(ifp && ifp->info && ifp->idx);   /* Virtual interfaces only */
 836
 837        dhd = ifp->info;
 838
 839        DHD_TRACE(("%s: idx %d, state %d\n", __func__, ifp->idx, ifp->state));
 840
 841        switch (ifp->state) {
 842        case WLC_E_IF_ADD:
 843                /*
 844                 * Delete the existing interface before overwriting it
 845                 * in case we missed the WLC_E_IF_DEL event.
 846                 */
 847                if (ifp->net != NULL) {
 848                        DHD_ERROR(("%s: ERROR: netdev:%s already exists, "
 849                        "try free & unregister\n",
 850                        __func__, ifp->net->name));
 851                        netif_stop_queue(ifp->net);
 852                        unregister_netdev(ifp->net);
 853                        free_netdev(ifp->net);
 854                }
 855                /* Allocate etherdev, including space for private structure */
 856                ifp->net = alloc_etherdev(sizeof(dhd));
 857                if (!ifp->net) {
 858                        DHD_ERROR(("%s: OOM - alloc_etherdev\n", __func__));
 859                        ret = -ENOMEM;
 860                }
 861                if (ret == 0) {
 862                        strcpy(ifp->net->name, ifp->name);
 863                        memcpy(netdev_priv(ifp->net), &dhd, sizeof(dhd));
 864                        err = dhd_net_attach(&dhd->pub, ifp->idx);
 865                        if (err != 0) {
 866                                DHD_ERROR(("%s: dhd_net_attach failed, "
 867                                        "err %d\n",
 868                                        __func__, err));
 869                                ret = -EOPNOTSUPP;
 870                        } else {
 871#ifdef SOFTAP
 872                                /* semaphore that the soft AP CODE
 873                                         waits on */
 874                                extern struct semaphore ap_eth_sema;
 875
 876                                /* save ptr to wl0.1 netdev for use
 877                                         in wl_iw.c  */
 878                                ap_net_dev = ifp->net;
 879                                /* signal to the SOFTAP 'sleeper' thread,
 880                                         wl0.1 is ready */
 881                                up(&ap_eth_sema);
 882#endif
 883                                DHD_TRACE(("\n ==== pid:%x, net_device for "
 884                                        "if:%s created ===\n\n",
 885                                        current->pid, ifp->net->name));
 886                                ifp->state = 0;
 887                        }
 888                }
 889                break;
 890        case WLC_E_IF_DEL:
 891                if (ifp->net != NULL) {
 892                        DHD_TRACE(("\n%s: got 'WLC_E_IF_DEL' state\n",
 893                                   __func__));
 894                        netif_stop_queue(ifp->net);
 895                        unregister_netdev(ifp->net);
 896                        ret = DHD_DEL_IF;       /* Make sure the free_netdev()
 897                                                         is called */
 898                }
 899                break;
 900        default:
 901                DHD_ERROR(("%s: bad op %d\n", __func__, ifp->state));
 902                ASSERT(!ifp->state);
 903                break;
 904        }
 905
 906        if (ret < 0) {
 907                if (ifp->net)
 908                        free_netdev(ifp->net);
 909
 910                dhd->iflist[ifp->idx] = NULL;
 911                kfree(ifp);
 912#ifdef SOFTAP
 913                if (ifp->net == ap_net_dev)
 914                        ap_net_dev = NULL;      /*  NULL  SOFTAP global
 915                                                         wl0.1 as well */
 916#endif                          /*  SOFTAP */
 917        }
 918}
 919
 920static int _dhd_sysioc_thread(void *data)
 921{
 922        dhd_info_t *dhd = (dhd_info_t *) data;
 923        int i;
 924#ifdef SOFTAP
 925        bool in_ap = false;
 926#endif
 927
 928        allow_signal(SIGTERM);
 929
 930        while (down_interruptible(&dhd->sysioc_sem) == 0) {
 931                if (kthread_should_stop())
 932                        break;
 933                for (i = 0; i < DHD_MAX_IFS; i++) {
 934                        if (dhd->iflist[i]) {
 935#ifdef SOFTAP
 936                                in_ap = (ap_net_dev != NULL);
 937#endif                          /* SOFTAP */
 938                                if (dhd->iflist[i]->state)
 939                                        dhd_op_if(dhd->iflist[i]);
 940#ifdef SOFTAP
 941                                if (dhd->iflist[i] == NULL) {
 942                                        DHD_TRACE(("\n\n %s: interface %d "
 943                                                "removed!\n", __func__, i));
 944                                        continue;
 945                                }
 946
 947                                if (in_ap && dhd->set_macaddress) {
 948                                        DHD_TRACE(("attempt to set MAC for %s "
 949                                                "in AP Mode," "blocked. \n",
 950                                                dhd->iflist[i]->net->name));
 951                                        dhd->set_macaddress = false;
 952                                        continue;
 953                                }
 954
 955                                if (in_ap && dhd->set_multicast) {
 956                                        DHD_TRACE(("attempt to set MULTICAST list for %s" "in AP Mode, blocked. \n",
 957                                                dhd->iflist[i]->net->name));
 958                                        dhd->set_multicast = false;
 959                                        continue;
 960                                }
 961#endif                          /* SOFTAP */
 962                                if (dhd->set_multicast) {
 963                                        dhd->set_multicast = false;
 964                                        _dhd_set_multicast_list(dhd, i);
 965                                }
 966                                if (dhd->set_macaddress) {
 967                                        dhd->set_macaddress = false;
 968                                        _dhd_set_mac_address(dhd, i,
 969                                                             dhd->macvalue);
 970                                }
 971                        }
 972                }
 973        }
 974        return 0;
 975}
 976
 977static int dhd_set_mac_address(struct net_device *dev, void *addr)
 978{
 979        int ret = 0;
 980
 981        dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
 982        struct sockaddr *sa = (struct sockaddr *)addr;
 983        int ifidx;
 984
 985        ifidx = dhd_net2idx(dhd, dev);
 986        if (ifidx == DHD_BAD_IF)
 987                return -1;
 988
 989        ASSERT(dhd->sysioc_tsk);
 990        memcpy(&dhd->macvalue, sa->sa_data, ETH_ALEN);
 991        dhd->set_macaddress = true;
 992        up(&dhd->sysioc_sem);
 993
 994        return ret;
 995}
 996
 997static void dhd_set_multicast_list(struct net_device *dev)
 998{
 999        dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
1000        int ifidx;
1001
1002        ifidx = dhd_net2idx(dhd, dev);
1003        if (ifidx == DHD_BAD_IF)
1004                return;
1005
1006        ASSERT(dhd->sysioc_tsk);
1007        dhd->set_multicast = true;
1008        up(&dhd->sysioc_sem);
1009}
1010
1011int dhd_sendpkt(dhd_pub_t *dhdp, int ifidx, struct sk_buff *pktbuf)
1012{
1013        int ret;
1014        dhd_info_t *dhd = (dhd_info_t *) (dhdp->info);
1015
1016        /* Reject if down */
1017        if (!dhdp->up || (dhdp->busstate == DHD_BUS_DOWN))
1018                return -ENODEV;
1019
1020        /* Update multicast statistic */
1021        if (pktbuf->len >= ETH_ALEN) {
1022                u8 *pktdata = (u8 *) (pktbuf->data);
1023                struct ethhdr *eh = (struct ethhdr *)pktdata;
1024
1025                if (is_multicast_ether_addr(eh->h_dest))
1026                        dhdp->tx_multicast++;
1027                if (ntohs(eh->h_proto) == ETH_P_PAE)
1028                        atomic_inc(&dhd->pend_8021x_cnt);
1029        }
1030
1031        /* If the protocol uses a data header, apply it */
1032        dhd_prot_hdrpush(dhdp, ifidx, pktbuf);
1033
1034        /* Use bus module to send data frame */
1035#ifdef BCMDBUS
1036        ret = dbus_send_pkt(dhdp->dbus, pktbuf, NULL /* pktinfo */);
1037#else
1038        ret = dhd_bus_txdata(dhdp->bus, pktbuf);
1039#endif                          /* BCMDBUS */
1040
1041        return ret;
1042}
1043
1044static inline void *
1045osl_pkt_frmnative(struct sk_buff *skb)
1046{
1047        return (void *)skb;
1048}
1049#define PKTFRMNATIVE(osh, skb)  \
1050        osl_pkt_frmnative((struct sk_buff *)(skb))
1051
1052static inline struct sk_buff *
1053osl_pkt_tonative(void *pkt)
1054{
1055        return (struct sk_buff *)pkt;
1056}
1057#define PKTTONATIVE(osh, pkt)   \
1058        osl_pkt_tonative((pkt))
1059
1060static int dhd_start_xmit(struct sk_buff *skb, struct net_device *net)
1061{
1062        int ret;
1063        void *pktbuf;
1064        dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1065        int ifidx;
1066
1067        DHD_TRACE(("%s: Enter\n", __func__));
1068
1069        /* Reject if down */
1070        if (!dhd->pub.up || (dhd->pub.busstate == DHD_BUS_DOWN)) {
1071                DHD_ERROR(("%s: xmit rejected pub.up=%d busstate=%d\n",
1072                           __func__, dhd->pub.up, dhd->pub.busstate));
1073                netif_stop_queue(net);
1074                return -ENODEV;
1075        }
1076
1077        ifidx = dhd_net2idx(dhd, net);
1078        if (ifidx == DHD_BAD_IF) {
1079                DHD_ERROR(("%s: bad ifidx %d\n", __func__, ifidx));
1080                netif_stop_queue(net);
1081                return -ENODEV;
1082        }
1083
1084        /* Make sure there's enough room for any header */
1085        if (skb_headroom(skb) < dhd->pub.hdrlen) {
1086                struct sk_buff *skb2;
1087
1088                DHD_INFO(("%s: insufficient headroom\n",
1089                          dhd_ifname(&dhd->pub, ifidx)));
1090                dhd->pub.tx_realloc++;
1091                skb2 = skb_realloc_headroom(skb, dhd->pub.hdrlen);
1092                dev_kfree_skb(skb);
1093                skb = skb2;
1094                if (skb == NULL) {
1095                        DHD_ERROR(("%s: skb_realloc_headroom failed\n",
1096                                   dhd_ifname(&dhd->pub, ifidx)));
1097                        ret = -ENOMEM;
1098                        goto done;
1099                }
1100        }
1101
1102        /* Convert to packet */
1103        pktbuf = PKTFRMNATIVE(dhd->pub.osh, skb);
1104        if (!pktbuf) {
1105                DHD_ERROR(("%s: PKTFRMNATIVE failed\n",
1106                           dhd_ifname(&dhd->pub, ifidx)));
1107                dev_kfree_skb_any(skb);
1108                ret = -ENOMEM;
1109                goto done;
1110        }
1111
1112        ret = dhd_sendpkt(&dhd->pub, ifidx, pktbuf);
1113
1114done:
1115        if (ret)
1116                dhd->pub.dstats.tx_dropped++;
1117        else
1118                dhd->pub.tx_packets++;
1119
1120        /* Return ok: we always eat the packet */
1121        return 0;
1122}
1123
1124void dhd_txflowcontrol(dhd_pub_t *dhdp, int ifidx, bool state)
1125{
1126        struct net_device *net;
1127        dhd_info_t *dhd = dhdp->info;
1128
1129        DHD_TRACE(("%s: Enter\n", __func__));
1130
1131        dhdp->txoff = state;
1132        ASSERT(dhd && dhd->iflist[ifidx]);
1133        net = dhd->iflist[ifidx]->net;
1134        if (state == ON)
1135                netif_stop_queue(net);
1136        else
1137                netif_wake_queue(net);
1138}
1139
1140void dhd_rx_frame(dhd_pub_t *dhdp, int ifidx, struct sk_buff *pktbuf,
1141                  int numpkt)
1142{
1143        dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
1144        struct sk_buff *skb;
1145        unsigned char *eth;
1146        uint len;
1147        void *data;
1148        struct sk_buff *pnext, *save_pktbuf;
1149        int i;
1150        dhd_if_t *ifp;
1151        wl_event_msg_t event;
1152
1153        DHD_TRACE(("%s: Enter\n", __func__));
1154
1155        save_pktbuf = pktbuf;
1156
1157        for (i = 0; pktbuf && i < numpkt; i++, pktbuf = pnext) {
1158
1159                pnext = pktbuf->next;
1160                pktbuf->next = NULL;
1161
1162                skb = PKTTONATIVE(dhdp->osh, pktbuf);
1163
1164                /* Get the protocol, maintain skb around eth_type_trans()
1165                 * The main reason for this hack is for the limitation of
1166                 * Linux 2.4 where 'eth_type_trans' uses the
1167                 * 'net->hard_header_len'
1168                 * to perform skb_pull inside vs ETH_HLEN. Since to avoid
1169                 * coping of the packet coming from the network stack to add
1170                 * BDC, Hardware header etc, during network interface
1171                 * registration
1172                 * we set the 'net->hard_header_len' to ETH_HLEN + extra space
1173                 * required
1174                 * for BDC, Hardware header etc. and not just the ETH_HLEN
1175                 */
1176                eth = skb->data;
1177                len = skb->len;
1178
1179                ifp = dhd->iflist[ifidx];
1180                if (ifp == NULL)
1181                        ifp = dhd->iflist[0];
1182
1183                ASSERT(ifp);
1184                skb->dev = ifp->net;
1185                skb->protocol = eth_type_trans(skb, skb->dev);
1186
1187                if (skb->pkt_type == PACKET_MULTICAST)
1188                        dhd->pub.rx_multicast++;
1189
1190                skb->data = eth;
1191                skb->len = len;
1192
1193                /* Strip header, count, deliver upward */
1194                skb_pull(skb, ETH_HLEN);
1195
1196                /* Process special event packets and then discard them */
1197                if (ntohs(skb->protocol) == ETH_P_BRCM)
1198                        dhd_wl_host_event(dhd, &ifidx,
1199                                          skb_mac_header(skb),
1200                                          &event, &data);
1201
1202                ASSERT(ifidx < DHD_MAX_IFS && dhd->iflist[ifidx]);
1203                if (dhd->iflist[ifidx] && !dhd->iflist[ifidx]->state)
1204                        ifp = dhd->iflist[ifidx];
1205
1206                if (ifp->net)
1207                        ifp->net->last_rx = jiffies;
1208
1209                dhdp->dstats.rx_bytes += skb->len;
1210                dhdp->rx_packets++;     /* Local count */
1211
1212                if (in_interrupt()) {
1213                        netif_rx(skb);
1214                } else {
1215                        /* If the receive is not processed inside an ISR,
1216                         * the softirqd must be woken explicitly to service
1217                         * the NET_RX_SOFTIRQ.  In 2.6 kernels, this is handled
1218                         * by netif_rx_ni(), but in earlier kernels, we need
1219                         * to do it manually.
1220                         */
1221                        netif_rx_ni(skb);
1222                }
1223        }
1224}
1225
1226void dhd_event(struct dhd_info *dhd, char *evpkt, int evlen, int ifidx)
1227{
1228        /* Linux version has nothing to do */
1229        return;
1230}
1231
1232void dhd_txcomplete(dhd_pub_t *dhdp, struct sk_buff *txp, bool success)
1233{
1234        uint ifidx;
1235        dhd_info_t *dhd = (dhd_info_t *) (dhdp->info);
1236        struct ethhdr *eh;
1237        u16 type;
1238
1239        dhd_prot_hdrpull(dhdp, &ifidx, txp);
1240
1241        eh = (struct ethhdr *)(txp->data);
1242        type = ntohs(eh->h_proto);
1243
1244        if (type == ETH_P_PAE)
1245                atomic_dec(&dhd->pend_8021x_cnt);
1246
1247}
1248
1249static struct net_device_stats *dhd_get_stats(struct net_device *net)
1250{
1251        dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1252        dhd_if_t *ifp;
1253        int ifidx;
1254
1255        DHD_TRACE(("%s: Enter\n", __func__));
1256
1257        ifidx = dhd_net2idx(dhd, net);
1258        if (ifidx == DHD_BAD_IF)
1259                return NULL;
1260
1261        ifp = dhd->iflist[ifidx];
1262        ASSERT(dhd && ifp);
1263
1264        if (dhd->pub.up) {
1265                /* Use the protocol to get dongle stats */
1266                dhd_prot_dstats(&dhd->pub);
1267        }
1268
1269        /* Copy dongle stats to net device stats */
1270        ifp->stats.rx_packets = dhd->pub.dstats.rx_packets;
1271        ifp->stats.tx_packets = dhd->pub.dstats.tx_packets;
1272        ifp->stats.rx_bytes = dhd->pub.dstats.rx_bytes;
1273        ifp->stats.tx_bytes = dhd->pub.dstats.tx_bytes;
1274        ifp->stats.rx_errors = dhd->pub.dstats.rx_errors;
1275        ifp->stats.tx_errors = dhd->pub.dstats.tx_errors;
1276        ifp->stats.rx_dropped = dhd->pub.dstats.rx_dropped;
1277        ifp->stats.tx_dropped = dhd->pub.dstats.tx_dropped;
1278        ifp->stats.multicast = dhd->pub.dstats.multicast;
1279
1280        return &ifp->stats;
1281}
1282
1283static int dhd_watchdog_thread(void *data)
1284{
1285        dhd_info_t *dhd = (dhd_info_t *) data;
1286
1287        /* This thread doesn't need any user-level access,
1288         * so get rid of all our resources
1289         */
1290#ifdef DHD_SCHED
1291        if (dhd_watchdog_prio > 0) {
1292                struct sched_param param;
1293                param.sched_priority = (dhd_watchdog_prio < MAX_RT_PRIO) ?
1294                    dhd_watchdog_prio : (MAX_RT_PRIO - 1);
1295                setScheduler(current, SCHED_FIFO, &param);
1296        }
1297#endif                          /* DHD_SCHED */
1298
1299        allow_signal(SIGTERM);
1300        /* Run until signal received */
1301        while (1) {
1302                if (kthread_should_stop())
1303                        break;
1304                if (down_interruptible(&dhd->watchdog_sem) == 0) {
1305                        if (dhd->pub.dongle_reset == false) {
1306                                /* Call the bus module watchdog */
1307                                dhd_bus_watchdog(&dhd->pub);
1308                        }
1309                        /* Count the tick for reference */
1310                        dhd->pub.tickcnt++;
1311                } else
1312                        break;
1313        }
1314        return 0;
1315}
1316
1317static void dhd_watchdog(unsigned long data)
1318{
1319        dhd_info_t *dhd = (dhd_info_t *) data;
1320
1321        if (dhd->watchdog_tsk) {
1322                up(&dhd->watchdog_sem);
1323
1324                /* Reschedule the watchdog */
1325                if (dhd->wd_timer_valid) {
1326                        mod_timer(&dhd->timer,
1327                                  jiffies + dhd_watchdog_ms * HZ / 1000);
1328                }
1329                return;
1330        }
1331
1332        /* Call the bus module watchdog */
1333        dhd_bus_watchdog(&dhd->pub);
1334
1335        /* Count the tick for reference */
1336        dhd->pub.tickcnt++;
1337
1338        /* Reschedule the watchdog */
1339        if (dhd->wd_timer_valid)
1340                mod_timer(&dhd->timer, jiffies + dhd_watchdog_ms * HZ / 1000);
1341}
1342
1343static int dhd_dpc_thread(void *data)
1344{
1345        dhd_info_t *dhd = (dhd_info_t *) data;
1346
1347        /* This thread doesn't need any user-level access,
1348         * so get rid of all our resources
1349         */
1350#ifdef DHD_SCHED
1351        if (dhd_dpc_prio > 0) {
1352                struct sched_param param;
1353                param.sched_priority =
1354                    (dhd_dpc_prio <
1355                     MAX_RT_PRIO) ? dhd_dpc_prio : (MAX_RT_PRIO - 1);
1356                setScheduler(current, SCHED_FIFO, &param);
1357        }
1358#endif                          /* DHD_SCHED */
1359
1360        allow_signal(SIGTERM);
1361        /* Run until signal received */
1362        while (1) {
1363                if (kthread_should_stop())
1364                        break;
1365                if (down_interruptible(&dhd->dpc_sem) == 0) {
1366                        /* Call bus dpc unless it indicated down
1367                                 (then clean stop) */
1368                        if (dhd->pub.busstate != DHD_BUS_DOWN) {
1369                                if (dhd_bus_dpc(dhd->pub.bus)) {
1370                                        up(&dhd->dpc_sem);
1371                                }
1372                        } else {
1373                                dhd_bus_stop(dhd->pub.bus, true);
1374                        }
1375                } else
1376                        break;
1377        }
1378        return 0;
1379}
1380
1381static void dhd_dpc(unsigned long data)
1382{
1383        dhd_info_t *dhd;
1384
1385        dhd = (dhd_info_t *) data;
1386
1387        /* Call bus dpc unless it indicated down (then clean stop) */
1388        if (dhd->pub.busstate != DHD_BUS_DOWN) {
1389                if (dhd_bus_dpc(dhd->pub.bus))
1390                        tasklet_schedule(&dhd->tasklet);
1391        } else {
1392                dhd_bus_stop(dhd->pub.bus, true);
1393        }
1394}
1395
1396void dhd_sched_dpc(dhd_pub_t *dhdp)
1397{
1398        dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
1399
1400        if (dhd->dpc_tsk) {
1401                up(&dhd->dpc_sem);
1402                return;
1403        }
1404
1405        tasklet_schedule(&dhd->tasklet);
1406}
1407
1408#ifdef TOE
1409/* Retrieve current toe component enables, which are kept
1410         as a bitmap in toe_ol iovar */
1411static int dhd_toe_get(dhd_info_t *dhd, int ifidx, u32 *toe_ol)
1412{
1413        wl_ioctl_t ioc;
1414        char buf[32];
1415        int ret;
1416
1417        memset(&ioc, 0, sizeof(ioc));
1418
1419        ioc.cmd = WLC_GET_VAR;
1420        ioc.buf = buf;
1421        ioc.len = (uint) sizeof(buf);
1422        ioc.set = false;
1423
1424        strcpy(buf, "toe_ol");
1425        ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
1426        if (ret < 0) {
1427                /* Check for older dongle image that doesn't support toe_ol */
1428                if (ret == -EIO) {
1429                        DHD_ERROR(("%s: toe not supported by device\n",
1430                                   dhd_ifname(&dhd->pub, ifidx)));
1431                        return -EOPNOTSUPP;
1432                }
1433
1434                DHD_INFO(("%s: could not get toe_ol: ret=%d\n",
1435                          dhd_ifname(&dhd->pub, ifidx), ret));
1436                return ret;
1437        }
1438
1439        memcpy(toe_ol, buf, sizeof(u32));
1440        return 0;
1441}
1442
1443/* Set current toe component enables in toe_ol iovar,
1444         and set toe global enable iovar */
1445static int dhd_toe_set(dhd_info_t *dhd, int ifidx, u32 toe_ol)
1446{
1447        wl_ioctl_t ioc;
1448        char buf[32];
1449        int toe, ret;
1450
1451        memset(&ioc, 0, sizeof(ioc));
1452
1453        ioc.cmd = WLC_SET_VAR;
1454        ioc.buf = buf;
1455        ioc.len = (uint) sizeof(buf);
1456        ioc.set = true;
1457
1458        /* Set toe_ol as requested */
1459
1460        strcpy(buf, "toe_ol");
1461        memcpy(&buf[sizeof("toe_ol")], &toe_ol, sizeof(u32));
1462
1463        ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
1464        if (ret < 0) {
1465                DHD_ERROR(("%s: could not set toe_ol: ret=%d\n",
1466                           dhd_ifname(&dhd->pub, ifidx), ret));
1467                return ret;
1468        }
1469
1470        /* Enable toe globally only if any components are enabled. */
1471
1472        toe = (toe_ol != 0);
1473
1474        strcpy(buf, "toe");
1475        memcpy(&buf[sizeof("toe")], &toe, sizeof(u32));
1476
1477        ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
1478        if (ret < 0) {
1479                DHD_ERROR(("%s: could not set toe: ret=%d\n",
1480                           dhd_ifname(&dhd->pub, ifidx), ret));
1481                return ret;
1482        }
1483
1484        return 0;
1485}
1486#endif                          /* TOE */
1487
1488static void dhd_ethtool_get_drvinfo(struct net_device *net,
1489                                    struct ethtool_drvinfo *info)
1490{
1491        dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1492
1493        sprintf(info->driver, DRV_MODULE_NAME);
1494        sprintf(info->version, "%lu", dhd->pub.drv_version);
1495        sprintf(info->fw_version, "%s", wl_cfg80211_get_fwname());
1496        sprintf(info->bus_info, "%s", dev_name(&wl_cfg80211_get_sdio_func()->dev));
1497}
1498
1499struct ethtool_ops dhd_ethtool_ops = {
1500        .get_drvinfo = dhd_ethtool_get_drvinfo
1501};
1502
1503static int dhd_ethtool(dhd_info_t *dhd, void *uaddr)
1504{
1505        struct ethtool_drvinfo info;
1506        char drvname[sizeof(info.driver)];
1507        u32 cmd;
1508#ifdef TOE
1509        struct ethtool_value edata;
1510        u32 toe_cmpnt, csum_dir;
1511        int ret;
1512#endif
1513
1514        DHD_TRACE(("%s: Enter\n", __func__));
1515
1516        /* all ethtool calls start with a cmd word */
1517        if (copy_from_user(&cmd, uaddr, sizeof(u32)))
1518                return -EFAULT;
1519
1520        switch (cmd) {
1521        case ETHTOOL_GDRVINFO:
1522                /* Copy out any request driver name */
1523                if (copy_from_user(&info, uaddr, sizeof(info)))
1524                        return -EFAULT;
1525                strncpy(drvname, info.driver, sizeof(info.driver));
1526                drvname[sizeof(info.driver) - 1] = '\0';
1527
1528                /* clear struct for return */
1529                memset(&info, 0, sizeof(info));
1530                info.cmd = cmd;
1531
1532                /* if dhd requested, identify ourselves */
1533                if (strcmp(drvname, "?dhd") == 0) {
1534                        sprintf(info.driver, "dhd");
1535                        strcpy(info.version, EPI_VERSION_STR);
1536                }
1537
1538                /* otherwise, require dongle to be up */
1539                else if (!dhd->pub.up) {
1540                        DHD_ERROR(("%s: dongle is not up\n", __func__));
1541                        return -ENODEV;
1542                }
1543
1544                /* finally, report dongle driver type */
1545                else if (dhd->pub.iswl)
1546                        sprintf(info.driver, "wl");
1547                else
1548                        sprintf(info.driver, "xx");
1549
1550                sprintf(info.version, "%lu", dhd->pub.drv_version);
1551                if (copy_to_user(uaddr, &info, sizeof(info)))
1552                        return -EFAULT;
1553                DHD_CTL(("%s: given %*s, returning %s\n", __func__,
1554                         (int)sizeof(drvname), drvname, info.driver));
1555                break;
1556
1557#ifdef TOE
1558                /* Get toe offload components from dongle */
1559        case ETHTOOL_GRXCSUM:
1560        case ETHTOOL_GTXCSUM:
1561                ret = dhd_toe_get(dhd, 0, &toe_cmpnt);
1562                if (ret < 0)
1563                        return ret;
1564
1565                csum_dir =
1566                    (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
1567
1568                edata.cmd = cmd;
1569                edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
1570
1571                if (copy_to_user(uaddr, &edata, sizeof(edata)))
1572                        return -EFAULT;
1573                break;
1574
1575                /* Set toe offload components in dongle */
1576        case ETHTOOL_SRXCSUM:
1577        case ETHTOOL_STXCSUM:
1578                if (copy_from_user(&edata, uaddr, sizeof(edata)))
1579                        return -EFAULT;
1580
1581                /* Read the current settings, update and write back */
1582                ret = dhd_toe_get(dhd, 0, &toe_cmpnt);
1583                if (ret < 0)
1584                        return ret;
1585
1586                csum_dir =
1587                    (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
1588
1589                if (edata.data != 0)
1590                        toe_cmpnt |= csum_dir;
1591                else
1592                        toe_cmpnt &= ~csum_dir;
1593
1594                ret = dhd_toe_set(dhd, 0, toe_cmpnt);
1595                if (ret < 0)
1596                        return ret;
1597
1598                /* If setting TX checksum mode, tell Linux the new mode */
1599                if (cmd == ETHTOOL_STXCSUM) {
1600                        if (edata.data)
1601                                dhd->iflist[0]->net->features |=
1602                                    NETIF_F_IP_CSUM;
1603                        else
1604                                dhd->iflist[0]->net->features &=
1605                                    ~NETIF_F_IP_CSUM;
1606                }
1607
1608                break;
1609#endif                          /* TOE */
1610
1611        default:
1612                return -EOPNOTSUPP;
1613        }
1614
1615        return 0;
1616}
1617
1618static int dhd_ioctl_entry(struct net_device *net, struct ifreq *ifr, int cmd)
1619{
1620        dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1621        dhd_ioctl_t ioc;
1622        int bcmerror = 0;
1623        int buflen = 0;
1624        void *buf = NULL;
1625        uint driver = 0;
1626        int ifidx;
1627        bool is_set_key_cmd;
1628
1629        ifidx = dhd_net2idx(dhd, net);
1630        DHD_TRACE(("%s: ifidx %d, cmd 0x%04x\n", __func__, ifidx, cmd));
1631
1632        if (ifidx == DHD_BAD_IF)
1633                return -1;
1634
1635#if defined(CONFIG_WIRELESS_EXT)
1636        /* linux wireless extensions */
1637        if ((cmd >= SIOCIWFIRST) && (cmd <= SIOCIWLAST)) {
1638                /* may recurse, do NOT lock */
1639                return wl_iw_ioctl(net, ifr, cmd);
1640        }
1641#endif                          /* defined(CONFIG_WIRELESS_EXT) */
1642
1643        if (cmd == SIOCETHTOOL)
1644                return dhd_ethtool(dhd, (void *)ifr->ifr_data);
1645
1646        if (cmd != SIOCDEVPRIVATE)
1647                return -EOPNOTSUPP;
1648
1649        memset(&ioc, 0, sizeof(ioc));
1650
1651        /* Copy the ioc control structure part of ioctl request */
1652        if (copy_from_user(&ioc, ifr->ifr_data, sizeof(wl_ioctl_t))) {
1653                bcmerror = -EINVAL;
1654                goto done;
1655        }
1656
1657        /* Copy out any buffer passed */
1658        if (ioc.buf) {
1659                buflen = min_t(int, ioc.len, DHD_IOCTL_MAXLEN);
1660                /* optimization for direct ioctl calls from kernel */
1661                /*
1662                   if (segment_eq(get_fs(), KERNEL_DS)) {
1663                   buf = ioc.buf;
1664                   } else {
1665                 */
1666                {
1667                        buf = kmalloc(buflen, GFP_ATOMIC);
1668                        if (!buf) {
1669                                bcmerror = -ENOMEM;
1670                                goto done;
1671                        }
1672                        if (copy_from_user(buf, ioc.buf, buflen)) {
1673                                bcmerror = -EINVAL;
1674                                goto done;
1675                        }
1676                }
1677        }
1678
1679        /* To differentiate between wl and dhd read 4 more byes */
1680        if ((copy_from_user(&driver, (char *)ifr->ifr_data + sizeof(wl_ioctl_t),
1681                            sizeof(uint)) != 0)) {
1682                bcmerror = -EINVAL;
1683                goto done;
1684        }
1685
1686        if (!capable(CAP_NET_ADMIN)) {
1687                bcmerror = -EPERM;
1688                goto done;
1689        }
1690
1691        /* check for local dhd ioctl and handle it */
1692        if (driver == DHD_IOCTL_MAGIC) {
1693                bcmerror = dhd_ioctl((void *)&dhd->pub, &ioc, buf, buflen);
1694                if (bcmerror)
1695                        dhd->pub.bcmerror = bcmerror;
1696                goto done;
1697        }
1698
1699        /* send to dongle (must be up, and wl) */
1700        if ((dhd->pub.busstate != DHD_BUS_DATA)) {
1701                DHD_ERROR(("%s DONGLE_DOWN,__func__\n", __func__));
1702                bcmerror = -EIO;
1703                goto done;
1704        }
1705
1706        if (!dhd->pub.iswl) {
1707                bcmerror = -EIO;
1708                goto done;
1709        }
1710
1711        /* Intercept WLC_SET_KEY IOCTL - serialize M4 send and set key IOCTL to
1712         * prevent M4 encryption.
1713         */
1714        is_set_key_cmd = ((ioc.cmd == WLC_SET_KEY) ||
1715                          ((ioc.cmd == WLC_SET_VAR) &&
1716                           !(strncmp("wsec_key", ioc.buf, 9))) ||
1717                          ((ioc.cmd == WLC_SET_VAR) &&
1718                           !(strncmp("bsscfg:wsec_key", ioc.buf, 15))));
1719        if (is_set_key_cmd)
1720                dhd_wait_pend8021x(net);
1721
1722        bcmerror =
1723            dhd_prot_ioctl(&dhd->pub, ifidx, (wl_ioctl_t *)&ioc, buf, buflen);
1724
1725done:
1726        if (!bcmerror && buf && ioc.buf) {
1727                if (copy_to_user(ioc.buf, buf, buflen))
1728                        bcmerror = -EFAULT;
1729        }
1730
1731        kfree(buf);
1732
1733        if (bcmerror > 0)
1734                bcmerror = 0;
1735
1736        return bcmerror;
1737}
1738
1739static int dhd_stop(struct net_device *net)
1740{
1741#if !defined(IGNORE_ETH0_DOWN)
1742        dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1743
1744        DHD_TRACE(("%s: Enter\n", __func__));
1745        if (IS_CFG80211_FAVORITE()) {
1746                wl_cfg80211_down();
1747        }
1748        if (dhd->pub.up == 0)
1749                return 0;
1750
1751        /* Set state and stop OS transmissions */
1752        dhd->pub.up = 0;
1753        netif_stop_queue(net);
1754#else
1755        DHD_ERROR(("BYPASS %s:due to BRCM compilation : under investigation\n",
1756                __func__));
1757#endif                          /* !defined(IGNORE_ETH0_DOWN) */
1758
1759        return 0;
1760}
1761
1762static int dhd_open(struct net_device *net)
1763{
1764        dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1765#ifdef TOE
1766        u32 toe_ol;
1767#endif
1768        int ifidx = dhd_net2idx(dhd, net);
1769        s32 ret = 0;
1770
1771        DHD_TRACE(("%s: ifidx %d\n", __func__, ifidx));
1772
1773        if (ifidx == 0) {       /* do it only for primary eth0 */
1774
1775                /* try to bring up bus */
1776                ret = dhd_bus_start(&dhd->pub);
1777                if (ret != 0) {
1778                        DHD_ERROR(("%s: failed with code %d\n", __func__, ret));
1779                        return -1;
1780                }
1781                atomic_set(&dhd->pend_8021x_cnt, 0);
1782
1783                memcpy(net->dev_addr, dhd->pub.mac, ETH_ALEN);
1784
1785#ifdef TOE
1786                /* Get current TOE mode from dongle */
1787                if (dhd_toe_get(dhd, ifidx, &toe_ol) >= 0
1788                    && (toe_ol & TOE_TX_CSUM_OL) != 0)
1789                        dhd->iflist[ifidx]->net->features |= NETIF_F_IP_CSUM;
1790                else
1791                        dhd->iflist[ifidx]->net->features &= ~NETIF_F_IP_CSUM;
1792#endif
1793        }
1794        /* Allow transmit calls */
1795        netif_start_queue(net);
1796        dhd->pub.up = 1;
1797        if (IS_CFG80211_FAVORITE()) {
1798                if (unlikely(wl_cfg80211_up())) {
1799                        DHD_ERROR(("%s: failed to bring up cfg80211\n",
1800                                   __func__));
1801                        return -1;
1802                }
1803        }
1804
1805        return ret;
1806}
1807
1808int
1809dhd_add_if(dhd_info_t *dhd, int ifidx, void *handle, char *name,
1810           u8 *mac_addr, u32 flags, u8 bssidx)
1811{
1812        dhd_if_t *ifp;
1813
1814        DHD_TRACE(("%s: idx %d, handle->%p\n", __func__, ifidx, handle));
1815
1816        ASSERT(dhd && (ifidx < DHD_MAX_IFS));
1817
1818        ifp = dhd->iflist[ifidx];
1819        if (!ifp && !(ifp = kmalloc(sizeof(dhd_if_t), GFP_ATOMIC))) {
1820                DHD_ERROR(("%s: OOM - dhd_if_t\n", __func__));
1821                return -ENOMEM;
1822        }
1823
1824        memset(ifp, 0, sizeof(dhd_if_t));
1825        ifp->info = dhd;
1826        dhd->iflist[ifidx] = ifp;
1827        strlcpy(ifp->name, name, IFNAMSIZ);
1828        if (mac_addr != NULL)
1829                memcpy(&ifp->mac_addr, mac_addr, ETH_ALEN);
1830
1831        if (handle == NULL) {
1832                ifp->state = WLC_E_IF_ADD;
1833                ifp->idx = ifidx;
1834                ASSERT(dhd->sysioc_tsk);
1835                up(&dhd->sysioc_sem);
1836        } else
1837                ifp->net = (struct net_device *)handle;
1838
1839        return 0;
1840}
1841
1842void dhd_del_if(dhd_info_t *dhd, int ifidx)
1843{
1844        dhd_if_t *ifp;
1845
1846        DHD_TRACE(("%s: idx %d\n", __func__, ifidx));
1847
1848        ASSERT(dhd && ifidx && (ifidx < DHD_MAX_IFS));
1849        ifp = dhd->iflist[ifidx];
1850        if (!ifp) {
1851                DHD_ERROR(("%s: Null interface\n", __func__));
1852                return;
1853        }
1854
1855        ifp->state = WLC_E_IF_DEL;
1856        ifp->idx = ifidx;
1857        ASSERT(dhd->sysioc_tsk);
1858        up(&dhd->sysioc_sem);
1859}
1860
1861dhd_pub_t *dhd_attach(struct dhd_bus *bus, uint bus_hdrlen)
1862{
1863        dhd_info_t *dhd = NULL;
1864        struct net_device *net;
1865
1866        DHD_TRACE(("%s: Enter\n", __func__));
1867        /* updates firmware nvram path if it was provided as module
1868                 paramters */
1869        if ((firmware_path != NULL) && (firmware_path[0] != '\0'))
1870                strcpy(fw_path, firmware_path);
1871        if ((nvram_path != NULL) && (nvram_path[0] != '\0'))
1872                strcpy(nv_path, nvram_path);
1873
1874        /* Allocate etherdev, including space for private structure */
1875        net = alloc_etherdev(sizeof(dhd));
1876        if (!net) {
1877                DHD_ERROR(("%s: OOM - alloc_etherdev\n", __func__));
1878                goto fail;
1879        }
1880
1881        /* Allocate primary dhd_info */
1882        dhd = kzalloc(sizeof(dhd_info_t), GFP_ATOMIC);
1883        if (!dhd) {
1884                DHD_ERROR(("%s: OOM - alloc dhd_info\n", __func__));
1885                goto fail;
1886        }
1887
1888        /*
1889         * Save the dhd_info into the priv
1890         */
1891        memcpy(netdev_priv(net), &dhd, sizeof(dhd));
1892
1893        /* Set network interface name if it was provided as module parameter */
1894        if (iface_name[0]) {
1895                int len;
1896                char ch;
1897                strncpy(net->name, iface_name, IFNAMSIZ);
1898                net->name[IFNAMSIZ - 1] = 0;
1899                len = strlen(net->name);
1900                ch = net->name[len - 1];
1901                if ((ch > '9' || ch < '0') && (len < IFNAMSIZ - 2))
1902                        strcat(net->name, "%d");
1903        }
1904
1905        if (dhd_add_if(dhd, 0, (void *)net, net->name, NULL, 0, 0) ==
1906            DHD_BAD_IF)
1907                goto fail;
1908
1909        net->netdev_ops = NULL;
1910        sema_init(&dhd->proto_sem, 1);
1911        /* Initialize other structure content */
1912        init_waitqueue_head(&dhd->ioctl_resp_wait);
1913        init_waitqueue_head(&dhd->ctrl_wait);
1914
1915        /* Initialize the spinlocks */
1916        spin_lock_init(&dhd->sdlock);
1917        spin_lock_init(&dhd->txqlock);
1918
1919        /* Link to info module */
1920        dhd->pub.info = dhd;
1921
1922        /* Link to bus module */
1923        dhd->pub.bus = bus;
1924        dhd->pub.hdrlen = bus_hdrlen;
1925
1926        /* Attach and link in the protocol */
1927        if (dhd_prot_attach(&dhd->pub) != 0) {
1928                DHD_ERROR(("dhd_prot_attach failed\n"));
1929                goto fail;
1930        }
1931#if defined(CONFIG_WIRELESS_EXT)
1932        /* Attach and link in the iw */
1933        if (wl_iw_attach(net, (void *)&dhd->pub) != 0) {
1934                DHD_ERROR(("wl_iw_attach failed\n"));
1935                goto fail;
1936        }
1937#endif  /* defined(CONFIG_WIRELESS_EXT) */
1938
1939        /* Attach and link in the cfg80211 */
1940        if (IS_CFG80211_FAVORITE()) {
1941                if (unlikely(wl_cfg80211_attach(net, &dhd->pub))) {
1942                        DHD_ERROR(("wl_cfg80211_attach failed\n"));
1943                        goto fail;
1944                }
1945                if (!NO_FW_REQ()) {
1946                        strcpy(fw_path, wl_cfg80211_get_fwname());
1947                        strcpy(nv_path, wl_cfg80211_get_nvramname());
1948                }
1949        }
1950
1951        /* Set up the watchdog timer */
1952        init_timer(&dhd->timer);
1953        dhd->timer.data = (unsigned long) dhd;
1954        dhd->timer.function = dhd_watchdog;
1955
1956        /* Initialize thread based operation and lock */
1957        sema_init(&dhd->sdsem, 1);
1958        if ((dhd_watchdog_prio >= 0) && (dhd_dpc_prio >= 0))
1959                dhd->threads_only = true;
1960        else
1961                dhd->threads_only = false;
1962
1963        if (dhd_dpc_prio >= 0) {
1964                /* Initialize watchdog thread */
1965                sema_init(&dhd->watchdog_sem, 0);
1966                dhd->watchdog_tsk = kthread_run(dhd_watchdog_thread, dhd,
1967                                                "dhd_watchdog");
1968                if (IS_ERR(dhd->watchdog_tsk)) {
1969                        printk(KERN_WARNING
1970                                "dhd_watchdog thread failed to start\n");
1971                        dhd->watchdog_tsk = NULL;
1972                }
1973        } else {
1974                dhd->watchdog_tsk = NULL;
1975        }
1976
1977        /* Set up the bottom half handler */
1978        if (dhd_dpc_prio >= 0) {
1979                /* Initialize DPC thread */
1980                sema_init(&dhd->dpc_sem, 0);
1981                dhd->dpc_tsk = kthread_run(dhd_dpc_thread, dhd, "dhd_dpc");
1982                if (IS_ERR(dhd->dpc_tsk)) {
1983                        printk(KERN_WARNING
1984                                "dhd_dpc thread failed to start\n");
1985                        dhd->dpc_tsk = NULL;
1986                }
1987        } else {
1988                tasklet_init(&dhd->tasklet, dhd_dpc, (unsigned long) dhd);
1989                dhd->dpc_tsk = NULL;
1990        }
1991
1992        if (dhd_sysioc) {
1993                sema_init(&dhd->sysioc_sem, 0);
1994                dhd->sysioc_tsk = kthread_run(_dhd_sysioc_thread, dhd,
1995                                                "_dhd_sysioc");
1996                if (IS_ERR(dhd->sysioc_tsk)) {
1997                        printk(KERN_WARNING
1998                                "_dhd_sysioc thread failed to start\n");
1999                        dhd->sysioc_tsk = NULL;
2000                }
2001        } else
2002                dhd->sysioc_tsk = NULL;
2003
2004        /*
2005         * Save the dhd_info into the priv
2006         */
2007        memcpy(netdev_priv(net), &dhd, sizeof(dhd));
2008
2009#if defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC)
2010        g_bus = bus;
2011#endif
2012#if defined(CONFIG_PM_SLEEP)
2013        atomic_set(&dhd_mmc_suspend, false);
2014        if (!IS_CFG80211_FAVORITE())
2015                register_pm_notifier(&dhd_sleep_pm_notifier);
2016#endif  /* defined(CONFIG_PM_SLEEP) */
2017        /* && defined(DHD_GPL) */
2018        /* Init lock suspend to prevent kernel going to suspend */
2019#ifdef CONFIG_HAS_EARLYSUSPEND
2020        dhd->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 20;
2021        dhd->early_suspend.suspend = dhd_early_suspend;
2022        dhd->early_suspend.resume = dhd_late_resume;
2023        register_early_suspend(&dhd->early_suspend);
2024#endif
2025
2026        return &dhd->pub;
2027
2028fail:
2029        if (net)
2030                free_netdev(net);
2031        if (dhd)
2032                dhd_detach(&dhd->pub);
2033
2034        return NULL;
2035}
2036
2037int dhd_bus_start(dhd_pub_t *dhdp)
2038{
2039        int ret = -1;
2040        dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
2041#ifdef EMBEDDED_PLATFORM
2042        char iovbuf[WL_EVENTING_MASK_LEN + 12]; /*  Room for "event_msgs" +
2043                                                 '\0' + bitvec  */
2044#endif                          /* EMBEDDED_PLATFORM */
2045
2046        ASSERT(dhd);
2047
2048        DHD_TRACE(("%s:\n", __func__));
2049
2050        /* try to download image and nvram to the dongle */
2051        if (dhd->pub.busstate == DHD_BUS_DOWN) {
2052                if (!(dhd_bus_download_firmware(dhd->pub.bus,
2053                                                fw_path, nv_path))) {
2054                        DHD_ERROR(("%s: dhdsdio_probe_download failed. "
2055                                "firmware = %s nvram = %s\n",
2056                                __func__, fw_path, nv_path));
2057                        return -1;
2058                }
2059        }
2060
2061        /* Start the watchdog timer */
2062        dhd->pub.tickcnt = 0;
2063        dhd_os_wd_timer(&dhd->pub, dhd_watchdog_ms);
2064
2065        /* Bring up the bus */
2066        ret = dhd_bus_init(&dhd->pub, true);
2067        if (ret != 0) {
2068                DHD_ERROR(("%s, dhd_bus_init failed %d\n", __func__, ret));
2069                return ret;
2070        }
2071#if defined(OOB_INTR_ONLY)
2072        /* Host registration for OOB interrupt */
2073        if (bcmsdh_register_oob_intr(dhdp)) {
2074                del_timer_sync(&dhd->timer);
2075                dhd->wd_timer_valid = false;
2076                DHD_ERROR(("%s Host failed to resgister for OOB\n", __func__));
2077                return -ENODEV;
2078        }
2079
2080        /* Enable oob at firmware */
2081        dhd_enable_oob_intr(dhd->pub.bus, true);
2082#endif                          /* defined(OOB_INTR_ONLY) */
2083
2084        /* If bus is not ready, can't come up */
2085        if (dhd->pub.busstate != DHD_BUS_DATA) {
2086                del_timer_sync(&dhd->timer);
2087                dhd->wd_timer_valid = false;
2088                DHD_ERROR(("%s failed bus is not ready\n", __func__));
2089                return -ENODEV;
2090        }
2091#ifdef EMBEDDED_PLATFORM
2092        bcm_mkiovar("event_msgs", dhdp->eventmask, WL_EVENTING_MASK_LEN, iovbuf,
2093                    sizeof(iovbuf));
2094        dhdcdc_query_ioctl(dhdp, 0, WLC_GET_VAR, iovbuf, sizeof(iovbuf));
2095        memcpy(dhdp->eventmask, iovbuf, WL_EVENTING_MASK_LEN);
2096
2097        setbit(dhdp->eventmask, WLC_E_SET_SSID);
2098        setbit(dhdp->eventmask, WLC_E_PRUNE);
2099        setbit(dhdp->eventmask, WLC_E_AUTH);
2100        setbit(dhdp->eventmask, WLC_E_REASSOC);
2101        setbit(dhdp->eventmask, WLC_E_REASSOC_IND);
2102        setbit(dhdp->eventmask, WLC_E_DEAUTH_IND);
2103        setbit(dhdp->eventmask, WLC_E_DISASSOC_IND);
2104        setbit(dhdp->eventmask, WLC_E_DISASSOC);
2105        setbit(dhdp->eventmask, WLC_E_JOIN);
2106        setbit(dhdp->eventmask, WLC_E_ASSOC_IND);
2107        setbit(dhdp->eventmask, WLC_E_PSK_SUP);
2108        setbit(dhdp->eventmask, WLC_E_LINK);
2109        setbit(dhdp->eventmask, WLC_E_NDIS_LINK);
2110        setbit(dhdp->eventmask, WLC_E_MIC_ERROR);
2111        setbit(dhdp->eventmask, WLC_E_PMKID_CACHE);
2112        setbit(dhdp->eventmask, WLC_E_TXFAIL);
2113        setbit(dhdp->eventmask, WLC_E_JOIN_START);
2114        setbit(dhdp->eventmask, WLC_E_SCAN_COMPLETE);
2115#ifdef PNO_SUPPORT
2116        setbit(dhdp->eventmask, WLC_E_PFN_NET_FOUND);
2117#endif                          /* PNO_SUPPORT */
2118
2119/* enable dongle roaming event */
2120
2121        dhdp->pktfilter_count = 1;
2122        /* Setup filter to allow only unicast */
2123        dhdp->pktfilter[0] = "100 0 0 0 0x01 0x00";
2124#endif                          /* EMBEDDED_PLATFORM */
2125
2126        /* Bus is ready, do any protocol initialization */
2127        ret = dhd_prot_init(&dhd->pub);
2128        if (ret < 0)
2129                return ret;
2130
2131        return 0;
2132}
2133
2134int
2135dhd_iovar(dhd_pub_t *pub, int ifidx, char *name, char *cmd_buf, uint cmd_len,
2136          int set)
2137{
2138        char buf[strlen(name) + 1 + cmd_len];
2139        int len = sizeof(buf);
2140        wl_ioctl_t ioc;
2141        int ret;
2142
2143        len = bcm_mkiovar(name, cmd_buf, cmd_len, buf, len);
2144
2145        memset(&ioc, 0, sizeof(ioc));
2146
2147        ioc.cmd = set ? WLC_SET_VAR : WLC_GET_VAR;
2148        ioc.buf = buf;
2149        ioc.len = len;
2150        ioc.set = set;
2151
2152        ret = dhd_prot_ioctl(pub, ifidx, &ioc, ioc.buf, ioc.len);
2153        if (!set && ret >= 0)
2154                memcpy(cmd_buf, buf, cmd_len);
2155
2156        return ret;
2157}
2158
2159static struct net_device_ops dhd_ops_pri = {
2160        .ndo_open = dhd_open,
2161        .ndo_stop = dhd_stop,
2162        .ndo_get_stats = dhd_get_stats,
2163        .ndo_do_ioctl = dhd_ioctl_entry,
2164        .ndo_start_xmit = dhd_start_xmit,
2165        .ndo_set_mac_address = dhd_set_mac_address,
2166        .ndo_set_multicast_list = dhd_set_multicast_list
2167};
2168
2169int dhd_net_attach(dhd_pub_t *dhdp, int ifidx)
2170{
2171        dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
2172        struct net_device *net;
2173        u8 temp_addr[ETH_ALEN] = {
2174                0x00, 0x90, 0x4c, 0x11, 0x22, 0x33};
2175
2176        DHD_TRACE(("%s: ifidx %d\n", __func__, ifidx));
2177
2178        ASSERT(dhd && dhd->iflist[ifidx]);
2179
2180        net = dhd->iflist[ifidx]->net;
2181        ASSERT(net);
2182
2183        ASSERT(!net->netdev_ops);
2184        net->netdev_ops = &dhd_ops_pri;
2185
2186        /*
2187         * We have to use the primary MAC for virtual interfaces
2188         */
2189        if (ifidx != 0) {
2190                /* for virtual interfaces use the primary MAC  */
2191                memcpy(temp_addr, dhd->pub.mac, ETH_ALEN);
2192
2193        }
2194
2195        if (ifidx == 1) {
2196                DHD_TRACE(("%s ACCESS POINT MAC: \n", __func__));
2197                /*  ACCESSPOINT INTERFACE CASE */
2198                temp_addr[0] |= 0X02;   /* set bit 2 ,
2199                         - Locally Administered address  */
2200
2201        }
2202        net->hard_header_len = ETH_HLEN + dhd->pub.hdrlen;
2203        net->ethtool_ops = &dhd_ethtool_ops;
2204
2205        dhd->pub.rxsz = net->mtu + net->hard_header_len + dhd->pub.hdrlen;
2206
2207        memcpy(net->dev_addr, temp_addr, ETH_ALEN);
2208
2209        if (register_netdev(net) != 0) {
2210                DHD_ERROR(("%s: couldn't register the net device\n",
2211                        __func__));
2212                goto fail;
2213        }
2214
2215        DHD_INFO(("%s: Broadcom Dongle Host Driver\n", net->name));
2216
2217        return 0;
2218
2219fail:
2220        net->netdev_ops = NULL;
2221        return -EBADE;
2222}
2223
2224void dhd_bus_detach(dhd_pub_t *dhdp)
2225{
2226        dhd_info_t *dhd;
2227
2228        DHD_TRACE(("%s: Enter\n", __func__));
2229
2230        if (dhdp) {
2231                dhd = (dhd_info_t *) dhdp->info;
2232                if (dhd) {
2233                        /* Stop the protocol module */
2234                        dhd_prot_stop(&dhd->pub);
2235
2236                        /* Stop the bus module */
2237                        dhd_bus_stop(dhd->pub.bus, true);
2238#if defined(OOB_INTR_ONLY)
2239                        bcmsdh_unregister_oob_intr();
2240#endif                          /* defined(OOB_INTR_ONLY) */
2241
2242                        /* Clear the watchdog timer */
2243                        del_timer_sync(&dhd->timer);
2244                        dhd->wd_timer_valid = false;
2245                }
2246        }
2247}
2248
2249void dhd_detach(dhd_pub_t *dhdp)
2250{
2251        dhd_info_t *dhd;
2252
2253        DHD_TRACE(("%s: Enter\n", __func__));
2254
2255        if (dhdp) {
2256                dhd = (dhd_info_t *) dhdp->info;
2257                if (dhd) {
2258                        dhd_if_t *ifp;
2259                        int i;
2260
2261#if defined(CONFIG_HAS_EARLYSUSPEND)
2262                        if (dhd->early_suspend.suspend)
2263                                unregister_early_suspend(&dhd->early_suspend);
2264#endif                          /* defined(CONFIG_HAS_EARLYSUSPEND) */
2265
2266                        for (i = 1; i < DHD_MAX_IFS; i++)
2267                                if (dhd->iflist[i])
2268                                        dhd_del_if(dhd, i);
2269
2270                        ifp = dhd->iflist[0];
2271                        ASSERT(ifp);
2272                        if (ifp->net->netdev_ops == &dhd_ops_pri) {
2273                                dhd_stop(ifp->net);
2274                                unregister_netdev(ifp->net);
2275                        }
2276
2277                        if (dhd->watchdog_tsk) {
2278                                send_sig(SIGTERM, dhd->watchdog_tsk, 1);
2279                                kthread_stop(dhd->watchdog_tsk);
2280                                dhd->watchdog_tsk = NULL;
2281                        }
2282
2283                        if (dhd->dpc_tsk) {
2284                                send_sig(SIGTERM, dhd->dpc_tsk, 1);
2285                                kthread_stop(dhd->dpc_tsk);
2286                                dhd->dpc_tsk = NULL;
2287                        } else
2288                                tasklet_kill(&dhd->tasklet);
2289
2290                        if (dhd->sysioc_tsk) {
2291                                send_sig(SIGTERM, dhd->sysioc_tsk, 1);
2292                                kthread_stop(dhd->sysioc_tsk);
2293                                dhd->sysioc_tsk = NULL;
2294                        }
2295
2296                        dhd_bus_detach(dhdp);
2297
2298                        if (dhdp->prot)
2299                                dhd_prot_detach(dhdp);
2300
2301#if defined(CONFIG_WIRELESS_EXT)
2302                        wl_iw_detach();
2303#endif                          /* (CONFIG_WIRELESS_EXT) */
2304
2305                        if (IS_CFG80211_FAVORITE())
2306                                wl_cfg80211_detach();
2307
2308#if defined(CONFIG_PM_SLEEP)
2309                        if (!IS_CFG80211_FAVORITE())
2310                                unregister_pm_notifier(&dhd_sleep_pm_notifier);
2311#endif  /* defined(CONFIG_PM_SLEEP) */
2312                        /* && defined(DHD_GPL) */
2313                        free_netdev(ifp->net);
2314                        kfree(ifp);
2315                        kfree(dhd);
2316                }
2317        }
2318}
2319
2320static void __exit dhd_module_cleanup(void)
2321{
2322        DHD_TRACE(("%s: Enter\n", __func__));
2323
2324        dhd_bus_unregister();
2325#if defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC)
2326        wifi_del_dev();
2327#endif
2328        /* Call customer gpio to turn off power with WL_REG_ON signal */
2329        dhd_customer_gpio_wlan_ctrl(WLAN_POWER_OFF);
2330}
2331
2332static int __init dhd_module_init(void)
2333{
2334        int error;
2335
2336        DHD_TRACE(("%s: Enter\n", __func__));
2337
2338        /* Sanity check on the module parameters */
2339        do {
2340                /* Both watchdog and DPC as tasklets are ok */
2341                if ((dhd_watchdog_prio < 0) && (dhd_dpc_prio < 0))
2342                        break;
2343
2344                /* If both watchdog and DPC are threads, TX must be deferred */
2345                if ((dhd_watchdog_prio >= 0) && (dhd_dpc_prio >= 0)
2346                    && dhd_deferred_tx)
2347                        break;
2348
2349                DHD_ERROR(("Invalid module parameters.\n"));
2350                return -EINVAL;
2351        } while (0);
2352        /* Call customer gpio to turn on power with WL_REG_ON signal */
2353        dhd_customer_gpio_wlan_ctrl(WLAN_POWER_ON);
2354
2355#if defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC)
2356        sema_init(&wifi_control_sem, 0);
2357
2358        error = wifi_add_dev();
2359        if (error) {
2360                DHD_ERROR(("%s: platform_driver_register failed\n", __func__));
2361                goto failed;
2362        }
2363
2364        /* Waiting callback after platform_driver_register is done or
2365                 exit with error */
2366        if (down_timeout(&wifi_control_sem, msecs_to_jiffies(1000)) != 0) {
2367                printk(KERN_ERR "%s: platform_driver_register timeout\n",
2368                        __func__);
2369                /* remove device */
2370                wifi_del_dev();
2371                goto failed;
2372        }
2373#endif  /* #if defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC) */
2374
2375        error = dhd_bus_register();
2376
2377        if (error) {
2378                DHD_ERROR(("%s: sdio_register_driver failed\n", __func__));
2379                goto failed;
2380        }
2381        return error;
2382
2383failed:
2384        /* turn off power and exit */
2385        dhd_customer_gpio_wlan_ctrl(WLAN_POWER_OFF);
2386        return -EINVAL;
2387}
2388
2389module_init(dhd_module_init);
2390module_exit(dhd_module_cleanup);
2391
2392/*
2393 * OS specific functions required to implement DHD driver in OS independent way
2394 */
2395int dhd_os_proto_block(dhd_pub_t *pub)
2396{
2397        dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2398
2399        if (dhd) {
2400                down(&dhd->proto_sem);
2401                return 1;
2402        }
2403        return 0;
2404}
2405
2406int dhd_os_proto_unblock(dhd_pub_t *pub)
2407{
2408        dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2409
2410        if (dhd) {
2411                up(&dhd->proto_sem);
2412                return 1;
2413        }
2414
2415        return 0;
2416}
2417
2418unsigned int dhd_os_get_ioctl_resp_timeout(void)
2419{
2420        return (unsigned int)dhd_ioctl_timeout_msec;
2421}
2422
2423void dhd_os_set_ioctl_resp_timeout(unsigned int timeout_msec)
2424{
2425        dhd_ioctl_timeout_msec = (int)timeout_msec;
2426}
2427
2428int dhd_os_ioctl_resp_wait(dhd_pub_t *pub, uint *condition, bool *pending)
2429{
2430        dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2431        DECLARE_WAITQUEUE(wait, current);
2432        int timeout = dhd_ioctl_timeout_msec;
2433
2434        /* Convert timeout in millsecond to jiffies */
2435        timeout = timeout * HZ / 1000;
2436
2437        /* Wait until control frame is available */
2438        add_wait_queue(&dhd->ioctl_resp_wait, &wait);
2439        set_current_state(TASK_INTERRUPTIBLE);
2440
2441        while (!(*condition) && (!signal_pending(current) && timeout))
2442                timeout = schedule_timeout(timeout);
2443
2444        if (signal_pending(current))
2445                *pending = true;
2446
2447        set_current_state(TASK_RUNNING);
2448        remove_wait_queue(&dhd->ioctl_resp_wait, &wait);
2449
2450        return timeout;
2451}
2452
2453int dhd_os_ioctl_resp_wake(dhd_pub_t *pub)
2454{
2455        dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2456
2457        if (waitqueue_active(&dhd->ioctl_resp_wait))
2458                wake_up_interruptible(&dhd->ioctl_resp_wait);
2459
2460        return 0;
2461}
2462
2463void dhd_os_wd_timer(void *bus, uint wdtick)
2464{
2465        dhd_pub_t *pub = bus;
2466        static uint save_dhd_watchdog_ms;
2467        dhd_info_t *dhd = (dhd_info_t *) pub->info;
2468
2469        /* don't start the wd until fw is loaded */
2470        if (pub->busstate == DHD_BUS_DOWN)
2471                return;
2472
2473        /* Totally stop the timer */
2474        if (!wdtick && dhd->wd_timer_valid == true) {
2475                del_timer_sync(&dhd->timer);
2476                dhd->wd_timer_valid = false;
2477                save_dhd_watchdog_ms = wdtick;
2478                return;
2479        }
2480
2481        if (wdtick) {
2482                dhd_watchdog_ms = (uint) wdtick;
2483
2484                if (save_dhd_watchdog_ms != dhd_watchdog_ms) {
2485
2486                        if (dhd->wd_timer_valid == true)
2487                                /* Stop timer and restart at new value */
2488                                del_timer_sync(&dhd->timer);
2489
2490                        /* Create timer again when watchdog period is
2491                           dynamically changed or in the first instance
2492                         */
2493                        dhd->timer.expires =
2494                            jiffies + dhd_watchdog_ms * HZ / 1000;
2495                        add_timer(&dhd->timer);
2496
2497                } else {
2498                        /* Re arm the timer, at last watchdog period */
2499                        mod_timer(&dhd->timer,
2500                                  jiffies + dhd_watchdog_ms * HZ / 1000);
2501                }
2502
2503                dhd->wd_timer_valid = true;
2504                save_dhd_watchdog_ms = wdtick;
2505        }
2506}
2507
2508void *dhd_os_open_image(char *filename)
2509{
2510        struct file *fp;
2511
2512        if (IS_CFG80211_FAVORITE() && !NO_FW_REQ())
2513                return wl_cfg80211_request_fw(filename);
2514
2515        fp = filp_open(filename, O_RDONLY, 0);
2516        /*
2517         * 2.6.11 (FC4) supports filp_open() but later revs don't?
2518         * Alternative:
2519         * fp = open_namei(AT_FDCWD, filename, O_RD, 0);
2520         * ???
2521         */
2522        if (IS_ERR(fp))
2523                fp = NULL;
2524
2525        return fp;
2526}
2527
2528int dhd_os_get_image_block(char *buf, int len, void *image)
2529{
2530        struct file *fp = (struct file *)image;
2531        int rdlen;
2532
2533        if (IS_CFG80211_FAVORITE() && !NO_FW_REQ())
2534                return wl_cfg80211_read_fw(buf, len);
2535
2536        if (!image)
2537                return 0;
2538
2539        rdlen = kernel_read(fp, fp->f_pos, buf, len);
2540        if (rdlen > 0)
2541                fp->f_pos += rdlen;
2542
2543        return rdlen;
2544}
2545
2546void dhd_os_close_image(void *image)
2547{
2548        if (IS_CFG80211_FAVORITE() && !NO_FW_REQ())
2549                return wl_cfg80211_release_fw();
2550        if (image)
2551                filp_close((struct file *)image, NULL);
2552}
2553
2554void dhd_os_sdlock(dhd_pub_t *pub)
2555{
2556        dhd_info_t *dhd;
2557
2558        dhd = (dhd_info_t *) (pub->info);
2559
2560        if (dhd->threads_only)
2561                down(&dhd->sdsem);
2562        else
2563                spin_lock_bh(&dhd->sdlock);
2564}
2565
2566void dhd_os_sdunlock(dhd_pub_t *pub)
2567{
2568        dhd_info_t *dhd;
2569
2570        dhd = (dhd_info_t *) (pub->info);
2571
2572        if (dhd->threads_only)
2573                up(&dhd->sdsem);
2574        else
2575                spin_unlock_bh(&dhd->sdlock);
2576}
2577
2578void dhd_os_sdlock_txq(dhd_pub_t *pub)
2579{
2580        dhd_info_t *dhd;
2581
2582        dhd = (dhd_info_t *) (pub->info);
2583        spin_lock_bh(&dhd->txqlock);
2584}
2585
2586void dhd_os_sdunlock_txq(dhd_pub_t *pub)
2587{
2588        dhd_info_t *dhd;
2589
2590        dhd = (dhd_info_t *) (pub->info);
2591        spin_unlock_bh(&dhd->txqlock);
2592}
2593
2594void dhd_os_sdlock_rxq(dhd_pub_t *pub)
2595{
2596}
2597
2598void dhd_os_sdunlock_rxq(dhd_pub_t *pub)
2599{
2600}
2601
2602void dhd_os_sdtxlock(dhd_pub_t *pub)
2603{
2604        dhd_os_sdlock(pub);
2605}
2606
2607void dhd_os_sdtxunlock(dhd_pub_t *pub)
2608{
2609        dhd_os_sdunlock(pub);
2610}
2611
2612static int
2613dhd_wl_host_event(dhd_info_t *dhd, int *ifidx, void *pktdata,
2614                  wl_event_msg_t *event, void **data)
2615{
2616        int bcmerror = 0;
2617
2618        ASSERT(dhd != NULL);
2619
2620        bcmerror = wl_host_event(dhd, ifidx, pktdata, event, data);
2621        if (bcmerror != 0)
2622                return bcmerror;
2623
2624#if defined(CONFIG_WIRELESS_EXT)
2625        if (!IS_CFG80211_FAVORITE()) {
2626                if ((dhd->iflist[*ifidx] == NULL)
2627                    || (dhd->iflist[*ifidx]->net == NULL)) {
2628                        DHD_ERROR(("%s Exit null pointer\n", __func__));
2629                        return bcmerror;
2630                }
2631
2632                if (dhd->iflist[*ifidx]->net)
2633                        wl_iw_event(dhd->iflist[*ifidx]->net, event, *data);
2634        }
2635#endif                          /* defined(CONFIG_WIRELESS_EXT)  */
2636
2637        if (IS_CFG80211_FAVORITE()) {
2638                ASSERT(dhd->iflist[*ifidx] != NULL);
2639                ASSERT(dhd->iflist[*ifidx]->net != NULL);
2640                if (dhd->iflist[*ifidx]->net)
2641                        wl_cfg80211_event(dhd->iflist[*ifidx]->net, event,
2642                                          *data);
2643        }
2644
2645        return bcmerror;
2646}
2647
2648/* send up locally generated event */
2649void dhd_sendup_event(dhd_pub_t *dhdp, wl_event_msg_t *event, void *data)
2650{
2651        switch (be32_to_cpu(event->event_type)) {
2652        default:
2653                break;
2654        }
2655}
2656
2657void dhd_wait_for_event(dhd_pub_t *dhd, bool *lockvar)
2658{
2659        struct dhd_info *dhdinfo = dhd->info;
2660        dhd_os_sdunlock(dhd);
2661        wait_event_interruptible_timeout(dhdinfo->ctrl_wait,
2662                                         (*lockvar == false), HZ * 2);
2663        dhd_os_sdlock(dhd);
2664        return;
2665}
2666
2667void dhd_wait_event_wakeup(dhd_pub_t *dhd)
2668{
2669        struct dhd_info *dhdinfo = dhd->info;
2670        if (waitqueue_active(&dhdinfo->ctrl_wait))
2671                wake_up_interruptible(&dhdinfo->ctrl_wait);
2672        return;
2673}
2674
2675int dhd_dev_reset(struct net_device *dev, u8 flag)
2676{
2677        dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2678
2679        /* Turning off watchdog */
2680        if (flag)
2681                dhd_os_wd_timer(&dhd->pub, 0);
2682
2683        dhd_bus_devreset(&dhd->pub, flag);
2684
2685        /* Turning on watchdog back */
2686        if (!flag)
2687                dhd_os_wd_timer(&dhd->pub, dhd_watchdog_ms);
2688        DHD_ERROR(("%s:  WLAN OFF DONE\n", __func__));
2689
2690        return 1;
2691}
2692
2693int net_os_set_suspend_disable(struct net_device *dev, int val)
2694{
2695        dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2696        int ret = 0;
2697
2698        if (dhd) {
2699                ret = dhd->pub.suspend_disable_flag;
2700                dhd->pub.suspend_disable_flag = val;
2701        }
2702        return ret;
2703}
2704
2705int net_os_set_suspend(struct net_device *dev, int val)
2706{
2707        int ret = 0;
2708#if defined(CONFIG_HAS_EARLYSUSPEND)
2709        dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2710
2711        if (dhd) {
2712                dhd_os_proto_block(&dhd->pub);
2713                ret = dhd_set_suspend(val, &dhd->pub);
2714                dhd_os_proto_unblock(&dhd->pub);
2715        }
2716#endif          /* defined(CONFIG_HAS_EARLYSUSPEND) */
2717        return ret;
2718}
2719
2720int net_os_set_dtim_skip(struct net_device *dev, int val)
2721{
2722        dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
2723
2724        if (dhd)
2725                dhd->pub.dtim_skip = val;
2726
2727        return 0;
2728}
2729
2730int net_os_set_packet_filter(struct net_device *dev, int val)
2731{
2732        dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
2733        int ret = 0;
2734
2735        /* Packet filtering is set only if we still in early-suspend and
2736         * we need either to turn it ON or turn it OFF
2737         * We can always turn it OFF in case of early-suspend, but we turn it
2738         * back ON only if suspend_disable_flag was not set
2739         */
2740        if (dhd && dhd->pub.up) {
2741                dhd_os_proto_block(&dhd->pub);
2742                if (dhd->pub.in_suspend) {
2743                        if (!val || (val && !dhd->pub.suspend_disable_flag))
2744                                dhd_set_packet_filter(val, &dhd->pub);
2745                }
2746                dhd_os_proto_unblock(&dhd->pub);
2747        }
2748        return ret;
2749}
2750
2751void dhd_dev_init_ioctl(struct net_device *dev)
2752{
2753        dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2754
2755        dhd_preinit_ioctls(&dhd->pub);
2756}
2757
2758#ifdef PNO_SUPPORT
2759/* Linux wrapper to call common dhd_pno_clean */
2760int dhd_dev_pno_reset(struct net_device *dev)
2761{
2762        dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2763
2764        return dhd_pno_clean(&dhd->pub);
2765}
2766
2767/* Linux wrapper to call common dhd_pno_enable */
2768int dhd_dev_pno_enable(struct net_device *dev, int pfn_enabled)
2769{
2770        dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2771
2772        return dhd_pno_enable(&dhd->pub, pfn_enabled);
2773}
2774
2775/* Linux wrapper to call common dhd_pno_set */
2776int
2777dhd_dev_pno_set(struct net_device *dev, wlc_ssid_t *ssids_local, int nssid,
2778                unsigned char scan_fr)
2779{
2780        dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2781
2782        return dhd_pno_set(&dhd->pub, ssids_local, nssid, scan_fr);
2783}
2784
2785/* Linux wrapper to get  pno status */
2786int dhd_dev_get_pno_status(struct net_device *dev)
2787{
2788        dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2789
2790        return dhd_pno_get_status(&dhd->pub);
2791}
2792
2793#endif                          /* PNO_SUPPORT */
2794
2795static int dhd_get_pend_8021x_cnt(dhd_info_t *dhd)
2796{
2797        return atomic_read(&dhd->pend_8021x_cnt);
2798}
2799
2800#define MAX_WAIT_FOR_8021X_TX   10
2801
2802int dhd_wait_pend8021x(struct net_device *dev)
2803{
2804        dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2805        int timeout = 10 * HZ / 1000;
2806        int ntimes = MAX_WAIT_FOR_8021X_TX;
2807        int pend = dhd_get_pend_8021x_cnt(dhd);
2808
2809        while (ntimes && pend) {
2810                if (pend) {
2811                        set_current_state(TASK_INTERRUPTIBLE);
2812                        schedule_timeout(timeout);
2813                        set_current_state(TASK_RUNNING);
2814                        ntimes--;
2815                }
2816                pend = dhd_get_pend_8021x_cnt(dhd);
2817        }
2818        return pend;
2819}
2820
2821void wl_os_wd_timer(struct net_device *ndev, uint wdtick)
2822{
2823        dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(ndev);
2824
2825        dhd_os_wd_timer(&dhd->pub, wdtick);
2826}
2827
2828#ifdef DHD_DEBUG
2829int write_to_file(dhd_pub_t *dhd, u8 *buf, int size)
2830{
2831        int ret = 0;
2832        struct file *fp;
2833        mm_segment_t old_fs;
2834        loff_t pos = 0;
2835
2836        /* change to KERNEL_DS address limit */
2837        old_fs = get_fs();
2838        set_fs(KERNEL_DS);
2839
2840        /* open file to write */
2841        fp = filp_open("/tmp/mem_dump", O_WRONLY | O_CREAT, 0640);
2842        if (!fp) {
2843                DHD_ERROR(("%s: open file error\n", __func__));
2844                ret = -1;
2845                goto exit;
2846        }
2847
2848        /* Write buf to file */
2849        fp->f_op->write(fp, buf, size, &pos);
2850
2851exit:
2852        /* free buf before return */
2853        kfree(buf);
2854        /* close file before return */
2855        if (fp)
2856                filp_close(fp, current->files);
2857        /* restore previous address limit */
2858        set_fs(old_fs);
2859
2860        return ret;
2861}
2862#endif                          /* DHD_DEBUG */
2863