rt73usb.c 66 KB

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