blob: 0727b41a397e21cf27dd28f4e348611daaf1adb4 [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);
David Kilroy6415f7d2009-06-18 23:21:30 +0100214static int __orinoco_up(struct orinoco_private *priv);
215static int __orinoco_down(struct orinoco_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217/********************************************************************/
218/* Internal helper functions */
219/********************************************************************/
220
David Kilroycb1576a2009-02-04 23:05:56 +0000221void set_port_type(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 switch (priv->iw_mode) {
224 case IW_MODE_INFRA:
225 priv->port_type = 1;
226 priv->createibss = 0;
227 break;
228 case IW_MODE_ADHOC:
229 if (priv->prefer_port3) {
230 priv->port_type = 3;
231 priv->createibss = 0;
232 } else {
233 priv->port_type = priv->ibss_port;
234 priv->createibss = 1;
235 }
236 break;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200237 case IW_MODE_MONITOR:
238 priv->port_type = 3;
239 priv->createibss = 0;
240 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 default:
242 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
243 priv->ndev->name);
244 }
245}
246
David Kilroy3994d502008-08-21 23:27:54 +0100247/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/* Device methods */
249/********************************************************************/
250
251static int orinoco_open(struct net_device *dev)
252{
David Kilroyea60a6a2009-06-18 23:21:26 +0100253 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 unsigned long flags;
255 int err;
256
257 if (orinoco_lock(priv, &flags) != 0)
258 return -EBUSY;
259
David Kilroya2608362009-06-18 23:21:23 +0100260 err = __orinoco_up(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
David Kilroya94e8422009-02-04 23:05:44 +0000262 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 priv->open = 1;
264
265 orinoco_unlock(priv, &flags);
266
267 return err;
268}
269
Christoph Hellwigad8f4512005-05-14 17:30:17 +0200270static int orinoco_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
David Kilroyea60a6a2009-06-18 23:21:26 +0100272 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 int err = 0;
274
275 /* We mustn't use orinoco_lock() here, because we need to be
276 able to close the interface even if hw_unavailable is set
277 (e.g. as we're released after a PC Card removal) */
278 spin_lock_irq(&priv->lock);
279
280 priv->open = 0;
281
David Kilroya2608362009-06-18 23:21:23 +0100282 err = __orinoco_down(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 spin_unlock_irq(&priv->lock);
285
286 return err;
287}
288
289static struct net_device_stats *orinoco_get_stats(struct net_device *dev)
290{
David Kilroyea60a6a2009-06-18 23:21:26 +0100291 struct orinoco_private *priv = ndev_priv(dev);
David Kilroy6fe9deb2009-02-04 23:05:43 +0000292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return &priv->stats;
294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static void orinoco_set_multicast_list(struct net_device *dev)
297{
David Kilroyea60a6a2009-06-18 23:21:26 +0100298 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 unsigned long flags;
300
301 if (orinoco_lock(priv, &flags) != 0) {
302 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
303 "called when hw_unavailable\n", dev->name);
304 return;
305 }
306
307 __orinoco_set_multicast_list(dev);
308 orinoco_unlock(priv, &flags);
309}
310
311static int orinoco_change_mtu(struct net_device *dev, int new_mtu)
312{
David Kilroyea60a6a2009-06-18 23:21:26 +0100313 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
David Kilroya94e8422009-02-04 23:05:44 +0000315 if ((new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return -EINVAL;
317
Johannes Berg2c7060022008-10-30 22:09:54 +0100318 /* MTU + encapsulation + header length */
David Kilroya94e8422009-02-04 23:05:44 +0000319 if ((new_mtu + ENCAPS_OVERHEAD + sizeof(struct ieee80211_hdr)) >
320 (priv->nicbuf_size - ETH_HLEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return -EINVAL;
322
323 dev->mtu = new_mtu;
324
325 return 0;
326}
327
328/********************************************************************/
329/* Tx path */
330/********************************************************************/
331
332static int orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
333{
David Kilroyea60a6a2009-06-18 23:21:26 +0100334 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 struct net_device_stats *stats = &priv->stats;
336 hermes_t *hw = &priv->hw;
337 int err = 0;
338 u16 txfid = priv->txfid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 struct ethhdr *eh;
David Kilroy6eecad72008-08-21 23:27:56 +0100340 int tx_control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 unsigned long flags;
342
David Kilroya94e8422009-02-04 23:05:44 +0000343 if (!netif_running(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 printk(KERN_ERR "%s: Tx on stopped device!\n",
345 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400346 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
David Kilroy6fe9deb2009-02-04 23:05:43 +0000348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (netif_queue_stopped(dev)) {
David Kilroy6fe9deb2009-02-04 23:05:43 +0000350 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400352 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
David Kilroy6fe9deb2009-02-04 23:05:43 +0000354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (orinoco_lock(priv, &flags) != 0) {
356 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
357 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400358 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360
David Kilroya94e8422009-02-04 23:05:44 +0000361 if (!netif_carrier_ok(dev) || (priv->iw_mode == IW_MODE_MONITOR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /* Oops, the firmware hasn't established a connection,
David Kilroy6fe9deb2009-02-04 23:05:43 +0000363 silently drop the packet (this seems to be the
364 safest approach). */
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400365 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367
Pavel Roskin8d5be082006-04-07 04:10:41 -0400368 /* Check packet length */
Pavel Roskina28dc812006-04-07 04:10:45 -0400369 if (skb->len < ETH_HLEN)
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400370 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
David Kilroy6eecad72008-08-21 23:27:56 +0100372 tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
David Kilroy23edcc42008-08-21 23:28:05 +0100374 if (priv->encode_alg == IW_ENCODE_ALG_TKIP)
375 tx_control |= (priv->tx_key << HERMES_MIC_KEY_ID_SHIFT) |
376 HERMES_TXCTRL_MIC;
377
David Kilroy6eecad72008-08-21 23:27:56 +0100378 if (priv->has_alt_txcntl) {
379 /* WPA enabled firmwares have tx_cntl at the end of
380 * the 802.11 header. So write zeroed descriptor and
381 * 802.11 header at the same time
382 */
383 char desc[HERMES_802_3_OFFSET];
384 __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
385
386 memset(&desc, 0, sizeof(desc));
387
388 *txcntl = cpu_to_le16(tx_control);
389 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
390 txfid, 0);
391 if (err) {
392 if (net_ratelimit())
393 printk(KERN_ERR "%s: Error %d writing Tx "
394 "descriptor to BAP\n", dev->name, err);
395 goto busy;
396 }
397 } else {
398 struct hermes_tx_descriptor desc;
399
400 memset(&desc, 0, sizeof(desc));
401
402 desc.tx_control = cpu_to_le16(tx_control);
403 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
404 txfid, 0);
405 if (err) {
406 if (net_ratelimit())
407 printk(KERN_ERR "%s: Error %d writing Tx "
408 "descriptor to BAP\n", dev->name, err);
409 goto busy;
410 }
411
412 /* Clear the 802.11 header and data length fields - some
413 * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
414 * if this isn't done. */
415 hermes_clear_words(hw, HERMES_DATA0,
416 HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
David Kilroy23edcc42008-08-21 23:28:05 +0100419 eh = (struct ethhdr *)skb->data;
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /* Encapsulate Ethernet-II frames */
422 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
Pavel Roskina28dc812006-04-07 04:10:45 -0400423 struct header_struct {
424 struct ethhdr eth; /* 802.3 header */
425 u8 encap[6]; /* 802.2 header */
426 } __attribute__ ((packed)) hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Pavel Roskina28dc812006-04-07 04:10:45 -0400428 /* Strip destination and source from the data */
429 skb_pull(skb, 2 * ETH_ALEN);
Pavel Roskina28dc812006-04-07 04:10:45 -0400430
431 /* And move them to a separate header */
432 memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
433 hdr.eth.h_proto = htons(sizeof(encaps_hdr) + skb->len);
434 memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
435
David Kilroy23edcc42008-08-21 23:28:05 +0100436 /* Insert the SNAP header */
437 if (skb_headroom(skb) < sizeof(hdr)) {
438 printk(KERN_ERR
439 "%s: Not enough headroom for 802.2 headers %d\n",
440 dev->name, skb_headroom(skb));
441 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
David Kilroy23edcc42008-08-21 23:28:05 +0100443 eh = (struct ethhdr *) skb_push(skb, sizeof(hdr));
444 memcpy(eh, &hdr, sizeof(hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446
Pavel Roskina28dc812006-04-07 04:10:45 -0400447 err = hermes_bap_pwrite(hw, USER_BAP, skb->data, skb->len,
David Kilroy23edcc42008-08-21 23:28:05 +0100448 txfid, HERMES_802_3_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (err) {
450 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
451 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400452 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454
David Kilroy23edcc42008-08-21 23:28:05 +0100455 /* Calculate Michael MIC */
456 if (priv->encode_alg == IW_ENCODE_ALG_TKIP) {
457 u8 mic_buf[MICHAEL_MIC_LEN + 1];
458 u8 *mic;
459 size_t offset;
460 size_t len;
461
462 if (skb->len % 2) {
463 /* MIC start is on an odd boundary */
464 mic_buf[0] = skb->data[skb->len - 1];
465 mic = &mic_buf[1];
466 offset = skb->len - 1;
467 len = MICHAEL_MIC_LEN + 1;
468 } else {
469 mic = &mic_buf[0];
470 offset = skb->len;
471 len = MICHAEL_MIC_LEN;
472 }
473
David Kilroy4adb4742009-02-04 23:05:51 +0000474 orinoco_mic(priv->tx_tfm_mic,
David Kilroy23edcc42008-08-21 23:28:05 +0100475 priv->tkip_key[priv->tx_key].tx_mic,
476 eh->h_dest, eh->h_source, 0 /* priority */,
477 skb->data + ETH_HLEN, skb->len - ETH_HLEN, mic);
478
479 /* Write the MIC */
480 err = hermes_bap_pwrite(hw, USER_BAP, &mic_buf[0], len,
481 txfid, HERMES_802_3_OFFSET + offset);
482 if (err) {
483 printk(KERN_ERR "%s: Error %d writing MIC to BAP\n",
484 dev->name, err);
485 goto busy;
486 }
487 }
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /* Finally, we actually initiate the send */
490 netif_stop_queue(dev);
491
492 err = hermes_docmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
493 txfid, NULL);
494 if (err) {
495 netif_start_queue(dev);
Andrew Mortonc367c212005-10-19 21:23:44 -0700496 if (net_ratelimit())
497 printk(KERN_ERR "%s: Error %d transmitting packet\n",
498 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400499 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501
502 dev->trans_start = jiffies;
David Kilroy23edcc42008-08-21 23:28:05 +0100503 stats->tx_bytes += HERMES_802_3_OFFSET + skb->len;
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400504 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400506 drop:
507 stats->tx_errors++;
508 stats->tx_dropped++;
509
510 ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 dev_kfree_skb(skb);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400513 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400515 busy:
Jiri Benc2c1bd262006-04-07 04:10:47 -0400516 if (err == -EIO)
517 schedule_work(&priv->reset_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 orinoco_unlock(priv, &flags);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400519 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
523{
David Kilroyea60a6a2009-06-18 23:21:26 +0100524 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 u16 fid = hermes_read_regn(hw, ALLOCFID);
526
527 if (fid != priv->txfid) {
528 if (fid != DUMMY_FID)
529 printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
530 dev->name, fid);
531 return;
532 }
533
534 hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
535}
536
537static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
538{
David Kilroyea60a6a2009-06-18 23:21:26 +0100539 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 struct net_device_stats *stats = &priv->stats;
541
542 stats->tx_packets++;
543
544 netif_wake_queue(dev);
545
546 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
547}
548
549static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
550{
David Kilroyea60a6a2009-06-18 23:21:26 +0100551 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 struct net_device_stats *stats = &priv->stats;
553 u16 fid = hermes_read_regn(hw, TXCOMPLFID);
Pavel Roskind133ae42005-09-23 04:18:06 -0400554 u16 status;
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400555 struct hermes_txexc_data hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 int err = 0;
557
558 if (fid == DUMMY_FID)
559 return; /* Nothing's really happened */
560
Pavel Roskin48ca7032005-09-23 04:18:06 -0400561 /* Read part of the frame header - we need status and addr1 */
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200562 err = hermes_bap_pread(hw, IRQ_BAP, &hdr,
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400563 sizeof(struct hermes_txexc_data),
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200564 fid, 0);
565
566 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
567 stats->tx_errors++;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (err) {
570 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
571 "(FID=%04X error %d)\n",
572 dev->name, fid, err);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200573 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
David Kilroy6fe9deb2009-02-04 23:05:43 +0000575
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200576 DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
577 err, fid);
David Kilroy6fe9deb2009-02-04 23:05:43 +0000578
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200579 /* We produce a TXDROP event only for retry or lifetime
580 * exceeded, because that's the only status that really mean
581 * that this particular node went away.
582 * Other errors means that *we* screwed up. - Jean II */
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400583 status = le16_to_cpu(hdr.desc.status);
Pavel Roskind133ae42005-09-23 04:18:06 -0400584 if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200585 union iwreq_data wrqu;
586
587 /* Copy 802.11 dest address.
588 * We use the 802.11 header because the frame may
589 * not be 802.3 or may be mangled...
590 * In Ad-Hoc mode, it will be the node address.
591 * In managed mode, it will be most likely the AP addr
592 * User space will figure out how to convert it to
593 * whatever it needs (IP address or else).
594 * - Jean II */
595 memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
596 wrqu.addr.sa_family = ARPHRD_ETHER;
597
598 /* Send event to user space */
599 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605static void orinoco_tx_timeout(struct net_device *dev)
606{
David Kilroyea60a6a2009-06-18 23:21:26 +0100607 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 struct net_device_stats *stats = &priv->stats;
609 struct hermes *hw = &priv->hw;
610
611 printk(KERN_WARNING "%s: Tx timeout! "
612 "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
613 dev->name, hermes_read_regn(hw, ALLOCFID),
614 hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
615
616 stats->tx_errors++;
617
618 schedule_work(&priv->reset_work);
619}
620
621/********************************************************************/
622/* Rx path (data frames) */
623/********************************************************************/
624
625/* Does the frame have a SNAP header indicating it should be
626 * de-encapsulated to Ethernet-II? */
627static inline int is_ethersnap(void *_hdr)
628{
629 u8 *hdr = _hdr;
630
631 /* We de-encapsulate all packets which, a) have SNAP headers
632 * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
633 * and where b) the OUI of the SNAP header is 00:00:00 or
634 * 00:00:f8 - we need both because different APs appear to use
635 * different OUIs for some reason */
636 return (memcmp(hdr, &encaps_hdr, 5) == 0)
David Kilroya94e8422009-02-04 23:05:44 +0000637 && ((hdr[5] == 0x00) || (hdr[5] == 0xf8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
640static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
641 int level, int noise)
642{
Pavel Roskin343c6862005-09-09 18:43:02 -0400643 struct iw_quality wstats;
644 wstats.level = level - 0x95;
645 wstats.noise = noise - 0x95;
646 wstats.qual = (level > noise) ? (level - noise) : 0;
Andrey Borzenkovf941f852008-11-15 17:15:09 +0300647 wstats.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
Pavel Roskin343c6862005-09-09 18:43:02 -0400648 /* Update spy records */
649 wireless_spy_update(dev, mac, &wstats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652static void orinoco_stat_gather(struct net_device *dev,
653 struct sk_buff *skb,
654 struct hermes_rx_descriptor *desc)
655{
David Kilroyea60a6a2009-06-18 23:21:26 +0100656 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 /* Using spy support with lots of Rx packets, like in an
659 * infrastructure (AP), will really slow down everything, because
660 * the MAC address must be compared to each entry of the spy list.
661 * If the user really asks for it (set some address in the
662 * spy list), we do it, but he will pay the price.
663 * Note that to get here, you need both WIRELESS_SPY
664 * compiled in AND some addresses in the list !!!
665 */
666 /* Note : gcc will optimise the whole section away if
667 * WIRELESS_SPY is not defined... - Jean II */
668 if (SPY_NUMBER(priv)) {
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700669 orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 desc->signal, desc->silence);
671 }
672}
673
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200674/*
675 * orinoco_rx_monitor - handle received monitor frames.
676 *
677 * Arguments:
678 * dev network device
679 * rxfid received FID
680 * desc rx descriptor of the frame
681 *
682 * Call context: interrupt
683 */
684static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
685 struct hermes_rx_descriptor *desc)
686{
687 u32 hdrlen = 30; /* return full header by default */
688 u32 datalen = 0;
689 u16 fc;
690 int err;
691 int len;
692 struct sk_buff *skb;
David Kilroyea60a6a2009-06-18 23:21:26 +0100693 struct orinoco_private *priv = ndev_priv(dev);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200694 struct net_device_stats *stats = &priv->stats;
695 hermes_t *hw = &priv->hw;
696
697 len = le16_to_cpu(desc->data_len);
698
699 /* Determine the size of the header and the data */
700 fc = le16_to_cpu(desc->frame_ctl);
701 switch (fc & IEEE80211_FCTL_FTYPE) {
702 case IEEE80211_FTYPE_DATA:
703 if ((fc & IEEE80211_FCTL_TODS)
704 && (fc & IEEE80211_FCTL_FROMDS))
705 hdrlen = 30;
706 else
707 hdrlen = 24;
708 datalen = len;
709 break;
710 case IEEE80211_FTYPE_MGMT:
711 hdrlen = 24;
712 datalen = len;
713 break;
714 case IEEE80211_FTYPE_CTL:
715 switch (fc & IEEE80211_FCTL_STYPE) {
716 case IEEE80211_STYPE_PSPOLL:
717 case IEEE80211_STYPE_RTS:
718 case IEEE80211_STYPE_CFEND:
719 case IEEE80211_STYPE_CFENDACK:
720 hdrlen = 16;
721 break;
722 case IEEE80211_STYPE_CTS:
723 case IEEE80211_STYPE_ACK:
724 hdrlen = 10;
725 break;
726 }
727 break;
728 default:
729 /* Unknown frame type */
730 break;
731 }
732
733 /* sanity check the length */
Johannes Berg2c7060022008-10-30 22:09:54 +0100734 if (datalen > IEEE80211_MAX_DATA_LEN + 12) {
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200735 printk(KERN_DEBUG "%s: oversized monitor frame, "
736 "data length = %d\n", dev->name, datalen);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200737 stats->rx_length_errors++;
738 goto update_stats;
739 }
740
741 skb = dev_alloc_skb(hdrlen + datalen);
742 if (!skb) {
743 printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
744 dev->name);
Florin Malitabb6e0932006-05-22 22:35:30 -0700745 goto update_stats;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200746 }
747
748 /* Copy the 802.11 header to the skb */
749 memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700750 skb_reset_mac_header(skb);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200751
752 /* If any, copy the data from the card to the skb */
753 if (datalen > 0) {
754 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
755 ALIGN(datalen, 2), rxfid,
756 HERMES_802_2_OFFSET);
757 if (err) {
758 printk(KERN_ERR "%s: error %d reading monitor frame\n",
759 dev->name, err);
760 goto drop;
761 }
762 }
763
764 skb->dev = dev;
765 skb->ip_summed = CHECKSUM_NONE;
766 skb->pkt_type = PACKET_OTHERHOST;
Harvey Harrisonc1b4aa32009-01-29 13:26:44 -0800767 skb->protocol = cpu_to_be16(ETH_P_802_2);
David Kilroy6fe9deb2009-02-04 23:05:43 +0000768
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200769 stats->rx_packets++;
770 stats->rx_bytes += skb->len;
771
772 netif_rx(skb);
773 return;
774
775 drop:
776 dev_kfree_skb_irq(skb);
777 update_stats:
778 stats->rx_errors++;
779 stats->rx_dropped++;
780}
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782static void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
783{
David Kilroyea60a6a2009-06-18 23:21:26 +0100784 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 struct net_device_stats *stats = &priv->stats;
786 struct iw_statistics *wstats = &priv->wstats;
787 struct sk_buff *skb = NULL;
David Kilroy31afcef2008-08-21 23:28:04 +0100788 u16 rxfid, status;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200789 int length;
David Kilroy31afcef2008-08-21 23:28:04 +0100790 struct hermes_rx_descriptor *desc;
791 struct orinoco_rx_data *rx_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 int err;
793
David Kilroy31afcef2008-08-21 23:28:04 +0100794 desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
795 if (!desc) {
796 printk(KERN_WARNING
797 "%s: Can't allocate space for RX descriptor\n",
798 dev->name);
799 goto update_stats;
800 }
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 rxfid = hermes_read_regn(hw, RXFID);
803
David Kilroy31afcef2008-08-21 23:28:04 +0100804 err = hermes_bap_pread(hw, IRQ_BAP, desc, sizeof(*desc),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 rxfid, 0);
806 if (err) {
807 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
808 "Frame dropped.\n", dev->name, err);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200809 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811
David Kilroy31afcef2008-08-21 23:28:04 +0100812 status = le16_to_cpu(desc->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200814 if (status & HERMES_RXSTAT_BADCRC) {
815 DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
816 dev->name);
817 stats->rx_crc_errors++;
818 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200821 /* Handle frames in monitor mode */
822 if (priv->iw_mode == IW_MODE_MONITOR) {
David Kilroy31afcef2008-08-21 23:28:04 +0100823 orinoco_rx_monitor(dev, rxfid, desc);
824 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200827 if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
828 DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
829 dev->name);
830 wstats->discard.code++;
831 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833
David Kilroy31afcef2008-08-21 23:28:04 +0100834 length = le16_to_cpu(desc->data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /* Sanity checks */
837 if (length < 3) { /* No for even an 802.2 LLC header */
838 /* At least on Symbol firmware with PCF we get quite a
David Kilroy6fe9deb2009-02-04 23:05:43 +0000839 lot of these legitimately - Poll frames with no
840 data. */
David Kilroy31afcef2008-08-21 23:28:04 +0100841 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
Johannes Berg2c7060022008-10-30 22:09:54 +0100843 if (length > IEEE80211_MAX_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
845 dev->name, length);
846 stats->rx_length_errors++;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200847 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849
David Kilroy23edcc42008-08-21 23:28:05 +0100850 /* Payload size does not include Michael MIC. Increase payload
851 * size to read it together with the data. */
852 if (status & HERMES_RXSTAT_MIC)
853 length += MICHAEL_MIC_LEN;
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /* We need space for the packet data itself, plus an ethernet
856 header, plus 2 bytes so we can align the IP header on a
857 32bit boundary, plus 1 byte so we can read in odd length
858 packets from the card, which has an IO granularity of 16
David Kilroy6fe9deb2009-02-04 23:05:43 +0000859 bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 skb = dev_alloc_skb(length+ETH_HLEN+2+1);
861 if (!skb) {
862 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
863 dev->name);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200864 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200867 /* We'll prepend the header, so reserve space for it. The worst
868 case is no decapsulation, when 802.3 header is prepended and
869 nothing is removed. 2 is for aligning the IP header. */
870 skb_reserve(skb, ETH_HLEN + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200872 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, length),
873 ALIGN(length, 2), rxfid,
874 HERMES_802_2_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (err) {
876 printk(KERN_ERR "%s: error %d reading frame. "
877 "Frame dropped.\n", dev->name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 goto drop;
879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
David Kilroy31afcef2008-08-21 23:28:04 +0100881 /* Add desc and skb to rx queue */
882 rx_data = kzalloc(sizeof(*rx_data), GFP_ATOMIC);
883 if (!rx_data) {
884 printk(KERN_WARNING "%s: Can't allocate RX packet\n",
885 dev->name);
886 goto drop;
887 }
888 rx_data->desc = desc;
889 rx_data->skb = skb;
890 list_add_tail(&rx_data->list, &priv->rx_list);
891 tasklet_schedule(&priv->rx_tasklet);
892
893 return;
894
895drop:
896 dev_kfree_skb_irq(skb);
897update_stats:
898 stats->rx_errors++;
899 stats->rx_dropped++;
900out:
901 kfree(desc);
902}
903
904static void orinoco_rx(struct net_device *dev,
905 struct hermes_rx_descriptor *desc,
906 struct sk_buff *skb)
907{
David Kilroyea60a6a2009-06-18 23:21:26 +0100908 struct orinoco_private *priv = ndev_priv(dev);
David Kilroy31afcef2008-08-21 23:28:04 +0100909 struct net_device_stats *stats = &priv->stats;
910 u16 status, fc;
911 int length;
912 struct ethhdr *hdr;
913
914 status = le16_to_cpu(desc->status);
915 length = le16_to_cpu(desc->data_len);
916 fc = le16_to_cpu(desc->frame_ctl);
917
David Kilroy23edcc42008-08-21 23:28:05 +0100918 /* Calculate and check MIC */
919 if (status & HERMES_RXSTAT_MIC) {
920 int key_id = ((status & HERMES_RXSTAT_MIC_KEY_ID) >>
921 HERMES_MIC_KEY_ID_SHIFT);
922 u8 mic[MICHAEL_MIC_LEN];
923 u8 *rxmic;
924 u8 *src = (fc & IEEE80211_FCTL_FROMDS) ?
925 desc->addr3 : desc->addr2;
926
927 /* Extract Michael MIC from payload */
928 rxmic = skb->data + skb->len - MICHAEL_MIC_LEN;
929
930 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
931 length -= MICHAEL_MIC_LEN;
932
David Kilroy4adb4742009-02-04 23:05:51 +0000933 orinoco_mic(priv->rx_tfm_mic,
David Kilroy23edcc42008-08-21 23:28:05 +0100934 priv->tkip_key[key_id].rx_mic,
935 desc->addr1,
936 src,
937 0, /* priority or QoS? */
938 skb->data,
939 skb->len,
940 &mic[0]);
941
942 if (memcmp(mic, rxmic,
943 MICHAEL_MIC_LEN)) {
944 union iwreq_data wrqu;
945 struct iw_michaelmicfailure wxmic;
David Kilroy23edcc42008-08-21 23:28:05 +0100946
947 printk(KERN_WARNING "%s: "
Johannes Berge1749612008-10-27 15:59:26 -0700948 "Invalid Michael MIC in data frame from %pM, "
David Kilroy23edcc42008-08-21 23:28:05 +0100949 "using key %i\n",
Johannes Berge1749612008-10-27 15:59:26 -0700950 dev->name, src, key_id);
David Kilroy23edcc42008-08-21 23:28:05 +0100951
952 /* TODO: update stats */
953
954 /* Notify userspace */
955 memset(&wxmic, 0, sizeof(wxmic));
956 wxmic.flags = key_id & IW_MICFAILURE_KEY_ID;
957 wxmic.flags |= (desc->addr1[0] & 1) ?
958 IW_MICFAILURE_GROUP : IW_MICFAILURE_PAIRWISE;
959 wxmic.src_addr.sa_family = ARPHRD_ETHER;
960 memcpy(wxmic.src_addr.sa_data, src, ETH_ALEN);
961
962 (void) orinoco_hw_get_tkip_iv(priv, key_id,
963 &wxmic.tsc[0]);
964
965 memset(&wrqu, 0, sizeof(wrqu));
966 wrqu.data.length = sizeof(wxmic);
967 wireless_send_event(dev, IWEVMICHAELMICFAILURE, &wrqu,
968 (char *) &wxmic);
969
970 goto drop;
971 }
972 }
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 /* Handle decapsulation
975 * In most cases, the firmware tell us about SNAP frames.
976 * For some reason, the SNAP frames sent by LinkSys APs
977 * are not properly recognised by most firmwares.
978 * So, check ourselves */
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200979 if (length >= ENCAPS_OVERHEAD &&
980 (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
981 ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
982 is_ethersnap(skb->data))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 /* These indicate a SNAP within 802.2 LLC within
984 802.11 frame which we'll need to de-encapsulate to
985 the original EthernetII frame. */
David Kilroyb2f30a02009-02-04 23:05:46 +0000986 hdr = (struct ethhdr *)skb_push(skb,
987 ETH_HLEN - ENCAPS_OVERHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 } else {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200989 /* 802.3 frame - prepend 802.3 header as is */
990 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
991 hdr->h_proto = htons(length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
David Kilroy31afcef2008-08-21 23:28:04 +0100993 memcpy(hdr->h_dest, desc->addr1, ETH_ALEN);
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200994 if (fc & IEEE80211_FCTL_FROMDS)
David Kilroy31afcef2008-08-21 23:28:04 +0100995 memcpy(hdr->h_source, desc->addr3, ETH_ALEN);
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200996 else
David Kilroy31afcef2008-08-21 23:28:04 +0100997 memcpy(hdr->h_source, desc->addr2, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 skb->protocol = eth_type_trans(skb, dev);
1000 skb->ip_summed = CHECKSUM_NONE;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001001 if (fc & IEEE80211_FCTL_TODS)
1002 skb->pkt_type = PACKET_OTHERHOST;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 /* Process the wireless stats if needed */
David Kilroy31afcef2008-08-21 23:28:04 +01001005 orinoco_stat_gather(dev, skb, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 /* Pass the packet to the networking stack */
1008 netif_rx(skb);
1009 stats->rx_packets++;
1010 stats->rx_bytes += length;
1011
1012 return;
David Kilroy23edcc42008-08-21 23:28:05 +01001013
1014 drop:
1015 dev_kfree_skb(skb);
1016 stats->rx_errors++;
1017 stats->rx_dropped++;
David Kilroy31afcef2008-08-21 23:28:04 +01001018}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
David Kilroy31afcef2008-08-21 23:28:04 +01001020static void orinoco_rx_isr_tasklet(unsigned long data)
1021{
David Kilroy53819562009-06-18 23:21:28 +01001022 struct orinoco_private *priv = (struct orinoco_private *) data;
1023 struct net_device *dev = priv->ndev;
David Kilroy31afcef2008-08-21 23:28:04 +01001024 struct orinoco_rx_data *rx_data, *temp;
1025 struct hermes_rx_descriptor *desc;
1026 struct sk_buff *skb;
David Kilroy20953ad2009-01-07 00:23:55 +00001027 unsigned long flags;
1028
1029 /* orinoco_rx requires the driver lock, and we also need to
1030 * protect priv->rx_list, so just hold the lock over the
1031 * lot.
1032 *
1033 * If orinoco_lock fails, we've unplugged the card. In this
1034 * case just abort. */
1035 if (orinoco_lock(priv, &flags) != 0)
1036 return;
David Kilroy31afcef2008-08-21 23:28:04 +01001037
1038 /* extract desc and skb from queue */
1039 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
1040 desc = rx_data->desc;
1041 skb = rx_data->skb;
1042 list_del(&rx_data->list);
1043 kfree(rx_data);
1044
1045 orinoco_rx(dev, desc, skb);
1046
1047 kfree(desc);
1048 }
David Kilroy20953ad2009-01-07 00:23:55 +00001049
1050 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053/********************************************************************/
1054/* Rx path (info frames) */
1055/********************************************************************/
1056
1057static void print_linkstatus(struct net_device *dev, u16 status)
1058{
David Kilroy21312662009-02-04 23:05:47 +00001059 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 if (suppress_linkstatus)
1062 return;
1063
1064 switch (status) {
1065 case HERMES_LINKSTATUS_NOT_CONNECTED:
1066 s = "Not Connected";
1067 break;
1068 case HERMES_LINKSTATUS_CONNECTED:
1069 s = "Connected";
1070 break;
1071 case HERMES_LINKSTATUS_DISCONNECTED:
1072 s = "Disconnected";
1073 break;
1074 case HERMES_LINKSTATUS_AP_CHANGE:
1075 s = "AP Changed";
1076 break;
1077 case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1078 s = "AP Out of Range";
1079 break;
1080 case HERMES_LINKSTATUS_AP_IN_RANGE:
1081 s = "AP In Range";
1082 break;
1083 case HERMES_LINKSTATUS_ASSOC_FAILED:
1084 s = "Association Failed";
1085 break;
1086 default:
1087 s = "UNKNOWN";
1088 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001089
Pavel Roskin11eaea42009-01-18 23:20:58 -05001090 printk(KERN_DEBUG "%s: New link status: %s (%04x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 dev->name, s, status);
1092}
1093
Christoph Hellwig16739b02005-06-19 01:27:51 +02001094/* Search scan results for requested BSSID, join it if found */
David Howellsc4028952006-11-22 14:57:56 +00001095static void orinoco_join_ap(struct work_struct *work)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001096{
David Howellsc4028952006-11-22 14:57:56 +00001097 struct orinoco_private *priv =
1098 container_of(work, struct orinoco_private, join_work);
1099 struct net_device *dev = priv->ndev;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001100 struct hermes *hw = &priv->hw;
1101 int err;
1102 unsigned long flags;
1103 struct join_req {
1104 u8 bssid[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -04001105 __le16 channel;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001106 } __attribute__ ((packed)) req;
1107 const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001108 struct prism2_scan_apinfo *atom = NULL;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001109 int offset = 4;
Pavel Roskinc89cc222005-09-01 20:06:06 -04001110 int found = 0;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001111 u8 *buf;
1112 u16 len;
1113
1114 /* Allocate buffer for scan results */
1115 buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
David Kilroya94e8422009-02-04 23:05:44 +00001116 if (!buf)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001117 return;
1118
1119 if (orinoco_lock(priv, &flags) != 0)
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001120 goto fail_lock;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001121
1122 /* Sanity checks in case user changed something in the meantime */
David Kilroya94e8422009-02-04 23:05:44 +00001123 if (!priv->bssid_fixed)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001124 goto out;
1125
1126 if (strlen(priv->desired_essid) == 0)
1127 goto out;
1128
1129 /* Read scan results from the firmware */
1130 err = hermes_read_ltv(hw, USER_BAP,
1131 HERMES_RID_SCANRESULTSTABLE,
1132 MAX_SCAN_LEN, &len, buf);
1133 if (err) {
1134 printk(KERN_ERR "%s: Cannot read scan results\n",
1135 dev->name);
1136 goto out;
1137 }
1138
1139 len = HERMES_RECLEN_TO_BYTES(len);
1140
1141 /* Go through the scan results looking for the channel of the AP
1142 * we were requested to join */
1143 for (; offset + atom_len <= len; offset += atom_len) {
1144 atom = (struct prism2_scan_apinfo *) (buf + offset);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001145 if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1146 found = 1;
1147 break;
1148 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001149 }
1150
David Kilroya94e8422009-02-04 23:05:44 +00001151 if (!found) {
Pavel Roskinc89cc222005-09-01 20:06:06 -04001152 DEBUG(1, "%s: Requested AP not found in scan results\n",
1153 dev->name);
1154 goto out;
1155 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001156
Christoph Hellwig16739b02005-06-19 01:27:51 +02001157 memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1158 req.channel = atom->channel; /* both are little-endian */
1159 err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1160 &req);
1161 if (err)
1162 printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1163
1164 out:
Christoph Hellwig16739b02005-06-19 01:27:51 +02001165 orinoco_unlock(priv, &flags);
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001166
1167 fail_lock:
1168 kfree(buf);
Christoph Hellwig16739b02005-06-19 01:27:51 +02001169}
1170
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001171/* Send new BSSID to userspace */
David Kilroy6cd90b12008-08-21 23:28:00 +01001172static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001173{
David Howellsc4028952006-11-22 14:57:56 +00001174 struct net_device *dev = priv->ndev;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001175 struct hermes *hw = &priv->hw;
1176 union iwreq_data wrqu;
1177 int err;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001178
David Kilroy499b7022008-12-09 21:46:29 +00001179 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001180 ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
1181 if (err != 0)
David Kilroy6cd90b12008-08-21 23:28:00 +01001182 return;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001183
1184 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1185
1186 /* Send event to user space */
1187 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001188}
1189
David Kilroy06009fd2008-08-21 23:28:03 +01001190static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
1191{
1192 struct net_device *dev = priv->ndev;
1193 struct hermes *hw = &priv->hw;
1194 union iwreq_data wrqu;
1195 int err;
1196 u8 buf[88];
1197 u8 *ie;
1198
1199 if (!priv->has_wpa)
1200 return;
1201
David Kilroy499b7022008-12-09 21:46:29 +00001202 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
David Kilroy06009fd2008-08-21 23:28:03 +01001203 sizeof(buf), NULL, &buf);
1204 if (err != 0)
1205 return;
1206
1207 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1208 if (ie) {
1209 int rem = sizeof(buf) - (ie - &buf[0]);
1210 wrqu.data.length = ie[1] + 2;
1211 if (wrqu.data.length > rem)
1212 wrqu.data.length = rem;
1213
1214 if (wrqu.data.length)
1215 /* Send event to user space */
1216 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
1217 }
1218}
1219
1220static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
1221{
1222 struct net_device *dev = priv->ndev;
1223 struct hermes *hw = &priv->hw;
1224 union iwreq_data wrqu;
1225 int err;
1226 u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
1227 u8 *ie;
1228
1229 if (!priv->has_wpa)
1230 return;
1231
David Kilroy499b7022008-12-09 21:46:29 +00001232 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_RESP_INFO,
David Kilroy06009fd2008-08-21 23:28:03 +01001233 sizeof(buf), NULL, &buf);
1234 if (err != 0)
1235 return;
1236
1237 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1238 if (ie) {
1239 int rem = sizeof(buf) - (ie - &buf[0]);
1240 wrqu.data.length = ie[1] + 2;
1241 if (wrqu.data.length > rem)
1242 wrqu.data.length = rem;
1243
1244 if (wrqu.data.length)
1245 /* Send event to user space */
1246 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
1247 }
1248}
1249
David Kilroy6cd90b12008-08-21 23:28:00 +01001250static void orinoco_send_wevents(struct work_struct *work)
1251{
1252 struct orinoco_private *priv =
1253 container_of(work, struct orinoco_private, wevent_work);
1254 unsigned long flags;
1255
1256 if (orinoco_lock(priv, &flags) != 0)
1257 return;
1258
David Kilroy06009fd2008-08-21 23:28:03 +01001259 orinoco_send_assocreqie_wevent(priv);
1260 orinoco_send_assocrespie_wevent(priv);
David Kilroy6cd90b12008-08-21 23:28:00 +01001261 orinoco_send_bssid_wevent(priv);
1262
1263 orinoco_unlock(priv, &flags);
1264}
Dan Williams1e3428e2007-10-10 23:56:25 -04001265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266static void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
1267{
David Kilroyea60a6a2009-06-18 23:21:26 +01001268 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 u16 infofid;
1270 struct {
Pavel Roskind133ae42005-09-23 04:18:06 -04001271 __le16 len;
1272 __le16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 } __attribute__ ((packed)) info;
1274 int len, type;
1275 int err;
1276
1277 /* This is an answer to an INQUIRE command that we did earlier,
1278 * or an information "event" generated by the card
1279 * The controller return to us a pseudo frame containing
1280 * the information in question - Jean II */
1281 infofid = hermes_read_regn(hw, INFOFID);
1282
1283 /* Read the info frame header - don't try too hard */
1284 err = hermes_bap_pread(hw, IRQ_BAP, &info, sizeof(info),
1285 infofid, 0);
1286 if (err) {
1287 printk(KERN_ERR "%s: error %d reading info frame. "
1288 "Frame dropped.\n", dev->name, err);
1289 return;
1290 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
1293 type = le16_to_cpu(info.type);
1294
1295 switch (type) {
1296 case HERMES_INQ_TALLIES: {
1297 struct hermes_tallies_frame tallies;
1298 struct iw_statistics *wstats = &priv->wstats;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (len > sizeof(tallies)) {
1301 printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
1302 dev->name, len);
1303 len = sizeof(tallies);
1304 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001305
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001306 err = hermes_bap_pread(hw, IRQ_BAP, &tallies, len,
1307 infofid, sizeof(info));
1308 if (err)
1309 break;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 /* Increment our various counters */
1312 /* wstats->discard.nwid - no wrong BSSID stuff */
1313 wstats->discard.code +=
1314 le16_to_cpu(tallies.RxWEPUndecryptable);
David Kilroy6fe9deb2009-02-04 23:05:43 +00001315 if (len == sizeof(tallies))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 wstats->discard.code +=
1317 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
1318 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
1319 wstats->discard.misc +=
1320 le16_to_cpu(tallies.TxDiscardsWrongSA);
1321 wstats->discard.fragment +=
1322 le16_to_cpu(tallies.RxMsgInBadMsgFragments);
1323 wstats->discard.retries +=
1324 le16_to_cpu(tallies.TxRetryLimitExceeded);
1325 /* wstats->miss.beacon - no match */
1326 }
1327 break;
1328 case HERMES_INQ_LINKSTATUS: {
1329 struct hermes_linkstatus linkstatus;
1330 u16 newstatus;
1331 int connected;
1332
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001333 if (priv->iw_mode == IW_MODE_MONITOR)
1334 break;
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (len != sizeof(linkstatus)) {
1337 printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
1338 dev->name, len);
1339 break;
1340 }
1341
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001342 err = hermes_bap_pread(hw, IRQ_BAP, &linkstatus, len,
1343 infofid, sizeof(info));
1344 if (err)
1345 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 newstatus = le16_to_cpu(linkstatus.linkstatus);
1347
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001348 /* Symbol firmware uses "out of range" to signal that
1349 * the hostscan frame can be requested. */
1350 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
1351 priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
1352 priv->has_hostscan && priv->scan_inprogress) {
1353 hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
1354 break;
1355 }
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
1358 || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
1359 || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
1360
1361 if (connected)
1362 netif_carrier_on(dev);
David Gibson7bb7c3a2005-05-12 20:02:10 -04001363 else if (!ignore_disconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 netif_carrier_off(dev);
1365
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001366 if (newstatus != priv->last_linkstatus) {
1367 priv->last_linkstatus = newstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 print_linkstatus(dev, newstatus);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001369 /* The info frame contains only one word which is the
1370 * status (see hermes.h). The status is pretty boring
1371 * in itself, that's why we export the new BSSID...
1372 * Jean II */
1373 schedule_work(&priv->wevent_work);
1374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
1376 break;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001377 case HERMES_INQ_SCAN:
1378 if (!priv->scan_inprogress && priv->bssid_fixed &&
1379 priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
1380 schedule_work(&priv->join_work);
1381 break;
1382 }
1383 /* fall through */
1384 case HERMES_INQ_HOSTSCAN:
1385 case HERMES_INQ_HOSTSCAN_SYMBOL: {
1386 /* Result of a scanning. Contains information about
1387 * cells in the vicinity - Jean II */
1388 union iwreq_data wrqu;
1389 unsigned char *buf;
1390
Dan Williams1e3428e2007-10-10 23:56:25 -04001391 /* Scan is no longer in progress */
1392 priv->scan_inprogress = 0;
1393
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001394 /* Sanity check */
1395 if (len > 4096) {
1396 printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
1397 dev->name, len);
1398 break;
1399 }
1400
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001401 /* Allocate buffer for results */
1402 buf = kmalloc(len, GFP_ATOMIC);
1403 if (buf == NULL)
1404 /* No memory, so can't printk()... */
1405 break;
1406
1407 /* Read scan data */
1408 err = hermes_bap_pread(hw, IRQ_BAP, (void *) buf, len,
1409 infofid, sizeof(info));
Pavel Roskin708218b2005-09-01 20:05:19 -04001410 if (err) {
1411 kfree(buf);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001412 break;
Pavel Roskin708218b2005-09-01 20:05:19 -04001413 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001414
1415#ifdef ORINOCO_DEBUG
1416 {
1417 int i;
1418 printk(KERN_DEBUG "Scan result [%02X", buf[0]);
David Kilroya94e8422009-02-04 23:05:44 +00001419 for (i = 1; i < (len * 2); i++)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001420 printk(":%02X", buf[i]);
1421 printk("]\n");
1422 }
1423#endif /* ORINOCO_DEBUG */
1424
David Kilroyaea48b12009-02-04 23:05:49 +00001425 if (orinoco_process_scan_results(priv, buf, len) == 0) {
Dan Williams1e3428e2007-10-10 23:56:25 -04001426 /* Send an empty event to user space.
1427 * We don't send the received data on the event because
1428 * it would require us to do complex transcoding, and
1429 * we want to minimise the work done in the irq handler
1430 * Use a request to extract the data - Jean II */
1431 wrqu.data.length = 0;
1432 wrqu.data.flags = 0;
1433 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
1434 }
1435 kfree(buf);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001436 }
1437 break;
David Kilroy01632fa2008-08-21 23:27:58 +01001438 case HERMES_INQ_CHANNELINFO:
1439 {
1440 struct agere_ext_scan_info *bss;
1441
1442 if (!priv->scan_inprogress) {
1443 printk(KERN_DEBUG "%s: Got chaninfo without scan, "
1444 "len=%d\n", dev->name, len);
1445 break;
1446 }
1447
1448 /* An empty result indicates that the scan is complete */
1449 if (len == 0) {
1450 union iwreq_data wrqu;
1451
1452 /* Scan is no longer in progress */
1453 priv->scan_inprogress = 0;
1454
1455 wrqu.data.length = 0;
1456 wrqu.data.flags = 0;
1457 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
1458 break;
1459 }
1460
1461 /* Sanity check */
1462 else if (len > sizeof(*bss)) {
1463 printk(KERN_WARNING
1464 "%s: Ext scan results too large (%d bytes). "
1465 "Truncating results to %zd bytes.\n",
1466 dev->name, len, sizeof(*bss));
1467 len = sizeof(*bss);
1468 } else if (len < (offsetof(struct agere_ext_scan_info,
1469 data) + 2)) {
1470 /* Drop this result now so we don't have to
1471 * keep checking later */
1472 printk(KERN_WARNING
1473 "%s: Ext scan results too short (%d bytes)\n",
1474 dev->name, len);
1475 break;
1476 }
1477
1478 bss = kmalloc(sizeof(*bss), GFP_ATOMIC);
1479 if (bss == NULL)
1480 break;
1481
1482 /* Read scan data */
1483 err = hermes_bap_pread(hw, IRQ_BAP, (void *) bss, len,
1484 infofid, sizeof(info));
1485 if (err) {
1486 kfree(bss);
1487 break;
1488 }
1489
1490 orinoco_add_ext_scan_result(priv, bss);
1491
1492 kfree(bss);
1493 break;
1494 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001495 case HERMES_INQ_SEC_STAT_AGERE:
1496 /* Security status (Agere specific) */
1497 /* Ignore this frame for now */
1498 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
1499 break;
1500 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 default:
1502 printk(KERN_DEBUG "%s: Unknown information frame received: "
1503 "type 0x%04x, length %d\n", dev->name, type, len);
1504 /* We don't actually do anything about it */
1505 break;
1506 }
1507}
1508
1509static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
1510{
1511 if (net_ratelimit())
1512 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
1513}
1514
1515/********************************************************************/
1516/* Internal hardware control routines */
1517/********************************************************************/
1518
David Kilroy6415f7d2009-06-18 23:21:30 +01001519static int __orinoco_up(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
David Kilroya2608362009-06-18 23:21:23 +01001521 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 struct hermes *hw = &priv->hw;
1523 int err;
1524
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001525 netif_carrier_off(dev); /* just to make sure */
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 err = __orinoco_program_rids(dev);
1528 if (err) {
1529 printk(KERN_ERR "%s: Error %d configuring card\n",
1530 dev->name, err);
1531 return err;
1532 }
1533
1534 /* Fire things up again */
1535 hermes_set_irqmask(hw, ORINOCO_INTEN);
1536 err = hermes_enable_port(hw, 0);
1537 if (err) {
1538 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
1539 dev->name, err);
1540 return err;
1541 }
1542
1543 netif_start_queue(dev);
1544
1545 return 0;
1546}
1547
David Kilroy6415f7d2009-06-18 23:21:30 +01001548static int __orinoco_down(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
David Kilroya2608362009-06-18 23:21:23 +01001550 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 struct hermes *hw = &priv->hw;
1552 int err;
1553
1554 netif_stop_queue(dev);
1555
David Kilroya94e8422009-02-04 23:05:44 +00001556 if (!priv->hw_unavailable) {
1557 if (!priv->broken_disableport) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 err = hermes_disable_port(hw, 0);
1559 if (err) {
1560 /* Some firmwares (e.g. Intersil 1.3.x) seem
1561 * to have problems disabling the port, oh
1562 * well, too bad. */
1563 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
1564 dev->name, err);
1565 priv->broken_disableport = 1;
1566 }
1567 }
1568 hermes_set_irqmask(hw, 0);
1569 hermes_write_regn(hw, EVACK, 0xffff);
1570 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 /* firmware will have to reassociate */
1573 netif_carrier_off(dev);
1574 priv->last_linkstatus = 0xffff;
1575
1576 return 0;
1577}
1578
David Kilroy6415f7d2009-06-18 23:21:30 +01001579static int 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}
1595
David Kilroycb1576a2009-02-04 23:05:56 +00001596int __orinoco_program_rids(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
David Kilroyea60a6a2009-06-18 23:21:26 +01001598 struct orinoco_private *priv = ndev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 hermes_t *hw = &priv->hw;
1600 int err;
1601 struct hermes_idstring idbuf;
1602
1603 /* Set the MAC address */
1604 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
1605 HERMES_BYTES_TO_RECLEN(ETH_ALEN), dev->dev_addr);
1606 if (err) {
1607 printk(KERN_ERR "%s: Error %d setting MAC address\n",
1608 dev->name, err);
1609 return err;
1610 }
1611
1612 /* Set up the link mode */
1613 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFPORTTYPE,
1614 priv->port_type);
1615 if (err) {
1616 printk(KERN_ERR "%s: Error %d setting port type\n",
1617 dev->name, err);
1618 return err;
1619 }
1620 /* Set the channel/frequency */
David Gibsond51d8b12005-05-12 20:03:36 -04001621 if (priv->channel != 0 && priv->iw_mode != IW_MODE_INFRA) {
1622 err = hermes_write_wordrec(hw, USER_BAP,
1623 HERMES_RID_CNFOWNCHANNEL,
1624 priv->channel);
1625 if (err) {
1626 printk(KERN_ERR "%s: Error %d setting channel %d\n",
1627 dev->name, err, priv->channel);
1628 return err;
1629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
1631
1632 if (priv->has_ibss) {
1633 u16 createibss;
1634
1635 if ((strlen(priv->desired_essid) == 0) && (priv->createibss)) {
1636 printk(KERN_WARNING "%s: This firmware requires an "
1637 "ESSID in IBSS-Ad-Hoc mode.\n", dev->name);
1638 /* With wvlan_cs, in this case, we would crash.
1639 * hopefully, this driver will behave better...
1640 * Jean II */
1641 createibss = 0;
1642 } else {
1643 createibss = priv->createibss;
1644 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 err = hermes_write_wordrec(hw, USER_BAP,
1647 HERMES_RID_CNFCREATEIBSS,
1648 createibss);
1649 if (err) {
1650 printk(KERN_ERR "%s: Error %d setting CREATEIBSS\n",
1651 dev->name, err);
1652 return err;
1653 }
1654 }
1655
Christoph Hellwig16739b02005-06-19 01:27:51 +02001656 /* Set the desired BSSID */
1657 err = __orinoco_hw_set_wap(priv);
1658 if (err) {
1659 printk(KERN_ERR "%s: Error %d setting AP address\n",
1660 dev->name, err);
1661 return err;
1662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 /* Set the desired ESSID */
1664 idbuf.len = cpu_to_le16(strlen(priv->desired_essid));
1665 memcpy(&idbuf.val, priv->desired_essid, sizeof(idbuf.val));
1666 /* WinXP wants partner to configure OWNSSID even in IBSS mode. (jimc) */
1667 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNSSID,
David Kilroyb2f30a02009-02-04 23:05:46 +00001668 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
1669 &idbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (err) {
1671 printk(KERN_ERR "%s: Error %d setting OWNSSID\n",
1672 dev->name, err);
1673 return err;
1674 }
1675 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFDESIREDSSID,
David Kilroyb2f30a02009-02-04 23:05:46 +00001676 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
1677 &idbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 if (err) {
1679 printk(KERN_ERR "%s: Error %d setting DESIREDSSID\n",
1680 dev->name, err);
1681 return err;
1682 }
1683
1684 /* Set the station name */
1685 idbuf.len = cpu_to_le16(strlen(priv->nick));
1686 memcpy(&idbuf.val, priv->nick, sizeof(idbuf.val));
1687 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
1688 HERMES_BYTES_TO_RECLEN(strlen(priv->nick)+2),
1689 &idbuf);
1690 if (err) {
1691 printk(KERN_ERR "%s: Error %d setting nickname\n",
1692 dev->name, err);
1693 return err;
1694 }
1695
1696 /* Set AP density */
1697 if (priv->has_sensitivity) {
1698 err = hermes_write_wordrec(hw, USER_BAP,
1699 HERMES_RID_CNFSYSTEMSCALE,
1700 priv->ap_density);
1701 if (err) {
David Kilroyb2f30a02009-02-04 23:05:46 +00001702 printk(KERN_WARNING "%s: Error %d setting SYSTEMSCALE. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 "Disabling sensitivity control\n",
1704 dev->name, err);
1705
1706 priv->has_sensitivity = 0;
1707 }
1708 }
1709
1710 /* Set RTS threshold */
1711 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
1712 priv->rts_thresh);
1713 if (err) {
1714 printk(KERN_ERR "%s: Error %d setting RTS threshold\n",
1715 dev->name, err);
1716 return err;
1717 }
1718
1719 /* Set fragmentation threshold or MWO robustness */
1720 if (priv->has_mwo)
1721 err = hermes_write_wordrec(hw, USER_BAP,
1722 HERMES_RID_CNFMWOROBUST_AGERE,
1723 priv->mwo_robust);
1724 else
1725 err = hermes_write_wordrec(hw, USER_BAP,
1726 HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
1727 priv->frag_thresh);
1728 if (err) {
1729 printk(KERN_ERR "%s: Error %d setting fragmentation\n",
1730 dev->name, err);
1731 return err;
1732 }
1733
1734 /* Set bitrate */
1735 err = __orinoco_hw_set_bitrate(priv);
1736 if (err) {
1737 printk(KERN_ERR "%s: Error %d setting bitrate\n",
1738 dev->name, err);
1739 return err;
1740 }
1741
1742 /* Set power management */
1743 if (priv->has_pm) {
1744 err = hermes_write_wordrec(hw, USER_BAP,
1745 HERMES_RID_CNFPMENABLED,
1746 priv->pm_on);
1747 if (err) {
1748 printk(KERN_ERR "%s: Error %d setting up PM\n",
1749 dev->name, err);
1750 return err;
1751 }
1752
1753 err = hermes_write_wordrec(hw, USER_BAP,
1754 HERMES_RID_CNFMULTICASTRECEIVE,
1755 priv->pm_mcast);
1756 if (err) {
1757 printk(KERN_ERR "%s: Error %d setting up PM\n",
1758 dev->name, err);
1759 return err;
1760 }
1761 err = hermes_write_wordrec(hw, USER_BAP,
1762 HERMES_RID_CNFMAXSLEEPDURATION,
1763 priv->pm_period);
1764 if (err) {
1765 printk(KERN_ERR "%s: Error %d setting up PM\n",
1766 dev->name, err);
1767 return err;
1768 }
1769 err = hermes_write_wordrec(hw, USER_BAP,
1770 HERMES_RID_CNFPMHOLDOVERDURATION,
1771 priv->pm_timeout);
1772 if (err) {
1773 printk(KERN_ERR "%s: Error %d setting up PM\n",
1774 dev->name, err);
1775 return err;
1776 }
1777 }
1778
1779 /* Set preamble - only for Symbol so far... */
1780 if (priv->has_preamble) {
1781 err = hermes_write_wordrec(hw, USER_BAP,
1782 HERMES_RID_CNFPREAMBLE_SYMBOL,
1783 priv->preamble);
1784 if (err) {
1785 printk(KERN_ERR "%s: Error %d setting preamble\n",
1786 dev->name, err);
1787 return err;
1788 }
1789 }
1790
1791 /* Set up encryption */
David Kilroyd03032a2008-08-21 23:28:02 +01001792 if (priv->has_wep || priv->has_wpa) {
1793 err = __orinoco_hw_setup_enc(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 if (err) {
David Kilroyd03032a2008-08-21 23:28:02 +01001795 printk(KERN_ERR "%s: Error %d activating encryption\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 dev->name, err);
1797 return err;
1798 }
1799 }
1800
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001801 if (priv->iw_mode == IW_MODE_MONITOR) {
1802 /* Enable monitor mode */
1803 dev->type = ARPHRD_IEEE80211;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001804 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001805 HERMES_TEST_MONITOR, 0, NULL);
1806 } else {
1807 /* Disable monitor mode */
1808 dev->type = ARPHRD_ETHER;
1809 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
1810 HERMES_TEST_STOP, 0, NULL);
1811 }
1812 if (err)
1813 return err;
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 /* Set promiscuity / multicast*/
1816 priv->promiscuous = 0;
1817 priv->mc_count = 0;
Herbert Xu932ff272006-06-09 12:20:56 -07001818
1819 /* FIXME: what about netif_tx_lock */
1820 __orinoco_set_multicast_list(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822 return 0;
1823}
1824
David Kilroy5865d012009-02-04 23:05:54 +00001825/* FIXME: return int? */
1826static void
1827__orinoco_set_multicast_list(struct net_device *dev)
1828{
David Kilroyea60a6a2009-06-18 23:21:26 +01001829 struct orinoco_private *priv = ndev_priv(dev);
David Kilroy5865d012009-02-04 23:05:54 +00001830 int err = 0;
1831 int promisc, mc_count;
1832
1833 /* The Hermes doesn't seem to have an allmulti mode, so we go
1834 * into promiscuous mode and let the upper levels deal. */
1835 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
1836 (dev->mc_count > MAX_MULTICAST(priv))) {
1837 promisc = 1;
1838 mc_count = 0;
1839 } else {
1840 promisc = 0;
1841 mc_count = dev->mc_count;
1842 }
1843
1844 err = __orinoco_hw_set_multicast_list(priv, dev->mc_list, mc_count,
1845 promisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848/* This must be called from user context, without locks held - use
1849 * schedule_work() */
David Kilroycb1576a2009-02-04 23:05:56 +00001850void orinoco_reset(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
David Howellsc4028952006-11-22 14:57:56 +00001852 struct orinoco_private *priv =
1853 container_of(work, struct orinoco_private, reset_work);
1854 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 struct hermes *hw = &priv->hw;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001856 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 unsigned long flags;
1858
1859 if (orinoco_lock(priv, &flags) != 0)
1860 /* When the hardware becomes available again, whatever
1861 * detects that is responsible for re-initializing
1862 * it. So no need for anything further */
1863 return;
1864
1865 netif_stop_queue(dev);
1866
1867 /* Shut off interrupts. Depending on what state the hardware
1868 * is in, this might not work, but we'll try anyway */
1869 hermes_set_irqmask(hw, 0);
1870 hermes_write_regn(hw, EVACK, 0xffff);
1871
1872 priv->hw_unavailable++;
1873 priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
1874 netif_carrier_off(dev);
1875
1876 orinoco_unlock(priv, &flags);
1877
David Kilroy6fe9deb2009-02-04 23:05:43 +00001878 /* Scanning support: Cleanup of driver struct */
Dan Williams1e3428e2007-10-10 23:56:25 -04001879 orinoco_clear_scan_results(priv, 0);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001880 priv->scan_inprogress = 0;
1881
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001882 if (priv->hard_reset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 err = (*priv->hard_reset)(priv);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001884 if (err) {
1885 printk(KERN_ERR "%s: orinoco_reset: Error %d "
1886 "performing hard reset\n", dev->name, err);
1887 goto disable;
1888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 }
1890
David Kilroya2608362009-06-18 23:21:23 +01001891 err = orinoco_reinit_firmware(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 if (err) {
1893 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
1894 dev->name, err);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001895 goto disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 }
1897
David Kilroyb2f30a02009-02-04 23:05:46 +00001898 /* This has to be called from user context */
1899 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 priv->hw_unavailable--;
1902
1903 /* priv->open or priv->hw_unavailable might have changed while
1904 * we dropped the lock */
David Kilroya94e8422009-02-04 23:05:44 +00001905 if (priv->open && (!priv->hw_unavailable)) {
David Kilroya2608362009-06-18 23:21:23 +01001906 err = __orinoco_up(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 if (err) {
1908 printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
1909 dev->name, err);
1910 } else
1911 dev->trans_start = jiffies;
1912 }
1913
1914 spin_unlock_irq(&priv->lock);
1915
1916 return;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001917 disable:
1918 hermes_set_irqmask(hw, 0);
1919 netif_device_detach(dev);
1920 printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921}
1922
1923/********************************************************************/
1924/* Interrupt handler */
1925/********************************************************************/
1926
1927static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
1928{
1929 printk(KERN_DEBUG "%s: TICK\n", dev->name);
1930}
1931
1932static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
1933{
1934 /* This seems to happen a fair bit under load, but ignoring it
1935 seems to work fine...*/
1936 printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
1937 dev->name);
1938}
1939
David Howells7d12e782006-10-05 14:55:46 +01001940irqreturn_t orinoco_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
David Kilroya2608362009-06-18 23:21:23 +01001942 struct orinoco_private *priv = dev_id;
1943 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 hermes_t *hw = &priv->hw;
1945 int count = MAX_IRQLOOPS_PER_IRQ;
1946 u16 evstat, events;
David Kilroy21312662009-02-04 23:05:47 +00001947 /* These are used to detect a runaway interrupt situation.
1948 *
1949 * If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
1950 * we panic and shut down the hardware
1951 */
1952 /* jiffies value the last time we were called */
1953 static int last_irq_jiffy; /* = 0 */
1954 static int loops_this_jiffy; /* = 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 unsigned long flags;
1956
1957 if (orinoco_lock(priv, &flags) != 0) {
1958 /* If hw is unavailable - we don't know if the irq was
1959 * for us or not */
1960 return IRQ_HANDLED;
1961 }
1962
1963 evstat = hermes_read_regn(hw, EVSTAT);
1964 events = evstat & hw->inten;
David Kilroya94e8422009-02-04 23:05:44 +00001965 if (!events) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 orinoco_unlock(priv, &flags);
1967 return IRQ_NONE;
1968 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (jiffies != last_irq_jiffy)
1971 loops_this_jiffy = 0;
1972 last_irq_jiffy = jiffies;
1973
1974 while (events && count--) {
1975 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
1976 printk(KERN_WARNING "%s: IRQ handler is looping too "
1977 "much! Resetting.\n", dev->name);
1978 /* Disable interrupts for now */
1979 hermes_set_irqmask(hw, 0);
1980 schedule_work(&priv->reset_work);
1981 break;
1982 }
1983
1984 /* Check the card hasn't been removed */
David Kilroya94e8422009-02-04 23:05:44 +00001985 if (!hermes_present(hw)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 DEBUG(0, "orinoco_interrupt(): card removed\n");
1987 break;
1988 }
1989
1990 if (events & HERMES_EV_TICK)
1991 __orinoco_ev_tick(dev, hw);
1992 if (events & HERMES_EV_WTERR)
1993 __orinoco_ev_wterr(dev, hw);
1994 if (events & HERMES_EV_INFDROP)
1995 __orinoco_ev_infdrop(dev, hw);
1996 if (events & HERMES_EV_INFO)
1997 __orinoco_ev_info(dev, hw);
1998 if (events & HERMES_EV_RX)
1999 __orinoco_ev_rx(dev, hw);
2000 if (events & HERMES_EV_TXEXC)
2001 __orinoco_ev_txexc(dev, hw);
2002 if (events & HERMES_EV_TX)
2003 __orinoco_ev_tx(dev, hw);
2004 if (events & HERMES_EV_ALLOC)
2005 __orinoco_ev_alloc(dev, hw);
David Kilroy6fe9deb2009-02-04 23:05:43 +00002006
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002007 hermes_write_regn(hw, EVACK, evstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
2009 evstat = hermes_read_regn(hw, EVSTAT);
2010 events = evstat & hw->inten;
2011 };
2012
2013 orinoco_unlock(priv, &flags);
2014 return IRQ_HANDLED;
2015}
David Kilroy21312662009-02-04 23:05:47 +00002016EXPORT_SYMBOL(orinoco_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
2018/********************************************************************/
David Kilroy39d1ffe2008-11-22 10:37:28 +00002019/* Power management */
2020/********************************************************************/
2021#if defined(CONFIG_PM_SLEEP) && !defined(CONFIG_HERMES_CACHE_FW_ON_INIT)
2022static int orinoco_pm_notifier(struct notifier_block *notifier,
2023 unsigned long pm_event,
2024 void *unused)
2025{
2026 struct orinoco_private *priv = container_of(notifier,
2027 struct orinoco_private,
2028 pm_notifier);
2029
2030 /* All we need to do is cache the firmware before suspend, and
2031 * release it when we come out.
2032 *
2033 * Only need to do this if we're downloading firmware. */
2034 if (!priv->do_fw_download)
2035 return NOTIFY_DONE;
2036
2037 switch (pm_event) {
2038 case PM_HIBERNATION_PREPARE:
2039 case PM_SUSPEND_PREPARE:
2040 orinoco_cache_fw(priv, 0);
2041 break;
2042
2043 case PM_POST_RESTORE:
2044 /* Restore from hibernation failed. We need to clean
2045 * up in exactly the same way, so fall through. */
2046 case PM_POST_HIBERNATION:
2047 case PM_POST_SUSPEND:
2048 orinoco_uncache_fw(priv);
2049 break;
2050
2051 case PM_RESTORE_PREPARE:
2052 default:
2053 break;
2054 }
2055
2056 return NOTIFY_DONE;
2057}
David S. Millerf11c1792009-02-25 00:02:05 -08002058
2059static void orinoco_register_pm_notifier(struct orinoco_private *priv)
2060{
2061 priv->pm_notifier.notifier_call = orinoco_pm_notifier;
2062 register_pm_notifier(&priv->pm_notifier);
2063}
2064
2065static void orinoco_unregister_pm_notifier(struct orinoco_private *priv)
2066{
2067 unregister_pm_notifier(&priv->pm_notifier);
2068}
David Kilroy39d1ffe2008-11-22 10:37:28 +00002069#else /* !PM_SLEEP || HERMES_CACHE_FW_ON_INIT */
David S. Millerf11c1792009-02-25 00:02:05 -08002070#define orinoco_register_pm_notifier(priv) do { } while(0)
2071#define orinoco_unregister_pm_notifier(priv) do { } while(0)
David Kilroy39d1ffe2008-11-22 10:37:28 +00002072#endif
2073
2074/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075/* Initialization */
2076/********************************************************************/
2077
David Kilroy8e638262009-06-18 23:21:24 +01002078int orinoco_init(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
David Kilroy8e638262009-06-18 23:21:24 +01002080 struct device *dev = priv->dev;
David Kilroyea60a6a2009-06-18 23:21:26 +01002081 struct wiphy *wiphy = priv_to_wiphy(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 hermes_t *hw = &priv->hw;
2083 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 /* No need to lock, the hw_unavailable flag is already set in
2086 * alloc_orinocodev() */
Johannes Berg2c7060022008-10-30 22:09:54 +01002087 priv->nicbuf_size = IEEE80211_MAX_FRAME_LEN + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 /* Initialize the firmware */
Pavel Roskin37a6c612006-04-07 04:10:49 -04002090 err = hermes_init(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 if (err != 0) {
David Kilroy8e638262009-06-18 23:21:24 +01002092 dev_err(dev, "Failed to initialize firmware (err = %d)\n",
2093 err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 goto out;
2095 }
2096
David Kilroya2d1a422009-06-18 23:21:18 +01002097 err = determine_fw_capabilities(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 if (err != 0) {
David Kilroy8e638262009-06-18 23:21:24 +01002099 dev_err(dev, "Incompatible firmware, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 goto out;
2101 }
2102
David Kilroy3994d502008-08-21 23:27:54 +01002103 if (priv->do_fw_download) {
David Kilroy39d1ffe2008-11-22 10:37:28 +00002104#ifdef CONFIG_HERMES_CACHE_FW_ON_INIT
David Kilroy74734312008-11-22 10:37:25 +00002105 orinoco_cache_fw(priv, 0);
David Kilroy39d1ffe2008-11-22 10:37:28 +00002106#endif
David Kilroy74734312008-11-22 10:37:25 +00002107
David Kilroy3994d502008-08-21 23:27:54 +01002108 err = orinoco_download(priv);
2109 if (err)
2110 priv->do_fw_download = 0;
2111
2112 /* Check firmware version again */
David Kilroya2d1a422009-06-18 23:21:18 +01002113 err = determine_fw_capabilities(priv);
David Kilroy3994d502008-08-21 23:27:54 +01002114 if (err != 0) {
David Kilroy8e638262009-06-18 23:21:24 +01002115 dev_err(dev, "Incompatible firmware, aborting\n");
David Kilroy3994d502008-08-21 23:27:54 +01002116 goto out;
2117 }
2118 }
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 if (priv->has_port3)
David Kilroy8e638262009-06-18 23:21:24 +01002121 dev_info(dev, "Ad-hoc demo mode supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 if (priv->has_ibss)
David Kilroy8e638262009-06-18 23:21:24 +01002123 dev_info(dev, "IEEE standard IBSS ad-hoc mode supported\n");
2124 if (priv->has_wep)
2125 dev_info(dev, "WEP supported, %s-bit key\n",
2126 priv->has_big_wep ? "104" : "40");
David Kilroy23edcc42008-08-21 23:28:05 +01002127 if (priv->has_wpa) {
David Kilroy8e638262009-06-18 23:21:24 +01002128 dev_info(dev, "WPA-PSK supported\n");
David Kilroy23edcc42008-08-21 23:28:05 +01002129 if (orinoco_mic_init(priv)) {
David Kilroy8e638262009-06-18 23:21:24 +01002130 dev_err(dev, "Failed to setup MIC crypto algorithm. "
2131 "Disabling WPA support\n");
David Kilroy23edcc42008-08-21 23:28:05 +01002132 priv->has_wpa = 0;
2133 }
2134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
David Kilroy01632fa2008-08-21 23:27:58 +01002136 /* Now we have the firmware capabilities, allocate appropiate
2137 * sized scan buffers */
2138 if (orinoco_bss_data_allocate(priv))
2139 goto out;
2140 orinoco_bss_data_init(priv);
2141
David Kilroyea60a6a2009-06-18 23:21:26 +01002142 err = orinoco_hw_read_card_settings(priv, wiphy->perm_addr);
David Kilroye9e3d012009-06-18 23:21:19 +01002143 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
David Kilroy42a51b92009-06-18 23:21:20 +01002146 err = orinoco_hw_allocate_fid(priv);
Pavel Roskin37a6c612006-04-07 04:10:49 -04002147 if (err) {
David Kilroy8e638262009-06-18 23:21:24 +01002148 dev_err(dev, "Failed to allocate NIC buffer!\n");
Pavel Roskin37a6c612006-04-07 04:10:49 -04002149 goto out;
2150 }
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 /* Set up the default configuration */
2153 priv->iw_mode = IW_MODE_INFRA;
2154 /* By default use IEEE/IBSS ad-hoc mode if we have it */
David Kilroya94e8422009-02-04 23:05:44 +00002155 priv->prefer_port3 = priv->has_port3 && (!priv->has_ibss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 set_port_type(priv);
David Gibsond51d8b12005-05-12 20:03:36 -04002157 priv->channel = 0; /* use firmware default */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
2159 priv->promiscuous = 0;
David Kilroy4ae6ee22008-08-21 23:27:59 +01002160 priv->encode_alg = IW_ENCODE_ALG_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 priv->tx_key = 0;
David Kilroyd03032a2008-08-21 23:28:02 +01002162 priv->wpa_enabled = 0;
2163 priv->tkip_cm_active = 0;
2164 priv->key_mgmt = 0;
2165 priv->wpa_ie_len = 0;
2166 priv->wpa_ie = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
David Kilroyea60a6a2009-06-18 23:21:26 +01002168 if (orinoco_wiphy_register(wiphy)) {
2169 err = -ENODEV;
2170 goto out;
2171 }
2172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 /* Make the hardware available, as long as it hasn't been
2174 * removed elsewhere (e.g. by PCMCIA hot unplug) */
2175 spin_lock_irq(&priv->lock);
2176 priv->hw_unavailable--;
2177 spin_unlock_irq(&priv->lock);
2178
David Kilroy8e638262009-06-18 23:21:24 +01002179 dev_dbg(dev, "Ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return err;
2183}
David Kilroy8e638262009-06-18 23:21:24 +01002184EXPORT_SYMBOL(orinoco_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Andrey Borzenkov89ea4092009-01-21 20:46:46 +03002186static const struct net_device_ops orinoco_netdev_ops = {
Andrey Borzenkov89ea4092009-01-21 20:46:46 +03002187 .ndo_open = orinoco_open,
2188 .ndo_stop = orinoco_stop,
2189 .ndo_start_xmit = orinoco_xmit,
2190 .ndo_set_multicast_list = orinoco_set_multicast_list,
2191 .ndo_change_mtu = orinoco_change_mtu,
2192 .ndo_tx_timeout = orinoco_tx_timeout,
2193 .ndo_get_stats = orinoco_get_stats,
2194};
2195
David Kilroyea60a6a2009-06-18 23:21:26 +01002196/* Allocate private data.
2197 *
2198 * This driver has a number of structures associated with it
2199 * netdev - Net device structure for each network interface
2200 * wiphy - structure associated with wireless phy
2201 * wireless_dev (wdev) - structure for each wireless interface
2202 * hw - structure for hermes chip info
2203 * card - card specific structure for use by the card driver
2204 * (airport, orinoco_cs)
2205 * priv - orinoco private data
2206 * device - generic linux device structure
2207 *
2208 * +---------+ +---------+
2209 * | wiphy | | netdev |
2210 * | +-------+ | +-------+
2211 * | | priv | | | wdev |
2212 * | | +-----+ +-+-------+
2213 * | | | hw |
2214 * | +-+-----+
2215 * | | card |
2216 * +-+-------+
2217 *
2218 * priv has a link to netdev and device
2219 * wdev has a link to wiphy
2220 */
David Kilroya2608362009-06-18 23:21:23 +01002221struct orinoco_private
David Kilroy3994d502008-08-21 23:27:54 +01002222*alloc_orinocodev(int sizeof_card,
2223 struct device *device,
2224 int (*hard_reset)(struct orinoco_private *),
2225 int (*stop_fw)(struct orinoco_private *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 struct orinoco_private *priv;
David Kilroyea60a6a2009-06-18 23:21:26 +01002228 struct wiphy *wiphy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
David Kilroyea60a6a2009-06-18 23:21:26 +01002230 /* allocate wiphy
2231 * NOTE: We only support a single virtual interface
2232 * but this may change when monitor mode is added
2233 */
2234 wiphy = wiphy_new(&orinoco_cfg_ops,
2235 sizeof(struct orinoco_private) + sizeof_card);
2236 if (!wiphy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return NULL;
David Kilroyea60a6a2009-06-18 23:21:26 +01002238
David Kilroyea60a6a2009-06-18 23:21:26 +01002239 priv = wiphy_priv(wiphy);
David Kilroy53819562009-06-18 23:21:28 +01002240 priv->dev = device;
David Kilroyea60a6a2009-06-18 23:21:26 +01002241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 if (sizeof_card)
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002243 priv->card = (void *)((unsigned long)priv
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 + sizeof(struct orinoco_private));
2245 else
2246 priv->card = NULL;
2247
David Kilroyea60a6a2009-06-18 23:21:26 +01002248 orinoco_wiphy_init(wiphy);
2249
Pavel Roskin343c6862005-09-09 18:43:02 -04002250#ifdef WIRELESS_SPY
2251 priv->wireless_data.spy_data = &priv->spy_data;
Pavel Roskin343c6862005-09-09 18:43:02 -04002252#endif
David Kilroy23edcc42008-08-21 23:28:05 +01002253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 /* Set up default callbacks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 priv->hard_reset = hard_reset;
David Kilroy3994d502008-08-21 23:27:54 +01002256 priv->stop_fw = stop_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
2258 spin_lock_init(&priv->lock);
2259 priv->open = 0;
2260 priv->hw_unavailable = 1; /* orinoco_init() must clear this
2261 * before anything else touches the
2262 * hardware */
David Howellsc4028952006-11-22 14:57:56 +00002263 INIT_WORK(&priv->reset_work, orinoco_reset);
2264 INIT_WORK(&priv->join_work, orinoco_join_ap);
2265 INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
David Kilroy31afcef2008-08-21 23:28:04 +01002267 INIT_LIST_HEAD(&priv->rx_list);
2268 tasklet_init(&priv->rx_tasklet, orinoco_rx_isr_tasklet,
David Kilroy53819562009-06-18 23:21:28 +01002269 (unsigned long) priv);
David Kilroy31afcef2008-08-21 23:28:04 +01002270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 priv->last_linkstatus = 0xffff;
2272
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +03002273#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
David Kilroy2cea7b22008-11-22 10:37:26 +00002274 priv->cached_pri_fw = NULL;
Andrey Borzenkov4fb30782008-10-19 12:06:11 +04002275 priv->cached_fw = NULL;
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +03002276#endif
Andrey Borzenkov4fb30782008-10-19 12:06:11 +04002277
David Kilroy39d1ffe2008-11-22 10:37:28 +00002278 /* Register PM notifiers */
David S. Millerf11c1792009-02-25 00:02:05 -08002279 orinoco_register_pm_notifier(priv);
David Kilroy39d1ffe2008-11-22 10:37:28 +00002280
David Kilroya2608362009-06-18 23:21:23 +01002281 return priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282}
David Kilroy21312662009-02-04 23:05:47 +00002283EXPORT_SYMBOL(alloc_orinocodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
David Kilroy53819562009-06-18 23:21:28 +01002285/* We can only support a single interface. We provide a separate
2286 * function to set it up to distinguish between hardware
2287 * initialisation and interface setup.
2288 *
2289 * The base_addr and irq parameters are passed on to netdev for use
2290 * with SIOCGIFMAP.
2291 */
2292int orinoco_if_add(struct orinoco_private *priv,
2293 unsigned long base_addr,
2294 unsigned int irq)
2295{
2296 struct wiphy *wiphy = priv_to_wiphy(priv);
2297 struct wireless_dev *wdev;
2298 struct net_device *dev;
2299 int ret;
2300
2301 dev = alloc_etherdev(sizeof(struct wireless_dev));
2302
2303 if (!dev)
2304 return -ENOMEM;
2305
2306 /* Initialise wireless_dev */
2307 wdev = netdev_priv(dev);
2308 wdev->wiphy = wiphy;
2309 wdev->iftype = NL80211_IFTYPE_STATION;
2310
2311 /* Setup / override net_device fields */
2312 dev->ieee80211_ptr = wdev;
2313 dev->netdev_ops = &orinoco_netdev_ops;
2314 dev->watchdog_timeo = HZ; /* 1 second timeout */
2315 dev->ethtool_ops = &orinoco_ethtool_ops;
2316 dev->wireless_handlers = &orinoco_handler_def;
2317#ifdef WIRELESS_SPY
2318 dev->wireless_data = &priv->wireless_data;
2319#endif
2320 /* we use the default eth_mac_addr for setting the MAC addr */
2321
2322 /* Reserve space in skb for the SNAP header */
2323 dev->hard_header_len += ENCAPS_OVERHEAD;
2324
2325 netif_carrier_off(dev);
2326
2327 memcpy(dev->dev_addr, wiphy->perm_addr, ETH_ALEN);
2328
2329 dev->base_addr = base_addr;
2330 dev->irq = irq;
2331
2332 SET_NETDEV_DEV(dev, priv->dev);
2333 ret = register_netdev(dev);
2334 if (ret)
2335 goto fail;
2336
2337 priv->ndev = dev;
2338
2339 /* Report what we've done */
2340 dev_dbg(priv->dev, "Registerred interface %s.\n", dev->name);
2341
2342 return 0;
2343
2344 fail:
2345 free_netdev(dev);
2346 return ret;
2347}
2348EXPORT_SYMBOL(orinoco_if_add);
2349
2350void orinoco_if_del(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
David Kilroya2608362009-06-18 23:21:23 +01002352 struct net_device *dev = priv->ndev;
David Kilroy53819562009-06-18 23:21:28 +01002353
2354 unregister_netdev(dev);
2355 free_netdev(dev);
2356}
2357EXPORT_SYMBOL(orinoco_if_del);
2358
2359void free_orinocodev(struct orinoco_private *priv)
2360{
David Kilroyea60a6a2009-06-18 23:21:26 +01002361 struct wiphy *wiphy = priv_to_wiphy(priv);
David Kilroy20953ad2009-01-07 00:23:55 +00002362 struct orinoco_rx_data *rx_data, *temp;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002363
David Kilroyea60a6a2009-06-18 23:21:26 +01002364 wiphy_unregister(wiphy);
2365
David Kilroy20953ad2009-01-07 00:23:55 +00002366 /* If the tasklet is scheduled when we call tasklet_kill it
2367 * will run one final time. However the tasklet will only
2368 * drain priv->rx_list if the hw is still available. */
David Kilroy31afcef2008-08-21 23:28:04 +01002369 tasklet_kill(&priv->rx_tasklet);
David Kilroy74734312008-11-22 10:37:25 +00002370
David Kilroy20953ad2009-01-07 00:23:55 +00002371 /* Explicitly drain priv->rx_list */
2372 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
2373 list_del(&rx_data->list);
2374
2375 dev_kfree_skb(rx_data->skb);
2376 kfree(rx_data->desc);
2377 kfree(rx_data);
2378 }
2379
David S. Millerf11c1792009-02-25 00:02:05 -08002380 orinoco_unregister_pm_notifier(priv);
David Kilroy74734312008-11-22 10:37:25 +00002381 orinoco_uncache_fw(priv);
2382
David Kilroyd03032a2008-08-21 23:28:02 +01002383 priv->wpa_ie_len = 0;
2384 kfree(priv->wpa_ie);
David Kilroy23edcc42008-08-21 23:28:05 +01002385 orinoco_mic_free(priv);
Dan Williams1e3428e2007-10-10 23:56:25 -04002386 orinoco_bss_data_free(priv);
David Kilroyea60a6a2009-06-18 23:21:26 +01002387 wiphy_free(wiphy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388}
David Kilroy21312662009-02-04 23:05:47 +00002389EXPORT_SYMBOL(free_orinocodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
David Kilroy6415f7d2009-06-18 23:21:30 +01002391int orinoco_up(struct orinoco_private *priv)
2392{
2393 struct net_device *dev = priv->ndev;
2394 unsigned long flags;
2395 int err;
2396
2397 spin_lock_irqsave(&priv->lock, flags);
2398
2399 err = orinoco_reinit_firmware(priv);
2400 if (err) {
2401 printk(KERN_ERR "%s: Error %d re-initializing firmware\n",
2402 dev->name, err);
2403 goto exit;
2404 }
2405
2406 netif_device_attach(dev);
2407 priv->hw_unavailable--;
2408
2409 if (priv->open && !priv->hw_unavailable) {
2410 err = __orinoco_up(priv);
2411 if (err)
2412 printk(KERN_ERR "%s: Error %d restarting card\n",
2413 dev->name, err);
2414 }
2415
2416exit:
2417 spin_unlock_irqrestore(&priv->lock, flags);
2418
2419 return 0;
2420}
2421EXPORT_SYMBOL(orinoco_up);
2422
2423void orinoco_down(struct orinoco_private *priv)
2424{
2425 struct net_device *dev = priv->ndev;
2426 unsigned long flags;
2427 int err;
2428
2429 spin_lock_irqsave(&priv->lock, flags);
2430 err = __orinoco_down(priv);
2431 if (err)
2432 printk(KERN_WARNING "%s: Error %d downing interface\n",
2433 dev->name, err);
2434
2435 netif_device_detach(dev);
2436 priv->hw_unavailable++;
2437 spin_unlock_irqrestore(&priv->lock, flags);
2438}
2439EXPORT_SYMBOL(orinoco_down);
2440
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02002441static void orinoco_get_drvinfo(struct net_device *dev,
2442 struct ethtool_drvinfo *info)
2443{
David Kilroyea60a6a2009-06-18 23:21:26 +01002444 struct orinoco_private *priv = ndev_priv(dev);
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02002445
2446 strncpy(info->driver, DRIVER_NAME, sizeof(info->driver) - 1);
2447 strncpy(info->version, DRIVER_VERSION, sizeof(info->version) - 1);
2448 strncpy(info->fw_version, priv->fw_name, sizeof(info->fw_version) - 1);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07002449 if (dev->dev.parent)
Kay Sieversfb28ad32008-11-10 13:55:14 -08002450 strncpy(info->bus_info, dev_name(dev->dev.parent),
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02002451 sizeof(info->bus_info) - 1);
2452 else
2453 snprintf(info->bus_info, sizeof(info->bus_info) - 1,
2454 "PCMCIA %p", priv->hw.iobase);
2455}
2456
Jeff Garzik7282d492006-09-13 14:30:00 -04002457static const struct ethtool_ops orinoco_ethtool_ops = {
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02002458 .get_drvinfo = orinoco_get_drvinfo,
2459 .get_link = ethtool_op_get_link,
2460};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
2462/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463/* Module initialization */
2464/********************************************************************/
2465
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466/* Can't be declared "const" or the whole __initdata section will
2467 * become const */
2468static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
2469 " (David Gibson <hermes@gibson.dropbear.id.au>, "
2470 "Pavel Roskin <proski@gnu.org>, et al)";
2471
2472static int __init init_orinoco(void)
2473{
2474 printk(KERN_DEBUG "%s\n", version);
2475 return 0;
2476}
2477
2478static void __exit exit_orinoco(void)
2479{
2480}
2481
2482module_init(init_orinoco);
2483module_exit(exit_orinoco);