linux/drivers/net/wireless/p54/main.c
<<
>>
Prefs
   1/*
   2 * mac80211 glue code for mac80211 Prism54 drivers
   3 *
   4 * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
   5 * Copyright (c) 2007-2009, Christian Lamparter <chunkeey@web.de>
   6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
   7 *
   8 * Based on:
   9 * - the islsm (softmac prism54) driver, which is:
  10 *   Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
  11 * - stlc45xx driver
  12 *   Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
  13 *
  14 * This program is free software; you can redistribute it and/or modify
  15 * it under the terms of the GNU General Public License version 2 as
  16 * published by the Free Software Foundation.
  17 */
  18
  19#include <linux/init.h>
  20#include <linux/slab.h>
  21#include <linux/firmware.h>
  22#include <linux/etherdevice.h>
  23#include <linux/module.h>
  24
  25#include <net/mac80211.h>
  26
  27#include "p54.h"
  28#include "lmac.h"
  29
  30static bool modparam_nohwcrypt;
  31module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
  32MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  33MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
  34MODULE_DESCRIPTION("Softmac Prism54 common code");
  35MODULE_LICENSE("GPL");
  36MODULE_ALIAS("prism54common");
  37
  38static int p54_sta_add_remove(struct ieee80211_hw *hw,
  39                              struct ieee80211_vif *vif,
  40                              struct ieee80211_sta *sta)
  41{
  42        struct p54_common *priv = hw->priv;
  43
  44        /*
  45         * Notify the firmware that we don't want or we don't
  46         * need to buffer frames for this station anymore.
  47         */
  48
  49        p54_sta_unlock(priv, sta->addr);
  50
  51        return 0;
  52}
  53
  54static void p54_sta_notify(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
  55                              enum sta_notify_cmd notify_cmd,
  56                              struct ieee80211_sta *sta)
  57{
  58        struct p54_common *priv = dev->priv;
  59
  60        switch (notify_cmd) {
  61        case STA_NOTIFY_AWAKE:
  62                /* update the firmware's filter table */
  63                p54_sta_unlock(priv, sta->addr);
  64                break;
  65        default:
  66                break;
  67        }
  68}
  69
  70static int p54_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
  71                        bool set)
  72{
  73        struct p54_common *priv = dev->priv;
  74
  75        return p54_update_beacon_tim(priv, sta->aid, set);
  76}
  77
  78u8 *p54_find_ie(struct sk_buff *skb, u8 ie)
  79{
  80        struct ieee80211_mgmt *mgmt = (void *)skb->data;
  81        u8 *pos, *end;
  82
  83        if (skb->len <= sizeof(mgmt))
  84                return NULL;
  85
  86        pos = (u8 *)mgmt->u.beacon.variable;
  87        end = skb->data + skb->len;
  88        while (pos < end) {
  89                if (pos + 2 + pos[1] > end)
  90                        return NULL;
  91
  92                if (pos[0] == ie)
  93                        return pos;
  94
  95                pos += 2 + pos[1];
  96        }
  97        return NULL;
  98}
  99
 100static int p54_beacon_format_ie_tim(struct sk_buff *skb)
 101{
 102        /*
 103         * the good excuse for this mess is ... the firmware.
 104         * The dummy TIM MUST be at the end of the beacon frame,
 105         * because it'll be overwritten!
 106         */
 107        u8 *tim;
 108        u8 dtim_len;
 109        u8 dtim_period;
 110        u8 *next;
 111
 112        tim = p54_find_ie(skb, WLAN_EID_TIM);
 113        if (!tim)
 114                return 0;
 115
 116        dtim_len = tim[1];
 117        dtim_period = tim[3];
 118        next = tim + 2 + dtim_len;
 119
 120        if (dtim_len < 3)
 121                return -EINVAL;
 122
 123        memmove(tim, next, skb_tail_pointer(skb) - next);
 124        tim = skb_tail_pointer(skb) - (dtim_len + 2);
 125
 126        /* add the dummy at the end */
 127        tim[0] = WLAN_EID_TIM;
 128        tim[1] = 3;
 129        tim[2] = 0;
 130        tim[3] = dtim_period;
 131        tim[4] = 0;
 132
 133        if (dtim_len > 3)
 134                skb_trim(skb, skb->len - (dtim_len - 3));
 135
 136        return 0;
 137}
 138
 139static int p54_beacon_update(struct p54_common *priv,
 140                        struct ieee80211_vif *vif)
 141{
 142        struct ieee80211_tx_control control = { };
 143        struct sk_buff *beacon;
 144        int ret;
 145
 146        beacon = ieee80211_beacon_get(priv->hw, vif);
 147        if (!beacon)
 148                return -ENOMEM;
 149        ret = p54_beacon_format_ie_tim(beacon);
 150        if (ret)
 151                return ret;
 152
 153        /*
 154         * During operation, the firmware takes care of beaconing.
 155         * The driver only needs to upload a new beacon template, once
 156         * the template was changed by the stack or userspace.
 157         *
 158         * LMAC API 3.2.2 also specifies that the driver does not need
 159         * to cancel the old beacon template by hand, instead the firmware
 160         * will release the previous one through the feedback mechanism.
 161         */
 162        p54_tx_80211(priv->hw, &control, beacon);
 163        priv->tsf_high32 = 0;
 164        priv->tsf_low32 = 0;
 165
 166        return 0;
 167}
 168
 169static int p54_start(struct ieee80211_hw *dev)
 170{
 171        struct p54_common *priv = dev->priv;
 172        int err;
 173
 174        mutex_lock(&priv->conf_mutex);
 175        err = priv->open(dev);
 176        if (err)
 177                goto out;
 178        P54_SET_QUEUE(priv->qos_params[0], 0x0002, 0x0003, 0x0007, 47);
 179        P54_SET_QUEUE(priv->qos_params[1], 0x0002, 0x0007, 0x000f, 94);
 180        P54_SET_QUEUE(priv->qos_params[2], 0x0003, 0x000f, 0x03ff, 0);
 181        P54_SET_QUEUE(priv->qos_params[3], 0x0007, 0x000f, 0x03ff, 0);
 182        err = p54_set_edcf(priv);
 183        if (err)
 184                goto out;
 185
 186        memset(priv->bssid, ~0, ETH_ALEN);
 187        priv->mode = NL80211_IFTYPE_MONITOR;
 188        err = p54_setup_mac(priv);
 189        if (err) {
 190                priv->mode = NL80211_IFTYPE_UNSPECIFIED;
 191                goto out;
 192        }
 193
 194        ieee80211_queue_delayed_work(dev, &priv->work, 0);
 195
 196        priv->softled_state = 0;
 197        err = p54_set_leds(priv);
 198
 199out:
 200        mutex_unlock(&priv->conf_mutex);
 201        return err;
 202}
 203
 204static void p54_stop(struct ieee80211_hw *dev)
 205{
 206        struct p54_common *priv = dev->priv;
 207        int i;
 208
 209        priv->mode = NL80211_IFTYPE_UNSPECIFIED;
 210        priv->softled_state = 0;
 211        cancel_delayed_work_sync(&priv->work);
 212        mutex_lock(&priv->conf_mutex);
 213        p54_set_leds(priv);
 214        priv->stop(dev);
 215        skb_queue_purge(&priv->tx_pending);
 216        skb_queue_purge(&priv->tx_queue);
 217        for (i = 0; i < P54_QUEUE_NUM; i++) {
 218                priv->tx_stats[i].count = 0;
 219                priv->tx_stats[i].len = 0;
 220        }
 221
 222        priv->beacon_req_id = cpu_to_le32(0);
 223        priv->tsf_high32 = priv->tsf_low32 = 0;
 224        mutex_unlock(&priv->conf_mutex);
 225}
 226
 227static int p54_add_interface(struct ieee80211_hw *dev,
 228                             struct ieee80211_vif *vif)
 229{
 230        struct p54_common *priv = dev->priv;
 231        int err;
 232
 233        vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
 234
 235        mutex_lock(&priv->conf_mutex);
 236        if (priv->mode != NL80211_IFTYPE_MONITOR) {
 237                mutex_unlock(&priv->conf_mutex);
 238                return -EOPNOTSUPP;
 239        }
 240
 241        priv->vif = vif;
 242
 243        switch (vif->type) {
 244        case NL80211_IFTYPE_STATION:
 245        case NL80211_IFTYPE_ADHOC:
 246        case NL80211_IFTYPE_AP:
 247        case NL80211_IFTYPE_MESH_POINT:
 248                priv->mode = vif->type;
 249                break;
 250        default:
 251                mutex_unlock(&priv->conf_mutex);
 252                return -EOPNOTSUPP;
 253        }
 254
 255        memcpy(priv->mac_addr, vif->addr, ETH_ALEN);
 256        err = p54_setup_mac(priv);
 257        mutex_unlock(&priv->conf_mutex);
 258        return err;
 259}
 260
 261static void p54_remove_interface(struct ieee80211_hw *dev,
 262                                 struct ieee80211_vif *vif)
 263{
 264        struct p54_common *priv = dev->priv;
 265
 266        mutex_lock(&priv->conf_mutex);
 267        priv->vif = NULL;
 268
 269        /*
 270         * LMAC API 3.2.2 states that any active beacon template must be
 271         * canceled by the driver before attempting a mode transition.
 272         */
 273        if (le32_to_cpu(priv->beacon_req_id) != 0) {
 274                p54_tx_cancel(priv, priv->beacon_req_id);
 275                wait_for_completion_interruptible_timeout(&priv->beacon_comp, HZ);
 276        }
 277        priv->mode = NL80211_IFTYPE_MONITOR;
 278        memset(priv->mac_addr, 0, ETH_ALEN);
 279        memset(priv->bssid, 0, ETH_ALEN);
 280        p54_setup_mac(priv);
 281        mutex_unlock(&priv->conf_mutex);
 282}
 283
 284static int p54_wait_for_stats(struct ieee80211_hw *dev)
 285{
 286        struct p54_common *priv = dev->priv;
 287        int ret;
 288
 289        priv->update_stats = true;
 290        ret = p54_fetch_statistics(priv);
 291        if (ret)
 292                return ret;
 293
 294        ret = wait_for_completion_interruptible_timeout(&priv->stat_comp, HZ);
 295        if (ret == 0)
 296                return -ETIMEDOUT;
 297
 298        return 0;
 299}
 300
 301static void p54_reset_stats(struct p54_common *priv)
 302{
 303        struct ieee80211_channel *chan = priv->curchan;
 304
 305        if (chan) {
 306                struct survey_info *info = &priv->survey[chan->hw_value];
 307
 308                /* only reset channel statistics, don't touch .filled, etc. */
 309                info->channel_time = 0;
 310                info->channel_time_busy = 0;
 311                info->channel_time_tx = 0;
 312        }
 313
 314        priv->update_stats = true;
 315        priv->survey_raw.active = 0;
 316        priv->survey_raw.cca = 0;
 317        priv->survey_raw.tx = 0;
 318}
 319
 320static int p54_config(struct ieee80211_hw *dev, u32 changed)
 321{
 322        int ret = 0;
 323        struct p54_common *priv = dev->priv;
 324        struct ieee80211_conf *conf = &dev->conf;
 325
 326        mutex_lock(&priv->conf_mutex);
 327        if (changed & IEEE80211_CONF_CHANGE_POWER)
 328                priv->output_power = conf->power_level << 2;
 329        if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
 330                struct ieee80211_channel *oldchan;
 331                WARN_ON(p54_wait_for_stats(dev));
 332                oldchan = priv->curchan;
 333                priv->curchan = NULL;
 334                ret = p54_scan(priv, P54_SCAN_EXIT, 0);
 335                if (ret) {
 336                        priv->curchan = oldchan;
 337                        goto out;
 338                }
 339                /*
 340                 * TODO: Use the LM_SCAN_TRAP to determine the current
 341                 * operating channel.
 342                 */
 343                priv->curchan = priv->hw->conf.chandef.chan;
 344                p54_reset_stats(priv);
 345                WARN_ON(p54_fetch_statistics(priv));
 346        }
 347        if (changed & IEEE80211_CONF_CHANGE_PS) {
 348                WARN_ON(p54_wait_for_stats(dev));
 349                ret = p54_set_ps(priv);
 350                if (ret)
 351                        goto out;
 352                WARN_ON(p54_wait_for_stats(dev));
 353        }
 354        if (changed & IEEE80211_CONF_CHANGE_IDLE) {
 355                WARN_ON(p54_wait_for_stats(dev));
 356                ret = p54_setup_mac(priv);
 357                if (ret)
 358                        goto out;
 359                WARN_ON(p54_wait_for_stats(dev));
 360        }
 361
 362out:
 363        mutex_unlock(&priv->conf_mutex);
 364        return ret;
 365}
 366
 367static u64 p54_prepare_multicast(struct ieee80211_hw *dev,
 368                                 struct netdev_hw_addr_list *mc_list)
 369{
 370        struct p54_common *priv = dev->priv;
 371        struct netdev_hw_addr *ha;
 372        int i;
 373
 374        BUILD_BUG_ON(ARRAY_SIZE(priv->mc_maclist) !=
 375                ARRAY_SIZE(((struct p54_group_address_table *)NULL)->mac_list));
 376        /*
 377         * The first entry is reserved for the global broadcast MAC.
 378         * Otherwise the firmware will drop it and ARP will no longer work.
 379         */
 380        i = 1;
 381        priv->mc_maclist_num = netdev_hw_addr_list_count(mc_list) + i;
 382        netdev_hw_addr_list_for_each(ha, mc_list) {
 383                memcpy(&priv->mc_maclist[i], ha->addr, ETH_ALEN);
 384                i++;
 385                if (i >= ARRAY_SIZE(priv->mc_maclist))
 386                        break;
 387        }
 388
 389        return 1; /* update */
 390}
 391
 392static void p54_configure_filter(struct ieee80211_hw *dev,
 393                                 unsigned int changed_flags,
 394                                 unsigned int *total_flags,
 395                                 u64 multicast)
 396{
 397        struct p54_common *priv = dev->priv;
 398
 399        *total_flags &= FIF_PROMISC_IN_BSS |
 400                        FIF_ALLMULTI |
 401                        FIF_OTHER_BSS;
 402
 403        priv->filter_flags = *total_flags;
 404
 405        if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS))
 406                p54_setup_mac(priv);
 407
 408        if (changed_flags & FIF_ALLMULTI || multicast)
 409                p54_set_groupfilter(priv);
 410}
 411
 412static int p54_conf_tx(struct ieee80211_hw *dev,
 413                       struct ieee80211_vif *vif, u16 queue,
 414                       const struct ieee80211_tx_queue_params *params)
 415{
 416        struct p54_common *priv = dev->priv;
 417        int ret;
 418
 419        mutex_lock(&priv->conf_mutex);
 420        if (queue < dev->queues) {
 421                P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
 422                        params->cw_min, params->cw_max, params->txop);
 423                ret = p54_set_edcf(priv);
 424        } else
 425                ret = -EINVAL;
 426        mutex_unlock(&priv->conf_mutex);
 427        return ret;
 428}
 429
 430static void p54_work(struct work_struct *work)
 431{
 432        struct p54_common *priv = container_of(work, struct p54_common,
 433                                               work.work);
 434
 435        if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
 436                return ;
 437
 438        /*
 439         * TODO: walk through tx_queue and do the following tasks
 440         *      1. initiate bursts.
 441         *      2. cancel stuck frames / reset the device if necessary.
 442         */
 443
 444        mutex_lock(&priv->conf_mutex);
 445        WARN_ON_ONCE(p54_fetch_statistics(priv));
 446        mutex_unlock(&priv->conf_mutex);
 447}
 448
 449static int p54_get_stats(struct ieee80211_hw *dev,
 450                         struct ieee80211_low_level_stats *stats)
 451{
 452        struct p54_common *priv = dev->priv;
 453
 454        memcpy(stats, &priv->stats, sizeof(*stats));
 455        return 0;
 456}
 457
 458static void p54_bss_info_changed(struct ieee80211_hw *dev,
 459                                 struct ieee80211_vif *vif,
 460                                 struct ieee80211_bss_conf *info,
 461                                 u32 changed)
 462{
 463        struct p54_common *priv = dev->priv;
 464
 465        mutex_lock(&priv->conf_mutex);
 466        if (changed & BSS_CHANGED_BSSID) {
 467                memcpy(priv->bssid, info->bssid, ETH_ALEN);
 468                p54_setup_mac(priv);
 469        }
 470
 471        if (changed & BSS_CHANGED_BEACON) {
 472                p54_scan(priv, P54_SCAN_EXIT, 0);
 473                p54_setup_mac(priv);
 474                p54_beacon_update(priv, vif);
 475                p54_set_edcf(priv);
 476        }
 477
 478        if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_BEACON)) {
 479                priv->use_short_slot = info->use_short_slot;
 480                p54_set_edcf(priv);
 481        }
 482        if (changed & BSS_CHANGED_BASIC_RATES) {
 483                if (dev->conf.chandef.chan->band == IEEE80211_BAND_5GHZ)
 484                        priv->basic_rate_mask = (info->basic_rates << 4);
 485                else
 486                        priv->basic_rate_mask = info->basic_rates;
 487                p54_setup_mac(priv);
 488                if (priv->fw_var >= 0x500)
 489                        p54_scan(priv, P54_SCAN_EXIT, 0);
 490        }
 491        if (changed & BSS_CHANGED_ASSOC) {
 492                if (info->assoc) {
 493                        priv->aid = info->aid;
 494                        priv->wakeup_timer = info->beacon_int *
 495                                             info->dtim_period * 5;
 496                        p54_setup_mac(priv);
 497                } else {
 498                        priv->wakeup_timer = 500;
 499                        priv->aid = 0;
 500                }
 501        }
 502
 503        mutex_unlock(&priv->conf_mutex);
 504}
 505
 506static int p54_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd,
 507                       struct ieee80211_vif *vif, struct ieee80211_sta *sta,
 508                       struct ieee80211_key_conf *key)
 509{
 510        struct p54_common *priv = dev->priv;
 511        int slot, ret = 0;
 512        u8 algo = 0;
 513        u8 *addr = NULL;
 514
 515        if (modparam_nohwcrypt)
 516                return -EOPNOTSUPP;
 517
 518        if (key->flags & IEEE80211_KEY_FLAG_RX_MGMT) {
 519                /*
 520                 * Unfortunately most/all firmwares are trying to decrypt
 521                 * incoming management frames if a suitable key can be found.
 522                 * However, in doing so the data in these frames gets
 523                 * corrupted. So, we can't have firmware supported crypto
 524                 * offload in this case.
 525                 */
 526                return -EOPNOTSUPP;
 527        }
 528
 529        mutex_lock(&priv->conf_mutex);
 530        if (cmd == SET_KEY) {
 531                switch (key->cipher) {
 532                case WLAN_CIPHER_SUITE_TKIP:
 533                        if (!(priv->privacy_caps & (BR_DESC_PRIV_CAP_MICHAEL |
 534                              BR_DESC_PRIV_CAP_TKIP))) {
 535                                ret = -EOPNOTSUPP;
 536                                goto out_unlock;
 537                        }
 538                        key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
 539                        algo = P54_CRYPTO_TKIPMICHAEL;
 540                        break;
 541                case WLAN_CIPHER_SUITE_WEP40:
 542                case WLAN_CIPHER_SUITE_WEP104:
 543                        if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_WEP)) {
 544                                ret = -EOPNOTSUPP;
 545                                goto out_unlock;
 546                        }
 547                        key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
 548                        algo = P54_CRYPTO_WEP;
 549                        break;
 550                case WLAN_CIPHER_SUITE_CCMP:
 551                        if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP)) {
 552                                ret = -EOPNOTSUPP;
 553                                goto out_unlock;
 554                        }
 555                        key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
 556                        algo = P54_CRYPTO_AESCCMP;
 557                        break;
 558                default:
 559                        ret = -EOPNOTSUPP;
 560                        goto out_unlock;
 561                }
 562                slot = bitmap_find_free_region(priv->used_rxkeys,
 563                                               priv->rx_keycache_size, 0);
 564
 565                if (slot < 0) {
 566                        /*
 567                         * The device supports the chosen algorithm, but the
 568                         * firmware does not provide enough key slots to store
 569                         * all of them.
 570                         * But encryption offload for outgoing frames is always
 571                         * possible, so we just pretend that the upload was
 572                         * successful and do the decryption in software.
 573                         */
 574
 575                        /* mark the key as invalid. */
 576                        key->hw_key_idx = 0xff;
 577                        goto out_unlock;
 578                }
 579        } else {
 580                slot = key->hw_key_idx;
 581
 582                if (slot == 0xff) {
 583                        /* This key was not uploaded into the rx key cache. */
 584
 585                        goto out_unlock;
 586                }
 587
 588                bitmap_release_region(priv->used_rxkeys, slot, 0);
 589                algo = 0;
 590        }
 591
 592        if (sta)
 593                addr = sta->addr;
 594
 595        ret = p54_upload_key(priv, algo, slot, key->keyidx,
 596                             key->keylen, addr, key->key);
 597        if (ret) {
 598                bitmap_release_region(priv->used_rxkeys, slot, 0);
 599                ret = -EOPNOTSUPP;
 600                goto out_unlock;
 601        }
 602
 603        key->hw_key_idx = slot;
 604
 605out_unlock:
 606        mutex_unlock(&priv->conf_mutex);
 607        return ret;
 608}
 609
 610static int p54_get_survey(struct ieee80211_hw *dev, int idx,
 611                                struct survey_info *survey)
 612{
 613        struct p54_common *priv = dev->priv;
 614        struct ieee80211_channel *chan;
 615        int err, tries;
 616        bool in_use = false;
 617
 618        if (idx >= priv->chan_num)
 619                return -ENOENT;
 620
 621#define MAX_TRIES 1
 622        for (tries = 0; tries < MAX_TRIES; tries++) {
 623                chan = priv->curchan;
 624                if (chan && chan->hw_value == idx) {
 625                        mutex_lock(&priv->conf_mutex);
 626                        err = p54_wait_for_stats(dev);
 627                        mutex_unlock(&priv->conf_mutex);
 628                        if (err)
 629                                return err;
 630
 631                        in_use = true;
 632                }
 633
 634                memcpy(survey, &priv->survey[idx], sizeof(*survey));
 635
 636                if (in_use) {
 637                        /* test if the reported statistics are valid. */
 638                        if  (survey->channel_time != 0) {
 639                                survey->filled |= SURVEY_INFO_IN_USE;
 640                        } else {
 641                                /*
 642                                 * hw/fw has not accumulated enough sample sets.
 643                                 * Wait for 100ms, this ought to be enough to
 644                                 * to get at least one non-null set of channel
 645                                 * usage statistics.
 646                                 */
 647                                msleep(100);
 648                                continue;
 649                        }
 650                }
 651                return 0;
 652        }
 653        return -ETIMEDOUT;
 654#undef MAX_TRIES
 655}
 656
 657static unsigned int p54_flush_count(struct p54_common *priv)
 658{
 659        unsigned int total = 0, i;
 660
 661        BUILD_BUG_ON(P54_QUEUE_NUM > ARRAY_SIZE(priv->tx_stats));
 662
 663        /*
 664         * Because the firmware has the sole control over any frames
 665         * in the P54_QUEUE_BEACON or P54_QUEUE_SCAN queues, they
 666         * don't really count as pending or active.
 667         */
 668        for (i = P54_QUEUE_MGMT; i < P54_QUEUE_NUM; i++)
 669                total += priv->tx_stats[i].len;
 670        return total;
 671}
 672
 673static void p54_flush(struct ieee80211_hw *dev, u32 queues, bool drop)
 674{
 675        struct p54_common *priv = dev->priv;
 676        unsigned int total, i;
 677
 678        /*
 679         * Currently, it wouldn't really matter if we wait for one second
 680         * or 15 minutes. But once someone gets around and completes the
 681         * TODOs [ancel stuck frames / reset device] in p54_work, it will
 682         * suddenly make sense to wait that long.
 683         */
 684        i = P54_STATISTICS_UPDATE * 2 / 20;
 685
 686        /*
 687         * In this case no locking is required because as we speak the
 688         * queues have already been stopped and no new frames can sneak
 689         * up from behind.
 690         */
 691        while ((total = p54_flush_count(priv) && i--)) {
 692                /* waste time */
 693                msleep(20);
 694        }
 695
 696        WARN(total, "tx flush timeout, unresponsive firmware");
 697}
 698
 699static void p54_set_coverage_class(struct ieee80211_hw *dev, u8 coverage_class)
 700{
 701        struct p54_common *priv = dev->priv;
 702
 703        mutex_lock(&priv->conf_mutex);
 704        /* support all coverage class values as in 802.11-2007 Table 7-27 */
 705        priv->coverage_class = clamp_t(u8, coverage_class, 0, 31);
 706        p54_set_edcf(priv);
 707        mutex_unlock(&priv->conf_mutex);
 708}
 709
 710static const struct ieee80211_ops p54_ops = {
 711        .tx                     = p54_tx_80211,
 712        .start                  = p54_start,
 713        .stop                   = p54_stop,
 714        .add_interface          = p54_add_interface,
 715        .remove_interface       = p54_remove_interface,
 716        .set_tim                = p54_set_tim,
 717        .sta_notify             = p54_sta_notify,
 718        .sta_add                = p54_sta_add_remove,
 719        .sta_remove             = p54_sta_add_remove,
 720        .set_key                = p54_set_key,
 721        .config                 = p54_config,
 722        .flush                  = p54_flush,
 723        .bss_info_changed       = p54_bss_info_changed,
 724        .prepare_multicast      = p54_prepare_multicast,
 725        .configure_filter       = p54_configure_filter,
 726        .conf_tx                = p54_conf_tx,
 727        .get_stats              = p54_get_stats,
 728        .get_survey             = p54_get_survey,
 729        .set_coverage_class     = p54_set_coverage_class,
 730};
 731
 732struct ieee80211_hw *p54_init_common(size_t priv_data_len)
 733{
 734        struct ieee80211_hw *dev;
 735        struct p54_common *priv;
 736
 737        dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
 738        if (!dev)
 739                return NULL;
 740
 741        priv = dev->priv;
 742        priv->hw = dev;
 743        priv->mode = NL80211_IFTYPE_UNSPECIFIED;
 744        priv->basic_rate_mask = 0x15f;
 745        spin_lock_init(&priv->tx_stats_lock);
 746        skb_queue_head_init(&priv->tx_queue);
 747        skb_queue_head_init(&priv->tx_pending);
 748        dev->flags = IEEE80211_HW_RX_INCLUDES_FCS |
 749                     IEEE80211_HW_SIGNAL_DBM |
 750                     IEEE80211_HW_SUPPORTS_PS |
 751                     IEEE80211_HW_PS_NULLFUNC_STACK |
 752                     IEEE80211_HW_MFP_CAPABLE |
 753                     IEEE80211_HW_REPORTS_TX_ACK_STATUS;
 754
 755        dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
 756                                      BIT(NL80211_IFTYPE_ADHOC) |
 757                                      BIT(NL80211_IFTYPE_AP) |
 758                                      BIT(NL80211_IFTYPE_MESH_POINT);
 759
 760        dev->channel_change_time = 1000;        /* TODO: find actual value */
 761        priv->beacon_req_id = cpu_to_le32(0);
 762        priv->tx_stats[P54_QUEUE_BEACON].limit = 1;
 763        priv->tx_stats[P54_QUEUE_FWSCAN].limit = 1;
 764        priv->tx_stats[P54_QUEUE_MGMT].limit = 3;
 765        priv->tx_stats[P54_QUEUE_CAB].limit = 3;
 766        priv->tx_stats[P54_QUEUE_DATA].limit = 5;
 767        dev->queues = 1;
 768        priv->noise = -94;
 769        /*
 770         * We support at most 8 tries no matter which rate they're at,
 771         * we cannot support max_rates * max_rate_tries as we set it
 772         * here, but setting it correctly to 4/2 or so would limit us
 773         * artificially if the RC algorithm wants just two rates, so
 774         * let's say 4/7, we'll redistribute it at TX time, see the
 775         * comments there.
 776         */
 777        dev->max_rates = 4;
 778        dev->max_rate_tries = 7;
 779        dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
 780                                 sizeof(struct p54_tx_data);
 781
 782        /*
 783         * For now, disable PS by default because it affects
 784         * link stability significantly.
 785         */
 786        dev->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
 787
 788        mutex_init(&priv->conf_mutex);
 789        mutex_init(&priv->eeprom_mutex);
 790        init_completion(&priv->stat_comp);
 791        init_completion(&priv->eeprom_comp);
 792        init_completion(&priv->beacon_comp);
 793        INIT_DELAYED_WORK(&priv->work, p54_work);
 794
 795        memset(&priv->mc_maclist[0], ~0, ETH_ALEN);
 796        priv->curchan = NULL;
 797        p54_reset_stats(priv);
 798        return dev;
 799}
 800EXPORT_SYMBOL_GPL(p54_init_common);
 801
 802int p54_register_common(struct ieee80211_hw *dev, struct device *pdev)
 803{
 804        struct p54_common __maybe_unused *priv = dev->priv;
 805        int err;
 806
 807        err = ieee80211_register_hw(dev);
 808        if (err) {
 809                dev_err(pdev, "Cannot register device (%d).\n", err);
 810                return err;
 811        }
 812        priv->registered = true;
 813
 814#ifdef CONFIG_P54_LEDS
 815        err = p54_init_leds(priv);
 816        if (err) {
 817                p54_unregister_common(dev);
 818                return err;
 819        }
 820#endif /* CONFIG_P54_LEDS */
 821
 822        dev_info(pdev, "is registered as '%s'\n", wiphy_name(dev->wiphy));
 823        return 0;
 824}
 825EXPORT_SYMBOL_GPL(p54_register_common);
 826
 827void p54_free_common(struct ieee80211_hw *dev)
 828{
 829        struct p54_common *priv = dev->priv;
 830        unsigned int i;
 831
 832        for (i = 0; i < IEEE80211_NUM_BANDS; i++)
 833                kfree(priv->band_table[i]);
 834
 835        kfree(priv->iq_autocal);
 836        kfree(priv->output_limit);
 837        kfree(priv->curve_data);
 838        kfree(priv->rssi_db);
 839        kfree(priv->used_rxkeys);
 840        kfree(priv->survey);
 841        priv->iq_autocal = NULL;
 842        priv->output_limit = NULL;
 843        priv->curve_data = NULL;
 844        priv->rssi_db = NULL;
 845        priv->used_rxkeys = NULL;
 846        priv->survey = NULL;
 847        ieee80211_free_hw(dev);
 848}
 849EXPORT_SYMBOL_GPL(p54_free_common);
 850
 851void p54_unregister_common(struct ieee80211_hw *dev)
 852{
 853        struct p54_common *priv = dev->priv;
 854
 855#ifdef CONFIG_P54_LEDS
 856        p54_unregister_leds(priv);
 857#endif /* CONFIG_P54_LEDS */
 858
 859        if (priv->registered) {
 860                priv->registered = false;
 861                ieee80211_unregister_hw(dev);
 862        }
 863
 864        mutex_destroy(&priv->conf_mutex);
 865        mutex_destroy(&priv->eeprom_mutex);
 866}
 867EXPORT_SYMBOL_GPL(p54_unregister_common);
 868