dev.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996
  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 = dma_map_single(&priv->pdev->dev,
  236. skb_tail_pointer(new_skb),
  237. MAX_RX_SIZE, DMA_FROM_DEVICE);
  238. if (dma_mapping_error(&priv->pdev->dev, mapping)) {
  239. kfree_skb(new_skb);
  240. dev_err(&priv->pdev->dev, "RX DMA map error\n");
  241. goto done;
  242. }
  243. dma_unmap_single(&priv->pdev->dev,
  244. *((dma_addr_t *)skb->cb),
  245. MAX_RX_SIZE, DMA_FROM_DEVICE);
  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. dma_unmap_single(&priv->pdev->dev, le32_to_cpu(entry->tx_buf),
  318. skb->len, DMA_TO_DEVICE);
  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 int prio = 0;
  399. unsigned long flags;
  400. unsigned int idx, hw_prio;
  401. dma_addr_t mapping;
  402. u32 tx_flags;
  403. u8 rc_flags;
  404. u16 plcp_len = 0;
  405. __le16 rts_duration = 0;
  406. /* do arithmetic and then convert to le16 */
  407. u16 frame_duration = 0;
  408. /* rtl8180/rtl8185 only has one useable tx queue */
  409. if (dev->queues > IEEE80211_AC_BK)
  410. prio = skb_get_queue_mapping(skb);
  411. ring = &priv->tx_ring[prio];
  412. mapping = dma_map_single(&priv->pdev->dev, skb->data, skb->len,
  413. DMA_TO_DEVICE);
  414. if (dma_mapping_error(&priv->pdev->dev, mapping)) {
  415. kfree_skb(skb);
  416. dev_err(&priv->pdev->dev, "TX DMA mapping error\n");
  417. return;
  418. }
  419. tx_flags = RTL818X_TX_DESC_FLAG_OWN | RTL818X_TX_DESC_FLAG_FS |
  420. RTL818X_TX_DESC_FLAG_LS |
  421. (ieee80211_get_tx_rate(dev, info)->hw_value << 24) |
  422. skb->len;
  423. if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
  424. tx_flags |= RTL818X_TX_DESC_FLAG_DMA |
  425. RTL818X_TX_DESC_FLAG_NO_ENC;
  426. rc_flags = info->control.rates[0].flags;
  427. /* HW will perform RTS-CTS when only RTS flags is set.
  428. * HW will perform CTS-to-self when both RTS and CTS flags are set.
  429. * RTS rate and RTS duration will be used also for CTS-to-self.
  430. */
  431. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  432. tx_flags |= RTL818X_TX_DESC_FLAG_RTS;
  433. tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
  434. rts_duration = ieee80211_rts_duration(dev, priv->vif,
  435. skb->len, info);
  436. } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  437. tx_flags |= RTL818X_TX_DESC_FLAG_RTS | RTL818X_TX_DESC_FLAG_CTS;
  438. tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
  439. rts_duration = ieee80211_ctstoself_duration(dev, priv->vif,
  440. skb->len, info);
  441. }
  442. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
  443. unsigned int remainder;
  444. plcp_len = DIV_ROUND_UP(16 * (skb->len + 4),
  445. (ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
  446. remainder = (16 * (skb->len + 4)) %
  447. ((ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
  448. if (remainder <= 6)
  449. plcp_len |= 1 << 15;
  450. }
  451. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  452. __le16 duration;
  453. /* SIFS time (required by HW) is already included by
  454. * ieee80211_generic_frame_duration
  455. */
  456. duration = ieee80211_generic_frame_duration(dev, priv->vif,
  457. NL80211_BAND_2GHZ, skb->len,
  458. ieee80211_get_tx_rate(dev, info));
  459. frame_duration = priv->ack_time + le16_to_cpu(duration);
  460. }
  461. spin_lock_irqsave(&priv->lock, flags);
  462. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  463. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  464. priv->seqno += 0x10;
  465. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  466. hdr->seq_ctrl |= cpu_to_le16(priv->seqno);
  467. }
  468. idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
  469. entry = &ring->desc[idx];
  470. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  471. entry->frame_duration = cpu_to_le16(frame_duration);
  472. entry->frame_len_se = cpu_to_le16(skb->len);
  473. /* tpc polarity */
  474. entry->flags3 = cpu_to_le16(1<<4);
  475. } else
  476. entry->frame_len = cpu_to_le32(skb->len);
  477. entry->rts_duration = rts_duration;
  478. entry->plcp_len = cpu_to_le16(plcp_len);
  479. entry->tx_buf = cpu_to_le32(mapping);
  480. entry->retry_limit = info->control.rates[0].count - 1;
  481. /* We must be sure that tx_flags is written last because the HW
  482. * looks at it to check if the rest of data is valid or not
  483. */
  484. wmb();
  485. entry->flags = cpu_to_le32(tx_flags);
  486. /* We must be sure this has been written before followings HW
  487. * register write, because this write will made the HW attempts
  488. * to DMA the just-written data
  489. */
  490. wmb();
  491. __skb_queue_tail(&ring->queue, skb);
  492. if (ring->entries - skb_queue_len(&ring->queue) < 2)
  493. ieee80211_stop_queue(dev, prio);
  494. spin_unlock_irqrestore(&priv->lock, flags);
  495. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  496. /* just poll: rings are stopped with TPPollStop reg */
  497. hw_prio = rtl8187se_queues_map[prio];
  498. rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
  499. (1 << hw_prio));
  500. } else {
  501. hw_prio = rtl8180_queues_map[prio];
  502. rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
  503. (1 << hw_prio) | /* ring to poll */
  504. (1<<1) | (1<<2));/* stopped rings */
  505. }
  506. }
  507. static void rtl8180_set_anaparam3(struct rtl8180_priv *priv, u16 anaparam3)
  508. {
  509. u8 reg;
  510. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  511. RTL818X_EEPROM_CMD_CONFIG);
  512. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  513. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  514. reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
  515. rtl818x_iowrite16(priv, &priv->map->ANAPARAM3, anaparam3);
  516. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  517. reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
  518. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  519. RTL818X_EEPROM_CMD_NORMAL);
  520. }
  521. void rtl8180_set_anaparam2(struct rtl8180_priv *priv, u32 anaparam2)
  522. {
  523. u8 reg;
  524. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  525. RTL818X_EEPROM_CMD_CONFIG);
  526. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  527. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  528. reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
  529. rtl818x_iowrite32(priv, &priv->map->ANAPARAM2, anaparam2);
  530. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  531. reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
  532. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  533. RTL818X_EEPROM_CMD_NORMAL);
  534. }
  535. void rtl8180_set_anaparam(struct rtl8180_priv *priv, u32 anaparam)
  536. {
  537. u8 reg;
  538. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  539. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  540. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  541. reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
  542. rtl818x_iowrite32(priv, &priv->map->ANAPARAM, anaparam);
  543. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  544. reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
  545. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  546. }
  547. static void rtl8187se_mac_config(struct ieee80211_hw *dev)
  548. {
  549. struct rtl8180_priv *priv = dev->priv;
  550. u8 reg;
  551. rtl818x_iowrite32(priv, REG_ADDR4(0x1F0), 0);
  552. rtl818x_ioread32(priv, REG_ADDR4(0x1F0));
  553. rtl818x_iowrite32(priv, REG_ADDR4(0x1F4), 0);
  554. rtl818x_ioread32(priv, REG_ADDR4(0x1F4));
  555. rtl818x_iowrite8(priv, REG_ADDR1(0x1F8), 0);
  556. rtl818x_ioread8(priv, REG_ADDR1(0x1F8));
  557. /* Enable DA10 TX power saving */
  558. reg = rtl818x_ioread8(priv, &priv->map->PHY_PR);
  559. rtl818x_iowrite8(priv, &priv->map->PHY_PR, reg | 0x04);
  560. /* Power */
  561. rtl818x_iowrite16(priv, PI_DATA_REG, 0x1000);
  562. rtl818x_iowrite16(priv, SI_DATA_REG, 0x1000);
  563. /* AFE - default to power ON */
  564. rtl818x_iowrite16(priv, REG_ADDR2(0x370), 0x0560);
  565. rtl818x_iowrite16(priv, REG_ADDR2(0x372), 0x0560);
  566. rtl818x_iowrite16(priv, REG_ADDR2(0x374), 0x0DA4);
  567. rtl818x_iowrite16(priv, REG_ADDR2(0x376), 0x0DA4);
  568. rtl818x_iowrite16(priv, REG_ADDR2(0x378), 0x0560);
  569. rtl818x_iowrite16(priv, REG_ADDR2(0x37A), 0x0560);
  570. rtl818x_iowrite16(priv, REG_ADDR2(0x37C), 0x00EC);
  571. rtl818x_iowrite16(priv, REG_ADDR2(0x37E), 0x00EC);
  572. rtl818x_iowrite8(priv, REG_ADDR1(0x24E), 0x01);
  573. /* unknown, needed for suspend to RAM resume */
  574. rtl818x_iowrite8(priv, REG_ADDR1(0x0A), 0x72);
  575. }
  576. static void rtl8187se_set_antenna_config(struct ieee80211_hw *dev, u8 def_ant,
  577. bool diversity)
  578. {
  579. struct rtl8180_priv *priv = dev->priv;
  580. rtl8225_write_phy_cck(dev, 0x0C, 0x09);
  581. if (diversity) {
  582. if (def_ant == 1) {
  583. rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x00);
  584. rtl8225_write_phy_cck(dev, 0x11, 0xBB);
  585. rtl8225_write_phy_cck(dev, 0x01, 0xC7);
  586. rtl8225_write_phy_ofdm(dev, 0x0D, 0x54);
  587. rtl8225_write_phy_ofdm(dev, 0x18, 0xB2);
  588. } else { /* main antenna */
  589. rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x03);
  590. rtl8225_write_phy_cck(dev, 0x11, 0x9B);
  591. rtl8225_write_phy_cck(dev, 0x01, 0xC7);
  592. rtl8225_write_phy_ofdm(dev, 0x0D, 0x5C);
  593. rtl8225_write_phy_ofdm(dev, 0x18, 0xB2);
  594. }
  595. } else { /* disable antenna diversity */
  596. if (def_ant == 1) {
  597. rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x00);
  598. rtl8225_write_phy_cck(dev, 0x11, 0xBB);
  599. rtl8225_write_phy_cck(dev, 0x01, 0x47);
  600. rtl8225_write_phy_ofdm(dev, 0x0D, 0x54);
  601. rtl8225_write_phy_ofdm(dev, 0x18, 0x32);
  602. } else { /* main antenna */
  603. rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x03);
  604. rtl8225_write_phy_cck(dev, 0x11, 0x9B);
  605. rtl8225_write_phy_cck(dev, 0x01, 0x47);
  606. rtl8225_write_phy_ofdm(dev, 0x0D, 0x5C);
  607. rtl8225_write_phy_ofdm(dev, 0x18, 0x32);
  608. }
  609. }
  610. /* priv->curr_ant = def_ant; */
  611. }
  612. static void rtl8180_int_enable(struct ieee80211_hw *dev)
  613. {
  614. struct rtl8180_priv *priv = dev->priv;
  615. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  616. rtl818x_iowrite32(priv, &priv->map->IMR,
  617. IMR_TBDER | IMR_TBDOK |
  618. IMR_TVODER | IMR_TVODOK |
  619. IMR_TVIDER | IMR_TVIDOK |
  620. IMR_TBEDER | IMR_TBEDOK |
  621. IMR_TBKDER | IMR_TBKDOK |
  622. IMR_RDU | IMR_RER |
  623. IMR_ROK | IMR_RQOSOK);
  624. } else {
  625. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
  626. }
  627. }
  628. static void rtl8180_int_disable(struct ieee80211_hw *dev)
  629. {
  630. struct rtl8180_priv *priv = dev->priv;
  631. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  632. rtl818x_iowrite32(priv, &priv->map->IMR, 0);
  633. } else {
  634. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
  635. }
  636. }
  637. static void rtl8180_conf_basic_rates(struct ieee80211_hw *dev,
  638. u32 basic_mask)
  639. {
  640. struct rtl8180_priv *priv = dev->priv;
  641. u16 reg;
  642. u32 resp_mask;
  643. u8 basic_max;
  644. u8 resp_max, resp_min;
  645. resp_mask = basic_mask;
  646. /* IEEE80211 says the response rate should be equal to the highest basic
  647. * rate that is not faster than received frame. But it says also that if
  648. * the basic rate set does not contains any rate for the current
  649. * modulation class then mandatory rate set must be used for that
  650. * modulation class. Eventually add OFDM mandatory rates..
  651. */
  652. if ((resp_mask & 0xf) == resp_mask)
  653. resp_mask |= 0x150; /* 6, 12, 24Mbps */
  654. switch (priv->chip_family) {
  655. case RTL818X_CHIP_FAMILY_RTL8180:
  656. /* in 8180 this is NOT a BITMAP */
  657. basic_max = fls(basic_mask) - 1;
  658. reg = rtl818x_ioread16(priv, &priv->map->BRSR);
  659. reg &= ~3;
  660. reg |= basic_max;
  661. rtl818x_iowrite16(priv, &priv->map->BRSR, reg);
  662. break;
  663. case RTL818X_CHIP_FAMILY_RTL8185:
  664. resp_max = fls(resp_mask) - 1;
  665. resp_min = ffs(resp_mask) - 1;
  666. /* in 8185 this is a BITMAP */
  667. rtl818x_iowrite16(priv, &priv->map->BRSR, basic_mask);
  668. rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (resp_max << 4) |
  669. resp_min);
  670. break;
  671. case RTL818X_CHIP_FAMILY_RTL8187SE:
  672. /* in 8187se this is a BITMAP. BRSR reg actually sets
  673. * response rates.
  674. */
  675. rtl818x_iowrite16(priv, &priv->map->BRSR_8187SE, resp_mask);
  676. break;
  677. }
  678. }
  679. static void rtl8180_config_cardbus(struct ieee80211_hw *dev)
  680. {
  681. struct rtl8180_priv *priv = dev->priv;
  682. u16 reg16;
  683. u8 reg8;
  684. reg8 = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  685. reg8 |= 1 << 1;
  686. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg8);
  687. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  688. rtl818x_iowrite16(priv, FEMR_SE, 0xffff);
  689. } else {
  690. reg16 = rtl818x_ioread16(priv, &priv->map->FEMR);
  691. reg16 |= (1 << 15) | (1 << 14) | (1 << 4);
  692. rtl818x_iowrite16(priv, &priv->map->FEMR, reg16);
  693. }
  694. }
  695. static int rtl8180_init_hw(struct ieee80211_hw *dev)
  696. {
  697. struct rtl8180_priv *priv = dev->priv;
  698. u16 reg;
  699. u32 reg32;
  700. rtl818x_iowrite8(priv, &priv->map->CMD, 0);
  701. rtl818x_ioread8(priv, &priv->map->CMD);
  702. msleep(10);
  703. /* reset */
  704. rtl8180_int_disable(dev);
  705. rtl818x_ioread8(priv, &priv->map->CMD);
  706. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  707. reg &= (1 << 1);
  708. reg |= RTL818X_CMD_RESET;
  709. rtl818x_iowrite8(priv, &priv->map->CMD, RTL818X_CMD_RESET);
  710. rtl818x_ioread8(priv, &priv->map->CMD);
  711. msleep(200);
  712. /* check success of reset */
  713. if (rtl818x_ioread8(priv, &priv->map->CMD) & RTL818X_CMD_RESET) {
  714. wiphy_err(dev->wiphy, "reset timeout!\n");
  715. return -ETIMEDOUT;
  716. }
  717. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
  718. rtl818x_ioread8(priv, &priv->map->CMD);
  719. msleep(200);
  720. if (rtl818x_ioread8(priv, &priv->map->CONFIG3) & (1 << 3)) {
  721. rtl8180_config_cardbus(dev);
  722. }
  723. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  724. rtl818x_iowrite8(priv, &priv->map->MSR, RTL818X_MSR_ENEDCA);
  725. else
  726. rtl818x_iowrite8(priv, &priv->map->MSR, 0);
  727. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
  728. rtl8180_set_anaparam(priv, priv->anaparam);
  729. rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
  730. /* mac80211 queue have higher prio for lower index. The last queue
  731. * (that mac80211 is not aware of) is reserved for beacons (and have
  732. * the highest priority on the NIC)
  733. */
  734. if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8187SE) {
  735. rtl818x_iowrite32(priv, &priv->map->TBDA,
  736. priv->tx_ring[1].dma);
  737. rtl818x_iowrite32(priv, &priv->map->TLPDA,
  738. priv->tx_ring[0].dma);
  739. } else {
  740. rtl818x_iowrite32(priv, &priv->map->TBDA,
  741. priv->tx_ring[4].dma);
  742. rtl818x_iowrite32(priv, &priv->map->TVODA,
  743. priv->tx_ring[0].dma);
  744. rtl818x_iowrite32(priv, &priv->map->TVIDA,
  745. priv->tx_ring[1].dma);
  746. rtl818x_iowrite32(priv, &priv->map->TBEDA,
  747. priv->tx_ring[2].dma);
  748. rtl818x_iowrite32(priv, &priv->map->TBKDA,
  749. priv->tx_ring[3].dma);
  750. }
  751. /* TODO: necessary? specs indicate not */
  752. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  753. reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
  754. rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg & ~(1 << 3));
  755. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
  756. reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
  757. rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg | (1 << 4));
  758. }
  759. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  760. /* TODO: set CONFIG5 for calibrating AGC on rtl8180 + philips radio? */
  761. /* TODO: turn off hw wep on rtl8180 */
  762. rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
  763. if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
  764. rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
  765. rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0);
  766. } else {
  767. rtl818x_iowrite8(priv, &priv->map->SECURITY, 0);
  768. rtl818x_iowrite8(priv, &priv->map->PHY_DELAY, 0x6);
  769. rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, 0x4C);
  770. }
  771. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
  772. /* TODO: set ClkRun enable? necessary? */
  773. reg = rtl818x_ioread8(priv, &priv->map->GP_ENABLE);
  774. rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, reg & ~(1 << 6));
  775. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  776. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  777. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | (1 << 2));
  778. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  779. /* fix eccessive IFS after CTS-to-self */
  780. if (priv->map_pio) {
  781. u8 reg;
  782. reg = rtl818x_ioread8(priv, &priv->map->PGSELECT);
  783. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg | 1);
  784. rtl818x_iowrite8(priv, REG_ADDR1(0xff), 0x35);
  785. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
  786. } else
  787. rtl818x_iowrite8(priv, REG_ADDR1(0x1ff), 0x35);
  788. }
  789. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  790. /* the set auto rate fallback bitmask from 1M to 54 Mb/s */
  791. rtl818x_iowrite16(priv, ARFR, 0xFFF);
  792. rtl818x_ioread16(priv, ARFR);
  793. /* stop unused queus (no dma alloc) */
  794. rtl818x_iowrite8(priv, &priv->map->TPPOLL_STOP,
  795. RTL818x_TPPOLL_STOP_MG | RTL818x_TPPOLL_STOP_HI);
  796. rtl818x_iowrite8(priv, &priv->map->ACM_CONTROL, 0x00);
  797. rtl818x_iowrite16(priv, &priv->map->TID_AC_MAP, 0xFA50);
  798. rtl818x_iowrite16(priv, &priv->map->INT_MIG, 0);
  799. /* some black magic here.. */
  800. rtl8187se_mac_config(dev);
  801. rtl818x_iowrite16(priv, RFSW_CTRL, 0x569A);
  802. rtl818x_ioread16(priv, RFSW_CTRL);
  803. rtl8180_set_anaparam(priv, RTL8225SE_ANAPARAM_ON);
  804. rtl8180_set_anaparam2(priv, RTL8225SE_ANAPARAM2_ON);
  805. rtl8180_set_anaparam3(priv, RTL8225SE_ANAPARAM3);
  806. rtl818x_iowrite8(priv, &priv->map->CONFIG5,
  807. rtl818x_ioread8(priv, &priv->map->CONFIG5) & 0x7F);
  808. /*probably this switch led on */
  809. rtl818x_iowrite8(priv, &priv->map->PGSELECT,
  810. rtl818x_ioread8(priv, &priv->map->PGSELECT) | 0x08);
  811. rtl818x_iowrite16(priv, &priv->map->RFPinsOutput, 0x0480);
  812. rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x1BFF);
  813. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0x2488);
  814. rtl818x_iowrite32(priv, &priv->map->RF_TIMING, 0x4003);
  815. /* the reference code mac hardcode table write
  816. * this reg by doing byte-wide accesses.
  817. * It does it just for lowest and highest byte..
  818. */
  819. reg32 = rtl818x_ioread32(priv, &priv->map->RF_PARA);
  820. reg32 &= 0x00ffff00;
  821. reg32 |= 0xb8000054;
  822. rtl818x_iowrite32(priv, &priv->map->RF_PARA, reg32);
  823. } else
  824. /* stop unused queus (no dma alloc) */
  825. rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
  826. (1<<1) | (1<<2));
  827. priv->rf->init(dev);
  828. /* default basic rates are 1,2 Mbps for rtl8180. 1,2,6,9,12,18,24 Mbps
  829. * otherwise. bitmask 0x3 and 0x01f3 respectively.
  830. * NOTE: currenty rtl8225 RF code changes basic rates, so we need to do
  831. * this after rf init.
  832. * TODO: try to find out whether RF code really needs to do this..
  833. */
  834. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
  835. rtl8180_conf_basic_rates(dev, 0x3);
  836. else
  837. rtl8180_conf_basic_rates(dev, 0x1f3);
  838. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  839. rtl8187se_set_antenna_config(dev,
  840. priv->antenna_diversity_default,
  841. priv->antenna_diversity_en);
  842. return 0;
  843. }
  844. static int rtl8180_init_rx_ring(struct ieee80211_hw *dev)
  845. {
  846. struct rtl8180_priv *priv = dev->priv;
  847. struct rtl818x_rx_cmd_desc *entry;
  848. int i;
  849. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  850. priv->rx_ring_sz = sizeof(struct rtl8187se_rx_desc);
  851. else
  852. priv->rx_ring_sz = sizeof(struct rtl8180_rx_desc);
  853. priv->rx_ring = dma_alloc_coherent(&priv->pdev->dev,
  854. priv->rx_ring_sz * 32,
  855. &priv->rx_ring_dma, GFP_KERNEL);
  856. if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
  857. wiphy_err(dev->wiphy, "Cannot allocate RX ring\n");
  858. return -ENOMEM;
  859. }
  860. priv->rx_idx = 0;
  861. for (i = 0; i < 32; i++) {
  862. struct sk_buff *skb = dev_alloc_skb(MAX_RX_SIZE);
  863. dma_addr_t *mapping;
  864. entry = priv->rx_ring + priv->rx_ring_sz*i;
  865. if (!skb) {
  866. dma_free_coherent(&priv->pdev->dev,
  867. priv->rx_ring_sz * 32,
  868. priv->rx_ring, priv->rx_ring_dma);
  869. wiphy_err(dev->wiphy, "Cannot allocate RX skb\n");
  870. return -ENOMEM;
  871. }
  872. priv->rx_buf[i] = skb;
  873. mapping = (dma_addr_t *)skb->cb;
  874. *mapping = dma_map_single(&priv->pdev->dev,
  875. skb_tail_pointer(skb), MAX_RX_SIZE,
  876. DMA_FROM_DEVICE);
  877. if (dma_mapping_error(&priv->pdev->dev, *mapping)) {
  878. kfree_skb(skb);
  879. dma_free_coherent(&priv->pdev->dev,
  880. priv->rx_ring_sz * 32,
  881. priv->rx_ring, priv->rx_ring_dma);
  882. wiphy_err(dev->wiphy, "Cannot map DMA for RX skb\n");
  883. return -ENOMEM;
  884. }
  885. entry->rx_buf = cpu_to_le32(*mapping);
  886. entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
  887. MAX_RX_SIZE);
  888. }
  889. entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
  890. return 0;
  891. }
  892. static void rtl8180_free_rx_ring(struct ieee80211_hw *dev)
  893. {
  894. struct rtl8180_priv *priv = dev->priv;
  895. int i;
  896. for (i = 0; i < 32; i++) {
  897. struct sk_buff *skb = priv->rx_buf[i];
  898. if (!skb)
  899. continue;
  900. dma_unmap_single(&priv->pdev->dev, *((dma_addr_t *)skb->cb),
  901. MAX_RX_SIZE, DMA_FROM_DEVICE);
  902. kfree_skb(skb);
  903. }
  904. dma_free_coherent(&priv->pdev->dev, priv->rx_ring_sz * 32,
  905. priv->rx_ring, priv->rx_ring_dma);
  906. priv->rx_ring = NULL;
  907. }
  908. static int rtl8180_init_tx_ring(struct ieee80211_hw *dev,
  909. unsigned int prio, unsigned int entries)
  910. {
  911. struct rtl8180_priv *priv = dev->priv;
  912. struct rtl8180_tx_desc *ring;
  913. dma_addr_t dma;
  914. int i;
  915. ring = dma_alloc_coherent(&priv->pdev->dev, sizeof(*ring) * entries,
  916. &dma, GFP_KERNEL);
  917. if (!ring || (unsigned long)ring & 0xFF) {
  918. wiphy_err(dev->wiphy, "Cannot allocate TX ring (prio = %d)\n",
  919. prio);
  920. return -ENOMEM;
  921. }
  922. priv->tx_ring[prio].desc = ring;
  923. priv->tx_ring[prio].dma = dma;
  924. priv->tx_ring[prio].idx = 0;
  925. priv->tx_ring[prio].entries = entries;
  926. skb_queue_head_init(&priv->tx_ring[prio].queue);
  927. for (i = 0; i < entries; i++)
  928. ring[i].next_tx_desc =
  929. cpu_to_le32((u32)dma + ((i + 1) % entries) * sizeof(*ring));
  930. return 0;
  931. }
  932. static void rtl8180_free_tx_ring(struct ieee80211_hw *dev, unsigned int prio)
  933. {
  934. struct rtl8180_priv *priv = dev->priv;
  935. struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
  936. while (skb_queue_len(&ring->queue)) {
  937. struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
  938. struct sk_buff *skb = __skb_dequeue(&ring->queue);
  939. dma_unmap_single(&priv->pdev->dev, le32_to_cpu(entry->tx_buf),
  940. skb->len, DMA_TO_DEVICE);
  941. kfree_skb(skb);
  942. ring->idx = (ring->idx + 1) % ring->entries;
  943. }
  944. dma_free_coherent(&priv->pdev->dev,
  945. sizeof(*ring->desc) * ring->entries, ring->desc,
  946. ring->dma);
  947. ring->desc = NULL;
  948. }
  949. static int rtl8180_start(struct ieee80211_hw *dev)
  950. {
  951. struct rtl8180_priv *priv = dev->priv;
  952. int ret, i;
  953. u32 reg;
  954. ret = rtl8180_init_rx_ring(dev);
  955. if (ret)
  956. return ret;
  957. for (i = 0; i < (dev->queues + 1); i++)
  958. if ((ret = rtl8180_init_tx_ring(dev, i, 16)))
  959. goto err_free_rings;
  960. ret = rtl8180_init_hw(dev);
  961. if (ret)
  962. goto err_free_rings;
  963. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  964. ret = request_irq(priv->pdev->irq, rtl8187se_interrupt,
  965. IRQF_SHARED, KBUILD_MODNAME, dev);
  966. } else {
  967. ret = request_irq(priv->pdev->irq, rtl8180_interrupt,
  968. IRQF_SHARED, KBUILD_MODNAME, dev);
  969. }
  970. if (ret) {
  971. wiphy_err(dev->wiphy, "failed to register IRQ handler\n");
  972. goto err_free_rings;
  973. }
  974. rtl8180_int_enable(dev);
  975. /* in rtl8187se at MAR regs offset there is the management
  976. * TX descriptor DMA addres..
  977. */
  978. if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8187SE) {
  979. rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
  980. rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
  981. }
  982. reg = RTL818X_RX_CONF_ONLYERLPKT |
  983. RTL818X_RX_CONF_RX_AUTORESETPHY |
  984. RTL818X_RX_CONF_MGMT |
  985. RTL818X_RX_CONF_DATA |
  986. (7 << 8 /* MAX RX DMA */) |
  987. RTL818X_RX_CONF_BROADCAST |
  988. RTL818X_RX_CONF_NICMAC;
  989. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185)
  990. reg |= RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2;
  991. else if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
  992. reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE1)
  993. ? RTL818X_RX_CONF_CSDM1 : 0;
  994. reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE2)
  995. ? RTL818X_RX_CONF_CSDM2 : 0;
  996. } else {
  997. reg &= ~(RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2);
  998. }
  999. priv->rx_conf = reg;
  1000. rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
  1001. if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
  1002. reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
  1003. /* CW is not on per-packet basis.
  1004. * in rtl8185 the CW_VALUE reg is used.
  1005. * in rtl8187se the AC param regs are used.
  1006. */
  1007. reg &= ~RTL818X_CW_CONF_PERPACKET_CW;
  1008. /* retry limit IS on per-packet basis.
  1009. * the short and long retry limit in TX_CONF
  1010. * reg are ignored
  1011. */
  1012. reg |= RTL818X_CW_CONF_PERPACKET_RETRY;
  1013. rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
  1014. reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
  1015. /* TX antenna and TX gain are not on per-packet basis.
  1016. * TX Antenna is selected by ANTSEL reg (RX in BB regs).
  1017. * TX gain is selected with CCK_TX_AGC and OFDM_TX_AGC regs
  1018. */
  1019. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN;
  1020. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL;
  1021. reg |= RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
  1022. rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
  1023. /* disable early TX */
  1024. rtl818x_iowrite8(priv, (u8 __iomem *)priv->map + 0xec, 0x3f);
  1025. }
  1026. reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
  1027. reg |= (6 << 21 /* MAX TX DMA */) |
  1028. RTL818X_TX_CONF_NO_ICV;
  1029. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  1030. reg |= 1<<30; /* "duration procedure mode" */
  1031. if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
  1032. reg &= ~RTL818X_TX_CONF_PROBE_DTS;
  1033. else
  1034. reg &= ~RTL818X_TX_CONF_HW_SEQNUM;
  1035. reg &= ~RTL818X_TX_CONF_DISCW;
  1036. /* different meaning, same value on both rtl8185 and rtl8180 */
  1037. reg &= ~RTL818X_TX_CONF_SAT_HWPLCP;
  1038. rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
  1039. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  1040. reg |= RTL818X_CMD_RX_ENABLE;
  1041. reg |= RTL818X_CMD_TX_ENABLE;
  1042. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  1043. return 0;
  1044. err_free_rings:
  1045. rtl8180_free_rx_ring(dev);
  1046. for (i = 0; i < (dev->queues + 1); i++)
  1047. if (priv->tx_ring[i].desc)
  1048. rtl8180_free_tx_ring(dev, i);
  1049. return ret;
  1050. }
  1051. static void rtl8180_stop(struct ieee80211_hw *dev, bool suspend)
  1052. {
  1053. struct rtl8180_priv *priv = dev->priv;
  1054. u8 reg;
  1055. int i;
  1056. rtl8180_int_disable(dev);
  1057. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  1058. reg &= ~RTL818X_CMD_TX_ENABLE;
  1059. reg &= ~RTL818X_CMD_RX_ENABLE;
  1060. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  1061. priv->rf->stop(dev);
  1062. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  1063. reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
  1064. rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
  1065. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  1066. free_irq(priv->pdev->irq, dev);
  1067. rtl8180_free_rx_ring(dev);
  1068. for (i = 0; i < (dev->queues + 1); i++)
  1069. rtl8180_free_tx_ring(dev, i);
  1070. }
  1071. static u64 rtl8180_get_tsf(struct ieee80211_hw *dev,
  1072. struct ieee80211_vif *vif)
  1073. {
  1074. struct rtl8180_priv *priv = dev->priv;
  1075. return rtl818x_ioread32(priv, &priv->map->TSFT[0]) |
  1076. (u64)(rtl818x_ioread32(priv, &priv->map->TSFT[1])) << 32;
  1077. }
  1078. static void rtl8180_beacon_work(struct work_struct *work)
  1079. {
  1080. struct rtl8180_vif *vif_priv =
  1081. container_of(work, struct rtl8180_vif, beacon_work.work);
  1082. struct ieee80211_vif *vif =
  1083. container_of((void *)vif_priv, struct ieee80211_vif, drv_priv);
  1084. struct ieee80211_hw *dev = vif_priv->dev;
  1085. struct ieee80211_mgmt *mgmt;
  1086. struct sk_buff *skb;
  1087. /* don't overflow the tx ring */
  1088. if (ieee80211_queue_stopped(dev, 0))
  1089. goto resched;
  1090. /* grab a fresh beacon */
  1091. skb = ieee80211_beacon_get(dev, vif, 0);
  1092. if (!skb)
  1093. goto resched;
  1094. /*
  1095. * update beacon timestamp w/ TSF value
  1096. * TODO: make hardware update beacon timestamp
  1097. */
  1098. mgmt = (struct ieee80211_mgmt *)skb->data;
  1099. mgmt->u.beacon.timestamp = cpu_to_le64(rtl8180_get_tsf(dev, vif));
  1100. /* TODO: use actual beacon queue */
  1101. skb_set_queue_mapping(skb, 0);
  1102. rtl8180_tx(dev, NULL, skb);
  1103. resched:
  1104. /*
  1105. * schedule next beacon
  1106. * TODO: use hardware support for beacon timing
  1107. */
  1108. schedule_delayed_work(&vif_priv->beacon_work,
  1109. usecs_to_jiffies(1024 * vif->bss_conf.beacon_int));
  1110. }
  1111. static int rtl8180_add_interface(struct ieee80211_hw *dev,
  1112. struct ieee80211_vif *vif)
  1113. {
  1114. struct rtl8180_priv *priv = dev->priv;
  1115. struct rtl8180_vif *vif_priv;
  1116. /*
  1117. * We only support one active interface at a time.
  1118. */
  1119. if (priv->vif)
  1120. return -EBUSY;
  1121. switch (vif->type) {
  1122. case NL80211_IFTYPE_STATION:
  1123. case NL80211_IFTYPE_ADHOC:
  1124. break;
  1125. default:
  1126. return -EOPNOTSUPP;
  1127. }
  1128. priv->vif = vif;
  1129. /* Initialize driver private area */
  1130. vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
  1131. vif_priv->dev = dev;
  1132. INIT_DELAYED_WORK(&vif_priv->beacon_work, rtl8180_beacon_work);
  1133. vif_priv->enable_beacon = false;
  1134. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  1135. rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->MAC[0],
  1136. le32_to_cpu(*(__le32 *)vif->addr));
  1137. rtl818x_iowrite16(priv, (__le16 __iomem *)&priv->map->MAC[4],
  1138. le16_to_cpu(*(__le16 *)(vif->addr + 4)));
  1139. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  1140. return 0;
  1141. }
  1142. static void rtl8180_remove_interface(struct ieee80211_hw *dev,
  1143. struct ieee80211_vif *vif)
  1144. {
  1145. struct rtl8180_priv *priv = dev->priv;
  1146. priv->vif = NULL;
  1147. }
  1148. static int rtl8180_config(struct ieee80211_hw *dev, u32 changed)
  1149. {
  1150. struct rtl8180_priv *priv = dev->priv;
  1151. struct ieee80211_conf *conf = &dev->conf;
  1152. priv->rf->set_chan(dev, conf);
  1153. return 0;
  1154. }
  1155. static void rtl8187se_conf_ac_parm(struct ieee80211_hw *dev, u8 queue)
  1156. {
  1157. const struct ieee80211_tx_queue_params *params;
  1158. struct rtl8180_priv *priv = dev->priv;
  1159. /* hw value */
  1160. u32 ac_param;
  1161. u8 aifs;
  1162. u8 txop;
  1163. u8 cw_min, cw_max;
  1164. params = &priv->queue_param[queue];
  1165. cw_min = fls(params->cw_min);
  1166. cw_max = fls(params->cw_max);
  1167. aifs = 10 + params->aifs * priv->slot_time;
  1168. /* TODO: check if txop HW is in us (mult by 32) */
  1169. txop = params->txop;
  1170. ac_param = txop << AC_PARAM_TXOP_LIMIT_SHIFT |
  1171. cw_max << AC_PARAM_ECW_MAX_SHIFT |
  1172. cw_min << AC_PARAM_ECW_MIN_SHIFT |
  1173. aifs << AC_PARAM_AIFS_SHIFT;
  1174. switch (queue) {
  1175. case IEEE80211_AC_BK:
  1176. rtl818x_iowrite32(priv, &priv->map->AC_BK_PARAM, ac_param);
  1177. break;
  1178. case IEEE80211_AC_BE:
  1179. rtl818x_iowrite32(priv, &priv->map->AC_BE_PARAM, ac_param);
  1180. break;
  1181. case IEEE80211_AC_VI:
  1182. rtl818x_iowrite32(priv, &priv->map->AC_VI_PARAM, ac_param);
  1183. break;
  1184. case IEEE80211_AC_VO:
  1185. rtl818x_iowrite32(priv, &priv->map->AC_VO_PARAM, ac_param);
  1186. break;
  1187. }
  1188. }
  1189. static int rtl8180_conf_tx(struct ieee80211_hw *dev,
  1190. struct ieee80211_vif *vif,
  1191. unsigned int link_id, u16 queue,
  1192. const struct ieee80211_tx_queue_params *params)
  1193. {
  1194. struct rtl8180_priv *priv = dev->priv;
  1195. u8 cw_min, cw_max;
  1196. /* nothing to do ? */
  1197. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
  1198. return 0;
  1199. cw_min = fls(params->cw_min);
  1200. cw_max = fls(params->cw_max);
  1201. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  1202. priv->queue_param[queue] = *params;
  1203. rtl8187se_conf_ac_parm(dev, queue);
  1204. } else
  1205. rtl818x_iowrite8(priv, &priv->map->CW_VAL,
  1206. (cw_max << 4) | cw_min);
  1207. return 0;
  1208. }
  1209. static void rtl8180_conf_erp(struct ieee80211_hw *dev,
  1210. struct ieee80211_bss_conf *info)
  1211. {
  1212. struct rtl8180_priv *priv = dev->priv;
  1213. u8 sifs, difs;
  1214. int eifs;
  1215. u8 hw_eifs;
  1216. /* TODO: should we do something ? */
  1217. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
  1218. return;
  1219. /* I _hope_ this means 10uS for the HW.
  1220. * In reference code it is 0x22 for
  1221. * both rtl8187L and rtl8187SE
  1222. */
  1223. sifs = 0x22;
  1224. if (info->use_short_slot)
  1225. priv->slot_time = 9;
  1226. else
  1227. priv->slot_time = 20;
  1228. /* 10 is SIFS time in uS */
  1229. difs = 10 + 2 * priv->slot_time;
  1230. eifs = 10 + difs + priv->ack_time;
  1231. /* HW should use 4uS units for EIFS (I'm sure for rtl8185)*/
  1232. hw_eifs = DIV_ROUND_UP(eifs, 4);
  1233. rtl818x_iowrite8(priv, &priv->map->SLOT, priv->slot_time);
  1234. rtl818x_iowrite8(priv, &priv->map->SIFS, sifs);
  1235. rtl818x_iowrite8(priv, &priv->map->DIFS, difs);
  1236. /* from reference code. set ack timeout reg = eifs reg */
  1237. rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, hw_eifs);
  1238. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  1239. rtl818x_iowrite8(priv, &priv->map->EIFS_8187SE, hw_eifs);
  1240. else if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
  1241. /* rtl8187/rtl8185 HW bug. After EIFS is elapsed,
  1242. * the HW still wait for DIFS.
  1243. * HW uses 4uS units for EIFS.
  1244. */
  1245. hw_eifs = DIV_ROUND_UP(eifs - difs, 4);
  1246. rtl818x_iowrite8(priv, &priv->map->EIFS, hw_eifs);
  1247. }
  1248. }
  1249. static void rtl8180_bss_info_changed(struct ieee80211_hw *dev,
  1250. struct ieee80211_vif *vif,
  1251. struct ieee80211_bss_conf *info,
  1252. u64 changed)
  1253. {
  1254. struct rtl8180_priv *priv = dev->priv;
  1255. struct rtl8180_vif *vif_priv;
  1256. int i;
  1257. u8 reg;
  1258. vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
  1259. if (changed & BSS_CHANGED_BSSID) {
  1260. rtl818x_iowrite16(priv, (__le16 __iomem *)&priv->map->BSSID[0],
  1261. le16_to_cpu(*(__le16 *)info->bssid));
  1262. rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->BSSID[2],
  1263. le32_to_cpu(*(__le32 *)(info->bssid + 2)));
  1264. if (is_valid_ether_addr(info->bssid)) {
  1265. if (vif->type == NL80211_IFTYPE_ADHOC)
  1266. reg = RTL818X_MSR_ADHOC;
  1267. else
  1268. reg = RTL818X_MSR_INFRA;
  1269. } else
  1270. reg = RTL818X_MSR_NO_LINK;
  1271. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  1272. reg |= RTL818X_MSR_ENEDCA;
  1273. rtl818x_iowrite8(priv, &priv->map->MSR, reg);
  1274. }
  1275. if (changed & BSS_CHANGED_BASIC_RATES)
  1276. rtl8180_conf_basic_rates(dev, info->basic_rates);
  1277. if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_ERP_PREAMBLE)) {
  1278. /* when preamble changes, acktime duration changes, and erp must
  1279. * be recalculated. ACK time is calculated at lowest rate.
  1280. * Since mac80211 include SIFS time we remove it (-10)
  1281. */
  1282. priv->ack_time =
  1283. le16_to_cpu(ieee80211_generic_frame_duration(dev,
  1284. priv->vif,
  1285. NL80211_BAND_2GHZ, 10,
  1286. &priv->rates[0])) - 10;
  1287. rtl8180_conf_erp(dev, info);
  1288. /* mac80211 supplies aifs_n to driver and calls
  1289. * conf_tx callback whether aifs_n changes, NOT
  1290. * when aifs changes.
  1291. * Aifs should be recalculated if slot changes.
  1292. */
  1293. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  1294. for (i = 0; i < 4; i++)
  1295. rtl8187se_conf_ac_parm(dev, i);
  1296. }
  1297. }
  1298. if (changed & BSS_CHANGED_BEACON_ENABLED)
  1299. vif_priv->enable_beacon = info->enable_beacon;
  1300. if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON)) {
  1301. cancel_delayed_work_sync(&vif_priv->beacon_work);
  1302. if (vif_priv->enable_beacon)
  1303. schedule_work(&vif_priv->beacon_work.work);
  1304. }
  1305. }
  1306. static u64 rtl8180_prepare_multicast(struct ieee80211_hw *dev,
  1307. struct netdev_hw_addr_list *mc_list)
  1308. {
  1309. return netdev_hw_addr_list_count(mc_list);
  1310. }
  1311. static void rtl8180_configure_filter(struct ieee80211_hw *dev,
  1312. unsigned int changed_flags,
  1313. unsigned int *total_flags,
  1314. u64 multicast)
  1315. {
  1316. struct rtl8180_priv *priv = dev->priv;
  1317. if (changed_flags & FIF_FCSFAIL)
  1318. priv->rx_conf ^= RTL818X_RX_CONF_FCS;
  1319. if (changed_flags & FIF_CONTROL)
  1320. priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
  1321. if (changed_flags & FIF_OTHER_BSS)
  1322. priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
  1323. if (*total_flags & FIF_ALLMULTI || multicast > 0)
  1324. priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
  1325. else
  1326. priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
  1327. *total_flags = 0;
  1328. if (priv->rx_conf & RTL818X_RX_CONF_FCS)
  1329. *total_flags |= FIF_FCSFAIL;
  1330. if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
  1331. *total_flags |= FIF_CONTROL;
  1332. if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
  1333. *total_flags |= FIF_OTHER_BSS;
  1334. if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
  1335. *total_flags |= FIF_ALLMULTI;
  1336. rtl818x_iowrite32(priv, &priv->map->RX_CONF, priv->rx_conf);
  1337. }
  1338. static const struct ieee80211_ops rtl8180_ops = {
  1339. .add_chanctx = ieee80211_emulate_add_chanctx,
  1340. .remove_chanctx = ieee80211_emulate_remove_chanctx,
  1341. .change_chanctx = ieee80211_emulate_change_chanctx,
  1342. .switch_vif_chanctx = ieee80211_emulate_switch_vif_chanctx,
  1343. .tx = rtl8180_tx,
  1344. .wake_tx_queue = ieee80211_handle_wake_tx_queue,
  1345. .start = rtl8180_start,
  1346. .stop = rtl8180_stop,
  1347. .add_interface = rtl8180_add_interface,
  1348. .remove_interface = rtl8180_remove_interface,
  1349. .config = rtl8180_config,
  1350. .bss_info_changed = rtl8180_bss_info_changed,
  1351. .conf_tx = rtl8180_conf_tx,
  1352. .prepare_multicast = rtl8180_prepare_multicast,
  1353. .configure_filter = rtl8180_configure_filter,
  1354. .get_tsf = rtl8180_get_tsf,
  1355. };
  1356. static void rtl8180_eeprom_register_read(struct eeprom_93cx6 *eeprom)
  1357. {
  1358. struct rtl8180_priv *priv = eeprom->data;
  1359. u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  1360. eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
  1361. eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
  1362. eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
  1363. eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
  1364. }
  1365. static void rtl8180_eeprom_register_write(struct eeprom_93cx6 *eeprom)
  1366. {
  1367. struct rtl8180_priv *priv = eeprom->data;
  1368. u8 reg = 2 << 6;
  1369. if (eeprom->reg_data_in)
  1370. reg |= RTL818X_EEPROM_CMD_WRITE;
  1371. if (eeprom->reg_data_out)
  1372. reg |= RTL818X_EEPROM_CMD_READ;
  1373. if (eeprom->reg_data_clock)
  1374. reg |= RTL818X_EEPROM_CMD_CK;
  1375. if (eeprom->reg_chip_select)
  1376. reg |= RTL818X_EEPROM_CMD_CS;
  1377. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
  1378. rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  1379. udelay(10);
  1380. }
  1381. static void rtl8180_eeprom_read(struct rtl8180_priv *priv)
  1382. {
  1383. struct eeprom_93cx6 eeprom;
  1384. int eeprom_cck_table_adr;
  1385. u16 eeprom_val;
  1386. int i;
  1387. eeprom.data = priv;
  1388. eeprom.register_read = rtl8180_eeprom_register_read;
  1389. eeprom.register_write = rtl8180_eeprom_register_write;
  1390. if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
  1391. eeprom.width = PCI_EEPROM_WIDTH_93C66;
  1392. else
  1393. eeprom.width = PCI_EEPROM_WIDTH_93C46;
  1394. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  1395. RTL818X_EEPROM_CMD_PROGRAM);
  1396. rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  1397. udelay(10);
  1398. eeprom_93cx6_read(&eeprom, 0x06, &eeprom_val);
  1399. eeprom_val &= 0xFF;
  1400. priv->rf_type = eeprom_val;
  1401. eeprom_93cx6_read(&eeprom, 0x17, &eeprom_val);
  1402. priv->csthreshold = eeprom_val >> 8;
  1403. eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)priv->mac_addr, 3);
  1404. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  1405. eeprom_cck_table_adr = 0x30;
  1406. else
  1407. eeprom_cck_table_adr = 0x10;
  1408. /* CCK TX power */
  1409. for (i = 0; i < 14; i += 2) {
  1410. u16 txpwr;
  1411. eeprom_93cx6_read(&eeprom, eeprom_cck_table_adr + (i >> 1),
  1412. &txpwr);
  1413. priv->channels[i].hw_value = txpwr & 0xFF;
  1414. priv->channels[i + 1].hw_value = txpwr >> 8;
  1415. }
  1416. /* OFDM TX power */
  1417. if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
  1418. for (i = 0; i < 14; i += 2) {
  1419. u16 txpwr;
  1420. eeprom_93cx6_read(&eeprom, 0x20 + (i >> 1), &txpwr);
  1421. priv->channels[i].hw_value |= (txpwr & 0xFF) << 8;
  1422. priv->channels[i + 1].hw_value |= txpwr & 0xFF00;
  1423. }
  1424. }
  1425. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
  1426. __le32 anaparam;
  1427. eeprom_93cx6_multiread(&eeprom, 0xD, (__le16 *)&anaparam, 2);
  1428. priv->anaparam = le32_to_cpu(anaparam);
  1429. eeprom_93cx6_read(&eeprom, 0x19, &priv->rfparam);
  1430. }
  1431. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  1432. eeprom_93cx6_read(&eeprom, 0x3F, &eeprom_val);
  1433. priv->antenna_diversity_en = !!(eeprom_val & 0x100);
  1434. priv->antenna_diversity_default = (eeprom_val & 0xC00) == 0x400;
  1435. eeprom_93cx6_read(&eeprom, 0x7C, &eeprom_val);
  1436. priv->xtal_out = eeprom_val & 0xF;
  1437. priv->xtal_in = (eeprom_val & 0xF0) >> 4;
  1438. priv->xtal_cal = !!(eeprom_val & 0x1000);
  1439. priv->thermal_meter_val = (eeprom_val & 0xF00) >> 8;
  1440. priv->thermal_meter_en = !!(eeprom_val & 0x2000);
  1441. }
  1442. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  1443. RTL818X_EEPROM_CMD_NORMAL);
  1444. }
  1445. static int rtl8180_probe(struct pci_dev *pdev,
  1446. const struct pci_device_id *id)
  1447. {
  1448. struct ieee80211_hw *dev;
  1449. struct rtl8180_priv *priv;
  1450. unsigned long mem_len;
  1451. unsigned int io_len;
  1452. int err;
  1453. const char *chip_name, *rf_name = NULL;
  1454. u32 reg;
  1455. err = pci_enable_device(pdev);
  1456. if (err) {
  1457. printk(KERN_ERR "%s (rtl8180): Cannot enable new PCI device\n",
  1458. pci_name(pdev));
  1459. return err;
  1460. }
  1461. err = pci_request_regions(pdev, KBUILD_MODNAME);
  1462. if (err) {
  1463. printk(KERN_ERR "%s (rtl8180): Cannot obtain PCI resources\n",
  1464. pci_name(pdev));
  1465. goto err_disable_dev;
  1466. }
  1467. io_len = pci_resource_len(pdev, 0);
  1468. mem_len = pci_resource_len(pdev, 1);
  1469. if (mem_len < sizeof(struct rtl818x_csr) ||
  1470. io_len < sizeof(struct rtl818x_csr)) {
  1471. printk(KERN_ERR "%s (rtl8180): Too short PCI resources\n",
  1472. pci_name(pdev));
  1473. err = -ENOMEM;
  1474. goto err_free_reg;
  1475. }
  1476. if ((err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) ||
  1477. (err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32)))) {
  1478. printk(KERN_ERR "%s (rtl8180): No suitable DMA available\n",
  1479. pci_name(pdev));
  1480. goto err_free_reg;
  1481. }
  1482. pci_set_master(pdev);
  1483. dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8180_ops);
  1484. if (!dev) {
  1485. printk(KERN_ERR "%s (rtl8180): ieee80211 alloc failed\n",
  1486. pci_name(pdev));
  1487. err = -ENOMEM;
  1488. goto err_free_reg;
  1489. }
  1490. priv = dev->priv;
  1491. priv->pdev = pdev;
  1492. dev->max_rates = 1;
  1493. SET_IEEE80211_DEV(dev, &pdev->dev);
  1494. pci_set_drvdata(pdev, dev);
  1495. priv->map_pio = false;
  1496. priv->map = pci_iomap(pdev, 1, mem_len);
  1497. if (!priv->map) {
  1498. priv->map = pci_iomap(pdev, 0, io_len);
  1499. priv->map_pio = true;
  1500. }
  1501. if (!priv->map) {
  1502. dev_err(&pdev->dev, "Cannot map device memory/PIO\n");
  1503. err = -ENOMEM;
  1504. goto err_free_dev;
  1505. }
  1506. BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
  1507. BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
  1508. memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
  1509. memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
  1510. priv->band.band = NL80211_BAND_2GHZ;
  1511. priv->band.channels = priv->channels;
  1512. priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
  1513. priv->band.bitrates = priv->rates;
  1514. priv->band.n_bitrates = 4;
  1515. dev->wiphy->bands[NL80211_BAND_2GHZ] = &priv->band;
  1516. ieee80211_hw_set(dev, HOST_BROADCAST_PS_BUFFERING);
  1517. ieee80211_hw_set(dev, RX_INCLUDES_FCS);
  1518. dev->vif_data_size = sizeof(struct rtl8180_vif);
  1519. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  1520. BIT(NL80211_IFTYPE_ADHOC);
  1521. dev->max_signal = 65;
  1522. reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
  1523. reg &= RTL818X_TX_CONF_HWVER_MASK;
  1524. switch (reg) {
  1525. case RTL818X_TX_CONF_R8180_ABCD:
  1526. chip_name = "RTL8180";
  1527. priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
  1528. break;
  1529. case RTL818X_TX_CONF_R8180_F:
  1530. chip_name = "RTL8180vF";
  1531. priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
  1532. break;
  1533. case RTL818X_TX_CONF_R8185_ABC:
  1534. chip_name = "RTL8185";
  1535. priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
  1536. break;
  1537. case RTL818X_TX_CONF_R8185_D:
  1538. chip_name = "RTL8185vD";
  1539. priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
  1540. break;
  1541. case RTL818X_TX_CONF_RTL8187SE:
  1542. chip_name = "RTL8187SE";
  1543. if (priv->map_pio) {
  1544. dev_err(&pdev->dev,
  1545. "MMIO failed. PIO not supported on RTL8187SE\n");
  1546. err = -ENOMEM;
  1547. goto err_iounmap;
  1548. }
  1549. priv->chip_family = RTL818X_CHIP_FAMILY_RTL8187SE;
  1550. break;
  1551. default:
  1552. printk(KERN_ERR "%s (rtl8180): Unknown chip! (0x%x)\n",
  1553. pci_name(pdev), reg >> 25);
  1554. err = -ENODEV;
  1555. goto err_iounmap;
  1556. }
  1557. /* we declare to MAC80211 all the queues except for beacon queue
  1558. * that will be eventually handled by DRV.
  1559. * TX rings are arranged in such a way that lower is the IDX,
  1560. * higher is the priority, in order to achieve direct mapping
  1561. * with mac80211, however the beacon queue is an exception and it
  1562. * is mapped on the highst tx ring IDX.
  1563. */
  1564. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  1565. dev->queues = RTL8187SE_NR_TX_QUEUES - 1;
  1566. else
  1567. dev->queues = RTL8180_NR_TX_QUEUES - 1;
  1568. if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
  1569. priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
  1570. pci_try_set_mwi(pdev);
  1571. }
  1572. if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
  1573. ieee80211_hw_set(dev, SIGNAL_DBM);
  1574. else
  1575. ieee80211_hw_set(dev, SIGNAL_UNSPEC);
  1576. wiphy_ext_feature_set(dev->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
  1577. rtl8180_eeprom_read(priv);
  1578. switch (priv->rf_type) {
  1579. case 1: rf_name = "Intersil";
  1580. break;
  1581. case 2: rf_name = "RFMD";
  1582. break;
  1583. case 3: priv->rf = &sa2400_rf_ops;
  1584. break;
  1585. case 4: priv->rf = &max2820_rf_ops;
  1586. break;
  1587. case 5: priv->rf = &grf5101_rf_ops;
  1588. break;
  1589. case 9:
  1590. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  1591. priv->rf = rtl8187se_detect_rf(dev);
  1592. else
  1593. priv->rf = rtl8180_detect_rf(dev);
  1594. break;
  1595. case 10:
  1596. rf_name = "RTL8255";
  1597. break;
  1598. default:
  1599. printk(KERN_ERR "%s (rtl8180): Unknown RF! (0x%x)\n",
  1600. pci_name(pdev), priv->rf_type);
  1601. err = -ENODEV;
  1602. goto err_iounmap;
  1603. }
  1604. if (!priv->rf) {
  1605. printk(KERN_ERR "%s (rtl8180): %s RF frontend not supported!\n",
  1606. pci_name(pdev), rf_name);
  1607. err = -ENODEV;
  1608. goto err_iounmap;
  1609. }
  1610. if (!is_valid_ether_addr(priv->mac_addr)) {
  1611. printk(KERN_WARNING "%s (rtl8180): Invalid hwaddr! Using"
  1612. " randomly generated MAC addr\n", pci_name(pdev));
  1613. eth_random_addr(priv->mac_addr);
  1614. }
  1615. SET_IEEE80211_PERM_ADDR(dev, priv->mac_addr);
  1616. spin_lock_init(&priv->lock);
  1617. err = ieee80211_register_hw(dev);
  1618. if (err) {
  1619. printk(KERN_ERR "%s (rtl8180): Cannot register device\n",
  1620. pci_name(pdev));
  1621. goto err_iounmap;
  1622. }
  1623. wiphy_info(dev->wiphy, "hwaddr %pm, %s + %s\n",
  1624. priv->mac_addr, chip_name, priv->rf->name);
  1625. return 0;
  1626. err_iounmap:
  1627. pci_iounmap(pdev, priv->map);
  1628. err_free_dev:
  1629. ieee80211_free_hw(dev);
  1630. err_free_reg:
  1631. pci_release_regions(pdev);
  1632. err_disable_dev:
  1633. pci_disable_device(pdev);
  1634. return err;
  1635. }
  1636. static void rtl8180_remove(struct pci_dev *pdev)
  1637. {
  1638. struct ieee80211_hw *dev = pci_get_drvdata(pdev);
  1639. struct rtl8180_priv *priv;
  1640. if (!dev)
  1641. return;
  1642. ieee80211_unregister_hw(dev);
  1643. priv = dev->priv;
  1644. pci_iounmap(pdev, priv->map);
  1645. pci_release_regions(pdev);
  1646. pci_disable_device(pdev);
  1647. ieee80211_free_hw(dev);
  1648. }
  1649. #define rtl8180_suspend NULL
  1650. #define rtl8180_resume NULL
  1651. static SIMPLE_DEV_PM_OPS(rtl8180_pm_ops, rtl8180_suspend, rtl8180_resume);
  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. .driver.pm = &rtl8180_pm_ops,
  1658. };
  1659. module_pci_driver(rtl8180_driver);