3 * Linux device driver for RTL8180 / RTL8185
5 * Copyright 2007 Michael Wu <flamingice@sourmilk.net>
6 * Copyright 2007 Andrea Merello <andreamrl@tiscali.it>
8 * Based on the r8180 driver, which is:
9 * Copyright 2004-2005 Andrea Merello <andreamrl@tiscali.it>, et al.
11 * Thanks to Realtek for their support!
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/pci.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/etherdevice.h>
24 #include <linux/eeprom_93cx6.h>
25 #include <net/mac80211.h>
33 MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
34 MODULE_AUTHOR("Andrea Merello <andreamrl@tiscali.it>");
35 MODULE_DESCRIPTION("RTL8180 / RTL8185 PCI wireless driver");
36 MODULE_LICENSE("GPL");
38 static DEFINE_PCI_DEVICE_TABLE(rtl8180_table
) = {
40 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK
, 0x8185) },
41 { PCI_DEVICE(PCI_VENDOR_ID_BELKIN
, 0x700f) },
42 { PCI_DEVICE(PCI_VENDOR_ID_BELKIN
, 0x701f) },
45 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK
, 0x8180) },
46 { PCI_DEVICE(0x1799, 0x6001) },
47 { PCI_DEVICE(0x1799, 0x6020) },
48 { PCI_DEVICE(PCI_VENDOR_ID_DLINK
, 0x3300) },
52 MODULE_DEVICE_TABLE(pci
, rtl8180_table
);
54 static const struct ieee80211_rate rtl818x_rates
[] = {
55 { .bitrate
= 10, .hw_value
= 0, },
56 { .bitrate
= 20, .hw_value
= 1, },
57 { .bitrate
= 55, .hw_value
= 2, },
58 { .bitrate
= 110, .hw_value
= 3, },
59 { .bitrate
= 60, .hw_value
= 4, },
60 { .bitrate
= 90, .hw_value
= 5, },
61 { .bitrate
= 120, .hw_value
= 6, },
62 { .bitrate
= 180, .hw_value
= 7, },
63 { .bitrate
= 240, .hw_value
= 8, },
64 { .bitrate
= 360, .hw_value
= 9, },
65 { .bitrate
= 480, .hw_value
= 10, },
66 { .bitrate
= 540, .hw_value
= 11, },
69 static const struct ieee80211_channel rtl818x_channels
[] = {
70 { .center_freq
= 2412 },
71 { .center_freq
= 2417 },
72 { .center_freq
= 2422 },
73 { .center_freq
= 2427 },
74 { .center_freq
= 2432 },
75 { .center_freq
= 2437 },
76 { .center_freq
= 2442 },
77 { .center_freq
= 2447 },
78 { .center_freq
= 2452 },
79 { .center_freq
= 2457 },
80 { .center_freq
= 2462 },
81 { .center_freq
= 2467 },
82 { .center_freq
= 2472 },
83 { .center_freq
= 2484 },
87 void rtl8180_write_phy(struct ieee80211_hw
*dev
, u8 addr
, u32 data
)
89 struct rtl8180_priv
*priv
= dev
->priv
;
93 buf
= (data
<< 8) | addr
;
95 rtl818x_iowrite32(priv
, (__le32 __iomem
*)&priv
->map
->PHY
[0], buf
| 0x80);
97 rtl818x_iowrite32(priv
, (__le32 __iomem
*)&priv
->map
->PHY
[0], buf
);
98 if (rtl818x_ioread8(priv
, &priv
->map
->PHY
[2]) == (data
& 0xFF))
103 static void rtl8180_handle_rx(struct ieee80211_hw
*dev
)
105 struct rtl8180_priv
*priv
= dev
->priv
;
106 unsigned int count
= 32;
110 struct rtl8180_rx_desc
*entry
= &priv
->rx_ring
[priv
->rx_idx
];
111 struct sk_buff
*skb
= priv
->rx_buf
[priv
->rx_idx
];
112 u32 flags
= le32_to_cpu(entry
->flags
);
114 if (flags
& RTL818X_RX_DESC_FLAG_OWN
)
117 if (unlikely(flags
& (RTL818X_RX_DESC_FLAG_DMA_FAIL
|
118 RTL818X_RX_DESC_FLAG_FOF
|
119 RTL818X_RX_DESC_FLAG_RX_ERR
)))
122 u32 flags2
= le32_to_cpu(entry
->flags2
);
123 struct ieee80211_rx_status rx_status
= {0};
124 struct sk_buff
*new_skb
= dev_alloc_skb(MAX_RX_SIZE
);
126 if (unlikely(!new_skb
))
129 pci_unmap_single(priv
->pdev
,
130 *((dma_addr_t
*)skb
->cb
),
131 MAX_RX_SIZE
, PCI_DMA_FROMDEVICE
);
132 skb_put(skb
, flags
& 0xFFF);
134 rx_status
.antenna
= (flags2
>> 15) & 1;
135 rx_status
.rate_idx
= (flags
>> 20) & 0xF;
136 agc
= (flags2
>> 17) & 0x7F;
138 if (rx_status
.rate_idx
> 3)
139 signal
= 90 - clamp_t(u8
, agc
, 25, 90);
141 signal
= 95 - clamp_t(u8
, agc
, 30, 95);
144 signal
= priv
->rf
->calc_rssi(agc
, sq
);
146 rx_status
.signal
= signal
;
147 rx_status
.freq
= dev
->conf
.channel
->center_freq
;
148 rx_status
.band
= dev
->conf
.channel
->band
;
149 rx_status
.mactime
= le64_to_cpu(entry
->tsft
);
150 rx_status
.flag
|= RX_FLAG_MACTIME_MPDU
;
151 if (flags
& RTL818X_RX_DESC_FLAG_CRC32_ERR
)
152 rx_status
.flag
|= RX_FLAG_FAILED_FCS_CRC
;
154 memcpy(IEEE80211_SKB_RXCB(skb
), &rx_status
, sizeof(rx_status
));
155 ieee80211_rx_irqsafe(dev
, skb
);
158 priv
->rx_buf
[priv
->rx_idx
] = skb
;
159 *((dma_addr_t
*) skb
->cb
) =
160 pci_map_single(priv
->pdev
, skb_tail_pointer(skb
),
161 MAX_RX_SIZE
, PCI_DMA_FROMDEVICE
);
165 entry
->rx_buf
= cpu_to_le32(*((dma_addr_t
*)skb
->cb
));
166 entry
->flags
= cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN
|
168 if (priv
->rx_idx
== 31)
169 entry
->flags
|= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR
);
170 priv
->rx_idx
= (priv
->rx_idx
+ 1) % 32;
174 static void rtl8180_handle_tx(struct ieee80211_hw
*dev
, unsigned int prio
)
176 struct rtl8180_priv
*priv
= dev
->priv
;
177 struct rtl8180_tx_ring
*ring
= &priv
->tx_ring
[prio
];
179 while (skb_queue_len(&ring
->queue
)) {
180 struct rtl8180_tx_desc
*entry
= &ring
->desc
[ring
->idx
];
182 struct ieee80211_tx_info
*info
;
183 u32 flags
= le32_to_cpu(entry
->flags
);
185 if (flags
& RTL818X_TX_DESC_FLAG_OWN
)
188 ring
->idx
= (ring
->idx
+ 1) % ring
->entries
;
189 skb
= __skb_dequeue(&ring
->queue
);
190 pci_unmap_single(priv
->pdev
, le32_to_cpu(entry
->tx_buf
),
191 skb
->len
, PCI_DMA_TODEVICE
);
193 info
= IEEE80211_SKB_CB(skb
);
194 ieee80211_tx_info_clear_status(info
);
196 if (!(info
->flags
& IEEE80211_TX_CTL_NO_ACK
) &&
197 (flags
& RTL818X_TX_DESC_FLAG_TX_OK
))
198 info
->flags
|= IEEE80211_TX_STAT_ACK
;
200 info
->status
.rates
[0].count
= (flags
& 0xFF) + 1;
201 info
->status
.rates
[1].idx
= -1;
203 ieee80211_tx_status_irqsafe(dev
, skb
);
204 if (ring
->entries
- skb_queue_len(&ring
->queue
) == 2)
205 ieee80211_wake_queue(dev
, prio
);
209 static irqreturn_t
rtl8180_interrupt(int irq
, void *dev_id
)
211 struct ieee80211_hw
*dev
= dev_id
;
212 struct rtl8180_priv
*priv
= dev
->priv
;
215 spin_lock(&priv
->lock
);
216 reg
= rtl818x_ioread16(priv
, &priv
->map
->INT_STATUS
);
217 if (unlikely(reg
== 0xFFFF)) {
218 spin_unlock(&priv
->lock
);
222 rtl818x_iowrite16(priv
, &priv
->map
->INT_STATUS
, reg
);
224 if (reg
& (RTL818X_INT_TXB_OK
| RTL818X_INT_TXB_ERR
))
225 rtl8180_handle_tx(dev
, 3);
227 if (reg
& (RTL818X_INT_TXH_OK
| RTL818X_INT_TXH_ERR
))
228 rtl8180_handle_tx(dev
, 2);
230 if (reg
& (RTL818X_INT_TXN_OK
| RTL818X_INT_TXN_ERR
))
231 rtl8180_handle_tx(dev
, 1);
233 if (reg
& (RTL818X_INT_TXL_OK
| RTL818X_INT_TXL_ERR
))
234 rtl8180_handle_tx(dev
, 0);
236 if (reg
& (RTL818X_INT_RX_OK
| RTL818X_INT_RX_ERR
))
237 rtl8180_handle_rx(dev
);
239 spin_unlock(&priv
->lock
);
244 static void rtl8180_tx(struct ieee80211_hw
*dev
, struct sk_buff
*skb
)
246 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
247 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)skb
->data
;
248 struct rtl8180_priv
*priv
= dev
->priv
;
249 struct rtl8180_tx_ring
*ring
;
250 struct rtl8180_tx_desc
*entry
;
252 unsigned int idx
, prio
;
257 __le16 rts_duration
= 0;
259 prio
= skb_get_queue_mapping(skb
);
260 ring
= &priv
->tx_ring
[prio
];
262 mapping
= pci_map_single(priv
->pdev
, skb
->data
,
263 skb
->len
, PCI_DMA_TODEVICE
);
265 tx_flags
= RTL818X_TX_DESC_FLAG_OWN
| RTL818X_TX_DESC_FLAG_FS
|
266 RTL818X_TX_DESC_FLAG_LS
|
267 (ieee80211_get_tx_rate(dev
, info
)->hw_value
<< 24) |
271 tx_flags
|= RTL818X_TX_DESC_FLAG_DMA
|
272 RTL818X_TX_DESC_FLAG_NO_ENC
;
274 rc_flags
= info
->control
.rates
[0].flags
;
275 if (rc_flags
& IEEE80211_TX_RC_USE_RTS_CTS
) {
276 tx_flags
|= RTL818X_TX_DESC_FLAG_RTS
;
277 tx_flags
|= ieee80211_get_rts_cts_rate(dev
, info
)->hw_value
<< 19;
278 } else if (rc_flags
& IEEE80211_TX_RC_USE_CTS_PROTECT
) {
279 tx_flags
|= RTL818X_TX_DESC_FLAG_CTS
;
280 tx_flags
|= ieee80211_get_rts_cts_rate(dev
, info
)->hw_value
<< 19;
283 if (rc_flags
& IEEE80211_TX_RC_USE_RTS_CTS
)
284 rts_duration
= ieee80211_rts_duration(dev
, priv
->vif
, skb
->len
,
288 unsigned int remainder
;
290 plcp_len
= DIV_ROUND_UP(16 * (skb
->len
+ 4),
291 (ieee80211_get_tx_rate(dev
, info
)->bitrate
* 2) / 10);
292 remainder
= (16 * (skb
->len
+ 4)) %
293 ((ieee80211_get_tx_rate(dev
, info
)->bitrate
* 2) / 10);
298 spin_lock_irqsave(&priv
->lock
, flags
);
300 if (info
->flags
& IEEE80211_TX_CTL_ASSIGN_SEQ
) {
301 if (info
->flags
& IEEE80211_TX_CTL_FIRST_FRAGMENT
)
303 hdr
->seq_ctrl
&= cpu_to_le16(IEEE80211_SCTL_FRAG
);
304 hdr
->seq_ctrl
|= cpu_to_le16(priv
->seqno
);
307 idx
= (ring
->idx
+ skb_queue_len(&ring
->queue
)) % ring
->entries
;
308 entry
= &ring
->desc
[idx
];
310 entry
->rts_duration
= rts_duration
;
311 entry
->plcp_len
= cpu_to_le16(plcp_len
);
312 entry
->tx_buf
= cpu_to_le32(mapping
);
313 entry
->frame_len
= cpu_to_le32(skb
->len
);
314 entry
->flags2
= info
->control
.rates
[1].idx
>= 0 ?
315 ieee80211_get_alt_retry_rate(dev
, info
, 0)->bitrate
<< 4 : 0;
316 entry
->retry_limit
= info
->control
.rates
[0].count
;
317 entry
->flags
= cpu_to_le32(tx_flags
);
318 __skb_queue_tail(&ring
->queue
, skb
);
319 if (ring
->entries
- skb_queue_len(&ring
->queue
) < 2)
320 ieee80211_stop_queue(dev
, prio
);
322 spin_unlock_irqrestore(&priv
->lock
, flags
);
324 rtl818x_iowrite8(priv
, &priv
->map
->TX_DMA_POLLING
, (1 << (prio
+ 4)));
327 void rtl8180_set_anaparam(struct rtl8180_priv
*priv
, u32 anaparam
)
331 rtl818x_iowrite8(priv
, &priv
->map
->EEPROM_CMD
, RTL818X_EEPROM_CMD_CONFIG
);
332 reg
= rtl818x_ioread8(priv
, &priv
->map
->CONFIG3
);
333 rtl818x_iowrite8(priv
, &priv
->map
->CONFIG3
,
334 reg
| RTL818X_CONFIG3_ANAPARAM_WRITE
);
335 rtl818x_iowrite32(priv
, &priv
->map
->ANAPARAM
, anaparam
);
336 rtl818x_iowrite8(priv
, &priv
->map
->CONFIG3
,
337 reg
& ~RTL818X_CONFIG3_ANAPARAM_WRITE
);
338 rtl818x_iowrite8(priv
, &priv
->map
->EEPROM_CMD
, RTL818X_EEPROM_CMD_NORMAL
);
341 static int rtl8180_init_hw(struct ieee80211_hw
*dev
)
343 struct rtl8180_priv
*priv
= dev
->priv
;
346 rtl818x_iowrite8(priv
, &priv
->map
->CMD
, 0);
347 rtl818x_ioread8(priv
, &priv
->map
->CMD
);
351 rtl818x_iowrite16(priv
, &priv
->map
->INT_MASK
, 0);
352 rtl818x_ioread8(priv
, &priv
->map
->CMD
);
354 reg
= rtl818x_ioread8(priv
, &priv
->map
->CMD
);
356 reg
|= RTL818X_CMD_RESET
;
357 rtl818x_iowrite8(priv
, &priv
->map
->CMD
, RTL818X_CMD_RESET
);
358 rtl818x_ioread8(priv
, &priv
->map
->CMD
);
361 /* check success of reset */
362 if (rtl818x_ioread8(priv
, &priv
->map
->CMD
) & RTL818X_CMD_RESET
) {
363 wiphy_err(dev
->wiphy
, "reset timeout!\n");
367 rtl818x_iowrite8(priv
, &priv
->map
->EEPROM_CMD
, RTL818X_EEPROM_CMD_LOAD
);
368 rtl818x_ioread8(priv
, &priv
->map
->CMD
);
371 if (rtl818x_ioread8(priv
, &priv
->map
->CONFIG3
) & (1 << 3)) {
373 reg
= rtl818x_ioread8(priv
, &priv
->map
->CONFIG3
);
375 rtl818x_iowrite8(priv
, &priv
->map
->CONFIG3
, reg
);
376 reg
= rtl818x_ioread16(priv
, &priv
->map
->FEMR
);
377 reg
|= (1 << 15) | (1 << 14) | (1 << 4);
378 rtl818x_iowrite16(priv
, &priv
->map
->FEMR
, reg
);
381 rtl818x_iowrite8(priv
, &priv
->map
->MSR
, 0);
384 rtl8180_set_anaparam(priv
, priv
->anaparam
);
386 rtl818x_iowrite32(priv
, &priv
->map
->RDSAR
, priv
->rx_ring_dma
);
387 rtl818x_iowrite32(priv
, &priv
->map
->TBDA
, priv
->tx_ring
[3].dma
);
388 rtl818x_iowrite32(priv
, &priv
->map
->THPDA
, priv
->tx_ring
[2].dma
);
389 rtl818x_iowrite32(priv
, &priv
->map
->TNPDA
, priv
->tx_ring
[1].dma
);
390 rtl818x_iowrite32(priv
, &priv
->map
->TLPDA
, priv
->tx_ring
[0].dma
);
392 /* TODO: necessary? specs indicate not */
393 rtl818x_iowrite8(priv
, &priv
->map
->EEPROM_CMD
, RTL818X_EEPROM_CMD_CONFIG
);
394 reg
= rtl818x_ioread8(priv
, &priv
->map
->CONFIG2
);
395 rtl818x_iowrite8(priv
, &priv
->map
->CONFIG2
, reg
& ~(1 << 3));
397 reg
= rtl818x_ioread8(priv
, &priv
->map
->CONFIG2
);
398 rtl818x_iowrite8(priv
, &priv
->map
->CONFIG2
, reg
| (1 << 4));
400 rtl818x_iowrite8(priv
, &priv
->map
->EEPROM_CMD
, RTL818X_EEPROM_CMD_NORMAL
);
402 /* TODO: set CONFIG5 for calibrating AGC on rtl8180 + philips radio? */
404 /* TODO: turn off hw wep on rtl8180 */
406 rtl818x_iowrite32(priv
, &priv
->map
->INT_TIMEOUT
, 0);
409 rtl818x_iowrite8(priv
, &priv
->map
->WPA_CONF
, 0);
410 rtl818x_iowrite8(priv
, &priv
->map
->RATE_FALLBACK
, 0x81);
411 rtl818x_iowrite8(priv
, &priv
->map
->RESP_RATE
, (8 << 4) | 0);
413 rtl818x_iowrite16(priv
, &priv
->map
->BRSR
, 0x01F3);
415 /* TODO: set ClkRun enable? necessary? */
416 reg
= rtl818x_ioread8(priv
, &priv
->map
->GP_ENABLE
);
417 rtl818x_iowrite8(priv
, &priv
->map
->GP_ENABLE
, reg
& ~(1 << 6));
418 rtl818x_iowrite8(priv
, &priv
->map
->EEPROM_CMD
, RTL818X_EEPROM_CMD_CONFIG
);
419 reg
= rtl818x_ioread8(priv
, &priv
->map
->CONFIG3
);
420 rtl818x_iowrite8(priv
, &priv
->map
->CONFIG3
, reg
| (1 << 2));
421 rtl818x_iowrite8(priv
, &priv
->map
->EEPROM_CMD
, RTL818X_EEPROM_CMD_NORMAL
);
423 rtl818x_iowrite16(priv
, &priv
->map
->BRSR
, 0x1);
424 rtl818x_iowrite8(priv
, &priv
->map
->SECURITY
, 0);
426 rtl818x_iowrite8(priv
, &priv
->map
->PHY_DELAY
, 0x6);
427 rtl818x_iowrite8(priv
, &priv
->map
->CARRIER_SENSE_COUNTER
, 0x4C);
432 rtl818x_iowrite16(priv
, &priv
->map
->BRSR
, 0x01F3);
436 static int rtl8180_init_rx_ring(struct ieee80211_hw
*dev
)
438 struct rtl8180_priv
*priv
= dev
->priv
;
439 struct rtl8180_rx_desc
*entry
;
442 priv
->rx_ring
= pci_alloc_consistent(priv
->pdev
,
443 sizeof(*priv
->rx_ring
) * 32,
446 if (!priv
->rx_ring
|| (unsigned long)priv
->rx_ring
& 0xFF) {
447 wiphy_err(dev
->wiphy
, "Cannot allocate RX ring\n");
451 memset(priv
->rx_ring
, 0, sizeof(*priv
->rx_ring
) * 32);
454 for (i
= 0; i
< 32; i
++) {
455 struct sk_buff
*skb
= dev_alloc_skb(MAX_RX_SIZE
);
457 entry
= &priv
->rx_ring
[i
];
461 priv
->rx_buf
[i
] = skb
;
462 mapping
= (dma_addr_t
*)skb
->cb
;
463 *mapping
= pci_map_single(priv
->pdev
, skb_tail_pointer(skb
),
464 MAX_RX_SIZE
, PCI_DMA_FROMDEVICE
);
465 entry
->rx_buf
= cpu_to_le32(*mapping
);
466 entry
->flags
= cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN
|
469 entry
->flags
|= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR
);
473 static void rtl8180_free_rx_ring(struct ieee80211_hw
*dev
)
475 struct rtl8180_priv
*priv
= dev
->priv
;
478 for (i
= 0; i
< 32; i
++) {
479 struct sk_buff
*skb
= priv
->rx_buf
[i
];
483 pci_unmap_single(priv
->pdev
,
484 *((dma_addr_t
*)skb
->cb
),
485 MAX_RX_SIZE
, PCI_DMA_FROMDEVICE
);
489 pci_free_consistent(priv
->pdev
, sizeof(*priv
->rx_ring
) * 32,
490 priv
->rx_ring
, priv
->rx_ring_dma
);
491 priv
->rx_ring
= NULL
;
494 static int rtl8180_init_tx_ring(struct ieee80211_hw
*dev
,
495 unsigned int prio
, unsigned int entries
)
497 struct rtl8180_priv
*priv
= dev
->priv
;
498 struct rtl8180_tx_desc
*ring
;
502 ring
= pci_alloc_consistent(priv
->pdev
, sizeof(*ring
) * entries
, &dma
);
503 if (!ring
|| (unsigned long)ring
& 0xFF) {
504 wiphy_err(dev
->wiphy
, "Cannot allocate TX ring (prio = %d)\n",
509 memset(ring
, 0, sizeof(*ring
)*entries
);
510 priv
->tx_ring
[prio
].desc
= ring
;
511 priv
->tx_ring
[prio
].dma
= dma
;
512 priv
->tx_ring
[prio
].idx
= 0;
513 priv
->tx_ring
[prio
].entries
= entries
;
514 skb_queue_head_init(&priv
->tx_ring
[prio
].queue
);
516 for (i
= 0; i
< entries
; i
++)
517 ring
[i
].next_tx_desc
=
518 cpu_to_le32((u32
)dma
+ ((i
+ 1) % entries
) * sizeof(*ring
));
523 static void rtl8180_free_tx_ring(struct ieee80211_hw
*dev
, unsigned int prio
)
525 struct rtl8180_priv
*priv
= dev
->priv
;
526 struct rtl8180_tx_ring
*ring
= &priv
->tx_ring
[prio
];
528 while (skb_queue_len(&ring
->queue
)) {
529 struct rtl8180_tx_desc
*entry
= &ring
->desc
[ring
->idx
];
530 struct sk_buff
*skb
= __skb_dequeue(&ring
->queue
);
532 pci_unmap_single(priv
->pdev
, le32_to_cpu(entry
->tx_buf
),
533 skb
->len
, PCI_DMA_TODEVICE
);
535 ring
->idx
= (ring
->idx
+ 1) % ring
->entries
;
538 pci_free_consistent(priv
->pdev
, sizeof(*ring
->desc
)*ring
->entries
,
539 ring
->desc
, ring
->dma
);
543 static int rtl8180_start(struct ieee80211_hw
*dev
)
545 struct rtl8180_priv
*priv
= dev
->priv
;
549 ret
= rtl8180_init_rx_ring(dev
);
553 for (i
= 0; i
< 4; i
++)
554 if ((ret
= rtl8180_init_tx_ring(dev
, i
, 16)))
557 ret
= rtl8180_init_hw(dev
);
561 rtl818x_iowrite32(priv
, &priv
->map
->RDSAR
, priv
->rx_ring_dma
);
562 rtl818x_iowrite32(priv
, &priv
->map
->TBDA
, priv
->tx_ring
[3].dma
);
563 rtl818x_iowrite32(priv
, &priv
->map
->THPDA
, priv
->tx_ring
[2].dma
);
564 rtl818x_iowrite32(priv
, &priv
->map
->TNPDA
, priv
->tx_ring
[1].dma
);
565 rtl818x_iowrite32(priv
, &priv
->map
->TLPDA
, priv
->tx_ring
[0].dma
);
567 ret
= request_irq(priv
->pdev
->irq
, rtl8180_interrupt
,
568 IRQF_SHARED
, KBUILD_MODNAME
, dev
);
570 wiphy_err(dev
->wiphy
, "failed to register IRQ handler\n");
574 rtl818x_iowrite16(priv
, &priv
->map
->INT_MASK
, 0xFFFF);
576 rtl818x_iowrite32(priv
, &priv
->map
->MAR
[0], ~0);
577 rtl818x_iowrite32(priv
, &priv
->map
->MAR
[1], ~0);
579 reg
= RTL818X_RX_CONF_ONLYERLPKT
|
580 RTL818X_RX_CONF_RX_AUTORESETPHY
|
581 RTL818X_RX_CONF_MGMT
|
582 RTL818X_RX_CONF_DATA
|
583 (7 << 8 /* MAX RX DMA */) |
584 RTL818X_RX_CONF_BROADCAST
|
585 RTL818X_RX_CONF_NICMAC
;
588 reg
|= RTL818X_RX_CONF_CSDM1
| RTL818X_RX_CONF_CSDM2
;
590 reg
|= (priv
->rfparam
& RF_PARAM_CARRIERSENSE1
)
591 ? RTL818X_RX_CONF_CSDM1
: 0;
592 reg
|= (priv
->rfparam
& RF_PARAM_CARRIERSENSE2
)
593 ? RTL818X_RX_CONF_CSDM2
: 0;
597 rtl818x_iowrite32(priv
, &priv
->map
->RX_CONF
, reg
);
600 reg
= rtl818x_ioread8(priv
, &priv
->map
->CW_CONF
);
601 reg
&= ~RTL818X_CW_CONF_PERPACKET_CW_SHIFT
;
602 reg
|= RTL818X_CW_CONF_PERPACKET_RETRY_SHIFT
;
603 rtl818x_iowrite8(priv
, &priv
->map
->CW_CONF
, reg
);
605 reg
= rtl818x_ioread8(priv
, &priv
->map
->TX_AGC_CTL
);
606 reg
&= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN_SHIFT
;
607 reg
&= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT
;
608 reg
|= RTL818X_TX_AGC_CTL_FEEDBACK_ANT
;
609 rtl818x_iowrite8(priv
, &priv
->map
->TX_AGC_CTL
, reg
);
611 /* disable early TX */
612 rtl818x_iowrite8(priv
, (u8 __iomem
*)priv
->map
+ 0xec, 0x3f);
615 reg
= rtl818x_ioread32(priv
, &priv
->map
->TX_CONF
);
616 reg
|= (6 << 21 /* MAX TX DMA */) |
617 RTL818X_TX_CONF_NO_ICV
;
620 reg
&= ~RTL818X_TX_CONF_PROBE_DTS
;
622 reg
&= ~RTL818X_TX_CONF_HW_SEQNUM
;
624 /* different meaning, same value on both rtl8185 and rtl8180 */
625 reg
&= ~RTL818X_TX_CONF_SAT_HWPLCP
;
627 rtl818x_iowrite32(priv
, &priv
->map
->TX_CONF
, reg
);
629 reg
= rtl818x_ioread8(priv
, &priv
->map
->CMD
);
630 reg
|= RTL818X_CMD_RX_ENABLE
;
631 reg
|= RTL818X_CMD_TX_ENABLE
;
632 rtl818x_iowrite8(priv
, &priv
->map
->CMD
, reg
);
637 rtl8180_free_rx_ring(dev
);
638 for (i
= 0; i
< 4; i
++)
639 if (priv
->tx_ring
[i
].desc
)
640 rtl8180_free_tx_ring(dev
, i
);
645 static void rtl8180_stop(struct ieee80211_hw
*dev
)
647 struct rtl8180_priv
*priv
= dev
->priv
;
651 rtl818x_iowrite16(priv
, &priv
->map
->INT_MASK
, 0);
653 reg
= rtl818x_ioread8(priv
, &priv
->map
->CMD
);
654 reg
&= ~RTL818X_CMD_TX_ENABLE
;
655 reg
&= ~RTL818X_CMD_RX_ENABLE
;
656 rtl818x_iowrite8(priv
, &priv
->map
->CMD
, reg
);
660 rtl818x_iowrite8(priv
, &priv
->map
->EEPROM_CMD
, RTL818X_EEPROM_CMD_CONFIG
);
661 reg
= rtl818x_ioread8(priv
, &priv
->map
->CONFIG4
);
662 rtl818x_iowrite8(priv
, &priv
->map
->CONFIG4
, reg
| RTL818X_CONFIG4_VCOOFF
);
663 rtl818x_iowrite8(priv
, &priv
->map
->EEPROM_CMD
, RTL818X_EEPROM_CMD_NORMAL
);
665 free_irq(priv
->pdev
->irq
, dev
);
667 rtl8180_free_rx_ring(dev
);
668 for (i
= 0; i
< 4; i
++)
669 rtl8180_free_tx_ring(dev
, i
);
672 static u64
rtl8180_get_tsf(struct ieee80211_hw
*dev
,
673 struct ieee80211_vif
*vif
)
675 struct rtl8180_priv
*priv
= dev
->priv
;
677 return rtl818x_ioread32(priv
, &priv
->map
->TSFT
[0]) |
678 (u64
)(rtl818x_ioread32(priv
, &priv
->map
->TSFT
[1])) << 32;
681 static void rtl8180_beacon_work(struct work_struct
*work
)
683 struct rtl8180_vif
*vif_priv
=
684 container_of(work
, struct rtl8180_vif
, beacon_work
.work
);
685 struct ieee80211_vif
*vif
=
686 container_of((void *)vif_priv
, struct ieee80211_vif
, drv_priv
);
687 struct ieee80211_hw
*dev
= vif_priv
->dev
;
688 struct ieee80211_mgmt
*mgmt
;
691 /* don't overflow the tx ring */
692 if (ieee80211_queue_stopped(dev
, 0))
695 /* grab a fresh beacon */
696 skb
= ieee80211_beacon_get(dev
, vif
);
701 * update beacon timestamp w/ TSF value
702 * TODO: make hardware update beacon timestamp
704 mgmt
= (struct ieee80211_mgmt
*)skb
->data
;
705 mgmt
->u
.beacon
.timestamp
= cpu_to_le64(rtl8180_get_tsf(dev
, vif
));
707 /* TODO: use actual beacon queue */
708 skb_set_queue_mapping(skb
, 0);
710 rtl8180_tx(dev
, skb
);
714 * schedule next beacon
715 * TODO: use hardware support for beacon timing
717 schedule_delayed_work(&vif_priv
->beacon_work
,
718 usecs_to_jiffies(1024 * vif
->bss_conf
.beacon_int
));
721 static int rtl8180_add_interface(struct ieee80211_hw
*dev
,
722 struct ieee80211_vif
*vif
)
724 struct rtl8180_priv
*priv
= dev
->priv
;
725 struct rtl8180_vif
*vif_priv
;
728 * We only support one active interface at a time.
734 case NL80211_IFTYPE_STATION
:
735 case NL80211_IFTYPE_ADHOC
:
743 /* Initialize driver private area */
744 vif_priv
= (struct rtl8180_vif
*)&vif
->drv_priv
;
746 INIT_DELAYED_WORK(&vif_priv
->beacon_work
, rtl8180_beacon_work
);
747 vif_priv
->enable_beacon
= false;
749 rtl818x_iowrite8(priv
, &priv
->map
->EEPROM_CMD
, RTL818X_EEPROM_CMD_CONFIG
);
750 rtl818x_iowrite32(priv
, (__le32 __iomem
*)&priv
->map
->MAC
[0],
751 le32_to_cpu(*(__le32
*)vif
->addr
));
752 rtl818x_iowrite16(priv
, (__le16 __iomem
*)&priv
->map
->MAC
[4],
753 le16_to_cpu(*(__le16
*)(vif
->addr
+ 4)));
754 rtl818x_iowrite8(priv
, &priv
->map
->EEPROM_CMD
, RTL818X_EEPROM_CMD_NORMAL
);
759 static void rtl8180_remove_interface(struct ieee80211_hw
*dev
,
760 struct ieee80211_vif
*vif
)
762 struct rtl8180_priv
*priv
= dev
->priv
;
766 static int rtl8180_config(struct ieee80211_hw
*dev
, u32 changed
)
768 struct rtl8180_priv
*priv
= dev
->priv
;
769 struct ieee80211_conf
*conf
= &dev
->conf
;
771 priv
->rf
->set_chan(dev
, conf
);
776 static void rtl8180_bss_info_changed(struct ieee80211_hw
*dev
,
777 struct ieee80211_vif
*vif
,
778 struct ieee80211_bss_conf
*info
,
781 struct rtl8180_priv
*priv
= dev
->priv
;
782 struct rtl8180_vif
*vif_priv
;
786 vif_priv
= (struct rtl8180_vif
*)&vif
->drv_priv
;
788 if (changed
& BSS_CHANGED_BSSID
) {
789 for (i
= 0; i
< ETH_ALEN
; i
++)
790 rtl818x_iowrite8(priv
, &priv
->map
->BSSID
[i
],
793 if (is_valid_ether_addr(info
->bssid
)) {
794 if (vif
->type
== NL80211_IFTYPE_ADHOC
)
795 reg
= RTL818X_MSR_ADHOC
;
797 reg
= RTL818X_MSR_INFRA
;
799 reg
= RTL818X_MSR_NO_LINK
;
800 rtl818x_iowrite8(priv
, &priv
->map
->MSR
, reg
);
803 if (changed
& BSS_CHANGED_ERP_SLOT
&& priv
->rf
->conf_erp
)
804 priv
->rf
->conf_erp(dev
, info
);
806 if (changed
& BSS_CHANGED_BEACON_ENABLED
)
807 vif_priv
->enable_beacon
= info
->enable_beacon
;
809 if (changed
& (BSS_CHANGED_BEACON_ENABLED
| BSS_CHANGED_BEACON
)) {
810 cancel_delayed_work_sync(&vif_priv
->beacon_work
);
811 if (vif_priv
->enable_beacon
)
812 schedule_work(&vif_priv
->beacon_work
.work
);
816 static u64
rtl8180_prepare_multicast(struct ieee80211_hw
*dev
,
817 struct netdev_hw_addr_list
*mc_list
)
819 return netdev_hw_addr_list_count(mc_list
);
822 static void rtl8180_configure_filter(struct ieee80211_hw
*dev
,
823 unsigned int changed_flags
,
824 unsigned int *total_flags
,
827 struct rtl8180_priv
*priv
= dev
->priv
;
829 if (changed_flags
& FIF_FCSFAIL
)
830 priv
->rx_conf
^= RTL818X_RX_CONF_FCS
;
831 if (changed_flags
& FIF_CONTROL
)
832 priv
->rx_conf
^= RTL818X_RX_CONF_CTRL
;
833 if (changed_flags
& FIF_OTHER_BSS
)
834 priv
->rx_conf
^= RTL818X_RX_CONF_MONITOR
;
835 if (*total_flags
& FIF_ALLMULTI
|| multicast
> 0)
836 priv
->rx_conf
|= RTL818X_RX_CONF_MULTICAST
;
838 priv
->rx_conf
&= ~RTL818X_RX_CONF_MULTICAST
;
842 if (priv
->rx_conf
& RTL818X_RX_CONF_FCS
)
843 *total_flags
|= FIF_FCSFAIL
;
844 if (priv
->rx_conf
& RTL818X_RX_CONF_CTRL
)
845 *total_flags
|= FIF_CONTROL
;
846 if (priv
->rx_conf
& RTL818X_RX_CONF_MONITOR
)
847 *total_flags
|= FIF_OTHER_BSS
;
848 if (priv
->rx_conf
& RTL818X_RX_CONF_MULTICAST
)
849 *total_flags
|= FIF_ALLMULTI
;
851 rtl818x_iowrite32(priv
, &priv
->map
->RX_CONF
, priv
->rx_conf
);
854 static const struct ieee80211_ops rtl8180_ops
= {
856 .start
= rtl8180_start
,
857 .stop
= rtl8180_stop
,
858 .add_interface
= rtl8180_add_interface
,
859 .remove_interface
= rtl8180_remove_interface
,
860 .config
= rtl8180_config
,
861 .bss_info_changed
= rtl8180_bss_info_changed
,
862 .prepare_multicast
= rtl8180_prepare_multicast
,
863 .configure_filter
= rtl8180_configure_filter
,
864 .get_tsf
= rtl8180_get_tsf
,
867 static void rtl8180_eeprom_register_read(struct eeprom_93cx6
*eeprom
)
869 struct ieee80211_hw
*dev
= eeprom
->data
;
870 struct rtl8180_priv
*priv
= dev
->priv
;
871 u8 reg
= rtl818x_ioread8(priv
, &priv
->map
->EEPROM_CMD
);
873 eeprom
->reg_data_in
= reg
& RTL818X_EEPROM_CMD_WRITE
;
874 eeprom
->reg_data_out
= reg
& RTL818X_EEPROM_CMD_READ
;
875 eeprom
->reg_data_clock
= reg
& RTL818X_EEPROM_CMD_CK
;
876 eeprom
->reg_chip_select
= reg
& RTL818X_EEPROM_CMD_CS
;
879 static void rtl8180_eeprom_register_write(struct eeprom_93cx6
*eeprom
)
881 struct ieee80211_hw
*dev
= eeprom
->data
;
882 struct rtl8180_priv
*priv
= dev
->priv
;
885 if (eeprom
->reg_data_in
)
886 reg
|= RTL818X_EEPROM_CMD_WRITE
;
887 if (eeprom
->reg_data_out
)
888 reg
|= RTL818X_EEPROM_CMD_READ
;
889 if (eeprom
->reg_data_clock
)
890 reg
|= RTL818X_EEPROM_CMD_CK
;
891 if (eeprom
->reg_chip_select
)
892 reg
|= RTL818X_EEPROM_CMD_CS
;
894 rtl818x_iowrite8(priv
, &priv
->map
->EEPROM_CMD
, reg
);
895 rtl818x_ioread8(priv
, &priv
->map
->EEPROM_CMD
);
899 static int __devinit
rtl8180_probe(struct pci_dev
*pdev
,
900 const struct pci_device_id
*id
)
902 struct ieee80211_hw
*dev
;
903 struct rtl8180_priv
*priv
;
904 unsigned long mem_addr
, mem_len
;
905 unsigned int io_addr
, io_len
;
907 struct eeprom_93cx6 eeprom
;
908 const char *chip_name
, *rf_name
= NULL
;
911 u8 mac_addr
[ETH_ALEN
];
913 err
= pci_enable_device(pdev
);
915 printk(KERN_ERR
"%s (rtl8180): Cannot enable new PCI device\n",
920 err
= pci_request_regions(pdev
, KBUILD_MODNAME
);
922 printk(KERN_ERR
"%s (rtl8180): Cannot obtain PCI resources\n",
927 io_addr
= pci_resource_start(pdev
, 0);
928 io_len
= pci_resource_len(pdev
, 0);
929 mem_addr
= pci_resource_start(pdev
, 1);
930 mem_len
= pci_resource_len(pdev
, 1);
932 if (mem_len
< sizeof(struct rtl818x_csr
) ||
933 io_len
< sizeof(struct rtl818x_csr
)) {
934 printk(KERN_ERR
"%s (rtl8180): Too short PCI resources\n",
940 if ((err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32))) ||
941 (err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32)))) {
942 printk(KERN_ERR
"%s (rtl8180): No suitable DMA available\n",
947 pci_set_master(pdev
);
949 dev
= ieee80211_alloc_hw(sizeof(*priv
), &rtl8180_ops
);
951 printk(KERN_ERR
"%s (rtl8180): ieee80211 alloc failed\n",
961 SET_IEEE80211_DEV(dev
, &pdev
->dev
);
962 pci_set_drvdata(pdev
, dev
);
964 priv
->map
= pci_iomap(pdev
, 1, mem_len
);
966 priv
->map
= pci_iomap(pdev
, 0, io_len
);
969 printk(KERN_ERR
"%s (rtl8180): Cannot map device memory\n",
974 BUILD_BUG_ON(sizeof(priv
->channels
) != sizeof(rtl818x_channels
));
975 BUILD_BUG_ON(sizeof(priv
->rates
) != sizeof(rtl818x_rates
));
977 memcpy(priv
->channels
, rtl818x_channels
, sizeof(rtl818x_channels
));
978 memcpy(priv
->rates
, rtl818x_rates
, sizeof(rtl818x_rates
));
980 priv
->band
.band
= IEEE80211_BAND_2GHZ
;
981 priv
->band
.channels
= priv
->channels
;
982 priv
->band
.n_channels
= ARRAY_SIZE(rtl818x_channels
);
983 priv
->band
.bitrates
= priv
->rates
;
984 priv
->band
.n_bitrates
= 4;
985 dev
->wiphy
->bands
[IEEE80211_BAND_2GHZ
] = &priv
->band
;
987 dev
->flags
= IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING
|
988 IEEE80211_HW_RX_INCLUDES_FCS
|
989 IEEE80211_HW_SIGNAL_UNSPEC
;
990 dev
->vif_data_size
= sizeof(struct rtl8180_vif
);
991 dev
->wiphy
->interface_modes
= BIT(NL80211_IFTYPE_STATION
) |
992 BIT(NL80211_IFTYPE_ADHOC
);
994 dev
->max_signal
= 65;
996 reg
= rtl818x_ioread32(priv
, &priv
->map
->TX_CONF
);
997 reg
&= RTL818X_TX_CONF_HWVER_MASK
;
999 case RTL818X_TX_CONF_R8180_ABCD
:
1000 chip_name
= "RTL8180";
1002 case RTL818X_TX_CONF_R8180_F
:
1003 chip_name
= "RTL8180vF";
1005 case RTL818X_TX_CONF_R8185_ABC
:
1006 chip_name
= "RTL8185";
1008 case RTL818X_TX_CONF_R8185_D
:
1009 chip_name
= "RTL8185vD";
1012 printk(KERN_ERR
"%s (rtl8180): Unknown chip! (0x%x)\n",
1013 pci_name(pdev
), reg
>> 25);
1017 priv
->r8185
= reg
& RTL818X_TX_CONF_R8185_ABC
;
1019 priv
->band
.n_bitrates
= ARRAY_SIZE(rtl818x_rates
);
1020 pci_try_set_mwi(pdev
);
1024 eeprom
.register_read
= rtl8180_eeprom_register_read
;
1025 eeprom
.register_write
= rtl8180_eeprom_register_write
;
1026 if (rtl818x_ioread32(priv
, &priv
->map
->RX_CONF
) & (1 << 6))
1027 eeprom
.width
= PCI_EEPROM_WIDTH_93C66
;
1029 eeprom
.width
= PCI_EEPROM_WIDTH_93C46
;
1031 rtl818x_iowrite8(priv
, &priv
->map
->EEPROM_CMD
, RTL818X_EEPROM_CMD_PROGRAM
);
1032 rtl818x_ioread8(priv
, &priv
->map
->EEPROM_CMD
);
1035 eeprom_93cx6_read(&eeprom
, 0x06, &eeprom_val
);
1037 switch (eeprom_val
) {
1038 case 1: rf_name
= "Intersil";
1040 case 2: rf_name
= "RFMD";
1042 case 3: priv
->rf
= &sa2400_rf_ops
;
1044 case 4: priv
->rf
= &max2820_rf_ops
;
1046 case 5: priv
->rf
= &grf5101_rf_ops
;
1048 case 9: priv
->rf
= rtl8180_detect_rf(dev
);
1051 rf_name
= "RTL8255";
1054 printk(KERN_ERR
"%s (rtl8180): Unknown RF! (0x%x)\n",
1055 pci_name(pdev
), eeprom_val
);
1060 printk(KERN_ERR
"%s (rtl8180): %s RF frontend not supported!\n",
1061 pci_name(pdev
), rf_name
);
1065 eeprom_93cx6_read(&eeprom
, 0x17, &eeprom_val
);
1066 priv
->csthreshold
= eeprom_val
>> 8;
1069 eeprom_93cx6_multiread(&eeprom
, 0xD, (__le16
*)&anaparam
, 2);
1070 priv
->anaparam
= le32_to_cpu(anaparam
);
1071 eeprom_93cx6_read(&eeprom
, 0x19, &priv
->rfparam
);
1074 eeprom_93cx6_multiread(&eeprom
, 0x7, (__le16
*)mac_addr
, 3);
1075 if (!is_valid_ether_addr(mac_addr
)) {
1076 printk(KERN_WARNING
"%s (rtl8180): Invalid hwaddr! Using"
1077 " randomly generated MAC addr\n", pci_name(pdev
));
1078 random_ether_addr(mac_addr
);
1080 SET_IEEE80211_PERM_ADDR(dev
, mac_addr
);
1083 for (i
= 0; i
< 14; i
+= 2) {
1085 eeprom_93cx6_read(&eeprom
, 0x10 + (i
>> 1), &txpwr
);
1086 priv
->channels
[i
].hw_value
= txpwr
& 0xFF;
1087 priv
->channels
[i
+ 1].hw_value
= txpwr
>> 8;
1092 for (i
= 0; i
< 14; i
+= 2) {
1094 eeprom_93cx6_read(&eeprom
, 0x20 + (i
>> 1), &txpwr
);
1095 priv
->channels
[i
].hw_value
|= (txpwr
& 0xFF) << 8;
1096 priv
->channels
[i
+ 1].hw_value
|= txpwr
& 0xFF00;
1100 rtl818x_iowrite8(priv
, &priv
->map
->EEPROM_CMD
, RTL818X_EEPROM_CMD_NORMAL
);
1102 spin_lock_init(&priv
->lock
);
1104 err
= ieee80211_register_hw(dev
);
1106 printk(KERN_ERR
"%s (rtl8180): Cannot register device\n",
1111 wiphy_info(dev
->wiphy
, "hwaddr %pm, %s + %s\n",
1112 mac_addr
, chip_name
, priv
->rf
->name
);
1120 pci_set_drvdata(pdev
, NULL
);
1121 ieee80211_free_hw(dev
);
1124 pci_release_regions(pdev
);
1125 pci_disable_device(pdev
);
1129 static void __devexit
rtl8180_remove(struct pci_dev
*pdev
)
1131 struct ieee80211_hw
*dev
= pci_get_drvdata(pdev
);
1132 struct rtl8180_priv
*priv
;
1137 ieee80211_unregister_hw(dev
);
1141 pci_iounmap(pdev
, priv
->map
);
1142 pci_release_regions(pdev
);
1143 pci_disable_device(pdev
);
1144 ieee80211_free_hw(dev
);
1148 static int rtl8180_suspend(struct pci_dev
*pdev
, pm_message_t state
)
1150 pci_save_state(pdev
);
1151 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
1155 static int rtl8180_resume(struct pci_dev
*pdev
)
1157 pci_set_power_state(pdev
, PCI_D0
);
1158 pci_restore_state(pdev
);
1162 #endif /* CONFIG_PM */
1164 static struct pci_driver rtl8180_driver
= {
1165 .name
= KBUILD_MODNAME
,
1166 .id_table
= rtl8180_table
,
1167 .probe
= rtl8180_probe
,
1168 .remove
= __devexit_p(rtl8180_remove
),
1170 .suspend
= rtl8180_suspend
,
1171 .resume
= rtl8180_resume
,
1172 #endif /* CONFIG_PM */
1175 static int __init
rtl8180_init(void)
1177 return pci_register_driver(&rtl8180_driver
);
1180 static void __exit
rtl8180_exit(void)
1182 pci_unregister_driver(&rtl8180_driver
);
1185 module_init(rtl8180_init
);
1186 module_exit(rtl8180_exit
);