rtl8180_dev.c 32 KB

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