b43legacy: use frame control helpers
[safe/jmp/linux-2.6] / drivers / net / wireless / atmel.c
index f0ccfef..28b6ff3 100644 (file)
 
 ******************************************************************************/
 
-#include <linux/config.h>
 #include <linux/init.h>
 
 #include <linux/kernel.h>
-#include <linux/sched.h>
 #include <linux/ptrace.h>
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/ctype.h>
 #include <linux/timer.h>
+#include <asm/byteorder.h>
 #include <asm/io.h>
 #include <asm/system.h>
 #include <asm/uaccess.h>
 #include <linux/delay.h>
 #include <linux/wireless.h>
 #include <net/iw_handler.h>
-#include <linux/byteorder/generic.h>
 #include <linux/crc32.h>
 #include <linux/proc_fs.h>
 #include <linux/device.h>
 #include <linux/moduleparam.h>
 #include <linux/firmware.h>
+#include <linux/jiffies.h>
 #include <net/ieee80211.h>
 #include "atmel.h"
 
@@ -137,44 +136,6 @@ static struct {
 #define MAC_BOOT_COMPLETE       0x0010        // MAC boot has been completed
 #define MAC_INIT_OK             0x0002        // MAC boot has been completed
 
-#define C80211_SUBTYPE_MGMT_ASS_REQUEST                 0x00
-#define C80211_SUBTYPE_MGMT_ASS_RESPONSE                0x10
-#define C80211_SUBTYPE_MGMT_REASS_REQUEST               0x20
-#define C80211_SUBTYPE_MGMT_REASS_RESPONSE              0x30
-#define C80211_SUBTYPE_MGMT_ProbeRequest                0x40
-#define C80211_SUBTYPE_MGMT_ProbeResponse               0x50
-#define C80211_SUBTYPE_MGMT_BEACON                      0x80
-#define C80211_SUBTYPE_MGMT_ATIM                        0x90
-#define C80211_SUBTYPE_MGMT_DISASSOSIATION              0xA0
-#define C80211_SUBTYPE_MGMT_Authentication              0xB0
-#define C80211_SUBTYPE_MGMT_Deauthentication    0xC0
-
-#define C80211_MGMT_AAN_OPENSYSTEM              0x0000
-#define C80211_MGMT_AAN_SHAREDKEY               0x0001
-
-#define C80211_MGMT_CAPABILITY_ESS              0x0001  // see 802.11 p.58
-#define C80211_MGMT_CAPABILITY_IBSS             0x0002  //      - " -
-#define C80211_MGMT_CAPABILITY_CFPollable       0x0004  //      - " -
-#define C80211_MGMT_CAPABILITY_CFPollRequest    0x0008  //      - " -
-#define C80211_MGMT_CAPABILITY_Privacy          0x0010  //      - " -
-
-#define C80211_MGMT_SC_Success                  0
-#define C80211_MGMT_SC_Unspecified              1
-#define C80211_MGMT_SC_SupportCapabilities      10
-#define C80211_MGMT_SC_ReassDenied              11
-#define C80211_MGMT_SC_AssDenied                12
-#define C80211_MGMT_SC_AuthAlgNotSupported      13
-#define C80211_MGMT_SC_AuthTransSeqNumError     14
-#define C80211_MGMT_SC_AuthRejectChallenge      15
-#define C80211_MGMT_SC_AuthRejectTimeout        16
-#define C80211_MGMT_SC_AssDeniedHandleAP        17
-#define C80211_MGMT_SC_AssDeniedBSSRate         18
-
-#define C80211_MGMT_ElementID_SSID              0
-#define C80211_MGMT_ElementID_SupportedRates    1
-#define C80211_MGMT_ElementID_ChallengeText     16
-#define C80211_MGMT_CAPABILITY_ShortPreamble    0x0020
-
 #define MIB_MAX_DATA_BYTES    212
 #define MIB_HEADER_SIZE       4    /* first four fields */
 
@@ -472,7 +433,6 @@ struct atmel_private {
        struct net_device *dev;
        struct device *sys_dev;
        struct iw_statistics wstats;
-       struct net_device_stats stats;  // device stats
        spinlock_t irqlock, timerlock;  // spinlocks
        enum { BUS_TYPE_PCCARD, BUS_TYPE_PCI } bus_type;
        enum {
@@ -556,7 +516,7 @@ struct atmel_private {
                SITE_SURVEY_IN_PROGRESS,
                SITE_SURVEY_COMPLETED
        } site_survey_state;
-       time_t last_survey;
+       unsigned long last_survey;
 
        int station_was_associated, station_is_associated;
        int fast_scan;
@@ -634,7 +594,7 @@ static void atmel_join_bss(struct atmel_private *priv, int bss_index);
 static void atmel_smooth_qual(struct atmel_private *priv);
 static void atmel_writeAR(struct net_device *dev, u16 data);
 static int probe_atmel_card(struct net_device *dev);
-static int reset_atmel_card(struct net_device *dev );
+static int reset_atmel_card(struct net_device *dev);
 static void atmel_enter_state(struct atmel_private *priv, int new_state);
 int atmel_open (struct net_device *dev);
 
@@ -733,9 +693,9 @@ static void tx_done_irq(struct atmel_private *priv)
 
                if (type == TX_PACKET_TYPE_DATA) {
                        if (status == TX_STATUS_SUCCESS)
-                               priv->stats.tx_packets++;
+                               priv->dev->stats.tx_packets++;
                        else
-                               priv->stats.tx_errors++;
+                               priv->dev->stats.tx_errors++;
                        netif_wake_queue(priv->dev);
                }
        }
@@ -823,21 +783,21 @@ static void tx_update_descriptor(struct atmel_private *priv, int is_bcast,
 
 static int start_tx(struct sk_buff *skb, struct net_device *dev)
 {
+       static const u8 SNAP_RFC1024[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
        struct atmel_private *priv = netdev_priv(dev);
        struct ieee80211_hdr_4addr header;
        unsigned long flags;
        u16 buff, frame_ctl, len = (ETH_ZLEN < skb->len) ? skb->len : ETH_ZLEN;
-       u8 SNAP_RFC1024[6] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
 
        if (priv->card && priv->present_callback &&
            !(*priv->present_callback)(priv->card)) {
-               priv->stats.tx_errors++;
+               dev->stats.tx_errors++;
                dev_kfree_skb(skb);
                return 0;
        }
 
        if (priv->station_state != STATION_STATE_READY) {
-               priv->stats.tx_errors++;
+               dev->stats.tx_errors++;
                dev_kfree_skb(skb);
                return 0;
        }
@@ -854,7 +814,7 @@ static int start_tx(struct sk_buff *skb, struct net_device *dev)
           initial + 18 (+30-12) */
 
        if (!(buff = find_tx_buff(priv, len + 18))) {
-               priv->stats.tx_dropped++;
+               dev->stats.tx_dropped++;
                spin_unlock_irqrestore(&priv->irqlock, flags);
                spin_unlock_bh(&priv->timerlock);
                netif_stop_queue(dev);
@@ -867,14 +827,14 @@ static int start_tx(struct sk_buff *skb, struct net_device *dev)
        if (priv->wep_is_on)
                frame_ctl |= IEEE80211_FCTL_PROTECTED;
        if (priv->operating_mode == IW_MODE_ADHOC) {
-               memcpy(&header.addr1, skb->data, 6);
+               skb_copy_from_linear_data(skb, &header.addr1, 6);
                memcpy(&header.addr2, dev->dev_addr, 6);
                memcpy(&header.addr3, priv->BSSID, 6);
        } else {
                frame_ctl |= IEEE80211_FCTL_TODS;
                memcpy(&header.addr1, priv->CurrentBSSID, 6);
                memcpy(&header.addr2, dev->dev_addr, 6);
-               memcpy(&header.addr3, skb->data, 6);
+               skb_copy_from_linear_data(skb, &header.addr3, 6);
        }
 
        if (priv->use_wpa)
@@ -890,7 +850,7 @@ static int start_tx(struct sk_buff *skb, struct net_device *dev)
        /* low bit of first byte of destination tells us if broadcast */
        tx_update_descriptor(priv, *(skb->data) & 0x01, len + 18, buff, TX_PACKET_TYPE_DATA);
        dev->trans_start = jiffies;
-       priv->stats.tx_bytes += len;
+       dev->stats.tx_bytes += len;
 
        spin_unlock_irqrestore(&priv->irqlock, flags);
        spin_unlock_bh(&priv->timerlock);
@@ -934,7 +894,7 @@ static void fast_rx_path(struct atmel_private *priv,
        }
 
        if (!(skb = dev_alloc_skb(msdu_size + 14))) {
-               priv->stats.rx_dropped++;
+               priv->dev->stats.rx_dropped++;
                return;
        }
 
@@ -947,7 +907,7 @@ static void fast_rx_path(struct atmel_private *priv,
                crc = crc32_le(crc, skbp + 12, msdu_size);
                atmel_copy_to_host(priv->dev, (void *)&netcrc, rx_packet_loc + 30 + msdu_size, 4);
                if ((crc ^ 0xffffffff) != netcrc) {
-                       priv->stats.rx_crc_errors++;
+                       priv->dev->stats.rx_crc_errors++;
                        dev_kfree_skb(skb);
                        return;
                }
@@ -960,12 +920,11 @@ static void fast_rx_path(struct atmel_private *priv,
                memcpy(&skbp[6], header->addr2, 6); /* source address */
 
        priv->dev->last_rx = jiffies;
-       skb->dev = priv->dev;
        skb->protocol = eth_type_trans(skb, priv->dev);
        skb->ip_summed = CHECKSUM_NONE;
        netif_rx(skb);
-       priv->stats.rx_bytes += 12 + msdu_size;
-       priv->stats.rx_packets++;
+       priv->dev->stats.rx_bytes += 12 + msdu_size;
+       priv->dev->stats.rx_packets++;
 }
 
 /* Test to see if the packet in card memory at packet_loc has a valid CRC
@@ -1031,7 +990,7 @@ static void frag_rx_path(struct atmel_private *priv,
                        crc = crc32_le(crc, &priv->rx_buf[12], msdu_size);
                        atmel_copy_to_host(priv->dev, (void *)&netcrc, rx_packet_loc + msdu_size, 4);
                        if ((crc ^ 0xffffffff) != netcrc) {
-                               priv->stats.rx_crc_errors++;
+                               priv->dev->stats.rx_crc_errors++;
                                memset(priv->frag_source, 0xff, 6);
                        }
                }
@@ -1049,7 +1008,7 @@ static void frag_rx_path(struct atmel_private *priv,
                                       msdu_size);
                        atmel_copy_to_host(priv->dev, (void *)&netcrc, rx_packet_loc + msdu_size, 4);
                        if ((crc ^ 0xffffffff) != netcrc) {
-                               priv->stats.rx_crc_errors++;
+                               priv->dev->stats.rx_crc_errors++;
                                memset(priv->frag_source, 0xff, 6);
                                more_frags = 1; /* don't send broken assembly */
                        }
@@ -1061,19 +1020,18 @@ static void frag_rx_path(struct atmel_private *priv,
                if (!more_frags) { /* last one */
                        memset(priv->frag_source, 0xff, 6);
                        if (!(skb = dev_alloc_skb(priv->frag_len + 14))) {
-                               priv->stats.rx_dropped++;
+                               priv->dev->stats.rx_dropped++;
                        } else {
                                skb_reserve(skb, 2);
                                memcpy(skb_put(skb, priv->frag_len + 12),
                                       priv->rx_buf,
                                       priv->frag_len + 12);
                                priv->dev->last_rx = jiffies;
-                               skb->dev = priv->dev;
                                skb->protocol = eth_type_trans(skb, priv->dev);
                                skb->ip_summed = CHECKSUM_NONE;
                                netif_rx(skb);
-                               priv->stats.rx_bytes += priv->frag_len + 12;
-                               priv->stats.rx_packets++;
+                               priv->dev->stats.rx_bytes += priv->frag_len + 12;
+                               priv->dev->stats.rx_packets++;
                        }
                }
        } else
@@ -1098,7 +1056,7 @@ static void rx_done_irq(struct atmel_private *priv)
                        if (status == 0xc1) /* determined by experiment */
                                priv->wstats.discard.nwid++;
                        else
-                               priv->stats.rx_errors++;
+                               priv->dev->stats.rx_errors++;
                        goto next;
                }
 
@@ -1106,7 +1064,7 @@ static void rx_done_irq(struct atmel_private *priv)
                rx_packet_loc = atmel_rmem16(priv, atmel_rx(priv, RX_DESC_MSDU_POS_OFFSET, priv->rx_desc_head));
 
                if (msdu_size < 30) {
-                       priv->stats.rx_errors++;
+                       priv->dev->stats.rx_errors++;
                        goto next;
                }
 
@@ -1164,7 +1122,7 @@ static void rx_done_irq(struct atmel_private *priv)
                                msdu_size -= 4;
                                crc = crc32_le(crc, (unsigned char *)&priv->rx_buf, msdu_size);
                                if ((crc ^ 0xffffffff) != (*((u32 *)&priv->rx_buf[msdu_size]))) {
-                                       priv->stats.rx_crc_errors++;
+                                       priv->dev->stats.rx_crc_errors++;
                                        goto next;
                                }
                        }
@@ -1184,7 +1142,7 @@ next:
        }
 }
 
-static irqreturn_t service_interrupt(int irq, void *dev_id, struct pt_regs *regs)
+static irqreturn_t service_interrupt(int irq, void *dev_id)
 {
        struct net_device *dev = (struct net_device *) dev_id;
        struct atmel_private *priv = netdev_priv(dev);
@@ -1232,7 +1190,7 @@ static irqreturn_t service_interrupt(int irq, void *dev_id, struct pt_regs *regs
 
                atmel_set_gcr(dev, GCR_ACKINT); /* acknowledge interrupt */
 
-               for (i = 0; i < sizeof(irq_order)/sizeof(u8); i++)
+               for (i = 0; i < ARRAY_SIZE(irq_order); i++)
                        if (isr & irq_order[i])
                                break;
 
@@ -1291,12 +1249,6 @@ static irqreturn_t service_interrupt(int irq, void *dev_id, struct pt_regs *regs
        }
 }
 
-static struct net_device_stats *atmel_get_stats(struct net_device *dev)
-{
-       struct atmel_private *priv = netdev_priv(dev);
-       return &priv->stats;
-}
-
 static struct iw_statistics *atmel_get_wireless_stats(struct net_device *dev)
 {
        struct atmel_private *priv = netdev_priv(dev);
@@ -1384,10 +1336,10 @@ int atmel_open(struct net_device *dev)
                atmel_set_mib8(priv, Phy_Mib_Type, PHY_MIB_REG_DOMAIN_POS, priv->reg_domain);
        } else {
                priv->reg_domain = atmel_get_mib8(priv, Phy_Mib_Type, PHY_MIB_REG_DOMAIN_POS);
-               for (i = 0; i < sizeof(channel_table)/sizeof(channel_table[0]); i++)
+               for (i = 0; i < ARRAY_SIZE(channel_table); i++)
                        if (priv->reg_domain == channel_table[i].reg_domain)
                                break;
-               if (i == sizeof(channel_table)/sizeof(channel_table[0])) {
+               if (i == ARRAY_SIZE(channel_table)) {
                        priv->reg_domain = REG_DOMAIN_MKK1;
                        printk(KERN_ALERT "%s: failed to get regulatory domain: assuming MKK1.\n", dev->name);
                }
@@ -1432,7 +1384,7 @@ static int atmel_validate_channel(struct atmel_private *priv, int channel)
           else return suitable default channel */
        int i;
 
-       for (i = 0; i < sizeof(channel_table)/sizeof(channel_table[0]); i++)
+       for (i = 0; i < ARRAY_SIZE(channel_table); i++)
                if (priv->reg_domain == channel_table[i].reg_domain) {
                        if (channel >= channel_table[i].min &&
                            channel <= channel_table[i].max)
@@ -1476,7 +1428,7 @@ static int atmel_proc_output (char *buf, struct atmel_private *priv)
                }
 
                r = "<unknown>";
-               for (i = 0; i < sizeof(channel_table)/sizeof(channel_table[0]); i++)
+               for (i = 0; i < ARRAY_SIZE(channel_table); i++)
                        if (priv->reg_domain == channel_table[i].reg_domain)
                                r = channel_table[i].name;
 
@@ -1526,6 +1478,7 @@ struct net_device *init_atmel_card(unsigned short irq, unsigned long port,
        struct net_device *dev;
        struct atmel_private *priv;
        int rc;
+       DECLARE_MAC_BUF(mac);
 
        /* Create the network device object. */
         dev = alloc_etherdev(sizeof(*priv));
@@ -1558,8 +1511,6 @@ struct net_device *init_atmel_card(unsigned short irq, unsigned long port,
                priv->crc_ok_cnt = priv->crc_ko_cnt = 0;
        } else
                priv->probe_crc = 0;
-       memset(&priv->stats, 0, sizeof(priv->stats));
-       memset(&priv->wstats, 0, sizeof(priv->wstats));
        priv->last_qual = jiffies;
        priv->last_beacon_timestamp = 0;
        memset(priv->frag_source, 0xff, sizeof(priv->frag_source));
@@ -1608,7 +1559,6 @@ struct net_device *init_atmel_card(unsigned short irq, unsigned long port,
        dev->change_mtu = atmel_change_mtu;
        dev->set_mac_address = atmel_set_mac_address;
        dev->hard_start_xmit = start_tx;
-       dev->get_stats = atmel_get_stats;
        dev->wireless_handlers = (struct iw_handler_def *)&atmel_handler_def;
        dev->do_ioctl = atmel_ioctl;
        dev->irq = irq;
@@ -1616,7 +1566,7 @@ struct net_device *init_atmel_card(unsigned short irq, unsigned long port,
 
        SET_NETDEV_DEV(dev, sys_dev);
 
-       if ((rc = request_irq(dev->irq, service_interrupt, SA_SHIRQ, dev->name, dev))) {
+       if ((rc = request_irq(dev->irq, service_interrupt, IRQF_SHARED, dev->name, dev))) {
                printk(KERN_ERR "%s: register interrupt %d failed, rc %d\n", dev->name, irq, rc);
                goto err_out_free;
        }
@@ -1640,12 +1590,9 @@ struct net_device *init_atmel_card(unsigned short irq, unsigned long port,
        if (!ent)
                printk(KERN_WARNING "atmel: unable to create /proc entry.\n");
 
-       printk(KERN_INFO "%s: Atmel at76c50x. Version %d.%d. MAC %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
-              dev->name, DRIVER_MAJOR, DRIVER_MINOR,
-              dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
-              dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5] );
+       printk(KERN_INFO "%s: Atmel at76c50x. Version %d.%d. MAC %s\n",
+              dev->name, DRIVER_MAJOR, DRIVER_MINOR, print_mac(mac, dev->dev_addr));
 
-       SET_MODULE_OWNER(dev);
        return dev;
 
 err_out_res:
@@ -1695,13 +1642,13 @@ static int atmel_set_essid(struct net_device *dev,
                priv->connect_to_any_BSS = 0;
 
                /* Check the size of the string */
-               if (dwrq->length > MAX_SSID_LENGTH + 1)
+               if (dwrq->length > MAX_SSID_LENGTH)
                         return -E2BIG;
                if (index != 0)
                        return -EINVAL;
 
-               memcpy(priv->new_SSID, extra, dwrq->length - 1);
-               priv->new_SSID_size = dwrq->length - 1;
+               memcpy(priv->new_SSID, extra, dwrq->length);
+               priv->new_SSID_size = dwrq->length;
        }
 
        return -EINPROGRESS;
@@ -1717,12 +1664,10 @@ static int atmel_get_essid(struct net_device *dev,
        /* Get the current SSID */
        if (priv->new_SSID_size != 0) {
                memcpy(extra, priv->new_SSID, priv->new_SSID_size);
-               extra[priv->new_SSID_size] = '\0';
-               dwrq->length = priv->new_SSID_size + 1;
+               dwrq->length = priv->new_SSID_size;
        } else {
                memcpy(extra, priv->SSID, priv->SSID_size);
-               extra[priv->SSID_size] = '\0';
-               dwrq->length = priv->SSID_size + 1;
+               dwrq->length = priv->SSID_size;
        }
 
        dwrq->flags = !priv->connect_to_any_BSS; /* active */
@@ -1777,7 +1722,7 @@ static int atmel_set_encode(struct net_device *dev,
                                /* Disable the key */
                                priv->wep_key_len[index] = 0;
                /* Check if the key is not marked as invalid */
-               if(!(dwrq->flags & IW_ENCODE_NOKEY)) {
+               if (!(dwrq->flags & IW_ENCODE_NOKEY)) {
                        /* Cleanup */
                        memset(priv->wep_keys[index], 0, 13);
                        /* Copy the key in the driver */
@@ -1805,9 +1750,8 @@ static int atmel_set_encode(struct net_device *dev,
                        priv->default_key = index;
                } else
                        /* Don't complain if only change the mode */
-                       if (!dwrq->flags & IW_ENCODE_MODE) {
+                       if (!(dwrq->flags & IW_ENCODE_MODE))
                                return -EINVAL;
-                       }
        }
        /* Read the flags */
        if (dwrq->flags & IW_ENCODE_DISABLED) {
@@ -1872,7 +1816,7 @@ static int atmel_set_encodeext(struct net_device *dev,
        struct atmel_private *priv = netdev_priv(dev);
        struct iw_point *encoding = &wrqu->encoding;
        struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
-       int idx, key_len;
+       int idx, key_len, alg = ext->alg, set_key = 1;
 
        /* Determine and validate the key index */
        idx = encoding->flags & IW_ENCODE_INDEX;
@@ -1883,39 +1827,42 @@ static int atmel_set_encodeext(struct net_device *dev,
        } else
                idx = priv->default_key;
 
-       if ((encoding->flags & IW_ENCODE_DISABLED) ||
-           ext->alg == IW_ENCODE_ALG_NONE) {
-               priv->wep_is_on = 0;
-               priv->encryption_level = 0;
-               priv->pairwise_cipher_suite = CIPHER_SUITE_NONE;
-       }
+       if (encoding->flags & IW_ENCODE_DISABLED)
+           alg = IW_ENCODE_ALG_NONE;
 
-       if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
+       if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
                priv->default_key = idx;
+               set_key = ext->key_len > 0 ? 1 : 0;
+       }
 
-       /* Set the requested key */
-       switch (ext->alg) {
-       case IW_ENCODE_ALG_NONE:
-               break;
-       case IW_ENCODE_ALG_WEP:
-               if (ext->key_len > 5) {
-                       priv->wep_key_len[idx] = 13;
-                       priv->pairwise_cipher_suite = CIPHER_SUITE_WEP_128;
-                       priv->encryption_level = 2;
-               } else if (ext->key_len > 0) {
-                       priv->wep_key_len[idx] = 5;
-                       priv->pairwise_cipher_suite = CIPHER_SUITE_WEP_64;
-                       priv->encryption_level = 1;
-               } else {
+       if (set_key) {
+               /* Set the requested key first */
+               switch (alg) {
+               case IW_ENCODE_ALG_NONE:
+                       priv->wep_is_on = 0;
+                       priv->encryption_level = 0;
+                       priv->pairwise_cipher_suite = CIPHER_SUITE_NONE;
+                       break;
+               case IW_ENCODE_ALG_WEP:
+                       if (ext->key_len > 5) {
+                               priv->wep_key_len[idx] = 13;
+                               priv->pairwise_cipher_suite = CIPHER_SUITE_WEP_128;
+                               priv->encryption_level = 2;
+                       } else if (ext->key_len > 0) {
+                               priv->wep_key_len[idx] = 5;
+                               priv->pairwise_cipher_suite = CIPHER_SUITE_WEP_64;
+                               priv->encryption_level = 1;
+                       } else {
+                               return -EINVAL;
+                       }
+                       priv->wep_is_on = 1;
+                       memset(priv->wep_keys[idx], 0, 13);
+                       key_len = min ((int)ext->key_len, priv->wep_key_len[idx]);
+                       memcpy(priv->wep_keys[idx], ext->key, key_len);
+                       break;
+               default:
                        return -EINVAL;
                }
-               priv->wep_is_on = 1;
-               memset(priv->wep_keys[idx], 0, 13);
-               key_len = min ((int)ext->key_len, priv->wep_key_len[idx]);
-               memcpy(priv->wep_keys[idx], ext->key, key_len);
-               break;
-       default:
-               return -EINVAL;
        }
 
        return -EINPROGRESS;
@@ -1945,7 +1892,7 @@ static int atmel_get_encodeext(struct net_device *dev,
 
        encoding->flags = idx + 1;
        memset(ext, 0, sizeof(*ext));
-       
+
        if (!priv->wep_is_on) {
                ext->alg = IW_ENCODE_ALG_NONE;
                ext->key_len = 0;
@@ -2156,9 +2103,9 @@ static int atmel_set_retry(struct net_device *dev,
        struct atmel_private *priv = netdev_priv(dev);
 
        if (!vwrq->disabled && (vwrq->flags & IW_RETRY_LIMIT)) {
-               if (vwrq->flags & IW_RETRY_MAX)
+               if (vwrq->flags & IW_RETRY_LONG)
                        priv->long_retry = vwrq->value;
-               else if (vwrq->flags & IW_RETRY_MIN)
+               else if (vwrq->flags & IW_RETRY_SHORT)
                        priv->short_retry = vwrq->value;
                else {
                        /* No modifier : set both */
@@ -2180,15 +2127,15 @@ static int atmel_get_retry(struct net_device *dev,
 
        vwrq->disabled = 0;      /* Can't be disabled */
 
-       /* Note : by default, display the min retry number */
-       if (vwrq->flags & IW_RETRY_MAX) {
-               vwrq->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
+       /* Note : by default, display the short retry number */
+       if (vwrq->flags & IW_RETRY_LONG) {
+               vwrq->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
                vwrq->value = priv->long_retry;
        } else {
                vwrq->flags = IW_RETRY_LIMIT;
                vwrq->value = priv->short_retry;
                if (priv->long_retry != priv->short_retry)
-                       vwrq->flags |= IW_RETRY_MIN;
+                       vwrq->flags |= IW_RETRY_SHORT;
        }
 
        return 0;
@@ -2327,7 +2274,7 @@ static int atmel_set_scan(struct net_device *dev,
                return -EAGAIN;
 
        /* Timeout old surveys. */
-       if ((jiffies - priv->last_survey) > (20 * HZ))
+       if (time_after(jiffies, priv->last_survey + 20 * HZ))
                priv->site_survey_state = SITE_SURVEY_IDLE;
        priv->last_survey = jiffies;
 
@@ -2363,23 +2310,41 @@ static int atmel_get_scan(struct net_device *dev,
                iwe.cmd = SIOCGIWAP;
                iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
                memcpy(iwe.u.ap_addr.sa_data, priv->BSSinfo[i].BSSID, 6);
-               current_ev = iwe_stream_add_event(current_ev, extra + IW_SCAN_MAX_DATA, &iwe, IW_EV_ADDR_LEN);
+               current_ev = iwe_stream_add_event(info, current_ev,
+                                                 extra + IW_SCAN_MAX_DATA,
+                                                 &iwe, IW_EV_ADDR_LEN);
 
                iwe.u.data.length =  priv->BSSinfo[i].SSIDsize;
                if (iwe.u.data.length > 32)
                        iwe.u.data.length = 32;
                iwe.cmd = SIOCGIWESSID;
                iwe.u.data.flags = 1;
-               current_ev = iwe_stream_add_point(current_ev, extra + IW_SCAN_MAX_DATA, &iwe, priv->BSSinfo[i].SSID);
+               current_ev = iwe_stream_add_point(info, current_ev,
+                                                 extra + IW_SCAN_MAX_DATA,
+                                                 &iwe, priv->BSSinfo[i].SSID);
 
                iwe.cmd = SIOCGIWMODE;
                iwe.u.mode = priv->BSSinfo[i].BSStype;
-               current_ev = iwe_stream_add_event(current_ev, extra + IW_SCAN_MAX_DATA, &iwe, IW_EV_UINT_LEN);
+               current_ev = iwe_stream_add_event(info, current_ev,
+                                                 extra + IW_SCAN_MAX_DATA,
+                                                 &iwe, IW_EV_UINT_LEN);
 
                iwe.cmd = SIOCGIWFREQ;
                iwe.u.freq.m = priv->BSSinfo[i].channel;
                iwe.u.freq.e = 0;
-               current_ev = iwe_stream_add_event(current_ev, extra + IW_SCAN_MAX_DATA, &iwe, IW_EV_FREQ_LEN);
+               current_ev = iwe_stream_add_event(info, current_ev,
+                                                 extra + IW_SCAN_MAX_DATA,
+                                                 &iwe, IW_EV_FREQ_LEN);
+
+               /* Add quality statistics */
+               iwe.cmd = IWEVQUAL;
+               iwe.u.qual.level = priv->BSSinfo[i].RSSI;
+               iwe.u.qual.qual  = iwe.u.qual.level;
+               /* iwe.u.qual.noise  = SOMETHING */
+               current_ev = iwe_stream_add_event(info, current_ev,
+                                                 extra + IW_SCAN_MAX_DATA,
+                                                 &iwe, IW_EV_QUAL_LEN);
+
 
                iwe.cmd = SIOCGIWENCODE;
                if (priv->BSSinfo[i].UsingWEP)
@@ -2387,7 +2352,9 @@ static int atmel_get_scan(struct net_device *dev,
                else
                        iwe.u.data.flags = IW_ENCODE_DISABLED;
                iwe.u.data.length = 0;
-               current_ev = iwe_stream_add_point(current_ev, extra + IW_SCAN_MAX_DATA, &iwe, NULL);
+               current_ev = iwe_stream_add_point(info, current_ev,
+                                                 extra + IW_SCAN_MAX_DATA,
+                                                 &iwe, NULL);
        }
 
        /* Length of data */
@@ -2411,7 +2378,7 @@ static int atmel_get_range(struct net_device *dev,
        range->min_nwid = 0x0000;
        range->max_nwid = 0x0000;
        range->num_channels = 0;
-       for (j = 0; j < sizeof(channel_table)/sizeof(channel_table[0]); j++)
+       for (j = 0; j < ARRAY_SIZE(channel_table); j++)
                if (priv->reg_domain == channel_table[j].reg_domain) {
                        range->num_channels = channel_table[j].max - channel_table[j].min + 1;
                        break;
@@ -2617,9 +2584,9 @@ static const struct iw_priv_args atmel_private_args[] = {
 
 static const struct iw_handler_def atmel_handler_def =
 {
-       .num_standard   = sizeof(atmel_handler)/sizeof(iw_handler),
-       .num_private    = sizeof(atmel_private_handler)/sizeof(iw_handler),
-       .num_private_args = sizeof(atmel_private_args)/sizeof(struct iw_priv_args),
+       .num_standard   = ARRAY_SIZE(atmel_handler),
+       .num_private    = ARRAY_SIZE(atmel_private_handler),
+       .num_private_args = ARRAY_SIZE(atmel_private_args),
        .standard       = (iw_handler *) atmel_handler,
        .private        = (iw_handler *) atmel_private_handler,
        .private_args   = (struct iw_priv_args *) atmel_private_args,
@@ -2683,7 +2650,7 @@ static int atmel_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
 
                domain[REGDOMAINSZ] = 0;
                rc = -EINVAL;
-               for (i = 0; i < sizeof(channel_table)/sizeof(channel_table[0]); i++) {
+               for (i = 0; i < ARRAY_SIZE(channel_table); i++) {
                        /* strcasecmp doesn't exist in the library */
                        char *a = channel_table[i].name;
                        char *b = domain;
@@ -2711,9 +2678,9 @@ static int atmel_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
 }
 
 struct auth_body {
-       u16 alg;
-       u16 trans_seq;
-       u16 status;
+       __le16 alg;
+       __le16 trans_seq;
+       __le16 status;
        u8 el_id;
        u8 chall_text_len;
        u8 chall_text[253];
@@ -2748,9 +2715,9 @@ static void atmel_scan(struct atmel_private *priv, int specific_ssid)
                u8 SSID[MAX_SSID_LENGTH];
                u8 scan_type;
                u8 channel;
-               u16 BSS_type;
-               u16 min_channel_time;
-               u16 max_channel_time;
+               __le16 BSS_type;
+               __le16 min_channel_time;
+               __le16 max_channel_time;
                u8 options;
                u8 SSID_size;
        } cmd;
@@ -2793,7 +2760,7 @@ static void join(struct atmel_private *priv, int type)
                u8 SSID[MAX_SSID_LENGTH];
                u8 BSS_type; /* this is a short in a scan command - weird */
                u8 channel;
-               u16 timeout;
+               __le16 timeout;
                u8 SSID_size;
                u8 reserved;
        } cmd;
@@ -2832,7 +2799,7 @@ static void handle_beacon_probe(struct atmel_private *priv, u16 capability,
                                u8 channel)
 {
        int rejoin = 0;
-       int new = capability  & C80211_MGMT_CAPABILITY_ShortPreamble ?
+       int new = capability & MFIE_TYPE_POWER_CONSTRAINT ?
                SHORT_PREAMBLE : LONG_PREAMBLE;
 
        if (priv->preamble != new) {
@@ -2898,8 +2865,8 @@ static void send_association_request(struct atmel_private *priv, int is_reassoc)
        int bodysize;
        struct ieee80211_hdr_4addr header;
        struct ass_req_format {
-               u16 capability;
-               u16 listen_interval;
+               __le16 capability;
+               __le16 listen_interval;
                u8 ap[6]; /* nothing after here directly accessible */
                u8 ssid_el_id;
                u8 ssid_len;
@@ -2918,11 +2885,11 @@ static void send_association_request(struct atmel_private *priv, int is_reassoc)
        memcpy(header.addr2, priv->dev->dev_addr, 6);
        memcpy(header.addr3, priv->CurrentBSSID, 6);
 
-       body.capability = cpu_to_le16(C80211_MGMT_CAPABILITY_ESS);
+       body.capability = cpu_to_le16(WLAN_CAPABILITY_ESS);
        if (priv->wep_is_on)
-               body.capability |= cpu_to_le16(C80211_MGMT_CAPABILITY_Privacy);
+               body.capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
        if (priv->preamble == SHORT_PREAMBLE)
-               body.capability |= cpu_to_le16(C80211_MGMT_CAPABILITY_ShortPreamble);
+               body.capability |= cpu_to_le16(MFIE_TYPE_POWER_CONSTRAINT);
 
        body.listen_interval = cpu_to_le16(priv->listen_interval * priv->beacon_period);
 
@@ -2936,10 +2903,10 @@ static void send_association_request(struct atmel_private *priv, int is_reassoc)
                bodysize = 12 + priv->SSID_size;
        }
 
-       ssid_el_p[0] = C80211_MGMT_ElementID_SSID;
+       ssid_el_p[0] = MFIE_TYPE_SSID;
        ssid_el_p[1] = priv->SSID_size;
        memcpy(ssid_el_p + 2, priv->SSID, priv->SSID_size);
-       ssid_el_p[2 + priv->SSID_size] = C80211_MGMT_ElementID_SupportedRates;
+       ssid_el_p[2 + priv->SSID_size] = MFIE_TYPE_RATES;
        ssid_el_p[3 + priv->SSID_size] = 4; /* len of suported rates */
        memcpy(ssid_el_p + 4 + priv->SSID_size, atmel_basic_rates, 4);
 
@@ -3001,7 +2968,7 @@ static void store_bss_info(struct atmel_private *priv,
                           u16 beacon_period, u8 channel, u8 rssi, u8 ssid_len,
                           u8 *ssid, int is_beacon)
 {
-       u8 *bss = capability & C80211_MGMT_CAPABILITY_ESS ? header->addr2 : header->addr3;
+       u8 *bss = capability & WLAN_CAPABILITY_ESS ? header->addr2 : header->addr3;
        int i, index;
 
        for (index = -1, i = 0; i < priv->BSS_list_entries; i++)
@@ -3027,16 +2994,16 @@ static void store_bss_info(struct atmel_private *priv,
 
        priv->BSSinfo[index].channel = channel;
        priv->BSSinfo[index].beacon_period = beacon_period;
-       priv->BSSinfo[index].UsingWEP = capability & C80211_MGMT_CAPABILITY_Privacy;
+       priv->BSSinfo[index].UsingWEP = capability & WLAN_CAPABILITY_PRIVACY;
        memcpy(priv->BSSinfo[index].SSID, ssid, ssid_len);
        priv->BSSinfo[index].SSIDsize = ssid_len;
 
-       if (capability & C80211_MGMT_CAPABILITY_IBSS)
+       if (capability & WLAN_CAPABILITY_IBSS)
                priv->BSSinfo[index].BSStype = IW_MODE_ADHOC;
-       else if (capability & C80211_MGMT_CAPABILITY_ESS)
+       else if (capability & WLAN_CAPABILITY_ESS)
                priv->BSSinfo[index].BSStype =IW_MODE_INFRA;
 
-       priv->BSSinfo[index].preamble = capability & C80211_MGMT_CAPABILITY_ShortPreamble ?
+       priv->BSSinfo[index].preamble = capability & MFIE_TYPE_POWER_CONSTRAINT ?
                SHORT_PREAMBLE : LONG_PREAMBLE;
 }
 
@@ -3047,7 +3014,7 @@ static void authenticate(struct atmel_private *priv, u16 frame_len)
        u16 trans_seq_no = le16_to_cpu(auth->trans_seq);
        u16 system = le16_to_cpu(auth->alg);
 
-       if (status == C80211_MGMT_SC_Success && !priv->wep_is_on) {
+       if (status == WLAN_STATUS_SUCCESS && !priv->wep_is_on) {
                /* no WEP */
                if (priv->station_was_associated) {
                        atmel_enter_state(priv, STATION_STATE_REASSOCIATING);
@@ -3060,18 +3027,27 @@ static void authenticate(struct atmel_private *priv, u16 frame_len)
                }
        }
 
-       if (status == C80211_MGMT_SC_Success && priv->wep_is_on) {
+       if (status == WLAN_STATUS_SUCCESS && priv->wep_is_on) {
+               int should_associate = 0;
                /* WEP */
                if (trans_seq_no != priv->ExpectedAuthentTransactionSeqNum)
                        return;
 
-               if (trans_seq_no == 0x0002 &&
-                   auth->el_id == C80211_MGMT_ElementID_ChallengeText) {
-                       send_authentication_request(priv, system, auth->chall_text, auth->chall_text_len);
-                       return;
+               if (system == WLAN_AUTH_OPEN) {
+                       if (trans_seq_no == 0x0002) {
+                               should_associate = 1;
+                       }
+               } else if (system == WLAN_AUTH_SHARED_KEY) {
+                       if (trans_seq_no == 0x0002 &&
+                           auth->el_id == MFIE_TYPE_CHALLENGE) {
+                               send_authentication_request(priv, system, auth->chall_text, auth->chall_text_len);
+                               return;
+                       } else if (trans_seq_no == 0x0004) {
+                               should_associate = 1;
+                       }
                }
 
-               if (trans_seq_no == 0x0004) {
+               if (should_associate) {
                        if(priv->station_was_associated) {
                                atmel_enter_state(priv, STATION_STATE_REASSOCIATING);
                                send_association_request(priv, 1);
@@ -3084,11 +3060,13 @@ static void authenticate(struct atmel_private *priv, u16 frame_len)
                }
        }
 
-       if (status == C80211_MGMT_SC_AuthAlgNotSupported) {
+       if (status == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
                /* Do opensystem first, then try sharedkey */
-               if (system ==  C80211_MGMT_AAN_OPENSYSTEM) {
+               if (system == WLAN_AUTH_OPEN) {
                        priv->CurrentAuthentTransactionSeqNum = 0x001;
-                       send_authentication_request(priv, C80211_MGMT_AAN_SHAREDKEY, NULL, 0);
+                       priv->exclude_unencrypted = 1;
+                       send_authentication_request(priv, WLAN_AUTH_SHARED_KEY, NULL, 0);
+                       return;
                } else if (priv->connect_to_any_BSS) {
                        int bss_index;
 
@@ -3109,9 +3087,9 @@ static void authenticate(struct atmel_private *priv, u16 frame_len)
 static void associate(struct atmel_private *priv, u16 frame_len, u16 subtype)
 {
        struct ass_resp_format {
-               u16 capability;
-               u16 status;
-               u16 ass_id;
+               __le16 capability;
+               __le16 status;
+               __le16 ass_id;
                u8 el_id;
                u8 length;
                u8 rates[4];
@@ -3126,8 +3104,8 @@ static void associate(struct atmel_private *priv, u16 frame_len, u16 subtype)
        if (frame_len < 8 + rates_len)
                return;
 
-       if (status == C80211_MGMT_SC_Success) {
-               if (subtype == C80211_SUBTYPE_MGMT_ASS_RESPONSE)
+       if (status == WLAN_STATUS_SUCCESS) {
+               if (subtype == IEEE80211_STYPE_ASSOC_RESP)
                        priv->AssociationRequestRetryCnt = 0;
                else
                        priv->ReAssociationRequestRetryCnt = 0;
@@ -3164,9 +3142,9 @@ static void associate(struct atmel_private *priv, u16 frame_len, u16 subtype)
                return;
        }
 
-       if (subtype == C80211_SUBTYPE_MGMT_ASS_RESPONSE &&
-           status != C80211_MGMT_SC_AssDeniedBSSRate &&
-           status != C80211_MGMT_SC_SupportCapabilities &&
+       if (subtype == IEEE80211_STYPE_ASSOC_RESP &&
+           status != WLAN_STATUS_ASSOC_DENIED_RATES &&
+           status != WLAN_STATUS_CAPS_UNSUPPORTED &&
            priv->AssociationRequestRetryCnt < MAX_ASSOCIATION_RETRIES) {
                mod_timer(&priv->management_timer, jiffies + MGMT_JIFFIES);
                priv->AssociationRequestRetryCnt++;
@@ -3174,9 +3152,9 @@ static void associate(struct atmel_private *priv, u16 frame_len, u16 subtype)
                return;
        }
 
-       if (subtype == C80211_SUBTYPE_MGMT_REASS_RESPONSE &&
-           status != C80211_MGMT_SC_AssDeniedBSSRate &&
-           status != C80211_MGMT_SC_SupportCapabilities &&
+       if (subtype == IEEE80211_STYPE_REASSOC_RESP &&
+           status != WLAN_STATUS_ASSOC_DENIED_RATES &&
+           status != WLAN_STATUS_CAPS_UNSUPPORTED &&
            priv->AssociationRequestRetryCnt < MAX_ASSOCIATION_RETRIES) {
                mod_timer(&priv->management_timer, jiffies + MGMT_JIFFIES);
                priv->ReAssociationRequestRetryCnt++;
@@ -3311,16 +3289,16 @@ static void atmel_management_frame(struct atmel_private *priv,
 
        subtype = le16_to_cpu(header->frame_ctl) & IEEE80211_FCTL_STYPE;
        switch (subtype) {
-       case C80211_SUBTYPE_MGMT_BEACON:
-       case C80211_SUBTYPE_MGMT_ProbeResponse:
+       case IEEE80211_STYPE_BEACON:
+       case IEEE80211_STYPE_PROBE_RESP:
 
                /* beacon frame has multiple variable-length fields -
                   never let an engineer loose with a data structure design. */
                {
                        struct beacon_format {
-                               u64 timestamp;
-                               u16 interval;
-                               u16 capability;
+                               __le64 timestamp;
+                               __le16 interval;
+                               __le16 capability;
                                u8 ssid_el_id;
                                u8 ssid_length;
                                /* ssid here */
@@ -3370,19 +3348,19 @@ static void atmel_management_frame(struct atmel_private *priv,
                                               beacon_interval, channel, rssi,
                                               ssid_length,
                                               &beacon->rates_el_id,
-                                              subtype == C80211_SUBTYPE_MGMT_BEACON);
+                                              subtype == IEEE80211_STYPE_BEACON);
                }
                break;
 
-       case C80211_SUBTYPE_MGMT_Authentication:
+       case IEEE80211_STYPE_AUTH:
 
                if (priv->station_state == STATION_STATE_AUTHENTICATING)
                        authenticate(priv, frame_len);
 
                break;
 
-       case C80211_SUBTYPE_MGMT_ASS_RESPONSE:
-       case C80211_SUBTYPE_MGMT_REASS_RESPONSE:
+       case IEEE80211_STYPE_ASSOC_RESP:
+       case IEEE80211_STYPE_REASSOC_RESP:
 
                if (priv->station_state == STATION_STATE_ASSOCIATING ||
                    priv->station_state == STATION_STATE_REASSOCIATING)
@@ -3390,7 +3368,7 @@ static void atmel_management_frame(struct atmel_private *priv,
 
                break;
 
-       case C80211_SUBTYPE_MGMT_DISASSOSIATION:
+       case IEEE80211_STYPE_DISASSOC:
                if (priv->station_is_associated &&
                    priv->operating_mode == IW_MODE_INFRA &&
                    is_frame_from_current_bss(priv, header)) {
@@ -3403,7 +3381,7 @@ static void atmel_management_frame(struct atmel_private *priv,
 
                break;
 
-       case C80211_SUBTYPE_MGMT_Deauthentication:
+       case IEEE80211_STYPE_DEAUTH:
                if (priv->operating_mode == IW_MODE_INFRA &&
                    is_frame_from_current_bss(priv, header)) {
                        priv->station_was_associated = 0;
@@ -3439,10 +3417,13 @@ static void atmel_management_timer(u_long a)
                        priv->AuthenticationRequestRetryCnt = 0;
                        restart_search(priv);
                } else {
+                       int auth = WLAN_AUTH_OPEN;
                        priv->AuthenticationRequestRetryCnt++;
                        priv->CurrentAuthentTransactionSeqNum = 0x0001;
                        mod_timer(&priv->management_timer, jiffies + MGMT_JIFFIES);
-                       send_authentication_request(priv, C80211_MGMT_AAN_OPENSYSTEM, NULL, 0);
+                       if (priv->wep_is_on && priv->exclude_unencrypted)
+                               auth = WLAN_AUTH_SHARED_KEY;
+                       send_authentication_request(priv, auth, NULL, 0);
          }
          break;
 
@@ -3484,6 +3465,7 @@ static void atmel_command_irq(struct atmel_private *priv)
        u8 status = atmel_rmem8(priv, atmel_co(priv, CMD_BLOCK_STATUS_OFFSET));
        u8 command = atmel_rmem8(priv, atmel_co(priv, CMD_BLOCK_COMMAND_OFFSET));
        int fast_scan;
+       union iwreq_data wrqu;
 
        if (status == CMD_STATUS_IDLE ||
            status == CMD_STATUS_IN_PROGRESS)
@@ -3508,6 +3490,7 @@ static void atmel_command_irq(struct atmel_private *priv)
                        atmel_scan(priv, 1);
                } else {
                        int bss_index = retrieve_bss(priv);
+                       int notify_scan_complete = 1;
                        if (bss_index != -1) {
                                atmel_join_bss(priv, bss_index);
                        } else if (priv->operating_mode == IW_MODE_ADHOC &&
@@ -3516,8 +3499,14 @@ static void atmel_command_irq(struct atmel_private *priv)
                        } else {
                                priv->fast_scan = !fast_scan;
                                atmel_scan(priv, 1);
+                               notify_scan_complete = 0;
                        }
                        priv->site_survey_state = SITE_SURVEY_COMPLETED;
+                       if (notify_scan_complete) {
+                               wrqu.data.length = 0;
+                               wrqu.data.flags = 0;
+                               wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
+                       }
                }
                break;
 
@@ -3530,6 +3519,9 @@ static void atmel_command_irq(struct atmel_private *priv)
                priv->site_survey_state = SITE_SURVEY_COMPLETED;
                if (priv->station_is_associated) {
                        atmel_enter_state(priv, STATION_STATE_READY);
+                       wrqu.data.length = 0;
+                       wrqu.data.flags = 0;
+                       wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
                } else {
                        atmel_scan(priv, 1);
                }
@@ -3541,12 +3533,15 @@ static void atmel_command_irq(struct atmel_private *priv)
                                priv->station_was_associated = priv->station_is_associated;
                                atmel_enter_state(priv, STATION_STATE_READY);
                        } else {
+                               int auth = WLAN_AUTH_OPEN;
                                priv->AuthenticationRequestRetryCnt = 0;
                                atmel_enter_state(priv, STATION_STATE_AUTHENTICATING);
 
                                mod_timer(&priv->management_timer, jiffies + MGMT_JIFFIES);
                                priv->CurrentAuthentTransactionSeqNum = 0x0001;
-                               send_authentication_request(priv, C80211_MGMT_AAN_SHAREDKEY, NULL, 0);
+                               if (priv->wep_is_on && priv->exclude_unencrypted)
+                                       auth = WLAN_AUTH_SHARED_KEY;
+                               send_authentication_request(priv, auth, NULL, 0);
                        }
                        return;
                }