blob: 767e1048f3368e18bd2ef6934377d6afc30d9c60 [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);
415
416 break;
417
418 case RTL818X_CHIP_FAMILY_RTL8185:
419 /* in 8185 this is a BITMAP */
420 rtl818x_iowrite16(priv, &priv->map->BRSR, rates_mask);
421 rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (max << 4) | min);
422 break;
423 }
424}
425
Michael Wuf6532112007-10-14 14:43:16 -0400426static int rtl8180_init_hw(struct ieee80211_hw *dev)
427{
428 struct rtl8180_priv *priv = dev->priv;
429 u16 reg;
430
431 rtl818x_iowrite8(priv, &priv->map->CMD, 0);
432 rtl818x_ioread8(priv, &priv->map->CMD);
433 msleep(10);
434
435 /* reset */
436 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
437 rtl818x_ioread8(priv, &priv->map->CMD);
438
439 reg = rtl818x_ioread8(priv, &priv->map->CMD);
440 reg &= (1 << 1);
441 reg |= RTL818X_CMD_RESET;
442 rtl818x_iowrite8(priv, &priv->map->CMD, RTL818X_CMD_RESET);
443 rtl818x_ioread8(priv, &priv->map->CMD);
444 msleep(200);
445
446 /* check success of reset */
447 if (rtl818x_ioread8(priv, &priv->map->CMD) & RTL818X_CMD_RESET) {
Joe Perchesc96c31e2010-07-26 14:39:58 -0700448 wiphy_err(dev->wiphy, "reset timeout!\n");
Michael Wuf6532112007-10-14 14:43:16 -0400449 return -ETIMEDOUT;
450 }
451
452 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
453 rtl818x_ioread8(priv, &priv->map->CMD);
454 msleep(200);
455
456 if (rtl818x_ioread8(priv, &priv->map->CONFIG3) & (1 << 3)) {
457 /* For cardbus */
458 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
459 reg |= 1 << 1;
460 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
461 reg = rtl818x_ioread16(priv, &priv->map->FEMR);
462 reg |= (1 << 15) | (1 << 14) | (1 << 4);
463 rtl818x_iowrite16(priv, &priv->map->FEMR, reg);
464 }
465
466 rtl818x_iowrite8(priv, &priv->map->MSR, 0);
467
Andrea Merello6caefd12014-03-08 18:36:37 +0100468 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
Michael Wuf6532112007-10-14 14:43:16 -0400469 rtl8180_set_anaparam(priv, priv->anaparam);
470
471 rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
Andrea Merellofd6564f2014-03-22 18:51:20 +0100472 rtl818x_iowrite32(priv, &priv->map->TBDA, priv->tx_ring[1].dma);
Michael Wuf6532112007-10-14 14:43:16 -0400473 rtl818x_iowrite32(priv, &priv->map->TLPDA, priv->tx_ring[0].dma);
474
475 /* TODO: necessary? specs indicate not */
476 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
477 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
478 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg & ~(1 << 3));
Andrea Merello6caefd12014-03-08 18:36:37 +0100479 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
Michael Wuf6532112007-10-14 14:43:16 -0400480 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
481 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg | (1 << 4));
482 }
483 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
484
485 /* TODO: set CONFIG5 for calibrating AGC on rtl8180 + philips radio? */
486
487 /* TODO: turn off hw wep on rtl8180 */
488
489 rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
490
Andrea Merello6caefd12014-03-08 18:36:37 +0100491 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400492 rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
493 rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
Michael Wuf6532112007-10-14 14:43:16 -0400494
495 /* TODO: set ClkRun enable? necessary? */
496 reg = rtl818x_ioread8(priv, &priv->map->GP_ENABLE);
497 rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, reg & ~(1 << 6));
498 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
499 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
500 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | (1 << 2));
501 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
502 } else {
Michael Wuf6532112007-10-14 14:43:16 -0400503 rtl818x_iowrite8(priv, &priv->map->SECURITY, 0);
504
505 rtl818x_iowrite8(priv, &priv->map->PHY_DELAY, 0x6);
506 rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, 0x4C);
507 }
508
509 priv->rf->init(dev);
Andrea Merello516a0932014-03-15 18:29:36 +0100510
511 /* default basic rates are 1,2 Mbps for rtl8180. 1,2,6,9,12,18,24 Mbps
512 * otherwise. bitmask 0x3 and 0x01f3 respectively.
513 * NOTE: currenty rtl8225 RF code changes basic rates, so we need to do
514 * this after rf init.
515 * TODO: try to find out whether RF code really needs to do this..
516 */
517 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
518 rtl8180_conf_basic_rates(dev, 0x3);
519 else
520 rtl8180_conf_basic_rates(dev, 0x1f3);
521
Michael Wuf6532112007-10-14 14:43:16 -0400522 return 0;
523}
524
525static int rtl8180_init_rx_ring(struct ieee80211_hw *dev)
526{
527 struct rtl8180_priv *priv = dev->priv;
528 struct rtl8180_rx_desc *entry;
529 int i;
530
531 priv->rx_ring = pci_alloc_consistent(priv->pdev,
532 sizeof(*priv->rx_ring) * 32,
533 &priv->rx_ring_dma);
534
535 if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -0700536 wiphy_err(dev->wiphy, "Cannot allocate RX ring\n");
Michael Wuf6532112007-10-14 14:43:16 -0400537 return -ENOMEM;
538 }
539
540 memset(priv->rx_ring, 0, sizeof(*priv->rx_ring) * 32);
541 priv->rx_idx = 0;
542
543 for (i = 0; i < 32; i++) {
544 struct sk_buff *skb = dev_alloc_skb(MAX_RX_SIZE);
545 dma_addr_t *mapping;
546 entry = &priv->rx_ring[i];
andrea merello4da18bb2014-02-18 02:10:43 +0100547 if (!skb) {
548 wiphy_err(dev->wiphy, "Cannot allocate RX skb\n");
549 return -ENOMEM;
550 }
Michael Wuf6532112007-10-14 14:43:16 -0400551 priv->rx_buf[i] = skb;
552 mapping = (dma_addr_t *)skb->cb;
553 *mapping = pci_map_single(priv->pdev, skb_tail_pointer(skb),
554 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
andrea merelloec1da082014-02-22 17:57:23 +0100555
556 if (pci_dma_mapping_error(priv->pdev, *mapping)) {
557 kfree_skb(skb);
558 wiphy_err(dev->wiphy, "Cannot map DMA for RX skb\n");
559 return -ENOMEM;
560 }
561
Michael Wuf6532112007-10-14 14:43:16 -0400562 entry->rx_buf = cpu_to_le32(*mapping);
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300563 entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
Michael Wuf6532112007-10-14 14:43:16 -0400564 MAX_RX_SIZE);
565 }
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300566 entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
Michael Wuf6532112007-10-14 14:43:16 -0400567 return 0;
568}
569
570static void rtl8180_free_rx_ring(struct ieee80211_hw *dev)
571{
572 struct rtl8180_priv *priv = dev->priv;
573 int i;
574
575 for (i = 0; i < 32; i++) {
576 struct sk_buff *skb = priv->rx_buf[i];
577 if (!skb)
578 continue;
579
580 pci_unmap_single(priv->pdev,
581 *((dma_addr_t *)skb->cb),
582 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
583 kfree_skb(skb);
584 }
585
586 pci_free_consistent(priv->pdev, sizeof(*priv->rx_ring) * 32,
587 priv->rx_ring, priv->rx_ring_dma);
588 priv->rx_ring = NULL;
589}
590
591static int rtl8180_init_tx_ring(struct ieee80211_hw *dev,
592 unsigned int prio, unsigned int entries)
593{
594 struct rtl8180_priv *priv = dev->priv;
595 struct rtl8180_tx_desc *ring;
596 dma_addr_t dma;
597 int i;
598
599 ring = pci_alloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
600 if (!ring || (unsigned long)ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -0700601 wiphy_err(dev->wiphy, "Cannot allocate TX ring (prio = %d)\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -0700602 prio);
Michael Wuf6532112007-10-14 14:43:16 -0400603 return -ENOMEM;
604 }
605
606 memset(ring, 0, sizeof(*ring)*entries);
607 priv->tx_ring[prio].desc = ring;
608 priv->tx_ring[prio].dma = dma;
609 priv->tx_ring[prio].idx = 0;
610 priv->tx_ring[prio].entries = entries;
611 skb_queue_head_init(&priv->tx_ring[prio].queue);
612
613 for (i = 0; i < entries; i++)
614 ring[i].next_tx_desc =
615 cpu_to_le32((u32)dma + ((i + 1) % entries) * sizeof(*ring));
616
617 return 0;
618}
619
620static void rtl8180_free_tx_ring(struct ieee80211_hw *dev, unsigned int prio)
621{
622 struct rtl8180_priv *priv = dev->priv;
623 struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
624
625 while (skb_queue_len(&ring->queue)) {
626 struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
627 struct sk_buff *skb = __skb_dequeue(&ring->queue);
628
629 pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
630 skb->len, PCI_DMA_TODEVICE);
Michael Wuf6532112007-10-14 14:43:16 -0400631 kfree_skb(skb);
632 ring->idx = (ring->idx + 1) % ring->entries;
633 }
634
635 pci_free_consistent(priv->pdev, sizeof(*ring->desc)*ring->entries,
636 ring->desc, ring->dma);
637 ring->desc = NULL;
638}
639
640static int rtl8180_start(struct ieee80211_hw *dev)
641{
642 struct rtl8180_priv *priv = dev->priv;
643 int ret, i;
644 u32 reg;
645
646 ret = rtl8180_init_rx_ring(dev);
647 if (ret)
648 return ret;
649
Andrea Merellofd6564f2014-03-22 18:51:20 +0100650 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -0400651 if ((ret = rtl8180_init_tx_ring(dev, i, 16)))
652 goto err_free_rings;
653
654 ret = rtl8180_init_hw(dev);
655 if (ret)
656 goto err_free_rings;
657
Julia Lawallea31ba32009-11-18 08:26:02 +0000658 ret = request_irq(priv->pdev->irq, rtl8180_interrupt,
Michael Wuf6532112007-10-14 14:43:16 -0400659 IRQF_SHARED, KBUILD_MODNAME, dev);
660 if (ret) {
Joe Perches5db55842010-08-11 19:11:19 -0700661 wiphy_err(dev->wiphy, "failed to register IRQ handler\n");
Michael Wuf6532112007-10-14 14:43:16 -0400662 goto err_free_rings;
663 }
664
665 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
666
667 rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
668 rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
669
670 reg = RTL818X_RX_CONF_ONLYERLPKT |
671 RTL818X_RX_CONF_RX_AUTORESETPHY |
672 RTL818X_RX_CONF_MGMT |
673 RTL818X_RX_CONF_DATA |
674 (7 << 8 /* MAX RX DMA */) |
675 RTL818X_RX_CONF_BROADCAST |
676 RTL818X_RX_CONF_NICMAC;
677
Andrea Merello6caefd12014-03-08 18:36:37 +0100678 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185)
Michael Wuf6532112007-10-14 14:43:16 -0400679 reg |= RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2;
680 else {
681 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE1)
682 ? RTL818X_RX_CONF_CSDM1 : 0;
683 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE2)
684 ? RTL818X_RX_CONF_CSDM2 : 0;
685 }
686
687 priv->rx_conf = reg;
688 rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
689
Andrea Merello6caefd12014-03-08 18:36:37 +0100690 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400691 reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
andrea merello14c76152014-02-18 02:10:44 +0100692
693 /* CW is not on per-packet basis.
694 * in rtl8185 the CW_VALUE reg is used.
695 */
andrea merello6f7343d2014-01-21 20:16:43 +0100696 reg &= ~RTL818X_CW_CONF_PERPACKET_CW;
andrea merello14c76152014-02-18 02:10:44 +0100697 /* retry limit IS on per-packet basis.
698 * the short and long retry limit in TX_CONF
699 * reg are ignored
700 */
andrea merello6f7343d2014-01-21 20:16:43 +0100701 reg |= RTL818X_CW_CONF_PERPACKET_RETRY;
Michael Wuf6532112007-10-14 14:43:16 -0400702 rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
703
704 reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
andrea merello14c76152014-02-18 02:10:44 +0100705 /* TX antenna and TX gain are not on per-packet basis.
706 * TX Antenna is selected by ANTSEL reg (RX in BB regs).
707 * TX gain is selected with CCK_TX_AGC and OFDM_TX_AGC regs
708 */
andrea merello6f7343d2014-01-21 20:16:43 +0100709 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN;
710 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL;
Michael Wuf6532112007-10-14 14:43:16 -0400711 reg |= RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
712 rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
713
714 /* disable early TX */
715 rtl818x_iowrite8(priv, (u8 __iomem *)priv->map + 0xec, 0x3f);
716 }
717
718 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
719 reg |= (6 << 21 /* MAX TX DMA */) |
720 RTL818X_TX_CONF_NO_ICV;
721
Andrea Merello6caefd12014-03-08 18:36:37 +0100722
723
724 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
Michael Wuf6532112007-10-14 14:43:16 -0400725 reg &= ~RTL818X_TX_CONF_PROBE_DTS;
726 else
727 reg &= ~RTL818X_TX_CONF_HW_SEQNUM;
728
andrea merelloe74075a2014-02-18 02:10:40 +0100729 reg &= ~RTL818X_TX_CONF_DISCW;
730
Michael Wuf6532112007-10-14 14:43:16 -0400731 /* different meaning, same value on both rtl8185 and rtl8180 */
732 reg &= ~RTL818X_TX_CONF_SAT_HWPLCP;
733
734 rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
735
736 reg = rtl818x_ioread8(priv, &priv->map->CMD);
737 reg |= RTL818X_CMD_RX_ENABLE;
738 reg |= RTL818X_CMD_TX_ENABLE;
739 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
740
Michael Wuf6532112007-10-14 14:43:16 -0400741 return 0;
742
743 err_free_rings:
744 rtl8180_free_rx_ring(dev);
Andrea Merellofd6564f2014-03-22 18:51:20 +0100745 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -0400746 if (priv->tx_ring[i].desc)
747 rtl8180_free_tx_ring(dev, i);
748
749 return ret;
750}
751
752static void rtl8180_stop(struct ieee80211_hw *dev)
753{
754 struct rtl8180_priv *priv = dev->priv;
755 u8 reg;
756 int i;
757
Michael Wuf6532112007-10-14 14:43:16 -0400758 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
759
760 reg = rtl818x_ioread8(priv, &priv->map->CMD);
761 reg &= ~RTL818X_CMD_TX_ENABLE;
762 reg &= ~RTL818X_CMD_RX_ENABLE;
763 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
764
765 priv->rf->stop(dev);
766
767 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
768 reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
769 rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
770 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
771
772 free_irq(priv->pdev->irq, dev);
773
774 rtl8180_free_rx_ring(dev);
Andrea Merellofd6564f2014-03-22 18:51:20 +0100775 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -0400776 rtl8180_free_tx_ring(dev, i);
777}
778
Eliad Peller37a41b42011-09-21 14:06:11 +0300779static u64 rtl8180_get_tsf(struct ieee80211_hw *dev,
780 struct ieee80211_vif *vif)
John W. Linvillec809e862010-05-06 16:49:40 -0400781{
782 struct rtl8180_priv *priv = dev->priv;
783
784 return rtl818x_ioread32(priv, &priv->map->TSFT[0]) |
785 (u64)(rtl818x_ioread32(priv, &priv->map->TSFT[1])) << 32;
786}
787
John W. Linvillea3275e22010-06-24 11:08:37 -0400788static void rtl8180_beacon_work(struct work_struct *work)
John W. Linvillec809e862010-05-06 16:49:40 -0400789{
790 struct rtl8180_vif *vif_priv =
791 container_of(work, struct rtl8180_vif, beacon_work.work);
792 struct ieee80211_vif *vif =
793 container_of((void *)vif_priv, struct ieee80211_vif, drv_priv);
794 struct ieee80211_hw *dev = vif_priv->dev;
795 struct ieee80211_mgmt *mgmt;
796 struct sk_buff *skb;
John W. Linvillec809e862010-05-06 16:49:40 -0400797
798 /* don't overflow the tx ring */
799 if (ieee80211_queue_stopped(dev, 0))
800 goto resched;
801
802 /* grab a fresh beacon */
803 skb = ieee80211_beacon_get(dev, vif);
John W. Linville8f1d2d22010-08-05 13:46:27 -0400804 if (!skb)
805 goto resched;
John W. Linvillec809e862010-05-06 16:49:40 -0400806
807 /*
808 * update beacon timestamp w/ TSF value
809 * TODO: make hardware update beacon timestamp
810 */
811 mgmt = (struct ieee80211_mgmt *)skb->data;
Eliad Peller37a41b42011-09-21 14:06:11 +0300812 mgmt->u.beacon.timestamp = cpu_to_le64(rtl8180_get_tsf(dev, vif));
John W. Linvillec809e862010-05-06 16:49:40 -0400813
814 /* TODO: use actual beacon queue */
815 skb_set_queue_mapping(skb, 0);
816
Thomas Huehn36323f82012-07-23 21:33:42 +0200817 rtl8180_tx(dev, NULL, skb);
John W. Linvillec809e862010-05-06 16:49:40 -0400818
819resched:
820 /*
821 * schedule next beacon
822 * TODO: use hardware support for beacon timing
823 */
824 schedule_delayed_work(&vif_priv->beacon_work,
825 usecs_to_jiffies(1024 * vif->bss_conf.beacon_int));
826}
827
Michael Wuf6532112007-10-14 14:43:16 -0400828static int rtl8180_add_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100829 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -0400830{
831 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -0400832 struct rtl8180_vif *vif_priv;
Michael Wuf6532112007-10-14 14:43:16 -0400833
John W. Linville643aab62009-12-22 18:13:04 -0500834 /*
835 * We only support one active interface at a time.
836 */
837 if (priv->vif)
838 return -EBUSY;
Michael Wuf6532112007-10-14 14:43:16 -0400839
Johannes Berg1ed32e42009-12-23 13:15:45 +0100840 switch (vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200841 case NL80211_IFTYPE_STATION:
John W. Linvillec809e862010-05-06 16:49:40 -0400842 case NL80211_IFTYPE_ADHOC:
Michael Wuf6532112007-10-14 14:43:16 -0400843 break;
844 default:
845 return -EOPNOTSUPP;
846 }
847
Johannes Berg1ed32e42009-12-23 13:15:45 +0100848 priv->vif = vif;
Johannes Berg32bfd352007-12-19 01:31:26 +0100849
John W. Linvillec809e862010-05-06 16:49:40 -0400850 /* Initialize driver private area */
851 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
852 vif_priv->dev = dev;
853 INIT_DELAYED_WORK(&vif_priv->beacon_work, rtl8180_beacon_work);
854 vif_priv->enable_beacon = false;
855
Michael Wuf6532112007-10-14 14:43:16 -0400856 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
857 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->MAC[0],
Johannes Berg1ed32e42009-12-23 13:15:45 +0100858 le32_to_cpu(*(__le32 *)vif->addr));
Michael Wuf6532112007-10-14 14:43:16 -0400859 rtl818x_iowrite16(priv, (__le16 __iomem *)&priv->map->MAC[4],
Johannes Berg1ed32e42009-12-23 13:15:45 +0100860 le16_to_cpu(*(__le16 *)(vif->addr + 4)));
Michael Wuf6532112007-10-14 14:43:16 -0400861 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
862
863 return 0;
864}
865
866static void rtl8180_remove_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100867 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -0400868{
869 struct rtl8180_priv *priv = dev->priv;
Johannes Berg32bfd352007-12-19 01:31:26 +0100870 priv->vif = NULL;
Michael Wuf6532112007-10-14 14:43:16 -0400871}
872
Johannes Berge8975582008-10-09 12:18:51 +0200873static int rtl8180_config(struct ieee80211_hw *dev, u32 changed)
Michael Wuf6532112007-10-14 14:43:16 -0400874{
875 struct rtl8180_priv *priv = dev->priv;
Johannes Berge8975582008-10-09 12:18:51 +0200876 struct ieee80211_conf *conf = &dev->conf;
Michael Wuf6532112007-10-14 14:43:16 -0400877
878 priv->rf->set_chan(dev, conf);
879
880 return 0;
881}
882
Andrea Merello9069af72014-03-15 18:29:37 +0100883static int rtl8180_conf_tx(struct ieee80211_hw *dev,
884 struct ieee80211_vif *vif, u16 queue,
885 const struct ieee80211_tx_queue_params *params)
886{
887 struct rtl8180_priv *priv = dev->priv;
888 u8 cw_min, cw_max;
889
890 /* nothing to do ? */
891 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
892 return 0;
893
894 cw_min = fls(params->cw_min);
895 cw_max = fls(params->cw_max);
896
897 rtl818x_iowrite8(priv, &priv->map->CW_VAL, (cw_max << 4) | cw_min);
898
899 return 0;
900}
901
902static void rtl8180_conf_erp(struct ieee80211_hw *dev,
903 struct ieee80211_bss_conf *info)
904{
905 struct rtl8180_priv *priv = dev->priv;
906 u8 sifs, difs;
907 int eifs;
908 u8 hw_eifs;
909
910 /* TODO: should we do something ? */
911 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
912 return;
913
914 /* I _hope_ this means 10uS for the HW.
915 * In reference code it is 0x22 for
916 * both rtl8187L and rtl8187SE
917 */
918 sifs = 0x22;
919
920 if (info->use_short_slot)
921 priv->slot_time = 9;
922 else
923 priv->slot_time = 20;
924
925 /* 10 is SIFS time in uS */
926 difs = 10 + 2 * priv->slot_time;
927 eifs = 10 + difs + priv->ack_time;
928
929 /* HW should use 4uS units for EIFS (I'm sure for rtl8185)*/
930 hw_eifs = DIV_ROUND_UP(eifs, 4);
931
932
933 rtl818x_iowrite8(priv, &priv->map->SLOT, priv->slot_time);
934 rtl818x_iowrite8(priv, &priv->map->SIFS, sifs);
935 rtl818x_iowrite8(priv, &priv->map->DIFS, difs);
936
937 /* from reference code. set ack timeout reg = eifs reg */
938 rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, hw_eifs);
939
940 /* rtl8187/rtl8185 HW bug. After EIFS is elapsed,
941 * the HW still wait for DIFS.
942 * HW uses 4uS units for EIFS.
943 */
944 hw_eifs = DIV_ROUND_UP(eifs - difs, 4);
945
946 rtl818x_iowrite8(priv, &priv->map->EIFS, hw_eifs);
947}
948
John W. Linvilleda81ded2008-11-12 14:37:11 -0500949static void rtl8180_bss_info_changed(struct ieee80211_hw *dev,
950 struct ieee80211_vif *vif,
951 struct ieee80211_bss_conf *info,
952 u32 changed)
953{
954 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -0400955 struct rtl8180_vif *vif_priv;
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200956 int i;
John W. Linville0f956e72010-07-29 21:50:29 -0400957 u8 reg;
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200958
John W. Linvillec809e862010-05-06 16:49:40 -0400959 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
960
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200961 if (changed & BSS_CHANGED_BSSID) {
962 for (i = 0; i < ETH_ALEN; i++)
963 rtl818x_iowrite8(priv, &priv->map->BSSID[i],
964 info->bssid[i]);
965
John W. Linville0f956e72010-07-29 21:50:29 -0400966 if (is_valid_ether_addr(info->bssid)) {
967 if (vif->type == NL80211_IFTYPE_ADHOC)
968 reg = RTL818X_MSR_ADHOC;
969 else
970 reg = RTL818X_MSR_INFRA;
971 } else
972 reg = RTL818X_MSR_NO_LINK;
973 rtl818x_iowrite8(priv, &priv->map->MSR, reg);
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200974 }
John W. Linvilleda81ded2008-11-12 14:37:11 -0500975
Andrea Merello516a0932014-03-15 18:29:36 +0100976 if (changed & BSS_CHANGED_BASIC_RATES)
977 rtl8180_conf_basic_rates(dev, info->basic_rates);
978
Andrea Merello9069af72014-03-15 18:29:37 +0100979 if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_ERP_PREAMBLE)) {
980
981 /* when preamble changes, acktime duration changes, and erp must
982 * be recalculated. ACK time is calculated at lowest rate.
983 * Since mac80211 include SIFS time we remove it (-10)
984 */
985 priv->ack_time =
986 le16_to_cpu(ieee80211_generic_frame_duration(dev,
987 priv->vif,
988 IEEE80211_BAND_2GHZ, 10,
989 &priv->rates[0])) - 10;
990
991 rtl8180_conf_erp(dev, info);
992 }
John W. Linvillec809e862010-05-06 16:49:40 -0400993
994 if (changed & BSS_CHANGED_BEACON_ENABLED)
995 vif_priv->enable_beacon = info->enable_beacon;
996
997 if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON)) {
998 cancel_delayed_work_sync(&vif_priv->beacon_work);
999 if (vif_priv->enable_beacon)
1000 schedule_work(&vif_priv->beacon_work.work);
1001 }
John W. Linvilleda81ded2008-11-12 14:37:11 -05001002}
1003
Jiri Pirko22bedad32010-04-01 21:22:57 +00001004static u64 rtl8180_prepare_multicast(struct ieee80211_hw *dev,
1005 struct netdev_hw_addr_list *mc_list)
Johannes Berg3ac64be2009-08-17 16:16:53 +02001006{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001007 return netdev_hw_addr_list_count(mc_list);
Johannes Berg3ac64be2009-08-17 16:16:53 +02001008}
1009
Michael Wuf6532112007-10-14 14:43:16 -04001010static void rtl8180_configure_filter(struct ieee80211_hw *dev,
1011 unsigned int changed_flags,
1012 unsigned int *total_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001013 u64 multicast)
Michael Wuf6532112007-10-14 14:43:16 -04001014{
1015 struct rtl8180_priv *priv = dev->priv;
1016
1017 if (changed_flags & FIF_FCSFAIL)
1018 priv->rx_conf ^= RTL818X_RX_CONF_FCS;
1019 if (changed_flags & FIF_CONTROL)
1020 priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
1021 if (changed_flags & FIF_OTHER_BSS)
1022 priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
Johannes Berg3ac64be2009-08-17 16:16:53 +02001023 if (*total_flags & FIF_ALLMULTI || multicast > 0)
Michael Wuf6532112007-10-14 14:43:16 -04001024 priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
1025 else
1026 priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
1027
1028 *total_flags = 0;
1029
1030 if (priv->rx_conf & RTL818X_RX_CONF_FCS)
1031 *total_flags |= FIF_FCSFAIL;
1032 if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
1033 *total_flags |= FIF_CONTROL;
1034 if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
1035 *total_flags |= FIF_OTHER_BSS;
1036 if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
1037 *total_flags |= FIF_ALLMULTI;
1038
1039 rtl818x_iowrite32(priv, &priv->map->RX_CONF, priv->rx_conf);
1040}
1041
1042static const struct ieee80211_ops rtl8180_ops = {
1043 .tx = rtl8180_tx,
1044 .start = rtl8180_start,
1045 .stop = rtl8180_stop,
1046 .add_interface = rtl8180_add_interface,
1047 .remove_interface = rtl8180_remove_interface,
1048 .config = rtl8180_config,
John W. Linvilleda81ded2008-11-12 14:37:11 -05001049 .bss_info_changed = rtl8180_bss_info_changed,
Andrea Merello9069af72014-03-15 18:29:37 +01001050 .conf_tx = rtl8180_conf_tx,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001051 .prepare_multicast = rtl8180_prepare_multicast,
Michael Wuf6532112007-10-14 14:43:16 -04001052 .configure_filter = rtl8180_configure_filter,
John W. Linvilled2bb8e02010-01-26 16:22:20 -05001053 .get_tsf = rtl8180_get_tsf,
Michael Wuf6532112007-10-14 14:43:16 -04001054};
1055
1056static void rtl8180_eeprom_register_read(struct eeprom_93cx6 *eeprom)
1057{
Andrea Merello7d4b8292014-03-15 18:29:38 +01001058 struct rtl8180_priv *priv = eeprom->data;
Michael Wuf6532112007-10-14 14:43:16 -04001059 u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1060
1061 eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
1062 eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
1063 eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
1064 eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
1065}
1066
1067static void rtl8180_eeprom_register_write(struct eeprom_93cx6 *eeprom)
1068{
Andrea Merello7d4b8292014-03-15 18:29:38 +01001069 struct rtl8180_priv *priv = eeprom->data;
Michael Wuf6532112007-10-14 14:43:16 -04001070 u8 reg = 2 << 6;
1071
1072 if (eeprom->reg_data_in)
1073 reg |= RTL818X_EEPROM_CMD_WRITE;
1074 if (eeprom->reg_data_out)
1075 reg |= RTL818X_EEPROM_CMD_READ;
1076 if (eeprom->reg_data_clock)
1077 reg |= RTL818X_EEPROM_CMD_CK;
1078 if (eeprom->reg_chip_select)
1079 reg |= RTL818X_EEPROM_CMD_CS;
1080
1081 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
1082 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1083 udelay(10);
1084}
1085
Andrea Merello7d4b8292014-03-15 18:29:38 +01001086static void rtl8180_eeprom_read(struct rtl8180_priv *priv)
1087{
1088 struct eeprom_93cx6 eeprom;
1089 int eeprom_cck_table_adr;
1090 u16 eeprom_val;
1091 int i;
1092
1093 eeprom.data = priv;
1094 eeprom.register_read = rtl8180_eeprom_register_read;
1095 eeprom.register_write = rtl8180_eeprom_register_write;
1096 if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
1097 eeprom.width = PCI_EEPROM_WIDTH_93C66;
1098 else
1099 eeprom.width = PCI_EEPROM_WIDTH_93C46;
1100
1101 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
1102 RTL818X_EEPROM_CMD_PROGRAM);
1103 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1104 udelay(10);
1105
1106 eeprom_93cx6_read(&eeprom, 0x06, &eeprom_val);
1107 eeprom_val &= 0xFF;
1108 priv->rf_type = eeprom_val;
1109
1110 eeprom_93cx6_read(&eeprom, 0x17, &eeprom_val);
1111 priv->csthreshold = eeprom_val >> 8;
1112
1113 eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)priv->mac_addr, 3);
1114
1115 eeprom_cck_table_adr = 0x10;
1116
1117 /* CCK TX power */
1118 for (i = 0; i < 14; i += 2) {
1119 u16 txpwr;
1120 eeprom_93cx6_read(&eeprom, eeprom_cck_table_adr + (i >> 1),
1121 &txpwr);
1122 priv->channels[i].hw_value = txpwr & 0xFF;
1123 priv->channels[i + 1].hw_value = txpwr >> 8;
1124 }
1125
1126 /* OFDM TX power */
1127 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
1128 for (i = 0; i < 14; i += 2) {
1129 u16 txpwr;
1130 eeprom_93cx6_read(&eeprom, 0x20 + (i >> 1), &txpwr);
1131 priv->channels[i].hw_value |= (txpwr & 0xFF) << 8;
1132 priv->channels[i + 1].hw_value |= txpwr & 0xFF00;
1133 }
1134 }
1135
1136 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
1137 __le32 anaparam;
1138 eeprom_93cx6_multiread(&eeprom, 0xD, (__le16 *)&anaparam, 2);
1139 priv->anaparam = le32_to_cpu(anaparam);
1140 eeprom_93cx6_read(&eeprom, 0x19, &priv->rfparam);
1141 }
1142
1143 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
1144 RTL818X_EEPROM_CMD_NORMAL);
1145}
1146
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001147static int rtl8180_probe(struct pci_dev *pdev,
Michael Wuf6532112007-10-14 14:43:16 -04001148 const struct pci_device_id *id)
1149{
1150 struct ieee80211_hw *dev;
1151 struct rtl8180_priv *priv;
1152 unsigned long mem_addr, mem_len;
1153 unsigned int io_addr, io_len;
Andrea Merello7d4b8292014-03-15 18:29:38 +01001154 int err;
Michael Wuf6532112007-10-14 14:43:16 -04001155 const char *chip_name, *rf_name = NULL;
1156 u32 reg;
Michael Wuf6532112007-10-14 14:43:16 -04001157
1158 err = pci_enable_device(pdev);
1159 if (err) {
1160 printk(KERN_ERR "%s (rtl8180): Cannot enable new PCI device\n",
1161 pci_name(pdev));
1162 return err;
1163 }
1164
1165 err = pci_request_regions(pdev, KBUILD_MODNAME);
1166 if (err) {
1167 printk(KERN_ERR "%s (rtl8180): Cannot obtain PCI resources\n",
1168 pci_name(pdev));
1169 return err;
1170 }
1171
1172 io_addr = pci_resource_start(pdev, 0);
1173 io_len = pci_resource_len(pdev, 0);
1174 mem_addr = pci_resource_start(pdev, 1);
1175 mem_len = pci_resource_len(pdev, 1);
1176
1177 if (mem_len < sizeof(struct rtl818x_csr) ||
1178 io_len < sizeof(struct rtl818x_csr)) {
1179 printk(KERN_ERR "%s (rtl8180): Too short PCI resources\n",
1180 pci_name(pdev));
1181 err = -ENOMEM;
1182 goto err_free_reg;
1183 }
1184
John W. Linville9e385c52010-05-10 14:24:34 -04001185 if ((err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) ||
1186 (err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))) {
Michael Wuf6532112007-10-14 14:43:16 -04001187 printk(KERN_ERR "%s (rtl8180): No suitable DMA available\n",
1188 pci_name(pdev));
1189 goto err_free_reg;
1190 }
1191
1192 pci_set_master(pdev);
1193
1194 dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8180_ops);
1195 if (!dev) {
1196 printk(KERN_ERR "%s (rtl8180): ieee80211 alloc failed\n",
1197 pci_name(pdev));
1198 err = -ENOMEM;
1199 goto err_free_reg;
1200 }
1201
1202 priv = dev->priv;
1203 priv->pdev = pdev;
1204
Johannes Berge6a98542008-10-21 12:40:02 +02001205 dev->max_rates = 2;
Michael Wuf6532112007-10-14 14:43:16 -04001206 SET_IEEE80211_DEV(dev, &pdev->dev);
1207 pci_set_drvdata(pdev, dev);
1208
1209 priv->map = pci_iomap(pdev, 1, mem_len);
1210 if (!priv->map)
1211 priv->map = pci_iomap(pdev, 0, io_len);
1212
1213 if (!priv->map) {
1214 printk(KERN_ERR "%s (rtl8180): Cannot map device memory\n",
1215 pci_name(pdev));
1216 goto err_free_dev;
1217 }
1218
Johannes Berg8318d782008-01-24 19:38:38 +01001219 BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
1220 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
1221
Michael Wuf6532112007-10-14 14:43:16 -04001222 memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
1223 memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
Johannes Berg8318d782008-01-24 19:38:38 +01001224
1225 priv->band.band = IEEE80211_BAND_2GHZ;
1226 priv->band.channels = priv->channels;
1227 priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
1228 priv->band.bitrates = priv->rates;
1229 priv->band.n_bitrates = 4;
1230 dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
1231
Michael Wuf6532112007-10-14 14:43:16 -04001232 dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
Bruno Randolf566bfe52008-05-08 19:15:40 +02001233 IEEE80211_HW_RX_INCLUDES_FCS |
1234 IEEE80211_HW_SIGNAL_UNSPEC;
John W. Linvillec809e862010-05-06 16:49:40 -04001235 dev->vif_data_size = sizeof(struct rtl8180_vif);
1236 dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1237 BIT(NL80211_IFTYPE_ADHOC);
Bruno Randolf566bfe52008-05-08 19:15:40 +02001238 dev->max_signal = 65;
Michael Wuf6532112007-10-14 14:43:16 -04001239
1240 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
1241 reg &= RTL818X_TX_CONF_HWVER_MASK;
1242 switch (reg) {
1243 case RTL818X_TX_CONF_R8180_ABCD:
1244 chip_name = "RTL8180";
Andrea Merello6caefd12014-03-08 18:36:37 +01001245 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
Michael Wuf6532112007-10-14 14:43:16 -04001246 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001247
Michael Wuf6532112007-10-14 14:43:16 -04001248 case RTL818X_TX_CONF_R8180_F:
1249 chip_name = "RTL8180vF";
Andrea Merello6caefd12014-03-08 18:36:37 +01001250 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
Michael Wuf6532112007-10-14 14:43:16 -04001251 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001252
Michael Wuf6532112007-10-14 14:43:16 -04001253 case RTL818X_TX_CONF_R8185_ABC:
1254 chip_name = "RTL8185";
Andrea Merello6caefd12014-03-08 18:36:37 +01001255 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
Michael Wuf6532112007-10-14 14:43:16 -04001256 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001257
Michael Wuf6532112007-10-14 14:43:16 -04001258 case RTL818X_TX_CONF_R8185_D:
1259 chip_name = "RTL8185vD";
Andrea Merello6caefd12014-03-08 18:36:37 +01001260 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
Michael Wuf6532112007-10-14 14:43:16 -04001261 break;
1262 default:
1263 printk(KERN_ERR "%s (rtl8180): Unknown chip! (0x%x)\n",
1264 pci_name(pdev), reg >> 25);
1265 goto err_iounmap;
1266 }
1267
Andrea Merellofd6564f2014-03-22 18:51:20 +01001268 /* we declare to MAC80211 all the queues except for beacon queue
1269 * that will be eventually handled by DRV.
1270 * TX rings are arranged in such a way that lower is the IDX,
1271 * higher is the priority, in order to achieve direct mapping
1272 * with mac80211, however the beacon queue is an exception and it
1273 * is mapped on the highst tx ring IDX.
1274 */
1275 dev->queues = RTL8180_NR_TX_QUEUES - 1;
1276
Andrea Merello6caefd12014-03-08 18:36:37 +01001277 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Johannes Berg8318d782008-01-24 19:38:38 +01001278 priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
Michael Wuf6532112007-10-14 14:43:16 -04001279 pci_try_set_mwi(pdev);
1280 }
1281
Andrea Merello7d4b8292014-03-15 18:29:38 +01001282 rtl8180_eeprom_read(priv);
Michael Wuf6532112007-10-14 14:43:16 -04001283
Andrea Merello7d4b8292014-03-15 18:29:38 +01001284 switch (priv->rf_type) {
Michael Wuf6532112007-10-14 14:43:16 -04001285 case 1: rf_name = "Intersil";
1286 break;
1287 case 2: rf_name = "RFMD";
1288 break;
1289 case 3: priv->rf = &sa2400_rf_ops;
1290 break;
1291 case 4: priv->rf = &max2820_rf_ops;
1292 break;
1293 case 5: priv->rf = &grf5101_rf_ops;
1294 break;
1295 case 9: priv->rf = rtl8180_detect_rf(dev);
1296 break;
1297 case 10:
1298 rf_name = "RTL8255";
1299 break;
1300 default:
1301 printk(KERN_ERR "%s (rtl8180): Unknown RF! (0x%x)\n",
Andrea Merello7d4b8292014-03-15 18:29:38 +01001302 pci_name(pdev), priv->rf_type);
Michael Wuf6532112007-10-14 14:43:16 -04001303 goto err_iounmap;
1304 }
1305
1306 if (!priv->rf) {
1307 printk(KERN_ERR "%s (rtl8180): %s RF frontend not supported!\n",
1308 pci_name(pdev), rf_name);
1309 goto err_iounmap;
1310 }
1311
Andrea Merello7d4b8292014-03-15 18:29:38 +01001312 if (!is_valid_ether_addr(priv->mac_addr)) {
Michael Wuf6532112007-10-14 14:43:16 -04001313 printk(KERN_WARNING "%s (rtl8180): Invalid hwaddr! Using"
1314 " randomly generated MAC addr\n", pci_name(pdev));
Andrea Merello7d4b8292014-03-15 18:29:38 +01001315 eth_random_addr(priv->mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001316 }
Andrea Merello7d4b8292014-03-15 18:29:38 +01001317 SET_IEEE80211_PERM_ADDR(dev, priv->mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001318
1319 spin_lock_init(&priv->lock);
1320
1321 err = ieee80211_register_hw(dev);
1322 if (err) {
1323 printk(KERN_ERR "%s (rtl8180): Cannot register device\n",
1324 pci_name(pdev));
1325 goto err_iounmap;
1326 }
1327
Joe Perchesc96c31e2010-07-26 14:39:58 -07001328 wiphy_info(dev->wiphy, "hwaddr %pm, %s + %s\n",
Andrea Merello7d4b8292014-03-15 18:29:38 +01001329 priv->mac_addr, chip_name, priv->rf->name);
Michael Wuf6532112007-10-14 14:43:16 -04001330
1331 return 0;
1332
1333 err_iounmap:
andrea merello0269da22014-02-18 02:10:41 +01001334 pci_iounmap(pdev, priv->map);
Michael Wuf6532112007-10-14 14:43:16 -04001335
1336 err_free_dev:
Michael Wuf6532112007-10-14 14:43:16 -04001337 ieee80211_free_hw(dev);
1338
1339 err_free_reg:
1340 pci_release_regions(pdev);
1341 pci_disable_device(pdev);
1342 return err;
1343}
1344
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001345static void rtl8180_remove(struct pci_dev *pdev)
Michael Wuf6532112007-10-14 14:43:16 -04001346{
1347 struct ieee80211_hw *dev = pci_get_drvdata(pdev);
1348 struct rtl8180_priv *priv;
1349
1350 if (!dev)
1351 return;
1352
1353 ieee80211_unregister_hw(dev);
1354
1355 priv = dev->priv;
1356
1357 pci_iounmap(pdev, priv->map);
1358 pci_release_regions(pdev);
1359 pci_disable_device(pdev);
1360 ieee80211_free_hw(dev);
1361}
1362
1363#ifdef CONFIG_PM
1364static int rtl8180_suspend(struct pci_dev *pdev, pm_message_t state)
1365{
1366 pci_save_state(pdev);
1367 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1368 return 0;
1369}
1370
1371static int rtl8180_resume(struct pci_dev *pdev)
1372{
1373 pci_set_power_state(pdev, PCI_D0);
1374 pci_restore_state(pdev);
1375 return 0;
1376}
1377
1378#endif /* CONFIG_PM */
1379
1380static struct pci_driver rtl8180_driver = {
1381 .name = KBUILD_MODNAME,
1382 .id_table = rtl8180_table,
1383 .probe = rtl8180_probe,
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001384 .remove = rtl8180_remove,
Michael Wuf6532112007-10-14 14:43:16 -04001385#ifdef CONFIG_PM
1386 .suspend = rtl8180_suspend,
1387 .resume = rtl8180_resume,
1388#endif /* CONFIG_PM */
1389};
1390
Axel Lin5b0a3b72012-04-14 10:38:36 +08001391module_pci_driver(rtl8180_driver);