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