]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/wireless/orinoco/main.c
orinoco: add function to retrieve current bssid
[net-next-2.6.git] / drivers / net / wireless / orinoco / main.c
CommitLineData
47445cb9 1/* main.c - (formerly known as dldwd_cs.c, orinoco_cs.c and orinoco.c)
1da177e4
LT
2 *
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/*
1da177e4 49 * TODO
1da177e4
LT
50 * o Handle de-encapsulation within network layer, provide 802.11
51 * headers (patch from Thomas 'Dent' Mirlacher)
1da177e4
LT
52 * 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
1da177e4
LT
79#include <linux/module.h>
80#include <linux/kernel.h>
81#include <linux/init.h>
d03032af 82#include <linux/delay.h>
8e638267 83#include <linux/device.h>
1da177e4 84#include <linux/netdevice.h>
1da177e4 85#include <linux/etherdevice.h>
1fab2e8b 86#include <linux/ethtool.h>
39d1ffee 87#include <linux/suspend.h>
9c974fb1 88#include <linux/if_arp.h>
1da177e4 89#include <linux/wireless.h>
2c706002 90#include <linux/ieee80211.h>
620554e4 91#include <net/iw_handler.h>
ea60a6aa 92#include <net/cfg80211.h>
1da177e4 93
1da177e4 94#include "hermes_rid.h"
3994d502 95#include "hermes_dld.h"
712a4342 96#include "hw.h"
fb791b1c 97#include "scan.h"
4adb474b 98#include "mic.h"
37a2e566 99#include "fw.h"
cb1576a8 100#include "wext.h"
ea60a6aa 101#include "cfg.h"
cb1576a8 102#include "main.h"
fb791b1c 103
1da177e4 104#include "orinoco.h"
1da177e4
LT
105
106/********************************************************************/
107/* Module information */
108/********************************************************************/
109
b2f30a0a
DK
110MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & "
111 "David Gibson <hermes@gibson.dropbear.id.au>");
112MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based "
113 "and similar wireless cards");
1da177e4
LT
114MODULE_LICENSE("Dual MPL/GPL");
115
116/* Level of debugging. Used in the macros in orinoco.h */
117#ifdef ORINOCO_DEBUG
118int orinoco_debug = ORINOCO_DEBUG;
2131266d 119EXPORT_SYMBOL(orinoco_debug);
1da177e4
LT
120module_param(orinoco_debug, int, 0644);
121MODULE_PARM_DESC(orinoco_debug, "Debug level");
1da177e4
LT
122#endif
123
124static int suppress_linkstatus; /* = 0 */
125module_param(suppress_linkstatus, bool, 0644);
126MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
b2f30a0a 127
7bb7c3a3
DG
128static int ignore_disconnect; /* = 0 */
129module_param(ignore_disconnect, int, 0644);
b2f30a0a
DK
130MODULE_PARM_DESC(ignore_disconnect,
131 "Don't report lost link to the network layer");
1da177e4 132
cb1576a8 133int force_monitor; /* = 0 */
98c4cae1
CH
134module_param(force_monitor, int, 0644);
135MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
136
1da177e4
LT
137/********************************************************************/
138/* Internal constants */
139/********************************************************************/
140
95dd91fb
CH
141/* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
142static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
143#define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
144
1da177e4 145#define ORINOCO_MIN_MTU 256
2c706002 146#define ORINOCO_MAX_MTU (IEEE80211_MAX_DATA_LEN - ENCAPS_OVERHEAD)
1da177e4 147
1da177e4
LT
148#define MAX_IRQLOOPS_PER_IRQ 10
149#define MAX_IRQLOOPS_PER_JIFFY (20000/HZ) /* Based on a guestimate of
150 * how many events the
151 * device could
152 * legitimately generate */
1da177e4
LT
153
154#define DUMMY_FID 0xFFFF
155
156/*#define MAX_MULTICAST(priv) (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
157 HERMES_MAX_MULTICAST : 0)*/
158#define MAX_MULTICAST(priv) (HERMES_MAX_MULTICAST)
159
160#define ORINOCO_INTEN (HERMES_EV_RX | HERMES_EV_ALLOC \
161 | HERMES_EV_TX | HERMES_EV_TXEXC \
162 | HERMES_EV_WTERR | HERMES_EV_INFO \
a94e8427 163 | HERMES_EV_INFDROP)
1da177e4 164
7282d491 165static const struct ethtool_ops orinoco_ethtool_ops;
620554e4 166
1da177e4
LT
167/********************************************************************/
168/* Data types */
169/********************************************************************/
170
30c2d3b4
PR
171/* Beginning of the Tx descriptor, used in TxExc handling */
172struct hermes_txexc_data {
173 struct hermes_tx_descriptor desc;
d133ae4c
PR
174 __le16 frame_ctl;
175 __le16 duration_id;
95dd91fb 176 u8 addr1[ETH_ALEN];
1da177e4
LT
177} __attribute__ ((packed));
178
8f2abf44 179/* Rx frame header except compatibility 802.3 header */
1da177e4 180struct hermes_rx_descriptor {
8f2abf44 181 /* Control */
d133ae4c
PR
182 __le16 status;
183 __le32 time;
1da177e4
LT
184 u8 silence;
185 u8 signal;
186 u8 rate;
187 u8 rxflow;
d133ae4c 188 __le32 reserved;
8f2abf44
CH
189
190 /* 802.11 header */
d133ae4c
PR
191 __le16 frame_ctl;
192 __le16 duration_id;
8f2abf44
CH
193 u8 addr1[ETH_ALEN];
194 u8 addr2[ETH_ALEN];
195 u8 addr3[ETH_ALEN];
d133ae4c 196 __le16 seq_ctl;
8f2abf44
CH
197 u8 addr4[ETH_ALEN];
198
199 /* Data length */
d133ae4c 200 __le16 data_len;
1da177e4
LT
201} __attribute__ ((packed));
202
47166791
DK
203struct orinoco_rx_data {
204 struct hermes_rx_descriptor *desc;
205 struct sk_buff *skb;
206 struct list_head list;
207};
208
c63cdbe8
DK
209struct orinoco_scan_data {
210 void *buf;
211 size_t len;
212 int type;
213 struct list_head list;
214};
215
1da177e4
LT
216/********************************************************************/
217/* Function prototypes */
218/********************************************************************/
219
721aa2f7 220static int __orinoco_set_multicast_list(struct net_device *dev);
6415f7df
DK
221static int __orinoco_up(struct orinoco_private *priv);
222static int __orinoco_down(struct orinoco_private *priv);
721aa2f7 223static int __orinoco_commit(struct orinoco_private *priv);
1da177e4
LT
224
225/********************************************************************/
226/* Internal helper functions */
227/********************************************************************/
228
cb1576a8 229void set_port_type(struct orinoco_private *priv)
1da177e4
LT
230{
231 switch (priv->iw_mode) {
5217c571 232 case NL80211_IFTYPE_STATION:
1da177e4
LT
233 priv->port_type = 1;
234 priv->createibss = 0;
235 break;
5217c571 236 case NL80211_IFTYPE_ADHOC:
1da177e4
LT
237 if (priv->prefer_port3) {
238 priv->port_type = 3;
239 priv->createibss = 0;
240 } else {
241 priv->port_type = priv->ibss_port;
242 priv->createibss = 1;
243 }
244 break;
5217c571 245 case NL80211_IFTYPE_MONITOR:
98c4cae1
CH
246 priv->port_type = 3;
247 priv->createibss = 0;
248 break;
1da177e4
LT
249 default:
250 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
251 priv->ndev->name);
252 }
253}
254
255/********************************************************************/
256/* Device methods */
257/********************************************************************/
258
259static int orinoco_open(struct net_device *dev)
260{
ea60a6aa 261 struct orinoco_private *priv = ndev_priv(dev);
1da177e4
LT
262 unsigned long flags;
263 int err;
264
265 if (orinoco_lock(priv, &flags) != 0)
266 return -EBUSY;
267
a2608362 268 err = __orinoco_up(priv);
1da177e4 269
a94e8427 270 if (!err)
1da177e4
LT
271 priv->open = 1;
272
273 orinoco_unlock(priv, &flags);
274
275 return err;
276}
277
ad8f451b 278static int orinoco_stop(struct net_device *dev)
1da177e4 279{
ea60a6aa 280 struct orinoco_private *priv = ndev_priv(dev);
1da177e4
LT
281 int err = 0;
282
283 /* We mustn't use orinoco_lock() here, because we need to be
284 able to close the interface even if hw_unavailable is set
285 (e.g. as we're released after a PC Card removal) */
286 spin_lock_irq(&priv->lock);
287
288 priv->open = 0;
289
a2608362 290 err = __orinoco_down(priv);
1da177e4
LT
291
292 spin_unlock_irq(&priv->lock);
293
294 return err;
295}
296
297static struct net_device_stats *orinoco_get_stats(struct net_device *dev)
298{
ea60a6aa 299 struct orinoco_private *priv = ndev_priv(dev);
6fe9deb1 300
1da177e4
LT
301 return &priv->stats;
302}
303
1da177e4
LT
304static void orinoco_set_multicast_list(struct net_device *dev)
305{
ea60a6aa 306 struct orinoco_private *priv = ndev_priv(dev);
1da177e4
LT
307 unsigned long flags;
308
309 if (orinoco_lock(priv, &flags) != 0) {
310 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
311 "called when hw_unavailable\n", dev->name);
312 return;
313 }
314
315 __orinoco_set_multicast_list(dev);
316 orinoco_unlock(priv, &flags);
317}
318
319static int orinoco_change_mtu(struct net_device *dev, int new_mtu)
320{
ea60a6aa 321 struct orinoco_private *priv = ndev_priv(dev);
1da177e4 322
a94e8427 323 if ((new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU))
1da177e4
LT
324 return -EINVAL;
325
2c706002 326 /* MTU + encapsulation + header length */
a94e8427
DK
327 if ((new_mtu + ENCAPS_OVERHEAD + sizeof(struct ieee80211_hdr)) >
328 (priv->nicbuf_size - ETH_HLEN))
1da177e4
LT
329 return -EINVAL;
330
331 dev->mtu = new_mtu;
332
333 return 0;
334}
335
336/********************************************************************/
337/* Tx path */
338/********************************************************************/
339
340static int orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
341{
ea60a6aa 342 struct orinoco_private *priv = ndev_priv(dev);
1da177e4
LT
343 struct net_device_stats *stats = &priv->stats;
344 hermes_t *hw = &priv->hw;
345 int err = 0;
346 u16 txfid = priv->txfid;
1da177e4 347 struct ethhdr *eh;
6eecad77 348 int tx_control;
1da177e4
LT
349 unsigned long flags;
350
a94e8427 351 if (!netif_running(dev)) {
1da177e4
LT
352 printk(KERN_ERR "%s: Tx on stopped device!\n",
353 dev->name);
b34b867e 354 return NETDEV_TX_BUSY;
1da177e4 355 }
6fe9deb1 356
1da177e4 357 if (netif_queue_stopped(dev)) {
6fe9deb1 358 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
1da177e4 359 dev->name);
b34b867e 360 return NETDEV_TX_BUSY;
1da177e4 361 }
6fe9deb1 362
1da177e4
LT
363 if (orinoco_lock(priv, &flags) != 0) {
364 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
365 dev->name);
b34b867e 366 return NETDEV_TX_BUSY;
1da177e4
LT
367 }
368
5217c571
DK
369 if (!netif_carrier_ok(dev) ||
370 (priv->iw_mode == NL80211_IFTYPE_MONITOR)) {
1da177e4 371 /* Oops, the firmware hasn't established a connection,
6fe9deb1
DK
372 silently drop the packet (this seems to be the
373 safest approach). */
470e2aa6 374 goto drop;
1da177e4
LT
375 }
376
8d5be088 377 /* Check packet length */
a28dc81d 378 if (skb->len < ETH_HLEN)
470e2aa6 379 goto drop;
1da177e4 380
6eecad77
DK
381 tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
382
5c9f41e2 383 if (priv->encode_alg == ORINOCO_ALG_TKIP)
23edcc41
DK
384 tx_control |= (priv->tx_key << HERMES_MIC_KEY_ID_SHIFT) |
385 HERMES_TXCTRL_MIC;
386
6eecad77
DK
387 if (priv->has_alt_txcntl) {
388 /* WPA enabled firmwares have tx_cntl at the end of
389 * the 802.11 header. So write zeroed descriptor and
390 * 802.11 header at the same time
391 */
392 char desc[HERMES_802_3_OFFSET];
393 __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
394
395 memset(&desc, 0, sizeof(desc));
396
397 *txcntl = 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 } else {
407 struct hermes_tx_descriptor desc;
408
409 memset(&desc, 0, sizeof(desc));
410
411 desc.tx_control = cpu_to_le16(tx_control);
412 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
413 txfid, 0);
414 if (err) {
415 if (net_ratelimit())
416 printk(KERN_ERR "%s: Error %d writing Tx "
417 "descriptor to BAP\n", dev->name, err);
418 goto busy;
419 }
1da177e4 420
6eecad77
DK
421 /* Clear the 802.11 header and data length fields - some
422 * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
423 * if this isn't done. */
424 hermes_clear_words(hw, HERMES_DATA0,
425 HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
426 }
1da177e4 427
23edcc41
DK
428 eh = (struct ethhdr *)skb->data;
429
1da177e4
LT
430 /* Encapsulate Ethernet-II frames */
431 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
a28dc81d
PR
432 struct header_struct {
433 struct ethhdr eth; /* 802.3 header */
434 u8 encap[6]; /* 802.2 header */
435 } __attribute__ ((packed)) hdr;
436
437 /* Strip destination and source from the data */
438 skb_pull(skb, 2 * ETH_ALEN);
a28dc81d
PR
439
440 /* And move them to a separate header */
441 memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
442 hdr.eth.h_proto = htons(sizeof(encaps_hdr) + skb->len);
443 memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
444
23edcc41
DK
445 /* Insert the SNAP header */
446 if (skb_headroom(skb) < sizeof(hdr)) {
447 printk(KERN_ERR
448 "%s: Not enough headroom for 802.2 headers %d\n",
449 dev->name, skb_headroom(skb));
450 goto drop;
1da177e4 451 }
23edcc41
DK
452 eh = (struct ethhdr *) skb_push(skb, sizeof(hdr));
453 memcpy(eh, &hdr, sizeof(hdr));
1da177e4
LT
454 }
455
a28dc81d 456 err = hermes_bap_pwrite(hw, USER_BAP, skb->data, skb->len,
23edcc41 457 txfid, HERMES_802_3_OFFSET);
1da177e4
LT
458 if (err) {
459 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
460 dev->name, err);
470e2aa6 461 goto busy;
1da177e4
LT
462 }
463
23edcc41 464 /* Calculate Michael MIC */
5c9f41e2 465 if (priv->encode_alg == ORINOCO_ALG_TKIP) {
23edcc41
DK
466 u8 mic_buf[MICHAEL_MIC_LEN + 1];
467 u8 *mic;
468 size_t offset;
469 size_t len;
470
471 if (skb->len % 2) {
472 /* MIC start is on an odd boundary */
473 mic_buf[0] = skb->data[skb->len - 1];
474 mic = &mic_buf[1];
475 offset = skb->len - 1;
476 len = MICHAEL_MIC_LEN + 1;
477 } else {
478 mic = &mic_buf[0];
479 offset = skb->len;
480 len = MICHAEL_MIC_LEN;
481 }
482
4adb474b 483 orinoco_mic(priv->tx_tfm_mic,
23edcc41
DK
484 priv->tkip_key[priv->tx_key].tx_mic,
485 eh->h_dest, eh->h_source, 0 /* priority */,
486 skb->data + ETH_HLEN, skb->len - ETH_HLEN, mic);
487
488 /* Write the MIC */
489 err = hermes_bap_pwrite(hw, USER_BAP, &mic_buf[0], len,
490 txfid, HERMES_802_3_OFFSET + offset);
491 if (err) {
492 printk(KERN_ERR "%s: Error %d writing MIC to BAP\n",
493 dev->name, err);
494 goto busy;
495 }
496 }
497
1da177e4
LT
498 /* Finally, we actually initiate the send */
499 netif_stop_queue(dev);
500
501 err = hermes_docmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
502 txfid, NULL);
503 if (err) {
504 netif_start_queue(dev);
c367c21c
AM
505 if (net_ratelimit())
506 printk(KERN_ERR "%s: Error %d transmitting packet\n",
507 dev->name, err);
470e2aa6 508 goto busy;
1da177e4
LT
509 }
510
511 dev->trans_start = jiffies;
23edcc41 512 stats->tx_bytes += HERMES_802_3_OFFSET + skb->len;
470e2aa6 513 goto ok;
1da177e4 514
470e2aa6
PR
515 drop:
516 stats->tx_errors++;
517 stats->tx_dropped++;
1da177e4 518
470e2aa6
PR
519 ok:
520 orinoco_unlock(priv, &flags);
1da177e4 521 dev_kfree_skb(skb);
b34b867e 522 return NETDEV_TX_OK;
1da177e4 523
470e2aa6 524 busy:
2c1bd260
JB
525 if (err == -EIO)
526 schedule_work(&priv->reset_work);
1da177e4 527 orinoco_unlock(priv, &flags);
b34b867e 528 return NETDEV_TX_BUSY;
1da177e4
LT
529}
530
531static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
532{
ea60a6aa 533 struct orinoco_private *priv = ndev_priv(dev);
1da177e4
LT
534 u16 fid = hermes_read_regn(hw, ALLOCFID);
535
536 if (fid != priv->txfid) {
537 if (fid != DUMMY_FID)
538 printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
539 dev->name, fid);
540 return;
541 }
542
543 hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
544}
545
546static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
547{
ea60a6aa 548 struct orinoco_private *priv = ndev_priv(dev);
1da177e4
LT
549 struct net_device_stats *stats = &priv->stats;
550
551 stats->tx_packets++;
552
553 netif_wake_queue(dev);
554
555 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
556}
557
558static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
559{
ea60a6aa 560 struct orinoco_private *priv = ndev_priv(dev);
1da177e4
LT
561 struct net_device_stats *stats = &priv->stats;
562 u16 fid = hermes_read_regn(hw, TXCOMPLFID);
d133ae4c 563 u16 status;
30c2d3b4 564 struct hermes_txexc_data hdr;
1da177e4
LT
565 int err = 0;
566
567 if (fid == DUMMY_FID)
568 return; /* Nothing's really happened */
569
48ca7038 570 /* Read part of the frame header - we need status and addr1 */
95dd91fb 571 err = hermes_bap_pread(hw, IRQ_BAP, &hdr,
30c2d3b4 572 sizeof(struct hermes_txexc_data),
95dd91fb
CH
573 fid, 0);
574
575 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
576 stats->tx_errors++;
577
1da177e4
LT
578 if (err) {
579 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
580 "(FID=%04X error %d)\n",
581 dev->name, fid, err);
95dd91fb 582 return;
1da177e4 583 }
6fe9deb1 584
95dd91fb
CH
585 DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
586 err, fid);
6fe9deb1 587
95dd91fb
CH
588 /* We produce a TXDROP event only for retry or lifetime
589 * exceeded, because that's the only status that really mean
590 * that this particular node went away.
591 * Other errors means that *we* screwed up. - Jean II */
30c2d3b4 592 status = le16_to_cpu(hdr.desc.status);
d133ae4c 593 if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
95dd91fb
CH
594 union iwreq_data wrqu;
595
596 /* Copy 802.11 dest address.
597 * We use the 802.11 header because the frame may
598 * not be 802.3 or may be mangled...
599 * In Ad-Hoc mode, it will be the node address.
600 * In managed mode, it will be most likely the AP addr
601 * User space will figure out how to convert it to
602 * whatever it needs (IP address or else).
603 * - Jean II */
604 memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
605 wrqu.addr.sa_family = ARPHRD_ETHER;
606
607 /* Send event to user space */
608 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
609 }
1da177e4
LT
610
611 netif_wake_queue(dev);
1da177e4
LT
612}
613
614static void orinoco_tx_timeout(struct net_device *dev)
615{
ea60a6aa 616 struct orinoco_private *priv = ndev_priv(dev);
1da177e4
LT
617 struct net_device_stats *stats = &priv->stats;
618 struct hermes *hw = &priv->hw;
619
620 printk(KERN_WARNING "%s: Tx timeout! "
621 "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
622 dev->name, hermes_read_regn(hw, ALLOCFID),
623 hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
624
625 stats->tx_errors++;
626
627 schedule_work(&priv->reset_work);
628}
629
630/********************************************************************/
631/* Rx path (data frames) */
632/********************************************************************/
633
634/* Does the frame have a SNAP header indicating it should be
635 * de-encapsulated to Ethernet-II? */
636static inline int is_ethersnap(void *_hdr)
637{
638 u8 *hdr = _hdr;
639
640 /* We de-encapsulate all packets which, a) have SNAP headers
641 * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
642 * and where b) the OUI of the SNAP header is 00:00:00 or
643 * 00:00:f8 - we need both because different APs appear to use
644 * different OUIs for some reason */
645 return (memcmp(hdr, &encaps_hdr, 5) == 0)
a94e8427 646 && ((hdr[5] == 0x00) || (hdr[5] == 0xf8));
1da177e4
LT
647}
648
649static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
650 int level, int noise)
651{
343c686c
PR
652 struct iw_quality wstats;
653 wstats.level = level - 0x95;
654 wstats.noise = noise - 0x95;
655 wstats.qual = (level > noise) ? (level - noise) : 0;
f941f859 656 wstats.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
343c686c
PR
657 /* Update spy records */
658 wireless_spy_update(dev, mac, &wstats);
1da177e4
LT
659}
660
661static void orinoco_stat_gather(struct net_device *dev,
662 struct sk_buff *skb,
663 struct hermes_rx_descriptor *desc)
664{
ea60a6aa 665 struct orinoco_private *priv = ndev_priv(dev);
1da177e4
LT
666
667 /* Using spy support with lots of Rx packets, like in an
668 * infrastructure (AP), will really slow down everything, because
669 * the MAC address must be compared to each entry of the spy list.
670 * If the user really asks for it (set some address in the
671 * spy list), we do it, but he will pay the price.
672 * Note that to get here, you need both WIRELESS_SPY
673 * compiled in AND some addresses in the list !!!
674 */
675 /* Note : gcc will optimise the whole section away if
676 * WIRELESS_SPY is not defined... - Jean II */
677 if (SPY_NUMBER(priv)) {
98e399f8 678 orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
1da177e4
LT
679 desc->signal, desc->silence);
680 }
681}
682
98c4cae1
CH
683/*
684 * orinoco_rx_monitor - handle received monitor frames.
685 *
686 * Arguments:
687 * dev network device
688 * rxfid received FID
689 * desc rx descriptor of the frame
690 *
691 * Call context: interrupt
692 */
693static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
694 struct hermes_rx_descriptor *desc)
695{
696 u32 hdrlen = 30; /* return full header by default */
697 u32 datalen = 0;
698 u16 fc;
699 int err;
700 int len;
701 struct sk_buff *skb;
ea60a6aa 702 struct orinoco_private *priv = ndev_priv(dev);
98c4cae1
CH
703 struct net_device_stats *stats = &priv->stats;
704 hermes_t *hw = &priv->hw;
705
706 len = le16_to_cpu(desc->data_len);
707
708 /* Determine the size of the header and the data */
709 fc = le16_to_cpu(desc->frame_ctl);
710 switch (fc & IEEE80211_FCTL_FTYPE) {
711 case IEEE80211_FTYPE_DATA:
712 if ((fc & IEEE80211_FCTL_TODS)
713 && (fc & IEEE80211_FCTL_FROMDS))
714 hdrlen = 30;
715 else
716 hdrlen = 24;
717 datalen = len;
718 break;
719 case IEEE80211_FTYPE_MGMT:
720 hdrlen = 24;
721 datalen = len;
722 break;
723 case IEEE80211_FTYPE_CTL:
724 switch (fc & IEEE80211_FCTL_STYPE) {
725 case IEEE80211_STYPE_PSPOLL:
726 case IEEE80211_STYPE_RTS:
727 case IEEE80211_STYPE_CFEND:
728 case IEEE80211_STYPE_CFENDACK:
729 hdrlen = 16;
730 break;
731 case IEEE80211_STYPE_CTS:
732 case IEEE80211_STYPE_ACK:
733 hdrlen = 10;
734 break;
735 }
736 break;
737 default:
738 /* Unknown frame type */
739 break;
740 }
741
742 /* sanity check the length */
2c706002 743 if (datalen > IEEE80211_MAX_DATA_LEN + 12) {
98c4cae1
CH
744 printk(KERN_DEBUG "%s: oversized monitor frame, "
745 "data length = %d\n", dev->name, datalen);
98c4cae1
CH
746 stats->rx_length_errors++;
747 goto update_stats;
748 }
749
750 skb = dev_alloc_skb(hdrlen + datalen);
751 if (!skb) {
752 printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
753 dev->name);
bb6e093d 754 goto update_stats;
98c4cae1
CH
755 }
756
757 /* Copy the 802.11 header to the skb */
758 memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
459a98ed 759 skb_reset_mac_header(skb);
98c4cae1
CH
760
761 /* If any, copy the data from the card to the skb */
762 if (datalen > 0) {
763 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
764 ALIGN(datalen, 2), rxfid,
765 HERMES_802_2_OFFSET);
766 if (err) {
767 printk(KERN_ERR "%s: error %d reading monitor frame\n",
768 dev->name, err);
769 goto drop;
770 }
771 }
772
773 skb->dev = dev;
774 skb->ip_summed = CHECKSUM_NONE;
775 skb->pkt_type = PACKET_OTHERHOST;
c1b4aa3f 776 skb->protocol = cpu_to_be16(ETH_P_802_2);
6fe9deb1 777
98c4cae1
CH
778 stats->rx_packets++;
779 stats->rx_bytes += skb->len;
780
781 netif_rx(skb);
782 return;
783
784 drop:
785 dev_kfree_skb_irq(skb);
786 update_stats:
787 stats->rx_errors++;
788 stats->rx_dropped++;
789}
790
1da177e4
LT
791static void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
792{
ea60a6aa 793 struct orinoco_private *priv = ndev_priv(dev);
1da177e4
LT
794 struct net_device_stats *stats = &priv->stats;
795 struct iw_statistics *wstats = &priv->wstats;
796 struct sk_buff *skb = NULL;
31afcef3 797 u16 rxfid, status;
8f2abf44 798 int length;
31afcef3
DK
799 struct hermes_rx_descriptor *desc;
800 struct orinoco_rx_data *rx_data;
1da177e4
LT
801 int err;
802
31afcef3
DK
803 desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
804 if (!desc) {
805 printk(KERN_WARNING
806 "%s: Can't allocate space for RX descriptor\n",
807 dev->name);
808 goto update_stats;
809 }
810
1da177e4
LT
811 rxfid = hermes_read_regn(hw, RXFID);
812
31afcef3 813 err = hermes_bap_pread(hw, IRQ_BAP, desc, sizeof(*desc),
1da177e4
LT
814 rxfid, 0);
815 if (err) {
816 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
817 "Frame dropped.\n", dev->name, err);
98c4cae1 818 goto update_stats;
1da177e4
LT
819 }
820
31afcef3 821 status = le16_to_cpu(desc->status);
1da177e4 822
98c4cae1
CH
823 if (status & HERMES_RXSTAT_BADCRC) {
824 DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
825 dev->name);
826 stats->rx_crc_errors++;
827 goto update_stats;
1da177e4
LT
828 }
829
98c4cae1 830 /* Handle frames in monitor mode */
5217c571 831 if (priv->iw_mode == NL80211_IFTYPE_MONITOR) {
31afcef3
DK
832 orinoco_rx_monitor(dev, rxfid, desc);
833 goto out;
98c4cae1 834 }
1da177e4 835
98c4cae1
CH
836 if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
837 DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
838 dev->name);
839 wstats->discard.code++;
840 goto update_stats;
1da177e4
LT
841 }
842
31afcef3 843 length = le16_to_cpu(desc->data_len);
8f2abf44 844
1da177e4
LT
845 /* Sanity checks */
846 if (length < 3) { /* No for even an 802.2 LLC header */
847 /* At least on Symbol firmware with PCF we get quite a
6fe9deb1
DK
848 lot of these legitimately - Poll frames with no
849 data. */
31afcef3 850 goto out;
1da177e4 851 }
2c706002 852 if (length > IEEE80211_MAX_DATA_LEN) {
1da177e4
LT
853 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
854 dev->name, length);
855 stats->rx_length_errors++;
98c4cae1 856 goto update_stats;
1da177e4
LT
857 }
858
23edcc41
DK
859 /* Payload size does not include Michael MIC. Increase payload
860 * size to read it together with the data. */
861 if (status & HERMES_RXSTAT_MIC)
862 length += MICHAEL_MIC_LEN;
863
1da177e4
LT
864 /* We need space for the packet data itself, plus an ethernet
865 header, plus 2 bytes so we can align the IP header on a
866 32bit boundary, plus 1 byte so we can read in odd length
867 packets from the card, which has an IO granularity of 16
6fe9deb1 868 bits */
1da177e4
LT
869 skb = dev_alloc_skb(length+ETH_HLEN+2+1);
870 if (!skb) {
871 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
872 dev->name);
98c4cae1 873 goto update_stats;
1da177e4
LT
874 }
875
8f2abf44
CH
876 /* We'll prepend the header, so reserve space for it. The worst
877 case is no decapsulation, when 802.3 header is prepended and
878 nothing is removed. 2 is for aligning the IP header. */
879 skb_reserve(skb, ETH_HLEN + 2);
880
881 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, length),
882 ALIGN(length, 2), rxfid,
883 HERMES_802_2_OFFSET);
884 if (err) {
885 printk(KERN_ERR "%s: error %d reading frame. "
886 "Frame dropped.\n", dev->name, err);
8f2abf44
CH
887 goto drop;
888 }
1da177e4 889
31afcef3
DK
890 /* Add desc and skb to rx queue */
891 rx_data = kzalloc(sizeof(*rx_data), GFP_ATOMIC);
892 if (!rx_data) {
893 printk(KERN_WARNING "%s: Can't allocate RX packet\n",
894 dev->name);
895 goto drop;
896 }
897 rx_data->desc = desc;
898 rx_data->skb = skb;
899 list_add_tail(&rx_data->list, &priv->rx_list);
900 tasklet_schedule(&priv->rx_tasklet);
901
902 return;
903
904drop:
905 dev_kfree_skb_irq(skb);
906update_stats:
907 stats->rx_errors++;
908 stats->rx_dropped++;
909out:
910 kfree(desc);
911}
912
913static void orinoco_rx(struct net_device *dev,
914 struct hermes_rx_descriptor *desc,
915 struct sk_buff *skb)
916{
ea60a6aa 917 struct orinoco_private *priv = ndev_priv(dev);
31afcef3
DK
918 struct net_device_stats *stats = &priv->stats;
919 u16 status, fc;
920 int length;
921 struct ethhdr *hdr;
922
923 status = le16_to_cpu(desc->status);
924 length = le16_to_cpu(desc->data_len);
925 fc = le16_to_cpu(desc->frame_ctl);
926
23edcc41
DK
927 /* Calculate and check MIC */
928 if (status & HERMES_RXSTAT_MIC) {
929 int key_id = ((status & HERMES_RXSTAT_MIC_KEY_ID) >>
930 HERMES_MIC_KEY_ID_SHIFT);
931 u8 mic[MICHAEL_MIC_LEN];
932 u8 *rxmic;
933 u8 *src = (fc & IEEE80211_FCTL_FROMDS) ?
934 desc->addr3 : desc->addr2;
935
936 /* Extract Michael MIC from payload */
937 rxmic = skb->data + skb->len - MICHAEL_MIC_LEN;
938
939 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
940 length -= MICHAEL_MIC_LEN;
941
4adb474b 942 orinoco_mic(priv->rx_tfm_mic,
23edcc41
DK
943 priv->tkip_key[key_id].rx_mic,
944 desc->addr1,
945 src,
946 0, /* priority or QoS? */
947 skb->data,
948 skb->len,
949 &mic[0]);
950
951 if (memcmp(mic, rxmic,
952 MICHAEL_MIC_LEN)) {
953 union iwreq_data wrqu;
954 struct iw_michaelmicfailure wxmic;
23edcc41
DK
955
956 printk(KERN_WARNING "%s: "
e174961c 957 "Invalid Michael MIC in data frame from %pM, "
23edcc41 958 "using key %i\n",
e174961c 959 dev->name, src, key_id);
23edcc41
DK
960
961 /* TODO: update stats */
962
963 /* Notify userspace */
964 memset(&wxmic, 0, sizeof(wxmic));
965 wxmic.flags = key_id & IW_MICFAILURE_KEY_ID;
966 wxmic.flags |= (desc->addr1[0] & 1) ?
967 IW_MICFAILURE_GROUP : IW_MICFAILURE_PAIRWISE;
968 wxmic.src_addr.sa_family = ARPHRD_ETHER;
969 memcpy(wxmic.src_addr.sa_data, src, ETH_ALEN);
970
971 (void) orinoco_hw_get_tkip_iv(priv, key_id,
972 &wxmic.tsc[0]);
973
974 memset(&wrqu, 0, sizeof(wrqu));
975 wrqu.data.length = sizeof(wxmic);
976 wireless_send_event(dev, IWEVMICHAELMICFAILURE, &wrqu,
977 (char *) &wxmic);
978
979 goto drop;
980 }
981 }
982
1da177e4
LT
983 /* Handle decapsulation
984 * In most cases, the firmware tell us about SNAP frames.
985 * For some reason, the SNAP frames sent by LinkSys APs
986 * are not properly recognised by most firmwares.
987 * So, check ourselves */
8f2abf44
CH
988 if (length >= ENCAPS_OVERHEAD &&
989 (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
990 ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
991 is_ethersnap(skb->data))) {
1da177e4
LT
992 /* These indicate a SNAP within 802.2 LLC within
993 802.11 frame which we'll need to de-encapsulate to
994 the original EthernetII frame. */
b2f30a0a
DK
995 hdr = (struct ethhdr *)skb_push(skb,
996 ETH_HLEN - ENCAPS_OVERHEAD);
1da177e4 997 } else {
8f2abf44
CH
998 /* 802.3 frame - prepend 802.3 header as is */
999 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
1000 hdr->h_proto = htons(length);
1da177e4 1001 }
31afcef3 1002 memcpy(hdr->h_dest, desc->addr1, ETH_ALEN);
8f2abf44 1003 if (fc & IEEE80211_FCTL_FROMDS)
31afcef3 1004 memcpy(hdr->h_source, desc->addr3, ETH_ALEN);
8f2abf44 1005 else
31afcef3 1006 memcpy(hdr->h_source, desc->addr2, ETH_ALEN);
1da177e4 1007
1da177e4
LT
1008 skb->protocol = eth_type_trans(skb, dev);
1009 skb->ip_summed = CHECKSUM_NONE;
8f2abf44
CH
1010 if (fc & IEEE80211_FCTL_TODS)
1011 skb->pkt_type = PACKET_OTHERHOST;
6fe9deb1 1012
1da177e4 1013 /* Process the wireless stats if needed */
31afcef3 1014 orinoco_stat_gather(dev, skb, desc);
1da177e4
LT
1015
1016 /* Pass the packet to the networking stack */
1017 netif_rx(skb);
1018 stats->rx_packets++;
1019 stats->rx_bytes += length;
1020
1021 return;
23edcc41
DK
1022
1023 drop:
1024 dev_kfree_skb(skb);
1025 stats->rx_errors++;
1026 stats->rx_dropped++;
31afcef3 1027}
1da177e4 1028
31afcef3
DK
1029static void orinoco_rx_isr_tasklet(unsigned long data)
1030{
5381956b
DK
1031 struct orinoco_private *priv = (struct orinoco_private *) data;
1032 struct net_device *dev = priv->ndev;
31afcef3
DK
1033 struct orinoco_rx_data *rx_data, *temp;
1034 struct hermes_rx_descriptor *desc;
1035 struct sk_buff *skb;
20953ad6
DK
1036 unsigned long flags;
1037
1038 /* orinoco_rx requires the driver lock, and we also need to
1039 * protect priv->rx_list, so just hold the lock over the
1040 * lot.
1041 *
1042 * If orinoco_lock fails, we've unplugged the card. In this
1043 * case just abort. */
1044 if (orinoco_lock(priv, &flags) != 0)
1045 return;
31afcef3
DK
1046
1047 /* extract desc and skb from queue */
1048 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
1049 desc = rx_data->desc;
1050 skb = rx_data->skb;
1051 list_del(&rx_data->list);
1052 kfree(rx_data);
1053
1054 orinoco_rx(dev, desc, skb);
1055
1056 kfree(desc);
1057 }
20953ad6
DK
1058
1059 orinoco_unlock(priv, &flags);
1da177e4
LT
1060}
1061
1062/********************************************************************/
1063/* Rx path (info frames) */
1064/********************************************************************/
1065
1066static void print_linkstatus(struct net_device *dev, u16 status)
1067{
2131266d 1068 char *s;
1da177e4
LT
1069
1070 if (suppress_linkstatus)
1071 return;
1072
1073 switch (status) {
1074 case HERMES_LINKSTATUS_NOT_CONNECTED:
1075 s = "Not Connected";
1076 break;
1077 case HERMES_LINKSTATUS_CONNECTED:
1078 s = "Connected";
1079 break;
1080 case HERMES_LINKSTATUS_DISCONNECTED:
1081 s = "Disconnected";
1082 break;
1083 case HERMES_LINKSTATUS_AP_CHANGE:
1084 s = "AP Changed";
1085 break;
1086 case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1087 s = "AP Out of Range";
1088 break;
1089 case HERMES_LINKSTATUS_AP_IN_RANGE:
1090 s = "AP In Range";
1091 break;
1092 case HERMES_LINKSTATUS_ASSOC_FAILED:
1093 s = "Association Failed";
1094 break;
1095 default:
1096 s = "UNKNOWN";
1097 }
6fe9deb1 1098
11eaea41 1099 printk(KERN_DEBUG "%s: New link status: %s (%04x)\n",
1da177e4
LT
1100 dev->name, s, status);
1101}
1102
16739b06 1103/* Search scan results for requested BSSID, join it if found */
c4028958 1104static void orinoco_join_ap(struct work_struct *work)
16739b06 1105{
c4028958
DH
1106 struct orinoco_private *priv =
1107 container_of(work, struct orinoco_private, join_work);
1108 struct net_device *dev = priv->ndev;
16739b06
CH
1109 struct hermes *hw = &priv->hw;
1110 int err;
1111 unsigned long flags;
1112 struct join_req {
1113 u8 bssid[ETH_ALEN];
d133ae4c 1114 __le16 channel;
16739b06
CH
1115 } __attribute__ ((packed)) req;
1116 const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
c89cc225 1117 struct prism2_scan_apinfo *atom = NULL;
16739b06 1118 int offset = 4;
c89cc225 1119 int found = 0;
16739b06
CH
1120 u8 *buf;
1121 u16 len;
1122
1123 /* Allocate buffer for scan results */
1124 buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
a94e8427 1125 if (!buf)
16739b06
CH
1126 return;
1127
1128 if (orinoco_lock(priv, &flags) != 0)
f3cb4cc1 1129 goto fail_lock;
16739b06
CH
1130
1131 /* Sanity checks in case user changed something in the meantime */
a94e8427 1132 if (!priv->bssid_fixed)
16739b06
CH
1133 goto out;
1134
1135 if (strlen(priv->desired_essid) == 0)
1136 goto out;
1137
1138 /* Read scan results from the firmware */
1139 err = hermes_read_ltv(hw, USER_BAP,
1140 HERMES_RID_SCANRESULTSTABLE,
1141 MAX_SCAN_LEN, &len, buf);
1142 if (err) {
1143 printk(KERN_ERR "%s: Cannot read scan results\n",
1144 dev->name);
1145 goto out;
1146 }
1147
1148 len = HERMES_RECLEN_TO_BYTES(len);
1149
1150 /* Go through the scan results looking for the channel of the AP
1151 * we were requested to join */
1152 for (; offset + atom_len <= len; offset += atom_len) {
1153 atom = (struct prism2_scan_apinfo *) (buf + offset);
c89cc225
PR
1154 if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1155 found = 1;
1156 break;
1157 }
16739b06
CH
1158 }
1159
a94e8427 1160 if (!found) {
c89cc225
PR
1161 DEBUG(1, "%s: Requested AP not found in scan results\n",
1162 dev->name);
1163 goto out;
1164 }
16739b06 1165
16739b06
CH
1166 memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1167 req.channel = atom->channel; /* both are little-endian */
1168 err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1169 &req);
1170 if (err)
1171 printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1172
1173 out:
16739b06 1174 orinoco_unlock(priv, &flags);
f3cb4cc1
PR
1175
1176 fail_lock:
1177 kfree(buf);
16739b06
CH
1178}
1179
95dd91fb 1180/* Send new BSSID to userspace */
6cd90b1c 1181static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
95dd91fb 1182{
c4028958 1183 struct net_device *dev = priv->ndev;
95dd91fb
CH
1184 struct hermes *hw = &priv->hw;
1185 union iwreq_data wrqu;
1186 int err;
95dd91fb 1187
499b702a 1188 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
95dd91fb
CH
1189 ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
1190 if (err != 0)
6cd90b1c 1191 return;
95dd91fb
CH
1192
1193 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1194
1195 /* Send event to user space */
1196 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
6cd90b1c
DK
1197}
1198
06009fda
DK
1199static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
1200{
1201 struct net_device *dev = priv->ndev;
1202 struct hermes *hw = &priv->hw;
1203 union iwreq_data wrqu;
1204 int err;
1205 u8 buf[88];
1206 u8 *ie;
1207
1208 if (!priv->has_wpa)
1209 return;
1210
499b702a 1211 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
06009fda
DK
1212 sizeof(buf), NULL, &buf);
1213 if (err != 0)
1214 return;
1215
1216 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1217 if (ie) {
1218 int rem = sizeof(buf) - (ie - &buf[0]);
1219 wrqu.data.length = ie[1] + 2;
1220 if (wrqu.data.length > rem)
1221 wrqu.data.length = rem;
1222
1223 if (wrqu.data.length)
1224 /* Send event to user space */
1225 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
1226 }
1227}
1228
1229static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
1230{
1231 struct net_device *dev = priv->ndev;
1232 struct hermes *hw = &priv->hw;
1233 union iwreq_data wrqu;
1234 int err;
1235 u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
1236 u8 *ie;
1237
1238 if (!priv->has_wpa)
1239 return;
1240
499b702a 1241 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_RESP_INFO,
06009fda
DK
1242 sizeof(buf), NULL, &buf);
1243 if (err != 0)
1244 return;
1245
1246 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1247 if (ie) {
1248 int rem = sizeof(buf) - (ie - &buf[0]);
1249 wrqu.data.length = ie[1] + 2;
1250 if (wrqu.data.length > rem)
1251 wrqu.data.length = rem;
1252
1253 if (wrqu.data.length)
1254 /* Send event to user space */
1255 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
1256 }
1257}
1258
6cd90b1c
DK
1259static void orinoco_send_wevents(struct work_struct *work)
1260{
1261 struct orinoco_private *priv =
1262 container_of(work, struct orinoco_private, wevent_work);
1263 unsigned long flags;
1264
1265 if (orinoco_lock(priv, &flags) != 0)
1266 return;
1267
06009fda
DK
1268 orinoco_send_assocreqie_wevent(priv);
1269 orinoco_send_assocrespie_wevent(priv);
6cd90b1c 1270 orinoco_send_bssid_wevent(priv);
8aeabc37 1271
95dd91fb
CH
1272 orinoco_unlock(priv, &flags);
1273}
1274
c63cdbe8
DK
1275static void qbuf_scan(struct orinoco_private *priv, void *buf,
1276 int len, int type)
1277{
1278 struct orinoco_scan_data *sd;
1279 unsigned long flags;
1280
1281 sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
1282 sd->buf = buf;
1283 sd->len = len;
1284 sd->type = type;
1285
1286 spin_lock_irqsave(&priv->scan_lock, flags);
1287 list_add_tail(&sd->list, &priv->scan_list);
1288 spin_unlock_irqrestore(&priv->scan_lock, flags);
1289
1290 schedule_work(&priv->process_scan);
1291}
1292
1293static void qabort_scan(struct orinoco_private *priv)
1294{
1295 struct orinoco_scan_data *sd;
1296 unsigned long flags;
1297
1298 sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
1299 sd->len = -1; /* Abort */
1300
1301 spin_lock_irqsave(&priv->scan_lock, flags);
1302 list_add_tail(&sd->list, &priv->scan_list);
1303 spin_unlock_irqrestore(&priv->scan_lock, flags);
1304
1305 schedule_work(&priv->process_scan);
1306}
1307
1308static void orinoco_process_scan_results(struct work_struct *work)
1309{
1310 struct orinoco_private *priv =
1311 container_of(work, struct orinoco_private, process_scan);
1312 struct orinoco_scan_data *sd, *temp;
1313 unsigned long flags;
1314 void *buf;
1315 int len;
1316 int type;
1317
1318 spin_lock_irqsave(&priv->scan_lock, flags);
1319 list_for_each_entry_safe(sd, temp, &priv->scan_list, list) {
1320 spin_unlock_irqrestore(&priv->scan_lock, flags);
1321
1322 buf = sd->buf;
1323 len = sd->len;
1324 type = sd->type;
1325
1326 list_del(&sd->list);
1327 kfree(sd);
1328
1329 if (len > 0) {
1330 if (type == HERMES_INQ_CHANNELINFO)
1331 orinoco_add_extscan_result(priv, buf, len);
1332 else
1333 orinoco_add_hostscan_results(priv, buf, len);
1334
1335 kfree(buf);
1336 } else if (priv->scan_request) {
1337 /* Either abort or complete the scan */
1338 cfg80211_scan_done(priv->scan_request, (len < 0));
1339 priv->scan_request = NULL;
1340 }
1341
1342 spin_lock_irqsave(&priv->scan_lock, flags);
1343 }
1344 spin_unlock_irqrestore(&priv->scan_lock, flags);
1345}
1346
1da177e4
LT
1347static void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
1348{
ea60a6aa 1349 struct orinoco_private *priv = ndev_priv(dev);
1da177e4
LT
1350 u16 infofid;
1351 struct {
d133ae4c
PR
1352 __le16 len;
1353 __le16 type;
1da177e4
LT
1354 } __attribute__ ((packed)) info;
1355 int len, type;
1356 int err;
1357
1358 /* This is an answer to an INQUIRE command that we did earlier,
1359 * or an information "event" generated by the card
1360 * The controller return to us a pseudo frame containing
1361 * the information in question - Jean II */
1362 infofid = hermes_read_regn(hw, INFOFID);
1363
1364 /* Read the info frame header - don't try too hard */
1365 err = hermes_bap_pread(hw, IRQ_BAP, &info, sizeof(info),
1366 infofid, 0);
1367 if (err) {
1368 printk(KERN_ERR "%s: error %d reading info frame. "
1369 "Frame dropped.\n", dev->name, err);
1370 return;
1371 }
6fe9deb1 1372
1da177e4
LT
1373 len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
1374 type = le16_to_cpu(info.type);
1375
1376 switch (type) {
1377 case HERMES_INQ_TALLIES: {
1378 struct hermes_tallies_frame tallies;
1379 struct iw_statistics *wstats = &priv->wstats;
6fe9deb1 1380
1da177e4
LT
1381 if (len > sizeof(tallies)) {
1382 printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
1383 dev->name, len);
1384 len = sizeof(tallies);
1385 }
6fe9deb1 1386
84d8a2fb
CH
1387 err = hermes_bap_pread(hw, IRQ_BAP, &tallies, len,
1388 infofid, sizeof(info));
1389 if (err)
1390 break;
6fe9deb1 1391
1da177e4
LT
1392 /* Increment our various counters */
1393 /* wstats->discard.nwid - no wrong BSSID stuff */
1394 wstats->discard.code +=
1395 le16_to_cpu(tallies.RxWEPUndecryptable);
6fe9deb1 1396 if (len == sizeof(tallies))
1da177e4
LT
1397 wstats->discard.code +=
1398 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
1399 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
1400 wstats->discard.misc +=
1401 le16_to_cpu(tallies.TxDiscardsWrongSA);
1402 wstats->discard.fragment +=
1403 le16_to_cpu(tallies.RxMsgInBadMsgFragments);
1404 wstats->discard.retries +=
1405 le16_to_cpu(tallies.TxRetryLimitExceeded);
1406 /* wstats->miss.beacon - no match */
1407 }
1408 break;
1409 case HERMES_INQ_LINKSTATUS: {
1410 struct hermes_linkstatus linkstatus;
1411 u16 newstatus;
1412 int connected;
1413
5217c571 1414 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
8f2abf44
CH
1415 break;
1416
1da177e4
LT
1417 if (len != sizeof(linkstatus)) {
1418 printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
1419 dev->name, len);
1420 break;
1421 }
1422
84d8a2fb
CH
1423 err = hermes_bap_pread(hw, IRQ_BAP, &linkstatus, len,
1424 infofid, sizeof(info));
1425 if (err)
1426 break;
1da177e4
LT
1427 newstatus = le16_to_cpu(linkstatus.linkstatus);
1428
95dd91fb
CH
1429 /* Symbol firmware uses "out of range" to signal that
1430 * the hostscan frame can be requested. */
1431 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
1432 priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
c63cdbe8 1433 priv->has_hostscan && priv->scan_request) {
95dd91fb
CH
1434 hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
1435 break;
1436 }
1437
1da177e4
LT
1438 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
1439 || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
1440 || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
1441
1442 if (connected)
1443 netif_carrier_on(dev);
7bb7c3a3 1444 else if (!ignore_disconnect)
1da177e4
LT
1445 netif_carrier_off(dev);
1446
95dd91fb
CH
1447 if (newstatus != priv->last_linkstatus) {
1448 priv->last_linkstatus = newstatus;
1da177e4 1449 print_linkstatus(dev, newstatus);
95dd91fb
CH
1450 /* The info frame contains only one word which is the
1451 * status (see hermes.h). The status is pretty boring
1452 * in itself, that's why we export the new BSSID...
1453 * Jean II */
1454 schedule_work(&priv->wevent_work);
1455 }
1456 }
1457 break;
1458 case HERMES_INQ_SCAN:
c63cdbe8 1459 if (!priv->scan_request && priv->bssid_fixed &&
95dd91fb
CH
1460 priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
1461 schedule_work(&priv->join_work);
1462 break;
1463 }
1464 /* fall through */
1465 case HERMES_INQ_HOSTSCAN:
1466 case HERMES_INQ_HOSTSCAN_SYMBOL: {
1467 /* Result of a scanning. Contains information about
1468 * cells in the vicinity - Jean II */
95dd91fb
CH
1469 unsigned char *buf;
1470
1471 /* Sanity check */
1472 if (len > 4096) {
1473 printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
1474 dev->name, len);
c63cdbe8 1475 qabort_scan(priv);
95dd91fb
CH
1476 break;
1477 }
1478
95dd91fb
CH
1479 /* Allocate buffer for results */
1480 buf = kmalloc(len, GFP_ATOMIC);
c63cdbe8 1481 if (buf == NULL) {
95dd91fb 1482 /* No memory, so can't printk()... */
c63cdbe8 1483 qabort_scan(priv);
95dd91fb 1484 break;
c63cdbe8 1485 }
95dd91fb
CH
1486
1487 /* Read scan data */
1488 err = hermes_bap_pread(hw, IRQ_BAP, (void *) buf, len,
1489 infofid, sizeof(info));
708218b0
PR
1490 if (err) {
1491 kfree(buf);
c63cdbe8 1492 qabort_scan(priv);
95dd91fb 1493 break;
708218b0 1494 }
1da177e4 1495
95dd91fb
CH
1496#ifdef ORINOCO_DEBUG
1497 {
1498 int i;
1499 printk(KERN_DEBUG "Scan result [%02X", buf[0]);
a94e8427 1500 for (i = 1; i < (len * 2); i++)
95dd91fb
CH
1501 printk(":%02X", buf[i]);
1502 printk("]\n");
1503 }
1504#endif /* ORINOCO_DEBUG */
1505
c63cdbe8 1506 qbuf_scan(priv, buf, len, type);
1da177e4
LT
1507 }
1508 break;
01632fa4
DK
1509 case HERMES_INQ_CHANNELINFO:
1510 {
1511 struct agere_ext_scan_info *bss;
1512
c63cdbe8 1513 if (!priv->scan_request) {
01632fa4
DK
1514 printk(KERN_DEBUG "%s: Got chaninfo without scan, "
1515 "len=%d\n", dev->name, len);
1516 break;
1517 }
1518
1519 /* An empty result indicates that the scan is complete */
1520 if (len == 0) {
c63cdbe8 1521 qbuf_scan(priv, NULL, len, type);
01632fa4
DK
1522 break;
1523 }
1524
1525 /* Sanity check */
c63cdbe8 1526 else if (len < (offsetof(struct agere_ext_scan_info,
01632fa4
DK
1527 data) + 2)) {
1528 /* Drop this result now so we don't have to
1529 * keep checking later */
1530 printk(KERN_WARNING
1531 "%s: Ext scan results too short (%d bytes)\n",
1532 dev->name, len);
1533 break;
1534 }
1535
c63cdbe8 1536 bss = kmalloc(len, GFP_ATOMIC);
01632fa4
DK
1537 if (bss == NULL)
1538 break;
1539
1540 /* Read scan data */
1541 err = hermes_bap_pread(hw, IRQ_BAP, (void *) bss, len,
1542 infofid, sizeof(info));
c63cdbe8 1543 if (err)
01632fa4 1544 kfree(bss);
c63cdbe8
DK
1545 else
1546 qbuf_scan(priv, bss, len, type);
01632fa4 1547
01632fa4
DK
1548 break;
1549 }
95dd91fb
CH
1550 case HERMES_INQ_SEC_STAT_AGERE:
1551 /* Security status (Agere specific) */
1552 /* Ignore this frame for now */
1553 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
1554 break;
1555 /* fall through */
1da177e4
LT
1556 default:
1557 printk(KERN_DEBUG "%s: Unknown information frame received: "
1558 "type 0x%04x, length %d\n", dev->name, type, len);
1559 /* We don't actually do anything about it */
1560 break;
1561 }
c63cdbe8
DK
1562
1563 return;
1da177e4
LT
1564}
1565
1566static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
1567{
1568 if (net_ratelimit())
1569 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
1570}
1571
1572/********************************************************************/
1573/* Internal hardware control routines */
1574/********************************************************************/
1575
6415f7df 1576static int __orinoco_up(struct orinoco_private *priv)
1da177e4 1577{
a2608362 1578 struct net_device *dev = priv->ndev;
1da177e4
LT
1579 struct hermes *hw = &priv->hw;
1580 int err;
1581
84d8a2fb
CH
1582 netif_carrier_off(dev); /* just to make sure */
1583
721aa2f7 1584 err = __orinoco_commit(priv);
1da177e4
LT
1585 if (err) {
1586 printk(KERN_ERR "%s: Error %d configuring card\n",
1587 dev->name, err);
1588 return err;
1589 }
1590
1591 /* Fire things up again */
1592 hermes_set_irqmask(hw, ORINOCO_INTEN);
1593 err = hermes_enable_port(hw, 0);
1594 if (err) {
1595 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
1596 dev->name, err);
1597 return err;
1598 }
1599
1600 netif_start_queue(dev);
1601
1602 return 0;
1603}
1604
6415f7df 1605static int __orinoco_down(struct orinoco_private *priv)
1da177e4 1606{
a2608362 1607 struct net_device *dev = priv->ndev;
1da177e4
LT
1608 struct hermes *hw = &priv->hw;
1609 int err;
1610
1611 netif_stop_queue(dev);
1612
a94e8427
DK
1613 if (!priv->hw_unavailable) {
1614 if (!priv->broken_disableport) {
1da177e4
LT
1615 err = hermes_disable_port(hw, 0);
1616 if (err) {
1617 /* Some firmwares (e.g. Intersil 1.3.x) seem
1618 * to have problems disabling the port, oh
1619 * well, too bad. */
1620 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
1621 dev->name, err);
1622 priv->broken_disableport = 1;
1623 }
1624 }
1625 hermes_set_irqmask(hw, 0);
1626 hermes_write_regn(hw, EVACK, 0xffff);
1627 }
6fe9deb1 1628
1da177e4
LT
1629 /* firmware will have to reassociate */
1630 netif_carrier_off(dev);
1631 priv->last_linkstatus = 0xffff;
1632
1633 return 0;
1634}
1635
6415f7df 1636static int orinoco_reinit_firmware(struct orinoco_private *priv)
37a6c611 1637{
37a6c611
PR
1638 struct hermes *hw = &priv->hw;
1639 int err;
1640
1641 err = hermes_init(hw);
0df6cbb7
AB
1642 if (priv->do_fw_download && !err) {
1643 err = orinoco_download(priv);
1644 if (err)
1645 priv->do_fw_download = 0;
1646 }
37a6c611 1647 if (!err)
42a51b93 1648 err = orinoco_hw_allocate_fid(priv);
37a6c611
PR
1649
1650 return err;
1651}
1652
721aa2f7 1653static int
5865d015
DK
1654__orinoco_set_multicast_list(struct net_device *dev)
1655{
ea60a6aa 1656 struct orinoco_private *priv = ndev_priv(dev);
5865d015
DK
1657 int err = 0;
1658 int promisc, mc_count;
1659
1660 /* The Hermes doesn't seem to have an allmulti mode, so we go
1661 * into promiscuous mode and let the upper levels deal. */
1662 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
1663 (dev->mc_count > MAX_MULTICAST(priv))) {
1664 promisc = 1;
1665 mc_count = 0;
1666 } else {
1667 promisc = 0;
1668 mc_count = dev->mc_count;
1669 }
1670
1671 err = __orinoco_hw_set_multicast_list(priv, dev->mc_list, mc_count,
1672 promisc);
721aa2f7
DK
1673
1674 return err;
1da177e4
LT
1675}
1676
1da177e4
LT
1677/* This must be called from user context, without locks held - use
1678 * schedule_work() */
cb1576a8 1679void orinoco_reset(struct work_struct *work)
1da177e4 1680{
c4028958
DH
1681 struct orinoco_private *priv =
1682 container_of(work, struct orinoco_private, reset_work);
1683 struct net_device *dev = priv->ndev;
1da177e4 1684 struct hermes *hw = &priv->hw;
8551cb98 1685 int err;
1da177e4
LT
1686 unsigned long flags;
1687
1688 if (orinoco_lock(priv, &flags) != 0)
1689 /* When the hardware becomes available again, whatever
1690 * detects that is responsible for re-initializing
1691 * it. So no need for anything further */
1692 return;
1693
1694 netif_stop_queue(dev);
1695
1696 /* Shut off interrupts. Depending on what state the hardware
1697 * is in, this might not work, but we'll try anyway */
1698 hermes_set_irqmask(hw, 0);
1699 hermes_write_regn(hw, EVACK, 0xffff);
1700
1701 priv->hw_unavailable++;
1702 priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
1703 netif_carrier_off(dev);
1704
1705 orinoco_unlock(priv, &flags);
1706
c63cdbe8
DK
1707 /* Scanning support: Notify scan cancellation */
1708 if (priv->scan_request) {
1709 cfg80211_scan_done(priv->scan_request, 1);
1710 priv->scan_request = NULL;
1711 }
95dd91fb 1712
8551cb98 1713 if (priv->hard_reset) {
1da177e4 1714 err = (*priv->hard_reset)(priv);
8551cb98
CH
1715 if (err) {
1716 printk(KERN_ERR "%s: orinoco_reset: Error %d "
1717 "performing hard reset\n", dev->name, err);
1718 goto disable;
1719 }
1da177e4
LT
1720 }
1721
a2608362 1722 err = orinoco_reinit_firmware(priv);
1da177e4
LT
1723 if (err) {
1724 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
1725 dev->name, err);
8551cb98 1726 goto disable;
1da177e4
LT
1727 }
1728
b2f30a0a
DK
1729 /* This has to be called from user context */
1730 spin_lock_irq(&priv->lock);
1da177e4
LT
1731
1732 priv->hw_unavailable--;
1733
1734 /* priv->open or priv->hw_unavailable might have changed while
1735 * we dropped the lock */
a94e8427 1736 if (priv->open && (!priv->hw_unavailable)) {
a2608362 1737 err = __orinoco_up(priv);
1da177e4
LT
1738 if (err) {
1739 printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
1740 dev->name, err);
1741 } else
1742 dev->trans_start = jiffies;
1743 }
1744
1745 spin_unlock_irq(&priv->lock);
1746
1747 return;
8551cb98
CH
1748 disable:
1749 hermes_set_irqmask(hw, 0);
1750 netif_device_detach(dev);
1751 printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
1da177e4
LT
1752}
1753
721aa2f7
DK
1754static int __orinoco_commit(struct orinoco_private *priv)
1755{
1756 struct net_device *dev = priv->ndev;
1757 int err = 0;
1758
1759 err = orinoco_hw_program_rids(priv);
1760
1761 /* FIXME: what about netif_tx_lock */
1762 (void) __orinoco_set_multicast_list(dev);
1763
1764 return err;
1765}
1766
1767/* Ensures configuration changes are applied. May result in a reset.
1768 * The caller should hold priv->lock
1769 */
1770int orinoco_commit(struct orinoco_private *priv)
1771{
1772 struct net_device *dev = priv->ndev;
1773 hermes_t *hw = &priv->hw;
1774 int err;
1775
1776 if (priv->broken_disableport) {
1777 schedule_work(&priv->reset_work);
1778 return 0;
1779 }
1780
1781 err = hermes_disable_port(hw, 0);
1782 if (err) {
1783 printk(KERN_WARNING "%s: Unable to disable port "
1784 "while reconfiguring card\n", dev->name);
1785 priv->broken_disableport = 1;
1786 goto out;
1787 }
1788
1789 err = __orinoco_commit(priv);
1790 if (err) {
1791 printk(KERN_WARNING "%s: Unable to reconfigure card\n",
1792 dev->name);
1793 goto out;
1794 }
1795
1796 err = hermes_enable_port(hw, 0);
1797 if (err) {
1798 printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
1799 dev->name);
1800 goto out;
1801 }
1802
1803 out:
1804 if (err) {
1805 printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
1806 schedule_work(&priv->reset_work);
1807 err = 0;
1808 }
1809 return err;
1810}
1811
1da177e4
LT
1812/********************************************************************/
1813/* Interrupt handler */
1814/********************************************************************/
1815
1816static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
1817{
1818 printk(KERN_DEBUG "%s: TICK\n", dev->name);
1819}
1820
1821static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
1822{
1823 /* This seems to happen a fair bit under load, but ignoring it
1824 seems to work fine...*/
1825 printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
1826 dev->name);
1827}
1828
7d12e780 1829irqreturn_t orinoco_interrupt(int irq, void *dev_id)
1da177e4 1830{
a2608362
DK
1831 struct orinoco_private *priv = dev_id;
1832 struct net_device *dev = priv->ndev;
1da177e4
LT
1833 hermes_t *hw = &priv->hw;
1834 int count = MAX_IRQLOOPS_PER_IRQ;
1835 u16 evstat, events;
2131266d
DK
1836 /* These are used to detect a runaway interrupt situation.
1837 *
1838 * If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
1839 * we panic and shut down the hardware
1840 */
1841 /* jiffies value the last time we were called */
1842 static int last_irq_jiffy; /* = 0 */
1843 static int loops_this_jiffy; /* = 0 */
1da177e4
LT
1844 unsigned long flags;
1845
1846 if (orinoco_lock(priv, &flags) != 0) {
1847 /* If hw is unavailable - we don't know if the irq was
1848 * for us or not */
1849 return IRQ_HANDLED;
1850 }
1851
1852 evstat = hermes_read_regn(hw, EVSTAT);
1853 events = evstat & hw->inten;
a94e8427 1854 if (!events) {
1da177e4
LT
1855 orinoco_unlock(priv, &flags);
1856 return IRQ_NONE;
1857 }
6fe9deb1 1858
1da177e4
LT
1859 if (jiffies != last_irq_jiffy)
1860 loops_this_jiffy = 0;
1861 last_irq_jiffy = jiffies;
1862
1863 while (events && count--) {
1864 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
1865 printk(KERN_WARNING "%s: IRQ handler is looping too "
1866 "much! Resetting.\n", dev->name);
1867 /* Disable interrupts for now */
1868 hermes_set_irqmask(hw, 0);
1869 schedule_work(&priv->reset_work);
1870 break;
1871 }
1872
1873 /* Check the card hasn't been removed */
a94e8427 1874 if (!hermes_present(hw)) {
1da177e4
LT
1875 DEBUG(0, "orinoco_interrupt(): card removed\n");
1876 break;
1877 }
1878
1879 if (events & HERMES_EV_TICK)
1880 __orinoco_ev_tick(dev, hw);
1881 if (events & HERMES_EV_WTERR)
1882 __orinoco_ev_wterr(dev, hw);
1883 if (events & HERMES_EV_INFDROP)
1884 __orinoco_ev_infdrop(dev, hw);
1885 if (events & HERMES_EV_INFO)
1886 __orinoco_ev_info(dev, hw);
1887 if (events & HERMES_EV_RX)
1888 __orinoco_ev_rx(dev, hw);
1889 if (events & HERMES_EV_TXEXC)
1890 __orinoco_ev_txexc(dev, hw);
1891 if (events & HERMES_EV_TX)
1892 __orinoco_ev_tx(dev, hw);
1893 if (events & HERMES_EV_ALLOC)
1894 __orinoco_ev_alloc(dev, hw);
6fe9deb1 1895
84d8a2fb 1896 hermes_write_regn(hw, EVACK, evstat);
1da177e4
LT
1897
1898 evstat = hermes_read_regn(hw, EVSTAT);
1899 events = evstat & hw->inten;
1900 };
1901
1902 orinoco_unlock(priv, &flags);
1903 return IRQ_HANDLED;
1904}
2131266d 1905EXPORT_SYMBOL(orinoco_interrupt);
1da177e4 1906
39d1ffee
DK
1907/********************************************************************/
1908/* Power management */
1909/********************************************************************/
1910#if defined(CONFIG_PM_SLEEP) && !defined(CONFIG_HERMES_CACHE_FW_ON_INIT)
1911static int orinoco_pm_notifier(struct notifier_block *notifier,
1912 unsigned long pm_event,
1913 void *unused)
1914{
1915 struct orinoco_private *priv = container_of(notifier,
1916 struct orinoco_private,
1917 pm_notifier);
1918
1919 /* All we need to do is cache the firmware before suspend, and
1920 * release it when we come out.
1921 *
1922 * Only need to do this if we're downloading firmware. */
1923 if (!priv->do_fw_download)
1924 return NOTIFY_DONE;
1925
1926 switch (pm_event) {
1927 case PM_HIBERNATION_PREPARE:
1928 case PM_SUSPEND_PREPARE:
1929 orinoco_cache_fw(priv, 0);
1930 break;
1931
1932 case PM_POST_RESTORE:
1933 /* Restore from hibernation failed. We need to clean
1934 * up in exactly the same way, so fall through. */
1935 case PM_POST_HIBERNATION:
1936 case PM_POST_SUSPEND:
1937 orinoco_uncache_fw(priv);
1938 break;
1939
1940 case PM_RESTORE_PREPARE:
1941 default:
1942 break;
1943 }
1944
1945 return NOTIFY_DONE;
1946}
f11c179e
DM
1947
1948static void orinoco_register_pm_notifier(struct orinoco_private *priv)
1949{
1950 priv->pm_notifier.notifier_call = orinoco_pm_notifier;
1951 register_pm_notifier(&priv->pm_notifier);
1952}
1953
1954static void orinoco_unregister_pm_notifier(struct orinoco_private *priv)
1955{
1956 unregister_pm_notifier(&priv->pm_notifier);
1957}
39d1ffee 1958#else /* !PM_SLEEP || HERMES_CACHE_FW_ON_INIT */
f11c179e
DM
1959#define orinoco_register_pm_notifier(priv) do { } while(0)
1960#define orinoco_unregister_pm_notifier(priv) do { } while(0)
39d1ffee
DK
1961#endif
1962
1da177e4
LT
1963/********************************************************************/
1964/* Initialization */
1965/********************************************************************/
1966
8e638267 1967int orinoco_init(struct orinoco_private *priv)
1da177e4 1968{
8e638267 1969 struct device *dev = priv->dev;
ea60a6aa 1970 struct wiphy *wiphy = priv_to_wiphy(priv);
1da177e4
LT
1971 hermes_t *hw = &priv->hw;
1972 int err = 0;
1da177e4 1973
1da177e4
LT
1974 /* No need to lock, the hw_unavailable flag is already set in
1975 * alloc_orinocodev() */
2c706002 1976 priv->nicbuf_size = IEEE80211_MAX_FRAME_LEN + ETH_HLEN;
1da177e4
LT
1977
1978 /* Initialize the firmware */
37a6c611 1979 err = hermes_init(hw);
1da177e4 1980 if (err != 0) {
8e638267
DK
1981 dev_err(dev, "Failed to initialize firmware (err = %d)\n",
1982 err);
1da177e4
LT
1983 goto out;
1984 }
1985
a2d1a42a 1986 err = determine_fw_capabilities(priv);
1da177e4 1987 if (err != 0) {
8e638267 1988 dev_err(dev, "Incompatible firmware, aborting\n");
1da177e4
LT
1989 goto out;
1990 }
1991
3994d502 1992 if (priv->do_fw_download) {
39d1ffee 1993#ifdef CONFIG_HERMES_CACHE_FW_ON_INIT
74734312 1994 orinoco_cache_fw(priv, 0);
39d1ffee 1995#endif
74734312 1996
3994d502
DK
1997 err = orinoco_download(priv);
1998 if (err)
1999 priv->do_fw_download = 0;
2000
2001 /* Check firmware version again */
a2d1a42a 2002 err = determine_fw_capabilities(priv);
3994d502 2003 if (err != 0) {
8e638267 2004 dev_err(dev, "Incompatible firmware, aborting\n");
3994d502
DK
2005 goto out;
2006 }
2007 }
2008
1da177e4 2009 if (priv->has_port3)
8e638267 2010 dev_info(dev, "Ad-hoc demo mode supported\n");
1da177e4 2011 if (priv->has_ibss)
8e638267
DK
2012 dev_info(dev, "IEEE standard IBSS ad-hoc mode supported\n");
2013 if (priv->has_wep)
2014 dev_info(dev, "WEP supported, %s-bit key\n",
2015 priv->has_big_wep ? "104" : "40");
23edcc41 2016 if (priv->has_wpa) {
8e638267 2017 dev_info(dev, "WPA-PSK supported\n");
23edcc41 2018 if (orinoco_mic_init(priv)) {
8e638267
DK
2019 dev_err(dev, "Failed to setup MIC crypto algorithm. "
2020 "Disabling WPA support\n");
23edcc41
DK
2021 priv->has_wpa = 0;
2022 }
2023 }
1da177e4 2024
ea60a6aa 2025 err = orinoco_hw_read_card_settings(priv, wiphy->perm_addr);
e9e3d010 2026 if (err)
1da177e4 2027 goto out;
1da177e4 2028
42a51b93 2029 err = orinoco_hw_allocate_fid(priv);
37a6c611 2030 if (err) {
8e638267 2031 dev_err(dev, "Failed to allocate NIC buffer!\n");
37a6c611
PR
2032 goto out;
2033 }
2034
1da177e4 2035 /* Set up the default configuration */
5217c571 2036 priv->iw_mode = NL80211_IFTYPE_STATION;
1da177e4 2037 /* By default use IEEE/IBSS ad-hoc mode if we have it */
a94e8427 2038 priv->prefer_port3 = priv->has_port3 && (!priv->has_ibss);
1da177e4 2039 set_port_type(priv);
d51d8b1f 2040 priv->channel = 0; /* use firmware default */
1da177e4
LT
2041
2042 priv->promiscuous = 0;
5c9f41e2 2043 priv->encode_alg = ORINOCO_ALG_NONE;
1da177e4 2044 priv->tx_key = 0;
d03032af
DK
2045 priv->wpa_enabled = 0;
2046 priv->tkip_cm_active = 0;
2047 priv->key_mgmt = 0;
2048 priv->wpa_ie_len = 0;
2049 priv->wpa_ie = NULL;
1da177e4 2050
ea60a6aa
DK
2051 if (orinoco_wiphy_register(wiphy)) {
2052 err = -ENODEV;
2053 goto out;
2054 }
2055
1da177e4
LT
2056 /* Make the hardware available, as long as it hasn't been
2057 * removed elsewhere (e.g. by PCMCIA hot unplug) */
2058 spin_lock_irq(&priv->lock);
2059 priv->hw_unavailable--;
2060 spin_unlock_irq(&priv->lock);
2061
8e638267 2062 dev_dbg(dev, "Ready\n");
1da177e4
LT
2063
2064 out:
1da177e4
LT
2065 return err;
2066}
8e638267 2067EXPORT_SYMBOL(orinoco_init);
1da177e4 2068
89ea4090 2069static const struct net_device_ops orinoco_netdev_ops = {
89ea4090
AB
2070 .ndo_open = orinoco_open,
2071 .ndo_stop = orinoco_stop,
2072 .ndo_start_xmit = orinoco_xmit,
2073 .ndo_set_multicast_list = orinoco_set_multicast_list,
2074 .ndo_change_mtu = orinoco_change_mtu,
240c102d
BH
2075 .ndo_set_mac_address = eth_mac_addr,
2076 .ndo_validate_addr = eth_validate_addr,
89ea4090
AB
2077 .ndo_tx_timeout = orinoco_tx_timeout,
2078 .ndo_get_stats = orinoco_get_stats,
2079};
2080
ea60a6aa
DK
2081/* Allocate private data.
2082 *
2083 * This driver has a number of structures associated with it
2084 * netdev - Net device structure for each network interface
2085 * wiphy - structure associated with wireless phy
2086 * wireless_dev (wdev) - structure for each wireless interface
2087 * hw - structure for hermes chip info
2088 * card - card specific structure for use by the card driver
2089 * (airport, orinoco_cs)
2090 * priv - orinoco private data
2091 * device - generic linux device structure
2092 *
2093 * +---------+ +---------+
2094 * | wiphy | | netdev |
2095 * | +-------+ | +-------+
2096 * | | priv | | | wdev |
2097 * | | +-----+ +-+-------+
2098 * | | | hw |
2099 * | +-+-----+
2100 * | | card |
2101 * +-+-------+
2102 *
2103 * priv has a link to netdev and device
2104 * wdev has a link to wiphy
2105 */
a2608362 2106struct orinoco_private
3994d502
DK
2107*alloc_orinocodev(int sizeof_card,
2108 struct device *device,
2109 int (*hard_reset)(struct orinoco_private *),
2110 int (*stop_fw)(struct orinoco_private *, int))
1da177e4 2111{
1da177e4 2112 struct orinoco_private *priv;
ea60a6aa
DK
2113 struct wiphy *wiphy;
2114
2115 /* allocate wiphy
2116 * NOTE: We only support a single virtual interface
2117 * but this may change when monitor mode is added
2118 */
2119 wiphy = wiphy_new(&orinoco_cfg_ops,
2120 sizeof(struct orinoco_private) + sizeof_card);
2121 if (!wiphy)
2122 return NULL;
1da177e4 2123
ea60a6aa 2124 priv = wiphy_priv(wiphy);
5381956b 2125 priv->dev = device;
ea60a6aa 2126
1da177e4 2127 if (sizeof_card)
84d8a2fb 2128 priv->card = (void *)((unsigned long)priv
1da177e4
LT
2129 + sizeof(struct orinoco_private));
2130 else
2131 priv->card = NULL;
2132
ea60a6aa
DK
2133 orinoco_wiphy_init(wiphy);
2134
343c686c
PR
2135#ifdef WIRELESS_SPY
2136 priv->wireless_data.spy_data = &priv->spy_data;
343c686c 2137#endif
23edcc41 2138
1da177e4 2139 /* Set up default callbacks */
1da177e4 2140 priv->hard_reset = hard_reset;
3994d502 2141 priv->stop_fw = stop_fw;
1da177e4
LT
2142
2143 spin_lock_init(&priv->lock);
2144 priv->open = 0;
2145 priv->hw_unavailable = 1; /* orinoco_init() must clear this
2146 * before anything else touches the
2147 * hardware */
c4028958
DH
2148 INIT_WORK(&priv->reset_work, orinoco_reset);
2149 INIT_WORK(&priv->join_work, orinoco_join_ap);
2150 INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
1da177e4 2151
31afcef3
DK
2152 INIT_LIST_HEAD(&priv->rx_list);
2153 tasklet_init(&priv->rx_tasklet, orinoco_rx_isr_tasklet,
5381956b 2154 (unsigned long) priv);
31afcef3 2155
c63cdbe8
DK
2156 spin_lock_init(&priv->scan_lock);
2157 INIT_LIST_HEAD(&priv->scan_list);
2158 INIT_WORK(&priv->process_scan, orinoco_process_scan_results);
2159
1da177e4
LT
2160 priv->last_linkstatus = 0xffff;
2161
2bfc5cb5 2162#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
2cea7b26 2163 priv->cached_pri_fw = NULL;
4fb30784 2164 priv->cached_fw = NULL;
2bfc5cb5 2165#endif
4fb30784 2166
39d1ffee 2167 /* Register PM notifiers */
f11c179e 2168 orinoco_register_pm_notifier(priv);
39d1ffee 2169
a2608362 2170 return priv;
1da177e4 2171}
2131266d 2172EXPORT_SYMBOL(alloc_orinocodev);
1da177e4 2173
5381956b
DK
2174/* We can only support a single interface. We provide a separate
2175 * function to set it up to distinguish between hardware
2176 * initialisation and interface setup.
2177 *
2178 * The base_addr and irq parameters are passed on to netdev for use
2179 * with SIOCGIFMAP.
2180 */
2181int orinoco_if_add(struct orinoco_private *priv,
2182 unsigned long base_addr,
2183 unsigned int irq)
2184{
2185 struct wiphy *wiphy = priv_to_wiphy(priv);
2186 struct wireless_dev *wdev;
2187 struct net_device *dev;
2188 int ret;
2189
2190 dev = alloc_etherdev(sizeof(struct wireless_dev));
2191
2192 if (!dev)
2193 return -ENOMEM;
2194
2195 /* Initialise wireless_dev */
2196 wdev = netdev_priv(dev);
2197 wdev->wiphy = wiphy;
2198 wdev->iftype = NL80211_IFTYPE_STATION;
2199
2200 /* Setup / override net_device fields */
2201 dev->ieee80211_ptr = wdev;
2202 dev->netdev_ops = &orinoco_netdev_ops;
2203 dev->watchdog_timeo = HZ; /* 1 second timeout */
2204 dev->ethtool_ops = &orinoco_ethtool_ops;
2205 dev->wireless_handlers = &orinoco_handler_def;
2206#ifdef WIRELESS_SPY
2207 dev->wireless_data = &priv->wireless_data;
2208#endif
2209 /* we use the default eth_mac_addr for setting the MAC addr */
2210
2211 /* Reserve space in skb for the SNAP header */
2212 dev->hard_header_len += ENCAPS_OVERHEAD;
2213
2214 netif_carrier_off(dev);
2215
2216 memcpy(dev->dev_addr, wiphy->perm_addr, ETH_ALEN);
2217
2218 dev->base_addr = base_addr;
2219 dev->irq = irq;
2220
2221 SET_NETDEV_DEV(dev, priv->dev);
2222 ret = register_netdev(dev);
2223 if (ret)
2224 goto fail;
2225
2226 priv->ndev = dev;
2227
2228 /* Report what we've done */
2229 dev_dbg(priv->dev, "Registerred interface %s.\n", dev->name);
2230
2231 return 0;
2232
2233 fail:
2234 free_netdev(dev);
2235 return ret;
2236}
2237EXPORT_SYMBOL(orinoco_if_add);
2238
2239void orinoco_if_del(struct orinoco_private *priv)
1da177e4 2240{
a2608362 2241 struct net_device *dev = priv->ndev;
5381956b
DK
2242
2243 unregister_netdev(dev);
2244 free_netdev(dev);
2245}
2246EXPORT_SYMBOL(orinoco_if_del);
2247
2248void free_orinocodev(struct orinoco_private *priv)
2249{
ea60a6aa 2250 struct wiphy *wiphy = priv_to_wiphy(priv);
20953ad6 2251 struct orinoco_rx_data *rx_data, *temp;
c63cdbe8 2252 struct orinoco_scan_data *sd, *sdtemp;
95dd91fb 2253
ea60a6aa
DK
2254 wiphy_unregister(wiphy);
2255
20953ad6
DK
2256 /* If the tasklet is scheduled when we call tasklet_kill it
2257 * will run one final time. However the tasklet will only
2258 * drain priv->rx_list if the hw is still available. */
31afcef3 2259 tasklet_kill(&priv->rx_tasklet);
74734312 2260
20953ad6
DK
2261 /* Explicitly drain priv->rx_list */
2262 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
2263 list_del(&rx_data->list);
2264
2265 dev_kfree_skb(rx_data->skb);
2266 kfree(rx_data->desc);
2267 kfree(rx_data);
2268 }
2269
c63cdbe8
DK
2270 cancel_work_sync(&priv->process_scan);
2271 /* Explicitly drain priv->scan_list */
2272 list_for_each_entry_safe(sd, sdtemp, &priv->scan_list, list) {
2273 list_del(&sd->list);
2274
2275 if ((sd->len > 0) && sd->buf)
2276 kfree(sd->buf);
2277 kfree(sd);
2278 }
2279
f11c179e 2280 orinoco_unregister_pm_notifier(priv);
74734312
DK
2281 orinoco_uncache_fw(priv);
2282
d03032af
DK
2283 priv->wpa_ie_len = 0;
2284 kfree(priv->wpa_ie);
23edcc41 2285 orinoco_mic_free(priv);
ea60a6aa 2286 wiphy_free(wiphy);
1da177e4 2287}
2131266d 2288EXPORT_SYMBOL(free_orinocodev);
1da177e4 2289
6415f7df
DK
2290int orinoco_up(struct orinoco_private *priv)
2291{
2292 struct net_device *dev = priv->ndev;
2293 unsigned long flags;
2294 int err;
2295
2296 spin_lock_irqsave(&priv->lock, flags);
2297
2298 err = orinoco_reinit_firmware(priv);
2299 if (err) {
2300 printk(KERN_ERR "%s: Error %d re-initializing firmware\n",
2301 dev->name, err);
2302 goto exit;
2303 }
2304
2305 netif_device_attach(dev);
2306 priv->hw_unavailable--;
2307
2308 if (priv->open && !priv->hw_unavailable) {
2309 err = __orinoco_up(priv);
2310 if (err)
2311 printk(KERN_ERR "%s: Error %d restarting card\n",
2312 dev->name, err);
2313 }
2314
2315exit:
2316 spin_unlock_irqrestore(&priv->lock, flags);
2317
2318 return 0;
2319}
2320EXPORT_SYMBOL(orinoco_up);
2321
2322void orinoco_down(struct orinoco_private *priv)
2323{
2324 struct net_device *dev = priv->ndev;
2325 unsigned long flags;
2326 int err;
2327
2328 spin_lock_irqsave(&priv->lock, flags);
2329 err = __orinoco_down(priv);
2330 if (err)
2331 printk(KERN_WARNING "%s: Error %d downing interface\n",
2332 dev->name, err);
2333
2334 netif_device_detach(dev);
2335 priv->hw_unavailable++;
2336 spin_unlock_irqrestore(&priv->lock, flags);
2337}
2338EXPORT_SYMBOL(orinoco_down);
2339
1fab2e8b
CH
2340static void orinoco_get_drvinfo(struct net_device *dev,
2341 struct ethtool_drvinfo *info)
2342{
ea60a6aa 2343 struct orinoco_private *priv = ndev_priv(dev);
1da177e4 2344
1fab2e8b
CH
2345 strncpy(info->driver, DRIVER_NAME, sizeof(info->driver) - 1);
2346 strncpy(info->version, DRIVER_VERSION, sizeof(info->version) - 1);
2347 strncpy(info->fw_version, priv->fw_name, sizeof(info->fw_version) - 1);
43cb76d9 2348 if (dev->dev.parent)
fb28ad35 2349 strncpy(info->bus_info, dev_name(dev->dev.parent),
1fab2e8b
CH
2350 sizeof(info->bus_info) - 1);
2351 else
2352 snprintf(info->bus_info, sizeof(info->bus_info) - 1,
2353 "PCMCIA %p", priv->hw.iobase);
1da177e4
LT
2354}
2355
7282d491 2356static const struct ethtool_ops orinoco_ethtool_ops = {
1fab2e8b
CH
2357 .get_drvinfo = orinoco_get_drvinfo,
2358 .get_link = ethtool_op_get_link,
2359};
1da177e4 2360
1da177e4
LT
2361/********************************************************************/
2362/* Module initialization */
2363/********************************************************************/
2364
1da177e4
LT
2365/* Can't be declared "const" or the whole __initdata section will
2366 * become const */
2367static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
2368 " (David Gibson <hermes@gibson.dropbear.id.au>, "
2369 "Pavel Roskin <proski@gnu.org>, et al)";
2370
2371static int __init init_orinoco(void)
2372{
2373 printk(KERN_DEBUG "%s\n", version);
2374 return 0;
2375}
2376
2377static void __exit exit_orinoco(void)
2378{
2379}
2380
2381module_init(init_orinoco);
2382module_exit(exit_orinoco);