dev.c 32 KB

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