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