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