blob: 867ec07da41457605b8a44b2648144c77dd18648 [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;
Andrea Merello21025922014-03-26 20:59:52 +0100132 struct rtl818x_rx_cmd_desc *cmd_desc;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400133 unsigned int count = 32;
John W. Linville8b73fb82010-07-21 16:26:40 -0400134 u8 signal, agc, sq;
andrea.merello2b4db052014-02-05 22:38:05 +0100135 dma_addr_t mapping;
Michael Wuf6532112007-10-14 14:43:16 -0400136
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400137 while (count--) {
Andrea Merello21025922014-03-26 20:59:52 +0100138 void *entry = priv->rx_ring + priv->rx_idx * priv->rx_ring_sz;
Michael Wuf6532112007-10-14 14:43:16 -0400139 struct sk_buff *skb = priv->rx_buf[priv->rx_idx];
Andrea Merello21025922014-03-26 20:59:52 +0100140 u32 flags, flags2;
141 u64 tsft;
142
143 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
144 struct rtl8187se_rx_desc *desc = entry;
145
146 flags = le32_to_cpu(desc->flags);
147 flags2 = le32_to_cpu(desc->flags2);
148 tsft = le64_to_cpu(desc->tsft);
149 } else {
150 struct rtl8180_rx_desc *desc = entry;
151
152 flags = le32_to_cpu(desc->flags);
153 flags2 = le32_to_cpu(desc->flags2);
154 tsft = le64_to_cpu(desc->tsft);
155 }
Michael Wuf6532112007-10-14 14:43:16 -0400156
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300157 if (flags & RTL818X_RX_DESC_FLAG_OWN)
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400158 return;
Michael Wuf6532112007-10-14 14:43:16 -0400159
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300160 if (unlikely(flags & (RTL818X_RX_DESC_FLAG_DMA_FAIL |
161 RTL818X_RX_DESC_FLAG_FOF |
162 RTL818X_RX_DESC_FLAG_RX_ERR)))
Michael Wuf6532112007-10-14 14:43:16 -0400163 goto done;
164 else {
Michael Wuf6532112007-10-14 14:43:16 -0400165 struct ieee80211_rx_status rx_status = {0};
166 struct sk_buff *new_skb = dev_alloc_skb(MAX_RX_SIZE);
167
168 if (unlikely(!new_skb))
169 goto done;
170
andrea.merello2b4db052014-02-05 22:38:05 +0100171 mapping = pci_map_single(priv->pdev,
172 skb_tail_pointer(new_skb),
173 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
174
175 if (pci_dma_mapping_error(priv->pdev, mapping)) {
176 kfree_skb(new_skb);
177 dev_err(&priv->pdev->dev, "RX DMA map error\n");
178
179 goto done;
180 }
181
Michael Wuf6532112007-10-14 14:43:16 -0400182 pci_unmap_single(priv->pdev,
183 *((dma_addr_t *)skb->cb),
184 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
185 skb_put(skb, flags & 0xFFF);
186
187 rx_status.antenna = (flags2 >> 15) & 1;
Johannes Berg8318d782008-01-24 19:38:38 +0100188 rx_status.rate_idx = (flags >> 20) & 0xF;
John W. Linville8b73fb82010-07-21 16:26:40 -0400189 agc = (flags2 >> 17) & 0x7F;
Andrea Merello6caefd12014-03-08 18:36:37 +0100190
191 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
John W. Linville8b73fb82010-07-21 16:26:40 -0400192 if (rx_status.rate_idx > 3)
193 signal = 90 - clamp_t(u8, agc, 25, 90);
194 else
195 signal = 95 - clamp_t(u8, agc, 30, 95);
Andrea Merello21025922014-03-26 20:59:52 +0100196 } else if (priv->chip_family ==
197 RTL818X_CHIP_FAMILY_RTL8180) {
John W. Linville8b73fb82010-07-21 16:26:40 -0400198 sq = flags2 & 0xff;
199 signal = priv->rf->calc_rssi(agc, sq);
Andrea Merello21025922014-03-26 20:59:52 +0100200 } else {
201 /* TODO: rtl8187se rssi */
202 signal = 10;
John W. Linville8b73fb82010-07-21 16:26:40 -0400203 }
John W. Linville8b749642010-07-19 16:35:20 -0400204 rx_status.signal = signal;
Karl Beldan675a0b02013-03-25 16:26:57 +0100205 rx_status.freq = dev->conf.chandef.chan->center_freq;
206 rx_status.band = dev->conf.chandef.chan->band;
Andrea Merello21025922014-03-26 20:59:52 +0100207 rx_status.mactime = tsft;
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800208 rx_status.flag |= RX_FLAG_MACTIME_START;
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300209 if (flags & RTL818X_RX_DESC_FLAG_CRC32_ERR)
Michael Wuf6532112007-10-14 14:43:16 -0400210 rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
211
Johannes Bergf1d58c22009-06-17 13:13:00 +0200212 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400213 ieee80211_rx_irqsafe(dev, skb);
Michael Wuf6532112007-10-14 14:43:16 -0400214
215 skb = new_skb;
216 priv->rx_buf[priv->rx_idx] = skb;
andrea.merello2b4db052014-02-05 22:38:05 +0100217 *((dma_addr_t *) skb->cb) = mapping;
Michael Wuf6532112007-10-14 14:43:16 -0400218 }
219
220 done:
Andrea Merello21025922014-03-26 20:59:52 +0100221 cmd_desc = entry;
222 cmd_desc->rx_buf = cpu_to_le32(*((dma_addr_t *)skb->cb));
223 cmd_desc->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
Michael Wuf6532112007-10-14 14:43:16 -0400224 MAX_RX_SIZE);
225 if (priv->rx_idx == 31)
Andrea Merello21025922014-03-26 20:59:52 +0100226 cmd_desc->flags |=
227 cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
Michael Wuf6532112007-10-14 14:43:16 -0400228 priv->rx_idx = (priv->rx_idx + 1) % 32;
229 }
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400230}
Michael Wuf6532112007-10-14 14:43:16 -0400231
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400232static void rtl8180_handle_tx(struct ieee80211_hw *dev, unsigned int prio)
233{
234 struct rtl8180_priv *priv = dev->priv;
235 struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
Michael Wuf6532112007-10-14 14:43:16 -0400236
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400237 while (skb_queue_len(&ring->queue)) {
238 struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
239 struct sk_buff *skb;
240 struct ieee80211_tx_info *info;
241 u32 flags = le32_to_cpu(entry->flags);
242
243 if (flags & RTL818X_TX_DESC_FLAG_OWN)
244 return;
245
246 ring->idx = (ring->idx + 1) % ring->entries;
247 skb = __skb_dequeue(&ring->queue);
248 pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
249 skb->len, PCI_DMA_TODEVICE);
250
251 info = IEEE80211_SKB_CB(skb);
252 ieee80211_tx_info_clear_status(info);
253
254 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
255 (flags & RTL818X_TX_DESC_FLAG_TX_OK))
256 info->flags |= IEEE80211_TX_STAT_ACK;
257
258 info->status.rates[0].count = (flags & 0xFF) + 1;
259 info->status.rates[1].idx = -1;
260
261 ieee80211_tx_status_irqsafe(dev, skb);
262 if (ring->entries - skb_queue_len(&ring->queue) == 2)
263 ieee80211_wake_queue(dev, prio);
Michael Wuf6532112007-10-14 14:43:16 -0400264 }
265}
266
267static irqreturn_t rtl8180_interrupt(int irq, void *dev_id)
268{
269 struct ieee80211_hw *dev = dev_id;
270 struct rtl8180_priv *priv = dev->priv;
271 u16 reg;
272
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400273 spin_lock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400274 reg = rtl818x_ioread16(priv, &priv->map->INT_STATUS);
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400275 if (unlikely(reg == 0xFFFF)) {
276 spin_unlock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400277 return IRQ_HANDLED;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400278 }
Michael Wuf6532112007-10-14 14:43:16 -0400279
280 rtl818x_iowrite16(priv, &priv->map->INT_STATUS, reg);
281
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400282 if (reg & (RTL818X_INT_TXB_OK | RTL818X_INT_TXB_ERR))
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400283 rtl8180_handle_tx(dev, 1);
284
285 if (reg & (RTL818X_INT_TXL_OK | RTL818X_INT_TXL_ERR))
286 rtl8180_handle_tx(dev, 0);
287
288 if (reg & (RTL818X_INT_RX_OK | RTL818X_INT_RX_ERR))
289 rtl8180_handle_rx(dev);
290
291 spin_unlock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400292
293 return IRQ_HANDLED;
294}
295
Thomas Huehn36323f82012-07-23 21:33:42 +0200296static void rtl8180_tx(struct ieee80211_hw *dev,
297 struct ieee80211_tx_control *control,
298 struct sk_buff *skb)
Michael Wuf6532112007-10-14 14:43:16 -0400299{
Johannes Berge039fa42008-05-15 12:55:29 +0200300 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
John W. Linville51e080d2010-05-06 16:26:23 -0400301 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Michael Wuf6532112007-10-14 14:43:16 -0400302 struct rtl8180_priv *priv = dev->priv;
303 struct rtl8180_tx_ring *ring;
304 struct rtl8180_tx_desc *entry;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400305 unsigned long flags;
Andrea Merellofd6564f2014-03-22 18:51:20 +0100306 unsigned int idx, prio, hw_prio;
Michael Wuf6532112007-10-14 14:43:16 -0400307 dma_addr_t mapping;
308 u32 tx_flags;
Johannes Berge6a98542008-10-21 12:40:02 +0200309 u8 rc_flags;
Michael Wuf6532112007-10-14 14:43:16 -0400310 u16 plcp_len = 0;
311 __le16 rts_duration = 0;
312
Johannes Berge2530082008-05-17 00:57:14 +0200313 prio = skb_get_queue_mapping(skb);
Michael Wuf6532112007-10-14 14:43:16 -0400314 ring = &priv->tx_ring[prio];
315
316 mapping = pci_map_single(priv->pdev, skb->data,
317 skb->len, PCI_DMA_TODEVICE);
318
andrea.merello348f7d42014-02-05 22:38:06 +0100319 if (pci_dma_mapping_error(priv->pdev, mapping)) {
320 kfree_skb(skb);
321 dev_err(&priv->pdev->dev, "TX DMA mapping error\n");
322 return;
323
324 }
325
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300326 tx_flags = RTL818X_TX_DESC_FLAG_OWN | RTL818X_TX_DESC_FLAG_FS |
327 RTL818X_TX_DESC_FLAG_LS |
Johannes Berge039fa42008-05-15 12:55:29 +0200328 (ieee80211_get_tx_rate(dev, info)->hw_value << 24) |
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200329 skb->len;
Michael Wuf6532112007-10-14 14:43:16 -0400330
Andrea Merello6caefd12014-03-08 18:36:37 +0100331 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300332 tx_flags |= RTL818X_TX_DESC_FLAG_DMA |
333 RTL818X_TX_DESC_FLAG_NO_ENC;
Michael Wuf6532112007-10-14 14:43:16 -0400334
Johannes Berge6a98542008-10-21 12:40:02 +0200335 rc_flags = info->control.rates[0].flags;
336 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300337 tx_flags |= RTL818X_TX_DESC_FLAG_RTS;
Johannes Berge039fa42008-05-15 12:55:29 +0200338 tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
Johannes Berge6a98542008-10-21 12:40:02 +0200339 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300340 tx_flags |= RTL818X_TX_DESC_FLAG_CTS;
Johannes Berge039fa42008-05-15 12:55:29 +0200341 tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
Johannes Bergaa68cbf2008-02-18 14:20:30 +0100342 }
Michael Wuf6532112007-10-14 14:43:16 -0400343
Johannes Berge6a98542008-10-21 12:40:02 +0200344 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS)
Johannes Berg32bfd352007-12-19 01:31:26 +0100345 rts_duration = ieee80211_rts_duration(dev, priv->vif, skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +0200346 info);
Michael Wuf6532112007-10-14 14:43:16 -0400347
Andrea Merello6caefd12014-03-08 18:36:37 +0100348 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400349 unsigned int remainder;
350
351 plcp_len = DIV_ROUND_UP(16 * (skb->len + 4),
Johannes Berge039fa42008-05-15 12:55:29 +0200352 (ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
Michael Wuf6532112007-10-14 14:43:16 -0400353 remainder = (16 * (skb->len + 4)) %
Johannes Berge039fa42008-05-15 12:55:29 +0200354 ((ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
Roel Kluin35a0ace2009-06-22 17:42:21 +0200355 if (remainder <= 6)
Michael Wuf6532112007-10-14 14:43:16 -0400356 plcp_len |= 1 << 15;
357 }
358
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400359 spin_lock_irqsave(&priv->lock, flags);
John W. Linville51e080d2010-05-06 16:26:23 -0400360
361 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
362 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
363 priv->seqno += 0x10;
364 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
365 hdr->seq_ctrl |= cpu_to_le16(priv->seqno);
366 }
367
Michael Wuf6532112007-10-14 14:43:16 -0400368 idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
369 entry = &ring->desc[idx];
370
371 entry->rts_duration = rts_duration;
372 entry->plcp_len = cpu_to_le16(plcp_len);
373 entry->tx_buf = cpu_to_le32(mapping);
374 entry->frame_len = cpu_to_le32(skb->len);
Johannes Berge6a98542008-10-21 12:40:02 +0200375 entry->flags2 = info->control.rates[1].idx >= 0 ?
Felix Fietkau870abdf2008-10-05 18:04:24 +0200376 ieee80211_get_alt_retry_rate(dev, info, 0)->bitrate << 4 : 0;
Johannes Berge6a98542008-10-21 12:40:02 +0200377 entry->retry_limit = info->control.rates[0].count;
andrea merello4c552a52014-02-18 02:10:45 +0100378
379 /* We must be sure that tx_flags is written last because the HW
380 * looks at it to check if the rest of data is valid or not
381 */
382 wmb();
Michael Wuf6532112007-10-14 14:43:16 -0400383 entry->flags = cpu_to_le32(tx_flags);
andrea merelloc24782e2014-02-18 02:10:46 +0100384 /* We must be sure this has been written before followings HW
385 * register write, because this write will made the HW attempts
386 * to DMA the just-written data
387 */
388 wmb();
389
Michael Wuf6532112007-10-14 14:43:16 -0400390 __skb_queue_tail(&ring->queue, skb);
391 if (ring->entries - skb_queue_len(&ring->queue) < 2)
John W. Linvilled10e2e02010-04-27 16:57:38 -0400392 ieee80211_stop_queue(dev, prio);
John W. Linville51e080d2010-05-06 16:26:23 -0400393
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400394 spin_unlock_irqrestore(&priv->lock, flags);
Michael Wuf6532112007-10-14 14:43:16 -0400395
Andrea Merellofd6564f2014-03-22 18:51:20 +0100396 hw_prio = rtl8180_queues_map[prio];
397
398 rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
399 (1 << hw_prio) | /* ring to poll */
400 (1<<1) | (1<<2));/* stopped rings */
Michael Wuf6532112007-10-14 14:43:16 -0400401}
402
403void rtl8180_set_anaparam(struct rtl8180_priv *priv, u32 anaparam)
404{
405 u8 reg;
406
407 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
408 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
409 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
410 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
411 rtl818x_iowrite32(priv, &priv->map->ANAPARAM, anaparam);
412 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
413 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
414 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
415}
416
Andrea Merello516a0932014-03-15 18:29:36 +0100417static void rtl8180_conf_basic_rates(struct ieee80211_hw *dev,
418 u32 rates_mask)
419{
420 struct rtl8180_priv *priv = dev->priv;
421
422 u8 max, min;
423 u16 reg;
424
425 max = fls(rates_mask) - 1;
426 min = ffs(rates_mask) - 1;
427
428 switch (priv->chip_family) {
429
430 case RTL818X_CHIP_FAMILY_RTL8180:
431 /* in 8180 this is NOT a BITMAP */
432 reg = rtl818x_ioread16(priv, &priv->map->BRSR);
433 reg &= ~3;
434 reg |= max;
435 rtl818x_iowrite16(priv, &priv->map->BRSR, reg);
Andrea Merello516a0932014-03-15 18:29:36 +0100436 break;
437
438 case RTL818X_CHIP_FAMILY_RTL8185:
439 /* in 8185 this is a BITMAP */
440 rtl818x_iowrite16(priv, &priv->map->BRSR, rates_mask);
441 rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (max << 4) | min);
442 break;
Andrea Merellod209f3b2014-03-26 20:59:25 +0100443
444 case RTL818X_CHIP_FAMILY_RTL8187SE:
445 /* in 8187se this is a BITMAP */
446 rtl818x_iowrite16(priv, &priv->map->BRSR_8187SE, rates_mask);
447 break;
Andrea Merello516a0932014-03-15 18:29:36 +0100448 }
449}
450
Michael Wuf6532112007-10-14 14:43:16 -0400451static int rtl8180_init_hw(struct ieee80211_hw *dev)
452{
453 struct rtl8180_priv *priv = dev->priv;
454 u16 reg;
455
456 rtl818x_iowrite8(priv, &priv->map->CMD, 0);
457 rtl818x_ioread8(priv, &priv->map->CMD);
458 msleep(10);
459
460 /* reset */
461 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
462 rtl818x_ioread8(priv, &priv->map->CMD);
463
464 reg = rtl818x_ioread8(priv, &priv->map->CMD);
465 reg &= (1 << 1);
466 reg |= RTL818X_CMD_RESET;
467 rtl818x_iowrite8(priv, &priv->map->CMD, RTL818X_CMD_RESET);
468 rtl818x_ioread8(priv, &priv->map->CMD);
469 msleep(200);
470
471 /* check success of reset */
472 if (rtl818x_ioread8(priv, &priv->map->CMD) & RTL818X_CMD_RESET) {
Joe Perchesc96c31e2010-07-26 14:39:58 -0700473 wiphy_err(dev->wiphy, "reset timeout!\n");
Michael Wuf6532112007-10-14 14:43:16 -0400474 return -ETIMEDOUT;
475 }
476
477 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
478 rtl818x_ioread8(priv, &priv->map->CMD);
479 msleep(200);
480
481 if (rtl818x_ioread8(priv, &priv->map->CONFIG3) & (1 << 3)) {
482 /* For cardbus */
483 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
484 reg |= 1 << 1;
485 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
486 reg = rtl818x_ioread16(priv, &priv->map->FEMR);
487 reg |= (1 << 15) | (1 << 14) | (1 << 4);
488 rtl818x_iowrite16(priv, &priv->map->FEMR, reg);
489 }
490
491 rtl818x_iowrite8(priv, &priv->map->MSR, 0);
492
Andrea Merello6caefd12014-03-08 18:36:37 +0100493 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
Michael Wuf6532112007-10-14 14:43:16 -0400494 rtl8180_set_anaparam(priv, priv->anaparam);
495
496 rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
Andrea Merellofd6564f2014-03-22 18:51:20 +0100497 rtl818x_iowrite32(priv, &priv->map->TBDA, priv->tx_ring[1].dma);
Michael Wuf6532112007-10-14 14:43:16 -0400498 rtl818x_iowrite32(priv, &priv->map->TLPDA, priv->tx_ring[0].dma);
499
500 /* TODO: necessary? specs indicate not */
501 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
502 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
503 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg & ~(1 << 3));
Andrea Merello6caefd12014-03-08 18:36:37 +0100504 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
Michael Wuf6532112007-10-14 14:43:16 -0400505 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
506 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg | (1 << 4));
507 }
508 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
509
510 /* TODO: set CONFIG5 for calibrating AGC on rtl8180 + philips radio? */
511
512 /* TODO: turn off hw wep on rtl8180 */
513
514 rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
515
Andrea Merello6caefd12014-03-08 18:36:37 +0100516 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400517 rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
518 rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
Michael Wuf6532112007-10-14 14:43:16 -0400519
520 /* TODO: set ClkRun enable? necessary? */
521 reg = rtl818x_ioread8(priv, &priv->map->GP_ENABLE);
522 rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, reg & ~(1 << 6));
523 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
524 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
525 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | (1 << 2));
526 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
527 } else {
Michael Wuf6532112007-10-14 14:43:16 -0400528 rtl818x_iowrite8(priv, &priv->map->SECURITY, 0);
529
530 rtl818x_iowrite8(priv, &priv->map->PHY_DELAY, 0x6);
531 rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, 0x4C);
532 }
533
534 priv->rf->init(dev);
Andrea Merello516a0932014-03-15 18:29:36 +0100535
536 /* default basic rates are 1,2 Mbps for rtl8180. 1,2,6,9,12,18,24 Mbps
537 * otherwise. bitmask 0x3 and 0x01f3 respectively.
538 * NOTE: currenty rtl8225 RF code changes basic rates, so we need to do
539 * this after rf init.
540 * TODO: try to find out whether RF code really needs to do this..
541 */
542 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
543 rtl8180_conf_basic_rates(dev, 0x3);
544 else
545 rtl8180_conf_basic_rates(dev, 0x1f3);
546
Michael Wuf6532112007-10-14 14:43:16 -0400547 return 0;
548}
549
550static int rtl8180_init_rx_ring(struct ieee80211_hw *dev)
551{
552 struct rtl8180_priv *priv = dev->priv;
Andrea Merello21025922014-03-26 20:59:52 +0100553 struct rtl818x_rx_cmd_desc *entry;
Michael Wuf6532112007-10-14 14:43:16 -0400554 int i;
555
Andrea Merello21025922014-03-26 20:59:52 +0100556 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
557 priv->rx_ring_sz = sizeof(struct rtl8187se_rx_desc);
558 else
559 priv->rx_ring_sz = sizeof(struct rtl8180_rx_desc);
560
Michael Wuf6532112007-10-14 14:43:16 -0400561 priv->rx_ring = pci_alloc_consistent(priv->pdev,
Andrea Merello21025922014-03-26 20:59:52 +0100562 priv->rx_ring_sz * 32,
Michael Wuf6532112007-10-14 14:43:16 -0400563 &priv->rx_ring_dma);
564
565 if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -0700566 wiphy_err(dev->wiphy, "Cannot allocate RX ring\n");
Michael Wuf6532112007-10-14 14:43:16 -0400567 return -ENOMEM;
568 }
569
Andrea Merello21025922014-03-26 20:59:52 +0100570 memset(priv->rx_ring, 0, priv->rx_ring_sz * 32);
Michael Wuf6532112007-10-14 14:43:16 -0400571 priv->rx_idx = 0;
572
573 for (i = 0; i < 32; i++) {
574 struct sk_buff *skb = dev_alloc_skb(MAX_RX_SIZE);
575 dma_addr_t *mapping;
Andrea Merello21025922014-03-26 20:59:52 +0100576 entry = priv->rx_ring + priv->rx_ring_sz*i;
andrea merello4da18bb2014-02-18 02:10:43 +0100577 if (!skb) {
578 wiphy_err(dev->wiphy, "Cannot allocate RX skb\n");
579 return -ENOMEM;
580 }
Michael Wuf6532112007-10-14 14:43:16 -0400581 priv->rx_buf[i] = skb;
582 mapping = (dma_addr_t *)skb->cb;
583 *mapping = pci_map_single(priv->pdev, skb_tail_pointer(skb),
584 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
andrea merelloec1da082014-02-22 17:57:23 +0100585
586 if (pci_dma_mapping_error(priv->pdev, *mapping)) {
587 kfree_skb(skb);
588 wiphy_err(dev->wiphy, "Cannot map DMA for RX skb\n");
589 return -ENOMEM;
590 }
591
Michael Wuf6532112007-10-14 14:43:16 -0400592 entry->rx_buf = cpu_to_le32(*mapping);
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300593 entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
Michael Wuf6532112007-10-14 14:43:16 -0400594 MAX_RX_SIZE);
595 }
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300596 entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
Michael Wuf6532112007-10-14 14:43:16 -0400597 return 0;
598}
599
600static void rtl8180_free_rx_ring(struct ieee80211_hw *dev)
601{
602 struct rtl8180_priv *priv = dev->priv;
603 int i;
604
605 for (i = 0; i < 32; i++) {
606 struct sk_buff *skb = priv->rx_buf[i];
607 if (!skb)
608 continue;
609
610 pci_unmap_single(priv->pdev,
611 *((dma_addr_t *)skb->cb),
612 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
613 kfree_skb(skb);
614 }
615
Andrea Merello21025922014-03-26 20:59:52 +0100616 pci_free_consistent(priv->pdev, priv->rx_ring_sz * 32,
Michael Wuf6532112007-10-14 14:43:16 -0400617 priv->rx_ring, priv->rx_ring_dma);
618 priv->rx_ring = NULL;
619}
620
621static int rtl8180_init_tx_ring(struct ieee80211_hw *dev,
622 unsigned int prio, unsigned int entries)
623{
624 struct rtl8180_priv *priv = dev->priv;
625 struct rtl8180_tx_desc *ring;
626 dma_addr_t dma;
627 int i;
628
629 ring = pci_alloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
630 if (!ring || (unsigned long)ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -0700631 wiphy_err(dev->wiphy, "Cannot allocate TX ring (prio = %d)\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -0700632 prio);
Michael Wuf6532112007-10-14 14:43:16 -0400633 return -ENOMEM;
634 }
635
636 memset(ring, 0, sizeof(*ring)*entries);
637 priv->tx_ring[prio].desc = ring;
638 priv->tx_ring[prio].dma = dma;
639 priv->tx_ring[prio].idx = 0;
640 priv->tx_ring[prio].entries = entries;
641 skb_queue_head_init(&priv->tx_ring[prio].queue);
642
643 for (i = 0; i < entries; i++)
644 ring[i].next_tx_desc =
645 cpu_to_le32((u32)dma + ((i + 1) % entries) * sizeof(*ring));
646
647 return 0;
648}
649
650static void rtl8180_free_tx_ring(struct ieee80211_hw *dev, unsigned int prio)
651{
652 struct rtl8180_priv *priv = dev->priv;
653 struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
654
655 while (skb_queue_len(&ring->queue)) {
656 struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
657 struct sk_buff *skb = __skb_dequeue(&ring->queue);
658
659 pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
660 skb->len, PCI_DMA_TODEVICE);
Michael Wuf6532112007-10-14 14:43:16 -0400661 kfree_skb(skb);
662 ring->idx = (ring->idx + 1) % ring->entries;
663 }
664
665 pci_free_consistent(priv->pdev, sizeof(*ring->desc)*ring->entries,
666 ring->desc, ring->dma);
667 ring->desc = NULL;
668}
669
670static int rtl8180_start(struct ieee80211_hw *dev)
671{
672 struct rtl8180_priv *priv = dev->priv;
673 int ret, i;
674 u32 reg;
675
676 ret = rtl8180_init_rx_ring(dev);
677 if (ret)
678 return ret;
679
Andrea Merellofd6564f2014-03-22 18:51:20 +0100680 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -0400681 if ((ret = rtl8180_init_tx_ring(dev, i, 16)))
682 goto err_free_rings;
683
684 ret = rtl8180_init_hw(dev);
685 if (ret)
686 goto err_free_rings;
687
Julia Lawallea31ba32009-11-18 08:26:02 +0000688 ret = request_irq(priv->pdev->irq, rtl8180_interrupt,
Michael Wuf6532112007-10-14 14:43:16 -0400689 IRQF_SHARED, KBUILD_MODNAME, dev);
690 if (ret) {
Joe Perches5db55842010-08-11 19:11:19 -0700691 wiphy_err(dev->wiphy, "failed to register IRQ handler\n");
Michael Wuf6532112007-10-14 14:43:16 -0400692 goto err_free_rings;
693 }
694
695 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
696
697 rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
698 rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
699
700 reg = RTL818X_RX_CONF_ONLYERLPKT |
701 RTL818X_RX_CONF_RX_AUTORESETPHY |
702 RTL818X_RX_CONF_MGMT |
703 RTL818X_RX_CONF_DATA |
704 (7 << 8 /* MAX RX DMA */) |
705 RTL818X_RX_CONF_BROADCAST |
706 RTL818X_RX_CONF_NICMAC;
707
Andrea Merello6caefd12014-03-08 18:36:37 +0100708 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185)
Michael Wuf6532112007-10-14 14:43:16 -0400709 reg |= RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2;
710 else {
711 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE1)
712 ? RTL818X_RX_CONF_CSDM1 : 0;
713 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE2)
714 ? RTL818X_RX_CONF_CSDM2 : 0;
715 }
716
717 priv->rx_conf = reg;
718 rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
719
Andrea Merello6caefd12014-03-08 18:36:37 +0100720 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400721 reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
andrea merello14c76152014-02-18 02:10:44 +0100722
723 /* CW is not on per-packet basis.
724 * in rtl8185 the CW_VALUE reg is used.
725 */
andrea merello6f7343d2014-01-21 20:16:43 +0100726 reg &= ~RTL818X_CW_CONF_PERPACKET_CW;
andrea merello14c76152014-02-18 02:10:44 +0100727 /* retry limit IS on per-packet basis.
728 * the short and long retry limit in TX_CONF
729 * reg are ignored
730 */
andrea merello6f7343d2014-01-21 20:16:43 +0100731 reg |= RTL818X_CW_CONF_PERPACKET_RETRY;
Michael Wuf6532112007-10-14 14:43:16 -0400732 rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
733
734 reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
andrea merello14c76152014-02-18 02:10:44 +0100735 /* TX antenna and TX gain are not on per-packet basis.
736 * TX Antenna is selected by ANTSEL reg (RX in BB regs).
737 * TX gain is selected with CCK_TX_AGC and OFDM_TX_AGC regs
738 */
andrea merello6f7343d2014-01-21 20:16:43 +0100739 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN;
740 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL;
Michael Wuf6532112007-10-14 14:43:16 -0400741 reg |= RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
742 rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
743
744 /* disable early TX */
745 rtl818x_iowrite8(priv, (u8 __iomem *)priv->map + 0xec, 0x3f);
746 }
747
748 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
749 reg |= (6 << 21 /* MAX TX DMA */) |
750 RTL818X_TX_CONF_NO_ICV;
751
Andrea Merello6caefd12014-03-08 18:36:37 +0100752
753
754 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
Michael Wuf6532112007-10-14 14:43:16 -0400755 reg &= ~RTL818X_TX_CONF_PROBE_DTS;
756 else
757 reg &= ~RTL818X_TX_CONF_HW_SEQNUM;
758
andrea merelloe74075a2014-02-18 02:10:40 +0100759 reg &= ~RTL818X_TX_CONF_DISCW;
760
Michael Wuf6532112007-10-14 14:43:16 -0400761 /* different meaning, same value on both rtl8185 and rtl8180 */
762 reg &= ~RTL818X_TX_CONF_SAT_HWPLCP;
763
764 rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
765
766 reg = rtl818x_ioread8(priv, &priv->map->CMD);
767 reg |= RTL818X_CMD_RX_ENABLE;
768 reg |= RTL818X_CMD_TX_ENABLE;
769 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
770
Michael Wuf6532112007-10-14 14:43:16 -0400771 return 0;
772
773 err_free_rings:
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 if (priv->tx_ring[i].desc)
777 rtl8180_free_tx_ring(dev, i);
778
779 return ret;
780}
781
782static void rtl8180_stop(struct ieee80211_hw *dev)
783{
784 struct rtl8180_priv *priv = dev->priv;
785 u8 reg;
786 int i;
787
Michael Wuf6532112007-10-14 14:43:16 -0400788 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
789
790 reg = rtl818x_ioread8(priv, &priv->map->CMD);
791 reg &= ~RTL818X_CMD_TX_ENABLE;
792 reg &= ~RTL818X_CMD_RX_ENABLE;
793 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
794
795 priv->rf->stop(dev);
796
797 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
798 reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
799 rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
800 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
801
802 free_irq(priv->pdev->irq, dev);
803
804 rtl8180_free_rx_ring(dev);
Andrea Merellofd6564f2014-03-22 18:51:20 +0100805 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -0400806 rtl8180_free_tx_ring(dev, i);
807}
808
Eliad Peller37a41b42011-09-21 14:06:11 +0300809static u64 rtl8180_get_tsf(struct ieee80211_hw *dev,
810 struct ieee80211_vif *vif)
John W. Linvillec809e862010-05-06 16:49:40 -0400811{
812 struct rtl8180_priv *priv = dev->priv;
813
814 return rtl818x_ioread32(priv, &priv->map->TSFT[0]) |
815 (u64)(rtl818x_ioread32(priv, &priv->map->TSFT[1])) << 32;
816}
817
John W. Linvillea3275e22010-06-24 11:08:37 -0400818static void rtl8180_beacon_work(struct work_struct *work)
John W. Linvillec809e862010-05-06 16:49:40 -0400819{
820 struct rtl8180_vif *vif_priv =
821 container_of(work, struct rtl8180_vif, beacon_work.work);
822 struct ieee80211_vif *vif =
823 container_of((void *)vif_priv, struct ieee80211_vif, drv_priv);
824 struct ieee80211_hw *dev = vif_priv->dev;
825 struct ieee80211_mgmt *mgmt;
826 struct sk_buff *skb;
John W. Linvillec809e862010-05-06 16:49:40 -0400827
828 /* don't overflow the tx ring */
829 if (ieee80211_queue_stopped(dev, 0))
830 goto resched;
831
832 /* grab a fresh beacon */
833 skb = ieee80211_beacon_get(dev, vif);
John W. Linville8f1d2d22010-08-05 13:46:27 -0400834 if (!skb)
835 goto resched;
John W. Linvillec809e862010-05-06 16:49:40 -0400836
837 /*
838 * update beacon timestamp w/ TSF value
839 * TODO: make hardware update beacon timestamp
840 */
841 mgmt = (struct ieee80211_mgmt *)skb->data;
Eliad Peller37a41b42011-09-21 14:06:11 +0300842 mgmt->u.beacon.timestamp = cpu_to_le64(rtl8180_get_tsf(dev, vif));
John W. Linvillec809e862010-05-06 16:49:40 -0400843
844 /* TODO: use actual beacon queue */
845 skb_set_queue_mapping(skb, 0);
846
Thomas Huehn36323f82012-07-23 21:33:42 +0200847 rtl8180_tx(dev, NULL, skb);
John W. Linvillec809e862010-05-06 16:49:40 -0400848
849resched:
850 /*
851 * schedule next beacon
852 * TODO: use hardware support for beacon timing
853 */
854 schedule_delayed_work(&vif_priv->beacon_work,
855 usecs_to_jiffies(1024 * vif->bss_conf.beacon_int));
856}
857
Michael Wuf6532112007-10-14 14:43:16 -0400858static int rtl8180_add_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100859 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -0400860{
861 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -0400862 struct rtl8180_vif *vif_priv;
Michael Wuf6532112007-10-14 14:43:16 -0400863
John W. Linville643aab62009-12-22 18:13:04 -0500864 /*
865 * We only support one active interface at a time.
866 */
867 if (priv->vif)
868 return -EBUSY;
Michael Wuf6532112007-10-14 14:43:16 -0400869
Johannes Berg1ed32e42009-12-23 13:15:45 +0100870 switch (vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200871 case NL80211_IFTYPE_STATION:
John W. Linvillec809e862010-05-06 16:49:40 -0400872 case NL80211_IFTYPE_ADHOC:
Michael Wuf6532112007-10-14 14:43:16 -0400873 break;
874 default:
875 return -EOPNOTSUPP;
876 }
877
Johannes Berg1ed32e42009-12-23 13:15:45 +0100878 priv->vif = vif;
Johannes Berg32bfd352007-12-19 01:31:26 +0100879
John W. Linvillec809e862010-05-06 16:49:40 -0400880 /* Initialize driver private area */
881 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
882 vif_priv->dev = dev;
883 INIT_DELAYED_WORK(&vif_priv->beacon_work, rtl8180_beacon_work);
884 vif_priv->enable_beacon = false;
885
Michael Wuf6532112007-10-14 14:43:16 -0400886 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
887 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->MAC[0],
Johannes Berg1ed32e42009-12-23 13:15:45 +0100888 le32_to_cpu(*(__le32 *)vif->addr));
Michael Wuf6532112007-10-14 14:43:16 -0400889 rtl818x_iowrite16(priv, (__le16 __iomem *)&priv->map->MAC[4],
Johannes Berg1ed32e42009-12-23 13:15:45 +0100890 le16_to_cpu(*(__le16 *)(vif->addr + 4)));
Michael Wuf6532112007-10-14 14:43:16 -0400891 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
892
893 return 0;
894}
895
896static void rtl8180_remove_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100897 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -0400898{
899 struct rtl8180_priv *priv = dev->priv;
Johannes Berg32bfd352007-12-19 01:31:26 +0100900 priv->vif = NULL;
Michael Wuf6532112007-10-14 14:43:16 -0400901}
902
Johannes Berge8975582008-10-09 12:18:51 +0200903static int rtl8180_config(struct ieee80211_hw *dev, u32 changed)
Michael Wuf6532112007-10-14 14:43:16 -0400904{
905 struct rtl8180_priv *priv = dev->priv;
Johannes Berge8975582008-10-09 12:18:51 +0200906 struct ieee80211_conf *conf = &dev->conf;
Michael Wuf6532112007-10-14 14:43:16 -0400907
908 priv->rf->set_chan(dev, conf);
909
910 return 0;
911}
912
Andrea Merello9069af72014-03-15 18:29:37 +0100913static int rtl8180_conf_tx(struct ieee80211_hw *dev,
914 struct ieee80211_vif *vif, u16 queue,
915 const struct ieee80211_tx_queue_params *params)
916{
917 struct rtl8180_priv *priv = dev->priv;
918 u8 cw_min, cw_max;
919
920 /* nothing to do ? */
921 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
922 return 0;
923
924 cw_min = fls(params->cw_min);
925 cw_max = fls(params->cw_max);
926
927 rtl818x_iowrite8(priv, &priv->map->CW_VAL, (cw_max << 4) | cw_min);
928
929 return 0;
930}
931
932static void rtl8180_conf_erp(struct ieee80211_hw *dev,
933 struct ieee80211_bss_conf *info)
934{
935 struct rtl8180_priv *priv = dev->priv;
936 u8 sifs, difs;
937 int eifs;
938 u8 hw_eifs;
939
940 /* TODO: should we do something ? */
941 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
942 return;
943
944 /* I _hope_ this means 10uS for the HW.
945 * In reference code it is 0x22 for
946 * both rtl8187L and rtl8187SE
947 */
948 sifs = 0x22;
949
950 if (info->use_short_slot)
951 priv->slot_time = 9;
952 else
953 priv->slot_time = 20;
954
955 /* 10 is SIFS time in uS */
956 difs = 10 + 2 * priv->slot_time;
957 eifs = 10 + difs + priv->ack_time;
958
959 /* HW should use 4uS units for EIFS (I'm sure for rtl8185)*/
960 hw_eifs = DIV_ROUND_UP(eifs, 4);
961
962
963 rtl818x_iowrite8(priv, &priv->map->SLOT, priv->slot_time);
964 rtl818x_iowrite8(priv, &priv->map->SIFS, sifs);
965 rtl818x_iowrite8(priv, &priv->map->DIFS, difs);
966
967 /* from reference code. set ack timeout reg = eifs reg */
968 rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, hw_eifs);
969
970 /* rtl8187/rtl8185 HW bug. After EIFS is elapsed,
971 * the HW still wait for DIFS.
972 * HW uses 4uS units for EIFS.
973 */
974 hw_eifs = DIV_ROUND_UP(eifs - difs, 4);
975
976 rtl818x_iowrite8(priv, &priv->map->EIFS, hw_eifs);
977}
978
John W. Linvilleda81ded2008-11-12 14:37:11 -0500979static void rtl8180_bss_info_changed(struct ieee80211_hw *dev,
980 struct ieee80211_vif *vif,
981 struct ieee80211_bss_conf *info,
982 u32 changed)
983{
984 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -0400985 struct rtl8180_vif *vif_priv;
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200986 int i;
John W. Linville0f956e72010-07-29 21:50:29 -0400987 u8 reg;
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200988
John W. Linvillec809e862010-05-06 16:49:40 -0400989 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
990
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200991 if (changed & BSS_CHANGED_BSSID) {
992 for (i = 0; i < ETH_ALEN; i++)
993 rtl818x_iowrite8(priv, &priv->map->BSSID[i],
994 info->bssid[i]);
995
John W. Linville0f956e72010-07-29 21:50:29 -0400996 if (is_valid_ether_addr(info->bssid)) {
997 if (vif->type == NL80211_IFTYPE_ADHOC)
998 reg = RTL818X_MSR_ADHOC;
999 else
1000 reg = RTL818X_MSR_INFRA;
1001 } else
1002 reg = RTL818X_MSR_NO_LINK;
1003 rtl818x_iowrite8(priv, &priv->map->MSR, reg);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001004 }
John W. Linvilleda81ded2008-11-12 14:37:11 -05001005
Andrea Merello516a0932014-03-15 18:29:36 +01001006 if (changed & BSS_CHANGED_BASIC_RATES)
1007 rtl8180_conf_basic_rates(dev, info->basic_rates);
1008
Andrea Merello9069af72014-03-15 18:29:37 +01001009 if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_ERP_PREAMBLE)) {
1010
1011 /* when preamble changes, acktime duration changes, and erp must
1012 * be recalculated. ACK time is calculated at lowest rate.
1013 * Since mac80211 include SIFS time we remove it (-10)
1014 */
1015 priv->ack_time =
1016 le16_to_cpu(ieee80211_generic_frame_duration(dev,
1017 priv->vif,
1018 IEEE80211_BAND_2GHZ, 10,
1019 &priv->rates[0])) - 10;
1020
1021 rtl8180_conf_erp(dev, info);
1022 }
John W. Linvillec809e862010-05-06 16:49:40 -04001023
1024 if (changed & BSS_CHANGED_BEACON_ENABLED)
1025 vif_priv->enable_beacon = info->enable_beacon;
1026
1027 if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON)) {
1028 cancel_delayed_work_sync(&vif_priv->beacon_work);
1029 if (vif_priv->enable_beacon)
1030 schedule_work(&vif_priv->beacon_work.work);
1031 }
John W. Linvilleda81ded2008-11-12 14:37:11 -05001032}
1033
Jiri Pirko22bedad32010-04-01 21:22:57 +00001034static u64 rtl8180_prepare_multicast(struct ieee80211_hw *dev,
1035 struct netdev_hw_addr_list *mc_list)
Johannes Berg3ac64be2009-08-17 16:16:53 +02001036{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001037 return netdev_hw_addr_list_count(mc_list);
Johannes Berg3ac64be2009-08-17 16:16:53 +02001038}
1039
Michael Wuf6532112007-10-14 14:43:16 -04001040static void rtl8180_configure_filter(struct ieee80211_hw *dev,
1041 unsigned int changed_flags,
1042 unsigned int *total_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001043 u64 multicast)
Michael Wuf6532112007-10-14 14:43:16 -04001044{
1045 struct rtl8180_priv *priv = dev->priv;
1046
1047 if (changed_flags & FIF_FCSFAIL)
1048 priv->rx_conf ^= RTL818X_RX_CONF_FCS;
1049 if (changed_flags & FIF_CONTROL)
1050 priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
1051 if (changed_flags & FIF_OTHER_BSS)
1052 priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
Johannes Berg3ac64be2009-08-17 16:16:53 +02001053 if (*total_flags & FIF_ALLMULTI || multicast > 0)
Michael Wuf6532112007-10-14 14:43:16 -04001054 priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
1055 else
1056 priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
1057
1058 *total_flags = 0;
1059
1060 if (priv->rx_conf & RTL818X_RX_CONF_FCS)
1061 *total_flags |= FIF_FCSFAIL;
1062 if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
1063 *total_flags |= FIF_CONTROL;
1064 if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
1065 *total_flags |= FIF_OTHER_BSS;
1066 if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
1067 *total_flags |= FIF_ALLMULTI;
1068
1069 rtl818x_iowrite32(priv, &priv->map->RX_CONF, priv->rx_conf);
1070}
1071
1072static const struct ieee80211_ops rtl8180_ops = {
1073 .tx = rtl8180_tx,
1074 .start = rtl8180_start,
1075 .stop = rtl8180_stop,
1076 .add_interface = rtl8180_add_interface,
1077 .remove_interface = rtl8180_remove_interface,
1078 .config = rtl8180_config,
John W. Linvilleda81ded2008-11-12 14:37:11 -05001079 .bss_info_changed = rtl8180_bss_info_changed,
Andrea Merello9069af72014-03-15 18:29:37 +01001080 .conf_tx = rtl8180_conf_tx,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001081 .prepare_multicast = rtl8180_prepare_multicast,
Michael Wuf6532112007-10-14 14:43:16 -04001082 .configure_filter = rtl8180_configure_filter,
John W. Linvilled2bb8e02010-01-26 16:22:20 -05001083 .get_tsf = rtl8180_get_tsf,
Michael Wuf6532112007-10-14 14:43:16 -04001084};
1085
1086static void rtl8180_eeprom_register_read(struct eeprom_93cx6 *eeprom)
1087{
Andrea Merello7d4b8292014-03-15 18:29:38 +01001088 struct rtl8180_priv *priv = eeprom->data;
Michael Wuf6532112007-10-14 14:43:16 -04001089 u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1090
1091 eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
1092 eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
1093 eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
1094 eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
1095}
1096
1097static void rtl8180_eeprom_register_write(struct eeprom_93cx6 *eeprom)
1098{
Andrea Merello7d4b8292014-03-15 18:29:38 +01001099 struct rtl8180_priv *priv = eeprom->data;
Michael Wuf6532112007-10-14 14:43:16 -04001100 u8 reg = 2 << 6;
1101
1102 if (eeprom->reg_data_in)
1103 reg |= RTL818X_EEPROM_CMD_WRITE;
1104 if (eeprom->reg_data_out)
1105 reg |= RTL818X_EEPROM_CMD_READ;
1106 if (eeprom->reg_data_clock)
1107 reg |= RTL818X_EEPROM_CMD_CK;
1108 if (eeprom->reg_chip_select)
1109 reg |= RTL818X_EEPROM_CMD_CS;
1110
1111 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
1112 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1113 udelay(10);
1114}
1115
Andrea Merello7d4b8292014-03-15 18:29:38 +01001116static void rtl8180_eeprom_read(struct rtl8180_priv *priv)
1117{
1118 struct eeprom_93cx6 eeprom;
1119 int eeprom_cck_table_adr;
1120 u16 eeprom_val;
1121 int i;
1122
1123 eeprom.data = priv;
1124 eeprom.register_read = rtl8180_eeprom_register_read;
1125 eeprom.register_write = rtl8180_eeprom_register_write;
1126 if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
1127 eeprom.width = PCI_EEPROM_WIDTH_93C66;
1128 else
1129 eeprom.width = PCI_EEPROM_WIDTH_93C46;
1130
1131 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
1132 RTL818X_EEPROM_CMD_PROGRAM);
1133 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1134 udelay(10);
1135
1136 eeprom_93cx6_read(&eeprom, 0x06, &eeprom_val);
1137 eeprom_val &= 0xFF;
1138 priv->rf_type = eeprom_val;
1139
1140 eeprom_93cx6_read(&eeprom, 0x17, &eeprom_val);
1141 priv->csthreshold = eeprom_val >> 8;
1142
1143 eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)priv->mac_addr, 3);
1144
1145 eeprom_cck_table_adr = 0x10;
1146
1147 /* CCK TX power */
1148 for (i = 0; i < 14; i += 2) {
1149 u16 txpwr;
1150 eeprom_93cx6_read(&eeprom, eeprom_cck_table_adr + (i >> 1),
1151 &txpwr);
1152 priv->channels[i].hw_value = txpwr & 0xFF;
1153 priv->channels[i + 1].hw_value = txpwr >> 8;
1154 }
1155
1156 /* OFDM TX power */
1157 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
1158 for (i = 0; i < 14; i += 2) {
1159 u16 txpwr;
1160 eeprom_93cx6_read(&eeprom, 0x20 + (i >> 1), &txpwr);
1161 priv->channels[i].hw_value |= (txpwr & 0xFF) << 8;
1162 priv->channels[i + 1].hw_value |= txpwr & 0xFF00;
1163 }
1164 }
1165
1166 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
1167 __le32 anaparam;
1168 eeprom_93cx6_multiread(&eeprom, 0xD, (__le16 *)&anaparam, 2);
1169 priv->anaparam = le32_to_cpu(anaparam);
1170 eeprom_93cx6_read(&eeprom, 0x19, &priv->rfparam);
1171 }
1172
1173 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
1174 RTL818X_EEPROM_CMD_NORMAL);
1175}
1176
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001177static int rtl8180_probe(struct pci_dev *pdev,
Michael Wuf6532112007-10-14 14:43:16 -04001178 const struct pci_device_id *id)
1179{
1180 struct ieee80211_hw *dev;
1181 struct rtl8180_priv *priv;
1182 unsigned long mem_addr, mem_len;
1183 unsigned int io_addr, io_len;
Andrea Merello7d4b8292014-03-15 18:29:38 +01001184 int err;
Michael Wuf6532112007-10-14 14:43:16 -04001185 const char *chip_name, *rf_name = NULL;
1186 u32 reg;
Michael Wuf6532112007-10-14 14:43:16 -04001187
1188 err = pci_enable_device(pdev);
1189 if (err) {
1190 printk(KERN_ERR "%s (rtl8180): Cannot enable new PCI device\n",
1191 pci_name(pdev));
1192 return err;
1193 }
1194
1195 err = pci_request_regions(pdev, KBUILD_MODNAME);
1196 if (err) {
1197 printk(KERN_ERR "%s (rtl8180): Cannot obtain PCI resources\n",
1198 pci_name(pdev));
1199 return err;
1200 }
1201
1202 io_addr = pci_resource_start(pdev, 0);
1203 io_len = pci_resource_len(pdev, 0);
1204 mem_addr = pci_resource_start(pdev, 1);
1205 mem_len = pci_resource_len(pdev, 1);
1206
1207 if (mem_len < sizeof(struct rtl818x_csr) ||
1208 io_len < sizeof(struct rtl818x_csr)) {
1209 printk(KERN_ERR "%s (rtl8180): Too short PCI resources\n",
1210 pci_name(pdev));
1211 err = -ENOMEM;
1212 goto err_free_reg;
1213 }
1214
John W. Linville9e385c52010-05-10 14:24:34 -04001215 if ((err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) ||
1216 (err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))) {
Michael Wuf6532112007-10-14 14:43:16 -04001217 printk(KERN_ERR "%s (rtl8180): No suitable DMA available\n",
1218 pci_name(pdev));
1219 goto err_free_reg;
1220 }
1221
1222 pci_set_master(pdev);
1223
1224 dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8180_ops);
1225 if (!dev) {
1226 printk(KERN_ERR "%s (rtl8180): ieee80211 alloc failed\n",
1227 pci_name(pdev));
1228 err = -ENOMEM;
1229 goto err_free_reg;
1230 }
1231
1232 priv = dev->priv;
1233 priv->pdev = pdev;
1234
Johannes Berge6a98542008-10-21 12:40:02 +02001235 dev->max_rates = 2;
Michael Wuf6532112007-10-14 14:43:16 -04001236 SET_IEEE80211_DEV(dev, &pdev->dev);
1237 pci_set_drvdata(pdev, dev);
1238
1239 priv->map = pci_iomap(pdev, 1, mem_len);
1240 if (!priv->map)
1241 priv->map = pci_iomap(pdev, 0, io_len);
1242
1243 if (!priv->map) {
1244 printk(KERN_ERR "%s (rtl8180): Cannot map device memory\n",
1245 pci_name(pdev));
1246 goto err_free_dev;
1247 }
1248
Johannes Berg8318d782008-01-24 19:38:38 +01001249 BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
1250 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
1251
Michael Wuf6532112007-10-14 14:43:16 -04001252 memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
1253 memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
Johannes Berg8318d782008-01-24 19:38:38 +01001254
1255 priv->band.band = IEEE80211_BAND_2GHZ;
1256 priv->band.channels = priv->channels;
1257 priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
1258 priv->band.bitrates = priv->rates;
1259 priv->band.n_bitrates = 4;
1260 dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
1261
Michael Wuf6532112007-10-14 14:43:16 -04001262 dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
Bruno Randolf566bfe52008-05-08 19:15:40 +02001263 IEEE80211_HW_RX_INCLUDES_FCS |
1264 IEEE80211_HW_SIGNAL_UNSPEC;
John W. Linvillec809e862010-05-06 16:49:40 -04001265 dev->vif_data_size = sizeof(struct rtl8180_vif);
1266 dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1267 BIT(NL80211_IFTYPE_ADHOC);
Bruno Randolf566bfe52008-05-08 19:15:40 +02001268 dev->max_signal = 65;
Michael Wuf6532112007-10-14 14:43:16 -04001269
1270 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
1271 reg &= RTL818X_TX_CONF_HWVER_MASK;
1272 switch (reg) {
1273 case RTL818X_TX_CONF_R8180_ABCD:
1274 chip_name = "RTL8180";
Andrea Merello6caefd12014-03-08 18:36:37 +01001275 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
Michael Wuf6532112007-10-14 14:43:16 -04001276 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001277
Michael Wuf6532112007-10-14 14:43:16 -04001278 case RTL818X_TX_CONF_R8180_F:
1279 chip_name = "RTL8180vF";
Andrea Merello6caefd12014-03-08 18:36:37 +01001280 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
Michael Wuf6532112007-10-14 14:43:16 -04001281 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001282
Michael Wuf6532112007-10-14 14:43:16 -04001283 case RTL818X_TX_CONF_R8185_ABC:
1284 chip_name = "RTL8185";
Andrea Merello6caefd12014-03-08 18:36:37 +01001285 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
Michael Wuf6532112007-10-14 14:43:16 -04001286 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001287
Michael Wuf6532112007-10-14 14:43:16 -04001288 case RTL818X_TX_CONF_R8185_D:
1289 chip_name = "RTL8185vD";
Andrea Merello6caefd12014-03-08 18:36:37 +01001290 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
Michael Wuf6532112007-10-14 14:43:16 -04001291 break;
1292 default:
1293 printk(KERN_ERR "%s (rtl8180): Unknown chip! (0x%x)\n",
1294 pci_name(pdev), reg >> 25);
1295 goto err_iounmap;
1296 }
1297
Andrea Merellofd6564f2014-03-22 18:51:20 +01001298 /* we declare to MAC80211 all the queues except for beacon queue
1299 * that will be eventually handled by DRV.
1300 * TX rings are arranged in such a way that lower is the IDX,
1301 * higher is the priority, in order to achieve direct mapping
1302 * with mac80211, however the beacon queue is an exception and it
1303 * is mapped on the highst tx ring IDX.
1304 */
1305 dev->queues = RTL8180_NR_TX_QUEUES - 1;
1306
Andrea Merello6caefd12014-03-08 18:36:37 +01001307 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Johannes Berg8318d782008-01-24 19:38:38 +01001308 priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
Michael Wuf6532112007-10-14 14:43:16 -04001309 pci_try_set_mwi(pdev);
1310 }
1311
Andrea Merello7d4b8292014-03-15 18:29:38 +01001312 rtl8180_eeprom_read(priv);
Michael Wuf6532112007-10-14 14:43:16 -04001313
Andrea Merello7d4b8292014-03-15 18:29:38 +01001314 switch (priv->rf_type) {
Michael Wuf6532112007-10-14 14:43:16 -04001315 case 1: rf_name = "Intersil";
1316 break;
1317 case 2: rf_name = "RFMD";
1318 break;
1319 case 3: priv->rf = &sa2400_rf_ops;
1320 break;
1321 case 4: priv->rf = &max2820_rf_ops;
1322 break;
1323 case 5: priv->rf = &grf5101_rf_ops;
1324 break;
1325 case 9: priv->rf = rtl8180_detect_rf(dev);
1326 break;
1327 case 10:
1328 rf_name = "RTL8255";
1329 break;
1330 default:
1331 printk(KERN_ERR "%s (rtl8180): Unknown RF! (0x%x)\n",
Andrea Merello7d4b8292014-03-15 18:29:38 +01001332 pci_name(pdev), priv->rf_type);
Michael Wuf6532112007-10-14 14:43:16 -04001333 goto err_iounmap;
1334 }
1335
1336 if (!priv->rf) {
1337 printk(KERN_ERR "%s (rtl8180): %s RF frontend not supported!\n",
1338 pci_name(pdev), rf_name);
1339 goto err_iounmap;
1340 }
1341
Andrea Merello7d4b8292014-03-15 18:29:38 +01001342 if (!is_valid_ether_addr(priv->mac_addr)) {
Michael Wuf6532112007-10-14 14:43:16 -04001343 printk(KERN_WARNING "%s (rtl8180): Invalid hwaddr! Using"
1344 " randomly generated MAC addr\n", pci_name(pdev));
Andrea Merello7d4b8292014-03-15 18:29:38 +01001345 eth_random_addr(priv->mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001346 }
Andrea Merello7d4b8292014-03-15 18:29:38 +01001347 SET_IEEE80211_PERM_ADDR(dev, priv->mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001348
1349 spin_lock_init(&priv->lock);
1350
1351 err = ieee80211_register_hw(dev);
1352 if (err) {
1353 printk(KERN_ERR "%s (rtl8180): Cannot register device\n",
1354 pci_name(pdev));
1355 goto err_iounmap;
1356 }
1357
Joe Perchesc96c31e2010-07-26 14:39:58 -07001358 wiphy_info(dev->wiphy, "hwaddr %pm, %s + %s\n",
Andrea Merello7d4b8292014-03-15 18:29:38 +01001359 priv->mac_addr, chip_name, priv->rf->name);
Michael Wuf6532112007-10-14 14:43:16 -04001360
1361 return 0;
1362
1363 err_iounmap:
andrea merello0269da22014-02-18 02:10:41 +01001364 pci_iounmap(pdev, priv->map);
Michael Wuf6532112007-10-14 14:43:16 -04001365
1366 err_free_dev:
Michael Wuf6532112007-10-14 14:43:16 -04001367 ieee80211_free_hw(dev);
1368
1369 err_free_reg:
1370 pci_release_regions(pdev);
1371 pci_disable_device(pdev);
1372 return err;
1373}
1374
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001375static void rtl8180_remove(struct pci_dev *pdev)
Michael Wuf6532112007-10-14 14:43:16 -04001376{
1377 struct ieee80211_hw *dev = pci_get_drvdata(pdev);
1378 struct rtl8180_priv *priv;
1379
1380 if (!dev)
1381 return;
1382
1383 ieee80211_unregister_hw(dev);
1384
1385 priv = dev->priv;
1386
1387 pci_iounmap(pdev, priv->map);
1388 pci_release_regions(pdev);
1389 pci_disable_device(pdev);
1390 ieee80211_free_hw(dev);
1391}
1392
1393#ifdef CONFIG_PM
1394static int rtl8180_suspend(struct pci_dev *pdev, pm_message_t state)
1395{
1396 pci_save_state(pdev);
1397 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1398 return 0;
1399}
1400
1401static int rtl8180_resume(struct pci_dev *pdev)
1402{
1403 pci_set_power_state(pdev, PCI_D0);
1404 pci_restore_state(pdev);
1405 return 0;
1406}
1407
1408#endif /* CONFIG_PM */
1409
1410static struct pci_driver rtl8180_driver = {
1411 .name = KBUILD_MODNAME,
1412 .id_table = rtl8180_table,
1413 .probe = rtl8180_probe,
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001414 .remove = rtl8180_remove,
Michael Wuf6532112007-10-14 14:43:16 -04001415#ifdef CONFIG_PM
1416 .suspend = rtl8180_suspend,
1417 .resume = rtl8180_resume,
1418#endif /* CONFIG_PM */
1419};
1420
Axel Lin5b0a3b72012-04-14 10:38:36 +08001421module_pci_driver(rtl8180_driver);