* Copyright (c) 2004 Balint Seeber <n0_5p4m_p13453@hotmail.com>
  * Copyright (c) 2007 Guido Guenther <agx@sigxcpu.org>
  * Copyright (c) 2007 Kalle Valo <kalle.valo@iki.fi>
+ * Copyright (c) 2010 Sebastian Smolorz <sesmo@gmx.net>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
        return 0;
 }
 
+static void at76_work_join_bssid(struct work_struct *work)
+{
+       struct at76_priv *priv = container_of(work, struct at76_priv,
+                                             work_join_bssid);
+
+       if (priv->device_unplugged)
+               return;
+
+       mutex_lock(&priv->mtx);
+
+       if (is_valid_ether_addr(priv->bssid))
+               at76_join(priv);
+
+       mutex_unlock(&priv->mtx);
+}
+
 static void at76_mac80211_tx_callback(struct urb *urb)
 {
        struct at76_priv *priv = urb->context;
        struct at76_priv *priv = hw->priv;
        struct at76_tx_buffer *tx_buffer = priv->bulk_out_buffer;
        struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
+       struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
        int padding, submit_len, ret;
 
        at76_dbg(DBG_MAC80211, "%s()", __func__);
                return NETDEV_TX_BUSY;
        }
 
+       /* The following code lines are important when the device is going to
+        * authenticate with a new bssid. The driver must send CMD_JOIN before
+        * an authentication frame is transmitted. For this to succeed, the
+        * correct bssid of the AP must be known. As mac80211 does not inform
+        * drivers about the bssid prior to the authentication process the
+        * following workaround is necessary. If the TX frame is an
+        * authentication frame extract the bssid and send the CMD_JOIN. */
+       if (mgmt->frame_control & cpu_to_le16(IEEE80211_STYPE_AUTH)) {
+               if (compare_ether_addr(priv->bssid, mgmt->bssid)) {
+                       memcpy(priv->bssid, mgmt->bssid, ETH_ALEN);
+                       ieee80211_queue_work(hw, &priv->work_join_bssid);
+                       return NETDEV_TX_BUSY;
+               }
+       }
+
        ieee80211_stop_queues(hw);
 
        at76_ledtrig_tx_activity();     /* tell ledtrigger we send a packet */
        at76_dbg(DBG_MAC80211, "%s()", __func__);
 
        cancel_delayed_work(&priv->dwork_hw_scan);
+       cancel_work_sync(&priv->work_join_bssid);
        cancel_work_sync(&priv->work_set_promisc);
 
        mutex_lock(&priv->mtx);
        mutex_init(&priv->mtx);
        INIT_WORK(&priv->work_set_promisc, at76_work_set_promisc);
        INIT_WORK(&priv->work_submit_rx, at76_work_submit_rx);
+       INIT_WORK(&priv->work_join_bssid, at76_work_join_bssid);
        INIT_DELAYED_WORK(&priv->dwork_hw_scan, at76_dwork_hw_scan);
 
        tasklet_init(&priv->rx_tasklet, at76_rx_tasklet, 0);
 MODULE_AUTHOR("Pavel Roskin <proski@gnu.org>");
 MODULE_AUTHOR("Guido Guenther <agx@sigxcpu.org>");
 MODULE_AUTHOR("Kalle Valo <kalle.valo@iki.fi>");
+MODULE_AUTHOR("Sebastian Smolorz <sesmo@gmx.net>");
 MODULE_DESCRIPTION(DRIVER_DESC);
 MODULE_LICENSE("GPL");