blob: 361cf09d6b70bc775780826fbb0a336d54dbec43 [file] [log] [blame]
David Kilroy47445cb2009-02-04 23:05:48 +00001/* main.c - (formerly known as dldwd_cs.c, orinoco_cs.c and orinoco.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * A driver for Hermes or Prism 2 chipset based PCMCIA wireless
4 * adaptors, with Lucent/Agere, Intersil or Symbol firmware.
5 *
6 * Current maintainers (as of 29 September 2003) are:
7 * Pavel Roskin <proski AT gnu.org>
8 * and David Gibson <hermes AT gibson.dropbear.id.au>
9 *
10 * (C) Copyright David Gibson, IBM Corporation 2001-2003.
11 * Copyright (C) 2000 David Gibson, Linuxcare Australia.
12 * With some help from :
13 * Copyright (C) 2001 Jean Tourrilhes, HP Labs
14 * Copyright (C) 2001 Benjamin Herrenschmidt
15 *
16 * Based on dummy_cs.c 1.27 2000/06/12 21:27:25
17 *
18 * Portions based on wvlan_cs.c 1.0.6, Copyright Andreas Neuhaus <andy
19 * AT fasta.fh-dortmund.de>
20 * http://www.stud.fh-dortmund.de/~andy/wvlan/
21 *
22 * The contents of this file are subject to the Mozilla Public License
23 * Version 1.1 (the "License"); you may not use this file except in
24 * compliance with the License. You may obtain a copy of the License
25 * at http://www.mozilla.org/MPL/
26 *
27 * Software distributed under the License is distributed on an "AS IS"
28 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
29 * the License for the specific language governing rights and
30 * limitations under the License.
31 *
32 * The initial developer of the original code is David A. Hinds
33 * <dahinds AT users.sourceforge.net>. Portions created by David
34 * A. Hinds are Copyright (C) 1999 David A. Hinds. All Rights
35 * Reserved.
36 *
37 * Alternatively, the contents of this file may be used under the
38 * terms of the GNU General Public License version 2 (the "GPL"), in
39 * which case the provisions of the GPL are applicable instead of the
40 * above. If you wish to allow the use of your version of this file
41 * only under the terms of the GPL and not to allow others to use your
42 * version of this file under the MPL, indicate your decision by
43 * deleting the provisions above and replace them with the notice and
44 * other provisions required by the GPL. If you do not delete the
45 * provisions above, a recipient may use your version of this file
46 * under either the MPL or the GPL. */
47
48/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * TODO
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * o Handle de-encapsulation within network layer, provide 802.11
51 * headers (patch from Thomas 'Dent' Mirlacher)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * o Fix possible races in SPY handling.
53 * o Disconnect wireless extensions from fundamental configuration.
54 * o (maybe) Software WEP support (patch from Stano Meduna).
55 * o (maybe) Use multiple Tx buffers - driver handling queue
56 * rather than firmware.
57 */
58
59/* Locking and synchronization:
60 *
61 * The basic principle is that everything is serialized through a
62 * single spinlock, priv->lock. The lock is used in user, bh and irq
63 * context, so when taken outside hardirq context it should always be
64 * taken with interrupts disabled. The lock protects both the
65 * hardware and the struct orinoco_private.
66 *
67 * Another flag, priv->hw_unavailable indicates that the hardware is
68 * unavailable for an extended period of time (e.g. suspended, or in
69 * the middle of a hard reset). This flag is protected by the
70 * spinlock. All code which touches the hardware should check the
71 * flag after taking the lock, and if it is set, give up on whatever
72 * they are doing and drop the lock again. The orinoco_lock()
73 * function handles this (it unlocks and returns -EBUSY if
74 * hw_unavailable is non-zero).
75 */
76
77#define DRIVER_NAME "orinoco"
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/module.h>
80#include <linux/kernel.h>
81#include <linux/init.h>
David Kilroyd03032a2008-08-21 23:28:02 +010082#include <linux/delay.h>
David Kilroy8e638262009-06-18 23:21:24 +010083#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#include <linux/etherdevice.h>
Christoph Hellwig1fab2e82005-06-19 01:27:40 +020086#include <linux/ethtool.h>
David Kilroy39d1ffe2008-11-22 10:37:28 +000087#include <linux/suspend.h>
Pavel Roskin9c974fb2006-08-15 20:45:03 -040088#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <linux/wireless.h>
Johannes Berg2c7060022008-10-30 22:09:54 +010090#include <linux/ieee80211.h>
Christoph Hellwig620554e2005-06-19 01:27:33 +020091#include <net/iw_handler.h>
David Kilroyea60a6a2009-06-18 23:21:26 +010092#include <net/cfg80211.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#include "hermes_rid.h"
David Kilroy3994d502008-08-21 23:27:54 +010095#include "hermes_dld.h"
David Kilroy712a4342009-02-04 23:05:55 +000096#include "hw.h"
David Kilroyfb791b12009-02-04 23:05:50 +000097#include "scan.h"
David Kilroy4adb4742009-02-04 23:05:51 +000098#include "mic.h"
David Kilroy37a2e562009-02-04 23:05:52 +000099#include "fw.h"
David Kilroycb1576a2009-02-04 23:05:56 +0000100#include "wext.h"
David Kilroyea60a6a2009-06-18 23:21:26 +0100101#include "cfg.h"
David Kilroycb1576a2009-02-04 23:05:56 +0000102#include "main.h"
David Kilroyfb791b12009-02-04 23:05:50 +0000103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#include "orinoco.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/********************************************************************/
107/* Module information */
108/********************************************************************/
109
David Kilroyb2f30a02009-02-04 23:05:46 +0000110MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & "
111 "David Gibson <hermes@gibson.dropbear.id.au>");
112MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based "
113 "and similar wireless cards");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114MODULE_LICENSE("Dual MPL/GPL");
115
116/* Level of debugging. Used in the macros in orinoco.h */
117#ifdef ORINOCO_DEBUG
118int orinoco_debug = ORINOCO_DEBUG;
David Kilroy21312662009-02-04 23:05:47 +0000119EXPORT_SYMBOL(orinoco_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120module_param(orinoco_debug, int, 0644);
121MODULE_PARM_DESC(orinoco_debug, "Debug level");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#endif
123
124static int suppress_linkstatus; /* = 0 */
125module_param(suppress_linkstatus, bool, 0644);
126MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
David Kilroyb2f30a02009-02-04 23:05:46 +0000127
David Gibson7bb7c3a2005-05-12 20:02:10 -0400128static int ignore_disconnect; /* = 0 */
129module_param(ignore_disconnect, int, 0644);
David Kilroyb2f30a02009-02-04 23:05:46 +0000130MODULE_PARM_DESC(ignore_disconnect,
131 "Don't report lost link to the network layer");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
David Kilroycb1576a2009-02-04 23:05:56 +0000133int force_monitor; /* = 0 */
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200134module_param(force_monitor, int, 0644);
135MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/* Internal constants */
139/********************************************************************/
140
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200141/* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
142static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
143#define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define ORINOCO_MIN_MTU 256
Johannes Berg2c7060022008-10-30 22:09:54 +0100146#define ORINOCO_MAX_MTU (IEEE80211_MAX_DATA_LEN - ENCAPS_OVERHEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#define MAX_IRQLOOPS_PER_IRQ 10
149#define MAX_IRQLOOPS_PER_JIFFY (20000/HZ) /* Based on a guestimate of
150 * how many events the
151 * device could
152 * legitimately generate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154#define DUMMY_FID 0xFFFF
155
156/*#define MAX_MULTICAST(priv) (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
157 HERMES_MAX_MULTICAST : 0)*/
158#define MAX_MULTICAST(priv) (HERMES_MAX_MULTICAST)
159
160#define ORINOCO_INTEN (HERMES_EV_RX | HERMES_EV_ALLOC \
161 | HERMES_EV_TX | HERMES_EV_TXEXC \
162 | HERMES_EV_WTERR | HERMES_EV_INFO \
David Kilroya94e8422009-02-04 23:05:44 +0000163 | HERMES_EV_INFDROP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Jeff Garzik7282d492006-09-13 14:30:00 -0400165static const struct ethtool_ops orinoco_ethtool_ops;
Christoph Hellwig620554e2005-06-19 01:27:33 +0200166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/* Data types */
169/********************************************************************/
170
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400171/* Beginning of the Tx descriptor, used in TxExc handling */
172struct hermes_txexc_data {
173 struct hermes_tx_descriptor desc;
Pavel Roskind133ae42005-09-23 04:18:06 -0400174 __le16 frame_ctl;
175 __le16 duration_id;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200176 u8 addr1[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177} __attribute__ ((packed));
178
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200179/* Rx frame header except compatibility 802.3 header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180struct hermes_rx_descriptor {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200181 /* Control */
Pavel Roskind133ae42005-09-23 04:18:06 -0400182 __le16 status;
183 __le32 time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 u8 silence;
185 u8 signal;
186 u8 rate;
187 u8 rxflow;
Pavel Roskind133ae42005-09-23 04:18:06 -0400188 __le32 reserved;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200189
190 /* 802.11 header */
Pavel Roskind133ae42005-09-23 04:18:06 -0400191 __le16 frame_ctl;
192 __le16 duration_id;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200193 u8 addr1[ETH_ALEN];
194 u8 addr2[ETH_ALEN];
195 u8 addr3[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -0400196 __le16 seq_ctl;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200197 u8 addr4[ETH_ALEN];
198
199 /* Data length */
Pavel Roskind133ae42005-09-23 04:18:06 -0400200 __le16 data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201} __attribute__ ((packed));
202
David Kilroy47166792009-01-07 00:43:54 +0000203struct orinoco_rx_data {
204 struct hermes_rx_descriptor *desc;
205 struct sk_buff *skb;
206 struct list_head list;
207};
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/********************************************************************/
210/* Function prototypes */
211/********************************************************************/
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213static void __orinoco_set_multicast_list(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215/********************************************************************/
216/* Internal helper functions */
217/********************************************************************/
218
David Kilroycb1576a2009-02-04 23:05:56 +0000219void set_port_type(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 switch (priv->iw_mode) {
222 case IW_MODE_INFRA:
223 priv->port_type = 1;
224 priv->createibss = 0;
225 break;
226 case IW_MODE_ADHOC:
227 if (priv->prefer_port3) {
228 priv->port_type = 3;
229 priv->createibss = 0;
230 } else {
231 priv->port_type = priv->ibss_port;
232 priv->createibss = 1;
233 }
234 break;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200235 case IW_MODE_MONITOR:
236 priv->port_type = 3;
237 priv->createibss = 0;
238 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 default:
240 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
241 priv->ndev->name);
242 }
243}
244
David Kilroy3994d502008-08-21 23:27:54 +0100245/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246/* Device methods */
247/********************************************************************/
248
249static int orinoco_open(struct net_device *dev)
250{
David Kilroyea60a6a2009-06-18 23:21:26 +0100251 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 unsigned long flags;
253 int err;
254
255 if (orinoco_lock(priv, &flags) != 0)
256 return -EBUSY;
257
David Kilroya2608362009-06-18 23:21:23 +0100258 err = __orinoco_up(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
David Kilroya94e8422009-02-04 23:05:44 +0000260 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 priv->open = 1;
262
263 orinoco_unlock(priv, &flags);
264
265 return err;
266}
267
Christoph Hellwigad8f4512005-05-14 17:30:17 +0200268static int orinoco_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
David Kilroyea60a6a2009-06-18 23:21:26 +0100270 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 int err = 0;
272
273 /* We mustn't use orinoco_lock() here, because we need to be
274 able to close the interface even if hw_unavailable is set
275 (e.g. as we're released after a PC Card removal) */
276 spin_lock_irq(&priv->lock);
277
278 priv->open = 0;
279
David Kilroya2608362009-06-18 23:21:23 +0100280 err = __orinoco_down(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 spin_unlock_irq(&priv->lock);
283
284 return err;
285}
286
287static struct net_device_stats *orinoco_get_stats(struct net_device *dev)
288{
David Kilroyea60a6a2009-06-18 23:21:26 +0100289 struct orinoco_private *priv = ndev_priv(dev);
David Kilroy6fe9deb2009-02-04 23:05:43 +0000290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return &priv->stats;
292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294static void orinoco_set_multicast_list(struct net_device *dev)
295{
David Kilroyea60a6a2009-06-18 23:21:26 +0100296 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 unsigned long flags;
298
299 if (orinoco_lock(priv, &flags) != 0) {
300 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
301 "called when hw_unavailable\n", dev->name);
302 return;
303 }
304
305 __orinoco_set_multicast_list(dev);
306 orinoco_unlock(priv, &flags);
307}
308
309static int orinoco_change_mtu(struct net_device *dev, int new_mtu)
310{
David Kilroyea60a6a2009-06-18 23:21:26 +0100311 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
David Kilroya94e8422009-02-04 23:05:44 +0000313 if ((new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return -EINVAL;
315
Johannes Berg2c7060022008-10-30 22:09:54 +0100316 /* MTU + encapsulation + header length */
David Kilroya94e8422009-02-04 23:05:44 +0000317 if ((new_mtu + ENCAPS_OVERHEAD + sizeof(struct ieee80211_hdr)) >
318 (priv->nicbuf_size - ETH_HLEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return -EINVAL;
320
321 dev->mtu = new_mtu;
322
323 return 0;
324}
325
326/********************************************************************/
327/* Tx path */
328/********************************************************************/
329
330static int orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
331{
David Kilroyea60a6a2009-06-18 23:21:26 +0100332 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 struct net_device_stats *stats = &priv->stats;
334 hermes_t *hw = &priv->hw;
335 int err = 0;
336 u16 txfid = priv->txfid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 struct ethhdr *eh;
David Kilroy6eecad72008-08-21 23:27:56 +0100338 int tx_control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 unsigned long flags;
340
David Kilroya94e8422009-02-04 23:05:44 +0000341 if (!netif_running(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 printk(KERN_ERR "%s: Tx on stopped device!\n",
343 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400344 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
David Kilroy6fe9deb2009-02-04 23:05:43 +0000346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (netif_queue_stopped(dev)) {
David Kilroy6fe9deb2009-02-04 23:05:43 +0000348 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400350 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
David Kilroy6fe9deb2009-02-04 23:05:43 +0000352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (orinoco_lock(priv, &flags) != 0) {
354 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
355 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400356 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358
David Kilroya94e8422009-02-04 23:05:44 +0000359 if (!netif_carrier_ok(dev) || (priv->iw_mode == IW_MODE_MONITOR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /* Oops, the firmware hasn't established a connection,
David Kilroy6fe9deb2009-02-04 23:05:43 +0000361 silently drop the packet (this seems to be the
362 safest approach). */
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400363 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365
Pavel Roskin8d5be082006-04-07 04:10:41 -0400366 /* Check packet length */
Pavel Roskina28dc812006-04-07 04:10:45 -0400367 if (skb->len < ETH_HLEN)
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400368 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
David Kilroy6eecad72008-08-21 23:27:56 +0100370 tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
David Kilroy23edcc42008-08-21 23:28:05 +0100372 if (priv->encode_alg == IW_ENCODE_ALG_TKIP)
373 tx_control |= (priv->tx_key << HERMES_MIC_KEY_ID_SHIFT) |
374 HERMES_TXCTRL_MIC;
375
David Kilroy6eecad72008-08-21 23:27:56 +0100376 if (priv->has_alt_txcntl) {
377 /* WPA enabled firmwares have tx_cntl at the end of
378 * the 802.11 header. So write zeroed descriptor and
379 * 802.11 header at the same time
380 */
381 char desc[HERMES_802_3_OFFSET];
382 __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
383
384 memset(&desc, 0, sizeof(desc));
385
386 *txcntl = cpu_to_le16(tx_control);
387 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
388 txfid, 0);
389 if (err) {
390 if (net_ratelimit())
391 printk(KERN_ERR "%s: Error %d writing Tx "
392 "descriptor to BAP\n", dev->name, err);
393 goto busy;
394 }
395 } else {
396 struct hermes_tx_descriptor desc;
397
398 memset(&desc, 0, sizeof(desc));
399
400 desc.tx_control = cpu_to_le16(tx_control);
401 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
402 txfid, 0);
403 if (err) {
404 if (net_ratelimit())
405 printk(KERN_ERR "%s: Error %d writing Tx "
406 "descriptor to BAP\n", dev->name, err);
407 goto busy;
408 }
409
410 /* Clear the 802.11 header and data length fields - some
411 * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
412 * if this isn't done. */
413 hermes_clear_words(hw, HERMES_DATA0,
414 HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
David Kilroy23edcc42008-08-21 23:28:05 +0100417 eh = (struct ethhdr *)skb->data;
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /* Encapsulate Ethernet-II frames */
420 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
Pavel Roskina28dc812006-04-07 04:10:45 -0400421 struct header_struct {
422 struct ethhdr eth; /* 802.3 header */
423 u8 encap[6]; /* 802.2 header */
424 } __attribute__ ((packed)) hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Pavel Roskina28dc812006-04-07 04:10:45 -0400426 /* Strip destination and source from the data */
427 skb_pull(skb, 2 * ETH_ALEN);
Pavel Roskina28dc812006-04-07 04:10:45 -0400428
429 /* And move them to a separate header */
430 memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
431 hdr.eth.h_proto = htons(sizeof(encaps_hdr) + skb->len);
432 memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
433
David Kilroy23edcc42008-08-21 23:28:05 +0100434 /* Insert the SNAP header */
435 if (skb_headroom(skb) < sizeof(hdr)) {
436 printk(KERN_ERR
437 "%s: Not enough headroom for 802.2 headers %d\n",
438 dev->name, skb_headroom(skb));
439 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
David Kilroy23edcc42008-08-21 23:28:05 +0100441 eh = (struct ethhdr *) skb_push(skb, sizeof(hdr));
442 memcpy(eh, &hdr, sizeof(hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
Pavel Roskina28dc812006-04-07 04:10:45 -0400445 err = hermes_bap_pwrite(hw, USER_BAP, skb->data, skb->len,
David Kilroy23edcc42008-08-21 23:28:05 +0100446 txfid, HERMES_802_3_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (err) {
448 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
449 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400450 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452
David Kilroy23edcc42008-08-21 23:28:05 +0100453 /* Calculate Michael MIC */
454 if (priv->encode_alg == IW_ENCODE_ALG_TKIP) {
455 u8 mic_buf[MICHAEL_MIC_LEN + 1];
456 u8 *mic;
457 size_t offset;
458 size_t len;
459
460 if (skb->len % 2) {
461 /* MIC start is on an odd boundary */
462 mic_buf[0] = skb->data[skb->len - 1];
463 mic = &mic_buf[1];
464 offset = skb->len - 1;
465 len = MICHAEL_MIC_LEN + 1;
466 } else {
467 mic = &mic_buf[0];
468 offset = skb->len;
469 len = MICHAEL_MIC_LEN;
470 }
471
David Kilroy4adb4742009-02-04 23:05:51 +0000472 orinoco_mic(priv->tx_tfm_mic,
David Kilroy23edcc42008-08-21 23:28:05 +0100473 priv->tkip_key[priv->tx_key].tx_mic,
474 eh->h_dest, eh->h_source, 0 /* priority */,
475 skb->data + ETH_HLEN, skb->len - ETH_HLEN, mic);
476
477 /* Write the MIC */
478 err = hermes_bap_pwrite(hw, USER_BAP, &mic_buf[0], len,
479 txfid, HERMES_802_3_OFFSET + offset);
480 if (err) {
481 printk(KERN_ERR "%s: Error %d writing MIC to BAP\n",
482 dev->name, err);
483 goto busy;
484 }
485 }
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 /* Finally, we actually initiate the send */
488 netif_stop_queue(dev);
489
490 err = hermes_docmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
491 txfid, NULL);
492 if (err) {
493 netif_start_queue(dev);
Andrew Mortonc367c212005-10-19 21:23:44 -0700494 if (net_ratelimit())
495 printk(KERN_ERR "%s: Error %d transmitting packet\n",
496 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400497 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499
500 dev->trans_start = jiffies;
David Kilroy23edcc42008-08-21 23:28:05 +0100501 stats->tx_bytes += HERMES_802_3_OFFSET + skb->len;
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400502 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400504 drop:
505 stats->tx_errors++;
506 stats->tx_dropped++;
507
508 ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 dev_kfree_skb(skb);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400511 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400513 busy:
Jiri Benc2c1bd262006-04-07 04:10:47 -0400514 if (err == -EIO)
515 schedule_work(&priv->reset_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 orinoco_unlock(priv, &flags);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400517 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
520static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
521{
David Kilroyea60a6a2009-06-18 23:21:26 +0100522 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 u16 fid = hermes_read_regn(hw, ALLOCFID);
524
525 if (fid != priv->txfid) {
526 if (fid != DUMMY_FID)
527 printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
528 dev->name, fid);
529 return;
530 }
531
532 hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
533}
534
535static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
536{
David Kilroyea60a6a2009-06-18 23:21:26 +0100537 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 struct net_device_stats *stats = &priv->stats;
539
540 stats->tx_packets++;
541
542 netif_wake_queue(dev);
543
544 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
545}
546
547static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
548{
David Kilroyea60a6a2009-06-18 23:21:26 +0100549 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 struct net_device_stats *stats = &priv->stats;
551 u16 fid = hermes_read_regn(hw, TXCOMPLFID);
Pavel Roskind133ae42005-09-23 04:18:06 -0400552 u16 status;
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400553 struct hermes_txexc_data hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 int err = 0;
555
556 if (fid == DUMMY_FID)
557 return; /* Nothing's really happened */
558
Pavel Roskin48ca7032005-09-23 04:18:06 -0400559 /* Read part of the frame header - we need status and addr1 */
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200560 err = hermes_bap_pread(hw, IRQ_BAP, &hdr,
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400561 sizeof(struct hermes_txexc_data),
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200562 fid, 0);
563
564 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
565 stats->tx_errors++;
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (err) {
568 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
569 "(FID=%04X error %d)\n",
570 dev->name, fid, err);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200571 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
David Kilroy6fe9deb2009-02-04 23:05:43 +0000573
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200574 DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
575 err, fid);
David Kilroy6fe9deb2009-02-04 23:05:43 +0000576
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200577 /* We produce a TXDROP event only for retry or lifetime
578 * exceeded, because that's the only status that really mean
579 * that this particular node went away.
580 * Other errors means that *we* screwed up. - Jean II */
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400581 status = le16_to_cpu(hdr.desc.status);
Pavel Roskind133ae42005-09-23 04:18:06 -0400582 if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200583 union iwreq_data wrqu;
584
585 /* Copy 802.11 dest address.
586 * We use the 802.11 header because the frame may
587 * not be 802.3 or may be mangled...
588 * In Ad-Hoc mode, it will be the node address.
589 * In managed mode, it will be most likely the AP addr
590 * User space will figure out how to convert it to
591 * whatever it needs (IP address or else).
592 * - Jean II */
593 memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
594 wrqu.addr.sa_family = ARPHRD_ETHER;
595
596 /* Send event to user space */
597 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603static void orinoco_tx_timeout(struct net_device *dev)
604{
David Kilroyea60a6a2009-06-18 23:21:26 +0100605 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 struct net_device_stats *stats = &priv->stats;
607 struct hermes *hw = &priv->hw;
608
609 printk(KERN_WARNING "%s: Tx timeout! "
610 "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
611 dev->name, hermes_read_regn(hw, ALLOCFID),
612 hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
613
614 stats->tx_errors++;
615
616 schedule_work(&priv->reset_work);
617}
618
619/********************************************************************/
620/* Rx path (data frames) */
621/********************************************************************/
622
623/* Does the frame have a SNAP header indicating it should be
624 * de-encapsulated to Ethernet-II? */
625static inline int is_ethersnap(void *_hdr)
626{
627 u8 *hdr = _hdr;
628
629 /* We de-encapsulate all packets which, a) have SNAP headers
630 * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
631 * and where b) the OUI of the SNAP header is 00:00:00 or
632 * 00:00:f8 - we need both because different APs appear to use
633 * different OUIs for some reason */
634 return (memcmp(hdr, &encaps_hdr, 5) == 0)
David Kilroya94e8422009-02-04 23:05:44 +0000635 && ((hdr[5] == 0x00) || (hdr[5] == 0xf8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
638static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
639 int level, int noise)
640{
Pavel Roskin343c6862005-09-09 18:43:02 -0400641 struct iw_quality wstats;
642 wstats.level = level - 0x95;
643 wstats.noise = noise - 0x95;
644 wstats.qual = (level > noise) ? (level - noise) : 0;
Andrey Borzenkovf941f852008-11-15 17:15:09 +0300645 wstats.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
Pavel Roskin343c6862005-09-09 18:43:02 -0400646 /* Update spy records */
647 wireless_spy_update(dev, mac, &wstats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
650static void orinoco_stat_gather(struct net_device *dev,
651 struct sk_buff *skb,
652 struct hermes_rx_descriptor *desc)
653{
David Kilroyea60a6a2009-06-18 23:21:26 +0100654 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 /* Using spy support with lots of Rx packets, like in an
657 * infrastructure (AP), will really slow down everything, because
658 * the MAC address must be compared to each entry of the spy list.
659 * If the user really asks for it (set some address in the
660 * spy list), we do it, but he will pay the price.
661 * Note that to get here, you need both WIRELESS_SPY
662 * compiled in AND some addresses in the list !!!
663 */
664 /* Note : gcc will optimise the whole section away if
665 * WIRELESS_SPY is not defined... - Jean II */
666 if (SPY_NUMBER(priv)) {
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700667 orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 desc->signal, desc->silence);
669 }
670}
671
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200672/*
673 * orinoco_rx_monitor - handle received monitor frames.
674 *
675 * Arguments:
676 * dev network device
677 * rxfid received FID
678 * desc rx descriptor of the frame
679 *
680 * Call context: interrupt
681 */
682static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
683 struct hermes_rx_descriptor *desc)
684{
685 u32 hdrlen = 30; /* return full header by default */
686 u32 datalen = 0;
687 u16 fc;
688 int err;
689 int len;
690 struct sk_buff *skb;
David Kilroyea60a6a2009-06-18 23:21:26 +0100691 struct orinoco_private *priv = ndev_priv(dev);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200692 struct net_device_stats *stats = &priv->stats;
693 hermes_t *hw = &priv->hw;
694
695 len = le16_to_cpu(desc->data_len);
696
697 /* Determine the size of the header and the data */
698 fc = le16_to_cpu(desc->frame_ctl);
699 switch (fc & IEEE80211_FCTL_FTYPE) {
700 case IEEE80211_FTYPE_DATA:
701 if ((fc & IEEE80211_FCTL_TODS)
702 && (fc & IEEE80211_FCTL_FROMDS))
703 hdrlen = 30;
704 else
705 hdrlen = 24;
706 datalen = len;
707 break;
708 case IEEE80211_FTYPE_MGMT:
709 hdrlen = 24;
710 datalen = len;
711 break;
712 case IEEE80211_FTYPE_CTL:
713 switch (fc & IEEE80211_FCTL_STYPE) {
714 case IEEE80211_STYPE_PSPOLL:
715 case IEEE80211_STYPE_RTS:
716 case IEEE80211_STYPE_CFEND:
717 case IEEE80211_STYPE_CFENDACK:
718 hdrlen = 16;
719 break;
720 case IEEE80211_STYPE_CTS:
721 case IEEE80211_STYPE_ACK:
722 hdrlen = 10;
723 break;
724 }
725 break;
726 default:
727 /* Unknown frame type */
728 break;
729 }
730
731 /* sanity check the length */
Johannes Berg2c7060022008-10-30 22:09:54 +0100732 if (datalen > IEEE80211_MAX_DATA_LEN + 12) {
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200733 printk(KERN_DEBUG "%s: oversized monitor frame, "
734 "data length = %d\n", dev->name, datalen);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200735 stats->rx_length_errors++;
736 goto update_stats;
737 }
738
739 skb = dev_alloc_skb(hdrlen + datalen);
740 if (!skb) {
741 printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
742 dev->name);
Florin Malitabb6e0932006-05-22 22:35:30 -0700743 goto update_stats;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200744 }
745
746 /* Copy the 802.11 header to the skb */
747 memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700748 skb_reset_mac_header(skb);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200749
750 /* If any, copy the data from the card to the skb */
751 if (datalen > 0) {
752 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
753 ALIGN(datalen, 2), rxfid,
754 HERMES_802_2_OFFSET);
755 if (err) {
756 printk(KERN_ERR "%s: error %d reading monitor frame\n",
757 dev->name, err);
758 goto drop;
759 }
760 }
761
762 skb->dev = dev;
763 skb->ip_summed = CHECKSUM_NONE;
764 skb->pkt_type = PACKET_OTHERHOST;
Harvey Harrisonc1b4aa32009-01-29 13:26:44 -0800765 skb->protocol = cpu_to_be16(ETH_P_802_2);
David Kilroy6fe9deb2009-02-04 23:05:43 +0000766
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200767 stats->rx_packets++;
768 stats->rx_bytes += skb->len;
769
770 netif_rx(skb);
771 return;
772
773 drop:
774 dev_kfree_skb_irq(skb);
775 update_stats:
776 stats->rx_errors++;
777 stats->rx_dropped++;
778}
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780static void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
781{
David Kilroyea60a6a2009-06-18 23:21:26 +0100782 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 struct net_device_stats *stats = &priv->stats;
784 struct iw_statistics *wstats = &priv->wstats;
785 struct sk_buff *skb = NULL;
David Kilroy31afcef2008-08-21 23:28:04 +0100786 u16 rxfid, status;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200787 int length;
David Kilroy31afcef2008-08-21 23:28:04 +0100788 struct hermes_rx_descriptor *desc;
789 struct orinoco_rx_data *rx_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 int err;
791
David Kilroy31afcef2008-08-21 23:28:04 +0100792 desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
793 if (!desc) {
794 printk(KERN_WARNING
795 "%s: Can't allocate space for RX descriptor\n",
796 dev->name);
797 goto update_stats;
798 }
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 rxfid = hermes_read_regn(hw, RXFID);
801
David Kilroy31afcef2008-08-21 23:28:04 +0100802 err = hermes_bap_pread(hw, IRQ_BAP, desc, sizeof(*desc),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 rxfid, 0);
804 if (err) {
805 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
806 "Frame dropped.\n", dev->name, err);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200807 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809
David Kilroy31afcef2008-08-21 23:28:04 +0100810 status = le16_to_cpu(desc->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200812 if (status & HERMES_RXSTAT_BADCRC) {
813 DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
814 dev->name);
815 stats->rx_crc_errors++;
816 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200819 /* Handle frames in monitor mode */
820 if (priv->iw_mode == IW_MODE_MONITOR) {
David Kilroy31afcef2008-08-21 23:28:04 +0100821 orinoco_rx_monitor(dev, rxfid, desc);
822 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200825 if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
826 DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
827 dev->name);
828 wstats->discard.code++;
829 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831
David Kilroy31afcef2008-08-21 23:28:04 +0100832 length = le16_to_cpu(desc->data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /* Sanity checks */
835 if (length < 3) { /* No for even an 802.2 LLC header */
836 /* At least on Symbol firmware with PCF we get quite a
David Kilroy6fe9deb2009-02-04 23:05:43 +0000837 lot of these legitimately - Poll frames with no
838 data. */
David Kilroy31afcef2008-08-21 23:28:04 +0100839 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
Johannes Berg2c7060022008-10-30 22:09:54 +0100841 if (length > IEEE80211_MAX_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
843 dev->name, length);
844 stats->rx_length_errors++;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200845 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847
David Kilroy23edcc42008-08-21 23:28:05 +0100848 /* Payload size does not include Michael MIC. Increase payload
849 * size to read it together with the data. */
850 if (status & HERMES_RXSTAT_MIC)
851 length += MICHAEL_MIC_LEN;
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /* We need space for the packet data itself, plus an ethernet
854 header, plus 2 bytes so we can align the IP header on a
855 32bit boundary, plus 1 byte so we can read in odd length
856 packets from the card, which has an IO granularity of 16
David Kilroy6fe9deb2009-02-04 23:05:43 +0000857 bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 skb = dev_alloc_skb(length+ETH_HLEN+2+1);
859 if (!skb) {
860 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
861 dev->name);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200862 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200865 /* We'll prepend the header, so reserve space for it. The worst
866 case is no decapsulation, when 802.3 header is prepended and
867 nothing is removed. 2 is for aligning the IP header. */
868 skb_reserve(skb, ETH_HLEN + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200870 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, length),
871 ALIGN(length, 2), rxfid,
872 HERMES_802_2_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (err) {
874 printk(KERN_ERR "%s: error %d reading frame. "
875 "Frame dropped.\n", dev->name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 goto drop;
877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
David Kilroy31afcef2008-08-21 23:28:04 +0100879 /* Add desc and skb to rx queue */
880 rx_data = kzalloc(sizeof(*rx_data), GFP_ATOMIC);
881 if (!rx_data) {
882 printk(KERN_WARNING "%s: Can't allocate RX packet\n",
883 dev->name);
884 goto drop;
885 }
886 rx_data->desc = desc;
887 rx_data->skb = skb;
888 list_add_tail(&rx_data->list, &priv->rx_list);
889 tasklet_schedule(&priv->rx_tasklet);
890
891 return;
892
893drop:
894 dev_kfree_skb_irq(skb);
895update_stats:
896 stats->rx_errors++;
897 stats->rx_dropped++;
898out:
899 kfree(desc);
900}
901
902static void orinoco_rx(struct net_device *dev,
903 struct hermes_rx_descriptor *desc,
904 struct sk_buff *skb)
905{
David Kilroyea60a6a2009-06-18 23:21:26 +0100906 struct orinoco_private *priv = ndev_priv(dev);
David Kilroy31afcef2008-08-21 23:28:04 +0100907 struct net_device_stats *stats = &priv->stats;
908 u16 status, fc;
909 int length;
910 struct ethhdr *hdr;
911
912 status = le16_to_cpu(desc->status);
913 length = le16_to_cpu(desc->data_len);
914 fc = le16_to_cpu(desc->frame_ctl);
915
David Kilroy23edcc42008-08-21 23:28:05 +0100916 /* Calculate and check MIC */
917 if (status & HERMES_RXSTAT_MIC) {
918 int key_id = ((status & HERMES_RXSTAT_MIC_KEY_ID) >>
919 HERMES_MIC_KEY_ID_SHIFT);
920 u8 mic[MICHAEL_MIC_LEN];
921 u8 *rxmic;
922 u8 *src = (fc & IEEE80211_FCTL_FROMDS) ?
923 desc->addr3 : desc->addr2;
924
925 /* Extract Michael MIC from payload */
926 rxmic = skb->data + skb->len - MICHAEL_MIC_LEN;
927
928 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
929 length -= MICHAEL_MIC_LEN;
930
David Kilroy4adb4742009-02-04 23:05:51 +0000931 orinoco_mic(priv->rx_tfm_mic,
David Kilroy23edcc42008-08-21 23:28:05 +0100932 priv->tkip_key[key_id].rx_mic,
933 desc->addr1,
934 src,
935 0, /* priority or QoS? */
936 skb->data,
937 skb->len,
938 &mic[0]);
939
940 if (memcmp(mic, rxmic,
941 MICHAEL_MIC_LEN)) {
942 union iwreq_data wrqu;
943 struct iw_michaelmicfailure wxmic;
David Kilroy23edcc42008-08-21 23:28:05 +0100944
945 printk(KERN_WARNING "%s: "
Johannes Berge1749612008-10-27 15:59:26 -0700946 "Invalid Michael MIC in data frame from %pM, "
David Kilroy23edcc42008-08-21 23:28:05 +0100947 "using key %i\n",
Johannes Berge1749612008-10-27 15:59:26 -0700948 dev->name, src, key_id);
David Kilroy23edcc42008-08-21 23:28:05 +0100949
950 /* TODO: update stats */
951
952 /* Notify userspace */
953 memset(&wxmic, 0, sizeof(wxmic));
954 wxmic.flags = key_id & IW_MICFAILURE_KEY_ID;
955 wxmic.flags |= (desc->addr1[0] & 1) ?
956 IW_MICFAILURE_GROUP : IW_MICFAILURE_PAIRWISE;
957 wxmic.src_addr.sa_family = ARPHRD_ETHER;
958 memcpy(wxmic.src_addr.sa_data, src, ETH_ALEN);
959
960 (void) orinoco_hw_get_tkip_iv(priv, key_id,
961 &wxmic.tsc[0]);
962
963 memset(&wrqu, 0, sizeof(wrqu));
964 wrqu.data.length = sizeof(wxmic);
965 wireless_send_event(dev, IWEVMICHAELMICFAILURE, &wrqu,
966 (char *) &wxmic);
967
968 goto drop;
969 }
970 }
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* Handle decapsulation
973 * In most cases, the firmware tell us about SNAP frames.
974 * For some reason, the SNAP frames sent by LinkSys APs
975 * are not properly recognised by most firmwares.
976 * So, check ourselves */
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200977 if (length >= ENCAPS_OVERHEAD &&
978 (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
979 ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
980 is_ethersnap(skb->data))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 /* These indicate a SNAP within 802.2 LLC within
982 802.11 frame which we'll need to de-encapsulate to
983 the original EthernetII frame. */
David Kilroyb2f30a02009-02-04 23:05:46 +0000984 hdr = (struct ethhdr *)skb_push(skb,
985 ETH_HLEN - ENCAPS_OVERHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 } else {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200987 /* 802.3 frame - prepend 802.3 header as is */
988 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
989 hdr->h_proto = htons(length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
David Kilroy31afcef2008-08-21 23:28:04 +0100991 memcpy(hdr->h_dest, desc->addr1, ETH_ALEN);
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200992 if (fc & IEEE80211_FCTL_FROMDS)
David Kilroy31afcef2008-08-21 23:28:04 +0100993 memcpy(hdr->h_source, desc->addr3, ETH_ALEN);
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200994 else
David Kilroy31afcef2008-08-21 23:28:04 +0100995 memcpy(hdr->h_source, desc->addr2, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 skb->protocol = eth_type_trans(skb, dev);
998 skb->ip_summed = CHECKSUM_NONE;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200999 if (fc & IEEE80211_FCTL_TODS)
1000 skb->pkt_type = PACKET_OTHERHOST;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 /* Process the wireless stats if needed */
David Kilroy31afcef2008-08-21 23:28:04 +01001003 orinoco_stat_gather(dev, skb, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 /* Pass the packet to the networking stack */
1006 netif_rx(skb);
1007 stats->rx_packets++;
1008 stats->rx_bytes += length;
1009
1010 return;
David Kilroy23edcc42008-08-21 23:28:05 +01001011
1012 drop:
1013 dev_kfree_skb(skb);
1014 stats->rx_errors++;
1015 stats->rx_dropped++;
David Kilroy31afcef2008-08-21 23:28:04 +01001016}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
David Kilroy31afcef2008-08-21 23:28:04 +01001018static void orinoco_rx_isr_tasklet(unsigned long data)
1019{
David Kilroy53819562009-06-18 23:21:28 +01001020 struct orinoco_private *priv = (struct orinoco_private *) data;
1021 struct net_device *dev = priv->ndev;
David Kilroy31afcef2008-08-21 23:28:04 +01001022 struct orinoco_rx_data *rx_data, *temp;
1023 struct hermes_rx_descriptor *desc;
1024 struct sk_buff *skb;
David Kilroy20953ad2009-01-07 00:23:55 +00001025 unsigned long flags;
1026
1027 /* orinoco_rx requires the driver lock, and we also need to
1028 * protect priv->rx_list, so just hold the lock over the
1029 * lot.
1030 *
1031 * If orinoco_lock fails, we've unplugged the card. In this
1032 * case just abort. */
1033 if (orinoco_lock(priv, &flags) != 0)
1034 return;
David Kilroy31afcef2008-08-21 23:28:04 +01001035
1036 /* extract desc and skb from queue */
1037 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
1038 desc = rx_data->desc;
1039 skb = rx_data->skb;
1040 list_del(&rx_data->list);
1041 kfree(rx_data);
1042
1043 orinoco_rx(dev, desc, skb);
1044
1045 kfree(desc);
1046 }
David Kilroy20953ad2009-01-07 00:23:55 +00001047
1048 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051/********************************************************************/
1052/* Rx path (info frames) */
1053/********************************************************************/
1054
1055static void print_linkstatus(struct net_device *dev, u16 status)
1056{
David Kilroy21312662009-02-04 23:05:47 +00001057 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 if (suppress_linkstatus)
1060 return;
1061
1062 switch (status) {
1063 case HERMES_LINKSTATUS_NOT_CONNECTED:
1064 s = "Not Connected";
1065 break;
1066 case HERMES_LINKSTATUS_CONNECTED:
1067 s = "Connected";
1068 break;
1069 case HERMES_LINKSTATUS_DISCONNECTED:
1070 s = "Disconnected";
1071 break;
1072 case HERMES_LINKSTATUS_AP_CHANGE:
1073 s = "AP Changed";
1074 break;
1075 case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1076 s = "AP Out of Range";
1077 break;
1078 case HERMES_LINKSTATUS_AP_IN_RANGE:
1079 s = "AP In Range";
1080 break;
1081 case HERMES_LINKSTATUS_ASSOC_FAILED:
1082 s = "Association Failed";
1083 break;
1084 default:
1085 s = "UNKNOWN";
1086 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001087
Pavel Roskin11eaea42009-01-18 23:20:58 -05001088 printk(KERN_DEBUG "%s: New link status: %s (%04x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 dev->name, s, status);
1090}
1091
Christoph Hellwig16739b02005-06-19 01:27:51 +02001092/* Search scan results for requested BSSID, join it if found */
David Howellsc4028952006-11-22 14:57:56 +00001093static void orinoco_join_ap(struct work_struct *work)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001094{
David Howellsc4028952006-11-22 14:57:56 +00001095 struct orinoco_private *priv =
1096 container_of(work, struct orinoco_private, join_work);
1097 struct net_device *dev = priv->ndev;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001098 struct hermes *hw = &priv->hw;
1099 int err;
1100 unsigned long flags;
1101 struct join_req {
1102 u8 bssid[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -04001103 __le16 channel;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001104 } __attribute__ ((packed)) req;
1105 const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001106 struct prism2_scan_apinfo *atom = NULL;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001107 int offset = 4;
Pavel Roskinc89cc222005-09-01 20:06:06 -04001108 int found = 0;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001109 u8 *buf;
1110 u16 len;
1111
1112 /* Allocate buffer for scan results */
1113 buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
David Kilroya94e8422009-02-04 23:05:44 +00001114 if (!buf)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001115 return;
1116
1117 if (orinoco_lock(priv, &flags) != 0)
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001118 goto fail_lock;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001119
1120 /* Sanity checks in case user changed something in the meantime */
David Kilroya94e8422009-02-04 23:05:44 +00001121 if (!priv->bssid_fixed)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001122 goto out;
1123
1124 if (strlen(priv->desired_essid) == 0)
1125 goto out;
1126
1127 /* Read scan results from the firmware */
1128 err = hermes_read_ltv(hw, USER_BAP,
1129 HERMES_RID_SCANRESULTSTABLE,
1130 MAX_SCAN_LEN, &len, buf);
1131 if (err) {
1132 printk(KERN_ERR "%s: Cannot read scan results\n",
1133 dev->name);
1134 goto out;
1135 }
1136
1137 len = HERMES_RECLEN_TO_BYTES(len);
1138
1139 /* Go through the scan results looking for the channel of the AP
1140 * we were requested to join */
1141 for (; offset + atom_len <= len; offset += atom_len) {
1142 atom = (struct prism2_scan_apinfo *) (buf + offset);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001143 if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1144 found = 1;
1145 break;
1146 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001147 }
1148
David Kilroya94e8422009-02-04 23:05:44 +00001149 if (!found) {
Pavel Roskinc89cc222005-09-01 20:06:06 -04001150 DEBUG(1, "%s: Requested AP not found in scan results\n",
1151 dev->name);
1152 goto out;
1153 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001154
Christoph Hellwig16739b02005-06-19 01:27:51 +02001155 memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1156 req.channel = atom->channel; /* both are little-endian */
1157 err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1158 &req);
1159 if (err)
1160 printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1161
1162 out:
Christoph Hellwig16739b02005-06-19 01:27:51 +02001163 orinoco_unlock(priv, &flags);
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001164
1165 fail_lock:
1166 kfree(buf);
Christoph Hellwig16739b02005-06-19 01:27:51 +02001167}
1168
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001169/* Send new BSSID to userspace */
David Kilroy6cd90b12008-08-21 23:28:00 +01001170static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001171{
David Howellsc4028952006-11-22 14:57:56 +00001172 struct net_device *dev = priv->ndev;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001173 struct hermes *hw = &priv->hw;
1174 union iwreq_data wrqu;
1175 int err;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001176
David Kilroy499b7022008-12-09 21:46:29 +00001177 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001178 ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
1179 if (err != 0)
David Kilroy6cd90b12008-08-21 23:28:00 +01001180 return;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001181
1182 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1183
1184 /* Send event to user space */
1185 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001186}
1187
David Kilroy06009fd2008-08-21 23:28:03 +01001188static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
1189{
1190 struct net_device *dev = priv->ndev;
1191 struct hermes *hw = &priv->hw;
1192 union iwreq_data wrqu;
1193 int err;
1194 u8 buf[88];
1195 u8 *ie;
1196
1197 if (!priv->has_wpa)
1198 return;
1199
David Kilroy499b7022008-12-09 21:46:29 +00001200 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
David Kilroy06009fd2008-08-21 23:28:03 +01001201 sizeof(buf), NULL, &buf);
1202 if (err != 0)
1203 return;
1204
1205 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1206 if (ie) {
1207 int rem = sizeof(buf) - (ie - &buf[0]);
1208 wrqu.data.length = ie[1] + 2;
1209 if (wrqu.data.length > rem)
1210 wrqu.data.length = rem;
1211
1212 if (wrqu.data.length)
1213 /* Send event to user space */
1214 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
1215 }
1216}
1217
1218static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
1219{
1220 struct net_device *dev = priv->ndev;
1221 struct hermes *hw = &priv->hw;
1222 union iwreq_data wrqu;
1223 int err;
1224 u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
1225 u8 *ie;
1226
1227 if (!priv->has_wpa)
1228 return;
1229
David Kilroy499b7022008-12-09 21:46:29 +00001230 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_RESP_INFO,
David Kilroy06009fd2008-08-21 23:28:03 +01001231 sizeof(buf), NULL, &buf);
1232 if (err != 0)
1233 return;
1234
1235 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1236 if (ie) {
1237 int rem = sizeof(buf) - (ie - &buf[0]);
1238 wrqu.data.length = ie[1] + 2;
1239 if (wrqu.data.length > rem)
1240 wrqu.data.length = rem;
1241
1242 if (wrqu.data.length)
1243 /* Send event to user space */
1244 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
1245 }
1246}
1247
David Kilroy6cd90b12008-08-21 23:28:00 +01001248static void orinoco_send_wevents(struct work_struct *work)
1249{
1250 struct orinoco_private *priv =
1251 container_of(work, struct orinoco_private, wevent_work);
1252 unsigned long flags;
1253
1254 if (orinoco_lock(priv, &flags) != 0)
1255 return;
1256
David Kilroy06009fd2008-08-21 23:28:03 +01001257 orinoco_send_assocreqie_wevent(priv);
1258 orinoco_send_assocrespie_wevent(priv);
David Kilroy6cd90b12008-08-21 23:28:00 +01001259 orinoco_send_bssid_wevent(priv);
1260
1261 orinoco_unlock(priv, &flags);
1262}
Dan Williams1e3428e2007-10-10 23:56:25 -04001263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264static void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
1265{
David Kilroyea60a6a2009-06-18 23:21:26 +01001266 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 u16 infofid;
1268 struct {
Pavel Roskind133ae42005-09-23 04:18:06 -04001269 __le16 len;
1270 __le16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 } __attribute__ ((packed)) info;
1272 int len, type;
1273 int err;
1274
1275 /* This is an answer to an INQUIRE command that we did earlier,
1276 * or an information "event" generated by the card
1277 * The controller return to us a pseudo frame containing
1278 * the information in question - Jean II */
1279 infofid = hermes_read_regn(hw, INFOFID);
1280
1281 /* Read the info frame header - don't try too hard */
1282 err = hermes_bap_pread(hw, IRQ_BAP, &info, sizeof(info),
1283 infofid, 0);
1284 if (err) {
1285 printk(KERN_ERR "%s: error %d reading info frame. "
1286 "Frame dropped.\n", dev->name, err);
1287 return;
1288 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
1291 type = le16_to_cpu(info.type);
1292
1293 switch (type) {
1294 case HERMES_INQ_TALLIES: {
1295 struct hermes_tallies_frame tallies;
1296 struct iw_statistics *wstats = &priv->wstats;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (len > sizeof(tallies)) {
1299 printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
1300 dev->name, len);
1301 len = sizeof(tallies);
1302 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001303
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001304 err = hermes_bap_pread(hw, IRQ_BAP, &tallies, len,
1305 infofid, sizeof(info));
1306 if (err)
1307 break;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 /* Increment our various counters */
1310 /* wstats->discard.nwid - no wrong BSSID stuff */
1311 wstats->discard.code +=
1312 le16_to_cpu(tallies.RxWEPUndecryptable);
David Kilroy6fe9deb2009-02-04 23:05:43 +00001313 if (len == sizeof(tallies))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 wstats->discard.code +=
1315 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
1316 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
1317 wstats->discard.misc +=
1318 le16_to_cpu(tallies.TxDiscardsWrongSA);
1319 wstats->discard.fragment +=
1320 le16_to_cpu(tallies.RxMsgInBadMsgFragments);
1321 wstats->discard.retries +=
1322 le16_to_cpu(tallies.TxRetryLimitExceeded);
1323 /* wstats->miss.beacon - no match */
1324 }
1325 break;
1326 case HERMES_INQ_LINKSTATUS: {
1327 struct hermes_linkstatus linkstatus;
1328 u16 newstatus;
1329 int connected;
1330
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001331 if (priv->iw_mode == IW_MODE_MONITOR)
1332 break;
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (len != sizeof(linkstatus)) {
1335 printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
1336 dev->name, len);
1337 break;
1338 }
1339
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001340 err = hermes_bap_pread(hw, IRQ_BAP, &linkstatus, len,
1341 infofid, sizeof(info));
1342 if (err)
1343 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 newstatus = le16_to_cpu(linkstatus.linkstatus);
1345
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001346 /* Symbol firmware uses "out of range" to signal that
1347 * the hostscan frame can be requested. */
1348 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
1349 priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
1350 priv->has_hostscan && priv->scan_inprogress) {
1351 hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
1352 break;
1353 }
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
1356 || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
1357 || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
1358
1359 if (connected)
1360 netif_carrier_on(dev);
David Gibson7bb7c3a2005-05-12 20:02:10 -04001361 else if (!ignore_disconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 netif_carrier_off(dev);
1363
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001364 if (newstatus != priv->last_linkstatus) {
1365 priv->last_linkstatus = newstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 print_linkstatus(dev, newstatus);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001367 /* The info frame contains only one word which is the
1368 * status (see hermes.h). The status is pretty boring
1369 * in itself, that's why we export the new BSSID...
1370 * Jean II */
1371 schedule_work(&priv->wevent_work);
1372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
1374 break;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001375 case HERMES_INQ_SCAN:
1376 if (!priv->scan_inprogress && priv->bssid_fixed &&
1377 priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
1378 schedule_work(&priv->join_work);
1379 break;
1380 }
1381 /* fall through */
1382 case HERMES_INQ_HOSTSCAN:
1383 case HERMES_INQ_HOSTSCAN_SYMBOL: {
1384 /* Result of a scanning. Contains information about
1385 * cells in the vicinity - Jean II */
1386 union iwreq_data wrqu;
1387 unsigned char *buf;
1388
Dan Williams1e3428e2007-10-10 23:56:25 -04001389 /* Scan is no longer in progress */
1390 priv->scan_inprogress = 0;
1391
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001392 /* Sanity check */
1393 if (len > 4096) {
1394 printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
1395 dev->name, len);
1396 break;
1397 }
1398
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001399 /* Allocate buffer for results */
1400 buf = kmalloc(len, GFP_ATOMIC);
1401 if (buf == NULL)
1402 /* No memory, so can't printk()... */
1403 break;
1404
1405 /* Read scan data */
1406 err = hermes_bap_pread(hw, IRQ_BAP, (void *) buf, len,
1407 infofid, sizeof(info));
Pavel Roskin708218b2005-09-01 20:05:19 -04001408 if (err) {
1409 kfree(buf);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001410 break;
Pavel Roskin708218b2005-09-01 20:05:19 -04001411 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001412
1413#ifdef ORINOCO_DEBUG
1414 {
1415 int i;
1416 printk(KERN_DEBUG "Scan result [%02X", buf[0]);
David Kilroya94e8422009-02-04 23:05:44 +00001417 for (i = 1; i < (len * 2); i++)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001418 printk(":%02X", buf[i]);
1419 printk("]\n");
1420 }
1421#endif /* ORINOCO_DEBUG */
1422
David Kilroyaea48b12009-02-04 23:05:49 +00001423 if (orinoco_process_scan_results(priv, buf, len) == 0) {
Dan Williams1e3428e2007-10-10 23:56:25 -04001424 /* Send an empty event to user space.
1425 * We don't send the received data on the event because
1426 * it would require us to do complex transcoding, and
1427 * we want to minimise the work done in the irq handler
1428 * Use a request to extract the data - Jean II */
1429 wrqu.data.length = 0;
1430 wrqu.data.flags = 0;
1431 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
1432 }
1433 kfree(buf);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001434 }
1435 break;
David Kilroy01632fa2008-08-21 23:27:58 +01001436 case HERMES_INQ_CHANNELINFO:
1437 {
1438 struct agere_ext_scan_info *bss;
1439
1440 if (!priv->scan_inprogress) {
1441 printk(KERN_DEBUG "%s: Got chaninfo without scan, "
1442 "len=%d\n", dev->name, len);
1443 break;
1444 }
1445
1446 /* An empty result indicates that the scan is complete */
1447 if (len == 0) {
1448 union iwreq_data wrqu;
1449
1450 /* Scan is no longer in progress */
1451 priv->scan_inprogress = 0;
1452
1453 wrqu.data.length = 0;
1454 wrqu.data.flags = 0;
1455 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
1456 break;
1457 }
1458
1459 /* Sanity check */
1460 else if (len > sizeof(*bss)) {
1461 printk(KERN_WARNING
1462 "%s: Ext scan results too large (%d bytes). "
1463 "Truncating results to %zd bytes.\n",
1464 dev->name, len, sizeof(*bss));
1465 len = sizeof(*bss);
1466 } else if (len < (offsetof(struct agere_ext_scan_info,
1467 data) + 2)) {
1468 /* Drop this result now so we don't have to
1469 * keep checking later */
1470 printk(KERN_WARNING
1471 "%s: Ext scan results too short (%d bytes)\n",
1472 dev->name, len);
1473 break;
1474 }
1475
1476 bss = kmalloc(sizeof(*bss), GFP_ATOMIC);
1477 if (bss == NULL)
1478 break;
1479
1480 /* Read scan data */
1481 err = hermes_bap_pread(hw, IRQ_BAP, (void *) bss, len,
1482 infofid, sizeof(info));
1483 if (err) {
1484 kfree(bss);
1485 break;
1486 }
1487
1488 orinoco_add_ext_scan_result(priv, bss);
1489
1490 kfree(bss);
1491 break;
1492 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001493 case HERMES_INQ_SEC_STAT_AGERE:
1494 /* Security status (Agere specific) */
1495 /* Ignore this frame for now */
1496 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
1497 break;
1498 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 default:
1500 printk(KERN_DEBUG "%s: Unknown information frame received: "
1501 "type 0x%04x, length %d\n", dev->name, type, len);
1502 /* We don't actually do anything about it */
1503 break;
1504 }
1505}
1506
1507static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
1508{
1509 if (net_ratelimit())
1510 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
1511}
1512
1513/********************************************************************/
1514/* Internal hardware control routines */
1515/********************************************************************/
1516
David Kilroya2608362009-06-18 23:21:23 +01001517int __orinoco_up(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
David Kilroya2608362009-06-18 23:21:23 +01001519 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 struct hermes *hw = &priv->hw;
1521 int err;
1522
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001523 netif_carrier_off(dev); /* just to make sure */
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 err = __orinoco_program_rids(dev);
1526 if (err) {
1527 printk(KERN_ERR "%s: Error %d configuring card\n",
1528 dev->name, err);
1529 return err;
1530 }
1531
1532 /* Fire things up again */
1533 hermes_set_irqmask(hw, ORINOCO_INTEN);
1534 err = hermes_enable_port(hw, 0);
1535 if (err) {
1536 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
1537 dev->name, err);
1538 return err;
1539 }
1540
1541 netif_start_queue(dev);
1542
1543 return 0;
1544}
David Kilroy21312662009-02-04 23:05:47 +00001545EXPORT_SYMBOL(__orinoco_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
David Kilroya2608362009-06-18 23:21:23 +01001547int __orinoco_down(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
David Kilroya2608362009-06-18 23:21:23 +01001549 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 struct hermes *hw = &priv->hw;
1551 int err;
1552
1553 netif_stop_queue(dev);
1554
David Kilroya94e8422009-02-04 23:05:44 +00001555 if (!priv->hw_unavailable) {
1556 if (!priv->broken_disableport) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 err = hermes_disable_port(hw, 0);
1558 if (err) {
1559 /* Some firmwares (e.g. Intersil 1.3.x) seem
1560 * to have problems disabling the port, oh
1561 * well, too bad. */
1562 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
1563 dev->name, err);
1564 priv->broken_disableport = 1;
1565 }
1566 }
1567 hermes_set_irqmask(hw, 0);
1568 hermes_write_regn(hw, EVACK, 0xffff);
1569 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 /* firmware will have to reassociate */
1572 netif_carrier_off(dev);
1573 priv->last_linkstatus = 0xffff;
1574
1575 return 0;
1576}
David Kilroy21312662009-02-04 23:05:47 +00001577EXPORT_SYMBOL(__orinoco_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
David Kilroya2608362009-06-18 23:21:23 +01001579int orinoco_reinit_firmware(struct orinoco_private *priv)
Pavel Roskin37a6c612006-04-07 04:10:49 -04001580{
Pavel Roskin37a6c612006-04-07 04:10:49 -04001581 struct hermes *hw = &priv->hw;
1582 int err;
1583
1584 err = hermes_init(hw);
Andrey Borzenkov0df6cbb2008-10-12 20:15:43 +04001585 if (priv->do_fw_download && !err) {
1586 err = orinoco_download(priv);
1587 if (err)
1588 priv->do_fw_download = 0;
1589 }
Pavel Roskin37a6c612006-04-07 04:10:49 -04001590 if (!err)
David Kilroy42a51b92009-06-18 23:21:20 +01001591 err = orinoco_hw_allocate_fid(priv);
Pavel Roskin37a6c612006-04-07 04:10:49 -04001592
1593 return err;
1594}
David Kilroy21312662009-02-04 23:05:47 +00001595EXPORT_SYMBOL(orinoco_reinit_firmware);
Pavel Roskin37a6c612006-04-07 04:10:49 -04001596
David Kilroycb1576a2009-02-04 23:05:56 +00001597int __orinoco_program_rids(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
David Kilroyea60a6a2009-06-18 23:21:26 +01001599 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 hermes_t *hw = &priv->hw;
1601 int err;
1602 struct hermes_idstring idbuf;
1603
1604 /* Set the MAC address */
1605 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
1606 HERMES_BYTES_TO_RECLEN(ETH_ALEN), dev->dev_addr);
1607 if (err) {
1608 printk(KERN_ERR "%s: Error %d setting MAC address\n",
1609 dev->name, err);
1610 return err;
1611 }
1612
1613 /* Set up the link mode */
1614 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFPORTTYPE,
1615 priv->port_type);
1616 if (err) {
1617 printk(KERN_ERR "%s: Error %d setting port type\n",
1618 dev->name, err);
1619 return err;
1620 }
1621 /* Set the channel/frequency */
David Gibsond51d8b12005-05-12 20:03:36 -04001622 if (priv->channel != 0 && priv->iw_mode != IW_MODE_INFRA) {
1623 err = hermes_write_wordrec(hw, USER_BAP,
1624 HERMES_RID_CNFOWNCHANNEL,
1625 priv->channel);
1626 if (err) {
1627 printk(KERN_ERR "%s: Error %d setting channel %d\n",
1628 dev->name, err, priv->channel);
1629 return err;
1630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
1632
1633 if (priv->has_ibss) {
1634 u16 createibss;
1635
1636 if ((strlen(priv->desired_essid) == 0) && (priv->createibss)) {
1637 printk(KERN_WARNING "%s: This firmware requires an "
1638 "ESSID in IBSS-Ad-Hoc mode.\n", dev->name);
1639 /* With wvlan_cs, in this case, we would crash.
1640 * hopefully, this driver will behave better...
1641 * Jean II */
1642 createibss = 0;
1643 } else {
1644 createibss = priv->createibss;
1645 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 err = hermes_write_wordrec(hw, USER_BAP,
1648 HERMES_RID_CNFCREATEIBSS,
1649 createibss);
1650 if (err) {
1651 printk(KERN_ERR "%s: Error %d setting CREATEIBSS\n",
1652 dev->name, err);
1653 return err;
1654 }
1655 }
1656
Christoph Hellwig16739b02005-06-19 01:27:51 +02001657 /* Set the desired BSSID */
1658 err = __orinoco_hw_set_wap(priv);
1659 if (err) {
1660 printk(KERN_ERR "%s: Error %d setting AP address\n",
1661 dev->name, err);
1662 return err;
1663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 /* Set the desired ESSID */
1665 idbuf.len = cpu_to_le16(strlen(priv->desired_essid));
1666 memcpy(&idbuf.val, priv->desired_essid, sizeof(idbuf.val));
1667 /* WinXP wants partner to configure OWNSSID even in IBSS mode. (jimc) */
1668 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNSSID,
David Kilroyb2f30a02009-02-04 23:05:46 +00001669 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
1670 &idbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if (err) {
1672 printk(KERN_ERR "%s: Error %d setting OWNSSID\n",
1673 dev->name, err);
1674 return err;
1675 }
1676 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFDESIREDSSID,
David Kilroyb2f30a02009-02-04 23:05:46 +00001677 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
1678 &idbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 if (err) {
1680 printk(KERN_ERR "%s: Error %d setting DESIREDSSID\n",
1681 dev->name, err);
1682 return err;
1683 }
1684
1685 /* Set the station name */
1686 idbuf.len = cpu_to_le16(strlen(priv->nick));
1687 memcpy(&idbuf.val, priv->nick, sizeof(idbuf.val));
1688 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
1689 HERMES_BYTES_TO_RECLEN(strlen(priv->nick)+2),
1690 &idbuf);
1691 if (err) {
1692 printk(KERN_ERR "%s: Error %d setting nickname\n",
1693 dev->name, err);
1694 return err;
1695 }
1696
1697 /* Set AP density */
1698 if (priv->has_sensitivity) {
1699 err = hermes_write_wordrec(hw, USER_BAP,
1700 HERMES_RID_CNFSYSTEMSCALE,
1701 priv->ap_density);
1702 if (err) {
David Kilroyb2f30a02009-02-04 23:05:46 +00001703 printk(KERN_WARNING "%s: Error %d setting SYSTEMSCALE. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 "Disabling sensitivity control\n",
1705 dev->name, err);
1706
1707 priv->has_sensitivity = 0;
1708 }
1709 }
1710
1711 /* Set RTS threshold */
1712 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
1713 priv->rts_thresh);
1714 if (err) {
1715 printk(KERN_ERR "%s: Error %d setting RTS threshold\n",
1716 dev->name, err);
1717 return err;
1718 }
1719
1720 /* Set fragmentation threshold or MWO robustness */
1721 if (priv->has_mwo)
1722 err = hermes_write_wordrec(hw, USER_BAP,
1723 HERMES_RID_CNFMWOROBUST_AGERE,
1724 priv->mwo_robust);
1725 else
1726 err = hermes_write_wordrec(hw, USER_BAP,
1727 HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
1728 priv->frag_thresh);
1729 if (err) {
1730 printk(KERN_ERR "%s: Error %d setting fragmentation\n",
1731 dev->name, err);
1732 return err;
1733 }
1734
1735 /* Set bitrate */
1736 err = __orinoco_hw_set_bitrate(priv);
1737 if (err) {
1738 printk(KERN_ERR "%s: Error %d setting bitrate\n",
1739 dev->name, err);
1740 return err;
1741 }
1742
1743 /* Set power management */
1744 if (priv->has_pm) {
1745 err = hermes_write_wordrec(hw, USER_BAP,
1746 HERMES_RID_CNFPMENABLED,
1747 priv->pm_on);
1748 if (err) {
1749 printk(KERN_ERR "%s: Error %d setting up PM\n",
1750 dev->name, err);
1751 return err;
1752 }
1753
1754 err = hermes_write_wordrec(hw, USER_BAP,
1755 HERMES_RID_CNFMULTICASTRECEIVE,
1756 priv->pm_mcast);
1757 if (err) {
1758 printk(KERN_ERR "%s: Error %d setting up PM\n",
1759 dev->name, err);
1760 return err;
1761 }
1762 err = hermes_write_wordrec(hw, USER_BAP,
1763 HERMES_RID_CNFMAXSLEEPDURATION,
1764 priv->pm_period);
1765 if (err) {
1766 printk(KERN_ERR "%s: Error %d setting up PM\n",
1767 dev->name, err);
1768 return err;
1769 }
1770 err = hermes_write_wordrec(hw, USER_BAP,
1771 HERMES_RID_CNFPMHOLDOVERDURATION,
1772 priv->pm_timeout);
1773 if (err) {
1774 printk(KERN_ERR "%s: Error %d setting up PM\n",
1775 dev->name, err);
1776 return err;
1777 }
1778 }
1779
1780 /* Set preamble - only for Symbol so far... */
1781 if (priv->has_preamble) {
1782 err = hermes_write_wordrec(hw, USER_BAP,
1783 HERMES_RID_CNFPREAMBLE_SYMBOL,
1784 priv->preamble);
1785 if (err) {
1786 printk(KERN_ERR "%s: Error %d setting preamble\n",
1787 dev->name, err);
1788 return err;
1789 }
1790 }
1791
1792 /* Set up encryption */
David Kilroyd03032a2008-08-21 23:28:02 +01001793 if (priv->has_wep || priv->has_wpa) {
1794 err = __orinoco_hw_setup_enc(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (err) {
David Kilroyd03032a2008-08-21 23:28:02 +01001796 printk(KERN_ERR "%s: Error %d activating encryption\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 dev->name, err);
1798 return err;
1799 }
1800 }
1801
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001802 if (priv->iw_mode == IW_MODE_MONITOR) {
1803 /* Enable monitor mode */
1804 dev->type = ARPHRD_IEEE80211;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001805 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001806 HERMES_TEST_MONITOR, 0, NULL);
1807 } else {
1808 /* Disable monitor mode */
1809 dev->type = ARPHRD_ETHER;
1810 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
1811 HERMES_TEST_STOP, 0, NULL);
1812 }
1813 if (err)
1814 return err;
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 /* Set promiscuity / multicast*/
1817 priv->promiscuous = 0;
1818 priv->mc_count = 0;
Herbert Xu932ff272006-06-09 12:20:56 -07001819
1820 /* FIXME: what about netif_tx_lock */
1821 __orinoco_set_multicast_list(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
1823 return 0;
1824}
1825
David Kilroy5865d012009-02-04 23:05:54 +00001826/* FIXME: return int? */
1827static void
1828__orinoco_set_multicast_list(struct net_device *dev)
1829{
David Kilroyea60a6a2009-06-18 23:21:26 +01001830 struct orinoco_private *priv = ndev_priv(dev);
David Kilroy5865d012009-02-04 23:05:54 +00001831 int err = 0;
1832 int promisc, mc_count;
1833
1834 /* The Hermes doesn't seem to have an allmulti mode, so we go
1835 * into promiscuous mode and let the upper levels deal. */
1836 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
1837 (dev->mc_count > MAX_MULTICAST(priv))) {
1838 promisc = 1;
1839 mc_count = 0;
1840 } else {
1841 promisc = 0;
1842 mc_count = dev->mc_count;
1843 }
1844
1845 err = __orinoco_hw_set_multicast_list(priv, dev->mc_list, mc_count,
1846 promisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847}
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849/* This must be called from user context, without locks held - use
1850 * schedule_work() */
David Kilroycb1576a2009-02-04 23:05:56 +00001851void orinoco_reset(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
David Howellsc4028952006-11-22 14:57:56 +00001853 struct orinoco_private *priv =
1854 container_of(work, struct orinoco_private, reset_work);
1855 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 struct hermes *hw = &priv->hw;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001857 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 unsigned long flags;
1859
1860 if (orinoco_lock(priv, &flags) != 0)
1861 /* When the hardware becomes available again, whatever
1862 * detects that is responsible for re-initializing
1863 * it. So no need for anything further */
1864 return;
1865
1866 netif_stop_queue(dev);
1867
1868 /* Shut off interrupts. Depending on what state the hardware
1869 * is in, this might not work, but we'll try anyway */
1870 hermes_set_irqmask(hw, 0);
1871 hermes_write_regn(hw, EVACK, 0xffff);
1872
1873 priv->hw_unavailable++;
1874 priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
1875 netif_carrier_off(dev);
1876
1877 orinoco_unlock(priv, &flags);
1878
David Kilroy6fe9deb2009-02-04 23:05:43 +00001879 /* Scanning support: Cleanup of driver struct */
Dan Williams1e3428e2007-10-10 23:56:25 -04001880 orinoco_clear_scan_results(priv, 0);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001881 priv->scan_inprogress = 0;
1882
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001883 if (priv->hard_reset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 err = (*priv->hard_reset)(priv);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001885 if (err) {
1886 printk(KERN_ERR "%s: orinoco_reset: Error %d "
1887 "performing hard reset\n", dev->name, err);
1888 goto disable;
1889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 }
1891
David Kilroya2608362009-06-18 23:21:23 +01001892 err = orinoco_reinit_firmware(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (err) {
1894 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
1895 dev->name, err);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001896 goto disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
1898
David Kilroyb2f30a02009-02-04 23:05:46 +00001899 /* This has to be called from user context */
1900 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
1902 priv->hw_unavailable--;
1903
1904 /* priv->open or priv->hw_unavailable might have changed while
1905 * we dropped the lock */
David Kilroya94e8422009-02-04 23:05:44 +00001906 if (priv->open && (!priv->hw_unavailable)) {
David Kilroya2608362009-06-18 23:21:23 +01001907 err = __orinoco_up(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 if (err) {
1909 printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
1910 dev->name, err);
1911 } else
1912 dev->trans_start = jiffies;
1913 }
1914
1915 spin_unlock_irq(&priv->lock);
1916
1917 return;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001918 disable:
1919 hermes_set_irqmask(hw, 0);
1920 netif_device_detach(dev);
1921 printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922}
1923
1924/********************************************************************/
1925/* Interrupt handler */
1926/********************************************************************/
1927
1928static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
1929{
1930 printk(KERN_DEBUG "%s: TICK\n", dev->name);
1931}
1932
1933static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
1934{
1935 /* This seems to happen a fair bit under load, but ignoring it
1936 seems to work fine...*/
1937 printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
1938 dev->name);
1939}
1940
David Howells7d12e782006-10-05 14:55:46 +01001941irqreturn_t orinoco_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
David Kilroya2608362009-06-18 23:21:23 +01001943 struct orinoco_private *priv = dev_id;
1944 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 hermes_t *hw = &priv->hw;
1946 int count = MAX_IRQLOOPS_PER_IRQ;
1947 u16 evstat, events;
David Kilroy21312662009-02-04 23:05:47 +00001948 /* These are used to detect a runaway interrupt situation.
1949 *
1950 * If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
1951 * we panic and shut down the hardware
1952 */
1953 /* jiffies value the last time we were called */
1954 static int last_irq_jiffy; /* = 0 */
1955 static int loops_this_jiffy; /* = 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 unsigned long flags;
1957
1958 if (orinoco_lock(priv, &flags) != 0) {
1959 /* If hw is unavailable - we don't know if the irq was
1960 * for us or not */
1961 return IRQ_HANDLED;
1962 }
1963
1964 evstat = hermes_read_regn(hw, EVSTAT);
1965 events = evstat & hw->inten;
David Kilroya94e8422009-02-04 23:05:44 +00001966 if (!events) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 orinoco_unlock(priv, &flags);
1968 return IRQ_NONE;
1969 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (jiffies != last_irq_jiffy)
1972 loops_this_jiffy = 0;
1973 last_irq_jiffy = jiffies;
1974
1975 while (events && count--) {
1976 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
1977 printk(KERN_WARNING "%s: IRQ handler is looping too "
1978 "much! Resetting.\n", dev->name);
1979 /* Disable interrupts for now */
1980 hermes_set_irqmask(hw, 0);
1981 schedule_work(&priv->reset_work);
1982 break;
1983 }
1984
1985 /* Check the card hasn't been removed */
David Kilroya94e8422009-02-04 23:05:44 +00001986 if (!hermes_present(hw)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 DEBUG(0, "orinoco_interrupt(): card removed\n");
1988 break;
1989 }
1990
1991 if (events & HERMES_EV_TICK)
1992 __orinoco_ev_tick(dev, hw);
1993 if (events & HERMES_EV_WTERR)
1994 __orinoco_ev_wterr(dev, hw);
1995 if (events & HERMES_EV_INFDROP)
1996 __orinoco_ev_infdrop(dev, hw);
1997 if (events & HERMES_EV_INFO)
1998 __orinoco_ev_info(dev, hw);
1999 if (events & HERMES_EV_RX)
2000 __orinoco_ev_rx(dev, hw);
2001 if (events & HERMES_EV_TXEXC)
2002 __orinoco_ev_txexc(dev, hw);
2003 if (events & HERMES_EV_TX)
2004 __orinoco_ev_tx(dev, hw);
2005 if (events & HERMES_EV_ALLOC)
2006 __orinoco_ev_alloc(dev, hw);
David Kilroy6fe9deb2009-02-04 23:05:43 +00002007
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002008 hermes_write_regn(hw, EVACK, evstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 evstat = hermes_read_regn(hw, EVSTAT);
2011 events = evstat & hw->inten;
2012 };
2013
2014 orinoco_unlock(priv, &flags);
2015 return IRQ_HANDLED;
2016}
David Kilroy21312662009-02-04 23:05:47 +00002017EXPORT_SYMBOL(orinoco_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
2019/********************************************************************/
David Kilroy39d1ffe2008-11-22 10:37:28 +00002020/* Power management */
2021/********************************************************************/
2022#if defined(CONFIG_PM_SLEEP) && !defined(CONFIG_HERMES_CACHE_FW_ON_INIT)
2023static int orinoco_pm_notifier(struct notifier_block *notifier,
2024 unsigned long pm_event,
2025 void *unused)
2026{
2027 struct orinoco_private *priv = container_of(notifier,
2028 struct orinoco_private,
2029 pm_notifier);
2030
2031 /* All we need to do is cache the firmware before suspend, and
2032 * release it when we come out.
2033 *
2034 * Only need to do this if we're downloading firmware. */
2035 if (!priv->do_fw_download)
2036 return NOTIFY_DONE;
2037
2038 switch (pm_event) {
2039 case PM_HIBERNATION_PREPARE:
2040 case PM_SUSPEND_PREPARE:
2041 orinoco_cache_fw(priv, 0);
2042 break;
2043
2044 case PM_POST_RESTORE:
2045 /* Restore from hibernation failed. We need to clean
2046 * up in exactly the same way, so fall through. */
2047 case PM_POST_HIBERNATION:
2048 case PM_POST_SUSPEND:
2049 orinoco_uncache_fw(priv);
2050 break;
2051
2052 case PM_RESTORE_PREPARE:
2053 default:
2054 break;
2055 }
2056
2057 return NOTIFY_DONE;
2058}
David S. Millerf11c1792009-02-25 00:02:05 -08002059
2060static void orinoco_register_pm_notifier(struct orinoco_private *priv)
2061{
2062 priv->pm_notifier.notifier_call = orinoco_pm_notifier;
2063 register_pm_notifier(&priv->pm_notifier);
2064}
2065
2066static void orinoco_unregister_pm_notifier(struct orinoco_private *priv)
2067{
2068 unregister_pm_notifier(&priv->pm_notifier);
2069}
David Kilroy39d1ffe2008-11-22 10:37:28 +00002070#else /* !PM_SLEEP || HERMES_CACHE_FW_ON_INIT */
David S. Millerf11c1792009-02-25 00:02:05 -08002071#define orinoco_register_pm_notifier(priv) do { } while(0)
2072#define orinoco_unregister_pm_notifier(priv) do { } while(0)
David Kilroy39d1ffe2008-11-22 10:37:28 +00002073#endif
2074
2075/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076/* Initialization */
2077/********************************************************************/
2078
David Kilroy8e638262009-06-18 23:21:24 +01002079int orinoco_init(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
David Kilroy8e638262009-06-18 23:21:24 +01002081 struct device *dev = priv->dev;
David Kilroyea60a6a2009-06-18 23:21:26 +01002082 struct wiphy *wiphy = priv_to_wiphy(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 hermes_t *hw = &priv->hw;
2084 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 /* No need to lock, the hw_unavailable flag is already set in
2087 * alloc_orinocodev() */
Johannes Berg2c7060022008-10-30 22:09:54 +01002088 priv->nicbuf_size = IEEE80211_MAX_FRAME_LEN + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090 /* Initialize the firmware */
Pavel Roskin37a6c612006-04-07 04:10:49 -04002091 err = hermes_init(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 if (err != 0) {
David Kilroy8e638262009-06-18 23:21:24 +01002093 dev_err(dev, "Failed to initialize firmware (err = %d)\n",
2094 err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 goto out;
2096 }
2097
David Kilroya2d1a422009-06-18 23:21:18 +01002098 err = determine_fw_capabilities(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 if (err != 0) {
David Kilroy8e638262009-06-18 23:21:24 +01002100 dev_err(dev, "Incompatible firmware, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 goto out;
2102 }
2103
David Kilroy3994d502008-08-21 23:27:54 +01002104 if (priv->do_fw_download) {
David Kilroy39d1ffe2008-11-22 10:37:28 +00002105#ifdef CONFIG_HERMES_CACHE_FW_ON_INIT
David Kilroy74734312008-11-22 10:37:25 +00002106 orinoco_cache_fw(priv, 0);
David Kilroy39d1ffe2008-11-22 10:37:28 +00002107#endif
David Kilroy74734312008-11-22 10:37:25 +00002108
David Kilroy3994d502008-08-21 23:27:54 +01002109 err = orinoco_download(priv);
2110 if (err)
2111 priv->do_fw_download = 0;
2112
2113 /* Check firmware version again */
David Kilroya2d1a422009-06-18 23:21:18 +01002114 err = determine_fw_capabilities(priv);
David Kilroy3994d502008-08-21 23:27:54 +01002115 if (err != 0) {
David Kilroy8e638262009-06-18 23:21:24 +01002116 dev_err(dev, "Incompatible firmware, aborting\n");
David Kilroy3994d502008-08-21 23:27:54 +01002117 goto out;
2118 }
2119 }
2120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 if (priv->has_port3)
David Kilroy8e638262009-06-18 23:21:24 +01002122 dev_info(dev, "Ad-hoc demo mode supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if (priv->has_ibss)
David Kilroy8e638262009-06-18 23:21:24 +01002124 dev_info(dev, "IEEE standard IBSS ad-hoc mode supported\n");
2125 if (priv->has_wep)
2126 dev_info(dev, "WEP supported, %s-bit key\n",
2127 priv->has_big_wep ? "104" : "40");
David Kilroy23edcc42008-08-21 23:28:05 +01002128 if (priv->has_wpa) {
David Kilroy8e638262009-06-18 23:21:24 +01002129 dev_info(dev, "WPA-PSK supported\n");
David Kilroy23edcc42008-08-21 23:28:05 +01002130 if (orinoco_mic_init(priv)) {
David Kilroy8e638262009-06-18 23:21:24 +01002131 dev_err(dev, "Failed to setup MIC crypto algorithm. "
2132 "Disabling WPA support\n");
David Kilroy23edcc42008-08-21 23:28:05 +01002133 priv->has_wpa = 0;
2134 }
2135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
David Kilroy01632fa2008-08-21 23:27:58 +01002137 /* Now we have the firmware capabilities, allocate appropiate
2138 * sized scan buffers */
2139 if (orinoco_bss_data_allocate(priv))
2140 goto out;
2141 orinoco_bss_data_init(priv);
2142
David Kilroyea60a6a2009-06-18 23:21:26 +01002143 err = orinoco_hw_read_card_settings(priv, wiphy->perm_addr);
David Kilroye9e3d012009-06-18 23:21:19 +01002144 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
David Kilroy42a51b92009-06-18 23:21:20 +01002147 err = orinoco_hw_allocate_fid(priv);
Pavel Roskin37a6c612006-04-07 04:10:49 -04002148 if (err) {
David Kilroy8e638262009-06-18 23:21:24 +01002149 dev_err(dev, "Failed to allocate NIC buffer!\n");
Pavel Roskin37a6c612006-04-07 04:10:49 -04002150 goto out;
2151 }
2152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 /* Set up the default configuration */
2154 priv->iw_mode = IW_MODE_INFRA;
2155 /* By default use IEEE/IBSS ad-hoc mode if we have it */
David Kilroya94e8422009-02-04 23:05:44 +00002156 priv->prefer_port3 = priv->has_port3 && (!priv->has_ibss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 set_port_type(priv);
David Gibsond51d8b12005-05-12 20:03:36 -04002158 priv->channel = 0; /* use firmware default */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 priv->promiscuous = 0;
David Kilroy4ae6ee22008-08-21 23:27:59 +01002161 priv->encode_alg = IW_ENCODE_ALG_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 priv->tx_key = 0;
David Kilroyd03032a2008-08-21 23:28:02 +01002163 priv->wpa_enabled = 0;
2164 priv->tkip_cm_active = 0;
2165 priv->key_mgmt = 0;
2166 priv->wpa_ie_len = 0;
2167 priv->wpa_ie = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
David Kilroyea60a6a2009-06-18 23:21:26 +01002169 if (orinoco_wiphy_register(wiphy)) {
2170 err = -ENODEV;
2171 goto out;
2172 }
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 /* Make the hardware available, as long as it hasn't been
2175 * removed elsewhere (e.g. by PCMCIA hot unplug) */
2176 spin_lock_irq(&priv->lock);
2177 priv->hw_unavailable--;
2178 spin_unlock_irq(&priv->lock);
2179
David Kilroy8e638262009-06-18 23:21:24 +01002180 dev_dbg(dev, "Ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return err;
2184}
David Kilroy8e638262009-06-18 23:21:24 +01002185EXPORT_SYMBOL(orinoco_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Andrey Borzenkov89ea4092009-01-21 20:46:46 +03002187static const struct net_device_ops orinoco_netdev_ops = {
Andrey Borzenkov89ea4092009-01-21 20:46:46 +03002188 .ndo_open = orinoco_open,
2189 .ndo_stop = orinoco_stop,
2190 .ndo_start_xmit = orinoco_xmit,
2191 .ndo_set_multicast_list = orinoco_set_multicast_list,
2192 .ndo_change_mtu = orinoco_change_mtu,
2193 .ndo_tx_timeout = orinoco_tx_timeout,
2194 .ndo_get_stats = orinoco_get_stats,
2195};
2196
David Kilroyea60a6a2009-06-18 23:21:26 +01002197/* Allocate private data.
2198 *
2199 * This driver has a number of structures associated with it
2200 * netdev - Net device structure for each network interface
2201 * wiphy - structure associated with wireless phy
2202 * wireless_dev (wdev) - structure for each wireless interface
2203 * hw - structure for hermes chip info
2204 * card - card specific structure for use by the card driver
2205 * (airport, orinoco_cs)
2206 * priv - orinoco private data
2207 * device - generic linux device structure
2208 *
2209 * +---------+ +---------+
2210 * | wiphy | | netdev |
2211 * | +-------+ | +-------+
2212 * | | priv | | | wdev |
2213 * | | +-----+ +-+-------+
2214 * | | | hw |
2215 * | +-+-----+
2216 * | | card |
2217 * +-+-------+
2218 *
2219 * priv has a link to netdev and device
2220 * wdev has a link to wiphy
2221 */
David Kilroya2608362009-06-18 23:21:23 +01002222struct orinoco_private
David Kilroy3994d502008-08-21 23:27:54 +01002223*alloc_orinocodev(int sizeof_card,
2224 struct device *device,
2225 int (*hard_reset)(struct orinoco_private *),
2226 int (*stop_fw)(struct orinoco_private *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 struct orinoco_private *priv;
David Kilroyea60a6a2009-06-18 23:21:26 +01002229 struct wiphy *wiphy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
David Kilroyea60a6a2009-06-18 23:21:26 +01002231 /* allocate wiphy
2232 * NOTE: We only support a single virtual interface
2233 * but this may change when monitor mode is added
2234 */
2235 wiphy = wiphy_new(&orinoco_cfg_ops,
2236 sizeof(struct orinoco_private) + sizeof_card);
2237 if (!wiphy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 return NULL;
David Kilroyea60a6a2009-06-18 23:21:26 +01002239
David Kilroyea60a6a2009-06-18 23:21:26 +01002240 priv = wiphy_priv(wiphy);
David Kilroy53819562009-06-18 23:21:28 +01002241 priv->dev = device;
David Kilroyea60a6a2009-06-18 23:21:26 +01002242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 if (sizeof_card)
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002244 priv->card = (void *)((unsigned long)priv
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 + sizeof(struct orinoco_private));
2246 else
2247 priv->card = NULL;
2248
David Kilroyea60a6a2009-06-18 23:21:26 +01002249 orinoco_wiphy_init(wiphy);
2250
Pavel Roskin343c6862005-09-09 18:43:02 -04002251#ifdef WIRELESS_SPY
2252 priv->wireless_data.spy_data = &priv->spy_data;
Pavel Roskin343c6862005-09-09 18:43:02 -04002253#endif
David Kilroy23edcc42008-08-21 23:28:05 +01002254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 /* Set up default callbacks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 priv->hard_reset = hard_reset;
David Kilroy3994d502008-08-21 23:27:54 +01002257 priv->stop_fw = stop_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
2259 spin_lock_init(&priv->lock);
2260 priv->open = 0;
2261 priv->hw_unavailable = 1; /* orinoco_init() must clear this
2262 * before anything else touches the
2263 * hardware */
David Howellsc4028952006-11-22 14:57:56 +00002264 INIT_WORK(&priv->reset_work, orinoco_reset);
2265 INIT_WORK(&priv->join_work, orinoco_join_ap);
2266 INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
David Kilroy31afcef2008-08-21 23:28:04 +01002268 INIT_LIST_HEAD(&priv->rx_list);
2269 tasklet_init(&priv->rx_tasklet, orinoco_rx_isr_tasklet,
David Kilroy53819562009-06-18 23:21:28 +01002270 (unsigned long) priv);
David Kilroy31afcef2008-08-21 23:28:04 +01002271
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 priv->last_linkstatus = 0xffff;
2273
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +03002274#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
David Kilroy2cea7b22008-11-22 10:37:26 +00002275 priv->cached_pri_fw = NULL;
Andrey Borzenkov4fb30782008-10-19 12:06:11 +04002276 priv->cached_fw = NULL;
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +03002277#endif
Andrey Borzenkov4fb30782008-10-19 12:06:11 +04002278
David Kilroy39d1ffe2008-11-22 10:37:28 +00002279 /* Register PM notifiers */
David S. Millerf11c1792009-02-25 00:02:05 -08002280 orinoco_register_pm_notifier(priv);
David Kilroy39d1ffe2008-11-22 10:37:28 +00002281
David Kilroya2608362009-06-18 23:21:23 +01002282 return priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283}
David Kilroy21312662009-02-04 23:05:47 +00002284EXPORT_SYMBOL(alloc_orinocodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
David Kilroy53819562009-06-18 23:21:28 +01002286/* We can only support a single interface. We provide a separate
2287 * function to set it up to distinguish between hardware
2288 * initialisation and interface setup.
2289 *
2290 * The base_addr and irq parameters are passed on to netdev for use
2291 * with SIOCGIFMAP.
2292 */
2293int orinoco_if_add(struct orinoco_private *priv,
2294 unsigned long base_addr,
2295 unsigned int irq)
2296{
2297 struct wiphy *wiphy = priv_to_wiphy(priv);
2298 struct wireless_dev *wdev;
2299 struct net_device *dev;
2300 int ret;
2301
2302 dev = alloc_etherdev(sizeof(struct wireless_dev));
2303
2304 if (!dev)
2305 return -ENOMEM;
2306
2307 /* Initialise wireless_dev */
2308 wdev = netdev_priv(dev);
2309 wdev->wiphy = wiphy;
2310 wdev->iftype = NL80211_IFTYPE_STATION;
2311
2312 /* Setup / override net_device fields */
2313 dev->ieee80211_ptr = wdev;
2314 dev->netdev_ops = &orinoco_netdev_ops;
2315 dev->watchdog_timeo = HZ; /* 1 second timeout */
2316 dev->ethtool_ops = &orinoco_ethtool_ops;
2317 dev->wireless_handlers = &orinoco_handler_def;
2318#ifdef WIRELESS_SPY
2319 dev->wireless_data = &priv->wireless_data;
2320#endif
2321 /* we use the default eth_mac_addr for setting the MAC addr */
2322
2323 /* Reserve space in skb for the SNAP header */
2324 dev->hard_header_len += ENCAPS_OVERHEAD;
2325
2326 netif_carrier_off(dev);
2327
2328 memcpy(dev->dev_addr, wiphy->perm_addr, ETH_ALEN);
2329
2330 dev->base_addr = base_addr;
2331 dev->irq = irq;
2332
2333 SET_NETDEV_DEV(dev, priv->dev);
2334 ret = register_netdev(dev);
2335 if (ret)
2336 goto fail;
2337
2338 priv->ndev = dev;
2339
2340 /* Report what we've done */
2341 dev_dbg(priv->dev, "Registerred interface %s.\n", dev->name);
2342
2343 return 0;
2344
2345 fail:
2346 free_netdev(dev);
2347 return ret;
2348}
2349EXPORT_SYMBOL(orinoco_if_add);
2350
2351void orinoco_if_del(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
David Kilroya2608362009-06-18 23:21:23 +01002353 struct net_device *dev = priv->ndev;
David Kilroy53819562009-06-18 23:21:28 +01002354
2355 unregister_netdev(dev);
2356 free_netdev(dev);
2357}
2358EXPORT_SYMBOL(orinoco_if_del);
2359
2360void free_orinocodev(struct orinoco_private *priv)
2361{
David Kilroyea60a6a2009-06-18 23:21:26 +01002362 struct wiphy *wiphy = priv_to_wiphy(priv);
David Kilroy20953ad2009-01-07 00:23:55 +00002363 struct orinoco_rx_data *rx_data, *temp;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002364
David Kilroyea60a6a2009-06-18 23:21:26 +01002365 wiphy_unregister(wiphy);
2366
David Kilroy20953ad2009-01-07 00:23:55 +00002367 /* If the tasklet is scheduled when we call tasklet_kill it
2368 * will run one final time. However the tasklet will only
2369 * drain priv->rx_list if the hw is still available. */
David Kilroy31afcef2008-08-21 23:28:04 +01002370 tasklet_kill(&priv->rx_tasklet);
David Kilroy74734312008-11-22 10:37:25 +00002371
David Kilroy20953ad2009-01-07 00:23:55 +00002372 /* Explicitly drain priv->rx_list */
2373 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
2374 list_del(&rx_data->list);
2375
2376 dev_kfree_skb(rx_data->skb);
2377 kfree(rx_data->desc);
2378 kfree(rx_data);
2379 }
2380
David S. Millerf11c1792009-02-25 00:02:05 -08002381 orinoco_unregister_pm_notifier(priv);
David Kilroy74734312008-11-22 10:37:25 +00002382 orinoco_uncache_fw(priv);
2383
David Kilroyd03032a2008-08-21 23:28:02 +01002384 priv->wpa_ie_len = 0;
2385 kfree(priv->wpa_ie);
David Kilroy23edcc42008-08-21 23:28:05 +01002386 orinoco_mic_free(priv);
Dan Williams1e3428e2007-10-10 23:56:25 -04002387 orinoco_bss_data_free(priv);
David Kilroyea60a6a2009-06-18 23:21:26 +01002388 wiphy_free(wiphy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389}
David Kilroy21312662009-02-04 23:05:47 +00002390EXPORT_SYMBOL(free_orinocodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02002392static void orinoco_get_drvinfo(struct net_device *dev,
2393 struct ethtool_drvinfo *info)
2394{
David Kilroyea60a6a2009-06-18 23:21:26 +01002395 struct orinoco_private *priv = ndev_priv(dev);
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02002396
2397 strncpy(info->driver, DRIVER_NAME, sizeof(info->driver) - 1);
2398 strncpy(info->version, DRIVER_VERSION, sizeof(info->version) - 1);
2399 strncpy(info->fw_version, priv->fw_name, sizeof(info->fw_version) - 1);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07002400 if (dev->dev.parent)
Kay Sieversfb28ad32008-11-10 13:55:14 -08002401 strncpy(info->bus_info, dev_name(dev->dev.parent),
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02002402 sizeof(info->bus_info) - 1);
2403 else
2404 snprintf(info->bus_info, sizeof(info->bus_info) - 1,
2405 "PCMCIA %p", priv->hw.iobase);
2406}
2407
Jeff Garzik7282d492006-09-13 14:30:00 -04002408static const struct ethtool_ops orinoco_ethtool_ops = {
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02002409 .get_drvinfo = orinoco_get_drvinfo,
2410 .get_link = ethtool_op_get_link,
2411};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
2413/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414/* Module initialization */
2415/********************************************************************/
2416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417/* Can't be declared "const" or the whole __initdata section will
2418 * become const */
2419static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
2420 " (David Gibson <hermes@gibson.dropbear.id.au>, "
2421 "Pavel Roskin <proski@gnu.org>, et al)";
2422
2423static int __init init_orinoco(void)
2424{
2425 printk(KERN_DEBUG "%s\n", version);
2426 return 0;
2427}
2428
2429static void __exit exit_orinoco(void)
2430{
2431}
2432
2433module_init(init_orinoco);
2434module_exit(exit_orinoco);