blob: 50d69b13f9848f2a4913e2452d98224284bbf7c0 [file] [log] [blame]
Michael Wuf6532112007-10-14 14:43:16 -04001
Andrea Merello1eba6482014-03-26 21:04:23 +01002/* Linux device driver for RTL8180 / RTL8185 / RTL8187SE
Michael Wuf6532112007-10-14 14:43:16 -04003 *
4 * Copyright 2007 Michael Wu <flamingice@sourmilk.net>
Andrea Merello1eba6482014-03-26 21:04:23 +01005 * Copyright 2007,2014 Andrea Merello <andrea.merello@gmail.com>
Michael Wuf6532112007-10-14 14:43:16 -04006 *
7 * Based on the r8180 driver, which is:
Andrea Merello93ba2a82013-08-26 13:53:30 +02008 * Copyright 2004-2005 Andrea Merello <andrea.merello@gmail.com>, et al.
Michael Wuf6532112007-10-14 14:43:16 -04009 *
10 * Thanks to Realtek for their support!
11 *
Andrea Merello1eba6482014-03-26 21:04:23 +010012 ************************************************************************
13 *
14 * The driver was extended to the RTL8187SE in 2014 by
15 * Andrea Merello <andrea.merello@gmail.com>
16 *
17 * based also on:
18 * - portions of rtl8187se Linux staging driver, Copyright Realtek corp.
19 * - other GPL, unpublished (until now), Linux driver code,
20 * Copyright Larry Finger <Larry.Finger@lwfinger.net>
21 *
22 * A huge thanks goes to Sara V. Nari who forgives me when I'm
23 * sitting in front of my laptop at evening, week-end, night...
24 *
25 * A special thanks goes to Antonio Cuni, who helped me with
26 * some python userspace stuff I used to debug RTL8187SE code, and who
27 * bought a laptop with an unsupported Wi-Fi card some years ago...
28 *
29 * Thanks to Larry Finger for writing some code for rtl8187se and for
30 * his suggestions.
31 *
32 * Thanks to Dan Carpenter for reviewing my initial patch and for his
33 * suggestions.
34 *
35 * Thanks to Bernhard Schiffner for his help in testing and for his
36 * suggestions.
37 *
38 ************************************************************************
39 *
Michael Wuf6532112007-10-14 14:43:16 -040040 * This program is free software; you can redistribute it and/or modify
41 * it under the terms of the GNU General Public License version 2 as
42 * published by the Free Software Foundation.
43 */
44
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000045#include <linux/interrupt.h>
Michael Wuf6532112007-10-14 14:43:16 -040046#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/slab.h>
Michael Wuf6532112007-10-14 14:43:16 -040048#include <linux/delay.h>
49#include <linux/etherdevice.h>
50#include <linux/eeprom_93cx6.h>
Paul Gortmaker9d9779e2011-07-03 15:21:01 -040051#include <linux/module.h>
Michael Wuf6532112007-10-14 14:43:16 -040052#include <net/mac80211.h>
53
54#include "rtl8180.h"
John W. Linville3cfeb0c2010-12-20 15:16:53 -050055#include "rtl8225.h"
56#include "sa2400.h"
57#include "max2820.h"
58#include "grf5101.h"
Andrea Merello711d4ed32014-03-26 21:02:28 +010059#include "rtl8225se.h"
Michael Wuf6532112007-10-14 14:43:16 -040060
61MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
Andrea Merello93ba2a82013-08-26 13:53:30 +020062MODULE_AUTHOR("Andrea Merello <andrea.merello@gmail.com>");
Andrea Merello1eba6482014-03-26 21:04:23 +010063MODULE_DESCRIPTION("RTL8180 / RTL8185 / RTL8187SE PCI wireless driver");
Michael Wuf6532112007-10-14 14:43:16 -040064MODULE_LICENSE("GPL");
65
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000066static DEFINE_PCI_DEVICE_TABLE(rtl8180_table) = {
Andrea Merello1eba6482014-03-26 21:04:23 +010067
68 /* rtl8187se */
69 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8199) },
70
Michael Wuf6532112007-10-14 14:43:16 -040071 /* rtl8185 */
72 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8185) },
Adrian Bassett4fcc5472008-01-23 16:38:33 +000073 { PCI_DEVICE(PCI_VENDOR_ID_BELKIN, 0x700f) },
Michael Wuf6532112007-10-14 14:43:16 -040074 { PCI_DEVICE(PCI_VENDOR_ID_BELKIN, 0x701f) },
75
76 /* rtl8180 */
77 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8180) },
78 { PCI_DEVICE(0x1799, 0x6001) },
79 { PCI_DEVICE(0x1799, 0x6020) },
80 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x3300) },
Xose Vazquez Perez29a6b502012-06-15 17:27:05 +020081 { PCI_DEVICE(0x1186, 0x3301) },
82 { PCI_DEVICE(0x1432, 0x7106) },
Michael Wuf6532112007-10-14 14:43:16 -040083 { }
84};
85
86MODULE_DEVICE_TABLE(pci, rtl8180_table);
87
Johannes Berg8318d782008-01-24 19:38:38 +010088static const struct ieee80211_rate rtl818x_rates[] = {
89 { .bitrate = 10, .hw_value = 0, },
90 { .bitrate = 20, .hw_value = 1, },
91 { .bitrate = 55, .hw_value = 2, },
92 { .bitrate = 110, .hw_value = 3, },
93 { .bitrate = 60, .hw_value = 4, },
94 { .bitrate = 90, .hw_value = 5, },
95 { .bitrate = 120, .hw_value = 6, },
96 { .bitrate = 180, .hw_value = 7, },
97 { .bitrate = 240, .hw_value = 8, },
98 { .bitrate = 360, .hw_value = 9, },
99 { .bitrate = 480, .hw_value = 10, },
100 { .bitrate = 540, .hw_value = 11, },
101};
102
103static const struct ieee80211_channel rtl818x_channels[] = {
104 { .center_freq = 2412 },
105 { .center_freq = 2417 },
106 { .center_freq = 2422 },
107 { .center_freq = 2427 },
108 { .center_freq = 2432 },
109 { .center_freq = 2437 },
110 { .center_freq = 2442 },
111 { .center_freq = 2447 },
112 { .center_freq = 2452 },
113 { .center_freq = 2457 },
114 { .center_freq = 2462 },
115 { .center_freq = 2467 },
116 { .center_freq = 2472 },
117 { .center_freq = 2484 },
118};
119
Andrea Merello3ee44d62014-03-26 21:00:57 +0100120/* Queues for rtl8187se card
121 *
122 * name | reg | queue
123 * BC | 7 | 6
124 * MG | 1 | 0
125 * HI | 6 | 1
126 * VO | 5 | 2
127 * VI | 4 | 3
128 * BE | 3 | 4
129 * BK | 2 | 5
130 *
131 * The complete map for DMA kick reg using use all queue is:
132 * static const int rtl8187se_queues_map[RTL8187SE_NR_TX_QUEUES] =
133 * {1, 6, 5, 4, 3, 2, 7};
134 *
135 * .. but.. Because for mac80211 4 queues are enough for QoS we use this
136 *
137 * name | reg | queue
138 * BC | 7 | 4 <- currently not used yet
139 * MG | 1 | x <- Not used
140 * HI | 6 | x <- Not used
141 * VO | 5 | 0 <- used
142 * VI | 4 | 1 <- used
143 * BE | 3 | 2 <- used
144 * BK | 2 | 3 <- used
145 *
146 * Beacon queue could be used, but this is not finished yet.
147 *
148 * I thougth about using the other two queues but I decided not to do this:
149 *
150 * - I'm unsure whether the mac80211 will ever try to use more than 4 queues
151 * by itself.
152 *
153 * - I could route MGMT frames (currently sent over VO queue) to the MGMT
154 * queue but since mac80211 will do not know about it, I will probably gain
155 * some HW priority whenever the VO queue is not empty, but this gain is
156 * limited by the fact that I had to stop the mac80211 queue whenever one of
157 * the VO or MGMT queues is full, stopping also submitting of MGMT frame
158 * to the driver.
159 *
160 * - I don't know how to set in the HW the contention window params for MGMT
161 * and HI-prio queues.
162 */
163
164static const int rtl8187se_queues_map[RTL8187SE_NR_TX_QUEUES] = {5, 4, 3, 2, 7};
165
Andrea Merellofd6564f2014-03-22 18:51:20 +0100166/* Queues for rtl8180/rtl8185 cards
167 *
168 * name | reg | prio
169 * BC | 7 | 3
170 * HI | 6 | 0
171 * NO | 5 | 1
172 * LO | 4 | 2
173 *
174 * The complete map for DMA kick reg using all queue is:
175 * static const int rtl8180_queues_map[RTL8180_NR_TX_QUEUES] = {6, 5, 4, 7};
176 *
177 * .. but .. Because the mac80211 needs at least 4 queues for QoS or
178 * otherwise QoS can't be done, we use just one.
179 * Beacon queue could be used, but this is not finished yet.
180 * Actual map is:
181 *
182 * name | reg | prio
183 * BC | 7 | 1 <- currently not used yet.
184 * HI | 6 | x <- not used
185 * NO | 5 | x <- not used
186 * LO | 4 | 0 <- used
187 */
188
189static const int rtl8180_queues_map[RTL8180_NR_TX_QUEUES] = {4, 7};
Johannes Berg8318d782008-01-24 19:38:38 +0100190
Michael Wuf6532112007-10-14 14:43:16 -0400191void rtl8180_write_phy(struct ieee80211_hw *dev, u8 addr, u32 data)
192{
193 struct rtl8180_priv *priv = dev->priv;
194 int i = 10;
195 u32 buf;
196
197 buf = (data << 8) | addr;
198
199 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->PHY[0], buf | 0x80);
200 while (i--) {
201 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->PHY[0], buf);
202 if (rtl818x_ioread8(priv, &priv->map->PHY[2]) == (data & 0xFF))
203 return;
204 }
205}
206
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400207static void rtl8180_handle_rx(struct ieee80211_hw *dev)
Michael Wuf6532112007-10-14 14:43:16 -0400208{
209 struct rtl8180_priv *priv = dev->priv;
Andrea Merello21025922014-03-26 20:59:52 +0100210 struct rtl818x_rx_cmd_desc *cmd_desc;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400211 unsigned int count = 32;
John W. Linville8b73fb82010-07-21 16:26:40 -0400212 u8 signal, agc, sq;
andrea.merello2b4db052014-02-05 22:38:05 +0100213 dma_addr_t mapping;
Michael Wuf6532112007-10-14 14:43:16 -0400214
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400215 while (count--) {
Andrea Merello21025922014-03-26 20:59:52 +0100216 void *entry = priv->rx_ring + priv->rx_idx * priv->rx_ring_sz;
Michael Wuf6532112007-10-14 14:43:16 -0400217 struct sk_buff *skb = priv->rx_buf[priv->rx_idx];
Andrea Merello21025922014-03-26 20:59:52 +0100218 u32 flags, flags2;
219 u64 tsft;
220
221 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
222 struct rtl8187se_rx_desc *desc = entry;
223
224 flags = le32_to_cpu(desc->flags);
225 flags2 = le32_to_cpu(desc->flags2);
226 tsft = le64_to_cpu(desc->tsft);
227 } else {
228 struct rtl8180_rx_desc *desc = entry;
229
230 flags = le32_to_cpu(desc->flags);
231 flags2 = le32_to_cpu(desc->flags2);
232 tsft = le64_to_cpu(desc->tsft);
233 }
Michael Wuf6532112007-10-14 14:43:16 -0400234
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300235 if (flags & RTL818X_RX_DESC_FLAG_OWN)
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400236 return;
Michael Wuf6532112007-10-14 14:43:16 -0400237
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300238 if (unlikely(flags & (RTL818X_RX_DESC_FLAG_DMA_FAIL |
239 RTL818X_RX_DESC_FLAG_FOF |
240 RTL818X_RX_DESC_FLAG_RX_ERR)))
Michael Wuf6532112007-10-14 14:43:16 -0400241 goto done;
242 else {
Michael Wuf6532112007-10-14 14:43:16 -0400243 struct ieee80211_rx_status rx_status = {0};
244 struct sk_buff *new_skb = dev_alloc_skb(MAX_RX_SIZE);
245
246 if (unlikely(!new_skb))
247 goto done;
248
andrea.merello2b4db052014-02-05 22:38:05 +0100249 mapping = pci_map_single(priv->pdev,
250 skb_tail_pointer(new_skb),
251 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
252
253 if (pci_dma_mapping_error(priv->pdev, mapping)) {
254 kfree_skb(new_skb);
255 dev_err(&priv->pdev->dev, "RX DMA map error\n");
256
257 goto done;
258 }
259
Michael Wuf6532112007-10-14 14:43:16 -0400260 pci_unmap_single(priv->pdev,
261 *((dma_addr_t *)skb->cb),
262 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
263 skb_put(skb, flags & 0xFFF);
264
265 rx_status.antenna = (flags2 >> 15) & 1;
Johannes Berg8318d782008-01-24 19:38:38 +0100266 rx_status.rate_idx = (flags >> 20) & 0xF;
John W. Linville8b73fb82010-07-21 16:26:40 -0400267 agc = (flags2 >> 17) & 0x7F;
Andrea Merello6caefd12014-03-08 18:36:37 +0100268
269 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
John W. Linville8b73fb82010-07-21 16:26:40 -0400270 if (rx_status.rate_idx > 3)
271 signal = 90 - clamp_t(u8, agc, 25, 90);
272 else
273 signal = 95 - clamp_t(u8, agc, 30, 95);
Andrea Merello21025922014-03-26 20:59:52 +0100274 } else if (priv->chip_family ==
275 RTL818X_CHIP_FAMILY_RTL8180) {
John W. Linville8b73fb82010-07-21 16:26:40 -0400276 sq = flags2 & 0xff;
277 signal = priv->rf->calc_rssi(agc, sq);
Andrea Merello21025922014-03-26 20:59:52 +0100278 } else {
279 /* TODO: rtl8187se rssi */
280 signal = 10;
John W. Linville8b73fb82010-07-21 16:26:40 -0400281 }
John W. Linville8b749642010-07-19 16:35:20 -0400282 rx_status.signal = signal;
Karl Beldan675a0b02013-03-25 16:26:57 +0100283 rx_status.freq = dev->conf.chandef.chan->center_freq;
284 rx_status.band = dev->conf.chandef.chan->band;
Andrea Merello21025922014-03-26 20:59:52 +0100285 rx_status.mactime = tsft;
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800286 rx_status.flag |= RX_FLAG_MACTIME_START;
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300287 if (flags & RTL818X_RX_DESC_FLAG_CRC32_ERR)
Michael Wuf6532112007-10-14 14:43:16 -0400288 rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
289
Johannes Bergf1d58c22009-06-17 13:13:00 +0200290 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400291 ieee80211_rx_irqsafe(dev, skb);
Michael Wuf6532112007-10-14 14:43:16 -0400292
293 skb = new_skb;
294 priv->rx_buf[priv->rx_idx] = skb;
andrea.merello2b4db052014-02-05 22:38:05 +0100295 *((dma_addr_t *) skb->cb) = mapping;
Michael Wuf6532112007-10-14 14:43:16 -0400296 }
297
298 done:
Andrea Merello21025922014-03-26 20:59:52 +0100299 cmd_desc = entry;
300 cmd_desc->rx_buf = cpu_to_le32(*((dma_addr_t *)skb->cb));
301 cmd_desc->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
Michael Wuf6532112007-10-14 14:43:16 -0400302 MAX_RX_SIZE);
303 if (priv->rx_idx == 31)
Andrea Merello21025922014-03-26 20:59:52 +0100304 cmd_desc->flags |=
305 cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
Michael Wuf6532112007-10-14 14:43:16 -0400306 priv->rx_idx = (priv->rx_idx + 1) % 32;
307 }
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400308}
Michael Wuf6532112007-10-14 14:43:16 -0400309
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400310static void rtl8180_handle_tx(struct ieee80211_hw *dev, unsigned int prio)
311{
312 struct rtl8180_priv *priv = dev->priv;
313 struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
Michael Wuf6532112007-10-14 14:43:16 -0400314
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400315 while (skb_queue_len(&ring->queue)) {
316 struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
317 struct sk_buff *skb;
318 struct ieee80211_tx_info *info;
319 u32 flags = le32_to_cpu(entry->flags);
320
321 if (flags & RTL818X_TX_DESC_FLAG_OWN)
322 return;
323
324 ring->idx = (ring->idx + 1) % ring->entries;
325 skb = __skb_dequeue(&ring->queue);
326 pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
327 skb->len, PCI_DMA_TODEVICE);
328
329 info = IEEE80211_SKB_CB(skb);
330 ieee80211_tx_info_clear_status(info);
331
332 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
333 (flags & RTL818X_TX_DESC_FLAG_TX_OK))
334 info->flags |= IEEE80211_TX_STAT_ACK;
335
336 info->status.rates[0].count = (flags & 0xFF) + 1;
337 info->status.rates[1].idx = -1;
338
339 ieee80211_tx_status_irqsafe(dev, skb);
340 if (ring->entries - skb_queue_len(&ring->queue) == 2)
341 ieee80211_wake_queue(dev, prio);
Michael Wuf6532112007-10-14 14:43:16 -0400342 }
343}
344
Andrea Merelloa373ebc2014-03-26 21:00:06 +0100345static irqreturn_t rtl8187se_interrupt(int irq, void *dev_id)
346{
347 struct ieee80211_hw *dev = dev_id;
348 struct rtl8180_priv *priv = dev->priv;
349 u32 reg;
350 unsigned long flags;
351 static int desc_err;
352
353 spin_lock_irqsave(&priv->lock, flags);
354 /* Note: 32-bit interrupt status */
355 reg = rtl818x_ioread32(priv, &priv->map->INT_STATUS_SE);
356 if (unlikely(reg == 0xFFFFFFFF)) {
357 spin_unlock_irqrestore(&priv->lock, flags);
358 return IRQ_HANDLED;
359 }
360
361 rtl818x_iowrite32(priv, &priv->map->INT_STATUS_SE, reg);
362
363 if (reg & IMR_TIMEOUT1)
364 rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
365
366 if (reg & (IMR_TBDOK | IMR_TBDER))
367 rtl8180_handle_tx(dev, 4);
368
369 if (reg & (IMR_TVODOK | IMR_TVODER))
370 rtl8180_handle_tx(dev, 0);
371
372 if (reg & (IMR_TVIDOK | IMR_TVIDER))
373 rtl8180_handle_tx(dev, 1);
374
375 if (reg & (IMR_TBEDOK | IMR_TBEDER))
376 rtl8180_handle_tx(dev, 2);
377
378 if (reg & (IMR_TBKDOK | IMR_TBKDER))
379 rtl8180_handle_tx(dev, 3);
380
381 if (reg & (IMR_ROK | IMR_RER | RTL818X_INT_SE_RX_DU | IMR_RQOSOK))
382 rtl8180_handle_rx(dev);
383 /* The interface sometimes generates several RX DMA descriptor errors
384 * at startup. Do not report these.
385 */
386 if ((reg & RTL818X_INT_SE_RX_DU) && desc_err++ > 2)
387 if (net_ratelimit())
388 wiphy_err(dev->wiphy, "No RX DMA Descriptor avail\n");
389
390 spin_unlock_irqrestore(&priv->lock, flags);
391 return IRQ_HANDLED;
392}
393
Michael Wuf6532112007-10-14 14:43:16 -0400394static irqreturn_t rtl8180_interrupt(int irq, void *dev_id)
395{
396 struct ieee80211_hw *dev = dev_id;
397 struct rtl8180_priv *priv = dev->priv;
398 u16 reg;
399
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400400 spin_lock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400401 reg = rtl818x_ioread16(priv, &priv->map->INT_STATUS);
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400402 if (unlikely(reg == 0xFFFF)) {
403 spin_unlock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400404 return IRQ_HANDLED;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400405 }
Michael Wuf6532112007-10-14 14:43:16 -0400406
407 rtl818x_iowrite16(priv, &priv->map->INT_STATUS, reg);
408
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400409 if (reg & (RTL818X_INT_TXB_OK | RTL818X_INT_TXB_ERR))
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400410 rtl8180_handle_tx(dev, 1);
411
412 if (reg & (RTL818X_INT_TXL_OK | RTL818X_INT_TXL_ERR))
413 rtl8180_handle_tx(dev, 0);
414
415 if (reg & (RTL818X_INT_RX_OK | RTL818X_INT_RX_ERR))
416 rtl8180_handle_rx(dev);
417
418 spin_unlock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400419
420 return IRQ_HANDLED;
421}
422
Thomas Huehn36323f82012-07-23 21:33:42 +0200423static void rtl8180_tx(struct ieee80211_hw *dev,
424 struct ieee80211_tx_control *control,
425 struct sk_buff *skb)
Michael Wuf6532112007-10-14 14:43:16 -0400426{
Johannes Berge039fa42008-05-15 12:55:29 +0200427 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
John W. Linville51e080d2010-05-06 16:26:23 -0400428 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Michael Wuf6532112007-10-14 14:43:16 -0400429 struct rtl8180_priv *priv = dev->priv;
430 struct rtl8180_tx_ring *ring;
431 struct rtl8180_tx_desc *entry;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400432 unsigned long flags;
Andrea Merellofd6564f2014-03-22 18:51:20 +0100433 unsigned int idx, prio, hw_prio;
Michael Wuf6532112007-10-14 14:43:16 -0400434 dma_addr_t mapping;
435 u32 tx_flags;
Johannes Berge6a98542008-10-21 12:40:02 +0200436 u8 rc_flags;
Michael Wuf6532112007-10-14 14:43:16 -0400437 u16 plcp_len = 0;
438 __le16 rts_duration = 0;
Andrea Merello3ee44d62014-03-26 21:00:57 +0100439 /* do arithmetic and then convert to le16 */
440 u16 frame_duration = 0;
Michael Wuf6532112007-10-14 14:43:16 -0400441
Johannes Berge2530082008-05-17 00:57:14 +0200442 prio = skb_get_queue_mapping(skb);
Michael Wuf6532112007-10-14 14:43:16 -0400443 ring = &priv->tx_ring[prio];
444
445 mapping = pci_map_single(priv->pdev, skb->data,
446 skb->len, PCI_DMA_TODEVICE);
447
andrea.merello348f7d42014-02-05 22:38:06 +0100448 if (pci_dma_mapping_error(priv->pdev, mapping)) {
449 kfree_skb(skb);
450 dev_err(&priv->pdev->dev, "TX DMA mapping error\n");
451 return;
andrea.merello348f7d42014-02-05 22:38:06 +0100452 }
453
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300454 tx_flags = RTL818X_TX_DESC_FLAG_OWN | RTL818X_TX_DESC_FLAG_FS |
455 RTL818X_TX_DESC_FLAG_LS |
Johannes Berge039fa42008-05-15 12:55:29 +0200456 (ieee80211_get_tx_rate(dev, info)->hw_value << 24) |
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200457 skb->len;
Michael Wuf6532112007-10-14 14:43:16 -0400458
Andrea Merello6caefd12014-03-08 18:36:37 +0100459 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300460 tx_flags |= RTL818X_TX_DESC_FLAG_DMA |
461 RTL818X_TX_DESC_FLAG_NO_ENC;
Michael Wuf6532112007-10-14 14:43:16 -0400462
Johannes Berge6a98542008-10-21 12:40:02 +0200463 rc_flags = info->control.rates[0].flags;
464 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300465 tx_flags |= RTL818X_TX_DESC_FLAG_RTS;
Johannes Berge039fa42008-05-15 12:55:29 +0200466 tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
Johannes Berge6a98542008-10-21 12:40:02 +0200467 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300468 tx_flags |= RTL818X_TX_DESC_FLAG_CTS;
Johannes Berge039fa42008-05-15 12:55:29 +0200469 tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
Johannes Bergaa68cbf2008-02-18 14:20:30 +0100470 }
Michael Wuf6532112007-10-14 14:43:16 -0400471
Johannes Berge6a98542008-10-21 12:40:02 +0200472 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS)
Johannes Berg32bfd352007-12-19 01:31:26 +0100473 rts_duration = ieee80211_rts_duration(dev, priv->vif, skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +0200474 info);
Michael Wuf6532112007-10-14 14:43:16 -0400475
Andrea Merello6caefd12014-03-08 18:36:37 +0100476 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400477 unsigned int remainder;
478
479 plcp_len = DIV_ROUND_UP(16 * (skb->len + 4),
Johannes Berge039fa42008-05-15 12:55:29 +0200480 (ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
Michael Wuf6532112007-10-14 14:43:16 -0400481 remainder = (16 * (skb->len + 4)) %
Johannes Berge039fa42008-05-15 12:55:29 +0200482 ((ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
Roel Kluin35a0ace2009-06-22 17:42:21 +0200483 if (remainder <= 6)
Michael Wuf6532112007-10-14 14:43:16 -0400484 plcp_len |= 1 << 15;
485 }
486
Andrea Merello3ee44d62014-03-26 21:00:57 +0100487 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
488 __le16 duration;
489 /* SIFS time (required by HW) is already included by
490 * ieee80211_generic_frame_duration
491 */
492 duration = ieee80211_generic_frame_duration(dev, priv->vif,
493 IEEE80211_BAND_2GHZ, skb->len,
494 ieee80211_get_tx_rate(dev, info));
495
496 frame_duration = priv->ack_time + le16_to_cpu(duration);
497 }
498
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400499 spin_lock_irqsave(&priv->lock, flags);
John W. Linville51e080d2010-05-06 16:26:23 -0400500
501 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
502 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
503 priv->seqno += 0x10;
504 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
505 hdr->seq_ctrl |= cpu_to_le16(priv->seqno);
506 }
507
Michael Wuf6532112007-10-14 14:43:16 -0400508 idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
509 entry = &ring->desc[idx];
510
Andrea Merello3ee44d62014-03-26 21:00:57 +0100511 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
512 entry->frame_duration = cpu_to_le16(frame_duration);
513 entry->frame_len_se = cpu_to_le16(skb->len);
514
515 /* tpc polarity */
516 entry->flags3 = cpu_to_le16(1<<4);
517 } else
518 entry->frame_len = cpu_to_le32(skb->len);
519
Michael Wuf6532112007-10-14 14:43:16 -0400520 entry->rts_duration = rts_duration;
521 entry->plcp_len = cpu_to_le16(plcp_len);
522 entry->tx_buf = cpu_to_le32(mapping);
Andrea Merello3ee44d62014-03-26 21:00:57 +0100523
Johannes Berge6a98542008-10-21 12:40:02 +0200524 entry->flags2 = info->control.rates[1].idx >= 0 ?
Felix Fietkau870abdf2008-10-05 18:04:24 +0200525 ieee80211_get_alt_retry_rate(dev, info, 0)->bitrate << 4 : 0;
Johannes Berge6a98542008-10-21 12:40:02 +0200526 entry->retry_limit = info->control.rates[0].count;
andrea merello4c552a52014-02-18 02:10:45 +0100527
528 /* We must be sure that tx_flags is written last because the HW
529 * looks at it to check if the rest of data is valid or not
530 */
531 wmb();
Michael Wuf6532112007-10-14 14:43:16 -0400532 entry->flags = cpu_to_le32(tx_flags);
andrea merelloc24782e2014-02-18 02:10:46 +0100533 /* We must be sure this has been written before followings HW
534 * register write, because this write will made the HW attempts
535 * to DMA the just-written data
536 */
537 wmb();
538
Michael Wuf6532112007-10-14 14:43:16 -0400539 __skb_queue_tail(&ring->queue, skb);
540 if (ring->entries - skb_queue_len(&ring->queue) < 2)
John W. Linvilled10e2e02010-04-27 16:57:38 -0400541 ieee80211_stop_queue(dev, prio);
John W. Linville51e080d2010-05-06 16:26:23 -0400542
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400543 spin_unlock_irqrestore(&priv->lock, flags);
Michael Wuf6532112007-10-14 14:43:16 -0400544
Andrea Merello3ee44d62014-03-26 21:00:57 +0100545 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
546 /* just poll: rings are stopped with TPPollStop reg */
547 hw_prio = rtl8187se_queues_map[prio];
548 rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
549 (1 << hw_prio));
550 } else {
551 hw_prio = rtl8180_queues_map[prio];
552 rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
Andrea Merellofd6564f2014-03-22 18:51:20 +0100553 (1 << hw_prio) | /* ring to poll */
554 (1<<1) | (1<<2));/* stopped rings */
Andrea Merello3ee44d62014-03-26 21:00:57 +0100555 }
Michael Wuf6532112007-10-14 14:43:16 -0400556}
557
Andrea Merelloff3cbc22014-03-26 21:02:05 +0100558static void rtl8180_set_anaparam3(struct rtl8180_priv *priv, u16 anaparam3)
559{
560 u8 reg;
561
562 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
563 RTL818X_EEPROM_CMD_CONFIG);
564
565 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
566 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
567 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
568
569 rtl818x_iowrite16(priv, &priv->map->ANAPARAM3, anaparam3);
570
571 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
572 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
573
574 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
575 RTL818X_EEPROM_CMD_NORMAL);
576}
577
578void rtl8180_set_anaparam2(struct rtl8180_priv *priv, u32 anaparam2)
579{
580 u8 reg;
581
582 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
583 RTL818X_EEPROM_CMD_CONFIG);
584
585 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
586 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
587 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
588
589 rtl818x_iowrite32(priv, &priv->map->ANAPARAM2, anaparam2);
590
591 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
592 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
593
594 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
595 RTL818X_EEPROM_CMD_NORMAL);
596}
597
Michael Wuf6532112007-10-14 14:43:16 -0400598void rtl8180_set_anaparam(struct rtl8180_priv *priv, u32 anaparam)
599{
600 u8 reg;
601
602 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
603 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
604 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
605 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
606 rtl818x_iowrite32(priv, &priv->map->ANAPARAM, anaparam);
607 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
608 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
609 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
610}
611
Andrea Merello4a67aa52014-03-26 21:02:46 +0100612static void rtl8187se_mac_config(struct ieee80211_hw *dev)
613{
614 struct rtl8180_priv *priv = dev->priv;
615 u8 reg;
616
617 rtl818x_iowrite32(priv, REG_ADDR4(0x1F0), 0);
618 rtl818x_ioread32(priv, REG_ADDR4(0x1F0));
619 rtl818x_iowrite32(priv, REG_ADDR4(0x1F4), 0);
620 rtl818x_ioread32(priv, REG_ADDR4(0x1F4));
621 rtl818x_iowrite8(priv, REG_ADDR1(0x1F8), 0);
622 rtl818x_ioread8(priv, REG_ADDR1(0x1F8));
623 /* Enable DA10 TX power saving */
624 reg = rtl818x_ioread8(priv, &priv->map->PHY_PR);
625 rtl818x_iowrite8(priv, &priv->map->PHY_PR, reg | 0x04);
626 /* Power */
627 rtl818x_iowrite16(priv, PI_DATA_REG, 0x1000);
628 rtl818x_iowrite16(priv, SI_DATA_REG, 0x1000);
629 /* AFE - default to power ON */
630 rtl818x_iowrite16(priv, REG_ADDR2(0x370), 0x0560);
631 rtl818x_iowrite16(priv, REG_ADDR2(0x372), 0x0560);
632 rtl818x_iowrite16(priv, REG_ADDR2(0x374), 0x0DA4);
633 rtl818x_iowrite16(priv, REG_ADDR2(0x376), 0x0DA4);
634 rtl818x_iowrite16(priv, REG_ADDR2(0x378), 0x0560);
635 rtl818x_iowrite16(priv, REG_ADDR2(0x37A), 0x0560);
636 rtl818x_iowrite16(priv, REG_ADDR2(0x37C), 0x00EC);
637 rtl818x_iowrite16(priv, REG_ADDR2(0x37E), 0x00EC);
638 rtl818x_iowrite8(priv, REG_ADDR1(0x24E), 0x01);
639 /* unknown, needed for suspend to RAM resume */
640 rtl818x_iowrite8(priv, REG_ADDR1(0x0A), 0x72);
641}
642
643static void rtl8187se_set_antenna_config(struct ieee80211_hw *dev, u8 def_ant,
644 bool diversity)
645{
646 struct rtl8180_priv *priv = dev->priv;
647
648 rtl8225_write_phy_cck(dev, 0x0C, 0x09);
649 if (diversity) {
650 if (def_ant == 1) {
651 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x00);
652 rtl8225_write_phy_cck(dev, 0x11, 0xBB);
653 rtl8225_write_phy_cck(dev, 0x01, 0xC7);
654 rtl8225_write_phy_ofdm(dev, 0x0D, 0x54);
655 rtl8225_write_phy_ofdm(dev, 0x18, 0xB2);
656 } else { /* main antenna */
657 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x03);
658 rtl8225_write_phy_cck(dev, 0x11, 0x9B);
659 rtl8225_write_phy_cck(dev, 0x01, 0xC7);
660 rtl8225_write_phy_ofdm(dev, 0x0D, 0x5C);
661 rtl8225_write_phy_ofdm(dev, 0x18, 0xB2);
662 }
663 } else { /* disable antenna diversity */
664 if (def_ant == 1) {
665 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x00);
666 rtl8225_write_phy_cck(dev, 0x11, 0xBB);
667 rtl8225_write_phy_cck(dev, 0x01, 0x47);
668 rtl8225_write_phy_ofdm(dev, 0x0D, 0x54);
669 rtl8225_write_phy_ofdm(dev, 0x18, 0x32);
670 } else { /* main antenna */
671 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x03);
672 rtl8225_write_phy_cck(dev, 0x11, 0x9B);
673 rtl8225_write_phy_cck(dev, 0x01, 0x47);
674 rtl8225_write_phy_ofdm(dev, 0x0D, 0x5C);
675 rtl8225_write_phy_ofdm(dev, 0x18, 0x32);
676 }
677 }
678 /* priv->curr_ant = def_ant; */
679}
680
Andrea Merello732c8932014-03-26 21:00:24 +0100681static void rtl8180_int_enable(struct ieee80211_hw *dev)
682{
683 struct rtl8180_priv *priv = dev->priv;
684
685 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
Andrea Merello1c3fb9b2014-04-04 18:21:14 +0200686 rtl818x_iowrite32(priv, &priv->map->IMR,
687 IMR_TBDER | IMR_TBDOK |
Andrea Merello732c8932014-03-26 21:00:24 +0100688 IMR_TVODER | IMR_TVODOK |
689 IMR_TVIDER | IMR_TVIDOK |
690 IMR_TBEDER | IMR_TBEDOK |
691 IMR_TBKDER | IMR_TBKDOK |
692 IMR_RDU | IMR_RER |
693 IMR_ROK | IMR_RQOSOK);
694 } else {
695 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
696 }
697}
698
699static void rtl8180_int_disable(struct ieee80211_hw *dev)
700{
701 struct rtl8180_priv *priv = dev->priv;
702
703 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
704 rtl818x_iowrite32(priv, &priv->map->IMR, 0);
705 } else {
706 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
707 }
708}
709
Andrea Merello516a0932014-03-15 18:29:36 +0100710static void rtl8180_conf_basic_rates(struct ieee80211_hw *dev,
711 u32 rates_mask)
712{
713 struct rtl8180_priv *priv = dev->priv;
714
715 u8 max, min;
716 u16 reg;
717
718 max = fls(rates_mask) - 1;
719 min = ffs(rates_mask) - 1;
720
721 switch (priv->chip_family) {
722
723 case RTL818X_CHIP_FAMILY_RTL8180:
724 /* in 8180 this is NOT a BITMAP */
725 reg = rtl818x_ioread16(priv, &priv->map->BRSR);
726 reg &= ~3;
727 reg |= max;
728 rtl818x_iowrite16(priv, &priv->map->BRSR, reg);
Andrea Merello516a0932014-03-15 18:29:36 +0100729 break;
730
731 case RTL818X_CHIP_FAMILY_RTL8185:
732 /* in 8185 this is a BITMAP */
733 rtl818x_iowrite16(priv, &priv->map->BRSR, rates_mask);
734 rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (max << 4) | min);
735 break;
Andrea Merellod209f3b2014-03-26 20:59:25 +0100736
737 case RTL818X_CHIP_FAMILY_RTL8187SE:
738 /* in 8187se this is a BITMAP */
739 rtl818x_iowrite16(priv, &priv->map->BRSR_8187SE, rates_mask);
740 break;
Andrea Merello516a0932014-03-15 18:29:36 +0100741 }
742}
743
Andrea Merellof1026df2014-03-26 21:01:19 +0100744static void rtl8180_config_cardbus(struct ieee80211_hw *dev)
745{
746 struct rtl8180_priv *priv = dev->priv;
747 u16 reg16;
748 u8 reg8;
749
750 reg8 = rtl818x_ioread8(priv, &priv->map->CONFIG3);
751 reg8 |= 1 << 1;
752 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg8);
753
754 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
755 rtl818x_iowrite16(priv, FEMR_SE, 0xffff);
756 } else {
757 reg16 = rtl818x_ioread16(priv, &priv->map->FEMR);
758 reg16 |= (1 << 15) | (1 << 14) | (1 << 4);
759 rtl818x_iowrite16(priv, &priv->map->FEMR, reg16);
760 }
761
762}
763
Michael Wuf6532112007-10-14 14:43:16 -0400764static int rtl8180_init_hw(struct ieee80211_hw *dev)
765{
766 struct rtl8180_priv *priv = dev->priv;
767 u16 reg;
Andrea Merello4a67aa52014-03-26 21:02:46 +0100768 u32 reg32;
Michael Wuf6532112007-10-14 14:43:16 -0400769
770 rtl818x_iowrite8(priv, &priv->map->CMD, 0);
771 rtl818x_ioread8(priv, &priv->map->CMD);
772 msleep(10);
773
774 /* reset */
Andrea Merello732c8932014-03-26 21:00:24 +0100775 rtl8180_int_disable(dev);
Michael Wuf6532112007-10-14 14:43:16 -0400776 rtl818x_ioread8(priv, &priv->map->CMD);
777
778 reg = rtl818x_ioread8(priv, &priv->map->CMD);
779 reg &= (1 << 1);
780 reg |= RTL818X_CMD_RESET;
781 rtl818x_iowrite8(priv, &priv->map->CMD, RTL818X_CMD_RESET);
782 rtl818x_ioread8(priv, &priv->map->CMD);
783 msleep(200);
784
785 /* check success of reset */
786 if (rtl818x_ioread8(priv, &priv->map->CMD) & RTL818X_CMD_RESET) {
Joe Perchesc96c31e2010-07-26 14:39:58 -0700787 wiphy_err(dev->wiphy, "reset timeout!\n");
Michael Wuf6532112007-10-14 14:43:16 -0400788 return -ETIMEDOUT;
789 }
790
791 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
792 rtl818x_ioread8(priv, &priv->map->CMD);
793 msleep(200);
794
795 if (rtl818x_ioread8(priv, &priv->map->CONFIG3) & (1 << 3)) {
Andrea Merellof1026df2014-03-26 21:01:19 +0100796 rtl8180_config_cardbus(dev);
Michael Wuf6532112007-10-14 14:43:16 -0400797 }
798
Andrea Merello4a67aa52014-03-26 21:02:46 +0100799 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
800 rtl818x_iowrite8(priv, &priv->map->MSR, RTL818X_MSR_ENEDCA);
801 else
802 rtl818x_iowrite8(priv, &priv->map->MSR, 0);
Michael Wuf6532112007-10-14 14:43:16 -0400803
Andrea Merello6caefd12014-03-08 18:36:37 +0100804 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
Michael Wuf6532112007-10-14 14:43:16 -0400805 rtl8180_set_anaparam(priv, priv->anaparam);
806
807 rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
Andrea Merello4a67aa52014-03-26 21:02:46 +0100808 /* mac80211 queue have higher prio for lower index. The last queue
809 * (that mac80211 is not aware of) is reserved for beacons (and have
810 * the highest priority on the NIC)
811 */
812 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8187SE) {
813 rtl818x_iowrite32(priv, &priv->map->TBDA,
814 priv->tx_ring[1].dma);
815 rtl818x_iowrite32(priv, &priv->map->TLPDA,
816 priv->tx_ring[0].dma);
817 } else {
818 rtl818x_iowrite32(priv, &priv->map->TBDA,
819 priv->tx_ring[4].dma);
820 rtl818x_iowrite32(priv, &priv->map->TVODA,
821 priv->tx_ring[0].dma);
822 rtl818x_iowrite32(priv, &priv->map->TVIDA,
823 priv->tx_ring[1].dma);
824 rtl818x_iowrite32(priv, &priv->map->TBEDA,
825 priv->tx_ring[2].dma);
826 rtl818x_iowrite32(priv, &priv->map->TBKDA,
827 priv->tx_ring[3].dma);
828 }
Michael Wuf6532112007-10-14 14:43:16 -0400829
830 /* TODO: necessary? specs indicate not */
831 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
832 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
833 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg & ~(1 << 3));
Andrea Merello6caefd12014-03-08 18:36:37 +0100834 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
Michael Wuf6532112007-10-14 14:43:16 -0400835 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
836 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg | (1 << 4));
837 }
838 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
839
840 /* TODO: set CONFIG5 for calibrating AGC on rtl8180 + philips radio? */
841
842 /* TODO: turn off hw wep on rtl8180 */
843
844 rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
845
Andrea Merello6caefd12014-03-08 18:36:37 +0100846 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400847 rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
848 rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
Andrea Merello4a67aa52014-03-26 21:02:46 +0100849 } else {
850 rtl818x_iowrite8(priv, &priv->map->SECURITY, 0);
Michael Wuf6532112007-10-14 14:43:16 -0400851
Andrea Merello4a67aa52014-03-26 21:02:46 +0100852 rtl818x_iowrite8(priv, &priv->map->PHY_DELAY, 0x6);
853 rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, 0x4C);
854 }
855
856 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
Michael Wuf6532112007-10-14 14:43:16 -0400857 /* TODO: set ClkRun enable? necessary? */
858 reg = rtl818x_ioread8(priv, &priv->map->GP_ENABLE);
859 rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, reg & ~(1 << 6));
860 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
861 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
862 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | (1 << 2));
863 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
Andrea Merello4a67aa52014-03-26 21:02:46 +0100864 }
Michael Wuf6532112007-10-14 14:43:16 -0400865
Andrea Merello4a67aa52014-03-26 21:02:46 +0100866 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
867
868 /* the set auto rate fallback bitmask from 1M to 54 Mb/s */
869 rtl818x_iowrite16(priv, ARFR, 0xFFF);
870 rtl818x_ioread16(priv, ARFR);
871
872 /* stop unused queus (no dma alloc) */
873 rtl818x_iowrite8(priv, &priv->map->TPPOLL_STOP,
874 RTL818x_TPPOLL_STOP_MG | RTL818x_TPPOLL_STOP_HI);
875
876 rtl818x_iowrite8(priv, &priv->map->ACM_CONTROL, 0x00);
877 rtl818x_iowrite16(priv, &priv->map->TID_AC_MAP, 0xFA50);
878
879 rtl818x_iowrite16(priv, &priv->map->INT_MIG, 0);
880
881 /* some black magic here.. */
882 rtl8187se_mac_config(dev);
883
884 rtl818x_iowrite16(priv, RFSW_CTRL, 0x569A);
885 rtl818x_ioread16(priv, RFSW_CTRL);
886
887 rtl8180_set_anaparam(priv, RTL8225SE_ANAPARAM_ON);
888 rtl8180_set_anaparam2(priv, RTL8225SE_ANAPARAM2_ON);
889 rtl8180_set_anaparam3(priv, RTL8225SE_ANAPARAM3);
890
891
892 rtl818x_iowrite8(priv, &priv->map->CONFIG5,
893 rtl818x_ioread8(priv, &priv->map->CONFIG5) & 0x7F);
894
895 /*probably this switch led on */
896 rtl818x_iowrite8(priv, &priv->map->PGSELECT,
897 rtl818x_ioread8(priv, &priv->map->PGSELECT) | 0x08);
898
899 rtl818x_iowrite16(priv, &priv->map->RFPinsOutput, 0x0480);
900 rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x1BFF);
901 rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0x2488);
902
903 rtl818x_iowrite32(priv, &priv->map->RF_TIMING, 0x4003);
904
905 /* the reference code mac hardcode table write
906 * this reg by doing byte-wide accesses.
907 * It does it just for lowest and highest byte..
908 */
909 reg32 = rtl818x_ioread32(priv, &priv->map->RF_PARA);
910 reg32 &= 0x00ffff00;
911 reg32 |= 0xb8000054;
912 rtl818x_iowrite32(priv, &priv->map->RF_PARA, reg32);
Andrea Merello294bc612014-04-04 18:25:51 +0200913 } else
914 /* stop unused queus (no dma alloc) */
915 rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
916 (1<<1) | (1<<2));
Michael Wuf6532112007-10-14 14:43:16 -0400917
918 priv->rf->init(dev);
Andrea Merello516a0932014-03-15 18:29:36 +0100919
920 /* default basic rates are 1,2 Mbps for rtl8180. 1,2,6,9,12,18,24 Mbps
921 * otherwise. bitmask 0x3 and 0x01f3 respectively.
922 * NOTE: currenty rtl8225 RF code changes basic rates, so we need to do
923 * this after rf init.
924 * TODO: try to find out whether RF code really needs to do this..
925 */
926 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
927 rtl8180_conf_basic_rates(dev, 0x3);
928 else
929 rtl8180_conf_basic_rates(dev, 0x1f3);
930
Andrea Merello4a67aa52014-03-26 21:02:46 +0100931 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
932 rtl8187se_set_antenna_config(dev,
933 priv->antenna_diversity_default,
934 priv->antenna_diversity_en);
Michael Wuf6532112007-10-14 14:43:16 -0400935 return 0;
936}
937
938static int rtl8180_init_rx_ring(struct ieee80211_hw *dev)
939{
940 struct rtl8180_priv *priv = dev->priv;
Andrea Merello21025922014-03-26 20:59:52 +0100941 struct rtl818x_rx_cmd_desc *entry;
Michael Wuf6532112007-10-14 14:43:16 -0400942 int i;
943
Andrea Merello21025922014-03-26 20:59:52 +0100944 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
945 priv->rx_ring_sz = sizeof(struct rtl8187se_rx_desc);
946 else
947 priv->rx_ring_sz = sizeof(struct rtl8180_rx_desc);
948
Michael Wuf6532112007-10-14 14:43:16 -0400949 priv->rx_ring = pci_alloc_consistent(priv->pdev,
Andrea Merello21025922014-03-26 20:59:52 +0100950 priv->rx_ring_sz * 32,
Michael Wuf6532112007-10-14 14:43:16 -0400951 &priv->rx_ring_dma);
952
953 if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -0700954 wiphy_err(dev->wiphy, "Cannot allocate RX ring\n");
Michael Wuf6532112007-10-14 14:43:16 -0400955 return -ENOMEM;
956 }
957
Andrea Merello21025922014-03-26 20:59:52 +0100958 memset(priv->rx_ring, 0, priv->rx_ring_sz * 32);
Michael Wuf6532112007-10-14 14:43:16 -0400959 priv->rx_idx = 0;
960
961 for (i = 0; i < 32; i++) {
962 struct sk_buff *skb = dev_alloc_skb(MAX_RX_SIZE);
963 dma_addr_t *mapping;
Andrea Merello21025922014-03-26 20:59:52 +0100964 entry = priv->rx_ring + priv->rx_ring_sz*i;
andrea merello4da18bb2014-02-18 02:10:43 +0100965 if (!skb) {
966 wiphy_err(dev->wiphy, "Cannot allocate RX skb\n");
967 return -ENOMEM;
968 }
Michael Wuf6532112007-10-14 14:43:16 -0400969 priv->rx_buf[i] = skb;
970 mapping = (dma_addr_t *)skb->cb;
971 *mapping = pci_map_single(priv->pdev, skb_tail_pointer(skb),
972 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
andrea merelloec1da082014-02-22 17:57:23 +0100973
974 if (pci_dma_mapping_error(priv->pdev, *mapping)) {
975 kfree_skb(skb);
976 wiphy_err(dev->wiphy, "Cannot map DMA for RX skb\n");
977 return -ENOMEM;
978 }
979
Michael Wuf6532112007-10-14 14:43:16 -0400980 entry->rx_buf = cpu_to_le32(*mapping);
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300981 entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
Michael Wuf6532112007-10-14 14:43:16 -0400982 MAX_RX_SIZE);
983 }
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300984 entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
Michael Wuf6532112007-10-14 14:43:16 -0400985 return 0;
986}
987
988static void rtl8180_free_rx_ring(struct ieee80211_hw *dev)
989{
990 struct rtl8180_priv *priv = dev->priv;
991 int i;
992
993 for (i = 0; i < 32; i++) {
994 struct sk_buff *skb = priv->rx_buf[i];
995 if (!skb)
996 continue;
997
998 pci_unmap_single(priv->pdev,
999 *((dma_addr_t *)skb->cb),
1000 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
1001 kfree_skb(skb);
1002 }
1003
Andrea Merello21025922014-03-26 20:59:52 +01001004 pci_free_consistent(priv->pdev, priv->rx_ring_sz * 32,
Michael Wuf6532112007-10-14 14:43:16 -04001005 priv->rx_ring, priv->rx_ring_dma);
1006 priv->rx_ring = NULL;
1007}
1008
1009static int rtl8180_init_tx_ring(struct ieee80211_hw *dev,
1010 unsigned int prio, unsigned int entries)
1011{
1012 struct rtl8180_priv *priv = dev->priv;
1013 struct rtl8180_tx_desc *ring;
1014 dma_addr_t dma;
1015 int i;
1016
1017 ring = pci_alloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
1018 if (!ring || (unsigned long)ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -07001019 wiphy_err(dev->wiphy, "Cannot allocate TX ring (prio = %d)\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001020 prio);
Michael Wuf6532112007-10-14 14:43:16 -04001021 return -ENOMEM;
1022 }
1023
1024 memset(ring, 0, sizeof(*ring)*entries);
1025 priv->tx_ring[prio].desc = ring;
1026 priv->tx_ring[prio].dma = dma;
1027 priv->tx_ring[prio].idx = 0;
1028 priv->tx_ring[prio].entries = entries;
1029 skb_queue_head_init(&priv->tx_ring[prio].queue);
1030
1031 for (i = 0; i < entries; i++)
1032 ring[i].next_tx_desc =
1033 cpu_to_le32((u32)dma + ((i + 1) % entries) * sizeof(*ring));
1034
1035 return 0;
1036}
1037
1038static void rtl8180_free_tx_ring(struct ieee80211_hw *dev, unsigned int prio)
1039{
1040 struct rtl8180_priv *priv = dev->priv;
1041 struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
1042
1043 while (skb_queue_len(&ring->queue)) {
1044 struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
1045 struct sk_buff *skb = __skb_dequeue(&ring->queue);
1046
1047 pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
1048 skb->len, PCI_DMA_TODEVICE);
Michael Wuf6532112007-10-14 14:43:16 -04001049 kfree_skb(skb);
1050 ring->idx = (ring->idx + 1) % ring->entries;
1051 }
1052
1053 pci_free_consistent(priv->pdev, sizeof(*ring->desc)*ring->entries,
1054 ring->desc, ring->dma);
1055 ring->desc = NULL;
1056}
1057
1058static int rtl8180_start(struct ieee80211_hw *dev)
1059{
1060 struct rtl8180_priv *priv = dev->priv;
1061 int ret, i;
1062 u32 reg;
1063
1064 ret = rtl8180_init_rx_ring(dev);
1065 if (ret)
1066 return ret;
1067
Andrea Merellofd6564f2014-03-22 18:51:20 +01001068 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -04001069 if ((ret = rtl8180_init_tx_ring(dev, i, 16)))
1070 goto err_free_rings;
1071
1072 ret = rtl8180_init_hw(dev);
1073 if (ret)
1074 goto err_free_rings;
1075
Andrea Merelloa373ebc2014-03-26 21:00:06 +01001076 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1077 ret = request_irq(priv->pdev->irq, rtl8187se_interrupt,
Michael Wuf6532112007-10-14 14:43:16 -04001078 IRQF_SHARED, KBUILD_MODNAME, dev);
Andrea Merelloa373ebc2014-03-26 21:00:06 +01001079 } else {
1080 ret = request_irq(priv->pdev->irq, rtl8180_interrupt,
1081 IRQF_SHARED, KBUILD_MODNAME, dev);
1082 }
1083
Michael Wuf6532112007-10-14 14:43:16 -04001084 if (ret) {
Joe Perches5db55842010-08-11 19:11:19 -07001085 wiphy_err(dev->wiphy, "failed to register IRQ handler\n");
Michael Wuf6532112007-10-14 14:43:16 -04001086 goto err_free_rings;
1087 }
1088
Andrea Merello732c8932014-03-26 21:00:24 +01001089 rtl8180_int_enable(dev);
Michael Wuf6532112007-10-14 14:43:16 -04001090
Andrea Merellof18f1122014-03-26 21:00:42 +01001091 /* in rtl8187se at MAR regs offset there is the management
1092 * TX descriptor DMA addres..
1093 */
1094 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8187SE) {
1095 rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
1096 rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
1097 }
Michael Wuf6532112007-10-14 14:43:16 -04001098
1099 reg = RTL818X_RX_CONF_ONLYERLPKT |
1100 RTL818X_RX_CONF_RX_AUTORESETPHY |
1101 RTL818X_RX_CONF_MGMT |
1102 RTL818X_RX_CONF_DATA |
1103 (7 << 8 /* MAX RX DMA */) |
1104 RTL818X_RX_CONF_BROADCAST |
1105 RTL818X_RX_CONF_NICMAC;
1106
Andrea Merello6caefd12014-03-08 18:36:37 +01001107 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185)
Michael Wuf6532112007-10-14 14:43:16 -04001108 reg |= RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2;
Andrea Merello4a67aa52014-03-26 21:02:46 +01001109 else if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -04001110 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE1)
1111 ? RTL818X_RX_CONF_CSDM1 : 0;
1112 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE2)
1113 ? RTL818X_RX_CONF_CSDM2 : 0;
Andrea Merello4a67aa52014-03-26 21:02:46 +01001114 } else {
1115 reg &= ~(RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2);
Michael Wuf6532112007-10-14 14:43:16 -04001116 }
1117
1118 priv->rx_conf = reg;
1119 rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
1120
Andrea Merello6caefd12014-03-08 18:36:37 +01001121 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -04001122 reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
andrea merello14c76152014-02-18 02:10:44 +01001123
1124 /* CW is not on per-packet basis.
1125 * in rtl8185 the CW_VALUE reg is used.
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001126 * in rtl8187se the AC param regs are used.
andrea merello14c76152014-02-18 02:10:44 +01001127 */
andrea merello6f7343d2014-01-21 20:16:43 +01001128 reg &= ~RTL818X_CW_CONF_PERPACKET_CW;
andrea merello14c76152014-02-18 02:10:44 +01001129 /* retry limit IS on per-packet basis.
1130 * the short and long retry limit in TX_CONF
1131 * reg are ignored
1132 */
andrea merello6f7343d2014-01-21 20:16:43 +01001133 reg |= RTL818X_CW_CONF_PERPACKET_RETRY;
Michael Wuf6532112007-10-14 14:43:16 -04001134 rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
1135
1136 reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
andrea merello14c76152014-02-18 02:10:44 +01001137 /* TX antenna and TX gain are not on per-packet basis.
1138 * TX Antenna is selected by ANTSEL reg (RX in BB regs).
1139 * TX gain is selected with CCK_TX_AGC and OFDM_TX_AGC regs
1140 */
andrea merello6f7343d2014-01-21 20:16:43 +01001141 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN;
1142 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL;
Michael Wuf6532112007-10-14 14:43:16 -04001143 reg |= RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
1144 rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
1145
1146 /* disable early TX */
1147 rtl818x_iowrite8(priv, (u8 __iomem *)priv->map + 0xec, 0x3f);
1148 }
1149
1150 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
1151 reg |= (6 << 21 /* MAX TX DMA */) |
1152 RTL818X_TX_CONF_NO_ICV;
1153
Andrea Merello4a67aa52014-03-26 21:02:46 +01001154 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1155 reg |= 1<<30; /* "duration procedure mode" */
Andrea Merello6caefd12014-03-08 18:36:37 +01001156
1157 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
Michael Wuf6532112007-10-14 14:43:16 -04001158 reg &= ~RTL818X_TX_CONF_PROBE_DTS;
1159 else
1160 reg &= ~RTL818X_TX_CONF_HW_SEQNUM;
1161
andrea merelloe74075a2014-02-18 02:10:40 +01001162 reg &= ~RTL818X_TX_CONF_DISCW;
1163
Michael Wuf6532112007-10-14 14:43:16 -04001164 /* different meaning, same value on both rtl8185 and rtl8180 */
1165 reg &= ~RTL818X_TX_CONF_SAT_HWPLCP;
1166
1167 rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
1168
1169 reg = rtl818x_ioread8(priv, &priv->map->CMD);
1170 reg |= RTL818X_CMD_RX_ENABLE;
1171 reg |= RTL818X_CMD_TX_ENABLE;
1172 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
1173
Michael Wuf6532112007-10-14 14:43:16 -04001174 return 0;
1175
1176 err_free_rings:
1177 rtl8180_free_rx_ring(dev);
Andrea Merellofd6564f2014-03-22 18:51:20 +01001178 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -04001179 if (priv->tx_ring[i].desc)
1180 rtl8180_free_tx_ring(dev, i);
1181
1182 return ret;
1183}
1184
1185static void rtl8180_stop(struct ieee80211_hw *dev)
1186{
1187 struct rtl8180_priv *priv = dev->priv;
1188 u8 reg;
1189 int i;
1190
Andrea Merello732c8932014-03-26 21:00:24 +01001191 rtl8180_int_disable(dev);
Michael Wuf6532112007-10-14 14:43:16 -04001192
1193 reg = rtl818x_ioread8(priv, &priv->map->CMD);
1194 reg &= ~RTL818X_CMD_TX_ENABLE;
1195 reg &= ~RTL818X_CMD_RX_ENABLE;
1196 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
1197
1198 priv->rf->stop(dev);
1199
1200 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
1201 reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
1202 rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
1203 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
1204
1205 free_irq(priv->pdev->irq, dev);
1206
1207 rtl8180_free_rx_ring(dev);
Andrea Merellofd6564f2014-03-22 18:51:20 +01001208 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -04001209 rtl8180_free_tx_ring(dev, i);
1210}
1211
Eliad Peller37a41b42011-09-21 14:06:11 +03001212static u64 rtl8180_get_tsf(struct ieee80211_hw *dev,
1213 struct ieee80211_vif *vif)
John W. Linvillec809e862010-05-06 16:49:40 -04001214{
1215 struct rtl8180_priv *priv = dev->priv;
1216
1217 return rtl818x_ioread32(priv, &priv->map->TSFT[0]) |
1218 (u64)(rtl818x_ioread32(priv, &priv->map->TSFT[1])) << 32;
1219}
1220
John W. Linvillea3275e22010-06-24 11:08:37 -04001221static void rtl8180_beacon_work(struct work_struct *work)
John W. Linvillec809e862010-05-06 16:49:40 -04001222{
1223 struct rtl8180_vif *vif_priv =
1224 container_of(work, struct rtl8180_vif, beacon_work.work);
1225 struct ieee80211_vif *vif =
1226 container_of((void *)vif_priv, struct ieee80211_vif, drv_priv);
1227 struct ieee80211_hw *dev = vif_priv->dev;
1228 struct ieee80211_mgmt *mgmt;
1229 struct sk_buff *skb;
John W. Linvillec809e862010-05-06 16:49:40 -04001230
1231 /* don't overflow the tx ring */
1232 if (ieee80211_queue_stopped(dev, 0))
1233 goto resched;
1234
1235 /* grab a fresh beacon */
1236 skb = ieee80211_beacon_get(dev, vif);
John W. Linville8f1d2d22010-08-05 13:46:27 -04001237 if (!skb)
1238 goto resched;
John W. Linvillec809e862010-05-06 16:49:40 -04001239
1240 /*
1241 * update beacon timestamp w/ TSF value
1242 * TODO: make hardware update beacon timestamp
1243 */
1244 mgmt = (struct ieee80211_mgmt *)skb->data;
Eliad Peller37a41b42011-09-21 14:06:11 +03001245 mgmt->u.beacon.timestamp = cpu_to_le64(rtl8180_get_tsf(dev, vif));
John W. Linvillec809e862010-05-06 16:49:40 -04001246
1247 /* TODO: use actual beacon queue */
1248 skb_set_queue_mapping(skb, 0);
1249
Thomas Huehn36323f82012-07-23 21:33:42 +02001250 rtl8180_tx(dev, NULL, skb);
John W. Linvillec809e862010-05-06 16:49:40 -04001251
1252resched:
1253 /*
1254 * schedule next beacon
1255 * TODO: use hardware support for beacon timing
1256 */
1257 schedule_delayed_work(&vif_priv->beacon_work,
1258 usecs_to_jiffies(1024 * vif->bss_conf.beacon_int));
1259}
1260
Michael Wuf6532112007-10-14 14:43:16 -04001261static int rtl8180_add_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001262 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -04001263{
1264 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -04001265 struct rtl8180_vif *vif_priv;
Michael Wuf6532112007-10-14 14:43:16 -04001266
John W. Linville643aab62009-12-22 18:13:04 -05001267 /*
1268 * We only support one active interface at a time.
1269 */
1270 if (priv->vif)
1271 return -EBUSY;
Michael Wuf6532112007-10-14 14:43:16 -04001272
Johannes Berg1ed32e42009-12-23 13:15:45 +01001273 switch (vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001274 case NL80211_IFTYPE_STATION:
John W. Linvillec809e862010-05-06 16:49:40 -04001275 case NL80211_IFTYPE_ADHOC:
Michael Wuf6532112007-10-14 14:43:16 -04001276 break;
1277 default:
1278 return -EOPNOTSUPP;
1279 }
1280
Johannes Berg1ed32e42009-12-23 13:15:45 +01001281 priv->vif = vif;
Johannes Berg32bfd352007-12-19 01:31:26 +01001282
John W. Linvillec809e862010-05-06 16:49:40 -04001283 /* Initialize driver private area */
1284 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
1285 vif_priv->dev = dev;
1286 INIT_DELAYED_WORK(&vif_priv->beacon_work, rtl8180_beacon_work);
1287 vif_priv->enable_beacon = false;
1288
Michael Wuf6532112007-10-14 14:43:16 -04001289 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
1290 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->MAC[0],
Johannes Berg1ed32e42009-12-23 13:15:45 +01001291 le32_to_cpu(*(__le32 *)vif->addr));
Michael Wuf6532112007-10-14 14:43:16 -04001292 rtl818x_iowrite16(priv, (__le16 __iomem *)&priv->map->MAC[4],
Johannes Berg1ed32e42009-12-23 13:15:45 +01001293 le16_to_cpu(*(__le16 *)(vif->addr + 4)));
Michael Wuf6532112007-10-14 14:43:16 -04001294 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
1295
1296 return 0;
1297}
1298
1299static void rtl8180_remove_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001300 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -04001301{
1302 struct rtl8180_priv *priv = dev->priv;
Johannes Berg32bfd352007-12-19 01:31:26 +01001303 priv->vif = NULL;
Michael Wuf6532112007-10-14 14:43:16 -04001304}
1305
Johannes Berge8975582008-10-09 12:18:51 +02001306static int rtl8180_config(struct ieee80211_hw *dev, u32 changed)
Michael Wuf6532112007-10-14 14:43:16 -04001307{
1308 struct rtl8180_priv *priv = dev->priv;
Johannes Berge8975582008-10-09 12:18:51 +02001309 struct ieee80211_conf *conf = &dev->conf;
Michael Wuf6532112007-10-14 14:43:16 -04001310
1311 priv->rf->set_chan(dev, conf);
1312
1313 return 0;
1314}
1315
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001316static void rtl8187se_conf_ac_parm(struct ieee80211_hw *dev, u8 queue)
1317{
1318 const struct ieee80211_tx_queue_params *params;
1319 struct rtl8180_priv *priv = dev->priv;
1320
1321 /* hw value */
1322 u32 ac_param;
1323
1324 u8 aifs;
1325 u8 txop;
1326 u8 cw_min, cw_max;
1327
1328 params = &priv->queue_param[queue];
1329
1330 cw_min = fls(params->cw_min);
1331 cw_max = fls(params->cw_max);
1332
1333 aifs = 10 + params->aifs * priv->slot_time;
1334
1335 /* TODO: check if txop HW is in us (mult by 32) */
1336 txop = params->txop;
1337
1338 ac_param = txop << AC_PARAM_TXOP_LIMIT_SHIFT |
1339 cw_max << AC_PARAM_ECW_MAX_SHIFT |
1340 cw_min << AC_PARAM_ECW_MIN_SHIFT |
1341 aifs << AC_PARAM_AIFS_SHIFT;
1342
1343 switch (queue) {
1344 case IEEE80211_AC_BK:
1345 rtl818x_iowrite32(priv, &priv->map->AC_BK_PARAM, ac_param);
1346 break;
1347 case IEEE80211_AC_BE:
1348 rtl818x_iowrite32(priv, &priv->map->AC_BE_PARAM, ac_param);
1349 break;
1350 case IEEE80211_AC_VI:
1351 rtl818x_iowrite32(priv, &priv->map->AC_VI_PARAM, ac_param);
1352 break;
1353 case IEEE80211_AC_VO:
1354 rtl818x_iowrite32(priv, &priv->map->AC_VO_PARAM, ac_param);
1355 break;
1356 }
1357}
1358
Andrea Merello9069af72014-03-15 18:29:37 +01001359static int rtl8180_conf_tx(struct ieee80211_hw *dev,
1360 struct ieee80211_vif *vif, u16 queue,
1361 const struct ieee80211_tx_queue_params *params)
1362{
1363 struct rtl8180_priv *priv = dev->priv;
1364 u8 cw_min, cw_max;
1365
1366 /* nothing to do ? */
1367 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
1368 return 0;
1369
1370 cw_min = fls(params->cw_min);
1371 cw_max = fls(params->cw_max);
1372
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001373 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1374 priv->queue_param[queue] = *params;
1375 rtl8187se_conf_ac_parm(dev, queue);
1376 } else
1377 rtl818x_iowrite8(priv, &priv->map->CW_VAL,
1378 (cw_max << 4) | cw_min);
Andrea Merello9069af72014-03-15 18:29:37 +01001379 return 0;
1380}
1381
1382static void rtl8180_conf_erp(struct ieee80211_hw *dev,
1383 struct ieee80211_bss_conf *info)
1384{
1385 struct rtl8180_priv *priv = dev->priv;
1386 u8 sifs, difs;
1387 int eifs;
1388 u8 hw_eifs;
1389
1390 /* TODO: should we do something ? */
1391 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
1392 return;
1393
1394 /* I _hope_ this means 10uS for the HW.
1395 * In reference code it is 0x22 for
1396 * both rtl8187L and rtl8187SE
1397 */
1398 sifs = 0x22;
1399
1400 if (info->use_short_slot)
1401 priv->slot_time = 9;
1402 else
1403 priv->slot_time = 20;
1404
1405 /* 10 is SIFS time in uS */
1406 difs = 10 + 2 * priv->slot_time;
1407 eifs = 10 + difs + priv->ack_time;
1408
1409 /* HW should use 4uS units for EIFS (I'm sure for rtl8185)*/
1410 hw_eifs = DIV_ROUND_UP(eifs, 4);
1411
1412
1413 rtl818x_iowrite8(priv, &priv->map->SLOT, priv->slot_time);
1414 rtl818x_iowrite8(priv, &priv->map->SIFS, sifs);
1415 rtl818x_iowrite8(priv, &priv->map->DIFS, difs);
1416
1417 /* from reference code. set ack timeout reg = eifs reg */
1418 rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, hw_eifs);
1419
Andrea Merello355668d2014-03-26 21:03:03 +01001420 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1421 rtl818x_iowrite8(priv, &priv->map->EIFS_8187SE, hw_eifs);
1422 else if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
1423 /* rtl8187/rtl8185 HW bug. After EIFS is elapsed,
1424 * the HW still wait for DIFS.
1425 * HW uses 4uS units for EIFS.
1426 */
1427 hw_eifs = DIV_ROUND_UP(eifs - difs, 4);
Andrea Merello9069af72014-03-15 18:29:37 +01001428
Andrea Merello355668d2014-03-26 21:03:03 +01001429 rtl818x_iowrite8(priv, &priv->map->EIFS, hw_eifs);
1430 }
Andrea Merello9069af72014-03-15 18:29:37 +01001431}
1432
John W. Linvilleda81ded2008-11-12 14:37:11 -05001433static void rtl8180_bss_info_changed(struct ieee80211_hw *dev,
1434 struct ieee80211_vif *vif,
1435 struct ieee80211_bss_conf *info,
1436 u32 changed)
1437{
1438 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -04001439 struct rtl8180_vif *vif_priv;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001440 int i;
John W. Linville0f956e72010-07-29 21:50:29 -04001441 u8 reg;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001442
John W. Linvillec809e862010-05-06 16:49:40 -04001443 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
1444
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001445 if (changed & BSS_CHANGED_BSSID) {
1446 for (i = 0; i < ETH_ALEN; i++)
1447 rtl818x_iowrite8(priv, &priv->map->BSSID[i],
1448 info->bssid[i]);
1449
John W. Linville0f956e72010-07-29 21:50:29 -04001450 if (is_valid_ether_addr(info->bssid)) {
1451 if (vif->type == NL80211_IFTYPE_ADHOC)
1452 reg = RTL818X_MSR_ADHOC;
1453 else
1454 reg = RTL818X_MSR_INFRA;
1455 } else
1456 reg = RTL818X_MSR_NO_LINK;
Andrea Merello833d15a2014-03-26 21:03:17 +01001457
1458 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1459 reg |= RTL818X_MSR_ENEDCA;
1460
John W. Linville0f956e72010-07-29 21:50:29 -04001461 rtl818x_iowrite8(priv, &priv->map->MSR, reg);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001462 }
John W. Linvilleda81ded2008-11-12 14:37:11 -05001463
Andrea Merello516a0932014-03-15 18:29:36 +01001464 if (changed & BSS_CHANGED_BASIC_RATES)
1465 rtl8180_conf_basic_rates(dev, info->basic_rates);
1466
Andrea Merello9069af72014-03-15 18:29:37 +01001467 if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_ERP_PREAMBLE)) {
1468
1469 /* when preamble changes, acktime duration changes, and erp must
1470 * be recalculated. ACK time is calculated at lowest rate.
1471 * Since mac80211 include SIFS time we remove it (-10)
1472 */
1473 priv->ack_time =
1474 le16_to_cpu(ieee80211_generic_frame_duration(dev,
1475 priv->vif,
1476 IEEE80211_BAND_2GHZ, 10,
1477 &priv->rates[0])) - 10;
1478
1479 rtl8180_conf_erp(dev, info);
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001480
1481 /* mac80211 supplies aifs_n to driver and calls
1482 * conf_tx callback whether aifs_n changes, NOT
1483 * when aifs changes.
1484 * Aifs should be recalculated if slot changes.
1485 */
1486 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1487 for (i = 0; i < 4; i++)
1488 rtl8187se_conf_ac_parm(dev, i);
1489 }
Andrea Merello9069af72014-03-15 18:29:37 +01001490 }
John W. Linvillec809e862010-05-06 16:49:40 -04001491
1492 if (changed & BSS_CHANGED_BEACON_ENABLED)
1493 vif_priv->enable_beacon = info->enable_beacon;
1494
1495 if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON)) {
1496 cancel_delayed_work_sync(&vif_priv->beacon_work);
1497 if (vif_priv->enable_beacon)
1498 schedule_work(&vif_priv->beacon_work.work);
1499 }
John W. Linvilleda81ded2008-11-12 14:37:11 -05001500}
1501
Jiri Pirko22bedad32010-04-01 21:22:57 +00001502static u64 rtl8180_prepare_multicast(struct ieee80211_hw *dev,
1503 struct netdev_hw_addr_list *mc_list)
Johannes Berg3ac64be2009-08-17 16:16:53 +02001504{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001505 return netdev_hw_addr_list_count(mc_list);
Johannes Berg3ac64be2009-08-17 16:16:53 +02001506}
1507
Michael Wuf6532112007-10-14 14:43:16 -04001508static void rtl8180_configure_filter(struct ieee80211_hw *dev,
1509 unsigned int changed_flags,
1510 unsigned int *total_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001511 u64 multicast)
Michael Wuf6532112007-10-14 14:43:16 -04001512{
1513 struct rtl8180_priv *priv = dev->priv;
1514
1515 if (changed_flags & FIF_FCSFAIL)
1516 priv->rx_conf ^= RTL818X_RX_CONF_FCS;
1517 if (changed_flags & FIF_CONTROL)
1518 priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
1519 if (changed_flags & FIF_OTHER_BSS)
1520 priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
Johannes Berg3ac64be2009-08-17 16:16:53 +02001521 if (*total_flags & FIF_ALLMULTI || multicast > 0)
Michael Wuf6532112007-10-14 14:43:16 -04001522 priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
1523 else
1524 priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
1525
1526 *total_flags = 0;
1527
1528 if (priv->rx_conf & RTL818X_RX_CONF_FCS)
1529 *total_flags |= FIF_FCSFAIL;
1530 if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
1531 *total_flags |= FIF_CONTROL;
1532 if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
1533 *total_flags |= FIF_OTHER_BSS;
1534 if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
1535 *total_flags |= FIF_ALLMULTI;
1536
1537 rtl818x_iowrite32(priv, &priv->map->RX_CONF, priv->rx_conf);
1538}
1539
1540static const struct ieee80211_ops rtl8180_ops = {
1541 .tx = rtl8180_tx,
1542 .start = rtl8180_start,
1543 .stop = rtl8180_stop,
1544 .add_interface = rtl8180_add_interface,
1545 .remove_interface = rtl8180_remove_interface,
1546 .config = rtl8180_config,
John W. Linvilleda81ded2008-11-12 14:37:11 -05001547 .bss_info_changed = rtl8180_bss_info_changed,
Andrea Merello9069af72014-03-15 18:29:37 +01001548 .conf_tx = rtl8180_conf_tx,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001549 .prepare_multicast = rtl8180_prepare_multicast,
Michael Wuf6532112007-10-14 14:43:16 -04001550 .configure_filter = rtl8180_configure_filter,
John W. Linvilled2bb8e02010-01-26 16:22:20 -05001551 .get_tsf = rtl8180_get_tsf,
Michael Wuf6532112007-10-14 14:43:16 -04001552};
1553
1554static void rtl8180_eeprom_register_read(struct eeprom_93cx6 *eeprom)
1555{
Andrea Merello7d4b8292014-03-15 18:29:38 +01001556 struct rtl8180_priv *priv = eeprom->data;
Michael Wuf6532112007-10-14 14:43:16 -04001557 u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1558
1559 eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
1560 eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
1561 eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
1562 eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
1563}
1564
1565static void rtl8180_eeprom_register_write(struct eeprom_93cx6 *eeprom)
1566{
Andrea Merello7d4b8292014-03-15 18:29:38 +01001567 struct rtl8180_priv *priv = eeprom->data;
Michael Wuf6532112007-10-14 14:43:16 -04001568 u8 reg = 2 << 6;
1569
1570 if (eeprom->reg_data_in)
1571 reg |= RTL818X_EEPROM_CMD_WRITE;
1572 if (eeprom->reg_data_out)
1573 reg |= RTL818X_EEPROM_CMD_READ;
1574 if (eeprom->reg_data_clock)
1575 reg |= RTL818X_EEPROM_CMD_CK;
1576 if (eeprom->reg_chip_select)
1577 reg |= RTL818X_EEPROM_CMD_CS;
1578
1579 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
1580 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1581 udelay(10);
1582}
1583
Andrea Merello7d4b8292014-03-15 18:29:38 +01001584static void rtl8180_eeprom_read(struct rtl8180_priv *priv)
1585{
1586 struct eeprom_93cx6 eeprom;
1587 int eeprom_cck_table_adr;
1588 u16 eeprom_val;
1589 int i;
1590
1591 eeprom.data = priv;
1592 eeprom.register_read = rtl8180_eeprom_register_read;
1593 eeprom.register_write = rtl8180_eeprom_register_write;
1594 if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
1595 eeprom.width = PCI_EEPROM_WIDTH_93C66;
1596 else
1597 eeprom.width = PCI_EEPROM_WIDTH_93C46;
1598
1599 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
1600 RTL818X_EEPROM_CMD_PROGRAM);
1601 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1602 udelay(10);
1603
1604 eeprom_93cx6_read(&eeprom, 0x06, &eeprom_val);
1605 eeprom_val &= 0xFF;
1606 priv->rf_type = eeprom_val;
1607
1608 eeprom_93cx6_read(&eeprom, 0x17, &eeprom_val);
1609 priv->csthreshold = eeprom_val >> 8;
1610
1611 eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)priv->mac_addr, 3);
1612
Andrea Merellofc32ac92014-03-26 21:01:47 +01001613 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1614 eeprom_cck_table_adr = 0x30;
1615 else
1616 eeprom_cck_table_adr = 0x10;
Andrea Merello7d4b8292014-03-15 18:29:38 +01001617
1618 /* CCK TX power */
1619 for (i = 0; i < 14; i += 2) {
1620 u16 txpwr;
1621 eeprom_93cx6_read(&eeprom, eeprom_cck_table_adr + (i >> 1),
1622 &txpwr);
1623 priv->channels[i].hw_value = txpwr & 0xFF;
1624 priv->channels[i + 1].hw_value = txpwr >> 8;
1625 }
1626
1627 /* OFDM TX power */
1628 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
1629 for (i = 0; i < 14; i += 2) {
1630 u16 txpwr;
1631 eeprom_93cx6_read(&eeprom, 0x20 + (i >> 1), &txpwr);
1632 priv->channels[i].hw_value |= (txpwr & 0xFF) << 8;
1633 priv->channels[i + 1].hw_value |= txpwr & 0xFF00;
1634 }
1635 }
1636
1637 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
1638 __le32 anaparam;
1639 eeprom_93cx6_multiread(&eeprom, 0xD, (__le16 *)&anaparam, 2);
1640 priv->anaparam = le32_to_cpu(anaparam);
1641 eeprom_93cx6_read(&eeprom, 0x19, &priv->rfparam);
1642 }
1643
Andrea Merellofc32ac92014-03-26 21:01:47 +01001644 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1645 eeprom_93cx6_read(&eeprom, 0x3F, &eeprom_val);
1646 priv->antenna_diversity_en = !!(eeprom_val & 0x100);
1647 priv->antenna_diversity_default = (eeprom_val & 0xC00) == 0x400;
1648
1649 eeprom_93cx6_read(&eeprom, 0x7C, &eeprom_val);
1650 priv->xtal_out = eeprom_val & 0xF;
1651 priv->xtal_in = (eeprom_val & 0xF0) >> 4;
1652 priv->xtal_cal = !!(eeprom_val & 0x1000);
1653 priv->thermal_meter_val = (eeprom_val & 0xF00) >> 8;
1654 priv->thermal_meter_en = !!(eeprom_val & 0x2000);
1655 }
1656
Andrea Merello7d4b8292014-03-15 18:29:38 +01001657 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
1658 RTL818X_EEPROM_CMD_NORMAL);
1659}
1660
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001661static int rtl8180_probe(struct pci_dev *pdev,
Michael Wuf6532112007-10-14 14:43:16 -04001662 const struct pci_device_id *id)
1663{
1664 struct ieee80211_hw *dev;
1665 struct rtl8180_priv *priv;
1666 unsigned long mem_addr, mem_len;
1667 unsigned int io_addr, io_len;
Andrea Merello7d4b8292014-03-15 18:29:38 +01001668 int err;
Michael Wuf6532112007-10-14 14:43:16 -04001669 const char *chip_name, *rf_name = NULL;
1670 u32 reg;
Michael Wuf6532112007-10-14 14:43:16 -04001671
1672 err = pci_enable_device(pdev);
1673 if (err) {
1674 printk(KERN_ERR "%s (rtl8180): Cannot enable new PCI device\n",
1675 pci_name(pdev));
1676 return err;
1677 }
1678
1679 err = pci_request_regions(pdev, KBUILD_MODNAME);
1680 if (err) {
1681 printk(KERN_ERR "%s (rtl8180): Cannot obtain PCI resources\n",
1682 pci_name(pdev));
1683 return err;
1684 }
1685
1686 io_addr = pci_resource_start(pdev, 0);
1687 io_len = pci_resource_len(pdev, 0);
1688 mem_addr = pci_resource_start(pdev, 1);
1689 mem_len = pci_resource_len(pdev, 1);
1690
1691 if (mem_len < sizeof(struct rtl818x_csr) ||
1692 io_len < sizeof(struct rtl818x_csr)) {
1693 printk(KERN_ERR "%s (rtl8180): Too short PCI resources\n",
1694 pci_name(pdev));
1695 err = -ENOMEM;
1696 goto err_free_reg;
1697 }
1698
John W. Linville9e385c52010-05-10 14:24:34 -04001699 if ((err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) ||
1700 (err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))) {
Michael Wuf6532112007-10-14 14:43:16 -04001701 printk(KERN_ERR "%s (rtl8180): No suitable DMA available\n",
1702 pci_name(pdev));
1703 goto err_free_reg;
1704 }
1705
1706 pci_set_master(pdev);
1707
1708 dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8180_ops);
1709 if (!dev) {
1710 printk(KERN_ERR "%s (rtl8180): ieee80211 alloc failed\n",
1711 pci_name(pdev));
1712 err = -ENOMEM;
1713 goto err_free_reg;
1714 }
1715
1716 priv = dev->priv;
1717 priv->pdev = pdev;
1718
Johannes Berge6a98542008-10-21 12:40:02 +02001719 dev->max_rates = 2;
Michael Wuf6532112007-10-14 14:43:16 -04001720 SET_IEEE80211_DEV(dev, &pdev->dev);
1721 pci_set_drvdata(pdev, dev);
1722
1723 priv->map = pci_iomap(pdev, 1, mem_len);
1724 if (!priv->map)
1725 priv->map = pci_iomap(pdev, 0, io_len);
1726
1727 if (!priv->map) {
1728 printk(KERN_ERR "%s (rtl8180): Cannot map device memory\n",
1729 pci_name(pdev));
1730 goto err_free_dev;
1731 }
1732
Johannes Berg8318d782008-01-24 19:38:38 +01001733 BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
1734 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
1735
Michael Wuf6532112007-10-14 14:43:16 -04001736 memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
1737 memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
Johannes Berg8318d782008-01-24 19:38:38 +01001738
1739 priv->band.band = IEEE80211_BAND_2GHZ;
1740 priv->band.channels = priv->channels;
1741 priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
1742 priv->band.bitrates = priv->rates;
1743 priv->band.n_bitrates = 4;
1744 dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
1745
Michael Wuf6532112007-10-14 14:43:16 -04001746 dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
Bruno Randolf566bfe52008-05-08 19:15:40 +02001747 IEEE80211_HW_RX_INCLUDES_FCS |
1748 IEEE80211_HW_SIGNAL_UNSPEC;
John W. Linvillec809e862010-05-06 16:49:40 -04001749 dev->vif_data_size = sizeof(struct rtl8180_vif);
1750 dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1751 BIT(NL80211_IFTYPE_ADHOC);
Bruno Randolf566bfe52008-05-08 19:15:40 +02001752 dev->max_signal = 65;
Michael Wuf6532112007-10-14 14:43:16 -04001753
1754 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
1755 reg &= RTL818X_TX_CONF_HWVER_MASK;
1756 switch (reg) {
1757 case RTL818X_TX_CONF_R8180_ABCD:
1758 chip_name = "RTL8180";
Andrea Merello6caefd12014-03-08 18:36:37 +01001759 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
Michael Wuf6532112007-10-14 14:43:16 -04001760 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001761
Michael Wuf6532112007-10-14 14:43:16 -04001762 case RTL818X_TX_CONF_R8180_F:
1763 chip_name = "RTL8180vF";
Andrea Merello6caefd12014-03-08 18:36:37 +01001764 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
Michael Wuf6532112007-10-14 14:43:16 -04001765 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001766
Michael Wuf6532112007-10-14 14:43:16 -04001767 case RTL818X_TX_CONF_R8185_ABC:
1768 chip_name = "RTL8185";
Andrea Merello6caefd12014-03-08 18:36:37 +01001769 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
Michael Wuf6532112007-10-14 14:43:16 -04001770 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001771
Michael Wuf6532112007-10-14 14:43:16 -04001772 case RTL818X_TX_CONF_R8185_D:
1773 chip_name = "RTL8185vD";
Andrea Merello6caefd12014-03-08 18:36:37 +01001774 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
Michael Wuf6532112007-10-14 14:43:16 -04001775 break;
Andrea Merello24b5fbf2014-03-26 21:04:05 +01001776
1777 case RTL818X_TX_CONF_RTL8187SE:
1778 chip_name = "RTL8187SE";
1779 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8187SE;
1780 break;
1781
Michael Wuf6532112007-10-14 14:43:16 -04001782 default:
1783 printk(KERN_ERR "%s (rtl8180): Unknown chip! (0x%x)\n",
1784 pci_name(pdev), reg >> 25);
1785 goto err_iounmap;
1786 }
1787
Andrea Merellofd6564f2014-03-22 18:51:20 +01001788 /* we declare to MAC80211 all the queues except for beacon queue
1789 * that will be eventually handled by DRV.
1790 * TX rings are arranged in such a way that lower is the IDX,
1791 * higher is the priority, in order to achieve direct mapping
1792 * with mac80211, however the beacon queue is an exception and it
1793 * is mapped on the highst tx ring IDX.
1794 */
Andrea Merello24b5fbf2014-03-26 21:04:05 +01001795 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1796 dev->queues = RTL8187SE_NR_TX_QUEUES - 1;
1797 else
1798 dev->queues = RTL8180_NR_TX_QUEUES - 1;
Andrea Merellofd6564f2014-03-22 18:51:20 +01001799
Andrea Merello6caefd12014-03-08 18:36:37 +01001800 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Johannes Berg8318d782008-01-24 19:38:38 +01001801 priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
Michael Wuf6532112007-10-14 14:43:16 -04001802 pci_try_set_mwi(pdev);
1803 }
1804
Andrea Merello7d4b8292014-03-15 18:29:38 +01001805 rtl8180_eeprom_read(priv);
Michael Wuf6532112007-10-14 14:43:16 -04001806
Andrea Merello7d4b8292014-03-15 18:29:38 +01001807 switch (priv->rf_type) {
Michael Wuf6532112007-10-14 14:43:16 -04001808 case 1: rf_name = "Intersil";
1809 break;
1810 case 2: rf_name = "RFMD";
1811 break;
1812 case 3: priv->rf = &sa2400_rf_ops;
1813 break;
1814 case 4: priv->rf = &max2820_rf_ops;
1815 break;
1816 case 5: priv->rf = &grf5101_rf_ops;
1817 break;
Andrea Merello24b5fbf2014-03-26 21:04:05 +01001818 case 9:
1819 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1820 priv->rf = rtl8187se_detect_rf(dev);
1821 else
1822 priv->rf = rtl8180_detect_rf(dev);
Michael Wuf6532112007-10-14 14:43:16 -04001823 break;
1824 case 10:
1825 rf_name = "RTL8255";
1826 break;
1827 default:
1828 printk(KERN_ERR "%s (rtl8180): Unknown RF! (0x%x)\n",
Andrea Merello7d4b8292014-03-15 18:29:38 +01001829 pci_name(pdev), priv->rf_type);
Michael Wuf6532112007-10-14 14:43:16 -04001830 goto err_iounmap;
1831 }
1832
1833 if (!priv->rf) {
1834 printk(KERN_ERR "%s (rtl8180): %s RF frontend not supported!\n",
1835 pci_name(pdev), rf_name);
1836 goto err_iounmap;
1837 }
1838
Andrea Merello7d4b8292014-03-15 18:29:38 +01001839 if (!is_valid_ether_addr(priv->mac_addr)) {
Michael Wuf6532112007-10-14 14:43:16 -04001840 printk(KERN_WARNING "%s (rtl8180): Invalid hwaddr! Using"
1841 " randomly generated MAC addr\n", pci_name(pdev));
Andrea Merello7d4b8292014-03-15 18:29:38 +01001842 eth_random_addr(priv->mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001843 }
Andrea Merello7d4b8292014-03-15 18:29:38 +01001844 SET_IEEE80211_PERM_ADDR(dev, priv->mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001845
1846 spin_lock_init(&priv->lock);
1847
1848 err = ieee80211_register_hw(dev);
1849 if (err) {
1850 printk(KERN_ERR "%s (rtl8180): Cannot register device\n",
1851 pci_name(pdev));
1852 goto err_iounmap;
1853 }
1854
Joe Perchesc96c31e2010-07-26 14:39:58 -07001855 wiphy_info(dev->wiphy, "hwaddr %pm, %s + %s\n",
Andrea Merello7d4b8292014-03-15 18:29:38 +01001856 priv->mac_addr, chip_name, priv->rf->name);
Michael Wuf6532112007-10-14 14:43:16 -04001857
1858 return 0;
1859
1860 err_iounmap:
andrea merello0269da22014-02-18 02:10:41 +01001861 pci_iounmap(pdev, priv->map);
Michael Wuf6532112007-10-14 14:43:16 -04001862
1863 err_free_dev:
Michael Wuf6532112007-10-14 14:43:16 -04001864 ieee80211_free_hw(dev);
1865
1866 err_free_reg:
1867 pci_release_regions(pdev);
1868 pci_disable_device(pdev);
1869 return err;
1870}
1871
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001872static void rtl8180_remove(struct pci_dev *pdev)
Michael Wuf6532112007-10-14 14:43:16 -04001873{
1874 struct ieee80211_hw *dev = pci_get_drvdata(pdev);
1875 struct rtl8180_priv *priv;
1876
1877 if (!dev)
1878 return;
1879
1880 ieee80211_unregister_hw(dev);
1881
1882 priv = dev->priv;
1883
1884 pci_iounmap(pdev, priv->map);
1885 pci_release_regions(pdev);
1886 pci_disable_device(pdev);
1887 ieee80211_free_hw(dev);
1888}
1889
1890#ifdef CONFIG_PM
1891static int rtl8180_suspend(struct pci_dev *pdev, pm_message_t state)
1892{
1893 pci_save_state(pdev);
1894 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1895 return 0;
1896}
1897
1898static int rtl8180_resume(struct pci_dev *pdev)
1899{
1900 pci_set_power_state(pdev, PCI_D0);
1901 pci_restore_state(pdev);
1902 return 0;
1903}
1904
1905#endif /* CONFIG_PM */
1906
1907static struct pci_driver rtl8180_driver = {
1908 .name = KBUILD_MODNAME,
1909 .id_table = rtl8180_table,
1910 .probe = rtl8180_probe,
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001911 .remove = rtl8180_remove,
Michael Wuf6532112007-10-14 14:43:16 -04001912#ifdef CONFIG_PM
1913 .suspend = rtl8180_suspend,
1914 .resume = rtl8180_resume,
1915#endif /* CONFIG_PM */
1916};
1917
Axel Lin5b0a3b72012-04-14 10:38:36 +08001918module_pci_driver(rtl8180_driver);