rt73usb.c 66 KB

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