wl1251_main.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  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. ieee80211_stop_queues(wl->hw);
  319. /*
  320. * FIXME: this is racy, the variable is not properly
  321. * protected. Maybe fix this by removing the stupid
  322. * variable altogether and checking the real queue state?
  323. */
  324. wl->tx_queue_stopped = true;
  325. }
  326. return NETDEV_TX_OK;
  327. }
  328. static int wl1251_op_start(struct ieee80211_hw *hw)
  329. {
  330. struct wl1251 *wl = hw->priv;
  331. int ret = 0;
  332. wl1251_debug(DEBUG_MAC80211, "mac80211 start");
  333. mutex_lock(&wl->mutex);
  334. if (wl->state != WL1251_STATE_OFF) {
  335. wl1251_error("cannot start because not in off state: %d",
  336. wl->state);
  337. ret = -EBUSY;
  338. goto out;
  339. }
  340. ret = wl1251_chip_wakeup(wl);
  341. if (ret < 0)
  342. goto out;
  343. ret = wl1251_boot(wl);
  344. if (ret < 0)
  345. goto out;
  346. ret = wl1251_hw_init(wl);
  347. if (ret < 0)
  348. goto out;
  349. ret = wl1251_acx_station_id(wl);
  350. if (ret < 0)
  351. goto out;
  352. wl->state = WL1251_STATE_ON;
  353. wl1251_info("firmware booted (%s)", wl->fw_ver);
  354. out:
  355. if (ret < 0)
  356. wl1251_power_off(wl);
  357. mutex_unlock(&wl->mutex);
  358. return ret;
  359. }
  360. static void wl1251_op_stop(struct ieee80211_hw *hw)
  361. {
  362. struct wl1251 *wl = hw->priv;
  363. wl1251_info("down");
  364. wl1251_debug(DEBUG_MAC80211, "mac80211 stop");
  365. mutex_lock(&wl->mutex);
  366. WARN_ON(wl->state != WL1251_STATE_ON);
  367. if (wl->scanning) {
  368. mutex_unlock(&wl->mutex);
  369. ieee80211_scan_completed(wl->hw, true);
  370. mutex_lock(&wl->mutex);
  371. wl->scanning = false;
  372. }
  373. wl->state = WL1251_STATE_OFF;
  374. wl1251_disable_interrupts(wl);
  375. mutex_unlock(&wl->mutex);
  376. cancel_work_sync(&wl->irq_work);
  377. cancel_work_sync(&wl->tx_work);
  378. cancel_work_sync(&wl->filter_work);
  379. mutex_lock(&wl->mutex);
  380. /* let's notify MAC80211 about the remaining pending TX frames */
  381. wl1251_tx_flush(wl);
  382. wl1251_power_off(wl);
  383. memset(wl->bssid, 0, ETH_ALEN);
  384. wl->listen_int = 1;
  385. wl->bss_type = MAX_BSS_TYPE;
  386. wl->data_in_count = 0;
  387. wl->rx_counter = 0;
  388. wl->rx_handled = 0;
  389. wl->rx_current_buffer = 0;
  390. wl->rx_last_id = 0;
  391. wl->next_tx_complete = 0;
  392. wl->elp = false;
  393. wl->psm = 0;
  394. wl->tx_queue_stopped = false;
  395. wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
  396. wl->channel = WL1251_DEFAULT_CHANNEL;
  397. wl1251_debugfs_reset(wl);
  398. mutex_unlock(&wl->mutex);
  399. }
  400. static int wl1251_op_add_interface(struct ieee80211_hw *hw,
  401. struct ieee80211_if_init_conf *conf)
  402. {
  403. struct wl1251 *wl = hw->priv;
  404. int ret = 0;
  405. wl1251_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
  406. conf->type, conf->mac_addr);
  407. mutex_lock(&wl->mutex);
  408. if (wl->vif) {
  409. ret = -EBUSY;
  410. goto out;
  411. }
  412. wl->vif = conf->vif;
  413. switch (conf->type) {
  414. case NL80211_IFTYPE_STATION:
  415. wl->bss_type = BSS_TYPE_STA_BSS;
  416. break;
  417. case NL80211_IFTYPE_ADHOC:
  418. wl->bss_type = BSS_TYPE_IBSS;
  419. break;
  420. default:
  421. ret = -EOPNOTSUPP;
  422. goto out;
  423. }
  424. if (memcmp(wl->mac_addr, conf->mac_addr, ETH_ALEN)) {
  425. memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
  426. SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
  427. ret = wl1251_acx_station_id(wl);
  428. if (ret < 0)
  429. goto out;
  430. }
  431. out:
  432. mutex_unlock(&wl->mutex);
  433. return ret;
  434. }
  435. static void wl1251_op_remove_interface(struct ieee80211_hw *hw,
  436. struct ieee80211_if_init_conf *conf)
  437. {
  438. struct wl1251 *wl = hw->priv;
  439. mutex_lock(&wl->mutex);
  440. wl1251_debug(DEBUG_MAC80211, "mac80211 remove interface");
  441. wl->vif = NULL;
  442. mutex_unlock(&wl->mutex);
  443. }
  444. static int wl1251_build_null_data(struct wl1251 *wl)
  445. {
  446. struct wl12xx_null_data_template template;
  447. if (!is_zero_ether_addr(wl->bssid)) {
  448. memcpy(template.header.da, wl->bssid, ETH_ALEN);
  449. memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
  450. } else {
  451. memset(template.header.da, 0xff, ETH_ALEN);
  452. memset(template.header.bssid, 0xff, ETH_ALEN);
  453. }
  454. memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
  455. template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
  456. IEEE80211_STYPE_NULLFUNC |
  457. IEEE80211_FCTL_TODS);
  458. return wl1251_cmd_template_set(wl, CMD_NULL_DATA, &template,
  459. sizeof(template));
  460. }
  461. static int wl1251_build_ps_poll(struct wl1251 *wl, u16 aid)
  462. {
  463. struct wl12xx_ps_poll_template template;
  464. memcpy(template.bssid, wl->bssid, ETH_ALEN);
  465. memcpy(template.ta, wl->mac_addr, ETH_ALEN);
  466. /* aid in PS-Poll has its two MSBs each set to 1 */
  467. template.aid = cpu_to_le16(1 << 15 | 1 << 14 | aid);
  468. template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
  469. return wl1251_cmd_template_set(wl, CMD_PS_POLL, &template,
  470. sizeof(template));
  471. }
  472. static int wl1251_op_config(struct ieee80211_hw *hw, u32 changed)
  473. {
  474. struct wl1251 *wl = hw->priv;
  475. struct ieee80211_conf *conf = &hw->conf;
  476. int channel, ret = 0;
  477. channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
  478. wl1251_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
  479. channel,
  480. conf->flags & IEEE80211_CONF_PS ? "on" : "off",
  481. conf->power_level);
  482. mutex_lock(&wl->mutex);
  483. ret = wl1251_ps_elp_wakeup(wl);
  484. if (ret < 0)
  485. goto out;
  486. if (channel != wl->channel) {
  487. wl->channel = channel;
  488. ret = wl1251_join(wl, wl->bss_type, wl->channel,
  489. wl->beacon_int, wl->dtim_period);
  490. if (ret < 0)
  491. goto out_sleep;
  492. }
  493. if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
  494. wl1251_debug(DEBUG_PSM, "psm enabled");
  495. wl->psm_requested = true;
  496. /*
  497. * We enter PSM only if we're already associated.
  498. * If we're not, we'll enter it when joining an SSID,
  499. * through the bss_info_changed() hook.
  500. */
  501. ret = wl1251_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
  502. } else if (!(conf->flags & IEEE80211_CONF_PS) &&
  503. wl->psm_requested) {
  504. wl1251_debug(DEBUG_PSM, "psm disabled");
  505. wl->psm_requested = false;
  506. if (wl->psm)
  507. ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
  508. }
  509. if (conf->power_level != wl->power_level) {
  510. ret = wl1251_acx_tx_power(wl, conf->power_level);
  511. if (ret < 0)
  512. goto out;
  513. wl->power_level = conf->power_level;
  514. }
  515. out_sleep:
  516. wl1251_ps_elp_sleep(wl);
  517. out:
  518. mutex_unlock(&wl->mutex);
  519. return ret;
  520. }
  521. #define WL1251_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
  522. FIF_ALLMULTI | \
  523. FIF_FCSFAIL | \
  524. FIF_BCN_PRBRESP_PROMISC | \
  525. FIF_CONTROL | \
  526. FIF_OTHER_BSS)
  527. static void wl1251_op_configure_filter(struct ieee80211_hw *hw,
  528. unsigned int changed,
  529. unsigned int *total,u64 multicast)
  530. {
  531. struct wl1251 *wl = hw->priv;
  532. wl1251_debug(DEBUG_MAC80211, "mac80211 configure filter");
  533. *total &= WL1251_SUPPORTED_FILTERS;
  534. changed &= WL1251_SUPPORTED_FILTERS;
  535. if (changed == 0)
  536. /* no filters which we support changed */
  537. return;
  538. /* FIXME: wl->rx_config and wl->rx_filter are not protected */
  539. wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
  540. wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
  541. if (*total & FIF_PROMISC_IN_BSS) {
  542. wl->rx_config |= CFG_BSSID_FILTER_EN;
  543. wl->rx_config |= CFG_RX_ALL_GOOD;
  544. }
  545. if (*total & FIF_ALLMULTI)
  546. /*
  547. * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
  548. * all multicast frames
  549. */
  550. wl->rx_config &= ~CFG_MC_FILTER_EN;
  551. if (*total & FIF_FCSFAIL)
  552. wl->rx_filter |= CFG_RX_FCS_ERROR;
  553. if (*total & FIF_BCN_PRBRESP_PROMISC) {
  554. wl->rx_config &= ~CFG_BSSID_FILTER_EN;
  555. wl->rx_config &= ~CFG_SSID_FILTER_EN;
  556. }
  557. if (*total & FIF_CONTROL)
  558. wl->rx_filter |= CFG_RX_CTL_EN;
  559. if (*total & FIF_OTHER_BSS)
  560. wl->rx_filter &= ~CFG_BSSID_FILTER_EN;
  561. /*
  562. * FIXME: workqueues need to be properly cancelled on stop(), for
  563. * now let's just disable changing the filter settings. They will
  564. * be updated any on config().
  565. */
  566. /* schedule_work(&wl->filter_work); */
  567. }
  568. /* HW encryption */
  569. static int wl1251_set_key_type(struct wl1251 *wl,
  570. struct wl1251_cmd_set_keys *key,
  571. enum set_key_cmd cmd,
  572. struct ieee80211_key_conf *mac80211_key,
  573. const u8 *addr)
  574. {
  575. switch (mac80211_key->alg) {
  576. case ALG_WEP:
  577. if (is_broadcast_ether_addr(addr))
  578. key->key_type = KEY_WEP_DEFAULT;
  579. else
  580. key->key_type = KEY_WEP_ADDR;
  581. mac80211_key->hw_key_idx = mac80211_key->keyidx;
  582. break;
  583. case ALG_TKIP:
  584. if (is_broadcast_ether_addr(addr))
  585. key->key_type = KEY_TKIP_MIC_GROUP;
  586. else
  587. key->key_type = KEY_TKIP_MIC_PAIRWISE;
  588. mac80211_key->hw_key_idx = mac80211_key->keyidx;
  589. break;
  590. case ALG_CCMP:
  591. if (is_broadcast_ether_addr(addr))
  592. key->key_type = KEY_AES_GROUP;
  593. else
  594. key->key_type = KEY_AES_PAIRWISE;
  595. mac80211_key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  596. break;
  597. default:
  598. wl1251_error("Unknown key algo 0x%x", mac80211_key->alg);
  599. return -EOPNOTSUPP;
  600. }
  601. return 0;
  602. }
  603. static int wl1251_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  604. struct ieee80211_vif *vif,
  605. struct ieee80211_sta *sta,
  606. struct ieee80211_key_conf *key)
  607. {
  608. struct wl1251 *wl = hw->priv;
  609. struct wl1251_cmd_set_keys *wl_cmd;
  610. const u8 *addr;
  611. int ret;
  612. static const u8 bcast_addr[ETH_ALEN] =
  613. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  614. wl1251_debug(DEBUG_MAC80211, "mac80211 set key");
  615. wl_cmd = kzalloc(sizeof(*wl_cmd), GFP_KERNEL);
  616. if (!wl_cmd) {
  617. ret = -ENOMEM;
  618. goto out;
  619. }
  620. addr = sta ? sta->addr : bcast_addr;
  621. wl1251_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
  622. wl1251_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
  623. wl1251_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
  624. key->alg, key->keyidx, key->keylen, key->flags);
  625. wl1251_dump(DEBUG_CRYPT, "KEY: ", key->key, key->keylen);
  626. if (is_zero_ether_addr(addr)) {
  627. /* We dont support TX only encryption */
  628. ret = -EOPNOTSUPP;
  629. goto out;
  630. }
  631. mutex_lock(&wl->mutex);
  632. ret = wl1251_ps_elp_wakeup(wl);
  633. if (ret < 0)
  634. goto out_unlock;
  635. switch (cmd) {
  636. case SET_KEY:
  637. wl_cmd->key_action = KEY_ADD_OR_REPLACE;
  638. break;
  639. case DISABLE_KEY:
  640. wl_cmd->key_action = KEY_REMOVE;
  641. break;
  642. default:
  643. wl1251_error("Unsupported key cmd 0x%x", cmd);
  644. break;
  645. }
  646. ret = wl1251_set_key_type(wl, wl_cmd, cmd, key, addr);
  647. if (ret < 0) {
  648. wl1251_error("Set KEY type failed");
  649. goto out_sleep;
  650. }
  651. if (wl_cmd->key_type != KEY_WEP_DEFAULT)
  652. memcpy(wl_cmd->addr, addr, ETH_ALEN);
  653. if ((wl_cmd->key_type == KEY_TKIP_MIC_GROUP) ||
  654. (wl_cmd->key_type == KEY_TKIP_MIC_PAIRWISE)) {
  655. /*
  656. * We get the key in the following form:
  657. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  658. * but the target is expecting:
  659. * TKIP - RX MIC - TX MIC
  660. */
  661. memcpy(wl_cmd->key, key->key, 16);
  662. memcpy(wl_cmd->key + 16, key->key + 24, 8);
  663. memcpy(wl_cmd->key + 24, key->key + 16, 8);
  664. } else {
  665. memcpy(wl_cmd->key, key->key, key->keylen);
  666. }
  667. wl_cmd->key_size = key->keylen;
  668. wl_cmd->id = key->keyidx;
  669. wl_cmd->ssid_profile = 0;
  670. wl1251_dump(DEBUG_CRYPT, "TARGET KEY: ", wl_cmd, sizeof(*wl_cmd));
  671. ret = wl1251_cmd_send(wl, CMD_SET_KEYS, wl_cmd, sizeof(*wl_cmd));
  672. if (ret < 0) {
  673. wl1251_warning("could not set keys");
  674. goto out_sleep;
  675. }
  676. out_sleep:
  677. wl1251_ps_elp_sleep(wl);
  678. out_unlock:
  679. mutex_unlock(&wl->mutex);
  680. out:
  681. kfree(wl_cmd);
  682. return ret;
  683. }
  684. static int wl1251_build_basic_rates(char *rates)
  685. {
  686. u8 index = 0;
  687. rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
  688. rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
  689. rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
  690. rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
  691. return index;
  692. }
  693. static int wl1251_build_extended_rates(char *rates)
  694. {
  695. u8 index = 0;
  696. rates[index++] = IEEE80211_OFDM_RATE_6MB;
  697. rates[index++] = IEEE80211_OFDM_RATE_9MB;
  698. rates[index++] = IEEE80211_OFDM_RATE_12MB;
  699. rates[index++] = IEEE80211_OFDM_RATE_18MB;
  700. rates[index++] = IEEE80211_OFDM_RATE_24MB;
  701. rates[index++] = IEEE80211_OFDM_RATE_36MB;
  702. rates[index++] = IEEE80211_OFDM_RATE_48MB;
  703. rates[index++] = IEEE80211_OFDM_RATE_54MB;
  704. return index;
  705. }
  706. static int wl1251_build_probe_req(struct wl1251 *wl, u8 *ssid, size_t ssid_len)
  707. {
  708. struct wl12xx_probe_req_template template;
  709. struct wl12xx_ie_rates *rates;
  710. char *ptr;
  711. u16 size;
  712. ptr = (char *)&template;
  713. size = sizeof(struct ieee80211_header);
  714. memset(template.header.da, 0xff, ETH_ALEN);
  715. memset(template.header.bssid, 0xff, ETH_ALEN);
  716. memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
  717. template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  718. /* IEs */
  719. /* SSID */
  720. template.ssid.header.id = WLAN_EID_SSID;
  721. template.ssid.header.len = ssid_len;
  722. if (ssid_len && ssid)
  723. memcpy(template.ssid.ssid, ssid, ssid_len);
  724. size += sizeof(struct wl12xx_ie_header) + ssid_len;
  725. ptr += size;
  726. /* Basic Rates */
  727. rates = (struct wl12xx_ie_rates *)ptr;
  728. rates->header.id = WLAN_EID_SUPP_RATES;
  729. rates->header.len = wl1251_build_basic_rates(rates->rates);
  730. size += sizeof(struct wl12xx_ie_header) + rates->header.len;
  731. ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
  732. /* Extended rates */
  733. rates = (struct wl12xx_ie_rates *)ptr;
  734. rates->header.id = WLAN_EID_EXT_SUPP_RATES;
  735. rates->header.len = wl1251_build_extended_rates(rates->rates);
  736. size += sizeof(struct wl12xx_ie_header) + rates->header.len;
  737. wl1251_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
  738. return wl1251_cmd_template_set(wl, CMD_PROBE_REQ, &template,
  739. size);
  740. }
  741. static int wl1251_hw_scan(struct wl1251 *wl, u8 *ssid, size_t len,
  742. u8 active_scan, u8 high_prio, u8 num_channels,
  743. u8 probe_requests)
  744. {
  745. struct wl1251_cmd_trigger_scan_to *trigger = NULL;
  746. struct cmd_scan *params = NULL;
  747. int i, ret;
  748. u16 scan_options = 0;
  749. if (wl->scanning)
  750. return -EINVAL;
  751. params = kzalloc(sizeof(*params), GFP_KERNEL);
  752. if (!params)
  753. return -ENOMEM;
  754. params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
  755. params->params.rx_filter_options =
  756. cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
  757. /* High priority scan */
  758. if (!active_scan)
  759. scan_options |= SCAN_PASSIVE;
  760. if (high_prio)
  761. scan_options |= SCAN_PRIORITY_HIGH;
  762. params->params.scan_options = scan_options;
  763. params->params.num_channels = num_channels;
  764. params->params.num_probe_requests = probe_requests;
  765. params->params.tx_rate = cpu_to_le16(1 << 1); /* 2 Mbps */
  766. params->params.tid_trigger = 0;
  767. for (i = 0; i < num_channels; i++) {
  768. params->channels[i].min_duration = cpu_to_le32(30000);
  769. params->channels[i].max_duration = cpu_to_le32(60000);
  770. memset(&params->channels[i].bssid_lsb, 0xff, 4);
  771. memset(&params->channels[i].bssid_msb, 0xff, 2);
  772. params->channels[i].early_termination = 0;
  773. params->channels[i].tx_power_att = 0;
  774. params->channels[i].channel = i + 1;
  775. memset(params->channels[i].pad, 0, 3);
  776. }
  777. for (i = num_channels; i < SCAN_MAX_NUM_OF_CHANNELS; i++)
  778. memset(&params->channels[i], 0,
  779. sizeof(struct basic_scan_channel_parameters));
  780. if (len && ssid) {
  781. params->params.ssid_len = len;
  782. memcpy(params->params.ssid, ssid, len);
  783. } else {
  784. params->params.ssid_len = 0;
  785. memset(params->params.ssid, 0, 32);
  786. }
  787. ret = wl1251_build_probe_req(wl, ssid, len);
  788. if (ret < 0) {
  789. wl1251_error("PROBE request template failed");
  790. goto out;
  791. }
  792. trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
  793. if (!trigger)
  794. goto out;
  795. trigger->timeout = 0;
  796. ret = wl1251_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
  797. sizeof(*trigger));
  798. if (ret < 0) {
  799. wl1251_error("trigger scan to failed for hw scan");
  800. goto out;
  801. }
  802. wl1251_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
  803. wl->scanning = true;
  804. ret = wl1251_cmd_send(wl, CMD_SCAN, params, sizeof(*params));
  805. if (ret < 0)
  806. wl1251_error("SCAN failed");
  807. wl1251_mem_read(wl, wl->cmd_box_addr, params, sizeof(*params));
  808. if (params->header.status != CMD_STATUS_SUCCESS) {
  809. wl1251_error("TEST command answer error: %d",
  810. params->header.status);
  811. wl->scanning = false;
  812. ret = -EIO;
  813. goto out;
  814. }
  815. out:
  816. kfree(params);
  817. return ret;
  818. }
  819. static int wl1251_op_hw_scan(struct ieee80211_hw *hw,
  820. struct cfg80211_scan_request *req)
  821. {
  822. struct wl1251 *wl = hw->priv;
  823. int ret;
  824. u8 *ssid = NULL;
  825. size_t ssid_len = 0;
  826. wl1251_debug(DEBUG_MAC80211, "mac80211 hw scan");
  827. if (req->n_ssids) {
  828. ssid = req->ssids[0].ssid;
  829. ssid_len = req->ssids[0].ssid_len;
  830. }
  831. mutex_lock(&wl->mutex);
  832. ret = wl1251_ps_elp_wakeup(wl);
  833. if (ret < 0)
  834. goto out;
  835. ret = wl1251_hw_scan(hw->priv, ssid, ssid_len, 1, 0, 13, 3);
  836. wl1251_ps_elp_sleep(wl);
  837. out:
  838. mutex_unlock(&wl->mutex);
  839. return ret;
  840. }
  841. static int wl1251_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  842. {
  843. struct wl1251 *wl = hw->priv;
  844. int ret;
  845. mutex_lock(&wl->mutex);
  846. ret = wl1251_ps_elp_wakeup(wl);
  847. if (ret < 0)
  848. goto out;
  849. ret = wl1251_acx_rts_threshold(wl, (u16) value);
  850. if (ret < 0)
  851. wl1251_warning("wl1251_op_set_rts_threshold failed: %d", ret);
  852. wl1251_ps_elp_sleep(wl);
  853. out:
  854. mutex_unlock(&wl->mutex);
  855. return ret;
  856. }
  857. static void wl1251_op_bss_info_changed(struct ieee80211_hw *hw,
  858. struct ieee80211_vif *vif,
  859. struct ieee80211_bss_conf *bss_conf,
  860. u32 changed)
  861. {
  862. enum wl1251_cmd_ps_mode mode;
  863. struct wl1251 *wl = hw->priv;
  864. struct sk_buff *beacon;
  865. int ret;
  866. wl1251_debug(DEBUG_MAC80211, "mac80211 bss info changed");
  867. mutex_lock(&wl->mutex);
  868. ret = wl1251_ps_elp_wakeup(wl);
  869. if (ret < 0)
  870. goto out;
  871. if (changed & BSS_CHANGED_BSSID) {
  872. memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
  873. ret = wl1251_build_null_data(wl);
  874. if (ret < 0)
  875. goto out;
  876. if (wl->bss_type != BSS_TYPE_IBSS) {
  877. ret = wl1251_join(wl, wl->bss_type, wl->channel,
  878. wl->beacon_int, wl->dtim_period);
  879. if (ret < 0)
  880. goto out_sleep;
  881. }
  882. }
  883. if (changed & BSS_CHANGED_ASSOC) {
  884. if (bss_conf->assoc) {
  885. wl->beacon_int = bss_conf->beacon_int;
  886. wl->dtim_period = bss_conf->dtim_period;
  887. ret = wl1251_acx_wr_tbtt_and_dtim(wl, wl->beacon_int,
  888. wl->dtim_period);
  889. wl->aid = bss_conf->aid;
  890. ret = wl1251_build_ps_poll(wl, wl->aid);
  891. if (ret < 0)
  892. goto out_sleep;
  893. ret = wl1251_acx_aid(wl, wl->aid);
  894. if (ret < 0)
  895. goto out_sleep;
  896. /* If we want to go in PSM but we're not there yet */
  897. if (wl->psm_requested && !wl->psm) {
  898. mode = STATION_POWER_SAVE_MODE;
  899. ret = wl1251_ps_set_mode(wl, mode);
  900. if (ret < 0)
  901. goto out_sleep;
  902. }
  903. } else {
  904. /* use defaults when not associated */
  905. wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
  906. wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
  907. }
  908. }
  909. if (changed & BSS_CHANGED_ERP_SLOT) {
  910. if (bss_conf->use_short_slot)
  911. ret = wl1251_acx_slot(wl, SLOT_TIME_SHORT);
  912. else
  913. ret = wl1251_acx_slot(wl, SLOT_TIME_LONG);
  914. if (ret < 0) {
  915. wl1251_warning("Set slot time failed %d", ret);
  916. goto out_sleep;
  917. }
  918. }
  919. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  920. if (bss_conf->use_short_preamble)
  921. wl1251_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
  922. else
  923. wl1251_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
  924. }
  925. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  926. if (bss_conf->use_cts_prot)
  927. ret = wl1251_acx_cts_protect(wl, CTSPROTECT_ENABLE);
  928. else
  929. ret = wl1251_acx_cts_protect(wl, CTSPROTECT_DISABLE);
  930. if (ret < 0) {
  931. wl1251_warning("Set ctsprotect failed %d", ret);
  932. goto out;
  933. }
  934. }
  935. if (changed & BSS_CHANGED_BEACON) {
  936. beacon = ieee80211_beacon_get(hw, vif);
  937. ret = wl1251_cmd_template_set(wl, CMD_BEACON, beacon->data,
  938. beacon->len);
  939. if (ret < 0) {
  940. dev_kfree_skb(beacon);
  941. goto out;
  942. }
  943. ret = wl1251_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
  944. beacon->len);
  945. dev_kfree_skb(beacon);
  946. if (ret < 0)
  947. goto out;
  948. ret = wl1251_join(wl, wl->bss_type, wl->beacon_int,
  949. wl->channel, wl->dtim_period);
  950. if (ret < 0)
  951. goto out;
  952. }
  953. out_sleep:
  954. wl1251_ps_elp_sleep(wl);
  955. out:
  956. mutex_unlock(&wl->mutex);
  957. }
  958. /* can't be const, mac80211 writes to this */
  959. static struct ieee80211_rate wl1251_rates[] = {
  960. { .bitrate = 10,
  961. .hw_value = 0x1,
  962. .hw_value_short = 0x1, },
  963. { .bitrate = 20,
  964. .hw_value = 0x2,
  965. .hw_value_short = 0x2,
  966. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  967. { .bitrate = 55,
  968. .hw_value = 0x4,
  969. .hw_value_short = 0x4,
  970. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  971. { .bitrate = 110,
  972. .hw_value = 0x20,
  973. .hw_value_short = 0x20,
  974. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  975. { .bitrate = 60,
  976. .hw_value = 0x8,
  977. .hw_value_short = 0x8, },
  978. { .bitrate = 90,
  979. .hw_value = 0x10,
  980. .hw_value_short = 0x10, },
  981. { .bitrate = 120,
  982. .hw_value = 0x40,
  983. .hw_value_short = 0x40, },
  984. { .bitrate = 180,
  985. .hw_value = 0x80,
  986. .hw_value_short = 0x80, },
  987. { .bitrate = 240,
  988. .hw_value = 0x200,
  989. .hw_value_short = 0x200, },
  990. { .bitrate = 360,
  991. .hw_value = 0x400,
  992. .hw_value_short = 0x400, },
  993. { .bitrate = 480,
  994. .hw_value = 0x800,
  995. .hw_value_short = 0x800, },
  996. { .bitrate = 540,
  997. .hw_value = 0x1000,
  998. .hw_value_short = 0x1000, },
  999. };
  1000. /* can't be const, mac80211 writes to this */
  1001. static struct ieee80211_channel wl1251_channels[] = {
  1002. { .hw_value = 1, .center_freq = 2412},
  1003. { .hw_value = 2, .center_freq = 2417},
  1004. { .hw_value = 3, .center_freq = 2422},
  1005. { .hw_value = 4, .center_freq = 2427},
  1006. { .hw_value = 5, .center_freq = 2432},
  1007. { .hw_value = 6, .center_freq = 2437},
  1008. { .hw_value = 7, .center_freq = 2442},
  1009. { .hw_value = 8, .center_freq = 2447},
  1010. { .hw_value = 9, .center_freq = 2452},
  1011. { .hw_value = 10, .center_freq = 2457},
  1012. { .hw_value = 11, .center_freq = 2462},
  1013. { .hw_value = 12, .center_freq = 2467},
  1014. { .hw_value = 13, .center_freq = 2472},
  1015. };
  1016. /* can't be const, mac80211 writes to this */
  1017. static struct ieee80211_supported_band wl1251_band_2ghz = {
  1018. .channels = wl1251_channels,
  1019. .n_channels = ARRAY_SIZE(wl1251_channels),
  1020. .bitrates = wl1251_rates,
  1021. .n_bitrates = ARRAY_SIZE(wl1251_rates),
  1022. };
  1023. static const struct ieee80211_ops wl1251_ops = {
  1024. .start = wl1251_op_start,
  1025. .stop = wl1251_op_stop,
  1026. .add_interface = wl1251_op_add_interface,
  1027. .remove_interface = wl1251_op_remove_interface,
  1028. .config = wl1251_op_config,
  1029. .configure_filter = wl1251_op_configure_filter,
  1030. .tx = wl1251_op_tx,
  1031. .set_key = wl1251_op_set_key,
  1032. .hw_scan = wl1251_op_hw_scan,
  1033. .bss_info_changed = wl1251_op_bss_info_changed,
  1034. .set_rts_threshold = wl1251_op_set_rts_threshold,
  1035. };
  1036. static int wl1251_register_hw(struct wl1251 *wl)
  1037. {
  1038. int ret;
  1039. if (wl->mac80211_registered)
  1040. return 0;
  1041. SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
  1042. ret = ieee80211_register_hw(wl->hw);
  1043. if (ret < 0) {
  1044. wl1251_error("unable to register mac80211 hw: %d", ret);
  1045. return ret;
  1046. }
  1047. wl->mac80211_registered = true;
  1048. wl1251_notice("loaded");
  1049. return 0;
  1050. }
  1051. int wl1251_init_ieee80211(struct wl1251 *wl)
  1052. {
  1053. int ret;
  1054. /* The tx descriptor buffer and the TKIP space */
  1055. wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
  1056. + WL1251_TKIP_IV_SPACE;
  1057. /* unit us */
  1058. /* FIXME: find a proper value */
  1059. wl->hw->channel_change_time = 10000;
  1060. wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
  1061. IEEE80211_HW_NOISE_DBM |
  1062. IEEE80211_HW_SUPPORTS_PS |
  1063. IEEE80211_HW_BEACON_FILTER;
  1064. wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  1065. wl->hw->wiphy->max_scan_ssids = 1;
  1066. wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1251_band_2ghz;
  1067. ret = wl1251_register_hw(wl);
  1068. if (ret)
  1069. goto out;
  1070. wl1251_debugfs_init(wl);
  1071. wl1251_notice("initialized");
  1072. ret = 0;
  1073. out:
  1074. return ret;
  1075. }
  1076. EXPORT_SYMBOL_GPL(wl1251_init_ieee80211);
  1077. struct ieee80211_hw *wl1251_alloc_hw(void)
  1078. {
  1079. struct ieee80211_hw *hw;
  1080. struct wl1251 *wl;
  1081. int i;
  1082. static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
  1083. hw = ieee80211_alloc_hw(sizeof(*wl), &wl1251_ops);
  1084. if (!hw) {
  1085. wl1251_error("could not alloc ieee80211_hw");
  1086. return ERR_PTR(-ENOMEM);
  1087. }
  1088. wl = hw->priv;
  1089. memset(wl, 0, sizeof(*wl));
  1090. wl->hw = hw;
  1091. wl->data_in_count = 0;
  1092. skb_queue_head_init(&wl->tx_queue);
  1093. INIT_WORK(&wl->filter_work, wl1251_filter_work);
  1094. INIT_DELAYED_WORK(&wl->elp_work, wl1251_elp_work);
  1095. wl->channel = WL1251_DEFAULT_CHANNEL;
  1096. wl->scanning = false;
  1097. wl->default_key = 0;
  1098. wl->listen_int = 1;
  1099. wl->rx_counter = 0;
  1100. wl->rx_handled = 0;
  1101. wl->rx_current_buffer = 0;
  1102. wl->rx_last_id = 0;
  1103. wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
  1104. wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
  1105. wl->elp = false;
  1106. wl->psm = 0;
  1107. wl->psm_requested = false;
  1108. wl->tx_queue_stopped = false;
  1109. wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
  1110. wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
  1111. wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
  1112. wl->vif = NULL;
  1113. for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
  1114. wl->tx_frames[i] = NULL;
  1115. wl->next_tx_complete = 0;
  1116. INIT_WORK(&wl->irq_work, wl1251_irq_work);
  1117. INIT_WORK(&wl->tx_work, wl1251_tx_work);
  1118. /*
  1119. * In case our MAC address is not correctly set,
  1120. * we use a random but Nokia MAC.
  1121. */
  1122. memcpy(wl->mac_addr, nokia_oui, 3);
  1123. get_random_bytes(wl->mac_addr + 3, 3);
  1124. wl->state = WL1251_STATE_OFF;
  1125. mutex_init(&wl->mutex);
  1126. wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
  1127. wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
  1128. wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
  1129. if (!wl->rx_descriptor) {
  1130. wl1251_error("could not allocate memory for rx descriptor");
  1131. ieee80211_free_hw(hw);
  1132. return ERR_PTR(-ENOMEM);
  1133. }
  1134. return hw;
  1135. }
  1136. EXPORT_SYMBOL_GPL(wl1251_alloc_hw);
  1137. int wl1251_free_hw(struct wl1251 *wl)
  1138. {
  1139. ieee80211_unregister_hw(wl->hw);
  1140. wl1251_debugfs_exit(wl);
  1141. kfree(wl->target_mem_map);
  1142. kfree(wl->data_path);
  1143. vfree(wl->fw);
  1144. wl->fw = NULL;
  1145. kfree(wl->nvs);
  1146. wl->nvs = NULL;
  1147. kfree(wl->rx_descriptor);
  1148. wl->rx_descriptor = NULL;
  1149. ieee80211_free_hw(wl->hw);
  1150. return 0;
  1151. }
  1152. EXPORT_SYMBOL_GPL(wl1251_free_hw);
  1153. MODULE_DESCRIPTION("TI wl1251 Wireles LAN Driver Core");
  1154. MODULE_LICENSE("GPL");
  1155. MODULE_AUTHOR("Kalle Valo <kalle.valo@nokia.com>");
  1156. MODULE_ALIAS("spi:wl1251");
  1157. MODULE_FIRMWARE(WL1251_FW_NAME);