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