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