blob: 58a48db692e3ed86404f5f7bd1691f1d5be4dd05 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <linux/etherdevice.h>
Christoph Hellwig1fab2e82005-06-19 01:27:40 +020085#include <linux/ethtool.h>
David Kilroy39d1ffe2008-11-22 10:37:28 +000086#include <linux/suspend.h>
Pavel Roskin9c974fb2006-08-15 20:45:03 -040087#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include <linux/wireless.h>
Johannes Berg2c7060022008-10-30 22:09:54 +010089#include <linux/ieee80211.h>
Christoph Hellwig620554e2005-06-19 01:27:33 +020090#include <net/iw_handler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#include "hermes_rid.h"
David Kilroy3994d502008-08-21 23:27:54 +010093#include "hermes_dld.h"
David Kilroy712a4342009-02-04 23:05:55 +000094#include "hw.h"
David Kilroyfb791b12009-02-04 23:05:50 +000095#include "scan.h"
David Kilroy4adb4742009-02-04 23:05:51 +000096#include "mic.h"
David Kilroy37a2e562009-02-04 23:05:52 +000097#include "fw.h"
David Kilroycb1576a2009-02-04 23:05:56 +000098#include "wext.h"
99#include "main.h"
David Kilroyfb791b12009-02-04 23:05:50 +0000100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#include "orinoco.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/********************************************************************/
104/* Module information */
105/********************************************************************/
106
David Kilroyb2f30a02009-02-04 23:05:46 +0000107MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & "
108 "David Gibson <hermes@gibson.dropbear.id.au>");
109MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based "
110 "and similar wireless cards");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111MODULE_LICENSE("Dual MPL/GPL");
112
113/* Level of debugging. Used in the macros in orinoco.h */
114#ifdef ORINOCO_DEBUG
115int orinoco_debug = ORINOCO_DEBUG;
David Kilroy21312662009-02-04 23:05:47 +0000116EXPORT_SYMBOL(orinoco_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117module_param(orinoco_debug, int, 0644);
118MODULE_PARM_DESC(orinoco_debug, "Debug level");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#endif
120
121static int suppress_linkstatus; /* = 0 */
122module_param(suppress_linkstatus, bool, 0644);
123MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
David Kilroyb2f30a02009-02-04 23:05:46 +0000124
David Gibson7bb7c3a2005-05-12 20:02:10 -0400125static int ignore_disconnect; /* = 0 */
126module_param(ignore_disconnect, int, 0644);
David Kilroyb2f30a02009-02-04 23:05:46 +0000127MODULE_PARM_DESC(ignore_disconnect,
128 "Don't report lost link to the network layer");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
David Kilroycb1576a2009-02-04 23:05:56 +0000130int force_monitor; /* = 0 */
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200131module_param(force_monitor, int, 0644);
132MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/* Internal constants */
136/********************************************************************/
137
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200138/* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
139static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
140#define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#define ORINOCO_MIN_MTU 256
Johannes Berg2c7060022008-10-30 22:09:54 +0100143#define ORINOCO_MAX_MTU (IEEE80211_MAX_DATA_LEN - ENCAPS_OVERHEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define MAX_IRQLOOPS_PER_IRQ 10
146#define MAX_IRQLOOPS_PER_JIFFY (20000/HZ) /* Based on a guestimate of
147 * how many events the
148 * device could
149 * legitimately generate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151#define DUMMY_FID 0xFFFF
152
153/*#define MAX_MULTICAST(priv) (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
154 HERMES_MAX_MULTICAST : 0)*/
155#define MAX_MULTICAST(priv) (HERMES_MAX_MULTICAST)
156
157#define ORINOCO_INTEN (HERMES_EV_RX | HERMES_EV_ALLOC \
158 | HERMES_EV_TX | HERMES_EV_TXEXC \
159 | HERMES_EV_WTERR | HERMES_EV_INFO \
David Kilroya94e8422009-02-04 23:05:44 +0000160 | HERMES_EV_INFDROP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Jeff Garzik7282d492006-09-13 14:30:00 -0400162static const struct ethtool_ops orinoco_ethtool_ops;
Christoph Hellwig620554e2005-06-19 01:27:33 +0200163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/* Data types */
166/********************************************************************/
167
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400168/* Beginning of the Tx descriptor, used in TxExc handling */
169struct hermes_txexc_data {
170 struct hermes_tx_descriptor desc;
Pavel Roskind133ae42005-09-23 04:18:06 -0400171 __le16 frame_ctl;
172 __le16 duration_id;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200173 u8 addr1[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174} __attribute__ ((packed));
175
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200176/* Rx frame header except compatibility 802.3 header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177struct hermes_rx_descriptor {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200178 /* Control */
Pavel Roskind133ae42005-09-23 04:18:06 -0400179 __le16 status;
180 __le32 time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 u8 silence;
182 u8 signal;
183 u8 rate;
184 u8 rxflow;
Pavel Roskind133ae42005-09-23 04:18:06 -0400185 __le32 reserved;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200186
187 /* 802.11 header */
Pavel Roskind133ae42005-09-23 04:18:06 -0400188 __le16 frame_ctl;
189 __le16 duration_id;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200190 u8 addr1[ETH_ALEN];
191 u8 addr2[ETH_ALEN];
192 u8 addr3[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -0400193 __le16 seq_ctl;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200194 u8 addr4[ETH_ALEN];
195
196 /* Data length */
Pavel Roskind133ae42005-09-23 04:18:06 -0400197 __le16 data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198} __attribute__ ((packed));
199
David Kilroy47166792009-01-07 00:43:54 +0000200struct orinoco_rx_data {
201 struct hermes_rx_descriptor *desc;
202 struct sk_buff *skb;
203 struct list_head list;
204};
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/********************************************************************/
207/* Function prototypes */
208/********************************************************************/
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210static void __orinoco_set_multicast_list(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212/********************************************************************/
213/* Internal helper functions */
214/********************************************************************/
215
David Kilroycb1576a2009-02-04 23:05:56 +0000216void set_port_type(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 switch (priv->iw_mode) {
219 case IW_MODE_INFRA:
220 priv->port_type = 1;
221 priv->createibss = 0;
222 break;
223 case IW_MODE_ADHOC:
224 if (priv->prefer_port3) {
225 priv->port_type = 3;
226 priv->createibss = 0;
227 } else {
228 priv->port_type = priv->ibss_port;
229 priv->createibss = 1;
230 }
231 break;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200232 case IW_MODE_MONITOR:
233 priv->port_type = 3;
234 priv->createibss = 0;
235 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 default:
237 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
238 priv->ndev->name);
239 }
240}
241
David Kilroy3994d502008-08-21 23:27:54 +0100242/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/* Device methods */
244/********************************************************************/
245
246static int orinoco_open(struct net_device *dev)
247{
248 struct orinoco_private *priv = netdev_priv(dev);
249 unsigned long flags;
250 int err;
251
252 if (orinoco_lock(priv, &flags) != 0)
253 return -EBUSY;
254
255 err = __orinoco_up(dev);
256
David Kilroya94e8422009-02-04 23:05:44 +0000257 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 priv->open = 1;
259
260 orinoco_unlock(priv, &flags);
261
262 return err;
263}
264
Christoph Hellwigad8f4512005-05-14 17:30:17 +0200265static int orinoco_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 struct orinoco_private *priv = netdev_priv(dev);
268 int err = 0;
269
270 /* We mustn't use orinoco_lock() here, because we need to be
271 able to close the interface even if hw_unavailable is set
272 (e.g. as we're released after a PC Card removal) */
273 spin_lock_irq(&priv->lock);
274
275 priv->open = 0;
276
277 err = __orinoco_down(dev);
278
279 spin_unlock_irq(&priv->lock);
280
281 return err;
282}
283
284static struct net_device_stats *orinoco_get_stats(struct net_device *dev)
285{
286 struct orinoco_private *priv = netdev_priv(dev);
David Kilroy6fe9deb2009-02-04 23:05:43 +0000287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return &priv->stats;
289}
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291static void orinoco_set_multicast_list(struct net_device *dev)
292{
293 struct orinoco_private *priv = netdev_priv(dev);
294 unsigned long flags;
295
296 if (orinoco_lock(priv, &flags) != 0) {
297 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
298 "called when hw_unavailable\n", dev->name);
299 return;
300 }
301
302 __orinoco_set_multicast_list(dev);
303 orinoco_unlock(priv, &flags);
304}
305
306static int orinoco_change_mtu(struct net_device *dev, int new_mtu)
307{
308 struct orinoco_private *priv = netdev_priv(dev);
309
David Kilroya94e8422009-02-04 23:05:44 +0000310 if ((new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return -EINVAL;
312
Johannes Berg2c7060022008-10-30 22:09:54 +0100313 /* MTU + encapsulation + header length */
David Kilroya94e8422009-02-04 23:05:44 +0000314 if ((new_mtu + ENCAPS_OVERHEAD + sizeof(struct ieee80211_hdr)) >
315 (priv->nicbuf_size - ETH_HLEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return -EINVAL;
317
318 dev->mtu = new_mtu;
319
320 return 0;
321}
322
323/********************************************************************/
324/* Tx path */
325/********************************************************************/
326
327static int orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
328{
329 struct orinoco_private *priv = netdev_priv(dev);
330 struct net_device_stats *stats = &priv->stats;
331 hermes_t *hw = &priv->hw;
332 int err = 0;
333 u16 txfid = priv->txfid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 struct ethhdr *eh;
David Kilroy6eecad72008-08-21 23:27:56 +0100335 int tx_control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 unsigned long flags;
337
David Kilroya94e8422009-02-04 23:05:44 +0000338 if (!netif_running(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 printk(KERN_ERR "%s: Tx on stopped device!\n",
340 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400341 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
David Kilroy6fe9deb2009-02-04 23:05:43 +0000343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (netif_queue_stopped(dev)) {
David Kilroy6fe9deb2009-02-04 23:05:43 +0000345 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400347 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
David Kilroy6fe9deb2009-02-04 23:05:43 +0000349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (orinoco_lock(priv, &flags) != 0) {
351 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
352 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400353 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355
David Kilroya94e8422009-02-04 23:05:44 +0000356 if (!netif_carrier_ok(dev) || (priv->iw_mode == IW_MODE_MONITOR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 /* Oops, the firmware hasn't established a connection,
David Kilroy6fe9deb2009-02-04 23:05:43 +0000358 silently drop the packet (this seems to be the
359 safest approach). */
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400360 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362
Pavel Roskin8d5be082006-04-07 04:10:41 -0400363 /* Check packet length */
Pavel Roskina28dc812006-04-07 04:10:45 -0400364 if (skb->len < ETH_HLEN)
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400365 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
David Kilroy6eecad72008-08-21 23:27:56 +0100367 tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
David Kilroy23edcc42008-08-21 23:28:05 +0100369 if (priv->encode_alg == IW_ENCODE_ALG_TKIP)
370 tx_control |= (priv->tx_key << HERMES_MIC_KEY_ID_SHIFT) |
371 HERMES_TXCTRL_MIC;
372
David Kilroy6eecad72008-08-21 23:27:56 +0100373 if (priv->has_alt_txcntl) {
374 /* WPA enabled firmwares have tx_cntl at the end of
375 * the 802.11 header. So write zeroed descriptor and
376 * 802.11 header at the same time
377 */
378 char desc[HERMES_802_3_OFFSET];
379 __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
380
381 memset(&desc, 0, sizeof(desc));
382
383 *txcntl = cpu_to_le16(tx_control);
384 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
385 txfid, 0);
386 if (err) {
387 if (net_ratelimit())
388 printk(KERN_ERR "%s: Error %d writing Tx "
389 "descriptor to BAP\n", dev->name, err);
390 goto busy;
391 }
392 } else {
393 struct hermes_tx_descriptor desc;
394
395 memset(&desc, 0, sizeof(desc));
396
397 desc.tx_control = cpu_to_le16(tx_control);
398 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
399 txfid, 0);
400 if (err) {
401 if (net_ratelimit())
402 printk(KERN_ERR "%s: Error %d writing Tx "
403 "descriptor to BAP\n", dev->name, err);
404 goto busy;
405 }
406
407 /* Clear the 802.11 header and data length fields - some
408 * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
409 * if this isn't done. */
410 hermes_clear_words(hw, HERMES_DATA0,
411 HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
David Kilroy23edcc42008-08-21 23:28:05 +0100414 eh = (struct ethhdr *)skb->data;
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 /* Encapsulate Ethernet-II frames */
417 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
Pavel Roskina28dc812006-04-07 04:10:45 -0400418 struct header_struct {
419 struct ethhdr eth; /* 802.3 header */
420 u8 encap[6]; /* 802.2 header */
421 } __attribute__ ((packed)) hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Pavel Roskina28dc812006-04-07 04:10:45 -0400423 /* Strip destination and source from the data */
424 skb_pull(skb, 2 * ETH_ALEN);
Pavel Roskina28dc812006-04-07 04:10:45 -0400425
426 /* And move them to a separate header */
427 memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
428 hdr.eth.h_proto = htons(sizeof(encaps_hdr) + skb->len);
429 memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
430
David Kilroy23edcc42008-08-21 23:28:05 +0100431 /* Insert the SNAP header */
432 if (skb_headroom(skb) < sizeof(hdr)) {
433 printk(KERN_ERR
434 "%s: Not enough headroom for 802.2 headers %d\n",
435 dev->name, skb_headroom(skb));
436 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
David Kilroy23edcc42008-08-21 23:28:05 +0100438 eh = (struct ethhdr *) skb_push(skb, sizeof(hdr));
439 memcpy(eh, &hdr, sizeof(hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
Pavel Roskina28dc812006-04-07 04:10:45 -0400442 err = hermes_bap_pwrite(hw, USER_BAP, skb->data, skb->len,
David Kilroy23edcc42008-08-21 23:28:05 +0100443 txfid, HERMES_802_3_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (err) {
445 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
446 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400447 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449
David Kilroy23edcc42008-08-21 23:28:05 +0100450 /* Calculate Michael MIC */
451 if (priv->encode_alg == IW_ENCODE_ALG_TKIP) {
452 u8 mic_buf[MICHAEL_MIC_LEN + 1];
453 u8 *mic;
454 size_t offset;
455 size_t len;
456
457 if (skb->len % 2) {
458 /* MIC start is on an odd boundary */
459 mic_buf[0] = skb->data[skb->len - 1];
460 mic = &mic_buf[1];
461 offset = skb->len - 1;
462 len = MICHAEL_MIC_LEN + 1;
463 } else {
464 mic = &mic_buf[0];
465 offset = skb->len;
466 len = MICHAEL_MIC_LEN;
467 }
468
David Kilroy4adb4742009-02-04 23:05:51 +0000469 orinoco_mic(priv->tx_tfm_mic,
David Kilroy23edcc42008-08-21 23:28:05 +0100470 priv->tkip_key[priv->tx_key].tx_mic,
471 eh->h_dest, eh->h_source, 0 /* priority */,
472 skb->data + ETH_HLEN, skb->len - ETH_HLEN, mic);
473
474 /* Write the MIC */
475 err = hermes_bap_pwrite(hw, USER_BAP, &mic_buf[0], len,
476 txfid, HERMES_802_3_OFFSET + offset);
477 if (err) {
478 printk(KERN_ERR "%s: Error %d writing MIC to BAP\n",
479 dev->name, err);
480 goto busy;
481 }
482 }
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 /* Finally, we actually initiate the send */
485 netif_stop_queue(dev);
486
487 err = hermes_docmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
488 txfid, NULL);
489 if (err) {
490 netif_start_queue(dev);
Andrew Mortonc367c212005-10-19 21:23:44 -0700491 if (net_ratelimit())
492 printk(KERN_ERR "%s: Error %d transmitting packet\n",
493 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400494 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496
497 dev->trans_start = jiffies;
David Kilroy23edcc42008-08-21 23:28:05 +0100498 stats->tx_bytes += HERMES_802_3_OFFSET + skb->len;
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400499 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400501 drop:
502 stats->tx_errors++;
503 stats->tx_dropped++;
504
505 ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 dev_kfree_skb(skb);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400508 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400510 busy:
Jiri Benc2c1bd262006-04-07 04:10:47 -0400511 if (err == -EIO)
512 schedule_work(&priv->reset_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 orinoco_unlock(priv, &flags);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400514 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
517static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
518{
519 struct orinoco_private *priv = netdev_priv(dev);
520 u16 fid = hermes_read_regn(hw, ALLOCFID);
521
522 if (fid != priv->txfid) {
523 if (fid != DUMMY_FID)
524 printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
525 dev->name, fid);
526 return;
527 }
528
529 hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
530}
531
532static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
533{
534 struct orinoco_private *priv = netdev_priv(dev);
535 struct net_device_stats *stats = &priv->stats;
536
537 stats->tx_packets++;
538
539 netif_wake_queue(dev);
540
541 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
542}
543
544static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
545{
546 struct orinoco_private *priv = netdev_priv(dev);
547 struct net_device_stats *stats = &priv->stats;
548 u16 fid = hermes_read_regn(hw, TXCOMPLFID);
Pavel Roskind133ae42005-09-23 04:18:06 -0400549 u16 status;
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400550 struct hermes_txexc_data hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 int err = 0;
552
553 if (fid == DUMMY_FID)
554 return; /* Nothing's really happened */
555
Pavel Roskin48ca7032005-09-23 04:18:06 -0400556 /* Read part of the frame header - we need status and addr1 */
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200557 err = hermes_bap_pread(hw, IRQ_BAP, &hdr,
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400558 sizeof(struct hermes_txexc_data),
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200559 fid, 0);
560
561 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
562 stats->tx_errors++;
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (err) {
565 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
566 "(FID=%04X error %d)\n",
567 dev->name, fid, err);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200568 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
David Kilroy6fe9deb2009-02-04 23:05:43 +0000570
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200571 DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
572 err, fid);
David Kilroy6fe9deb2009-02-04 23:05:43 +0000573
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200574 /* We produce a TXDROP event only for retry or lifetime
575 * exceeded, because that's the only status that really mean
576 * that this particular node went away.
577 * Other errors means that *we* screwed up. - Jean II */
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400578 status = le16_to_cpu(hdr.desc.status);
Pavel Roskind133ae42005-09-23 04:18:06 -0400579 if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200580 union iwreq_data wrqu;
581
582 /* Copy 802.11 dest address.
583 * We use the 802.11 header because the frame may
584 * not be 802.3 or may be mangled...
585 * In Ad-Hoc mode, it will be the node address.
586 * In managed mode, it will be most likely the AP addr
587 * User space will figure out how to convert it to
588 * whatever it needs (IP address or else).
589 * - Jean II */
590 memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
591 wrqu.addr.sa_family = ARPHRD_ETHER;
592
593 /* Send event to user space */
594 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
600static void orinoco_tx_timeout(struct net_device *dev)
601{
602 struct orinoco_private *priv = netdev_priv(dev);
603 struct net_device_stats *stats = &priv->stats;
604 struct hermes *hw = &priv->hw;
605
606 printk(KERN_WARNING "%s: Tx timeout! "
607 "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
608 dev->name, hermes_read_regn(hw, ALLOCFID),
609 hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
610
611 stats->tx_errors++;
612
613 schedule_work(&priv->reset_work);
614}
615
616/********************************************************************/
617/* Rx path (data frames) */
618/********************************************************************/
619
620/* Does the frame have a SNAP header indicating it should be
621 * de-encapsulated to Ethernet-II? */
622static inline int is_ethersnap(void *_hdr)
623{
624 u8 *hdr = _hdr;
625
626 /* We de-encapsulate all packets which, a) have SNAP headers
627 * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
628 * and where b) the OUI of the SNAP header is 00:00:00 or
629 * 00:00:f8 - we need both because different APs appear to use
630 * different OUIs for some reason */
631 return (memcmp(hdr, &encaps_hdr, 5) == 0)
David Kilroya94e8422009-02-04 23:05:44 +0000632 && ((hdr[5] == 0x00) || (hdr[5] == 0xf8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
635static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
636 int level, int noise)
637{
Pavel Roskin343c6862005-09-09 18:43:02 -0400638 struct iw_quality wstats;
639 wstats.level = level - 0x95;
640 wstats.noise = noise - 0x95;
641 wstats.qual = (level > noise) ? (level - noise) : 0;
Andrey Borzenkovf941f852008-11-15 17:15:09 +0300642 wstats.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
Pavel Roskin343c6862005-09-09 18:43:02 -0400643 /* Update spy records */
644 wireless_spy_update(dev, mac, &wstats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
647static void orinoco_stat_gather(struct net_device *dev,
648 struct sk_buff *skb,
649 struct hermes_rx_descriptor *desc)
650{
651 struct orinoco_private *priv = netdev_priv(dev);
652
653 /* Using spy support with lots of Rx packets, like in an
654 * infrastructure (AP), will really slow down everything, because
655 * the MAC address must be compared to each entry of the spy list.
656 * If the user really asks for it (set some address in the
657 * spy list), we do it, but he will pay the price.
658 * Note that to get here, you need both WIRELESS_SPY
659 * compiled in AND some addresses in the list !!!
660 */
661 /* Note : gcc will optimise the whole section away if
662 * WIRELESS_SPY is not defined... - Jean II */
663 if (SPY_NUMBER(priv)) {
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700664 orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 desc->signal, desc->silence);
666 }
667}
668
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200669/*
670 * orinoco_rx_monitor - handle received monitor frames.
671 *
672 * Arguments:
673 * dev network device
674 * rxfid received FID
675 * desc rx descriptor of the frame
676 *
677 * Call context: interrupt
678 */
679static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
680 struct hermes_rx_descriptor *desc)
681{
682 u32 hdrlen = 30; /* return full header by default */
683 u32 datalen = 0;
684 u16 fc;
685 int err;
686 int len;
687 struct sk_buff *skb;
688 struct orinoco_private *priv = netdev_priv(dev);
689 struct net_device_stats *stats = &priv->stats;
690 hermes_t *hw = &priv->hw;
691
692 len = le16_to_cpu(desc->data_len);
693
694 /* Determine the size of the header and the data */
695 fc = le16_to_cpu(desc->frame_ctl);
696 switch (fc & IEEE80211_FCTL_FTYPE) {
697 case IEEE80211_FTYPE_DATA:
698 if ((fc & IEEE80211_FCTL_TODS)
699 && (fc & IEEE80211_FCTL_FROMDS))
700 hdrlen = 30;
701 else
702 hdrlen = 24;
703 datalen = len;
704 break;
705 case IEEE80211_FTYPE_MGMT:
706 hdrlen = 24;
707 datalen = len;
708 break;
709 case IEEE80211_FTYPE_CTL:
710 switch (fc & IEEE80211_FCTL_STYPE) {
711 case IEEE80211_STYPE_PSPOLL:
712 case IEEE80211_STYPE_RTS:
713 case IEEE80211_STYPE_CFEND:
714 case IEEE80211_STYPE_CFENDACK:
715 hdrlen = 16;
716 break;
717 case IEEE80211_STYPE_CTS:
718 case IEEE80211_STYPE_ACK:
719 hdrlen = 10;
720 break;
721 }
722 break;
723 default:
724 /* Unknown frame type */
725 break;
726 }
727
728 /* sanity check the length */
Johannes Berg2c7060022008-10-30 22:09:54 +0100729 if (datalen > IEEE80211_MAX_DATA_LEN + 12) {
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200730 printk(KERN_DEBUG "%s: oversized monitor frame, "
731 "data length = %d\n", dev->name, datalen);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200732 stats->rx_length_errors++;
733 goto update_stats;
734 }
735
736 skb = dev_alloc_skb(hdrlen + datalen);
737 if (!skb) {
738 printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
739 dev->name);
Florin Malitabb6e0932006-05-22 22:35:30 -0700740 goto update_stats;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200741 }
742
743 /* Copy the 802.11 header to the skb */
744 memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700745 skb_reset_mac_header(skb);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200746
747 /* If any, copy the data from the card to the skb */
748 if (datalen > 0) {
749 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
750 ALIGN(datalen, 2), rxfid,
751 HERMES_802_2_OFFSET);
752 if (err) {
753 printk(KERN_ERR "%s: error %d reading monitor frame\n",
754 dev->name, err);
755 goto drop;
756 }
757 }
758
759 skb->dev = dev;
760 skb->ip_summed = CHECKSUM_NONE;
761 skb->pkt_type = PACKET_OTHERHOST;
Harvey Harrisonc1b4aa32009-01-29 13:26:44 -0800762 skb->protocol = cpu_to_be16(ETH_P_802_2);
David Kilroy6fe9deb2009-02-04 23:05:43 +0000763
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200764 stats->rx_packets++;
765 stats->rx_bytes += skb->len;
766
767 netif_rx(skb);
768 return;
769
770 drop:
771 dev_kfree_skb_irq(skb);
772 update_stats:
773 stats->rx_errors++;
774 stats->rx_dropped++;
775}
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777static void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
778{
779 struct orinoco_private *priv = netdev_priv(dev);
780 struct net_device_stats *stats = &priv->stats;
781 struct iw_statistics *wstats = &priv->wstats;
782 struct sk_buff *skb = NULL;
David Kilroy31afcef2008-08-21 23:28:04 +0100783 u16 rxfid, status;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200784 int length;
David Kilroy31afcef2008-08-21 23:28:04 +0100785 struct hermes_rx_descriptor *desc;
786 struct orinoco_rx_data *rx_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 int err;
788
David Kilroy31afcef2008-08-21 23:28:04 +0100789 desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
790 if (!desc) {
791 printk(KERN_WARNING
792 "%s: Can't allocate space for RX descriptor\n",
793 dev->name);
794 goto update_stats;
795 }
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 rxfid = hermes_read_regn(hw, RXFID);
798
David Kilroy31afcef2008-08-21 23:28:04 +0100799 err = hermes_bap_pread(hw, IRQ_BAP, desc, sizeof(*desc),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 rxfid, 0);
801 if (err) {
802 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
803 "Frame dropped.\n", dev->name, err);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200804 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806
David Kilroy31afcef2008-08-21 23:28:04 +0100807 status = le16_to_cpu(desc->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200809 if (status & HERMES_RXSTAT_BADCRC) {
810 DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
811 dev->name);
812 stats->rx_crc_errors++;
813 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200816 /* Handle frames in monitor mode */
817 if (priv->iw_mode == IW_MODE_MONITOR) {
David Kilroy31afcef2008-08-21 23:28:04 +0100818 orinoco_rx_monitor(dev, rxfid, desc);
819 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200822 if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
823 DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
824 dev->name);
825 wstats->discard.code++;
826 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828
David Kilroy31afcef2008-08-21 23:28:04 +0100829 length = le16_to_cpu(desc->data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 /* Sanity checks */
832 if (length < 3) { /* No for even an 802.2 LLC header */
833 /* At least on Symbol firmware with PCF we get quite a
David Kilroy6fe9deb2009-02-04 23:05:43 +0000834 lot of these legitimately - Poll frames with no
835 data. */
David Kilroy31afcef2008-08-21 23:28:04 +0100836 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Johannes Berg2c7060022008-10-30 22:09:54 +0100838 if (length > IEEE80211_MAX_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
840 dev->name, length);
841 stats->rx_length_errors++;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200842 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844
David Kilroy23edcc42008-08-21 23:28:05 +0100845 /* Payload size does not include Michael MIC. Increase payload
846 * size to read it together with the data. */
847 if (status & HERMES_RXSTAT_MIC)
848 length += MICHAEL_MIC_LEN;
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 /* We need space for the packet data itself, plus an ethernet
851 header, plus 2 bytes so we can align the IP header on a
852 32bit boundary, plus 1 byte so we can read in odd length
853 packets from the card, which has an IO granularity of 16
David Kilroy6fe9deb2009-02-04 23:05:43 +0000854 bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 skb = dev_alloc_skb(length+ETH_HLEN+2+1);
856 if (!skb) {
857 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
858 dev->name);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200859 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
861
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200862 /* We'll prepend the header, so reserve space for it. The worst
863 case is no decapsulation, when 802.3 header is prepended and
864 nothing is removed. 2 is for aligning the IP header. */
865 skb_reserve(skb, ETH_HLEN + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200867 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, length),
868 ALIGN(length, 2), rxfid,
869 HERMES_802_2_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (err) {
871 printk(KERN_ERR "%s: error %d reading frame. "
872 "Frame dropped.\n", dev->name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 goto drop;
874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
David Kilroy31afcef2008-08-21 23:28:04 +0100876 /* Add desc and skb to rx queue */
877 rx_data = kzalloc(sizeof(*rx_data), GFP_ATOMIC);
878 if (!rx_data) {
879 printk(KERN_WARNING "%s: Can't allocate RX packet\n",
880 dev->name);
881 goto drop;
882 }
883 rx_data->desc = desc;
884 rx_data->skb = skb;
885 list_add_tail(&rx_data->list, &priv->rx_list);
886 tasklet_schedule(&priv->rx_tasklet);
887
888 return;
889
890drop:
891 dev_kfree_skb_irq(skb);
892update_stats:
893 stats->rx_errors++;
894 stats->rx_dropped++;
895out:
896 kfree(desc);
897}
898
899static void orinoco_rx(struct net_device *dev,
900 struct hermes_rx_descriptor *desc,
901 struct sk_buff *skb)
902{
903 struct orinoco_private *priv = netdev_priv(dev);
904 struct net_device_stats *stats = &priv->stats;
905 u16 status, fc;
906 int length;
907 struct ethhdr *hdr;
908
909 status = le16_to_cpu(desc->status);
910 length = le16_to_cpu(desc->data_len);
911 fc = le16_to_cpu(desc->frame_ctl);
912
David Kilroy23edcc42008-08-21 23:28:05 +0100913 /* Calculate and check MIC */
914 if (status & HERMES_RXSTAT_MIC) {
915 int key_id = ((status & HERMES_RXSTAT_MIC_KEY_ID) >>
916 HERMES_MIC_KEY_ID_SHIFT);
917 u8 mic[MICHAEL_MIC_LEN];
918 u8 *rxmic;
919 u8 *src = (fc & IEEE80211_FCTL_FROMDS) ?
920 desc->addr3 : desc->addr2;
921
922 /* Extract Michael MIC from payload */
923 rxmic = skb->data + skb->len - MICHAEL_MIC_LEN;
924
925 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
926 length -= MICHAEL_MIC_LEN;
927
David Kilroy4adb4742009-02-04 23:05:51 +0000928 orinoco_mic(priv->rx_tfm_mic,
David Kilroy23edcc42008-08-21 23:28:05 +0100929 priv->tkip_key[key_id].rx_mic,
930 desc->addr1,
931 src,
932 0, /* priority or QoS? */
933 skb->data,
934 skb->len,
935 &mic[0]);
936
937 if (memcmp(mic, rxmic,
938 MICHAEL_MIC_LEN)) {
939 union iwreq_data wrqu;
940 struct iw_michaelmicfailure wxmic;
David Kilroy23edcc42008-08-21 23:28:05 +0100941
942 printk(KERN_WARNING "%s: "
Johannes Berge1749612008-10-27 15:59:26 -0700943 "Invalid Michael MIC in data frame from %pM, "
David Kilroy23edcc42008-08-21 23:28:05 +0100944 "using key %i\n",
Johannes Berge1749612008-10-27 15:59:26 -0700945 dev->name, src, key_id);
David Kilroy23edcc42008-08-21 23:28:05 +0100946
947 /* TODO: update stats */
948
949 /* Notify userspace */
950 memset(&wxmic, 0, sizeof(wxmic));
951 wxmic.flags = key_id & IW_MICFAILURE_KEY_ID;
952 wxmic.flags |= (desc->addr1[0] & 1) ?
953 IW_MICFAILURE_GROUP : IW_MICFAILURE_PAIRWISE;
954 wxmic.src_addr.sa_family = ARPHRD_ETHER;
955 memcpy(wxmic.src_addr.sa_data, src, ETH_ALEN);
956
957 (void) orinoco_hw_get_tkip_iv(priv, key_id,
958 &wxmic.tsc[0]);
959
960 memset(&wrqu, 0, sizeof(wrqu));
961 wrqu.data.length = sizeof(wxmic);
962 wireless_send_event(dev, IWEVMICHAELMICFAILURE, &wrqu,
963 (char *) &wxmic);
964
965 goto drop;
966 }
967 }
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 /* Handle decapsulation
970 * In most cases, the firmware tell us about SNAP frames.
971 * For some reason, the SNAP frames sent by LinkSys APs
972 * are not properly recognised by most firmwares.
973 * So, check ourselves */
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200974 if (length >= ENCAPS_OVERHEAD &&
975 (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
976 ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
977 is_ethersnap(skb->data))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 /* These indicate a SNAP within 802.2 LLC within
979 802.11 frame which we'll need to de-encapsulate to
980 the original EthernetII frame. */
David Kilroyb2f30a02009-02-04 23:05:46 +0000981 hdr = (struct ethhdr *)skb_push(skb,
982 ETH_HLEN - ENCAPS_OVERHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 } else {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200984 /* 802.3 frame - prepend 802.3 header as is */
985 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
986 hdr->h_proto = htons(length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
David Kilroy31afcef2008-08-21 23:28:04 +0100988 memcpy(hdr->h_dest, desc->addr1, ETH_ALEN);
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200989 if (fc & IEEE80211_FCTL_FROMDS)
David Kilroy31afcef2008-08-21 23:28:04 +0100990 memcpy(hdr->h_source, desc->addr3, ETH_ALEN);
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200991 else
David Kilroy31afcef2008-08-21 23:28:04 +0100992 memcpy(hdr->h_source, desc->addr2, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 skb->protocol = eth_type_trans(skb, dev);
995 skb->ip_summed = CHECKSUM_NONE;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200996 if (fc & IEEE80211_FCTL_TODS)
997 skb->pkt_type = PACKET_OTHERHOST;
David Kilroy6fe9deb2009-02-04 23:05:43 +0000998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 /* Process the wireless stats if needed */
David Kilroy31afcef2008-08-21 23:28:04 +01001000 orinoco_stat_gather(dev, skb, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 /* Pass the packet to the networking stack */
1003 netif_rx(skb);
1004 stats->rx_packets++;
1005 stats->rx_bytes += length;
1006
1007 return;
David Kilroy23edcc42008-08-21 23:28:05 +01001008
1009 drop:
1010 dev_kfree_skb(skb);
1011 stats->rx_errors++;
1012 stats->rx_dropped++;
David Kilroy31afcef2008-08-21 23:28:04 +01001013}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
David Kilroy31afcef2008-08-21 23:28:04 +01001015static void orinoco_rx_isr_tasklet(unsigned long data)
1016{
1017 struct net_device *dev = (struct net_device *) data;
1018 struct orinoco_private *priv = netdev_priv(dev);
1019 struct orinoco_rx_data *rx_data, *temp;
1020 struct hermes_rx_descriptor *desc;
1021 struct sk_buff *skb;
David Kilroy20953ad2009-01-07 00:23:55 +00001022 unsigned long flags;
1023
1024 /* orinoco_rx requires the driver lock, and we also need to
1025 * protect priv->rx_list, so just hold the lock over the
1026 * lot.
1027 *
1028 * If orinoco_lock fails, we've unplugged the card. In this
1029 * case just abort. */
1030 if (orinoco_lock(priv, &flags) != 0)
1031 return;
David Kilroy31afcef2008-08-21 23:28:04 +01001032
1033 /* extract desc and skb from queue */
1034 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
1035 desc = rx_data->desc;
1036 skb = rx_data->skb;
1037 list_del(&rx_data->list);
1038 kfree(rx_data);
1039
1040 orinoco_rx(dev, desc, skb);
1041
1042 kfree(desc);
1043 }
David Kilroy20953ad2009-01-07 00:23:55 +00001044
1045 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
1048/********************************************************************/
1049/* Rx path (info frames) */
1050/********************************************************************/
1051
1052static void print_linkstatus(struct net_device *dev, u16 status)
1053{
David Kilroy21312662009-02-04 23:05:47 +00001054 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 if (suppress_linkstatus)
1057 return;
1058
1059 switch (status) {
1060 case HERMES_LINKSTATUS_NOT_CONNECTED:
1061 s = "Not Connected";
1062 break;
1063 case HERMES_LINKSTATUS_CONNECTED:
1064 s = "Connected";
1065 break;
1066 case HERMES_LINKSTATUS_DISCONNECTED:
1067 s = "Disconnected";
1068 break;
1069 case HERMES_LINKSTATUS_AP_CHANGE:
1070 s = "AP Changed";
1071 break;
1072 case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1073 s = "AP Out of Range";
1074 break;
1075 case HERMES_LINKSTATUS_AP_IN_RANGE:
1076 s = "AP In Range";
1077 break;
1078 case HERMES_LINKSTATUS_ASSOC_FAILED:
1079 s = "Association Failed";
1080 break;
1081 default:
1082 s = "UNKNOWN";
1083 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001084
Pavel Roskin11eaea42009-01-18 23:20:58 -05001085 printk(KERN_DEBUG "%s: New link status: %s (%04x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 dev->name, s, status);
1087}
1088
Christoph Hellwig16739b02005-06-19 01:27:51 +02001089/* Search scan results for requested BSSID, join it if found */
David Howellsc4028952006-11-22 14:57:56 +00001090static void orinoco_join_ap(struct work_struct *work)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001091{
David Howellsc4028952006-11-22 14:57:56 +00001092 struct orinoco_private *priv =
1093 container_of(work, struct orinoco_private, join_work);
1094 struct net_device *dev = priv->ndev;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001095 struct hermes *hw = &priv->hw;
1096 int err;
1097 unsigned long flags;
1098 struct join_req {
1099 u8 bssid[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -04001100 __le16 channel;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001101 } __attribute__ ((packed)) req;
1102 const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001103 struct prism2_scan_apinfo *atom = NULL;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001104 int offset = 4;
Pavel Roskinc89cc222005-09-01 20:06:06 -04001105 int found = 0;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001106 u8 *buf;
1107 u16 len;
1108
1109 /* Allocate buffer for scan results */
1110 buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
David Kilroya94e8422009-02-04 23:05:44 +00001111 if (!buf)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001112 return;
1113
1114 if (orinoco_lock(priv, &flags) != 0)
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001115 goto fail_lock;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001116
1117 /* Sanity checks in case user changed something in the meantime */
David Kilroya94e8422009-02-04 23:05:44 +00001118 if (!priv->bssid_fixed)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001119 goto out;
1120
1121 if (strlen(priv->desired_essid) == 0)
1122 goto out;
1123
1124 /* Read scan results from the firmware */
1125 err = hermes_read_ltv(hw, USER_BAP,
1126 HERMES_RID_SCANRESULTSTABLE,
1127 MAX_SCAN_LEN, &len, buf);
1128 if (err) {
1129 printk(KERN_ERR "%s: Cannot read scan results\n",
1130 dev->name);
1131 goto out;
1132 }
1133
1134 len = HERMES_RECLEN_TO_BYTES(len);
1135
1136 /* Go through the scan results looking for the channel of the AP
1137 * we were requested to join */
1138 for (; offset + atom_len <= len; offset += atom_len) {
1139 atom = (struct prism2_scan_apinfo *) (buf + offset);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001140 if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1141 found = 1;
1142 break;
1143 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001144 }
1145
David Kilroya94e8422009-02-04 23:05:44 +00001146 if (!found) {
Pavel Roskinc89cc222005-09-01 20:06:06 -04001147 DEBUG(1, "%s: Requested AP not found in scan results\n",
1148 dev->name);
1149 goto out;
1150 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001151
Christoph Hellwig16739b02005-06-19 01:27:51 +02001152 memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1153 req.channel = atom->channel; /* both are little-endian */
1154 err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1155 &req);
1156 if (err)
1157 printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1158
1159 out:
Christoph Hellwig16739b02005-06-19 01:27:51 +02001160 orinoco_unlock(priv, &flags);
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001161
1162 fail_lock:
1163 kfree(buf);
Christoph Hellwig16739b02005-06-19 01:27:51 +02001164}
1165
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001166/* Send new BSSID to userspace */
David Kilroy6cd90b12008-08-21 23:28:00 +01001167static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001168{
David Howellsc4028952006-11-22 14:57:56 +00001169 struct net_device *dev = priv->ndev;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001170 struct hermes *hw = &priv->hw;
1171 union iwreq_data wrqu;
1172 int err;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001173
David Kilroy499b7022008-12-09 21:46:29 +00001174 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001175 ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
1176 if (err != 0)
David Kilroy6cd90b12008-08-21 23:28:00 +01001177 return;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001178
1179 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1180
1181 /* Send event to user space */
1182 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001183}
1184
David Kilroy06009fd2008-08-21 23:28:03 +01001185static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
1186{
1187 struct net_device *dev = priv->ndev;
1188 struct hermes *hw = &priv->hw;
1189 union iwreq_data wrqu;
1190 int err;
1191 u8 buf[88];
1192 u8 *ie;
1193
1194 if (!priv->has_wpa)
1195 return;
1196
David Kilroy499b7022008-12-09 21:46:29 +00001197 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
David Kilroy06009fd2008-08-21 23:28:03 +01001198 sizeof(buf), NULL, &buf);
1199 if (err != 0)
1200 return;
1201
1202 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1203 if (ie) {
1204 int rem = sizeof(buf) - (ie - &buf[0]);
1205 wrqu.data.length = ie[1] + 2;
1206 if (wrqu.data.length > rem)
1207 wrqu.data.length = rem;
1208
1209 if (wrqu.data.length)
1210 /* Send event to user space */
1211 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
1212 }
1213}
1214
1215static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
1216{
1217 struct net_device *dev = priv->ndev;
1218 struct hermes *hw = &priv->hw;
1219 union iwreq_data wrqu;
1220 int err;
1221 u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
1222 u8 *ie;
1223
1224 if (!priv->has_wpa)
1225 return;
1226
David Kilroy499b7022008-12-09 21:46:29 +00001227 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_RESP_INFO,
David Kilroy06009fd2008-08-21 23:28:03 +01001228 sizeof(buf), NULL, &buf);
1229 if (err != 0)
1230 return;
1231
1232 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1233 if (ie) {
1234 int rem = sizeof(buf) - (ie - &buf[0]);
1235 wrqu.data.length = ie[1] + 2;
1236 if (wrqu.data.length > rem)
1237 wrqu.data.length = rem;
1238
1239 if (wrqu.data.length)
1240 /* Send event to user space */
1241 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
1242 }
1243}
1244
David Kilroy6cd90b12008-08-21 23:28:00 +01001245static void orinoco_send_wevents(struct work_struct *work)
1246{
1247 struct orinoco_private *priv =
1248 container_of(work, struct orinoco_private, wevent_work);
1249 unsigned long flags;
1250
1251 if (orinoco_lock(priv, &flags) != 0)
1252 return;
1253
David Kilroy06009fd2008-08-21 23:28:03 +01001254 orinoco_send_assocreqie_wevent(priv);
1255 orinoco_send_assocrespie_wevent(priv);
David Kilroy6cd90b12008-08-21 23:28:00 +01001256 orinoco_send_bssid_wevent(priv);
1257
1258 orinoco_unlock(priv, &flags);
1259}
Dan Williams1e3428e2007-10-10 23:56:25 -04001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261static void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
1262{
1263 struct orinoco_private *priv = netdev_priv(dev);
1264 u16 infofid;
1265 struct {
Pavel Roskind133ae42005-09-23 04:18:06 -04001266 __le16 len;
1267 __le16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 } __attribute__ ((packed)) info;
1269 int len, type;
1270 int err;
1271
1272 /* This is an answer to an INQUIRE command that we did earlier,
1273 * or an information "event" generated by the card
1274 * The controller return to us a pseudo frame containing
1275 * the information in question - Jean II */
1276 infofid = hermes_read_regn(hw, INFOFID);
1277
1278 /* Read the info frame header - don't try too hard */
1279 err = hermes_bap_pread(hw, IRQ_BAP, &info, sizeof(info),
1280 infofid, 0);
1281 if (err) {
1282 printk(KERN_ERR "%s: error %d reading info frame. "
1283 "Frame dropped.\n", dev->name, err);
1284 return;
1285 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
1288 type = le16_to_cpu(info.type);
1289
1290 switch (type) {
1291 case HERMES_INQ_TALLIES: {
1292 struct hermes_tallies_frame tallies;
1293 struct iw_statistics *wstats = &priv->wstats;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (len > sizeof(tallies)) {
1296 printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
1297 dev->name, len);
1298 len = sizeof(tallies);
1299 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001300
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001301 err = hermes_bap_pread(hw, IRQ_BAP, &tallies, len,
1302 infofid, sizeof(info));
1303 if (err)
1304 break;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 /* Increment our various counters */
1307 /* wstats->discard.nwid - no wrong BSSID stuff */
1308 wstats->discard.code +=
1309 le16_to_cpu(tallies.RxWEPUndecryptable);
David Kilroy6fe9deb2009-02-04 23:05:43 +00001310 if (len == sizeof(tallies))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 wstats->discard.code +=
1312 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
1313 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
1314 wstats->discard.misc +=
1315 le16_to_cpu(tallies.TxDiscardsWrongSA);
1316 wstats->discard.fragment +=
1317 le16_to_cpu(tallies.RxMsgInBadMsgFragments);
1318 wstats->discard.retries +=
1319 le16_to_cpu(tallies.TxRetryLimitExceeded);
1320 /* wstats->miss.beacon - no match */
1321 }
1322 break;
1323 case HERMES_INQ_LINKSTATUS: {
1324 struct hermes_linkstatus linkstatus;
1325 u16 newstatus;
1326 int connected;
1327
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001328 if (priv->iw_mode == IW_MODE_MONITOR)
1329 break;
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (len != sizeof(linkstatus)) {
1332 printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
1333 dev->name, len);
1334 break;
1335 }
1336
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001337 err = hermes_bap_pread(hw, IRQ_BAP, &linkstatus, len,
1338 infofid, sizeof(info));
1339 if (err)
1340 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 newstatus = le16_to_cpu(linkstatus.linkstatus);
1342
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001343 /* Symbol firmware uses "out of range" to signal that
1344 * the hostscan frame can be requested. */
1345 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
1346 priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
1347 priv->has_hostscan && priv->scan_inprogress) {
1348 hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
1349 break;
1350 }
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
1353 || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
1354 || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
1355
1356 if (connected)
1357 netif_carrier_on(dev);
David Gibson7bb7c3a2005-05-12 20:02:10 -04001358 else if (!ignore_disconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 netif_carrier_off(dev);
1360
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001361 if (newstatus != priv->last_linkstatus) {
1362 priv->last_linkstatus = newstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 print_linkstatus(dev, newstatus);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001364 /* The info frame contains only one word which is the
1365 * status (see hermes.h). The status is pretty boring
1366 * in itself, that's why we export the new BSSID...
1367 * Jean II */
1368 schedule_work(&priv->wevent_work);
1369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
1371 break;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001372 case HERMES_INQ_SCAN:
1373 if (!priv->scan_inprogress && priv->bssid_fixed &&
1374 priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
1375 schedule_work(&priv->join_work);
1376 break;
1377 }
1378 /* fall through */
1379 case HERMES_INQ_HOSTSCAN:
1380 case HERMES_INQ_HOSTSCAN_SYMBOL: {
1381 /* Result of a scanning. Contains information about
1382 * cells in the vicinity - Jean II */
1383 union iwreq_data wrqu;
1384 unsigned char *buf;
1385
Dan Williams1e3428e2007-10-10 23:56:25 -04001386 /* Scan is no longer in progress */
1387 priv->scan_inprogress = 0;
1388
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001389 /* Sanity check */
1390 if (len > 4096) {
1391 printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
1392 dev->name, len);
1393 break;
1394 }
1395
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001396 /* Allocate buffer for results */
1397 buf = kmalloc(len, GFP_ATOMIC);
1398 if (buf == NULL)
1399 /* No memory, so can't printk()... */
1400 break;
1401
1402 /* Read scan data */
1403 err = hermes_bap_pread(hw, IRQ_BAP, (void *) buf, len,
1404 infofid, sizeof(info));
Pavel Roskin708218b2005-09-01 20:05:19 -04001405 if (err) {
1406 kfree(buf);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001407 break;
Pavel Roskin708218b2005-09-01 20:05:19 -04001408 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001409
1410#ifdef ORINOCO_DEBUG
1411 {
1412 int i;
1413 printk(KERN_DEBUG "Scan result [%02X", buf[0]);
David Kilroya94e8422009-02-04 23:05:44 +00001414 for (i = 1; i < (len * 2); i++)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001415 printk(":%02X", buf[i]);
1416 printk("]\n");
1417 }
1418#endif /* ORINOCO_DEBUG */
1419
David Kilroyaea48b12009-02-04 23:05:49 +00001420 if (orinoco_process_scan_results(priv, buf, len) == 0) {
Dan Williams1e3428e2007-10-10 23:56:25 -04001421 /* Send an empty event to user space.
1422 * We don't send the received data on the event because
1423 * it would require us to do complex transcoding, and
1424 * we want to minimise the work done in the irq handler
1425 * Use a request to extract the data - Jean II */
1426 wrqu.data.length = 0;
1427 wrqu.data.flags = 0;
1428 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
1429 }
1430 kfree(buf);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001431 }
1432 break;
David Kilroy01632fa2008-08-21 23:27:58 +01001433 case HERMES_INQ_CHANNELINFO:
1434 {
1435 struct agere_ext_scan_info *bss;
1436
1437 if (!priv->scan_inprogress) {
1438 printk(KERN_DEBUG "%s: Got chaninfo without scan, "
1439 "len=%d\n", dev->name, len);
1440 break;
1441 }
1442
1443 /* An empty result indicates that the scan is complete */
1444 if (len == 0) {
1445 union iwreq_data wrqu;
1446
1447 /* Scan is no longer in progress */
1448 priv->scan_inprogress = 0;
1449
1450 wrqu.data.length = 0;
1451 wrqu.data.flags = 0;
1452 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
1453 break;
1454 }
1455
1456 /* Sanity check */
1457 else if (len > sizeof(*bss)) {
1458 printk(KERN_WARNING
1459 "%s: Ext scan results too large (%d bytes). "
1460 "Truncating results to %zd bytes.\n",
1461 dev->name, len, sizeof(*bss));
1462 len = sizeof(*bss);
1463 } else if (len < (offsetof(struct agere_ext_scan_info,
1464 data) + 2)) {
1465 /* Drop this result now so we don't have to
1466 * keep checking later */
1467 printk(KERN_WARNING
1468 "%s: Ext scan results too short (%d bytes)\n",
1469 dev->name, len);
1470 break;
1471 }
1472
1473 bss = kmalloc(sizeof(*bss), GFP_ATOMIC);
1474 if (bss == NULL)
1475 break;
1476
1477 /* Read scan data */
1478 err = hermes_bap_pread(hw, IRQ_BAP, (void *) bss, len,
1479 infofid, sizeof(info));
1480 if (err) {
1481 kfree(bss);
1482 break;
1483 }
1484
1485 orinoco_add_ext_scan_result(priv, bss);
1486
1487 kfree(bss);
1488 break;
1489 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001490 case HERMES_INQ_SEC_STAT_AGERE:
1491 /* Security status (Agere specific) */
1492 /* Ignore this frame for now */
1493 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
1494 break;
1495 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 default:
1497 printk(KERN_DEBUG "%s: Unknown information frame received: "
1498 "type 0x%04x, length %d\n", dev->name, type, len);
1499 /* We don't actually do anything about it */
1500 break;
1501 }
1502}
1503
1504static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
1505{
1506 if (net_ratelimit())
1507 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
1508}
1509
1510/********************************************************************/
1511/* Internal hardware control routines */
1512/********************************************************************/
1513
1514int __orinoco_up(struct net_device *dev)
1515{
1516 struct orinoco_private *priv = netdev_priv(dev);
1517 struct hermes *hw = &priv->hw;
1518 int err;
1519
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001520 netif_carrier_off(dev); /* just to make sure */
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 err = __orinoco_program_rids(dev);
1523 if (err) {
1524 printk(KERN_ERR "%s: Error %d configuring card\n",
1525 dev->name, err);
1526 return err;
1527 }
1528
1529 /* Fire things up again */
1530 hermes_set_irqmask(hw, ORINOCO_INTEN);
1531 err = hermes_enable_port(hw, 0);
1532 if (err) {
1533 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
1534 dev->name, err);
1535 return err;
1536 }
1537
1538 netif_start_queue(dev);
1539
1540 return 0;
1541}
David Kilroy21312662009-02-04 23:05:47 +00001542EXPORT_SYMBOL(__orinoco_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544int __orinoco_down(struct net_device *dev)
1545{
1546 struct orinoco_private *priv = netdev_priv(dev);
1547 struct hermes *hw = &priv->hw;
1548 int err;
1549
1550 netif_stop_queue(dev);
1551
David Kilroya94e8422009-02-04 23:05:44 +00001552 if (!priv->hw_unavailable) {
1553 if (!priv->broken_disableport) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 err = hermes_disable_port(hw, 0);
1555 if (err) {
1556 /* Some firmwares (e.g. Intersil 1.3.x) seem
1557 * to have problems disabling the port, oh
1558 * well, too bad. */
1559 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
1560 dev->name, err);
1561 priv->broken_disableport = 1;
1562 }
1563 }
1564 hermes_set_irqmask(hw, 0);
1565 hermes_write_regn(hw, EVACK, 0xffff);
1566 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 /* firmware will have to reassociate */
1569 netif_carrier_off(dev);
1570 priv->last_linkstatus = 0xffff;
1571
1572 return 0;
1573}
David Kilroy21312662009-02-04 23:05:47 +00001574EXPORT_SYMBOL(__orinoco_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Pavel Roskin37a6c612006-04-07 04:10:49 -04001576int orinoco_reinit_firmware(struct net_device *dev)
1577{
1578 struct orinoco_private *priv = netdev_priv(dev);
1579 struct hermes *hw = &priv->hw;
1580 int err;
1581
1582 err = hermes_init(hw);
Andrey Borzenkov0df6cbb2008-10-12 20:15:43 +04001583 if (priv->do_fw_download && !err) {
1584 err = orinoco_download(priv);
1585 if (err)
1586 priv->do_fw_download = 0;
1587 }
Pavel Roskin37a6c612006-04-07 04:10:49 -04001588 if (!err)
David Kilroy42a51b92009-06-18 23:21:20 +01001589 err = orinoco_hw_allocate_fid(priv);
Pavel Roskin37a6c612006-04-07 04:10:49 -04001590
1591 return err;
1592}
David Kilroy21312662009-02-04 23:05:47 +00001593EXPORT_SYMBOL(orinoco_reinit_firmware);
Pavel Roskin37a6c612006-04-07 04:10:49 -04001594
David Kilroycb1576a2009-02-04 23:05:56 +00001595int __orinoco_program_rids(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
1597 struct orinoco_private *priv = netdev_priv(dev);
1598 hermes_t *hw = &priv->hw;
1599 int err;
1600 struct hermes_idstring idbuf;
1601
1602 /* Set the MAC address */
1603 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
1604 HERMES_BYTES_TO_RECLEN(ETH_ALEN), dev->dev_addr);
1605 if (err) {
1606 printk(KERN_ERR "%s: Error %d setting MAC address\n",
1607 dev->name, err);
1608 return err;
1609 }
1610
1611 /* Set up the link mode */
1612 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFPORTTYPE,
1613 priv->port_type);
1614 if (err) {
1615 printk(KERN_ERR "%s: Error %d setting port type\n",
1616 dev->name, err);
1617 return err;
1618 }
1619 /* Set the channel/frequency */
David Gibsond51d8b12005-05-12 20:03:36 -04001620 if (priv->channel != 0 && priv->iw_mode != IW_MODE_INFRA) {
1621 err = hermes_write_wordrec(hw, USER_BAP,
1622 HERMES_RID_CNFOWNCHANNEL,
1623 priv->channel);
1624 if (err) {
1625 printk(KERN_ERR "%s: Error %d setting channel %d\n",
1626 dev->name, err, priv->channel);
1627 return err;
1628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 }
1630
1631 if (priv->has_ibss) {
1632 u16 createibss;
1633
1634 if ((strlen(priv->desired_essid) == 0) && (priv->createibss)) {
1635 printk(KERN_WARNING "%s: This firmware requires an "
1636 "ESSID in IBSS-Ad-Hoc mode.\n", dev->name);
1637 /* With wvlan_cs, in this case, we would crash.
1638 * hopefully, this driver will behave better...
1639 * Jean II */
1640 createibss = 0;
1641 } else {
1642 createibss = priv->createibss;
1643 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 err = hermes_write_wordrec(hw, USER_BAP,
1646 HERMES_RID_CNFCREATEIBSS,
1647 createibss);
1648 if (err) {
1649 printk(KERN_ERR "%s: Error %d setting CREATEIBSS\n",
1650 dev->name, err);
1651 return err;
1652 }
1653 }
1654
Christoph Hellwig16739b02005-06-19 01:27:51 +02001655 /* Set the desired BSSID */
1656 err = __orinoco_hw_set_wap(priv);
1657 if (err) {
1658 printk(KERN_ERR "%s: Error %d setting AP address\n",
1659 dev->name, err);
1660 return err;
1661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 /* Set the desired ESSID */
1663 idbuf.len = cpu_to_le16(strlen(priv->desired_essid));
1664 memcpy(&idbuf.val, priv->desired_essid, sizeof(idbuf.val));
1665 /* WinXP wants partner to configure OWNSSID even in IBSS mode. (jimc) */
1666 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNSSID,
David Kilroyb2f30a02009-02-04 23:05:46 +00001667 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
1668 &idbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 if (err) {
1670 printk(KERN_ERR "%s: Error %d setting OWNSSID\n",
1671 dev->name, err);
1672 return err;
1673 }
1674 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFDESIREDSSID,
David Kilroyb2f30a02009-02-04 23:05:46 +00001675 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
1676 &idbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 if (err) {
1678 printk(KERN_ERR "%s: Error %d setting DESIREDSSID\n",
1679 dev->name, err);
1680 return err;
1681 }
1682
1683 /* Set the station name */
1684 idbuf.len = cpu_to_le16(strlen(priv->nick));
1685 memcpy(&idbuf.val, priv->nick, sizeof(idbuf.val));
1686 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
1687 HERMES_BYTES_TO_RECLEN(strlen(priv->nick)+2),
1688 &idbuf);
1689 if (err) {
1690 printk(KERN_ERR "%s: Error %d setting nickname\n",
1691 dev->name, err);
1692 return err;
1693 }
1694
1695 /* Set AP density */
1696 if (priv->has_sensitivity) {
1697 err = hermes_write_wordrec(hw, USER_BAP,
1698 HERMES_RID_CNFSYSTEMSCALE,
1699 priv->ap_density);
1700 if (err) {
David Kilroyb2f30a02009-02-04 23:05:46 +00001701 printk(KERN_WARNING "%s: Error %d setting SYSTEMSCALE. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 "Disabling sensitivity control\n",
1703 dev->name, err);
1704
1705 priv->has_sensitivity = 0;
1706 }
1707 }
1708
1709 /* Set RTS threshold */
1710 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
1711 priv->rts_thresh);
1712 if (err) {
1713 printk(KERN_ERR "%s: Error %d setting RTS threshold\n",
1714 dev->name, err);
1715 return err;
1716 }
1717
1718 /* Set fragmentation threshold or MWO robustness */
1719 if (priv->has_mwo)
1720 err = hermes_write_wordrec(hw, USER_BAP,
1721 HERMES_RID_CNFMWOROBUST_AGERE,
1722 priv->mwo_robust);
1723 else
1724 err = hermes_write_wordrec(hw, USER_BAP,
1725 HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
1726 priv->frag_thresh);
1727 if (err) {
1728 printk(KERN_ERR "%s: Error %d setting fragmentation\n",
1729 dev->name, err);
1730 return err;
1731 }
1732
1733 /* Set bitrate */
1734 err = __orinoco_hw_set_bitrate(priv);
1735 if (err) {
1736 printk(KERN_ERR "%s: Error %d setting bitrate\n",
1737 dev->name, err);
1738 return err;
1739 }
1740
1741 /* Set power management */
1742 if (priv->has_pm) {
1743 err = hermes_write_wordrec(hw, USER_BAP,
1744 HERMES_RID_CNFPMENABLED,
1745 priv->pm_on);
1746 if (err) {
1747 printk(KERN_ERR "%s: Error %d setting up PM\n",
1748 dev->name, err);
1749 return err;
1750 }
1751
1752 err = hermes_write_wordrec(hw, USER_BAP,
1753 HERMES_RID_CNFMULTICASTRECEIVE,
1754 priv->pm_mcast);
1755 if (err) {
1756 printk(KERN_ERR "%s: Error %d setting up PM\n",
1757 dev->name, err);
1758 return err;
1759 }
1760 err = hermes_write_wordrec(hw, USER_BAP,
1761 HERMES_RID_CNFMAXSLEEPDURATION,
1762 priv->pm_period);
1763 if (err) {
1764 printk(KERN_ERR "%s: Error %d setting up PM\n",
1765 dev->name, err);
1766 return err;
1767 }
1768 err = hermes_write_wordrec(hw, USER_BAP,
1769 HERMES_RID_CNFPMHOLDOVERDURATION,
1770 priv->pm_timeout);
1771 if (err) {
1772 printk(KERN_ERR "%s: Error %d setting up PM\n",
1773 dev->name, err);
1774 return err;
1775 }
1776 }
1777
1778 /* Set preamble - only for Symbol so far... */
1779 if (priv->has_preamble) {
1780 err = hermes_write_wordrec(hw, USER_BAP,
1781 HERMES_RID_CNFPREAMBLE_SYMBOL,
1782 priv->preamble);
1783 if (err) {
1784 printk(KERN_ERR "%s: Error %d setting preamble\n",
1785 dev->name, err);
1786 return err;
1787 }
1788 }
1789
1790 /* Set up encryption */
David Kilroyd03032a2008-08-21 23:28:02 +01001791 if (priv->has_wep || priv->has_wpa) {
1792 err = __orinoco_hw_setup_enc(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (err) {
David Kilroyd03032a2008-08-21 23:28:02 +01001794 printk(KERN_ERR "%s: Error %d activating encryption\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 dev->name, err);
1796 return err;
1797 }
1798 }
1799
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001800 if (priv->iw_mode == IW_MODE_MONITOR) {
1801 /* Enable monitor mode */
1802 dev->type = ARPHRD_IEEE80211;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001803 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001804 HERMES_TEST_MONITOR, 0, NULL);
1805 } else {
1806 /* Disable monitor mode */
1807 dev->type = ARPHRD_ETHER;
1808 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
1809 HERMES_TEST_STOP, 0, NULL);
1810 }
1811 if (err)
1812 return err;
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 /* Set promiscuity / multicast*/
1815 priv->promiscuous = 0;
1816 priv->mc_count = 0;
Herbert Xu932ff272006-06-09 12:20:56 -07001817
1818 /* FIXME: what about netif_tx_lock */
1819 __orinoco_set_multicast_list(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
1821 return 0;
1822}
1823
David Kilroy5865d012009-02-04 23:05:54 +00001824/* FIXME: return int? */
1825static void
1826__orinoco_set_multicast_list(struct net_device *dev)
1827{
1828 struct orinoco_private *priv = netdev_priv(dev);
1829 int err = 0;
1830 int promisc, mc_count;
1831
1832 /* The Hermes doesn't seem to have an allmulti mode, so we go
1833 * into promiscuous mode and let the upper levels deal. */
1834 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
1835 (dev->mc_count > MAX_MULTICAST(priv))) {
1836 promisc = 1;
1837 mc_count = 0;
1838 } else {
1839 promisc = 0;
1840 mc_count = dev->mc_count;
1841 }
1842
1843 err = __orinoco_hw_set_multicast_list(priv, dev->mc_list, mc_count,
1844 promisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
1846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847/* This must be called from user context, without locks held - use
1848 * schedule_work() */
David Kilroycb1576a2009-02-04 23:05:56 +00001849void orinoco_reset(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
David Howellsc4028952006-11-22 14:57:56 +00001851 struct orinoco_private *priv =
1852 container_of(work, struct orinoco_private, reset_work);
1853 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 struct hermes *hw = &priv->hw;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001855 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 unsigned long flags;
1857
1858 if (orinoco_lock(priv, &flags) != 0)
1859 /* When the hardware becomes available again, whatever
1860 * detects that is responsible for re-initializing
1861 * it. So no need for anything further */
1862 return;
1863
1864 netif_stop_queue(dev);
1865
1866 /* Shut off interrupts. Depending on what state the hardware
1867 * is in, this might not work, but we'll try anyway */
1868 hermes_set_irqmask(hw, 0);
1869 hermes_write_regn(hw, EVACK, 0xffff);
1870
1871 priv->hw_unavailable++;
1872 priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
1873 netif_carrier_off(dev);
1874
1875 orinoco_unlock(priv, &flags);
1876
David Kilroy6fe9deb2009-02-04 23:05:43 +00001877 /* Scanning support: Cleanup of driver struct */
Dan Williams1e3428e2007-10-10 23:56:25 -04001878 orinoco_clear_scan_results(priv, 0);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001879 priv->scan_inprogress = 0;
1880
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001881 if (priv->hard_reset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 err = (*priv->hard_reset)(priv);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001883 if (err) {
1884 printk(KERN_ERR "%s: orinoco_reset: Error %d "
1885 "performing hard reset\n", dev->name, err);
1886 goto disable;
1887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
1889
1890 err = orinoco_reinit_firmware(dev);
1891 if (err) {
1892 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
1893 dev->name, err);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001894 goto disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
1896
David Kilroyb2f30a02009-02-04 23:05:46 +00001897 /* This has to be called from user context */
1898 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 priv->hw_unavailable--;
1901
1902 /* priv->open or priv->hw_unavailable might have changed while
1903 * we dropped the lock */
David Kilroya94e8422009-02-04 23:05:44 +00001904 if (priv->open && (!priv->hw_unavailable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 err = __orinoco_up(dev);
1906 if (err) {
1907 printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
1908 dev->name, err);
1909 } else
1910 dev->trans_start = jiffies;
1911 }
1912
1913 spin_unlock_irq(&priv->lock);
1914
1915 return;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001916 disable:
1917 hermes_set_irqmask(hw, 0);
1918 netif_device_detach(dev);
1919 printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920}
1921
1922/********************************************************************/
1923/* Interrupt handler */
1924/********************************************************************/
1925
1926static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
1927{
1928 printk(KERN_DEBUG "%s: TICK\n", dev->name);
1929}
1930
1931static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
1932{
1933 /* This seems to happen a fair bit under load, but ignoring it
1934 seems to work fine...*/
1935 printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
1936 dev->name);
1937}
1938
David Howells7d12e782006-10-05 14:55:46 +01001939irqreturn_t orinoco_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
Jeff Garzikc31f28e2006-10-06 14:56:04 -04001941 struct net_device *dev = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 struct orinoco_private *priv = netdev_priv(dev);
1943 hermes_t *hw = &priv->hw;
1944 int count = MAX_IRQLOOPS_PER_IRQ;
1945 u16 evstat, events;
David Kilroy21312662009-02-04 23:05:47 +00001946 /* These are used to detect a runaway interrupt situation.
1947 *
1948 * If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
1949 * we panic and shut down the hardware
1950 */
1951 /* jiffies value the last time we were called */
1952 static int last_irq_jiffy; /* = 0 */
1953 static int loops_this_jiffy; /* = 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 unsigned long flags;
1955
1956 if (orinoco_lock(priv, &flags) != 0) {
1957 /* If hw is unavailable - we don't know if the irq was
1958 * for us or not */
1959 return IRQ_HANDLED;
1960 }
1961
1962 evstat = hermes_read_regn(hw, EVSTAT);
1963 events = evstat & hw->inten;
David Kilroya94e8422009-02-04 23:05:44 +00001964 if (!events) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 orinoco_unlock(priv, &flags);
1966 return IRQ_NONE;
1967 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if (jiffies != last_irq_jiffy)
1970 loops_this_jiffy = 0;
1971 last_irq_jiffy = jiffies;
1972
1973 while (events && count--) {
1974 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
1975 printk(KERN_WARNING "%s: IRQ handler is looping too "
1976 "much! Resetting.\n", dev->name);
1977 /* Disable interrupts for now */
1978 hermes_set_irqmask(hw, 0);
1979 schedule_work(&priv->reset_work);
1980 break;
1981 }
1982
1983 /* Check the card hasn't been removed */
David Kilroya94e8422009-02-04 23:05:44 +00001984 if (!hermes_present(hw)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 DEBUG(0, "orinoco_interrupt(): card removed\n");
1986 break;
1987 }
1988
1989 if (events & HERMES_EV_TICK)
1990 __orinoco_ev_tick(dev, hw);
1991 if (events & HERMES_EV_WTERR)
1992 __orinoco_ev_wterr(dev, hw);
1993 if (events & HERMES_EV_INFDROP)
1994 __orinoco_ev_infdrop(dev, hw);
1995 if (events & HERMES_EV_INFO)
1996 __orinoco_ev_info(dev, hw);
1997 if (events & HERMES_EV_RX)
1998 __orinoco_ev_rx(dev, hw);
1999 if (events & HERMES_EV_TXEXC)
2000 __orinoco_ev_txexc(dev, hw);
2001 if (events & HERMES_EV_TX)
2002 __orinoco_ev_tx(dev, hw);
2003 if (events & HERMES_EV_ALLOC)
2004 __orinoco_ev_alloc(dev, hw);
David Kilroy6fe9deb2009-02-04 23:05:43 +00002005
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002006 hermes_write_regn(hw, EVACK, evstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 evstat = hermes_read_regn(hw, EVSTAT);
2009 events = evstat & hw->inten;
2010 };
2011
2012 orinoco_unlock(priv, &flags);
2013 return IRQ_HANDLED;
2014}
David Kilroy21312662009-02-04 23:05:47 +00002015EXPORT_SYMBOL(orinoco_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017/********************************************************************/
David Kilroy39d1ffe2008-11-22 10:37:28 +00002018/* Power management */
2019/********************************************************************/
2020#if defined(CONFIG_PM_SLEEP) && !defined(CONFIG_HERMES_CACHE_FW_ON_INIT)
2021static int orinoco_pm_notifier(struct notifier_block *notifier,
2022 unsigned long pm_event,
2023 void *unused)
2024{
2025 struct orinoco_private *priv = container_of(notifier,
2026 struct orinoco_private,
2027 pm_notifier);
2028
2029 /* All we need to do is cache the firmware before suspend, and
2030 * release it when we come out.
2031 *
2032 * Only need to do this if we're downloading firmware. */
2033 if (!priv->do_fw_download)
2034 return NOTIFY_DONE;
2035
2036 switch (pm_event) {
2037 case PM_HIBERNATION_PREPARE:
2038 case PM_SUSPEND_PREPARE:
2039 orinoco_cache_fw(priv, 0);
2040 break;
2041
2042 case PM_POST_RESTORE:
2043 /* Restore from hibernation failed. We need to clean
2044 * up in exactly the same way, so fall through. */
2045 case PM_POST_HIBERNATION:
2046 case PM_POST_SUSPEND:
2047 orinoco_uncache_fw(priv);
2048 break;
2049
2050 case PM_RESTORE_PREPARE:
2051 default:
2052 break;
2053 }
2054
2055 return NOTIFY_DONE;
2056}
David S. Millerf11c1792009-02-25 00:02:05 -08002057
2058static void orinoco_register_pm_notifier(struct orinoco_private *priv)
2059{
2060 priv->pm_notifier.notifier_call = orinoco_pm_notifier;
2061 register_pm_notifier(&priv->pm_notifier);
2062}
2063
2064static void orinoco_unregister_pm_notifier(struct orinoco_private *priv)
2065{
2066 unregister_pm_notifier(&priv->pm_notifier);
2067}
David Kilroy39d1ffe2008-11-22 10:37:28 +00002068#else /* !PM_SLEEP || HERMES_CACHE_FW_ON_INIT */
David S. Millerf11c1792009-02-25 00:02:05 -08002069#define orinoco_register_pm_notifier(priv) do { } while(0)
2070#define orinoco_unregister_pm_notifier(priv) do { } while(0)
David Kilroy39d1ffe2008-11-22 10:37:28 +00002071#endif
2072
2073/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074/* Initialization */
2075/********************************************************************/
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077static int orinoco_init(struct net_device *dev)
2078{
2079 struct orinoco_private *priv = netdev_priv(dev);
2080 hermes_t *hw = &priv->hw;
2081 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 /* No need to lock, the hw_unavailable flag is already set in
2084 * alloc_orinocodev() */
Johannes Berg2c7060022008-10-30 22:09:54 +01002085 priv->nicbuf_size = IEEE80211_MAX_FRAME_LEN + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 /* Initialize the firmware */
Pavel Roskin37a6c612006-04-07 04:10:49 -04002088 err = hermes_init(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 if (err != 0) {
2090 printk(KERN_ERR "%s: failed to initialize firmware (err = %d)\n",
2091 dev->name, err);
2092 goto out;
2093 }
2094
David Kilroya2d1a422009-06-18 23:21:18 +01002095 err = determine_fw_capabilities(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (err != 0) {
2097 printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
2098 dev->name);
2099 goto out;
2100 }
2101
David Kilroy3994d502008-08-21 23:27:54 +01002102 if (priv->do_fw_download) {
David Kilroy39d1ffe2008-11-22 10:37:28 +00002103#ifdef CONFIG_HERMES_CACHE_FW_ON_INIT
David Kilroy74734312008-11-22 10:37:25 +00002104 orinoco_cache_fw(priv, 0);
David Kilroy39d1ffe2008-11-22 10:37:28 +00002105#endif
David Kilroy74734312008-11-22 10:37:25 +00002106
David Kilroy3994d502008-08-21 23:27:54 +01002107 err = orinoco_download(priv);
2108 if (err)
2109 priv->do_fw_download = 0;
2110
2111 /* Check firmware version again */
David Kilroya2d1a422009-06-18 23:21:18 +01002112 err = determine_fw_capabilities(priv);
David Kilroy3994d502008-08-21 23:27:54 +01002113 if (err != 0) {
2114 printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
2115 dev->name);
2116 goto out;
2117 }
2118 }
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 if (priv->has_port3)
David Kilroyb2f30a02009-02-04 23:05:46 +00002121 printk(KERN_DEBUG "%s: Ad-hoc demo mode supported\n",
2122 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if (priv->has_ibss)
2124 printk(KERN_DEBUG "%s: IEEE standard IBSS ad-hoc mode supported\n",
2125 dev->name);
2126 if (priv->has_wep) {
David Kilroy21312662009-02-04 23:05:47 +00002127 printk(KERN_DEBUG "%s: WEP supported, %s-bit key\n", dev->name,
2128 priv->has_big_wep ? "104" : "40");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 }
David Kilroy23edcc42008-08-21 23:28:05 +01002130 if (priv->has_wpa) {
David Kilroyd03032a2008-08-21 23:28:02 +01002131 printk(KERN_DEBUG "%s: WPA-PSK supported\n", dev->name);
David Kilroy23edcc42008-08-21 23:28:05 +01002132 if (orinoco_mic_init(priv)) {
2133 printk(KERN_ERR "%s: Failed to setup MIC crypto "
2134 "algorithm. Disabling WPA support\n", dev->name);
2135 priv->has_wpa = 0;
2136 }
2137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
David Kilroy01632fa2008-08-21 23:27:58 +01002139 /* Now we have the firmware capabilities, allocate appropiate
2140 * sized scan buffers */
2141 if (orinoco_bss_data_allocate(priv))
2142 goto out;
2143 orinoco_bss_data_init(priv);
2144
David Kilroye9e3d012009-06-18 23:21:19 +01002145 err = orinoco_hw_read_card_settings(priv, dev->dev_addr);
2146 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
David Kilroy42a51b92009-06-18 23:21:20 +01002149 err = orinoco_hw_allocate_fid(priv);
Pavel Roskin37a6c612006-04-07 04:10:49 -04002150 if (err) {
2151 printk(KERN_ERR "%s: failed to allocate NIC buffer!\n",
2152 dev->name);
2153 goto out;
2154 }
2155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 /* Set up the default configuration */
2157 priv->iw_mode = IW_MODE_INFRA;
2158 /* By default use IEEE/IBSS ad-hoc mode if we have it */
David Kilroya94e8422009-02-04 23:05:44 +00002159 priv->prefer_port3 = priv->has_port3 && (!priv->has_ibss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 set_port_type(priv);
David Gibsond51d8b12005-05-12 20:03:36 -04002161 priv->channel = 0; /* use firmware default */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163 priv->promiscuous = 0;
David Kilroy4ae6ee22008-08-21 23:27:59 +01002164 priv->encode_alg = IW_ENCODE_ALG_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 priv->tx_key = 0;
David Kilroyd03032a2008-08-21 23:28:02 +01002166 priv->wpa_enabled = 0;
2167 priv->tkip_cm_active = 0;
2168 priv->key_mgmt = 0;
2169 priv->wpa_ie_len = 0;
2170 priv->wpa_ie = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 /* Make the hardware available, as long as it hasn't been
2173 * removed elsewhere (e.g. by PCMCIA hot unplug) */
2174 spin_lock_irq(&priv->lock);
2175 priv->hw_unavailable--;
2176 spin_unlock_irq(&priv->lock);
2177
2178 printk(KERN_DEBUG "%s: ready\n", dev->name);
2179
2180 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 return err;
2182}
2183
Andrey Borzenkov89ea4092009-01-21 20:46:46 +03002184static const struct net_device_ops orinoco_netdev_ops = {
2185 .ndo_init = orinoco_init,
2186 .ndo_open = orinoco_open,
2187 .ndo_stop = orinoco_stop,
2188 .ndo_start_xmit = orinoco_xmit,
2189 .ndo_set_multicast_list = orinoco_set_multicast_list,
2190 .ndo_change_mtu = orinoco_change_mtu,
2191 .ndo_tx_timeout = orinoco_tx_timeout,
2192 .ndo_get_stats = orinoco_get_stats,
2193};
2194
David Kilroy3994d502008-08-21 23:27:54 +01002195struct net_device
2196*alloc_orinocodev(int sizeof_card,
2197 struct device *device,
2198 int (*hard_reset)(struct orinoco_private *),
2199 int (*stop_fw)(struct orinoco_private *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
2201 struct net_device *dev;
2202 struct orinoco_private *priv;
2203
2204 dev = alloc_etherdev(sizeof(struct orinoco_private) + sizeof_card);
Andrey Borzenkove129a942009-01-21 21:55:29 +03002205 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 return NULL;
2207 priv = netdev_priv(dev);
2208 priv->ndev = dev;
2209 if (sizeof_card)
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002210 priv->card = (void *)((unsigned long)priv
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 + sizeof(struct orinoco_private));
2212 else
2213 priv->card = NULL;
David Kilroy3994d502008-08-21 23:27:54 +01002214 priv->dev = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
2216 /* Setup / override net_device fields */
Andrey Borzenkov89ea4092009-01-21 20:46:46 +03002217 dev->netdev_ops = &orinoco_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 dev->watchdog_timeo = HZ; /* 1 second timeout */
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02002219 dev->ethtool_ops = &orinoco_ethtool_ops;
Andrey Borzenkove129a942009-01-21 21:55:29 +03002220 dev->wireless_handlers = &orinoco_handler_def;
Pavel Roskin343c6862005-09-09 18:43:02 -04002221#ifdef WIRELESS_SPY
2222 priv->wireless_data.spy_data = &priv->spy_data;
2223 dev->wireless_data = &priv->wireless_data;
2224#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 /* we use the default eth_mac_addr for setting the MAC addr */
2226
David Kilroy23edcc42008-08-21 23:28:05 +01002227 /* Reserve space in skb for the SNAP header */
2228 dev->hard_header_len += ENCAPS_OVERHEAD;
2229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 /* Set up default callbacks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 priv->hard_reset = hard_reset;
David Kilroy3994d502008-08-21 23:27:54 +01002232 priv->stop_fw = stop_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
2234 spin_lock_init(&priv->lock);
2235 priv->open = 0;
2236 priv->hw_unavailable = 1; /* orinoco_init() must clear this
2237 * before anything else touches the
2238 * hardware */
David Howellsc4028952006-11-22 14:57:56 +00002239 INIT_WORK(&priv->reset_work, orinoco_reset);
2240 INIT_WORK(&priv->join_work, orinoco_join_ap);
2241 INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
David Kilroy31afcef2008-08-21 23:28:04 +01002243 INIT_LIST_HEAD(&priv->rx_list);
2244 tasklet_init(&priv->rx_tasklet, orinoco_rx_isr_tasklet,
2245 (unsigned long) dev);
2246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 netif_carrier_off(dev);
2248 priv->last_linkstatus = 0xffff;
2249
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +03002250#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
David Kilroy2cea7b22008-11-22 10:37:26 +00002251 priv->cached_pri_fw = NULL;
Andrey Borzenkov4fb30782008-10-19 12:06:11 +04002252 priv->cached_fw = NULL;
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +03002253#endif
Andrey Borzenkov4fb30782008-10-19 12:06:11 +04002254
David Kilroy39d1ffe2008-11-22 10:37:28 +00002255 /* Register PM notifiers */
David S. Millerf11c1792009-02-25 00:02:05 -08002256 orinoco_register_pm_notifier(priv);
David Kilroy39d1ffe2008-11-22 10:37:28 +00002257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 return dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259}
David Kilroy21312662009-02-04 23:05:47 +00002260EXPORT_SYMBOL(alloc_orinocodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
2262void free_orinocodev(struct net_device *dev)
2263{
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002264 struct orinoco_private *priv = netdev_priv(dev);
David Kilroy20953ad2009-01-07 00:23:55 +00002265 struct orinoco_rx_data *rx_data, *temp;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002266
David Kilroy20953ad2009-01-07 00:23:55 +00002267 /* If the tasklet is scheduled when we call tasklet_kill it
2268 * will run one final time. However the tasklet will only
2269 * drain priv->rx_list if the hw is still available. */
David Kilroy31afcef2008-08-21 23:28:04 +01002270 tasklet_kill(&priv->rx_tasklet);
David Kilroy74734312008-11-22 10:37:25 +00002271
David Kilroy20953ad2009-01-07 00:23:55 +00002272 /* Explicitly drain priv->rx_list */
2273 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
2274 list_del(&rx_data->list);
2275
2276 dev_kfree_skb(rx_data->skb);
2277 kfree(rx_data->desc);
2278 kfree(rx_data);
2279 }
2280
David S. Millerf11c1792009-02-25 00:02:05 -08002281 orinoco_unregister_pm_notifier(priv);
David Kilroy74734312008-11-22 10:37:25 +00002282 orinoco_uncache_fw(priv);
2283
David Kilroyd03032a2008-08-21 23:28:02 +01002284 priv->wpa_ie_len = 0;
2285 kfree(priv->wpa_ie);
David Kilroy23edcc42008-08-21 23:28:05 +01002286 orinoco_mic_free(priv);
Dan Williams1e3428e2007-10-10 23:56:25 -04002287 orinoco_bss_data_free(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 free_netdev(dev);
2289}
David Kilroy21312662009-02-04 23:05:47 +00002290EXPORT_SYMBOL(free_orinocodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02002292static void orinoco_get_drvinfo(struct net_device *dev,
2293 struct ethtool_drvinfo *info)
2294{
2295 struct orinoco_private *priv = netdev_priv(dev);
2296
2297 strncpy(info->driver, DRIVER_NAME, sizeof(info->driver) - 1);
2298 strncpy(info->version, DRIVER_VERSION, sizeof(info->version) - 1);
2299 strncpy(info->fw_version, priv->fw_name, sizeof(info->fw_version) - 1);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07002300 if (dev->dev.parent)
Kay Sieversfb28ad32008-11-10 13:55:14 -08002301 strncpy(info->bus_info, dev_name(dev->dev.parent),
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02002302 sizeof(info->bus_info) - 1);
2303 else
2304 snprintf(info->bus_info, sizeof(info->bus_info) - 1,
2305 "PCMCIA %p", priv->hw.iobase);
2306}
2307
Jeff Garzik7282d492006-09-13 14:30:00 -04002308static const struct ethtool_ops orinoco_ethtool_ops = {
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02002309 .get_drvinfo = orinoco_get_drvinfo,
2310 .get_link = ethtool_op_get_link,
2311};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314/* Module initialization */
2315/********************************************************************/
2316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317/* Can't be declared "const" or the whole __initdata section will
2318 * become const */
2319static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
2320 " (David Gibson <hermes@gibson.dropbear.id.au>, "
2321 "Pavel Roskin <proski@gnu.org>, et al)";
2322
2323static int __init init_orinoco(void)
2324{
2325 printk(KERN_DEBUG "%s\n", version);
2326 return 0;
2327}
2328
2329static void __exit exit_orinoco(void)
2330{
2331}
2332
2333module_init(init_orinoco);
2334module_exit(exit_orinoco);