rt61pci.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000
  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 int modparam_nohwcrypt = 0;
  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. unsigned int beacon_base;
  477. u32 reg;
  478. if (flags & CONFIG_UPDATE_TYPE) {
  479. /*
  480. * Clear current synchronisation setup.
  481. * For the Beacon base registers, we only need to clear
  482. * the first byte since that byte contains the VALID and OWNER
  483. * bits which (when set to 0) will invalidate the entire beacon.
  484. */
  485. beacon_base = HW_BEACON_OFFSET(intf->beacon->entry_idx);
  486. rt2x00pci_register_write(rt2x00dev, beacon_base, 0);
  487. /*
  488. * Enable synchronisation.
  489. */
  490. rt2x00pci_register_read(rt2x00dev, TXRX_CSR9, &reg);
  491. rt2x00_set_field32(&reg, TXRX_CSR9_TSF_TICKING, 1);
  492. rt2x00_set_field32(&reg, TXRX_CSR9_TSF_SYNC, conf->sync);
  493. rt2x00_set_field32(&reg, TXRX_CSR9_TBTT_ENABLE, 1);
  494. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, reg);
  495. }
  496. if (flags & CONFIG_UPDATE_MAC) {
  497. reg = le32_to_cpu(conf->mac[1]);
  498. rt2x00_set_field32(&reg, MAC_CSR3_UNICAST_TO_ME_MASK, 0xff);
  499. conf->mac[1] = cpu_to_le32(reg);
  500. rt2x00pci_register_multiwrite(rt2x00dev, MAC_CSR2,
  501. conf->mac, sizeof(conf->mac));
  502. }
  503. if (flags & CONFIG_UPDATE_BSSID) {
  504. reg = le32_to_cpu(conf->bssid[1]);
  505. rt2x00_set_field32(&reg, MAC_CSR5_BSS_ID_MASK, 3);
  506. conf->bssid[1] = cpu_to_le32(reg);
  507. rt2x00pci_register_multiwrite(rt2x00dev, MAC_CSR4,
  508. conf->bssid, sizeof(conf->bssid));
  509. }
  510. }
  511. static void rt61pci_config_erp(struct rt2x00_dev *rt2x00dev,
  512. struct rt2x00lib_erp *erp,
  513. u32 changed)
  514. {
  515. u32 reg;
  516. rt2x00pci_register_read(rt2x00dev, TXRX_CSR0, &reg);
  517. rt2x00_set_field32(&reg, TXRX_CSR0_RX_ACK_TIMEOUT, 0x32);
  518. rt2x00_set_field32(&reg, TXRX_CSR0_TSF_OFFSET, IEEE80211_HEADER);
  519. rt2x00pci_register_write(rt2x00dev, TXRX_CSR0, reg);
  520. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  521. rt2x00pci_register_read(rt2x00dev, TXRX_CSR4, &reg);
  522. rt2x00_set_field32(&reg, TXRX_CSR4_AUTORESPOND_ENABLE, 1);
  523. rt2x00_set_field32(&reg, TXRX_CSR4_AUTORESPOND_PREAMBLE,
  524. !!erp->short_preamble);
  525. rt2x00pci_register_write(rt2x00dev, TXRX_CSR4, reg);
  526. }
  527. if (changed & BSS_CHANGED_BASIC_RATES)
  528. rt2x00pci_register_write(rt2x00dev, TXRX_CSR5,
  529. erp->basic_rates);
  530. if (changed & BSS_CHANGED_BEACON_INT) {
  531. rt2x00pci_register_read(rt2x00dev, TXRX_CSR9, &reg);
  532. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_INTERVAL,
  533. erp->beacon_int * 16);
  534. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, reg);
  535. }
  536. if (changed & BSS_CHANGED_ERP_SLOT) {
  537. rt2x00pci_register_read(rt2x00dev, MAC_CSR9, &reg);
  538. rt2x00_set_field32(&reg, MAC_CSR9_SLOT_TIME, erp->slot_time);
  539. rt2x00pci_register_write(rt2x00dev, MAC_CSR9, reg);
  540. rt2x00pci_register_read(rt2x00dev, MAC_CSR8, &reg);
  541. rt2x00_set_field32(&reg, MAC_CSR8_SIFS, erp->sifs);
  542. rt2x00_set_field32(&reg, MAC_CSR8_SIFS_AFTER_RX_OFDM, 3);
  543. rt2x00_set_field32(&reg, MAC_CSR8_EIFS, erp->eifs);
  544. rt2x00pci_register_write(rt2x00dev, MAC_CSR8, reg);
  545. }
  546. }
  547. static void rt61pci_config_antenna_5x(struct rt2x00_dev *rt2x00dev,
  548. struct antenna_setup *ant)
  549. {
  550. u8 r3;
  551. u8 r4;
  552. u8 r77;
  553. rt61pci_bbp_read(rt2x00dev, 3, &r3);
  554. rt61pci_bbp_read(rt2x00dev, 4, &r4);
  555. rt61pci_bbp_read(rt2x00dev, 77, &r77);
  556. rt2x00_set_field8(&r3, BBP_R3_SMART_MODE, rt2x00_rf(rt2x00dev, RF5325));
  557. /*
  558. * Configure the RX antenna.
  559. */
  560. switch (ant->rx) {
  561. case ANTENNA_HW_DIVERSITY:
  562. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 2);
  563. rt2x00_set_field8(&r4, BBP_R4_RX_FRAME_END,
  564. (rt2x00dev->curr_band != IEEE80211_BAND_5GHZ));
  565. break;
  566. case ANTENNA_A:
  567. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  568. rt2x00_set_field8(&r4, BBP_R4_RX_FRAME_END, 0);
  569. if (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ)
  570. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 0);
  571. else
  572. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 3);
  573. break;
  574. case ANTENNA_B:
  575. default:
  576. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  577. rt2x00_set_field8(&r4, BBP_R4_RX_FRAME_END, 0);
  578. if (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ)
  579. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 3);
  580. else
  581. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 0);
  582. break;
  583. }
  584. rt61pci_bbp_write(rt2x00dev, 77, r77);
  585. rt61pci_bbp_write(rt2x00dev, 3, r3);
  586. rt61pci_bbp_write(rt2x00dev, 4, r4);
  587. }
  588. static void rt61pci_config_antenna_2x(struct rt2x00_dev *rt2x00dev,
  589. struct antenna_setup *ant)
  590. {
  591. u8 r3;
  592. u8 r4;
  593. u8 r77;
  594. rt61pci_bbp_read(rt2x00dev, 3, &r3);
  595. rt61pci_bbp_read(rt2x00dev, 4, &r4);
  596. rt61pci_bbp_read(rt2x00dev, 77, &r77);
  597. rt2x00_set_field8(&r3, BBP_R3_SMART_MODE, rt2x00_rf(rt2x00dev, RF2529));
  598. rt2x00_set_field8(&r4, BBP_R4_RX_FRAME_END,
  599. !test_bit(CONFIG_FRAME_TYPE, &rt2x00dev->flags));
  600. /*
  601. * Configure the RX antenna.
  602. */
  603. switch (ant->rx) {
  604. case ANTENNA_HW_DIVERSITY:
  605. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 2);
  606. break;
  607. case ANTENNA_A:
  608. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  609. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 3);
  610. break;
  611. case ANTENNA_B:
  612. default:
  613. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  614. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 0);
  615. break;
  616. }
  617. rt61pci_bbp_write(rt2x00dev, 77, r77);
  618. rt61pci_bbp_write(rt2x00dev, 3, r3);
  619. rt61pci_bbp_write(rt2x00dev, 4, r4);
  620. }
  621. static void rt61pci_config_antenna_2529_rx(struct rt2x00_dev *rt2x00dev,
  622. const int p1, const int p2)
  623. {
  624. u32 reg;
  625. rt2x00pci_register_read(rt2x00dev, MAC_CSR13, &reg);
  626. rt2x00_set_field32(&reg, MAC_CSR13_BIT4, p1);
  627. rt2x00_set_field32(&reg, MAC_CSR13_BIT12, 0);
  628. rt2x00_set_field32(&reg, MAC_CSR13_BIT3, !p2);
  629. rt2x00_set_field32(&reg, MAC_CSR13_BIT11, 0);
  630. rt2x00pci_register_write(rt2x00dev, MAC_CSR13, reg);
  631. }
  632. static void rt61pci_config_antenna_2529(struct rt2x00_dev *rt2x00dev,
  633. struct antenna_setup *ant)
  634. {
  635. u8 r3;
  636. u8 r4;
  637. u8 r77;
  638. rt61pci_bbp_read(rt2x00dev, 3, &r3);
  639. rt61pci_bbp_read(rt2x00dev, 4, &r4);
  640. rt61pci_bbp_read(rt2x00dev, 77, &r77);
  641. /*
  642. * Configure the RX antenna.
  643. */
  644. switch (ant->rx) {
  645. case ANTENNA_A:
  646. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  647. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 0);
  648. rt61pci_config_antenna_2529_rx(rt2x00dev, 0, 0);
  649. break;
  650. case ANTENNA_HW_DIVERSITY:
  651. /*
  652. * FIXME: Antenna selection for the rf 2529 is very confusing
  653. * in the legacy driver. Just default to antenna B until the
  654. * legacy code can be properly translated into rt2x00 code.
  655. */
  656. case ANTENNA_B:
  657. default:
  658. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  659. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 3);
  660. rt61pci_config_antenna_2529_rx(rt2x00dev, 1, 1);
  661. break;
  662. }
  663. rt61pci_bbp_write(rt2x00dev, 77, r77);
  664. rt61pci_bbp_write(rt2x00dev, 3, r3);
  665. rt61pci_bbp_write(rt2x00dev, 4, r4);
  666. }
  667. struct antenna_sel {
  668. u8 word;
  669. /*
  670. * value[0] -> non-LNA
  671. * value[1] -> LNA
  672. */
  673. u8 value[2];
  674. };
  675. static const struct antenna_sel antenna_sel_a[] = {
  676. { 96, { 0x58, 0x78 } },
  677. { 104, { 0x38, 0x48 } },
  678. { 75, { 0xfe, 0x80 } },
  679. { 86, { 0xfe, 0x80 } },
  680. { 88, { 0xfe, 0x80 } },
  681. { 35, { 0x60, 0x60 } },
  682. { 97, { 0x58, 0x58 } },
  683. { 98, { 0x58, 0x58 } },
  684. };
  685. static const struct antenna_sel antenna_sel_bg[] = {
  686. { 96, { 0x48, 0x68 } },
  687. { 104, { 0x2c, 0x3c } },
  688. { 75, { 0xfe, 0x80 } },
  689. { 86, { 0xfe, 0x80 } },
  690. { 88, { 0xfe, 0x80 } },
  691. { 35, { 0x50, 0x50 } },
  692. { 97, { 0x48, 0x48 } },
  693. { 98, { 0x48, 0x48 } },
  694. };
  695. static void rt61pci_config_ant(struct rt2x00_dev *rt2x00dev,
  696. struct antenna_setup *ant)
  697. {
  698. const struct antenna_sel *sel;
  699. unsigned int lna;
  700. unsigned int i;
  701. u32 reg;
  702. /*
  703. * We should never come here because rt2x00lib is supposed
  704. * to catch this and send us the correct antenna explicitely.
  705. */
  706. BUG_ON(ant->rx == ANTENNA_SW_DIVERSITY ||
  707. ant->tx == ANTENNA_SW_DIVERSITY);
  708. if (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ) {
  709. sel = antenna_sel_a;
  710. lna = test_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags);
  711. } else {
  712. sel = antenna_sel_bg;
  713. lna = test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags);
  714. }
  715. for (i = 0; i < ARRAY_SIZE(antenna_sel_a); i++)
  716. rt61pci_bbp_write(rt2x00dev, sel[i].word, sel[i].value[lna]);
  717. rt2x00pci_register_read(rt2x00dev, PHY_CSR0, &reg);
  718. rt2x00_set_field32(&reg, PHY_CSR0_PA_PE_BG,
  719. rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
  720. rt2x00_set_field32(&reg, PHY_CSR0_PA_PE_A,
  721. rt2x00dev->curr_band == IEEE80211_BAND_5GHZ);
  722. rt2x00pci_register_write(rt2x00dev, PHY_CSR0, reg);
  723. if (rt2x00_rf(rt2x00dev, RF5225) || rt2x00_rf(rt2x00dev, RF5325))
  724. rt61pci_config_antenna_5x(rt2x00dev, ant);
  725. else if (rt2x00_rf(rt2x00dev, RF2527))
  726. rt61pci_config_antenna_2x(rt2x00dev, ant);
  727. else if (rt2x00_rf(rt2x00dev, RF2529)) {
  728. if (test_bit(CONFIG_DOUBLE_ANTENNA, &rt2x00dev->flags))
  729. rt61pci_config_antenna_2x(rt2x00dev, ant);
  730. else
  731. rt61pci_config_antenna_2529(rt2x00dev, ant);
  732. }
  733. }
  734. static void rt61pci_config_lna_gain(struct rt2x00_dev *rt2x00dev,
  735. struct rt2x00lib_conf *libconf)
  736. {
  737. u16 eeprom;
  738. short lna_gain = 0;
  739. if (libconf->conf->channel->band == IEEE80211_BAND_2GHZ) {
  740. if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags))
  741. lna_gain += 14;
  742. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_BG, &eeprom);
  743. lna_gain -= rt2x00_get_field16(eeprom, EEPROM_RSSI_OFFSET_BG_1);
  744. } else {
  745. if (test_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags))
  746. lna_gain += 14;
  747. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_A, &eeprom);
  748. lna_gain -= rt2x00_get_field16(eeprom, EEPROM_RSSI_OFFSET_A_1);
  749. }
  750. rt2x00dev->lna_gain = lna_gain;
  751. }
  752. static void rt61pci_config_channel(struct rt2x00_dev *rt2x00dev,
  753. struct rf_channel *rf, const int txpower)
  754. {
  755. u8 r3;
  756. u8 r94;
  757. u8 smart;
  758. rt2x00_set_field32(&rf->rf3, RF3_TXPOWER, TXPOWER_TO_DEV(txpower));
  759. rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
  760. smart = !(rt2x00_rf(rt2x00dev, RF5225) || rt2x00_rf(rt2x00dev, RF2527));
  761. rt61pci_bbp_read(rt2x00dev, 3, &r3);
  762. rt2x00_set_field8(&r3, BBP_R3_SMART_MODE, smart);
  763. rt61pci_bbp_write(rt2x00dev, 3, r3);
  764. r94 = 6;
  765. if (txpower > MAX_TXPOWER && txpower <= (MAX_TXPOWER + r94))
  766. r94 += txpower - MAX_TXPOWER;
  767. else if (txpower < MIN_TXPOWER && txpower >= (MIN_TXPOWER - r94))
  768. r94 += txpower;
  769. rt61pci_bbp_write(rt2x00dev, 94, r94);
  770. rt61pci_rf_write(rt2x00dev, 1, rf->rf1);
  771. rt61pci_rf_write(rt2x00dev, 2, rf->rf2);
  772. rt61pci_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
  773. rt61pci_rf_write(rt2x00dev, 4, rf->rf4);
  774. udelay(200);
  775. rt61pci_rf_write(rt2x00dev, 1, rf->rf1);
  776. rt61pci_rf_write(rt2x00dev, 2, rf->rf2);
  777. rt61pci_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
  778. rt61pci_rf_write(rt2x00dev, 4, rf->rf4);
  779. udelay(200);
  780. rt61pci_rf_write(rt2x00dev, 1, rf->rf1);
  781. rt61pci_rf_write(rt2x00dev, 2, rf->rf2);
  782. rt61pci_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
  783. rt61pci_rf_write(rt2x00dev, 4, rf->rf4);
  784. msleep(1);
  785. }
  786. static void rt61pci_config_txpower(struct rt2x00_dev *rt2x00dev,
  787. const int txpower)
  788. {
  789. struct rf_channel rf;
  790. rt2x00_rf_read(rt2x00dev, 1, &rf.rf1);
  791. rt2x00_rf_read(rt2x00dev, 2, &rf.rf2);
  792. rt2x00_rf_read(rt2x00dev, 3, &rf.rf3);
  793. rt2x00_rf_read(rt2x00dev, 4, &rf.rf4);
  794. rt61pci_config_channel(rt2x00dev, &rf, txpower);
  795. }
  796. static void rt61pci_config_retry_limit(struct rt2x00_dev *rt2x00dev,
  797. struct rt2x00lib_conf *libconf)
  798. {
  799. u32 reg;
  800. rt2x00pci_register_read(rt2x00dev, TXRX_CSR4, &reg);
  801. rt2x00_set_field32(&reg, TXRX_CSR4_OFDM_TX_RATE_DOWN, 1);
  802. rt2x00_set_field32(&reg, TXRX_CSR4_OFDM_TX_RATE_STEP, 0);
  803. rt2x00_set_field32(&reg, TXRX_CSR4_OFDM_TX_FALLBACK_CCK, 0);
  804. rt2x00_set_field32(&reg, TXRX_CSR4_LONG_RETRY_LIMIT,
  805. libconf->conf->long_frame_max_tx_count);
  806. rt2x00_set_field32(&reg, TXRX_CSR4_SHORT_RETRY_LIMIT,
  807. libconf->conf->short_frame_max_tx_count);
  808. rt2x00pci_register_write(rt2x00dev, TXRX_CSR4, reg);
  809. }
  810. static void rt61pci_config_ps(struct rt2x00_dev *rt2x00dev,
  811. struct rt2x00lib_conf *libconf)
  812. {
  813. enum dev_state state =
  814. (libconf->conf->flags & IEEE80211_CONF_PS) ?
  815. STATE_SLEEP : STATE_AWAKE;
  816. u32 reg;
  817. if (state == STATE_SLEEP) {
  818. rt2x00pci_register_read(rt2x00dev, MAC_CSR11, &reg);
  819. rt2x00_set_field32(&reg, MAC_CSR11_DELAY_AFTER_TBCN,
  820. rt2x00dev->beacon_int - 10);
  821. rt2x00_set_field32(&reg, MAC_CSR11_TBCN_BEFORE_WAKEUP,
  822. libconf->conf->listen_interval - 1);
  823. rt2x00_set_field32(&reg, MAC_CSR11_WAKEUP_LATENCY, 5);
  824. /* We must first disable autowake before it can be enabled */
  825. rt2x00_set_field32(&reg, MAC_CSR11_AUTOWAKE, 0);
  826. rt2x00pci_register_write(rt2x00dev, MAC_CSR11, reg);
  827. rt2x00_set_field32(&reg, MAC_CSR11_AUTOWAKE, 1);
  828. rt2x00pci_register_write(rt2x00dev, MAC_CSR11, reg);
  829. rt2x00pci_register_write(rt2x00dev, SOFT_RESET_CSR, 0x00000005);
  830. rt2x00pci_register_write(rt2x00dev, IO_CNTL_CSR, 0x0000001c);
  831. rt2x00pci_register_write(rt2x00dev, PCI_USEC_CSR, 0x00000060);
  832. rt61pci_mcu_request(rt2x00dev, MCU_SLEEP, 0xff, 0, 0);
  833. } else {
  834. rt2x00pci_register_read(rt2x00dev, MAC_CSR11, &reg);
  835. rt2x00_set_field32(&reg, MAC_CSR11_DELAY_AFTER_TBCN, 0);
  836. rt2x00_set_field32(&reg, MAC_CSR11_TBCN_BEFORE_WAKEUP, 0);
  837. rt2x00_set_field32(&reg, MAC_CSR11_AUTOWAKE, 0);
  838. rt2x00_set_field32(&reg, MAC_CSR11_WAKEUP_LATENCY, 0);
  839. rt2x00pci_register_write(rt2x00dev, MAC_CSR11, reg);
  840. rt2x00pci_register_write(rt2x00dev, SOFT_RESET_CSR, 0x00000007);
  841. rt2x00pci_register_write(rt2x00dev, IO_CNTL_CSR, 0x00000018);
  842. rt2x00pci_register_write(rt2x00dev, PCI_USEC_CSR, 0x00000020);
  843. rt61pci_mcu_request(rt2x00dev, MCU_WAKEUP, 0xff, 0, 0);
  844. }
  845. }
  846. static void rt61pci_config(struct rt2x00_dev *rt2x00dev,
  847. struct rt2x00lib_conf *libconf,
  848. const unsigned int flags)
  849. {
  850. /* Always recalculate LNA gain before changing configuration */
  851. rt61pci_config_lna_gain(rt2x00dev, libconf);
  852. if (flags & IEEE80211_CONF_CHANGE_CHANNEL)
  853. rt61pci_config_channel(rt2x00dev, &libconf->rf,
  854. libconf->conf->power_level);
  855. if ((flags & IEEE80211_CONF_CHANGE_POWER) &&
  856. !(flags & IEEE80211_CONF_CHANGE_CHANNEL))
  857. rt61pci_config_txpower(rt2x00dev, libconf->conf->power_level);
  858. if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
  859. rt61pci_config_retry_limit(rt2x00dev, libconf);
  860. if (flags & IEEE80211_CONF_CHANGE_PS)
  861. rt61pci_config_ps(rt2x00dev, libconf);
  862. }
  863. /*
  864. * Link tuning
  865. */
  866. static void rt61pci_link_stats(struct rt2x00_dev *rt2x00dev,
  867. struct link_qual *qual)
  868. {
  869. u32 reg;
  870. /*
  871. * Update FCS error count from register.
  872. */
  873. rt2x00pci_register_read(rt2x00dev, STA_CSR0, &reg);
  874. qual->rx_failed = rt2x00_get_field32(reg, STA_CSR0_FCS_ERROR);
  875. /*
  876. * Update False CCA count from register.
  877. */
  878. rt2x00pci_register_read(rt2x00dev, STA_CSR1, &reg);
  879. qual->false_cca = rt2x00_get_field32(reg, STA_CSR1_FALSE_CCA_ERROR);
  880. }
  881. static inline void rt61pci_set_vgc(struct rt2x00_dev *rt2x00dev,
  882. struct link_qual *qual, u8 vgc_level)
  883. {
  884. if (qual->vgc_level != vgc_level) {
  885. rt61pci_bbp_write(rt2x00dev, 17, vgc_level);
  886. qual->vgc_level = vgc_level;
  887. qual->vgc_level_reg = vgc_level;
  888. }
  889. }
  890. static void rt61pci_reset_tuner(struct rt2x00_dev *rt2x00dev,
  891. struct link_qual *qual)
  892. {
  893. rt61pci_set_vgc(rt2x00dev, qual, 0x20);
  894. }
  895. static void rt61pci_link_tuner(struct rt2x00_dev *rt2x00dev,
  896. struct link_qual *qual, const u32 count)
  897. {
  898. u8 up_bound;
  899. u8 low_bound;
  900. /*
  901. * Determine r17 bounds.
  902. */
  903. if (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ) {
  904. low_bound = 0x28;
  905. up_bound = 0x48;
  906. if (test_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags)) {
  907. low_bound += 0x10;
  908. up_bound += 0x10;
  909. }
  910. } else {
  911. low_bound = 0x20;
  912. up_bound = 0x40;
  913. if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags)) {
  914. low_bound += 0x10;
  915. up_bound += 0x10;
  916. }
  917. }
  918. /*
  919. * If we are not associated, we should go straight to the
  920. * dynamic CCA tuning.
  921. */
  922. if (!rt2x00dev->intf_associated)
  923. goto dynamic_cca_tune;
  924. /*
  925. * Special big-R17 for very short distance
  926. */
  927. if (qual->rssi >= -35) {
  928. rt61pci_set_vgc(rt2x00dev, qual, 0x60);
  929. return;
  930. }
  931. /*
  932. * Special big-R17 for short distance
  933. */
  934. if (qual->rssi >= -58) {
  935. rt61pci_set_vgc(rt2x00dev, qual, up_bound);
  936. return;
  937. }
  938. /*
  939. * Special big-R17 for middle-short distance
  940. */
  941. if (qual->rssi >= -66) {
  942. rt61pci_set_vgc(rt2x00dev, qual, low_bound + 0x10);
  943. return;
  944. }
  945. /*
  946. * Special mid-R17 for middle distance
  947. */
  948. if (qual->rssi >= -74) {
  949. rt61pci_set_vgc(rt2x00dev, qual, low_bound + 0x08);
  950. return;
  951. }
  952. /*
  953. * Special case: Change up_bound based on the rssi.
  954. * Lower up_bound when rssi is weaker then -74 dBm.
  955. */
  956. up_bound -= 2 * (-74 - qual->rssi);
  957. if (low_bound > up_bound)
  958. up_bound = low_bound;
  959. if (qual->vgc_level > up_bound) {
  960. rt61pci_set_vgc(rt2x00dev, qual, up_bound);
  961. return;
  962. }
  963. dynamic_cca_tune:
  964. /*
  965. * r17 does not yet exceed upper limit, continue and base
  966. * the r17 tuning on the false CCA count.
  967. */
  968. if ((qual->false_cca > 512) && (qual->vgc_level < up_bound))
  969. rt61pci_set_vgc(rt2x00dev, qual, ++qual->vgc_level);
  970. else if ((qual->false_cca < 100) && (qual->vgc_level > low_bound))
  971. rt61pci_set_vgc(rt2x00dev, qual, --qual->vgc_level);
  972. }
  973. /*
  974. * Queue handlers.
  975. */
  976. static void rt61pci_start_queue(struct data_queue *queue)
  977. {
  978. struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
  979. u32 reg;
  980. switch (queue->qid) {
  981. case QID_RX:
  982. rt2x00pci_register_read(rt2x00dev, TXRX_CSR0, &reg);
  983. rt2x00_set_field32(&reg, TXRX_CSR0_DISABLE_RX, 0);
  984. rt2x00pci_register_write(rt2x00dev, TXRX_CSR0, reg);
  985. break;
  986. case QID_BEACON:
  987. rt2x00pci_register_read(rt2x00dev, TXRX_CSR9, &reg);
  988. rt2x00_set_field32(&reg, TXRX_CSR9_TSF_TICKING, 1);
  989. rt2x00_set_field32(&reg, TXRX_CSR9_TBTT_ENABLE, 1);
  990. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 1);
  991. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, reg);
  992. break;
  993. default:
  994. break;
  995. }
  996. }
  997. static void rt61pci_kick_queue(struct data_queue *queue)
  998. {
  999. struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
  1000. u32 reg;
  1001. switch (queue->qid) {
  1002. case QID_AC_BE:
  1003. rt2x00pci_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1004. rt2x00_set_field32(&reg, TX_CNTL_CSR_KICK_TX_AC0, 1);
  1005. rt2x00pci_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1006. break;
  1007. case QID_AC_BK:
  1008. rt2x00pci_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1009. rt2x00_set_field32(&reg, TX_CNTL_CSR_KICK_TX_AC1, 1);
  1010. rt2x00pci_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1011. break;
  1012. case QID_AC_VI:
  1013. rt2x00pci_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1014. rt2x00_set_field32(&reg, TX_CNTL_CSR_KICK_TX_AC2, 1);
  1015. rt2x00pci_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1016. break;
  1017. case QID_AC_VO:
  1018. rt2x00pci_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1019. rt2x00_set_field32(&reg, TX_CNTL_CSR_KICK_TX_AC3, 1);
  1020. rt2x00pci_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1021. break;
  1022. default:
  1023. break;
  1024. }
  1025. }
  1026. static void rt61pci_stop_queue(struct data_queue *queue)
  1027. {
  1028. struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
  1029. u32 reg;
  1030. switch (queue->qid) {
  1031. case QID_AC_BE:
  1032. rt2x00pci_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1033. rt2x00_set_field32(&reg, TX_CNTL_CSR_ABORT_TX_AC0, 1);
  1034. rt2x00pci_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1035. break;
  1036. case QID_AC_BK:
  1037. rt2x00pci_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1038. rt2x00_set_field32(&reg, TX_CNTL_CSR_ABORT_TX_AC1, 1);
  1039. rt2x00pci_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1040. break;
  1041. case QID_AC_VI:
  1042. rt2x00pci_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1043. rt2x00_set_field32(&reg, TX_CNTL_CSR_ABORT_TX_AC2, 1);
  1044. rt2x00pci_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1045. break;
  1046. case QID_AC_VO:
  1047. rt2x00pci_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1048. rt2x00_set_field32(&reg, TX_CNTL_CSR_ABORT_TX_AC3, 1);
  1049. rt2x00pci_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1050. break;
  1051. case QID_RX:
  1052. rt2x00pci_register_read(rt2x00dev, TXRX_CSR0, &reg);
  1053. rt2x00_set_field32(&reg, TXRX_CSR0_DISABLE_RX, 1);
  1054. rt2x00pci_register_write(rt2x00dev, TXRX_CSR0, reg);
  1055. break;
  1056. case QID_BEACON:
  1057. rt2x00pci_register_read(rt2x00dev, TXRX_CSR9, &reg);
  1058. rt2x00_set_field32(&reg, TXRX_CSR9_TSF_TICKING, 0);
  1059. rt2x00_set_field32(&reg, TXRX_CSR9_TBTT_ENABLE, 0);
  1060. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 0);
  1061. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, reg);
  1062. break;
  1063. default:
  1064. break;
  1065. }
  1066. }
  1067. /*
  1068. * Firmware functions
  1069. */
  1070. static char *rt61pci_get_firmware_name(struct rt2x00_dev *rt2x00dev)
  1071. {
  1072. u16 chip;
  1073. char *fw_name;
  1074. pci_read_config_word(to_pci_dev(rt2x00dev->dev), PCI_DEVICE_ID, &chip);
  1075. switch (chip) {
  1076. case RT2561_PCI_ID:
  1077. fw_name = FIRMWARE_RT2561;
  1078. break;
  1079. case RT2561s_PCI_ID:
  1080. fw_name = FIRMWARE_RT2561s;
  1081. break;
  1082. case RT2661_PCI_ID:
  1083. fw_name = FIRMWARE_RT2661;
  1084. break;
  1085. default:
  1086. fw_name = NULL;
  1087. break;
  1088. }
  1089. return fw_name;
  1090. }
  1091. static int rt61pci_check_firmware(struct rt2x00_dev *rt2x00dev,
  1092. const u8 *data, const size_t len)
  1093. {
  1094. u16 fw_crc;
  1095. u16 crc;
  1096. /*
  1097. * Only support 8kb firmware files.
  1098. */
  1099. if (len != 8192)
  1100. return FW_BAD_LENGTH;
  1101. /*
  1102. * The last 2 bytes in the firmware array are the crc checksum itself.
  1103. * This means that we should never pass those 2 bytes to the crc
  1104. * algorithm.
  1105. */
  1106. fw_crc = (data[len - 2] << 8 | data[len - 1]);
  1107. /*
  1108. * Use the crc itu-t algorithm.
  1109. */
  1110. crc = crc_itu_t(0, data, len - 2);
  1111. crc = crc_itu_t_byte(crc, 0);
  1112. crc = crc_itu_t_byte(crc, 0);
  1113. return (fw_crc == crc) ? FW_OK : FW_BAD_CRC;
  1114. }
  1115. static int rt61pci_load_firmware(struct rt2x00_dev *rt2x00dev,
  1116. const u8 *data, const size_t len)
  1117. {
  1118. int i;
  1119. u32 reg;
  1120. /*
  1121. * Wait for stable hardware.
  1122. */
  1123. for (i = 0; i < 100; i++) {
  1124. rt2x00pci_register_read(rt2x00dev, MAC_CSR0, &reg);
  1125. if (reg)
  1126. break;
  1127. msleep(1);
  1128. }
  1129. if (!reg) {
  1130. ERROR(rt2x00dev, "Unstable hardware.\n");
  1131. return -EBUSY;
  1132. }
  1133. /*
  1134. * Prepare MCU and mailbox for firmware loading.
  1135. */
  1136. reg = 0;
  1137. rt2x00_set_field32(&reg, MCU_CNTL_CSR_RESET, 1);
  1138. rt2x00pci_register_write(rt2x00dev, MCU_CNTL_CSR, reg);
  1139. rt2x00pci_register_write(rt2x00dev, M2H_CMD_DONE_CSR, 0xffffffff);
  1140. rt2x00pci_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
  1141. rt2x00pci_register_write(rt2x00dev, HOST_CMD_CSR, 0);
  1142. /*
  1143. * Write firmware to device.
  1144. */
  1145. reg = 0;
  1146. rt2x00_set_field32(&reg, MCU_CNTL_CSR_RESET, 1);
  1147. rt2x00_set_field32(&reg, MCU_CNTL_CSR_SELECT_BANK, 1);
  1148. rt2x00pci_register_write(rt2x00dev, MCU_CNTL_CSR, reg);
  1149. rt2x00pci_register_multiwrite(rt2x00dev, FIRMWARE_IMAGE_BASE,
  1150. data, len);
  1151. rt2x00_set_field32(&reg, MCU_CNTL_CSR_SELECT_BANK, 0);
  1152. rt2x00pci_register_write(rt2x00dev, MCU_CNTL_CSR, reg);
  1153. rt2x00_set_field32(&reg, MCU_CNTL_CSR_RESET, 0);
  1154. rt2x00pci_register_write(rt2x00dev, MCU_CNTL_CSR, reg);
  1155. for (i = 0; i < 100; i++) {
  1156. rt2x00pci_register_read(rt2x00dev, MCU_CNTL_CSR, &reg);
  1157. if (rt2x00_get_field32(reg, MCU_CNTL_CSR_READY))
  1158. break;
  1159. msleep(1);
  1160. }
  1161. if (i == 100) {
  1162. ERROR(rt2x00dev, "MCU Control register not ready.\n");
  1163. return -EBUSY;
  1164. }
  1165. /*
  1166. * Hardware needs another millisecond before it is ready.
  1167. */
  1168. msleep(1);
  1169. /*
  1170. * Reset MAC and BBP registers.
  1171. */
  1172. reg = 0;
  1173. rt2x00_set_field32(&reg, MAC_CSR1_SOFT_RESET, 1);
  1174. rt2x00_set_field32(&reg, MAC_CSR1_BBP_RESET, 1);
  1175. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  1176. rt2x00pci_register_read(rt2x00dev, MAC_CSR1, &reg);
  1177. rt2x00_set_field32(&reg, MAC_CSR1_SOFT_RESET, 0);
  1178. rt2x00_set_field32(&reg, MAC_CSR1_BBP_RESET, 0);
  1179. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  1180. rt2x00pci_register_read(rt2x00dev, MAC_CSR1, &reg);
  1181. rt2x00_set_field32(&reg, MAC_CSR1_HOST_READY, 1);
  1182. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  1183. return 0;
  1184. }
  1185. /*
  1186. * Initialization functions.
  1187. */
  1188. static bool rt61pci_get_entry_state(struct queue_entry *entry)
  1189. {
  1190. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  1191. u32 word;
  1192. if (entry->queue->qid == QID_RX) {
  1193. rt2x00_desc_read(entry_priv->desc, 0, &word);
  1194. return rt2x00_get_field32(word, RXD_W0_OWNER_NIC);
  1195. } else {
  1196. rt2x00_desc_read(entry_priv->desc, 0, &word);
  1197. return (rt2x00_get_field32(word, TXD_W0_OWNER_NIC) ||
  1198. rt2x00_get_field32(word, TXD_W0_VALID));
  1199. }
  1200. }
  1201. static void rt61pci_clear_entry(struct queue_entry *entry)
  1202. {
  1203. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  1204. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  1205. u32 word;
  1206. if (entry->queue->qid == QID_RX) {
  1207. rt2x00_desc_read(entry_priv->desc, 5, &word);
  1208. rt2x00_set_field32(&word, RXD_W5_BUFFER_PHYSICAL_ADDRESS,
  1209. skbdesc->skb_dma);
  1210. rt2x00_desc_write(entry_priv->desc, 5, word);
  1211. rt2x00_desc_read(entry_priv->desc, 0, &word);
  1212. rt2x00_set_field32(&word, RXD_W0_OWNER_NIC, 1);
  1213. rt2x00_desc_write(entry_priv->desc, 0, word);
  1214. } else {
  1215. rt2x00_desc_read(entry_priv->desc, 0, &word);
  1216. rt2x00_set_field32(&word, TXD_W0_VALID, 0);
  1217. rt2x00_set_field32(&word, TXD_W0_OWNER_NIC, 0);
  1218. rt2x00_desc_write(entry_priv->desc, 0, word);
  1219. }
  1220. }
  1221. static int rt61pci_init_queues(struct rt2x00_dev *rt2x00dev)
  1222. {
  1223. struct queue_entry_priv_pci *entry_priv;
  1224. u32 reg;
  1225. /*
  1226. * Initialize registers.
  1227. */
  1228. rt2x00pci_register_read(rt2x00dev, TX_RING_CSR0, &reg);
  1229. rt2x00_set_field32(&reg, TX_RING_CSR0_AC0_RING_SIZE,
  1230. rt2x00dev->tx[0].limit);
  1231. rt2x00_set_field32(&reg, TX_RING_CSR0_AC1_RING_SIZE,
  1232. rt2x00dev->tx[1].limit);
  1233. rt2x00_set_field32(&reg, TX_RING_CSR0_AC2_RING_SIZE,
  1234. rt2x00dev->tx[2].limit);
  1235. rt2x00_set_field32(&reg, TX_RING_CSR0_AC3_RING_SIZE,
  1236. rt2x00dev->tx[3].limit);
  1237. rt2x00pci_register_write(rt2x00dev, TX_RING_CSR0, reg);
  1238. rt2x00pci_register_read(rt2x00dev, TX_RING_CSR1, &reg);
  1239. rt2x00_set_field32(&reg, TX_RING_CSR1_TXD_SIZE,
  1240. rt2x00dev->tx[0].desc_size / 4);
  1241. rt2x00pci_register_write(rt2x00dev, TX_RING_CSR1, reg);
  1242. entry_priv = rt2x00dev->tx[0].entries[0].priv_data;
  1243. rt2x00pci_register_read(rt2x00dev, AC0_BASE_CSR, &reg);
  1244. rt2x00_set_field32(&reg, AC0_BASE_CSR_RING_REGISTER,
  1245. entry_priv->desc_dma);
  1246. rt2x00pci_register_write(rt2x00dev, AC0_BASE_CSR, reg);
  1247. entry_priv = rt2x00dev->tx[1].entries[0].priv_data;
  1248. rt2x00pci_register_read(rt2x00dev, AC1_BASE_CSR, &reg);
  1249. rt2x00_set_field32(&reg, AC1_BASE_CSR_RING_REGISTER,
  1250. entry_priv->desc_dma);
  1251. rt2x00pci_register_write(rt2x00dev, AC1_BASE_CSR, reg);
  1252. entry_priv = rt2x00dev->tx[2].entries[0].priv_data;
  1253. rt2x00pci_register_read(rt2x00dev, AC2_BASE_CSR, &reg);
  1254. rt2x00_set_field32(&reg, AC2_BASE_CSR_RING_REGISTER,
  1255. entry_priv->desc_dma);
  1256. rt2x00pci_register_write(rt2x00dev, AC2_BASE_CSR, reg);
  1257. entry_priv = rt2x00dev->tx[3].entries[0].priv_data;
  1258. rt2x00pci_register_read(rt2x00dev, AC3_BASE_CSR, &reg);
  1259. rt2x00_set_field32(&reg, AC3_BASE_CSR_RING_REGISTER,
  1260. entry_priv->desc_dma);
  1261. rt2x00pci_register_write(rt2x00dev, AC3_BASE_CSR, reg);
  1262. rt2x00pci_register_read(rt2x00dev, RX_RING_CSR, &reg);
  1263. rt2x00_set_field32(&reg, RX_RING_CSR_RING_SIZE, rt2x00dev->rx->limit);
  1264. rt2x00_set_field32(&reg, RX_RING_CSR_RXD_SIZE,
  1265. rt2x00dev->rx->desc_size / 4);
  1266. rt2x00_set_field32(&reg, RX_RING_CSR_RXD_WRITEBACK_SIZE, 4);
  1267. rt2x00pci_register_write(rt2x00dev, RX_RING_CSR, reg);
  1268. entry_priv = rt2x00dev->rx->entries[0].priv_data;
  1269. rt2x00pci_register_read(rt2x00dev, RX_BASE_CSR, &reg);
  1270. rt2x00_set_field32(&reg, RX_BASE_CSR_RING_REGISTER,
  1271. entry_priv->desc_dma);
  1272. rt2x00pci_register_write(rt2x00dev, RX_BASE_CSR, reg);
  1273. rt2x00pci_register_read(rt2x00dev, TX_DMA_DST_CSR, &reg);
  1274. rt2x00_set_field32(&reg, TX_DMA_DST_CSR_DEST_AC0, 2);
  1275. rt2x00_set_field32(&reg, TX_DMA_DST_CSR_DEST_AC1, 2);
  1276. rt2x00_set_field32(&reg, TX_DMA_DST_CSR_DEST_AC2, 2);
  1277. rt2x00_set_field32(&reg, TX_DMA_DST_CSR_DEST_AC3, 2);
  1278. rt2x00pci_register_write(rt2x00dev, TX_DMA_DST_CSR, reg);
  1279. rt2x00pci_register_read(rt2x00dev, LOAD_TX_RING_CSR, &reg);
  1280. rt2x00_set_field32(&reg, LOAD_TX_RING_CSR_LOAD_TXD_AC0, 1);
  1281. rt2x00_set_field32(&reg, LOAD_TX_RING_CSR_LOAD_TXD_AC1, 1);
  1282. rt2x00_set_field32(&reg, LOAD_TX_RING_CSR_LOAD_TXD_AC2, 1);
  1283. rt2x00_set_field32(&reg, LOAD_TX_RING_CSR_LOAD_TXD_AC3, 1);
  1284. rt2x00pci_register_write(rt2x00dev, LOAD_TX_RING_CSR, reg);
  1285. rt2x00pci_register_read(rt2x00dev, RX_CNTL_CSR, &reg);
  1286. rt2x00_set_field32(&reg, RX_CNTL_CSR_LOAD_RXD, 1);
  1287. rt2x00pci_register_write(rt2x00dev, RX_CNTL_CSR, reg);
  1288. return 0;
  1289. }
  1290. static int rt61pci_init_registers(struct rt2x00_dev *rt2x00dev)
  1291. {
  1292. u32 reg;
  1293. rt2x00pci_register_read(rt2x00dev, TXRX_CSR0, &reg);
  1294. rt2x00_set_field32(&reg, TXRX_CSR0_AUTO_TX_SEQ, 1);
  1295. rt2x00_set_field32(&reg, TXRX_CSR0_DISABLE_RX, 0);
  1296. rt2x00_set_field32(&reg, TXRX_CSR0_TX_WITHOUT_WAITING, 0);
  1297. rt2x00pci_register_write(rt2x00dev, TXRX_CSR0, reg);
  1298. rt2x00pci_register_read(rt2x00dev, TXRX_CSR1, &reg);
  1299. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID0, 47); /* CCK Signal */
  1300. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID0_VALID, 1);
  1301. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID1, 30); /* Rssi */
  1302. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID1_VALID, 1);
  1303. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID2, 42); /* OFDM Rate */
  1304. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID2_VALID, 1);
  1305. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID3, 30); /* Rssi */
  1306. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID3_VALID, 1);
  1307. rt2x00pci_register_write(rt2x00dev, TXRX_CSR1, reg);
  1308. /*
  1309. * CCK TXD BBP registers
  1310. */
  1311. rt2x00pci_register_read(rt2x00dev, TXRX_CSR2, &reg);
  1312. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID0, 13);
  1313. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID0_VALID, 1);
  1314. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID1, 12);
  1315. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID1_VALID, 1);
  1316. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID2, 11);
  1317. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID2_VALID, 1);
  1318. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID3, 10);
  1319. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID3_VALID, 1);
  1320. rt2x00pci_register_write(rt2x00dev, TXRX_CSR2, reg);
  1321. /*
  1322. * OFDM TXD BBP registers
  1323. */
  1324. rt2x00pci_register_read(rt2x00dev, TXRX_CSR3, &reg);
  1325. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID0, 7);
  1326. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID0_VALID, 1);
  1327. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID1, 6);
  1328. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID1_VALID, 1);
  1329. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID2, 5);
  1330. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID2_VALID, 1);
  1331. rt2x00pci_register_write(rt2x00dev, TXRX_CSR3, reg);
  1332. rt2x00pci_register_read(rt2x00dev, TXRX_CSR7, &reg);
  1333. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_6MBS, 59);
  1334. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_9MBS, 53);
  1335. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_12MBS, 49);
  1336. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_18MBS, 46);
  1337. rt2x00pci_register_write(rt2x00dev, TXRX_CSR7, reg);
  1338. rt2x00pci_register_read(rt2x00dev, TXRX_CSR8, &reg);
  1339. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_24MBS, 44);
  1340. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_36MBS, 42);
  1341. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_48MBS, 42);
  1342. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_54MBS, 42);
  1343. rt2x00pci_register_write(rt2x00dev, TXRX_CSR8, reg);
  1344. rt2x00pci_register_read(rt2x00dev, TXRX_CSR9, &reg);
  1345. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_INTERVAL, 0);
  1346. rt2x00_set_field32(&reg, TXRX_CSR9_TSF_TICKING, 0);
  1347. rt2x00_set_field32(&reg, TXRX_CSR9_TSF_SYNC, 0);
  1348. rt2x00_set_field32(&reg, TXRX_CSR9_TBTT_ENABLE, 0);
  1349. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 0);
  1350. rt2x00_set_field32(&reg, TXRX_CSR9_TIMESTAMP_COMPENSATE, 0);
  1351. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, reg);
  1352. rt2x00pci_register_write(rt2x00dev, TXRX_CSR15, 0x0000000f);
  1353. rt2x00pci_register_write(rt2x00dev, MAC_CSR6, 0x00000fff);
  1354. rt2x00pci_register_read(rt2x00dev, MAC_CSR9, &reg);
  1355. rt2x00_set_field32(&reg, MAC_CSR9_CW_SELECT, 0);
  1356. rt2x00pci_register_write(rt2x00dev, MAC_CSR9, reg);
  1357. rt2x00pci_register_write(rt2x00dev, MAC_CSR10, 0x0000071c);
  1358. if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_AWAKE))
  1359. return -EBUSY;
  1360. rt2x00pci_register_write(rt2x00dev, MAC_CSR13, 0x0000e000);
  1361. /*
  1362. * Invalidate all Shared Keys (SEC_CSR0),
  1363. * and clear the Shared key Cipher algorithms (SEC_CSR1 & SEC_CSR5)
  1364. */
  1365. rt2x00pci_register_write(rt2x00dev, SEC_CSR0, 0x00000000);
  1366. rt2x00pci_register_write(rt2x00dev, SEC_CSR1, 0x00000000);
  1367. rt2x00pci_register_write(rt2x00dev, SEC_CSR5, 0x00000000);
  1368. rt2x00pci_register_write(rt2x00dev, PHY_CSR1, 0x000023b0);
  1369. rt2x00pci_register_write(rt2x00dev, PHY_CSR5, 0x060a100c);
  1370. rt2x00pci_register_write(rt2x00dev, PHY_CSR6, 0x00080606);
  1371. rt2x00pci_register_write(rt2x00dev, PHY_CSR7, 0x00000a08);
  1372. rt2x00pci_register_write(rt2x00dev, PCI_CFG_CSR, 0x28ca4404);
  1373. rt2x00pci_register_write(rt2x00dev, TEST_MODE_CSR, 0x00000200);
  1374. rt2x00pci_register_write(rt2x00dev, M2H_CMD_DONE_CSR, 0xffffffff);
  1375. /*
  1376. * Clear all beacons
  1377. * For the Beacon base registers we only need to clear
  1378. * the first byte since that byte contains the VALID and OWNER
  1379. * bits which (when set to 0) will invalidate the entire beacon.
  1380. */
  1381. rt2x00pci_register_write(rt2x00dev, HW_BEACON_BASE0, 0);
  1382. rt2x00pci_register_write(rt2x00dev, HW_BEACON_BASE1, 0);
  1383. rt2x00pci_register_write(rt2x00dev, HW_BEACON_BASE2, 0);
  1384. rt2x00pci_register_write(rt2x00dev, HW_BEACON_BASE3, 0);
  1385. /*
  1386. * We must clear the error counters.
  1387. * These registers are cleared on read,
  1388. * so we may pass a useless variable to store the value.
  1389. */
  1390. rt2x00pci_register_read(rt2x00dev, STA_CSR0, &reg);
  1391. rt2x00pci_register_read(rt2x00dev, STA_CSR1, &reg);
  1392. rt2x00pci_register_read(rt2x00dev, STA_CSR2, &reg);
  1393. /*
  1394. * Reset MAC and BBP registers.
  1395. */
  1396. rt2x00pci_register_read(rt2x00dev, MAC_CSR1, &reg);
  1397. rt2x00_set_field32(&reg, MAC_CSR1_SOFT_RESET, 1);
  1398. rt2x00_set_field32(&reg, MAC_CSR1_BBP_RESET, 1);
  1399. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  1400. rt2x00pci_register_read(rt2x00dev, MAC_CSR1, &reg);
  1401. rt2x00_set_field32(&reg, MAC_CSR1_SOFT_RESET, 0);
  1402. rt2x00_set_field32(&reg, MAC_CSR1_BBP_RESET, 0);
  1403. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  1404. rt2x00pci_register_read(rt2x00dev, MAC_CSR1, &reg);
  1405. rt2x00_set_field32(&reg, MAC_CSR1_HOST_READY, 1);
  1406. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  1407. return 0;
  1408. }
  1409. static int rt61pci_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
  1410. {
  1411. unsigned int i;
  1412. u8 value;
  1413. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  1414. rt61pci_bbp_read(rt2x00dev, 0, &value);
  1415. if ((value != 0xff) && (value != 0x00))
  1416. return 0;
  1417. udelay(REGISTER_BUSY_DELAY);
  1418. }
  1419. ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
  1420. return -EACCES;
  1421. }
  1422. static int rt61pci_init_bbp(struct rt2x00_dev *rt2x00dev)
  1423. {
  1424. unsigned int i;
  1425. u16 eeprom;
  1426. u8 reg_id;
  1427. u8 value;
  1428. if (unlikely(rt61pci_wait_bbp_ready(rt2x00dev)))
  1429. return -EACCES;
  1430. rt61pci_bbp_write(rt2x00dev, 3, 0x00);
  1431. rt61pci_bbp_write(rt2x00dev, 15, 0x30);
  1432. rt61pci_bbp_write(rt2x00dev, 21, 0xc8);
  1433. rt61pci_bbp_write(rt2x00dev, 22, 0x38);
  1434. rt61pci_bbp_write(rt2x00dev, 23, 0x06);
  1435. rt61pci_bbp_write(rt2x00dev, 24, 0xfe);
  1436. rt61pci_bbp_write(rt2x00dev, 25, 0x0a);
  1437. rt61pci_bbp_write(rt2x00dev, 26, 0x0d);
  1438. rt61pci_bbp_write(rt2x00dev, 34, 0x12);
  1439. rt61pci_bbp_write(rt2x00dev, 37, 0x07);
  1440. rt61pci_bbp_write(rt2x00dev, 39, 0xf8);
  1441. rt61pci_bbp_write(rt2x00dev, 41, 0x60);
  1442. rt61pci_bbp_write(rt2x00dev, 53, 0x10);
  1443. rt61pci_bbp_write(rt2x00dev, 54, 0x18);
  1444. rt61pci_bbp_write(rt2x00dev, 60, 0x10);
  1445. rt61pci_bbp_write(rt2x00dev, 61, 0x04);
  1446. rt61pci_bbp_write(rt2x00dev, 62, 0x04);
  1447. rt61pci_bbp_write(rt2x00dev, 75, 0xfe);
  1448. rt61pci_bbp_write(rt2x00dev, 86, 0xfe);
  1449. rt61pci_bbp_write(rt2x00dev, 88, 0xfe);
  1450. rt61pci_bbp_write(rt2x00dev, 90, 0x0f);
  1451. rt61pci_bbp_write(rt2x00dev, 99, 0x00);
  1452. rt61pci_bbp_write(rt2x00dev, 102, 0x16);
  1453. rt61pci_bbp_write(rt2x00dev, 107, 0x04);
  1454. for (i = 0; i < EEPROM_BBP_SIZE; i++) {
  1455. rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
  1456. if (eeprom != 0xffff && eeprom != 0x0000) {
  1457. reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
  1458. value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
  1459. rt61pci_bbp_write(rt2x00dev, reg_id, value);
  1460. }
  1461. }
  1462. return 0;
  1463. }
  1464. /*
  1465. * Device state switch handlers.
  1466. */
  1467. static void rt61pci_toggle_irq(struct rt2x00_dev *rt2x00dev,
  1468. enum dev_state state)
  1469. {
  1470. int mask = (state == STATE_RADIO_IRQ_OFF) ||
  1471. (state == STATE_RADIO_IRQ_OFF_ISR);
  1472. u32 reg;
  1473. /*
  1474. * When interrupts are being enabled, the interrupt registers
  1475. * should clear the register to assure a clean state.
  1476. */
  1477. if (state == STATE_RADIO_IRQ_ON) {
  1478. rt2x00pci_register_read(rt2x00dev, INT_SOURCE_CSR, &reg);
  1479. rt2x00pci_register_write(rt2x00dev, INT_SOURCE_CSR, reg);
  1480. rt2x00pci_register_read(rt2x00dev, MCU_INT_SOURCE_CSR, &reg);
  1481. rt2x00pci_register_write(rt2x00dev, MCU_INT_SOURCE_CSR, reg);
  1482. }
  1483. /*
  1484. * Only toggle the interrupts bits we are going to use.
  1485. * Non-checked interrupt bits are disabled by default.
  1486. */
  1487. rt2x00pci_register_read(rt2x00dev, INT_MASK_CSR, &reg);
  1488. rt2x00_set_field32(&reg, INT_MASK_CSR_TXDONE, mask);
  1489. rt2x00_set_field32(&reg, INT_MASK_CSR_RXDONE, mask);
  1490. rt2x00_set_field32(&reg, INT_MASK_CSR_BEACON_DONE, mask);
  1491. rt2x00_set_field32(&reg, INT_MASK_CSR_ENABLE_MITIGATION, mask);
  1492. rt2x00_set_field32(&reg, INT_MASK_CSR_MITIGATION_PERIOD, 0xff);
  1493. rt2x00pci_register_write(rt2x00dev, INT_MASK_CSR, reg);
  1494. rt2x00pci_register_read(rt2x00dev, MCU_INT_MASK_CSR, &reg);
  1495. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_0, mask);
  1496. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_1, mask);
  1497. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_2, mask);
  1498. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_3, mask);
  1499. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_4, mask);
  1500. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_5, mask);
  1501. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_6, mask);
  1502. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_7, mask);
  1503. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_TWAKEUP, mask);
  1504. rt2x00pci_register_write(rt2x00dev, MCU_INT_MASK_CSR, reg);
  1505. }
  1506. static int rt61pci_enable_radio(struct rt2x00_dev *rt2x00dev)
  1507. {
  1508. u32 reg;
  1509. /*
  1510. * Initialize all registers.
  1511. */
  1512. if (unlikely(rt61pci_init_queues(rt2x00dev) ||
  1513. rt61pci_init_registers(rt2x00dev) ||
  1514. rt61pci_init_bbp(rt2x00dev)))
  1515. return -EIO;
  1516. /*
  1517. * Enable RX.
  1518. */
  1519. rt2x00pci_register_read(rt2x00dev, RX_CNTL_CSR, &reg);
  1520. rt2x00_set_field32(&reg, RX_CNTL_CSR_ENABLE_RX_DMA, 1);
  1521. rt2x00pci_register_write(rt2x00dev, RX_CNTL_CSR, reg);
  1522. return 0;
  1523. }
  1524. static void rt61pci_disable_radio(struct rt2x00_dev *rt2x00dev)
  1525. {
  1526. /*
  1527. * Disable power
  1528. */
  1529. rt2x00pci_register_write(rt2x00dev, MAC_CSR10, 0x00001818);
  1530. }
  1531. static int rt61pci_set_state(struct rt2x00_dev *rt2x00dev, enum dev_state state)
  1532. {
  1533. u32 reg, reg2;
  1534. unsigned int i;
  1535. char put_to_sleep;
  1536. put_to_sleep = (state != STATE_AWAKE);
  1537. rt2x00pci_register_read(rt2x00dev, MAC_CSR12, &reg);
  1538. rt2x00_set_field32(&reg, MAC_CSR12_FORCE_WAKEUP, !put_to_sleep);
  1539. rt2x00_set_field32(&reg, MAC_CSR12_PUT_TO_SLEEP, put_to_sleep);
  1540. rt2x00pci_register_write(rt2x00dev, MAC_CSR12, reg);
  1541. /*
  1542. * Device is not guaranteed to be in the requested state yet.
  1543. * We must wait until the register indicates that the
  1544. * device has entered the correct state.
  1545. */
  1546. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  1547. rt2x00pci_register_read(rt2x00dev, MAC_CSR12, &reg2);
  1548. state = rt2x00_get_field32(reg2, MAC_CSR12_BBP_CURRENT_STATE);
  1549. if (state == !put_to_sleep)
  1550. return 0;
  1551. rt2x00pci_register_write(rt2x00dev, MAC_CSR12, reg);
  1552. msleep(10);
  1553. }
  1554. return -EBUSY;
  1555. }
  1556. static int rt61pci_set_device_state(struct rt2x00_dev *rt2x00dev,
  1557. enum dev_state state)
  1558. {
  1559. int retval = 0;
  1560. switch (state) {
  1561. case STATE_RADIO_ON:
  1562. retval = rt61pci_enable_radio(rt2x00dev);
  1563. break;
  1564. case STATE_RADIO_OFF:
  1565. rt61pci_disable_radio(rt2x00dev);
  1566. break;
  1567. case STATE_RADIO_RX_ON:
  1568. rt61pci_start_queue(rt2x00dev->rx);
  1569. break;
  1570. case STATE_RADIO_RX_OFF:
  1571. rt61pci_stop_queue(rt2x00dev->rx);
  1572. break;
  1573. case STATE_RADIO_IRQ_ON:
  1574. case STATE_RADIO_IRQ_ON_ISR:
  1575. case STATE_RADIO_IRQ_OFF:
  1576. case STATE_RADIO_IRQ_OFF_ISR:
  1577. rt61pci_toggle_irq(rt2x00dev, state);
  1578. break;
  1579. case STATE_DEEP_SLEEP:
  1580. case STATE_SLEEP:
  1581. case STATE_STANDBY:
  1582. case STATE_AWAKE:
  1583. retval = rt61pci_set_state(rt2x00dev, state);
  1584. break;
  1585. default:
  1586. retval = -ENOTSUPP;
  1587. break;
  1588. }
  1589. if (unlikely(retval))
  1590. ERROR(rt2x00dev, "Device failed to enter state %d (%d).\n",
  1591. state, retval);
  1592. return retval;
  1593. }
  1594. /*
  1595. * TX descriptor initialization
  1596. */
  1597. static void rt61pci_write_tx_desc(struct queue_entry *entry,
  1598. struct txentry_desc *txdesc)
  1599. {
  1600. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  1601. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  1602. __le32 *txd = entry_priv->desc;
  1603. u32 word;
  1604. /*
  1605. * Start writing the descriptor words.
  1606. */
  1607. rt2x00_desc_read(txd, 1, &word);
  1608. rt2x00_set_field32(&word, TXD_W1_HOST_Q_ID, entry->queue->qid);
  1609. rt2x00_set_field32(&word, TXD_W1_AIFSN, entry->queue->aifs);
  1610. rt2x00_set_field32(&word, TXD_W1_CWMIN, entry->queue->cw_min);
  1611. rt2x00_set_field32(&word, TXD_W1_CWMAX, entry->queue->cw_max);
  1612. rt2x00_set_field32(&word, TXD_W1_IV_OFFSET, txdesc->iv_offset);
  1613. rt2x00_set_field32(&word, TXD_W1_HW_SEQUENCE,
  1614. test_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags));
  1615. rt2x00_set_field32(&word, TXD_W1_BUFFER_COUNT, 1);
  1616. rt2x00_desc_write(txd, 1, word);
  1617. rt2x00_desc_read(txd, 2, &word);
  1618. rt2x00_set_field32(&word, TXD_W2_PLCP_SIGNAL, txdesc->signal);
  1619. rt2x00_set_field32(&word, TXD_W2_PLCP_SERVICE, txdesc->service);
  1620. rt2x00_set_field32(&word, TXD_W2_PLCP_LENGTH_LOW, txdesc->length_low);
  1621. rt2x00_set_field32(&word, TXD_W2_PLCP_LENGTH_HIGH, txdesc->length_high);
  1622. rt2x00_desc_write(txd, 2, word);
  1623. if (test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags)) {
  1624. _rt2x00_desc_write(txd, 3, skbdesc->iv[0]);
  1625. _rt2x00_desc_write(txd, 4, skbdesc->iv[1]);
  1626. }
  1627. rt2x00_desc_read(txd, 5, &word);
  1628. rt2x00_set_field32(&word, TXD_W5_PID_TYPE, entry->queue->qid);
  1629. rt2x00_set_field32(&word, TXD_W5_PID_SUBTYPE,
  1630. skbdesc->entry->entry_idx);
  1631. rt2x00_set_field32(&word, TXD_W5_TX_POWER,
  1632. TXPOWER_TO_DEV(entry->queue->rt2x00dev->tx_power));
  1633. rt2x00_set_field32(&word, TXD_W5_WAITING_DMA_DONE_INT, 1);
  1634. rt2x00_desc_write(txd, 5, word);
  1635. if (entry->queue->qid != QID_BEACON) {
  1636. rt2x00_desc_read(txd, 6, &word);
  1637. rt2x00_set_field32(&word, TXD_W6_BUFFER_PHYSICAL_ADDRESS,
  1638. skbdesc->skb_dma);
  1639. rt2x00_desc_write(txd, 6, word);
  1640. rt2x00_desc_read(txd, 11, &word);
  1641. rt2x00_set_field32(&word, TXD_W11_BUFFER_LENGTH0,
  1642. txdesc->length);
  1643. rt2x00_desc_write(txd, 11, word);
  1644. }
  1645. /*
  1646. * Writing TXD word 0 must the last to prevent a race condition with
  1647. * the device, whereby the device may take hold of the TXD before we
  1648. * finished updating it.
  1649. */
  1650. rt2x00_desc_read(txd, 0, &word);
  1651. rt2x00_set_field32(&word, TXD_W0_OWNER_NIC, 1);
  1652. rt2x00_set_field32(&word, TXD_W0_VALID, 1);
  1653. rt2x00_set_field32(&word, TXD_W0_MORE_FRAG,
  1654. test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
  1655. rt2x00_set_field32(&word, TXD_W0_ACK,
  1656. test_bit(ENTRY_TXD_ACK, &txdesc->flags));
  1657. rt2x00_set_field32(&word, TXD_W0_TIMESTAMP,
  1658. test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
  1659. rt2x00_set_field32(&word, TXD_W0_OFDM,
  1660. (txdesc->rate_mode == RATE_MODE_OFDM));
  1661. rt2x00_set_field32(&word, TXD_W0_IFS, txdesc->ifs);
  1662. rt2x00_set_field32(&word, TXD_W0_RETRY_MODE,
  1663. test_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags));
  1664. rt2x00_set_field32(&word, TXD_W0_TKIP_MIC,
  1665. test_bit(ENTRY_TXD_ENCRYPT_MMIC, &txdesc->flags));
  1666. rt2x00_set_field32(&word, TXD_W0_KEY_TABLE,
  1667. test_bit(ENTRY_TXD_ENCRYPT_PAIRWISE, &txdesc->flags));
  1668. rt2x00_set_field32(&word, TXD_W0_KEY_INDEX, txdesc->key_idx);
  1669. rt2x00_set_field32(&word, TXD_W0_DATABYTE_COUNT, txdesc->length);
  1670. rt2x00_set_field32(&word, TXD_W0_BURST,
  1671. test_bit(ENTRY_TXD_BURST, &txdesc->flags));
  1672. rt2x00_set_field32(&word, TXD_W0_CIPHER_ALG, txdesc->cipher);
  1673. rt2x00_desc_write(txd, 0, word);
  1674. /*
  1675. * Register descriptor details in skb frame descriptor.
  1676. */
  1677. skbdesc->desc = txd;
  1678. skbdesc->desc_len = (entry->queue->qid == QID_BEACON) ? TXINFO_SIZE :
  1679. TXD_DESC_SIZE;
  1680. }
  1681. /*
  1682. * TX data initialization
  1683. */
  1684. static void rt61pci_write_beacon(struct queue_entry *entry,
  1685. struct txentry_desc *txdesc)
  1686. {
  1687. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  1688. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  1689. unsigned int beacon_base;
  1690. u32 reg;
  1691. /*
  1692. * Disable beaconing while we are reloading the beacon data,
  1693. * otherwise we might be sending out invalid data.
  1694. */
  1695. rt2x00pci_register_read(rt2x00dev, TXRX_CSR9, &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 to register.
  1708. */
  1709. beacon_base = HW_BEACON_OFFSET(entry->entry_idx);
  1710. rt2x00pci_register_multiwrite(rt2x00dev, beacon_base,
  1711. entry_priv->desc, TXINFO_SIZE);
  1712. rt2x00pci_register_multiwrite(rt2x00dev, beacon_base + TXINFO_SIZE,
  1713. entry->skb->data, entry->skb->len);
  1714. /*
  1715. * Enable beaconing again.
  1716. *
  1717. * For Wi-Fi faily generated beacons between participating
  1718. * stations. Set TBTT phase adaptive adjustment step to 8us.
  1719. */
  1720. rt2x00pci_register_write(rt2x00dev, TXRX_CSR10, 0x00001008);
  1721. rt2x00_set_field32(&reg, TXRX_CSR9_TSF_TICKING, 1);
  1722. rt2x00_set_field32(&reg, TXRX_CSR9_TBTT_ENABLE, 1);
  1723. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 1);
  1724. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, reg);
  1725. /*
  1726. * Clean up beacon skb.
  1727. */
  1728. dev_kfree_skb_any(entry->skb);
  1729. entry->skb = NULL;
  1730. }
  1731. /*
  1732. * RX control handlers
  1733. */
  1734. static int rt61pci_agc_to_rssi(struct rt2x00_dev *rt2x00dev, int rxd_w1)
  1735. {
  1736. u8 offset = rt2x00dev->lna_gain;
  1737. u8 lna;
  1738. lna = rt2x00_get_field32(rxd_w1, RXD_W1_RSSI_LNA);
  1739. switch (lna) {
  1740. case 3:
  1741. offset += 90;
  1742. break;
  1743. case 2:
  1744. offset += 74;
  1745. break;
  1746. case 1:
  1747. offset += 64;
  1748. break;
  1749. default:
  1750. return 0;
  1751. }
  1752. if (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ) {
  1753. if (lna == 3 || lna == 2)
  1754. offset += 10;
  1755. }
  1756. return rt2x00_get_field32(rxd_w1, RXD_W1_RSSI_AGC) * 2 - offset;
  1757. }
  1758. static void rt61pci_fill_rxdone(struct queue_entry *entry,
  1759. struct rxdone_entry_desc *rxdesc)
  1760. {
  1761. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  1762. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  1763. u32 word0;
  1764. u32 word1;
  1765. rt2x00_desc_read(entry_priv->desc, 0, &word0);
  1766. rt2x00_desc_read(entry_priv->desc, 1, &word1);
  1767. if (rt2x00_get_field32(word0, RXD_W0_CRC_ERROR))
  1768. rxdesc->flags |= RX_FLAG_FAILED_FCS_CRC;
  1769. rxdesc->cipher = rt2x00_get_field32(word0, RXD_W0_CIPHER_ALG);
  1770. rxdesc->cipher_status = rt2x00_get_field32(word0, RXD_W0_CIPHER_ERROR);
  1771. if (rxdesc->cipher != CIPHER_NONE) {
  1772. _rt2x00_desc_read(entry_priv->desc, 2, &rxdesc->iv[0]);
  1773. _rt2x00_desc_read(entry_priv->desc, 3, &rxdesc->iv[1]);
  1774. rxdesc->dev_flags |= RXDONE_CRYPTO_IV;
  1775. _rt2x00_desc_read(entry_priv->desc, 4, &rxdesc->icv);
  1776. rxdesc->dev_flags |= RXDONE_CRYPTO_ICV;
  1777. /*
  1778. * Hardware has stripped IV/EIV data from 802.11 frame during
  1779. * decryption. It has provided the data separately but rt2x00lib
  1780. * should decide if it should be reinserted.
  1781. */
  1782. rxdesc->flags |= RX_FLAG_IV_STRIPPED;
  1783. /*
  1784. * FIXME: Legacy driver indicates that the frame does
  1785. * contain the Michael Mic. Unfortunately, in rt2x00
  1786. * the MIC seems to be missing completely...
  1787. */
  1788. rxdesc->flags |= RX_FLAG_MMIC_STRIPPED;
  1789. if (rxdesc->cipher_status == RX_CRYPTO_SUCCESS)
  1790. rxdesc->flags |= RX_FLAG_DECRYPTED;
  1791. else if (rxdesc->cipher_status == RX_CRYPTO_FAIL_MIC)
  1792. rxdesc->flags |= RX_FLAG_MMIC_ERROR;
  1793. }
  1794. /*
  1795. * Obtain the status about this packet.
  1796. * When frame was received with an OFDM bitrate,
  1797. * the signal is the PLCP value. If it was received with
  1798. * a CCK bitrate the signal is the rate in 100kbit/s.
  1799. */
  1800. rxdesc->signal = rt2x00_get_field32(word1, RXD_W1_SIGNAL);
  1801. rxdesc->rssi = rt61pci_agc_to_rssi(rt2x00dev, word1);
  1802. rxdesc->size = rt2x00_get_field32(word0, RXD_W0_DATABYTE_COUNT);
  1803. if (rt2x00_get_field32(word0, RXD_W0_OFDM))
  1804. rxdesc->dev_flags |= RXDONE_SIGNAL_PLCP;
  1805. else
  1806. rxdesc->dev_flags |= RXDONE_SIGNAL_BITRATE;
  1807. if (rt2x00_get_field32(word0, RXD_W0_MY_BSS))
  1808. rxdesc->dev_flags |= RXDONE_MY_BSS;
  1809. }
  1810. /*
  1811. * Interrupt functions.
  1812. */
  1813. static void rt61pci_txdone(struct rt2x00_dev *rt2x00dev)
  1814. {
  1815. struct data_queue *queue;
  1816. struct queue_entry *entry;
  1817. struct queue_entry *entry_done;
  1818. struct queue_entry_priv_pci *entry_priv;
  1819. struct txdone_entry_desc txdesc;
  1820. u32 word;
  1821. u32 reg;
  1822. int type;
  1823. int index;
  1824. int i;
  1825. /*
  1826. * TX_STA_FIFO is a stack of X entries, hence read TX_STA_FIFO
  1827. * at most X times and also stop processing once the TX_STA_FIFO_VALID
  1828. * flag is not set anymore.
  1829. *
  1830. * The legacy drivers use X=TX_RING_SIZE but state in a comment
  1831. * that the TX_STA_FIFO stack has a size of 16. We stick to our
  1832. * tx ring size for now.
  1833. */
  1834. for (i = 0; i < rt2x00dev->ops->tx->entry_num; i++) {
  1835. rt2x00pci_register_read(rt2x00dev, STA_CSR4, &reg);
  1836. if (!rt2x00_get_field32(reg, STA_CSR4_VALID))
  1837. break;
  1838. /*
  1839. * Skip this entry when it contains an invalid
  1840. * queue identication number.
  1841. */
  1842. type = rt2x00_get_field32(reg, STA_CSR4_PID_TYPE);
  1843. queue = rt2x00queue_get_queue(rt2x00dev, type);
  1844. if (unlikely(!queue))
  1845. continue;
  1846. /*
  1847. * Skip this entry when it contains an invalid
  1848. * index number.
  1849. */
  1850. index = rt2x00_get_field32(reg, STA_CSR4_PID_SUBTYPE);
  1851. if (unlikely(index >= queue->limit))
  1852. continue;
  1853. entry = &queue->entries[index];
  1854. entry_priv = entry->priv_data;
  1855. rt2x00_desc_read(entry_priv->desc, 0, &word);
  1856. if (rt2x00_get_field32(word, TXD_W0_OWNER_NIC) ||
  1857. !rt2x00_get_field32(word, TXD_W0_VALID))
  1858. return;
  1859. entry_done = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
  1860. while (entry != entry_done) {
  1861. /* Catch up.
  1862. * Just report any entries we missed as failed.
  1863. */
  1864. WARNING(rt2x00dev,
  1865. "TX status report missed for entry %d\n",
  1866. entry_done->entry_idx);
  1867. rt2x00lib_txdone_noinfo(entry_done, TXDONE_UNKNOWN);
  1868. entry_done = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
  1869. }
  1870. /*
  1871. * Obtain the status about this packet.
  1872. */
  1873. txdesc.flags = 0;
  1874. switch (rt2x00_get_field32(reg, STA_CSR4_TX_RESULT)) {
  1875. case 0: /* Success, maybe with retry */
  1876. __set_bit(TXDONE_SUCCESS, &txdesc.flags);
  1877. break;
  1878. case 6: /* Failure, excessive retries */
  1879. __set_bit(TXDONE_EXCESSIVE_RETRY, &txdesc.flags);
  1880. /* Don't break, this is a failed frame! */
  1881. default: /* Failure */
  1882. __set_bit(TXDONE_FAILURE, &txdesc.flags);
  1883. }
  1884. txdesc.retry = rt2x00_get_field32(reg, STA_CSR4_RETRY_COUNT);
  1885. /*
  1886. * the frame was retried at least once
  1887. * -> hw used fallback rates
  1888. */
  1889. if (txdesc.retry)
  1890. __set_bit(TXDONE_FALLBACK, &txdesc.flags);
  1891. rt2x00lib_txdone(entry, &txdesc);
  1892. }
  1893. }
  1894. static void rt61pci_wakeup(struct rt2x00_dev *rt2x00dev)
  1895. {
  1896. struct ieee80211_conf conf = { .flags = 0 };
  1897. struct rt2x00lib_conf libconf = { .conf = &conf };
  1898. rt61pci_config(rt2x00dev, &libconf, IEEE80211_CONF_CHANGE_PS);
  1899. }
  1900. static irqreturn_t rt61pci_interrupt_thread(int irq, void *dev_instance)
  1901. {
  1902. struct rt2x00_dev *rt2x00dev = dev_instance;
  1903. u32 reg = rt2x00dev->irqvalue[0];
  1904. u32 reg_mcu = rt2x00dev->irqvalue[1];
  1905. /*
  1906. * Handle interrupts, walk through all bits
  1907. * and run the tasks, the bits are checked in order of
  1908. * priority.
  1909. */
  1910. /*
  1911. * 1 - Rx ring done interrupt.
  1912. */
  1913. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_RXDONE))
  1914. rt2x00pci_rxdone(rt2x00dev);
  1915. /*
  1916. * 2 - Tx ring done interrupt.
  1917. */
  1918. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_TXDONE))
  1919. rt61pci_txdone(rt2x00dev);
  1920. /*
  1921. * 3 - Handle MCU command done.
  1922. */
  1923. if (reg_mcu)
  1924. rt2x00pci_register_write(rt2x00dev,
  1925. M2H_CMD_DONE_CSR, 0xffffffff);
  1926. /*
  1927. * 4 - MCU Autowakeup interrupt.
  1928. */
  1929. if (rt2x00_get_field32(reg_mcu, MCU_INT_SOURCE_CSR_TWAKEUP))
  1930. rt61pci_wakeup(rt2x00dev);
  1931. /*
  1932. * 5 - Beacon done interrupt.
  1933. */
  1934. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_BEACON_DONE))
  1935. rt2x00lib_beacondone(rt2x00dev);
  1936. /* Enable interrupts again. */
  1937. rt2x00dev->ops->lib->set_device_state(rt2x00dev,
  1938. STATE_RADIO_IRQ_ON_ISR);
  1939. return IRQ_HANDLED;
  1940. }
  1941. static irqreturn_t rt61pci_interrupt(int irq, void *dev_instance)
  1942. {
  1943. struct rt2x00_dev *rt2x00dev = dev_instance;
  1944. u32 reg_mcu;
  1945. u32 reg;
  1946. /*
  1947. * Get the interrupt sources & saved to local variable.
  1948. * Write register value back to clear pending interrupts.
  1949. */
  1950. rt2x00pci_register_read(rt2x00dev, MCU_INT_SOURCE_CSR, &reg_mcu);
  1951. rt2x00pci_register_write(rt2x00dev, MCU_INT_SOURCE_CSR, reg_mcu);
  1952. rt2x00pci_register_read(rt2x00dev, INT_SOURCE_CSR, &reg);
  1953. rt2x00pci_register_write(rt2x00dev, INT_SOURCE_CSR, reg);
  1954. if (!reg && !reg_mcu)
  1955. return IRQ_NONE;
  1956. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  1957. return IRQ_HANDLED;
  1958. /* Store irqvalues for use in the interrupt thread. */
  1959. rt2x00dev->irqvalue[0] = reg;
  1960. rt2x00dev->irqvalue[1] = reg_mcu;
  1961. /* Disable interrupts, will be enabled again in the interrupt thread. */
  1962. rt2x00dev->ops->lib->set_device_state(rt2x00dev,
  1963. STATE_RADIO_IRQ_OFF_ISR);
  1964. return IRQ_WAKE_THREAD;
  1965. }
  1966. /*
  1967. * Device probe functions.
  1968. */
  1969. static int rt61pci_validate_eeprom(struct rt2x00_dev *rt2x00dev)
  1970. {
  1971. struct eeprom_93cx6 eeprom;
  1972. u32 reg;
  1973. u16 word;
  1974. u8 *mac;
  1975. s8 value;
  1976. rt2x00pci_register_read(rt2x00dev, E2PROM_CSR, &reg);
  1977. eeprom.data = rt2x00dev;
  1978. eeprom.register_read = rt61pci_eepromregister_read;
  1979. eeprom.register_write = rt61pci_eepromregister_write;
  1980. eeprom.width = rt2x00_get_field32(reg, E2PROM_CSR_TYPE_93C46) ?
  1981. PCI_EEPROM_WIDTH_93C46 : PCI_EEPROM_WIDTH_93C66;
  1982. eeprom.reg_data_in = 0;
  1983. eeprom.reg_data_out = 0;
  1984. eeprom.reg_data_clock = 0;
  1985. eeprom.reg_chip_select = 0;
  1986. eeprom_93cx6_multiread(&eeprom, EEPROM_BASE, rt2x00dev->eeprom,
  1987. EEPROM_SIZE / sizeof(u16));
  1988. /*
  1989. * Start validation of the data that has been read.
  1990. */
  1991. mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
  1992. if (!is_valid_ether_addr(mac)) {
  1993. random_ether_addr(mac);
  1994. EEPROM(rt2x00dev, "MAC: %pM\n", mac);
  1995. }
  1996. rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &word);
  1997. if (word == 0xffff) {
  1998. rt2x00_set_field16(&word, EEPROM_ANTENNA_NUM, 2);
  1999. rt2x00_set_field16(&word, EEPROM_ANTENNA_TX_DEFAULT,
  2000. ANTENNA_B);
  2001. rt2x00_set_field16(&word, EEPROM_ANTENNA_RX_DEFAULT,
  2002. ANTENNA_B);
  2003. rt2x00_set_field16(&word, EEPROM_ANTENNA_FRAME_TYPE, 0);
  2004. rt2x00_set_field16(&word, EEPROM_ANTENNA_DYN_TXAGC, 0);
  2005. rt2x00_set_field16(&word, EEPROM_ANTENNA_HARDWARE_RADIO, 0);
  2006. rt2x00_set_field16(&word, EEPROM_ANTENNA_RF_TYPE, RF5225);
  2007. rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
  2008. EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
  2009. }
  2010. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &word);
  2011. if (word == 0xffff) {
  2012. rt2x00_set_field16(&word, EEPROM_NIC_ENABLE_DIVERSITY, 0);
  2013. rt2x00_set_field16(&word, EEPROM_NIC_TX_DIVERSITY, 0);
  2014. rt2x00_set_field16(&word, EEPROM_NIC_RX_FIXED, 0);
  2015. rt2x00_set_field16(&word, EEPROM_NIC_TX_FIXED, 0);
  2016. rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_BG, 0);
  2017. rt2x00_set_field16(&word, EEPROM_NIC_CARDBUS_ACCEL, 0);
  2018. rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_A, 0);
  2019. rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC, word);
  2020. EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
  2021. }
  2022. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED, &word);
  2023. if (word == 0xffff) {
  2024. rt2x00_set_field16(&word, EEPROM_LED_LED_MODE,
  2025. LED_MODE_DEFAULT);
  2026. rt2x00_eeprom_write(rt2x00dev, EEPROM_LED, word);
  2027. EEPROM(rt2x00dev, "Led: 0x%04x\n", word);
  2028. }
  2029. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
  2030. if (word == 0xffff) {
  2031. rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
  2032. rt2x00_set_field16(&word, EEPROM_FREQ_SEQ, 0);
  2033. rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
  2034. EEPROM(rt2x00dev, "Freq: 0x%04x\n", word);
  2035. }
  2036. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_BG, &word);
  2037. if (word == 0xffff) {
  2038. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_1, 0);
  2039. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_2, 0);
  2040. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_BG, word);
  2041. EEPROM(rt2x00dev, "RSSI OFFSET BG: 0x%04x\n", word);
  2042. } else {
  2043. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_BG_1);
  2044. if (value < -10 || value > 10)
  2045. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_1, 0);
  2046. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_BG_2);
  2047. if (value < -10 || value > 10)
  2048. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_2, 0);
  2049. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_BG, word);
  2050. }
  2051. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_A, &word);
  2052. if (word == 0xffff) {
  2053. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_1, 0);
  2054. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_2, 0);
  2055. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_A, word);
  2056. EEPROM(rt2x00dev, "RSSI OFFSET A: 0x%04x\n", word);
  2057. } else {
  2058. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_A_1);
  2059. if (value < -10 || value > 10)
  2060. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_1, 0);
  2061. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_A_2);
  2062. if (value < -10 || value > 10)
  2063. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_2, 0);
  2064. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_A, word);
  2065. }
  2066. return 0;
  2067. }
  2068. static int rt61pci_init_eeprom(struct rt2x00_dev *rt2x00dev)
  2069. {
  2070. u32 reg;
  2071. u16 value;
  2072. u16 eeprom;
  2073. /*
  2074. * Read EEPROM word for configuration.
  2075. */
  2076. rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
  2077. /*
  2078. * Identify RF chipset.
  2079. */
  2080. value = rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RF_TYPE);
  2081. rt2x00pci_register_read(rt2x00dev, MAC_CSR0, &reg);
  2082. rt2x00_set_chip(rt2x00dev, rt2x00_get_field32(reg, MAC_CSR0_CHIPSET),
  2083. value, rt2x00_get_field32(reg, MAC_CSR0_REVISION));
  2084. if (!rt2x00_rf(rt2x00dev, RF5225) &&
  2085. !rt2x00_rf(rt2x00dev, RF5325) &&
  2086. !rt2x00_rf(rt2x00dev, RF2527) &&
  2087. !rt2x00_rf(rt2x00dev, RF2529)) {
  2088. ERROR(rt2x00dev, "Invalid RF chipset detected.\n");
  2089. return -ENODEV;
  2090. }
  2091. /*
  2092. * Determine number of antennas.
  2093. */
  2094. if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_NUM) == 2)
  2095. __set_bit(CONFIG_DOUBLE_ANTENNA, &rt2x00dev->flags);
  2096. /*
  2097. * Identify default antenna configuration.
  2098. */
  2099. rt2x00dev->default_ant.tx =
  2100. rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TX_DEFAULT);
  2101. rt2x00dev->default_ant.rx =
  2102. rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RX_DEFAULT);
  2103. /*
  2104. * Read the Frame type.
  2105. */
  2106. if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_FRAME_TYPE))
  2107. __set_bit(CONFIG_FRAME_TYPE, &rt2x00dev->flags);
  2108. /*
  2109. * Detect if this device has a hardware controlled radio.
  2110. */
  2111. if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_HARDWARE_RADIO))
  2112. __set_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags);
  2113. /*
  2114. * Read frequency offset and RF programming sequence.
  2115. */
  2116. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
  2117. if (rt2x00_get_field16(eeprom, EEPROM_FREQ_SEQ))
  2118. __set_bit(CONFIG_RF_SEQUENCE, &rt2x00dev->flags);
  2119. rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
  2120. /*
  2121. * Read external LNA informations.
  2122. */
  2123. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
  2124. if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_A))
  2125. __set_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags);
  2126. if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_BG))
  2127. __set_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags);
  2128. /*
  2129. * When working with a RF2529 chip without double antenna,
  2130. * the antenna settings should be gathered from the NIC
  2131. * eeprom word.
  2132. */
  2133. if (rt2x00_rf(rt2x00dev, RF2529) &&
  2134. !test_bit(CONFIG_DOUBLE_ANTENNA, &rt2x00dev->flags)) {
  2135. rt2x00dev->default_ant.rx =
  2136. ANTENNA_A + rt2x00_get_field16(eeprom, EEPROM_NIC_RX_FIXED);
  2137. rt2x00dev->default_ant.tx =
  2138. ANTENNA_B - rt2x00_get_field16(eeprom, EEPROM_NIC_TX_FIXED);
  2139. if (rt2x00_get_field16(eeprom, EEPROM_NIC_TX_DIVERSITY))
  2140. rt2x00dev->default_ant.tx = ANTENNA_SW_DIVERSITY;
  2141. if (rt2x00_get_field16(eeprom, EEPROM_NIC_ENABLE_DIVERSITY))
  2142. rt2x00dev->default_ant.rx = ANTENNA_SW_DIVERSITY;
  2143. }
  2144. /*
  2145. * Store led settings, for correct led behaviour.
  2146. * If the eeprom value is invalid,
  2147. * switch to default led mode.
  2148. */
  2149. #ifdef CONFIG_RT2X00_LIB_LEDS
  2150. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED, &eeprom);
  2151. value = rt2x00_get_field16(eeprom, EEPROM_LED_LED_MODE);
  2152. rt61pci_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
  2153. rt61pci_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
  2154. if (value == LED_MODE_SIGNAL_STRENGTH)
  2155. rt61pci_init_led(rt2x00dev, &rt2x00dev->led_qual,
  2156. LED_TYPE_QUALITY);
  2157. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_LED_MODE, value);
  2158. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_GPIO_0,
  2159. rt2x00_get_field16(eeprom,
  2160. EEPROM_LED_POLARITY_GPIO_0));
  2161. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_GPIO_1,
  2162. rt2x00_get_field16(eeprom,
  2163. EEPROM_LED_POLARITY_GPIO_1));
  2164. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_GPIO_2,
  2165. rt2x00_get_field16(eeprom,
  2166. EEPROM_LED_POLARITY_GPIO_2));
  2167. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_GPIO_3,
  2168. rt2x00_get_field16(eeprom,
  2169. EEPROM_LED_POLARITY_GPIO_3));
  2170. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_GPIO_4,
  2171. rt2x00_get_field16(eeprom,
  2172. EEPROM_LED_POLARITY_GPIO_4));
  2173. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_ACT,
  2174. rt2x00_get_field16(eeprom, EEPROM_LED_POLARITY_ACT));
  2175. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_READY_BG,
  2176. rt2x00_get_field16(eeprom,
  2177. EEPROM_LED_POLARITY_RDY_G));
  2178. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_READY_A,
  2179. rt2x00_get_field16(eeprom,
  2180. EEPROM_LED_POLARITY_RDY_A));
  2181. #endif /* CONFIG_RT2X00_LIB_LEDS */
  2182. return 0;
  2183. }
  2184. /*
  2185. * RF value list for RF5225 & RF5325
  2186. * Supports: 2.4 GHz & 5.2 GHz, rf_sequence disabled
  2187. */
  2188. static const struct rf_channel rf_vals_noseq[] = {
  2189. { 1, 0x00002ccc, 0x00004786, 0x00068455, 0x000ffa0b },
  2190. { 2, 0x00002ccc, 0x00004786, 0x00068455, 0x000ffa1f },
  2191. { 3, 0x00002ccc, 0x0000478a, 0x00068455, 0x000ffa0b },
  2192. { 4, 0x00002ccc, 0x0000478a, 0x00068455, 0x000ffa1f },
  2193. { 5, 0x00002ccc, 0x0000478e, 0x00068455, 0x000ffa0b },
  2194. { 6, 0x00002ccc, 0x0000478e, 0x00068455, 0x000ffa1f },
  2195. { 7, 0x00002ccc, 0x00004792, 0x00068455, 0x000ffa0b },
  2196. { 8, 0x00002ccc, 0x00004792, 0x00068455, 0x000ffa1f },
  2197. { 9, 0x00002ccc, 0x00004796, 0x00068455, 0x000ffa0b },
  2198. { 10, 0x00002ccc, 0x00004796, 0x00068455, 0x000ffa1f },
  2199. { 11, 0x00002ccc, 0x0000479a, 0x00068455, 0x000ffa0b },
  2200. { 12, 0x00002ccc, 0x0000479a, 0x00068455, 0x000ffa1f },
  2201. { 13, 0x00002ccc, 0x0000479e, 0x00068455, 0x000ffa0b },
  2202. { 14, 0x00002ccc, 0x000047a2, 0x00068455, 0x000ffa13 },
  2203. /* 802.11 UNI / HyperLan 2 */
  2204. { 36, 0x00002ccc, 0x0000499a, 0x0009be55, 0x000ffa23 },
  2205. { 40, 0x00002ccc, 0x000049a2, 0x0009be55, 0x000ffa03 },
  2206. { 44, 0x00002ccc, 0x000049a6, 0x0009be55, 0x000ffa0b },
  2207. { 48, 0x00002ccc, 0x000049aa, 0x0009be55, 0x000ffa13 },
  2208. { 52, 0x00002ccc, 0x000049ae, 0x0009ae55, 0x000ffa1b },
  2209. { 56, 0x00002ccc, 0x000049b2, 0x0009ae55, 0x000ffa23 },
  2210. { 60, 0x00002ccc, 0x000049ba, 0x0009ae55, 0x000ffa03 },
  2211. { 64, 0x00002ccc, 0x000049be, 0x0009ae55, 0x000ffa0b },
  2212. /* 802.11 HyperLan 2 */
  2213. { 100, 0x00002ccc, 0x00004a2a, 0x000bae55, 0x000ffa03 },
  2214. { 104, 0x00002ccc, 0x00004a2e, 0x000bae55, 0x000ffa0b },
  2215. { 108, 0x00002ccc, 0x00004a32, 0x000bae55, 0x000ffa13 },
  2216. { 112, 0x00002ccc, 0x00004a36, 0x000bae55, 0x000ffa1b },
  2217. { 116, 0x00002ccc, 0x00004a3a, 0x000bbe55, 0x000ffa23 },
  2218. { 120, 0x00002ccc, 0x00004a82, 0x000bbe55, 0x000ffa03 },
  2219. { 124, 0x00002ccc, 0x00004a86, 0x000bbe55, 0x000ffa0b },
  2220. { 128, 0x00002ccc, 0x00004a8a, 0x000bbe55, 0x000ffa13 },
  2221. { 132, 0x00002ccc, 0x00004a8e, 0x000bbe55, 0x000ffa1b },
  2222. { 136, 0x00002ccc, 0x00004a92, 0x000bbe55, 0x000ffa23 },
  2223. /* 802.11 UNII */
  2224. { 140, 0x00002ccc, 0x00004a9a, 0x000bbe55, 0x000ffa03 },
  2225. { 149, 0x00002ccc, 0x00004aa2, 0x000bbe55, 0x000ffa1f },
  2226. { 153, 0x00002ccc, 0x00004aa6, 0x000bbe55, 0x000ffa27 },
  2227. { 157, 0x00002ccc, 0x00004aae, 0x000bbe55, 0x000ffa07 },
  2228. { 161, 0x00002ccc, 0x00004ab2, 0x000bbe55, 0x000ffa0f },
  2229. { 165, 0x00002ccc, 0x00004ab6, 0x000bbe55, 0x000ffa17 },
  2230. /* MMAC(Japan)J52 ch 34,38,42,46 */
  2231. { 34, 0x00002ccc, 0x0000499a, 0x0009be55, 0x000ffa0b },
  2232. { 38, 0x00002ccc, 0x0000499e, 0x0009be55, 0x000ffa13 },
  2233. { 42, 0x00002ccc, 0x000049a2, 0x0009be55, 0x000ffa1b },
  2234. { 46, 0x00002ccc, 0x000049a6, 0x0009be55, 0x000ffa23 },
  2235. };
  2236. /*
  2237. * RF value list for RF5225 & RF5325
  2238. * Supports: 2.4 GHz & 5.2 GHz, rf_sequence enabled
  2239. */
  2240. static const struct rf_channel rf_vals_seq[] = {
  2241. { 1, 0x00002ccc, 0x00004786, 0x00068455, 0x000ffa0b },
  2242. { 2, 0x00002ccc, 0x00004786, 0x00068455, 0x000ffa1f },
  2243. { 3, 0x00002ccc, 0x0000478a, 0x00068455, 0x000ffa0b },
  2244. { 4, 0x00002ccc, 0x0000478a, 0x00068455, 0x000ffa1f },
  2245. { 5, 0x00002ccc, 0x0000478e, 0x00068455, 0x000ffa0b },
  2246. { 6, 0x00002ccc, 0x0000478e, 0x00068455, 0x000ffa1f },
  2247. { 7, 0x00002ccc, 0x00004792, 0x00068455, 0x000ffa0b },
  2248. { 8, 0x00002ccc, 0x00004792, 0x00068455, 0x000ffa1f },
  2249. { 9, 0x00002ccc, 0x00004796, 0x00068455, 0x000ffa0b },
  2250. { 10, 0x00002ccc, 0x00004796, 0x00068455, 0x000ffa1f },
  2251. { 11, 0x00002ccc, 0x0000479a, 0x00068455, 0x000ffa0b },
  2252. { 12, 0x00002ccc, 0x0000479a, 0x00068455, 0x000ffa1f },
  2253. { 13, 0x00002ccc, 0x0000479e, 0x00068455, 0x000ffa0b },
  2254. { 14, 0x00002ccc, 0x000047a2, 0x00068455, 0x000ffa13 },
  2255. /* 802.11 UNI / HyperLan 2 */
  2256. { 36, 0x00002cd4, 0x0004481a, 0x00098455, 0x000c0a03 },
  2257. { 40, 0x00002cd0, 0x00044682, 0x00098455, 0x000c0a03 },
  2258. { 44, 0x00002cd0, 0x00044686, 0x00098455, 0x000c0a1b },
  2259. { 48, 0x00002cd0, 0x0004468e, 0x00098655, 0x000c0a0b },
  2260. { 52, 0x00002cd0, 0x00044692, 0x00098855, 0x000c0a23 },
  2261. { 56, 0x00002cd0, 0x0004469a, 0x00098c55, 0x000c0a13 },
  2262. { 60, 0x00002cd0, 0x000446a2, 0x00098e55, 0x000c0a03 },
  2263. { 64, 0x00002cd0, 0x000446a6, 0x00099255, 0x000c0a1b },
  2264. /* 802.11 HyperLan 2 */
  2265. { 100, 0x00002cd4, 0x0004489a, 0x000b9855, 0x000c0a03 },
  2266. { 104, 0x00002cd4, 0x000448a2, 0x000b9855, 0x000c0a03 },
  2267. { 108, 0x00002cd4, 0x000448aa, 0x000b9855, 0x000c0a03 },
  2268. { 112, 0x00002cd4, 0x000448b2, 0x000b9a55, 0x000c0a03 },
  2269. { 116, 0x00002cd4, 0x000448ba, 0x000b9a55, 0x000c0a03 },
  2270. { 120, 0x00002cd0, 0x00044702, 0x000b9a55, 0x000c0a03 },
  2271. { 124, 0x00002cd0, 0x00044706, 0x000b9a55, 0x000c0a1b },
  2272. { 128, 0x00002cd0, 0x0004470e, 0x000b9c55, 0x000c0a0b },
  2273. { 132, 0x00002cd0, 0x00044712, 0x000b9c55, 0x000c0a23 },
  2274. { 136, 0x00002cd0, 0x0004471a, 0x000b9e55, 0x000c0a13 },
  2275. /* 802.11 UNII */
  2276. { 140, 0x00002cd0, 0x00044722, 0x000b9e55, 0x000c0a03 },
  2277. { 149, 0x00002cd0, 0x0004472e, 0x000ba255, 0x000c0a1b },
  2278. { 153, 0x00002cd0, 0x00044736, 0x000ba255, 0x000c0a0b },
  2279. { 157, 0x00002cd4, 0x0004490a, 0x000ba255, 0x000c0a17 },
  2280. { 161, 0x00002cd4, 0x00044912, 0x000ba255, 0x000c0a17 },
  2281. { 165, 0x00002cd4, 0x0004491a, 0x000ba255, 0x000c0a17 },
  2282. /* MMAC(Japan)J52 ch 34,38,42,46 */
  2283. { 34, 0x00002ccc, 0x0000499a, 0x0009be55, 0x000c0a0b },
  2284. { 38, 0x00002ccc, 0x0000499e, 0x0009be55, 0x000c0a13 },
  2285. { 42, 0x00002ccc, 0x000049a2, 0x0009be55, 0x000c0a1b },
  2286. { 46, 0x00002ccc, 0x000049a6, 0x0009be55, 0x000c0a23 },
  2287. };
  2288. static int rt61pci_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
  2289. {
  2290. struct hw_mode_spec *spec = &rt2x00dev->spec;
  2291. struct channel_info *info;
  2292. char *tx_power;
  2293. unsigned int i;
  2294. /*
  2295. * Disable powersaving as default.
  2296. */
  2297. rt2x00dev->hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  2298. /*
  2299. * Initialize all hw fields.
  2300. */
  2301. rt2x00dev->hw->flags =
  2302. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  2303. IEEE80211_HW_SIGNAL_DBM |
  2304. IEEE80211_HW_SUPPORTS_PS |
  2305. IEEE80211_HW_PS_NULLFUNC_STACK;
  2306. SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
  2307. SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
  2308. rt2x00_eeprom_addr(rt2x00dev,
  2309. EEPROM_MAC_ADDR_0));
  2310. /*
  2311. * As rt61 has a global fallback table we cannot specify
  2312. * more then one tx rate per frame but since the hw will
  2313. * try several rates (based on the fallback table) we should
  2314. * initialize max_report_rates to the maximum number of rates
  2315. * we are going to try. Otherwise mac80211 will truncate our
  2316. * reported tx rates and the rc algortihm will end up with
  2317. * incorrect data.
  2318. */
  2319. rt2x00dev->hw->max_rates = 1;
  2320. rt2x00dev->hw->max_report_rates = 7;
  2321. rt2x00dev->hw->max_rate_tries = 1;
  2322. /*
  2323. * Initialize hw_mode information.
  2324. */
  2325. spec->supported_bands = SUPPORT_BAND_2GHZ;
  2326. spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
  2327. if (!test_bit(CONFIG_RF_SEQUENCE, &rt2x00dev->flags)) {
  2328. spec->num_channels = 14;
  2329. spec->channels = rf_vals_noseq;
  2330. } else {
  2331. spec->num_channels = 14;
  2332. spec->channels = rf_vals_seq;
  2333. }
  2334. if (rt2x00_rf(rt2x00dev, RF5225) || rt2x00_rf(rt2x00dev, RF5325)) {
  2335. spec->supported_bands |= SUPPORT_BAND_5GHZ;
  2336. spec->num_channels = ARRAY_SIZE(rf_vals_seq);
  2337. }
  2338. /*
  2339. * Create channel information array
  2340. */
  2341. info = kcalloc(spec->num_channels, sizeof(*info), GFP_KERNEL);
  2342. if (!info)
  2343. return -ENOMEM;
  2344. spec->channels_info = info;
  2345. tx_power = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_G_START);
  2346. for (i = 0; i < 14; i++) {
  2347. info[i].max_power = MAX_TXPOWER;
  2348. info[i].default_power1 = TXPOWER_FROM_DEV(tx_power[i]);
  2349. }
  2350. if (spec->num_channels > 14) {
  2351. tx_power = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A_START);
  2352. for (i = 14; i < spec->num_channels; i++) {
  2353. info[i].max_power = MAX_TXPOWER;
  2354. info[i].default_power1 = TXPOWER_FROM_DEV(tx_power[i]);
  2355. }
  2356. }
  2357. return 0;
  2358. }
  2359. static int rt61pci_probe_hw(struct rt2x00_dev *rt2x00dev)
  2360. {
  2361. int retval;
  2362. /*
  2363. * Disable power saving.
  2364. */
  2365. rt2x00pci_register_write(rt2x00dev, SOFT_RESET_CSR, 0x00000007);
  2366. /*
  2367. * Allocate eeprom data.
  2368. */
  2369. retval = rt61pci_validate_eeprom(rt2x00dev);
  2370. if (retval)
  2371. return retval;
  2372. retval = rt61pci_init_eeprom(rt2x00dev);
  2373. if (retval)
  2374. return retval;
  2375. /*
  2376. * Initialize hw specifications.
  2377. */
  2378. retval = rt61pci_probe_hw_mode(rt2x00dev);
  2379. if (retval)
  2380. return retval;
  2381. /*
  2382. * This device has multiple filters for control frames,
  2383. * but has no a separate filter for PS Poll frames.
  2384. */
  2385. __set_bit(DRIVER_SUPPORT_CONTROL_FILTERS, &rt2x00dev->flags);
  2386. /*
  2387. * This device requires firmware and DMA mapped skbs.
  2388. */
  2389. __set_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags);
  2390. __set_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags);
  2391. if (!modparam_nohwcrypt)
  2392. __set_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags);
  2393. __set_bit(DRIVER_SUPPORT_LINK_TUNING, &rt2x00dev->flags);
  2394. /*
  2395. * Set the rssi offset.
  2396. */
  2397. rt2x00dev->rssi_offset = DEFAULT_RSSI_OFFSET;
  2398. return 0;
  2399. }
  2400. /*
  2401. * IEEE80211 stack callback functions.
  2402. */
  2403. static int rt61pci_conf_tx(struct ieee80211_hw *hw, u16 queue_idx,
  2404. const struct ieee80211_tx_queue_params *params)
  2405. {
  2406. struct rt2x00_dev *rt2x00dev = hw->priv;
  2407. struct data_queue *queue;
  2408. struct rt2x00_field32 field;
  2409. int retval;
  2410. u32 reg;
  2411. u32 offset;
  2412. /*
  2413. * First pass the configuration through rt2x00lib, that will
  2414. * update the queue settings and validate the input. After that
  2415. * we are free to update the registers based on the value
  2416. * in the queue parameter.
  2417. */
  2418. retval = rt2x00mac_conf_tx(hw, queue_idx, params);
  2419. if (retval)
  2420. return retval;
  2421. /*
  2422. * We only need to perform additional register initialization
  2423. * for WMM queues.
  2424. */
  2425. if (queue_idx >= 4)
  2426. return 0;
  2427. queue = rt2x00queue_get_queue(rt2x00dev, queue_idx);
  2428. /* Update WMM TXOP register */
  2429. offset = AC_TXOP_CSR0 + (sizeof(u32) * (!!(queue_idx & 2)));
  2430. field.bit_offset = (queue_idx & 1) * 16;
  2431. field.bit_mask = 0xffff << field.bit_offset;
  2432. rt2x00pci_register_read(rt2x00dev, offset, &reg);
  2433. rt2x00_set_field32(&reg, field, queue->txop);
  2434. rt2x00pci_register_write(rt2x00dev, offset, reg);
  2435. /* Update WMM registers */
  2436. field.bit_offset = queue_idx * 4;
  2437. field.bit_mask = 0xf << field.bit_offset;
  2438. rt2x00pci_register_read(rt2x00dev, AIFSN_CSR, &reg);
  2439. rt2x00_set_field32(&reg, field, queue->aifs);
  2440. rt2x00pci_register_write(rt2x00dev, AIFSN_CSR, reg);
  2441. rt2x00pci_register_read(rt2x00dev, CWMIN_CSR, &reg);
  2442. rt2x00_set_field32(&reg, field, queue->cw_min);
  2443. rt2x00pci_register_write(rt2x00dev, CWMIN_CSR, reg);
  2444. rt2x00pci_register_read(rt2x00dev, CWMAX_CSR, &reg);
  2445. rt2x00_set_field32(&reg, field, queue->cw_max);
  2446. rt2x00pci_register_write(rt2x00dev, CWMAX_CSR, reg);
  2447. return 0;
  2448. }
  2449. static u64 rt61pci_get_tsf(struct ieee80211_hw *hw)
  2450. {
  2451. struct rt2x00_dev *rt2x00dev = hw->priv;
  2452. u64 tsf;
  2453. u32 reg;
  2454. rt2x00pci_register_read(rt2x00dev, TXRX_CSR13, &reg);
  2455. tsf = (u64) rt2x00_get_field32(reg, TXRX_CSR13_HIGH_TSFTIMER) << 32;
  2456. rt2x00pci_register_read(rt2x00dev, TXRX_CSR12, &reg);
  2457. tsf |= rt2x00_get_field32(reg, TXRX_CSR12_LOW_TSFTIMER);
  2458. return tsf;
  2459. }
  2460. static const struct ieee80211_ops rt61pci_mac80211_ops = {
  2461. .tx = rt2x00mac_tx,
  2462. .start = rt2x00mac_start,
  2463. .stop = rt2x00mac_stop,
  2464. .add_interface = rt2x00mac_add_interface,
  2465. .remove_interface = rt2x00mac_remove_interface,
  2466. .config = rt2x00mac_config,
  2467. .configure_filter = rt2x00mac_configure_filter,
  2468. .set_key = rt2x00mac_set_key,
  2469. .sw_scan_start = rt2x00mac_sw_scan_start,
  2470. .sw_scan_complete = rt2x00mac_sw_scan_complete,
  2471. .get_stats = rt2x00mac_get_stats,
  2472. .bss_info_changed = rt2x00mac_bss_info_changed,
  2473. .conf_tx = rt61pci_conf_tx,
  2474. .get_tsf = rt61pci_get_tsf,
  2475. .rfkill_poll = rt2x00mac_rfkill_poll,
  2476. .flush = rt2x00mac_flush,
  2477. };
  2478. static const struct rt2x00lib_ops rt61pci_rt2x00_ops = {
  2479. .irq_handler = rt61pci_interrupt,
  2480. .irq_handler_thread = rt61pci_interrupt_thread,
  2481. .probe_hw = rt61pci_probe_hw,
  2482. .get_firmware_name = rt61pci_get_firmware_name,
  2483. .check_firmware = rt61pci_check_firmware,
  2484. .load_firmware = rt61pci_load_firmware,
  2485. .initialize = rt2x00pci_initialize,
  2486. .uninitialize = rt2x00pci_uninitialize,
  2487. .get_entry_state = rt61pci_get_entry_state,
  2488. .clear_entry = rt61pci_clear_entry,
  2489. .set_device_state = rt61pci_set_device_state,
  2490. .rfkill_poll = rt61pci_rfkill_poll,
  2491. .link_stats = rt61pci_link_stats,
  2492. .reset_tuner = rt61pci_reset_tuner,
  2493. .link_tuner = rt61pci_link_tuner,
  2494. .write_tx_desc = rt61pci_write_tx_desc,
  2495. .write_beacon = rt61pci_write_beacon,
  2496. .kick_tx_queue = rt61pci_kick_queue,
  2497. .kill_tx_queue = rt61pci_stop_queue,
  2498. .fill_rxdone = rt61pci_fill_rxdone,
  2499. .config_shared_key = rt61pci_config_shared_key,
  2500. .config_pairwise_key = rt61pci_config_pairwise_key,
  2501. .config_filter = rt61pci_config_filter,
  2502. .config_intf = rt61pci_config_intf,
  2503. .config_erp = rt61pci_config_erp,
  2504. .config_ant = rt61pci_config_ant,
  2505. .config = rt61pci_config,
  2506. };
  2507. static const struct data_queue_desc rt61pci_queue_rx = {
  2508. .entry_num = 32,
  2509. .data_size = DATA_FRAME_SIZE,
  2510. .desc_size = RXD_DESC_SIZE,
  2511. .priv_size = sizeof(struct queue_entry_priv_pci),
  2512. };
  2513. static const struct data_queue_desc rt61pci_queue_tx = {
  2514. .entry_num = 32,
  2515. .data_size = DATA_FRAME_SIZE,
  2516. .desc_size = TXD_DESC_SIZE,
  2517. .priv_size = sizeof(struct queue_entry_priv_pci),
  2518. };
  2519. static const struct data_queue_desc rt61pci_queue_bcn = {
  2520. .entry_num = 4,
  2521. .data_size = 0, /* No DMA required for beacons */
  2522. .desc_size = TXINFO_SIZE,
  2523. .priv_size = sizeof(struct queue_entry_priv_pci),
  2524. };
  2525. static const struct rt2x00_ops rt61pci_ops = {
  2526. .name = KBUILD_MODNAME,
  2527. .max_sta_intf = 1,
  2528. .max_ap_intf = 4,
  2529. .eeprom_size = EEPROM_SIZE,
  2530. .rf_size = RF_SIZE,
  2531. .tx_queues = NUM_TX_QUEUES,
  2532. .extra_tx_headroom = 0,
  2533. .rx = &rt61pci_queue_rx,
  2534. .tx = &rt61pci_queue_tx,
  2535. .bcn = &rt61pci_queue_bcn,
  2536. .lib = &rt61pci_rt2x00_ops,
  2537. .hw = &rt61pci_mac80211_ops,
  2538. #ifdef CONFIG_RT2X00_LIB_DEBUGFS
  2539. .debugfs = &rt61pci_rt2x00debug,
  2540. #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
  2541. };
  2542. /*
  2543. * RT61pci module information.
  2544. */
  2545. static DEFINE_PCI_DEVICE_TABLE(rt61pci_device_table) = {
  2546. /* RT2561s */
  2547. { PCI_DEVICE(0x1814, 0x0301), PCI_DEVICE_DATA(&rt61pci_ops) },
  2548. /* RT2561 v2 */
  2549. { PCI_DEVICE(0x1814, 0x0302), PCI_DEVICE_DATA(&rt61pci_ops) },
  2550. /* RT2661 */
  2551. { PCI_DEVICE(0x1814, 0x0401), PCI_DEVICE_DATA(&rt61pci_ops) },
  2552. { 0, }
  2553. };
  2554. MODULE_AUTHOR(DRV_PROJECT);
  2555. MODULE_VERSION(DRV_VERSION);
  2556. MODULE_DESCRIPTION("Ralink RT61 PCI & PCMCIA Wireless LAN driver.");
  2557. MODULE_SUPPORTED_DEVICE("Ralink RT2561, RT2561s & RT2661 "
  2558. "PCI & PCMCIA chipset based cards");
  2559. MODULE_DEVICE_TABLE(pci, rt61pci_device_table);
  2560. MODULE_FIRMWARE(FIRMWARE_RT2561);
  2561. MODULE_FIRMWARE(FIRMWARE_RT2561s);
  2562. MODULE_FIRMWARE(FIRMWARE_RT2661);
  2563. MODULE_LICENSE("GPL");
  2564. static struct pci_driver rt61pci_driver = {
  2565. .name = KBUILD_MODNAME,
  2566. .id_table = rt61pci_device_table,
  2567. .probe = rt2x00pci_probe,
  2568. .remove = __devexit_p(rt2x00pci_remove),
  2569. .suspend = rt2x00pci_suspend,
  2570. .resume = rt2x00pci_resume,
  2571. };
  2572. static int __init rt61pci_init(void)
  2573. {
  2574. return pci_register_driver(&rt61pci_driver);
  2575. }
  2576. static void __exit rt61pci_exit(void)
  2577. {
  2578. pci_unregister_driver(&rt61pci_driver);
  2579. }
  2580. module_init(rt61pci_init);
  2581. module_exit(rt61pci_exit);