ACPI: thinkpad-acpi: add development version tag
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / wlan-ng / hfa384x_usb.c
blob51d9e520d52a381964a74a4f142b7a141a8f6b01
1 /* src/prism2/driver/hfa384x_usb.c
3 * Functions that talk to the USB variantof the Intersil hfa384x MAC
5 * Copyright (C) 1999 AbsoluteValue Systems, Inc. All Rights Reserved.
6 * --------------------------------------------------------------------
8 * linux-wlan
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/
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.
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.
31 * --------------------------------------------------------------------
33 * Inquiries regarding the linux-wlan Open Source project can be
34 * made directly to:
36 * AbsoluteValue Systems Inc.
37 * info@linux-wlan.com
38 * http://www.linux-wlan.com
40 * --------------------------------------------------------------------
42 * Portions of the development of this software were funded by
43 * Intersil Corporation as part of PRISM(R) chipset product development.
45 * --------------------------------------------------------------------
47 * This file implements functions that correspond to the prism2/hfa384x
48 * 802.11 MAC hardware and firmware host interface.
50 * The functions can be considered to represent several levels of
51 * abstraction. The lowest level functions are simply C-callable wrappers
52 * around the register accesses. The next higher level represents C-callable
53 * prism2 API functions that match the Intersil documentation as closely
54 * as is reasonable. The next higher layer implements common sequences
55 * of invokations of the API layer (e.g. write to bap, followed by cmd).
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.
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.
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.
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.
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.
91 * hfa384x_dl_xxx download related functions.
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()
104 * . Do interesting things w/ the hardware
106 * stop()
107 * destroy()
109 * Note that destroy() can be called without calling stop() first.
110 * --------------------------------------------------------------------
113 #include <linux/module.h>
114 #include <linux/kernel.h>
115 #include <linux/sched.h>
116 #include <linux/types.h>
117 #include <linux/slab.h>
118 #include <linux/wireless.h>
119 #include <linux/netdevice.h>
120 #include <linux/timer.h>
121 #include <asm/io.h>
122 #include <linux/delay.h>
123 #include <asm/byteorder.h>
124 #include <asm/bitops.h>
125 #include <linux/list.h>
126 #include <linux/usb.h>
127 #include <linux/byteorder/generic.h>
129 #define SUBMIT_URB(u,f) usb_submit_urb(u,f)
131 /*================================================================*/
132 /* Project Includes */
134 #include "p80211types.h"
135 #include "p80211hdr.h"
136 #include "p80211mgmt.h"
137 #include "p80211conv.h"
138 #include "p80211msg.h"
139 #include "p80211netdev.h"
140 #include "p80211req.h"
141 #include "p80211metadef.h"
142 #include "p80211metastruct.h"
143 #include "hfa384x.h"
144 #include "prism2mgmt.h"
146 enum cmd_mode {
147 DOWAIT = 0,
148 DOASYNC
150 typedef enum cmd_mode CMD_MODE;
152 #define THROTTLE_JIFFIES (HZ/8)
153 #define URB_ASYNC_UNLINK 0
154 #define USB_QUEUE_BULK 0
156 #define ROUNDUP64(a) (((a)+63)&~63)
158 #ifdef DEBUG_USB
159 static void dbprint_urb(struct urb *urb);
160 #endif
162 static void
163 hfa384x_int_rxmonitor(wlandevice_t * wlandev, hfa384x_usb_rxfrm_t * rxfrm);
165 static void hfa384x_usb_defer(struct work_struct *data);
167 static int submit_rx_urb(hfa384x_t * hw, gfp_t flags);
169 static int submit_tx_urb(hfa384x_t * hw, struct urb *tx_urb, gfp_t flags);
171 /*---------------------------------------------------*/
172 /* Callbacks */
173 static void hfa384x_usbout_callback(struct urb *urb);
174 static void hfa384x_ctlxout_callback(struct urb *urb);
175 static void hfa384x_usbin_callback(struct urb *urb);
177 static void
178 hfa384x_usbin_txcompl(wlandevice_t * wlandev, hfa384x_usbin_t * usbin);
180 static void hfa384x_usbin_rx(wlandevice_t * wlandev, struct sk_buff *skb);
182 static void hfa384x_usbin_info(wlandevice_t * wlandev, hfa384x_usbin_t * usbin);
184 static void
185 hfa384x_usbout_tx(wlandevice_t * wlandev, hfa384x_usbout_t * usbout);
187 static void hfa384x_usbin_ctlx(hfa384x_t * hw, hfa384x_usbin_t * usbin,
188 int urb_status);
190 /*---------------------------------------------------*/
191 /* Functions to support the prism2 usb command queue */
193 static void hfa384x_usbctlxq_run(hfa384x_t * hw);
195 static void hfa384x_usbctlx_reqtimerfn(unsigned long data);
197 static void hfa384x_usbctlx_resptimerfn(unsigned long data);
199 static void hfa384x_usb_throttlefn(unsigned long data);
201 static void hfa384x_usbctlx_completion_task(unsigned long data);
203 static void hfa384x_usbctlx_reaper_task(unsigned long data);
205 static int hfa384x_usbctlx_submit(hfa384x_t * hw, hfa384x_usbctlx_t * ctlx);
207 static void unlocked_usbctlx_complete(hfa384x_t * hw, hfa384x_usbctlx_t * ctlx);
209 struct usbctlx_completor {
210 int (*complete) (struct usbctlx_completor *);
212 typedef struct usbctlx_completor usbctlx_completor_t;
214 static int
215 hfa384x_usbctlx_complete_sync(hfa384x_t * hw,
216 hfa384x_usbctlx_t * ctlx,
217 usbctlx_completor_t * completor);
219 static int
220 unlocked_usbctlx_cancel_async(hfa384x_t * hw, hfa384x_usbctlx_t * ctlx);
222 static void hfa384x_cb_status(hfa384x_t * hw, const hfa384x_usbctlx_t * ctlx);
224 static void hfa384x_cb_rrid(hfa384x_t * hw, const hfa384x_usbctlx_t * ctlx);
226 static int
227 usbctlx_get_status(const hfa384x_usb_cmdresp_t * cmdresp,
228 hfa384x_cmdresult_t * result);
230 static void
231 usbctlx_get_rridresult(const hfa384x_usb_rridresp_t * rridresp,
232 hfa384x_rridresult_t * result);
234 /*---------------------------------------------------*/
235 /* Low level req/resp CTLX formatters and submitters */
236 static int
237 hfa384x_docmd(hfa384x_t * hw,
238 CMD_MODE mode,
239 hfa384x_metacmd_t * cmd,
240 ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data);
242 static int
243 hfa384x_dorrid(hfa384x_t * hw,
244 CMD_MODE mode,
245 u16 rid,
246 void *riddata,
247 unsigned int riddatalen,
248 ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data);
250 static int
251 hfa384x_dowrid(hfa384x_t * hw,
252 CMD_MODE mode,
253 u16 rid,
254 void *riddata,
255 unsigned int riddatalen,
256 ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data);
258 static int
259 hfa384x_dormem(hfa384x_t * hw,
260 CMD_MODE mode,
261 u16 page,
262 u16 offset,
263 void *data,
264 unsigned int len,
265 ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data);
267 static int
268 hfa384x_dowmem(hfa384x_t * hw,
269 CMD_MODE mode,
270 u16 page,
271 u16 offset,
272 void *data,
273 unsigned int len,
274 ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data);
276 static int hfa384x_isgood_pdrcode(u16 pdrcode);
278 static inline const char *ctlxstr(CTLX_STATE s)
280 static const char *ctlx_str[] = {
281 "Initial state",
282 "Complete",
283 "Request failed",
284 "Request pending",
285 "Request packet submitted",
286 "Request packet completed",
287 "Response packet completed"
290 return ctlx_str[s];
293 static inline hfa384x_usbctlx_t *get_active_ctlx(hfa384x_t * hw)
295 return list_entry(hw->ctlxq.active.next, hfa384x_usbctlx_t, list);
298 #ifdef DEBUG_USB
299 void dbprint_urb(struct urb *urb)
301 pr_debug("urb->pipe=0x%08x\n", urb->pipe);
302 pr_debug("urb->status=0x%08x\n", urb->status);
303 pr_debug("urb->transfer_flags=0x%08x\n", urb->transfer_flags);
304 pr_debug("urb->transfer_buffer=0x%08x\n",
305 (unsigned int)urb->transfer_buffer);
306 pr_debug("urb->transfer_buffer_length=0x%08x\n",
307 urb->transfer_buffer_length);
308 pr_debug("urb->actual_length=0x%08x\n", urb->actual_length);
309 pr_debug("urb->bandwidth=0x%08x\n", urb->bandwidth);
310 pr_debug("urb->setup_packet(ctl)=0x%08x\n",
311 (unsigned int)urb->setup_packet);
312 pr_debug("urb->start_frame(iso/irq)=0x%08x\n", urb->start_frame);
313 pr_debug("urb->interval(irq)=0x%08x\n", urb->interval);
314 pr_debug("urb->error_count(iso)=0x%08x\n", urb->error_count);
315 pr_debug("urb->timeout=0x%08x\n", urb->timeout);
316 pr_debug("urb->context=0x%08x\n", (unsigned int)urb->context);
317 pr_debug("urb->complete=0x%08x\n", (unsigned int)urb->complete);
319 #endif
321 /*----------------------------------------------------------------
322 * submit_rx_urb
324 * Listen for input data on the BULK-IN pipe. If the pipe has
325 * stalled then schedule it to be reset.
327 * Arguments:
328 * hw device struct
329 * memflags memory allocation flags
331 * Returns:
332 * error code from submission
334 * Call context:
335 * Any
336 ----------------------------------------------------------------*/
337 static int submit_rx_urb(hfa384x_t * hw, gfp_t memflags)
339 struct sk_buff *skb;
340 int result;
342 skb = dev_alloc_skb(sizeof(hfa384x_usbin_t));
343 if (skb == NULL) {
344 result = -ENOMEM;
345 goto done;
348 /* Post the IN urb */
349 usb_fill_bulk_urb(&hw->rx_urb, hw->usb,
350 hw->endp_in,
351 skb->data, sizeof(hfa384x_usbin_t),
352 hfa384x_usbin_callback, hw->wlandev);
354 hw->rx_urb_skb = skb;
356 result = -ENOLINK;
357 if (!hw->wlandev->hwremoved && !test_bit(WORK_RX_HALT, &hw->usb_flags)) {
358 result = SUBMIT_URB(&hw->rx_urb, memflags);
360 /* Check whether we need to reset the RX pipe */
361 if (result == -EPIPE) {
362 printk(KERN_WARNING
363 "%s rx pipe stalled: requesting reset\n",
364 hw->wlandev->netdev->name);
365 if (!test_and_set_bit(WORK_RX_HALT, &hw->usb_flags))
366 schedule_work(&hw->usb_work);
370 /* Don't leak memory if anything should go wrong */
371 if (result != 0) {
372 dev_kfree_skb(skb);
373 hw->rx_urb_skb = NULL;
376 done:
377 return result;
380 /*----------------------------------------------------------------
381 * submit_tx_urb
383 * Prepares and submits the URB of transmitted data. If the
384 * submission fails then it will schedule the output pipe to
385 * be reset.
387 * Arguments:
388 * hw device struct
389 * tx_urb URB of data for tranmission
390 * memflags memory allocation flags
392 * Returns:
393 * error code from submission
395 * Call context:
396 * Any
397 ----------------------------------------------------------------*/
398 static int submit_tx_urb(hfa384x_t * hw, struct urb *tx_urb, gfp_t memflags)
400 struct net_device *netdev = hw->wlandev->netdev;
401 int result;
403 result = -ENOLINK;
404 if (netif_running(netdev)) {
406 if (!hw->wlandev->hwremoved
407 && !test_bit(WORK_TX_HALT, &hw->usb_flags)) {
408 result = SUBMIT_URB(tx_urb, memflags);
410 /* Test whether we need to reset the TX pipe */
411 if (result == -EPIPE) {
412 printk(KERN_WARNING
413 "%s tx pipe stalled: requesting reset\n",
414 netdev->name);
415 set_bit(WORK_TX_HALT, &hw->usb_flags);
416 schedule_work(&hw->usb_work);
417 } else if (result == 0) {
418 netif_stop_queue(netdev);
423 return result;
426 /*----------------------------------------------------------------
427 * hfa394x_usb_defer
429 * There are some things that the USB stack cannot do while
430 * in interrupt context, so we arrange this function to run
431 * in process context.
433 * Arguments:
434 * hw device structure
436 * Returns:
437 * nothing
439 * Call context:
440 * process (by design)
441 ----------------------------------------------------------------*/
442 static void hfa384x_usb_defer(struct work_struct *data)
444 hfa384x_t *hw = container_of(data, struct hfa384x, usb_work);
445 struct net_device *netdev = hw->wlandev->netdev;
447 /* Don't bother trying to reset anything if the plug
448 * has been pulled ...
450 if (hw->wlandev->hwremoved)
451 return;
453 /* Reception has stopped: try to reset the input pipe */
454 if (test_bit(WORK_RX_HALT, &hw->usb_flags)) {
455 int ret;
457 usb_kill_urb(&hw->rx_urb); /* Cannot be holding spinlock! */
459 ret = usb_clear_halt(hw->usb, hw->endp_in);
460 if (ret != 0) {
461 printk(KERN_ERR
462 "Failed to clear rx pipe for %s: err=%d\n",
463 netdev->name, ret);
464 } else {
465 printk(KERN_INFO "%s rx pipe reset complete.\n",
466 netdev->name);
467 clear_bit(WORK_RX_HALT, &hw->usb_flags);
468 set_bit(WORK_RX_RESUME, &hw->usb_flags);
472 /* Resume receiving data back from the device. */
473 if (test_bit(WORK_RX_RESUME, &hw->usb_flags)) {
474 int ret;
476 ret = submit_rx_urb(hw, GFP_KERNEL);
477 if (ret != 0) {
478 printk(KERN_ERR
479 "Failed to resume %s rx pipe.\n", netdev->name);
480 } else {
481 clear_bit(WORK_RX_RESUME, &hw->usb_flags);
485 /* Transmission has stopped: try to reset the output pipe */
486 if (test_bit(WORK_TX_HALT, &hw->usb_flags)) {
487 int ret;
489 usb_kill_urb(&hw->tx_urb);
490 ret = usb_clear_halt(hw->usb, hw->endp_out);
491 if (ret != 0) {
492 printk(KERN_ERR
493 "Failed to clear tx pipe for %s: err=%d\n",
494 netdev->name, ret);
495 } else {
496 printk(KERN_INFO "%s tx pipe reset complete.\n",
497 netdev->name);
498 clear_bit(WORK_TX_HALT, &hw->usb_flags);
499 set_bit(WORK_TX_RESUME, &hw->usb_flags);
501 /* Stopping the BULK-OUT pipe also blocked
502 * us from sending any more CTLX URBs, so
503 * we need to re-run our queue ...
505 hfa384x_usbctlxq_run(hw);
509 /* Resume transmitting. */
510 if (test_and_clear_bit(WORK_TX_RESUME, &hw->usb_flags))
511 netif_wake_queue(hw->wlandev->netdev);
514 /*----------------------------------------------------------------
515 * hfa384x_create
517 * Sets up the hfa384x_t data structure for use. Note this
518 * does _not_ intialize the actual hardware, just the data structures
519 * we use to keep track of its state.
521 * Arguments:
522 * hw device structure
523 * irq device irq number
524 * iobase i/o base address for register access
525 * membase memory base address for register access
527 * Returns:
528 * nothing
530 * Side effects:
532 * Call context:
533 * process
534 ----------------------------------------------------------------*/
535 void hfa384x_create(hfa384x_t * hw, struct usb_device *usb)
537 memset(hw, 0, sizeof(hfa384x_t));
538 hw->usb = usb;
540 /* set up the endpoints */
541 hw->endp_in = usb_rcvbulkpipe(usb, 1);
542 hw->endp_out = usb_sndbulkpipe(usb, 2);
544 /* Set up the waitq */
545 init_waitqueue_head(&hw->cmdq);
547 /* Initialize the command queue */
548 spin_lock_init(&hw->ctlxq.lock);
549 INIT_LIST_HEAD(&hw->ctlxq.pending);
550 INIT_LIST_HEAD(&hw->ctlxq.active);
551 INIT_LIST_HEAD(&hw->ctlxq.completing);
552 INIT_LIST_HEAD(&hw->ctlxq.reapable);
554 /* Initialize the authentication queue */
555 skb_queue_head_init(&hw->authq);
557 tasklet_init(&hw->reaper_bh,
558 hfa384x_usbctlx_reaper_task, (unsigned long)hw);
559 tasklet_init(&hw->completion_bh,
560 hfa384x_usbctlx_completion_task, (unsigned long)hw);
561 INIT_WORK(&hw->link_bh, prism2sta_processing_defer);
562 INIT_WORK(&hw->usb_work, hfa384x_usb_defer);
564 init_timer(&hw->throttle);
565 hw->throttle.function = hfa384x_usb_throttlefn;
566 hw->throttle.data = (unsigned long)hw;
568 init_timer(&hw->resptimer);
569 hw->resptimer.function = hfa384x_usbctlx_resptimerfn;
570 hw->resptimer.data = (unsigned long)hw;
572 init_timer(&hw->reqtimer);
573 hw->reqtimer.function = hfa384x_usbctlx_reqtimerfn;
574 hw->reqtimer.data = (unsigned long)hw;
576 usb_init_urb(&hw->rx_urb);
577 usb_init_urb(&hw->tx_urb);
578 usb_init_urb(&hw->ctlx_urb);
580 hw->link_status = HFA384x_LINK_NOTCONNECTED;
581 hw->state = HFA384x_STATE_INIT;
583 INIT_WORK(&hw->commsqual_bh, prism2sta_commsqual_defer);
584 init_timer(&hw->commsqual_timer);
585 hw->commsqual_timer.data = (unsigned long)hw;
586 hw->commsqual_timer.function = prism2sta_commsqual_timer;
589 /*----------------------------------------------------------------
590 * hfa384x_destroy
592 * Partner to hfa384x_create(). This function cleans up the hw
593 * structure so that it can be freed by the caller using a simple
594 * kfree. Currently, this function is just a placeholder. If, at some
595 * point in the future, an hw in the 'shutdown' state requires a 'deep'
596 * kfree, this is where it should be done. Note that if this function
597 * is called on a _running_ hw structure, the drvr_stop() function is
598 * called.
600 * Arguments:
601 * hw device structure
603 * Returns:
604 * nothing, this function is not allowed to fail.
606 * Side effects:
608 * Call context:
609 * process
610 ----------------------------------------------------------------*/
611 void hfa384x_destroy(hfa384x_t * hw)
613 struct sk_buff *skb;
615 if (hw->state == HFA384x_STATE_RUNNING)
616 hfa384x_drvr_stop(hw);
617 hw->state = HFA384x_STATE_PREINIT;
619 if (hw->scanresults) {
620 kfree(hw->scanresults);
621 hw->scanresults = NULL;
624 /* Now to clean out the auth queue */
625 while ((skb = skb_dequeue(&hw->authq)))
626 dev_kfree_skb(skb);
629 static hfa384x_usbctlx_t *usbctlx_alloc(void)
631 hfa384x_usbctlx_t *ctlx;
633 ctlx = kmalloc(sizeof(*ctlx), in_interrupt()? GFP_ATOMIC : GFP_KERNEL);
634 if (ctlx != NULL) {
635 memset(ctlx, 0, sizeof(*ctlx));
636 init_completion(&ctlx->done);
639 return ctlx;
642 static int
643 usbctlx_get_status(const hfa384x_usb_cmdresp_t * cmdresp,
644 hfa384x_cmdresult_t * result)
646 result->status = le16_to_cpu(cmdresp->status);
647 result->resp0 = le16_to_cpu(cmdresp->resp0);
648 result->resp1 = le16_to_cpu(cmdresp->resp1);
649 result->resp2 = le16_to_cpu(cmdresp->resp2);
651 pr_debug("cmdresult:status=0x%04x "
652 "resp0=0x%04x resp1=0x%04x resp2=0x%04x\n",
653 result->status, result->resp0, result->resp1, result->resp2);
655 return result->status & HFA384x_STATUS_RESULT;
658 static void
659 usbctlx_get_rridresult(const hfa384x_usb_rridresp_t * rridresp,
660 hfa384x_rridresult_t * result)
662 result->rid = le16_to_cpu(rridresp->rid);
663 result->riddata = rridresp->data;
664 result->riddata_len = ((le16_to_cpu(rridresp->frmlen) - 1) * 2);
668 /*----------------------------------------------------------------
669 * Completor object:
670 * This completor must be passed to hfa384x_usbctlx_complete_sync()
671 * when processing a CTLX that returns a hfa384x_cmdresult_t structure.
672 ----------------------------------------------------------------*/
673 struct usbctlx_cmd_completor {
674 usbctlx_completor_t head;
676 const hfa384x_usb_cmdresp_t *cmdresp;
677 hfa384x_cmdresult_t *result;
679 typedef struct usbctlx_cmd_completor usbctlx_cmd_completor_t;
681 static int usbctlx_cmd_completor_fn(usbctlx_completor_t * head)
683 usbctlx_cmd_completor_t *complete = (usbctlx_cmd_completor_t *) head;
684 return usbctlx_get_status(complete->cmdresp, complete->result);
687 static inline usbctlx_completor_t *init_cmd_completor(usbctlx_cmd_completor_t *
688 completor,
689 const
690 hfa384x_usb_cmdresp_t *
691 cmdresp,
692 hfa384x_cmdresult_t *
693 result)
695 completor->head.complete = usbctlx_cmd_completor_fn;
696 completor->cmdresp = cmdresp;
697 completor->result = result;
698 return &(completor->head);
701 /*----------------------------------------------------------------
702 * Completor object:
703 * This completor must be passed to hfa384x_usbctlx_complete_sync()
704 * when processing a CTLX that reads a RID.
705 ----------------------------------------------------------------*/
706 struct usbctlx_rrid_completor {
707 usbctlx_completor_t head;
709 const hfa384x_usb_rridresp_t *rridresp;
710 void *riddata;
711 unsigned int riddatalen;
713 typedef struct usbctlx_rrid_completor usbctlx_rrid_completor_t;
715 static int usbctlx_rrid_completor_fn(usbctlx_completor_t * head)
717 usbctlx_rrid_completor_t *complete = (usbctlx_rrid_completor_t *) head;
718 hfa384x_rridresult_t rridresult;
720 usbctlx_get_rridresult(complete->rridresp, &rridresult);
722 /* Validate the length, note body len calculation in bytes */
723 if (rridresult.riddata_len != complete->riddatalen) {
724 printk(KERN_WARNING
725 "RID len mismatch, rid=0x%04x hlen=%d fwlen=%d\n",
726 rridresult.rid,
727 complete->riddatalen, rridresult.riddata_len);
728 return -ENODATA;
731 memcpy(complete->riddata, rridresult.riddata, complete->riddatalen);
732 return 0;
735 static inline usbctlx_completor_t *init_rrid_completor(usbctlx_rrid_completor_t
736 * completor,
737 const
738 hfa384x_usb_rridresp_t *
739 rridresp, void *riddata,
740 unsigned int riddatalen)
742 completor->head.complete = usbctlx_rrid_completor_fn;
743 completor->rridresp = rridresp;
744 completor->riddata = riddata;
745 completor->riddatalen = riddatalen;
746 return &(completor->head);
749 /*----------------------------------------------------------------
750 * Completor object:
751 * Interprets the results of a synchronous RID-write
752 ----------------------------------------------------------------*/
753 typedef usbctlx_cmd_completor_t usbctlx_wrid_completor_t;
754 #define init_wrid_completor init_cmd_completor
756 /*----------------------------------------------------------------
757 * Completor object:
758 * Interprets the results of a synchronous memory-write
759 ----------------------------------------------------------------*/
760 typedef usbctlx_cmd_completor_t usbctlx_wmem_completor_t;
761 #define init_wmem_completor init_cmd_completor
763 /*----------------------------------------------------------------
764 * Completor object:
765 * Interprets the results of a synchronous memory-read
766 ----------------------------------------------------------------*/
767 struct usbctlx_rmem_completor {
768 usbctlx_completor_t head;
770 const hfa384x_usb_rmemresp_t *rmemresp;
771 void *data;
772 unsigned int len;
774 typedef struct usbctlx_rmem_completor usbctlx_rmem_completor_t;
776 static int usbctlx_rmem_completor_fn(usbctlx_completor_t * head)
778 usbctlx_rmem_completor_t *complete = (usbctlx_rmem_completor_t *) head;
780 pr_debug("rmemresp:len=%d\n", complete->rmemresp->frmlen);
781 memcpy(complete->data, complete->rmemresp->data, complete->len);
782 return 0;
785 static inline usbctlx_completor_t *init_rmem_completor(usbctlx_rmem_completor_t
786 * completor,
787 hfa384x_usb_rmemresp_t
788 * rmemresp, void *data,
789 unsigned int len)
791 completor->head.complete = usbctlx_rmem_completor_fn;
792 completor->rmemresp = rmemresp;
793 completor->data = data;
794 completor->len = len;
795 return &(completor->head);
798 /*----------------------------------------------------------------
799 * hfa384x_cb_status
801 * Ctlx_complete handler for async CMD type control exchanges.
802 * mark the hw struct as such.
804 * Note: If the handling is changed here, it should probably be
805 * changed in docmd as well.
807 * Arguments:
808 * hw hw struct
809 * ctlx completed CTLX
811 * Returns:
812 * nothing
814 * Side effects:
816 * Call context:
817 * interrupt
818 ----------------------------------------------------------------*/
819 static void hfa384x_cb_status(hfa384x_t * hw, const hfa384x_usbctlx_t * ctlx)
821 if (ctlx->usercb != NULL) {
822 hfa384x_cmdresult_t cmdresult;
824 if (ctlx->state != CTLX_COMPLETE) {
825 memset(&cmdresult, 0, sizeof(cmdresult));
826 cmdresult.status =
827 HFA384x_STATUS_RESULT_SET(HFA384x_CMD_ERR);
828 } else {
829 usbctlx_get_status(&ctlx->inbuf.cmdresp, &cmdresult);
832 ctlx->usercb(hw, &cmdresult, ctlx->usercb_data);
836 /*----------------------------------------------------------------
837 * hfa384x_cb_rrid
839 * CTLX completion handler for async RRID type control exchanges.
841 * Note: If the handling is changed here, it should probably be
842 * changed in dorrid as well.
844 * Arguments:
845 * hw hw struct
846 * ctlx completed CTLX
848 * Returns:
849 * nothing
851 * Side effects:
853 * Call context:
854 * interrupt
855 ----------------------------------------------------------------*/
856 static void hfa384x_cb_rrid(hfa384x_t * hw, const hfa384x_usbctlx_t * ctlx)
858 if (ctlx->usercb != NULL) {
859 hfa384x_rridresult_t rridresult;
861 if (ctlx->state != CTLX_COMPLETE) {
862 memset(&rridresult, 0, sizeof(rridresult));
863 rridresult.rid = le16_to_cpu(ctlx->outbuf.rridreq.rid);
864 } else {
865 usbctlx_get_rridresult(&ctlx->inbuf.rridresp,
866 &rridresult);
869 ctlx->usercb(hw, &rridresult, ctlx->usercb_data);
873 static inline int hfa384x_docmd_wait(hfa384x_t * hw, hfa384x_metacmd_t * cmd)
875 return hfa384x_docmd(hw, DOWAIT, cmd, NULL, NULL, NULL);
878 static inline int
879 hfa384x_docmd_async(hfa384x_t * hw,
880 hfa384x_metacmd_t * cmd,
881 ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data)
883 return hfa384x_docmd(hw, DOASYNC, cmd, cmdcb, usercb, usercb_data);
886 static inline int
887 hfa384x_dorrid_wait(hfa384x_t * hw, u16 rid, void *riddata,
888 unsigned int riddatalen)
890 return hfa384x_dorrid(hw, DOWAIT,
891 rid, riddata, riddatalen, NULL, NULL, NULL);
894 static inline int
895 hfa384x_dorrid_async(hfa384x_t * hw,
896 u16 rid, void *riddata, unsigned int riddatalen,
897 ctlx_cmdcb_t cmdcb,
898 ctlx_usercb_t usercb, void *usercb_data)
900 return hfa384x_dorrid(hw, DOASYNC,
901 rid, riddata, riddatalen,
902 cmdcb, usercb, usercb_data);
905 static inline int
906 hfa384x_dowrid_wait(hfa384x_t * hw, u16 rid, void *riddata,
907 unsigned int riddatalen)
909 return hfa384x_dowrid(hw, DOWAIT,
910 rid, riddata, riddatalen, NULL, NULL, NULL);
913 static inline int
914 hfa384x_dowrid_async(hfa384x_t * hw,
915 u16 rid, void *riddata, unsigned int riddatalen,
916 ctlx_cmdcb_t cmdcb,
917 ctlx_usercb_t usercb, void *usercb_data)
919 return hfa384x_dowrid(hw, DOASYNC,
920 rid, riddata, riddatalen,
921 cmdcb, usercb, usercb_data);
924 static inline int
925 hfa384x_dormem_wait(hfa384x_t * hw,
926 u16 page, u16 offset, void *data, unsigned int len)
928 return hfa384x_dormem(hw, DOWAIT,
929 page, offset, data, len, NULL, NULL, NULL);
932 static inline int
933 hfa384x_dormem_async(hfa384x_t * hw,
934 u16 page, u16 offset, void *data, unsigned int len,
935 ctlx_cmdcb_t cmdcb,
936 ctlx_usercb_t usercb, void *usercb_data)
938 return hfa384x_dormem(hw, DOASYNC,
939 page, offset, data, len,
940 cmdcb, usercb, usercb_data);
943 static inline int
944 hfa384x_dowmem_wait(hfa384x_t * hw,
945 u16 page, u16 offset, void *data, unsigned int len)
947 return hfa384x_dowmem(hw, DOWAIT,
948 page, offset, data, len, NULL, NULL, NULL);
951 static inline int
952 hfa384x_dowmem_async(hfa384x_t * hw,
953 u16 page,
954 u16 offset,
955 void *data,
956 unsigned int len,
957 ctlx_cmdcb_t cmdcb,
958 ctlx_usercb_t usercb, void *usercb_data)
960 return hfa384x_dowmem(hw, DOASYNC,
961 page, offset, data, len,
962 cmdcb, usercb, usercb_data);
965 /*----------------------------------------------------------------
966 * hfa384x_cmd_initialize
968 * Issues the initialize command and sets the hw->state based
969 * on the result.
971 * Arguments:
972 * hw device structure
974 * Returns:
975 * 0 success
976 * >0 f/w reported error - f/w status code
977 * <0 driver reported error
979 * Side effects:
981 * Call context:
982 * process
983 ----------------------------------------------------------------*/
984 int hfa384x_cmd_initialize(hfa384x_t * hw)
986 int result = 0;
987 int i;
988 hfa384x_metacmd_t cmd;
990 cmd.cmd = HFA384x_CMDCODE_INIT;
991 cmd.parm0 = 0;
992 cmd.parm1 = 0;
993 cmd.parm2 = 0;
995 result = hfa384x_docmd_wait(hw, &cmd);
997 pr_debug("cmdresp.init: "
998 "status=0x%04x, resp0=0x%04x, "
999 "resp1=0x%04x, resp2=0x%04x\n",
1000 cmd.result.status,
1001 cmd.result.resp0, cmd.result.resp1, cmd.result.resp2);
1002 if (result == 0) {
1003 for (i = 0; i < HFA384x_NUMPORTS_MAX; i++)
1004 hw->port_enabled[i] = 0;
1007 hw->link_status = HFA384x_LINK_NOTCONNECTED;
1009 return result;
1012 /*----------------------------------------------------------------
1013 * hfa384x_cmd_disable
1015 * Issues the disable command to stop communications on one of
1016 * the MACs 'ports'.
1018 * Arguments:
1019 * hw device structure
1020 * macport MAC port number (host order)
1022 * Returns:
1023 * 0 success
1024 * >0 f/w reported failure - f/w status code
1025 * <0 driver reported error (timeout|bad arg)
1027 * Side effects:
1029 * Call context:
1030 * process
1031 ----------------------------------------------------------------*/
1032 int hfa384x_cmd_disable(hfa384x_t * hw, u16 macport)
1034 int result = 0;
1035 hfa384x_metacmd_t cmd;
1037 cmd.cmd = HFA384x_CMD_CMDCODE_SET(HFA384x_CMDCODE_DISABLE) |
1038 HFA384x_CMD_MACPORT_SET(macport);
1039 cmd.parm0 = 0;
1040 cmd.parm1 = 0;
1041 cmd.parm2 = 0;
1043 result = hfa384x_docmd_wait(hw, &cmd);
1045 return result;
1048 /*----------------------------------------------------------------
1049 * hfa384x_cmd_enable
1051 * Issues the enable command to enable communications on one of
1052 * the MACs 'ports'.
1054 * Arguments:
1055 * hw device structure
1056 * macport MAC port number
1058 * Returns:
1059 * 0 success
1060 * >0 f/w reported failure - f/w status code
1061 * <0 driver reported error (timeout|bad arg)
1063 * Side effects:
1065 * Call context:
1066 * process
1067 ----------------------------------------------------------------*/
1068 int hfa384x_cmd_enable(hfa384x_t * hw, u16 macport)
1070 int result = 0;
1071 hfa384x_metacmd_t cmd;
1073 cmd.cmd = HFA384x_CMD_CMDCODE_SET(HFA384x_CMDCODE_ENABLE) |
1074 HFA384x_CMD_MACPORT_SET(macport);
1075 cmd.parm0 = 0;
1076 cmd.parm1 = 0;
1077 cmd.parm2 = 0;
1079 result = hfa384x_docmd_wait(hw, &cmd);
1081 return result;
1084 /*----------------------------------------------------------------
1085 * hfa384x_cmd_monitor
1087 * Enables the 'monitor mode' of the MAC. Here's the description of
1088 * monitor mode that I've received thus far:
1090 * "The "monitor mode" of operation is that the MAC passes all
1091 * frames for which the PLCP checks are correct. All received
1092 * MPDUs are passed to the host with MAC Port = 7, with a
1093 * receive status of good, FCS error, or undecryptable. Passing
1094 * certain MPDUs is a violation of the 802.11 standard, but useful
1095 * for a debugging tool." Normal communication is not possible
1096 * while monitor mode is enabled.
1098 * Arguments:
1099 * hw device structure
1100 * enable a code (0x0b|0x0f) that enables/disables
1101 * monitor mode. (host order)
1103 * Returns:
1104 * 0 success
1105 * >0 f/w reported failure - f/w status code
1106 * <0 driver reported error (timeout|bad arg)
1108 * Side effects:
1110 * Call context:
1111 * process
1112 ----------------------------------------------------------------*/
1113 int hfa384x_cmd_monitor(hfa384x_t * hw, u16 enable)
1115 int result = 0;
1116 hfa384x_metacmd_t cmd;
1118 cmd.cmd = HFA384x_CMD_CMDCODE_SET(HFA384x_CMDCODE_MONITOR) |
1119 HFA384x_CMD_AINFO_SET(enable);
1120 cmd.parm0 = 0;
1121 cmd.parm1 = 0;
1122 cmd.parm2 = 0;
1124 result = hfa384x_docmd_wait(hw, &cmd);
1126 return result;
1129 /*----------------------------------------------------------------
1130 * hfa384x_cmd_download
1132 * Sets the controls for the MAC controller code/data download
1133 * process. The arguments set the mode and address associated
1134 * with a download. Note that the aux registers should be enabled
1135 * prior to setting one of the download enable modes.
1137 * Arguments:
1138 * hw device structure
1139 * mode 0 - Disable programming and begin code exec
1140 * 1 - Enable volatile mem programming
1141 * 2 - Enable non-volatile mem programming
1142 * 3 - Program non-volatile section from NV download
1143 * buffer.
1144 * (host order)
1145 * lowaddr
1146 * highaddr For mode 1, sets the high & low order bits of
1147 * the "destination address". This address will be
1148 * the execution start address when download is
1149 * subsequently disabled.
1150 * For mode 2, sets the high & low order bits of
1151 * the destination in NV ram.
1152 * For modes 0 & 3, should be zero. (host order)
1153 * NOTE: these are CMD format.
1154 * codelen Length of the data to write in mode 2,
1155 * zero otherwise. (host order)
1157 * Returns:
1158 * 0 success
1159 * >0 f/w reported failure - f/w status code
1160 * <0 driver reported error (timeout|bad arg)
1162 * Side effects:
1164 * Call context:
1165 * process
1166 ----------------------------------------------------------------*/
1167 int hfa384x_cmd_download(hfa384x_t * hw, u16 mode, u16 lowaddr,
1168 u16 highaddr, u16 codelen)
1170 int result = 0;
1171 hfa384x_metacmd_t cmd;
1173 pr_debug("mode=%d, lowaddr=0x%04x, highaddr=0x%04x, codelen=%d\n",
1174 mode, lowaddr, highaddr, codelen);
1176 cmd.cmd = (HFA384x_CMD_CMDCODE_SET(HFA384x_CMDCODE_DOWNLD) |
1177 HFA384x_CMD_PROGMODE_SET(mode));
1179 cmd.parm0 = lowaddr;
1180 cmd.parm1 = highaddr;
1181 cmd.parm2 = codelen;
1183 result = hfa384x_docmd_wait(hw, &cmd);
1185 return result;
1188 /*----------------------------------------------------------------
1189 * hfa384x_corereset
1191 * Perform a reset of the hfa38xx MAC core. We assume that the hw
1192 * structure is in its "created" state. That is, it is initialized
1193 * with proper values. Note that if a reset is done after the
1194 * device has been active for awhile, the caller might have to clean
1195 * up some leftover cruft in the hw structure.
1197 * Arguments:
1198 * hw device structure
1199 * holdtime how long (in ms) to hold the reset
1200 * settletime how long (in ms) to wait after releasing
1201 * the reset
1203 * Returns:
1204 * nothing
1206 * Side effects:
1208 * Call context:
1209 * process
1210 ----------------------------------------------------------------*/
1211 int hfa384x_corereset(hfa384x_t * hw, int holdtime, int settletime, int genesis)
1213 int result = 0;
1215 result = usb_reset_device(hw->usb);
1216 if (result < 0) {
1217 printk(KERN_ERR "usb_reset_device() failed, result=%d.\n",
1218 result);
1221 return result;
1224 /*----------------------------------------------------------------
1225 * hfa384x_usbctlx_complete_sync
1227 * Waits for a synchronous CTLX object to complete,
1228 * and then handles the response.
1230 * Arguments:
1231 * hw device structure
1232 * ctlx CTLX ptr
1233 * completor functor object to decide what to
1234 * do with the CTLX's result.
1236 * Returns:
1237 * 0 Success
1238 * -ERESTARTSYS Interrupted by a signal
1239 * -EIO CTLX failed
1240 * -ENODEV Adapter was unplugged
1241 * ??? Result from completor
1243 * Side effects:
1245 * Call context:
1246 * process
1247 ----------------------------------------------------------------*/
1248 static int hfa384x_usbctlx_complete_sync(hfa384x_t * hw,
1249 hfa384x_usbctlx_t * ctlx,
1250 usbctlx_completor_t * completor)
1252 unsigned long flags;
1253 int result;
1255 result = wait_for_completion_interruptible(&ctlx->done);
1257 spin_lock_irqsave(&hw->ctlxq.lock, flags);
1260 * We can only handle the CTLX if the USB disconnect
1261 * function has not run yet ...
1263 cleanup:
1264 if (hw->wlandev->hwremoved) {
1265 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
1266 result = -ENODEV;
1267 } else if (result != 0) {
1268 int runqueue = 0;
1271 * We were probably interrupted, so delete
1272 * this CTLX asynchronously, kill the timers
1273 * and the URB, and then start the next
1274 * pending CTLX.
1276 * NOTE: We can only delete the timers and
1277 * the URB if this CTLX is active.
1279 if (ctlx == get_active_ctlx(hw)) {
1280 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
1282 del_singleshot_timer_sync(&hw->reqtimer);
1283 del_singleshot_timer_sync(&hw->resptimer);
1284 hw->req_timer_done = 1;
1285 hw->resp_timer_done = 1;
1286 usb_kill_urb(&hw->ctlx_urb);
1288 spin_lock_irqsave(&hw->ctlxq.lock, flags);
1290 runqueue = 1;
1293 * This scenario is so unlikely that I'm
1294 * happy with a grubby "goto" solution ...
1296 if (hw->wlandev->hwremoved)
1297 goto cleanup;
1301 * The completion task will send this CTLX
1302 * to the reaper the next time it runs. We
1303 * are no longer in a hurry.
1305 ctlx->reapable = 1;
1306 ctlx->state = CTLX_REQ_FAILED;
1307 list_move_tail(&ctlx->list, &hw->ctlxq.completing);
1309 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
1311 if (runqueue)
1312 hfa384x_usbctlxq_run(hw);
1313 } else {
1314 if (ctlx->state == CTLX_COMPLETE) {
1315 result = completor->complete(completor);
1316 } else {
1317 printk(KERN_WARNING "CTLX[%d] error: state(%s)\n",
1318 le16_to_cpu(ctlx->outbuf.type),
1319 ctlxstr(ctlx->state));
1320 result = -EIO;
1323 list_del(&ctlx->list);
1324 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
1325 kfree(ctlx);
1328 return result;
1331 /*----------------------------------------------------------------
1332 * hfa384x_docmd
1334 * Constructs a command CTLX and submits it.
1336 * NOTE: Any changes to the 'post-submit' code in this function
1337 * need to be carried over to hfa384x_cbcmd() since the handling
1338 * is virtually identical.
1340 * Arguments:
1341 * hw device structure
1342 * mode DOWAIT or DOASYNC
1343 * cmd cmd structure. Includes all arguments and result
1344 * data points. All in host order. in host order
1345 * cmdcb command-specific callback
1346 * usercb user callback for async calls, NULL for DOWAIT calls
1347 * usercb_data user supplied data pointer for async calls, NULL
1348 * for DOASYNC calls
1350 * Returns:
1351 * 0 success
1352 * -EIO CTLX failure
1353 * -ERESTARTSYS Awakened on signal
1354 * >0 command indicated error, Status and Resp0-2 are
1355 * in hw structure.
1357 * Side effects:
1360 * Call context:
1361 * process
1362 ----------------------------------------------------------------*/
1363 static int
1364 hfa384x_docmd(hfa384x_t * hw,
1365 CMD_MODE mode,
1366 hfa384x_metacmd_t * cmd,
1367 ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data)
1369 int result;
1370 hfa384x_usbctlx_t *ctlx;
1372 ctlx = usbctlx_alloc();
1373 if (ctlx == NULL) {
1374 result = -ENOMEM;
1375 goto done;
1378 /* Initialize the command */
1379 ctlx->outbuf.cmdreq.type = cpu_to_le16(HFA384x_USB_CMDREQ);
1380 ctlx->outbuf.cmdreq.cmd = cpu_to_le16(cmd->cmd);
1381 ctlx->outbuf.cmdreq.parm0 = cpu_to_le16(cmd->parm0);
1382 ctlx->outbuf.cmdreq.parm1 = cpu_to_le16(cmd->parm1);
1383 ctlx->outbuf.cmdreq.parm2 = cpu_to_le16(cmd->parm2);
1385 ctlx->outbufsize = sizeof(ctlx->outbuf.cmdreq);
1387 pr_debug("cmdreq: cmd=0x%04x "
1388 "parm0=0x%04x parm1=0x%04x parm2=0x%04x\n",
1389 cmd->cmd, cmd->parm0, cmd->parm1, cmd->parm2);
1391 ctlx->reapable = mode;
1392 ctlx->cmdcb = cmdcb;
1393 ctlx->usercb = usercb;
1394 ctlx->usercb_data = usercb_data;
1396 result = hfa384x_usbctlx_submit(hw, ctlx);
1397 if (result != 0) {
1398 kfree(ctlx);
1399 } else if (mode == DOWAIT) {
1400 usbctlx_cmd_completor_t completor;
1402 result =
1403 hfa384x_usbctlx_complete_sync(hw, ctlx,
1404 init_cmd_completor(&completor,
1405 &ctlx->
1406 inbuf.
1407 cmdresp,
1408 &cmd->
1409 result));
1412 done:
1413 return result;
1416 /*----------------------------------------------------------------
1417 * hfa384x_dorrid
1419 * Constructs a read rid CTLX and issues it.
1421 * NOTE: Any changes to the 'post-submit' code in this function
1422 * need to be carried over to hfa384x_cbrrid() since the handling
1423 * is virtually identical.
1425 * Arguments:
1426 * hw device structure
1427 * mode DOWAIT or DOASYNC
1428 * rid Read RID number (host order)
1429 * riddata Caller supplied buffer that MAC formatted RID.data
1430 * record will be written to for DOWAIT calls. Should
1431 * be NULL for DOASYNC calls.
1432 * riddatalen Buffer length for DOWAIT calls. Zero for DOASYNC calls.
1433 * cmdcb command callback for async calls, NULL for DOWAIT calls
1434 * usercb user callback for async calls, NULL for DOWAIT calls
1435 * usercb_data user supplied data pointer for async calls, NULL
1436 * for DOWAIT calls
1438 * Returns:
1439 * 0 success
1440 * -EIO CTLX failure
1441 * -ERESTARTSYS Awakened on signal
1442 * -ENODATA riddatalen != macdatalen
1443 * >0 command indicated error, Status and Resp0-2 are
1444 * in hw structure.
1446 * Side effects:
1448 * Call context:
1449 * interrupt (DOASYNC)
1450 * process (DOWAIT or DOASYNC)
1451 ----------------------------------------------------------------*/
1452 static int
1453 hfa384x_dorrid(hfa384x_t * hw,
1454 CMD_MODE mode,
1455 u16 rid,
1456 void *riddata,
1457 unsigned int riddatalen,
1458 ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data)
1460 int result;
1461 hfa384x_usbctlx_t *ctlx;
1463 ctlx = usbctlx_alloc();
1464 if (ctlx == NULL) {
1465 result = -ENOMEM;
1466 goto done;
1469 /* Initialize the command */
1470 ctlx->outbuf.rridreq.type = cpu_to_le16(HFA384x_USB_RRIDREQ);
1471 ctlx->outbuf.rridreq.frmlen =
1472 cpu_to_le16(sizeof(ctlx->outbuf.rridreq.rid));
1473 ctlx->outbuf.rridreq.rid = cpu_to_le16(rid);
1475 ctlx->outbufsize = sizeof(ctlx->outbuf.rridreq);
1477 ctlx->reapable = mode;
1478 ctlx->cmdcb = cmdcb;
1479 ctlx->usercb = usercb;
1480 ctlx->usercb_data = usercb_data;
1482 /* Submit the CTLX */
1483 result = hfa384x_usbctlx_submit(hw, ctlx);
1484 if (result != 0) {
1485 kfree(ctlx);
1486 } else if (mode == DOWAIT) {
1487 usbctlx_rrid_completor_t completor;
1489 result =
1490 hfa384x_usbctlx_complete_sync(hw, ctlx,
1491 init_rrid_completor
1492 (&completor,
1493 &ctlx->inbuf.rridresp,
1494 riddata, riddatalen));
1497 done:
1498 return result;
1501 /*----------------------------------------------------------------
1502 * hfa384x_dowrid
1504 * Constructs a write rid CTLX and issues it.
1506 * NOTE: Any changes to the 'post-submit' code in this function
1507 * need to be carried over to hfa384x_cbwrid() since the handling
1508 * is virtually identical.
1510 * Arguments:
1511 * hw device structure
1512 * CMD_MODE DOWAIT or DOASYNC
1513 * rid RID code
1514 * riddata Data portion of RID formatted for MAC
1515 * riddatalen Length of the data portion in bytes
1516 * cmdcb command callback for async calls, NULL for DOWAIT calls
1517 * usercb user callback for async calls, NULL for DOWAIT calls
1518 * usercb_data user supplied data pointer for async calls
1520 * Returns:
1521 * 0 success
1522 * -ETIMEDOUT timed out waiting for register ready or
1523 * command completion
1524 * >0 command indicated error, Status and Resp0-2 are
1525 * in hw structure.
1527 * Side effects:
1529 * Call context:
1530 * interrupt (DOASYNC)
1531 * process (DOWAIT or DOASYNC)
1532 ----------------------------------------------------------------*/
1533 static int
1534 hfa384x_dowrid(hfa384x_t * hw,
1535 CMD_MODE mode,
1536 u16 rid,
1537 void *riddata,
1538 unsigned int riddatalen,
1539 ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data)
1541 int result;
1542 hfa384x_usbctlx_t *ctlx;
1544 ctlx = usbctlx_alloc();
1545 if (ctlx == NULL) {
1546 result = -ENOMEM;
1547 goto done;
1550 /* Initialize the command */
1551 ctlx->outbuf.wridreq.type = cpu_to_le16(HFA384x_USB_WRIDREQ);
1552 ctlx->outbuf.wridreq.frmlen = cpu_to_le16((sizeof
1553 (ctlx->outbuf.wridreq.rid) +
1554 riddatalen + 1) / 2);
1555 ctlx->outbuf.wridreq.rid = cpu_to_le16(rid);
1556 memcpy(ctlx->outbuf.wridreq.data, riddata, riddatalen);
1558 ctlx->outbufsize = sizeof(ctlx->outbuf.wridreq.type) +
1559 sizeof(ctlx->outbuf.wridreq.frmlen) +
1560 sizeof(ctlx->outbuf.wridreq.rid) + riddatalen;
1562 ctlx->reapable = mode;
1563 ctlx->cmdcb = cmdcb;
1564 ctlx->usercb = usercb;
1565 ctlx->usercb_data = usercb_data;
1567 /* Submit the CTLX */
1568 result = hfa384x_usbctlx_submit(hw, ctlx);
1569 if (result != 0) {
1570 kfree(ctlx);
1571 } else if (mode == DOWAIT) {
1572 usbctlx_wrid_completor_t completor;
1573 hfa384x_cmdresult_t wridresult;
1575 result = hfa384x_usbctlx_complete_sync(hw,
1576 ctlx,
1577 init_wrid_completor
1578 (&completor,
1579 &ctlx->inbuf.wridresp,
1580 &wridresult));
1583 done:
1584 return result;
1587 /*----------------------------------------------------------------
1588 * hfa384x_dormem
1590 * Constructs a readmem CTLX and issues it.
1592 * NOTE: Any changes to the 'post-submit' code in this function
1593 * need to be carried over to hfa384x_cbrmem() since the handling
1594 * is virtually identical.
1596 * Arguments:
1597 * hw device structure
1598 * mode DOWAIT or DOASYNC
1599 * page MAC address space page (CMD format)
1600 * offset MAC address space offset
1601 * data Ptr to data buffer to receive read
1602 * len Length of the data to read (max == 2048)
1603 * cmdcb command callback for async calls, NULL for DOWAIT calls
1604 * usercb user callback for async calls, NULL for DOWAIT calls
1605 * usercb_data user supplied data pointer for async calls
1607 * Returns:
1608 * 0 success
1609 * -ETIMEDOUT timed out waiting for register ready or
1610 * command completion
1611 * >0 command indicated error, Status and Resp0-2 are
1612 * in hw structure.
1614 * Side effects:
1616 * Call context:
1617 * interrupt (DOASYNC)
1618 * process (DOWAIT or DOASYNC)
1619 ----------------------------------------------------------------*/
1620 static int
1621 hfa384x_dormem(hfa384x_t * hw,
1622 CMD_MODE mode,
1623 u16 page,
1624 u16 offset,
1625 void *data,
1626 unsigned int len,
1627 ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data)
1629 int result;
1630 hfa384x_usbctlx_t *ctlx;
1632 ctlx = usbctlx_alloc();
1633 if (ctlx == NULL) {
1634 result = -ENOMEM;
1635 goto done;
1638 /* Initialize the command */
1639 ctlx->outbuf.rmemreq.type = cpu_to_le16(HFA384x_USB_RMEMREQ);
1640 ctlx->outbuf.rmemreq.frmlen =
1641 cpu_to_le16(sizeof(ctlx->outbuf.rmemreq.offset) +
1642 sizeof(ctlx->outbuf.rmemreq.page) + len);
1643 ctlx->outbuf.rmemreq.offset = cpu_to_le16(offset);
1644 ctlx->outbuf.rmemreq.page = cpu_to_le16(page);
1646 ctlx->outbufsize = sizeof(ctlx->outbuf.rmemreq);
1648 pr_debug("type=0x%04x frmlen=%d offset=0x%04x page=0x%04x\n",
1649 ctlx->outbuf.rmemreq.type,
1650 ctlx->outbuf.rmemreq.frmlen,
1651 ctlx->outbuf.rmemreq.offset, ctlx->outbuf.rmemreq.page);
1653 pr_debug("pktsize=%zd\n", ROUNDUP64(sizeof(ctlx->outbuf.rmemreq)));
1655 ctlx->reapable = mode;
1656 ctlx->cmdcb = cmdcb;
1657 ctlx->usercb = usercb;
1658 ctlx->usercb_data = usercb_data;
1660 result = hfa384x_usbctlx_submit(hw, ctlx);
1661 if (result != 0) {
1662 kfree(ctlx);
1663 } else if (mode == DOWAIT) {
1664 usbctlx_rmem_completor_t completor;
1666 result =
1667 hfa384x_usbctlx_complete_sync(hw, ctlx,
1668 init_rmem_completor
1669 (&completor,
1670 &ctlx->inbuf.rmemresp, data,
1671 len));
1674 done:
1675 return result;
1678 /*----------------------------------------------------------------
1679 * hfa384x_dowmem
1681 * Constructs a writemem CTLX and issues it.
1683 * NOTE: Any changes to the 'post-submit' code in this function
1684 * need to be carried over to hfa384x_cbwmem() since the handling
1685 * is virtually identical.
1687 * Arguments:
1688 * hw device structure
1689 * mode DOWAIT or DOASYNC
1690 * page MAC address space page (CMD format)
1691 * offset MAC address space offset
1692 * data Ptr to data buffer containing write data
1693 * len Length of the data to read (max == 2048)
1694 * cmdcb command callback for async calls, NULL for DOWAIT calls
1695 * usercb user callback for async calls, NULL for DOWAIT calls
1696 * usercb_data user supplied data pointer for async calls.
1698 * Returns:
1699 * 0 success
1700 * -ETIMEDOUT timed out waiting for register ready or
1701 * command completion
1702 * >0 command indicated error, Status and Resp0-2 are
1703 * in hw structure.
1705 * Side effects:
1707 * Call context:
1708 * interrupt (DOWAIT)
1709 * process (DOWAIT or DOASYNC)
1710 ----------------------------------------------------------------*/
1711 static int
1712 hfa384x_dowmem(hfa384x_t * hw,
1713 CMD_MODE mode,
1714 u16 page,
1715 u16 offset,
1716 void *data,
1717 unsigned int len,
1718 ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data)
1720 int result;
1721 hfa384x_usbctlx_t *ctlx;
1723 pr_debug("page=0x%04x offset=0x%04x len=%d\n", page, offset, len);
1725 ctlx = usbctlx_alloc();
1726 if (ctlx == NULL) {
1727 result = -ENOMEM;
1728 goto done;
1731 /* Initialize the command */
1732 ctlx->outbuf.wmemreq.type = cpu_to_le16(HFA384x_USB_WMEMREQ);
1733 ctlx->outbuf.wmemreq.frmlen =
1734 cpu_to_le16(sizeof(ctlx->outbuf.wmemreq.offset) +
1735 sizeof(ctlx->outbuf.wmemreq.page) + len);
1736 ctlx->outbuf.wmemreq.offset = cpu_to_le16(offset);
1737 ctlx->outbuf.wmemreq.page = cpu_to_le16(page);
1738 memcpy(ctlx->outbuf.wmemreq.data, data, len);
1740 ctlx->outbufsize = sizeof(ctlx->outbuf.wmemreq.type) +
1741 sizeof(ctlx->outbuf.wmemreq.frmlen) +
1742 sizeof(ctlx->outbuf.wmemreq.offset) +
1743 sizeof(ctlx->outbuf.wmemreq.page) + len;
1745 ctlx->reapable = mode;
1746 ctlx->cmdcb = cmdcb;
1747 ctlx->usercb = usercb;
1748 ctlx->usercb_data = usercb_data;
1750 result = hfa384x_usbctlx_submit(hw, ctlx);
1751 if (result != 0) {
1752 kfree(ctlx);
1753 } else if (mode == DOWAIT) {
1754 usbctlx_wmem_completor_t completor;
1755 hfa384x_cmdresult_t wmemresult;
1757 result = hfa384x_usbctlx_complete_sync(hw,
1758 ctlx,
1759 init_wmem_completor
1760 (&completor,
1761 &ctlx->inbuf.wmemresp,
1762 &wmemresult));
1765 done:
1766 return result;
1769 /*----------------------------------------------------------------
1770 * hfa384x_drvr_commtallies
1772 * Send a commtallies inquiry to the MAC. Note that this is an async
1773 * call that will result in an info frame arriving sometime later.
1775 * Arguments:
1776 * hw device structure
1778 * Returns:
1779 * zero success.
1781 * Side effects:
1783 * Call context:
1784 * process
1785 ----------------------------------------------------------------*/
1786 int hfa384x_drvr_commtallies(hfa384x_t * hw)
1788 hfa384x_metacmd_t cmd;
1790 cmd.cmd = HFA384x_CMDCODE_INQ;
1791 cmd.parm0 = HFA384x_IT_COMMTALLIES;
1792 cmd.parm1 = 0;
1793 cmd.parm2 = 0;
1795 hfa384x_docmd_async(hw, &cmd, NULL, NULL, NULL);
1797 return 0;
1800 /*----------------------------------------------------------------
1801 * hfa384x_drvr_disable
1803 * Issues the disable command to stop communications on one of
1804 * the MACs 'ports'. Only macport 0 is valid for stations.
1805 * APs may also disable macports 1-6. Only ports that have been
1806 * previously enabled may be disabled.
1808 * Arguments:
1809 * hw device structure
1810 * macport MAC port number (host order)
1812 * Returns:
1813 * 0 success
1814 * >0 f/w reported failure - f/w status code
1815 * <0 driver reported error (timeout|bad arg)
1817 * Side effects:
1819 * Call context:
1820 * process
1821 ----------------------------------------------------------------*/
1822 int hfa384x_drvr_disable(hfa384x_t * hw, u16 macport)
1824 int result = 0;
1826 if ((!hw->isap && macport != 0) ||
1827 (hw->isap && !(macport <= HFA384x_PORTID_MAX)) ||
1828 !(hw->port_enabled[macport])) {
1829 result = -EINVAL;
1830 } else {
1831 result = hfa384x_cmd_disable(hw, macport);
1832 if (result == 0)
1833 hw->port_enabled[macport] = 0;
1835 return result;
1838 /*----------------------------------------------------------------
1839 * hfa384x_drvr_enable
1841 * Issues the enable command to enable communications on one of
1842 * the MACs 'ports'. Only macport 0 is valid for stations.
1843 * APs may also enable macports 1-6. Only ports that are currently
1844 * disabled may be enabled.
1846 * Arguments:
1847 * hw device structure
1848 * macport MAC port number
1850 * Returns:
1851 * 0 success
1852 * >0 f/w reported failure - f/w status code
1853 * <0 driver reported error (timeout|bad arg)
1855 * Side effects:
1857 * Call context:
1858 * process
1859 ----------------------------------------------------------------*/
1860 int hfa384x_drvr_enable(hfa384x_t * hw, u16 macport)
1862 int result = 0;
1864 if ((!hw->isap && macport != 0) ||
1865 (hw->isap && !(macport <= HFA384x_PORTID_MAX)) ||
1866 (hw->port_enabled[macport])) {
1867 result = -EINVAL;
1868 } else {
1869 result = hfa384x_cmd_enable(hw, macport);
1870 if (result == 0)
1871 hw->port_enabled[macport] = 1;
1873 return result;
1876 /*----------------------------------------------------------------
1877 * hfa384x_drvr_flashdl_enable
1879 * Begins the flash download state. Checks to see that we're not
1880 * already in a download state and that a port isn't enabled.
1881 * Sets the download state and retrieves the flash download
1882 * buffer location, buffer size, and timeout length.
1884 * Arguments:
1885 * hw device structure
1887 * Returns:
1888 * 0 success
1889 * >0 f/w reported error - f/w status code
1890 * <0 driver reported error
1892 * Side effects:
1894 * Call context:
1895 * process
1896 ----------------------------------------------------------------*/
1897 int hfa384x_drvr_flashdl_enable(hfa384x_t * hw)
1899 int result = 0;
1900 int i;
1902 /* Check that a port isn't active */
1903 for (i = 0; i < HFA384x_PORTID_MAX; i++) {
1904 if (hw->port_enabled[i]) {
1905 pr_debug("called when port enabled.\n");
1906 return -EINVAL;
1910 /* Check that we're not already in a download state */
1911 if (hw->dlstate != HFA384x_DLSTATE_DISABLED)
1912 return -EINVAL;
1914 /* Retrieve the buffer loc&size and timeout */
1915 if ((result = hfa384x_drvr_getconfig(hw, HFA384x_RID_DOWNLOADBUFFER,
1916 &(hw->bufinfo),
1917 sizeof(hw->bufinfo)))) {
1918 return result;
1920 hw->bufinfo.page = le16_to_cpu(hw->bufinfo.page);
1921 hw->bufinfo.offset = le16_to_cpu(hw->bufinfo.offset);
1922 hw->bufinfo.len = le16_to_cpu(hw->bufinfo.len);
1923 if ((result = hfa384x_drvr_getconfig16(hw, HFA384x_RID_MAXLOADTIME,
1924 &(hw->dltimeout)))) {
1925 return result;
1927 hw->dltimeout = le16_to_cpu(hw->dltimeout);
1929 pr_debug("flashdl_enable\n");
1931 hw->dlstate = HFA384x_DLSTATE_FLASHENABLED;
1933 return result;
1936 /*----------------------------------------------------------------
1937 * hfa384x_drvr_flashdl_disable
1939 * Ends the flash download state. Note that this will cause the MAC
1940 * firmware to restart.
1942 * Arguments:
1943 * hw device structure
1945 * Returns:
1946 * 0 success
1947 * >0 f/w reported error - f/w status code
1948 * <0 driver reported error
1950 * Side effects:
1952 * Call context:
1953 * process
1954 ----------------------------------------------------------------*/
1955 int hfa384x_drvr_flashdl_disable(hfa384x_t * hw)
1957 /* Check that we're already in the download state */
1958 if (hw->dlstate != HFA384x_DLSTATE_FLASHENABLED)
1959 return -EINVAL;
1961 pr_debug("flashdl_enable\n");
1963 /* There isn't much we can do at this point, so I don't */
1964 /* bother w/ the return value */
1965 hfa384x_cmd_download(hw, HFA384x_PROGMODE_DISABLE, 0, 0, 0);
1966 hw->dlstate = HFA384x_DLSTATE_DISABLED;
1968 return 0;
1971 /*----------------------------------------------------------------
1972 * hfa384x_drvr_flashdl_write
1974 * Performs a FLASH download of a chunk of data. First checks to see
1975 * that we're in the FLASH download state, then sets the download
1976 * mode, uses the aux functions to 1) copy the data to the flash
1977 * buffer, 2) sets the download 'write flash' mode, 3) readback and
1978 * compare. Lather rinse, repeat as many times an necessary to get
1979 * all the given data into flash.
1980 * When all data has been written using this function (possibly
1981 * repeatedly), call drvr_flashdl_disable() to end the download state
1982 * and restart the MAC.
1984 * Arguments:
1985 * hw device structure
1986 * daddr Card address to write to. (host order)
1987 * buf Ptr to data to write.
1988 * len Length of data (host order).
1990 * Returns:
1991 * 0 success
1992 * >0 f/w reported error - f/w status code
1993 * <0 driver reported error
1995 * Side effects:
1997 * Call context:
1998 * process
1999 ----------------------------------------------------------------*/
2000 int hfa384x_drvr_flashdl_write(hfa384x_t * hw, u32 daddr, void *buf, u32 len)
2002 int result = 0;
2003 u32 dlbufaddr;
2004 int nburns;
2005 u32 burnlen;
2006 u32 burndaddr;
2007 u16 burnlo;
2008 u16 burnhi;
2009 int nwrites;
2010 u8 *writebuf;
2011 u16 writepage;
2012 u16 writeoffset;
2013 u32 writelen;
2014 int i;
2015 int j;
2017 pr_debug("daddr=0x%08x len=%d\n", daddr, len);
2019 /* Check that we're in the flash download state */
2020 if (hw->dlstate != HFA384x_DLSTATE_FLASHENABLED)
2021 return -EINVAL;
2023 printk(KERN_INFO "Download %d bytes to flash @0x%06x\n", len, daddr);
2025 /* Convert to flat address for arithmetic */
2026 /* NOTE: dlbuffer RID stores the address in AUX format */
2027 dlbufaddr =
2028 HFA384x_ADDR_AUX_MKFLAT(hw->bufinfo.page, hw->bufinfo.offset);
2029 pr_debug("dlbuf.page=0x%04x dlbuf.offset=0x%04x dlbufaddr=0x%08x\n",
2030 hw->bufinfo.page, hw->bufinfo.offset, dlbufaddr);
2032 #if 0
2033 printk(KERN_WARNING "dlbuf@0x%06lx len=%d to=%d\n", dlbufaddr,
2034 hw->bufinfo.len, hw->dltimeout);
2035 #endif
2036 /* Calculations to determine how many fills of the dlbuffer to do
2037 * and how many USB wmemreq's to do for each fill. At this point
2038 * in time, the dlbuffer size and the wmemreq size are the same.
2039 * Therefore, nwrites should always be 1. The extra complexity
2040 * here is a hedge against future changes.
2043 /* Figure out how many times to do the flash programming */
2044 nburns = len / hw->bufinfo.len;
2045 nburns += (len % hw->bufinfo.len) ? 1 : 0;
2047 /* For each flash program cycle, how many USB wmemreq's are needed? */
2048 nwrites = hw->bufinfo.len / HFA384x_USB_RWMEM_MAXLEN;
2049 nwrites += (hw->bufinfo.len % HFA384x_USB_RWMEM_MAXLEN) ? 1 : 0;
2051 /* For each burn */
2052 for (i = 0; i < nburns; i++) {
2053 /* Get the dest address and len */
2054 burnlen = (len - (hw->bufinfo.len * i)) > hw->bufinfo.len ?
2055 hw->bufinfo.len : (len - (hw->bufinfo.len * i));
2056 burndaddr = daddr + (hw->bufinfo.len * i);
2057 burnlo = HFA384x_ADDR_CMD_MKOFF(burndaddr);
2058 burnhi = HFA384x_ADDR_CMD_MKPAGE(burndaddr);
2060 printk(KERN_INFO "Writing %d bytes to flash @0x%06x\n",
2061 burnlen, burndaddr);
2063 /* Set the download mode */
2064 result = hfa384x_cmd_download(hw, HFA384x_PROGMODE_NV,
2065 burnlo, burnhi, burnlen);
2066 if (result) {
2067 printk(KERN_ERR "download(NV,lo=%x,hi=%x,len=%x) "
2068 "cmd failed, result=%d. Aborting d/l\n",
2069 burnlo, burnhi, burnlen, result);
2070 goto exit_proc;
2073 /* copy the data to the flash download buffer */
2074 for (j = 0; j < nwrites; j++) {
2075 writebuf = buf +
2076 (i * hw->bufinfo.len) +
2077 (j * HFA384x_USB_RWMEM_MAXLEN);
2079 writepage = HFA384x_ADDR_CMD_MKPAGE(dlbufaddr +
2080 (j *
2081 HFA384x_USB_RWMEM_MAXLEN));
2082 writeoffset =
2083 HFA384x_ADDR_CMD_MKOFF(dlbufaddr +
2084 (j *
2085 HFA384x_USB_RWMEM_MAXLEN));
2087 writelen = burnlen - (j * HFA384x_USB_RWMEM_MAXLEN);
2088 writelen = writelen > HFA384x_USB_RWMEM_MAXLEN ?
2089 HFA384x_USB_RWMEM_MAXLEN : writelen;
2091 result = hfa384x_dowmem_wait(hw,
2092 writepage,
2093 writeoffset,
2094 writebuf, writelen);
2097 /* set the download 'write flash' mode */
2098 result = hfa384x_cmd_download(hw,
2099 HFA384x_PROGMODE_NVWRITE,
2100 0, 0, 0);
2101 if (result) {
2102 printk(KERN_ERR
2103 "download(NVWRITE,lo=%x,hi=%x,len=%x) "
2104 "cmd failed, result=%d. Aborting d/l\n",
2105 burnlo, burnhi, burnlen, result);
2106 goto exit_proc;
2109 /* TODO: We really should do a readback and compare. */
2112 exit_proc:
2114 /* Leave the firmware in the 'post-prog' mode. flashdl_disable will */
2115 /* actually disable programming mode. Remember, that will cause the */
2116 /* the firmware to effectively reset itself. */
2118 return result;
2121 /*----------------------------------------------------------------
2122 * hfa384x_drvr_getconfig
2124 * Performs the sequence necessary to read a config/info item.
2126 * Arguments:
2127 * hw device structure
2128 * rid config/info record id (host order)
2129 * buf host side record buffer. Upon return it will
2130 * contain the body portion of the record (minus the
2131 * RID and len).
2132 * len buffer length (in bytes, should match record length)
2134 * Returns:
2135 * 0 success
2136 * >0 f/w reported error - f/w status code
2137 * <0 driver reported error
2138 * -ENODATA length mismatch between argument and retrieved
2139 * record.
2141 * Side effects:
2143 * Call context:
2144 * process
2145 ----------------------------------------------------------------*/
2146 int hfa384x_drvr_getconfig(hfa384x_t * hw, u16 rid, void *buf, u16 len)
2148 int result;
2150 result = hfa384x_dorrid_wait(hw, rid, buf, len);
2152 return result;
2155 /*----------------------------------------------------------------
2156 * hfa384x_drvr_getconfig_async
2158 * Performs the sequence necessary to perform an async read of
2159 * of a config/info item.
2161 * Arguments:
2162 * hw device structure
2163 * rid config/info record id (host order)
2164 * buf host side record buffer. Upon return it will
2165 * contain the body portion of the record (minus the
2166 * RID and len).
2167 * len buffer length (in bytes, should match record length)
2168 * cbfn caller supplied callback, called when the command
2169 * is done (successful or not).
2170 * cbfndata pointer to some caller supplied data that will be
2171 * passed in as an argument to the cbfn.
2173 * Returns:
2174 * nothing the cbfn gets a status argument identifying if
2175 * any errors occur.
2176 * Side effects:
2177 * Queues an hfa384x_usbcmd_t for subsequent execution.
2179 * Call context:
2180 * Any
2181 ----------------------------------------------------------------*/
2183 hfa384x_drvr_getconfig_async(hfa384x_t * hw,
2184 u16 rid, ctlx_usercb_t usercb, void *usercb_data)
2186 return hfa384x_dorrid_async(hw, rid, NULL, 0,
2187 hfa384x_cb_rrid, usercb, usercb_data);
2190 /*----------------------------------------------------------------
2191 * hfa384x_drvr_setconfig_async
2193 * Performs the sequence necessary to write a config/info item.
2195 * Arguments:
2196 * hw device structure
2197 * rid config/info record id (in host order)
2198 * buf host side record buffer
2199 * len buffer length (in bytes)
2200 * usercb completion callback
2201 * usercb_data completion callback argument
2203 * Returns:
2204 * 0 success
2205 * >0 f/w reported error - f/w status code
2206 * <0 driver reported error
2208 * Side effects:
2210 * Call context:
2211 * process
2212 ----------------------------------------------------------------*/
2214 hfa384x_drvr_setconfig_async(hfa384x_t * hw,
2215 u16 rid,
2216 void *buf,
2217 u16 len, ctlx_usercb_t usercb, void *usercb_data)
2219 return hfa384x_dowrid_async(hw, rid, buf, len,
2220 hfa384x_cb_status, usercb, usercb_data);
2223 /*----------------------------------------------------------------
2224 * hfa384x_drvr_ramdl_disable
2226 * Ends the ram download state.
2228 * Arguments:
2229 * hw device structure
2231 * Returns:
2232 * 0 success
2233 * >0 f/w reported error - f/w status code
2234 * <0 driver reported error
2236 * Side effects:
2238 * Call context:
2239 * process
2240 ----------------------------------------------------------------*/
2241 int hfa384x_drvr_ramdl_disable(hfa384x_t * hw)
2243 /* Check that we're already in the download state */
2244 if (hw->dlstate != HFA384x_DLSTATE_RAMENABLED)
2245 return -EINVAL;
2247 pr_debug("ramdl_disable()\n");
2249 /* There isn't much we can do at this point, so I don't */
2250 /* bother w/ the return value */
2251 hfa384x_cmd_download(hw, HFA384x_PROGMODE_DISABLE, 0, 0, 0);
2252 hw->dlstate = HFA384x_DLSTATE_DISABLED;
2254 return 0;
2257 /*----------------------------------------------------------------
2258 * hfa384x_drvr_ramdl_enable
2260 * Begins the ram download state. Checks to see that we're not
2261 * already in a download state and that a port isn't enabled.
2262 * Sets the download state and calls cmd_download with the
2263 * ENABLE_VOLATILE subcommand and the exeaddr argument.
2265 * Arguments:
2266 * hw device structure
2267 * exeaddr the card execution address that will be
2268 * jumped to when ramdl_disable() is called
2269 * (host order).
2271 * Returns:
2272 * 0 success
2273 * >0 f/w reported error - f/w status code
2274 * <0 driver reported error
2276 * Side effects:
2278 * Call context:
2279 * process
2280 ----------------------------------------------------------------*/
2281 int hfa384x_drvr_ramdl_enable(hfa384x_t * hw, u32 exeaddr)
2283 int result = 0;
2284 u16 lowaddr;
2285 u16 hiaddr;
2286 int i;
2288 /* Check that a port isn't active */
2289 for (i = 0; i < HFA384x_PORTID_MAX; i++) {
2290 if (hw->port_enabled[i]) {
2291 printk(KERN_ERR
2292 "Can't download with a macport enabled.\n");
2293 return -EINVAL;
2297 /* Check that we're not already in a download state */
2298 if (hw->dlstate != HFA384x_DLSTATE_DISABLED) {
2299 printk(KERN_ERR "Download state not disabled.\n");
2300 return -EINVAL;
2303 pr_debug("ramdl_enable, exeaddr=0x%08x\n", exeaddr);
2305 /* Call the download(1,addr) function */
2306 lowaddr = HFA384x_ADDR_CMD_MKOFF(exeaddr);
2307 hiaddr = HFA384x_ADDR_CMD_MKPAGE(exeaddr);
2309 result = hfa384x_cmd_download(hw, HFA384x_PROGMODE_RAM,
2310 lowaddr, hiaddr, 0);
2312 if (result == 0) {
2313 /* Set the download state */
2314 hw->dlstate = HFA384x_DLSTATE_RAMENABLED;
2315 } else {
2316 pr_debug("cmd_download(0x%04x, 0x%04x) failed, result=%d.\n",
2317 lowaddr, hiaddr, result);
2320 return result;
2323 /*----------------------------------------------------------------
2324 * hfa384x_drvr_ramdl_write
2326 * Performs a RAM download of a chunk of data. First checks to see
2327 * that we're in the RAM download state, then uses the [read|write]mem USB
2328 * commands to 1) copy the data, 2) readback and compare. The download
2329 * state is unaffected. When all data has been written using
2330 * this function, call drvr_ramdl_disable() to end the download state
2331 * and restart the MAC.
2333 * Arguments:
2334 * hw device structure
2335 * daddr Card address to write to. (host order)
2336 * buf Ptr to data to write.
2337 * len Length of data (host order).
2339 * Returns:
2340 * 0 success
2341 * >0 f/w reported error - f/w status code
2342 * <0 driver reported error
2344 * Side effects:
2346 * Call context:
2347 * process
2348 ----------------------------------------------------------------*/
2349 int hfa384x_drvr_ramdl_write(hfa384x_t * hw, u32 daddr, void *buf, u32 len)
2351 int result = 0;
2352 int nwrites;
2353 u8 *data = buf;
2354 int i;
2355 u32 curraddr;
2356 u16 currpage;
2357 u16 curroffset;
2358 u16 currlen;
2360 /* Check that we're in the ram download state */
2361 if (hw->dlstate != HFA384x_DLSTATE_RAMENABLED)
2362 return -EINVAL;
2364 printk(KERN_INFO "Writing %d bytes to ram @0x%06x\n", len, daddr);
2366 /* How many dowmem calls? */
2367 nwrites = len / HFA384x_USB_RWMEM_MAXLEN;
2368 nwrites += len % HFA384x_USB_RWMEM_MAXLEN ? 1 : 0;
2370 /* Do blocking wmem's */
2371 for (i = 0; i < nwrites; i++) {
2372 /* make address args */
2373 curraddr = daddr + (i * HFA384x_USB_RWMEM_MAXLEN);
2374 currpage = HFA384x_ADDR_CMD_MKPAGE(curraddr);
2375 curroffset = HFA384x_ADDR_CMD_MKOFF(curraddr);
2376 currlen = len - (i * HFA384x_USB_RWMEM_MAXLEN);
2377 if (currlen > HFA384x_USB_RWMEM_MAXLEN)
2378 currlen = HFA384x_USB_RWMEM_MAXLEN;
2380 /* Do blocking ctlx */
2381 result = hfa384x_dowmem_wait(hw,
2382 currpage,
2383 curroffset,
2384 data +
2385 (i * HFA384x_USB_RWMEM_MAXLEN),
2386 currlen);
2388 if (result)
2389 break;
2391 /* TODO: We really should have a readback. */
2394 return result;
2397 /*----------------------------------------------------------------
2398 * hfa384x_drvr_readpda
2400 * Performs the sequence to read the PDA space. Note there is no
2401 * drvr_writepda() function. Writing a PDA is
2402 * generally implemented by a calling component via calls to
2403 * cmd_download and writing to the flash download buffer via the
2404 * aux regs.
2406 * Arguments:
2407 * hw device structure
2408 * buf buffer to store PDA in
2409 * len buffer length
2411 * Returns:
2412 * 0 success
2413 * >0 f/w reported error - f/w status code
2414 * <0 driver reported error
2415 * -ETIMEDOUT timout waiting for the cmd regs to become
2416 * available, or waiting for the control reg
2417 * to indicate the Aux port is enabled.
2418 * -ENODATA the buffer does NOT contain a valid PDA.
2419 * Either the card PDA is bad, or the auxdata
2420 * reads are giving us garbage.
2423 * Side effects:
2425 * Call context:
2426 * process or non-card interrupt.
2427 ----------------------------------------------------------------*/
2428 int hfa384x_drvr_readpda(hfa384x_t * hw, void *buf, unsigned int len)
2430 int result = 0;
2431 u16 *pda = buf;
2432 int pdaok = 0;
2433 int morepdrs = 1;
2434 int currpdr = 0; /* word offset of the current pdr */
2435 size_t i;
2436 u16 pdrlen; /* pdr length in bytes, host order */
2437 u16 pdrcode; /* pdr code, host order */
2438 u16 currpage;
2439 u16 curroffset;
2440 struct pdaloc {
2441 u32 cardaddr;
2442 u16 auxctl;
2443 } pdaloc[] = {
2445 HFA3842_PDA_BASE, 0}, {
2446 HFA3841_PDA_BASE, 0}, {
2447 HFA3841_PDA_BOGUS_BASE, 0}
2450 /* Read the pda from each known address. */
2451 for (i = 0; i < ARRAY_SIZE(pdaloc); i++) {
2452 /* Make address */
2453 currpage = HFA384x_ADDR_CMD_MKPAGE(pdaloc[i].cardaddr);
2454 curroffset = HFA384x_ADDR_CMD_MKOFF(pdaloc[i].cardaddr);
2456 result = hfa384x_dormem_wait(hw, currpage, curroffset, buf, len); /* units of bytes */
2458 if (result) {
2459 printk(KERN_WARNING
2460 "Read from index %zd failed, continuing\n", i);
2461 continue;
2464 /* Test for garbage */
2465 pdaok = 1; /* initially assume good */
2466 morepdrs = 1;
2467 while (pdaok && morepdrs) {
2468 pdrlen = le16_to_cpu(pda[currpdr]) * 2;
2469 pdrcode = le16_to_cpu(pda[currpdr + 1]);
2470 /* Test the record length */
2471 if (pdrlen > HFA384x_PDR_LEN_MAX || pdrlen == 0) {
2472 printk(KERN_ERR "pdrlen invalid=%d\n", pdrlen);
2473 pdaok = 0;
2474 break;
2476 /* Test the code */
2477 if (!hfa384x_isgood_pdrcode(pdrcode)) {
2478 printk(KERN_ERR "pdrcode invalid=%d\n",
2479 pdrcode);
2480 pdaok = 0;
2481 break;
2483 /* Test for completion */
2484 if (pdrcode == HFA384x_PDR_END_OF_PDA)
2485 morepdrs = 0;
2487 /* Move to the next pdr (if necessary) */
2488 if (morepdrs) {
2489 /* note the access to pda[], need words here */
2490 currpdr += le16_to_cpu(pda[currpdr]) + 1;
2493 if (pdaok) {
2494 printk(KERN_INFO
2495 "PDA Read from 0x%08x in %s space.\n",
2496 pdaloc[i].cardaddr,
2497 pdaloc[i].auxctl == 0 ? "EXTDS" :
2498 pdaloc[i].auxctl == 1 ? "NV" :
2499 pdaloc[i].auxctl == 2 ? "PHY" :
2500 pdaloc[i].auxctl == 3 ? "ICSRAM" :
2501 "<bogus auxctl>");
2502 break;
2505 result = pdaok ? 0 : -ENODATA;
2507 if (result)
2508 pr_debug("Failure: pda is not okay\n");
2510 return result;
2513 /*----------------------------------------------------------------
2514 * hfa384x_drvr_setconfig
2516 * Performs the sequence necessary to write a config/info item.
2518 * Arguments:
2519 * hw device structure
2520 * rid config/info record id (in host order)
2521 * buf host side record buffer
2522 * len buffer length (in bytes)
2524 * Returns:
2525 * 0 success
2526 * >0 f/w reported error - f/w status code
2527 * <0 driver reported error
2529 * Side effects:
2531 * Call context:
2532 * process
2533 ----------------------------------------------------------------*/
2534 int hfa384x_drvr_setconfig(hfa384x_t * hw, u16 rid, void *buf, u16 len)
2536 return hfa384x_dowrid_wait(hw, rid, buf, len);
2539 /*----------------------------------------------------------------
2540 * hfa384x_drvr_start
2542 * Issues the MAC initialize command, sets up some data structures,
2543 * and enables the interrupts. After this function completes, the
2544 * low-level stuff should be ready for any/all commands.
2546 * Arguments:
2547 * hw device structure
2548 * Returns:
2549 * 0 success
2550 * >0 f/w reported error - f/w status code
2551 * <0 driver reported error
2553 * Side effects:
2555 * Call context:
2556 * process
2557 ----------------------------------------------------------------*/
2559 int hfa384x_drvr_start(hfa384x_t * hw)
2561 int result, result1, result2;
2562 u16 status;
2564 might_sleep();
2566 /* Clear endpoint stalls - but only do this if the endpoint
2567 * is showing a stall status. Some prism2 cards seem to behave
2568 * badly if a clear_halt is called when the endpoint is already
2569 * ok
2571 result =
2572 usb_get_status(hw->usb, USB_RECIP_ENDPOINT, hw->endp_in, &status);
2573 if (result < 0) {
2574 printk(KERN_ERR "Cannot get bulk in endpoint status.\n");
2575 goto done;
2577 if ((status == 1) && usb_clear_halt(hw->usb, hw->endp_in))
2578 printk(KERN_ERR "Failed to reset bulk in endpoint.\n");
2580 result =
2581 usb_get_status(hw->usb, USB_RECIP_ENDPOINT, hw->endp_out, &status);
2582 if (result < 0) {
2583 printk(KERN_ERR "Cannot get bulk out endpoint status.\n");
2584 goto done;
2586 if ((status == 1) && usb_clear_halt(hw->usb, hw->endp_out))
2587 printk(KERN_ERR "Failed to reset bulk out endpoint.\n");
2589 /* Synchronous unlink, in case we're trying to restart the driver */
2590 usb_kill_urb(&hw->rx_urb);
2592 /* Post the IN urb */
2593 result = submit_rx_urb(hw, GFP_KERNEL);
2594 if (result != 0) {
2595 printk(KERN_ERR
2596 "Fatal, failed to submit RX URB, result=%d\n", result);
2597 goto done;
2600 /* Call initialize twice, with a 1 second sleep in between.
2601 * This is a nasty work-around since many prism2 cards seem to
2602 * need time to settle after an init from cold. The second
2603 * call to initialize in theory is not necessary - but we call
2604 * it anyway as a double insurance policy:
2605 * 1) If the first init should fail, the second may well succeed
2606 * and the card can still be used
2607 * 2) It helps ensures all is well with the card after the first
2608 * init and settle time.
2610 result1 = hfa384x_cmd_initialize(hw);
2611 msleep(1000);
2612 result = result2 = hfa384x_cmd_initialize(hw);
2613 if (result1 != 0) {
2614 if (result2 != 0) {
2615 printk(KERN_ERR
2616 "cmd_initialize() failed on two attempts, results %d and %d\n",
2617 result1, result2);
2618 usb_kill_urb(&hw->rx_urb);
2619 goto done;
2620 } else {
2621 pr_debug("First cmd_initialize() failed (result %d),\n",
2622 result1);
2623 pr_debug
2624 ("but second attempt succeeded. All should be ok\n");
2626 } else if (result2 != 0) {
2627 printk(KERN_WARNING
2628 "First cmd_initialize() succeeded, but second attempt failed (result=%d)\n",
2629 result2);
2630 printk(KERN_WARNING
2631 "Most likely the card will be functional\n");
2632 goto done;
2635 hw->state = HFA384x_STATE_RUNNING;
2637 done:
2638 return result;
2641 /*----------------------------------------------------------------
2642 * hfa384x_drvr_stop
2644 * Shuts down the MAC to the point where it is safe to unload the
2645 * driver. Any subsystem that may be holding a data or function
2646 * ptr into the driver must be cleared/deinitialized.
2648 * Arguments:
2649 * hw device structure
2650 * Returns:
2651 * 0 success
2652 * >0 f/w reported error - f/w status code
2653 * <0 driver reported error
2655 * Side effects:
2657 * Call context:
2658 * process
2659 ----------------------------------------------------------------*/
2660 int hfa384x_drvr_stop(hfa384x_t * hw)
2662 int result = 0;
2663 int i;
2665 might_sleep();
2667 /* There's no need for spinlocks here. The USB "disconnect"
2668 * function sets this "removed" flag and then calls us.
2670 if (!hw->wlandev->hwremoved) {
2671 /* Call initialize to leave the MAC in its 'reset' state */
2672 hfa384x_cmd_initialize(hw);
2674 /* Cancel the rxurb */
2675 usb_kill_urb(&hw->rx_urb);
2678 hw->link_status = HFA384x_LINK_NOTCONNECTED;
2679 hw->state = HFA384x_STATE_INIT;
2681 del_timer_sync(&hw->commsqual_timer);
2683 /* Clear all the port status */
2684 for (i = 0; i < HFA384x_NUMPORTS_MAX; i++)
2685 hw->port_enabled[i] = 0;
2687 return result;
2690 /*----------------------------------------------------------------
2691 * hfa384x_drvr_txframe
2693 * Takes a frame from prism2sta and queues it for transmission.
2695 * Arguments:
2696 * hw device structure
2697 * skb packet buffer struct. Contains an 802.11
2698 * data frame.
2699 * p80211_hdr points to the 802.11 header for the packet.
2700 * Returns:
2701 * 0 Success and more buffs available
2702 * 1 Success but no more buffs
2703 * 2 Allocation failure
2704 * 4 Buffer full or queue busy
2706 * Side effects:
2708 * Call context:
2709 * interrupt
2710 ----------------------------------------------------------------*/
2711 int hfa384x_drvr_txframe(hfa384x_t * hw, struct sk_buff *skb,
2712 p80211_hdr_t * p80211_hdr,
2713 p80211_metawep_t * p80211_wep)
2715 int usbpktlen = sizeof(hfa384x_tx_frame_t);
2716 int result;
2717 int ret;
2718 char *ptr;
2720 if (hw->tx_urb.status == -EINPROGRESS) {
2721 printk(KERN_WARNING "TX URB already in use\n");
2722 result = 3;
2723 goto exit;
2726 /* Build Tx frame structure */
2727 /* Set up the control field */
2728 memset(&hw->txbuff.txfrm.desc, 0, sizeof(hw->txbuff.txfrm.desc));
2730 /* Setup the usb type field */
2731 hw->txbuff.type = cpu_to_le16(HFA384x_USB_TXFRM);
2733 /* Set up the sw_support field to identify this frame */
2734 hw->txbuff.txfrm.desc.sw_support = 0x0123;
2736 /* Tx complete and Tx exception disable per dleach. Might be causing
2737 * buf depletion
2739 /* #define DOEXC SLP -- doboth breaks horribly under load, doexc less so. */
2740 #if defined(DOBOTH)
2741 hw->txbuff.txfrm.desc.tx_control =
2742 HFA384x_TX_MACPORT_SET(0) | HFA384x_TX_STRUCTYPE_SET(1) |
2743 HFA384x_TX_TXEX_SET(1) | HFA384x_TX_TXOK_SET(1);
2744 #elif defined(DOEXC)
2745 hw->txbuff.txfrm.desc.tx_control =
2746 HFA384x_TX_MACPORT_SET(0) | HFA384x_TX_STRUCTYPE_SET(1) |
2747 HFA384x_TX_TXEX_SET(1) | HFA384x_TX_TXOK_SET(0);
2748 #else
2749 hw->txbuff.txfrm.desc.tx_control =
2750 HFA384x_TX_MACPORT_SET(0) | HFA384x_TX_STRUCTYPE_SET(1) |
2751 HFA384x_TX_TXEX_SET(0) | HFA384x_TX_TXOK_SET(0);
2752 #endif
2753 hw->txbuff.txfrm.desc.tx_control =
2754 cpu_to_le16(hw->txbuff.txfrm.desc.tx_control);
2756 /* copy the header over to the txdesc */
2757 memcpy(&(hw->txbuff.txfrm.desc.frame_control), p80211_hdr,
2758 sizeof(p80211_hdr_t));
2760 /* if we're using host WEP, increase size by IV+ICV */
2761 if (p80211_wep->data) {
2762 hw->txbuff.txfrm.desc.data_len = cpu_to_le16(skb->len + 8);
2763 usbpktlen += 8;
2764 } else {
2765 hw->txbuff.txfrm.desc.data_len = cpu_to_le16(skb->len);
2768 usbpktlen += skb->len;
2770 /* copy over the WEP IV if we are using host WEP */
2771 ptr = hw->txbuff.txfrm.data;
2772 if (p80211_wep->data) {
2773 memcpy(ptr, p80211_wep->iv, sizeof(p80211_wep->iv));
2774 ptr += sizeof(p80211_wep->iv);
2775 memcpy(ptr, p80211_wep->data, skb->len);
2776 } else {
2777 memcpy(ptr, skb->data, skb->len);
2779 /* copy over the packet data */
2780 ptr += skb->len;
2782 /* copy over the WEP ICV if we are using host WEP */
2783 if (p80211_wep->data)
2784 memcpy(ptr, p80211_wep->icv, sizeof(p80211_wep->icv));
2786 /* Send the USB packet */
2787 usb_fill_bulk_urb(&(hw->tx_urb), hw->usb,
2788 hw->endp_out,
2789 &(hw->txbuff), ROUNDUP64(usbpktlen),
2790 hfa384x_usbout_callback, hw->wlandev);
2791 hw->tx_urb.transfer_flags |= USB_QUEUE_BULK;
2793 result = 1;
2794 ret = submit_tx_urb(hw, &hw->tx_urb, GFP_ATOMIC);
2795 if (ret != 0) {
2796 printk(KERN_ERR "submit_tx_urb() failed, error=%d\n", ret);
2797 result = 3;
2800 exit:
2801 return result;
2804 void hfa384x_tx_timeout(wlandevice_t * wlandev)
2806 hfa384x_t *hw = wlandev->priv;
2807 unsigned long flags;
2809 spin_lock_irqsave(&hw->ctlxq.lock, flags);
2811 if (!hw->wlandev->hwremoved &&
2812 /* Note the bitwise OR, not the logical OR. */
2813 (!test_and_set_bit(WORK_TX_HALT, &hw->usb_flags) |
2814 !test_and_set_bit(WORK_RX_HALT, &hw->usb_flags))) {
2815 schedule_work(&hw->usb_work);
2818 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
2821 /*----------------------------------------------------------------
2822 * hfa384x_usbctlx_reaper_task
2824 * Tasklet to delete dead CTLX objects
2826 * Arguments:
2827 * data ptr to a hfa384x_t
2829 * Returns:
2831 * Call context:
2832 * Interrupt
2833 ----------------------------------------------------------------*/
2834 static void hfa384x_usbctlx_reaper_task(unsigned long data)
2836 hfa384x_t *hw = (hfa384x_t *) data;
2837 struct list_head *entry;
2838 struct list_head *temp;
2839 unsigned long flags;
2841 spin_lock_irqsave(&hw->ctlxq.lock, flags);
2843 /* This list is guaranteed to be empty if someone
2844 * has unplugged the adapter.
2846 list_for_each_safe(entry, temp, &hw->ctlxq.reapable) {
2847 hfa384x_usbctlx_t *ctlx;
2849 ctlx = list_entry(entry, hfa384x_usbctlx_t, list);
2850 list_del(&ctlx->list);
2851 kfree(ctlx);
2854 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
2858 /*----------------------------------------------------------------
2859 * hfa384x_usbctlx_completion_task
2861 * Tasklet to call completion handlers for returned CTLXs
2863 * Arguments:
2864 * data ptr to hfa384x_t
2866 * Returns:
2867 * Nothing
2869 * Call context:
2870 * Interrupt
2871 ----------------------------------------------------------------*/
2872 static void hfa384x_usbctlx_completion_task(unsigned long data)
2874 hfa384x_t *hw = (hfa384x_t *) data;
2875 struct list_head *entry;
2876 struct list_head *temp;
2877 unsigned long flags;
2879 int reap = 0;
2881 spin_lock_irqsave(&hw->ctlxq.lock, flags);
2883 /* This list is guaranteed to be empty if someone
2884 * has unplugged the adapter ...
2886 list_for_each_safe(entry, temp, &hw->ctlxq.completing) {
2887 hfa384x_usbctlx_t *ctlx;
2889 ctlx = list_entry(entry, hfa384x_usbctlx_t, list);
2891 /* Call the completion function that this
2892 * command was assigned, assuming it has one.
2894 if (ctlx->cmdcb != NULL) {
2895 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
2896 ctlx->cmdcb(hw, ctlx);
2897 spin_lock_irqsave(&hw->ctlxq.lock, flags);
2899 /* Make sure we don't try and complete
2900 * this CTLX more than once!
2902 ctlx->cmdcb = NULL;
2904 /* Did someone yank the adapter out
2905 * while our list was (briefly) unlocked?
2907 if (hw->wlandev->hwremoved) {
2908 reap = 0;
2909 break;
2914 * "Reapable" CTLXs are ones which don't have any
2915 * threads waiting for them to die. Hence they must
2916 * be delivered to The Reaper!
2918 if (ctlx->reapable) {
2919 /* Move the CTLX off the "completing" list (hopefully)
2920 * on to the "reapable" list where the reaper task
2921 * can find it. And "reapable" means that this CTLX
2922 * isn't sitting on a wait-queue somewhere.
2924 list_move_tail(&ctlx->list, &hw->ctlxq.reapable);
2925 reap = 1;
2928 complete(&ctlx->done);
2930 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
2932 if (reap)
2933 tasklet_schedule(&hw->reaper_bh);
2936 /*----------------------------------------------------------------
2937 * unlocked_usbctlx_cancel_async
2939 * Mark the CTLX dead asynchronously, and ensure that the
2940 * next command on the queue is run afterwards.
2942 * Arguments:
2943 * hw ptr to the hfa384x_t structure
2944 * ctlx ptr to a CTLX structure
2946 * Returns:
2947 * 0 the CTLX's URB is inactive
2948 * -EINPROGRESS the URB is currently being unlinked
2950 * Call context:
2951 * Either process or interrupt, but presumably interrupt
2952 ----------------------------------------------------------------*/
2953 static int unlocked_usbctlx_cancel_async(hfa384x_t * hw,
2954 hfa384x_usbctlx_t * ctlx)
2956 int ret;
2959 * Try to delete the URB containing our request packet.
2960 * If we succeed, then its completion handler will be
2961 * called with a status of -ECONNRESET.
2963 hw->ctlx_urb.transfer_flags |= URB_ASYNC_UNLINK;
2964 ret = usb_unlink_urb(&hw->ctlx_urb);
2966 if (ret != -EINPROGRESS) {
2968 * The OUT URB had either already completed
2969 * or was still in the pending queue, so the
2970 * URB's completion function will not be called.
2971 * We will have to complete the CTLX ourselves.
2973 ctlx->state = CTLX_REQ_FAILED;
2974 unlocked_usbctlx_complete(hw, ctlx);
2975 ret = 0;
2978 return ret;
2981 /*----------------------------------------------------------------
2982 * unlocked_usbctlx_complete
2984 * A CTLX has completed. It may have been successful, it may not
2985 * have been. At this point, the CTLX should be quiescent. The URBs
2986 * aren't active and the timers should have been stopped.
2988 * The CTLX is migrated to the "completing" queue, and the completing
2989 * tasklet is scheduled.
2991 * Arguments:
2992 * hw ptr to a hfa384x_t structure
2993 * ctlx ptr to a ctlx structure
2995 * Returns:
2996 * nothing
2998 * Side effects:
3000 * Call context:
3001 * Either, assume interrupt
3002 ----------------------------------------------------------------*/
3003 static void unlocked_usbctlx_complete(hfa384x_t * hw, hfa384x_usbctlx_t * ctlx)
3005 /* Timers have been stopped, and ctlx should be in
3006 * a terminal state. Retire it from the "active"
3007 * queue.
3009 list_move_tail(&ctlx->list, &hw->ctlxq.completing);
3010 tasklet_schedule(&hw->completion_bh);
3012 switch (ctlx->state) {
3013 case CTLX_COMPLETE:
3014 case CTLX_REQ_FAILED:
3015 /* This are the correct terminating states. */
3016 break;
3018 default:
3019 printk(KERN_ERR "CTLX[%d] not in a terminating state(%s)\n",
3020 le16_to_cpu(ctlx->outbuf.type), ctlxstr(ctlx->state));
3021 break;
3022 } /* switch */
3025 /*----------------------------------------------------------------
3026 * hfa384x_usbctlxq_run
3028 * Checks to see if the head item is running. If not, starts it.
3030 * Arguments:
3031 * hw ptr to hfa384x_t
3033 * Returns:
3034 * nothing
3036 * Side effects:
3038 * Call context:
3039 * any
3040 ----------------------------------------------------------------*/
3041 static void hfa384x_usbctlxq_run(hfa384x_t * hw)
3043 unsigned long flags;
3045 /* acquire lock */
3046 spin_lock_irqsave(&hw->ctlxq.lock, flags);
3048 /* Only one active CTLX at any one time, because there's no
3049 * other (reliable) way to match the response URB to the
3050 * correct CTLX.
3052 * Don't touch any of these CTLXs if the hardware
3053 * has been removed or the USB subsystem is stalled.
3055 if (!list_empty(&hw->ctlxq.active) ||
3056 test_bit(WORK_TX_HALT, &hw->usb_flags) || hw->wlandev->hwremoved)
3057 goto unlock;
3059 while (!list_empty(&hw->ctlxq.pending)) {
3060 hfa384x_usbctlx_t *head;
3061 int result;
3063 /* This is the first pending command */
3064 head = list_entry(hw->ctlxq.pending.next,
3065 hfa384x_usbctlx_t, list);
3067 /* We need to split this off to avoid a race condition */
3068 list_move_tail(&head->list, &hw->ctlxq.active);
3070 /* Fill the out packet */
3071 usb_fill_bulk_urb(&(hw->ctlx_urb), hw->usb,
3072 hw->endp_out,
3073 &(head->outbuf), ROUNDUP64(head->outbufsize),
3074 hfa384x_ctlxout_callback, hw);
3075 hw->ctlx_urb.transfer_flags |= USB_QUEUE_BULK;
3077 /* Now submit the URB and update the CTLX's state
3079 if ((result = SUBMIT_URB(&hw->ctlx_urb, GFP_ATOMIC)) == 0) {
3080 /* This CTLX is now running on the active queue */
3081 head->state = CTLX_REQ_SUBMITTED;
3083 /* Start the OUT wait timer */
3084 hw->req_timer_done = 0;
3085 hw->reqtimer.expires = jiffies + HZ;
3086 add_timer(&hw->reqtimer);
3088 /* Start the IN wait timer */
3089 hw->resp_timer_done = 0;
3090 hw->resptimer.expires = jiffies + 2 * HZ;
3091 add_timer(&hw->resptimer);
3093 break;
3096 if (result == -EPIPE) {
3097 /* The OUT pipe needs resetting, so put
3098 * this CTLX back in the "pending" queue
3099 * and schedule a reset ...
3101 printk(KERN_WARNING
3102 "%s tx pipe stalled: requesting reset\n",
3103 hw->wlandev->netdev->name);
3104 list_move(&head->list, &hw->ctlxq.pending);
3105 set_bit(WORK_TX_HALT, &hw->usb_flags);
3106 schedule_work(&hw->usb_work);
3107 break;
3110 if (result == -ESHUTDOWN) {
3111 printk(KERN_WARNING "%s urb shutdown!\n",
3112 hw->wlandev->netdev->name);
3113 break;
3116 printk(KERN_ERR "Failed to submit CTLX[%d]: error=%d\n",
3117 le16_to_cpu(head->outbuf.type), result);
3118 unlocked_usbctlx_complete(hw, head);
3119 } /* while */
3121 unlock:
3122 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3125 /*----------------------------------------------------------------
3126 * hfa384x_usbin_callback
3128 * Callback for URBs on the BULKIN endpoint.
3130 * Arguments:
3131 * urb ptr to the completed urb
3133 * Returns:
3134 * nothing
3136 * Side effects:
3138 * Call context:
3139 * interrupt
3140 ----------------------------------------------------------------*/
3141 static void hfa384x_usbin_callback(struct urb *urb)
3143 wlandevice_t *wlandev = urb->context;
3144 hfa384x_t *hw;
3145 hfa384x_usbin_t *usbin = (hfa384x_usbin_t *) urb->transfer_buffer;
3146 struct sk_buff *skb = NULL;
3147 int result;
3148 int urb_status;
3149 u16 type;
3151 enum USBIN_ACTION {
3152 HANDLE,
3153 RESUBMIT,
3154 ABORT
3155 } action;
3157 if (!wlandev || !wlandev->netdev || wlandev->hwremoved)
3158 goto exit;
3160 hw = wlandev->priv;
3161 if (!hw)
3162 goto exit;
3164 skb = hw->rx_urb_skb;
3165 BUG_ON(!skb || (skb->data != urb->transfer_buffer));
3167 hw->rx_urb_skb = NULL;
3169 /* Check for error conditions within the URB */
3170 switch (urb->status) {
3171 case 0:
3172 action = HANDLE;
3174 /* Check for short packet */
3175 if (urb->actual_length == 0) {
3176 ++(wlandev->linux_stats.rx_errors);
3177 ++(wlandev->linux_stats.rx_length_errors);
3178 action = RESUBMIT;
3180 break;
3182 case -EPIPE:
3183 printk(KERN_WARNING "%s rx pipe stalled: requesting reset\n",
3184 wlandev->netdev->name);
3185 if (!test_and_set_bit(WORK_RX_HALT, &hw->usb_flags))
3186 schedule_work(&hw->usb_work);
3187 ++(wlandev->linux_stats.rx_errors);
3188 action = ABORT;
3189 break;
3191 case -EILSEQ:
3192 case -ETIMEDOUT:
3193 case -EPROTO:
3194 if (!test_and_set_bit(THROTTLE_RX, &hw->usb_flags) &&
3195 !timer_pending(&hw->throttle)) {
3196 mod_timer(&hw->throttle, jiffies + THROTTLE_JIFFIES);
3198 ++(wlandev->linux_stats.rx_errors);
3199 action = ABORT;
3200 break;
3202 case -EOVERFLOW:
3203 ++(wlandev->linux_stats.rx_over_errors);
3204 action = RESUBMIT;
3205 break;
3207 case -ENODEV:
3208 case -ESHUTDOWN:
3209 pr_debug("status=%d, device removed.\n", urb->status);
3210 action = ABORT;
3211 break;
3213 case -ENOENT:
3214 case -ECONNRESET:
3215 pr_debug("status=%d, urb explicitly unlinked.\n", urb->status);
3216 action = ABORT;
3217 break;
3219 default:
3220 pr_debug("urb status=%d, transfer flags=0x%x\n",
3221 urb->status, urb->transfer_flags);
3222 ++(wlandev->linux_stats.rx_errors);
3223 action = RESUBMIT;
3224 break;
3227 urb_status = urb->status;
3229 if (action != ABORT) {
3230 /* Repost the RX URB */
3231 result = submit_rx_urb(hw, GFP_ATOMIC);
3233 if (result != 0) {
3234 printk(KERN_ERR
3235 "Fatal, failed to resubmit rx_urb. error=%d\n",
3236 result);
3240 /* Handle any USB-IN packet */
3241 /* Note: the check of the sw_support field, the type field doesn't
3242 * have bit 12 set like the docs suggest.
3244 type = le16_to_cpu(usbin->type);
3245 if (HFA384x_USB_ISRXFRM(type)) {
3246 if (action == HANDLE) {
3247 if (usbin->txfrm.desc.sw_support == 0x0123) {
3248 hfa384x_usbin_txcompl(wlandev, usbin);
3249 } else {
3250 skb_put(skb, sizeof(*usbin));
3251 hfa384x_usbin_rx(wlandev, skb);
3252 skb = NULL;
3255 goto exit;
3257 if (HFA384x_USB_ISTXFRM(type)) {
3258 if (action == HANDLE)
3259 hfa384x_usbin_txcompl(wlandev, usbin);
3260 goto exit;
3262 switch (type) {
3263 case HFA384x_USB_INFOFRM:
3264 if (action == ABORT)
3265 goto exit;
3266 if (action == HANDLE)
3267 hfa384x_usbin_info(wlandev, usbin);
3268 break;
3270 case HFA384x_USB_CMDRESP:
3271 case HFA384x_USB_WRIDRESP:
3272 case HFA384x_USB_RRIDRESP:
3273 case HFA384x_USB_WMEMRESP:
3274 case HFA384x_USB_RMEMRESP:
3275 /* ALWAYS, ALWAYS, ALWAYS handle this CTLX!!!! */
3276 hfa384x_usbin_ctlx(hw, usbin, urb_status);
3277 break;
3279 case HFA384x_USB_BUFAVAIL:
3280 pr_debug("Received BUFAVAIL packet, frmlen=%d\n",
3281 usbin->bufavail.frmlen);
3282 break;
3284 case HFA384x_USB_ERROR:
3285 pr_debug("Received USB_ERROR packet, errortype=%d\n",
3286 usbin->usberror.errortype);
3287 break;
3289 default:
3290 pr_debug("Unrecognized USBIN packet, type=%x, status=%d\n",
3291 usbin->type, urb_status);
3292 break;
3293 } /* switch */
3295 exit:
3297 if (skb)
3298 dev_kfree_skb(skb);
3301 /*----------------------------------------------------------------
3302 * hfa384x_usbin_ctlx
3304 * We've received a URB containing a Prism2 "response" message.
3305 * This message needs to be matched up with a CTLX on the active
3306 * queue and our state updated accordingly.
3308 * Arguments:
3309 * hw ptr to hfa384x_t
3310 * usbin ptr to USB IN packet
3311 * urb_status status of this Bulk-In URB
3313 * Returns:
3314 * nothing
3316 * Side effects:
3318 * Call context:
3319 * interrupt
3320 ----------------------------------------------------------------*/
3321 static void hfa384x_usbin_ctlx(hfa384x_t * hw, hfa384x_usbin_t * usbin,
3322 int urb_status)
3324 hfa384x_usbctlx_t *ctlx;
3325 int run_queue = 0;
3326 unsigned long flags;
3328 retry:
3329 spin_lock_irqsave(&hw->ctlxq.lock, flags);
3331 /* There can be only one CTLX on the active queue
3332 * at any one time, and this is the CTLX that the
3333 * timers are waiting for.
3335 if (list_empty(&hw->ctlxq.active))
3336 goto unlock;
3338 /* Remove the "response timeout". It's possible that
3339 * we are already too late, and that the timeout is
3340 * already running. And that's just too bad for us,
3341 * because we could lose our CTLX from the active
3342 * queue here ...
3344 if (del_timer(&hw->resptimer) == 0) {
3345 if (hw->resp_timer_done == 0) {
3346 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3347 goto retry;
3349 } else {
3350 hw->resp_timer_done = 1;
3353 ctlx = get_active_ctlx(hw);
3355 if (urb_status != 0) {
3357 * Bad CTLX, so get rid of it. But we only
3358 * remove it from the active queue if we're no
3359 * longer expecting the OUT URB to complete.
3361 if (unlocked_usbctlx_cancel_async(hw, ctlx) == 0)
3362 run_queue = 1;
3363 } else {
3364 const u16 intype = (usbin->type & ~cpu_to_le16(0x8000));
3367 * Check that our message is what we're expecting ...
3369 if (ctlx->outbuf.type != intype) {
3370 printk(KERN_WARNING
3371 "Expected IN[%d], received IN[%d] - ignored.\n",
3372 le16_to_cpu(ctlx->outbuf.type),
3373 le16_to_cpu(intype));
3374 goto unlock;
3377 /* This URB has succeeded, so grab the data ... */
3378 memcpy(&ctlx->inbuf, usbin, sizeof(ctlx->inbuf));
3380 switch (ctlx->state) {
3381 case CTLX_REQ_SUBMITTED:
3383 * We have received our response URB before
3384 * our request has been acknowledged. Odd,
3385 * but our OUT URB is still alive...
3387 pr_debug
3388 ("Causality violation: please reboot Universe, or email linux-wlan-devel@lists.linux-wlan.com\n");
3389 ctlx->state = CTLX_RESP_COMPLETE;
3390 break;
3392 case CTLX_REQ_COMPLETE:
3394 * This is the usual path: our request
3395 * has already been acknowledged, and
3396 * now we have received the reply too.
3398 ctlx->state = CTLX_COMPLETE;
3399 unlocked_usbctlx_complete(hw, ctlx);
3400 run_queue = 1;
3401 break;
3403 default:
3405 * Throw this CTLX away ...
3407 printk(KERN_ERR
3408 "Matched IN URB, CTLX[%d] in invalid state(%s)."
3409 " Discarded.\n",
3410 le16_to_cpu(ctlx->outbuf.type),
3411 ctlxstr(ctlx->state));
3412 if (unlocked_usbctlx_cancel_async(hw, ctlx) == 0)
3413 run_queue = 1;
3414 break;
3415 } /* switch */
3418 unlock:
3419 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3421 if (run_queue)
3422 hfa384x_usbctlxq_run(hw);
3425 /*----------------------------------------------------------------
3426 * hfa384x_usbin_txcompl
3428 * At this point we have the results of a previous transmit.
3430 * Arguments:
3431 * wlandev wlan device
3432 * usbin ptr to the usb transfer buffer
3434 * Returns:
3435 * nothing
3437 * Side effects:
3439 * Call context:
3440 * interrupt
3441 ----------------------------------------------------------------*/
3442 static void hfa384x_usbin_txcompl(wlandevice_t * wlandev,
3443 hfa384x_usbin_t * usbin)
3445 u16 status;
3447 status = le16_to_cpu(usbin->type); /* yeah I know it says type... */
3449 /* Was there an error? */
3450 if (HFA384x_TXSTATUS_ISERROR(status))
3451 prism2sta_ev_txexc(wlandev, status);
3452 else
3453 prism2sta_ev_tx(wlandev, status);
3456 /*----------------------------------------------------------------
3457 * hfa384x_usbin_rx
3459 * At this point we have a successful received a rx frame packet.
3461 * Arguments:
3462 * wlandev wlan device
3463 * usbin ptr to the usb transfer buffer
3465 * Returns:
3466 * nothing
3468 * Side effects:
3470 * Call context:
3471 * interrupt
3472 ----------------------------------------------------------------*/
3473 static void hfa384x_usbin_rx(wlandevice_t * wlandev, struct sk_buff *skb)
3475 hfa384x_usbin_t *usbin = (hfa384x_usbin_t *) skb->data;
3476 hfa384x_t *hw = wlandev->priv;
3477 int hdrlen;
3478 p80211_rxmeta_t *rxmeta;
3479 u16 data_len;
3480 u16 fc;
3482 /* Byte order convert once up front. */
3483 usbin->rxfrm.desc.status = le16_to_cpu(usbin->rxfrm.desc.status);
3484 usbin->rxfrm.desc.time = le32_to_cpu(usbin->rxfrm.desc.time);
3486 /* Now handle frame based on port# */
3487 switch (HFA384x_RXSTATUS_MACPORT_GET(usbin->rxfrm.desc.status)) {
3488 case 0:
3489 fc = le16_to_cpu(usbin->rxfrm.desc.frame_control);
3491 /* If exclude and we receive an unencrypted, drop it */
3492 if ((wlandev->hostwep & HOSTWEP_EXCLUDEUNENCRYPTED) &&
3493 !WLAN_GET_FC_ISWEP(fc)) {
3494 goto done;
3497 data_len = le16_to_cpu(usbin->rxfrm.desc.data_len);
3499 /* How much header data do we have? */
3500 hdrlen = p80211_headerlen(fc);
3502 /* Pull off the descriptor */
3503 skb_pull(skb, sizeof(hfa384x_rx_frame_t));
3505 /* Now shunt the header block up against the data block
3506 * with an "overlapping" copy
3508 memmove(skb_push(skb, hdrlen),
3509 &usbin->rxfrm.desc.frame_control, hdrlen);
3511 skb->dev = wlandev->netdev;
3512 skb->dev->last_rx = jiffies;
3514 /* And set the frame length properly */
3515 skb_trim(skb, data_len + hdrlen);
3517 /* The prism2 series does not return the CRC */
3518 memset(skb_put(skb, WLAN_CRC_LEN), 0xff, WLAN_CRC_LEN);
3520 skb_reset_mac_header(skb);
3522 /* Attach the rxmeta, set some stuff */
3523 p80211skb_rxmeta_attach(wlandev, skb);
3524 rxmeta = P80211SKB_RXMETA(skb);
3525 rxmeta->mactime = usbin->rxfrm.desc.time;
3526 rxmeta->rxrate = usbin->rxfrm.desc.rate;
3527 rxmeta->signal = usbin->rxfrm.desc.signal - hw->dbmadjust;
3528 rxmeta->noise = usbin->rxfrm.desc.silence - hw->dbmadjust;
3530 prism2sta_ev_rx(wlandev, skb);
3532 break;
3534 case 7:
3535 if (!HFA384x_RXSTATUS_ISFCSERR(usbin->rxfrm.desc.status)) {
3536 /* Copy to wlansnif skb */
3537 hfa384x_int_rxmonitor(wlandev, &usbin->rxfrm);
3538 dev_kfree_skb(skb);
3539 } else {
3540 pr_debug("Received monitor frame: FCSerr set\n");
3542 break;
3544 default:
3545 printk(KERN_WARNING "Received frame on unsupported port=%d\n",
3546 HFA384x_RXSTATUS_MACPORT_GET(usbin->rxfrm.desc.status));
3547 goto done;
3548 break;
3551 done:
3552 return;
3555 /*----------------------------------------------------------------
3556 * hfa384x_int_rxmonitor
3558 * Helper function for int_rx. Handles monitor frames.
3559 * Note that this function allocates space for the FCS and sets it
3560 * to 0xffffffff. The hfa384x doesn't give us the FCS value but the
3561 * higher layers expect it. 0xffffffff is used as a flag to indicate
3562 * the FCS is bogus.
3564 * Arguments:
3565 * wlandev wlan device structure
3566 * rxfrm rx descriptor read from card in int_rx
3568 * Returns:
3569 * nothing
3571 * Side effects:
3572 * Allocates an skb and passes it up via the PF_PACKET interface.
3573 * Call context:
3574 * interrupt
3575 ----------------------------------------------------------------*/
3576 static void hfa384x_int_rxmonitor(wlandevice_t * wlandev,
3577 hfa384x_usb_rxfrm_t * rxfrm)
3579 hfa384x_rx_frame_t *rxdesc = &(rxfrm->desc);
3580 unsigned int hdrlen = 0;
3581 unsigned int datalen = 0;
3582 unsigned int skblen = 0;
3583 u8 *datap;
3584 u16 fc;
3585 struct sk_buff *skb;
3586 hfa384x_t *hw = wlandev->priv;
3588 /* Don't forget the status, time, and data_len fields are in host order */
3589 /* Figure out how big the frame is */
3590 fc = le16_to_cpu(rxdesc->frame_control);
3591 hdrlen = p80211_headerlen(fc);
3592 datalen = le16_to_cpu(rxdesc->data_len);
3594 /* Allocate an ind message+framesize skb */
3595 skblen = sizeof(p80211_caphdr_t) + hdrlen + datalen + WLAN_CRC_LEN;
3597 /* sanity check the length */
3598 if (skblen >
3599 (sizeof(p80211_caphdr_t) +
3600 WLAN_HDR_A4_LEN + WLAN_DATA_MAXLEN + WLAN_CRC_LEN)) {
3601 pr_debug("overlen frm: len=%zd\n",
3602 skblen - sizeof(p80211_caphdr_t));
3605 if ((skb = dev_alloc_skb(skblen)) == NULL) {
3606 printk(KERN_ERR
3607 "alloc_skb failed trying to allocate %d bytes\n",
3608 skblen);
3609 return;
3612 /* only prepend the prism header if in the right mode */
3613 if ((wlandev->netdev->type == ARPHRD_IEEE80211_PRISM) &&
3614 (hw->sniffhdr != 0)) {
3615 p80211_caphdr_t *caphdr;
3616 /* The NEW header format! */
3617 datap = skb_put(skb, sizeof(p80211_caphdr_t));
3618 caphdr = (p80211_caphdr_t *) datap;
3620 caphdr->version = htonl(P80211CAPTURE_VERSION);
3621 caphdr->length = htonl(sizeof(p80211_caphdr_t));
3622 caphdr->mactime = __cpu_to_be64(rxdesc->time) * 1000;
3623 caphdr->hosttime = __cpu_to_be64(jiffies);
3624 caphdr->phytype = htonl(4); /* dss_dot11_b */
3625 caphdr->channel = htonl(hw->sniff_channel);
3626 caphdr->datarate = htonl(rxdesc->rate);
3627 caphdr->antenna = htonl(0); /* unknown */
3628 caphdr->priority = htonl(0); /* unknown */
3629 caphdr->ssi_type = htonl(3); /* rssi_raw */
3630 caphdr->ssi_signal = htonl(rxdesc->signal);
3631 caphdr->ssi_noise = htonl(rxdesc->silence);
3632 caphdr->preamble = htonl(0); /* unknown */
3633 caphdr->encoding = htonl(1); /* cck */
3636 /* Copy the 802.11 header to the skb (ctl frames may be less than a full header) */
3637 datap = skb_put(skb, hdrlen);
3638 memcpy(datap, &(rxdesc->frame_control), hdrlen);
3640 /* If any, copy the data from the card to the skb */
3641 if (datalen > 0) {
3642 datap = skb_put(skb, datalen);
3643 memcpy(datap, rxfrm->data, datalen);
3645 /* check for unencrypted stuff if WEP bit set. */
3646 if (*(datap - hdrlen + 1) & 0x40) /* wep set */
3647 if ((*(datap) == 0xaa) && (*(datap + 1) == 0xaa))
3648 *(datap - hdrlen + 1) &= 0xbf; // clear wep; it's the 802.2 header!
3651 if (hw->sniff_fcs) {
3652 /* Set the FCS */
3653 datap = skb_put(skb, WLAN_CRC_LEN);
3654 memset(datap, 0xff, WLAN_CRC_LEN);
3657 /* pass it back up */
3658 prism2sta_ev_rx(wlandev, skb);
3660 return;
3663 /*----------------------------------------------------------------
3664 * hfa384x_usbin_info
3666 * At this point we have a successful received a Prism2 info frame.
3668 * Arguments:
3669 * wlandev wlan device
3670 * usbin ptr to the usb transfer buffer
3672 * Returns:
3673 * nothing
3675 * Side effects:
3677 * Call context:
3678 * interrupt
3679 ----------------------------------------------------------------*/
3680 static void hfa384x_usbin_info(wlandevice_t * wlandev, hfa384x_usbin_t * usbin)
3682 usbin->infofrm.info.framelen =
3683 le16_to_cpu(usbin->infofrm.info.framelen);
3684 prism2sta_ev_info(wlandev, &usbin->infofrm.info);
3687 /*----------------------------------------------------------------
3688 * hfa384x_usbout_callback
3690 * Callback for URBs on the BULKOUT endpoint.
3692 * Arguments:
3693 * urb ptr to the completed urb
3695 * Returns:
3696 * nothing
3698 * Side effects:
3700 * Call context:
3701 * interrupt
3702 ----------------------------------------------------------------*/
3703 static void hfa384x_usbout_callback(struct urb *urb)
3705 wlandevice_t *wlandev = urb->context;
3706 hfa384x_usbout_t *usbout = urb->transfer_buffer;
3708 #ifdef DEBUG_USB
3709 dbprint_urb(urb);
3710 #endif
3712 if (wlandev && wlandev->netdev) {
3714 switch (urb->status) {
3715 case 0:
3716 hfa384x_usbout_tx(wlandev, usbout);
3717 break;
3719 case -EPIPE:
3721 hfa384x_t *hw = wlandev->priv;
3722 printk(KERN_WARNING
3723 "%s tx pipe stalled: requesting reset\n",
3724 wlandev->netdev->name);
3725 if (!test_and_set_bit
3726 (WORK_TX_HALT, &hw->usb_flags))
3727 schedule_work(&hw->usb_work);
3728 ++(wlandev->linux_stats.tx_errors);
3729 break;
3732 case -EPROTO:
3733 case -ETIMEDOUT:
3734 case -EILSEQ:
3736 hfa384x_t *hw = wlandev->priv;
3738 if (!test_and_set_bit
3739 (THROTTLE_TX, &hw->usb_flags)
3740 && !timer_pending(&hw->throttle)) {
3741 mod_timer(&hw->throttle,
3742 jiffies + THROTTLE_JIFFIES);
3744 ++(wlandev->linux_stats.tx_errors);
3745 netif_stop_queue(wlandev->netdev);
3746 break;
3749 case -ENOENT:
3750 case -ESHUTDOWN:
3751 /* Ignorable errors */
3752 break;
3754 default:
3755 printk(KERN_INFO "unknown urb->status=%d\n",
3756 urb->status);
3757 ++(wlandev->linux_stats.tx_errors);
3758 break;
3759 } /* switch */
3763 /*----------------------------------------------------------------
3764 * hfa384x_ctlxout_callback
3766 * Callback for control data on the BULKOUT endpoint.
3768 * Arguments:
3769 * urb ptr to the completed urb
3771 * Returns:
3772 * nothing
3774 * Side effects:
3776 * Call context:
3777 * interrupt
3778 ----------------------------------------------------------------*/
3779 static void hfa384x_ctlxout_callback(struct urb *urb)
3781 hfa384x_t *hw = urb->context;
3782 int delete_resptimer = 0;
3783 int timer_ok = 1;
3784 int run_queue = 0;
3785 hfa384x_usbctlx_t *ctlx;
3786 unsigned long flags;
3788 pr_debug("urb->status=%d\n", urb->status);
3789 #ifdef DEBUG_USB
3790 dbprint_urb(urb);
3791 #endif
3792 if ((urb->status == -ESHUTDOWN) ||
3793 (urb->status == -ENODEV) || (hw == NULL))
3794 goto done;
3796 retry:
3797 spin_lock_irqsave(&hw->ctlxq.lock, flags);
3800 * Only one CTLX at a time on the "active" list, and
3801 * none at all if we are unplugged. However, we can
3802 * rely on the disconnect function to clean everything
3803 * up if someone unplugged the adapter.
3805 if (list_empty(&hw->ctlxq.active)) {
3806 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3807 goto done;
3811 * Having something on the "active" queue means
3812 * that we have timers to worry about ...
3814 if (del_timer(&hw->reqtimer) == 0) {
3815 if (hw->req_timer_done == 0) {
3817 * This timer was actually running while we
3818 * were trying to delete it. Let it terminate
3819 * gracefully instead.
3821 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3822 goto retry;
3824 } else {
3825 hw->req_timer_done = 1;
3828 ctlx = get_active_ctlx(hw);
3830 if (urb->status == 0) {
3831 /* Request portion of a CTLX is successful */
3832 switch (ctlx->state) {
3833 case CTLX_REQ_SUBMITTED:
3834 /* This OUT-ACK received before IN */
3835 ctlx->state = CTLX_REQ_COMPLETE;
3836 break;
3838 case CTLX_RESP_COMPLETE:
3839 /* IN already received before this OUT-ACK,
3840 * so this command must now be complete.
3842 ctlx->state = CTLX_COMPLETE;
3843 unlocked_usbctlx_complete(hw, ctlx);
3844 run_queue = 1;
3845 break;
3847 default:
3848 /* This is NOT a valid CTLX "success" state! */
3849 printk(KERN_ERR
3850 "Illegal CTLX[%d] success state(%s, %d) in OUT URB\n",
3851 le16_to_cpu(ctlx->outbuf.type),
3852 ctlxstr(ctlx->state), urb->status);
3853 break;
3854 } /* switch */
3855 } else {
3856 /* If the pipe has stalled then we need to reset it */
3857 if ((urb->status == -EPIPE) &&
3858 !test_and_set_bit(WORK_TX_HALT, &hw->usb_flags)) {
3859 printk(KERN_WARNING
3860 "%s tx pipe stalled: requesting reset\n",
3861 hw->wlandev->netdev->name);
3862 schedule_work(&hw->usb_work);
3865 /* If someone cancels the OUT URB then its status
3866 * should be either -ECONNRESET or -ENOENT.
3868 ctlx->state = CTLX_REQ_FAILED;
3869 unlocked_usbctlx_complete(hw, ctlx);
3870 delete_resptimer = 1;
3871 run_queue = 1;
3874 delresp:
3875 if (delete_resptimer) {
3876 if ((timer_ok = del_timer(&hw->resptimer)) != 0) {
3877 hw->resp_timer_done = 1;
3881 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3883 if (!timer_ok && (hw->resp_timer_done == 0)) {
3884 spin_lock_irqsave(&hw->ctlxq.lock, flags);
3885 goto delresp;
3888 if (run_queue)
3889 hfa384x_usbctlxq_run(hw);
3891 done:
3895 /*----------------------------------------------------------------
3896 * hfa384x_usbctlx_reqtimerfn
3898 * Timer response function for CTLX request timeouts. If this
3899 * function is called, it means that the callback for the OUT
3900 * URB containing a Prism2.x XXX_Request was never called.
3902 * Arguments:
3903 * data a ptr to the hfa384x_t
3905 * Returns:
3906 * nothing
3908 * Side effects:
3910 * Call context:
3911 * interrupt
3912 ----------------------------------------------------------------*/
3913 static void hfa384x_usbctlx_reqtimerfn(unsigned long data)
3915 hfa384x_t *hw = (hfa384x_t *) data;
3916 unsigned long flags;
3918 spin_lock_irqsave(&hw->ctlxq.lock, flags);
3920 hw->req_timer_done = 1;
3922 /* Removing the hardware automatically empties
3923 * the active list ...
3925 if (!list_empty(&hw->ctlxq.active)) {
3927 * We must ensure that our URB is removed from
3928 * the system, if it hasn't already expired.
3930 hw->ctlx_urb.transfer_flags |= URB_ASYNC_UNLINK;
3931 if (usb_unlink_urb(&hw->ctlx_urb) == -EINPROGRESS) {
3932 hfa384x_usbctlx_t *ctlx = get_active_ctlx(hw);
3934 ctlx->state = CTLX_REQ_FAILED;
3936 /* This URB was active, but has now been
3937 * cancelled. It will now have a status of
3938 * -ECONNRESET in the callback function.
3940 * We are cancelling this CTLX, so we're
3941 * not going to need to wait for a response.
3942 * The URB's callback function will check
3943 * that this timer is truly dead.
3945 if (del_timer(&hw->resptimer) != 0)
3946 hw->resp_timer_done = 1;
3950 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3953 /*----------------------------------------------------------------
3954 * hfa384x_usbctlx_resptimerfn
3956 * Timer response function for CTLX response timeouts. If this
3957 * function is called, it means that the callback for the IN
3958 * URB containing a Prism2.x XXX_Response was never called.
3960 * Arguments:
3961 * data a ptr to the hfa384x_t
3963 * Returns:
3964 * nothing
3966 * Side effects:
3968 * Call context:
3969 * interrupt
3970 ----------------------------------------------------------------*/
3971 static void hfa384x_usbctlx_resptimerfn(unsigned long data)
3973 hfa384x_t *hw = (hfa384x_t *) data;
3974 unsigned long flags;
3976 spin_lock_irqsave(&hw->ctlxq.lock, flags);
3978 hw->resp_timer_done = 1;
3980 /* The active list will be empty if the
3981 * adapter has been unplugged ...
3983 if (!list_empty(&hw->ctlxq.active)) {
3984 hfa384x_usbctlx_t *ctlx = get_active_ctlx(hw);
3986 if (unlocked_usbctlx_cancel_async(hw, ctlx) == 0) {
3987 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3988 hfa384x_usbctlxq_run(hw);
3989 goto done;
3993 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3995 done:
4000 /*----------------------------------------------------------------
4001 * hfa384x_usb_throttlefn
4004 * Arguments:
4005 * data ptr to hw
4007 * Returns:
4008 * Nothing
4010 * Side effects:
4012 * Call context:
4013 * Interrupt
4014 ----------------------------------------------------------------*/
4015 static void hfa384x_usb_throttlefn(unsigned long data)
4017 hfa384x_t *hw = (hfa384x_t *) data;
4018 unsigned long flags;
4020 spin_lock_irqsave(&hw->ctlxq.lock, flags);
4023 * We need to check BOTH the RX and the TX throttle controls,
4024 * so we use the bitwise OR instead of the logical OR.
4026 pr_debug("flags=0x%lx\n", hw->usb_flags);
4027 if (!hw->wlandev->hwremoved &&
4028 ((test_and_clear_bit(THROTTLE_RX, &hw->usb_flags) &&
4029 !test_and_set_bit(WORK_RX_RESUME, &hw->usb_flags))
4031 (test_and_clear_bit(THROTTLE_TX, &hw->usb_flags) &&
4032 !test_and_set_bit(WORK_TX_RESUME, &hw->usb_flags))
4033 )) {
4034 schedule_work(&hw->usb_work);
4037 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
4040 /*----------------------------------------------------------------
4041 * hfa384x_usbctlx_submit
4043 * Called from the doxxx functions to submit a CTLX to the queue
4045 * Arguments:
4046 * hw ptr to the hw struct
4047 * ctlx ctlx structure to enqueue
4049 * Returns:
4050 * -ENODEV if the adapter is unplugged
4053 * Side effects:
4055 * Call context:
4056 * process or interrupt
4057 ----------------------------------------------------------------*/
4058 static int hfa384x_usbctlx_submit(hfa384x_t * hw, hfa384x_usbctlx_t * ctlx)
4060 unsigned long flags;
4061 int ret;
4063 spin_lock_irqsave(&hw->ctlxq.lock, flags);
4065 if (hw->wlandev->hwremoved) {
4066 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
4067 ret = -ENODEV;
4068 } else {
4069 ctlx->state = CTLX_PENDING;
4070 list_add_tail(&ctlx->list, &hw->ctlxq.pending);
4072 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
4073 hfa384x_usbctlxq_run(hw);
4074 ret = 0;
4077 return ret;
4080 /*----------------------------------------------------------------
4081 * hfa384x_usbout_tx
4083 * At this point we have finished a send of a frame. Mark the URB
4084 * as available and call ev_alloc to notify higher layers we're
4085 * ready for more.
4087 * Arguments:
4088 * wlandev wlan device
4089 * usbout ptr to the usb transfer buffer
4091 * Returns:
4092 * nothing
4094 * Side effects:
4096 * Call context:
4097 * interrupt
4098 ----------------------------------------------------------------*/
4099 static void hfa384x_usbout_tx(wlandevice_t * wlandev, hfa384x_usbout_t * usbout)
4101 prism2sta_ev_alloc(wlandev);
4104 /*----------------------------------------------------------------
4105 * hfa384x_isgood_pdrcore
4107 * Quick check of PDR codes.
4109 * Arguments:
4110 * pdrcode PDR code number (host order)
4112 * Returns:
4113 * zero not good.
4114 * one is good.
4116 * Side effects:
4118 * Call context:
4119 ----------------------------------------------------------------*/
4120 static int hfa384x_isgood_pdrcode(u16 pdrcode)
4122 switch (pdrcode) {
4123 case HFA384x_PDR_END_OF_PDA:
4124 case HFA384x_PDR_PCB_PARTNUM:
4125 case HFA384x_PDR_PDAVER:
4126 case HFA384x_PDR_NIC_SERIAL:
4127 case HFA384x_PDR_MKK_MEASUREMENTS:
4128 case HFA384x_PDR_NIC_RAMSIZE:
4129 case HFA384x_PDR_MFISUPRANGE:
4130 case HFA384x_PDR_CFISUPRANGE:
4131 case HFA384x_PDR_NICID:
4132 case HFA384x_PDR_MAC_ADDRESS:
4133 case HFA384x_PDR_REGDOMAIN:
4134 case HFA384x_PDR_ALLOWED_CHANNEL:
4135 case HFA384x_PDR_DEFAULT_CHANNEL:
4136 case HFA384x_PDR_TEMPTYPE:
4137 case HFA384x_PDR_IFR_SETTING:
4138 case HFA384x_PDR_RFR_SETTING:
4139 case HFA384x_PDR_HFA3861_BASELINE:
4140 case HFA384x_PDR_HFA3861_SHADOW:
4141 case HFA384x_PDR_HFA3861_IFRF:
4142 case HFA384x_PDR_HFA3861_CHCALSP:
4143 case HFA384x_PDR_HFA3861_CHCALI:
4144 case HFA384x_PDR_3842_NIC_CONFIG:
4145 case HFA384x_PDR_USB_ID:
4146 case HFA384x_PDR_PCI_ID:
4147 case HFA384x_PDR_PCI_IFCONF:
4148 case HFA384x_PDR_PCI_PMCONF:
4149 case HFA384x_PDR_RFENRGY:
4150 case HFA384x_PDR_HFA3861_MANF_TESTSP:
4151 case HFA384x_PDR_HFA3861_MANF_TESTI:
4152 /* code is OK */
4153 return 1;
4154 break;
4155 default:
4156 if (pdrcode < 0x1000) {
4157 /* code is OK, but we don't know exactly what it is */
4158 pr_debug("Encountered unknown PDR#=0x%04x, "
4159 "assuming it's ok.\n", pdrcode);
4160 return 1;
4161 } else {
4162 /* bad code */
4163 pr_debug("Encountered unknown PDR#=0x%04x, "
4164 "(>=0x1000), assuming it's bad.\n", pdrcode);
4165 return 0;
4167 break;
4169 return 0; /* avoid compiler warnings */