blob: dd6c64ac406e8f28d87e69d81dff7b4d8171e19c [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:
Pavel Roskin933d5942011-07-13 11:19:57 -04007 * Pavel Roskin <proski AT gnu.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090081#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <linux/init.h>
David Kilroyd03032a2008-08-21 23:28:02 +010083#include <linux/delay.h>
David Kilroy8e638262009-06-18 23:21:24 +010084#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#include <linux/etherdevice.h>
David Kilroy39d1ffe2008-11-22 10:37:28 +000087#include <linux/suspend.h>
Pavel Roskin9c974fb2006-08-15 20:45:03 -040088#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <linux/wireless.h>
Johannes Berg2c7060022008-10-30 22:09:54 +010090#include <linux/ieee80211.h>
Christoph Hellwig620554e2005-06-19 01:27:33 +020091#include <net/iw_handler.h>
David Kilroyea60a6a2009-06-18 23:21:26 +010092#include <net/cfg80211.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#include "hermes_rid.h"
David Kilroy3994d502008-08-21 23:27:54 +010095#include "hermes_dld.h"
David Kilroy712a4342009-02-04 23:05:55 +000096#include "hw.h"
David Kilroyfb791b12009-02-04 23:05:50 +000097#include "scan.h"
David Kilroy4adb4742009-02-04 23:05:51 +000098#include "mic.h"
David Kilroy37a2e562009-02-04 23:05:52 +000099#include "fw.h"
David Kilroycb1576a2009-02-04 23:05:56 +0000100#include "wext.h"
David Kilroyea60a6a2009-06-18 23:21:26 +0100101#include "cfg.h"
David Kilroycb1576a2009-02-04 23:05:56 +0000102#include "main.h"
David Kilroyfb791b12009-02-04 23:05:50 +0000103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#include "orinoco.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/********************************************************************/
107/* Module information */
108/********************************************************************/
109
David Kilroyb2f30a02009-02-04 23:05:46 +0000110MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & "
111 "David Gibson <hermes@gibson.dropbear.id.au>");
112MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based "
113 "and similar wireless cards");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114MODULE_LICENSE("Dual MPL/GPL");
115
116/* Level of debugging. Used in the macros in orinoco.h */
117#ifdef ORINOCO_DEBUG
118int orinoco_debug = ORINOCO_DEBUG;
David Kilroy21312662009-02-04 23:05:47 +0000119EXPORT_SYMBOL(orinoco_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120module_param(orinoco_debug, int, 0644);
121MODULE_PARM_DESC(orinoco_debug, "Debug level");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#endif
123
Rusty Russelleb939922011-12-19 14:08:01 +0000124static bool suppress_linkstatus; /* = 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125module_param(suppress_linkstatus, bool, 0644);
126MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
David Kilroyb2f30a02009-02-04 23:05:46 +0000127
David Gibson7bb7c3a2005-05-12 20:02:10 -0400128static int ignore_disconnect; /* = 0 */
129module_param(ignore_disconnect, int, 0644);
David Kilroyb2f30a02009-02-04 23:05:46 +0000130MODULE_PARM_DESC(ignore_disconnect,
131 "Don't report lost link to the network layer");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
David Kilroycb1576a2009-02-04 23:05:56 +0000133int force_monitor; /* = 0 */
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200134module_param(force_monitor, int, 0644);
135MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/* Internal constants */
139/********************************************************************/
140
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200141/* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
142static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
143#define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define ORINOCO_MIN_MTU 256
Johannes Berg2c7060022008-10-30 22:09:54 +0100146#define ORINOCO_MAX_MTU (IEEE80211_MAX_DATA_LEN - ENCAPS_OVERHEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#define MAX_IRQLOOPS_PER_IRQ 10
Pavel Roskin933d5942011-07-13 11:19:57 -0400149#define MAX_IRQLOOPS_PER_JIFFY (20000 / HZ) /* Based on a guestimate of
150 * how many events the
151 * device could
152 * legitimately generate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154#define DUMMY_FID 0xFFFF
155
156/*#define MAX_MULTICAST(priv) (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
157 HERMES_MAX_MULTICAST : 0)*/
158#define MAX_MULTICAST(priv) (HERMES_MAX_MULTICAST)
159
Pavel Roskin933d5942011-07-13 11:19:57 -0400160#define ORINOCO_INTEN (HERMES_EV_RX | HERMES_EV_ALLOC \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 | HERMES_EV_TX | HERMES_EV_TXEXC \
162 | HERMES_EV_WTERR | HERMES_EV_INFO \
David Kilroya94e8422009-02-04 23:05:44 +0000163 | HERMES_EV_INFDROP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/* Data types */
167/********************************************************************/
168
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400169/* Beginning of the Tx descriptor, used in TxExc handling */
170struct hermes_txexc_data {
171 struct hermes_tx_descriptor desc;
Pavel Roskind133ae42005-09-23 04:18:06 -0400172 __le16 frame_ctl;
173 __le16 duration_id;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200174 u8 addr1[ETH_ALEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000175} __packed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200177/* Rx frame header except compatibility 802.3 header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178struct hermes_rx_descriptor {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200179 /* Control */
Pavel Roskind133ae42005-09-23 04:18:06 -0400180 __le16 status;
181 __le32 time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 u8 silence;
183 u8 signal;
184 u8 rate;
185 u8 rxflow;
Pavel Roskind133ae42005-09-23 04:18:06 -0400186 __le32 reserved;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200187
188 /* 802.11 header */
Pavel Roskind133ae42005-09-23 04:18:06 -0400189 __le16 frame_ctl;
190 __le16 duration_id;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200191 u8 addr1[ETH_ALEN];
192 u8 addr2[ETH_ALEN];
193 u8 addr3[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -0400194 __le16 seq_ctl;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200195 u8 addr4[ETH_ALEN];
196
197 /* Data length */
Pavel Roskind133ae42005-09-23 04:18:06 -0400198 __le16 data_len;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000199} __packed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
David Kilroy47166792009-01-07 00:43:54 +0000201struct orinoco_rx_data {
202 struct hermes_rx_descriptor *desc;
203 struct sk_buff *skb;
204 struct list_head list;
205};
206
David Kilroyc63cdbe2009-06-18 23:21:33 +0100207struct orinoco_scan_data {
208 void *buf;
209 size_t len;
210 int type;
211 struct list_head list;
212};
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/********************************************************************/
215/* Function prototypes */
216/********************************************************************/
217
David Kilroy721aa2f2009-06-18 23:21:31 +0100218static int __orinoco_set_multicast_list(struct net_device *dev);
David Kilroy6415f7d2009-06-18 23:21:30 +0100219static int __orinoco_up(struct orinoco_private *priv);
220static int __orinoco_down(struct orinoco_private *priv);
David Kilroy721aa2f2009-06-18 23:21:31 +0100221static int __orinoco_commit(struct orinoco_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223/********************************************************************/
224/* Internal helper functions */
225/********************************************************************/
226
David Kilroycb1576a2009-02-04 23:05:56 +0000227void set_port_type(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 switch (priv->iw_mode) {
David Kilroy5217c572009-06-18 23:21:32 +0100230 case NL80211_IFTYPE_STATION:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 priv->port_type = 1;
232 priv->createibss = 0;
233 break;
David Kilroy5217c572009-06-18 23:21:32 +0100234 case NL80211_IFTYPE_ADHOC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (priv->prefer_port3) {
236 priv->port_type = 3;
237 priv->createibss = 0;
238 } else {
239 priv->port_type = priv->ibss_port;
240 priv->createibss = 1;
241 }
242 break;
David Kilroy5217c572009-06-18 23:21:32 +0100243 case NL80211_IFTYPE_MONITOR:
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200244 priv->port_type = 3;
245 priv->createibss = 0;
246 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 default:
248 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
249 priv->ndev->name);
250 }
251}
252
David Kilroy3994d502008-08-21 23:27:54 +0100253/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/* Device methods */
255/********************************************************************/
256
David Kilroy593ef092010-05-01 14:05:39 +0100257int orinoco_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
David Kilroyea60a6a2009-06-18 23:21:26 +0100259 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 unsigned long flags;
261 int err;
262
263 if (orinoco_lock(priv, &flags) != 0)
264 return -EBUSY;
265
David Kilroya2608362009-06-18 23:21:23 +0100266 err = __orinoco_up(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
David Kilroya94e8422009-02-04 23:05:44 +0000268 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 priv->open = 1;
270
271 orinoco_unlock(priv, &flags);
272
273 return err;
274}
David Kilroy593ef092010-05-01 14:05:39 +0100275EXPORT_SYMBOL(orinoco_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
David Kilroy593ef092010-05-01 14:05:39 +0100277int orinoco_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
David Kilroyea60a6a2009-06-18 23:21:26 +0100279 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 int err = 0;
281
282 /* We mustn't use orinoco_lock() here, because we need to be
283 able to close the interface even if hw_unavailable is set
284 (e.g. as we're released after a PC Card removal) */
David Kilroybcad6e82010-05-01 14:05:40 +0100285 orinoco_lock_irq(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 priv->open = 0;
288
David Kilroya2608362009-06-18 23:21:23 +0100289 err = __orinoco_down(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
David Kilroybcad6e82010-05-01 14:05:40 +0100291 orinoco_unlock_irq(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 return err;
294}
David Kilroy593ef092010-05-01 14:05:39 +0100295EXPORT_SYMBOL(orinoco_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
David Kilroy593ef092010-05-01 14:05:39 +0100297struct net_device_stats *orinoco_get_stats(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
David Kilroyea60a6a2009-06-18 23:21:26 +0100299 struct orinoco_private *priv = ndev_priv(dev);
David Kilroy6fe9deb2009-02-04 23:05:43 +0000300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return &priv->stats;
302}
David Kilroy593ef092010-05-01 14:05:39 +0100303EXPORT_SYMBOL(orinoco_get_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
David Kilroy593ef092010-05-01 14:05:39 +0100305void orinoco_set_multicast_list(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
David Kilroyea60a6a2009-06-18 23:21:26 +0100307 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 unsigned long flags;
309
310 if (orinoco_lock(priv, &flags) != 0) {
311 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
312 "called when hw_unavailable\n", dev->name);
313 return;
314 }
315
316 __orinoco_set_multicast_list(dev);
317 orinoco_unlock(priv, &flags);
318}
David Kilroy593ef092010-05-01 14:05:39 +0100319EXPORT_SYMBOL(orinoco_set_multicast_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
David Kilroy593ef092010-05-01 14:05:39 +0100321int orinoco_change_mtu(struct net_device *dev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
David Kilroyea60a6a2009-06-18 23:21:26 +0100323 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
David Kilroya94e8422009-02-04 23:05:44 +0000325 if ((new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return -EINVAL;
327
Johannes Berg2c7060022008-10-30 22:09:54 +0100328 /* MTU + encapsulation + header length */
David Kilroya94e8422009-02-04 23:05:44 +0000329 if ((new_mtu + ENCAPS_OVERHEAD + sizeof(struct ieee80211_hdr)) >
330 (priv->nicbuf_size - ETH_HLEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return -EINVAL;
332
333 dev->mtu = new_mtu;
334
335 return 0;
336}
David Kilroy593ef092010-05-01 14:05:39 +0100337EXPORT_SYMBOL(orinoco_change_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339/********************************************************************/
340/* Tx path */
341/********************************************************************/
342
David Kilroybac6faf2010-05-04 22:54:41 +0100343/* Add encapsulation and MIC to the existing SKB.
344 * The main xmit routine will then send the whole lot to the card.
345 * Need 8 bytes headroom
346 * Need 8 bytes tailroom
347 *
348 * With encapsulated ethernet II frame
349 * --------
350 * 803.3 header (14 bytes)
351 * dst[6]
352 * -------- src[6]
353 * 803.3 header (14 bytes) len[2]
354 * dst[6] 803.2 header (8 bytes)
355 * src[6] encaps[6]
356 * len[2] <- leave alone -> len[2]
357 * -------- -------- <-- 0
358 * Payload Payload
359 * ... ...
360 *
361 * -------- --------
362 * MIC (8 bytes)
363 * --------
364 *
365 * returns 0 on success, -ENOMEM on error.
366 */
367int orinoco_process_xmit_skb(struct sk_buff *skb,
368 struct net_device *dev,
369 struct orinoco_private *priv,
370 int *tx_control,
371 u8 *mic_buf)
372{
373 struct orinoco_tkip_key *key;
374 struct ethhdr *eh;
375 int do_mic;
376
377 key = (struct orinoco_tkip_key *) priv->keys[priv->tx_key].key;
378
379 do_mic = ((priv->encode_alg == ORINOCO_ALG_TKIP) &&
380 (key != NULL));
381
382 if (do_mic)
383 *tx_control |= (priv->tx_key << HERMES_MIC_KEY_ID_SHIFT) |
384 HERMES_TXCTRL_MIC;
385
386 eh = (struct ethhdr *)skb->data;
387
388 /* Encapsulate Ethernet-II frames */
389 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
390 struct header_struct {
391 struct ethhdr eth; /* 802.3 header */
392 u8 encap[6]; /* 802.2 header */
Eric Dumazetba2d3582010-06-02 18:10:09 +0000393 } __packed hdr;
David Kilroybac6faf2010-05-04 22:54:41 +0100394 int len = skb->len + sizeof(encaps_hdr) - (2 * ETH_ALEN);
395
396 if (skb_headroom(skb) < ENCAPS_OVERHEAD) {
397 if (net_ratelimit())
398 printk(KERN_ERR
399 "%s: Not enough headroom for 802.2 headers %d\n",
400 dev->name, skb_headroom(skb));
401 return -ENOMEM;
402 }
403
404 /* Fill in new header */
405 memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
406 hdr.eth.h_proto = htons(len);
407 memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
408
409 /* Make room for the new header, and copy it in */
410 eh = (struct ethhdr *) skb_push(skb, ENCAPS_OVERHEAD);
411 memcpy(eh, &hdr, sizeof(hdr));
412 }
413
414 /* Calculate Michael MIC */
415 if (do_mic) {
416 size_t len = skb->len - ETH_HLEN;
417 u8 *mic = &mic_buf[0];
418
419 /* Have to write to an even address, so copy the spare
420 * byte across */
421 if (skb->len % 2) {
422 *mic = skb->data[skb->len - 1];
423 mic++;
424 }
425
426 orinoco_mic(priv->tx_tfm_mic, key->tx_mic,
427 eh->h_dest, eh->h_source, 0 /* priority */,
428 skb->data + ETH_HLEN,
429 len, mic);
430 }
431
432 return 0;
433}
434EXPORT_SYMBOL(orinoco_process_xmit_skb);
435
Stephen Hemmingerd0cf9c02009-08-31 19:50:57 +0000436static netdev_tx_t orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
David Kilroyea60a6a2009-06-18 23:21:26 +0100438 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 struct net_device_stats *stats = &priv->stats;
Pavel Roskin933d5942011-07-13 11:19:57 -0400440 struct hermes *hw = &priv->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 int err = 0;
442 u16 txfid = priv->txfid;
David Kilroy6eecad72008-08-21 23:27:56 +0100443 int tx_control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 unsigned long flags;
Pavel Roskin933d5942011-07-13 11:19:57 -0400445 u8 mic_buf[MICHAEL_MIC_LEN + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
David Kilroya94e8422009-02-04 23:05:44 +0000447 if (!netif_running(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 printk(KERN_ERR "%s: Tx on stopped device!\n",
449 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400450 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
David Kilroy6fe9deb2009-02-04 23:05:43 +0000452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (netif_queue_stopped(dev)) {
David Kilroy6fe9deb2009-02-04 23:05:43 +0000454 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400456 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
David Kilroy6fe9deb2009-02-04 23:05:43 +0000458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (orinoco_lock(priv, &flags) != 0) {
460 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
461 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400462 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464
David Kilroy5217c572009-06-18 23:21:32 +0100465 if (!netif_carrier_ok(dev) ||
466 (priv->iw_mode == NL80211_IFTYPE_MONITOR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /* Oops, the firmware hasn't established a connection,
David Kilroy6fe9deb2009-02-04 23:05:43 +0000468 silently drop the packet (this seems to be the
469 safest approach). */
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400470 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472
Pavel Roskin8d5be082006-04-07 04:10:41 -0400473 /* Check packet length */
Pavel Roskina28dc812006-04-07 04:10:45 -0400474 if (skb->len < ETH_HLEN)
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400475 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
David Kilroy6eecad72008-08-21 23:27:56 +0100477 tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
David Kilroybac6faf2010-05-04 22:54:41 +0100479 err = orinoco_process_xmit_skb(skb, dev, priv, &tx_control,
480 &mic_buf[0]);
481 if (err)
482 goto drop;
David Kilroy23edcc42008-08-21 23:28:05 +0100483
David Kilroy6eecad72008-08-21 23:27:56 +0100484 if (priv->has_alt_txcntl) {
485 /* WPA enabled firmwares have tx_cntl at the end of
486 * the 802.11 header. So write zeroed descriptor and
487 * 802.11 header at the same time
488 */
489 char desc[HERMES_802_3_OFFSET];
490 __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
491
492 memset(&desc, 0, sizeof(desc));
493
494 *txcntl = cpu_to_le16(tx_control);
David Kilroyb42f2072010-05-01 14:05:38 +0100495 err = hw->ops->bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
496 txfid, 0);
David Kilroy6eecad72008-08-21 23:27:56 +0100497 if (err) {
498 if (net_ratelimit())
499 printk(KERN_ERR "%s: Error %d writing Tx "
500 "descriptor to BAP\n", dev->name, err);
501 goto busy;
502 }
503 } else {
504 struct hermes_tx_descriptor desc;
505
506 memset(&desc, 0, sizeof(desc));
507
508 desc.tx_control = cpu_to_le16(tx_control);
David Kilroyb42f2072010-05-01 14:05:38 +0100509 err = hw->ops->bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
510 txfid, 0);
David Kilroy6eecad72008-08-21 23:27:56 +0100511 if (err) {
512 if (net_ratelimit())
513 printk(KERN_ERR "%s: Error %d writing Tx "
514 "descriptor to BAP\n", dev->name, err);
515 goto busy;
516 }
517
518 /* Clear the 802.11 header and data length fields - some
519 * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
520 * if this isn't done. */
521 hermes_clear_words(hw, HERMES_DATA0,
522 HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
David Kilroyb42f2072010-05-01 14:05:38 +0100525 err = hw->ops->bap_pwrite(hw, USER_BAP, skb->data, skb->len,
526 txfid, HERMES_802_3_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (err) {
528 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
529 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400530 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532
David Kilroybac6faf2010-05-04 22:54:41 +0100533 if (tx_control & HERMES_TXCTRL_MIC) {
534 size_t offset = HERMES_802_3_OFFSET + skb->len;
535 size_t len = MICHAEL_MIC_LEN;
David Kilroy23edcc42008-08-21 23:28:05 +0100536
David Kilroybac6faf2010-05-04 22:54:41 +0100537 if (offset % 2) {
538 offset--;
539 len++;
David Kilroy23edcc42008-08-21 23:28:05 +0100540 }
David Kilroyb42f2072010-05-01 14:05:38 +0100541 err = hw->ops->bap_pwrite(hw, USER_BAP, &mic_buf[0], len,
David Kilroybac6faf2010-05-04 22:54:41 +0100542 txfid, offset);
David Kilroy23edcc42008-08-21 23:28:05 +0100543 if (err) {
544 printk(KERN_ERR "%s: Error %d writing MIC to BAP\n",
545 dev->name, err);
546 goto busy;
547 }
548 }
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /* Finally, we actually initiate the send */
551 netif_stop_queue(dev);
552
David Kilroyb42f2072010-05-01 14:05:38 +0100553 err = hw->ops->cmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 txfid, NULL);
555 if (err) {
556 netif_start_queue(dev);
Andrew Mortonc367c212005-10-19 21:23:44 -0700557 if (net_ratelimit())
558 printk(KERN_ERR "%s: Error %d transmitting packet\n",
559 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400560 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562
David Kilroy23edcc42008-08-21 23:28:05 +0100563 stats->tx_bytes += HERMES_802_3_OFFSET + skb->len;
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400564 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400566 drop:
567 stats->tx_errors++;
568 stats->tx_dropped++;
569
570 ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 dev_kfree_skb(skb);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400573 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400575 busy:
Jiri Benc2c1bd262006-04-07 04:10:47 -0400576 if (err == -EIO)
577 schedule_work(&priv->reset_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 orinoco_unlock(priv, &flags);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400579 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Pavel Roskin933d5942011-07-13 11:19:57 -0400582static void __orinoco_ev_alloc(struct net_device *dev, struct hermes *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
David Kilroyea60a6a2009-06-18 23:21:26 +0100584 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 u16 fid = hermes_read_regn(hw, ALLOCFID);
586
587 if (fid != priv->txfid) {
588 if (fid != DUMMY_FID)
589 printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
590 dev->name, fid);
591 return;
592 }
593
594 hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
595}
596
Pavel Roskin933d5942011-07-13 11:19:57 -0400597static void __orinoco_ev_tx(struct net_device *dev, struct hermes *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
David Kilroyea60a6a2009-06-18 23:21:26 +0100599 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 struct net_device_stats *stats = &priv->stats;
601
602 stats->tx_packets++;
603
604 netif_wake_queue(dev);
605
606 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
607}
608
Pavel Roskin933d5942011-07-13 11:19:57 -0400609static void __orinoco_ev_txexc(struct net_device *dev, struct hermes *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
David Kilroyea60a6a2009-06-18 23:21:26 +0100611 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 struct net_device_stats *stats = &priv->stats;
613 u16 fid = hermes_read_regn(hw, TXCOMPLFID);
Pavel Roskind133ae42005-09-23 04:18:06 -0400614 u16 status;
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400615 struct hermes_txexc_data hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 int err = 0;
617
618 if (fid == DUMMY_FID)
619 return; /* Nothing's really happened */
620
Pavel Roskin48ca7032005-09-23 04:18:06 -0400621 /* Read part of the frame header - we need status and addr1 */
David Kilroyb42f2072010-05-01 14:05:38 +0100622 err = hw->ops->bap_pread(hw, IRQ_BAP, &hdr,
623 sizeof(struct hermes_txexc_data),
624 fid, 0);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200625
626 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
627 stats->tx_errors++;
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (err) {
630 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
631 "(FID=%04X error %d)\n",
632 dev->name, fid, err);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200633 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
David Kilroy6fe9deb2009-02-04 23:05:43 +0000635
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200636 DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
637 err, fid);
David Kilroy6fe9deb2009-02-04 23:05:43 +0000638
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200639 /* We produce a TXDROP event only for retry or lifetime
640 * exceeded, because that's the only status that really mean
641 * that this particular node went away.
642 * Other errors means that *we* screwed up. - Jean II */
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400643 status = le16_to_cpu(hdr.desc.status);
Pavel Roskind133ae42005-09-23 04:18:06 -0400644 if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200645 union iwreq_data wrqu;
646
647 /* Copy 802.11 dest address.
648 * We use the 802.11 header because the frame may
649 * not be 802.3 or may be mangled...
650 * In Ad-Hoc mode, it will be the node address.
651 * In managed mode, it will be most likely the AP addr
652 * User space will figure out how to convert it to
653 * whatever it needs (IP address or else).
654 * - Jean II */
655 memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
656 wrqu.addr.sa_family = ARPHRD_ETHER;
657
658 /* Send event to user space */
659 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
David Kilroy593ef092010-05-01 14:05:39 +0100665void orinoco_tx_timeout(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
David Kilroyea60a6a2009-06-18 23:21:26 +0100667 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 struct net_device_stats *stats = &priv->stats;
669 struct hermes *hw = &priv->hw;
670
671 printk(KERN_WARNING "%s: Tx timeout! "
672 "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
673 dev->name, hermes_read_regn(hw, ALLOCFID),
674 hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
675
676 stats->tx_errors++;
677
678 schedule_work(&priv->reset_work);
679}
David Kilroy593ef092010-05-01 14:05:39 +0100680EXPORT_SYMBOL(orinoco_tx_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682/********************************************************************/
683/* Rx path (data frames) */
684/********************************************************************/
685
686/* Does the frame have a SNAP header indicating it should be
687 * de-encapsulated to Ethernet-II? */
688static inline int is_ethersnap(void *_hdr)
689{
690 u8 *hdr = _hdr;
691
692 /* We de-encapsulate all packets which, a) have SNAP headers
693 * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
694 * and where b) the OUI of the SNAP header is 00:00:00 or
695 * 00:00:f8 - we need both because different APs appear to use
696 * different OUIs for some reason */
697 return (memcmp(hdr, &encaps_hdr, 5) == 0)
David Kilroya94e8422009-02-04 23:05:44 +0000698 && ((hdr[5] == 0x00) || (hdr[5] == 0xf8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
701static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
702 int level, int noise)
703{
Pavel Roskin343c6862005-09-09 18:43:02 -0400704 struct iw_quality wstats;
705 wstats.level = level - 0x95;
706 wstats.noise = noise - 0x95;
707 wstats.qual = (level > noise) ? (level - noise) : 0;
Andrey Borzenkovf941f852008-11-15 17:15:09 +0300708 wstats.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
Pavel Roskin343c6862005-09-09 18:43:02 -0400709 /* Update spy records */
710 wireless_spy_update(dev, mac, &wstats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
713static void orinoco_stat_gather(struct net_device *dev,
714 struct sk_buff *skb,
715 struct hermes_rx_descriptor *desc)
716{
David Kilroyea60a6a2009-06-18 23:21:26 +0100717 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 /* Using spy support with lots of Rx packets, like in an
720 * infrastructure (AP), will really slow down everything, because
721 * the MAC address must be compared to each entry of the spy list.
722 * If the user really asks for it (set some address in the
723 * spy list), we do it, but he will pay the price.
724 * Note that to get here, you need both WIRELESS_SPY
725 * compiled in AND some addresses in the list !!!
726 */
727 /* Note : gcc will optimise the whole section away if
728 * WIRELESS_SPY is not defined... - Jean II */
729 if (SPY_NUMBER(priv)) {
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700730 orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 desc->signal, desc->silence);
732 }
733}
734
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200735/*
736 * orinoco_rx_monitor - handle received monitor frames.
737 *
738 * Arguments:
739 * dev network device
740 * rxfid received FID
741 * desc rx descriptor of the frame
742 *
743 * Call context: interrupt
744 */
745static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
746 struct hermes_rx_descriptor *desc)
747{
748 u32 hdrlen = 30; /* return full header by default */
749 u32 datalen = 0;
750 u16 fc;
751 int err;
752 int len;
753 struct sk_buff *skb;
David Kilroyea60a6a2009-06-18 23:21:26 +0100754 struct orinoco_private *priv = ndev_priv(dev);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200755 struct net_device_stats *stats = &priv->stats;
Pavel Roskin933d5942011-07-13 11:19:57 -0400756 struct hermes *hw = &priv->hw;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200757
758 len = le16_to_cpu(desc->data_len);
759
760 /* Determine the size of the header and the data */
761 fc = le16_to_cpu(desc->frame_ctl);
762 switch (fc & IEEE80211_FCTL_FTYPE) {
763 case IEEE80211_FTYPE_DATA:
764 if ((fc & IEEE80211_FCTL_TODS)
765 && (fc & IEEE80211_FCTL_FROMDS))
766 hdrlen = 30;
767 else
768 hdrlen = 24;
769 datalen = len;
770 break;
771 case IEEE80211_FTYPE_MGMT:
772 hdrlen = 24;
773 datalen = len;
774 break;
775 case IEEE80211_FTYPE_CTL:
776 switch (fc & IEEE80211_FCTL_STYPE) {
777 case IEEE80211_STYPE_PSPOLL:
778 case IEEE80211_STYPE_RTS:
779 case IEEE80211_STYPE_CFEND:
780 case IEEE80211_STYPE_CFENDACK:
781 hdrlen = 16;
782 break;
783 case IEEE80211_STYPE_CTS:
784 case IEEE80211_STYPE_ACK:
785 hdrlen = 10;
786 break;
787 }
788 break;
789 default:
790 /* Unknown frame type */
791 break;
792 }
793
794 /* sanity check the length */
Johannes Berg2c7060022008-10-30 22:09:54 +0100795 if (datalen > IEEE80211_MAX_DATA_LEN + 12) {
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200796 printk(KERN_DEBUG "%s: oversized monitor frame, "
797 "data length = %d\n", dev->name, datalen);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200798 stats->rx_length_errors++;
799 goto update_stats;
800 }
801
802 skb = dev_alloc_skb(hdrlen + datalen);
803 if (!skb) {
804 printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
805 dev->name);
Florin Malitabb6e0932006-05-22 22:35:30 -0700806 goto update_stats;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200807 }
808
809 /* Copy the 802.11 header to the skb */
810 memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700811 skb_reset_mac_header(skb);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200812
813 /* If any, copy the data from the card to the skb */
814 if (datalen > 0) {
David Kilroyb42f2072010-05-01 14:05:38 +0100815 err = hw->ops->bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
816 ALIGN(datalen, 2), rxfid,
817 HERMES_802_2_OFFSET);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200818 if (err) {
819 printk(KERN_ERR "%s: error %d reading monitor frame\n",
820 dev->name, err);
821 goto drop;
822 }
823 }
824
825 skb->dev = dev;
826 skb->ip_summed = CHECKSUM_NONE;
827 skb->pkt_type = PACKET_OTHERHOST;
Harvey Harrisonc1b4aa32009-01-29 13:26:44 -0800828 skb->protocol = cpu_to_be16(ETH_P_802_2);
David Kilroy6fe9deb2009-02-04 23:05:43 +0000829
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200830 stats->rx_packets++;
831 stats->rx_bytes += skb->len;
832
833 netif_rx(skb);
834 return;
835
836 drop:
837 dev_kfree_skb_irq(skb);
838 update_stats:
839 stats->rx_errors++;
840 stats->rx_dropped++;
841}
842
Pavel Roskin933d5942011-07-13 11:19:57 -0400843void __orinoco_ev_rx(struct net_device *dev, struct hermes *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
David Kilroyea60a6a2009-06-18 23:21:26 +0100845 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 struct net_device_stats *stats = &priv->stats;
847 struct iw_statistics *wstats = &priv->wstats;
848 struct sk_buff *skb = NULL;
David Kilroy31afcef2008-08-21 23:28:04 +0100849 u16 rxfid, status;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200850 int length;
David Kilroy31afcef2008-08-21 23:28:04 +0100851 struct hermes_rx_descriptor *desc;
852 struct orinoco_rx_data *rx_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 int err;
854
David Kilroy31afcef2008-08-21 23:28:04 +0100855 desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
856 if (!desc) {
857 printk(KERN_WARNING
858 "%s: Can't allocate space for RX descriptor\n",
859 dev->name);
860 goto update_stats;
861 }
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 rxfid = hermes_read_regn(hw, RXFID);
864
David Kilroyb42f2072010-05-01 14:05:38 +0100865 err = hw->ops->bap_pread(hw, IRQ_BAP, desc, sizeof(*desc),
866 rxfid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (err) {
868 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
869 "Frame dropped.\n", dev->name, err);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200870 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872
David Kilroy31afcef2008-08-21 23:28:04 +0100873 status = le16_to_cpu(desc->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200875 if (status & HERMES_RXSTAT_BADCRC) {
876 DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
877 dev->name);
878 stats->rx_crc_errors++;
879 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200882 /* Handle frames in monitor mode */
David Kilroy5217c572009-06-18 23:21:32 +0100883 if (priv->iw_mode == NL80211_IFTYPE_MONITOR) {
David Kilroy31afcef2008-08-21 23:28:04 +0100884 orinoco_rx_monitor(dev, rxfid, desc);
885 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200888 if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
889 DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
890 dev->name);
891 wstats->discard.code++;
892 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
894
David Kilroy31afcef2008-08-21 23:28:04 +0100895 length = le16_to_cpu(desc->data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 /* Sanity checks */
898 if (length < 3) { /* No for even an 802.2 LLC header */
899 /* At least on Symbol firmware with PCF we get quite a
David Kilroy6fe9deb2009-02-04 23:05:43 +0000900 lot of these legitimately - Poll frames with no
901 data. */
David Kilroy31afcef2008-08-21 23:28:04 +0100902 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
Johannes Berg2c7060022008-10-30 22:09:54 +0100904 if (length > IEEE80211_MAX_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
906 dev->name, length);
907 stats->rx_length_errors++;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200908 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910
David Kilroy23edcc42008-08-21 23:28:05 +0100911 /* Payload size does not include Michael MIC. Increase payload
912 * size to read it together with the data. */
913 if (status & HERMES_RXSTAT_MIC)
914 length += MICHAEL_MIC_LEN;
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 /* We need space for the packet data itself, plus an ethernet
917 header, plus 2 bytes so we can align the IP header on a
918 32bit boundary, plus 1 byte so we can read in odd length
919 packets from the card, which has an IO granularity of 16
David Kilroy6fe9deb2009-02-04 23:05:43 +0000920 bits */
Pavel Roskin933d5942011-07-13 11:19:57 -0400921 skb = dev_alloc_skb(length + ETH_HLEN + 2 + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (!skb) {
923 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
924 dev->name);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200925 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
927
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200928 /* We'll prepend the header, so reserve space for it. The worst
929 case is no decapsulation, when 802.3 header is prepended and
930 nothing is removed. 2 is for aligning the IP header. */
931 skb_reserve(skb, ETH_HLEN + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
David Kilroyb42f2072010-05-01 14:05:38 +0100933 err = hw->ops->bap_pread(hw, IRQ_BAP, skb_put(skb, length),
934 ALIGN(length, 2), rxfid,
935 HERMES_802_2_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (err) {
937 printk(KERN_ERR "%s: error %d reading frame. "
938 "Frame dropped.\n", dev->name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 goto drop;
940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
David Kilroy31afcef2008-08-21 23:28:04 +0100942 /* Add desc and skb to rx queue */
943 rx_data = kzalloc(sizeof(*rx_data), GFP_ATOMIC);
Joe Perchese404dec2012-01-29 12:56:23 +0000944 if (!rx_data)
David Kilroy31afcef2008-08-21 23:28:04 +0100945 goto drop;
Joe Perchese404dec2012-01-29 12:56:23 +0000946
David Kilroy31afcef2008-08-21 23:28:04 +0100947 rx_data->desc = desc;
948 rx_data->skb = skb;
949 list_add_tail(&rx_data->list, &priv->rx_list);
950 tasklet_schedule(&priv->rx_tasklet);
951
952 return;
953
954drop:
955 dev_kfree_skb_irq(skb);
956update_stats:
957 stats->rx_errors++;
958 stats->rx_dropped++;
959out:
960 kfree(desc);
961}
David Kilroy9afac702010-05-01 14:05:41 +0100962EXPORT_SYMBOL(__orinoco_ev_rx);
David Kilroy31afcef2008-08-21 23:28:04 +0100963
964static void orinoco_rx(struct net_device *dev,
965 struct hermes_rx_descriptor *desc,
966 struct sk_buff *skb)
967{
David Kilroyea60a6a2009-06-18 23:21:26 +0100968 struct orinoco_private *priv = ndev_priv(dev);
David Kilroy31afcef2008-08-21 23:28:04 +0100969 struct net_device_stats *stats = &priv->stats;
970 u16 status, fc;
971 int length;
972 struct ethhdr *hdr;
973
974 status = le16_to_cpu(desc->status);
975 length = le16_to_cpu(desc->data_len);
976 fc = le16_to_cpu(desc->frame_ctl);
977
David Kilroy23edcc42008-08-21 23:28:05 +0100978 /* Calculate and check MIC */
979 if (status & HERMES_RXSTAT_MIC) {
David Kilroy4af198f2009-08-05 21:23:32 +0100980 struct orinoco_tkip_key *key;
David Kilroy23edcc42008-08-21 23:28:05 +0100981 int key_id = ((status & HERMES_RXSTAT_MIC_KEY_ID) >>
982 HERMES_MIC_KEY_ID_SHIFT);
983 u8 mic[MICHAEL_MIC_LEN];
984 u8 *rxmic;
985 u8 *src = (fc & IEEE80211_FCTL_FROMDS) ?
986 desc->addr3 : desc->addr2;
987
988 /* Extract Michael MIC from payload */
989 rxmic = skb->data + skb->len - MICHAEL_MIC_LEN;
990
991 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
992 length -= MICHAEL_MIC_LEN;
993
David Kilroy4af198f2009-08-05 21:23:32 +0100994 key = (struct orinoco_tkip_key *) priv->keys[key_id].key;
995
996 if (!key) {
997 printk(KERN_WARNING "%s: Received encrypted frame from "
998 "%pM using key %i, but key is not installed\n",
999 dev->name, src, key_id);
1000 goto drop;
1001 }
1002
1003 orinoco_mic(priv->rx_tfm_mic, key->rx_mic, desc->addr1, src,
David Kilroy23edcc42008-08-21 23:28:05 +01001004 0, /* priority or QoS? */
David Kilroy4af198f2009-08-05 21:23:32 +01001005 skb->data, skb->len, &mic[0]);
David Kilroy23edcc42008-08-21 23:28:05 +01001006
1007 if (memcmp(mic, rxmic,
1008 MICHAEL_MIC_LEN)) {
1009 union iwreq_data wrqu;
1010 struct iw_michaelmicfailure wxmic;
David Kilroy23edcc42008-08-21 23:28:05 +01001011
1012 printk(KERN_WARNING "%s: "
Johannes Berge1749612008-10-27 15:59:26 -07001013 "Invalid Michael MIC in data frame from %pM, "
David Kilroy23edcc42008-08-21 23:28:05 +01001014 "using key %i\n",
Johannes Berge1749612008-10-27 15:59:26 -07001015 dev->name, src, key_id);
David Kilroy23edcc42008-08-21 23:28:05 +01001016
1017 /* TODO: update stats */
1018
1019 /* Notify userspace */
1020 memset(&wxmic, 0, sizeof(wxmic));
1021 wxmic.flags = key_id & IW_MICFAILURE_KEY_ID;
1022 wxmic.flags |= (desc->addr1[0] & 1) ?
1023 IW_MICFAILURE_GROUP : IW_MICFAILURE_PAIRWISE;
1024 wxmic.src_addr.sa_family = ARPHRD_ETHER;
1025 memcpy(wxmic.src_addr.sa_data, src, ETH_ALEN);
1026
1027 (void) orinoco_hw_get_tkip_iv(priv, key_id,
1028 &wxmic.tsc[0]);
1029
1030 memset(&wrqu, 0, sizeof(wrqu));
1031 wrqu.data.length = sizeof(wxmic);
1032 wireless_send_event(dev, IWEVMICHAELMICFAILURE, &wrqu,
1033 (char *) &wxmic);
1034
1035 goto drop;
1036 }
1037 }
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 /* Handle decapsulation
1040 * In most cases, the firmware tell us about SNAP frames.
1041 * For some reason, the SNAP frames sent by LinkSys APs
1042 * are not properly recognised by most firmwares.
1043 * So, check ourselves */
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001044 if (length >= ENCAPS_OVERHEAD &&
1045 (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
1046 ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
1047 is_ethersnap(skb->data))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 /* These indicate a SNAP within 802.2 LLC within
1049 802.11 frame which we'll need to de-encapsulate to
1050 the original EthernetII frame. */
David Kilroyb2f30a02009-02-04 23:05:46 +00001051 hdr = (struct ethhdr *)skb_push(skb,
1052 ETH_HLEN - ENCAPS_OVERHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 } else {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001054 /* 802.3 frame - prepend 802.3 header as is */
1055 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
1056 hdr->h_proto = htons(length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
David Kilroy31afcef2008-08-21 23:28:04 +01001058 memcpy(hdr->h_dest, desc->addr1, ETH_ALEN);
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001059 if (fc & IEEE80211_FCTL_FROMDS)
David Kilroy31afcef2008-08-21 23:28:04 +01001060 memcpy(hdr->h_source, desc->addr3, ETH_ALEN);
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001061 else
David Kilroy31afcef2008-08-21 23:28:04 +01001062 memcpy(hdr->h_source, desc->addr2, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 skb->protocol = eth_type_trans(skb, dev);
1065 skb->ip_summed = CHECKSUM_NONE;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001066 if (fc & IEEE80211_FCTL_TODS)
1067 skb->pkt_type = PACKET_OTHERHOST;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /* Process the wireless stats if needed */
David Kilroy31afcef2008-08-21 23:28:04 +01001070 orinoco_stat_gather(dev, skb, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 /* Pass the packet to the networking stack */
1073 netif_rx(skb);
1074 stats->rx_packets++;
1075 stats->rx_bytes += length;
1076
1077 return;
David Kilroy23edcc42008-08-21 23:28:05 +01001078
1079 drop:
1080 dev_kfree_skb(skb);
1081 stats->rx_errors++;
1082 stats->rx_dropped++;
David Kilroy31afcef2008-08-21 23:28:04 +01001083}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
David Kilroy31afcef2008-08-21 23:28:04 +01001085static void orinoco_rx_isr_tasklet(unsigned long data)
1086{
David Kilroy53819562009-06-18 23:21:28 +01001087 struct orinoco_private *priv = (struct orinoco_private *) data;
1088 struct net_device *dev = priv->ndev;
David Kilroy31afcef2008-08-21 23:28:04 +01001089 struct orinoco_rx_data *rx_data, *temp;
1090 struct hermes_rx_descriptor *desc;
1091 struct sk_buff *skb;
David Kilroy20953ad2009-01-07 00:23:55 +00001092 unsigned long flags;
1093
1094 /* orinoco_rx requires the driver lock, and we also need to
1095 * protect priv->rx_list, so just hold the lock over the
1096 * lot.
1097 *
1098 * If orinoco_lock fails, we've unplugged the card. In this
1099 * case just abort. */
1100 if (orinoco_lock(priv, &flags) != 0)
1101 return;
David Kilroy31afcef2008-08-21 23:28:04 +01001102
1103 /* extract desc and skb from queue */
1104 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
1105 desc = rx_data->desc;
1106 skb = rx_data->skb;
1107 list_del(&rx_data->list);
1108 kfree(rx_data);
1109
1110 orinoco_rx(dev, desc, skb);
1111
1112 kfree(desc);
1113 }
David Kilroy20953ad2009-01-07 00:23:55 +00001114
1115 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
1118/********************************************************************/
1119/* Rx path (info frames) */
1120/********************************************************************/
1121
1122static void print_linkstatus(struct net_device *dev, u16 status)
1123{
David Kilroy21312662009-02-04 23:05:47 +00001124 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 if (suppress_linkstatus)
1127 return;
1128
1129 switch (status) {
1130 case HERMES_LINKSTATUS_NOT_CONNECTED:
1131 s = "Not Connected";
1132 break;
1133 case HERMES_LINKSTATUS_CONNECTED:
1134 s = "Connected";
1135 break;
1136 case HERMES_LINKSTATUS_DISCONNECTED:
1137 s = "Disconnected";
1138 break;
1139 case HERMES_LINKSTATUS_AP_CHANGE:
1140 s = "AP Changed";
1141 break;
1142 case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1143 s = "AP Out of Range";
1144 break;
1145 case HERMES_LINKSTATUS_AP_IN_RANGE:
1146 s = "AP In Range";
1147 break;
1148 case HERMES_LINKSTATUS_ASSOC_FAILED:
1149 s = "Association Failed";
1150 break;
1151 default:
1152 s = "UNKNOWN";
1153 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001154
Pavel Roskin11eaea42009-01-18 23:20:58 -05001155 printk(KERN_DEBUG "%s: New link status: %s (%04x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 dev->name, s, status);
1157}
1158
Christoph Hellwig16739b02005-06-19 01:27:51 +02001159/* Search scan results for requested BSSID, join it if found */
David Howellsc4028952006-11-22 14:57:56 +00001160static void orinoco_join_ap(struct work_struct *work)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001161{
David Howellsc4028952006-11-22 14:57:56 +00001162 struct orinoco_private *priv =
1163 container_of(work, struct orinoco_private, join_work);
1164 struct net_device *dev = priv->ndev;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001165 struct hermes *hw = &priv->hw;
1166 int err;
1167 unsigned long flags;
1168 struct join_req {
1169 u8 bssid[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -04001170 __le16 channel;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001171 } __packed req;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001172 const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001173 struct prism2_scan_apinfo *atom = NULL;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001174 int offset = 4;
Pavel Roskinc89cc222005-09-01 20:06:06 -04001175 int found = 0;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001176 u8 *buf;
1177 u16 len;
1178
1179 /* Allocate buffer for scan results */
1180 buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
David Kilroya94e8422009-02-04 23:05:44 +00001181 if (!buf)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001182 return;
1183
1184 if (orinoco_lock(priv, &flags) != 0)
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001185 goto fail_lock;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001186
1187 /* Sanity checks in case user changed something in the meantime */
David Kilroya94e8422009-02-04 23:05:44 +00001188 if (!priv->bssid_fixed)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001189 goto out;
1190
1191 if (strlen(priv->desired_essid) == 0)
1192 goto out;
1193
1194 /* Read scan results from the firmware */
David Kilroyb42f2072010-05-01 14:05:38 +01001195 err = hw->ops->read_ltv(hw, USER_BAP,
1196 HERMES_RID_SCANRESULTSTABLE,
1197 MAX_SCAN_LEN, &len, buf);
Christoph Hellwig16739b02005-06-19 01:27:51 +02001198 if (err) {
1199 printk(KERN_ERR "%s: Cannot read scan results\n",
1200 dev->name);
1201 goto out;
1202 }
1203
1204 len = HERMES_RECLEN_TO_BYTES(len);
1205
1206 /* Go through the scan results looking for the channel of the AP
1207 * we were requested to join */
1208 for (; offset + atom_len <= len; offset += atom_len) {
1209 atom = (struct prism2_scan_apinfo *) (buf + offset);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001210 if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1211 found = 1;
1212 break;
1213 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001214 }
1215
David Kilroya94e8422009-02-04 23:05:44 +00001216 if (!found) {
Pavel Roskinc89cc222005-09-01 20:06:06 -04001217 DEBUG(1, "%s: Requested AP not found in scan results\n",
1218 dev->name);
1219 goto out;
1220 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001221
Christoph Hellwig16739b02005-06-19 01:27:51 +02001222 memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1223 req.channel = atom->channel; /* both are little-endian */
1224 err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1225 &req);
1226 if (err)
1227 printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1228
1229 out:
Christoph Hellwig16739b02005-06-19 01:27:51 +02001230 orinoco_unlock(priv, &flags);
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001231
1232 fail_lock:
1233 kfree(buf);
Christoph Hellwig16739b02005-06-19 01:27:51 +02001234}
1235
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001236/* Send new BSSID to userspace */
David Kilroy6cd90b12008-08-21 23:28:00 +01001237static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001238{
David Howellsc4028952006-11-22 14:57:56 +00001239 struct net_device *dev = priv->ndev;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001240 struct hermes *hw = &priv->hw;
1241 union iwreq_data wrqu;
1242 int err;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001243
David Kilroyb42f2072010-05-01 14:05:38 +01001244 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
1245 ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001246 if (err != 0)
David Kilroy6cd90b12008-08-21 23:28:00 +01001247 return;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001248
1249 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1250
1251 /* Send event to user space */
1252 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001253}
1254
David Kilroy06009fd2008-08-21 23:28:03 +01001255static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
1256{
1257 struct net_device *dev = priv->ndev;
1258 struct hermes *hw = &priv->hw;
1259 union iwreq_data wrqu;
1260 int err;
1261 u8 buf[88];
1262 u8 *ie;
1263
1264 if (!priv->has_wpa)
1265 return;
1266
David Kilroyb42f2072010-05-01 14:05:38 +01001267 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
1268 sizeof(buf), NULL, &buf);
David Kilroy06009fd2008-08-21 23:28:03 +01001269 if (err != 0)
1270 return;
1271
1272 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1273 if (ie) {
1274 int rem = sizeof(buf) - (ie - &buf[0]);
1275 wrqu.data.length = ie[1] + 2;
1276 if (wrqu.data.length > rem)
1277 wrqu.data.length = rem;
1278
1279 if (wrqu.data.length)
1280 /* Send event to user space */
1281 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
1282 }
1283}
1284
1285static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
1286{
1287 struct net_device *dev = priv->ndev;
1288 struct hermes *hw = &priv->hw;
1289 union iwreq_data wrqu;
1290 int err;
1291 u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
1292 u8 *ie;
1293
1294 if (!priv->has_wpa)
1295 return;
1296
David Kilroyb42f2072010-05-01 14:05:38 +01001297 err = hw->ops->read_ltv(hw, USER_BAP,
1298 HERMES_RID_CURRENT_ASSOC_RESP_INFO,
1299 sizeof(buf), NULL, &buf);
David Kilroy06009fd2008-08-21 23:28:03 +01001300 if (err != 0)
1301 return;
1302
1303 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1304 if (ie) {
1305 int rem = sizeof(buf) - (ie - &buf[0]);
1306 wrqu.data.length = ie[1] + 2;
1307 if (wrqu.data.length > rem)
1308 wrqu.data.length = rem;
1309
1310 if (wrqu.data.length)
1311 /* Send event to user space */
1312 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
1313 }
1314}
1315
David Kilroy6cd90b12008-08-21 23:28:00 +01001316static void orinoco_send_wevents(struct work_struct *work)
1317{
1318 struct orinoco_private *priv =
1319 container_of(work, struct orinoco_private, wevent_work);
1320 unsigned long flags;
1321
1322 if (orinoco_lock(priv, &flags) != 0)
1323 return;
1324
David Kilroy06009fd2008-08-21 23:28:03 +01001325 orinoco_send_assocreqie_wevent(priv);
1326 orinoco_send_assocrespie_wevent(priv);
David Kilroy6cd90b12008-08-21 23:28:00 +01001327 orinoco_send_bssid_wevent(priv);
1328
1329 orinoco_unlock(priv, &flags);
1330}
Dan Williams1e3428e2007-10-10 23:56:25 -04001331
David Kilroyc63cdbe2009-06-18 23:21:33 +01001332static void qbuf_scan(struct orinoco_private *priv, void *buf,
1333 int len, int type)
1334{
1335 struct orinoco_scan_data *sd;
1336 unsigned long flags;
1337
1338 sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
1339 sd->buf = buf;
1340 sd->len = len;
1341 sd->type = type;
1342
1343 spin_lock_irqsave(&priv->scan_lock, flags);
1344 list_add_tail(&sd->list, &priv->scan_list);
1345 spin_unlock_irqrestore(&priv->scan_lock, flags);
1346
1347 schedule_work(&priv->process_scan);
1348}
1349
1350static void qabort_scan(struct orinoco_private *priv)
1351{
1352 struct orinoco_scan_data *sd;
1353 unsigned long flags;
1354
1355 sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
1356 sd->len = -1; /* Abort */
1357
1358 spin_lock_irqsave(&priv->scan_lock, flags);
1359 list_add_tail(&sd->list, &priv->scan_list);
1360 spin_unlock_irqrestore(&priv->scan_lock, flags);
1361
1362 schedule_work(&priv->process_scan);
1363}
1364
1365static void orinoco_process_scan_results(struct work_struct *work)
1366{
1367 struct orinoco_private *priv =
1368 container_of(work, struct orinoco_private, process_scan);
1369 struct orinoco_scan_data *sd, *temp;
1370 unsigned long flags;
1371 void *buf;
1372 int len;
1373 int type;
1374
1375 spin_lock_irqsave(&priv->scan_lock, flags);
1376 list_for_each_entry_safe(sd, temp, &priv->scan_list, list) {
David Kilroyc63cdbe2009-06-18 23:21:33 +01001377
1378 buf = sd->buf;
1379 len = sd->len;
1380 type = sd->type;
1381
1382 list_del(&sd->list);
Joe Gunn3bf184a2011-03-15 08:41:19 -07001383 spin_unlock_irqrestore(&priv->scan_lock, flags);
David Kilroyc63cdbe2009-06-18 23:21:33 +01001384 kfree(sd);
1385
1386 if (len > 0) {
1387 if (type == HERMES_INQ_CHANNELINFO)
1388 orinoco_add_extscan_result(priv, buf, len);
1389 else
1390 orinoco_add_hostscan_results(priv, buf, len);
1391
1392 kfree(buf);
David Kilroycf634952010-11-24 20:33:02 +00001393 } else {
David Kilroyc63cdbe2009-06-18 23:21:33 +01001394 /* Either abort or complete the scan */
David Kilroycf634952010-11-24 20:33:02 +00001395 orinoco_scan_done(priv, (len < 0));
David Kilroyc63cdbe2009-06-18 23:21:33 +01001396 }
1397
1398 spin_lock_irqsave(&priv->scan_lock, flags);
1399 }
1400 spin_unlock_irqrestore(&priv->scan_lock, flags);
1401}
1402
Pavel Roskin933d5942011-07-13 11:19:57 -04001403void __orinoco_ev_info(struct net_device *dev, struct hermes *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
David Kilroyea60a6a2009-06-18 23:21:26 +01001405 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 u16 infofid;
1407 struct {
Pavel Roskind133ae42005-09-23 04:18:06 -04001408 __le16 len;
1409 __le16 type;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001410 } __packed info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 int len, type;
1412 int err;
1413
1414 /* This is an answer to an INQUIRE command that we did earlier,
1415 * or an information "event" generated by the card
1416 * The controller return to us a pseudo frame containing
1417 * the information in question - Jean II */
1418 infofid = hermes_read_regn(hw, INFOFID);
1419
1420 /* Read the info frame header - don't try too hard */
David Kilroyb42f2072010-05-01 14:05:38 +01001421 err = hw->ops->bap_pread(hw, IRQ_BAP, &info, sizeof(info),
1422 infofid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (err) {
1424 printk(KERN_ERR "%s: error %d reading info frame. "
1425 "Frame dropped.\n", dev->name, err);
1426 return;
1427 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
1430 type = le16_to_cpu(info.type);
1431
1432 switch (type) {
1433 case HERMES_INQ_TALLIES: {
1434 struct hermes_tallies_frame tallies;
1435 struct iw_statistics *wstats = &priv->wstats;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (len > sizeof(tallies)) {
1438 printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
1439 dev->name, len);
1440 len = sizeof(tallies);
1441 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001442
David Kilroyb42f2072010-05-01 14:05:38 +01001443 err = hw->ops->bap_pread(hw, IRQ_BAP, &tallies, len,
1444 infofid, sizeof(info));
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001445 if (err)
1446 break;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 /* Increment our various counters */
1449 /* wstats->discard.nwid - no wrong BSSID stuff */
1450 wstats->discard.code +=
1451 le16_to_cpu(tallies.RxWEPUndecryptable);
David Kilroy6fe9deb2009-02-04 23:05:43 +00001452 if (len == sizeof(tallies))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 wstats->discard.code +=
1454 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
1455 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
1456 wstats->discard.misc +=
1457 le16_to_cpu(tallies.TxDiscardsWrongSA);
1458 wstats->discard.fragment +=
1459 le16_to_cpu(tallies.RxMsgInBadMsgFragments);
1460 wstats->discard.retries +=
1461 le16_to_cpu(tallies.TxRetryLimitExceeded);
1462 /* wstats->miss.beacon - no match */
1463 }
1464 break;
1465 case HERMES_INQ_LINKSTATUS: {
1466 struct hermes_linkstatus linkstatus;
1467 u16 newstatus;
1468 int connected;
1469
David Kilroy5217c572009-06-18 23:21:32 +01001470 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001471 break;
1472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if (len != sizeof(linkstatus)) {
1474 printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
1475 dev->name, len);
1476 break;
1477 }
1478
David Kilroyb42f2072010-05-01 14:05:38 +01001479 err = hw->ops->bap_pread(hw, IRQ_BAP, &linkstatus, len,
1480 infofid, sizeof(info));
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001481 if (err)
1482 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 newstatus = le16_to_cpu(linkstatus.linkstatus);
1484
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001485 /* Symbol firmware uses "out of range" to signal that
1486 * the hostscan frame can be requested. */
1487 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
1488 priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
David Kilroyc63cdbe2009-06-18 23:21:33 +01001489 priv->has_hostscan && priv->scan_request) {
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001490 hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
1491 break;
1492 }
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
1495 || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
1496 || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
1497
1498 if (connected)
1499 netif_carrier_on(dev);
David Gibson7bb7c3a2005-05-12 20:02:10 -04001500 else if (!ignore_disconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 netif_carrier_off(dev);
1502
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001503 if (newstatus != priv->last_linkstatus) {
1504 priv->last_linkstatus = newstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 print_linkstatus(dev, newstatus);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001506 /* The info frame contains only one word which is the
1507 * status (see hermes.h). The status is pretty boring
1508 * in itself, that's why we export the new BSSID...
1509 * Jean II */
1510 schedule_work(&priv->wevent_work);
1511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
1513 break;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001514 case HERMES_INQ_SCAN:
David Kilroyc63cdbe2009-06-18 23:21:33 +01001515 if (!priv->scan_request && priv->bssid_fixed &&
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001516 priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
1517 schedule_work(&priv->join_work);
1518 break;
1519 }
1520 /* fall through */
1521 case HERMES_INQ_HOSTSCAN:
1522 case HERMES_INQ_HOSTSCAN_SYMBOL: {
1523 /* Result of a scanning. Contains information about
1524 * cells in the vicinity - Jean II */
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001525 unsigned char *buf;
1526
1527 /* Sanity check */
1528 if (len > 4096) {
1529 printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
1530 dev->name, len);
David Kilroyc63cdbe2009-06-18 23:21:33 +01001531 qabort_scan(priv);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001532 break;
1533 }
1534
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001535 /* Allocate buffer for results */
1536 buf = kmalloc(len, GFP_ATOMIC);
David Kilroyc63cdbe2009-06-18 23:21:33 +01001537 if (buf == NULL) {
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001538 /* No memory, so can't printk()... */
David Kilroyc63cdbe2009-06-18 23:21:33 +01001539 qabort_scan(priv);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001540 break;
David Kilroyc63cdbe2009-06-18 23:21:33 +01001541 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001542
1543 /* Read scan data */
David Kilroyb42f2072010-05-01 14:05:38 +01001544 err = hw->ops->bap_pread(hw, IRQ_BAP, (void *) buf, len,
1545 infofid, sizeof(info));
Pavel Roskin708218b2005-09-01 20:05:19 -04001546 if (err) {
1547 kfree(buf);
David Kilroyc63cdbe2009-06-18 23:21:33 +01001548 qabort_scan(priv);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001549 break;
Pavel Roskin708218b2005-09-01 20:05:19 -04001550 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001551
1552#ifdef ORINOCO_DEBUG
1553 {
1554 int i;
1555 printk(KERN_DEBUG "Scan result [%02X", buf[0]);
David Kilroya94e8422009-02-04 23:05:44 +00001556 for (i = 1; i < (len * 2); i++)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001557 printk(":%02X", buf[i]);
1558 printk("]\n");
1559 }
1560#endif /* ORINOCO_DEBUG */
1561
David Kilroyc63cdbe2009-06-18 23:21:33 +01001562 qbuf_scan(priv, buf, len, type);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001563 }
1564 break;
David Kilroy01632fa2008-08-21 23:27:58 +01001565 case HERMES_INQ_CHANNELINFO:
1566 {
1567 struct agere_ext_scan_info *bss;
1568
David Kilroyc63cdbe2009-06-18 23:21:33 +01001569 if (!priv->scan_request) {
David Kilroy01632fa2008-08-21 23:27:58 +01001570 printk(KERN_DEBUG "%s: Got chaninfo without scan, "
1571 "len=%d\n", dev->name, len);
1572 break;
1573 }
1574
1575 /* An empty result indicates that the scan is complete */
1576 if (len == 0) {
David Kilroyc63cdbe2009-06-18 23:21:33 +01001577 qbuf_scan(priv, NULL, len, type);
David Kilroy01632fa2008-08-21 23:27:58 +01001578 break;
1579 }
1580
1581 /* Sanity check */
David Kilroyc63cdbe2009-06-18 23:21:33 +01001582 else if (len < (offsetof(struct agere_ext_scan_info,
David Kilroy01632fa2008-08-21 23:27:58 +01001583 data) + 2)) {
1584 /* Drop this result now so we don't have to
1585 * keep checking later */
1586 printk(KERN_WARNING
1587 "%s: Ext scan results too short (%d bytes)\n",
1588 dev->name, len);
1589 break;
1590 }
1591
David Kilroyc63cdbe2009-06-18 23:21:33 +01001592 bss = kmalloc(len, GFP_ATOMIC);
David Kilroy01632fa2008-08-21 23:27:58 +01001593 if (bss == NULL)
1594 break;
1595
1596 /* Read scan data */
David Kilroyb42f2072010-05-01 14:05:38 +01001597 err = hw->ops->bap_pread(hw, IRQ_BAP, (void *) bss, len,
1598 infofid, sizeof(info));
David Kilroyc63cdbe2009-06-18 23:21:33 +01001599 if (err)
David Kilroy01632fa2008-08-21 23:27:58 +01001600 kfree(bss);
David Kilroyc63cdbe2009-06-18 23:21:33 +01001601 else
1602 qbuf_scan(priv, bss, len, type);
David Kilroy01632fa2008-08-21 23:27:58 +01001603
David Kilroy01632fa2008-08-21 23:27:58 +01001604 break;
1605 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001606 case HERMES_INQ_SEC_STAT_AGERE:
1607 /* Security status (Agere specific) */
1608 /* Ignore this frame for now */
1609 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
1610 break;
1611 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 default:
1613 printk(KERN_DEBUG "%s: Unknown information frame received: "
1614 "type 0x%04x, length %d\n", dev->name, type, len);
1615 /* We don't actually do anything about it */
1616 break;
1617 }
1618}
David Kilroy9afac702010-05-01 14:05:41 +01001619EXPORT_SYMBOL(__orinoco_ev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Pavel Roskin933d5942011-07-13 11:19:57 -04001621static void __orinoco_ev_infdrop(struct net_device *dev, struct hermes *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
1623 if (net_ratelimit())
1624 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
1625}
1626
1627/********************************************************************/
1628/* Internal hardware control routines */
1629/********************************************************************/
1630
David Kilroy6415f7d2009-06-18 23:21:30 +01001631static int __orinoco_up(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
David Kilroya2608362009-06-18 23:21:23 +01001633 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 struct hermes *hw = &priv->hw;
1635 int err;
1636
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001637 netif_carrier_off(dev); /* just to make sure */
1638
David Kilroy721aa2f2009-06-18 23:21:31 +01001639 err = __orinoco_commit(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (err) {
1641 printk(KERN_ERR "%s: Error %d configuring card\n",
1642 dev->name, err);
1643 return err;
1644 }
1645
1646 /* Fire things up again */
1647 hermes_set_irqmask(hw, ORINOCO_INTEN);
1648 err = hermes_enable_port(hw, 0);
1649 if (err) {
1650 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
1651 dev->name, err);
1652 return err;
1653 }
1654
1655 netif_start_queue(dev);
1656
1657 return 0;
1658}
1659
David Kilroy6415f7d2009-06-18 23:21:30 +01001660static int __orinoco_down(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
David Kilroya2608362009-06-18 23:21:23 +01001662 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 struct hermes *hw = &priv->hw;
1664 int err;
1665
1666 netif_stop_queue(dev);
1667
David Kilroya94e8422009-02-04 23:05:44 +00001668 if (!priv->hw_unavailable) {
1669 if (!priv->broken_disableport) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 err = hermes_disable_port(hw, 0);
1671 if (err) {
1672 /* Some firmwares (e.g. Intersil 1.3.x) seem
1673 * to have problems disabling the port, oh
1674 * well, too bad. */
1675 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
1676 dev->name, err);
1677 priv->broken_disableport = 1;
1678 }
1679 }
1680 hermes_set_irqmask(hw, 0);
1681 hermes_write_regn(hw, EVACK, 0xffff);
1682 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001683
David Kilroycf634952010-11-24 20:33:02 +00001684 orinoco_scan_done(priv, true);
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 /* firmware will have to reassociate */
1687 netif_carrier_off(dev);
1688 priv->last_linkstatus = 0xffff;
1689
1690 return 0;
1691}
1692
David Kilroy6415f7d2009-06-18 23:21:30 +01001693static int orinoco_reinit_firmware(struct orinoco_private *priv)
Pavel Roskin37a6c612006-04-07 04:10:49 -04001694{
Pavel Roskin37a6c612006-04-07 04:10:49 -04001695 struct hermes *hw = &priv->hw;
1696 int err;
1697
David Kilroyb42f2072010-05-01 14:05:38 +01001698 err = hw->ops->init(hw);
Andrey Borzenkov0df6cbb2008-10-12 20:15:43 +04001699 if (priv->do_fw_download && !err) {
1700 err = orinoco_download(priv);
1701 if (err)
1702 priv->do_fw_download = 0;
1703 }
Pavel Roskin37a6c612006-04-07 04:10:49 -04001704 if (!err)
David Kilroy42a51b92009-06-18 23:21:20 +01001705 err = orinoco_hw_allocate_fid(priv);
Pavel Roskin37a6c612006-04-07 04:10:49 -04001706
1707 return err;
1708}
1709
David Kilroy721aa2f2009-06-18 23:21:31 +01001710static int
David Kilroy5865d012009-02-04 23:05:54 +00001711__orinoco_set_multicast_list(struct net_device *dev)
1712{
David Kilroyea60a6a2009-06-18 23:21:26 +01001713 struct orinoco_private *priv = ndev_priv(dev);
David Kilroy5865d012009-02-04 23:05:54 +00001714 int err = 0;
1715 int promisc, mc_count;
1716
1717 /* The Hermes doesn't seem to have an allmulti mode, so we go
1718 * into promiscuous mode and let the upper levels deal. */
1719 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001720 (netdev_mc_count(dev) > MAX_MULTICAST(priv))) {
David Kilroy5865d012009-02-04 23:05:54 +00001721 promisc = 1;
1722 mc_count = 0;
1723 } else {
1724 promisc = 0;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001725 mc_count = netdev_mc_count(dev);
David Kilroy5865d012009-02-04 23:05:54 +00001726 }
1727
Jiri Pirko655ffee2010-02-27 07:35:45 +00001728 err = __orinoco_hw_set_multicast_list(priv, dev, mc_count, promisc);
David Kilroy721aa2f2009-06-18 23:21:31 +01001729
1730 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733/* This must be called from user context, without locks held - use
1734 * schedule_work() */
David Kilroycb1576a2009-02-04 23:05:56 +00001735void orinoco_reset(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
David Howellsc4028952006-11-22 14:57:56 +00001737 struct orinoco_private *priv =
1738 container_of(work, struct orinoco_private, reset_work);
1739 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 struct hermes *hw = &priv->hw;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001741 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 unsigned long flags;
1743
1744 if (orinoco_lock(priv, &flags) != 0)
1745 /* When the hardware becomes available again, whatever
1746 * detects that is responsible for re-initializing
1747 * it. So no need for anything further */
1748 return;
1749
1750 netif_stop_queue(dev);
1751
1752 /* Shut off interrupts. Depending on what state the hardware
1753 * is in, this might not work, but we'll try anyway */
1754 hermes_set_irqmask(hw, 0);
1755 hermes_write_regn(hw, EVACK, 0xffff);
1756
1757 priv->hw_unavailable++;
1758 priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
1759 netif_carrier_off(dev);
1760
1761 orinoco_unlock(priv, &flags);
1762
David Kilroyc63cdbe2009-06-18 23:21:33 +01001763 /* Scanning support: Notify scan cancellation */
David Kilroycf634952010-11-24 20:33:02 +00001764 orinoco_scan_done(priv, true);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001765
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001766 if (priv->hard_reset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 err = (*priv->hard_reset)(priv);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001768 if (err) {
1769 printk(KERN_ERR "%s: orinoco_reset: Error %d "
1770 "performing hard reset\n", dev->name, err);
1771 goto disable;
1772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774
David Kilroya2608362009-06-18 23:21:23 +01001775 err = orinoco_reinit_firmware(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (err) {
1777 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
1778 dev->name, err);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001779 goto disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781
David Kilroyb2f30a02009-02-04 23:05:46 +00001782 /* This has to be called from user context */
David Kilroybcad6e82010-05-01 14:05:40 +01001783 orinoco_lock_irq(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785 priv->hw_unavailable--;
1786
1787 /* priv->open or priv->hw_unavailable might have changed while
1788 * we dropped the lock */
David Kilroya94e8422009-02-04 23:05:44 +00001789 if (priv->open && (!priv->hw_unavailable)) {
David Kilroya2608362009-06-18 23:21:23 +01001790 err = __orinoco_up(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 if (err) {
1792 printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
1793 dev->name, err);
1794 } else
1795 dev->trans_start = jiffies;
1796 }
1797
David Kilroybcad6e82010-05-01 14:05:40 +01001798 orinoco_unlock_irq(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 return;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001801 disable:
1802 hermes_set_irqmask(hw, 0);
1803 netif_device_detach(dev);
1804 printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
David Kilroy721aa2f2009-06-18 23:21:31 +01001807static int __orinoco_commit(struct orinoco_private *priv)
1808{
1809 struct net_device *dev = priv->ndev;
1810 int err = 0;
1811
David Kilroyba34fce2010-12-05 15:45:58 +00001812 /* If we've called commit, we are reconfiguring or bringing the
1813 * interface up. Maintaining countermeasures across this would
1814 * be confusing, so note that we've disabled them. The port will
1815 * be enabled later in orinoco_commit or __orinoco_up. */
1816 priv->tkip_cm_active = 0;
1817
David Kilroy721aa2f2009-06-18 23:21:31 +01001818 err = orinoco_hw_program_rids(priv);
1819
1820 /* FIXME: what about netif_tx_lock */
1821 (void) __orinoco_set_multicast_list(dev);
1822
1823 return err;
1824}
1825
1826/* Ensures configuration changes are applied. May result in a reset.
1827 * The caller should hold priv->lock
1828 */
1829int orinoco_commit(struct orinoco_private *priv)
1830{
1831 struct net_device *dev = priv->ndev;
Pavel Roskin933d5942011-07-13 11:19:57 -04001832 struct hermes *hw = &priv->hw;
David Kilroy721aa2f2009-06-18 23:21:31 +01001833 int err;
1834
1835 if (priv->broken_disableport) {
1836 schedule_work(&priv->reset_work);
1837 return 0;
1838 }
1839
1840 err = hermes_disable_port(hw, 0);
1841 if (err) {
1842 printk(KERN_WARNING "%s: Unable to disable port "
1843 "while reconfiguring card\n", dev->name);
1844 priv->broken_disableport = 1;
1845 goto out;
1846 }
1847
1848 err = __orinoco_commit(priv);
1849 if (err) {
1850 printk(KERN_WARNING "%s: Unable to reconfigure card\n",
1851 dev->name);
1852 goto out;
1853 }
1854
1855 err = hermes_enable_port(hw, 0);
1856 if (err) {
1857 printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
1858 dev->name);
1859 goto out;
1860 }
1861
1862 out:
1863 if (err) {
1864 printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
1865 schedule_work(&priv->reset_work);
1866 err = 0;
1867 }
1868 return err;
1869}
1870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871/********************************************************************/
1872/* Interrupt handler */
1873/********************************************************************/
1874
Pavel Roskin933d5942011-07-13 11:19:57 -04001875static void __orinoco_ev_tick(struct net_device *dev, struct hermes *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
1877 printk(KERN_DEBUG "%s: TICK\n", dev->name);
1878}
1879
Pavel Roskin933d5942011-07-13 11:19:57 -04001880static void __orinoco_ev_wterr(struct net_device *dev, struct hermes *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
1882 /* This seems to happen a fair bit under load, but ignoring it
1883 seems to work fine...*/
1884 printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
1885 dev->name);
1886}
1887
David Howells7d12e782006-10-05 14:55:46 +01001888irqreturn_t orinoco_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
David Kilroya2608362009-06-18 23:21:23 +01001890 struct orinoco_private *priv = dev_id;
1891 struct net_device *dev = priv->ndev;
Pavel Roskin933d5942011-07-13 11:19:57 -04001892 struct hermes *hw = &priv->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 int count = MAX_IRQLOOPS_PER_IRQ;
1894 u16 evstat, events;
David Kilroy21312662009-02-04 23:05:47 +00001895 /* These are used to detect a runaway interrupt situation.
1896 *
1897 * If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
1898 * we panic and shut down the hardware
1899 */
1900 /* jiffies value the last time we were called */
1901 static int last_irq_jiffy; /* = 0 */
1902 static int loops_this_jiffy; /* = 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 unsigned long flags;
1904
1905 if (orinoco_lock(priv, &flags) != 0) {
1906 /* If hw is unavailable - we don't know if the irq was
1907 * for us or not */
1908 return IRQ_HANDLED;
1909 }
1910
1911 evstat = hermes_read_regn(hw, EVSTAT);
1912 events = evstat & hw->inten;
David Kilroya94e8422009-02-04 23:05:44 +00001913 if (!events) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 orinoco_unlock(priv, &flags);
1915 return IRQ_NONE;
1916 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 if (jiffies != last_irq_jiffy)
1919 loops_this_jiffy = 0;
1920 last_irq_jiffy = jiffies;
1921
1922 while (events && count--) {
1923 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
1924 printk(KERN_WARNING "%s: IRQ handler is looping too "
1925 "much! Resetting.\n", dev->name);
1926 /* Disable interrupts for now */
1927 hermes_set_irqmask(hw, 0);
1928 schedule_work(&priv->reset_work);
1929 break;
1930 }
1931
1932 /* Check the card hasn't been removed */
David Kilroya94e8422009-02-04 23:05:44 +00001933 if (!hermes_present(hw)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 DEBUG(0, "orinoco_interrupt(): card removed\n");
1935 break;
1936 }
1937
1938 if (events & HERMES_EV_TICK)
1939 __orinoco_ev_tick(dev, hw);
1940 if (events & HERMES_EV_WTERR)
1941 __orinoco_ev_wterr(dev, hw);
1942 if (events & HERMES_EV_INFDROP)
1943 __orinoco_ev_infdrop(dev, hw);
1944 if (events & HERMES_EV_INFO)
1945 __orinoco_ev_info(dev, hw);
1946 if (events & HERMES_EV_RX)
1947 __orinoco_ev_rx(dev, hw);
1948 if (events & HERMES_EV_TXEXC)
1949 __orinoco_ev_txexc(dev, hw);
1950 if (events & HERMES_EV_TX)
1951 __orinoco_ev_tx(dev, hw);
1952 if (events & HERMES_EV_ALLOC)
1953 __orinoco_ev_alloc(dev, hw);
David Kilroy6fe9deb2009-02-04 23:05:43 +00001954
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001955 hermes_write_regn(hw, EVACK, evstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957 evstat = hermes_read_regn(hw, EVSTAT);
1958 events = evstat & hw->inten;
Joe Perches6403eab2011-06-03 11:51:20 +00001959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 orinoco_unlock(priv, &flags);
1962 return IRQ_HANDLED;
1963}
David Kilroy21312662009-02-04 23:05:47 +00001964EXPORT_SYMBOL(orinoco_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966/********************************************************************/
David Kilroy39d1ffe2008-11-22 10:37:28 +00001967/* Power management */
1968/********************************************************************/
1969#if defined(CONFIG_PM_SLEEP) && !defined(CONFIG_HERMES_CACHE_FW_ON_INIT)
1970static int orinoco_pm_notifier(struct notifier_block *notifier,
1971 unsigned long pm_event,
1972 void *unused)
1973{
1974 struct orinoco_private *priv = container_of(notifier,
1975 struct orinoco_private,
1976 pm_notifier);
1977
1978 /* All we need to do is cache the firmware before suspend, and
1979 * release it when we come out.
1980 *
1981 * Only need to do this if we're downloading firmware. */
1982 if (!priv->do_fw_download)
1983 return NOTIFY_DONE;
1984
1985 switch (pm_event) {
1986 case PM_HIBERNATION_PREPARE:
1987 case PM_SUSPEND_PREPARE:
1988 orinoco_cache_fw(priv, 0);
1989 break;
1990
1991 case PM_POST_RESTORE:
1992 /* Restore from hibernation failed. We need to clean
1993 * up in exactly the same way, so fall through. */
1994 case PM_POST_HIBERNATION:
1995 case PM_POST_SUSPEND:
1996 orinoco_uncache_fw(priv);
1997 break;
1998
1999 case PM_RESTORE_PREPARE:
2000 default:
2001 break;
2002 }
2003
2004 return NOTIFY_DONE;
2005}
David S. Millerf11c1792009-02-25 00:02:05 -08002006
2007static void orinoco_register_pm_notifier(struct orinoco_private *priv)
2008{
2009 priv->pm_notifier.notifier_call = orinoco_pm_notifier;
2010 register_pm_notifier(&priv->pm_notifier);
2011}
2012
2013static void orinoco_unregister_pm_notifier(struct orinoco_private *priv)
2014{
2015 unregister_pm_notifier(&priv->pm_notifier);
2016}
David Kilroy39d1ffe2008-11-22 10:37:28 +00002017#else /* !PM_SLEEP || HERMES_CACHE_FW_ON_INIT */
Pavel Roskin933d5942011-07-13 11:19:57 -04002018#define orinoco_register_pm_notifier(priv) do { } while (0)
2019#define orinoco_unregister_pm_notifier(priv) do { } while (0)
David Kilroy39d1ffe2008-11-22 10:37:28 +00002020#endif
2021
2022/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023/* Initialization */
2024/********************************************************************/
2025
David Kilroy8e638262009-06-18 23:21:24 +01002026int orinoco_init(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
David Kilroy8e638262009-06-18 23:21:24 +01002028 struct device *dev = priv->dev;
David Kilroyea60a6a2009-06-18 23:21:26 +01002029 struct wiphy *wiphy = priv_to_wiphy(priv);
Pavel Roskin933d5942011-07-13 11:19:57 -04002030 struct hermes *hw = &priv->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 /* No need to lock, the hw_unavailable flag is already set in
2034 * alloc_orinocodev() */
Johannes Berg2c7060022008-10-30 22:09:54 +01002035 priv->nicbuf_size = IEEE80211_MAX_FRAME_LEN + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 /* Initialize the firmware */
David Kilroyb42f2072010-05-01 14:05:38 +01002038 err = hw->ops->init(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 if (err != 0) {
David Kilroy8e638262009-06-18 23:21:24 +01002040 dev_err(dev, "Failed to initialize firmware (err = %d)\n",
2041 err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 goto out;
2043 }
2044
David Kilroy3414fc32009-10-07 22:23:32 +01002045 err = determine_fw_capabilities(priv, wiphy->fw_version,
2046 sizeof(wiphy->fw_version),
2047 &wiphy->hw_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if (err != 0) {
David Kilroy8e638262009-06-18 23:21:24 +01002049 dev_err(dev, "Incompatible firmware, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 goto out;
2051 }
2052
David Kilroy3994d502008-08-21 23:27:54 +01002053 if (priv->do_fw_download) {
David Kilroy39d1ffe2008-11-22 10:37:28 +00002054#ifdef CONFIG_HERMES_CACHE_FW_ON_INIT
David Kilroy74734312008-11-22 10:37:25 +00002055 orinoco_cache_fw(priv, 0);
David Kilroy39d1ffe2008-11-22 10:37:28 +00002056#endif
David Kilroy74734312008-11-22 10:37:25 +00002057
David Kilroy3994d502008-08-21 23:27:54 +01002058 err = orinoco_download(priv);
2059 if (err)
2060 priv->do_fw_download = 0;
2061
2062 /* Check firmware version again */
David Kilroy3414fc32009-10-07 22:23:32 +01002063 err = determine_fw_capabilities(priv, wiphy->fw_version,
2064 sizeof(wiphy->fw_version),
2065 &wiphy->hw_version);
David Kilroy3994d502008-08-21 23:27:54 +01002066 if (err != 0) {
David Kilroy8e638262009-06-18 23:21:24 +01002067 dev_err(dev, "Incompatible firmware, aborting\n");
David Kilroy3994d502008-08-21 23:27:54 +01002068 goto out;
2069 }
2070 }
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 if (priv->has_port3)
David Kilroy8e638262009-06-18 23:21:24 +01002073 dev_info(dev, "Ad-hoc demo mode supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if (priv->has_ibss)
David Kilroy8e638262009-06-18 23:21:24 +01002075 dev_info(dev, "IEEE standard IBSS ad-hoc mode supported\n");
2076 if (priv->has_wep)
2077 dev_info(dev, "WEP supported, %s-bit key\n",
2078 priv->has_big_wep ? "104" : "40");
David Kilroy23edcc42008-08-21 23:28:05 +01002079 if (priv->has_wpa) {
David Kilroy8e638262009-06-18 23:21:24 +01002080 dev_info(dev, "WPA-PSK supported\n");
David Kilroy23edcc42008-08-21 23:28:05 +01002081 if (orinoco_mic_init(priv)) {
David Kilroy8e638262009-06-18 23:21:24 +01002082 dev_err(dev, "Failed to setup MIC crypto algorithm. "
2083 "Disabling WPA support\n");
David Kilroy23edcc42008-08-21 23:28:05 +01002084 priv->has_wpa = 0;
2085 }
2086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
David Kilroyea60a6a2009-06-18 23:21:26 +01002088 err = orinoco_hw_read_card_settings(priv, wiphy->perm_addr);
David Kilroye9e3d012009-06-18 23:21:19 +01002089 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
David Kilroy42a51b92009-06-18 23:21:20 +01002092 err = orinoco_hw_allocate_fid(priv);
Pavel Roskin37a6c612006-04-07 04:10:49 -04002093 if (err) {
David Kilroy8e638262009-06-18 23:21:24 +01002094 dev_err(dev, "Failed to allocate NIC buffer!\n");
Pavel Roskin37a6c612006-04-07 04:10:49 -04002095 goto out;
2096 }
2097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 /* Set up the default configuration */
David Kilroy5217c572009-06-18 23:21:32 +01002099 priv->iw_mode = NL80211_IFTYPE_STATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 /* By default use IEEE/IBSS ad-hoc mode if we have it */
David Kilroya94e8422009-02-04 23:05:44 +00002101 priv->prefer_port3 = priv->has_port3 && (!priv->has_ibss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 set_port_type(priv);
David Gibsond51d8b12005-05-12 20:03:36 -04002103 priv->channel = 0; /* use firmware default */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 priv->promiscuous = 0;
David Kilroy5c9f41e2009-08-05 21:23:28 +01002106 priv->encode_alg = ORINOCO_ALG_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 priv->tx_key = 0;
David Kilroyd03032a2008-08-21 23:28:02 +01002108 priv->wpa_enabled = 0;
2109 priv->tkip_cm_active = 0;
2110 priv->key_mgmt = 0;
2111 priv->wpa_ie_len = 0;
2112 priv->wpa_ie = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
David Kilroyea60a6a2009-06-18 23:21:26 +01002114 if (orinoco_wiphy_register(wiphy)) {
2115 err = -ENODEV;
2116 goto out;
2117 }
2118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 /* Make the hardware available, as long as it hasn't been
2120 * removed elsewhere (e.g. by PCMCIA hot unplug) */
David Kilroybcad6e82010-05-01 14:05:40 +01002121 orinoco_lock_irq(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 priv->hw_unavailable--;
David Kilroybcad6e82010-05-01 14:05:40 +01002123 orinoco_unlock_irq(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
David Kilroy8e638262009-06-18 23:21:24 +01002125 dev_dbg(dev, "Ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 return err;
2129}
David Kilroy8e638262009-06-18 23:21:24 +01002130EXPORT_SYMBOL(orinoco_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Andrey Borzenkov89ea4092009-01-21 20:46:46 +03002132static const struct net_device_ops orinoco_netdev_ops = {
Andrey Borzenkov89ea4092009-01-21 20:46:46 +03002133 .ndo_open = orinoco_open,
2134 .ndo_stop = orinoco_stop,
2135 .ndo_start_xmit = orinoco_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002136 .ndo_set_rx_mode = orinoco_set_multicast_list,
Andrey Borzenkov89ea4092009-01-21 20:46:46 +03002137 .ndo_change_mtu = orinoco_change_mtu,
Ben Hutchings240c1022009-07-09 17:54:35 +00002138 .ndo_set_mac_address = eth_mac_addr,
2139 .ndo_validate_addr = eth_validate_addr,
Andrey Borzenkov89ea4092009-01-21 20:46:46 +03002140 .ndo_tx_timeout = orinoco_tx_timeout,
2141 .ndo_get_stats = orinoco_get_stats,
2142};
2143
David Kilroyea60a6a2009-06-18 23:21:26 +01002144/* Allocate private data.
2145 *
2146 * This driver has a number of structures associated with it
2147 * netdev - Net device structure for each network interface
2148 * wiphy - structure associated with wireless phy
2149 * wireless_dev (wdev) - structure for each wireless interface
2150 * hw - structure for hermes chip info
2151 * card - card specific structure for use by the card driver
2152 * (airport, orinoco_cs)
2153 * priv - orinoco private data
2154 * device - generic linux device structure
2155 *
2156 * +---------+ +---------+
2157 * | wiphy | | netdev |
2158 * | +-------+ | +-------+
2159 * | | priv | | | wdev |
2160 * | | +-----+ +-+-------+
2161 * | | | hw |
2162 * | +-+-----+
2163 * | | card |
2164 * +-+-------+
2165 *
2166 * priv has a link to netdev and device
2167 * wdev has a link to wiphy
2168 */
David Kilroya2608362009-06-18 23:21:23 +01002169struct orinoco_private
David Kilroy3994d502008-08-21 23:27:54 +01002170*alloc_orinocodev(int sizeof_card,
2171 struct device *device,
2172 int (*hard_reset)(struct orinoco_private *),
2173 int (*stop_fw)(struct orinoco_private *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 struct orinoco_private *priv;
David Kilroyea60a6a2009-06-18 23:21:26 +01002176 struct wiphy *wiphy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
David Kilroyea60a6a2009-06-18 23:21:26 +01002178 /* allocate wiphy
2179 * NOTE: We only support a single virtual interface
2180 * but this may change when monitor mode is added
2181 */
2182 wiphy = wiphy_new(&orinoco_cfg_ops,
2183 sizeof(struct orinoco_private) + sizeof_card);
2184 if (!wiphy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 return NULL;
David Kilroyea60a6a2009-06-18 23:21:26 +01002186
David Kilroyea60a6a2009-06-18 23:21:26 +01002187 priv = wiphy_priv(wiphy);
David Kilroy53819562009-06-18 23:21:28 +01002188 priv->dev = device;
David Kilroyea60a6a2009-06-18 23:21:26 +01002189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 if (sizeof_card)
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002191 priv->card = (void *)((unsigned long)priv
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 + sizeof(struct orinoco_private));
2193 else
2194 priv->card = NULL;
2195
David Kilroyea60a6a2009-06-18 23:21:26 +01002196 orinoco_wiphy_init(wiphy);
2197
Pavel Roskin343c6862005-09-09 18:43:02 -04002198#ifdef WIRELESS_SPY
2199 priv->wireless_data.spy_data = &priv->spy_data;
Pavel Roskin343c6862005-09-09 18:43:02 -04002200#endif
David Kilroy23edcc42008-08-21 23:28:05 +01002201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 /* Set up default callbacks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 priv->hard_reset = hard_reset;
David Kilroy3994d502008-08-21 23:27:54 +01002204 priv->stop_fw = stop_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
2206 spin_lock_init(&priv->lock);
2207 priv->open = 0;
2208 priv->hw_unavailable = 1; /* orinoco_init() must clear this
2209 * before anything else touches the
2210 * hardware */
David Howellsc4028952006-11-22 14:57:56 +00002211 INIT_WORK(&priv->reset_work, orinoco_reset);
2212 INIT_WORK(&priv->join_work, orinoco_join_ap);
2213 INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
David Kilroy31afcef2008-08-21 23:28:04 +01002215 INIT_LIST_HEAD(&priv->rx_list);
2216 tasklet_init(&priv->rx_tasklet, orinoco_rx_isr_tasklet,
David Kilroy53819562009-06-18 23:21:28 +01002217 (unsigned long) priv);
David Kilroy31afcef2008-08-21 23:28:04 +01002218
David Kilroyc63cdbe2009-06-18 23:21:33 +01002219 spin_lock_init(&priv->scan_lock);
2220 INIT_LIST_HEAD(&priv->scan_list);
2221 INIT_WORK(&priv->process_scan, orinoco_process_scan_results);
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 priv->last_linkstatus = 0xffff;
2224
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +03002225#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
David Kilroy2cea7b22008-11-22 10:37:26 +00002226 priv->cached_pri_fw = NULL;
Andrey Borzenkov4fb30782008-10-19 12:06:11 +04002227 priv->cached_fw = NULL;
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +03002228#endif
Andrey Borzenkov4fb30782008-10-19 12:06:11 +04002229
David Kilroy39d1ffe2008-11-22 10:37:28 +00002230 /* Register PM notifiers */
David S. Millerf11c1792009-02-25 00:02:05 -08002231 orinoco_register_pm_notifier(priv);
David Kilroy39d1ffe2008-11-22 10:37:28 +00002232
David Kilroya2608362009-06-18 23:21:23 +01002233 return priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234}
David Kilroy21312662009-02-04 23:05:47 +00002235EXPORT_SYMBOL(alloc_orinocodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
David Kilroy53819562009-06-18 23:21:28 +01002237/* We can only support a single interface. We provide a separate
2238 * function to set it up to distinguish between hardware
2239 * initialisation and interface setup.
2240 *
2241 * The base_addr and irq parameters are passed on to netdev for use
2242 * with SIOCGIFMAP.
2243 */
2244int orinoco_if_add(struct orinoco_private *priv,
2245 unsigned long base_addr,
David Kilroy593ef092010-05-01 14:05:39 +01002246 unsigned int irq,
2247 const struct net_device_ops *ops)
David Kilroy53819562009-06-18 23:21:28 +01002248{
2249 struct wiphy *wiphy = priv_to_wiphy(priv);
2250 struct wireless_dev *wdev;
2251 struct net_device *dev;
2252 int ret;
2253
2254 dev = alloc_etherdev(sizeof(struct wireless_dev));
2255
2256 if (!dev)
2257 return -ENOMEM;
2258
2259 /* Initialise wireless_dev */
2260 wdev = netdev_priv(dev);
2261 wdev->wiphy = wiphy;
2262 wdev->iftype = NL80211_IFTYPE_STATION;
2263
2264 /* Setup / override net_device fields */
2265 dev->ieee80211_ptr = wdev;
David Kilroy53819562009-06-18 23:21:28 +01002266 dev->watchdog_timeo = HZ; /* 1 second timeout */
David Kilroy53819562009-06-18 23:21:28 +01002267 dev->wireless_handlers = &orinoco_handler_def;
2268#ifdef WIRELESS_SPY
2269 dev->wireless_data = &priv->wireless_data;
2270#endif
David Kilroy593ef092010-05-01 14:05:39 +01002271 /* Default to standard ops if not set */
2272 if (ops)
2273 dev->netdev_ops = ops;
2274 else
2275 dev->netdev_ops = &orinoco_netdev_ops;
2276
David Kilroy53819562009-06-18 23:21:28 +01002277 /* we use the default eth_mac_addr for setting the MAC addr */
2278
2279 /* Reserve space in skb for the SNAP header */
David Kilroybac6faf2010-05-04 22:54:41 +01002280 dev->needed_headroom = ENCAPS_OVERHEAD;
David Kilroy53819562009-06-18 23:21:28 +01002281
2282 netif_carrier_off(dev);
2283
2284 memcpy(dev->dev_addr, wiphy->perm_addr, ETH_ALEN);
John W. Linvillecf32ed92009-10-06 16:47:23 -04002285 memcpy(dev->perm_addr, wiphy->perm_addr, ETH_ALEN);
David Kilroy53819562009-06-18 23:21:28 +01002286
2287 dev->base_addr = base_addr;
2288 dev->irq = irq;
2289
2290 SET_NETDEV_DEV(dev, priv->dev);
2291 ret = register_netdev(dev);
2292 if (ret)
2293 goto fail;
2294
2295 priv->ndev = dev;
2296
2297 /* Report what we've done */
2298 dev_dbg(priv->dev, "Registerred interface %s.\n", dev->name);
2299
2300 return 0;
2301
2302 fail:
2303 free_netdev(dev);
2304 return ret;
2305}
2306EXPORT_SYMBOL(orinoco_if_add);
2307
2308void orinoco_if_del(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
David Kilroya2608362009-06-18 23:21:23 +01002310 struct net_device *dev = priv->ndev;
David Kilroy53819562009-06-18 23:21:28 +01002311
2312 unregister_netdev(dev);
2313 free_netdev(dev);
2314}
2315EXPORT_SYMBOL(orinoco_if_del);
2316
2317void free_orinocodev(struct orinoco_private *priv)
2318{
David Kilroyea60a6a2009-06-18 23:21:26 +01002319 struct wiphy *wiphy = priv_to_wiphy(priv);
David Kilroy20953ad2009-01-07 00:23:55 +00002320 struct orinoco_rx_data *rx_data, *temp;
David Kilroyc63cdbe2009-06-18 23:21:33 +01002321 struct orinoco_scan_data *sd, *sdtemp;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002322
David Kilroyea60a6a2009-06-18 23:21:26 +01002323 wiphy_unregister(wiphy);
2324
David Kilroy20953ad2009-01-07 00:23:55 +00002325 /* If the tasklet is scheduled when we call tasklet_kill it
2326 * will run one final time. However the tasklet will only
2327 * drain priv->rx_list if the hw is still available. */
David Kilroy31afcef2008-08-21 23:28:04 +01002328 tasklet_kill(&priv->rx_tasklet);
David Kilroy74734312008-11-22 10:37:25 +00002329
David Kilroy20953ad2009-01-07 00:23:55 +00002330 /* Explicitly drain priv->rx_list */
2331 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
2332 list_del(&rx_data->list);
2333
2334 dev_kfree_skb(rx_data->skb);
2335 kfree(rx_data->desc);
2336 kfree(rx_data);
2337 }
2338
David Kilroyc63cdbe2009-06-18 23:21:33 +01002339 cancel_work_sync(&priv->process_scan);
2340 /* Explicitly drain priv->scan_list */
2341 list_for_each_entry_safe(sd, sdtemp, &priv->scan_list, list) {
2342 list_del(&sd->list);
2343
2344 if ((sd->len > 0) && sd->buf)
2345 kfree(sd->buf);
2346 kfree(sd);
2347 }
2348
David S. Millerf11c1792009-02-25 00:02:05 -08002349 orinoco_unregister_pm_notifier(priv);
David Kilroy74734312008-11-22 10:37:25 +00002350 orinoco_uncache_fw(priv);
2351
David Kilroyd03032a2008-08-21 23:28:02 +01002352 priv->wpa_ie_len = 0;
2353 kfree(priv->wpa_ie);
David Kilroy23edcc42008-08-21 23:28:05 +01002354 orinoco_mic_free(priv);
David Kilroyea60a6a2009-06-18 23:21:26 +01002355 wiphy_free(wiphy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356}
David Kilroy21312662009-02-04 23:05:47 +00002357EXPORT_SYMBOL(free_orinocodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
David Kilroy6415f7d2009-06-18 23:21:30 +01002359int orinoco_up(struct orinoco_private *priv)
2360{
2361 struct net_device *dev = priv->ndev;
2362 unsigned long flags;
2363 int err;
2364
David Kilroybcad6e82010-05-01 14:05:40 +01002365 priv->hw.ops->lock_irqsave(&priv->lock, &flags);
David Kilroy6415f7d2009-06-18 23:21:30 +01002366
2367 err = orinoco_reinit_firmware(priv);
2368 if (err) {
2369 printk(KERN_ERR "%s: Error %d re-initializing firmware\n",
2370 dev->name, err);
2371 goto exit;
2372 }
2373
2374 netif_device_attach(dev);
2375 priv->hw_unavailable--;
2376
2377 if (priv->open && !priv->hw_unavailable) {
2378 err = __orinoco_up(priv);
2379 if (err)
2380 printk(KERN_ERR "%s: Error %d restarting card\n",
2381 dev->name, err);
2382 }
2383
2384exit:
David Kilroybcad6e82010-05-01 14:05:40 +01002385 priv->hw.ops->unlock_irqrestore(&priv->lock, &flags);
David Kilroy6415f7d2009-06-18 23:21:30 +01002386
2387 return 0;
2388}
2389EXPORT_SYMBOL(orinoco_up);
2390
2391void orinoco_down(struct orinoco_private *priv)
2392{
2393 struct net_device *dev = priv->ndev;
2394 unsigned long flags;
2395 int err;
2396
David Kilroybcad6e82010-05-01 14:05:40 +01002397 priv->hw.ops->lock_irqsave(&priv->lock, &flags);
David Kilroy6415f7d2009-06-18 23:21:30 +01002398 err = __orinoco_down(priv);
2399 if (err)
2400 printk(KERN_WARNING "%s: Error %d downing interface\n",
2401 dev->name, err);
2402
2403 netif_device_detach(dev);
2404 priv->hw_unavailable++;
David Kilroybcad6e82010-05-01 14:05:40 +01002405 priv->hw.ops->unlock_irqrestore(&priv->lock, &flags);
David Kilroy6415f7d2009-06-18 23:21:30 +01002406}
2407EXPORT_SYMBOL(orinoco_down);
2408
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410/* Module initialization */
2411/********************************************************************/
2412
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413/* Can't be declared "const" or the whole __initdata section will
2414 * become const */
2415static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
2416 " (David Gibson <hermes@gibson.dropbear.id.au>, "
2417 "Pavel Roskin <proski@gnu.org>, et al)";
2418
2419static int __init init_orinoco(void)
2420{
2421 printk(KERN_DEBUG "%s\n", version);
2422 return 0;
2423}
2424
2425static void __exit exit_orinoco(void)
2426{
2427}
2428
2429module_init(init_orinoco);
2430module_exit(exit_orinoco);