linux/drivers/staging/wlan-ng/hfa384x_usb.c
<<
>>
Prefs
   1// SPDX-License-Identifier: (GPL-2.0 OR MPL-1.1)
   2/* src/prism2/driver/hfa384x_usb.c
   3 *
   4 * Functions that talk to the USB variant of the Intersil hfa384x MAC
   5 *
   6 * Copyright (C) 1999 AbsoluteValue Systems, Inc.  All Rights Reserved.
   7 * --------------------------------------------------------------------
   8 *
   9 * linux-wlan
  10 *
  11 *   The contents of this file are subject to the Mozilla Public
  12 *   License Version 1.1 (the "License"); you may not use this file
  13 *   except in compliance with the License. You may obtain a copy of
  14 *   the License at http://www.mozilla.org/MPL/
  15 *
  16 *   Software distributed under the License is distributed on an "AS
  17 *   IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  18 *   implied. See the License for the specific language governing
  19 *   rights and limitations under the License.
  20 *
  21 *   Alternatively, the contents of this file may be used under the
  22 *   terms of the GNU Public License version 2 (the "GPL"), in which
  23 *   case the provisions of the GPL are applicable instead of the
  24 *   above.  If you wish to allow the use of your version of this file
  25 *   only under the terms of the GPL and not to allow others to use
  26 *   your version of this file under the MPL, indicate your decision
  27 *   by deleting the provisions above and replace them with the notice
  28 *   and other provisions required by the GPL.  If you do not delete
  29 *   the provisions above, a recipient may use your version of this
  30 *   file under either the MPL or the GPL.
  31 *
  32 * --------------------------------------------------------------------
  33 *
  34 * Inquiries regarding the linux-wlan Open Source project can be
  35 * made directly to:
  36 *
  37 * AbsoluteValue Systems Inc.
  38 * info@linux-wlan.com
  39 * http://www.linux-wlan.com
  40 *
  41 * --------------------------------------------------------------------
  42 *
  43 * Portions of the development of this software were funded by
  44 * Intersil Corporation as part of PRISM(R) chipset product development.
  45 *
  46 * --------------------------------------------------------------------
  47 *
  48 * This file implements functions that correspond to the prism2/hfa384x
  49 * 802.11 MAC hardware and firmware host interface.
  50 *
  51 * The functions can be considered to represent several levels of
  52 * abstraction.  The lowest level functions are simply C-callable wrappers
  53 * around the register accesses.  The next higher level represents C-callable
  54 * prism2 API functions that match the Intersil documentation as closely
  55 * as is reasonable.  The next higher layer implements common sequences
  56 * of invocations of the API layer (e.g. write to bap, followed by cmd).
  57 *
  58 * Common sequences:
  59 * hfa384x_drvr_xxx     Highest level abstractions provided by the
  60 *                      hfa384x code.  They are driver defined wrappers
  61 *                      for common sequences.  These functions generally
  62 *                      use the services of the lower levels.
  63 *
  64 * hfa384x_drvr_xxxconfig  An example of the drvr level abstraction. These
  65 *                      functions are wrappers for the RID get/set
  66 *                      sequence. They call copy_[to|from]_bap() and
  67 *                      cmd_access(). These functions operate on the
  68 *                      RIDs and buffers without validation. The caller
  69 *                      is responsible for that.
  70 *
  71 * API wrapper functions:
  72 * hfa384x_cmd_xxx      functions that provide access to the f/w commands.
  73 *                      The function arguments correspond to each command
  74 *                      argument, even command arguments that get packed
  75 *                      into single registers.  These functions _just_
  76 *                      issue the command by setting the cmd/parm regs
  77 *                      & reading the status/resp regs.  Additional
  78 *                      activities required to fully use a command
  79 *                      (read/write from/to bap, get/set int status etc.)
  80 *                      are implemented separately.  Think of these as
  81 *                      C-callable prism2 commands.
  82 *
  83 * Lowest Layer Functions:
  84 * hfa384x_docmd_xxx    These functions implement the sequence required
  85 *                      to issue any prism2 command.  Primarily used by the
  86 *                      hfa384x_cmd_xxx functions.
  87 *
  88 * hfa384x_bap_xxx      BAP read/write access functions.
  89 *                      Note: we usually use BAP0 for non-interrupt context
  90 *                       and BAP1 for interrupt context.
  91 *
  92 * hfa384x_dl_xxx       download related functions.
  93 *
  94 * Driver State Issues:
  95 * Note that there are two pairs of functions that manage the
  96 * 'initialized' and 'running' states of the hw/MAC combo.  The four
  97 * functions are create(), destroy(), start(), and stop().  create()
  98 * sets up the data structures required to support the hfa384x_*
  99 * functions and destroy() cleans them up.  The start() function gets
 100 * the actual hardware running and enables the interrupts.  The stop()
 101 * function shuts the hardware down.  The sequence should be:
 102 * create()
 103 * start()
 104 *  .
 105 *  .  Do interesting things w/ the hardware
 106 *  .
 107 * stop()
 108 * destroy()
 109 *
 110 * Note that destroy() can be called without calling stop() first.
 111 * --------------------------------------------------------------------
 112 */
 113
 114#include <linux/module.h>
 115#include <linux/kernel.h>
 116#include <linux/sched.h>
 117#include <linux/types.h>
 118#include <linux/slab.h>
 119#include <linux/wireless.h>
 120#include <linux/netdevice.h>
 121#include <linux/timer.h>
 122#include <linux/io.h>
 123#include <linux/delay.h>
 124#include <asm/byteorder.h>
 125#include <linux/bitops.h>
 126#include <linux/list.h>
 127#include <linux/usb.h>
 128#include <linux/byteorder/generic.h>
 129
 130#include "p80211types.h"
 131#include "p80211hdr.h"
 132#include "p80211mgmt.h"
 133#include "p80211conv.h"
 134#include "p80211msg.h"
 135#include "p80211netdev.h"
 136#include "p80211req.h"
 137#include "p80211metadef.h"
 138#include "p80211metastruct.h"
 139#include "hfa384x.h"
 140#include "prism2mgmt.h"
 141
 142enum cmd_mode {
 143        DOWAIT = 0,
 144        DOASYNC
 145};
 146
 147#define THROTTLE_JIFFIES        (HZ / 8)
 148#define URB_ASYNC_UNLINK 0
 149#define USB_QUEUE_BULK 0
 150
 151#define ROUNDUP64(a) (((a) + 63) & ~63)
 152
 153#ifdef DEBUG_USB
 154static void dbprint_urb(struct urb *urb);
 155#endif
 156
 157static void hfa384x_int_rxmonitor(struct wlandevice *wlandev,
 158                                  struct hfa384x_usb_rxfrm *rxfrm);
 159
 160static void hfa384x_usb_defer(struct work_struct *data);
 161
 162static int submit_rx_urb(struct hfa384x *hw, gfp_t flags);
 163
 164static int submit_tx_urb(struct hfa384x *hw, struct urb *tx_urb, gfp_t flags);
 165
 166/*---------------------------------------------------*/
 167/* Callbacks */
 168static void hfa384x_usbout_callback(struct urb *urb);
 169static void hfa384x_ctlxout_callback(struct urb *urb);
 170static void hfa384x_usbin_callback(struct urb *urb);
 171
 172static void
 173hfa384x_usbin_txcompl(struct wlandevice *wlandev, union hfa384x_usbin *usbin);
 174
 175static void hfa384x_usbin_rx(struct wlandevice *wlandev, struct sk_buff *skb);
 176
 177static void hfa384x_usbin_info(struct wlandevice *wlandev,
 178                               union hfa384x_usbin *usbin);
 179
 180static void hfa384x_usbin_ctlx(struct hfa384x *hw, union hfa384x_usbin *usbin,
 181                               int urb_status);
 182
 183/*---------------------------------------------------*/
 184/* Functions to support the prism2 usb command queue */
 185
 186static void hfa384x_usbctlxq_run(struct hfa384x *hw);
 187
 188static void hfa384x_usbctlx_reqtimerfn(struct timer_list *t);
 189
 190static void hfa384x_usbctlx_resptimerfn(struct timer_list *t);
 191
 192static void hfa384x_usb_throttlefn(struct timer_list *t);
 193
 194static void hfa384x_usbctlx_completion_task(unsigned long data);
 195
 196static void hfa384x_usbctlx_reaper_task(unsigned long data);
 197
 198static int hfa384x_usbctlx_submit(struct hfa384x *hw,
 199                                  struct hfa384x_usbctlx *ctlx);
 200
 201static void unlocked_usbctlx_complete(struct hfa384x *hw,
 202                                      struct hfa384x_usbctlx *ctlx);
 203
 204struct usbctlx_completor {
 205        int (*complete)(struct usbctlx_completor *completor);
 206};
 207
 208static int
 209hfa384x_usbctlx_complete_sync(struct hfa384x *hw,
 210                              struct hfa384x_usbctlx *ctlx,
 211                              struct usbctlx_completor *completor);
 212
 213static int
 214unlocked_usbctlx_cancel_async(struct hfa384x *hw, struct hfa384x_usbctlx *ctlx);
 215
 216static void hfa384x_cb_status(struct hfa384x *hw,
 217                              const struct hfa384x_usbctlx *ctlx);
 218
 219static int
 220usbctlx_get_status(const struct hfa384x_usb_statusresp *cmdresp,
 221                   struct hfa384x_cmdresult *result);
 222
 223static void
 224usbctlx_get_rridresult(const struct hfa384x_usb_rridresp *rridresp,
 225                       struct hfa384x_rridresult *result);
 226
 227/*---------------------------------------------------*/
 228/* Low level req/resp CTLX formatters and submitters */
 229static inline int
 230hfa384x_docmd(struct hfa384x *hw,
 231              struct hfa384x_metacmd *cmd);
 232
 233static int
 234hfa384x_dorrid(struct hfa384x *hw,
 235               enum cmd_mode mode,
 236               u16 rid,
 237               void *riddata,
 238               unsigned int riddatalen,
 239               ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data);
 240
 241static int
 242hfa384x_dowrid(struct hfa384x *hw,
 243               enum cmd_mode mode,
 244               u16 rid,
 245               void *riddata,
 246               unsigned int riddatalen,
 247               ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data);
 248
 249static int
 250hfa384x_dormem(struct hfa384x *hw,
 251               u16 page,
 252               u16 offset,
 253               void *data,
 254               unsigned int len);
 255
 256static int
 257hfa384x_dowmem(struct hfa384x *hw,
 258               u16 page,
 259               u16 offset,
 260               void *data,
 261               unsigned int len);
 262
 263static int hfa384x_isgood_pdrcode(u16 pdrcode);
 264
 265static inline const char *ctlxstr(enum ctlx_state s)
 266{
 267        static const char * const ctlx_str[] = {
 268                "Initial state",
 269                "Complete",
 270                "Request failed",
 271                "Request pending",
 272                "Request packet submitted",
 273                "Request packet completed",
 274                "Response packet completed"
 275        };
 276
 277        return ctlx_str[s];
 278};
 279
 280static inline struct hfa384x_usbctlx *get_active_ctlx(struct hfa384x *hw)
 281{
 282        return list_entry(hw->ctlxq.active.next, struct hfa384x_usbctlx, list);
 283}
 284
 285#ifdef DEBUG_USB
 286void dbprint_urb(struct urb *urb)
 287{
 288        pr_debug("urb->pipe=0x%08x\n", urb->pipe);
 289        pr_debug("urb->status=0x%08x\n", urb->status);
 290        pr_debug("urb->transfer_flags=0x%08x\n", urb->transfer_flags);
 291        pr_debug("urb->transfer_buffer=0x%08x\n",
 292                 (unsigned int)urb->transfer_buffer);
 293        pr_debug("urb->transfer_buffer_length=0x%08x\n",
 294                 urb->transfer_buffer_length);
 295        pr_debug("urb->actual_length=0x%08x\n", urb->actual_length);
 296        pr_debug("urb->setup_packet(ctl)=0x%08x\n",
 297                 (unsigned int)urb->setup_packet);
 298        pr_debug("urb->start_frame(iso/irq)=0x%08x\n", urb->start_frame);
 299        pr_debug("urb->interval(irq)=0x%08x\n", urb->interval);
 300        pr_debug("urb->error_count(iso)=0x%08x\n", urb->error_count);
 301        pr_debug("urb->context=0x%08x\n", (unsigned int)urb->context);
 302        pr_debug("urb->complete=0x%08x\n", (unsigned int)urb->complete);
 303}
 304#endif
 305
 306/*----------------------------------------------------------------
 307 * submit_rx_urb
 308 *
 309 * Listen for input data on the BULK-IN pipe. If the pipe has
 310 * stalled then schedule it to be reset.
 311 *
 312 * Arguments:
 313 *      hw              device struct
 314 *      memflags        memory allocation flags
 315 *
 316 * Returns:
 317 *      error code from submission
 318 *
 319 * Call context:
 320 *      Any
 321 *----------------------------------------------------------------
 322 */
 323static int submit_rx_urb(struct hfa384x *hw, gfp_t memflags)
 324{
 325        struct sk_buff *skb;
 326        int result;
 327
 328        skb = dev_alloc_skb(sizeof(union hfa384x_usbin));
 329        if (!skb) {
 330                result = -ENOMEM;
 331                goto done;
 332        }
 333
 334        /* Post the IN urb */
 335        usb_fill_bulk_urb(&hw->rx_urb, hw->usb,
 336                          hw->endp_in,
 337                          skb->data, sizeof(union hfa384x_usbin),
 338                          hfa384x_usbin_callback, hw->wlandev);
 339
 340        hw->rx_urb_skb = skb;
 341
 342        result = -ENOLINK;
 343        if (!hw->wlandev->hwremoved &&
 344            !test_bit(WORK_RX_HALT, &hw->usb_flags)) {
 345                result = usb_submit_urb(&hw->rx_urb, memflags);
 346
 347                /* Check whether we need to reset the RX pipe */
 348                if (result == -EPIPE) {
 349                        netdev_warn(hw->wlandev->netdev,
 350                                    "%s rx pipe stalled: requesting reset\n",
 351                                    hw->wlandev->netdev->name);
 352                        if (!test_and_set_bit(WORK_RX_HALT, &hw->usb_flags))
 353                                schedule_work(&hw->usb_work);
 354                }
 355        }
 356
 357        /* Don't leak memory if anything should go wrong */
 358        if (result != 0) {
 359                dev_kfree_skb(skb);
 360                hw->rx_urb_skb = NULL;
 361        }
 362
 363done:
 364        return result;
 365}
 366
 367/*----------------------------------------------------------------
 368 * submit_tx_urb
 369 *
 370 * Prepares and submits the URB of transmitted data. If the
 371 * submission fails then it will schedule the output pipe to
 372 * be reset.
 373 *
 374 * Arguments:
 375 *      hw              device struct
 376 *      tx_urb          URB of data for transmission
 377 *      memflags        memory allocation flags
 378 *
 379 * Returns:
 380 *      error code from submission
 381 *
 382 * Call context:
 383 *      Any
 384 *----------------------------------------------------------------
 385 */
 386static int submit_tx_urb(struct hfa384x *hw, struct urb *tx_urb, gfp_t memflags)
 387{
 388        struct net_device *netdev = hw->wlandev->netdev;
 389        int result;
 390
 391        result = -ENOLINK;
 392        if (netif_running(netdev)) {
 393                if (!hw->wlandev->hwremoved &&
 394                    !test_bit(WORK_TX_HALT, &hw->usb_flags)) {
 395                        result = usb_submit_urb(tx_urb, memflags);
 396
 397                        /* Test whether we need to reset the TX pipe */
 398                        if (result == -EPIPE) {
 399                                netdev_warn(hw->wlandev->netdev,
 400                                            "%s tx pipe stalled: requesting reset\n",
 401                                            netdev->name);
 402                                set_bit(WORK_TX_HALT, &hw->usb_flags);
 403                                schedule_work(&hw->usb_work);
 404                        } else if (result == 0) {
 405                                netif_stop_queue(netdev);
 406                        }
 407                }
 408        }
 409
 410        return result;
 411}
 412
 413/*----------------------------------------------------------------
 414 * hfa394x_usb_defer
 415 *
 416 * There are some things that the USB stack cannot do while
 417 * in interrupt context, so we arrange this function to run
 418 * in process context.
 419 *
 420 * Arguments:
 421 *      hw      device structure
 422 *
 423 * Returns:
 424 *      nothing
 425 *
 426 * Call context:
 427 *      process (by design)
 428 *----------------------------------------------------------------
 429 */
 430static void hfa384x_usb_defer(struct work_struct *data)
 431{
 432        struct hfa384x *hw = container_of(data, struct hfa384x, usb_work);
 433        struct net_device *netdev = hw->wlandev->netdev;
 434
 435        /* Don't bother trying to reset anything if the plug
 436         * has been pulled ...
 437         */
 438        if (hw->wlandev->hwremoved)
 439                return;
 440
 441        /* Reception has stopped: try to reset the input pipe */
 442        if (test_bit(WORK_RX_HALT, &hw->usb_flags)) {
 443                int ret;
 444
 445                usb_kill_urb(&hw->rx_urb); /* Cannot be holding spinlock! */
 446
 447                ret = usb_clear_halt(hw->usb, hw->endp_in);
 448                if (ret != 0) {
 449                        netdev_err(hw->wlandev->netdev,
 450                                   "Failed to clear rx pipe for %s: err=%d\n",
 451                                   netdev->name, ret);
 452                } else {
 453                        netdev_info(hw->wlandev->netdev, "%s rx pipe reset complete.\n",
 454                                    netdev->name);
 455                        clear_bit(WORK_RX_HALT, &hw->usb_flags);
 456                        set_bit(WORK_RX_RESUME, &hw->usb_flags);
 457                }
 458        }
 459
 460        /* Resume receiving data back from the device. */
 461        if (test_bit(WORK_RX_RESUME, &hw->usb_flags)) {
 462                int ret;
 463
 464                ret = submit_rx_urb(hw, GFP_KERNEL);
 465                if (ret != 0) {
 466                        netdev_err(hw->wlandev->netdev,
 467                                   "Failed to resume %s rx pipe.\n",
 468                                   netdev->name);
 469                } else {
 470                        clear_bit(WORK_RX_RESUME, &hw->usb_flags);
 471                }
 472        }
 473
 474        /* Transmission has stopped: try to reset the output pipe */
 475        if (test_bit(WORK_TX_HALT, &hw->usb_flags)) {
 476                int ret;
 477
 478                usb_kill_urb(&hw->tx_urb);
 479                ret = usb_clear_halt(hw->usb, hw->endp_out);
 480                if (ret != 0) {
 481                        netdev_err(hw->wlandev->netdev,
 482                                   "Failed to clear tx pipe for %s: err=%d\n",
 483                                   netdev->name, ret);
 484                } else {
 485                        netdev_info(hw->wlandev->netdev, "%s tx pipe reset complete.\n",
 486                                    netdev->name);
 487                        clear_bit(WORK_TX_HALT, &hw->usb_flags);
 488                        set_bit(WORK_TX_RESUME, &hw->usb_flags);
 489
 490                        /* Stopping the BULK-OUT pipe also blocked
 491                         * us from sending any more CTLX URBs, so
 492                         * we need to re-run our queue ...
 493                         */
 494                        hfa384x_usbctlxq_run(hw);
 495                }
 496        }
 497
 498        /* Resume transmitting. */
 499        if (test_and_clear_bit(WORK_TX_RESUME, &hw->usb_flags))
 500                netif_wake_queue(hw->wlandev->netdev);
 501}
 502
 503/*----------------------------------------------------------------
 504 * hfa384x_create
 505 *
 506 * Sets up the struct hfa384x data structure for use.  Note this
 507 * does _not_ initialize the actual hardware, just the data structures
 508 * we use to keep track of its state.
 509 *
 510 * Arguments:
 511 *      hw              device structure
 512 *      irq             device irq number
 513 *      iobase          i/o base address for register access
 514 *      membase         memory base address for register access
 515 *
 516 * Returns:
 517 *      nothing
 518 *
 519 * Side effects:
 520 *
 521 * Call context:
 522 *      process
 523 *----------------------------------------------------------------
 524 */
 525void hfa384x_create(struct hfa384x *hw, struct usb_device *usb)
 526{
 527        hw->usb = usb;
 528
 529        /* Set up the waitq */
 530        init_waitqueue_head(&hw->cmdq);
 531
 532        /* Initialize the command queue */
 533        spin_lock_init(&hw->ctlxq.lock);
 534        INIT_LIST_HEAD(&hw->ctlxq.pending);
 535        INIT_LIST_HEAD(&hw->ctlxq.active);
 536        INIT_LIST_HEAD(&hw->ctlxq.completing);
 537        INIT_LIST_HEAD(&hw->ctlxq.reapable);
 538
 539        /* Initialize the authentication queue */
 540        skb_queue_head_init(&hw->authq);
 541
 542        tasklet_init(&hw->reaper_bh,
 543                     hfa384x_usbctlx_reaper_task, (unsigned long)hw);
 544        tasklet_init(&hw->completion_bh,
 545                     hfa384x_usbctlx_completion_task, (unsigned long)hw);
 546        INIT_WORK(&hw->link_bh, prism2sta_processing_defer);
 547        INIT_WORK(&hw->usb_work, hfa384x_usb_defer);
 548
 549        timer_setup(&hw->throttle, hfa384x_usb_throttlefn, 0);
 550
 551        timer_setup(&hw->resptimer, hfa384x_usbctlx_resptimerfn, 0);
 552
 553        timer_setup(&hw->reqtimer, hfa384x_usbctlx_reqtimerfn, 0);
 554
 555        usb_init_urb(&hw->rx_urb);
 556        usb_init_urb(&hw->tx_urb);
 557        usb_init_urb(&hw->ctlx_urb);
 558
 559        hw->link_status = HFA384x_LINK_NOTCONNECTED;
 560        hw->state = HFA384x_STATE_INIT;
 561
 562        INIT_WORK(&hw->commsqual_bh, prism2sta_commsqual_defer);
 563        timer_setup(&hw->commsqual_timer, prism2sta_commsqual_timer, 0);
 564}
 565
 566/*----------------------------------------------------------------
 567 * hfa384x_destroy
 568 *
 569 * Partner to hfa384x_create().  This function cleans up the hw
 570 * structure so that it can be freed by the caller using a simple
 571 * kfree.  Currently, this function is just a placeholder.  If, at some
 572 * point in the future, an hw in the 'shutdown' state requires a 'deep'
 573 * kfree, this is where it should be done.  Note that if this function
 574 * is called on a _running_ hw structure, the drvr_stop() function is
 575 * called.
 576 *
 577 * Arguments:
 578 *      hw              device structure
 579 *
 580 * Returns:
 581 *      nothing, this function is not allowed to fail.
 582 *
 583 * Side effects:
 584 *
 585 * Call context:
 586 *      process
 587 *----------------------------------------------------------------
 588 */
 589void hfa384x_destroy(struct hfa384x *hw)
 590{
 591        struct sk_buff *skb;
 592
 593        if (hw->state == HFA384x_STATE_RUNNING)
 594                hfa384x_drvr_stop(hw);
 595        hw->state = HFA384x_STATE_PREINIT;
 596
 597        kfree(hw->scanresults);
 598        hw->scanresults = NULL;
 599
 600        /* Now to clean out the auth queue */
 601        while ((skb = skb_dequeue(&hw->authq)))
 602                dev_kfree_skb(skb);
 603}
 604
 605static struct hfa384x_usbctlx *usbctlx_alloc(void)
 606{
 607        struct hfa384x_usbctlx *ctlx;
 608
 609        ctlx = kzalloc(sizeof(*ctlx),
 610                       in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
 611        if (ctlx)
 612                init_completion(&ctlx->done);
 613
 614        return ctlx;
 615}
 616
 617static int
 618usbctlx_get_status(const struct hfa384x_usb_statusresp *cmdresp,
 619                   struct hfa384x_cmdresult *result)
 620{
 621        result->status = le16_to_cpu(cmdresp->status);
 622        result->resp0 = le16_to_cpu(cmdresp->resp0);
 623        result->resp1 = le16_to_cpu(cmdresp->resp1);
 624        result->resp2 = le16_to_cpu(cmdresp->resp2);
 625
 626        pr_debug("cmdresult:status=0x%04x resp0=0x%04x resp1=0x%04x resp2=0x%04x\n",
 627                 result->status, result->resp0, result->resp1, result->resp2);
 628
 629        return result->status & HFA384x_STATUS_RESULT;
 630}
 631
 632static void
 633usbctlx_get_rridresult(const struct hfa384x_usb_rridresp *rridresp,
 634                       struct hfa384x_rridresult *result)
 635{
 636        result->rid = le16_to_cpu(rridresp->rid);
 637        result->riddata = rridresp->data;
 638        result->riddata_len = ((le16_to_cpu(rridresp->frmlen) - 1) * 2);
 639}
 640
 641/*----------------------------------------------------------------
 642 * Completor object:
 643 * This completor must be passed to hfa384x_usbctlx_complete_sync()
 644 * when processing a CTLX that returns a struct hfa384x_cmdresult structure.
 645 *----------------------------------------------------------------
 646 */
 647struct usbctlx_cmd_completor {
 648        struct usbctlx_completor head;
 649
 650        const struct hfa384x_usb_statusresp *cmdresp;
 651        struct hfa384x_cmdresult *result;
 652};
 653
 654static inline int usbctlx_cmd_completor_fn(struct usbctlx_completor *head)
 655{
 656        struct usbctlx_cmd_completor *complete;
 657
 658        complete = (struct usbctlx_cmd_completor *)head;
 659        return usbctlx_get_status(complete->cmdresp, complete->result);
 660}
 661
 662static inline struct usbctlx_completor *
 663init_cmd_completor(struct usbctlx_cmd_completor *completor,
 664                   const struct hfa384x_usb_statusresp *cmdresp,
 665                   struct hfa384x_cmdresult *result)
 666{
 667        completor->head.complete = usbctlx_cmd_completor_fn;
 668        completor->cmdresp = cmdresp;
 669        completor->result = result;
 670        return &completor->head;
 671}
 672
 673/*----------------------------------------------------------------
 674 * Completor object:
 675 * This completor must be passed to hfa384x_usbctlx_complete_sync()
 676 * when processing a CTLX that reads a RID.
 677 *----------------------------------------------------------------
 678 */
 679struct usbctlx_rrid_completor {
 680        struct usbctlx_completor head;
 681
 682        const struct hfa384x_usb_rridresp *rridresp;
 683        void *riddata;
 684        unsigned int riddatalen;
 685};
 686
 687static int usbctlx_rrid_completor_fn(struct usbctlx_completor *head)
 688{
 689        struct usbctlx_rrid_completor *complete;
 690        struct hfa384x_rridresult rridresult;
 691
 692        complete = (struct usbctlx_rrid_completor *)head;
 693        usbctlx_get_rridresult(complete->rridresp, &rridresult);
 694
 695        /* Validate the length, note body len calculation in bytes */
 696        if (rridresult.riddata_len != complete->riddatalen) {
 697                pr_warn("RID len mismatch, rid=0x%04x hlen=%d fwlen=%d\n",
 698                        rridresult.rid,
 699                        complete->riddatalen, rridresult.riddata_len);
 700                return -ENODATA;
 701        }
 702
 703        memcpy(complete->riddata, rridresult.riddata, complete->riddatalen);
 704        return 0;
 705}
 706
 707static inline struct usbctlx_completor *
 708init_rrid_completor(struct usbctlx_rrid_completor *completor,
 709                    const struct hfa384x_usb_rridresp *rridresp,
 710                    void *riddata,
 711                    unsigned int riddatalen)
 712{
 713        completor->head.complete = usbctlx_rrid_completor_fn;
 714        completor->rridresp = rridresp;
 715        completor->riddata = riddata;
 716        completor->riddatalen = riddatalen;
 717        return &completor->head;
 718}
 719
 720/*----------------------------------------------------------------
 721 * Completor object:
 722 * Interprets the results of a synchronous RID-write
 723 *----------------------------------------------------------------
 724 */
 725#define init_wrid_completor  init_cmd_completor
 726
 727/*----------------------------------------------------------------
 728 * Completor object:
 729 * Interprets the results of a synchronous memory-write
 730 *----------------------------------------------------------------
 731 */
 732#define init_wmem_completor  init_cmd_completor
 733
 734/*----------------------------------------------------------------
 735 * Completor object:
 736 * Interprets the results of a synchronous memory-read
 737 *----------------------------------------------------------------
 738 */
 739struct usbctlx_rmem_completor {
 740        struct usbctlx_completor head;
 741
 742        const struct hfa384x_usb_rmemresp *rmemresp;
 743        void *data;
 744        unsigned int len;
 745};
 746
 747static int usbctlx_rmem_completor_fn(struct usbctlx_completor *head)
 748{
 749        struct usbctlx_rmem_completor *complete =
 750                (struct usbctlx_rmem_completor *)head;
 751
 752        pr_debug("rmemresp:len=%d\n", complete->rmemresp->frmlen);
 753        memcpy(complete->data, complete->rmemresp->data, complete->len);
 754        return 0;
 755}
 756
 757static inline struct usbctlx_completor *
 758init_rmem_completor(struct usbctlx_rmem_completor *completor,
 759                    struct hfa384x_usb_rmemresp *rmemresp,
 760                    void *data,
 761                    unsigned int len)
 762{
 763        completor->head.complete = usbctlx_rmem_completor_fn;
 764        completor->rmemresp = rmemresp;
 765        completor->data = data;
 766        completor->len = len;
 767        return &completor->head;
 768}
 769
 770/*----------------------------------------------------------------
 771 * hfa384x_cb_status
 772 *
 773 * Ctlx_complete handler for async CMD type control exchanges.
 774 * mark the hw struct as such.
 775 *
 776 * Note: If the handling is changed here, it should probably be
 777 *       changed in docmd as well.
 778 *
 779 * Arguments:
 780 *      hw              hw struct
 781 *      ctlx            completed CTLX
 782 *
 783 * Returns:
 784 *      nothing
 785 *
 786 * Side effects:
 787 *
 788 * Call context:
 789 *      interrupt
 790 *----------------------------------------------------------------
 791 */
 792static void hfa384x_cb_status(struct hfa384x *hw,
 793                              const struct hfa384x_usbctlx *ctlx)
 794{
 795        if (ctlx->usercb) {
 796                struct hfa384x_cmdresult cmdresult;
 797
 798                if (ctlx->state != CTLX_COMPLETE) {
 799                        memset(&cmdresult, 0, sizeof(cmdresult));
 800                        cmdresult.status =
 801                            HFA384x_STATUS_RESULT_SET(HFA384x_CMD_ERR);
 802                } else {
 803                        usbctlx_get_status(&ctlx->inbuf.cmdresp, &cmdresult);
 804                }
 805
 806                ctlx->usercb(hw, &cmdresult, ctlx->usercb_data);
 807        }
 808}
 809
 810/*----------------------------------------------------------------
 811 * hfa384x_cmd_initialize
 812 *
 813 * Issues the initialize command and sets the hw->state based
 814 * on the result.
 815 *
 816 * Arguments:
 817 *      hw              device structure
 818 *
 819 * Returns:
 820 *      0               success
 821 *      >0              f/w reported error - f/w status code
 822 *      <0              driver reported error
 823 *
 824 * Side effects:
 825 *
 826 * Call context:
 827 *      process
 828 *----------------------------------------------------------------
 829 */
 830int hfa384x_cmd_initialize(struct hfa384x *hw)
 831{
 832        int result = 0;
 833        int i;
 834        struct hfa384x_metacmd cmd;
 835
 836        cmd.cmd = HFA384x_CMDCODE_INIT;
 837        cmd.parm0 = 0;
 838        cmd.parm1 = 0;
 839        cmd.parm2 = 0;
 840
 841        result = hfa384x_docmd(hw, &cmd);
 842
 843        pr_debug("cmdresp.init: status=0x%04x, resp0=0x%04x, resp1=0x%04x, resp2=0x%04x\n",
 844                 cmd.result.status,
 845                 cmd.result.resp0, cmd.result.resp1, cmd.result.resp2);
 846        if (result == 0) {
 847                for (i = 0; i < HFA384x_NUMPORTS_MAX; i++)
 848                        hw->port_enabled[i] = 0;
 849        }
 850
 851        hw->link_status = HFA384x_LINK_NOTCONNECTED;
 852
 853        return result;
 854}
 855
 856/*----------------------------------------------------------------
 857 * hfa384x_cmd_disable
 858 *
 859 * Issues the disable command to stop communications on one of
 860 * the MACs 'ports'.
 861 *
 862 * Arguments:
 863 *      hw              device structure
 864 *      macport         MAC port number (host order)
 865 *
 866 * Returns:
 867 *      0               success
 868 *      >0              f/w reported failure - f/w status code
 869 *      <0              driver reported error (timeout|bad arg)
 870 *
 871 * Side effects:
 872 *
 873 * Call context:
 874 *      process
 875 *----------------------------------------------------------------
 876 */
 877int hfa384x_cmd_disable(struct hfa384x *hw, u16 macport)
 878{
 879        struct hfa384x_metacmd cmd;
 880
 881        cmd.cmd = HFA384x_CMD_CMDCODE_SET(HFA384x_CMDCODE_DISABLE) |
 882            HFA384x_CMD_MACPORT_SET(macport);
 883        cmd.parm0 = 0;
 884        cmd.parm1 = 0;
 885        cmd.parm2 = 0;
 886
 887        return hfa384x_docmd(hw, &cmd);
 888}
 889
 890/*----------------------------------------------------------------
 891 * hfa384x_cmd_enable
 892 *
 893 * Issues the enable command to enable communications on one of
 894 * the MACs 'ports'.
 895 *
 896 * Arguments:
 897 *      hw              device structure
 898 *      macport         MAC port number
 899 *
 900 * Returns:
 901 *      0               success
 902 *      >0              f/w reported failure - f/w status code
 903 *      <0              driver reported error (timeout|bad arg)
 904 *
 905 * Side effects:
 906 *
 907 * Call context:
 908 *      process
 909 *----------------------------------------------------------------
 910 */
 911int hfa384x_cmd_enable(struct hfa384x *hw, u16 macport)
 912{
 913        struct hfa384x_metacmd cmd;
 914
 915        cmd.cmd = HFA384x_CMD_CMDCODE_SET(HFA384x_CMDCODE_ENABLE) |
 916            HFA384x_CMD_MACPORT_SET(macport);
 917        cmd.parm0 = 0;
 918        cmd.parm1 = 0;
 919        cmd.parm2 = 0;
 920
 921        return hfa384x_docmd(hw, &cmd);
 922}
 923
 924/*----------------------------------------------------------------
 925 * hfa384x_cmd_monitor
 926 *
 927 * Enables the 'monitor mode' of the MAC.  Here's the description of
 928 * monitor mode that I've received thus far:
 929 *
 930 *  "The "monitor mode" of operation is that the MAC passes all
 931 *  frames for which the PLCP checks are correct. All received
 932 *  MPDUs are passed to the host with MAC Port = 7, with a
 933 *  receive status of good, FCS error, or undecryptable. Passing
 934 *  certain MPDUs is a violation of the 802.11 standard, but useful
 935 *  for a debugging tool."  Normal communication is not possible
 936 *  while monitor mode is enabled.
 937 *
 938 * Arguments:
 939 *      hw              device structure
 940 *      enable          a code (0x0b|0x0f) that enables/disables
 941 *                      monitor mode. (host order)
 942 *
 943 * Returns:
 944 *      0               success
 945 *      >0              f/w reported failure - f/w status code
 946 *      <0              driver reported error (timeout|bad arg)
 947 *
 948 * Side effects:
 949 *
 950 * Call context:
 951 *      process
 952 *----------------------------------------------------------------
 953 */
 954int hfa384x_cmd_monitor(struct hfa384x *hw, u16 enable)
 955{
 956        struct hfa384x_metacmd cmd;
 957
 958        cmd.cmd = HFA384x_CMD_CMDCODE_SET(HFA384x_CMDCODE_MONITOR) |
 959            HFA384x_CMD_AINFO_SET(enable);
 960        cmd.parm0 = 0;
 961        cmd.parm1 = 0;
 962        cmd.parm2 = 0;
 963
 964        return hfa384x_docmd(hw, &cmd);
 965}
 966
 967/*----------------------------------------------------------------
 968 * hfa384x_cmd_download
 969 *
 970 * Sets the controls for the MAC controller code/data download
 971 * process.  The arguments set the mode and address associated
 972 * with a download.  Note that the aux registers should be enabled
 973 * prior to setting one of the download enable modes.
 974 *
 975 * Arguments:
 976 *      hw              device structure
 977 *      mode            0 - Disable programming and begin code exec
 978 *                      1 - Enable volatile mem programming
 979 *                      2 - Enable non-volatile mem programming
 980 *                      3 - Program non-volatile section from NV download
 981 *                          buffer.
 982 *                      (host order)
 983 *      lowaddr
 984 *      highaddr        For mode 1, sets the high & low order bits of
 985 *                      the "destination address".  This address will be
 986 *                      the execution start address when download is
 987 *                      subsequently disabled.
 988 *                      For mode 2, sets the high & low order bits of
 989 *                      the destination in NV ram.
 990 *                      For modes 0 & 3, should be zero. (host order)
 991 *                      NOTE: these are CMD format.
 992 *      codelen         Length of the data to write in mode 2,
 993 *                      zero otherwise. (host order)
 994 *
 995 * Returns:
 996 *      0               success
 997 *      >0              f/w reported failure - f/w status code
 998 *      <0              driver reported error (timeout|bad arg)
 999 *
1000 * Side effects:
1001 *
1002 * Call context:
1003 *      process
1004 *----------------------------------------------------------------
1005 */
1006int hfa384x_cmd_download(struct hfa384x *hw, u16 mode, u16 lowaddr,
1007                         u16 highaddr, u16 codelen)
1008{
1009        struct hfa384x_metacmd cmd;
1010
1011        pr_debug("mode=%d, lowaddr=0x%04x, highaddr=0x%04x, codelen=%d\n",
1012                 mode, lowaddr, highaddr, codelen);
1013
1014        cmd.cmd = (HFA384x_CMD_CMDCODE_SET(HFA384x_CMDCODE_DOWNLD) |
1015                   HFA384x_CMD_PROGMODE_SET(mode));
1016
1017        cmd.parm0 = lowaddr;
1018        cmd.parm1 = highaddr;
1019        cmd.parm2 = codelen;
1020
1021        return hfa384x_docmd(hw, &cmd);
1022}
1023
1024/*----------------------------------------------------------------
1025 * hfa384x_corereset
1026 *
1027 * Perform a reset of the hfa38xx MAC core.  We assume that the hw
1028 * structure is in its "created" state.  That is, it is initialized
1029 * with proper values.  Note that if a reset is done after the
1030 * device has been active for awhile, the caller might have to clean
1031 * up some leftover cruft in the hw structure.
1032 *
1033 * Arguments:
1034 *      hw              device structure
1035 *      holdtime        how long (in ms) to hold the reset
1036 *      settletime      how long (in ms) to wait after releasing
1037 *                      the reset
1038 *
1039 * Returns:
1040 *      nothing
1041 *
1042 * Side effects:
1043 *
1044 * Call context:
1045 *      process
1046 *----------------------------------------------------------------
1047 */
1048int hfa384x_corereset(struct hfa384x *hw, int holdtime,
1049                      int settletime, int genesis)
1050{
1051        int result;
1052
1053        result = usb_reset_device(hw->usb);
1054        if (result < 0) {
1055                netdev_err(hw->wlandev->netdev, "usb_reset_device() failed, result=%d.\n",
1056                           result);
1057        }
1058
1059        return result;
1060}
1061
1062/*----------------------------------------------------------------
1063 * hfa384x_usbctlx_complete_sync
1064 *
1065 * Waits for a synchronous CTLX object to complete,
1066 * and then handles the response.
1067 *
1068 * Arguments:
1069 *      hw              device structure
1070 *      ctlx            CTLX ptr
1071 *      completor       functor object to decide what to
1072 *                      do with the CTLX's result.
1073 *
1074 * Returns:
1075 *      0               Success
1076 *      -ERESTARTSYS    Interrupted by a signal
1077 *      -EIO            CTLX failed
1078 *      -ENODEV         Adapter was unplugged
1079 *      ???             Result from completor
1080 *
1081 * Side effects:
1082 *
1083 * Call context:
1084 *      process
1085 *----------------------------------------------------------------
1086 */
1087static int hfa384x_usbctlx_complete_sync(struct hfa384x *hw,
1088                                         struct hfa384x_usbctlx *ctlx,
1089                                         struct usbctlx_completor *completor)
1090{
1091        unsigned long flags;
1092        int result;
1093
1094        result = wait_for_completion_interruptible(&ctlx->done);
1095
1096        spin_lock_irqsave(&hw->ctlxq.lock, flags);
1097
1098        /*
1099         * We can only handle the CTLX if the USB disconnect
1100         * function has not run yet ...
1101         */
1102cleanup:
1103        if (hw->wlandev->hwremoved) {
1104                spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
1105                result = -ENODEV;
1106        } else if (result != 0) {
1107                int runqueue = 0;
1108
1109                /*
1110                 * We were probably interrupted, so delete
1111                 * this CTLX asynchronously, kill the timers
1112                 * and the URB, and then start the next
1113                 * pending CTLX.
1114                 *
1115                 * NOTE: We can only delete the timers and
1116                 *       the URB if this CTLX is active.
1117                 */
1118                if (ctlx == get_active_ctlx(hw)) {
1119                        spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
1120
1121                        del_singleshot_timer_sync(&hw->reqtimer);
1122                        del_singleshot_timer_sync(&hw->resptimer);
1123                        hw->req_timer_done = 1;
1124                        hw->resp_timer_done = 1;
1125                        usb_kill_urb(&hw->ctlx_urb);
1126
1127                        spin_lock_irqsave(&hw->ctlxq.lock, flags);
1128
1129                        runqueue = 1;
1130
1131                        /*
1132                         * This scenario is so unlikely that I'm
1133                         * happy with a grubby "goto" solution ...
1134                         */
1135                        if (hw->wlandev->hwremoved)
1136                                goto cleanup;
1137                }
1138
1139                /*
1140                 * The completion task will send this CTLX
1141                 * to the reaper the next time it runs. We
1142                 * are no longer in a hurry.
1143                 */
1144                ctlx->reapable = 1;
1145                ctlx->state = CTLX_REQ_FAILED;
1146                list_move_tail(&ctlx->list, &hw->ctlxq.completing);
1147
1148                spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
1149
1150                if (runqueue)
1151                        hfa384x_usbctlxq_run(hw);
1152        } else {
1153                if (ctlx->state == CTLX_COMPLETE) {
1154                        result = completor->complete(completor);
1155                } else {
1156                        netdev_warn(hw->wlandev->netdev, "CTLX[%d] error: state(%s)\n",
1157                                    le16_to_cpu(ctlx->outbuf.type),
1158                                    ctlxstr(ctlx->state));
1159                        result = -EIO;
1160                }
1161
1162                list_del(&ctlx->list);
1163                spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
1164                kfree(ctlx);
1165        }
1166
1167        return result;
1168}
1169
1170/*----------------------------------------------------------------
1171 * hfa384x_docmd
1172 *
1173 * Constructs a command CTLX and submits it.
1174 *
1175 * NOTE: Any changes to the 'post-submit' code in this function
1176 *       need to be carried over to hfa384x_cbcmd() since the handling
1177 *       is virtually identical.
1178 *
1179 * Arguments:
1180 *      hw              device structure
1181 *       cmd             cmd structure.  Includes all arguments and result
1182 *                       data points.  All in host order. in host order
1183 *
1184 * Returns:
1185 *      0               success
1186 *      -EIO            CTLX failure
1187 *      -ERESTARTSYS    Awakened on signal
1188 *      >0              command indicated error, Status and Resp0-2 are
1189 *                      in hw structure.
1190 *
1191 * Side effects:
1192 *
1193 *
1194 * Call context:
1195 *      process
1196 *----------------------------------------------------------------
1197 */
1198static inline int
1199hfa384x_docmd(struct hfa384x *hw,
1200              struct hfa384x_metacmd *cmd)
1201{
1202        int result;
1203        struct hfa384x_usbctlx *ctlx;
1204
1205        ctlx = usbctlx_alloc();
1206        if (!ctlx) {
1207                result = -ENOMEM;
1208                goto done;
1209        }
1210
1211        /* Initialize the command */
1212        ctlx->outbuf.cmdreq.type = cpu_to_le16(HFA384x_USB_CMDREQ);
1213        ctlx->outbuf.cmdreq.cmd = cpu_to_le16(cmd->cmd);
1214        ctlx->outbuf.cmdreq.parm0 = cpu_to_le16(cmd->parm0);
1215        ctlx->outbuf.cmdreq.parm1 = cpu_to_le16(cmd->parm1);
1216        ctlx->outbuf.cmdreq.parm2 = cpu_to_le16(cmd->parm2);
1217
1218        ctlx->outbufsize = sizeof(ctlx->outbuf.cmdreq);
1219
1220        pr_debug("cmdreq: cmd=0x%04x parm0=0x%04x parm1=0x%04x parm2=0x%04x\n",
1221                 cmd->cmd, cmd->parm0, cmd->parm1, cmd->parm2);
1222
1223        ctlx->reapable = DOWAIT;
1224        ctlx->cmdcb = NULL;
1225        ctlx->usercb = NULL;
1226        ctlx->usercb_data = NULL;
1227
1228        result = hfa384x_usbctlx_submit(hw, ctlx);
1229        if (result != 0) {
1230                kfree(ctlx);
1231        } else {
1232                struct usbctlx_cmd_completor cmd_completor;
1233                struct usbctlx_completor *completor;
1234
1235                completor = init_cmd_completor(&cmd_completor,
1236                                               &ctlx->inbuf.cmdresp,
1237                                               &cmd->result);
1238
1239                result = hfa384x_usbctlx_complete_sync(hw, ctlx, completor);
1240        }
1241
1242done:
1243        return result;
1244}
1245
1246/*----------------------------------------------------------------
1247 * hfa384x_dorrid
1248 *
1249 * Constructs a read rid CTLX and issues it.
1250 *
1251 * NOTE: Any changes to the 'post-submit' code in this function
1252 *       need to be carried over to hfa384x_cbrrid() since the handling
1253 *       is virtually identical.
1254 *
1255 * Arguments:
1256 *      hw              device structure
1257 *      mode            DOWAIT or DOASYNC
1258 *      rid             Read RID number (host order)
1259 *      riddata         Caller supplied buffer that MAC formatted RID.data
1260 *                      record will be written to for DOWAIT calls. Should
1261 *                      be NULL for DOASYNC calls.
1262 *      riddatalen      Buffer length for DOWAIT calls. Zero for DOASYNC calls.
1263 *      cmdcb           command callback for async calls, NULL for DOWAIT calls
1264 *      usercb          user callback for async calls, NULL for DOWAIT calls
1265 *      usercb_data     user supplied data pointer for async calls, NULL
1266 *                      for DOWAIT calls
1267 *
1268 * Returns:
1269 *      0               success
1270 *      -EIO            CTLX failure
1271 *      -ERESTARTSYS    Awakened on signal
1272 *      -ENODATA        riddatalen != macdatalen
1273 *      >0              command indicated error, Status and Resp0-2 are
1274 *                      in hw structure.
1275 *
1276 * Side effects:
1277 *
1278 * Call context:
1279 *      interrupt (DOASYNC)
1280 *      process (DOWAIT or DOASYNC)
1281 *----------------------------------------------------------------
1282 */
1283static int
1284hfa384x_dorrid(struct hfa384x *hw,
1285               enum cmd_mode mode,
1286               u16 rid,
1287               void *riddata,
1288               unsigned int riddatalen,
1289               ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data)
1290{
1291        int result;
1292        struct hfa384x_usbctlx *ctlx;
1293
1294        ctlx = usbctlx_alloc();
1295        if (!ctlx) {
1296                result = -ENOMEM;
1297                goto done;
1298        }
1299
1300        /* Initialize the command */
1301        ctlx->outbuf.rridreq.type = cpu_to_le16(HFA384x_USB_RRIDREQ);
1302        ctlx->outbuf.rridreq.frmlen =
1303            cpu_to_le16(sizeof(ctlx->outbuf.rridreq.rid));
1304        ctlx->outbuf.rridreq.rid = cpu_to_le16(rid);
1305
1306        ctlx->outbufsize = sizeof(ctlx->outbuf.rridreq);
1307
1308        ctlx->reapable = mode;
1309        ctlx->cmdcb = cmdcb;
1310        ctlx->usercb = usercb;
1311        ctlx->usercb_data = usercb_data;
1312
1313        /* Submit the CTLX */
1314        result = hfa384x_usbctlx_submit(hw, ctlx);
1315        if (result != 0) {
1316                kfree(ctlx);
1317        } else if (mode == DOWAIT) {
1318                struct usbctlx_rrid_completor completor;
1319
1320                result =
1321                    hfa384x_usbctlx_complete_sync(hw, ctlx,
1322                                                  init_rrid_completor
1323                                                  (&completor,
1324                                                   &ctlx->inbuf.rridresp,
1325                                                   riddata, riddatalen));
1326        }
1327
1328done:
1329        return result;
1330}
1331
1332/*----------------------------------------------------------------
1333 * hfa384x_dowrid
1334 *
1335 * Constructs a write rid CTLX and issues it.
1336 *
1337 * NOTE: Any changes to the 'post-submit' code in this function
1338 *       need to be carried over to hfa384x_cbwrid() since the handling
1339 *       is virtually identical.
1340 *
1341 * Arguments:
1342 *      hw              device structure
1343 *      enum cmd_mode   DOWAIT or DOASYNC
1344 *      rid             RID code
1345 *      riddata         Data portion of RID formatted for MAC
1346 *      riddatalen      Length of the data portion in bytes
1347 *       cmdcb           command callback for async calls, NULL for DOWAIT calls
1348 *      usercb          user callback for async calls, NULL for DOWAIT calls
1349 *      usercb_data     user supplied data pointer for async calls
1350 *
1351 * Returns:
1352 *      0               success
1353 *      -ETIMEDOUT      timed out waiting for register ready or
1354 *                      command completion
1355 *      >0              command indicated error, Status and Resp0-2 are
1356 *                      in hw structure.
1357 *
1358 * Side effects:
1359 *
1360 * Call context:
1361 *      interrupt (DOASYNC)
1362 *      process (DOWAIT or DOASYNC)
1363 *----------------------------------------------------------------
1364 */
1365static int
1366hfa384x_dowrid(struct hfa384x *hw,
1367               enum cmd_mode mode,
1368               u16 rid,
1369               void *riddata,
1370               unsigned int riddatalen,
1371               ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data)
1372{
1373        int result;
1374        struct hfa384x_usbctlx *ctlx;
1375
1376        ctlx = usbctlx_alloc();
1377        if (!ctlx) {
1378                result = -ENOMEM;
1379                goto done;
1380        }
1381
1382        /* Initialize the command */
1383        ctlx->outbuf.wridreq.type = cpu_to_le16(HFA384x_USB_WRIDREQ);
1384        ctlx->outbuf.wridreq.frmlen = cpu_to_le16((sizeof
1385                                                   (ctlx->outbuf.wridreq.rid) +
1386                                                   riddatalen + 1) / 2);
1387        ctlx->outbuf.wridreq.rid = cpu_to_le16(rid);
1388        memcpy(ctlx->outbuf.wridreq.data, riddata, riddatalen);
1389
1390        ctlx->outbufsize = sizeof(ctlx->outbuf.wridreq.type) +
1391            sizeof(ctlx->outbuf.wridreq.frmlen) +
1392            sizeof(ctlx->outbuf.wridreq.rid) + riddatalen;
1393
1394        ctlx->reapable = mode;
1395        ctlx->cmdcb = cmdcb;
1396        ctlx->usercb = usercb;
1397        ctlx->usercb_data = usercb_data;
1398
1399        /* Submit the CTLX */
1400        result = hfa384x_usbctlx_submit(hw, ctlx);
1401        if (result != 0) {
1402                kfree(ctlx);
1403        } else if (mode == DOWAIT) {
1404                struct usbctlx_cmd_completor completor;
1405                struct hfa384x_cmdresult wridresult;
1406
1407                result = hfa384x_usbctlx_complete_sync(hw,
1408                                                       ctlx,
1409                                                       init_wrid_completor
1410                                                       (&completor,
1411                                                        &ctlx->inbuf.wridresp,
1412                                                        &wridresult));
1413        }
1414
1415done:
1416        return result;
1417}
1418
1419/*----------------------------------------------------------------
1420 * hfa384x_dormem
1421 *
1422 * Constructs a readmem CTLX and issues it.
1423 *
1424 * NOTE: Any changes to the 'post-submit' code in this function
1425 *       need to be carried over to hfa384x_cbrmem() since the handling
1426 *       is virtually identical.
1427 *
1428 * Arguments:
1429 *      hw              device structure
1430 *      page            MAC address space page (CMD format)
1431 *      offset          MAC address space offset
1432 *      data            Ptr to data buffer to receive read
1433 *      len             Length of the data to read (max == 2048)
1434 *
1435 * Returns:
1436 *      0               success
1437 *      -ETIMEDOUT      timed out waiting for register ready or
1438 *                      command completion
1439 *      >0              command indicated error, Status and Resp0-2 are
1440 *                      in hw structure.
1441 *
1442 * Side effects:
1443 *
1444 * Call context:
1445 *      process (DOWAIT)
1446 *----------------------------------------------------------------
1447 */
1448static int
1449hfa384x_dormem(struct hfa384x *hw,
1450               u16 page,
1451               u16 offset,
1452               void *data,
1453               unsigned int len)
1454{
1455        int result;
1456        struct hfa384x_usbctlx *ctlx;
1457
1458        ctlx = usbctlx_alloc();
1459        if (!ctlx) {
1460                result = -ENOMEM;
1461                goto done;
1462        }
1463
1464        /* Initialize the command */
1465        ctlx->outbuf.rmemreq.type = cpu_to_le16(HFA384x_USB_RMEMREQ);
1466        ctlx->outbuf.rmemreq.frmlen =
1467            cpu_to_le16(sizeof(ctlx->outbuf.rmemreq.offset) +
1468                        sizeof(ctlx->outbuf.rmemreq.page) + len);
1469        ctlx->outbuf.rmemreq.offset = cpu_to_le16(offset);
1470        ctlx->outbuf.rmemreq.page = cpu_to_le16(page);
1471
1472        ctlx->outbufsize = sizeof(ctlx->outbuf.rmemreq);
1473
1474        pr_debug("type=0x%04x frmlen=%d offset=0x%04x page=0x%04x\n",
1475                 ctlx->outbuf.rmemreq.type,
1476                 ctlx->outbuf.rmemreq.frmlen,
1477                 ctlx->outbuf.rmemreq.offset, ctlx->outbuf.rmemreq.page);
1478
1479        pr_debug("pktsize=%zd\n", ROUNDUP64(sizeof(ctlx->outbuf.rmemreq)));
1480
1481        ctlx->reapable = DOWAIT;
1482        ctlx->cmdcb = NULL;
1483        ctlx->usercb = NULL;
1484        ctlx->usercb_data = NULL;
1485
1486        result = hfa384x_usbctlx_submit(hw, ctlx);
1487        if (result != 0) {
1488                kfree(ctlx);
1489        } else {
1490                struct usbctlx_rmem_completor completor;
1491
1492                result =
1493                    hfa384x_usbctlx_complete_sync(hw, ctlx,
1494                                                  init_rmem_completor
1495                                                  (&completor,
1496                                                   &ctlx->inbuf.rmemresp, data,
1497                                                   len));
1498        }
1499
1500done:
1501        return result;
1502}
1503
1504/*----------------------------------------------------------------
1505 * hfa384x_dowmem
1506 *
1507 * Constructs a writemem CTLX and issues it.
1508 *
1509 * NOTE: Any changes to the 'post-submit' code in this function
1510 *       need to be carried over to hfa384x_cbwmem() since the handling
1511 *       is virtually identical.
1512 *
1513 * Arguments:
1514 *      hw              device structure
1515 *      page            MAC address space page (CMD format)
1516 *      offset          MAC address space offset
1517 *      data            Ptr to data buffer containing write data
1518 *      len             Length of the data to read (max == 2048)
1519 *
1520 * Returns:
1521 *      0               success
1522 *      -ETIMEDOUT      timed out waiting for register ready or
1523 *                      command completion
1524 *      >0              command indicated error, Status and Resp0-2 are
1525 *                      in hw structure.
1526 *
1527 * Side effects:
1528 *
1529 * Call context:
1530 *      interrupt (DOWAIT)
1531 *      process (DOWAIT)
1532 *----------------------------------------------------------------
1533 */
1534static int
1535hfa384x_dowmem(struct hfa384x *hw,
1536               u16 page,
1537               u16 offset,
1538               void *data,
1539               unsigned int len)
1540{
1541        int result;
1542        struct hfa384x_usbctlx *ctlx;
1543
1544        pr_debug("page=0x%04x offset=0x%04x len=%d\n", page, offset, len);
1545
1546        ctlx = usbctlx_alloc();
1547        if (!ctlx) {
1548                result = -ENOMEM;
1549                goto done;
1550        }
1551
1552        /* Initialize the command */
1553        ctlx->outbuf.wmemreq.type = cpu_to_le16(HFA384x_USB_WMEMREQ);
1554        ctlx->outbuf.wmemreq.frmlen =
1555            cpu_to_le16(sizeof(ctlx->outbuf.wmemreq.offset) +
1556                        sizeof(ctlx->outbuf.wmemreq.page) + len);
1557        ctlx->outbuf.wmemreq.offset = cpu_to_le16(offset);
1558        ctlx->outbuf.wmemreq.page = cpu_to_le16(page);
1559        memcpy(ctlx->outbuf.wmemreq.data, data, len);
1560
1561        ctlx->outbufsize = sizeof(ctlx->outbuf.wmemreq.type) +
1562            sizeof(ctlx->outbuf.wmemreq.frmlen) +
1563            sizeof(ctlx->outbuf.wmemreq.offset) +
1564            sizeof(ctlx->outbuf.wmemreq.page) + len;
1565
1566        ctlx->reapable = DOWAIT;
1567        ctlx->cmdcb = NULL;
1568        ctlx->usercb = NULL;
1569        ctlx->usercb_data = NULL;
1570
1571        result = hfa384x_usbctlx_submit(hw, ctlx);
1572        if (result != 0) {
1573                kfree(ctlx);
1574        } else {
1575                struct usbctlx_cmd_completor completor;
1576                struct hfa384x_cmdresult wmemresult;
1577
1578                result = hfa384x_usbctlx_complete_sync(hw,
1579                                                       ctlx,
1580                                                       init_wmem_completor
1581                                                       (&completor,
1582                                                        &ctlx->inbuf.wmemresp,
1583                                                        &wmemresult));
1584        }
1585
1586done:
1587        return result;
1588}
1589
1590/*----------------------------------------------------------------
1591 * hfa384x_drvr_disable
1592 *
1593 * Issues the disable command to stop communications on one of
1594 * the MACs 'ports'.  Only macport 0 is valid  for stations.
1595 * APs may also disable macports 1-6.  Only ports that have been
1596 * previously enabled may be disabled.
1597 *
1598 * Arguments:
1599 *      hw              device structure
1600 *      macport         MAC port number (host order)
1601 *
1602 * Returns:
1603 *      0               success
1604 *      >0              f/w reported failure - f/w status code
1605 *      <0              driver reported error (timeout|bad arg)
1606 *
1607 * Side effects:
1608 *
1609 * Call context:
1610 *      process
1611 *----------------------------------------------------------------
1612 */
1613int hfa384x_drvr_disable(struct hfa384x *hw, u16 macport)
1614{
1615        int result = 0;
1616
1617        if ((!hw->isap && macport != 0) ||
1618            (hw->isap && !(macport <= HFA384x_PORTID_MAX)) ||
1619            !(hw->port_enabled[macport])) {
1620                result = -EINVAL;
1621        } else {
1622                result = hfa384x_cmd_disable(hw, macport);
1623                if (result == 0)
1624                        hw->port_enabled[macport] = 0;
1625        }
1626        return result;
1627}
1628
1629/*----------------------------------------------------------------
1630 * hfa384x_drvr_enable
1631 *
1632 * Issues the enable command to enable communications on one of
1633 * the MACs 'ports'.  Only macport 0 is valid  for stations.
1634 * APs may also enable macports 1-6.  Only ports that are currently
1635 * disabled may be enabled.
1636 *
1637 * Arguments:
1638 *      hw              device structure
1639 *      macport         MAC port number
1640 *
1641 * Returns:
1642 *      0               success
1643 *      >0              f/w reported failure - f/w status code
1644 *      <0              driver reported error (timeout|bad arg)
1645 *
1646 * Side effects:
1647 *
1648 * Call context:
1649 *      process
1650 *----------------------------------------------------------------
1651 */
1652int hfa384x_drvr_enable(struct hfa384x *hw, u16 macport)
1653{
1654        int result = 0;
1655
1656        if ((!hw->isap && macport != 0) ||
1657            (hw->isap && !(macport <= HFA384x_PORTID_MAX)) ||
1658            (hw->port_enabled[macport])) {
1659                result = -EINVAL;
1660        } else {
1661                result = hfa384x_cmd_enable(hw, macport);
1662                if (result == 0)
1663                        hw->port_enabled[macport] = 1;
1664        }
1665        return result;
1666}
1667
1668/*----------------------------------------------------------------
1669 * hfa384x_drvr_flashdl_enable
1670 *
1671 * Begins the flash download state.  Checks to see that we're not
1672 * already in a download state and that a port isn't enabled.
1673 * Sets the download state and retrieves the flash download
1674 * buffer location, buffer size, and timeout length.
1675 *
1676 * Arguments:
1677 *      hw              device structure
1678 *
1679 * Returns:
1680 *      0               success
1681 *      >0              f/w reported error - f/w status code
1682 *      <0              driver reported error
1683 *
1684 * Side effects:
1685 *
1686 * Call context:
1687 *      process
1688 *----------------------------------------------------------------
1689 */
1690int hfa384x_drvr_flashdl_enable(struct hfa384x *hw)
1691{
1692        int result = 0;
1693        int i;
1694
1695        /* Check that a port isn't active */
1696        for (i = 0; i < HFA384x_PORTID_MAX; i++) {
1697                if (hw->port_enabled[i]) {
1698                        pr_debug("called when port enabled.\n");
1699                        return -EINVAL;
1700                }
1701        }
1702
1703        /* Check that we're not already in a download state */
1704        if (hw->dlstate != HFA384x_DLSTATE_DISABLED)
1705                return -EINVAL;
1706
1707        /* Retrieve the buffer loc&size and timeout */
1708        result = hfa384x_drvr_getconfig(hw, HFA384x_RID_DOWNLOADBUFFER,
1709                                        &hw->bufinfo, sizeof(hw->bufinfo));
1710        if (result)
1711                return result;
1712
1713        le16_to_cpus(&hw->bufinfo.page);
1714        le16_to_cpus(&hw->bufinfo.offset);
1715        le16_to_cpus(&hw->bufinfo.len);
1716        result = hfa384x_drvr_getconfig16(hw, HFA384x_RID_MAXLOADTIME,
1717                                          &hw->dltimeout);
1718        if (result)
1719                return result;
1720
1721        le16_to_cpus(&hw->dltimeout);
1722
1723        pr_debug("flashdl_enable\n");
1724
1725        hw->dlstate = HFA384x_DLSTATE_FLASHENABLED;
1726
1727        return result;
1728}
1729
1730/*----------------------------------------------------------------
1731 * hfa384x_drvr_flashdl_disable
1732 *
1733 * Ends the flash download state.  Note that this will cause the MAC
1734 * firmware to restart.
1735 *
1736 * Arguments:
1737 *      hw              device structure
1738 *
1739 * Returns:
1740 *      0               success
1741 *      >0              f/w reported error - f/w status code
1742 *      <0              driver reported error
1743 *
1744 * Side effects:
1745 *
1746 * Call context:
1747 *      process
1748 *----------------------------------------------------------------
1749 */
1750int hfa384x_drvr_flashdl_disable(struct hfa384x *hw)
1751{
1752        /* Check that we're already in the download state */
1753        if (hw->dlstate != HFA384x_DLSTATE_FLASHENABLED)
1754                return -EINVAL;
1755
1756        pr_debug("flashdl_enable\n");
1757
1758        /* There isn't much we can do at this point, so I don't */
1759        /*  bother  w/ the return value */
1760        hfa384x_cmd_download(hw, HFA384x_PROGMODE_DISABLE, 0, 0, 0);
1761        hw->dlstate = HFA384x_DLSTATE_DISABLED;
1762
1763        return 0;
1764}
1765
1766/*----------------------------------------------------------------
1767 * hfa384x_drvr_flashdl_write
1768 *
1769 * Performs a FLASH download of a chunk of data. First checks to see
1770 * that we're in the FLASH download state, then sets the download
1771 * mode, uses the aux functions to 1) copy the data to the flash
1772 * buffer, 2) sets the download 'write flash' mode, 3) readback and
1773 * compare.  Lather rinse, repeat as many times an necessary to get
1774 * all the given data into flash.
1775 * When all data has been written using this function (possibly
1776 * repeatedly), call drvr_flashdl_disable() to end the download state
1777 * and restart the MAC.
1778 *
1779 * Arguments:
1780 *      hw              device structure
1781 *      daddr           Card address to write to. (host order)
1782 *      buf             Ptr to data to write.
1783 *      len             Length of data (host order).
1784 *
1785 * Returns:
1786 *      0               success
1787 *      >0              f/w reported error - f/w status code
1788 *      <0              driver reported error
1789 *
1790 * Side effects:
1791 *
1792 * Call context:
1793 *      process
1794 *----------------------------------------------------------------
1795 */
1796int hfa384x_drvr_flashdl_write(struct hfa384x *hw, u32 daddr,
1797                               void *buf, u32 len)
1798{
1799        int result = 0;
1800        u32 dlbufaddr;
1801        int nburns;
1802        u32 burnlen;
1803        u32 burndaddr;
1804        u16 burnlo;
1805        u16 burnhi;
1806        int nwrites;
1807        u8 *writebuf;
1808        u16 writepage;
1809        u16 writeoffset;
1810        u32 writelen;
1811        int i;
1812        int j;
1813
1814        pr_debug("daddr=0x%08x len=%d\n", daddr, len);
1815
1816        /* Check that we're in the flash download state */
1817        if (hw->dlstate != HFA384x_DLSTATE_FLASHENABLED)
1818                return -EINVAL;
1819
1820        netdev_info(hw->wlandev->netdev,
1821                    "Download %d bytes to flash @0x%06x\n", len, daddr);
1822
1823        /* Convert to flat address for arithmetic */
1824        /* NOTE: dlbuffer RID stores the address in AUX format */
1825        dlbufaddr =
1826            HFA384x_ADDR_AUX_MKFLAT(hw->bufinfo.page, hw->bufinfo.offset);
1827        pr_debug("dlbuf.page=0x%04x dlbuf.offset=0x%04x dlbufaddr=0x%08x\n",
1828                 hw->bufinfo.page, hw->bufinfo.offset, dlbufaddr);
1829        /* Calculations to determine how many fills of the dlbuffer to do
1830         * and how many USB wmemreq's to do for each fill.  At this point
1831         * in time, the dlbuffer size and the wmemreq size are the same.
1832         * Therefore, nwrites should always be 1.  The extra complexity
1833         * here is a hedge against future changes.
1834         */
1835
1836        /* Figure out how many times to do the flash programming */
1837        nburns = len / hw->bufinfo.len;
1838        nburns += (len % hw->bufinfo.len) ? 1 : 0;
1839
1840        /* For each flash program cycle, how many USB wmemreq's are needed? */
1841        nwrites = hw->bufinfo.len / HFA384x_USB_RWMEM_MAXLEN;
1842        nwrites += (hw->bufinfo.len % HFA384x_USB_RWMEM_MAXLEN) ? 1 : 0;
1843
1844        /* For each burn */
1845        for (i = 0; i < nburns; i++) {
1846                /* Get the dest address and len */
1847                burnlen = (len - (hw->bufinfo.len * i)) > hw->bufinfo.len ?
1848                    hw->bufinfo.len : (len - (hw->bufinfo.len * i));
1849                burndaddr = daddr + (hw->bufinfo.len * i);
1850                burnlo = HFA384x_ADDR_CMD_MKOFF(burndaddr);
1851                burnhi = HFA384x_ADDR_CMD_MKPAGE(burndaddr);
1852
1853                netdev_info(hw->wlandev->netdev, "Writing %d bytes to flash @0x%06x\n",
1854                            burnlen, burndaddr);
1855
1856                /* Set the download mode */
1857                result = hfa384x_cmd_download(hw, HFA384x_PROGMODE_NV,
1858                                              burnlo, burnhi, burnlen);
1859                if (result) {
1860                        netdev_err(hw->wlandev->netdev,
1861                                   "download(NV,lo=%x,hi=%x,len=%x) cmd failed, result=%d. Aborting d/l\n",
1862                                   burnlo, burnhi, burnlen, result);
1863                        goto exit_proc;
1864                }
1865
1866                /* copy the data to the flash download buffer */
1867                for (j = 0; j < nwrites; j++) {
1868                        writebuf = buf +
1869                            (i * hw->bufinfo.len) +
1870                            (j * HFA384x_USB_RWMEM_MAXLEN);
1871
1872                        writepage = HFA384x_ADDR_CMD_MKPAGE(dlbufaddr +
1873                                                (j * HFA384x_USB_RWMEM_MAXLEN));
1874                        writeoffset = HFA384x_ADDR_CMD_MKOFF(dlbufaddr +
1875                                                (j * HFA384x_USB_RWMEM_MAXLEN));
1876
1877                        writelen = burnlen - (j * HFA384x_USB_RWMEM_MAXLEN);
1878                        writelen = writelen > HFA384x_USB_RWMEM_MAXLEN ?
1879                            HFA384x_USB_RWMEM_MAXLEN : writelen;
1880
1881                        result = hfa384x_dowmem(hw,
1882                                                writepage,
1883                                                writeoffset,
1884                                                writebuf, writelen);
1885                }
1886
1887                /* set the download 'write flash' mode */
1888                result = hfa384x_cmd_download(hw,
1889                                              HFA384x_PROGMODE_NVWRITE,
1890                                              0, 0, 0);
1891                if (result) {
1892                        netdev_err(hw->wlandev->netdev,
1893                                   "download(NVWRITE,lo=%x,hi=%x,len=%x) cmd failed, result=%d. Aborting d/l\n",
1894                                   burnlo, burnhi, burnlen, result);
1895                        goto exit_proc;
1896                }
1897
1898                /* TODO: We really should do a readback and compare. */
1899        }
1900
1901exit_proc:
1902
1903        /* Leave the firmware in the 'post-prog' mode.  flashdl_disable will */
1904        /*  actually disable programming mode.  Remember, that will cause the */
1905        /*  the firmware to effectively reset itself. */
1906
1907        return result;
1908}
1909
1910/*----------------------------------------------------------------
1911 * hfa384x_drvr_getconfig
1912 *
1913 * Performs the sequence necessary to read a config/info item.
1914 *
1915 * Arguments:
1916 *      hw              device structure
1917 *      rid             config/info record id (host order)
1918 *      buf             host side record buffer.  Upon return it will
1919 *                      contain the body portion of the record (minus the
1920 *                      RID and len).
1921 *      len             buffer length (in bytes, should match record length)
1922 *
1923 * Returns:
1924 *      0               success
1925 *      >0              f/w reported error - f/w status code
1926 *      <0              driver reported error
1927 *      -ENODATA        length mismatch between argument and retrieved
1928 *                      record.
1929 *
1930 * Side effects:
1931 *
1932 * Call context:
1933 *      process
1934 *----------------------------------------------------------------
1935 */
1936int hfa384x_drvr_getconfig(struct hfa384x *hw, u16 rid, void *buf, u16 len)
1937{
1938        return hfa384x_dorrid(hw, DOWAIT, rid, buf, len, NULL, NULL, NULL);
1939}
1940
1941/*----------------------------------------------------------------
1942 * hfa384x_drvr_setconfig_async
1943 *
1944 * Performs the sequence necessary to write a config/info item.
1945 *
1946 * Arguments:
1947 *       hw              device structure
1948 *       rid             config/info record id (in host order)
1949 *       buf             host side record buffer
1950 *       len             buffer length (in bytes)
1951 *       usercb          completion callback
1952 *       usercb_data     completion callback argument
1953 *
1954 * Returns:
1955 *       0               success
1956 *       >0              f/w reported error - f/w status code
1957 *       <0              driver reported error
1958 *
1959 * Side effects:
1960 *
1961 * Call context:
1962 *       process
1963 *----------------------------------------------------------------
1964 */
1965int
1966hfa384x_drvr_setconfig_async(struct hfa384x *hw,
1967                             u16 rid,
1968                             void *buf,
1969                             u16 len, ctlx_usercb_t usercb, void *usercb_data)
1970{
1971        return hfa384x_dowrid(hw, DOASYNC, rid, buf, len, hfa384x_cb_status,
1972                              usercb, usercb_data);
1973}
1974
1975/*----------------------------------------------------------------
1976 * hfa384x_drvr_ramdl_disable
1977 *
1978 * Ends the ram download state.
1979 *
1980 * Arguments:
1981 *      hw              device structure
1982 *
1983 * Returns:
1984 *      0               success
1985 *      >0              f/w reported error - f/w status code
1986 *      <0              driver reported error
1987 *
1988 * Side effects:
1989 *
1990 * Call context:
1991 *      process
1992 *----------------------------------------------------------------
1993 */
1994int hfa384x_drvr_ramdl_disable(struct hfa384x *hw)
1995{
1996        /* Check that we're already in the download state */
1997        if (hw->dlstate != HFA384x_DLSTATE_RAMENABLED)
1998                return -EINVAL;
1999
2000        pr_debug("ramdl_disable()\n");
2001
2002        /* There isn't much we can do at this point, so I don't */
2003        /*  bother  w/ the return value */
2004        hfa384x_cmd_download(hw, HFA384x_PROGMODE_DISABLE, 0, 0, 0);
2005        hw->dlstate = HFA384x_DLSTATE_DISABLED;
2006
2007        return 0;
2008}
2009
2010/*----------------------------------------------------------------
2011 * hfa384x_drvr_ramdl_enable
2012 *
2013 * Begins the ram download state.  Checks to see that we're not
2014 * already in a download state and that a port isn't enabled.
2015 * Sets the download state and calls cmd_download with the
2016 * ENABLE_VOLATILE subcommand and the exeaddr argument.
2017 *
2018 * Arguments:
2019 *      hw              device structure
2020 *      exeaddr         the card execution address that will be
2021 *                       jumped to when ramdl_disable() is called
2022 *                      (host order).
2023 *
2024 * Returns:
2025 *      0               success
2026 *      >0              f/w reported error - f/w status code
2027 *      <0              driver reported error
2028 *
2029 * Side effects:
2030 *
2031 * Call context:
2032 *      process
2033 *----------------------------------------------------------------
2034 */
2035int hfa384x_drvr_ramdl_enable(struct hfa384x *hw, u32 exeaddr)
2036{
2037        int result = 0;
2038        u16 lowaddr;
2039        u16 hiaddr;
2040        int i;
2041
2042        /* Check that a port isn't active */
2043        for (i = 0; i < HFA384x_PORTID_MAX; i++) {
2044                if (hw->port_enabled[i]) {
2045                        netdev_err(hw->wlandev->netdev,
2046                                   "Can't download with a macport enabled.\n");
2047                        return -EINVAL;
2048                }
2049        }
2050
2051        /* Check that we're not already in a download state */
2052        if (hw->dlstate != HFA384x_DLSTATE_DISABLED) {
2053                netdev_err(hw->wlandev->netdev,
2054                           "Download state not disabled.\n");
2055                return -EINVAL;
2056        }
2057
2058        pr_debug("ramdl_enable, exeaddr=0x%08x\n", exeaddr);
2059
2060        /* Call the download(1,addr) function */
2061        lowaddr = HFA384x_ADDR_CMD_MKOFF(exeaddr);
2062        hiaddr = HFA384x_ADDR_CMD_MKPAGE(exeaddr);
2063
2064        result = hfa384x_cmd_download(hw, HFA384x_PROGMODE_RAM,
2065                                      lowaddr, hiaddr, 0);
2066
2067        if (result == 0) {
2068                /* Set the download state */
2069                hw->dlstate = HFA384x_DLSTATE_RAMENABLED;
2070        } else {
2071                pr_debug("cmd_download(0x%04x, 0x%04x) failed, result=%d.\n",
2072                         lowaddr, hiaddr, result);
2073        }
2074
2075        return result;
2076}
2077
2078/*----------------------------------------------------------------
2079 * hfa384x_drvr_ramdl_write
2080 *
2081 * Performs a RAM download of a chunk of data. First checks to see
2082 * that we're in the RAM download state, then uses the [read|write]mem USB
2083 * commands to 1) copy the data, 2) readback and compare.  The download
2084 * state is unaffected.  When all data has been written using
2085 * this function, call drvr_ramdl_disable() to end the download state
2086 * and restart the MAC.
2087 *
2088 * Arguments:
2089 *      hw              device structure
2090 *      daddr           Card address to write to. (host order)
2091 *      buf             Ptr to data to write.
2092 *      len             Length of data (host order).
2093 *
2094 * Returns:
2095 *      0               success
2096 *      >0              f/w reported error - f/w status code
2097 *      <0              driver reported error
2098 *
2099 * Side effects:
2100 *
2101 * Call context:
2102 *      process
2103 *----------------------------------------------------------------
2104 */
2105int hfa384x_drvr_ramdl_write(struct hfa384x *hw, u32 daddr, void *buf, u32 len)
2106{
2107        int result = 0;
2108        int nwrites;
2109        u8 *data = buf;
2110        int i;
2111        u32 curraddr;
2112        u16 currpage;
2113        u16 curroffset;
2114        u16 currlen;
2115
2116        /* Check that we're in the ram download state */
2117        if (hw->dlstate != HFA384x_DLSTATE_RAMENABLED)
2118                return -EINVAL;
2119
2120        netdev_info(hw->wlandev->netdev, "Writing %d bytes to ram @0x%06x\n",
2121                    len, daddr);
2122
2123        /* How many dowmem calls?  */
2124        nwrites = len / HFA384x_USB_RWMEM_MAXLEN;
2125        nwrites += len % HFA384x_USB_RWMEM_MAXLEN ? 1 : 0;
2126
2127        /* Do blocking wmem's */
2128        for (i = 0; i < nwrites; i++) {
2129                /* make address args */
2130                curraddr = daddr + (i * HFA384x_USB_RWMEM_MAXLEN);
2131                currpage = HFA384x_ADDR_CMD_MKPAGE(curraddr);
2132                curroffset = HFA384x_ADDR_CMD_MKOFF(curraddr);
2133                currlen = len - (i * HFA384x_USB_RWMEM_MAXLEN);
2134                if (currlen > HFA384x_USB_RWMEM_MAXLEN)
2135                        currlen = HFA384x_USB_RWMEM_MAXLEN;
2136
2137                /* Do blocking ctlx */
2138                result = hfa384x_dowmem(hw,
2139                                        currpage,
2140                                        curroffset,
2141                                        data + (i * HFA384x_USB_RWMEM_MAXLEN),
2142                                        currlen);
2143
2144                if (result)
2145                        break;
2146
2147                /* TODO: We really should have a readback. */
2148        }
2149
2150        return result;
2151}
2152
2153/*----------------------------------------------------------------
2154 * hfa384x_drvr_readpda
2155 *
2156 * Performs the sequence to read the PDA space.  Note there is no
2157 * drvr_writepda() function.  Writing a PDA is
2158 * generally implemented by a calling component via calls to
2159 * cmd_download and writing to the flash download buffer via the
2160 * aux regs.
2161 *
2162 * Arguments:
2163 *      hw              device structure
2164 *      buf             buffer to store PDA in
2165 *      len             buffer length
2166 *
2167 * Returns:
2168 *      0               success
2169 *      >0              f/w reported error - f/w status code
2170 *      <0              driver reported error
2171 *      -ETIMEDOUT      timeout waiting for the cmd regs to become
2172 *                      available, or waiting for the control reg
2173 *                      to indicate the Aux port is enabled.
2174 *      -ENODATA        the buffer does NOT contain a valid PDA.
2175 *                      Either the card PDA is bad, or the auxdata
2176 *                      reads are giving us garbage.
2177 *
2178 *
2179 * Side effects:
2180 *
2181 * Call context:
2182 *      process or non-card interrupt.
2183 *----------------------------------------------------------------
2184 */
2185int hfa384x_drvr_readpda(struct hfa384x *hw, void *buf, unsigned int len)
2186{
2187        int result = 0;
2188        __le16 *pda = buf;
2189        int pdaok = 0;
2190        int morepdrs = 1;
2191        int currpdr = 0;        /* word offset of the current pdr */
2192        size_t i;
2193        u16 pdrlen;             /* pdr length in bytes, host order */
2194        u16 pdrcode;            /* pdr code, host order */
2195        u16 currpage;
2196        u16 curroffset;
2197        struct pdaloc {
2198                u32 cardaddr;
2199                u16 auxctl;
2200        } pdaloc[] = {
2201                {
2202                HFA3842_PDA_BASE, 0}, {
2203                HFA3841_PDA_BASE, 0}, {
2204                HFA3841_PDA_BOGUS_BASE, 0}
2205        };
2206
2207        /* Read the pda from each known address.  */
2208        for (i = 0; i < ARRAY_SIZE(pdaloc); i++) {
2209                /* Make address */
2210                currpage = HFA384x_ADDR_CMD_MKPAGE(pdaloc[i].cardaddr);
2211                curroffset = HFA384x_ADDR_CMD_MKOFF(pdaloc[i].cardaddr);
2212
2213                /* units of bytes */
2214                result = hfa384x_dormem(hw, currpage, curroffset, buf,
2215                                        len);
2216
2217                if (result) {
2218                        netdev_warn(hw->wlandev->netdev,
2219                                    "Read from index %zd failed, continuing\n",
2220                                    i);
2221                        continue;
2222                }
2223
2224                /* Test for garbage */
2225                pdaok = 1;      /* initially assume good */
2226                morepdrs = 1;
2227                while (pdaok && morepdrs) {
2228                        pdrlen = le16_to_cpu(pda[currpdr]) * 2;
2229                        pdrcode = le16_to_cpu(pda[currpdr + 1]);
2230                        /* Test the record length */
2231                        if (pdrlen > HFA384x_PDR_LEN_MAX || pdrlen == 0) {
2232                                netdev_err(hw->wlandev->netdev,
2233                                           "pdrlen invalid=%d\n", pdrlen);
2234                                pdaok = 0;
2235                                break;
2236                        }
2237                        /* Test the code */
2238                        if (!hfa384x_isgood_pdrcode(pdrcode)) {
2239                                netdev_err(hw->wlandev->netdev, "pdrcode invalid=%d\n",
2240                                           pdrcode);
2241                                pdaok = 0;
2242                                break;
2243                        }
2244                        /* Test for completion */
2245                        if (pdrcode == HFA384x_PDR_END_OF_PDA)
2246                                morepdrs = 0;
2247
2248                        /* Move to the next pdr (if necessary) */
2249                        if (morepdrs) {
2250                                /* note the access to pda[], need words here */
2251                                currpdr += le16_to_cpu(pda[currpdr]) + 1;
2252                        }
2253                }
2254                if (pdaok) {
2255                        netdev_info(hw->wlandev->netdev,
2256                                    "PDA Read from 0x%08x in %s space.\n",
2257                                    pdaloc[i].cardaddr,
2258                                    pdaloc[i].auxctl == 0 ? "EXTDS" :
2259                                    pdaloc[i].auxctl == 1 ? "NV" :
2260                                    pdaloc[i].auxctl == 2 ? "PHY" :
2261                                    pdaloc[i].auxctl == 3 ? "ICSRAM" :
2262                                    "<bogus auxctl>");
2263                        break;
2264                }
2265        }
2266        result = pdaok ? 0 : -ENODATA;
2267
2268        if (result)
2269                pr_debug("Failure: pda is not okay\n");
2270
2271        return result;
2272}
2273
2274/*----------------------------------------------------------------
2275 * hfa384x_drvr_setconfig
2276 *
2277 * Performs the sequence necessary to write a config/info item.
2278 *
2279 * Arguments:
2280 *      hw              device structure
2281 *      rid             config/info record id (in host order)
2282 *      buf             host side record buffer
2283 *      len             buffer length (in bytes)
2284 *
2285 * Returns:
2286 *      0               success
2287 *      >0              f/w reported error - f/w status code
2288 *      <0              driver reported error
2289 *
2290 * Side effects:
2291 *
2292 * Call context:
2293 *      process
2294 *----------------------------------------------------------------
2295 */
2296int hfa384x_drvr_setconfig(struct hfa384x *hw, u16 rid, void *buf, u16 len)
2297{
2298        return hfa384x_dowrid(hw, DOWAIT, rid, buf, len, NULL, NULL, NULL);
2299}
2300
2301/*----------------------------------------------------------------
2302 * hfa384x_drvr_start
2303 *
2304 * Issues the MAC initialize command, sets up some data structures,
2305 * and enables the interrupts.  After this function completes, the
2306 * low-level stuff should be ready for any/all commands.
2307 *
2308 * Arguments:
2309 *      hw              device structure
2310 * Returns:
2311 *      0               success
2312 *      >0              f/w reported error - f/w status code
2313 *      <0              driver reported error
2314 *
2315 * Side effects:
2316 *
2317 * Call context:
2318 *      process
2319 *----------------------------------------------------------------
2320 */
2321int hfa384x_drvr_start(struct hfa384x *hw)
2322{
2323        int result, result1, result2;
2324        u16 status;
2325
2326        might_sleep();
2327
2328        /* Clear endpoint stalls - but only do this if the endpoint
2329         * is showing a stall status. Some prism2 cards seem to behave
2330         * badly if a clear_halt is called when the endpoint is already
2331         * ok
2332         */
2333        result =
2334            usb_get_std_status(hw->usb, USB_RECIP_ENDPOINT, hw->endp_in,
2335                               &status);
2336        if (result < 0) {
2337                netdev_err(hw->wlandev->netdev, "Cannot get bulk in endpoint status.\n");
2338                goto done;
2339        }
2340        if ((status == 1) && usb_clear_halt(hw->usb, hw->endp_in))
2341                netdev_err(hw->wlandev->netdev, "Failed to reset bulk in endpoint.\n");
2342
2343        result =
2344            usb_get_std_status(hw->usb, USB_RECIP_ENDPOINT, hw->endp_out,
2345                               &status);
2346        if (result < 0) {
2347                netdev_err(hw->wlandev->netdev, "Cannot get bulk out endpoint status.\n");
2348                goto done;
2349        }
2350        if ((status == 1) && usb_clear_halt(hw->usb, hw->endp_out))
2351                netdev_err(hw->wlandev->netdev, "Failed to reset bulk out endpoint.\n");
2352
2353        /* Synchronous unlink, in case we're trying to restart the driver */
2354        usb_kill_urb(&hw->rx_urb);
2355
2356        /* Post the IN urb */
2357        result = submit_rx_urb(hw, GFP_KERNEL);
2358        if (result != 0) {
2359                netdev_err(hw->wlandev->netdev,
2360                           "Fatal, failed to submit RX URB, result=%d\n",
2361                           result);
2362                goto done;
2363        }
2364
2365        /* Call initialize twice, with a 1 second sleep in between.
2366         * This is a nasty work-around since many prism2 cards seem to
2367         * need time to settle after an init from cold. The second
2368         * call to initialize in theory is not necessary - but we call
2369         * it anyway as a double insurance policy:
2370         * 1) If the first init should fail, the second may well succeed
2371         *    and the card can still be used
2372         * 2) It helps ensures all is well with the card after the first
2373         *    init and settle time.
2374         */
2375        result1 = hfa384x_cmd_initialize(hw);
2376        msleep(1000);
2377        result = hfa384x_cmd_initialize(hw);
2378        result2 = result;
2379        if (result1 != 0) {
2380                if (result2 != 0) {
2381                        netdev_err(hw->wlandev->netdev,
2382                                   "cmd_initialize() failed on two attempts, results %d and %d\n",
2383                                   result1, result2);
2384                        usb_kill_urb(&hw->rx_urb);
2385                        goto done;
2386                } else {
2387                        pr_debug("First cmd_initialize() failed (result %d),\n",
2388                                 result1);
2389                        pr_debug("but second attempt succeeded. All should be ok\n");
2390                }
2391        } else if (result2 != 0) {
2392                netdev_warn(hw->wlandev->netdev, "First cmd_initialize() succeeded, but second attempt failed (result=%d)\n",
2393                            result2);
2394                netdev_warn(hw->wlandev->netdev,
2395                            "Most likely the card will be functional\n");
2396                goto done;
2397        }
2398
2399        hw->state = HFA384x_STATE_RUNNING;
2400
2401done:
2402        return result;
2403}
2404
2405/*----------------------------------------------------------------
2406 * hfa384x_drvr_stop
2407 *
2408 * Shuts down the MAC to the point where it is safe to unload the
2409 * driver.  Any subsystem that may be holding a data or function
2410 * ptr into the driver must be cleared/deinitialized.
2411 *
2412 * Arguments:
2413 *      hw              device structure
2414 * Returns:
2415 *      0               success
2416 *      >0              f/w reported error - f/w status code
2417 *      <0              driver reported error
2418 *
2419 * Side effects:
2420 *
2421 * Call context:
2422 *      process
2423 *----------------------------------------------------------------
2424 */
2425int hfa384x_drvr_stop(struct hfa384x *hw)
2426{
2427        int i;
2428
2429        might_sleep();
2430
2431        /* There's no need for spinlocks here. The USB "disconnect"
2432         * function sets this "removed" flag and then calls us.
2433         */
2434        if (!hw->wlandev->hwremoved) {
2435                /* Call initialize to leave the MAC in its 'reset' state */
2436                hfa384x_cmd_initialize(hw);
2437
2438                /* Cancel the rxurb */
2439                usb_kill_urb(&hw->rx_urb);
2440        }
2441
2442        hw->link_status = HFA384x_LINK_NOTCONNECTED;
2443        hw->state = HFA384x_STATE_INIT;
2444
2445        del_timer_sync(&hw->commsqual_timer);
2446
2447        /* Clear all the port status */
2448        for (i = 0; i < HFA384x_NUMPORTS_MAX; i++)
2449                hw->port_enabled[i] = 0;
2450
2451        return 0;
2452}
2453
2454/*----------------------------------------------------------------
2455 * hfa384x_drvr_txframe
2456 *
2457 * Takes a frame from prism2sta and queues it for transmission.
2458 *
2459 * Arguments:
2460 *      hw              device structure
2461 *      skb             packet buffer struct.  Contains an 802.11
2462 *                      data frame.
2463 *       p80211_hdr      points to the 802.11 header for the packet.
2464 * Returns:
2465 *      0               Success and more buffs available
2466 *      1               Success but no more buffs
2467 *      2               Allocation failure
2468 *      4               Buffer full or queue busy
2469 *
2470 * Side effects:
2471 *
2472 * Call context:
2473 *      interrupt
2474 *----------------------------------------------------------------
2475 */
2476int hfa384x_drvr_txframe(struct hfa384x *hw, struct sk_buff *skb,
2477                         union p80211_hdr *p80211_hdr,
2478                         struct p80211_metawep *p80211_wep)
2479{
2480        int usbpktlen = sizeof(struct hfa384x_tx_frame);
2481        int result;
2482        int ret;
2483        char *ptr;
2484
2485        if (hw->tx_urb.status == -EINPROGRESS) {
2486                netdev_warn(hw->wlandev->netdev, "TX URB already in use\n");
2487                result = 3;
2488                goto exit;
2489        }
2490
2491        /* Build Tx frame structure */
2492        /* Set up the control field */
2493        memset(&hw->txbuff.txfrm.desc, 0, sizeof(hw->txbuff.txfrm.desc));
2494
2495        /* Setup the usb type field */
2496        hw->txbuff.type = cpu_to_le16(HFA384x_USB_TXFRM);
2497
2498        /* Set up the sw_support field to identify this frame */
2499        hw->txbuff.txfrm.desc.sw_support = 0x0123;
2500
2501/* Tx complete and Tx exception disable per dleach.  Might be causing
2502 * buf depletion
2503 */
2504/* #define DOEXC  SLP -- doboth breaks horribly under load, doexc less so. */
2505#if defined(DOBOTH)
2506        hw->txbuff.txfrm.desc.tx_control =
2507            HFA384x_TX_MACPORT_SET(0) | HFA384x_TX_STRUCTYPE_SET(1) |
2508            HFA384x_TX_TXEX_SET(1) | HFA384x_TX_TXOK_SET(1);
2509#elif defined(DOEXC)
2510        hw->txbuff.txfrm.desc.tx_control =
2511            HFA384x_TX_MACPORT_SET(0) | HFA384x_TX_STRUCTYPE_SET(1) |
2512            HFA384x_TX_TXEX_SET(1) | HFA384x_TX_TXOK_SET(0);
2513#else
2514        hw->txbuff.txfrm.desc.tx_control =
2515            HFA384x_TX_MACPORT_SET(0) | HFA384x_TX_STRUCTYPE_SET(1) |
2516            HFA384x_TX_TXEX_SET(0) | HFA384x_TX_TXOK_SET(0);
2517#endif
2518        cpu_to_le16s(&hw->txbuff.txfrm.desc.tx_control);
2519
2520        /* copy the header over to the txdesc */
2521        memcpy(&hw->txbuff.txfrm.desc.frame_control, p80211_hdr,
2522               sizeof(union p80211_hdr));
2523
2524        /* if we're using host WEP, increase size by IV+ICV */
2525        if (p80211_wep->data) {
2526                hw->txbuff.txfrm.desc.data_len = cpu_to_le16(skb->len + 8);
2527                usbpktlen += 8;
2528        } else {
2529                hw->txbuff.txfrm.desc.data_len = cpu_to_le16(skb->len);
2530        }
2531
2532        usbpktlen += skb->len;
2533
2534        /* copy over the WEP IV if we are using host WEP */
2535        ptr = hw->txbuff.txfrm.data;
2536        if (p80211_wep->data) {
2537                memcpy(ptr, p80211_wep->iv, sizeof(p80211_wep->iv));
2538                ptr += sizeof(p80211_wep->iv);
2539                memcpy(ptr, p80211_wep->data, skb->len);
2540        } else {
2541                memcpy(ptr, skb->data, skb->len);
2542        }
2543        /* copy over the packet data */
2544        ptr += skb->len;
2545
2546        /* copy over the WEP ICV if we are using host WEP */
2547        if (p80211_wep->data)
2548                memcpy(ptr, p80211_wep->icv, sizeof(p80211_wep->icv));
2549
2550        /* Send the USB packet */
2551        usb_fill_bulk_urb(&hw->tx_urb, hw->usb,
2552                          hw->endp_out,
2553                          &hw->txbuff, ROUNDUP64(usbpktlen),
2554                          hfa384x_usbout_callback, hw->wlandev);
2555        hw->tx_urb.transfer_flags |= USB_QUEUE_BULK;
2556
2557        result = 1;
2558        ret = submit_tx_urb(hw, &hw->tx_urb, GFP_ATOMIC);
2559        if (ret != 0) {
2560                netdev_err(hw->wlandev->netdev,
2561                           "submit_tx_urb() failed, error=%d\n", ret);
2562                result = 3;
2563        }
2564
2565exit:
2566        return result;
2567}
2568
2569void hfa384x_tx_timeout(struct wlandevice *wlandev)
2570{
2571        struct hfa384x *hw = wlandev->priv;
2572        unsigned long flags;
2573
2574        spin_lock_irqsave(&hw->ctlxq.lock, flags);
2575
2576        if (!hw->wlandev->hwremoved) {
2577                int sched;
2578
2579                sched = !test_and_set_bit(WORK_TX_HALT, &hw->usb_flags);
2580                sched |= !test_and_set_bit(WORK_RX_HALT, &hw->usb_flags);
2581                if (sched)
2582                        schedule_work(&hw->usb_work);
2583        }
2584
2585        spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
2586}
2587
2588/*----------------------------------------------------------------
2589 * hfa384x_usbctlx_reaper_task
2590 *
2591 * Tasklet to delete dead CTLX objects
2592 *
2593 * Arguments:
2594 *      data    ptr to a struct hfa384x
2595 *
2596 * Returns:
2597 *
2598 * Call context:
2599 *      Interrupt
2600 *----------------------------------------------------------------
2601 */
2602static void hfa384x_usbctlx_reaper_task(unsigned long data)
2603{
2604        struct hfa384x *hw = (struct hfa384x *)data;
2605        struct hfa384x_usbctlx *ctlx, *temp;
2606        unsigned long flags;
2607
2608        spin_lock_irqsave(&hw->ctlxq.lock, flags);
2609
2610        /* This list is guaranteed to be empty if someone
2611         * has unplugged the adapter.
2612         */
2613        list_for_each_entry_safe(ctlx, temp, &hw->ctlxq.reapable, list) {
2614                list_del(&ctlx->list);
2615                kfree(ctlx);
2616        }
2617
2618        spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
2619}
2620
2621/*----------------------------------------------------------------
2622 * hfa384x_usbctlx_completion_task
2623 *
2624 * Tasklet to call completion handlers for returned CTLXs
2625 *
2626 * Arguments:
2627 *      data    ptr to struct hfa384x
2628 *
2629 * Returns:
2630 *      Nothing
2631 *
2632 * Call context:
2633 *      Interrupt
2634 *----------------------------------------------------------------
2635 */
2636static void hfa384x_usbctlx_completion_task(unsigned long data)
2637{
2638        struct hfa384x *hw = (struct hfa384x *)data;
2639        struct hfa384x_usbctlx *ctlx, *temp;
2640        unsigned long flags;
2641
2642        int reap = 0;
2643
2644        spin_lock_irqsave(&hw->ctlxq.lock, flags);
2645
2646        /* This list is guaranteed to be empty if someone
2647         * has unplugged the adapter ...
2648         */
2649        list_for_each_entry_safe(ctlx, temp, &hw->ctlxq.completing, list) {
2650                /* Call the completion function that this
2651                 * command was assigned, assuming it has one.
2652                 */
2653                if (ctlx->cmdcb) {
2654                        spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
2655                        ctlx->cmdcb(hw, ctlx);
2656                        spin_lock_irqsave(&hw->ctlxq.lock, flags);
2657
2658                        /* Make sure we don't try and complete
2659                         * this CTLX more than once!
2660                         */
2661                        ctlx->cmdcb = NULL;
2662
2663                        /* Did someone yank the adapter out
2664                         * while our list was (briefly) unlocked?
2665                         */
2666                        if (hw->wlandev->hwremoved) {
2667                                reap = 0;
2668                                break;
2669                        }
2670                }
2671
2672                /*
2673                 * "Reapable" CTLXs are ones which don't have any
2674                 * threads waiting for them to die. Hence they must
2675                 * be delivered to The Reaper!
2676                 */
2677                if (ctlx->reapable) {
2678                        /* Move the CTLX off the "completing" list (hopefully)
2679                         * on to the "reapable" list where the reaper task
2680                         * can find it. And "reapable" means that this CTLX
2681                         * isn't sitting on a wait-queue somewhere.
2682                         */
2683                        list_move_tail(&ctlx->list, &hw->ctlxq.reapable);
2684                        reap = 1;
2685                }
2686
2687                complete(&ctlx->done);
2688        }
2689        spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
2690
2691        if (reap)
2692                tasklet_schedule(&hw->reaper_bh);
2693}
2694
2695/*----------------------------------------------------------------
2696 * unlocked_usbctlx_cancel_async
2697 *
2698 * Mark the CTLX dead asynchronously, and ensure that the
2699 * next command on the queue is run afterwards.
2700 *
2701 * Arguments:
2702 *      hw      ptr to the struct hfa384x structure
2703 *      ctlx    ptr to a CTLX structure
2704 *
2705 * Returns:
2706 *      0       the CTLX's URB is inactive
2707 * -EINPROGRESS the URB is currently being unlinked
2708 *
2709 * Call context:
2710 *      Either process or interrupt, but presumably interrupt
2711 *----------------------------------------------------------------
2712 */
2713static int unlocked_usbctlx_cancel_async(struct hfa384x *hw,
2714                                         struct hfa384x_usbctlx *ctlx)
2715{
2716        int ret;
2717
2718        /*
2719         * Try to delete the URB containing our request packet.
2720         * If we succeed, then its completion handler will be
2721         * called with a status of -ECONNRESET.
2722         */
2723        hw->ctlx_urb.transfer_flags |= URB_ASYNC_UNLINK;
2724        ret = usb_unlink_urb(&hw->ctlx_urb);
2725
2726        if (ret != -EINPROGRESS) {
2727                /*
2728                 * The OUT URB had either already completed
2729                 * or was still in the pending queue, so the
2730                 * URB's completion function will not be called.
2731                 * We will have to complete the CTLX ourselves.
2732                 */
2733                ctlx->state = CTLX_REQ_FAILED;
2734                unlocked_usbctlx_complete(hw, ctlx);
2735                ret = 0;
2736        }
2737
2738        return ret;
2739}
2740
2741/*----------------------------------------------------------------
2742 * unlocked_usbctlx_complete
2743 *
2744 * A CTLX has completed.  It may have been successful, it may not
2745 * have been. At this point, the CTLX should be quiescent.  The URBs
2746 * aren't active and the timers should have been stopped.
2747 *
2748 * The CTLX is migrated to the "completing" queue, and the completing
2749 * tasklet is scheduled.
2750 *
2751 * Arguments:
2752 *      hw              ptr to a struct hfa384x structure
2753 *      ctlx            ptr to a ctlx structure
2754 *
2755 * Returns:
2756 *      nothing
2757 *
2758 * Side effects:
2759 *
2760 * Call context:
2761 *      Either, assume interrupt
2762 *----------------------------------------------------------------
2763 */
2764static void unlocked_usbctlx_complete(struct hfa384x *hw,
2765                                      struct hfa384x_usbctlx *ctlx)
2766{
2767        /* Timers have been stopped, and ctlx should be in
2768         * a terminal state. Retire it from the "active"
2769         * queue.
2770         */
2771        list_move_tail(&ctlx->list, &hw->ctlxq.completing);
2772        tasklet_schedule(&hw->completion_bh);
2773
2774        switch (ctlx->state) {
2775        case CTLX_COMPLETE:
2776        case CTLX_REQ_FAILED:
2777                /* This are the correct terminating states. */
2778                break;
2779
2780        default:
2781                netdev_err(hw->wlandev->netdev, "CTLX[%d] not in a terminating state(%s)\n",
2782                           le16_to_cpu(ctlx->outbuf.type),
2783                           ctlxstr(ctlx->state));
2784                break;
2785        }                       /* switch */
2786}
2787
2788/*----------------------------------------------------------------
2789 * hfa384x_usbctlxq_run
2790 *
2791 * Checks to see if the head item is running.  If not, starts it.
2792 *
2793 * Arguments:
2794 *      hw      ptr to struct hfa384x
2795 *
2796 * Returns:
2797 *      nothing
2798 *
2799 * Side effects:
2800 *
2801 * Call context:
2802 *      any
2803 *----------------------------------------------------------------
2804 */
2805static void hfa384x_usbctlxq_run(struct hfa384x *hw)
2806{
2807        unsigned long flags;
2808
2809        /* acquire lock */
2810        spin_lock_irqsave(&hw->ctlxq.lock, flags);
2811
2812        /* Only one active CTLX at any one time, because there's no
2813         * other (reliable) way to match the response URB to the
2814         * correct CTLX.
2815         *
2816         * Don't touch any of these CTLXs if the hardware
2817         * has been removed or the USB subsystem is stalled.
2818         */
2819        if (!list_empty(&hw->ctlxq.active) ||
2820            test_bit(WORK_TX_HALT, &hw->usb_flags) || hw->wlandev->hwremoved)
2821                goto unlock;
2822
2823        while (!list_empty(&hw->ctlxq.pending)) {
2824                struct hfa384x_usbctlx *head;
2825                int result;
2826
2827                /* This is the first pending command */
2828                head = list_entry(hw->ctlxq.pending.next,
2829                                  struct hfa384x_usbctlx, list);
2830
2831                /* We need to split this off to avoid a race condition */
2832                list_move_tail(&head->list, &hw->ctlxq.active);
2833
2834                /* Fill the out packet */
2835                usb_fill_bulk_urb(&hw->ctlx_urb, hw->usb,
2836                                  hw->endp_out,
2837                                  &head->outbuf, ROUNDUP64(head->outbufsize),
2838                                  hfa384x_ctlxout_callback, hw);
2839                hw->ctlx_urb.transfer_flags |= USB_QUEUE_BULK;
2840
2841                /* Now submit the URB and update the CTLX's state */
2842                result = usb_submit_urb(&hw->ctlx_urb, GFP_ATOMIC);
2843                if (result == 0) {
2844                        /* This CTLX is now running on the active queue */
2845                        head->state = CTLX_REQ_SUBMITTED;
2846
2847                        /* Start the OUT wait timer */
2848                        hw->req_timer_done = 0;
2849                        hw->reqtimer.expires = jiffies + HZ;
2850                        add_timer(&hw->reqtimer);
2851
2852                        /* Start the IN wait timer */
2853                        hw->resp_timer_done = 0;
2854                        hw->resptimer.expires = jiffies + 2 * HZ;
2855                        add_timer(&hw->resptimer);
2856
2857                        break;
2858                }
2859
2860                if (result == -EPIPE) {
2861                        /* The OUT pipe needs resetting, so put
2862                         * this CTLX back in the "pending" queue
2863                         * and schedule a reset ...
2864                         */
2865                        netdev_warn(hw->wlandev->netdev,
2866                                    "%s tx pipe stalled: requesting reset\n",
2867                                    hw->wlandev->netdev->name);
2868                        list_move(&head->list, &hw->ctlxq.pending);
2869                        set_bit(WORK_TX_HALT, &hw->usb_flags);
2870                        schedule_work(&hw->usb_work);
2871                        break;
2872                }
2873
2874                if (result == -ESHUTDOWN) {
2875                        netdev_warn(hw->wlandev->netdev, "%s urb shutdown!\n",
2876                                    hw->wlandev->netdev->name);
2877                        break;
2878                }
2879
2880                netdev_err(hw->wlandev->netdev, "Failed to submit CTLX[%d]: error=%d\n",
2881                           le16_to_cpu(head->outbuf.type), result);
2882                unlocked_usbctlx_complete(hw, head);
2883        }                       /* while */
2884
2885unlock:
2886        spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
2887}
2888
2889/*----------------------------------------------------------------
2890 * hfa384x_usbin_callback
2891 *
2892 * Callback for URBs on the BULKIN endpoint.
2893 *
2894 * Arguments:
2895 *      urb             ptr to the completed urb
2896 *
2897 * Returns:
2898 *      nothing
2899 *
2900 * Side effects:
2901 *
2902 * Call context:
2903 *      interrupt
2904 *----------------------------------------------------------------
2905 */
2906static void hfa384x_usbin_callback(struct urb *urb)
2907{
2908        struct wlandevice *wlandev = urb->context;
2909        struct hfa384x *hw;
2910        union hfa384x_usbin *usbin;
2911        struct sk_buff *skb = NULL;
2912        int result;
2913        int urb_status;
2914        u16 type;
2915
2916        enum USBIN_ACTION {
2917                HANDLE,
2918                RESUBMIT,
2919                ABORT
2920        } action;
2921
2922        if (!wlandev || !wlandev->netdev || wlandev->hwremoved)
2923                goto exit;
2924
2925        hw = wlandev->priv;
2926        if (!hw)
2927                goto exit;
2928
2929        skb = hw->rx_urb_skb;
2930        if (!skb || (skb->data != urb->transfer_buffer)) {
2931                WARN_ON(1);
2932                return;
2933        }
2934
2935        hw->rx_urb_skb = NULL;
2936
2937        /* Check for error conditions within the URB */
2938        switch (urb->status) {
2939        case 0:
2940                action = HANDLE;
2941
2942                /* Check for short packet */
2943                if (urb->actual_length == 0) {
2944                        wlandev->netdev->stats.rx_errors++;
2945                        wlandev->netdev->stats.rx_length_errors++;
2946                        action = RESUBMIT;
2947                }
2948                break;
2949
2950        case -EPIPE:
2951                netdev_warn(hw->wlandev->netdev, "%s rx pipe stalled: requesting reset\n",
2952                            wlandev->netdev->name);
2953                if (!test_and_set_bit(WORK_RX_HALT, &hw->usb_flags))
2954                        schedule_work(&hw->usb_work);
2955                wlandev->netdev->stats.rx_errors++;
2956                action = ABORT;
2957                break;
2958
2959        case -EILSEQ:
2960        case -ETIMEDOUT:
2961        case -EPROTO:
2962                if (!test_and_set_bit(THROTTLE_RX, &hw->usb_flags) &&
2963                    !timer_pending(&hw->throttle)) {
2964                        mod_timer(&hw->throttle, jiffies + THROTTLE_JIFFIES);
2965                }
2966                wlandev->netdev->stats.rx_errors++;
2967                action = ABORT;
2968                break;
2969
2970        case -EOVERFLOW:
2971                wlandev->netdev->stats.rx_over_errors++;
2972                action = RESUBMIT;
2973                break;
2974
2975        case -ENODEV:
2976        case -ESHUTDOWN:
2977                pr_debug("status=%d, device removed.\n", urb->status);
2978                action = ABORT;
2979                break;
2980
2981        case -ENOENT:
2982        case -ECONNRESET:
2983                pr_debug("status=%d, urb explicitly unlinked.\n", urb->status);
2984                action = ABORT;
2985                break;
2986
2987        default:
2988                pr_debug("urb status=%d, transfer flags=0x%x\n",
2989                         urb->status, urb->transfer_flags);
2990                wlandev->netdev->stats.rx_errors++;
2991                action = RESUBMIT;
2992                break;
2993        }
2994
2995        /* Save values from the RX URB before reposting overwrites it. */
2996        urb_status = urb->status;
2997        usbin = (union hfa384x_usbin *)urb->transfer_buffer;
2998
2999        if (action != ABORT) {
3000                /* Repost the RX URB */
3001                result = submit_rx_urb(hw, GFP_ATOMIC);
3002
3003                if (result != 0) {
3004                        netdev_err(hw->wlandev->netdev,
3005                                   "Fatal, failed to resubmit rx_urb. error=%d\n",
3006                                   result);
3007                }
3008        }
3009
3010        /* Handle any USB-IN packet */
3011        /* Note: the check of the sw_support field, the type field doesn't
3012         *       have bit 12 set like the docs suggest.
3013         */
3014        type = le16_to_cpu(usbin->type);
3015        if (HFA384x_USB_ISRXFRM(type)) {
3016                if (action == HANDLE) {
3017                        if (usbin->txfrm.desc.sw_support == 0x0123) {
3018                                hfa384x_usbin_txcompl(wlandev, usbin);
3019                        } else {
3020                                skb_put(skb, sizeof(*usbin));
3021                                hfa384x_usbin_rx(wlandev, skb);
3022                                skb = NULL;
3023                        }
3024                }
3025                goto exit;
3026        }
3027        if (HFA384x_USB_ISTXFRM(type)) {
3028                if (action == HANDLE)
3029                        hfa384x_usbin_txcompl(wlandev, usbin);
3030                goto exit;
3031        }
3032        switch (type) {
3033        case HFA384x_USB_INFOFRM:
3034                if (action == ABORT)
3035                        goto exit;
3036                if (action == HANDLE)
3037                        hfa384x_usbin_info(wlandev, usbin);
3038                break;
3039
3040        case HFA384x_USB_CMDRESP:
3041        case HFA384x_USB_WRIDRESP:
3042        case HFA384x_USB_RRIDRESP:
3043        case HFA384x_USB_WMEMRESP:
3044        case HFA384x_USB_RMEMRESP:
3045                /* ALWAYS, ALWAYS, ALWAYS handle this CTLX!!!! */
3046                hfa384x_usbin_ctlx(hw, usbin, urb_status);
3047                break;
3048
3049        case HFA384x_USB_BUFAVAIL:
3050                pr_debug("Received BUFAVAIL packet, frmlen=%d\n",
3051                         usbin->bufavail.frmlen);
3052                break;
3053
3054        case HFA384x_USB_ERROR:
3055                pr_debug("Received USB_ERROR packet, errortype=%d\n",
3056                         usbin->usberror.errortype);
3057                break;
3058
3059        default:
3060                pr_debug("Unrecognized USBIN packet, type=%x, status=%d\n",
3061                         usbin->type, urb_status);
3062                break;
3063        }                       /* switch */
3064
3065exit:
3066
3067        if (skb)
3068                dev_kfree_skb(skb);
3069}
3070
3071/*----------------------------------------------------------------
3072 * hfa384x_usbin_ctlx
3073 *
3074 * We've received a URB containing a Prism2 "response" message.
3075 * This message needs to be matched up with a CTLX on the active
3076 * queue and our state updated accordingly.
3077 *
3078 * Arguments:
3079 *      hw              ptr to struct hfa384x
3080 *      usbin           ptr to USB IN packet
3081 *      urb_status      status of this Bulk-In URB
3082 *
3083 * Returns:
3084 *      nothing
3085 *
3086 * Side effects:
3087 *
3088 * Call context:
3089 *      interrupt
3090 *----------------------------------------------------------------
3091 */
3092static void hfa384x_usbin_ctlx(struct hfa384x *hw, union hfa384x_usbin *usbin,
3093                               int urb_status)
3094{
3095        struct hfa384x_usbctlx *ctlx;
3096        int run_queue = 0;
3097        unsigned long flags;
3098
3099retry:
3100        spin_lock_irqsave(&hw->ctlxq.lock, flags);
3101
3102        /* There can be only one CTLX on the active queue
3103         * at any one time, and this is the CTLX that the
3104         * timers are waiting for.
3105         */
3106        if (list_empty(&hw->ctlxq.active))
3107                goto unlock;
3108
3109        /* Remove the "response timeout". It's possible that
3110         * we are already too late, and that the timeout is
3111         * already running. And that's just too bad for us,
3112         * because we could lose our CTLX from the active
3113         * queue here ...
3114         */
3115        if (del_timer(&hw->resptimer) == 0) {
3116                if (hw->resp_timer_done == 0) {
3117                        spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3118                        goto retry;
3119                }
3120        } else {
3121                hw->resp_timer_done = 1;
3122        }
3123
3124        ctlx = get_active_ctlx(hw);
3125
3126        if (urb_status != 0) {
3127                /*
3128                 * Bad CTLX, so get rid of it. But we only
3129                 * remove it from the active queue if we're no
3130                 * longer expecting the OUT URB to complete.
3131                 */
3132                if (unlocked_usbctlx_cancel_async(hw, ctlx) == 0)
3133                        run_queue = 1;
3134        } else {
3135                const __le16 intype = (usbin->type & ~cpu_to_le16(0x8000));
3136
3137                /*
3138                 * Check that our message is what we're expecting ...
3139                 */
3140                if (ctlx->outbuf.type != intype) {
3141                        netdev_warn(hw->wlandev->netdev,
3142                                    "Expected IN[%d], received IN[%d] - ignored.\n",
3143                                    le16_to_cpu(ctlx->outbuf.type),
3144                                    le16_to_cpu(intype));
3145                        goto unlock;
3146                }
3147
3148                /* This URB has succeeded, so grab the data ... */
3149                memcpy(&ctlx->inbuf, usbin, sizeof(ctlx->inbuf));
3150
3151                switch (ctlx->state) {
3152                case CTLX_REQ_SUBMITTED:
3153                        /*
3154                         * We have received our response URB before
3155                         * our request has been acknowledged. Odd,
3156                         * but our OUT URB is still alive...
3157                         */
3158                        pr_debug("Causality violation: please reboot Universe\n");
3159                        ctlx->state = CTLX_RESP_COMPLETE;
3160                        break;
3161
3162                case CTLX_REQ_COMPLETE:
3163                        /*
3164                         * This is the usual path: our request
3165                         * has already been acknowledged, and
3166                         * now we have received the reply too.
3167                         */
3168                        ctlx->state = CTLX_COMPLETE;
3169                        unlocked_usbctlx_complete(hw, ctlx);
3170                        run_queue = 1;
3171                        break;
3172
3173                default:
3174                        /*
3175                         * Throw this CTLX away ...
3176                         */
3177                        netdev_err(hw->wlandev->netdev,
3178                                   "Matched IN URB, CTLX[%d] in invalid state(%s). Discarded.\n",
3179                                   le16_to_cpu(ctlx->outbuf.type),
3180                                   ctlxstr(ctlx->state));
3181                        if (unlocked_usbctlx_cancel_async(hw, ctlx) == 0)
3182                                run_queue = 1;
3183                        break;
3184                }               /* switch */
3185        }
3186
3187unlock:
3188        spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3189
3190        if (run_queue)
3191                hfa384x_usbctlxq_run(hw);
3192}
3193
3194/*----------------------------------------------------------------
3195 * hfa384x_usbin_txcompl
3196 *
3197 * At this point we have the results of a previous transmit.
3198 *
3199 * Arguments:
3200 *      wlandev         wlan device
3201 *      usbin           ptr to the usb transfer buffer
3202 *
3203 * Returns:
3204 *      nothing
3205 *
3206 * Side effects:
3207 *
3208 * Call context:
3209 *      interrupt
3210 *----------------------------------------------------------------
3211 */
3212static void hfa384x_usbin_txcompl(struct wlandevice *wlandev,
3213                                  union hfa384x_usbin *usbin)
3214{
3215        u16 status;
3216
3217        status = le16_to_cpu(usbin->type); /* yeah I know it says type... */
3218
3219        /* Was there an error? */
3220        if (HFA384x_TXSTATUS_ISERROR(status))
3221                prism2sta_ev_txexc(wlandev, status);
3222        else
3223                prism2sta_ev_tx(wlandev, status);
3224}
3225
3226/*----------------------------------------------------------------
3227 * hfa384x_usbin_rx
3228 *
3229 * At this point we have a successful received a rx frame packet.
3230 *
3231 * Arguments:
3232 *      wlandev         wlan device
3233 *      usbin           ptr to the usb transfer buffer
3234 *
3235 * Returns:
3236 *      nothing
3237 *
3238 * Side effects:
3239 *
3240 * Call context:
3241 *      interrupt
3242 *----------------------------------------------------------------
3243 */
3244static void hfa384x_usbin_rx(struct wlandevice *wlandev, struct sk_buff *skb)
3245{
3246        union hfa384x_usbin *usbin = (union hfa384x_usbin *)skb->data;
3247        struct hfa384x *hw = wlandev->priv;
3248        int hdrlen;
3249        struct p80211_rxmeta *rxmeta;
3250        u16 data_len;
3251        u16 fc;
3252        u16 status;
3253
3254        /* Byte order convert once up front. */
3255        le16_to_cpus(&usbin->rxfrm.desc.status);
3256        le32_to_cpus(&usbin->rxfrm.desc.time);
3257
3258        /* Now handle frame based on port# */
3259        status = HFA384x_RXSTATUS_MACPORT_GET(usbin->rxfrm.desc.status);
3260
3261        switch (status) {
3262        case 0:
3263                fc = le16_to_cpu(usbin->rxfrm.desc.frame_control);
3264
3265                /* If exclude and we receive an unencrypted, drop it */
3266                if ((wlandev->hostwep & HOSTWEP_EXCLUDEUNENCRYPTED) &&
3267                    !WLAN_GET_FC_ISWEP(fc)) {
3268                        break;
3269                }
3270
3271                data_len = le16_to_cpu(usbin->rxfrm.desc.data_len);
3272
3273                /* How much header data do we have? */
3274                hdrlen = p80211_headerlen(fc);
3275
3276                /* Pull off the descriptor */
3277                skb_pull(skb, sizeof(struct hfa384x_rx_frame));
3278
3279                /* Now shunt the header block up against the data block
3280                 * with an "overlapping" copy
3281                 */
3282                memmove(skb_push(skb, hdrlen),
3283                        &usbin->rxfrm.desc.frame_control, hdrlen);
3284
3285                skb->dev = wlandev->netdev;
3286
3287                /* And set the frame length properly */
3288                skb_trim(skb, data_len + hdrlen);
3289
3290                /* The prism2 series does not return the CRC */
3291                memset(skb_put(skb, WLAN_CRC_LEN), 0xff, WLAN_CRC_LEN);
3292
3293                skb_reset_mac_header(skb);
3294
3295                /* Attach the rxmeta, set some stuff */
3296                p80211skb_rxmeta_attach(wlandev, skb);
3297                rxmeta = p80211skb_rxmeta(skb);
3298                rxmeta->mactime = usbin->rxfrm.desc.time;
3299                rxmeta->rxrate = usbin->rxfrm.desc.rate;
3300                rxmeta->signal = usbin->rxfrm.desc.signal - hw->dbmadjust;
3301                rxmeta->noise = usbin->rxfrm.desc.silence - hw->dbmadjust;
3302
3303                p80211netdev_rx(wlandev, skb);
3304
3305                break;
3306
3307        case 7:
3308                if (!HFA384x_RXSTATUS_ISFCSERR(usbin->rxfrm.desc.status)) {
3309                        /* Copy to wlansnif skb */
3310                        hfa384x_int_rxmonitor(wlandev, &usbin->rxfrm);
3311                        dev_kfree_skb(skb);
3312                } else {
3313                        pr_debug("Received monitor frame: FCSerr set\n");
3314                }
3315                break;
3316
3317        default:
3318                netdev_warn(hw->wlandev->netdev,
3319                            "Received frame on unsupported port=%d\n",
3320                            status);
3321                break;
3322        }
3323}
3324
3325/*----------------------------------------------------------------
3326 * hfa384x_int_rxmonitor
3327 *
3328 * Helper function for int_rx.  Handles monitor frames.
3329 * Note that this function allocates space for the FCS and sets it
3330 * to 0xffffffff.  The hfa384x doesn't give us the FCS value but the
3331 * higher layers expect it.  0xffffffff is used as a flag to indicate
3332 * the FCS is bogus.
3333 *
3334 * Arguments:
3335 *      wlandev         wlan device structure
3336 *      rxfrm           rx descriptor read from card in int_rx
3337 *
3338 * Returns:
3339 *      nothing
3340 *
3341 * Side effects:
3342 *      Allocates an skb and passes it up via the PF_PACKET interface.
3343 * Call context:
3344 *      interrupt
3345 *----------------------------------------------------------------
3346 */
3347static void hfa384x_int_rxmonitor(struct wlandevice *wlandev,
3348                                  struct hfa384x_usb_rxfrm *rxfrm)
3349{
3350        struct hfa384x_rx_frame *rxdesc = &rxfrm->desc;
3351        unsigned int hdrlen = 0;
3352        unsigned int datalen = 0;
3353        unsigned int skblen = 0;
3354        u8 *datap;
3355        u16 fc;
3356        struct sk_buff *skb;
3357        struct hfa384x *hw = wlandev->priv;
3358
3359        /* Remember the status, time, and data_len fields are in host order */
3360        /* Figure out how big the frame is */
3361        fc = le16_to_cpu(rxdesc->frame_control);
3362        hdrlen = p80211_headerlen(fc);
3363        datalen = le16_to_cpu(rxdesc->data_len);
3364
3365        /* Allocate an ind message+framesize skb */
3366        skblen = sizeof(struct p80211_caphdr) + hdrlen + datalen + WLAN_CRC_LEN;
3367
3368        /* sanity check the length */
3369        if (skblen >
3370            (sizeof(struct p80211_caphdr) +
3371             WLAN_HDR_A4_LEN + WLAN_DATA_MAXLEN + WLAN_CRC_LEN)) {
3372                pr_debug("overlen frm: len=%zd\n",
3373                         skblen - sizeof(struct p80211_caphdr));
3374
3375                return;
3376        }
3377
3378        skb = dev_alloc_skb(skblen);
3379        if (!skb)
3380                return;
3381
3382        /* only prepend the prism header if in the right mode */
3383        if ((wlandev->netdev->type == ARPHRD_IEEE80211_PRISM) &&
3384            (hw->sniffhdr != 0)) {
3385                struct p80211_caphdr *caphdr;
3386                /* The NEW header format! */
3387                datap = skb_put(skb, sizeof(struct p80211_caphdr));
3388                caphdr = (struct p80211_caphdr *)datap;
3389
3390                caphdr->version = htonl(P80211CAPTURE_VERSION);
3391                caphdr->length = htonl(sizeof(struct p80211_caphdr));
3392                caphdr->mactime = __cpu_to_be64(rxdesc->time * 1000);
3393                caphdr->hosttime = __cpu_to_be64(jiffies);
3394                caphdr->phytype = htonl(4);     /* dss_dot11_b */
3395                caphdr->channel = htonl(hw->sniff_channel);
3396                caphdr->datarate = htonl(rxdesc->rate);
3397                caphdr->antenna = htonl(0);     /* unknown */
3398                caphdr->priority = htonl(0);    /* unknown */
3399                caphdr->ssi_type = htonl(3);    /* rssi_raw */
3400                caphdr->ssi_signal = htonl(rxdesc->signal);
3401                caphdr->ssi_noise = htonl(rxdesc->silence);
3402                caphdr->preamble = htonl(0);    /* unknown */
3403                caphdr->encoding = htonl(1);    /* cck */
3404        }
3405
3406        /* Copy the 802.11 header to the skb
3407         * (ctl frames may be less than a full header)
3408         */
3409        skb_put_data(skb, &rxdesc->frame_control, hdrlen);
3410
3411        /* If any, copy the data from the card to the skb */
3412        if (datalen > 0) {
3413                datap = skb_put_data(skb, rxfrm->data, datalen);
3414
3415                /* check for unencrypted stuff if WEP bit set. */
3416                if (*(datap - hdrlen + 1) & 0x40)       /* wep set */
3417                        if ((*(datap) == 0xaa) && (*(datap + 1) == 0xaa))
3418                                /* clear wep; it's the 802.2 header! */
3419                                *(datap - hdrlen + 1) &= 0xbf;
3420        }
3421
3422        if (hw->sniff_fcs) {
3423                /* Set the FCS */
3424                datap = skb_put(skb, WLAN_CRC_LEN);
3425                memset(datap, 0xff, WLAN_CRC_LEN);
3426        }
3427
3428        /* pass it back up */
3429        p80211netdev_rx(wlandev, skb);
3430}
3431
3432/*----------------------------------------------------------------
3433 * hfa384x_usbin_info
3434 *
3435 * At this point we have a successful received a Prism2 info frame.
3436 *
3437 * Arguments:
3438 *      wlandev         wlan device
3439 *      usbin           ptr to the usb transfer buffer
3440 *
3441 * Returns:
3442 *      nothing
3443 *
3444 * Side effects:
3445 *
3446 * Call context:
3447 *      interrupt
3448 *----------------------------------------------------------------
3449 */
3450static void hfa384x_usbin_info(struct wlandevice *wlandev,
3451                               union hfa384x_usbin *usbin)
3452{
3453        le16_to_cpus(&usbin->infofrm.info.framelen);
3454        prism2sta_ev_info(wlandev, &usbin->infofrm.info);
3455}
3456
3457/*----------------------------------------------------------------
3458 * hfa384x_usbout_callback
3459 *
3460 * Callback for URBs on the BULKOUT endpoint.
3461 *
3462 * Arguments:
3463 *      urb             ptr to the completed urb
3464 *
3465 * Returns:
3466 *      nothing
3467 *
3468 * Side effects:
3469 *
3470 * Call context:
3471 *      interrupt
3472 *----------------------------------------------------------------
3473 */
3474static void hfa384x_usbout_callback(struct urb *urb)
3475{
3476        struct wlandevice *wlandev = urb->context;
3477
3478#ifdef DEBUG_USB
3479        dbprint_urb(urb);
3480#endif
3481
3482        if (wlandev && wlandev->netdev) {
3483                switch (urb->status) {
3484                case 0:
3485                        prism2sta_ev_alloc(wlandev);
3486                        break;
3487
3488                case -EPIPE: {
3489                        struct hfa384x *hw = wlandev->priv;
3490
3491                        netdev_warn(hw->wlandev->netdev,
3492                                    "%s tx pipe stalled: requesting reset\n",
3493                                    wlandev->netdev->name);
3494                        if (!test_and_set_bit(WORK_TX_HALT, &hw->usb_flags))
3495                                schedule_work(&hw->usb_work);
3496                        wlandev->netdev->stats.tx_errors++;
3497                        break;
3498                }
3499
3500                case -EPROTO:
3501                case -ETIMEDOUT:
3502                case -EILSEQ: {
3503                        struct hfa384x *hw = wlandev->priv;
3504
3505                        if (!test_and_set_bit(THROTTLE_TX, &hw->usb_flags) &&
3506                            !timer_pending(&hw->throttle)) {
3507                                mod_timer(&hw->throttle,
3508                                          jiffies + THROTTLE_JIFFIES);
3509                        }
3510                        wlandev->netdev->stats.tx_errors++;
3511                        netif_stop_queue(wlandev->netdev);
3512                        break;
3513                }
3514
3515                case -ENOENT:
3516                case -ESHUTDOWN:
3517                        /* Ignorable errors */
3518                        break;
3519
3520                default:
3521                        netdev_info(wlandev->netdev, "unknown urb->status=%d\n",
3522                                    urb->status);
3523                        wlandev->netdev->stats.tx_errors++;
3524                        break;
3525                }               /* switch */
3526        }
3527}
3528
3529/*----------------------------------------------------------------
3530 * hfa384x_ctlxout_callback
3531 *
3532 * Callback for control data on the BULKOUT endpoint.
3533 *
3534 * Arguments:
3535 *      urb             ptr to the completed urb
3536 *
3537 * Returns:
3538 * nothing
3539 *
3540 * Side effects:
3541 *
3542 * Call context:
3543 * interrupt
3544 *----------------------------------------------------------------
3545 */
3546static void hfa384x_ctlxout_callback(struct urb *urb)
3547{
3548        struct hfa384x *hw = urb->context;
3549        int delete_resptimer = 0;
3550        int timer_ok = 1;
3551        int run_queue = 0;
3552        struct hfa384x_usbctlx *ctlx;
3553        unsigned long flags;
3554
3555        pr_debug("urb->status=%d\n", urb->status);
3556#ifdef DEBUG_USB
3557        dbprint_urb(urb);
3558#endif
3559        if ((urb->status == -ESHUTDOWN) ||
3560            (urb->status == -ENODEV) || !hw)
3561                return;
3562
3563retry:
3564        spin_lock_irqsave(&hw->ctlxq.lock, flags);
3565
3566        /*
3567         * Only one CTLX at a time on the "active" list, and
3568         * none at all if we are unplugged. However, we can
3569         * rely on the disconnect function to clean everything
3570         * up if someone unplugged the adapter.
3571         */
3572        if (list_empty(&hw->ctlxq.active)) {
3573                spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3574                return;
3575        }
3576
3577        /*
3578         * Having something on the "active" queue means
3579         * that we have timers to worry about ...
3580         */
3581        if (del_timer(&hw->reqtimer) == 0) {
3582                if (hw->req_timer_done == 0) {
3583                        /*
3584                         * This timer was actually running while we
3585                         * were trying to delete it. Let it terminate
3586                         * gracefully instead.
3587                         */
3588                        spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3589                        goto retry;
3590                }
3591        } else {
3592                hw->req_timer_done = 1;
3593        }
3594
3595        ctlx = get_active_ctlx(hw);
3596
3597        if (urb->status == 0) {
3598                /* Request portion of a CTLX is successful */
3599                switch (ctlx->state) {
3600                case CTLX_REQ_SUBMITTED:
3601                        /* This OUT-ACK received before IN */
3602                        ctlx->state = CTLX_REQ_COMPLETE;
3603                        break;
3604
3605                case CTLX_RESP_COMPLETE:
3606                        /* IN already received before this OUT-ACK,
3607                         * so this command must now be complete.
3608                         */
3609                        ctlx->state = CTLX_COMPLETE;
3610                        unlocked_usbctlx_complete(hw, ctlx);
3611                        run_queue = 1;
3612                        break;
3613
3614                default:
3615                        /* This is NOT a valid CTLX "success" state! */
3616                        netdev_err(hw->wlandev->netdev,
3617                                   "Illegal CTLX[%d] success state(%s, %d) in OUT URB\n",
3618                                   le16_to_cpu(ctlx->outbuf.type),
3619                                   ctlxstr(ctlx->state), urb->status);
3620                        break;
3621                }               /* switch */
3622        } else {
3623                /* If the pipe has stalled then we need to reset it */
3624                if ((urb->status == -EPIPE) &&
3625                    !test_and_set_bit(WORK_TX_HALT, &hw->usb_flags)) {
3626                        netdev_warn(hw->wlandev->netdev,
3627                                    "%s tx pipe stalled: requesting reset\n",
3628                                    hw->wlandev->netdev->name);
3629                        schedule_work(&hw->usb_work);
3630                }
3631
3632                /* If someone cancels the OUT URB then its status
3633                 * should be either -ECONNRESET or -ENOENT.
3634                 */
3635                ctlx->state = CTLX_REQ_FAILED;
3636                unlocked_usbctlx_complete(hw, ctlx);
3637                delete_resptimer = 1;
3638                run_queue = 1;
3639        }
3640
3641delresp:
3642        if (delete_resptimer) {
3643                timer_ok = del_timer(&hw->resptimer);
3644                if (timer_ok != 0)
3645                        hw->resp_timer_done = 1;
3646        }
3647
3648        spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3649
3650        if (!timer_ok && (hw->resp_timer_done == 0)) {
3651                spin_lock_irqsave(&hw->ctlxq.lock, flags);
3652                goto delresp;
3653        }
3654
3655        if (run_queue)
3656                hfa384x_usbctlxq_run(hw);
3657}
3658
3659/*----------------------------------------------------------------
3660 * hfa384x_usbctlx_reqtimerfn
3661 *
3662 * Timer response function for CTLX request timeouts.  If this
3663 * function is called, it means that the callback for the OUT
3664 * URB containing a Prism2.x XXX_Request was never called.
3665 *
3666 * Arguments:
3667 *      data            a ptr to the struct hfa384x
3668 *
3669 * Returns:
3670 *      nothing
3671 *
3672 * Side effects:
3673 *
3674 * Call context:
3675 *      interrupt
3676 *----------------------------------------------------------------
3677 */
3678static void hfa384x_usbctlx_reqtimerfn(struct timer_list *t)
3679{
3680        struct hfa384x *hw = from_timer(hw, t, reqtimer);
3681        unsigned long flags;
3682
3683        spin_lock_irqsave(&hw->ctlxq.lock, flags);
3684
3685        hw->req_timer_done = 1;
3686
3687        /* Removing the hardware automatically empties
3688         * the active list ...
3689         */
3690        if (!list_empty(&hw->ctlxq.active)) {
3691                /*
3692                 * We must ensure that our URB is removed from
3693                 * the system, if it hasn't already expired.
3694                 */
3695                hw->ctlx_urb.transfer_flags |= URB_ASYNC_UNLINK;
3696                if (usb_unlink_urb(&hw->ctlx_urb) == -EINPROGRESS) {
3697                        struct hfa384x_usbctlx *ctlx = get_active_ctlx(hw);
3698
3699                        ctlx->state = CTLX_REQ_FAILED;
3700
3701                        /* This URB was active, but has now been
3702                         * cancelled. It will now have a status of
3703                         * -ECONNRESET in the callback function.
3704                         *
3705                         * We are cancelling this CTLX, so we're
3706                         * not going to need to wait for a response.
3707                         * The URB's callback function will check
3708                         * that this timer is truly dead.
3709                         */
3710                        if (del_timer(&hw->resptimer) != 0)
3711                                hw->resp_timer_done = 1;
3712                }
3713        }
3714
3715        spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3716}
3717
3718/*----------------------------------------------------------------
3719 * hfa384x_usbctlx_resptimerfn
3720 *
3721 * Timer response function for CTLX response timeouts.  If this
3722 * function is called, it means that the callback for the IN
3723 * URB containing a Prism2.x XXX_Response was never called.
3724 *
3725 * Arguments:
3726 *      data            a ptr to the struct hfa384x
3727 *
3728 * Returns:
3729 *      nothing
3730 *
3731 * Side effects:
3732 *
3733 * Call context:
3734 *      interrupt
3735 *----------------------------------------------------------------
3736 */
3737static void hfa384x_usbctlx_resptimerfn(struct timer_list *t)
3738{
3739        struct hfa384x *hw = from_timer(hw, t, resptimer);
3740        unsigned long flags;
3741
3742        spin_lock_irqsave(&hw->ctlxq.lock, flags);
3743
3744        hw->resp_timer_done = 1;
3745
3746        /* The active list will be empty if the
3747         * adapter has been unplugged ...
3748         */
3749        if (!list_empty(&hw->ctlxq.active)) {
3750                struct hfa384x_usbctlx *ctlx = get_active_ctlx(hw);
3751
3752                if (unlocked_usbctlx_cancel_async(hw, ctlx) == 0) {
3753                        spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3754                        hfa384x_usbctlxq_run(hw);
3755                        return;
3756                }
3757        }
3758        spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3759}
3760
3761/*----------------------------------------------------------------
3762 * hfa384x_usb_throttlefn
3763 *
3764 *
3765 * Arguments:
3766 *      data    ptr to hw
3767 *
3768 * Returns:
3769 *      Nothing
3770 *
3771 * Side effects:
3772 *
3773 * Call context:
3774 *      Interrupt
3775 *----------------------------------------------------------------
3776 */
3777static void hfa384x_usb_throttlefn(struct timer_list *t)
3778{
3779        struct hfa384x *hw = from_timer(hw, t, throttle);
3780        unsigned long flags;
3781
3782        spin_lock_irqsave(&hw->ctlxq.lock, flags);
3783
3784        /*
3785         * We need to check BOTH the RX and the TX throttle controls,
3786         * so we use the bitwise OR instead of the logical OR.
3787         */
3788        pr_debug("flags=0x%lx\n", hw->usb_flags);
3789        if (!hw->wlandev->hwremoved &&
3790            ((test_and_clear_bit(THROTTLE_RX, &hw->usb_flags) &&
3791              !test_and_set_bit(WORK_RX_RESUME, &hw->usb_flags)) |
3792             (test_and_clear_bit(THROTTLE_TX, &hw->usb_flags) &&
3793              !test_and_set_bit(WORK_TX_RESUME, &hw->usb_flags))
3794            )) {
3795                schedule_work(&hw->usb_work);
3796        }
3797
3798        spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3799}
3800
3801/*----------------------------------------------------------------
3802 * hfa384x_usbctlx_submit
3803 *
3804 * Called from the doxxx functions to submit a CTLX to the queue
3805 *
3806 * Arguments:
3807 *      hw              ptr to the hw struct
3808 *      ctlx            ctlx structure to enqueue
3809 *
3810 * Returns:
3811 *      -ENODEV if the adapter is unplugged
3812 *      0
3813 *
3814 * Side effects:
3815 *
3816 * Call context:
3817 *      process or interrupt
3818 *----------------------------------------------------------------
3819 */
3820static int hfa384x_usbctlx_submit(struct hfa384x *hw,
3821                                  struct hfa384x_usbctlx *ctlx)
3822{
3823        unsigned long flags;
3824
3825        spin_lock_irqsave(&hw->ctlxq.lock, flags);
3826
3827        if (hw->wlandev->hwremoved) {
3828                spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3829                return -ENODEV;
3830        }
3831
3832        ctlx->state = CTLX_PENDING;
3833        list_add_tail(&ctlx->list, &hw->ctlxq.pending);
3834        spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3835        hfa384x_usbctlxq_run(hw);
3836
3837        return 0;
3838}
3839
3840/*----------------------------------------------------------------
3841 * hfa384x_isgood_pdrcore
3842 *
3843 * Quick check of PDR codes.
3844 *
3845 * Arguments:
3846 *      pdrcode         PDR code number (host order)
3847 *
3848 * Returns:
3849 *      zero            not good.
3850 *      one             is good.
3851 *
3852 * Side effects:
3853 *
3854 * Call context:
3855 *----------------------------------------------------------------
3856 */
3857static int hfa384x_isgood_pdrcode(u16 pdrcode)
3858{
3859        switch (pdrcode) {
3860        case HFA384x_PDR_END_OF_PDA:
3861        case HFA384x_PDR_PCB_PARTNUM:
3862        case HFA384x_PDR_PDAVER:
3863        case HFA384x_PDR_NIC_SERIAL:
3864        case HFA384x_PDR_MKK_MEASUREMENTS:
3865        case HFA384x_PDR_NIC_RAMSIZE:
3866        case HFA384x_PDR_MFISUPRANGE:
3867        case HFA384x_PDR_CFISUPRANGE:
3868        case HFA384x_PDR_NICID:
3869        case HFA384x_PDR_MAC_ADDRESS:
3870        case HFA384x_PDR_REGDOMAIN:
3871        case HFA384x_PDR_ALLOWED_CHANNEL:
3872        case HFA384x_PDR_DEFAULT_CHANNEL:
3873        case HFA384x_PDR_TEMPTYPE:
3874        case HFA384x_PDR_IFR_SETTING:
3875        case HFA384x_PDR_RFR_SETTING:
3876        case HFA384x_PDR_HFA3861_BASELINE:
3877        case HFA384x_PDR_HFA3861_SHADOW:
3878        case HFA384x_PDR_HFA3861_IFRF:
3879        case HFA384x_PDR_HFA3861_CHCALSP:
3880        case HFA384x_PDR_HFA3861_CHCALI:
3881        case HFA384x_PDR_3842_NIC_CONFIG:
3882        case HFA384x_PDR_USB_ID:
3883        case HFA384x_PDR_PCI_ID:
3884        case HFA384x_PDR_PCI_IFCONF:
3885        case HFA384x_PDR_PCI_PMCONF:
3886        case HFA384x_PDR_RFENRGY:
3887        case HFA384x_PDR_HFA3861_MANF_TESTSP:
3888        case HFA384x_PDR_HFA3861_MANF_TESTI:
3889                /* code is OK */
3890                return 1;
3891        default:
3892                if (pdrcode < 0x1000) {
3893                        /* code is OK, but we don't know exactly what it is */
3894                        pr_debug("Encountered unknown PDR#=0x%04x, assuming it's ok.\n",
3895                                 pdrcode);
3896                        return 1;
3897                }
3898                break;
3899        }
3900        /* bad code */
3901        pr_debug("Encountered unknown PDR#=0x%04x, (>=0x1000), assuming it's bad.\n",
3902                 pdrcode);
3903        return 0;
3904}
3905