rt73usb.c 66 KB

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