rt73usb.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208
  1. /*
  2. Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
  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: rt73usb
  19. Abstract: rt73usb device specific routines.
  20. Supported chipsets: rt2571W & rt2671.
  21. */
  22. #include <linux/delay.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/init.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/usb.h>
  28. #include "rt2x00.h"
  29. #include "rt2x00usb.h"
  30. #include "rt73usb.h"
  31. /*
  32. * Register access.
  33. * All access to the CSR registers will go through the methods
  34. * rt73usb_register_read and rt73usb_register_write.
  35. * BBP and RF register require indirect register access,
  36. * and use the CSR registers BBPCSR and RFCSR to achieve this.
  37. * These indirect registers work with busy bits,
  38. * and we will try maximal REGISTER_BUSY_COUNT times to access
  39. * the register while taking a REGISTER_BUSY_DELAY us delay
  40. * between each attampt. When the busy bit is still set at that time,
  41. * the access attempt is considered to have failed,
  42. * and we will print an error.
  43. * The _lock versions must be used if you already hold the usb_cache_mutex
  44. */
  45. static inline void rt73usb_register_read(struct rt2x00_dev *rt2x00dev,
  46. const unsigned int offset, u32 *value)
  47. {
  48. __le32 reg;
  49. rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_READ,
  50. USB_VENDOR_REQUEST_IN, offset,
  51. &reg, sizeof(u32), REGISTER_TIMEOUT);
  52. *value = le32_to_cpu(reg);
  53. }
  54. static inline void rt73usb_register_read_lock(struct rt2x00_dev *rt2x00dev,
  55. const unsigned int offset, u32 *value)
  56. {
  57. __le32 reg;
  58. rt2x00usb_vendor_req_buff_lock(rt2x00dev, USB_MULTI_READ,
  59. USB_VENDOR_REQUEST_IN, offset,
  60. &reg, sizeof(u32), REGISTER_TIMEOUT);
  61. *value = le32_to_cpu(reg);
  62. }
  63. static inline void rt73usb_register_multiread(struct rt2x00_dev *rt2x00dev,
  64. const unsigned int offset,
  65. void *value, const u32 length)
  66. {
  67. int timeout = REGISTER_TIMEOUT * (length / sizeof(u32));
  68. rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_READ,
  69. USB_VENDOR_REQUEST_IN, offset,
  70. value, length, timeout);
  71. }
  72. static inline void rt73usb_register_write(struct rt2x00_dev *rt2x00dev,
  73. const unsigned int offset, u32 value)
  74. {
  75. __le32 reg = cpu_to_le32(value);
  76. rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_WRITE,
  77. USB_VENDOR_REQUEST_OUT, offset,
  78. &reg, sizeof(u32), REGISTER_TIMEOUT);
  79. }
  80. static inline void rt73usb_register_write_lock(struct rt2x00_dev *rt2x00dev,
  81. const unsigned int offset, u32 value)
  82. {
  83. __le32 reg = cpu_to_le32(value);
  84. rt2x00usb_vendor_req_buff_lock(rt2x00dev, USB_MULTI_WRITE,
  85. USB_VENDOR_REQUEST_OUT, offset,
  86. &reg, sizeof(u32), REGISTER_TIMEOUT);
  87. }
  88. static inline void rt73usb_register_multiwrite(struct rt2x00_dev *rt2x00dev,
  89. const unsigned int offset,
  90. void *value, const u32 length)
  91. {
  92. int timeout = REGISTER_TIMEOUT * (length / sizeof(u32));
  93. rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_WRITE,
  94. USB_VENDOR_REQUEST_OUT, offset,
  95. value, length, timeout);
  96. }
  97. static u32 rt73usb_bbp_check(struct rt2x00_dev *rt2x00dev)
  98. {
  99. u32 reg;
  100. unsigned int i;
  101. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  102. rt73usb_register_read_lock(rt2x00dev, PHY_CSR3, &reg);
  103. if (!rt2x00_get_field32(reg, PHY_CSR3_BUSY))
  104. break;
  105. udelay(REGISTER_BUSY_DELAY);
  106. }
  107. return reg;
  108. }
  109. static void rt73usb_bbp_write(struct rt2x00_dev *rt2x00dev,
  110. const unsigned int word, const u8 value)
  111. {
  112. u32 reg;
  113. mutex_lock(&rt2x00dev->usb_cache_mutex);
  114. /*
  115. * Wait until the BBP becomes ready.
  116. */
  117. reg = rt73usb_bbp_check(rt2x00dev);
  118. if (rt2x00_get_field32(reg, PHY_CSR3_BUSY)) {
  119. ERROR(rt2x00dev, "PHY_CSR3 register busy. Write failed.\n");
  120. mutex_unlock(&rt2x00dev->usb_cache_mutex);
  121. return;
  122. }
  123. /*
  124. * Write the data into the BBP.
  125. */
  126. reg = 0;
  127. rt2x00_set_field32(&reg, PHY_CSR3_VALUE, value);
  128. rt2x00_set_field32(&reg, PHY_CSR3_REGNUM, word);
  129. rt2x00_set_field32(&reg, PHY_CSR3_BUSY, 1);
  130. rt2x00_set_field32(&reg, PHY_CSR3_READ_CONTROL, 0);
  131. rt73usb_register_write_lock(rt2x00dev, PHY_CSR3, reg);
  132. mutex_unlock(&rt2x00dev->usb_cache_mutex);
  133. }
  134. static void rt73usb_bbp_read(struct rt2x00_dev *rt2x00dev,
  135. const unsigned int word, u8 *value)
  136. {
  137. u32 reg;
  138. mutex_lock(&rt2x00dev->usb_cache_mutex);
  139. /*
  140. * Wait until the BBP becomes ready.
  141. */
  142. reg = rt73usb_bbp_check(rt2x00dev);
  143. if (rt2x00_get_field32(reg, PHY_CSR3_BUSY)) {
  144. ERROR(rt2x00dev, "PHY_CSR3 register busy. Read failed.\n");
  145. mutex_unlock(&rt2x00dev->usb_cache_mutex);
  146. return;
  147. }
  148. /*
  149. * Write the request into the BBP.
  150. */
  151. reg = 0;
  152. rt2x00_set_field32(&reg, PHY_CSR3_REGNUM, word);
  153. rt2x00_set_field32(&reg, PHY_CSR3_BUSY, 1);
  154. rt2x00_set_field32(&reg, PHY_CSR3_READ_CONTROL, 1);
  155. rt73usb_register_write_lock(rt2x00dev, PHY_CSR3, reg);
  156. /*
  157. * Wait until the BBP becomes ready.
  158. */
  159. reg = rt73usb_bbp_check(rt2x00dev);
  160. if (rt2x00_get_field32(reg, PHY_CSR3_BUSY)) {
  161. ERROR(rt2x00dev, "PHY_CSR3 register busy. Read failed.\n");
  162. *value = 0xff;
  163. return;
  164. }
  165. *value = rt2x00_get_field32(reg, PHY_CSR3_VALUE);
  166. mutex_unlock(&rt2x00dev->usb_cache_mutex);
  167. }
  168. static void rt73usb_rf_write(struct rt2x00_dev *rt2x00dev,
  169. const unsigned int word, const u32 value)
  170. {
  171. u32 reg;
  172. unsigned int i;
  173. if (!word)
  174. return;
  175. mutex_lock(&rt2x00dev->usb_cache_mutex);
  176. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  177. rt73usb_register_read_lock(rt2x00dev, PHY_CSR4, &reg);
  178. if (!rt2x00_get_field32(reg, PHY_CSR4_BUSY))
  179. goto rf_write;
  180. udelay(REGISTER_BUSY_DELAY);
  181. }
  182. mutex_unlock(&rt2x00dev->usb_cache_mutex);
  183. ERROR(rt2x00dev, "PHY_CSR4 register busy. Write failed.\n");
  184. return;
  185. rf_write:
  186. reg = 0;
  187. rt2x00_set_field32(&reg, PHY_CSR4_VALUE, value);
  188. /*
  189. * RF5225 and RF2527 contain 21 bits per RF register value,
  190. * all others contain 20 bits.
  191. */
  192. rt2x00_set_field32(&reg, PHY_CSR4_NUMBER_OF_BITS,
  193. 20 + (rt2x00_rf(&rt2x00dev->chip, RF5225) ||
  194. rt2x00_rf(&rt2x00dev->chip, RF2527)));
  195. rt2x00_set_field32(&reg, PHY_CSR4_IF_SELECT, 0);
  196. rt2x00_set_field32(&reg, PHY_CSR4_BUSY, 1);
  197. rt73usb_register_write_lock(rt2x00dev, PHY_CSR4, reg);
  198. rt2x00_rf_write(rt2x00dev, word, value);
  199. mutex_unlock(&rt2x00dev->usb_cache_mutex);
  200. }
  201. #ifdef CONFIG_RT2X00_LIB_DEBUGFS
  202. #define CSR_OFFSET(__word) ( CSR_REG_BASE + ((__word) * sizeof(u32)) )
  203. static void rt73usb_read_csr(struct rt2x00_dev *rt2x00dev,
  204. const unsigned int word, u32 *data)
  205. {
  206. rt73usb_register_read(rt2x00dev, CSR_OFFSET(word), data);
  207. }
  208. static void rt73usb_write_csr(struct rt2x00_dev *rt2x00dev,
  209. const unsigned int word, u32 data)
  210. {
  211. rt73usb_register_write(rt2x00dev, CSR_OFFSET(word), data);
  212. }
  213. static const struct rt2x00debug rt73usb_rt2x00debug = {
  214. .owner = THIS_MODULE,
  215. .csr = {
  216. .read = rt73usb_read_csr,
  217. .write = rt73usb_write_csr,
  218. .word_size = sizeof(u32),
  219. .word_count = CSR_REG_SIZE / sizeof(u32),
  220. },
  221. .eeprom = {
  222. .read = rt2x00_eeprom_read,
  223. .write = rt2x00_eeprom_write,
  224. .word_size = sizeof(u16),
  225. .word_count = EEPROM_SIZE / sizeof(u16),
  226. },
  227. .bbp = {
  228. .read = rt73usb_bbp_read,
  229. .write = rt73usb_bbp_write,
  230. .word_size = sizeof(u8),
  231. .word_count = BBP_SIZE / sizeof(u8),
  232. },
  233. .rf = {
  234. .read = rt2x00_rf_read,
  235. .write = rt73usb_rf_write,
  236. .word_size = sizeof(u32),
  237. .word_count = RF_SIZE / sizeof(u32),
  238. },
  239. };
  240. #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
  241. #ifdef CONFIG_RT73USB_LEDS
  242. static void rt73usb_led_brightness(struct led_classdev *led_cdev,
  243. enum led_brightness brightness)
  244. {
  245. struct rt2x00_led *led =
  246. container_of(led_cdev, struct rt2x00_led, led_dev);
  247. unsigned int enabled = brightness != LED_OFF;
  248. unsigned int a_mode =
  249. (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_5GHZ);
  250. unsigned int bg_mode =
  251. (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
  252. if (in_atomic()) {
  253. NOTICE(led->rt2x00dev,
  254. "Ignoring LED brightness command for led %d", led->type);
  255. return;
  256. }
  257. if (led->type == LED_TYPE_RADIO) {
  258. rt2x00_set_field16(&led->rt2x00dev->led_mcu_reg,
  259. MCU_LEDCS_RADIO_STATUS, enabled);
  260. rt2x00usb_vendor_request_sw(led->rt2x00dev, USB_LED_CONTROL,
  261. 0, led->rt2x00dev->led_mcu_reg,
  262. REGISTER_TIMEOUT);
  263. } else if (led->type == LED_TYPE_ASSOC) {
  264. rt2x00_set_field16(&led->rt2x00dev->led_mcu_reg,
  265. MCU_LEDCS_LINK_BG_STATUS, bg_mode);
  266. rt2x00_set_field16(&led->rt2x00dev->led_mcu_reg,
  267. MCU_LEDCS_LINK_A_STATUS, a_mode);
  268. rt2x00usb_vendor_request_sw(led->rt2x00dev, USB_LED_CONTROL,
  269. 0, led->rt2x00dev->led_mcu_reg,
  270. REGISTER_TIMEOUT);
  271. } else if (led->type == LED_TYPE_QUALITY) {
  272. /*
  273. * The brightness is divided into 6 levels (0 - 5),
  274. * this means we need to convert the brightness
  275. * argument into the matching level within that range.
  276. */
  277. rt2x00usb_vendor_request_sw(led->rt2x00dev, USB_LED_CONTROL,
  278. brightness / (LED_FULL / 6),
  279. led->rt2x00dev->led_mcu_reg,
  280. REGISTER_TIMEOUT);
  281. }
  282. }
  283. #else
  284. #define rt73usb_led_brightness NULL
  285. #endif /* CONFIG_RT73USB_LEDS */
  286. /*
  287. * Configuration handlers.
  288. */
  289. static void rt73usb_config_intf(struct rt2x00_dev *rt2x00dev,
  290. struct rt2x00_intf *intf,
  291. struct rt2x00intf_conf *conf,
  292. const unsigned int flags)
  293. {
  294. unsigned int beacon_base;
  295. u32 reg;
  296. if (flags & CONFIG_UPDATE_TYPE) {
  297. /*
  298. * Clear current synchronisation setup.
  299. * For the Beacon base registers we only need to clear
  300. * the first byte since that byte contains the VALID and OWNER
  301. * bits which (when set to 0) will invalidate the entire beacon.
  302. */
  303. beacon_base = HW_BEACON_OFFSET(intf->beacon->entry_idx);
  304. rt73usb_register_write(rt2x00dev, TXRX_CSR9, 0);
  305. rt73usb_register_write(rt2x00dev, beacon_base, 0);
  306. /*
  307. * Enable synchronisation.
  308. */
  309. rt73usb_register_read(rt2x00dev, TXRX_CSR9, &reg);
  310. rt2x00_set_field32(&reg, TXRX_CSR9_TSF_TICKING, 1);
  311. rt2x00_set_field32(&reg, TXRX_CSR9_TBTT_ENABLE,
  312. (conf->sync == TSF_SYNC_BEACON));
  313. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 0);
  314. rt2x00_set_field32(&reg, TXRX_CSR9_TSF_SYNC, conf->sync);
  315. rt73usb_register_write(rt2x00dev, TXRX_CSR9, reg);
  316. }
  317. if (flags & CONFIG_UPDATE_MAC) {
  318. reg = le32_to_cpu(conf->mac[1]);
  319. rt2x00_set_field32(&reg, MAC_CSR3_UNICAST_TO_ME_MASK, 0xff);
  320. conf->mac[1] = cpu_to_le32(reg);
  321. rt73usb_register_multiwrite(rt2x00dev, MAC_CSR2,
  322. conf->mac, sizeof(conf->mac));
  323. }
  324. if (flags & CONFIG_UPDATE_BSSID) {
  325. reg = le32_to_cpu(conf->bssid[1]);
  326. rt2x00_set_field32(&reg, MAC_CSR5_BSS_ID_MASK, 3);
  327. conf->bssid[1] = cpu_to_le32(reg);
  328. rt73usb_register_multiwrite(rt2x00dev, MAC_CSR4,
  329. conf->bssid, sizeof(conf->bssid));
  330. }
  331. }
  332. static int rt73usb_config_preamble(struct rt2x00_dev *rt2x00dev,
  333. const int short_preamble,
  334. const int ack_timeout,
  335. const int ack_consume_time)
  336. {
  337. u32 reg;
  338. /*
  339. * When in atomic context, we should let rt2x00lib
  340. * try this configuration again later.
  341. */
  342. if (in_atomic())
  343. return -EAGAIN;
  344. rt73usb_register_read(rt2x00dev, TXRX_CSR0, &reg);
  345. rt2x00_set_field32(&reg, TXRX_CSR0_RX_ACK_TIMEOUT, ack_timeout);
  346. rt73usb_register_write(rt2x00dev, TXRX_CSR0, reg);
  347. rt73usb_register_read(rt2x00dev, TXRX_CSR4, &reg);
  348. rt2x00_set_field32(&reg, TXRX_CSR4_AUTORESPOND_PREAMBLE,
  349. !!short_preamble);
  350. rt73usb_register_write(rt2x00dev, TXRX_CSR4, reg);
  351. return 0;
  352. }
  353. static void rt73usb_config_phymode(struct rt2x00_dev *rt2x00dev,
  354. const int basic_rate_mask)
  355. {
  356. rt73usb_register_write(rt2x00dev, TXRX_CSR5, basic_rate_mask);
  357. }
  358. static void rt73usb_config_channel(struct rt2x00_dev *rt2x00dev,
  359. struct rf_channel *rf, const int txpower)
  360. {
  361. u8 r3;
  362. u8 r94;
  363. u8 smart;
  364. rt2x00_set_field32(&rf->rf3, RF3_TXPOWER, TXPOWER_TO_DEV(txpower));
  365. rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
  366. smart = !(rt2x00_rf(&rt2x00dev->chip, RF5225) ||
  367. rt2x00_rf(&rt2x00dev->chip, RF2527));
  368. rt73usb_bbp_read(rt2x00dev, 3, &r3);
  369. rt2x00_set_field8(&r3, BBP_R3_SMART_MODE, smart);
  370. rt73usb_bbp_write(rt2x00dev, 3, r3);
  371. r94 = 6;
  372. if (txpower > MAX_TXPOWER && txpower <= (MAX_TXPOWER + r94))
  373. r94 += txpower - MAX_TXPOWER;
  374. else if (txpower < MIN_TXPOWER && txpower >= (MIN_TXPOWER - r94))
  375. r94 += txpower;
  376. rt73usb_bbp_write(rt2x00dev, 94, r94);
  377. rt73usb_rf_write(rt2x00dev, 1, rf->rf1);
  378. rt73usb_rf_write(rt2x00dev, 2, rf->rf2);
  379. rt73usb_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
  380. rt73usb_rf_write(rt2x00dev, 4, rf->rf4);
  381. rt73usb_rf_write(rt2x00dev, 1, rf->rf1);
  382. rt73usb_rf_write(rt2x00dev, 2, rf->rf2);
  383. rt73usb_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
  384. rt73usb_rf_write(rt2x00dev, 4, rf->rf4);
  385. rt73usb_rf_write(rt2x00dev, 1, rf->rf1);
  386. rt73usb_rf_write(rt2x00dev, 2, rf->rf2);
  387. rt73usb_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
  388. rt73usb_rf_write(rt2x00dev, 4, rf->rf4);
  389. udelay(10);
  390. }
  391. static void rt73usb_config_txpower(struct rt2x00_dev *rt2x00dev,
  392. const int txpower)
  393. {
  394. struct rf_channel rf;
  395. rt2x00_rf_read(rt2x00dev, 1, &rf.rf1);
  396. rt2x00_rf_read(rt2x00dev, 2, &rf.rf2);
  397. rt2x00_rf_read(rt2x00dev, 3, &rf.rf3);
  398. rt2x00_rf_read(rt2x00dev, 4, &rf.rf4);
  399. rt73usb_config_channel(rt2x00dev, &rf, txpower);
  400. }
  401. static void rt73usb_config_antenna_5x(struct rt2x00_dev *rt2x00dev,
  402. struct antenna_setup *ant)
  403. {
  404. u8 r3;
  405. u8 r4;
  406. u8 r77;
  407. u8 temp;
  408. rt73usb_bbp_read(rt2x00dev, 3, &r3);
  409. rt73usb_bbp_read(rt2x00dev, 4, &r4);
  410. rt73usb_bbp_read(rt2x00dev, 77, &r77);
  411. rt2x00_set_field8(&r3, BBP_R3_SMART_MODE, 0);
  412. /*
  413. * Configure the RX antenna.
  414. */
  415. switch (ant->rx) {
  416. case ANTENNA_HW_DIVERSITY:
  417. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 2);
  418. temp = !test_bit(CONFIG_FRAME_TYPE, &rt2x00dev->flags)
  419. && (rt2x00dev->curr_band != IEEE80211_BAND_5GHZ);
  420. rt2x00_set_field8(&r4, BBP_R4_RX_FRAME_END, temp);
  421. break;
  422. case ANTENNA_A:
  423. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  424. rt2x00_set_field8(&r4, BBP_R4_RX_FRAME_END, 0);
  425. if (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ)
  426. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 0);
  427. else
  428. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 3);
  429. break;
  430. case ANTENNA_SW_DIVERSITY:
  431. /*
  432. * NOTE: We should never come here because rt2x00lib is
  433. * supposed to catch this and send us the correct antenna
  434. * explicitely. However we are nog going to bug about this.
  435. * Instead, just default to antenna B.
  436. */
  437. case ANTENNA_B:
  438. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  439. rt2x00_set_field8(&r4, BBP_R4_RX_FRAME_END, 0);
  440. if (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ)
  441. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 3);
  442. else
  443. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 0);
  444. break;
  445. }
  446. rt73usb_bbp_write(rt2x00dev, 77, r77);
  447. rt73usb_bbp_write(rt2x00dev, 3, r3);
  448. rt73usb_bbp_write(rt2x00dev, 4, r4);
  449. }
  450. static void rt73usb_config_antenna_2x(struct rt2x00_dev *rt2x00dev,
  451. struct antenna_setup *ant)
  452. {
  453. u8 r3;
  454. u8 r4;
  455. u8 r77;
  456. rt73usb_bbp_read(rt2x00dev, 3, &r3);
  457. rt73usb_bbp_read(rt2x00dev, 4, &r4);
  458. rt73usb_bbp_read(rt2x00dev, 77, &r77);
  459. rt2x00_set_field8(&r3, BBP_R3_SMART_MODE, 0);
  460. rt2x00_set_field8(&r4, BBP_R4_RX_FRAME_END,
  461. !test_bit(CONFIG_FRAME_TYPE, &rt2x00dev->flags));
  462. /*
  463. * Configure the RX antenna.
  464. */
  465. switch (ant->rx) {
  466. case ANTENNA_HW_DIVERSITY:
  467. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 2);
  468. break;
  469. case ANTENNA_A:
  470. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 3);
  471. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  472. break;
  473. case ANTENNA_SW_DIVERSITY:
  474. /*
  475. * NOTE: We should never come here because rt2x00lib is
  476. * supposed to catch this and send us the correct antenna
  477. * explicitely. However we are nog going to bug about this.
  478. * Instead, just default to antenna B.
  479. */
  480. case ANTENNA_B:
  481. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 0);
  482. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  483. break;
  484. }
  485. rt73usb_bbp_write(rt2x00dev, 77, r77);
  486. rt73usb_bbp_write(rt2x00dev, 3, r3);
  487. rt73usb_bbp_write(rt2x00dev, 4, r4);
  488. }
  489. struct antenna_sel {
  490. u8 word;
  491. /*
  492. * value[0] -> non-LNA
  493. * value[1] -> LNA
  494. */
  495. u8 value[2];
  496. };
  497. static const struct antenna_sel antenna_sel_a[] = {
  498. { 96, { 0x58, 0x78 } },
  499. { 104, { 0x38, 0x48 } },
  500. { 75, { 0xfe, 0x80 } },
  501. { 86, { 0xfe, 0x80 } },
  502. { 88, { 0xfe, 0x80 } },
  503. { 35, { 0x60, 0x60 } },
  504. { 97, { 0x58, 0x58 } },
  505. { 98, { 0x58, 0x58 } },
  506. };
  507. static const struct antenna_sel antenna_sel_bg[] = {
  508. { 96, { 0x48, 0x68 } },
  509. { 104, { 0x2c, 0x3c } },
  510. { 75, { 0xfe, 0x80 } },
  511. { 86, { 0xfe, 0x80 } },
  512. { 88, { 0xfe, 0x80 } },
  513. { 35, { 0x50, 0x50 } },
  514. { 97, { 0x48, 0x48 } },
  515. { 98, { 0x48, 0x48 } },
  516. };
  517. static void rt73usb_config_antenna(struct rt2x00_dev *rt2x00dev,
  518. struct antenna_setup *ant)
  519. {
  520. const struct antenna_sel *sel;
  521. unsigned int lna;
  522. unsigned int i;
  523. u32 reg;
  524. if (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ) {
  525. sel = antenna_sel_a;
  526. lna = test_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags);
  527. } else {
  528. sel = antenna_sel_bg;
  529. lna = test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags);
  530. }
  531. for (i = 0; i < ARRAY_SIZE(antenna_sel_a); i++)
  532. rt73usb_bbp_write(rt2x00dev, sel[i].word, sel[i].value[lna]);
  533. rt73usb_register_read(rt2x00dev, PHY_CSR0, &reg);
  534. rt2x00_set_field32(&reg, PHY_CSR0_PA_PE_BG,
  535. (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ));
  536. rt2x00_set_field32(&reg, PHY_CSR0_PA_PE_A,
  537. (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ));
  538. rt73usb_register_write(rt2x00dev, PHY_CSR0, reg);
  539. if (rt2x00_rf(&rt2x00dev->chip, RF5226) ||
  540. rt2x00_rf(&rt2x00dev->chip, RF5225))
  541. rt73usb_config_antenna_5x(rt2x00dev, ant);
  542. else if (rt2x00_rf(&rt2x00dev->chip, RF2528) ||
  543. rt2x00_rf(&rt2x00dev->chip, RF2527))
  544. rt73usb_config_antenna_2x(rt2x00dev, ant);
  545. }
  546. static void rt73usb_config_duration(struct rt2x00_dev *rt2x00dev,
  547. struct rt2x00lib_conf *libconf)
  548. {
  549. u32 reg;
  550. rt73usb_register_read(rt2x00dev, MAC_CSR9, &reg);
  551. rt2x00_set_field32(&reg, MAC_CSR9_SLOT_TIME, libconf->slot_time);
  552. rt73usb_register_write(rt2x00dev, MAC_CSR9, reg);
  553. rt73usb_register_read(rt2x00dev, MAC_CSR8, &reg);
  554. rt2x00_set_field32(&reg, MAC_CSR8_SIFS, libconf->sifs);
  555. rt2x00_set_field32(&reg, MAC_CSR8_SIFS_AFTER_RX_OFDM, 3);
  556. rt2x00_set_field32(&reg, MAC_CSR8_EIFS, libconf->eifs);
  557. rt73usb_register_write(rt2x00dev, MAC_CSR8, reg);
  558. rt73usb_register_read(rt2x00dev, TXRX_CSR0, &reg);
  559. rt2x00_set_field32(&reg, TXRX_CSR0_TSF_OFFSET, IEEE80211_HEADER);
  560. rt73usb_register_write(rt2x00dev, TXRX_CSR0, reg);
  561. rt73usb_register_read(rt2x00dev, TXRX_CSR4, &reg);
  562. rt2x00_set_field32(&reg, TXRX_CSR4_AUTORESPOND_ENABLE, 1);
  563. rt73usb_register_write(rt2x00dev, TXRX_CSR4, reg);
  564. rt73usb_register_read(rt2x00dev, TXRX_CSR9, &reg);
  565. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_INTERVAL,
  566. libconf->conf->beacon_int * 16);
  567. rt73usb_register_write(rt2x00dev, TXRX_CSR9, reg);
  568. }
  569. static void rt73usb_config(struct rt2x00_dev *rt2x00dev,
  570. struct rt2x00lib_conf *libconf,
  571. const unsigned int flags)
  572. {
  573. if (flags & CONFIG_UPDATE_PHYMODE)
  574. rt73usb_config_phymode(rt2x00dev, libconf->basic_rates);
  575. if (flags & CONFIG_UPDATE_CHANNEL)
  576. rt73usb_config_channel(rt2x00dev, &libconf->rf,
  577. libconf->conf->power_level);
  578. if ((flags & CONFIG_UPDATE_TXPOWER) && !(flags & CONFIG_UPDATE_CHANNEL))
  579. rt73usb_config_txpower(rt2x00dev, libconf->conf->power_level);
  580. if (flags & CONFIG_UPDATE_ANTENNA)
  581. rt73usb_config_antenna(rt2x00dev, &libconf->ant);
  582. if (flags & (CONFIG_UPDATE_SLOT_TIME | CONFIG_UPDATE_BEACON_INT))
  583. rt73usb_config_duration(rt2x00dev, libconf);
  584. }
  585. /*
  586. * Link tuning
  587. */
  588. static void rt73usb_link_stats(struct rt2x00_dev *rt2x00dev,
  589. struct link_qual *qual)
  590. {
  591. u32 reg;
  592. /*
  593. * Update FCS error count from register.
  594. */
  595. rt73usb_register_read(rt2x00dev, STA_CSR0, &reg);
  596. qual->rx_failed = rt2x00_get_field32(reg, STA_CSR0_FCS_ERROR);
  597. /*
  598. * Update False CCA count from register.
  599. */
  600. rt73usb_register_read(rt2x00dev, STA_CSR1, &reg);
  601. qual->false_cca = rt2x00_get_field32(reg, STA_CSR1_FALSE_CCA_ERROR);
  602. }
  603. static void rt73usb_reset_tuner(struct rt2x00_dev *rt2x00dev)
  604. {
  605. rt73usb_bbp_write(rt2x00dev, 17, 0x20);
  606. rt2x00dev->link.vgc_level = 0x20;
  607. }
  608. static void rt73usb_link_tuner(struct rt2x00_dev *rt2x00dev)
  609. {
  610. int rssi = rt2x00_get_link_rssi(&rt2x00dev->link);
  611. u8 r17;
  612. u8 up_bound;
  613. u8 low_bound;
  614. rt73usb_bbp_read(rt2x00dev, 17, &r17);
  615. /*
  616. * Determine r17 bounds.
  617. */
  618. if (rt2x00dev->rx_status.band == IEEE80211_BAND_5GHZ) {
  619. low_bound = 0x28;
  620. up_bound = 0x48;
  621. if (test_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags)) {
  622. low_bound += 0x10;
  623. up_bound += 0x10;
  624. }
  625. } else {
  626. if (rssi > -82) {
  627. low_bound = 0x1c;
  628. up_bound = 0x40;
  629. } else if (rssi > -84) {
  630. low_bound = 0x1c;
  631. up_bound = 0x20;
  632. } else {
  633. low_bound = 0x1c;
  634. up_bound = 0x1c;
  635. }
  636. if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags)) {
  637. low_bound += 0x14;
  638. up_bound += 0x10;
  639. }
  640. }
  641. /*
  642. * If we are not associated, we should go straight to the
  643. * dynamic CCA tuning.
  644. */
  645. if (!rt2x00dev->intf_associated)
  646. goto dynamic_cca_tune;
  647. /*
  648. * Special big-R17 for very short distance
  649. */
  650. if (rssi > -35) {
  651. if (r17 != 0x60)
  652. rt73usb_bbp_write(rt2x00dev, 17, 0x60);
  653. return;
  654. }
  655. /*
  656. * Special big-R17 for short distance
  657. */
  658. if (rssi >= -58) {
  659. if (r17 != up_bound)
  660. rt73usb_bbp_write(rt2x00dev, 17, up_bound);
  661. return;
  662. }
  663. /*
  664. * Special big-R17 for middle-short distance
  665. */
  666. if (rssi >= -66) {
  667. low_bound += 0x10;
  668. if (r17 != low_bound)
  669. rt73usb_bbp_write(rt2x00dev, 17, low_bound);
  670. return;
  671. }
  672. /*
  673. * Special mid-R17 for middle distance
  674. */
  675. if (rssi >= -74) {
  676. if (r17 != (low_bound + 0x10))
  677. rt73usb_bbp_write(rt2x00dev, 17, low_bound + 0x08);
  678. return;
  679. }
  680. /*
  681. * Special case: Change up_bound based on the rssi.
  682. * Lower up_bound when rssi is weaker then -74 dBm.
  683. */
  684. up_bound -= 2 * (-74 - rssi);
  685. if (low_bound > up_bound)
  686. up_bound = low_bound;
  687. if (r17 > up_bound) {
  688. rt73usb_bbp_write(rt2x00dev, 17, up_bound);
  689. return;
  690. }
  691. dynamic_cca_tune:
  692. /*
  693. * r17 does not yet exceed upper limit, continue and base
  694. * the r17 tuning on the false CCA count.
  695. */
  696. if (rt2x00dev->link.qual.false_cca > 512 && r17 < up_bound) {
  697. r17 += 4;
  698. if (r17 > up_bound)
  699. r17 = up_bound;
  700. rt73usb_bbp_write(rt2x00dev, 17, r17);
  701. } else if (rt2x00dev->link.qual.false_cca < 100 && r17 > low_bound) {
  702. r17 -= 4;
  703. if (r17 < low_bound)
  704. r17 = low_bound;
  705. rt73usb_bbp_write(rt2x00dev, 17, r17);
  706. }
  707. }
  708. /*
  709. * Firmware name function.
  710. */
  711. static char *rt73usb_get_firmware_name(struct rt2x00_dev *rt2x00dev)
  712. {
  713. return FIRMWARE_RT2571;
  714. }
  715. /*
  716. * Initialization functions.
  717. */
  718. static int rt73usb_load_firmware(struct rt2x00_dev *rt2x00dev, void *data,
  719. const size_t len)
  720. {
  721. unsigned int i;
  722. int status;
  723. u32 reg;
  724. char *ptr = data;
  725. char *cache;
  726. int buflen;
  727. int timeout;
  728. /*
  729. * Wait for stable hardware.
  730. */
  731. for (i = 0; i < 100; i++) {
  732. rt73usb_register_read(rt2x00dev, MAC_CSR0, &reg);
  733. if (reg)
  734. break;
  735. msleep(1);
  736. }
  737. if (!reg) {
  738. ERROR(rt2x00dev, "Unstable hardware.\n");
  739. return -EBUSY;
  740. }
  741. /*
  742. * Write firmware to device.
  743. * We setup a seperate cache for this action,
  744. * since we are going to write larger chunks of data
  745. * then normally used cache size.
  746. */
  747. cache = kmalloc(CSR_CACHE_SIZE_FIRMWARE, GFP_KERNEL);
  748. if (!cache) {
  749. ERROR(rt2x00dev, "Failed to allocate firmware cache.\n");
  750. return -ENOMEM;
  751. }
  752. for (i = 0; i < len; i += CSR_CACHE_SIZE_FIRMWARE) {
  753. buflen = min_t(int, len - i, CSR_CACHE_SIZE_FIRMWARE);
  754. timeout = REGISTER_TIMEOUT * (buflen / sizeof(u32));
  755. memcpy(cache, ptr, buflen);
  756. rt2x00usb_vendor_request(rt2x00dev, USB_MULTI_WRITE,
  757. USB_VENDOR_REQUEST_OUT,
  758. FIRMWARE_IMAGE_BASE + i, 0,
  759. cache, buflen, timeout);
  760. ptr += buflen;
  761. }
  762. kfree(cache);
  763. /*
  764. * Send firmware request to device to load firmware,
  765. * we need to specify a long timeout time.
  766. */
  767. status = rt2x00usb_vendor_request_sw(rt2x00dev, USB_DEVICE_MODE,
  768. 0, USB_MODE_FIRMWARE,
  769. REGISTER_TIMEOUT_FIRMWARE);
  770. if (status < 0) {
  771. ERROR(rt2x00dev, "Failed to write Firmware to device.\n");
  772. return status;
  773. }
  774. return 0;
  775. }
  776. static int rt73usb_init_registers(struct rt2x00_dev *rt2x00dev)
  777. {
  778. u32 reg;
  779. rt73usb_register_read(rt2x00dev, TXRX_CSR0, &reg);
  780. rt2x00_set_field32(&reg, TXRX_CSR0_AUTO_TX_SEQ, 1);
  781. rt2x00_set_field32(&reg, TXRX_CSR0_DISABLE_RX, 0);
  782. rt2x00_set_field32(&reg, TXRX_CSR0_TX_WITHOUT_WAITING, 0);
  783. rt73usb_register_write(rt2x00dev, TXRX_CSR0, reg);
  784. rt73usb_register_read(rt2x00dev, TXRX_CSR1, &reg);
  785. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID0, 47); /* CCK Signal */
  786. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID0_VALID, 1);
  787. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID1, 30); /* Rssi */
  788. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID1_VALID, 1);
  789. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID2, 42); /* OFDM Rate */
  790. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID2_VALID, 1);
  791. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID3, 30); /* Rssi */
  792. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID3_VALID, 1);
  793. rt73usb_register_write(rt2x00dev, TXRX_CSR1, reg);
  794. /*
  795. * CCK TXD BBP registers
  796. */
  797. rt73usb_register_read(rt2x00dev, TXRX_CSR2, &reg);
  798. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID0, 13);
  799. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID0_VALID, 1);
  800. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID1, 12);
  801. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID1_VALID, 1);
  802. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID2, 11);
  803. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID2_VALID, 1);
  804. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID3, 10);
  805. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID3_VALID, 1);
  806. rt73usb_register_write(rt2x00dev, TXRX_CSR2, reg);
  807. /*
  808. * OFDM TXD BBP registers
  809. */
  810. rt73usb_register_read(rt2x00dev, TXRX_CSR3, &reg);
  811. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID0, 7);
  812. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID0_VALID, 1);
  813. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID1, 6);
  814. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID1_VALID, 1);
  815. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID2, 5);
  816. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID2_VALID, 1);
  817. rt73usb_register_write(rt2x00dev, TXRX_CSR3, reg);
  818. rt73usb_register_read(rt2x00dev, TXRX_CSR7, &reg);
  819. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_6MBS, 59);
  820. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_9MBS, 53);
  821. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_12MBS, 49);
  822. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_18MBS, 46);
  823. rt73usb_register_write(rt2x00dev, TXRX_CSR7, reg);
  824. rt73usb_register_read(rt2x00dev, TXRX_CSR8, &reg);
  825. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_24MBS, 44);
  826. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_36MBS, 42);
  827. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_48MBS, 42);
  828. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_54MBS, 42);
  829. rt73usb_register_write(rt2x00dev, TXRX_CSR8, reg);
  830. rt73usb_register_write(rt2x00dev, TXRX_CSR15, 0x0000000f);
  831. rt73usb_register_read(rt2x00dev, MAC_CSR6, &reg);
  832. rt2x00_set_field32(&reg, MAC_CSR6_MAX_FRAME_UNIT, 0xfff);
  833. rt73usb_register_write(rt2x00dev, MAC_CSR6, reg);
  834. rt73usb_register_write(rt2x00dev, MAC_CSR10, 0x00000718);
  835. if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_AWAKE))
  836. return -EBUSY;
  837. rt73usb_register_write(rt2x00dev, MAC_CSR13, 0x00007f00);
  838. rt73usb_register_read(rt2x00dev, MAC_CSR14, &reg);
  839. rt2x00_set_field32(&reg, MAC_CSR14_ON_PERIOD, 70);
  840. rt2x00_set_field32(&reg, MAC_CSR14_OFF_PERIOD, 30);
  841. rt73usb_register_write(rt2x00dev, MAC_CSR14, reg);
  842. /*
  843. * Invalidate all Shared Keys (SEC_CSR0),
  844. * and clear the Shared key Cipher algorithms (SEC_CSR1 & SEC_CSR5)
  845. */
  846. rt73usb_register_write(rt2x00dev, SEC_CSR0, 0x00000000);
  847. rt73usb_register_write(rt2x00dev, SEC_CSR1, 0x00000000);
  848. rt73usb_register_write(rt2x00dev, SEC_CSR5, 0x00000000);
  849. reg = 0x000023b0;
  850. if (rt2x00_rf(&rt2x00dev->chip, RF5225) ||
  851. rt2x00_rf(&rt2x00dev->chip, RF2527))
  852. rt2x00_set_field32(&reg, PHY_CSR1_RF_RPI, 1);
  853. rt73usb_register_write(rt2x00dev, PHY_CSR1, reg);
  854. rt73usb_register_write(rt2x00dev, PHY_CSR5, 0x00040a06);
  855. rt73usb_register_write(rt2x00dev, PHY_CSR6, 0x00080606);
  856. rt73usb_register_write(rt2x00dev, PHY_CSR7, 0x00000408);
  857. rt73usb_register_read(rt2x00dev, AC_TXOP_CSR0, &reg);
  858. rt2x00_set_field32(&reg, AC_TXOP_CSR0_AC0_TX_OP, 0);
  859. rt2x00_set_field32(&reg, AC_TXOP_CSR0_AC1_TX_OP, 0);
  860. rt73usb_register_write(rt2x00dev, AC_TXOP_CSR0, reg);
  861. rt73usb_register_read(rt2x00dev, AC_TXOP_CSR1, &reg);
  862. rt2x00_set_field32(&reg, AC_TXOP_CSR1_AC2_TX_OP, 192);
  863. rt2x00_set_field32(&reg, AC_TXOP_CSR1_AC3_TX_OP, 48);
  864. rt73usb_register_write(rt2x00dev, AC_TXOP_CSR1, reg);
  865. rt73usb_register_read(rt2x00dev, MAC_CSR9, &reg);
  866. rt2x00_set_field32(&reg, MAC_CSR9_CW_SELECT, 0);
  867. rt73usb_register_write(rt2x00dev, MAC_CSR9, reg);
  868. /*
  869. * Clear all beacons
  870. * For the Beacon base registers we only need to clear
  871. * the first byte since that byte contains the VALID and OWNER
  872. * bits which (when set to 0) will invalidate the entire beacon.
  873. */
  874. rt73usb_register_write(rt2x00dev, HW_BEACON_BASE0, 0);
  875. rt73usb_register_write(rt2x00dev, HW_BEACON_BASE1, 0);
  876. rt73usb_register_write(rt2x00dev, HW_BEACON_BASE2, 0);
  877. rt73usb_register_write(rt2x00dev, HW_BEACON_BASE3, 0);
  878. /*
  879. * We must clear the error counters.
  880. * These registers are cleared on read,
  881. * so we may pass a useless variable to store the value.
  882. */
  883. rt73usb_register_read(rt2x00dev, STA_CSR0, &reg);
  884. rt73usb_register_read(rt2x00dev, STA_CSR1, &reg);
  885. rt73usb_register_read(rt2x00dev, STA_CSR2, &reg);
  886. /*
  887. * Reset MAC and BBP registers.
  888. */
  889. rt73usb_register_read(rt2x00dev, MAC_CSR1, &reg);
  890. rt2x00_set_field32(&reg, MAC_CSR1_SOFT_RESET, 1);
  891. rt2x00_set_field32(&reg, MAC_CSR1_BBP_RESET, 1);
  892. rt73usb_register_write(rt2x00dev, MAC_CSR1, reg);
  893. rt73usb_register_read(rt2x00dev, MAC_CSR1, &reg);
  894. rt2x00_set_field32(&reg, MAC_CSR1_SOFT_RESET, 0);
  895. rt2x00_set_field32(&reg, MAC_CSR1_BBP_RESET, 0);
  896. rt73usb_register_write(rt2x00dev, MAC_CSR1, reg);
  897. rt73usb_register_read(rt2x00dev, MAC_CSR1, &reg);
  898. rt2x00_set_field32(&reg, MAC_CSR1_HOST_READY, 1);
  899. rt73usb_register_write(rt2x00dev, MAC_CSR1, reg);
  900. return 0;
  901. }
  902. static int rt73usb_init_bbp(struct rt2x00_dev *rt2x00dev)
  903. {
  904. unsigned int i;
  905. u16 eeprom;
  906. u8 reg_id;
  907. u8 value;
  908. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  909. rt73usb_bbp_read(rt2x00dev, 0, &value);
  910. if ((value != 0xff) && (value != 0x00))
  911. goto continue_csr_init;
  912. NOTICE(rt2x00dev, "Waiting for BBP register.\n");
  913. udelay(REGISTER_BUSY_DELAY);
  914. }
  915. ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
  916. return -EACCES;
  917. continue_csr_init:
  918. rt73usb_bbp_write(rt2x00dev, 3, 0x80);
  919. rt73usb_bbp_write(rt2x00dev, 15, 0x30);
  920. rt73usb_bbp_write(rt2x00dev, 21, 0xc8);
  921. rt73usb_bbp_write(rt2x00dev, 22, 0x38);
  922. rt73usb_bbp_write(rt2x00dev, 23, 0x06);
  923. rt73usb_bbp_write(rt2x00dev, 24, 0xfe);
  924. rt73usb_bbp_write(rt2x00dev, 25, 0x0a);
  925. rt73usb_bbp_write(rt2x00dev, 26, 0x0d);
  926. rt73usb_bbp_write(rt2x00dev, 32, 0x0b);
  927. rt73usb_bbp_write(rt2x00dev, 34, 0x12);
  928. rt73usb_bbp_write(rt2x00dev, 37, 0x07);
  929. rt73usb_bbp_write(rt2x00dev, 39, 0xf8);
  930. rt73usb_bbp_write(rt2x00dev, 41, 0x60);
  931. rt73usb_bbp_write(rt2x00dev, 53, 0x10);
  932. rt73usb_bbp_write(rt2x00dev, 54, 0x18);
  933. rt73usb_bbp_write(rt2x00dev, 60, 0x10);
  934. rt73usb_bbp_write(rt2x00dev, 61, 0x04);
  935. rt73usb_bbp_write(rt2x00dev, 62, 0x04);
  936. rt73usb_bbp_write(rt2x00dev, 75, 0xfe);
  937. rt73usb_bbp_write(rt2x00dev, 86, 0xfe);
  938. rt73usb_bbp_write(rt2x00dev, 88, 0xfe);
  939. rt73usb_bbp_write(rt2x00dev, 90, 0x0f);
  940. rt73usb_bbp_write(rt2x00dev, 99, 0x00);
  941. rt73usb_bbp_write(rt2x00dev, 102, 0x16);
  942. rt73usb_bbp_write(rt2x00dev, 107, 0x04);
  943. for (i = 0; i < EEPROM_BBP_SIZE; i++) {
  944. rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
  945. if (eeprom != 0xffff && eeprom != 0x0000) {
  946. reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
  947. value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
  948. rt73usb_bbp_write(rt2x00dev, reg_id, value);
  949. }
  950. }
  951. return 0;
  952. }
  953. /*
  954. * Device state switch handlers.
  955. */
  956. static void rt73usb_toggle_rx(struct rt2x00_dev *rt2x00dev,
  957. enum dev_state state)
  958. {
  959. u32 reg;
  960. rt73usb_register_read(rt2x00dev, TXRX_CSR0, &reg);
  961. rt2x00_set_field32(&reg, TXRX_CSR0_DISABLE_RX,
  962. state == STATE_RADIO_RX_OFF);
  963. rt73usb_register_write(rt2x00dev, TXRX_CSR0, reg);
  964. }
  965. static int rt73usb_enable_radio(struct rt2x00_dev *rt2x00dev)
  966. {
  967. /*
  968. * Initialize all registers.
  969. */
  970. if (rt73usb_init_registers(rt2x00dev) ||
  971. rt73usb_init_bbp(rt2x00dev)) {
  972. ERROR(rt2x00dev, "Register initialization failed.\n");
  973. return -EIO;
  974. }
  975. return 0;
  976. }
  977. static void rt73usb_disable_radio(struct rt2x00_dev *rt2x00dev)
  978. {
  979. rt73usb_register_write(rt2x00dev, MAC_CSR10, 0x00001818);
  980. /*
  981. * Disable synchronisation.
  982. */
  983. rt73usb_register_write(rt2x00dev, TXRX_CSR9, 0);
  984. rt2x00usb_disable_radio(rt2x00dev);
  985. }
  986. static int rt73usb_set_state(struct rt2x00_dev *rt2x00dev, enum dev_state state)
  987. {
  988. u32 reg;
  989. unsigned int i;
  990. char put_to_sleep;
  991. char current_state;
  992. put_to_sleep = (state != STATE_AWAKE);
  993. rt73usb_register_read(rt2x00dev, MAC_CSR12, &reg);
  994. rt2x00_set_field32(&reg, MAC_CSR12_FORCE_WAKEUP, !put_to_sleep);
  995. rt2x00_set_field32(&reg, MAC_CSR12_PUT_TO_SLEEP, put_to_sleep);
  996. rt73usb_register_write(rt2x00dev, MAC_CSR12, reg);
  997. /*
  998. * Device is not guaranteed to be in the requested state yet.
  999. * We must wait until the register indicates that the
  1000. * device has entered the correct state.
  1001. */
  1002. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  1003. rt73usb_register_read(rt2x00dev, MAC_CSR12, &reg);
  1004. current_state =
  1005. rt2x00_get_field32(reg, MAC_CSR12_BBP_CURRENT_STATE);
  1006. if (current_state == !put_to_sleep)
  1007. return 0;
  1008. msleep(10);
  1009. }
  1010. NOTICE(rt2x00dev, "Device failed to enter state %d, "
  1011. "current device state %d.\n", !put_to_sleep, current_state);
  1012. return -EBUSY;
  1013. }
  1014. static int rt73usb_set_device_state(struct rt2x00_dev *rt2x00dev,
  1015. enum dev_state state)
  1016. {
  1017. int retval = 0;
  1018. switch (state) {
  1019. case STATE_RADIO_ON:
  1020. retval = rt73usb_enable_radio(rt2x00dev);
  1021. break;
  1022. case STATE_RADIO_OFF:
  1023. rt73usb_disable_radio(rt2x00dev);
  1024. break;
  1025. case STATE_RADIO_RX_ON:
  1026. case STATE_RADIO_RX_ON_LINK:
  1027. rt73usb_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
  1028. break;
  1029. case STATE_RADIO_RX_OFF:
  1030. case STATE_RADIO_RX_OFF_LINK:
  1031. rt73usb_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
  1032. break;
  1033. case STATE_DEEP_SLEEP:
  1034. case STATE_SLEEP:
  1035. case STATE_STANDBY:
  1036. case STATE_AWAKE:
  1037. retval = rt73usb_set_state(rt2x00dev, state);
  1038. break;
  1039. default:
  1040. retval = -ENOTSUPP;
  1041. break;
  1042. }
  1043. return retval;
  1044. }
  1045. /*
  1046. * TX descriptor initialization
  1047. */
  1048. static void rt73usb_write_tx_desc(struct rt2x00_dev *rt2x00dev,
  1049. struct sk_buff *skb,
  1050. struct txentry_desc *txdesc,
  1051. struct ieee80211_tx_control *control)
  1052. {
  1053. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  1054. __le32 *txd = skbdesc->desc;
  1055. u32 word;
  1056. /*
  1057. * Start writing the descriptor words.
  1058. */
  1059. rt2x00_desc_read(txd, 1, &word);
  1060. rt2x00_set_field32(&word, TXD_W1_HOST_Q_ID, txdesc->queue);
  1061. rt2x00_set_field32(&word, TXD_W1_AIFSN, txdesc->aifs);
  1062. rt2x00_set_field32(&word, TXD_W1_CWMIN, txdesc->cw_min);
  1063. rt2x00_set_field32(&word, TXD_W1_CWMAX, txdesc->cw_max);
  1064. rt2x00_set_field32(&word, TXD_W1_IV_OFFSET, IEEE80211_HEADER);
  1065. rt2x00_set_field32(&word, TXD_W1_HW_SEQUENCE, 1);
  1066. rt2x00_desc_write(txd, 1, word);
  1067. rt2x00_desc_read(txd, 2, &word);
  1068. rt2x00_set_field32(&word, TXD_W2_PLCP_SIGNAL, txdesc->signal);
  1069. rt2x00_set_field32(&word, TXD_W2_PLCP_SERVICE, txdesc->service);
  1070. rt2x00_set_field32(&word, TXD_W2_PLCP_LENGTH_LOW, txdesc->length_low);
  1071. rt2x00_set_field32(&word, TXD_W2_PLCP_LENGTH_HIGH, txdesc->length_high);
  1072. rt2x00_desc_write(txd, 2, word);
  1073. rt2x00_desc_read(txd, 5, &word);
  1074. rt2x00_set_field32(&word, TXD_W5_TX_POWER,
  1075. TXPOWER_TO_DEV(rt2x00dev->tx_power));
  1076. rt2x00_set_field32(&word, TXD_W5_WAITING_DMA_DONE_INT, 1);
  1077. rt2x00_desc_write(txd, 5, word);
  1078. rt2x00_desc_read(txd, 0, &word);
  1079. rt2x00_set_field32(&word, TXD_W0_BURST,
  1080. test_bit(ENTRY_TXD_BURST, &txdesc->flags));
  1081. rt2x00_set_field32(&word, TXD_W0_VALID, 1);
  1082. rt2x00_set_field32(&word, TXD_W0_MORE_FRAG,
  1083. test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
  1084. rt2x00_set_field32(&word, TXD_W0_ACK,
  1085. test_bit(ENTRY_TXD_ACK, &txdesc->flags));
  1086. rt2x00_set_field32(&word, TXD_W0_TIMESTAMP,
  1087. test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
  1088. rt2x00_set_field32(&word, TXD_W0_OFDM,
  1089. test_bit(ENTRY_TXD_OFDM_RATE, &txdesc->flags));
  1090. rt2x00_set_field32(&word, TXD_W0_IFS, txdesc->ifs);
  1091. rt2x00_set_field32(&word, TXD_W0_RETRY_MODE,
  1092. !!(control->flags &
  1093. IEEE80211_TXCTL_LONG_RETRY_LIMIT));
  1094. rt2x00_set_field32(&word, TXD_W0_TKIP_MIC, 0);
  1095. rt2x00_set_field32(&word, TXD_W0_DATABYTE_COUNT, skbdesc->data_len);
  1096. rt2x00_set_field32(&word, TXD_W0_BURST2,
  1097. test_bit(ENTRY_TXD_BURST, &txdesc->flags));
  1098. rt2x00_set_field32(&word, TXD_W0_CIPHER_ALG, CIPHER_NONE);
  1099. rt2x00_desc_write(txd, 0, word);
  1100. }
  1101. static int rt73usb_get_tx_data_len(struct rt2x00_dev *rt2x00dev,
  1102. struct sk_buff *skb)
  1103. {
  1104. int length;
  1105. /*
  1106. * The length _must_ be a multiple of 4,
  1107. * but it must _not_ be a multiple of the USB packet size.
  1108. */
  1109. length = roundup(skb->len, 4);
  1110. length += (4 * !(length % rt2x00dev->usb_maxpacket));
  1111. return length;
  1112. }
  1113. /*
  1114. * TX data initialization
  1115. */
  1116. static void rt73usb_kick_tx_queue(struct rt2x00_dev *rt2x00dev,
  1117. const unsigned int queue)
  1118. {
  1119. u32 reg;
  1120. if (queue != RT2X00_BCN_QUEUE_BEACON)
  1121. return;
  1122. /*
  1123. * For Wi-Fi faily generated beacons between participating stations.
  1124. * Set TBTT phase adaptive adjustment step to 8us (default 16us)
  1125. */
  1126. rt73usb_register_write(rt2x00dev, TXRX_CSR10, 0x00001008);
  1127. rt73usb_register_read(rt2x00dev, TXRX_CSR9, &reg);
  1128. if (!rt2x00_get_field32(reg, TXRX_CSR9_BEACON_GEN)) {
  1129. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 1);
  1130. rt73usb_register_write(rt2x00dev, TXRX_CSR9, reg);
  1131. }
  1132. }
  1133. /*
  1134. * RX control handlers
  1135. */
  1136. static int rt73usb_agc_to_rssi(struct rt2x00_dev *rt2x00dev, int rxd_w1)
  1137. {
  1138. u16 eeprom;
  1139. u8 offset;
  1140. u8 lna;
  1141. lna = rt2x00_get_field32(rxd_w1, RXD_W1_RSSI_LNA);
  1142. switch (lna) {
  1143. case 3:
  1144. offset = 90;
  1145. break;
  1146. case 2:
  1147. offset = 74;
  1148. break;
  1149. case 1:
  1150. offset = 64;
  1151. break;
  1152. default:
  1153. return 0;
  1154. }
  1155. if (rt2x00dev->rx_status.band == IEEE80211_BAND_5GHZ) {
  1156. if (test_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags)) {
  1157. if (lna == 3 || lna == 2)
  1158. offset += 10;
  1159. } else {
  1160. if (lna == 3)
  1161. offset += 6;
  1162. else if (lna == 2)
  1163. offset += 8;
  1164. }
  1165. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_A, &eeprom);
  1166. offset -= rt2x00_get_field16(eeprom, EEPROM_RSSI_OFFSET_A_1);
  1167. } else {
  1168. if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags))
  1169. offset += 14;
  1170. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_BG, &eeprom);
  1171. offset -= rt2x00_get_field16(eeprom, EEPROM_RSSI_OFFSET_BG_1);
  1172. }
  1173. return rt2x00_get_field32(rxd_w1, RXD_W1_RSSI_AGC) * 2 - offset;
  1174. }
  1175. static void rt73usb_fill_rxdone(struct queue_entry *entry,
  1176. struct rxdone_entry_desc *rxdesc)
  1177. {
  1178. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  1179. __le32 *rxd = (__le32 *)entry->skb->data;
  1180. struct ieee80211_hdr *hdr =
  1181. (struct ieee80211_hdr *)entry->skb->data + entry->queue->desc_size;
  1182. int header_size = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
  1183. u32 word0;
  1184. u32 word1;
  1185. rt2x00_desc_read(rxd, 0, &word0);
  1186. rt2x00_desc_read(rxd, 1, &word1);
  1187. rxdesc->flags = 0;
  1188. if (rt2x00_get_field32(word0, RXD_W0_CRC_ERROR))
  1189. rxdesc->flags |= RX_FLAG_FAILED_FCS_CRC;
  1190. /*
  1191. * Obtain the status about this packet.
  1192. */
  1193. rxdesc->signal = rt2x00_get_field32(word1, RXD_W1_SIGNAL);
  1194. rxdesc->rssi = rt73usb_agc_to_rssi(entry->queue->rt2x00dev, word1);
  1195. rxdesc->ofdm = rt2x00_get_field32(word0, RXD_W0_OFDM);
  1196. rxdesc->size = rt2x00_get_field32(word0, RXD_W0_DATABYTE_COUNT);
  1197. rxdesc->my_bss = !!rt2x00_get_field32(word0, RXD_W0_MY_BSS);
  1198. /*
  1199. * The data behind the ieee80211 header must be
  1200. * aligned on a 4 byte boundary.
  1201. */
  1202. if (header_size % 4 == 0) {
  1203. skb_push(entry->skb, 2);
  1204. memmove(entry->skb->data, entry->skb->data + 2,
  1205. entry->skb->len - 2);
  1206. }
  1207. /*
  1208. * Set descriptor and data pointer.
  1209. */
  1210. skbdesc->data = entry->skb->data + entry->queue->desc_size;
  1211. skbdesc->data_len = rxdesc->size;
  1212. skbdesc->desc = entry->skb->data;
  1213. skbdesc->desc_len = entry->queue->desc_size;
  1214. /*
  1215. * Remove descriptor from skb buffer and trim the whole thing
  1216. * down to only contain data.
  1217. */
  1218. skb_pull(entry->skb, skbdesc->desc_len);
  1219. skb_trim(entry->skb, rxdesc->size);
  1220. }
  1221. /*
  1222. * Device probe functions.
  1223. */
  1224. static int rt73usb_validate_eeprom(struct rt2x00_dev *rt2x00dev)
  1225. {
  1226. u16 word;
  1227. u8 *mac;
  1228. s8 value;
  1229. rt2x00usb_eeprom_read(rt2x00dev, rt2x00dev->eeprom, EEPROM_SIZE);
  1230. /*
  1231. * Start validation of the data that has been read.
  1232. */
  1233. mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
  1234. if (!is_valid_ether_addr(mac)) {
  1235. DECLARE_MAC_BUF(macbuf);
  1236. random_ether_addr(mac);
  1237. EEPROM(rt2x00dev, "MAC: %s\n", print_mac(macbuf, mac));
  1238. }
  1239. rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &word);
  1240. if (word == 0xffff) {
  1241. rt2x00_set_field16(&word, EEPROM_ANTENNA_NUM, 2);
  1242. rt2x00_set_field16(&word, EEPROM_ANTENNA_TX_DEFAULT,
  1243. ANTENNA_B);
  1244. rt2x00_set_field16(&word, EEPROM_ANTENNA_RX_DEFAULT,
  1245. ANTENNA_B);
  1246. rt2x00_set_field16(&word, EEPROM_ANTENNA_FRAME_TYPE, 0);
  1247. rt2x00_set_field16(&word, EEPROM_ANTENNA_DYN_TXAGC, 0);
  1248. rt2x00_set_field16(&word, EEPROM_ANTENNA_HARDWARE_RADIO, 0);
  1249. rt2x00_set_field16(&word, EEPROM_ANTENNA_RF_TYPE, RF5226);
  1250. rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
  1251. EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
  1252. }
  1253. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &word);
  1254. if (word == 0xffff) {
  1255. rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA, 0);
  1256. rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC, word);
  1257. EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
  1258. }
  1259. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED, &word);
  1260. if (word == 0xffff) {
  1261. rt2x00_set_field16(&word, EEPROM_LED_POLARITY_RDY_G, 0);
  1262. rt2x00_set_field16(&word, EEPROM_LED_POLARITY_RDY_A, 0);
  1263. rt2x00_set_field16(&word, EEPROM_LED_POLARITY_ACT, 0);
  1264. rt2x00_set_field16(&word, EEPROM_LED_POLARITY_GPIO_0, 0);
  1265. rt2x00_set_field16(&word, EEPROM_LED_POLARITY_GPIO_1, 0);
  1266. rt2x00_set_field16(&word, EEPROM_LED_POLARITY_GPIO_2, 0);
  1267. rt2x00_set_field16(&word, EEPROM_LED_POLARITY_GPIO_3, 0);
  1268. rt2x00_set_field16(&word, EEPROM_LED_POLARITY_GPIO_4, 0);
  1269. rt2x00_set_field16(&word, EEPROM_LED_LED_MODE,
  1270. LED_MODE_DEFAULT);
  1271. rt2x00_eeprom_write(rt2x00dev, EEPROM_LED, word);
  1272. EEPROM(rt2x00dev, "Led: 0x%04x\n", word);
  1273. }
  1274. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
  1275. if (word == 0xffff) {
  1276. rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
  1277. rt2x00_set_field16(&word, EEPROM_FREQ_SEQ, 0);
  1278. rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
  1279. EEPROM(rt2x00dev, "Freq: 0x%04x\n", word);
  1280. }
  1281. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_BG, &word);
  1282. if (word == 0xffff) {
  1283. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_1, 0);
  1284. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_2, 0);
  1285. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_BG, word);
  1286. EEPROM(rt2x00dev, "RSSI OFFSET BG: 0x%04x\n", word);
  1287. } else {
  1288. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_BG_1);
  1289. if (value < -10 || value > 10)
  1290. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_1, 0);
  1291. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_BG_2);
  1292. if (value < -10 || value > 10)
  1293. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_2, 0);
  1294. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_BG, word);
  1295. }
  1296. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_A, &word);
  1297. if (word == 0xffff) {
  1298. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_1, 0);
  1299. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_2, 0);
  1300. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_A, word);
  1301. EEPROM(rt2x00dev, "RSSI OFFSET A: 0x%04x\n", word);
  1302. } else {
  1303. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_A_1);
  1304. if (value < -10 || value > 10)
  1305. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_1, 0);
  1306. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_A_2);
  1307. if (value < -10 || value > 10)
  1308. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_2, 0);
  1309. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_A, word);
  1310. }
  1311. return 0;
  1312. }
  1313. static int rt73usb_init_eeprom(struct rt2x00_dev *rt2x00dev)
  1314. {
  1315. u32 reg;
  1316. u16 value;
  1317. u16 eeprom;
  1318. /*
  1319. * Read EEPROM word for configuration.
  1320. */
  1321. rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
  1322. /*
  1323. * Identify RF chipset.
  1324. */
  1325. value = rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RF_TYPE);
  1326. rt73usb_register_read(rt2x00dev, MAC_CSR0, &reg);
  1327. rt2x00_set_chip(rt2x00dev, RT2571, value, reg);
  1328. if (!rt2x00_check_rev(&rt2x00dev->chip, 0x25730)) {
  1329. ERROR(rt2x00dev, "Invalid RT chipset detected.\n");
  1330. return -ENODEV;
  1331. }
  1332. if (!rt2x00_rf(&rt2x00dev->chip, RF5226) &&
  1333. !rt2x00_rf(&rt2x00dev->chip, RF2528) &&
  1334. !rt2x00_rf(&rt2x00dev->chip, RF5225) &&
  1335. !rt2x00_rf(&rt2x00dev->chip, RF2527)) {
  1336. ERROR(rt2x00dev, "Invalid RF chipset detected.\n");
  1337. return -ENODEV;
  1338. }
  1339. /*
  1340. * Identify default antenna configuration.
  1341. */
  1342. rt2x00dev->default_ant.tx =
  1343. rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TX_DEFAULT);
  1344. rt2x00dev->default_ant.rx =
  1345. rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RX_DEFAULT);
  1346. /*
  1347. * Read the Frame type.
  1348. */
  1349. if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_FRAME_TYPE))
  1350. __set_bit(CONFIG_FRAME_TYPE, &rt2x00dev->flags);
  1351. /*
  1352. * Read frequency offset.
  1353. */
  1354. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
  1355. rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
  1356. /*
  1357. * Read external LNA informations.
  1358. */
  1359. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
  1360. if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA)) {
  1361. __set_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags);
  1362. __set_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags);
  1363. }
  1364. /*
  1365. * Store led settings, for correct led behaviour.
  1366. */
  1367. #ifdef CONFIG_RT73USB_LEDS
  1368. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED, &eeprom);
  1369. switch (value) {
  1370. case LED_MODE_TXRX_ACTIVITY:
  1371. case LED_MODE_ASUS:
  1372. case LED_MODE_ALPHA:
  1373. case LED_MODE_DEFAULT:
  1374. rt2x00dev->led_flags =
  1375. LED_SUPPORT_RADIO | LED_SUPPORT_ASSOC;
  1376. break;
  1377. case LED_MODE_SIGNAL_STRENGTH:
  1378. rt2x00dev->led_flags =
  1379. LED_SUPPORT_RADIO | LED_SUPPORT_ASSOC |
  1380. LED_SUPPORT_QUALITY;
  1381. break;
  1382. }
  1383. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_LED_MODE, value);
  1384. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_GPIO_0,
  1385. rt2x00_get_field16(eeprom,
  1386. EEPROM_LED_POLARITY_GPIO_0));
  1387. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_GPIO_1,
  1388. rt2x00_get_field16(eeprom,
  1389. EEPROM_LED_POLARITY_GPIO_1));
  1390. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_GPIO_2,
  1391. rt2x00_get_field16(eeprom,
  1392. EEPROM_LED_POLARITY_GPIO_2));
  1393. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_GPIO_3,
  1394. rt2x00_get_field16(eeprom,
  1395. EEPROM_LED_POLARITY_GPIO_3));
  1396. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_GPIO_4,
  1397. rt2x00_get_field16(eeprom,
  1398. EEPROM_LED_POLARITY_GPIO_4));
  1399. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_ACT,
  1400. rt2x00_get_field16(eeprom, EEPROM_LED_POLARITY_ACT));
  1401. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_READY_BG,
  1402. rt2x00_get_field16(eeprom,
  1403. EEPROM_LED_POLARITY_RDY_G));
  1404. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_READY_A,
  1405. rt2x00_get_field16(eeprom,
  1406. EEPROM_LED_POLARITY_RDY_A));
  1407. #endif /* CONFIG_RT73USB_LEDS */
  1408. return 0;
  1409. }
  1410. /*
  1411. * RF value list for RF2528
  1412. * Supports: 2.4 GHz
  1413. */
  1414. static const struct rf_channel rf_vals_bg_2528[] = {
  1415. { 1, 0x00002c0c, 0x00000786, 0x00068255, 0x000fea0b },
  1416. { 2, 0x00002c0c, 0x00000786, 0x00068255, 0x000fea1f },
  1417. { 3, 0x00002c0c, 0x0000078a, 0x00068255, 0x000fea0b },
  1418. { 4, 0x00002c0c, 0x0000078a, 0x00068255, 0x000fea1f },
  1419. { 5, 0x00002c0c, 0x0000078e, 0x00068255, 0x000fea0b },
  1420. { 6, 0x00002c0c, 0x0000078e, 0x00068255, 0x000fea1f },
  1421. { 7, 0x00002c0c, 0x00000792, 0x00068255, 0x000fea0b },
  1422. { 8, 0x00002c0c, 0x00000792, 0x00068255, 0x000fea1f },
  1423. { 9, 0x00002c0c, 0x00000796, 0x00068255, 0x000fea0b },
  1424. { 10, 0x00002c0c, 0x00000796, 0x00068255, 0x000fea1f },
  1425. { 11, 0x00002c0c, 0x0000079a, 0x00068255, 0x000fea0b },
  1426. { 12, 0x00002c0c, 0x0000079a, 0x00068255, 0x000fea1f },
  1427. { 13, 0x00002c0c, 0x0000079e, 0x00068255, 0x000fea0b },
  1428. { 14, 0x00002c0c, 0x000007a2, 0x00068255, 0x000fea13 },
  1429. };
  1430. /*
  1431. * RF value list for RF5226
  1432. * Supports: 2.4 GHz & 5.2 GHz
  1433. */
  1434. static const struct rf_channel rf_vals_5226[] = {
  1435. { 1, 0x00002c0c, 0x00000786, 0x00068255, 0x000fea0b },
  1436. { 2, 0x00002c0c, 0x00000786, 0x00068255, 0x000fea1f },
  1437. { 3, 0x00002c0c, 0x0000078a, 0x00068255, 0x000fea0b },
  1438. { 4, 0x00002c0c, 0x0000078a, 0x00068255, 0x000fea1f },
  1439. { 5, 0x00002c0c, 0x0000078e, 0x00068255, 0x000fea0b },
  1440. { 6, 0x00002c0c, 0x0000078e, 0x00068255, 0x000fea1f },
  1441. { 7, 0x00002c0c, 0x00000792, 0x00068255, 0x000fea0b },
  1442. { 8, 0x00002c0c, 0x00000792, 0x00068255, 0x000fea1f },
  1443. { 9, 0x00002c0c, 0x00000796, 0x00068255, 0x000fea0b },
  1444. { 10, 0x00002c0c, 0x00000796, 0x00068255, 0x000fea1f },
  1445. { 11, 0x00002c0c, 0x0000079a, 0x00068255, 0x000fea0b },
  1446. { 12, 0x00002c0c, 0x0000079a, 0x00068255, 0x000fea1f },
  1447. { 13, 0x00002c0c, 0x0000079e, 0x00068255, 0x000fea0b },
  1448. { 14, 0x00002c0c, 0x000007a2, 0x00068255, 0x000fea13 },
  1449. /* 802.11 UNI / HyperLan 2 */
  1450. { 36, 0x00002c0c, 0x0000099a, 0x00098255, 0x000fea23 },
  1451. { 40, 0x00002c0c, 0x000009a2, 0x00098255, 0x000fea03 },
  1452. { 44, 0x00002c0c, 0x000009a6, 0x00098255, 0x000fea0b },
  1453. { 48, 0x00002c0c, 0x000009aa, 0x00098255, 0x000fea13 },
  1454. { 52, 0x00002c0c, 0x000009ae, 0x00098255, 0x000fea1b },
  1455. { 56, 0x00002c0c, 0x000009b2, 0x00098255, 0x000fea23 },
  1456. { 60, 0x00002c0c, 0x000009ba, 0x00098255, 0x000fea03 },
  1457. { 64, 0x00002c0c, 0x000009be, 0x00098255, 0x000fea0b },
  1458. /* 802.11 HyperLan 2 */
  1459. { 100, 0x00002c0c, 0x00000a2a, 0x000b8255, 0x000fea03 },
  1460. { 104, 0x00002c0c, 0x00000a2e, 0x000b8255, 0x000fea0b },
  1461. { 108, 0x00002c0c, 0x00000a32, 0x000b8255, 0x000fea13 },
  1462. { 112, 0x00002c0c, 0x00000a36, 0x000b8255, 0x000fea1b },
  1463. { 116, 0x00002c0c, 0x00000a3a, 0x000b8255, 0x000fea23 },
  1464. { 120, 0x00002c0c, 0x00000a82, 0x000b8255, 0x000fea03 },
  1465. { 124, 0x00002c0c, 0x00000a86, 0x000b8255, 0x000fea0b },
  1466. { 128, 0x00002c0c, 0x00000a8a, 0x000b8255, 0x000fea13 },
  1467. { 132, 0x00002c0c, 0x00000a8e, 0x000b8255, 0x000fea1b },
  1468. { 136, 0x00002c0c, 0x00000a92, 0x000b8255, 0x000fea23 },
  1469. /* 802.11 UNII */
  1470. { 140, 0x00002c0c, 0x00000a9a, 0x000b8255, 0x000fea03 },
  1471. { 149, 0x00002c0c, 0x00000aa2, 0x000b8255, 0x000fea1f },
  1472. { 153, 0x00002c0c, 0x00000aa6, 0x000b8255, 0x000fea27 },
  1473. { 157, 0x00002c0c, 0x00000aae, 0x000b8255, 0x000fea07 },
  1474. { 161, 0x00002c0c, 0x00000ab2, 0x000b8255, 0x000fea0f },
  1475. { 165, 0x00002c0c, 0x00000ab6, 0x000b8255, 0x000fea17 },
  1476. /* MMAC(Japan)J52 ch 34,38,42,46 */
  1477. { 34, 0x00002c0c, 0x0008099a, 0x000da255, 0x000d3a0b },
  1478. { 38, 0x00002c0c, 0x0008099e, 0x000da255, 0x000d3a13 },
  1479. { 42, 0x00002c0c, 0x000809a2, 0x000da255, 0x000d3a1b },
  1480. { 46, 0x00002c0c, 0x000809a6, 0x000da255, 0x000d3a23 },
  1481. };
  1482. /*
  1483. * RF value list for RF5225 & RF2527
  1484. * Supports: 2.4 GHz & 5.2 GHz
  1485. */
  1486. static const struct rf_channel rf_vals_5225_2527[] = {
  1487. { 1, 0x00002ccc, 0x00004786, 0x00068455, 0x000ffa0b },
  1488. { 2, 0x00002ccc, 0x00004786, 0x00068455, 0x000ffa1f },
  1489. { 3, 0x00002ccc, 0x0000478a, 0x00068455, 0x000ffa0b },
  1490. { 4, 0x00002ccc, 0x0000478a, 0x00068455, 0x000ffa1f },
  1491. { 5, 0x00002ccc, 0x0000478e, 0x00068455, 0x000ffa0b },
  1492. { 6, 0x00002ccc, 0x0000478e, 0x00068455, 0x000ffa1f },
  1493. { 7, 0x00002ccc, 0x00004792, 0x00068455, 0x000ffa0b },
  1494. { 8, 0x00002ccc, 0x00004792, 0x00068455, 0x000ffa1f },
  1495. { 9, 0x00002ccc, 0x00004796, 0x00068455, 0x000ffa0b },
  1496. { 10, 0x00002ccc, 0x00004796, 0x00068455, 0x000ffa1f },
  1497. { 11, 0x00002ccc, 0x0000479a, 0x00068455, 0x000ffa0b },
  1498. { 12, 0x00002ccc, 0x0000479a, 0x00068455, 0x000ffa1f },
  1499. { 13, 0x00002ccc, 0x0000479e, 0x00068455, 0x000ffa0b },
  1500. { 14, 0x00002ccc, 0x000047a2, 0x00068455, 0x000ffa13 },
  1501. /* 802.11 UNI / HyperLan 2 */
  1502. { 36, 0x00002ccc, 0x0000499a, 0x0009be55, 0x000ffa23 },
  1503. { 40, 0x00002ccc, 0x000049a2, 0x0009be55, 0x000ffa03 },
  1504. { 44, 0x00002ccc, 0x000049a6, 0x0009be55, 0x000ffa0b },
  1505. { 48, 0x00002ccc, 0x000049aa, 0x0009be55, 0x000ffa13 },
  1506. { 52, 0x00002ccc, 0x000049ae, 0x0009ae55, 0x000ffa1b },
  1507. { 56, 0x00002ccc, 0x000049b2, 0x0009ae55, 0x000ffa23 },
  1508. { 60, 0x00002ccc, 0x000049ba, 0x0009ae55, 0x000ffa03 },
  1509. { 64, 0x00002ccc, 0x000049be, 0x0009ae55, 0x000ffa0b },
  1510. /* 802.11 HyperLan 2 */
  1511. { 100, 0x00002ccc, 0x00004a2a, 0x000bae55, 0x000ffa03 },
  1512. { 104, 0x00002ccc, 0x00004a2e, 0x000bae55, 0x000ffa0b },
  1513. { 108, 0x00002ccc, 0x00004a32, 0x000bae55, 0x000ffa13 },
  1514. { 112, 0x00002ccc, 0x00004a36, 0x000bae55, 0x000ffa1b },
  1515. { 116, 0x00002ccc, 0x00004a3a, 0x000bbe55, 0x000ffa23 },
  1516. { 120, 0x00002ccc, 0x00004a82, 0x000bbe55, 0x000ffa03 },
  1517. { 124, 0x00002ccc, 0x00004a86, 0x000bbe55, 0x000ffa0b },
  1518. { 128, 0x00002ccc, 0x00004a8a, 0x000bbe55, 0x000ffa13 },
  1519. { 132, 0x00002ccc, 0x00004a8e, 0x000bbe55, 0x000ffa1b },
  1520. { 136, 0x00002ccc, 0x00004a92, 0x000bbe55, 0x000ffa23 },
  1521. /* 802.11 UNII */
  1522. { 140, 0x00002ccc, 0x00004a9a, 0x000bbe55, 0x000ffa03 },
  1523. { 149, 0x00002ccc, 0x00004aa2, 0x000bbe55, 0x000ffa1f },
  1524. { 153, 0x00002ccc, 0x00004aa6, 0x000bbe55, 0x000ffa27 },
  1525. { 157, 0x00002ccc, 0x00004aae, 0x000bbe55, 0x000ffa07 },
  1526. { 161, 0x00002ccc, 0x00004ab2, 0x000bbe55, 0x000ffa0f },
  1527. { 165, 0x00002ccc, 0x00004ab6, 0x000bbe55, 0x000ffa17 },
  1528. /* MMAC(Japan)J52 ch 34,38,42,46 */
  1529. { 34, 0x00002ccc, 0x0000499a, 0x0009be55, 0x000ffa0b },
  1530. { 38, 0x00002ccc, 0x0000499e, 0x0009be55, 0x000ffa13 },
  1531. { 42, 0x00002ccc, 0x000049a2, 0x0009be55, 0x000ffa1b },
  1532. { 46, 0x00002ccc, 0x000049a6, 0x0009be55, 0x000ffa23 },
  1533. };
  1534. static void rt73usb_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
  1535. {
  1536. struct hw_mode_spec *spec = &rt2x00dev->spec;
  1537. u8 *txpower;
  1538. unsigned int i;
  1539. /*
  1540. * Initialize all hw fields.
  1541. */
  1542. rt2x00dev->hw->flags =
  1543. IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE |
  1544. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
  1545. rt2x00dev->hw->extra_tx_headroom = TXD_DESC_SIZE;
  1546. rt2x00dev->hw->max_signal = MAX_SIGNAL;
  1547. rt2x00dev->hw->max_rssi = MAX_RX_SSI;
  1548. rt2x00dev->hw->queues = 4;
  1549. SET_IEEE80211_DEV(rt2x00dev->hw, &rt2x00dev_usb(rt2x00dev)->dev);
  1550. SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
  1551. rt2x00_eeprom_addr(rt2x00dev,
  1552. EEPROM_MAC_ADDR_0));
  1553. /*
  1554. * Convert tx_power array in eeprom.
  1555. */
  1556. txpower = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_G_START);
  1557. for (i = 0; i < 14; i++)
  1558. txpower[i] = TXPOWER_FROM_DEV(txpower[i]);
  1559. /*
  1560. * Initialize hw_mode information.
  1561. */
  1562. spec->supported_bands = SUPPORT_BAND_2GHZ;
  1563. spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
  1564. spec->tx_power_a = NULL;
  1565. spec->tx_power_bg = txpower;
  1566. spec->tx_power_default = DEFAULT_TXPOWER;
  1567. if (rt2x00_rf(&rt2x00dev->chip, RF2528)) {
  1568. spec->num_channels = ARRAY_SIZE(rf_vals_bg_2528);
  1569. spec->channels = rf_vals_bg_2528;
  1570. } else if (rt2x00_rf(&rt2x00dev->chip, RF5226)) {
  1571. spec->supported_bands |= SUPPORT_BAND_5GHZ;
  1572. spec->num_channels = ARRAY_SIZE(rf_vals_5226);
  1573. spec->channels = rf_vals_5226;
  1574. } else if (rt2x00_rf(&rt2x00dev->chip, RF2527)) {
  1575. spec->num_channels = 14;
  1576. spec->channels = rf_vals_5225_2527;
  1577. } else if (rt2x00_rf(&rt2x00dev->chip, RF5225)) {
  1578. spec->supported_bands |= SUPPORT_BAND_5GHZ;
  1579. spec->num_channels = ARRAY_SIZE(rf_vals_5225_2527);
  1580. spec->channels = rf_vals_5225_2527;
  1581. }
  1582. if (rt2x00_rf(&rt2x00dev->chip, RF5225) ||
  1583. rt2x00_rf(&rt2x00dev->chip, RF5226)) {
  1584. txpower = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A_START);
  1585. for (i = 0; i < 14; i++)
  1586. txpower[i] = TXPOWER_FROM_DEV(txpower[i]);
  1587. spec->tx_power_a = txpower;
  1588. }
  1589. }
  1590. static int rt73usb_probe_hw(struct rt2x00_dev *rt2x00dev)
  1591. {
  1592. int retval;
  1593. /*
  1594. * Allocate eeprom data.
  1595. */
  1596. retval = rt73usb_validate_eeprom(rt2x00dev);
  1597. if (retval)
  1598. return retval;
  1599. retval = rt73usb_init_eeprom(rt2x00dev);
  1600. if (retval)
  1601. return retval;
  1602. /*
  1603. * Initialize hw specifications.
  1604. */
  1605. rt73usb_probe_hw_mode(rt2x00dev);
  1606. /*
  1607. * This device requires firmware.
  1608. */
  1609. __set_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags);
  1610. __set_bit(DRIVER_REQUIRE_FIRMWARE_CRC_ITU_T, &rt2x00dev->flags);
  1611. /*
  1612. * Set the rssi offset.
  1613. */
  1614. rt2x00dev->rssi_offset = DEFAULT_RSSI_OFFSET;
  1615. return 0;
  1616. }
  1617. /*
  1618. * IEEE80211 stack callback functions.
  1619. */
  1620. static void rt73usb_configure_filter(struct ieee80211_hw *hw,
  1621. unsigned int changed_flags,
  1622. unsigned int *total_flags,
  1623. int mc_count,
  1624. struct dev_addr_list *mc_list)
  1625. {
  1626. struct rt2x00_dev *rt2x00dev = hw->priv;
  1627. u32 reg;
  1628. /*
  1629. * Mask off any flags we are going to ignore from
  1630. * the total_flags field.
  1631. */
  1632. *total_flags &=
  1633. FIF_ALLMULTI |
  1634. FIF_FCSFAIL |
  1635. FIF_PLCPFAIL |
  1636. FIF_CONTROL |
  1637. FIF_OTHER_BSS |
  1638. FIF_PROMISC_IN_BSS;
  1639. /*
  1640. * Apply some rules to the filters:
  1641. * - Some filters imply different filters to be set.
  1642. * - Some things we can't filter out at all.
  1643. */
  1644. if (mc_count)
  1645. *total_flags |= FIF_ALLMULTI;
  1646. if (*total_flags & FIF_OTHER_BSS ||
  1647. *total_flags & FIF_PROMISC_IN_BSS)
  1648. *total_flags |= FIF_PROMISC_IN_BSS | FIF_OTHER_BSS;
  1649. /*
  1650. * Check if there is any work left for us.
  1651. */
  1652. if (rt2x00dev->packet_filter == *total_flags)
  1653. return;
  1654. rt2x00dev->packet_filter = *total_flags;
  1655. /*
  1656. * When in atomic context, reschedule and let rt2x00lib
  1657. * call this function again.
  1658. */
  1659. if (in_atomic()) {
  1660. queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->filter_work);
  1661. return;
  1662. }
  1663. /*
  1664. * Start configuration steps.
  1665. * Note that the version error will always be dropped
  1666. * and broadcast frames will always be accepted since
  1667. * there is no filter for it at this time.
  1668. */
  1669. rt73usb_register_read(rt2x00dev, TXRX_CSR0, &reg);
  1670. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_CRC,
  1671. !(*total_flags & FIF_FCSFAIL));
  1672. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_PHYSICAL,
  1673. !(*total_flags & FIF_PLCPFAIL));
  1674. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_CONTROL,
  1675. !(*total_flags & FIF_CONTROL));
  1676. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_NOT_TO_ME,
  1677. !(*total_flags & FIF_PROMISC_IN_BSS));
  1678. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_TO_DS,
  1679. !(*total_flags & FIF_PROMISC_IN_BSS));
  1680. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_VERSION_ERROR, 1);
  1681. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_MULTICAST,
  1682. !(*total_flags & FIF_ALLMULTI));
  1683. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_BROADCAST, 0);
  1684. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_ACK_CTS,
  1685. !(*total_flags & FIF_CONTROL));
  1686. rt73usb_register_write(rt2x00dev, TXRX_CSR0, reg);
  1687. }
  1688. static int rt73usb_set_retry_limit(struct ieee80211_hw *hw,
  1689. u32 short_retry, u32 long_retry)
  1690. {
  1691. struct rt2x00_dev *rt2x00dev = hw->priv;
  1692. u32 reg;
  1693. rt73usb_register_read(rt2x00dev, TXRX_CSR4, &reg);
  1694. rt2x00_set_field32(&reg, TXRX_CSR4_LONG_RETRY_LIMIT, long_retry);
  1695. rt2x00_set_field32(&reg, TXRX_CSR4_SHORT_RETRY_LIMIT, short_retry);
  1696. rt73usb_register_write(rt2x00dev, TXRX_CSR4, reg);
  1697. return 0;
  1698. }
  1699. #if 0
  1700. /*
  1701. * Mac80211 demands get_tsf must be atomic.
  1702. * This is not possible for rt73usb since all register access
  1703. * functions require sleeping. Untill mac80211 no longer needs
  1704. * get_tsf to be atomic, this function should be disabled.
  1705. */
  1706. static u64 rt73usb_get_tsf(struct ieee80211_hw *hw)
  1707. {
  1708. struct rt2x00_dev *rt2x00dev = hw->priv;
  1709. u64 tsf;
  1710. u32 reg;
  1711. rt73usb_register_read(rt2x00dev, TXRX_CSR13, &reg);
  1712. tsf = (u64) rt2x00_get_field32(reg, TXRX_CSR13_HIGH_TSFTIMER) << 32;
  1713. rt73usb_register_read(rt2x00dev, TXRX_CSR12, &reg);
  1714. tsf |= rt2x00_get_field32(reg, TXRX_CSR12_LOW_TSFTIMER);
  1715. return tsf;
  1716. }
  1717. #else
  1718. #define rt73usb_get_tsf NULL
  1719. #endif
  1720. static int rt73usb_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
  1721. struct ieee80211_tx_control *control)
  1722. {
  1723. struct rt2x00_dev *rt2x00dev = hw->priv;
  1724. struct rt2x00_intf *intf = vif_to_intf(control->vif);
  1725. struct skb_frame_desc *skbdesc;
  1726. unsigned int beacon_base;
  1727. unsigned int timeout;
  1728. if (unlikely(!intf->beacon))
  1729. return -ENOBUFS;
  1730. /*
  1731. * Add the descriptor in front of the skb.
  1732. */
  1733. skb_push(skb, intf->beacon->queue->desc_size);
  1734. memset(skb->data, 0, intf->beacon->queue->desc_size);
  1735. /*
  1736. * Fill in skb descriptor
  1737. */
  1738. skbdesc = get_skb_frame_desc(skb);
  1739. memset(skbdesc, 0, sizeof(*skbdesc));
  1740. skbdesc->flags |= FRAME_DESC_DRIVER_GENERATED;
  1741. skbdesc->data = skb->data + intf->beacon->queue->desc_size;
  1742. skbdesc->data_len = skb->len - intf->beacon->queue->desc_size;
  1743. skbdesc->desc = skb->data;
  1744. skbdesc->desc_len = intf->beacon->queue->desc_size;
  1745. skbdesc->entry = intf->beacon;
  1746. /*
  1747. * mac80211 doesn't provide the control->queue variable
  1748. * for beacons. Set our own queue identification so
  1749. * it can be used during descriptor initialization.
  1750. */
  1751. control->queue = RT2X00_BCN_QUEUE_BEACON;
  1752. rt2x00lib_write_tx_desc(rt2x00dev, skb, control);
  1753. /*
  1754. * Write entire beacon with descriptor to register,
  1755. * and kick the beacon generator.
  1756. */
  1757. beacon_base = HW_BEACON_OFFSET(intf->beacon->entry_idx);
  1758. timeout = REGISTER_TIMEOUT * (skb->len / sizeof(u32));
  1759. rt2x00usb_vendor_request(rt2x00dev, USB_MULTI_WRITE,
  1760. USB_VENDOR_REQUEST_OUT, beacon_base, 0,
  1761. skb->data, skb->len, timeout);
  1762. rt73usb_kick_tx_queue(rt2x00dev, control->queue);
  1763. return 0;
  1764. }
  1765. static const struct ieee80211_ops rt73usb_mac80211_ops = {
  1766. .tx = rt2x00mac_tx,
  1767. .start = rt2x00mac_start,
  1768. .stop = rt2x00mac_stop,
  1769. .add_interface = rt2x00mac_add_interface,
  1770. .remove_interface = rt2x00mac_remove_interface,
  1771. .config = rt2x00mac_config,
  1772. .config_interface = rt2x00mac_config_interface,
  1773. .configure_filter = rt73usb_configure_filter,
  1774. .get_stats = rt2x00mac_get_stats,
  1775. .set_retry_limit = rt73usb_set_retry_limit,
  1776. .bss_info_changed = rt2x00mac_bss_info_changed,
  1777. .conf_tx = rt2x00mac_conf_tx,
  1778. .get_tx_stats = rt2x00mac_get_tx_stats,
  1779. .get_tsf = rt73usb_get_tsf,
  1780. .beacon_update = rt73usb_beacon_update,
  1781. };
  1782. static const struct rt2x00lib_ops rt73usb_rt2x00_ops = {
  1783. .probe_hw = rt73usb_probe_hw,
  1784. .get_firmware_name = rt73usb_get_firmware_name,
  1785. .load_firmware = rt73usb_load_firmware,
  1786. .initialize = rt2x00usb_initialize,
  1787. .uninitialize = rt2x00usb_uninitialize,
  1788. .init_rxentry = rt2x00usb_init_rxentry,
  1789. .init_txentry = rt2x00usb_init_txentry,
  1790. .set_device_state = rt73usb_set_device_state,
  1791. .link_stats = rt73usb_link_stats,
  1792. .reset_tuner = rt73usb_reset_tuner,
  1793. .link_tuner = rt73usb_link_tuner,
  1794. .led_brightness = rt73usb_led_brightness,
  1795. .write_tx_desc = rt73usb_write_tx_desc,
  1796. .write_tx_data = rt2x00usb_write_tx_data,
  1797. .get_tx_data_len = rt73usb_get_tx_data_len,
  1798. .kick_tx_queue = rt73usb_kick_tx_queue,
  1799. .fill_rxdone = rt73usb_fill_rxdone,
  1800. .config_intf = rt73usb_config_intf,
  1801. .config_preamble = rt73usb_config_preamble,
  1802. .config = rt73usb_config,
  1803. };
  1804. static const struct data_queue_desc rt73usb_queue_rx = {
  1805. .entry_num = RX_ENTRIES,
  1806. .data_size = DATA_FRAME_SIZE,
  1807. .desc_size = RXD_DESC_SIZE,
  1808. .priv_size = sizeof(struct queue_entry_priv_usb_rx),
  1809. };
  1810. static const struct data_queue_desc rt73usb_queue_tx = {
  1811. .entry_num = TX_ENTRIES,
  1812. .data_size = DATA_FRAME_SIZE,
  1813. .desc_size = TXD_DESC_SIZE,
  1814. .priv_size = sizeof(struct queue_entry_priv_usb_tx),
  1815. };
  1816. static const struct data_queue_desc rt73usb_queue_bcn = {
  1817. .entry_num = 4 * BEACON_ENTRIES,
  1818. .data_size = MGMT_FRAME_SIZE,
  1819. .desc_size = TXINFO_SIZE,
  1820. .priv_size = sizeof(struct queue_entry_priv_usb_tx),
  1821. };
  1822. static const struct rt2x00_ops rt73usb_ops = {
  1823. .name = KBUILD_MODNAME,
  1824. .max_sta_intf = 1,
  1825. .max_ap_intf = 4,
  1826. .eeprom_size = EEPROM_SIZE,
  1827. .rf_size = RF_SIZE,
  1828. .rx = &rt73usb_queue_rx,
  1829. .tx = &rt73usb_queue_tx,
  1830. .bcn = &rt73usb_queue_bcn,
  1831. .lib = &rt73usb_rt2x00_ops,
  1832. .hw = &rt73usb_mac80211_ops,
  1833. #ifdef CONFIG_RT2X00_LIB_DEBUGFS
  1834. .debugfs = &rt73usb_rt2x00debug,
  1835. #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
  1836. };
  1837. /*
  1838. * rt73usb module information.
  1839. */
  1840. static struct usb_device_id rt73usb_device_table[] = {
  1841. /* AboCom */
  1842. { USB_DEVICE(0x07b8, 0xb21d), USB_DEVICE_DATA(&rt73usb_ops) },
  1843. /* Askey */
  1844. { USB_DEVICE(0x1690, 0x0722), USB_DEVICE_DATA(&rt73usb_ops) },
  1845. /* ASUS */
  1846. { USB_DEVICE(0x0b05, 0x1723), USB_DEVICE_DATA(&rt73usb_ops) },
  1847. { USB_DEVICE(0x0b05, 0x1724), USB_DEVICE_DATA(&rt73usb_ops) },
  1848. /* Belkin */
  1849. { USB_DEVICE(0x050d, 0x7050), USB_DEVICE_DATA(&rt73usb_ops) },
  1850. { USB_DEVICE(0x050d, 0x705a), USB_DEVICE_DATA(&rt73usb_ops) },
  1851. { USB_DEVICE(0x050d, 0x905b), USB_DEVICE_DATA(&rt73usb_ops) },
  1852. { USB_DEVICE(0x050d, 0x905c), USB_DEVICE_DATA(&rt73usb_ops) },
  1853. /* Billionton */
  1854. { USB_DEVICE(0x1631, 0xc019), USB_DEVICE_DATA(&rt73usb_ops) },
  1855. /* Buffalo */
  1856. { USB_DEVICE(0x0411, 0x00f4), USB_DEVICE_DATA(&rt73usb_ops) },
  1857. /* CNet */
  1858. { USB_DEVICE(0x1371, 0x9022), USB_DEVICE_DATA(&rt73usb_ops) },
  1859. { USB_DEVICE(0x1371, 0x9032), USB_DEVICE_DATA(&rt73usb_ops) },
  1860. /* Conceptronic */
  1861. { USB_DEVICE(0x14b2, 0x3c22), USB_DEVICE_DATA(&rt73usb_ops) },
  1862. /* D-Link */
  1863. { USB_DEVICE(0x07d1, 0x3c03), USB_DEVICE_DATA(&rt73usb_ops) },
  1864. { USB_DEVICE(0x07d1, 0x3c04), USB_DEVICE_DATA(&rt73usb_ops) },
  1865. /* Gemtek */
  1866. { USB_DEVICE(0x15a9, 0x0004), USB_DEVICE_DATA(&rt73usb_ops) },
  1867. /* Gigabyte */
  1868. { USB_DEVICE(0x1044, 0x8008), USB_DEVICE_DATA(&rt73usb_ops) },
  1869. { USB_DEVICE(0x1044, 0x800a), USB_DEVICE_DATA(&rt73usb_ops) },
  1870. /* Huawei-3Com */
  1871. { USB_DEVICE(0x1472, 0x0009), USB_DEVICE_DATA(&rt73usb_ops) },
  1872. /* Hercules */
  1873. { USB_DEVICE(0x06f8, 0xe010), USB_DEVICE_DATA(&rt73usb_ops) },
  1874. { USB_DEVICE(0x06f8, 0xe020), USB_DEVICE_DATA(&rt73usb_ops) },
  1875. /* Linksys */
  1876. { USB_DEVICE(0x13b1, 0x0020), USB_DEVICE_DATA(&rt73usb_ops) },
  1877. { USB_DEVICE(0x13b1, 0x0023), USB_DEVICE_DATA(&rt73usb_ops) },
  1878. /* MSI */
  1879. { USB_DEVICE(0x0db0, 0x6877), USB_DEVICE_DATA(&rt73usb_ops) },
  1880. { USB_DEVICE(0x0db0, 0x6874), USB_DEVICE_DATA(&rt73usb_ops) },
  1881. { USB_DEVICE(0x0db0, 0xa861), USB_DEVICE_DATA(&rt73usb_ops) },
  1882. { USB_DEVICE(0x0db0, 0xa874), USB_DEVICE_DATA(&rt73usb_ops) },
  1883. /* Ralink */
  1884. { USB_DEVICE(0x148f, 0x2573), USB_DEVICE_DATA(&rt73usb_ops) },
  1885. { USB_DEVICE(0x148f, 0x2671), USB_DEVICE_DATA(&rt73usb_ops) },
  1886. /* Qcom */
  1887. { USB_DEVICE(0x18e8, 0x6196), USB_DEVICE_DATA(&rt73usb_ops) },
  1888. { USB_DEVICE(0x18e8, 0x6229), USB_DEVICE_DATA(&rt73usb_ops) },
  1889. { USB_DEVICE(0x18e8, 0x6238), USB_DEVICE_DATA(&rt73usb_ops) },
  1890. /* Senao */
  1891. { USB_DEVICE(0x1740, 0x7100), USB_DEVICE_DATA(&rt73usb_ops) },
  1892. /* Sitecom */
  1893. { USB_DEVICE(0x0df6, 0x9712), USB_DEVICE_DATA(&rt73usb_ops) },
  1894. { USB_DEVICE(0x0df6, 0x90ac), USB_DEVICE_DATA(&rt73usb_ops) },
  1895. /* Surecom */
  1896. { USB_DEVICE(0x0769, 0x31f3), USB_DEVICE_DATA(&rt73usb_ops) },
  1897. /* Planex */
  1898. { USB_DEVICE(0x2019, 0xab01), USB_DEVICE_DATA(&rt73usb_ops) },
  1899. { USB_DEVICE(0x2019, 0xab50), USB_DEVICE_DATA(&rt73usb_ops) },
  1900. { 0, }
  1901. };
  1902. MODULE_AUTHOR(DRV_PROJECT);
  1903. MODULE_VERSION(DRV_VERSION);
  1904. MODULE_DESCRIPTION("Ralink RT73 USB Wireless LAN driver.");
  1905. MODULE_SUPPORTED_DEVICE("Ralink RT2571W & RT2671 USB chipset based cards");
  1906. MODULE_DEVICE_TABLE(usb, rt73usb_device_table);
  1907. MODULE_FIRMWARE(FIRMWARE_RT2571);
  1908. MODULE_LICENSE("GPL");
  1909. static struct usb_driver rt73usb_driver = {
  1910. .name = KBUILD_MODNAME,
  1911. .id_table = rt73usb_device_table,
  1912. .probe = rt2x00usb_probe,
  1913. .disconnect = rt2x00usb_disconnect,
  1914. .suspend = rt2x00usb_suspend,
  1915. .resume = rt2x00usb_resume,
  1916. };
  1917. static int __init rt73usb_init(void)
  1918. {
  1919. return usb_register(&rt73usb_driver);
  1920. }
  1921. static void __exit rt73usb_exit(void)
  1922. {
  1923. usb_deregister(&rt73usb_driver);
  1924. }
  1925. module_init(rt73usb_init);
  1926. module_exit(rt73usb_exit);