dev.c 56 KB

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