blob: b42634c614b5814a9822ef3011efd11b6ab30345 [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>
David Kilroy39d1ffe2008-11-22 10:37:28 +000086#include <linux/suspend.h>
Pavel Roskin9c974fb2006-08-15 20:45:03 -040087#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include <linux/wireless.h>
Johannes Berg2c7060022008-10-30 22:09:54 +010089#include <linux/ieee80211.h>
Christoph Hellwig620554e2005-06-19 01:27:33 +020090#include <net/iw_handler.h>
David Kilroyea60a6a2009-06-18 23:21:26 +010091#include <net/cfg80211.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#include "hermes_rid.h"
David Kilroy3994d502008-08-21 23:27:54 +010094#include "hermes_dld.h"
David Kilroy712a4342009-02-04 23:05:55 +000095#include "hw.h"
David Kilroyfb791b12009-02-04 23:05:50 +000096#include "scan.h"
David Kilroy4adb4742009-02-04 23:05:51 +000097#include "mic.h"
David Kilroy37a2e562009-02-04 23:05:52 +000098#include "fw.h"
David Kilroycb1576a2009-02-04 23:05:56 +000099#include "wext.h"
David Kilroyea60a6a2009-06-18 23:21:26 +0100100#include "cfg.h"
David Kilroycb1576a2009-02-04 23:05:56 +0000101#include "main.h"
David Kilroyfb791b12009-02-04 23:05:50 +0000102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#include "orinoco.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/********************************************************************/
106/* Module information */
107/********************************************************************/
108
David Kilroyb2f30a02009-02-04 23:05:46 +0000109MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & "
110 "David Gibson <hermes@gibson.dropbear.id.au>");
111MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based "
112 "and similar wireless cards");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113MODULE_LICENSE("Dual MPL/GPL");
114
115/* Level of debugging. Used in the macros in orinoco.h */
116#ifdef ORINOCO_DEBUG
117int orinoco_debug = ORINOCO_DEBUG;
David Kilroy21312662009-02-04 23:05:47 +0000118EXPORT_SYMBOL(orinoco_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119module_param(orinoco_debug, int, 0644);
120MODULE_PARM_DESC(orinoco_debug, "Debug level");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#endif
122
123static int suppress_linkstatus; /* = 0 */
124module_param(suppress_linkstatus, bool, 0644);
125MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
David Kilroyb2f30a02009-02-04 23:05:46 +0000126
David Gibson7bb7c3a2005-05-12 20:02:10 -0400127static int ignore_disconnect; /* = 0 */
128module_param(ignore_disconnect, int, 0644);
David Kilroyb2f30a02009-02-04 23:05:46 +0000129MODULE_PARM_DESC(ignore_disconnect,
130 "Don't report lost link to the network layer");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
David Kilroycb1576a2009-02-04 23:05:56 +0000132int force_monitor; /* = 0 */
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200133module_param(force_monitor, int, 0644);
134MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/* Internal constants */
138/********************************************************************/
139
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200140/* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
141static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
142#define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#define ORINOCO_MIN_MTU 256
Johannes Berg2c7060022008-10-30 22:09:54 +0100145#define ORINOCO_MAX_MTU (IEEE80211_MAX_DATA_LEN - ENCAPS_OVERHEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#define MAX_IRQLOOPS_PER_IRQ 10
148#define MAX_IRQLOOPS_PER_JIFFY (20000/HZ) /* Based on a guestimate of
149 * how many events the
150 * device could
151 * legitimately generate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153#define DUMMY_FID 0xFFFF
154
155/*#define MAX_MULTICAST(priv) (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
156 HERMES_MAX_MULTICAST : 0)*/
157#define MAX_MULTICAST(priv) (HERMES_MAX_MULTICAST)
158
159#define ORINOCO_INTEN (HERMES_EV_RX | HERMES_EV_ALLOC \
160 | HERMES_EV_TX | HERMES_EV_TXEXC \
161 | HERMES_EV_WTERR | HERMES_EV_INFO \
David Kilroya94e8422009-02-04 23:05:44 +0000162 | HERMES_EV_INFDROP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/* Data types */
166/********************************************************************/
167
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400168/* Beginning of the Tx descriptor, used in TxExc handling */
169struct hermes_txexc_data {
170 struct hermes_tx_descriptor desc;
Pavel Roskind133ae42005-09-23 04:18:06 -0400171 __le16 frame_ctl;
172 __le16 duration_id;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200173 u8 addr1[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174} __attribute__ ((packed));
175
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200176/* Rx frame header except compatibility 802.3 header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177struct hermes_rx_descriptor {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200178 /* Control */
Pavel Roskind133ae42005-09-23 04:18:06 -0400179 __le16 status;
180 __le32 time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 u8 silence;
182 u8 signal;
183 u8 rate;
184 u8 rxflow;
Pavel Roskind133ae42005-09-23 04:18:06 -0400185 __le32 reserved;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200186
187 /* 802.11 header */
Pavel Roskind133ae42005-09-23 04:18:06 -0400188 __le16 frame_ctl;
189 __le16 duration_id;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200190 u8 addr1[ETH_ALEN];
191 u8 addr2[ETH_ALEN];
192 u8 addr3[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -0400193 __le16 seq_ctl;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200194 u8 addr4[ETH_ALEN];
195
196 /* Data length */
Pavel Roskind133ae42005-09-23 04:18:06 -0400197 __le16 data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198} __attribute__ ((packed));
199
David Kilroy47166792009-01-07 00:43:54 +0000200struct orinoco_rx_data {
201 struct hermes_rx_descriptor *desc;
202 struct sk_buff *skb;
203 struct list_head list;
204};
205
David Kilroyc63cdbe2009-06-18 23:21:33 +0100206struct orinoco_scan_data {
207 void *buf;
208 size_t len;
209 int type;
210 struct list_head list;
211};
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/********************************************************************/
214/* Function prototypes */
215/********************************************************************/
216
David Kilroy721aa2f2009-06-18 23:21:31 +0100217static int __orinoco_set_multicast_list(struct net_device *dev);
David Kilroy6415f7d2009-06-18 23:21:30 +0100218static int __orinoco_up(struct orinoco_private *priv);
219static int __orinoco_down(struct orinoco_private *priv);
David Kilroy721aa2f2009-06-18 23:21:31 +0100220static int __orinoco_commit(struct orinoco_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222/********************************************************************/
223/* Internal helper functions */
224/********************************************************************/
225
David Kilroycb1576a2009-02-04 23:05:56 +0000226void set_port_type(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 switch (priv->iw_mode) {
David Kilroy5217c572009-06-18 23:21:32 +0100229 case NL80211_IFTYPE_STATION:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 priv->port_type = 1;
231 priv->createibss = 0;
232 break;
David Kilroy5217c572009-06-18 23:21:32 +0100233 case NL80211_IFTYPE_ADHOC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (priv->prefer_port3) {
235 priv->port_type = 3;
236 priv->createibss = 0;
237 } else {
238 priv->port_type = priv->ibss_port;
239 priv->createibss = 1;
240 }
241 break;
David Kilroy5217c572009-06-18 23:21:32 +0100242 case NL80211_IFTYPE_MONITOR:
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200243 priv->port_type = 3;
244 priv->createibss = 0;
245 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 default:
247 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
248 priv->ndev->name);
249 }
250}
251
David Kilroy3994d502008-08-21 23:27:54 +0100252/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253/* Device methods */
254/********************************************************************/
255
256static int orinoco_open(struct net_device *dev)
257{
David Kilroyea60a6a2009-06-18 23:21:26 +0100258 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 unsigned long flags;
260 int err;
261
262 if (orinoco_lock(priv, &flags) != 0)
263 return -EBUSY;
264
David Kilroya2608362009-06-18 23:21:23 +0100265 err = __orinoco_up(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
David Kilroya94e8422009-02-04 23:05:44 +0000267 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 priv->open = 1;
269
270 orinoco_unlock(priv, &flags);
271
272 return err;
273}
274
Christoph Hellwigad8f4512005-05-14 17:30:17 +0200275static int orinoco_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
David Kilroyea60a6a2009-06-18 23:21:26 +0100277 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 int err = 0;
279
280 /* We mustn't use orinoco_lock() here, because we need to be
281 able to close the interface even if hw_unavailable is set
282 (e.g. as we're released after a PC Card removal) */
283 spin_lock_irq(&priv->lock);
284
285 priv->open = 0;
286
David Kilroya2608362009-06-18 23:21:23 +0100287 err = __orinoco_down(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 spin_unlock_irq(&priv->lock);
290
291 return err;
292}
293
294static struct net_device_stats *orinoco_get_stats(struct net_device *dev)
295{
David Kilroyea60a6a2009-06-18 23:21:26 +0100296 struct orinoco_private *priv = ndev_priv(dev);
David Kilroy6fe9deb2009-02-04 23:05:43 +0000297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return &priv->stats;
299}
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301static void orinoco_set_multicast_list(struct net_device *dev)
302{
David Kilroyea60a6a2009-06-18 23:21:26 +0100303 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 unsigned long flags;
305
306 if (orinoco_lock(priv, &flags) != 0) {
307 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
308 "called when hw_unavailable\n", dev->name);
309 return;
310 }
311
312 __orinoco_set_multicast_list(dev);
313 orinoco_unlock(priv, &flags);
314}
315
316static int orinoco_change_mtu(struct net_device *dev, int new_mtu)
317{
David Kilroyea60a6a2009-06-18 23:21:26 +0100318 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
David Kilroya94e8422009-02-04 23:05:44 +0000320 if ((new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return -EINVAL;
322
Johannes Berg2c7060022008-10-30 22:09:54 +0100323 /* MTU + encapsulation + header length */
David Kilroya94e8422009-02-04 23:05:44 +0000324 if ((new_mtu + ENCAPS_OVERHEAD + sizeof(struct ieee80211_hdr)) >
325 (priv->nicbuf_size - ETH_HLEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return -EINVAL;
327
328 dev->mtu = new_mtu;
329
330 return 0;
331}
332
333/********************************************************************/
334/* Tx path */
335/********************************************************************/
336
Stephen Hemmingerd0cf9c02009-08-31 19:50:57 +0000337static netdev_tx_t orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
David Kilroyea60a6a2009-06-18 23:21:26 +0100339 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct net_device_stats *stats = &priv->stats;
David Kilroy4af198f2009-08-05 21:23:32 +0100341 struct orinoco_tkip_key *key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 hermes_t *hw = &priv->hw;
343 int err = 0;
344 u16 txfid = priv->txfid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 struct ethhdr *eh;
David Kilroy6eecad72008-08-21 23:27:56 +0100346 int tx_control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 unsigned long flags;
David Kilroy4af198f2009-08-05 21:23:32 +0100348 int do_mic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
David Kilroya94e8422009-02-04 23:05:44 +0000350 if (!netif_running(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 printk(KERN_ERR "%s: Tx on stopped device!\n",
352 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400353 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
David Kilroy6fe9deb2009-02-04 23:05:43 +0000355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (netif_queue_stopped(dev)) {
David Kilroy6fe9deb2009-02-04 23:05:43 +0000357 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400359 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
David Kilroy6fe9deb2009-02-04 23:05:43 +0000361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (orinoco_lock(priv, &flags) != 0) {
363 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
364 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400365 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367
David Kilroy5217c572009-06-18 23:21:32 +0100368 if (!netif_carrier_ok(dev) ||
369 (priv->iw_mode == NL80211_IFTYPE_MONITOR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /* Oops, the firmware hasn't established a connection,
David Kilroy6fe9deb2009-02-04 23:05:43 +0000371 silently drop the packet (this seems to be the
372 safest approach). */
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400373 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
Pavel Roskin8d5be082006-04-07 04:10:41 -0400376 /* Check packet length */
Pavel Roskina28dc812006-04-07 04:10:45 -0400377 if (skb->len < ETH_HLEN)
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400378 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
David Kilroy4af198f2009-08-05 21:23:32 +0100380 key = (struct orinoco_tkip_key *) priv->keys[priv->tx_key].key;
381
382 do_mic = ((priv->encode_alg == ORINOCO_ALG_TKIP) &&
383 (key != NULL));
384
David Kilroy6eecad72008-08-21 23:27:56 +0100385 tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
David Kilroy4af198f2009-08-05 21:23:32 +0100387 if (do_mic)
David Kilroy23edcc42008-08-21 23:28:05 +0100388 tx_control |= (priv->tx_key << HERMES_MIC_KEY_ID_SHIFT) |
389 HERMES_TXCTRL_MIC;
390
David Kilroy6eecad72008-08-21 23:27:56 +0100391 if (priv->has_alt_txcntl) {
392 /* WPA enabled firmwares have tx_cntl at the end of
393 * the 802.11 header. So write zeroed descriptor and
394 * 802.11 header at the same time
395 */
396 char desc[HERMES_802_3_OFFSET];
397 __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
398
399 memset(&desc, 0, sizeof(desc));
400
401 *txcntl = cpu_to_le16(tx_control);
402 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
403 txfid, 0);
404 if (err) {
405 if (net_ratelimit())
406 printk(KERN_ERR "%s: Error %d writing Tx "
407 "descriptor to BAP\n", dev->name, err);
408 goto busy;
409 }
410 } else {
411 struct hermes_tx_descriptor desc;
412
413 memset(&desc, 0, sizeof(desc));
414
415 desc.tx_control = cpu_to_le16(tx_control);
416 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
417 txfid, 0);
418 if (err) {
419 if (net_ratelimit())
420 printk(KERN_ERR "%s: Error %d writing Tx "
421 "descriptor to BAP\n", dev->name, err);
422 goto busy;
423 }
424
425 /* Clear the 802.11 header and data length fields - some
426 * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
427 * if this isn't done. */
428 hermes_clear_words(hw, HERMES_DATA0,
429 HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
David Kilroy23edcc42008-08-21 23:28:05 +0100432 eh = (struct ethhdr *)skb->data;
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /* Encapsulate Ethernet-II frames */
435 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
Pavel Roskina28dc812006-04-07 04:10:45 -0400436 struct header_struct {
437 struct ethhdr eth; /* 802.3 header */
438 u8 encap[6]; /* 802.2 header */
439 } __attribute__ ((packed)) hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Pavel Roskina28dc812006-04-07 04:10:45 -0400441 /* Strip destination and source from the data */
442 skb_pull(skb, 2 * ETH_ALEN);
Pavel Roskina28dc812006-04-07 04:10:45 -0400443
444 /* And move them to a separate header */
445 memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
446 hdr.eth.h_proto = htons(sizeof(encaps_hdr) + skb->len);
447 memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
448
David Kilroy23edcc42008-08-21 23:28:05 +0100449 /* Insert the SNAP header */
450 if (skb_headroom(skb) < sizeof(hdr)) {
451 printk(KERN_ERR
452 "%s: Not enough headroom for 802.2 headers %d\n",
453 dev->name, skb_headroom(skb));
454 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
David Kilroy23edcc42008-08-21 23:28:05 +0100456 eh = (struct ethhdr *) skb_push(skb, sizeof(hdr));
457 memcpy(eh, &hdr, sizeof(hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459
Pavel Roskina28dc812006-04-07 04:10:45 -0400460 err = hermes_bap_pwrite(hw, USER_BAP, skb->data, skb->len,
David Kilroy23edcc42008-08-21 23:28:05 +0100461 txfid, HERMES_802_3_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (err) {
463 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
464 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400465 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467
David Kilroy23edcc42008-08-21 23:28:05 +0100468 /* Calculate Michael MIC */
David Kilroy4af198f2009-08-05 21:23:32 +0100469 if (do_mic) {
David Kilroy23edcc42008-08-21 23:28:05 +0100470 u8 mic_buf[MICHAEL_MIC_LEN + 1];
471 u8 *mic;
472 size_t offset;
473 size_t len;
474
475 if (skb->len % 2) {
476 /* MIC start is on an odd boundary */
477 mic_buf[0] = skb->data[skb->len - 1];
478 mic = &mic_buf[1];
479 offset = skb->len - 1;
480 len = MICHAEL_MIC_LEN + 1;
481 } else {
482 mic = &mic_buf[0];
483 offset = skb->len;
484 len = MICHAEL_MIC_LEN;
485 }
486
David Kilroy4af198f2009-08-05 21:23:32 +0100487 orinoco_mic(priv->tx_tfm_mic, key->tx_mic,
David Kilroy23edcc42008-08-21 23:28:05 +0100488 eh->h_dest, eh->h_source, 0 /* priority */,
489 skb->data + ETH_HLEN, skb->len - ETH_HLEN, mic);
490
491 /* Write the MIC */
492 err = hermes_bap_pwrite(hw, USER_BAP, &mic_buf[0], len,
493 txfid, HERMES_802_3_OFFSET + offset);
494 if (err) {
495 printk(KERN_ERR "%s: Error %d writing MIC to BAP\n",
496 dev->name, err);
497 goto busy;
498 }
499 }
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 /* Finally, we actually initiate the send */
502 netif_stop_queue(dev);
503
504 err = hermes_docmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
505 txfid, NULL);
506 if (err) {
507 netif_start_queue(dev);
Andrew Mortonc367c212005-10-19 21:23:44 -0700508 if (net_ratelimit())
509 printk(KERN_ERR "%s: Error %d transmitting packet\n",
510 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400511 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513
514 dev->trans_start = jiffies;
David Kilroy23edcc42008-08-21 23:28:05 +0100515 stats->tx_bytes += HERMES_802_3_OFFSET + skb->len;
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400516 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400518 drop:
519 stats->tx_errors++;
520 stats->tx_dropped++;
521
522 ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 dev_kfree_skb(skb);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400525 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400527 busy:
Jiri Benc2c1bd262006-04-07 04:10:47 -0400528 if (err == -EIO)
529 schedule_work(&priv->reset_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 orinoco_unlock(priv, &flags);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400531 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
534static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
535{
David Kilroyea60a6a2009-06-18 23:21:26 +0100536 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 u16 fid = hermes_read_regn(hw, ALLOCFID);
538
539 if (fid != priv->txfid) {
540 if (fid != DUMMY_FID)
541 printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
542 dev->name, fid);
543 return;
544 }
545
546 hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
547}
548
549static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
550{
David Kilroyea60a6a2009-06-18 23:21:26 +0100551 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 struct net_device_stats *stats = &priv->stats;
553
554 stats->tx_packets++;
555
556 netif_wake_queue(dev);
557
558 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
559}
560
561static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
562{
David Kilroyea60a6a2009-06-18 23:21:26 +0100563 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 struct net_device_stats *stats = &priv->stats;
565 u16 fid = hermes_read_regn(hw, TXCOMPLFID);
Pavel Roskind133ae42005-09-23 04:18:06 -0400566 u16 status;
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400567 struct hermes_txexc_data hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 int err = 0;
569
570 if (fid == DUMMY_FID)
571 return; /* Nothing's really happened */
572
Pavel Roskin48ca7032005-09-23 04:18:06 -0400573 /* Read part of the frame header - we need status and addr1 */
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200574 err = hermes_bap_pread(hw, IRQ_BAP, &hdr,
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400575 sizeof(struct hermes_txexc_data),
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200576 fid, 0);
577
578 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
579 stats->tx_errors++;
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (err) {
582 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
583 "(FID=%04X error %d)\n",
584 dev->name, fid, err);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200585 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
David Kilroy6fe9deb2009-02-04 23:05:43 +0000587
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200588 DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
589 err, fid);
David Kilroy6fe9deb2009-02-04 23:05:43 +0000590
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200591 /* We produce a TXDROP event only for retry or lifetime
592 * exceeded, because that's the only status that really mean
593 * that this particular node went away.
594 * Other errors means that *we* screwed up. - Jean II */
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400595 status = le16_to_cpu(hdr.desc.status);
Pavel Roskind133ae42005-09-23 04:18:06 -0400596 if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200597 union iwreq_data wrqu;
598
599 /* Copy 802.11 dest address.
600 * We use the 802.11 header because the frame may
601 * not be 802.3 or may be mangled...
602 * In Ad-Hoc mode, it will be the node address.
603 * In managed mode, it will be most likely the AP addr
604 * User space will figure out how to convert it to
605 * whatever it needs (IP address or else).
606 * - Jean II */
607 memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
608 wrqu.addr.sa_family = ARPHRD_ETHER;
609
610 /* Send event to user space */
611 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
617static void orinoco_tx_timeout(struct net_device *dev)
618{
David Kilroyea60a6a2009-06-18 23:21:26 +0100619 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 struct net_device_stats *stats = &priv->stats;
621 struct hermes *hw = &priv->hw;
622
623 printk(KERN_WARNING "%s: Tx timeout! "
624 "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
625 dev->name, hermes_read_regn(hw, ALLOCFID),
626 hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
627
628 stats->tx_errors++;
629
630 schedule_work(&priv->reset_work);
631}
632
633/********************************************************************/
634/* Rx path (data frames) */
635/********************************************************************/
636
637/* Does the frame have a SNAP header indicating it should be
638 * de-encapsulated to Ethernet-II? */
639static inline int is_ethersnap(void *_hdr)
640{
641 u8 *hdr = _hdr;
642
643 /* We de-encapsulate all packets which, a) have SNAP headers
644 * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
645 * and where b) the OUI of the SNAP header is 00:00:00 or
646 * 00:00:f8 - we need both because different APs appear to use
647 * different OUIs for some reason */
648 return (memcmp(hdr, &encaps_hdr, 5) == 0)
David Kilroya94e8422009-02-04 23:05:44 +0000649 && ((hdr[5] == 0x00) || (hdr[5] == 0xf8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
653 int level, int noise)
654{
Pavel Roskin343c6862005-09-09 18:43:02 -0400655 struct iw_quality wstats;
656 wstats.level = level - 0x95;
657 wstats.noise = noise - 0x95;
658 wstats.qual = (level > noise) ? (level - noise) : 0;
Andrey Borzenkovf941f852008-11-15 17:15:09 +0300659 wstats.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
Pavel Roskin343c6862005-09-09 18:43:02 -0400660 /* Update spy records */
661 wireless_spy_update(dev, mac, &wstats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
664static void orinoco_stat_gather(struct net_device *dev,
665 struct sk_buff *skb,
666 struct hermes_rx_descriptor *desc)
667{
David Kilroyea60a6a2009-06-18 23:21:26 +0100668 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 /* Using spy support with lots of Rx packets, like in an
671 * infrastructure (AP), will really slow down everything, because
672 * the MAC address must be compared to each entry of the spy list.
673 * If the user really asks for it (set some address in the
674 * spy list), we do it, but he will pay the price.
675 * Note that to get here, you need both WIRELESS_SPY
676 * compiled in AND some addresses in the list !!!
677 */
678 /* Note : gcc will optimise the whole section away if
679 * WIRELESS_SPY is not defined... - Jean II */
680 if (SPY_NUMBER(priv)) {
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700681 orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 desc->signal, desc->silence);
683 }
684}
685
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200686/*
687 * orinoco_rx_monitor - handle received monitor frames.
688 *
689 * Arguments:
690 * dev network device
691 * rxfid received FID
692 * desc rx descriptor of the frame
693 *
694 * Call context: interrupt
695 */
696static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
697 struct hermes_rx_descriptor *desc)
698{
699 u32 hdrlen = 30; /* return full header by default */
700 u32 datalen = 0;
701 u16 fc;
702 int err;
703 int len;
704 struct sk_buff *skb;
David Kilroyea60a6a2009-06-18 23:21:26 +0100705 struct orinoco_private *priv = ndev_priv(dev);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200706 struct net_device_stats *stats = &priv->stats;
707 hermes_t *hw = &priv->hw;
708
709 len = le16_to_cpu(desc->data_len);
710
711 /* Determine the size of the header and the data */
712 fc = le16_to_cpu(desc->frame_ctl);
713 switch (fc & IEEE80211_FCTL_FTYPE) {
714 case IEEE80211_FTYPE_DATA:
715 if ((fc & IEEE80211_FCTL_TODS)
716 && (fc & IEEE80211_FCTL_FROMDS))
717 hdrlen = 30;
718 else
719 hdrlen = 24;
720 datalen = len;
721 break;
722 case IEEE80211_FTYPE_MGMT:
723 hdrlen = 24;
724 datalen = len;
725 break;
726 case IEEE80211_FTYPE_CTL:
727 switch (fc & IEEE80211_FCTL_STYPE) {
728 case IEEE80211_STYPE_PSPOLL:
729 case IEEE80211_STYPE_RTS:
730 case IEEE80211_STYPE_CFEND:
731 case IEEE80211_STYPE_CFENDACK:
732 hdrlen = 16;
733 break;
734 case IEEE80211_STYPE_CTS:
735 case IEEE80211_STYPE_ACK:
736 hdrlen = 10;
737 break;
738 }
739 break;
740 default:
741 /* Unknown frame type */
742 break;
743 }
744
745 /* sanity check the length */
Johannes Berg2c7060022008-10-30 22:09:54 +0100746 if (datalen > IEEE80211_MAX_DATA_LEN + 12) {
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200747 printk(KERN_DEBUG "%s: oversized monitor frame, "
748 "data length = %d\n", dev->name, datalen);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200749 stats->rx_length_errors++;
750 goto update_stats;
751 }
752
753 skb = dev_alloc_skb(hdrlen + datalen);
754 if (!skb) {
755 printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
756 dev->name);
Florin Malitabb6e0932006-05-22 22:35:30 -0700757 goto update_stats;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200758 }
759
760 /* Copy the 802.11 header to the skb */
761 memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700762 skb_reset_mac_header(skb);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200763
764 /* If any, copy the data from the card to the skb */
765 if (datalen > 0) {
766 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
767 ALIGN(datalen, 2), rxfid,
768 HERMES_802_2_OFFSET);
769 if (err) {
770 printk(KERN_ERR "%s: error %d reading monitor frame\n",
771 dev->name, err);
772 goto drop;
773 }
774 }
775
776 skb->dev = dev;
777 skb->ip_summed = CHECKSUM_NONE;
778 skb->pkt_type = PACKET_OTHERHOST;
Harvey Harrisonc1b4aa32009-01-29 13:26:44 -0800779 skb->protocol = cpu_to_be16(ETH_P_802_2);
David Kilroy6fe9deb2009-02-04 23:05:43 +0000780
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200781 stats->rx_packets++;
782 stats->rx_bytes += skb->len;
783
784 netif_rx(skb);
785 return;
786
787 drop:
788 dev_kfree_skb_irq(skb);
789 update_stats:
790 stats->rx_errors++;
791 stats->rx_dropped++;
792}
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794static void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
795{
David Kilroyea60a6a2009-06-18 23:21:26 +0100796 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 struct net_device_stats *stats = &priv->stats;
798 struct iw_statistics *wstats = &priv->wstats;
799 struct sk_buff *skb = NULL;
David Kilroy31afcef2008-08-21 23:28:04 +0100800 u16 rxfid, status;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200801 int length;
David Kilroy31afcef2008-08-21 23:28:04 +0100802 struct hermes_rx_descriptor *desc;
803 struct orinoco_rx_data *rx_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 int err;
805
David Kilroy31afcef2008-08-21 23:28:04 +0100806 desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
807 if (!desc) {
808 printk(KERN_WARNING
809 "%s: Can't allocate space for RX descriptor\n",
810 dev->name);
811 goto update_stats;
812 }
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 rxfid = hermes_read_regn(hw, RXFID);
815
David Kilroy31afcef2008-08-21 23:28:04 +0100816 err = hermes_bap_pread(hw, IRQ_BAP, desc, sizeof(*desc),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 rxfid, 0);
818 if (err) {
819 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
820 "Frame dropped.\n", dev->name, err);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200821 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823
David Kilroy31afcef2008-08-21 23:28:04 +0100824 status = le16_to_cpu(desc->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200826 if (status & HERMES_RXSTAT_BADCRC) {
827 DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
828 dev->name);
829 stats->rx_crc_errors++;
830 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200833 /* Handle frames in monitor mode */
David Kilroy5217c572009-06-18 23:21:32 +0100834 if (priv->iw_mode == NL80211_IFTYPE_MONITOR) {
David Kilroy31afcef2008-08-21 23:28:04 +0100835 orinoco_rx_monitor(dev, rxfid, desc);
836 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200839 if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
840 DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
841 dev->name);
842 wstats->discard.code++;
843 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845
David Kilroy31afcef2008-08-21 23:28:04 +0100846 length = le16_to_cpu(desc->data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 /* Sanity checks */
849 if (length < 3) { /* No for even an 802.2 LLC header */
850 /* At least on Symbol firmware with PCF we get quite a
David Kilroy6fe9deb2009-02-04 23:05:43 +0000851 lot of these legitimately - Poll frames with no
852 data. */
David Kilroy31afcef2008-08-21 23:28:04 +0100853 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
Johannes Berg2c7060022008-10-30 22:09:54 +0100855 if (length > IEEE80211_MAX_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
857 dev->name, length);
858 stats->rx_length_errors++;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200859 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
861
David Kilroy23edcc42008-08-21 23:28:05 +0100862 /* Payload size does not include Michael MIC. Increase payload
863 * size to read it together with the data. */
864 if (status & HERMES_RXSTAT_MIC)
865 length += MICHAEL_MIC_LEN;
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 /* We need space for the packet data itself, plus an ethernet
868 header, plus 2 bytes so we can align the IP header on a
869 32bit boundary, plus 1 byte so we can read in odd length
870 packets from the card, which has an IO granularity of 16
David Kilroy6fe9deb2009-02-04 23:05:43 +0000871 bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 skb = dev_alloc_skb(length+ETH_HLEN+2+1);
873 if (!skb) {
874 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
875 dev->name);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200876 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200879 /* We'll prepend the header, so reserve space for it. The worst
880 case is no decapsulation, when 802.3 header is prepended and
881 nothing is removed. 2 is for aligning the IP header. */
882 skb_reserve(skb, ETH_HLEN + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200884 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, length),
885 ALIGN(length, 2), rxfid,
886 HERMES_802_2_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (err) {
888 printk(KERN_ERR "%s: error %d reading frame. "
889 "Frame dropped.\n", dev->name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 goto drop;
891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
David Kilroy31afcef2008-08-21 23:28:04 +0100893 /* Add desc and skb to rx queue */
894 rx_data = kzalloc(sizeof(*rx_data), GFP_ATOMIC);
895 if (!rx_data) {
896 printk(KERN_WARNING "%s: Can't allocate RX packet\n",
897 dev->name);
898 goto drop;
899 }
900 rx_data->desc = desc;
901 rx_data->skb = skb;
902 list_add_tail(&rx_data->list, &priv->rx_list);
903 tasklet_schedule(&priv->rx_tasklet);
904
905 return;
906
907drop:
908 dev_kfree_skb_irq(skb);
909update_stats:
910 stats->rx_errors++;
911 stats->rx_dropped++;
912out:
913 kfree(desc);
914}
915
916static void orinoco_rx(struct net_device *dev,
917 struct hermes_rx_descriptor *desc,
918 struct sk_buff *skb)
919{
David Kilroyea60a6a2009-06-18 23:21:26 +0100920 struct orinoco_private *priv = ndev_priv(dev);
David Kilroy31afcef2008-08-21 23:28:04 +0100921 struct net_device_stats *stats = &priv->stats;
922 u16 status, fc;
923 int length;
924 struct ethhdr *hdr;
925
926 status = le16_to_cpu(desc->status);
927 length = le16_to_cpu(desc->data_len);
928 fc = le16_to_cpu(desc->frame_ctl);
929
David Kilroy23edcc42008-08-21 23:28:05 +0100930 /* Calculate and check MIC */
931 if (status & HERMES_RXSTAT_MIC) {
David Kilroy4af198f2009-08-05 21:23:32 +0100932 struct orinoco_tkip_key *key;
David Kilroy23edcc42008-08-21 23:28:05 +0100933 int key_id = ((status & HERMES_RXSTAT_MIC_KEY_ID) >>
934 HERMES_MIC_KEY_ID_SHIFT);
935 u8 mic[MICHAEL_MIC_LEN];
936 u8 *rxmic;
937 u8 *src = (fc & IEEE80211_FCTL_FROMDS) ?
938 desc->addr3 : desc->addr2;
939
940 /* Extract Michael MIC from payload */
941 rxmic = skb->data + skb->len - MICHAEL_MIC_LEN;
942
943 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
944 length -= MICHAEL_MIC_LEN;
945
David Kilroy4af198f2009-08-05 21:23:32 +0100946 key = (struct orinoco_tkip_key *) priv->keys[key_id].key;
947
948 if (!key) {
949 printk(KERN_WARNING "%s: Received encrypted frame from "
950 "%pM using key %i, but key is not installed\n",
951 dev->name, src, key_id);
952 goto drop;
953 }
954
955 orinoco_mic(priv->rx_tfm_mic, key->rx_mic, desc->addr1, src,
David Kilroy23edcc42008-08-21 23:28:05 +0100956 0, /* priority or QoS? */
David Kilroy4af198f2009-08-05 21:23:32 +0100957 skb->data, skb->len, &mic[0]);
David Kilroy23edcc42008-08-21 23:28:05 +0100958
959 if (memcmp(mic, rxmic,
960 MICHAEL_MIC_LEN)) {
961 union iwreq_data wrqu;
962 struct iw_michaelmicfailure wxmic;
David Kilroy23edcc42008-08-21 23:28:05 +0100963
964 printk(KERN_WARNING "%s: "
Johannes Berge1749612008-10-27 15:59:26 -0700965 "Invalid Michael MIC in data frame from %pM, "
David Kilroy23edcc42008-08-21 23:28:05 +0100966 "using key %i\n",
Johannes Berge1749612008-10-27 15:59:26 -0700967 dev->name, src, key_id);
David Kilroy23edcc42008-08-21 23:28:05 +0100968
969 /* TODO: update stats */
970
971 /* Notify userspace */
972 memset(&wxmic, 0, sizeof(wxmic));
973 wxmic.flags = key_id & IW_MICFAILURE_KEY_ID;
974 wxmic.flags |= (desc->addr1[0] & 1) ?
975 IW_MICFAILURE_GROUP : IW_MICFAILURE_PAIRWISE;
976 wxmic.src_addr.sa_family = ARPHRD_ETHER;
977 memcpy(wxmic.src_addr.sa_data, src, ETH_ALEN);
978
979 (void) orinoco_hw_get_tkip_iv(priv, key_id,
980 &wxmic.tsc[0]);
981
982 memset(&wrqu, 0, sizeof(wrqu));
983 wrqu.data.length = sizeof(wxmic);
984 wireless_send_event(dev, IWEVMICHAELMICFAILURE, &wrqu,
985 (char *) &wxmic);
986
987 goto drop;
988 }
989 }
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 /* Handle decapsulation
992 * In most cases, the firmware tell us about SNAP frames.
993 * For some reason, the SNAP frames sent by LinkSys APs
994 * are not properly recognised by most firmwares.
995 * So, check ourselves */
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200996 if (length >= ENCAPS_OVERHEAD &&
997 (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
998 ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
999 is_ethersnap(skb->data))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 /* These indicate a SNAP within 802.2 LLC within
1001 802.11 frame which we'll need to de-encapsulate to
1002 the original EthernetII frame. */
David Kilroyb2f30a02009-02-04 23:05:46 +00001003 hdr = (struct ethhdr *)skb_push(skb,
1004 ETH_HLEN - ENCAPS_OVERHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 } else {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001006 /* 802.3 frame - prepend 802.3 header as is */
1007 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
1008 hdr->h_proto = htons(length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
David Kilroy31afcef2008-08-21 23:28:04 +01001010 memcpy(hdr->h_dest, desc->addr1, ETH_ALEN);
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001011 if (fc & IEEE80211_FCTL_FROMDS)
David Kilroy31afcef2008-08-21 23:28:04 +01001012 memcpy(hdr->h_source, desc->addr3, ETH_ALEN);
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001013 else
David Kilroy31afcef2008-08-21 23:28:04 +01001014 memcpy(hdr->h_source, desc->addr2, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 skb->protocol = eth_type_trans(skb, dev);
1017 skb->ip_summed = CHECKSUM_NONE;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001018 if (fc & IEEE80211_FCTL_TODS)
1019 skb->pkt_type = PACKET_OTHERHOST;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 /* Process the wireless stats if needed */
David Kilroy31afcef2008-08-21 23:28:04 +01001022 orinoco_stat_gather(dev, skb, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 /* Pass the packet to the networking stack */
1025 netif_rx(skb);
1026 stats->rx_packets++;
1027 stats->rx_bytes += length;
1028
1029 return;
David Kilroy23edcc42008-08-21 23:28:05 +01001030
1031 drop:
1032 dev_kfree_skb(skb);
1033 stats->rx_errors++;
1034 stats->rx_dropped++;
David Kilroy31afcef2008-08-21 23:28:04 +01001035}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
David Kilroy31afcef2008-08-21 23:28:04 +01001037static void orinoco_rx_isr_tasklet(unsigned long data)
1038{
David Kilroy53819562009-06-18 23:21:28 +01001039 struct orinoco_private *priv = (struct orinoco_private *) data;
1040 struct net_device *dev = priv->ndev;
David Kilroy31afcef2008-08-21 23:28:04 +01001041 struct orinoco_rx_data *rx_data, *temp;
1042 struct hermes_rx_descriptor *desc;
1043 struct sk_buff *skb;
David Kilroy20953ad2009-01-07 00:23:55 +00001044 unsigned long flags;
1045
1046 /* orinoco_rx requires the driver lock, and we also need to
1047 * protect priv->rx_list, so just hold the lock over the
1048 * lot.
1049 *
1050 * If orinoco_lock fails, we've unplugged the card. In this
1051 * case just abort. */
1052 if (orinoco_lock(priv, &flags) != 0)
1053 return;
David Kilroy31afcef2008-08-21 23:28:04 +01001054
1055 /* extract desc and skb from queue */
1056 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
1057 desc = rx_data->desc;
1058 skb = rx_data->skb;
1059 list_del(&rx_data->list);
1060 kfree(rx_data);
1061
1062 orinoco_rx(dev, desc, skb);
1063
1064 kfree(desc);
1065 }
David Kilroy20953ad2009-01-07 00:23:55 +00001066
1067 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
1070/********************************************************************/
1071/* Rx path (info frames) */
1072/********************************************************************/
1073
1074static void print_linkstatus(struct net_device *dev, u16 status)
1075{
David Kilroy21312662009-02-04 23:05:47 +00001076 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 if (suppress_linkstatus)
1079 return;
1080
1081 switch (status) {
1082 case HERMES_LINKSTATUS_NOT_CONNECTED:
1083 s = "Not Connected";
1084 break;
1085 case HERMES_LINKSTATUS_CONNECTED:
1086 s = "Connected";
1087 break;
1088 case HERMES_LINKSTATUS_DISCONNECTED:
1089 s = "Disconnected";
1090 break;
1091 case HERMES_LINKSTATUS_AP_CHANGE:
1092 s = "AP Changed";
1093 break;
1094 case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1095 s = "AP Out of Range";
1096 break;
1097 case HERMES_LINKSTATUS_AP_IN_RANGE:
1098 s = "AP In Range";
1099 break;
1100 case HERMES_LINKSTATUS_ASSOC_FAILED:
1101 s = "Association Failed";
1102 break;
1103 default:
1104 s = "UNKNOWN";
1105 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001106
Pavel Roskin11eaea42009-01-18 23:20:58 -05001107 printk(KERN_DEBUG "%s: New link status: %s (%04x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 dev->name, s, status);
1109}
1110
Christoph Hellwig16739b02005-06-19 01:27:51 +02001111/* Search scan results for requested BSSID, join it if found */
David Howellsc4028952006-11-22 14:57:56 +00001112static void orinoco_join_ap(struct work_struct *work)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001113{
David Howellsc4028952006-11-22 14:57:56 +00001114 struct orinoco_private *priv =
1115 container_of(work, struct orinoco_private, join_work);
1116 struct net_device *dev = priv->ndev;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001117 struct hermes *hw = &priv->hw;
1118 int err;
1119 unsigned long flags;
1120 struct join_req {
1121 u8 bssid[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -04001122 __le16 channel;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001123 } __attribute__ ((packed)) req;
1124 const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001125 struct prism2_scan_apinfo *atom = NULL;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001126 int offset = 4;
Pavel Roskinc89cc222005-09-01 20:06:06 -04001127 int found = 0;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001128 u8 *buf;
1129 u16 len;
1130
1131 /* Allocate buffer for scan results */
1132 buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
David Kilroya94e8422009-02-04 23:05:44 +00001133 if (!buf)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001134 return;
1135
1136 if (orinoco_lock(priv, &flags) != 0)
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001137 goto fail_lock;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001138
1139 /* Sanity checks in case user changed something in the meantime */
David Kilroya94e8422009-02-04 23:05:44 +00001140 if (!priv->bssid_fixed)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001141 goto out;
1142
1143 if (strlen(priv->desired_essid) == 0)
1144 goto out;
1145
1146 /* Read scan results from the firmware */
1147 err = hermes_read_ltv(hw, USER_BAP,
1148 HERMES_RID_SCANRESULTSTABLE,
1149 MAX_SCAN_LEN, &len, buf);
1150 if (err) {
1151 printk(KERN_ERR "%s: Cannot read scan results\n",
1152 dev->name);
1153 goto out;
1154 }
1155
1156 len = HERMES_RECLEN_TO_BYTES(len);
1157
1158 /* Go through the scan results looking for the channel of the AP
1159 * we were requested to join */
1160 for (; offset + atom_len <= len; offset += atom_len) {
1161 atom = (struct prism2_scan_apinfo *) (buf + offset);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001162 if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1163 found = 1;
1164 break;
1165 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001166 }
1167
David Kilroya94e8422009-02-04 23:05:44 +00001168 if (!found) {
Pavel Roskinc89cc222005-09-01 20:06:06 -04001169 DEBUG(1, "%s: Requested AP not found in scan results\n",
1170 dev->name);
1171 goto out;
1172 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001173
Christoph Hellwig16739b02005-06-19 01:27:51 +02001174 memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1175 req.channel = atom->channel; /* both are little-endian */
1176 err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1177 &req);
1178 if (err)
1179 printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1180
1181 out:
Christoph Hellwig16739b02005-06-19 01:27:51 +02001182 orinoco_unlock(priv, &flags);
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001183
1184 fail_lock:
1185 kfree(buf);
Christoph Hellwig16739b02005-06-19 01:27:51 +02001186}
1187
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001188/* Send new BSSID to userspace */
David Kilroy6cd90b12008-08-21 23:28:00 +01001189static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001190{
David Howellsc4028952006-11-22 14:57:56 +00001191 struct net_device *dev = priv->ndev;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001192 struct hermes *hw = &priv->hw;
1193 union iwreq_data wrqu;
1194 int err;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001195
David Kilroy499b7022008-12-09 21:46:29 +00001196 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001197 ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
1198 if (err != 0)
David Kilroy6cd90b12008-08-21 23:28:00 +01001199 return;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001200
1201 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1202
1203 /* Send event to user space */
1204 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001205}
1206
David Kilroy06009fd2008-08-21 23:28:03 +01001207static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
1208{
1209 struct net_device *dev = priv->ndev;
1210 struct hermes *hw = &priv->hw;
1211 union iwreq_data wrqu;
1212 int err;
1213 u8 buf[88];
1214 u8 *ie;
1215
1216 if (!priv->has_wpa)
1217 return;
1218
David Kilroy499b7022008-12-09 21:46:29 +00001219 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
David Kilroy06009fd2008-08-21 23:28:03 +01001220 sizeof(buf), NULL, &buf);
1221 if (err != 0)
1222 return;
1223
1224 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1225 if (ie) {
1226 int rem = sizeof(buf) - (ie - &buf[0]);
1227 wrqu.data.length = ie[1] + 2;
1228 if (wrqu.data.length > rem)
1229 wrqu.data.length = rem;
1230
1231 if (wrqu.data.length)
1232 /* Send event to user space */
1233 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
1234 }
1235}
1236
1237static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
1238{
1239 struct net_device *dev = priv->ndev;
1240 struct hermes *hw = &priv->hw;
1241 union iwreq_data wrqu;
1242 int err;
1243 u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
1244 u8 *ie;
1245
1246 if (!priv->has_wpa)
1247 return;
1248
David Kilroy499b7022008-12-09 21:46:29 +00001249 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_RESP_INFO,
David Kilroy06009fd2008-08-21 23:28:03 +01001250 sizeof(buf), NULL, &buf);
1251 if (err != 0)
1252 return;
1253
1254 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1255 if (ie) {
1256 int rem = sizeof(buf) - (ie - &buf[0]);
1257 wrqu.data.length = ie[1] + 2;
1258 if (wrqu.data.length > rem)
1259 wrqu.data.length = rem;
1260
1261 if (wrqu.data.length)
1262 /* Send event to user space */
1263 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
1264 }
1265}
1266
David Kilroy6cd90b12008-08-21 23:28:00 +01001267static void orinoco_send_wevents(struct work_struct *work)
1268{
1269 struct orinoco_private *priv =
1270 container_of(work, struct orinoco_private, wevent_work);
1271 unsigned long flags;
1272
1273 if (orinoco_lock(priv, &flags) != 0)
1274 return;
1275
David Kilroy06009fd2008-08-21 23:28:03 +01001276 orinoco_send_assocreqie_wevent(priv);
1277 orinoco_send_assocrespie_wevent(priv);
David Kilroy6cd90b12008-08-21 23:28:00 +01001278 orinoco_send_bssid_wevent(priv);
1279
1280 orinoco_unlock(priv, &flags);
1281}
Dan Williams1e3428e2007-10-10 23:56:25 -04001282
David Kilroyc63cdbe2009-06-18 23:21:33 +01001283static void qbuf_scan(struct orinoco_private *priv, void *buf,
1284 int len, int type)
1285{
1286 struct orinoco_scan_data *sd;
1287 unsigned long flags;
1288
1289 sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
1290 sd->buf = buf;
1291 sd->len = len;
1292 sd->type = type;
1293
1294 spin_lock_irqsave(&priv->scan_lock, flags);
1295 list_add_tail(&sd->list, &priv->scan_list);
1296 spin_unlock_irqrestore(&priv->scan_lock, flags);
1297
1298 schedule_work(&priv->process_scan);
1299}
1300
1301static void qabort_scan(struct orinoco_private *priv)
1302{
1303 struct orinoco_scan_data *sd;
1304 unsigned long flags;
1305
1306 sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
1307 sd->len = -1; /* Abort */
1308
1309 spin_lock_irqsave(&priv->scan_lock, flags);
1310 list_add_tail(&sd->list, &priv->scan_list);
1311 spin_unlock_irqrestore(&priv->scan_lock, flags);
1312
1313 schedule_work(&priv->process_scan);
1314}
1315
1316static void orinoco_process_scan_results(struct work_struct *work)
1317{
1318 struct orinoco_private *priv =
1319 container_of(work, struct orinoco_private, process_scan);
1320 struct orinoco_scan_data *sd, *temp;
1321 unsigned long flags;
1322 void *buf;
1323 int len;
1324 int type;
1325
1326 spin_lock_irqsave(&priv->scan_lock, flags);
1327 list_for_each_entry_safe(sd, temp, &priv->scan_list, list) {
1328 spin_unlock_irqrestore(&priv->scan_lock, flags);
1329
1330 buf = sd->buf;
1331 len = sd->len;
1332 type = sd->type;
1333
1334 list_del(&sd->list);
1335 kfree(sd);
1336
1337 if (len > 0) {
1338 if (type == HERMES_INQ_CHANNELINFO)
1339 orinoco_add_extscan_result(priv, buf, len);
1340 else
1341 orinoco_add_hostscan_results(priv, buf, len);
1342
1343 kfree(buf);
1344 } else if (priv->scan_request) {
1345 /* Either abort or complete the scan */
1346 cfg80211_scan_done(priv->scan_request, (len < 0));
1347 priv->scan_request = NULL;
1348 }
1349
1350 spin_lock_irqsave(&priv->scan_lock, flags);
1351 }
1352 spin_unlock_irqrestore(&priv->scan_lock, flags);
1353}
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355static void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
1356{
David Kilroyea60a6a2009-06-18 23:21:26 +01001357 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 u16 infofid;
1359 struct {
Pavel Roskind133ae42005-09-23 04:18:06 -04001360 __le16 len;
1361 __le16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 } __attribute__ ((packed)) info;
1363 int len, type;
1364 int err;
1365
1366 /* This is an answer to an INQUIRE command that we did earlier,
1367 * or an information "event" generated by the card
1368 * The controller return to us a pseudo frame containing
1369 * the information in question - Jean II */
1370 infofid = hermes_read_regn(hw, INFOFID);
1371
1372 /* Read the info frame header - don't try too hard */
1373 err = hermes_bap_pread(hw, IRQ_BAP, &info, sizeof(info),
1374 infofid, 0);
1375 if (err) {
1376 printk(KERN_ERR "%s: error %d reading info frame. "
1377 "Frame dropped.\n", dev->name, err);
1378 return;
1379 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
1382 type = le16_to_cpu(info.type);
1383
1384 switch (type) {
1385 case HERMES_INQ_TALLIES: {
1386 struct hermes_tallies_frame tallies;
1387 struct iw_statistics *wstats = &priv->wstats;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (len > sizeof(tallies)) {
1390 printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
1391 dev->name, len);
1392 len = sizeof(tallies);
1393 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001394
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001395 err = hermes_bap_pread(hw, IRQ_BAP, &tallies, len,
1396 infofid, sizeof(info));
1397 if (err)
1398 break;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 /* Increment our various counters */
1401 /* wstats->discard.nwid - no wrong BSSID stuff */
1402 wstats->discard.code +=
1403 le16_to_cpu(tallies.RxWEPUndecryptable);
David Kilroy6fe9deb2009-02-04 23:05:43 +00001404 if (len == sizeof(tallies))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 wstats->discard.code +=
1406 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
1407 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
1408 wstats->discard.misc +=
1409 le16_to_cpu(tallies.TxDiscardsWrongSA);
1410 wstats->discard.fragment +=
1411 le16_to_cpu(tallies.RxMsgInBadMsgFragments);
1412 wstats->discard.retries +=
1413 le16_to_cpu(tallies.TxRetryLimitExceeded);
1414 /* wstats->miss.beacon - no match */
1415 }
1416 break;
1417 case HERMES_INQ_LINKSTATUS: {
1418 struct hermes_linkstatus linkstatus;
1419 u16 newstatus;
1420 int connected;
1421
David Kilroy5217c572009-06-18 23:21:32 +01001422 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001423 break;
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (len != sizeof(linkstatus)) {
1426 printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
1427 dev->name, len);
1428 break;
1429 }
1430
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001431 err = hermes_bap_pread(hw, IRQ_BAP, &linkstatus, len,
1432 infofid, sizeof(info));
1433 if (err)
1434 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 newstatus = le16_to_cpu(linkstatus.linkstatus);
1436
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001437 /* Symbol firmware uses "out of range" to signal that
1438 * the hostscan frame can be requested. */
1439 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
1440 priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
David Kilroyc63cdbe2009-06-18 23:21:33 +01001441 priv->has_hostscan && priv->scan_request) {
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001442 hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
1443 break;
1444 }
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
1447 || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
1448 || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
1449
1450 if (connected)
1451 netif_carrier_on(dev);
David Gibson7bb7c3a2005-05-12 20:02:10 -04001452 else if (!ignore_disconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 netif_carrier_off(dev);
1454
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001455 if (newstatus != priv->last_linkstatus) {
1456 priv->last_linkstatus = newstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 print_linkstatus(dev, newstatus);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001458 /* The info frame contains only one word which is the
1459 * status (see hermes.h). The status is pretty boring
1460 * in itself, that's why we export the new BSSID...
1461 * Jean II */
1462 schedule_work(&priv->wevent_work);
1463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
1465 break;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001466 case HERMES_INQ_SCAN:
David Kilroyc63cdbe2009-06-18 23:21:33 +01001467 if (!priv->scan_request && priv->bssid_fixed &&
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001468 priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
1469 schedule_work(&priv->join_work);
1470 break;
1471 }
1472 /* fall through */
1473 case HERMES_INQ_HOSTSCAN:
1474 case HERMES_INQ_HOSTSCAN_SYMBOL: {
1475 /* Result of a scanning. Contains information about
1476 * cells in the vicinity - Jean II */
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001477 unsigned char *buf;
1478
1479 /* Sanity check */
1480 if (len > 4096) {
1481 printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
1482 dev->name, len);
David Kilroyc63cdbe2009-06-18 23:21:33 +01001483 qabort_scan(priv);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001484 break;
1485 }
1486
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001487 /* Allocate buffer for results */
1488 buf = kmalloc(len, GFP_ATOMIC);
David Kilroyc63cdbe2009-06-18 23:21:33 +01001489 if (buf == NULL) {
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001490 /* No memory, so can't printk()... */
David Kilroyc63cdbe2009-06-18 23:21:33 +01001491 qabort_scan(priv);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001492 break;
David Kilroyc63cdbe2009-06-18 23:21:33 +01001493 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001494
1495 /* Read scan data */
1496 err = hermes_bap_pread(hw, IRQ_BAP, (void *) buf, len,
1497 infofid, sizeof(info));
Pavel Roskin708218b2005-09-01 20:05:19 -04001498 if (err) {
1499 kfree(buf);
David Kilroyc63cdbe2009-06-18 23:21:33 +01001500 qabort_scan(priv);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001501 break;
Pavel Roskin708218b2005-09-01 20:05:19 -04001502 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001503
1504#ifdef ORINOCO_DEBUG
1505 {
1506 int i;
1507 printk(KERN_DEBUG "Scan result [%02X", buf[0]);
David Kilroya94e8422009-02-04 23:05:44 +00001508 for (i = 1; i < (len * 2); i++)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001509 printk(":%02X", buf[i]);
1510 printk("]\n");
1511 }
1512#endif /* ORINOCO_DEBUG */
1513
David Kilroyc63cdbe2009-06-18 23:21:33 +01001514 qbuf_scan(priv, buf, len, type);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001515 }
1516 break;
David Kilroy01632fa2008-08-21 23:27:58 +01001517 case HERMES_INQ_CHANNELINFO:
1518 {
1519 struct agere_ext_scan_info *bss;
1520
David Kilroyc63cdbe2009-06-18 23:21:33 +01001521 if (!priv->scan_request) {
David Kilroy01632fa2008-08-21 23:27:58 +01001522 printk(KERN_DEBUG "%s: Got chaninfo without scan, "
1523 "len=%d\n", dev->name, len);
1524 break;
1525 }
1526
1527 /* An empty result indicates that the scan is complete */
1528 if (len == 0) {
David Kilroyc63cdbe2009-06-18 23:21:33 +01001529 qbuf_scan(priv, NULL, len, type);
David Kilroy01632fa2008-08-21 23:27:58 +01001530 break;
1531 }
1532
1533 /* Sanity check */
David Kilroyc63cdbe2009-06-18 23:21:33 +01001534 else if (len < (offsetof(struct agere_ext_scan_info,
David Kilroy01632fa2008-08-21 23:27:58 +01001535 data) + 2)) {
1536 /* Drop this result now so we don't have to
1537 * keep checking later */
1538 printk(KERN_WARNING
1539 "%s: Ext scan results too short (%d bytes)\n",
1540 dev->name, len);
1541 break;
1542 }
1543
David Kilroyc63cdbe2009-06-18 23:21:33 +01001544 bss = kmalloc(len, GFP_ATOMIC);
David Kilroy01632fa2008-08-21 23:27:58 +01001545 if (bss == NULL)
1546 break;
1547
1548 /* Read scan data */
1549 err = hermes_bap_pread(hw, IRQ_BAP, (void *) bss, len,
1550 infofid, sizeof(info));
David Kilroyc63cdbe2009-06-18 23:21:33 +01001551 if (err)
David Kilroy01632fa2008-08-21 23:27:58 +01001552 kfree(bss);
David Kilroyc63cdbe2009-06-18 23:21:33 +01001553 else
1554 qbuf_scan(priv, bss, len, type);
David Kilroy01632fa2008-08-21 23:27:58 +01001555
David Kilroy01632fa2008-08-21 23:27:58 +01001556 break;
1557 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001558 case HERMES_INQ_SEC_STAT_AGERE:
1559 /* Security status (Agere specific) */
1560 /* Ignore this frame for now */
1561 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
1562 break;
1563 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 default:
1565 printk(KERN_DEBUG "%s: Unknown information frame received: "
1566 "type 0x%04x, length %d\n", dev->name, type, len);
1567 /* We don't actually do anything about it */
1568 break;
1569 }
David Kilroyc63cdbe2009-06-18 23:21:33 +01001570
1571 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
1574static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
1575{
1576 if (net_ratelimit())
1577 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
1578}
1579
1580/********************************************************************/
1581/* Internal hardware control routines */
1582/********************************************************************/
1583
David Kilroy6415f7d2009-06-18 23:21:30 +01001584static int __orinoco_up(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
David Kilroya2608362009-06-18 23:21:23 +01001586 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 struct hermes *hw = &priv->hw;
1588 int err;
1589
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001590 netif_carrier_off(dev); /* just to make sure */
1591
David Kilroy721aa2f2009-06-18 23:21:31 +01001592 err = __orinoco_commit(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (err) {
1594 printk(KERN_ERR "%s: Error %d configuring card\n",
1595 dev->name, err);
1596 return err;
1597 }
1598
1599 /* Fire things up again */
1600 hermes_set_irqmask(hw, ORINOCO_INTEN);
1601 err = hermes_enable_port(hw, 0);
1602 if (err) {
1603 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
1604 dev->name, err);
1605 return err;
1606 }
1607
1608 netif_start_queue(dev);
1609
1610 return 0;
1611}
1612
David Kilroy6415f7d2009-06-18 23:21:30 +01001613static int __orinoco_down(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
David Kilroya2608362009-06-18 23:21:23 +01001615 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 struct hermes *hw = &priv->hw;
1617 int err;
1618
1619 netif_stop_queue(dev);
1620
David Kilroya94e8422009-02-04 23:05:44 +00001621 if (!priv->hw_unavailable) {
1622 if (!priv->broken_disableport) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 err = hermes_disable_port(hw, 0);
1624 if (err) {
1625 /* Some firmwares (e.g. Intersil 1.3.x) seem
1626 * to have problems disabling the port, oh
1627 * well, too bad. */
1628 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
1629 dev->name, err);
1630 priv->broken_disableport = 1;
1631 }
1632 }
1633 hermes_set_irqmask(hw, 0);
1634 hermes_write_regn(hw, EVACK, 0xffff);
1635 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 /* firmware will have to reassociate */
1638 netif_carrier_off(dev);
1639 priv->last_linkstatus = 0xffff;
1640
1641 return 0;
1642}
1643
David Kilroy6415f7d2009-06-18 23:21:30 +01001644static int orinoco_reinit_firmware(struct orinoco_private *priv)
Pavel Roskin37a6c612006-04-07 04:10:49 -04001645{
Pavel Roskin37a6c612006-04-07 04:10:49 -04001646 struct hermes *hw = &priv->hw;
1647 int err;
1648
1649 err = hermes_init(hw);
Andrey Borzenkov0df6cbb2008-10-12 20:15:43 +04001650 if (priv->do_fw_download && !err) {
1651 err = orinoco_download(priv);
1652 if (err)
1653 priv->do_fw_download = 0;
1654 }
Pavel Roskin37a6c612006-04-07 04:10:49 -04001655 if (!err)
David Kilroy42a51b92009-06-18 23:21:20 +01001656 err = orinoco_hw_allocate_fid(priv);
Pavel Roskin37a6c612006-04-07 04:10:49 -04001657
1658 return err;
1659}
1660
David Kilroy721aa2f2009-06-18 23:21:31 +01001661static int
David Kilroy5865d012009-02-04 23:05:54 +00001662__orinoco_set_multicast_list(struct net_device *dev)
1663{
David Kilroyea60a6a2009-06-18 23:21:26 +01001664 struct orinoco_private *priv = ndev_priv(dev);
David Kilroy5865d012009-02-04 23:05:54 +00001665 int err = 0;
1666 int promisc, mc_count;
1667
1668 /* The Hermes doesn't seem to have an allmulti mode, so we go
1669 * into promiscuous mode and let the upper levels deal. */
1670 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001671 (netdev_mc_count(dev) > MAX_MULTICAST(priv))) {
David Kilroy5865d012009-02-04 23:05:54 +00001672 promisc = 1;
1673 mc_count = 0;
1674 } else {
1675 promisc = 0;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001676 mc_count = netdev_mc_count(dev);
David Kilroy5865d012009-02-04 23:05:54 +00001677 }
1678
Jiri Pirko655ffee2010-02-27 07:35:45 +00001679 err = __orinoco_hw_set_multicast_list(priv, dev, mc_count, promisc);
David Kilroy721aa2f2009-06-18 23:21:31 +01001680
1681 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682}
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684/* This must be called from user context, without locks held - use
1685 * schedule_work() */
David Kilroycb1576a2009-02-04 23:05:56 +00001686void orinoco_reset(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
David Howellsc4028952006-11-22 14:57:56 +00001688 struct orinoco_private *priv =
1689 container_of(work, struct orinoco_private, reset_work);
1690 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 struct hermes *hw = &priv->hw;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001692 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 unsigned long flags;
1694
1695 if (orinoco_lock(priv, &flags) != 0)
1696 /* When the hardware becomes available again, whatever
1697 * detects that is responsible for re-initializing
1698 * it. So no need for anything further */
1699 return;
1700
1701 netif_stop_queue(dev);
1702
1703 /* Shut off interrupts. Depending on what state the hardware
1704 * is in, this might not work, but we'll try anyway */
1705 hermes_set_irqmask(hw, 0);
1706 hermes_write_regn(hw, EVACK, 0xffff);
1707
1708 priv->hw_unavailable++;
1709 priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
1710 netif_carrier_off(dev);
1711
1712 orinoco_unlock(priv, &flags);
1713
David Kilroyc63cdbe2009-06-18 23:21:33 +01001714 /* Scanning support: Notify scan cancellation */
1715 if (priv->scan_request) {
1716 cfg80211_scan_done(priv->scan_request, 1);
1717 priv->scan_request = NULL;
1718 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001719
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001720 if (priv->hard_reset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 err = (*priv->hard_reset)(priv);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001722 if (err) {
1723 printk(KERN_ERR "%s: orinoco_reset: Error %d "
1724 "performing hard reset\n", dev->name, err);
1725 goto disable;
1726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 }
1728
David Kilroya2608362009-06-18 23:21:23 +01001729 err = orinoco_reinit_firmware(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 if (err) {
1731 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
1732 dev->name, err);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001733 goto disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 }
1735
David Kilroyb2f30a02009-02-04 23:05:46 +00001736 /* This has to be called from user context */
1737 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 priv->hw_unavailable--;
1740
1741 /* priv->open or priv->hw_unavailable might have changed while
1742 * we dropped the lock */
David Kilroya94e8422009-02-04 23:05:44 +00001743 if (priv->open && (!priv->hw_unavailable)) {
David Kilroya2608362009-06-18 23:21:23 +01001744 err = __orinoco_up(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (err) {
1746 printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
1747 dev->name, err);
1748 } else
1749 dev->trans_start = jiffies;
1750 }
1751
1752 spin_unlock_irq(&priv->lock);
1753
1754 return;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001755 disable:
1756 hermes_set_irqmask(hw, 0);
1757 netif_device_detach(dev);
1758 printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
David Kilroy721aa2f2009-06-18 23:21:31 +01001761static int __orinoco_commit(struct orinoco_private *priv)
1762{
1763 struct net_device *dev = priv->ndev;
1764 int err = 0;
1765
1766 err = orinoco_hw_program_rids(priv);
1767
1768 /* FIXME: what about netif_tx_lock */
1769 (void) __orinoco_set_multicast_list(dev);
1770
1771 return err;
1772}
1773
1774/* Ensures configuration changes are applied. May result in a reset.
1775 * The caller should hold priv->lock
1776 */
1777int orinoco_commit(struct orinoco_private *priv)
1778{
1779 struct net_device *dev = priv->ndev;
1780 hermes_t *hw = &priv->hw;
1781 int err;
1782
1783 if (priv->broken_disableport) {
1784 schedule_work(&priv->reset_work);
1785 return 0;
1786 }
1787
1788 err = hermes_disable_port(hw, 0);
1789 if (err) {
1790 printk(KERN_WARNING "%s: Unable to disable port "
1791 "while reconfiguring card\n", dev->name);
1792 priv->broken_disableport = 1;
1793 goto out;
1794 }
1795
1796 err = __orinoco_commit(priv);
1797 if (err) {
1798 printk(KERN_WARNING "%s: Unable to reconfigure card\n",
1799 dev->name);
1800 goto out;
1801 }
1802
1803 err = hermes_enable_port(hw, 0);
1804 if (err) {
1805 printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
1806 dev->name);
1807 goto out;
1808 }
1809
1810 out:
1811 if (err) {
1812 printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
1813 schedule_work(&priv->reset_work);
1814 err = 0;
1815 }
1816 return err;
1817}
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819/********************************************************************/
1820/* Interrupt handler */
1821/********************************************************************/
1822
1823static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
1824{
1825 printk(KERN_DEBUG "%s: TICK\n", dev->name);
1826}
1827
1828static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
1829{
1830 /* This seems to happen a fair bit under load, but ignoring it
1831 seems to work fine...*/
1832 printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
1833 dev->name);
1834}
1835
David Howells7d12e782006-10-05 14:55:46 +01001836irqreturn_t orinoco_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
David Kilroya2608362009-06-18 23:21:23 +01001838 struct orinoco_private *priv = dev_id;
1839 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 hermes_t *hw = &priv->hw;
1841 int count = MAX_IRQLOOPS_PER_IRQ;
1842 u16 evstat, events;
David Kilroy21312662009-02-04 23:05:47 +00001843 /* These are used to detect a runaway interrupt situation.
1844 *
1845 * If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
1846 * we panic and shut down the hardware
1847 */
1848 /* jiffies value the last time we were called */
1849 static int last_irq_jiffy; /* = 0 */
1850 static int loops_this_jiffy; /* = 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 unsigned long flags;
1852
1853 if (orinoco_lock(priv, &flags) != 0) {
1854 /* If hw is unavailable - we don't know if the irq was
1855 * for us or not */
1856 return IRQ_HANDLED;
1857 }
1858
1859 evstat = hermes_read_regn(hw, EVSTAT);
1860 events = evstat & hw->inten;
David Kilroya94e8422009-02-04 23:05:44 +00001861 if (!events) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 orinoco_unlock(priv, &flags);
1863 return IRQ_NONE;
1864 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 if (jiffies != last_irq_jiffy)
1867 loops_this_jiffy = 0;
1868 last_irq_jiffy = jiffies;
1869
1870 while (events && count--) {
1871 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
1872 printk(KERN_WARNING "%s: IRQ handler is looping too "
1873 "much! Resetting.\n", dev->name);
1874 /* Disable interrupts for now */
1875 hermes_set_irqmask(hw, 0);
1876 schedule_work(&priv->reset_work);
1877 break;
1878 }
1879
1880 /* Check the card hasn't been removed */
David Kilroya94e8422009-02-04 23:05:44 +00001881 if (!hermes_present(hw)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 DEBUG(0, "orinoco_interrupt(): card removed\n");
1883 break;
1884 }
1885
1886 if (events & HERMES_EV_TICK)
1887 __orinoco_ev_tick(dev, hw);
1888 if (events & HERMES_EV_WTERR)
1889 __orinoco_ev_wterr(dev, hw);
1890 if (events & HERMES_EV_INFDROP)
1891 __orinoco_ev_infdrop(dev, hw);
1892 if (events & HERMES_EV_INFO)
1893 __orinoco_ev_info(dev, hw);
1894 if (events & HERMES_EV_RX)
1895 __orinoco_ev_rx(dev, hw);
1896 if (events & HERMES_EV_TXEXC)
1897 __orinoco_ev_txexc(dev, hw);
1898 if (events & HERMES_EV_TX)
1899 __orinoco_ev_tx(dev, hw);
1900 if (events & HERMES_EV_ALLOC)
1901 __orinoco_ev_alloc(dev, hw);
David Kilroy6fe9deb2009-02-04 23:05:43 +00001902
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001903 hermes_write_regn(hw, EVACK, evstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905 evstat = hermes_read_regn(hw, EVSTAT);
1906 events = evstat & hw->inten;
1907 };
1908
1909 orinoco_unlock(priv, &flags);
1910 return IRQ_HANDLED;
1911}
David Kilroy21312662009-02-04 23:05:47 +00001912EXPORT_SYMBOL(orinoco_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914/********************************************************************/
David Kilroy39d1ffe2008-11-22 10:37:28 +00001915/* Power management */
1916/********************************************************************/
1917#if defined(CONFIG_PM_SLEEP) && !defined(CONFIG_HERMES_CACHE_FW_ON_INIT)
1918static int orinoco_pm_notifier(struct notifier_block *notifier,
1919 unsigned long pm_event,
1920 void *unused)
1921{
1922 struct orinoco_private *priv = container_of(notifier,
1923 struct orinoco_private,
1924 pm_notifier);
1925
1926 /* All we need to do is cache the firmware before suspend, and
1927 * release it when we come out.
1928 *
1929 * Only need to do this if we're downloading firmware. */
1930 if (!priv->do_fw_download)
1931 return NOTIFY_DONE;
1932
1933 switch (pm_event) {
1934 case PM_HIBERNATION_PREPARE:
1935 case PM_SUSPEND_PREPARE:
1936 orinoco_cache_fw(priv, 0);
1937 break;
1938
1939 case PM_POST_RESTORE:
1940 /* Restore from hibernation failed. We need to clean
1941 * up in exactly the same way, so fall through. */
1942 case PM_POST_HIBERNATION:
1943 case PM_POST_SUSPEND:
1944 orinoco_uncache_fw(priv);
1945 break;
1946
1947 case PM_RESTORE_PREPARE:
1948 default:
1949 break;
1950 }
1951
1952 return NOTIFY_DONE;
1953}
David S. Millerf11c1792009-02-25 00:02:05 -08001954
1955static void orinoco_register_pm_notifier(struct orinoco_private *priv)
1956{
1957 priv->pm_notifier.notifier_call = orinoco_pm_notifier;
1958 register_pm_notifier(&priv->pm_notifier);
1959}
1960
1961static void orinoco_unregister_pm_notifier(struct orinoco_private *priv)
1962{
1963 unregister_pm_notifier(&priv->pm_notifier);
1964}
David Kilroy39d1ffe2008-11-22 10:37:28 +00001965#else /* !PM_SLEEP || HERMES_CACHE_FW_ON_INIT */
David S. Millerf11c1792009-02-25 00:02:05 -08001966#define orinoco_register_pm_notifier(priv) do { } while(0)
1967#define orinoco_unregister_pm_notifier(priv) do { } while(0)
David Kilroy39d1ffe2008-11-22 10:37:28 +00001968#endif
1969
1970/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971/* Initialization */
1972/********************************************************************/
1973
David Kilroy8e638262009-06-18 23:21:24 +01001974int orinoco_init(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
David Kilroy8e638262009-06-18 23:21:24 +01001976 struct device *dev = priv->dev;
David Kilroyea60a6a2009-06-18 23:21:26 +01001977 struct wiphy *wiphy = priv_to_wiphy(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 hermes_t *hw = &priv->hw;
1979 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 /* No need to lock, the hw_unavailable flag is already set in
1982 * alloc_orinocodev() */
Johannes Berg2c7060022008-10-30 22:09:54 +01001983 priv->nicbuf_size = IEEE80211_MAX_FRAME_LEN + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 /* Initialize the firmware */
Pavel Roskin37a6c612006-04-07 04:10:49 -04001986 err = hermes_init(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 if (err != 0) {
David Kilroy8e638262009-06-18 23:21:24 +01001988 dev_err(dev, "Failed to initialize firmware (err = %d)\n",
1989 err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 goto out;
1991 }
1992
David Kilroy3414fc32009-10-07 22:23:32 +01001993 err = determine_fw_capabilities(priv, wiphy->fw_version,
1994 sizeof(wiphy->fw_version),
1995 &wiphy->hw_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (err != 0) {
David Kilroy8e638262009-06-18 23:21:24 +01001997 dev_err(dev, "Incompatible firmware, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 goto out;
1999 }
2000
David Kilroy3994d502008-08-21 23:27:54 +01002001 if (priv->do_fw_download) {
David Kilroy39d1ffe2008-11-22 10:37:28 +00002002#ifdef CONFIG_HERMES_CACHE_FW_ON_INIT
David Kilroy74734312008-11-22 10:37:25 +00002003 orinoco_cache_fw(priv, 0);
David Kilroy39d1ffe2008-11-22 10:37:28 +00002004#endif
David Kilroy74734312008-11-22 10:37:25 +00002005
David Kilroy3994d502008-08-21 23:27:54 +01002006 err = orinoco_download(priv);
2007 if (err)
2008 priv->do_fw_download = 0;
2009
2010 /* Check firmware version again */
David Kilroy3414fc32009-10-07 22:23:32 +01002011 err = determine_fw_capabilities(priv, wiphy->fw_version,
2012 sizeof(wiphy->fw_version),
2013 &wiphy->hw_version);
David Kilroy3994d502008-08-21 23:27:54 +01002014 if (err != 0) {
David Kilroy8e638262009-06-18 23:21:24 +01002015 dev_err(dev, "Incompatible firmware, aborting\n");
David Kilroy3994d502008-08-21 23:27:54 +01002016 goto out;
2017 }
2018 }
2019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 if (priv->has_port3)
David Kilroy8e638262009-06-18 23:21:24 +01002021 dev_info(dev, "Ad-hoc demo mode supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if (priv->has_ibss)
David Kilroy8e638262009-06-18 23:21:24 +01002023 dev_info(dev, "IEEE standard IBSS ad-hoc mode supported\n");
2024 if (priv->has_wep)
2025 dev_info(dev, "WEP supported, %s-bit key\n",
2026 priv->has_big_wep ? "104" : "40");
David Kilroy23edcc42008-08-21 23:28:05 +01002027 if (priv->has_wpa) {
David Kilroy8e638262009-06-18 23:21:24 +01002028 dev_info(dev, "WPA-PSK supported\n");
David Kilroy23edcc42008-08-21 23:28:05 +01002029 if (orinoco_mic_init(priv)) {
David Kilroy8e638262009-06-18 23:21:24 +01002030 dev_err(dev, "Failed to setup MIC crypto algorithm. "
2031 "Disabling WPA support\n");
David Kilroy23edcc42008-08-21 23:28:05 +01002032 priv->has_wpa = 0;
2033 }
2034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
David Kilroyea60a6a2009-06-18 23:21:26 +01002036 err = orinoco_hw_read_card_settings(priv, wiphy->perm_addr);
David Kilroye9e3d012009-06-18 23:21:19 +01002037 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
David Kilroy42a51b92009-06-18 23:21:20 +01002040 err = orinoco_hw_allocate_fid(priv);
Pavel Roskin37a6c612006-04-07 04:10:49 -04002041 if (err) {
David Kilroy8e638262009-06-18 23:21:24 +01002042 dev_err(dev, "Failed to allocate NIC buffer!\n");
Pavel Roskin37a6c612006-04-07 04:10:49 -04002043 goto out;
2044 }
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 /* Set up the default configuration */
David Kilroy5217c572009-06-18 23:21:32 +01002047 priv->iw_mode = NL80211_IFTYPE_STATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 /* By default use IEEE/IBSS ad-hoc mode if we have it */
David Kilroya94e8422009-02-04 23:05:44 +00002049 priv->prefer_port3 = priv->has_port3 && (!priv->has_ibss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 set_port_type(priv);
David Gibsond51d8b12005-05-12 20:03:36 -04002051 priv->channel = 0; /* use firmware default */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 priv->promiscuous = 0;
David Kilroy5c9f41e2009-08-05 21:23:28 +01002054 priv->encode_alg = ORINOCO_ALG_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 priv->tx_key = 0;
David Kilroyd03032a2008-08-21 23:28:02 +01002056 priv->wpa_enabled = 0;
2057 priv->tkip_cm_active = 0;
2058 priv->key_mgmt = 0;
2059 priv->wpa_ie_len = 0;
2060 priv->wpa_ie = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
David Kilroyea60a6a2009-06-18 23:21:26 +01002062 if (orinoco_wiphy_register(wiphy)) {
2063 err = -ENODEV;
2064 goto out;
2065 }
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 /* Make the hardware available, as long as it hasn't been
2068 * removed elsewhere (e.g. by PCMCIA hot unplug) */
2069 spin_lock_irq(&priv->lock);
2070 priv->hw_unavailable--;
2071 spin_unlock_irq(&priv->lock);
2072
David Kilroy8e638262009-06-18 23:21:24 +01002073 dev_dbg(dev, "Ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 return err;
2077}
David Kilroy8e638262009-06-18 23:21:24 +01002078EXPORT_SYMBOL(orinoco_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Andrey Borzenkov89ea4092009-01-21 20:46:46 +03002080static const struct net_device_ops orinoco_netdev_ops = {
Andrey Borzenkov89ea4092009-01-21 20:46:46 +03002081 .ndo_open = orinoco_open,
2082 .ndo_stop = orinoco_stop,
2083 .ndo_start_xmit = orinoco_xmit,
2084 .ndo_set_multicast_list = orinoco_set_multicast_list,
2085 .ndo_change_mtu = orinoco_change_mtu,
Ben Hutchings240c1022009-07-09 17:54:35 +00002086 .ndo_set_mac_address = eth_mac_addr,
2087 .ndo_validate_addr = eth_validate_addr,
Andrey Borzenkov89ea4092009-01-21 20:46:46 +03002088 .ndo_tx_timeout = orinoco_tx_timeout,
2089 .ndo_get_stats = orinoco_get_stats,
2090};
2091
David Kilroyea60a6a2009-06-18 23:21:26 +01002092/* Allocate private data.
2093 *
2094 * This driver has a number of structures associated with it
2095 * netdev - Net device structure for each network interface
2096 * wiphy - structure associated with wireless phy
2097 * wireless_dev (wdev) - structure for each wireless interface
2098 * hw - structure for hermes chip info
2099 * card - card specific structure for use by the card driver
2100 * (airport, orinoco_cs)
2101 * priv - orinoco private data
2102 * device - generic linux device structure
2103 *
2104 * +---------+ +---------+
2105 * | wiphy | | netdev |
2106 * | +-------+ | +-------+
2107 * | | priv | | | wdev |
2108 * | | +-----+ +-+-------+
2109 * | | | hw |
2110 * | +-+-----+
2111 * | | card |
2112 * +-+-------+
2113 *
2114 * priv has a link to netdev and device
2115 * wdev has a link to wiphy
2116 */
David Kilroya2608362009-06-18 23:21:23 +01002117struct orinoco_private
David Kilroy3994d502008-08-21 23:27:54 +01002118*alloc_orinocodev(int sizeof_card,
2119 struct device *device,
2120 int (*hard_reset)(struct orinoco_private *),
2121 int (*stop_fw)(struct orinoco_private *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 struct orinoco_private *priv;
David Kilroyea60a6a2009-06-18 23:21:26 +01002124 struct wiphy *wiphy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
David Kilroyea60a6a2009-06-18 23:21:26 +01002126 /* allocate wiphy
2127 * NOTE: We only support a single virtual interface
2128 * but this may change when monitor mode is added
2129 */
2130 wiphy = wiphy_new(&orinoco_cfg_ops,
2131 sizeof(struct orinoco_private) + sizeof_card);
2132 if (!wiphy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 return NULL;
David Kilroyea60a6a2009-06-18 23:21:26 +01002134
David Kilroyea60a6a2009-06-18 23:21:26 +01002135 priv = wiphy_priv(wiphy);
David Kilroy53819562009-06-18 23:21:28 +01002136 priv->dev = device;
David Kilroyea60a6a2009-06-18 23:21:26 +01002137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 if (sizeof_card)
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002139 priv->card = (void *)((unsigned long)priv
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 + sizeof(struct orinoco_private));
2141 else
2142 priv->card = NULL;
2143
David Kilroyea60a6a2009-06-18 23:21:26 +01002144 orinoco_wiphy_init(wiphy);
2145
Pavel Roskin343c6862005-09-09 18:43:02 -04002146#ifdef WIRELESS_SPY
2147 priv->wireless_data.spy_data = &priv->spy_data;
Pavel Roskin343c6862005-09-09 18:43:02 -04002148#endif
David Kilroy23edcc42008-08-21 23:28:05 +01002149
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 /* Set up default callbacks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 priv->hard_reset = hard_reset;
David Kilroy3994d502008-08-21 23:27:54 +01002152 priv->stop_fw = stop_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
2154 spin_lock_init(&priv->lock);
2155 priv->open = 0;
2156 priv->hw_unavailable = 1; /* orinoco_init() must clear this
2157 * before anything else touches the
2158 * hardware */
David Howellsc4028952006-11-22 14:57:56 +00002159 INIT_WORK(&priv->reset_work, orinoco_reset);
2160 INIT_WORK(&priv->join_work, orinoco_join_ap);
2161 INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
David Kilroy31afcef2008-08-21 23:28:04 +01002163 INIT_LIST_HEAD(&priv->rx_list);
2164 tasklet_init(&priv->rx_tasklet, orinoco_rx_isr_tasklet,
David Kilroy53819562009-06-18 23:21:28 +01002165 (unsigned long) priv);
David Kilroy31afcef2008-08-21 23:28:04 +01002166
David Kilroyc63cdbe2009-06-18 23:21:33 +01002167 spin_lock_init(&priv->scan_lock);
2168 INIT_LIST_HEAD(&priv->scan_list);
2169 INIT_WORK(&priv->process_scan, orinoco_process_scan_results);
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 priv->last_linkstatus = 0xffff;
2172
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +03002173#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
David Kilroy2cea7b22008-11-22 10:37:26 +00002174 priv->cached_pri_fw = NULL;
Andrey Borzenkov4fb30782008-10-19 12:06:11 +04002175 priv->cached_fw = NULL;
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +03002176#endif
Andrey Borzenkov4fb30782008-10-19 12:06:11 +04002177
David Kilroy39d1ffe2008-11-22 10:37:28 +00002178 /* Register PM notifiers */
David S. Millerf11c1792009-02-25 00:02:05 -08002179 orinoco_register_pm_notifier(priv);
David Kilroy39d1ffe2008-11-22 10:37:28 +00002180
David Kilroya2608362009-06-18 23:21:23 +01002181 return priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
David Kilroy21312662009-02-04 23:05:47 +00002183EXPORT_SYMBOL(alloc_orinocodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
David Kilroy53819562009-06-18 23:21:28 +01002185/* We can only support a single interface. We provide a separate
2186 * function to set it up to distinguish between hardware
2187 * initialisation and interface setup.
2188 *
2189 * The base_addr and irq parameters are passed on to netdev for use
2190 * with SIOCGIFMAP.
2191 */
2192int orinoco_if_add(struct orinoco_private *priv,
2193 unsigned long base_addr,
2194 unsigned int irq)
2195{
2196 struct wiphy *wiphy = priv_to_wiphy(priv);
2197 struct wireless_dev *wdev;
2198 struct net_device *dev;
2199 int ret;
2200
2201 dev = alloc_etherdev(sizeof(struct wireless_dev));
2202
2203 if (!dev)
2204 return -ENOMEM;
2205
2206 /* Initialise wireless_dev */
2207 wdev = netdev_priv(dev);
2208 wdev->wiphy = wiphy;
2209 wdev->iftype = NL80211_IFTYPE_STATION;
2210
2211 /* Setup / override net_device fields */
2212 dev->ieee80211_ptr = wdev;
2213 dev->netdev_ops = &orinoco_netdev_ops;
2214 dev->watchdog_timeo = HZ; /* 1 second timeout */
David Kilroy53819562009-06-18 23:21:28 +01002215 dev->wireless_handlers = &orinoco_handler_def;
2216#ifdef WIRELESS_SPY
2217 dev->wireless_data = &priv->wireless_data;
2218#endif
2219 /* we use the default eth_mac_addr for setting the MAC addr */
2220
2221 /* Reserve space in skb for the SNAP header */
2222 dev->hard_header_len += ENCAPS_OVERHEAD;
2223
2224 netif_carrier_off(dev);
2225
2226 memcpy(dev->dev_addr, wiphy->perm_addr, ETH_ALEN);
John W. Linvillecf32ed92009-10-06 16:47:23 -04002227 memcpy(dev->perm_addr, wiphy->perm_addr, ETH_ALEN);
David Kilroy53819562009-06-18 23:21:28 +01002228
2229 dev->base_addr = base_addr;
2230 dev->irq = irq;
2231
2232 SET_NETDEV_DEV(dev, priv->dev);
2233 ret = register_netdev(dev);
2234 if (ret)
2235 goto fail;
2236
2237 priv->ndev = dev;
2238
2239 /* Report what we've done */
2240 dev_dbg(priv->dev, "Registerred interface %s.\n", dev->name);
2241
2242 return 0;
2243
2244 fail:
2245 free_netdev(dev);
2246 return ret;
2247}
2248EXPORT_SYMBOL(orinoco_if_add);
2249
2250void orinoco_if_del(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
David Kilroya2608362009-06-18 23:21:23 +01002252 struct net_device *dev = priv->ndev;
David Kilroy53819562009-06-18 23:21:28 +01002253
2254 unregister_netdev(dev);
2255 free_netdev(dev);
2256}
2257EXPORT_SYMBOL(orinoco_if_del);
2258
2259void free_orinocodev(struct orinoco_private *priv)
2260{
David Kilroyea60a6a2009-06-18 23:21:26 +01002261 struct wiphy *wiphy = priv_to_wiphy(priv);
David Kilroy20953ad2009-01-07 00:23:55 +00002262 struct orinoco_rx_data *rx_data, *temp;
David Kilroyc63cdbe2009-06-18 23:21:33 +01002263 struct orinoco_scan_data *sd, *sdtemp;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002264
David Kilroyea60a6a2009-06-18 23:21:26 +01002265 wiphy_unregister(wiphy);
2266
David Kilroy20953ad2009-01-07 00:23:55 +00002267 /* If the tasklet is scheduled when we call tasklet_kill it
2268 * will run one final time. However the tasklet will only
2269 * drain priv->rx_list if the hw is still available. */
David Kilroy31afcef2008-08-21 23:28:04 +01002270 tasklet_kill(&priv->rx_tasklet);
David Kilroy74734312008-11-22 10:37:25 +00002271
David Kilroy20953ad2009-01-07 00:23:55 +00002272 /* Explicitly drain priv->rx_list */
2273 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
2274 list_del(&rx_data->list);
2275
2276 dev_kfree_skb(rx_data->skb);
2277 kfree(rx_data->desc);
2278 kfree(rx_data);
2279 }
2280
David Kilroyc63cdbe2009-06-18 23:21:33 +01002281 cancel_work_sync(&priv->process_scan);
2282 /* Explicitly drain priv->scan_list */
2283 list_for_each_entry_safe(sd, sdtemp, &priv->scan_list, list) {
2284 list_del(&sd->list);
2285
2286 if ((sd->len > 0) && sd->buf)
2287 kfree(sd->buf);
2288 kfree(sd);
2289 }
2290
David S. Millerf11c1792009-02-25 00:02:05 -08002291 orinoco_unregister_pm_notifier(priv);
David Kilroy74734312008-11-22 10:37:25 +00002292 orinoco_uncache_fw(priv);
2293
David Kilroyd03032a2008-08-21 23:28:02 +01002294 priv->wpa_ie_len = 0;
2295 kfree(priv->wpa_ie);
David Kilroy23edcc42008-08-21 23:28:05 +01002296 orinoco_mic_free(priv);
David Kilroyea60a6a2009-06-18 23:21:26 +01002297 wiphy_free(wiphy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298}
David Kilroy21312662009-02-04 23:05:47 +00002299EXPORT_SYMBOL(free_orinocodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
David Kilroy6415f7d2009-06-18 23:21:30 +01002301int orinoco_up(struct orinoco_private *priv)
2302{
2303 struct net_device *dev = priv->ndev;
2304 unsigned long flags;
2305 int err;
2306
2307 spin_lock_irqsave(&priv->lock, flags);
2308
2309 err = orinoco_reinit_firmware(priv);
2310 if (err) {
2311 printk(KERN_ERR "%s: Error %d re-initializing firmware\n",
2312 dev->name, err);
2313 goto exit;
2314 }
2315
2316 netif_device_attach(dev);
2317 priv->hw_unavailable--;
2318
2319 if (priv->open && !priv->hw_unavailable) {
2320 err = __orinoco_up(priv);
2321 if (err)
2322 printk(KERN_ERR "%s: Error %d restarting card\n",
2323 dev->name, err);
2324 }
2325
2326exit:
2327 spin_unlock_irqrestore(&priv->lock, flags);
2328
2329 return 0;
2330}
2331EXPORT_SYMBOL(orinoco_up);
2332
2333void orinoco_down(struct orinoco_private *priv)
2334{
2335 struct net_device *dev = priv->ndev;
2336 unsigned long flags;
2337 int err;
2338
2339 spin_lock_irqsave(&priv->lock, flags);
2340 err = __orinoco_down(priv);
2341 if (err)
2342 printk(KERN_WARNING "%s: Error %d downing interface\n",
2343 dev->name, err);
2344
2345 netif_device_detach(dev);
2346 priv->hw_unavailable++;
2347 spin_unlock_irqrestore(&priv->lock, flags);
2348}
2349EXPORT_SYMBOL(orinoco_down);
2350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352/* Module initialization */
2353/********************************************************************/
2354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355/* Can't be declared "const" or the whole __initdata section will
2356 * become const */
2357static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
2358 " (David Gibson <hermes@gibson.dropbear.id.au>, "
2359 "Pavel Roskin <proski@gnu.org>, et al)";
2360
2361static int __init init_orinoco(void)
2362{
2363 printk(KERN_DEBUG "%s\n", version);
2364 return 0;
2365}
2366
2367static void __exit exit_orinoco(void)
2368{
2369}
2370
2371module_init(init_orinoco);
2372module_exit(exit_orinoco);