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