rt73usb.c 63 KB

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