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