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