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