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