wl1251_main.c 31 KB

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