blob: 6b237ed15eed6b6a1d1c9e93496f41f9df84fce8 [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
Andrea Merellofd6564f2014-03-22 18:51:20 +010088/* Queues for rtl8180/rtl8185 cards
89 *
90 * name | reg | prio
91 * BC | 7 | 3
92 * HI | 6 | 0
93 * NO | 5 | 1
94 * LO | 4 | 2
95 *
96 * The complete map for DMA kick reg using all queue is:
97 * static const int rtl8180_queues_map[RTL8180_NR_TX_QUEUES] = {6, 5, 4, 7};
98 *
99 * .. but .. Because the mac80211 needs at least 4 queues for QoS or
100 * otherwise QoS can't be done, we use just one.
101 * Beacon queue could be used, but this is not finished yet.
102 * Actual map is:
103 *
104 * name | reg | prio
105 * BC | 7 | 1 <- currently not used yet.
106 * HI | 6 | x <- not used
107 * NO | 5 | x <- not used
108 * LO | 4 | 0 <- used
109 */
110
111static const int rtl8180_queues_map[RTL8180_NR_TX_QUEUES] = {4, 7};
Johannes Berg8318d782008-01-24 19:38:38 +0100112
Michael Wuf6532112007-10-14 14:43:16 -0400113void rtl8180_write_phy(struct ieee80211_hw *dev, u8 addr, u32 data)
114{
115 struct rtl8180_priv *priv = dev->priv;
116 int i = 10;
117 u32 buf;
118
119 buf = (data << 8) | addr;
120
121 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->PHY[0], buf | 0x80);
122 while (i--) {
123 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->PHY[0], buf);
124 if (rtl818x_ioread8(priv, &priv->map->PHY[2]) == (data & 0xFF))
125 return;
126 }
127}
128
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400129static void rtl8180_handle_rx(struct ieee80211_hw *dev)
Michael Wuf6532112007-10-14 14:43:16 -0400130{
131 struct rtl8180_priv *priv = dev->priv;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400132 unsigned int count = 32;
John W. Linville8b73fb82010-07-21 16:26:40 -0400133 u8 signal, agc, sq;
andrea.merello2b4db052014-02-05 22:38:05 +0100134 dma_addr_t mapping;
Michael Wuf6532112007-10-14 14:43:16 -0400135
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400136 while (count--) {
Michael Wuf6532112007-10-14 14:43:16 -0400137 struct rtl8180_rx_desc *entry = &priv->rx_ring[priv->rx_idx];
138 struct sk_buff *skb = priv->rx_buf[priv->rx_idx];
139 u32 flags = le32_to_cpu(entry->flags);
140
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300141 if (flags & RTL818X_RX_DESC_FLAG_OWN)
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400142 return;
Michael Wuf6532112007-10-14 14:43:16 -0400143
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300144 if (unlikely(flags & (RTL818X_RX_DESC_FLAG_DMA_FAIL |
145 RTL818X_RX_DESC_FLAG_FOF |
146 RTL818X_RX_DESC_FLAG_RX_ERR)))
Michael Wuf6532112007-10-14 14:43:16 -0400147 goto done;
148 else {
149 u32 flags2 = le32_to_cpu(entry->flags2);
150 struct ieee80211_rx_status rx_status = {0};
151 struct sk_buff *new_skb = dev_alloc_skb(MAX_RX_SIZE);
152
153 if (unlikely(!new_skb))
154 goto done;
155
andrea.merello2b4db052014-02-05 22:38:05 +0100156 mapping = pci_map_single(priv->pdev,
157 skb_tail_pointer(new_skb),
158 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
159
160 if (pci_dma_mapping_error(priv->pdev, mapping)) {
161 kfree_skb(new_skb);
162 dev_err(&priv->pdev->dev, "RX DMA map error\n");
163
164 goto done;
165 }
166
Michael Wuf6532112007-10-14 14:43:16 -0400167 pci_unmap_single(priv->pdev,
168 *((dma_addr_t *)skb->cb),
169 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
170 skb_put(skb, flags & 0xFFF);
171
172 rx_status.antenna = (flags2 >> 15) & 1;
Johannes Berg8318d782008-01-24 19:38:38 +0100173 rx_status.rate_idx = (flags >> 20) & 0xF;
John W. Linville8b73fb82010-07-21 16:26:40 -0400174 agc = (flags2 >> 17) & 0x7F;
Andrea Merello6caefd12014-03-08 18:36:37 +0100175
176 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
John W. Linville8b73fb82010-07-21 16:26:40 -0400177 if (rx_status.rate_idx > 3)
178 signal = 90 - clamp_t(u8, agc, 25, 90);
179 else
180 signal = 95 - clamp_t(u8, agc, 30, 95);
181 } else {
182 sq = flags2 & 0xff;
183 signal = priv->rf->calc_rssi(agc, sq);
184 }
John W. Linville8b749642010-07-19 16:35:20 -0400185 rx_status.signal = signal;
Karl Beldan675a0b02013-03-25 16:26:57 +0100186 rx_status.freq = dev->conf.chandef.chan->center_freq;
187 rx_status.band = dev->conf.chandef.chan->band;
Michael Wuf6532112007-10-14 14:43:16 -0400188 rx_status.mactime = le64_to_cpu(entry->tsft);
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800189 rx_status.flag |= RX_FLAG_MACTIME_START;
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300190 if (flags & RTL818X_RX_DESC_FLAG_CRC32_ERR)
Michael Wuf6532112007-10-14 14:43:16 -0400191 rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
192
Johannes Bergf1d58c22009-06-17 13:13:00 +0200193 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400194 ieee80211_rx_irqsafe(dev, skb);
Michael Wuf6532112007-10-14 14:43:16 -0400195
196 skb = new_skb;
197 priv->rx_buf[priv->rx_idx] = skb;
andrea.merello2b4db052014-02-05 22:38:05 +0100198 *((dma_addr_t *) skb->cb) = mapping;
Michael Wuf6532112007-10-14 14:43:16 -0400199 }
200
201 done:
202 entry->rx_buf = cpu_to_le32(*((dma_addr_t *)skb->cb));
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300203 entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
Michael Wuf6532112007-10-14 14:43:16 -0400204 MAX_RX_SIZE);
205 if (priv->rx_idx == 31)
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300206 entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
Michael Wuf6532112007-10-14 14:43:16 -0400207 priv->rx_idx = (priv->rx_idx + 1) % 32;
208 }
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400209}
Michael Wuf6532112007-10-14 14:43:16 -0400210
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400211static void rtl8180_handle_tx(struct ieee80211_hw *dev, unsigned int prio)
212{
213 struct rtl8180_priv *priv = dev->priv;
214 struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
Michael Wuf6532112007-10-14 14:43:16 -0400215
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400216 while (skb_queue_len(&ring->queue)) {
217 struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
218 struct sk_buff *skb;
219 struct ieee80211_tx_info *info;
220 u32 flags = le32_to_cpu(entry->flags);
221
222 if (flags & RTL818X_TX_DESC_FLAG_OWN)
223 return;
224
225 ring->idx = (ring->idx + 1) % ring->entries;
226 skb = __skb_dequeue(&ring->queue);
227 pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
228 skb->len, PCI_DMA_TODEVICE);
229
230 info = IEEE80211_SKB_CB(skb);
231 ieee80211_tx_info_clear_status(info);
232
233 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
234 (flags & RTL818X_TX_DESC_FLAG_TX_OK))
235 info->flags |= IEEE80211_TX_STAT_ACK;
236
237 info->status.rates[0].count = (flags & 0xFF) + 1;
238 info->status.rates[1].idx = -1;
239
240 ieee80211_tx_status_irqsafe(dev, skb);
241 if (ring->entries - skb_queue_len(&ring->queue) == 2)
242 ieee80211_wake_queue(dev, prio);
Michael Wuf6532112007-10-14 14:43:16 -0400243 }
244}
245
246static irqreturn_t rtl8180_interrupt(int irq, void *dev_id)
247{
248 struct ieee80211_hw *dev = dev_id;
249 struct rtl8180_priv *priv = dev->priv;
250 u16 reg;
251
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400252 spin_lock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400253 reg = rtl818x_ioread16(priv, &priv->map->INT_STATUS);
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400254 if (unlikely(reg == 0xFFFF)) {
255 spin_unlock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400256 return IRQ_HANDLED;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400257 }
Michael Wuf6532112007-10-14 14:43:16 -0400258
259 rtl818x_iowrite16(priv, &priv->map->INT_STATUS, reg);
260
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400261 if (reg & (RTL818X_INT_TXB_OK | RTL818X_INT_TXB_ERR))
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400262 rtl8180_handle_tx(dev, 1);
263
264 if (reg & (RTL818X_INT_TXL_OK | RTL818X_INT_TXL_ERR))
265 rtl8180_handle_tx(dev, 0);
266
267 if (reg & (RTL818X_INT_RX_OK | RTL818X_INT_RX_ERR))
268 rtl8180_handle_rx(dev);
269
270 spin_unlock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400271
272 return IRQ_HANDLED;
273}
274
Thomas Huehn36323f82012-07-23 21:33:42 +0200275static void rtl8180_tx(struct ieee80211_hw *dev,
276 struct ieee80211_tx_control *control,
277 struct sk_buff *skb)
Michael Wuf6532112007-10-14 14:43:16 -0400278{
Johannes Berge039fa42008-05-15 12:55:29 +0200279 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
John W. Linville51e080d2010-05-06 16:26:23 -0400280 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Michael Wuf6532112007-10-14 14:43:16 -0400281 struct rtl8180_priv *priv = dev->priv;
282 struct rtl8180_tx_ring *ring;
283 struct rtl8180_tx_desc *entry;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400284 unsigned long flags;
Andrea Merellofd6564f2014-03-22 18:51:20 +0100285 unsigned int idx, prio, hw_prio;
Michael Wuf6532112007-10-14 14:43:16 -0400286 dma_addr_t mapping;
287 u32 tx_flags;
Johannes Berge6a98542008-10-21 12:40:02 +0200288 u8 rc_flags;
Michael Wuf6532112007-10-14 14:43:16 -0400289 u16 plcp_len = 0;
290 __le16 rts_duration = 0;
291
Johannes Berge2530082008-05-17 00:57:14 +0200292 prio = skb_get_queue_mapping(skb);
Michael Wuf6532112007-10-14 14:43:16 -0400293 ring = &priv->tx_ring[prio];
294
295 mapping = pci_map_single(priv->pdev, skb->data,
296 skb->len, PCI_DMA_TODEVICE);
297
andrea.merello348f7d42014-02-05 22:38:06 +0100298 if (pci_dma_mapping_error(priv->pdev, mapping)) {
299 kfree_skb(skb);
300 dev_err(&priv->pdev->dev, "TX DMA mapping error\n");
301 return;
302
303 }
304
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300305 tx_flags = RTL818X_TX_DESC_FLAG_OWN | RTL818X_TX_DESC_FLAG_FS |
306 RTL818X_TX_DESC_FLAG_LS |
Johannes Berge039fa42008-05-15 12:55:29 +0200307 (ieee80211_get_tx_rate(dev, info)->hw_value << 24) |
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200308 skb->len;
Michael Wuf6532112007-10-14 14:43:16 -0400309
Andrea Merello6caefd12014-03-08 18:36:37 +0100310 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300311 tx_flags |= RTL818X_TX_DESC_FLAG_DMA |
312 RTL818X_TX_DESC_FLAG_NO_ENC;
Michael Wuf6532112007-10-14 14:43:16 -0400313
Johannes Berge6a98542008-10-21 12:40:02 +0200314 rc_flags = info->control.rates[0].flags;
315 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300316 tx_flags |= RTL818X_TX_DESC_FLAG_RTS;
Johannes Berge039fa42008-05-15 12:55:29 +0200317 tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
Johannes Berge6a98542008-10-21 12:40:02 +0200318 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300319 tx_flags |= RTL818X_TX_DESC_FLAG_CTS;
Johannes Berge039fa42008-05-15 12:55:29 +0200320 tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
Johannes Bergaa68cbf2008-02-18 14:20:30 +0100321 }
Michael Wuf6532112007-10-14 14:43:16 -0400322
Johannes Berge6a98542008-10-21 12:40:02 +0200323 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS)
Johannes Berg32bfd352007-12-19 01:31:26 +0100324 rts_duration = ieee80211_rts_duration(dev, priv->vif, skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +0200325 info);
Michael Wuf6532112007-10-14 14:43:16 -0400326
Andrea Merello6caefd12014-03-08 18:36:37 +0100327 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400328 unsigned int remainder;
329
330 plcp_len = DIV_ROUND_UP(16 * (skb->len + 4),
Johannes Berge039fa42008-05-15 12:55:29 +0200331 (ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
Michael Wuf6532112007-10-14 14:43:16 -0400332 remainder = (16 * (skb->len + 4)) %
Johannes Berge039fa42008-05-15 12:55:29 +0200333 ((ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
Roel Kluin35a0ace2009-06-22 17:42:21 +0200334 if (remainder <= 6)
Michael Wuf6532112007-10-14 14:43:16 -0400335 plcp_len |= 1 << 15;
336 }
337
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400338 spin_lock_irqsave(&priv->lock, flags);
John W. Linville51e080d2010-05-06 16:26:23 -0400339
340 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
341 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
342 priv->seqno += 0x10;
343 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
344 hdr->seq_ctrl |= cpu_to_le16(priv->seqno);
345 }
346
Michael Wuf6532112007-10-14 14:43:16 -0400347 idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
348 entry = &ring->desc[idx];
349
350 entry->rts_duration = rts_duration;
351 entry->plcp_len = cpu_to_le16(plcp_len);
352 entry->tx_buf = cpu_to_le32(mapping);
353 entry->frame_len = cpu_to_le32(skb->len);
Johannes Berge6a98542008-10-21 12:40:02 +0200354 entry->flags2 = info->control.rates[1].idx >= 0 ?
Felix Fietkau870abdf2008-10-05 18:04:24 +0200355 ieee80211_get_alt_retry_rate(dev, info, 0)->bitrate << 4 : 0;
Johannes Berge6a98542008-10-21 12:40:02 +0200356 entry->retry_limit = info->control.rates[0].count;
andrea merello4c552a52014-02-18 02:10:45 +0100357
358 /* We must be sure that tx_flags is written last because the HW
359 * looks at it to check if the rest of data is valid or not
360 */
361 wmb();
Michael Wuf6532112007-10-14 14:43:16 -0400362 entry->flags = cpu_to_le32(tx_flags);
andrea merelloc24782e2014-02-18 02:10:46 +0100363 /* We must be sure this has been written before followings HW
364 * register write, because this write will made the HW attempts
365 * to DMA the just-written data
366 */
367 wmb();
368
Michael Wuf6532112007-10-14 14:43:16 -0400369 __skb_queue_tail(&ring->queue, skb);
370 if (ring->entries - skb_queue_len(&ring->queue) < 2)
John W. Linvilled10e2e02010-04-27 16:57:38 -0400371 ieee80211_stop_queue(dev, prio);
John W. Linville51e080d2010-05-06 16:26:23 -0400372
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400373 spin_unlock_irqrestore(&priv->lock, flags);
Michael Wuf6532112007-10-14 14:43:16 -0400374
Andrea Merellofd6564f2014-03-22 18:51:20 +0100375 hw_prio = rtl8180_queues_map[prio];
376
377 rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
378 (1 << hw_prio) | /* ring to poll */
379 (1<<1) | (1<<2));/* stopped rings */
Michael Wuf6532112007-10-14 14:43:16 -0400380}
381
382void rtl8180_set_anaparam(struct rtl8180_priv *priv, u32 anaparam)
383{
384 u8 reg;
385
386 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
387 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
388 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
389 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
390 rtl818x_iowrite32(priv, &priv->map->ANAPARAM, anaparam);
391 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
392 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
393 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
394}
395
Andrea Merello516a0932014-03-15 18:29:36 +0100396static void rtl8180_conf_basic_rates(struct ieee80211_hw *dev,
397 u32 rates_mask)
398{
399 struct rtl8180_priv *priv = dev->priv;
400
401 u8 max, min;
402 u16 reg;
403
404 max = fls(rates_mask) - 1;
405 min = ffs(rates_mask) - 1;
406
407 switch (priv->chip_family) {
408
409 case RTL818X_CHIP_FAMILY_RTL8180:
410 /* in 8180 this is NOT a BITMAP */
411 reg = rtl818x_ioread16(priv, &priv->map->BRSR);
412 reg &= ~3;
413 reg |= max;
414 rtl818x_iowrite16(priv, &priv->map->BRSR, reg);
Andrea Merello516a0932014-03-15 18:29:36 +0100415 break;
416
417 case RTL818X_CHIP_FAMILY_RTL8185:
418 /* in 8185 this is a BITMAP */
419 rtl818x_iowrite16(priv, &priv->map->BRSR, rates_mask);
420 rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (max << 4) | min);
421 break;
Andrea Merellod209f3b2014-03-26 20:59:25 +0100422
423 case RTL818X_CHIP_FAMILY_RTL8187SE:
424 /* in 8187se this is a BITMAP */
425 rtl818x_iowrite16(priv, &priv->map->BRSR_8187SE, rates_mask);
426 break;
Andrea Merello516a0932014-03-15 18:29:36 +0100427 }
428}
429
Michael Wuf6532112007-10-14 14:43:16 -0400430static int rtl8180_init_hw(struct ieee80211_hw *dev)
431{
432 struct rtl8180_priv *priv = dev->priv;
433 u16 reg;
434
435 rtl818x_iowrite8(priv, &priv->map->CMD, 0);
436 rtl818x_ioread8(priv, &priv->map->CMD);
437 msleep(10);
438
439 /* reset */
440 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
441 rtl818x_ioread8(priv, &priv->map->CMD);
442
443 reg = rtl818x_ioread8(priv, &priv->map->CMD);
444 reg &= (1 << 1);
445 reg |= RTL818X_CMD_RESET;
446 rtl818x_iowrite8(priv, &priv->map->CMD, RTL818X_CMD_RESET);
447 rtl818x_ioread8(priv, &priv->map->CMD);
448 msleep(200);
449
450 /* check success of reset */
451 if (rtl818x_ioread8(priv, &priv->map->CMD) & RTL818X_CMD_RESET) {
Joe Perchesc96c31e2010-07-26 14:39:58 -0700452 wiphy_err(dev->wiphy, "reset timeout!\n");
Michael Wuf6532112007-10-14 14:43:16 -0400453 return -ETIMEDOUT;
454 }
455
456 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
457 rtl818x_ioread8(priv, &priv->map->CMD);
458 msleep(200);
459
460 if (rtl818x_ioread8(priv, &priv->map->CONFIG3) & (1 << 3)) {
461 /* For cardbus */
462 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
463 reg |= 1 << 1;
464 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
465 reg = rtl818x_ioread16(priv, &priv->map->FEMR);
466 reg |= (1 << 15) | (1 << 14) | (1 << 4);
467 rtl818x_iowrite16(priv, &priv->map->FEMR, reg);
468 }
469
470 rtl818x_iowrite8(priv, &priv->map->MSR, 0);
471
Andrea Merello6caefd12014-03-08 18:36:37 +0100472 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
Michael Wuf6532112007-10-14 14:43:16 -0400473 rtl8180_set_anaparam(priv, priv->anaparam);
474
475 rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
Andrea Merellofd6564f2014-03-22 18:51:20 +0100476 rtl818x_iowrite32(priv, &priv->map->TBDA, priv->tx_ring[1].dma);
Michael Wuf6532112007-10-14 14:43:16 -0400477 rtl818x_iowrite32(priv, &priv->map->TLPDA, priv->tx_ring[0].dma);
478
479 /* TODO: necessary? specs indicate not */
480 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
481 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
482 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg & ~(1 << 3));
Andrea Merello6caefd12014-03-08 18:36:37 +0100483 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
Michael Wuf6532112007-10-14 14:43:16 -0400484 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
485 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg | (1 << 4));
486 }
487 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
488
489 /* TODO: set CONFIG5 for calibrating AGC on rtl8180 + philips radio? */
490
491 /* TODO: turn off hw wep on rtl8180 */
492
493 rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
494
Andrea Merello6caefd12014-03-08 18:36:37 +0100495 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400496 rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
497 rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
Michael Wuf6532112007-10-14 14:43:16 -0400498
499 /* TODO: set ClkRun enable? necessary? */
500 reg = rtl818x_ioread8(priv, &priv->map->GP_ENABLE);
501 rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, reg & ~(1 << 6));
502 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
503 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
504 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | (1 << 2));
505 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
506 } else {
Michael Wuf6532112007-10-14 14:43:16 -0400507 rtl818x_iowrite8(priv, &priv->map->SECURITY, 0);
508
509 rtl818x_iowrite8(priv, &priv->map->PHY_DELAY, 0x6);
510 rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, 0x4C);
511 }
512
513 priv->rf->init(dev);
Andrea Merello516a0932014-03-15 18:29:36 +0100514
515 /* default basic rates are 1,2 Mbps for rtl8180. 1,2,6,9,12,18,24 Mbps
516 * otherwise. bitmask 0x3 and 0x01f3 respectively.
517 * NOTE: currenty rtl8225 RF code changes basic rates, so we need to do
518 * this after rf init.
519 * TODO: try to find out whether RF code really needs to do this..
520 */
521 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
522 rtl8180_conf_basic_rates(dev, 0x3);
523 else
524 rtl8180_conf_basic_rates(dev, 0x1f3);
525
Michael Wuf6532112007-10-14 14:43:16 -0400526 return 0;
527}
528
529static int rtl8180_init_rx_ring(struct ieee80211_hw *dev)
530{
531 struct rtl8180_priv *priv = dev->priv;
532 struct rtl8180_rx_desc *entry;
533 int i;
534
535 priv->rx_ring = pci_alloc_consistent(priv->pdev,
536 sizeof(*priv->rx_ring) * 32,
537 &priv->rx_ring_dma);
538
539 if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -0700540 wiphy_err(dev->wiphy, "Cannot allocate RX ring\n");
Michael Wuf6532112007-10-14 14:43:16 -0400541 return -ENOMEM;
542 }
543
544 memset(priv->rx_ring, 0, sizeof(*priv->rx_ring) * 32);
545 priv->rx_idx = 0;
546
547 for (i = 0; i < 32; i++) {
548 struct sk_buff *skb = dev_alloc_skb(MAX_RX_SIZE);
549 dma_addr_t *mapping;
550 entry = &priv->rx_ring[i];
andrea merello4da18bb2014-02-18 02:10:43 +0100551 if (!skb) {
552 wiphy_err(dev->wiphy, "Cannot allocate RX skb\n");
553 return -ENOMEM;
554 }
Michael Wuf6532112007-10-14 14:43:16 -0400555 priv->rx_buf[i] = skb;
556 mapping = (dma_addr_t *)skb->cb;
557 *mapping = pci_map_single(priv->pdev, skb_tail_pointer(skb),
558 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
andrea merelloec1da082014-02-22 17:57:23 +0100559
560 if (pci_dma_mapping_error(priv->pdev, *mapping)) {
561 kfree_skb(skb);
562 wiphy_err(dev->wiphy, "Cannot map DMA for RX skb\n");
563 return -ENOMEM;
564 }
565
Michael Wuf6532112007-10-14 14:43:16 -0400566 entry->rx_buf = cpu_to_le32(*mapping);
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300567 entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
Michael Wuf6532112007-10-14 14:43:16 -0400568 MAX_RX_SIZE);
569 }
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300570 entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
Michael Wuf6532112007-10-14 14:43:16 -0400571 return 0;
572}
573
574static void rtl8180_free_rx_ring(struct ieee80211_hw *dev)
575{
576 struct rtl8180_priv *priv = dev->priv;
577 int i;
578
579 for (i = 0; i < 32; i++) {
580 struct sk_buff *skb = priv->rx_buf[i];
581 if (!skb)
582 continue;
583
584 pci_unmap_single(priv->pdev,
585 *((dma_addr_t *)skb->cb),
586 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
587 kfree_skb(skb);
588 }
589
590 pci_free_consistent(priv->pdev, sizeof(*priv->rx_ring) * 32,
591 priv->rx_ring, priv->rx_ring_dma);
592 priv->rx_ring = NULL;
593}
594
595static int rtl8180_init_tx_ring(struct ieee80211_hw *dev,
596 unsigned int prio, unsigned int entries)
597{
598 struct rtl8180_priv *priv = dev->priv;
599 struct rtl8180_tx_desc *ring;
600 dma_addr_t dma;
601 int i;
602
603 ring = pci_alloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
604 if (!ring || (unsigned long)ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -0700605 wiphy_err(dev->wiphy, "Cannot allocate TX ring (prio = %d)\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -0700606 prio);
Michael Wuf6532112007-10-14 14:43:16 -0400607 return -ENOMEM;
608 }
609
610 memset(ring, 0, sizeof(*ring)*entries);
611 priv->tx_ring[prio].desc = ring;
612 priv->tx_ring[prio].dma = dma;
613 priv->tx_ring[prio].idx = 0;
614 priv->tx_ring[prio].entries = entries;
615 skb_queue_head_init(&priv->tx_ring[prio].queue);
616
617 for (i = 0; i < entries; i++)
618 ring[i].next_tx_desc =
619 cpu_to_le32((u32)dma + ((i + 1) % entries) * sizeof(*ring));
620
621 return 0;
622}
623
624static void rtl8180_free_tx_ring(struct ieee80211_hw *dev, unsigned int prio)
625{
626 struct rtl8180_priv *priv = dev->priv;
627 struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
628
629 while (skb_queue_len(&ring->queue)) {
630 struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
631 struct sk_buff *skb = __skb_dequeue(&ring->queue);
632
633 pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
634 skb->len, PCI_DMA_TODEVICE);
Michael Wuf6532112007-10-14 14:43:16 -0400635 kfree_skb(skb);
636 ring->idx = (ring->idx + 1) % ring->entries;
637 }
638
639 pci_free_consistent(priv->pdev, sizeof(*ring->desc)*ring->entries,
640 ring->desc, ring->dma);
641 ring->desc = NULL;
642}
643
644static int rtl8180_start(struct ieee80211_hw *dev)
645{
646 struct rtl8180_priv *priv = dev->priv;
647 int ret, i;
648 u32 reg;
649
650 ret = rtl8180_init_rx_ring(dev);
651 if (ret)
652 return ret;
653
Andrea Merellofd6564f2014-03-22 18:51:20 +0100654 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -0400655 if ((ret = rtl8180_init_tx_ring(dev, i, 16)))
656 goto err_free_rings;
657
658 ret = rtl8180_init_hw(dev);
659 if (ret)
660 goto err_free_rings;
661
Julia Lawallea31ba32009-11-18 08:26:02 +0000662 ret = request_irq(priv->pdev->irq, rtl8180_interrupt,
Michael Wuf6532112007-10-14 14:43:16 -0400663 IRQF_SHARED, KBUILD_MODNAME, dev);
664 if (ret) {
Joe Perches5db55842010-08-11 19:11:19 -0700665 wiphy_err(dev->wiphy, "failed to register IRQ handler\n");
Michael Wuf6532112007-10-14 14:43:16 -0400666 goto err_free_rings;
667 }
668
669 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
670
671 rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
672 rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
673
674 reg = RTL818X_RX_CONF_ONLYERLPKT |
675 RTL818X_RX_CONF_RX_AUTORESETPHY |
676 RTL818X_RX_CONF_MGMT |
677 RTL818X_RX_CONF_DATA |
678 (7 << 8 /* MAX RX DMA */) |
679 RTL818X_RX_CONF_BROADCAST |
680 RTL818X_RX_CONF_NICMAC;
681
Andrea Merello6caefd12014-03-08 18:36:37 +0100682 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185)
Michael Wuf6532112007-10-14 14:43:16 -0400683 reg |= RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2;
684 else {
685 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE1)
686 ? RTL818X_RX_CONF_CSDM1 : 0;
687 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE2)
688 ? RTL818X_RX_CONF_CSDM2 : 0;
689 }
690
691 priv->rx_conf = reg;
692 rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
693
Andrea Merello6caefd12014-03-08 18:36:37 +0100694 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400695 reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
andrea merello14c76152014-02-18 02:10:44 +0100696
697 /* CW is not on per-packet basis.
698 * in rtl8185 the CW_VALUE reg is used.
699 */
andrea merello6f7343d2014-01-21 20:16:43 +0100700 reg &= ~RTL818X_CW_CONF_PERPACKET_CW;
andrea merello14c76152014-02-18 02:10:44 +0100701 /* retry limit IS on per-packet basis.
702 * the short and long retry limit in TX_CONF
703 * reg are ignored
704 */
andrea merello6f7343d2014-01-21 20:16:43 +0100705 reg |= RTL818X_CW_CONF_PERPACKET_RETRY;
Michael Wuf6532112007-10-14 14:43:16 -0400706 rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
707
708 reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
andrea merello14c76152014-02-18 02:10:44 +0100709 /* TX antenna and TX gain are not on per-packet basis.
710 * TX Antenna is selected by ANTSEL reg (RX in BB regs).
711 * TX gain is selected with CCK_TX_AGC and OFDM_TX_AGC regs
712 */
andrea merello6f7343d2014-01-21 20:16:43 +0100713 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN;
714 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL;
Michael Wuf6532112007-10-14 14:43:16 -0400715 reg |= RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
716 rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
717
718 /* disable early TX */
719 rtl818x_iowrite8(priv, (u8 __iomem *)priv->map + 0xec, 0x3f);
720 }
721
722 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
723 reg |= (6 << 21 /* MAX TX DMA */) |
724 RTL818X_TX_CONF_NO_ICV;
725
Andrea Merello6caefd12014-03-08 18:36:37 +0100726
727
728 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
Michael Wuf6532112007-10-14 14:43:16 -0400729 reg &= ~RTL818X_TX_CONF_PROBE_DTS;
730 else
731 reg &= ~RTL818X_TX_CONF_HW_SEQNUM;
732
andrea merelloe74075a2014-02-18 02:10:40 +0100733 reg &= ~RTL818X_TX_CONF_DISCW;
734
Michael Wuf6532112007-10-14 14:43:16 -0400735 /* different meaning, same value on both rtl8185 and rtl8180 */
736 reg &= ~RTL818X_TX_CONF_SAT_HWPLCP;
737
738 rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
739
740 reg = rtl818x_ioread8(priv, &priv->map->CMD);
741 reg |= RTL818X_CMD_RX_ENABLE;
742 reg |= RTL818X_CMD_TX_ENABLE;
743 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
744
Michael Wuf6532112007-10-14 14:43:16 -0400745 return 0;
746
747 err_free_rings:
748 rtl8180_free_rx_ring(dev);
Andrea Merellofd6564f2014-03-22 18:51:20 +0100749 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -0400750 if (priv->tx_ring[i].desc)
751 rtl8180_free_tx_ring(dev, i);
752
753 return ret;
754}
755
756static void rtl8180_stop(struct ieee80211_hw *dev)
757{
758 struct rtl8180_priv *priv = dev->priv;
759 u8 reg;
760 int i;
761
Michael Wuf6532112007-10-14 14:43:16 -0400762 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
763
764 reg = rtl818x_ioread8(priv, &priv->map->CMD);
765 reg &= ~RTL818X_CMD_TX_ENABLE;
766 reg &= ~RTL818X_CMD_RX_ENABLE;
767 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
768
769 priv->rf->stop(dev);
770
771 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
772 reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
773 rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
774 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
775
776 free_irq(priv->pdev->irq, dev);
777
778 rtl8180_free_rx_ring(dev);
Andrea Merellofd6564f2014-03-22 18:51:20 +0100779 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -0400780 rtl8180_free_tx_ring(dev, i);
781}
782
Eliad Peller37a41b42011-09-21 14:06:11 +0300783static u64 rtl8180_get_tsf(struct ieee80211_hw *dev,
784 struct ieee80211_vif *vif)
John W. Linvillec809e862010-05-06 16:49:40 -0400785{
786 struct rtl8180_priv *priv = dev->priv;
787
788 return rtl818x_ioread32(priv, &priv->map->TSFT[0]) |
789 (u64)(rtl818x_ioread32(priv, &priv->map->TSFT[1])) << 32;
790}
791
John W. Linvillea3275e22010-06-24 11:08:37 -0400792static void rtl8180_beacon_work(struct work_struct *work)
John W. Linvillec809e862010-05-06 16:49:40 -0400793{
794 struct rtl8180_vif *vif_priv =
795 container_of(work, struct rtl8180_vif, beacon_work.work);
796 struct ieee80211_vif *vif =
797 container_of((void *)vif_priv, struct ieee80211_vif, drv_priv);
798 struct ieee80211_hw *dev = vif_priv->dev;
799 struct ieee80211_mgmt *mgmt;
800 struct sk_buff *skb;
John W. Linvillec809e862010-05-06 16:49:40 -0400801
802 /* don't overflow the tx ring */
803 if (ieee80211_queue_stopped(dev, 0))
804 goto resched;
805
806 /* grab a fresh beacon */
807 skb = ieee80211_beacon_get(dev, vif);
John W. Linville8f1d2d22010-08-05 13:46:27 -0400808 if (!skb)
809 goto resched;
John W. Linvillec809e862010-05-06 16:49:40 -0400810
811 /*
812 * update beacon timestamp w/ TSF value
813 * TODO: make hardware update beacon timestamp
814 */
815 mgmt = (struct ieee80211_mgmt *)skb->data;
Eliad Peller37a41b42011-09-21 14:06:11 +0300816 mgmt->u.beacon.timestamp = cpu_to_le64(rtl8180_get_tsf(dev, vif));
John W. Linvillec809e862010-05-06 16:49:40 -0400817
818 /* TODO: use actual beacon queue */
819 skb_set_queue_mapping(skb, 0);
820
Thomas Huehn36323f82012-07-23 21:33:42 +0200821 rtl8180_tx(dev, NULL, skb);
John W. Linvillec809e862010-05-06 16:49:40 -0400822
823resched:
824 /*
825 * schedule next beacon
826 * TODO: use hardware support for beacon timing
827 */
828 schedule_delayed_work(&vif_priv->beacon_work,
829 usecs_to_jiffies(1024 * vif->bss_conf.beacon_int));
830}
831
Michael Wuf6532112007-10-14 14:43:16 -0400832static int rtl8180_add_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100833 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -0400834{
835 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -0400836 struct rtl8180_vif *vif_priv;
Michael Wuf6532112007-10-14 14:43:16 -0400837
John W. Linville643aab62009-12-22 18:13:04 -0500838 /*
839 * We only support one active interface at a time.
840 */
841 if (priv->vif)
842 return -EBUSY;
Michael Wuf6532112007-10-14 14:43:16 -0400843
Johannes Berg1ed32e42009-12-23 13:15:45 +0100844 switch (vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200845 case NL80211_IFTYPE_STATION:
John W. Linvillec809e862010-05-06 16:49:40 -0400846 case NL80211_IFTYPE_ADHOC:
Michael Wuf6532112007-10-14 14:43:16 -0400847 break;
848 default:
849 return -EOPNOTSUPP;
850 }
851
Johannes Berg1ed32e42009-12-23 13:15:45 +0100852 priv->vif = vif;
Johannes Berg32bfd352007-12-19 01:31:26 +0100853
John W. Linvillec809e862010-05-06 16:49:40 -0400854 /* Initialize driver private area */
855 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
856 vif_priv->dev = dev;
857 INIT_DELAYED_WORK(&vif_priv->beacon_work, rtl8180_beacon_work);
858 vif_priv->enable_beacon = false;
859
Michael Wuf6532112007-10-14 14:43:16 -0400860 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
861 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->MAC[0],
Johannes Berg1ed32e42009-12-23 13:15:45 +0100862 le32_to_cpu(*(__le32 *)vif->addr));
Michael Wuf6532112007-10-14 14:43:16 -0400863 rtl818x_iowrite16(priv, (__le16 __iomem *)&priv->map->MAC[4],
Johannes Berg1ed32e42009-12-23 13:15:45 +0100864 le16_to_cpu(*(__le16 *)(vif->addr + 4)));
Michael Wuf6532112007-10-14 14:43:16 -0400865 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
866
867 return 0;
868}
869
870static void rtl8180_remove_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100871 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -0400872{
873 struct rtl8180_priv *priv = dev->priv;
Johannes Berg32bfd352007-12-19 01:31:26 +0100874 priv->vif = NULL;
Michael Wuf6532112007-10-14 14:43:16 -0400875}
876
Johannes Berge8975582008-10-09 12:18:51 +0200877static int rtl8180_config(struct ieee80211_hw *dev, u32 changed)
Michael Wuf6532112007-10-14 14:43:16 -0400878{
879 struct rtl8180_priv *priv = dev->priv;
Johannes Berge8975582008-10-09 12:18:51 +0200880 struct ieee80211_conf *conf = &dev->conf;
Michael Wuf6532112007-10-14 14:43:16 -0400881
882 priv->rf->set_chan(dev, conf);
883
884 return 0;
885}
886
Andrea Merello9069af72014-03-15 18:29:37 +0100887static int rtl8180_conf_tx(struct ieee80211_hw *dev,
888 struct ieee80211_vif *vif, u16 queue,
889 const struct ieee80211_tx_queue_params *params)
890{
891 struct rtl8180_priv *priv = dev->priv;
892 u8 cw_min, cw_max;
893
894 /* nothing to do ? */
895 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
896 return 0;
897
898 cw_min = fls(params->cw_min);
899 cw_max = fls(params->cw_max);
900
901 rtl818x_iowrite8(priv, &priv->map->CW_VAL, (cw_max << 4) | cw_min);
902
903 return 0;
904}
905
906static void rtl8180_conf_erp(struct ieee80211_hw *dev,
907 struct ieee80211_bss_conf *info)
908{
909 struct rtl8180_priv *priv = dev->priv;
910 u8 sifs, difs;
911 int eifs;
912 u8 hw_eifs;
913
914 /* TODO: should we do something ? */
915 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
916 return;
917
918 /* I _hope_ this means 10uS for the HW.
919 * In reference code it is 0x22 for
920 * both rtl8187L and rtl8187SE
921 */
922 sifs = 0x22;
923
924 if (info->use_short_slot)
925 priv->slot_time = 9;
926 else
927 priv->slot_time = 20;
928
929 /* 10 is SIFS time in uS */
930 difs = 10 + 2 * priv->slot_time;
931 eifs = 10 + difs + priv->ack_time;
932
933 /* HW should use 4uS units for EIFS (I'm sure for rtl8185)*/
934 hw_eifs = DIV_ROUND_UP(eifs, 4);
935
936
937 rtl818x_iowrite8(priv, &priv->map->SLOT, priv->slot_time);
938 rtl818x_iowrite8(priv, &priv->map->SIFS, sifs);
939 rtl818x_iowrite8(priv, &priv->map->DIFS, difs);
940
941 /* from reference code. set ack timeout reg = eifs reg */
942 rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, hw_eifs);
943
944 /* rtl8187/rtl8185 HW bug. After EIFS is elapsed,
945 * the HW still wait for DIFS.
946 * HW uses 4uS units for EIFS.
947 */
948 hw_eifs = DIV_ROUND_UP(eifs - difs, 4);
949
950 rtl818x_iowrite8(priv, &priv->map->EIFS, hw_eifs);
951}
952
John W. Linvilleda81ded2008-11-12 14:37:11 -0500953static void rtl8180_bss_info_changed(struct ieee80211_hw *dev,
954 struct ieee80211_vif *vif,
955 struct ieee80211_bss_conf *info,
956 u32 changed)
957{
958 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -0400959 struct rtl8180_vif *vif_priv;
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200960 int i;
John W. Linville0f956e72010-07-29 21:50:29 -0400961 u8 reg;
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200962
John W. Linvillec809e862010-05-06 16:49:40 -0400963 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
964
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200965 if (changed & BSS_CHANGED_BSSID) {
966 for (i = 0; i < ETH_ALEN; i++)
967 rtl818x_iowrite8(priv, &priv->map->BSSID[i],
968 info->bssid[i]);
969
John W. Linville0f956e72010-07-29 21:50:29 -0400970 if (is_valid_ether_addr(info->bssid)) {
971 if (vif->type == NL80211_IFTYPE_ADHOC)
972 reg = RTL818X_MSR_ADHOC;
973 else
974 reg = RTL818X_MSR_INFRA;
975 } else
976 reg = RTL818X_MSR_NO_LINK;
977 rtl818x_iowrite8(priv, &priv->map->MSR, reg);
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200978 }
John W. Linvilleda81ded2008-11-12 14:37:11 -0500979
Andrea Merello516a0932014-03-15 18:29:36 +0100980 if (changed & BSS_CHANGED_BASIC_RATES)
981 rtl8180_conf_basic_rates(dev, info->basic_rates);
982
Andrea Merello9069af72014-03-15 18:29:37 +0100983 if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_ERP_PREAMBLE)) {
984
985 /* when preamble changes, acktime duration changes, and erp must
986 * be recalculated. ACK time is calculated at lowest rate.
987 * Since mac80211 include SIFS time we remove it (-10)
988 */
989 priv->ack_time =
990 le16_to_cpu(ieee80211_generic_frame_duration(dev,
991 priv->vif,
992 IEEE80211_BAND_2GHZ, 10,
993 &priv->rates[0])) - 10;
994
995 rtl8180_conf_erp(dev, info);
996 }
John W. Linvillec809e862010-05-06 16:49:40 -0400997
998 if (changed & BSS_CHANGED_BEACON_ENABLED)
999 vif_priv->enable_beacon = info->enable_beacon;
1000
1001 if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON)) {
1002 cancel_delayed_work_sync(&vif_priv->beacon_work);
1003 if (vif_priv->enable_beacon)
1004 schedule_work(&vif_priv->beacon_work.work);
1005 }
John W. Linvilleda81ded2008-11-12 14:37:11 -05001006}
1007
Jiri Pirko22bedad32010-04-01 21:22:57 +00001008static u64 rtl8180_prepare_multicast(struct ieee80211_hw *dev,
1009 struct netdev_hw_addr_list *mc_list)
Johannes Berg3ac64be2009-08-17 16:16:53 +02001010{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001011 return netdev_hw_addr_list_count(mc_list);
Johannes Berg3ac64be2009-08-17 16:16:53 +02001012}
1013
Michael Wuf6532112007-10-14 14:43:16 -04001014static void rtl8180_configure_filter(struct ieee80211_hw *dev,
1015 unsigned int changed_flags,
1016 unsigned int *total_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001017 u64 multicast)
Michael Wuf6532112007-10-14 14:43:16 -04001018{
1019 struct rtl8180_priv *priv = dev->priv;
1020
1021 if (changed_flags & FIF_FCSFAIL)
1022 priv->rx_conf ^= RTL818X_RX_CONF_FCS;
1023 if (changed_flags & FIF_CONTROL)
1024 priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
1025 if (changed_flags & FIF_OTHER_BSS)
1026 priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
Johannes Berg3ac64be2009-08-17 16:16:53 +02001027 if (*total_flags & FIF_ALLMULTI || multicast > 0)
Michael Wuf6532112007-10-14 14:43:16 -04001028 priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
1029 else
1030 priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
1031
1032 *total_flags = 0;
1033
1034 if (priv->rx_conf & RTL818X_RX_CONF_FCS)
1035 *total_flags |= FIF_FCSFAIL;
1036 if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
1037 *total_flags |= FIF_CONTROL;
1038 if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
1039 *total_flags |= FIF_OTHER_BSS;
1040 if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
1041 *total_flags |= FIF_ALLMULTI;
1042
1043 rtl818x_iowrite32(priv, &priv->map->RX_CONF, priv->rx_conf);
1044}
1045
1046static const struct ieee80211_ops rtl8180_ops = {
1047 .tx = rtl8180_tx,
1048 .start = rtl8180_start,
1049 .stop = rtl8180_stop,
1050 .add_interface = rtl8180_add_interface,
1051 .remove_interface = rtl8180_remove_interface,
1052 .config = rtl8180_config,
John W. Linvilleda81ded2008-11-12 14:37:11 -05001053 .bss_info_changed = rtl8180_bss_info_changed,
Andrea Merello9069af72014-03-15 18:29:37 +01001054 .conf_tx = rtl8180_conf_tx,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001055 .prepare_multicast = rtl8180_prepare_multicast,
Michael Wuf6532112007-10-14 14:43:16 -04001056 .configure_filter = rtl8180_configure_filter,
John W. Linvilled2bb8e02010-01-26 16:22:20 -05001057 .get_tsf = rtl8180_get_tsf,
Michael Wuf6532112007-10-14 14:43:16 -04001058};
1059
1060static void rtl8180_eeprom_register_read(struct eeprom_93cx6 *eeprom)
1061{
Andrea Merello7d4b8292014-03-15 18:29:38 +01001062 struct rtl8180_priv *priv = eeprom->data;
Michael Wuf6532112007-10-14 14:43:16 -04001063 u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1064
1065 eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
1066 eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
1067 eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
1068 eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
1069}
1070
1071static void rtl8180_eeprom_register_write(struct eeprom_93cx6 *eeprom)
1072{
Andrea Merello7d4b8292014-03-15 18:29:38 +01001073 struct rtl8180_priv *priv = eeprom->data;
Michael Wuf6532112007-10-14 14:43:16 -04001074 u8 reg = 2 << 6;
1075
1076 if (eeprom->reg_data_in)
1077 reg |= RTL818X_EEPROM_CMD_WRITE;
1078 if (eeprom->reg_data_out)
1079 reg |= RTL818X_EEPROM_CMD_READ;
1080 if (eeprom->reg_data_clock)
1081 reg |= RTL818X_EEPROM_CMD_CK;
1082 if (eeprom->reg_chip_select)
1083 reg |= RTL818X_EEPROM_CMD_CS;
1084
1085 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
1086 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1087 udelay(10);
1088}
1089
Andrea Merello7d4b8292014-03-15 18:29:38 +01001090static void rtl8180_eeprom_read(struct rtl8180_priv *priv)
1091{
1092 struct eeprom_93cx6 eeprom;
1093 int eeprom_cck_table_adr;
1094 u16 eeprom_val;
1095 int i;
1096
1097 eeprom.data = priv;
1098 eeprom.register_read = rtl8180_eeprom_register_read;
1099 eeprom.register_write = rtl8180_eeprom_register_write;
1100 if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
1101 eeprom.width = PCI_EEPROM_WIDTH_93C66;
1102 else
1103 eeprom.width = PCI_EEPROM_WIDTH_93C46;
1104
1105 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
1106 RTL818X_EEPROM_CMD_PROGRAM);
1107 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1108 udelay(10);
1109
1110 eeprom_93cx6_read(&eeprom, 0x06, &eeprom_val);
1111 eeprom_val &= 0xFF;
1112 priv->rf_type = eeprom_val;
1113
1114 eeprom_93cx6_read(&eeprom, 0x17, &eeprom_val);
1115 priv->csthreshold = eeprom_val >> 8;
1116
1117 eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)priv->mac_addr, 3);
1118
1119 eeprom_cck_table_adr = 0x10;
1120
1121 /* CCK TX power */
1122 for (i = 0; i < 14; i += 2) {
1123 u16 txpwr;
1124 eeprom_93cx6_read(&eeprom, eeprom_cck_table_adr + (i >> 1),
1125 &txpwr);
1126 priv->channels[i].hw_value = txpwr & 0xFF;
1127 priv->channels[i + 1].hw_value = txpwr >> 8;
1128 }
1129
1130 /* OFDM TX power */
1131 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
1132 for (i = 0; i < 14; i += 2) {
1133 u16 txpwr;
1134 eeprom_93cx6_read(&eeprom, 0x20 + (i >> 1), &txpwr);
1135 priv->channels[i].hw_value |= (txpwr & 0xFF) << 8;
1136 priv->channels[i + 1].hw_value |= txpwr & 0xFF00;
1137 }
1138 }
1139
1140 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
1141 __le32 anaparam;
1142 eeprom_93cx6_multiread(&eeprom, 0xD, (__le16 *)&anaparam, 2);
1143 priv->anaparam = le32_to_cpu(anaparam);
1144 eeprom_93cx6_read(&eeprom, 0x19, &priv->rfparam);
1145 }
1146
1147 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
1148 RTL818X_EEPROM_CMD_NORMAL);
1149}
1150
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001151static int rtl8180_probe(struct pci_dev *pdev,
Michael Wuf6532112007-10-14 14:43:16 -04001152 const struct pci_device_id *id)
1153{
1154 struct ieee80211_hw *dev;
1155 struct rtl8180_priv *priv;
1156 unsigned long mem_addr, mem_len;
1157 unsigned int io_addr, io_len;
Andrea Merello7d4b8292014-03-15 18:29:38 +01001158 int err;
Michael Wuf6532112007-10-14 14:43:16 -04001159 const char *chip_name, *rf_name = NULL;
1160 u32 reg;
Michael Wuf6532112007-10-14 14:43:16 -04001161
1162 err = pci_enable_device(pdev);
1163 if (err) {
1164 printk(KERN_ERR "%s (rtl8180): Cannot enable new PCI device\n",
1165 pci_name(pdev));
1166 return err;
1167 }
1168
1169 err = pci_request_regions(pdev, KBUILD_MODNAME);
1170 if (err) {
1171 printk(KERN_ERR "%s (rtl8180): Cannot obtain PCI resources\n",
1172 pci_name(pdev));
1173 return err;
1174 }
1175
1176 io_addr = pci_resource_start(pdev, 0);
1177 io_len = pci_resource_len(pdev, 0);
1178 mem_addr = pci_resource_start(pdev, 1);
1179 mem_len = pci_resource_len(pdev, 1);
1180
1181 if (mem_len < sizeof(struct rtl818x_csr) ||
1182 io_len < sizeof(struct rtl818x_csr)) {
1183 printk(KERN_ERR "%s (rtl8180): Too short PCI resources\n",
1184 pci_name(pdev));
1185 err = -ENOMEM;
1186 goto err_free_reg;
1187 }
1188
John W. Linville9e385c52010-05-10 14:24:34 -04001189 if ((err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) ||
1190 (err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))) {
Michael Wuf6532112007-10-14 14:43:16 -04001191 printk(KERN_ERR "%s (rtl8180): No suitable DMA available\n",
1192 pci_name(pdev));
1193 goto err_free_reg;
1194 }
1195
1196 pci_set_master(pdev);
1197
1198 dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8180_ops);
1199 if (!dev) {
1200 printk(KERN_ERR "%s (rtl8180): ieee80211 alloc failed\n",
1201 pci_name(pdev));
1202 err = -ENOMEM;
1203 goto err_free_reg;
1204 }
1205
1206 priv = dev->priv;
1207 priv->pdev = pdev;
1208
Johannes Berge6a98542008-10-21 12:40:02 +02001209 dev->max_rates = 2;
Michael Wuf6532112007-10-14 14:43:16 -04001210 SET_IEEE80211_DEV(dev, &pdev->dev);
1211 pci_set_drvdata(pdev, dev);
1212
1213 priv->map = pci_iomap(pdev, 1, mem_len);
1214 if (!priv->map)
1215 priv->map = pci_iomap(pdev, 0, io_len);
1216
1217 if (!priv->map) {
1218 printk(KERN_ERR "%s (rtl8180): Cannot map device memory\n",
1219 pci_name(pdev));
1220 goto err_free_dev;
1221 }
1222
Johannes Berg8318d782008-01-24 19:38:38 +01001223 BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
1224 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
1225
Michael Wuf6532112007-10-14 14:43:16 -04001226 memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
1227 memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
Johannes Berg8318d782008-01-24 19:38:38 +01001228
1229 priv->band.band = IEEE80211_BAND_2GHZ;
1230 priv->band.channels = priv->channels;
1231 priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
1232 priv->band.bitrates = priv->rates;
1233 priv->band.n_bitrates = 4;
1234 dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
1235
Michael Wuf6532112007-10-14 14:43:16 -04001236 dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
Bruno Randolf566bfe52008-05-08 19:15:40 +02001237 IEEE80211_HW_RX_INCLUDES_FCS |
1238 IEEE80211_HW_SIGNAL_UNSPEC;
John W. Linvillec809e862010-05-06 16:49:40 -04001239 dev->vif_data_size = sizeof(struct rtl8180_vif);
1240 dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1241 BIT(NL80211_IFTYPE_ADHOC);
Bruno Randolf566bfe52008-05-08 19:15:40 +02001242 dev->max_signal = 65;
Michael Wuf6532112007-10-14 14:43:16 -04001243
1244 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
1245 reg &= RTL818X_TX_CONF_HWVER_MASK;
1246 switch (reg) {
1247 case RTL818X_TX_CONF_R8180_ABCD:
1248 chip_name = "RTL8180";
Andrea Merello6caefd12014-03-08 18:36:37 +01001249 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
Michael Wuf6532112007-10-14 14:43:16 -04001250 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001251
Michael Wuf6532112007-10-14 14:43:16 -04001252 case RTL818X_TX_CONF_R8180_F:
1253 chip_name = "RTL8180vF";
Andrea Merello6caefd12014-03-08 18:36:37 +01001254 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
Michael Wuf6532112007-10-14 14:43:16 -04001255 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001256
Michael Wuf6532112007-10-14 14:43:16 -04001257 case RTL818X_TX_CONF_R8185_ABC:
1258 chip_name = "RTL8185";
Andrea Merello6caefd12014-03-08 18:36:37 +01001259 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
Michael Wuf6532112007-10-14 14:43:16 -04001260 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001261
Michael Wuf6532112007-10-14 14:43:16 -04001262 case RTL818X_TX_CONF_R8185_D:
1263 chip_name = "RTL8185vD";
Andrea Merello6caefd12014-03-08 18:36:37 +01001264 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
Michael Wuf6532112007-10-14 14:43:16 -04001265 break;
1266 default:
1267 printk(KERN_ERR "%s (rtl8180): Unknown chip! (0x%x)\n",
1268 pci_name(pdev), reg >> 25);
1269 goto err_iounmap;
1270 }
1271
Andrea Merellofd6564f2014-03-22 18:51:20 +01001272 /* we declare to MAC80211 all the queues except for beacon queue
1273 * that will be eventually handled by DRV.
1274 * TX rings are arranged in such a way that lower is the IDX,
1275 * higher is the priority, in order to achieve direct mapping
1276 * with mac80211, however the beacon queue is an exception and it
1277 * is mapped on the highst tx ring IDX.
1278 */
1279 dev->queues = RTL8180_NR_TX_QUEUES - 1;
1280
Andrea Merello6caefd12014-03-08 18:36:37 +01001281 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Johannes Berg8318d782008-01-24 19:38:38 +01001282 priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
Michael Wuf6532112007-10-14 14:43:16 -04001283 pci_try_set_mwi(pdev);
1284 }
1285
Andrea Merello7d4b8292014-03-15 18:29:38 +01001286 rtl8180_eeprom_read(priv);
Michael Wuf6532112007-10-14 14:43:16 -04001287
Andrea Merello7d4b8292014-03-15 18:29:38 +01001288 switch (priv->rf_type) {
Michael Wuf6532112007-10-14 14:43:16 -04001289 case 1: rf_name = "Intersil";
1290 break;
1291 case 2: rf_name = "RFMD";
1292 break;
1293 case 3: priv->rf = &sa2400_rf_ops;
1294 break;
1295 case 4: priv->rf = &max2820_rf_ops;
1296 break;
1297 case 5: priv->rf = &grf5101_rf_ops;
1298 break;
1299 case 9: priv->rf = rtl8180_detect_rf(dev);
1300 break;
1301 case 10:
1302 rf_name = "RTL8255";
1303 break;
1304 default:
1305 printk(KERN_ERR "%s (rtl8180): Unknown RF! (0x%x)\n",
Andrea Merello7d4b8292014-03-15 18:29:38 +01001306 pci_name(pdev), priv->rf_type);
Michael Wuf6532112007-10-14 14:43:16 -04001307 goto err_iounmap;
1308 }
1309
1310 if (!priv->rf) {
1311 printk(KERN_ERR "%s (rtl8180): %s RF frontend not supported!\n",
1312 pci_name(pdev), rf_name);
1313 goto err_iounmap;
1314 }
1315
Andrea Merello7d4b8292014-03-15 18:29:38 +01001316 if (!is_valid_ether_addr(priv->mac_addr)) {
Michael Wuf6532112007-10-14 14:43:16 -04001317 printk(KERN_WARNING "%s (rtl8180): Invalid hwaddr! Using"
1318 " randomly generated MAC addr\n", pci_name(pdev));
Andrea Merello7d4b8292014-03-15 18:29:38 +01001319 eth_random_addr(priv->mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001320 }
Andrea Merello7d4b8292014-03-15 18:29:38 +01001321 SET_IEEE80211_PERM_ADDR(dev, priv->mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001322
1323 spin_lock_init(&priv->lock);
1324
1325 err = ieee80211_register_hw(dev);
1326 if (err) {
1327 printk(KERN_ERR "%s (rtl8180): Cannot register device\n",
1328 pci_name(pdev));
1329 goto err_iounmap;
1330 }
1331
Joe Perchesc96c31e2010-07-26 14:39:58 -07001332 wiphy_info(dev->wiphy, "hwaddr %pm, %s + %s\n",
Andrea Merello7d4b8292014-03-15 18:29:38 +01001333 priv->mac_addr, chip_name, priv->rf->name);
Michael Wuf6532112007-10-14 14:43:16 -04001334
1335 return 0;
1336
1337 err_iounmap:
andrea merello0269da22014-02-18 02:10:41 +01001338 pci_iounmap(pdev, priv->map);
Michael Wuf6532112007-10-14 14:43:16 -04001339
1340 err_free_dev:
Michael Wuf6532112007-10-14 14:43:16 -04001341 ieee80211_free_hw(dev);
1342
1343 err_free_reg:
1344 pci_release_regions(pdev);
1345 pci_disable_device(pdev);
1346 return err;
1347}
1348
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001349static void rtl8180_remove(struct pci_dev *pdev)
Michael Wuf6532112007-10-14 14:43:16 -04001350{
1351 struct ieee80211_hw *dev = pci_get_drvdata(pdev);
1352 struct rtl8180_priv *priv;
1353
1354 if (!dev)
1355 return;
1356
1357 ieee80211_unregister_hw(dev);
1358
1359 priv = dev->priv;
1360
1361 pci_iounmap(pdev, priv->map);
1362 pci_release_regions(pdev);
1363 pci_disable_device(pdev);
1364 ieee80211_free_hw(dev);
1365}
1366
1367#ifdef CONFIG_PM
1368static int rtl8180_suspend(struct pci_dev *pdev, pm_message_t state)
1369{
1370 pci_save_state(pdev);
1371 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1372 return 0;
1373}
1374
1375static int rtl8180_resume(struct pci_dev *pdev)
1376{
1377 pci_set_power_state(pdev, PCI_D0);
1378 pci_restore_state(pdev);
1379 return 0;
1380}
1381
1382#endif /* CONFIG_PM */
1383
1384static struct pci_driver rtl8180_driver = {
1385 .name = KBUILD_MODNAME,
1386 .id_table = rtl8180_table,
1387 .probe = rtl8180_probe,
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001388 .remove = rtl8180_remove,
Michael Wuf6532112007-10-14 14:43:16 -04001389#ifdef CONFIG_PM
1390 .suspend = rtl8180_suspend,
1391 .resume = rtl8180_resume,
1392#endif /* CONFIG_PM */
1393};
1394
Axel Lin5b0a3b72012-04-14 10:38:36 +08001395module_pci_driver(rtl8180_driver);