rt73usb.c 67 KB

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