Merge master.kernel.org:/pub/scm/linux/kernel/git/jejb/scsi-rc-fixes-2.6
[linux-2.6] / drivers / net / wireless / hostap / hostap_hw.c
1 /*
2  * Host AP (software wireless LAN access point) driver for
3  * Intersil Prism2/2.5/3.
4  *
5  * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
6  * <jkmaline@cc.hut.fi>
7  * Copyright (c) 2002-2005, Jouni Malinen <jkmaline@cc.hut.fi>
8  *
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
12  * more details.
13  *
14  * FIX:
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)
20  *
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.
30  */
31
32
33
34 #include <asm/delay.h>
35 #include <asm/uaccess.h>
36
37 #include <linux/slab.h>
38 #include <linux/netdevice.h>
39 #include <linux/etherdevice.h>
40 #include <linux/proc_fs.h>
41 #include <linux/if_arp.h>
42 #include <linux/delay.h>
43 #include <linux/random.h>
44 #include <linux/wait.h>
45 #include <linux/sched.h>
46 #include <linux/rtnetlink.h>
47 #include <linux/wireless.h>
48 #include <net/iw_handler.h>
49 #include <net/ieee80211.h>
50 #include <net/ieee80211_crypt.h>
51 #include <asm/irq.h>
52
53 #include "hostap_80211.h"
54 #include "hostap.h"
55 #include "hostap_ap.h"
56
57
58 /* #define final_version */
59
60 static int mtu = 1500;
61 module_param(mtu, int, 0444);
62 MODULE_PARM_DESC(mtu, "Maximum transfer unit");
63
64 static int channel[MAX_PARM_DEVICES] = { 3, DEF_INTS };
65 module_param_array(channel, int, NULL, 0444);
66 MODULE_PARM_DESC(channel, "Initial channel");
67
68 static char essid[33] = "test";
69 module_param_string(essid, essid, sizeof(essid), 0444);
70 MODULE_PARM_DESC(essid, "Host AP's ESSID");
71
72 static int iw_mode[MAX_PARM_DEVICES] = { IW_MODE_MASTER, DEF_INTS };
73 module_param_array(iw_mode, int, NULL, 0444);
74 MODULE_PARM_DESC(iw_mode, "Initial operation mode");
75
76 static int beacon_int[MAX_PARM_DEVICES] = { 100, DEF_INTS };
77 module_param_array(beacon_int, int, NULL, 0444);
78 MODULE_PARM_DESC(beacon_int, "Beacon interval (1 = 1024 usec)");
79
80 static int dtim_period[MAX_PARM_DEVICES] = { 1, DEF_INTS };
81 module_param_array(dtim_period, int, NULL, 0444);
82 MODULE_PARM_DESC(dtim_period, "DTIM period");
83
84 static char dev_template[16] = "wlan%d";
85 module_param_string(dev_template, dev_template, sizeof(dev_template), 0444);
86 MODULE_PARM_DESC(dev_template, "Prefix for network device name (default: "
87                  "wlan%d)");
88
89 #ifdef final_version
90 #define EXTRA_EVENTS_WTERR 0
91 #else
92 /* check WTERR events (Wait Time-out) in development versions */
93 #define EXTRA_EVENTS_WTERR HFA384X_EV_WTERR
94 #endif
95
96 /* Events that will be using BAP0 */
97 #define HFA384X_BAP0_EVENTS \
98         (HFA384X_EV_TXEXC | HFA384X_EV_RX | HFA384X_EV_INFO | HFA384X_EV_TX)
99
100 /* event mask, i.e., events that will result in an interrupt */
101 #define HFA384X_EVENT_MASK \
102         (HFA384X_BAP0_EVENTS | HFA384X_EV_ALLOC | HFA384X_EV_INFDROP | \
103         HFA384X_EV_CMD | HFA384X_EV_TICK | \
104         EXTRA_EVENTS_WTERR)
105
106 /* Default TX control flags: use 802.11 headers and request interrupt for
107  * failed transmits. Frames that request ACK callback, will add
108  * _TX_OK flag and _ALT_RTRY flag may be used to select different retry policy.
109  */
110 #define HFA384X_TX_CTRL_FLAGS \
111         (HFA384X_TX_CTRL_802_11 | HFA384X_TX_CTRL_TX_EX)
112
113
114 /* ca. 1 usec */
115 #define HFA384X_CMD_BUSY_TIMEOUT 5000
116 #define HFA384X_BAP_BUSY_TIMEOUT 50000
117
118 /* ca. 10 usec */
119 #define HFA384X_CMD_COMPL_TIMEOUT 20000
120 #define HFA384X_DL_COMPL_TIMEOUT 1000000
121
122 /* Wait times for initialization; yield to other processes to avoid busy
123  * waiting for long time. */
124 #define HFA384X_INIT_TIMEOUT (HZ / 2) /* 500 ms */
125 #define HFA384X_ALLOC_COMPL_TIMEOUT (HZ / 20) /* 50 ms */
126
127
128 static void prism2_hw_reset(struct net_device *dev);
129 static void prism2_check_sta_fw_version(local_info_t *local);
130
131 #ifdef PRISM2_DOWNLOAD_SUPPORT
132 /* hostap_download.c */
133 static int prism2_download_aux_dump(struct net_device *dev,
134                                     unsigned int addr, int len, u8 *buf);
135 static u8 * prism2_read_pda(struct net_device *dev);
136 static int prism2_download(local_info_t *local,
137                            struct prism2_download_param *param);
138 static void prism2_download_free_data(struct prism2_download_data *dl);
139 static int prism2_download_volatile(local_info_t *local,
140                                     struct prism2_download_data *param);
141 static int prism2_download_genesis(local_info_t *local,
142                                    struct prism2_download_data *param);
143 static int prism2_get_ram_size(local_info_t *local);
144 #endif /* PRISM2_DOWNLOAD_SUPPORT */
145
146
147
148
149 #ifndef final_version
150 /* magic value written to SWSUPPORT0 reg. for detecting whether card is still
151  * present */
152 #define HFA384X_MAGIC 0x8A32
153 #endif
154
155
156 static u16 hfa384x_read_reg(struct net_device *dev, u16 reg)
157 {
158         return HFA384X_INW(reg);
159 }
160
161
162 static void hfa384x_read_regs(struct net_device *dev,
163                               struct hfa384x_regs *regs)
164 {
165         regs->cmd = HFA384X_INW(HFA384X_CMD_OFF);
166         regs->evstat = HFA384X_INW(HFA384X_EVSTAT_OFF);
167         regs->offset0 = HFA384X_INW(HFA384X_OFFSET0_OFF);
168         regs->offset1 = HFA384X_INW(HFA384X_OFFSET1_OFF);
169         regs->swsupport0 = HFA384X_INW(HFA384X_SWSUPPORT0_OFF);
170 }
171
172
173 /**
174  * __hostap_cmd_queue_free - Free Prism2 command queue entry (private)
175  * @local: pointer to private Host AP driver data
176  * @entry: Prism2 command queue entry to be freed
177  * @del_req: request the entry to be removed
178  *
179  * Internal helper function for freeing Prism2 command queue entries.
180  * Caller must have acquired local->cmdlock before calling this function.
181  */
182 static inline void __hostap_cmd_queue_free(local_info_t *local,
183                                            struct hostap_cmd_queue *entry,
184                                            int del_req)
185 {
186         if (del_req) {
187                 entry->del_req = 1;
188                 if (!list_empty(&entry->list)) {
189                         list_del_init(&entry->list);
190                         local->cmd_queue_len--;
191                 }
192         }
193
194         if (atomic_dec_and_test(&entry->usecnt) && entry->del_req)
195                 kfree(entry);
196 }
197
198
199 /**
200  * hostap_cmd_queue_free - Free Prism2 command queue entry
201  * @local: pointer to private Host AP driver data
202  * @entry: Prism2 command queue entry to be freed
203  * @del_req: request the entry to be removed
204  *
205  * Free a Prism2 command queue entry.
206  */
207 static inline void hostap_cmd_queue_free(local_info_t *local,
208                                          struct hostap_cmd_queue *entry,
209                                          int del_req)
210 {
211         unsigned long flags;
212
213         spin_lock_irqsave(&local->cmdlock, flags);
214         __hostap_cmd_queue_free(local, entry, del_req);
215         spin_unlock_irqrestore(&local->cmdlock, flags);
216 }
217
218
219 /**
220  * prism2_clear_cmd_queue - Free all pending Prism2 command queue entries
221  * @local: pointer to private Host AP driver data
222  */
223 static void prism2_clear_cmd_queue(local_info_t *local)
224 {
225         struct list_head *ptr, *n;
226         unsigned long flags;
227         struct hostap_cmd_queue *entry;
228
229         spin_lock_irqsave(&local->cmdlock, flags);
230         list_for_each_safe(ptr, n, &local->cmd_queue) {
231                 entry = list_entry(ptr, struct hostap_cmd_queue, list);
232                 atomic_inc(&entry->usecnt);
233                 printk(KERN_DEBUG "%s: removed pending cmd_queue entry "
234                        "(type=%d, cmd=0x%04x, param0=0x%04x)\n",
235                        local->dev->name, entry->type, entry->cmd,
236                        entry->param0);
237                 __hostap_cmd_queue_free(local, entry, 1);
238         }
239         if (local->cmd_queue_len) {
240                 /* This should not happen; print debug message and clear
241                  * queue length. */
242                 printk(KERN_DEBUG "%s: cmd_queue_len (%d) not zero after "
243                        "flush\n", local->dev->name, local->cmd_queue_len);
244                 local->cmd_queue_len = 0;
245         }
246         spin_unlock_irqrestore(&local->cmdlock, flags);
247 }
248
249
250 /**
251  * hfa384x_cmd_issue - Issue a Prism2 command to the hardware
252  * @dev: pointer to net_device
253  * @entry: Prism2 command queue entry to be issued
254  */
255 static int hfa384x_cmd_issue(struct net_device *dev,
256                                     struct hostap_cmd_queue *entry)
257 {
258         struct hostap_interface *iface;
259         local_info_t *local;
260         int tries;
261         u16 reg;
262         unsigned long flags;
263
264         iface = netdev_priv(dev);
265         local = iface->local;
266
267         if (local->func->card_present && !local->func->card_present(local))
268                 return -ENODEV;
269
270         if (entry->issued) {
271                 printk(KERN_DEBUG "%s: driver bug - re-issuing command @%p\n",
272                        dev->name, entry);
273         }
274
275         /* wait until busy bit is clear; this should always be clear since the
276          * commands are serialized */
277         tries = HFA384X_CMD_BUSY_TIMEOUT;
278         while (HFA384X_INW(HFA384X_CMD_OFF) & HFA384X_CMD_BUSY && tries > 0) {
279                 tries--;
280                 udelay(1);
281         }
282 #ifndef final_version
283         if (tries != HFA384X_CMD_BUSY_TIMEOUT) {
284                 prism2_io_debug_error(dev, 1);
285                 printk(KERN_DEBUG "%s: hfa384x_cmd_issue: cmd reg was busy "
286                        "for %d usec\n", dev->name,
287                        HFA384X_CMD_BUSY_TIMEOUT - tries);
288         }
289 #endif
290         if (tries == 0) {
291                 reg = HFA384X_INW(HFA384X_CMD_OFF);
292                 prism2_io_debug_error(dev, 2);
293                 printk(KERN_DEBUG "%s: hfa384x_cmd_issue - timeout - "
294                        "reg=0x%04x\n", dev->name, reg);
295                 return -ETIMEDOUT;
296         }
297
298         /* write command */
299         spin_lock_irqsave(&local->cmdlock, flags);
300         HFA384X_OUTW(entry->param0, HFA384X_PARAM0_OFF);
301         HFA384X_OUTW(entry->param1, HFA384X_PARAM1_OFF);
302         HFA384X_OUTW(entry->cmd, HFA384X_CMD_OFF);
303         entry->issued = 1;
304         spin_unlock_irqrestore(&local->cmdlock, flags);
305
306         return 0;
307 }
308
309
310 /**
311  * hfa384x_cmd - Issue a Prism2 command and wait (sleep) for completion
312  * @dev: pointer to net_device
313  * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
314  * @param0: value for Param0 register
315  * @param1: value for Param1 register (pointer; %NULL if not used)
316  * @resp0: pointer for Resp0 data or %NULL if Resp0 is not needed
317  *
318  * Issue given command (possibly after waiting in command queue) and sleep
319  * until the command is completed (or timed out or interrupted). This can be
320  * called only from user process context.
321  */
322 static int hfa384x_cmd(struct net_device *dev, u16 cmd, u16 param0,
323                        u16 *param1, u16 *resp0)
324 {
325         struct hostap_interface *iface;
326         local_info_t *local;
327         int err, res, issue, issued = 0;
328         unsigned long flags;
329         struct hostap_cmd_queue *entry;
330         DECLARE_WAITQUEUE(wait, current);
331
332         iface = netdev_priv(dev);
333         local = iface->local;
334
335         if (in_interrupt()) {
336                 printk(KERN_DEBUG "%s: hfa384x_cmd called from interrupt "
337                        "context\n", dev->name);
338                 return -1;
339         }
340
341         if (local->cmd_queue_len >= HOSTAP_CMD_QUEUE_MAX_LEN) {
342                 printk(KERN_DEBUG "%s: hfa384x_cmd: cmd_queue full\n",
343                        dev->name);
344                 return -1;
345         }
346
347         if (signal_pending(current))
348                 return -EINTR;
349
350         entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
351         if (entry == NULL) {
352                 printk(KERN_DEBUG "%s: hfa384x_cmd - kmalloc failed\n",
353                        dev->name);
354                 return -ENOMEM;
355         }
356         atomic_set(&entry->usecnt, 1);
357         entry->type = CMD_SLEEP;
358         entry->cmd = cmd;
359         entry->param0 = param0;
360         if (param1)
361                 entry->param1 = *param1;
362         init_waitqueue_head(&entry->compl);
363
364         /* prepare to wait for command completion event, but do not sleep yet
365          */
366         add_wait_queue(&entry->compl, &wait);
367         set_current_state(TASK_INTERRUPTIBLE);
368
369         spin_lock_irqsave(&local->cmdlock, flags);
370         issue = list_empty(&local->cmd_queue);
371         if (issue)
372                 entry->issuing = 1;
373         list_add_tail(&entry->list, &local->cmd_queue);
374         local->cmd_queue_len++;
375         spin_unlock_irqrestore(&local->cmdlock, flags);
376
377         err = 0;
378         if (!issue)
379                 goto wait_completion;
380
381         if (signal_pending(current))
382                 err = -EINTR;
383
384         if (!err) {
385                 if (hfa384x_cmd_issue(dev, entry))
386                         err = -ETIMEDOUT;
387                 else
388                         issued = 1;
389         }
390
391  wait_completion:
392         if (!err && entry->type != CMD_COMPLETED) {
393                 /* sleep until command is completed or timed out */
394                 res = schedule_timeout(2 * HZ);
395         } else
396                 res = -1;
397
398         if (!err && signal_pending(current))
399                 err = -EINTR;
400
401         if (err && issued) {
402                 /* the command was issued, so a CmdCompl event should occur
403                  * soon; however, there's a pending signal and
404                  * schedule_timeout() would be interrupted; wait a short period
405                  * of time to avoid removing entry from the list before
406                  * CmdCompl event */
407                 udelay(300);
408         }
409
410         set_current_state(TASK_RUNNING);
411         remove_wait_queue(&entry->compl, &wait);
412
413         /* If entry->list is still in the list, it must be removed
414          * first and in this case prism2_cmd_ev() does not yet have
415          * local reference to it, and the data can be kfree()'d
416          * here. If the command completion event is still generated,
417          * it will be assigned to next (possibly) pending command, but
418          * the driver will reset the card anyway due to timeout
419          *
420          * If the entry is not in the list prism2_cmd_ev() has a local
421          * reference to it, but keeps cmdlock as long as the data is
422          * needed, so the data can be kfree()'d here. */
423
424         /* FIX: if the entry->list is in the list, it has not been completed
425          * yet, so removing it here is somewhat wrong.. this could cause
426          * references to freed memory and next list_del() causing NULL pointer
427          * dereference.. it would probably be better to leave the entry in the
428          * list and the list should be emptied during hw reset */
429
430         spin_lock_irqsave(&local->cmdlock, flags);
431         if (!list_empty(&entry->list)) {
432                 printk(KERN_DEBUG "%s: hfa384x_cmd: entry still in list? "
433                        "(entry=%p, type=%d, res=%d)\n", dev->name, entry,
434                        entry->type, res);
435                 list_del_init(&entry->list);
436                 local->cmd_queue_len--;
437         }
438         spin_unlock_irqrestore(&local->cmdlock, flags);
439
440         if (err) {
441                 printk(KERN_DEBUG "%s: hfa384x_cmd: interrupted; err=%d\n",
442                        dev->name, err);
443                 res = err;
444                 goto done;
445         }
446
447         if (entry->type != CMD_COMPLETED) {
448                 u16 reg = HFA384X_INW(HFA384X_EVSTAT_OFF);
449                 printk(KERN_DEBUG "%s: hfa384x_cmd: command was not "
450                        "completed (res=%d, entry=%p, type=%d, cmd=0x%04x, "
451                        "param0=0x%04x, EVSTAT=%04x INTEN=%04x)\n", dev->name,
452                        res, entry, entry->type, entry->cmd, entry->param0, reg,
453                        HFA384X_INW(HFA384X_INTEN_OFF));
454                 if (reg & HFA384X_EV_CMD) {
455                         /* Command completion event is pending, but the
456                          * interrupt was not delivered - probably an issue
457                          * with pcmcia-cs configuration. */
458                         printk(KERN_WARNING "%s: interrupt delivery does not "
459                                "seem to work\n", dev->name);
460                 }
461                 prism2_io_debug_error(dev, 3);
462                 res = -ETIMEDOUT;
463                 goto done;
464         }
465
466         if (resp0 != NULL)
467                 *resp0 = entry->resp0;
468 #ifndef final_version
469         if (entry->res) {
470                 printk(KERN_DEBUG "%s: CMD=0x%04x => res=0x%02x, "
471                        "resp0=0x%04x\n",
472                        dev->name, cmd, entry->res, entry->resp0);
473         }
474 #endif /* final_version */
475
476         res = entry->res;
477  done:
478         hostap_cmd_queue_free(local, entry, 1);
479         return res;
480 }
481
482
483 /**
484  * hfa384x_cmd_callback - Issue a Prism2 command; callback when completed
485  * @dev: pointer to net_device
486  * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
487  * @param0: value for Param0 register
488  * @callback: command completion callback function (%NULL = no callback)
489  * @context: context data to be given to the callback function
490  *
491  * Issue given command (possibly after waiting in command queue) and use
492  * callback function to indicate command completion. This can be called both
493  * from user and interrupt context. The callback function will be called in
494  * hardware IRQ context. It can be %NULL, when no function is called when
495  * command is completed.
496  */
497 static int hfa384x_cmd_callback(struct net_device *dev, u16 cmd, u16 param0,
498                                 void (*callback)(struct net_device *dev,
499                                                  long context, u16 resp0,
500                                                  u16 status),
501                                 long context)
502 {
503         struct hostap_interface *iface;
504         local_info_t *local;
505         int issue, ret;
506         unsigned long flags;
507         struct hostap_cmd_queue *entry;
508
509         iface = netdev_priv(dev);
510         local = iface->local;
511
512         if (local->cmd_queue_len >= HOSTAP_CMD_QUEUE_MAX_LEN + 2) {
513                 printk(KERN_DEBUG "%s: hfa384x_cmd: cmd_queue full\n",
514                        dev->name);
515                 return -1;
516         }
517
518         entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
519         if (entry == NULL) {
520                 printk(KERN_DEBUG "%s: hfa384x_cmd_callback - kmalloc "
521                        "failed\n", dev->name);
522                 return -ENOMEM;
523         }
524         atomic_set(&entry->usecnt, 1);
525         entry->type = CMD_CALLBACK;
526         entry->cmd = cmd;
527         entry->param0 = param0;
528         entry->callback = callback;
529         entry->context = context;
530
531         spin_lock_irqsave(&local->cmdlock, flags);
532         issue = list_empty(&local->cmd_queue);
533         if (issue)
534                 entry->issuing = 1;
535         list_add_tail(&entry->list, &local->cmd_queue);
536         local->cmd_queue_len++;
537         spin_unlock_irqrestore(&local->cmdlock, flags);
538
539         if (issue && hfa384x_cmd_issue(dev, entry))
540                 ret = -ETIMEDOUT;
541         else
542                 ret = 0;
543
544         hostap_cmd_queue_free(local, entry, ret);
545
546         return ret;
547 }
548
549
550 /**
551  * __hfa384x_cmd_no_wait - Issue a Prism2 command (private)
552  * @dev: pointer to net_device
553  * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
554  * @param0: value for Param0 register
555  * @io_debug_num: I/O debug error number
556  *
557  * Shared helper function for hfa384x_cmd_wait() and hfa384x_cmd_no_wait().
558  */
559 static int __hfa384x_cmd_no_wait(struct net_device *dev, u16 cmd, u16 param0,
560                                  int io_debug_num)
561 {
562         int tries;
563         u16 reg;
564
565         /* wait until busy bit is clear; this should always be clear since the
566          * commands are serialized */
567         tries = HFA384X_CMD_BUSY_TIMEOUT;
568         while (HFA384X_INW(HFA384X_CMD_OFF) & HFA384X_CMD_BUSY && tries > 0) {
569                 tries--;
570                 udelay(1);
571         }
572         if (tries == 0) {
573                 reg = HFA384X_INW(HFA384X_CMD_OFF);
574                 prism2_io_debug_error(dev, io_debug_num);
575                 printk(KERN_DEBUG "%s: __hfa384x_cmd_no_wait(%d) - timeout - "
576                        "reg=0x%04x\n", dev->name, io_debug_num, reg);
577                 return -ETIMEDOUT;
578         }
579
580         /* write command */
581         HFA384X_OUTW(param0, HFA384X_PARAM0_OFF);
582         HFA384X_OUTW(cmd, HFA384X_CMD_OFF);
583
584         return 0;
585 }
586
587
588 /**
589  * hfa384x_cmd_wait - Issue a Prism2 command and busy wait for completion
590  * @dev: pointer to net_device
591  * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
592  * @param0: value for Param0 register
593  */
594 static int hfa384x_cmd_wait(struct net_device *dev, u16 cmd, u16 param0)
595 {
596         int res, tries;
597         u16 reg;
598
599         res = __hfa384x_cmd_no_wait(dev, cmd, param0, 4);
600         if (res)
601                 return res;
602
603         /* wait for command completion */
604         if ((cmd & HFA384X_CMDCODE_MASK) == HFA384X_CMDCODE_DOWNLOAD)
605                 tries = HFA384X_DL_COMPL_TIMEOUT;
606         else
607                 tries = HFA384X_CMD_COMPL_TIMEOUT;
608
609         while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD) &&
610                tries > 0) {
611                 tries--;
612                 udelay(10);
613         }
614         if (tries == 0) {
615                 reg = HFA384X_INW(HFA384X_EVSTAT_OFF);
616                 prism2_io_debug_error(dev, 5);
617                 printk(KERN_DEBUG "%s: hfa384x_cmd_wait - timeout2 - "
618                        "reg=0x%04x\n", dev->name, reg);
619                 return -ETIMEDOUT;
620         }
621
622         res = (HFA384X_INW(HFA384X_STATUS_OFF) &
623                (BIT(14) | BIT(13) | BIT(12) | BIT(11) | BIT(10) | BIT(9) |
624                 BIT(8))) >> 8;
625 #ifndef final_version
626         if (res) {
627                 printk(KERN_DEBUG "%s: CMD=0x%04x => res=0x%02x\n",
628                        dev->name, cmd, res);
629         }
630 #endif
631
632         HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
633
634         return res;
635 }
636
637
638 /**
639  * hfa384x_cmd_no_wait - Issue a Prism2 command; do not wait for completion
640  * @dev: pointer to net_device
641  * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
642  * @param0: value for Param0 register
643  */
644 static inline int hfa384x_cmd_no_wait(struct net_device *dev, u16 cmd,
645                                       u16 param0)
646 {
647         return __hfa384x_cmd_no_wait(dev, cmd, param0, 6);
648 }
649
650
651 /**
652  * prism2_cmd_ev - Prism2 command completion event handler
653  * @dev: pointer to net_device
654  *
655  * Interrupt handler for command completion events. Called by the main
656  * interrupt handler in hardware IRQ context. Read Resp0 and status registers
657  * from the hardware and ACK the event. Depending on the issued command type
658  * either wake up the sleeping process that is waiting for command completion
659  * or call the callback function. Issue the next command, if one is pending.
660  */
661 static void prism2_cmd_ev(struct net_device *dev)
662 {
663         struct hostap_interface *iface;
664         local_info_t *local;
665         struct hostap_cmd_queue *entry = NULL;
666
667         iface = netdev_priv(dev);
668         local = iface->local;
669
670         spin_lock(&local->cmdlock);
671         if (!list_empty(&local->cmd_queue)) {
672                 entry = list_entry(local->cmd_queue.next,
673                                    struct hostap_cmd_queue, list);
674                 atomic_inc(&entry->usecnt);
675                 list_del_init(&entry->list);
676                 local->cmd_queue_len--;
677
678                 if (!entry->issued) {
679                         printk(KERN_DEBUG "%s: Command completion event, but "
680                                "cmd not issued\n", dev->name);
681                         __hostap_cmd_queue_free(local, entry, 1);
682                         entry = NULL;
683                 }
684         }
685         spin_unlock(&local->cmdlock);
686
687         if (!entry) {
688                 HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
689                 printk(KERN_DEBUG "%s: Command completion event, but no "
690                        "pending commands\n", dev->name);
691                 return;
692         }
693
694         entry->resp0 = HFA384X_INW(HFA384X_RESP0_OFF);
695         entry->res = (HFA384X_INW(HFA384X_STATUS_OFF) &
696                       (BIT(14) | BIT(13) | BIT(12) | BIT(11) | BIT(10) |
697                        BIT(9) | BIT(8))) >> 8;
698         HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
699
700         /* TODO: rest of the CmdEv handling could be moved to tasklet */
701         if (entry->type == CMD_SLEEP) {
702                 entry->type = CMD_COMPLETED;
703                 wake_up_interruptible(&entry->compl);
704         } else if (entry->type == CMD_CALLBACK) {
705                 if (entry->callback)
706                         entry->callback(dev, entry->context, entry->resp0,
707                                         entry->res);
708         } else {
709                 printk(KERN_DEBUG "%s: Invalid command completion type %d\n",
710                        dev->name, entry->type);
711         }
712         hostap_cmd_queue_free(local, entry, 1);
713
714         /* issue next command, if pending */
715         entry = NULL;
716         spin_lock(&local->cmdlock);
717         if (!list_empty(&local->cmd_queue)) {
718                 entry = list_entry(local->cmd_queue.next,
719                                    struct hostap_cmd_queue, list);
720                 if (entry->issuing) {
721                         /* hfa384x_cmd() has already started issuing this
722                          * command, so do not start here */
723                         entry = NULL;
724                 }
725                 if (entry)
726                         atomic_inc(&entry->usecnt);
727         }
728         spin_unlock(&local->cmdlock);
729
730         if (entry) {
731                 /* issue next command; if command issuing fails, remove the
732                  * entry from cmd_queue */
733                 int res = hfa384x_cmd_issue(dev, entry);
734                 spin_lock(&local->cmdlock);
735                 __hostap_cmd_queue_free(local, entry, res);
736                 spin_unlock(&local->cmdlock);
737         }
738 }
739
740
741 static int hfa384x_wait_offset(struct net_device *dev, u16 o_off)
742 {
743         int tries = HFA384X_BAP_BUSY_TIMEOUT;
744         int res = HFA384X_INW(o_off) & HFA384X_OFFSET_BUSY;
745
746         while (res && tries > 0) {
747                 tries--;
748                 udelay(1);
749                 res = HFA384X_INW(o_off) & HFA384X_OFFSET_BUSY;
750         }
751         return res;
752 }
753
754
755 /* Offset must be even */
756 static int hfa384x_setup_bap(struct net_device *dev, u16 bap, u16 id,
757                              int offset)
758 {
759         u16 o_off, s_off;
760         int ret = 0;
761
762         if (offset % 2 || bap > 1)
763                 return -EINVAL;
764
765         if (bap == BAP1) {
766                 o_off = HFA384X_OFFSET1_OFF;
767                 s_off = HFA384X_SELECT1_OFF;
768         } else {
769                 o_off = HFA384X_OFFSET0_OFF;
770                 s_off = HFA384X_SELECT0_OFF;
771         }
772
773         if (hfa384x_wait_offset(dev, o_off)) {
774                 prism2_io_debug_error(dev, 7);
775                 printk(KERN_DEBUG "%s: hfa384x_setup_bap - timeout before\n",
776                        dev->name);
777                 ret = -ETIMEDOUT;
778                 goto out;
779         }
780
781         HFA384X_OUTW(id, s_off);
782         HFA384X_OUTW(offset, o_off);
783
784         if (hfa384x_wait_offset(dev, o_off)) {
785                 prism2_io_debug_error(dev, 8);
786                 printk(KERN_DEBUG "%s: hfa384x_setup_bap - timeout after\n",
787                        dev->name);
788                 ret = -ETIMEDOUT;
789                 goto out;
790         }
791 #ifndef final_version
792         if (HFA384X_INW(o_off) & HFA384X_OFFSET_ERR) {
793                 prism2_io_debug_error(dev, 9);
794                 printk(KERN_DEBUG "%s: hfa384x_setup_bap - offset error "
795                        "(%d,0x04%x,%d); reg=0x%04x\n",
796                        dev->name, bap, id, offset, HFA384X_INW(o_off));
797                 ret = -EINVAL;
798         }
799 #endif
800
801  out:
802         return ret;
803 }
804
805
806 static int hfa384x_get_rid(struct net_device *dev, u16 rid, void *buf, int len,
807                            int exact_len)
808 {
809         struct hostap_interface *iface;
810         local_info_t *local;
811         int res, rlen = 0;
812         struct hfa384x_rid_hdr rec;
813
814         iface = netdev_priv(dev);
815         local = iface->local;
816
817         if (local->no_pri) {
818                 printk(KERN_DEBUG "%s: cannot get RID %04x (len=%d) - no PRI "
819                        "f/w\n", dev->name, rid, len);
820                 return -ENOTTY; /* Well.. not really correct, but return
821                                  * something unique enough.. */
822         }
823
824         if ((local->func->card_present && !local->func->card_present(local)) ||
825             local->hw_downloading)
826                 return -ENODEV;
827
828         res = down_interruptible(&local->rid_bap_sem);
829         if (res)
830                 return res;
831
832         res = hfa384x_cmd(dev, HFA384X_CMDCODE_ACCESS, rid, NULL, NULL);
833         if (res) {
834                 printk(KERN_DEBUG "%s: hfa384x_get_rid: CMDCODE_ACCESS failed "
835                        "(res=%d, rid=%04x, len=%d)\n",
836                        dev->name, res, rid, len);
837                 up(&local->rid_bap_sem);
838                 return res;
839         }
840
841         spin_lock_bh(&local->baplock);
842
843         res = hfa384x_setup_bap(dev, BAP0, rid, 0);
844         if (!res)
845                 res = hfa384x_from_bap(dev, BAP0, &rec, sizeof(rec));
846
847         if (le16_to_cpu(rec.len) == 0) {
848                 /* RID not available */
849                 res = -ENODATA;
850         }
851
852         rlen = (le16_to_cpu(rec.len) - 1) * 2;
853         if (!res && exact_len && rlen != len) {
854                 printk(KERN_DEBUG "%s: hfa384x_get_rid - RID len mismatch: "
855                        "rid=0x%04x, len=%d (expected %d)\n",
856                        dev->name, rid, rlen, len);
857                 res = -ENODATA;
858         }
859
860         if (!res)
861                 res = hfa384x_from_bap(dev, BAP0, buf, len);
862
863         spin_unlock_bh(&local->baplock);
864         up(&local->rid_bap_sem);
865
866         if (res) {
867                 if (res != -ENODATA)
868                         printk(KERN_DEBUG "%s: hfa384x_get_rid (rid=%04x, "
869                                "len=%d) - failed - res=%d\n", dev->name, rid,
870                                len, res);
871                 if (res == -ETIMEDOUT)
872                         prism2_hw_reset(dev);
873                 return res;
874         }
875
876         return rlen;
877 }
878
879
880 static int hfa384x_set_rid(struct net_device *dev, u16 rid, void *buf, int len)
881 {
882         struct hostap_interface *iface;
883         local_info_t *local;
884         struct hfa384x_rid_hdr rec;
885         int res;
886
887         iface = netdev_priv(dev);
888         local = iface->local;
889
890         if (local->no_pri) {
891                 printk(KERN_DEBUG "%s: cannot set RID %04x (len=%d) - no PRI "
892                        "f/w\n", dev->name, rid, len);
893                 return -ENOTTY; /* Well.. not really correct, but return
894                                  * something unique enough.. */
895         }
896
897         if ((local->func->card_present && !local->func->card_present(local)) ||
898             local->hw_downloading)
899                 return -ENODEV;
900
901         rec.rid = cpu_to_le16(rid);
902         /* RID len in words and +1 for rec.rid */
903         rec.len = cpu_to_le16(len / 2 + len % 2 + 1);
904
905         res = down_interruptible(&local->rid_bap_sem);
906         if (res)
907                 return res;
908
909         spin_lock_bh(&local->baplock);
910         res = hfa384x_setup_bap(dev, BAP0, rid, 0);
911         if (!res)
912                 res = hfa384x_to_bap(dev, BAP0, &rec, sizeof(rec));
913         if (!res)
914                 res = hfa384x_to_bap(dev, BAP0, buf, len);
915         spin_unlock_bh(&local->baplock);
916
917         if (res) {
918                 printk(KERN_DEBUG "%s: hfa384x_set_rid (rid=%04x, len=%d) - "
919                        "failed - res=%d\n", dev->name, rid, len, res);
920                 up(&local->rid_bap_sem);
921                 return res;
922         }
923
924         res = hfa384x_cmd(dev, HFA384X_CMDCODE_ACCESS_WRITE, rid, NULL, NULL);
925         up(&local->rid_bap_sem);
926
927         if (res) {
928                 printk(KERN_DEBUG "%s: hfa384x_set_rid: CMDCODE_ACCESS_WRITE "
929                        "failed (res=%d, rid=%04x, len=%d)\n",
930                        dev->name, res, rid, len);
931
932                 if (res == -ETIMEDOUT)
933                         prism2_hw_reset(dev);
934         }
935
936         return res;
937 }
938
939
940 static void hfa384x_disable_interrupts(struct net_device *dev)
941 {
942         /* disable interrupts and clear event status */
943         HFA384X_OUTW(0, HFA384X_INTEN_OFF);
944         HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
945 }
946
947
948 static void hfa384x_enable_interrupts(struct net_device *dev)
949 {
950         /* ack pending events and enable interrupts from selected events */
951         HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
952         HFA384X_OUTW(HFA384X_EVENT_MASK, HFA384X_INTEN_OFF);
953 }
954
955
956 static void hfa384x_events_no_bap0(struct net_device *dev)
957 {
958         HFA384X_OUTW(HFA384X_EVENT_MASK & ~HFA384X_BAP0_EVENTS,
959                      HFA384X_INTEN_OFF);
960 }
961
962
963 static void hfa384x_events_all(struct net_device *dev)
964 {
965         HFA384X_OUTW(HFA384X_EVENT_MASK, HFA384X_INTEN_OFF);
966 }
967
968
969 static void hfa384x_events_only_cmd(struct net_device *dev)
970 {
971         HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_INTEN_OFF);
972 }
973
974
975 static u16 hfa384x_allocate_fid(struct net_device *dev, int len)
976 {
977         u16 fid;
978         unsigned long delay;
979
980         /* FIX: this could be replace with hfa384x_cmd() if the Alloc event
981          * below would be handled like CmdCompl event (sleep here, wake up from
982          * interrupt handler */
983         if (hfa384x_cmd_wait(dev, HFA384X_CMDCODE_ALLOC, len)) {
984                 printk(KERN_DEBUG "%s: cannot allocate fid, len=%d\n",
985                        dev->name, len);
986                 return 0xffff;
987         }
988
989         delay = jiffies + HFA384X_ALLOC_COMPL_TIMEOUT;
990         while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_ALLOC) &&
991                time_before(jiffies, delay))
992                 yield();
993         if (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_ALLOC)) {
994                 printk("%s: fid allocate, len=%d - timeout\n", dev->name, len);
995                 return 0xffff;
996         }
997
998         fid = HFA384X_INW(HFA384X_ALLOCFID_OFF);
999         HFA384X_OUTW(HFA384X_EV_ALLOC, HFA384X_EVACK_OFF);
1000
1001         return fid;
1002 }
1003
1004
1005 static int prism2_reset_port(struct net_device *dev)
1006 {
1007         struct hostap_interface *iface;
1008         local_info_t *local;
1009         int res;
1010
1011         iface = netdev_priv(dev);
1012         local = iface->local;
1013
1014         if (!local->dev_enabled)
1015                 return 0;
1016
1017         res = hfa384x_cmd(dev, HFA384X_CMDCODE_DISABLE, 0,
1018                           NULL, NULL);
1019         if (res)
1020                 printk(KERN_DEBUG "%s: reset port failed to disable port\n",
1021                        dev->name);
1022         else {
1023                 res = hfa384x_cmd(dev, HFA384X_CMDCODE_ENABLE, 0,
1024                                   NULL, NULL);
1025                 if (res)
1026                         printk(KERN_DEBUG "%s: reset port failed to enable "
1027                                "port\n", dev->name);
1028         }
1029
1030         /* It looks like at least some STA firmware versions reset
1031          * fragmentation threshold back to 2346 after enable command. Restore
1032          * the configured value, if it differs from this default. */
1033         if (local->fragm_threshold != 2346 &&
1034             hostap_set_word(dev, HFA384X_RID_FRAGMENTATIONTHRESHOLD,
1035                             local->fragm_threshold)) {
1036                 printk(KERN_DEBUG "%s: failed to restore fragmentation "
1037                        "threshold (%d) after Port0 enable\n",
1038                        dev->name, local->fragm_threshold);
1039         }
1040
1041         /* Some firmwares lose antenna selection settings on reset */
1042         (void) hostap_set_antsel(local);
1043
1044         return res;
1045 }
1046
1047
1048 static int prism2_get_version_info(struct net_device *dev, u16 rid,
1049                                    const char *txt)
1050 {
1051         struct hfa384x_comp_ident comp;
1052         struct hostap_interface *iface;
1053         local_info_t *local;
1054
1055         iface = netdev_priv(dev);
1056         local = iface->local;
1057
1058         if (local->no_pri) {
1059                 /* PRI f/w not yet available - cannot read RIDs */
1060                 return -1;
1061         }
1062         if (hfa384x_get_rid(dev, rid, &comp, sizeof(comp), 1) < 0) {
1063                 printk(KERN_DEBUG "Could not get RID for component %s\n", txt);
1064                 return -1;
1065         }
1066
1067         printk(KERN_INFO "%s: %s: id=0x%02x v%d.%d.%d\n", dev->name, txt,
1068                __le16_to_cpu(comp.id), __le16_to_cpu(comp.major),
1069                __le16_to_cpu(comp.minor), __le16_to_cpu(comp.variant));
1070         return 0;
1071 }
1072
1073
1074 static int prism2_setup_rids(struct net_device *dev)
1075 {
1076         struct hostap_interface *iface;
1077         local_info_t *local;
1078         u16 tmp;
1079         int ret = 0;
1080
1081         iface = netdev_priv(dev);
1082         local = iface->local;
1083
1084         hostap_set_word(dev, HFA384X_RID_TICKTIME, 2000);
1085
1086         if (!local->fw_ap) {
1087                 tmp = hostap_get_porttype(local);
1088                 ret = hostap_set_word(dev, HFA384X_RID_CNFPORTTYPE, tmp);
1089                 if (ret) {
1090                         printk("%s: Port type setting to %d failed\n",
1091                                dev->name, tmp);
1092                         goto fail;
1093                 }
1094         }
1095
1096         /* Setting SSID to empty string seems to kill the card in Host AP mode
1097          */
1098         if (local->iw_mode != IW_MODE_MASTER || local->essid[0] != '\0') {
1099                 ret = hostap_set_string(dev, HFA384X_RID_CNFOWNSSID,
1100                                         local->essid);
1101                 if (ret) {
1102                         printk("%s: AP own SSID setting failed\n", dev->name);
1103                         goto fail;
1104                 }
1105         }
1106
1107         ret = hostap_set_word(dev, HFA384X_RID_CNFMAXDATALEN,
1108                               PRISM2_DATA_MAXLEN);
1109         if (ret) {
1110                 printk("%s: MAC data length setting to %d failed\n",
1111                        dev->name, PRISM2_DATA_MAXLEN);
1112                 goto fail;
1113         }
1114
1115         if (hfa384x_get_rid(dev, HFA384X_RID_CHANNELLIST, &tmp, 2, 1) < 0) {
1116                 printk("%s: Channel list read failed\n", dev->name);
1117                 ret = -EINVAL;
1118                 goto fail;
1119         }
1120         local->channel_mask = __le16_to_cpu(tmp);
1121
1122         if (local->channel < 1 || local->channel > 14 ||
1123             !(local->channel_mask & (1 << (local->channel - 1)))) {
1124                 printk(KERN_WARNING "%s: Channel setting out of range "
1125                        "(%d)!\n", dev->name, local->channel);
1126                 ret = -EBUSY;
1127                 goto fail;
1128         }
1129
1130         ret = hostap_set_word(dev, HFA384X_RID_CNFOWNCHANNEL, local->channel);
1131         if (ret) {
1132                 printk("%s: Channel setting to %d failed\n",
1133                        dev->name, local->channel);
1134                 goto fail;
1135         }
1136
1137         ret = hostap_set_word(dev, HFA384X_RID_CNFBEACONINT,
1138                               local->beacon_int);
1139         if (ret) {
1140                 printk("%s: Beacon interval setting to %d failed\n",
1141                        dev->name, local->beacon_int);
1142                 /* this may fail with Symbol/Lucent firmware */
1143                 if (ret == -ETIMEDOUT)
1144                         goto fail;
1145         }
1146
1147         ret = hostap_set_word(dev, HFA384X_RID_CNFOWNDTIMPERIOD,
1148                               local->dtim_period);
1149         if (ret) {
1150                 printk("%s: DTIM period setting to %d failed\n",
1151                        dev->name, local->dtim_period);
1152                 /* this may fail with Symbol/Lucent firmware */
1153                 if (ret == -ETIMEDOUT)
1154                         goto fail;
1155         }
1156
1157         ret = hostap_set_word(dev, HFA384X_RID_PROMISCUOUSMODE,
1158                               local->is_promisc);
1159         if (ret)
1160                 printk(KERN_INFO "%s: Setting promiscuous mode (%d) failed\n",
1161                        dev->name, local->is_promisc);
1162
1163         if (!local->fw_ap) {
1164                 ret = hostap_set_string(dev, HFA384X_RID_CNFDESIREDSSID,
1165                                         local->essid);
1166                 if (ret) {
1167                         printk("%s: Desired SSID setting failed\n", dev->name);
1168                         goto fail;
1169                 }
1170         }
1171
1172         /* Setup TXRateControl, defaults to allow use of 1, 2, 5.5, and
1173          * 11 Mbps in automatic TX rate fallback and 1 and 2 Mbps as basic
1174          * rates */
1175         if (local->tx_rate_control == 0) {
1176                 local->tx_rate_control =
1177                         HFA384X_RATES_1MBPS |
1178                         HFA384X_RATES_2MBPS |
1179                         HFA384X_RATES_5MBPS |
1180                         HFA384X_RATES_11MBPS;
1181         }
1182         if (local->basic_rates == 0)
1183                 local->basic_rates = HFA384X_RATES_1MBPS | HFA384X_RATES_2MBPS;
1184
1185         if (!local->fw_ap) {
1186                 ret = hostap_set_word(dev, HFA384X_RID_TXRATECONTROL,
1187                                       local->tx_rate_control);
1188                 if (ret) {
1189                         printk("%s: TXRateControl setting to %d failed\n",
1190                                dev->name, local->tx_rate_control);
1191                         goto fail;
1192                 }
1193
1194                 ret = hostap_set_word(dev, HFA384X_RID_CNFSUPPORTEDRATES,
1195                                       local->tx_rate_control);
1196                 if (ret) {
1197                         printk("%s: cnfSupportedRates setting to %d failed\n",
1198                                dev->name, local->tx_rate_control);
1199                 }
1200
1201                 ret = hostap_set_word(dev, HFA384X_RID_CNFBASICRATES,
1202                                       local->basic_rates);
1203                 if (ret) {
1204                         printk("%s: cnfBasicRates setting to %d failed\n",
1205                                dev->name, local->basic_rates);
1206                 }
1207
1208                 ret = hostap_set_word(dev, HFA384X_RID_CREATEIBSS, 1);
1209                 if (ret) {
1210                         printk("%s: Create IBSS setting to 1 failed\n",
1211                                dev->name);
1212                 }
1213         }
1214
1215         if (local->name_set)
1216                 (void) hostap_set_string(dev, HFA384X_RID_CNFOWNNAME,
1217                                          local->name);
1218
1219         if (hostap_set_encryption(local)) {
1220                 printk(KERN_INFO "%s: could not configure encryption\n",
1221                        dev->name);
1222         }
1223
1224         (void) hostap_set_antsel(local);
1225
1226         if (hostap_set_roaming(local)) {
1227                 printk(KERN_INFO "%s: could not set host roaming\n",
1228                        dev->name);
1229         }
1230
1231         if (local->sta_fw_ver >= PRISM2_FW_VER(1,6,3) &&
1232             hostap_set_word(dev, HFA384X_RID_CNFENHSECURITY, local->enh_sec))
1233                 printk(KERN_INFO "%s: cnfEnhSecurity setting to 0x%x failed\n",
1234                        dev->name, local->enh_sec);
1235
1236         /* 32-bit tallies were added in STA f/w 0.8.0, but they were apparently
1237          * not working correctly (last seven counters report bogus values).
1238          * This has been fixed in 0.8.2, so enable 32-bit tallies only
1239          * beginning with that firmware version. Another bug fix for 32-bit
1240          * tallies in 1.4.0; should 16-bit tallies be used for some other
1241          * versions, too? */
1242         if (local->sta_fw_ver >= PRISM2_FW_VER(0,8,2)) {
1243                 if (hostap_set_word(dev, HFA384X_RID_CNFTHIRTY2TALLY, 1)) {
1244                         printk(KERN_INFO "%s: cnfThirty2Tally setting "
1245                                "failed\n", dev->name);
1246                         local->tallies32 = 0;
1247                 } else
1248                         local->tallies32 = 1;
1249         } else
1250                 local->tallies32 = 0;
1251
1252         hostap_set_auth_algs(local);
1253
1254         if (hostap_set_word(dev, HFA384X_RID_FRAGMENTATIONTHRESHOLD,
1255                             local->fragm_threshold)) {
1256                 printk(KERN_INFO "%s: setting FragmentationThreshold to %d "
1257                        "failed\n", dev->name, local->fragm_threshold);
1258         }
1259
1260         if (hostap_set_word(dev, HFA384X_RID_RTSTHRESHOLD,
1261                             local->rts_threshold)) {
1262                 printk(KERN_INFO "%s: setting RTSThreshold to %d failed\n",
1263                        dev->name, local->rts_threshold);
1264         }
1265
1266         if (local->manual_retry_count >= 0 &&
1267             hostap_set_word(dev, HFA384X_RID_CNFALTRETRYCOUNT,
1268                             local->manual_retry_count)) {
1269                 printk(KERN_INFO "%s: setting cnfAltRetryCount to %d failed\n",
1270                        dev->name, local->manual_retry_count);
1271         }
1272
1273         if (local->sta_fw_ver >= PRISM2_FW_VER(1,3,1) &&
1274             hfa384x_get_rid(dev, HFA384X_RID_CNFDBMADJUST, &tmp, 2, 1) == 2) {
1275                 local->rssi_to_dBm = le16_to_cpu(tmp);
1276         }
1277
1278         if (local->sta_fw_ver >= PRISM2_FW_VER(1,7,0) && local->wpa &&
1279             hostap_set_word(dev, HFA384X_RID_SSNHANDLINGMODE, 1)) {
1280                 printk(KERN_INFO "%s: setting ssnHandlingMode to 1 failed\n",
1281                        dev->name);
1282         }
1283
1284         if (local->sta_fw_ver >= PRISM2_FW_VER(1,7,0) && local->generic_elem &&
1285             hfa384x_set_rid(dev, HFA384X_RID_GENERICELEMENT,
1286                             local->generic_elem, local->generic_elem_len)) {
1287                 printk(KERN_INFO "%s: setting genericElement failed\n",
1288                        dev->name);
1289         }
1290
1291  fail:
1292         return ret;
1293 }
1294
1295
1296 static int prism2_hw_init(struct net_device *dev, int initial)
1297 {
1298         struct hostap_interface *iface;
1299         local_info_t *local;
1300         int ret, first = 1;
1301         unsigned long start, delay;
1302
1303         PDEBUG(DEBUG_FLOW, "prism2_hw_init()\n");
1304
1305         iface = netdev_priv(dev);
1306         local = iface->local;
1307
1308         clear_bit(HOSTAP_BITS_TRANSMIT, &local->bits);
1309
1310  init:
1311         /* initialize HFA 384x */
1312         ret = hfa384x_cmd_no_wait(dev, HFA384X_CMDCODE_INIT, 0);
1313         if (ret) {
1314                 printk(KERN_INFO "%s: first command failed - assuming card "
1315                        "does not have primary firmware\n", dev_info);
1316         }
1317
1318         if (first && (HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD)) {
1319                 /* EvStat has Cmd bit set in some cases, so retry once if no
1320                  * wait was needed */
1321                 HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
1322                 printk(KERN_DEBUG "%s: init command completed too quickly - "
1323                        "retrying\n", dev->name);
1324                 first = 0;
1325                 goto init;
1326         }
1327
1328         start = jiffies;
1329         delay = jiffies + HFA384X_INIT_TIMEOUT;
1330         while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD) &&
1331                time_before(jiffies, delay))
1332                 yield();
1333         if (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD)) {
1334                 printk(KERN_DEBUG "%s: assuming no Primary image in "
1335                        "flash - card initialization not completed\n",
1336                        dev_info);
1337                 local->no_pri = 1;
1338 #ifdef PRISM2_DOWNLOAD_SUPPORT
1339                         if (local->sram_type == -1)
1340                                 local->sram_type = prism2_get_ram_size(local);
1341 #endif /* PRISM2_DOWNLOAD_SUPPORT */
1342                 return 1;
1343         }
1344         local->no_pri = 0;
1345         printk(KERN_DEBUG "prism2_hw_init: initialized in %lu ms\n",
1346                (jiffies - start) * 1000 / HZ);
1347         HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
1348         return 0;
1349 }
1350
1351
1352 static int prism2_hw_init2(struct net_device *dev, int initial)
1353 {
1354         struct hostap_interface *iface;
1355         local_info_t *local;
1356         int i;
1357
1358         iface = netdev_priv(dev);
1359         local = iface->local;
1360
1361 #ifdef PRISM2_DOWNLOAD_SUPPORT
1362         kfree(local->pda);
1363         if (local->no_pri)
1364                 local->pda = NULL;
1365         else
1366                 local->pda = prism2_read_pda(dev);
1367 #endif /* PRISM2_DOWNLOAD_SUPPORT */
1368
1369         hfa384x_disable_interrupts(dev);
1370
1371 #ifndef final_version
1372         HFA384X_OUTW(HFA384X_MAGIC, HFA384X_SWSUPPORT0_OFF);
1373         if (HFA384X_INW(HFA384X_SWSUPPORT0_OFF) != HFA384X_MAGIC) {
1374                 printk("SWSUPPORT0 write/read failed: %04X != %04X\n",
1375                        HFA384X_INW(HFA384X_SWSUPPORT0_OFF), HFA384X_MAGIC);
1376                 goto failed;
1377         }
1378 #endif
1379
1380         if (initial || local->pri_only) {
1381                 hfa384x_events_only_cmd(dev);
1382                 /* get card version information */
1383                 if (prism2_get_version_info(dev, HFA384X_RID_NICID, "NIC") ||
1384                     prism2_get_version_info(dev, HFA384X_RID_PRIID, "PRI")) {
1385                         hfa384x_disable_interrupts(dev);
1386                         goto failed;
1387                 }
1388
1389                 if (prism2_get_version_info(dev, HFA384X_RID_STAID, "STA")) {
1390                         printk(KERN_DEBUG "%s: Failed to read STA f/w version "
1391                                "- only Primary f/w present\n", dev->name);
1392                         local->pri_only = 1;
1393                         return 0;
1394                 }
1395                 local->pri_only = 0;
1396                 hfa384x_disable_interrupts(dev);
1397         }
1398
1399         /* FIX: could convert allocate_fid to use sleeping CmdCompl wait and
1400          * enable interrupts before this. This would also require some sort of
1401          * sleeping AllocEv waiting */
1402
1403         /* allocate TX FIDs */
1404         local->txfid_len = PRISM2_TXFID_LEN;
1405         for (i = 0; i < PRISM2_TXFID_COUNT; i++) {
1406                 local->txfid[i] = hfa384x_allocate_fid(dev, local->txfid_len);
1407                 if (local->txfid[i] == 0xffff && local->txfid_len > 1600) {
1408                         local->txfid[i] = hfa384x_allocate_fid(dev, 1600);
1409                         if (local->txfid[i] != 0xffff) {
1410                                 printk(KERN_DEBUG "%s: Using shorter TX FID "
1411                                        "(1600 bytes)\n", dev->name);
1412                                 local->txfid_len = 1600;
1413                         }
1414                 }
1415                 if (local->txfid[i] == 0xffff)
1416                         goto failed;
1417                 local->intransmitfid[i] = PRISM2_TXFID_EMPTY;
1418         }
1419
1420         hfa384x_events_only_cmd(dev);
1421
1422         if (initial) {
1423                 struct list_head *ptr;
1424                 prism2_check_sta_fw_version(local);
1425
1426                 if (hfa384x_get_rid(dev, HFA384X_RID_CNFOWNMACADDR,
1427                                     &dev->dev_addr, 6, 1) < 0) {
1428                         printk("%s: could not get own MAC address\n",
1429                                dev->name);
1430                 }
1431                 list_for_each(ptr, &local->hostap_interfaces) {
1432                         iface = list_entry(ptr, struct hostap_interface, list);
1433                         memcpy(iface->dev->dev_addr, dev->dev_addr, ETH_ALEN);
1434                 }
1435         } else if (local->fw_ap)
1436                 prism2_check_sta_fw_version(local);
1437
1438         prism2_setup_rids(dev);
1439
1440         /* MAC is now configured, but port 0 is not yet enabled */
1441         return 0;
1442
1443  failed:
1444         if (!local->no_pri)
1445                 printk(KERN_WARNING "%s: Initialization failed\n", dev_info);
1446         return 1;
1447 }
1448
1449
1450 static int prism2_hw_enable(struct net_device *dev, int initial)
1451 {
1452         struct hostap_interface *iface;
1453         local_info_t *local;
1454         int was_resetting;
1455
1456         iface = netdev_priv(dev);
1457         local = iface->local;
1458         was_resetting = local->hw_resetting;
1459
1460         if (hfa384x_cmd(dev, HFA384X_CMDCODE_ENABLE, 0, NULL, NULL)) {
1461                 printk("%s: MAC port 0 enabling failed\n", dev->name);
1462                 return 1;
1463         }
1464
1465         local->hw_ready = 1;
1466         local->hw_reset_tries = 0;
1467         local->hw_resetting = 0;
1468         hfa384x_enable_interrupts(dev);
1469
1470         /* at least D-Link DWL-650 seems to require additional port reset
1471          * before it starts acting as an AP, so reset port automatically
1472          * here just in case */
1473         if (initial && prism2_reset_port(dev)) {
1474                 printk("%s: MAC port 0 reseting failed\n", dev->name);
1475                 return 1;
1476         }
1477
1478         if (was_resetting && netif_queue_stopped(dev)) {
1479                 /* If hw_reset() was called during pending transmit, netif
1480                  * queue was stopped. Wake it up now since the wlan card has
1481                  * been resetted. */
1482                 netif_wake_queue(dev);
1483         }
1484
1485         return 0;
1486 }
1487
1488
1489 static int prism2_hw_config(struct net_device *dev, int initial)
1490 {
1491         struct hostap_interface *iface;
1492         local_info_t *local;
1493
1494         iface = netdev_priv(dev);
1495         local = iface->local;
1496
1497         if (local->hw_downloading)
1498                 return 1;
1499
1500         if (prism2_hw_init(dev, initial)) {
1501                 return local->no_pri ? 0 : 1;
1502         }
1503
1504         if (prism2_hw_init2(dev, initial))
1505                 return 1;
1506
1507         /* Enable firmware if secondary image is loaded and at least one of the
1508          * netdevices is up. */
1509         if (!local->pri_only &&
1510             (initial == 0 || (initial == 2 && local->num_dev_open > 0))) {
1511                 if (!local->dev_enabled)
1512                         prism2_callback(local, PRISM2_CALLBACK_ENABLE);
1513                 local->dev_enabled = 1;
1514                 return prism2_hw_enable(dev, initial);
1515         }
1516
1517         return 0;
1518 }
1519
1520
1521 static void prism2_hw_shutdown(struct net_device *dev, int no_disable)
1522 {
1523         struct hostap_interface *iface;
1524         local_info_t *local;
1525
1526         iface = netdev_priv(dev);
1527         local = iface->local;
1528
1529         /* Allow only command completion events during disable */
1530         hfa384x_events_only_cmd(dev);
1531
1532         local->hw_ready = 0;
1533         if (local->dev_enabled)
1534                 prism2_callback(local, PRISM2_CALLBACK_DISABLE);
1535         local->dev_enabled = 0;
1536
1537         if (local->func->card_present && !local->func->card_present(local)) {
1538                 printk(KERN_DEBUG "%s: card already removed or not configured "
1539                        "during shutdown\n", dev->name);
1540                 return;
1541         }
1542
1543         if ((no_disable & HOSTAP_HW_NO_DISABLE) == 0 &&
1544             hfa384x_cmd(dev, HFA384X_CMDCODE_DISABLE, 0, NULL, NULL))
1545                 printk(KERN_WARNING "%s: Shutdown failed\n", dev_info);
1546
1547         hfa384x_disable_interrupts(dev);
1548
1549         if (no_disable & HOSTAP_HW_ENABLE_CMDCOMPL)
1550                 hfa384x_events_only_cmd(dev);
1551         else
1552                 prism2_clear_cmd_queue(local);
1553 }
1554
1555
1556 static void prism2_hw_reset(struct net_device *dev)
1557 {
1558         struct hostap_interface *iface;
1559         local_info_t *local;
1560
1561 #if 0
1562         static long last_reset = 0;
1563
1564         /* do not reset card more than once per second to avoid ending up in a
1565          * busy loop reseting the card */
1566         if (time_before_eq(jiffies, last_reset + HZ))
1567                 return;
1568         last_reset = jiffies;
1569 #endif
1570
1571         iface = netdev_priv(dev);
1572         local = iface->local;
1573
1574         if (in_interrupt()) {
1575                 printk(KERN_DEBUG "%s: driver bug - prism2_hw_reset() called "
1576                        "in interrupt context\n", dev->name);
1577                 return;
1578         }
1579
1580         if (local->hw_downloading)
1581                 return;
1582
1583         if (local->hw_resetting) {
1584                 printk(KERN_WARNING "%s: %s: already resetting card - "
1585                        "ignoring reset request\n", dev_info, dev->name);
1586                 return;
1587         }
1588
1589         local->hw_reset_tries++;
1590         if (local->hw_reset_tries > 10) {
1591                 printk(KERN_WARNING "%s: too many reset tries, skipping\n",
1592                        dev->name);
1593                 return;
1594         }
1595
1596         printk(KERN_WARNING "%s: %s: resetting card\n", dev_info, dev->name);
1597         hfa384x_disable_interrupts(dev);
1598         local->hw_resetting = 1;
1599         if (local->func->cor_sreset) {
1600                 /* Host system seems to hang in some cases with high traffic
1601                  * load or shared interrupts during COR sreset. Disable shared
1602                  * interrupts during reset to avoid these crashes. COS sreset
1603                  * takes quite a long time, so it is unfortunate that this
1604                  * seems to be needed. Anyway, I do not know of any better way
1605                  * of avoiding the crash. */
1606                 disable_irq(dev->irq);
1607                 local->func->cor_sreset(local);
1608                 enable_irq(dev->irq);
1609         }
1610         prism2_hw_shutdown(dev, 1);
1611         prism2_hw_config(dev, 0);
1612         local->hw_resetting = 0;
1613
1614 #ifdef PRISM2_DOWNLOAD_SUPPORT
1615         if (local->dl_pri) {
1616                 printk(KERN_DEBUG "%s: persistent download of primary "
1617                        "firmware\n", dev->name);
1618                 if (prism2_download_genesis(local, local->dl_pri) < 0)
1619                         printk(KERN_WARNING "%s: download (PRI) failed\n",
1620                                dev->name);
1621         }
1622
1623         if (local->dl_sec) {
1624                 printk(KERN_DEBUG "%s: persistent download of secondary "
1625                        "firmware\n", dev->name);
1626                 if (prism2_download_volatile(local, local->dl_sec) < 0)
1627                         printk(KERN_WARNING "%s: download (SEC) failed\n",
1628                                dev->name);
1629         }
1630 #endif /* PRISM2_DOWNLOAD_SUPPORT */
1631
1632         /* TODO: restore beacon TIM bits for STAs that have buffered frames */
1633 }
1634
1635
1636 static void prism2_schedule_reset(local_info_t *local)
1637 {
1638         schedule_work(&local->reset_queue);
1639 }
1640
1641
1642 /* Called only as scheduled task after noticing card timeout in interrupt
1643  * context */
1644 static void handle_reset_queue(struct work_struct *work)
1645 {
1646         local_info_t *local = container_of(work, local_info_t, reset_queue);
1647
1648         printk(KERN_DEBUG "%s: scheduled card reset\n", local->dev->name);
1649         prism2_hw_reset(local->dev);
1650
1651         if (netif_queue_stopped(local->dev)) {
1652                 int i;
1653
1654                 for (i = 0; i < PRISM2_TXFID_COUNT; i++)
1655                         if (local->intransmitfid[i] == PRISM2_TXFID_EMPTY) {
1656                                 PDEBUG(DEBUG_EXTRA, "prism2_tx_timeout: "
1657                                        "wake up queue\n");
1658                                 netif_wake_queue(local->dev);
1659                                 break;
1660                         }
1661         }
1662 }
1663
1664
1665 static int prism2_get_txfid_idx(local_info_t *local)
1666 {
1667         int idx, end;
1668         unsigned long flags;
1669
1670         spin_lock_irqsave(&local->txfidlock, flags);
1671         end = idx = local->next_txfid;
1672         do {
1673                 if (local->intransmitfid[idx] == PRISM2_TXFID_EMPTY) {
1674                         local->intransmitfid[idx] = PRISM2_TXFID_RESERVED;
1675                         spin_unlock_irqrestore(&local->txfidlock, flags);
1676                         return idx;
1677                 }
1678                 idx++;
1679                 if (idx >= PRISM2_TXFID_COUNT)
1680                         idx = 0;
1681         } while (idx != end);
1682         spin_unlock_irqrestore(&local->txfidlock, flags);
1683
1684         PDEBUG(DEBUG_EXTRA2, "prism2_get_txfid_idx: no room in txfid buf: "
1685                "packet dropped\n");
1686         local->stats.tx_dropped++;
1687
1688         return -1;
1689 }
1690
1691
1692 /* Called only from hardware IRQ */
1693 static void prism2_transmit_cb(struct net_device *dev, long context,
1694                                u16 resp0, u16 res)
1695 {
1696         struct hostap_interface *iface;
1697         local_info_t *local;
1698         int idx = (int) context;
1699
1700         iface = netdev_priv(dev);
1701         local = iface->local;
1702
1703         if (res) {
1704                 printk(KERN_DEBUG "%s: prism2_transmit_cb - res=0x%02x\n",
1705                        dev->name, res);
1706                 return;
1707         }
1708
1709         if (idx < 0 || idx >= PRISM2_TXFID_COUNT) {
1710                 printk(KERN_DEBUG "%s: prism2_transmit_cb called with invalid "
1711                        "idx=%d\n", dev->name, idx);
1712                 return;
1713         }
1714
1715         if (!test_and_clear_bit(HOSTAP_BITS_TRANSMIT, &local->bits)) {
1716                 printk(KERN_DEBUG "%s: driver bug: prism2_transmit_cb called "
1717                        "with no pending transmit\n", dev->name);
1718         }
1719
1720         if (netif_queue_stopped(dev)) {
1721                 /* ready for next TX, so wake up queue that was stopped in
1722                  * prism2_transmit() */
1723                 netif_wake_queue(dev);
1724         }
1725
1726         spin_lock(&local->txfidlock);
1727
1728         /* With reclaim, Resp0 contains new txfid for transmit; the old txfid
1729          * will be automatically allocated for the next TX frame */
1730         local->intransmitfid[idx] = resp0;
1731
1732         PDEBUG(DEBUG_FID, "%s: prism2_transmit_cb: txfid[%d]=0x%04x, "
1733                "resp0=0x%04x, transmit_txfid=0x%04x\n",
1734                dev->name, idx, local->txfid[idx],
1735                resp0, local->intransmitfid[local->next_txfid]);
1736
1737         idx++;
1738         if (idx >= PRISM2_TXFID_COUNT)
1739                 idx = 0;
1740         local->next_txfid = idx;
1741
1742         /* check if all TX buffers are occupied */
1743         do {
1744                 if (local->intransmitfid[idx] == PRISM2_TXFID_EMPTY) {
1745                         spin_unlock(&local->txfidlock);
1746                         return;
1747                 }
1748                 idx++;
1749                 if (idx >= PRISM2_TXFID_COUNT)
1750                         idx = 0;
1751         } while (idx != local->next_txfid);
1752         spin_unlock(&local->txfidlock);
1753
1754         /* no empty TX buffers, stop queue */
1755         netif_stop_queue(dev);
1756 }
1757
1758
1759 /* Called only from software IRQ if PCI bus master is not used (with bus master
1760  * this can be called both from software and hardware IRQ) */
1761 static int prism2_transmit(struct net_device *dev, int idx)
1762 {
1763         struct hostap_interface *iface;
1764         local_info_t *local;
1765         int res;
1766
1767         iface = netdev_priv(dev);
1768         local = iface->local;
1769
1770         /* The driver tries to stop netif queue so that there would not be
1771          * more than one attempt to transmit frames going on; check that this
1772          * is really the case */
1773
1774         if (test_and_set_bit(HOSTAP_BITS_TRANSMIT, &local->bits)) {
1775                 printk(KERN_DEBUG "%s: driver bug - prism2_transmit() called "
1776                        "when previous TX was pending\n", dev->name);
1777                 return -1;
1778         }
1779
1780         /* stop the queue for the time that transmit is pending */
1781         netif_stop_queue(dev);
1782
1783         /* transmit packet */
1784         res = hfa384x_cmd_callback(
1785                 dev,
1786                 HFA384X_CMDCODE_TRANSMIT | HFA384X_CMD_TX_RECLAIM,
1787                 local->txfid[idx],
1788                 prism2_transmit_cb, (long) idx);
1789
1790         if (res) {
1791                 struct net_device_stats *stats;
1792                 printk(KERN_DEBUG "%s: prism2_transmit: CMDCODE_TRANSMIT "
1793                        "failed (res=%d)\n", dev->name, res);
1794                 stats = hostap_get_stats(dev);
1795                 stats->tx_dropped++;
1796                 netif_wake_queue(dev);
1797                 return -1;
1798         }
1799         dev->trans_start = jiffies;
1800
1801         /* Since we did not wait for command completion, the card continues
1802          * to process on the background and we will finish handling when
1803          * command completion event is handled (prism2_cmd_ev() function) */
1804
1805         return 0;
1806 }
1807
1808
1809 /* Send IEEE 802.11 frame (convert the header into Prism2 TX descriptor and
1810  * send the payload with this descriptor) */
1811 /* Called only from software IRQ */
1812 static int prism2_tx_80211(struct sk_buff *skb, struct net_device *dev)
1813 {
1814         struct hostap_interface *iface;
1815         local_info_t *local;
1816         struct hfa384x_tx_frame txdesc;
1817         struct hostap_skb_tx_data *meta;
1818         int hdr_len, data_len, idx, res, ret = -1;
1819         u16 tx_control, fc;
1820
1821         iface = netdev_priv(dev);
1822         local = iface->local;
1823
1824         meta = (struct hostap_skb_tx_data *) skb->cb;
1825
1826         prism2_callback(local, PRISM2_CALLBACK_TX_START);
1827
1828         if ((local->func->card_present && !local->func->card_present(local)) ||
1829             !local->hw_ready || local->hw_downloading || local->pri_only) {
1830                 if (net_ratelimit()) {
1831                         printk(KERN_DEBUG "%s: prism2_tx_80211: hw not ready -"
1832                                " skipping\n", dev->name);
1833                 }
1834                 goto fail;
1835         }
1836
1837         memset(&txdesc, 0, sizeof(txdesc));
1838
1839         /* skb->data starts with txdesc->frame_control */
1840         hdr_len = 24;
1841         memcpy(&txdesc.frame_control, skb->data, hdr_len);
1842         fc = le16_to_cpu(txdesc.frame_control);
1843         if (WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA &&
1844             (fc & IEEE80211_FCTL_FROMDS) && (fc & IEEE80211_FCTL_TODS) &&
1845             skb->len >= 30) {
1846                 /* Addr4 */
1847                 memcpy(txdesc.addr4, skb->data + hdr_len, ETH_ALEN);
1848                 hdr_len += ETH_ALEN;
1849         }
1850
1851         tx_control = local->tx_control;
1852         if (meta->tx_cb_idx) {
1853                 tx_control |= HFA384X_TX_CTRL_TX_OK;
1854                 txdesc.sw_support = cpu_to_le16(meta->tx_cb_idx);
1855         }
1856         txdesc.tx_control = cpu_to_le16(tx_control);
1857         txdesc.tx_rate = meta->rate;
1858
1859         data_len = skb->len - hdr_len;
1860         txdesc.data_len = cpu_to_le16(data_len);
1861         txdesc.len = cpu_to_be16(data_len);
1862
1863         idx = prism2_get_txfid_idx(local);
1864         if (idx < 0)
1865                 goto fail;
1866
1867         if (local->frame_dump & PRISM2_DUMP_TX_HDR)
1868                 hostap_dump_tx_header(dev->name, &txdesc);
1869
1870         spin_lock(&local->baplock);
1871         res = hfa384x_setup_bap(dev, BAP0, local->txfid[idx], 0);
1872
1873         if (!res)
1874                 res = hfa384x_to_bap(dev, BAP0, &txdesc, sizeof(txdesc));
1875         if (!res)
1876                 res = hfa384x_to_bap(dev, BAP0, skb->data + hdr_len,
1877                                      skb->len - hdr_len);
1878         spin_unlock(&local->baplock);
1879
1880         if (!res)
1881                 res = prism2_transmit(dev, idx);
1882         if (res) {
1883                 printk(KERN_DEBUG "%s: prism2_tx_80211 - to BAP0 failed\n",
1884                        dev->name);
1885                 local->intransmitfid[idx] = PRISM2_TXFID_EMPTY;
1886                 schedule_work(&local->reset_queue);
1887                 goto fail;
1888         }
1889
1890         ret = 0;
1891
1892 fail:
1893         prism2_callback(local, PRISM2_CALLBACK_TX_END);
1894         return ret;
1895 }
1896
1897
1898 /* Some SMP systems have reported number of odd errors with hostap_pci. fid
1899  * register has changed values between consecutive reads for an unknown reason.
1900  * This should really not happen, so more debugging is needed. This test
1901  * version is a big slower, but it will detect most of such register changes
1902  * and will try to get the correct fid eventually. */
1903 #define EXTRA_FID_READ_TESTS
1904
1905 static u16 prism2_read_fid_reg(struct net_device *dev, u16 reg)
1906 {
1907 #ifdef EXTRA_FID_READ_TESTS
1908         u16 val, val2, val3;
1909         int i;
1910
1911         for (i = 0; i < 10; i++) {
1912                 val = HFA384X_INW(reg);
1913                 val2 = HFA384X_INW(reg);
1914                 val3 = HFA384X_INW(reg);
1915
1916                 if (val == val2 && val == val3)
1917                         return val;
1918
1919                 printk(KERN_DEBUG "%s: detected fid change (try=%d, reg=%04x):"
1920                        " %04x %04x %04x\n",
1921                        dev->name, i, reg, val, val2, val3);
1922                 if ((val == val2 || val == val3) && val != 0)
1923                         return val;
1924                 if (val2 == val3 && val2 != 0)
1925                         return val2;
1926         }
1927         printk(KERN_WARNING "%s: Uhhuh.. could not read good fid from reg "
1928                "%04x (%04x %04x %04x)\n", dev->name, reg, val, val2, val3);
1929         return val;
1930 #else /* EXTRA_FID_READ_TESTS */
1931         return HFA384X_INW(reg);
1932 #endif /* EXTRA_FID_READ_TESTS */
1933 }
1934
1935
1936 /* Called only as a tasklet (software IRQ) */
1937 static void prism2_rx(local_info_t *local)
1938 {
1939         struct net_device *dev = local->dev;
1940         int res, rx_pending = 0;
1941         u16 len, hdr_len, rxfid, status, macport;
1942         struct net_device_stats *stats;
1943         struct hfa384x_rx_frame rxdesc;
1944         struct sk_buff *skb = NULL;
1945
1946         prism2_callback(local, PRISM2_CALLBACK_RX_START);
1947         stats = hostap_get_stats(dev);
1948
1949         rxfid = prism2_read_fid_reg(dev, HFA384X_RXFID_OFF);
1950 #ifndef final_version
1951         if (rxfid == 0) {
1952                 rxfid = HFA384X_INW(HFA384X_RXFID_OFF);
1953                 printk(KERN_DEBUG "prism2_rx: rxfid=0 (next 0x%04x)\n",
1954                        rxfid);
1955                 if (rxfid == 0) {
1956                         schedule_work(&local->reset_queue);
1957                         goto rx_dropped;
1958                 }
1959                 /* try to continue with the new rxfid value */
1960         }
1961 #endif
1962
1963         spin_lock(&local->baplock);
1964         res = hfa384x_setup_bap(dev, BAP0, rxfid, 0);
1965         if (!res)
1966                 res = hfa384x_from_bap(dev, BAP0, &rxdesc, sizeof(rxdesc));
1967
1968         if (res) {
1969                 spin_unlock(&local->baplock);
1970                 printk(KERN_DEBUG "%s: copy from BAP0 failed %d\n", dev->name,
1971                        res);
1972                 if (res == -ETIMEDOUT) {
1973                         schedule_work(&local->reset_queue);
1974                 }
1975                 goto rx_dropped;
1976         }
1977
1978         len = le16_to_cpu(rxdesc.data_len);
1979         hdr_len = sizeof(rxdesc);
1980         status = le16_to_cpu(rxdesc.status);
1981         macport = (status >> 8) & 0x07;
1982
1983         /* Drop frames with too large reported payload length. Monitor mode
1984          * seems to sometimes pass frames (e.g., ctrl::ack) with signed and
1985          * negative value, so allow also values 65522 .. 65534 (-14 .. -2) for
1986          * macport 7 */
1987         if (len > PRISM2_DATA_MAXLEN + 8 /* WEP */) {
1988                 if (macport == 7 && local->iw_mode == IW_MODE_MONITOR) {
1989                         if (len >= (u16) -14) {
1990                                 hdr_len -= 65535 - len;
1991                                 hdr_len--;
1992                         }
1993                         len = 0;
1994                 } else {
1995                         spin_unlock(&local->baplock);
1996                         printk(KERN_DEBUG "%s: Received frame with invalid "
1997                                "length 0x%04x\n", dev->name, len);
1998                         hostap_dump_rx_header(dev->name, &rxdesc);
1999                         goto rx_dropped;
2000                 }
2001         }
2002
2003         skb = dev_alloc_skb(len + hdr_len);
2004         if (!skb) {
2005                 spin_unlock(&local->baplock);
2006                 printk(KERN_DEBUG "%s: RX failed to allocate skb\n",
2007                        dev->name);
2008                 goto rx_dropped;
2009         }
2010         skb->dev = dev;
2011         memcpy(skb_put(skb, hdr_len), &rxdesc, hdr_len);
2012
2013         if (len > 0)
2014                 res = hfa384x_from_bap(dev, BAP0, skb_put(skb, len), len);
2015         spin_unlock(&local->baplock);
2016         if (res) {
2017                 printk(KERN_DEBUG "%s: RX failed to read "
2018                        "frame data\n", dev->name);
2019                 goto rx_dropped;
2020         }
2021
2022         skb_queue_tail(&local->rx_list, skb);
2023         tasklet_schedule(&local->rx_tasklet);
2024
2025  rx_exit:
2026         prism2_callback(local, PRISM2_CALLBACK_RX_END);
2027         if (!rx_pending) {
2028                 HFA384X_OUTW(HFA384X_EV_RX, HFA384X_EVACK_OFF);
2029         }
2030
2031         return;
2032
2033  rx_dropped:
2034         stats->rx_dropped++;
2035         if (skb)
2036                 dev_kfree_skb(skb);
2037         goto rx_exit;
2038 }
2039
2040
2041 /* Called only as a tasklet (software IRQ) */
2042 static void hostap_rx_skb(local_info_t *local, struct sk_buff *skb)
2043 {
2044         struct hfa384x_rx_frame *rxdesc;
2045         struct net_device *dev = skb->dev;
2046         struct hostap_80211_rx_status stats;
2047         int hdrlen, rx_hdrlen;
2048
2049         rx_hdrlen = sizeof(*rxdesc);
2050         if (skb->len < sizeof(*rxdesc)) {
2051                 /* Allow monitor mode to receive shorter frames */
2052                 if (local->iw_mode == IW_MODE_MONITOR &&
2053                     skb->len >= sizeof(*rxdesc) - 30) {
2054                         rx_hdrlen = skb->len;
2055                 } else {
2056                         dev_kfree_skb(skb);
2057                         return;
2058                 }
2059         }
2060
2061         rxdesc = (struct hfa384x_rx_frame *) skb->data;
2062
2063         if (local->frame_dump & PRISM2_DUMP_RX_HDR &&
2064             skb->len >= sizeof(*rxdesc))
2065                 hostap_dump_rx_header(dev->name, rxdesc);
2066
2067         if (le16_to_cpu(rxdesc->status) & HFA384X_RX_STATUS_FCSERR &&
2068             (!local->monitor_allow_fcserr ||
2069              local->iw_mode != IW_MODE_MONITOR))
2070                 goto drop;
2071
2072         if (skb->len > PRISM2_DATA_MAXLEN) {
2073                 printk(KERN_DEBUG "%s: RX: len(%d) > MAX(%d)\n",
2074                        dev->name, skb->len, PRISM2_DATA_MAXLEN);
2075                 goto drop;
2076         }
2077
2078         stats.mac_time = le32_to_cpu(rxdesc->time);
2079         stats.signal = rxdesc->signal - local->rssi_to_dBm;
2080         stats.noise = rxdesc->silence - local->rssi_to_dBm;
2081         stats.rate = rxdesc->rate;
2082
2083         /* Convert Prism2 RX structure into IEEE 802.11 header */
2084         hdrlen = hostap_80211_get_hdrlen(le16_to_cpu(rxdesc->frame_control));
2085         if (hdrlen > rx_hdrlen)
2086                 hdrlen = rx_hdrlen;
2087
2088         memmove(skb_pull(skb, rx_hdrlen - hdrlen),
2089                 &rxdesc->frame_control, hdrlen);
2090
2091         hostap_80211_rx(dev, skb, &stats);
2092         return;
2093
2094  drop:
2095         dev_kfree_skb(skb);
2096 }
2097
2098
2099 /* Called only as a tasklet (software IRQ) */
2100 static void hostap_rx_tasklet(unsigned long data)
2101 {
2102         local_info_t *local = (local_info_t *) data;
2103         struct sk_buff *skb;
2104
2105         while ((skb = skb_dequeue(&local->rx_list)) != NULL)
2106                 hostap_rx_skb(local, skb);
2107 }
2108
2109
2110 /* Called only from hardware IRQ */
2111 static void prism2_alloc_ev(struct net_device *dev)
2112 {
2113         struct hostap_interface *iface;
2114         local_info_t *local;
2115         int idx;
2116         u16 fid;
2117
2118         iface = netdev_priv(dev);
2119         local = iface->local;
2120
2121         fid = prism2_read_fid_reg(dev, HFA384X_ALLOCFID_OFF);
2122
2123         PDEBUG(DEBUG_FID, "FID: interrupt: ALLOC - fid=0x%04x\n", fid);
2124
2125         spin_lock(&local->txfidlock);
2126         idx = local->next_alloc;
2127
2128         do {
2129                 if (local->txfid[idx] == fid) {
2130                         PDEBUG(DEBUG_FID, "FID: found matching txfid[%d]\n",
2131                                idx);
2132
2133 #ifndef final_version
2134                         if (local->intransmitfid[idx] == PRISM2_TXFID_EMPTY)
2135                                 printk("Already released txfid found at idx "
2136                                        "%d\n", idx);
2137                         if (local->intransmitfid[idx] == PRISM2_TXFID_RESERVED)
2138                                 printk("Already reserved txfid found at idx "
2139                                        "%d\n", idx);
2140 #endif
2141                         local->intransmitfid[idx] = PRISM2_TXFID_EMPTY;
2142                         idx++;
2143                         local->next_alloc = idx >= PRISM2_TXFID_COUNT ? 0 :
2144                                 idx;
2145
2146                         if (!test_bit(HOSTAP_BITS_TRANSMIT, &local->bits) &&
2147                             netif_queue_stopped(dev))
2148                                 netif_wake_queue(dev);
2149
2150                         spin_unlock(&local->txfidlock);
2151                         return;
2152                 }
2153
2154                 idx++;
2155                 if (idx >= PRISM2_TXFID_COUNT)
2156                         idx = 0;
2157         } while (idx != local->next_alloc);
2158
2159         printk(KERN_WARNING "%s: could not find matching txfid (0x%04x, new "
2160                "read 0x%04x) for alloc event\n", dev->name, fid,
2161                HFA384X_INW(HFA384X_ALLOCFID_OFF));
2162         printk(KERN_DEBUG "TXFIDs:");
2163         for (idx = 0; idx < PRISM2_TXFID_COUNT; idx++)
2164                 printk(" %04x[%04x]", local->txfid[idx],
2165                        local->intransmitfid[idx]);
2166         printk("\n");
2167         spin_unlock(&local->txfidlock);
2168
2169         /* FIX: should probably schedule reset; reference to one txfid was lost
2170          * completely.. Bad things will happen if we run out of txfids
2171          * Actually, this will cause netdev watchdog to notice TX timeout and
2172          * then card reset after all txfids have been leaked. */
2173 }
2174
2175
2176 /* Called only as a tasklet (software IRQ) */
2177 static void hostap_tx_callback(local_info_t *local,
2178                                struct hfa384x_tx_frame *txdesc, int ok,
2179                                char *payload)
2180 {
2181         u16 sw_support, hdrlen, len;
2182         struct sk_buff *skb;
2183         struct hostap_tx_callback_info *cb;
2184
2185         /* Make sure that frame was from us. */
2186         if (memcmp(txdesc->addr2, local->dev->dev_addr, ETH_ALEN)) {
2187                 printk(KERN_DEBUG "%s: TX callback - foreign frame\n",
2188                        local->dev->name);
2189                 return;
2190         }
2191
2192         sw_support = le16_to_cpu(txdesc->sw_support);
2193
2194         spin_lock(&local->lock);
2195         cb = local->tx_callback;
2196         while (cb != NULL && cb->idx != sw_support)
2197                 cb = cb->next;
2198         spin_unlock(&local->lock);
2199
2200         if (cb == NULL) {
2201                 printk(KERN_DEBUG "%s: could not find TX callback (idx %d)\n",
2202                        local->dev->name, sw_support);
2203                 return;
2204         }
2205
2206         hdrlen = hostap_80211_get_hdrlen(le16_to_cpu(txdesc->frame_control));
2207         len = le16_to_cpu(txdesc->data_len);
2208         skb = dev_alloc_skb(hdrlen + len);
2209         if (skb == NULL) {
2210                 printk(KERN_DEBUG "%s: hostap_tx_callback failed to allocate "
2211                        "skb\n", local->dev->name);
2212                 return;
2213         }
2214
2215         memcpy(skb_put(skb, hdrlen), (void *) &txdesc->frame_control, hdrlen);
2216         if (payload)
2217                 memcpy(skb_put(skb, len), payload, len);
2218
2219         skb->dev = local->dev;
2220         skb->mac.raw = skb->data;
2221
2222         cb->func(skb, ok, cb->data);
2223 }
2224
2225
2226 /* Called only as a tasklet (software IRQ) */
2227 static int hostap_tx_compl_read(local_info_t *local, int error,
2228                                 struct hfa384x_tx_frame *txdesc,
2229                                 char **payload)
2230 {
2231         u16 fid, len;
2232         int res, ret = 0;
2233         struct net_device *dev = local->dev;
2234
2235         fid = prism2_read_fid_reg(dev, HFA384X_TXCOMPLFID_OFF);
2236
2237         PDEBUG(DEBUG_FID, "interrupt: TX (err=%d) - fid=0x%04x\n", fid, error);
2238
2239         spin_lock(&local->baplock);
2240         res = hfa384x_setup_bap(dev, BAP0, fid, 0);
2241         if (!res)
2242                 res = hfa384x_from_bap(dev, BAP0, txdesc, sizeof(*txdesc));
2243         if (res) {
2244                 PDEBUG(DEBUG_EXTRA, "%s: TX (err=%d) - fid=0x%04x - could not "
2245                        "read txdesc\n", dev->name, error, fid);
2246                 if (res == -ETIMEDOUT) {
2247                         schedule_work(&local->reset_queue);
2248                 }
2249                 ret = -1;
2250                 goto fail;
2251         }
2252         if (txdesc->sw_support) {
2253                 len = le16_to_cpu(txdesc->data_len);
2254                 if (len < PRISM2_DATA_MAXLEN) {
2255                         *payload = kmalloc(len, GFP_ATOMIC);
2256                         if (*payload == NULL ||
2257                             hfa384x_from_bap(dev, BAP0, *payload, len)) {
2258                                 PDEBUG(DEBUG_EXTRA, "%s: could not read TX "
2259                                        "frame payload\n", dev->name);
2260                                 kfree(*payload);
2261                                 *payload = NULL;
2262                                 ret = -1;
2263                                 goto fail;
2264                         }
2265                 }
2266         }
2267
2268  fail:
2269         spin_unlock(&local->baplock);
2270
2271         return ret;
2272 }
2273
2274
2275 /* Called only as a tasklet (software IRQ) */
2276 static void prism2_tx_ev(local_info_t *local)
2277 {
2278         struct net_device *dev = local->dev;
2279         char *payload = NULL;
2280         struct hfa384x_tx_frame txdesc;
2281
2282         if (hostap_tx_compl_read(local, 0, &txdesc, &payload))
2283                 goto fail;
2284
2285         if (local->frame_dump & PRISM2_DUMP_TX_HDR) {
2286                 PDEBUG(DEBUG_EXTRA, "%s: TX - status=0x%04x "
2287                        "retry_count=%d tx_rate=%d seq_ctrl=%d "
2288                        "duration_id=%d\n",
2289                        dev->name, le16_to_cpu(txdesc.status),
2290                        txdesc.retry_count, txdesc.tx_rate,
2291                        le16_to_cpu(txdesc.seq_ctrl),
2292                        le16_to_cpu(txdesc.duration_id));
2293         }
2294
2295         if (txdesc.sw_support)
2296                 hostap_tx_callback(local, &txdesc, 1, payload);
2297         kfree(payload);
2298
2299  fail:
2300         HFA384X_OUTW(HFA384X_EV_TX, HFA384X_EVACK_OFF);
2301 }
2302
2303
2304 /* Called only as a tasklet (software IRQ) */
2305 static void hostap_sta_tx_exc_tasklet(unsigned long data)
2306 {
2307         local_info_t *local = (local_info_t *) data;
2308         struct sk_buff *skb;
2309
2310         while ((skb = skb_dequeue(&local->sta_tx_exc_list)) != NULL) {
2311                 struct hfa384x_tx_frame *txdesc =
2312                         (struct hfa384x_tx_frame *) skb->data;
2313
2314                 if (skb->len >= sizeof(*txdesc)) {
2315                         /* Convert Prism2 RX structure into IEEE 802.11 header
2316                          */
2317                         u16 fc = le16_to_cpu(txdesc->frame_control);
2318                         int hdrlen = hostap_80211_get_hdrlen(fc);
2319                         memmove(skb_pull(skb, sizeof(*txdesc) - hdrlen),
2320                                 &txdesc->frame_control, hdrlen);
2321
2322                         hostap_handle_sta_tx_exc(local, skb);
2323                 }
2324                 dev_kfree_skb(skb);
2325         }
2326 }
2327
2328
2329 /* Called only as a tasklet (software IRQ) */
2330 static void prism2_txexc(local_info_t *local)
2331 {
2332         struct net_device *dev = local->dev;
2333         u16 status, fc;
2334         int show_dump, res;
2335         char *payload = NULL;
2336         struct hfa384x_tx_frame txdesc;
2337
2338         show_dump = local->frame_dump & PRISM2_DUMP_TXEXC_HDR;
2339         local->stats.tx_errors++;
2340
2341         res = hostap_tx_compl_read(local, 1, &txdesc, &payload);
2342         HFA384X_OUTW(HFA384X_EV_TXEXC, HFA384X_EVACK_OFF);
2343         if (res)
2344                 return;
2345
2346         status = le16_to_cpu(txdesc.status);
2347
2348         /* We produce a TXDROP event only for retry or lifetime
2349          * exceeded, because that's the only status that really mean
2350          * that this particular node went away.
2351          * Other errors means that *we* screwed up. - Jean II */
2352         if (status & (HFA384X_TX_STATUS_RETRYERR | HFA384X_TX_STATUS_AGEDERR))
2353         {
2354                 union iwreq_data wrqu;
2355
2356                 /* Copy 802.11 dest address. */
2357                 memcpy(wrqu.addr.sa_data, txdesc.addr1, ETH_ALEN);
2358                 wrqu.addr.sa_family = ARPHRD_ETHER;
2359                 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
2360         } else
2361                 show_dump = 1;
2362
2363         if (local->iw_mode == IW_MODE_MASTER ||
2364             local->iw_mode == IW_MODE_REPEAT ||
2365             local->wds_type & HOSTAP_WDS_AP_CLIENT) {
2366                 struct sk_buff *skb;
2367                 skb = dev_alloc_skb(sizeof(txdesc));
2368                 if (skb) {
2369                         memcpy(skb_put(skb, sizeof(txdesc)), &txdesc,
2370                                sizeof(txdesc));
2371                         skb_queue_tail(&local->sta_tx_exc_list, skb);
2372                         tasklet_schedule(&local->sta_tx_exc_tasklet);
2373                 }
2374         }
2375
2376         if (txdesc.sw_support)
2377                 hostap_tx_callback(local, &txdesc, 0, payload);
2378         kfree(payload);
2379
2380         if (!show_dump)
2381                 return;
2382
2383         PDEBUG(DEBUG_EXTRA, "%s: TXEXC - status=0x%04x (%s%s%s%s)"
2384                " tx_control=%04x\n",
2385                dev->name, status,
2386                status & HFA384X_TX_STATUS_RETRYERR ? "[RetryErr]" : "",
2387                status & HFA384X_TX_STATUS_AGEDERR ? "[AgedErr]" : "",
2388                status & HFA384X_TX_STATUS_DISCON ? "[Discon]" : "",
2389                status & HFA384X_TX_STATUS_FORMERR ? "[FormErr]" : "",
2390                le16_to_cpu(txdesc.tx_control));
2391
2392         fc = le16_to_cpu(txdesc.frame_control);
2393         PDEBUG(DEBUG_EXTRA, "   retry_count=%d tx_rate=%d fc=0x%04x "
2394                "(%s%s%s::%d%s%s)\n",
2395                txdesc.retry_count, txdesc.tx_rate, fc,
2396                WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_MGMT ? "Mgmt" : "",
2397                WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_CTL ? "Ctrl" : "",
2398                WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA ? "Data" : "",
2399                WLAN_FC_GET_STYPE(fc) >> 4,
2400                fc & IEEE80211_FCTL_TODS ? " ToDS" : "",
2401                fc & IEEE80211_FCTL_FROMDS ? " FromDS" : "");
2402         PDEBUG(DEBUG_EXTRA, "   A1=" MACSTR " A2=" MACSTR " A3="
2403                MACSTR " A4=" MACSTR "\n",
2404                MAC2STR(txdesc.addr1), MAC2STR(txdesc.addr2),
2405                MAC2STR(txdesc.addr3), MAC2STR(txdesc.addr4));
2406 }
2407
2408
2409 /* Called only as a tasklet (software IRQ) */
2410 static void hostap_info_tasklet(unsigned long data)
2411 {
2412         local_info_t *local = (local_info_t *) data;
2413         struct sk_buff *skb;
2414
2415         while ((skb = skb_dequeue(&local->info_list)) != NULL) {
2416                 hostap_info_process(local, skb);
2417                 dev_kfree_skb(skb);
2418         }
2419 }
2420
2421
2422 /* Called only as a tasklet (software IRQ) */
2423 static void prism2_info(local_info_t *local)
2424 {
2425         struct net_device *dev = local->dev;
2426         u16 fid;
2427         int res, left;
2428         struct hfa384x_info_frame info;
2429         struct sk_buff *skb;
2430
2431         fid = HFA384X_INW(HFA384X_INFOFID_OFF);
2432
2433         spin_lock(&local->baplock);
2434         res = hfa384x_setup_bap(dev, BAP0, fid, 0);
2435         if (!res)
2436                 res = hfa384x_from_bap(dev, BAP0, &info, sizeof(info));
2437         if (res) {
2438                 spin_unlock(&local->baplock);
2439                 printk(KERN_DEBUG "Could not get info frame (fid=0x%04x)\n",
2440                        fid);
2441                 if (res == -ETIMEDOUT) {
2442                         schedule_work(&local->reset_queue);
2443                 }
2444                 goto out;
2445         }
2446
2447         le16_to_cpus(&info.len);
2448         le16_to_cpus(&info.type);
2449         left = (info.len - 1) * 2;
2450
2451         if (info.len & 0x8000 || info.len == 0 || left > 2060) {
2452                 /* data register seems to give 0x8000 in some error cases even
2453                  * though busy bit is not set in offset register;
2454                  * in addition, length must be at least 1 due to type field */
2455                 spin_unlock(&local->baplock);
2456                 printk(KERN_DEBUG "%s: Received info frame with invalid "
2457                        "length 0x%04x (type 0x%04x)\n", dev->name, info.len,
2458                        info.type);
2459                 goto out;
2460         }
2461
2462         skb = dev_alloc_skb(sizeof(info) + left);
2463         if (skb == NULL) {
2464                 spin_unlock(&local->baplock);
2465                 printk(KERN_DEBUG "%s: Could not allocate skb for info "
2466                        "frame\n", dev->name);
2467                 goto out;
2468         }
2469
2470         memcpy(skb_put(skb, sizeof(info)), &info, sizeof(info));
2471         if (left > 0 && hfa384x_from_bap(dev, BAP0, skb_put(skb, left), left))
2472         {
2473                 spin_unlock(&local->baplock);
2474                 printk(KERN_WARNING "%s: Info frame read failed (fid=0x%04x, "
2475                        "len=0x%04x, type=0x%04x\n",
2476                        dev->name, fid, info.len, info.type);
2477                 dev_kfree_skb(skb);
2478                 goto out;
2479         }
2480         spin_unlock(&local->baplock);
2481
2482         skb_queue_tail(&local->info_list, skb);
2483         tasklet_schedule(&local->info_tasklet);
2484
2485  out:
2486         HFA384X_OUTW(HFA384X_EV_INFO, HFA384X_EVACK_OFF);
2487 }
2488
2489
2490 /* Called only as a tasklet (software IRQ) */
2491 static void hostap_bap_tasklet(unsigned long data)
2492 {
2493         local_info_t *local = (local_info_t *) data;
2494         struct net_device *dev = local->dev;
2495         u16 ev;
2496         int frames = 30;
2497
2498         if (local->func->card_present && !local->func->card_present(local))
2499                 return;
2500
2501         set_bit(HOSTAP_BITS_BAP_TASKLET, &local->bits);
2502
2503         /* Process all pending BAP events without generating new interrupts
2504          * for them */
2505         while (frames-- > 0) {
2506                 ev = HFA384X_INW(HFA384X_EVSTAT_OFF);
2507                 if (ev == 0xffff || !(ev & HFA384X_BAP0_EVENTS))
2508                         break;
2509                 if (ev & HFA384X_EV_RX)
2510                         prism2_rx(local);
2511                 if (ev & HFA384X_EV_INFO)
2512                         prism2_info(local);
2513                 if (ev & HFA384X_EV_TX)
2514                         prism2_tx_ev(local);
2515                 if (ev & HFA384X_EV_TXEXC)
2516                         prism2_txexc(local);
2517         }
2518
2519         set_bit(HOSTAP_BITS_BAP_TASKLET2, &local->bits);
2520         clear_bit(HOSTAP_BITS_BAP_TASKLET, &local->bits);
2521
2522         /* Enable interrupts for new BAP events */
2523         hfa384x_events_all(dev);
2524         clear_bit(HOSTAP_BITS_BAP_TASKLET2, &local->bits);
2525 }
2526
2527
2528 /* Called only from hardware IRQ */
2529 static void prism2_infdrop(struct net_device *dev)
2530 {
2531         static unsigned long last_inquire = 0;
2532
2533         PDEBUG(DEBUG_EXTRA, "%s: INFDROP event\n", dev->name);
2534
2535         /* some firmware versions seem to get stuck with
2536          * full CommTallies in high traffic load cases; every
2537          * packet will then cause INFDROP event and CommTallies
2538          * info frame will not be sent automatically. Try to
2539          * get out of this state by inquiring CommTallies. */
2540         if (!last_inquire || time_after(jiffies, last_inquire + HZ)) {
2541                 hfa384x_cmd_callback(dev, HFA384X_CMDCODE_INQUIRE,
2542                                      HFA384X_INFO_COMMTALLIES, NULL, 0);
2543                 last_inquire = jiffies;
2544         }
2545 }
2546
2547
2548 /* Called only from hardware IRQ */
2549 static void prism2_ev_tick(struct net_device *dev)
2550 {
2551         struct hostap_interface *iface;
2552         local_info_t *local;
2553         u16 evstat, inten;
2554         static int prev_stuck = 0;
2555
2556         iface = netdev_priv(dev);
2557         local = iface->local;
2558
2559         if (time_after(jiffies, local->last_tick_timer + 5 * HZ) &&
2560             local->last_tick_timer) {
2561                 evstat = HFA384X_INW(HFA384X_EVSTAT_OFF);
2562                 inten = HFA384X_INW(HFA384X_INTEN_OFF);
2563                 if (!prev_stuck) {
2564                         printk(KERN_INFO "%s: SW TICK stuck? "
2565                                "bits=0x%lx EvStat=%04x IntEn=%04x\n",
2566                                dev->name, local->bits, evstat, inten);
2567                 }
2568                 local->sw_tick_stuck++;
2569                 if ((evstat & HFA384X_BAP0_EVENTS) &&
2570                     (inten & HFA384X_BAP0_EVENTS)) {
2571                         printk(KERN_INFO "%s: trying to recover from IRQ "
2572                                "hang\n", dev->name);
2573                         hfa384x_events_no_bap0(dev);
2574                 }
2575                 prev_stuck = 1;
2576         } else
2577                 prev_stuck = 0;
2578 }
2579
2580
2581 /* Called only from hardware IRQ */
2582 static void prism2_check_magic(local_info_t *local)
2583 {
2584         /* at least PCI Prism2.5 with bus mastering seems to sometimes
2585          * return 0x0000 in SWSUPPORT0 for unknown reason, but re-reading the
2586          * register once or twice seems to get the correct value.. PCI cards
2587          * cannot anyway be removed during normal operation, so there is not
2588          * really any need for this verification with them. */
2589
2590 #ifndef PRISM2_PCI
2591 #ifndef final_version
2592         static unsigned long last_magic_err = 0;
2593         struct net_device *dev = local->dev;
2594
2595         if (HFA384X_INW(HFA384X_SWSUPPORT0_OFF) != HFA384X_MAGIC) {
2596                 if (!local->hw_ready)
2597                         return;
2598                 HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
2599                 if (time_after(jiffies, last_magic_err + 10 * HZ)) {
2600                         printk("%s: Interrupt, but SWSUPPORT0 does not match: "
2601                                "%04X != %04X - card removed?\n", dev->name,
2602                                HFA384X_INW(HFA384X_SWSUPPORT0_OFF),
2603                                HFA384X_MAGIC);
2604                         last_magic_err = jiffies;
2605                 } else if (net_ratelimit()) {
2606                         printk(KERN_DEBUG "%s: interrupt - SWSUPPORT0=%04x "
2607                                "MAGIC=%04x\n", dev->name,
2608                                HFA384X_INW(HFA384X_SWSUPPORT0_OFF),
2609                                HFA384X_MAGIC);
2610                 }
2611                 if (HFA384X_INW(HFA384X_SWSUPPORT0_OFF) != 0xffff)
2612                         schedule_work(&local->reset_queue);
2613                 return;
2614         }
2615 #endif /* final_version */
2616 #endif /* !PRISM2_PCI */
2617 }
2618
2619
2620 /* Called only from hardware IRQ */
2621 static irqreturn_t prism2_interrupt(int irq, void *dev_id)
2622 {
2623         struct net_device *dev = (struct net_device *) dev_id;
2624         struct hostap_interface *iface;
2625         local_info_t *local;
2626         int events = 0;
2627         u16 ev;
2628
2629         iface = netdev_priv(dev);
2630         local = iface->local;
2631
2632         prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_INTERRUPT, 0, 0);
2633
2634         if (local->func->card_present && !local->func->card_present(local)) {
2635                 if (net_ratelimit()) {
2636                         printk(KERN_DEBUG "%s: Interrupt, but dev not OK\n",
2637                                dev->name);
2638                 }
2639                 return IRQ_HANDLED;
2640         }
2641
2642         prism2_check_magic(local);
2643
2644         for (;;) {
2645                 ev = HFA384X_INW(HFA384X_EVSTAT_OFF);
2646                 if (ev == 0xffff) {
2647                         if (local->shutdown)
2648                                 return IRQ_HANDLED;
2649                         HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
2650                         printk(KERN_DEBUG "%s: prism2_interrupt: ev=0xffff\n",
2651                                dev->name);
2652                         return IRQ_HANDLED;
2653                 }
2654
2655                 ev &= HFA384X_INW(HFA384X_INTEN_OFF);
2656                 if (ev == 0)
2657                         break;
2658
2659                 if (ev & HFA384X_EV_CMD) {
2660                         prism2_cmd_ev(dev);
2661                 }
2662
2663                 /* Above events are needed even before hw is ready, but other
2664                  * events should be skipped during initialization. This may
2665                  * change for AllocEv if allocate_fid is implemented without
2666                  * busy waiting. */
2667                 if (!local->hw_ready || local->hw_resetting ||
2668                     !local->dev_enabled) {
2669                         ev = HFA384X_INW(HFA384X_EVSTAT_OFF);
2670                         if (ev & HFA384X_EV_CMD)
2671                                 goto next_event;
2672                         if ((ev & HFA384X_EVENT_MASK) == 0)
2673                                 return IRQ_HANDLED;
2674                         if (local->dev_enabled && (ev & ~HFA384X_EV_TICK) &&
2675                             net_ratelimit()) {
2676                                 printk(KERN_DEBUG "%s: prism2_interrupt: hw "
2677                                        "not ready; skipping events 0x%04x "
2678                                        "(IntEn=0x%04x)%s%s%s\n",
2679                                        dev->name, ev,
2680                                        HFA384X_INW(HFA384X_INTEN_OFF),
2681                                        !local->hw_ready ? " (!hw_ready)" : "",
2682                                        local->hw_resetting ?
2683                                        " (hw_resetting)" : "",
2684                                        !local->dev_enabled ?
2685                                        " (!dev_enabled)" : "");
2686                         }
2687                         HFA384X_OUTW(ev, HFA384X_EVACK_OFF);
2688                         return IRQ_HANDLED;
2689                 }
2690
2691                 if (ev & HFA384X_EV_TICK) {
2692                         prism2_ev_tick(dev);
2693                         HFA384X_OUTW(HFA384X_EV_TICK, HFA384X_EVACK_OFF);
2694                 }
2695
2696                 if (ev & HFA384X_EV_ALLOC) {
2697                         prism2_alloc_ev(dev);
2698                         HFA384X_OUTW(HFA384X_EV_ALLOC, HFA384X_EVACK_OFF);
2699                 }
2700
2701                 /* Reading data from the card is quite time consuming, so do it
2702                  * in tasklets. TX, TXEXC, RX, and INFO events will be ACKed
2703                  * and unmasked after needed data has been read completely. */
2704                 if (ev & HFA384X_BAP0_EVENTS) {
2705                         hfa384x_events_no_bap0(dev);
2706                         tasklet_schedule(&local->bap_tasklet);
2707                 }
2708
2709 #ifndef final_version
2710                 if (ev & HFA384X_EV_WTERR) {
2711                         PDEBUG(DEBUG_EXTRA, "%s: WTERR event\n", dev->name);
2712                         HFA384X_OUTW(HFA384X_EV_WTERR, HFA384X_EVACK_OFF);
2713                 }
2714 #endif /* final_version */
2715
2716                 if (ev & HFA384X_EV_INFDROP) {
2717                         prism2_infdrop(dev);
2718                         HFA384X_OUTW(HFA384X_EV_INFDROP, HFA384X_EVACK_OFF);
2719                 }
2720
2721         next_event:
2722                 events++;
2723                 if (events >= PRISM2_MAX_INTERRUPT_EVENTS) {
2724                         PDEBUG(DEBUG_EXTRA, "prism2_interrupt: >%d events "
2725                                "(EvStat=0x%04x)\n",
2726                                PRISM2_MAX_INTERRUPT_EVENTS,
2727                                HFA384X_INW(HFA384X_EVSTAT_OFF));
2728                         break;
2729                 }
2730         }
2731         prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_INTERRUPT, 0, 1);
2732         return IRQ_RETVAL(events);
2733 }
2734
2735
2736 static void prism2_check_sta_fw_version(local_info_t *local)
2737 {
2738         struct hfa384x_comp_ident comp;
2739         int id, variant, major, minor;
2740
2741         if (hfa384x_get_rid(local->dev, HFA384X_RID_STAID,
2742                             &comp, sizeof(comp), 1) < 0)
2743                 return;
2744
2745         local->fw_ap = 0;
2746         id = le16_to_cpu(comp.id);
2747         if (id != HFA384X_COMP_ID_STA) {
2748                 if (id == HFA384X_COMP_ID_FW_AP)
2749                         local->fw_ap = 1;
2750                 return;
2751         }
2752
2753         major = __le16_to_cpu(comp.major);
2754         minor = __le16_to_cpu(comp.minor);
2755         variant = __le16_to_cpu(comp.variant);
2756         local->sta_fw_ver = PRISM2_FW_VER(major, minor, variant);
2757
2758         /* Station firmware versions before 1.4.x seem to have a bug in
2759          * firmware-based WEP encryption when using Host AP mode, so use
2760          * host_encrypt as a default for them. Firmware version 1.4.9 is the
2761          * first one that has been seen to produce correct encryption, but the
2762          * bug might be fixed before that (although, at least 1.4.2 is broken).
2763          */
2764         local->fw_encrypt_ok = local->sta_fw_ver >= PRISM2_FW_VER(1,4,9);
2765
2766         if (local->iw_mode == IW_MODE_MASTER && !local->host_encrypt &&
2767             !local->fw_encrypt_ok) {
2768                 printk(KERN_DEBUG "%s: defaulting to host-based encryption as "
2769                        "a workaround for firmware bug in Host AP mode WEP\n",
2770                        local->dev->name);
2771                 local->host_encrypt = 1;
2772         }
2773
2774         /* IEEE 802.11 standard compliant WDS frames (4 addresses) were broken
2775          * in station firmware versions before 1.5.x. With these versions, the
2776          * driver uses a workaround with bogus frame format (4th address after
2777          * the payload). This is not compatible with other AP devices. Since
2778          * the firmware bug is fixed in the latest station firmware versions,
2779          * automatically enable standard compliant mode for cards using station
2780          * firmware version 1.5.0 or newer. */
2781         if (local->sta_fw_ver >= PRISM2_FW_VER(1,5,0))
2782                 local->wds_type |= HOSTAP_WDS_STANDARD_FRAME;
2783         else {
2784                 printk(KERN_DEBUG "%s: defaulting to bogus WDS frame as a "
2785                        "workaround for firmware bug in Host AP mode WDS\n",
2786                        local->dev->name);
2787         }
2788
2789         hostap_check_sta_fw_version(local->ap, local->sta_fw_ver);
2790 }
2791
2792
2793 static void prism2_crypt_deinit_entries(local_info_t *local, int force)
2794 {
2795         struct list_head *ptr, *n;
2796         struct ieee80211_crypt_data *entry;
2797
2798         for (ptr = local->crypt_deinit_list.next, n = ptr->next;
2799              ptr != &local->crypt_deinit_list; ptr = n, n = ptr->next) {
2800                 entry = list_entry(ptr, struct ieee80211_crypt_data, list);
2801
2802                 if (atomic_read(&entry->refcnt) != 0 && !force)
2803                         continue;
2804
2805                 list_del(ptr);
2806
2807                 if (entry->ops)
2808                         entry->ops->deinit(entry->priv);
2809                 kfree(entry);
2810         }
2811 }
2812
2813
2814 static void prism2_crypt_deinit_handler(unsigned long data)
2815 {
2816         local_info_t *local = (local_info_t *) data;
2817         unsigned long flags;
2818
2819         spin_lock_irqsave(&local->lock, flags);
2820         prism2_crypt_deinit_entries(local, 0);
2821         if (!list_empty(&local->crypt_deinit_list)) {
2822                 printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
2823                        "deletion list\n", local->dev->name);
2824                 local->crypt_deinit_timer.expires = jiffies + HZ;
2825                 add_timer(&local->crypt_deinit_timer);
2826         }
2827         spin_unlock_irqrestore(&local->lock, flags);
2828
2829 }
2830
2831
2832 static void hostap_passive_scan(unsigned long data)
2833 {
2834         local_info_t *local = (local_info_t *) data;
2835         struct net_device *dev = local->dev;
2836         u16 channel;
2837
2838         if (local->passive_scan_interval <= 0)
2839                 return;
2840
2841         if (local->passive_scan_state == PASSIVE_SCAN_LISTEN) {
2842                 int max_tries = 16;
2843
2844                 /* Even though host system does not really know when the WLAN
2845                  * MAC is sending frames, try to avoid changing channels for
2846                  * passive scanning when a host-generated frame is being
2847                  * transmitted */
2848                 if (test_bit(HOSTAP_BITS_TRANSMIT, &local->bits)) {
2849                         printk(KERN_DEBUG "%s: passive scan detected pending "
2850                                "TX - delaying\n", dev->name);
2851                         local->passive_scan_timer.expires = jiffies + HZ / 10;
2852                         add_timer(&local->passive_scan_timer);
2853                         return;
2854                 }
2855
2856                 do {
2857                         local->passive_scan_channel++;
2858                         if (local->passive_scan_channel > 14)
2859                                 local->passive_scan_channel = 1;
2860                         max_tries--;
2861                 } while (!(local->channel_mask &
2862                            (1 << (local->passive_scan_channel - 1))) &&
2863                          max_tries > 0);
2864
2865                 if (max_tries == 0) {
2866                         printk(KERN_INFO "%s: no allowed passive scan channels"
2867                                " found\n", dev->name);
2868                         return;
2869                 }
2870
2871                 printk(KERN_DEBUG "%s: passive scan channel %d\n",
2872                        dev->name, local->passive_scan_channel);
2873                 channel = local->passive_scan_channel;
2874                 local->passive_scan_state = PASSIVE_SCAN_WAIT;
2875                 local->passive_scan_timer.expires = jiffies + HZ / 10;
2876         } else {
2877                 channel = local->channel;
2878                 local->passive_scan_state = PASSIVE_SCAN_LISTEN;
2879                 local->passive_scan_timer.expires = jiffies +
2880                         local->passive_scan_interval * HZ;
2881         }
2882
2883         if (hfa384x_cmd_callback(dev, HFA384X_CMDCODE_TEST |
2884                                  (HFA384X_TEST_CHANGE_CHANNEL << 8),
2885                                  channel, NULL, 0))
2886                 printk(KERN_ERR "%s: passive scan channel set %d "
2887                        "failed\n", dev->name, channel);
2888
2889         add_timer(&local->passive_scan_timer);
2890 }
2891
2892
2893 /* Called only as a scheduled task when communications quality values should
2894  * be updated. */
2895 static void handle_comms_qual_update(struct work_struct *work)
2896 {
2897         local_info_t *local =
2898                 container_of(work, local_info_t, comms_qual_update);
2899         prism2_update_comms_qual(local->dev);
2900 }
2901
2902
2903 /* Software watchdog - called as a timer. Hardware interrupt (Tick event) is
2904  * used to monitor that local->last_tick_timer is being updated. If not,
2905  * interrupt busy-loop is assumed and driver tries to recover by masking out
2906  * some events. */
2907 static void hostap_tick_timer(unsigned long data)
2908 {
2909         static unsigned long last_inquire = 0;
2910         local_info_t *local = (local_info_t *) data;
2911         local->last_tick_timer = jiffies;
2912
2913         /* Inquire CommTallies every 10 seconds to keep the statistics updated
2914          * more often during low load and when using 32-bit tallies. */
2915         if ((!last_inquire || time_after(jiffies, last_inquire + 10 * HZ)) &&
2916             !local->hw_downloading && local->hw_ready &&
2917             !local->hw_resetting && local->dev_enabled) {
2918                 hfa384x_cmd_callback(local->dev, HFA384X_CMDCODE_INQUIRE,
2919                                      HFA384X_INFO_COMMTALLIES, NULL, 0);
2920                 last_inquire = jiffies;
2921         }
2922
2923         if ((local->last_comms_qual_update == 0 ||
2924              time_after(jiffies, local->last_comms_qual_update + 10 * HZ)) &&
2925             (local->iw_mode == IW_MODE_INFRA ||
2926              local->iw_mode == IW_MODE_ADHOC)) {
2927                 schedule_work(&local->comms_qual_update);
2928         }
2929
2930         local->tick_timer.expires = jiffies + 2 * HZ;
2931         add_timer(&local->tick_timer);
2932 }
2933
2934
2935 #ifndef PRISM2_NO_PROCFS_DEBUG
2936 static int prism2_registers_proc_read(char *page, char **start, off_t off,
2937                                       int count, int *eof, void *data)
2938 {
2939         char *p = page;
2940         local_info_t *local = (local_info_t *) data;
2941
2942         if (off != 0) {
2943                 *eof = 1;
2944                 return 0;
2945         }
2946
2947 #define SHOW_REG(n) \
2948 p += sprintf(p, #n "=%04x\n", hfa384x_read_reg(local->dev, HFA384X_##n##_OFF))
2949
2950         SHOW_REG(CMD);
2951         SHOW_REG(PARAM0);
2952         SHOW_REG(PARAM1);
2953         SHOW_REG(PARAM2);
2954         SHOW_REG(STATUS);
2955         SHOW_REG(RESP0);
2956         SHOW_REG(RESP1);
2957         SHOW_REG(RESP2);
2958         SHOW_REG(INFOFID);
2959         SHOW_REG(CONTROL);
2960         SHOW_REG(SELECT0);
2961         SHOW_REG(SELECT1);
2962         SHOW_REG(OFFSET0);
2963         SHOW_REG(OFFSET1);
2964         SHOW_REG(RXFID);
2965         SHOW_REG(ALLOCFID);
2966         SHOW_REG(TXCOMPLFID);
2967         SHOW_REG(SWSUPPORT0);
2968         SHOW_REG(SWSUPPORT1);
2969         SHOW_REG(SWSUPPORT2);
2970         SHOW_REG(EVSTAT);
2971         SHOW_REG(INTEN);
2972         SHOW_REG(EVACK);
2973         /* Do not read data registers, because they change the state of the
2974          * MAC (offset += 2) */
2975         /* SHOW_REG(DATA0); */
2976         /* SHOW_REG(DATA1); */
2977         SHOW_REG(AUXPAGE);
2978         SHOW_REG(AUXOFFSET);
2979         /* SHOW_REG(AUXDATA); */
2980 #ifdef PRISM2_PCI
2981         SHOW_REG(PCICOR);
2982         SHOW_REG(PCIHCR);
2983         SHOW_REG(PCI_M0_ADDRH);
2984         SHOW_REG(PCI_M0_ADDRL);
2985         SHOW_REG(PCI_M0_LEN);
2986         SHOW_REG(PCI_M0_CTL);
2987         SHOW_REG(PCI_STATUS);
2988         SHOW_REG(PCI_M1_ADDRH);
2989         SHOW_REG(PCI_M1_ADDRL);
2990         SHOW_REG(PCI_M1_LEN);
2991         SHOW_REG(PCI_M1_CTL);
2992 #endif /* PRISM2_PCI */
2993
2994         return (p - page);
2995 }
2996 #endif /* PRISM2_NO_PROCFS_DEBUG */
2997
2998
2999 struct set_tim_data {
3000         struct list_head list;
3001         int aid;
3002         int set;
3003 };
3004
3005 static int prism2_set_tim(struct net_device *dev, int aid, int set)
3006 {
3007         struct list_head *ptr;
3008         struct set_tim_data *new_entry;
3009         struct hostap_interface *iface;
3010         local_info_t *local;
3011
3012         iface = netdev_priv(dev);
3013         local = iface->local;
3014
3015         new_entry = kzalloc(sizeof(*new_entry), GFP_ATOMIC);
3016         if (new_entry == NULL) {
3017                 printk(KERN_DEBUG "%s: prism2_set_tim: kmalloc failed\n",
3018                        local->dev->name);
3019                 return -ENOMEM;
3020         }
3021         new_entry->aid = aid;
3022         new_entry->set = set;
3023
3024         spin_lock_bh(&local->set_tim_lock);
3025         list_for_each(ptr, &local->set_tim_list) {
3026                 struct set_tim_data *entry =
3027                         list_entry(ptr, struct set_tim_data, list);
3028                 if (entry->aid == aid) {
3029                         PDEBUG(DEBUG_PS2, "%s: prism2_set_tim: aid=%d "
3030                                "set=%d ==> %d\n",
3031                                local->dev->name, aid, entry->set, set);
3032                         entry->set = set;
3033                         kfree(new_entry);
3034                         new_entry = NULL;
3035                         break;
3036                 }
3037         }
3038         if (new_entry)
3039                 list_add_tail(&new_entry->list, &local->set_tim_list);
3040         spin_unlock_bh(&local->set_tim_lock);
3041
3042         schedule_work(&local->set_tim_queue);
3043
3044         return 0;
3045 }
3046
3047
3048 static void handle_set_tim_queue(struct work_struct *work)
3049 {
3050         local_info_t *local = container_of(work, local_info_t, set_tim_queue);
3051         struct set_tim_data *entry;
3052         u16 val;
3053
3054         for (;;) {
3055                 entry = NULL;
3056                 spin_lock_bh(&local->set_tim_lock);
3057                 if (!list_empty(&local->set_tim_list)) {
3058                         entry = list_entry(local->set_tim_list.next,
3059                                            struct set_tim_data, list);
3060                         list_del(&entry->list);
3061                 }
3062                 spin_unlock_bh(&local->set_tim_lock);
3063                 if (!entry)
3064                         break;
3065
3066                 PDEBUG(DEBUG_PS2, "%s: handle_set_tim_queue: aid=%d set=%d\n",
3067                        local->dev->name, entry->aid, entry->set);
3068
3069                 val = entry->aid;
3070                 if (entry->set)
3071                         val |= 0x8000;
3072                 if (hostap_set_word(local->dev, HFA384X_RID_CNFTIMCTRL, val)) {
3073                         printk(KERN_DEBUG "%s: set_tim failed (aid=%d "
3074                                "set=%d)\n",
3075                                local->dev->name, entry->aid, entry->set);
3076                 }
3077
3078                 kfree(entry);
3079         }
3080 }
3081
3082
3083 static void prism2_clear_set_tim_queue(local_info_t *local)
3084 {
3085         struct list_head *ptr, *n;
3086
3087         list_for_each_safe(ptr, n, &local->set_tim_list) {
3088                 struct set_tim_data *entry;
3089                 entry = list_entry(ptr, struct set_tim_data, list);
3090                 list_del(&entry->list);
3091                 kfree(entry);
3092         }
3093 }
3094
3095
3096 /*
3097  * HostAP uses two layers of net devices, where the inner
3098  * layer gets called all the time from the outer layer.
3099  * This is a natural nesting, which needs a split lock type.
3100  */
3101 static struct lock_class_key hostap_netdev_xmit_lock_key;
3102
3103
3104 static struct net_device *
3105 prism2_init_local_data(struct prism2_helper_functions *funcs, int card_idx,
3106                        struct device *sdev)
3107 {
3108         struct net_device *dev;
3109         struct hostap_interface *iface;
3110         struct local_info *local;
3111         int len, i, ret;
3112
3113         if (funcs == NULL)
3114                 return NULL;
3115
3116         len = strlen(dev_template);
3117         if (len >= IFNAMSIZ || strstr(dev_template, "%d") == NULL) {
3118                 printk(KERN_WARNING "hostap: Invalid dev_template='%s'\n",
3119                        dev_template);
3120                 return NULL;
3121         }
3122
3123         len = sizeof(struct hostap_interface) +
3124                 3 + sizeof(struct local_info) +
3125                 3 + sizeof(struct ap_data);
3126
3127         dev = alloc_etherdev(len);
3128         if (dev == NULL)
3129                 return NULL;
3130
3131         iface = netdev_priv(dev);
3132         local = (struct local_info *) ((((long) (iface + 1)) + 3) & ~3);
3133         local->ap = (struct ap_data *) ((((long) (local + 1)) + 3) & ~3);
3134         local->dev = iface->dev = dev;
3135         iface->local = local;
3136         iface->type = HOSTAP_INTERFACE_MASTER;
3137         INIT_LIST_HEAD(&local->hostap_interfaces);
3138
3139         local->hw_module = THIS_MODULE;
3140
3141 #ifdef PRISM2_IO_DEBUG
3142         local->io_debug_enabled = 1;
3143 #endif /* PRISM2_IO_DEBUG */
3144
3145         local->func = funcs;
3146         local->func->cmd = hfa384x_cmd;
3147         local->func->read_regs = hfa384x_read_regs;
3148         local->func->get_rid = hfa384x_get_rid;
3149         local->func->set_rid = hfa384x_set_rid;
3150         local->func->hw_enable = prism2_hw_enable;
3151         local->func->hw_config = prism2_hw_config;
3152         local->func->hw_reset = prism2_hw_reset;
3153         local->func->hw_shutdown = prism2_hw_shutdown;
3154         local->func->reset_port = prism2_reset_port;
3155         local->func->schedule_reset = prism2_schedule_reset;
3156 #ifdef PRISM2_DOWNLOAD_SUPPORT
3157         local->func->read_aux = prism2_download_aux_dump;
3158         local->func->download = prism2_download;
3159 #endif /* PRISM2_DOWNLOAD_SUPPORT */
3160         local->func->tx = prism2_tx_80211;
3161         local->func->set_tim = prism2_set_tim;
3162         local->func->need_tx_headroom = 0; /* no need to add txdesc in
3163                                             * skb->data (FIX: maybe for DMA bus
3164                                             * mastering? */
3165
3166         local->mtu = mtu;
3167
3168         rwlock_init(&local->iface_lock);
3169         spin_lock_init(&local->txfidlock);
3170         spin_lock_init(&local->cmdlock);
3171         spin_lock_init(&local->baplock);
3172         spin_lock_init(&local->lock);
3173         init_MUTEX(&local->rid_bap_sem);
3174
3175         if (card_idx < 0 || card_idx >= MAX_PARM_DEVICES)
3176                 card_idx = 0;
3177         local->card_idx = card_idx;
3178
3179         len = strlen(essid);
3180         memcpy(local->essid, essid,
3181                len > MAX_SSID_LEN ? MAX_SSID_LEN : len);
3182         local->essid[MAX_SSID_LEN] = '\0';
3183         i = GET_INT_PARM(iw_mode, card_idx);
3184         if ((i >= IW_MODE_ADHOC && i <= IW_MODE_REPEAT) ||
3185             i == IW_MODE_MONITOR) {
3186                 local->iw_mode = i;
3187         } else {
3188                 printk(KERN_WARNING "prism2: Unknown iw_mode %d; using "
3189                        "IW_MODE_MASTER\n", i);
3190                 local->iw_mode = IW_MODE_MASTER;
3191         }
3192         local->channel = GET_INT_PARM(channel, card_idx);
3193         local->beacon_int = GET_INT_PARM(beacon_int, card_idx);
3194         local->dtim_period = GET_INT_PARM(dtim_period, card_idx);
3195         local->wds_max_connections = 16;
3196         local->tx_control = HFA384X_TX_CTRL_FLAGS;
3197         local->manual_retry_count = -1;
3198         local->rts_threshold = 2347;
3199         local->fragm_threshold = 2346;
3200         local->rssi_to_dBm = 100; /* default; to be overriden by
3201                                    * cnfDbmAdjust, if available */
3202         local->auth_algs = PRISM2_AUTH_OPEN | PRISM2_AUTH_SHARED_KEY;
3203         local->sram_type = -1;
3204         local->scan_channel_mask = 0xffff;
3205
3206         /* Initialize task queue structures */
3207         INIT_WORK(&local->reset_queue, handle_reset_queue);
3208         INIT_WORK(&local->set_multicast_list_queue,
3209                   hostap_set_multicast_list_queue);
3210
3211         INIT_WORK(&local->set_tim_queue, handle_set_tim_queue);
3212         INIT_LIST_HEAD(&local->set_tim_list);
3213         spin_lock_init(&local->set_tim_lock);
3214
3215         INIT_WORK(&local->comms_qual_update, handle_comms_qual_update);
3216
3217         /* Initialize tasklets for handling hardware IRQ related operations
3218          * outside hw IRQ handler */
3219 #define HOSTAP_TASKLET_INIT(q, f, d) \
3220 do { memset((q), 0, sizeof(*(q))); (q)->func = (f); (q)->data = (d); } \
3221 while (0)
3222         HOSTAP_TASKLET_INIT(&local->bap_tasklet, hostap_bap_tasklet,
3223                             (unsigned long) local);
3224
3225         HOSTAP_TASKLET_INIT(&local->info_tasklet, hostap_info_tasklet,
3226                             (unsigned long) local);
3227         hostap_info_init(local);
3228
3229         HOSTAP_TASKLET_INIT(&local->rx_tasklet,
3230                             hostap_rx_tasklet, (unsigned long) local);
3231         skb_queue_head_init(&local->rx_list);
3232
3233         HOSTAP_TASKLET_INIT(&local->sta_tx_exc_tasklet,
3234                             hostap_sta_tx_exc_tasklet, (unsigned long) local);
3235         skb_queue_head_init(&local->sta_tx_exc_list);
3236
3237         INIT_LIST_HEAD(&local->cmd_queue);
3238         init_waitqueue_head(&local->hostscan_wq);
3239         INIT_LIST_HEAD(&local->crypt_deinit_list);
3240         init_timer(&local->crypt_deinit_timer);
3241         local->crypt_deinit_timer.data = (unsigned long) local;
3242         local->crypt_deinit_timer.function = prism2_crypt_deinit_handler;
3243
3244         init_timer(&local->passive_scan_timer);
3245         local->passive_scan_timer.data = (unsigned long) local;
3246         local->passive_scan_timer.function = hostap_passive_scan;
3247
3248         init_timer(&local->tick_timer);
3249         local->tick_timer.data = (unsigned long) local;
3250         local->tick_timer.function = hostap_tick_timer;
3251         local->tick_timer.expires = jiffies + 2 * HZ;
3252         add_timer(&local->tick_timer);
3253
3254         INIT_LIST_HEAD(&local->bss_list);
3255
3256         hostap_setup_dev(dev, local, 1);
3257         local->saved_eth_header_parse = dev->hard_header_parse;
3258
3259         dev->hard_start_xmit = hostap_master_start_xmit;
3260         dev->type = ARPHRD_IEEE80211;
3261         dev->hard_header_parse = hostap_80211_header_parse;
3262
3263         rtnl_lock();
3264         ret = dev_alloc_name(dev, "wifi%d");
3265         SET_NETDEV_DEV(dev, sdev);
3266         if (ret >= 0)
3267                 ret = register_netdevice(dev);
3268
3269         lockdep_set_class(&dev->_xmit_lock, &hostap_netdev_xmit_lock_key);
3270         rtnl_unlock();
3271         if (ret < 0) {
3272                 printk(KERN_WARNING "%s: register netdevice failed!\n",
3273                        dev_info);
3274                 goto fail;
3275         }
3276         printk(KERN_INFO "%s: Registered netdevice %s\n", dev_info, dev->name);
3277
3278 #ifndef PRISM2_NO_PROCFS_DEBUG
3279         create_proc_read_entry("registers", 0, local->proc,
3280                                prism2_registers_proc_read, local);
3281 #endif /* PRISM2_NO_PROCFS_DEBUG */
3282
3283         hostap_init_data(local);
3284         return dev;
3285
3286  fail:
3287         free_netdev(dev);
3288         return NULL;
3289 }
3290
3291
3292 static int hostap_hw_ready(struct net_device *dev)
3293 {
3294         struct hostap_interface *iface;
3295         struct local_info *local;
3296
3297         iface = netdev_priv(dev);
3298         local = iface->local;
3299         local->ddev = hostap_add_interface(local, HOSTAP_INTERFACE_MAIN, 0,
3300                                            "", dev_template);
3301
3302         if (local->ddev) {
3303                 if (local->iw_mode == IW_MODE_INFRA ||
3304                     local->iw_mode == IW_MODE_ADHOC) {
3305                         netif_carrier_off(local->dev);
3306                         netif_carrier_off(local->ddev);
3307                 }
3308                 hostap_init_proc(local);
3309                 hostap_init_ap_proc(local);
3310                 return 0;
3311         }
3312
3313         return -1;
3314 }
3315
3316
3317 static void prism2_free_local_data(struct net_device *dev)
3318 {
3319         struct hostap_tx_callback_info *tx_cb, *tx_cb_prev;
3320         int i;
3321         struct hostap_interface *iface;
3322         struct local_info *local;
3323         struct list_head *ptr, *n;
3324
3325         if (dev == NULL)
3326                 return;
3327
3328         iface = netdev_priv(dev);
3329         local = iface->local;
3330
3331         /* Unregister all netdevs before freeing local data. */
3332         list_for_each_safe(ptr, n, &local->hostap_interfaces) {
3333                 iface = list_entry(ptr, struct hostap_interface, list);
3334                 if (iface->type == HOSTAP_INTERFACE_MASTER) {
3335                         /* special handling for this interface below */
3336                         continue;
3337                 }
3338                 hostap_remove_interface(iface->dev, 0, 1);
3339         }
3340
3341         unregister_netdev(local->dev);
3342
3343         flush_scheduled_work();
3344
3345         if (timer_pending(&local->crypt_deinit_timer))
3346                 del_timer(&local->crypt_deinit_timer);
3347         prism2_crypt_deinit_entries(local, 1);
3348
3349         if (timer_pending(&local->passive_scan_timer))
3350                 del_timer(&local->passive_scan_timer);
3351
3352         if (timer_pending(&local->tick_timer))
3353                 del_timer(&local->tick_timer);
3354
3355         prism2_clear_cmd_queue(local);
3356
3357         skb_queue_purge(&local->info_list);
3358         skb_queue_purge(&local->rx_list);
3359         skb_queue_purge(&local->sta_tx_exc_list);
3360
3361         if (local->dev_enabled)
3362                 prism2_callback(local, PRISM2_CALLBACK_DISABLE);
3363
3364         for (i = 0; i < WEP_KEYS; i++) {
3365                 struct ieee80211_crypt_data *crypt = local->crypt[i];
3366                 if (crypt) {
3367                         if (crypt->ops)
3368                                 crypt->ops->deinit(crypt->priv);
3369                         kfree(crypt);
3370                         local->crypt[i] = NULL;
3371                 }
3372         }
3373
3374         if (local->ap != NULL)
3375                 hostap_free_data(local->ap);
3376
3377 #ifndef PRISM2_NO_PROCFS_DEBUG
3378         if (local->proc != NULL)
3379                 remove_proc_entry("registers", local->proc);
3380 #endif /* PRISM2_NO_PROCFS_DEBUG */
3381         hostap_remove_proc(local);
3382
3383         tx_cb = local->tx_callback;
3384         while (tx_cb != NULL) {
3385                 tx_cb_prev = tx_cb;
3386                 tx_cb = tx_cb->next;
3387                 kfree(tx_cb_prev);
3388         }
3389
3390         hostap_set_hostapd(local, 0, 0);
3391         hostap_set_hostapd_sta(local, 0, 0);
3392
3393         for (i = 0; i < PRISM2_FRAG_CACHE_LEN; i++) {
3394                 if (local->frag_cache[i].skb != NULL)
3395                         dev_kfree_skb(local->frag_cache[i].skb);
3396         }
3397
3398 #ifdef PRISM2_DOWNLOAD_SUPPORT
3399         prism2_download_free_data(local->dl_pri);
3400         prism2_download_free_data(local->dl_sec);
3401 #endif /* PRISM2_DOWNLOAD_SUPPORT */
3402
3403         prism2_clear_set_tim_queue(local);
3404
3405         list_for_each_safe(ptr, n, &local->bss_list) {
3406                 struct hostap_bss_info *bss =
3407                         list_entry(ptr, struct hostap_bss_info, list);
3408                 kfree(bss);
3409         }
3410
3411         kfree(local->pda);
3412         kfree(local->last_scan_results);
3413         kfree(local->generic_elem);
3414
3415         free_netdev(local->dev);
3416 }
3417
3418
3419 #ifndef PRISM2_PLX
3420 static void prism2_suspend(struct net_device *dev)
3421 {
3422         struct hostap_interface *iface;
3423         struct local_info *local;
3424         union iwreq_data wrqu;
3425
3426         iface = dev->priv;
3427         local = iface->local;
3428
3429         /* Send disconnect event, e.g., to trigger reassociation after resume
3430          * if wpa_supplicant is used. */
3431         memset(&wrqu, 0, sizeof(wrqu));
3432         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
3433         wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
3434
3435         /* Disable hardware and firmware */
3436         prism2_hw_shutdown(dev, 0);
3437 }
3438 #endif /* PRISM2_PLX */
3439
3440
3441 /* These might at some point be compiled separately and used as separate
3442  * kernel modules or linked into one */
3443 #ifdef PRISM2_DOWNLOAD_SUPPORT
3444 #include "hostap_download.c"
3445 #endif /* PRISM2_DOWNLOAD_SUPPORT */
3446
3447 #ifdef PRISM2_CALLBACK
3448 /* External hostap_callback.c file can be used to, e.g., blink activity led.
3449  * This can use platform specific code and must define prism2_callback()
3450  * function (if PRISM2_CALLBACK is not defined, these function calls are not
3451  * used. */
3452 #include "hostap_callback.c"
3453 #endif /* PRISM2_CALLBACK */