rtl8180: add software-based support for IBSS mode
[safe/jmp/linux-2.6] / drivers / net / wireless / rtl818x / rtl8180_dev.c
index 47521c5..cbdc5cf 100644 (file)
@@ -33,7 +33,7 @@ MODULE_AUTHOR("Andrea Merello <andreamrl@tiscali.it>");
 MODULE_DESCRIPTION("RTL8180 / RTL8185 PCI wireless driver");
 MODULE_LICENSE("GPL");
 
-static struct pci_device_id rtl8180_table[] __devinitdata = {
+static DEFINE_PCI_DEVICE_TABLE(rtl8180_table) = {
        /* rtl8185 */
        { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8185) },
        { PCI_DEVICE(PCI_VENDOR_ID_BELKIN, 0x700f) },
@@ -82,8 +82,6 @@ static const struct ieee80211_channel rtl818x_channels[] = {
 };
 
 
-
-
 void rtl8180_write_phy(struct ieee80211_hw *dev, u8 addr, u32 data)
 {
        struct rtl8180_priv *priv = dev->priv;
@@ -132,7 +130,6 @@ static void rtl8180_handle_rx(struct ieee80211_hw *dev)
 
                        rx_status.antenna = (flags2 >> 15) & 1;
                        /* TODO: improve signal/rssi reporting */
-                       rx_status.qual = flags2 & 0xFF;
                        rx_status.signal = (flags2 >> 8) & 0x7F;
                        /* XXX: is this correct? */
                        rx_status.rate_idx = (flags >> 20) & 0xF;
@@ -190,6 +187,7 @@ static void rtl8180_handle_tx(struct ieee80211_hw *dev, unsigned int prio)
                        info->flags |= IEEE80211_TX_STAT_ACK;
 
                info->status.rates[0].count = (flags & 0xFF) + 1;
+               info->status.rates[1].idx = -1;
 
                ieee80211_tx_status_irqsafe(dev, skb);
                if (ring->entries - skb_queue_len(&ring->queue) == 2)
@@ -235,6 +233,7 @@ static irqreturn_t rtl8180_interrupt(int irq, void *dev_id)
 static int rtl8180_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
 {
        struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
+       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
        struct rtl8180_priv *priv = dev->priv;
        struct rtl8180_tx_ring *ring;
        struct rtl8180_tx_desc *entry;
@@ -281,11 +280,19 @@ static int rtl8180_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
                                (ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
                remainder = (16 * (skb->len + 4)) %
                            ((ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
-               if (remainder > 0 && remainder <= 6)
+               if (remainder <= 6)
                        plcp_len |= 1 << 15;
        }
 
        spin_lock_irqsave(&priv->lock, flags);
+
+       if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
+               if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
+                       priv->seqno += 0x10;
+               hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
+               hdr->seq_ctrl |= cpu_to_le16(priv->seqno);
+       }
+
        idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
        entry = &ring->desc[idx];
 
@@ -299,7 +306,8 @@ static int rtl8180_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
        entry->flags = cpu_to_le32(tx_flags);
        __skb_queue_tail(&ring->queue, skb);
        if (ring->entries - skb_queue_len(&ring->queue) < 2)
-               ieee80211_stop_queue(dev, skb_get_queue_mapping(skb));
+               ieee80211_stop_queue(dev, prio);
+
        spin_unlock_irqrestore(&priv->lock, flags);
 
        rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING, (1 << (prio + 4)));
@@ -548,7 +556,7 @@ static int rtl8180_start(struct ieee80211_hw *dev)
        rtl818x_iowrite32(priv, &priv->map->TNPDA, priv->tx_ring[1].dma);
        rtl818x_iowrite32(priv, &priv->map->TLPDA, priv->tx_ring[0].dma);
 
-       ret = request_irq(priv->pdev->irq, &rtl8180_interrupt,
+       ret = request_irq(priv->pdev->irq, rtl8180_interrupt,
                          IRQF_SHARED, KBUILD_MODNAME, dev);
        if (ret) {
                printk(KERN_ERR "%s: failed to register IRQ handler\n",
@@ -616,7 +624,6 @@ static int rtl8180_start(struct ieee80211_hw *dev)
        reg |= RTL818X_CMD_TX_ENABLE;
        rtl818x_iowrite8(priv, &priv->map->CMD, reg);
 
-       priv->mode = NL80211_IFTYPE_MONITOR;
        return 0;
 
  err_free_rings:
@@ -634,8 +641,6 @@ static void rtl8180_stop(struct ieee80211_hw *dev)
        u8 reg;
        int i;
 
-       priv->mode = NL80211_IFTYPE_UNSPECIFIED;
-
        rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
 
        reg = rtl818x_ioread8(priv, &priv->map->CMD);
@@ -657,39 +662,96 @@ static void rtl8180_stop(struct ieee80211_hw *dev)
                rtl8180_free_tx_ring(dev, i);
 }
 
+static u64 rtl8180_get_tsf(struct ieee80211_hw *dev)
+{
+       struct rtl8180_priv *priv = dev->priv;
+
+       return rtl818x_ioread32(priv, &priv->map->TSFT[0]) |
+              (u64)(rtl818x_ioread32(priv, &priv->map->TSFT[1])) << 32;
+}
+
+void rtl8180_beacon_work(struct work_struct *work)
+{
+       struct rtl8180_vif *vif_priv =
+               container_of(work, struct rtl8180_vif, beacon_work.work);
+       struct ieee80211_vif *vif =
+               container_of((void *)vif_priv, struct ieee80211_vif, drv_priv);
+       struct ieee80211_hw *dev = vif_priv->dev;
+       struct ieee80211_mgmt *mgmt;
+       struct sk_buff *skb;
+       int err = 0;
+
+       /* don't overflow the tx ring */
+       if (ieee80211_queue_stopped(dev, 0))
+               goto resched;
+
+       /* grab a fresh beacon */
+       skb = ieee80211_beacon_get(dev, vif);
+
+       /*
+        * update beacon timestamp w/ TSF value
+        * TODO: make hardware update beacon timestamp
+        */
+       mgmt = (struct ieee80211_mgmt *)skb->data;
+       mgmt->u.beacon.timestamp = cpu_to_le64(rtl8180_get_tsf(dev));
+
+       /* TODO: use actual beacon queue */
+       skb_set_queue_mapping(skb, 0);
+
+       err = rtl8180_tx(dev, skb);
+       WARN_ON(err);
+
+resched:
+       /*
+        * schedule next beacon
+        * TODO: use hardware support for beacon timing
+        */
+       schedule_delayed_work(&vif_priv->beacon_work,
+                       usecs_to_jiffies(1024 * vif->bss_conf.beacon_int));
+}
+
 static int rtl8180_add_interface(struct ieee80211_hw *dev,
-                                struct ieee80211_if_init_conf *conf)
+                                struct ieee80211_vif *vif)
 {
        struct rtl8180_priv *priv = dev->priv;
+       struct rtl8180_vif *vif_priv;
 
-       if (priv->mode != NL80211_IFTYPE_MONITOR)
-               return -EOPNOTSUPP;
+       /*
+        * We only support one active interface at a time.
+        */
+       if (priv->vif)
+               return -EBUSY;
 
-       switch (conf->type) {
+       switch (vif->type) {
        case NL80211_IFTYPE_STATION:
-               priv->mode = conf->type;
+       case NL80211_IFTYPE_ADHOC:
                break;
        default:
                return -EOPNOTSUPP;
        }
 
-       priv->vif = conf->vif;
+       priv->vif = vif;
+
+       /* Initialize driver private area */
+       vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
+       vif_priv->dev = dev;
+       INIT_DELAYED_WORK(&vif_priv->beacon_work, rtl8180_beacon_work);
+       vif_priv->enable_beacon = false;
 
        rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
        rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->MAC[0],
-                         le32_to_cpu(*(__le32 *)conf->mac_addr));
+                         le32_to_cpu(*(__le32 *)vif->addr));
        rtl818x_iowrite16(priv, (__le16 __iomem *)&priv->map->MAC[4],
-                         le16_to_cpu(*(__le16 *)(conf->mac_addr + 4)));
+                         le16_to_cpu(*(__le16 *)(vif->addr + 4)));
        rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
 
        return 0;
 }
 
 static void rtl8180_remove_interface(struct ieee80211_hw *dev,
-                                    struct ieee80211_if_init_conf *conf)
+                                    struct ieee80211_vif *vif)
 {
        struct rtl8180_priv *priv = dev->priv;
-       priv->mode = NL80211_IFTYPE_MONITOR;
        priv->vif = NULL;
 }
 
@@ -709,8 +771,11 @@ static void rtl8180_bss_info_changed(struct ieee80211_hw *dev,
                                     u32 changed)
 {
        struct rtl8180_priv *priv = dev->priv;
+       struct rtl8180_vif *vif_priv;
        int i;
 
+       vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
+
        if (changed & BSS_CHANGED_BSSID) {
                for (i = 0; i < ETH_ALEN; i++)
                        rtl818x_iowrite8(priv, &priv->map->BSSID[i],
@@ -725,13 +790,28 @@ static void rtl8180_bss_info_changed(struct ieee80211_hw *dev,
        }
 
        if (changed & BSS_CHANGED_ERP_SLOT && priv->rf->conf_erp)
-               priv->rf->conf_erp(dev, info);
+               priv->rf->conf_erp(dev, info);
+
+       if (changed & BSS_CHANGED_BEACON_ENABLED)
+               vif_priv->enable_beacon = info->enable_beacon;
+
+       if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON)) {
+               cancel_delayed_work_sync(&vif_priv->beacon_work);
+               if (vif_priv->enable_beacon)
+                       schedule_work(&vif_priv->beacon_work.work);
+       }
+}
+
+static u64 rtl8180_prepare_multicast(struct ieee80211_hw *dev, int mc_count,
+                                    struct dev_addr_list *mc_list)
+{
+       return mc_count;
 }
 
 static void rtl8180_configure_filter(struct ieee80211_hw *dev,
                                     unsigned int changed_flags,
                                     unsigned int *total_flags,
-                                    int mc_count, struct dev_addr_list *mclist)
+                                    u64 multicast)
 {
        struct rtl8180_priv *priv = dev->priv;
 
@@ -741,7 +821,7 @@ static void rtl8180_configure_filter(struct ieee80211_hw *dev,
                priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
        if (changed_flags & FIF_OTHER_BSS)
                priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
-       if (*total_flags & FIF_ALLMULTI || mc_count > 0)
+       if (*total_flags & FIF_ALLMULTI || multicast > 0)
                priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
        else
                priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
@@ -768,7 +848,9 @@ static const struct ieee80211_ops rtl8180_ops = {
        .remove_interface       = rtl8180_remove_interface,
        .config                 = rtl8180_config,
        .bss_info_changed       = rtl8180_bss_info_changed,
+       .prepare_multicast      = rtl8180_prepare_multicast,
        .configure_filter       = rtl8180_configure_filter,
+       .get_tsf                = rtl8180_get_tsf,
 };
 
 static void rtl8180_eeprom_register_read(struct eeprom_93cx6 *eeprom)
@@ -815,6 +897,7 @@ static int __devinit rtl8180_probe(struct pci_dev *pdev,
        const char *chip_name, *rf_name = NULL;
        u32 reg;
        u16 eeprom_val;
+       u8 mac_addr[ETH_ALEN];
 
        err = pci_enable_device(pdev);
        if (err) {
@@ -893,7 +976,9 @@ static int __devinit rtl8180_probe(struct pci_dev *pdev,
        dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
                     IEEE80211_HW_RX_INCLUDES_FCS |
                     IEEE80211_HW_SIGNAL_UNSPEC;
-       dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
+       dev->vif_data_size = sizeof(struct rtl8180_vif);
+       dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
+                                       BIT(NL80211_IFTYPE_ADHOC);
        dev->queues = 1;
        dev->max_signal = 65;
 
@@ -975,12 +1060,13 @@ static int __devinit rtl8180_probe(struct pci_dev *pdev,
                eeprom_93cx6_read(&eeprom, 0x19, &priv->rfparam);
        }
 
-       eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)dev->wiphy->perm_addr, 3);
-       if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
+       eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)mac_addr, 3);
+       if (!is_valid_ether_addr(mac_addr)) {
                printk(KERN_WARNING "%s (rtl8180): Invalid hwaddr! Using"
                       " randomly generated MAC addr\n", pci_name(pdev));
-               random_ether_addr(dev->wiphy->perm_addr);
+               random_ether_addr(mac_addr);
        }
+       SET_IEEE80211_PERM_ADDR(dev, mac_addr);
 
        /* CCK TX power */
        for (i = 0; i < 14; i += 2) {
@@ -1012,7 +1098,7 @@ static int __devinit rtl8180_probe(struct pci_dev *pdev,
        }
 
        printk(KERN_INFO "%s: hwaddr %pM, %s + %s\n",
-              wiphy_name(dev->wiphy), dev->wiphy->perm_addr,
+              wiphy_name(dev->wiphy), mac_addr,
               chip_name, priv->rf->name);
 
        return 0;