adm8211.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017
  1. /*
  2. * Linux device driver for ADMtek ADM8211 (IEEE 802.11b MAC/BBP)
  3. *
  4. * Copyright (c) 2003, Jouni Malinen <j@w1.fi>
  5. * Copyright (c) 2004-2007, Michael Wu <flamingice@sourmilk.net>
  6. * Some parts copyright (c) 2003 by David Young <dyoung@pobox.com>
  7. * and used with permission.
  8. *
  9. * Much thanks to Infineon-ADMtek for their support of this driver.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation. See README and COPYING for
  14. * more details.
  15. */
  16. #include <linux/interrupt.h>
  17. #include <linux/if.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/slab.h>
  20. #include <linux/etherdevice.h>
  21. #include <linux/pci.h>
  22. #include <linux/delay.h>
  23. #include <linux/crc32.h>
  24. #include <linux/eeprom_93cx6.h>
  25. #include <linux/module.h>
  26. #include <net/mac80211.h>
  27. #include "adm8211.h"
  28. MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
  29. MODULE_AUTHOR("Jouni Malinen <j@w1.fi>");
  30. MODULE_DESCRIPTION("Driver for IEEE 802.11b wireless cards based on ADMtek ADM8211");
  31. MODULE_SUPPORTED_DEVICE("ADM8211");
  32. MODULE_LICENSE("GPL");
  33. static unsigned int tx_ring_size __read_mostly = 16;
  34. static unsigned int rx_ring_size __read_mostly = 16;
  35. module_param(tx_ring_size, uint, 0);
  36. module_param(rx_ring_size, uint, 0);
  37. static const struct pci_device_id adm8211_pci_id_table[] = {
  38. /* ADMtek ADM8211 */
  39. { PCI_DEVICE(0x10B7, 0x6000) }, /* 3Com 3CRSHPW796 */
  40. { PCI_DEVICE(0x1200, 0x8201) }, /* ? */
  41. { PCI_DEVICE(0x1317, 0x8201) }, /* ADM8211A */
  42. { PCI_DEVICE(0x1317, 0x8211) }, /* ADM8211B/C */
  43. { 0 }
  44. };
  45. static struct ieee80211_rate adm8211_rates[] = {
  46. { .bitrate = 10, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  47. { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  48. { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  49. { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  50. { .bitrate = 220, .flags = IEEE80211_RATE_SHORT_PREAMBLE }, /* XX ?? */
  51. };
  52. static const struct ieee80211_channel adm8211_channels[] = {
  53. { .center_freq = 2412},
  54. { .center_freq = 2417},
  55. { .center_freq = 2422},
  56. { .center_freq = 2427},
  57. { .center_freq = 2432},
  58. { .center_freq = 2437},
  59. { .center_freq = 2442},
  60. { .center_freq = 2447},
  61. { .center_freq = 2452},
  62. { .center_freq = 2457},
  63. { .center_freq = 2462},
  64. { .center_freq = 2467},
  65. { .center_freq = 2472},
  66. { .center_freq = 2484},
  67. };
  68. static void adm8211_eeprom_register_read(struct eeprom_93cx6 *eeprom)
  69. {
  70. struct adm8211_priv *priv = eeprom->data;
  71. u32 reg = ADM8211_CSR_READ(SPR);
  72. eeprom->reg_data_in = reg & ADM8211_SPR_SDI;
  73. eeprom->reg_data_out = reg & ADM8211_SPR_SDO;
  74. eeprom->reg_data_clock = reg & ADM8211_SPR_SCLK;
  75. eeprom->reg_chip_select = reg & ADM8211_SPR_SCS;
  76. }
  77. static void adm8211_eeprom_register_write(struct eeprom_93cx6 *eeprom)
  78. {
  79. struct adm8211_priv *priv = eeprom->data;
  80. u32 reg = 0x4000 | ADM8211_SPR_SRS;
  81. if (eeprom->reg_data_in)
  82. reg |= ADM8211_SPR_SDI;
  83. if (eeprom->reg_data_out)
  84. reg |= ADM8211_SPR_SDO;
  85. if (eeprom->reg_data_clock)
  86. reg |= ADM8211_SPR_SCLK;
  87. if (eeprom->reg_chip_select)
  88. reg |= ADM8211_SPR_SCS;
  89. ADM8211_CSR_WRITE(SPR, reg);
  90. ADM8211_CSR_READ(SPR); /* eeprom_delay */
  91. }
  92. static int adm8211_read_eeprom(struct ieee80211_hw *dev)
  93. {
  94. struct adm8211_priv *priv = dev->priv;
  95. unsigned int words, i;
  96. struct ieee80211_chan_range chan_range;
  97. u16 cr49;
  98. struct eeprom_93cx6 eeprom = {
  99. .data = priv,
  100. .register_read = adm8211_eeprom_register_read,
  101. .register_write = adm8211_eeprom_register_write
  102. };
  103. if (ADM8211_CSR_READ(CSR_TEST0) & ADM8211_CSR_TEST0_EPTYP) {
  104. /* 256 * 16-bit = 512 bytes */
  105. eeprom.width = PCI_EEPROM_WIDTH_93C66;
  106. words = 256;
  107. } else {
  108. /* 64 * 16-bit = 128 bytes */
  109. eeprom.width = PCI_EEPROM_WIDTH_93C46;
  110. words = 64;
  111. }
  112. priv->eeprom_len = words * 2;
  113. priv->eeprom = kmalloc(priv->eeprom_len, GFP_KERNEL);
  114. if (!priv->eeprom)
  115. return -ENOMEM;
  116. eeprom_93cx6_multiread(&eeprom, 0, (__le16 *)priv->eeprom, words);
  117. cr49 = le16_to_cpu(priv->eeprom->cr49);
  118. priv->rf_type = (cr49 >> 3) & 0x7;
  119. switch (priv->rf_type) {
  120. case ADM8211_TYPE_INTERSIL:
  121. case ADM8211_TYPE_RFMD:
  122. case ADM8211_TYPE_MARVEL:
  123. case ADM8211_TYPE_AIROHA:
  124. case ADM8211_TYPE_ADMTEK:
  125. break;
  126. default:
  127. if (priv->pdev->revision < ADM8211_REV_CA)
  128. priv->rf_type = ADM8211_TYPE_RFMD;
  129. else
  130. priv->rf_type = ADM8211_TYPE_AIROHA;
  131. printk(KERN_WARNING "%s (adm8211): Unknown RFtype %d\n",
  132. pci_name(priv->pdev), (cr49 >> 3) & 0x7);
  133. }
  134. priv->bbp_type = cr49 & 0x7;
  135. switch (priv->bbp_type) {
  136. case ADM8211_TYPE_INTERSIL:
  137. case ADM8211_TYPE_RFMD:
  138. case ADM8211_TYPE_MARVEL:
  139. case ADM8211_TYPE_AIROHA:
  140. case ADM8211_TYPE_ADMTEK:
  141. break;
  142. default:
  143. if (priv->pdev->revision < ADM8211_REV_CA)
  144. priv->bbp_type = ADM8211_TYPE_RFMD;
  145. else
  146. priv->bbp_type = ADM8211_TYPE_ADMTEK;
  147. printk(KERN_WARNING "%s (adm8211): Unknown BBPtype: %d\n",
  148. pci_name(priv->pdev), cr49 >> 3);
  149. }
  150. if (priv->eeprom->country_code >= ARRAY_SIZE(cranges)) {
  151. printk(KERN_WARNING "%s (adm8211): Invalid country code (%d)\n",
  152. pci_name(priv->pdev), priv->eeprom->country_code);
  153. chan_range = cranges[2];
  154. } else
  155. chan_range = cranges[priv->eeprom->country_code];
  156. printk(KERN_DEBUG "%s (adm8211): Channel range: %d - %d\n",
  157. pci_name(priv->pdev), (int)chan_range.min, (int)chan_range.max);
  158. BUILD_BUG_ON(sizeof(priv->channels) != sizeof(adm8211_channels));
  159. memcpy(priv->channels, adm8211_channels, sizeof(priv->channels));
  160. priv->band.channels = priv->channels;
  161. priv->band.n_channels = ARRAY_SIZE(adm8211_channels);
  162. priv->band.bitrates = adm8211_rates;
  163. priv->band.n_bitrates = ARRAY_SIZE(adm8211_rates);
  164. for (i = 1; i <= ARRAY_SIZE(adm8211_channels); i++)
  165. if (i < chan_range.min || i > chan_range.max)
  166. priv->channels[i - 1].flags |= IEEE80211_CHAN_DISABLED;
  167. switch (priv->eeprom->specific_bbptype) {
  168. case ADM8211_BBP_RFMD3000:
  169. case ADM8211_BBP_RFMD3002:
  170. case ADM8211_BBP_ADM8011:
  171. priv->specific_bbptype = priv->eeprom->specific_bbptype;
  172. break;
  173. default:
  174. if (priv->pdev->revision < ADM8211_REV_CA)
  175. priv->specific_bbptype = ADM8211_BBP_RFMD3000;
  176. else
  177. priv->specific_bbptype = ADM8211_BBP_ADM8011;
  178. printk(KERN_WARNING "%s (adm8211): Unknown specific BBP: %d\n",
  179. pci_name(priv->pdev), priv->eeprom->specific_bbptype);
  180. }
  181. switch (priv->eeprom->specific_rftype) {
  182. case ADM8211_RFMD2948:
  183. case ADM8211_RFMD2958:
  184. case ADM8211_RFMD2958_RF3000_CONTROL_POWER:
  185. case ADM8211_MAX2820:
  186. case ADM8211_AL2210L:
  187. priv->transceiver_type = priv->eeprom->specific_rftype;
  188. break;
  189. default:
  190. if (priv->pdev->revision == ADM8211_REV_BA)
  191. priv->transceiver_type = ADM8211_RFMD2958_RF3000_CONTROL_POWER;
  192. else if (priv->pdev->revision == ADM8211_REV_CA)
  193. priv->transceiver_type = ADM8211_AL2210L;
  194. else if (priv->pdev->revision == ADM8211_REV_AB)
  195. priv->transceiver_type = ADM8211_RFMD2948;
  196. printk(KERN_WARNING "%s (adm8211): Unknown transceiver: %d\n",
  197. pci_name(priv->pdev), priv->eeprom->specific_rftype);
  198. break;
  199. }
  200. printk(KERN_DEBUG "%s (adm8211): RFtype=%d BBPtype=%d Specific BBP=%d "
  201. "Transceiver=%d\n", pci_name(priv->pdev), priv->rf_type,
  202. priv->bbp_type, priv->specific_bbptype, priv->transceiver_type);
  203. return 0;
  204. }
  205. static inline void adm8211_write_sram(struct ieee80211_hw *dev,
  206. u32 addr, u32 data)
  207. {
  208. struct adm8211_priv *priv = dev->priv;
  209. ADM8211_CSR_WRITE(WEPCTL, addr | ADM8211_WEPCTL_TABLE_WR |
  210. (priv->pdev->revision < ADM8211_REV_BA ?
  211. 0 : ADM8211_WEPCTL_SEL_WEPTABLE ));
  212. ADM8211_CSR_READ(WEPCTL);
  213. msleep(1);
  214. ADM8211_CSR_WRITE(WESK, data);
  215. ADM8211_CSR_READ(WESK);
  216. msleep(1);
  217. }
  218. static void adm8211_write_sram_bytes(struct ieee80211_hw *dev,
  219. unsigned int addr, u8 *buf,
  220. unsigned int len)
  221. {
  222. struct adm8211_priv *priv = dev->priv;
  223. u32 reg = ADM8211_CSR_READ(WEPCTL);
  224. unsigned int i;
  225. if (priv->pdev->revision < ADM8211_REV_BA) {
  226. for (i = 0; i < len; i += 2) {
  227. u16 val = buf[i] | (buf[i + 1] << 8);
  228. adm8211_write_sram(dev, addr + i / 2, val);
  229. }
  230. } else {
  231. for (i = 0; i < len; i += 4) {
  232. u32 val = (buf[i + 0] << 0 ) | (buf[i + 1] << 8 ) |
  233. (buf[i + 2] << 16) | (buf[i + 3] << 24);
  234. adm8211_write_sram(dev, addr + i / 4, val);
  235. }
  236. }
  237. ADM8211_CSR_WRITE(WEPCTL, reg);
  238. }
  239. static void adm8211_clear_sram(struct ieee80211_hw *dev)
  240. {
  241. struct adm8211_priv *priv = dev->priv;
  242. u32 reg = ADM8211_CSR_READ(WEPCTL);
  243. unsigned int addr;
  244. for (addr = 0; addr < ADM8211_SRAM_SIZE; addr++)
  245. adm8211_write_sram(dev, addr, 0);
  246. ADM8211_CSR_WRITE(WEPCTL, reg);
  247. }
  248. static int adm8211_get_stats(struct ieee80211_hw *dev,
  249. struct ieee80211_low_level_stats *stats)
  250. {
  251. struct adm8211_priv *priv = dev->priv;
  252. memcpy(stats, &priv->stats, sizeof(*stats));
  253. return 0;
  254. }
  255. static void adm8211_interrupt_tci(struct ieee80211_hw *dev)
  256. {
  257. struct adm8211_priv *priv = dev->priv;
  258. unsigned int dirty_tx;
  259. spin_lock(&priv->lock);
  260. for (dirty_tx = priv->dirty_tx; priv->cur_tx - dirty_tx; dirty_tx++) {
  261. unsigned int entry = dirty_tx % priv->tx_ring_size;
  262. u32 status = le32_to_cpu(priv->tx_ring[entry].status);
  263. struct ieee80211_tx_info *txi;
  264. struct adm8211_tx_ring_info *info;
  265. struct sk_buff *skb;
  266. if (status & TDES0_CONTROL_OWN ||
  267. !(status & TDES0_CONTROL_DONE))
  268. break;
  269. info = &priv->tx_buffers[entry];
  270. skb = info->skb;
  271. txi = IEEE80211_SKB_CB(skb);
  272. /* TODO: check TDES0_STATUS_TUF and TDES0_STATUS_TRO */
  273. pci_unmap_single(priv->pdev, info->mapping,
  274. info->skb->len, PCI_DMA_TODEVICE);
  275. ieee80211_tx_info_clear_status(txi);
  276. skb_pull(skb, sizeof(struct adm8211_tx_hdr));
  277. memcpy(skb_push(skb, info->hdrlen), skb->cb, info->hdrlen);
  278. if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) &&
  279. !(status & TDES0_STATUS_ES))
  280. txi->flags |= IEEE80211_TX_STAT_ACK;
  281. ieee80211_tx_status_irqsafe(dev, skb);
  282. info->skb = NULL;
  283. }
  284. if (priv->cur_tx - dirty_tx < priv->tx_ring_size - 2)
  285. ieee80211_wake_queue(dev, 0);
  286. priv->dirty_tx = dirty_tx;
  287. spin_unlock(&priv->lock);
  288. }
  289. static void adm8211_interrupt_rci(struct ieee80211_hw *dev)
  290. {
  291. struct adm8211_priv *priv = dev->priv;
  292. unsigned int entry = priv->cur_rx % priv->rx_ring_size;
  293. u32 status;
  294. unsigned int pktlen;
  295. struct sk_buff *skb, *newskb;
  296. unsigned int limit = priv->rx_ring_size;
  297. u8 rssi, rate;
  298. while (!(priv->rx_ring[entry].status & cpu_to_le32(RDES0_STATUS_OWN))) {
  299. if (!limit--)
  300. break;
  301. status = le32_to_cpu(priv->rx_ring[entry].status);
  302. rate = (status & RDES0_STATUS_RXDR) >> 12;
  303. rssi = le32_to_cpu(priv->rx_ring[entry].length) &
  304. RDES1_STATUS_RSSI;
  305. pktlen = status & RDES0_STATUS_FL;
  306. if (pktlen > RX_PKT_SIZE) {
  307. if (net_ratelimit())
  308. wiphy_debug(dev->wiphy, "frame too long (%d)\n",
  309. pktlen);
  310. pktlen = RX_PKT_SIZE;
  311. }
  312. if (!priv->soft_rx_crc && status & RDES0_STATUS_ES) {
  313. skb = NULL; /* old buffer will be reused */
  314. /* TODO: update RX error stats */
  315. /* TODO: check RDES0_STATUS_CRC*E */
  316. } else if (pktlen < RX_COPY_BREAK) {
  317. skb = dev_alloc_skb(pktlen);
  318. if (skb) {
  319. pci_dma_sync_single_for_cpu(
  320. priv->pdev,
  321. priv->rx_buffers[entry].mapping,
  322. pktlen, PCI_DMA_FROMDEVICE);
  323. skb_put_data(skb,
  324. skb_tail_pointer(priv->rx_buffers[entry].skb),
  325. pktlen);
  326. pci_dma_sync_single_for_device(
  327. priv->pdev,
  328. priv->rx_buffers[entry].mapping,
  329. RX_PKT_SIZE, PCI_DMA_FROMDEVICE);
  330. }
  331. } else {
  332. newskb = dev_alloc_skb(RX_PKT_SIZE);
  333. if (newskb) {
  334. skb = priv->rx_buffers[entry].skb;
  335. skb_put(skb, pktlen);
  336. pci_unmap_single(
  337. priv->pdev,
  338. priv->rx_buffers[entry].mapping,
  339. RX_PKT_SIZE, PCI_DMA_FROMDEVICE);
  340. priv->rx_buffers[entry].skb = newskb;
  341. priv->rx_buffers[entry].mapping =
  342. pci_map_single(priv->pdev,
  343. skb_tail_pointer(newskb),
  344. RX_PKT_SIZE,
  345. PCI_DMA_FROMDEVICE);
  346. if (pci_dma_mapping_error(priv->pdev,
  347. priv->rx_buffers[entry].mapping)) {
  348. priv->rx_buffers[entry].skb = NULL;
  349. dev_kfree_skb(newskb);
  350. skb = NULL;
  351. /* TODO: update rx dropped stats */
  352. }
  353. } else {
  354. skb = NULL;
  355. /* TODO: update rx dropped stats */
  356. }
  357. priv->rx_ring[entry].buffer1 =
  358. cpu_to_le32(priv->rx_buffers[entry].mapping);
  359. }
  360. priv->rx_ring[entry].status = cpu_to_le32(RDES0_STATUS_OWN |
  361. RDES0_STATUS_SQL);
  362. priv->rx_ring[entry].length =
  363. cpu_to_le32(RX_PKT_SIZE |
  364. (entry == priv->rx_ring_size - 1 ?
  365. RDES1_CONTROL_RER : 0));
  366. if (skb) {
  367. struct ieee80211_rx_status rx_status = {0};
  368. if (priv->pdev->revision < ADM8211_REV_CA)
  369. rx_status.signal = rssi;
  370. else
  371. rx_status.signal = 100 - rssi;
  372. rx_status.rate_idx = rate;
  373. rx_status.freq = adm8211_channels[priv->channel - 1].center_freq;
  374. rx_status.band = NL80211_BAND_2GHZ;
  375. memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
  376. ieee80211_rx_irqsafe(dev, skb);
  377. }
  378. entry = (++priv->cur_rx) % priv->rx_ring_size;
  379. }
  380. /* TODO: check LPC and update stats? */
  381. }
  382. static irqreturn_t adm8211_interrupt(int irq, void *dev_id)
  383. {
  384. #define ADM8211_INT(x) \
  385. do { \
  386. if (unlikely(stsr & ADM8211_STSR_ ## x)) \
  387. wiphy_debug(dev->wiphy, "%s\n", #x); \
  388. } while (0)
  389. struct ieee80211_hw *dev = dev_id;
  390. struct adm8211_priv *priv = dev->priv;
  391. u32 stsr = ADM8211_CSR_READ(STSR);
  392. ADM8211_CSR_WRITE(STSR, stsr);
  393. if (stsr == 0xffffffff)
  394. return IRQ_HANDLED;
  395. if (!(stsr & (ADM8211_STSR_NISS | ADM8211_STSR_AISS)))
  396. return IRQ_HANDLED;
  397. if (stsr & ADM8211_STSR_RCI)
  398. adm8211_interrupt_rci(dev);
  399. if (stsr & ADM8211_STSR_TCI)
  400. adm8211_interrupt_tci(dev);
  401. ADM8211_INT(PCF);
  402. ADM8211_INT(BCNTC);
  403. ADM8211_INT(GPINT);
  404. ADM8211_INT(ATIMTC);
  405. ADM8211_INT(TSFTF);
  406. ADM8211_INT(TSCZ);
  407. ADM8211_INT(SQL);
  408. ADM8211_INT(WEPTD);
  409. ADM8211_INT(ATIME);
  410. ADM8211_INT(TEIS);
  411. ADM8211_INT(FBE);
  412. ADM8211_INT(REIS);
  413. ADM8211_INT(GPTT);
  414. ADM8211_INT(RPS);
  415. ADM8211_INT(RDU);
  416. ADM8211_INT(TUF);
  417. ADM8211_INT(TPS);
  418. return IRQ_HANDLED;
  419. #undef ADM8211_INT
  420. }
  421. #define WRITE_SYN(name,v_mask,v_shift,a_mask,a_shift,bits,prewrite,postwrite)\
  422. static void adm8211_rf_write_syn_ ## name (struct ieee80211_hw *dev, \
  423. u16 addr, u32 value) { \
  424. struct adm8211_priv *priv = dev->priv; \
  425. unsigned int i; \
  426. u32 reg, bitbuf; \
  427. \
  428. value &= v_mask; \
  429. addr &= a_mask; \
  430. bitbuf = (value << v_shift) | (addr << a_shift); \
  431. \
  432. ADM8211_CSR_WRITE(SYNRF, ADM8211_SYNRF_IF_SELECT_1); \
  433. ADM8211_CSR_READ(SYNRF); \
  434. ADM8211_CSR_WRITE(SYNRF, ADM8211_SYNRF_IF_SELECT_0); \
  435. ADM8211_CSR_READ(SYNRF); \
  436. \
  437. if (prewrite) { \
  438. ADM8211_CSR_WRITE(SYNRF, ADM8211_SYNRF_WRITE_SYNDATA_0); \
  439. ADM8211_CSR_READ(SYNRF); \
  440. } \
  441. \
  442. for (i = 0; i <= bits; i++) { \
  443. if (bitbuf & (1 << (bits - i))) \
  444. reg = ADM8211_SYNRF_WRITE_SYNDATA_1; \
  445. else \
  446. reg = ADM8211_SYNRF_WRITE_SYNDATA_0; \
  447. \
  448. ADM8211_CSR_WRITE(SYNRF, reg); \
  449. ADM8211_CSR_READ(SYNRF); \
  450. \
  451. ADM8211_CSR_WRITE(SYNRF, reg | ADM8211_SYNRF_WRITE_CLOCK_1); \
  452. ADM8211_CSR_READ(SYNRF); \
  453. ADM8211_CSR_WRITE(SYNRF, reg | ADM8211_SYNRF_WRITE_CLOCK_0); \
  454. ADM8211_CSR_READ(SYNRF); \
  455. } \
  456. \
  457. if (postwrite == 1) { \
  458. ADM8211_CSR_WRITE(SYNRF, reg | ADM8211_SYNRF_IF_SELECT_0); \
  459. ADM8211_CSR_READ(SYNRF); \
  460. } \
  461. if (postwrite == 2) { \
  462. ADM8211_CSR_WRITE(SYNRF, reg | ADM8211_SYNRF_IF_SELECT_1); \
  463. ADM8211_CSR_READ(SYNRF); \
  464. } \
  465. \
  466. ADM8211_CSR_WRITE(SYNRF, 0); \
  467. ADM8211_CSR_READ(SYNRF); \
  468. }
  469. WRITE_SYN(max2820, 0x00FFF, 0, 0x0F, 12, 15, 1, 1)
  470. WRITE_SYN(al2210l, 0xFFFFF, 4, 0x0F, 0, 23, 1, 1)
  471. WRITE_SYN(rfmd2958, 0x3FFFF, 0, 0x1F, 18, 23, 0, 1)
  472. WRITE_SYN(rfmd2948, 0x0FFFF, 4, 0x0F, 0, 21, 0, 2)
  473. #undef WRITE_SYN
  474. static int adm8211_write_bbp(struct ieee80211_hw *dev, u8 addr, u8 data)
  475. {
  476. struct adm8211_priv *priv = dev->priv;
  477. unsigned int timeout;
  478. u32 reg;
  479. timeout = 10;
  480. while (timeout > 0) {
  481. reg = ADM8211_CSR_READ(BBPCTL);
  482. if (!(reg & (ADM8211_BBPCTL_WR | ADM8211_BBPCTL_RD)))
  483. break;
  484. timeout--;
  485. msleep(2);
  486. }
  487. if (timeout == 0) {
  488. wiphy_debug(dev->wiphy,
  489. "adm8211_write_bbp(%d,%d) failed prewrite (reg=0x%08x)\n",
  490. addr, data, reg);
  491. return -ETIMEDOUT;
  492. }
  493. switch (priv->bbp_type) {
  494. case ADM8211_TYPE_INTERSIL:
  495. reg = ADM8211_BBPCTL_MMISEL; /* three wire interface */
  496. break;
  497. case ADM8211_TYPE_RFMD:
  498. reg = (0x20 << 24) | ADM8211_BBPCTL_TXCE | ADM8211_BBPCTL_CCAP |
  499. (0x01 << 18);
  500. break;
  501. case ADM8211_TYPE_ADMTEK:
  502. reg = (0x20 << 24) | ADM8211_BBPCTL_TXCE | ADM8211_BBPCTL_CCAP |
  503. (0x05 << 18);
  504. break;
  505. }
  506. reg |= ADM8211_BBPCTL_WR | (addr << 8) | data;
  507. ADM8211_CSR_WRITE(BBPCTL, reg);
  508. timeout = 10;
  509. while (timeout > 0) {
  510. reg = ADM8211_CSR_READ(BBPCTL);
  511. if (!(reg & ADM8211_BBPCTL_WR))
  512. break;
  513. timeout--;
  514. msleep(2);
  515. }
  516. if (timeout == 0) {
  517. ADM8211_CSR_WRITE(BBPCTL, ADM8211_CSR_READ(BBPCTL) &
  518. ~ADM8211_BBPCTL_WR);
  519. wiphy_debug(dev->wiphy,
  520. "adm8211_write_bbp(%d,%d) failed postwrite (reg=0x%08x)\n",
  521. addr, data, reg);
  522. return -ETIMEDOUT;
  523. }
  524. return 0;
  525. }
  526. static int adm8211_rf_set_channel(struct ieee80211_hw *dev, unsigned int chan)
  527. {
  528. static const u32 adm8211_rfmd2958_reg5[] =
  529. {0x22BD, 0x22D2, 0x22E8, 0x22FE, 0x2314, 0x232A, 0x2340,
  530. 0x2355, 0x236B, 0x2381, 0x2397, 0x23AD, 0x23C2, 0x23F7};
  531. static const u32 adm8211_rfmd2958_reg6[] =
  532. {0x05D17, 0x3A2E8, 0x2E8BA, 0x22E8B, 0x1745D, 0x0BA2E, 0x00000,
  533. 0x345D1, 0x28BA2, 0x1D174, 0x11745, 0x05D17, 0x3A2E8, 0x11745};
  534. struct adm8211_priv *priv = dev->priv;
  535. u8 ant_power = priv->ant_power > 0x3F ?
  536. priv->eeprom->antenna_power[chan - 1] : priv->ant_power;
  537. u8 tx_power = priv->tx_power > 0x3F ?
  538. priv->eeprom->tx_power[chan - 1] : priv->tx_power;
  539. u8 lpf_cutoff = priv->lpf_cutoff == 0xFF ?
  540. priv->eeprom->lpf_cutoff[chan - 1] : priv->lpf_cutoff;
  541. u8 lnags_thresh = priv->lnags_threshold == 0xFF ?
  542. priv->eeprom->lnags_threshold[chan - 1] : priv->lnags_threshold;
  543. u32 reg;
  544. ADM8211_IDLE();
  545. /* Program synthesizer to new channel */
  546. switch (priv->transceiver_type) {
  547. case ADM8211_RFMD2958:
  548. case ADM8211_RFMD2958_RF3000_CONTROL_POWER:
  549. adm8211_rf_write_syn_rfmd2958(dev, 0x00, 0x04007);
  550. adm8211_rf_write_syn_rfmd2958(dev, 0x02, 0x00033);
  551. adm8211_rf_write_syn_rfmd2958(dev, 0x05,
  552. adm8211_rfmd2958_reg5[chan - 1]);
  553. adm8211_rf_write_syn_rfmd2958(dev, 0x06,
  554. adm8211_rfmd2958_reg6[chan - 1]);
  555. break;
  556. case ADM8211_RFMD2948:
  557. adm8211_rf_write_syn_rfmd2948(dev, SI4126_MAIN_CONF,
  558. SI4126_MAIN_XINDIV2);
  559. adm8211_rf_write_syn_rfmd2948(dev, SI4126_POWERDOWN,
  560. SI4126_POWERDOWN_PDIB |
  561. SI4126_POWERDOWN_PDRB);
  562. adm8211_rf_write_syn_rfmd2948(dev, SI4126_PHASE_DET_GAIN, 0);
  563. adm8211_rf_write_syn_rfmd2948(dev, SI4126_RF2_N_DIV,
  564. (chan == 14 ?
  565. 2110 : (2033 + (chan * 5))));
  566. adm8211_rf_write_syn_rfmd2948(dev, SI4126_IF_N_DIV, 1496);
  567. adm8211_rf_write_syn_rfmd2948(dev, SI4126_RF2_R_DIV, 44);
  568. adm8211_rf_write_syn_rfmd2948(dev, SI4126_IF_R_DIV, 44);
  569. break;
  570. case ADM8211_MAX2820:
  571. adm8211_rf_write_syn_max2820(dev, 0x3,
  572. (chan == 14 ? 0x054 : (0x7 + (chan * 5))));
  573. break;
  574. case ADM8211_AL2210L:
  575. adm8211_rf_write_syn_al2210l(dev, 0x0,
  576. (chan == 14 ? 0x229B4 : (0x22967 + (chan * 5))));
  577. break;
  578. default:
  579. wiphy_debug(dev->wiphy, "unsupported transceiver type %d\n",
  580. priv->transceiver_type);
  581. break;
  582. }
  583. /* write BBP regs */
  584. if (priv->bbp_type == ADM8211_TYPE_RFMD) {
  585. /* SMC 2635W specific? adm8211b doesn't use the 2948 though.. */
  586. /* TODO: remove if SMC 2635W doesn't need this */
  587. if (priv->transceiver_type == ADM8211_RFMD2948) {
  588. reg = ADM8211_CSR_READ(GPIO);
  589. reg &= 0xfffc0000;
  590. reg |= ADM8211_CSR_GPIO_EN0;
  591. if (chan != 14)
  592. reg |= ADM8211_CSR_GPIO_O0;
  593. ADM8211_CSR_WRITE(GPIO, reg);
  594. }
  595. if (priv->transceiver_type == ADM8211_RFMD2958) {
  596. /* set PCNT2 */
  597. adm8211_rf_write_syn_rfmd2958(dev, 0x0B, 0x07100);
  598. /* set PCNT1 P_DESIRED/MID_BIAS */
  599. reg = le16_to_cpu(priv->eeprom->cr49);
  600. reg >>= 13;
  601. reg <<= 15;
  602. reg |= ant_power << 9;
  603. adm8211_rf_write_syn_rfmd2958(dev, 0x0A, reg);
  604. /* set TXRX TX_GAIN */
  605. adm8211_rf_write_syn_rfmd2958(dev, 0x09, 0x00050 |
  606. (priv->pdev->revision < ADM8211_REV_CA ? tx_power : 0));
  607. } else {
  608. reg = ADM8211_CSR_READ(PLCPHD);
  609. reg &= 0xff00ffff;
  610. reg |= tx_power << 18;
  611. ADM8211_CSR_WRITE(PLCPHD, reg);
  612. }
  613. ADM8211_CSR_WRITE(SYNRF, ADM8211_SYNRF_SELRF |
  614. ADM8211_SYNRF_PE1 | ADM8211_SYNRF_PHYRST);
  615. ADM8211_CSR_READ(SYNRF);
  616. msleep(30);
  617. /* RF3000 BBP */
  618. if (priv->transceiver_type != ADM8211_RFMD2958)
  619. adm8211_write_bbp(dev, RF3000_TX_VAR_GAIN__TX_LEN_EXT,
  620. tx_power<<2);
  621. adm8211_write_bbp(dev, RF3000_LOW_GAIN_CALIB, lpf_cutoff);
  622. adm8211_write_bbp(dev, RF3000_HIGH_GAIN_CALIB, lnags_thresh);
  623. adm8211_write_bbp(dev, 0x1c, priv->pdev->revision == ADM8211_REV_BA ?
  624. priv->eeprom->cr28 : 0);
  625. adm8211_write_bbp(dev, 0x1d, priv->eeprom->cr29);
  626. ADM8211_CSR_WRITE(SYNRF, 0);
  627. /* Nothing to do for ADMtek BBP */
  628. } else if (priv->bbp_type != ADM8211_TYPE_ADMTEK)
  629. wiphy_debug(dev->wiphy, "unsupported BBP type %d\n",
  630. priv->bbp_type);
  631. ADM8211_RESTORE();
  632. /* update current channel for adhoc (and maybe AP mode) */
  633. reg = ADM8211_CSR_READ(CAP0);
  634. reg &= ~0xF;
  635. reg |= chan;
  636. ADM8211_CSR_WRITE(CAP0, reg);
  637. return 0;
  638. }
  639. static void adm8211_update_mode(struct ieee80211_hw *dev)
  640. {
  641. struct adm8211_priv *priv = dev->priv;
  642. ADM8211_IDLE();
  643. priv->soft_rx_crc = 0;
  644. switch (priv->mode) {
  645. case NL80211_IFTYPE_STATION:
  646. priv->nar &= ~(ADM8211_NAR_PR | ADM8211_NAR_EA);
  647. priv->nar |= ADM8211_NAR_ST | ADM8211_NAR_SR;
  648. break;
  649. case NL80211_IFTYPE_ADHOC:
  650. priv->nar &= ~ADM8211_NAR_PR;
  651. priv->nar |= ADM8211_NAR_EA | ADM8211_NAR_ST | ADM8211_NAR_SR;
  652. /* don't trust the error bits on rev 0x20 and up in adhoc */
  653. if (priv->pdev->revision >= ADM8211_REV_BA)
  654. priv->soft_rx_crc = 1;
  655. break;
  656. case NL80211_IFTYPE_MONITOR:
  657. priv->nar &= ~(ADM8211_NAR_EA | ADM8211_NAR_ST);
  658. priv->nar |= ADM8211_NAR_PR | ADM8211_NAR_SR;
  659. break;
  660. }
  661. ADM8211_RESTORE();
  662. }
  663. static void adm8211_hw_init_syn(struct ieee80211_hw *dev)
  664. {
  665. struct adm8211_priv *priv = dev->priv;
  666. switch (priv->transceiver_type) {
  667. case ADM8211_RFMD2958:
  668. case ADM8211_RFMD2958_RF3000_CONTROL_POWER:
  669. /* comments taken from ADMtek vendor driver */
  670. /* Reset RF2958 after power on */
  671. adm8211_rf_write_syn_rfmd2958(dev, 0x1F, 0x00000);
  672. /* Initialize RF VCO Core Bias to maximum */
  673. adm8211_rf_write_syn_rfmd2958(dev, 0x0C, 0x3001F);
  674. /* Initialize IF PLL */
  675. adm8211_rf_write_syn_rfmd2958(dev, 0x01, 0x29C03);
  676. /* Initialize IF PLL Coarse Tuning */
  677. adm8211_rf_write_syn_rfmd2958(dev, 0x03, 0x1FF6F);
  678. /* Initialize RF PLL */
  679. adm8211_rf_write_syn_rfmd2958(dev, 0x04, 0x29403);
  680. /* Initialize RF PLL Coarse Tuning */
  681. adm8211_rf_write_syn_rfmd2958(dev, 0x07, 0x1456F);
  682. /* Initialize TX gain and filter BW (R9) */
  683. adm8211_rf_write_syn_rfmd2958(dev, 0x09,
  684. (priv->transceiver_type == ADM8211_RFMD2958 ?
  685. 0x10050 : 0x00050));
  686. /* Initialize CAL register */
  687. adm8211_rf_write_syn_rfmd2958(dev, 0x08, 0x3FFF8);
  688. break;
  689. case ADM8211_MAX2820:
  690. adm8211_rf_write_syn_max2820(dev, 0x1, 0x01E);
  691. adm8211_rf_write_syn_max2820(dev, 0x2, 0x001);
  692. adm8211_rf_write_syn_max2820(dev, 0x3, 0x054);
  693. adm8211_rf_write_syn_max2820(dev, 0x4, 0x310);
  694. adm8211_rf_write_syn_max2820(dev, 0x5, 0x000);
  695. break;
  696. case ADM8211_AL2210L:
  697. adm8211_rf_write_syn_al2210l(dev, 0x0, 0x0196C);
  698. adm8211_rf_write_syn_al2210l(dev, 0x1, 0x007CB);
  699. adm8211_rf_write_syn_al2210l(dev, 0x2, 0x3582F);
  700. adm8211_rf_write_syn_al2210l(dev, 0x3, 0x010A9);
  701. adm8211_rf_write_syn_al2210l(dev, 0x4, 0x77280);
  702. adm8211_rf_write_syn_al2210l(dev, 0x5, 0x45641);
  703. adm8211_rf_write_syn_al2210l(dev, 0x6, 0xEA130);
  704. adm8211_rf_write_syn_al2210l(dev, 0x7, 0x80000);
  705. adm8211_rf_write_syn_al2210l(dev, 0x8, 0x7850F);
  706. adm8211_rf_write_syn_al2210l(dev, 0x9, 0xF900C);
  707. adm8211_rf_write_syn_al2210l(dev, 0xA, 0x00000);
  708. adm8211_rf_write_syn_al2210l(dev, 0xB, 0x00000);
  709. break;
  710. case ADM8211_RFMD2948:
  711. default:
  712. break;
  713. }
  714. }
  715. static int adm8211_hw_init_bbp(struct ieee80211_hw *dev)
  716. {
  717. struct adm8211_priv *priv = dev->priv;
  718. u32 reg;
  719. /* write addresses */
  720. if (priv->bbp_type == ADM8211_TYPE_INTERSIL) {
  721. ADM8211_CSR_WRITE(MMIWA, 0x100E0C0A);
  722. ADM8211_CSR_WRITE(MMIRD0, 0x00007C7E);
  723. ADM8211_CSR_WRITE(MMIRD1, 0x00100000);
  724. } else if (priv->bbp_type == ADM8211_TYPE_RFMD ||
  725. priv->bbp_type == ADM8211_TYPE_ADMTEK) {
  726. /* check specific BBP type */
  727. switch (priv->specific_bbptype) {
  728. case ADM8211_BBP_RFMD3000:
  729. case ADM8211_BBP_RFMD3002:
  730. ADM8211_CSR_WRITE(MMIWA, 0x00009101);
  731. ADM8211_CSR_WRITE(MMIRD0, 0x00000301);
  732. break;
  733. case ADM8211_BBP_ADM8011:
  734. ADM8211_CSR_WRITE(MMIWA, 0x00008903);
  735. ADM8211_CSR_WRITE(MMIRD0, 0x00001716);
  736. reg = ADM8211_CSR_READ(BBPCTL);
  737. reg &= ~ADM8211_BBPCTL_TYPE;
  738. reg |= 0x5 << 18;
  739. ADM8211_CSR_WRITE(BBPCTL, reg);
  740. break;
  741. }
  742. switch (priv->pdev->revision) {
  743. case ADM8211_REV_CA:
  744. if (priv->transceiver_type == ADM8211_RFMD2958 ||
  745. priv->transceiver_type == ADM8211_RFMD2958_RF3000_CONTROL_POWER ||
  746. priv->transceiver_type == ADM8211_RFMD2948)
  747. ADM8211_CSR_WRITE(SYNCTL, 0x1 << 22);
  748. else if (priv->transceiver_type == ADM8211_MAX2820 ||
  749. priv->transceiver_type == ADM8211_AL2210L)
  750. ADM8211_CSR_WRITE(SYNCTL, 0x3 << 22);
  751. break;
  752. case ADM8211_REV_BA:
  753. reg = ADM8211_CSR_READ(MMIRD1);
  754. reg &= 0x0000FFFF;
  755. reg |= 0x7e100000;
  756. ADM8211_CSR_WRITE(MMIRD1, reg);
  757. break;
  758. case ADM8211_REV_AB:
  759. case ADM8211_REV_AF:
  760. default:
  761. ADM8211_CSR_WRITE(MMIRD1, 0x7e100000);
  762. break;
  763. }
  764. /* For RFMD */
  765. ADM8211_CSR_WRITE(MACTEST, 0x800);
  766. }
  767. adm8211_hw_init_syn(dev);
  768. /* Set RF Power control IF pin to PE1+PHYRST# */
  769. ADM8211_CSR_WRITE(SYNRF, ADM8211_SYNRF_SELRF |
  770. ADM8211_SYNRF_PE1 | ADM8211_SYNRF_PHYRST);
  771. ADM8211_CSR_READ(SYNRF);
  772. msleep(20);
  773. /* write BBP regs */
  774. if (priv->bbp_type == ADM8211_TYPE_RFMD) {
  775. /* RF3000 BBP */
  776. /* another set:
  777. * 11: c8
  778. * 14: 14
  779. * 15: 50 (chan 1..13; chan 14: d0)
  780. * 1c: 00
  781. * 1d: 84
  782. */
  783. adm8211_write_bbp(dev, RF3000_CCA_CTRL, 0x80);
  784. /* antenna selection: diversity */
  785. adm8211_write_bbp(dev, RF3000_DIVERSITY__RSSI, 0x80);
  786. adm8211_write_bbp(dev, RF3000_TX_VAR_GAIN__TX_LEN_EXT, 0x74);
  787. adm8211_write_bbp(dev, RF3000_LOW_GAIN_CALIB, 0x38);
  788. adm8211_write_bbp(dev, RF3000_HIGH_GAIN_CALIB, 0x40);
  789. if (priv->eeprom->major_version < 2) {
  790. adm8211_write_bbp(dev, 0x1c, 0x00);
  791. adm8211_write_bbp(dev, 0x1d, 0x80);
  792. } else {
  793. if (priv->pdev->revision == ADM8211_REV_BA)
  794. adm8211_write_bbp(dev, 0x1c, priv->eeprom->cr28);
  795. else
  796. adm8211_write_bbp(dev, 0x1c, 0x00);
  797. adm8211_write_bbp(dev, 0x1d, priv->eeprom->cr29);
  798. }
  799. } else if (priv->bbp_type == ADM8211_TYPE_ADMTEK) {
  800. /* reset baseband */
  801. adm8211_write_bbp(dev, 0x00, 0xFF);
  802. /* antenna selection: diversity */
  803. adm8211_write_bbp(dev, 0x07, 0x0A);
  804. /* TODO: find documentation for this */
  805. switch (priv->transceiver_type) {
  806. case ADM8211_RFMD2958:
  807. case ADM8211_RFMD2958_RF3000_CONTROL_POWER:
  808. adm8211_write_bbp(dev, 0x00, 0x00);
  809. adm8211_write_bbp(dev, 0x01, 0x00);
  810. adm8211_write_bbp(dev, 0x02, 0x00);
  811. adm8211_write_bbp(dev, 0x03, 0x00);
  812. adm8211_write_bbp(dev, 0x06, 0x0f);
  813. adm8211_write_bbp(dev, 0x09, 0x00);
  814. adm8211_write_bbp(dev, 0x0a, 0x00);
  815. adm8211_write_bbp(dev, 0x0b, 0x00);
  816. adm8211_write_bbp(dev, 0x0c, 0x00);
  817. adm8211_write_bbp(dev, 0x0f, 0xAA);
  818. adm8211_write_bbp(dev, 0x10, 0x8c);
  819. adm8211_write_bbp(dev, 0x11, 0x43);
  820. adm8211_write_bbp(dev, 0x18, 0x40);
  821. adm8211_write_bbp(dev, 0x20, 0x23);
  822. adm8211_write_bbp(dev, 0x21, 0x02);
  823. adm8211_write_bbp(dev, 0x22, 0x28);
  824. adm8211_write_bbp(dev, 0x23, 0x30);
  825. adm8211_write_bbp(dev, 0x24, 0x2d);
  826. adm8211_write_bbp(dev, 0x28, 0x35);
  827. adm8211_write_bbp(dev, 0x2a, 0x8c);
  828. adm8211_write_bbp(dev, 0x2b, 0x81);
  829. adm8211_write_bbp(dev, 0x2c, 0x44);
  830. adm8211_write_bbp(dev, 0x2d, 0x0A);
  831. adm8211_write_bbp(dev, 0x29, 0x40);
  832. adm8211_write_bbp(dev, 0x60, 0x08);
  833. adm8211_write_bbp(dev, 0x64, 0x01);
  834. break;
  835. case ADM8211_MAX2820:
  836. adm8211_write_bbp(dev, 0x00, 0x00);
  837. adm8211_write_bbp(dev, 0x01, 0x00);
  838. adm8211_write_bbp(dev, 0x02, 0x00);
  839. adm8211_write_bbp(dev, 0x03, 0x00);
  840. adm8211_write_bbp(dev, 0x06, 0x0f);
  841. adm8211_write_bbp(dev, 0x09, 0x05);
  842. adm8211_write_bbp(dev, 0x0a, 0x02);
  843. adm8211_write_bbp(dev, 0x0b, 0x00);
  844. adm8211_write_bbp(dev, 0x0c, 0x0f);
  845. adm8211_write_bbp(dev, 0x0f, 0x55);
  846. adm8211_write_bbp(dev, 0x10, 0x8d);
  847. adm8211_write_bbp(dev, 0x11, 0x43);
  848. adm8211_write_bbp(dev, 0x18, 0x4a);
  849. adm8211_write_bbp(dev, 0x20, 0x20);
  850. adm8211_write_bbp(dev, 0x21, 0x02);
  851. adm8211_write_bbp(dev, 0x22, 0x23);
  852. adm8211_write_bbp(dev, 0x23, 0x30);
  853. adm8211_write_bbp(dev, 0x24, 0x2d);
  854. adm8211_write_bbp(dev, 0x2a, 0x8c);
  855. adm8211_write_bbp(dev, 0x2b, 0x81);
  856. adm8211_write_bbp(dev, 0x2c, 0x44);
  857. adm8211_write_bbp(dev, 0x29, 0x4a);
  858. adm8211_write_bbp(dev, 0x60, 0x2b);
  859. adm8211_write_bbp(dev, 0x64, 0x01);
  860. break;
  861. case ADM8211_AL2210L:
  862. adm8211_write_bbp(dev, 0x00, 0x00);
  863. adm8211_write_bbp(dev, 0x01, 0x00);
  864. adm8211_write_bbp(dev, 0x02, 0x00);
  865. adm8211_write_bbp(dev, 0x03, 0x00);
  866. adm8211_write_bbp(dev, 0x06, 0x0f);
  867. adm8211_write_bbp(dev, 0x07, 0x05);
  868. adm8211_write_bbp(dev, 0x08, 0x03);
  869. adm8211_write_bbp(dev, 0x09, 0x00);
  870. adm8211_write_bbp(dev, 0x0a, 0x00);
  871. adm8211_write_bbp(dev, 0x0b, 0x00);
  872. adm8211_write_bbp(dev, 0x0c, 0x10);
  873. adm8211_write_bbp(dev, 0x0f, 0x55);
  874. adm8211_write_bbp(dev, 0x10, 0x8d);
  875. adm8211_write_bbp(dev, 0x11, 0x43);
  876. adm8211_write_bbp(dev, 0x18, 0x4a);
  877. adm8211_write_bbp(dev, 0x20, 0x20);
  878. adm8211_write_bbp(dev, 0x21, 0x02);
  879. adm8211_write_bbp(dev, 0x22, 0x23);
  880. adm8211_write_bbp(dev, 0x23, 0x30);
  881. adm8211_write_bbp(dev, 0x24, 0x2d);
  882. adm8211_write_bbp(dev, 0x2a, 0xaa);
  883. adm8211_write_bbp(dev, 0x2b, 0x81);
  884. adm8211_write_bbp(dev, 0x2c, 0x44);
  885. adm8211_write_bbp(dev, 0x29, 0xfa);
  886. adm8211_write_bbp(dev, 0x60, 0x2d);
  887. adm8211_write_bbp(dev, 0x64, 0x01);
  888. break;
  889. case ADM8211_RFMD2948:
  890. break;
  891. default:
  892. wiphy_debug(dev->wiphy, "unsupported transceiver %d\n",
  893. priv->transceiver_type);
  894. break;
  895. }
  896. } else
  897. wiphy_debug(dev->wiphy, "unsupported BBP %d\n", priv->bbp_type);
  898. ADM8211_CSR_WRITE(SYNRF, 0);
  899. /* Set RF CAL control source to MAC control */
  900. reg = ADM8211_CSR_READ(SYNCTL);
  901. reg |= ADM8211_SYNCTL_SELCAL;
  902. ADM8211_CSR_WRITE(SYNCTL, reg);
  903. return 0;
  904. }
  905. /* configures hw beacons/probe responses */
  906. static int adm8211_set_rate(struct ieee80211_hw *dev)
  907. {
  908. struct adm8211_priv *priv = dev->priv;
  909. u32 reg;
  910. int i = 0;
  911. u8 rate_buf[12] = {0};
  912. /* write supported rates */
  913. if (priv->pdev->revision != ADM8211_REV_BA) {
  914. rate_buf[0] = ARRAY_SIZE(adm8211_rates);
  915. for (i = 0; i < ARRAY_SIZE(adm8211_rates); i++)
  916. rate_buf[i + 1] = (adm8211_rates[i].bitrate / 5) | 0x80;
  917. } else {
  918. /* workaround for rev BA specific bug */
  919. rate_buf[0] = 0x04;
  920. rate_buf[1] = 0x82;
  921. rate_buf[2] = 0x04;
  922. rate_buf[3] = 0x0b;
  923. rate_buf[4] = 0x16;
  924. }
  925. adm8211_write_sram_bytes(dev, ADM8211_SRAM_SUPP_RATE, rate_buf,
  926. ARRAY_SIZE(adm8211_rates) + 1);
  927. reg = ADM8211_CSR_READ(PLCPHD) & 0x00FFFFFF; /* keep bits 0-23 */
  928. reg |= 1 << 15; /* short preamble */
  929. reg |= 110 << 24;
  930. ADM8211_CSR_WRITE(PLCPHD, reg);
  931. /* MTMLT = 512 TU (max TX MSDU lifetime)
  932. * BCNTSIG = plcp_signal (beacon, probe resp, and atim TX rate)
  933. * SRTYLIM = 224 (short retry limit, TX header value is default) */
  934. ADM8211_CSR_WRITE(TXLMT, (512 << 16) | (110 << 8) | (224 << 0));
  935. return 0;
  936. }
  937. static void adm8211_hw_init(struct ieee80211_hw *dev)
  938. {
  939. struct adm8211_priv *priv = dev->priv;
  940. u32 reg;
  941. u8 cline;
  942. reg = ADM8211_CSR_READ(PAR);
  943. reg |= ADM8211_PAR_MRLE | ADM8211_PAR_MRME;
  944. reg &= ~(ADM8211_PAR_BAR | ADM8211_PAR_CAL);
  945. if (!pci_set_mwi(priv->pdev)) {
  946. reg |= 0x1 << 24;
  947. pci_read_config_byte(priv->pdev, PCI_CACHE_LINE_SIZE, &cline);
  948. switch (cline) {
  949. case 0x8:
  950. reg |= (0x1 << 14);
  951. break;
  952. case 0x10:
  953. reg |= (0x2 << 14);
  954. break;
  955. case 0x20:
  956. reg |= (0x3 << 14);
  957. break;
  958. default:
  959. reg |= (0x0 << 14);
  960. break;
  961. }
  962. }
  963. ADM8211_CSR_WRITE(PAR, reg);
  964. reg = ADM8211_CSR_READ(CSR_TEST1);
  965. reg &= ~(0xF << 28);
  966. reg |= (1 << 28) | (1 << 31);
  967. ADM8211_CSR_WRITE(CSR_TEST1, reg);
  968. /* lose link after 4 lost beacons */
  969. reg = (0x04 << 21) | ADM8211_WCSR_TSFTWE | ADM8211_WCSR_LSOE;
  970. ADM8211_CSR_WRITE(WCSR, reg);
  971. /* Disable APM, enable receive FIFO threshold, and set drain receive
  972. * threshold to store-and-forward */
  973. reg = ADM8211_CSR_READ(CMDR);
  974. reg &= ~(ADM8211_CMDR_APM | ADM8211_CMDR_DRT);
  975. reg |= ADM8211_CMDR_RTE | ADM8211_CMDR_DRT_SF;
  976. ADM8211_CSR_WRITE(CMDR, reg);
  977. adm8211_set_rate(dev);
  978. /* 4-bit values:
  979. * PWR1UP = 8 * 2 ms
  980. * PWR0PAPE = 8 us or 5 us
  981. * PWR1PAPE = 1 us or 3 us
  982. * PWR0TRSW = 5 us
  983. * PWR1TRSW = 12 us
  984. * PWR0PE2 = 13 us
  985. * PWR1PE2 = 1 us
  986. * PWR0TXPE = 8 or 6 */
  987. if (priv->pdev->revision < ADM8211_REV_CA)
  988. ADM8211_CSR_WRITE(TOFS2, 0x8815cd18);
  989. else
  990. ADM8211_CSR_WRITE(TOFS2, 0x8535cd16);
  991. /* Enable store and forward for transmit */
  992. priv->nar = ADM8211_NAR_SF | ADM8211_NAR_PB;
  993. ADM8211_CSR_WRITE(NAR, priv->nar);
  994. /* Reset RF */
  995. ADM8211_CSR_WRITE(SYNRF, ADM8211_SYNRF_RADIO);
  996. ADM8211_CSR_READ(SYNRF);
  997. msleep(10);
  998. ADM8211_CSR_WRITE(SYNRF, 0);
  999. ADM8211_CSR_READ(SYNRF);
  1000. msleep(5);
  1001. /* Set CFP Max Duration to 0x10 TU */
  1002. reg = ADM8211_CSR_READ(CFPP);
  1003. reg &= ~(0xffff << 8);
  1004. reg |= 0x0010 << 8;
  1005. ADM8211_CSR_WRITE(CFPP, reg);
  1006. /* USCNT = 0x16 (number of system clocks, 22 MHz, in 1us
  1007. * TUCNT = 0x3ff - Tu counter 1024 us */
  1008. ADM8211_CSR_WRITE(TOFS0, (0x16 << 24) | 0x3ff);
  1009. /* SLOT=20 us, SIFS=110 cycles of 22 MHz (5 us),
  1010. * DIFS=50 us, EIFS=100 us */
  1011. if (priv->pdev->revision < ADM8211_REV_CA)
  1012. ADM8211_CSR_WRITE(IFST, (20 << 23) | (110 << 15) |
  1013. (50 << 9) | 100);
  1014. else
  1015. ADM8211_CSR_WRITE(IFST, (20 << 23) | (24 << 15) |
  1016. (50 << 9) | 100);
  1017. /* PCNT = 1 (MAC idle time awake/sleep, unit S)
  1018. * RMRD = 2346 * 8 + 1 us (max RX duration) */
  1019. ADM8211_CSR_WRITE(RMD, (1 << 16) | 18769);
  1020. /* MART=65535 us, MIRT=256 us, TSFTOFST=0 us */
  1021. ADM8211_CSR_WRITE(RSPT, 0xffffff00);
  1022. /* Initialize BBP (and SYN) */
  1023. adm8211_hw_init_bbp(dev);
  1024. /* make sure interrupts are off */
  1025. ADM8211_CSR_WRITE(IER, 0);
  1026. /* ACK interrupts */
  1027. ADM8211_CSR_WRITE(STSR, ADM8211_CSR_READ(STSR));
  1028. /* Setup WEP (turns it off for now) */
  1029. reg = ADM8211_CSR_READ(MACTEST);
  1030. reg &= ~(7 << 20);
  1031. ADM8211_CSR_WRITE(MACTEST, reg);
  1032. reg = ADM8211_CSR_READ(WEPCTL);
  1033. reg &= ~ADM8211_WEPCTL_WEPENABLE;
  1034. reg |= ADM8211_WEPCTL_WEPRXBYP;
  1035. ADM8211_CSR_WRITE(WEPCTL, reg);
  1036. /* Clear the missed-packet counter. */
  1037. ADM8211_CSR_READ(LPC);
  1038. }
  1039. static int adm8211_hw_reset(struct ieee80211_hw *dev)
  1040. {
  1041. struct adm8211_priv *priv = dev->priv;
  1042. u32 reg, tmp;
  1043. int timeout = 100;
  1044. /* Power-on issue */
  1045. /* TODO: check if this is necessary */
  1046. ADM8211_CSR_WRITE(FRCTL, 0);
  1047. /* Reset the chip */
  1048. tmp = ADM8211_CSR_READ(PAR);
  1049. ADM8211_CSR_WRITE(PAR, ADM8211_PAR_SWR);
  1050. while ((ADM8211_CSR_READ(PAR) & ADM8211_PAR_SWR) && timeout--)
  1051. msleep(50);
  1052. if (timeout <= 0)
  1053. return -ETIMEDOUT;
  1054. ADM8211_CSR_WRITE(PAR, tmp);
  1055. if (priv->pdev->revision == ADM8211_REV_BA &&
  1056. (priv->transceiver_type == ADM8211_RFMD2958_RF3000_CONTROL_POWER ||
  1057. priv->transceiver_type == ADM8211_RFMD2958)) {
  1058. reg = ADM8211_CSR_READ(CSR_TEST1);
  1059. reg |= (1 << 4) | (1 << 5);
  1060. ADM8211_CSR_WRITE(CSR_TEST1, reg);
  1061. } else if (priv->pdev->revision == ADM8211_REV_CA) {
  1062. reg = ADM8211_CSR_READ(CSR_TEST1);
  1063. reg &= ~((1 << 4) | (1 << 5));
  1064. ADM8211_CSR_WRITE(CSR_TEST1, reg);
  1065. }
  1066. ADM8211_CSR_WRITE(FRCTL, 0);
  1067. reg = ADM8211_CSR_READ(CSR_TEST0);
  1068. reg |= ADM8211_CSR_TEST0_EPRLD; /* EEPROM Recall */
  1069. ADM8211_CSR_WRITE(CSR_TEST0, reg);
  1070. adm8211_clear_sram(dev);
  1071. return 0;
  1072. }
  1073. static u64 adm8211_get_tsft(struct ieee80211_hw *dev,
  1074. struct ieee80211_vif *vif)
  1075. {
  1076. struct adm8211_priv *priv = dev->priv;
  1077. u32 tsftl;
  1078. u64 tsft;
  1079. tsftl = ADM8211_CSR_READ(TSFTL);
  1080. tsft = ADM8211_CSR_READ(TSFTH);
  1081. tsft <<= 32;
  1082. tsft |= tsftl;
  1083. return tsft;
  1084. }
  1085. static void adm8211_set_interval(struct ieee80211_hw *dev,
  1086. unsigned short bi, unsigned short li)
  1087. {
  1088. struct adm8211_priv *priv = dev->priv;
  1089. u32 reg;
  1090. /* BP (beacon interval) = data->beacon_interval
  1091. * LI (listen interval) = data->listen_interval (in beacon intervals) */
  1092. reg = (bi << 16) | li;
  1093. ADM8211_CSR_WRITE(BPLI, reg);
  1094. }
  1095. static void adm8211_set_bssid(struct ieee80211_hw *dev, const u8 *bssid)
  1096. {
  1097. struct adm8211_priv *priv = dev->priv;
  1098. u32 reg;
  1099. ADM8211_CSR_WRITE(BSSID0, le32_to_cpu(*(__le32 *)bssid));
  1100. reg = ADM8211_CSR_READ(ABDA1);
  1101. reg &= 0x0000ffff;
  1102. reg |= (bssid[4] << 16) | (bssid[5] << 24);
  1103. ADM8211_CSR_WRITE(ABDA1, reg);
  1104. }
  1105. static int adm8211_config(struct ieee80211_hw *dev, u32 changed)
  1106. {
  1107. struct adm8211_priv *priv = dev->priv;
  1108. struct ieee80211_conf *conf = &dev->conf;
  1109. int channel =
  1110. ieee80211_frequency_to_channel(conf->chandef.chan->center_freq);
  1111. if (channel != priv->channel) {
  1112. priv->channel = channel;
  1113. adm8211_rf_set_channel(dev, priv->channel);
  1114. }
  1115. return 0;
  1116. }
  1117. static void adm8211_bss_info_changed(struct ieee80211_hw *dev,
  1118. struct ieee80211_vif *vif,
  1119. struct ieee80211_bss_conf *conf,
  1120. u32 changes)
  1121. {
  1122. struct adm8211_priv *priv = dev->priv;
  1123. if (!(changes & BSS_CHANGED_BSSID))
  1124. return;
  1125. if (!ether_addr_equal(conf->bssid, priv->bssid)) {
  1126. adm8211_set_bssid(dev, conf->bssid);
  1127. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  1128. }
  1129. }
  1130. static u64 adm8211_prepare_multicast(struct ieee80211_hw *hw,
  1131. struct netdev_hw_addr_list *mc_list)
  1132. {
  1133. unsigned int bit_nr;
  1134. u32 mc_filter[2];
  1135. struct netdev_hw_addr *ha;
  1136. mc_filter[1] = mc_filter[0] = 0;
  1137. netdev_hw_addr_list_for_each(ha, mc_list) {
  1138. bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
  1139. bit_nr &= 0x3F;
  1140. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  1141. }
  1142. return mc_filter[0] | ((u64)(mc_filter[1]) << 32);
  1143. }
  1144. static void adm8211_configure_filter(struct ieee80211_hw *dev,
  1145. unsigned int changed_flags,
  1146. unsigned int *total_flags,
  1147. u64 multicast)
  1148. {
  1149. static const u8 bcast[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  1150. struct adm8211_priv *priv = dev->priv;
  1151. unsigned int new_flags;
  1152. u32 mc_filter[2];
  1153. mc_filter[0] = multicast;
  1154. mc_filter[1] = multicast >> 32;
  1155. new_flags = 0;
  1156. if (*total_flags & FIF_ALLMULTI || multicast == ~(0ULL)) {
  1157. new_flags |= FIF_ALLMULTI;
  1158. priv->nar &= ~ADM8211_NAR_PR;
  1159. priv->nar |= ADM8211_NAR_MM;
  1160. mc_filter[1] = mc_filter[0] = ~0;
  1161. } else {
  1162. priv->nar &= ~(ADM8211_NAR_MM | ADM8211_NAR_PR);
  1163. }
  1164. ADM8211_IDLE_RX();
  1165. ADM8211_CSR_WRITE(MAR0, mc_filter[0]);
  1166. ADM8211_CSR_WRITE(MAR1, mc_filter[1]);
  1167. ADM8211_CSR_READ(NAR);
  1168. if (priv->nar & ADM8211_NAR_PR)
  1169. ieee80211_hw_set(dev, RX_INCLUDES_FCS);
  1170. else
  1171. __clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, dev->flags);
  1172. if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
  1173. adm8211_set_bssid(dev, bcast);
  1174. else
  1175. adm8211_set_bssid(dev, priv->bssid);
  1176. ADM8211_RESTORE();
  1177. *total_flags = new_flags;
  1178. }
  1179. static int adm8211_add_interface(struct ieee80211_hw *dev,
  1180. struct ieee80211_vif *vif)
  1181. {
  1182. struct adm8211_priv *priv = dev->priv;
  1183. if (priv->mode != NL80211_IFTYPE_MONITOR)
  1184. return -EOPNOTSUPP;
  1185. switch (vif->type) {
  1186. case NL80211_IFTYPE_STATION:
  1187. priv->mode = vif->type;
  1188. break;
  1189. default:
  1190. return -EOPNOTSUPP;
  1191. }
  1192. ADM8211_IDLE();
  1193. ADM8211_CSR_WRITE(PAR0, le32_to_cpu(*(__le32 *)vif->addr));
  1194. ADM8211_CSR_WRITE(PAR1, le16_to_cpu(*(__le16 *)(vif->addr + 4)));
  1195. adm8211_update_mode(dev);
  1196. ADM8211_RESTORE();
  1197. return 0;
  1198. }
  1199. static void adm8211_remove_interface(struct ieee80211_hw *dev,
  1200. struct ieee80211_vif *vif)
  1201. {
  1202. struct adm8211_priv *priv = dev->priv;
  1203. priv->mode = NL80211_IFTYPE_MONITOR;
  1204. }
  1205. static int adm8211_init_rings(struct ieee80211_hw *dev)
  1206. {
  1207. struct adm8211_priv *priv = dev->priv;
  1208. struct adm8211_desc *desc = NULL;
  1209. struct adm8211_rx_ring_info *rx_info;
  1210. struct adm8211_tx_ring_info *tx_info;
  1211. unsigned int i;
  1212. for (i = 0; i < priv->rx_ring_size; i++) {
  1213. desc = &priv->rx_ring[i];
  1214. desc->status = 0;
  1215. desc->length = cpu_to_le32(RX_PKT_SIZE);
  1216. priv->rx_buffers[i].skb = NULL;
  1217. }
  1218. /* Mark the end of RX ring; hw returns to base address after this
  1219. * descriptor */
  1220. desc->length |= cpu_to_le32(RDES1_CONTROL_RER);
  1221. for (i = 0; i < priv->rx_ring_size; i++) {
  1222. desc = &priv->rx_ring[i];
  1223. rx_info = &priv->rx_buffers[i];
  1224. rx_info->skb = dev_alloc_skb(RX_PKT_SIZE);
  1225. if (rx_info->skb == NULL)
  1226. break;
  1227. rx_info->mapping = pci_map_single(priv->pdev,
  1228. skb_tail_pointer(rx_info->skb),
  1229. RX_PKT_SIZE,
  1230. PCI_DMA_FROMDEVICE);
  1231. if (pci_dma_mapping_error(priv->pdev, rx_info->mapping)) {
  1232. dev_kfree_skb(rx_info->skb);
  1233. rx_info->skb = NULL;
  1234. break;
  1235. }
  1236. desc->buffer1 = cpu_to_le32(rx_info->mapping);
  1237. desc->status = cpu_to_le32(RDES0_STATUS_OWN | RDES0_STATUS_SQL);
  1238. }
  1239. /* Setup TX ring. TX buffers descriptors will be filled in as needed */
  1240. for (i = 0; i < priv->tx_ring_size; i++) {
  1241. desc = &priv->tx_ring[i];
  1242. tx_info = &priv->tx_buffers[i];
  1243. tx_info->skb = NULL;
  1244. tx_info->mapping = 0;
  1245. desc->status = 0;
  1246. }
  1247. desc->length = cpu_to_le32(TDES1_CONTROL_TER);
  1248. priv->cur_rx = priv->cur_tx = priv->dirty_tx = 0;
  1249. ADM8211_CSR_WRITE(RDB, priv->rx_ring_dma);
  1250. ADM8211_CSR_WRITE(TDBD, priv->tx_ring_dma);
  1251. return 0;
  1252. }
  1253. static void adm8211_free_rings(struct ieee80211_hw *dev)
  1254. {
  1255. struct adm8211_priv *priv = dev->priv;
  1256. unsigned int i;
  1257. for (i = 0; i < priv->rx_ring_size; i++) {
  1258. if (!priv->rx_buffers[i].skb)
  1259. continue;
  1260. pci_unmap_single(
  1261. priv->pdev,
  1262. priv->rx_buffers[i].mapping,
  1263. RX_PKT_SIZE, PCI_DMA_FROMDEVICE);
  1264. dev_kfree_skb(priv->rx_buffers[i].skb);
  1265. }
  1266. for (i = 0; i < priv->tx_ring_size; i++) {
  1267. if (!priv->tx_buffers[i].skb)
  1268. continue;
  1269. pci_unmap_single(priv->pdev,
  1270. priv->tx_buffers[i].mapping,
  1271. priv->tx_buffers[i].skb->len,
  1272. PCI_DMA_TODEVICE);
  1273. dev_kfree_skb(priv->tx_buffers[i].skb);
  1274. }
  1275. }
  1276. static int adm8211_start(struct ieee80211_hw *dev)
  1277. {
  1278. struct adm8211_priv *priv = dev->priv;
  1279. int retval;
  1280. /* Power up MAC and RF chips */
  1281. retval = adm8211_hw_reset(dev);
  1282. if (retval) {
  1283. wiphy_err(dev->wiphy, "hardware reset failed\n");
  1284. goto fail;
  1285. }
  1286. retval = adm8211_init_rings(dev);
  1287. if (retval) {
  1288. wiphy_err(dev->wiphy, "failed to initialize rings\n");
  1289. goto fail;
  1290. }
  1291. /* Init hardware */
  1292. adm8211_hw_init(dev);
  1293. adm8211_rf_set_channel(dev, priv->channel);
  1294. retval = request_irq(priv->pdev->irq, adm8211_interrupt,
  1295. IRQF_SHARED, "adm8211", dev);
  1296. if (retval) {
  1297. wiphy_err(dev->wiphy, "failed to register IRQ handler\n");
  1298. goto fail;
  1299. }
  1300. ADM8211_CSR_WRITE(IER, ADM8211_IER_NIE | ADM8211_IER_AIE |
  1301. ADM8211_IER_RCIE | ADM8211_IER_TCIE |
  1302. ADM8211_IER_TDUIE | ADM8211_IER_GPTIE);
  1303. priv->mode = NL80211_IFTYPE_MONITOR;
  1304. adm8211_update_mode(dev);
  1305. ADM8211_CSR_WRITE(RDR, 0);
  1306. adm8211_set_interval(dev, 100, 10);
  1307. return 0;
  1308. fail:
  1309. return retval;
  1310. }
  1311. static void adm8211_stop(struct ieee80211_hw *dev)
  1312. {
  1313. struct adm8211_priv *priv = dev->priv;
  1314. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1315. priv->nar = 0;
  1316. ADM8211_CSR_WRITE(NAR, 0);
  1317. ADM8211_CSR_WRITE(IER, 0);
  1318. ADM8211_CSR_READ(NAR);
  1319. free_irq(priv->pdev->irq, dev);
  1320. adm8211_free_rings(dev);
  1321. }
  1322. static void adm8211_calc_durations(int *dur, int *plcp, size_t payload_len, int len,
  1323. int plcp_signal, int short_preamble)
  1324. {
  1325. /* Alternative calculation from NetBSD: */
  1326. /* IEEE 802.11b durations for DSSS PHY in microseconds */
  1327. #define IEEE80211_DUR_DS_LONG_PREAMBLE 144
  1328. #define IEEE80211_DUR_DS_SHORT_PREAMBLE 72
  1329. #define IEEE80211_DUR_DS_FAST_PLCPHDR 24
  1330. #define IEEE80211_DUR_DS_SLOW_PLCPHDR 48
  1331. #define IEEE80211_DUR_DS_SLOW_ACK 112
  1332. #define IEEE80211_DUR_DS_FAST_ACK 56
  1333. #define IEEE80211_DUR_DS_SLOW_CTS 112
  1334. #define IEEE80211_DUR_DS_FAST_CTS 56
  1335. #define IEEE80211_DUR_DS_SLOT 20
  1336. #define IEEE80211_DUR_DS_SIFS 10
  1337. int remainder;
  1338. *dur = (80 * (24 + payload_len) + plcp_signal - 1)
  1339. / plcp_signal;
  1340. if (plcp_signal <= PLCP_SIGNAL_2M)
  1341. /* 1-2Mbps WLAN: send ACK/CTS at 1Mbps */
  1342. *dur += 3 * (IEEE80211_DUR_DS_SIFS +
  1343. IEEE80211_DUR_DS_SHORT_PREAMBLE +
  1344. IEEE80211_DUR_DS_FAST_PLCPHDR) +
  1345. IEEE80211_DUR_DS_SLOW_CTS + IEEE80211_DUR_DS_SLOW_ACK;
  1346. else
  1347. /* 5-11Mbps WLAN: send ACK/CTS at 2Mbps */
  1348. *dur += 3 * (IEEE80211_DUR_DS_SIFS +
  1349. IEEE80211_DUR_DS_SHORT_PREAMBLE +
  1350. IEEE80211_DUR_DS_FAST_PLCPHDR) +
  1351. IEEE80211_DUR_DS_FAST_CTS + IEEE80211_DUR_DS_FAST_ACK;
  1352. /* lengthen duration if long preamble */
  1353. if (!short_preamble)
  1354. *dur += 3 * (IEEE80211_DUR_DS_LONG_PREAMBLE -
  1355. IEEE80211_DUR_DS_SHORT_PREAMBLE) +
  1356. 3 * (IEEE80211_DUR_DS_SLOW_PLCPHDR -
  1357. IEEE80211_DUR_DS_FAST_PLCPHDR);
  1358. *plcp = (80 * len) / plcp_signal;
  1359. remainder = (80 * len) % plcp_signal;
  1360. if (plcp_signal == PLCP_SIGNAL_11M &&
  1361. remainder <= 30 && remainder > 0)
  1362. *plcp = (*plcp | 0x8000) + 1;
  1363. else if (remainder)
  1364. (*plcp)++;
  1365. }
  1366. /* Transmit skb w/adm8211_tx_hdr (802.11 header created by hardware) */
  1367. static int adm8211_tx_raw(struct ieee80211_hw *dev, struct sk_buff *skb,
  1368. u16 plcp_signal,
  1369. size_t hdrlen)
  1370. {
  1371. struct adm8211_priv *priv = dev->priv;
  1372. unsigned long flags;
  1373. dma_addr_t mapping;
  1374. unsigned int entry;
  1375. u32 flag;
  1376. mapping = pci_map_single(priv->pdev, skb->data, skb->len,
  1377. PCI_DMA_TODEVICE);
  1378. if (pci_dma_mapping_error(priv->pdev, mapping))
  1379. return -ENOMEM;
  1380. spin_lock_irqsave(&priv->lock, flags);
  1381. if (priv->cur_tx - priv->dirty_tx == priv->tx_ring_size / 2)
  1382. flag = TDES1_CONTROL_IC | TDES1_CONTROL_LS | TDES1_CONTROL_FS;
  1383. else
  1384. flag = TDES1_CONTROL_LS | TDES1_CONTROL_FS;
  1385. if (priv->cur_tx - priv->dirty_tx == priv->tx_ring_size - 2)
  1386. ieee80211_stop_queue(dev, 0);
  1387. entry = priv->cur_tx % priv->tx_ring_size;
  1388. priv->tx_buffers[entry].skb = skb;
  1389. priv->tx_buffers[entry].mapping = mapping;
  1390. priv->tx_buffers[entry].hdrlen = hdrlen;
  1391. priv->tx_ring[entry].buffer1 = cpu_to_le32(mapping);
  1392. if (entry == priv->tx_ring_size - 1)
  1393. flag |= TDES1_CONTROL_TER;
  1394. priv->tx_ring[entry].length = cpu_to_le32(flag | skb->len);
  1395. /* Set TX rate (SIGNAL field in PLCP PPDU format) */
  1396. flag = TDES0_CONTROL_OWN | (plcp_signal << 20) | 8 /* ? */;
  1397. priv->tx_ring[entry].status = cpu_to_le32(flag);
  1398. priv->cur_tx++;
  1399. spin_unlock_irqrestore(&priv->lock, flags);
  1400. /* Trigger transmit poll */
  1401. ADM8211_CSR_WRITE(TDR, 0);
  1402. return 0;
  1403. }
  1404. /* Put adm8211_tx_hdr on skb and transmit */
  1405. static void adm8211_tx(struct ieee80211_hw *dev,
  1406. struct ieee80211_tx_control *control,
  1407. struct sk_buff *skb)
  1408. {
  1409. struct adm8211_tx_hdr *txhdr;
  1410. size_t payload_len, hdrlen;
  1411. int plcp, dur, len, plcp_signal, short_preamble;
  1412. struct ieee80211_hdr *hdr;
  1413. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1414. struct ieee80211_rate *txrate = ieee80211_get_tx_rate(dev, info);
  1415. u8 rc_flags;
  1416. rc_flags = info->control.rates[0].flags;
  1417. short_preamble = !!(rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
  1418. plcp_signal = txrate->bitrate;
  1419. hdr = (struct ieee80211_hdr *)skb->data;
  1420. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1421. memcpy(skb->cb, skb->data, hdrlen);
  1422. hdr = (struct ieee80211_hdr *)skb->cb;
  1423. skb_pull(skb, hdrlen);
  1424. payload_len = skb->len;
  1425. txhdr = skb_push(skb, sizeof(*txhdr));
  1426. memset(txhdr, 0, sizeof(*txhdr));
  1427. memcpy(txhdr->da, ieee80211_get_DA(hdr), ETH_ALEN);
  1428. txhdr->signal = plcp_signal;
  1429. txhdr->frame_body_size = cpu_to_le16(payload_len);
  1430. txhdr->frame_control = hdr->frame_control;
  1431. len = hdrlen + payload_len + FCS_LEN;
  1432. txhdr->frag = cpu_to_le16(0x0FFF);
  1433. adm8211_calc_durations(&dur, &plcp, payload_len,
  1434. len, plcp_signal, short_preamble);
  1435. txhdr->plcp_frag_head_len = cpu_to_le16(plcp);
  1436. txhdr->plcp_frag_tail_len = cpu_to_le16(plcp);
  1437. txhdr->dur_frag_head = cpu_to_le16(dur);
  1438. txhdr->dur_frag_tail = cpu_to_le16(dur);
  1439. txhdr->header_control = cpu_to_le16(ADM8211_TXHDRCTL_ENABLE_EXTEND_HEADER);
  1440. if (short_preamble)
  1441. txhdr->header_control |= cpu_to_le16(ADM8211_TXHDRCTL_SHORT_PREAMBLE);
  1442. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS)
  1443. txhdr->header_control |= cpu_to_le16(ADM8211_TXHDRCTL_ENABLE_RTS);
  1444. txhdr->retry_limit = info->control.rates[0].count;
  1445. if (adm8211_tx_raw(dev, skb, plcp_signal, hdrlen)) {
  1446. /* Drop packet */
  1447. ieee80211_free_txskb(dev, skb);
  1448. }
  1449. }
  1450. static int adm8211_alloc_rings(struct ieee80211_hw *dev)
  1451. {
  1452. struct adm8211_priv *priv = dev->priv;
  1453. unsigned int ring_size;
  1454. priv->rx_buffers = kmalloc(sizeof(*priv->rx_buffers) * priv->rx_ring_size +
  1455. sizeof(*priv->tx_buffers) * priv->tx_ring_size, GFP_KERNEL);
  1456. if (!priv->rx_buffers)
  1457. return -ENOMEM;
  1458. priv->tx_buffers = (void *)priv->rx_buffers +
  1459. sizeof(*priv->rx_buffers) * priv->rx_ring_size;
  1460. /* Allocate TX/RX descriptors */
  1461. ring_size = sizeof(struct adm8211_desc) * priv->rx_ring_size +
  1462. sizeof(struct adm8211_desc) * priv->tx_ring_size;
  1463. priv->rx_ring = pci_alloc_consistent(priv->pdev, ring_size,
  1464. &priv->rx_ring_dma);
  1465. if (!priv->rx_ring) {
  1466. kfree(priv->rx_buffers);
  1467. priv->rx_buffers = NULL;
  1468. priv->tx_buffers = NULL;
  1469. return -ENOMEM;
  1470. }
  1471. priv->tx_ring = priv->rx_ring + priv->rx_ring_size;
  1472. priv->tx_ring_dma = priv->rx_ring_dma +
  1473. sizeof(struct adm8211_desc) * priv->rx_ring_size;
  1474. return 0;
  1475. }
  1476. static const struct ieee80211_ops adm8211_ops = {
  1477. .tx = adm8211_tx,
  1478. .start = adm8211_start,
  1479. .stop = adm8211_stop,
  1480. .add_interface = adm8211_add_interface,
  1481. .remove_interface = adm8211_remove_interface,
  1482. .config = adm8211_config,
  1483. .bss_info_changed = adm8211_bss_info_changed,
  1484. .prepare_multicast = adm8211_prepare_multicast,
  1485. .configure_filter = adm8211_configure_filter,
  1486. .get_stats = adm8211_get_stats,
  1487. .get_tsf = adm8211_get_tsft
  1488. };
  1489. static int adm8211_probe(struct pci_dev *pdev,
  1490. const struct pci_device_id *id)
  1491. {
  1492. struct ieee80211_hw *dev;
  1493. struct adm8211_priv *priv;
  1494. unsigned long mem_addr, mem_len;
  1495. unsigned int io_addr, io_len;
  1496. int err;
  1497. u32 reg;
  1498. u8 perm_addr[ETH_ALEN];
  1499. err = pci_enable_device(pdev);
  1500. if (err) {
  1501. printk(KERN_ERR "%s (adm8211): Cannot enable new PCI device\n",
  1502. pci_name(pdev));
  1503. return err;
  1504. }
  1505. io_addr = pci_resource_start(pdev, 0);
  1506. io_len = pci_resource_len(pdev, 0);
  1507. mem_addr = pci_resource_start(pdev, 1);
  1508. mem_len = pci_resource_len(pdev, 1);
  1509. if (io_len < 256 || mem_len < 1024) {
  1510. printk(KERN_ERR "%s (adm8211): Too short PCI resources\n",
  1511. pci_name(pdev));
  1512. goto err_disable_pdev;
  1513. }
  1514. /* check signature */
  1515. pci_read_config_dword(pdev, 0x80 /* CR32 */, &reg);
  1516. if (reg != ADM8211_SIG1 && reg != ADM8211_SIG2) {
  1517. printk(KERN_ERR "%s (adm8211): Invalid signature (0x%x)\n",
  1518. pci_name(pdev), reg);
  1519. goto err_disable_pdev;
  1520. }
  1521. err = pci_request_regions(pdev, "adm8211");
  1522. if (err) {
  1523. printk(KERN_ERR "%s (adm8211): Cannot obtain PCI resources\n",
  1524. pci_name(pdev));
  1525. return err; /* someone else grabbed it? don't disable it */
  1526. }
  1527. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) ||
  1528. pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
  1529. printk(KERN_ERR "%s (adm8211): No suitable DMA available\n",
  1530. pci_name(pdev));
  1531. goto err_free_reg;
  1532. }
  1533. pci_set_master(pdev);
  1534. dev = ieee80211_alloc_hw(sizeof(*priv), &adm8211_ops);
  1535. if (!dev) {
  1536. printk(KERN_ERR "%s (adm8211): ieee80211 alloc failed\n",
  1537. pci_name(pdev));
  1538. err = -ENOMEM;
  1539. goto err_free_reg;
  1540. }
  1541. priv = dev->priv;
  1542. priv->pdev = pdev;
  1543. spin_lock_init(&priv->lock);
  1544. SET_IEEE80211_DEV(dev, &pdev->dev);
  1545. pci_set_drvdata(pdev, dev);
  1546. priv->map = pci_iomap(pdev, 1, mem_len);
  1547. if (!priv->map)
  1548. priv->map = pci_iomap(pdev, 0, io_len);
  1549. if (!priv->map) {
  1550. printk(KERN_ERR "%s (adm8211): Cannot map device memory\n",
  1551. pci_name(pdev));
  1552. err = -ENOMEM;
  1553. goto err_free_dev;
  1554. }
  1555. priv->rx_ring_size = rx_ring_size;
  1556. priv->tx_ring_size = tx_ring_size;
  1557. err = adm8211_alloc_rings(dev);
  1558. if (err) {
  1559. printk(KERN_ERR "%s (adm8211): Cannot allocate TX/RX ring\n",
  1560. pci_name(pdev));
  1561. goto err_iounmap;
  1562. }
  1563. *(__le32 *)perm_addr = cpu_to_le32(ADM8211_CSR_READ(PAR0));
  1564. *(__le16 *)&perm_addr[4] =
  1565. cpu_to_le16(ADM8211_CSR_READ(PAR1) & 0xFFFF);
  1566. if (!is_valid_ether_addr(perm_addr)) {
  1567. printk(KERN_WARNING "%s (adm8211): Invalid hwaddr in EEPROM!\n",
  1568. pci_name(pdev));
  1569. eth_random_addr(perm_addr);
  1570. }
  1571. SET_IEEE80211_PERM_ADDR(dev, perm_addr);
  1572. dev->extra_tx_headroom = sizeof(struct adm8211_tx_hdr);
  1573. /* dev->flags = RX_INCLUDES_FCS in promisc mode */
  1574. ieee80211_hw_set(dev, SIGNAL_UNSPEC);
  1575. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  1576. dev->max_signal = 100; /* FIXME: find better value */
  1577. dev->queues = 1; /* ADM8211C supports more, maybe ADM8211B too */
  1578. priv->retry_limit = 3;
  1579. priv->ant_power = 0x40;
  1580. priv->tx_power = 0x40;
  1581. priv->lpf_cutoff = 0xFF;
  1582. priv->lnags_threshold = 0xFF;
  1583. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1584. /* Power-on issue. EEPROM won't read correctly without */
  1585. if (pdev->revision >= ADM8211_REV_BA) {
  1586. ADM8211_CSR_WRITE(FRCTL, 0);
  1587. ADM8211_CSR_READ(FRCTL);
  1588. ADM8211_CSR_WRITE(FRCTL, 1);
  1589. ADM8211_CSR_READ(FRCTL);
  1590. msleep(100);
  1591. }
  1592. err = adm8211_read_eeprom(dev);
  1593. if (err) {
  1594. printk(KERN_ERR "%s (adm8211): Can't alloc eeprom buffer\n",
  1595. pci_name(pdev));
  1596. goto err_free_desc;
  1597. }
  1598. priv->channel = 1;
  1599. dev->wiphy->bands[NL80211_BAND_2GHZ] = &priv->band;
  1600. wiphy_ext_feature_set(dev->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
  1601. err = ieee80211_register_hw(dev);
  1602. if (err) {
  1603. printk(KERN_ERR "%s (adm8211): Cannot register device\n",
  1604. pci_name(pdev));
  1605. goto err_free_eeprom;
  1606. }
  1607. wiphy_info(dev->wiphy, "hwaddr %pM, Rev 0x%02x\n",
  1608. dev->wiphy->perm_addr, pdev->revision);
  1609. return 0;
  1610. err_free_eeprom:
  1611. kfree(priv->eeprom);
  1612. err_free_desc:
  1613. pci_free_consistent(pdev,
  1614. sizeof(struct adm8211_desc) * priv->rx_ring_size +
  1615. sizeof(struct adm8211_desc) * priv->tx_ring_size,
  1616. priv->rx_ring, priv->rx_ring_dma);
  1617. kfree(priv->rx_buffers);
  1618. err_iounmap:
  1619. pci_iounmap(pdev, priv->map);
  1620. err_free_dev:
  1621. ieee80211_free_hw(dev);
  1622. err_free_reg:
  1623. pci_release_regions(pdev);
  1624. err_disable_pdev:
  1625. pci_disable_device(pdev);
  1626. return err;
  1627. }
  1628. static void adm8211_remove(struct pci_dev *pdev)
  1629. {
  1630. struct ieee80211_hw *dev = pci_get_drvdata(pdev);
  1631. struct adm8211_priv *priv;
  1632. if (!dev)
  1633. return;
  1634. ieee80211_unregister_hw(dev);
  1635. priv = dev->priv;
  1636. pci_free_consistent(pdev,
  1637. sizeof(struct adm8211_desc) * priv->rx_ring_size +
  1638. sizeof(struct adm8211_desc) * priv->tx_ring_size,
  1639. priv->rx_ring, priv->rx_ring_dma);
  1640. kfree(priv->rx_buffers);
  1641. kfree(priv->eeprom);
  1642. pci_iounmap(pdev, priv->map);
  1643. pci_release_regions(pdev);
  1644. pci_disable_device(pdev);
  1645. ieee80211_free_hw(dev);
  1646. }
  1647. #ifdef CONFIG_PM
  1648. static int adm8211_suspend(struct pci_dev *pdev, pm_message_t state)
  1649. {
  1650. pci_save_state(pdev);
  1651. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1652. return 0;
  1653. }
  1654. static int adm8211_resume(struct pci_dev *pdev)
  1655. {
  1656. pci_set_power_state(pdev, PCI_D0);
  1657. pci_restore_state(pdev);
  1658. return 0;
  1659. }
  1660. #endif /* CONFIG_PM */
  1661. MODULE_DEVICE_TABLE(pci, adm8211_pci_id_table);
  1662. /* TODO: implement enable_wake */
  1663. static struct pci_driver adm8211_driver = {
  1664. .name = "adm8211",
  1665. .id_table = adm8211_pci_id_table,
  1666. .probe = adm8211_probe,
  1667. .remove = adm8211_remove,
  1668. #ifdef CONFIG_PM
  1669. .suspend = adm8211_suspend,
  1670. .resume = adm8211_resume,
  1671. #endif /* CONFIG_PM */
  1672. };
  1673. module_pci_driver(adm8211_driver);