blob: 2c1c02bafa10bbfe1f198279400a84ad08256bb4 [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;
Andrea Merelloe58342d2014-05-07 17:52:16 +0200287 if (flags & RTL818X_RX_DESC_FLAG_SPLCP)
288 rx_status.flag |= RX_FLAG_SHORTPRE;
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300289 if (flags & RTL818X_RX_DESC_FLAG_CRC32_ERR)
Michael Wuf6532112007-10-14 14:43:16 -0400290 rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
291
Johannes Bergf1d58c22009-06-17 13:13:00 +0200292 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400293 ieee80211_rx_irqsafe(dev, skb);
Michael Wuf6532112007-10-14 14:43:16 -0400294
295 skb = new_skb;
296 priv->rx_buf[priv->rx_idx] = skb;
andrea.merello2b4db052014-02-05 22:38:05 +0100297 *((dma_addr_t *) skb->cb) = mapping;
Michael Wuf6532112007-10-14 14:43:16 -0400298 }
299
300 done:
Andrea Merello21025922014-03-26 20:59:52 +0100301 cmd_desc = entry;
302 cmd_desc->rx_buf = cpu_to_le32(*((dma_addr_t *)skb->cb));
303 cmd_desc->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
Michael Wuf6532112007-10-14 14:43:16 -0400304 MAX_RX_SIZE);
305 if (priv->rx_idx == 31)
Andrea Merello21025922014-03-26 20:59:52 +0100306 cmd_desc->flags |=
307 cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
Michael Wuf6532112007-10-14 14:43:16 -0400308 priv->rx_idx = (priv->rx_idx + 1) % 32;
309 }
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400310}
Michael Wuf6532112007-10-14 14:43:16 -0400311
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400312static void rtl8180_handle_tx(struct ieee80211_hw *dev, unsigned int prio)
313{
314 struct rtl8180_priv *priv = dev->priv;
315 struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
Michael Wuf6532112007-10-14 14:43:16 -0400316
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400317 while (skb_queue_len(&ring->queue)) {
318 struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
319 struct sk_buff *skb;
320 struct ieee80211_tx_info *info;
321 u32 flags = le32_to_cpu(entry->flags);
322
323 if (flags & RTL818X_TX_DESC_FLAG_OWN)
324 return;
325
326 ring->idx = (ring->idx + 1) % ring->entries;
327 skb = __skb_dequeue(&ring->queue);
328 pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
329 skb->len, PCI_DMA_TODEVICE);
330
331 info = IEEE80211_SKB_CB(skb);
332 ieee80211_tx_info_clear_status(info);
333
334 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
335 (flags & RTL818X_TX_DESC_FLAG_TX_OK))
336 info->flags |= IEEE80211_TX_STAT_ACK;
337
338 info->status.rates[0].count = (flags & 0xFF) + 1;
339 info->status.rates[1].idx = -1;
340
341 ieee80211_tx_status_irqsafe(dev, skb);
342 if (ring->entries - skb_queue_len(&ring->queue) == 2)
343 ieee80211_wake_queue(dev, prio);
Michael Wuf6532112007-10-14 14:43:16 -0400344 }
345}
346
Andrea Merelloa373ebc2014-03-26 21:00:06 +0100347static irqreturn_t rtl8187se_interrupt(int irq, void *dev_id)
348{
349 struct ieee80211_hw *dev = dev_id;
350 struct rtl8180_priv *priv = dev->priv;
351 u32 reg;
352 unsigned long flags;
353 static int desc_err;
354
355 spin_lock_irqsave(&priv->lock, flags);
356 /* Note: 32-bit interrupt status */
357 reg = rtl818x_ioread32(priv, &priv->map->INT_STATUS_SE);
358 if (unlikely(reg == 0xFFFFFFFF)) {
359 spin_unlock_irqrestore(&priv->lock, flags);
360 return IRQ_HANDLED;
361 }
362
363 rtl818x_iowrite32(priv, &priv->map->INT_STATUS_SE, reg);
364
365 if (reg & IMR_TIMEOUT1)
366 rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
367
368 if (reg & (IMR_TBDOK | IMR_TBDER))
369 rtl8180_handle_tx(dev, 4);
370
371 if (reg & (IMR_TVODOK | IMR_TVODER))
372 rtl8180_handle_tx(dev, 0);
373
374 if (reg & (IMR_TVIDOK | IMR_TVIDER))
375 rtl8180_handle_tx(dev, 1);
376
377 if (reg & (IMR_TBEDOK | IMR_TBEDER))
378 rtl8180_handle_tx(dev, 2);
379
380 if (reg & (IMR_TBKDOK | IMR_TBKDER))
381 rtl8180_handle_tx(dev, 3);
382
383 if (reg & (IMR_ROK | IMR_RER | RTL818X_INT_SE_RX_DU | IMR_RQOSOK))
384 rtl8180_handle_rx(dev);
385 /* The interface sometimes generates several RX DMA descriptor errors
386 * at startup. Do not report these.
387 */
388 if ((reg & RTL818X_INT_SE_RX_DU) && desc_err++ > 2)
389 if (net_ratelimit())
390 wiphy_err(dev->wiphy, "No RX DMA Descriptor avail\n");
391
392 spin_unlock_irqrestore(&priv->lock, flags);
393 return IRQ_HANDLED;
394}
395
Michael Wuf6532112007-10-14 14:43:16 -0400396static irqreturn_t rtl8180_interrupt(int irq, void *dev_id)
397{
398 struct ieee80211_hw *dev = dev_id;
399 struct rtl8180_priv *priv = dev->priv;
400 u16 reg;
401
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400402 spin_lock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400403 reg = rtl818x_ioread16(priv, &priv->map->INT_STATUS);
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400404 if (unlikely(reg == 0xFFFF)) {
405 spin_unlock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400406 return IRQ_HANDLED;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400407 }
Michael Wuf6532112007-10-14 14:43:16 -0400408
409 rtl818x_iowrite16(priv, &priv->map->INT_STATUS, reg);
410
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400411 if (reg & (RTL818X_INT_TXB_OK | RTL818X_INT_TXB_ERR))
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400412 rtl8180_handle_tx(dev, 1);
413
414 if (reg & (RTL818X_INT_TXL_OK | RTL818X_INT_TXL_ERR))
415 rtl8180_handle_tx(dev, 0);
416
417 if (reg & (RTL818X_INT_RX_OK | RTL818X_INT_RX_ERR))
418 rtl8180_handle_rx(dev);
419
420 spin_unlock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400421
422 return IRQ_HANDLED;
423}
424
Thomas Huehn36323f82012-07-23 21:33:42 +0200425static void rtl8180_tx(struct ieee80211_hw *dev,
426 struct ieee80211_tx_control *control,
427 struct sk_buff *skb)
Michael Wuf6532112007-10-14 14:43:16 -0400428{
Johannes Berge039fa42008-05-15 12:55:29 +0200429 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
John W. Linville51e080d2010-05-06 16:26:23 -0400430 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Michael Wuf6532112007-10-14 14:43:16 -0400431 struct rtl8180_priv *priv = dev->priv;
432 struct rtl8180_tx_ring *ring;
433 struct rtl8180_tx_desc *entry;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400434 unsigned long flags;
Andrea Merellofd6564f2014-03-22 18:51:20 +0100435 unsigned int idx, prio, hw_prio;
Michael Wuf6532112007-10-14 14:43:16 -0400436 dma_addr_t mapping;
437 u32 tx_flags;
Johannes Berge6a98542008-10-21 12:40:02 +0200438 u8 rc_flags;
Michael Wuf6532112007-10-14 14:43:16 -0400439 u16 plcp_len = 0;
440 __le16 rts_duration = 0;
Andrea Merello3ee44d62014-03-26 21:00:57 +0100441 /* do arithmetic and then convert to le16 */
442 u16 frame_duration = 0;
Michael Wuf6532112007-10-14 14:43:16 -0400443
Johannes Berge2530082008-05-17 00:57:14 +0200444 prio = skb_get_queue_mapping(skb);
Michael Wuf6532112007-10-14 14:43:16 -0400445 ring = &priv->tx_ring[prio];
446
447 mapping = pci_map_single(priv->pdev, skb->data,
448 skb->len, PCI_DMA_TODEVICE);
449
andrea.merello348f7d42014-02-05 22:38:06 +0100450 if (pci_dma_mapping_error(priv->pdev, mapping)) {
451 kfree_skb(skb);
452 dev_err(&priv->pdev->dev, "TX DMA mapping error\n");
453 return;
andrea.merello348f7d42014-02-05 22:38:06 +0100454 }
455
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300456 tx_flags = RTL818X_TX_DESC_FLAG_OWN | RTL818X_TX_DESC_FLAG_FS |
457 RTL818X_TX_DESC_FLAG_LS |
Johannes Berge039fa42008-05-15 12:55:29 +0200458 (ieee80211_get_tx_rate(dev, info)->hw_value << 24) |
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200459 skb->len;
Michael Wuf6532112007-10-14 14:43:16 -0400460
Andrea Merello6caefd12014-03-08 18:36:37 +0100461 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300462 tx_flags |= RTL818X_TX_DESC_FLAG_DMA |
463 RTL818X_TX_DESC_FLAG_NO_ENC;
Michael Wuf6532112007-10-14 14:43:16 -0400464
Johannes Berge6a98542008-10-21 12:40:02 +0200465 rc_flags = info->control.rates[0].flags;
Andrea Merello5d0d1e92014-05-07 17:53:17 +0200466
467 /* HW will perform RTS-CTS when only RTS flags is set.
468 * HW will perform CTS-to-self when both RTS and CTS flags are set.
469 * RTS rate and RTS duration will be used also for CTS-to-self.
470 */
Johannes Berge6a98542008-10-21 12:40:02 +0200471 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300472 tx_flags |= RTL818X_TX_DESC_FLAG_RTS;
Johannes Berge039fa42008-05-15 12:55:29 +0200473 tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
Andrea Merello5d0d1e92014-05-07 17:53:17 +0200474 rts_duration = ieee80211_rts_duration(dev, priv->vif,
475 skb->len, info);
Johannes Berge6a98542008-10-21 12:40:02 +0200476 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Andrea Merello5d0d1e92014-05-07 17:53:17 +0200477 tx_flags |= RTL818X_TX_DESC_FLAG_RTS | RTL818X_TX_DESC_FLAG_CTS;
Johannes Berge039fa42008-05-15 12:55:29 +0200478 tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
Andrea Merello5d0d1e92014-05-07 17:53:17 +0200479 rts_duration = ieee80211_ctstoself_duration(dev, priv->vif,
480 skb->len, info);
Johannes Bergaa68cbf2008-02-18 14:20:30 +0100481 }
Michael Wuf6532112007-10-14 14:43:16 -0400482
Andrea Merello6caefd12014-03-08 18:36:37 +0100483 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400484 unsigned int remainder;
485
486 plcp_len = DIV_ROUND_UP(16 * (skb->len + 4),
Johannes Berge039fa42008-05-15 12:55:29 +0200487 (ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
Michael Wuf6532112007-10-14 14:43:16 -0400488 remainder = (16 * (skb->len + 4)) %
Johannes Berge039fa42008-05-15 12:55:29 +0200489 ((ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
Roel Kluin35a0ace2009-06-22 17:42:21 +0200490 if (remainder <= 6)
Michael Wuf6532112007-10-14 14:43:16 -0400491 plcp_len |= 1 << 15;
492 }
493
Andrea Merello3ee44d62014-03-26 21:00:57 +0100494 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
495 __le16 duration;
496 /* SIFS time (required by HW) is already included by
497 * ieee80211_generic_frame_duration
498 */
499 duration = ieee80211_generic_frame_duration(dev, priv->vif,
500 IEEE80211_BAND_2GHZ, skb->len,
501 ieee80211_get_tx_rate(dev, info));
502
503 frame_duration = priv->ack_time + le16_to_cpu(duration);
504 }
505
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400506 spin_lock_irqsave(&priv->lock, flags);
John W. Linville51e080d2010-05-06 16:26:23 -0400507
508 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
509 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
510 priv->seqno += 0x10;
511 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
512 hdr->seq_ctrl |= cpu_to_le16(priv->seqno);
513 }
514
Michael Wuf6532112007-10-14 14:43:16 -0400515 idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
516 entry = &ring->desc[idx];
517
Andrea Merello3ee44d62014-03-26 21:00:57 +0100518 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
519 entry->frame_duration = cpu_to_le16(frame_duration);
520 entry->frame_len_se = cpu_to_le16(skb->len);
521
522 /* tpc polarity */
523 entry->flags3 = cpu_to_le16(1<<4);
524 } else
525 entry->frame_len = cpu_to_le32(skb->len);
526
Michael Wuf6532112007-10-14 14:43:16 -0400527 entry->rts_duration = rts_duration;
528 entry->plcp_len = cpu_to_le16(plcp_len);
529 entry->tx_buf = cpu_to_le32(mapping);
Andrea Merello3ee44d62014-03-26 21:00:57 +0100530
Johannes Berge6a98542008-10-21 12:40:02 +0200531 entry->flags2 = info->control.rates[1].idx >= 0 ?
Felix Fietkau870abdf2008-10-05 18:04:24 +0200532 ieee80211_get_alt_retry_rate(dev, info, 0)->bitrate << 4 : 0;
Johannes Berge6a98542008-10-21 12:40:02 +0200533 entry->retry_limit = info->control.rates[0].count;
andrea merello4c552a52014-02-18 02:10:45 +0100534
535 /* We must be sure that tx_flags is written last because the HW
536 * looks at it to check if the rest of data is valid or not
537 */
538 wmb();
Michael Wuf6532112007-10-14 14:43:16 -0400539 entry->flags = cpu_to_le32(tx_flags);
andrea merelloc24782e2014-02-18 02:10:46 +0100540 /* We must be sure this has been written before followings HW
541 * register write, because this write will made the HW attempts
542 * to DMA the just-written data
543 */
544 wmb();
545
Michael Wuf6532112007-10-14 14:43:16 -0400546 __skb_queue_tail(&ring->queue, skb);
547 if (ring->entries - skb_queue_len(&ring->queue) < 2)
John W. Linvilled10e2e02010-04-27 16:57:38 -0400548 ieee80211_stop_queue(dev, prio);
John W. Linville51e080d2010-05-06 16:26:23 -0400549
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400550 spin_unlock_irqrestore(&priv->lock, flags);
Michael Wuf6532112007-10-14 14:43:16 -0400551
Andrea Merello3ee44d62014-03-26 21:00:57 +0100552 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
553 /* just poll: rings are stopped with TPPollStop reg */
554 hw_prio = rtl8187se_queues_map[prio];
555 rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
556 (1 << hw_prio));
557 } else {
558 hw_prio = rtl8180_queues_map[prio];
559 rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
Andrea Merellofd6564f2014-03-22 18:51:20 +0100560 (1 << hw_prio) | /* ring to poll */
561 (1<<1) | (1<<2));/* stopped rings */
Andrea Merello3ee44d62014-03-26 21:00:57 +0100562 }
Michael Wuf6532112007-10-14 14:43:16 -0400563}
564
Andrea Merelloff3cbc22014-03-26 21:02:05 +0100565static void rtl8180_set_anaparam3(struct rtl8180_priv *priv, u16 anaparam3)
566{
567 u8 reg;
568
569 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
570 RTL818X_EEPROM_CMD_CONFIG);
571
572 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
573 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
574 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
575
576 rtl818x_iowrite16(priv, &priv->map->ANAPARAM3, anaparam3);
577
578 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
579 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
580
581 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
582 RTL818X_EEPROM_CMD_NORMAL);
583}
584
585void rtl8180_set_anaparam2(struct rtl8180_priv *priv, u32 anaparam2)
586{
587 u8 reg;
588
589 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
590 RTL818X_EEPROM_CMD_CONFIG);
591
592 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
593 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
594 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
595
596 rtl818x_iowrite32(priv, &priv->map->ANAPARAM2, anaparam2);
597
598 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
599 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
600
601 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
602 RTL818X_EEPROM_CMD_NORMAL);
603}
604
Michael Wuf6532112007-10-14 14:43:16 -0400605void rtl8180_set_anaparam(struct rtl8180_priv *priv, u32 anaparam)
606{
607 u8 reg;
608
609 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
610 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
611 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
612 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
613 rtl818x_iowrite32(priv, &priv->map->ANAPARAM, anaparam);
614 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
615 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
616 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
617}
618
Andrea Merello4a67aa52014-03-26 21:02:46 +0100619static void rtl8187se_mac_config(struct ieee80211_hw *dev)
620{
621 struct rtl8180_priv *priv = dev->priv;
622 u8 reg;
623
624 rtl818x_iowrite32(priv, REG_ADDR4(0x1F0), 0);
625 rtl818x_ioread32(priv, REG_ADDR4(0x1F0));
626 rtl818x_iowrite32(priv, REG_ADDR4(0x1F4), 0);
627 rtl818x_ioread32(priv, REG_ADDR4(0x1F4));
628 rtl818x_iowrite8(priv, REG_ADDR1(0x1F8), 0);
629 rtl818x_ioread8(priv, REG_ADDR1(0x1F8));
630 /* Enable DA10 TX power saving */
631 reg = rtl818x_ioread8(priv, &priv->map->PHY_PR);
632 rtl818x_iowrite8(priv, &priv->map->PHY_PR, reg | 0x04);
633 /* Power */
634 rtl818x_iowrite16(priv, PI_DATA_REG, 0x1000);
635 rtl818x_iowrite16(priv, SI_DATA_REG, 0x1000);
636 /* AFE - default to power ON */
637 rtl818x_iowrite16(priv, REG_ADDR2(0x370), 0x0560);
638 rtl818x_iowrite16(priv, REG_ADDR2(0x372), 0x0560);
639 rtl818x_iowrite16(priv, REG_ADDR2(0x374), 0x0DA4);
640 rtl818x_iowrite16(priv, REG_ADDR2(0x376), 0x0DA4);
641 rtl818x_iowrite16(priv, REG_ADDR2(0x378), 0x0560);
642 rtl818x_iowrite16(priv, REG_ADDR2(0x37A), 0x0560);
643 rtl818x_iowrite16(priv, REG_ADDR2(0x37C), 0x00EC);
644 rtl818x_iowrite16(priv, REG_ADDR2(0x37E), 0x00EC);
645 rtl818x_iowrite8(priv, REG_ADDR1(0x24E), 0x01);
646 /* unknown, needed for suspend to RAM resume */
647 rtl818x_iowrite8(priv, REG_ADDR1(0x0A), 0x72);
648}
649
650static void rtl8187se_set_antenna_config(struct ieee80211_hw *dev, u8 def_ant,
651 bool diversity)
652{
653 struct rtl8180_priv *priv = dev->priv;
654
655 rtl8225_write_phy_cck(dev, 0x0C, 0x09);
656 if (diversity) {
657 if (def_ant == 1) {
658 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x00);
659 rtl8225_write_phy_cck(dev, 0x11, 0xBB);
660 rtl8225_write_phy_cck(dev, 0x01, 0xC7);
661 rtl8225_write_phy_ofdm(dev, 0x0D, 0x54);
662 rtl8225_write_phy_ofdm(dev, 0x18, 0xB2);
663 } else { /* main antenna */
664 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x03);
665 rtl8225_write_phy_cck(dev, 0x11, 0x9B);
666 rtl8225_write_phy_cck(dev, 0x01, 0xC7);
667 rtl8225_write_phy_ofdm(dev, 0x0D, 0x5C);
668 rtl8225_write_phy_ofdm(dev, 0x18, 0xB2);
669 }
670 } else { /* disable antenna diversity */
671 if (def_ant == 1) {
672 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x00);
673 rtl8225_write_phy_cck(dev, 0x11, 0xBB);
674 rtl8225_write_phy_cck(dev, 0x01, 0x47);
675 rtl8225_write_phy_ofdm(dev, 0x0D, 0x54);
676 rtl8225_write_phy_ofdm(dev, 0x18, 0x32);
677 } else { /* main antenna */
678 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x03);
679 rtl8225_write_phy_cck(dev, 0x11, 0x9B);
680 rtl8225_write_phy_cck(dev, 0x01, 0x47);
681 rtl8225_write_phy_ofdm(dev, 0x0D, 0x5C);
682 rtl8225_write_phy_ofdm(dev, 0x18, 0x32);
683 }
684 }
685 /* priv->curr_ant = def_ant; */
686}
687
Andrea Merello732c8932014-03-26 21:00:24 +0100688static void rtl8180_int_enable(struct ieee80211_hw *dev)
689{
690 struct rtl8180_priv *priv = dev->priv;
691
692 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
Andrea Merello1c3fb9b2014-04-04 18:21:14 +0200693 rtl818x_iowrite32(priv, &priv->map->IMR,
694 IMR_TBDER | IMR_TBDOK |
Andrea Merello732c8932014-03-26 21:00:24 +0100695 IMR_TVODER | IMR_TVODOK |
696 IMR_TVIDER | IMR_TVIDOK |
697 IMR_TBEDER | IMR_TBEDOK |
698 IMR_TBKDER | IMR_TBKDOK |
699 IMR_RDU | IMR_RER |
700 IMR_ROK | IMR_RQOSOK);
701 } else {
702 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
703 }
704}
705
706static void rtl8180_int_disable(struct ieee80211_hw *dev)
707{
708 struct rtl8180_priv *priv = dev->priv;
709
710 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
711 rtl818x_iowrite32(priv, &priv->map->IMR, 0);
712 } else {
713 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
714 }
715}
716
Andrea Merello516a0932014-03-15 18:29:36 +0100717static void rtl8180_conf_basic_rates(struct ieee80211_hw *dev,
718 u32 rates_mask)
719{
720 struct rtl8180_priv *priv = dev->priv;
721
722 u8 max, min;
723 u16 reg;
724
725 max = fls(rates_mask) - 1;
726 min = ffs(rates_mask) - 1;
727
728 switch (priv->chip_family) {
729
730 case RTL818X_CHIP_FAMILY_RTL8180:
731 /* in 8180 this is NOT a BITMAP */
732 reg = rtl818x_ioread16(priv, &priv->map->BRSR);
733 reg &= ~3;
734 reg |= max;
735 rtl818x_iowrite16(priv, &priv->map->BRSR, reg);
Andrea Merello516a0932014-03-15 18:29:36 +0100736 break;
737
738 case RTL818X_CHIP_FAMILY_RTL8185:
739 /* in 8185 this is a BITMAP */
740 rtl818x_iowrite16(priv, &priv->map->BRSR, rates_mask);
741 rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (max << 4) | min);
742 break;
Andrea Merellod209f3b2014-03-26 20:59:25 +0100743
744 case RTL818X_CHIP_FAMILY_RTL8187SE:
745 /* in 8187se this is a BITMAP */
746 rtl818x_iowrite16(priv, &priv->map->BRSR_8187SE, rates_mask);
747 break;
Andrea Merello516a0932014-03-15 18:29:36 +0100748 }
749}
750
Andrea Merellof1026df2014-03-26 21:01:19 +0100751static void rtl8180_config_cardbus(struct ieee80211_hw *dev)
752{
753 struct rtl8180_priv *priv = dev->priv;
754 u16 reg16;
755 u8 reg8;
756
757 reg8 = rtl818x_ioread8(priv, &priv->map->CONFIG3);
758 reg8 |= 1 << 1;
759 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg8);
760
761 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
762 rtl818x_iowrite16(priv, FEMR_SE, 0xffff);
763 } else {
764 reg16 = rtl818x_ioread16(priv, &priv->map->FEMR);
765 reg16 |= (1 << 15) | (1 << 14) | (1 << 4);
766 rtl818x_iowrite16(priv, &priv->map->FEMR, reg16);
767 }
768
769}
770
Michael Wuf6532112007-10-14 14:43:16 -0400771static int rtl8180_init_hw(struct ieee80211_hw *dev)
772{
773 struct rtl8180_priv *priv = dev->priv;
774 u16 reg;
Andrea Merello4a67aa52014-03-26 21:02:46 +0100775 u32 reg32;
Michael Wuf6532112007-10-14 14:43:16 -0400776
777 rtl818x_iowrite8(priv, &priv->map->CMD, 0);
778 rtl818x_ioread8(priv, &priv->map->CMD);
779 msleep(10);
780
781 /* reset */
Andrea Merello732c8932014-03-26 21:00:24 +0100782 rtl8180_int_disable(dev);
Michael Wuf6532112007-10-14 14:43:16 -0400783 rtl818x_ioread8(priv, &priv->map->CMD);
784
785 reg = rtl818x_ioread8(priv, &priv->map->CMD);
786 reg &= (1 << 1);
787 reg |= RTL818X_CMD_RESET;
788 rtl818x_iowrite8(priv, &priv->map->CMD, RTL818X_CMD_RESET);
789 rtl818x_ioread8(priv, &priv->map->CMD);
790 msleep(200);
791
792 /* check success of reset */
793 if (rtl818x_ioread8(priv, &priv->map->CMD) & RTL818X_CMD_RESET) {
Joe Perchesc96c31e2010-07-26 14:39:58 -0700794 wiphy_err(dev->wiphy, "reset timeout!\n");
Michael Wuf6532112007-10-14 14:43:16 -0400795 return -ETIMEDOUT;
796 }
797
798 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
799 rtl818x_ioread8(priv, &priv->map->CMD);
800 msleep(200);
801
802 if (rtl818x_ioread8(priv, &priv->map->CONFIG3) & (1 << 3)) {
Andrea Merellof1026df2014-03-26 21:01:19 +0100803 rtl8180_config_cardbus(dev);
Michael Wuf6532112007-10-14 14:43:16 -0400804 }
805
Andrea Merello4a67aa52014-03-26 21:02:46 +0100806 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
807 rtl818x_iowrite8(priv, &priv->map->MSR, RTL818X_MSR_ENEDCA);
808 else
809 rtl818x_iowrite8(priv, &priv->map->MSR, 0);
Michael Wuf6532112007-10-14 14:43:16 -0400810
Andrea Merello6caefd12014-03-08 18:36:37 +0100811 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
Michael Wuf6532112007-10-14 14:43:16 -0400812 rtl8180_set_anaparam(priv, priv->anaparam);
813
814 rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
Andrea Merello4a67aa52014-03-26 21:02:46 +0100815 /* mac80211 queue have higher prio for lower index. The last queue
816 * (that mac80211 is not aware of) is reserved for beacons (and have
817 * the highest priority on the NIC)
818 */
819 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8187SE) {
820 rtl818x_iowrite32(priv, &priv->map->TBDA,
821 priv->tx_ring[1].dma);
822 rtl818x_iowrite32(priv, &priv->map->TLPDA,
823 priv->tx_ring[0].dma);
824 } else {
825 rtl818x_iowrite32(priv, &priv->map->TBDA,
826 priv->tx_ring[4].dma);
827 rtl818x_iowrite32(priv, &priv->map->TVODA,
828 priv->tx_ring[0].dma);
829 rtl818x_iowrite32(priv, &priv->map->TVIDA,
830 priv->tx_ring[1].dma);
831 rtl818x_iowrite32(priv, &priv->map->TBEDA,
832 priv->tx_ring[2].dma);
833 rtl818x_iowrite32(priv, &priv->map->TBKDA,
834 priv->tx_ring[3].dma);
835 }
Michael Wuf6532112007-10-14 14:43:16 -0400836
837 /* TODO: necessary? specs indicate not */
838 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
839 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
840 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg & ~(1 << 3));
Andrea Merello6caefd12014-03-08 18:36:37 +0100841 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
Michael Wuf6532112007-10-14 14:43:16 -0400842 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
843 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg | (1 << 4));
844 }
845 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
846
847 /* TODO: set CONFIG5 for calibrating AGC on rtl8180 + philips radio? */
848
849 /* TODO: turn off hw wep on rtl8180 */
850
851 rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
852
Andrea Merello6caefd12014-03-08 18:36:37 +0100853 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400854 rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
855 rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
Andrea Merello4a67aa52014-03-26 21:02:46 +0100856 } else {
857 rtl818x_iowrite8(priv, &priv->map->SECURITY, 0);
Michael Wuf6532112007-10-14 14:43:16 -0400858
Andrea Merello4a67aa52014-03-26 21:02:46 +0100859 rtl818x_iowrite8(priv, &priv->map->PHY_DELAY, 0x6);
860 rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, 0x4C);
861 }
862
863 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
Michael Wuf6532112007-10-14 14:43:16 -0400864 /* TODO: set ClkRun enable? necessary? */
865 reg = rtl818x_ioread8(priv, &priv->map->GP_ENABLE);
866 rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, reg & ~(1 << 6));
867 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
868 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
869 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | (1 << 2));
870 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
Andrea Merello4a67aa52014-03-26 21:02:46 +0100871 }
Michael Wuf6532112007-10-14 14:43:16 -0400872
Andrea Merello4a67aa52014-03-26 21:02:46 +0100873 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
874
875 /* the set auto rate fallback bitmask from 1M to 54 Mb/s */
876 rtl818x_iowrite16(priv, ARFR, 0xFFF);
877 rtl818x_ioread16(priv, ARFR);
878
879 /* stop unused queus (no dma alloc) */
880 rtl818x_iowrite8(priv, &priv->map->TPPOLL_STOP,
881 RTL818x_TPPOLL_STOP_MG | RTL818x_TPPOLL_STOP_HI);
882
883 rtl818x_iowrite8(priv, &priv->map->ACM_CONTROL, 0x00);
884 rtl818x_iowrite16(priv, &priv->map->TID_AC_MAP, 0xFA50);
885
886 rtl818x_iowrite16(priv, &priv->map->INT_MIG, 0);
887
888 /* some black magic here.. */
889 rtl8187se_mac_config(dev);
890
891 rtl818x_iowrite16(priv, RFSW_CTRL, 0x569A);
892 rtl818x_ioread16(priv, RFSW_CTRL);
893
894 rtl8180_set_anaparam(priv, RTL8225SE_ANAPARAM_ON);
895 rtl8180_set_anaparam2(priv, RTL8225SE_ANAPARAM2_ON);
896 rtl8180_set_anaparam3(priv, RTL8225SE_ANAPARAM3);
897
898
899 rtl818x_iowrite8(priv, &priv->map->CONFIG5,
900 rtl818x_ioread8(priv, &priv->map->CONFIG5) & 0x7F);
901
902 /*probably this switch led on */
903 rtl818x_iowrite8(priv, &priv->map->PGSELECT,
904 rtl818x_ioread8(priv, &priv->map->PGSELECT) | 0x08);
905
906 rtl818x_iowrite16(priv, &priv->map->RFPinsOutput, 0x0480);
907 rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x1BFF);
908 rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0x2488);
909
910 rtl818x_iowrite32(priv, &priv->map->RF_TIMING, 0x4003);
911
912 /* the reference code mac hardcode table write
913 * this reg by doing byte-wide accesses.
914 * It does it just for lowest and highest byte..
915 */
916 reg32 = rtl818x_ioread32(priv, &priv->map->RF_PARA);
917 reg32 &= 0x00ffff00;
918 reg32 |= 0xb8000054;
919 rtl818x_iowrite32(priv, &priv->map->RF_PARA, reg32);
Andrea Merello294bc612014-04-04 18:25:51 +0200920 } else
921 /* stop unused queus (no dma alloc) */
922 rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
923 (1<<1) | (1<<2));
Michael Wuf6532112007-10-14 14:43:16 -0400924
925 priv->rf->init(dev);
Andrea Merello516a0932014-03-15 18:29:36 +0100926
927 /* default basic rates are 1,2 Mbps for rtl8180. 1,2,6,9,12,18,24 Mbps
928 * otherwise. bitmask 0x3 and 0x01f3 respectively.
929 * NOTE: currenty rtl8225 RF code changes basic rates, so we need to do
930 * this after rf init.
931 * TODO: try to find out whether RF code really needs to do this..
932 */
933 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
934 rtl8180_conf_basic_rates(dev, 0x3);
935 else
936 rtl8180_conf_basic_rates(dev, 0x1f3);
937
Andrea Merello4a67aa52014-03-26 21:02:46 +0100938 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
939 rtl8187se_set_antenna_config(dev,
940 priv->antenna_diversity_default,
941 priv->antenna_diversity_en);
Michael Wuf6532112007-10-14 14:43:16 -0400942 return 0;
943}
944
945static int rtl8180_init_rx_ring(struct ieee80211_hw *dev)
946{
947 struct rtl8180_priv *priv = dev->priv;
Andrea Merello21025922014-03-26 20:59:52 +0100948 struct rtl818x_rx_cmd_desc *entry;
Michael Wuf6532112007-10-14 14:43:16 -0400949 int i;
950
Andrea Merello21025922014-03-26 20:59:52 +0100951 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
952 priv->rx_ring_sz = sizeof(struct rtl8187se_rx_desc);
953 else
954 priv->rx_ring_sz = sizeof(struct rtl8180_rx_desc);
955
Michael Wuf6532112007-10-14 14:43:16 -0400956 priv->rx_ring = pci_alloc_consistent(priv->pdev,
Andrea Merello21025922014-03-26 20:59:52 +0100957 priv->rx_ring_sz * 32,
Michael Wuf6532112007-10-14 14:43:16 -0400958 &priv->rx_ring_dma);
959
960 if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -0700961 wiphy_err(dev->wiphy, "Cannot allocate RX ring\n");
Michael Wuf6532112007-10-14 14:43:16 -0400962 return -ENOMEM;
963 }
964
Andrea Merello21025922014-03-26 20:59:52 +0100965 memset(priv->rx_ring, 0, priv->rx_ring_sz * 32);
Michael Wuf6532112007-10-14 14:43:16 -0400966 priv->rx_idx = 0;
967
968 for (i = 0; i < 32; i++) {
969 struct sk_buff *skb = dev_alloc_skb(MAX_RX_SIZE);
970 dma_addr_t *mapping;
Andrea Merello21025922014-03-26 20:59:52 +0100971 entry = priv->rx_ring + priv->rx_ring_sz*i;
andrea merello4da18bb2014-02-18 02:10:43 +0100972 if (!skb) {
973 wiphy_err(dev->wiphy, "Cannot allocate RX skb\n");
974 return -ENOMEM;
975 }
Michael Wuf6532112007-10-14 14:43:16 -0400976 priv->rx_buf[i] = skb;
977 mapping = (dma_addr_t *)skb->cb;
978 *mapping = pci_map_single(priv->pdev, skb_tail_pointer(skb),
979 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
andrea merelloec1da082014-02-22 17:57:23 +0100980
981 if (pci_dma_mapping_error(priv->pdev, *mapping)) {
982 kfree_skb(skb);
983 wiphy_err(dev->wiphy, "Cannot map DMA for RX skb\n");
984 return -ENOMEM;
985 }
986
Michael Wuf6532112007-10-14 14:43:16 -0400987 entry->rx_buf = cpu_to_le32(*mapping);
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300988 entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
Michael Wuf6532112007-10-14 14:43:16 -0400989 MAX_RX_SIZE);
990 }
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300991 entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
Michael Wuf6532112007-10-14 14:43:16 -0400992 return 0;
993}
994
995static void rtl8180_free_rx_ring(struct ieee80211_hw *dev)
996{
997 struct rtl8180_priv *priv = dev->priv;
998 int i;
999
1000 for (i = 0; i < 32; i++) {
1001 struct sk_buff *skb = priv->rx_buf[i];
1002 if (!skb)
1003 continue;
1004
1005 pci_unmap_single(priv->pdev,
1006 *((dma_addr_t *)skb->cb),
1007 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
1008 kfree_skb(skb);
1009 }
1010
Andrea Merello21025922014-03-26 20:59:52 +01001011 pci_free_consistent(priv->pdev, priv->rx_ring_sz * 32,
Michael Wuf6532112007-10-14 14:43:16 -04001012 priv->rx_ring, priv->rx_ring_dma);
1013 priv->rx_ring = NULL;
1014}
1015
1016static int rtl8180_init_tx_ring(struct ieee80211_hw *dev,
1017 unsigned int prio, unsigned int entries)
1018{
1019 struct rtl8180_priv *priv = dev->priv;
1020 struct rtl8180_tx_desc *ring;
1021 dma_addr_t dma;
1022 int i;
1023
1024 ring = pci_alloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
1025 if (!ring || (unsigned long)ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -07001026 wiphy_err(dev->wiphy, "Cannot allocate TX ring (prio = %d)\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001027 prio);
Michael Wuf6532112007-10-14 14:43:16 -04001028 return -ENOMEM;
1029 }
1030
1031 memset(ring, 0, sizeof(*ring)*entries);
1032 priv->tx_ring[prio].desc = ring;
1033 priv->tx_ring[prio].dma = dma;
1034 priv->tx_ring[prio].idx = 0;
1035 priv->tx_ring[prio].entries = entries;
1036 skb_queue_head_init(&priv->tx_ring[prio].queue);
1037
1038 for (i = 0; i < entries; i++)
1039 ring[i].next_tx_desc =
1040 cpu_to_le32((u32)dma + ((i + 1) % entries) * sizeof(*ring));
1041
1042 return 0;
1043}
1044
1045static void rtl8180_free_tx_ring(struct ieee80211_hw *dev, unsigned int prio)
1046{
1047 struct rtl8180_priv *priv = dev->priv;
1048 struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
1049
1050 while (skb_queue_len(&ring->queue)) {
1051 struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
1052 struct sk_buff *skb = __skb_dequeue(&ring->queue);
1053
1054 pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
1055 skb->len, PCI_DMA_TODEVICE);
Michael Wuf6532112007-10-14 14:43:16 -04001056 kfree_skb(skb);
1057 ring->idx = (ring->idx + 1) % ring->entries;
1058 }
1059
1060 pci_free_consistent(priv->pdev, sizeof(*ring->desc)*ring->entries,
1061 ring->desc, ring->dma);
1062 ring->desc = NULL;
1063}
1064
1065static int rtl8180_start(struct ieee80211_hw *dev)
1066{
1067 struct rtl8180_priv *priv = dev->priv;
1068 int ret, i;
1069 u32 reg;
1070
1071 ret = rtl8180_init_rx_ring(dev);
1072 if (ret)
1073 return ret;
1074
Andrea Merellofd6564f2014-03-22 18:51:20 +01001075 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -04001076 if ((ret = rtl8180_init_tx_ring(dev, i, 16)))
1077 goto err_free_rings;
1078
1079 ret = rtl8180_init_hw(dev);
1080 if (ret)
1081 goto err_free_rings;
1082
Andrea Merelloa373ebc2014-03-26 21:00:06 +01001083 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1084 ret = request_irq(priv->pdev->irq, rtl8187se_interrupt,
Michael Wuf6532112007-10-14 14:43:16 -04001085 IRQF_SHARED, KBUILD_MODNAME, dev);
Andrea Merelloa373ebc2014-03-26 21:00:06 +01001086 } else {
1087 ret = request_irq(priv->pdev->irq, rtl8180_interrupt,
1088 IRQF_SHARED, KBUILD_MODNAME, dev);
1089 }
1090
Michael Wuf6532112007-10-14 14:43:16 -04001091 if (ret) {
Joe Perches5db55842010-08-11 19:11:19 -07001092 wiphy_err(dev->wiphy, "failed to register IRQ handler\n");
Michael Wuf6532112007-10-14 14:43:16 -04001093 goto err_free_rings;
1094 }
1095
Andrea Merello732c8932014-03-26 21:00:24 +01001096 rtl8180_int_enable(dev);
Michael Wuf6532112007-10-14 14:43:16 -04001097
Andrea Merellof18f1122014-03-26 21:00:42 +01001098 /* in rtl8187se at MAR regs offset there is the management
1099 * TX descriptor DMA addres..
1100 */
1101 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8187SE) {
1102 rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
1103 rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
1104 }
Michael Wuf6532112007-10-14 14:43:16 -04001105
1106 reg = RTL818X_RX_CONF_ONLYERLPKT |
1107 RTL818X_RX_CONF_RX_AUTORESETPHY |
1108 RTL818X_RX_CONF_MGMT |
1109 RTL818X_RX_CONF_DATA |
1110 (7 << 8 /* MAX RX DMA */) |
1111 RTL818X_RX_CONF_BROADCAST |
1112 RTL818X_RX_CONF_NICMAC;
1113
Andrea Merello6caefd12014-03-08 18:36:37 +01001114 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185)
Michael Wuf6532112007-10-14 14:43:16 -04001115 reg |= RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2;
Andrea Merello4a67aa52014-03-26 21:02:46 +01001116 else if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -04001117 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE1)
1118 ? RTL818X_RX_CONF_CSDM1 : 0;
1119 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE2)
1120 ? RTL818X_RX_CONF_CSDM2 : 0;
Andrea Merello4a67aa52014-03-26 21:02:46 +01001121 } else {
1122 reg &= ~(RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2);
Michael Wuf6532112007-10-14 14:43:16 -04001123 }
1124
1125 priv->rx_conf = reg;
1126 rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
1127
Andrea Merello6caefd12014-03-08 18:36:37 +01001128 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -04001129 reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
andrea merello14c76152014-02-18 02:10:44 +01001130
1131 /* CW is not on per-packet basis.
1132 * in rtl8185 the CW_VALUE reg is used.
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001133 * in rtl8187se the AC param regs are used.
andrea merello14c76152014-02-18 02:10:44 +01001134 */
andrea merello6f7343d2014-01-21 20:16:43 +01001135 reg &= ~RTL818X_CW_CONF_PERPACKET_CW;
andrea merello14c76152014-02-18 02:10:44 +01001136 /* retry limit IS on per-packet basis.
1137 * the short and long retry limit in TX_CONF
1138 * reg are ignored
1139 */
andrea merello6f7343d2014-01-21 20:16:43 +01001140 reg |= RTL818X_CW_CONF_PERPACKET_RETRY;
Michael Wuf6532112007-10-14 14:43:16 -04001141 rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
1142
1143 reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
andrea merello14c76152014-02-18 02:10:44 +01001144 /* TX antenna and TX gain are not on per-packet basis.
1145 * TX Antenna is selected by ANTSEL reg (RX in BB regs).
1146 * TX gain is selected with CCK_TX_AGC and OFDM_TX_AGC regs
1147 */
andrea merello6f7343d2014-01-21 20:16:43 +01001148 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN;
1149 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL;
Michael Wuf6532112007-10-14 14:43:16 -04001150 reg |= RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
1151 rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
1152
1153 /* disable early TX */
1154 rtl818x_iowrite8(priv, (u8 __iomem *)priv->map + 0xec, 0x3f);
1155 }
1156
1157 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
1158 reg |= (6 << 21 /* MAX TX DMA */) |
1159 RTL818X_TX_CONF_NO_ICV;
1160
Andrea Merello4a67aa52014-03-26 21:02:46 +01001161 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1162 reg |= 1<<30; /* "duration procedure mode" */
Andrea Merello6caefd12014-03-08 18:36:37 +01001163
1164 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
Michael Wuf6532112007-10-14 14:43:16 -04001165 reg &= ~RTL818X_TX_CONF_PROBE_DTS;
1166 else
1167 reg &= ~RTL818X_TX_CONF_HW_SEQNUM;
1168
andrea merelloe74075a2014-02-18 02:10:40 +01001169 reg &= ~RTL818X_TX_CONF_DISCW;
1170
Michael Wuf6532112007-10-14 14:43:16 -04001171 /* different meaning, same value on both rtl8185 and rtl8180 */
1172 reg &= ~RTL818X_TX_CONF_SAT_HWPLCP;
1173
1174 rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
1175
1176 reg = rtl818x_ioread8(priv, &priv->map->CMD);
1177 reg |= RTL818X_CMD_RX_ENABLE;
1178 reg |= RTL818X_CMD_TX_ENABLE;
1179 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
1180
Michael Wuf6532112007-10-14 14:43:16 -04001181 return 0;
1182
1183 err_free_rings:
1184 rtl8180_free_rx_ring(dev);
Andrea Merellofd6564f2014-03-22 18:51:20 +01001185 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -04001186 if (priv->tx_ring[i].desc)
1187 rtl8180_free_tx_ring(dev, i);
1188
1189 return ret;
1190}
1191
1192static void rtl8180_stop(struct ieee80211_hw *dev)
1193{
1194 struct rtl8180_priv *priv = dev->priv;
1195 u8 reg;
1196 int i;
1197
Andrea Merello732c8932014-03-26 21:00:24 +01001198 rtl8180_int_disable(dev);
Michael Wuf6532112007-10-14 14:43:16 -04001199
1200 reg = rtl818x_ioread8(priv, &priv->map->CMD);
1201 reg &= ~RTL818X_CMD_TX_ENABLE;
1202 reg &= ~RTL818X_CMD_RX_ENABLE;
1203 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
1204
1205 priv->rf->stop(dev);
1206
1207 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
1208 reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
1209 rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
1210 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
1211
1212 free_irq(priv->pdev->irq, dev);
1213
1214 rtl8180_free_rx_ring(dev);
Andrea Merellofd6564f2014-03-22 18:51:20 +01001215 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -04001216 rtl8180_free_tx_ring(dev, i);
1217}
1218
Eliad Peller37a41b42011-09-21 14:06:11 +03001219static u64 rtl8180_get_tsf(struct ieee80211_hw *dev,
1220 struct ieee80211_vif *vif)
John W. Linvillec809e862010-05-06 16:49:40 -04001221{
1222 struct rtl8180_priv *priv = dev->priv;
1223
1224 return rtl818x_ioread32(priv, &priv->map->TSFT[0]) |
1225 (u64)(rtl818x_ioread32(priv, &priv->map->TSFT[1])) << 32;
1226}
1227
John W. Linvillea3275e22010-06-24 11:08:37 -04001228static void rtl8180_beacon_work(struct work_struct *work)
John W. Linvillec809e862010-05-06 16:49:40 -04001229{
1230 struct rtl8180_vif *vif_priv =
1231 container_of(work, struct rtl8180_vif, beacon_work.work);
1232 struct ieee80211_vif *vif =
1233 container_of((void *)vif_priv, struct ieee80211_vif, drv_priv);
1234 struct ieee80211_hw *dev = vif_priv->dev;
1235 struct ieee80211_mgmt *mgmt;
1236 struct sk_buff *skb;
John W. Linvillec809e862010-05-06 16:49:40 -04001237
1238 /* don't overflow the tx ring */
1239 if (ieee80211_queue_stopped(dev, 0))
1240 goto resched;
1241
1242 /* grab a fresh beacon */
1243 skb = ieee80211_beacon_get(dev, vif);
John W. Linville8f1d2d22010-08-05 13:46:27 -04001244 if (!skb)
1245 goto resched;
John W. Linvillec809e862010-05-06 16:49:40 -04001246
1247 /*
1248 * update beacon timestamp w/ TSF value
1249 * TODO: make hardware update beacon timestamp
1250 */
1251 mgmt = (struct ieee80211_mgmt *)skb->data;
Eliad Peller37a41b42011-09-21 14:06:11 +03001252 mgmt->u.beacon.timestamp = cpu_to_le64(rtl8180_get_tsf(dev, vif));
John W. Linvillec809e862010-05-06 16:49:40 -04001253
1254 /* TODO: use actual beacon queue */
1255 skb_set_queue_mapping(skb, 0);
1256
Thomas Huehn36323f82012-07-23 21:33:42 +02001257 rtl8180_tx(dev, NULL, skb);
John W. Linvillec809e862010-05-06 16:49:40 -04001258
1259resched:
1260 /*
1261 * schedule next beacon
1262 * TODO: use hardware support for beacon timing
1263 */
1264 schedule_delayed_work(&vif_priv->beacon_work,
1265 usecs_to_jiffies(1024 * vif->bss_conf.beacon_int));
1266}
1267
Michael Wuf6532112007-10-14 14:43:16 -04001268static int rtl8180_add_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001269 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -04001270{
1271 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -04001272 struct rtl8180_vif *vif_priv;
Michael Wuf6532112007-10-14 14:43:16 -04001273
John W. Linville643aab62009-12-22 18:13:04 -05001274 /*
1275 * We only support one active interface at a time.
1276 */
1277 if (priv->vif)
1278 return -EBUSY;
Michael Wuf6532112007-10-14 14:43:16 -04001279
Johannes Berg1ed32e42009-12-23 13:15:45 +01001280 switch (vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001281 case NL80211_IFTYPE_STATION:
John W. Linvillec809e862010-05-06 16:49:40 -04001282 case NL80211_IFTYPE_ADHOC:
Michael Wuf6532112007-10-14 14:43:16 -04001283 break;
1284 default:
1285 return -EOPNOTSUPP;
1286 }
1287
Johannes Berg1ed32e42009-12-23 13:15:45 +01001288 priv->vif = vif;
Johannes Berg32bfd352007-12-19 01:31:26 +01001289
John W. Linvillec809e862010-05-06 16:49:40 -04001290 /* Initialize driver private area */
1291 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
1292 vif_priv->dev = dev;
1293 INIT_DELAYED_WORK(&vif_priv->beacon_work, rtl8180_beacon_work);
1294 vif_priv->enable_beacon = false;
1295
Michael Wuf6532112007-10-14 14:43:16 -04001296 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
1297 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->MAC[0],
Johannes Berg1ed32e42009-12-23 13:15:45 +01001298 le32_to_cpu(*(__le32 *)vif->addr));
Michael Wuf6532112007-10-14 14:43:16 -04001299 rtl818x_iowrite16(priv, (__le16 __iomem *)&priv->map->MAC[4],
Johannes Berg1ed32e42009-12-23 13:15:45 +01001300 le16_to_cpu(*(__le16 *)(vif->addr + 4)));
Michael Wuf6532112007-10-14 14:43:16 -04001301 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
1302
1303 return 0;
1304}
1305
1306static void rtl8180_remove_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001307 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -04001308{
1309 struct rtl8180_priv *priv = dev->priv;
Johannes Berg32bfd352007-12-19 01:31:26 +01001310 priv->vif = NULL;
Michael Wuf6532112007-10-14 14:43:16 -04001311}
1312
Johannes Berge8975582008-10-09 12:18:51 +02001313static int rtl8180_config(struct ieee80211_hw *dev, u32 changed)
Michael Wuf6532112007-10-14 14:43:16 -04001314{
1315 struct rtl8180_priv *priv = dev->priv;
Johannes Berge8975582008-10-09 12:18:51 +02001316 struct ieee80211_conf *conf = &dev->conf;
Michael Wuf6532112007-10-14 14:43:16 -04001317
1318 priv->rf->set_chan(dev, conf);
1319
1320 return 0;
1321}
1322
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001323static void rtl8187se_conf_ac_parm(struct ieee80211_hw *dev, u8 queue)
1324{
1325 const struct ieee80211_tx_queue_params *params;
1326 struct rtl8180_priv *priv = dev->priv;
1327
1328 /* hw value */
1329 u32 ac_param;
1330
1331 u8 aifs;
1332 u8 txop;
1333 u8 cw_min, cw_max;
1334
1335 params = &priv->queue_param[queue];
1336
1337 cw_min = fls(params->cw_min);
1338 cw_max = fls(params->cw_max);
1339
1340 aifs = 10 + params->aifs * priv->slot_time;
1341
1342 /* TODO: check if txop HW is in us (mult by 32) */
1343 txop = params->txop;
1344
1345 ac_param = txop << AC_PARAM_TXOP_LIMIT_SHIFT |
1346 cw_max << AC_PARAM_ECW_MAX_SHIFT |
1347 cw_min << AC_PARAM_ECW_MIN_SHIFT |
1348 aifs << AC_PARAM_AIFS_SHIFT;
1349
1350 switch (queue) {
1351 case IEEE80211_AC_BK:
1352 rtl818x_iowrite32(priv, &priv->map->AC_BK_PARAM, ac_param);
1353 break;
1354 case IEEE80211_AC_BE:
1355 rtl818x_iowrite32(priv, &priv->map->AC_BE_PARAM, ac_param);
1356 break;
1357 case IEEE80211_AC_VI:
1358 rtl818x_iowrite32(priv, &priv->map->AC_VI_PARAM, ac_param);
1359 break;
1360 case IEEE80211_AC_VO:
1361 rtl818x_iowrite32(priv, &priv->map->AC_VO_PARAM, ac_param);
1362 break;
1363 }
1364}
1365
Andrea Merello9069af72014-03-15 18:29:37 +01001366static int rtl8180_conf_tx(struct ieee80211_hw *dev,
1367 struct ieee80211_vif *vif, u16 queue,
1368 const struct ieee80211_tx_queue_params *params)
1369{
1370 struct rtl8180_priv *priv = dev->priv;
1371 u8 cw_min, cw_max;
1372
1373 /* nothing to do ? */
1374 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
1375 return 0;
1376
1377 cw_min = fls(params->cw_min);
1378 cw_max = fls(params->cw_max);
1379
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001380 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1381 priv->queue_param[queue] = *params;
1382 rtl8187se_conf_ac_parm(dev, queue);
1383 } else
1384 rtl818x_iowrite8(priv, &priv->map->CW_VAL,
1385 (cw_max << 4) | cw_min);
Andrea Merello9069af72014-03-15 18:29:37 +01001386 return 0;
1387}
1388
1389static void rtl8180_conf_erp(struct ieee80211_hw *dev,
1390 struct ieee80211_bss_conf *info)
1391{
1392 struct rtl8180_priv *priv = dev->priv;
1393 u8 sifs, difs;
1394 int eifs;
1395 u8 hw_eifs;
1396
1397 /* TODO: should we do something ? */
1398 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
1399 return;
1400
1401 /* I _hope_ this means 10uS for the HW.
1402 * In reference code it is 0x22 for
1403 * both rtl8187L and rtl8187SE
1404 */
1405 sifs = 0x22;
1406
1407 if (info->use_short_slot)
1408 priv->slot_time = 9;
1409 else
1410 priv->slot_time = 20;
1411
1412 /* 10 is SIFS time in uS */
1413 difs = 10 + 2 * priv->slot_time;
1414 eifs = 10 + difs + priv->ack_time;
1415
1416 /* HW should use 4uS units for EIFS (I'm sure for rtl8185)*/
1417 hw_eifs = DIV_ROUND_UP(eifs, 4);
1418
1419
1420 rtl818x_iowrite8(priv, &priv->map->SLOT, priv->slot_time);
1421 rtl818x_iowrite8(priv, &priv->map->SIFS, sifs);
1422 rtl818x_iowrite8(priv, &priv->map->DIFS, difs);
1423
1424 /* from reference code. set ack timeout reg = eifs reg */
1425 rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, hw_eifs);
1426
Andrea Merello355668d2014-03-26 21:03:03 +01001427 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1428 rtl818x_iowrite8(priv, &priv->map->EIFS_8187SE, hw_eifs);
1429 else if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
1430 /* rtl8187/rtl8185 HW bug. After EIFS is elapsed,
1431 * the HW still wait for DIFS.
1432 * HW uses 4uS units for EIFS.
1433 */
1434 hw_eifs = DIV_ROUND_UP(eifs - difs, 4);
Andrea Merello9069af72014-03-15 18:29:37 +01001435
Andrea Merello355668d2014-03-26 21:03:03 +01001436 rtl818x_iowrite8(priv, &priv->map->EIFS, hw_eifs);
1437 }
Andrea Merello9069af72014-03-15 18:29:37 +01001438}
1439
John W. Linvilleda81ded2008-11-12 14:37:11 -05001440static void rtl8180_bss_info_changed(struct ieee80211_hw *dev,
1441 struct ieee80211_vif *vif,
1442 struct ieee80211_bss_conf *info,
1443 u32 changed)
1444{
1445 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -04001446 struct rtl8180_vif *vif_priv;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001447 int i;
John W. Linville0f956e72010-07-29 21:50:29 -04001448 u8 reg;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001449
John W. Linvillec809e862010-05-06 16:49:40 -04001450 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
1451
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001452 if (changed & BSS_CHANGED_BSSID) {
1453 for (i = 0; i < ETH_ALEN; i++)
1454 rtl818x_iowrite8(priv, &priv->map->BSSID[i],
1455 info->bssid[i]);
1456
John W. Linville0f956e72010-07-29 21:50:29 -04001457 if (is_valid_ether_addr(info->bssid)) {
1458 if (vif->type == NL80211_IFTYPE_ADHOC)
1459 reg = RTL818X_MSR_ADHOC;
1460 else
1461 reg = RTL818X_MSR_INFRA;
1462 } else
1463 reg = RTL818X_MSR_NO_LINK;
Andrea Merello833d15a2014-03-26 21:03:17 +01001464
1465 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1466 reg |= RTL818X_MSR_ENEDCA;
1467
John W. Linville0f956e72010-07-29 21:50:29 -04001468 rtl818x_iowrite8(priv, &priv->map->MSR, reg);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001469 }
John W. Linvilleda81ded2008-11-12 14:37:11 -05001470
Andrea Merello516a0932014-03-15 18:29:36 +01001471 if (changed & BSS_CHANGED_BASIC_RATES)
1472 rtl8180_conf_basic_rates(dev, info->basic_rates);
1473
Andrea Merello9069af72014-03-15 18:29:37 +01001474 if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_ERP_PREAMBLE)) {
1475
1476 /* when preamble changes, acktime duration changes, and erp must
1477 * be recalculated. ACK time is calculated at lowest rate.
1478 * Since mac80211 include SIFS time we remove it (-10)
1479 */
1480 priv->ack_time =
1481 le16_to_cpu(ieee80211_generic_frame_duration(dev,
1482 priv->vif,
1483 IEEE80211_BAND_2GHZ, 10,
1484 &priv->rates[0])) - 10;
1485
1486 rtl8180_conf_erp(dev, info);
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001487
1488 /* mac80211 supplies aifs_n to driver and calls
1489 * conf_tx callback whether aifs_n changes, NOT
1490 * when aifs changes.
1491 * Aifs should be recalculated if slot changes.
1492 */
1493 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1494 for (i = 0; i < 4; i++)
1495 rtl8187se_conf_ac_parm(dev, i);
1496 }
Andrea Merello9069af72014-03-15 18:29:37 +01001497 }
John W. Linvillec809e862010-05-06 16:49:40 -04001498
1499 if (changed & BSS_CHANGED_BEACON_ENABLED)
1500 vif_priv->enable_beacon = info->enable_beacon;
1501
1502 if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON)) {
1503 cancel_delayed_work_sync(&vif_priv->beacon_work);
1504 if (vif_priv->enable_beacon)
1505 schedule_work(&vif_priv->beacon_work.work);
1506 }
John W. Linvilleda81ded2008-11-12 14:37:11 -05001507}
1508
Jiri Pirko22bedad32010-04-01 21:22:57 +00001509static u64 rtl8180_prepare_multicast(struct ieee80211_hw *dev,
1510 struct netdev_hw_addr_list *mc_list)
Johannes Berg3ac64be2009-08-17 16:16:53 +02001511{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001512 return netdev_hw_addr_list_count(mc_list);
Johannes Berg3ac64be2009-08-17 16:16:53 +02001513}
1514
Michael Wuf6532112007-10-14 14:43:16 -04001515static void rtl8180_configure_filter(struct ieee80211_hw *dev,
1516 unsigned int changed_flags,
1517 unsigned int *total_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001518 u64 multicast)
Michael Wuf6532112007-10-14 14:43:16 -04001519{
1520 struct rtl8180_priv *priv = dev->priv;
1521
1522 if (changed_flags & FIF_FCSFAIL)
1523 priv->rx_conf ^= RTL818X_RX_CONF_FCS;
1524 if (changed_flags & FIF_CONTROL)
1525 priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
1526 if (changed_flags & FIF_OTHER_BSS)
1527 priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
Johannes Berg3ac64be2009-08-17 16:16:53 +02001528 if (*total_flags & FIF_ALLMULTI || multicast > 0)
Michael Wuf6532112007-10-14 14:43:16 -04001529 priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
1530 else
1531 priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
1532
1533 *total_flags = 0;
1534
1535 if (priv->rx_conf & RTL818X_RX_CONF_FCS)
1536 *total_flags |= FIF_FCSFAIL;
1537 if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
1538 *total_flags |= FIF_CONTROL;
1539 if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
1540 *total_flags |= FIF_OTHER_BSS;
1541 if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
1542 *total_flags |= FIF_ALLMULTI;
1543
1544 rtl818x_iowrite32(priv, &priv->map->RX_CONF, priv->rx_conf);
1545}
1546
1547static const struct ieee80211_ops rtl8180_ops = {
1548 .tx = rtl8180_tx,
1549 .start = rtl8180_start,
1550 .stop = rtl8180_stop,
1551 .add_interface = rtl8180_add_interface,
1552 .remove_interface = rtl8180_remove_interface,
1553 .config = rtl8180_config,
John W. Linvilleda81ded2008-11-12 14:37:11 -05001554 .bss_info_changed = rtl8180_bss_info_changed,
Andrea Merello9069af72014-03-15 18:29:37 +01001555 .conf_tx = rtl8180_conf_tx,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001556 .prepare_multicast = rtl8180_prepare_multicast,
Michael Wuf6532112007-10-14 14:43:16 -04001557 .configure_filter = rtl8180_configure_filter,
John W. Linvilled2bb8e02010-01-26 16:22:20 -05001558 .get_tsf = rtl8180_get_tsf,
Michael Wuf6532112007-10-14 14:43:16 -04001559};
1560
1561static void rtl8180_eeprom_register_read(struct eeprom_93cx6 *eeprom)
1562{
Andrea Merello7d4b8292014-03-15 18:29:38 +01001563 struct rtl8180_priv *priv = eeprom->data;
Michael Wuf6532112007-10-14 14:43:16 -04001564 u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1565
1566 eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
1567 eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
1568 eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
1569 eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
1570}
1571
1572static void rtl8180_eeprom_register_write(struct eeprom_93cx6 *eeprom)
1573{
Andrea Merello7d4b8292014-03-15 18:29:38 +01001574 struct rtl8180_priv *priv = eeprom->data;
Michael Wuf6532112007-10-14 14:43:16 -04001575 u8 reg = 2 << 6;
1576
1577 if (eeprom->reg_data_in)
1578 reg |= RTL818X_EEPROM_CMD_WRITE;
1579 if (eeprom->reg_data_out)
1580 reg |= RTL818X_EEPROM_CMD_READ;
1581 if (eeprom->reg_data_clock)
1582 reg |= RTL818X_EEPROM_CMD_CK;
1583 if (eeprom->reg_chip_select)
1584 reg |= RTL818X_EEPROM_CMD_CS;
1585
1586 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
1587 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1588 udelay(10);
1589}
1590
Andrea Merello7d4b8292014-03-15 18:29:38 +01001591static void rtl8180_eeprom_read(struct rtl8180_priv *priv)
1592{
1593 struct eeprom_93cx6 eeprom;
1594 int eeprom_cck_table_adr;
1595 u16 eeprom_val;
1596 int i;
1597
1598 eeprom.data = priv;
1599 eeprom.register_read = rtl8180_eeprom_register_read;
1600 eeprom.register_write = rtl8180_eeprom_register_write;
1601 if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
1602 eeprom.width = PCI_EEPROM_WIDTH_93C66;
1603 else
1604 eeprom.width = PCI_EEPROM_WIDTH_93C46;
1605
1606 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
1607 RTL818X_EEPROM_CMD_PROGRAM);
1608 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1609 udelay(10);
1610
1611 eeprom_93cx6_read(&eeprom, 0x06, &eeprom_val);
1612 eeprom_val &= 0xFF;
1613 priv->rf_type = eeprom_val;
1614
1615 eeprom_93cx6_read(&eeprom, 0x17, &eeprom_val);
1616 priv->csthreshold = eeprom_val >> 8;
1617
1618 eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)priv->mac_addr, 3);
1619
Andrea Merellofc32ac92014-03-26 21:01:47 +01001620 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1621 eeprom_cck_table_adr = 0x30;
1622 else
1623 eeprom_cck_table_adr = 0x10;
Andrea Merello7d4b8292014-03-15 18:29:38 +01001624
1625 /* CCK TX power */
1626 for (i = 0; i < 14; i += 2) {
1627 u16 txpwr;
1628 eeprom_93cx6_read(&eeprom, eeprom_cck_table_adr + (i >> 1),
1629 &txpwr);
1630 priv->channels[i].hw_value = txpwr & 0xFF;
1631 priv->channels[i + 1].hw_value = txpwr >> 8;
1632 }
1633
1634 /* OFDM TX power */
1635 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
1636 for (i = 0; i < 14; i += 2) {
1637 u16 txpwr;
1638 eeprom_93cx6_read(&eeprom, 0x20 + (i >> 1), &txpwr);
1639 priv->channels[i].hw_value |= (txpwr & 0xFF) << 8;
1640 priv->channels[i + 1].hw_value |= txpwr & 0xFF00;
1641 }
1642 }
1643
1644 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
1645 __le32 anaparam;
1646 eeprom_93cx6_multiread(&eeprom, 0xD, (__le16 *)&anaparam, 2);
1647 priv->anaparam = le32_to_cpu(anaparam);
1648 eeprom_93cx6_read(&eeprom, 0x19, &priv->rfparam);
1649 }
1650
Andrea Merellofc32ac92014-03-26 21:01:47 +01001651 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1652 eeprom_93cx6_read(&eeprom, 0x3F, &eeprom_val);
1653 priv->antenna_diversity_en = !!(eeprom_val & 0x100);
1654 priv->antenna_diversity_default = (eeprom_val & 0xC00) == 0x400;
1655
1656 eeprom_93cx6_read(&eeprom, 0x7C, &eeprom_val);
1657 priv->xtal_out = eeprom_val & 0xF;
1658 priv->xtal_in = (eeprom_val & 0xF0) >> 4;
1659 priv->xtal_cal = !!(eeprom_val & 0x1000);
1660 priv->thermal_meter_val = (eeprom_val & 0xF00) >> 8;
1661 priv->thermal_meter_en = !!(eeprom_val & 0x2000);
1662 }
1663
Andrea Merello7d4b8292014-03-15 18:29:38 +01001664 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
1665 RTL818X_EEPROM_CMD_NORMAL);
1666}
1667
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001668static int rtl8180_probe(struct pci_dev *pdev,
Michael Wuf6532112007-10-14 14:43:16 -04001669 const struct pci_device_id *id)
1670{
1671 struct ieee80211_hw *dev;
1672 struct rtl8180_priv *priv;
1673 unsigned long mem_addr, mem_len;
1674 unsigned int io_addr, io_len;
Andrea Merello7d4b8292014-03-15 18:29:38 +01001675 int err;
Michael Wuf6532112007-10-14 14:43:16 -04001676 const char *chip_name, *rf_name = NULL;
1677 u32 reg;
Michael Wuf6532112007-10-14 14:43:16 -04001678
1679 err = pci_enable_device(pdev);
1680 if (err) {
1681 printk(KERN_ERR "%s (rtl8180): Cannot enable new PCI device\n",
1682 pci_name(pdev));
1683 return err;
1684 }
1685
1686 err = pci_request_regions(pdev, KBUILD_MODNAME);
1687 if (err) {
1688 printk(KERN_ERR "%s (rtl8180): Cannot obtain PCI resources\n",
1689 pci_name(pdev));
1690 return err;
1691 }
1692
1693 io_addr = pci_resource_start(pdev, 0);
1694 io_len = pci_resource_len(pdev, 0);
1695 mem_addr = pci_resource_start(pdev, 1);
1696 mem_len = pci_resource_len(pdev, 1);
1697
1698 if (mem_len < sizeof(struct rtl818x_csr) ||
1699 io_len < sizeof(struct rtl818x_csr)) {
1700 printk(KERN_ERR "%s (rtl8180): Too short PCI resources\n",
1701 pci_name(pdev));
1702 err = -ENOMEM;
1703 goto err_free_reg;
1704 }
1705
John W. Linville9e385c52010-05-10 14:24:34 -04001706 if ((err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) ||
1707 (err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))) {
Michael Wuf6532112007-10-14 14:43:16 -04001708 printk(KERN_ERR "%s (rtl8180): No suitable DMA available\n",
1709 pci_name(pdev));
1710 goto err_free_reg;
1711 }
1712
1713 pci_set_master(pdev);
1714
1715 dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8180_ops);
1716 if (!dev) {
1717 printk(KERN_ERR "%s (rtl8180): ieee80211 alloc failed\n",
1718 pci_name(pdev));
1719 err = -ENOMEM;
1720 goto err_free_reg;
1721 }
1722
1723 priv = dev->priv;
1724 priv->pdev = pdev;
1725
Johannes Berge6a98542008-10-21 12:40:02 +02001726 dev->max_rates = 2;
Michael Wuf6532112007-10-14 14:43:16 -04001727 SET_IEEE80211_DEV(dev, &pdev->dev);
1728 pci_set_drvdata(pdev, dev);
1729
1730 priv->map = pci_iomap(pdev, 1, mem_len);
1731 if (!priv->map)
1732 priv->map = pci_iomap(pdev, 0, io_len);
1733
1734 if (!priv->map) {
1735 printk(KERN_ERR "%s (rtl8180): Cannot map device memory\n",
1736 pci_name(pdev));
1737 goto err_free_dev;
1738 }
1739
Johannes Berg8318d782008-01-24 19:38:38 +01001740 BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
1741 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
1742
Michael Wuf6532112007-10-14 14:43:16 -04001743 memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
1744 memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
Johannes Berg8318d782008-01-24 19:38:38 +01001745
1746 priv->band.band = IEEE80211_BAND_2GHZ;
1747 priv->band.channels = priv->channels;
1748 priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
1749 priv->band.bitrates = priv->rates;
1750 priv->band.n_bitrates = 4;
1751 dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
1752
Michael Wuf6532112007-10-14 14:43:16 -04001753 dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
Bruno Randolf566bfe52008-05-08 19:15:40 +02001754 IEEE80211_HW_RX_INCLUDES_FCS |
1755 IEEE80211_HW_SIGNAL_UNSPEC;
John W. Linvillec809e862010-05-06 16:49:40 -04001756 dev->vif_data_size = sizeof(struct rtl8180_vif);
1757 dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1758 BIT(NL80211_IFTYPE_ADHOC);
Bruno Randolf566bfe52008-05-08 19:15:40 +02001759 dev->max_signal = 65;
Michael Wuf6532112007-10-14 14:43:16 -04001760
1761 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
1762 reg &= RTL818X_TX_CONF_HWVER_MASK;
1763 switch (reg) {
1764 case RTL818X_TX_CONF_R8180_ABCD:
1765 chip_name = "RTL8180";
Andrea Merello6caefd12014-03-08 18:36:37 +01001766 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
Michael Wuf6532112007-10-14 14:43:16 -04001767 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001768
Michael Wuf6532112007-10-14 14:43:16 -04001769 case RTL818X_TX_CONF_R8180_F:
1770 chip_name = "RTL8180vF";
Andrea Merello6caefd12014-03-08 18:36:37 +01001771 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
Michael Wuf6532112007-10-14 14:43:16 -04001772 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001773
Michael Wuf6532112007-10-14 14:43:16 -04001774 case RTL818X_TX_CONF_R8185_ABC:
1775 chip_name = "RTL8185";
Andrea Merello6caefd12014-03-08 18:36:37 +01001776 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
Michael Wuf6532112007-10-14 14:43:16 -04001777 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001778
Michael Wuf6532112007-10-14 14:43:16 -04001779 case RTL818X_TX_CONF_R8185_D:
1780 chip_name = "RTL8185vD";
Andrea Merello6caefd12014-03-08 18:36:37 +01001781 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
Michael Wuf6532112007-10-14 14:43:16 -04001782 break;
Andrea Merello24b5fbf2014-03-26 21:04:05 +01001783
1784 case RTL818X_TX_CONF_RTL8187SE:
1785 chip_name = "RTL8187SE";
1786 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8187SE;
1787 break;
1788
Michael Wuf6532112007-10-14 14:43:16 -04001789 default:
1790 printk(KERN_ERR "%s (rtl8180): Unknown chip! (0x%x)\n",
1791 pci_name(pdev), reg >> 25);
1792 goto err_iounmap;
1793 }
1794
Andrea Merellofd6564f2014-03-22 18:51:20 +01001795 /* we declare to MAC80211 all the queues except for beacon queue
1796 * that will be eventually handled by DRV.
1797 * TX rings are arranged in such a way that lower is the IDX,
1798 * higher is the priority, in order to achieve direct mapping
1799 * with mac80211, however the beacon queue is an exception and it
1800 * is mapped on the highst tx ring IDX.
1801 */
Andrea Merello24b5fbf2014-03-26 21:04:05 +01001802 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1803 dev->queues = RTL8187SE_NR_TX_QUEUES - 1;
1804 else
1805 dev->queues = RTL8180_NR_TX_QUEUES - 1;
Andrea Merellofd6564f2014-03-22 18:51:20 +01001806
Andrea Merello6caefd12014-03-08 18:36:37 +01001807 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Johannes Berg8318d782008-01-24 19:38:38 +01001808 priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
Michael Wuf6532112007-10-14 14:43:16 -04001809 pci_try_set_mwi(pdev);
1810 }
1811
Andrea Merello7d4b8292014-03-15 18:29:38 +01001812 rtl8180_eeprom_read(priv);
Michael Wuf6532112007-10-14 14:43:16 -04001813
Andrea Merello7d4b8292014-03-15 18:29:38 +01001814 switch (priv->rf_type) {
Michael Wuf6532112007-10-14 14:43:16 -04001815 case 1: rf_name = "Intersil";
1816 break;
1817 case 2: rf_name = "RFMD";
1818 break;
1819 case 3: priv->rf = &sa2400_rf_ops;
1820 break;
1821 case 4: priv->rf = &max2820_rf_ops;
1822 break;
1823 case 5: priv->rf = &grf5101_rf_ops;
1824 break;
Andrea Merello24b5fbf2014-03-26 21:04:05 +01001825 case 9:
1826 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1827 priv->rf = rtl8187se_detect_rf(dev);
1828 else
1829 priv->rf = rtl8180_detect_rf(dev);
Michael Wuf6532112007-10-14 14:43:16 -04001830 break;
1831 case 10:
1832 rf_name = "RTL8255";
1833 break;
1834 default:
1835 printk(KERN_ERR "%s (rtl8180): Unknown RF! (0x%x)\n",
Andrea Merello7d4b8292014-03-15 18:29:38 +01001836 pci_name(pdev), priv->rf_type);
Michael Wuf6532112007-10-14 14:43:16 -04001837 goto err_iounmap;
1838 }
1839
1840 if (!priv->rf) {
1841 printk(KERN_ERR "%s (rtl8180): %s RF frontend not supported!\n",
1842 pci_name(pdev), rf_name);
1843 goto err_iounmap;
1844 }
1845
Andrea Merello7d4b8292014-03-15 18:29:38 +01001846 if (!is_valid_ether_addr(priv->mac_addr)) {
Michael Wuf6532112007-10-14 14:43:16 -04001847 printk(KERN_WARNING "%s (rtl8180): Invalid hwaddr! Using"
1848 " randomly generated MAC addr\n", pci_name(pdev));
Andrea Merello7d4b8292014-03-15 18:29:38 +01001849 eth_random_addr(priv->mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001850 }
Andrea Merello7d4b8292014-03-15 18:29:38 +01001851 SET_IEEE80211_PERM_ADDR(dev, priv->mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001852
1853 spin_lock_init(&priv->lock);
1854
1855 err = ieee80211_register_hw(dev);
1856 if (err) {
1857 printk(KERN_ERR "%s (rtl8180): Cannot register device\n",
1858 pci_name(pdev));
1859 goto err_iounmap;
1860 }
1861
Joe Perchesc96c31e2010-07-26 14:39:58 -07001862 wiphy_info(dev->wiphy, "hwaddr %pm, %s + %s\n",
Andrea Merello7d4b8292014-03-15 18:29:38 +01001863 priv->mac_addr, chip_name, priv->rf->name);
Michael Wuf6532112007-10-14 14:43:16 -04001864
1865 return 0;
1866
1867 err_iounmap:
andrea merello0269da22014-02-18 02:10:41 +01001868 pci_iounmap(pdev, priv->map);
Michael Wuf6532112007-10-14 14:43:16 -04001869
1870 err_free_dev:
Michael Wuf6532112007-10-14 14:43:16 -04001871 ieee80211_free_hw(dev);
1872
1873 err_free_reg:
1874 pci_release_regions(pdev);
1875 pci_disable_device(pdev);
1876 return err;
1877}
1878
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001879static void rtl8180_remove(struct pci_dev *pdev)
Michael Wuf6532112007-10-14 14:43:16 -04001880{
1881 struct ieee80211_hw *dev = pci_get_drvdata(pdev);
1882 struct rtl8180_priv *priv;
1883
1884 if (!dev)
1885 return;
1886
1887 ieee80211_unregister_hw(dev);
1888
1889 priv = dev->priv;
1890
1891 pci_iounmap(pdev, priv->map);
1892 pci_release_regions(pdev);
1893 pci_disable_device(pdev);
1894 ieee80211_free_hw(dev);
1895}
1896
1897#ifdef CONFIG_PM
1898static int rtl8180_suspend(struct pci_dev *pdev, pm_message_t state)
1899{
1900 pci_save_state(pdev);
1901 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1902 return 0;
1903}
1904
1905static int rtl8180_resume(struct pci_dev *pdev)
1906{
1907 pci_set_power_state(pdev, PCI_D0);
1908 pci_restore_state(pdev);
1909 return 0;
1910}
1911
1912#endif /* CONFIG_PM */
1913
1914static struct pci_driver rtl8180_driver = {
1915 .name = KBUILD_MODNAME,
1916 .id_table = rtl8180_table,
1917 .probe = rtl8180_probe,
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001918 .remove = rtl8180_remove,
Michael Wuf6532112007-10-14 14:43:16 -04001919#ifdef CONFIG_PM
1920 .suspend = rtl8180_suspend,
1921 .resume = rtl8180_resume,
1922#endif /* CONFIG_PM */
1923};
1924
Axel Lin5b0a3b72012-04-14 10:38:36 +08001925module_pci_driver(rtl8180_driver);