main.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  1. /*
  2. * This file is part of wl1251
  3. *
  4. * Copyright (C) 2008-2009 Nokia Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  18. * 02110-1301 USA
  19. *
  20. */
  21. #include <linux/module.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/firmware.h>
  24. #include <linux/delay.h>
  25. #include <linux/irq.h>
  26. #include <linux/crc32.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/vmalloc.h>
  29. #include <linux/slab.h>
  30. #include <linux/netdevice.h>
  31. #include "wl1251.h"
  32. #include "wl12xx_80211.h"
  33. #include "reg.h"
  34. #include "io.h"
  35. #include "cmd.h"
  36. #include "event.h"
  37. #include "tx.h"
  38. #include "rx.h"
  39. #include "ps.h"
  40. #include "init.h"
  41. #include "debugfs.h"
  42. #include "boot.h"
  43. void wl1251_enable_interrupts(struct wl1251 *wl)
  44. {
  45. wl->if_ops->enable_irq(wl);
  46. }
  47. void wl1251_disable_interrupts(struct wl1251 *wl)
  48. {
  49. wl->if_ops->disable_irq(wl);
  50. }
  51. static int wl1251_power_off(struct wl1251 *wl)
  52. {
  53. return wl->if_ops->power(wl, false);
  54. }
  55. static int wl1251_power_on(struct wl1251 *wl)
  56. {
  57. return wl->if_ops->power(wl, true);
  58. }
  59. static int wl1251_fetch_firmware(struct wl1251 *wl)
  60. {
  61. const struct firmware *fw;
  62. struct device *dev = wiphy_dev(wl->hw->wiphy);
  63. int ret;
  64. ret = request_firmware(&fw, WL1251_FW_NAME, dev);
  65. if (ret < 0) {
  66. wl1251_error("could not get firmware: %d", ret);
  67. return ret;
  68. }
  69. if (fw->size % 4) {
  70. wl1251_error("firmware size is not multiple of 32 bits: %zu",
  71. fw->size);
  72. ret = -EILSEQ;
  73. goto out;
  74. }
  75. wl->fw_len = fw->size;
  76. wl->fw = vmalloc(wl->fw_len);
  77. if (!wl->fw) {
  78. wl1251_error("could not allocate memory for the firmware");
  79. ret = -ENOMEM;
  80. goto out;
  81. }
  82. memcpy(wl->fw, fw->data, wl->fw_len);
  83. ret = 0;
  84. out:
  85. release_firmware(fw);
  86. return ret;
  87. }
  88. static int wl1251_fetch_nvs(struct wl1251 *wl)
  89. {
  90. const struct firmware *fw;
  91. struct device *dev = wiphy_dev(wl->hw->wiphy);
  92. int ret;
  93. ret = request_firmware(&fw, WL1251_NVS_NAME, dev);
  94. if (ret < 0) {
  95. wl1251_error("could not get nvs file: %d", ret);
  96. return ret;
  97. }
  98. if (fw->size % 4) {
  99. wl1251_error("nvs size is not multiple of 32 bits: %zu",
  100. fw->size);
  101. ret = -EILSEQ;
  102. goto out;
  103. }
  104. wl->nvs = kmemdup(fw->data, fw->size, GFP_KERNEL);
  105. if (!wl->nvs) {
  106. wl1251_error("could not allocate memory for the nvs file");
  107. ret = -ENOMEM;
  108. goto out;
  109. }
  110. wl->nvs_len = fw->size;
  111. ret = 0;
  112. out:
  113. release_firmware(fw);
  114. return ret;
  115. }
  116. static void wl1251_fw_wakeup(struct wl1251 *wl)
  117. {
  118. u32 elp_reg;
  119. elp_reg = ELPCTRL_WAKE_UP;
  120. wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
  121. elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
  122. if (!(elp_reg & ELPCTRL_WLAN_READY))
  123. wl1251_warning("WLAN not ready");
  124. }
  125. static int wl1251_chip_wakeup(struct wl1251 *wl)
  126. {
  127. int ret;
  128. ret = wl1251_power_on(wl);
  129. if (ret < 0)
  130. return ret;
  131. msleep(WL1251_POWER_ON_SLEEP);
  132. wl->if_ops->reset(wl);
  133. /* We don't need a real memory partition here, because we only want
  134. * to use the registers at this point. */
  135. wl1251_set_partition(wl,
  136. 0x00000000,
  137. 0x00000000,
  138. REGISTERS_BASE,
  139. REGISTERS_DOWN_SIZE);
  140. /* ELP module wake up */
  141. wl1251_fw_wakeup(wl);
  142. /* whal_FwCtrl_BootSm() */
  143. /* 0. read chip id from CHIP_ID */
  144. wl->chip_id = wl1251_reg_read32(wl, CHIP_ID_B);
  145. /* 1. check if chip id is valid */
  146. switch (wl->chip_id) {
  147. case CHIP_ID_1251_PG12:
  148. wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG12)",
  149. wl->chip_id);
  150. break;
  151. case CHIP_ID_1251_PG11:
  152. wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG11)",
  153. wl->chip_id);
  154. break;
  155. case CHIP_ID_1251_PG10:
  156. default:
  157. wl1251_error("unsupported chip id: 0x%x", wl->chip_id);
  158. ret = -ENODEV;
  159. goto out;
  160. }
  161. if (wl->fw == NULL) {
  162. ret = wl1251_fetch_firmware(wl);
  163. if (ret < 0)
  164. goto out;
  165. }
  166. out:
  167. return ret;
  168. }
  169. #define WL1251_IRQ_LOOP_COUNT 10
  170. static void wl1251_irq_work(struct work_struct *work)
  171. {
  172. u32 intr, ctr = WL1251_IRQ_LOOP_COUNT;
  173. struct wl1251 *wl =
  174. container_of(work, struct wl1251, irq_work);
  175. int ret;
  176. mutex_lock(&wl->mutex);
  177. wl1251_debug(DEBUG_IRQ, "IRQ work");
  178. if (wl->state == WL1251_STATE_OFF)
  179. goto out;
  180. ret = wl1251_ps_elp_wakeup(wl);
  181. if (ret < 0)
  182. goto out;
  183. wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, WL1251_ACX_INTR_ALL);
  184. intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
  185. wl1251_debug(DEBUG_IRQ, "intr: 0x%x", intr);
  186. do {
  187. if (wl->data_path) {
  188. wl->rx_counter = wl1251_mem_read32(
  189. wl, wl->data_path->rx_control_addr);
  190. /* We handle a frmware bug here */
  191. switch ((wl->rx_counter - wl->rx_handled) & 0xf) {
  192. case 0:
  193. wl1251_debug(DEBUG_IRQ,
  194. "RX: FW and host in sync");
  195. intr &= ~WL1251_ACX_INTR_RX0_DATA;
  196. intr &= ~WL1251_ACX_INTR_RX1_DATA;
  197. break;
  198. case 1:
  199. wl1251_debug(DEBUG_IRQ, "RX: FW +1");
  200. intr |= WL1251_ACX_INTR_RX0_DATA;
  201. intr &= ~WL1251_ACX_INTR_RX1_DATA;
  202. break;
  203. case 2:
  204. wl1251_debug(DEBUG_IRQ, "RX: FW +2");
  205. intr |= WL1251_ACX_INTR_RX0_DATA;
  206. intr |= WL1251_ACX_INTR_RX1_DATA;
  207. break;
  208. default:
  209. wl1251_warning(
  210. "RX: FW and host out of sync: %d",
  211. wl->rx_counter - wl->rx_handled);
  212. break;
  213. }
  214. wl->rx_handled = wl->rx_counter;
  215. wl1251_debug(DEBUG_IRQ, "RX counter: %d",
  216. wl->rx_counter);
  217. }
  218. intr &= wl->intr_mask;
  219. if (intr == 0) {
  220. wl1251_debug(DEBUG_IRQ, "INTR is 0");
  221. goto out_sleep;
  222. }
  223. if (intr & WL1251_ACX_INTR_RX0_DATA) {
  224. wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX0_DATA");
  225. wl1251_rx(wl);
  226. }
  227. if (intr & WL1251_ACX_INTR_RX1_DATA) {
  228. wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX1_DATA");
  229. wl1251_rx(wl);
  230. }
  231. if (intr & WL1251_ACX_INTR_TX_RESULT) {
  232. wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_TX_RESULT");
  233. wl1251_tx_complete(wl);
  234. }
  235. if (intr & WL1251_ACX_INTR_EVENT_A) {
  236. wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT_A");
  237. wl1251_event_handle(wl, 0);
  238. }
  239. if (intr & WL1251_ACX_INTR_EVENT_B) {
  240. wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT_B");
  241. wl1251_event_handle(wl, 1);
  242. }
  243. if (intr & WL1251_ACX_INTR_INIT_COMPLETE)
  244. wl1251_debug(DEBUG_IRQ,
  245. "WL1251_ACX_INTR_INIT_COMPLETE");
  246. if (--ctr == 0)
  247. break;
  248. intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
  249. } while (intr);
  250. out_sleep:
  251. wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, ~(wl->intr_mask));
  252. wl1251_ps_elp_sleep(wl);
  253. out:
  254. mutex_unlock(&wl->mutex);
  255. }
  256. static int wl1251_join(struct wl1251 *wl, u8 bss_type, u8 channel,
  257. u16 beacon_interval, u8 dtim_period)
  258. {
  259. int ret;
  260. ret = wl1251_acx_frame_rates(wl, DEFAULT_HW_GEN_TX_RATE,
  261. DEFAULT_HW_GEN_MODULATION_TYPE,
  262. wl->tx_mgmt_frm_rate,
  263. wl->tx_mgmt_frm_mod);
  264. if (ret < 0)
  265. goto out;
  266. /*
  267. * Join command applies filters, and if we are not associated,
  268. * BSSID filter must be disabled for association to work.
  269. */
  270. if (is_zero_ether_addr(wl->bssid))
  271. wl->rx_config &= ~CFG_BSSID_FILTER_EN;
  272. ret = wl1251_cmd_join(wl, bss_type, channel, beacon_interval,
  273. dtim_period);
  274. if (ret < 0)
  275. goto out;
  276. ret = wl1251_event_wait(wl, JOIN_EVENT_COMPLETE_ID, 100);
  277. if (ret < 0)
  278. wl1251_warning("join timeout");
  279. out:
  280. return ret;
  281. }
  282. static void wl1251_op_tx(struct ieee80211_hw *hw,
  283. struct ieee80211_tx_control *control,
  284. struct sk_buff *skb)
  285. {
  286. struct wl1251 *wl = hw->priv;
  287. unsigned long flags;
  288. skb_queue_tail(&wl->tx_queue, skb);
  289. /*
  290. * The chip specific setup must run before the first TX packet -
  291. * before that, the tx_work will not be initialized!
  292. */
  293. ieee80211_queue_work(wl->hw, &wl->tx_work);
  294. /*
  295. * The workqueue is slow to process the tx_queue and we need stop
  296. * the queue here, otherwise the queue will get too long.
  297. */
  298. if (skb_queue_len(&wl->tx_queue) >= WL1251_TX_QUEUE_HIGH_WATERMARK) {
  299. wl1251_debug(DEBUG_TX, "op_tx: tx_queue full, stop queues");
  300. spin_lock_irqsave(&wl->wl_lock, flags);
  301. ieee80211_stop_queues(wl->hw);
  302. wl->tx_queue_stopped = true;
  303. spin_unlock_irqrestore(&wl->wl_lock, flags);
  304. }
  305. }
  306. static int wl1251_op_start(struct ieee80211_hw *hw)
  307. {
  308. struct wl1251 *wl = hw->priv;
  309. struct wiphy *wiphy = hw->wiphy;
  310. int ret = 0;
  311. wl1251_debug(DEBUG_MAC80211, "mac80211 start");
  312. mutex_lock(&wl->mutex);
  313. if (wl->state != WL1251_STATE_OFF) {
  314. wl1251_error("cannot start because not in off state: %d",
  315. wl->state);
  316. ret = -EBUSY;
  317. goto out;
  318. }
  319. ret = wl1251_chip_wakeup(wl);
  320. if (ret < 0)
  321. goto out;
  322. ret = wl1251_boot(wl);
  323. if (ret < 0)
  324. goto out;
  325. ret = wl1251_hw_init(wl);
  326. if (ret < 0)
  327. goto out;
  328. ret = wl1251_acx_station_id(wl);
  329. if (ret < 0)
  330. goto out;
  331. wl->state = WL1251_STATE_ON;
  332. wl1251_info("firmware booted (%s)", wl->fw_ver);
  333. /* update hw/fw version info in wiphy struct */
  334. wiphy->hw_version = wl->chip_id;
  335. strncpy(wiphy->fw_version, wl->fw_ver, sizeof(wiphy->fw_version));
  336. out:
  337. if (ret < 0)
  338. wl1251_power_off(wl);
  339. mutex_unlock(&wl->mutex);
  340. return ret;
  341. }
  342. static void wl1251_op_stop(struct ieee80211_hw *hw)
  343. {
  344. struct wl1251 *wl = hw->priv;
  345. wl1251_info("down");
  346. wl1251_debug(DEBUG_MAC80211, "mac80211 stop");
  347. mutex_lock(&wl->mutex);
  348. WARN_ON(wl->state != WL1251_STATE_ON);
  349. if (wl->scanning) {
  350. struct cfg80211_scan_info info = {
  351. .aborted = true,
  352. };
  353. ieee80211_scan_completed(wl->hw, &info);
  354. wl->scanning = false;
  355. }
  356. wl->state = WL1251_STATE_OFF;
  357. wl1251_disable_interrupts(wl);
  358. mutex_unlock(&wl->mutex);
  359. cancel_work_sync(&wl->irq_work);
  360. cancel_work_sync(&wl->tx_work);
  361. cancel_delayed_work_sync(&wl->elp_work);
  362. mutex_lock(&wl->mutex);
  363. /* let's notify MAC80211 about the remaining pending TX frames */
  364. wl1251_tx_flush(wl);
  365. wl1251_power_off(wl);
  366. eth_zero_addr(wl->bssid);
  367. wl->listen_int = 1;
  368. wl->bss_type = MAX_BSS_TYPE;
  369. wl->data_in_count = 0;
  370. wl->rx_counter = 0;
  371. wl->rx_handled = 0;
  372. wl->rx_current_buffer = 0;
  373. wl->rx_last_id = 0;
  374. wl->next_tx_complete = 0;
  375. wl->elp = false;
  376. wl->station_mode = STATION_ACTIVE_MODE;
  377. wl->psm_entry_retry = 0;
  378. wl->tx_queue_stopped = false;
  379. wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
  380. wl->rssi_thold = 0;
  381. wl->channel = WL1251_DEFAULT_CHANNEL;
  382. wl->monitor_present = false;
  383. wl->joined = false;
  384. wl1251_debugfs_reset(wl);
  385. mutex_unlock(&wl->mutex);
  386. }
  387. static int wl1251_op_add_interface(struct ieee80211_hw *hw,
  388. struct ieee80211_vif *vif)
  389. {
  390. struct wl1251 *wl = hw->priv;
  391. int ret = 0;
  392. vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
  393. IEEE80211_VIF_SUPPORTS_UAPSD |
  394. IEEE80211_VIF_SUPPORTS_CQM_RSSI;
  395. wl1251_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
  396. vif->type, vif->addr);
  397. mutex_lock(&wl->mutex);
  398. if (wl->vif) {
  399. ret = -EBUSY;
  400. goto out;
  401. }
  402. wl->vif = vif;
  403. switch (vif->type) {
  404. case NL80211_IFTYPE_STATION:
  405. wl->bss_type = BSS_TYPE_STA_BSS;
  406. break;
  407. case NL80211_IFTYPE_ADHOC:
  408. wl->bss_type = BSS_TYPE_IBSS;
  409. break;
  410. default:
  411. ret = -EOPNOTSUPP;
  412. goto out;
  413. }
  414. if (!ether_addr_equal_unaligned(wl->mac_addr, vif->addr)) {
  415. memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
  416. SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
  417. ret = wl1251_acx_station_id(wl);
  418. if (ret < 0)
  419. goto out;
  420. }
  421. out:
  422. mutex_unlock(&wl->mutex);
  423. return ret;
  424. }
  425. static void wl1251_op_remove_interface(struct ieee80211_hw *hw,
  426. struct ieee80211_vif *vif)
  427. {
  428. struct wl1251 *wl = hw->priv;
  429. mutex_lock(&wl->mutex);
  430. wl1251_debug(DEBUG_MAC80211, "mac80211 remove interface");
  431. wl->vif = NULL;
  432. eth_zero_addr(wl->bssid);
  433. mutex_unlock(&wl->mutex);
  434. }
  435. static int wl1251_build_null_data(struct wl1251 *wl)
  436. {
  437. struct sk_buff *skb = NULL;
  438. int size;
  439. void *ptr;
  440. int ret = -ENOMEM;
  441. if (wl->bss_type == BSS_TYPE_IBSS) {
  442. size = sizeof(struct wl12xx_null_data_template);
  443. ptr = NULL;
  444. } else {
  445. skb = ieee80211_nullfunc_get(wl->hw, wl->vif, false);
  446. if (!skb)
  447. goto out;
  448. size = skb->len;
  449. ptr = skb->data;
  450. }
  451. ret = wl1251_cmd_template_set(wl, CMD_NULL_DATA, ptr, size);
  452. out:
  453. dev_kfree_skb(skb);
  454. if (ret)
  455. wl1251_warning("cmd buld null data failed: %d", ret);
  456. return ret;
  457. }
  458. static int wl1251_build_qos_null_data(struct wl1251 *wl)
  459. {
  460. struct ieee80211_qos_hdr template;
  461. memset(&template, 0, sizeof(template));
  462. memcpy(template.addr1, wl->bssid, ETH_ALEN);
  463. memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
  464. memcpy(template.addr3, wl->bssid, ETH_ALEN);
  465. template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  466. IEEE80211_STYPE_QOS_NULLFUNC |
  467. IEEE80211_FCTL_TODS);
  468. /* FIXME: not sure what priority to use here */
  469. template.qos_ctrl = cpu_to_le16(0);
  470. return wl1251_cmd_template_set(wl, CMD_QOS_NULL_DATA, &template,
  471. sizeof(template));
  472. }
  473. static bool wl1251_can_do_pm(struct ieee80211_conf *conf, struct wl1251 *wl)
  474. {
  475. return (conf->flags & IEEE80211_CONF_PS) && !wl->monitor_present;
  476. }
  477. static int wl1251_op_config(struct ieee80211_hw *hw, u32 changed)
  478. {
  479. struct wl1251 *wl = hw->priv;
  480. struct ieee80211_conf *conf = &hw->conf;
  481. int channel, ret = 0;
  482. channel = ieee80211_frequency_to_channel(
  483. conf->chandef.chan->center_freq);
  484. wl1251_debug(DEBUG_MAC80211,
  485. "mac80211 config ch %d monitor %s psm %s power %d",
  486. channel,
  487. conf->flags & IEEE80211_CONF_MONITOR ? "on" : "off",
  488. conf->flags & IEEE80211_CONF_PS ? "on" : "off",
  489. conf->power_level);
  490. mutex_lock(&wl->mutex);
  491. ret = wl1251_ps_elp_wakeup(wl);
  492. if (ret < 0)
  493. goto out;
  494. if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
  495. u32 mode;
  496. if (conf->flags & IEEE80211_CONF_MONITOR) {
  497. wl->monitor_present = true;
  498. mode = DF_SNIFF_MODE_ENABLE | DF_ENCRYPTION_DISABLE;
  499. } else {
  500. wl->monitor_present = false;
  501. mode = 0;
  502. }
  503. ret = wl1251_acx_feature_cfg(wl, mode);
  504. if (ret < 0)
  505. goto out_sleep;
  506. }
  507. if (channel != wl->channel) {
  508. wl->channel = channel;
  509. /*
  510. * Use ENABLE_RX command for channel switching when no
  511. * interface is present (monitor mode only).
  512. * This leaves the tx path disabled in firmware, whereas
  513. * the usual JOIN command seems to transmit some frames
  514. * at firmware level.
  515. */
  516. if (wl->vif == NULL) {
  517. wl->joined = false;
  518. ret = wl1251_cmd_data_path_rx(wl, wl->channel, 1);
  519. } else {
  520. ret = wl1251_join(wl, wl->bss_type, wl->channel,
  521. wl->beacon_int, wl->dtim_period);
  522. }
  523. if (ret < 0)
  524. goto out_sleep;
  525. }
  526. if (wl1251_can_do_pm(conf, wl) && !wl->psm_requested) {
  527. wl1251_debug(DEBUG_PSM, "psm enabled");
  528. wl->psm_requested = true;
  529. wl->dtim_period = conf->ps_dtim_period;
  530. ret = wl1251_acx_wr_tbtt_and_dtim(wl, wl->beacon_int,
  531. wl->dtim_period);
  532. /*
  533. * mac80211 enables PSM only if we're already associated.
  534. */
  535. ret = wl1251_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
  536. if (ret < 0)
  537. goto out_sleep;
  538. } else if (!wl1251_can_do_pm(conf, wl) && wl->psm_requested) {
  539. wl1251_debug(DEBUG_PSM, "psm disabled");
  540. wl->psm_requested = false;
  541. if (wl->station_mode != STATION_ACTIVE_MODE) {
  542. ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
  543. if (ret < 0)
  544. goto out_sleep;
  545. }
  546. }
  547. if (changed & IEEE80211_CONF_CHANGE_IDLE && !wl->scanning) {
  548. if (conf->flags & IEEE80211_CONF_IDLE) {
  549. ret = wl1251_ps_set_mode(wl, STATION_IDLE);
  550. if (ret < 0)
  551. goto out_sleep;
  552. } else {
  553. ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
  554. if (ret < 0)
  555. goto out_sleep;
  556. ret = wl1251_join(wl, wl->bss_type, wl->channel,
  557. wl->beacon_int, wl->dtim_period);
  558. if (ret < 0)
  559. goto out_sleep;
  560. }
  561. }
  562. if (conf->power_level != wl->power_level) {
  563. ret = wl1251_acx_tx_power(wl, conf->power_level);
  564. if (ret < 0)
  565. goto out_sleep;
  566. wl->power_level = conf->power_level;
  567. }
  568. out_sleep:
  569. wl1251_ps_elp_sleep(wl);
  570. out:
  571. mutex_unlock(&wl->mutex);
  572. return ret;
  573. }
  574. struct wl1251_filter_params {
  575. bool enabled;
  576. int mc_list_length;
  577. u8 mc_list[ACX_MC_ADDRESS_GROUP_MAX][ETH_ALEN];
  578. };
  579. static u64 wl1251_op_prepare_multicast(struct ieee80211_hw *hw,
  580. struct netdev_hw_addr_list *mc_list)
  581. {
  582. struct wl1251_filter_params *fp;
  583. struct netdev_hw_addr *ha;
  584. struct wl1251 *wl = hw->priv;
  585. if (unlikely(wl->state == WL1251_STATE_OFF))
  586. return 0;
  587. fp = kzalloc(sizeof(*fp), GFP_ATOMIC);
  588. if (!fp) {
  589. wl1251_error("Out of memory setting filters.");
  590. return 0;
  591. }
  592. /* update multicast filtering parameters */
  593. fp->mc_list_length = 0;
  594. if (netdev_hw_addr_list_count(mc_list) > ACX_MC_ADDRESS_GROUP_MAX) {
  595. fp->enabled = false;
  596. } else {
  597. fp->enabled = true;
  598. netdev_hw_addr_list_for_each(ha, mc_list) {
  599. memcpy(fp->mc_list[fp->mc_list_length],
  600. ha->addr, ETH_ALEN);
  601. fp->mc_list_length++;
  602. }
  603. }
  604. return (u64)(unsigned long)fp;
  605. }
  606. #define WL1251_SUPPORTED_FILTERS (FIF_ALLMULTI | \
  607. FIF_FCSFAIL | \
  608. FIF_BCN_PRBRESP_PROMISC | \
  609. FIF_CONTROL | \
  610. FIF_OTHER_BSS | \
  611. FIF_PROBE_REQ)
  612. static void wl1251_op_configure_filter(struct ieee80211_hw *hw,
  613. unsigned int changed,
  614. unsigned int *total, u64 multicast)
  615. {
  616. struct wl1251_filter_params *fp = (void *)(unsigned long)multicast;
  617. struct wl1251 *wl = hw->priv;
  618. int ret;
  619. wl1251_debug(DEBUG_MAC80211, "mac80211 configure filter");
  620. *total &= WL1251_SUPPORTED_FILTERS;
  621. changed &= WL1251_SUPPORTED_FILTERS;
  622. if (changed == 0) {
  623. /* no filters which we support changed */
  624. kfree(fp);
  625. return;
  626. }
  627. mutex_lock(&wl->mutex);
  628. wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
  629. wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
  630. if (*total & FIF_ALLMULTI)
  631. /*
  632. * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
  633. * all multicast frames
  634. */
  635. wl->rx_config &= ~CFG_MC_FILTER_EN;
  636. if (*total & FIF_FCSFAIL)
  637. wl->rx_filter |= CFG_RX_FCS_ERROR;
  638. if (*total & FIF_BCN_PRBRESP_PROMISC) {
  639. wl->rx_config &= ~CFG_BSSID_FILTER_EN;
  640. wl->rx_config &= ~CFG_SSID_FILTER_EN;
  641. }
  642. if (*total & FIF_CONTROL)
  643. wl->rx_filter |= CFG_RX_CTL_EN;
  644. if (*total & FIF_OTHER_BSS || is_zero_ether_addr(wl->bssid))
  645. wl->rx_config &= ~CFG_BSSID_FILTER_EN;
  646. if (*total & FIF_PROBE_REQ)
  647. wl->rx_filter |= CFG_RX_PREQ_EN;
  648. if (wl->state == WL1251_STATE_OFF)
  649. goto out;
  650. ret = wl1251_ps_elp_wakeup(wl);
  651. if (ret < 0)
  652. goto out;
  653. if (*total & FIF_ALLMULTI)
  654. ret = wl1251_acx_group_address_tbl(wl, false, NULL, 0);
  655. else if (fp)
  656. ret = wl1251_acx_group_address_tbl(wl, fp->enabled,
  657. fp->mc_list,
  658. fp->mc_list_length);
  659. if (ret < 0)
  660. goto out;
  661. /* send filters to firmware */
  662. wl1251_acx_rx_config(wl, wl->rx_config, wl->rx_filter);
  663. wl1251_ps_elp_sleep(wl);
  664. out:
  665. mutex_unlock(&wl->mutex);
  666. kfree(fp);
  667. }
  668. /* HW encryption */
  669. static int wl1251_set_key_type(struct wl1251 *wl,
  670. struct wl1251_cmd_set_keys *key,
  671. enum set_key_cmd cmd,
  672. struct ieee80211_key_conf *mac80211_key,
  673. const u8 *addr)
  674. {
  675. switch (mac80211_key->cipher) {
  676. case WLAN_CIPHER_SUITE_WEP40:
  677. case WLAN_CIPHER_SUITE_WEP104:
  678. if (is_broadcast_ether_addr(addr))
  679. key->key_type = KEY_WEP_DEFAULT;
  680. else
  681. key->key_type = KEY_WEP_ADDR;
  682. mac80211_key->hw_key_idx = mac80211_key->keyidx;
  683. break;
  684. case WLAN_CIPHER_SUITE_TKIP:
  685. if (is_broadcast_ether_addr(addr))
  686. key->key_type = KEY_TKIP_MIC_GROUP;
  687. else
  688. key->key_type = KEY_TKIP_MIC_PAIRWISE;
  689. mac80211_key->hw_key_idx = mac80211_key->keyidx;
  690. break;
  691. case WLAN_CIPHER_SUITE_CCMP:
  692. if (is_broadcast_ether_addr(addr))
  693. key->key_type = KEY_AES_GROUP;
  694. else
  695. key->key_type = KEY_AES_PAIRWISE;
  696. mac80211_key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  697. break;
  698. default:
  699. wl1251_error("Unknown key cipher 0x%x", mac80211_key->cipher);
  700. return -EOPNOTSUPP;
  701. }
  702. return 0;
  703. }
  704. static int wl1251_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  705. struct ieee80211_vif *vif,
  706. struct ieee80211_sta *sta,
  707. struct ieee80211_key_conf *key)
  708. {
  709. struct wl1251 *wl = hw->priv;
  710. struct wl1251_cmd_set_keys *wl_cmd;
  711. const u8 *addr;
  712. int ret;
  713. static const u8 bcast_addr[ETH_ALEN] =
  714. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  715. wl1251_debug(DEBUG_MAC80211, "mac80211 set key");
  716. wl_cmd = kzalloc(sizeof(*wl_cmd), GFP_KERNEL);
  717. if (!wl_cmd) {
  718. ret = -ENOMEM;
  719. goto out;
  720. }
  721. addr = sta ? sta->addr : bcast_addr;
  722. wl1251_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
  723. wl1251_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
  724. wl1251_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
  725. key->cipher, key->keyidx, key->keylen, key->flags);
  726. wl1251_dump(DEBUG_CRYPT, "KEY: ", key->key, key->keylen);
  727. if (is_zero_ether_addr(addr)) {
  728. /* We dont support TX only encryption */
  729. ret = -EOPNOTSUPP;
  730. goto out;
  731. }
  732. mutex_lock(&wl->mutex);
  733. switch (cmd) {
  734. case SET_KEY:
  735. if (wl->monitor_present) {
  736. ret = -EOPNOTSUPP;
  737. goto out_unlock;
  738. }
  739. wl_cmd->key_action = KEY_ADD_OR_REPLACE;
  740. break;
  741. case DISABLE_KEY:
  742. wl_cmd->key_action = KEY_REMOVE;
  743. break;
  744. default:
  745. wl1251_error("Unsupported key cmd 0x%x", cmd);
  746. break;
  747. }
  748. ret = wl1251_ps_elp_wakeup(wl);
  749. if (ret < 0)
  750. goto out_unlock;
  751. ret = wl1251_set_key_type(wl, wl_cmd, cmd, key, addr);
  752. if (ret < 0) {
  753. wl1251_error("Set KEY type failed");
  754. goto out_sleep;
  755. }
  756. if (wl_cmd->key_type != KEY_WEP_DEFAULT)
  757. memcpy(wl_cmd->addr, addr, ETH_ALEN);
  758. if ((wl_cmd->key_type == KEY_TKIP_MIC_GROUP) ||
  759. (wl_cmd->key_type == KEY_TKIP_MIC_PAIRWISE)) {
  760. /*
  761. * We get the key in the following form:
  762. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  763. * but the target is expecting:
  764. * TKIP - RX MIC - TX MIC
  765. */
  766. memcpy(wl_cmd->key, key->key, 16);
  767. memcpy(wl_cmd->key + 16, key->key + 24, 8);
  768. memcpy(wl_cmd->key + 24, key->key + 16, 8);
  769. } else {
  770. memcpy(wl_cmd->key, key->key, key->keylen);
  771. }
  772. wl_cmd->key_size = key->keylen;
  773. wl_cmd->id = key->keyidx;
  774. wl_cmd->ssid_profile = 0;
  775. wl1251_dump(DEBUG_CRYPT, "TARGET KEY: ", wl_cmd, sizeof(*wl_cmd));
  776. ret = wl1251_cmd_send(wl, CMD_SET_KEYS, wl_cmd, sizeof(*wl_cmd));
  777. if (ret < 0) {
  778. wl1251_warning("could not set keys");
  779. goto out_sleep;
  780. }
  781. out_sleep:
  782. wl1251_ps_elp_sleep(wl);
  783. out_unlock:
  784. mutex_unlock(&wl->mutex);
  785. out:
  786. kfree(wl_cmd);
  787. return ret;
  788. }
  789. static int wl1251_op_hw_scan(struct ieee80211_hw *hw,
  790. struct ieee80211_vif *vif,
  791. struct ieee80211_scan_request *hw_req)
  792. {
  793. struct cfg80211_scan_request *req = &hw_req->req;
  794. struct wl1251 *wl = hw->priv;
  795. struct sk_buff *skb;
  796. size_t ssid_len = 0;
  797. u8 *ssid = NULL;
  798. int ret;
  799. wl1251_debug(DEBUG_MAC80211, "mac80211 hw scan");
  800. if (req->n_ssids) {
  801. ssid = req->ssids[0].ssid;
  802. ssid_len = req->ssids[0].ssid_len;
  803. }
  804. mutex_lock(&wl->mutex);
  805. if (wl->scanning) {
  806. wl1251_debug(DEBUG_SCAN, "scan already in progress");
  807. ret = -EINVAL;
  808. goto out;
  809. }
  810. ret = wl1251_ps_elp_wakeup(wl);
  811. if (ret < 0)
  812. goto out;
  813. if (hw->conf.flags & IEEE80211_CONF_IDLE) {
  814. ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
  815. if (ret < 0)
  816. goto out_sleep;
  817. ret = wl1251_join(wl, wl->bss_type, wl->channel,
  818. wl->beacon_int, wl->dtim_period);
  819. if (ret < 0)
  820. goto out_sleep;
  821. }
  822. skb = ieee80211_probereq_get(wl->hw, wl->vif->addr, ssid, ssid_len,
  823. req->ie_len);
  824. if (!skb) {
  825. ret = -ENOMEM;
  826. goto out_idle;
  827. }
  828. if (req->ie_len)
  829. skb_put_data(skb, req->ie, req->ie_len);
  830. ret = wl1251_cmd_template_set(wl, CMD_PROBE_REQ, skb->data,
  831. skb->len);
  832. dev_kfree_skb(skb);
  833. if (ret < 0)
  834. goto out_idle;
  835. ret = wl1251_cmd_trigger_scan_to(wl, 0);
  836. if (ret < 0)
  837. goto out_idle;
  838. wl->scanning = true;
  839. ret = wl1251_cmd_scan(wl, ssid, ssid_len, req->channels,
  840. req->n_channels, WL1251_SCAN_NUM_PROBES);
  841. if (ret < 0) {
  842. wl1251_debug(DEBUG_SCAN, "scan failed %d", ret);
  843. wl->scanning = false;
  844. goto out_idle;
  845. }
  846. goto out_sleep;
  847. out_idle:
  848. if (hw->conf.flags & IEEE80211_CONF_IDLE)
  849. ret = wl1251_ps_set_mode(wl, STATION_IDLE);
  850. out_sleep:
  851. wl1251_ps_elp_sleep(wl);
  852. out:
  853. mutex_unlock(&wl->mutex);
  854. return ret;
  855. }
  856. static int wl1251_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  857. {
  858. struct wl1251 *wl = hw->priv;
  859. int ret;
  860. mutex_lock(&wl->mutex);
  861. ret = wl1251_ps_elp_wakeup(wl);
  862. if (ret < 0)
  863. goto out;
  864. ret = wl1251_acx_rts_threshold(wl, (u16) value);
  865. if (ret < 0)
  866. wl1251_warning("wl1251_op_set_rts_threshold failed: %d", ret);
  867. wl1251_ps_elp_sleep(wl);
  868. out:
  869. mutex_unlock(&wl->mutex);
  870. return ret;
  871. }
  872. static void wl1251_op_bss_info_changed(struct ieee80211_hw *hw,
  873. struct ieee80211_vif *vif,
  874. struct ieee80211_bss_conf *bss_conf,
  875. u32 changed)
  876. {
  877. struct wl1251 *wl = hw->priv;
  878. struct sk_buff *beacon, *skb;
  879. bool enable;
  880. int ret;
  881. wl1251_debug(DEBUG_MAC80211, "mac80211 bss info changed");
  882. mutex_lock(&wl->mutex);
  883. ret = wl1251_ps_elp_wakeup(wl);
  884. if (ret < 0)
  885. goto out;
  886. if (changed & BSS_CHANGED_CQM) {
  887. ret = wl1251_acx_low_rssi(wl, bss_conf->cqm_rssi_thold,
  888. WL1251_DEFAULT_LOW_RSSI_WEIGHT,
  889. WL1251_DEFAULT_LOW_RSSI_DEPTH,
  890. WL1251_ACX_LOW_RSSI_TYPE_EDGE);
  891. if (ret < 0)
  892. goto out;
  893. wl->rssi_thold = bss_conf->cqm_rssi_thold;
  894. }
  895. if ((changed & BSS_CHANGED_BSSID) &&
  896. memcmp(wl->bssid, bss_conf->bssid, ETH_ALEN)) {
  897. memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
  898. if (!is_zero_ether_addr(wl->bssid)) {
  899. ret = wl1251_build_null_data(wl);
  900. if (ret < 0)
  901. goto out_sleep;
  902. ret = wl1251_build_qos_null_data(wl);
  903. if (ret < 0)
  904. goto out_sleep;
  905. ret = wl1251_join(wl, wl->bss_type, wl->channel,
  906. wl->beacon_int, wl->dtim_period);
  907. if (ret < 0)
  908. goto out_sleep;
  909. }
  910. }
  911. if (changed & BSS_CHANGED_ASSOC) {
  912. if (bss_conf->assoc) {
  913. wl->beacon_int = bss_conf->beacon_int;
  914. skb = ieee80211_pspoll_get(wl->hw, wl->vif);
  915. if (!skb)
  916. goto out_sleep;
  917. ret = wl1251_cmd_template_set(wl, CMD_PS_POLL,
  918. skb->data,
  919. skb->len);
  920. dev_kfree_skb(skb);
  921. if (ret < 0)
  922. goto out_sleep;
  923. ret = wl1251_acx_aid(wl, bss_conf->aid);
  924. if (ret < 0)
  925. goto out_sleep;
  926. } else {
  927. /* use defaults when not associated */
  928. wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
  929. wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
  930. }
  931. }
  932. if (changed & BSS_CHANGED_ERP_SLOT) {
  933. if (bss_conf->use_short_slot)
  934. ret = wl1251_acx_slot(wl, SLOT_TIME_SHORT);
  935. else
  936. ret = wl1251_acx_slot(wl, SLOT_TIME_LONG);
  937. if (ret < 0) {
  938. wl1251_warning("Set slot time failed %d", ret);
  939. goto out_sleep;
  940. }
  941. }
  942. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  943. if (bss_conf->use_short_preamble)
  944. wl1251_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
  945. else
  946. wl1251_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
  947. }
  948. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  949. if (bss_conf->use_cts_prot)
  950. ret = wl1251_acx_cts_protect(wl, CTSPROTECT_ENABLE);
  951. else
  952. ret = wl1251_acx_cts_protect(wl, CTSPROTECT_DISABLE);
  953. if (ret < 0) {
  954. wl1251_warning("Set ctsprotect failed %d", ret);
  955. goto out_sleep;
  956. }
  957. }
  958. if (changed & BSS_CHANGED_ARP_FILTER) {
  959. __be32 addr = bss_conf->arp_addr_list[0];
  960. WARN_ON(wl->bss_type != BSS_TYPE_STA_BSS);
  961. enable = bss_conf->arp_addr_cnt == 1 && bss_conf->assoc;
  962. ret = wl1251_acx_arp_ip_filter(wl, enable, addr);
  963. if (ret < 0)
  964. goto out_sleep;
  965. }
  966. if (changed & BSS_CHANGED_BEACON) {
  967. beacon = ieee80211_beacon_get(hw, vif);
  968. if (!beacon)
  969. goto out_sleep;
  970. ret = wl1251_cmd_template_set(wl, CMD_BEACON, beacon->data,
  971. beacon->len);
  972. if (ret < 0) {
  973. dev_kfree_skb(beacon);
  974. goto out_sleep;
  975. }
  976. ret = wl1251_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
  977. beacon->len);
  978. dev_kfree_skb(beacon);
  979. if (ret < 0)
  980. goto out_sleep;
  981. ret = wl1251_join(wl, wl->bss_type, wl->channel,
  982. wl->beacon_int, wl->dtim_period);
  983. if (ret < 0)
  984. goto out_sleep;
  985. }
  986. out_sleep:
  987. wl1251_ps_elp_sleep(wl);
  988. out:
  989. mutex_unlock(&wl->mutex);
  990. }
  991. /* can't be const, mac80211 writes to this */
  992. static struct ieee80211_rate wl1251_rates[] = {
  993. { .bitrate = 10,
  994. .hw_value = 0x1,
  995. .hw_value_short = 0x1, },
  996. { .bitrate = 20,
  997. .hw_value = 0x2,
  998. .hw_value_short = 0x2,
  999. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  1000. { .bitrate = 55,
  1001. .hw_value = 0x4,
  1002. .hw_value_short = 0x4,
  1003. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  1004. { .bitrate = 110,
  1005. .hw_value = 0x20,
  1006. .hw_value_short = 0x20,
  1007. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  1008. { .bitrate = 60,
  1009. .hw_value = 0x8,
  1010. .hw_value_short = 0x8, },
  1011. { .bitrate = 90,
  1012. .hw_value = 0x10,
  1013. .hw_value_short = 0x10, },
  1014. { .bitrate = 120,
  1015. .hw_value = 0x40,
  1016. .hw_value_short = 0x40, },
  1017. { .bitrate = 180,
  1018. .hw_value = 0x80,
  1019. .hw_value_short = 0x80, },
  1020. { .bitrate = 240,
  1021. .hw_value = 0x200,
  1022. .hw_value_short = 0x200, },
  1023. { .bitrate = 360,
  1024. .hw_value = 0x400,
  1025. .hw_value_short = 0x400, },
  1026. { .bitrate = 480,
  1027. .hw_value = 0x800,
  1028. .hw_value_short = 0x800, },
  1029. { .bitrate = 540,
  1030. .hw_value = 0x1000,
  1031. .hw_value_short = 0x1000, },
  1032. };
  1033. /* can't be const, mac80211 writes to this */
  1034. static struct ieee80211_channel wl1251_channels[] = {
  1035. { .hw_value = 1, .center_freq = 2412},
  1036. { .hw_value = 2, .center_freq = 2417},
  1037. { .hw_value = 3, .center_freq = 2422},
  1038. { .hw_value = 4, .center_freq = 2427},
  1039. { .hw_value = 5, .center_freq = 2432},
  1040. { .hw_value = 6, .center_freq = 2437},
  1041. { .hw_value = 7, .center_freq = 2442},
  1042. { .hw_value = 8, .center_freq = 2447},
  1043. { .hw_value = 9, .center_freq = 2452},
  1044. { .hw_value = 10, .center_freq = 2457},
  1045. { .hw_value = 11, .center_freq = 2462},
  1046. { .hw_value = 12, .center_freq = 2467},
  1047. { .hw_value = 13, .center_freq = 2472},
  1048. };
  1049. static int wl1251_op_conf_tx(struct ieee80211_hw *hw,
  1050. struct ieee80211_vif *vif, u16 queue,
  1051. const struct ieee80211_tx_queue_params *params)
  1052. {
  1053. enum wl1251_acx_ps_scheme ps_scheme;
  1054. struct wl1251 *wl = hw->priv;
  1055. int ret;
  1056. mutex_lock(&wl->mutex);
  1057. wl1251_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
  1058. ret = wl1251_ps_elp_wakeup(wl);
  1059. if (ret < 0)
  1060. goto out;
  1061. /* mac80211 uses units of 32 usec */
  1062. ret = wl1251_acx_ac_cfg(wl, wl1251_tx_get_queue(queue),
  1063. params->cw_min, params->cw_max,
  1064. params->aifs, params->txop * 32);
  1065. if (ret < 0)
  1066. goto out_sleep;
  1067. if (params->uapsd)
  1068. ps_scheme = WL1251_ACX_PS_SCHEME_UPSD_TRIGGER;
  1069. else
  1070. ps_scheme = WL1251_ACX_PS_SCHEME_LEGACY;
  1071. ret = wl1251_acx_tid_cfg(wl, wl1251_tx_get_queue(queue),
  1072. CHANNEL_TYPE_EDCF,
  1073. wl1251_tx_get_queue(queue), ps_scheme,
  1074. WL1251_ACX_ACK_POLICY_LEGACY);
  1075. if (ret < 0)
  1076. goto out_sleep;
  1077. out_sleep:
  1078. wl1251_ps_elp_sleep(wl);
  1079. out:
  1080. mutex_unlock(&wl->mutex);
  1081. return ret;
  1082. }
  1083. static int wl1251_op_get_survey(struct ieee80211_hw *hw, int idx,
  1084. struct survey_info *survey)
  1085. {
  1086. struct wl1251 *wl = hw->priv;
  1087. struct ieee80211_conf *conf = &hw->conf;
  1088. if (idx != 0)
  1089. return -ENOENT;
  1090. survey->channel = conf->chandef.chan;
  1091. survey->filled = SURVEY_INFO_NOISE_DBM;
  1092. survey->noise = wl->noise;
  1093. return 0;
  1094. }
  1095. /* can't be const, mac80211 writes to this */
  1096. static struct ieee80211_supported_band wl1251_band_2ghz = {
  1097. .channels = wl1251_channels,
  1098. .n_channels = ARRAY_SIZE(wl1251_channels),
  1099. .bitrates = wl1251_rates,
  1100. .n_bitrates = ARRAY_SIZE(wl1251_rates),
  1101. };
  1102. static const struct ieee80211_ops wl1251_ops = {
  1103. .start = wl1251_op_start,
  1104. .stop = wl1251_op_stop,
  1105. .add_interface = wl1251_op_add_interface,
  1106. .remove_interface = wl1251_op_remove_interface,
  1107. .config = wl1251_op_config,
  1108. .prepare_multicast = wl1251_op_prepare_multicast,
  1109. .configure_filter = wl1251_op_configure_filter,
  1110. .tx = wl1251_op_tx,
  1111. .set_key = wl1251_op_set_key,
  1112. .hw_scan = wl1251_op_hw_scan,
  1113. .bss_info_changed = wl1251_op_bss_info_changed,
  1114. .set_rts_threshold = wl1251_op_set_rts_threshold,
  1115. .conf_tx = wl1251_op_conf_tx,
  1116. .get_survey = wl1251_op_get_survey,
  1117. };
  1118. static int wl1251_read_eeprom_byte(struct wl1251 *wl, off_t offset, u8 *data)
  1119. {
  1120. unsigned long timeout;
  1121. wl1251_reg_write32(wl, EE_ADDR, offset);
  1122. wl1251_reg_write32(wl, EE_CTL, EE_CTL_READ);
  1123. /* EE_CTL_READ clears when data is ready */
  1124. timeout = jiffies + msecs_to_jiffies(100);
  1125. while (1) {
  1126. if (!(wl1251_reg_read32(wl, EE_CTL) & EE_CTL_READ))
  1127. break;
  1128. if (time_after(jiffies, timeout))
  1129. return -ETIMEDOUT;
  1130. msleep(1);
  1131. }
  1132. *data = wl1251_reg_read32(wl, EE_DATA);
  1133. return 0;
  1134. }
  1135. static int wl1251_read_eeprom(struct wl1251 *wl, off_t offset,
  1136. u8 *data, size_t len)
  1137. {
  1138. size_t i;
  1139. int ret;
  1140. wl1251_reg_write32(wl, EE_START, 0);
  1141. for (i = 0; i < len; i++) {
  1142. ret = wl1251_read_eeprom_byte(wl, offset + i, &data[i]);
  1143. if (ret < 0)
  1144. return ret;
  1145. }
  1146. return 0;
  1147. }
  1148. static int wl1251_read_eeprom_mac(struct wl1251 *wl)
  1149. {
  1150. u8 mac[ETH_ALEN];
  1151. int i, ret;
  1152. wl1251_set_partition(wl, 0, 0, REGISTERS_BASE, REGISTERS_DOWN_SIZE);
  1153. ret = wl1251_read_eeprom(wl, 0x1c, mac, sizeof(mac));
  1154. if (ret < 0) {
  1155. wl1251_warning("failed to read MAC address from EEPROM");
  1156. return ret;
  1157. }
  1158. /* MAC is stored in reverse order */
  1159. for (i = 0; i < ETH_ALEN; i++)
  1160. wl->mac_addr[i] = mac[ETH_ALEN - i - 1];
  1161. return 0;
  1162. }
  1163. #define NVS_OFF_MAC_LEN 0x19
  1164. #define NVS_OFF_MAC_ADDR_LO 0x1a
  1165. #define NVS_OFF_MAC_ADDR_HI 0x1b
  1166. #define NVS_OFF_MAC_DATA 0x1c
  1167. static int wl1251_check_nvs_mac(struct wl1251 *wl)
  1168. {
  1169. if (wl->nvs_len < 0x24)
  1170. return -ENODATA;
  1171. /* length is 2 and data address is 0x546c (ANDed with 0xfffe) */
  1172. if (wl->nvs[NVS_OFF_MAC_LEN] != 2 ||
  1173. wl->nvs[NVS_OFF_MAC_ADDR_LO] != 0x6d ||
  1174. wl->nvs[NVS_OFF_MAC_ADDR_HI] != 0x54)
  1175. return -EINVAL;
  1176. return 0;
  1177. }
  1178. static int wl1251_read_nvs_mac(struct wl1251 *wl)
  1179. {
  1180. u8 mac[ETH_ALEN];
  1181. int i, ret;
  1182. ret = wl1251_check_nvs_mac(wl);
  1183. if (ret)
  1184. return ret;
  1185. /* MAC is stored in reverse order */
  1186. for (i = 0; i < ETH_ALEN; i++)
  1187. mac[i] = wl->nvs[NVS_OFF_MAC_DATA + ETH_ALEN - i - 1];
  1188. /* 00:00:20:07:03:09 is in example file wl1251-nvs.bin, so invalid */
  1189. if (ether_addr_equal_unaligned(mac, "\x00\x00\x20\x07\x03\x09"))
  1190. return -EINVAL;
  1191. memcpy(wl->mac_addr, mac, ETH_ALEN);
  1192. return 0;
  1193. }
  1194. static int wl1251_write_nvs_mac(struct wl1251 *wl)
  1195. {
  1196. int i, ret;
  1197. ret = wl1251_check_nvs_mac(wl);
  1198. if (ret)
  1199. return ret;
  1200. /* MAC is stored in reverse order */
  1201. for (i = 0; i < ETH_ALEN; i++)
  1202. wl->nvs[NVS_OFF_MAC_DATA + i] = wl->mac_addr[ETH_ALEN - i - 1];
  1203. return 0;
  1204. }
  1205. static int wl1251_register_hw(struct wl1251 *wl)
  1206. {
  1207. int ret;
  1208. if (wl->mac80211_registered)
  1209. return 0;
  1210. SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
  1211. ret = ieee80211_register_hw(wl->hw);
  1212. if (ret < 0) {
  1213. wl1251_error("unable to register mac80211 hw: %d", ret);
  1214. return ret;
  1215. }
  1216. wl->mac80211_registered = true;
  1217. wl1251_notice("loaded");
  1218. return 0;
  1219. }
  1220. int wl1251_init_ieee80211(struct wl1251 *wl)
  1221. {
  1222. int ret;
  1223. /* The tx descriptor buffer and the TKIP space */
  1224. wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
  1225. + WL1251_TKIP_IV_SPACE;
  1226. /* unit us */
  1227. /* FIXME: find a proper value */
  1228. ieee80211_hw_set(wl->hw, SIGNAL_DBM);
  1229. ieee80211_hw_set(wl->hw, SUPPORTS_PS);
  1230. wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  1231. BIT(NL80211_IFTYPE_ADHOC);
  1232. wl->hw->wiphy->max_scan_ssids = 1;
  1233. wl->hw->wiphy->bands[NL80211_BAND_2GHZ] = &wl1251_band_2ghz;
  1234. wl->hw->queues = 4;
  1235. if (wl->nvs == NULL && !wl->use_eeprom) {
  1236. ret = wl1251_fetch_nvs(wl);
  1237. if (ret < 0)
  1238. goto out;
  1239. }
  1240. if (wl->use_eeprom)
  1241. ret = wl1251_read_eeprom_mac(wl);
  1242. else
  1243. ret = wl1251_read_nvs_mac(wl);
  1244. if (ret == 0 && !is_valid_ether_addr(wl->mac_addr))
  1245. ret = -EINVAL;
  1246. if (ret < 0) {
  1247. /*
  1248. * In case our MAC address is not correctly set,
  1249. * we use a random but Nokia MAC.
  1250. */
  1251. static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
  1252. memcpy(wl->mac_addr, nokia_oui, 3);
  1253. get_random_bytes(wl->mac_addr + 3, 3);
  1254. if (!wl->use_eeprom)
  1255. wl1251_write_nvs_mac(wl);
  1256. wl1251_warning("MAC address in eeprom or nvs data is not valid");
  1257. wl1251_warning("Setting random MAC address: %pM", wl->mac_addr);
  1258. }
  1259. ret = wl1251_register_hw(wl);
  1260. if (ret)
  1261. goto out;
  1262. wl1251_debugfs_init(wl);
  1263. wl1251_notice("initialized");
  1264. ret = 0;
  1265. out:
  1266. return ret;
  1267. }
  1268. EXPORT_SYMBOL_GPL(wl1251_init_ieee80211);
  1269. struct ieee80211_hw *wl1251_alloc_hw(void)
  1270. {
  1271. struct ieee80211_hw *hw;
  1272. struct wl1251 *wl;
  1273. int i;
  1274. hw = ieee80211_alloc_hw(sizeof(*wl), &wl1251_ops);
  1275. if (!hw) {
  1276. wl1251_error("could not alloc ieee80211_hw");
  1277. return ERR_PTR(-ENOMEM);
  1278. }
  1279. wl = hw->priv;
  1280. memset(wl, 0, sizeof(*wl));
  1281. wl->hw = hw;
  1282. wl->data_in_count = 0;
  1283. skb_queue_head_init(&wl->tx_queue);
  1284. INIT_DELAYED_WORK(&wl->elp_work, wl1251_elp_work);
  1285. wl->channel = WL1251_DEFAULT_CHANNEL;
  1286. wl->monitor_present = false;
  1287. wl->joined = false;
  1288. wl->scanning = false;
  1289. wl->bss_type = MAX_BSS_TYPE;
  1290. wl->default_key = 0;
  1291. wl->listen_int = 1;
  1292. wl->rx_counter = 0;
  1293. wl->rx_handled = 0;
  1294. wl->rx_current_buffer = 0;
  1295. wl->rx_last_id = 0;
  1296. wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
  1297. wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
  1298. wl->elp = false;
  1299. wl->station_mode = STATION_ACTIVE_MODE;
  1300. wl->psm_requested = false;
  1301. wl->psm_entry_retry = 0;
  1302. wl->tx_queue_stopped = false;
  1303. wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
  1304. wl->rssi_thold = 0;
  1305. wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
  1306. wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
  1307. wl->vif = NULL;
  1308. for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
  1309. wl->tx_frames[i] = NULL;
  1310. wl->next_tx_complete = 0;
  1311. INIT_WORK(&wl->irq_work, wl1251_irq_work);
  1312. INIT_WORK(&wl->tx_work, wl1251_tx_work);
  1313. wl->state = WL1251_STATE_OFF;
  1314. mutex_init(&wl->mutex);
  1315. spin_lock_init(&wl->wl_lock);
  1316. wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
  1317. wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
  1318. wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
  1319. if (!wl->rx_descriptor) {
  1320. wl1251_error("could not allocate memory for rx descriptor");
  1321. ieee80211_free_hw(hw);
  1322. return ERR_PTR(-ENOMEM);
  1323. }
  1324. return hw;
  1325. }
  1326. EXPORT_SYMBOL_GPL(wl1251_alloc_hw);
  1327. int wl1251_free_hw(struct wl1251 *wl)
  1328. {
  1329. ieee80211_unregister_hw(wl->hw);
  1330. wl1251_debugfs_exit(wl);
  1331. kfree(wl->target_mem_map);
  1332. kfree(wl->data_path);
  1333. vfree(wl->fw);
  1334. wl->fw = NULL;
  1335. kfree(wl->nvs);
  1336. wl->nvs = NULL;
  1337. kfree(wl->rx_descriptor);
  1338. wl->rx_descriptor = NULL;
  1339. ieee80211_free_hw(wl->hw);
  1340. return 0;
  1341. }
  1342. EXPORT_SYMBOL_GPL(wl1251_free_hw);
  1343. MODULE_DESCRIPTION("TI wl1251 Wireless LAN Driver Core");
  1344. MODULE_LICENSE("GPL");
  1345. MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");
  1346. MODULE_FIRMWARE(WL1251_FW_NAME);
  1347. MODULE_FIRMWARE(WL1251_NVS_NAME);