rt73usb.c 65 KB

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