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