rt73usb.c 62 KB

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