dev.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990
  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.flag |= RX_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. IEEE80211_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. wiphy_err(dev->wiphy, "Cannot allocate RX skb\n");
  863. return -ENOMEM;
  864. }
  865. priv->rx_buf[i] = skb;
  866. mapping = (dma_addr_t *)skb->cb;
  867. *mapping = pci_map_single(priv->pdev, skb_tail_pointer(skb),
  868. MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
  869. if (pci_dma_mapping_error(priv->pdev, *mapping)) {
  870. kfree_skb(skb);
  871. wiphy_err(dev->wiphy, "Cannot map DMA for RX skb\n");
  872. return -ENOMEM;
  873. }
  874. entry->rx_buf = cpu_to_le32(*mapping);
  875. entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
  876. MAX_RX_SIZE);
  877. }
  878. entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
  879. return 0;
  880. }
  881. static void rtl8180_free_rx_ring(struct ieee80211_hw *dev)
  882. {
  883. struct rtl8180_priv *priv = dev->priv;
  884. int i;
  885. for (i = 0; i < 32; i++) {
  886. struct sk_buff *skb = priv->rx_buf[i];
  887. if (!skb)
  888. continue;
  889. pci_unmap_single(priv->pdev,
  890. *((dma_addr_t *)skb->cb),
  891. MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
  892. kfree_skb(skb);
  893. }
  894. pci_free_consistent(priv->pdev, priv->rx_ring_sz * 32,
  895. priv->rx_ring, priv->rx_ring_dma);
  896. priv->rx_ring = NULL;
  897. }
  898. static int rtl8180_init_tx_ring(struct ieee80211_hw *dev,
  899. unsigned int prio, unsigned int entries)
  900. {
  901. struct rtl8180_priv *priv = dev->priv;
  902. struct rtl8180_tx_desc *ring;
  903. dma_addr_t dma;
  904. int i;
  905. ring = pci_zalloc_consistent(priv->pdev, sizeof(*ring) * entries,
  906. &dma);
  907. if (!ring || (unsigned long)ring & 0xFF) {
  908. wiphy_err(dev->wiphy, "Cannot allocate TX ring (prio = %d)\n",
  909. prio);
  910. return -ENOMEM;
  911. }
  912. priv->tx_ring[prio].desc = ring;
  913. priv->tx_ring[prio].dma = dma;
  914. priv->tx_ring[prio].idx = 0;
  915. priv->tx_ring[prio].entries = entries;
  916. skb_queue_head_init(&priv->tx_ring[prio].queue);
  917. for (i = 0; i < entries; i++)
  918. ring[i].next_tx_desc =
  919. cpu_to_le32((u32)dma + ((i + 1) % entries) * sizeof(*ring));
  920. return 0;
  921. }
  922. static void rtl8180_free_tx_ring(struct ieee80211_hw *dev, unsigned int prio)
  923. {
  924. struct rtl8180_priv *priv = dev->priv;
  925. struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
  926. while (skb_queue_len(&ring->queue)) {
  927. struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
  928. struct sk_buff *skb = __skb_dequeue(&ring->queue);
  929. pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
  930. skb->len, PCI_DMA_TODEVICE);
  931. kfree_skb(skb);
  932. ring->idx = (ring->idx + 1) % ring->entries;
  933. }
  934. pci_free_consistent(priv->pdev, sizeof(*ring->desc)*ring->entries,
  935. ring->desc, ring->dma);
  936. ring->desc = NULL;
  937. }
  938. static int rtl8180_start(struct ieee80211_hw *dev)
  939. {
  940. struct rtl8180_priv *priv = dev->priv;
  941. int ret, i;
  942. u32 reg;
  943. ret = rtl8180_init_rx_ring(dev);
  944. if (ret)
  945. return ret;
  946. for (i = 0; i < (dev->queues + 1); i++)
  947. if ((ret = rtl8180_init_tx_ring(dev, i, 16)))
  948. goto err_free_rings;
  949. ret = rtl8180_init_hw(dev);
  950. if (ret)
  951. goto err_free_rings;
  952. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  953. ret = request_irq(priv->pdev->irq, rtl8187se_interrupt,
  954. IRQF_SHARED, KBUILD_MODNAME, dev);
  955. } else {
  956. ret = request_irq(priv->pdev->irq, rtl8180_interrupt,
  957. IRQF_SHARED, KBUILD_MODNAME, dev);
  958. }
  959. if (ret) {
  960. wiphy_err(dev->wiphy, "failed to register IRQ handler\n");
  961. goto err_free_rings;
  962. }
  963. rtl8180_int_enable(dev);
  964. /* in rtl8187se at MAR regs offset there is the management
  965. * TX descriptor DMA addres..
  966. */
  967. if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8187SE) {
  968. rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
  969. rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
  970. }
  971. reg = RTL818X_RX_CONF_ONLYERLPKT |
  972. RTL818X_RX_CONF_RX_AUTORESETPHY |
  973. RTL818X_RX_CONF_MGMT |
  974. RTL818X_RX_CONF_DATA |
  975. (7 << 8 /* MAX RX DMA */) |
  976. RTL818X_RX_CONF_BROADCAST |
  977. RTL818X_RX_CONF_NICMAC;
  978. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185)
  979. reg |= RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2;
  980. else if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
  981. reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE1)
  982. ? RTL818X_RX_CONF_CSDM1 : 0;
  983. reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE2)
  984. ? RTL818X_RX_CONF_CSDM2 : 0;
  985. } else {
  986. reg &= ~(RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2);
  987. }
  988. priv->rx_conf = reg;
  989. rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
  990. if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
  991. reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
  992. /* CW is not on per-packet basis.
  993. * in rtl8185 the CW_VALUE reg is used.
  994. * in rtl8187se the AC param regs are used.
  995. */
  996. reg &= ~RTL818X_CW_CONF_PERPACKET_CW;
  997. /* retry limit IS on per-packet basis.
  998. * the short and long retry limit in TX_CONF
  999. * reg are ignored
  1000. */
  1001. reg |= RTL818X_CW_CONF_PERPACKET_RETRY;
  1002. rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
  1003. reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
  1004. /* TX antenna and TX gain are not on per-packet basis.
  1005. * TX Antenna is selected by ANTSEL reg (RX in BB regs).
  1006. * TX gain is selected with CCK_TX_AGC and OFDM_TX_AGC regs
  1007. */
  1008. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN;
  1009. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL;
  1010. reg |= RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
  1011. rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
  1012. /* disable early TX */
  1013. rtl818x_iowrite8(priv, (u8 __iomem *)priv->map + 0xec, 0x3f);
  1014. }
  1015. reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
  1016. reg |= (6 << 21 /* MAX TX DMA */) |
  1017. RTL818X_TX_CONF_NO_ICV;
  1018. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  1019. reg |= 1<<30; /* "duration procedure mode" */
  1020. if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
  1021. reg &= ~RTL818X_TX_CONF_PROBE_DTS;
  1022. else
  1023. reg &= ~RTL818X_TX_CONF_HW_SEQNUM;
  1024. reg &= ~RTL818X_TX_CONF_DISCW;
  1025. /* different meaning, same value on both rtl8185 and rtl8180 */
  1026. reg &= ~RTL818X_TX_CONF_SAT_HWPLCP;
  1027. rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
  1028. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  1029. reg |= RTL818X_CMD_RX_ENABLE;
  1030. reg |= RTL818X_CMD_TX_ENABLE;
  1031. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  1032. return 0;
  1033. err_free_rings:
  1034. rtl8180_free_rx_ring(dev);
  1035. for (i = 0; i < (dev->queues + 1); i++)
  1036. if (priv->tx_ring[i].desc)
  1037. rtl8180_free_tx_ring(dev, i);
  1038. return ret;
  1039. }
  1040. static void rtl8180_stop(struct ieee80211_hw *dev)
  1041. {
  1042. struct rtl8180_priv *priv = dev->priv;
  1043. u8 reg;
  1044. int i;
  1045. rtl8180_int_disable(dev);
  1046. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  1047. reg &= ~RTL818X_CMD_TX_ENABLE;
  1048. reg &= ~RTL818X_CMD_RX_ENABLE;
  1049. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  1050. priv->rf->stop(dev);
  1051. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  1052. reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
  1053. rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
  1054. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  1055. free_irq(priv->pdev->irq, dev);
  1056. rtl8180_free_rx_ring(dev);
  1057. for (i = 0; i < (dev->queues + 1); i++)
  1058. rtl8180_free_tx_ring(dev, i);
  1059. }
  1060. static u64 rtl8180_get_tsf(struct ieee80211_hw *dev,
  1061. struct ieee80211_vif *vif)
  1062. {
  1063. struct rtl8180_priv *priv = dev->priv;
  1064. return rtl818x_ioread32(priv, &priv->map->TSFT[0]) |
  1065. (u64)(rtl818x_ioread32(priv, &priv->map->TSFT[1])) << 32;
  1066. }
  1067. static void rtl8180_beacon_work(struct work_struct *work)
  1068. {
  1069. struct rtl8180_vif *vif_priv =
  1070. container_of(work, struct rtl8180_vif, beacon_work.work);
  1071. struct ieee80211_vif *vif =
  1072. container_of((void *)vif_priv, struct ieee80211_vif, drv_priv);
  1073. struct ieee80211_hw *dev = vif_priv->dev;
  1074. struct ieee80211_mgmt *mgmt;
  1075. struct sk_buff *skb;
  1076. /* don't overflow the tx ring */
  1077. if (ieee80211_queue_stopped(dev, 0))
  1078. goto resched;
  1079. /* grab a fresh beacon */
  1080. skb = ieee80211_beacon_get(dev, vif);
  1081. if (!skb)
  1082. goto resched;
  1083. /*
  1084. * update beacon timestamp w/ TSF value
  1085. * TODO: make hardware update beacon timestamp
  1086. */
  1087. mgmt = (struct ieee80211_mgmt *)skb->data;
  1088. mgmt->u.beacon.timestamp = cpu_to_le64(rtl8180_get_tsf(dev, vif));
  1089. /* TODO: use actual beacon queue */
  1090. skb_set_queue_mapping(skb, 0);
  1091. rtl8180_tx(dev, NULL, skb);
  1092. resched:
  1093. /*
  1094. * schedule next beacon
  1095. * TODO: use hardware support for beacon timing
  1096. */
  1097. schedule_delayed_work(&vif_priv->beacon_work,
  1098. usecs_to_jiffies(1024 * vif->bss_conf.beacon_int));
  1099. }
  1100. static int rtl8180_add_interface(struct ieee80211_hw *dev,
  1101. struct ieee80211_vif *vif)
  1102. {
  1103. struct rtl8180_priv *priv = dev->priv;
  1104. struct rtl8180_vif *vif_priv;
  1105. /*
  1106. * We only support one active interface at a time.
  1107. */
  1108. if (priv->vif)
  1109. return -EBUSY;
  1110. switch (vif->type) {
  1111. case NL80211_IFTYPE_STATION:
  1112. case NL80211_IFTYPE_ADHOC:
  1113. break;
  1114. default:
  1115. return -EOPNOTSUPP;
  1116. }
  1117. priv->vif = vif;
  1118. /* Initialize driver private area */
  1119. vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
  1120. vif_priv->dev = dev;
  1121. INIT_DELAYED_WORK(&vif_priv->beacon_work, rtl8180_beacon_work);
  1122. vif_priv->enable_beacon = false;
  1123. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  1124. rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->MAC[0],
  1125. le32_to_cpu(*(__le32 *)vif->addr));
  1126. rtl818x_iowrite16(priv, (__le16 __iomem *)&priv->map->MAC[4],
  1127. le16_to_cpu(*(__le16 *)(vif->addr + 4)));
  1128. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  1129. return 0;
  1130. }
  1131. static void rtl8180_remove_interface(struct ieee80211_hw *dev,
  1132. struct ieee80211_vif *vif)
  1133. {
  1134. struct rtl8180_priv *priv = dev->priv;
  1135. priv->vif = NULL;
  1136. }
  1137. static int rtl8180_config(struct ieee80211_hw *dev, u32 changed)
  1138. {
  1139. struct rtl8180_priv *priv = dev->priv;
  1140. struct ieee80211_conf *conf = &dev->conf;
  1141. priv->rf->set_chan(dev, conf);
  1142. return 0;
  1143. }
  1144. static void rtl8187se_conf_ac_parm(struct ieee80211_hw *dev, u8 queue)
  1145. {
  1146. const struct ieee80211_tx_queue_params *params;
  1147. struct rtl8180_priv *priv = dev->priv;
  1148. /* hw value */
  1149. u32 ac_param;
  1150. u8 aifs;
  1151. u8 txop;
  1152. u8 cw_min, cw_max;
  1153. params = &priv->queue_param[queue];
  1154. cw_min = fls(params->cw_min);
  1155. cw_max = fls(params->cw_max);
  1156. aifs = 10 + params->aifs * priv->slot_time;
  1157. /* TODO: check if txop HW is in us (mult by 32) */
  1158. txop = params->txop;
  1159. ac_param = txop << AC_PARAM_TXOP_LIMIT_SHIFT |
  1160. cw_max << AC_PARAM_ECW_MAX_SHIFT |
  1161. cw_min << AC_PARAM_ECW_MIN_SHIFT |
  1162. aifs << AC_PARAM_AIFS_SHIFT;
  1163. switch (queue) {
  1164. case IEEE80211_AC_BK:
  1165. rtl818x_iowrite32(priv, &priv->map->AC_BK_PARAM, ac_param);
  1166. break;
  1167. case IEEE80211_AC_BE:
  1168. rtl818x_iowrite32(priv, &priv->map->AC_BE_PARAM, ac_param);
  1169. break;
  1170. case IEEE80211_AC_VI:
  1171. rtl818x_iowrite32(priv, &priv->map->AC_VI_PARAM, ac_param);
  1172. break;
  1173. case IEEE80211_AC_VO:
  1174. rtl818x_iowrite32(priv, &priv->map->AC_VO_PARAM, ac_param);
  1175. break;
  1176. }
  1177. }
  1178. static int rtl8180_conf_tx(struct ieee80211_hw *dev,
  1179. struct ieee80211_vif *vif, u16 queue,
  1180. const struct ieee80211_tx_queue_params *params)
  1181. {
  1182. struct rtl8180_priv *priv = dev->priv;
  1183. u8 cw_min, cw_max;
  1184. /* nothing to do ? */
  1185. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
  1186. return 0;
  1187. cw_min = fls(params->cw_min);
  1188. cw_max = fls(params->cw_max);
  1189. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  1190. priv->queue_param[queue] = *params;
  1191. rtl8187se_conf_ac_parm(dev, queue);
  1192. } else
  1193. rtl818x_iowrite8(priv, &priv->map->CW_VAL,
  1194. (cw_max << 4) | cw_min);
  1195. return 0;
  1196. }
  1197. static void rtl8180_conf_erp(struct ieee80211_hw *dev,
  1198. struct ieee80211_bss_conf *info)
  1199. {
  1200. struct rtl8180_priv *priv = dev->priv;
  1201. u8 sifs, difs;
  1202. int eifs;
  1203. u8 hw_eifs;
  1204. /* TODO: should we do something ? */
  1205. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
  1206. return;
  1207. /* I _hope_ this means 10uS for the HW.
  1208. * In reference code it is 0x22 for
  1209. * both rtl8187L and rtl8187SE
  1210. */
  1211. sifs = 0x22;
  1212. if (info->use_short_slot)
  1213. priv->slot_time = 9;
  1214. else
  1215. priv->slot_time = 20;
  1216. /* 10 is SIFS time in uS */
  1217. difs = 10 + 2 * priv->slot_time;
  1218. eifs = 10 + difs + priv->ack_time;
  1219. /* HW should use 4uS units for EIFS (I'm sure for rtl8185)*/
  1220. hw_eifs = DIV_ROUND_UP(eifs, 4);
  1221. rtl818x_iowrite8(priv, &priv->map->SLOT, priv->slot_time);
  1222. rtl818x_iowrite8(priv, &priv->map->SIFS, sifs);
  1223. rtl818x_iowrite8(priv, &priv->map->DIFS, difs);
  1224. /* from reference code. set ack timeout reg = eifs reg */
  1225. rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, hw_eifs);
  1226. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  1227. rtl818x_iowrite8(priv, &priv->map->EIFS_8187SE, hw_eifs);
  1228. else if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
  1229. /* rtl8187/rtl8185 HW bug. After EIFS is elapsed,
  1230. * the HW still wait for DIFS.
  1231. * HW uses 4uS units for EIFS.
  1232. */
  1233. hw_eifs = DIV_ROUND_UP(eifs - difs, 4);
  1234. rtl818x_iowrite8(priv, &priv->map->EIFS, hw_eifs);
  1235. }
  1236. }
  1237. static void rtl8180_bss_info_changed(struct ieee80211_hw *dev,
  1238. struct ieee80211_vif *vif,
  1239. struct ieee80211_bss_conf *info,
  1240. u32 changed)
  1241. {
  1242. struct rtl8180_priv *priv = dev->priv;
  1243. struct rtl8180_vif *vif_priv;
  1244. int i;
  1245. u8 reg;
  1246. vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
  1247. if (changed & BSS_CHANGED_BSSID) {
  1248. rtl818x_iowrite16(priv, (__le16 __iomem *)&priv->map->BSSID[0],
  1249. le16_to_cpu(*(__le16 *)info->bssid));
  1250. rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->BSSID[2],
  1251. le32_to_cpu(*(__le32 *)(info->bssid + 2)));
  1252. if (is_valid_ether_addr(info->bssid)) {
  1253. if (vif->type == NL80211_IFTYPE_ADHOC)
  1254. reg = RTL818X_MSR_ADHOC;
  1255. else
  1256. reg = RTL818X_MSR_INFRA;
  1257. } else
  1258. reg = RTL818X_MSR_NO_LINK;
  1259. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  1260. reg |= RTL818X_MSR_ENEDCA;
  1261. rtl818x_iowrite8(priv, &priv->map->MSR, reg);
  1262. }
  1263. if (changed & BSS_CHANGED_BASIC_RATES)
  1264. rtl8180_conf_basic_rates(dev, info->basic_rates);
  1265. if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_ERP_PREAMBLE)) {
  1266. /* when preamble changes, acktime duration changes, and erp must
  1267. * be recalculated. ACK time is calculated at lowest rate.
  1268. * Since mac80211 include SIFS time we remove it (-10)
  1269. */
  1270. priv->ack_time =
  1271. le16_to_cpu(ieee80211_generic_frame_duration(dev,
  1272. priv->vif,
  1273. IEEE80211_BAND_2GHZ, 10,
  1274. &priv->rates[0])) - 10;
  1275. rtl8180_conf_erp(dev, info);
  1276. /* mac80211 supplies aifs_n to driver and calls
  1277. * conf_tx callback whether aifs_n changes, NOT
  1278. * when aifs changes.
  1279. * Aifs should be recalculated if slot changes.
  1280. */
  1281. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  1282. for (i = 0; i < 4; i++)
  1283. rtl8187se_conf_ac_parm(dev, i);
  1284. }
  1285. }
  1286. if (changed & BSS_CHANGED_BEACON_ENABLED)
  1287. vif_priv->enable_beacon = info->enable_beacon;
  1288. if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON)) {
  1289. cancel_delayed_work_sync(&vif_priv->beacon_work);
  1290. if (vif_priv->enable_beacon)
  1291. schedule_work(&vif_priv->beacon_work.work);
  1292. }
  1293. }
  1294. static u64 rtl8180_prepare_multicast(struct ieee80211_hw *dev,
  1295. struct netdev_hw_addr_list *mc_list)
  1296. {
  1297. return netdev_hw_addr_list_count(mc_list);
  1298. }
  1299. static void rtl8180_configure_filter(struct ieee80211_hw *dev,
  1300. unsigned int changed_flags,
  1301. unsigned int *total_flags,
  1302. u64 multicast)
  1303. {
  1304. struct rtl8180_priv *priv = dev->priv;
  1305. if (changed_flags & FIF_FCSFAIL)
  1306. priv->rx_conf ^= RTL818X_RX_CONF_FCS;
  1307. if (changed_flags & FIF_CONTROL)
  1308. priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
  1309. if (changed_flags & FIF_OTHER_BSS)
  1310. priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
  1311. if (*total_flags & FIF_ALLMULTI || multicast > 0)
  1312. priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
  1313. else
  1314. priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
  1315. *total_flags = 0;
  1316. if (priv->rx_conf & RTL818X_RX_CONF_FCS)
  1317. *total_flags |= FIF_FCSFAIL;
  1318. if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
  1319. *total_flags |= FIF_CONTROL;
  1320. if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
  1321. *total_flags |= FIF_OTHER_BSS;
  1322. if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
  1323. *total_flags |= FIF_ALLMULTI;
  1324. rtl818x_iowrite32(priv, &priv->map->RX_CONF, priv->rx_conf);
  1325. }
  1326. static const struct ieee80211_ops rtl8180_ops = {
  1327. .tx = rtl8180_tx,
  1328. .start = rtl8180_start,
  1329. .stop = rtl8180_stop,
  1330. .add_interface = rtl8180_add_interface,
  1331. .remove_interface = rtl8180_remove_interface,
  1332. .config = rtl8180_config,
  1333. .bss_info_changed = rtl8180_bss_info_changed,
  1334. .conf_tx = rtl8180_conf_tx,
  1335. .prepare_multicast = rtl8180_prepare_multicast,
  1336. .configure_filter = rtl8180_configure_filter,
  1337. .get_tsf = rtl8180_get_tsf,
  1338. };
  1339. static void rtl8180_eeprom_register_read(struct eeprom_93cx6 *eeprom)
  1340. {
  1341. struct rtl8180_priv *priv = eeprom->data;
  1342. u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  1343. eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
  1344. eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
  1345. eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
  1346. eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
  1347. }
  1348. static void rtl8180_eeprom_register_write(struct eeprom_93cx6 *eeprom)
  1349. {
  1350. struct rtl8180_priv *priv = eeprom->data;
  1351. u8 reg = 2 << 6;
  1352. if (eeprom->reg_data_in)
  1353. reg |= RTL818X_EEPROM_CMD_WRITE;
  1354. if (eeprom->reg_data_out)
  1355. reg |= RTL818X_EEPROM_CMD_READ;
  1356. if (eeprom->reg_data_clock)
  1357. reg |= RTL818X_EEPROM_CMD_CK;
  1358. if (eeprom->reg_chip_select)
  1359. reg |= RTL818X_EEPROM_CMD_CS;
  1360. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
  1361. rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  1362. udelay(10);
  1363. }
  1364. static void rtl8180_eeprom_read(struct rtl8180_priv *priv)
  1365. {
  1366. struct eeprom_93cx6 eeprom;
  1367. int eeprom_cck_table_adr;
  1368. u16 eeprom_val;
  1369. int i;
  1370. eeprom.data = priv;
  1371. eeprom.register_read = rtl8180_eeprom_register_read;
  1372. eeprom.register_write = rtl8180_eeprom_register_write;
  1373. if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
  1374. eeprom.width = PCI_EEPROM_WIDTH_93C66;
  1375. else
  1376. eeprom.width = PCI_EEPROM_WIDTH_93C46;
  1377. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  1378. RTL818X_EEPROM_CMD_PROGRAM);
  1379. rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  1380. udelay(10);
  1381. eeprom_93cx6_read(&eeprom, 0x06, &eeprom_val);
  1382. eeprom_val &= 0xFF;
  1383. priv->rf_type = eeprom_val;
  1384. eeprom_93cx6_read(&eeprom, 0x17, &eeprom_val);
  1385. priv->csthreshold = eeprom_val >> 8;
  1386. eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)priv->mac_addr, 3);
  1387. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  1388. eeprom_cck_table_adr = 0x30;
  1389. else
  1390. eeprom_cck_table_adr = 0x10;
  1391. /* CCK TX power */
  1392. for (i = 0; i < 14; i += 2) {
  1393. u16 txpwr;
  1394. eeprom_93cx6_read(&eeprom, eeprom_cck_table_adr + (i >> 1),
  1395. &txpwr);
  1396. priv->channels[i].hw_value = txpwr & 0xFF;
  1397. priv->channels[i + 1].hw_value = txpwr >> 8;
  1398. }
  1399. /* OFDM TX power */
  1400. if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
  1401. for (i = 0; i < 14; i += 2) {
  1402. u16 txpwr;
  1403. eeprom_93cx6_read(&eeprom, 0x20 + (i >> 1), &txpwr);
  1404. priv->channels[i].hw_value |= (txpwr & 0xFF) << 8;
  1405. priv->channels[i + 1].hw_value |= txpwr & 0xFF00;
  1406. }
  1407. }
  1408. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
  1409. __le32 anaparam;
  1410. eeprom_93cx6_multiread(&eeprom, 0xD, (__le16 *)&anaparam, 2);
  1411. priv->anaparam = le32_to_cpu(anaparam);
  1412. eeprom_93cx6_read(&eeprom, 0x19, &priv->rfparam);
  1413. }
  1414. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
  1415. eeprom_93cx6_read(&eeprom, 0x3F, &eeprom_val);
  1416. priv->antenna_diversity_en = !!(eeprom_val & 0x100);
  1417. priv->antenna_diversity_default = (eeprom_val & 0xC00) == 0x400;
  1418. eeprom_93cx6_read(&eeprom, 0x7C, &eeprom_val);
  1419. priv->xtal_out = eeprom_val & 0xF;
  1420. priv->xtal_in = (eeprom_val & 0xF0) >> 4;
  1421. priv->xtal_cal = !!(eeprom_val & 0x1000);
  1422. priv->thermal_meter_val = (eeprom_val & 0xF00) >> 8;
  1423. priv->thermal_meter_en = !!(eeprom_val & 0x2000);
  1424. }
  1425. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  1426. RTL818X_EEPROM_CMD_NORMAL);
  1427. }
  1428. static int rtl8180_probe(struct pci_dev *pdev,
  1429. const struct pci_device_id *id)
  1430. {
  1431. struct ieee80211_hw *dev;
  1432. struct rtl8180_priv *priv;
  1433. unsigned long mem_addr, mem_len;
  1434. unsigned int io_addr, io_len;
  1435. int err;
  1436. const char *chip_name, *rf_name = NULL;
  1437. u32 reg;
  1438. err = pci_enable_device(pdev);
  1439. if (err) {
  1440. printk(KERN_ERR "%s (rtl8180): Cannot enable new PCI device\n",
  1441. pci_name(pdev));
  1442. return err;
  1443. }
  1444. err = pci_request_regions(pdev, KBUILD_MODNAME);
  1445. if (err) {
  1446. printk(KERN_ERR "%s (rtl8180): Cannot obtain PCI resources\n",
  1447. pci_name(pdev));
  1448. return err;
  1449. }
  1450. io_addr = pci_resource_start(pdev, 0);
  1451. io_len = pci_resource_len(pdev, 0);
  1452. mem_addr = pci_resource_start(pdev, 1);
  1453. mem_len = pci_resource_len(pdev, 1);
  1454. if (mem_len < sizeof(struct rtl818x_csr) ||
  1455. io_len < sizeof(struct rtl818x_csr)) {
  1456. printk(KERN_ERR "%s (rtl8180): Too short PCI resources\n",
  1457. pci_name(pdev));
  1458. err = -ENOMEM;
  1459. goto err_free_reg;
  1460. }
  1461. if ((err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) ||
  1462. (err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))) {
  1463. printk(KERN_ERR "%s (rtl8180): No suitable DMA available\n",
  1464. pci_name(pdev));
  1465. goto err_free_reg;
  1466. }
  1467. pci_set_master(pdev);
  1468. dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8180_ops);
  1469. if (!dev) {
  1470. printk(KERN_ERR "%s (rtl8180): ieee80211 alloc failed\n",
  1471. pci_name(pdev));
  1472. err = -ENOMEM;
  1473. goto err_free_reg;
  1474. }
  1475. priv = dev->priv;
  1476. priv->pdev = pdev;
  1477. dev->max_rates = 1;
  1478. SET_IEEE80211_DEV(dev, &pdev->dev);
  1479. pci_set_drvdata(pdev, dev);
  1480. priv->map_pio = false;
  1481. priv->map = pci_iomap(pdev, 1, mem_len);
  1482. if (!priv->map) {
  1483. priv->map = pci_iomap(pdev, 0, io_len);
  1484. priv->map_pio = true;
  1485. }
  1486. if (!priv->map) {
  1487. dev_err(&pdev->dev, "Cannot map device memory/PIO\n");
  1488. err = -ENOMEM;
  1489. goto err_free_dev;
  1490. }
  1491. BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
  1492. BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
  1493. memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
  1494. memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
  1495. priv->band.band = IEEE80211_BAND_2GHZ;
  1496. priv->band.channels = priv->channels;
  1497. priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
  1498. priv->band.bitrates = priv->rates;
  1499. priv->band.n_bitrates = 4;
  1500. dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  1501. dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  1502. IEEE80211_HW_RX_INCLUDES_FCS;
  1503. dev->vif_data_size = sizeof(struct rtl8180_vif);
  1504. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  1505. BIT(NL80211_IFTYPE_ADHOC);
  1506. dev->max_signal = 65;
  1507. reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
  1508. reg &= RTL818X_TX_CONF_HWVER_MASK;
  1509. switch (reg) {
  1510. case RTL818X_TX_CONF_R8180_ABCD:
  1511. chip_name = "RTL8180";
  1512. priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
  1513. break;
  1514. case RTL818X_TX_CONF_R8180_F:
  1515. chip_name = "RTL8180vF";
  1516. priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
  1517. break;
  1518. case RTL818X_TX_CONF_R8185_ABC:
  1519. chip_name = "RTL8185";
  1520. priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
  1521. break;
  1522. case RTL818X_TX_CONF_R8185_D:
  1523. chip_name = "RTL8185vD";
  1524. priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
  1525. break;
  1526. case RTL818X_TX_CONF_RTL8187SE:
  1527. chip_name = "RTL8187SE";
  1528. if (priv->map_pio) {
  1529. dev_err(&pdev->dev,
  1530. "MMIO failed. PIO not supported on RTL8187SE\n");
  1531. err = -ENOMEM;
  1532. goto err_iounmap;
  1533. }
  1534. priv->chip_family = RTL818X_CHIP_FAMILY_RTL8187SE;
  1535. break;
  1536. default:
  1537. printk(KERN_ERR "%s (rtl8180): Unknown chip! (0x%x)\n",
  1538. pci_name(pdev), reg >> 25);
  1539. err = -ENODEV;
  1540. goto err_iounmap;
  1541. }
  1542. /* we declare to MAC80211 all the queues except for beacon queue
  1543. * that will be eventually handled by DRV.
  1544. * TX rings are arranged in such a way that lower is the IDX,
  1545. * higher is the priority, in order to achieve direct mapping
  1546. * with mac80211, however the beacon queue is an exception and it
  1547. * is mapped on the highst tx ring IDX.
  1548. */
  1549. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  1550. dev->queues = RTL8187SE_NR_TX_QUEUES - 1;
  1551. else
  1552. dev->queues = RTL8180_NR_TX_QUEUES - 1;
  1553. if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
  1554. priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
  1555. pci_try_set_mwi(pdev);
  1556. }
  1557. if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
  1558. dev->flags |= IEEE80211_HW_SIGNAL_DBM;
  1559. else
  1560. dev->flags |= IEEE80211_HW_SIGNAL_UNSPEC;
  1561. rtl8180_eeprom_read(priv);
  1562. switch (priv->rf_type) {
  1563. case 1: rf_name = "Intersil";
  1564. break;
  1565. case 2: rf_name = "RFMD";
  1566. break;
  1567. case 3: priv->rf = &sa2400_rf_ops;
  1568. break;
  1569. case 4: priv->rf = &max2820_rf_ops;
  1570. break;
  1571. case 5: priv->rf = &grf5101_rf_ops;
  1572. break;
  1573. case 9:
  1574. if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
  1575. priv->rf = rtl8187se_detect_rf(dev);
  1576. else
  1577. priv->rf = rtl8180_detect_rf(dev);
  1578. break;
  1579. case 10:
  1580. rf_name = "RTL8255";
  1581. break;
  1582. default:
  1583. printk(KERN_ERR "%s (rtl8180): Unknown RF! (0x%x)\n",
  1584. pci_name(pdev), priv->rf_type);
  1585. err = -ENODEV;
  1586. goto err_iounmap;
  1587. }
  1588. if (!priv->rf) {
  1589. printk(KERN_ERR "%s (rtl8180): %s RF frontend not supported!\n",
  1590. pci_name(pdev), rf_name);
  1591. err = -ENODEV;
  1592. goto err_iounmap;
  1593. }
  1594. if (!is_valid_ether_addr(priv->mac_addr)) {
  1595. printk(KERN_WARNING "%s (rtl8180): Invalid hwaddr! Using"
  1596. " randomly generated MAC addr\n", pci_name(pdev));
  1597. eth_random_addr(priv->mac_addr);
  1598. }
  1599. SET_IEEE80211_PERM_ADDR(dev, priv->mac_addr);
  1600. spin_lock_init(&priv->lock);
  1601. err = ieee80211_register_hw(dev);
  1602. if (err) {
  1603. printk(KERN_ERR "%s (rtl8180): Cannot register device\n",
  1604. pci_name(pdev));
  1605. goto err_iounmap;
  1606. }
  1607. wiphy_info(dev->wiphy, "hwaddr %pm, %s + %s\n",
  1608. priv->mac_addr, chip_name, priv->rf->name);
  1609. return 0;
  1610. err_iounmap:
  1611. pci_iounmap(pdev, priv->map);
  1612. err_free_dev:
  1613. ieee80211_free_hw(dev);
  1614. err_free_reg:
  1615. pci_release_regions(pdev);
  1616. pci_disable_device(pdev);
  1617. return err;
  1618. }
  1619. static void rtl8180_remove(struct pci_dev *pdev)
  1620. {
  1621. struct ieee80211_hw *dev = pci_get_drvdata(pdev);
  1622. struct rtl8180_priv *priv;
  1623. if (!dev)
  1624. return;
  1625. ieee80211_unregister_hw(dev);
  1626. priv = dev->priv;
  1627. pci_iounmap(pdev, priv->map);
  1628. pci_release_regions(pdev);
  1629. pci_disable_device(pdev);
  1630. ieee80211_free_hw(dev);
  1631. }
  1632. #ifdef CONFIG_PM
  1633. static int rtl8180_suspend(struct pci_dev *pdev, pm_message_t state)
  1634. {
  1635. pci_save_state(pdev);
  1636. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1637. return 0;
  1638. }
  1639. static int rtl8180_resume(struct pci_dev *pdev)
  1640. {
  1641. pci_set_power_state(pdev, PCI_D0);
  1642. pci_restore_state(pdev);
  1643. return 0;
  1644. }
  1645. #endif /* CONFIG_PM */
  1646. static struct pci_driver rtl8180_driver = {
  1647. .name = KBUILD_MODNAME,
  1648. .id_table = rtl8180_table,
  1649. .probe = rtl8180_probe,
  1650. .remove = rtl8180_remove,
  1651. #ifdef CONFIG_PM
  1652. .suspend = rtl8180_suspend,
  1653. .resume = rtl8180_resume,
  1654. #endif /* CONFIG_PM */
  1655. };
  1656. module_pci_driver(rtl8180_driver);