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