rt61pci.c 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116
  1. /*
  2. Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
  3. <http://rt2x00.serialmonkey.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the
  14. Free Software Foundation, Inc.,
  15. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. /*
  18. Module: rt61pci
  19. Abstract: rt61pci device specific routines.
  20. Supported chipsets: RT2561, RT2561s, RT2661.
  21. */
  22. #include <linux/crc-itu-t.h>
  23. #include <linux/delay.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/init.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/slab.h>
  29. #include <linux/pci.h>
  30. #include <linux/eeprom_93cx6.h>
  31. #include "rt2x00.h"
  32. #include "rt2x00pci.h"
  33. #include "rt61pci.h"
  34. /*
  35. * Allow hardware encryption to be disabled.
  36. */
  37. static bool modparam_nohwcrypt = false;
  38. module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
  39. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  40. /*
  41. * Register access.
  42. * BBP and RF register require indirect register access,
  43. * and use the CSR registers PHY_CSR3 and PHY_CSR4 to achieve this.
  44. * These indirect registers work with busy bits,
  45. * and we will try maximal REGISTER_BUSY_COUNT times to access
  46. * the register while taking a REGISTER_BUSY_DELAY us delay
  47. * between each attempt. When the busy bit is still set at that time,
  48. * the access attempt is considered to have failed,
  49. * and we will print an error.
  50. */
  51. #define WAIT_FOR_BBP(__dev, __reg) \
  52. rt2x00pci_regbusy_read((__dev), PHY_CSR3, PHY_CSR3_BUSY, (__reg))
  53. #define WAIT_FOR_RF(__dev, __reg) \
  54. rt2x00pci_regbusy_read((__dev), PHY_CSR4, PHY_CSR4_BUSY, (__reg))
  55. #define WAIT_FOR_MCU(__dev, __reg) \
  56. rt2x00pci_regbusy_read((__dev), H2M_MAILBOX_CSR, \
  57. H2M_MAILBOX_CSR_OWNER, (__reg))
  58. static void rt61pci_bbp_write(struct rt2x00_dev *rt2x00dev,
  59. const unsigned int word, const u8 value)
  60. {
  61. u32 reg;
  62. mutex_lock(&rt2x00dev->csr_mutex);
  63. /*
  64. * Wait until the BBP becomes available, afterwards we
  65. * can safely write the new data into the register.
  66. */
  67. if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
  68. reg = 0;
  69. rt2x00_set_field32(&reg, PHY_CSR3_VALUE, value);
  70. rt2x00_set_field32(&reg, PHY_CSR3_REGNUM, word);
  71. rt2x00_set_field32(&reg, PHY_CSR3_BUSY, 1);
  72. rt2x00_set_field32(&reg, PHY_CSR3_READ_CONTROL, 0);
  73. rt2x00pci_register_write(rt2x00dev, PHY_CSR3, reg);
  74. }
  75. mutex_unlock(&rt2x00dev->csr_mutex);
  76. }
  77. static void rt61pci_bbp_read(struct rt2x00_dev *rt2x00dev,
  78. const unsigned int word, u8 *value)
  79. {
  80. u32 reg;
  81. mutex_lock(&rt2x00dev->csr_mutex);
  82. /*
  83. * Wait until the BBP becomes available, afterwards we
  84. * can safely write the read request into the register.
  85. * After the data has been written, we wait until hardware
  86. * returns the correct value, if at any time the register
  87. * doesn't become available in time, reg will be 0xffffffff
  88. * which means we return 0xff to the caller.
  89. */
  90. if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
  91. reg = 0;
  92. rt2x00_set_field32(&reg, PHY_CSR3_REGNUM, word);
  93. rt2x00_set_field32(&reg, PHY_CSR3_BUSY, 1);
  94. rt2x00_set_field32(&reg, PHY_CSR3_READ_CONTROL, 1);
  95. rt2x00pci_register_write(rt2x00dev, PHY_CSR3, reg);
  96. WAIT_FOR_BBP(rt2x00dev, &reg);
  97. }
  98. *value = rt2x00_get_field32(reg, PHY_CSR3_VALUE);
  99. mutex_unlock(&rt2x00dev->csr_mutex);
  100. }
  101. static void rt61pci_rf_write(struct rt2x00_dev *rt2x00dev,
  102. const unsigned int word, const u32 value)
  103. {
  104. u32 reg;
  105. mutex_lock(&rt2x00dev->csr_mutex);
  106. /*
  107. * Wait until the RF becomes available, afterwards we
  108. * can safely write the new data into the register.
  109. */
  110. if (WAIT_FOR_RF(rt2x00dev, &reg)) {
  111. reg = 0;
  112. rt2x00_set_field32(&reg, PHY_CSR4_VALUE, value);
  113. rt2x00_set_field32(&reg, PHY_CSR4_NUMBER_OF_BITS, 21);
  114. rt2x00_set_field32(&reg, PHY_CSR4_IF_SELECT, 0);
  115. rt2x00_set_field32(&reg, PHY_CSR4_BUSY, 1);
  116. rt2x00pci_register_write(rt2x00dev, PHY_CSR4, reg);
  117. rt2x00_rf_write(rt2x00dev, word, value);
  118. }
  119. mutex_unlock(&rt2x00dev->csr_mutex);
  120. }
  121. static void rt61pci_mcu_request(struct rt2x00_dev *rt2x00dev,
  122. const u8 command, const u8 token,
  123. const u8 arg0, const u8 arg1)
  124. {
  125. u32 reg;
  126. mutex_lock(&rt2x00dev->csr_mutex);
  127. /*
  128. * Wait until the MCU becomes available, afterwards we
  129. * can safely write the new data into the register.
  130. */
  131. if (WAIT_FOR_MCU(rt2x00dev, &reg)) {
  132. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_OWNER, 1);
  133. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_CMD_TOKEN, token);
  134. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG0, arg0);
  135. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG1, arg1);
  136. rt2x00pci_register_write(rt2x00dev, H2M_MAILBOX_CSR, reg);
  137. rt2x00pci_register_read(rt2x00dev, HOST_CMD_CSR, &reg);
  138. rt2x00_set_field32(&reg, HOST_CMD_CSR_HOST_COMMAND, command);
  139. rt2x00_set_field32(&reg, HOST_CMD_CSR_INTERRUPT_MCU, 1);
  140. rt2x00pci_register_write(rt2x00dev, HOST_CMD_CSR, reg);
  141. }
  142. mutex_unlock(&rt2x00dev->csr_mutex);
  143. }
  144. static void rt61pci_eepromregister_read(struct eeprom_93cx6 *eeprom)
  145. {
  146. struct rt2x00_dev *rt2x00dev = eeprom->data;
  147. u32 reg;
  148. rt2x00pci_register_read(rt2x00dev, E2PROM_CSR, &reg);
  149. eeprom->reg_data_in = !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_IN);
  150. eeprom->reg_data_out = !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_OUT);
  151. eeprom->reg_data_clock =
  152. !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_CLOCK);
  153. eeprom->reg_chip_select =
  154. !!rt2x00_get_field32(reg, E2PROM_CSR_CHIP_SELECT);
  155. }
  156. static void rt61pci_eepromregister_write(struct eeprom_93cx6 *eeprom)
  157. {
  158. struct rt2x00_dev *rt2x00dev = eeprom->data;
  159. u32 reg = 0;
  160. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_IN, !!eeprom->reg_data_in);
  161. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_OUT, !!eeprom->reg_data_out);
  162. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_CLOCK,
  163. !!eeprom->reg_data_clock);
  164. rt2x00_set_field32(&reg, E2PROM_CSR_CHIP_SELECT,
  165. !!eeprom->reg_chip_select);
  166. rt2x00pci_register_write(rt2x00dev, E2PROM_CSR, reg);
  167. }
  168. #ifdef CONFIG_RT2X00_LIB_DEBUGFS
  169. static const struct rt2x00debug rt61pci_rt2x00debug = {
  170. .owner = THIS_MODULE,
  171. .csr = {
  172. .read = rt2x00pci_register_read,
  173. .write = rt2x00pci_register_write,
  174. .flags = RT2X00DEBUGFS_OFFSET,
  175. .word_base = CSR_REG_BASE,
  176. .word_size = sizeof(u32),
  177. .word_count = CSR_REG_SIZE / sizeof(u32),
  178. },
  179. .eeprom = {
  180. .read = rt2x00_eeprom_read,
  181. .write = rt2x00_eeprom_write,
  182. .word_base = EEPROM_BASE,
  183. .word_size = sizeof(u16),
  184. .word_count = EEPROM_SIZE / sizeof(u16),
  185. },
  186. .bbp = {
  187. .read = rt61pci_bbp_read,
  188. .write = rt61pci_bbp_write,
  189. .word_base = BBP_BASE,
  190. .word_size = sizeof(u8),
  191. .word_count = BBP_SIZE / sizeof(u8),
  192. },
  193. .rf = {
  194. .read = rt2x00_rf_read,
  195. .write = rt61pci_rf_write,
  196. .word_base = RF_BASE,
  197. .word_size = sizeof(u32),
  198. .word_count = RF_SIZE / sizeof(u32),
  199. },
  200. };
  201. #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
  202. static int rt61pci_rfkill_poll(struct rt2x00_dev *rt2x00dev)
  203. {
  204. u32 reg;
  205. rt2x00pci_register_read(rt2x00dev, MAC_CSR13, &reg);
  206. return rt2x00_get_field32(reg, MAC_CSR13_BIT5);
  207. }
  208. #ifdef CONFIG_RT2X00_LIB_LEDS
  209. static void rt61pci_brightness_set(struct led_classdev *led_cdev,
  210. enum led_brightness brightness)
  211. {
  212. struct rt2x00_led *led =
  213. container_of(led_cdev, struct rt2x00_led, led_dev);
  214. unsigned int enabled = brightness != LED_OFF;
  215. unsigned int a_mode =
  216. (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_5GHZ);
  217. unsigned int bg_mode =
  218. (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
  219. if (led->type == LED_TYPE_RADIO) {
  220. rt2x00_set_field16(&led->rt2x00dev->led_mcu_reg,
  221. MCU_LEDCS_RADIO_STATUS, enabled);
  222. rt61pci_mcu_request(led->rt2x00dev, MCU_LED, 0xff,
  223. (led->rt2x00dev->led_mcu_reg & 0xff),
  224. ((led->rt2x00dev->led_mcu_reg >> 8)));
  225. } else if (led->type == LED_TYPE_ASSOC) {
  226. rt2x00_set_field16(&led->rt2x00dev->led_mcu_reg,
  227. MCU_LEDCS_LINK_BG_STATUS, bg_mode);
  228. rt2x00_set_field16(&led->rt2x00dev->led_mcu_reg,
  229. MCU_LEDCS_LINK_A_STATUS, a_mode);
  230. rt61pci_mcu_request(led->rt2x00dev, MCU_LED, 0xff,
  231. (led->rt2x00dev->led_mcu_reg & 0xff),
  232. ((led->rt2x00dev->led_mcu_reg >> 8)));
  233. } else if (led->type == LED_TYPE_QUALITY) {
  234. /*
  235. * The brightness is divided into 6 levels (0 - 5),
  236. * this means we need to convert the brightness
  237. * argument into the matching level within that range.
  238. */
  239. rt61pci_mcu_request(led->rt2x00dev, MCU_LED_STRENGTH, 0xff,
  240. brightness / (LED_FULL / 6), 0);
  241. }
  242. }
  243. static int rt61pci_blink_set(struct led_classdev *led_cdev,
  244. unsigned long *delay_on,
  245. unsigned long *delay_off)
  246. {
  247. struct rt2x00_led *led =
  248. container_of(led_cdev, struct rt2x00_led, led_dev);
  249. u32 reg;
  250. rt2x00pci_register_read(led->rt2x00dev, MAC_CSR14, &reg);
  251. rt2x00_set_field32(&reg, MAC_CSR14_ON_PERIOD, *delay_on);
  252. rt2x00_set_field32(&reg, MAC_CSR14_OFF_PERIOD, *delay_off);
  253. rt2x00pci_register_write(led->rt2x00dev, MAC_CSR14, reg);
  254. return 0;
  255. }
  256. static void rt61pci_init_led(struct rt2x00_dev *rt2x00dev,
  257. struct rt2x00_led *led,
  258. enum led_type type)
  259. {
  260. led->rt2x00dev = rt2x00dev;
  261. led->type = type;
  262. led->led_dev.brightness_set = rt61pci_brightness_set;
  263. led->led_dev.blink_set = rt61pci_blink_set;
  264. led->flags = LED_INITIALIZED;
  265. }
  266. #endif /* CONFIG_RT2X00_LIB_LEDS */
  267. /*
  268. * Configuration handlers.
  269. */
  270. static int rt61pci_config_shared_key(struct rt2x00_dev *rt2x00dev,
  271. struct rt2x00lib_crypto *crypto,
  272. struct ieee80211_key_conf *key)
  273. {
  274. struct hw_key_entry key_entry;
  275. struct rt2x00_field32 field;
  276. u32 mask;
  277. u32 reg;
  278. if (crypto->cmd == SET_KEY) {
  279. /*
  280. * rt2x00lib can't determine the correct free
  281. * key_idx for shared keys. We have 1 register
  282. * with key valid bits. The goal is simple, read
  283. * the register, if that is full we have no slots
  284. * left.
  285. * Note that each BSS is allowed to have up to 4
  286. * shared keys, so put a mask over the allowed
  287. * entries.
  288. */
  289. mask = (0xf << crypto->bssidx);
  290. rt2x00pci_register_read(rt2x00dev, SEC_CSR0, &reg);
  291. reg &= mask;
  292. if (reg && reg == mask)
  293. return -ENOSPC;
  294. key->hw_key_idx += reg ? ffz(reg) : 0;
  295. /*
  296. * Upload key to hardware
  297. */
  298. memcpy(key_entry.key, crypto->key,
  299. sizeof(key_entry.key));
  300. memcpy(key_entry.tx_mic, crypto->tx_mic,
  301. sizeof(key_entry.tx_mic));
  302. memcpy(key_entry.rx_mic, crypto->rx_mic,
  303. sizeof(key_entry.rx_mic));
  304. reg = SHARED_KEY_ENTRY(key->hw_key_idx);
  305. rt2x00pci_register_multiwrite(rt2x00dev, reg,
  306. &key_entry, sizeof(key_entry));
  307. /*
  308. * The cipher types are stored over 2 registers.
  309. * bssidx 0 and 1 keys are stored in SEC_CSR1 and
  310. * bssidx 1 and 2 keys are stored in SEC_CSR5.
  311. * Using the correct defines correctly will cause overhead,
  312. * so just calculate the correct offset.
  313. */
  314. if (key->hw_key_idx < 8) {
  315. field.bit_offset = (3 * key->hw_key_idx);
  316. field.bit_mask = 0x7 << field.bit_offset;
  317. rt2x00pci_register_read(rt2x00dev, SEC_CSR1, &reg);
  318. rt2x00_set_field32(&reg, field, crypto->cipher);
  319. rt2x00pci_register_write(rt2x00dev, SEC_CSR1, reg);
  320. } else {
  321. field.bit_offset = (3 * (key->hw_key_idx - 8));
  322. field.bit_mask = 0x7 << field.bit_offset;
  323. rt2x00pci_register_read(rt2x00dev, SEC_CSR5, &reg);
  324. rt2x00_set_field32(&reg, field, crypto->cipher);
  325. rt2x00pci_register_write(rt2x00dev, SEC_CSR5, reg);
  326. }
  327. /*
  328. * The driver does not support the IV/EIV generation
  329. * in hardware. However it doesn't support the IV/EIV
  330. * inside the ieee80211 frame either, but requires it
  331. * to be provided separately for the descriptor.
  332. * rt2x00lib will cut the IV/EIV data out of all frames
  333. * given to us by mac80211, but we must tell mac80211
  334. * to generate the IV/EIV data.
  335. */
  336. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  337. }
  338. /*
  339. * SEC_CSR0 contains only single-bit fields to indicate
  340. * a particular key is valid. Because using the FIELD32()
  341. * defines directly will cause a lot of overhead, we use
  342. * a calculation to determine the correct bit directly.
  343. */
  344. mask = 1 << key->hw_key_idx;
  345. rt2x00pci_register_read(rt2x00dev, SEC_CSR0, &reg);
  346. if (crypto->cmd == SET_KEY)
  347. reg |= mask;
  348. else if (crypto->cmd == DISABLE_KEY)
  349. reg &= ~mask;
  350. rt2x00pci_register_write(rt2x00dev, SEC_CSR0, reg);
  351. return 0;
  352. }
  353. static int rt61pci_config_pairwise_key(struct rt2x00_dev *rt2x00dev,
  354. struct rt2x00lib_crypto *crypto,
  355. struct ieee80211_key_conf *key)
  356. {
  357. struct hw_pairwise_ta_entry addr_entry;
  358. struct hw_key_entry key_entry;
  359. u32 mask;
  360. u32 reg;
  361. if (crypto->cmd == SET_KEY) {
  362. /*
  363. * rt2x00lib can't determine the correct free
  364. * key_idx for pairwise keys. We have 2 registers
  365. * with key valid bits. The goal is simple: read
  366. * the first register. If that is full, move to
  367. * the next register.
  368. * When both registers are full, we drop the key.
  369. * Otherwise, we use the first invalid entry.
  370. */
  371. rt2x00pci_register_read(rt2x00dev, SEC_CSR2, &reg);
  372. if (reg && reg == ~0) {
  373. key->hw_key_idx = 32;
  374. rt2x00pci_register_read(rt2x00dev, SEC_CSR3, &reg);
  375. if (reg && reg == ~0)
  376. return -ENOSPC;
  377. }
  378. key->hw_key_idx += reg ? ffz(reg) : 0;
  379. /*
  380. * Upload key to hardware
  381. */
  382. memcpy(key_entry.key, crypto->key,
  383. sizeof(key_entry.key));
  384. memcpy(key_entry.tx_mic, crypto->tx_mic,
  385. sizeof(key_entry.tx_mic));
  386. memcpy(key_entry.rx_mic, crypto->rx_mic,
  387. sizeof(key_entry.rx_mic));
  388. memset(&addr_entry, 0, sizeof(addr_entry));
  389. memcpy(&addr_entry, crypto->address, ETH_ALEN);
  390. addr_entry.cipher = crypto->cipher;
  391. reg = PAIRWISE_KEY_ENTRY(key->hw_key_idx);
  392. rt2x00pci_register_multiwrite(rt2x00dev, reg,
  393. &key_entry, sizeof(key_entry));
  394. reg = PAIRWISE_TA_ENTRY(key->hw_key_idx);
  395. rt2x00pci_register_multiwrite(rt2x00dev, reg,
  396. &addr_entry, sizeof(addr_entry));
  397. /*
  398. * Enable pairwise lookup table for given BSS idx.
  399. * Without this, received frames will not be decrypted
  400. * by the hardware.
  401. */
  402. rt2x00pci_register_read(rt2x00dev, SEC_CSR4, &reg);
  403. reg |= (1 << crypto->bssidx);
  404. rt2x00pci_register_write(rt2x00dev, SEC_CSR4, reg);
  405. /*
  406. * The driver does not support the IV/EIV generation
  407. * in hardware. However it doesn't support the IV/EIV
  408. * inside the ieee80211 frame either, but requires it
  409. * to be provided separately for the descriptor.
  410. * rt2x00lib will cut the IV/EIV data out of all frames
  411. * given to us by mac80211, but we must tell mac80211
  412. * to generate the IV/EIV data.
  413. */
  414. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  415. }
  416. /*
  417. * SEC_CSR2 and SEC_CSR3 contain only single-bit fields to indicate
  418. * a particular key is valid. Because using the FIELD32()
  419. * defines directly will cause a lot of overhead, we use
  420. * a calculation to determine the correct bit directly.
  421. */
  422. if (key->hw_key_idx < 32) {
  423. mask = 1 << key->hw_key_idx;
  424. rt2x00pci_register_read(rt2x00dev, SEC_CSR2, &reg);
  425. if (crypto->cmd == SET_KEY)
  426. reg |= mask;
  427. else if (crypto->cmd == DISABLE_KEY)
  428. reg &= ~mask;
  429. rt2x00pci_register_write(rt2x00dev, SEC_CSR2, reg);
  430. } else {
  431. mask = 1 << (key->hw_key_idx - 32);
  432. rt2x00pci_register_read(rt2x00dev, SEC_CSR3, &reg);
  433. if (crypto->cmd == SET_KEY)
  434. reg |= mask;
  435. else if (crypto->cmd == DISABLE_KEY)
  436. reg &= ~mask;
  437. rt2x00pci_register_write(rt2x00dev, SEC_CSR3, reg);
  438. }
  439. return 0;
  440. }
  441. static void rt61pci_config_filter(struct rt2x00_dev *rt2x00dev,
  442. const unsigned int filter_flags)
  443. {
  444. u32 reg;
  445. /*
  446. * Start configuration steps.
  447. * Note that the version error will always be dropped
  448. * and broadcast frames will always be accepted since
  449. * there is no filter for it at this time.
  450. */
  451. rt2x00pci_register_read(rt2x00dev, TXRX_CSR0, &reg);
  452. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_CRC,
  453. !(filter_flags & FIF_FCSFAIL));
  454. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_PHYSICAL,
  455. !(filter_flags & FIF_PLCPFAIL));
  456. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_CONTROL,
  457. !(filter_flags & (FIF_CONTROL | FIF_PSPOLL)));
  458. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_NOT_TO_ME,
  459. !(filter_flags & FIF_PROMISC_IN_BSS));
  460. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_TO_DS,
  461. !(filter_flags & FIF_PROMISC_IN_BSS) &&
  462. !rt2x00dev->intf_ap_count);
  463. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_VERSION_ERROR, 1);
  464. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_MULTICAST,
  465. !(filter_flags & FIF_ALLMULTI));
  466. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_BROADCAST, 0);
  467. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_ACK_CTS,
  468. !(filter_flags & FIF_CONTROL));
  469. rt2x00pci_register_write(rt2x00dev, TXRX_CSR0, reg);
  470. }
  471. static void rt61pci_config_intf(struct rt2x00_dev *rt2x00dev,
  472. struct rt2x00_intf *intf,
  473. struct rt2x00intf_conf *conf,
  474. const unsigned int flags)
  475. {
  476. u32 reg;
  477. if (flags & CONFIG_UPDATE_TYPE) {
  478. /*
  479. * Enable synchronisation.
  480. */
  481. rt2x00pci_register_read(rt2x00dev, TXRX_CSR9, &reg);
  482. rt2x00_set_field32(&reg, TXRX_CSR9_TSF_SYNC, conf->sync);
  483. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, reg);
  484. }
  485. if (flags & CONFIG_UPDATE_MAC) {
  486. reg = le32_to_cpu(conf->mac[1]);
  487. rt2x00_set_field32(&reg, MAC_CSR3_UNICAST_TO_ME_MASK, 0xff);
  488. conf->mac[1] = cpu_to_le32(reg);
  489. rt2x00pci_register_multiwrite(rt2x00dev, MAC_CSR2,
  490. conf->mac, sizeof(conf->mac));
  491. }
  492. if (flags & CONFIG_UPDATE_BSSID) {
  493. reg = le32_to_cpu(conf->bssid[1]);
  494. rt2x00_set_field32(&reg, MAC_CSR5_BSS_ID_MASK, 3);
  495. conf->bssid[1] = cpu_to_le32(reg);
  496. rt2x00pci_register_multiwrite(rt2x00dev, MAC_CSR4,
  497. conf->bssid, sizeof(conf->bssid));
  498. }
  499. }
  500. static void rt61pci_config_erp(struct rt2x00_dev *rt2x00dev,
  501. struct rt2x00lib_erp *erp,
  502. u32 changed)
  503. {
  504. u32 reg;
  505. rt2x00pci_register_read(rt2x00dev, TXRX_CSR0, &reg);
  506. rt2x00_set_field32(&reg, TXRX_CSR0_RX_ACK_TIMEOUT, 0x32);
  507. rt2x00_set_field32(&reg, TXRX_CSR0_TSF_OFFSET, IEEE80211_HEADER);
  508. rt2x00pci_register_write(rt2x00dev, TXRX_CSR0, reg);
  509. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  510. rt2x00pci_register_read(rt2x00dev, TXRX_CSR4, &reg);
  511. rt2x00_set_field32(&reg, TXRX_CSR4_AUTORESPOND_ENABLE, 1);
  512. rt2x00_set_field32(&reg, TXRX_CSR4_AUTORESPOND_PREAMBLE,
  513. !!erp->short_preamble);
  514. rt2x00pci_register_write(rt2x00dev, TXRX_CSR4, reg);
  515. }
  516. if (changed & BSS_CHANGED_BASIC_RATES)
  517. rt2x00pci_register_write(rt2x00dev, TXRX_CSR5,
  518. erp->basic_rates);
  519. if (changed & BSS_CHANGED_BEACON_INT) {
  520. rt2x00pci_register_read(rt2x00dev, TXRX_CSR9, &reg);
  521. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_INTERVAL,
  522. erp->beacon_int * 16);
  523. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, reg);
  524. }
  525. if (changed & BSS_CHANGED_ERP_SLOT) {
  526. rt2x00pci_register_read(rt2x00dev, MAC_CSR9, &reg);
  527. rt2x00_set_field32(&reg, MAC_CSR9_SLOT_TIME, erp->slot_time);
  528. rt2x00pci_register_write(rt2x00dev, MAC_CSR9, reg);
  529. rt2x00pci_register_read(rt2x00dev, MAC_CSR8, &reg);
  530. rt2x00_set_field32(&reg, MAC_CSR8_SIFS, erp->sifs);
  531. rt2x00_set_field32(&reg, MAC_CSR8_SIFS_AFTER_RX_OFDM, 3);
  532. rt2x00_set_field32(&reg, MAC_CSR8_EIFS, erp->eifs);
  533. rt2x00pci_register_write(rt2x00dev, MAC_CSR8, reg);
  534. }
  535. }
  536. static void rt61pci_config_antenna_5x(struct rt2x00_dev *rt2x00dev,
  537. struct antenna_setup *ant)
  538. {
  539. u8 r3;
  540. u8 r4;
  541. u8 r77;
  542. rt61pci_bbp_read(rt2x00dev, 3, &r3);
  543. rt61pci_bbp_read(rt2x00dev, 4, &r4);
  544. rt61pci_bbp_read(rt2x00dev, 77, &r77);
  545. rt2x00_set_field8(&r3, BBP_R3_SMART_MODE, rt2x00_rf(rt2x00dev, RF5325));
  546. /*
  547. * Configure the RX antenna.
  548. */
  549. switch (ant->rx) {
  550. case ANTENNA_HW_DIVERSITY:
  551. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 2);
  552. rt2x00_set_field8(&r4, BBP_R4_RX_FRAME_END,
  553. (rt2x00dev->curr_band != IEEE80211_BAND_5GHZ));
  554. break;
  555. case ANTENNA_A:
  556. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  557. rt2x00_set_field8(&r4, BBP_R4_RX_FRAME_END, 0);
  558. if (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ)
  559. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 0);
  560. else
  561. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 3);
  562. break;
  563. case ANTENNA_B:
  564. default:
  565. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  566. rt2x00_set_field8(&r4, BBP_R4_RX_FRAME_END, 0);
  567. if (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ)
  568. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 3);
  569. else
  570. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 0);
  571. break;
  572. }
  573. rt61pci_bbp_write(rt2x00dev, 77, r77);
  574. rt61pci_bbp_write(rt2x00dev, 3, r3);
  575. rt61pci_bbp_write(rt2x00dev, 4, r4);
  576. }
  577. static void rt61pci_config_antenna_2x(struct rt2x00_dev *rt2x00dev,
  578. struct antenna_setup *ant)
  579. {
  580. u8 r3;
  581. u8 r4;
  582. u8 r77;
  583. rt61pci_bbp_read(rt2x00dev, 3, &r3);
  584. rt61pci_bbp_read(rt2x00dev, 4, &r4);
  585. rt61pci_bbp_read(rt2x00dev, 77, &r77);
  586. rt2x00_set_field8(&r3, BBP_R3_SMART_MODE, rt2x00_rf(rt2x00dev, RF2529));
  587. rt2x00_set_field8(&r4, BBP_R4_RX_FRAME_END,
  588. !test_bit(CAPABILITY_FRAME_TYPE, &rt2x00dev->cap_flags));
  589. /*
  590. * Configure the RX antenna.
  591. */
  592. switch (ant->rx) {
  593. case ANTENNA_HW_DIVERSITY:
  594. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 2);
  595. break;
  596. case ANTENNA_A:
  597. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  598. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 3);
  599. break;
  600. case ANTENNA_B:
  601. default:
  602. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  603. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 0);
  604. break;
  605. }
  606. rt61pci_bbp_write(rt2x00dev, 77, r77);
  607. rt61pci_bbp_write(rt2x00dev, 3, r3);
  608. rt61pci_bbp_write(rt2x00dev, 4, r4);
  609. }
  610. static void rt61pci_config_antenna_2529_rx(struct rt2x00_dev *rt2x00dev,
  611. const int p1, const int p2)
  612. {
  613. u32 reg;
  614. rt2x00pci_register_read(rt2x00dev, MAC_CSR13, &reg);
  615. rt2x00_set_field32(&reg, MAC_CSR13_BIT4, p1);
  616. rt2x00_set_field32(&reg, MAC_CSR13_BIT12, 0);
  617. rt2x00_set_field32(&reg, MAC_CSR13_BIT3, !p2);
  618. rt2x00_set_field32(&reg, MAC_CSR13_BIT11, 0);
  619. rt2x00pci_register_write(rt2x00dev, MAC_CSR13, reg);
  620. }
  621. static void rt61pci_config_antenna_2529(struct rt2x00_dev *rt2x00dev,
  622. struct antenna_setup *ant)
  623. {
  624. u8 r3;
  625. u8 r4;
  626. u8 r77;
  627. rt61pci_bbp_read(rt2x00dev, 3, &r3);
  628. rt61pci_bbp_read(rt2x00dev, 4, &r4);
  629. rt61pci_bbp_read(rt2x00dev, 77, &r77);
  630. /*
  631. * Configure the RX antenna.
  632. */
  633. switch (ant->rx) {
  634. case ANTENNA_A:
  635. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  636. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 0);
  637. rt61pci_config_antenna_2529_rx(rt2x00dev, 0, 0);
  638. break;
  639. case ANTENNA_HW_DIVERSITY:
  640. /*
  641. * FIXME: Antenna selection for the rf 2529 is very confusing
  642. * in the legacy driver. Just default to antenna B until the
  643. * legacy code can be properly translated into rt2x00 code.
  644. */
  645. case ANTENNA_B:
  646. default:
  647. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  648. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 3);
  649. rt61pci_config_antenna_2529_rx(rt2x00dev, 1, 1);
  650. break;
  651. }
  652. rt61pci_bbp_write(rt2x00dev, 77, r77);
  653. rt61pci_bbp_write(rt2x00dev, 3, r3);
  654. rt61pci_bbp_write(rt2x00dev, 4, r4);
  655. }
  656. struct antenna_sel {
  657. u8 word;
  658. /*
  659. * value[0] -> non-LNA
  660. * value[1] -> LNA
  661. */
  662. u8 value[2];
  663. };
  664. static const struct antenna_sel antenna_sel_a[] = {
  665. { 96, { 0x58, 0x78 } },
  666. { 104, { 0x38, 0x48 } },
  667. { 75, { 0xfe, 0x80 } },
  668. { 86, { 0xfe, 0x80 } },
  669. { 88, { 0xfe, 0x80 } },
  670. { 35, { 0x60, 0x60 } },
  671. { 97, { 0x58, 0x58 } },
  672. { 98, { 0x58, 0x58 } },
  673. };
  674. static const struct antenna_sel antenna_sel_bg[] = {
  675. { 96, { 0x48, 0x68 } },
  676. { 104, { 0x2c, 0x3c } },
  677. { 75, { 0xfe, 0x80 } },
  678. { 86, { 0xfe, 0x80 } },
  679. { 88, { 0xfe, 0x80 } },
  680. { 35, { 0x50, 0x50 } },
  681. { 97, { 0x48, 0x48 } },
  682. { 98, { 0x48, 0x48 } },
  683. };
  684. static void rt61pci_config_ant(struct rt2x00_dev *rt2x00dev,
  685. struct antenna_setup *ant)
  686. {
  687. const struct antenna_sel *sel;
  688. unsigned int lna;
  689. unsigned int i;
  690. u32 reg;
  691. /*
  692. * We should never come here because rt2x00lib is supposed
  693. * to catch this and send us the correct antenna explicitely.
  694. */
  695. BUG_ON(ant->rx == ANTENNA_SW_DIVERSITY ||
  696. ant->tx == ANTENNA_SW_DIVERSITY);
  697. if (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ) {
  698. sel = antenna_sel_a;
  699. lna = test_bit(CAPABILITY_EXTERNAL_LNA_A, &rt2x00dev->cap_flags);
  700. } else {
  701. sel = antenna_sel_bg;
  702. lna = test_bit(CAPABILITY_EXTERNAL_LNA_BG, &rt2x00dev->cap_flags);
  703. }
  704. for (i = 0; i < ARRAY_SIZE(antenna_sel_a); i++)
  705. rt61pci_bbp_write(rt2x00dev, sel[i].word, sel[i].value[lna]);
  706. rt2x00pci_register_read(rt2x00dev, PHY_CSR0, &reg);
  707. rt2x00_set_field32(&reg, PHY_CSR0_PA_PE_BG,
  708. rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
  709. rt2x00_set_field32(&reg, PHY_CSR0_PA_PE_A,
  710. rt2x00dev->curr_band == IEEE80211_BAND_5GHZ);
  711. rt2x00pci_register_write(rt2x00dev, PHY_CSR0, reg);
  712. if (rt2x00_rf(rt2x00dev, RF5225) || rt2x00_rf(rt2x00dev, RF5325))
  713. rt61pci_config_antenna_5x(rt2x00dev, ant);
  714. else if (rt2x00_rf(rt2x00dev, RF2527))
  715. rt61pci_config_antenna_2x(rt2x00dev, ant);
  716. else if (rt2x00_rf(rt2x00dev, RF2529)) {
  717. if (test_bit(CAPABILITY_DOUBLE_ANTENNA, &rt2x00dev->cap_flags))
  718. rt61pci_config_antenna_2x(rt2x00dev, ant);
  719. else
  720. rt61pci_config_antenna_2529(rt2x00dev, ant);
  721. }
  722. }
  723. static void rt61pci_config_lna_gain(struct rt2x00_dev *rt2x00dev,
  724. struct rt2x00lib_conf *libconf)
  725. {
  726. u16 eeprom;
  727. short lna_gain = 0;
  728. if (libconf->conf->channel->band == IEEE80211_BAND_2GHZ) {
  729. if (test_bit(CAPABILITY_EXTERNAL_LNA_BG, &rt2x00dev->cap_flags))
  730. lna_gain += 14;
  731. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_BG, &eeprom);
  732. lna_gain -= rt2x00_get_field16(eeprom, EEPROM_RSSI_OFFSET_BG_1);
  733. } else {
  734. if (test_bit(CAPABILITY_EXTERNAL_LNA_A, &rt2x00dev->cap_flags))
  735. lna_gain += 14;
  736. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_A, &eeprom);
  737. lna_gain -= rt2x00_get_field16(eeprom, EEPROM_RSSI_OFFSET_A_1);
  738. }
  739. rt2x00dev->lna_gain = lna_gain;
  740. }
  741. static void rt61pci_config_channel(struct rt2x00_dev *rt2x00dev,
  742. struct rf_channel *rf, const int txpower)
  743. {
  744. u8 r3;
  745. u8 r94;
  746. u8 smart;
  747. rt2x00_set_field32(&rf->rf3, RF3_TXPOWER, TXPOWER_TO_DEV(txpower));
  748. rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
  749. smart = !(rt2x00_rf(rt2x00dev, RF5225) || rt2x00_rf(rt2x00dev, RF2527));
  750. rt61pci_bbp_read(rt2x00dev, 3, &r3);
  751. rt2x00_set_field8(&r3, BBP_R3_SMART_MODE, smart);
  752. rt61pci_bbp_write(rt2x00dev, 3, r3);
  753. r94 = 6;
  754. if (txpower > MAX_TXPOWER && txpower <= (MAX_TXPOWER + r94))
  755. r94 += txpower - MAX_TXPOWER;
  756. else if (txpower < MIN_TXPOWER && txpower >= (MIN_TXPOWER - r94))
  757. r94 += txpower;
  758. rt61pci_bbp_write(rt2x00dev, 94, r94);
  759. rt61pci_rf_write(rt2x00dev, 1, rf->rf1);
  760. rt61pci_rf_write(rt2x00dev, 2, rf->rf2);
  761. rt61pci_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
  762. rt61pci_rf_write(rt2x00dev, 4, rf->rf4);
  763. udelay(200);
  764. rt61pci_rf_write(rt2x00dev, 1, rf->rf1);
  765. rt61pci_rf_write(rt2x00dev, 2, rf->rf2);
  766. rt61pci_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
  767. rt61pci_rf_write(rt2x00dev, 4, rf->rf4);
  768. udelay(200);
  769. rt61pci_rf_write(rt2x00dev, 1, rf->rf1);
  770. rt61pci_rf_write(rt2x00dev, 2, rf->rf2);
  771. rt61pci_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
  772. rt61pci_rf_write(rt2x00dev, 4, rf->rf4);
  773. msleep(1);
  774. }
  775. static void rt61pci_config_txpower(struct rt2x00_dev *rt2x00dev,
  776. const int txpower)
  777. {
  778. struct rf_channel rf;
  779. rt2x00_rf_read(rt2x00dev, 1, &rf.rf1);
  780. rt2x00_rf_read(rt2x00dev, 2, &rf.rf2);
  781. rt2x00_rf_read(rt2x00dev, 3, &rf.rf3);
  782. rt2x00_rf_read(rt2x00dev, 4, &rf.rf4);
  783. rt61pci_config_channel(rt2x00dev, &rf, txpower);
  784. }
  785. static void rt61pci_config_retry_limit(struct rt2x00_dev *rt2x00dev,
  786. struct rt2x00lib_conf *libconf)
  787. {
  788. u32 reg;
  789. rt2x00pci_register_read(rt2x00dev, TXRX_CSR4, &reg);
  790. rt2x00_set_field32(&reg, TXRX_CSR4_OFDM_TX_RATE_DOWN, 1);
  791. rt2x00_set_field32(&reg, TXRX_CSR4_OFDM_TX_RATE_STEP, 0);
  792. rt2x00_set_field32(&reg, TXRX_CSR4_OFDM_TX_FALLBACK_CCK, 0);
  793. rt2x00_set_field32(&reg, TXRX_CSR4_LONG_RETRY_LIMIT,
  794. libconf->conf->long_frame_max_tx_count);
  795. rt2x00_set_field32(&reg, TXRX_CSR4_SHORT_RETRY_LIMIT,
  796. libconf->conf->short_frame_max_tx_count);
  797. rt2x00pci_register_write(rt2x00dev, TXRX_CSR4, reg);
  798. }
  799. static void rt61pci_config_ps(struct rt2x00_dev *rt2x00dev,
  800. struct rt2x00lib_conf *libconf)
  801. {
  802. enum dev_state state =
  803. (libconf->conf->flags & IEEE80211_CONF_PS) ?
  804. STATE_SLEEP : STATE_AWAKE;
  805. u32 reg;
  806. if (state == STATE_SLEEP) {
  807. rt2x00pci_register_read(rt2x00dev, MAC_CSR11, &reg);
  808. rt2x00_set_field32(&reg, MAC_CSR11_DELAY_AFTER_TBCN,
  809. rt2x00dev->beacon_int - 10);
  810. rt2x00_set_field32(&reg, MAC_CSR11_TBCN_BEFORE_WAKEUP,
  811. libconf->conf->listen_interval - 1);
  812. rt2x00_set_field32(&reg, MAC_CSR11_WAKEUP_LATENCY, 5);
  813. /* We must first disable autowake before it can be enabled */
  814. rt2x00_set_field32(&reg, MAC_CSR11_AUTOWAKE, 0);
  815. rt2x00pci_register_write(rt2x00dev, MAC_CSR11, reg);
  816. rt2x00_set_field32(&reg, MAC_CSR11_AUTOWAKE, 1);
  817. rt2x00pci_register_write(rt2x00dev, MAC_CSR11, reg);
  818. rt2x00pci_register_write(rt2x00dev, SOFT_RESET_CSR, 0x00000005);
  819. rt2x00pci_register_write(rt2x00dev, IO_CNTL_CSR, 0x0000001c);
  820. rt2x00pci_register_write(rt2x00dev, PCI_USEC_CSR, 0x00000060);
  821. rt61pci_mcu_request(rt2x00dev, MCU_SLEEP, 0xff, 0, 0);
  822. } else {
  823. rt2x00pci_register_read(rt2x00dev, MAC_CSR11, &reg);
  824. rt2x00_set_field32(&reg, MAC_CSR11_DELAY_AFTER_TBCN, 0);
  825. rt2x00_set_field32(&reg, MAC_CSR11_TBCN_BEFORE_WAKEUP, 0);
  826. rt2x00_set_field32(&reg, MAC_CSR11_AUTOWAKE, 0);
  827. rt2x00_set_field32(&reg, MAC_CSR11_WAKEUP_LATENCY, 0);
  828. rt2x00pci_register_write(rt2x00dev, MAC_CSR11, reg);
  829. rt2x00pci_register_write(rt2x00dev, SOFT_RESET_CSR, 0x00000007);
  830. rt2x00pci_register_write(rt2x00dev, IO_CNTL_CSR, 0x00000018);
  831. rt2x00pci_register_write(rt2x00dev, PCI_USEC_CSR, 0x00000020);
  832. rt61pci_mcu_request(rt2x00dev, MCU_WAKEUP, 0xff, 0, 0);
  833. }
  834. }
  835. static void rt61pci_config(struct rt2x00_dev *rt2x00dev,
  836. struct rt2x00lib_conf *libconf,
  837. const unsigned int flags)
  838. {
  839. /* Always recalculate LNA gain before changing configuration */
  840. rt61pci_config_lna_gain(rt2x00dev, libconf);
  841. if (flags & IEEE80211_CONF_CHANGE_CHANNEL)
  842. rt61pci_config_channel(rt2x00dev, &libconf->rf,
  843. libconf->conf->power_level);
  844. if ((flags & IEEE80211_CONF_CHANGE_POWER) &&
  845. !(flags & IEEE80211_CONF_CHANGE_CHANNEL))
  846. rt61pci_config_txpower(rt2x00dev, libconf->conf->power_level);
  847. if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
  848. rt61pci_config_retry_limit(rt2x00dev, libconf);
  849. if (flags & IEEE80211_CONF_CHANGE_PS)
  850. rt61pci_config_ps(rt2x00dev, libconf);
  851. }
  852. /*
  853. * Link tuning
  854. */
  855. static void rt61pci_link_stats(struct rt2x00_dev *rt2x00dev,
  856. struct link_qual *qual)
  857. {
  858. u32 reg;
  859. /*
  860. * Update FCS error count from register.
  861. */
  862. rt2x00pci_register_read(rt2x00dev, STA_CSR0, &reg);
  863. qual->rx_failed = rt2x00_get_field32(reg, STA_CSR0_FCS_ERROR);
  864. /*
  865. * Update False CCA count from register.
  866. */
  867. rt2x00pci_register_read(rt2x00dev, STA_CSR1, &reg);
  868. qual->false_cca = rt2x00_get_field32(reg, STA_CSR1_FALSE_CCA_ERROR);
  869. }
  870. static inline void rt61pci_set_vgc(struct rt2x00_dev *rt2x00dev,
  871. struct link_qual *qual, u8 vgc_level)
  872. {
  873. if (qual->vgc_level != vgc_level) {
  874. rt61pci_bbp_write(rt2x00dev, 17, vgc_level);
  875. qual->vgc_level = vgc_level;
  876. qual->vgc_level_reg = vgc_level;
  877. }
  878. }
  879. static void rt61pci_reset_tuner(struct rt2x00_dev *rt2x00dev,
  880. struct link_qual *qual)
  881. {
  882. rt61pci_set_vgc(rt2x00dev, qual, 0x20);
  883. }
  884. static void rt61pci_link_tuner(struct rt2x00_dev *rt2x00dev,
  885. struct link_qual *qual, const u32 count)
  886. {
  887. u8 up_bound;
  888. u8 low_bound;
  889. /*
  890. * Determine r17 bounds.
  891. */
  892. if (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ) {
  893. low_bound = 0x28;
  894. up_bound = 0x48;
  895. if (test_bit(CAPABILITY_EXTERNAL_LNA_A, &rt2x00dev->cap_flags)) {
  896. low_bound += 0x10;
  897. up_bound += 0x10;
  898. }
  899. } else {
  900. low_bound = 0x20;
  901. up_bound = 0x40;
  902. if (test_bit(CAPABILITY_EXTERNAL_LNA_BG, &rt2x00dev->cap_flags)) {
  903. low_bound += 0x10;
  904. up_bound += 0x10;
  905. }
  906. }
  907. /*
  908. * If we are not associated, we should go straight to the
  909. * dynamic CCA tuning.
  910. */
  911. if (!rt2x00dev->intf_associated)
  912. goto dynamic_cca_tune;
  913. /*
  914. * Special big-R17 for very short distance
  915. */
  916. if (qual->rssi >= -35) {
  917. rt61pci_set_vgc(rt2x00dev, qual, 0x60);
  918. return;
  919. }
  920. /*
  921. * Special big-R17 for short distance
  922. */
  923. if (qual->rssi >= -58) {
  924. rt61pci_set_vgc(rt2x00dev, qual, up_bound);
  925. return;
  926. }
  927. /*
  928. * Special big-R17 for middle-short distance
  929. */
  930. if (qual->rssi >= -66) {
  931. rt61pci_set_vgc(rt2x00dev, qual, low_bound + 0x10);
  932. return;
  933. }
  934. /*
  935. * Special mid-R17 for middle distance
  936. */
  937. if (qual->rssi >= -74) {
  938. rt61pci_set_vgc(rt2x00dev, qual, low_bound + 0x08);
  939. return;
  940. }
  941. /*
  942. * Special case: Change up_bound based on the rssi.
  943. * Lower up_bound when rssi is weaker then -74 dBm.
  944. */
  945. up_bound -= 2 * (-74 - qual->rssi);
  946. if (low_bound > up_bound)
  947. up_bound = low_bound;
  948. if (qual->vgc_level > up_bound) {
  949. rt61pci_set_vgc(rt2x00dev, qual, up_bound);
  950. return;
  951. }
  952. dynamic_cca_tune:
  953. /*
  954. * r17 does not yet exceed upper limit, continue and base
  955. * the r17 tuning on the false CCA count.
  956. */
  957. if ((qual->false_cca > 512) && (qual->vgc_level < up_bound))
  958. rt61pci_set_vgc(rt2x00dev, qual, ++qual->vgc_level);
  959. else if ((qual->false_cca < 100) && (qual->vgc_level > low_bound))
  960. rt61pci_set_vgc(rt2x00dev, qual, --qual->vgc_level);
  961. }
  962. /*
  963. * Queue handlers.
  964. */
  965. static void rt61pci_start_queue(struct data_queue *queue)
  966. {
  967. struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
  968. u32 reg;
  969. switch (queue->qid) {
  970. case QID_RX:
  971. rt2x00pci_register_read(rt2x00dev, TXRX_CSR0, &reg);
  972. rt2x00_set_field32(&reg, TXRX_CSR0_DISABLE_RX, 0);
  973. rt2x00pci_register_write(rt2x00dev, TXRX_CSR0, reg);
  974. break;
  975. case QID_BEACON:
  976. rt2x00pci_register_read(rt2x00dev, TXRX_CSR9, &reg);
  977. rt2x00_set_field32(&reg, TXRX_CSR9_TSF_TICKING, 1);
  978. rt2x00_set_field32(&reg, TXRX_CSR9_TBTT_ENABLE, 1);
  979. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 1);
  980. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, reg);
  981. break;
  982. default:
  983. break;
  984. }
  985. }
  986. static void rt61pci_kick_queue(struct data_queue *queue)
  987. {
  988. struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
  989. u32 reg;
  990. switch (queue->qid) {
  991. case QID_AC_VO:
  992. rt2x00pci_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  993. rt2x00_set_field32(&reg, TX_CNTL_CSR_KICK_TX_AC0, 1);
  994. rt2x00pci_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  995. break;
  996. case QID_AC_VI:
  997. rt2x00pci_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  998. rt2x00_set_field32(&reg, TX_CNTL_CSR_KICK_TX_AC1, 1);
  999. rt2x00pci_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1000. break;
  1001. case QID_AC_BE:
  1002. rt2x00pci_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1003. rt2x00_set_field32(&reg, TX_CNTL_CSR_KICK_TX_AC2, 1);
  1004. rt2x00pci_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1005. break;
  1006. case QID_AC_BK:
  1007. rt2x00pci_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1008. rt2x00_set_field32(&reg, TX_CNTL_CSR_KICK_TX_AC3, 1);
  1009. rt2x00pci_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1010. break;
  1011. default:
  1012. break;
  1013. }
  1014. }
  1015. static void rt61pci_stop_queue(struct data_queue *queue)
  1016. {
  1017. struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
  1018. u32 reg;
  1019. switch (queue->qid) {
  1020. case QID_AC_VO:
  1021. rt2x00pci_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1022. rt2x00_set_field32(&reg, TX_CNTL_CSR_ABORT_TX_AC0, 1);
  1023. rt2x00pci_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1024. break;
  1025. case QID_AC_VI:
  1026. rt2x00pci_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1027. rt2x00_set_field32(&reg, TX_CNTL_CSR_ABORT_TX_AC1, 1);
  1028. rt2x00pci_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1029. break;
  1030. case QID_AC_BE:
  1031. rt2x00pci_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1032. rt2x00_set_field32(&reg, TX_CNTL_CSR_ABORT_TX_AC2, 1);
  1033. rt2x00pci_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1034. break;
  1035. case QID_AC_BK:
  1036. rt2x00pci_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1037. rt2x00_set_field32(&reg, TX_CNTL_CSR_ABORT_TX_AC3, 1);
  1038. rt2x00pci_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1039. break;
  1040. case QID_RX:
  1041. rt2x00pci_register_read(rt2x00dev, TXRX_CSR0, &reg);
  1042. rt2x00_set_field32(&reg, TXRX_CSR0_DISABLE_RX, 1);
  1043. rt2x00pci_register_write(rt2x00dev, TXRX_CSR0, reg);
  1044. break;
  1045. case QID_BEACON:
  1046. rt2x00pci_register_read(rt2x00dev, TXRX_CSR9, &reg);
  1047. rt2x00_set_field32(&reg, TXRX_CSR9_TSF_TICKING, 0);
  1048. rt2x00_set_field32(&reg, TXRX_CSR9_TBTT_ENABLE, 0);
  1049. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 0);
  1050. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, reg);
  1051. /*
  1052. * Wait for possibly running tbtt tasklets.
  1053. */
  1054. tasklet_kill(&rt2x00dev->tbtt_tasklet);
  1055. break;
  1056. default:
  1057. break;
  1058. }
  1059. }
  1060. /*
  1061. * Firmware functions
  1062. */
  1063. static char *rt61pci_get_firmware_name(struct rt2x00_dev *rt2x00dev)
  1064. {
  1065. u16 chip;
  1066. char *fw_name;
  1067. pci_read_config_word(to_pci_dev(rt2x00dev->dev), PCI_DEVICE_ID, &chip);
  1068. switch (chip) {
  1069. case RT2561_PCI_ID:
  1070. fw_name = FIRMWARE_RT2561;
  1071. break;
  1072. case RT2561s_PCI_ID:
  1073. fw_name = FIRMWARE_RT2561s;
  1074. break;
  1075. case RT2661_PCI_ID:
  1076. fw_name = FIRMWARE_RT2661;
  1077. break;
  1078. default:
  1079. fw_name = NULL;
  1080. break;
  1081. }
  1082. return fw_name;
  1083. }
  1084. static int rt61pci_check_firmware(struct rt2x00_dev *rt2x00dev,
  1085. const u8 *data, const size_t len)
  1086. {
  1087. u16 fw_crc;
  1088. u16 crc;
  1089. /*
  1090. * Only support 8kb firmware files.
  1091. */
  1092. if (len != 8192)
  1093. return FW_BAD_LENGTH;
  1094. /*
  1095. * The last 2 bytes in the firmware array are the crc checksum itself.
  1096. * This means that we should never pass those 2 bytes to the crc
  1097. * algorithm.
  1098. */
  1099. fw_crc = (data[len - 2] << 8 | data[len - 1]);
  1100. /*
  1101. * Use the crc itu-t algorithm.
  1102. */
  1103. crc = crc_itu_t(0, data, len - 2);
  1104. crc = crc_itu_t_byte(crc, 0);
  1105. crc = crc_itu_t_byte(crc, 0);
  1106. return (fw_crc == crc) ? FW_OK : FW_BAD_CRC;
  1107. }
  1108. static int rt61pci_load_firmware(struct rt2x00_dev *rt2x00dev,
  1109. const u8 *data, const size_t len)
  1110. {
  1111. int i;
  1112. u32 reg;
  1113. /*
  1114. * Wait for stable hardware.
  1115. */
  1116. for (i = 0; i < 100; i++) {
  1117. rt2x00pci_register_read(rt2x00dev, MAC_CSR0, &reg);
  1118. if (reg)
  1119. break;
  1120. msleep(1);
  1121. }
  1122. if (!reg) {
  1123. ERROR(rt2x00dev, "Unstable hardware.\n");
  1124. return -EBUSY;
  1125. }
  1126. /*
  1127. * Prepare MCU and mailbox for firmware loading.
  1128. */
  1129. reg = 0;
  1130. rt2x00_set_field32(&reg, MCU_CNTL_CSR_RESET, 1);
  1131. rt2x00pci_register_write(rt2x00dev, MCU_CNTL_CSR, reg);
  1132. rt2x00pci_register_write(rt2x00dev, M2H_CMD_DONE_CSR, 0xffffffff);
  1133. rt2x00pci_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
  1134. rt2x00pci_register_write(rt2x00dev, HOST_CMD_CSR, 0);
  1135. /*
  1136. * Write firmware to device.
  1137. */
  1138. reg = 0;
  1139. rt2x00_set_field32(&reg, MCU_CNTL_CSR_RESET, 1);
  1140. rt2x00_set_field32(&reg, MCU_CNTL_CSR_SELECT_BANK, 1);
  1141. rt2x00pci_register_write(rt2x00dev, MCU_CNTL_CSR, reg);
  1142. rt2x00pci_register_multiwrite(rt2x00dev, FIRMWARE_IMAGE_BASE,
  1143. data, len);
  1144. rt2x00_set_field32(&reg, MCU_CNTL_CSR_SELECT_BANK, 0);
  1145. rt2x00pci_register_write(rt2x00dev, MCU_CNTL_CSR, reg);
  1146. rt2x00_set_field32(&reg, MCU_CNTL_CSR_RESET, 0);
  1147. rt2x00pci_register_write(rt2x00dev, MCU_CNTL_CSR, reg);
  1148. for (i = 0; i < 100; i++) {
  1149. rt2x00pci_register_read(rt2x00dev, MCU_CNTL_CSR, &reg);
  1150. if (rt2x00_get_field32(reg, MCU_CNTL_CSR_READY))
  1151. break;
  1152. msleep(1);
  1153. }
  1154. if (i == 100) {
  1155. ERROR(rt2x00dev, "MCU Control register not ready.\n");
  1156. return -EBUSY;
  1157. }
  1158. /*
  1159. * Hardware needs another millisecond before it is ready.
  1160. */
  1161. msleep(1);
  1162. /*
  1163. * Reset MAC and BBP registers.
  1164. */
  1165. reg = 0;
  1166. rt2x00_set_field32(&reg, MAC_CSR1_SOFT_RESET, 1);
  1167. rt2x00_set_field32(&reg, MAC_CSR1_BBP_RESET, 1);
  1168. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  1169. rt2x00pci_register_read(rt2x00dev, MAC_CSR1, &reg);
  1170. rt2x00_set_field32(&reg, MAC_CSR1_SOFT_RESET, 0);
  1171. rt2x00_set_field32(&reg, MAC_CSR1_BBP_RESET, 0);
  1172. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  1173. rt2x00pci_register_read(rt2x00dev, MAC_CSR1, &reg);
  1174. rt2x00_set_field32(&reg, MAC_CSR1_HOST_READY, 1);
  1175. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  1176. return 0;
  1177. }
  1178. /*
  1179. * Initialization functions.
  1180. */
  1181. static bool rt61pci_get_entry_state(struct queue_entry *entry)
  1182. {
  1183. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  1184. u32 word;
  1185. if (entry->queue->qid == QID_RX) {
  1186. rt2x00_desc_read(entry_priv->desc, 0, &word);
  1187. return rt2x00_get_field32(word, RXD_W0_OWNER_NIC);
  1188. } else {
  1189. rt2x00_desc_read(entry_priv->desc, 0, &word);
  1190. return (rt2x00_get_field32(word, TXD_W0_OWNER_NIC) ||
  1191. rt2x00_get_field32(word, TXD_W0_VALID));
  1192. }
  1193. }
  1194. static void rt61pci_clear_entry(struct queue_entry *entry)
  1195. {
  1196. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  1197. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  1198. u32 word;
  1199. if (entry->queue->qid == QID_RX) {
  1200. rt2x00_desc_read(entry_priv->desc, 5, &word);
  1201. rt2x00_set_field32(&word, RXD_W5_BUFFER_PHYSICAL_ADDRESS,
  1202. skbdesc->skb_dma);
  1203. rt2x00_desc_write(entry_priv->desc, 5, word);
  1204. rt2x00_desc_read(entry_priv->desc, 0, &word);
  1205. rt2x00_set_field32(&word, RXD_W0_OWNER_NIC, 1);
  1206. rt2x00_desc_write(entry_priv->desc, 0, word);
  1207. } else {
  1208. rt2x00_desc_read(entry_priv->desc, 0, &word);
  1209. rt2x00_set_field32(&word, TXD_W0_VALID, 0);
  1210. rt2x00_set_field32(&word, TXD_W0_OWNER_NIC, 0);
  1211. rt2x00_desc_write(entry_priv->desc, 0, word);
  1212. }
  1213. }
  1214. static int rt61pci_init_queues(struct rt2x00_dev *rt2x00dev)
  1215. {
  1216. struct queue_entry_priv_pci *entry_priv;
  1217. u32 reg;
  1218. /*
  1219. * Initialize registers.
  1220. */
  1221. rt2x00pci_register_read(rt2x00dev, TX_RING_CSR0, &reg);
  1222. rt2x00_set_field32(&reg, TX_RING_CSR0_AC0_RING_SIZE,
  1223. rt2x00dev->tx[0].limit);
  1224. rt2x00_set_field32(&reg, TX_RING_CSR0_AC1_RING_SIZE,
  1225. rt2x00dev->tx[1].limit);
  1226. rt2x00_set_field32(&reg, TX_RING_CSR0_AC2_RING_SIZE,
  1227. rt2x00dev->tx[2].limit);
  1228. rt2x00_set_field32(&reg, TX_RING_CSR0_AC3_RING_SIZE,
  1229. rt2x00dev->tx[3].limit);
  1230. rt2x00pci_register_write(rt2x00dev, TX_RING_CSR0, reg);
  1231. rt2x00pci_register_read(rt2x00dev, TX_RING_CSR1, &reg);
  1232. rt2x00_set_field32(&reg, TX_RING_CSR1_TXD_SIZE,
  1233. rt2x00dev->tx[0].desc_size / 4);
  1234. rt2x00pci_register_write(rt2x00dev, TX_RING_CSR1, reg);
  1235. entry_priv = rt2x00dev->tx[0].entries[0].priv_data;
  1236. rt2x00pci_register_read(rt2x00dev, AC0_BASE_CSR, &reg);
  1237. rt2x00_set_field32(&reg, AC0_BASE_CSR_RING_REGISTER,
  1238. entry_priv->desc_dma);
  1239. rt2x00pci_register_write(rt2x00dev, AC0_BASE_CSR, reg);
  1240. entry_priv = rt2x00dev->tx[1].entries[0].priv_data;
  1241. rt2x00pci_register_read(rt2x00dev, AC1_BASE_CSR, &reg);
  1242. rt2x00_set_field32(&reg, AC1_BASE_CSR_RING_REGISTER,
  1243. entry_priv->desc_dma);
  1244. rt2x00pci_register_write(rt2x00dev, AC1_BASE_CSR, reg);
  1245. entry_priv = rt2x00dev->tx[2].entries[0].priv_data;
  1246. rt2x00pci_register_read(rt2x00dev, AC2_BASE_CSR, &reg);
  1247. rt2x00_set_field32(&reg, AC2_BASE_CSR_RING_REGISTER,
  1248. entry_priv->desc_dma);
  1249. rt2x00pci_register_write(rt2x00dev, AC2_BASE_CSR, reg);
  1250. entry_priv = rt2x00dev->tx[3].entries[0].priv_data;
  1251. rt2x00pci_register_read(rt2x00dev, AC3_BASE_CSR, &reg);
  1252. rt2x00_set_field32(&reg, AC3_BASE_CSR_RING_REGISTER,
  1253. entry_priv->desc_dma);
  1254. rt2x00pci_register_write(rt2x00dev, AC3_BASE_CSR, reg);
  1255. rt2x00pci_register_read(rt2x00dev, RX_RING_CSR, &reg);
  1256. rt2x00_set_field32(&reg, RX_RING_CSR_RING_SIZE, rt2x00dev->rx->limit);
  1257. rt2x00_set_field32(&reg, RX_RING_CSR_RXD_SIZE,
  1258. rt2x00dev->rx->desc_size / 4);
  1259. rt2x00_set_field32(&reg, RX_RING_CSR_RXD_WRITEBACK_SIZE, 4);
  1260. rt2x00pci_register_write(rt2x00dev, RX_RING_CSR, reg);
  1261. entry_priv = rt2x00dev->rx->entries[0].priv_data;
  1262. rt2x00pci_register_read(rt2x00dev, RX_BASE_CSR, &reg);
  1263. rt2x00_set_field32(&reg, RX_BASE_CSR_RING_REGISTER,
  1264. entry_priv->desc_dma);
  1265. rt2x00pci_register_write(rt2x00dev, RX_BASE_CSR, reg);
  1266. rt2x00pci_register_read(rt2x00dev, TX_DMA_DST_CSR, &reg);
  1267. rt2x00_set_field32(&reg, TX_DMA_DST_CSR_DEST_AC0, 2);
  1268. rt2x00_set_field32(&reg, TX_DMA_DST_CSR_DEST_AC1, 2);
  1269. rt2x00_set_field32(&reg, TX_DMA_DST_CSR_DEST_AC2, 2);
  1270. rt2x00_set_field32(&reg, TX_DMA_DST_CSR_DEST_AC3, 2);
  1271. rt2x00pci_register_write(rt2x00dev, TX_DMA_DST_CSR, reg);
  1272. rt2x00pci_register_read(rt2x00dev, LOAD_TX_RING_CSR, &reg);
  1273. rt2x00_set_field32(&reg, LOAD_TX_RING_CSR_LOAD_TXD_AC0, 1);
  1274. rt2x00_set_field32(&reg, LOAD_TX_RING_CSR_LOAD_TXD_AC1, 1);
  1275. rt2x00_set_field32(&reg, LOAD_TX_RING_CSR_LOAD_TXD_AC2, 1);
  1276. rt2x00_set_field32(&reg, LOAD_TX_RING_CSR_LOAD_TXD_AC3, 1);
  1277. rt2x00pci_register_write(rt2x00dev, LOAD_TX_RING_CSR, reg);
  1278. rt2x00pci_register_read(rt2x00dev, RX_CNTL_CSR, &reg);
  1279. rt2x00_set_field32(&reg, RX_CNTL_CSR_LOAD_RXD, 1);
  1280. rt2x00pci_register_write(rt2x00dev, RX_CNTL_CSR, reg);
  1281. return 0;
  1282. }
  1283. static int rt61pci_init_registers(struct rt2x00_dev *rt2x00dev)
  1284. {
  1285. u32 reg;
  1286. rt2x00pci_register_read(rt2x00dev, TXRX_CSR0, &reg);
  1287. rt2x00_set_field32(&reg, TXRX_CSR0_AUTO_TX_SEQ, 1);
  1288. rt2x00_set_field32(&reg, TXRX_CSR0_DISABLE_RX, 0);
  1289. rt2x00_set_field32(&reg, TXRX_CSR0_TX_WITHOUT_WAITING, 0);
  1290. rt2x00pci_register_write(rt2x00dev, TXRX_CSR0, reg);
  1291. rt2x00pci_register_read(rt2x00dev, TXRX_CSR1, &reg);
  1292. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID0, 47); /* CCK Signal */
  1293. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID0_VALID, 1);
  1294. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID1, 30); /* Rssi */
  1295. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID1_VALID, 1);
  1296. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID2, 42); /* OFDM Rate */
  1297. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID2_VALID, 1);
  1298. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID3, 30); /* Rssi */
  1299. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID3_VALID, 1);
  1300. rt2x00pci_register_write(rt2x00dev, TXRX_CSR1, reg);
  1301. /*
  1302. * CCK TXD BBP registers
  1303. */
  1304. rt2x00pci_register_read(rt2x00dev, TXRX_CSR2, &reg);
  1305. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID0, 13);
  1306. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID0_VALID, 1);
  1307. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID1, 12);
  1308. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID1_VALID, 1);
  1309. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID2, 11);
  1310. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID2_VALID, 1);
  1311. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID3, 10);
  1312. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID3_VALID, 1);
  1313. rt2x00pci_register_write(rt2x00dev, TXRX_CSR2, reg);
  1314. /*
  1315. * OFDM TXD BBP registers
  1316. */
  1317. rt2x00pci_register_read(rt2x00dev, TXRX_CSR3, &reg);
  1318. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID0, 7);
  1319. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID0_VALID, 1);
  1320. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID1, 6);
  1321. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID1_VALID, 1);
  1322. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID2, 5);
  1323. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID2_VALID, 1);
  1324. rt2x00pci_register_write(rt2x00dev, TXRX_CSR3, reg);
  1325. rt2x00pci_register_read(rt2x00dev, TXRX_CSR7, &reg);
  1326. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_6MBS, 59);
  1327. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_9MBS, 53);
  1328. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_12MBS, 49);
  1329. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_18MBS, 46);
  1330. rt2x00pci_register_write(rt2x00dev, TXRX_CSR7, reg);
  1331. rt2x00pci_register_read(rt2x00dev, TXRX_CSR8, &reg);
  1332. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_24MBS, 44);
  1333. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_36MBS, 42);
  1334. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_48MBS, 42);
  1335. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_54MBS, 42);
  1336. rt2x00pci_register_write(rt2x00dev, TXRX_CSR8, reg);
  1337. rt2x00pci_register_read(rt2x00dev, TXRX_CSR9, &reg);
  1338. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_INTERVAL, 0);
  1339. rt2x00_set_field32(&reg, TXRX_CSR9_TSF_TICKING, 0);
  1340. rt2x00_set_field32(&reg, TXRX_CSR9_TSF_SYNC, 0);
  1341. rt2x00_set_field32(&reg, TXRX_CSR9_TBTT_ENABLE, 0);
  1342. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 0);
  1343. rt2x00_set_field32(&reg, TXRX_CSR9_TIMESTAMP_COMPENSATE, 0);
  1344. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, reg);
  1345. rt2x00pci_register_write(rt2x00dev, TXRX_CSR15, 0x0000000f);
  1346. rt2x00pci_register_write(rt2x00dev, MAC_CSR6, 0x00000fff);
  1347. rt2x00pci_register_read(rt2x00dev, MAC_CSR9, &reg);
  1348. rt2x00_set_field32(&reg, MAC_CSR9_CW_SELECT, 0);
  1349. rt2x00pci_register_write(rt2x00dev, MAC_CSR9, reg);
  1350. rt2x00pci_register_write(rt2x00dev, MAC_CSR10, 0x0000071c);
  1351. if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_AWAKE))
  1352. return -EBUSY;
  1353. rt2x00pci_register_write(rt2x00dev, MAC_CSR13, 0x0000e000);
  1354. /*
  1355. * Invalidate all Shared Keys (SEC_CSR0),
  1356. * and clear the Shared key Cipher algorithms (SEC_CSR1 & SEC_CSR5)
  1357. */
  1358. rt2x00pci_register_write(rt2x00dev, SEC_CSR0, 0x00000000);
  1359. rt2x00pci_register_write(rt2x00dev, SEC_CSR1, 0x00000000);
  1360. rt2x00pci_register_write(rt2x00dev, SEC_CSR5, 0x00000000);
  1361. rt2x00pci_register_write(rt2x00dev, PHY_CSR1, 0x000023b0);
  1362. rt2x00pci_register_write(rt2x00dev, PHY_CSR5, 0x060a100c);
  1363. rt2x00pci_register_write(rt2x00dev, PHY_CSR6, 0x00080606);
  1364. rt2x00pci_register_write(rt2x00dev, PHY_CSR7, 0x00000a08);
  1365. rt2x00pci_register_write(rt2x00dev, PCI_CFG_CSR, 0x28ca4404);
  1366. rt2x00pci_register_write(rt2x00dev, TEST_MODE_CSR, 0x00000200);
  1367. rt2x00pci_register_write(rt2x00dev, M2H_CMD_DONE_CSR, 0xffffffff);
  1368. /*
  1369. * Clear all beacons
  1370. * For the Beacon base registers we only need to clear
  1371. * the first byte since that byte contains the VALID and OWNER
  1372. * bits which (when set to 0) will invalidate the entire beacon.
  1373. */
  1374. rt2x00pci_register_write(rt2x00dev, HW_BEACON_BASE0, 0);
  1375. rt2x00pci_register_write(rt2x00dev, HW_BEACON_BASE1, 0);
  1376. rt2x00pci_register_write(rt2x00dev, HW_BEACON_BASE2, 0);
  1377. rt2x00pci_register_write(rt2x00dev, HW_BEACON_BASE3, 0);
  1378. /*
  1379. * We must clear the error counters.
  1380. * These registers are cleared on read,
  1381. * so we may pass a useless variable to store the value.
  1382. */
  1383. rt2x00pci_register_read(rt2x00dev, STA_CSR0, &reg);
  1384. rt2x00pci_register_read(rt2x00dev, STA_CSR1, &reg);
  1385. rt2x00pci_register_read(rt2x00dev, STA_CSR2, &reg);
  1386. /*
  1387. * Reset MAC and BBP registers.
  1388. */
  1389. rt2x00pci_register_read(rt2x00dev, MAC_CSR1, &reg);
  1390. rt2x00_set_field32(&reg, MAC_CSR1_SOFT_RESET, 1);
  1391. rt2x00_set_field32(&reg, MAC_CSR1_BBP_RESET, 1);
  1392. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  1393. rt2x00pci_register_read(rt2x00dev, MAC_CSR1, &reg);
  1394. rt2x00_set_field32(&reg, MAC_CSR1_SOFT_RESET, 0);
  1395. rt2x00_set_field32(&reg, MAC_CSR1_BBP_RESET, 0);
  1396. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  1397. rt2x00pci_register_read(rt2x00dev, MAC_CSR1, &reg);
  1398. rt2x00_set_field32(&reg, MAC_CSR1_HOST_READY, 1);
  1399. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  1400. return 0;
  1401. }
  1402. static int rt61pci_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
  1403. {
  1404. unsigned int i;
  1405. u8 value;
  1406. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  1407. rt61pci_bbp_read(rt2x00dev, 0, &value);
  1408. if ((value != 0xff) && (value != 0x00))
  1409. return 0;
  1410. udelay(REGISTER_BUSY_DELAY);
  1411. }
  1412. ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
  1413. return -EACCES;
  1414. }
  1415. static int rt61pci_init_bbp(struct rt2x00_dev *rt2x00dev)
  1416. {
  1417. unsigned int i;
  1418. u16 eeprom;
  1419. u8 reg_id;
  1420. u8 value;
  1421. if (unlikely(rt61pci_wait_bbp_ready(rt2x00dev)))
  1422. return -EACCES;
  1423. rt61pci_bbp_write(rt2x00dev, 3, 0x00);
  1424. rt61pci_bbp_write(rt2x00dev, 15, 0x30);
  1425. rt61pci_bbp_write(rt2x00dev, 21, 0xc8);
  1426. rt61pci_bbp_write(rt2x00dev, 22, 0x38);
  1427. rt61pci_bbp_write(rt2x00dev, 23, 0x06);
  1428. rt61pci_bbp_write(rt2x00dev, 24, 0xfe);
  1429. rt61pci_bbp_write(rt2x00dev, 25, 0x0a);
  1430. rt61pci_bbp_write(rt2x00dev, 26, 0x0d);
  1431. rt61pci_bbp_write(rt2x00dev, 34, 0x12);
  1432. rt61pci_bbp_write(rt2x00dev, 37, 0x07);
  1433. rt61pci_bbp_write(rt2x00dev, 39, 0xf8);
  1434. rt61pci_bbp_write(rt2x00dev, 41, 0x60);
  1435. rt61pci_bbp_write(rt2x00dev, 53, 0x10);
  1436. rt61pci_bbp_write(rt2x00dev, 54, 0x18);
  1437. rt61pci_bbp_write(rt2x00dev, 60, 0x10);
  1438. rt61pci_bbp_write(rt2x00dev, 61, 0x04);
  1439. rt61pci_bbp_write(rt2x00dev, 62, 0x04);
  1440. rt61pci_bbp_write(rt2x00dev, 75, 0xfe);
  1441. rt61pci_bbp_write(rt2x00dev, 86, 0xfe);
  1442. rt61pci_bbp_write(rt2x00dev, 88, 0xfe);
  1443. rt61pci_bbp_write(rt2x00dev, 90, 0x0f);
  1444. rt61pci_bbp_write(rt2x00dev, 99, 0x00);
  1445. rt61pci_bbp_write(rt2x00dev, 102, 0x16);
  1446. rt61pci_bbp_write(rt2x00dev, 107, 0x04);
  1447. for (i = 0; i < EEPROM_BBP_SIZE; i++) {
  1448. rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
  1449. if (eeprom != 0xffff && eeprom != 0x0000) {
  1450. reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
  1451. value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
  1452. rt61pci_bbp_write(rt2x00dev, reg_id, value);
  1453. }
  1454. }
  1455. return 0;
  1456. }
  1457. /*
  1458. * Device state switch handlers.
  1459. */
  1460. static void rt61pci_toggle_irq(struct rt2x00_dev *rt2x00dev,
  1461. enum dev_state state)
  1462. {
  1463. int mask = (state == STATE_RADIO_IRQ_OFF);
  1464. u32 reg;
  1465. unsigned long flags;
  1466. /*
  1467. * When interrupts are being enabled, the interrupt registers
  1468. * should clear the register to assure a clean state.
  1469. */
  1470. if (state == STATE_RADIO_IRQ_ON) {
  1471. rt2x00pci_register_read(rt2x00dev, INT_SOURCE_CSR, &reg);
  1472. rt2x00pci_register_write(rt2x00dev, INT_SOURCE_CSR, reg);
  1473. rt2x00pci_register_read(rt2x00dev, MCU_INT_SOURCE_CSR, &reg);
  1474. rt2x00pci_register_write(rt2x00dev, MCU_INT_SOURCE_CSR, reg);
  1475. }
  1476. /*
  1477. * Only toggle the interrupts bits we are going to use.
  1478. * Non-checked interrupt bits are disabled by default.
  1479. */
  1480. spin_lock_irqsave(&rt2x00dev->irqmask_lock, flags);
  1481. rt2x00pci_register_read(rt2x00dev, INT_MASK_CSR, &reg);
  1482. rt2x00_set_field32(&reg, INT_MASK_CSR_TXDONE, mask);
  1483. rt2x00_set_field32(&reg, INT_MASK_CSR_RXDONE, mask);
  1484. rt2x00_set_field32(&reg, INT_MASK_CSR_BEACON_DONE, mask);
  1485. rt2x00_set_field32(&reg, INT_MASK_CSR_ENABLE_MITIGATION, mask);
  1486. rt2x00_set_field32(&reg, INT_MASK_CSR_MITIGATION_PERIOD, 0xff);
  1487. rt2x00pci_register_write(rt2x00dev, INT_MASK_CSR, reg);
  1488. rt2x00pci_register_read(rt2x00dev, MCU_INT_MASK_CSR, &reg);
  1489. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_0, mask);
  1490. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_1, mask);
  1491. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_2, mask);
  1492. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_3, mask);
  1493. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_4, mask);
  1494. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_5, mask);
  1495. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_6, mask);
  1496. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_7, mask);
  1497. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_TWAKEUP, mask);
  1498. rt2x00pci_register_write(rt2x00dev, MCU_INT_MASK_CSR, reg);
  1499. spin_unlock_irqrestore(&rt2x00dev->irqmask_lock, flags);
  1500. if (state == STATE_RADIO_IRQ_OFF) {
  1501. /*
  1502. * Ensure that all tasklets are finished.
  1503. */
  1504. tasklet_kill(&rt2x00dev->txstatus_tasklet);
  1505. tasklet_kill(&rt2x00dev->rxdone_tasklet);
  1506. tasklet_kill(&rt2x00dev->autowake_tasklet);
  1507. tasklet_kill(&rt2x00dev->tbtt_tasklet);
  1508. }
  1509. }
  1510. static int rt61pci_enable_radio(struct rt2x00_dev *rt2x00dev)
  1511. {
  1512. u32 reg;
  1513. /*
  1514. * Initialize all registers.
  1515. */
  1516. if (unlikely(rt61pci_init_queues(rt2x00dev) ||
  1517. rt61pci_init_registers(rt2x00dev) ||
  1518. rt61pci_init_bbp(rt2x00dev)))
  1519. return -EIO;
  1520. /*
  1521. * Enable RX.
  1522. */
  1523. rt2x00pci_register_read(rt2x00dev, RX_CNTL_CSR, &reg);
  1524. rt2x00_set_field32(&reg, RX_CNTL_CSR_ENABLE_RX_DMA, 1);
  1525. rt2x00pci_register_write(rt2x00dev, RX_CNTL_CSR, reg);
  1526. return 0;
  1527. }
  1528. static void rt61pci_disable_radio(struct rt2x00_dev *rt2x00dev)
  1529. {
  1530. /*
  1531. * Disable power
  1532. */
  1533. rt2x00pci_register_write(rt2x00dev, MAC_CSR10, 0x00001818);
  1534. }
  1535. static int rt61pci_set_state(struct rt2x00_dev *rt2x00dev, enum dev_state state)
  1536. {
  1537. u32 reg, reg2;
  1538. unsigned int i;
  1539. char put_to_sleep;
  1540. put_to_sleep = (state != STATE_AWAKE);
  1541. rt2x00pci_register_read(rt2x00dev, MAC_CSR12, &reg);
  1542. rt2x00_set_field32(&reg, MAC_CSR12_FORCE_WAKEUP, !put_to_sleep);
  1543. rt2x00_set_field32(&reg, MAC_CSR12_PUT_TO_SLEEP, put_to_sleep);
  1544. rt2x00pci_register_write(rt2x00dev, MAC_CSR12, reg);
  1545. /*
  1546. * Device is not guaranteed to be in the requested state yet.
  1547. * We must wait until the register indicates that the
  1548. * device has entered the correct state.
  1549. */
  1550. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  1551. rt2x00pci_register_read(rt2x00dev, MAC_CSR12, &reg2);
  1552. state = rt2x00_get_field32(reg2, MAC_CSR12_BBP_CURRENT_STATE);
  1553. if (state == !put_to_sleep)
  1554. return 0;
  1555. rt2x00pci_register_write(rt2x00dev, MAC_CSR12, reg);
  1556. msleep(10);
  1557. }
  1558. return -EBUSY;
  1559. }
  1560. static int rt61pci_set_device_state(struct rt2x00_dev *rt2x00dev,
  1561. enum dev_state state)
  1562. {
  1563. int retval = 0;
  1564. switch (state) {
  1565. case STATE_RADIO_ON:
  1566. retval = rt61pci_enable_radio(rt2x00dev);
  1567. break;
  1568. case STATE_RADIO_OFF:
  1569. rt61pci_disable_radio(rt2x00dev);
  1570. break;
  1571. case STATE_RADIO_IRQ_ON:
  1572. case STATE_RADIO_IRQ_OFF:
  1573. rt61pci_toggle_irq(rt2x00dev, state);
  1574. break;
  1575. case STATE_DEEP_SLEEP:
  1576. case STATE_SLEEP:
  1577. case STATE_STANDBY:
  1578. case STATE_AWAKE:
  1579. retval = rt61pci_set_state(rt2x00dev, state);
  1580. break;
  1581. default:
  1582. retval = -ENOTSUPP;
  1583. break;
  1584. }
  1585. if (unlikely(retval))
  1586. ERROR(rt2x00dev, "Device failed to enter state %d (%d).\n",
  1587. state, retval);
  1588. return retval;
  1589. }
  1590. /*
  1591. * TX descriptor initialization
  1592. */
  1593. static void rt61pci_write_tx_desc(struct queue_entry *entry,
  1594. struct txentry_desc *txdesc)
  1595. {
  1596. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  1597. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  1598. __le32 *txd = entry_priv->desc;
  1599. u32 word;
  1600. /*
  1601. * Start writing the descriptor words.
  1602. */
  1603. rt2x00_desc_read(txd, 1, &word);
  1604. rt2x00_set_field32(&word, TXD_W1_HOST_Q_ID, entry->queue->qid);
  1605. rt2x00_set_field32(&word, TXD_W1_AIFSN, entry->queue->aifs);
  1606. rt2x00_set_field32(&word, TXD_W1_CWMIN, entry->queue->cw_min);
  1607. rt2x00_set_field32(&word, TXD_W1_CWMAX, entry->queue->cw_max);
  1608. rt2x00_set_field32(&word, TXD_W1_IV_OFFSET, txdesc->iv_offset);
  1609. rt2x00_set_field32(&word, TXD_W1_HW_SEQUENCE,
  1610. test_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags));
  1611. rt2x00_set_field32(&word, TXD_W1_BUFFER_COUNT, 1);
  1612. rt2x00_desc_write(txd, 1, word);
  1613. rt2x00_desc_read(txd, 2, &word);
  1614. rt2x00_set_field32(&word, TXD_W2_PLCP_SIGNAL, txdesc->u.plcp.signal);
  1615. rt2x00_set_field32(&word, TXD_W2_PLCP_SERVICE, txdesc->u.plcp.service);
  1616. rt2x00_set_field32(&word, TXD_W2_PLCP_LENGTH_LOW,
  1617. txdesc->u.plcp.length_low);
  1618. rt2x00_set_field32(&word, TXD_W2_PLCP_LENGTH_HIGH,
  1619. txdesc->u.plcp.length_high);
  1620. rt2x00_desc_write(txd, 2, word);
  1621. if (test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags)) {
  1622. _rt2x00_desc_write(txd, 3, skbdesc->iv[0]);
  1623. _rt2x00_desc_write(txd, 4, skbdesc->iv[1]);
  1624. }
  1625. rt2x00_desc_read(txd, 5, &word);
  1626. rt2x00_set_field32(&word, TXD_W5_PID_TYPE, entry->queue->qid);
  1627. rt2x00_set_field32(&word, TXD_W5_PID_SUBTYPE,
  1628. skbdesc->entry->entry_idx);
  1629. rt2x00_set_field32(&word, TXD_W5_TX_POWER,
  1630. TXPOWER_TO_DEV(entry->queue->rt2x00dev->tx_power));
  1631. rt2x00_set_field32(&word, TXD_W5_WAITING_DMA_DONE_INT, 1);
  1632. rt2x00_desc_write(txd, 5, word);
  1633. if (entry->queue->qid != QID_BEACON) {
  1634. rt2x00_desc_read(txd, 6, &word);
  1635. rt2x00_set_field32(&word, TXD_W6_BUFFER_PHYSICAL_ADDRESS,
  1636. skbdesc->skb_dma);
  1637. rt2x00_desc_write(txd, 6, word);
  1638. rt2x00_desc_read(txd, 11, &word);
  1639. rt2x00_set_field32(&word, TXD_W11_BUFFER_LENGTH0,
  1640. txdesc->length);
  1641. rt2x00_desc_write(txd, 11, word);
  1642. }
  1643. /*
  1644. * Writing TXD word 0 must the last to prevent a race condition with
  1645. * the device, whereby the device may take hold of the TXD before we
  1646. * finished updating it.
  1647. */
  1648. rt2x00_desc_read(txd, 0, &word);
  1649. rt2x00_set_field32(&word, TXD_W0_OWNER_NIC, 1);
  1650. rt2x00_set_field32(&word, TXD_W0_VALID, 1);
  1651. rt2x00_set_field32(&word, TXD_W0_MORE_FRAG,
  1652. test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
  1653. rt2x00_set_field32(&word, TXD_W0_ACK,
  1654. test_bit(ENTRY_TXD_ACK, &txdesc->flags));
  1655. rt2x00_set_field32(&word, TXD_W0_TIMESTAMP,
  1656. test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
  1657. rt2x00_set_field32(&word, TXD_W0_OFDM,
  1658. (txdesc->rate_mode == RATE_MODE_OFDM));
  1659. rt2x00_set_field32(&word, TXD_W0_IFS, txdesc->u.plcp.ifs);
  1660. rt2x00_set_field32(&word, TXD_W0_RETRY_MODE,
  1661. test_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags));
  1662. rt2x00_set_field32(&word, TXD_W0_TKIP_MIC,
  1663. test_bit(ENTRY_TXD_ENCRYPT_MMIC, &txdesc->flags));
  1664. rt2x00_set_field32(&word, TXD_W0_KEY_TABLE,
  1665. test_bit(ENTRY_TXD_ENCRYPT_PAIRWISE, &txdesc->flags));
  1666. rt2x00_set_field32(&word, TXD_W0_KEY_INDEX, txdesc->key_idx);
  1667. rt2x00_set_field32(&word, TXD_W0_DATABYTE_COUNT, txdesc->length);
  1668. rt2x00_set_field32(&word, TXD_W0_BURST,
  1669. test_bit(ENTRY_TXD_BURST, &txdesc->flags));
  1670. rt2x00_set_field32(&word, TXD_W0_CIPHER_ALG, txdesc->cipher);
  1671. rt2x00_desc_write(txd, 0, word);
  1672. /*
  1673. * Register descriptor details in skb frame descriptor.
  1674. */
  1675. skbdesc->desc = txd;
  1676. skbdesc->desc_len = (entry->queue->qid == QID_BEACON) ? TXINFO_SIZE :
  1677. TXD_DESC_SIZE;
  1678. }
  1679. /*
  1680. * TX data initialization
  1681. */
  1682. static void rt61pci_write_beacon(struct queue_entry *entry,
  1683. struct txentry_desc *txdesc)
  1684. {
  1685. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  1686. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  1687. unsigned int beacon_base;
  1688. unsigned int padding_len;
  1689. u32 orig_reg, reg;
  1690. /*
  1691. * Disable beaconing while we are reloading the beacon data,
  1692. * otherwise we might be sending out invalid data.
  1693. */
  1694. rt2x00pci_register_read(rt2x00dev, TXRX_CSR9, &reg);
  1695. orig_reg = reg;
  1696. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 0);
  1697. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, reg);
  1698. /*
  1699. * Write the TX descriptor for the beacon.
  1700. */
  1701. rt61pci_write_tx_desc(entry, txdesc);
  1702. /*
  1703. * Dump beacon to userspace through debugfs.
  1704. */
  1705. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_BEACON, entry->skb);
  1706. /*
  1707. * Write entire beacon with descriptor and padding to register.
  1708. */
  1709. padding_len = roundup(entry->skb->len, 4) - entry->skb->len;
  1710. if (padding_len && skb_pad(entry->skb, padding_len)) {
  1711. ERROR(rt2x00dev, "Failure padding beacon, aborting\n");
  1712. /* skb freed by skb_pad() on failure */
  1713. entry->skb = NULL;
  1714. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, orig_reg);
  1715. return;
  1716. }
  1717. beacon_base = HW_BEACON_OFFSET(entry->entry_idx);
  1718. rt2x00pci_register_multiwrite(rt2x00dev, beacon_base,
  1719. entry_priv->desc, TXINFO_SIZE);
  1720. rt2x00pci_register_multiwrite(rt2x00dev, beacon_base + TXINFO_SIZE,
  1721. entry->skb->data,
  1722. entry->skb->len + padding_len);
  1723. /*
  1724. * Enable beaconing again.
  1725. *
  1726. * For Wi-Fi faily generated beacons between participating
  1727. * stations. Set TBTT phase adaptive adjustment step to 8us.
  1728. */
  1729. rt2x00pci_register_write(rt2x00dev, TXRX_CSR10, 0x00001008);
  1730. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 1);
  1731. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, reg);
  1732. /*
  1733. * Clean up beacon skb.
  1734. */
  1735. dev_kfree_skb_any(entry->skb);
  1736. entry->skb = NULL;
  1737. }
  1738. static void rt61pci_clear_beacon(struct queue_entry *entry)
  1739. {
  1740. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  1741. u32 reg;
  1742. /*
  1743. * Disable beaconing while we are reloading the beacon data,
  1744. * otherwise we might be sending out invalid data.
  1745. */
  1746. rt2x00pci_register_read(rt2x00dev, TXRX_CSR9, &reg);
  1747. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 0);
  1748. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, reg);
  1749. /*
  1750. * Clear beacon.
  1751. */
  1752. rt2x00pci_register_write(rt2x00dev,
  1753. HW_BEACON_OFFSET(entry->entry_idx), 0);
  1754. /*
  1755. * Enable beaconing again.
  1756. */
  1757. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 1);
  1758. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, reg);
  1759. }
  1760. /*
  1761. * RX control handlers
  1762. */
  1763. static int rt61pci_agc_to_rssi(struct rt2x00_dev *rt2x00dev, int rxd_w1)
  1764. {
  1765. u8 offset = rt2x00dev->lna_gain;
  1766. u8 lna;
  1767. lna = rt2x00_get_field32(rxd_w1, RXD_W1_RSSI_LNA);
  1768. switch (lna) {
  1769. case 3:
  1770. offset += 90;
  1771. break;
  1772. case 2:
  1773. offset += 74;
  1774. break;
  1775. case 1:
  1776. offset += 64;
  1777. break;
  1778. default:
  1779. return 0;
  1780. }
  1781. if (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ) {
  1782. if (lna == 3 || lna == 2)
  1783. offset += 10;
  1784. }
  1785. return rt2x00_get_field32(rxd_w1, RXD_W1_RSSI_AGC) * 2 - offset;
  1786. }
  1787. static void rt61pci_fill_rxdone(struct queue_entry *entry,
  1788. struct rxdone_entry_desc *rxdesc)
  1789. {
  1790. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  1791. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  1792. u32 word0;
  1793. u32 word1;
  1794. rt2x00_desc_read(entry_priv->desc, 0, &word0);
  1795. rt2x00_desc_read(entry_priv->desc, 1, &word1);
  1796. if (rt2x00_get_field32(word0, RXD_W0_CRC_ERROR))
  1797. rxdesc->flags |= RX_FLAG_FAILED_FCS_CRC;
  1798. rxdesc->cipher = rt2x00_get_field32(word0, RXD_W0_CIPHER_ALG);
  1799. rxdesc->cipher_status = rt2x00_get_field32(word0, RXD_W0_CIPHER_ERROR);
  1800. if (rxdesc->cipher != CIPHER_NONE) {
  1801. _rt2x00_desc_read(entry_priv->desc, 2, &rxdesc->iv[0]);
  1802. _rt2x00_desc_read(entry_priv->desc, 3, &rxdesc->iv[1]);
  1803. rxdesc->dev_flags |= RXDONE_CRYPTO_IV;
  1804. _rt2x00_desc_read(entry_priv->desc, 4, &rxdesc->icv);
  1805. rxdesc->dev_flags |= RXDONE_CRYPTO_ICV;
  1806. /*
  1807. * Hardware has stripped IV/EIV data from 802.11 frame during
  1808. * decryption. It has provided the data separately but rt2x00lib
  1809. * should decide if it should be reinserted.
  1810. */
  1811. rxdesc->flags |= RX_FLAG_IV_STRIPPED;
  1812. /*
  1813. * The hardware has already checked the Michael Mic and has
  1814. * stripped it from the frame. Signal this to mac80211.
  1815. */
  1816. rxdesc->flags |= RX_FLAG_MMIC_STRIPPED;
  1817. if (rxdesc->cipher_status == RX_CRYPTO_SUCCESS)
  1818. rxdesc->flags |= RX_FLAG_DECRYPTED;
  1819. else if (rxdesc->cipher_status == RX_CRYPTO_FAIL_MIC)
  1820. rxdesc->flags |= RX_FLAG_MMIC_ERROR;
  1821. }
  1822. /*
  1823. * Obtain the status about this packet.
  1824. * When frame was received with an OFDM bitrate,
  1825. * the signal is the PLCP value. If it was received with
  1826. * a CCK bitrate the signal is the rate in 100kbit/s.
  1827. */
  1828. rxdesc->signal = rt2x00_get_field32(word1, RXD_W1_SIGNAL);
  1829. rxdesc->rssi = rt61pci_agc_to_rssi(rt2x00dev, word1);
  1830. rxdesc->size = rt2x00_get_field32(word0, RXD_W0_DATABYTE_COUNT);
  1831. if (rt2x00_get_field32(word0, RXD_W0_OFDM))
  1832. rxdesc->dev_flags |= RXDONE_SIGNAL_PLCP;
  1833. else
  1834. rxdesc->dev_flags |= RXDONE_SIGNAL_BITRATE;
  1835. if (rt2x00_get_field32(word0, RXD_W0_MY_BSS))
  1836. rxdesc->dev_flags |= RXDONE_MY_BSS;
  1837. }
  1838. /*
  1839. * Interrupt functions.
  1840. */
  1841. static void rt61pci_txdone(struct rt2x00_dev *rt2x00dev)
  1842. {
  1843. struct data_queue *queue;
  1844. struct queue_entry *entry;
  1845. struct queue_entry *entry_done;
  1846. struct queue_entry_priv_pci *entry_priv;
  1847. struct txdone_entry_desc txdesc;
  1848. u32 word;
  1849. u32 reg;
  1850. int type;
  1851. int index;
  1852. int i;
  1853. /*
  1854. * TX_STA_FIFO is a stack of X entries, hence read TX_STA_FIFO
  1855. * at most X times and also stop processing once the TX_STA_FIFO_VALID
  1856. * flag is not set anymore.
  1857. *
  1858. * The legacy drivers use X=TX_RING_SIZE but state in a comment
  1859. * that the TX_STA_FIFO stack has a size of 16. We stick to our
  1860. * tx ring size for now.
  1861. */
  1862. for (i = 0; i < rt2x00dev->ops->tx->entry_num; i++) {
  1863. rt2x00pci_register_read(rt2x00dev, STA_CSR4, &reg);
  1864. if (!rt2x00_get_field32(reg, STA_CSR4_VALID))
  1865. break;
  1866. /*
  1867. * Skip this entry when it contains an invalid
  1868. * queue identication number.
  1869. */
  1870. type = rt2x00_get_field32(reg, STA_CSR4_PID_TYPE);
  1871. queue = rt2x00queue_get_tx_queue(rt2x00dev, type);
  1872. if (unlikely(!queue))
  1873. continue;
  1874. /*
  1875. * Skip this entry when it contains an invalid
  1876. * index number.
  1877. */
  1878. index = rt2x00_get_field32(reg, STA_CSR4_PID_SUBTYPE);
  1879. if (unlikely(index >= queue->limit))
  1880. continue;
  1881. entry = &queue->entries[index];
  1882. entry_priv = entry->priv_data;
  1883. rt2x00_desc_read(entry_priv->desc, 0, &word);
  1884. if (rt2x00_get_field32(word, TXD_W0_OWNER_NIC) ||
  1885. !rt2x00_get_field32(word, TXD_W0_VALID))
  1886. return;
  1887. entry_done = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
  1888. while (entry != entry_done) {
  1889. /* Catch up.
  1890. * Just report any entries we missed as failed.
  1891. */
  1892. WARNING(rt2x00dev,
  1893. "TX status report missed for entry %d\n",
  1894. entry_done->entry_idx);
  1895. rt2x00lib_txdone_noinfo(entry_done, TXDONE_UNKNOWN);
  1896. entry_done = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
  1897. }
  1898. /*
  1899. * Obtain the status about this packet.
  1900. */
  1901. txdesc.flags = 0;
  1902. switch (rt2x00_get_field32(reg, STA_CSR4_TX_RESULT)) {
  1903. case 0: /* Success, maybe with retry */
  1904. __set_bit(TXDONE_SUCCESS, &txdesc.flags);
  1905. break;
  1906. case 6: /* Failure, excessive retries */
  1907. __set_bit(TXDONE_EXCESSIVE_RETRY, &txdesc.flags);
  1908. /* Don't break, this is a failed frame! */
  1909. default: /* Failure */
  1910. __set_bit(TXDONE_FAILURE, &txdesc.flags);
  1911. }
  1912. txdesc.retry = rt2x00_get_field32(reg, STA_CSR4_RETRY_COUNT);
  1913. /*
  1914. * the frame was retried at least once
  1915. * -> hw used fallback rates
  1916. */
  1917. if (txdesc.retry)
  1918. __set_bit(TXDONE_FALLBACK, &txdesc.flags);
  1919. rt2x00lib_txdone(entry, &txdesc);
  1920. }
  1921. }
  1922. static void rt61pci_wakeup(struct rt2x00_dev *rt2x00dev)
  1923. {
  1924. struct rt2x00lib_conf libconf = { .conf = &rt2x00dev->hw->conf };
  1925. rt61pci_config(rt2x00dev, &libconf, IEEE80211_CONF_CHANGE_PS);
  1926. }
  1927. static inline void rt61pci_enable_interrupt(struct rt2x00_dev *rt2x00dev,
  1928. struct rt2x00_field32 irq_field)
  1929. {
  1930. u32 reg;
  1931. /*
  1932. * Enable a single interrupt. The interrupt mask register
  1933. * access needs locking.
  1934. */
  1935. spin_lock_irq(&rt2x00dev->irqmask_lock);
  1936. rt2x00pci_register_read(rt2x00dev, INT_MASK_CSR, &reg);
  1937. rt2x00_set_field32(&reg, irq_field, 0);
  1938. rt2x00pci_register_write(rt2x00dev, INT_MASK_CSR, reg);
  1939. spin_unlock_irq(&rt2x00dev->irqmask_lock);
  1940. }
  1941. static void rt61pci_enable_mcu_interrupt(struct rt2x00_dev *rt2x00dev,
  1942. struct rt2x00_field32 irq_field)
  1943. {
  1944. u32 reg;
  1945. /*
  1946. * Enable a single MCU interrupt. The interrupt mask register
  1947. * access needs locking.
  1948. */
  1949. spin_lock_irq(&rt2x00dev->irqmask_lock);
  1950. rt2x00pci_register_read(rt2x00dev, MCU_INT_MASK_CSR, &reg);
  1951. rt2x00_set_field32(&reg, irq_field, 0);
  1952. rt2x00pci_register_write(rt2x00dev, MCU_INT_MASK_CSR, reg);
  1953. spin_unlock_irq(&rt2x00dev->irqmask_lock);
  1954. }
  1955. static void rt61pci_txstatus_tasklet(unsigned long data)
  1956. {
  1957. struct rt2x00_dev *rt2x00dev = (struct rt2x00_dev *)data;
  1958. rt61pci_txdone(rt2x00dev);
  1959. if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  1960. rt61pci_enable_interrupt(rt2x00dev, INT_MASK_CSR_TXDONE);
  1961. }
  1962. static void rt61pci_tbtt_tasklet(unsigned long data)
  1963. {
  1964. struct rt2x00_dev *rt2x00dev = (struct rt2x00_dev *)data;
  1965. rt2x00lib_beacondone(rt2x00dev);
  1966. if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  1967. rt61pci_enable_interrupt(rt2x00dev, INT_MASK_CSR_BEACON_DONE);
  1968. }
  1969. static void rt61pci_rxdone_tasklet(unsigned long data)
  1970. {
  1971. struct rt2x00_dev *rt2x00dev = (struct rt2x00_dev *)data;
  1972. if (rt2x00pci_rxdone(rt2x00dev))
  1973. tasklet_schedule(&rt2x00dev->rxdone_tasklet);
  1974. else if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  1975. rt61pci_enable_interrupt(rt2x00dev, INT_MASK_CSR_RXDONE);
  1976. }
  1977. static void rt61pci_autowake_tasklet(unsigned long data)
  1978. {
  1979. struct rt2x00_dev *rt2x00dev = (struct rt2x00_dev *)data;
  1980. rt61pci_wakeup(rt2x00dev);
  1981. rt2x00pci_register_write(rt2x00dev,
  1982. M2H_CMD_DONE_CSR, 0xffffffff);
  1983. if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  1984. rt61pci_enable_mcu_interrupt(rt2x00dev, MCU_INT_MASK_CSR_TWAKEUP);
  1985. }
  1986. static irqreturn_t rt61pci_interrupt(int irq, void *dev_instance)
  1987. {
  1988. struct rt2x00_dev *rt2x00dev = dev_instance;
  1989. u32 reg_mcu, mask_mcu;
  1990. u32 reg, mask;
  1991. /*
  1992. * Get the interrupt sources & saved to local variable.
  1993. * Write register value back to clear pending interrupts.
  1994. */
  1995. rt2x00pci_register_read(rt2x00dev, MCU_INT_SOURCE_CSR, &reg_mcu);
  1996. rt2x00pci_register_write(rt2x00dev, MCU_INT_SOURCE_CSR, reg_mcu);
  1997. rt2x00pci_register_read(rt2x00dev, INT_SOURCE_CSR, &reg);
  1998. rt2x00pci_register_write(rt2x00dev, INT_SOURCE_CSR, reg);
  1999. if (!reg && !reg_mcu)
  2000. return IRQ_NONE;
  2001. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  2002. return IRQ_HANDLED;
  2003. /*
  2004. * Schedule tasklets for interrupt handling.
  2005. */
  2006. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_RXDONE))
  2007. tasklet_schedule(&rt2x00dev->rxdone_tasklet);
  2008. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_TXDONE))
  2009. tasklet_schedule(&rt2x00dev->txstatus_tasklet);
  2010. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_BEACON_DONE))
  2011. tasklet_hi_schedule(&rt2x00dev->tbtt_tasklet);
  2012. if (rt2x00_get_field32(reg_mcu, MCU_INT_SOURCE_CSR_TWAKEUP))
  2013. tasklet_schedule(&rt2x00dev->autowake_tasklet);
  2014. /*
  2015. * Since INT_MASK_CSR and INT_SOURCE_CSR use the same bits
  2016. * for interrupts and interrupt masks we can just use the value of
  2017. * INT_SOURCE_CSR to create the interrupt mask.
  2018. */
  2019. mask = reg;
  2020. mask_mcu = reg_mcu;
  2021. /*
  2022. * Disable all interrupts for which a tasklet was scheduled right now,
  2023. * the tasklet will reenable the appropriate interrupts.
  2024. */
  2025. spin_lock(&rt2x00dev->irqmask_lock);
  2026. rt2x00pci_register_read(rt2x00dev, INT_MASK_CSR, &reg);
  2027. reg |= mask;
  2028. rt2x00pci_register_write(rt2x00dev, INT_MASK_CSR, reg);
  2029. rt2x00pci_register_read(rt2x00dev, MCU_INT_MASK_CSR, &reg);
  2030. reg |= mask_mcu;
  2031. rt2x00pci_register_write(rt2x00dev, MCU_INT_MASK_CSR, reg);
  2032. spin_unlock(&rt2x00dev->irqmask_lock);
  2033. return IRQ_HANDLED;
  2034. }
  2035. /*
  2036. * Device probe functions.
  2037. */
  2038. static int rt61pci_validate_eeprom(struct rt2x00_dev *rt2x00dev)
  2039. {
  2040. struct eeprom_93cx6 eeprom;
  2041. u32 reg;
  2042. u16 word;
  2043. u8 *mac;
  2044. s8 value;
  2045. rt2x00pci_register_read(rt2x00dev, E2PROM_CSR, &reg);
  2046. eeprom.data = rt2x00dev;
  2047. eeprom.register_read = rt61pci_eepromregister_read;
  2048. eeprom.register_write = rt61pci_eepromregister_write;
  2049. eeprom.width = rt2x00_get_field32(reg, E2PROM_CSR_TYPE_93C46) ?
  2050. PCI_EEPROM_WIDTH_93C46 : PCI_EEPROM_WIDTH_93C66;
  2051. eeprom.reg_data_in = 0;
  2052. eeprom.reg_data_out = 0;
  2053. eeprom.reg_data_clock = 0;
  2054. eeprom.reg_chip_select = 0;
  2055. eeprom_93cx6_multiread(&eeprom, EEPROM_BASE, rt2x00dev->eeprom,
  2056. EEPROM_SIZE / sizeof(u16));
  2057. /*
  2058. * Start validation of the data that has been read.
  2059. */
  2060. mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
  2061. if (!is_valid_ether_addr(mac)) {
  2062. random_ether_addr(mac);
  2063. EEPROM(rt2x00dev, "MAC: %pM\n", mac);
  2064. }
  2065. rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &word);
  2066. if (word == 0xffff) {
  2067. rt2x00_set_field16(&word, EEPROM_ANTENNA_NUM, 2);
  2068. rt2x00_set_field16(&word, EEPROM_ANTENNA_TX_DEFAULT,
  2069. ANTENNA_B);
  2070. rt2x00_set_field16(&word, EEPROM_ANTENNA_RX_DEFAULT,
  2071. ANTENNA_B);
  2072. rt2x00_set_field16(&word, EEPROM_ANTENNA_FRAME_TYPE, 0);
  2073. rt2x00_set_field16(&word, EEPROM_ANTENNA_DYN_TXAGC, 0);
  2074. rt2x00_set_field16(&word, EEPROM_ANTENNA_HARDWARE_RADIO, 0);
  2075. rt2x00_set_field16(&word, EEPROM_ANTENNA_RF_TYPE, RF5225);
  2076. rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
  2077. EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
  2078. }
  2079. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &word);
  2080. if (word == 0xffff) {
  2081. rt2x00_set_field16(&word, EEPROM_NIC_ENABLE_DIVERSITY, 0);
  2082. rt2x00_set_field16(&word, EEPROM_NIC_TX_DIVERSITY, 0);
  2083. rt2x00_set_field16(&word, EEPROM_NIC_RX_FIXED, 0);
  2084. rt2x00_set_field16(&word, EEPROM_NIC_TX_FIXED, 0);
  2085. rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_BG, 0);
  2086. rt2x00_set_field16(&word, EEPROM_NIC_CARDBUS_ACCEL, 0);
  2087. rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_A, 0);
  2088. rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC, word);
  2089. EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
  2090. }
  2091. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED, &word);
  2092. if (word == 0xffff) {
  2093. rt2x00_set_field16(&word, EEPROM_LED_LED_MODE,
  2094. LED_MODE_DEFAULT);
  2095. rt2x00_eeprom_write(rt2x00dev, EEPROM_LED, word);
  2096. EEPROM(rt2x00dev, "Led: 0x%04x\n", word);
  2097. }
  2098. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
  2099. if (word == 0xffff) {
  2100. rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
  2101. rt2x00_set_field16(&word, EEPROM_FREQ_SEQ, 0);
  2102. rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
  2103. EEPROM(rt2x00dev, "Freq: 0x%04x\n", word);
  2104. }
  2105. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_BG, &word);
  2106. if (word == 0xffff) {
  2107. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_1, 0);
  2108. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_2, 0);
  2109. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_BG, word);
  2110. EEPROM(rt2x00dev, "RSSI OFFSET BG: 0x%04x\n", word);
  2111. } else {
  2112. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_BG_1);
  2113. if (value < -10 || value > 10)
  2114. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_1, 0);
  2115. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_BG_2);
  2116. if (value < -10 || value > 10)
  2117. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_2, 0);
  2118. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_BG, word);
  2119. }
  2120. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_A, &word);
  2121. if (word == 0xffff) {
  2122. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_1, 0);
  2123. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_2, 0);
  2124. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_A, word);
  2125. EEPROM(rt2x00dev, "RSSI OFFSET A: 0x%04x\n", word);
  2126. } else {
  2127. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_A_1);
  2128. if (value < -10 || value > 10)
  2129. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_1, 0);
  2130. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_A_2);
  2131. if (value < -10 || value > 10)
  2132. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_2, 0);
  2133. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_A, word);
  2134. }
  2135. return 0;
  2136. }
  2137. static int rt61pci_init_eeprom(struct rt2x00_dev *rt2x00dev)
  2138. {
  2139. u32 reg;
  2140. u16 value;
  2141. u16 eeprom;
  2142. /*
  2143. * Read EEPROM word for configuration.
  2144. */
  2145. rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
  2146. /*
  2147. * Identify RF chipset.
  2148. */
  2149. value = rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RF_TYPE);
  2150. rt2x00pci_register_read(rt2x00dev, MAC_CSR0, &reg);
  2151. rt2x00_set_chip(rt2x00dev, rt2x00_get_field32(reg, MAC_CSR0_CHIPSET),
  2152. value, rt2x00_get_field32(reg, MAC_CSR0_REVISION));
  2153. if (!rt2x00_rf(rt2x00dev, RF5225) &&
  2154. !rt2x00_rf(rt2x00dev, RF5325) &&
  2155. !rt2x00_rf(rt2x00dev, RF2527) &&
  2156. !rt2x00_rf(rt2x00dev, RF2529)) {
  2157. ERROR(rt2x00dev, "Invalid RF chipset detected.\n");
  2158. return -ENODEV;
  2159. }
  2160. /*
  2161. * Determine number of antennas.
  2162. */
  2163. if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_NUM) == 2)
  2164. __set_bit(CAPABILITY_DOUBLE_ANTENNA, &rt2x00dev->cap_flags);
  2165. /*
  2166. * Identify default antenna configuration.
  2167. */
  2168. rt2x00dev->default_ant.tx =
  2169. rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TX_DEFAULT);
  2170. rt2x00dev->default_ant.rx =
  2171. rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RX_DEFAULT);
  2172. /*
  2173. * Read the Frame type.
  2174. */
  2175. if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_FRAME_TYPE))
  2176. __set_bit(CAPABILITY_FRAME_TYPE, &rt2x00dev->cap_flags);
  2177. /*
  2178. * Detect if this device has a hardware controlled radio.
  2179. */
  2180. if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_HARDWARE_RADIO))
  2181. __set_bit(CAPABILITY_HW_BUTTON, &rt2x00dev->cap_flags);
  2182. /*
  2183. * Read frequency offset and RF programming sequence.
  2184. */
  2185. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
  2186. if (rt2x00_get_field16(eeprom, EEPROM_FREQ_SEQ))
  2187. __set_bit(CAPABILITY_RF_SEQUENCE, &rt2x00dev->cap_flags);
  2188. rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
  2189. /*
  2190. * Read external LNA informations.
  2191. */
  2192. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
  2193. if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_A))
  2194. __set_bit(CAPABILITY_EXTERNAL_LNA_A, &rt2x00dev->cap_flags);
  2195. if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_BG))
  2196. __set_bit(CAPABILITY_EXTERNAL_LNA_BG, &rt2x00dev->cap_flags);
  2197. /*
  2198. * When working with a RF2529 chip without double antenna,
  2199. * the antenna settings should be gathered from the NIC
  2200. * eeprom word.
  2201. */
  2202. if (rt2x00_rf(rt2x00dev, RF2529) &&
  2203. !test_bit(CAPABILITY_DOUBLE_ANTENNA, &rt2x00dev->cap_flags)) {
  2204. rt2x00dev->default_ant.rx =
  2205. ANTENNA_A + rt2x00_get_field16(eeprom, EEPROM_NIC_RX_FIXED);
  2206. rt2x00dev->default_ant.tx =
  2207. ANTENNA_B - rt2x00_get_field16(eeprom, EEPROM_NIC_TX_FIXED);
  2208. if (rt2x00_get_field16(eeprom, EEPROM_NIC_TX_DIVERSITY))
  2209. rt2x00dev->default_ant.tx = ANTENNA_SW_DIVERSITY;
  2210. if (rt2x00_get_field16(eeprom, EEPROM_NIC_ENABLE_DIVERSITY))
  2211. rt2x00dev->default_ant.rx = ANTENNA_SW_DIVERSITY;
  2212. }
  2213. /*
  2214. * Store led settings, for correct led behaviour.
  2215. * If the eeprom value is invalid,
  2216. * switch to default led mode.
  2217. */
  2218. #ifdef CONFIG_RT2X00_LIB_LEDS
  2219. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED, &eeprom);
  2220. value = rt2x00_get_field16(eeprom, EEPROM_LED_LED_MODE);
  2221. rt61pci_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
  2222. rt61pci_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
  2223. if (value == LED_MODE_SIGNAL_STRENGTH)
  2224. rt61pci_init_led(rt2x00dev, &rt2x00dev->led_qual,
  2225. LED_TYPE_QUALITY);
  2226. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_LED_MODE, value);
  2227. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_GPIO_0,
  2228. rt2x00_get_field16(eeprom,
  2229. EEPROM_LED_POLARITY_GPIO_0));
  2230. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_GPIO_1,
  2231. rt2x00_get_field16(eeprom,
  2232. EEPROM_LED_POLARITY_GPIO_1));
  2233. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_GPIO_2,
  2234. rt2x00_get_field16(eeprom,
  2235. EEPROM_LED_POLARITY_GPIO_2));
  2236. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_GPIO_3,
  2237. rt2x00_get_field16(eeprom,
  2238. EEPROM_LED_POLARITY_GPIO_3));
  2239. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_GPIO_4,
  2240. rt2x00_get_field16(eeprom,
  2241. EEPROM_LED_POLARITY_GPIO_4));
  2242. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_ACT,
  2243. rt2x00_get_field16(eeprom, EEPROM_LED_POLARITY_ACT));
  2244. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_READY_BG,
  2245. rt2x00_get_field16(eeprom,
  2246. EEPROM_LED_POLARITY_RDY_G));
  2247. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_READY_A,
  2248. rt2x00_get_field16(eeprom,
  2249. EEPROM_LED_POLARITY_RDY_A));
  2250. #endif /* CONFIG_RT2X00_LIB_LEDS */
  2251. return 0;
  2252. }
  2253. /*
  2254. * RF value list for RF5225 & RF5325
  2255. * Supports: 2.4 GHz & 5.2 GHz, rf_sequence disabled
  2256. */
  2257. static const struct rf_channel rf_vals_noseq[] = {
  2258. { 1, 0x00002ccc, 0x00004786, 0x00068455, 0x000ffa0b },
  2259. { 2, 0x00002ccc, 0x00004786, 0x00068455, 0x000ffa1f },
  2260. { 3, 0x00002ccc, 0x0000478a, 0x00068455, 0x000ffa0b },
  2261. { 4, 0x00002ccc, 0x0000478a, 0x00068455, 0x000ffa1f },
  2262. { 5, 0x00002ccc, 0x0000478e, 0x00068455, 0x000ffa0b },
  2263. { 6, 0x00002ccc, 0x0000478e, 0x00068455, 0x000ffa1f },
  2264. { 7, 0x00002ccc, 0x00004792, 0x00068455, 0x000ffa0b },
  2265. { 8, 0x00002ccc, 0x00004792, 0x00068455, 0x000ffa1f },
  2266. { 9, 0x00002ccc, 0x00004796, 0x00068455, 0x000ffa0b },
  2267. { 10, 0x00002ccc, 0x00004796, 0x00068455, 0x000ffa1f },
  2268. { 11, 0x00002ccc, 0x0000479a, 0x00068455, 0x000ffa0b },
  2269. { 12, 0x00002ccc, 0x0000479a, 0x00068455, 0x000ffa1f },
  2270. { 13, 0x00002ccc, 0x0000479e, 0x00068455, 0x000ffa0b },
  2271. { 14, 0x00002ccc, 0x000047a2, 0x00068455, 0x000ffa13 },
  2272. /* 802.11 UNI / HyperLan 2 */
  2273. { 36, 0x00002ccc, 0x0000499a, 0x0009be55, 0x000ffa23 },
  2274. { 40, 0x00002ccc, 0x000049a2, 0x0009be55, 0x000ffa03 },
  2275. { 44, 0x00002ccc, 0x000049a6, 0x0009be55, 0x000ffa0b },
  2276. { 48, 0x00002ccc, 0x000049aa, 0x0009be55, 0x000ffa13 },
  2277. { 52, 0x00002ccc, 0x000049ae, 0x0009ae55, 0x000ffa1b },
  2278. { 56, 0x00002ccc, 0x000049b2, 0x0009ae55, 0x000ffa23 },
  2279. { 60, 0x00002ccc, 0x000049ba, 0x0009ae55, 0x000ffa03 },
  2280. { 64, 0x00002ccc, 0x000049be, 0x0009ae55, 0x000ffa0b },
  2281. /* 802.11 HyperLan 2 */
  2282. { 100, 0x00002ccc, 0x00004a2a, 0x000bae55, 0x000ffa03 },
  2283. { 104, 0x00002ccc, 0x00004a2e, 0x000bae55, 0x000ffa0b },
  2284. { 108, 0x00002ccc, 0x00004a32, 0x000bae55, 0x000ffa13 },
  2285. { 112, 0x00002ccc, 0x00004a36, 0x000bae55, 0x000ffa1b },
  2286. { 116, 0x00002ccc, 0x00004a3a, 0x000bbe55, 0x000ffa23 },
  2287. { 120, 0x00002ccc, 0x00004a82, 0x000bbe55, 0x000ffa03 },
  2288. { 124, 0x00002ccc, 0x00004a86, 0x000bbe55, 0x000ffa0b },
  2289. { 128, 0x00002ccc, 0x00004a8a, 0x000bbe55, 0x000ffa13 },
  2290. { 132, 0x00002ccc, 0x00004a8e, 0x000bbe55, 0x000ffa1b },
  2291. { 136, 0x00002ccc, 0x00004a92, 0x000bbe55, 0x000ffa23 },
  2292. /* 802.11 UNII */
  2293. { 140, 0x00002ccc, 0x00004a9a, 0x000bbe55, 0x000ffa03 },
  2294. { 149, 0x00002ccc, 0x00004aa2, 0x000bbe55, 0x000ffa1f },
  2295. { 153, 0x00002ccc, 0x00004aa6, 0x000bbe55, 0x000ffa27 },
  2296. { 157, 0x00002ccc, 0x00004aae, 0x000bbe55, 0x000ffa07 },
  2297. { 161, 0x00002ccc, 0x00004ab2, 0x000bbe55, 0x000ffa0f },
  2298. { 165, 0x00002ccc, 0x00004ab6, 0x000bbe55, 0x000ffa17 },
  2299. /* MMAC(Japan)J52 ch 34,38,42,46 */
  2300. { 34, 0x00002ccc, 0x0000499a, 0x0009be55, 0x000ffa0b },
  2301. { 38, 0x00002ccc, 0x0000499e, 0x0009be55, 0x000ffa13 },
  2302. { 42, 0x00002ccc, 0x000049a2, 0x0009be55, 0x000ffa1b },
  2303. { 46, 0x00002ccc, 0x000049a6, 0x0009be55, 0x000ffa23 },
  2304. };
  2305. /*
  2306. * RF value list for RF5225 & RF5325
  2307. * Supports: 2.4 GHz & 5.2 GHz, rf_sequence enabled
  2308. */
  2309. static const struct rf_channel rf_vals_seq[] = {
  2310. { 1, 0x00002ccc, 0x00004786, 0x00068455, 0x000ffa0b },
  2311. { 2, 0x00002ccc, 0x00004786, 0x00068455, 0x000ffa1f },
  2312. { 3, 0x00002ccc, 0x0000478a, 0x00068455, 0x000ffa0b },
  2313. { 4, 0x00002ccc, 0x0000478a, 0x00068455, 0x000ffa1f },
  2314. { 5, 0x00002ccc, 0x0000478e, 0x00068455, 0x000ffa0b },
  2315. { 6, 0x00002ccc, 0x0000478e, 0x00068455, 0x000ffa1f },
  2316. { 7, 0x00002ccc, 0x00004792, 0x00068455, 0x000ffa0b },
  2317. { 8, 0x00002ccc, 0x00004792, 0x00068455, 0x000ffa1f },
  2318. { 9, 0x00002ccc, 0x00004796, 0x00068455, 0x000ffa0b },
  2319. { 10, 0x00002ccc, 0x00004796, 0x00068455, 0x000ffa1f },
  2320. { 11, 0x00002ccc, 0x0000479a, 0x00068455, 0x000ffa0b },
  2321. { 12, 0x00002ccc, 0x0000479a, 0x00068455, 0x000ffa1f },
  2322. { 13, 0x00002ccc, 0x0000479e, 0x00068455, 0x000ffa0b },
  2323. { 14, 0x00002ccc, 0x000047a2, 0x00068455, 0x000ffa13 },
  2324. /* 802.11 UNI / HyperLan 2 */
  2325. { 36, 0x00002cd4, 0x0004481a, 0x00098455, 0x000c0a03 },
  2326. { 40, 0x00002cd0, 0x00044682, 0x00098455, 0x000c0a03 },
  2327. { 44, 0x00002cd0, 0x00044686, 0x00098455, 0x000c0a1b },
  2328. { 48, 0x00002cd0, 0x0004468e, 0x00098655, 0x000c0a0b },
  2329. { 52, 0x00002cd0, 0x00044692, 0x00098855, 0x000c0a23 },
  2330. { 56, 0x00002cd0, 0x0004469a, 0x00098c55, 0x000c0a13 },
  2331. { 60, 0x00002cd0, 0x000446a2, 0x00098e55, 0x000c0a03 },
  2332. { 64, 0x00002cd0, 0x000446a6, 0x00099255, 0x000c0a1b },
  2333. /* 802.11 HyperLan 2 */
  2334. { 100, 0x00002cd4, 0x0004489a, 0x000b9855, 0x000c0a03 },
  2335. { 104, 0x00002cd4, 0x000448a2, 0x000b9855, 0x000c0a03 },
  2336. { 108, 0x00002cd4, 0x000448aa, 0x000b9855, 0x000c0a03 },
  2337. { 112, 0x00002cd4, 0x000448b2, 0x000b9a55, 0x000c0a03 },
  2338. { 116, 0x00002cd4, 0x000448ba, 0x000b9a55, 0x000c0a03 },
  2339. { 120, 0x00002cd0, 0x00044702, 0x000b9a55, 0x000c0a03 },
  2340. { 124, 0x00002cd0, 0x00044706, 0x000b9a55, 0x000c0a1b },
  2341. { 128, 0x00002cd0, 0x0004470e, 0x000b9c55, 0x000c0a0b },
  2342. { 132, 0x00002cd0, 0x00044712, 0x000b9c55, 0x000c0a23 },
  2343. { 136, 0x00002cd0, 0x0004471a, 0x000b9e55, 0x000c0a13 },
  2344. /* 802.11 UNII */
  2345. { 140, 0x00002cd0, 0x00044722, 0x000b9e55, 0x000c0a03 },
  2346. { 149, 0x00002cd0, 0x0004472e, 0x000ba255, 0x000c0a1b },
  2347. { 153, 0x00002cd0, 0x00044736, 0x000ba255, 0x000c0a0b },
  2348. { 157, 0x00002cd4, 0x0004490a, 0x000ba255, 0x000c0a17 },
  2349. { 161, 0x00002cd4, 0x00044912, 0x000ba255, 0x000c0a17 },
  2350. { 165, 0x00002cd4, 0x0004491a, 0x000ba255, 0x000c0a17 },
  2351. /* MMAC(Japan)J52 ch 34,38,42,46 */
  2352. { 34, 0x00002ccc, 0x0000499a, 0x0009be55, 0x000c0a0b },
  2353. { 38, 0x00002ccc, 0x0000499e, 0x0009be55, 0x000c0a13 },
  2354. { 42, 0x00002ccc, 0x000049a2, 0x0009be55, 0x000c0a1b },
  2355. { 46, 0x00002ccc, 0x000049a6, 0x0009be55, 0x000c0a23 },
  2356. };
  2357. static int rt61pci_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
  2358. {
  2359. struct hw_mode_spec *spec = &rt2x00dev->spec;
  2360. struct channel_info *info;
  2361. char *tx_power;
  2362. unsigned int i;
  2363. /*
  2364. * Disable powersaving as default.
  2365. */
  2366. rt2x00dev->hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  2367. /*
  2368. * Initialize all hw fields.
  2369. */
  2370. rt2x00dev->hw->flags =
  2371. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  2372. IEEE80211_HW_SIGNAL_DBM |
  2373. IEEE80211_HW_SUPPORTS_PS |
  2374. IEEE80211_HW_PS_NULLFUNC_STACK;
  2375. SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
  2376. SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
  2377. rt2x00_eeprom_addr(rt2x00dev,
  2378. EEPROM_MAC_ADDR_0));
  2379. /*
  2380. * As rt61 has a global fallback table we cannot specify
  2381. * more then one tx rate per frame but since the hw will
  2382. * try several rates (based on the fallback table) we should
  2383. * initialize max_report_rates to the maximum number of rates
  2384. * we are going to try. Otherwise mac80211 will truncate our
  2385. * reported tx rates and the rc algortihm will end up with
  2386. * incorrect data.
  2387. */
  2388. rt2x00dev->hw->max_rates = 1;
  2389. rt2x00dev->hw->max_report_rates = 7;
  2390. rt2x00dev->hw->max_rate_tries = 1;
  2391. /*
  2392. * Initialize hw_mode information.
  2393. */
  2394. spec->supported_bands = SUPPORT_BAND_2GHZ;
  2395. spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
  2396. if (!test_bit(CAPABILITY_RF_SEQUENCE, &rt2x00dev->cap_flags)) {
  2397. spec->num_channels = 14;
  2398. spec->channels = rf_vals_noseq;
  2399. } else {
  2400. spec->num_channels = 14;
  2401. spec->channels = rf_vals_seq;
  2402. }
  2403. if (rt2x00_rf(rt2x00dev, RF5225) || rt2x00_rf(rt2x00dev, RF5325)) {
  2404. spec->supported_bands |= SUPPORT_BAND_5GHZ;
  2405. spec->num_channels = ARRAY_SIZE(rf_vals_seq);
  2406. }
  2407. /*
  2408. * Create channel information array
  2409. */
  2410. info = kcalloc(spec->num_channels, sizeof(*info), GFP_KERNEL);
  2411. if (!info)
  2412. return -ENOMEM;
  2413. spec->channels_info = info;
  2414. tx_power = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_G_START);
  2415. for (i = 0; i < 14; i++) {
  2416. info[i].max_power = MAX_TXPOWER;
  2417. info[i].default_power1 = TXPOWER_FROM_DEV(tx_power[i]);
  2418. }
  2419. if (spec->num_channels > 14) {
  2420. tx_power = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A_START);
  2421. for (i = 14; i < spec->num_channels; i++) {
  2422. info[i].max_power = MAX_TXPOWER;
  2423. info[i].default_power1 =
  2424. TXPOWER_FROM_DEV(tx_power[i - 14]);
  2425. }
  2426. }
  2427. return 0;
  2428. }
  2429. static int rt61pci_probe_hw(struct rt2x00_dev *rt2x00dev)
  2430. {
  2431. int retval;
  2432. u32 reg;
  2433. /*
  2434. * Disable power saving.
  2435. */
  2436. rt2x00pci_register_write(rt2x00dev, SOFT_RESET_CSR, 0x00000007);
  2437. /*
  2438. * Allocate eeprom data.
  2439. */
  2440. retval = rt61pci_validate_eeprom(rt2x00dev);
  2441. if (retval)
  2442. return retval;
  2443. retval = rt61pci_init_eeprom(rt2x00dev);
  2444. if (retval)
  2445. return retval;
  2446. /*
  2447. * Enable rfkill polling by setting GPIO direction of the
  2448. * rfkill switch GPIO pin correctly.
  2449. */
  2450. rt2x00pci_register_read(rt2x00dev, MAC_CSR13, &reg);
  2451. rt2x00_set_field32(&reg, MAC_CSR13_BIT13, 1);
  2452. rt2x00pci_register_write(rt2x00dev, MAC_CSR13, reg);
  2453. /*
  2454. * Initialize hw specifications.
  2455. */
  2456. retval = rt61pci_probe_hw_mode(rt2x00dev);
  2457. if (retval)
  2458. return retval;
  2459. /*
  2460. * This device has multiple filters for control frames,
  2461. * but has no a separate filter for PS Poll frames.
  2462. */
  2463. __set_bit(CAPABILITY_CONTROL_FILTERS, &rt2x00dev->cap_flags);
  2464. /*
  2465. * This device requires firmware and DMA mapped skbs.
  2466. */
  2467. __set_bit(REQUIRE_FIRMWARE, &rt2x00dev->cap_flags);
  2468. __set_bit(REQUIRE_DMA, &rt2x00dev->cap_flags);
  2469. if (!modparam_nohwcrypt)
  2470. __set_bit(CAPABILITY_HW_CRYPTO, &rt2x00dev->cap_flags);
  2471. __set_bit(CAPABILITY_LINK_TUNING, &rt2x00dev->cap_flags);
  2472. /*
  2473. * Set the rssi offset.
  2474. */
  2475. rt2x00dev->rssi_offset = DEFAULT_RSSI_OFFSET;
  2476. return 0;
  2477. }
  2478. /*
  2479. * IEEE80211 stack callback functions.
  2480. */
  2481. static int rt61pci_conf_tx(struct ieee80211_hw *hw,
  2482. struct ieee80211_vif *vif, u16 queue_idx,
  2483. const struct ieee80211_tx_queue_params *params)
  2484. {
  2485. struct rt2x00_dev *rt2x00dev = hw->priv;
  2486. struct data_queue *queue;
  2487. struct rt2x00_field32 field;
  2488. int retval;
  2489. u32 reg;
  2490. u32 offset;
  2491. /*
  2492. * First pass the configuration through rt2x00lib, that will
  2493. * update the queue settings and validate the input. After that
  2494. * we are free to update the registers based on the value
  2495. * in the queue parameter.
  2496. */
  2497. retval = rt2x00mac_conf_tx(hw, vif, queue_idx, params);
  2498. if (retval)
  2499. return retval;
  2500. /*
  2501. * We only need to perform additional register initialization
  2502. * for WMM queues.
  2503. */
  2504. if (queue_idx >= 4)
  2505. return 0;
  2506. queue = rt2x00queue_get_tx_queue(rt2x00dev, queue_idx);
  2507. /* Update WMM TXOP register */
  2508. offset = AC_TXOP_CSR0 + (sizeof(u32) * (!!(queue_idx & 2)));
  2509. field.bit_offset = (queue_idx & 1) * 16;
  2510. field.bit_mask = 0xffff << field.bit_offset;
  2511. rt2x00pci_register_read(rt2x00dev, offset, &reg);
  2512. rt2x00_set_field32(&reg, field, queue->txop);
  2513. rt2x00pci_register_write(rt2x00dev, offset, reg);
  2514. /* Update WMM registers */
  2515. field.bit_offset = queue_idx * 4;
  2516. field.bit_mask = 0xf << field.bit_offset;
  2517. rt2x00pci_register_read(rt2x00dev, AIFSN_CSR, &reg);
  2518. rt2x00_set_field32(&reg, field, queue->aifs);
  2519. rt2x00pci_register_write(rt2x00dev, AIFSN_CSR, reg);
  2520. rt2x00pci_register_read(rt2x00dev, CWMIN_CSR, &reg);
  2521. rt2x00_set_field32(&reg, field, queue->cw_min);
  2522. rt2x00pci_register_write(rt2x00dev, CWMIN_CSR, reg);
  2523. rt2x00pci_register_read(rt2x00dev, CWMAX_CSR, &reg);
  2524. rt2x00_set_field32(&reg, field, queue->cw_max);
  2525. rt2x00pci_register_write(rt2x00dev, CWMAX_CSR, reg);
  2526. return 0;
  2527. }
  2528. static u64 rt61pci_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  2529. {
  2530. struct rt2x00_dev *rt2x00dev = hw->priv;
  2531. u64 tsf;
  2532. u32 reg;
  2533. rt2x00pci_register_read(rt2x00dev, TXRX_CSR13, &reg);
  2534. tsf = (u64) rt2x00_get_field32(reg, TXRX_CSR13_HIGH_TSFTIMER) << 32;
  2535. rt2x00pci_register_read(rt2x00dev, TXRX_CSR12, &reg);
  2536. tsf |= rt2x00_get_field32(reg, TXRX_CSR12_LOW_TSFTIMER);
  2537. return tsf;
  2538. }
  2539. static const struct ieee80211_ops rt61pci_mac80211_ops = {
  2540. .tx = rt2x00mac_tx,
  2541. .start = rt2x00mac_start,
  2542. .stop = rt2x00mac_stop,
  2543. .add_interface = rt2x00mac_add_interface,
  2544. .remove_interface = rt2x00mac_remove_interface,
  2545. .config = rt2x00mac_config,
  2546. .configure_filter = rt2x00mac_configure_filter,
  2547. .set_key = rt2x00mac_set_key,
  2548. .sw_scan_start = rt2x00mac_sw_scan_start,
  2549. .sw_scan_complete = rt2x00mac_sw_scan_complete,
  2550. .get_stats = rt2x00mac_get_stats,
  2551. .bss_info_changed = rt2x00mac_bss_info_changed,
  2552. .conf_tx = rt61pci_conf_tx,
  2553. .get_tsf = rt61pci_get_tsf,
  2554. .rfkill_poll = rt2x00mac_rfkill_poll,
  2555. .flush = rt2x00mac_flush,
  2556. .set_antenna = rt2x00mac_set_antenna,
  2557. .get_antenna = rt2x00mac_get_antenna,
  2558. .get_ringparam = rt2x00mac_get_ringparam,
  2559. .tx_frames_pending = rt2x00mac_tx_frames_pending,
  2560. };
  2561. static const struct rt2x00lib_ops rt61pci_rt2x00_ops = {
  2562. .irq_handler = rt61pci_interrupt,
  2563. .txstatus_tasklet = rt61pci_txstatus_tasklet,
  2564. .tbtt_tasklet = rt61pci_tbtt_tasklet,
  2565. .rxdone_tasklet = rt61pci_rxdone_tasklet,
  2566. .autowake_tasklet = rt61pci_autowake_tasklet,
  2567. .probe_hw = rt61pci_probe_hw,
  2568. .get_firmware_name = rt61pci_get_firmware_name,
  2569. .check_firmware = rt61pci_check_firmware,
  2570. .load_firmware = rt61pci_load_firmware,
  2571. .initialize = rt2x00pci_initialize,
  2572. .uninitialize = rt2x00pci_uninitialize,
  2573. .get_entry_state = rt61pci_get_entry_state,
  2574. .clear_entry = rt61pci_clear_entry,
  2575. .set_device_state = rt61pci_set_device_state,
  2576. .rfkill_poll = rt61pci_rfkill_poll,
  2577. .link_stats = rt61pci_link_stats,
  2578. .reset_tuner = rt61pci_reset_tuner,
  2579. .link_tuner = rt61pci_link_tuner,
  2580. .start_queue = rt61pci_start_queue,
  2581. .kick_queue = rt61pci_kick_queue,
  2582. .stop_queue = rt61pci_stop_queue,
  2583. .flush_queue = rt2x00pci_flush_queue,
  2584. .write_tx_desc = rt61pci_write_tx_desc,
  2585. .write_beacon = rt61pci_write_beacon,
  2586. .clear_beacon = rt61pci_clear_beacon,
  2587. .fill_rxdone = rt61pci_fill_rxdone,
  2588. .config_shared_key = rt61pci_config_shared_key,
  2589. .config_pairwise_key = rt61pci_config_pairwise_key,
  2590. .config_filter = rt61pci_config_filter,
  2591. .config_intf = rt61pci_config_intf,
  2592. .config_erp = rt61pci_config_erp,
  2593. .config_ant = rt61pci_config_ant,
  2594. .config = rt61pci_config,
  2595. };
  2596. static const struct data_queue_desc rt61pci_queue_rx = {
  2597. .entry_num = 32,
  2598. .data_size = DATA_FRAME_SIZE,
  2599. .desc_size = RXD_DESC_SIZE,
  2600. .priv_size = sizeof(struct queue_entry_priv_pci),
  2601. };
  2602. static const struct data_queue_desc rt61pci_queue_tx = {
  2603. .entry_num = 32,
  2604. .data_size = DATA_FRAME_SIZE,
  2605. .desc_size = TXD_DESC_SIZE,
  2606. .priv_size = sizeof(struct queue_entry_priv_pci),
  2607. };
  2608. static const struct data_queue_desc rt61pci_queue_bcn = {
  2609. .entry_num = 4,
  2610. .data_size = 0, /* No DMA required for beacons */
  2611. .desc_size = TXINFO_SIZE,
  2612. .priv_size = sizeof(struct queue_entry_priv_pci),
  2613. };
  2614. static const struct rt2x00_ops rt61pci_ops = {
  2615. .name = KBUILD_MODNAME,
  2616. .max_sta_intf = 1,
  2617. .max_ap_intf = 4,
  2618. .eeprom_size = EEPROM_SIZE,
  2619. .rf_size = RF_SIZE,
  2620. .tx_queues = NUM_TX_QUEUES,
  2621. .extra_tx_headroom = 0,
  2622. .rx = &rt61pci_queue_rx,
  2623. .tx = &rt61pci_queue_tx,
  2624. .bcn = &rt61pci_queue_bcn,
  2625. .lib = &rt61pci_rt2x00_ops,
  2626. .hw = &rt61pci_mac80211_ops,
  2627. #ifdef CONFIG_RT2X00_LIB_DEBUGFS
  2628. .debugfs = &rt61pci_rt2x00debug,
  2629. #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
  2630. };
  2631. /*
  2632. * RT61pci module information.
  2633. */
  2634. static DEFINE_PCI_DEVICE_TABLE(rt61pci_device_table) = {
  2635. /* RT2561s */
  2636. { PCI_DEVICE(0x1814, 0x0301) },
  2637. /* RT2561 v2 */
  2638. { PCI_DEVICE(0x1814, 0x0302) },
  2639. /* RT2661 */
  2640. { PCI_DEVICE(0x1814, 0x0401) },
  2641. { 0, }
  2642. };
  2643. MODULE_AUTHOR(DRV_PROJECT);
  2644. MODULE_VERSION(DRV_VERSION);
  2645. MODULE_DESCRIPTION("Ralink RT61 PCI & PCMCIA Wireless LAN driver.");
  2646. MODULE_SUPPORTED_DEVICE("Ralink RT2561, RT2561s & RT2661 "
  2647. "PCI & PCMCIA chipset based cards");
  2648. MODULE_DEVICE_TABLE(pci, rt61pci_device_table);
  2649. MODULE_FIRMWARE(FIRMWARE_RT2561);
  2650. MODULE_FIRMWARE(FIRMWARE_RT2561s);
  2651. MODULE_FIRMWARE(FIRMWARE_RT2661);
  2652. MODULE_LICENSE("GPL");
  2653. static int rt61pci_probe(struct pci_dev *pci_dev,
  2654. const struct pci_device_id *id)
  2655. {
  2656. return rt2x00pci_probe(pci_dev, &rt61pci_ops);
  2657. }
  2658. static struct pci_driver rt61pci_driver = {
  2659. .name = KBUILD_MODNAME,
  2660. .id_table = rt61pci_device_table,
  2661. .probe = rt61pci_probe,
  2662. .remove = __devexit_p(rt2x00pci_remove),
  2663. .suspend = rt2x00pci_suspend,
  2664. .resume = rt2x00pci_resume,
  2665. };
  2666. static int __init rt61pci_init(void)
  2667. {
  2668. return pci_register_driver(&rt61pci_driver);
  2669. }
  2670. static void __exit rt61pci_exit(void)
  2671. {
  2672. pci_unregister_driver(&rt61pci_driver);
  2673. }
  2674. module_init(rt61pci_init);
  2675. module_exit(rt61pci_exit);