dev.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /*
  2. * Linux device driver for RTL8180 / RTL8185
  3. *
  4. * Copyright 2007 Michael Wu <flamingice@sourmilk.net>
  5. * Copyright 2007 Andrea Merello <andrea.merello@gmail.com>
  6. *
  7. * Based on the r8180 driver, which is:
  8. * Copyright 2004-2005 Andrea Merello <andrea.merello@gmail.com>, et al.
  9. *
  10. * Thanks to Realtek for their support!
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/interrupt.h>
  17. #include <linux/pci.h>
  18. #include <linux/slab.h>
  19. #include <linux/delay.h>
  20. #include <linux/etherdevice.h>
  21. #include <linux/eeprom_93cx6.h>
  22. #include <linux/module.h>
  23. #include <net/mac80211.h>
  24. #include "rtl8180.h"
  25. #include "rtl8225.h"
  26. #include "sa2400.h"
  27. #include "max2820.h"
  28. #include "grf5101.h"
  29. MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
  30. MODULE_AUTHOR("Andrea Merello <andrea.merello@gmail.com>");
  31. MODULE_DESCRIPTION("RTL8180 / RTL8185 PCI wireless driver");
  32. MODULE_LICENSE("GPL");
  33. static DEFINE_PCI_DEVICE_TABLE(rtl8180_table) = {
  34. /* rtl8185 */
  35. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8185) },
  36. { PCI_DEVICE(PCI_VENDOR_ID_BELKIN, 0x700f) },
  37. { PCI_DEVICE(PCI_VENDOR_ID_BELKIN, 0x701f) },
  38. /* rtl8180 */
  39. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8180) },
  40. { PCI_DEVICE(0x1799, 0x6001) },
  41. { PCI_DEVICE(0x1799, 0x6020) },
  42. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x3300) },
  43. { PCI_DEVICE(0x1186, 0x3301) },
  44. { PCI_DEVICE(0x1432, 0x7106) },
  45. { }
  46. };
  47. MODULE_DEVICE_TABLE(pci, rtl8180_table);
  48. static const struct ieee80211_rate rtl818x_rates[] = {
  49. { .bitrate = 10, .hw_value = 0, },
  50. { .bitrate = 20, .hw_value = 1, },
  51. { .bitrate = 55, .hw_value = 2, },
  52. { .bitrate = 110, .hw_value = 3, },
  53. { .bitrate = 60, .hw_value = 4, },
  54. { .bitrate = 90, .hw_value = 5, },
  55. { .bitrate = 120, .hw_value = 6, },
  56. { .bitrate = 180, .hw_value = 7, },
  57. { .bitrate = 240, .hw_value = 8, },
  58. { .bitrate = 360, .hw_value = 9, },
  59. { .bitrate = 480, .hw_value = 10, },
  60. { .bitrate = 540, .hw_value = 11, },
  61. };
  62. static const struct ieee80211_channel rtl818x_channels[] = {
  63. { .center_freq = 2412 },
  64. { .center_freq = 2417 },
  65. { .center_freq = 2422 },
  66. { .center_freq = 2427 },
  67. { .center_freq = 2432 },
  68. { .center_freq = 2437 },
  69. { .center_freq = 2442 },
  70. { .center_freq = 2447 },
  71. { .center_freq = 2452 },
  72. { .center_freq = 2457 },
  73. { .center_freq = 2462 },
  74. { .center_freq = 2467 },
  75. { .center_freq = 2472 },
  76. { .center_freq = 2484 },
  77. };
  78. void rtl8180_write_phy(struct ieee80211_hw *dev, u8 addr, u32 data)
  79. {
  80. struct rtl8180_priv *priv = dev->priv;
  81. int i = 10;
  82. u32 buf;
  83. buf = (data << 8) | addr;
  84. rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->PHY[0], buf | 0x80);
  85. while (i--) {
  86. rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->PHY[0], buf);
  87. if (rtl818x_ioread8(priv, &priv->map->PHY[2]) == (data & 0xFF))
  88. return;
  89. }
  90. }
  91. static void rtl8180_handle_rx(struct ieee80211_hw *dev)
  92. {
  93. struct rtl8180_priv *priv = dev->priv;
  94. unsigned int count = 32;
  95. u8 signal, agc, sq;
  96. dma_addr_t mapping;
  97. while (count--) {
  98. struct rtl8180_rx_desc *entry = &priv->rx_ring[priv->rx_idx];
  99. struct sk_buff *skb = priv->rx_buf[priv->rx_idx];
  100. u32 flags = le32_to_cpu(entry->flags);
  101. if (flags & RTL818X_RX_DESC_FLAG_OWN)
  102. return;
  103. if (unlikely(flags & (RTL818X_RX_DESC_FLAG_DMA_FAIL |
  104. RTL818X_RX_DESC_FLAG_FOF |
  105. RTL818X_RX_DESC_FLAG_RX_ERR)))
  106. goto done;
  107. else {
  108. u32 flags2 = le32_to_cpu(entry->flags2);
  109. struct ieee80211_rx_status rx_status = {0};
  110. struct sk_buff *new_skb = dev_alloc_skb(MAX_RX_SIZE);
  111. if (unlikely(!new_skb))
  112. goto done;
  113. mapping = pci_map_single(priv->pdev,
  114. skb_tail_pointer(new_skb),
  115. MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
  116. if (pci_dma_mapping_error(priv->pdev, mapping)) {
  117. kfree_skb(new_skb);
  118. dev_err(&priv->pdev->dev, "RX DMA map error\n");
  119. goto done;
  120. }
  121. pci_unmap_single(priv->pdev,
  122. *((dma_addr_t *)skb->cb),
  123. MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
  124. skb_put(skb, flags & 0xFFF);
  125. rx_status.antenna = (flags2 >> 15) & 1;
  126. rx_status.rate_idx = (flags >> 20) & 0xF;
  127. agc = (flags2 >> 17) & 0x7F;
  128. if (priv->r8185) {
  129. if (rx_status.rate_idx > 3)
  130. signal = 90 - clamp_t(u8, agc, 25, 90);
  131. else
  132. signal = 95 - clamp_t(u8, agc, 30, 95);
  133. } else {
  134. sq = flags2 & 0xff;
  135. signal = priv->rf->calc_rssi(agc, sq);
  136. }
  137. rx_status.signal = signal;
  138. rx_status.freq = dev->conf.chandef.chan->center_freq;
  139. rx_status.band = dev->conf.chandef.chan->band;
  140. rx_status.mactime = le64_to_cpu(entry->tsft);
  141. rx_status.flag |= RX_FLAG_MACTIME_START;
  142. if (flags & RTL818X_RX_DESC_FLAG_CRC32_ERR)
  143. rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
  144. memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
  145. ieee80211_rx_irqsafe(dev, skb);
  146. skb = new_skb;
  147. priv->rx_buf[priv->rx_idx] = skb;
  148. *((dma_addr_t *) skb->cb) = mapping;
  149. }
  150. done:
  151. entry->rx_buf = cpu_to_le32(*((dma_addr_t *)skb->cb));
  152. entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
  153. MAX_RX_SIZE);
  154. if (priv->rx_idx == 31)
  155. entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
  156. priv->rx_idx = (priv->rx_idx + 1) % 32;
  157. }
  158. }
  159. static void rtl8180_handle_tx(struct ieee80211_hw *dev, unsigned int prio)
  160. {
  161. struct rtl8180_priv *priv = dev->priv;
  162. struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
  163. while (skb_queue_len(&ring->queue)) {
  164. struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
  165. struct sk_buff *skb;
  166. struct ieee80211_tx_info *info;
  167. u32 flags = le32_to_cpu(entry->flags);
  168. if (flags & RTL818X_TX_DESC_FLAG_OWN)
  169. return;
  170. ring->idx = (ring->idx + 1) % ring->entries;
  171. skb = __skb_dequeue(&ring->queue);
  172. pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
  173. skb->len, PCI_DMA_TODEVICE);
  174. info = IEEE80211_SKB_CB(skb);
  175. ieee80211_tx_info_clear_status(info);
  176. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
  177. (flags & RTL818X_TX_DESC_FLAG_TX_OK))
  178. info->flags |= IEEE80211_TX_STAT_ACK;
  179. info->status.rates[0].count = (flags & 0xFF) + 1;
  180. info->status.rates[1].idx = -1;
  181. ieee80211_tx_status_irqsafe(dev, skb);
  182. if (ring->entries - skb_queue_len(&ring->queue) == 2)
  183. ieee80211_wake_queue(dev, prio);
  184. }
  185. }
  186. static irqreturn_t rtl8180_interrupt(int irq, void *dev_id)
  187. {
  188. struct ieee80211_hw *dev = dev_id;
  189. struct rtl8180_priv *priv = dev->priv;
  190. u16 reg;
  191. spin_lock(&priv->lock);
  192. reg = rtl818x_ioread16(priv, &priv->map->INT_STATUS);
  193. if (unlikely(reg == 0xFFFF)) {
  194. spin_unlock(&priv->lock);
  195. return IRQ_HANDLED;
  196. }
  197. rtl818x_iowrite16(priv, &priv->map->INT_STATUS, reg);
  198. if (reg & (RTL818X_INT_TXB_OK | RTL818X_INT_TXB_ERR))
  199. rtl8180_handle_tx(dev, 3);
  200. if (reg & (RTL818X_INT_TXH_OK | RTL818X_INT_TXH_ERR))
  201. rtl8180_handle_tx(dev, 2);
  202. if (reg & (RTL818X_INT_TXN_OK | RTL818X_INT_TXN_ERR))
  203. rtl8180_handle_tx(dev, 1);
  204. if (reg & (RTL818X_INT_TXL_OK | RTL818X_INT_TXL_ERR))
  205. rtl8180_handle_tx(dev, 0);
  206. if (reg & (RTL818X_INT_RX_OK | RTL818X_INT_RX_ERR))
  207. rtl8180_handle_rx(dev);
  208. spin_unlock(&priv->lock);
  209. return IRQ_HANDLED;
  210. }
  211. static void rtl8180_tx(struct ieee80211_hw *dev,
  212. struct ieee80211_tx_control *control,
  213. struct sk_buff *skb)
  214. {
  215. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  216. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  217. struct rtl8180_priv *priv = dev->priv;
  218. struct rtl8180_tx_ring *ring;
  219. struct rtl8180_tx_desc *entry;
  220. unsigned long flags;
  221. unsigned int idx, prio;
  222. dma_addr_t mapping;
  223. u32 tx_flags;
  224. u8 rc_flags;
  225. u16 plcp_len = 0;
  226. __le16 rts_duration = 0;
  227. prio = skb_get_queue_mapping(skb);
  228. ring = &priv->tx_ring[prio];
  229. mapping = pci_map_single(priv->pdev, skb->data,
  230. skb->len, PCI_DMA_TODEVICE);
  231. if (pci_dma_mapping_error(priv->pdev, mapping)) {
  232. kfree_skb(skb);
  233. dev_err(&priv->pdev->dev, "TX DMA mapping error\n");
  234. return;
  235. }
  236. tx_flags = RTL818X_TX_DESC_FLAG_OWN | RTL818X_TX_DESC_FLAG_FS |
  237. RTL818X_TX_DESC_FLAG_LS |
  238. (ieee80211_get_tx_rate(dev, info)->hw_value << 24) |
  239. skb->len;
  240. if (priv->r8185)
  241. tx_flags |= RTL818X_TX_DESC_FLAG_DMA |
  242. RTL818X_TX_DESC_FLAG_NO_ENC;
  243. rc_flags = info->control.rates[0].flags;
  244. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  245. tx_flags |= RTL818X_TX_DESC_FLAG_RTS;
  246. tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
  247. } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  248. tx_flags |= RTL818X_TX_DESC_FLAG_CTS;
  249. tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
  250. }
  251. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS)
  252. rts_duration = ieee80211_rts_duration(dev, priv->vif, skb->len,
  253. info);
  254. if (!priv->r8185) {
  255. unsigned int remainder;
  256. plcp_len = DIV_ROUND_UP(16 * (skb->len + 4),
  257. (ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
  258. remainder = (16 * (skb->len + 4)) %
  259. ((ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
  260. if (remainder <= 6)
  261. plcp_len |= 1 << 15;
  262. }
  263. spin_lock_irqsave(&priv->lock, flags);
  264. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  265. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  266. priv->seqno += 0x10;
  267. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  268. hdr->seq_ctrl |= cpu_to_le16(priv->seqno);
  269. }
  270. idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
  271. entry = &ring->desc[idx];
  272. entry->rts_duration = rts_duration;
  273. entry->plcp_len = cpu_to_le16(plcp_len);
  274. entry->tx_buf = cpu_to_le32(mapping);
  275. entry->frame_len = cpu_to_le32(skb->len);
  276. entry->flags2 = info->control.rates[1].idx >= 0 ?
  277. ieee80211_get_alt_retry_rate(dev, info, 0)->bitrate << 4 : 0;
  278. entry->retry_limit = info->control.rates[0].count;
  279. entry->flags = cpu_to_le32(tx_flags);
  280. __skb_queue_tail(&ring->queue, skb);
  281. if (ring->entries - skb_queue_len(&ring->queue) < 2)
  282. ieee80211_stop_queue(dev, prio);
  283. spin_unlock_irqrestore(&priv->lock, flags);
  284. rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING, (1 << (prio + 4)));
  285. }
  286. void rtl8180_set_anaparam(struct rtl8180_priv *priv, u32 anaparam)
  287. {
  288. u8 reg;
  289. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  290. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  291. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  292. reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
  293. rtl818x_iowrite32(priv, &priv->map->ANAPARAM, anaparam);
  294. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  295. reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
  296. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  297. }
  298. static int rtl8180_init_hw(struct ieee80211_hw *dev)
  299. {
  300. struct rtl8180_priv *priv = dev->priv;
  301. u16 reg;
  302. rtl818x_iowrite8(priv, &priv->map->CMD, 0);
  303. rtl818x_ioread8(priv, &priv->map->CMD);
  304. msleep(10);
  305. /* reset */
  306. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
  307. rtl818x_ioread8(priv, &priv->map->CMD);
  308. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  309. reg &= (1 << 1);
  310. reg |= RTL818X_CMD_RESET;
  311. rtl818x_iowrite8(priv, &priv->map->CMD, RTL818X_CMD_RESET);
  312. rtl818x_ioread8(priv, &priv->map->CMD);
  313. msleep(200);
  314. /* check success of reset */
  315. if (rtl818x_ioread8(priv, &priv->map->CMD) & RTL818X_CMD_RESET) {
  316. wiphy_err(dev->wiphy, "reset timeout!\n");
  317. return -ETIMEDOUT;
  318. }
  319. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
  320. rtl818x_ioread8(priv, &priv->map->CMD);
  321. msleep(200);
  322. if (rtl818x_ioread8(priv, &priv->map->CONFIG3) & (1 << 3)) {
  323. /* For cardbus */
  324. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  325. reg |= 1 << 1;
  326. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
  327. reg = rtl818x_ioread16(priv, &priv->map->FEMR);
  328. reg |= (1 << 15) | (1 << 14) | (1 << 4);
  329. rtl818x_iowrite16(priv, &priv->map->FEMR, reg);
  330. }
  331. rtl818x_iowrite8(priv, &priv->map->MSR, 0);
  332. if (!priv->r8185)
  333. rtl8180_set_anaparam(priv, priv->anaparam);
  334. rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
  335. rtl818x_iowrite32(priv, &priv->map->TBDA, priv->tx_ring[3].dma);
  336. rtl818x_iowrite32(priv, &priv->map->THPDA, priv->tx_ring[2].dma);
  337. rtl818x_iowrite32(priv, &priv->map->TNPDA, priv->tx_ring[1].dma);
  338. rtl818x_iowrite32(priv, &priv->map->TLPDA, priv->tx_ring[0].dma);
  339. /* TODO: necessary? specs indicate not */
  340. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  341. reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
  342. rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg & ~(1 << 3));
  343. if (priv->r8185) {
  344. reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
  345. rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg | (1 << 4));
  346. }
  347. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  348. /* TODO: set CONFIG5 for calibrating AGC on rtl8180 + philips radio? */
  349. /* TODO: turn off hw wep on rtl8180 */
  350. rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
  351. if (priv->r8185) {
  352. rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
  353. rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
  354. rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (8 << 4) | 0);
  355. rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
  356. /* TODO: set ClkRun enable? necessary? */
  357. reg = rtl818x_ioread8(priv, &priv->map->GP_ENABLE);
  358. rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, reg & ~(1 << 6));
  359. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  360. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  361. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | (1 << 2));
  362. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  363. } else {
  364. rtl818x_iowrite16(priv, &priv->map->BRSR, 0x1);
  365. rtl818x_iowrite8(priv, &priv->map->SECURITY, 0);
  366. rtl818x_iowrite8(priv, &priv->map->PHY_DELAY, 0x6);
  367. rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, 0x4C);
  368. }
  369. priv->rf->init(dev);
  370. if (priv->r8185)
  371. rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
  372. return 0;
  373. }
  374. static int rtl8180_init_rx_ring(struct ieee80211_hw *dev)
  375. {
  376. struct rtl8180_priv *priv = dev->priv;
  377. struct rtl8180_rx_desc *entry;
  378. int i;
  379. priv->rx_ring = pci_alloc_consistent(priv->pdev,
  380. sizeof(*priv->rx_ring) * 32,
  381. &priv->rx_ring_dma);
  382. if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
  383. wiphy_err(dev->wiphy, "Cannot allocate RX ring\n");
  384. return -ENOMEM;
  385. }
  386. memset(priv->rx_ring, 0, sizeof(*priv->rx_ring) * 32);
  387. priv->rx_idx = 0;
  388. for (i = 0; i < 32; i++) {
  389. struct sk_buff *skb = dev_alloc_skb(MAX_RX_SIZE);
  390. dma_addr_t *mapping;
  391. entry = &priv->rx_ring[i];
  392. if (!skb)
  393. return 0;
  394. priv->rx_buf[i] = skb;
  395. mapping = (dma_addr_t *)skb->cb;
  396. *mapping = pci_map_single(priv->pdev, skb_tail_pointer(skb),
  397. MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
  398. entry->rx_buf = cpu_to_le32(*mapping);
  399. entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
  400. MAX_RX_SIZE);
  401. }
  402. entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
  403. return 0;
  404. }
  405. static void rtl8180_free_rx_ring(struct ieee80211_hw *dev)
  406. {
  407. struct rtl8180_priv *priv = dev->priv;
  408. int i;
  409. for (i = 0; i < 32; i++) {
  410. struct sk_buff *skb = priv->rx_buf[i];
  411. if (!skb)
  412. continue;
  413. pci_unmap_single(priv->pdev,
  414. *((dma_addr_t *)skb->cb),
  415. MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
  416. kfree_skb(skb);
  417. }
  418. pci_free_consistent(priv->pdev, sizeof(*priv->rx_ring) * 32,
  419. priv->rx_ring, priv->rx_ring_dma);
  420. priv->rx_ring = NULL;
  421. }
  422. static int rtl8180_init_tx_ring(struct ieee80211_hw *dev,
  423. unsigned int prio, unsigned int entries)
  424. {
  425. struct rtl8180_priv *priv = dev->priv;
  426. struct rtl8180_tx_desc *ring;
  427. dma_addr_t dma;
  428. int i;
  429. ring = pci_alloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
  430. if (!ring || (unsigned long)ring & 0xFF) {
  431. wiphy_err(dev->wiphy, "Cannot allocate TX ring (prio = %d)\n",
  432. prio);
  433. return -ENOMEM;
  434. }
  435. memset(ring, 0, sizeof(*ring)*entries);
  436. priv->tx_ring[prio].desc = ring;
  437. priv->tx_ring[prio].dma = dma;
  438. priv->tx_ring[prio].idx = 0;
  439. priv->tx_ring[prio].entries = entries;
  440. skb_queue_head_init(&priv->tx_ring[prio].queue);
  441. for (i = 0; i < entries; i++)
  442. ring[i].next_tx_desc =
  443. cpu_to_le32((u32)dma + ((i + 1) % entries) * sizeof(*ring));
  444. return 0;
  445. }
  446. static void rtl8180_free_tx_ring(struct ieee80211_hw *dev, unsigned int prio)
  447. {
  448. struct rtl8180_priv *priv = dev->priv;
  449. struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
  450. while (skb_queue_len(&ring->queue)) {
  451. struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
  452. struct sk_buff *skb = __skb_dequeue(&ring->queue);
  453. pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
  454. skb->len, PCI_DMA_TODEVICE);
  455. kfree_skb(skb);
  456. ring->idx = (ring->idx + 1) % ring->entries;
  457. }
  458. pci_free_consistent(priv->pdev, sizeof(*ring->desc)*ring->entries,
  459. ring->desc, ring->dma);
  460. ring->desc = NULL;
  461. }
  462. static int rtl8180_start(struct ieee80211_hw *dev)
  463. {
  464. struct rtl8180_priv *priv = dev->priv;
  465. int ret, i;
  466. u32 reg;
  467. ret = rtl8180_init_rx_ring(dev);
  468. if (ret)
  469. return ret;
  470. for (i = 0; i < 4; i++)
  471. if ((ret = rtl8180_init_tx_ring(dev, i, 16)))
  472. goto err_free_rings;
  473. ret = rtl8180_init_hw(dev);
  474. if (ret)
  475. goto err_free_rings;
  476. rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
  477. rtl818x_iowrite32(priv, &priv->map->TBDA, priv->tx_ring[3].dma);
  478. rtl818x_iowrite32(priv, &priv->map->THPDA, priv->tx_ring[2].dma);
  479. rtl818x_iowrite32(priv, &priv->map->TNPDA, priv->tx_ring[1].dma);
  480. rtl818x_iowrite32(priv, &priv->map->TLPDA, priv->tx_ring[0].dma);
  481. ret = request_irq(priv->pdev->irq, rtl8180_interrupt,
  482. IRQF_SHARED, KBUILD_MODNAME, dev);
  483. if (ret) {
  484. wiphy_err(dev->wiphy, "failed to register IRQ handler\n");
  485. goto err_free_rings;
  486. }
  487. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
  488. rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
  489. rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
  490. reg = RTL818X_RX_CONF_ONLYERLPKT |
  491. RTL818X_RX_CONF_RX_AUTORESETPHY |
  492. RTL818X_RX_CONF_MGMT |
  493. RTL818X_RX_CONF_DATA |
  494. (7 << 8 /* MAX RX DMA */) |
  495. RTL818X_RX_CONF_BROADCAST |
  496. RTL818X_RX_CONF_NICMAC;
  497. if (priv->r8185)
  498. reg |= RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2;
  499. else {
  500. reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE1)
  501. ? RTL818X_RX_CONF_CSDM1 : 0;
  502. reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE2)
  503. ? RTL818X_RX_CONF_CSDM2 : 0;
  504. }
  505. priv->rx_conf = reg;
  506. rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
  507. if (priv->r8185) {
  508. reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
  509. reg &= ~RTL818X_CW_CONF_PERPACKET_CW_SHIFT;
  510. reg |= RTL818X_CW_CONF_PERPACKET_RETRY_SHIFT;
  511. rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
  512. reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
  513. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN_SHIFT;
  514. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT;
  515. reg |= RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
  516. rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
  517. /* disable early TX */
  518. rtl818x_iowrite8(priv, (u8 __iomem *)priv->map + 0xec, 0x3f);
  519. }
  520. reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
  521. reg |= (6 << 21 /* MAX TX DMA */) |
  522. RTL818X_TX_CONF_NO_ICV;
  523. if (priv->r8185)
  524. reg &= ~RTL818X_TX_CONF_PROBE_DTS;
  525. else
  526. reg &= ~RTL818X_TX_CONF_HW_SEQNUM;
  527. /* different meaning, same value on both rtl8185 and rtl8180 */
  528. reg &= ~RTL818X_TX_CONF_SAT_HWPLCP;
  529. rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
  530. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  531. reg |= RTL818X_CMD_RX_ENABLE;
  532. reg |= RTL818X_CMD_TX_ENABLE;
  533. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  534. return 0;
  535. err_free_rings:
  536. rtl8180_free_rx_ring(dev);
  537. for (i = 0; i < 4; i++)
  538. if (priv->tx_ring[i].desc)
  539. rtl8180_free_tx_ring(dev, i);
  540. return ret;
  541. }
  542. static void rtl8180_stop(struct ieee80211_hw *dev)
  543. {
  544. struct rtl8180_priv *priv = dev->priv;
  545. u8 reg;
  546. int i;
  547. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
  548. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  549. reg &= ~RTL818X_CMD_TX_ENABLE;
  550. reg &= ~RTL818X_CMD_RX_ENABLE;
  551. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  552. priv->rf->stop(dev);
  553. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  554. reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
  555. rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
  556. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  557. free_irq(priv->pdev->irq, dev);
  558. rtl8180_free_rx_ring(dev);
  559. for (i = 0; i < 4; i++)
  560. rtl8180_free_tx_ring(dev, i);
  561. }
  562. static u64 rtl8180_get_tsf(struct ieee80211_hw *dev,
  563. struct ieee80211_vif *vif)
  564. {
  565. struct rtl8180_priv *priv = dev->priv;
  566. return rtl818x_ioread32(priv, &priv->map->TSFT[0]) |
  567. (u64)(rtl818x_ioread32(priv, &priv->map->TSFT[1])) << 32;
  568. }
  569. static void rtl8180_beacon_work(struct work_struct *work)
  570. {
  571. struct rtl8180_vif *vif_priv =
  572. container_of(work, struct rtl8180_vif, beacon_work.work);
  573. struct ieee80211_vif *vif =
  574. container_of((void *)vif_priv, struct ieee80211_vif, drv_priv);
  575. struct ieee80211_hw *dev = vif_priv->dev;
  576. struct ieee80211_mgmt *mgmt;
  577. struct sk_buff *skb;
  578. /* don't overflow the tx ring */
  579. if (ieee80211_queue_stopped(dev, 0))
  580. goto resched;
  581. /* grab a fresh beacon */
  582. skb = ieee80211_beacon_get(dev, vif);
  583. if (!skb)
  584. goto resched;
  585. /*
  586. * update beacon timestamp w/ TSF value
  587. * TODO: make hardware update beacon timestamp
  588. */
  589. mgmt = (struct ieee80211_mgmt *)skb->data;
  590. mgmt->u.beacon.timestamp = cpu_to_le64(rtl8180_get_tsf(dev, vif));
  591. /* TODO: use actual beacon queue */
  592. skb_set_queue_mapping(skb, 0);
  593. rtl8180_tx(dev, NULL, skb);
  594. resched:
  595. /*
  596. * schedule next beacon
  597. * TODO: use hardware support for beacon timing
  598. */
  599. schedule_delayed_work(&vif_priv->beacon_work,
  600. usecs_to_jiffies(1024 * vif->bss_conf.beacon_int));
  601. }
  602. static int rtl8180_add_interface(struct ieee80211_hw *dev,
  603. struct ieee80211_vif *vif)
  604. {
  605. struct rtl8180_priv *priv = dev->priv;
  606. struct rtl8180_vif *vif_priv;
  607. /*
  608. * We only support one active interface at a time.
  609. */
  610. if (priv->vif)
  611. return -EBUSY;
  612. switch (vif->type) {
  613. case NL80211_IFTYPE_STATION:
  614. case NL80211_IFTYPE_ADHOC:
  615. break;
  616. default:
  617. return -EOPNOTSUPP;
  618. }
  619. priv->vif = vif;
  620. /* Initialize driver private area */
  621. vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
  622. vif_priv->dev = dev;
  623. INIT_DELAYED_WORK(&vif_priv->beacon_work, rtl8180_beacon_work);
  624. vif_priv->enable_beacon = false;
  625. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  626. rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->MAC[0],
  627. le32_to_cpu(*(__le32 *)vif->addr));
  628. rtl818x_iowrite16(priv, (__le16 __iomem *)&priv->map->MAC[4],
  629. le16_to_cpu(*(__le16 *)(vif->addr + 4)));
  630. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  631. return 0;
  632. }
  633. static void rtl8180_remove_interface(struct ieee80211_hw *dev,
  634. struct ieee80211_vif *vif)
  635. {
  636. struct rtl8180_priv *priv = dev->priv;
  637. priv->vif = NULL;
  638. }
  639. static int rtl8180_config(struct ieee80211_hw *dev, u32 changed)
  640. {
  641. struct rtl8180_priv *priv = dev->priv;
  642. struct ieee80211_conf *conf = &dev->conf;
  643. priv->rf->set_chan(dev, conf);
  644. return 0;
  645. }
  646. static void rtl8180_bss_info_changed(struct ieee80211_hw *dev,
  647. struct ieee80211_vif *vif,
  648. struct ieee80211_bss_conf *info,
  649. u32 changed)
  650. {
  651. struct rtl8180_priv *priv = dev->priv;
  652. struct rtl8180_vif *vif_priv;
  653. int i;
  654. u8 reg;
  655. vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
  656. if (changed & BSS_CHANGED_BSSID) {
  657. for (i = 0; i < ETH_ALEN; i++)
  658. rtl818x_iowrite8(priv, &priv->map->BSSID[i],
  659. info->bssid[i]);
  660. if (is_valid_ether_addr(info->bssid)) {
  661. if (vif->type == NL80211_IFTYPE_ADHOC)
  662. reg = RTL818X_MSR_ADHOC;
  663. else
  664. reg = RTL818X_MSR_INFRA;
  665. } else
  666. reg = RTL818X_MSR_NO_LINK;
  667. rtl818x_iowrite8(priv, &priv->map->MSR, reg);
  668. }
  669. if (changed & BSS_CHANGED_ERP_SLOT && priv->rf->conf_erp)
  670. priv->rf->conf_erp(dev, info);
  671. if (changed & BSS_CHANGED_BEACON_ENABLED)
  672. vif_priv->enable_beacon = info->enable_beacon;
  673. if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON)) {
  674. cancel_delayed_work_sync(&vif_priv->beacon_work);
  675. if (vif_priv->enable_beacon)
  676. schedule_work(&vif_priv->beacon_work.work);
  677. }
  678. }
  679. static u64 rtl8180_prepare_multicast(struct ieee80211_hw *dev,
  680. struct netdev_hw_addr_list *mc_list)
  681. {
  682. return netdev_hw_addr_list_count(mc_list);
  683. }
  684. static void rtl8180_configure_filter(struct ieee80211_hw *dev,
  685. unsigned int changed_flags,
  686. unsigned int *total_flags,
  687. u64 multicast)
  688. {
  689. struct rtl8180_priv *priv = dev->priv;
  690. if (changed_flags & FIF_FCSFAIL)
  691. priv->rx_conf ^= RTL818X_RX_CONF_FCS;
  692. if (changed_flags & FIF_CONTROL)
  693. priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
  694. if (changed_flags & FIF_OTHER_BSS)
  695. priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
  696. if (*total_flags & FIF_ALLMULTI || multicast > 0)
  697. priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
  698. else
  699. priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
  700. *total_flags = 0;
  701. if (priv->rx_conf & RTL818X_RX_CONF_FCS)
  702. *total_flags |= FIF_FCSFAIL;
  703. if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
  704. *total_flags |= FIF_CONTROL;
  705. if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
  706. *total_flags |= FIF_OTHER_BSS;
  707. if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
  708. *total_flags |= FIF_ALLMULTI;
  709. rtl818x_iowrite32(priv, &priv->map->RX_CONF, priv->rx_conf);
  710. }
  711. static const struct ieee80211_ops rtl8180_ops = {
  712. .tx = rtl8180_tx,
  713. .start = rtl8180_start,
  714. .stop = rtl8180_stop,
  715. .add_interface = rtl8180_add_interface,
  716. .remove_interface = rtl8180_remove_interface,
  717. .config = rtl8180_config,
  718. .bss_info_changed = rtl8180_bss_info_changed,
  719. .prepare_multicast = rtl8180_prepare_multicast,
  720. .configure_filter = rtl8180_configure_filter,
  721. .get_tsf = rtl8180_get_tsf,
  722. };
  723. static void rtl8180_eeprom_register_read(struct eeprom_93cx6 *eeprom)
  724. {
  725. struct ieee80211_hw *dev = eeprom->data;
  726. struct rtl8180_priv *priv = dev->priv;
  727. u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  728. eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
  729. eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
  730. eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
  731. eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
  732. }
  733. static void rtl8180_eeprom_register_write(struct eeprom_93cx6 *eeprom)
  734. {
  735. struct ieee80211_hw *dev = eeprom->data;
  736. struct rtl8180_priv *priv = dev->priv;
  737. u8 reg = 2 << 6;
  738. if (eeprom->reg_data_in)
  739. reg |= RTL818X_EEPROM_CMD_WRITE;
  740. if (eeprom->reg_data_out)
  741. reg |= RTL818X_EEPROM_CMD_READ;
  742. if (eeprom->reg_data_clock)
  743. reg |= RTL818X_EEPROM_CMD_CK;
  744. if (eeprom->reg_chip_select)
  745. reg |= RTL818X_EEPROM_CMD_CS;
  746. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
  747. rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  748. udelay(10);
  749. }
  750. static int rtl8180_probe(struct pci_dev *pdev,
  751. const struct pci_device_id *id)
  752. {
  753. struct ieee80211_hw *dev;
  754. struct rtl8180_priv *priv;
  755. unsigned long mem_addr, mem_len;
  756. unsigned int io_addr, io_len;
  757. int err, i;
  758. struct eeprom_93cx6 eeprom;
  759. const char *chip_name, *rf_name = NULL;
  760. u32 reg;
  761. u16 eeprom_val;
  762. u8 mac_addr[ETH_ALEN];
  763. err = pci_enable_device(pdev);
  764. if (err) {
  765. printk(KERN_ERR "%s (rtl8180): Cannot enable new PCI device\n",
  766. pci_name(pdev));
  767. return err;
  768. }
  769. err = pci_request_regions(pdev, KBUILD_MODNAME);
  770. if (err) {
  771. printk(KERN_ERR "%s (rtl8180): Cannot obtain PCI resources\n",
  772. pci_name(pdev));
  773. return err;
  774. }
  775. io_addr = pci_resource_start(pdev, 0);
  776. io_len = pci_resource_len(pdev, 0);
  777. mem_addr = pci_resource_start(pdev, 1);
  778. mem_len = pci_resource_len(pdev, 1);
  779. if (mem_len < sizeof(struct rtl818x_csr) ||
  780. io_len < sizeof(struct rtl818x_csr)) {
  781. printk(KERN_ERR "%s (rtl8180): Too short PCI resources\n",
  782. pci_name(pdev));
  783. err = -ENOMEM;
  784. goto err_free_reg;
  785. }
  786. if ((err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) ||
  787. (err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))) {
  788. printk(KERN_ERR "%s (rtl8180): No suitable DMA available\n",
  789. pci_name(pdev));
  790. goto err_free_reg;
  791. }
  792. pci_set_master(pdev);
  793. dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8180_ops);
  794. if (!dev) {
  795. printk(KERN_ERR "%s (rtl8180): ieee80211 alloc failed\n",
  796. pci_name(pdev));
  797. err = -ENOMEM;
  798. goto err_free_reg;
  799. }
  800. priv = dev->priv;
  801. priv->pdev = pdev;
  802. dev->max_rates = 2;
  803. SET_IEEE80211_DEV(dev, &pdev->dev);
  804. pci_set_drvdata(pdev, dev);
  805. priv->map = pci_iomap(pdev, 1, mem_len);
  806. if (!priv->map)
  807. priv->map = pci_iomap(pdev, 0, io_len);
  808. if (!priv->map) {
  809. printk(KERN_ERR "%s (rtl8180): Cannot map device memory\n",
  810. pci_name(pdev));
  811. goto err_free_dev;
  812. }
  813. BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
  814. BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
  815. memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
  816. memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
  817. priv->band.band = IEEE80211_BAND_2GHZ;
  818. priv->band.channels = priv->channels;
  819. priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
  820. priv->band.bitrates = priv->rates;
  821. priv->band.n_bitrates = 4;
  822. dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  823. dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  824. IEEE80211_HW_RX_INCLUDES_FCS |
  825. IEEE80211_HW_SIGNAL_UNSPEC;
  826. dev->vif_data_size = sizeof(struct rtl8180_vif);
  827. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  828. BIT(NL80211_IFTYPE_ADHOC);
  829. dev->queues = 1;
  830. dev->max_signal = 65;
  831. reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
  832. reg &= RTL818X_TX_CONF_HWVER_MASK;
  833. switch (reg) {
  834. case RTL818X_TX_CONF_R8180_ABCD:
  835. chip_name = "RTL8180";
  836. break;
  837. case RTL818X_TX_CONF_R8180_F:
  838. chip_name = "RTL8180vF";
  839. break;
  840. case RTL818X_TX_CONF_R8185_ABC:
  841. chip_name = "RTL8185";
  842. break;
  843. case RTL818X_TX_CONF_R8185_D:
  844. chip_name = "RTL8185vD";
  845. break;
  846. default:
  847. printk(KERN_ERR "%s (rtl8180): Unknown chip! (0x%x)\n",
  848. pci_name(pdev), reg >> 25);
  849. goto err_iounmap;
  850. }
  851. priv->r8185 = reg & RTL818X_TX_CONF_R8185_ABC;
  852. if (priv->r8185) {
  853. priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
  854. pci_try_set_mwi(pdev);
  855. }
  856. eeprom.data = dev;
  857. eeprom.register_read = rtl8180_eeprom_register_read;
  858. eeprom.register_write = rtl8180_eeprom_register_write;
  859. if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
  860. eeprom.width = PCI_EEPROM_WIDTH_93C66;
  861. else
  862. eeprom.width = PCI_EEPROM_WIDTH_93C46;
  863. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_PROGRAM);
  864. rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  865. udelay(10);
  866. eeprom_93cx6_read(&eeprom, 0x06, &eeprom_val);
  867. eeprom_val &= 0xFF;
  868. switch (eeprom_val) {
  869. case 1: rf_name = "Intersil";
  870. break;
  871. case 2: rf_name = "RFMD";
  872. break;
  873. case 3: priv->rf = &sa2400_rf_ops;
  874. break;
  875. case 4: priv->rf = &max2820_rf_ops;
  876. break;
  877. case 5: priv->rf = &grf5101_rf_ops;
  878. break;
  879. case 9: priv->rf = rtl8180_detect_rf(dev);
  880. break;
  881. case 10:
  882. rf_name = "RTL8255";
  883. break;
  884. default:
  885. printk(KERN_ERR "%s (rtl8180): Unknown RF! (0x%x)\n",
  886. pci_name(pdev), eeprom_val);
  887. goto err_iounmap;
  888. }
  889. if (!priv->rf) {
  890. printk(KERN_ERR "%s (rtl8180): %s RF frontend not supported!\n",
  891. pci_name(pdev), rf_name);
  892. goto err_iounmap;
  893. }
  894. eeprom_93cx6_read(&eeprom, 0x17, &eeprom_val);
  895. priv->csthreshold = eeprom_val >> 8;
  896. if (!priv->r8185) {
  897. __le32 anaparam;
  898. eeprom_93cx6_multiread(&eeprom, 0xD, (__le16 *)&anaparam, 2);
  899. priv->anaparam = le32_to_cpu(anaparam);
  900. eeprom_93cx6_read(&eeprom, 0x19, &priv->rfparam);
  901. }
  902. eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)mac_addr, 3);
  903. if (!is_valid_ether_addr(mac_addr)) {
  904. printk(KERN_WARNING "%s (rtl8180): Invalid hwaddr! Using"
  905. " randomly generated MAC addr\n", pci_name(pdev));
  906. eth_random_addr(mac_addr);
  907. }
  908. SET_IEEE80211_PERM_ADDR(dev, mac_addr);
  909. /* CCK TX power */
  910. for (i = 0; i < 14; i += 2) {
  911. u16 txpwr;
  912. eeprom_93cx6_read(&eeprom, 0x10 + (i >> 1), &txpwr);
  913. priv->channels[i].hw_value = txpwr & 0xFF;
  914. priv->channels[i + 1].hw_value = txpwr >> 8;
  915. }
  916. /* OFDM TX power */
  917. if (priv->r8185) {
  918. for (i = 0; i < 14; i += 2) {
  919. u16 txpwr;
  920. eeprom_93cx6_read(&eeprom, 0x20 + (i >> 1), &txpwr);
  921. priv->channels[i].hw_value |= (txpwr & 0xFF) << 8;
  922. priv->channels[i + 1].hw_value |= txpwr & 0xFF00;
  923. }
  924. }
  925. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  926. spin_lock_init(&priv->lock);
  927. err = ieee80211_register_hw(dev);
  928. if (err) {
  929. printk(KERN_ERR "%s (rtl8180): Cannot register device\n",
  930. pci_name(pdev));
  931. goto err_iounmap;
  932. }
  933. wiphy_info(dev->wiphy, "hwaddr %pm, %s + %s\n",
  934. mac_addr, chip_name, priv->rf->name);
  935. return 0;
  936. err_iounmap:
  937. iounmap(priv->map);
  938. err_free_dev:
  939. ieee80211_free_hw(dev);
  940. err_free_reg:
  941. pci_release_regions(pdev);
  942. pci_disable_device(pdev);
  943. return err;
  944. }
  945. static void rtl8180_remove(struct pci_dev *pdev)
  946. {
  947. struct ieee80211_hw *dev = pci_get_drvdata(pdev);
  948. struct rtl8180_priv *priv;
  949. if (!dev)
  950. return;
  951. ieee80211_unregister_hw(dev);
  952. priv = dev->priv;
  953. pci_iounmap(pdev, priv->map);
  954. pci_release_regions(pdev);
  955. pci_disable_device(pdev);
  956. ieee80211_free_hw(dev);
  957. }
  958. #ifdef CONFIG_PM
  959. static int rtl8180_suspend(struct pci_dev *pdev, pm_message_t state)
  960. {
  961. pci_save_state(pdev);
  962. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  963. return 0;
  964. }
  965. static int rtl8180_resume(struct pci_dev *pdev)
  966. {
  967. pci_set_power_state(pdev, PCI_D0);
  968. pci_restore_state(pdev);
  969. return 0;
  970. }
  971. #endif /* CONFIG_PM */
  972. static struct pci_driver rtl8180_driver = {
  973. .name = KBUILD_MODNAME,
  974. .id_table = rtl8180_table,
  975. .probe = rtl8180_probe,
  976. .remove = rtl8180_remove,
  977. #ifdef CONFIG_PM
  978. .suspend = rtl8180_suspend,
  979. .resume = rtl8180_resume,
  980. #endif /* CONFIG_PM */
  981. };
  982. module_pci_driver(rtl8180_driver);