rtl8180_dev.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  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_tx(struct ieee80211_hw *dev)
  89. {
  90. struct rtl8180_priv *priv = dev->priv;
  91. struct rtl8180_tx_ring *ring;
  92. int prio;
  93. spin_lock(&priv->lock);
  94. for (prio = 3; prio >= 0; prio--) {
  95. ring = &priv->tx_ring[prio];
  96. while (skb_queue_len(&ring->queue)) {
  97. struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
  98. struct sk_buff *skb;
  99. struct ieee80211_tx_info *info;
  100. u32 flags = le32_to_cpu(entry->flags);
  101. if (flags & RTL818X_TX_DESC_FLAG_OWN)
  102. break;
  103. ring->idx = (ring->idx + 1) % ring->entries;
  104. skb = __skb_dequeue(&ring->queue);
  105. pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
  106. skb->len, PCI_DMA_TODEVICE);
  107. info = IEEE80211_SKB_CB(skb);
  108. ieee80211_tx_info_clear_status(info);
  109. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
  110. (flags & RTL818X_TX_DESC_FLAG_TX_OK))
  111. info->flags |= IEEE80211_TX_STAT_ACK;
  112. info->status.rates[0].count = (flags & 0xFF) + 1;
  113. info->status.rates[1].idx = -1;
  114. ieee80211_tx_status(dev, skb);
  115. if (ring->entries - skb_queue_len(&ring->queue) == 2)
  116. ieee80211_wake_queue(dev, prio);
  117. }
  118. }
  119. spin_unlock(&priv->lock);
  120. }
  121. static int rtl8180_poll(struct ieee80211_hw *dev, int budget)
  122. {
  123. struct rtl8180_priv *priv = dev->priv;
  124. unsigned int count = 0;
  125. u8 signal, agc, sq;
  126. /* handle pending Tx queue cleanup */
  127. rtl8180_handle_tx(dev);
  128. while (count++ < budget) {
  129. struct rtl8180_rx_desc *entry = &priv->rx_ring[priv->rx_idx];
  130. struct sk_buff *skb = priv->rx_buf[priv->rx_idx];
  131. u32 flags = le32_to_cpu(entry->flags);
  132. if (flags & RTL818X_RX_DESC_FLAG_OWN)
  133. break;
  134. if (unlikely(flags & (RTL818X_RX_DESC_FLAG_DMA_FAIL |
  135. RTL818X_RX_DESC_FLAG_FOF |
  136. RTL818X_RX_DESC_FLAG_RX_ERR)))
  137. goto done;
  138. else {
  139. u32 flags2 = le32_to_cpu(entry->flags2);
  140. struct ieee80211_rx_status rx_status = {0};
  141. struct sk_buff *new_skb = dev_alloc_skb(MAX_RX_SIZE);
  142. if (unlikely(!new_skb))
  143. goto done;
  144. pci_unmap_single(priv->pdev,
  145. *((dma_addr_t *)skb->cb),
  146. MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
  147. skb_put(skb, flags & 0xFFF);
  148. rx_status.antenna = (flags2 >> 15) & 1;
  149. rx_status.rate_idx = (flags >> 20) & 0xF;
  150. agc = (flags2 >> 17) & 0x7F;
  151. if (priv->r8185) {
  152. if (rx_status.rate_idx > 3)
  153. signal = 90 - clamp_t(u8, agc, 25, 90);
  154. else
  155. signal = 95 - clamp_t(u8, agc, 30, 95);
  156. } else {
  157. sq = flags2 & 0xff;
  158. signal = priv->rf->calc_rssi(agc, sq);
  159. }
  160. rx_status.signal = signal;
  161. rx_status.freq = dev->conf.channel->center_freq;
  162. rx_status.band = dev->conf.channel->band;
  163. rx_status.mactime = le64_to_cpu(entry->tsft);
  164. rx_status.flag |= RX_FLAG_TSFT;
  165. if (flags & RTL818X_RX_DESC_FLAG_CRC32_ERR)
  166. rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
  167. memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
  168. ieee80211_rx(dev, skb);
  169. skb = new_skb;
  170. priv->rx_buf[priv->rx_idx] = skb;
  171. *((dma_addr_t *) skb->cb) =
  172. pci_map_single(priv->pdev, skb_tail_pointer(skb),
  173. MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
  174. }
  175. done:
  176. entry->rx_buf = cpu_to_le32(*((dma_addr_t *)skb->cb));
  177. entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
  178. MAX_RX_SIZE);
  179. if (priv->rx_idx == 31)
  180. entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
  181. priv->rx_idx = (priv->rx_idx + 1) % 32;
  182. }
  183. if (count < budget) {
  184. /* disable polling */
  185. ieee80211_napi_complete(dev);
  186. /* enable interrupts */
  187. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
  188. }
  189. return count;
  190. }
  191. static irqreturn_t rtl8180_interrupt(int irq, void *dev_id)
  192. {
  193. struct ieee80211_hw *dev = dev_id;
  194. struct rtl8180_priv *priv = dev->priv;
  195. u16 reg;
  196. reg = rtl818x_ioread16(priv, &priv->map->INT_STATUS);
  197. if (unlikely(reg == 0xFFFF))
  198. return IRQ_HANDLED;
  199. rtl818x_iowrite16(priv, &priv->map->INT_STATUS, reg);
  200. /* disable interrupts */
  201. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
  202. /* enable polling */
  203. ieee80211_napi_schedule(dev);
  204. return IRQ_HANDLED;
  205. }
  206. static int rtl8180_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
  207. {
  208. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  209. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  210. struct rtl8180_priv *priv = dev->priv;
  211. struct rtl8180_tx_ring *ring;
  212. struct rtl8180_tx_desc *entry;
  213. unsigned int idx, prio;
  214. dma_addr_t mapping;
  215. u32 tx_flags;
  216. u8 rc_flags;
  217. u16 plcp_len = 0;
  218. __le16 rts_duration = 0;
  219. prio = skb_get_queue_mapping(skb);
  220. ring = &priv->tx_ring[prio];
  221. mapping = pci_map_single(priv->pdev, skb->data,
  222. skb->len, PCI_DMA_TODEVICE);
  223. tx_flags = RTL818X_TX_DESC_FLAG_OWN | RTL818X_TX_DESC_FLAG_FS |
  224. RTL818X_TX_DESC_FLAG_LS |
  225. (ieee80211_get_tx_rate(dev, info)->hw_value << 24) |
  226. skb->len;
  227. if (priv->r8185)
  228. tx_flags |= RTL818X_TX_DESC_FLAG_DMA |
  229. RTL818X_TX_DESC_FLAG_NO_ENC;
  230. rc_flags = info->control.rates[0].flags;
  231. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  232. tx_flags |= RTL818X_TX_DESC_FLAG_RTS;
  233. tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
  234. } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  235. tx_flags |= RTL818X_TX_DESC_FLAG_CTS;
  236. tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
  237. }
  238. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS)
  239. rts_duration = ieee80211_rts_duration(dev, priv->vif, skb->len,
  240. info);
  241. if (!priv->r8185) {
  242. unsigned int remainder;
  243. plcp_len = DIV_ROUND_UP(16 * (skb->len + 4),
  244. (ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
  245. remainder = (16 * (skb->len + 4)) %
  246. ((ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
  247. if (remainder <= 6)
  248. plcp_len |= 1 << 15;
  249. }
  250. spin_lock(&priv->lock);
  251. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  252. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  253. priv->seqno += 0x10;
  254. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  255. hdr->seq_ctrl |= cpu_to_le16(priv->seqno);
  256. }
  257. idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
  258. entry = &ring->desc[idx];
  259. entry->rts_duration = rts_duration;
  260. entry->plcp_len = cpu_to_le16(plcp_len);
  261. entry->tx_buf = cpu_to_le32(mapping);
  262. entry->frame_len = cpu_to_le32(skb->len);
  263. entry->flags2 = info->control.rates[1].idx >= 0 ?
  264. ieee80211_get_alt_retry_rate(dev, info, 0)->bitrate << 4 : 0;
  265. entry->retry_limit = info->control.rates[0].count;
  266. entry->flags = cpu_to_le32(tx_flags);
  267. __skb_queue_tail(&ring->queue, skb);
  268. if (ring->entries - skb_queue_len(&ring->queue) < 2)
  269. ieee80211_stop_queue(dev, prio);
  270. spin_unlock(&priv->lock);
  271. rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING, (1 << (prio + 4)));
  272. return 0;
  273. }
  274. void rtl8180_set_anaparam(struct rtl8180_priv *priv, u32 anaparam)
  275. {
  276. u8 reg;
  277. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  278. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  279. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  280. reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
  281. rtl818x_iowrite32(priv, &priv->map->ANAPARAM, anaparam);
  282. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  283. reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
  284. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  285. }
  286. static int rtl8180_init_hw(struct ieee80211_hw *dev)
  287. {
  288. struct rtl8180_priv *priv = dev->priv;
  289. u16 reg;
  290. rtl818x_iowrite8(priv, &priv->map->CMD, 0);
  291. rtl818x_ioread8(priv, &priv->map->CMD);
  292. msleep(10);
  293. /* reset */
  294. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
  295. rtl818x_ioread8(priv, &priv->map->CMD);
  296. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  297. reg &= (1 << 1);
  298. reg |= RTL818X_CMD_RESET;
  299. rtl818x_iowrite8(priv, &priv->map->CMD, RTL818X_CMD_RESET);
  300. rtl818x_ioread8(priv, &priv->map->CMD);
  301. msleep(200);
  302. /* check success of reset */
  303. if (rtl818x_ioread8(priv, &priv->map->CMD) & RTL818X_CMD_RESET) {
  304. wiphy_err(dev->wiphy, "reset timeout!\n");
  305. return -ETIMEDOUT;
  306. }
  307. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
  308. rtl818x_ioread8(priv, &priv->map->CMD);
  309. msleep(200);
  310. if (rtl818x_ioread8(priv, &priv->map->CONFIG3) & (1 << 3)) {
  311. /* For cardbus */
  312. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  313. reg |= 1 << 1;
  314. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
  315. reg = rtl818x_ioread16(priv, &priv->map->FEMR);
  316. reg |= (1 << 15) | (1 << 14) | (1 << 4);
  317. rtl818x_iowrite16(priv, &priv->map->FEMR, reg);
  318. }
  319. rtl818x_iowrite8(priv, &priv->map->MSR, 0);
  320. if (!priv->r8185)
  321. rtl8180_set_anaparam(priv, priv->anaparam);
  322. rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
  323. rtl818x_iowrite32(priv, &priv->map->TBDA, priv->tx_ring[3].dma);
  324. rtl818x_iowrite32(priv, &priv->map->THPDA, priv->tx_ring[2].dma);
  325. rtl818x_iowrite32(priv, &priv->map->TNPDA, priv->tx_ring[1].dma);
  326. rtl818x_iowrite32(priv, &priv->map->TLPDA, priv->tx_ring[0].dma);
  327. /* TODO: necessary? specs indicate not */
  328. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  329. reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
  330. rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg & ~(1 << 3));
  331. if (priv->r8185) {
  332. reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
  333. rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg | (1 << 4));
  334. }
  335. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  336. /* TODO: set CONFIG5 for calibrating AGC on rtl8180 + philips radio? */
  337. /* TODO: turn off hw wep on rtl8180 */
  338. rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
  339. if (priv->r8185) {
  340. rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
  341. rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
  342. rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (8 << 4) | 0);
  343. rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
  344. /* TODO: set ClkRun enable? necessary? */
  345. reg = rtl818x_ioread8(priv, &priv->map->GP_ENABLE);
  346. rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, reg & ~(1 << 6));
  347. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  348. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  349. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | (1 << 2));
  350. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  351. } else {
  352. rtl818x_iowrite16(priv, &priv->map->BRSR, 0x1);
  353. rtl818x_iowrite8(priv, &priv->map->SECURITY, 0);
  354. rtl818x_iowrite8(priv, &priv->map->PHY_DELAY, 0x6);
  355. rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, 0x4C);
  356. }
  357. priv->rf->init(dev);
  358. if (priv->r8185)
  359. rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
  360. return 0;
  361. }
  362. static int rtl8180_init_rx_ring(struct ieee80211_hw *dev)
  363. {
  364. struct rtl8180_priv *priv = dev->priv;
  365. struct rtl8180_rx_desc *entry;
  366. int i;
  367. priv->rx_ring = pci_alloc_consistent(priv->pdev,
  368. sizeof(*priv->rx_ring) * 32,
  369. &priv->rx_ring_dma);
  370. if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
  371. wiphy_err(dev->wiphy, "Cannot allocate RX ring\n");
  372. return -ENOMEM;
  373. }
  374. memset(priv->rx_ring, 0, sizeof(*priv->rx_ring) * 32);
  375. priv->rx_idx = 0;
  376. for (i = 0; i < 32; i++) {
  377. struct sk_buff *skb = dev_alloc_skb(MAX_RX_SIZE);
  378. dma_addr_t *mapping;
  379. entry = &priv->rx_ring[i];
  380. if (!skb)
  381. return 0;
  382. priv->rx_buf[i] = skb;
  383. mapping = (dma_addr_t *)skb->cb;
  384. *mapping = pci_map_single(priv->pdev, skb_tail_pointer(skb),
  385. MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
  386. entry->rx_buf = cpu_to_le32(*mapping);
  387. entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
  388. MAX_RX_SIZE);
  389. }
  390. entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
  391. return 0;
  392. }
  393. static void rtl8180_free_rx_ring(struct ieee80211_hw *dev)
  394. {
  395. struct rtl8180_priv *priv = dev->priv;
  396. int i;
  397. for (i = 0; i < 32; i++) {
  398. struct sk_buff *skb = priv->rx_buf[i];
  399. if (!skb)
  400. continue;
  401. pci_unmap_single(priv->pdev,
  402. *((dma_addr_t *)skb->cb),
  403. MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
  404. kfree_skb(skb);
  405. }
  406. pci_free_consistent(priv->pdev, sizeof(*priv->rx_ring) * 32,
  407. priv->rx_ring, priv->rx_ring_dma);
  408. priv->rx_ring = NULL;
  409. }
  410. static int rtl8180_init_tx_ring(struct ieee80211_hw *dev,
  411. unsigned int prio, unsigned int entries)
  412. {
  413. struct rtl8180_priv *priv = dev->priv;
  414. struct rtl8180_tx_desc *ring;
  415. dma_addr_t dma;
  416. int i;
  417. ring = pci_alloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
  418. if (!ring || (unsigned long)ring & 0xFF) {
  419. wiphy_err(dev->wiphy, "Cannot allocate TX ring (prio = %d)\n",
  420. prio);
  421. return -ENOMEM;
  422. }
  423. memset(ring, 0, sizeof(*ring)*entries);
  424. priv->tx_ring[prio].desc = ring;
  425. priv->tx_ring[prio].dma = dma;
  426. priv->tx_ring[prio].idx = 0;
  427. priv->tx_ring[prio].entries = entries;
  428. skb_queue_head_init(&priv->tx_ring[prio].queue);
  429. for (i = 0; i < entries; i++)
  430. ring[i].next_tx_desc =
  431. cpu_to_le32((u32)dma + ((i + 1) % entries) * sizeof(*ring));
  432. return 0;
  433. }
  434. static void rtl8180_free_tx_ring(struct ieee80211_hw *dev, unsigned int prio)
  435. {
  436. struct rtl8180_priv *priv = dev->priv;
  437. struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
  438. while (skb_queue_len(&ring->queue)) {
  439. struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
  440. struct sk_buff *skb = __skb_dequeue(&ring->queue);
  441. pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
  442. skb->len, PCI_DMA_TODEVICE);
  443. kfree_skb(skb);
  444. ring->idx = (ring->idx + 1) % ring->entries;
  445. }
  446. pci_free_consistent(priv->pdev, sizeof(*ring->desc)*ring->entries,
  447. ring->desc, ring->dma);
  448. ring->desc = NULL;
  449. }
  450. static int rtl8180_start(struct ieee80211_hw *dev)
  451. {
  452. struct rtl8180_priv *priv = dev->priv;
  453. int ret, i;
  454. u32 reg;
  455. ret = rtl8180_init_rx_ring(dev);
  456. if (ret)
  457. return ret;
  458. for (i = 0; i < 4; i++)
  459. if ((ret = rtl8180_init_tx_ring(dev, i, 16)))
  460. goto err_free_rings;
  461. ret = rtl8180_init_hw(dev);
  462. if (ret)
  463. goto err_free_rings;
  464. rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
  465. rtl818x_iowrite32(priv, &priv->map->TBDA, priv->tx_ring[3].dma);
  466. rtl818x_iowrite32(priv, &priv->map->THPDA, priv->tx_ring[2].dma);
  467. rtl818x_iowrite32(priv, &priv->map->TNPDA, priv->tx_ring[1].dma);
  468. rtl818x_iowrite32(priv, &priv->map->TLPDA, priv->tx_ring[0].dma);
  469. ret = request_irq(priv->pdev->irq, rtl8180_interrupt,
  470. IRQF_SHARED, KBUILD_MODNAME, dev);
  471. if (ret) {
  472. wiphy_err(dev->wiphy, "failed to register IRQ handler\n");
  473. goto err_free_rings;
  474. }
  475. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
  476. rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
  477. rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
  478. reg = RTL818X_RX_CONF_ONLYERLPKT |
  479. RTL818X_RX_CONF_RX_AUTORESETPHY |
  480. RTL818X_RX_CONF_MGMT |
  481. RTL818X_RX_CONF_DATA |
  482. (7 << 8 /* MAX RX DMA */) |
  483. RTL818X_RX_CONF_BROADCAST |
  484. RTL818X_RX_CONF_NICMAC;
  485. if (priv->r8185)
  486. reg |= RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2;
  487. else {
  488. reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE1)
  489. ? RTL818X_RX_CONF_CSDM1 : 0;
  490. reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE2)
  491. ? RTL818X_RX_CONF_CSDM2 : 0;
  492. }
  493. priv->rx_conf = reg;
  494. rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
  495. if (priv->r8185) {
  496. reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
  497. reg &= ~RTL818X_CW_CONF_PERPACKET_CW_SHIFT;
  498. reg |= RTL818X_CW_CONF_PERPACKET_RETRY_SHIFT;
  499. rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
  500. reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
  501. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN_SHIFT;
  502. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT;
  503. reg |= RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
  504. rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
  505. /* disable early TX */
  506. rtl818x_iowrite8(priv, (u8 __iomem *)priv->map + 0xec, 0x3f);
  507. }
  508. reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
  509. reg |= (6 << 21 /* MAX TX DMA */) |
  510. RTL818X_TX_CONF_NO_ICV;
  511. if (priv->r8185)
  512. reg &= ~RTL818X_TX_CONF_PROBE_DTS;
  513. else
  514. reg &= ~RTL818X_TX_CONF_HW_SEQNUM;
  515. /* different meaning, same value on both rtl8185 and rtl8180 */
  516. reg &= ~RTL818X_TX_CONF_SAT_HWPLCP;
  517. rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
  518. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  519. reg |= RTL818X_CMD_RX_ENABLE;
  520. reg |= RTL818X_CMD_TX_ENABLE;
  521. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  522. return 0;
  523. err_free_rings:
  524. rtl8180_free_rx_ring(dev);
  525. for (i = 0; i < 4; i++)
  526. if (priv->tx_ring[i].desc)
  527. rtl8180_free_tx_ring(dev, i);
  528. return ret;
  529. }
  530. static void rtl8180_stop(struct ieee80211_hw *dev)
  531. {
  532. struct rtl8180_priv *priv = dev->priv;
  533. u8 reg;
  534. int i;
  535. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
  536. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  537. reg &= ~RTL818X_CMD_TX_ENABLE;
  538. reg &= ~RTL818X_CMD_RX_ENABLE;
  539. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  540. priv->rf->stop(dev);
  541. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  542. reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
  543. rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
  544. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  545. free_irq(priv->pdev->irq, dev);
  546. rtl8180_free_rx_ring(dev);
  547. for (i = 0; i < 4; i++)
  548. rtl8180_free_tx_ring(dev, i);
  549. }
  550. static u64 rtl8180_get_tsf(struct ieee80211_hw *dev)
  551. {
  552. struct rtl8180_priv *priv = dev->priv;
  553. return rtl818x_ioread32(priv, &priv->map->TSFT[0]) |
  554. (u64)(rtl818x_ioread32(priv, &priv->map->TSFT[1])) << 32;
  555. }
  556. static void rtl8180_beacon_work(struct work_struct *work)
  557. {
  558. struct rtl8180_vif *vif_priv =
  559. container_of(work, struct rtl8180_vif, beacon_work.work);
  560. struct ieee80211_vif *vif =
  561. container_of((void *)vif_priv, struct ieee80211_vif, drv_priv);
  562. struct ieee80211_hw *dev = vif_priv->dev;
  563. struct ieee80211_mgmt *mgmt;
  564. struct sk_buff *skb;
  565. int err = 0;
  566. /* don't overflow the tx ring */
  567. if (ieee80211_queue_stopped(dev, 0))
  568. goto resched;
  569. /* grab a fresh beacon */
  570. skb = ieee80211_beacon_get(dev, vif);
  571. if (!skb)
  572. goto resched;
  573. /*
  574. * update beacon timestamp w/ TSF value
  575. * TODO: make hardware update beacon timestamp
  576. */
  577. mgmt = (struct ieee80211_mgmt *)skb->data;
  578. mgmt->u.beacon.timestamp = cpu_to_le64(rtl8180_get_tsf(dev));
  579. /* TODO: use actual beacon queue */
  580. skb_set_queue_mapping(skb, 0);
  581. err = rtl8180_tx(dev, skb);
  582. WARN_ON(err);
  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. .napi_poll = rtl8180_poll,
  712. };
  713. static void rtl8180_eeprom_register_read(struct eeprom_93cx6 *eeprom)
  714. {
  715. struct ieee80211_hw *dev = eeprom->data;
  716. struct rtl8180_priv *priv = dev->priv;
  717. u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  718. eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
  719. eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
  720. eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
  721. eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
  722. }
  723. static void rtl8180_eeprom_register_write(struct eeprom_93cx6 *eeprom)
  724. {
  725. struct ieee80211_hw *dev = eeprom->data;
  726. struct rtl8180_priv *priv = dev->priv;
  727. u8 reg = 2 << 6;
  728. if (eeprom->reg_data_in)
  729. reg |= RTL818X_EEPROM_CMD_WRITE;
  730. if (eeprom->reg_data_out)
  731. reg |= RTL818X_EEPROM_CMD_READ;
  732. if (eeprom->reg_data_clock)
  733. reg |= RTL818X_EEPROM_CMD_CK;
  734. if (eeprom->reg_chip_select)
  735. reg |= RTL818X_EEPROM_CMD_CS;
  736. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
  737. rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  738. udelay(10);
  739. }
  740. static int __devinit rtl8180_probe(struct pci_dev *pdev,
  741. const struct pci_device_id *id)
  742. {
  743. struct ieee80211_hw *dev;
  744. struct rtl8180_priv *priv;
  745. unsigned long mem_addr, mem_len;
  746. unsigned int io_addr, io_len;
  747. int err, i;
  748. struct eeprom_93cx6 eeprom;
  749. const char *chip_name, *rf_name = NULL;
  750. u32 reg;
  751. u16 eeprom_val;
  752. u8 mac_addr[ETH_ALEN];
  753. err = pci_enable_device(pdev);
  754. if (err) {
  755. printk(KERN_ERR "%s (rtl8180): Cannot enable new PCI device\n",
  756. pci_name(pdev));
  757. return err;
  758. }
  759. err = pci_request_regions(pdev, KBUILD_MODNAME);
  760. if (err) {
  761. printk(KERN_ERR "%s (rtl8180): Cannot obtain PCI resources\n",
  762. pci_name(pdev));
  763. return err;
  764. }
  765. io_addr = pci_resource_start(pdev, 0);
  766. io_len = pci_resource_len(pdev, 0);
  767. mem_addr = pci_resource_start(pdev, 1);
  768. mem_len = pci_resource_len(pdev, 1);
  769. if (mem_len < sizeof(struct rtl818x_csr) ||
  770. io_len < sizeof(struct rtl818x_csr)) {
  771. printk(KERN_ERR "%s (rtl8180): Too short PCI resources\n",
  772. pci_name(pdev));
  773. err = -ENOMEM;
  774. goto err_free_reg;
  775. }
  776. if ((err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) ||
  777. (err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))) {
  778. printk(KERN_ERR "%s (rtl8180): No suitable DMA available\n",
  779. pci_name(pdev));
  780. goto err_free_reg;
  781. }
  782. pci_set_master(pdev);
  783. dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8180_ops);
  784. if (!dev) {
  785. printk(KERN_ERR "%s (rtl8180): ieee80211 alloc failed\n",
  786. pci_name(pdev));
  787. err = -ENOMEM;
  788. goto err_free_reg;
  789. }
  790. priv = dev->priv;
  791. priv->pdev = pdev;
  792. dev->max_rates = 2;
  793. SET_IEEE80211_DEV(dev, &pdev->dev);
  794. pci_set_drvdata(pdev, dev);
  795. priv->map = pci_iomap(pdev, 1, mem_len);
  796. if (!priv->map)
  797. priv->map = pci_iomap(pdev, 0, io_len);
  798. if (!priv->map) {
  799. printk(KERN_ERR "%s (rtl8180): Cannot map device memory\n",
  800. pci_name(pdev));
  801. goto err_free_dev;
  802. }
  803. BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
  804. BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
  805. memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
  806. memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
  807. priv->band.band = IEEE80211_BAND_2GHZ;
  808. priv->band.channels = priv->channels;
  809. priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
  810. priv->band.bitrates = priv->rates;
  811. priv->band.n_bitrates = 4;
  812. dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  813. dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  814. IEEE80211_HW_RX_INCLUDES_FCS |
  815. IEEE80211_HW_SIGNAL_UNSPEC;
  816. dev->vif_data_size = sizeof(struct rtl8180_vif);
  817. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  818. BIT(NL80211_IFTYPE_ADHOC);
  819. dev->queues = 1;
  820. dev->max_signal = 65;
  821. dev->napi_weight = 64;
  822. reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
  823. reg &= RTL818X_TX_CONF_HWVER_MASK;
  824. switch (reg) {
  825. case RTL818X_TX_CONF_R8180_ABCD:
  826. chip_name = "RTL8180";
  827. break;
  828. case RTL818X_TX_CONF_R8180_F:
  829. chip_name = "RTL8180vF";
  830. break;
  831. case RTL818X_TX_CONF_R8185_ABC:
  832. chip_name = "RTL8185";
  833. break;
  834. case RTL818X_TX_CONF_R8185_D:
  835. chip_name = "RTL8185vD";
  836. break;
  837. default:
  838. printk(KERN_ERR "%s (rtl8180): Unknown chip! (0x%x)\n",
  839. pci_name(pdev), reg >> 25);
  840. goto err_iounmap;
  841. }
  842. priv->r8185 = reg & RTL818X_TX_CONF_R8185_ABC;
  843. if (priv->r8185) {
  844. priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
  845. pci_try_set_mwi(pdev);
  846. }
  847. eeprom.data = dev;
  848. eeprom.register_read = rtl8180_eeprom_register_read;
  849. eeprom.register_write = rtl8180_eeprom_register_write;
  850. if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
  851. eeprom.width = PCI_EEPROM_WIDTH_93C66;
  852. else
  853. eeprom.width = PCI_EEPROM_WIDTH_93C46;
  854. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_PROGRAM);
  855. rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  856. udelay(10);
  857. eeprom_93cx6_read(&eeprom, 0x06, &eeprom_val);
  858. eeprom_val &= 0xFF;
  859. switch (eeprom_val) {
  860. case 1: rf_name = "Intersil";
  861. break;
  862. case 2: rf_name = "RFMD";
  863. break;
  864. case 3: priv->rf = &sa2400_rf_ops;
  865. break;
  866. case 4: priv->rf = &max2820_rf_ops;
  867. break;
  868. case 5: priv->rf = &grf5101_rf_ops;
  869. break;
  870. case 9: priv->rf = rtl8180_detect_rf(dev);
  871. break;
  872. case 10:
  873. rf_name = "RTL8255";
  874. break;
  875. default:
  876. printk(KERN_ERR "%s (rtl8180): Unknown RF! (0x%x)\n",
  877. pci_name(pdev), eeprom_val);
  878. goto err_iounmap;
  879. }
  880. if (!priv->rf) {
  881. printk(KERN_ERR "%s (rtl8180): %s RF frontend not supported!\n",
  882. pci_name(pdev), rf_name);
  883. goto err_iounmap;
  884. }
  885. eeprom_93cx6_read(&eeprom, 0x17, &eeprom_val);
  886. priv->csthreshold = eeprom_val >> 8;
  887. if (!priv->r8185) {
  888. __le32 anaparam;
  889. eeprom_93cx6_multiread(&eeprom, 0xD, (__le16 *)&anaparam, 2);
  890. priv->anaparam = le32_to_cpu(anaparam);
  891. eeprom_93cx6_read(&eeprom, 0x19, &priv->rfparam);
  892. }
  893. eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)mac_addr, 3);
  894. if (!is_valid_ether_addr(mac_addr)) {
  895. printk(KERN_WARNING "%s (rtl8180): Invalid hwaddr! Using"
  896. " randomly generated MAC addr\n", pci_name(pdev));
  897. random_ether_addr(mac_addr);
  898. }
  899. SET_IEEE80211_PERM_ADDR(dev, mac_addr);
  900. /* CCK TX power */
  901. for (i = 0; i < 14; i += 2) {
  902. u16 txpwr;
  903. eeprom_93cx6_read(&eeprom, 0x10 + (i >> 1), &txpwr);
  904. priv->channels[i].hw_value = txpwr & 0xFF;
  905. priv->channels[i + 1].hw_value = txpwr >> 8;
  906. }
  907. /* OFDM TX power */
  908. if (priv->r8185) {
  909. for (i = 0; i < 14; i += 2) {
  910. u16 txpwr;
  911. eeprom_93cx6_read(&eeprom, 0x20 + (i >> 1), &txpwr);
  912. priv->channels[i].hw_value |= (txpwr & 0xFF) << 8;
  913. priv->channels[i + 1].hw_value |= txpwr & 0xFF00;
  914. }
  915. }
  916. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  917. spin_lock_init(&priv->lock);
  918. err = ieee80211_register_hw(dev);
  919. if (err) {
  920. printk(KERN_ERR "%s (rtl8180): Cannot register device\n",
  921. pci_name(pdev));
  922. goto err_iounmap;
  923. }
  924. wiphy_info(dev->wiphy, "hwaddr %pm, %s + %s\n",
  925. mac_addr, chip_name, priv->rf->name);
  926. return 0;
  927. err_iounmap:
  928. iounmap(priv->map);
  929. err_free_dev:
  930. pci_set_drvdata(pdev, NULL);
  931. ieee80211_free_hw(dev);
  932. err_free_reg:
  933. pci_release_regions(pdev);
  934. pci_disable_device(pdev);
  935. return err;
  936. }
  937. static void __devexit rtl8180_remove(struct pci_dev *pdev)
  938. {
  939. struct ieee80211_hw *dev = pci_get_drvdata(pdev);
  940. struct rtl8180_priv *priv;
  941. if (!dev)
  942. return;
  943. ieee80211_unregister_hw(dev);
  944. priv = dev->priv;
  945. pci_iounmap(pdev, priv->map);
  946. pci_release_regions(pdev);
  947. pci_disable_device(pdev);
  948. ieee80211_free_hw(dev);
  949. }
  950. #ifdef CONFIG_PM
  951. static int rtl8180_suspend(struct pci_dev *pdev, pm_message_t state)
  952. {
  953. pci_save_state(pdev);
  954. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  955. return 0;
  956. }
  957. static int rtl8180_resume(struct pci_dev *pdev)
  958. {
  959. pci_set_power_state(pdev, PCI_D0);
  960. pci_restore_state(pdev);
  961. return 0;
  962. }
  963. #endif /* CONFIG_PM */
  964. static struct pci_driver rtl8180_driver = {
  965. .name = KBUILD_MODNAME,
  966. .id_table = rtl8180_table,
  967. .probe = rtl8180_probe,
  968. .remove = __devexit_p(rtl8180_remove),
  969. #ifdef CONFIG_PM
  970. .suspend = rtl8180_suspend,
  971. .resume = rtl8180_resume,
  972. #endif /* CONFIG_PM */
  973. };
  974. static int __init rtl8180_init(void)
  975. {
  976. return pci_register_driver(&rtl8180_driver);
  977. }
  978. static void __exit rtl8180_exit(void)
  979. {
  980. pci_unregister_driver(&rtl8180_driver);
  981. }
  982. module_init(rtl8180_init);
  983. module_exit(rtl8180_exit);