2 * Host AP (software wireless LAN access point) driver for
3 * Intersil Prism2/2.5/3.
5 * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
7 * Copyright (c) 2002-2005, Jouni Malinen <jkmaline@cc.hut.fi>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation. See README and COPYING for
15 * - there is currently no way of associating TX packets to correct wds device
16 * when TX Exc/OK event occurs, so all tx_packets and some
17 * tx_errors/tx_dropped are added to the main netdevice; using sw_support
18 * field in txdesc might be used to fix this (using Alloc event to increment
19 * tx_packets would need some further info in txfid table)
21 * Buffer Access Path (BAP) usage:
22 * Prism2 cards have two separate BAPs for accessing the card memory. These
23 * should allow concurrent access to two different frames and the driver
24 * previously used BAP0 for sending data and BAP1 for receiving data.
25 * However, there seems to be number of issues with concurrent access and at
26 * least one know hardware bug in using BAP0 and BAP1 concurrently with PCI
27 * Prism2.5. Therefore, the driver now only uses BAP0 for moving data between
28 * host and card memories. BAP0 accesses are protected with local->baplock
29 * (spin_lock_bh) to prevent concurrent use.
33 #include <linux/config.h>
34 #include <linux/version.h>
36 #include <asm/delay.h>
37 #include <asm/uaccess.h>
39 #include <linux/slab.h>
40 #include <linux/netdevice.h>
41 #include <linux/etherdevice.h>
42 #include <linux/proc_fs.h>
43 #include <linux/if_arp.h>
44 #include <linux/delay.h>
45 #include <linux/random.h>
46 #include <linux/wait.h>
47 #include <linux/sched.h>
48 #include <linux/rtnetlink.h>
49 #include <linux/wireless.h>
50 #include <net/iw_handler.h>
51 #include <net/ieee80211.h>
52 #include <net/ieee80211_crypt.h>
55 #include "hostap_80211.h"
57 #include "hostap_ap.h"
60 /* #define final_version */
62 static int mtu = 1500;
63 module_param(mtu, int, 0444);
64 MODULE_PARM_DESC(mtu, "Maximum transfer unit");
66 static int channel[MAX_PARM_DEVICES] = { 3, DEF_INTS };
67 module_param_array(channel, int, NULL, 0444);
68 MODULE_PARM_DESC(channel, "Initial channel");
70 static char essid[33] = "test";
71 module_param_string(essid, essid, sizeof(essid), 0444);
72 MODULE_PARM_DESC(essid, "Host AP's ESSID");
74 static int iw_mode[MAX_PARM_DEVICES] = { IW_MODE_MASTER, DEF_INTS };
75 module_param_array(iw_mode, int, NULL, 0444);
76 MODULE_PARM_DESC(iw_mode, "Initial operation mode");
78 static int beacon_int[MAX_PARM_DEVICES] = { 100, DEF_INTS };
79 module_param_array(beacon_int, int, NULL, 0444);
80 MODULE_PARM_DESC(beacon_int, "Beacon interval (1 = 1024 usec)");
82 static int dtim_period[MAX_PARM_DEVICES] = { 1, DEF_INTS };
83 module_param_array(dtim_period, int, NULL, 0444);
84 MODULE_PARM_DESC(dtim_period, "DTIM period");
86 #if defined(PRISM2_PCI) && defined(PRISM2_BUS_MASTER)
87 static int bus_master_threshold_rx[MAX_PARM_DEVICES] = { 100, DEF_INTS };
88 module_param_array(bus_master_threshold_rx, int, NULL, 0444);
89 MODULE_PARM_DESC(bus_master_threshold_rx, "Packet length threshold for using "
90 "PCI bus master on RX");
92 static int bus_master_threshold_tx[MAX_PARM_DEVICES] = { 100, DEF_INTS };
93 module_param_array(bus_master_threshold_tx, int, NULL, 0444);
94 MODULE_PARM_DESC(bus_master_threshold_tx, "Packet length threshold for using "
95 "PCI bus master on TX");
96 #endif /* PRISM2_PCI and PRISM2_BUS_MASTER */
98 static char dev_template[16] = "wlan%d";
99 module_param_string(dev_template, dev_template, sizeof(dev_template), 0444);
100 MODULE_PARM_DESC(dev_template, "Prefix for network device name (default: "
104 #define EXTRA_EVENTS_WTERR 0
106 /* check WTERR events (Wait Time-out) in development versions */
107 #define EXTRA_EVENTS_WTERR HFA384X_EV_WTERR
110 #if defined(PRISM2_PCI) && defined(PRISM2_BUS_MASTER)
111 #define EXTRA_EVENTS_BUS_MASTER (HFA384X_EV_PCI_M0 | HFA384X_EV_PCI_M1)
113 #define EXTRA_EVENTS_BUS_MASTER 0
116 /* Events that will be using BAP0 */
117 #define HFA384X_BAP0_EVENTS \
118 (HFA384X_EV_TXEXC | HFA384X_EV_RX | HFA384X_EV_INFO | HFA384X_EV_TX)
120 /* event mask, i.e., events that will result in an interrupt */
121 #define HFA384X_EVENT_MASK \
122 (HFA384X_BAP0_EVENTS | HFA384X_EV_ALLOC | HFA384X_EV_INFDROP | \
123 HFA384X_EV_CMD | HFA384X_EV_TICK | \
124 EXTRA_EVENTS_WTERR | EXTRA_EVENTS_BUS_MASTER)
126 /* Default TX control flags: use 802.11 headers and request interrupt for
127 * failed transmits. Frames that request ACK callback, will add
128 * _TX_OK flag and _ALT_RTRY flag may be used to select different retry policy.
130 #define HFA384X_TX_CTRL_FLAGS \
131 (HFA384X_TX_CTRL_802_11 | HFA384X_TX_CTRL_TX_EX)
135 #define HFA384X_CMD_BUSY_TIMEOUT 5000
136 #define HFA384X_BAP_BUSY_TIMEOUT 50000
139 #define HFA384X_CMD_COMPL_TIMEOUT 20000
140 #define HFA384X_DL_COMPL_TIMEOUT 1000000
142 /* Wait times for initialization; yield to other processes to avoid busy
143 * waiting for long time. */
144 #define HFA384X_INIT_TIMEOUT (HZ / 2) /* 500 ms */
145 #define HFA384X_ALLOC_COMPL_TIMEOUT (HZ / 20) /* 50 ms */
148 static void prism2_hw_reset(struct net_device *dev);
149 static void prism2_check_sta_fw_version(local_info_t *local);
151 #ifdef PRISM2_DOWNLOAD_SUPPORT
152 /* hostap_download.c */
153 static int prism2_download_aux_dump(struct net_device *dev,
154 unsigned int addr, int len, u8 *buf);
155 static u8 * prism2_read_pda(struct net_device *dev);
156 static int prism2_download(local_info_t *local,
157 struct prism2_download_param *param);
158 static void prism2_download_free_data(struct prism2_download_data *dl);
159 static int prism2_download_volatile(local_info_t *local,
160 struct prism2_download_data *param);
161 static int prism2_download_genesis(local_info_t *local,
162 struct prism2_download_data *param);
163 static int prism2_get_ram_size(local_info_t *local);
164 #endif /* PRISM2_DOWNLOAD_SUPPORT */
169 #ifndef final_version
170 /* magic value written to SWSUPPORT0 reg. for detecting whether card is still
172 #define HFA384X_MAGIC 0x8A32
176 static u16 hfa384x_read_reg(struct net_device *dev, u16 reg)
178 return HFA384X_INW(reg);
182 static void hfa384x_read_regs(struct net_device *dev,
183 struct hfa384x_regs *regs)
185 regs->cmd = HFA384X_INW(HFA384X_CMD_OFF);
186 regs->evstat = HFA384X_INW(HFA384X_EVSTAT_OFF);
187 regs->offset0 = HFA384X_INW(HFA384X_OFFSET0_OFF);
188 regs->offset1 = HFA384X_INW(HFA384X_OFFSET1_OFF);
189 regs->swsupport0 = HFA384X_INW(HFA384X_SWSUPPORT0_OFF);
194 * __hostap_cmd_queue_free - Free Prism2 command queue entry (private)
195 * @local: pointer to private Host AP driver data
196 * @entry: Prism2 command queue entry to be freed
197 * @del_req: request the entry to be removed
199 * Internal helper function for freeing Prism2 command queue entries.
200 * Caller must have acquired local->cmdlock before calling this function.
202 static inline void __hostap_cmd_queue_free(local_info_t *local,
203 struct hostap_cmd_queue *entry,
208 if (!list_empty(&entry->list)) {
209 list_del_init(&entry->list);
210 local->cmd_queue_len--;
214 if (atomic_dec_and_test(&entry->usecnt) && entry->del_req)
220 * hostap_cmd_queue_free - Free Prism2 command queue entry
221 * @local: pointer to private Host AP driver data
222 * @entry: Prism2 command queue entry to be freed
223 * @del_req: request the entry to be removed
225 * Free a Prism2 command queue entry.
227 static inline void hostap_cmd_queue_free(local_info_t *local,
228 struct hostap_cmd_queue *entry,
233 spin_lock_irqsave(&local->cmdlock, flags);
234 __hostap_cmd_queue_free(local, entry, del_req);
235 spin_unlock_irqrestore(&local->cmdlock, flags);
240 * prism2_clear_cmd_queue - Free all pending Prism2 command queue entries
241 * @local: pointer to private Host AP driver data
243 static void prism2_clear_cmd_queue(local_info_t *local)
245 struct list_head *ptr, *n;
247 struct hostap_cmd_queue *entry;
249 spin_lock_irqsave(&local->cmdlock, flags);
250 list_for_each_safe(ptr, n, &local->cmd_queue) {
251 entry = list_entry(ptr, struct hostap_cmd_queue, list);
252 atomic_inc(&entry->usecnt);
253 printk(KERN_DEBUG "%s: removed pending cmd_queue entry "
254 "(type=%d, cmd=0x%04x, param0=0x%04x)\n",
255 local->dev->name, entry->type, entry->cmd,
257 __hostap_cmd_queue_free(local, entry, 1);
259 if (local->cmd_queue_len) {
260 /* This should not happen; print debug message and clear
262 printk(KERN_DEBUG "%s: cmd_queue_len (%d) not zero after "
263 "flush\n", local->dev->name, local->cmd_queue_len);
264 local->cmd_queue_len = 0;
266 spin_unlock_irqrestore(&local->cmdlock, flags);
271 * hfa384x_cmd_issue - Issue a Prism2 command to the hardware
272 * @dev: pointer to net_device
273 * @entry: Prism2 command queue entry to be issued
275 static inline int hfa384x_cmd_issue(struct net_device *dev,
276 struct hostap_cmd_queue *entry)
278 struct hostap_interface *iface;
284 iface = netdev_priv(dev);
285 local = iface->local;
287 if (local->func->card_present && !local->func->card_present(local))
291 printk(KERN_DEBUG "%s: driver bug - re-issuing command @%p\n",
295 /* wait until busy bit is clear; this should always be clear since the
296 * commands are serialized */
297 tries = HFA384X_CMD_BUSY_TIMEOUT;
298 while (HFA384X_INW(HFA384X_CMD_OFF) & HFA384X_CMD_BUSY && tries > 0) {
302 #ifndef final_version
303 if (tries != HFA384X_CMD_BUSY_TIMEOUT) {
304 prism2_io_debug_error(dev, 1);
305 printk(KERN_DEBUG "%s: hfa384x_cmd_issue: cmd reg was busy "
306 "for %d usec\n", dev->name,
307 HFA384X_CMD_BUSY_TIMEOUT - tries);
311 reg = HFA384X_INW(HFA384X_CMD_OFF);
312 prism2_io_debug_error(dev, 2);
313 printk(KERN_DEBUG "%s: hfa384x_cmd_issue - timeout - "
314 "reg=0x%04x\n", dev->name, reg);
319 spin_lock_irqsave(&local->cmdlock, flags);
320 HFA384X_OUTW(entry->param0, HFA384X_PARAM0_OFF);
321 HFA384X_OUTW(entry->param1, HFA384X_PARAM1_OFF);
322 HFA384X_OUTW(entry->cmd, HFA384X_CMD_OFF);
324 spin_unlock_irqrestore(&local->cmdlock, flags);
331 * hfa384x_cmd - Issue a Prism2 command and wait (sleep) for completion
332 * @dev: pointer to net_device
333 * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
334 * @param0: value for Param0 register
335 * @param1: value for Param1 register (pointer; %NULL if not used)
336 * @resp0: pointer for Resp0 data or %NULL if Resp0 is not needed
338 * Issue given command (possibly after waiting in command queue) and sleep
339 * until the command is completed (or timed out or interrupted). This can be
340 * called only from user process context.
342 static int hfa384x_cmd(struct net_device *dev, u16 cmd, u16 param0,
343 u16 *param1, u16 *resp0)
345 struct hostap_interface *iface;
347 int err, res, issue, issued = 0;
349 struct hostap_cmd_queue *entry;
350 DECLARE_WAITQUEUE(wait, current);
352 iface = netdev_priv(dev);
353 local = iface->local;
355 if (in_interrupt()) {
356 printk(KERN_DEBUG "%s: hfa384x_cmd called from interrupt "
357 "context\n", dev->name);
361 if (local->cmd_queue_len >= HOSTAP_CMD_QUEUE_MAX_LEN) {
362 printk(KERN_DEBUG "%s: hfa384x_cmd: cmd_queue full\n",
367 if (signal_pending(current))
370 entry = (struct hostap_cmd_queue *)
371 kmalloc(sizeof(*entry), GFP_ATOMIC);
373 printk(KERN_DEBUG "%s: hfa384x_cmd - kmalloc failed\n",
377 memset(entry, 0, sizeof(*entry));
378 atomic_set(&entry->usecnt, 1);
379 entry->type = CMD_SLEEP;
381 entry->param0 = param0;
383 entry->param1 = *param1;
384 init_waitqueue_head(&entry->compl);
386 /* prepare to wait for command completion event, but do not sleep yet
388 add_wait_queue(&entry->compl, &wait);
389 set_current_state(TASK_INTERRUPTIBLE);
391 spin_lock_irqsave(&local->cmdlock, flags);
392 issue = list_empty(&local->cmd_queue);
395 list_add_tail(&entry->list, &local->cmd_queue);
396 local->cmd_queue_len++;
397 spin_unlock_irqrestore(&local->cmdlock, flags);
401 goto wait_completion;
403 if (signal_pending(current))
407 if (hfa384x_cmd_issue(dev, entry))
414 if (!err && entry->type != CMD_COMPLETED) {
415 /* sleep until command is completed or timed out */
416 res = schedule_timeout(2 * HZ);
420 if (!err && signal_pending(current))
424 /* the command was issued, so a CmdCompl event should occur
425 * soon; however, there's a pending signal and
426 * schedule_timeout() would be interrupted; wait a short period
427 * of time to avoid removing entry from the list before
432 set_current_state(TASK_RUNNING);
433 remove_wait_queue(&entry->compl, &wait);
435 /* If entry->list is still in the list, it must be removed
436 * first and in this case prism2_cmd_ev() does not yet have
437 * local reference to it, and the data can be kfree()'d
438 * here. If the command completion event is still generated,
439 * it will be assigned to next (possibly) pending command, but
440 * the driver will reset the card anyway due to timeout
442 * If the entry is not in the list prism2_cmd_ev() has a local
443 * reference to it, but keeps cmdlock as long as the data is
444 * needed, so the data can be kfree()'d here. */
446 /* FIX: if the entry->list is in the list, it has not been completed
447 * yet, so removing it here is somewhat wrong.. this could cause
448 * references to freed memory and next list_del() causing NULL pointer
449 * dereference.. it would probably be better to leave the entry in the
450 * list and the list should be emptied during hw reset */
452 spin_lock_irqsave(&local->cmdlock, flags);
453 if (!list_empty(&entry->list)) {
454 printk(KERN_DEBUG "%s: hfa384x_cmd: entry still in list? "
455 "(entry=%p, type=%d, res=%d)\n", dev->name, entry,
457 list_del_init(&entry->list);
458 local->cmd_queue_len--;
460 spin_unlock_irqrestore(&local->cmdlock, flags);
463 printk(KERN_DEBUG "%s: hfa384x_cmd: interrupted; err=%d\n",
469 if (entry->type != CMD_COMPLETED) {
470 u16 reg = HFA384X_INW(HFA384X_EVSTAT_OFF);
471 printk(KERN_DEBUG "%s: hfa384x_cmd: command was not "
472 "completed (res=%d, entry=%p, type=%d, cmd=0x%04x, "
473 "param0=0x%04x, EVSTAT=%04x INTEN=%04x)\n", dev->name,
474 res, entry, entry->type, entry->cmd, entry->param0, reg,
475 HFA384X_INW(HFA384X_INTEN_OFF));
476 if (reg & HFA384X_EV_CMD) {
477 /* Command completion event is pending, but the
478 * interrupt was not delivered - probably an issue
479 * with pcmcia-cs configuration. */
480 printk(KERN_WARNING "%s: interrupt delivery does not "
481 "seem to work\n", dev->name);
483 prism2_io_debug_error(dev, 3);
489 *resp0 = entry->resp0;
490 #ifndef final_version
492 printk(KERN_DEBUG "%s: CMD=0x%04x => res=0x%02x, "
494 dev->name, cmd, entry->res, entry->resp0);
496 #endif /* final_version */
500 hostap_cmd_queue_free(local, entry, 1);
506 * hfa384x_cmd_callback - Issue a Prism2 command; callback when completed
507 * @dev: pointer to net_device
508 * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
509 * @param0: value for Param0 register
510 * @callback: command completion callback function (%NULL = no callback)
511 * @context: context data to be given to the callback function
513 * Issue given command (possibly after waiting in command queue) and use
514 * callback function to indicate command completion. This can be called both
515 * from user and interrupt context. The callback function will be called in
516 * hardware IRQ context. It can be %NULL, when no function is called when
517 * command is completed.
519 static int hfa384x_cmd_callback(struct net_device *dev, u16 cmd, u16 param0,
520 void (*callback)(struct net_device *dev,
521 long context, u16 resp0,
525 struct hostap_interface *iface;
529 struct hostap_cmd_queue *entry;
531 iface = netdev_priv(dev);
532 local = iface->local;
534 if (local->cmd_queue_len >= HOSTAP_CMD_QUEUE_MAX_LEN + 2) {
535 printk(KERN_DEBUG "%s: hfa384x_cmd: cmd_queue full\n",
540 entry = (struct hostap_cmd_queue *)
541 kmalloc(sizeof(*entry), GFP_ATOMIC);
543 printk(KERN_DEBUG "%s: hfa384x_cmd_callback - kmalloc "
544 "failed\n", dev->name);
547 memset(entry, 0, sizeof(*entry));
548 atomic_set(&entry->usecnt, 1);
549 entry->type = CMD_CALLBACK;
551 entry->param0 = param0;
552 entry->callback = callback;
553 entry->context = context;
555 spin_lock_irqsave(&local->cmdlock, flags);
556 issue = list_empty(&local->cmd_queue);
559 list_add_tail(&entry->list, &local->cmd_queue);
560 local->cmd_queue_len++;
561 spin_unlock_irqrestore(&local->cmdlock, flags);
563 if (issue && hfa384x_cmd_issue(dev, entry))
568 hostap_cmd_queue_free(local, entry, ret);
575 * __hfa384x_cmd_no_wait - Issue a Prism2 command (private)
576 * @dev: pointer to net_device
577 * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
578 * @param0: value for Param0 register
579 * @io_debug_num: I/O debug error number
581 * Shared helper function for hfa384x_cmd_wait() and hfa384x_cmd_no_wait().
583 static int __hfa384x_cmd_no_wait(struct net_device *dev, u16 cmd, u16 param0,
589 /* wait until busy bit is clear; this should always be clear since the
590 * commands are serialized */
591 tries = HFA384X_CMD_BUSY_TIMEOUT;
592 while (HFA384X_INW(HFA384X_CMD_OFF) & HFA384X_CMD_BUSY && tries > 0) {
597 reg = HFA384X_INW(HFA384X_CMD_OFF);
598 prism2_io_debug_error(dev, io_debug_num);
599 printk(KERN_DEBUG "%s: __hfa384x_cmd_no_wait(%d) - timeout - "
600 "reg=0x%04x\n", dev->name, io_debug_num, reg);
605 HFA384X_OUTW(param0, HFA384X_PARAM0_OFF);
606 HFA384X_OUTW(cmd, HFA384X_CMD_OFF);
613 * hfa384x_cmd_wait - Issue a Prism2 command and busy wait for completion
614 * @dev: pointer to net_device
615 * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
616 * @param0: value for Param0 register
618 static int hfa384x_cmd_wait(struct net_device *dev, u16 cmd, u16 param0)
623 res = __hfa384x_cmd_no_wait(dev, cmd, param0, 4);
627 /* wait for command completion */
628 if ((cmd & HFA384X_CMDCODE_MASK) == HFA384X_CMDCODE_DOWNLOAD)
629 tries = HFA384X_DL_COMPL_TIMEOUT;
631 tries = HFA384X_CMD_COMPL_TIMEOUT;
633 while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD) &&
639 reg = HFA384X_INW(HFA384X_EVSTAT_OFF);
640 prism2_io_debug_error(dev, 5);
641 printk(KERN_DEBUG "%s: hfa384x_cmd_wait - timeout2 - "
642 "reg=0x%04x\n", dev->name, reg);
646 res = (HFA384X_INW(HFA384X_STATUS_OFF) &
647 (BIT(14) | BIT(13) | BIT(12) | BIT(11) | BIT(10) | BIT(9) |
649 #ifndef final_version
651 printk(KERN_DEBUG "%s: CMD=0x%04x => res=0x%02x\n",
652 dev->name, cmd, res);
656 HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
663 * hfa384x_cmd_no_wait - Issue a Prism2 command; do not wait for completion
664 * @dev: pointer to net_device
665 * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
666 * @param0: value for Param0 register
668 static inline int hfa384x_cmd_no_wait(struct net_device *dev, u16 cmd,
671 return __hfa384x_cmd_no_wait(dev, cmd, param0, 6);
676 * prism2_cmd_ev - Prism2 command completion event handler
677 * @dev: pointer to net_device
679 * Interrupt handler for command completion events. Called by the main
680 * interrupt handler in hardware IRQ context. Read Resp0 and status registers
681 * from the hardware and ACK the event. Depending on the issued command type
682 * either wake up the sleeping process that is waiting for command completion
683 * or call the callback function. Issue the next command, if one is pending.
685 static void prism2_cmd_ev(struct net_device *dev)
687 struct hostap_interface *iface;
689 struct hostap_cmd_queue *entry = NULL;
691 iface = netdev_priv(dev);
692 local = iface->local;
694 spin_lock(&local->cmdlock);
695 if (!list_empty(&local->cmd_queue)) {
696 entry = list_entry(local->cmd_queue.next,
697 struct hostap_cmd_queue, list);
698 atomic_inc(&entry->usecnt);
699 list_del_init(&entry->list);
700 local->cmd_queue_len--;
702 if (!entry->issued) {
703 printk(KERN_DEBUG "%s: Command completion event, but "
704 "cmd not issued\n", dev->name);
705 __hostap_cmd_queue_free(local, entry, 1);
709 spin_unlock(&local->cmdlock);
712 HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
713 printk(KERN_DEBUG "%s: Command completion event, but no "
714 "pending commands\n", dev->name);
718 entry->resp0 = HFA384X_INW(HFA384X_RESP0_OFF);
719 entry->res = (HFA384X_INW(HFA384X_STATUS_OFF) &
720 (BIT(14) | BIT(13) | BIT(12) | BIT(11) | BIT(10) |
721 BIT(9) | BIT(8))) >> 8;
722 HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
724 /* TODO: rest of the CmdEv handling could be moved to tasklet */
725 if (entry->type == CMD_SLEEP) {
726 entry->type = CMD_COMPLETED;
727 wake_up_interruptible(&entry->compl);
728 } else if (entry->type == CMD_CALLBACK) {
730 entry->callback(dev, entry->context, entry->resp0,
733 printk(KERN_DEBUG "%s: Invalid command completion type %d\n",
734 dev->name, entry->type);
736 hostap_cmd_queue_free(local, entry, 1);
738 /* issue next command, if pending */
740 spin_lock(&local->cmdlock);
741 if (!list_empty(&local->cmd_queue)) {
742 entry = list_entry(local->cmd_queue.next,
743 struct hostap_cmd_queue, list);
744 if (entry->issuing) {
745 /* hfa384x_cmd() has already started issuing this
746 * command, so do not start here */
750 atomic_inc(&entry->usecnt);
752 spin_unlock(&local->cmdlock);
755 /* issue next command; if command issuing fails, remove the
756 * entry from cmd_queue */
757 int res = hfa384x_cmd_issue(dev, entry);
758 spin_lock(&local->cmdlock);
759 __hostap_cmd_queue_free(local, entry, res);
760 spin_unlock(&local->cmdlock);
765 static inline int hfa384x_wait_offset(struct net_device *dev, u16 o_off)
767 int tries = HFA384X_BAP_BUSY_TIMEOUT;
768 int res = HFA384X_INW(o_off) & HFA384X_OFFSET_BUSY;
770 while (res && tries > 0) {
773 res = HFA384X_INW(o_off) & HFA384X_OFFSET_BUSY;
779 /* Offset must be even */
780 static int hfa384x_setup_bap(struct net_device *dev, u16 bap, u16 id,
786 if (offset % 2 || bap > 1)
790 o_off = HFA384X_OFFSET1_OFF;
791 s_off = HFA384X_SELECT1_OFF;
793 o_off = HFA384X_OFFSET0_OFF;
794 s_off = HFA384X_SELECT0_OFF;
797 if (hfa384x_wait_offset(dev, o_off)) {
798 prism2_io_debug_error(dev, 7);
799 printk(KERN_DEBUG "%s: hfa384x_setup_bap - timeout before\n",
805 HFA384X_OUTW(id, s_off);
806 HFA384X_OUTW(offset, o_off);
808 if (hfa384x_wait_offset(dev, o_off)) {
809 prism2_io_debug_error(dev, 8);
810 printk(KERN_DEBUG "%s: hfa384x_setup_bap - timeout after\n",
815 #ifndef final_version
816 if (HFA384X_INW(o_off) & HFA384X_OFFSET_ERR) {
817 prism2_io_debug_error(dev, 9);
818 printk(KERN_DEBUG "%s: hfa384x_setup_bap - offset error "
819 "(%d,0x04%x,%d); reg=0x%04x\n",
820 dev->name, bap, id, offset, HFA384X_INW(o_off));
830 static int hfa384x_get_rid(struct net_device *dev, u16 rid, void *buf, int len,
833 struct hostap_interface *iface;
836 struct hfa384x_rid_hdr rec;
838 iface = netdev_priv(dev);
839 local = iface->local;
842 printk(KERN_DEBUG "%s: cannot get RID %04x (len=%d) - no PRI "
843 "f/w\n", dev->name, rid, len);
844 return -ENOTTY; /* Well.. not really correct, but return
845 * something unique enough.. */
848 if ((local->func->card_present && !local->func->card_present(local)) ||
849 local->hw_downloading)
852 res = down_interruptible(&local->rid_bap_sem);
856 res = hfa384x_cmd(dev, HFA384X_CMDCODE_ACCESS, rid, NULL, NULL);
858 printk(KERN_DEBUG "%s: hfa384x_get_rid: CMDCODE_ACCESS failed "
859 "(res=%d, rid=%04x, len=%d)\n",
860 dev->name, res, rid, len);
861 up(&local->rid_bap_sem);
865 spin_lock_bh(&local->baplock);
867 res = hfa384x_setup_bap(dev, BAP0, rid, 0);
869 res = hfa384x_from_bap(dev, BAP0, &rec, sizeof(rec));
871 if (le16_to_cpu(rec.len) == 0) {
872 /* RID not available */
876 rlen = (le16_to_cpu(rec.len) - 1) * 2;
877 if (!res && exact_len && rlen != len) {
878 printk(KERN_DEBUG "%s: hfa384x_get_rid - RID len mismatch: "
879 "rid=0x%04x, len=%d (expected %d)\n",
880 dev->name, rid, rlen, len);
885 res = hfa384x_from_bap(dev, BAP0, buf, len);
887 spin_unlock_bh(&local->baplock);
888 up(&local->rid_bap_sem);
892 printk(KERN_DEBUG "%s: hfa384x_get_rid (rid=%04x, "
893 "len=%d) - failed - res=%d\n", dev->name, rid,
895 if (res == -ETIMEDOUT)
896 prism2_hw_reset(dev);
904 static int hfa384x_set_rid(struct net_device *dev, u16 rid, void *buf, int len)
906 struct hostap_interface *iface;
908 struct hfa384x_rid_hdr rec;
911 iface = netdev_priv(dev);
912 local = iface->local;
915 printk(KERN_DEBUG "%s: cannot set RID %04x (len=%d) - no PRI "
916 "f/w\n", dev->name, rid, len);
917 return -ENOTTY; /* Well.. not really correct, but return
918 * something unique enough.. */
921 if ((local->func->card_present && !local->func->card_present(local)) ||
922 local->hw_downloading)
925 rec.rid = cpu_to_le16(rid);
926 /* RID len in words and +1 for rec.rid */
927 rec.len = cpu_to_le16(len / 2 + len % 2 + 1);
929 res = down_interruptible(&local->rid_bap_sem);
933 spin_lock_bh(&local->baplock);
934 res = hfa384x_setup_bap(dev, BAP0, rid, 0);
936 res = hfa384x_to_bap(dev, BAP0, &rec, sizeof(rec));
938 res = hfa384x_to_bap(dev, BAP0, buf, len);
939 spin_unlock_bh(&local->baplock);
942 printk(KERN_DEBUG "%s: hfa384x_set_rid (rid=%04x, len=%d) - "
943 "failed - res=%d\n", dev->name, rid, len, res);
944 up(&local->rid_bap_sem);
948 res = hfa384x_cmd(dev, HFA384X_CMDCODE_ACCESS_WRITE, rid, NULL, NULL);
949 up(&local->rid_bap_sem);
951 printk(KERN_DEBUG "%s: hfa384x_set_rid: CMDCODE_ACCESS_WRITE "
952 "failed (res=%d, rid=%04x, len=%d)\n",
953 dev->name, res, rid, len);
957 if (res == -ETIMEDOUT)
958 prism2_hw_reset(dev);
964 static void hfa384x_disable_interrupts(struct net_device *dev)
966 /* disable interrupts and clear event status */
967 HFA384X_OUTW(0, HFA384X_INTEN_OFF);
968 HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
972 static void hfa384x_enable_interrupts(struct net_device *dev)
974 /* ack pending events and enable interrupts from selected events */
975 HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
976 HFA384X_OUTW(HFA384X_EVENT_MASK, HFA384X_INTEN_OFF);
980 static void hfa384x_events_no_bap0(struct net_device *dev)
982 HFA384X_OUTW(HFA384X_EVENT_MASK & ~HFA384X_BAP0_EVENTS,
987 static void hfa384x_events_all(struct net_device *dev)
989 HFA384X_OUTW(HFA384X_EVENT_MASK, HFA384X_INTEN_OFF);
993 static void hfa384x_events_only_cmd(struct net_device *dev)
995 HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_INTEN_OFF);
999 static u16 hfa384x_allocate_fid(struct net_device *dev, int len)
1002 unsigned long delay;
1004 /* FIX: this could be replace with hfa384x_cmd() if the Alloc event
1005 * below would be handled like CmdCompl event (sleep here, wake up from
1006 * interrupt handler */
1007 if (hfa384x_cmd_wait(dev, HFA384X_CMDCODE_ALLOC, len)) {
1008 printk(KERN_DEBUG "%s: cannot allocate fid, len=%d\n",
1013 delay = jiffies + HFA384X_ALLOC_COMPL_TIMEOUT;
1014 while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_ALLOC) &&
1015 time_before(jiffies, delay))
1017 if (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_ALLOC)) {
1018 printk("%s: fid allocate, len=%d - timeout\n", dev->name, len);
1022 fid = HFA384X_INW(HFA384X_ALLOCFID_OFF);
1023 HFA384X_OUTW(HFA384X_EV_ALLOC, HFA384X_EVACK_OFF);
1029 static int prism2_reset_port(struct net_device *dev)
1031 struct hostap_interface *iface;
1032 local_info_t *local;
1035 iface = netdev_priv(dev);
1036 local = iface->local;
1038 if (!local->dev_enabled)
1041 res = hfa384x_cmd(dev, HFA384X_CMDCODE_DISABLE, 0,
1044 printk(KERN_DEBUG "%s: reset port failed to disable port\n",
1047 res = hfa384x_cmd(dev, HFA384X_CMDCODE_ENABLE, 0,
1050 printk(KERN_DEBUG "%s: reset port failed to enable "
1051 "port\n", dev->name);
1054 /* It looks like at least some STA firmware versions reset
1055 * fragmentation threshold back to 2346 after enable command. Restore
1056 * the configured value, if it differs from this default. */
1057 if (local->fragm_threshold != 2346 &&
1058 hostap_set_word(dev, HFA384X_RID_FRAGMENTATIONTHRESHOLD,
1059 local->fragm_threshold)) {
1060 printk(KERN_DEBUG "%s: failed to restore fragmentation "
1061 "threshold (%d) after Port0 enable\n",
1062 dev->name, local->fragm_threshold);
1069 static int prism2_get_version_info(struct net_device *dev, u16 rid,
1072 struct hfa384x_comp_ident comp;
1073 struct hostap_interface *iface;
1074 local_info_t *local;
1076 iface = netdev_priv(dev);
1077 local = iface->local;
1079 if (local->no_pri) {
1080 /* PRI f/w not yet available - cannot read RIDs */
1083 if (hfa384x_get_rid(dev, rid, &comp, sizeof(comp), 1) < 0) {
1084 printk(KERN_DEBUG "Could not get RID for component %s\n", txt);
1088 printk(KERN_INFO "%s: %s: id=0x%02x v%d.%d.%d\n", dev->name, txt,
1089 __le16_to_cpu(comp.id), __le16_to_cpu(comp.major),
1090 __le16_to_cpu(comp.minor), __le16_to_cpu(comp.variant));
1095 static int prism2_setup_rids(struct net_device *dev)
1097 struct hostap_interface *iface;
1098 local_info_t *local;
1102 iface = netdev_priv(dev);
1103 local = iface->local;
1105 hostap_set_word(dev, HFA384X_RID_TICKTIME, 2000);
1107 if (!local->fw_ap) {
1108 tmp = hostap_get_porttype(local);
1109 ret = hostap_set_word(dev, HFA384X_RID_CNFPORTTYPE, tmp);
1111 printk("%s: Port type setting to %d failed\n",
1117 /* Setting SSID to empty string seems to kill the card in Host AP mode
1119 if (local->iw_mode != IW_MODE_MASTER || local->essid[0] != '\0') {
1120 ret = hostap_set_string(dev, HFA384X_RID_CNFOWNSSID,
1123 printk("%s: AP own SSID setting failed\n", dev->name);
1128 ret = hostap_set_word(dev, HFA384X_RID_CNFMAXDATALEN,
1129 PRISM2_DATA_MAXLEN);
1131 printk("%s: MAC data length setting to %d failed\n",
1132 dev->name, PRISM2_DATA_MAXLEN);
1136 if (hfa384x_get_rid(dev, HFA384X_RID_CHANNELLIST, &tmp, 2, 1) < 0) {
1137 printk("%s: Channel list read failed\n", dev->name);
1141 local->channel_mask = __le16_to_cpu(tmp);
1143 if (local->channel < 1 || local->channel > 14 ||
1144 !(local->channel_mask & (1 << (local->channel - 1)))) {
1145 printk(KERN_WARNING "%s: Channel setting out of range "
1146 "(%d)!\n", dev->name, local->channel);
1151 ret = hostap_set_word(dev, HFA384X_RID_CNFOWNCHANNEL, local->channel);
1153 printk("%s: Channel setting to %d failed\n",
1154 dev->name, local->channel);
1158 ret = hostap_set_word(dev, HFA384X_RID_CNFBEACONINT,
1161 printk("%s: Beacon interval setting to %d failed\n",
1162 dev->name, local->beacon_int);
1163 /* this may fail with Symbol/Lucent firmware */
1164 if (ret == -ETIMEDOUT)
1168 ret = hostap_set_word(dev, HFA384X_RID_CNFOWNDTIMPERIOD,
1169 local->dtim_period);
1171 printk("%s: DTIM period setting to %d failed\n",
1172 dev->name, local->dtim_period);
1173 /* this may fail with Symbol/Lucent firmware */
1174 if (ret == -ETIMEDOUT)
1178 ret = hostap_set_word(dev, HFA384X_RID_PROMISCUOUSMODE,
1181 printk(KERN_INFO "%s: Setting promiscuous mode (%d) failed\n",
1182 dev->name, local->is_promisc);
1184 if (!local->fw_ap) {
1185 ret = hostap_set_string(dev, HFA384X_RID_CNFDESIREDSSID,
1188 printk("%s: Desired SSID setting failed\n", dev->name);
1193 /* Setup TXRateControl, defaults to allow use of 1, 2, 5.5, and
1194 * 11 Mbps in automatic TX rate fallback and 1 and 2 Mbps as basic
1196 if (local->tx_rate_control == 0) {
1197 local->tx_rate_control =
1198 HFA384X_RATES_1MBPS |
1199 HFA384X_RATES_2MBPS |
1200 HFA384X_RATES_5MBPS |
1201 HFA384X_RATES_11MBPS;
1203 if (local->basic_rates == 0)
1204 local->basic_rates = HFA384X_RATES_1MBPS | HFA384X_RATES_2MBPS;
1206 if (!local->fw_ap) {
1207 ret = hostap_set_word(dev, HFA384X_RID_TXRATECONTROL,
1208 local->tx_rate_control);
1210 printk("%s: TXRateControl setting to %d failed\n",
1211 dev->name, local->tx_rate_control);
1215 ret = hostap_set_word(dev, HFA384X_RID_CNFSUPPORTEDRATES,
1216 local->tx_rate_control);
1218 printk("%s: cnfSupportedRates setting to %d failed\n",
1219 dev->name, local->tx_rate_control);
1222 ret = hostap_set_word(dev, HFA384X_RID_CNFBASICRATES,
1223 local->basic_rates);
1225 printk("%s: cnfBasicRates setting to %d failed\n",
1226 dev->name, local->basic_rates);
1229 ret = hostap_set_word(dev, HFA384X_RID_CREATEIBSS, 1);
1231 printk("%s: Create IBSS setting to 1 failed\n",
1236 if (local->name_set)
1237 (void) hostap_set_string(dev, HFA384X_RID_CNFOWNNAME,
1240 if (hostap_set_encryption(local)) {
1241 printk(KERN_INFO "%s: could not configure encryption\n",
1245 (void) hostap_set_antsel(local);
1247 if (hostap_set_roaming(local)) {
1248 printk(KERN_INFO "%s: could not set host roaming\n",
1252 if (local->sta_fw_ver >= PRISM2_FW_VER(1,6,3) &&
1253 hostap_set_word(dev, HFA384X_RID_CNFENHSECURITY, local->enh_sec))
1254 printk(KERN_INFO "%s: cnfEnhSecurity setting to 0x%x failed\n",
1255 dev->name, local->enh_sec);
1257 /* 32-bit tallies were added in STA f/w 0.8.0, but they were apparently
1258 * not working correctly (last seven counters report bogus values).
1259 * This has been fixed in 0.8.2, so enable 32-bit tallies only
1260 * beginning with that firmware version. Another bug fix for 32-bit
1261 * tallies in 1.4.0; should 16-bit tallies be used for some other
1263 if (local->sta_fw_ver >= PRISM2_FW_VER(0,8,2)) {
1264 if (hostap_set_word(dev, HFA384X_RID_CNFTHIRTY2TALLY, 1)) {
1265 printk(KERN_INFO "%s: cnfThirty2Tally setting "
1266 "failed\n", dev->name);
1267 local->tallies32 = 0;
1269 local->tallies32 = 1;
1271 local->tallies32 = 0;
1273 hostap_set_auth_algs(local);
1275 if (hostap_set_word(dev, HFA384X_RID_FRAGMENTATIONTHRESHOLD,
1276 local->fragm_threshold)) {
1277 printk(KERN_INFO "%s: setting FragmentationThreshold to %d "
1278 "failed\n", dev->name, local->fragm_threshold);
1281 if (hostap_set_word(dev, HFA384X_RID_RTSTHRESHOLD,
1282 local->rts_threshold)) {
1283 printk(KERN_INFO "%s: setting RTSThreshold to %d failed\n",
1284 dev->name, local->rts_threshold);
1287 if (local->manual_retry_count >= 0 &&
1288 hostap_set_word(dev, HFA384X_RID_CNFALTRETRYCOUNT,
1289 local->manual_retry_count)) {
1290 printk(KERN_INFO "%s: setting cnfAltRetryCount to %d failed\n",
1291 dev->name, local->manual_retry_count);
1294 if (local->sta_fw_ver >= PRISM2_FW_VER(1,3,1) &&
1295 hfa384x_get_rid(dev, HFA384X_RID_CNFDBMADJUST, &tmp, 2, 1) == 2) {
1296 local->rssi_to_dBm = le16_to_cpu(tmp);
1299 if (local->sta_fw_ver >= PRISM2_FW_VER(1,7,0) && local->wpa &&
1300 hostap_set_word(dev, HFA384X_RID_SSNHANDLINGMODE, 1)) {
1301 printk(KERN_INFO "%s: setting ssnHandlingMode to 1 failed\n",
1305 if (local->sta_fw_ver >= PRISM2_FW_VER(1,7,0) && local->generic_elem &&
1306 hfa384x_set_rid(dev, HFA384X_RID_GENERICELEMENT,
1307 local->generic_elem, local->generic_elem_len)) {
1308 printk(KERN_INFO "%s: setting genericElement failed\n",
1317 static int prism2_hw_init(struct net_device *dev, int initial)
1319 struct hostap_interface *iface;
1320 local_info_t *local;
1322 unsigned long start, delay;
1324 PDEBUG(DEBUG_FLOW, "prism2_hw_init()\n");
1326 iface = netdev_priv(dev);
1327 local = iface->local;
1329 clear_bit(HOSTAP_BITS_TRANSMIT, &local->bits);
1332 /* initialize HFA 384x */
1333 ret = hfa384x_cmd_no_wait(dev, HFA384X_CMDCODE_INIT, 0);
1335 printk(KERN_INFO "%s: first command failed - assuming card "
1336 "does not have primary firmware\n", dev_info);
1339 if (first && (HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD)) {
1340 /* EvStat has Cmd bit set in some cases, so retry once if no
1341 * wait was needed */
1342 HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
1343 printk(KERN_DEBUG "%s: init command completed too quickly - "
1344 "retrying\n", dev->name);
1350 delay = jiffies + HFA384X_INIT_TIMEOUT;
1351 while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD) &&
1352 time_before(jiffies, delay))
1354 if (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD)) {
1355 printk(KERN_DEBUG "%s: assuming no Primary image in "
1356 "flash - card initialization not completed\n",
1359 #ifdef PRISM2_DOWNLOAD_SUPPORT
1360 if (local->sram_type == -1)
1361 local->sram_type = prism2_get_ram_size(local);
1362 #endif /* PRISM2_DOWNLOAD_SUPPORT */
1366 printk(KERN_DEBUG "prism2_hw_init: initialized in %lu ms\n",
1367 (jiffies - start) * 1000 / HZ);
1368 HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
1373 static int prism2_hw_init2(struct net_device *dev, int initial)
1375 struct hostap_interface *iface;
1376 local_info_t *local;
1379 iface = netdev_priv(dev);
1380 local = iface->local;
1382 #ifdef PRISM2_DOWNLOAD_SUPPORT
1387 local->pda = prism2_read_pda(dev);
1388 #endif /* PRISM2_DOWNLOAD_SUPPORT */
1390 hfa384x_disable_interrupts(dev);
1392 #ifndef final_version
1393 HFA384X_OUTW(HFA384X_MAGIC, HFA384X_SWSUPPORT0_OFF);
1394 if (HFA384X_INW(HFA384X_SWSUPPORT0_OFF) != HFA384X_MAGIC) {
1395 printk("SWSUPPORT0 write/read failed: %04X != %04X\n",
1396 HFA384X_INW(HFA384X_SWSUPPORT0_OFF), HFA384X_MAGIC);
1401 if (initial || local->pri_only) {
1402 hfa384x_events_only_cmd(dev);
1403 /* get card version information */
1404 if (prism2_get_version_info(dev, HFA384X_RID_NICID, "NIC") ||
1405 prism2_get_version_info(dev, HFA384X_RID_PRIID, "PRI")) {
1406 hfa384x_disable_interrupts(dev);
1410 if (prism2_get_version_info(dev, HFA384X_RID_STAID, "STA")) {
1411 printk(KERN_DEBUG "%s: Failed to read STA f/w version "
1412 "- only Primary f/w present\n", dev->name);
1413 local->pri_only = 1;
1416 local->pri_only = 0;
1417 hfa384x_disable_interrupts(dev);
1420 /* FIX: could convert allocate_fid to use sleeping CmdCompl wait and
1421 * enable interrupts before this. This would also require some sort of
1422 * sleeping AllocEv waiting */
1424 /* allocate TX FIDs */
1425 local->txfid_len = PRISM2_TXFID_LEN;
1426 for (i = 0; i < PRISM2_TXFID_COUNT; i++) {
1427 local->txfid[i] = hfa384x_allocate_fid(dev, local->txfid_len);
1428 if (local->txfid[i] == 0xffff && local->txfid_len > 1600) {
1429 local->txfid[i] = hfa384x_allocate_fid(dev, 1600);
1430 if (local->txfid[i] != 0xffff) {
1431 printk(KERN_DEBUG "%s: Using shorter TX FID "
1432 "(1600 bytes)\n", dev->name);
1433 local->txfid_len = 1600;
1436 if (local->txfid[i] == 0xffff)
1438 local->intransmitfid[i] = PRISM2_TXFID_EMPTY;
1441 hfa384x_events_only_cmd(dev);
1444 struct list_head *ptr;
1445 prism2_check_sta_fw_version(local);
1447 if (hfa384x_get_rid(dev, HFA384X_RID_CNFOWNMACADDR,
1448 &dev->dev_addr, 6, 1) < 0) {
1449 printk("%s: could not get own MAC address\n",
1452 list_for_each(ptr, &local->hostap_interfaces) {
1453 iface = list_entry(ptr, struct hostap_interface, list);
1454 memcpy(iface->dev->dev_addr, dev->dev_addr, ETH_ALEN);
1456 } else if (local->fw_ap)
1457 prism2_check_sta_fw_version(local);
1459 prism2_setup_rids(dev);
1461 /* MAC is now configured, but port 0 is not yet enabled */
1466 printk(KERN_WARNING "%s: Initialization failed\n", dev_info);
1471 static int prism2_hw_enable(struct net_device *dev, int initial)
1473 struct hostap_interface *iface;
1474 local_info_t *local;
1477 iface = netdev_priv(dev);
1478 local = iface->local;
1479 was_resetting = local->hw_resetting;
1481 if (hfa384x_cmd(dev, HFA384X_CMDCODE_ENABLE, 0, NULL, NULL)) {
1482 printk("%s: MAC port 0 enabling failed\n", dev->name);
1486 local->hw_ready = 1;
1487 local->hw_reset_tries = 0;
1488 local->hw_resetting = 0;
1489 hfa384x_enable_interrupts(dev);
1491 /* at least D-Link DWL-650 seems to require additional port reset
1492 * before it starts acting as an AP, so reset port automatically
1493 * here just in case */
1494 if (initial && prism2_reset_port(dev)) {
1495 printk("%s: MAC port 0 reseting failed\n", dev->name);
1499 if (was_resetting && netif_queue_stopped(dev)) {
1500 /* If hw_reset() was called during pending transmit, netif
1501 * queue was stopped. Wake it up now since the wlan card has
1503 netif_wake_queue(dev);
1510 static int prism2_hw_config(struct net_device *dev, int initial)
1512 struct hostap_interface *iface;
1513 local_info_t *local;
1515 iface = netdev_priv(dev);
1516 local = iface->local;
1518 if (local->hw_downloading)
1521 if (prism2_hw_init(dev, initial)) {
1522 return local->no_pri ? 0 : 1;
1525 if (prism2_hw_init2(dev, initial))
1528 /* Enable firmware if secondary image is loaded and at least one of the
1529 * netdevices is up. */
1530 if (!local->pri_only &&
1531 (initial == 0 || (initial == 2 && local->num_dev_open > 0))) {
1532 if (!local->dev_enabled)
1533 prism2_callback(local, PRISM2_CALLBACK_ENABLE);
1534 local->dev_enabled = 1;
1535 return prism2_hw_enable(dev, initial);
1542 static void prism2_hw_shutdown(struct net_device *dev, int no_disable)
1544 struct hostap_interface *iface;
1545 local_info_t *local;
1547 iface = netdev_priv(dev);
1548 local = iface->local;
1550 /* Allow only command completion events during disable */
1551 hfa384x_events_only_cmd(dev);
1553 local->hw_ready = 0;
1554 if (local->dev_enabled)
1555 prism2_callback(local, PRISM2_CALLBACK_DISABLE);
1556 local->dev_enabled = 0;
1558 if (local->func->card_present && !local->func->card_present(local)) {
1559 printk(KERN_DEBUG "%s: card already removed or not configured "
1560 "during shutdown\n", dev->name);
1564 if ((no_disable & HOSTAP_HW_NO_DISABLE) == 0 &&
1565 hfa384x_cmd(dev, HFA384X_CMDCODE_DISABLE, 0, NULL, NULL))
1566 printk(KERN_WARNING "%s: Shutdown failed\n", dev_info);
1568 hfa384x_disable_interrupts(dev);
1570 if (no_disable & HOSTAP_HW_ENABLE_CMDCOMPL)
1571 hfa384x_events_only_cmd(dev);
1573 prism2_clear_cmd_queue(local);
1577 static void prism2_hw_reset(struct net_device *dev)
1579 struct hostap_interface *iface;
1580 local_info_t *local;
1583 static long last_reset = 0;
1585 /* do not reset card more than once per second to avoid ending up in a
1586 * busy loop reseting the card */
1587 if (time_before_eq(jiffies, last_reset + HZ))
1589 last_reset = jiffies;
1592 iface = netdev_priv(dev);
1593 local = iface->local;
1595 if (in_interrupt()) {
1596 printk(KERN_DEBUG "%s: driver bug - prism2_hw_reset() called "
1597 "in interrupt context\n", dev->name);
1601 if (local->hw_downloading)
1604 if (local->hw_resetting) {
1605 printk(KERN_WARNING "%s: %s: already resetting card - "
1606 "ignoring reset request\n", dev_info, dev->name);
1610 local->hw_reset_tries++;
1611 if (local->hw_reset_tries > 10) {
1612 printk(KERN_WARNING "%s: too many reset tries, skipping\n",
1617 printk(KERN_WARNING "%s: %s: resetting card\n", dev_info, dev->name);
1618 hfa384x_disable_interrupts(dev);
1619 local->hw_resetting = 1;
1620 if (local->func->cor_sreset) {
1621 /* Host system seems to hang in some cases with high traffic
1622 * load or shared interrupts during COR sreset. Disable shared
1623 * interrupts during reset to avoid these crashes. COS sreset
1624 * takes quite a long time, so it is unfortunate that this
1625 * seems to be needed. Anyway, I do not know of any better way
1626 * of avoiding the crash. */
1627 disable_irq(dev->irq);
1628 local->func->cor_sreset(local);
1629 enable_irq(dev->irq);
1631 prism2_hw_shutdown(dev, 1);
1632 prism2_hw_config(dev, 0);
1633 local->hw_resetting = 0;
1635 #ifdef PRISM2_DOWNLOAD_SUPPORT
1636 if (local->dl_pri) {
1637 printk(KERN_DEBUG "%s: persistent download of primary "
1638 "firmware\n", dev->name);
1639 if (prism2_download_genesis(local, local->dl_pri) < 0)
1640 printk(KERN_WARNING "%s: download (PRI) failed\n",
1644 if (local->dl_sec) {
1645 printk(KERN_DEBUG "%s: persistent download of secondary "
1646 "firmware\n", dev->name);
1647 if (prism2_download_volatile(local, local->dl_sec) < 0)
1648 printk(KERN_WARNING "%s: download (SEC) failed\n",
1651 #endif /* PRISM2_DOWNLOAD_SUPPORT */
1653 /* TODO: restore beacon TIM bits for STAs that have buffered frames */
1657 static void prism2_schedule_reset(local_info_t *local)
1659 schedule_work(&local->reset_queue);
1663 /* Called only as scheduled task after noticing card timeout in interrupt
1665 static void handle_reset_queue(void *data)
1667 local_info_t *local = (local_info_t *) data;
1669 printk(KERN_DEBUG "%s: scheduled card reset\n", local->dev->name);
1670 prism2_hw_reset(local->dev);
1672 if (netif_queue_stopped(local->dev)) {
1675 for (i = 0; i < PRISM2_TXFID_COUNT; i++)
1676 if (local->intransmitfid[i] == PRISM2_TXFID_EMPTY) {
1677 PDEBUG(DEBUG_EXTRA, "prism2_tx_timeout: "
1679 netif_wake_queue(local->dev);
1686 static int prism2_get_txfid_idx(local_info_t *local)
1689 unsigned long flags;
1691 spin_lock_irqsave(&local->txfidlock, flags);
1692 end = idx = local->next_txfid;
1694 if (local->intransmitfid[idx] == PRISM2_TXFID_EMPTY) {
1695 local->intransmitfid[idx] = PRISM2_TXFID_RESERVED;
1696 spin_unlock_irqrestore(&local->txfidlock, flags);
1700 if (idx >= PRISM2_TXFID_COUNT)
1702 } while (idx != end);
1703 spin_unlock_irqrestore(&local->txfidlock, flags);
1705 PDEBUG(DEBUG_EXTRA2, "prism2_get_txfid_idx: no room in txfid buf: "
1706 "packet dropped\n");
1707 local->stats.tx_dropped++;
1713 /* Called only from hardware IRQ */
1714 static void prism2_transmit_cb(struct net_device *dev, long context,
1717 struct hostap_interface *iface;
1718 local_info_t *local;
1719 int idx = (int) context;
1721 iface = netdev_priv(dev);
1722 local = iface->local;
1725 printk(KERN_DEBUG "%s: prism2_transmit_cb - res=0x%02x\n",
1730 if (idx < 0 || idx >= PRISM2_TXFID_COUNT) {
1731 printk(KERN_DEBUG "%s: prism2_transmit_cb called with invalid "
1732 "idx=%d\n", dev->name, idx);
1736 if (!test_and_clear_bit(HOSTAP_BITS_TRANSMIT, &local->bits)) {
1737 printk(KERN_DEBUG "%s: driver bug: prism2_transmit_cb called "
1738 "with no pending transmit\n", dev->name);
1741 if (netif_queue_stopped(dev)) {
1742 /* ready for next TX, so wake up queue that was stopped in
1743 * prism2_transmit() */
1744 netif_wake_queue(dev);
1747 spin_lock(&local->txfidlock);
1749 /* With reclaim, Resp0 contains new txfid for transmit; the old txfid
1750 * will be automatically allocated for the next TX frame */
1751 local->intransmitfid[idx] = resp0;
1753 PDEBUG(DEBUG_FID, "%s: prism2_transmit_cb: txfid[%d]=0x%04x, "
1754 "resp0=0x%04x, transmit_txfid=0x%04x\n",
1755 dev->name, idx, local->txfid[idx],
1756 resp0, local->intransmitfid[local->next_txfid]);
1759 if (idx >= PRISM2_TXFID_COUNT)
1761 local->next_txfid = idx;
1763 /* check if all TX buffers are occupied */
1765 if (local->intransmitfid[idx] == PRISM2_TXFID_EMPTY) {
1766 spin_unlock(&local->txfidlock);
1770 if (idx >= PRISM2_TXFID_COUNT)
1772 } while (idx != local->next_txfid);
1773 spin_unlock(&local->txfidlock);
1775 /* no empty TX buffers, stop queue */
1776 netif_stop_queue(dev);
1780 /* Called only from software IRQ if PCI bus master is not used (with bus master
1781 * this can be called both from software and hardware IRQ) */
1782 static int prism2_transmit(struct net_device *dev, int idx)
1784 struct hostap_interface *iface;
1785 local_info_t *local;
1788 iface = netdev_priv(dev);
1789 local = iface->local;
1791 /* The driver tries to stop netif queue so that there would not be
1792 * more than one attempt to transmit frames going on; check that this
1793 * is really the case */
1795 if (test_and_set_bit(HOSTAP_BITS_TRANSMIT, &local->bits)) {
1796 printk(KERN_DEBUG "%s: driver bug - prism2_transmit() called "
1797 "when previous TX was pending\n", dev->name);
1801 /* stop the queue for the time that transmit is pending */
1802 netif_stop_queue(dev);
1804 /* transmit packet */
1805 res = hfa384x_cmd_callback(
1807 HFA384X_CMDCODE_TRANSMIT | HFA384X_CMD_TX_RECLAIM,
1809 prism2_transmit_cb, (long) idx);
1812 struct net_device_stats *stats;
1813 printk(KERN_DEBUG "%s: prism2_transmit: CMDCODE_TRANSMIT "
1814 "failed (res=%d)\n", dev->name, res);
1815 stats = hostap_get_stats(dev);
1816 stats->tx_dropped++;
1817 netif_wake_queue(dev);
1820 dev->trans_start = jiffies;
1822 /* Since we did not wait for command completion, the card continues
1823 * to process on the background and we will finish handling when
1824 * command completion event is handled (prism2_cmd_ev() function) */
1830 #if defined(PRISM2_PCI) && defined(PRISM2_BUS_MASTER)
1831 /* Called only from hardware IRQ */
1832 static void prism2_tx_cb(struct net_device *dev, void *context,
1835 struct hostap_interface *iface;
1836 local_info_t *local;
1838 int buf_len = (int) context;
1840 iface = netdev_priv(dev);
1841 local = iface->local;
1844 printk(KERN_DEBUG "%s: prism2_tx_cb - res=0x%02x\n",
1849 addr = virt_to_phys(local->bus_m0_buf);
1850 HFA384X_OUTW((addr & 0xffff0000) >> 16, HFA384X_PCI_M0_ADDRH_OFF);
1851 HFA384X_OUTW(addr & 0x0000ffff, HFA384X_PCI_M0_ADDRL_OFF);
1852 HFA384X_OUTW(buf_len / 2, HFA384X_PCI_M0_LEN_OFF);
1853 HFA384X_OUTW(HFA384X_PCI_CTL_TO_BAP, HFA384X_PCI_M0_CTL_OFF);
1855 #endif /* PRISM2_PCI and PRISM2_BUS_MASTER */
1858 /* Send IEEE 802.11 frame (convert the header into Prism2 TX descriptor and
1859 * send the payload with this descriptor) */
1860 /* Called only from software IRQ */
1861 static int prism2_tx_80211(struct sk_buff *skb, struct net_device *dev)
1863 struct hostap_interface *iface;
1864 local_info_t *local;
1865 struct hfa384x_tx_frame txdesc;
1866 struct hostap_ieee80211_hdr *hdr;
1867 struct hostap_skb_tx_data *meta;
1868 int hdr_len, data_len, idx, res, ret = -1;
1871 iface = netdev_priv(dev);
1872 local = iface->local;
1874 meta = (struct hostap_skb_tx_data *) skb->cb;
1875 hdr = (struct hostap_ieee80211_hdr *) skb->data;
1877 prism2_callback(local, PRISM2_CALLBACK_TX_START);
1879 if ((local->func->card_present && !local->func->card_present(local)) ||
1880 !local->hw_ready || local->hw_downloading || local->pri_only) {
1881 if (net_ratelimit()) {
1882 printk(KERN_DEBUG "%s: prism2_tx_80211: hw not ready -"
1883 " skipping\n", dev->name);
1888 memset(&txdesc, 0, sizeof(txdesc));
1890 /* skb->data starts with txdesc->frame_control */
1892 memcpy(&txdesc.frame_control, skb->data, hdr_len);
1893 fc = le16_to_cpu(txdesc.frame_control);
1894 if (HOSTAP_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA &&
1895 (fc & WLAN_FC_FROMDS) && (fc & WLAN_FC_TODS) && skb->len >= 30) {
1897 memcpy(txdesc.addr4, skb->data + hdr_len, ETH_ALEN);
1898 hdr_len += ETH_ALEN;
1901 tx_control = local->tx_control;
1902 if (meta->tx_cb_idx) {
1903 tx_control |= HFA384X_TX_CTRL_TX_OK;
1904 txdesc.sw_support = cpu_to_le16(meta->tx_cb_idx);
1906 txdesc.tx_control = cpu_to_le16(tx_control);
1907 txdesc.tx_rate = meta->rate;
1909 data_len = skb->len - hdr_len;
1910 txdesc.data_len = cpu_to_le16(data_len);
1911 txdesc.len = cpu_to_be16(data_len);
1913 idx = prism2_get_txfid_idx(local);
1917 if (local->frame_dump & PRISM2_DUMP_TX_HDR)
1918 hostap_dump_tx_header(dev->name, &txdesc);
1920 spin_lock(&local->baplock);
1921 res = hfa384x_setup_bap(dev, BAP0, local->txfid[idx], 0);
1923 #if defined(PRISM2_PCI) && defined(PRISM2_BUS_MASTER)
1924 if (!res && skb->len >= local->bus_master_threshold_tx) {
1928 local->bus_m0_tx_idx = idx;
1930 /* FIX: BAP0 should be locked during bus master transfer, but
1931 * baplock with BH's disabled is not OK for this; netif queue
1932 * stopping is not enough since BAP0 is used also for RID
1935 /* stop the queue for the time that bus mastering on BAP0 is
1937 netif_stop_queue(dev);
1939 spin_unlock(&local->baplock);
1941 /* Copy frame data to bus_m0_buf */
1942 pos = local->bus_m0_buf;
1943 memcpy(pos, &txdesc, sizeof(txdesc));
1944 pos += sizeof(txdesc);
1945 memcpy(pos, skb->data + hdr_len, skb->len - hdr_len);
1946 pos += skb->len - hdr_len;
1947 buf_len = pos - local->bus_m0_buf;
1951 #ifdef PRISM2_ENABLE_BEFORE_TX_BUS_MASTER
1952 /* Any RX packet seems to break something with TX bus
1953 * mastering; enable command is enough to fix this.. */
1954 if (hfa384x_cmd_callback(dev, HFA384X_CMDCODE_ENABLE, 0,
1955 prism2_tx_cb, (long) buf_len)) {
1956 printk(KERN_DEBUG "%s: TX: enable port0 failed\n",
1959 #else /* PRISM2_ENABLE_BEFORE_TX_BUS_MASTER */
1960 prism2_tx_cb(dev, (void *) buf_len, 0, 0);
1961 #endif /* PRISM2_ENABLE_BEFORE_TX_BUS_MASTER */
1963 /* Bus master transfer will be started from command completion
1964 * event handler and TX handling will be finished by calling
1965 * prism2_transmit() from bus master event handler */
1968 #endif /* PRISM2_PCI and PRISM2_BUS_MASTER */
1971 res = hfa384x_to_bap(dev, BAP0, &txdesc, sizeof(txdesc));
1973 res = hfa384x_to_bap(dev, BAP0, skb->data + hdr_len,
1974 skb->len - hdr_len);
1975 spin_unlock(&local->baplock);
1978 res = prism2_transmit(dev, idx);
1980 printk(KERN_DEBUG "%s: prism2_tx_80211 - to BAP0 failed\n",
1982 local->intransmitfid[idx] = PRISM2_TXFID_EMPTY;
1983 schedule_work(&local->reset_queue);
1990 prism2_callback(local, PRISM2_CALLBACK_TX_END);
1995 /* Some SMP systems have reported number of odd errors with hostap_pci. fid
1996 * register has changed values between consecutive reads for an unknown reason.
1997 * This should really not happen, so more debugging is needed. This test
1998 * version is a big slower, but it will detect most of such register changes
1999 * and will try to get the correct fid eventually. */
2000 #define EXTRA_FID_READ_TESTS
2002 static inline u16 prism2_read_fid_reg(struct net_device *dev, u16 reg)
2004 #ifdef EXTRA_FID_READ_TESTS
2005 u16 val, val2, val3;
2008 for (i = 0; i < 10; i++) {
2009 val = HFA384X_INW(reg);
2010 val2 = HFA384X_INW(reg);
2011 val3 = HFA384X_INW(reg);
2013 if (val == val2 && val == val3)
2016 printk(KERN_DEBUG "%s: detected fid change (try=%d, reg=%04x):"
2017 " %04x %04x %04x\n",
2018 dev->name, i, reg, val, val2, val3);
2019 if ((val == val2 || val == val3) && val != 0)
2021 if (val2 == val3 && val2 != 0)
2024 printk(KERN_WARNING "%s: Uhhuh.. could not read good fid from reg "
2025 "%04x (%04x %04x %04x)\n", dev->name, reg, val, val2, val3);
2027 #else /* EXTRA_FID_READ_TESTS */
2028 return HFA384X_INW(reg);
2029 #endif /* EXTRA_FID_READ_TESTS */
2033 /* Called only as a tasklet (software IRQ) */
2034 static void prism2_rx(local_info_t *local)
2036 struct net_device *dev = local->dev;
2037 int res, rx_pending = 0;
2038 u16 len, hdr_len, rxfid, status, macport;
2039 struct net_device_stats *stats;
2040 struct hfa384x_rx_frame rxdesc;
2041 struct sk_buff *skb = NULL;
2043 prism2_callback(local, PRISM2_CALLBACK_RX_START);
2044 stats = hostap_get_stats(dev);
2046 rxfid = prism2_read_fid_reg(dev, HFA384X_RXFID_OFF);
2047 #ifndef final_version
2049 rxfid = HFA384X_INW(HFA384X_RXFID_OFF);
2050 printk(KERN_DEBUG "prism2_rx: rxfid=0 (next 0x%04x)\n",
2053 schedule_work(&local->reset_queue);
2056 /* try to continue with the new rxfid value */
2060 spin_lock(&local->baplock);
2061 res = hfa384x_setup_bap(dev, BAP0, rxfid, 0);
2063 res = hfa384x_from_bap(dev, BAP0, &rxdesc, sizeof(rxdesc));
2066 spin_unlock(&local->baplock);
2067 printk(KERN_DEBUG "%s: copy from BAP0 failed %d\n", dev->name,
2069 if (res == -ETIMEDOUT) {
2070 schedule_work(&local->reset_queue);
2075 len = le16_to_cpu(rxdesc.data_len);
2076 hdr_len = sizeof(rxdesc);
2077 status = le16_to_cpu(rxdesc.status);
2078 macport = (status >> 8) & 0x07;
2080 /* Drop frames with too large reported payload length. Monitor mode
2081 * seems to sometimes pass frames (e.g., ctrl::ack) with signed and
2082 * negative value, so allow also values 65522 .. 65534 (-14 .. -2) for
2084 if (len > PRISM2_DATA_MAXLEN + 8 /* WEP */) {
2085 if (macport == 7 && local->iw_mode == IW_MODE_MONITOR) {
2086 if (len >= (u16) -14) {
2087 hdr_len -= 65535 - len;
2092 spin_unlock(&local->baplock);
2093 printk(KERN_DEBUG "%s: Received frame with invalid "
2094 "length 0x%04x\n", dev->name, len);
2095 hostap_dump_rx_header(dev->name, &rxdesc);
2100 skb = dev_alloc_skb(len + hdr_len);
2102 spin_unlock(&local->baplock);
2103 printk(KERN_DEBUG "%s: RX failed to allocate skb\n",
2108 memcpy(skb_put(skb, hdr_len), &rxdesc, hdr_len);
2110 #if defined(PRISM2_PCI) && defined(PRISM2_BUS_MASTER)
2111 if (len >= local->bus_master_threshold_rx) {
2114 hfa384x_events_no_bap1(dev);
2116 local->rx_skb = skb;
2117 /* Internal BAP0 offset points to the byte following rxdesc;
2118 * copy rest of the data using bus master */
2119 addr = virt_to_phys(skb_put(skb, len));
2120 HFA384X_OUTW((addr & 0xffff0000) >> 16,
2121 HFA384X_PCI_M0_ADDRH_OFF);
2122 HFA384X_OUTW(addr & 0x0000ffff, HFA384X_PCI_M0_ADDRL_OFF);
2125 HFA384X_OUTW(len / 2, HFA384X_PCI_M0_LEN_OFF);
2126 HFA384X_OUTW(HFA384X_PCI_CTL_FROM_BAP, HFA384X_PCI_M0_CTL_OFF);
2128 /* pci_bus_m1 event will be generated when data transfer is
2129 * complete and the frame will then be added to rx_list and
2130 * rx_tasklet is scheduled */
2133 /* Have to release baplock before returning, although BAP0
2134 * should really not be used before DMA transfer has been
2136 spin_unlock(&local->baplock);
2138 #endif /* PRISM2_PCI and PRISM2_BUS_MASTER */
2141 res = hfa384x_from_bap(dev, BAP0, skb_put(skb, len),
2143 spin_unlock(&local->baplock);
2145 printk(KERN_DEBUG "%s: RX failed to read "
2146 "frame data\n", dev->name);
2150 skb_queue_tail(&local->rx_list, skb);
2151 tasklet_schedule(&local->rx_tasklet);
2155 prism2_callback(local, PRISM2_CALLBACK_RX_END);
2157 HFA384X_OUTW(HFA384X_EV_RX, HFA384X_EVACK_OFF);
2163 stats->rx_dropped++;
2170 /* Called only as a tasklet (software IRQ) */
2171 static void hostap_rx_skb(local_info_t *local, struct sk_buff *skb)
2173 struct hfa384x_rx_frame *rxdesc;
2174 struct net_device *dev = skb->dev;
2175 struct hostap_80211_rx_status stats;
2176 int hdrlen, rx_hdrlen;
2178 rx_hdrlen = sizeof(*rxdesc);
2179 if (skb->len < sizeof(*rxdesc)) {
2180 /* Allow monitor mode to receive shorter frames */
2181 if (local->iw_mode == IW_MODE_MONITOR &&
2182 skb->len >= sizeof(*rxdesc) - 30) {
2183 rx_hdrlen = skb->len;
2190 rxdesc = (struct hfa384x_rx_frame *) skb->data;
2192 if (local->frame_dump & PRISM2_DUMP_RX_HDR &&
2193 skb->len >= sizeof(*rxdesc))
2194 hostap_dump_rx_header(dev->name, rxdesc);
2196 if (le16_to_cpu(rxdesc->status) & HFA384X_RX_STATUS_FCSERR &&
2197 (!local->monitor_allow_fcserr ||
2198 local->iw_mode != IW_MODE_MONITOR))
2201 if (skb->len > PRISM2_DATA_MAXLEN) {
2202 printk(KERN_DEBUG "%s: RX: len(%d) > MAX(%d)\n",
2203 dev->name, skb->len, PRISM2_DATA_MAXLEN);
2207 stats.mac_time = le32_to_cpu(rxdesc->time);
2208 stats.signal = rxdesc->signal - local->rssi_to_dBm;
2209 stats.noise = rxdesc->silence - local->rssi_to_dBm;
2210 stats.rate = rxdesc->rate;
2212 /* Convert Prism2 RX structure into IEEE 802.11 header */
2213 hdrlen = hostap_80211_get_hdrlen(le16_to_cpu(rxdesc->frame_control));
2214 if (hdrlen > rx_hdrlen)
2217 memmove(skb_pull(skb, rx_hdrlen - hdrlen),
2218 &rxdesc->frame_control, hdrlen);
2220 hostap_80211_rx(dev, skb, &stats);
2228 /* Called only as a tasklet (software IRQ) */
2229 static void hostap_rx_tasklet(unsigned long data)
2231 local_info_t *local = (local_info_t *) data;
2232 struct sk_buff *skb;
2234 while ((skb = skb_dequeue(&local->rx_list)) != NULL)
2235 hostap_rx_skb(local, skb);
2239 /* Called only from hardware IRQ */
2240 static void prism2_alloc_ev(struct net_device *dev)
2242 struct hostap_interface *iface;
2243 local_info_t *local;
2247 iface = netdev_priv(dev);
2248 local = iface->local;
2250 fid = prism2_read_fid_reg(dev, HFA384X_ALLOCFID_OFF);
2252 PDEBUG(DEBUG_FID, "FID: interrupt: ALLOC - fid=0x%04x\n", fid);
2254 spin_lock(&local->txfidlock);
2255 idx = local->next_alloc;
2258 if (local->txfid[idx] == fid) {
2259 PDEBUG(DEBUG_FID, "FID: found matching txfid[%d]\n",
2262 #ifndef final_version
2263 if (local->intransmitfid[idx] == PRISM2_TXFID_EMPTY)
2264 printk("Already released txfid found at idx "
2266 if (local->intransmitfid[idx] == PRISM2_TXFID_RESERVED)
2267 printk("Already reserved txfid found at idx "
2270 local->intransmitfid[idx] = PRISM2_TXFID_EMPTY;
2272 local->next_alloc = idx >= PRISM2_TXFID_COUNT ? 0 :
2275 if (!test_bit(HOSTAP_BITS_TRANSMIT, &local->bits) &&
2276 netif_queue_stopped(dev))
2277 netif_wake_queue(dev);
2279 spin_unlock(&local->txfidlock);
2284 if (idx >= PRISM2_TXFID_COUNT)
2286 } while (idx != local->next_alloc);
2288 printk(KERN_WARNING "%s: could not find matching txfid (0x%04x, new "
2289 "read 0x%04x) for alloc event\n", dev->name, fid,
2290 HFA384X_INW(HFA384X_ALLOCFID_OFF));
2291 printk(KERN_DEBUG "TXFIDs:");
2292 for (idx = 0; idx < PRISM2_TXFID_COUNT; idx++)
2293 printk(" %04x[%04x]", local->txfid[idx],
2294 local->intransmitfid[idx]);
2296 spin_unlock(&local->txfidlock);
2298 /* FIX: should probably schedule reset; reference to one txfid was lost
2299 * completely.. Bad things will happen if we run out of txfids
2300 * Actually, this will cause netdev watchdog to notice TX timeout and
2301 * then card reset after all txfids have been leaked. */
2305 /* Called only as a tasklet (software IRQ) */
2306 static void hostap_tx_callback(local_info_t *local,
2307 struct hfa384x_tx_frame *txdesc, int ok,
2310 u16 sw_support, hdrlen, len;
2311 struct sk_buff *skb;
2312 struct hostap_tx_callback_info *cb;
2314 /* Make sure that frame was from us. */
2315 if (memcmp(txdesc->addr2, local->dev->dev_addr, ETH_ALEN)) {
2316 printk(KERN_DEBUG "%s: TX callback - foreign frame\n",
2321 sw_support = le16_to_cpu(txdesc->sw_support);
2323 spin_lock(&local->lock);
2324 cb = local->tx_callback;
2325 while (cb != NULL && cb->idx != sw_support)
2327 spin_unlock(&local->lock);
2330 printk(KERN_DEBUG "%s: could not find TX callback (idx %d)\n",
2331 local->dev->name, sw_support);
2335 hdrlen = hostap_80211_get_hdrlen(le16_to_cpu(txdesc->frame_control));
2336 len = le16_to_cpu(txdesc->data_len);
2337 skb = dev_alloc_skb(hdrlen + len);
2339 printk(KERN_DEBUG "%s: hostap_tx_callback failed to allocate "
2340 "skb\n", local->dev->name);
2344 memcpy(skb_put(skb, hdrlen), (void *) &txdesc->frame_control, hdrlen);
2346 memcpy(skb_put(skb, len), payload, len);
2348 skb->dev = local->dev;
2349 skb->mac.raw = skb->data;
2351 cb->func(skb, ok, cb->data);
2355 /* Called only as a tasklet (software IRQ) */
2356 static int hostap_tx_compl_read(local_info_t *local, int error,
2357 struct hfa384x_tx_frame *txdesc,
2362 struct net_device *dev = local->dev;
2364 fid = prism2_read_fid_reg(dev, HFA384X_TXCOMPLFID_OFF);
2366 PDEBUG(DEBUG_FID, "interrupt: TX (err=%d) - fid=0x%04x\n", fid, error);
2368 spin_lock(&local->baplock);
2369 res = hfa384x_setup_bap(dev, BAP0, fid, 0);
2371 res = hfa384x_from_bap(dev, BAP0, txdesc, sizeof(*txdesc));
2373 PDEBUG(DEBUG_EXTRA, "%s: TX (err=%d) - fid=0x%04x - could not "
2374 "read txdesc\n", dev->name, error, fid);
2375 if (res == -ETIMEDOUT) {
2376 schedule_work(&local->reset_queue);
2381 if (txdesc->sw_support) {
2382 len = le16_to_cpu(txdesc->data_len);
2383 if (len < PRISM2_DATA_MAXLEN) {
2384 *payload = (char *) kmalloc(len, GFP_ATOMIC);
2385 if (*payload == NULL ||
2386 hfa384x_from_bap(dev, BAP0, *payload, len)) {
2387 PDEBUG(DEBUG_EXTRA, "%s: could not read TX "
2388 "frame payload\n", dev->name);
2398 spin_unlock(&local->baplock);
2404 /* Called only as a tasklet (software IRQ) */
2405 static void prism2_tx_ev(local_info_t *local)
2407 struct net_device *dev = local->dev;
2408 char *payload = NULL;
2409 struct hfa384x_tx_frame txdesc;
2411 if (hostap_tx_compl_read(local, 0, &txdesc, &payload))
2414 if (local->frame_dump & PRISM2_DUMP_TX_HDR) {
2415 PDEBUG(DEBUG_EXTRA, "%s: TX - status=0x%04x "
2416 "retry_count=%d tx_rate=%d seq_ctrl=%d "
2418 dev->name, le16_to_cpu(txdesc.status),
2419 txdesc.retry_count, txdesc.tx_rate,
2420 le16_to_cpu(txdesc.seq_ctrl),
2421 le16_to_cpu(txdesc.duration_id));
2424 if (txdesc.sw_support)
2425 hostap_tx_callback(local, &txdesc, 1, payload);
2429 HFA384X_OUTW(HFA384X_EV_TX, HFA384X_EVACK_OFF);
2433 /* Called only as a tasklet (software IRQ) */
2434 static void hostap_sta_tx_exc_tasklet(unsigned long data)
2436 local_info_t *local = (local_info_t *) data;
2437 struct sk_buff *skb;
2439 while ((skb = skb_dequeue(&local->sta_tx_exc_list)) != NULL) {
2440 struct hfa384x_tx_frame *txdesc =
2441 (struct hfa384x_tx_frame *) skb->data;
2443 if (skb->len >= sizeof(*txdesc)) {
2444 /* Convert Prism2 RX structure into IEEE 802.11 header
2446 u16 fc = le16_to_cpu(txdesc->frame_control);
2447 int hdrlen = hostap_80211_get_hdrlen(fc);
2448 memmove(skb_pull(skb, sizeof(*txdesc) - hdrlen),
2449 &txdesc->frame_control, hdrlen);
2451 hostap_handle_sta_tx_exc(local, skb);
2458 /* Called only as a tasklet (software IRQ) */
2459 static void prism2_txexc(local_info_t *local)
2461 struct net_device *dev = local->dev;
2464 char *payload = NULL;
2465 struct hfa384x_tx_frame txdesc;
2467 show_dump = local->frame_dump & PRISM2_DUMP_TXEXC_HDR;
2468 local->stats.tx_errors++;
2470 res = hostap_tx_compl_read(local, 1, &txdesc, &payload);
2471 HFA384X_OUTW(HFA384X_EV_TXEXC, HFA384X_EVACK_OFF);
2475 status = le16_to_cpu(txdesc.status);
2477 /* We produce a TXDROP event only for retry or lifetime
2478 * exceeded, because that's the only status that really mean
2479 * that this particular node went away.
2480 * Other errors means that *we* screwed up. - Jean II */
2481 if (status & (HFA384X_TX_STATUS_RETRYERR | HFA384X_TX_STATUS_AGEDERR))
2483 union iwreq_data wrqu;
2485 /* Copy 802.11 dest address. */
2486 memcpy(wrqu.addr.sa_data, txdesc.addr1, ETH_ALEN);
2487 wrqu.addr.sa_family = ARPHRD_ETHER;
2488 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
2492 if (local->iw_mode == IW_MODE_MASTER ||
2493 local->iw_mode == IW_MODE_REPEAT ||
2494 local->wds_type & HOSTAP_WDS_AP_CLIENT) {
2495 struct sk_buff *skb;
2496 skb = dev_alloc_skb(sizeof(txdesc));
2498 memcpy(skb_put(skb, sizeof(txdesc)), &txdesc,
2500 skb_queue_tail(&local->sta_tx_exc_list, skb);
2501 tasklet_schedule(&local->sta_tx_exc_tasklet);
2505 if (txdesc.sw_support)
2506 hostap_tx_callback(local, &txdesc, 0, payload);
2512 PDEBUG(DEBUG_EXTRA, "%s: TXEXC - status=0x%04x (%s%s%s%s)"
2513 " tx_control=%04x\n",
2515 status & HFA384X_TX_STATUS_RETRYERR ? "[RetryErr]" : "",
2516 status & HFA384X_TX_STATUS_AGEDERR ? "[AgedErr]" : "",
2517 status & HFA384X_TX_STATUS_DISCON ? "[Discon]" : "",
2518 status & HFA384X_TX_STATUS_FORMERR ? "[FormErr]" : "",
2519 le16_to_cpu(txdesc.tx_control));
2521 fc = le16_to_cpu(txdesc.frame_control);
2522 PDEBUG(DEBUG_EXTRA, " retry_count=%d tx_rate=%d fc=0x%04x "
2523 "(%s%s%s::%d%s%s)\n",
2524 txdesc.retry_count, txdesc.tx_rate, fc,
2525 HOSTAP_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT ? "Mgmt" : "",
2526 HOSTAP_FC_GET_TYPE(fc) == WLAN_FC_TYPE_CTRL ? "Ctrl" : "",
2527 HOSTAP_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA ? "Data" : "",
2528 HOSTAP_FC_GET_STYPE(fc),
2529 fc & WLAN_FC_TODS ? " ToDS" : "",
2530 fc & WLAN_FC_FROMDS ? " FromDS" : "");
2531 PDEBUG(DEBUG_EXTRA, " A1=" MACSTR " A2=" MACSTR " A3="
2532 MACSTR " A4=" MACSTR "\n",
2533 MAC2STR(txdesc.addr1), MAC2STR(txdesc.addr2),
2534 MAC2STR(txdesc.addr3), MAC2STR(txdesc.addr4));
2538 /* Called only as a tasklet (software IRQ) */
2539 static void hostap_info_tasklet(unsigned long data)
2541 local_info_t *local = (local_info_t *) data;
2542 struct sk_buff *skb;
2544 while ((skb = skb_dequeue(&local->info_list)) != NULL) {
2545 hostap_info_process(local, skb);
2551 /* Called only as a tasklet (software IRQ) */
2552 static void prism2_info(local_info_t *local)
2554 struct net_device *dev = local->dev;
2557 struct hfa384x_info_frame info;
2558 struct sk_buff *skb;
2560 fid = HFA384X_INW(HFA384X_INFOFID_OFF);
2562 spin_lock(&local->baplock);
2563 res = hfa384x_setup_bap(dev, BAP0, fid, 0);
2565 res = hfa384x_from_bap(dev, BAP0, &info, sizeof(info));
2567 spin_unlock(&local->baplock);
2568 printk(KERN_DEBUG "Could not get info frame (fid=0x%04x)\n",
2570 if (res == -ETIMEDOUT) {
2571 schedule_work(&local->reset_queue);
2576 le16_to_cpus(&info.len);
2577 le16_to_cpus(&info.type);
2578 left = (info.len - 1) * 2;
2580 if (info.len & 0x8000 || info.len == 0 || left > 2060) {
2581 /* data register seems to give 0x8000 in some error cases even
2582 * though busy bit is not set in offset register;
2583 * in addition, length must be at least 1 due to type field */
2584 spin_unlock(&local->baplock);
2585 printk(KERN_DEBUG "%s: Received info frame with invalid "
2586 "length 0x%04x (type 0x%04x)\n", dev->name, info.len,
2591 skb = dev_alloc_skb(sizeof(info) + left);
2593 spin_unlock(&local->baplock);
2594 printk(KERN_DEBUG "%s: Could not allocate skb for info "
2595 "frame\n", dev->name);
2599 memcpy(skb_put(skb, sizeof(info)), &info, sizeof(info));
2600 if (left > 0 && hfa384x_from_bap(dev, BAP0, skb_put(skb, left), left))
2602 spin_unlock(&local->baplock);
2603 printk(KERN_WARNING "%s: Info frame read failed (fid=0x%04x, "
2604 "len=0x%04x, type=0x%04x\n",
2605 dev->name, fid, info.len, info.type);
2609 spin_unlock(&local->baplock);
2611 skb_queue_tail(&local->info_list, skb);
2612 tasklet_schedule(&local->info_tasklet);
2615 HFA384X_OUTW(HFA384X_EV_INFO, HFA384X_EVACK_OFF);
2619 /* Called only as a tasklet (software IRQ) */
2620 static void hostap_bap_tasklet(unsigned long data)
2622 local_info_t *local = (local_info_t *) data;
2623 struct net_device *dev = local->dev;
2627 if (local->func->card_present && !local->func->card_present(local))
2630 set_bit(HOSTAP_BITS_BAP_TASKLET, &local->bits);
2632 /* Process all pending BAP events without generating new interrupts
2634 while (frames-- > 0) {
2635 ev = HFA384X_INW(HFA384X_EVSTAT_OFF);
2636 if (ev == 0xffff || !(ev & HFA384X_BAP0_EVENTS))
2638 if (ev & HFA384X_EV_RX)
2640 if (ev & HFA384X_EV_INFO)
2642 if (ev & HFA384X_EV_TX)
2643 prism2_tx_ev(local);
2644 if (ev & HFA384X_EV_TXEXC)
2645 prism2_txexc(local);
2648 set_bit(HOSTAP_BITS_BAP_TASKLET2, &local->bits);
2649 clear_bit(HOSTAP_BITS_BAP_TASKLET, &local->bits);
2651 /* Enable interrupts for new BAP events */
2652 hfa384x_events_all(dev);
2653 clear_bit(HOSTAP_BITS_BAP_TASKLET2, &local->bits);
2657 #if defined(PRISM2_PCI) && defined(PRISM2_BUS_MASTER)
2658 /* Called only from hardware IRQ */
2659 static void prism2_bus_master_ev(struct net_device *dev, int bap)
2661 struct hostap_interface *iface;
2662 local_info_t *local;
2664 iface = netdev_priv(dev);
2665 local = iface->local;
2668 /* FIX: frame payload was DMA'd to skb->data; might need to
2669 * invalidate data cache for that memory area */
2670 skb_queue_tail(&local->rx_list, local->rx_skb);
2671 tasklet_schedule(&local->rx_tasklet);
2672 HFA384X_OUTW(HFA384X_EV_RX, HFA384X_EVACK_OFF);
2674 if (prism2_transmit(dev, local->bus_m0_tx_idx)) {
2675 printk(KERN_DEBUG "%s: prism2_transmit() failed "
2676 "when called from bus master event\n",
2678 local->intransmitfid[local->bus_m0_tx_idx] =
2680 schedule_work(&local->reset_queue);
2684 #endif /* PRISM2_PCI and PRISM2_BUS_MASTER */
2687 /* Called only from hardware IRQ */
2688 static void prism2_infdrop(struct net_device *dev)
2690 static unsigned long last_inquire = 0;
2692 PDEBUG(DEBUG_EXTRA, "%s: INFDROP event\n", dev->name);
2694 /* some firmware versions seem to get stuck with
2695 * full CommTallies in high traffic load cases; every
2696 * packet will then cause INFDROP event and CommTallies
2697 * info frame will not be sent automatically. Try to
2698 * get out of this state by inquiring CommTallies. */
2699 if (!last_inquire || time_after(jiffies, last_inquire + HZ)) {
2700 hfa384x_cmd_callback(dev, HFA384X_CMDCODE_INQUIRE,
2701 HFA384X_INFO_COMMTALLIES, NULL, 0);
2702 last_inquire = jiffies;
2707 /* Called only from hardware IRQ */
2708 static void prism2_ev_tick(struct net_device *dev)
2710 struct hostap_interface *iface;
2711 local_info_t *local;
2713 static int prev_stuck = 0;
2715 iface = netdev_priv(dev);
2716 local = iface->local;
2718 if (time_after(jiffies, local->last_tick_timer + 5 * HZ) &&
2719 local->last_tick_timer) {
2720 evstat = HFA384X_INW(HFA384X_EVSTAT_OFF);
2721 inten = HFA384X_INW(HFA384X_INTEN_OFF);
2723 printk(KERN_INFO "%s: SW TICK stuck? "
2724 "bits=0x%lx EvStat=%04x IntEn=%04x\n",
2725 dev->name, local->bits, evstat, inten);
2727 local->sw_tick_stuck++;
2728 if ((evstat & HFA384X_BAP0_EVENTS) &&
2729 (inten & HFA384X_BAP0_EVENTS)) {
2730 printk(KERN_INFO "%s: trying to recover from IRQ "
2731 "hang\n", dev->name);
2732 hfa384x_events_no_bap0(dev);
2740 /* Called only from hardware IRQ */
2741 static inline void prism2_check_magic(local_info_t *local)
2743 /* at least PCI Prism2.5 with bus mastering seems to sometimes
2744 * return 0x0000 in SWSUPPORT0 for unknown reason, but re-reading the
2745 * register once or twice seems to get the correct value.. PCI cards
2746 * cannot anyway be removed during normal operation, so there is not
2747 * really any need for this verification with them. */
2750 #ifndef final_version
2751 static unsigned long last_magic_err = 0;
2752 struct net_device *dev = local->dev;
2754 if (HFA384X_INW(HFA384X_SWSUPPORT0_OFF) != HFA384X_MAGIC) {
2755 if (!local->hw_ready)
2757 HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
2758 if (time_after(jiffies, last_magic_err + 10 * HZ)) {
2759 printk("%s: Interrupt, but SWSUPPORT0 does not match: "
2760 "%04X != %04X - card removed?\n", dev->name,
2761 HFA384X_INW(HFA384X_SWSUPPORT0_OFF),
2763 last_magic_err = jiffies;
2764 } else if (net_ratelimit()) {
2765 printk(KERN_DEBUG "%s: interrupt - SWSUPPORT0=%04x "
2766 "MAGIC=%04x\n", dev->name,
2767 HFA384X_INW(HFA384X_SWSUPPORT0_OFF),
2770 if (HFA384X_INW(HFA384X_SWSUPPORT0_OFF) != 0xffff)
2771 schedule_work(&local->reset_queue);
2774 #endif /* final_version */
2775 #endif /* !PRISM2_PCI */
2779 /* Called only from hardware IRQ */
2780 static irqreturn_t prism2_interrupt(int irq, void *dev_id, struct pt_regs *regs)
2782 struct net_device *dev = (struct net_device *) dev_id;
2783 struct hostap_interface *iface;
2784 local_info_t *local;
2788 iface = netdev_priv(dev);
2789 local = iface->local;
2791 prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_INTERRUPT, 0, 0);
2793 if (local->func->card_present && !local->func->card_present(local)) {
2794 if (net_ratelimit()) {
2795 printk(KERN_DEBUG "%s: Interrupt, but dev not OK\n",
2801 prism2_check_magic(local);
2804 ev = HFA384X_INW(HFA384X_EVSTAT_OFF);
2806 if (local->shutdown)
2808 HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
2809 printk(KERN_DEBUG "%s: prism2_interrupt: ev=0xffff\n",
2814 ev &= HFA384X_INW(HFA384X_INTEN_OFF);
2818 if (ev & HFA384X_EV_CMD) {
2822 /* Above events are needed even before hw is ready, but other
2823 * events should be skipped during initialization. This may
2824 * change for AllocEv if allocate_fid is implemented without
2826 if (!local->hw_ready || local->hw_resetting ||
2827 !local->dev_enabled) {
2828 ev = HFA384X_INW(HFA384X_EVSTAT_OFF);
2829 if (ev & HFA384X_EV_CMD)
2831 if ((ev & HFA384X_EVENT_MASK) == 0)
2833 if (local->dev_enabled && (ev & ~HFA384X_EV_TICK) &&
2835 printk(KERN_DEBUG "%s: prism2_interrupt: hw "
2836 "not ready; skipping events 0x%04x "
2837 "(IntEn=0x%04x)%s%s%s\n",
2839 HFA384X_INW(HFA384X_INTEN_OFF),
2840 !local->hw_ready ? " (!hw_ready)" : "",
2841 local->hw_resetting ?
2842 " (hw_resetting)" : "",
2843 !local->dev_enabled ?
2844 " (!dev_enabled)" : "");
2846 HFA384X_OUTW(ev, HFA384X_EVACK_OFF);
2850 if (ev & HFA384X_EV_TICK) {
2851 prism2_ev_tick(dev);
2852 HFA384X_OUTW(HFA384X_EV_TICK, HFA384X_EVACK_OFF);
2855 #if defined(PRISM2_PCI) && defined(PRISM2_BUS_MASTER)
2856 if (ev & HFA384X_EV_PCI_M0) {
2857 prism2_bus_master_ev(dev, BAP0);
2858 HFA384X_OUTW(HFA384X_EV_PCI_M0, HFA384X_EVACK_OFF);
2861 if (ev & HFA384X_EV_PCI_M1) {
2862 /* previous RX has been copied can be ACKed now */
2863 HFA384X_OUTW(HFA384X_EV_RX, HFA384X_EVACK_OFF);
2865 prism2_bus_master_ev(dev, BAP1);
2866 HFA384X_OUTW(HFA384X_EV_PCI_M1, HFA384X_EVACK_OFF);
2868 #endif /* PRISM2_PCI and PRISM2_BUS_MASTER */
2870 if (ev & HFA384X_EV_ALLOC) {
2871 prism2_alloc_ev(dev);
2872 HFA384X_OUTW(HFA384X_EV_ALLOC, HFA384X_EVACK_OFF);
2875 /* Reading data from the card is quite time consuming, so do it
2876 * in tasklets. TX, TXEXC, RX, and INFO events will be ACKed
2877 * and unmasked after needed data has been read completely. */
2878 if (ev & HFA384X_BAP0_EVENTS) {
2879 hfa384x_events_no_bap0(dev);
2880 tasklet_schedule(&local->bap_tasklet);
2883 #ifndef final_version
2884 if (ev & HFA384X_EV_WTERR) {
2885 PDEBUG(DEBUG_EXTRA, "%s: WTERR event\n", dev->name);
2886 HFA384X_OUTW(HFA384X_EV_WTERR, HFA384X_EVACK_OFF);
2888 #endif /* final_version */
2890 if (ev & HFA384X_EV_INFDROP) {
2891 prism2_infdrop(dev);
2892 HFA384X_OUTW(HFA384X_EV_INFDROP, HFA384X_EVACK_OFF);
2897 if (events >= PRISM2_MAX_INTERRUPT_EVENTS) {
2898 PDEBUG(DEBUG_EXTRA, "prism2_interrupt: >%d events "
2899 "(EvStat=0x%04x)\n",
2900 PRISM2_MAX_INTERRUPT_EVENTS,
2901 HFA384X_INW(HFA384X_EVSTAT_OFF));
2905 prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_INTERRUPT, 0, 1);
2906 return IRQ_RETVAL(events);
2910 static void prism2_check_sta_fw_version(local_info_t *local)
2912 struct hfa384x_comp_ident comp;
2913 int id, variant, major, minor;
2915 if (hfa384x_get_rid(local->dev, HFA384X_RID_STAID,
2916 &comp, sizeof(comp), 1) < 0)
2920 id = le16_to_cpu(comp.id);
2921 if (id != HFA384X_COMP_ID_STA) {
2922 if (id == HFA384X_COMP_ID_FW_AP)
2927 major = __le16_to_cpu(comp.major);
2928 minor = __le16_to_cpu(comp.minor);
2929 variant = __le16_to_cpu(comp.variant);
2930 local->sta_fw_ver = PRISM2_FW_VER(major, minor, variant);
2932 /* Station firmware versions before 1.4.x seem to have a bug in
2933 * firmware-based WEP encryption when using Host AP mode, so use
2934 * host_encrypt as a default for them. Firmware version 1.4.9 is the
2935 * first one that has been seen to produce correct encryption, but the
2936 * bug might be fixed before that (although, at least 1.4.2 is broken).
2938 local->fw_encrypt_ok = local->sta_fw_ver >= PRISM2_FW_VER(1,4,9);
2940 if (local->iw_mode == IW_MODE_MASTER && !local->host_encrypt &&
2941 !local->fw_encrypt_ok) {
2942 printk(KERN_DEBUG "%s: defaulting to host-based encryption as "
2943 "a workaround for firmware bug in Host AP mode WEP\n",
2945 local->host_encrypt = 1;
2948 /* IEEE 802.11 standard compliant WDS frames (4 addresses) were broken
2949 * in station firmware versions before 1.5.x. With these versions, the
2950 * driver uses a workaround with bogus frame format (4th address after
2951 * the payload). This is not compatible with other AP devices. Since
2952 * the firmware bug is fixed in the latest station firmware versions,
2953 * automatically enable standard compliant mode for cards using station
2954 * firmware version 1.5.0 or newer. */
2955 if (local->sta_fw_ver >= PRISM2_FW_VER(1,5,0))
2956 local->wds_type |= HOSTAP_WDS_STANDARD_FRAME;
2958 printk(KERN_DEBUG "%s: defaulting to bogus WDS frame as a "
2959 "workaround for firmware bug in Host AP mode WDS\n",
2963 hostap_check_sta_fw_version(local->ap, local->sta_fw_ver);
2967 static void prism2_crypt_deinit_entries(local_info_t *local, int force)
2969 struct list_head *ptr, *n;
2970 struct ieee80211_crypt_data *entry;
2972 for (ptr = local->crypt_deinit_list.next, n = ptr->next;
2973 ptr != &local->crypt_deinit_list; ptr = n, n = ptr->next) {
2974 entry = list_entry(ptr, struct ieee80211_crypt_data, list);
2976 if (atomic_read(&entry->refcnt) != 0 && !force)
2982 entry->ops->deinit(entry->priv);
2988 static void prism2_crypt_deinit_handler(unsigned long data)
2990 local_info_t *local = (local_info_t *) data;
2991 unsigned long flags;
2993 spin_lock_irqsave(&local->lock, flags);
2994 prism2_crypt_deinit_entries(local, 0);
2995 if (!list_empty(&local->crypt_deinit_list)) {
2996 printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
2997 "deletion list\n", local->dev->name);
2998 local->crypt_deinit_timer.expires = jiffies + HZ;
2999 add_timer(&local->crypt_deinit_timer);
3001 spin_unlock_irqrestore(&local->lock, flags);
3006 static void hostap_passive_scan(unsigned long data)
3008 local_info_t *local = (local_info_t *) data;
3009 struct net_device *dev = local->dev;
3012 if (local->passive_scan_interval <= 0)
3015 if (local->passive_scan_state == PASSIVE_SCAN_LISTEN) {
3018 /* Even though host system does not really know when the WLAN
3019 * MAC is sending frames, try to avoid changing channels for
3020 * passive scanning when a host-generated frame is being
3022 if (test_bit(HOSTAP_BITS_TRANSMIT, &local->bits)) {
3023 printk(KERN_DEBUG "%s: passive scan detected pending "
3024 "TX - delaying\n", dev->name);
3025 local->passive_scan_timer.expires = jiffies + HZ / 10;
3026 add_timer(&local->passive_scan_timer);
3031 local->passive_scan_channel++;
3032 if (local->passive_scan_channel > 14)
3033 local->passive_scan_channel = 1;
3035 } while (!(local->channel_mask &
3036 (1 << (local->passive_scan_channel - 1))) &&
3039 if (max_tries == 0) {
3040 printk(KERN_INFO "%s: no allowed passive scan channels"
3041 " found\n", dev->name);
3045 printk(KERN_DEBUG "%s: passive scan channel %d\n",
3046 dev->name, local->passive_scan_channel);
3047 channel = local->passive_scan_channel;
3048 local->passive_scan_state = PASSIVE_SCAN_WAIT;
3049 local->passive_scan_timer.expires = jiffies + HZ / 10;
3051 channel = local->channel;
3052 local->passive_scan_state = PASSIVE_SCAN_LISTEN;
3053 local->passive_scan_timer.expires = jiffies +
3054 local->passive_scan_interval * HZ;
3057 if (hfa384x_cmd_callback(dev, HFA384X_CMDCODE_TEST |
3058 (HFA384X_TEST_CHANGE_CHANNEL << 8),
3060 printk(KERN_ERR "%s: passive scan channel set %d "
3061 "failed\n", dev->name, channel);
3063 add_timer(&local->passive_scan_timer);
3067 /* Called only as a scheduled task when communications quality values should
3069 static void handle_comms_qual_update(void *data)
3071 local_info_t *local = data;
3072 prism2_update_comms_qual(local->dev);
3076 /* Software watchdog - called as a timer. Hardware interrupt (Tick event) is
3077 * used to monitor that local->last_tick_timer is being updated. If not,
3078 * interrupt busy-loop is assumed and driver tries to recover by masking out
3080 static void hostap_tick_timer(unsigned long data)
3082 static unsigned long last_inquire = 0;
3083 local_info_t *local = (local_info_t *) data;
3084 local->last_tick_timer = jiffies;
3086 /* Inquire CommTallies every 10 seconds to keep the statistics updated
3087 * more often during low load and when using 32-bit tallies. */
3088 if ((!last_inquire || time_after(jiffies, last_inquire + 10 * HZ)) &&
3089 !local->hw_downloading && local->hw_ready &&
3090 !local->hw_resetting && local->dev_enabled) {
3091 hfa384x_cmd_callback(local->dev, HFA384X_CMDCODE_INQUIRE,
3092 HFA384X_INFO_COMMTALLIES, NULL, 0);
3093 last_inquire = jiffies;
3096 if ((local->last_comms_qual_update == 0 ||
3097 time_after(jiffies, local->last_comms_qual_update + 10 * HZ)) &&
3098 (local->iw_mode == IW_MODE_INFRA ||
3099 local->iw_mode == IW_MODE_ADHOC)) {
3100 schedule_work(&local->comms_qual_update);
3103 local->tick_timer.expires = jiffies + 2 * HZ;
3104 add_timer(&local->tick_timer);
3108 #ifndef PRISM2_NO_PROCFS_DEBUG
3109 static int prism2_registers_proc_read(char *page, char **start, off_t off,
3110 int count, int *eof, void *data)
3113 local_info_t *local = (local_info_t *) data;
3120 #define SHOW_REG(n) \
3121 p += sprintf(p, #n "=%04x\n", hfa384x_read_reg(local->dev, HFA384X_##n##_OFF))
3139 SHOW_REG(TXCOMPLFID);
3140 SHOW_REG(SWSUPPORT0);
3141 SHOW_REG(SWSUPPORT1);
3142 SHOW_REG(SWSUPPORT2);
3146 /* Do not read data registers, because they change the state of the
3147 * MAC (offset += 2) */
3148 /* SHOW_REG(DATA0); */
3149 /* SHOW_REG(DATA1); */
3151 SHOW_REG(AUXOFFSET);
3152 /* SHOW_REG(AUXDATA); */
3156 SHOW_REG(PCI_M0_ADDRH);
3157 SHOW_REG(PCI_M0_ADDRL);
3158 SHOW_REG(PCI_M0_LEN);
3159 SHOW_REG(PCI_M0_CTL);
3160 SHOW_REG(PCI_STATUS);
3161 SHOW_REG(PCI_M1_ADDRH);
3162 SHOW_REG(PCI_M1_ADDRL);
3163 SHOW_REG(PCI_M1_LEN);
3164 SHOW_REG(PCI_M1_CTL);
3165 #endif /* PRISM2_PCI */
3169 #endif /* PRISM2_NO_PROCFS_DEBUG */
3172 struct set_tim_data {
3173 struct list_head list;
3178 static int prism2_set_tim(struct net_device *dev, int aid, int set)
3180 struct list_head *ptr;
3181 struct set_tim_data *new_entry;
3182 struct hostap_interface *iface;
3183 local_info_t *local;
3185 iface = netdev_priv(dev);
3186 local = iface->local;
3188 new_entry = (struct set_tim_data *)
3189 kmalloc(sizeof(*new_entry), GFP_ATOMIC);
3190 if (new_entry == NULL) {
3191 printk(KERN_DEBUG "%s: prism2_set_tim: kmalloc failed\n",
3195 memset(new_entry, 0, sizeof(*new_entry));
3196 new_entry->aid = aid;
3197 new_entry->set = set;
3199 spin_lock_bh(&local->set_tim_lock);
3200 list_for_each(ptr, &local->set_tim_list) {
3201 struct set_tim_data *entry =
3202 list_entry(ptr, struct set_tim_data, list);
3203 if (entry->aid == aid) {
3204 PDEBUG(DEBUG_PS2, "%s: prism2_set_tim: aid=%d "
3206 local->dev->name, aid, entry->set, set);
3214 list_add_tail(&new_entry->list, &local->set_tim_list);
3215 spin_unlock_bh(&local->set_tim_lock);
3217 schedule_work(&local->set_tim_queue);
3223 static void handle_set_tim_queue(void *data)
3225 local_info_t *local = (local_info_t *) data;
3226 struct set_tim_data *entry;
3231 spin_lock_bh(&local->set_tim_lock);
3232 if (!list_empty(&local->set_tim_list)) {
3233 entry = list_entry(local->set_tim_list.next,
3234 struct set_tim_data, list);
3235 list_del(&entry->list);
3237 spin_unlock_bh(&local->set_tim_lock);
3241 PDEBUG(DEBUG_PS2, "%s: handle_set_tim_queue: aid=%d set=%d\n",
3242 local->dev->name, entry->aid, entry->set);
3247 if (hostap_set_word(local->dev, HFA384X_RID_CNFTIMCTRL, val)) {
3248 printk(KERN_DEBUG "%s: set_tim failed (aid=%d "
3250 local->dev->name, entry->aid, entry->set);
3258 static void prism2_clear_set_tim_queue(local_info_t *local)
3260 struct list_head *ptr, *n;
3262 list_for_each_safe(ptr, n, &local->set_tim_list) {
3263 struct set_tim_data *entry;
3264 entry = list_entry(ptr, struct set_tim_data, list);
3265 list_del(&entry->list);
3271 static struct net_device *
3272 prism2_init_local_data(struct prism2_helper_functions *funcs, int card_idx,
3273 struct device *sdev)
3275 struct net_device *dev;
3276 struct hostap_interface *iface;
3277 struct local_info *local;
3283 len = strlen(dev_template);
3284 if (len >= IFNAMSIZ || strstr(dev_template, "%d") == NULL) {
3285 printk(KERN_WARNING "hostap: Invalid dev_template='%s'\n",
3290 len = sizeof(struct hostap_interface) +
3291 3 + sizeof(struct local_info) +
3292 3 + sizeof(struct ap_data);
3294 dev = alloc_etherdev(len);
3298 iface = netdev_priv(dev);
3299 local = (struct local_info *) ((((long) (iface + 1)) + 3) & ~3);
3300 local->ap = (struct ap_data *) ((((long) (local + 1)) + 3) & ~3);
3301 local->dev = iface->dev = dev;
3302 iface->local = local;
3303 iface->type = HOSTAP_INTERFACE_MASTER;
3304 INIT_LIST_HEAD(&local->hostap_interfaces);
3306 local->hw_module = THIS_MODULE;
3308 #ifdef PRISM2_IO_DEBUG
3309 local->io_debug_enabled = 1;
3310 #endif /* PRISM2_IO_DEBUG */
3312 #if defined(PRISM2_PCI) && defined(PRISM2_BUS_MASTER)
3313 local->bus_m0_buf = (u8 *) kmalloc(sizeof(struct hfa384x_tx_frame) +
3314 PRISM2_DATA_MAXLEN, GFP_DMA);
3315 if (local->bus_m0_buf == NULL)
3317 #endif /* PRISM2_PCI and PRISM2_BUS_MASTER */
3319 local->func = funcs;
3320 local->func->cmd = hfa384x_cmd;
3321 local->func->read_regs = hfa384x_read_regs;
3322 local->func->get_rid = hfa384x_get_rid;
3323 local->func->set_rid = hfa384x_set_rid;
3324 local->func->hw_enable = prism2_hw_enable;
3325 local->func->hw_config = prism2_hw_config;
3326 local->func->hw_reset = prism2_hw_reset;
3327 local->func->hw_shutdown = prism2_hw_shutdown;
3328 local->func->reset_port = prism2_reset_port;
3329 local->func->schedule_reset = prism2_schedule_reset;
3330 #ifdef PRISM2_DOWNLOAD_SUPPORT
3331 local->func->read_aux = prism2_download_aux_dump;
3332 local->func->download = prism2_download;
3333 #endif /* PRISM2_DOWNLOAD_SUPPORT */
3334 local->func->tx = prism2_tx_80211;
3335 local->func->set_tim = prism2_set_tim;
3336 local->func->need_tx_headroom = 0; /* no need to add txdesc in
3337 * skb->data (FIX: maybe for DMA bus
3342 rwlock_init(&local->iface_lock);
3343 spin_lock_init(&local->txfidlock);
3344 spin_lock_init(&local->cmdlock);
3345 spin_lock_init(&local->baplock);
3346 spin_lock_init(&local->lock);
3347 init_MUTEX(&local->rid_bap_sem);
3349 if (card_idx < 0 || card_idx >= MAX_PARM_DEVICES)
3351 local->card_idx = card_idx;
3353 len = strlen(essid);
3354 memcpy(local->essid, essid,
3355 len > MAX_SSID_LEN ? MAX_SSID_LEN : len);
3356 local->essid[MAX_SSID_LEN] = '\0';
3357 i = GET_INT_PARM(iw_mode, card_idx);
3358 if ((i >= IW_MODE_ADHOC && i <= IW_MODE_REPEAT) ||
3359 i == IW_MODE_MONITOR) {
3362 printk(KERN_WARNING "prism2: Unknown iw_mode %d; using "
3363 "IW_MODE_MASTER\n", i);
3364 local->iw_mode = IW_MODE_MASTER;
3366 local->channel = GET_INT_PARM(channel, card_idx);
3367 local->beacon_int = GET_INT_PARM(beacon_int, card_idx);
3368 local->dtim_period = GET_INT_PARM(dtim_period, card_idx);
3369 local->wds_max_connections = 16;
3370 local->tx_control = HFA384X_TX_CTRL_FLAGS;
3371 local->manual_retry_count = -1;
3372 local->rts_threshold = 2347;
3373 local->fragm_threshold = 2346;
3374 local->rssi_to_dBm = 100; /* default; to be overriden by
3375 * cnfDbmAdjust, if available */
3376 local->auth_algs = PRISM2_AUTH_OPEN | PRISM2_AUTH_SHARED_KEY;
3377 local->sram_type = -1;
3378 local->scan_channel_mask = 0xffff;
3379 #if defined(PRISM2_PCI) && defined(PRISM2_BUS_MASTER)
3380 local->bus_master_threshold_rx = GET_INT_PARM(bus_master_threshold_rx,
3382 local->bus_master_threshold_tx = GET_INT_PARM(bus_master_threshold_tx,
3384 #endif /* PRISM2_PCI and PRISM2_BUS_MASTER */
3386 /* Initialize task queue structures */
3387 INIT_WORK(&local->reset_queue, handle_reset_queue, local);
3388 INIT_WORK(&local->set_multicast_list_queue,
3389 hostap_set_multicast_list_queue, local->dev);
3391 INIT_WORK(&local->set_tim_queue, handle_set_tim_queue, local);
3392 INIT_LIST_HEAD(&local->set_tim_list);
3393 spin_lock_init(&local->set_tim_lock);
3395 INIT_WORK(&local->comms_qual_update, handle_comms_qual_update, local);
3397 /* Initialize tasklets for handling hardware IRQ related operations
3398 * outside hw IRQ handler */
3399 #define HOSTAP_TASKLET_INIT(q, f, d) \
3400 do { memset((q), 0, sizeof(*(q))); (q)->func = (f); (q)->data = (d); } \
3402 HOSTAP_TASKLET_INIT(&local->bap_tasklet, hostap_bap_tasklet,
3403 (unsigned long) local);
3405 HOSTAP_TASKLET_INIT(&local->info_tasklet, hostap_info_tasklet,
3406 (unsigned long) local);
3407 hostap_info_init(local);
3409 HOSTAP_TASKLET_INIT(&local->rx_tasklet,
3410 hostap_rx_tasklet, (unsigned long) local);
3411 skb_queue_head_init(&local->rx_list);
3413 HOSTAP_TASKLET_INIT(&local->sta_tx_exc_tasklet,
3414 hostap_sta_tx_exc_tasklet, (unsigned long) local);
3415 skb_queue_head_init(&local->sta_tx_exc_list);
3417 INIT_LIST_HEAD(&local->cmd_queue);
3418 init_waitqueue_head(&local->hostscan_wq);
3419 INIT_LIST_HEAD(&local->crypt_deinit_list);
3420 init_timer(&local->crypt_deinit_timer);
3421 local->crypt_deinit_timer.data = (unsigned long) local;
3422 local->crypt_deinit_timer.function = prism2_crypt_deinit_handler;
3424 init_timer(&local->passive_scan_timer);
3425 local->passive_scan_timer.data = (unsigned long) local;
3426 local->passive_scan_timer.function = hostap_passive_scan;
3428 init_timer(&local->tick_timer);
3429 local->tick_timer.data = (unsigned long) local;
3430 local->tick_timer.function = hostap_tick_timer;
3431 local->tick_timer.expires = jiffies + 2 * HZ;
3432 add_timer(&local->tick_timer);
3434 INIT_LIST_HEAD(&local->bss_list);
3436 hostap_setup_dev(dev, local, 1);
3437 local->saved_eth_header_parse = dev->hard_header_parse;
3439 dev->hard_start_xmit = hostap_master_start_xmit;
3440 dev->type = ARPHRD_IEEE80211;
3441 dev->hard_header_parse = hostap_80211_header_parse;
3444 ret = dev_alloc_name(dev, "wifi%d");
3445 SET_NETDEV_DEV(dev, sdev);
3447 ret = register_netdevice(dev);
3450 printk(KERN_WARNING "%s: register netdevice failed!\n",
3454 printk(KERN_INFO "%s: Registered netdevice %s\n", dev_info, dev->name);
3456 #ifndef PRISM2_NO_PROCFS_DEBUG
3457 create_proc_read_entry("registers", 0, local->proc,
3458 prism2_registers_proc_read, local);
3459 #endif /* PRISM2_NO_PROCFS_DEBUG */
3461 hostap_init_data(local);
3465 #if defined(PRISM2_PCI) && defined(PRISM2_BUS_MASTER)
3466 kfree(local->bus_m0_buf);
3467 #endif /* PRISM2_PCI and PRISM2_BUS_MASTER */
3473 static int hostap_hw_ready(struct net_device *dev)
3475 struct hostap_interface *iface;
3476 struct local_info *local;
3478 iface = netdev_priv(dev);
3479 local = iface->local;
3480 local->ddev = hostap_add_interface(local, HOSTAP_INTERFACE_MAIN, 0,
3484 if (local->iw_mode == IW_MODE_INFRA ||
3485 local->iw_mode == IW_MODE_ADHOC) {
3486 netif_carrier_off(local->dev);
3487 netif_carrier_off(local->ddev);
3489 hostap_init_proc(local);
3490 hostap_init_ap_proc(local);
3498 static void prism2_free_local_data(struct net_device *dev)
3500 struct hostap_tx_callback_info *tx_cb, *tx_cb_prev;
3502 struct hostap_interface *iface;
3503 struct local_info *local;
3504 struct list_head *ptr, *n;
3509 iface = netdev_priv(dev);
3510 local = iface->local;
3512 flush_scheduled_work();
3514 if (timer_pending(&local->crypt_deinit_timer))
3515 del_timer(&local->crypt_deinit_timer);
3516 prism2_crypt_deinit_entries(local, 1);
3518 if (timer_pending(&local->passive_scan_timer))
3519 del_timer(&local->passive_scan_timer);
3521 if (timer_pending(&local->tick_timer))
3522 del_timer(&local->tick_timer);
3524 prism2_clear_cmd_queue(local);
3526 skb_queue_purge(&local->info_list);
3527 skb_queue_purge(&local->rx_list);
3528 skb_queue_purge(&local->sta_tx_exc_list);
3530 if (local->dev_enabled)
3531 prism2_callback(local, PRISM2_CALLBACK_DISABLE);
3533 for (i = 0; i < WEP_KEYS; i++) {
3534 struct ieee80211_crypt_data *crypt = local->crypt[i];
3537 crypt->ops->deinit(crypt->priv);
3539 local->crypt[i] = NULL;
3543 if (local->ap != NULL)
3544 hostap_free_data(local->ap);
3546 #ifndef PRISM2_NO_PROCFS_DEBUG
3547 if (local->proc != NULL)
3548 remove_proc_entry("registers", local->proc);
3549 #endif /* PRISM2_NO_PROCFS_DEBUG */
3550 hostap_remove_proc(local);
3552 tx_cb = local->tx_callback;
3553 while (tx_cb != NULL) {
3555 tx_cb = tx_cb->next;
3559 hostap_set_hostapd(local, 0, 0);
3560 hostap_set_hostapd_sta(local, 0, 0);
3562 for (i = 0; i < PRISM2_FRAG_CACHE_LEN; i++) {
3563 if (local->frag_cache[i].skb != NULL)
3564 dev_kfree_skb(local->frag_cache[i].skb);
3567 #ifdef PRISM2_DOWNLOAD_SUPPORT
3568 prism2_download_free_data(local->dl_pri);
3569 prism2_download_free_data(local->dl_sec);
3570 #endif /* PRISM2_DOWNLOAD_SUPPORT */
3572 list_for_each_safe(ptr, n, &local->hostap_interfaces) {
3573 iface = list_entry(ptr, struct hostap_interface, list);
3574 if (iface->type == HOSTAP_INTERFACE_MASTER) {
3575 /* special handling for this interface below */
3578 hostap_remove_interface(iface->dev, 0, 1);
3581 prism2_clear_set_tim_queue(local);
3583 list_for_each_safe(ptr, n, &local->bss_list) {
3584 struct hostap_bss_info *bss =
3585 list_entry(ptr, struct hostap_bss_info, list);
3589 #if defined(PRISM2_PCI) && defined(PRISM2_BUS_MASTER)
3590 kfree(local->bus_m0_buf);
3591 #endif /* PRISM2_PCI and PRISM2_BUS_MASTER */
3593 kfree(local->last_scan_results);
3594 kfree(local->generic_elem);
3596 unregister_netdev(local->dev);
3597 free_netdev(local->dev);
3602 static void prism2_suspend(struct net_device *dev)
3604 struct hostap_interface *iface;
3605 struct local_info *local;
3606 union iwreq_data wrqu;
3609 local = iface->local;
3611 /* Send disconnect event, e.g., to trigger reassociation after resume
3612 * if wpa_supplicant is used. */
3613 memset(&wrqu, 0, sizeof(wrqu));
3614 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
3615 wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
3617 /* Disable hardware and firmware */
3618 prism2_hw_shutdown(dev, 0);
3620 #endif /* PRISM2_PLX */
3623 /* These might at some point be compiled separately and used as separate
3624 * kernel modules or linked into one */
3625 #ifdef PRISM2_DOWNLOAD_SUPPORT
3626 #include "hostap_download.c"
3627 #endif /* PRISM2_DOWNLOAD_SUPPORT */
3629 #ifdef PRISM2_CALLBACK
3630 /* External hostap_callback.c file can be used to, e.g., blink activity led.
3631 * This can use platform specific code and must define prism2_callback()
3632 * function (if PRISM2_CALLBACK is not defined, these function calls are not
3634 #include "hostap_callback.c"
3635 #endif /* PRISM2_CALLBACK */