dev.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925
  1. /* Linux device driver for RTL8180 / RTL8185 / RTL8187SE
  2. *
  3. * Copyright 2007 Michael Wu <flamingice@sourmilk.net>
  4. * Copyright 2007,2014 Andrea Merello <andrea.merello@gmail.com>
  5. *
  6. * Based on the r8180 driver, which is:
  7. * Copyright 2004-2005 Andrea Merello <andrea.merello@gmail.com>, et al.
  8. *
  9. * Thanks to Realtek for their support!
  10. *
  11. ************************************************************************
  12. *
  13. * The driver was extended to the RTL8187SE in 2014 by
  14. * Andrea Merello <andrea.merello@gmail.com>
  15. *
  16. * based also on:
  17. * - portions of rtl8187se Linux staging driver, Copyright Realtek corp.
  18. * - other GPL, unpublished (until now), Linux driver code,
  19. * Copyright Larry Finger <Larry.Finger@lwfinger.net>
  20. *
  21. * A huge thanks goes to Sara V. Nari who forgives me when I'm
  22. * sitting in front of my laptop at evening, week-end, night...
  23. *
  24. * A special thanks goes to Antonio Cuni, who helped me with
  25. * some python userspace stuff I used to debug RTL8187SE code, and who
  26. * bought a laptop with an unsupported Wi-Fi card some years ago...
  27. *
  28. * Thanks to Larry Finger for writing some code for rtl8187se and for
  29. * his suggestions.
  30. *
  31. * Thanks to Dan Carpenter for reviewing my initial patch and for his
  32. * suggestions.
  33. *
  34. * Thanks to Bernhard Schiffner for his help in testing and for his
  35. * suggestions.
  36. *
  37. ************************************************************************
  38. *
  39. * This program is free software; you can redistribute it and/or modify
  40. * it under the terms of the GNU General Public License version 2 as
  41. * published by the Free Software Foundation.
  42. */
  43. #include <linux/interrupt.h>
  44. #include <linux/pci.h>
  45. #include <linux/slab.h>
  46. #include <linux/delay.h>
  47. #include <linux/etherdevice.h>
  48. #include <linux/eeprom_93cx6.h>
  49. #include <linux/module.h>
  50. #include <net/mac80211.h>
  51. #include "rtl8180.h"
  52. #include "rtl8225.h"
  53. #include "sa2400.h"
  54. #include "max2820.h"
  55. #include "grf5101.h"
  56. #include "rtl8225se.h"
  57. MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
  58. MODULE_AUTHOR("Andrea Merello <andrea.merello@gmail.com>");
  59. MODULE_DESCRIPTION("RTL8180 / RTL8185 / RTL8187SE PCI wireless driver");
  60. MODULE_LICENSE("GPL");
  61. static DEFINE_PCI_DEVICE_TABLE(rtl8180_table) = {
  62. /* rtl8187se */
  63. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8199) },
  64. /* rtl8185 */
  65. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8185) },
  66. { PCI_DEVICE(PCI_VENDOR_ID_BELKIN, 0x700f) },
  67. { PCI_DEVICE(PCI_VENDOR_ID_BELKIN, 0x701f) },
  68. /* rtl8180 */
  69. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8180) },
  70. { PCI_DEVICE(0x1799, 0x6001) },
  71. { PCI_DEVICE(0x1799, 0x6020) },
  72. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x3300) },
  73. { PCI_DEVICE(0x1186, 0x3301) },
  74. { PCI_DEVICE(0x1432, 0x7106) },
  75. { }
  76. };
  77. MODULE_DEVICE_TABLE(pci, rtl8180_table);
  78. static const struct ieee80211_rate rtl818x_rates[] = {
  79. { .bitrate = 10, .hw_value = 0, },
  80. { .bitrate = 20, .hw_value = 1, },
  81. { .bitrate = 55, .hw_value = 2, },
  82. { .bitrate = 110, .hw_value = 3, },
  83. { .bitrate = 60, .hw_value = 4, },
  84. { .bitrate = 90, .hw_value = 5, },
  85. { .bitrate = 120, .hw_value = 6, },
  86. { .bitrate = 180, .hw_value = 7, },
  87. { .bitrate = 240, .hw_value = 8, },
  88. { .bitrate = 360, .hw_value = 9, },
  89. { .bitrate = 480, .hw_value = 10, },
  90. { .bitrate = 540, .hw_value = 11, },
  91. };
  92. static const struct ieee80211_channel rtl818x_channels[] = {
  93. { .center_freq = 2412 },
  94. { .center_freq = 2417 },
  95. { .center_freq = 2422 },
  96. { .center_freq = 2427 },
  97. { .center_freq = 2432 },
  98. { .center_freq = 2437 },
  99. { .center_freq = 2442 },
  100. { .center_freq = 2447 },
  101. { .center_freq = 2452 },
  102. { .center_freq = 2457 },
  103. { .center_freq = 2462 },
  104. { .center_freq = 2467 },
  105. { .center_freq = 2472 },
  106. { .center_freq = 2484 },
  107. };
  108. /* Queues for rtl8187se card
  109. *
  110. * name | reg | queue
  111. * BC | 7 | 6
  112. * MG | 1 | 0
  113. * HI | 6 | 1
  114. * VO | 5 | 2
  115. * VI | 4 | 3
  116. * BE | 3 | 4
  117. * BK | 2 | 5
  118. *
  119. * The complete map for DMA kick reg using use all queue is:
  120. * static const int rtl8187se_queues_map[RTL8187SE_NR_TX_QUEUES] =
  121. * {1, 6, 5, 4, 3, 2, 7};
  122. *
  123. * .. but.. Because for mac80211 4 queues are enough for QoS we use this
  124. *
  125. * name | reg | queue
  126. * BC | 7 | 4 <- currently not used yet
  127. * MG | 1 | x <- Not used
  128. * HI | 6 | x <- Not used
  129. * VO | 5 | 0 <- used
  130. * VI | 4 | 1 <- used
  131. * BE | 3 | 2 <- used
  132. * BK | 2 | 3 <- used
  133. *
  134. * Beacon queue could be used, but this is not finished yet.
  135. *
  136. * I thougth about using the other two queues but I decided not to do this:
  137. *
  138. * - I'm unsure whether the mac80211 will ever try to use more than 4 queues
  139. * by itself.
  140. *
  141. * - I could route MGMT frames (currently sent over VO queue) to the MGMT
  142. * queue but since mac80211 will do not know about it, I will probably gain
  143. * some HW priority whenever the VO queue is not empty, but this gain is
  144. * limited by the fact that I had to stop the mac80211 queue whenever one of
  145. * the VO or MGMT queues is full, stopping also submitting of MGMT frame
  146. * to the driver.
  147. *
  148. * - I don't know how to set in the HW the contention window params for MGMT
  149. * and HI-prio queues.
  150. */
  151. static const int rtl8187se_queues_map[RTL8187SE_NR_TX_QUEUES] = {5, 4, 3, 2, 7};
  152. /* Queues for rtl8180/rtl8185 cards
  153. *
  154. * name | reg | prio
  155. * BC | 7 | 3
  156. * HI | 6 | 0
  157. * NO | 5 | 1
  158. * LO | 4 | 2
  159. *
  160. * The complete map for DMA kick reg using all queue is:
  161. * static const int rtl8180_queues_map[RTL8180_NR_TX_QUEUES] = {6, 5, 4, 7};
  162. *
  163. * .. but .. Because the mac80211 needs at least 4 queues for QoS or
  164. * otherwise QoS can't be done, we use just one.
  165. * Beacon queue could be used, but this is not finished yet.
  166. * Actual map is:
  167. *
  168. * name | reg | prio
  169. * BC | 7 | 1 <- currently not used yet.
  170. * HI | 6 | x <- not used
  171. * NO | 5 | x <- not used
  172. * LO | 4 | 0 <- used
  173. */
  174. static const int rtl8180_queues_map[RTL8180_NR_TX_QUEUES] = {4, 7};
  175. void rtl8180_write_phy(struct ieee80211_hw *dev, u8 addr, u32 data)
  176. {
  177. struct rtl8180_priv *priv = dev->priv;
  178. int i = 10;
  179. u32 buf;
  180. buf = (data << 8) | addr;
  181. rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->PHY[0], buf | 0x80);
  182. while (i--) {
  183. rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->PHY[0], buf);
  184. if (rtl818x_ioread8(priv, &priv->map->PHY[2]) == (data & 0xFF))
  185. return;
  186. }
  187. }
  188. static void rtl8180_handle_rx(struct ieee80211_hw *dev)
  189. {
  190. struct rtl8180_priv *priv = dev->priv;
  191. struct rtl818x_rx_cmd_desc *cmd_desc;
  192. unsigned int count = 32;
  193. u8 signal, agc, sq;
  194. dma_addr_t mapping;
  195. while (count--) {
  196. void *entry = priv->rx_ring + priv->rx_idx * priv->rx_ring_sz;
  197. struct sk_buff *skb = priv->rx_buf[priv->rx_idx];
  198. u32 flags, flags2;
  199. u64 tsft;
  200. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  201. struct rtl8187se_rx_desc *desc = entry;
  202. flags = le32_to_cpu(desc->flags);
  203. flags2 = le32_to_cpu(desc->flags2);
  204. tsft = le64_to_cpu(desc->tsft);
  205. } else {
  206. struct rtl8180_rx_desc *desc = entry;
  207. flags = le32_to_cpu(desc->flags);
  208. flags2 = le32_to_cpu(desc->flags2);
  209. tsft = le64_to_cpu(desc->tsft);
  210. }
  211. if (flags & RTL818X_RX_DESC_FLAG_OWN)
  212. return;
  213. if (unlikely(flags & (RTL818X_RX_DESC_FLAG_DMA_FAIL |
  214. RTL818X_RX_DESC_FLAG_FOF |
  215. RTL818X_RX_DESC_FLAG_RX_ERR)))
  216. goto done;
  217. else {
  218. struct ieee80211_rx_status rx_status = {0};
  219. struct sk_buff *new_skb = dev_alloc_skb(MAX_RX_SIZE);
  220. if (unlikely(!new_skb))
  221. goto done;
  222. mapping = pci_map_single(priv->pdev,
  223. skb_tail_pointer(new_skb),
  224. MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
  225. if (pci_dma_mapping_error(priv->pdev, mapping)) {
  226. kfree_skb(new_skb);
  227. dev_err(&priv->pdev->dev, "RX DMA map error\n");
  228. goto done;
  229. }
  230. pci_unmap_single(priv->pdev,
  231. *((dma_addr_t *)skb->cb),
  232. MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
  233. skb_put(skb, flags & 0xFFF);
  234. rx_status.antenna = (flags2 >> 15) & 1;
  235. rx_status.rate_idx = (flags >> 20) & 0xF;
  236. agc = (flags2 >> 17) & 0x7F;
  237. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
  238. if (rx_status.rate_idx > 3)
  239. signal = 90 - clamp_t(u8, agc, 25, 90);
  240. else
  241. signal = 95 - clamp_t(u8, agc, 30, 95);
  242. } else if (priv->chip_family ==
  243. RTL818X_CHIP_FAMILY_RTL8180) {
  244. sq = flags2 & 0xff;
  245. signal = priv->rf->calc_rssi(agc, sq);
  246. } else {
  247. /* TODO: rtl8187se rssi */
  248. signal = 10;
  249. }
  250. rx_status.signal = signal;
  251. rx_status.freq = dev->conf.chandef.chan->center_freq;
  252. rx_status.band = dev->conf.chandef.chan->band;
  253. rx_status.mactime = tsft;
  254. rx_status.flag |= RX_FLAG_MACTIME_START;
  255. if (flags & RTL818X_RX_DESC_FLAG_SPLCP)
  256. rx_status.flag |= RX_FLAG_SHORTPRE;
  257. if (flags & RTL818X_RX_DESC_FLAG_CRC32_ERR)
  258. rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
  259. memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
  260. ieee80211_rx_irqsafe(dev, skb);
  261. skb = new_skb;
  262. priv->rx_buf[priv->rx_idx] = skb;
  263. *((dma_addr_t *) skb->cb) = mapping;
  264. }
  265. done:
  266. cmd_desc = entry;
  267. cmd_desc->rx_buf = cpu_to_le32(*((dma_addr_t *)skb->cb));
  268. cmd_desc->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
  269. MAX_RX_SIZE);
  270. if (priv->rx_idx == 31)
  271. cmd_desc->flags |=
  272. cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
  273. priv->rx_idx = (priv->rx_idx + 1) % 32;
  274. }
  275. }
  276. static void rtl8180_handle_tx(struct ieee80211_hw *dev, unsigned int prio)
  277. {
  278. struct rtl8180_priv *priv = dev->priv;
  279. struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
  280. while (skb_queue_len(&ring->queue)) {
  281. struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
  282. struct sk_buff *skb;
  283. struct ieee80211_tx_info *info;
  284. u32 flags = le32_to_cpu(entry->flags);
  285. if (flags & RTL818X_TX_DESC_FLAG_OWN)
  286. return;
  287. ring->idx = (ring->idx + 1) % ring->entries;
  288. skb = __skb_dequeue(&ring->queue);
  289. pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
  290. skb->len, PCI_DMA_TODEVICE);
  291. info = IEEE80211_SKB_CB(skb);
  292. ieee80211_tx_info_clear_status(info);
  293. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
  294. (flags & RTL818X_TX_DESC_FLAG_TX_OK))
  295. info->flags |= IEEE80211_TX_STAT_ACK;
  296. info->status.rates[0].count = (flags & 0xFF) + 1;
  297. info->status.rates[1].idx = -1;
  298. ieee80211_tx_status_irqsafe(dev, skb);
  299. if (ring->entries - skb_queue_len(&ring->queue) == 2)
  300. ieee80211_wake_queue(dev, prio);
  301. }
  302. }
  303. static irqreturn_t rtl8187se_interrupt(int irq, void *dev_id)
  304. {
  305. struct ieee80211_hw *dev = dev_id;
  306. struct rtl8180_priv *priv = dev->priv;
  307. u32 reg;
  308. unsigned long flags;
  309. static int desc_err;
  310. spin_lock_irqsave(&priv->lock, flags);
  311. /* Note: 32-bit interrupt status */
  312. reg = rtl818x_ioread32(priv, &priv->map->INT_STATUS_SE);
  313. if (unlikely(reg == 0xFFFFFFFF)) {
  314. spin_unlock_irqrestore(&priv->lock, flags);
  315. return IRQ_HANDLED;
  316. }
  317. rtl818x_iowrite32(priv, &priv->map->INT_STATUS_SE, reg);
  318. if (reg & IMR_TIMEOUT1)
  319. rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
  320. if (reg & (IMR_TBDOK | IMR_TBDER))
  321. rtl8180_handle_tx(dev, 4);
  322. if (reg & (IMR_TVODOK | IMR_TVODER))
  323. rtl8180_handle_tx(dev, 0);
  324. if (reg & (IMR_TVIDOK | IMR_TVIDER))
  325. rtl8180_handle_tx(dev, 1);
  326. if (reg & (IMR_TBEDOK | IMR_TBEDER))
  327. rtl8180_handle_tx(dev, 2);
  328. if (reg & (IMR_TBKDOK | IMR_TBKDER))
  329. rtl8180_handle_tx(dev, 3);
  330. if (reg & (IMR_ROK | IMR_RER | RTL818X_INT_SE_RX_DU | IMR_RQOSOK))
  331. rtl8180_handle_rx(dev);
  332. /* The interface sometimes generates several RX DMA descriptor errors
  333. * at startup. Do not report these.
  334. */
  335. if ((reg & RTL818X_INT_SE_RX_DU) && desc_err++ > 2)
  336. if (net_ratelimit())
  337. wiphy_err(dev->wiphy, "No RX DMA Descriptor avail\n");
  338. spin_unlock_irqrestore(&priv->lock, flags);
  339. return IRQ_HANDLED;
  340. }
  341. static irqreturn_t rtl8180_interrupt(int irq, void *dev_id)
  342. {
  343. struct ieee80211_hw *dev = dev_id;
  344. struct rtl8180_priv *priv = dev->priv;
  345. u16 reg;
  346. spin_lock(&priv->lock);
  347. reg = rtl818x_ioread16(priv, &priv->map->INT_STATUS);
  348. if (unlikely(reg == 0xFFFF)) {
  349. spin_unlock(&priv->lock);
  350. return IRQ_HANDLED;
  351. }
  352. rtl818x_iowrite16(priv, &priv->map->INT_STATUS, reg);
  353. if (reg & (RTL818X_INT_TXB_OK | RTL818X_INT_TXB_ERR))
  354. rtl8180_handle_tx(dev, 1);
  355. if (reg & (RTL818X_INT_TXL_OK | RTL818X_INT_TXL_ERR))
  356. rtl8180_handle_tx(dev, 0);
  357. if (reg & (RTL818X_INT_RX_OK | RTL818X_INT_RX_ERR))
  358. rtl8180_handle_rx(dev);
  359. spin_unlock(&priv->lock);
  360. return IRQ_HANDLED;
  361. }
  362. static void rtl8180_tx(struct ieee80211_hw *dev,
  363. struct ieee80211_tx_control *control,
  364. struct sk_buff *skb)
  365. {
  366. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  367. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  368. struct rtl8180_priv *priv = dev->priv;
  369. struct rtl8180_tx_ring *ring;
  370. struct rtl8180_tx_desc *entry;
  371. unsigned long flags;
  372. unsigned int idx, prio, hw_prio;
  373. dma_addr_t mapping;
  374. u32 tx_flags;
  375. u8 rc_flags;
  376. u16 plcp_len = 0;
  377. __le16 rts_duration = 0;
  378. /* do arithmetic and then convert to le16 */
  379. u16 frame_duration = 0;
  380. prio = skb_get_queue_mapping(skb);
  381. ring = &priv->tx_ring[prio];
  382. mapping = pci_map_single(priv->pdev, skb->data,
  383. skb->len, PCI_DMA_TODEVICE);
  384. if (pci_dma_mapping_error(priv->pdev, mapping)) {
  385. kfree_skb(skb);
  386. dev_err(&priv->pdev->dev, "TX DMA mapping error\n");
  387. return;
  388. }
  389. tx_flags = RTL818X_TX_DESC_FLAG_OWN | RTL818X_TX_DESC_FLAG_FS |
  390. RTL818X_TX_DESC_FLAG_LS |
  391. (ieee80211_get_tx_rate(dev, info)->hw_value << 24) |
  392. skb->len;
  393. if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
  394. tx_flags |= RTL818X_TX_DESC_FLAG_DMA |
  395. RTL818X_TX_DESC_FLAG_NO_ENC;
  396. rc_flags = info->control.rates[0].flags;
  397. /* HW will perform RTS-CTS when only RTS flags is set.
  398. * HW will perform CTS-to-self when both RTS and CTS flags are set.
  399. * RTS rate and RTS duration will be used also for CTS-to-self.
  400. */
  401. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  402. tx_flags |= RTL818X_TX_DESC_FLAG_RTS;
  403. tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
  404. rts_duration = ieee80211_rts_duration(dev, priv->vif,
  405. skb->len, info);
  406. } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  407. tx_flags |= RTL818X_TX_DESC_FLAG_RTS | RTL818X_TX_DESC_FLAG_CTS;
  408. tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
  409. rts_duration = ieee80211_ctstoself_duration(dev, priv->vif,
  410. skb->len, info);
  411. }
  412. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
  413. unsigned int remainder;
  414. plcp_len = DIV_ROUND_UP(16 * (skb->len + 4),
  415. (ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
  416. remainder = (16 * (skb->len + 4)) %
  417. ((ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
  418. if (remainder <= 6)
  419. plcp_len |= 1 << 15;
  420. }
  421. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  422. __le16 duration;
  423. /* SIFS time (required by HW) is already included by
  424. * ieee80211_generic_frame_duration
  425. */
  426. duration = ieee80211_generic_frame_duration(dev, priv->vif,
  427. IEEE80211_BAND_2GHZ, skb->len,
  428. ieee80211_get_tx_rate(dev, info));
  429. frame_duration = priv->ack_time + le16_to_cpu(duration);
  430. }
  431. spin_lock_irqsave(&priv->lock, flags);
  432. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  433. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  434. priv->seqno += 0x10;
  435. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  436. hdr->seq_ctrl |= cpu_to_le16(priv->seqno);
  437. }
  438. idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
  439. entry = &ring->desc[idx];
  440. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  441. entry->frame_duration = cpu_to_le16(frame_duration);
  442. entry->frame_len_se = cpu_to_le16(skb->len);
  443. /* tpc polarity */
  444. entry->flags3 = cpu_to_le16(1<<4);
  445. } else
  446. entry->frame_len = cpu_to_le32(skb->len);
  447. entry->rts_duration = rts_duration;
  448. entry->plcp_len = cpu_to_le16(plcp_len);
  449. entry->tx_buf = cpu_to_le32(mapping);
  450. entry->flags2 = info->control.rates[1].idx >= 0 ?
  451. ieee80211_get_alt_retry_rate(dev, info, 0)->bitrate << 4 : 0;
  452. entry->retry_limit = info->control.rates[0].count;
  453. /* We must be sure that tx_flags is written last because the HW
  454. * looks at it to check if the rest of data is valid or not
  455. */
  456. wmb();
  457. entry->flags = cpu_to_le32(tx_flags);
  458. /* We must be sure this has been written before followings HW
  459. * register write, because this write will made the HW attempts
  460. * to DMA the just-written data
  461. */
  462. wmb();
  463. __skb_queue_tail(&ring->queue, skb);
  464. if (ring->entries - skb_queue_len(&ring->queue) < 2)
  465. ieee80211_stop_queue(dev, prio);
  466. spin_unlock_irqrestore(&priv->lock, flags);
  467. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  468. /* just poll: rings are stopped with TPPollStop reg */
  469. hw_prio = rtl8187se_queues_map[prio];
  470. rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
  471. (1 << hw_prio));
  472. } else {
  473. hw_prio = rtl8180_queues_map[prio];
  474. rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
  475. (1 << hw_prio) | /* ring to poll */
  476. (1<<1) | (1<<2));/* stopped rings */
  477. }
  478. }
  479. static void rtl8180_set_anaparam3(struct rtl8180_priv *priv, u16 anaparam3)
  480. {
  481. u8 reg;
  482. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  483. RTL818X_EEPROM_CMD_CONFIG);
  484. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  485. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  486. reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
  487. rtl818x_iowrite16(priv, &priv->map->ANAPARAM3, anaparam3);
  488. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  489. reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
  490. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  491. RTL818X_EEPROM_CMD_NORMAL);
  492. }
  493. void rtl8180_set_anaparam2(struct rtl8180_priv *priv, u32 anaparam2)
  494. {
  495. u8 reg;
  496. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  497. RTL818X_EEPROM_CMD_CONFIG);
  498. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  499. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  500. reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
  501. rtl818x_iowrite32(priv, &priv->map->ANAPARAM2, anaparam2);
  502. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  503. reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
  504. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  505. RTL818X_EEPROM_CMD_NORMAL);
  506. }
  507. void rtl8180_set_anaparam(struct rtl8180_priv *priv, u32 anaparam)
  508. {
  509. u8 reg;
  510. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  511. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  512. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  513. reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
  514. rtl818x_iowrite32(priv, &priv->map->ANAPARAM, anaparam);
  515. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  516. reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
  517. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  518. }
  519. static void rtl8187se_mac_config(struct ieee80211_hw *dev)
  520. {
  521. struct rtl8180_priv *priv = dev->priv;
  522. u8 reg;
  523. rtl818x_iowrite32(priv, REG_ADDR4(0x1F0), 0);
  524. rtl818x_ioread32(priv, REG_ADDR4(0x1F0));
  525. rtl818x_iowrite32(priv, REG_ADDR4(0x1F4), 0);
  526. rtl818x_ioread32(priv, REG_ADDR4(0x1F4));
  527. rtl818x_iowrite8(priv, REG_ADDR1(0x1F8), 0);
  528. rtl818x_ioread8(priv, REG_ADDR1(0x1F8));
  529. /* Enable DA10 TX power saving */
  530. reg = rtl818x_ioread8(priv, &priv->map->PHY_PR);
  531. rtl818x_iowrite8(priv, &priv->map->PHY_PR, reg | 0x04);
  532. /* Power */
  533. rtl818x_iowrite16(priv, PI_DATA_REG, 0x1000);
  534. rtl818x_iowrite16(priv, SI_DATA_REG, 0x1000);
  535. /* AFE - default to power ON */
  536. rtl818x_iowrite16(priv, REG_ADDR2(0x370), 0x0560);
  537. rtl818x_iowrite16(priv, REG_ADDR2(0x372), 0x0560);
  538. rtl818x_iowrite16(priv, REG_ADDR2(0x374), 0x0DA4);
  539. rtl818x_iowrite16(priv, REG_ADDR2(0x376), 0x0DA4);
  540. rtl818x_iowrite16(priv, REG_ADDR2(0x378), 0x0560);
  541. rtl818x_iowrite16(priv, REG_ADDR2(0x37A), 0x0560);
  542. rtl818x_iowrite16(priv, REG_ADDR2(0x37C), 0x00EC);
  543. rtl818x_iowrite16(priv, REG_ADDR2(0x37E), 0x00EC);
  544. rtl818x_iowrite8(priv, REG_ADDR1(0x24E), 0x01);
  545. /* unknown, needed for suspend to RAM resume */
  546. rtl818x_iowrite8(priv, REG_ADDR1(0x0A), 0x72);
  547. }
  548. static void rtl8187se_set_antenna_config(struct ieee80211_hw *dev, u8 def_ant,
  549. bool diversity)
  550. {
  551. struct rtl8180_priv *priv = dev->priv;
  552. rtl8225_write_phy_cck(dev, 0x0C, 0x09);
  553. if (diversity) {
  554. if (def_ant == 1) {
  555. rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x00);
  556. rtl8225_write_phy_cck(dev, 0x11, 0xBB);
  557. rtl8225_write_phy_cck(dev, 0x01, 0xC7);
  558. rtl8225_write_phy_ofdm(dev, 0x0D, 0x54);
  559. rtl8225_write_phy_ofdm(dev, 0x18, 0xB2);
  560. } else { /* main antenna */
  561. rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x03);
  562. rtl8225_write_phy_cck(dev, 0x11, 0x9B);
  563. rtl8225_write_phy_cck(dev, 0x01, 0xC7);
  564. rtl8225_write_phy_ofdm(dev, 0x0D, 0x5C);
  565. rtl8225_write_phy_ofdm(dev, 0x18, 0xB2);
  566. }
  567. } else { /* disable antenna diversity */
  568. if (def_ant == 1) {
  569. rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x00);
  570. rtl8225_write_phy_cck(dev, 0x11, 0xBB);
  571. rtl8225_write_phy_cck(dev, 0x01, 0x47);
  572. rtl8225_write_phy_ofdm(dev, 0x0D, 0x54);
  573. rtl8225_write_phy_ofdm(dev, 0x18, 0x32);
  574. } else { /* main antenna */
  575. rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x03);
  576. rtl8225_write_phy_cck(dev, 0x11, 0x9B);
  577. rtl8225_write_phy_cck(dev, 0x01, 0x47);
  578. rtl8225_write_phy_ofdm(dev, 0x0D, 0x5C);
  579. rtl8225_write_phy_ofdm(dev, 0x18, 0x32);
  580. }
  581. }
  582. /* priv->curr_ant = def_ant; */
  583. }
  584. static void rtl8180_int_enable(struct ieee80211_hw *dev)
  585. {
  586. struct rtl8180_priv *priv = dev->priv;
  587. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  588. rtl818x_iowrite32(priv, &priv->map->IMR,
  589. IMR_TBDER | IMR_TBDOK |
  590. IMR_TVODER | IMR_TVODOK |
  591. IMR_TVIDER | IMR_TVIDOK |
  592. IMR_TBEDER | IMR_TBEDOK |
  593. IMR_TBKDER | IMR_TBKDOK |
  594. IMR_RDU | IMR_RER |
  595. IMR_ROK | IMR_RQOSOK);
  596. } else {
  597. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
  598. }
  599. }
  600. static void rtl8180_int_disable(struct ieee80211_hw *dev)
  601. {
  602. struct rtl8180_priv *priv = dev->priv;
  603. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  604. rtl818x_iowrite32(priv, &priv->map->IMR, 0);
  605. } else {
  606. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
  607. }
  608. }
  609. static void rtl8180_conf_basic_rates(struct ieee80211_hw *dev,
  610. u32 rates_mask)
  611. {
  612. struct rtl8180_priv *priv = dev->priv;
  613. u8 max, min;
  614. u16 reg;
  615. max = fls(rates_mask) - 1;
  616. min = ffs(rates_mask) - 1;
  617. switch (priv->chip_family) {
  618. case RTL818X_CHIP_FAMILY_RTL8180:
  619. /* in 8180 this is NOT a BITMAP */
  620. reg = rtl818x_ioread16(priv, &priv->map->BRSR);
  621. reg &= ~3;
  622. reg |= max;
  623. rtl818x_iowrite16(priv, &priv->map->BRSR, reg);
  624. break;
  625. case RTL818X_CHIP_FAMILY_RTL8185:
  626. /* in 8185 this is a BITMAP */
  627. rtl818x_iowrite16(priv, &priv->map->BRSR, rates_mask);
  628. rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (max << 4) | min);
  629. break;
  630. case RTL818X_CHIP_FAMILY_RTL8187SE:
  631. /* in 8187se this is a BITMAP */
  632. rtl818x_iowrite16(priv, &priv->map->BRSR_8187SE, rates_mask);
  633. break;
  634. }
  635. }
  636. static void rtl8180_config_cardbus(struct ieee80211_hw *dev)
  637. {
  638. struct rtl8180_priv *priv = dev->priv;
  639. u16 reg16;
  640. u8 reg8;
  641. reg8 = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  642. reg8 |= 1 << 1;
  643. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg8);
  644. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  645. rtl818x_iowrite16(priv, FEMR_SE, 0xffff);
  646. } else {
  647. reg16 = rtl818x_ioread16(priv, &priv->map->FEMR);
  648. reg16 |= (1 << 15) | (1 << 14) | (1 << 4);
  649. rtl818x_iowrite16(priv, &priv->map->FEMR, reg16);
  650. }
  651. }
  652. static int rtl8180_init_hw(struct ieee80211_hw *dev)
  653. {
  654. struct rtl8180_priv *priv = dev->priv;
  655. u16 reg;
  656. u32 reg32;
  657. rtl818x_iowrite8(priv, &priv->map->CMD, 0);
  658. rtl818x_ioread8(priv, &priv->map->CMD);
  659. msleep(10);
  660. /* reset */
  661. rtl8180_int_disable(dev);
  662. rtl818x_ioread8(priv, &priv->map->CMD);
  663. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  664. reg &= (1 << 1);
  665. reg |= RTL818X_CMD_RESET;
  666. rtl818x_iowrite8(priv, &priv->map->CMD, RTL818X_CMD_RESET);
  667. rtl818x_ioread8(priv, &priv->map->CMD);
  668. msleep(200);
  669. /* check success of reset */
  670. if (rtl818x_ioread8(priv, &priv->map->CMD) & RTL818X_CMD_RESET) {
  671. wiphy_err(dev->wiphy, "reset timeout!\n");
  672. return -ETIMEDOUT;
  673. }
  674. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
  675. rtl818x_ioread8(priv, &priv->map->CMD);
  676. msleep(200);
  677. if (rtl818x_ioread8(priv, &priv->map->CONFIG3) & (1 << 3)) {
  678. rtl8180_config_cardbus(dev);
  679. }
  680. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  681. rtl818x_iowrite8(priv, &priv->map->MSR, RTL818X_MSR_ENEDCA);
  682. else
  683. rtl818x_iowrite8(priv, &priv->map->MSR, 0);
  684. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
  685. rtl8180_set_anaparam(priv, priv->anaparam);
  686. rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
  687. /* mac80211 queue have higher prio for lower index. The last queue
  688. * (that mac80211 is not aware of) is reserved for beacons (and have
  689. * the highest priority on the NIC)
  690. */
  691. if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8187SE) {
  692. rtl818x_iowrite32(priv, &priv->map->TBDA,
  693. priv->tx_ring[1].dma);
  694. rtl818x_iowrite32(priv, &priv->map->TLPDA,
  695. priv->tx_ring[0].dma);
  696. } else {
  697. rtl818x_iowrite32(priv, &priv->map->TBDA,
  698. priv->tx_ring[4].dma);
  699. rtl818x_iowrite32(priv, &priv->map->TVODA,
  700. priv->tx_ring[0].dma);
  701. rtl818x_iowrite32(priv, &priv->map->TVIDA,
  702. priv->tx_ring[1].dma);
  703. rtl818x_iowrite32(priv, &priv->map->TBEDA,
  704. priv->tx_ring[2].dma);
  705. rtl818x_iowrite32(priv, &priv->map->TBKDA,
  706. priv->tx_ring[3].dma);
  707. }
  708. /* TODO: necessary? specs indicate not */
  709. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  710. reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
  711. rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg & ~(1 << 3));
  712. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
  713. reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
  714. rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg | (1 << 4));
  715. }
  716. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  717. /* TODO: set CONFIG5 for calibrating AGC on rtl8180 + philips radio? */
  718. /* TODO: turn off hw wep on rtl8180 */
  719. rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
  720. if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
  721. rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
  722. rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
  723. } else {
  724. rtl818x_iowrite8(priv, &priv->map->SECURITY, 0);
  725. rtl818x_iowrite8(priv, &priv->map->PHY_DELAY, 0x6);
  726. rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, 0x4C);
  727. }
  728. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
  729. /* TODO: set ClkRun enable? necessary? */
  730. reg = rtl818x_ioread8(priv, &priv->map->GP_ENABLE);
  731. rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, reg & ~(1 << 6));
  732. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  733. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  734. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | (1 << 2));
  735. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  736. }
  737. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  738. /* the set auto rate fallback bitmask from 1M to 54 Mb/s */
  739. rtl818x_iowrite16(priv, ARFR, 0xFFF);
  740. rtl818x_ioread16(priv, ARFR);
  741. /* stop unused queus (no dma alloc) */
  742. rtl818x_iowrite8(priv, &priv->map->TPPOLL_STOP,
  743. RTL818x_TPPOLL_STOP_MG | RTL818x_TPPOLL_STOP_HI);
  744. rtl818x_iowrite8(priv, &priv->map->ACM_CONTROL, 0x00);
  745. rtl818x_iowrite16(priv, &priv->map->TID_AC_MAP, 0xFA50);
  746. rtl818x_iowrite16(priv, &priv->map->INT_MIG, 0);
  747. /* some black magic here.. */
  748. rtl8187se_mac_config(dev);
  749. rtl818x_iowrite16(priv, RFSW_CTRL, 0x569A);
  750. rtl818x_ioread16(priv, RFSW_CTRL);
  751. rtl8180_set_anaparam(priv, RTL8225SE_ANAPARAM_ON);
  752. rtl8180_set_anaparam2(priv, RTL8225SE_ANAPARAM2_ON);
  753. rtl8180_set_anaparam3(priv, RTL8225SE_ANAPARAM3);
  754. rtl818x_iowrite8(priv, &priv->map->CONFIG5,
  755. rtl818x_ioread8(priv, &priv->map->CONFIG5) & 0x7F);
  756. /*probably this switch led on */
  757. rtl818x_iowrite8(priv, &priv->map->PGSELECT,
  758. rtl818x_ioread8(priv, &priv->map->PGSELECT) | 0x08);
  759. rtl818x_iowrite16(priv, &priv->map->RFPinsOutput, 0x0480);
  760. rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x1BFF);
  761. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0x2488);
  762. rtl818x_iowrite32(priv, &priv->map->RF_TIMING, 0x4003);
  763. /* the reference code mac hardcode table write
  764. * this reg by doing byte-wide accesses.
  765. * It does it just for lowest and highest byte..
  766. */
  767. reg32 = rtl818x_ioread32(priv, &priv->map->RF_PARA);
  768. reg32 &= 0x00ffff00;
  769. reg32 |= 0xb8000054;
  770. rtl818x_iowrite32(priv, &priv->map->RF_PARA, reg32);
  771. } else
  772. /* stop unused queus (no dma alloc) */
  773. rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
  774. (1<<1) | (1<<2));
  775. priv->rf->init(dev);
  776. /* default basic rates are 1,2 Mbps for rtl8180. 1,2,6,9,12,18,24 Mbps
  777. * otherwise. bitmask 0x3 and 0x01f3 respectively.
  778. * NOTE: currenty rtl8225 RF code changes basic rates, so we need to do
  779. * this after rf init.
  780. * TODO: try to find out whether RF code really needs to do this..
  781. */
  782. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
  783. rtl8180_conf_basic_rates(dev, 0x3);
  784. else
  785. rtl8180_conf_basic_rates(dev, 0x1f3);
  786. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  787. rtl8187se_set_antenna_config(dev,
  788. priv->antenna_diversity_default,
  789. priv->antenna_diversity_en);
  790. return 0;
  791. }
  792. static int rtl8180_init_rx_ring(struct ieee80211_hw *dev)
  793. {
  794. struct rtl8180_priv *priv = dev->priv;
  795. struct rtl818x_rx_cmd_desc *entry;
  796. int i;
  797. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  798. priv->rx_ring_sz = sizeof(struct rtl8187se_rx_desc);
  799. else
  800. priv->rx_ring_sz = sizeof(struct rtl8180_rx_desc);
  801. priv->rx_ring = pci_alloc_consistent(priv->pdev,
  802. priv->rx_ring_sz * 32,
  803. &priv->rx_ring_dma);
  804. if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
  805. wiphy_err(dev->wiphy, "Cannot allocate RX ring\n");
  806. return -ENOMEM;
  807. }
  808. memset(priv->rx_ring, 0, priv->rx_ring_sz * 32);
  809. priv->rx_idx = 0;
  810. for (i = 0; i < 32; i++) {
  811. struct sk_buff *skb = dev_alloc_skb(MAX_RX_SIZE);
  812. dma_addr_t *mapping;
  813. entry = priv->rx_ring + priv->rx_ring_sz*i;
  814. if (!skb) {
  815. wiphy_err(dev->wiphy, "Cannot allocate RX skb\n");
  816. return -ENOMEM;
  817. }
  818. priv->rx_buf[i] = skb;
  819. mapping = (dma_addr_t *)skb->cb;
  820. *mapping = pci_map_single(priv->pdev, skb_tail_pointer(skb),
  821. MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
  822. if (pci_dma_mapping_error(priv->pdev, *mapping)) {
  823. kfree_skb(skb);
  824. wiphy_err(dev->wiphy, "Cannot map DMA for RX skb\n");
  825. return -ENOMEM;
  826. }
  827. entry->rx_buf = cpu_to_le32(*mapping);
  828. entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
  829. MAX_RX_SIZE);
  830. }
  831. entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
  832. return 0;
  833. }
  834. static void rtl8180_free_rx_ring(struct ieee80211_hw *dev)
  835. {
  836. struct rtl8180_priv *priv = dev->priv;
  837. int i;
  838. for (i = 0; i < 32; i++) {
  839. struct sk_buff *skb = priv->rx_buf[i];
  840. if (!skb)
  841. continue;
  842. pci_unmap_single(priv->pdev,
  843. *((dma_addr_t *)skb->cb),
  844. MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
  845. kfree_skb(skb);
  846. }
  847. pci_free_consistent(priv->pdev, priv->rx_ring_sz * 32,
  848. priv->rx_ring, priv->rx_ring_dma);
  849. priv->rx_ring = NULL;
  850. }
  851. static int rtl8180_init_tx_ring(struct ieee80211_hw *dev,
  852. unsigned int prio, unsigned int entries)
  853. {
  854. struct rtl8180_priv *priv = dev->priv;
  855. struct rtl8180_tx_desc *ring;
  856. dma_addr_t dma;
  857. int i;
  858. ring = pci_alloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
  859. if (!ring || (unsigned long)ring & 0xFF) {
  860. wiphy_err(dev->wiphy, "Cannot allocate TX ring (prio = %d)\n",
  861. prio);
  862. return -ENOMEM;
  863. }
  864. memset(ring, 0, sizeof(*ring)*entries);
  865. priv->tx_ring[prio].desc = ring;
  866. priv->tx_ring[prio].dma = dma;
  867. priv->tx_ring[prio].idx = 0;
  868. priv->tx_ring[prio].entries = entries;
  869. skb_queue_head_init(&priv->tx_ring[prio].queue);
  870. for (i = 0; i < entries; i++)
  871. ring[i].next_tx_desc =
  872. cpu_to_le32((u32)dma + ((i + 1) % entries) * sizeof(*ring));
  873. return 0;
  874. }
  875. static void rtl8180_free_tx_ring(struct ieee80211_hw *dev, unsigned int prio)
  876. {
  877. struct rtl8180_priv *priv = dev->priv;
  878. struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
  879. while (skb_queue_len(&ring->queue)) {
  880. struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
  881. struct sk_buff *skb = __skb_dequeue(&ring->queue);
  882. pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
  883. skb->len, PCI_DMA_TODEVICE);
  884. kfree_skb(skb);
  885. ring->idx = (ring->idx + 1) % ring->entries;
  886. }
  887. pci_free_consistent(priv->pdev, sizeof(*ring->desc)*ring->entries,
  888. ring->desc, ring->dma);
  889. ring->desc = NULL;
  890. }
  891. static int rtl8180_start(struct ieee80211_hw *dev)
  892. {
  893. struct rtl8180_priv *priv = dev->priv;
  894. int ret, i;
  895. u32 reg;
  896. ret = rtl8180_init_rx_ring(dev);
  897. if (ret)
  898. return ret;
  899. for (i = 0; i < (dev->queues + 1); i++)
  900. if ((ret = rtl8180_init_tx_ring(dev, i, 16)))
  901. goto err_free_rings;
  902. ret = rtl8180_init_hw(dev);
  903. if (ret)
  904. goto err_free_rings;
  905. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  906. ret = request_irq(priv->pdev->irq, rtl8187se_interrupt,
  907. IRQF_SHARED, KBUILD_MODNAME, dev);
  908. } else {
  909. ret = request_irq(priv->pdev->irq, rtl8180_interrupt,
  910. IRQF_SHARED, KBUILD_MODNAME, dev);
  911. }
  912. if (ret) {
  913. wiphy_err(dev->wiphy, "failed to register IRQ handler\n");
  914. goto err_free_rings;
  915. }
  916. rtl8180_int_enable(dev);
  917. /* in rtl8187se at MAR regs offset there is the management
  918. * TX descriptor DMA addres..
  919. */
  920. if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8187SE) {
  921. rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
  922. rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
  923. }
  924. reg = RTL818X_RX_CONF_ONLYERLPKT |
  925. RTL818X_RX_CONF_RX_AUTORESETPHY |
  926. RTL818X_RX_CONF_MGMT |
  927. RTL818X_RX_CONF_DATA |
  928. (7 << 8 /* MAX RX DMA */) |
  929. RTL818X_RX_CONF_BROADCAST |
  930. RTL818X_RX_CONF_NICMAC;
  931. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185)
  932. reg |= RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2;
  933. else if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
  934. reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE1)
  935. ? RTL818X_RX_CONF_CSDM1 : 0;
  936. reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE2)
  937. ? RTL818X_RX_CONF_CSDM2 : 0;
  938. } else {
  939. reg &= ~(RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2);
  940. }
  941. priv->rx_conf = reg;
  942. rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
  943. if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
  944. reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
  945. /* CW is not on per-packet basis.
  946. * in rtl8185 the CW_VALUE reg is used.
  947. * in rtl8187se the AC param regs are used.
  948. */
  949. reg &= ~RTL818X_CW_CONF_PERPACKET_CW;
  950. /* retry limit IS on per-packet basis.
  951. * the short and long retry limit in TX_CONF
  952. * reg are ignored
  953. */
  954. reg |= RTL818X_CW_CONF_PERPACKET_RETRY;
  955. rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
  956. reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
  957. /* TX antenna and TX gain are not on per-packet basis.
  958. * TX Antenna is selected by ANTSEL reg (RX in BB regs).
  959. * TX gain is selected with CCK_TX_AGC and OFDM_TX_AGC regs
  960. */
  961. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN;
  962. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL;
  963. reg |= RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
  964. rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
  965. /* disable early TX */
  966. rtl818x_iowrite8(priv, (u8 __iomem *)priv->map + 0xec, 0x3f);
  967. }
  968. reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
  969. reg |= (6 << 21 /* MAX TX DMA */) |
  970. RTL818X_TX_CONF_NO_ICV;
  971. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  972. reg |= 1<<30; /* "duration procedure mode" */
  973. if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
  974. reg &= ~RTL818X_TX_CONF_PROBE_DTS;
  975. else
  976. reg &= ~RTL818X_TX_CONF_HW_SEQNUM;
  977. reg &= ~RTL818X_TX_CONF_DISCW;
  978. /* different meaning, same value on both rtl8185 and rtl8180 */
  979. reg &= ~RTL818X_TX_CONF_SAT_HWPLCP;
  980. rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
  981. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  982. reg |= RTL818X_CMD_RX_ENABLE;
  983. reg |= RTL818X_CMD_TX_ENABLE;
  984. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  985. return 0;
  986. err_free_rings:
  987. rtl8180_free_rx_ring(dev);
  988. for (i = 0; i < (dev->queues + 1); i++)
  989. if (priv->tx_ring[i].desc)
  990. rtl8180_free_tx_ring(dev, i);
  991. return ret;
  992. }
  993. static void rtl8180_stop(struct ieee80211_hw *dev)
  994. {
  995. struct rtl8180_priv *priv = dev->priv;
  996. u8 reg;
  997. int i;
  998. rtl8180_int_disable(dev);
  999. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  1000. reg &= ~RTL818X_CMD_TX_ENABLE;
  1001. reg &= ~RTL818X_CMD_RX_ENABLE;
  1002. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  1003. priv->rf->stop(dev);
  1004. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  1005. reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
  1006. rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
  1007. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  1008. free_irq(priv->pdev->irq, dev);
  1009. rtl8180_free_rx_ring(dev);
  1010. for (i = 0; i < (dev->queues + 1); i++)
  1011. rtl8180_free_tx_ring(dev, i);
  1012. }
  1013. static u64 rtl8180_get_tsf(struct ieee80211_hw *dev,
  1014. struct ieee80211_vif *vif)
  1015. {
  1016. struct rtl8180_priv *priv = dev->priv;
  1017. return rtl818x_ioread32(priv, &priv->map->TSFT[0]) |
  1018. (u64)(rtl818x_ioread32(priv, &priv->map->TSFT[1])) << 32;
  1019. }
  1020. static void rtl8180_beacon_work(struct work_struct *work)
  1021. {
  1022. struct rtl8180_vif *vif_priv =
  1023. container_of(work, struct rtl8180_vif, beacon_work.work);
  1024. struct ieee80211_vif *vif =
  1025. container_of((void *)vif_priv, struct ieee80211_vif, drv_priv);
  1026. struct ieee80211_hw *dev = vif_priv->dev;
  1027. struct ieee80211_mgmt *mgmt;
  1028. struct sk_buff *skb;
  1029. /* don't overflow the tx ring */
  1030. if (ieee80211_queue_stopped(dev, 0))
  1031. goto resched;
  1032. /* grab a fresh beacon */
  1033. skb = ieee80211_beacon_get(dev, vif);
  1034. if (!skb)
  1035. goto resched;
  1036. /*
  1037. * update beacon timestamp w/ TSF value
  1038. * TODO: make hardware update beacon timestamp
  1039. */
  1040. mgmt = (struct ieee80211_mgmt *)skb->data;
  1041. mgmt->u.beacon.timestamp = cpu_to_le64(rtl8180_get_tsf(dev, vif));
  1042. /* TODO: use actual beacon queue */
  1043. skb_set_queue_mapping(skb, 0);
  1044. rtl8180_tx(dev, NULL, skb);
  1045. resched:
  1046. /*
  1047. * schedule next beacon
  1048. * TODO: use hardware support for beacon timing
  1049. */
  1050. schedule_delayed_work(&vif_priv->beacon_work,
  1051. usecs_to_jiffies(1024 * vif->bss_conf.beacon_int));
  1052. }
  1053. static int rtl8180_add_interface(struct ieee80211_hw *dev,
  1054. struct ieee80211_vif *vif)
  1055. {
  1056. struct rtl8180_priv *priv = dev->priv;
  1057. struct rtl8180_vif *vif_priv;
  1058. /*
  1059. * We only support one active interface at a time.
  1060. */
  1061. if (priv->vif)
  1062. return -EBUSY;
  1063. switch (vif->type) {
  1064. case NL80211_IFTYPE_STATION:
  1065. case NL80211_IFTYPE_ADHOC:
  1066. break;
  1067. default:
  1068. return -EOPNOTSUPP;
  1069. }
  1070. priv->vif = vif;
  1071. /* Initialize driver private area */
  1072. vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
  1073. vif_priv->dev = dev;
  1074. INIT_DELAYED_WORK(&vif_priv->beacon_work, rtl8180_beacon_work);
  1075. vif_priv->enable_beacon = false;
  1076. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  1077. rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->MAC[0],
  1078. le32_to_cpu(*(__le32 *)vif->addr));
  1079. rtl818x_iowrite16(priv, (__le16 __iomem *)&priv->map->MAC[4],
  1080. le16_to_cpu(*(__le16 *)(vif->addr + 4)));
  1081. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  1082. return 0;
  1083. }
  1084. static void rtl8180_remove_interface(struct ieee80211_hw *dev,
  1085. struct ieee80211_vif *vif)
  1086. {
  1087. struct rtl8180_priv *priv = dev->priv;
  1088. priv->vif = NULL;
  1089. }
  1090. static int rtl8180_config(struct ieee80211_hw *dev, u32 changed)
  1091. {
  1092. struct rtl8180_priv *priv = dev->priv;
  1093. struct ieee80211_conf *conf = &dev->conf;
  1094. priv->rf->set_chan(dev, conf);
  1095. return 0;
  1096. }
  1097. static void rtl8187se_conf_ac_parm(struct ieee80211_hw *dev, u8 queue)
  1098. {
  1099. const struct ieee80211_tx_queue_params *params;
  1100. struct rtl8180_priv *priv = dev->priv;
  1101. /* hw value */
  1102. u32 ac_param;
  1103. u8 aifs;
  1104. u8 txop;
  1105. u8 cw_min, cw_max;
  1106. params = &priv->queue_param[queue];
  1107. cw_min = fls(params->cw_min);
  1108. cw_max = fls(params->cw_max);
  1109. aifs = 10 + params->aifs * priv->slot_time;
  1110. /* TODO: check if txop HW is in us (mult by 32) */
  1111. txop = params->txop;
  1112. ac_param = txop << AC_PARAM_TXOP_LIMIT_SHIFT |
  1113. cw_max << AC_PARAM_ECW_MAX_SHIFT |
  1114. cw_min << AC_PARAM_ECW_MIN_SHIFT |
  1115. aifs << AC_PARAM_AIFS_SHIFT;
  1116. switch (queue) {
  1117. case IEEE80211_AC_BK:
  1118. rtl818x_iowrite32(priv, &priv->map->AC_BK_PARAM, ac_param);
  1119. break;
  1120. case IEEE80211_AC_BE:
  1121. rtl818x_iowrite32(priv, &priv->map->AC_BE_PARAM, ac_param);
  1122. break;
  1123. case IEEE80211_AC_VI:
  1124. rtl818x_iowrite32(priv, &priv->map->AC_VI_PARAM, ac_param);
  1125. break;
  1126. case IEEE80211_AC_VO:
  1127. rtl818x_iowrite32(priv, &priv->map->AC_VO_PARAM, ac_param);
  1128. break;
  1129. }
  1130. }
  1131. static int rtl8180_conf_tx(struct ieee80211_hw *dev,
  1132. struct ieee80211_vif *vif, u16 queue,
  1133. const struct ieee80211_tx_queue_params *params)
  1134. {
  1135. struct rtl8180_priv *priv = dev->priv;
  1136. u8 cw_min, cw_max;
  1137. /* nothing to do ? */
  1138. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
  1139. return 0;
  1140. cw_min = fls(params->cw_min);
  1141. cw_max = fls(params->cw_max);
  1142. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  1143. priv->queue_param[queue] = *params;
  1144. rtl8187se_conf_ac_parm(dev, queue);
  1145. } else
  1146. rtl818x_iowrite8(priv, &priv->map->CW_VAL,
  1147. (cw_max << 4) | cw_min);
  1148. return 0;
  1149. }
  1150. static void rtl8180_conf_erp(struct ieee80211_hw *dev,
  1151. struct ieee80211_bss_conf *info)
  1152. {
  1153. struct rtl8180_priv *priv = dev->priv;
  1154. u8 sifs, difs;
  1155. int eifs;
  1156. u8 hw_eifs;
  1157. /* TODO: should we do something ? */
  1158. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
  1159. return;
  1160. /* I _hope_ this means 10uS for the HW.
  1161. * In reference code it is 0x22 for
  1162. * both rtl8187L and rtl8187SE
  1163. */
  1164. sifs = 0x22;
  1165. if (info->use_short_slot)
  1166. priv->slot_time = 9;
  1167. else
  1168. priv->slot_time = 20;
  1169. /* 10 is SIFS time in uS */
  1170. difs = 10 + 2 * priv->slot_time;
  1171. eifs = 10 + difs + priv->ack_time;
  1172. /* HW should use 4uS units for EIFS (I'm sure for rtl8185)*/
  1173. hw_eifs = DIV_ROUND_UP(eifs, 4);
  1174. rtl818x_iowrite8(priv, &priv->map->SLOT, priv->slot_time);
  1175. rtl818x_iowrite8(priv, &priv->map->SIFS, sifs);
  1176. rtl818x_iowrite8(priv, &priv->map->DIFS, difs);
  1177. /* from reference code. set ack timeout reg = eifs reg */
  1178. rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, hw_eifs);
  1179. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  1180. rtl818x_iowrite8(priv, &priv->map->EIFS_8187SE, hw_eifs);
  1181. else if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
  1182. /* rtl8187/rtl8185 HW bug. After EIFS is elapsed,
  1183. * the HW still wait for DIFS.
  1184. * HW uses 4uS units for EIFS.
  1185. */
  1186. hw_eifs = DIV_ROUND_UP(eifs - difs, 4);
  1187. rtl818x_iowrite8(priv, &priv->map->EIFS, hw_eifs);
  1188. }
  1189. }
  1190. static void rtl8180_bss_info_changed(struct ieee80211_hw *dev,
  1191. struct ieee80211_vif *vif,
  1192. struct ieee80211_bss_conf *info,
  1193. u32 changed)
  1194. {
  1195. struct rtl8180_priv *priv = dev->priv;
  1196. struct rtl8180_vif *vif_priv;
  1197. int i;
  1198. u8 reg;
  1199. vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
  1200. if (changed & BSS_CHANGED_BSSID) {
  1201. for (i = 0; i < ETH_ALEN; i++)
  1202. rtl818x_iowrite8(priv, &priv->map->BSSID[i],
  1203. info->bssid[i]);
  1204. if (is_valid_ether_addr(info->bssid)) {
  1205. if (vif->type == NL80211_IFTYPE_ADHOC)
  1206. reg = RTL818X_MSR_ADHOC;
  1207. else
  1208. reg = RTL818X_MSR_INFRA;
  1209. } else
  1210. reg = RTL818X_MSR_NO_LINK;
  1211. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  1212. reg |= RTL818X_MSR_ENEDCA;
  1213. rtl818x_iowrite8(priv, &priv->map->MSR, reg);
  1214. }
  1215. if (changed & BSS_CHANGED_BASIC_RATES)
  1216. rtl8180_conf_basic_rates(dev, info->basic_rates);
  1217. if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_ERP_PREAMBLE)) {
  1218. /* when preamble changes, acktime duration changes, and erp must
  1219. * be recalculated. ACK time is calculated at lowest rate.
  1220. * Since mac80211 include SIFS time we remove it (-10)
  1221. */
  1222. priv->ack_time =
  1223. le16_to_cpu(ieee80211_generic_frame_duration(dev,
  1224. priv->vif,
  1225. IEEE80211_BAND_2GHZ, 10,
  1226. &priv->rates[0])) - 10;
  1227. rtl8180_conf_erp(dev, info);
  1228. /* mac80211 supplies aifs_n to driver and calls
  1229. * conf_tx callback whether aifs_n changes, NOT
  1230. * when aifs changes.
  1231. * Aifs should be recalculated if slot changes.
  1232. */
  1233. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  1234. for (i = 0; i < 4; i++)
  1235. rtl8187se_conf_ac_parm(dev, i);
  1236. }
  1237. }
  1238. if (changed & BSS_CHANGED_BEACON_ENABLED)
  1239. vif_priv->enable_beacon = info->enable_beacon;
  1240. if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON)) {
  1241. cancel_delayed_work_sync(&vif_priv->beacon_work);
  1242. if (vif_priv->enable_beacon)
  1243. schedule_work(&vif_priv->beacon_work.work);
  1244. }
  1245. }
  1246. static u64 rtl8180_prepare_multicast(struct ieee80211_hw *dev,
  1247. struct netdev_hw_addr_list *mc_list)
  1248. {
  1249. return netdev_hw_addr_list_count(mc_list);
  1250. }
  1251. static void rtl8180_configure_filter(struct ieee80211_hw *dev,
  1252. unsigned int changed_flags,
  1253. unsigned int *total_flags,
  1254. u64 multicast)
  1255. {
  1256. struct rtl8180_priv *priv = dev->priv;
  1257. if (changed_flags & FIF_FCSFAIL)
  1258. priv->rx_conf ^= RTL818X_RX_CONF_FCS;
  1259. if (changed_flags & FIF_CONTROL)
  1260. priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
  1261. if (changed_flags & FIF_OTHER_BSS)
  1262. priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
  1263. if (*total_flags & FIF_ALLMULTI || multicast > 0)
  1264. priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
  1265. else
  1266. priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
  1267. *total_flags = 0;
  1268. if (priv->rx_conf & RTL818X_RX_CONF_FCS)
  1269. *total_flags |= FIF_FCSFAIL;
  1270. if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
  1271. *total_flags |= FIF_CONTROL;
  1272. if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
  1273. *total_flags |= FIF_OTHER_BSS;
  1274. if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
  1275. *total_flags |= FIF_ALLMULTI;
  1276. rtl818x_iowrite32(priv, &priv->map->RX_CONF, priv->rx_conf);
  1277. }
  1278. static const struct ieee80211_ops rtl8180_ops = {
  1279. .tx = rtl8180_tx,
  1280. .start = rtl8180_start,
  1281. .stop = rtl8180_stop,
  1282. .add_interface = rtl8180_add_interface,
  1283. .remove_interface = rtl8180_remove_interface,
  1284. .config = rtl8180_config,
  1285. .bss_info_changed = rtl8180_bss_info_changed,
  1286. .conf_tx = rtl8180_conf_tx,
  1287. .prepare_multicast = rtl8180_prepare_multicast,
  1288. .configure_filter = rtl8180_configure_filter,
  1289. .get_tsf = rtl8180_get_tsf,
  1290. };
  1291. static void rtl8180_eeprom_register_read(struct eeprom_93cx6 *eeprom)
  1292. {
  1293. struct rtl8180_priv *priv = eeprom->data;
  1294. u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  1295. eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
  1296. eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
  1297. eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
  1298. eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
  1299. }
  1300. static void rtl8180_eeprom_register_write(struct eeprom_93cx6 *eeprom)
  1301. {
  1302. struct rtl8180_priv *priv = eeprom->data;
  1303. u8 reg = 2 << 6;
  1304. if (eeprom->reg_data_in)
  1305. reg |= RTL818X_EEPROM_CMD_WRITE;
  1306. if (eeprom->reg_data_out)
  1307. reg |= RTL818X_EEPROM_CMD_READ;
  1308. if (eeprom->reg_data_clock)
  1309. reg |= RTL818X_EEPROM_CMD_CK;
  1310. if (eeprom->reg_chip_select)
  1311. reg |= RTL818X_EEPROM_CMD_CS;
  1312. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
  1313. rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  1314. udelay(10);
  1315. }
  1316. static void rtl8180_eeprom_read(struct rtl8180_priv *priv)
  1317. {
  1318. struct eeprom_93cx6 eeprom;
  1319. int eeprom_cck_table_adr;
  1320. u16 eeprom_val;
  1321. int i;
  1322. eeprom.data = priv;
  1323. eeprom.register_read = rtl8180_eeprom_register_read;
  1324. eeprom.register_write = rtl8180_eeprom_register_write;
  1325. if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
  1326. eeprom.width = PCI_EEPROM_WIDTH_93C66;
  1327. else
  1328. eeprom.width = PCI_EEPROM_WIDTH_93C46;
  1329. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  1330. RTL818X_EEPROM_CMD_PROGRAM);
  1331. rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  1332. udelay(10);
  1333. eeprom_93cx6_read(&eeprom, 0x06, &eeprom_val);
  1334. eeprom_val &= 0xFF;
  1335. priv->rf_type = eeprom_val;
  1336. eeprom_93cx6_read(&eeprom, 0x17, &eeprom_val);
  1337. priv->csthreshold = eeprom_val >> 8;
  1338. eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)priv->mac_addr, 3);
  1339. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  1340. eeprom_cck_table_adr = 0x30;
  1341. else
  1342. eeprom_cck_table_adr = 0x10;
  1343. /* CCK TX power */
  1344. for (i = 0; i < 14; i += 2) {
  1345. u16 txpwr;
  1346. eeprom_93cx6_read(&eeprom, eeprom_cck_table_adr + (i >> 1),
  1347. &txpwr);
  1348. priv->channels[i].hw_value = txpwr & 0xFF;
  1349. priv->channels[i + 1].hw_value = txpwr >> 8;
  1350. }
  1351. /* OFDM TX power */
  1352. if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
  1353. for (i = 0; i < 14; i += 2) {
  1354. u16 txpwr;
  1355. eeprom_93cx6_read(&eeprom, 0x20 + (i >> 1), &txpwr);
  1356. priv->channels[i].hw_value |= (txpwr & 0xFF) << 8;
  1357. priv->channels[i + 1].hw_value |= txpwr & 0xFF00;
  1358. }
  1359. }
  1360. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
  1361. __le32 anaparam;
  1362. eeprom_93cx6_multiread(&eeprom, 0xD, (__le16 *)&anaparam, 2);
  1363. priv->anaparam = le32_to_cpu(anaparam);
  1364. eeprom_93cx6_read(&eeprom, 0x19, &priv->rfparam);
  1365. }
  1366. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  1367. eeprom_93cx6_read(&eeprom, 0x3F, &eeprom_val);
  1368. priv->antenna_diversity_en = !!(eeprom_val & 0x100);
  1369. priv->antenna_diversity_default = (eeprom_val & 0xC00) == 0x400;
  1370. eeprom_93cx6_read(&eeprom, 0x7C, &eeprom_val);
  1371. priv->xtal_out = eeprom_val & 0xF;
  1372. priv->xtal_in = (eeprom_val & 0xF0) >> 4;
  1373. priv->xtal_cal = !!(eeprom_val & 0x1000);
  1374. priv->thermal_meter_val = (eeprom_val & 0xF00) >> 8;
  1375. priv->thermal_meter_en = !!(eeprom_val & 0x2000);
  1376. }
  1377. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  1378. RTL818X_EEPROM_CMD_NORMAL);
  1379. }
  1380. static int rtl8180_probe(struct pci_dev *pdev,
  1381. const struct pci_device_id *id)
  1382. {
  1383. struct ieee80211_hw *dev;
  1384. struct rtl8180_priv *priv;
  1385. unsigned long mem_addr, mem_len;
  1386. unsigned int io_addr, io_len;
  1387. int err;
  1388. const char *chip_name, *rf_name = NULL;
  1389. u32 reg;
  1390. err = pci_enable_device(pdev);
  1391. if (err) {
  1392. printk(KERN_ERR "%s (rtl8180): Cannot enable new PCI device\n",
  1393. pci_name(pdev));
  1394. return err;
  1395. }
  1396. err = pci_request_regions(pdev, KBUILD_MODNAME);
  1397. if (err) {
  1398. printk(KERN_ERR "%s (rtl8180): Cannot obtain PCI resources\n",
  1399. pci_name(pdev));
  1400. return err;
  1401. }
  1402. io_addr = pci_resource_start(pdev, 0);
  1403. io_len = pci_resource_len(pdev, 0);
  1404. mem_addr = pci_resource_start(pdev, 1);
  1405. mem_len = pci_resource_len(pdev, 1);
  1406. if (mem_len < sizeof(struct rtl818x_csr) ||
  1407. io_len < sizeof(struct rtl818x_csr)) {
  1408. printk(KERN_ERR "%s (rtl8180): Too short PCI resources\n",
  1409. pci_name(pdev));
  1410. err = -ENOMEM;
  1411. goto err_free_reg;
  1412. }
  1413. if ((err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) ||
  1414. (err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))) {
  1415. printk(KERN_ERR "%s (rtl8180): No suitable DMA available\n",
  1416. pci_name(pdev));
  1417. goto err_free_reg;
  1418. }
  1419. pci_set_master(pdev);
  1420. dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8180_ops);
  1421. if (!dev) {
  1422. printk(KERN_ERR "%s (rtl8180): ieee80211 alloc failed\n",
  1423. pci_name(pdev));
  1424. err = -ENOMEM;
  1425. goto err_free_reg;
  1426. }
  1427. priv = dev->priv;
  1428. priv->pdev = pdev;
  1429. dev->max_rates = 2;
  1430. SET_IEEE80211_DEV(dev, &pdev->dev);
  1431. pci_set_drvdata(pdev, dev);
  1432. priv->map = pci_iomap(pdev, 1, mem_len);
  1433. if (!priv->map)
  1434. priv->map = pci_iomap(pdev, 0, io_len);
  1435. if (!priv->map) {
  1436. printk(KERN_ERR "%s (rtl8180): Cannot map device memory\n",
  1437. pci_name(pdev));
  1438. goto err_free_dev;
  1439. }
  1440. BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
  1441. BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
  1442. memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
  1443. memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
  1444. priv->band.band = IEEE80211_BAND_2GHZ;
  1445. priv->band.channels = priv->channels;
  1446. priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
  1447. priv->band.bitrates = priv->rates;
  1448. priv->band.n_bitrates = 4;
  1449. dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  1450. dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  1451. IEEE80211_HW_RX_INCLUDES_FCS |
  1452. IEEE80211_HW_SIGNAL_UNSPEC;
  1453. dev->vif_data_size = sizeof(struct rtl8180_vif);
  1454. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  1455. BIT(NL80211_IFTYPE_ADHOC);
  1456. dev->max_signal = 65;
  1457. reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
  1458. reg &= RTL818X_TX_CONF_HWVER_MASK;
  1459. switch (reg) {
  1460. case RTL818X_TX_CONF_R8180_ABCD:
  1461. chip_name = "RTL8180";
  1462. priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
  1463. break;
  1464. case RTL818X_TX_CONF_R8180_F:
  1465. chip_name = "RTL8180vF";
  1466. priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
  1467. break;
  1468. case RTL818X_TX_CONF_R8185_ABC:
  1469. chip_name = "RTL8185";
  1470. priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
  1471. break;
  1472. case RTL818X_TX_CONF_R8185_D:
  1473. chip_name = "RTL8185vD";
  1474. priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
  1475. break;
  1476. case RTL818X_TX_CONF_RTL8187SE:
  1477. chip_name = "RTL8187SE";
  1478. priv->chip_family = RTL818X_CHIP_FAMILY_RTL8187SE;
  1479. break;
  1480. default:
  1481. printk(KERN_ERR "%s (rtl8180): Unknown chip! (0x%x)\n",
  1482. pci_name(pdev), reg >> 25);
  1483. goto err_iounmap;
  1484. }
  1485. /* we declare to MAC80211 all the queues except for beacon queue
  1486. * that will be eventually handled by DRV.
  1487. * TX rings are arranged in such a way that lower is the IDX,
  1488. * higher is the priority, in order to achieve direct mapping
  1489. * with mac80211, however the beacon queue is an exception and it
  1490. * is mapped on the highst tx ring IDX.
  1491. */
  1492. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  1493. dev->queues = RTL8187SE_NR_TX_QUEUES - 1;
  1494. else
  1495. dev->queues = RTL8180_NR_TX_QUEUES - 1;
  1496. if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
  1497. priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
  1498. pci_try_set_mwi(pdev);
  1499. }
  1500. rtl8180_eeprom_read(priv);
  1501. switch (priv->rf_type) {
  1502. case 1: rf_name = "Intersil";
  1503. break;
  1504. case 2: rf_name = "RFMD";
  1505. break;
  1506. case 3: priv->rf = &sa2400_rf_ops;
  1507. break;
  1508. case 4: priv->rf = &max2820_rf_ops;
  1509. break;
  1510. case 5: priv->rf = &grf5101_rf_ops;
  1511. break;
  1512. case 9:
  1513. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  1514. priv->rf = rtl8187se_detect_rf(dev);
  1515. else
  1516. priv->rf = rtl8180_detect_rf(dev);
  1517. break;
  1518. case 10:
  1519. rf_name = "RTL8255";
  1520. break;
  1521. default:
  1522. printk(KERN_ERR "%s (rtl8180): Unknown RF! (0x%x)\n",
  1523. pci_name(pdev), priv->rf_type);
  1524. goto err_iounmap;
  1525. }
  1526. if (!priv->rf) {
  1527. printk(KERN_ERR "%s (rtl8180): %s RF frontend not supported!\n",
  1528. pci_name(pdev), rf_name);
  1529. goto err_iounmap;
  1530. }
  1531. if (!is_valid_ether_addr(priv->mac_addr)) {
  1532. printk(KERN_WARNING "%s (rtl8180): Invalid hwaddr! Using"
  1533. " randomly generated MAC addr\n", pci_name(pdev));
  1534. eth_random_addr(priv->mac_addr);
  1535. }
  1536. SET_IEEE80211_PERM_ADDR(dev, priv->mac_addr);
  1537. spin_lock_init(&priv->lock);
  1538. err = ieee80211_register_hw(dev);
  1539. if (err) {
  1540. printk(KERN_ERR "%s (rtl8180): Cannot register device\n",
  1541. pci_name(pdev));
  1542. goto err_iounmap;
  1543. }
  1544. wiphy_info(dev->wiphy, "hwaddr %pm, %s + %s\n",
  1545. priv->mac_addr, chip_name, priv->rf->name);
  1546. return 0;
  1547. err_iounmap:
  1548. pci_iounmap(pdev, priv->map);
  1549. err_free_dev:
  1550. ieee80211_free_hw(dev);
  1551. err_free_reg:
  1552. pci_release_regions(pdev);
  1553. pci_disable_device(pdev);
  1554. return err;
  1555. }
  1556. static void rtl8180_remove(struct pci_dev *pdev)
  1557. {
  1558. struct ieee80211_hw *dev = pci_get_drvdata(pdev);
  1559. struct rtl8180_priv *priv;
  1560. if (!dev)
  1561. return;
  1562. ieee80211_unregister_hw(dev);
  1563. priv = dev->priv;
  1564. pci_iounmap(pdev, priv->map);
  1565. pci_release_regions(pdev);
  1566. pci_disable_device(pdev);
  1567. ieee80211_free_hw(dev);
  1568. }
  1569. #ifdef CONFIG_PM
  1570. static int rtl8180_suspend(struct pci_dev *pdev, pm_message_t state)
  1571. {
  1572. pci_save_state(pdev);
  1573. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1574. return 0;
  1575. }
  1576. static int rtl8180_resume(struct pci_dev *pdev)
  1577. {
  1578. pci_set_power_state(pdev, PCI_D0);
  1579. pci_restore_state(pdev);
  1580. return 0;
  1581. }
  1582. #endif /* CONFIG_PM */
  1583. static struct pci_driver rtl8180_driver = {
  1584. .name = KBUILD_MODNAME,
  1585. .id_table = rtl8180_table,
  1586. .probe = rtl8180_probe,
  1587. .remove = rtl8180_remove,
  1588. #ifdef CONFIG_PM
  1589. .suspend = rtl8180_suspend,
  1590. .resume = rtl8180_resume,
  1591. #endif /* CONFIG_PM */
  1592. };
  1593. module_pci_driver(rtl8180_driver);