1 /* main.c - (formerly known as dldwd_cs.c, orinoco_cs.c and orinoco.c)
2  *
3  * A driver for Hermes or Prism 2 chipset based PCMCIA wireless
4  * adaptors, with Lucent/Agere, Intersil or Symbol firmware.
5  *
6  * Current maintainers (as of 29 September 2003) are:
7  *	Pavel Roskin <proski AT gnu.org>
8  * and	David Gibson <hermes AT gibson.dropbear.id.au>
9  *
10  * (C) Copyright David Gibson, IBM Corporation 2001-2003.
11  * Copyright (C) 2000 David Gibson, Linuxcare Australia.
12  *	With some help from :
13  * Copyright (C) 2001 Jean Tourrilhes, HP Labs
14  * Copyright (C) 2001 Benjamin Herrenschmidt
15  *
16  * Based on dummy_cs.c 1.27 2000/06/12 21:27:25
17  *
18  * Portions based on wvlan_cs.c 1.0.6, Copyright Andreas Neuhaus <andy
19  * AT fasta.fh-dortmund.de>
20  *      http://www.stud.fh-dortmund.de/~andy/wvlan/
21  *
22  * The contents of this file are subject to the Mozilla Public License
23  * Version 1.1 (the "License"); you may not use this file except in
24  * compliance with the License. You may obtain a copy of the License
25  * at http://www.mozilla.org/MPL/
26  *
27  * Software distributed under the License is distributed on an "AS IS"
28  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
29  * the License for the specific language governing rights and
30  * limitations under the License.
31  *
32  * The initial developer of the original code is David A. Hinds
33  * <dahinds AT users.sourceforge.net>.  Portions created by David
34  * A. Hinds are Copyright (C) 1999 David A. Hinds.  All Rights
35  * Reserved.
36  *
37  * Alternatively, the contents of this file may be used under the
38  * terms of the GNU General Public License version 2 (the "GPL"), in
39  * which case the provisions of the GPL are applicable instead of the
40  * above.  If you wish to allow the use of your version of this file
41  * only under the terms of the GPL and not to allow others to use your
42  * version of this file under the MPL, indicate your decision by
43  * deleting the provisions above and replace them with the notice and
44  * other provisions required by the GPL.  If you do not delete the
45  * provisions above, a recipient may use your version of this file
46  * under either the MPL or the GPL.  */
47 
48 /*
49  * TODO
50  *	o Handle de-encapsulation within network layer, provide 802.11
51  *	  headers (patch from Thomas 'Dent' Mirlacher)
52  *	o Fix possible races in SPY handling.
53  *	o Disconnect wireless extensions from fundamental configuration.
54  *	o (maybe) Software WEP support (patch from Stano Meduna).
55  *	o (maybe) Use multiple Tx buffers - driver handling queue
56  *	  rather than firmware.
57  */
58 
59 /* Locking and synchronization:
60  *
61  * The basic principle is that everything is serialized through a
62  * single spinlock, priv->lock.  The lock is used in user, bh and irq
63  * context, so when taken outside hardirq context it should always be
64  * taken with interrupts disabled.  The lock protects both the
65  * hardware and the struct orinoco_private.
66  *
67  * Another flag, priv->hw_unavailable indicates that the hardware is
68  * unavailable for an extended period of time (e.g. suspended, or in
69  * the middle of a hard reset).  This flag is protected by the
70  * spinlock.  All code which touches the hardware should check the
71  * flag after taking the lock, and if it is set, give up on whatever
72  * they are doing and drop the lock again.  The orinoco_lock()
73  * function handles this (it unlocks and returns -EBUSY if
74  * hw_unavailable is non-zero).
75  */
76 
77 #define DRIVER_NAME "orinoco"
78 
79 #include <linux/module.h>
80 #include <linux/kernel.h>
81 #include <linux/slab.h>
82 #include <linux/init.h>
83 #include <linux/delay.h>
84 #include <linux/device.h>
85 #include <linux/netdevice.h>
86 #include <linux/etherdevice.h>
87 #include <linux/suspend.h>
88 #include <linux/if_arp.h>
89 #include <linux/wireless.h>
90 #include <linux/ieee80211.h>
91 #include <net/iw_handler.h>
92 #include <net/cfg80211.h>
93 
94 #include "hermes_rid.h"
95 #include "hermes_dld.h"
96 #include "hw.h"
97 #include "scan.h"
98 #include "mic.h"
99 #include "fw.h"
100 #include "wext.h"
101 #include "cfg.h"
102 #include "main.h"
103 
104 #include "orinoco.h"
105 
106 /********************************************************************/
107 /* Module information                                               */
108 /********************************************************************/
109 
110 MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & "
111 	      "David Gibson <hermes@gibson.dropbear.id.au>");
112 MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based "
113 		   "and similar wireless cards");
114 MODULE_LICENSE("Dual MPL/GPL");
115 
116 /* Level of debugging. Used in the macros in orinoco.h */
117 #ifdef ORINOCO_DEBUG
118 int orinoco_debug = ORINOCO_DEBUG;
119 EXPORT_SYMBOL(orinoco_debug);
120 module_param(orinoco_debug, int, 0644);
121 MODULE_PARM_DESC(orinoco_debug, "Debug level");
122 #endif
123 
124 static bool suppress_linkstatus; /* = 0 */
125 module_param(suppress_linkstatus, bool, 0644);
126 MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
127 
128 static int ignore_disconnect; /* = 0 */
129 module_param(ignore_disconnect, int, 0644);
130 MODULE_PARM_DESC(ignore_disconnect,
131 		 "Don't report lost link to the network layer");
132 
133 int force_monitor; /* = 0 */
134 module_param(force_monitor, int, 0644);
135 MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
136 
137 /********************************************************************/
138 /* Internal constants                                               */
139 /********************************************************************/
140 
141 /* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
142 static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
143 #define ENCAPS_OVERHEAD		(sizeof(encaps_hdr) + 2)
144 
145 #define ORINOCO_MIN_MTU		256
146 #define ORINOCO_MAX_MTU		(IEEE80211_MAX_DATA_LEN - ENCAPS_OVERHEAD)
147 
148 #define MAX_IRQLOOPS_PER_IRQ	10
149 #define MAX_IRQLOOPS_PER_JIFFY	(20000 / HZ)	/* Based on a guestimate of
150 						 * how many events the
151 						 * device could
152 						 * legitimately generate */
153 
154 #define DUMMY_FID		0xFFFF
155 
156 /*#define MAX_MULTICAST(priv)	(priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
157   HERMES_MAX_MULTICAST : 0)*/
158 #define MAX_MULTICAST(priv)	(HERMES_MAX_MULTICAST)
159 
160 #define ORINOCO_INTEN		(HERMES_EV_RX | HERMES_EV_ALLOC \
161 				 | HERMES_EV_TX | HERMES_EV_TXEXC \
162 				 | HERMES_EV_WTERR | HERMES_EV_INFO \
163 				 | HERMES_EV_INFDROP)
164 
165 /********************************************************************/
166 /* Data types                                                       */
167 /********************************************************************/
168 
169 /* Beginning of the Tx descriptor, used in TxExc handling */
170 struct hermes_txexc_data {
171 	struct hermes_tx_descriptor desc;
172 	__le16 frame_ctl;
173 	__le16 duration_id;
174 	u8 addr1[ETH_ALEN];
175 } __packed;
176 
177 /* Rx frame header except compatibility 802.3 header */
178 struct hermes_rx_descriptor {
179 	/* Control */
180 	__le16 status;
181 	__le32 time;
182 	u8 silence;
183 	u8 signal;
184 	u8 rate;
185 	u8 rxflow;
186 	__le32 reserved;
187 
188 	/* 802.11 header */
189 	__le16 frame_ctl;
190 	__le16 duration_id;
191 	u8 addr1[ETH_ALEN];
192 	u8 addr2[ETH_ALEN];
193 	u8 addr3[ETH_ALEN];
194 	__le16 seq_ctl;
195 	u8 addr4[ETH_ALEN];
196 
197 	/* Data length */
198 	__le16 data_len;
199 } __packed;
200 
201 struct orinoco_rx_data {
202 	struct hermes_rx_descriptor *desc;
203 	struct sk_buff *skb;
204 	struct list_head list;
205 };
206 
207 struct orinoco_scan_data {
208 	void *buf;
209 	size_t len;
210 	int type;
211 	struct list_head list;
212 };
213 
214 /********************************************************************/
215 /* Function prototypes                                              */
216 /********************************************************************/
217 
218 static int __orinoco_set_multicast_list(struct net_device *dev);
219 static int __orinoco_up(struct orinoco_private *priv);
220 static int __orinoco_down(struct orinoco_private *priv);
221 static int __orinoco_commit(struct orinoco_private *priv);
222 
223 /********************************************************************/
224 /* Internal helper functions                                        */
225 /********************************************************************/
226 
227 void set_port_type(struct orinoco_private *priv)
228 {
229 	switch (priv->iw_mode) {
230 	case NL80211_IFTYPE_STATION:
231 		priv->port_type = 1;
232 		priv->createibss = 0;
233 		break;
234 	case NL80211_IFTYPE_ADHOC:
235 		if (priv->prefer_port3) {
236 			priv->port_type = 3;
237 			priv->createibss = 0;
238 		} else {
239 			priv->port_type = priv->ibss_port;
240 			priv->createibss = 1;
241 		}
242 		break;
243 	case NL80211_IFTYPE_MONITOR:
244 		priv->port_type = 3;
245 		priv->createibss = 0;
246 		break;
247 	default:
248 		printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
249 		       priv->ndev->name);
250 	}
251 }
252 
253 /********************************************************************/
254 /* Device methods                                                   */
255 /********************************************************************/
256 
257 int orinoco_open(struct net_device *dev)
258 {
259 	struct orinoco_private *priv = ndev_priv(dev);
260 	unsigned long flags;
261 	int err;
262 
263 	if (orinoco_lock(priv, &flags) != 0)
264 		return -EBUSY;
265 
266 	err = __orinoco_up(priv);
267 
268 	if (!err)
269 		priv->open = 1;
270 
271 	orinoco_unlock(priv, &flags);
272 
273 	return err;
274 }
275 EXPORT_SYMBOL(orinoco_open);
276 
277 int orinoco_stop(struct net_device *dev)
278 {
279 	struct orinoco_private *priv = ndev_priv(dev);
280 	int err = 0;
281 
282 	/* We mustn't use orinoco_lock() here, because we need to be
283 	   able to close the interface even if hw_unavailable is set
284 	   (e.g. as we're released after a PC Card removal) */
285 	orinoco_lock_irq(priv);
286 
287 	priv->open = 0;
288 
289 	err = __orinoco_down(priv);
290 
291 	orinoco_unlock_irq(priv);
292 
293 	return err;
294 }
295 EXPORT_SYMBOL(orinoco_stop);
296 
297 void orinoco_set_multicast_list(struct net_device *dev)
298 {
299 	struct orinoco_private *priv = ndev_priv(dev);
300 	unsigned long flags;
301 
302 	if (orinoco_lock(priv, &flags) != 0) {
303 		printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
304 		       "called when hw_unavailable\n", dev->name);
305 		return;
306 	}
307 
308 	__orinoco_set_multicast_list(dev);
309 	orinoco_unlock(priv, &flags);
310 }
311 EXPORT_SYMBOL(orinoco_set_multicast_list);
312 
313 int orinoco_change_mtu(struct net_device *dev, int new_mtu)
314 {
315 	struct orinoco_private *priv = ndev_priv(dev);
316 
317 	/* MTU + encapsulation + header length */
318 	if ((new_mtu + ENCAPS_OVERHEAD + sizeof(struct ieee80211_hdr)) >
319 	     (priv->nicbuf_size - ETH_HLEN))
320 		return -EINVAL;
321 
322 	dev->mtu = new_mtu;
323 
324 	return 0;
325 }
326 EXPORT_SYMBOL(orinoco_change_mtu);
327 
328 /********************************************************************/
329 /* Tx path                                                          */
330 /********************************************************************/
331 
332 /* Add encapsulation and MIC to the existing SKB.
333  * The main xmit routine will then send the whole lot to the card.
334  * Need 8 bytes headroom
335  * Need 8 bytes tailroom
336  *
337  *                          With encapsulated ethernet II frame
338  *                          --------
339  *                          803.3 header (14 bytes)
340  *                           dst[6]
341  * --------                  src[6]
342  * 803.3 header (14 bytes)   len[2]
343  *  dst[6]                  803.2 header (8 bytes)
344  *  src[6]                   encaps[6]
345  *  len[2] <- leave alone -> len[2]
346  * --------                 -------- <-- 0
347  * Payload                  Payload
348  * ...                      ...
349  *
350  * --------                 --------
351  *                          MIC (8 bytes)
352  *                          --------
353  *
354  * returns 0 on success, -ENOMEM on error.
355  */
356 int orinoco_process_xmit_skb(struct sk_buff *skb,
357 			     struct net_device *dev,
358 			     struct orinoco_private *priv,
359 			     int *tx_control,
360 			     u8 *mic_buf)
361 {
362 	struct orinoco_tkip_key *key;
363 	struct ethhdr *eh;
364 	int do_mic;
365 
366 	key = (struct orinoco_tkip_key *) priv->keys[priv->tx_key].key;
367 
368 	do_mic = ((priv->encode_alg == ORINOCO_ALG_TKIP) &&
369 		  (key != NULL));
370 
371 	if (do_mic)
372 		*tx_control |= (priv->tx_key << HERMES_MIC_KEY_ID_SHIFT) |
373 			HERMES_TXCTRL_MIC;
374 
375 	eh = (struct ethhdr *)skb->data;
376 
377 	/* Encapsulate Ethernet-II frames */
378 	if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
379 		struct header_struct {
380 			struct ethhdr eth;	/* 802.3 header */
381 			u8 encap[6];		/* 802.2 header */
382 		} __packed hdr;
383 		int len = skb->len + sizeof(encaps_hdr) - (2 * ETH_ALEN);
384 
385 		if (skb_headroom(skb) < ENCAPS_OVERHEAD) {
386 			if (net_ratelimit())
387 				printk(KERN_ERR
388 				       "%s: Not enough headroom for 802.2 headers %d\n",
389 				       dev->name, skb_headroom(skb));
390 			return -ENOMEM;
391 		}
392 
393 		/* Fill in new header */
394 		memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
395 		hdr.eth.h_proto = htons(len);
396 		memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
397 
398 		/* Make room for the new header, and copy it in */
399 		eh = (struct ethhdr *) skb_push(skb, ENCAPS_OVERHEAD);
400 		memcpy(eh, &hdr, sizeof(hdr));
401 	}
402 
403 	/* Calculate Michael MIC */
404 	if (do_mic) {
405 		size_t len = skb->len - ETH_HLEN;
406 		u8 *mic = &mic_buf[0];
407 
408 		/* Have to write to an even address, so copy the spare
409 		 * byte across */
410 		if (skb->len % 2) {
411 			*mic = skb->data[skb->len - 1];
412 			mic++;
413 		}
414 
415 		orinoco_mic(priv->tx_tfm_mic, key->tx_mic,
416 			    eh->h_dest, eh->h_source, 0 /* priority */,
417 			    skb->data + ETH_HLEN,
418 			    len, mic);
419 	}
420 
421 	return 0;
422 }
423 EXPORT_SYMBOL(orinoco_process_xmit_skb);
424 
425 static netdev_tx_t orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
426 {
427 	struct orinoco_private *priv = ndev_priv(dev);
428 	struct net_device_stats *stats = &dev->stats;
429 	struct hermes *hw = &priv->hw;
430 	int err = 0;
431 	u16 txfid = priv->txfid;
432 	int tx_control;
433 	unsigned long flags;
434 	u8 mic_buf[MICHAEL_MIC_LEN + 1];
435 
436 	if (!netif_running(dev)) {
437 		printk(KERN_ERR "%s: Tx on stopped device!\n",
438 		       dev->name);
439 		return NETDEV_TX_BUSY;
440 	}
441 
442 	if (netif_queue_stopped(dev)) {
443 		printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
444 		       dev->name);
445 		return NETDEV_TX_BUSY;
446 	}
447 
448 	if (orinoco_lock(priv, &flags) != 0) {
449 		printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
450 		       dev->name);
451 		return NETDEV_TX_BUSY;
452 	}
453 
454 	if (!netif_carrier_ok(dev) ||
455 	    (priv->iw_mode == NL80211_IFTYPE_MONITOR)) {
456 		/* Oops, the firmware hasn't established a connection,
457 		   silently drop the packet (this seems to be the
458 		   safest approach). */
459 		goto drop;
460 	}
461 
462 	/* Check packet length */
463 	if (skb->len < ETH_HLEN)
464 		goto drop;
465 
466 	tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
467 
468 	err = orinoco_process_xmit_skb(skb, dev, priv, &tx_control,
469 				       &mic_buf[0]);
470 	if (err)
471 		goto drop;
472 
473 	if (priv->has_alt_txcntl) {
474 		/* WPA enabled firmwares have tx_cntl at the end of
475 		 * the 802.11 header.  So write zeroed descriptor and
476 		 * 802.11 header at the same time
477 		 */
478 		char desc[HERMES_802_3_OFFSET];
479 		__le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
480 
481 		memset(&desc, 0, sizeof(desc));
482 
483 		*txcntl = cpu_to_le16(tx_control);
484 		err = hw->ops->bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
485 					  txfid, 0);
486 		if (err) {
487 			if (net_ratelimit())
488 				printk(KERN_ERR "%s: Error %d writing Tx "
489 				       "descriptor to BAP\n", dev->name, err);
490 			goto busy;
491 		}
492 	} else {
493 		struct hermes_tx_descriptor desc;
494 
495 		memset(&desc, 0, sizeof(desc));
496 
497 		desc.tx_control = cpu_to_le16(tx_control);
498 		err = hw->ops->bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
499 					  txfid, 0);
500 		if (err) {
501 			if (net_ratelimit())
502 				printk(KERN_ERR "%s: Error %d writing Tx "
503 				       "descriptor to BAP\n", dev->name, err);
504 			goto busy;
505 		}
506 
507 		/* Clear the 802.11 header and data length fields - some
508 		 * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
509 		 * if this isn't done. */
510 		hermes_clear_words(hw, HERMES_DATA0,
511 				   HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
512 	}
513 
514 	err = hw->ops->bap_pwrite(hw, USER_BAP, skb->data, skb->len,
515 				  txfid, HERMES_802_3_OFFSET);
516 	if (err) {
517 		printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
518 		       dev->name, err);
519 		goto busy;
520 	}
521 
522 	if (tx_control & HERMES_TXCTRL_MIC) {
523 		size_t offset = HERMES_802_3_OFFSET + skb->len;
524 		size_t len = MICHAEL_MIC_LEN;
525 
526 		if (offset % 2) {
527 			offset--;
528 			len++;
529 		}
530 		err = hw->ops->bap_pwrite(hw, USER_BAP, &mic_buf[0], len,
531 					  txfid, offset);
532 		if (err) {
533 			printk(KERN_ERR "%s: Error %d writing MIC to BAP\n",
534 			       dev->name, err);
535 			goto busy;
536 		}
537 	}
538 
539 	/* Finally, we actually initiate the send */
540 	netif_stop_queue(dev);
541 
542 	err = hw->ops->cmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
543 				txfid, NULL);
544 	if (err) {
545 		netif_start_queue(dev);
546 		if (net_ratelimit())
547 			printk(KERN_ERR "%s: Error %d transmitting packet\n",
548 				dev->name, err);
549 		goto busy;
550 	}
551 
552 	stats->tx_bytes += HERMES_802_3_OFFSET + skb->len;
553 	goto ok;
554 
555  drop:
556 	stats->tx_errors++;
557 	stats->tx_dropped++;
558 
559  ok:
560 	orinoco_unlock(priv, &flags);
561 	dev_kfree_skb(skb);
562 	return NETDEV_TX_OK;
563 
564  busy:
565 	if (err == -EIO)
566 		schedule_work(&priv->reset_work);
567 	orinoco_unlock(priv, &flags);
568 	return NETDEV_TX_BUSY;
569 }
570 
571 static void __orinoco_ev_alloc(struct net_device *dev, struct hermes *hw)
572 {
573 	struct orinoco_private *priv = ndev_priv(dev);
574 	u16 fid = hermes_read_regn(hw, ALLOCFID);
575 
576 	if (fid != priv->txfid) {
577 		if (fid != DUMMY_FID)
578 			printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
579 			       dev->name, fid);
580 		return;
581 	}
582 
583 	hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
584 }
585 
586 static void __orinoco_ev_tx(struct net_device *dev, struct hermes *hw)
587 {
588 	dev->stats.tx_packets++;
589 
590 	netif_wake_queue(dev);
591 
592 	hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
593 }
594 
595 static void __orinoco_ev_txexc(struct net_device *dev, struct hermes *hw)
596 {
597 	struct net_device_stats *stats = &dev->stats;
598 	u16 fid = hermes_read_regn(hw, TXCOMPLFID);
599 	u16 status;
600 	struct hermes_txexc_data hdr;
601 	int err = 0;
602 
603 	if (fid == DUMMY_FID)
604 		return; /* Nothing's really happened */
605 
606 	/* Read part of the frame header - we need status and addr1 */
607 	err = hw->ops->bap_pread(hw, IRQ_BAP, &hdr,
608 				 sizeof(struct hermes_txexc_data),
609 				 fid, 0);
610 
611 	hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
612 	stats->tx_errors++;
613 
614 	if (err) {
615 		printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
616 		       "(FID=%04X error %d)\n",
617 		       dev->name, fid, err);
618 		return;
619 	}
620 
621 	DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
622 	      err, fid);
623 
624 	/* We produce a TXDROP event only for retry or lifetime
625 	 * exceeded, because that's the only status that really mean
626 	 * that this particular node went away.
627 	 * Other errors means that *we* screwed up. - Jean II */
628 	status = le16_to_cpu(hdr.desc.status);
629 	if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
630 		union iwreq_data	wrqu;
631 
632 		/* Copy 802.11 dest address.
633 		 * We use the 802.11 header because the frame may
634 		 * not be 802.3 or may be mangled...
635 		 * In Ad-Hoc mode, it will be the node address.
636 		 * In managed mode, it will be most likely the AP addr
637 		 * User space will figure out how to convert it to
638 		 * whatever it needs (IP address or else).
639 		 * - Jean II */
640 		memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
641 		wrqu.addr.sa_family = ARPHRD_ETHER;
642 
643 		/* Send event to user space */
644 		wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
645 	}
646 
647 	netif_wake_queue(dev);
648 }
649 
650 void orinoco_tx_timeout(struct net_device *dev)
651 {
652 	struct orinoco_private *priv = ndev_priv(dev);
653 	struct net_device_stats *stats = &dev->stats;
654 	struct hermes *hw = &priv->hw;
655 
656 	printk(KERN_WARNING "%s: Tx timeout! "
657 	       "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
658 	       dev->name, hermes_read_regn(hw, ALLOCFID),
659 	       hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
660 
661 	stats->tx_errors++;
662 
663 	schedule_work(&priv->reset_work);
664 }
665 EXPORT_SYMBOL(orinoco_tx_timeout);
666 
667 /********************************************************************/
668 /* Rx path (data frames)                                            */
669 /********************************************************************/
670 
671 /* Does the frame have a SNAP header indicating it should be
672  * de-encapsulated to Ethernet-II? */
673 static inline int is_ethersnap(void *_hdr)
674 {
675 	u8 *hdr = _hdr;
676 
677 	/* We de-encapsulate all packets which, a) have SNAP headers
678 	 * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
679 	 * and where b) the OUI of the SNAP header is 00:00:00 or
680 	 * 00:00:f8 - we need both because different APs appear to use
681 	 * different OUIs for some reason */
682 	return (memcmp(hdr, &encaps_hdr, 5) == 0)
683 		&& ((hdr[5] == 0x00) || (hdr[5] == 0xf8));
684 }
685 
686 static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
687 				      int level, int noise)
688 {
689 	struct iw_quality wstats;
690 	wstats.level = level - 0x95;
691 	wstats.noise = noise - 0x95;
692 	wstats.qual = (level > noise) ? (level - noise) : 0;
693 	wstats.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
694 	/* Update spy records */
695 	wireless_spy_update(dev, mac, &wstats);
696 }
697 
698 static void orinoco_stat_gather(struct net_device *dev,
699 				struct sk_buff *skb,
700 				struct hermes_rx_descriptor *desc)
701 {
702 	struct orinoco_private *priv = ndev_priv(dev);
703 
704 	/* Using spy support with lots of Rx packets, like in an
705 	 * infrastructure (AP), will really slow down everything, because
706 	 * the MAC address must be compared to each entry of the spy list.
707 	 * If the user really asks for it (set some address in the
708 	 * spy list), we do it, but he will pay the price.
709 	 * Note that to get here, you need both WIRELESS_SPY
710 	 * compiled in AND some addresses in the list !!!
711 	 */
712 	/* Note : gcc will optimise the whole section away if
713 	 * WIRELESS_SPY is not defined... - Jean II */
714 	if (SPY_NUMBER(priv)) {
715 		orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
716 				   desc->signal, desc->silence);
717 	}
718 }
719 
720 /*
721  * orinoco_rx_monitor - handle received monitor frames.
722  *
723  * Arguments:
724  *	dev		network device
725  *	rxfid		received FID
726  *	desc		rx descriptor of the frame
727  *
728  * Call context: interrupt
729  */
730 static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
731 			       struct hermes_rx_descriptor *desc)
732 {
733 	u32 hdrlen = 30;	/* return full header by default */
734 	u32 datalen = 0;
735 	u16 fc;
736 	int err;
737 	int len;
738 	struct sk_buff *skb;
739 	struct orinoco_private *priv = ndev_priv(dev);
740 	struct net_device_stats *stats = &dev->stats;
741 	struct hermes *hw = &priv->hw;
742 
743 	len = le16_to_cpu(desc->data_len);
744 
745 	/* Determine the size of the header and the data */
746 	fc = le16_to_cpu(desc->frame_ctl);
747 	switch (fc & IEEE80211_FCTL_FTYPE) {
748 	case IEEE80211_FTYPE_DATA:
749 		if ((fc & IEEE80211_FCTL_TODS)
750 		    && (fc & IEEE80211_FCTL_FROMDS))
751 			hdrlen = 30;
752 		else
753 			hdrlen = 24;
754 		datalen = len;
755 		break;
756 	case IEEE80211_FTYPE_MGMT:
757 		hdrlen = 24;
758 		datalen = len;
759 		break;
760 	case IEEE80211_FTYPE_CTL:
761 		switch (fc & IEEE80211_FCTL_STYPE) {
762 		case IEEE80211_STYPE_PSPOLL:
763 		case IEEE80211_STYPE_RTS:
764 		case IEEE80211_STYPE_CFEND:
765 		case IEEE80211_STYPE_CFENDACK:
766 			hdrlen = 16;
767 			break;
768 		case IEEE80211_STYPE_CTS:
769 		case IEEE80211_STYPE_ACK:
770 			hdrlen = 10;
771 			break;
772 		}
773 		break;
774 	default:
775 		/* Unknown frame type */
776 		break;
777 	}
778 
779 	/* sanity check the length */
780 	if (datalen > IEEE80211_MAX_DATA_LEN + 12) {
781 		printk(KERN_DEBUG "%s: oversized monitor frame, "
782 		       "data length = %d\n", dev->name, datalen);
783 		stats->rx_length_errors++;
784 		goto update_stats;
785 	}
786 
787 	skb = dev_alloc_skb(hdrlen + datalen);
788 	if (!skb) {
789 		printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
790 		       dev->name);
791 		goto update_stats;
792 	}
793 
794 	/* Copy the 802.11 header to the skb */
795 	memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
796 	skb_reset_mac_header(skb);
797 
798 	/* If any, copy the data from the card to the skb */
799 	if (datalen > 0) {
800 		err = hw->ops->bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
801 					 ALIGN(datalen, 2), rxfid,
802 					 HERMES_802_2_OFFSET);
803 		if (err) {
804 			printk(KERN_ERR "%s: error %d reading monitor frame\n",
805 			       dev->name, err);
806 			goto drop;
807 		}
808 	}
809 
810 	skb->dev = dev;
811 	skb->ip_summed = CHECKSUM_NONE;
812 	skb->pkt_type = PACKET_OTHERHOST;
813 	skb->protocol = cpu_to_be16(ETH_P_802_2);
814 
815 	stats->rx_packets++;
816 	stats->rx_bytes += skb->len;
817 
818 	netif_rx(skb);
819 	return;
820 
821  drop:
822 	dev_kfree_skb_irq(skb);
823  update_stats:
824 	stats->rx_errors++;
825 	stats->rx_dropped++;
826 }
827 
828 void __orinoco_ev_rx(struct net_device *dev, struct hermes *hw)
829 {
830 	struct orinoco_private *priv = ndev_priv(dev);
831 	struct net_device_stats *stats = &dev->stats;
832 	struct iw_statistics *wstats = &priv->wstats;
833 	struct sk_buff *skb = NULL;
834 	u16 rxfid, status;
835 	int length;
836 	struct hermes_rx_descriptor *desc;
837 	struct orinoco_rx_data *rx_data;
838 	int err;
839 
840 	desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
841 	if (!desc)
842 		goto update_stats;
843 
844 	rxfid = hermes_read_regn(hw, RXFID);
845 
846 	err = hw->ops->bap_pread(hw, IRQ_BAP, desc, sizeof(*desc),
847 				 rxfid, 0);
848 	if (err) {
849 		printk(KERN_ERR "%s: error %d reading Rx descriptor. "
850 		       "Frame dropped.\n", dev->name, err);
851 		goto update_stats;
852 	}
853 
854 	status = le16_to_cpu(desc->status);
855 
856 	if (status & HERMES_RXSTAT_BADCRC) {
857 		DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
858 		      dev->name);
859 		stats->rx_crc_errors++;
860 		goto update_stats;
861 	}
862 
863 	/* Handle frames in monitor mode */
864 	if (priv->iw_mode == NL80211_IFTYPE_MONITOR) {
865 		orinoco_rx_monitor(dev, rxfid, desc);
866 		goto out;
867 	}
868 
869 	if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
870 		DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
871 		      dev->name);
872 		wstats->discard.code++;
873 		goto update_stats;
874 	}
875 
876 	length = le16_to_cpu(desc->data_len);
877 
878 	/* Sanity checks */
879 	if (length < 3) { /* No for even an 802.2 LLC header */
880 		/* At least on Symbol firmware with PCF we get quite a
881 		   lot of these legitimately - Poll frames with no
882 		   data. */
883 		goto out;
884 	}
885 	if (length > IEEE80211_MAX_DATA_LEN) {
886 		printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
887 		       dev->name, length);
888 		stats->rx_length_errors++;
889 		goto update_stats;
890 	}
891 
892 	/* Payload size does not include Michael MIC. Increase payload
893 	 * size to read it together with the data. */
894 	if (status & HERMES_RXSTAT_MIC)
895 		length += MICHAEL_MIC_LEN;
896 
897 	/* We need space for the packet data itself, plus an ethernet
898 	   header, plus 2 bytes so we can align the IP header on a
899 	   32bit boundary, plus 1 byte so we can read in odd length
900 	   packets from the card, which has an IO granularity of 16
901 	   bits */
902 	skb = dev_alloc_skb(length + ETH_HLEN + 2 + 1);
903 	if (!skb) {
904 		printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
905 		       dev->name);
906 		goto update_stats;
907 	}
908 
909 	/* We'll prepend the header, so reserve space for it.  The worst
910 	   case is no decapsulation, when 802.3 header is prepended and
911 	   nothing is removed.  2 is for aligning the IP header.  */
912 	skb_reserve(skb, ETH_HLEN + 2);
913 
914 	err = hw->ops->bap_pread(hw, IRQ_BAP, skb_put(skb, length),
915 				 ALIGN(length, 2), rxfid,
916 				 HERMES_802_2_OFFSET);
917 	if (err) {
918 		printk(KERN_ERR "%s: error %d reading frame. "
919 		       "Frame dropped.\n", dev->name, err);
920 		goto drop;
921 	}
922 
923 	/* Add desc and skb to rx queue */
924 	rx_data = kzalloc(sizeof(*rx_data), GFP_ATOMIC);
925 	if (!rx_data)
926 		goto drop;
927 
928 	rx_data->desc = desc;
929 	rx_data->skb = skb;
930 	list_add_tail(&rx_data->list, &priv->rx_list);
931 	tasklet_schedule(&priv->rx_tasklet);
932 
933 	return;
934 
935 drop:
936 	dev_kfree_skb_irq(skb);
937 update_stats:
938 	stats->rx_errors++;
939 	stats->rx_dropped++;
940 out:
941 	kfree(desc);
942 }
943 EXPORT_SYMBOL(__orinoco_ev_rx);
944 
945 static void orinoco_rx(struct net_device *dev,
946 		       struct hermes_rx_descriptor *desc,
947 		       struct sk_buff *skb)
948 {
949 	struct orinoco_private *priv = ndev_priv(dev);
950 	struct net_device_stats *stats = &dev->stats;
951 	u16 status, fc;
952 	int length;
953 	struct ethhdr *hdr;
954 
955 	status = le16_to_cpu(desc->status);
956 	length = le16_to_cpu(desc->data_len);
957 	fc = le16_to_cpu(desc->frame_ctl);
958 
959 	/* Calculate and check MIC */
960 	if (status & HERMES_RXSTAT_MIC) {
961 		struct orinoco_tkip_key *key;
962 		int key_id = ((status & HERMES_RXSTAT_MIC_KEY_ID) >>
963 			      HERMES_MIC_KEY_ID_SHIFT);
964 		u8 mic[MICHAEL_MIC_LEN];
965 		u8 *rxmic;
966 		u8 *src = (fc & IEEE80211_FCTL_FROMDS) ?
967 			desc->addr3 : desc->addr2;
968 
969 		/* Extract Michael MIC from payload */
970 		rxmic = skb->data + skb->len - MICHAEL_MIC_LEN;
971 
972 		skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
973 		length -= MICHAEL_MIC_LEN;
974 
975 		key = (struct orinoco_tkip_key *) priv->keys[key_id].key;
976 
977 		if (!key) {
978 			printk(KERN_WARNING "%s: Received encrypted frame from "
979 			       "%pM using key %i, but key is not installed\n",
980 			       dev->name, src, key_id);
981 			goto drop;
982 		}
983 
984 		orinoco_mic(priv->rx_tfm_mic, key->rx_mic, desc->addr1, src,
985 			    0, /* priority or QoS? */
986 			    skb->data, skb->len, &mic[0]);
987 
988 		if (memcmp(mic, rxmic,
989 			   MICHAEL_MIC_LEN)) {
990 			union iwreq_data wrqu;
991 			struct iw_michaelmicfailure wxmic;
992 
993 			printk(KERN_WARNING "%s: "
994 			       "Invalid Michael MIC in data frame from %pM, "
995 			       "using key %i\n",
996 			       dev->name, src, key_id);
997 
998 			/* TODO: update stats */
999 
1000 			/* Notify userspace */
1001 			memset(&wxmic, 0, sizeof(wxmic));
1002 			wxmic.flags = key_id & IW_MICFAILURE_KEY_ID;
1003 			wxmic.flags |= (desc->addr1[0] & 1) ?
1004 				IW_MICFAILURE_GROUP : IW_MICFAILURE_PAIRWISE;
1005 			wxmic.src_addr.sa_family = ARPHRD_ETHER;
1006 			memcpy(wxmic.src_addr.sa_data, src, ETH_ALEN);
1007 
1008 			(void) orinoco_hw_get_tkip_iv(priv, key_id,
1009 						      &wxmic.tsc[0]);
1010 
1011 			memset(&wrqu, 0, sizeof(wrqu));
1012 			wrqu.data.length = sizeof(wxmic);
1013 			wireless_send_event(dev, IWEVMICHAELMICFAILURE, &wrqu,
1014 					    (char *) &wxmic);
1015 
1016 			goto drop;
1017 		}
1018 	}
1019 
1020 	/* Handle decapsulation
1021 	 * In most cases, the firmware tell us about SNAP frames.
1022 	 * For some reason, the SNAP frames sent by LinkSys APs
1023 	 * are not properly recognised by most firmwares.
1024 	 * So, check ourselves */
1025 	if (length >= ENCAPS_OVERHEAD &&
1026 	    (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
1027 	     ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
1028 	     is_ethersnap(skb->data))) {
1029 		/* These indicate a SNAP within 802.2 LLC within
1030 		   802.11 frame which we'll need to de-encapsulate to
1031 		   the original EthernetII frame. */
1032 		hdr = (struct ethhdr *)skb_push(skb,
1033 						ETH_HLEN - ENCAPS_OVERHEAD);
1034 	} else {
1035 		/* 802.3 frame - prepend 802.3 header as is */
1036 		hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
1037 		hdr->h_proto = htons(length);
1038 	}
1039 	memcpy(hdr->h_dest, desc->addr1, ETH_ALEN);
1040 	if (fc & IEEE80211_FCTL_FROMDS)
1041 		memcpy(hdr->h_source, desc->addr3, ETH_ALEN);
1042 	else
1043 		memcpy(hdr->h_source, desc->addr2, ETH_ALEN);
1044 
1045 	skb->protocol = eth_type_trans(skb, dev);
1046 	skb->ip_summed = CHECKSUM_NONE;
1047 	if (fc & IEEE80211_FCTL_TODS)
1048 		skb->pkt_type = PACKET_OTHERHOST;
1049 
1050 	/* Process the wireless stats if needed */
1051 	orinoco_stat_gather(dev, skb, desc);
1052 
1053 	/* Pass the packet to the networking stack */
1054 	netif_rx(skb);
1055 	stats->rx_packets++;
1056 	stats->rx_bytes += length;
1057 
1058 	return;
1059 
1060  drop:
1061 	dev_kfree_skb(skb);
1062 	stats->rx_errors++;
1063 	stats->rx_dropped++;
1064 }
1065 
1066 static void orinoco_rx_isr_tasklet(unsigned long data)
1067 {
1068 	struct orinoco_private *priv = (struct orinoco_private *) data;
1069 	struct net_device *dev = priv->ndev;
1070 	struct orinoco_rx_data *rx_data, *temp;
1071 	struct hermes_rx_descriptor *desc;
1072 	struct sk_buff *skb;
1073 	unsigned long flags;
1074 
1075 	/* orinoco_rx requires the driver lock, and we also need to
1076 	 * protect priv->rx_list, so just hold the lock over the
1077 	 * lot.
1078 	 *
1079 	 * If orinoco_lock fails, we've unplugged the card. In this
1080 	 * case just abort. */
1081 	if (orinoco_lock(priv, &flags) != 0)
1082 		return;
1083 
1084 	/* extract desc and skb from queue */
1085 	list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
1086 		desc = rx_data->desc;
1087 		skb = rx_data->skb;
1088 		list_del(&rx_data->list);
1089 		kfree(rx_data);
1090 
1091 		orinoco_rx(dev, desc, skb);
1092 
1093 		kfree(desc);
1094 	}
1095 
1096 	orinoco_unlock(priv, &flags);
1097 }
1098 
1099 /********************************************************************/
1100 /* Rx path (info frames)                                            */
1101 /********************************************************************/
1102 
1103 static void print_linkstatus(struct net_device *dev, u16 status)
1104 {
1105 	char *s;
1106 
1107 	if (suppress_linkstatus)
1108 		return;
1109 
1110 	switch (status) {
1111 	case HERMES_LINKSTATUS_NOT_CONNECTED:
1112 		s = "Not Connected";
1113 		break;
1114 	case HERMES_LINKSTATUS_CONNECTED:
1115 		s = "Connected";
1116 		break;
1117 	case HERMES_LINKSTATUS_DISCONNECTED:
1118 		s = "Disconnected";
1119 		break;
1120 	case HERMES_LINKSTATUS_AP_CHANGE:
1121 		s = "AP Changed";
1122 		break;
1123 	case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1124 		s = "AP Out of Range";
1125 		break;
1126 	case HERMES_LINKSTATUS_AP_IN_RANGE:
1127 		s = "AP In Range";
1128 		break;
1129 	case HERMES_LINKSTATUS_ASSOC_FAILED:
1130 		s = "Association Failed";
1131 		break;
1132 	default:
1133 		s = "UNKNOWN";
1134 	}
1135 
1136 	printk(KERN_DEBUG "%s: New link status: %s (%04x)\n",
1137 	       dev->name, s, status);
1138 }
1139 
1140 /* Search scan results for requested BSSID, join it if found */
1141 static void orinoco_join_ap(struct work_struct *work)
1142 {
1143 	struct orinoco_private *priv =
1144 		container_of(work, struct orinoco_private, join_work);
1145 	struct net_device *dev = priv->ndev;
1146 	struct hermes *hw = &priv->hw;
1147 	int err;
1148 	unsigned long flags;
1149 	struct join_req {
1150 		u8 bssid[ETH_ALEN];
1151 		__le16 channel;
1152 	} __packed req;
1153 	const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
1154 	struct prism2_scan_apinfo *atom = NULL;
1155 	int offset = 4;
1156 	int found = 0;
1157 	u8 *buf;
1158 	u16 len;
1159 
1160 	/* Allocate buffer for scan results */
1161 	buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
1162 	if (!buf)
1163 		return;
1164 
1165 	if (orinoco_lock(priv, &flags) != 0)
1166 		goto fail_lock;
1167 
1168 	/* Sanity checks in case user changed something in the meantime */
1169 	if (!priv->bssid_fixed)
1170 		goto out;
1171 
1172 	if (strlen(priv->desired_essid) == 0)
1173 		goto out;
1174 
1175 	/* Read scan results from the firmware */
1176 	err = hw->ops->read_ltv(hw, USER_BAP,
1177 				HERMES_RID_SCANRESULTSTABLE,
1178 				MAX_SCAN_LEN, &len, buf);
1179 	if (err) {
1180 		printk(KERN_ERR "%s: Cannot read scan results\n",
1181 		       dev->name);
1182 		goto out;
1183 	}
1184 
1185 	len = HERMES_RECLEN_TO_BYTES(len);
1186 
1187 	/* Go through the scan results looking for the channel of the AP
1188 	 * we were requested to join */
1189 	for (; offset + atom_len <= len; offset += atom_len) {
1190 		atom = (struct prism2_scan_apinfo *) (buf + offset);
1191 		if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1192 			found = 1;
1193 			break;
1194 		}
1195 	}
1196 
1197 	if (!found) {
1198 		DEBUG(1, "%s: Requested AP not found in scan results\n",
1199 		      dev->name);
1200 		goto out;
1201 	}
1202 
1203 	memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1204 	req.channel = atom->channel;	/* both are little-endian */
1205 	err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1206 				  &req);
1207 	if (err)
1208 		printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1209 
1210  out:
1211 	orinoco_unlock(priv, &flags);
1212 
1213  fail_lock:
1214 	kfree(buf);
1215 }
1216 
1217 /* Send new BSSID to userspace */
1218 static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
1219 {
1220 	struct net_device *dev = priv->ndev;
1221 	struct hermes *hw = &priv->hw;
1222 	union iwreq_data wrqu;
1223 	int err;
1224 
1225 	err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
1226 				ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
1227 	if (err != 0)
1228 		return;
1229 
1230 	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1231 
1232 	/* Send event to user space */
1233 	wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
1234 }
1235 
1236 static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
1237 {
1238 	struct net_device *dev = priv->ndev;
1239 	struct hermes *hw = &priv->hw;
1240 	union iwreq_data wrqu;
1241 	int err;
1242 	u8 buf[88];
1243 	u8 *ie;
1244 
1245 	if (!priv->has_wpa)
1246 		return;
1247 
1248 	err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
1249 				sizeof(buf), NULL, &buf);
1250 	if (err != 0)
1251 		return;
1252 
1253 	ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1254 	if (ie) {
1255 		int rem = sizeof(buf) - (ie - &buf[0]);
1256 		wrqu.data.length = ie[1] + 2;
1257 		if (wrqu.data.length > rem)
1258 			wrqu.data.length = rem;
1259 
1260 		if (wrqu.data.length)
1261 			/* Send event to user space */
1262 			wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
1263 	}
1264 }
1265 
1266 static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
1267 {
1268 	struct net_device *dev = priv->ndev;
1269 	struct hermes *hw = &priv->hw;
1270 	union iwreq_data wrqu;
1271 	int err;
1272 	u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
1273 	u8 *ie;
1274 
1275 	if (!priv->has_wpa)
1276 		return;
1277 
1278 	err = hw->ops->read_ltv(hw, USER_BAP,
1279 				HERMES_RID_CURRENT_ASSOC_RESP_INFO,
1280 				sizeof(buf), NULL, &buf);
1281 	if (err != 0)
1282 		return;
1283 
1284 	ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1285 	if (ie) {
1286 		int rem = sizeof(buf) - (ie - &buf[0]);
1287 		wrqu.data.length = ie[1] + 2;
1288 		if (wrqu.data.length > rem)
1289 			wrqu.data.length = rem;
1290 
1291 		if (wrqu.data.length)
1292 			/* Send event to user space */
1293 			wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
1294 	}
1295 }
1296 
1297 static void orinoco_send_wevents(struct work_struct *work)
1298 {
1299 	struct orinoco_private *priv =
1300 		container_of(work, struct orinoco_private, wevent_work);
1301 	unsigned long flags;
1302 
1303 	if (orinoco_lock(priv, &flags) != 0)
1304 		return;
1305 
1306 	orinoco_send_assocreqie_wevent(priv);
1307 	orinoco_send_assocrespie_wevent(priv);
1308 	orinoco_send_bssid_wevent(priv);
1309 
1310 	orinoco_unlock(priv, &flags);
1311 }
1312 
1313 static void qbuf_scan(struct orinoco_private *priv, void *buf,
1314 		      int len, int type)
1315 {
1316 	struct orinoco_scan_data *sd;
1317 	unsigned long flags;
1318 
1319 	sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
1320 	if (!sd)
1321 		return;
1322 
1323 	sd->buf = buf;
1324 	sd->len = len;
1325 	sd->type = type;
1326 
1327 	spin_lock_irqsave(&priv->scan_lock, flags);
1328 	list_add_tail(&sd->list, &priv->scan_list);
1329 	spin_unlock_irqrestore(&priv->scan_lock, flags);
1330 
1331 	schedule_work(&priv->process_scan);
1332 }
1333 
1334 static void qabort_scan(struct orinoco_private *priv)
1335 {
1336 	struct orinoco_scan_data *sd;
1337 	unsigned long flags;
1338 
1339 	sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
1340 	if (!sd)
1341 		return;
1342 
1343 	sd->len = -1; /* Abort */
1344 
1345 	spin_lock_irqsave(&priv->scan_lock, flags);
1346 	list_add_tail(&sd->list, &priv->scan_list);
1347 	spin_unlock_irqrestore(&priv->scan_lock, flags);
1348 
1349 	schedule_work(&priv->process_scan);
1350 }
1351 
1352 static void orinoco_process_scan_results(struct work_struct *work)
1353 {
1354 	struct orinoco_private *priv =
1355 		container_of(work, struct orinoco_private, process_scan);
1356 	struct orinoco_scan_data *sd, *temp;
1357 	unsigned long flags;
1358 	void *buf;
1359 	int len;
1360 	int type;
1361 
1362 	spin_lock_irqsave(&priv->scan_lock, flags);
1363 	list_for_each_entry_safe(sd, temp, &priv->scan_list, list) {
1364 
1365 		buf = sd->buf;
1366 		len = sd->len;
1367 		type = sd->type;
1368 
1369 		list_del(&sd->list);
1370 		spin_unlock_irqrestore(&priv->scan_lock, flags);
1371 		kfree(sd);
1372 
1373 		if (len > 0) {
1374 			if (type == HERMES_INQ_CHANNELINFO)
1375 				orinoco_add_extscan_result(priv, buf, len);
1376 			else
1377 				orinoco_add_hostscan_results(priv, buf, len);
1378 
1379 			kfree(buf);
1380 		} else {
1381 			/* Either abort or complete the scan */
1382 			orinoco_scan_done(priv, (len < 0));
1383 		}
1384 
1385 		spin_lock_irqsave(&priv->scan_lock, flags);
1386 	}
1387 	spin_unlock_irqrestore(&priv->scan_lock, flags);
1388 }
1389 
1390 void __orinoco_ev_info(struct net_device *dev, struct hermes *hw)
1391 {
1392 	struct orinoco_private *priv = ndev_priv(dev);
1393 	u16 infofid;
1394 	struct {
1395 		__le16 len;
1396 		__le16 type;
1397 	} __packed info;
1398 	int len, type;
1399 	int err;
1400 
1401 	/* This is an answer to an INQUIRE command that we did earlier,
1402 	 * or an information "event" generated by the card
1403 	 * The controller return to us a pseudo frame containing
1404 	 * the information in question - Jean II */
1405 	infofid = hermes_read_regn(hw, INFOFID);
1406 
1407 	/* Read the info frame header - don't try too hard */
1408 	err = hw->ops->bap_pread(hw, IRQ_BAP, &info, sizeof(info),
1409 				 infofid, 0);
1410 	if (err) {
1411 		printk(KERN_ERR "%s: error %d reading info frame. "
1412 		       "Frame dropped.\n", dev->name, err);
1413 		return;
1414 	}
1415 
1416 	len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
1417 	type = le16_to_cpu(info.type);
1418 
1419 	switch (type) {
1420 	case HERMES_INQ_TALLIES: {
1421 		struct hermes_tallies_frame tallies;
1422 		struct iw_statistics *wstats = &priv->wstats;
1423 
1424 		if (len > sizeof(tallies)) {
1425 			printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
1426 			       dev->name, len);
1427 			len = sizeof(tallies);
1428 		}
1429 
1430 		err = hw->ops->bap_pread(hw, IRQ_BAP, &tallies, len,
1431 					 infofid, sizeof(info));
1432 		if (err)
1433 			break;
1434 
1435 		/* Increment our various counters */
1436 		/* wstats->discard.nwid - no wrong BSSID stuff */
1437 		wstats->discard.code +=
1438 			le16_to_cpu(tallies.RxWEPUndecryptable);
1439 		if (len == sizeof(tallies))
1440 			wstats->discard.code +=
1441 				le16_to_cpu(tallies.RxDiscards_WEPICVError) +
1442 				le16_to_cpu(tallies.RxDiscards_WEPExcluded);
1443 		wstats->discard.misc +=
1444 			le16_to_cpu(tallies.TxDiscardsWrongSA);
1445 		wstats->discard.fragment +=
1446 			le16_to_cpu(tallies.RxMsgInBadMsgFragments);
1447 		wstats->discard.retries +=
1448 			le16_to_cpu(tallies.TxRetryLimitExceeded);
1449 		/* wstats->miss.beacon - no match */
1450 	}
1451 	break;
1452 	case HERMES_INQ_LINKSTATUS: {
1453 		struct hermes_linkstatus linkstatus;
1454 		u16 newstatus;
1455 		int connected;
1456 
1457 		if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
1458 			break;
1459 
1460 		if (len != sizeof(linkstatus)) {
1461 			printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
1462 			       dev->name, len);
1463 			break;
1464 		}
1465 
1466 		err = hw->ops->bap_pread(hw, IRQ_BAP, &linkstatus, len,
1467 					 infofid, sizeof(info));
1468 		if (err)
1469 			break;
1470 		newstatus = le16_to_cpu(linkstatus.linkstatus);
1471 
1472 		/* Symbol firmware uses "out of range" to signal that
1473 		 * the hostscan frame can be requested.  */
1474 		if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
1475 		    priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
1476 		    priv->has_hostscan && priv->scan_request) {
1477 			hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
1478 			break;
1479 		}
1480 
1481 		connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
1482 			|| (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
1483 			|| (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
1484 
1485 		if (connected)
1486 			netif_carrier_on(dev);
1487 		else if (!ignore_disconnect)
1488 			netif_carrier_off(dev);
1489 
1490 		if (newstatus != priv->last_linkstatus) {
1491 			priv->last_linkstatus = newstatus;
1492 			print_linkstatus(dev, newstatus);
1493 			/* The info frame contains only one word which is the
1494 			 * status (see hermes.h). The status is pretty boring
1495 			 * in itself, that's why we export the new BSSID...
1496 			 * Jean II */
1497 			schedule_work(&priv->wevent_work);
1498 		}
1499 	}
1500 	break;
1501 	case HERMES_INQ_SCAN:
1502 		if (!priv->scan_request && priv->bssid_fixed &&
1503 		    priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
1504 			schedule_work(&priv->join_work);
1505 			break;
1506 		}
1507 		/* fall through */
1508 	case HERMES_INQ_HOSTSCAN:
1509 	case HERMES_INQ_HOSTSCAN_SYMBOL: {
1510 		/* Result of a scanning. Contains information about
1511 		 * cells in the vicinity - Jean II */
1512 		unsigned char *buf;
1513 
1514 		/* Sanity check */
1515 		if (len > 4096) {
1516 			printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
1517 			       dev->name, len);
1518 			qabort_scan(priv);
1519 			break;
1520 		}
1521 
1522 		/* Allocate buffer for results */
1523 		buf = kmalloc(len, GFP_ATOMIC);
1524 		if (buf == NULL) {
1525 			/* No memory, so can't printk()... */
1526 			qabort_scan(priv);
1527 			break;
1528 		}
1529 
1530 		/* Read scan data */
1531 		err = hw->ops->bap_pread(hw, IRQ_BAP, (void *) buf, len,
1532 					 infofid, sizeof(info));
1533 		if (err) {
1534 			kfree(buf);
1535 			qabort_scan(priv);
1536 			break;
1537 		}
1538 
1539 #ifdef ORINOCO_DEBUG
1540 		{
1541 			int	i;
1542 			printk(KERN_DEBUG "Scan result [%02X", buf[0]);
1543 			for (i = 1; i < (len * 2); i++)
1544 				printk(":%02X", buf[i]);
1545 			printk("]\n");
1546 		}
1547 #endif	/* ORINOCO_DEBUG */
1548 
1549 		qbuf_scan(priv, buf, len, type);
1550 	}
1551 	break;
1552 	case HERMES_INQ_CHANNELINFO:
1553 	{
1554 		struct agere_ext_scan_info *bss;
1555 
1556 		if (!priv->scan_request) {
1557 			printk(KERN_DEBUG "%s: Got chaninfo without scan, "
1558 			       "len=%d\n", dev->name, len);
1559 			break;
1560 		}
1561 
1562 		/* An empty result indicates that the scan is complete */
1563 		if (len == 0) {
1564 			qbuf_scan(priv, NULL, len, type);
1565 			break;
1566 		}
1567 
1568 		/* Sanity check */
1569 		else if (len < (offsetof(struct agere_ext_scan_info,
1570 					   data) + 2)) {
1571 			/* Drop this result now so we don't have to
1572 			 * keep checking later */
1573 			printk(KERN_WARNING
1574 			       "%s: Ext scan results too short (%d bytes)\n",
1575 			       dev->name, len);
1576 			break;
1577 		}
1578 
1579 		bss = kmalloc(len, GFP_ATOMIC);
1580 		if (bss == NULL)
1581 			break;
1582 
1583 		/* Read scan data */
1584 		err = hw->ops->bap_pread(hw, IRQ_BAP, (void *) bss, len,
1585 					 infofid, sizeof(info));
1586 		if (err)
1587 			kfree(bss);
1588 		else
1589 			qbuf_scan(priv, bss, len, type);
1590 
1591 		break;
1592 	}
1593 	case HERMES_INQ_SEC_STAT_AGERE:
1594 		/* Security status (Agere specific) */
1595 		/* Ignore this frame for now */
1596 		if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
1597 			break;
1598 		/* fall through */
1599 	default:
1600 		printk(KERN_DEBUG "%s: Unknown information frame received: "
1601 		       "type 0x%04x, length %d\n", dev->name, type, len);
1602 		/* We don't actually do anything about it */
1603 		break;
1604 	}
1605 }
1606 EXPORT_SYMBOL(__orinoco_ev_info);
1607 
1608 static void __orinoco_ev_infdrop(struct net_device *dev, struct hermes *hw)
1609 {
1610 	if (net_ratelimit())
1611 		printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
1612 }
1613 
1614 /********************************************************************/
1615 /* Internal hardware control routines                               */
1616 /********************************************************************/
1617 
1618 static int __orinoco_up(struct orinoco_private *priv)
1619 {
1620 	struct net_device *dev = priv->ndev;
1621 	struct hermes *hw = &priv->hw;
1622 	int err;
1623 
1624 	netif_carrier_off(dev); /* just to make sure */
1625 
1626 	err = __orinoco_commit(priv);
1627 	if (err) {
1628 		printk(KERN_ERR "%s: Error %d configuring card\n",
1629 		       dev->name, err);
1630 		return err;
1631 	}
1632 
1633 	/* Fire things up again */
1634 	hermes_set_irqmask(hw, ORINOCO_INTEN);
1635 	err = hermes_enable_port(hw, 0);
1636 	if (err) {
1637 		printk(KERN_ERR "%s: Error %d enabling MAC port\n",
1638 		       dev->name, err);
1639 		return err;
1640 	}
1641 
1642 	netif_start_queue(dev);
1643 
1644 	return 0;
1645 }
1646 
1647 static int __orinoco_down(struct orinoco_private *priv)
1648 {
1649 	struct net_device *dev = priv->ndev;
1650 	struct hermes *hw = &priv->hw;
1651 	int err;
1652 
1653 	netif_stop_queue(dev);
1654 
1655 	if (!priv->hw_unavailable) {
1656 		if (!priv->broken_disableport) {
1657 			err = hermes_disable_port(hw, 0);
1658 			if (err) {
1659 				/* Some firmwares (e.g. Intersil 1.3.x) seem
1660 				 * to have problems disabling the port, oh
1661 				 * well, too bad. */
1662 				printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
1663 				       dev->name, err);
1664 				priv->broken_disableport = 1;
1665 			}
1666 		}
1667 		hermes_set_irqmask(hw, 0);
1668 		hermes_write_regn(hw, EVACK, 0xffff);
1669 	}
1670 
1671 	orinoco_scan_done(priv, true);
1672 
1673 	/* firmware will have to reassociate */
1674 	netif_carrier_off(dev);
1675 	priv->last_linkstatus = 0xffff;
1676 
1677 	return 0;
1678 }
1679 
1680 static int orinoco_reinit_firmware(struct orinoco_private *priv)
1681 {
1682 	struct hermes *hw = &priv->hw;
1683 	int err;
1684 
1685 	err = hw->ops->init(hw);
1686 	if (priv->do_fw_download && !err) {
1687 		err = orinoco_download(priv);
1688 		if (err)
1689 			priv->do_fw_download = 0;
1690 	}
1691 	if (!err)
1692 		err = orinoco_hw_allocate_fid(priv);
1693 
1694 	return err;
1695 }
1696 
1697 static int
1698 __orinoco_set_multicast_list(struct net_device *dev)
1699 {
1700 	struct orinoco_private *priv = ndev_priv(dev);
1701 	int err = 0;
1702 	int promisc, mc_count;
1703 
1704 	/* The Hermes doesn't seem to have an allmulti mode, so we go
1705 	 * into promiscuous mode and let the upper levels deal. */
1706 	if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
1707 	    (netdev_mc_count(dev) > MAX_MULTICAST(priv))) {
1708 		promisc = 1;
1709 		mc_count = 0;
1710 	} else {
1711 		promisc = 0;
1712 		mc_count = netdev_mc_count(dev);
1713 	}
1714 
1715 	err = __orinoco_hw_set_multicast_list(priv, dev, mc_count, promisc);
1716 
1717 	return err;
1718 }
1719 
1720 /* This must be called from user context, without locks held - use
1721  * schedule_work() */
1722 void orinoco_reset(struct work_struct *work)
1723 {
1724 	struct orinoco_private *priv =
1725 		container_of(work, struct orinoco_private, reset_work);
1726 	struct net_device *dev = priv->ndev;
1727 	struct hermes *hw = &priv->hw;
1728 	int err;
1729 	unsigned long flags;
1730 
1731 	if (orinoco_lock(priv, &flags) != 0)
1732 		/* When the hardware becomes available again, whatever
1733 		 * detects that is responsible for re-initializing
1734 		 * it. So no need for anything further */
1735 		return;
1736 
1737 	netif_stop_queue(dev);
1738 
1739 	/* Shut off interrupts.  Depending on what state the hardware
1740 	 * is in, this might not work, but we'll try anyway */
1741 	hermes_set_irqmask(hw, 0);
1742 	hermes_write_regn(hw, EVACK, 0xffff);
1743 
1744 	priv->hw_unavailable++;
1745 	priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
1746 	netif_carrier_off(dev);
1747 
1748 	orinoco_unlock(priv, &flags);
1749 
1750 	/* Scanning support: Notify scan cancellation */
1751 	orinoco_scan_done(priv, true);
1752 
1753 	if (priv->hard_reset) {
1754 		err = (*priv->hard_reset)(priv);
1755 		if (err) {
1756 			printk(KERN_ERR "%s: orinoco_reset: Error %d "
1757 			       "performing hard reset\n", dev->name, err);
1758 			goto disable;
1759 		}
1760 	}
1761 
1762 	err = orinoco_reinit_firmware(priv);
1763 	if (err) {
1764 		printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
1765 		       dev->name, err);
1766 		goto disable;
1767 	}
1768 
1769 	/* This has to be called from user context */
1770 	orinoco_lock_irq(priv);
1771 
1772 	priv->hw_unavailable--;
1773 
1774 	/* priv->open or priv->hw_unavailable might have changed while
1775 	 * we dropped the lock */
1776 	if (priv->open && (!priv->hw_unavailable)) {
1777 		err = __orinoco_up(priv);
1778 		if (err) {
1779 			printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
1780 			       dev->name, err);
1781 		} else
1782 			netif_trans_update(dev);
1783 	}
1784 
1785 	orinoco_unlock_irq(priv);
1786 
1787 	return;
1788  disable:
1789 	hermes_set_irqmask(hw, 0);
1790 	netif_device_detach(dev);
1791 	printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
1792 }
1793 
1794 static int __orinoco_commit(struct orinoco_private *priv)
1795 {
1796 	struct net_device *dev = priv->ndev;
1797 	int err = 0;
1798 
1799 	/* If we've called commit, we are reconfiguring or bringing the
1800 	 * interface up. Maintaining countermeasures across this would
1801 	 * be confusing, so note that we've disabled them. The port will
1802 	 * be enabled later in orinoco_commit or __orinoco_up. */
1803 	priv->tkip_cm_active = 0;
1804 
1805 	err = orinoco_hw_program_rids(priv);
1806 
1807 	/* FIXME: what about netif_tx_lock */
1808 	(void) __orinoco_set_multicast_list(dev);
1809 
1810 	return err;
1811 }
1812 
1813 /* Ensures configuration changes are applied. May result in a reset.
1814  * The caller should hold priv->lock
1815  */
1816 int orinoco_commit(struct orinoco_private *priv)
1817 {
1818 	struct net_device *dev = priv->ndev;
1819 	struct hermes *hw = &priv->hw;
1820 	int err;
1821 
1822 	if (priv->broken_disableport) {
1823 		schedule_work(&priv->reset_work);
1824 		return 0;
1825 	}
1826 
1827 	err = hermes_disable_port(hw, 0);
1828 	if (err) {
1829 		printk(KERN_WARNING "%s: Unable to disable port "
1830 		       "while reconfiguring card\n", dev->name);
1831 		priv->broken_disableport = 1;
1832 		goto out;
1833 	}
1834 
1835 	err = __orinoco_commit(priv);
1836 	if (err) {
1837 		printk(KERN_WARNING "%s: Unable to reconfigure card\n",
1838 		       dev->name);
1839 		goto out;
1840 	}
1841 
1842 	err = hermes_enable_port(hw, 0);
1843 	if (err) {
1844 		printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
1845 		       dev->name);
1846 		goto out;
1847 	}
1848 
1849  out:
1850 	if (err) {
1851 		printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
1852 		schedule_work(&priv->reset_work);
1853 		err = 0;
1854 	}
1855 	return err;
1856 }
1857 
1858 /********************************************************************/
1859 /* Interrupt handler                                                */
1860 /********************************************************************/
1861 
1862 static void __orinoco_ev_tick(struct net_device *dev, struct hermes *hw)
1863 {
1864 	printk(KERN_DEBUG "%s: TICK\n", dev->name);
1865 }
1866 
1867 static void __orinoco_ev_wterr(struct net_device *dev, struct hermes *hw)
1868 {
1869 	/* This seems to happen a fair bit under load, but ignoring it
1870 	   seems to work fine...*/
1871 	printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
1872 	       dev->name);
1873 }
1874 
1875 irqreturn_t orinoco_interrupt(int irq, void *dev_id)
1876 {
1877 	struct orinoco_private *priv = dev_id;
1878 	struct net_device *dev = priv->ndev;
1879 	struct hermes *hw = &priv->hw;
1880 	int count = MAX_IRQLOOPS_PER_IRQ;
1881 	u16 evstat, events;
1882 	/* These are used to detect a runaway interrupt situation.
1883 	 *
1884 	 * If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
1885 	 * we panic and shut down the hardware
1886 	 */
1887 	/* jiffies value the last time we were called */
1888 	static int last_irq_jiffy; /* = 0 */
1889 	static int loops_this_jiffy; /* = 0 */
1890 	unsigned long flags;
1891 
1892 	if (orinoco_lock(priv, &flags) != 0) {
1893 		/* If hw is unavailable - we don't know if the irq was
1894 		 * for us or not */
1895 		return IRQ_HANDLED;
1896 	}
1897 
1898 	evstat = hermes_read_regn(hw, EVSTAT);
1899 	events = evstat & hw->inten;
1900 	if (!events) {
1901 		orinoco_unlock(priv, &flags);
1902 		return IRQ_NONE;
1903 	}
1904 
1905 	if (jiffies != last_irq_jiffy)
1906 		loops_this_jiffy = 0;
1907 	last_irq_jiffy = jiffies;
1908 
1909 	while (events && count--) {
1910 		if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
1911 			printk(KERN_WARNING "%s: IRQ handler is looping too "
1912 			       "much! Resetting.\n", dev->name);
1913 			/* Disable interrupts for now */
1914 			hermes_set_irqmask(hw, 0);
1915 			schedule_work(&priv->reset_work);
1916 			break;
1917 		}
1918 
1919 		/* Check the card hasn't been removed */
1920 		if (!hermes_present(hw)) {
1921 			DEBUG(0, "orinoco_interrupt(): card removed\n");
1922 			break;
1923 		}
1924 
1925 		if (events & HERMES_EV_TICK)
1926 			__orinoco_ev_tick(dev, hw);
1927 		if (events & HERMES_EV_WTERR)
1928 			__orinoco_ev_wterr(dev, hw);
1929 		if (events & HERMES_EV_INFDROP)
1930 			__orinoco_ev_infdrop(dev, hw);
1931 		if (events & HERMES_EV_INFO)
1932 			__orinoco_ev_info(dev, hw);
1933 		if (events & HERMES_EV_RX)
1934 			__orinoco_ev_rx(dev, hw);
1935 		if (events & HERMES_EV_TXEXC)
1936 			__orinoco_ev_txexc(dev, hw);
1937 		if (events & HERMES_EV_TX)
1938 			__orinoco_ev_tx(dev, hw);
1939 		if (events & HERMES_EV_ALLOC)
1940 			__orinoco_ev_alloc(dev, hw);
1941 
1942 		hermes_write_regn(hw, EVACK, evstat);
1943 
1944 		evstat = hermes_read_regn(hw, EVSTAT);
1945 		events = evstat & hw->inten;
1946 	}
1947 
1948 	orinoco_unlock(priv, &flags);
1949 	return IRQ_HANDLED;
1950 }
1951 EXPORT_SYMBOL(orinoco_interrupt);
1952 
1953 /********************************************************************/
1954 /* Power management                                                 */
1955 /********************************************************************/
1956 #if defined(CONFIG_PM_SLEEP) && !defined(CONFIG_HERMES_CACHE_FW_ON_INIT)
1957 static int orinoco_pm_notifier(struct notifier_block *notifier,
1958 			       unsigned long pm_event,
1959 			       void *unused)
1960 {
1961 	struct orinoco_private *priv = container_of(notifier,
1962 						    struct orinoco_private,
1963 						    pm_notifier);
1964 
1965 	/* All we need to do is cache the firmware before suspend, and
1966 	 * release it when we come out.
1967 	 *
1968 	 * Only need to do this if we're downloading firmware. */
1969 	if (!priv->do_fw_download)
1970 		return NOTIFY_DONE;
1971 
1972 	switch (pm_event) {
1973 	case PM_HIBERNATION_PREPARE:
1974 	case PM_SUSPEND_PREPARE:
1975 		orinoco_cache_fw(priv, 0);
1976 		break;
1977 
1978 	case PM_POST_RESTORE:
1979 		/* Restore from hibernation failed. We need to clean
1980 		 * up in exactly the same way, so fall through. */
1981 	case PM_POST_HIBERNATION:
1982 	case PM_POST_SUSPEND:
1983 		orinoco_uncache_fw(priv);
1984 		break;
1985 
1986 	case PM_RESTORE_PREPARE:
1987 	default:
1988 		break;
1989 	}
1990 
1991 	return NOTIFY_DONE;
1992 }
1993 
1994 static void orinoco_register_pm_notifier(struct orinoco_private *priv)
1995 {
1996 	priv->pm_notifier.notifier_call = orinoco_pm_notifier;
1997 	register_pm_notifier(&priv->pm_notifier);
1998 }
1999 
2000 static void orinoco_unregister_pm_notifier(struct orinoco_private *priv)
2001 {
2002 	unregister_pm_notifier(&priv->pm_notifier);
2003 }
2004 #else /* !PM_SLEEP || HERMES_CACHE_FW_ON_INIT */
2005 #define orinoco_register_pm_notifier(priv) do { } while (0)
2006 #define orinoco_unregister_pm_notifier(priv) do { } while (0)
2007 #endif
2008 
2009 /********************************************************************/
2010 /* Initialization                                                   */
2011 /********************************************************************/
2012 
2013 int orinoco_init(struct orinoco_private *priv)
2014 {
2015 	struct device *dev = priv->dev;
2016 	struct wiphy *wiphy = priv_to_wiphy(priv);
2017 	struct hermes *hw = &priv->hw;
2018 	int err = 0;
2019 
2020 	/* No need to lock, the hw_unavailable flag is already set in
2021 	 * alloc_orinocodev() */
2022 	priv->nicbuf_size = IEEE80211_MAX_FRAME_LEN + ETH_HLEN;
2023 
2024 	/* Initialize the firmware */
2025 	err = hw->ops->init(hw);
2026 	if (err != 0) {
2027 		dev_err(dev, "Failed to initialize firmware (err = %d)\n",
2028 			err);
2029 		goto out;
2030 	}
2031 
2032 	err = determine_fw_capabilities(priv, wiphy->fw_version,
2033 					sizeof(wiphy->fw_version),
2034 					&wiphy->hw_version);
2035 	if (err != 0) {
2036 		dev_err(dev, "Incompatible firmware, aborting\n");
2037 		goto out;
2038 	}
2039 
2040 	if (priv->do_fw_download) {
2041 #ifdef CONFIG_HERMES_CACHE_FW_ON_INIT
2042 		orinoco_cache_fw(priv, 0);
2043 #endif
2044 
2045 		err = orinoco_download(priv);
2046 		if (err)
2047 			priv->do_fw_download = 0;
2048 
2049 		/* Check firmware version again */
2050 		err = determine_fw_capabilities(priv, wiphy->fw_version,
2051 						sizeof(wiphy->fw_version),
2052 						&wiphy->hw_version);
2053 		if (err != 0) {
2054 			dev_err(dev, "Incompatible firmware, aborting\n");
2055 			goto out;
2056 		}
2057 	}
2058 
2059 	if (priv->has_port3)
2060 		dev_info(dev, "Ad-hoc demo mode supported\n");
2061 	if (priv->has_ibss)
2062 		dev_info(dev, "IEEE standard IBSS ad-hoc mode supported\n");
2063 	if (priv->has_wep)
2064 		dev_info(dev, "WEP supported, %s-bit key\n",
2065 			 priv->has_big_wep ? "104" : "40");
2066 	if (priv->has_wpa) {
2067 		dev_info(dev, "WPA-PSK supported\n");
2068 		if (orinoco_mic_init(priv)) {
2069 			dev_err(dev, "Failed to setup MIC crypto algorithm. "
2070 				"Disabling WPA support\n");
2071 			priv->has_wpa = 0;
2072 		}
2073 	}
2074 
2075 	err = orinoco_hw_read_card_settings(priv, wiphy->perm_addr);
2076 	if (err)
2077 		goto out;
2078 
2079 	err = orinoco_hw_allocate_fid(priv);
2080 	if (err) {
2081 		dev_err(dev, "Failed to allocate NIC buffer!\n");
2082 		goto out;
2083 	}
2084 
2085 	/* Set up the default configuration */
2086 	priv->iw_mode = NL80211_IFTYPE_STATION;
2087 	/* By default use IEEE/IBSS ad-hoc mode if we have it */
2088 	priv->prefer_port3 = priv->has_port3 && (!priv->has_ibss);
2089 	set_port_type(priv);
2090 	priv->channel = 0; /* use firmware default */
2091 
2092 	priv->promiscuous = 0;
2093 	priv->encode_alg = ORINOCO_ALG_NONE;
2094 	priv->tx_key = 0;
2095 	priv->wpa_enabled = 0;
2096 	priv->tkip_cm_active = 0;
2097 	priv->key_mgmt = 0;
2098 	priv->wpa_ie_len = 0;
2099 	priv->wpa_ie = NULL;
2100 
2101 	if (orinoco_wiphy_register(wiphy)) {
2102 		err = -ENODEV;
2103 		goto out;
2104 	}
2105 
2106 	/* Make the hardware available, as long as it hasn't been
2107 	 * removed elsewhere (e.g. by PCMCIA hot unplug) */
2108 	orinoco_lock_irq(priv);
2109 	priv->hw_unavailable--;
2110 	orinoco_unlock_irq(priv);
2111 
2112 	dev_dbg(dev, "Ready\n");
2113 
2114  out:
2115 	return err;
2116 }
2117 EXPORT_SYMBOL(orinoco_init);
2118 
2119 static const struct net_device_ops orinoco_netdev_ops = {
2120 	.ndo_open		= orinoco_open,
2121 	.ndo_stop		= orinoco_stop,
2122 	.ndo_start_xmit		= orinoco_xmit,
2123 	.ndo_set_rx_mode	= orinoco_set_multicast_list,
2124 	.ndo_change_mtu		= orinoco_change_mtu,
2125 	.ndo_set_mac_address	= eth_mac_addr,
2126 	.ndo_validate_addr	= eth_validate_addr,
2127 	.ndo_tx_timeout		= orinoco_tx_timeout,
2128 };
2129 
2130 /* Allocate private data.
2131  *
2132  * This driver has a number of structures associated with it
2133  *  netdev - Net device structure for each network interface
2134  *  wiphy - structure associated with wireless phy
2135  *  wireless_dev (wdev) - structure for each wireless interface
2136  *  hw - structure for hermes chip info
2137  *  card - card specific structure for use by the card driver
2138  *         (airport, orinoco_cs)
2139  *  priv - orinoco private data
2140  *  device - generic linux device structure
2141  *
2142  *  +---------+    +---------+
2143  *  |  wiphy  |    | netdev  |
2144  *  | +-------+    | +-------+
2145  *  | | priv  |    | | wdev  |
2146  *  | | +-----+    +-+-------+
2147  *  | | | hw  |
2148  *  | +-+-----+
2149  *  | | card  |
2150  *  +-+-------+
2151  *
2152  * priv has a link to netdev and device
2153  * wdev has a link to wiphy
2154  */
2155 struct orinoco_private
2156 *alloc_orinocodev(int sizeof_card,
2157 		  struct device *device,
2158 		  int (*hard_reset)(struct orinoco_private *),
2159 		  int (*stop_fw)(struct orinoco_private *, int))
2160 {
2161 	struct orinoco_private *priv;
2162 	struct wiphy *wiphy;
2163 
2164 	/* allocate wiphy
2165 	 * NOTE: We only support a single virtual interface
2166 	 *       but this may change when monitor mode is added
2167 	 */
2168 	wiphy = wiphy_new(&orinoco_cfg_ops,
2169 			  sizeof(struct orinoco_private) + sizeof_card);
2170 	if (!wiphy)
2171 		return NULL;
2172 
2173 	priv = wiphy_priv(wiphy);
2174 	priv->dev = device;
2175 
2176 	if (sizeof_card)
2177 		priv->card = (void *)((unsigned long)priv
2178 				      + sizeof(struct orinoco_private));
2179 	else
2180 		priv->card = NULL;
2181 
2182 	orinoco_wiphy_init(wiphy);
2183 
2184 #ifdef WIRELESS_SPY
2185 	priv->wireless_data.spy_data = &priv->spy_data;
2186 #endif
2187 
2188 	/* Set up default callbacks */
2189 	priv->hard_reset = hard_reset;
2190 	priv->stop_fw = stop_fw;
2191 
2192 	spin_lock_init(&priv->lock);
2193 	priv->open = 0;
2194 	priv->hw_unavailable = 1; /* orinoco_init() must clear this
2195 				   * before anything else touches the
2196 				   * hardware */
2197 	INIT_WORK(&priv->reset_work, orinoco_reset);
2198 	INIT_WORK(&priv->join_work, orinoco_join_ap);
2199 	INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
2200 
2201 	INIT_LIST_HEAD(&priv->rx_list);
2202 	tasklet_init(&priv->rx_tasklet, orinoco_rx_isr_tasklet,
2203 		     (unsigned long) priv);
2204 
2205 	spin_lock_init(&priv->scan_lock);
2206 	INIT_LIST_HEAD(&priv->scan_list);
2207 	INIT_WORK(&priv->process_scan, orinoco_process_scan_results);
2208 
2209 	priv->last_linkstatus = 0xffff;
2210 
2211 #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
2212 	priv->cached_pri_fw = NULL;
2213 	priv->cached_fw = NULL;
2214 #endif
2215 
2216 	/* Register PM notifiers */
2217 	orinoco_register_pm_notifier(priv);
2218 
2219 	return priv;
2220 }
2221 EXPORT_SYMBOL(alloc_orinocodev);
2222 
2223 /* We can only support a single interface. We provide a separate
2224  * function to set it up to distinguish between hardware
2225  * initialisation and interface setup.
2226  *
2227  * The base_addr and irq parameters are passed on to netdev for use
2228  * with SIOCGIFMAP.
2229  */
2230 int orinoco_if_add(struct orinoco_private *priv,
2231 		   unsigned long base_addr,
2232 		   unsigned int irq,
2233 		   const struct net_device_ops *ops)
2234 {
2235 	struct wiphy *wiphy = priv_to_wiphy(priv);
2236 	struct wireless_dev *wdev;
2237 	struct net_device *dev;
2238 	int ret;
2239 
2240 	dev = alloc_etherdev(sizeof(struct wireless_dev));
2241 
2242 	if (!dev)
2243 		return -ENOMEM;
2244 
2245 	/* Initialise wireless_dev */
2246 	wdev = netdev_priv(dev);
2247 	wdev->wiphy = wiphy;
2248 	wdev->iftype = NL80211_IFTYPE_STATION;
2249 
2250 	/* Setup / override net_device fields */
2251 	dev->ieee80211_ptr = wdev;
2252 	dev->watchdog_timeo = HZ; /* 1 second timeout */
2253 	dev->wireless_handlers = &orinoco_handler_def;
2254 #ifdef WIRELESS_SPY
2255 	dev->wireless_data = &priv->wireless_data;
2256 #endif
2257 	/* Default to standard ops if not set */
2258 	if (ops)
2259 		dev->netdev_ops = ops;
2260 	else
2261 		dev->netdev_ops = &orinoco_netdev_ops;
2262 
2263 	/* we use the default eth_mac_addr for setting the MAC addr */
2264 
2265 	/* Reserve space in skb for the SNAP header */
2266 	dev->needed_headroom = ENCAPS_OVERHEAD;
2267 
2268 	netif_carrier_off(dev);
2269 
2270 	memcpy(dev->dev_addr, wiphy->perm_addr, ETH_ALEN);
2271 
2272 	dev->base_addr = base_addr;
2273 	dev->irq = irq;
2274 
2275 	dev->min_mtu = ORINOCO_MIN_MTU;
2276 	dev->max_mtu = ORINOCO_MAX_MTU;
2277 
2278 	SET_NETDEV_DEV(dev, priv->dev);
2279 	ret = register_netdev(dev);
2280 	if (ret)
2281 		goto fail;
2282 
2283 	priv->ndev = dev;
2284 
2285 	/* Report what we've done */
2286 	dev_dbg(priv->dev, "Registerred interface %s.\n", dev->name);
2287 
2288 	return 0;
2289 
2290  fail:
2291 	free_netdev(dev);
2292 	return ret;
2293 }
2294 EXPORT_SYMBOL(orinoco_if_add);
2295 
2296 void orinoco_if_del(struct orinoco_private *priv)
2297 {
2298 	struct net_device *dev = priv->ndev;
2299 
2300 	unregister_netdev(dev);
2301 	free_netdev(dev);
2302 }
2303 EXPORT_SYMBOL(orinoco_if_del);
2304 
2305 void free_orinocodev(struct orinoco_private *priv)
2306 {
2307 	struct wiphy *wiphy = priv_to_wiphy(priv);
2308 	struct orinoco_rx_data *rx_data, *temp;
2309 	struct orinoco_scan_data *sd, *sdtemp;
2310 
2311 	/* If the tasklet is scheduled when we call tasklet_kill it
2312 	 * will run one final time. However the tasklet will only
2313 	 * drain priv->rx_list if the hw is still available. */
2314 	tasklet_kill(&priv->rx_tasklet);
2315 
2316 	/* Explicitly drain priv->rx_list */
2317 	list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
2318 		list_del(&rx_data->list);
2319 
2320 		dev_kfree_skb(rx_data->skb);
2321 		kfree(rx_data->desc);
2322 		kfree(rx_data);
2323 	}
2324 
2325 	cancel_work_sync(&priv->process_scan);
2326 	/* Explicitly drain priv->scan_list */
2327 	list_for_each_entry_safe(sd, sdtemp, &priv->scan_list, list) {
2328 		list_del(&sd->list);
2329 
2330 		if (sd->len > 0)
2331 			kfree(sd->buf);
2332 		kfree(sd);
2333 	}
2334 
2335 	orinoco_unregister_pm_notifier(priv);
2336 	orinoco_uncache_fw(priv);
2337 
2338 	priv->wpa_ie_len = 0;
2339 	kfree(priv->wpa_ie);
2340 	orinoco_mic_free(priv);
2341 	wiphy_free(wiphy);
2342 }
2343 EXPORT_SYMBOL(free_orinocodev);
2344 
2345 int orinoco_up(struct orinoco_private *priv)
2346 {
2347 	struct net_device *dev = priv->ndev;
2348 	unsigned long flags;
2349 	int err;
2350 
2351 	priv->hw.ops->lock_irqsave(&priv->lock, &flags);
2352 
2353 	err = orinoco_reinit_firmware(priv);
2354 	if (err) {
2355 		printk(KERN_ERR "%s: Error %d re-initializing firmware\n",
2356 		       dev->name, err);
2357 		goto exit;
2358 	}
2359 
2360 	netif_device_attach(dev);
2361 	priv->hw_unavailable--;
2362 
2363 	if (priv->open && !priv->hw_unavailable) {
2364 		err = __orinoco_up(priv);
2365 		if (err)
2366 			printk(KERN_ERR "%s: Error %d restarting card\n",
2367 			       dev->name, err);
2368 	}
2369 
2370 exit:
2371 	priv->hw.ops->unlock_irqrestore(&priv->lock, &flags);
2372 
2373 	return 0;
2374 }
2375 EXPORT_SYMBOL(orinoco_up);
2376 
2377 void orinoco_down(struct orinoco_private *priv)
2378 {
2379 	struct net_device *dev = priv->ndev;
2380 	unsigned long flags;
2381 	int err;
2382 
2383 	priv->hw.ops->lock_irqsave(&priv->lock, &flags);
2384 	err = __orinoco_down(priv);
2385 	if (err)
2386 		printk(KERN_WARNING "%s: Error %d downing interface\n",
2387 		       dev->name, err);
2388 
2389 	netif_device_detach(dev);
2390 	priv->hw_unavailable++;
2391 	priv->hw.ops->unlock_irqrestore(&priv->lock, &flags);
2392 }
2393 EXPORT_SYMBOL(orinoco_down);
2394 
2395 /********************************************************************/
2396 /* Module initialization                                            */
2397 /********************************************************************/
2398 
2399 /* Can't be declared "const" or the whole __initdata section will
2400  * become const */
2401 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
2402 	" (David Gibson <hermes@gibson.dropbear.id.au>, "
2403 	"Pavel Roskin <proski@gnu.org>, et al)";
2404 
2405 static int __init init_orinoco(void)
2406 {
2407 	printk(KERN_DEBUG "%s\n", version);
2408 	return 0;
2409 }
2410 
2411 static void __exit exit_orinoco(void)
2412 {
2413 }
2414 
2415 module_init(init_orinoco);
2416 module_exit(exit_orinoco);
2417