rt73usb.c 66 KB

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