blob: 7acb6bc3ebffec31d4fba242d7a4ef2f5488014c [file] [log] [blame]
David Kilroy47445cb2009-02-04 23:05:48 +00001/* main.c - (formerly known as dldwd_cs.c, orinoco_cs.c and orinoco.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * A driver for Hermes or Prism 2 chipset based PCMCIA wireless
4 * adaptors, with Lucent/Agere, Intersil or Symbol firmware.
5 *
6 * Current maintainers (as of 29 September 2003) are:
7 * Pavel Roskin <proski AT gnu.org>
8 * and David Gibson <hermes AT gibson.dropbear.id.au>
9 *
10 * (C) Copyright David Gibson, IBM Corporation 2001-2003.
11 * Copyright (C) 2000 David Gibson, Linuxcare Australia.
12 * With some help from :
13 * Copyright (C) 2001 Jean Tourrilhes, HP Labs
14 * Copyright (C) 2001 Benjamin Herrenschmidt
15 *
16 * Based on dummy_cs.c 1.27 2000/06/12 21:27:25
17 *
18 * Portions based on wvlan_cs.c 1.0.6, Copyright Andreas Neuhaus <andy
19 * AT fasta.fh-dortmund.de>
20 * http://www.stud.fh-dortmund.de/~andy/wvlan/
21 *
22 * The contents of this file are subject to the Mozilla Public License
23 * Version 1.1 (the "License"); you may not use this file except in
24 * compliance with the License. You may obtain a copy of the License
25 * at http://www.mozilla.org/MPL/
26 *
27 * Software distributed under the License is distributed on an "AS IS"
28 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
29 * the License for the specific language governing rights and
30 * limitations under the License.
31 *
32 * The initial developer of the original code is David A. Hinds
33 * <dahinds AT users.sourceforge.net>. Portions created by David
34 * A. Hinds are Copyright (C) 1999 David A. Hinds. All Rights
35 * Reserved.
36 *
37 * Alternatively, the contents of this file may be used under the
38 * terms of the GNU General Public License version 2 (the "GPL"), in
39 * which case the provisions of the GPL are applicable instead of the
40 * above. If you wish to allow the use of your version of this file
41 * only under the terms of the GPL and not to allow others to use your
42 * version of this file under the MPL, indicate your decision by
43 * deleting the provisions above and replace them with the notice and
44 * other provisions required by the GPL. If you do not delete the
45 * provisions above, a recipient may use your version of this file
46 * under either the MPL or the GPL. */
47
48/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * TODO
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * o Handle de-encapsulation within network layer, provide 802.11
51 * headers (patch from Thomas 'Dent' Mirlacher)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * o Fix possible races in SPY handling.
53 * o Disconnect wireless extensions from fundamental configuration.
54 * o (maybe) Software WEP support (patch from Stano Meduna).
55 * o (maybe) Use multiple Tx buffers - driver handling queue
56 * rather than firmware.
57 */
58
59/* Locking and synchronization:
60 *
61 * The basic principle is that everything is serialized through a
62 * single spinlock, priv->lock. The lock is used in user, bh and irq
63 * context, so when taken outside hardirq context it should always be
64 * taken with interrupts disabled. The lock protects both the
65 * hardware and the struct orinoco_private.
66 *
67 * Another flag, priv->hw_unavailable indicates that the hardware is
68 * unavailable for an extended period of time (e.g. suspended, or in
69 * the middle of a hard reset). This flag is protected by the
70 * spinlock. All code which touches the hardware should check the
71 * flag after taking the lock, and if it is set, give up on whatever
72 * they are doing and drop the lock again. The orinoco_lock()
73 * function handles this (it unlocks and returns -EBUSY if
74 * hw_unavailable is non-zero).
75 */
76
77#define DRIVER_NAME "orinoco"
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/module.h>
80#include <linux/kernel.h>
81#include <linux/init.h>
David Kilroyd03032a2008-08-21 23:28:02 +010082#include <linux/delay.h>
David Kilroy8e638262009-06-18 23:21:24 +010083#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#include <linux/etherdevice.h>
David Kilroy39d1ffe2008-11-22 10:37:28 +000086#include <linux/suspend.h>
Pavel Roskin9c974fb2006-08-15 20:45:03 -040087#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include <linux/wireless.h>
Johannes Berg2c7060022008-10-30 22:09:54 +010089#include <linux/ieee80211.h>
Christoph Hellwig620554e2005-06-19 01:27:33 +020090#include <net/iw_handler.h>
David Kilroyea60a6a2009-06-18 23:21:26 +010091#include <net/cfg80211.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#include "hermes_rid.h"
David Kilroy3994d502008-08-21 23:27:54 +010094#include "hermes_dld.h"
David Kilroy712a4342009-02-04 23:05:55 +000095#include "hw.h"
David Kilroyfb791b12009-02-04 23:05:50 +000096#include "scan.h"
David Kilroy4adb4742009-02-04 23:05:51 +000097#include "mic.h"
David Kilroy37a2e562009-02-04 23:05:52 +000098#include "fw.h"
David Kilroycb1576a2009-02-04 23:05:56 +000099#include "wext.h"
David Kilroyea60a6a2009-06-18 23:21:26 +0100100#include "cfg.h"
David Kilroycb1576a2009-02-04 23:05:56 +0000101#include "main.h"
David Kilroyfb791b12009-02-04 23:05:50 +0000102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#include "orinoco.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/********************************************************************/
106/* Module information */
107/********************************************************************/
108
David Kilroyb2f30a02009-02-04 23:05:46 +0000109MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & "
110 "David Gibson <hermes@gibson.dropbear.id.au>");
111MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based "
112 "and similar wireless cards");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113MODULE_LICENSE("Dual MPL/GPL");
114
115/* Level of debugging. Used in the macros in orinoco.h */
116#ifdef ORINOCO_DEBUG
117int orinoco_debug = ORINOCO_DEBUG;
David Kilroy21312662009-02-04 23:05:47 +0000118EXPORT_SYMBOL(orinoco_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119module_param(orinoco_debug, int, 0644);
120MODULE_PARM_DESC(orinoco_debug, "Debug level");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#endif
122
123static int suppress_linkstatus; /* = 0 */
124module_param(suppress_linkstatus, bool, 0644);
125MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
David Kilroyb2f30a02009-02-04 23:05:46 +0000126
David Gibson7bb7c3a2005-05-12 20:02:10 -0400127static int ignore_disconnect; /* = 0 */
128module_param(ignore_disconnect, int, 0644);
David Kilroyb2f30a02009-02-04 23:05:46 +0000129MODULE_PARM_DESC(ignore_disconnect,
130 "Don't report lost link to the network layer");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
David Kilroycb1576a2009-02-04 23:05:56 +0000132int force_monitor; /* = 0 */
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200133module_param(force_monitor, int, 0644);
134MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/* Internal constants */
138/********************************************************************/
139
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200140/* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
141static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
142#define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#define ORINOCO_MIN_MTU 256
Johannes Berg2c7060022008-10-30 22:09:54 +0100145#define ORINOCO_MAX_MTU (IEEE80211_MAX_DATA_LEN - ENCAPS_OVERHEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#define MAX_IRQLOOPS_PER_IRQ 10
148#define MAX_IRQLOOPS_PER_JIFFY (20000/HZ) /* Based on a guestimate of
149 * how many events the
150 * device could
151 * legitimately generate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153#define DUMMY_FID 0xFFFF
154
155/*#define MAX_MULTICAST(priv) (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
156 HERMES_MAX_MULTICAST : 0)*/
157#define MAX_MULTICAST(priv) (HERMES_MAX_MULTICAST)
158
159#define ORINOCO_INTEN (HERMES_EV_RX | HERMES_EV_ALLOC \
160 | HERMES_EV_TX | HERMES_EV_TXEXC \
161 | HERMES_EV_WTERR | HERMES_EV_INFO \
David Kilroya94e8422009-02-04 23:05:44 +0000162 | HERMES_EV_INFDROP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/* Data types */
166/********************************************************************/
167
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400168/* Beginning of the Tx descriptor, used in TxExc handling */
169struct hermes_txexc_data {
170 struct hermes_tx_descriptor desc;
Pavel Roskind133ae42005-09-23 04:18:06 -0400171 __le16 frame_ctl;
172 __le16 duration_id;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200173 u8 addr1[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174} __attribute__ ((packed));
175
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200176/* Rx frame header except compatibility 802.3 header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177struct hermes_rx_descriptor {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200178 /* Control */
Pavel Roskind133ae42005-09-23 04:18:06 -0400179 __le16 status;
180 __le32 time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 u8 silence;
182 u8 signal;
183 u8 rate;
184 u8 rxflow;
Pavel Roskind133ae42005-09-23 04:18:06 -0400185 __le32 reserved;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200186
187 /* 802.11 header */
Pavel Roskind133ae42005-09-23 04:18:06 -0400188 __le16 frame_ctl;
189 __le16 duration_id;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200190 u8 addr1[ETH_ALEN];
191 u8 addr2[ETH_ALEN];
192 u8 addr3[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -0400193 __le16 seq_ctl;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200194 u8 addr4[ETH_ALEN];
195
196 /* Data length */
Pavel Roskind133ae42005-09-23 04:18:06 -0400197 __le16 data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198} __attribute__ ((packed));
199
David Kilroy47166792009-01-07 00:43:54 +0000200struct orinoco_rx_data {
201 struct hermes_rx_descriptor *desc;
202 struct sk_buff *skb;
203 struct list_head list;
204};
205
David Kilroyc63cdbe2009-06-18 23:21:33 +0100206struct orinoco_scan_data {
207 void *buf;
208 size_t len;
209 int type;
210 struct list_head list;
211};
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/********************************************************************/
214/* Function prototypes */
215/********************************************************************/
216
David Kilroy721aa2f2009-06-18 23:21:31 +0100217static int __orinoco_set_multicast_list(struct net_device *dev);
David Kilroy6415f7d2009-06-18 23:21:30 +0100218static int __orinoco_up(struct orinoco_private *priv);
219static int __orinoco_down(struct orinoco_private *priv);
David Kilroy721aa2f2009-06-18 23:21:31 +0100220static int __orinoco_commit(struct orinoco_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222/********************************************************************/
223/* Internal helper functions */
224/********************************************************************/
225
David Kilroycb1576a2009-02-04 23:05:56 +0000226void set_port_type(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 switch (priv->iw_mode) {
David Kilroy5217c572009-06-18 23:21:32 +0100229 case NL80211_IFTYPE_STATION:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 priv->port_type = 1;
231 priv->createibss = 0;
232 break;
David Kilroy5217c572009-06-18 23:21:32 +0100233 case NL80211_IFTYPE_ADHOC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (priv->prefer_port3) {
235 priv->port_type = 3;
236 priv->createibss = 0;
237 } else {
238 priv->port_type = priv->ibss_port;
239 priv->createibss = 1;
240 }
241 break;
David Kilroy5217c572009-06-18 23:21:32 +0100242 case NL80211_IFTYPE_MONITOR:
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200243 priv->port_type = 3;
244 priv->createibss = 0;
245 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 default:
247 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
248 priv->ndev->name);
249 }
250}
251
David Kilroy3994d502008-08-21 23:27:54 +0100252/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253/* Device methods */
254/********************************************************************/
255
David Kilroy593ef092010-05-01 14:05:39 +0100256int orinoco_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
David Kilroyea60a6a2009-06-18 23:21:26 +0100258 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 unsigned long flags;
260 int err;
261
262 if (orinoco_lock(priv, &flags) != 0)
263 return -EBUSY;
264
David Kilroya2608362009-06-18 23:21:23 +0100265 err = __orinoco_up(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
David Kilroya94e8422009-02-04 23:05:44 +0000267 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 priv->open = 1;
269
270 orinoco_unlock(priv, &flags);
271
272 return err;
273}
David Kilroy593ef092010-05-01 14:05:39 +0100274EXPORT_SYMBOL(orinoco_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
David Kilroy593ef092010-05-01 14:05:39 +0100276int orinoco_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
David Kilroyea60a6a2009-06-18 23:21:26 +0100278 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 int err = 0;
280
281 /* We mustn't use orinoco_lock() here, because we need to be
282 able to close the interface even if hw_unavailable is set
283 (e.g. as we're released after a PC Card removal) */
284 spin_lock_irq(&priv->lock);
285
286 priv->open = 0;
287
David Kilroya2608362009-06-18 23:21:23 +0100288 err = __orinoco_down(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 spin_unlock_irq(&priv->lock);
291
292 return err;
293}
David Kilroy593ef092010-05-01 14:05:39 +0100294EXPORT_SYMBOL(orinoco_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
David Kilroy593ef092010-05-01 14:05:39 +0100296struct net_device_stats *orinoco_get_stats(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
David Kilroyea60a6a2009-06-18 23:21:26 +0100298 struct orinoco_private *priv = ndev_priv(dev);
David Kilroy6fe9deb2009-02-04 23:05:43 +0000299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return &priv->stats;
301}
David Kilroy593ef092010-05-01 14:05:39 +0100302EXPORT_SYMBOL(orinoco_get_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
David Kilroy593ef092010-05-01 14:05:39 +0100304void orinoco_set_multicast_list(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
David Kilroyea60a6a2009-06-18 23:21:26 +0100306 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 unsigned long flags;
308
309 if (orinoco_lock(priv, &flags) != 0) {
310 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
311 "called when hw_unavailable\n", dev->name);
312 return;
313 }
314
315 __orinoco_set_multicast_list(dev);
316 orinoco_unlock(priv, &flags);
317}
David Kilroy593ef092010-05-01 14:05:39 +0100318EXPORT_SYMBOL(orinoco_set_multicast_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
David Kilroy593ef092010-05-01 14:05:39 +0100320int orinoco_change_mtu(struct net_device *dev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
David Kilroyea60a6a2009-06-18 23:21:26 +0100322 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
David Kilroya94e8422009-02-04 23:05:44 +0000324 if ((new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return -EINVAL;
326
Johannes Berg2c7060022008-10-30 22:09:54 +0100327 /* MTU + encapsulation + header length */
David Kilroya94e8422009-02-04 23:05:44 +0000328 if ((new_mtu + ENCAPS_OVERHEAD + sizeof(struct ieee80211_hdr)) >
329 (priv->nicbuf_size - ETH_HLEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return -EINVAL;
331
332 dev->mtu = new_mtu;
333
334 return 0;
335}
David Kilroy593ef092010-05-01 14:05:39 +0100336EXPORT_SYMBOL(orinoco_change_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338/********************************************************************/
339/* Tx path */
340/********************************************************************/
341
Stephen Hemmingerd0cf9c02009-08-31 19:50:57 +0000342static netdev_tx_t orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
David Kilroyea60a6a2009-06-18 23:21:26 +0100344 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 struct net_device_stats *stats = &priv->stats;
David Kilroy4af198f2009-08-05 21:23:32 +0100346 struct orinoco_tkip_key *key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 hermes_t *hw = &priv->hw;
348 int err = 0;
349 u16 txfid = priv->txfid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct ethhdr *eh;
David Kilroy6eecad72008-08-21 23:27:56 +0100351 int tx_control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 unsigned long flags;
David Kilroy4af198f2009-08-05 21:23:32 +0100353 int do_mic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
David Kilroya94e8422009-02-04 23:05:44 +0000355 if (!netif_running(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 printk(KERN_ERR "%s: Tx on stopped device!\n",
357 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400358 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
David Kilroy6fe9deb2009-02-04 23:05:43 +0000360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (netif_queue_stopped(dev)) {
David Kilroy6fe9deb2009-02-04 23:05:43 +0000362 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400364 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
David Kilroy6fe9deb2009-02-04 23:05:43 +0000366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (orinoco_lock(priv, &flags) != 0) {
368 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
369 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400370 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372
David Kilroy5217c572009-06-18 23:21:32 +0100373 if (!netif_carrier_ok(dev) ||
374 (priv->iw_mode == NL80211_IFTYPE_MONITOR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /* Oops, the firmware hasn't established a connection,
David Kilroy6fe9deb2009-02-04 23:05:43 +0000376 silently drop the packet (this seems to be the
377 safest approach). */
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400378 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380
Pavel Roskin8d5be082006-04-07 04:10:41 -0400381 /* Check packet length */
Pavel Roskina28dc812006-04-07 04:10:45 -0400382 if (skb->len < ETH_HLEN)
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400383 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
David Kilroy4af198f2009-08-05 21:23:32 +0100385 key = (struct orinoco_tkip_key *) priv->keys[priv->tx_key].key;
386
387 do_mic = ((priv->encode_alg == ORINOCO_ALG_TKIP) &&
388 (key != NULL));
389
David Kilroy6eecad72008-08-21 23:27:56 +0100390 tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
David Kilroy4af198f2009-08-05 21:23:32 +0100392 if (do_mic)
David Kilroy23edcc42008-08-21 23:28:05 +0100393 tx_control |= (priv->tx_key << HERMES_MIC_KEY_ID_SHIFT) |
394 HERMES_TXCTRL_MIC;
395
David Kilroy6eecad72008-08-21 23:27:56 +0100396 if (priv->has_alt_txcntl) {
397 /* WPA enabled firmwares have tx_cntl at the end of
398 * the 802.11 header. So write zeroed descriptor and
399 * 802.11 header at the same time
400 */
401 char desc[HERMES_802_3_OFFSET];
402 __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
403
404 memset(&desc, 0, sizeof(desc));
405
406 *txcntl = cpu_to_le16(tx_control);
David Kilroyb42f2072010-05-01 14:05:38 +0100407 err = hw->ops->bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
408 txfid, 0);
David Kilroy6eecad72008-08-21 23:27:56 +0100409 if (err) {
410 if (net_ratelimit())
411 printk(KERN_ERR "%s: Error %d writing Tx "
412 "descriptor to BAP\n", dev->name, err);
413 goto busy;
414 }
415 } else {
416 struct hermes_tx_descriptor desc;
417
418 memset(&desc, 0, sizeof(desc));
419
420 desc.tx_control = cpu_to_le16(tx_control);
David Kilroyb42f2072010-05-01 14:05:38 +0100421 err = hw->ops->bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
422 txfid, 0);
David Kilroy6eecad72008-08-21 23:27:56 +0100423 if (err) {
424 if (net_ratelimit())
425 printk(KERN_ERR "%s: Error %d writing Tx "
426 "descriptor to BAP\n", dev->name, err);
427 goto busy;
428 }
429
430 /* Clear the 802.11 header and data length fields - some
431 * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
432 * if this isn't done. */
433 hermes_clear_words(hw, HERMES_DATA0,
434 HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
David Kilroy23edcc42008-08-21 23:28:05 +0100437 eh = (struct ethhdr *)skb->data;
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 /* Encapsulate Ethernet-II frames */
440 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
Pavel Roskina28dc812006-04-07 04:10:45 -0400441 struct header_struct {
442 struct ethhdr eth; /* 802.3 header */
443 u8 encap[6]; /* 802.2 header */
444 } __attribute__ ((packed)) hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Pavel Roskina28dc812006-04-07 04:10:45 -0400446 /* Strip destination and source from the data */
447 skb_pull(skb, 2 * ETH_ALEN);
Pavel Roskina28dc812006-04-07 04:10:45 -0400448
449 /* And move them to a separate header */
450 memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
451 hdr.eth.h_proto = htons(sizeof(encaps_hdr) + skb->len);
452 memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
453
David Kilroy23edcc42008-08-21 23:28:05 +0100454 /* Insert the SNAP header */
455 if (skb_headroom(skb) < sizeof(hdr)) {
456 printk(KERN_ERR
457 "%s: Not enough headroom for 802.2 headers %d\n",
458 dev->name, skb_headroom(skb));
459 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
David Kilroy23edcc42008-08-21 23:28:05 +0100461 eh = (struct ethhdr *) skb_push(skb, sizeof(hdr));
462 memcpy(eh, &hdr, sizeof(hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464
David Kilroyb42f2072010-05-01 14:05:38 +0100465 err = hw->ops->bap_pwrite(hw, USER_BAP, skb->data, skb->len,
466 txfid, HERMES_802_3_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (err) {
468 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
469 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400470 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472
David Kilroy23edcc42008-08-21 23:28:05 +0100473 /* Calculate Michael MIC */
David Kilroy4af198f2009-08-05 21:23:32 +0100474 if (do_mic) {
David Kilroy23edcc42008-08-21 23:28:05 +0100475 u8 mic_buf[MICHAEL_MIC_LEN + 1];
476 u8 *mic;
477 size_t offset;
478 size_t len;
479
480 if (skb->len % 2) {
481 /* MIC start is on an odd boundary */
482 mic_buf[0] = skb->data[skb->len - 1];
483 mic = &mic_buf[1];
484 offset = skb->len - 1;
485 len = MICHAEL_MIC_LEN + 1;
486 } else {
487 mic = &mic_buf[0];
488 offset = skb->len;
489 len = MICHAEL_MIC_LEN;
490 }
491
David Kilroy4af198f2009-08-05 21:23:32 +0100492 orinoco_mic(priv->tx_tfm_mic, key->tx_mic,
David Kilroy23edcc42008-08-21 23:28:05 +0100493 eh->h_dest, eh->h_source, 0 /* priority */,
494 skb->data + ETH_HLEN, skb->len - ETH_HLEN, mic);
495
496 /* Write the MIC */
David Kilroyb42f2072010-05-01 14:05:38 +0100497 err = hw->ops->bap_pwrite(hw, USER_BAP, &mic_buf[0], len,
498 txfid, HERMES_802_3_OFFSET + offset);
David Kilroy23edcc42008-08-21 23:28:05 +0100499 if (err) {
500 printk(KERN_ERR "%s: Error %d writing MIC to BAP\n",
501 dev->name, err);
502 goto busy;
503 }
504 }
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /* Finally, we actually initiate the send */
507 netif_stop_queue(dev);
508
David Kilroyb42f2072010-05-01 14:05:38 +0100509 err = hw->ops->cmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 txfid, NULL);
511 if (err) {
512 netif_start_queue(dev);
Andrew Mortonc367c212005-10-19 21:23:44 -0700513 if (net_ratelimit())
514 printk(KERN_ERR "%s: Error %d transmitting packet\n",
515 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400516 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518
519 dev->trans_start = jiffies;
David Kilroy23edcc42008-08-21 23:28:05 +0100520 stats->tx_bytes += HERMES_802_3_OFFSET + skb->len;
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400521 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400523 drop:
524 stats->tx_errors++;
525 stats->tx_dropped++;
526
527 ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 dev_kfree_skb(skb);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400530 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400532 busy:
Jiri Benc2c1bd262006-04-07 04:10:47 -0400533 if (err == -EIO)
534 schedule_work(&priv->reset_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 orinoco_unlock(priv, &flags);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400536 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
540{
David Kilroyea60a6a2009-06-18 23:21:26 +0100541 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 u16 fid = hermes_read_regn(hw, ALLOCFID);
543
544 if (fid != priv->txfid) {
545 if (fid != DUMMY_FID)
546 printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
547 dev->name, fid);
548 return;
549 }
550
551 hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
552}
553
554static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
555{
David Kilroyea60a6a2009-06-18 23:21:26 +0100556 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 struct net_device_stats *stats = &priv->stats;
558
559 stats->tx_packets++;
560
561 netif_wake_queue(dev);
562
563 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
564}
565
566static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
567{
David Kilroyea60a6a2009-06-18 23:21:26 +0100568 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 struct net_device_stats *stats = &priv->stats;
570 u16 fid = hermes_read_regn(hw, TXCOMPLFID);
Pavel Roskind133ae42005-09-23 04:18:06 -0400571 u16 status;
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400572 struct hermes_txexc_data hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 int err = 0;
574
575 if (fid == DUMMY_FID)
576 return; /* Nothing's really happened */
577
Pavel Roskin48ca7032005-09-23 04:18:06 -0400578 /* Read part of the frame header - we need status and addr1 */
David Kilroyb42f2072010-05-01 14:05:38 +0100579 err = hw->ops->bap_pread(hw, IRQ_BAP, &hdr,
580 sizeof(struct hermes_txexc_data),
581 fid, 0);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200582
583 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
584 stats->tx_errors++;
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (err) {
587 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
588 "(FID=%04X error %d)\n",
589 dev->name, fid, err);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200590 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
David Kilroy6fe9deb2009-02-04 23:05:43 +0000592
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200593 DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
594 err, fid);
David Kilroy6fe9deb2009-02-04 23:05:43 +0000595
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200596 /* We produce a TXDROP event only for retry or lifetime
597 * exceeded, because that's the only status that really mean
598 * that this particular node went away.
599 * Other errors means that *we* screwed up. - Jean II */
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400600 status = le16_to_cpu(hdr.desc.status);
Pavel Roskind133ae42005-09-23 04:18:06 -0400601 if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200602 union iwreq_data wrqu;
603
604 /* Copy 802.11 dest address.
605 * We use the 802.11 header because the frame may
606 * not be 802.3 or may be mangled...
607 * In Ad-Hoc mode, it will be the node address.
608 * In managed mode, it will be most likely the AP addr
609 * User space will figure out how to convert it to
610 * whatever it needs (IP address or else).
611 * - Jean II */
612 memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
613 wrqu.addr.sa_family = ARPHRD_ETHER;
614
615 /* Send event to user space */
616 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
David Kilroy593ef092010-05-01 14:05:39 +0100622void orinoco_tx_timeout(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
David Kilroyea60a6a2009-06-18 23:21:26 +0100624 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 struct net_device_stats *stats = &priv->stats;
626 struct hermes *hw = &priv->hw;
627
628 printk(KERN_WARNING "%s: Tx timeout! "
629 "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
630 dev->name, hermes_read_regn(hw, ALLOCFID),
631 hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
632
633 stats->tx_errors++;
634
635 schedule_work(&priv->reset_work);
636}
David Kilroy593ef092010-05-01 14:05:39 +0100637EXPORT_SYMBOL(orinoco_tx_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639/********************************************************************/
640/* Rx path (data frames) */
641/********************************************************************/
642
643/* Does the frame have a SNAP header indicating it should be
644 * de-encapsulated to Ethernet-II? */
645static inline int is_ethersnap(void *_hdr)
646{
647 u8 *hdr = _hdr;
648
649 /* We de-encapsulate all packets which, a) have SNAP headers
650 * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
651 * and where b) the OUI of the SNAP header is 00:00:00 or
652 * 00:00:f8 - we need both because different APs appear to use
653 * different OUIs for some reason */
654 return (memcmp(hdr, &encaps_hdr, 5) == 0)
David Kilroya94e8422009-02-04 23:05:44 +0000655 && ((hdr[5] == 0x00) || (hdr[5] == 0xf8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
658static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
659 int level, int noise)
660{
Pavel Roskin343c6862005-09-09 18:43:02 -0400661 struct iw_quality wstats;
662 wstats.level = level - 0x95;
663 wstats.noise = noise - 0x95;
664 wstats.qual = (level > noise) ? (level - noise) : 0;
Andrey Borzenkovf941f852008-11-15 17:15:09 +0300665 wstats.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
Pavel Roskin343c6862005-09-09 18:43:02 -0400666 /* Update spy records */
667 wireless_spy_update(dev, mac, &wstats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
670static void orinoco_stat_gather(struct net_device *dev,
671 struct sk_buff *skb,
672 struct hermes_rx_descriptor *desc)
673{
David Kilroyea60a6a2009-06-18 23:21:26 +0100674 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 /* Using spy support with lots of Rx packets, like in an
677 * infrastructure (AP), will really slow down everything, because
678 * the MAC address must be compared to each entry of the spy list.
679 * If the user really asks for it (set some address in the
680 * spy list), we do it, but he will pay the price.
681 * Note that to get here, you need both WIRELESS_SPY
682 * compiled in AND some addresses in the list !!!
683 */
684 /* Note : gcc will optimise the whole section away if
685 * WIRELESS_SPY is not defined... - Jean II */
686 if (SPY_NUMBER(priv)) {
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700687 orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 desc->signal, desc->silence);
689 }
690}
691
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200692/*
693 * orinoco_rx_monitor - handle received monitor frames.
694 *
695 * Arguments:
696 * dev network device
697 * rxfid received FID
698 * desc rx descriptor of the frame
699 *
700 * Call context: interrupt
701 */
702static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
703 struct hermes_rx_descriptor *desc)
704{
705 u32 hdrlen = 30; /* return full header by default */
706 u32 datalen = 0;
707 u16 fc;
708 int err;
709 int len;
710 struct sk_buff *skb;
David Kilroyea60a6a2009-06-18 23:21:26 +0100711 struct orinoco_private *priv = ndev_priv(dev);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200712 struct net_device_stats *stats = &priv->stats;
713 hermes_t *hw = &priv->hw;
714
715 len = le16_to_cpu(desc->data_len);
716
717 /* Determine the size of the header and the data */
718 fc = le16_to_cpu(desc->frame_ctl);
719 switch (fc & IEEE80211_FCTL_FTYPE) {
720 case IEEE80211_FTYPE_DATA:
721 if ((fc & IEEE80211_FCTL_TODS)
722 && (fc & IEEE80211_FCTL_FROMDS))
723 hdrlen = 30;
724 else
725 hdrlen = 24;
726 datalen = len;
727 break;
728 case IEEE80211_FTYPE_MGMT:
729 hdrlen = 24;
730 datalen = len;
731 break;
732 case IEEE80211_FTYPE_CTL:
733 switch (fc & IEEE80211_FCTL_STYPE) {
734 case IEEE80211_STYPE_PSPOLL:
735 case IEEE80211_STYPE_RTS:
736 case IEEE80211_STYPE_CFEND:
737 case IEEE80211_STYPE_CFENDACK:
738 hdrlen = 16;
739 break;
740 case IEEE80211_STYPE_CTS:
741 case IEEE80211_STYPE_ACK:
742 hdrlen = 10;
743 break;
744 }
745 break;
746 default:
747 /* Unknown frame type */
748 break;
749 }
750
751 /* sanity check the length */
Johannes Berg2c7060022008-10-30 22:09:54 +0100752 if (datalen > IEEE80211_MAX_DATA_LEN + 12) {
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200753 printk(KERN_DEBUG "%s: oversized monitor frame, "
754 "data length = %d\n", dev->name, datalen);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200755 stats->rx_length_errors++;
756 goto update_stats;
757 }
758
759 skb = dev_alloc_skb(hdrlen + datalen);
760 if (!skb) {
761 printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
762 dev->name);
Florin Malitabb6e0932006-05-22 22:35:30 -0700763 goto update_stats;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200764 }
765
766 /* Copy the 802.11 header to the skb */
767 memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700768 skb_reset_mac_header(skb);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200769
770 /* If any, copy the data from the card to the skb */
771 if (datalen > 0) {
David Kilroyb42f2072010-05-01 14:05:38 +0100772 err = hw->ops->bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
773 ALIGN(datalen, 2), rxfid,
774 HERMES_802_2_OFFSET);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200775 if (err) {
776 printk(KERN_ERR "%s: error %d reading monitor frame\n",
777 dev->name, err);
778 goto drop;
779 }
780 }
781
782 skb->dev = dev;
783 skb->ip_summed = CHECKSUM_NONE;
784 skb->pkt_type = PACKET_OTHERHOST;
Harvey Harrisonc1b4aa32009-01-29 13:26:44 -0800785 skb->protocol = cpu_to_be16(ETH_P_802_2);
David Kilroy6fe9deb2009-02-04 23:05:43 +0000786
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200787 stats->rx_packets++;
788 stats->rx_bytes += skb->len;
789
790 netif_rx(skb);
791 return;
792
793 drop:
794 dev_kfree_skb_irq(skb);
795 update_stats:
796 stats->rx_errors++;
797 stats->rx_dropped++;
798}
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800static void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
801{
David Kilroyea60a6a2009-06-18 23:21:26 +0100802 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 struct net_device_stats *stats = &priv->stats;
804 struct iw_statistics *wstats = &priv->wstats;
805 struct sk_buff *skb = NULL;
David Kilroy31afcef2008-08-21 23:28:04 +0100806 u16 rxfid, status;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200807 int length;
David Kilroy31afcef2008-08-21 23:28:04 +0100808 struct hermes_rx_descriptor *desc;
809 struct orinoco_rx_data *rx_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 int err;
811
David Kilroy31afcef2008-08-21 23:28:04 +0100812 desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
813 if (!desc) {
814 printk(KERN_WARNING
815 "%s: Can't allocate space for RX descriptor\n",
816 dev->name);
817 goto update_stats;
818 }
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 rxfid = hermes_read_regn(hw, RXFID);
821
David Kilroyb42f2072010-05-01 14:05:38 +0100822 err = hw->ops->bap_pread(hw, IRQ_BAP, desc, sizeof(*desc),
823 rxfid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (err) {
825 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
826 "Frame dropped.\n", dev->name, err);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200827 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829
David Kilroy31afcef2008-08-21 23:28:04 +0100830 status = le16_to_cpu(desc->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200832 if (status & HERMES_RXSTAT_BADCRC) {
833 DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
834 dev->name);
835 stats->rx_crc_errors++;
836 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200839 /* Handle frames in monitor mode */
David Kilroy5217c572009-06-18 23:21:32 +0100840 if (priv->iw_mode == NL80211_IFTYPE_MONITOR) {
David Kilroy31afcef2008-08-21 23:28:04 +0100841 orinoco_rx_monitor(dev, rxfid, desc);
842 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200845 if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
846 DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
847 dev->name);
848 wstats->discard.code++;
849 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
851
David Kilroy31afcef2008-08-21 23:28:04 +0100852 length = le16_to_cpu(desc->data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 /* Sanity checks */
855 if (length < 3) { /* No for even an 802.2 LLC header */
856 /* At least on Symbol firmware with PCF we get quite a
David Kilroy6fe9deb2009-02-04 23:05:43 +0000857 lot of these legitimately - Poll frames with no
858 data. */
David Kilroy31afcef2008-08-21 23:28:04 +0100859 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Johannes Berg2c7060022008-10-30 22:09:54 +0100861 if (length > IEEE80211_MAX_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
863 dev->name, length);
864 stats->rx_length_errors++;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200865 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867
David Kilroy23edcc42008-08-21 23:28:05 +0100868 /* Payload size does not include Michael MIC. Increase payload
869 * size to read it together with the data. */
870 if (status & HERMES_RXSTAT_MIC)
871 length += MICHAEL_MIC_LEN;
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 /* We need space for the packet data itself, plus an ethernet
874 header, plus 2 bytes so we can align the IP header on a
875 32bit boundary, plus 1 byte so we can read in odd length
876 packets from the card, which has an IO granularity of 16
David Kilroy6fe9deb2009-02-04 23:05:43 +0000877 bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 skb = dev_alloc_skb(length+ETH_HLEN+2+1);
879 if (!skb) {
880 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
881 dev->name);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200882 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200885 /* We'll prepend the header, so reserve space for it. The worst
886 case is no decapsulation, when 802.3 header is prepended and
887 nothing is removed. 2 is for aligning the IP header. */
888 skb_reserve(skb, ETH_HLEN + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
David Kilroyb42f2072010-05-01 14:05:38 +0100890 err = hw->ops->bap_pread(hw, IRQ_BAP, skb_put(skb, length),
891 ALIGN(length, 2), rxfid,
892 HERMES_802_2_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (err) {
894 printk(KERN_ERR "%s: error %d reading frame. "
895 "Frame dropped.\n", dev->name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 goto drop;
897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
David Kilroy31afcef2008-08-21 23:28:04 +0100899 /* Add desc and skb to rx queue */
900 rx_data = kzalloc(sizeof(*rx_data), GFP_ATOMIC);
901 if (!rx_data) {
902 printk(KERN_WARNING "%s: Can't allocate RX packet\n",
903 dev->name);
904 goto drop;
905 }
906 rx_data->desc = desc;
907 rx_data->skb = skb;
908 list_add_tail(&rx_data->list, &priv->rx_list);
909 tasklet_schedule(&priv->rx_tasklet);
910
911 return;
912
913drop:
914 dev_kfree_skb_irq(skb);
915update_stats:
916 stats->rx_errors++;
917 stats->rx_dropped++;
918out:
919 kfree(desc);
920}
921
922static void orinoco_rx(struct net_device *dev,
923 struct hermes_rx_descriptor *desc,
924 struct sk_buff *skb)
925{
David Kilroyea60a6a2009-06-18 23:21:26 +0100926 struct orinoco_private *priv = ndev_priv(dev);
David Kilroy31afcef2008-08-21 23:28:04 +0100927 struct net_device_stats *stats = &priv->stats;
928 u16 status, fc;
929 int length;
930 struct ethhdr *hdr;
931
932 status = le16_to_cpu(desc->status);
933 length = le16_to_cpu(desc->data_len);
934 fc = le16_to_cpu(desc->frame_ctl);
935
David Kilroy23edcc42008-08-21 23:28:05 +0100936 /* Calculate and check MIC */
937 if (status & HERMES_RXSTAT_MIC) {
David Kilroy4af198f2009-08-05 21:23:32 +0100938 struct orinoco_tkip_key *key;
David Kilroy23edcc42008-08-21 23:28:05 +0100939 int key_id = ((status & HERMES_RXSTAT_MIC_KEY_ID) >>
940 HERMES_MIC_KEY_ID_SHIFT);
941 u8 mic[MICHAEL_MIC_LEN];
942 u8 *rxmic;
943 u8 *src = (fc & IEEE80211_FCTL_FROMDS) ?
944 desc->addr3 : desc->addr2;
945
946 /* Extract Michael MIC from payload */
947 rxmic = skb->data + skb->len - MICHAEL_MIC_LEN;
948
949 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
950 length -= MICHAEL_MIC_LEN;
951
David Kilroy4af198f2009-08-05 21:23:32 +0100952 key = (struct orinoco_tkip_key *) priv->keys[key_id].key;
953
954 if (!key) {
955 printk(KERN_WARNING "%s: Received encrypted frame from "
956 "%pM using key %i, but key is not installed\n",
957 dev->name, src, key_id);
958 goto drop;
959 }
960
961 orinoco_mic(priv->rx_tfm_mic, key->rx_mic, desc->addr1, src,
David Kilroy23edcc42008-08-21 23:28:05 +0100962 0, /* priority or QoS? */
David Kilroy4af198f2009-08-05 21:23:32 +0100963 skb->data, skb->len, &mic[0]);
David Kilroy23edcc42008-08-21 23:28:05 +0100964
965 if (memcmp(mic, rxmic,
966 MICHAEL_MIC_LEN)) {
967 union iwreq_data wrqu;
968 struct iw_michaelmicfailure wxmic;
David Kilroy23edcc42008-08-21 23:28:05 +0100969
970 printk(KERN_WARNING "%s: "
Johannes Berge1749612008-10-27 15:59:26 -0700971 "Invalid Michael MIC in data frame from %pM, "
David Kilroy23edcc42008-08-21 23:28:05 +0100972 "using key %i\n",
Johannes Berge1749612008-10-27 15:59:26 -0700973 dev->name, src, key_id);
David Kilroy23edcc42008-08-21 23:28:05 +0100974
975 /* TODO: update stats */
976
977 /* Notify userspace */
978 memset(&wxmic, 0, sizeof(wxmic));
979 wxmic.flags = key_id & IW_MICFAILURE_KEY_ID;
980 wxmic.flags |= (desc->addr1[0] & 1) ?
981 IW_MICFAILURE_GROUP : IW_MICFAILURE_PAIRWISE;
982 wxmic.src_addr.sa_family = ARPHRD_ETHER;
983 memcpy(wxmic.src_addr.sa_data, src, ETH_ALEN);
984
985 (void) orinoco_hw_get_tkip_iv(priv, key_id,
986 &wxmic.tsc[0]);
987
988 memset(&wrqu, 0, sizeof(wrqu));
989 wrqu.data.length = sizeof(wxmic);
990 wireless_send_event(dev, IWEVMICHAELMICFAILURE, &wrqu,
991 (char *) &wxmic);
992
993 goto drop;
994 }
995 }
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 /* Handle decapsulation
998 * In most cases, the firmware tell us about SNAP frames.
999 * For some reason, the SNAP frames sent by LinkSys APs
1000 * are not properly recognised by most firmwares.
1001 * So, check ourselves */
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001002 if (length >= ENCAPS_OVERHEAD &&
1003 (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
1004 ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
1005 is_ethersnap(skb->data))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 /* These indicate a SNAP within 802.2 LLC within
1007 802.11 frame which we'll need to de-encapsulate to
1008 the original EthernetII frame. */
David Kilroyb2f30a02009-02-04 23:05:46 +00001009 hdr = (struct ethhdr *)skb_push(skb,
1010 ETH_HLEN - ENCAPS_OVERHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 } else {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001012 /* 802.3 frame - prepend 802.3 header as is */
1013 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
1014 hdr->h_proto = htons(length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
David Kilroy31afcef2008-08-21 23:28:04 +01001016 memcpy(hdr->h_dest, desc->addr1, ETH_ALEN);
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001017 if (fc & IEEE80211_FCTL_FROMDS)
David Kilroy31afcef2008-08-21 23:28:04 +01001018 memcpy(hdr->h_source, desc->addr3, ETH_ALEN);
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001019 else
David Kilroy31afcef2008-08-21 23:28:04 +01001020 memcpy(hdr->h_source, desc->addr2, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 skb->protocol = eth_type_trans(skb, dev);
1023 skb->ip_summed = CHECKSUM_NONE;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001024 if (fc & IEEE80211_FCTL_TODS)
1025 skb->pkt_type = PACKET_OTHERHOST;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 /* Process the wireless stats if needed */
David Kilroy31afcef2008-08-21 23:28:04 +01001028 orinoco_stat_gather(dev, skb, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 /* Pass the packet to the networking stack */
1031 netif_rx(skb);
1032 stats->rx_packets++;
1033 stats->rx_bytes += length;
1034
1035 return;
David Kilroy23edcc42008-08-21 23:28:05 +01001036
1037 drop:
1038 dev_kfree_skb(skb);
1039 stats->rx_errors++;
1040 stats->rx_dropped++;
David Kilroy31afcef2008-08-21 23:28:04 +01001041}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
David Kilroy31afcef2008-08-21 23:28:04 +01001043static void orinoco_rx_isr_tasklet(unsigned long data)
1044{
David Kilroy53819562009-06-18 23:21:28 +01001045 struct orinoco_private *priv = (struct orinoco_private *) data;
1046 struct net_device *dev = priv->ndev;
David Kilroy31afcef2008-08-21 23:28:04 +01001047 struct orinoco_rx_data *rx_data, *temp;
1048 struct hermes_rx_descriptor *desc;
1049 struct sk_buff *skb;
David Kilroy20953ad2009-01-07 00:23:55 +00001050 unsigned long flags;
1051
1052 /* orinoco_rx requires the driver lock, and we also need to
1053 * protect priv->rx_list, so just hold the lock over the
1054 * lot.
1055 *
1056 * If orinoco_lock fails, we've unplugged the card. In this
1057 * case just abort. */
1058 if (orinoco_lock(priv, &flags) != 0)
1059 return;
David Kilroy31afcef2008-08-21 23:28:04 +01001060
1061 /* extract desc and skb from queue */
1062 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
1063 desc = rx_data->desc;
1064 skb = rx_data->skb;
1065 list_del(&rx_data->list);
1066 kfree(rx_data);
1067
1068 orinoco_rx(dev, desc, skb);
1069
1070 kfree(desc);
1071 }
David Kilroy20953ad2009-01-07 00:23:55 +00001072
1073 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
1076/********************************************************************/
1077/* Rx path (info frames) */
1078/********************************************************************/
1079
1080static void print_linkstatus(struct net_device *dev, u16 status)
1081{
David Kilroy21312662009-02-04 23:05:47 +00001082 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 if (suppress_linkstatus)
1085 return;
1086
1087 switch (status) {
1088 case HERMES_LINKSTATUS_NOT_CONNECTED:
1089 s = "Not Connected";
1090 break;
1091 case HERMES_LINKSTATUS_CONNECTED:
1092 s = "Connected";
1093 break;
1094 case HERMES_LINKSTATUS_DISCONNECTED:
1095 s = "Disconnected";
1096 break;
1097 case HERMES_LINKSTATUS_AP_CHANGE:
1098 s = "AP Changed";
1099 break;
1100 case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1101 s = "AP Out of Range";
1102 break;
1103 case HERMES_LINKSTATUS_AP_IN_RANGE:
1104 s = "AP In Range";
1105 break;
1106 case HERMES_LINKSTATUS_ASSOC_FAILED:
1107 s = "Association Failed";
1108 break;
1109 default:
1110 s = "UNKNOWN";
1111 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001112
Pavel Roskin11eaea42009-01-18 23:20:58 -05001113 printk(KERN_DEBUG "%s: New link status: %s (%04x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 dev->name, s, status);
1115}
1116
Christoph Hellwig16739b02005-06-19 01:27:51 +02001117/* Search scan results for requested BSSID, join it if found */
David Howellsc4028952006-11-22 14:57:56 +00001118static void orinoco_join_ap(struct work_struct *work)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001119{
David Howellsc4028952006-11-22 14:57:56 +00001120 struct orinoco_private *priv =
1121 container_of(work, struct orinoco_private, join_work);
1122 struct net_device *dev = priv->ndev;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001123 struct hermes *hw = &priv->hw;
1124 int err;
1125 unsigned long flags;
1126 struct join_req {
1127 u8 bssid[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -04001128 __le16 channel;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001129 } __attribute__ ((packed)) req;
1130 const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001131 struct prism2_scan_apinfo *atom = NULL;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001132 int offset = 4;
Pavel Roskinc89cc222005-09-01 20:06:06 -04001133 int found = 0;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001134 u8 *buf;
1135 u16 len;
1136
1137 /* Allocate buffer for scan results */
1138 buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
David Kilroya94e8422009-02-04 23:05:44 +00001139 if (!buf)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001140 return;
1141
1142 if (orinoco_lock(priv, &flags) != 0)
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001143 goto fail_lock;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001144
1145 /* Sanity checks in case user changed something in the meantime */
David Kilroya94e8422009-02-04 23:05:44 +00001146 if (!priv->bssid_fixed)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001147 goto out;
1148
1149 if (strlen(priv->desired_essid) == 0)
1150 goto out;
1151
1152 /* Read scan results from the firmware */
David Kilroyb42f2072010-05-01 14:05:38 +01001153 err = hw->ops->read_ltv(hw, USER_BAP,
1154 HERMES_RID_SCANRESULTSTABLE,
1155 MAX_SCAN_LEN, &len, buf);
Christoph Hellwig16739b02005-06-19 01:27:51 +02001156 if (err) {
1157 printk(KERN_ERR "%s: Cannot read scan results\n",
1158 dev->name);
1159 goto out;
1160 }
1161
1162 len = HERMES_RECLEN_TO_BYTES(len);
1163
1164 /* Go through the scan results looking for the channel of the AP
1165 * we were requested to join */
1166 for (; offset + atom_len <= len; offset += atom_len) {
1167 atom = (struct prism2_scan_apinfo *) (buf + offset);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001168 if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1169 found = 1;
1170 break;
1171 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001172 }
1173
David Kilroya94e8422009-02-04 23:05:44 +00001174 if (!found) {
Pavel Roskinc89cc222005-09-01 20:06:06 -04001175 DEBUG(1, "%s: Requested AP not found in scan results\n",
1176 dev->name);
1177 goto out;
1178 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001179
Christoph Hellwig16739b02005-06-19 01:27:51 +02001180 memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1181 req.channel = atom->channel; /* both are little-endian */
1182 err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1183 &req);
1184 if (err)
1185 printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1186
1187 out:
Christoph Hellwig16739b02005-06-19 01:27:51 +02001188 orinoco_unlock(priv, &flags);
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001189
1190 fail_lock:
1191 kfree(buf);
Christoph Hellwig16739b02005-06-19 01:27:51 +02001192}
1193
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001194/* Send new BSSID to userspace */
David Kilroy6cd90b12008-08-21 23:28:00 +01001195static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001196{
David Howellsc4028952006-11-22 14:57:56 +00001197 struct net_device *dev = priv->ndev;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001198 struct hermes *hw = &priv->hw;
1199 union iwreq_data wrqu;
1200 int err;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001201
David Kilroyb42f2072010-05-01 14:05:38 +01001202 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
1203 ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001204 if (err != 0)
David Kilroy6cd90b12008-08-21 23:28:00 +01001205 return;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001206
1207 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1208
1209 /* Send event to user space */
1210 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001211}
1212
David Kilroy06009fd2008-08-21 23:28:03 +01001213static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
1214{
1215 struct net_device *dev = priv->ndev;
1216 struct hermes *hw = &priv->hw;
1217 union iwreq_data wrqu;
1218 int err;
1219 u8 buf[88];
1220 u8 *ie;
1221
1222 if (!priv->has_wpa)
1223 return;
1224
David Kilroyb42f2072010-05-01 14:05:38 +01001225 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
1226 sizeof(buf), NULL, &buf);
David Kilroy06009fd2008-08-21 23:28:03 +01001227 if (err != 0)
1228 return;
1229
1230 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1231 if (ie) {
1232 int rem = sizeof(buf) - (ie - &buf[0]);
1233 wrqu.data.length = ie[1] + 2;
1234 if (wrqu.data.length > rem)
1235 wrqu.data.length = rem;
1236
1237 if (wrqu.data.length)
1238 /* Send event to user space */
1239 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
1240 }
1241}
1242
1243static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
1244{
1245 struct net_device *dev = priv->ndev;
1246 struct hermes *hw = &priv->hw;
1247 union iwreq_data wrqu;
1248 int err;
1249 u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
1250 u8 *ie;
1251
1252 if (!priv->has_wpa)
1253 return;
1254
David Kilroyb42f2072010-05-01 14:05:38 +01001255 err = hw->ops->read_ltv(hw, USER_BAP,
1256 HERMES_RID_CURRENT_ASSOC_RESP_INFO,
1257 sizeof(buf), NULL, &buf);
David Kilroy06009fd2008-08-21 23:28:03 +01001258 if (err != 0)
1259 return;
1260
1261 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1262 if (ie) {
1263 int rem = sizeof(buf) - (ie - &buf[0]);
1264 wrqu.data.length = ie[1] + 2;
1265 if (wrqu.data.length > rem)
1266 wrqu.data.length = rem;
1267
1268 if (wrqu.data.length)
1269 /* Send event to user space */
1270 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
1271 }
1272}
1273
David Kilroy6cd90b12008-08-21 23:28:00 +01001274static void orinoco_send_wevents(struct work_struct *work)
1275{
1276 struct orinoco_private *priv =
1277 container_of(work, struct orinoco_private, wevent_work);
1278 unsigned long flags;
1279
1280 if (orinoco_lock(priv, &flags) != 0)
1281 return;
1282
David Kilroy06009fd2008-08-21 23:28:03 +01001283 orinoco_send_assocreqie_wevent(priv);
1284 orinoco_send_assocrespie_wevent(priv);
David Kilroy6cd90b12008-08-21 23:28:00 +01001285 orinoco_send_bssid_wevent(priv);
1286
1287 orinoco_unlock(priv, &flags);
1288}
Dan Williams1e3428e2007-10-10 23:56:25 -04001289
David Kilroyc63cdbe2009-06-18 23:21:33 +01001290static void qbuf_scan(struct orinoco_private *priv, void *buf,
1291 int len, int type)
1292{
1293 struct orinoco_scan_data *sd;
1294 unsigned long flags;
1295
1296 sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
1297 sd->buf = buf;
1298 sd->len = len;
1299 sd->type = type;
1300
1301 spin_lock_irqsave(&priv->scan_lock, flags);
1302 list_add_tail(&sd->list, &priv->scan_list);
1303 spin_unlock_irqrestore(&priv->scan_lock, flags);
1304
1305 schedule_work(&priv->process_scan);
1306}
1307
1308static void qabort_scan(struct orinoco_private *priv)
1309{
1310 struct orinoco_scan_data *sd;
1311 unsigned long flags;
1312
1313 sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
1314 sd->len = -1; /* Abort */
1315
1316 spin_lock_irqsave(&priv->scan_lock, flags);
1317 list_add_tail(&sd->list, &priv->scan_list);
1318 spin_unlock_irqrestore(&priv->scan_lock, flags);
1319
1320 schedule_work(&priv->process_scan);
1321}
1322
1323static void orinoco_process_scan_results(struct work_struct *work)
1324{
1325 struct orinoco_private *priv =
1326 container_of(work, struct orinoco_private, process_scan);
1327 struct orinoco_scan_data *sd, *temp;
1328 unsigned long flags;
1329 void *buf;
1330 int len;
1331 int type;
1332
1333 spin_lock_irqsave(&priv->scan_lock, flags);
1334 list_for_each_entry_safe(sd, temp, &priv->scan_list, list) {
1335 spin_unlock_irqrestore(&priv->scan_lock, flags);
1336
1337 buf = sd->buf;
1338 len = sd->len;
1339 type = sd->type;
1340
1341 list_del(&sd->list);
1342 kfree(sd);
1343
1344 if (len > 0) {
1345 if (type == HERMES_INQ_CHANNELINFO)
1346 orinoco_add_extscan_result(priv, buf, len);
1347 else
1348 orinoco_add_hostscan_results(priv, buf, len);
1349
1350 kfree(buf);
1351 } else if (priv->scan_request) {
1352 /* Either abort or complete the scan */
1353 cfg80211_scan_done(priv->scan_request, (len < 0));
1354 priv->scan_request = NULL;
1355 }
1356
1357 spin_lock_irqsave(&priv->scan_lock, flags);
1358 }
1359 spin_unlock_irqrestore(&priv->scan_lock, flags);
1360}
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362static void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
1363{
David Kilroyea60a6a2009-06-18 23:21:26 +01001364 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 u16 infofid;
1366 struct {
Pavel Roskind133ae42005-09-23 04:18:06 -04001367 __le16 len;
1368 __le16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 } __attribute__ ((packed)) info;
1370 int len, type;
1371 int err;
1372
1373 /* This is an answer to an INQUIRE command that we did earlier,
1374 * or an information "event" generated by the card
1375 * The controller return to us a pseudo frame containing
1376 * the information in question - Jean II */
1377 infofid = hermes_read_regn(hw, INFOFID);
1378
1379 /* Read the info frame header - don't try too hard */
David Kilroyb42f2072010-05-01 14:05:38 +01001380 err = hw->ops->bap_pread(hw, IRQ_BAP, &info, sizeof(info),
1381 infofid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (err) {
1383 printk(KERN_ERR "%s: error %d reading info frame. "
1384 "Frame dropped.\n", dev->name, err);
1385 return;
1386 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
1389 type = le16_to_cpu(info.type);
1390
1391 switch (type) {
1392 case HERMES_INQ_TALLIES: {
1393 struct hermes_tallies_frame tallies;
1394 struct iw_statistics *wstats = &priv->wstats;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 if (len > sizeof(tallies)) {
1397 printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
1398 dev->name, len);
1399 len = sizeof(tallies);
1400 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001401
David Kilroyb42f2072010-05-01 14:05:38 +01001402 err = hw->ops->bap_pread(hw, IRQ_BAP, &tallies, len,
1403 infofid, sizeof(info));
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001404 if (err)
1405 break;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 /* Increment our various counters */
1408 /* wstats->discard.nwid - no wrong BSSID stuff */
1409 wstats->discard.code +=
1410 le16_to_cpu(tallies.RxWEPUndecryptable);
David Kilroy6fe9deb2009-02-04 23:05:43 +00001411 if (len == sizeof(tallies))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 wstats->discard.code +=
1413 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
1414 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
1415 wstats->discard.misc +=
1416 le16_to_cpu(tallies.TxDiscardsWrongSA);
1417 wstats->discard.fragment +=
1418 le16_to_cpu(tallies.RxMsgInBadMsgFragments);
1419 wstats->discard.retries +=
1420 le16_to_cpu(tallies.TxRetryLimitExceeded);
1421 /* wstats->miss.beacon - no match */
1422 }
1423 break;
1424 case HERMES_INQ_LINKSTATUS: {
1425 struct hermes_linkstatus linkstatus;
1426 u16 newstatus;
1427 int connected;
1428
David Kilroy5217c572009-06-18 23:21:32 +01001429 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001430 break;
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (len != sizeof(linkstatus)) {
1433 printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
1434 dev->name, len);
1435 break;
1436 }
1437
David Kilroyb42f2072010-05-01 14:05:38 +01001438 err = hw->ops->bap_pread(hw, IRQ_BAP, &linkstatus, len,
1439 infofid, sizeof(info));
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001440 if (err)
1441 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 newstatus = le16_to_cpu(linkstatus.linkstatus);
1443
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001444 /* Symbol firmware uses "out of range" to signal that
1445 * the hostscan frame can be requested. */
1446 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
1447 priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
David Kilroyc63cdbe2009-06-18 23:21:33 +01001448 priv->has_hostscan && priv->scan_request) {
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001449 hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
1450 break;
1451 }
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
1454 || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
1455 || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
1456
1457 if (connected)
1458 netif_carrier_on(dev);
David Gibson7bb7c3a2005-05-12 20:02:10 -04001459 else if (!ignore_disconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 netif_carrier_off(dev);
1461
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001462 if (newstatus != priv->last_linkstatus) {
1463 priv->last_linkstatus = newstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 print_linkstatus(dev, newstatus);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001465 /* The info frame contains only one word which is the
1466 * status (see hermes.h). The status is pretty boring
1467 * in itself, that's why we export the new BSSID...
1468 * Jean II */
1469 schedule_work(&priv->wevent_work);
1470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
1472 break;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001473 case HERMES_INQ_SCAN:
David Kilroyc63cdbe2009-06-18 23:21:33 +01001474 if (!priv->scan_request && priv->bssid_fixed &&
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001475 priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
1476 schedule_work(&priv->join_work);
1477 break;
1478 }
1479 /* fall through */
1480 case HERMES_INQ_HOSTSCAN:
1481 case HERMES_INQ_HOSTSCAN_SYMBOL: {
1482 /* Result of a scanning. Contains information about
1483 * cells in the vicinity - Jean II */
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001484 unsigned char *buf;
1485
1486 /* Sanity check */
1487 if (len > 4096) {
1488 printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
1489 dev->name, len);
David Kilroyc63cdbe2009-06-18 23:21:33 +01001490 qabort_scan(priv);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001491 break;
1492 }
1493
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001494 /* Allocate buffer for results */
1495 buf = kmalloc(len, GFP_ATOMIC);
David Kilroyc63cdbe2009-06-18 23:21:33 +01001496 if (buf == NULL) {
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001497 /* No memory, so can't printk()... */
David Kilroyc63cdbe2009-06-18 23:21:33 +01001498 qabort_scan(priv);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001499 break;
David Kilroyc63cdbe2009-06-18 23:21:33 +01001500 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001501
1502 /* Read scan data */
David Kilroyb42f2072010-05-01 14:05:38 +01001503 err = hw->ops->bap_pread(hw, IRQ_BAP, (void *) buf, len,
1504 infofid, sizeof(info));
Pavel Roskin708218b2005-09-01 20:05:19 -04001505 if (err) {
1506 kfree(buf);
David Kilroyc63cdbe2009-06-18 23:21:33 +01001507 qabort_scan(priv);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001508 break;
Pavel Roskin708218b2005-09-01 20:05:19 -04001509 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001510
1511#ifdef ORINOCO_DEBUG
1512 {
1513 int i;
1514 printk(KERN_DEBUG "Scan result [%02X", buf[0]);
David Kilroya94e8422009-02-04 23:05:44 +00001515 for (i = 1; i < (len * 2); i++)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001516 printk(":%02X", buf[i]);
1517 printk("]\n");
1518 }
1519#endif /* ORINOCO_DEBUG */
1520
David Kilroyc63cdbe2009-06-18 23:21:33 +01001521 qbuf_scan(priv, buf, len, type);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001522 }
1523 break;
David Kilroy01632fa2008-08-21 23:27:58 +01001524 case HERMES_INQ_CHANNELINFO:
1525 {
1526 struct agere_ext_scan_info *bss;
1527
David Kilroyc63cdbe2009-06-18 23:21:33 +01001528 if (!priv->scan_request) {
David Kilroy01632fa2008-08-21 23:27:58 +01001529 printk(KERN_DEBUG "%s: Got chaninfo without scan, "
1530 "len=%d\n", dev->name, len);
1531 break;
1532 }
1533
1534 /* An empty result indicates that the scan is complete */
1535 if (len == 0) {
David Kilroyc63cdbe2009-06-18 23:21:33 +01001536 qbuf_scan(priv, NULL, len, type);
David Kilroy01632fa2008-08-21 23:27:58 +01001537 break;
1538 }
1539
1540 /* Sanity check */
David Kilroyc63cdbe2009-06-18 23:21:33 +01001541 else if (len < (offsetof(struct agere_ext_scan_info,
David Kilroy01632fa2008-08-21 23:27:58 +01001542 data) + 2)) {
1543 /* Drop this result now so we don't have to
1544 * keep checking later */
1545 printk(KERN_WARNING
1546 "%s: Ext scan results too short (%d bytes)\n",
1547 dev->name, len);
1548 break;
1549 }
1550
David Kilroyc63cdbe2009-06-18 23:21:33 +01001551 bss = kmalloc(len, GFP_ATOMIC);
David Kilroy01632fa2008-08-21 23:27:58 +01001552 if (bss == NULL)
1553 break;
1554
1555 /* Read scan data */
David Kilroyb42f2072010-05-01 14:05:38 +01001556 err = hw->ops->bap_pread(hw, IRQ_BAP, (void *) bss, len,
1557 infofid, sizeof(info));
David Kilroyc63cdbe2009-06-18 23:21:33 +01001558 if (err)
David Kilroy01632fa2008-08-21 23:27:58 +01001559 kfree(bss);
David Kilroyc63cdbe2009-06-18 23:21:33 +01001560 else
1561 qbuf_scan(priv, bss, len, type);
David Kilroy01632fa2008-08-21 23:27:58 +01001562
David Kilroy01632fa2008-08-21 23:27:58 +01001563 break;
1564 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001565 case HERMES_INQ_SEC_STAT_AGERE:
1566 /* Security status (Agere specific) */
1567 /* Ignore this frame for now */
1568 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
1569 break;
1570 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 default:
1572 printk(KERN_DEBUG "%s: Unknown information frame received: "
1573 "type 0x%04x, length %d\n", dev->name, type, len);
1574 /* We don't actually do anything about it */
1575 break;
1576 }
David Kilroyc63cdbe2009-06-18 23:21:33 +01001577
1578 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
1581static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
1582{
1583 if (net_ratelimit())
1584 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
1585}
1586
1587/********************************************************************/
1588/* Internal hardware control routines */
1589/********************************************************************/
1590
David Kilroy6415f7d2009-06-18 23:21:30 +01001591static int __orinoco_up(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
David Kilroya2608362009-06-18 23:21:23 +01001593 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 struct hermes *hw = &priv->hw;
1595 int err;
1596
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001597 netif_carrier_off(dev); /* just to make sure */
1598
David Kilroy721aa2f2009-06-18 23:21:31 +01001599 err = __orinoco_commit(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (err) {
1601 printk(KERN_ERR "%s: Error %d configuring card\n",
1602 dev->name, err);
1603 return err;
1604 }
1605
1606 /* Fire things up again */
1607 hermes_set_irqmask(hw, ORINOCO_INTEN);
1608 err = hermes_enable_port(hw, 0);
1609 if (err) {
1610 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
1611 dev->name, err);
1612 return err;
1613 }
1614
1615 netif_start_queue(dev);
1616
1617 return 0;
1618}
1619
David Kilroy6415f7d2009-06-18 23:21:30 +01001620static int __orinoco_down(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
David Kilroya2608362009-06-18 23:21:23 +01001622 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 struct hermes *hw = &priv->hw;
1624 int err;
1625
1626 netif_stop_queue(dev);
1627
David Kilroya94e8422009-02-04 23:05:44 +00001628 if (!priv->hw_unavailable) {
1629 if (!priv->broken_disableport) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 err = hermes_disable_port(hw, 0);
1631 if (err) {
1632 /* Some firmwares (e.g. Intersil 1.3.x) seem
1633 * to have problems disabling the port, oh
1634 * well, too bad. */
1635 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
1636 dev->name, err);
1637 priv->broken_disableport = 1;
1638 }
1639 }
1640 hermes_set_irqmask(hw, 0);
1641 hermes_write_regn(hw, EVACK, 0xffff);
1642 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 /* firmware will have to reassociate */
1645 netif_carrier_off(dev);
1646 priv->last_linkstatus = 0xffff;
1647
1648 return 0;
1649}
1650
David Kilroy6415f7d2009-06-18 23:21:30 +01001651static int orinoco_reinit_firmware(struct orinoco_private *priv)
Pavel Roskin37a6c612006-04-07 04:10:49 -04001652{
Pavel Roskin37a6c612006-04-07 04:10:49 -04001653 struct hermes *hw = &priv->hw;
1654 int err;
1655
David Kilroyb42f2072010-05-01 14:05:38 +01001656 err = hw->ops->init(hw);
Andrey Borzenkov0df6cbb2008-10-12 20:15:43 +04001657 if (priv->do_fw_download && !err) {
1658 err = orinoco_download(priv);
1659 if (err)
1660 priv->do_fw_download = 0;
1661 }
Pavel Roskin37a6c612006-04-07 04:10:49 -04001662 if (!err)
David Kilroy42a51b92009-06-18 23:21:20 +01001663 err = orinoco_hw_allocate_fid(priv);
Pavel Roskin37a6c612006-04-07 04:10:49 -04001664
1665 return err;
1666}
1667
David Kilroy721aa2f2009-06-18 23:21:31 +01001668static int
David Kilroy5865d012009-02-04 23:05:54 +00001669__orinoco_set_multicast_list(struct net_device *dev)
1670{
David Kilroyea60a6a2009-06-18 23:21:26 +01001671 struct orinoco_private *priv = ndev_priv(dev);
David Kilroy5865d012009-02-04 23:05:54 +00001672 int err = 0;
1673 int promisc, mc_count;
1674
1675 /* The Hermes doesn't seem to have an allmulti mode, so we go
1676 * into promiscuous mode and let the upper levels deal. */
1677 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001678 (netdev_mc_count(dev) > MAX_MULTICAST(priv))) {
David Kilroy5865d012009-02-04 23:05:54 +00001679 promisc = 1;
1680 mc_count = 0;
1681 } else {
1682 promisc = 0;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001683 mc_count = netdev_mc_count(dev);
David Kilroy5865d012009-02-04 23:05:54 +00001684 }
1685
Jiri Pirko655ffee2010-02-27 07:35:45 +00001686 err = __orinoco_hw_set_multicast_list(priv, dev, mc_count, promisc);
David Kilroy721aa2f2009-06-18 23:21:31 +01001687
1688 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691/* This must be called from user context, without locks held - use
1692 * schedule_work() */
David Kilroycb1576a2009-02-04 23:05:56 +00001693void orinoco_reset(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
David Howellsc4028952006-11-22 14:57:56 +00001695 struct orinoco_private *priv =
1696 container_of(work, struct orinoco_private, reset_work);
1697 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 struct hermes *hw = &priv->hw;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001699 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 unsigned long flags;
1701
1702 if (orinoco_lock(priv, &flags) != 0)
1703 /* When the hardware becomes available again, whatever
1704 * detects that is responsible for re-initializing
1705 * it. So no need for anything further */
1706 return;
1707
1708 netif_stop_queue(dev);
1709
1710 /* Shut off interrupts. Depending on what state the hardware
1711 * is in, this might not work, but we'll try anyway */
1712 hermes_set_irqmask(hw, 0);
1713 hermes_write_regn(hw, EVACK, 0xffff);
1714
1715 priv->hw_unavailable++;
1716 priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
1717 netif_carrier_off(dev);
1718
1719 orinoco_unlock(priv, &flags);
1720
David Kilroyc63cdbe2009-06-18 23:21:33 +01001721 /* Scanning support: Notify scan cancellation */
1722 if (priv->scan_request) {
1723 cfg80211_scan_done(priv->scan_request, 1);
1724 priv->scan_request = NULL;
1725 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001726
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001727 if (priv->hard_reset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 err = (*priv->hard_reset)(priv);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001729 if (err) {
1730 printk(KERN_ERR "%s: orinoco_reset: Error %d "
1731 "performing hard reset\n", dev->name, err);
1732 goto disable;
1733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 }
1735
David Kilroya2608362009-06-18 23:21:23 +01001736 err = orinoco_reinit_firmware(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 if (err) {
1738 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
1739 dev->name, err);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001740 goto disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
1742
David Kilroyb2f30a02009-02-04 23:05:46 +00001743 /* This has to be called from user context */
1744 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 priv->hw_unavailable--;
1747
1748 /* priv->open or priv->hw_unavailable might have changed while
1749 * we dropped the lock */
David Kilroya94e8422009-02-04 23:05:44 +00001750 if (priv->open && (!priv->hw_unavailable)) {
David Kilroya2608362009-06-18 23:21:23 +01001751 err = __orinoco_up(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 if (err) {
1753 printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
1754 dev->name, err);
1755 } else
1756 dev->trans_start = jiffies;
1757 }
1758
1759 spin_unlock_irq(&priv->lock);
1760
1761 return;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001762 disable:
1763 hermes_set_irqmask(hw, 0);
1764 netif_device_detach(dev);
1765 printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766}
1767
David Kilroy721aa2f2009-06-18 23:21:31 +01001768static int __orinoco_commit(struct orinoco_private *priv)
1769{
1770 struct net_device *dev = priv->ndev;
1771 int err = 0;
1772
1773 err = orinoco_hw_program_rids(priv);
1774
1775 /* FIXME: what about netif_tx_lock */
1776 (void) __orinoco_set_multicast_list(dev);
1777
1778 return err;
1779}
1780
1781/* Ensures configuration changes are applied. May result in a reset.
1782 * The caller should hold priv->lock
1783 */
1784int orinoco_commit(struct orinoco_private *priv)
1785{
1786 struct net_device *dev = priv->ndev;
1787 hermes_t *hw = &priv->hw;
1788 int err;
1789
1790 if (priv->broken_disableport) {
1791 schedule_work(&priv->reset_work);
1792 return 0;
1793 }
1794
1795 err = hermes_disable_port(hw, 0);
1796 if (err) {
1797 printk(KERN_WARNING "%s: Unable to disable port "
1798 "while reconfiguring card\n", dev->name);
1799 priv->broken_disableport = 1;
1800 goto out;
1801 }
1802
1803 err = __orinoco_commit(priv);
1804 if (err) {
1805 printk(KERN_WARNING "%s: Unable to reconfigure card\n",
1806 dev->name);
1807 goto out;
1808 }
1809
1810 err = hermes_enable_port(hw, 0);
1811 if (err) {
1812 printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
1813 dev->name);
1814 goto out;
1815 }
1816
1817 out:
1818 if (err) {
1819 printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
1820 schedule_work(&priv->reset_work);
1821 err = 0;
1822 }
1823 return err;
1824}
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826/********************************************************************/
1827/* Interrupt handler */
1828/********************************************************************/
1829
1830static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
1831{
1832 printk(KERN_DEBUG "%s: TICK\n", dev->name);
1833}
1834
1835static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
1836{
1837 /* This seems to happen a fair bit under load, but ignoring it
1838 seems to work fine...*/
1839 printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
1840 dev->name);
1841}
1842
David Howells7d12e782006-10-05 14:55:46 +01001843irqreturn_t orinoco_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
David Kilroya2608362009-06-18 23:21:23 +01001845 struct orinoco_private *priv = dev_id;
1846 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 hermes_t *hw = &priv->hw;
1848 int count = MAX_IRQLOOPS_PER_IRQ;
1849 u16 evstat, events;
David Kilroy21312662009-02-04 23:05:47 +00001850 /* These are used to detect a runaway interrupt situation.
1851 *
1852 * If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
1853 * we panic and shut down the hardware
1854 */
1855 /* jiffies value the last time we were called */
1856 static int last_irq_jiffy; /* = 0 */
1857 static int loops_this_jiffy; /* = 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 unsigned long flags;
1859
1860 if (orinoco_lock(priv, &flags) != 0) {
1861 /* If hw is unavailable - we don't know if the irq was
1862 * for us or not */
1863 return IRQ_HANDLED;
1864 }
1865
1866 evstat = hermes_read_regn(hw, EVSTAT);
1867 events = evstat & hw->inten;
David Kilroya94e8422009-02-04 23:05:44 +00001868 if (!events) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 orinoco_unlock(priv, &flags);
1870 return IRQ_NONE;
1871 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if (jiffies != last_irq_jiffy)
1874 loops_this_jiffy = 0;
1875 last_irq_jiffy = jiffies;
1876
1877 while (events && count--) {
1878 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
1879 printk(KERN_WARNING "%s: IRQ handler is looping too "
1880 "much! Resetting.\n", dev->name);
1881 /* Disable interrupts for now */
1882 hermes_set_irqmask(hw, 0);
1883 schedule_work(&priv->reset_work);
1884 break;
1885 }
1886
1887 /* Check the card hasn't been removed */
David Kilroya94e8422009-02-04 23:05:44 +00001888 if (!hermes_present(hw)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 DEBUG(0, "orinoco_interrupt(): card removed\n");
1890 break;
1891 }
1892
1893 if (events & HERMES_EV_TICK)
1894 __orinoco_ev_tick(dev, hw);
1895 if (events & HERMES_EV_WTERR)
1896 __orinoco_ev_wterr(dev, hw);
1897 if (events & HERMES_EV_INFDROP)
1898 __orinoco_ev_infdrop(dev, hw);
1899 if (events & HERMES_EV_INFO)
1900 __orinoco_ev_info(dev, hw);
1901 if (events & HERMES_EV_RX)
1902 __orinoco_ev_rx(dev, hw);
1903 if (events & HERMES_EV_TXEXC)
1904 __orinoco_ev_txexc(dev, hw);
1905 if (events & HERMES_EV_TX)
1906 __orinoco_ev_tx(dev, hw);
1907 if (events & HERMES_EV_ALLOC)
1908 __orinoco_ev_alloc(dev, hw);
David Kilroy6fe9deb2009-02-04 23:05:43 +00001909
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001910 hermes_write_regn(hw, EVACK, evstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 evstat = hermes_read_regn(hw, EVSTAT);
1913 events = evstat & hw->inten;
1914 };
1915
1916 orinoco_unlock(priv, &flags);
1917 return IRQ_HANDLED;
1918}
David Kilroy21312662009-02-04 23:05:47 +00001919EXPORT_SYMBOL(orinoco_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921/********************************************************************/
David Kilroy39d1ffe2008-11-22 10:37:28 +00001922/* Power management */
1923/********************************************************************/
1924#if defined(CONFIG_PM_SLEEP) && !defined(CONFIG_HERMES_CACHE_FW_ON_INIT)
1925static int orinoco_pm_notifier(struct notifier_block *notifier,
1926 unsigned long pm_event,
1927 void *unused)
1928{
1929 struct orinoco_private *priv = container_of(notifier,
1930 struct orinoco_private,
1931 pm_notifier);
1932
1933 /* All we need to do is cache the firmware before suspend, and
1934 * release it when we come out.
1935 *
1936 * Only need to do this if we're downloading firmware. */
1937 if (!priv->do_fw_download)
1938 return NOTIFY_DONE;
1939
1940 switch (pm_event) {
1941 case PM_HIBERNATION_PREPARE:
1942 case PM_SUSPEND_PREPARE:
1943 orinoco_cache_fw(priv, 0);
1944 break;
1945
1946 case PM_POST_RESTORE:
1947 /* Restore from hibernation failed. We need to clean
1948 * up in exactly the same way, so fall through. */
1949 case PM_POST_HIBERNATION:
1950 case PM_POST_SUSPEND:
1951 orinoco_uncache_fw(priv);
1952 break;
1953
1954 case PM_RESTORE_PREPARE:
1955 default:
1956 break;
1957 }
1958
1959 return NOTIFY_DONE;
1960}
David S. Millerf11c1792009-02-25 00:02:05 -08001961
1962static void orinoco_register_pm_notifier(struct orinoco_private *priv)
1963{
1964 priv->pm_notifier.notifier_call = orinoco_pm_notifier;
1965 register_pm_notifier(&priv->pm_notifier);
1966}
1967
1968static void orinoco_unregister_pm_notifier(struct orinoco_private *priv)
1969{
1970 unregister_pm_notifier(&priv->pm_notifier);
1971}
David Kilroy39d1ffe2008-11-22 10:37:28 +00001972#else /* !PM_SLEEP || HERMES_CACHE_FW_ON_INIT */
David S. Millerf11c1792009-02-25 00:02:05 -08001973#define orinoco_register_pm_notifier(priv) do { } while(0)
1974#define orinoco_unregister_pm_notifier(priv) do { } while(0)
David Kilroy39d1ffe2008-11-22 10:37:28 +00001975#endif
1976
1977/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978/* Initialization */
1979/********************************************************************/
1980
David Kilroy8e638262009-06-18 23:21:24 +01001981int orinoco_init(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
David Kilroy8e638262009-06-18 23:21:24 +01001983 struct device *dev = priv->dev;
David Kilroyea60a6a2009-06-18 23:21:26 +01001984 struct wiphy *wiphy = priv_to_wiphy(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 hermes_t *hw = &priv->hw;
1986 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 /* No need to lock, the hw_unavailable flag is already set in
1989 * alloc_orinocodev() */
Johannes Berg2c7060022008-10-30 22:09:54 +01001990 priv->nicbuf_size = IEEE80211_MAX_FRAME_LEN + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
1992 /* Initialize the firmware */
David Kilroyb42f2072010-05-01 14:05:38 +01001993 err = hw->ops->init(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 if (err != 0) {
David Kilroy8e638262009-06-18 23:21:24 +01001995 dev_err(dev, "Failed to initialize firmware (err = %d)\n",
1996 err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 goto out;
1998 }
1999
David Kilroy3414fc32009-10-07 22:23:32 +01002000 err = determine_fw_capabilities(priv, wiphy->fw_version,
2001 sizeof(wiphy->fw_version),
2002 &wiphy->hw_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (err != 0) {
David Kilroy8e638262009-06-18 23:21:24 +01002004 dev_err(dev, "Incompatible firmware, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 goto out;
2006 }
2007
David Kilroy3994d502008-08-21 23:27:54 +01002008 if (priv->do_fw_download) {
David Kilroy39d1ffe2008-11-22 10:37:28 +00002009#ifdef CONFIG_HERMES_CACHE_FW_ON_INIT
David Kilroy74734312008-11-22 10:37:25 +00002010 orinoco_cache_fw(priv, 0);
David Kilroy39d1ffe2008-11-22 10:37:28 +00002011#endif
David Kilroy74734312008-11-22 10:37:25 +00002012
David Kilroy3994d502008-08-21 23:27:54 +01002013 err = orinoco_download(priv);
2014 if (err)
2015 priv->do_fw_download = 0;
2016
2017 /* Check firmware version again */
David Kilroy3414fc32009-10-07 22:23:32 +01002018 err = determine_fw_capabilities(priv, wiphy->fw_version,
2019 sizeof(wiphy->fw_version),
2020 &wiphy->hw_version);
David Kilroy3994d502008-08-21 23:27:54 +01002021 if (err != 0) {
David Kilroy8e638262009-06-18 23:21:24 +01002022 dev_err(dev, "Incompatible firmware, aborting\n");
David Kilroy3994d502008-08-21 23:27:54 +01002023 goto out;
2024 }
2025 }
2026
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 if (priv->has_port3)
David Kilroy8e638262009-06-18 23:21:24 +01002028 dev_info(dev, "Ad-hoc demo mode supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 if (priv->has_ibss)
David Kilroy8e638262009-06-18 23:21:24 +01002030 dev_info(dev, "IEEE standard IBSS ad-hoc mode supported\n");
2031 if (priv->has_wep)
2032 dev_info(dev, "WEP supported, %s-bit key\n",
2033 priv->has_big_wep ? "104" : "40");
David Kilroy23edcc42008-08-21 23:28:05 +01002034 if (priv->has_wpa) {
David Kilroy8e638262009-06-18 23:21:24 +01002035 dev_info(dev, "WPA-PSK supported\n");
David Kilroy23edcc42008-08-21 23:28:05 +01002036 if (orinoco_mic_init(priv)) {
David Kilroy8e638262009-06-18 23:21:24 +01002037 dev_err(dev, "Failed to setup MIC crypto algorithm. "
2038 "Disabling WPA support\n");
David Kilroy23edcc42008-08-21 23:28:05 +01002039 priv->has_wpa = 0;
2040 }
2041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
David Kilroyea60a6a2009-06-18 23:21:26 +01002043 err = orinoco_hw_read_card_settings(priv, wiphy->perm_addr);
David Kilroye9e3d012009-06-18 23:21:19 +01002044 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
David Kilroy42a51b92009-06-18 23:21:20 +01002047 err = orinoco_hw_allocate_fid(priv);
Pavel Roskin37a6c612006-04-07 04:10:49 -04002048 if (err) {
David Kilroy8e638262009-06-18 23:21:24 +01002049 dev_err(dev, "Failed to allocate NIC buffer!\n");
Pavel Roskin37a6c612006-04-07 04:10:49 -04002050 goto out;
2051 }
2052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 /* Set up the default configuration */
David Kilroy5217c572009-06-18 23:21:32 +01002054 priv->iw_mode = NL80211_IFTYPE_STATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 /* By default use IEEE/IBSS ad-hoc mode if we have it */
David Kilroya94e8422009-02-04 23:05:44 +00002056 priv->prefer_port3 = priv->has_port3 && (!priv->has_ibss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 set_port_type(priv);
David Gibsond51d8b12005-05-12 20:03:36 -04002058 priv->channel = 0; /* use firmware default */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 priv->promiscuous = 0;
David Kilroy5c9f41e2009-08-05 21:23:28 +01002061 priv->encode_alg = ORINOCO_ALG_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 priv->tx_key = 0;
David Kilroyd03032a2008-08-21 23:28:02 +01002063 priv->wpa_enabled = 0;
2064 priv->tkip_cm_active = 0;
2065 priv->key_mgmt = 0;
2066 priv->wpa_ie_len = 0;
2067 priv->wpa_ie = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
David Kilroyea60a6a2009-06-18 23:21:26 +01002069 if (orinoco_wiphy_register(wiphy)) {
2070 err = -ENODEV;
2071 goto out;
2072 }
2073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 /* Make the hardware available, as long as it hasn't been
2075 * removed elsewhere (e.g. by PCMCIA hot unplug) */
2076 spin_lock_irq(&priv->lock);
2077 priv->hw_unavailable--;
2078 spin_unlock_irq(&priv->lock);
2079
David Kilroy8e638262009-06-18 23:21:24 +01002080 dev_dbg(dev, "Ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 return err;
2084}
David Kilroy8e638262009-06-18 23:21:24 +01002085EXPORT_SYMBOL(orinoco_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Andrey Borzenkov89ea4092009-01-21 20:46:46 +03002087static const struct net_device_ops orinoco_netdev_ops = {
Andrey Borzenkov89ea4092009-01-21 20:46:46 +03002088 .ndo_open = orinoco_open,
2089 .ndo_stop = orinoco_stop,
2090 .ndo_start_xmit = orinoco_xmit,
2091 .ndo_set_multicast_list = orinoco_set_multicast_list,
2092 .ndo_change_mtu = orinoco_change_mtu,
Ben Hutchings240c1022009-07-09 17:54:35 +00002093 .ndo_set_mac_address = eth_mac_addr,
2094 .ndo_validate_addr = eth_validate_addr,
Andrey Borzenkov89ea4092009-01-21 20:46:46 +03002095 .ndo_tx_timeout = orinoco_tx_timeout,
2096 .ndo_get_stats = orinoco_get_stats,
2097};
2098
David Kilroyea60a6a2009-06-18 23:21:26 +01002099/* Allocate private data.
2100 *
2101 * This driver has a number of structures associated with it
2102 * netdev - Net device structure for each network interface
2103 * wiphy - structure associated with wireless phy
2104 * wireless_dev (wdev) - structure for each wireless interface
2105 * hw - structure for hermes chip info
2106 * card - card specific structure for use by the card driver
2107 * (airport, orinoco_cs)
2108 * priv - orinoco private data
2109 * device - generic linux device structure
2110 *
2111 * +---------+ +---------+
2112 * | wiphy | | netdev |
2113 * | +-------+ | +-------+
2114 * | | priv | | | wdev |
2115 * | | +-----+ +-+-------+
2116 * | | | hw |
2117 * | +-+-----+
2118 * | | card |
2119 * +-+-------+
2120 *
2121 * priv has a link to netdev and device
2122 * wdev has a link to wiphy
2123 */
David Kilroya2608362009-06-18 23:21:23 +01002124struct orinoco_private
David Kilroy3994d502008-08-21 23:27:54 +01002125*alloc_orinocodev(int sizeof_card,
2126 struct device *device,
2127 int (*hard_reset)(struct orinoco_private *),
2128 int (*stop_fw)(struct orinoco_private *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 struct orinoco_private *priv;
David Kilroyea60a6a2009-06-18 23:21:26 +01002131 struct wiphy *wiphy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
David Kilroyea60a6a2009-06-18 23:21:26 +01002133 /* allocate wiphy
2134 * NOTE: We only support a single virtual interface
2135 * but this may change when monitor mode is added
2136 */
2137 wiphy = wiphy_new(&orinoco_cfg_ops,
2138 sizeof(struct orinoco_private) + sizeof_card);
2139 if (!wiphy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return NULL;
David Kilroyea60a6a2009-06-18 23:21:26 +01002141
David Kilroyea60a6a2009-06-18 23:21:26 +01002142 priv = wiphy_priv(wiphy);
David Kilroy53819562009-06-18 23:21:28 +01002143 priv->dev = device;
David Kilroyea60a6a2009-06-18 23:21:26 +01002144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 if (sizeof_card)
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002146 priv->card = (void *)((unsigned long)priv
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 + sizeof(struct orinoco_private));
2148 else
2149 priv->card = NULL;
2150
David Kilroyea60a6a2009-06-18 23:21:26 +01002151 orinoco_wiphy_init(wiphy);
2152
Pavel Roskin343c6862005-09-09 18:43:02 -04002153#ifdef WIRELESS_SPY
2154 priv->wireless_data.spy_data = &priv->spy_data;
Pavel Roskin343c6862005-09-09 18:43:02 -04002155#endif
David Kilroy23edcc42008-08-21 23:28:05 +01002156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 /* Set up default callbacks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 priv->hard_reset = hard_reset;
David Kilroy3994d502008-08-21 23:27:54 +01002159 priv->stop_fw = stop_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161 spin_lock_init(&priv->lock);
2162 priv->open = 0;
2163 priv->hw_unavailable = 1; /* orinoco_init() must clear this
2164 * before anything else touches the
2165 * hardware */
David Howellsc4028952006-11-22 14:57:56 +00002166 INIT_WORK(&priv->reset_work, orinoco_reset);
2167 INIT_WORK(&priv->join_work, orinoco_join_ap);
2168 INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
David Kilroy31afcef2008-08-21 23:28:04 +01002170 INIT_LIST_HEAD(&priv->rx_list);
2171 tasklet_init(&priv->rx_tasklet, orinoco_rx_isr_tasklet,
David Kilroy53819562009-06-18 23:21:28 +01002172 (unsigned long) priv);
David Kilroy31afcef2008-08-21 23:28:04 +01002173
David Kilroyc63cdbe2009-06-18 23:21:33 +01002174 spin_lock_init(&priv->scan_lock);
2175 INIT_LIST_HEAD(&priv->scan_list);
2176 INIT_WORK(&priv->process_scan, orinoco_process_scan_results);
2177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 priv->last_linkstatus = 0xffff;
2179
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +03002180#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
David Kilroy2cea7b22008-11-22 10:37:26 +00002181 priv->cached_pri_fw = NULL;
Andrey Borzenkov4fb30782008-10-19 12:06:11 +04002182 priv->cached_fw = NULL;
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +03002183#endif
Andrey Borzenkov4fb30782008-10-19 12:06:11 +04002184
David Kilroy39d1ffe2008-11-22 10:37:28 +00002185 /* Register PM notifiers */
David S. Millerf11c1792009-02-25 00:02:05 -08002186 orinoco_register_pm_notifier(priv);
David Kilroy39d1ffe2008-11-22 10:37:28 +00002187
David Kilroya2608362009-06-18 23:21:23 +01002188 return priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189}
David Kilroy21312662009-02-04 23:05:47 +00002190EXPORT_SYMBOL(alloc_orinocodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
David Kilroy53819562009-06-18 23:21:28 +01002192/* We can only support a single interface. We provide a separate
2193 * function to set it up to distinguish between hardware
2194 * initialisation and interface setup.
2195 *
2196 * The base_addr and irq parameters are passed on to netdev for use
2197 * with SIOCGIFMAP.
2198 */
2199int orinoco_if_add(struct orinoco_private *priv,
2200 unsigned long base_addr,
David Kilroy593ef092010-05-01 14:05:39 +01002201 unsigned int irq,
2202 const struct net_device_ops *ops)
David Kilroy53819562009-06-18 23:21:28 +01002203{
2204 struct wiphy *wiphy = priv_to_wiphy(priv);
2205 struct wireless_dev *wdev;
2206 struct net_device *dev;
2207 int ret;
2208
2209 dev = alloc_etherdev(sizeof(struct wireless_dev));
2210
2211 if (!dev)
2212 return -ENOMEM;
2213
2214 /* Initialise wireless_dev */
2215 wdev = netdev_priv(dev);
2216 wdev->wiphy = wiphy;
2217 wdev->iftype = NL80211_IFTYPE_STATION;
2218
2219 /* Setup / override net_device fields */
2220 dev->ieee80211_ptr = wdev;
David Kilroy53819562009-06-18 23:21:28 +01002221 dev->watchdog_timeo = HZ; /* 1 second timeout */
David Kilroy53819562009-06-18 23:21:28 +01002222 dev->wireless_handlers = &orinoco_handler_def;
2223#ifdef WIRELESS_SPY
2224 dev->wireless_data = &priv->wireless_data;
2225#endif
David Kilroy593ef092010-05-01 14:05:39 +01002226 /* Default to standard ops if not set */
2227 if (ops)
2228 dev->netdev_ops = ops;
2229 else
2230 dev->netdev_ops = &orinoco_netdev_ops;
2231
David Kilroy53819562009-06-18 23:21:28 +01002232 /* we use the default eth_mac_addr for setting the MAC addr */
2233
2234 /* Reserve space in skb for the SNAP header */
2235 dev->hard_header_len += ENCAPS_OVERHEAD;
2236
2237 netif_carrier_off(dev);
2238
2239 memcpy(dev->dev_addr, wiphy->perm_addr, ETH_ALEN);
John W. Linvillecf32ed92009-10-06 16:47:23 -04002240 memcpy(dev->perm_addr, wiphy->perm_addr, ETH_ALEN);
David Kilroy53819562009-06-18 23:21:28 +01002241
2242 dev->base_addr = base_addr;
2243 dev->irq = irq;
2244
2245 SET_NETDEV_DEV(dev, priv->dev);
2246 ret = register_netdev(dev);
2247 if (ret)
2248 goto fail;
2249
2250 priv->ndev = dev;
2251
2252 /* Report what we've done */
2253 dev_dbg(priv->dev, "Registerred interface %s.\n", dev->name);
2254
2255 return 0;
2256
2257 fail:
2258 free_netdev(dev);
2259 return ret;
2260}
2261EXPORT_SYMBOL(orinoco_if_add);
2262
2263void orinoco_if_del(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
David Kilroya2608362009-06-18 23:21:23 +01002265 struct net_device *dev = priv->ndev;
David Kilroy53819562009-06-18 23:21:28 +01002266
2267 unregister_netdev(dev);
2268 free_netdev(dev);
2269}
2270EXPORT_SYMBOL(orinoco_if_del);
2271
2272void free_orinocodev(struct orinoco_private *priv)
2273{
David Kilroyea60a6a2009-06-18 23:21:26 +01002274 struct wiphy *wiphy = priv_to_wiphy(priv);
David Kilroy20953ad2009-01-07 00:23:55 +00002275 struct orinoco_rx_data *rx_data, *temp;
David Kilroyc63cdbe2009-06-18 23:21:33 +01002276 struct orinoco_scan_data *sd, *sdtemp;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002277
David Kilroyea60a6a2009-06-18 23:21:26 +01002278 wiphy_unregister(wiphy);
2279
David Kilroy20953ad2009-01-07 00:23:55 +00002280 /* If the tasklet is scheduled when we call tasklet_kill it
2281 * will run one final time. However the tasklet will only
2282 * drain priv->rx_list if the hw is still available. */
David Kilroy31afcef2008-08-21 23:28:04 +01002283 tasklet_kill(&priv->rx_tasklet);
David Kilroy74734312008-11-22 10:37:25 +00002284
David Kilroy20953ad2009-01-07 00:23:55 +00002285 /* Explicitly drain priv->rx_list */
2286 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
2287 list_del(&rx_data->list);
2288
2289 dev_kfree_skb(rx_data->skb);
2290 kfree(rx_data->desc);
2291 kfree(rx_data);
2292 }
2293
David Kilroyc63cdbe2009-06-18 23:21:33 +01002294 cancel_work_sync(&priv->process_scan);
2295 /* Explicitly drain priv->scan_list */
2296 list_for_each_entry_safe(sd, sdtemp, &priv->scan_list, list) {
2297 list_del(&sd->list);
2298
2299 if ((sd->len > 0) && sd->buf)
2300 kfree(sd->buf);
2301 kfree(sd);
2302 }
2303
David S. Millerf11c1792009-02-25 00:02:05 -08002304 orinoco_unregister_pm_notifier(priv);
David Kilroy74734312008-11-22 10:37:25 +00002305 orinoco_uncache_fw(priv);
2306
David Kilroyd03032a2008-08-21 23:28:02 +01002307 priv->wpa_ie_len = 0;
2308 kfree(priv->wpa_ie);
David Kilroy23edcc42008-08-21 23:28:05 +01002309 orinoco_mic_free(priv);
David Kilroyea60a6a2009-06-18 23:21:26 +01002310 wiphy_free(wiphy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311}
David Kilroy21312662009-02-04 23:05:47 +00002312EXPORT_SYMBOL(free_orinocodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
David Kilroy6415f7d2009-06-18 23:21:30 +01002314int orinoco_up(struct orinoco_private *priv)
2315{
2316 struct net_device *dev = priv->ndev;
2317 unsigned long flags;
2318 int err;
2319
2320 spin_lock_irqsave(&priv->lock, flags);
2321
2322 err = orinoco_reinit_firmware(priv);
2323 if (err) {
2324 printk(KERN_ERR "%s: Error %d re-initializing firmware\n",
2325 dev->name, err);
2326 goto exit;
2327 }
2328
2329 netif_device_attach(dev);
2330 priv->hw_unavailable--;
2331
2332 if (priv->open && !priv->hw_unavailable) {
2333 err = __orinoco_up(priv);
2334 if (err)
2335 printk(KERN_ERR "%s: Error %d restarting card\n",
2336 dev->name, err);
2337 }
2338
2339exit:
2340 spin_unlock_irqrestore(&priv->lock, flags);
2341
2342 return 0;
2343}
2344EXPORT_SYMBOL(orinoco_up);
2345
2346void orinoco_down(struct orinoco_private *priv)
2347{
2348 struct net_device *dev = priv->ndev;
2349 unsigned long flags;
2350 int err;
2351
2352 spin_lock_irqsave(&priv->lock, flags);
2353 err = __orinoco_down(priv);
2354 if (err)
2355 printk(KERN_WARNING "%s: Error %d downing interface\n",
2356 dev->name, err);
2357
2358 netif_device_detach(dev);
2359 priv->hw_unavailable++;
2360 spin_unlock_irqrestore(&priv->lock, flags);
2361}
2362EXPORT_SYMBOL(orinoco_down);
2363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365/* Module initialization */
2366/********************************************************************/
2367
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368/* Can't be declared "const" or the whole __initdata section will
2369 * become const */
2370static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
2371 " (David Gibson <hermes@gibson.dropbear.id.au>, "
2372 "Pavel Roskin <proski@gnu.org>, et al)";
2373
2374static int __init init_orinoco(void)
2375{
2376 printk(KERN_DEBUG "%s\n", version);
2377 return 0;
2378}
2379
2380static void __exit exit_orinoco(void)
2381{
2382}
2383
2384module_init(init_orinoco);
2385module_exit(exit_orinoco);