wl1251_main.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  1. /*
  2. * This file is part of wl1251
  3. *
  4. * Copyright (C) 2008-2009 Nokia Corporation
  5. *
  6. * Contact: Kalle Valo <kalle.valo@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/firmware.h>
  26. #include <linux/delay.h>
  27. #include <linux/irq.h>
  28. #include <linux/crc32.h>
  29. #include <linux/etherdevice.h>
  30. #include <linux/vmalloc.h>
  31. #include "wl1251.h"
  32. #include "wl12xx_80211.h"
  33. #include "wl1251_reg.h"
  34. #include "wl1251_io.h"
  35. #include "wl1251_cmd.h"
  36. #include "wl1251_event.h"
  37. #include "wl1251_tx.h"
  38. #include "wl1251_rx.h"
  39. #include "wl1251_ps.h"
  40. #include "wl1251_init.h"
  41. #include "wl1251_debugfs.h"
  42. #include "wl1251_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 void wl1251_power_off(struct wl1251 *wl)
  52. {
  53. wl->set_power(false);
  54. }
  55. static void wl1251_power_on(struct wl1251 *wl)
  56. {
  57. wl->set_power(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_len = fw->size;
  105. wl->nvs = kmalloc(wl->nvs_len, GFP_KERNEL);
  106. if (!wl->nvs) {
  107. wl1251_error("could not allocate memory for the nvs file");
  108. ret = -ENOMEM;
  109. goto out;
  110. }
  111. memcpy(wl->nvs, fw->data, wl->nvs_len);
  112. ret = 0;
  113. out:
  114. release_firmware(fw);
  115. return ret;
  116. }
  117. static void wl1251_fw_wakeup(struct wl1251 *wl)
  118. {
  119. u32 elp_reg;
  120. elp_reg = ELPCTRL_WAKE_UP;
  121. wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
  122. elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
  123. if (!(elp_reg & ELPCTRL_WLAN_READY))
  124. wl1251_warning("WLAN not ready");
  125. }
  126. static int wl1251_chip_wakeup(struct wl1251 *wl)
  127. {
  128. int ret = 0;
  129. wl1251_power_on(wl);
  130. msleep(WL1251_POWER_ON_SLEEP);
  131. wl->if_ops->reset(wl);
  132. /* We don't need a real memory partition here, because we only want
  133. * to use the registers at this point. */
  134. wl1251_set_partition(wl,
  135. 0x00000000,
  136. 0x00000000,
  137. REGISTERS_BASE,
  138. REGISTERS_DOWN_SIZE);
  139. /* ELP module wake up */
  140. wl1251_fw_wakeup(wl);
  141. /* whal_FwCtrl_BootSm() */
  142. /* 0. read chip id from CHIP_ID */
  143. wl->chip_id = wl1251_reg_read32(wl, CHIP_ID_B);
  144. /* 1. check if chip id is valid */
  145. switch (wl->chip_id) {
  146. case CHIP_ID_1251_PG12:
  147. wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG12)",
  148. wl->chip_id);
  149. break;
  150. case CHIP_ID_1251_PG11:
  151. wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG11)",
  152. wl->chip_id);
  153. break;
  154. case CHIP_ID_1251_PG10:
  155. default:
  156. wl1251_error("unsupported chip id: 0x%x", wl->chip_id);
  157. ret = -ENODEV;
  158. goto out;
  159. }
  160. if (wl->fw == NULL) {
  161. ret = wl1251_fetch_firmware(wl);
  162. if (ret < 0)
  163. goto out;
  164. }
  165. /* No NVS from netlink, try to get it from the filesystem */
  166. if (wl->nvs == NULL) {
  167. ret = wl1251_fetch_nvs(wl);
  168. if (ret < 0)
  169. goto out;
  170. }
  171. out:
  172. return ret;
  173. }
  174. #define WL1251_IRQ_LOOP_COUNT 10
  175. static void wl1251_irq_work(struct work_struct *work)
  176. {
  177. u32 intr, ctr = WL1251_IRQ_LOOP_COUNT;
  178. struct wl1251 *wl =
  179. container_of(work, struct wl1251, irq_work);
  180. int ret;
  181. mutex_lock(&wl->mutex);
  182. wl1251_debug(DEBUG_IRQ, "IRQ work");
  183. if (wl->state == WL1251_STATE_OFF)
  184. goto out;
  185. ret = wl1251_ps_elp_wakeup(wl);
  186. if (ret < 0)
  187. goto out;
  188. wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, WL1251_ACX_INTR_ALL);
  189. intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
  190. wl1251_debug(DEBUG_IRQ, "intr: 0x%x", intr);
  191. do {
  192. if (wl->data_path) {
  193. wl->rx_counter = wl1251_mem_read32(
  194. wl, wl->data_path->rx_control_addr);
  195. /* We handle a frmware bug here */
  196. switch ((wl->rx_counter - wl->rx_handled) & 0xf) {
  197. case 0:
  198. wl1251_debug(DEBUG_IRQ,
  199. "RX: FW and host in sync");
  200. intr &= ~WL1251_ACX_INTR_RX0_DATA;
  201. intr &= ~WL1251_ACX_INTR_RX1_DATA;
  202. break;
  203. case 1:
  204. wl1251_debug(DEBUG_IRQ, "RX: FW +1");
  205. intr |= WL1251_ACX_INTR_RX0_DATA;
  206. intr &= ~WL1251_ACX_INTR_RX1_DATA;
  207. break;
  208. case 2:
  209. wl1251_debug(DEBUG_IRQ, "RX: FW +2");
  210. intr |= WL1251_ACX_INTR_RX0_DATA;
  211. intr |= WL1251_ACX_INTR_RX1_DATA;
  212. break;
  213. default:
  214. wl1251_warning(
  215. "RX: FW and host out of sync: %d",
  216. wl->rx_counter - wl->rx_handled);
  217. break;
  218. }
  219. wl->rx_handled = wl->rx_counter;
  220. wl1251_debug(DEBUG_IRQ, "RX counter: %d",
  221. wl->rx_counter);
  222. }
  223. intr &= wl->intr_mask;
  224. if (intr == 0) {
  225. wl1251_debug(DEBUG_IRQ, "INTR is 0");
  226. goto out_sleep;
  227. }
  228. if (intr & WL1251_ACX_INTR_RX0_DATA) {
  229. wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX0_DATA");
  230. wl1251_rx(wl);
  231. }
  232. if (intr & WL1251_ACX_INTR_RX1_DATA) {
  233. wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX1_DATA");
  234. wl1251_rx(wl);
  235. }
  236. if (intr & WL1251_ACX_INTR_TX_RESULT) {
  237. wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_TX_RESULT");
  238. wl1251_tx_complete(wl);
  239. }
  240. if (intr & (WL1251_ACX_INTR_EVENT_A |
  241. WL1251_ACX_INTR_EVENT_B)) {
  242. wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT (0x%x)",
  243. intr);
  244. if (intr & WL1251_ACX_INTR_EVENT_A)
  245. wl1251_event_handle(wl, 0);
  246. else
  247. wl1251_event_handle(wl, 1);
  248. }
  249. if (intr & WL1251_ACX_INTR_INIT_COMPLETE)
  250. wl1251_debug(DEBUG_IRQ,
  251. "WL1251_ACX_INTR_INIT_COMPLETE");
  252. if (--ctr == 0)
  253. break;
  254. intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
  255. } while (intr);
  256. out_sleep:
  257. wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, ~(wl->intr_mask));
  258. wl1251_ps_elp_sleep(wl);
  259. out:
  260. mutex_unlock(&wl->mutex);
  261. }
  262. static int wl1251_join(struct wl1251 *wl, u8 bss_type, u8 channel,
  263. u16 beacon_interval, u8 dtim_period)
  264. {
  265. int ret;
  266. ret = wl1251_acx_frame_rates(wl, DEFAULT_HW_GEN_TX_RATE,
  267. DEFAULT_HW_GEN_MODULATION_TYPE,
  268. wl->tx_mgmt_frm_rate,
  269. wl->tx_mgmt_frm_mod);
  270. if (ret < 0)
  271. goto out;
  272. ret = wl1251_cmd_join(wl, bss_type, channel, beacon_interval,
  273. dtim_period);
  274. if (ret < 0)
  275. goto out;
  276. /*
  277. * FIXME: we should wait for JOIN_EVENT_COMPLETE_ID but to simplify
  278. * locking we just sleep instead, for now
  279. */
  280. msleep(10);
  281. out:
  282. return ret;
  283. }
  284. static void wl1251_filter_work(struct work_struct *work)
  285. {
  286. struct wl1251 *wl =
  287. container_of(work, struct wl1251, filter_work);
  288. int ret;
  289. mutex_lock(&wl->mutex);
  290. if (wl->state == WL1251_STATE_OFF)
  291. goto out;
  292. ret = wl1251_ps_elp_wakeup(wl);
  293. if (ret < 0)
  294. goto out;
  295. ret = wl1251_join(wl, wl->bss_type, wl->channel, wl->beacon_int,
  296. wl->dtim_period);
  297. if (ret < 0)
  298. goto out_sleep;
  299. out_sleep:
  300. wl1251_ps_elp_sleep(wl);
  301. out:
  302. mutex_unlock(&wl->mutex);
  303. }
  304. static int wl1251_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  305. {
  306. struct wl1251 *wl = hw->priv;
  307. skb_queue_tail(&wl->tx_queue, skb);
  308. /*
  309. * The chip specific setup must run before the first TX packet -
  310. * before that, the tx_work will not be initialized!
  311. */
  312. ieee80211_queue_work(wl->hw, &wl->tx_work);
  313. /*
  314. * The workqueue is slow to process the tx_queue and we need stop
  315. * the queue here, otherwise the queue will get too long.
  316. */
  317. if (skb_queue_len(&wl->tx_queue) >= WL1251_TX_QUEUE_MAX_LENGTH) {
  318. wl1251_debug(DEBUG_TX, "op_tx: tx_queue full, stop queues");
  319. ieee80211_stop_queues(wl->hw);
  320. /*
  321. * FIXME: this is racy, the variable is not properly
  322. * protected. Maybe fix this by removing the stupid
  323. * variable altogether and checking the real queue state?
  324. */
  325. wl->tx_queue_stopped = true;
  326. }
  327. return NETDEV_TX_OK;
  328. }
  329. static int wl1251_op_start(struct ieee80211_hw *hw)
  330. {
  331. struct wl1251 *wl = hw->priv;
  332. int ret = 0;
  333. wl1251_debug(DEBUG_MAC80211, "mac80211 start");
  334. mutex_lock(&wl->mutex);
  335. if (wl->state != WL1251_STATE_OFF) {
  336. wl1251_error("cannot start because not in off state: %d",
  337. wl->state);
  338. ret = -EBUSY;
  339. goto out;
  340. }
  341. ret = wl1251_chip_wakeup(wl);
  342. if (ret < 0)
  343. goto out;
  344. ret = wl1251_boot(wl);
  345. if (ret < 0)
  346. goto out;
  347. ret = wl1251_hw_init(wl);
  348. if (ret < 0)
  349. goto out;
  350. ret = wl1251_acx_station_id(wl);
  351. if (ret < 0)
  352. goto out;
  353. wl->state = WL1251_STATE_ON;
  354. wl1251_info("firmware booted (%s)", wl->fw_ver);
  355. out:
  356. if (ret < 0)
  357. wl1251_power_off(wl);
  358. mutex_unlock(&wl->mutex);
  359. return ret;
  360. }
  361. static void wl1251_op_stop(struct ieee80211_hw *hw)
  362. {
  363. struct wl1251 *wl = hw->priv;
  364. wl1251_info("down");
  365. wl1251_debug(DEBUG_MAC80211, "mac80211 stop");
  366. mutex_lock(&wl->mutex);
  367. WARN_ON(wl->state != WL1251_STATE_ON);
  368. if (wl->scanning) {
  369. mutex_unlock(&wl->mutex);
  370. ieee80211_scan_completed(wl->hw, true);
  371. mutex_lock(&wl->mutex);
  372. wl->scanning = false;
  373. }
  374. wl->state = WL1251_STATE_OFF;
  375. wl1251_disable_interrupts(wl);
  376. mutex_unlock(&wl->mutex);
  377. cancel_work_sync(&wl->irq_work);
  378. cancel_work_sync(&wl->tx_work);
  379. cancel_work_sync(&wl->filter_work);
  380. mutex_lock(&wl->mutex);
  381. /* let's notify MAC80211 about the remaining pending TX frames */
  382. wl1251_tx_flush(wl);
  383. wl1251_power_off(wl);
  384. memset(wl->bssid, 0, ETH_ALEN);
  385. wl->listen_int = 1;
  386. wl->bss_type = MAX_BSS_TYPE;
  387. wl->data_in_count = 0;
  388. wl->rx_counter = 0;
  389. wl->rx_handled = 0;
  390. wl->rx_current_buffer = 0;
  391. wl->rx_last_id = 0;
  392. wl->next_tx_complete = 0;
  393. wl->elp = false;
  394. wl->psm = 0;
  395. wl->tx_queue_stopped = false;
  396. wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
  397. wl->channel = WL1251_DEFAULT_CHANNEL;
  398. wl1251_debugfs_reset(wl);
  399. mutex_unlock(&wl->mutex);
  400. }
  401. static int wl1251_op_add_interface(struct ieee80211_hw *hw,
  402. struct ieee80211_vif *vif)
  403. {
  404. struct wl1251 *wl = hw->priv;
  405. int ret = 0;
  406. wl1251_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
  407. vif->type, vif->addr);
  408. mutex_lock(&wl->mutex);
  409. if (wl->vif) {
  410. ret = -EBUSY;
  411. goto out;
  412. }
  413. wl->vif = vif;
  414. switch (vif->type) {
  415. case NL80211_IFTYPE_STATION:
  416. wl->bss_type = BSS_TYPE_STA_BSS;
  417. break;
  418. case NL80211_IFTYPE_ADHOC:
  419. wl->bss_type = BSS_TYPE_IBSS;
  420. break;
  421. default:
  422. ret = -EOPNOTSUPP;
  423. goto out;
  424. }
  425. if (memcmp(wl->mac_addr, vif->addr, ETH_ALEN)) {
  426. memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
  427. SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
  428. ret = wl1251_acx_station_id(wl);
  429. if (ret < 0)
  430. goto out;
  431. }
  432. out:
  433. mutex_unlock(&wl->mutex);
  434. return ret;
  435. }
  436. static void wl1251_op_remove_interface(struct ieee80211_hw *hw,
  437. struct ieee80211_vif *vif)
  438. {
  439. struct wl1251 *wl = hw->priv;
  440. mutex_lock(&wl->mutex);
  441. wl1251_debug(DEBUG_MAC80211, "mac80211 remove interface");
  442. wl->vif = NULL;
  443. mutex_unlock(&wl->mutex);
  444. }
  445. static int wl1251_op_config(struct ieee80211_hw *hw, u32 changed)
  446. {
  447. struct wl1251 *wl = hw->priv;
  448. struct ieee80211_conf *conf = &hw->conf;
  449. int channel, ret = 0;
  450. channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
  451. wl1251_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
  452. channel,
  453. conf->flags & IEEE80211_CONF_PS ? "on" : "off",
  454. conf->power_level);
  455. mutex_lock(&wl->mutex);
  456. ret = wl1251_ps_elp_wakeup(wl);
  457. if (ret < 0)
  458. goto out;
  459. if (channel != wl->channel) {
  460. wl->channel = channel;
  461. ret = wl1251_join(wl, wl->bss_type, wl->channel,
  462. wl->beacon_int, wl->dtim_period);
  463. if (ret < 0)
  464. goto out_sleep;
  465. }
  466. if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
  467. wl1251_debug(DEBUG_PSM, "psm enabled");
  468. wl->psm_requested = true;
  469. /*
  470. * We enter PSM only if we're already associated.
  471. * If we're not, we'll enter it when joining an SSID,
  472. * through the bss_info_changed() hook.
  473. */
  474. ret = wl1251_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
  475. if (ret < 0)
  476. goto out_sleep;
  477. } else if (!(conf->flags & IEEE80211_CONF_PS) &&
  478. wl->psm_requested) {
  479. wl1251_debug(DEBUG_PSM, "psm disabled");
  480. wl->psm_requested = false;
  481. if (wl->psm) {
  482. ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
  483. if (ret < 0)
  484. goto out_sleep;
  485. }
  486. }
  487. if (conf->power_level != wl->power_level) {
  488. ret = wl1251_acx_tx_power(wl, conf->power_level);
  489. if (ret < 0)
  490. goto out_sleep;
  491. wl->power_level = conf->power_level;
  492. }
  493. out_sleep:
  494. wl1251_ps_elp_sleep(wl);
  495. out:
  496. mutex_unlock(&wl->mutex);
  497. return ret;
  498. }
  499. #define WL1251_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
  500. FIF_ALLMULTI | \
  501. FIF_FCSFAIL | \
  502. FIF_BCN_PRBRESP_PROMISC | \
  503. FIF_CONTROL | \
  504. FIF_OTHER_BSS)
  505. static void wl1251_op_configure_filter(struct ieee80211_hw *hw,
  506. unsigned int changed,
  507. unsigned int *total,u64 multicast)
  508. {
  509. struct wl1251 *wl = hw->priv;
  510. wl1251_debug(DEBUG_MAC80211, "mac80211 configure filter");
  511. *total &= WL1251_SUPPORTED_FILTERS;
  512. changed &= WL1251_SUPPORTED_FILTERS;
  513. if (changed == 0)
  514. /* no filters which we support changed */
  515. return;
  516. /* FIXME: wl->rx_config and wl->rx_filter are not protected */
  517. wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
  518. wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
  519. if (*total & FIF_PROMISC_IN_BSS) {
  520. wl->rx_config |= CFG_BSSID_FILTER_EN;
  521. wl->rx_config |= CFG_RX_ALL_GOOD;
  522. }
  523. if (*total & FIF_ALLMULTI)
  524. /*
  525. * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
  526. * all multicast frames
  527. */
  528. wl->rx_config &= ~CFG_MC_FILTER_EN;
  529. if (*total & FIF_FCSFAIL)
  530. wl->rx_filter |= CFG_RX_FCS_ERROR;
  531. if (*total & FIF_BCN_PRBRESP_PROMISC) {
  532. wl->rx_config &= ~CFG_BSSID_FILTER_EN;
  533. wl->rx_config &= ~CFG_SSID_FILTER_EN;
  534. }
  535. if (*total & FIF_CONTROL)
  536. wl->rx_filter |= CFG_RX_CTL_EN;
  537. if (*total & FIF_OTHER_BSS)
  538. wl->rx_filter &= ~CFG_BSSID_FILTER_EN;
  539. /*
  540. * FIXME: workqueues need to be properly cancelled on stop(), for
  541. * now let's just disable changing the filter settings. They will
  542. * be updated any on config().
  543. */
  544. /* schedule_work(&wl->filter_work); */
  545. }
  546. /* HW encryption */
  547. static int wl1251_set_key_type(struct wl1251 *wl,
  548. struct wl1251_cmd_set_keys *key,
  549. enum set_key_cmd cmd,
  550. struct ieee80211_key_conf *mac80211_key,
  551. const u8 *addr)
  552. {
  553. switch (mac80211_key->alg) {
  554. case ALG_WEP:
  555. if (is_broadcast_ether_addr(addr))
  556. key->key_type = KEY_WEP_DEFAULT;
  557. else
  558. key->key_type = KEY_WEP_ADDR;
  559. mac80211_key->hw_key_idx = mac80211_key->keyidx;
  560. break;
  561. case ALG_TKIP:
  562. if (is_broadcast_ether_addr(addr))
  563. key->key_type = KEY_TKIP_MIC_GROUP;
  564. else
  565. key->key_type = KEY_TKIP_MIC_PAIRWISE;
  566. mac80211_key->hw_key_idx = mac80211_key->keyidx;
  567. break;
  568. case ALG_CCMP:
  569. if (is_broadcast_ether_addr(addr))
  570. key->key_type = KEY_AES_GROUP;
  571. else
  572. key->key_type = KEY_AES_PAIRWISE;
  573. mac80211_key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  574. break;
  575. default:
  576. wl1251_error("Unknown key algo 0x%x", mac80211_key->alg);
  577. return -EOPNOTSUPP;
  578. }
  579. return 0;
  580. }
  581. static int wl1251_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  582. struct ieee80211_vif *vif,
  583. struct ieee80211_sta *sta,
  584. struct ieee80211_key_conf *key)
  585. {
  586. struct wl1251 *wl = hw->priv;
  587. struct wl1251_cmd_set_keys *wl_cmd;
  588. const u8 *addr;
  589. int ret;
  590. static const u8 bcast_addr[ETH_ALEN] =
  591. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  592. wl1251_debug(DEBUG_MAC80211, "mac80211 set key");
  593. wl_cmd = kzalloc(sizeof(*wl_cmd), GFP_KERNEL);
  594. if (!wl_cmd) {
  595. ret = -ENOMEM;
  596. goto out;
  597. }
  598. addr = sta ? sta->addr : bcast_addr;
  599. wl1251_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
  600. wl1251_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
  601. wl1251_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
  602. key->alg, key->keyidx, key->keylen, key->flags);
  603. wl1251_dump(DEBUG_CRYPT, "KEY: ", key->key, key->keylen);
  604. if (is_zero_ether_addr(addr)) {
  605. /* We dont support TX only encryption */
  606. ret = -EOPNOTSUPP;
  607. goto out;
  608. }
  609. mutex_lock(&wl->mutex);
  610. ret = wl1251_ps_elp_wakeup(wl);
  611. if (ret < 0)
  612. goto out_unlock;
  613. switch (cmd) {
  614. case SET_KEY:
  615. wl_cmd->key_action = KEY_ADD_OR_REPLACE;
  616. break;
  617. case DISABLE_KEY:
  618. wl_cmd->key_action = KEY_REMOVE;
  619. break;
  620. default:
  621. wl1251_error("Unsupported key cmd 0x%x", cmd);
  622. break;
  623. }
  624. ret = wl1251_set_key_type(wl, wl_cmd, cmd, key, addr);
  625. if (ret < 0) {
  626. wl1251_error("Set KEY type failed");
  627. goto out_sleep;
  628. }
  629. if (wl_cmd->key_type != KEY_WEP_DEFAULT)
  630. memcpy(wl_cmd->addr, addr, ETH_ALEN);
  631. if ((wl_cmd->key_type == KEY_TKIP_MIC_GROUP) ||
  632. (wl_cmd->key_type == KEY_TKIP_MIC_PAIRWISE)) {
  633. /*
  634. * We get the key in the following form:
  635. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  636. * but the target is expecting:
  637. * TKIP - RX MIC - TX MIC
  638. */
  639. memcpy(wl_cmd->key, key->key, 16);
  640. memcpy(wl_cmd->key + 16, key->key + 24, 8);
  641. memcpy(wl_cmd->key + 24, key->key + 16, 8);
  642. } else {
  643. memcpy(wl_cmd->key, key->key, key->keylen);
  644. }
  645. wl_cmd->key_size = key->keylen;
  646. wl_cmd->id = key->keyidx;
  647. wl_cmd->ssid_profile = 0;
  648. wl1251_dump(DEBUG_CRYPT, "TARGET KEY: ", wl_cmd, sizeof(*wl_cmd));
  649. ret = wl1251_cmd_send(wl, CMD_SET_KEYS, wl_cmd, sizeof(*wl_cmd));
  650. if (ret < 0) {
  651. wl1251_warning("could not set keys");
  652. goto out_sleep;
  653. }
  654. out_sleep:
  655. wl1251_ps_elp_sleep(wl);
  656. out_unlock:
  657. mutex_unlock(&wl->mutex);
  658. out:
  659. kfree(wl_cmd);
  660. return ret;
  661. }
  662. static int wl1251_build_basic_rates(char *rates)
  663. {
  664. u8 index = 0;
  665. rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
  666. rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
  667. rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
  668. rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
  669. return index;
  670. }
  671. static int wl1251_build_extended_rates(char *rates)
  672. {
  673. u8 index = 0;
  674. rates[index++] = IEEE80211_OFDM_RATE_6MB;
  675. rates[index++] = IEEE80211_OFDM_RATE_9MB;
  676. rates[index++] = IEEE80211_OFDM_RATE_12MB;
  677. rates[index++] = IEEE80211_OFDM_RATE_18MB;
  678. rates[index++] = IEEE80211_OFDM_RATE_24MB;
  679. rates[index++] = IEEE80211_OFDM_RATE_36MB;
  680. rates[index++] = IEEE80211_OFDM_RATE_48MB;
  681. rates[index++] = IEEE80211_OFDM_RATE_54MB;
  682. return index;
  683. }
  684. static int wl1251_build_probe_req(struct wl1251 *wl, u8 *ssid, size_t ssid_len)
  685. {
  686. struct wl12xx_probe_req_template template;
  687. struct wl12xx_ie_rates *rates;
  688. char *ptr;
  689. u16 size;
  690. ptr = (char *)&template;
  691. size = sizeof(struct ieee80211_header);
  692. memset(template.header.da, 0xff, ETH_ALEN);
  693. memset(template.header.bssid, 0xff, ETH_ALEN);
  694. memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
  695. template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  696. /* IEs */
  697. /* SSID */
  698. template.ssid.header.id = WLAN_EID_SSID;
  699. template.ssid.header.len = ssid_len;
  700. if (ssid_len && ssid)
  701. memcpy(template.ssid.ssid, ssid, ssid_len);
  702. size += sizeof(struct wl12xx_ie_header) + ssid_len;
  703. ptr += size;
  704. /* Basic Rates */
  705. rates = (struct wl12xx_ie_rates *)ptr;
  706. rates->header.id = WLAN_EID_SUPP_RATES;
  707. rates->header.len = wl1251_build_basic_rates(rates->rates);
  708. size += sizeof(struct wl12xx_ie_header) + rates->header.len;
  709. ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
  710. /* Extended rates */
  711. rates = (struct wl12xx_ie_rates *)ptr;
  712. rates->header.id = WLAN_EID_EXT_SUPP_RATES;
  713. rates->header.len = wl1251_build_extended_rates(rates->rates);
  714. size += sizeof(struct wl12xx_ie_header) + rates->header.len;
  715. wl1251_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
  716. return wl1251_cmd_template_set(wl, CMD_PROBE_REQ, &template,
  717. size);
  718. }
  719. static int wl1251_hw_scan(struct wl1251 *wl, u8 *ssid, size_t len,
  720. u8 active_scan, u8 high_prio, u8 num_channels,
  721. u8 probe_requests)
  722. {
  723. struct wl1251_cmd_trigger_scan_to *trigger = NULL;
  724. struct cmd_scan *params = NULL;
  725. int i, ret;
  726. u16 scan_options = 0;
  727. if (wl->scanning)
  728. return -EINVAL;
  729. params = kzalloc(sizeof(*params), GFP_KERNEL);
  730. if (!params)
  731. return -ENOMEM;
  732. params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
  733. params->params.rx_filter_options =
  734. cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
  735. /* High priority scan */
  736. if (!active_scan)
  737. scan_options |= SCAN_PASSIVE;
  738. if (high_prio)
  739. scan_options |= SCAN_PRIORITY_HIGH;
  740. params->params.scan_options = scan_options;
  741. params->params.num_channels = num_channels;
  742. params->params.num_probe_requests = probe_requests;
  743. params->params.tx_rate = cpu_to_le16(1 << 1); /* 2 Mbps */
  744. params->params.tid_trigger = 0;
  745. for (i = 0; i < num_channels; i++) {
  746. params->channels[i].min_duration = cpu_to_le32(30000);
  747. params->channels[i].max_duration = cpu_to_le32(60000);
  748. memset(&params->channels[i].bssid_lsb, 0xff, 4);
  749. memset(&params->channels[i].bssid_msb, 0xff, 2);
  750. params->channels[i].early_termination = 0;
  751. params->channels[i].tx_power_att = 0;
  752. params->channels[i].channel = i + 1;
  753. memset(params->channels[i].pad, 0, 3);
  754. }
  755. for (i = num_channels; i < SCAN_MAX_NUM_OF_CHANNELS; i++)
  756. memset(&params->channels[i], 0,
  757. sizeof(struct basic_scan_channel_parameters));
  758. if (len && ssid) {
  759. params->params.ssid_len = len;
  760. memcpy(params->params.ssid, ssid, len);
  761. } else {
  762. params->params.ssid_len = 0;
  763. memset(params->params.ssid, 0, 32);
  764. }
  765. ret = wl1251_build_probe_req(wl, ssid, len);
  766. if (ret < 0) {
  767. wl1251_error("PROBE request template failed");
  768. goto out;
  769. }
  770. trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
  771. if (!trigger)
  772. goto out;
  773. trigger->timeout = 0;
  774. ret = wl1251_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
  775. sizeof(*trigger));
  776. if (ret < 0) {
  777. wl1251_error("trigger scan to failed for hw scan");
  778. goto out;
  779. }
  780. wl1251_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
  781. wl->scanning = true;
  782. ret = wl1251_cmd_send(wl, CMD_SCAN, params, sizeof(*params));
  783. if (ret < 0)
  784. wl1251_error("SCAN failed");
  785. wl1251_mem_read(wl, wl->cmd_box_addr, params, sizeof(*params));
  786. if (params->header.status != CMD_STATUS_SUCCESS) {
  787. wl1251_error("TEST command answer error: %d",
  788. params->header.status);
  789. wl->scanning = false;
  790. ret = -EIO;
  791. goto out;
  792. }
  793. out:
  794. kfree(params);
  795. return ret;
  796. }
  797. static int wl1251_op_hw_scan(struct ieee80211_hw *hw,
  798. struct cfg80211_scan_request *req)
  799. {
  800. struct wl1251 *wl = hw->priv;
  801. int ret;
  802. u8 *ssid = NULL;
  803. size_t ssid_len = 0;
  804. wl1251_debug(DEBUG_MAC80211, "mac80211 hw scan");
  805. if (req->n_ssids) {
  806. ssid = req->ssids[0].ssid;
  807. ssid_len = req->ssids[0].ssid_len;
  808. }
  809. mutex_lock(&wl->mutex);
  810. ret = wl1251_ps_elp_wakeup(wl);
  811. if (ret < 0)
  812. goto out;
  813. ret = wl1251_hw_scan(hw->priv, ssid, ssid_len, 1, 0, 13, 3);
  814. wl1251_ps_elp_sleep(wl);
  815. out:
  816. mutex_unlock(&wl->mutex);
  817. return ret;
  818. }
  819. static int wl1251_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  820. {
  821. struct wl1251 *wl = hw->priv;
  822. int ret;
  823. mutex_lock(&wl->mutex);
  824. ret = wl1251_ps_elp_wakeup(wl);
  825. if (ret < 0)
  826. goto out;
  827. ret = wl1251_acx_rts_threshold(wl, (u16) value);
  828. if (ret < 0)
  829. wl1251_warning("wl1251_op_set_rts_threshold failed: %d", ret);
  830. wl1251_ps_elp_sleep(wl);
  831. out:
  832. mutex_unlock(&wl->mutex);
  833. return ret;
  834. }
  835. static void wl1251_op_bss_info_changed(struct ieee80211_hw *hw,
  836. struct ieee80211_vif *vif,
  837. struct ieee80211_bss_conf *bss_conf,
  838. u32 changed)
  839. {
  840. enum wl1251_cmd_ps_mode mode;
  841. struct wl1251 *wl = hw->priv;
  842. struct sk_buff *beacon, *skb;
  843. int ret;
  844. wl1251_debug(DEBUG_MAC80211, "mac80211 bss info changed");
  845. mutex_lock(&wl->mutex);
  846. ret = wl1251_ps_elp_wakeup(wl);
  847. if (ret < 0)
  848. goto out;
  849. if (changed & BSS_CHANGED_BSSID) {
  850. memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
  851. skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
  852. if (!skb)
  853. goto out_sleep;
  854. ret = wl1251_cmd_template_set(wl, CMD_NULL_DATA,
  855. skb->data, skb->len);
  856. dev_kfree_skb(skb);
  857. if (ret < 0)
  858. goto out;
  859. if (wl->bss_type != BSS_TYPE_IBSS) {
  860. ret = wl1251_join(wl, wl->bss_type, wl->channel,
  861. wl->beacon_int, wl->dtim_period);
  862. if (ret < 0)
  863. goto out_sleep;
  864. }
  865. }
  866. if (changed & BSS_CHANGED_ASSOC) {
  867. if (bss_conf->assoc) {
  868. wl->beacon_int = bss_conf->beacon_int;
  869. wl->dtim_period = bss_conf->dtim_period;
  870. ret = wl1251_acx_wr_tbtt_and_dtim(wl, wl->beacon_int,
  871. wl->dtim_period);
  872. wl->aid = bss_conf->aid;
  873. skb = ieee80211_pspoll_get(wl->hw, wl->vif);
  874. if (!skb)
  875. goto out_sleep;
  876. ret = wl1251_cmd_template_set(wl, CMD_PS_POLL,
  877. skb->data,
  878. skb->len);
  879. dev_kfree_skb(skb);
  880. if (ret < 0)
  881. goto out_sleep;
  882. ret = wl1251_acx_aid(wl, wl->aid);
  883. if (ret < 0)
  884. goto out_sleep;
  885. /* If we want to go in PSM but we're not there yet */
  886. if (wl->psm_requested && !wl->psm) {
  887. mode = STATION_POWER_SAVE_MODE;
  888. ret = wl1251_ps_set_mode(wl, mode);
  889. if (ret < 0)
  890. goto out_sleep;
  891. }
  892. } else {
  893. /* use defaults when not associated */
  894. wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
  895. wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
  896. }
  897. }
  898. if (changed & BSS_CHANGED_ERP_SLOT) {
  899. if (bss_conf->use_short_slot)
  900. ret = wl1251_acx_slot(wl, SLOT_TIME_SHORT);
  901. else
  902. ret = wl1251_acx_slot(wl, SLOT_TIME_LONG);
  903. if (ret < 0) {
  904. wl1251_warning("Set slot time failed %d", ret);
  905. goto out_sleep;
  906. }
  907. }
  908. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  909. if (bss_conf->use_short_preamble)
  910. wl1251_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
  911. else
  912. wl1251_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
  913. }
  914. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  915. if (bss_conf->use_cts_prot)
  916. ret = wl1251_acx_cts_protect(wl, CTSPROTECT_ENABLE);
  917. else
  918. ret = wl1251_acx_cts_protect(wl, CTSPROTECT_DISABLE);
  919. if (ret < 0) {
  920. wl1251_warning("Set ctsprotect failed %d", ret);
  921. goto out;
  922. }
  923. }
  924. if (changed & BSS_CHANGED_BEACON) {
  925. beacon = ieee80211_beacon_get(hw, vif);
  926. ret = wl1251_cmd_template_set(wl, CMD_BEACON, beacon->data,
  927. beacon->len);
  928. if (ret < 0) {
  929. dev_kfree_skb(beacon);
  930. goto out;
  931. }
  932. ret = wl1251_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
  933. beacon->len);
  934. dev_kfree_skb(beacon);
  935. if (ret < 0)
  936. goto out;
  937. ret = wl1251_join(wl, wl->bss_type, wl->beacon_int,
  938. wl->channel, wl->dtim_period);
  939. if (ret < 0)
  940. goto out;
  941. }
  942. out_sleep:
  943. wl1251_ps_elp_sleep(wl);
  944. out:
  945. mutex_unlock(&wl->mutex);
  946. }
  947. /* can't be const, mac80211 writes to this */
  948. static struct ieee80211_rate wl1251_rates[] = {
  949. { .bitrate = 10,
  950. .hw_value = 0x1,
  951. .hw_value_short = 0x1, },
  952. { .bitrate = 20,
  953. .hw_value = 0x2,
  954. .hw_value_short = 0x2,
  955. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  956. { .bitrate = 55,
  957. .hw_value = 0x4,
  958. .hw_value_short = 0x4,
  959. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  960. { .bitrate = 110,
  961. .hw_value = 0x20,
  962. .hw_value_short = 0x20,
  963. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  964. { .bitrate = 60,
  965. .hw_value = 0x8,
  966. .hw_value_short = 0x8, },
  967. { .bitrate = 90,
  968. .hw_value = 0x10,
  969. .hw_value_short = 0x10, },
  970. { .bitrate = 120,
  971. .hw_value = 0x40,
  972. .hw_value_short = 0x40, },
  973. { .bitrate = 180,
  974. .hw_value = 0x80,
  975. .hw_value_short = 0x80, },
  976. { .bitrate = 240,
  977. .hw_value = 0x200,
  978. .hw_value_short = 0x200, },
  979. { .bitrate = 360,
  980. .hw_value = 0x400,
  981. .hw_value_short = 0x400, },
  982. { .bitrate = 480,
  983. .hw_value = 0x800,
  984. .hw_value_short = 0x800, },
  985. { .bitrate = 540,
  986. .hw_value = 0x1000,
  987. .hw_value_short = 0x1000, },
  988. };
  989. /* can't be const, mac80211 writes to this */
  990. static struct ieee80211_channel wl1251_channels[] = {
  991. { .hw_value = 1, .center_freq = 2412},
  992. { .hw_value = 2, .center_freq = 2417},
  993. { .hw_value = 3, .center_freq = 2422},
  994. { .hw_value = 4, .center_freq = 2427},
  995. { .hw_value = 5, .center_freq = 2432},
  996. { .hw_value = 6, .center_freq = 2437},
  997. { .hw_value = 7, .center_freq = 2442},
  998. { .hw_value = 8, .center_freq = 2447},
  999. { .hw_value = 9, .center_freq = 2452},
  1000. { .hw_value = 10, .center_freq = 2457},
  1001. { .hw_value = 11, .center_freq = 2462},
  1002. { .hw_value = 12, .center_freq = 2467},
  1003. { .hw_value = 13, .center_freq = 2472},
  1004. };
  1005. static int wl1251_op_conf_tx(struct ieee80211_hw *hw, u16 queue,
  1006. const struct ieee80211_tx_queue_params *params)
  1007. {
  1008. struct wl1251 *wl = hw->priv;
  1009. int ret;
  1010. mutex_lock(&wl->mutex);
  1011. wl1251_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
  1012. ret = wl1251_ps_elp_wakeup(wl);
  1013. if (ret < 0)
  1014. goto out;
  1015. ret = wl1251_acx_ac_cfg(wl, wl1251_tx_get_queue(queue),
  1016. params->cw_min, params->cw_max,
  1017. params->aifs, params->txop);
  1018. if (ret < 0)
  1019. goto out_sleep;
  1020. ret = wl1251_acx_tid_cfg(wl, wl1251_tx_get_queue(queue),
  1021. CHANNEL_TYPE_EDCF,
  1022. wl1251_tx_get_queue(queue),
  1023. WL1251_ACX_PS_SCHEME_LEGACY,
  1024. WL1251_ACX_ACK_POLICY_LEGACY);
  1025. if (ret < 0)
  1026. goto out_sleep;
  1027. out_sleep:
  1028. wl1251_ps_elp_sleep(wl);
  1029. out:
  1030. mutex_unlock(&wl->mutex);
  1031. return ret;
  1032. }
  1033. /* can't be const, mac80211 writes to this */
  1034. static struct ieee80211_supported_band wl1251_band_2ghz = {
  1035. .channels = wl1251_channels,
  1036. .n_channels = ARRAY_SIZE(wl1251_channels),
  1037. .bitrates = wl1251_rates,
  1038. .n_bitrates = ARRAY_SIZE(wl1251_rates),
  1039. };
  1040. static const struct ieee80211_ops wl1251_ops = {
  1041. .start = wl1251_op_start,
  1042. .stop = wl1251_op_stop,
  1043. .add_interface = wl1251_op_add_interface,
  1044. .remove_interface = wl1251_op_remove_interface,
  1045. .config = wl1251_op_config,
  1046. .configure_filter = wl1251_op_configure_filter,
  1047. .tx = wl1251_op_tx,
  1048. .set_key = wl1251_op_set_key,
  1049. .hw_scan = wl1251_op_hw_scan,
  1050. .bss_info_changed = wl1251_op_bss_info_changed,
  1051. .set_rts_threshold = wl1251_op_set_rts_threshold,
  1052. .conf_tx = wl1251_op_conf_tx,
  1053. };
  1054. static int wl1251_register_hw(struct wl1251 *wl)
  1055. {
  1056. int ret;
  1057. if (wl->mac80211_registered)
  1058. return 0;
  1059. SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
  1060. ret = ieee80211_register_hw(wl->hw);
  1061. if (ret < 0) {
  1062. wl1251_error("unable to register mac80211 hw: %d", ret);
  1063. return ret;
  1064. }
  1065. wl->mac80211_registered = true;
  1066. wl1251_notice("loaded");
  1067. return 0;
  1068. }
  1069. int wl1251_init_ieee80211(struct wl1251 *wl)
  1070. {
  1071. int ret;
  1072. /* The tx descriptor buffer and the TKIP space */
  1073. wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
  1074. + WL1251_TKIP_IV_SPACE;
  1075. /* unit us */
  1076. /* FIXME: find a proper value */
  1077. wl->hw->channel_change_time = 10000;
  1078. wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
  1079. IEEE80211_HW_NOISE_DBM |
  1080. IEEE80211_HW_SUPPORTS_PS |
  1081. IEEE80211_HW_BEACON_FILTER;
  1082. wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  1083. wl->hw->wiphy->max_scan_ssids = 1;
  1084. wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1251_band_2ghz;
  1085. wl->hw->queues = 4;
  1086. ret = wl1251_register_hw(wl);
  1087. if (ret)
  1088. goto out;
  1089. wl1251_debugfs_init(wl);
  1090. wl1251_notice("initialized");
  1091. ret = 0;
  1092. out:
  1093. return ret;
  1094. }
  1095. EXPORT_SYMBOL_GPL(wl1251_init_ieee80211);
  1096. struct ieee80211_hw *wl1251_alloc_hw(void)
  1097. {
  1098. struct ieee80211_hw *hw;
  1099. struct wl1251 *wl;
  1100. int i;
  1101. static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
  1102. hw = ieee80211_alloc_hw(sizeof(*wl), &wl1251_ops);
  1103. if (!hw) {
  1104. wl1251_error("could not alloc ieee80211_hw");
  1105. return ERR_PTR(-ENOMEM);
  1106. }
  1107. wl = hw->priv;
  1108. memset(wl, 0, sizeof(*wl));
  1109. wl->hw = hw;
  1110. wl->data_in_count = 0;
  1111. skb_queue_head_init(&wl->tx_queue);
  1112. INIT_WORK(&wl->filter_work, wl1251_filter_work);
  1113. INIT_DELAYED_WORK(&wl->elp_work, wl1251_elp_work);
  1114. wl->channel = WL1251_DEFAULT_CHANNEL;
  1115. wl->scanning = false;
  1116. wl->default_key = 0;
  1117. wl->listen_int = 1;
  1118. wl->rx_counter = 0;
  1119. wl->rx_handled = 0;
  1120. wl->rx_current_buffer = 0;
  1121. wl->rx_last_id = 0;
  1122. wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
  1123. wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
  1124. wl->elp = false;
  1125. wl->psm = 0;
  1126. wl->psm_requested = false;
  1127. wl->tx_queue_stopped = false;
  1128. wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
  1129. wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
  1130. wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
  1131. wl->vif = NULL;
  1132. for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
  1133. wl->tx_frames[i] = NULL;
  1134. wl->next_tx_complete = 0;
  1135. INIT_WORK(&wl->irq_work, wl1251_irq_work);
  1136. INIT_WORK(&wl->tx_work, wl1251_tx_work);
  1137. /*
  1138. * In case our MAC address is not correctly set,
  1139. * we use a random but Nokia MAC.
  1140. */
  1141. memcpy(wl->mac_addr, nokia_oui, 3);
  1142. get_random_bytes(wl->mac_addr + 3, 3);
  1143. wl->state = WL1251_STATE_OFF;
  1144. mutex_init(&wl->mutex);
  1145. wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
  1146. wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
  1147. wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
  1148. if (!wl->rx_descriptor) {
  1149. wl1251_error("could not allocate memory for rx descriptor");
  1150. ieee80211_free_hw(hw);
  1151. return ERR_PTR(-ENOMEM);
  1152. }
  1153. return hw;
  1154. }
  1155. EXPORT_SYMBOL_GPL(wl1251_alloc_hw);
  1156. int wl1251_free_hw(struct wl1251 *wl)
  1157. {
  1158. ieee80211_unregister_hw(wl->hw);
  1159. wl1251_debugfs_exit(wl);
  1160. kfree(wl->target_mem_map);
  1161. kfree(wl->data_path);
  1162. vfree(wl->fw);
  1163. wl->fw = NULL;
  1164. kfree(wl->nvs);
  1165. wl->nvs = NULL;
  1166. kfree(wl->rx_descriptor);
  1167. wl->rx_descriptor = NULL;
  1168. ieee80211_free_hw(wl->hw);
  1169. return 0;
  1170. }
  1171. EXPORT_SYMBOL_GPL(wl1251_free_hw);
  1172. MODULE_DESCRIPTION("TI wl1251 Wireles LAN Driver Core");
  1173. MODULE_LICENSE("GPL");
  1174. MODULE_AUTHOR("Kalle Valo <kalle.valo@nokia.com>");
  1175. MODULE_ALIAS("spi:wl1251");
  1176. MODULE_FIRMWARE(WL1251_FW_NAME);