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