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