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