blob: 45d2cc14d71c59369eecc42f47ceb892178cdc42 [file] [log] [blame]
Michael Wuf6532112007-10-14 14:43:16 -04001
2/*
3 * Linux device driver for RTL8180 / RTL8185
4 *
5 * Copyright 2007 Michael Wu <flamingice@sourmilk.net>
Andrea Merello93ba2a82013-08-26 13:53:30 +02006 * Copyright 2007 Andrea Merello <andrea.merello@gmail.com>
Michael Wuf6532112007-10-14 14:43:16 -04007 *
8 * Based on the r8180 driver, which is:
Andrea Merello93ba2a82013-08-26 13:53:30 +02009 * Copyright 2004-2005 Andrea Merello <andrea.merello@gmail.com>, et al.
Michael Wuf6532112007-10-14 14:43:16 -040010 *
11 * Thanks to Realtek for their support!
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 */
17
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000018#include <linux/interrupt.h>
Michael Wuf6532112007-10-14 14:43:16 -040019#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Michael Wuf6532112007-10-14 14:43:16 -040021#include <linux/delay.h>
22#include <linux/etherdevice.h>
23#include <linux/eeprom_93cx6.h>
Paul Gortmaker9d9779e2011-07-03 15:21:01 -040024#include <linux/module.h>
Michael Wuf6532112007-10-14 14:43:16 -040025#include <net/mac80211.h>
26
27#include "rtl8180.h"
John W. Linville3cfeb0c2010-12-20 15:16:53 -050028#include "rtl8225.h"
29#include "sa2400.h"
30#include "max2820.h"
31#include "grf5101.h"
Michael Wuf6532112007-10-14 14:43:16 -040032
33MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
Andrea Merello93ba2a82013-08-26 13:53:30 +020034MODULE_AUTHOR("Andrea Merello <andrea.merello@gmail.com>");
Michael Wuf6532112007-10-14 14:43:16 -040035MODULE_DESCRIPTION("RTL8180 / RTL8185 PCI wireless driver");
36MODULE_LICENSE("GPL");
37
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000038static DEFINE_PCI_DEVICE_TABLE(rtl8180_table) = {
Michael Wuf6532112007-10-14 14:43:16 -040039 /* rtl8185 */
40 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8185) },
Adrian Bassett4fcc5472008-01-23 16:38:33 +000041 { PCI_DEVICE(PCI_VENDOR_ID_BELKIN, 0x700f) },
Michael Wuf6532112007-10-14 14:43:16 -040042 { PCI_DEVICE(PCI_VENDOR_ID_BELKIN, 0x701f) },
43
44 /* rtl8180 */
45 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8180) },
46 { PCI_DEVICE(0x1799, 0x6001) },
47 { PCI_DEVICE(0x1799, 0x6020) },
48 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x3300) },
Xose Vazquez Perez29a6b502012-06-15 17:27:05 +020049 { PCI_DEVICE(0x1186, 0x3301) },
50 { PCI_DEVICE(0x1432, 0x7106) },
Michael Wuf6532112007-10-14 14:43:16 -040051 { }
52};
53
54MODULE_DEVICE_TABLE(pci, rtl8180_table);
55
Johannes Berg8318d782008-01-24 19:38:38 +010056static const struct ieee80211_rate rtl818x_rates[] = {
57 { .bitrate = 10, .hw_value = 0, },
58 { .bitrate = 20, .hw_value = 1, },
59 { .bitrate = 55, .hw_value = 2, },
60 { .bitrate = 110, .hw_value = 3, },
61 { .bitrate = 60, .hw_value = 4, },
62 { .bitrate = 90, .hw_value = 5, },
63 { .bitrate = 120, .hw_value = 6, },
64 { .bitrate = 180, .hw_value = 7, },
65 { .bitrate = 240, .hw_value = 8, },
66 { .bitrate = 360, .hw_value = 9, },
67 { .bitrate = 480, .hw_value = 10, },
68 { .bitrate = 540, .hw_value = 11, },
69};
70
71static const struct ieee80211_channel rtl818x_channels[] = {
72 { .center_freq = 2412 },
73 { .center_freq = 2417 },
74 { .center_freq = 2422 },
75 { .center_freq = 2427 },
76 { .center_freq = 2432 },
77 { .center_freq = 2437 },
78 { .center_freq = 2442 },
79 { .center_freq = 2447 },
80 { .center_freq = 2452 },
81 { .center_freq = 2457 },
82 { .center_freq = 2462 },
83 { .center_freq = 2467 },
84 { .center_freq = 2472 },
85 { .center_freq = 2484 },
86};
87
88
Michael Wuf6532112007-10-14 14:43:16 -040089void rtl8180_write_phy(struct ieee80211_hw *dev, u8 addr, u32 data)
90{
91 struct rtl8180_priv *priv = dev->priv;
92 int i = 10;
93 u32 buf;
94
95 buf = (data << 8) | addr;
96
97 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->PHY[0], buf | 0x80);
98 while (i--) {
99 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->PHY[0], buf);
100 if (rtl818x_ioread8(priv, &priv->map->PHY[2]) == (data & 0xFF))
101 return;
102 }
103}
104
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400105static void rtl8180_handle_rx(struct ieee80211_hw *dev)
Michael Wuf6532112007-10-14 14:43:16 -0400106{
107 struct rtl8180_priv *priv = dev->priv;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400108 unsigned int count = 32;
John W. Linville8b73fb82010-07-21 16:26:40 -0400109 u8 signal, agc, sq;
andrea.merello2b4db052014-02-05 22:38:05 +0100110 dma_addr_t mapping;
Michael Wuf6532112007-10-14 14:43:16 -0400111
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400112 while (count--) {
Michael Wuf6532112007-10-14 14:43:16 -0400113 struct rtl8180_rx_desc *entry = &priv->rx_ring[priv->rx_idx];
114 struct sk_buff *skb = priv->rx_buf[priv->rx_idx];
115 u32 flags = le32_to_cpu(entry->flags);
116
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300117 if (flags & RTL818X_RX_DESC_FLAG_OWN)
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400118 return;
Michael Wuf6532112007-10-14 14:43:16 -0400119
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300120 if (unlikely(flags & (RTL818X_RX_DESC_FLAG_DMA_FAIL |
121 RTL818X_RX_DESC_FLAG_FOF |
122 RTL818X_RX_DESC_FLAG_RX_ERR)))
Michael Wuf6532112007-10-14 14:43:16 -0400123 goto done;
124 else {
125 u32 flags2 = le32_to_cpu(entry->flags2);
126 struct ieee80211_rx_status rx_status = {0};
127 struct sk_buff *new_skb = dev_alloc_skb(MAX_RX_SIZE);
128
129 if (unlikely(!new_skb))
130 goto done;
131
andrea.merello2b4db052014-02-05 22:38:05 +0100132 mapping = pci_map_single(priv->pdev,
133 skb_tail_pointer(new_skb),
134 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
135
136 if (pci_dma_mapping_error(priv->pdev, mapping)) {
137 kfree_skb(new_skb);
138 dev_err(&priv->pdev->dev, "RX DMA map error\n");
139
140 goto done;
141 }
142
Michael Wuf6532112007-10-14 14:43:16 -0400143 pci_unmap_single(priv->pdev,
144 *((dma_addr_t *)skb->cb),
145 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
146 skb_put(skb, flags & 0xFFF);
147
148 rx_status.antenna = (flags2 >> 15) & 1;
Johannes Berg8318d782008-01-24 19:38:38 +0100149 rx_status.rate_idx = (flags >> 20) & 0xF;
John W. Linville8b73fb82010-07-21 16:26:40 -0400150 agc = (flags2 >> 17) & 0x7F;
151 if (priv->r8185) {
152 if (rx_status.rate_idx > 3)
153 signal = 90 - clamp_t(u8, agc, 25, 90);
154 else
155 signal = 95 - clamp_t(u8, agc, 30, 95);
156 } else {
157 sq = flags2 & 0xff;
158 signal = priv->rf->calc_rssi(agc, sq);
159 }
John W. Linville8b749642010-07-19 16:35:20 -0400160 rx_status.signal = signal;
Karl Beldan675a0b02013-03-25 16:26:57 +0100161 rx_status.freq = dev->conf.chandef.chan->center_freq;
162 rx_status.band = dev->conf.chandef.chan->band;
Michael Wuf6532112007-10-14 14:43:16 -0400163 rx_status.mactime = le64_to_cpu(entry->tsft);
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800164 rx_status.flag |= RX_FLAG_MACTIME_START;
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300165 if (flags & RTL818X_RX_DESC_FLAG_CRC32_ERR)
Michael Wuf6532112007-10-14 14:43:16 -0400166 rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
167
Johannes Bergf1d58c22009-06-17 13:13:00 +0200168 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400169 ieee80211_rx_irqsafe(dev, skb);
Michael Wuf6532112007-10-14 14:43:16 -0400170
171 skb = new_skb;
172 priv->rx_buf[priv->rx_idx] = skb;
andrea.merello2b4db052014-02-05 22:38:05 +0100173 *((dma_addr_t *) skb->cb) = mapping;
Michael Wuf6532112007-10-14 14:43:16 -0400174 }
175
176 done:
177 entry->rx_buf = cpu_to_le32(*((dma_addr_t *)skb->cb));
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300178 entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
Michael Wuf6532112007-10-14 14:43:16 -0400179 MAX_RX_SIZE);
180 if (priv->rx_idx == 31)
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300181 entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
Michael Wuf6532112007-10-14 14:43:16 -0400182 priv->rx_idx = (priv->rx_idx + 1) % 32;
183 }
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400184}
Michael Wuf6532112007-10-14 14:43:16 -0400185
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400186static void rtl8180_handle_tx(struct ieee80211_hw *dev, unsigned int prio)
187{
188 struct rtl8180_priv *priv = dev->priv;
189 struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
Michael Wuf6532112007-10-14 14:43:16 -0400190
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400191 while (skb_queue_len(&ring->queue)) {
192 struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
193 struct sk_buff *skb;
194 struct ieee80211_tx_info *info;
195 u32 flags = le32_to_cpu(entry->flags);
196
197 if (flags & RTL818X_TX_DESC_FLAG_OWN)
198 return;
199
200 ring->idx = (ring->idx + 1) % ring->entries;
201 skb = __skb_dequeue(&ring->queue);
202 pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
203 skb->len, PCI_DMA_TODEVICE);
204
205 info = IEEE80211_SKB_CB(skb);
206 ieee80211_tx_info_clear_status(info);
207
208 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
209 (flags & RTL818X_TX_DESC_FLAG_TX_OK))
210 info->flags |= IEEE80211_TX_STAT_ACK;
211
212 info->status.rates[0].count = (flags & 0xFF) + 1;
213 info->status.rates[1].idx = -1;
214
215 ieee80211_tx_status_irqsafe(dev, skb);
216 if (ring->entries - skb_queue_len(&ring->queue) == 2)
217 ieee80211_wake_queue(dev, prio);
Michael Wuf6532112007-10-14 14:43:16 -0400218 }
219}
220
221static irqreturn_t rtl8180_interrupt(int irq, void *dev_id)
222{
223 struct ieee80211_hw *dev = dev_id;
224 struct rtl8180_priv *priv = dev->priv;
225 u16 reg;
226
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400227 spin_lock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400228 reg = rtl818x_ioread16(priv, &priv->map->INT_STATUS);
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400229 if (unlikely(reg == 0xFFFF)) {
230 spin_unlock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400231 return IRQ_HANDLED;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400232 }
Michael Wuf6532112007-10-14 14:43:16 -0400233
234 rtl818x_iowrite16(priv, &priv->map->INT_STATUS, reg);
235
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400236 if (reg & (RTL818X_INT_TXB_OK | RTL818X_INT_TXB_ERR))
237 rtl8180_handle_tx(dev, 3);
Michael Wuf6532112007-10-14 14:43:16 -0400238
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400239 if (reg & (RTL818X_INT_TXH_OK | RTL818X_INT_TXH_ERR))
240 rtl8180_handle_tx(dev, 2);
241
242 if (reg & (RTL818X_INT_TXN_OK | RTL818X_INT_TXN_ERR))
243 rtl8180_handle_tx(dev, 1);
244
245 if (reg & (RTL818X_INT_TXL_OK | RTL818X_INT_TXL_ERR))
246 rtl8180_handle_tx(dev, 0);
247
248 if (reg & (RTL818X_INT_RX_OK | RTL818X_INT_RX_ERR))
249 rtl8180_handle_rx(dev);
250
251 spin_unlock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400252
253 return IRQ_HANDLED;
254}
255
Thomas Huehn36323f82012-07-23 21:33:42 +0200256static void rtl8180_tx(struct ieee80211_hw *dev,
257 struct ieee80211_tx_control *control,
258 struct sk_buff *skb)
Michael Wuf6532112007-10-14 14:43:16 -0400259{
Johannes Berge039fa42008-05-15 12:55:29 +0200260 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
John W. Linville51e080d2010-05-06 16:26:23 -0400261 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Michael Wuf6532112007-10-14 14:43:16 -0400262 struct rtl8180_priv *priv = dev->priv;
263 struct rtl8180_tx_ring *ring;
264 struct rtl8180_tx_desc *entry;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400265 unsigned long flags;
Michael Wuf6532112007-10-14 14:43:16 -0400266 unsigned int idx, prio;
267 dma_addr_t mapping;
268 u32 tx_flags;
Johannes Berge6a98542008-10-21 12:40:02 +0200269 u8 rc_flags;
Michael Wuf6532112007-10-14 14:43:16 -0400270 u16 plcp_len = 0;
271 __le16 rts_duration = 0;
272
Johannes Berge2530082008-05-17 00:57:14 +0200273 prio = skb_get_queue_mapping(skb);
Michael Wuf6532112007-10-14 14:43:16 -0400274 ring = &priv->tx_ring[prio];
275
276 mapping = pci_map_single(priv->pdev, skb->data,
277 skb->len, PCI_DMA_TODEVICE);
278
andrea.merello348f7d42014-02-05 22:38:06 +0100279 if (pci_dma_mapping_error(priv->pdev, mapping)) {
280 kfree_skb(skb);
281 dev_err(&priv->pdev->dev, "TX DMA mapping error\n");
282 return;
283
284 }
285
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300286 tx_flags = RTL818X_TX_DESC_FLAG_OWN | RTL818X_TX_DESC_FLAG_FS |
287 RTL818X_TX_DESC_FLAG_LS |
Johannes Berge039fa42008-05-15 12:55:29 +0200288 (ieee80211_get_tx_rate(dev, info)->hw_value << 24) |
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200289 skb->len;
Michael Wuf6532112007-10-14 14:43:16 -0400290
291 if (priv->r8185)
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300292 tx_flags |= RTL818X_TX_DESC_FLAG_DMA |
293 RTL818X_TX_DESC_FLAG_NO_ENC;
Michael Wuf6532112007-10-14 14:43:16 -0400294
Johannes Berge6a98542008-10-21 12:40:02 +0200295 rc_flags = info->control.rates[0].flags;
296 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300297 tx_flags |= RTL818X_TX_DESC_FLAG_RTS;
Johannes Berge039fa42008-05-15 12:55:29 +0200298 tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
Johannes Berge6a98542008-10-21 12:40:02 +0200299 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300300 tx_flags |= RTL818X_TX_DESC_FLAG_CTS;
Johannes Berge039fa42008-05-15 12:55:29 +0200301 tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
Johannes Bergaa68cbf2008-02-18 14:20:30 +0100302 }
Michael Wuf6532112007-10-14 14:43:16 -0400303
Johannes Berge6a98542008-10-21 12:40:02 +0200304 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS)
Johannes Berg32bfd352007-12-19 01:31:26 +0100305 rts_duration = ieee80211_rts_duration(dev, priv->vif, skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +0200306 info);
Michael Wuf6532112007-10-14 14:43:16 -0400307
308 if (!priv->r8185) {
309 unsigned int remainder;
310
311 plcp_len = DIV_ROUND_UP(16 * (skb->len + 4),
Johannes Berge039fa42008-05-15 12:55:29 +0200312 (ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
Michael Wuf6532112007-10-14 14:43:16 -0400313 remainder = (16 * (skb->len + 4)) %
Johannes Berge039fa42008-05-15 12:55:29 +0200314 ((ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
Roel Kluin35a0ace2009-06-22 17:42:21 +0200315 if (remainder <= 6)
Michael Wuf6532112007-10-14 14:43:16 -0400316 plcp_len |= 1 << 15;
317 }
318
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400319 spin_lock_irqsave(&priv->lock, flags);
John W. Linville51e080d2010-05-06 16:26:23 -0400320
321 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
322 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
323 priv->seqno += 0x10;
324 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
325 hdr->seq_ctrl |= cpu_to_le16(priv->seqno);
326 }
327
Michael Wuf6532112007-10-14 14:43:16 -0400328 idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
329 entry = &ring->desc[idx];
330
331 entry->rts_duration = rts_duration;
332 entry->plcp_len = cpu_to_le16(plcp_len);
333 entry->tx_buf = cpu_to_le32(mapping);
334 entry->frame_len = cpu_to_le32(skb->len);
Johannes Berge6a98542008-10-21 12:40:02 +0200335 entry->flags2 = info->control.rates[1].idx >= 0 ?
Felix Fietkau870abdf2008-10-05 18:04:24 +0200336 ieee80211_get_alt_retry_rate(dev, info, 0)->bitrate << 4 : 0;
Johannes Berge6a98542008-10-21 12:40:02 +0200337 entry->retry_limit = info->control.rates[0].count;
andrea merello4c552a52014-02-18 02:10:45 +0100338
339 /* We must be sure that tx_flags is written last because the HW
340 * looks at it to check if the rest of data is valid or not
341 */
342 wmb();
Michael Wuf6532112007-10-14 14:43:16 -0400343 entry->flags = cpu_to_le32(tx_flags);
344 __skb_queue_tail(&ring->queue, skb);
345 if (ring->entries - skb_queue_len(&ring->queue) < 2)
John W. Linvilled10e2e02010-04-27 16:57:38 -0400346 ieee80211_stop_queue(dev, prio);
John W. Linville51e080d2010-05-06 16:26:23 -0400347
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400348 spin_unlock_irqrestore(&priv->lock, flags);
Michael Wuf6532112007-10-14 14:43:16 -0400349
350 rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING, (1 << (prio + 4)));
Michael Wuf6532112007-10-14 14:43:16 -0400351}
352
353void rtl8180_set_anaparam(struct rtl8180_priv *priv, u32 anaparam)
354{
355 u8 reg;
356
357 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
358 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
359 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
360 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
361 rtl818x_iowrite32(priv, &priv->map->ANAPARAM, anaparam);
362 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
363 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
364 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
365}
366
367static int rtl8180_init_hw(struct ieee80211_hw *dev)
368{
369 struct rtl8180_priv *priv = dev->priv;
370 u16 reg;
371
372 rtl818x_iowrite8(priv, &priv->map->CMD, 0);
373 rtl818x_ioread8(priv, &priv->map->CMD);
374 msleep(10);
375
376 /* reset */
377 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
378 rtl818x_ioread8(priv, &priv->map->CMD);
379
380 reg = rtl818x_ioread8(priv, &priv->map->CMD);
381 reg &= (1 << 1);
382 reg |= RTL818X_CMD_RESET;
383 rtl818x_iowrite8(priv, &priv->map->CMD, RTL818X_CMD_RESET);
384 rtl818x_ioread8(priv, &priv->map->CMD);
385 msleep(200);
386
387 /* check success of reset */
388 if (rtl818x_ioread8(priv, &priv->map->CMD) & RTL818X_CMD_RESET) {
Joe Perchesc96c31e2010-07-26 14:39:58 -0700389 wiphy_err(dev->wiphy, "reset timeout!\n");
Michael Wuf6532112007-10-14 14:43:16 -0400390 return -ETIMEDOUT;
391 }
392
393 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
394 rtl818x_ioread8(priv, &priv->map->CMD);
395 msleep(200);
396
397 if (rtl818x_ioread8(priv, &priv->map->CONFIG3) & (1 << 3)) {
398 /* For cardbus */
399 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
400 reg |= 1 << 1;
401 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
402 reg = rtl818x_ioread16(priv, &priv->map->FEMR);
403 reg |= (1 << 15) | (1 << 14) | (1 << 4);
404 rtl818x_iowrite16(priv, &priv->map->FEMR, reg);
405 }
406
407 rtl818x_iowrite8(priv, &priv->map->MSR, 0);
408
409 if (!priv->r8185)
410 rtl8180_set_anaparam(priv, priv->anaparam);
411
412 rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
413 rtl818x_iowrite32(priv, &priv->map->TBDA, priv->tx_ring[3].dma);
414 rtl818x_iowrite32(priv, &priv->map->THPDA, priv->tx_ring[2].dma);
415 rtl818x_iowrite32(priv, &priv->map->TNPDA, priv->tx_ring[1].dma);
416 rtl818x_iowrite32(priv, &priv->map->TLPDA, priv->tx_ring[0].dma);
417
418 /* TODO: necessary? specs indicate not */
419 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
420 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
421 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg & ~(1 << 3));
422 if (priv->r8185) {
423 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
424 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg | (1 << 4));
425 }
426 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
427
428 /* TODO: set CONFIG5 for calibrating AGC on rtl8180 + philips radio? */
429
430 /* TODO: turn off hw wep on rtl8180 */
431
432 rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
433
434 if (priv->r8185) {
435 rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
436 rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
437 rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (8 << 4) | 0);
438
439 rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
440
441 /* TODO: set ClkRun enable? necessary? */
442 reg = rtl818x_ioread8(priv, &priv->map->GP_ENABLE);
443 rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, reg & ~(1 << 6));
444 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
445 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
446 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | (1 << 2));
447 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
448 } else {
449 rtl818x_iowrite16(priv, &priv->map->BRSR, 0x1);
450 rtl818x_iowrite8(priv, &priv->map->SECURITY, 0);
451
452 rtl818x_iowrite8(priv, &priv->map->PHY_DELAY, 0x6);
453 rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, 0x4C);
454 }
455
456 priv->rf->init(dev);
457 if (priv->r8185)
458 rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
459 return 0;
460}
461
462static int rtl8180_init_rx_ring(struct ieee80211_hw *dev)
463{
464 struct rtl8180_priv *priv = dev->priv;
465 struct rtl8180_rx_desc *entry;
466 int i;
467
468 priv->rx_ring = pci_alloc_consistent(priv->pdev,
469 sizeof(*priv->rx_ring) * 32,
470 &priv->rx_ring_dma);
471
472 if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -0700473 wiphy_err(dev->wiphy, "Cannot allocate RX ring\n");
Michael Wuf6532112007-10-14 14:43:16 -0400474 return -ENOMEM;
475 }
476
477 memset(priv->rx_ring, 0, sizeof(*priv->rx_ring) * 32);
478 priv->rx_idx = 0;
479
480 for (i = 0; i < 32; i++) {
481 struct sk_buff *skb = dev_alloc_skb(MAX_RX_SIZE);
482 dma_addr_t *mapping;
483 entry = &priv->rx_ring[i];
andrea merello4da18bb2014-02-18 02:10:43 +0100484 if (!skb) {
485 wiphy_err(dev->wiphy, "Cannot allocate RX skb\n");
486 return -ENOMEM;
487 }
Michael Wuf6532112007-10-14 14:43:16 -0400488 priv->rx_buf[i] = skb;
489 mapping = (dma_addr_t *)skb->cb;
490 *mapping = pci_map_single(priv->pdev, skb_tail_pointer(skb),
491 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
andrea merelloec1da082014-02-22 17:57:23 +0100492
493 if (pci_dma_mapping_error(priv->pdev, *mapping)) {
494 kfree_skb(skb);
495 wiphy_err(dev->wiphy, "Cannot map DMA for RX skb\n");
496 return -ENOMEM;
497 }
498
Michael Wuf6532112007-10-14 14:43:16 -0400499 entry->rx_buf = cpu_to_le32(*mapping);
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300500 entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
Michael Wuf6532112007-10-14 14:43:16 -0400501 MAX_RX_SIZE);
502 }
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300503 entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
Michael Wuf6532112007-10-14 14:43:16 -0400504 return 0;
505}
506
507static void rtl8180_free_rx_ring(struct ieee80211_hw *dev)
508{
509 struct rtl8180_priv *priv = dev->priv;
510 int i;
511
512 for (i = 0; i < 32; i++) {
513 struct sk_buff *skb = priv->rx_buf[i];
514 if (!skb)
515 continue;
516
517 pci_unmap_single(priv->pdev,
518 *((dma_addr_t *)skb->cb),
519 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
520 kfree_skb(skb);
521 }
522
523 pci_free_consistent(priv->pdev, sizeof(*priv->rx_ring) * 32,
524 priv->rx_ring, priv->rx_ring_dma);
525 priv->rx_ring = NULL;
526}
527
528static int rtl8180_init_tx_ring(struct ieee80211_hw *dev,
529 unsigned int prio, unsigned int entries)
530{
531 struct rtl8180_priv *priv = dev->priv;
532 struct rtl8180_tx_desc *ring;
533 dma_addr_t dma;
534 int i;
535
536 ring = pci_alloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
537 if (!ring || (unsigned long)ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -0700538 wiphy_err(dev->wiphy, "Cannot allocate TX ring (prio = %d)\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -0700539 prio);
Michael Wuf6532112007-10-14 14:43:16 -0400540 return -ENOMEM;
541 }
542
543 memset(ring, 0, sizeof(*ring)*entries);
544 priv->tx_ring[prio].desc = ring;
545 priv->tx_ring[prio].dma = dma;
546 priv->tx_ring[prio].idx = 0;
547 priv->tx_ring[prio].entries = entries;
548 skb_queue_head_init(&priv->tx_ring[prio].queue);
549
550 for (i = 0; i < entries; i++)
551 ring[i].next_tx_desc =
552 cpu_to_le32((u32)dma + ((i + 1) % entries) * sizeof(*ring));
553
554 return 0;
555}
556
557static void rtl8180_free_tx_ring(struct ieee80211_hw *dev, unsigned int prio)
558{
559 struct rtl8180_priv *priv = dev->priv;
560 struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
561
562 while (skb_queue_len(&ring->queue)) {
563 struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
564 struct sk_buff *skb = __skb_dequeue(&ring->queue);
565
566 pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
567 skb->len, PCI_DMA_TODEVICE);
Michael Wuf6532112007-10-14 14:43:16 -0400568 kfree_skb(skb);
569 ring->idx = (ring->idx + 1) % ring->entries;
570 }
571
572 pci_free_consistent(priv->pdev, sizeof(*ring->desc)*ring->entries,
573 ring->desc, ring->dma);
574 ring->desc = NULL;
575}
576
577static int rtl8180_start(struct ieee80211_hw *dev)
578{
579 struct rtl8180_priv *priv = dev->priv;
580 int ret, i;
581 u32 reg;
582
583 ret = rtl8180_init_rx_ring(dev);
584 if (ret)
585 return ret;
586
587 for (i = 0; i < 4; i++)
588 if ((ret = rtl8180_init_tx_ring(dev, i, 16)))
589 goto err_free_rings;
590
591 ret = rtl8180_init_hw(dev);
592 if (ret)
593 goto err_free_rings;
594
595 rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
596 rtl818x_iowrite32(priv, &priv->map->TBDA, priv->tx_ring[3].dma);
597 rtl818x_iowrite32(priv, &priv->map->THPDA, priv->tx_ring[2].dma);
598 rtl818x_iowrite32(priv, &priv->map->TNPDA, priv->tx_ring[1].dma);
599 rtl818x_iowrite32(priv, &priv->map->TLPDA, priv->tx_ring[0].dma);
600
Julia Lawallea31ba32009-11-18 08:26:02 +0000601 ret = request_irq(priv->pdev->irq, rtl8180_interrupt,
Michael Wuf6532112007-10-14 14:43:16 -0400602 IRQF_SHARED, KBUILD_MODNAME, dev);
603 if (ret) {
Joe Perches5db55842010-08-11 19:11:19 -0700604 wiphy_err(dev->wiphy, "failed to register IRQ handler\n");
Michael Wuf6532112007-10-14 14:43:16 -0400605 goto err_free_rings;
606 }
607
608 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
609
610 rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
611 rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
612
613 reg = RTL818X_RX_CONF_ONLYERLPKT |
614 RTL818X_RX_CONF_RX_AUTORESETPHY |
615 RTL818X_RX_CONF_MGMT |
616 RTL818X_RX_CONF_DATA |
617 (7 << 8 /* MAX RX DMA */) |
618 RTL818X_RX_CONF_BROADCAST |
619 RTL818X_RX_CONF_NICMAC;
620
621 if (priv->r8185)
622 reg |= RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2;
623 else {
624 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE1)
625 ? RTL818X_RX_CONF_CSDM1 : 0;
626 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE2)
627 ? RTL818X_RX_CONF_CSDM2 : 0;
628 }
629
630 priv->rx_conf = reg;
631 rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
632
633 if (priv->r8185) {
634 reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
andrea merello14c76152014-02-18 02:10:44 +0100635
636 /* CW is not on per-packet basis.
637 * in rtl8185 the CW_VALUE reg is used.
638 */
andrea merello6f7343d2014-01-21 20:16:43 +0100639 reg &= ~RTL818X_CW_CONF_PERPACKET_CW;
andrea merello14c76152014-02-18 02:10:44 +0100640 /* retry limit IS on per-packet basis.
641 * the short and long retry limit in TX_CONF
642 * reg are ignored
643 */
andrea merello6f7343d2014-01-21 20:16:43 +0100644 reg |= RTL818X_CW_CONF_PERPACKET_RETRY;
Michael Wuf6532112007-10-14 14:43:16 -0400645 rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
646
647 reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
andrea merello14c76152014-02-18 02:10:44 +0100648 /* TX antenna and TX gain are not on per-packet basis.
649 * TX Antenna is selected by ANTSEL reg (RX in BB regs).
650 * TX gain is selected with CCK_TX_AGC and OFDM_TX_AGC regs
651 */
andrea merello6f7343d2014-01-21 20:16:43 +0100652 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN;
653 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL;
Michael Wuf6532112007-10-14 14:43:16 -0400654 reg |= RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
655 rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
656
657 /* disable early TX */
658 rtl818x_iowrite8(priv, (u8 __iomem *)priv->map + 0xec, 0x3f);
659 }
660
661 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
662 reg |= (6 << 21 /* MAX TX DMA */) |
663 RTL818X_TX_CONF_NO_ICV;
664
665 if (priv->r8185)
666 reg &= ~RTL818X_TX_CONF_PROBE_DTS;
667 else
668 reg &= ~RTL818X_TX_CONF_HW_SEQNUM;
669
andrea merelloe74075a2014-02-18 02:10:40 +0100670 reg &= ~RTL818X_TX_CONF_DISCW;
671
Michael Wuf6532112007-10-14 14:43:16 -0400672 /* different meaning, same value on both rtl8185 and rtl8180 */
673 reg &= ~RTL818X_TX_CONF_SAT_HWPLCP;
674
675 rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
676
677 reg = rtl818x_ioread8(priv, &priv->map->CMD);
678 reg |= RTL818X_CMD_RX_ENABLE;
679 reg |= RTL818X_CMD_TX_ENABLE;
680 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
681
Michael Wuf6532112007-10-14 14:43:16 -0400682 return 0;
683
684 err_free_rings:
685 rtl8180_free_rx_ring(dev);
686 for (i = 0; i < 4; i++)
687 if (priv->tx_ring[i].desc)
688 rtl8180_free_tx_ring(dev, i);
689
690 return ret;
691}
692
693static void rtl8180_stop(struct ieee80211_hw *dev)
694{
695 struct rtl8180_priv *priv = dev->priv;
696 u8 reg;
697 int i;
698
Michael Wuf6532112007-10-14 14:43:16 -0400699 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
700
701 reg = rtl818x_ioread8(priv, &priv->map->CMD);
702 reg &= ~RTL818X_CMD_TX_ENABLE;
703 reg &= ~RTL818X_CMD_RX_ENABLE;
704 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
705
706 priv->rf->stop(dev);
707
708 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
709 reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
710 rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
711 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
712
713 free_irq(priv->pdev->irq, dev);
714
715 rtl8180_free_rx_ring(dev);
716 for (i = 0; i < 4; i++)
717 rtl8180_free_tx_ring(dev, i);
718}
719
Eliad Peller37a41b42011-09-21 14:06:11 +0300720static u64 rtl8180_get_tsf(struct ieee80211_hw *dev,
721 struct ieee80211_vif *vif)
John W. Linvillec809e862010-05-06 16:49:40 -0400722{
723 struct rtl8180_priv *priv = dev->priv;
724
725 return rtl818x_ioread32(priv, &priv->map->TSFT[0]) |
726 (u64)(rtl818x_ioread32(priv, &priv->map->TSFT[1])) << 32;
727}
728
John W. Linvillea3275e22010-06-24 11:08:37 -0400729static void rtl8180_beacon_work(struct work_struct *work)
John W. Linvillec809e862010-05-06 16:49:40 -0400730{
731 struct rtl8180_vif *vif_priv =
732 container_of(work, struct rtl8180_vif, beacon_work.work);
733 struct ieee80211_vif *vif =
734 container_of((void *)vif_priv, struct ieee80211_vif, drv_priv);
735 struct ieee80211_hw *dev = vif_priv->dev;
736 struct ieee80211_mgmt *mgmt;
737 struct sk_buff *skb;
John W. Linvillec809e862010-05-06 16:49:40 -0400738
739 /* don't overflow the tx ring */
740 if (ieee80211_queue_stopped(dev, 0))
741 goto resched;
742
743 /* grab a fresh beacon */
744 skb = ieee80211_beacon_get(dev, vif);
John W. Linville8f1d2d22010-08-05 13:46:27 -0400745 if (!skb)
746 goto resched;
John W. Linvillec809e862010-05-06 16:49:40 -0400747
748 /*
749 * update beacon timestamp w/ TSF value
750 * TODO: make hardware update beacon timestamp
751 */
752 mgmt = (struct ieee80211_mgmt *)skb->data;
Eliad Peller37a41b42011-09-21 14:06:11 +0300753 mgmt->u.beacon.timestamp = cpu_to_le64(rtl8180_get_tsf(dev, vif));
John W. Linvillec809e862010-05-06 16:49:40 -0400754
755 /* TODO: use actual beacon queue */
756 skb_set_queue_mapping(skb, 0);
757
Thomas Huehn36323f82012-07-23 21:33:42 +0200758 rtl8180_tx(dev, NULL, skb);
John W. Linvillec809e862010-05-06 16:49:40 -0400759
760resched:
761 /*
762 * schedule next beacon
763 * TODO: use hardware support for beacon timing
764 */
765 schedule_delayed_work(&vif_priv->beacon_work,
766 usecs_to_jiffies(1024 * vif->bss_conf.beacon_int));
767}
768
Michael Wuf6532112007-10-14 14:43:16 -0400769static int rtl8180_add_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100770 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -0400771{
772 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -0400773 struct rtl8180_vif *vif_priv;
Michael Wuf6532112007-10-14 14:43:16 -0400774
John W. Linville643aab62009-12-22 18:13:04 -0500775 /*
776 * We only support one active interface at a time.
777 */
778 if (priv->vif)
779 return -EBUSY;
Michael Wuf6532112007-10-14 14:43:16 -0400780
Johannes Berg1ed32e42009-12-23 13:15:45 +0100781 switch (vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200782 case NL80211_IFTYPE_STATION:
John W. Linvillec809e862010-05-06 16:49:40 -0400783 case NL80211_IFTYPE_ADHOC:
Michael Wuf6532112007-10-14 14:43:16 -0400784 break;
785 default:
786 return -EOPNOTSUPP;
787 }
788
Johannes Berg1ed32e42009-12-23 13:15:45 +0100789 priv->vif = vif;
Johannes Berg32bfd352007-12-19 01:31:26 +0100790
John W. Linvillec809e862010-05-06 16:49:40 -0400791 /* Initialize driver private area */
792 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
793 vif_priv->dev = dev;
794 INIT_DELAYED_WORK(&vif_priv->beacon_work, rtl8180_beacon_work);
795 vif_priv->enable_beacon = false;
796
Michael Wuf6532112007-10-14 14:43:16 -0400797 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
798 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->MAC[0],
Johannes Berg1ed32e42009-12-23 13:15:45 +0100799 le32_to_cpu(*(__le32 *)vif->addr));
Michael Wuf6532112007-10-14 14:43:16 -0400800 rtl818x_iowrite16(priv, (__le16 __iomem *)&priv->map->MAC[4],
Johannes Berg1ed32e42009-12-23 13:15:45 +0100801 le16_to_cpu(*(__le16 *)(vif->addr + 4)));
Michael Wuf6532112007-10-14 14:43:16 -0400802 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
803
804 return 0;
805}
806
807static void rtl8180_remove_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100808 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -0400809{
810 struct rtl8180_priv *priv = dev->priv;
Johannes Berg32bfd352007-12-19 01:31:26 +0100811 priv->vif = NULL;
Michael Wuf6532112007-10-14 14:43:16 -0400812}
813
Johannes Berge8975582008-10-09 12:18:51 +0200814static int rtl8180_config(struct ieee80211_hw *dev, u32 changed)
Michael Wuf6532112007-10-14 14:43:16 -0400815{
816 struct rtl8180_priv *priv = dev->priv;
Johannes Berge8975582008-10-09 12:18:51 +0200817 struct ieee80211_conf *conf = &dev->conf;
Michael Wuf6532112007-10-14 14:43:16 -0400818
819 priv->rf->set_chan(dev, conf);
820
821 return 0;
822}
823
John W. Linvilleda81ded2008-11-12 14:37:11 -0500824static void rtl8180_bss_info_changed(struct ieee80211_hw *dev,
825 struct ieee80211_vif *vif,
826 struct ieee80211_bss_conf *info,
827 u32 changed)
828{
829 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -0400830 struct rtl8180_vif *vif_priv;
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200831 int i;
John W. Linville0f956e72010-07-29 21:50:29 -0400832 u8 reg;
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200833
John W. Linvillec809e862010-05-06 16:49:40 -0400834 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
835
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200836 if (changed & BSS_CHANGED_BSSID) {
837 for (i = 0; i < ETH_ALEN; i++)
838 rtl818x_iowrite8(priv, &priv->map->BSSID[i],
839 info->bssid[i]);
840
John W. Linville0f956e72010-07-29 21:50:29 -0400841 if (is_valid_ether_addr(info->bssid)) {
842 if (vif->type == NL80211_IFTYPE_ADHOC)
843 reg = RTL818X_MSR_ADHOC;
844 else
845 reg = RTL818X_MSR_INFRA;
846 } else
847 reg = RTL818X_MSR_NO_LINK;
848 rtl818x_iowrite8(priv, &priv->map->MSR, reg);
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200849 }
John W. Linvilleda81ded2008-11-12 14:37:11 -0500850
851 if (changed & BSS_CHANGED_ERP_SLOT && priv->rf->conf_erp)
John W. Linvillec809e862010-05-06 16:49:40 -0400852 priv->rf->conf_erp(dev, info);
853
854 if (changed & BSS_CHANGED_BEACON_ENABLED)
855 vif_priv->enable_beacon = info->enable_beacon;
856
857 if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON)) {
858 cancel_delayed_work_sync(&vif_priv->beacon_work);
859 if (vif_priv->enable_beacon)
860 schedule_work(&vif_priv->beacon_work.work);
861 }
John W. Linvilleda81ded2008-11-12 14:37:11 -0500862}
863
Jiri Pirko22bedad32010-04-01 21:22:57 +0000864static u64 rtl8180_prepare_multicast(struct ieee80211_hw *dev,
865 struct netdev_hw_addr_list *mc_list)
Johannes Berg3ac64be2009-08-17 16:16:53 +0200866{
Jiri Pirko22bedad32010-04-01 21:22:57 +0000867 return netdev_hw_addr_list_count(mc_list);
Johannes Berg3ac64be2009-08-17 16:16:53 +0200868}
869
Michael Wuf6532112007-10-14 14:43:16 -0400870static void rtl8180_configure_filter(struct ieee80211_hw *dev,
871 unsigned int changed_flags,
872 unsigned int *total_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +0200873 u64 multicast)
Michael Wuf6532112007-10-14 14:43:16 -0400874{
875 struct rtl8180_priv *priv = dev->priv;
876
877 if (changed_flags & FIF_FCSFAIL)
878 priv->rx_conf ^= RTL818X_RX_CONF_FCS;
879 if (changed_flags & FIF_CONTROL)
880 priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
881 if (changed_flags & FIF_OTHER_BSS)
882 priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
Johannes Berg3ac64be2009-08-17 16:16:53 +0200883 if (*total_flags & FIF_ALLMULTI || multicast > 0)
Michael Wuf6532112007-10-14 14:43:16 -0400884 priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
885 else
886 priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
887
888 *total_flags = 0;
889
890 if (priv->rx_conf & RTL818X_RX_CONF_FCS)
891 *total_flags |= FIF_FCSFAIL;
892 if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
893 *total_flags |= FIF_CONTROL;
894 if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
895 *total_flags |= FIF_OTHER_BSS;
896 if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
897 *total_flags |= FIF_ALLMULTI;
898
899 rtl818x_iowrite32(priv, &priv->map->RX_CONF, priv->rx_conf);
900}
901
902static const struct ieee80211_ops rtl8180_ops = {
903 .tx = rtl8180_tx,
904 .start = rtl8180_start,
905 .stop = rtl8180_stop,
906 .add_interface = rtl8180_add_interface,
907 .remove_interface = rtl8180_remove_interface,
908 .config = rtl8180_config,
John W. Linvilleda81ded2008-11-12 14:37:11 -0500909 .bss_info_changed = rtl8180_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +0200910 .prepare_multicast = rtl8180_prepare_multicast,
Michael Wuf6532112007-10-14 14:43:16 -0400911 .configure_filter = rtl8180_configure_filter,
John W. Linvilled2bb8e02010-01-26 16:22:20 -0500912 .get_tsf = rtl8180_get_tsf,
Michael Wuf6532112007-10-14 14:43:16 -0400913};
914
915static void rtl8180_eeprom_register_read(struct eeprom_93cx6 *eeprom)
916{
917 struct ieee80211_hw *dev = eeprom->data;
918 struct rtl8180_priv *priv = dev->priv;
919 u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
920
921 eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
922 eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
923 eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
924 eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
925}
926
927static void rtl8180_eeprom_register_write(struct eeprom_93cx6 *eeprom)
928{
929 struct ieee80211_hw *dev = eeprom->data;
930 struct rtl8180_priv *priv = dev->priv;
931 u8 reg = 2 << 6;
932
933 if (eeprom->reg_data_in)
934 reg |= RTL818X_EEPROM_CMD_WRITE;
935 if (eeprom->reg_data_out)
936 reg |= RTL818X_EEPROM_CMD_READ;
937 if (eeprom->reg_data_clock)
938 reg |= RTL818X_EEPROM_CMD_CK;
939 if (eeprom->reg_chip_select)
940 reg |= RTL818X_EEPROM_CMD_CS;
941
942 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
943 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
944 udelay(10);
945}
946
Bill Pembertonfb4e8992012-12-03 09:56:40 -0500947static int rtl8180_probe(struct pci_dev *pdev,
Michael Wuf6532112007-10-14 14:43:16 -0400948 const struct pci_device_id *id)
949{
950 struct ieee80211_hw *dev;
951 struct rtl8180_priv *priv;
952 unsigned long mem_addr, mem_len;
953 unsigned int io_addr, io_len;
954 int err, i;
955 struct eeprom_93cx6 eeprom;
956 const char *chip_name, *rf_name = NULL;
957 u32 reg;
958 u16 eeprom_val;
John W. Linvillec693bf92010-05-04 15:46:15 -0400959 u8 mac_addr[ETH_ALEN];
Michael Wuf6532112007-10-14 14:43:16 -0400960
961 err = pci_enable_device(pdev);
962 if (err) {
963 printk(KERN_ERR "%s (rtl8180): Cannot enable new PCI device\n",
964 pci_name(pdev));
965 return err;
966 }
967
968 err = pci_request_regions(pdev, KBUILD_MODNAME);
969 if (err) {
970 printk(KERN_ERR "%s (rtl8180): Cannot obtain PCI resources\n",
971 pci_name(pdev));
972 return err;
973 }
974
975 io_addr = pci_resource_start(pdev, 0);
976 io_len = pci_resource_len(pdev, 0);
977 mem_addr = pci_resource_start(pdev, 1);
978 mem_len = pci_resource_len(pdev, 1);
979
980 if (mem_len < sizeof(struct rtl818x_csr) ||
981 io_len < sizeof(struct rtl818x_csr)) {
982 printk(KERN_ERR "%s (rtl8180): Too short PCI resources\n",
983 pci_name(pdev));
984 err = -ENOMEM;
985 goto err_free_reg;
986 }
987
John W. Linville9e385c52010-05-10 14:24:34 -0400988 if ((err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) ||
989 (err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))) {
Michael Wuf6532112007-10-14 14:43:16 -0400990 printk(KERN_ERR "%s (rtl8180): No suitable DMA available\n",
991 pci_name(pdev));
992 goto err_free_reg;
993 }
994
995 pci_set_master(pdev);
996
997 dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8180_ops);
998 if (!dev) {
999 printk(KERN_ERR "%s (rtl8180): ieee80211 alloc failed\n",
1000 pci_name(pdev));
1001 err = -ENOMEM;
1002 goto err_free_reg;
1003 }
1004
1005 priv = dev->priv;
1006 priv->pdev = pdev;
1007
Johannes Berge6a98542008-10-21 12:40:02 +02001008 dev->max_rates = 2;
Michael Wuf6532112007-10-14 14:43:16 -04001009 SET_IEEE80211_DEV(dev, &pdev->dev);
1010 pci_set_drvdata(pdev, dev);
1011
1012 priv->map = pci_iomap(pdev, 1, mem_len);
1013 if (!priv->map)
1014 priv->map = pci_iomap(pdev, 0, io_len);
1015
1016 if (!priv->map) {
1017 printk(KERN_ERR "%s (rtl8180): Cannot map device memory\n",
1018 pci_name(pdev));
1019 goto err_free_dev;
1020 }
1021
Johannes Berg8318d782008-01-24 19:38:38 +01001022 BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
1023 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
1024
Michael Wuf6532112007-10-14 14:43:16 -04001025 memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
1026 memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
Johannes Berg8318d782008-01-24 19:38:38 +01001027
1028 priv->band.band = IEEE80211_BAND_2GHZ;
1029 priv->band.channels = priv->channels;
1030 priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
1031 priv->band.bitrates = priv->rates;
1032 priv->band.n_bitrates = 4;
1033 dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
1034
Michael Wuf6532112007-10-14 14:43:16 -04001035 dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
Bruno Randolf566bfe52008-05-08 19:15:40 +02001036 IEEE80211_HW_RX_INCLUDES_FCS |
1037 IEEE80211_HW_SIGNAL_UNSPEC;
John W. Linvillec809e862010-05-06 16:49:40 -04001038 dev->vif_data_size = sizeof(struct rtl8180_vif);
1039 dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1040 BIT(NL80211_IFTYPE_ADHOC);
Michael Wuf6532112007-10-14 14:43:16 -04001041 dev->queues = 1;
Bruno Randolf566bfe52008-05-08 19:15:40 +02001042 dev->max_signal = 65;
Michael Wuf6532112007-10-14 14:43:16 -04001043
1044 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
1045 reg &= RTL818X_TX_CONF_HWVER_MASK;
1046 switch (reg) {
1047 case RTL818X_TX_CONF_R8180_ABCD:
1048 chip_name = "RTL8180";
1049 break;
1050 case RTL818X_TX_CONF_R8180_F:
1051 chip_name = "RTL8180vF";
1052 break;
1053 case RTL818X_TX_CONF_R8185_ABC:
1054 chip_name = "RTL8185";
1055 break;
1056 case RTL818X_TX_CONF_R8185_D:
1057 chip_name = "RTL8185vD";
1058 break;
1059 default:
1060 printk(KERN_ERR "%s (rtl8180): Unknown chip! (0x%x)\n",
1061 pci_name(pdev), reg >> 25);
1062 goto err_iounmap;
1063 }
1064
1065 priv->r8185 = reg & RTL818X_TX_CONF_R8185_ABC;
1066 if (priv->r8185) {
Johannes Berg8318d782008-01-24 19:38:38 +01001067 priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
Michael Wuf6532112007-10-14 14:43:16 -04001068 pci_try_set_mwi(pdev);
1069 }
1070
Michael Wuf6532112007-10-14 14:43:16 -04001071 eeprom.data = dev;
1072 eeprom.register_read = rtl8180_eeprom_register_read;
1073 eeprom.register_write = rtl8180_eeprom_register_write;
1074 if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
1075 eeprom.width = PCI_EEPROM_WIDTH_93C66;
1076 else
1077 eeprom.width = PCI_EEPROM_WIDTH_93C46;
1078
1079 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_PROGRAM);
1080 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1081 udelay(10);
1082
1083 eeprom_93cx6_read(&eeprom, 0x06, &eeprom_val);
1084 eeprom_val &= 0xFF;
1085 switch (eeprom_val) {
1086 case 1: rf_name = "Intersil";
1087 break;
1088 case 2: rf_name = "RFMD";
1089 break;
1090 case 3: priv->rf = &sa2400_rf_ops;
1091 break;
1092 case 4: priv->rf = &max2820_rf_ops;
1093 break;
1094 case 5: priv->rf = &grf5101_rf_ops;
1095 break;
1096 case 9: priv->rf = rtl8180_detect_rf(dev);
1097 break;
1098 case 10:
1099 rf_name = "RTL8255";
1100 break;
1101 default:
1102 printk(KERN_ERR "%s (rtl8180): Unknown RF! (0x%x)\n",
1103 pci_name(pdev), eeprom_val);
1104 goto err_iounmap;
1105 }
1106
1107 if (!priv->rf) {
1108 printk(KERN_ERR "%s (rtl8180): %s RF frontend not supported!\n",
1109 pci_name(pdev), rf_name);
1110 goto err_iounmap;
1111 }
1112
1113 eeprom_93cx6_read(&eeprom, 0x17, &eeprom_val);
1114 priv->csthreshold = eeprom_val >> 8;
1115 if (!priv->r8185) {
1116 __le32 anaparam;
1117 eeprom_93cx6_multiread(&eeprom, 0xD, (__le16 *)&anaparam, 2);
1118 priv->anaparam = le32_to_cpu(anaparam);
1119 eeprom_93cx6_read(&eeprom, 0x19, &priv->rfparam);
1120 }
1121
John W. Linvillec693bf92010-05-04 15:46:15 -04001122 eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)mac_addr, 3);
1123 if (!is_valid_ether_addr(mac_addr)) {
Michael Wuf6532112007-10-14 14:43:16 -04001124 printk(KERN_WARNING "%s (rtl8180): Invalid hwaddr! Using"
1125 " randomly generated MAC addr\n", pci_name(pdev));
Joe Perchesf4f7f4142012-07-12 19:33:08 +00001126 eth_random_addr(mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001127 }
John W. Linvillec693bf92010-05-04 15:46:15 -04001128 SET_IEEE80211_PERM_ADDR(dev, mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001129
1130 /* CCK TX power */
1131 for (i = 0; i < 14; i += 2) {
1132 u16 txpwr;
1133 eeprom_93cx6_read(&eeprom, 0x10 + (i >> 1), &txpwr);
Johannes Berg8318d782008-01-24 19:38:38 +01001134 priv->channels[i].hw_value = txpwr & 0xFF;
1135 priv->channels[i + 1].hw_value = txpwr >> 8;
Michael Wuf6532112007-10-14 14:43:16 -04001136 }
1137
1138 /* OFDM TX power */
1139 if (priv->r8185) {
1140 for (i = 0; i < 14; i += 2) {
1141 u16 txpwr;
1142 eeprom_93cx6_read(&eeprom, 0x20 + (i >> 1), &txpwr);
Johannes Berg8318d782008-01-24 19:38:38 +01001143 priv->channels[i].hw_value |= (txpwr & 0xFF) << 8;
1144 priv->channels[i + 1].hw_value |= txpwr & 0xFF00;
Michael Wuf6532112007-10-14 14:43:16 -04001145 }
1146 }
1147
1148 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
1149
1150 spin_lock_init(&priv->lock);
1151
1152 err = ieee80211_register_hw(dev);
1153 if (err) {
1154 printk(KERN_ERR "%s (rtl8180): Cannot register device\n",
1155 pci_name(pdev));
1156 goto err_iounmap;
1157 }
1158
Joe Perchesc96c31e2010-07-26 14:39:58 -07001159 wiphy_info(dev->wiphy, "hwaddr %pm, %s + %s\n",
1160 mac_addr, chip_name, priv->rf->name);
Michael Wuf6532112007-10-14 14:43:16 -04001161
1162 return 0;
1163
1164 err_iounmap:
andrea merello0269da22014-02-18 02:10:41 +01001165 pci_iounmap(pdev, priv->map);
Michael Wuf6532112007-10-14 14:43:16 -04001166
1167 err_free_dev:
Michael Wuf6532112007-10-14 14:43:16 -04001168 ieee80211_free_hw(dev);
1169
1170 err_free_reg:
1171 pci_release_regions(pdev);
1172 pci_disable_device(pdev);
1173 return err;
1174}
1175
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001176static void rtl8180_remove(struct pci_dev *pdev)
Michael Wuf6532112007-10-14 14:43:16 -04001177{
1178 struct ieee80211_hw *dev = pci_get_drvdata(pdev);
1179 struct rtl8180_priv *priv;
1180
1181 if (!dev)
1182 return;
1183
1184 ieee80211_unregister_hw(dev);
1185
1186 priv = dev->priv;
1187
1188 pci_iounmap(pdev, priv->map);
1189 pci_release_regions(pdev);
1190 pci_disable_device(pdev);
1191 ieee80211_free_hw(dev);
1192}
1193
1194#ifdef CONFIG_PM
1195static int rtl8180_suspend(struct pci_dev *pdev, pm_message_t state)
1196{
1197 pci_save_state(pdev);
1198 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1199 return 0;
1200}
1201
1202static int rtl8180_resume(struct pci_dev *pdev)
1203{
1204 pci_set_power_state(pdev, PCI_D0);
1205 pci_restore_state(pdev);
1206 return 0;
1207}
1208
1209#endif /* CONFIG_PM */
1210
1211static struct pci_driver rtl8180_driver = {
1212 .name = KBUILD_MODNAME,
1213 .id_table = rtl8180_table,
1214 .probe = rtl8180_probe,
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001215 .remove = rtl8180_remove,
Michael Wuf6532112007-10-14 14:43:16 -04001216#ifdef CONFIG_PM
1217 .suspend = rtl8180_suspend,
1218 .resume = rtl8180_resume,
1219#endif /* CONFIG_PM */
1220};
1221
Axel Lin5b0a3b72012-04-14 10:38:36 +08001222module_pci_driver(rtl8180_driver);