blob: cd1c04d42b6a083800a00ea49a128a8bf1ecbb8a [file] [log] [blame]
David Kilroy47445cb2009-02-04 23:05:48 +00001/* main.c - (formerly known as dldwd_cs.c, orinoco_cs.c and orinoco.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * A driver for Hermes or Prism 2 chipset based PCMCIA wireless
4 * adaptors, with Lucent/Agere, Intersil or Symbol firmware.
5 *
6 * Current maintainers (as of 29 September 2003) are:
7 * Pavel Roskin <proski AT gnu.org>
8 * and David Gibson <hermes AT gibson.dropbear.id.au>
9 *
10 * (C) Copyright David Gibson, IBM Corporation 2001-2003.
11 * Copyright (C) 2000 David Gibson, Linuxcare Australia.
12 * With some help from :
13 * Copyright (C) 2001 Jean Tourrilhes, HP Labs
14 * Copyright (C) 2001 Benjamin Herrenschmidt
15 *
16 * Based on dummy_cs.c 1.27 2000/06/12 21:27:25
17 *
18 * Portions based on wvlan_cs.c 1.0.6, Copyright Andreas Neuhaus <andy
19 * AT fasta.fh-dortmund.de>
20 * http://www.stud.fh-dortmund.de/~andy/wvlan/
21 *
22 * The contents of this file are subject to the Mozilla Public License
23 * Version 1.1 (the "License"); you may not use this file except in
24 * compliance with the License. You may obtain a copy of the License
25 * at http://www.mozilla.org/MPL/
26 *
27 * Software distributed under the License is distributed on an "AS IS"
28 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
29 * the License for the specific language governing rights and
30 * limitations under the License.
31 *
32 * The initial developer of the original code is David A. Hinds
33 * <dahinds AT users.sourceforge.net>. Portions created by David
34 * A. Hinds are Copyright (C) 1999 David A. Hinds. All Rights
35 * Reserved.
36 *
37 * Alternatively, the contents of this file may be used under the
38 * terms of the GNU General Public License version 2 (the "GPL"), in
39 * which case the provisions of the GPL are applicable instead of the
40 * above. If you wish to allow the use of your version of this file
41 * only under the terms of the GPL and not to allow others to use your
42 * version of this file under the MPL, indicate your decision by
43 * deleting the provisions above and replace them with the notice and
44 * other provisions required by the GPL. If you do not delete the
45 * provisions above, a recipient may use your version of this file
46 * under either the MPL or the GPL. */
47
48/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * TODO
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * o Handle de-encapsulation within network layer, provide 802.11
51 * headers (patch from Thomas 'Dent' Mirlacher)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * o Fix possible races in SPY handling.
53 * o Disconnect wireless extensions from fundamental configuration.
54 * o (maybe) Software WEP support (patch from Stano Meduna).
55 * o (maybe) Use multiple Tx buffers - driver handling queue
56 * rather than firmware.
57 */
58
59/* Locking and synchronization:
60 *
61 * The basic principle is that everything is serialized through a
62 * single spinlock, priv->lock. The lock is used in user, bh and irq
63 * context, so when taken outside hardirq context it should always be
64 * taken with interrupts disabled. The lock protects both the
65 * hardware and the struct orinoco_private.
66 *
67 * Another flag, priv->hw_unavailable indicates that the hardware is
68 * unavailable for an extended period of time (e.g. suspended, or in
69 * the middle of a hard reset). This flag is protected by the
70 * spinlock. All code which touches the hardware should check the
71 * flag after taking the lock, and if it is set, give up on whatever
72 * they are doing and drop the lock again. The orinoco_lock()
73 * function handles this (it unlocks and returns -EBUSY if
74 * hw_unavailable is non-zero).
75 */
76
77#define DRIVER_NAME "orinoco"
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/module.h>
80#include <linux/kernel.h>
81#include <linux/init.h>
David Kilroyd03032a2008-08-21 23:28:02 +010082#include <linux/delay.h>
David Kilroy8e638262009-06-18 23:21:24 +010083#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#include <linux/etherdevice.h>
Christoph Hellwig1fab2e82005-06-19 01:27:40 +020086#include <linux/ethtool.h>
David Kilroy39d1ffe2008-11-22 10:37:28 +000087#include <linux/suspend.h>
Pavel Roskin9c974fb2006-08-15 20:45:03 -040088#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <linux/wireless.h>
Johannes Berg2c7060022008-10-30 22:09:54 +010090#include <linux/ieee80211.h>
Christoph Hellwig620554e2005-06-19 01:27:33 +020091#include <net/iw_handler.h>
David Kilroyea60a6a2009-06-18 23:21:26 +010092#include <net/cfg80211.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#include "hermes_rid.h"
David Kilroy3994d502008-08-21 23:27:54 +010095#include "hermes_dld.h"
David Kilroy712a4342009-02-04 23:05:55 +000096#include "hw.h"
David Kilroyfb791b12009-02-04 23:05:50 +000097#include "scan.h"
David Kilroy4adb4742009-02-04 23:05:51 +000098#include "mic.h"
David Kilroy37a2e562009-02-04 23:05:52 +000099#include "fw.h"
David Kilroycb1576a2009-02-04 23:05:56 +0000100#include "wext.h"
David Kilroyea60a6a2009-06-18 23:21:26 +0100101#include "cfg.h"
David Kilroycb1576a2009-02-04 23:05:56 +0000102#include "main.h"
David Kilroyfb791b12009-02-04 23:05:50 +0000103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#include "orinoco.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/********************************************************************/
107/* Module information */
108/********************************************************************/
109
David Kilroyb2f30a02009-02-04 23:05:46 +0000110MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & "
111 "David Gibson <hermes@gibson.dropbear.id.au>");
112MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based "
113 "and similar wireless cards");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114MODULE_LICENSE("Dual MPL/GPL");
115
116/* Level of debugging. Used in the macros in orinoco.h */
117#ifdef ORINOCO_DEBUG
118int orinoco_debug = ORINOCO_DEBUG;
David Kilroy21312662009-02-04 23:05:47 +0000119EXPORT_SYMBOL(orinoco_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120module_param(orinoco_debug, int, 0644);
121MODULE_PARM_DESC(orinoco_debug, "Debug level");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#endif
123
124static int suppress_linkstatus; /* = 0 */
125module_param(suppress_linkstatus, bool, 0644);
126MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
David Kilroyb2f30a02009-02-04 23:05:46 +0000127
David Gibson7bb7c3a2005-05-12 20:02:10 -0400128static int ignore_disconnect; /* = 0 */
129module_param(ignore_disconnect, int, 0644);
David Kilroyb2f30a02009-02-04 23:05:46 +0000130MODULE_PARM_DESC(ignore_disconnect,
131 "Don't report lost link to the network layer");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
David Kilroycb1576a2009-02-04 23:05:56 +0000133int force_monitor; /* = 0 */
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200134module_param(force_monitor, int, 0644);
135MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/* Internal constants */
139/********************************************************************/
140
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200141/* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
142static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
143#define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define ORINOCO_MIN_MTU 256
Johannes Berg2c7060022008-10-30 22:09:54 +0100146#define ORINOCO_MAX_MTU (IEEE80211_MAX_DATA_LEN - ENCAPS_OVERHEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#define MAX_IRQLOOPS_PER_IRQ 10
149#define MAX_IRQLOOPS_PER_JIFFY (20000/HZ) /* Based on a guestimate of
150 * how many events the
151 * device could
152 * legitimately generate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154#define DUMMY_FID 0xFFFF
155
156/*#define MAX_MULTICAST(priv) (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
157 HERMES_MAX_MULTICAST : 0)*/
158#define MAX_MULTICAST(priv) (HERMES_MAX_MULTICAST)
159
160#define ORINOCO_INTEN (HERMES_EV_RX | HERMES_EV_ALLOC \
161 | HERMES_EV_TX | HERMES_EV_TXEXC \
162 | HERMES_EV_WTERR | HERMES_EV_INFO \
David Kilroya94e8422009-02-04 23:05:44 +0000163 | HERMES_EV_INFDROP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Jeff Garzik7282d492006-09-13 14:30:00 -0400165static const struct ethtool_ops orinoco_ethtool_ops;
Christoph Hellwig620554e2005-06-19 01:27:33 +0200166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/* Data types */
169/********************************************************************/
170
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400171/* Beginning of the Tx descriptor, used in TxExc handling */
172struct hermes_txexc_data {
173 struct hermes_tx_descriptor desc;
Pavel Roskind133ae42005-09-23 04:18:06 -0400174 __le16 frame_ctl;
175 __le16 duration_id;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200176 u8 addr1[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177} __attribute__ ((packed));
178
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200179/* Rx frame header except compatibility 802.3 header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180struct hermes_rx_descriptor {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200181 /* Control */
Pavel Roskind133ae42005-09-23 04:18:06 -0400182 __le16 status;
183 __le32 time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 u8 silence;
185 u8 signal;
186 u8 rate;
187 u8 rxflow;
Pavel Roskind133ae42005-09-23 04:18:06 -0400188 __le32 reserved;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200189
190 /* 802.11 header */
Pavel Roskind133ae42005-09-23 04:18:06 -0400191 __le16 frame_ctl;
192 __le16 duration_id;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200193 u8 addr1[ETH_ALEN];
194 u8 addr2[ETH_ALEN];
195 u8 addr3[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -0400196 __le16 seq_ctl;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200197 u8 addr4[ETH_ALEN];
198
199 /* Data length */
Pavel Roskind133ae42005-09-23 04:18:06 -0400200 __le16 data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201} __attribute__ ((packed));
202
David Kilroy47166792009-01-07 00:43:54 +0000203struct orinoco_rx_data {
204 struct hermes_rx_descriptor *desc;
205 struct sk_buff *skb;
206 struct list_head list;
207};
208
David Kilroyc63cdbe2009-06-18 23:21:33 +0100209struct orinoco_scan_data {
210 void *buf;
211 size_t len;
212 int type;
213 struct list_head list;
214};
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/********************************************************************/
217/* Function prototypes */
218/********************************************************************/
219
David Kilroy721aa2f2009-06-18 23:21:31 +0100220static int __orinoco_set_multicast_list(struct net_device *dev);
David Kilroy6415f7d2009-06-18 23:21:30 +0100221static int __orinoco_up(struct orinoco_private *priv);
222static int __orinoco_down(struct orinoco_private *priv);
David Kilroy721aa2f2009-06-18 23:21:31 +0100223static int __orinoco_commit(struct orinoco_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225/********************************************************************/
226/* Internal helper functions */
227/********************************************************************/
228
David Kilroycb1576a2009-02-04 23:05:56 +0000229void set_port_type(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 switch (priv->iw_mode) {
David Kilroy5217c572009-06-18 23:21:32 +0100232 case NL80211_IFTYPE_STATION:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 priv->port_type = 1;
234 priv->createibss = 0;
235 break;
David Kilroy5217c572009-06-18 23:21:32 +0100236 case NL80211_IFTYPE_ADHOC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (priv->prefer_port3) {
238 priv->port_type = 3;
239 priv->createibss = 0;
240 } else {
241 priv->port_type = priv->ibss_port;
242 priv->createibss = 1;
243 }
244 break;
David Kilroy5217c572009-06-18 23:21:32 +0100245 case NL80211_IFTYPE_MONITOR:
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200246 priv->port_type = 3;
247 priv->createibss = 0;
248 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 default:
250 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
251 priv->ndev->name);
252 }
253}
254
David Kilroy3994d502008-08-21 23:27:54 +0100255/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/* Device methods */
257/********************************************************************/
258
259static int orinoco_open(struct net_device *dev)
260{
David Kilroyea60a6a2009-06-18 23:21:26 +0100261 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 unsigned long flags;
263 int err;
264
265 if (orinoco_lock(priv, &flags) != 0)
266 return -EBUSY;
267
David Kilroya2608362009-06-18 23:21:23 +0100268 err = __orinoco_up(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
David Kilroya94e8422009-02-04 23:05:44 +0000270 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 priv->open = 1;
272
273 orinoco_unlock(priv, &flags);
274
275 return err;
276}
277
Christoph Hellwigad8f4512005-05-14 17:30:17 +0200278static int orinoco_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
David Kilroyea60a6a2009-06-18 23:21:26 +0100280 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 int err = 0;
282
283 /* We mustn't use orinoco_lock() here, because we need to be
284 able to close the interface even if hw_unavailable is set
285 (e.g. as we're released after a PC Card removal) */
286 spin_lock_irq(&priv->lock);
287
288 priv->open = 0;
289
David Kilroya2608362009-06-18 23:21:23 +0100290 err = __orinoco_down(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 spin_unlock_irq(&priv->lock);
293
294 return err;
295}
296
297static struct net_device_stats *orinoco_get_stats(struct net_device *dev)
298{
David Kilroyea60a6a2009-06-18 23:21:26 +0100299 struct orinoco_private *priv = ndev_priv(dev);
David Kilroy6fe9deb2009-02-04 23:05:43 +0000300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return &priv->stats;
302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304static void orinoco_set_multicast_list(struct net_device *dev)
305{
David Kilroyea60a6a2009-06-18 23:21:26 +0100306 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 unsigned long flags;
308
309 if (orinoco_lock(priv, &flags) != 0) {
310 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
311 "called when hw_unavailable\n", dev->name);
312 return;
313 }
314
315 __orinoco_set_multicast_list(dev);
316 orinoco_unlock(priv, &flags);
317}
318
319static int orinoco_change_mtu(struct net_device *dev, int new_mtu)
320{
David Kilroyea60a6a2009-06-18 23:21:26 +0100321 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
David Kilroya94e8422009-02-04 23:05:44 +0000323 if ((new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return -EINVAL;
325
Johannes Berg2c7060022008-10-30 22:09:54 +0100326 /* MTU + encapsulation + header length */
David Kilroya94e8422009-02-04 23:05:44 +0000327 if ((new_mtu + ENCAPS_OVERHEAD + sizeof(struct ieee80211_hdr)) >
328 (priv->nicbuf_size - ETH_HLEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return -EINVAL;
330
331 dev->mtu = new_mtu;
332
333 return 0;
334}
335
336/********************************************************************/
337/* Tx path */
338/********************************************************************/
339
340static int orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
341{
David Kilroyea60a6a2009-06-18 23:21:26 +0100342 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 struct net_device_stats *stats = &priv->stats;
344 hermes_t *hw = &priv->hw;
345 int err = 0;
346 u16 txfid = priv->txfid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct ethhdr *eh;
David Kilroy6eecad72008-08-21 23:27:56 +0100348 int tx_control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 unsigned long flags;
350
David Kilroya94e8422009-02-04 23:05:44 +0000351 if (!netif_running(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 printk(KERN_ERR "%s: Tx on stopped device!\n",
353 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400354 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
David Kilroy6fe9deb2009-02-04 23:05:43 +0000356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (netif_queue_stopped(dev)) {
David Kilroy6fe9deb2009-02-04 23:05:43 +0000358 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400360 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
David Kilroy6fe9deb2009-02-04 23:05:43 +0000362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (orinoco_lock(priv, &flags) != 0) {
364 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
365 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400366 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368
David Kilroy5217c572009-06-18 23:21:32 +0100369 if (!netif_carrier_ok(dev) ||
370 (priv->iw_mode == NL80211_IFTYPE_MONITOR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 /* Oops, the firmware hasn't established a connection,
David Kilroy6fe9deb2009-02-04 23:05:43 +0000372 silently drop the packet (this seems to be the
373 safest approach). */
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400374 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376
Pavel Roskin8d5be082006-04-07 04:10:41 -0400377 /* Check packet length */
Pavel Roskina28dc812006-04-07 04:10:45 -0400378 if (skb->len < ETH_HLEN)
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400379 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
David Kilroy6eecad72008-08-21 23:27:56 +0100381 tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
David Kilroy23edcc42008-08-21 23:28:05 +0100383 if (priv->encode_alg == IW_ENCODE_ALG_TKIP)
384 tx_control |= (priv->tx_key << HERMES_MIC_KEY_ID_SHIFT) |
385 HERMES_TXCTRL_MIC;
386
David Kilroy6eecad72008-08-21 23:27:56 +0100387 if (priv->has_alt_txcntl) {
388 /* WPA enabled firmwares have tx_cntl at the end of
389 * the 802.11 header. So write zeroed descriptor and
390 * 802.11 header at the same time
391 */
392 char desc[HERMES_802_3_OFFSET];
393 __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
394
395 memset(&desc, 0, sizeof(desc));
396
397 *txcntl = cpu_to_le16(tx_control);
398 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
399 txfid, 0);
400 if (err) {
401 if (net_ratelimit())
402 printk(KERN_ERR "%s: Error %d writing Tx "
403 "descriptor to BAP\n", dev->name, err);
404 goto busy;
405 }
406 } else {
407 struct hermes_tx_descriptor desc;
408
409 memset(&desc, 0, sizeof(desc));
410
411 desc.tx_control = cpu_to_le16(tx_control);
412 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
413 txfid, 0);
414 if (err) {
415 if (net_ratelimit())
416 printk(KERN_ERR "%s: Error %d writing Tx "
417 "descriptor to BAP\n", dev->name, err);
418 goto busy;
419 }
420
421 /* Clear the 802.11 header and data length fields - some
422 * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
423 * if this isn't done. */
424 hermes_clear_words(hw, HERMES_DATA0,
425 HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
David Kilroy23edcc42008-08-21 23:28:05 +0100428 eh = (struct ethhdr *)skb->data;
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /* Encapsulate Ethernet-II frames */
431 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
Pavel Roskina28dc812006-04-07 04:10:45 -0400432 struct header_struct {
433 struct ethhdr eth; /* 802.3 header */
434 u8 encap[6]; /* 802.2 header */
435 } __attribute__ ((packed)) hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Pavel Roskina28dc812006-04-07 04:10:45 -0400437 /* Strip destination and source from the data */
438 skb_pull(skb, 2 * ETH_ALEN);
Pavel Roskina28dc812006-04-07 04:10:45 -0400439
440 /* And move them to a separate header */
441 memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
442 hdr.eth.h_proto = htons(sizeof(encaps_hdr) + skb->len);
443 memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
444
David Kilroy23edcc42008-08-21 23:28:05 +0100445 /* Insert the SNAP header */
446 if (skb_headroom(skb) < sizeof(hdr)) {
447 printk(KERN_ERR
448 "%s: Not enough headroom for 802.2 headers %d\n",
449 dev->name, skb_headroom(skb));
450 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
David Kilroy23edcc42008-08-21 23:28:05 +0100452 eh = (struct ethhdr *) skb_push(skb, sizeof(hdr));
453 memcpy(eh, &hdr, sizeof(hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
Pavel Roskina28dc812006-04-07 04:10:45 -0400456 err = hermes_bap_pwrite(hw, USER_BAP, skb->data, skb->len,
David Kilroy23edcc42008-08-21 23:28:05 +0100457 txfid, HERMES_802_3_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (err) {
459 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
460 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400461 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463
David Kilroy23edcc42008-08-21 23:28:05 +0100464 /* Calculate Michael MIC */
465 if (priv->encode_alg == IW_ENCODE_ALG_TKIP) {
466 u8 mic_buf[MICHAEL_MIC_LEN + 1];
467 u8 *mic;
468 size_t offset;
469 size_t len;
470
471 if (skb->len % 2) {
472 /* MIC start is on an odd boundary */
473 mic_buf[0] = skb->data[skb->len - 1];
474 mic = &mic_buf[1];
475 offset = skb->len - 1;
476 len = MICHAEL_MIC_LEN + 1;
477 } else {
478 mic = &mic_buf[0];
479 offset = skb->len;
480 len = MICHAEL_MIC_LEN;
481 }
482
David Kilroy4adb4742009-02-04 23:05:51 +0000483 orinoco_mic(priv->tx_tfm_mic,
David Kilroy23edcc42008-08-21 23:28:05 +0100484 priv->tkip_key[priv->tx_key].tx_mic,
485 eh->h_dest, eh->h_source, 0 /* priority */,
486 skb->data + ETH_HLEN, skb->len - ETH_HLEN, mic);
487
488 /* Write the MIC */
489 err = hermes_bap_pwrite(hw, USER_BAP, &mic_buf[0], len,
490 txfid, HERMES_802_3_OFFSET + offset);
491 if (err) {
492 printk(KERN_ERR "%s: Error %d writing MIC to BAP\n",
493 dev->name, err);
494 goto busy;
495 }
496 }
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 /* Finally, we actually initiate the send */
499 netif_stop_queue(dev);
500
501 err = hermes_docmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
502 txfid, NULL);
503 if (err) {
504 netif_start_queue(dev);
Andrew Mortonc367c212005-10-19 21:23:44 -0700505 if (net_ratelimit())
506 printk(KERN_ERR "%s: Error %d transmitting packet\n",
507 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400508 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510
511 dev->trans_start = jiffies;
David Kilroy23edcc42008-08-21 23:28:05 +0100512 stats->tx_bytes += HERMES_802_3_OFFSET + skb->len;
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400513 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400515 drop:
516 stats->tx_errors++;
517 stats->tx_dropped++;
518
519 ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 dev_kfree_skb(skb);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400522 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400524 busy:
Jiri Benc2c1bd262006-04-07 04:10:47 -0400525 if (err == -EIO)
526 schedule_work(&priv->reset_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 orinoco_unlock(priv, &flags);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400528 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
532{
David Kilroyea60a6a2009-06-18 23:21:26 +0100533 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 u16 fid = hermes_read_regn(hw, ALLOCFID);
535
536 if (fid != priv->txfid) {
537 if (fid != DUMMY_FID)
538 printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
539 dev->name, fid);
540 return;
541 }
542
543 hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
544}
545
546static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
547{
David Kilroyea60a6a2009-06-18 23:21:26 +0100548 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 struct net_device_stats *stats = &priv->stats;
550
551 stats->tx_packets++;
552
553 netif_wake_queue(dev);
554
555 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
556}
557
558static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
559{
David Kilroyea60a6a2009-06-18 23:21:26 +0100560 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 struct net_device_stats *stats = &priv->stats;
562 u16 fid = hermes_read_regn(hw, TXCOMPLFID);
Pavel Roskind133ae42005-09-23 04:18:06 -0400563 u16 status;
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400564 struct hermes_txexc_data hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 int err = 0;
566
567 if (fid == DUMMY_FID)
568 return; /* Nothing's really happened */
569
Pavel Roskin48ca7032005-09-23 04:18:06 -0400570 /* Read part of the frame header - we need status and addr1 */
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200571 err = hermes_bap_pread(hw, IRQ_BAP, &hdr,
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400572 sizeof(struct hermes_txexc_data),
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200573 fid, 0);
574
575 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
576 stats->tx_errors++;
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (err) {
579 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
580 "(FID=%04X error %d)\n",
581 dev->name, fid, err);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200582 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
David Kilroy6fe9deb2009-02-04 23:05:43 +0000584
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200585 DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
586 err, fid);
David Kilroy6fe9deb2009-02-04 23:05:43 +0000587
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200588 /* We produce a TXDROP event only for retry or lifetime
589 * exceeded, because that's the only status that really mean
590 * that this particular node went away.
591 * Other errors means that *we* screwed up. - Jean II */
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400592 status = le16_to_cpu(hdr.desc.status);
Pavel Roskind133ae42005-09-23 04:18:06 -0400593 if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200594 union iwreq_data wrqu;
595
596 /* Copy 802.11 dest address.
597 * We use the 802.11 header because the frame may
598 * not be 802.3 or may be mangled...
599 * In Ad-Hoc mode, it will be the node address.
600 * In managed mode, it will be most likely the AP addr
601 * User space will figure out how to convert it to
602 * whatever it needs (IP address or else).
603 * - Jean II */
604 memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
605 wrqu.addr.sa_family = ARPHRD_ETHER;
606
607 /* Send event to user space */
608 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
614static void orinoco_tx_timeout(struct net_device *dev)
615{
David Kilroyea60a6a2009-06-18 23:21:26 +0100616 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 struct net_device_stats *stats = &priv->stats;
618 struct hermes *hw = &priv->hw;
619
620 printk(KERN_WARNING "%s: Tx timeout! "
621 "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
622 dev->name, hermes_read_regn(hw, ALLOCFID),
623 hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
624
625 stats->tx_errors++;
626
627 schedule_work(&priv->reset_work);
628}
629
630/********************************************************************/
631/* Rx path (data frames) */
632/********************************************************************/
633
634/* Does the frame have a SNAP header indicating it should be
635 * de-encapsulated to Ethernet-II? */
636static inline int is_ethersnap(void *_hdr)
637{
638 u8 *hdr = _hdr;
639
640 /* We de-encapsulate all packets which, a) have SNAP headers
641 * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
642 * and where b) the OUI of the SNAP header is 00:00:00 or
643 * 00:00:f8 - we need both because different APs appear to use
644 * different OUIs for some reason */
645 return (memcmp(hdr, &encaps_hdr, 5) == 0)
David Kilroya94e8422009-02-04 23:05:44 +0000646 && ((hdr[5] == 0x00) || (hdr[5] == 0xf8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
649static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
650 int level, int noise)
651{
Pavel Roskin343c6862005-09-09 18:43:02 -0400652 struct iw_quality wstats;
653 wstats.level = level - 0x95;
654 wstats.noise = noise - 0x95;
655 wstats.qual = (level > noise) ? (level - noise) : 0;
Andrey Borzenkovf941f852008-11-15 17:15:09 +0300656 wstats.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
Pavel Roskin343c6862005-09-09 18:43:02 -0400657 /* Update spy records */
658 wireless_spy_update(dev, mac, &wstats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
661static void orinoco_stat_gather(struct net_device *dev,
662 struct sk_buff *skb,
663 struct hermes_rx_descriptor *desc)
664{
David Kilroyea60a6a2009-06-18 23:21:26 +0100665 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 /* Using spy support with lots of Rx packets, like in an
668 * infrastructure (AP), will really slow down everything, because
669 * the MAC address must be compared to each entry of the spy list.
670 * If the user really asks for it (set some address in the
671 * spy list), we do it, but he will pay the price.
672 * Note that to get here, you need both WIRELESS_SPY
673 * compiled in AND some addresses in the list !!!
674 */
675 /* Note : gcc will optimise the whole section away if
676 * WIRELESS_SPY is not defined... - Jean II */
677 if (SPY_NUMBER(priv)) {
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700678 orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 desc->signal, desc->silence);
680 }
681}
682
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200683/*
684 * orinoco_rx_monitor - handle received monitor frames.
685 *
686 * Arguments:
687 * dev network device
688 * rxfid received FID
689 * desc rx descriptor of the frame
690 *
691 * Call context: interrupt
692 */
693static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
694 struct hermes_rx_descriptor *desc)
695{
696 u32 hdrlen = 30; /* return full header by default */
697 u32 datalen = 0;
698 u16 fc;
699 int err;
700 int len;
701 struct sk_buff *skb;
David Kilroyea60a6a2009-06-18 23:21:26 +0100702 struct orinoco_private *priv = ndev_priv(dev);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200703 struct net_device_stats *stats = &priv->stats;
704 hermes_t *hw = &priv->hw;
705
706 len = le16_to_cpu(desc->data_len);
707
708 /* Determine the size of the header and the data */
709 fc = le16_to_cpu(desc->frame_ctl);
710 switch (fc & IEEE80211_FCTL_FTYPE) {
711 case IEEE80211_FTYPE_DATA:
712 if ((fc & IEEE80211_FCTL_TODS)
713 && (fc & IEEE80211_FCTL_FROMDS))
714 hdrlen = 30;
715 else
716 hdrlen = 24;
717 datalen = len;
718 break;
719 case IEEE80211_FTYPE_MGMT:
720 hdrlen = 24;
721 datalen = len;
722 break;
723 case IEEE80211_FTYPE_CTL:
724 switch (fc & IEEE80211_FCTL_STYPE) {
725 case IEEE80211_STYPE_PSPOLL:
726 case IEEE80211_STYPE_RTS:
727 case IEEE80211_STYPE_CFEND:
728 case IEEE80211_STYPE_CFENDACK:
729 hdrlen = 16;
730 break;
731 case IEEE80211_STYPE_CTS:
732 case IEEE80211_STYPE_ACK:
733 hdrlen = 10;
734 break;
735 }
736 break;
737 default:
738 /* Unknown frame type */
739 break;
740 }
741
742 /* sanity check the length */
Johannes Berg2c7060022008-10-30 22:09:54 +0100743 if (datalen > IEEE80211_MAX_DATA_LEN + 12) {
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200744 printk(KERN_DEBUG "%s: oversized monitor frame, "
745 "data length = %d\n", dev->name, datalen);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200746 stats->rx_length_errors++;
747 goto update_stats;
748 }
749
750 skb = dev_alloc_skb(hdrlen + datalen);
751 if (!skb) {
752 printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
753 dev->name);
Florin Malitabb6e0932006-05-22 22:35:30 -0700754 goto update_stats;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200755 }
756
757 /* Copy the 802.11 header to the skb */
758 memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700759 skb_reset_mac_header(skb);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200760
761 /* If any, copy the data from the card to the skb */
762 if (datalen > 0) {
763 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
764 ALIGN(datalen, 2), rxfid,
765 HERMES_802_2_OFFSET);
766 if (err) {
767 printk(KERN_ERR "%s: error %d reading monitor frame\n",
768 dev->name, err);
769 goto drop;
770 }
771 }
772
773 skb->dev = dev;
774 skb->ip_summed = CHECKSUM_NONE;
775 skb->pkt_type = PACKET_OTHERHOST;
Harvey Harrisonc1b4aa32009-01-29 13:26:44 -0800776 skb->protocol = cpu_to_be16(ETH_P_802_2);
David Kilroy6fe9deb2009-02-04 23:05:43 +0000777
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200778 stats->rx_packets++;
779 stats->rx_bytes += skb->len;
780
781 netif_rx(skb);
782 return;
783
784 drop:
785 dev_kfree_skb_irq(skb);
786 update_stats:
787 stats->rx_errors++;
788 stats->rx_dropped++;
789}
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791static void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
792{
David Kilroyea60a6a2009-06-18 23:21:26 +0100793 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 struct net_device_stats *stats = &priv->stats;
795 struct iw_statistics *wstats = &priv->wstats;
796 struct sk_buff *skb = NULL;
David Kilroy31afcef2008-08-21 23:28:04 +0100797 u16 rxfid, status;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200798 int length;
David Kilroy31afcef2008-08-21 23:28:04 +0100799 struct hermes_rx_descriptor *desc;
800 struct orinoco_rx_data *rx_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 int err;
802
David Kilroy31afcef2008-08-21 23:28:04 +0100803 desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
804 if (!desc) {
805 printk(KERN_WARNING
806 "%s: Can't allocate space for RX descriptor\n",
807 dev->name);
808 goto update_stats;
809 }
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 rxfid = hermes_read_regn(hw, RXFID);
812
David Kilroy31afcef2008-08-21 23:28:04 +0100813 err = hermes_bap_pread(hw, IRQ_BAP, desc, sizeof(*desc),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 rxfid, 0);
815 if (err) {
816 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
817 "Frame dropped.\n", dev->name, err);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200818 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820
David Kilroy31afcef2008-08-21 23:28:04 +0100821 status = le16_to_cpu(desc->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200823 if (status & HERMES_RXSTAT_BADCRC) {
824 DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
825 dev->name);
826 stats->rx_crc_errors++;
827 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200830 /* Handle frames in monitor mode */
David Kilroy5217c572009-06-18 23:21:32 +0100831 if (priv->iw_mode == NL80211_IFTYPE_MONITOR) {
David Kilroy31afcef2008-08-21 23:28:04 +0100832 orinoco_rx_monitor(dev, rxfid, desc);
833 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200836 if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
837 DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
838 dev->name);
839 wstats->discard.code++;
840 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
842
David Kilroy31afcef2008-08-21 23:28:04 +0100843 length = le16_to_cpu(desc->data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 /* Sanity checks */
846 if (length < 3) { /* No for even an 802.2 LLC header */
847 /* At least on Symbol firmware with PCF we get quite a
David Kilroy6fe9deb2009-02-04 23:05:43 +0000848 lot of these legitimately - Poll frames with no
849 data. */
David Kilroy31afcef2008-08-21 23:28:04 +0100850 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
Johannes Berg2c7060022008-10-30 22:09:54 +0100852 if (length > IEEE80211_MAX_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
854 dev->name, length);
855 stats->rx_length_errors++;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200856 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858
David Kilroy23edcc42008-08-21 23:28:05 +0100859 /* Payload size does not include Michael MIC. Increase payload
860 * size to read it together with the data. */
861 if (status & HERMES_RXSTAT_MIC)
862 length += MICHAEL_MIC_LEN;
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 /* We need space for the packet data itself, plus an ethernet
865 header, plus 2 bytes so we can align the IP header on a
866 32bit boundary, plus 1 byte so we can read in odd length
867 packets from the card, which has an IO granularity of 16
David Kilroy6fe9deb2009-02-04 23:05:43 +0000868 bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 skb = dev_alloc_skb(length+ETH_HLEN+2+1);
870 if (!skb) {
871 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
872 dev->name);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200873 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200876 /* We'll prepend the header, so reserve space for it. The worst
877 case is no decapsulation, when 802.3 header is prepended and
878 nothing is removed. 2 is for aligning the IP header. */
879 skb_reserve(skb, ETH_HLEN + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200881 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, length),
882 ALIGN(length, 2), rxfid,
883 HERMES_802_2_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (err) {
885 printk(KERN_ERR "%s: error %d reading frame. "
886 "Frame dropped.\n", dev->name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 goto drop;
888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
David Kilroy31afcef2008-08-21 23:28:04 +0100890 /* Add desc and skb to rx queue */
891 rx_data = kzalloc(sizeof(*rx_data), GFP_ATOMIC);
892 if (!rx_data) {
893 printk(KERN_WARNING "%s: Can't allocate RX packet\n",
894 dev->name);
895 goto drop;
896 }
897 rx_data->desc = desc;
898 rx_data->skb = skb;
899 list_add_tail(&rx_data->list, &priv->rx_list);
900 tasklet_schedule(&priv->rx_tasklet);
901
902 return;
903
904drop:
905 dev_kfree_skb_irq(skb);
906update_stats:
907 stats->rx_errors++;
908 stats->rx_dropped++;
909out:
910 kfree(desc);
911}
912
913static void orinoco_rx(struct net_device *dev,
914 struct hermes_rx_descriptor *desc,
915 struct sk_buff *skb)
916{
David Kilroyea60a6a2009-06-18 23:21:26 +0100917 struct orinoco_private *priv = ndev_priv(dev);
David Kilroy31afcef2008-08-21 23:28:04 +0100918 struct net_device_stats *stats = &priv->stats;
919 u16 status, fc;
920 int length;
921 struct ethhdr *hdr;
922
923 status = le16_to_cpu(desc->status);
924 length = le16_to_cpu(desc->data_len);
925 fc = le16_to_cpu(desc->frame_ctl);
926
David Kilroy23edcc42008-08-21 23:28:05 +0100927 /* Calculate and check MIC */
928 if (status & HERMES_RXSTAT_MIC) {
929 int key_id = ((status & HERMES_RXSTAT_MIC_KEY_ID) >>
930 HERMES_MIC_KEY_ID_SHIFT);
931 u8 mic[MICHAEL_MIC_LEN];
932 u8 *rxmic;
933 u8 *src = (fc & IEEE80211_FCTL_FROMDS) ?
934 desc->addr3 : desc->addr2;
935
936 /* Extract Michael MIC from payload */
937 rxmic = skb->data + skb->len - MICHAEL_MIC_LEN;
938
939 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
940 length -= MICHAEL_MIC_LEN;
941
David Kilroy4adb4742009-02-04 23:05:51 +0000942 orinoco_mic(priv->rx_tfm_mic,
David Kilroy23edcc42008-08-21 23:28:05 +0100943 priv->tkip_key[key_id].rx_mic,
944 desc->addr1,
945 src,
946 0, /* priority or QoS? */
947 skb->data,
948 skb->len,
949 &mic[0]);
950
951 if (memcmp(mic, rxmic,
952 MICHAEL_MIC_LEN)) {
953 union iwreq_data wrqu;
954 struct iw_michaelmicfailure wxmic;
David Kilroy23edcc42008-08-21 23:28:05 +0100955
956 printk(KERN_WARNING "%s: "
Johannes Berge1749612008-10-27 15:59:26 -0700957 "Invalid Michael MIC in data frame from %pM, "
David Kilroy23edcc42008-08-21 23:28:05 +0100958 "using key %i\n",
Johannes Berge1749612008-10-27 15:59:26 -0700959 dev->name, src, key_id);
David Kilroy23edcc42008-08-21 23:28:05 +0100960
961 /* TODO: update stats */
962
963 /* Notify userspace */
964 memset(&wxmic, 0, sizeof(wxmic));
965 wxmic.flags = key_id & IW_MICFAILURE_KEY_ID;
966 wxmic.flags |= (desc->addr1[0] & 1) ?
967 IW_MICFAILURE_GROUP : IW_MICFAILURE_PAIRWISE;
968 wxmic.src_addr.sa_family = ARPHRD_ETHER;
969 memcpy(wxmic.src_addr.sa_data, src, ETH_ALEN);
970
971 (void) orinoco_hw_get_tkip_iv(priv, key_id,
972 &wxmic.tsc[0]);
973
974 memset(&wrqu, 0, sizeof(wrqu));
975 wrqu.data.length = sizeof(wxmic);
976 wireless_send_event(dev, IWEVMICHAELMICFAILURE, &wrqu,
977 (char *) &wxmic);
978
979 goto drop;
980 }
981 }
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 /* Handle decapsulation
984 * In most cases, the firmware tell us about SNAP frames.
985 * For some reason, the SNAP frames sent by LinkSys APs
986 * are not properly recognised by most firmwares.
987 * So, check ourselves */
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200988 if (length >= ENCAPS_OVERHEAD &&
989 (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
990 ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
991 is_ethersnap(skb->data))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 /* These indicate a SNAP within 802.2 LLC within
993 802.11 frame which we'll need to de-encapsulate to
994 the original EthernetII frame. */
David Kilroyb2f30a02009-02-04 23:05:46 +0000995 hdr = (struct ethhdr *)skb_push(skb,
996 ETH_HLEN - ENCAPS_OVERHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 } else {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200998 /* 802.3 frame - prepend 802.3 header as is */
999 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
1000 hdr->h_proto = htons(length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
David Kilroy31afcef2008-08-21 23:28:04 +01001002 memcpy(hdr->h_dest, desc->addr1, ETH_ALEN);
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001003 if (fc & IEEE80211_FCTL_FROMDS)
David Kilroy31afcef2008-08-21 23:28:04 +01001004 memcpy(hdr->h_source, desc->addr3, ETH_ALEN);
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001005 else
David Kilroy31afcef2008-08-21 23:28:04 +01001006 memcpy(hdr->h_source, desc->addr2, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 skb->protocol = eth_type_trans(skb, dev);
1009 skb->ip_summed = CHECKSUM_NONE;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001010 if (fc & IEEE80211_FCTL_TODS)
1011 skb->pkt_type = PACKET_OTHERHOST;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 /* Process the wireless stats if needed */
David Kilroy31afcef2008-08-21 23:28:04 +01001014 orinoco_stat_gather(dev, skb, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 /* Pass the packet to the networking stack */
1017 netif_rx(skb);
1018 stats->rx_packets++;
1019 stats->rx_bytes += length;
1020
1021 return;
David Kilroy23edcc42008-08-21 23:28:05 +01001022
1023 drop:
1024 dev_kfree_skb(skb);
1025 stats->rx_errors++;
1026 stats->rx_dropped++;
David Kilroy31afcef2008-08-21 23:28:04 +01001027}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
David Kilroy31afcef2008-08-21 23:28:04 +01001029static void orinoco_rx_isr_tasklet(unsigned long data)
1030{
David Kilroy53819562009-06-18 23:21:28 +01001031 struct orinoco_private *priv = (struct orinoco_private *) data;
1032 struct net_device *dev = priv->ndev;
David Kilroy31afcef2008-08-21 23:28:04 +01001033 struct orinoco_rx_data *rx_data, *temp;
1034 struct hermes_rx_descriptor *desc;
1035 struct sk_buff *skb;
David Kilroy20953ad2009-01-07 00:23:55 +00001036 unsigned long flags;
1037
1038 /* orinoco_rx requires the driver lock, and we also need to
1039 * protect priv->rx_list, so just hold the lock over the
1040 * lot.
1041 *
1042 * If orinoco_lock fails, we've unplugged the card. In this
1043 * case just abort. */
1044 if (orinoco_lock(priv, &flags) != 0)
1045 return;
David Kilroy31afcef2008-08-21 23:28:04 +01001046
1047 /* extract desc and skb from queue */
1048 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
1049 desc = rx_data->desc;
1050 skb = rx_data->skb;
1051 list_del(&rx_data->list);
1052 kfree(rx_data);
1053
1054 orinoco_rx(dev, desc, skb);
1055
1056 kfree(desc);
1057 }
David Kilroy20953ad2009-01-07 00:23:55 +00001058
1059 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
1062/********************************************************************/
1063/* Rx path (info frames) */
1064/********************************************************************/
1065
1066static void print_linkstatus(struct net_device *dev, u16 status)
1067{
David Kilroy21312662009-02-04 23:05:47 +00001068 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 if (suppress_linkstatus)
1071 return;
1072
1073 switch (status) {
1074 case HERMES_LINKSTATUS_NOT_CONNECTED:
1075 s = "Not Connected";
1076 break;
1077 case HERMES_LINKSTATUS_CONNECTED:
1078 s = "Connected";
1079 break;
1080 case HERMES_LINKSTATUS_DISCONNECTED:
1081 s = "Disconnected";
1082 break;
1083 case HERMES_LINKSTATUS_AP_CHANGE:
1084 s = "AP Changed";
1085 break;
1086 case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1087 s = "AP Out of Range";
1088 break;
1089 case HERMES_LINKSTATUS_AP_IN_RANGE:
1090 s = "AP In Range";
1091 break;
1092 case HERMES_LINKSTATUS_ASSOC_FAILED:
1093 s = "Association Failed";
1094 break;
1095 default:
1096 s = "UNKNOWN";
1097 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001098
Pavel Roskin11eaea42009-01-18 23:20:58 -05001099 printk(KERN_DEBUG "%s: New link status: %s (%04x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 dev->name, s, status);
1101}
1102
Christoph Hellwig16739b02005-06-19 01:27:51 +02001103/* Search scan results for requested BSSID, join it if found */
David Howellsc4028952006-11-22 14:57:56 +00001104static void orinoco_join_ap(struct work_struct *work)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001105{
David Howellsc4028952006-11-22 14:57:56 +00001106 struct orinoco_private *priv =
1107 container_of(work, struct orinoco_private, join_work);
1108 struct net_device *dev = priv->ndev;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001109 struct hermes *hw = &priv->hw;
1110 int err;
1111 unsigned long flags;
1112 struct join_req {
1113 u8 bssid[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -04001114 __le16 channel;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001115 } __attribute__ ((packed)) req;
1116 const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001117 struct prism2_scan_apinfo *atom = NULL;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001118 int offset = 4;
Pavel Roskinc89cc222005-09-01 20:06:06 -04001119 int found = 0;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001120 u8 *buf;
1121 u16 len;
1122
1123 /* Allocate buffer for scan results */
1124 buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
David Kilroya94e8422009-02-04 23:05:44 +00001125 if (!buf)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001126 return;
1127
1128 if (orinoco_lock(priv, &flags) != 0)
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001129 goto fail_lock;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001130
1131 /* Sanity checks in case user changed something in the meantime */
David Kilroya94e8422009-02-04 23:05:44 +00001132 if (!priv->bssid_fixed)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001133 goto out;
1134
1135 if (strlen(priv->desired_essid) == 0)
1136 goto out;
1137
1138 /* Read scan results from the firmware */
1139 err = hermes_read_ltv(hw, USER_BAP,
1140 HERMES_RID_SCANRESULTSTABLE,
1141 MAX_SCAN_LEN, &len, buf);
1142 if (err) {
1143 printk(KERN_ERR "%s: Cannot read scan results\n",
1144 dev->name);
1145 goto out;
1146 }
1147
1148 len = HERMES_RECLEN_TO_BYTES(len);
1149
1150 /* Go through the scan results looking for the channel of the AP
1151 * we were requested to join */
1152 for (; offset + atom_len <= len; offset += atom_len) {
1153 atom = (struct prism2_scan_apinfo *) (buf + offset);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001154 if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1155 found = 1;
1156 break;
1157 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001158 }
1159
David Kilroya94e8422009-02-04 23:05:44 +00001160 if (!found) {
Pavel Roskinc89cc222005-09-01 20:06:06 -04001161 DEBUG(1, "%s: Requested AP not found in scan results\n",
1162 dev->name);
1163 goto out;
1164 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001165
Christoph Hellwig16739b02005-06-19 01:27:51 +02001166 memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1167 req.channel = atom->channel; /* both are little-endian */
1168 err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1169 &req);
1170 if (err)
1171 printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1172
1173 out:
Christoph Hellwig16739b02005-06-19 01:27:51 +02001174 orinoco_unlock(priv, &flags);
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001175
1176 fail_lock:
1177 kfree(buf);
Christoph Hellwig16739b02005-06-19 01:27:51 +02001178}
1179
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001180/* Send new BSSID to userspace */
David Kilroy6cd90b12008-08-21 23:28:00 +01001181static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001182{
David Howellsc4028952006-11-22 14:57:56 +00001183 struct net_device *dev = priv->ndev;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001184 struct hermes *hw = &priv->hw;
1185 union iwreq_data wrqu;
1186 int err;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001187
David Kilroy499b7022008-12-09 21:46:29 +00001188 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001189 ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
1190 if (err != 0)
David Kilroy6cd90b12008-08-21 23:28:00 +01001191 return;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001192
1193 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1194
1195 /* Send event to user space */
1196 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001197}
1198
David Kilroy06009fd2008-08-21 23:28:03 +01001199static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
1200{
1201 struct net_device *dev = priv->ndev;
1202 struct hermes *hw = &priv->hw;
1203 union iwreq_data wrqu;
1204 int err;
1205 u8 buf[88];
1206 u8 *ie;
1207
1208 if (!priv->has_wpa)
1209 return;
1210
David Kilroy499b7022008-12-09 21:46:29 +00001211 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
David Kilroy06009fd2008-08-21 23:28:03 +01001212 sizeof(buf), NULL, &buf);
1213 if (err != 0)
1214 return;
1215
1216 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1217 if (ie) {
1218 int rem = sizeof(buf) - (ie - &buf[0]);
1219 wrqu.data.length = ie[1] + 2;
1220 if (wrqu.data.length > rem)
1221 wrqu.data.length = rem;
1222
1223 if (wrqu.data.length)
1224 /* Send event to user space */
1225 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
1226 }
1227}
1228
1229static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
1230{
1231 struct net_device *dev = priv->ndev;
1232 struct hermes *hw = &priv->hw;
1233 union iwreq_data wrqu;
1234 int err;
1235 u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
1236 u8 *ie;
1237
1238 if (!priv->has_wpa)
1239 return;
1240
David Kilroy499b7022008-12-09 21:46:29 +00001241 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_RESP_INFO,
David Kilroy06009fd2008-08-21 23:28:03 +01001242 sizeof(buf), NULL, &buf);
1243 if (err != 0)
1244 return;
1245
1246 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1247 if (ie) {
1248 int rem = sizeof(buf) - (ie - &buf[0]);
1249 wrqu.data.length = ie[1] + 2;
1250 if (wrqu.data.length > rem)
1251 wrqu.data.length = rem;
1252
1253 if (wrqu.data.length)
1254 /* Send event to user space */
1255 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
1256 }
1257}
1258
David Kilroy6cd90b12008-08-21 23:28:00 +01001259static void orinoco_send_wevents(struct work_struct *work)
1260{
1261 struct orinoco_private *priv =
1262 container_of(work, struct orinoco_private, wevent_work);
1263 unsigned long flags;
1264
1265 if (orinoco_lock(priv, &flags) != 0)
1266 return;
1267
David Kilroy06009fd2008-08-21 23:28:03 +01001268 orinoco_send_assocreqie_wevent(priv);
1269 orinoco_send_assocrespie_wevent(priv);
David Kilroy6cd90b12008-08-21 23:28:00 +01001270 orinoco_send_bssid_wevent(priv);
1271
1272 orinoco_unlock(priv, &flags);
1273}
Dan Williams1e3428e2007-10-10 23:56:25 -04001274
David Kilroyc63cdbe2009-06-18 23:21:33 +01001275static void qbuf_scan(struct orinoco_private *priv, void *buf,
1276 int len, int type)
1277{
1278 struct orinoco_scan_data *sd;
1279 unsigned long flags;
1280
1281 sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
1282 sd->buf = buf;
1283 sd->len = len;
1284 sd->type = type;
1285
1286 spin_lock_irqsave(&priv->scan_lock, flags);
1287 list_add_tail(&sd->list, &priv->scan_list);
1288 spin_unlock_irqrestore(&priv->scan_lock, flags);
1289
1290 schedule_work(&priv->process_scan);
1291}
1292
1293static void qabort_scan(struct orinoco_private *priv)
1294{
1295 struct orinoco_scan_data *sd;
1296 unsigned long flags;
1297
1298 sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
1299 sd->len = -1; /* Abort */
1300
1301 spin_lock_irqsave(&priv->scan_lock, flags);
1302 list_add_tail(&sd->list, &priv->scan_list);
1303 spin_unlock_irqrestore(&priv->scan_lock, flags);
1304
1305 schedule_work(&priv->process_scan);
1306}
1307
1308static void orinoco_process_scan_results(struct work_struct *work)
1309{
1310 struct orinoco_private *priv =
1311 container_of(work, struct orinoco_private, process_scan);
1312 struct orinoco_scan_data *sd, *temp;
1313 unsigned long flags;
1314 void *buf;
1315 int len;
1316 int type;
1317
1318 spin_lock_irqsave(&priv->scan_lock, flags);
1319 list_for_each_entry_safe(sd, temp, &priv->scan_list, list) {
1320 spin_unlock_irqrestore(&priv->scan_lock, flags);
1321
1322 buf = sd->buf;
1323 len = sd->len;
1324 type = sd->type;
1325
1326 list_del(&sd->list);
1327 kfree(sd);
1328
1329 if (len > 0) {
1330 if (type == HERMES_INQ_CHANNELINFO)
1331 orinoco_add_extscan_result(priv, buf, len);
1332 else
1333 orinoco_add_hostscan_results(priv, buf, len);
1334
1335 kfree(buf);
1336 } else if (priv->scan_request) {
1337 /* Either abort or complete the scan */
1338 cfg80211_scan_done(priv->scan_request, (len < 0));
1339 priv->scan_request = NULL;
1340 }
1341
1342 spin_lock_irqsave(&priv->scan_lock, flags);
1343 }
1344 spin_unlock_irqrestore(&priv->scan_lock, flags);
1345}
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347static void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
1348{
David Kilroyea60a6a2009-06-18 23:21:26 +01001349 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 u16 infofid;
1351 struct {
Pavel Roskind133ae42005-09-23 04:18:06 -04001352 __le16 len;
1353 __le16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 } __attribute__ ((packed)) info;
1355 int len, type;
1356 int err;
1357
1358 /* This is an answer to an INQUIRE command that we did earlier,
1359 * or an information "event" generated by the card
1360 * The controller return to us a pseudo frame containing
1361 * the information in question - Jean II */
1362 infofid = hermes_read_regn(hw, INFOFID);
1363
1364 /* Read the info frame header - don't try too hard */
1365 err = hermes_bap_pread(hw, IRQ_BAP, &info, sizeof(info),
1366 infofid, 0);
1367 if (err) {
1368 printk(KERN_ERR "%s: error %d reading info frame. "
1369 "Frame dropped.\n", dev->name, err);
1370 return;
1371 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
1374 type = le16_to_cpu(info.type);
1375
1376 switch (type) {
1377 case HERMES_INQ_TALLIES: {
1378 struct hermes_tallies_frame tallies;
1379 struct iw_statistics *wstats = &priv->wstats;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (len > sizeof(tallies)) {
1382 printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
1383 dev->name, len);
1384 len = sizeof(tallies);
1385 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001386
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001387 err = hermes_bap_pread(hw, IRQ_BAP, &tallies, len,
1388 infofid, sizeof(info));
1389 if (err)
1390 break;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 /* Increment our various counters */
1393 /* wstats->discard.nwid - no wrong BSSID stuff */
1394 wstats->discard.code +=
1395 le16_to_cpu(tallies.RxWEPUndecryptable);
David Kilroy6fe9deb2009-02-04 23:05:43 +00001396 if (len == sizeof(tallies))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 wstats->discard.code +=
1398 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
1399 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
1400 wstats->discard.misc +=
1401 le16_to_cpu(tallies.TxDiscardsWrongSA);
1402 wstats->discard.fragment +=
1403 le16_to_cpu(tallies.RxMsgInBadMsgFragments);
1404 wstats->discard.retries +=
1405 le16_to_cpu(tallies.TxRetryLimitExceeded);
1406 /* wstats->miss.beacon - no match */
1407 }
1408 break;
1409 case HERMES_INQ_LINKSTATUS: {
1410 struct hermes_linkstatus linkstatus;
1411 u16 newstatus;
1412 int connected;
1413
David Kilroy5217c572009-06-18 23:21:32 +01001414 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001415 break;
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (len != sizeof(linkstatus)) {
1418 printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
1419 dev->name, len);
1420 break;
1421 }
1422
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001423 err = hermes_bap_pread(hw, IRQ_BAP, &linkstatus, len,
1424 infofid, sizeof(info));
1425 if (err)
1426 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 newstatus = le16_to_cpu(linkstatus.linkstatus);
1428
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001429 /* Symbol firmware uses "out of range" to signal that
1430 * the hostscan frame can be requested. */
1431 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
1432 priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
David Kilroyc63cdbe2009-06-18 23:21:33 +01001433 priv->has_hostscan && priv->scan_request) {
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001434 hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
1435 break;
1436 }
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
1439 || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
1440 || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
1441
1442 if (connected)
1443 netif_carrier_on(dev);
David Gibson7bb7c3a2005-05-12 20:02:10 -04001444 else if (!ignore_disconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 netif_carrier_off(dev);
1446
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001447 if (newstatus != priv->last_linkstatus) {
1448 priv->last_linkstatus = newstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 print_linkstatus(dev, newstatus);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001450 /* The info frame contains only one word which is the
1451 * status (see hermes.h). The status is pretty boring
1452 * in itself, that's why we export the new BSSID...
1453 * Jean II */
1454 schedule_work(&priv->wevent_work);
1455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
1457 break;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001458 case HERMES_INQ_SCAN:
David Kilroyc63cdbe2009-06-18 23:21:33 +01001459 if (!priv->scan_request && priv->bssid_fixed &&
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001460 priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
1461 schedule_work(&priv->join_work);
1462 break;
1463 }
1464 /* fall through */
1465 case HERMES_INQ_HOSTSCAN:
1466 case HERMES_INQ_HOSTSCAN_SYMBOL: {
1467 /* Result of a scanning. Contains information about
1468 * cells in the vicinity - Jean II */
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001469 unsigned char *buf;
1470
1471 /* Sanity check */
1472 if (len > 4096) {
1473 printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
1474 dev->name, len);
David Kilroyc63cdbe2009-06-18 23:21:33 +01001475 qabort_scan(priv);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001476 break;
1477 }
1478
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001479 /* Allocate buffer for results */
1480 buf = kmalloc(len, GFP_ATOMIC);
David Kilroyc63cdbe2009-06-18 23:21:33 +01001481 if (buf == NULL) {
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001482 /* No memory, so can't printk()... */
David Kilroyc63cdbe2009-06-18 23:21:33 +01001483 qabort_scan(priv);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001484 break;
David Kilroyc63cdbe2009-06-18 23:21:33 +01001485 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001486
1487 /* Read scan data */
1488 err = hermes_bap_pread(hw, IRQ_BAP, (void *) buf, len,
1489 infofid, sizeof(info));
Pavel Roskin708218b2005-09-01 20:05:19 -04001490 if (err) {
1491 kfree(buf);
David Kilroyc63cdbe2009-06-18 23:21:33 +01001492 qabort_scan(priv);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001493 break;
Pavel Roskin708218b2005-09-01 20:05:19 -04001494 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001495
1496#ifdef ORINOCO_DEBUG
1497 {
1498 int i;
1499 printk(KERN_DEBUG "Scan result [%02X", buf[0]);
David Kilroya94e8422009-02-04 23:05:44 +00001500 for (i = 1; i < (len * 2); i++)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001501 printk(":%02X", buf[i]);
1502 printk("]\n");
1503 }
1504#endif /* ORINOCO_DEBUG */
1505
David Kilroyc63cdbe2009-06-18 23:21:33 +01001506 qbuf_scan(priv, buf, len, type);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001507 }
1508 break;
David Kilroy01632fa2008-08-21 23:27:58 +01001509 case HERMES_INQ_CHANNELINFO:
1510 {
1511 struct agere_ext_scan_info *bss;
1512
David Kilroyc63cdbe2009-06-18 23:21:33 +01001513 if (!priv->scan_request) {
David Kilroy01632fa2008-08-21 23:27:58 +01001514 printk(KERN_DEBUG "%s: Got chaninfo without scan, "
1515 "len=%d\n", dev->name, len);
1516 break;
1517 }
1518
1519 /* An empty result indicates that the scan is complete */
1520 if (len == 0) {
David Kilroyc63cdbe2009-06-18 23:21:33 +01001521 qbuf_scan(priv, NULL, len, type);
David Kilroy01632fa2008-08-21 23:27:58 +01001522 break;
1523 }
1524
1525 /* Sanity check */
David Kilroyc63cdbe2009-06-18 23:21:33 +01001526 else if (len < (offsetof(struct agere_ext_scan_info,
David Kilroy01632fa2008-08-21 23:27:58 +01001527 data) + 2)) {
1528 /* Drop this result now so we don't have to
1529 * keep checking later */
1530 printk(KERN_WARNING
1531 "%s: Ext scan results too short (%d bytes)\n",
1532 dev->name, len);
1533 break;
1534 }
1535
David Kilroyc63cdbe2009-06-18 23:21:33 +01001536 bss = kmalloc(len, GFP_ATOMIC);
David Kilroy01632fa2008-08-21 23:27:58 +01001537 if (bss == NULL)
1538 break;
1539
1540 /* Read scan data */
1541 err = hermes_bap_pread(hw, IRQ_BAP, (void *) bss, len,
1542 infofid, sizeof(info));
David Kilroyc63cdbe2009-06-18 23:21:33 +01001543 if (err)
David Kilroy01632fa2008-08-21 23:27:58 +01001544 kfree(bss);
David Kilroyc63cdbe2009-06-18 23:21:33 +01001545 else
1546 qbuf_scan(priv, bss, len, type);
David Kilroy01632fa2008-08-21 23:27:58 +01001547
David Kilroy01632fa2008-08-21 23:27:58 +01001548 break;
1549 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001550 case HERMES_INQ_SEC_STAT_AGERE:
1551 /* Security status (Agere specific) */
1552 /* Ignore this frame for now */
1553 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
1554 break;
1555 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 default:
1557 printk(KERN_DEBUG "%s: Unknown information frame received: "
1558 "type 0x%04x, length %d\n", dev->name, type, len);
1559 /* We don't actually do anything about it */
1560 break;
1561 }
David Kilroyc63cdbe2009-06-18 23:21:33 +01001562
1563 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
1566static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
1567{
1568 if (net_ratelimit())
1569 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
1570}
1571
1572/********************************************************************/
1573/* Internal hardware control routines */
1574/********************************************************************/
1575
David Kilroy6415f7d2009-06-18 23:21:30 +01001576static int __orinoco_up(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
David Kilroya2608362009-06-18 23:21:23 +01001578 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 struct hermes *hw = &priv->hw;
1580 int err;
1581
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001582 netif_carrier_off(dev); /* just to make sure */
1583
David Kilroy721aa2f2009-06-18 23:21:31 +01001584 err = __orinoco_commit(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (err) {
1586 printk(KERN_ERR "%s: Error %d configuring card\n",
1587 dev->name, err);
1588 return err;
1589 }
1590
1591 /* Fire things up again */
1592 hermes_set_irqmask(hw, ORINOCO_INTEN);
1593 err = hermes_enable_port(hw, 0);
1594 if (err) {
1595 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
1596 dev->name, err);
1597 return err;
1598 }
1599
1600 netif_start_queue(dev);
1601
1602 return 0;
1603}
1604
David Kilroy6415f7d2009-06-18 23:21:30 +01001605static int __orinoco_down(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
David Kilroya2608362009-06-18 23:21:23 +01001607 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 struct hermes *hw = &priv->hw;
1609 int err;
1610
1611 netif_stop_queue(dev);
1612
David Kilroya94e8422009-02-04 23:05:44 +00001613 if (!priv->hw_unavailable) {
1614 if (!priv->broken_disableport) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 err = hermes_disable_port(hw, 0);
1616 if (err) {
1617 /* Some firmwares (e.g. Intersil 1.3.x) seem
1618 * to have problems disabling the port, oh
1619 * well, too bad. */
1620 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
1621 dev->name, err);
1622 priv->broken_disableport = 1;
1623 }
1624 }
1625 hermes_set_irqmask(hw, 0);
1626 hermes_write_regn(hw, EVACK, 0xffff);
1627 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 /* firmware will have to reassociate */
1630 netif_carrier_off(dev);
1631 priv->last_linkstatus = 0xffff;
1632
1633 return 0;
1634}
1635
David Kilroy6415f7d2009-06-18 23:21:30 +01001636static int orinoco_reinit_firmware(struct orinoco_private *priv)
Pavel Roskin37a6c612006-04-07 04:10:49 -04001637{
Pavel Roskin37a6c612006-04-07 04:10:49 -04001638 struct hermes *hw = &priv->hw;
1639 int err;
1640
1641 err = hermes_init(hw);
Andrey Borzenkov0df6cbb2008-10-12 20:15:43 +04001642 if (priv->do_fw_download && !err) {
1643 err = orinoco_download(priv);
1644 if (err)
1645 priv->do_fw_download = 0;
1646 }
Pavel Roskin37a6c612006-04-07 04:10:49 -04001647 if (!err)
David Kilroy42a51b92009-06-18 23:21:20 +01001648 err = orinoco_hw_allocate_fid(priv);
Pavel Roskin37a6c612006-04-07 04:10:49 -04001649
1650 return err;
1651}
1652
David Kilroy721aa2f2009-06-18 23:21:31 +01001653static int
David Kilroy5865d012009-02-04 23:05:54 +00001654__orinoco_set_multicast_list(struct net_device *dev)
1655{
David Kilroyea60a6a2009-06-18 23:21:26 +01001656 struct orinoco_private *priv = ndev_priv(dev);
David Kilroy5865d012009-02-04 23:05:54 +00001657 int err = 0;
1658 int promisc, mc_count;
1659
1660 /* The Hermes doesn't seem to have an allmulti mode, so we go
1661 * into promiscuous mode and let the upper levels deal. */
1662 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
1663 (dev->mc_count > MAX_MULTICAST(priv))) {
1664 promisc = 1;
1665 mc_count = 0;
1666 } else {
1667 promisc = 0;
1668 mc_count = dev->mc_count;
1669 }
1670
1671 err = __orinoco_hw_set_multicast_list(priv, dev->mc_list, mc_count,
1672 promisc);
David Kilroy721aa2f2009-06-18 23:21:31 +01001673
1674 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675}
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677/* This must be called from user context, without locks held - use
1678 * schedule_work() */
David Kilroycb1576a2009-02-04 23:05:56 +00001679void orinoco_reset(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680{
David Howellsc4028952006-11-22 14:57:56 +00001681 struct orinoco_private *priv =
1682 container_of(work, struct orinoco_private, reset_work);
1683 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 struct hermes *hw = &priv->hw;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001685 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 unsigned long flags;
1687
1688 if (orinoco_lock(priv, &flags) != 0)
1689 /* When the hardware becomes available again, whatever
1690 * detects that is responsible for re-initializing
1691 * it. So no need for anything further */
1692 return;
1693
1694 netif_stop_queue(dev);
1695
1696 /* Shut off interrupts. Depending on what state the hardware
1697 * is in, this might not work, but we'll try anyway */
1698 hermes_set_irqmask(hw, 0);
1699 hermes_write_regn(hw, EVACK, 0xffff);
1700
1701 priv->hw_unavailable++;
1702 priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
1703 netif_carrier_off(dev);
1704
1705 orinoco_unlock(priv, &flags);
1706
David Kilroyc63cdbe2009-06-18 23:21:33 +01001707 /* Scanning support: Notify scan cancellation */
1708 if (priv->scan_request) {
1709 cfg80211_scan_done(priv->scan_request, 1);
1710 priv->scan_request = NULL;
1711 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001712
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001713 if (priv->hard_reset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 err = (*priv->hard_reset)(priv);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001715 if (err) {
1716 printk(KERN_ERR "%s: orinoco_reset: Error %d "
1717 "performing hard reset\n", dev->name, err);
1718 goto disable;
1719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
1721
David Kilroya2608362009-06-18 23:21:23 +01001722 err = orinoco_reinit_firmware(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 if (err) {
1724 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
1725 dev->name, err);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001726 goto disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 }
1728
David Kilroyb2f30a02009-02-04 23:05:46 +00001729 /* This has to be called from user context */
1730 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 priv->hw_unavailable--;
1733
1734 /* priv->open or priv->hw_unavailable might have changed while
1735 * we dropped the lock */
David Kilroya94e8422009-02-04 23:05:44 +00001736 if (priv->open && (!priv->hw_unavailable)) {
David Kilroya2608362009-06-18 23:21:23 +01001737 err = __orinoco_up(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 if (err) {
1739 printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
1740 dev->name, err);
1741 } else
1742 dev->trans_start = jiffies;
1743 }
1744
1745 spin_unlock_irq(&priv->lock);
1746
1747 return;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001748 disable:
1749 hermes_set_irqmask(hw, 0);
1750 netif_device_detach(dev);
1751 printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
1753
David Kilroy721aa2f2009-06-18 23:21:31 +01001754static int __orinoco_commit(struct orinoco_private *priv)
1755{
1756 struct net_device *dev = priv->ndev;
1757 int err = 0;
1758
1759 err = orinoco_hw_program_rids(priv);
1760
1761 /* FIXME: what about netif_tx_lock */
1762 (void) __orinoco_set_multicast_list(dev);
1763
1764 return err;
1765}
1766
1767/* Ensures configuration changes are applied. May result in a reset.
1768 * The caller should hold priv->lock
1769 */
1770int orinoco_commit(struct orinoco_private *priv)
1771{
1772 struct net_device *dev = priv->ndev;
1773 hermes_t *hw = &priv->hw;
1774 int err;
1775
1776 if (priv->broken_disableport) {
1777 schedule_work(&priv->reset_work);
1778 return 0;
1779 }
1780
1781 err = hermes_disable_port(hw, 0);
1782 if (err) {
1783 printk(KERN_WARNING "%s: Unable to disable port "
1784 "while reconfiguring card\n", dev->name);
1785 priv->broken_disableport = 1;
1786 goto out;
1787 }
1788
1789 err = __orinoco_commit(priv);
1790 if (err) {
1791 printk(KERN_WARNING "%s: Unable to reconfigure card\n",
1792 dev->name);
1793 goto out;
1794 }
1795
1796 err = hermes_enable_port(hw, 0);
1797 if (err) {
1798 printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
1799 dev->name);
1800 goto out;
1801 }
1802
1803 out:
1804 if (err) {
1805 printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
1806 schedule_work(&priv->reset_work);
1807 err = 0;
1808 }
1809 return err;
1810}
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812/********************************************************************/
1813/* Interrupt handler */
1814/********************************************************************/
1815
1816static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
1817{
1818 printk(KERN_DEBUG "%s: TICK\n", dev->name);
1819}
1820
1821static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
1822{
1823 /* This seems to happen a fair bit under load, but ignoring it
1824 seems to work fine...*/
1825 printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
1826 dev->name);
1827}
1828
David Howells7d12e782006-10-05 14:55:46 +01001829irqreturn_t orinoco_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
David Kilroya2608362009-06-18 23:21:23 +01001831 struct orinoco_private *priv = dev_id;
1832 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 hermes_t *hw = &priv->hw;
1834 int count = MAX_IRQLOOPS_PER_IRQ;
1835 u16 evstat, events;
David Kilroy21312662009-02-04 23:05:47 +00001836 /* These are used to detect a runaway interrupt situation.
1837 *
1838 * If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
1839 * we panic and shut down the hardware
1840 */
1841 /* jiffies value the last time we were called */
1842 static int last_irq_jiffy; /* = 0 */
1843 static int loops_this_jiffy; /* = 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 unsigned long flags;
1845
1846 if (orinoco_lock(priv, &flags) != 0) {
1847 /* If hw is unavailable - we don't know if the irq was
1848 * for us or not */
1849 return IRQ_HANDLED;
1850 }
1851
1852 evstat = hermes_read_regn(hw, EVSTAT);
1853 events = evstat & hw->inten;
David Kilroya94e8422009-02-04 23:05:44 +00001854 if (!events) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 orinoco_unlock(priv, &flags);
1856 return IRQ_NONE;
1857 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 if (jiffies != last_irq_jiffy)
1860 loops_this_jiffy = 0;
1861 last_irq_jiffy = jiffies;
1862
1863 while (events && count--) {
1864 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
1865 printk(KERN_WARNING "%s: IRQ handler is looping too "
1866 "much! Resetting.\n", dev->name);
1867 /* Disable interrupts for now */
1868 hermes_set_irqmask(hw, 0);
1869 schedule_work(&priv->reset_work);
1870 break;
1871 }
1872
1873 /* Check the card hasn't been removed */
David Kilroya94e8422009-02-04 23:05:44 +00001874 if (!hermes_present(hw)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 DEBUG(0, "orinoco_interrupt(): card removed\n");
1876 break;
1877 }
1878
1879 if (events & HERMES_EV_TICK)
1880 __orinoco_ev_tick(dev, hw);
1881 if (events & HERMES_EV_WTERR)
1882 __orinoco_ev_wterr(dev, hw);
1883 if (events & HERMES_EV_INFDROP)
1884 __orinoco_ev_infdrop(dev, hw);
1885 if (events & HERMES_EV_INFO)
1886 __orinoco_ev_info(dev, hw);
1887 if (events & HERMES_EV_RX)
1888 __orinoco_ev_rx(dev, hw);
1889 if (events & HERMES_EV_TXEXC)
1890 __orinoco_ev_txexc(dev, hw);
1891 if (events & HERMES_EV_TX)
1892 __orinoco_ev_tx(dev, hw);
1893 if (events & HERMES_EV_ALLOC)
1894 __orinoco_ev_alloc(dev, hw);
David Kilroy6fe9deb2009-02-04 23:05:43 +00001895
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001896 hermes_write_regn(hw, EVACK, evstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898 evstat = hermes_read_regn(hw, EVSTAT);
1899 events = evstat & hw->inten;
1900 };
1901
1902 orinoco_unlock(priv, &flags);
1903 return IRQ_HANDLED;
1904}
David Kilroy21312662009-02-04 23:05:47 +00001905EXPORT_SYMBOL(orinoco_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907/********************************************************************/
David Kilroy39d1ffe2008-11-22 10:37:28 +00001908/* Power management */
1909/********************************************************************/
1910#if defined(CONFIG_PM_SLEEP) && !defined(CONFIG_HERMES_CACHE_FW_ON_INIT)
1911static int orinoco_pm_notifier(struct notifier_block *notifier,
1912 unsigned long pm_event,
1913 void *unused)
1914{
1915 struct orinoco_private *priv = container_of(notifier,
1916 struct orinoco_private,
1917 pm_notifier);
1918
1919 /* All we need to do is cache the firmware before suspend, and
1920 * release it when we come out.
1921 *
1922 * Only need to do this if we're downloading firmware. */
1923 if (!priv->do_fw_download)
1924 return NOTIFY_DONE;
1925
1926 switch (pm_event) {
1927 case PM_HIBERNATION_PREPARE:
1928 case PM_SUSPEND_PREPARE:
1929 orinoco_cache_fw(priv, 0);
1930 break;
1931
1932 case PM_POST_RESTORE:
1933 /* Restore from hibernation failed. We need to clean
1934 * up in exactly the same way, so fall through. */
1935 case PM_POST_HIBERNATION:
1936 case PM_POST_SUSPEND:
1937 orinoco_uncache_fw(priv);
1938 break;
1939
1940 case PM_RESTORE_PREPARE:
1941 default:
1942 break;
1943 }
1944
1945 return NOTIFY_DONE;
1946}
David S. Millerf11c1792009-02-25 00:02:05 -08001947
1948static void orinoco_register_pm_notifier(struct orinoco_private *priv)
1949{
1950 priv->pm_notifier.notifier_call = orinoco_pm_notifier;
1951 register_pm_notifier(&priv->pm_notifier);
1952}
1953
1954static void orinoco_unregister_pm_notifier(struct orinoco_private *priv)
1955{
1956 unregister_pm_notifier(&priv->pm_notifier);
1957}
David Kilroy39d1ffe2008-11-22 10:37:28 +00001958#else /* !PM_SLEEP || HERMES_CACHE_FW_ON_INIT */
David S. Millerf11c1792009-02-25 00:02:05 -08001959#define orinoco_register_pm_notifier(priv) do { } while(0)
1960#define orinoco_unregister_pm_notifier(priv) do { } while(0)
David Kilroy39d1ffe2008-11-22 10:37:28 +00001961#endif
1962
1963/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964/* Initialization */
1965/********************************************************************/
1966
David Kilroy8e638262009-06-18 23:21:24 +01001967int orinoco_init(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
David Kilroy8e638262009-06-18 23:21:24 +01001969 struct device *dev = priv->dev;
David Kilroyea60a6a2009-06-18 23:21:26 +01001970 struct wiphy *wiphy = priv_to_wiphy(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 hermes_t *hw = &priv->hw;
1972 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 /* No need to lock, the hw_unavailable flag is already set in
1975 * alloc_orinocodev() */
Johannes Berg2c7060022008-10-30 22:09:54 +01001976 priv->nicbuf_size = IEEE80211_MAX_FRAME_LEN + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 /* Initialize the firmware */
Pavel Roskin37a6c612006-04-07 04:10:49 -04001979 err = hermes_init(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (err != 0) {
David Kilroy8e638262009-06-18 23:21:24 +01001981 dev_err(dev, "Failed to initialize firmware (err = %d)\n",
1982 err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 goto out;
1984 }
1985
David Kilroya2d1a422009-06-18 23:21:18 +01001986 err = determine_fw_capabilities(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 if (err != 0) {
David Kilroy8e638262009-06-18 23:21:24 +01001988 dev_err(dev, "Incompatible firmware, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 goto out;
1990 }
1991
David Kilroy3994d502008-08-21 23:27:54 +01001992 if (priv->do_fw_download) {
David Kilroy39d1ffe2008-11-22 10:37:28 +00001993#ifdef CONFIG_HERMES_CACHE_FW_ON_INIT
David Kilroy74734312008-11-22 10:37:25 +00001994 orinoco_cache_fw(priv, 0);
David Kilroy39d1ffe2008-11-22 10:37:28 +00001995#endif
David Kilroy74734312008-11-22 10:37:25 +00001996
David Kilroy3994d502008-08-21 23:27:54 +01001997 err = orinoco_download(priv);
1998 if (err)
1999 priv->do_fw_download = 0;
2000
2001 /* Check firmware version again */
David Kilroya2d1a422009-06-18 23:21:18 +01002002 err = determine_fw_capabilities(priv);
David Kilroy3994d502008-08-21 23:27:54 +01002003 if (err != 0) {
David Kilroy8e638262009-06-18 23:21:24 +01002004 dev_err(dev, "Incompatible firmware, aborting\n");
David Kilroy3994d502008-08-21 23:27:54 +01002005 goto out;
2006 }
2007 }
2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 if (priv->has_port3)
David Kilroy8e638262009-06-18 23:21:24 +01002010 dev_info(dev, "Ad-hoc demo mode supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 if (priv->has_ibss)
David Kilroy8e638262009-06-18 23:21:24 +01002012 dev_info(dev, "IEEE standard IBSS ad-hoc mode supported\n");
2013 if (priv->has_wep)
2014 dev_info(dev, "WEP supported, %s-bit key\n",
2015 priv->has_big_wep ? "104" : "40");
David Kilroy23edcc42008-08-21 23:28:05 +01002016 if (priv->has_wpa) {
David Kilroy8e638262009-06-18 23:21:24 +01002017 dev_info(dev, "WPA-PSK supported\n");
David Kilroy23edcc42008-08-21 23:28:05 +01002018 if (orinoco_mic_init(priv)) {
David Kilroy8e638262009-06-18 23:21:24 +01002019 dev_err(dev, "Failed to setup MIC crypto algorithm. "
2020 "Disabling WPA support\n");
David Kilroy23edcc42008-08-21 23:28:05 +01002021 priv->has_wpa = 0;
2022 }
2023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
David Kilroyea60a6a2009-06-18 23:21:26 +01002025 err = orinoco_hw_read_card_settings(priv, wiphy->perm_addr);
David Kilroye9e3d012009-06-18 23:21:19 +01002026 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
David Kilroy42a51b92009-06-18 23:21:20 +01002029 err = orinoco_hw_allocate_fid(priv);
Pavel Roskin37a6c612006-04-07 04:10:49 -04002030 if (err) {
David Kilroy8e638262009-06-18 23:21:24 +01002031 dev_err(dev, "Failed to allocate NIC buffer!\n");
Pavel Roskin37a6c612006-04-07 04:10:49 -04002032 goto out;
2033 }
2034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 /* Set up the default configuration */
David Kilroy5217c572009-06-18 23:21:32 +01002036 priv->iw_mode = NL80211_IFTYPE_STATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 /* By default use IEEE/IBSS ad-hoc mode if we have it */
David Kilroya94e8422009-02-04 23:05:44 +00002038 priv->prefer_port3 = priv->has_port3 && (!priv->has_ibss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 set_port_type(priv);
David Gibsond51d8b12005-05-12 20:03:36 -04002040 priv->channel = 0; /* use firmware default */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 priv->promiscuous = 0;
David Kilroy4ae6ee22008-08-21 23:27:59 +01002043 priv->encode_alg = IW_ENCODE_ALG_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 priv->tx_key = 0;
David Kilroyd03032a2008-08-21 23:28:02 +01002045 priv->wpa_enabled = 0;
2046 priv->tkip_cm_active = 0;
2047 priv->key_mgmt = 0;
2048 priv->wpa_ie_len = 0;
2049 priv->wpa_ie = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
David Kilroyea60a6a2009-06-18 23:21:26 +01002051 if (orinoco_wiphy_register(wiphy)) {
2052 err = -ENODEV;
2053 goto out;
2054 }
2055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 /* Make the hardware available, as long as it hasn't been
2057 * removed elsewhere (e.g. by PCMCIA hot unplug) */
2058 spin_lock_irq(&priv->lock);
2059 priv->hw_unavailable--;
2060 spin_unlock_irq(&priv->lock);
2061
David Kilroy8e638262009-06-18 23:21:24 +01002062 dev_dbg(dev, "Ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 return err;
2066}
David Kilroy8e638262009-06-18 23:21:24 +01002067EXPORT_SYMBOL(orinoco_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Andrey Borzenkov89ea4092009-01-21 20:46:46 +03002069static const struct net_device_ops orinoco_netdev_ops = {
Andrey Borzenkov89ea4092009-01-21 20:46:46 +03002070 .ndo_open = orinoco_open,
2071 .ndo_stop = orinoco_stop,
2072 .ndo_start_xmit = orinoco_xmit,
2073 .ndo_set_multicast_list = orinoco_set_multicast_list,
2074 .ndo_change_mtu = orinoco_change_mtu,
2075 .ndo_tx_timeout = orinoco_tx_timeout,
2076 .ndo_get_stats = orinoco_get_stats,
2077};
2078
David Kilroyea60a6a2009-06-18 23:21:26 +01002079/* Allocate private data.
2080 *
2081 * This driver has a number of structures associated with it
2082 * netdev - Net device structure for each network interface
2083 * wiphy - structure associated with wireless phy
2084 * wireless_dev (wdev) - structure for each wireless interface
2085 * hw - structure for hermes chip info
2086 * card - card specific structure for use by the card driver
2087 * (airport, orinoco_cs)
2088 * priv - orinoco private data
2089 * device - generic linux device structure
2090 *
2091 * +---------+ +---------+
2092 * | wiphy | | netdev |
2093 * | +-------+ | +-------+
2094 * | | priv | | | wdev |
2095 * | | +-----+ +-+-------+
2096 * | | | hw |
2097 * | +-+-----+
2098 * | | card |
2099 * +-+-------+
2100 *
2101 * priv has a link to netdev and device
2102 * wdev has a link to wiphy
2103 */
David Kilroya2608362009-06-18 23:21:23 +01002104struct orinoco_private
David Kilroy3994d502008-08-21 23:27:54 +01002105*alloc_orinocodev(int sizeof_card,
2106 struct device *device,
2107 int (*hard_reset)(struct orinoco_private *),
2108 int (*stop_fw)(struct orinoco_private *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 struct orinoco_private *priv;
David Kilroyea60a6a2009-06-18 23:21:26 +01002111 struct wiphy *wiphy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
David Kilroyea60a6a2009-06-18 23:21:26 +01002113 /* allocate wiphy
2114 * NOTE: We only support a single virtual interface
2115 * but this may change when monitor mode is added
2116 */
2117 wiphy = wiphy_new(&orinoco_cfg_ops,
2118 sizeof(struct orinoco_private) + sizeof_card);
2119 if (!wiphy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 return NULL;
David Kilroyea60a6a2009-06-18 23:21:26 +01002121
David Kilroyea60a6a2009-06-18 23:21:26 +01002122 priv = wiphy_priv(wiphy);
David Kilroy53819562009-06-18 23:21:28 +01002123 priv->dev = device;
David Kilroyea60a6a2009-06-18 23:21:26 +01002124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (sizeof_card)
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002126 priv->card = (void *)((unsigned long)priv
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 + sizeof(struct orinoco_private));
2128 else
2129 priv->card = NULL;
2130
David Kilroyea60a6a2009-06-18 23:21:26 +01002131 orinoco_wiphy_init(wiphy);
2132
Pavel Roskin343c6862005-09-09 18:43:02 -04002133#ifdef WIRELESS_SPY
2134 priv->wireless_data.spy_data = &priv->spy_data;
Pavel Roskin343c6862005-09-09 18:43:02 -04002135#endif
David Kilroy23edcc42008-08-21 23:28:05 +01002136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 /* Set up default callbacks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 priv->hard_reset = hard_reset;
David Kilroy3994d502008-08-21 23:27:54 +01002139 priv->stop_fw = stop_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 spin_lock_init(&priv->lock);
2142 priv->open = 0;
2143 priv->hw_unavailable = 1; /* orinoco_init() must clear this
2144 * before anything else touches the
2145 * hardware */
David Howellsc4028952006-11-22 14:57:56 +00002146 INIT_WORK(&priv->reset_work, orinoco_reset);
2147 INIT_WORK(&priv->join_work, orinoco_join_ap);
2148 INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
David Kilroy31afcef2008-08-21 23:28:04 +01002150 INIT_LIST_HEAD(&priv->rx_list);
2151 tasklet_init(&priv->rx_tasklet, orinoco_rx_isr_tasklet,
David Kilroy53819562009-06-18 23:21:28 +01002152 (unsigned long) priv);
David Kilroy31afcef2008-08-21 23:28:04 +01002153
David Kilroyc63cdbe2009-06-18 23:21:33 +01002154 spin_lock_init(&priv->scan_lock);
2155 INIT_LIST_HEAD(&priv->scan_list);
2156 INIT_WORK(&priv->process_scan, orinoco_process_scan_results);
2157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 priv->last_linkstatus = 0xffff;
2159
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +03002160#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
David Kilroy2cea7b22008-11-22 10:37:26 +00002161 priv->cached_pri_fw = NULL;
Andrey Borzenkov4fb30782008-10-19 12:06:11 +04002162 priv->cached_fw = NULL;
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +03002163#endif
Andrey Borzenkov4fb30782008-10-19 12:06:11 +04002164
David Kilroy39d1ffe2008-11-22 10:37:28 +00002165 /* Register PM notifiers */
David S. Millerf11c1792009-02-25 00:02:05 -08002166 orinoco_register_pm_notifier(priv);
David Kilroy39d1ffe2008-11-22 10:37:28 +00002167
David Kilroya2608362009-06-18 23:21:23 +01002168 return priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169}
David Kilroy21312662009-02-04 23:05:47 +00002170EXPORT_SYMBOL(alloc_orinocodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
David Kilroy53819562009-06-18 23:21:28 +01002172/* We can only support a single interface. We provide a separate
2173 * function to set it up to distinguish between hardware
2174 * initialisation and interface setup.
2175 *
2176 * The base_addr and irq parameters are passed on to netdev for use
2177 * with SIOCGIFMAP.
2178 */
2179int orinoco_if_add(struct orinoco_private *priv,
2180 unsigned long base_addr,
2181 unsigned int irq)
2182{
2183 struct wiphy *wiphy = priv_to_wiphy(priv);
2184 struct wireless_dev *wdev;
2185 struct net_device *dev;
2186 int ret;
2187
2188 dev = alloc_etherdev(sizeof(struct wireless_dev));
2189
2190 if (!dev)
2191 return -ENOMEM;
2192
2193 /* Initialise wireless_dev */
2194 wdev = netdev_priv(dev);
2195 wdev->wiphy = wiphy;
2196 wdev->iftype = NL80211_IFTYPE_STATION;
2197
2198 /* Setup / override net_device fields */
2199 dev->ieee80211_ptr = wdev;
2200 dev->netdev_ops = &orinoco_netdev_ops;
2201 dev->watchdog_timeo = HZ; /* 1 second timeout */
2202 dev->ethtool_ops = &orinoco_ethtool_ops;
2203 dev->wireless_handlers = &orinoco_handler_def;
2204#ifdef WIRELESS_SPY
2205 dev->wireless_data = &priv->wireless_data;
2206#endif
2207 /* we use the default eth_mac_addr for setting the MAC addr */
2208
2209 /* Reserve space in skb for the SNAP header */
2210 dev->hard_header_len += ENCAPS_OVERHEAD;
2211
2212 netif_carrier_off(dev);
2213
2214 memcpy(dev->dev_addr, wiphy->perm_addr, ETH_ALEN);
2215
2216 dev->base_addr = base_addr;
2217 dev->irq = irq;
2218
2219 SET_NETDEV_DEV(dev, priv->dev);
2220 ret = register_netdev(dev);
2221 if (ret)
2222 goto fail;
2223
2224 priv->ndev = dev;
2225
2226 /* Report what we've done */
2227 dev_dbg(priv->dev, "Registerred interface %s.\n", dev->name);
2228
2229 return 0;
2230
2231 fail:
2232 free_netdev(dev);
2233 return ret;
2234}
2235EXPORT_SYMBOL(orinoco_if_add);
2236
2237void orinoco_if_del(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
David Kilroya2608362009-06-18 23:21:23 +01002239 struct net_device *dev = priv->ndev;
David Kilroy53819562009-06-18 23:21:28 +01002240
2241 unregister_netdev(dev);
2242 free_netdev(dev);
2243}
2244EXPORT_SYMBOL(orinoco_if_del);
2245
2246void free_orinocodev(struct orinoco_private *priv)
2247{
David Kilroyea60a6a2009-06-18 23:21:26 +01002248 struct wiphy *wiphy = priv_to_wiphy(priv);
David Kilroy20953ad2009-01-07 00:23:55 +00002249 struct orinoco_rx_data *rx_data, *temp;
David Kilroyc63cdbe2009-06-18 23:21:33 +01002250 struct orinoco_scan_data *sd, *sdtemp;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002251
David Kilroyea60a6a2009-06-18 23:21:26 +01002252 wiphy_unregister(wiphy);
2253
David Kilroy20953ad2009-01-07 00:23:55 +00002254 /* If the tasklet is scheduled when we call tasklet_kill it
2255 * will run one final time. However the tasklet will only
2256 * drain priv->rx_list if the hw is still available. */
David Kilroy31afcef2008-08-21 23:28:04 +01002257 tasklet_kill(&priv->rx_tasklet);
David Kilroy74734312008-11-22 10:37:25 +00002258
David Kilroy20953ad2009-01-07 00:23:55 +00002259 /* Explicitly drain priv->rx_list */
2260 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
2261 list_del(&rx_data->list);
2262
2263 dev_kfree_skb(rx_data->skb);
2264 kfree(rx_data->desc);
2265 kfree(rx_data);
2266 }
2267
David Kilroyc63cdbe2009-06-18 23:21:33 +01002268 cancel_work_sync(&priv->process_scan);
2269 /* Explicitly drain priv->scan_list */
2270 list_for_each_entry_safe(sd, sdtemp, &priv->scan_list, list) {
2271 list_del(&sd->list);
2272
2273 if ((sd->len > 0) && sd->buf)
2274 kfree(sd->buf);
2275 kfree(sd);
2276 }
2277
David S. Millerf11c1792009-02-25 00:02:05 -08002278 orinoco_unregister_pm_notifier(priv);
David Kilroy74734312008-11-22 10:37:25 +00002279 orinoco_uncache_fw(priv);
2280
David Kilroyd03032a2008-08-21 23:28:02 +01002281 priv->wpa_ie_len = 0;
2282 kfree(priv->wpa_ie);
David Kilroy23edcc42008-08-21 23:28:05 +01002283 orinoco_mic_free(priv);
David Kilroyea60a6a2009-06-18 23:21:26 +01002284 wiphy_free(wiphy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285}
David Kilroy21312662009-02-04 23:05:47 +00002286EXPORT_SYMBOL(free_orinocodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
David Kilroy6415f7d2009-06-18 23:21:30 +01002288int orinoco_up(struct orinoco_private *priv)
2289{
2290 struct net_device *dev = priv->ndev;
2291 unsigned long flags;
2292 int err;
2293
2294 spin_lock_irqsave(&priv->lock, flags);
2295
2296 err = orinoco_reinit_firmware(priv);
2297 if (err) {
2298 printk(KERN_ERR "%s: Error %d re-initializing firmware\n",
2299 dev->name, err);
2300 goto exit;
2301 }
2302
2303 netif_device_attach(dev);
2304 priv->hw_unavailable--;
2305
2306 if (priv->open && !priv->hw_unavailable) {
2307 err = __orinoco_up(priv);
2308 if (err)
2309 printk(KERN_ERR "%s: Error %d restarting card\n",
2310 dev->name, err);
2311 }
2312
2313exit:
2314 spin_unlock_irqrestore(&priv->lock, flags);
2315
2316 return 0;
2317}
2318EXPORT_SYMBOL(orinoco_up);
2319
2320void orinoco_down(struct orinoco_private *priv)
2321{
2322 struct net_device *dev = priv->ndev;
2323 unsigned long flags;
2324 int err;
2325
2326 spin_lock_irqsave(&priv->lock, flags);
2327 err = __orinoco_down(priv);
2328 if (err)
2329 printk(KERN_WARNING "%s: Error %d downing interface\n",
2330 dev->name, err);
2331
2332 netif_device_detach(dev);
2333 priv->hw_unavailable++;
2334 spin_unlock_irqrestore(&priv->lock, flags);
2335}
2336EXPORT_SYMBOL(orinoco_down);
2337
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02002338static void orinoco_get_drvinfo(struct net_device *dev,
2339 struct ethtool_drvinfo *info)
2340{
David Kilroyea60a6a2009-06-18 23:21:26 +01002341 struct orinoco_private *priv = ndev_priv(dev);
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02002342
2343 strncpy(info->driver, DRIVER_NAME, sizeof(info->driver) - 1);
2344 strncpy(info->version, DRIVER_VERSION, sizeof(info->version) - 1);
2345 strncpy(info->fw_version, priv->fw_name, sizeof(info->fw_version) - 1);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07002346 if (dev->dev.parent)
Kay Sieversfb28ad32008-11-10 13:55:14 -08002347 strncpy(info->bus_info, dev_name(dev->dev.parent),
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02002348 sizeof(info->bus_info) - 1);
2349 else
2350 snprintf(info->bus_info, sizeof(info->bus_info) - 1,
2351 "PCMCIA %p", priv->hw.iobase);
2352}
2353
Jeff Garzik7282d492006-09-13 14:30:00 -04002354static const struct ethtool_ops orinoco_ethtool_ops = {
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02002355 .get_drvinfo = orinoco_get_drvinfo,
2356 .get_link = ethtool_op_get_link,
2357};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
2359/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360/* Module initialization */
2361/********************************************************************/
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363/* Can't be declared "const" or the whole __initdata section will
2364 * become const */
2365static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
2366 " (David Gibson <hermes@gibson.dropbear.id.au>, "
2367 "Pavel Roskin <proski@gnu.org>, et al)";
2368
2369static int __init init_orinoco(void)
2370{
2371 printk(KERN_DEBUG "%s\n", version);
2372 return 0;
2373}
2374
2375static void __exit exit_orinoco(void)
2376{
2377}
2378
2379module_init(init_orinoco);
2380module_exit(exit_orinoco);