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