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