staging: wlan-ng: replace init_timer by setup_timer
[linux-2.6/btrfs-unstable.git] / drivers / staging / wlan-ng / hfa384x_usb.c
blobc85b1b55fdb33a9f6b2d8b8272a3d735836ee280
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 invocations 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 <linux/io.h>
122 #include <linux/delay.h>
123 #include <asm/byteorder.h>
124 #include <linux/bitops.h>
125 #include <linux/list.h>
126 #include <linux/usb.h>
127 #include <linux/byteorder/generic.h>
129 #define SUBMIT_URB(u, f) usb_submit_urb(u, f)
131 #include "p80211types.h"
132 #include "p80211hdr.h"
133 #include "p80211mgmt.h"
134 #include "p80211conv.h"
135 #include "p80211msg.h"
136 #include "p80211netdev.h"
137 #include "p80211req.h"
138 #include "p80211metadef.h"
139 #include "p80211metastruct.h"
140 #include "hfa384x.h"
141 #include "prism2mgmt.h"
143 enum cmd_mode {
144 DOWAIT = 0,
145 DOASYNC
148 #define THROTTLE_JIFFIES (HZ/8)
149 #define URB_ASYNC_UNLINK 0
150 #define USB_QUEUE_BULK 0
152 #define ROUNDUP64(a) (((a)+63)&~63)
154 #ifdef DEBUG_USB
155 static void dbprint_urb(struct urb *urb);
156 #endif
158 static void
159 hfa384x_int_rxmonitor(wlandevice_t *wlandev, hfa384x_usb_rxfrm_t *rxfrm);
161 static void hfa384x_usb_defer(struct work_struct *data);
163 static int submit_rx_urb(hfa384x_t *hw, gfp_t flags);
165 static int submit_tx_urb(hfa384x_t *hw, struct urb *tx_urb, gfp_t flags);
167 /*---------------------------------------------------*/
168 /* Callbacks */
169 static void hfa384x_usbout_callback(struct urb *urb);
170 static void hfa384x_ctlxout_callback(struct urb *urb);
171 static void hfa384x_usbin_callback(struct urb *urb);
173 static void
174 hfa384x_usbin_txcompl(wlandevice_t *wlandev, hfa384x_usbin_t *usbin);
176 static void hfa384x_usbin_rx(wlandevice_t *wlandev, struct sk_buff *skb);
178 static void hfa384x_usbin_info(wlandevice_t *wlandev, hfa384x_usbin_t *usbin);
180 static void
181 hfa384x_usbout_tx(wlandevice_t *wlandev, hfa384x_usbout_t *usbout);
183 static void hfa384x_usbin_ctlx(hfa384x_t *hw, hfa384x_usbin_t *usbin,
184 int urb_status);
186 /*---------------------------------------------------*/
187 /* Functions to support the prism2 usb command queue */
189 static void hfa384x_usbctlxq_run(hfa384x_t *hw);
191 static void hfa384x_usbctlx_reqtimerfn(unsigned long data);
193 static void hfa384x_usbctlx_resptimerfn(unsigned long data);
195 static void hfa384x_usb_throttlefn(unsigned long data);
197 static void hfa384x_usbctlx_completion_task(unsigned long data);
199 static void hfa384x_usbctlx_reaper_task(unsigned long data);
201 static int hfa384x_usbctlx_submit(hfa384x_t *hw, hfa384x_usbctlx_t *ctlx);
203 static void unlocked_usbctlx_complete(hfa384x_t *hw, hfa384x_usbctlx_t *ctlx);
205 struct usbctlx_completor {
206 int (*complete)(struct usbctlx_completor *);
209 static int
210 hfa384x_usbctlx_complete_sync(hfa384x_t *hw,
211 hfa384x_usbctlx_t *ctlx,
212 struct usbctlx_completor *completor);
214 static int
215 unlocked_usbctlx_cancel_async(hfa384x_t *hw, hfa384x_usbctlx_t *ctlx);
217 static void hfa384x_cb_status(hfa384x_t *hw, const hfa384x_usbctlx_t *ctlx);
219 static void hfa384x_cb_rrid(hfa384x_t *hw, const hfa384x_usbctlx_t *ctlx);
221 static int
222 usbctlx_get_status(const hfa384x_usb_cmdresp_t *cmdresp,
223 hfa384x_cmdresult_t *result);
225 static void
226 usbctlx_get_rridresult(const hfa384x_usb_rridresp_t *rridresp,
227 hfa384x_rridresult_t *result);
229 /*---------------------------------------------------*/
230 /* Low level req/resp CTLX formatters and submitters */
231 static int
232 hfa384x_docmd(hfa384x_t *hw,
233 enum cmd_mode mode,
234 hfa384x_metacmd_t *cmd,
235 ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data);
237 static int
238 hfa384x_dorrid(hfa384x_t *hw,
239 enum cmd_mode mode,
240 u16 rid,
241 void *riddata,
242 unsigned int riddatalen,
243 ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data);
245 static int
246 hfa384x_dowrid(hfa384x_t *hw,
247 enum cmd_mode mode,
248 u16 rid,
249 void *riddata,
250 unsigned int riddatalen,
251 ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data);
253 static int
254 hfa384x_dormem(hfa384x_t *hw,
255 enum cmd_mode mode,
256 u16 page,
257 u16 offset,
258 void *data,
259 unsigned int len,
260 ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data);
262 static int
263 hfa384x_dowmem(hfa384x_t *hw,
264 enum cmd_mode mode,
265 u16 page,
266 u16 offset,
267 void *data,
268 unsigned int len,
269 ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data);
271 static int hfa384x_isgood_pdrcode(u16 pdrcode);
273 static inline const char *ctlxstr(CTLX_STATE s)
275 static const char * const ctlx_str[] = {
276 "Initial state",
277 "Complete",
278 "Request failed",
279 "Request pending",
280 "Request packet submitted",
281 "Request packet completed",
282 "Response packet completed"
285 return ctlx_str[s];
288 static inline hfa384x_usbctlx_t *get_active_ctlx(hfa384x_t *hw)
290 return list_entry(hw->ctlxq.active.next, hfa384x_usbctlx_t, list);
293 #ifdef DEBUG_USB
294 void dbprint_urb(struct urb *urb)
296 pr_debug("urb->pipe=0x%08x\n", urb->pipe);
297 pr_debug("urb->status=0x%08x\n", urb->status);
298 pr_debug("urb->transfer_flags=0x%08x\n", urb->transfer_flags);
299 pr_debug("urb->transfer_buffer=0x%08x\n",
300 (unsigned int)urb->transfer_buffer);
301 pr_debug("urb->transfer_buffer_length=0x%08x\n",
302 urb->transfer_buffer_length);
303 pr_debug("urb->actual_length=0x%08x\n", urb->actual_length);
304 pr_debug("urb->bandwidth=0x%08x\n", urb->bandwidth);
305 pr_debug("urb->setup_packet(ctl)=0x%08x\n",
306 (unsigned int)urb->setup_packet);
307 pr_debug("urb->start_frame(iso/irq)=0x%08x\n", urb->start_frame);
308 pr_debug("urb->interval(irq)=0x%08x\n", urb->interval);
309 pr_debug("urb->error_count(iso)=0x%08x\n", urb->error_count);
310 pr_debug("urb->timeout=0x%08x\n", urb->timeout);
311 pr_debug("urb->context=0x%08x\n", (unsigned int)urb->context);
312 pr_debug("urb->complete=0x%08x\n", (unsigned int)urb->complete);
314 #endif
316 /*----------------------------------------------------------------
317 * submit_rx_urb
319 * Listen for input data on the BULK-IN pipe. If the pipe has
320 * stalled then schedule it to be reset.
322 * Arguments:
323 * hw device struct
324 * memflags memory allocation flags
326 * Returns:
327 * error code from submission
329 * Call context:
330 * Any
331 ----------------------------------------------------------------*/
332 static int submit_rx_urb(hfa384x_t *hw, gfp_t memflags)
334 struct sk_buff *skb;
335 int result;
337 skb = dev_alloc_skb(sizeof(hfa384x_usbin_t));
338 if (skb == NULL) {
339 result = -ENOMEM;
340 goto done;
343 /* Post the IN urb */
344 usb_fill_bulk_urb(&hw->rx_urb, hw->usb,
345 hw->endp_in,
346 skb->data, sizeof(hfa384x_usbin_t),
347 hfa384x_usbin_callback, hw->wlandev);
349 hw->rx_urb_skb = skb;
351 result = -ENOLINK;
352 if (!hw->wlandev->hwremoved &&
353 !test_bit(WORK_RX_HALT, &hw->usb_flags)) {
354 result = SUBMIT_URB(&hw->rx_urb, memflags);
356 /* Check whether we need to reset the RX pipe */
357 if (result == -EPIPE) {
358 netdev_warn(hw->wlandev->netdev,
359 "%s rx pipe stalled: requesting reset\n",
360 hw->wlandev->netdev->name);
361 if (!test_and_set_bit(WORK_RX_HALT, &hw->usb_flags))
362 schedule_work(&hw->usb_work);
366 /* Don't leak memory if anything should go wrong */
367 if (result != 0) {
368 dev_kfree_skb(skb);
369 hw->rx_urb_skb = NULL;
372 done:
373 return result;
376 /*----------------------------------------------------------------
377 * submit_tx_urb
379 * Prepares and submits the URB of transmitted data. If the
380 * submission fails then it will schedule the output pipe to
381 * be reset.
383 * Arguments:
384 * hw device struct
385 * tx_urb URB of data for transmission
386 * memflags memory allocation flags
388 * Returns:
389 * error code from submission
391 * Call context:
392 * Any
393 ----------------------------------------------------------------*/
394 static int submit_tx_urb(hfa384x_t *hw, struct urb *tx_urb, gfp_t memflags)
396 struct net_device *netdev = hw->wlandev->netdev;
397 int result;
399 result = -ENOLINK;
400 if (netif_running(netdev)) {
401 if (!hw->wlandev->hwremoved &&
402 !test_bit(WORK_TX_HALT, &hw->usb_flags)) {
403 result = SUBMIT_URB(tx_urb, memflags);
405 /* Test whether we need to reset the TX pipe */
406 if (result == -EPIPE) {
407 netdev_warn(hw->wlandev->netdev,
408 "%s tx pipe stalled: requesting reset\n",
409 netdev->name);
410 set_bit(WORK_TX_HALT, &hw->usb_flags);
411 schedule_work(&hw->usb_work);
412 } else if (result == 0) {
413 netif_stop_queue(netdev);
418 return result;
421 /*----------------------------------------------------------------
422 * hfa394x_usb_defer
424 * There are some things that the USB stack cannot do while
425 * in interrupt context, so we arrange this function to run
426 * in process context.
428 * Arguments:
429 * hw device structure
431 * Returns:
432 * nothing
434 * Call context:
435 * process (by design)
436 ----------------------------------------------------------------*/
437 static void hfa384x_usb_defer(struct work_struct *data)
439 hfa384x_t *hw = container_of(data, struct hfa384x, usb_work);
440 struct net_device *netdev = hw->wlandev->netdev;
442 /* Don't bother trying to reset anything if the plug
443 * has been pulled ...
445 if (hw->wlandev->hwremoved)
446 return;
448 /* Reception has stopped: try to reset the input pipe */
449 if (test_bit(WORK_RX_HALT, &hw->usb_flags)) {
450 int ret;
452 usb_kill_urb(&hw->rx_urb); /* Cannot be holding spinlock! */
454 ret = usb_clear_halt(hw->usb, hw->endp_in);
455 if (ret != 0) {
456 netdev_err(hw->wlandev->netdev,
457 "Failed to clear rx pipe for %s: err=%d\n",
458 netdev->name, ret);
459 } else {
460 netdev_info(hw->wlandev->netdev, "%s rx pipe reset complete.\n",
461 netdev->name);
462 clear_bit(WORK_RX_HALT, &hw->usb_flags);
463 set_bit(WORK_RX_RESUME, &hw->usb_flags);
467 /* Resume receiving data back from the device. */
468 if (test_bit(WORK_RX_RESUME, &hw->usb_flags)) {
469 int ret;
471 ret = submit_rx_urb(hw, GFP_KERNEL);
472 if (ret != 0) {
473 netdev_err(hw->wlandev->netdev,
474 "Failed to resume %s rx pipe.\n",
475 netdev->name);
476 } else {
477 clear_bit(WORK_RX_RESUME, &hw->usb_flags);
481 /* Transmission has stopped: try to reset the output pipe */
482 if (test_bit(WORK_TX_HALT, &hw->usb_flags)) {
483 int ret;
485 usb_kill_urb(&hw->tx_urb);
486 ret = usb_clear_halt(hw->usb, hw->endp_out);
487 if (ret != 0) {
488 netdev_err(hw->wlandev->netdev,
489 "Failed to clear tx pipe for %s: err=%d\n",
490 netdev->name, ret);
491 } else {
492 netdev_info(hw->wlandev->netdev, "%s tx pipe reset complete.\n",
493 netdev->name);
494 clear_bit(WORK_TX_HALT, &hw->usb_flags);
495 set_bit(WORK_TX_RESUME, &hw->usb_flags);
497 /* Stopping the BULK-OUT pipe also blocked
498 * us from sending any more CTLX URBs, so
499 * we need to re-run our queue ...
501 hfa384x_usbctlxq_run(hw);
505 /* Resume transmitting. */
506 if (test_and_clear_bit(WORK_TX_RESUME, &hw->usb_flags))
507 netif_wake_queue(hw->wlandev->netdev);
510 /*----------------------------------------------------------------
511 * hfa384x_create
513 * Sets up the hfa384x_t data structure for use. Note this
514 * does _not_ initialize the actual hardware, just the data structures
515 * we use to keep track of its state.
517 * Arguments:
518 * hw device structure
519 * irq device irq number
520 * iobase i/o base address for register access
521 * membase memory base address for register access
523 * Returns:
524 * nothing
526 * Side effects:
528 * Call context:
529 * process
530 ----------------------------------------------------------------*/
531 void hfa384x_create(hfa384x_t *hw, struct usb_device *usb)
533 memset(hw, 0, sizeof(hfa384x_t));
534 hw->usb = usb;
536 /* set up the endpoints */
537 hw->endp_in = usb_rcvbulkpipe(usb, 1);
538 hw->endp_out = usb_sndbulkpipe(usb, 2);
540 /* Set up the waitq */
541 init_waitqueue_head(&hw->cmdq);
543 /* Initialize the command queue */
544 spin_lock_init(&hw->ctlxq.lock);
545 INIT_LIST_HEAD(&hw->ctlxq.pending);
546 INIT_LIST_HEAD(&hw->ctlxq.active);
547 INIT_LIST_HEAD(&hw->ctlxq.completing);
548 INIT_LIST_HEAD(&hw->ctlxq.reapable);
550 /* Initialize the authentication queue */
551 skb_queue_head_init(&hw->authq);
553 tasklet_init(&hw->reaper_bh,
554 hfa384x_usbctlx_reaper_task, (unsigned long)hw);
555 tasklet_init(&hw->completion_bh,
556 hfa384x_usbctlx_completion_task, (unsigned long)hw);
557 INIT_WORK(&hw->link_bh, prism2sta_processing_defer);
558 INIT_WORK(&hw->usb_work, hfa384x_usb_defer);
560 setup_timer(&hw->throttle, hfa384x_usb_throttlefn, (unsigned long)hw);
562 setup_timer(&hw->resptimer, hfa384x_usbctlx_resptimerfn,
563 (unsigned long)hw);
565 setup_timer(&hw->reqtimer, hfa384x_usbctlx_reqtimerfn,
566 (unsigned long)hw);
568 usb_init_urb(&hw->rx_urb);
569 usb_init_urb(&hw->tx_urb);
570 usb_init_urb(&hw->ctlx_urb);
572 hw->link_status = HFA384x_LINK_NOTCONNECTED;
573 hw->state = HFA384x_STATE_INIT;
575 INIT_WORK(&hw->commsqual_bh, prism2sta_commsqual_defer);
576 setup_timer(&hw->commsqual_timer, prism2sta_commsqual_timer,
577 (unsigned long)hw);
580 /*----------------------------------------------------------------
581 * hfa384x_destroy
583 * Partner to hfa384x_create(). This function cleans up the hw
584 * structure so that it can be freed by the caller using a simple
585 * kfree. Currently, this function is just a placeholder. If, at some
586 * point in the future, an hw in the 'shutdown' state requires a 'deep'
587 * kfree, this is where it should be done. Note that if this function
588 * is called on a _running_ hw structure, the drvr_stop() function is
589 * called.
591 * Arguments:
592 * hw device structure
594 * Returns:
595 * nothing, this function is not allowed to fail.
597 * Side effects:
599 * Call context:
600 * process
601 ----------------------------------------------------------------*/
602 void hfa384x_destroy(hfa384x_t *hw)
604 struct sk_buff *skb;
606 if (hw->state == HFA384x_STATE_RUNNING)
607 hfa384x_drvr_stop(hw);
608 hw->state = HFA384x_STATE_PREINIT;
610 kfree(hw->scanresults);
611 hw->scanresults = NULL;
613 /* Now to clean out the auth queue */
614 while ((skb = skb_dequeue(&hw->authq)))
615 dev_kfree_skb(skb);
618 static hfa384x_usbctlx_t *usbctlx_alloc(void)
620 hfa384x_usbctlx_t *ctlx;
622 ctlx = kmalloc(sizeof(*ctlx), in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
623 if (ctlx != NULL) {
624 memset(ctlx, 0, sizeof(*ctlx));
625 init_completion(&ctlx->done);
628 return ctlx;
631 static int
632 usbctlx_get_status(const hfa384x_usb_cmdresp_t *cmdresp,
633 hfa384x_cmdresult_t *result)
635 result->status = le16_to_cpu(cmdresp->status);
636 result->resp0 = le16_to_cpu(cmdresp->resp0);
637 result->resp1 = le16_to_cpu(cmdresp->resp1);
638 result->resp2 = le16_to_cpu(cmdresp->resp2);
640 pr_debug("cmdresult:status=0x%04x resp0=0x%04x resp1=0x%04x resp2=0x%04x\n",
641 result->status, result->resp0, result->resp1, result->resp2);
643 return result->status & HFA384x_STATUS_RESULT;
646 static void
647 usbctlx_get_rridresult(const hfa384x_usb_rridresp_t *rridresp,
648 hfa384x_rridresult_t *result)
650 result->rid = le16_to_cpu(rridresp->rid);
651 result->riddata = rridresp->data;
652 result->riddata_len = ((le16_to_cpu(rridresp->frmlen) - 1) * 2);
655 /*----------------------------------------------------------------
656 * Completor object:
657 * This completor must be passed to hfa384x_usbctlx_complete_sync()
658 * when processing a CTLX that returns a hfa384x_cmdresult_t structure.
659 ----------------------------------------------------------------*/
660 struct usbctlx_cmd_completor {
661 struct usbctlx_completor head;
663 const hfa384x_usb_cmdresp_t *cmdresp;
664 hfa384x_cmdresult_t *result;
667 static inline int usbctlx_cmd_completor_fn(struct usbctlx_completor *head)
669 struct usbctlx_cmd_completor *complete;
671 complete = (struct usbctlx_cmd_completor *)head;
672 return usbctlx_get_status(complete->cmdresp, complete->result);
675 static inline struct usbctlx_completor *init_cmd_completor(
676 struct usbctlx_cmd_completor
677 *completor,
678 const hfa384x_usb_cmdresp_t
679 *cmdresp,
680 hfa384x_cmdresult_t *result)
682 completor->head.complete = usbctlx_cmd_completor_fn;
683 completor->cmdresp = cmdresp;
684 completor->result = result;
685 return &(completor->head);
688 /*----------------------------------------------------------------
689 * Completor object:
690 * This completor must be passed to hfa384x_usbctlx_complete_sync()
691 * when processing a CTLX that reads a RID.
692 ----------------------------------------------------------------*/
693 struct usbctlx_rrid_completor {
694 struct usbctlx_completor head;
696 const hfa384x_usb_rridresp_t *rridresp;
697 void *riddata;
698 unsigned int riddatalen;
701 static int usbctlx_rrid_completor_fn(struct usbctlx_completor *head)
703 struct usbctlx_rrid_completor *complete;
704 hfa384x_rridresult_t rridresult;
706 complete = (struct usbctlx_rrid_completor *)head;
707 usbctlx_get_rridresult(complete->rridresp, &rridresult);
709 /* Validate the length, note body len calculation in bytes */
710 if (rridresult.riddata_len != complete->riddatalen) {
711 pr_warn("RID len mismatch, rid=0x%04x hlen=%d fwlen=%d\n",
712 rridresult.rid,
713 complete->riddatalen, rridresult.riddata_len);
714 return -ENODATA;
717 memcpy(complete->riddata, rridresult.riddata, complete->riddatalen);
718 return 0;
721 static inline struct usbctlx_completor *init_rrid_completor(
722 struct usbctlx_rrid_completor
723 *completor,
724 const hfa384x_usb_rridresp_t
725 *rridresp,
726 void *riddata,
727 unsigned int riddatalen)
729 completor->head.complete = usbctlx_rrid_completor_fn;
730 completor->rridresp = rridresp;
731 completor->riddata = riddata;
732 completor->riddatalen = riddatalen;
733 return &(completor->head);
736 /*----------------------------------------------------------------
737 * Completor object:
738 * Interprets the results of a synchronous RID-write
739 ----------------------------------------------------------------*/
740 #define init_wrid_completor init_cmd_completor
742 /*----------------------------------------------------------------
743 * Completor object:
744 * Interprets the results of a synchronous memory-write
745 ----------------------------------------------------------------*/
746 #define init_wmem_completor init_cmd_completor
748 /*----------------------------------------------------------------
749 * Completor object:
750 * Interprets the results of a synchronous memory-read
751 ----------------------------------------------------------------*/
752 struct usbctlx_rmem_completor {
753 struct usbctlx_completor head;
755 const hfa384x_usb_rmemresp_t *rmemresp;
756 void *data;
757 unsigned int len;
760 static int usbctlx_rmem_completor_fn(struct usbctlx_completor *head)
762 struct usbctlx_rmem_completor *complete =
763 (struct usbctlx_rmem_completor *)head;
765 pr_debug("rmemresp:len=%d\n", complete->rmemresp->frmlen);
766 memcpy(complete->data, complete->rmemresp->data, complete->len);
767 return 0;
770 static inline struct usbctlx_completor *init_rmem_completor(
771 struct usbctlx_rmem_completor
772 *completor,
773 hfa384x_usb_rmemresp_t
774 *rmemresp,
775 void *data,
776 unsigned int len)
778 completor->head.complete = usbctlx_rmem_completor_fn;
779 completor->rmemresp = rmemresp;
780 completor->data = data;
781 completor->len = len;
782 return &(completor->head);
785 /*----------------------------------------------------------------
786 * hfa384x_cb_status
788 * Ctlx_complete handler for async CMD type control exchanges.
789 * mark the hw struct as such.
791 * Note: If the handling is changed here, it should probably be
792 * changed in docmd as well.
794 * Arguments:
795 * hw hw struct
796 * ctlx completed CTLX
798 * Returns:
799 * nothing
801 * Side effects:
803 * Call context:
804 * interrupt
805 ----------------------------------------------------------------*/
806 static void hfa384x_cb_status(hfa384x_t *hw, const hfa384x_usbctlx_t *ctlx)
808 if (ctlx->usercb != NULL) {
809 hfa384x_cmdresult_t cmdresult;
811 if (ctlx->state != CTLX_COMPLETE) {
812 memset(&cmdresult, 0, sizeof(cmdresult));
813 cmdresult.status =
814 HFA384x_STATUS_RESULT_SET(HFA384x_CMD_ERR);
815 } else {
816 usbctlx_get_status(&ctlx->inbuf.cmdresp, &cmdresult);
819 ctlx->usercb(hw, &cmdresult, ctlx->usercb_data);
823 /*----------------------------------------------------------------
824 * hfa384x_cb_rrid
826 * CTLX completion handler for async RRID type control exchanges.
828 * Note: If the handling is changed here, it should probably be
829 * changed in dorrid as well.
831 * Arguments:
832 * hw hw struct
833 * ctlx completed CTLX
835 * Returns:
836 * nothing
838 * Side effects:
840 * Call context:
841 * interrupt
842 ----------------------------------------------------------------*/
843 static void hfa384x_cb_rrid(hfa384x_t *hw, const hfa384x_usbctlx_t *ctlx)
845 if (ctlx->usercb != NULL) {
846 hfa384x_rridresult_t rridresult;
848 if (ctlx->state != CTLX_COMPLETE) {
849 memset(&rridresult, 0, sizeof(rridresult));
850 rridresult.rid = le16_to_cpu(ctlx->outbuf.rridreq.rid);
851 } else {
852 usbctlx_get_rridresult(&ctlx->inbuf.rridresp,
853 &rridresult);
856 ctlx->usercb(hw, &rridresult, ctlx->usercb_data);
860 static inline int hfa384x_docmd_wait(hfa384x_t *hw, hfa384x_metacmd_t *cmd)
862 return hfa384x_docmd(hw, DOWAIT, cmd, NULL, NULL, NULL);
865 static inline int
866 hfa384x_docmd_async(hfa384x_t *hw,
867 hfa384x_metacmd_t *cmd,
868 ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data)
870 return hfa384x_docmd(hw, DOASYNC, cmd, cmdcb, usercb, usercb_data);
873 static inline int
874 hfa384x_dorrid_wait(hfa384x_t *hw, u16 rid, void *riddata,
875 unsigned int riddatalen)
877 return hfa384x_dorrid(hw, DOWAIT,
878 rid, riddata, riddatalen, NULL, NULL, NULL);
881 static inline int
882 hfa384x_dorrid_async(hfa384x_t *hw,
883 u16 rid, void *riddata, unsigned int riddatalen,
884 ctlx_cmdcb_t cmdcb,
885 ctlx_usercb_t usercb, void *usercb_data)
887 return hfa384x_dorrid(hw, DOASYNC,
888 rid, riddata, riddatalen,
889 cmdcb, usercb, usercb_data);
892 static inline int
893 hfa384x_dowrid_wait(hfa384x_t *hw, u16 rid, void *riddata,
894 unsigned int riddatalen)
896 return hfa384x_dowrid(hw, DOWAIT,
897 rid, riddata, riddatalen, NULL, NULL, NULL);
900 static inline int
901 hfa384x_dowrid_async(hfa384x_t *hw,
902 u16 rid, void *riddata, unsigned int riddatalen,
903 ctlx_cmdcb_t cmdcb,
904 ctlx_usercb_t usercb, void *usercb_data)
906 return hfa384x_dowrid(hw, DOASYNC,
907 rid, riddata, riddatalen,
908 cmdcb, usercb, usercb_data);
911 static inline int
912 hfa384x_dormem_wait(hfa384x_t *hw,
913 u16 page, u16 offset, void *data, unsigned int len)
915 return hfa384x_dormem(hw, DOWAIT,
916 page, offset, data, len, NULL, NULL, NULL);
919 static inline int
920 hfa384x_dormem_async(hfa384x_t *hw,
921 u16 page, u16 offset, void *data, unsigned int len,
922 ctlx_cmdcb_t cmdcb,
923 ctlx_usercb_t usercb, void *usercb_data)
925 return hfa384x_dormem(hw, DOASYNC,
926 page, offset, data, len,
927 cmdcb, usercb, usercb_data);
930 static inline int
931 hfa384x_dowmem_wait(hfa384x_t *hw,
932 u16 page, u16 offset, void *data, unsigned int len)
934 return hfa384x_dowmem(hw, DOWAIT,
935 page, offset, data, len, NULL, NULL, NULL);
938 static inline int
939 hfa384x_dowmem_async(hfa384x_t *hw,
940 u16 page,
941 u16 offset,
942 void *data,
943 unsigned int len,
944 ctlx_cmdcb_t cmdcb,
945 ctlx_usercb_t usercb, void *usercb_data)
947 return hfa384x_dowmem(hw, DOASYNC,
948 page, offset, data, len,
949 cmdcb, usercb, usercb_data);
952 /*----------------------------------------------------------------
953 * hfa384x_cmd_initialize
955 * Issues the initialize command and sets the hw->state based
956 * on the result.
958 * Arguments:
959 * hw device structure
961 * Returns:
962 * 0 success
963 * >0 f/w reported error - f/w status code
964 * <0 driver reported error
966 * Side effects:
968 * Call context:
969 * process
970 ----------------------------------------------------------------*/
971 int hfa384x_cmd_initialize(hfa384x_t *hw)
973 int result = 0;
974 int i;
975 hfa384x_metacmd_t cmd;
977 cmd.cmd = HFA384x_CMDCODE_INIT;
978 cmd.parm0 = 0;
979 cmd.parm1 = 0;
980 cmd.parm2 = 0;
982 result = hfa384x_docmd_wait(hw, &cmd);
984 pr_debug("cmdresp.init: status=0x%04x, resp0=0x%04x, resp1=0x%04x, resp2=0x%04x\n",
985 cmd.result.status,
986 cmd.result.resp0, cmd.result.resp1, cmd.result.resp2);
987 if (result == 0) {
988 for (i = 0; i < HFA384x_NUMPORTS_MAX; i++)
989 hw->port_enabled[i] = 0;
992 hw->link_status = HFA384x_LINK_NOTCONNECTED;
994 return result;
997 /*----------------------------------------------------------------
998 * hfa384x_cmd_disable
1000 * Issues the disable command to stop communications on one of
1001 * the MACs 'ports'.
1003 * Arguments:
1004 * hw device structure
1005 * macport MAC port number (host order)
1007 * Returns:
1008 * 0 success
1009 * >0 f/w reported failure - f/w status code
1010 * <0 driver reported error (timeout|bad arg)
1012 * Side effects:
1014 * Call context:
1015 * process
1016 ----------------------------------------------------------------*/
1017 int hfa384x_cmd_disable(hfa384x_t *hw, u16 macport)
1019 int result = 0;
1020 hfa384x_metacmd_t cmd;
1022 cmd.cmd = HFA384x_CMD_CMDCODE_SET(HFA384x_CMDCODE_DISABLE) |
1023 HFA384x_CMD_MACPORT_SET(macport);
1024 cmd.parm0 = 0;
1025 cmd.parm1 = 0;
1026 cmd.parm2 = 0;
1028 result = hfa384x_docmd_wait(hw, &cmd);
1030 return result;
1033 /*----------------------------------------------------------------
1034 * hfa384x_cmd_enable
1036 * Issues the enable command to enable communications on one of
1037 * the MACs 'ports'.
1039 * Arguments:
1040 * hw device structure
1041 * macport MAC port number
1043 * Returns:
1044 * 0 success
1045 * >0 f/w reported failure - f/w status code
1046 * <0 driver reported error (timeout|bad arg)
1048 * Side effects:
1050 * Call context:
1051 * process
1052 ----------------------------------------------------------------*/
1053 int hfa384x_cmd_enable(hfa384x_t *hw, u16 macport)
1055 int result = 0;
1056 hfa384x_metacmd_t cmd;
1058 cmd.cmd = HFA384x_CMD_CMDCODE_SET(HFA384x_CMDCODE_ENABLE) |
1059 HFA384x_CMD_MACPORT_SET(macport);
1060 cmd.parm0 = 0;
1061 cmd.parm1 = 0;
1062 cmd.parm2 = 0;
1064 result = hfa384x_docmd_wait(hw, &cmd);
1066 return result;
1069 /*----------------------------------------------------------------
1070 * hfa384x_cmd_monitor
1072 * Enables the 'monitor mode' of the MAC. Here's the description of
1073 * monitor mode that I've received thus far:
1075 * "The "monitor mode" of operation is that the MAC passes all
1076 * frames for which the PLCP checks are correct. All received
1077 * MPDUs are passed to the host with MAC Port = 7, with a
1078 * receive status of good, FCS error, or undecryptable. Passing
1079 * certain MPDUs is a violation of the 802.11 standard, but useful
1080 * for a debugging tool." Normal communication is not possible
1081 * while monitor mode is enabled.
1083 * Arguments:
1084 * hw device structure
1085 * enable a code (0x0b|0x0f) that enables/disables
1086 * monitor mode. (host order)
1088 * Returns:
1089 * 0 success
1090 * >0 f/w reported failure - f/w status code
1091 * <0 driver reported error (timeout|bad arg)
1093 * Side effects:
1095 * Call context:
1096 * process
1097 ----------------------------------------------------------------*/
1098 int hfa384x_cmd_monitor(hfa384x_t *hw, u16 enable)
1100 int result = 0;
1101 hfa384x_metacmd_t cmd;
1103 cmd.cmd = HFA384x_CMD_CMDCODE_SET(HFA384x_CMDCODE_MONITOR) |
1104 HFA384x_CMD_AINFO_SET(enable);
1105 cmd.parm0 = 0;
1106 cmd.parm1 = 0;
1107 cmd.parm2 = 0;
1109 result = hfa384x_docmd_wait(hw, &cmd);
1111 return result;
1114 /*----------------------------------------------------------------
1115 * hfa384x_cmd_download
1117 * Sets the controls for the MAC controller code/data download
1118 * process. The arguments set the mode and address associated
1119 * with a download. Note that the aux registers should be enabled
1120 * prior to setting one of the download enable modes.
1122 * Arguments:
1123 * hw device structure
1124 * mode 0 - Disable programming and begin code exec
1125 * 1 - Enable volatile mem programming
1126 * 2 - Enable non-volatile mem programming
1127 * 3 - Program non-volatile section from NV download
1128 * buffer.
1129 * (host order)
1130 * lowaddr
1131 * highaddr For mode 1, sets the high & low order bits of
1132 * the "destination address". This address will be
1133 * the execution start address when download is
1134 * subsequently disabled.
1135 * For mode 2, sets the high & low order bits of
1136 * the destination in NV ram.
1137 * For modes 0 & 3, should be zero. (host order)
1138 * NOTE: these are CMD format.
1139 * codelen Length of the data to write in mode 2,
1140 * zero otherwise. (host order)
1142 * Returns:
1143 * 0 success
1144 * >0 f/w reported failure - f/w status code
1145 * <0 driver reported error (timeout|bad arg)
1147 * Side effects:
1149 * Call context:
1150 * process
1151 ----------------------------------------------------------------*/
1152 int hfa384x_cmd_download(hfa384x_t *hw, u16 mode, u16 lowaddr,
1153 u16 highaddr, u16 codelen)
1155 int result = 0;
1156 hfa384x_metacmd_t cmd;
1158 pr_debug("mode=%d, lowaddr=0x%04x, highaddr=0x%04x, codelen=%d\n",
1159 mode, lowaddr, highaddr, codelen);
1161 cmd.cmd = (HFA384x_CMD_CMDCODE_SET(HFA384x_CMDCODE_DOWNLD) |
1162 HFA384x_CMD_PROGMODE_SET(mode));
1164 cmd.parm0 = lowaddr;
1165 cmd.parm1 = highaddr;
1166 cmd.parm2 = codelen;
1168 result = hfa384x_docmd_wait(hw, &cmd);
1170 return result;
1173 /*----------------------------------------------------------------
1174 * hfa384x_corereset
1176 * Perform a reset of the hfa38xx MAC core. We assume that the hw
1177 * structure is in its "created" state. That is, it is initialized
1178 * with proper values. Note that if a reset is done after the
1179 * device has been active for awhile, the caller might have to clean
1180 * up some leftover cruft in the hw structure.
1182 * Arguments:
1183 * hw device structure
1184 * holdtime how long (in ms) to hold the reset
1185 * settletime how long (in ms) to wait after releasing
1186 * the reset
1188 * Returns:
1189 * nothing
1191 * Side effects:
1193 * Call context:
1194 * process
1195 ----------------------------------------------------------------*/
1196 int hfa384x_corereset(hfa384x_t *hw, int holdtime, int settletime, int genesis)
1198 int result = 0;
1200 result = usb_reset_device(hw->usb);
1201 if (result < 0) {
1202 netdev_err(hw->wlandev->netdev, "usb_reset_device() failed, result=%d.\n",
1203 result);
1206 return result;
1209 /*----------------------------------------------------------------
1210 * hfa384x_usbctlx_complete_sync
1212 * Waits for a synchronous CTLX object to complete,
1213 * and then handles the response.
1215 * Arguments:
1216 * hw device structure
1217 * ctlx CTLX ptr
1218 * completor functor object to decide what to
1219 * do with the CTLX's result.
1221 * Returns:
1222 * 0 Success
1223 * -ERESTARTSYS Interrupted by a signal
1224 * -EIO CTLX failed
1225 * -ENODEV Adapter was unplugged
1226 * ??? Result from completor
1228 * Side effects:
1230 * Call context:
1231 * process
1232 ----------------------------------------------------------------*/
1233 static int hfa384x_usbctlx_complete_sync(hfa384x_t *hw,
1234 hfa384x_usbctlx_t *ctlx,
1235 struct usbctlx_completor *completor)
1237 unsigned long flags;
1238 int result;
1240 result = wait_for_completion_interruptible(&ctlx->done);
1242 spin_lock_irqsave(&hw->ctlxq.lock, flags);
1245 * We can only handle the CTLX if the USB disconnect
1246 * function has not run yet ...
1248 cleanup:
1249 if (hw->wlandev->hwremoved) {
1250 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
1251 result = -ENODEV;
1252 } else if (result != 0) {
1253 int runqueue = 0;
1256 * We were probably interrupted, so delete
1257 * this CTLX asynchronously, kill the timers
1258 * and the URB, and then start the next
1259 * pending CTLX.
1261 * NOTE: We can only delete the timers and
1262 * the URB if this CTLX is active.
1264 if (ctlx == get_active_ctlx(hw)) {
1265 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
1267 del_singleshot_timer_sync(&hw->reqtimer);
1268 del_singleshot_timer_sync(&hw->resptimer);
1269 hw->req_timer_done = 1;
1270 hw->resp_timer_done = 1;
1271 usb_kill_urb(&hw->ctlx_urb);
1273 spin_lock_irqsave(&hw->ctlxq.lock, flags);
1275 runqueue = 1;
1278 * This scenario is so unlikely that I'm
1279 * happy with a grubby "goto" solution ...
1281 if (hw->wlandev->hwremoved)
1282 goto cleanup;
1286 * The completion task will send this CTLX
1287 * to the reaper the next time it runs. We
1288 * are no longer in a hurry.
1290 ctlx->reapable = 1;
1291 ctlx->state = CTLX_REQ_FAILED;
1292 list_move_tail(&ctlx->list, &hw->ctlxq.completing);
1294 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
1296 if (runqueue)
1297 hfa384x_usbctlxq_run(hw);
1298 } else {
1299 if (ctlx->state == CTLX_COMPLETE) {
1300 result = completor->complete(completor);
1301 } else {
1302 netdev_warn(hw->wlandev->netdev, "CTLX[%d] error: state(%s)\n",
1303 le16_to_cpu(ctlx->outbuf.type),
1304 ctlxstr(ctlx->state));
1305 result = -EIO;
1308 list_del(&ctlx->list);
1309 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
1310 kfree(ctlx);
1313 return result;
1316 /*----------------------------------------------------------------
1317 * hfa384x_docmd
1319 * Constructs a command CTLX and submits it.
1321 * NOTE: Any changes to the 'post-submit' code in this function
1322 * need to be carried over to hfa384x_cbcmd() since the handling
1323 * is virtually identical.
1325 * Arguments:
1326 * hw device structure
1327 * mode DOWAIT or DOASYNC
1328 * cmd cmd structure. Includes all arguments and result
1329 * data points. All in host order. in host order
1330 * cmdcb command-specific callback
1331 * usercb user callback for async calls, NULL for DOWAIT calls
1332 * usercb_data user supplied data pointer for async calls, NULL
1333 * for DOASYNC calls
1335 * Returns:
1336 * 0 success
1337 * -EIO CTLX failure
1338 * -ERESTARTSYS Awakened on signal
1339 * >0 command indicated error, Status and Resp0-2 are
1340 * in hw structure.
1342 * Side effects:
1345 * Call context:
1346 * process
1347 ----------------------------------------------------------------*/
1348 static int
1349 hfa384x_docmd(hfa384x_t *hw,
1350 enum cmd_mode mode,
1351 hfa384x_metacmd_t *cmd,
1352 ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data)
1354 int result;
1355 hfa384x_usbctlx_t *ctlx;
1357 ctlx = usbctlx_alloc();
1358 if (ctlx == NULL) {
1359 result = -ENOMEM;
1360 goto done;
1363 /* Initialize the command */
1364 ctlx->outbuf.cmdreq.type = cpu_to_le16(HFA384x_USB_CMDREQ);
1365 ctlx->outbuf.cmdreq.cmd = cpu_to_le16(cmd->cmd);
1366 ctlx->outbuf.cmdreq.parm0 = cpu_to_le16(cmd->parm0);
1367 ctlx->outbuf.cmdreq.parm1 = cpu_to_le16(cmd->parm1);
1368 ctlx->outbuf.cmdreq.parm2 = cpu_to_le16(cmd->parm2);
1370 ctlx->outbufsize = sizeof(ctlx->outbuf.cmdreq);
1372 pr_debug("cmdreq: cmd=0x%04x parm0=0x%04x parm1=0x%04x parm2=0x%04x\n",
1373 cmd->cmd, cmd->parm0, cmd->parm1, cmd->parm2);
1375 ctlx->reapable = mode;
1376 ctlx->cmdcb = cmdcb;
1377 ctlx->usercb = usercb;
1378 ctlx->usercb_data = usercb_data;
1380 result = hfa384x_usbctlx_submit(hw, ctlx);
1381 if (result != 0) {
1382 kfree(ctlx);
1383 } else if (mode == DOWAIT) {
1384 struct usbctlx_cmd_completor completor;
1386 result =
1387 hfa384x_usbctlx_complete_sync(hw, ctlx,
1388 init_cmd_completor(&completor,
1389 &ctlx->
1390 inbuf.
1391 cmdresp,
1392 &cmd->
1393 result));
1396 done:
1397 return result;
1400 /*----------------------------------------------------------------
1401 * hfa384x_dorrid
1403 * Constructs a read rid CTLX and issues it.
1405 * NOTE: Any changes to the 'post-submit' code in this function
1406 * need to be carried over to hfa384x_cbrrid() since the handling
1407 * is virtually identical.
1409 * Arguments:
1410 * hw device structure
1411 * mode DOWAIT or DOASYNC
1412 * rid Read RID number (host order)
1413 * riddata Caller supplied buffer that MAC formatted RID.data
1414 * record will be written to for DOWAIT calls. Should
1415 * be NULL for DOASYNC calls.
1416 * riddatalen Buffer length for DOWAIT calls. Zero for DOASYNC calls.
1417 * cmdcb command callback for async calls, NULL for DOWAIT calls
1418 * usercb user callback for async calls, NULL for DOWAIT calls
1419 * usercb_data user supplied data pointer for async calls, NULL
1420 * for DOWAIT calls
1422 * Returns:
1423 * 0 success
1424 * -EIO CTLX failure
1425 * -ERESTARTSYS Awakened on signal
1426 * -ENODATA riddatalen != macdatalen
1427 * >0 command indicated error, Status and Resp0-2 are
1428 * in hw structure.
1430 * Side effects:
1432 * Call context:
1433 * interrupt (DOASYNC)
1434 * process (DOWAIT or DOASYNC)
1435 ----------------------------------------------------------------*/
1436 static int
1437 hfa384x_dorrid(hfa384x_t *hw,
1438 enum cmd_mode mode,
1439 u16 rid,
1440 void *riddata,
1441 unsigned int riddatalen,
1442 ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data)
1444 int result;
1445 hfa384x_usbctlx_t *ctlx;
1447 ctlx = usbctlx_alloc();
1448 if (ctlx == NULL) {
1449 result = -ENOMEM;
1450 goto done;
1453 /* Initialize the command */
1454 ctlx->outbuf.rridreq.type = cpu_to_le16(HFA384x_USB_RRIDREQ);
1455 ctlx->outbuf.rridreq.frmlen =
1456 cpu_to_le16(sizeof(ctlx->outbuf.rridreq.rid));
1457 ctlx->outbuf.rridreq.rid = cpu_to_le16(rid);
1459 ctlx->outbufsize = sizeof(ctlx->outbuf.rridreq);
1461 ctlx->reapable = mode;
1462 ctlx->cmdcb = cmdcb;
1463 ctlx->usercb = usercb;
1464 ctlx->usercb_data = usercb_data;
1466 /* Submit the CTLX */
1467 result = hfa384x_usbctlx_submit(hw, ctlx);
1468 if (result != 0) {
1469 kfree(ctlx);
1470 } else if (mode == DOWAIT) {
1471 struct usbctlx_rrid_completor completor;
1473 result =
1474 hfa384x_usbctlx_complete_sync(hw, ctlx,
1475 init_rrid_completor
1476 (&completor,
1477 &ctlx->inbuf.rridresp,
1478 riddata, riddatalen));
1481 done:
1482 return result;
1485 /*----------------------------------------------------------------
1486 * hfa384x_dowrid
1488 * Constructs a write rid CTLX and issues it.
1490 * NOTE: Any changes to the 'post-submit' code in this function
1491 * need to be carried over to hfa384x_cbwrid() since the handling
1492 * is virtually identical.
1494 * Arguments:
1495 * hw device structure
1496 * enum cmd_mode DOWAIT or DOASYNC
1497 * rid RID code
1498 * riddata Data portion of RID formatted for MAC
1499 * riddatalen Length of the data portion in bytes
1500 * cmdcb command callback for async calls, NULL for DOWAIT calls
1501 * usercb user callback for async calls, NULL for DOWAIT calls
1502 * usercb_data user supplied data pointer for async calls
1504 * Returns:
1505 * 0 success
1506 * -ETIMEDOUT timed out waiting for register ready or
1507 * command completion
1508 * >0 command indicated error, Status and Resp0-2 are
1509 * in hw structure.
1511 * Side effects:
1513 * Call context:
1514 * interrupt (DOASYNC)
1515 * process (DOWAIT or DOASYNC)
1516 ----------------------------------------------------------------*/
1517 static int
1518 hfa384x_dowrid(hfa384x_t *hw,
1519 enum cmd_mode mode,
1520 u16 rid,
1521 void *riddata,
1522 unsigned int riddatalen,
1523 ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data)
1525 int result;
1526 hfa384x_usbctlx_t *ctlx;
1528 ctlx = usbctlx_alloc();
1529 if (ctlx == NULL) {
1530 result = -ENOMEM;
1531 goto done;
1534 /* Initialize the command */
1535 ctlx->outbuf.wridreq.type = cpu_to_le16(HFA384x_USB_WRIDREQ);
1536 ctlx->outbuf.wridreq.frmlen = cpu_to_le16((sizeof
1537 (ctlx->outbuf.wridreq.rid) +
1538 riddatalen + 1) / 2);
1539 ctlx->outbuf.wridreq.rid = cpu_to_le16(rid);
1540 memcpy(ctlx->outbuf.wridreq.data, riddata, riddatalen);
1542 ctlx->outbufsize = sizeof(ctlx->outbuf.wridreq.type) +
1543 sizeof(ctlx->outbuf.wridreq.frmlen) +
1544 sizeof(ctlx->outbuf.wridreq.rid) + riddatalen;
1546 ctlx->reapable = mode;
1547 ctlx->cmdcb = cmdcb;
1548 ctlx->usercb = usercb;
1549 ctlx->usercb_data = usercb_data;
1551 /* Submit the CTLX */
1552 result = hfa384x_usbctlx_submit(hw, ctlx);
1553 if (result != 0) {
1554 kfree(ctlx);
1555 } else if (mode == DOWAIT) {
1556 struct usbctlx_cmd_completor completor;
1557 hfa384x_cmdresult_t wridresult;
1559 result = hfa384x_usbctlx_complete_sync(hw,
1560 ctlx,
1561 init_wrid_completor
1562 (&completor,
1563 &ctlx->inbuf.wridresp,
1564 &wridresult));
1567 done:
1568 return result;
1571 /*----------------------------------------------------------------
1572 * hfa384x_dormem
1574 * Constructs a readmem CTLX and issues it.
1576 * NOTE: Any changes to the 'post-submit' code in this function
1577 * need to be carried over to hfa384x_cbrmem() since the handling
1578 * is virtually identical.
1580 * Arguments:
1581 * hw device structure
1582 * mode DOWAIT or DOASYNC
1583 * page MAC address space page (CMD format)
1584 * offset MAC address space offset
1585 * data Ptr to data buffer to receive read
1586 * len Length of the data to read (max == 2048)
1587 * cmdcb command callback for async calls, NULL for DOWAIT calls
1588 * usercb user callback for async calls, NULL for DOWAIT calls
1589 * usercb_data user supplied data pointer for async calls
1591 * Returns:
1592 * 0 success
1593 * -ETIMEDOUT timed out waiting for register ready or
1594 * command completion
1595 * >0 command indicated error, Status and Resp0-2 are
1596 * in hw structure.
1598 * Side effects:
1600 * Call context:
1601 * interrupt (DOASYNC)
1602 * process (DOWAIT or DOASYNC)
1603 ----------------------------------------------------------------*/
1604 static int
1605 hfa384x_dormem(hfa384x_t *hw,
1606 enum cmd_mode mode,
1607 u16 page,
1608 u16 offset,
1609 void *data,
1610 unsigned int len,
1611 ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data)
1613 int result;
1614 hfa384x_usbctlx_t *ctlx;
1616 ctlx = usbctlx_alloc();
1617 if (ctlx == NULL) {
1618 result = -ENOMEM;
1619 goto done;
1622 /* Initialize the command */
1623 ctlx->outbuf.rmemreq.type = cpu_to_le16(HFA384x_USB_RMEMREQ);
1624 ctlx->outbuf.rmemreq.frmlen =
1625 cpu_to_le16(sizeof(ctlx->outbuf.rmemreq.offset) +
1626 sizeof(ctlx->outbuf.rmemreq.page) + len);
1627 ctlx->outbuf.rmemreq.offset = cpu_to_le16(offset);
1628 ctlx->outbuf.rmemreq.page = cpu_to_le16(page);
1630 ctlx->outbufsize = sizeof(ctlx->outbuf.rmemreq);
1632 pr_debug("type=0x%04x frmlen=%d offset=0x%04x page=0x%04x\n",
1633 ctlx->outbuf.rmemreq.type,
1634 ctlx->outbuf.rmemreq.frmlen,
1635 ctlx->outbuf.rmemreq.offset, ctlx->outbuf.rmemreq.page);
1637 pr_debug("pktsize=%zd\n", ROUNDUP64(sizeof(ctlx->outbuf.rmemreq)));
1639 ctlx->reapable = mode;
1640 ctlx->cmdcb = cmdcb;
1641 ctlx->usercb = usercb;
1642 ctlx->usercb_data = usercb_data;
1644 result = hfa384x_usbctlx_submit(hw, ctlx);
1645 if (result != 0) {
1646 kfree(ctlx);
1647 } else if (mode == DOWAIT) {
1648 struct usbctlx_rmem_completor completor;
1650 result =
1651 hfa384x_usbctlx_complete_sync(hw, ctlx,
1652 init_rmem_completor
1653 (&completor,
1654 &ctlx->inbuf.rmemresp, data,
1655 len));
1658 done:
1659 return result;
1662 /*----------------------------------------------------------------
1663 * hfa384x_dowmem
1665 * Constructs a writemem CTLX and issues it.
1667 * NOTE: Any changes to the 'post-submit' code in this function
1668 * need to be carried over to hfa384x_cbwmem() since the handling
1669 * is virtually identical.
1671 * Arguments:
1672 * hw device structure
1673 * mode DOWAIT or DOASYNC
1674 * page MAC address space page (CMD format)
1675 * offset MAC address space offset
1676 * data Ptr to data buffer containing write data
1677 * len Length of the data to read (max == 2048)
1678 * cmdcb command callback for async calls, NULL for DOWAIT calls
1679 * usercb user callback for async calls, NULL for DOWAIT calls
1680 * usercb_data user supplied data pointer for async calls.
1682 * Returns:
1683 * 0 success
1684 * -ETIMEDOUT timed out waiting for register ready or
1685 * command completion
1686 * >0 command indicated error, Status and Resp0-2 are
1687 * in hw structure.
1689 * Side effects:
1691 * Call context:
1692 * interrupt (DOWAIT)
1693 * process (DOWAIT or DOASYNC)
1694 ----------------------------------------------------------------*/
1695 static int
1696 hfa384x_dowmem(hfa384x_t *hw,
1697 enum cmd_mode mode,
1698 u16 page,
1699 u16 offset,
1700 void *data,
1701 unsigned int len,
1702 ctlx_cmdcb_t cmdcb, ctlx_usercb_t usercb, void *usercb_data)
1704 int result;
1705 hfa384x_usbctlx_t *ctlx;
1707 pr_debug("page=0x%04x offset=0x%04x len=%d\n", page, offset, len);
1709 ctlx = usbctlx_alloc();
1710 if (ctlx == NULL) {
1711 result = -ENOMEM;
1712 goto done;
1715 /* Initialize the command */
1716 ctlx->outbuf.wmemreq.type = cpu_to_le16(HFA384x_USB_WMEMREQ);
1717 ctlx->outbuf.wmemreq.frmlen =
1718 cpu_to_le16(sizeof(ctlx->outbuf.wmemreq.offset) +
1719 sizeof(ctlx->outbuf.wmemreq.page) + len);
1720 ctlx->outbuf.wmemreq.offset = cpu_to_le16(offset);
1721 ctlx->outbuf.wmemreq.page = cpu_to_le16(page);
1722 memcpy(ctlx->outbuf.wmemreq.data, data, len);
1724 ctlx->outbufsize = sizeof(ctlx->outbuf.wmemreq.type) +
1725 sizeof(ctlx->outbuf.wmemreq.frmlen) +
1726 sizeof(ctlx->outbuf.wmemreq.offset) +
1727 sizeof(ctlx->outbuf.wmemreq.page) + len;
1729 ctlx->reapable = mode;
1730 ctlx->cmdcb = cmdcb;
1731 ctlx->usercb = usercb;
1732 ctlx->usercb_data = usercb_data;
1734 result = hfa384x_usbctlx_submit(hw, ctlx);
1735 if (result != 0) {
1736 kfree(ctlx);
1737 } else if (mode == DOWAIT) {
1738 struct usbctlx_cmd_completor completor;
1739 hfa384x_cmdresult_t wmemresult;
1741 result = hfa384x_usbctlx_complete_sync(hw,
1742 ctlx,
1743 init_wmem_completor
1744 (&completor,
1745 &ctlx->inbuf.wmemresp,
1746 &wmemresult));
1749 done:
1750 return result;
1753 /*----------------------------------------------------------------
1754 * hfa384x_drvr_commtallies
1756 * Send a commtallies inquiry to the MAC. Note that this is an async
1757 * call that will result in an info frame arriving sometime later.
1759 * Arguments:
1760 * hw device structure
1762 * Returns:
1763 * zero success.
1765 * Side effects:
1767 * Call context:
1768 * process
1769 ----------------------------------------------------------------*/
1770 int hfa384x_drvr_commtallies(hfa384x_t *hw)
1772 hfa384x_metacmd_t cmd;
1774 cmd.cmd = HFA384x_CMDCODE_INQ;
1775 cmd.parm0 = HFA384x_IT_COMMTALLIES;
1776 cmd.parm1 = 0;
1777 cmd.parm2 = 0;
1779 hfa384x_docmd_async(hw, &cmd, NULL, NULL, NULL);
1781 return 0;
1784 /*----------------------------------------------------------------
1785 * hfa384x_drvr_disable
1787 * Issues the disable command to stop communications on one of
1788 * the MACs 'ports'. Only macport 0 is valid for stations.
1789 * APs may also disable macports 1-6. Only ports that have been
1790 * previously enabled may be disabled.
1792 * Arguments:
1793 * hw device structure
1794 * macport MAC port number (host order)
1796 * Returns:
1797 * 0 success
1798 * >0 f/w reported failure - f/w status code
1799 * <0 driver reported error (timeout|bad arg)
1801 * Side effects:
1803 * Call context:
1804 * process
1805 ----------------------------------------------------------------*/
1806 int hfa384x_drvr_disable(hfa384x_t *hw, u16 macport)
1808 int result = 0;
1810 if ((!hw->isap && macport != 0) ||
1811 (hw->isap && !(macport <= HFA384x_PORTID_MAX)) ||
1812 !(hw->port_enabled[macport])) {
1813 result = -EINVAL;
1814 } else {
1815 result = hfa384x_cmd_disable(hw, macport);
1816 if (result == 0)
1817 hw->port_enabled[macport] = 0;
1819 return result;
1822 /*----------------------------------------------------------------
1823 * hfa384x_drvr_enable
1825 * Issues the enable command to enable communications on one of
1826 * the MACs 'ports'. Only macport 0 is valid for stations.
1827 * APs may also enable macports 1-6. Only ports that are currently
1828 * disabled may be enabled.
1830 * Arguments:
1831 * hw device structure
1832 * macport MAC port number
1834 * Returns:
1835 * 0 success
1836 * >0 f/w reported failure - f/w status code
1837 * <0 driver reported error (timeout|bad arg)
1839 * Side effects:
1841 * Call context:
1842 * process
1843 ----------------------------------------------------------------*/
1844 int hfa384x_drvr_enable(hfa384x_t *hw, u16 macport)
1846 int result = 0;
1848 if ((!hw->isap && macport != 0) ||
1849 (hw->isap && !(macport <= HFA384x_PORTID_MAX)) ||
1850 (hw->port_enabled[macport])) {
1851 result = -EINVAL;
1852 } else {
1853 result = hfa384x_cmd_enable(hw, macport);
1854 if (result == 0)
1855 hw->port_enabled[macport] = 1;
1857 return result;
1860 /*----------------------------------------------------------------
1861 * hfa384x_drvr_flashdl_enable
1863 * Begins the flash download state. Checks to see that we're not
1864 * already in a download state and that a port isn't enabled.
1865 * Sets the download state and retrieves the flash download
1866 * buffer location, buffer size, and timeout length.
1868 * Arguments:
1869 * hw device structure
1871 * Returns:
1872 * 0 success
1873 * >0 f/w reported error - f/w status code
1874 * <0 driver reported error
1876 * Side effects:
1878 * Call context:
1879 * process
1880 ----------------------------------------------------------------*/
1881 int hfa384x_drvr_flashdl_enable(hfa384x_t *hw)
1883 int result = 0;
1884 int i;
1886 /* Check that a port isn't active */
1887 for (i = 0; i < HFA384x_PORTID_MAX; i++) {
1888 if (hw->port_enabled[i]) {
1889 pr_debug("called when port enabled.\n");
1890 return -EINVAL;
1894 /* Check that we're not already in a download state */
1895 if (hw->dlstate != HFA384x_DLSTATE_DISABLED)
1896 return -EINVAL;
1898 /* Retrieve the buffer loc&size and timeout */
1899 result = hfa384x_drvr_getconfig(hw, HFA384x_RID_DOWNLOADBUFFER,
1900 &(hw->bufinfo), sizeof(hw->bufinfo));
1901 if (result)
1902 return result;
1904 hw->bufinfo.page = le16_to_cpu(hw->bufinfo.page);
1905 hw->bufinfo.offset = le16_to_cpu(hw->bufinfo.offset);
1906 hw->bufinfo.len = le16_to_cpu(hw->bufinfo.len);
1907 result = hfa384x_drvr_getconfig16(hw, HFA384x_RID_MAXLOADTIME,
1908 &(hw->dltimeout));
1909 if (result)
1910 return result;
1912 hw->dltimeout = le16_to_cpu(hw->dltimeout);
1914 pr_debug("flashdl_enable\n");
1916 hw->dlstate = HFA384x_DLSTATE_FLASHENABLED;
1918 return result;
1921 /*----------------------------------------------------------------
1922 * hfa384x_drvr_flashdl_disable
1924 * Ends the flash download state. Note that this will cause the MAC
1925 * firmware to restart.
1927 * Arguments:
1928 * hw device structure
1930 * Returns:
1931 * 0 success
1932 * >0 f/w reported error - f/w status code
1933 * <0 driver reported error
1935 * Side effects:
1937 * Call context:
1938 * process
1939 ----------------------------------------------------------------*/
1940 int hfa384x_drvr_flashdl_disable(hfa384x_t *hw)
1942 /* Check that we're already in the download state */
1943 if (hw->dlstate != HFA384x_DLSTATE_FLASHENABLED)
1944 return -EINVAL;
1946 pr_debug("flashdl_enable\n");
1948 /* There isn't much we can do at this point, so I don't */
1949 /* bother w/ the return value */
1950 hfa384x_cmd_download(hw, HFA384x_PROGMODE_DISABLE, 0, 0, 0);
1951 hw->dlstate = HFA384x_DLSTATE_DISABLED;
1953 return 0;
1956 /*----------------------------------------------------------------
1957 * hfa384x_drvr_flashdl_write
1959 * Performs a FLASH download of a chunk of data. First checks to see
1960 * that we're in the FLASH download state, then sets the download
1961 * mode, uses the aux functions to 1) copy the data to the flash
1962 * buffer, 2) sets the download 'write flash' mode, 3) readback and
1963 * compare. Lather rinse, repeat as many times an necessary to get
1964 * all the given data into flash.
1965 * When all data has been written using this function (possibly
1966 * repeatedly), call drvr_flashdl_disable() to end the download state
1967 * and restart the MAC.
1969 * Arguments:
1970 * hw device structure
1971 * daddr Card address to write to. (host order)
1972 * buf Ptr to data to write.
1973 * len Length of data (host order).
1975 * Returns:
1976 * 0 success
1977 * >0 f/w reported error - f/w status code
1978 * <0 driver reported error
1980 * Side effects:
1982 * Call context:
1983 * process
1984 ----------------------------------------------------------------*/
1985 int hfa384x_drvr_flashdl_write(hfa384x_t *hw, u32 daddr, void *buf, u32 len)
1987 int result = 0;
1988 u32 dlbufaddr;
1989 int nburns;
1990 u32 burnlen;
1991 u32 burndaddr;
1992 u16 burnlo;
1993 u16 burnhi;
1994 int nwrites;
1995 u8 *writebuf;
1996 u16 writepage;
1997 u16 writeoffset;
1998 u32 writelen;
1999 int i;
2000 int j;
2002 pr_debug("daddr=0x%08x len=%d\n", daddr, len);
2004 /* Check that we're in the flash download state */
2005 if (hw->dlstate != HFA384x_DLSTATE_FLASHENABLED)
2006 return -EINVAL;
2008 netdev_info(hw->wlandev->netdev,
2009 "Download %d bytes to flash @0x%06x\n", len, daddr);
2011 /* Convert to flat address for arithmetic */
2012 /* NOTE: dlbuffer RID stores the address in AUX format */
2013 dlbufaddr =
2014 HFA384x_ADDR_AUX_MKFLAT(hw->bufinfo.page, hw->bufinfo.offset);
2015 pr_debug("dlbuf.page=0x%04x dlbuf.offset=0x%04x dlbufaddr=0x%08x\n",
2016 hw->bufinfo.page, hw->bufinfo.offset, dlbufaddr);
2017 /* Calculations to determine how many fills of the dlbuffer to do
2018 * and how many USB wmemreq's to do for each fill. At this point
2019 * in time, the dlbuffer size and the wmemreq size are the same.
2020 * Therefore, nwrites should always be 1. The extra complexity
2021 * here is a hedge against future changes.
2024 /* Figure out how many times to do the flash programming */
2025 nburns = len / hw->bufinfo.len;
2026 nburns += (len % hw->bufinfo.len) ? 1 : 0;
2028 /* For each flash program cycle, how many USB wmemreq's are needed? */
2029 nwrites = hw->bufinfo.len / HFA384x_USB_RWMEM_MAXLEN;
2030 nwrites += (hw->bufinfo.len % HFA384x_USB_RWMEM_MAXLEN) ? 1 : 0;
2032 /* For each burn */
2033 for (i = 0; i < nburns; i++) {
2034 /* Get the dest address and len */
2035 burnlen = (len - (hw->bufinfo.len * i)) > hw->bufinfo.len ?
2036 hw->bufinfo.len : (len - (hw->bufinfo.len * i));
2037 burndaddr = daddr + (hw->bufinfo.len * i);
2038 burnlo = HFA384x_ADDR_CMD_MKOFF(burndaddr);
2039 burnhi = HFA384x_ADDR_CMD_MKPAGE(burndaddr);
2041 netdev_info(hw->wlandev->netdev, "Writing %d bytes to flash @0x%06x\n",
2042 burnlen, burndaddr);
2044 /* Set the download mode */
2045 result = hfa384x_cmd_download(hw, HFA384x_PROGMODE_NV,
2046 burnlo, burnhi, burnlen);
2047 if (result) {
2048 netdev_err(hw->wlandev->netdev,
2049 "download(NV,lo=%x,hi=%x,len=%x) cmd failed, result=%d. Aborting d/l\n",
2050 burnlo, burnhi, burnlen, result);
2051 goto exit_proc;
2054 /* copy the data to the flash download buffer */
2055 for (j = 0; j < nwrites; j++) {
2056 writebuf = buf +
2057 (i * hw->bufinfo.len) +
2058 (j * HFA384x_USB_RWMEM_MAXLEN);
2060 writepage = HFA384x_ADDR_CMD_MKPAGE(dlbufaddr +
2061 (j * HFA384x_USB_RWMEM_MAXLEN));
2062 writeoffset = HFA384x_ADDR_CMD_MKOFF(dlbufaddr +
2063 (j * HFA384x_USB_RWMEM_MAXLEN));
2065 writelen = burnlen - (j * HFA384x_USB_RWMEM_MAXLEN);
2066 writelen = writelen > HFA384x_USB_RWMEM_MAXLEN ?
2067 HFA384x_USB_RWMEM_MAXLEN : writelen;
2069 result = hfa384x_dowmem_wait(hw,
2070 writepage,
2071 writeoffset,
2072 writebuf, writelen);
2075 /* set the download 'write flash' mode */
2076 result = hfa384x_cmd_download(hw,
2077 HFA384x_PROGMODE_NVWRITE,
2078 0, 0, 0);
2079 if (result) {
2080 netdev_err(hw->wlandev->netdev,
2081 "download(NVWRITE,lo=%x,hi=%x,len=%x) cmd failed, result=%d. Aborting d/l\n",
2082 burnlo, burnhi, burnlen, result);
2083 goto exit_proc;
2086 /* TODO: We really should do a readback and compare. */
2089 exit_proc:
2091 /* Leave the firmware in the 'post-prog' mode. flashdl_disable will */
2092 /* actually disable programming mode. Remember, that will cause the */
2093 /* the firmware to effectively reset itself. */
2095 return result;
2098 /*----------------------------------------------------------------
2099 * hfa384x_drvr_getconfig
2101 * Performs the sequence necessary to read a config/info item.
2103 * Arguments:
2104 * hw device structure
2105 * rid config/info record id (host order)
2106 * buf host side record buffer. Upon return it will
2107 * contain the body portion of the record (minus the
2108 * RID and len).
2109 * len buffer length (in bytes, should match record length)
2111 * Returns:
2112 * 0 success
2113 * >0 f/w reported error - f/w status code
2114 * <0 driver reported error
2115 * -ENODATA length mismatch between argument and retrieved
2116 * record.
2118 * Side effects:
2120 * Call context:
2121 * process
2122 ----------------------------------------------------------------*/
2123 int hfa384x_drvr_getconfig(hfa384x_t *hw, u16 rid, void *buf, u16 len)
2125 return hfa384x_dorrid_wait(hw, rid, buf, len);
2128 /*----------------------------------------------------------------
2129 * hfa384x_drvr_getconfig_async
2131 * Performs the sequence necessary to perform an async read of
2132 * of a config/info item.
2134 * Arguments:
2135 * hw device structure
2136 * rid config/info record id (host order)
2137 * buf host side record buffer. Upon return it will
2138 * contain the body portion of the record (minus the
2139 * RID and len).
2140 * len buffer length (in bytes, should match record length)
2141 * cbfn caller supplied callback, called when the command
2142 * is done (successful or not).
2143 * cbfndata pointer to some caller supplied data that will be
2144 * passed in as an argument to the cbfn.
2146 * Returns:
2147 * nothing the cbfn gets a status argument identifying if
2148 * any errors occur.
2149 * Side effects:
2150 * Queues an hfa384x_usbcmd_t for subsequent execution.
2152 * Call context:
2153 * Any
2154 ----------------------------------------------------------------*/
2156 hfa384x_drvr_getconfig_async(hfa384x_t *hw,
2157 u16 rid, ctlx_usercb_t usercb, void *usercb_data)
2159 return hfa384x_dorrid_async(hw, rid, NULL, 0,
2160 hfa384x_cb_rrid, usercb, usercb_data);
2163 /*----------------------------------------------------------------
2164 * hfa384x_drvr_setconfig_async
2166 * Performs the sequence necessary to write a config/info item.
2168 * Arguments:
2169 * hw device structure
2170 * rid config/info record id (in host order)
2171 * buf host side record buffer
2172 * len buffer length (in bytes)
2173 * usercb completion callback
2174 * usercb_data completion callback argument
2176 * Returns:
2177 * 0 success
2178 * >0 f/w reported error - f/w status code
2179 * <0 driver reported error
2181 * Side effects:
2183 * Call context:
2184 * process
2185 ----------------------------------------------------------------*/
2187 hfa384x_drvr_setconfig_async(hfa384x_t *hw,
2188 u16 rid,
2189 void *buf,
2190 u16 len, ctlx_usercb_t usercb, void *usercb_data)
2192 return hfa384x_dowrid_async(hw, rid, buf, len,
2193 hfa384x_cb_status, usercb, usercb_data);
2196 /*----------------------------------------------------------------
2197 * hfa384x_drvr_ramdl_disable
2199 * Ends the ram download state.
2201 * Arguments:
2202 * hw device structure
2204 * Returns:
2205 * 0 success
2206 * >0 f/w reported error - f/w status code
2207 * <0 driver reported error
2209 * Side effects:
2211 * Call context:
2212 * process
2213 ----------------------------------------------------------------*/
2214 int hfa384x_drvr_ramdl_disable(hfa384x_t *hw)
2216 /* Check that we're already in the download state */
2217 if (hw->dlstate != HFA384x_DLSTATE_RAMENABLED)
2218 return -EINVAL;
2220 pr_debug("ramdl_disable()\n");
2222 /* There isn't much we can do at this point, so I don't */
2223 /* bother w/ the return value */
2224 hfa384x_cmd_download(hw, HFA384x_PROGMODE_DISABLE, 0, 0, 0);
2225 hw->dlstate = HFA384x_DLSTATE_DISABLED;
2227 return 0;
2230 /*----------------------------------------------------------------
2231 * hfa384x_drvr_ramdl_enable
2233 * Begins the ram download state. Checks to see that we're not
2234 * already in a download state and that a port isn't enabled.
2235 * Sets the download state and calls cmd_download with the
2236 * ENABLE_VOLATILE subcommand and the exeaddr argument.
2238 * Arguments:
2239 * hw device structure
2240 * exeaddr the card execution address that will be
2241 * jumped to when ramdl_disable() is called
2242 * (host order).
2244 * Returns:
2245 * 0 success
2246 * >0 f/w reported error - f/w status code
2247 * <0 driver reported error
2249 * Side effects:
2251 * Call context:
2252 * process
2253 ----------------------------------------------------------------*/
2254 int hfa384x_drvr_ramdl_enable(hfa384x_t *hw, u32 exeaddr)
2256 int result = 0;
2257 u16 lowaddr;
2258 u16 hiaddr;
2259 int i;
2261 /* Check that a port isn't active */
2262 for (i = 0; i < HFA384x_PORTID_MAX; i++) {
2263 if (hw->port_enabled[i]) {
2264 netdev_err(hw->wlandev->netdev,
2265 "Can't download with a macport enabled.\n");
2266 return -EINVAL;
2270 /* Check that we're not already in a download state */
2271 if (hw->dlstate != HFA384x_DLSTATE_DISABLED) {
2272 netdev_err(hw->wlandev->netdev, "Download state not disabled.\n");
2273 return -EINVAL;
2276 pr_debug("ramdl_enable, exeaddr=0x%08x\n", exeaddr);
2278 /* Call the download(1,addr) function */
2279 lowaddr = HFA384x_ADDR_CMD_MKOFF(exeaddr);
2280 hiaddr = HFA384x_ADDR_CMD_MKPAGE(exeaddr);
2282 result = hfa384x_cmd_download(hw, HFA384x_PROGMODE_RAM,
2283 lowaddr, hiaddr, 0);
2285 if (result == 0) {
2286 /* Set the download state */
2287 hw->dlstate = HFA384x_DLSTATE_RAMENABLED;
2288 } else {
2289 pr_debug("cmd_download(0x%04x, 0x%04x) failed, result=%d.\n",
2290 lowaddr, hiaddr, result);
2293 return result;
2296 /*----------------------------------------------------------------
2297 * hfa384x_drvr_ramdl_write
2299 * Performs a RAM download of a chunk of data. First checks to see
2300 * that we're in the RAM download state, then uses the [read|write]mem USB
2301 * commands to 1) copy the data, 2) readback and compare. The download
2302 * state is unaffected. When all data has been written using
2303 * this function, call drvr_ramdl_disable() to end the download state
2304 * and restart the MAC.
2306 * Arguments:
2307 * hw device structure
2308 * daddr Card address to write to. (host order)
2309 * buf Ptr to data to write.
2310 * len Length of data (host order).
2312 * Returns:
2313 * 0 success
2314 * >0 f/w reported error - f/w status code
2315 * <0 driver reported error
2317 * Side effects:
2319 * Call context:
2320 * process
2321 ----------------------------------------------------------------*/
2322 int hfa384x_drvr_ramdl_write(hfa384x_t *hw, u32 daddr, void *buf, u32 len)
2324 int result = 0;
2325 int nwrites;
2326 u8 *data = buf;
2327 int i;
2328 u32 curraddr;
2329 u16 currpage;
2330 u16 curroffset;
2331 u16 currlen;
2333 /* Check that we're in the ram download state */
2334 if (hw->dlstate != HFA384x_DLSTATE_RAMENABLED)
2335 return -EINVAL;
2337 netdev_info(hw->wlandev->netdev, "Writing %d bytes to ram @0x%06x\n",
2338 len, daddr);
2340 /* How many dowmem calls? */
2341 nwrites = len / HFA384x_USB_RWMEM_MAXLEN;
2342 nwrites += len % HFA384x_USB_RWMEM_MAXLEN ? 1 : 0;
2344 /* Do blocking wmem's */
2345 for (i = 0; i < nwrites; i++) {
2346 /* make address args */
2347 curraddr = daddr + (i * HFA384x_USB_RWMEM_MAXLEN);
2348 currpage = HFA384x_ADDR_CMD_MKPAGE(curraddr);
2349 curroffset = HFA384x_ADDR_CMD_MKOFF(curraddr);
2350 currlen = len - (i * HFA384x_USB_RWMEM_MAXLEN);
2351 if (currlen > HFA384x_USB_RWMEM_MAXLEN)
2352 currlen = HFA384x_USB_RWMEM_MAXLEN;
2354 /* Do blocking ctlx */
2355 result = hfa384x_dowmem_wait(hw,
2356 currpage,
2357 curroffset,
2358 data +
2359 (i * HFA384x_USB_RWMEM_MAXLEN),
2360 currlen);
2362 if (result)
2363 break;
2365 /* TODO: We really should have a readback. */
2368 return result;
2371 /*----------------------------------------------------------------
2372 * hfa384x_drvr_readpda
2374 * Performs the sequence to read the PDA space. Note there is no
2375 * drvr_writepda() function. Writing a PDA is
2376 * generally implemented by a calling component via calls to
2377 * cmd_download and writing to the flash download buffer via the
2378 * aux regs.
2380 * Arguments:
2381 * hw device structure
2382 * buf buffer to store PDA in
2383 * len buffer length
2385 * Returns:
2386 * 0 success
2387 * >0 f/w reported error - f/w status code
2388 * <0 driver reported error
2389 * -ETIMEDOUT timeout waiting for the cmd regs to become
2390 * available, or waiting for the control reg
2391 * to indicate the Aux port is enabled.
2392 * -ENODATA the buffer does NOT contain a valid PDA.
2393 * Either the card PDA is bad, or the auxdata
2394 * reads are giving us garbage.
2397 * Side effects:
2399 * Call context:
2400 * process or non-card interrupt.
2401 ----------------------------------------------------------------*/
2402 int hfa384x_drvr_readpda(hfa384x_t *hw, void *buf, unsigned int len)
2404 int result = 0;
2405 u16 *pda = buf;
2406 int pdaok = 0;
2407 int morepdrs = 1;
2408 int currpdr = 0; /* word offset of the current pdr */
2409 size_t i;
2410 u16 pdrlen; /* pdr length in bytes, host order */
2411 u16 pdrcode; /* pdr code, host order */
2412 u16 currpage;
2413 u16 curroffset;
2414 struct pdaloc {
2415 u32 cardaddr;
2416 u16 auxctl;
2417 } pdaloc[] = {
2419 HFA3842_PDA_BASE, 0}, {
2420 HFA3841_PDA_BASE, 0}, {
2421 HFA3841_PDA_BOGUS_BASE, 0}
2424 /* Read the pda from each known address. */
2425 for (i = 0; i < ARRAY_SIZE(pdaloc); i++) {
2426 /* Make address */
2427 currpage = HFA384x_ADDR_CMD_MKPAGE(pdaloc[i].cardaddr);
2428 curroffset = HFA384x_ADDR_CMD_MKOFF(pdaloc[i].cardaddr);
2430 /* units of bytes */
2431 result = hfa384x_dormem_wait(hw, currpage, curroffset, buf,
2432 len);
2434 if (result) {
2435 netdev_warn(hw->wlandev->netdev,
2436 "Read from index %zd failed, continuing\n",
2438 continue;
2441 /* Test for garbage */
2442 pdaok = 1; /* initially assume good */
2443 morepdrs = 1;
2444 while (pdaok && morepdrs) {
2445 pdrlen = le16_to_cpu(pda[currpdr]) * 2;
2446 pdrcode = le16_to_cpu(pda[currpdr + 1]);
2447 /* Test the record length */
2448 if (pdrlen > HFA384x_PDR_LEN_MAX || pdrlen == 0) {
2449 netdev_err(hw->wlandev->netdev,
2450 "pdrlen invalid=%d\n", pdrlen);
2451 pdaok = 0;
2452 break;
2454 /* Test the code */
2455 if (!hfa384x_isgood_pdrcode(pdrcode)) {
2456 netdev_err(hw->wlandev->netdev, "pdrcode invalid=%d\n",
2457 pdrcode);
2458 pdaok = 0;
2459 break;
2461 /* Test for completion */
2462 if (pdrcode == HFA384x_PDR_END_OF_PDA)
2463 morepdrs = 0;
2465 /* Move to the next pdr (if necessary) */
2466 if (morepdrs) {
2467 /* note the access to pda[], need words here */
2468 currpdr += le16_to_cpu(pda[currpdr]) + 1;
2471 if (pdaok) {
2472 netdev_info(hw->wlandev->netdev,
2473 "PDA Read from 0x%08x in %s space.\n",
2474 pdaloc[i].cardaddr,
2475 pdaloc[i].auxctl == 0 ? "EXTDS" :
2476 pdaloc[i].auxctl == 1 ? "NV" :
2477 pdaloc[i].auxctl == 2 ? "PHY" :
2478 pdaloc[i].auxctl == 3 ? "ICSRAM" :
2479 "<bogus auxctl>");
2480 break;
2483 result = pdaok ? 0 : -ENODATA;
2485 if (result)
2486 pr_debug("Failure: pda is not okay\n");
2488 return result;
2491 /*----------------------------------------------------------------
2492 * hfa384x_drvr_setconfig
2494 * Performs the sequence necessary to write a config/info item.
2496 * Arguments:
2497 * hw device structure
2498 * rid config/info record id (in host order)
2499 * buf host side record buffer
2500 * len buffer length (in bytes)
2502 * Returns:
2503 * 0 success
2504 * >0 f/w reported error - f/w status code
2505 * <0 driver reported error
2507 * Side effects:
2509 * Call context:
2510 * process
2511 ----------------------------------------------------------------*/
2512 int hfa384x_drvr_setconfig(hfa384x_t *hw, u16 rid, void *buf, u16 len)
2514 return hfa384x_dowrid_wait(hw, rid, buf, len);
2517 /*----------------------------------------------------------------
2518 * hfa384x_drvr_start
2520 * Issues the MAC initialize command, sets up some data structures,
2521 * and enables the interrupts. After this function completes, the
2522 * low-level stuff should be ready for any/all commands.
2524 * Arguments:
2525 * hw device structure
2526 * Returns:
2527 * 0 success
2528 * >0 f/w reported error - f/w status code
2529 * <0 driver reported error
2531 * Side effects:
2533 * Call context:
2534 * process
2535 ----------------------------------------------------------------*/
2537 int hfa384x_drvr_start(hfa384x_t *hw)
2539 int result, result1, result2;
2540 u16 status;
2542 might_sleep();
2544 /* Clear endpoint stalls - but only do this if the endpoint
2545 * is showing a stall status. Some prism2 cards seem to behave
2546 * badly if a clear_halt is called when the endpoint is already
2547 * ok
2549 result =
2550 usb_get_status(hw->usb, USB_RECIP_ENDPOINT, hw->endp_in, &status);
2551 if (result < 0) {
2552 netdev_err(hw->wlandev->netdev, "Cannot get bulk in endpoint status.\n");
2553 goto done;
2555 if ((status == 1) && usb_clear_halt(hw->usb, hw->endp_in))
2556 netdev_err(hw->wlandev->netdev, "Failed to reset bulk in endpoint.\n");
2558 result =
2559 usb_get_status(hw->usb, USB_RECIP_ENDPOINT, hw->endp_out, &status);
2560 if (result < 0) {
2561 netdev_err(hw->wlandev->netdev, "Cannot get bulk out endpoint status.\n");
2562 goto done;
2564 if ((status == 1) && usb_clear_halt(hw->usb, hw->endp_out))
2565 netdev_err(hw->wlandev->netdev, "Failed to reset bulk out endpoint.\n");
2567 /* Synchronous unlink, in case we're trying to restart the driver */
2568 usb_kill_urb(&hw->rx_urb);
2570 /* Post the IN urb */
2571 result = submit_rx_urb(hw, GFP_KERNEL);
2572 if (result != 0) {
2573 netdev_err(hw->wlandev->netdev,
2574 "Fatal, failed to submit RX URB, result=%d\n",
2575 result);
2576 goto done;
2579 /* Call initialize twice, with a 1 second sleep in between.
2580 * This is a nasty work-around since many prism2 cards seem to
2581 * need time to settle after an init from cold. The second
2582 * call to initialize in theory is not necessary - but we call
2583 * it anyway as a double insurance policy:
2584 * 1) If the first init should fail, the second may well succeed
2585 * and the card can still be used
2586 * 2) It helps ensures all is well with the card after the first
2587 * init and settle time.
2589 result1 = hfa384x_cmd_initialize(hw);
2590 msleep(1000);
2591 result = hfa384x_cmd_initialize(hw);
2592 result2 = result;
2593 if (result1 != 0) {
2594 if (result2 != 0) {
2595 netdev_err(hw->wlandev->netdev,
2596 "cmd_initialize() failed on two attempts, results %d and %d\n",
2597 result1, result2);
2598 usb_kill_urb(&hw->rx_urb);
2599 goto done;
2600 } else {
2601 pr_debug("First cmd_initialize() failed (result %d),\n",
2602 result1);
2603 pr_debug("but second attempt succeeded. All should be ok\n");
2605 } else if (result2 != 0) {
2606 netdev_warn(hw->wlandev->netdev, "First cmd_initialize() succeeded, but second attempt failed (result=%d)\n",
2607 result2);
2608 netdev_warn(hw->wlandev->netdev,
2609 "Most likely the card will be functional\n");
2610 goto done;
2613 hw->state = HFA384x_STATE_RUNNING;
2615 done:
2616 return result;
2619 /*----------------------------------------------------------------
2620 * hfa384x_drvr_stop
2622 * Shuts down the MAC to the point where it is safe to unload the
2623 * driver. Any subsystem that may be holding a data or function
2624 * ptr into the driver must be cleared/deinitialized.
2626 * Arguments:
2627 * hw device structure
2628 * Returns:
2629 * 0 success
2630 * >0 f/w reported error - f/w status code
2631 * <0 driver reported error
2633 * Side effects:
2635 * Call context:
2636 * process
2637 ----------------------------------------------------------------*/
2638 int hfa384x_drvr_stop(hfa384x_t *hw)
2640 int i;
2642 might_sleep();
2644 /* There's no need for spinlocks here. The USB "disconnect"
2645 * function sets this "removed" flag and then calls us.
2647 if (!hw->wlandev->hwremoved) {
2648 /* Call initialize to leave the MAC in its 'reset' state */
2649 hfa384x_cmd_initialize(hw);
2651 /* Cancel the rxurb */
2652 usb_kill_urb(&hw->rx_urb);
2655 hw->link_status = HFA384x_LINK_NOTCONNECTED;
2656 hw->state = HFA384x_STATE_INIT;
2658 del_timer_sync(&hw->commsqual_timer);
2660 /* Clear all the port status */
2661 for (i = 0; i < HFA384x_NUMPORTS_MAX; i++)
2662 hw->port_enabled[i] = 0;
2664 return 0;
2667 /*----------------------------------------------------------------
2668 * hfa384x_drvr_txframe
2670 * Takes a frame from prism2sta and queues it for transmission.
2672 * Arguments:
2673 * hw device structure
2674 * skb packet buffer struct. Contains an 802.11
2675 * data frame.
2676 * p80211_hdr points to the 802.11 header for the packet.
2677 * Returns:
2678 * 0 Success and more buffs available
2679 * 1 Success but no more buffs
2680 * 2 Allocation failure
2681 * 4 Buffer full or queue busy
2683 * Side effects:
2685 * Call context:
2686 * interrupt
2687 ----------------------------------------------------------------*/
2688 int hfa384x_drvr_txframe(hfa384x_t *hw, struct sk_buff *skb,
2689 union p80211_hdr *p80211_hdr,
2690 struct p80211_metawep *p80211_wep)
2692 int usbpktlen = sizeof(hfa384x_tx_frame_t);
2693 int result;
2694 int ret;
2695 char *ptr;
2697 if (hw->tx_urb.status == -EINPROGRESS) {
2698 netdev_warn(hw->wlandev->netdev, "TX URB already in use\n");
2699 result = 3;
2700 goto exit;
2703 /* Build Tx frame structure */
2704 /* Set up the control field */
2705 memset(&hw->txbuff.txfrm.desc, 0, sizeof(hw->txbuff.txfrm.desc));
2707 /* Setup the usb type field */
2708 hw->txbuff.type = cpu_to_le16(HFA384x_USB_TXFRM);
2710 /* Set up the sw_support field to identify this frame */
2711 hw->txbuff.txfrm.desc.sw_support = 0x0123;
2713 /* Tx complete and Tx exception disable per dleach. Might be causing
2714 * buf depletion
2716 /* #define DOEXC SLP -- doboth breaks horribly under load, doexc less so. */
2717 #if defined(DOBOTH)
2718 hw->txbuff.txfrm.desc.tx_control =
2719 HFA384x_TX_MACPORT_SET(0) | HFA384x_TX_STRUCTYPE_SET(1) |
2720 HFA384x_TX_TXEX_SET(1) | HFA384x_TX_TXOK_SET(1);
2721 #elif defined(DOEXC)
2722 hw->txbuff.txfrm.desc.tx_control =
2723 HFA384x_TX_MACPORT_SET(0) | HFA384x_TX_STRUCTYPE_SET(1) |
2724 HFA384x_TX_TXEX_SET(1) | HFA384x_TX_TXOK_SET(0);
2725 #else
2726 hw->txbuff.txfrm.desc.tx_control =
2727 HFA384x_TX_MACPORT_SET(0) | HFA384x_TX_STRUCTYPE_SET(1) |
2728 HFA384x_TX_TXEX_SET(0) | HFA384x_TX_TXOK_SET(0);
2729 #endif
2730 hw->txbuff.txfrm.desc.tx_control =
2731 cpu_to_le16(hw->txbuff.txfrm.desc.tx_control);
2733 /* copy the header over to the txdesc */
2734 memcpy(&(hw->txbuff.txfrm.desc.frame_control), p80211_hdr,
2735 sizeof(union p80211_hdr));
2737 /* if we're using host WEP, increase size by IV+ICV */
2738 if (p80211_wep->data) {
2739 hw->txbuff.txfrm.desc.data_len = cpu_to_le16(skb->len + 8);
2740 usbpktlen += 8;
2741 } else {
2742 hw->txbuff.txfrm.desc.data_len = cpu_to_le16(skb->len);
2745 usbpktlen += skb->len;
2747 /* copy over the WEP IV if we are using host WEP */
2748 ptr = hw->txbuff.txfrm.data;
2749 if (p80211_wep->data) {
2750 memcpy(ptr, p80211_wep->iv, sizeof(p80211_wep->iv));
2751 ptr += sizeof(p80211_wep->iv);
2752 memcpy(ptr, p80211_wep->data, skb->len);
2753 } else {
2754 memcpy(ptr, skb->data, skb->len);
2756 /* copy over the packet data */
2757 ptr += skb->len;
2759 /* copy over the WEP ICV if we are using host WEP */
2760 if (p80211_wep->data)
2761 memcpy(ptr, p80211_wep->icv, sizeof(p80211_wep->icv));
2763 /* Send the USB packet */
2764 usb_fill_bulk_urb(&(hw->tx_urb), hw->usb,
2765 hw->endp_out,
2766 &(hw->txbuff), ROUNDUP64(usbpktlen),
2767 hfa384x_usbout_callback, hw->wlandev);
2768 hw->tx_urb.transfer_flags |= USB_QUEUE_BULK;
2770 result = 1;
2771 ret = submit_tx_urb(hw, &hw->tx_urb, GFP_ATOMIC);
2772 if (ret != 0) {
2773 netdev_err(hw->wlandev->netdev,
2774 "submit_tx_urb() failed, error=%d\n", ret);
2775 result = 3;
2778 exit:
2779 return result;
2782 void hfa384x_tx_timeout(wlandevice_t *wlandev)
2784 hfa384x_t *hw = wlandev->priv;
2785 unsigned long flags;
2787 spin_lock_irqsave(&hw->ctlxq.lock, flags);
2789 if (!hw->wlandev->hwremoved) {
2790 int sched;
2792 sched = !test_and_set_bit(WORK_TX_HALT, &hw->usb_flags);
2793 sched |= !test_and_set_bit(WORK_RX_HALT, &hw->usb_flags);
2794 if (sched)
2795 schedule_work(&hw->usb_work);
2798 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
2801 /*----------------------------------------------------------------
2802 * hfa384x_usbctlx_reaper_task
2804 * Tasklet to delete dead CTLX objects
2806 * Arguments:
2807 * data ptr to a hfa384x_t
2809 * Returns:
2811 * Call context:
2812 * Interrupt
2813 ----------------------------------------------------------------*/
2814 static void hfa384x_usbctlx_reaper_task(unsigned long data)
2816 hfa384x_t *hw = (hfa384x_t *)data;
2817 struct list_head *entry;
2818 struct list_head *temp;
2819 unsigned long flags;
2821 spin_lock_irqsave(&hw->ctlxq.lock, flags);
2823 /* This list is guaranteed to be empty if someone
2824 * has unplugged the adapter.
2826 list_for_each_safe(entry, temp, &hw->ctlxq.reapable) {
2827 hfa384x_usbctlx_t *ctlx;
2829 ctlx = list_entry(entry, hfa384x_usbctlx_t, list);
2830 list_del(&ctlx->list);
2831 kfree(ctlx);
2834 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
2837 /*----------------------------------------------------------------
2838 * hfa384x_usbctlx_completion_task
2840 * Tasklet to call completion handlers for returned CTLXs
2842 * Arguments:
2843 * data ptr to hfa384x_t
2845 * Returns:
2846 * Nothing
2848 * Call context:
2849 * Interrupt
2850 ----------------------------------------------------------------*/
2851 static void hfa384x_usbctlx_completion_task(unsigned long data)
2853 hfa384x_t *hw = (hfa384x_t *)data;
2854 struct list_head *entry;
2855 struct list_head *temp;
2856 unsigned long flags;
2858 int reap = 0;
2860 spin_lock_irqsave(&hw->ctlxq.lock, flags);
2862 /* This list is guaranteed to be empty if someone
2863 * has unplugged the adapter ...
2865 list_for_each_safe(entry, temp, &hw->ctlxq.completing) {
2866 hfa384x_usbctlx_t *ctlx;
2868 ctlx = list_entry(entry, hfa384x_usbctlx_t, list);
2870 /* Call the completion function that this
2871 * command was assigned, assuming it has one.
2873 if (ctlx->cmdcb != NULL) {
2874 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
2875 ctlx->cmdcb(hw, ctlx);
2876 spin_lock_irqsave(&hw->ctlxq.lock, flags);
2878 /* Make sure we don't try and complete
2879 * this CTLX more than once!
2881 ctlx->cmdcb = NULL;
2883 /* Did someone yank the adapter out
2884 * while our list was (briefly) unlocked?
2886 if (hw->wlandev->hwremoved) {
2887 reap = 0;
2888 break;
2893 * "Reapable" CTLXs are ones which don't have any
2894 * threads waiting for them to die. Hence they must
2895 * be delivered to The Reaper!
2897 if (ctlx->reapable) {
2898 /* Move the CTLX off the "completing" list (hopefully)
2899 * on to the "reapable" list where the reaper task
2900 * can find it. And "reapable" means that this CTLX
2901 * isn't sitting on a wait-queue somewhere.
2903 list_move_tail(&ctlx->list, &hw->ctlxq.reapable);
2904 reap = 1;
2907 complete(&ctlx->done);
2909 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
2911 if (reap)
2912 tasklet_schedule(&hw->reaper_bh);
2915 /*----------------------------------------------------------------
2916 * unlocked_usbctlx_cancel_async
2918 * Mark the CTLX dead asynchronously, and ensure that the
2919 * next command on the queue is run afterwards.
2921 * Arguments:
2922 * hw ptr to the hfa384x_t structure
2923 * ctlx ptr to a CTLX structure
2925 * Returns:
2926 * 0 the CTLX's URB is inactive
2927 * -EINPROGRESS the URB is currently being unlinked
2929 * Call context:
2930 * Either process or interrupt, but presumably interrupt
2931 ----------------------------------------------------------------*/
2932 static int unlocked_usbctlx_cancel_async(hfa384x_t *hw,
2933 hfa384x_usbctlx_t *ctlx)
2935 int ret;
2938 * Try to delete the URB containing our request packet.
2939 * If we succeed, then its completion handler will be
2940 * called with a status of -ECONNRESET.
2942 hw->ctlx_urb.transfer_flags |= URB_ASYNC_UNLINK;
2943 ret = usb_unlink_urb(&hw->ctlx_urb);
2945 if (ret != -EINPROGRESS) {
2947 * The OUT URB had either already completed
2948 * or was still in the pending queue, so the
2949 * URB's completion function will not be called.
2950 * We will have to complete the CTLX ourselves.
2952 ctlx->state = CTLX_REQ_FAILED;
2953 unlocked_usbctlx_complete(hw, ctlx);
2954 ret = 0;
2957 return ret;
2960 /*----------------------------------------------------------------
2961 * unlocked_usbctlx_complete
2963 * A CTLX has completed. It may have been successful, it may not
2964 * have been. At this point, the CTLX should be quiescent. The URBs
2965 * aren't active and the timers should have been stopped.
2967 * The CTLX is migrated to the "completing" queue, and the completing
2968 * tasklet is scheduled.
2970 * Arguments:
2971 * hw ptr to a hfa384x_t structure
2972 * ctlx ptr to a ctlx structure
2974 * Returns:
2975 * nothing
2977 * Side effects:
2979 * Call context:
2980 * Either, assume interrupt
2981 ----------------------------------------------------------------*/
2982 static void unlocked_usbctlx_complete(hfa384x_t *hw, hfa384x_usbctlx_t *ctlx)
2984 /* Timers have been stopped, and ctlx should be in
2985 * a terminal state. Retire it from the "active"
2986 * queue.
2988 list_move_tail(&ctlx->list, &hw->ctlxq.completing);
2989 tasklet_schedule(&hw->completion_bh);
2991 switch (ctlx->state) {
2992 case CTLX_COMPLETE:
2993 case CTLX_REQ_FAILED:
2994 /* This are the correct terminating states. */
2995 break;
2997 default:
2998 netdev_err(hw->wlandev->netdev, "CTLX[%d] not in a terminating state(%s)\n",
2999 le16_to_cpu(ctlx->outbuf.type),
3000 ctlxstr(ctlx->state));
3001 break;
3002 } /* switch */
3005 /*----------------------------------------------------------------
3006 * hfa384x_usbctlxq_run
3008 * Checks to see if the head item is running. If not, starts it.
3010 * Arguments:
3011 * hw ptr to hfa384x_t
3013 * Returns:
3014 * nothing
3016 * Side effects:
3018 * Call context:
3019 * any
3020 ----------------------------------------------------------------*/
3021 static void hfa384x_usbctlxq_run(hfa384x_t *hw)
3023 unsigned long flags;
3025 /* acquire lock */
3026 spin_lock_irqsave(&hw->ctlxq.lock, flags);
3028 /* Only one active CTLX at any one time, because there's no
3029 * other (reliable) way to match the response URB to the
3030 * correct CTLX.
3032 * Don't touch any of these CTLXs if the hardware
3033 * has been removed or the USB subsystem is stalled.
3035 if (!list_empty(&hw->ctlxq.active) ||
3036 test_bit(WORK_TX_HALT, &hw->usb_flags) || hw->wlandev->hwremoved)
3037 goto unlock;
3039 while (!list_empty(&hw->ctlxq.pending)) {
3040 hfa384x_usbctlx_t *head;
3041 int result;
3043 /* This is the first pending command */
3044 head = list_entry(hw->ctlxq.pending.next,
3045 hfa384x_usbctlx_t, list);
3047 /* We need to split this off to avoid a race condition */
3048 list_move_tail(&head->list, &hw->ctlxq.active);
3050 /* Fill the out packet */
3051 usb_fill_bulk_urb(&(hw->ctlx_urb), hw->usb,
3052 hw->endp_out,
3053 &(head->outbuf), ROUNDUP64(head->outbufsize),
3054 hfa384x_ctlxout_callback, hw);
3055 hw->ctlx_urb.transfer_flags |= USB_QUEUE_BULK;
3057 /* Now submit the URB and update the CTLX's state */
3058 result = SUBMIT_URB(&hw->ctlx_urb, GFP_ATOMIC);
3059 if (result == 0) {
3060 /* This CTLX is now running on the active queue */
3061 head->state = CTLX_REQ_SUBMITTED;
3063 /* Start the OUT wait timer */
3064 hw->req_timer_done = 0;
3065 hw->reqtimer.expires = jiffies + HZ;
3066 add_timer(&hw->reqtimer);
3068 /* Start the IN wait timer */
3069 hw->resp_timer_done = 0;
3070 hw->resptimer.expires = jiffies + 2 * HZ;
3071 add_timer(&hw->resptimer);
3073 break;
3076 if (result == -EPIPE) {
3077 /* The OUT pipe needs resetting, so put
3078 * this CTLX back in the "pending" queue
3079 * and schedule a reset ...
3081 netdev_warn(hw->wlandev->netdev,
3082 "%s tx pipe stalled: requesting reset\n",
3083 hw->wlandev->netdev->name);
3084 list_move(&head->list, &hw->ctlxq.pending);
3085 set_bit(WORK_TX_HALT, &hw->usb_flags);
3086 schedule_work(&hw->usb_work);
3087 break;
3090 if (result == -ESHUTDOWN) {
3091 netdev_warn(hw->wlandev->netdev, "%s urb shutdown!\n",
3092 hw->wlandev->netdev->name);
3093 break;
3096 netdev_err(hw->wlandev->netdev, "Failed to submit CTLX[%d]: error=%d\n",
3097 le16_to_cpu(head->outbuf.type), result);
3098 unlocked_usbctlx_complete(hw, head);
3099 } /* while */
3101 unlock:
3102 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3105 /*----------------------------------------------------------------
3106 * hfa384x_usbin_callback
3108 * Callback for URBs on the BULKIN endpoint.
3110 * Arguments:
3111 * urb ptr to the completed urb
3113 * Returns:
3114 * nothing
3116 * Side effects:
3118 * Call context:
3119 * interrupt
3120 ----------------------------------------------------------------*/
3121 static void hfa384x_usbin_callback(struct urb *urb)
3123 wlandevice_t *wlandev = urb->context;
3124 hfa384x_t *hw;
3125 hfa384x_usbin_t *usbin = (hfa384x_usbin_t *)urb->transfer_buffer;
3126 struct sk_buff *skb = NULL;
3127 int result;
3128 int urb_status;
3129 u16 type;
3131 enum USBIN_ACTION {
3132 HANDLE,
3133 RESUBMIT,
3134 ABORT
3135 } action;
3137 if (!wlandev || !wlandev->netdev || wlandev->hwremoved)
3138 goto exit;
3140 hw = wlandev->priv;
3141 if (!hw)
3142 goto exit;
3144 skb = hw->rx_urb_skb;
3145 BUG_ON(!skb || (skb->data != urb->transfer_buffer));
3147 hw->rx_urb_skb = NULL;
3149 /* Check for error conditions within the URB */
3150 switch (urb->status) {
3151 case 0:
3152 action = HANDLE;
3154 /* Check for short packet */
3155 if (urb->actual_length == 0) {
3156 wlandev->netdev->stats.rx_errors++;
3157 wlandev->netdev->stats.rx_length_errors++;
3158 action = RESUBMIT;
3160 break;
3162 case -EPIPE:
3163 netdev_warn(hw->wlandev->netdev, "%s rx pipe stalled: requesting reset\n",
3164 wlandev->netdev->name);
3165 if (!test_and_set_bit(WORK_RX_HALT, &hw->usb_flags))
3166 schedule_work(&hw->usb_work);
3167 wlandev->netdev->stats.rx_errors++;
3168 action = ABORT;
3169 break;
3171 case -EILSEQ:
3172 case -ETIMEDOUT:
3173 case -EPROTO:
3174 if (!test_and_set_bit(THROTTLE_RX, &hw->usb_flags) &&
3175 !timer_pending(&hw->throttle)) {
3176 mod_timer(&hw->throttle, jiffies + THROTTLE_JIFFIES);
3178 wlandev->netdev->stats.rx_errors++;
3179 action = ABORT;
3180 break;
3182 case -EOVERFLOW:
3183 wlandev->netdev->stats.rx_over_errors++;
3184 action = RESUBMIT;
3185 break;
3187 case -ENODEV:
3188 case -ESHUTDOWN:
3189 pr_debug("status=%d, device removed.\n", urb->status);
3190 action = ABORT;
3191 break;
3193 case -ENOENT:
3194 case -ECONNRESET:
3195 pr_debug("status=%d, urb explicitly unlinked.\n", urb->status);
3196 action = ABORT;
3197 break;
3199 default:
3200 pr_debug("urb status=%d, transfer flags=0x%x\n",
3201 urb->status, urb->transfer_flags);
3202 wlandev->netdev->stats.rx_errors++;
3203 action = RESUBMIT;
3204 break;
3207 urb_status = urb->status;
3209 if (action != ABORT) {
3210 /* Repost the RX URB */
3211 result = submit_rx_urb(hw, GFP_ATOMIC);
3213 if (result != 0) {
3214 netdev_err(hw->wlandev->netdev,
3215 "Fatal, failed to resubmit rx_urb. error=%d\n",
3216 result);
3220 /* Handle any USB-IN packet */
3221 /* Note: the check of the sw_support field, the type field doesn't
3222 * have bit 12 set like the docs suggest.
3224 type = le16_to_cpu(usbin->type);
3225 if (HFA384x_USB_ISRXFRM(type)) {
3226 if (action == HANDLE) {
3227 if (usbin->txfrm.desc.sw_support == 0x0123) {
3228 hfa384x_usbin_txcompl(wlandev, usbin);
3229 } else {
3230 skb_put(skb, sizeof(*usbin));
3231 hfa384x_usbin_rx(wlandev, skb);
3232 skb = NULL;
3235 goto exit;
3237 if (HFA384x_USB_ISTXFRM(type)) {
3238 if (action == HANDLE)
3239 hfa384x_usbin_txcompl(wlandev, usbin);
3240 goto exit;
3242 switch (type) {
3243 case HFA384x_USB_INFOFRM:
3244 if (action == ABORT)
3245 goto exit;
3246 if (action == HANDLE)
3247 hfa384x_usbin_info(wlandev, usbin);
3248 break;
3250 case HFA384x_USB_CMDRESP:
3251 case HFA384x_USB_WRIDRESP:
3252 case HFA384x_USB_RRIDRESP:
3253 case HFA384x_USB_WMEMRESP:
3254 case HFA384x_USB_RMEMRESP:
3255 /* ALWAYS, ALWAYS, ALWAYS handle this CTLX!!!! */
3256 hfa384x_usbin_ctlx(hw, usbin, urb_status);
3257 break;
3259 case HFA384x_USB_BUFAVAIL:
3260 pr_debug("Received BUFAVAIL packet, frmlen=%d\n",
3261 usbin->bufavail.frmlen);
3262 break;
3264 case HFA384x_USB_ERROR:
3265 pr_debug("Received USB_ERROR packet, errortype=%d\n",
3266 usbin->usberror.errortype);
3267 break;
3269 default:
3270 pr_debug("Unrecognized USBIN packet, type=%x, status=%d\n",
3271 usbin->type, urb_status);
3272 break;
3273 } /* switch */
3275 exit:
3277 if (skb)
3278 dev_kfree_skb(skb);
3281 /*----------------------------------------------------------------
3282 * hfa384x_usbin_ctlx
3284 * We've received a URB containing a Prism2 "response" message.
3285 * This message needs to be matched up with a CTLX on the active
3286 * queue and our state updated accordingly.
3288 * Arguments:
3289 * hw ptr to hfa384x_t
3290 * usbin ptr to USB IN packet
3291 * urb_status status of this Bulk-In URB
3293 * Returns:
3294 * nothing
3296 * Side effects:
3298 * Call context:
3299 * interrupt
3300 ----------------------------------------------------------------*/
3301 static void hfa384x_usbin_ctlx(hfa384x_t *hw, hfa384x_usbin_t *usbin,
3302 int urb_status)
3304 hfa384x_usbctlx_t *ctlx;
3305 int run_queue = 0;
3306 unsigned long flags;
3308 retry:
3309 spin_lock_irqsave(&hw->ctlxq.lock, flags);
3311 /* There can be only one CTLX on the active queue
3312 * at any one time, and this is the CTLX that the
3313 * timers are waiting for.
3315 if (list_empty(&hw->ctlxq.active))
3316 goto unlock;
3318 /* Remove the "response timeout". It's possible that
3319 * we are already too late, and that the timeout is
3320 * already running. And that's just too bad for us,
3321 * because we could lose our CTLX from the active
3322 * queue here ...
3324 if (del_timer(&hw->resptimer) == 0) {
3325 if (hw->resp_timer_done == 0) {
3326 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3327 goto retry;
3329 } else {
3330 hw->resp_timer_done = 1;
3333 ctlx = get_active_ctlx(hw);
3335 if (urb_status != 0) {
3337 * Bad CTLX, so get rid of it. But we only
3338 * remove it from the active queue if we're no
3339 * longer expecting the OUT URB to complete.
3341 if (unlocked_usbctlx_cancel_async(hw, ctlx) == 0)
3342 run_queue = 1;
3343 } else {
3344 const __le16 intype = (usbin->type & ~cpu_to_le16(0x8000));
3347 * Check that our message is what we're expecting ...
3349 if (ctlx->outbuf.type != intype) {
3350 netdev_warn(hw->wlandev->netdev,
3351 "Expected IN[%d], received IN[%d] - ignored.\n",
3352 le16_to_cpu(ctlx->outbuf.type),
3353 le16_to_cpu(intype));
3354 goto unlock;
3357 /* This URB has succeeded, so grab the data ... */
3358 memcpy(&ctlx->inbuf, usbin, sizeof(ctlx->inbuf));
3360 switch (ctlx->state) {
3361 case CTLX_REQ_SUBMITTED:
3363 * We have received our response URB before
3364 * our request has been acknowledged. Odd,
3365 * but our OUT URB is still alive...
3367 pr_debug("Causality violation: please reboot Universe\n");
3368 ctlx->state = CTLX_RESP_COMPLETE;
3369 break;
3371 case CTLX_REQ_COMPLETE:
3373 * This is the usual path: our request
3374 * has already been acknowledged, and
3375 * now we have received the reply too.
3377 ctlx->state = CTLX_COMPLETE;
3378 unlocked_usbctlx_complete(hw, ctlx);
3379 run_queue = 1;
3380 break;
3382 default:
3384 * Throw this CTLX away ...
3386 netdev_err(hw->wlandev->netdev,
3387 "Matched IN URB, CTLX[%d] in invalid state(%s). Discarded.\n",
3388 le16_to_cpu(ctlx->outbuf.type),
3389 ctlxstr(ctlx->state));
3390 if (unlocked_usbctlx_cancel_async(hw, ctlx) == 0)
3391 run_queue = 1;
3392 break;
3393 } /* switch */
3396 unlock:
3397 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3399 if (run_queue)
3400 hfa384x_usbctlxq_run(hw);
3403 /*----------------------------------------------------------------
3404 * hfa384x_usbin_txcompl
3406 * At this point we have the results of a previous transmit.
3408 * Arguments:
3409 * wlandev wlan device
3410 * usbin ptr to the usb transfer buffer
3412 * Returns:
3413 * nothing
3415 * Side effects:
3417 * Call context:
3418 * interrupt
3419 ----------------------------------------------------------------*/
3420 static void hfa384x_usbin_txcompl(wlandevice_t *wlandev,
3421 hfa384x_usbin_t *usbin)
3423 u16 status;
3425 status = le16_to_cpu(usbin->type); /* yeah I know it says type... */
3427 /* Was there an error? */
3428 if (HFA384x_TXSTATUS_ISERROR(status))
3429 prism2sta_ev_txexc(wlandev, status);
3430 else
3431 prism2sta_ev_tx(wlandev, status);
3434 /*----------------------------------------------------------------
3435 * hfa384x_usbin_rx
3437 * At this point we have a successful received a rx frame packet.
3439 * Arguments:
3440 * wlandev wlan device
3441 * usbin ptr to the usb transfer buffer
3443 * Returns:
3444 * nothing
3446 * Side effects:
3448 * Call context:
3449 * interrupt
3450 ----------------------------------------------------------------*/
3451 static void hfa384x_usbin_rx(wlandevice_t *wlandev, struct sk_buff *skb)
3453 hfa384x_usbin_t *usbin = (hfa384x_usbin_t *)skb->data;
3454 hfa384x_t *hw = wlandev->priv;
3455 int hdrlen;
3456 struct p80211_rxmeta *rxmeta;
3457 u16 data_len;
3458 u16 fc;
3460 /* Byte order convert once up front. */
3461 usbin->rxfrm.desc.status = le16_to_cpu(usbin->rxfrm.desc.status);
3462 usbin->rxfrm.desc.time = le32_to_cpu(usbin->rxfrm.desc.time);
3464 /* Now handle frame based on port# */
3465 switch (HFA384x_RXSTATUS_MACPORT_GET(usbin->rxfrm.desc.status)) {
3466 case 0:
3467 fc = le16_to_cpu(usbin->rxfrm.desc.frame_control);
3469 /* If exclude and we receive an unencrypted, drop it */
3470 if ((wlandev->hostwep & HOSTWEP_EXCLUDEUNENCRYPTED) &&
3471 !WLAN_GET_FC_ISWEP(fc)) {
3472 break;
3475 data_len = le16_to_cpu(usbin->rxfrm.desc.data_len);
3477 /* How much header data do we have? */
3478 hdrlen = p80211_headerlen(fc);
3480 /* Pull off the descriptor */
3481 skb_pull(skb, sizeof(hfa384x_rx_frame_t));
3483 /* Now shunt the header block up against the data block
3484 * with an "overlapping" copy
3486 memmove(skb_push(skb, hdrlen),
3487 &usbin->rxfrm.desc.frame_control, hdrlen);
3489 skb->dev = wlandev->netdev;
3490 skb->dev->last_rx = jiffies;
3492 /* And set the frame length properly */
3493 skb_trim(skb, data_len + hdrlen);
3495 /* The prism2 series does not return the CRC */
3496 memset(skb_put(skb, WLAN_CRC_LEN), 0xff, WLAN_CRC_LEN);
3498 skb_reset_mac_header(skb);
3500 /* Attach the rxmeta, set some stuff */
3501 p80211skb_rxmeta_attach(wlandev, skb);
3502 rxmeta = P80211SKB_RXMETA(skb);
3503 rxmeta->mactime = usbin->rxfrm.desc.time;
3504 rxmeta->rxrate = usbin->rxfrm.desc.rate;
3505 rxmeta->signal = usbin->rxfrm.desc.signal - hw->dbmadjust;
3506 rxmeta->noise = usbin->rxfrm.desc.silence - hw->dbmadjust;
3508 prism2sta_ev_rx(wlandev, skb);
3510 break;
3512 case 7:
3513 if (!HFA384x_RXSTATUS_ISFCSERR(usbin->rxfrm.desc.status)) {
3514 /* Copy to wlansnif skb */
3515 hfa384x_int_rxmonitor(wlandev, &usbin->rxfrm);
3516 dev_kfree_skb(skb);
3517 } else {
3518 pr_debug("Received monitor frame: FCSerr set\n");
3520 break;
3522 default:
3523 netdev_warn(hw->wlandev->netdev, "Received frame on unsupported port=%d\n",
3524 HFA384x_RXSTATUS_MACPORT_GET(
3525 usbin->rxfrm.desc.status));
3526 break;
3530 /*----------------------------------------------------------------
3531 * hfa384x_int_rxmonitor
3533 * Helper function for int_rx. Handles monitor frames.
3534 * Note that this function allocates space for the FCS and sets it
3535 * to 0xffffffff. The hfa384x doesn't give us the FCS value but the
3536 * higher layers expect it. 0xffffffff is used as a flag to indicate
3537 * the FCS is bogus.
3539 * Arguments:
3540 * wlandev wlan device structure
3541 * rxfrm rx descriptor read from card in int_rx
3543 * Returns:
3544 * nothing
3546 * Side effects:
3547 * Allocates an skb and passes it up via the PF_PACKET interface.
3548 * Call context:
3549 * interrupt
3550 ----------------------------------------------------------------*/
3551 static void hfa384x_int_rxmonitor(wlandevice_t *wlandev,
3552 hfa384x_usb_rxfrm_t *rxfrm)
3554 hfa384x_rx_frame_t *rxdesc = &(rxfrm->desc);
3555 unsigned int hdrlen = 0;
3556 unsigned int datalen = 0;
3557 unsigned int skblen = 0;
3558 u8 *datap;
3559 u16 fc;
3560 struct sk_buff *skb;
3561 hfa384x_t *hw = wlandev->priv;
3563 /* Remember the status, time, and data_len fields are in host order */
3564 /* Figure out how big the frame is */
3565 fc = le16_to_cpu(rxdesc->frame_control);
3566 hdrlen = p80211_headerlen(fc);
3567 datalen = le16_to_cpu(rxdesc->data_len);
3569 /* Allocate an ind message+framesize skb */
3570 skblen = sizeof(struct p80211_caphdr) + hdrlen + datalen + WLAN_CRC_LEN;
3572 /* sanity check the length */
3573 if (skblen >
3574 (sizeof(struct p80211_caphdr) +
3575 WLAN_HDR_A4_LEN + WLAN_DATA_MAXLEN + WLAN_CRC_LEN)) {
3576 pr_debug("overlen frm: len=%zd\n",
3577 skblen - sizeof(struct p80211_caphdr));
3580 skb = dev_alloc_skb(skblen);
3581 if (skb == NULL)
3582 return;
3584 /* only prepend the prism header if in the right mode */
3585 if ((wlandev->netdev->type == ARPHRD_IEEE80211_PRISM) &&
3586 (hw->sniffhdr != 0)) {
3587 struct p80211_caphdr *caphdr;
3588 /* The NEW header format! */
3589 datap = skb_put(skb, sizeof(struct p80211_caphdr));
3590 caphdr = (struct p80211_caphdr *)datap;
3592 caphdr->version = htonl(P80211CAPTURE_VERSION);
3593 caphdr->length = htonl(sizeof(struct p80211_caphdr));
3594 caphdr->mactime = __cpu_to_be64(rxdesc->time) * 1000;
3595 caphdr->hosttime = __cpu_to_be64(jiffies);
3596 caphdr->phytype = htonl(4); /* dss_dot11_b */
3597 caphdr->channel = htonl(hw->sniff_channel);
3598 caphdr->datarate = htonl(rxdesc->rate);
3599 caphdr->antenna = htonl(0); /* unknown */
3600 caphdr->priority = htonl(0); /* unknown */
3601 caphdr->ssi_type = htonl(3); /* rssi_raw */
3602 caphdr->ssi_signal = htonl(rxdesc->signal);
3603 caphdr->ssi_noise = htonl(rxdesc->silence);
3604 caphdr->preamble = htonl(0); /* unknown */
3605 caphdr->encoding = htonl(1); /* cck */
3608 /* Copy the 802.11 header to the skb
3609 (ctl frames may be less than a full header) */
3610 datap = skb_put(skb, hdrlen);
3611 memcpy(datap, &(rxdesc->frame_control), hdrlen);
3613 /* If any, copy the data from the card to the skb */
3614 if (datalen > 0) {
3615 datap = skb_put(skb, datalen);
3616 memcpy(datap, rxfrm->data, datalen);
3618 /* check for unencrypted stuff if WEP bit set. */
3619 if (*(datap - hdrlen + 1) & 0x40) /* wep set */
3620 if ((*(datap) == 0xaa) && (*(datap + 1) == 0xaa))
3621 /* clear wep; it's the 802.2 header! */
3622 *(datap - hdrlen + 1) &= 0xbf;
3625 if (hw->sniff_fcs) {
3626 /* Set the FCS */
3627 datap = skb_put(skb, WLAN_CRC_LEN);
3628 memset(datap, 0xff, WLAN_CRC_LEN);
3631 /* pass it back up */
3632 prism2sta_ev_rx(wlandev, skb);
3635 /*----------------------------------------------------------------
3636 * hfa384x_usbin_info
3638 * At this point we have a successful received a Prism2 info frame.
3640 * Arguments:
3641 * wlandev wlan device
3642 * usbin ptr to the usb transfer buffer
3644 * Returns:
3645 * nothing
3647 * Side effects:
3649 * Call context:
3650 * interrupt
3651 ----------------------------------------------------------------*/
3652 static void hfa384x_usbin_info(wlandevice_t *wlandev, hfa384x_usbin_t *usbin)
3654 usbin->infofrm.info.framelen =
3655 le16_to_cpu(usbin->infofrm.info.framelen);
3656 prism2sta_ev_info(wlandev, &usbin->infofrm.info);
3659 /*----------------------------------------------------------------
3660 * hfa384x_usbout_callback
3662 * Callback for URBs on the BULKOUT endpoint.
3664 * Arguments:
3665 * urb ptr to the completed urb
3667 * Returns:
3668 * nothing
3670 * Side effects:
3672 * Call context:
3673 * interrupt
3674 ----------------------------------------------------------------*/
3675 static void hfa384x_usbout_callback(struct urb *urb)
3677 wlandevice_t *wlandev = urb->context;
3678 hfa384x_usbout_t *usbout = urb->transfer_buffer;
3680 #ifdef DEBUG_USB
3681 dbprint_urb(urb);
3682 #endif
3684 if (wlandev && wlandev->netdev) {
3685 switch (urb->status) {
3686 case 0:
3687 hfa384x_usbout_tx(wlandev, usbout);
3688 break;
3690 case -EPIPE:
3692 hfa384x_t *hw = wlandev->priv;
3694 netdev_warn(hw->wlandev->netdev,
3695 "%s tx pipe stalled: requesting reset\n",
3696 wlandev->netdev->name);
3697 if (!test_and_set_bit
3698 (WORK_TX_HALT, &hw->usb_flags))
3699 schedule_work(&hw->usb_work);
3700 wlandev->netdev->stats.tx_errors++;
3701 break;
3704 case -EPROTO:
3705 case -ETIMEDOUT:
3706 case -EILSEQ:
3708 hfa384x_t *hw = wlandev->priv;
3710 if (!test_and_set_bit
3711 (THROTTLE_TX, &hw->usb_flags) &&
3712 !timer_pending(&hw->throttle)) {
3713 mod_timer(&hw->throttle,
3714 jiffies + THROTTLE_JIFFIES);
3716 wlandev->netdev->stats.tx_errors++;
3717 netif_stop_queue(wlandev->netdev);
3718 break;
3721 case -ENOENT:
3722 case -ESHUTDOWN:
3723 /* Ignorable errors */
3724 break;
3726 default:
3727 netdev_info(wlandev->netdev, "unknown urb->status=%d\n",
3728 urb->status);
3729 wlandev->netdev->stats.tx_errors++;
3730 break;
3731 } /* switch */
3735 /*----------------------------------------------------------------
3736 * hfa384x_ctlxout_callback
3738 * Callback for control data on the BULKOUT endpoint.
3740 * Arguments:
3741 * urb ptr to the completed urb
3743 * Returns:
3744 * nothing
3746 * Side effects:
3748 * Call context:
3749 * interrupt
3750 ----------------------------------------------------------------*/
3751 static void hfa384x_ctlxout_callback(struct urb *urb)
3753 hfa384x_t *hw = urb->context;
3754 int delete_resptimer = 0;
3755 int timer_ok = 1;
3756 int run_queue = 0;
3757 hfa384x_usbctlx_t *ctlx;
3758 unsigned long flags;
3760 pr_debug("urb->status=%d\n", urb->status);
3761 #ifdef DEBUG_USB
3762 dbprint_urb(urb);
3763 #endif
3764 if ((urb->status == -ESHUTDOWN) ||
3765 (urb->status == -ENODEV) || (hw == NULL))
3766 return;
3768 retry:
3769 spin_lock_irqsave(&hw->ctlxq.lock, flags);
3772 * Only one CTLX at a time on the "active" list, and
3773 * none at all if we are unplugged. However, we can
3774 * rely on the disconnect function to clean everything
3775 * up if someone unplugged the adapter.
3777 if (list_empty(&hw->ctlxq.active)) {
3778 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3779 return;
3783 * Having something on the "active" queue means
3784 * that we have timers to worry about ...
3786 if (del_timer(&hw->reqtimer) == 0) {
3787 if (hw->req_timer_done == 0) {
3789 * This timer was actually running while we
3790 * were trying to delete it. Let it terminate
3791 * gracefully instead.
3793 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3794 goto retry;
3796 } else {
3797 hw->req_timer_done = 1;
3800 ctlx = get_active_ctlx(hw);
3802 if (urb->status == 0) {
3803 /* Request portion of a CTLX is successful */
3804 switch (ctlx->state) {
3805 case CTLX_REQ_SUBMITTED:
3806 /* This OUT-ACK received before IN */
3807 ctlx->state = CTLX_REQ_COMPLETE;
3808 break;
3810 case CTLX_RESP_COMPLETE:
3811 /* IN already received before this OUT-ACK,
3812 * so this command must now be complete.
3814 ctlx->state = CTLX_COMPLETE;
3815 unlocked_usbctlx_complete(hw, ctlx);
3816 run_queue = 1;
3817 break;
3819 default:
3820 /* This is NOT a valid CTLX "success" state! */
3821 netdev_err(hw->wlandev->netdev,
3822 "Illegal CTLX[%d] success state(%s, %d) in OUT URB\n",
3823 le16_to_cpu(ctlx->outbuf.type),
3824 ctlxstr(ctlx->state), urb->status);
3825 break;
3826 } /* switch */
3827 } else {
3828 /* If the pipe has stalled then we need to reset it */
3829 if ((urb->status == -EPIPE) &&
3830 !test_and_set_bit(WORK_TX_HALT, &hw->usb_flags)) {
3831 netdev_warn(hw->wlandev->netdev,
3832 "%s tx pipe stalled: requesting reset\n",
3833 hw->wlandev->netdev->name);
3834 schedule_work(&hw->usb_work);
3837 /* If someone cancels the OUT URB then its status
3838 * should be either -ECONNRESET or -ENOENT.
3840 ctlx->state = CTLX_REQ_FAILED;
3841 unlocked_usbctlx_complete(hw, ctlx);
3842 delete_resptimer = 1;
3843 run_queue = 1;
3846 delresp:
3847 if (delete_resptimer) {
3848 timer_ok = del_timer(&hw->resptimer);
3849 if (timer_ok != 0)
3850 hw->resp_timer_done = 1;
3853 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3855 if (!timer_ok && (hw->resp_timer_done == 0)) {
3856 spin_lock_irqsave(&hw->ctlxq.lock, flags);
3857 goto delresp;
3860 if (run_queue)
3861 hfa384x_usbctlxq_run(hw);
3864 /*----------------------------------------------------------------
3865 * hfa384x_usbctlx_reqtimerfn
3867 * Timer response function for CTLX request timeouts. If this
3868 * function is called, it means that the callback for the OUT
3869 * URB containing a Prism2.x XXX_Request was never called.
3871 * Arguments:
3872 * data a ptr to the hfa384x_t
3874 * Returns:
3875 * nothing
3877 * Side effects:
3879 * Call context:
3880 * interrupt
3881 ----------------------------------------------------------------*/
3882 static void hfa384x_usbctlx_reqtimerfn(unsigned long data)
3884 hfa384x_t *hw = (hfa384x_t *)data;
3885 unsigned long flags;
3887 spin_lock_irqsave(&hw->ctlxq.lock, flags);
3889 hw->req_timer_done = 1;
3891 /* Removing the hardware automatically empties
3892 * the active list ...
3894 if (!list_empty(&hw->ctlxq.active)) {
3896 * We must ensure that our URB is removed from
3897 * the system, if it hasn't already expired.
3899 hw->ctlx_urb.transfer_flags |= URB_ASYNC_UNLINK;
3900 if (usb_unlink_urb(&hw->ctlx_urb) == -EINPROGRESS) {
3901 hfa384x_usbctlx_t *ctlx = get_active_ctlx(hw);
3903 ctlx->state = CTLX_REQ_FAILED;
3905 /* This URB was active, but has now been
3906 * cancelled. It will now have a status of
3907 * -ECONNRESET in the callback function.
3909 * We are cancelling this CTLX, so we're
3910 * not going to need to wait for a response.
3911 * The URB's callback function will check
3912 * that this timer is truly dead.
3914 if (del_timer(&hw->resptimer) != 0)
3915 hw->resp_timer_done = 1;
3919 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3922 /*----------------------------------------------------------------
3923 * hfa384x_usbctlx_resptimerfn
3925 * Timer response function for CTLX response timeouts. If this
3926 * function is called, it means that the callback for the IN
3927 * URB containing a Prism2.x XXX_Response was never called.
3929 * Arguments:
3930 * data a ptr to the hfa384x_t
3932 * Returns:
3933 * nothing
3935 * Side effects:
3937 * Call context:
3938 * interrupt
3939 ----------------------------------------------------------------*/
3940 static void hfa384x_usbctlx_resptimerfn(unsigned long data)
3942 hfa384x_t *hw = (hfa384x_t *)data;
3943 unsigned long flags;
3945 spin_lock_irqsave(&hw->ctlxq.lock, flags);
3947 hw->resp_timer_done = 1;
3949 /* The active list will be empty if the
3950 * adapter has been unplugged ...
3952 if (!list_empty(&hw->ctlxq.active)) {
3953 hfa384x_usbctlx_t *ctlx = get_active_ctlx(hw);
3955 if (unlocked_usbctlx_cancel_async(hw, ctlx) == 0) {
3956 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3957 hfa384x_usbctlxq_run(hw);
3958 return;
3961 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
3964 /*----------------------------------------------------------------
3965 * hfa384x_usb_throttlefn
3968 * Arguments:
3969 * data ptr to hw
3971 * Returns:
3972 * Nothing
3974 * Side effects:
3976 * Call context:
3977 * Interrupt
3978 ----------------------------------------------------------------*/
3979 static void hfa384x_usb_throttlefn(unsigned long data)
3981 hfa384x_t *hw = (hfa384x_t *)data;
3982 unsigned long flags;
3984 spin_lock_irqsave(&hw->ctlxq.lock, flags);
3987 * We need to check BOTH the RX and the TX throttle controls,
3988 * so we use the bitwise OR instead of the logical OR.
3990 pr_debug("flags=0x%lx\n", hw->usb_flags);
3991 if (!hw->wlandev->hwremoved &&
3992 ((test_and_clear_bit(THROTTLE_RX, &hw->usb_flags) &&
3993 !test_and_set_bit(WORK_RX_RESUME, &hw->usb_flags))
3995 (test_and_clear_bit(THROTTLE_TX, &hw->usb_flags) &&
3996 !test_and_set_bit(WORK_TX_RESUME, &hw->usb_flags))
3997 )) {
3998 schedule_work(&hw->usb_work);
4001 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
4004 /*----------------------------------------------------------------
4005 * hfa384x_usbctlx_submit
4007 * Called from the doxxx functions to submit a CTLX to the queue
4009 * Arguments:
4010 * hw ptr to the hw struct
4011 * ctlx ctlx structure to enqueue
4013 * Returns:
4014 * -ENODEV if the adapter is unplugged
4017 * Side effects:
4019 * Call context:
4020 * process or interrupt
4021 ----------------------------------------------------------------*/
4022 static int hfa384x_usbctlx_submit(hfa384x_t *hw, hfa384x_usbctlx_t *ctlx)
4024 unsigned long flags;
4026 spin_lock_irqsave(&hw->ctlxq.lock, flags);
4028 if (hw->wlandev->hwremoved) {
4029 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
4030 return -ENODEV;
4033 ctlx->state = CTLX_PENDING;
4034 list_add_tail(&ctlx->list, &hw->ctlxq.pending);
4035 spin_unlock_irqrestore(&hw->ctlxq.lock, flags);
4036 hfa384x_usbctlxq_run(hw);
4038 return 0;
4041 /*----------------------------------------------------------------
4042 * hfa384x_usbout_tx
4044 * At this point we have finished a send of a frame. Mark the URB
4045 * as available and call ev_alloc to notify higher layers we're
4046 * ready for more.
4048 * Arguments:
4049 * wlandev wlan device
4050 * usbout ptr to the usb transfer buffer
4052 * Returns:
4053 * nothing
4055 * Side effects:
4057 * Call context:
4058 * interrupt
4059 ----------------------------------------------------------------*/
4060 static void hfa384x_usbout_tx(wlandevice_t *wlandev, hfa384x_usbout_t *usbout)
4062 prism2sta_ev_alloc(wlandev);
4065 /*----------------------------------------------------------------
4066 * hfa384x_isgood_pdrcore
4068 * Quick check of PDR codes.
4070 * Arguments:
4071 * pdrcode PDR code number (host order)
4073 * Returns:
4074 * zero not good.
4075 * one is good.
4077 * Side effects:
4079 * Call context:
4080 ----------------------------------------------------------------*/
4081 static int hfa384x_isgood_pdrcode(u16 pdrcode)
4083 switch (pdrcode) {
4084 case HFA384x_PDR_END_OF_PDA:
4085 case HFA384x_PDR_PCB_PARTNUM:
4086 case HFA384x_PDR_PDAVER:
4087 case HFA384x_PDR_NIC_SERIAL:
4088 case HFA384x_PDR_MKK_MEASUREMENTS:
4089 case HFA384x_PDR_NIC_RAMSIZE:
4090 case HFA384x_PDR_MFISUPRANGE:
4091 case HFA384x_PDR_CFISUPRANGE:
4092 case HFA384x_PDR_NICID:
4093 case HFA384x_PDR_MAC_ADDRESS:
4094 case HFA384x_PDR_REGDOMAIN:
4095 case HFA384x_PDR_ALLOWED_CHANNEL:
4096 case HFA384x_PDR_DEFAULT_CHANNEL:
4097 case HFA384x_PDR_TEMPTYPE:
4098 case HFA384x_PDR_IFR_SETTING:
4099 case HFA384x_PDR_RFR_SETTING:
4100 case HFA384x_PDR_HFA3861_BASELINE:
4101 case HFA384x_PDR_HFA3861_SHADOW:
4102 case HFA384x_PDR_HFA3861_IFRF:
4103 case HFA384x_PDR_HFA3861_CHCALSP:
4104 case HFA384x_PDR_HFA3861_CHCALI:
4105 case HFA384x_PDR_3842_NIC_CONFIG:
4106 case HFA384x_PDR_USB_ID:
4107 case HFA384x_PDR_PCI_ID:
4108 case HFA384x_PDR_PCI_IFCONF:
4109 case HFA384x_PDR_PCI_PMCONF:
4110 case HFA384x_PDR_RFENRGY:
4111 case HFA384x_PDR_HFA3861_MANF_TESTSP:
4112 case HFA384x_PDR_HFA3861_MANF_TESTI:
4113 /* code is OK */
4114 return 1;
4115 default:
4116 if (pdrcode < 0x1000) {
4117 /* code is OK, but we don't know exactly what it is */
4118 pr_debug("Encountered unknown PDR#=0x%04x, assuming it's ok.\n",
4119 pdrcode);
4120 return 1;
4122 break;
4124 /* bad code */
4125 pr_debug("Encountered unknown PDR#=0x%04x, (>=0x1000), assuming it's bad.\n",
4126 pdrcode);
4127 return 0;