wl1251_main.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  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 "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 = kmalloc(wl->fw_len, GFP_KERNEL);
  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 = kmalloc(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. memcpy(wl->nvs, fw->data, wl->nvs_len);
  111. ret = 0;
  112. out:
  113. release_firmware(fw);
  114. return ret;
  115. }
  116. static void wl1251_fw_wakeup(struct wl1251 *wl)
  117. {
  118. u32 elp_reg;
  119. elp_reg = ELPCTRL_WAKE_UP;
  120. wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
  121. elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
  122. if (!(elp_reg & ELPCTRL_WLAN_READY))
  123. wl1251_warning("WLAN not ready");
  124. }
  125. static int wl1251_chip_wakeup(struct wl1251 *wl)
  126. {
  127. int ret = 0;
  128. wl1251_power_on(wl);
  129. msleep(WL1251_POWER_ON_SLEEP);
  130. wl->if_ops->reset(wl);
  131. /* We don't need a real memory partition here, because we only want
  132. * to use the registers at this point. */
  133. wl1251_set_partition(wl,
  134. 0x00000000,
  135. 0x00000000,
  136. REGISTERS_BASE,
  137. REGISTERS_DOWN_SIZE);
  138. /* ELP module wake up */
  139. wl1251_fw_wakeup(wl);
  140. /* whal_FwCtrl_BootSm() */
  141. /* 0. read chip id from CHIP_ID */
  142. wl->chip_id = wl1251_reg_read32(wl, CHIP_ID_B);
  143. /* 1. check if chip id is valid */
  144. switch (wl->chip_id) {
  145. case CHIP_ID_1251_PG12:
  146. wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG12)",
  147. wl->chip_id);
  148. break;
  149. case CHIP_ID_1251_PG10:
  150. case CHIP_ID_1251_PG11:
  151. default:
  152. wl1251_error("unsupported chip id: 0x%x", wl->chip_id);
  153. ret = -ENODEV;
  154. goto out;
  155. }
  156. if (wl->fw == NULL) {
  157. ret = wl1251_fetch_firmware(wl);
  158. if (ret < 0)
  159. goto out;
  160. }
  161. /* No NVS from netlink, try to get it from the filesystem */
  162. if (wl->nvs == NULL) {
  163. ret = wl1251_fetch_nvs(wl);
  164. if (ret < 0)
  165. goto out;
  166. }
  167. out:
  168. return ret;
  169. }
  170. static void wl1251_irq_work(struct work_struct *work)
  171. {
  172. u32 intr;
  173. struct wl1251 *wl =
  174. container_of(work, struct wl1251, irq_work);
  175. int ret;
  176. mutex_lock(&wl->mutex);
  177. wl1251_debug(DEBUG_IRQ, "IRQ work");
  178. if (wl->state == WL1251_STATE_OFF)
  179. goto out;
  180. ret = wl1251_ps_elp_wakeup(wl);
  181. if (ret < 0)
  182. goto out;
  183. wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, WL1251_ACX_INTR_ALL);
  184. intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
  185. wl1251_debug(DEBUG_IRQ, "intr: 0x%x", intr);
  186. if (wl->data_path) {
  187. wl->rx_counter =
  188. wl1251_mem_read32(wl, wl->data_path->rx_control_addr);
  189. /* We handle a frmware bug here */
  190. switch ((wl->rx_counter - wl->rx_handled) & 0xf) {
  191. case 0:
  192. wl1251_debug(DEBUG_IRQ, "RX: FW and host in sync");
  193. intr &= ~WL1251_ACX_INTR_RX0_DATA;
  194. intr &= ~WL1251_ACX_INTR_RX1_DATA;
  195. break;
  196. case 1:
  197. wl1251_debug(DEBUG_IRQ, "RX: FW +1");
  198. intr |= WL1251_ACX_INTR_RX0_DATA;
  199. intr &= ~WL1251_ACX_INTR_RX1_DATA;
  200. break;
  201. case 2:
  202. wl1251_debug(DEBUG_IRQ, "RX: FW +2");
  203. intr |= WL1251_ACX_INTR_RX0_DATA;
  204. intr |= WL1251_ACX_INTR_RX1_DATA;
  205. break;
  206. default:
  207. wl1251_warning("RX: FW and host out of sync: %d",
  208. wl->rx_counter - wl->rx_handled);
  209. break;
  210. }
  211. wl->rx_handled = wl->rx_counter;
  212. wl1251_debug(DEBUG_IRQ, "RX counter: %d", wl->rx_counter);
  213. }
  214. intr &= wl->intr_mask;
  215. if (intr == 0) {
  216. wl1251_debug(DEBUG_IRQ, "INTR is 0");
  217. wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK,
  218. ~(wl->intr_mask));
  219. goto out_sleep;
  220. }
  221. if (intr & WL1251_ACX_INTR_RX0_DATA) {
  222. wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX0_DATA");
  223. wl1251_rx(wl);
  224. }
  225. if (intr & WL1251_ACX_INTR_RX1_DATA) {
  226. wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX1_DATA");
  227. wl1251_rx(wl);
  228. }
  229. if (intr & WL1251_ACX_INTR_TX_RESULT) {
  230. wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_TX_RESULT");
  231. wl1251_tx_complete(wl);
  232. }
  233. if (intr & (WL1251_ACX_INTR_EVENT_A | WL1251_ACX_INTR_EVENT_B)) {
  234. wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT (0x%x)", intr);
  235. if (intr & WL1251_ACX_INTR_EVENT_A)
  236. wl1251_event_handle(wl, 0);
  237. else
  238. wl1251_event_handle(wl, 1);
  239. }
  240. if (intr & WL1251_ACX_INTR_INIT_COMPLETE)
  241. wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_INIT_COMPLETE");
  242. wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, ~(wl->intr_mask));
  243. out_sleep:
  244. wl1251_ps_elp_sleep(wl);
  245. out:
  246. mutex_unlock(&wl->mutex);
  247. }
  248. static int wl1251_join(struct wl1251 *wl, u8 bss_type, u8 channel,
  249. u16 beacon_interval, u8 dtim_period)
  250. {
  251. int ret;
  252. ret = wl1251_acx_frame_rates(wl, DEFAULT_HW_GEN_TX_RATE,
  253. DEFAULT_HW_GEN_MODULATION_TYPE,
  254. wl->tx_mgmt_frm_rate,
  255. wl->tx_mgmt_frm_mod);
  256. if (ret < 0)
  257. goto out;
  258. ret = wl1251_cmd_join(wl, bss_type, channel, beacon_interval,
  259. dtim_period);
  260. if (ret < 0)
  261. goto out;
  262. /*
  263. * FIXME: we should wait for JOIN_EVENT_COMPLETE_ID but to simplify
  264. * locking we just sleep instead, for now
  265. */
  266. msleep(10);
  267. out:
  268. return ret;
  269. }
  270. static void wl1251_filter_work(struct work_struct *work)
  271. {
  272. struct wl1251 *wl =
  273. container_of(work, struct wl1251, filter_work);
  274. int ret;
  275. mutex_lock(&wl->mutex);
  276. if (wl->state == WL1251_STATE_OFF)
  277. goto out;
  278. ret = wl1251_ps_elp_wakeup(wl);
  279. if (ret < 0)
  280. goto out;
  281. ret = wl1251_join(wl, wl->bss_type, wl->channel, wl->beacon_int,
  282. wl->dtim_period);
  283. if (ret < 0)
  284. goto out_sleep;
  285. out_sleep:
  286. wl1251_ps_elp_sleep(wl);
  287. out:
  288. mutex_unlock(&wl->mutex);
  289. }
  290. static int wl1251_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  291. {
  292. struct wl1251 *wl = hw->priv;
  293. skb_queue_tail(&wl->tx_queue, skb);
  294. /*
  295. * The chip specific setup must run before the first TX packet -
  296. * before that, the tx_work will not be initialized!
  297. */
  298. ieee80211_queue_work(wl->hw, &wl->tx_work);
  299. /*
  300. * The workqueue is slow to process the tx_queue and we need stop
  301. * the queue here, otherwise the queue will get too long.
  302. */
  303. if (skb_queue_len(&wl->tx_queue) >= WL1251_TX_QUEUE_MAX_LENGTH) {
  304. ieee80211_stop_queues(wl->hw);
  305. /*
  306. * FIXME: this is racy, the variable is not properly
  307. * protected. Maybe fix this by removing the stupid
  308. * variable altogether and checking the real queue state?
  309. */
  310. wl->tx_queue_stopped = true;
  311. }
  312. return NETDEV_TX_OK;
  313. }
  314. static int wl1251_op_start(struct ieee80211_hw *hw)
  315. {
  316. struct wl1251 *wl = hw->priv;
  317. int ret = 0;
  318. wl1251_debug(DEBUG_MAC80211, "mac80211 start");
  319. mutex_lock(&wl->mutex);
  320. if (wl->state != WL1251_STATE_OFF) {
  321. wl1251_error("cannot start because not in off state: %d",
  322. wl->state);
  323. ret = -EBUSY;
  324. goto out;
  325. }
  326. ret = wl1251_chip_wakeup(wl);
  327. if (ret < 0)
  328. goto out;
  329. ret = wl1251_boot(wl);
  330. if (ret < 0)
  331. goto out;
  332. ret = wl1251_hw_init(wl);
  333. if (ret < 0)
  334. goto out;
  335. ret = wl1251_acx_station_id(wl);
  336. if (ret < 0)
  337. goto out;
  338. wl->state = WL1251_STATE_ON;
  339. wl1251_info("firmware booted (%s)", wl->fw_ver);
  340. out:
  341. if (ret < 0)
  342. wl1251_power_off(wl);
  343. mutex_unlock(&wl->mutex);
  344. return ret;
  345. }
  346. static void wl1251_op_stop(struct ieee80211_hw *hw)
  347. {
  348. struct wl1251 *wl = hw->priv;
  349. wl1251_info("down");
  350. wl1251_debug(DEBUG_MAC80211, "mac80211 stop");
  351. mutex_lock(&wl->mutex);
  352. WARN_ON(wl->state != WL1251_STATE_ON);
  353. if (wl->scanning) {
  354. mutex_unlock(&wl->mutex);
  355. ieee80211_scan_completed(wl->hw, true);
  356. mutex_lock(&wl->mutex);
  357. wl->scanning = false;
  358. }
  359. wl->state = WL1251_STATE_OFF;
  360. wl1251_disable_interrupts(wl);
  361. mutex_unlock(&wl->mutex);
  362. cancel_work_sync(&wl->irq_work);
  363. cancel_work_sync(&wl->tx_work);
  364. cancel_work_sync(&wl->filter_work);
  365. mutex_lock(&wl->mutex);
  366. /* let's notify MAC80211 about the remaining pending TX frames */
  367. wl1251_tx_flush(wl);
  368. wl1251_power_off(wl);
  369. memset(wl->bssid, 0, ETH_ALEN);
  370. wl->listen_int = 1;
  371. wl->bss_type = MAX_BSS_TYPE;
  372. wl->data_in_count = 0;
  373. wl->rx_counter = 0;
  374. wl->rx_handled = 0;
  375. wl->rx_current_buffer = 0;
  376. wl->rx_last_id = 0;
  377. wl->next_tx_complete = 0;
  378. wl->elp = false;
  379. wl->psm = 0;
  380. wl->tx_queue_stopped = false;
  381. wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
  382. wl->channel = WL1251_DEFAULT_CHANNEL;
  383. wl1251_debugfs_reset(wl);
  384. mutex_unlock(&wl->mutex);
  385. }
  386. static int wl1251_op_add_interface(struct ieee80211_hw *hw,
  387. struct ieee80211_if_init_conf *conf)
  388. {
  389. struct wl1251 *wl = hw->priv;
  390. int ret = 0;
  391. wl1251_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
  392. conf->type, conf->mac_addr);
  393. mutex_lock(&wl->mutex);
  394. switch (conf->type) {
  395. case NL80211_IFTYPE_STATION:
  396. wl->bss_type = BSS_TYPE_STA_BSS;
  397. break;
  398. case NL80211_IFTYPE_ADHOC:
  399. wl->bss_type = BSS_TYPE_IBSS;
  400. break;
  401. default:
  402. ret = -EOPNOTSUPP;
  403. goto out;
  404. }
  405. if (memcmp(wl->mac_addr, conf->mac_addr, ETH_ALEN)) {
  406. memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
  407. SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
  408. ret = wl1251_acx_station_id(wl);
  409. if (ret < 0)
  410. goto out;
  411. }
  412. out:
  413. mutex_unlock(&wl->mutex);
  414. return ret;
  415. }
  416. static void wl1251_op_remove_interface(struct ieee80211_hw *hw,
  417. struct ieee80211_if_init_conf *conf)
  418. {
  419. wl1251_debug(DEBUG_MAC80211, "mac80211 remove interface");
  420. }
  421. static int wl1251_build_null_data(struct wl1251 *wl)
  422. {
  423. struct wl12xx_null_data_template template;
  424. if (!is_zero_ether_addr(wl->bssid)) {
  425. memcpy(template.header.da, wl->bssid, ETH_ALEN);
  426. memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
  427. } else {
  428. memset(template.header.da, 0xff, ETH_ALEN);
  429. memset(template.header.bssid, 0xff, ETH_ALEN);
  430. }
  431. memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
  432. template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
  433. IEEE80211_STYPE_NULLFUNC);
  434. return wl1251_cmd_template_set(wl, CMD_NULL_DATA, &template,
  435. sizeof(template));
  436. }
  437. static int wl1251_build_ps_poll(struct wl1251 *wl, u16 aid)
  438. {
  439. struct wl12xx_ps_poll_template template;
  440. memcpy(template.bssid, wl->bssid, ETH_ALEN);
  441. memcpy(template.ta, wl->mac_addr, ETH_ALEN);
  442. template.aid = aid;
  443. template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
  444. return wl1251_cmd_template_set(wl, CMD_PS_POLL, &template,
  445. sizeof(template));
  446. }
  447. static int wl1251_op_config(struct ieee80211_hw *hw, u32 changed)
  448. {
  449. struct wl1251 *wl = hw->priv;
  450. struct ieee80211_conf *conf = &hw->conf;
  451. int channel, ret = 0;
  452. channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
  453. wl1251_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
  454. channel,
  455. conf->flags & IEEE80211_CONF_PS ? "on" : "off",
  456. conf->power_level);
  457. mutex_lock(&wl->mutex);
  458. ret = wl1251_ps_elp_wakeup(wl);
  459. if (ret < 0)
  460. goto out;
  461. if (channel != wl->channel) {
  462. wl->channel = channel;
  463. ret = wl1251_join(wl, wl->bss_type, wl->channel,
  464. wl->beacon_int, wl->dtim_period);
  465. if (ret < 0)
  466. goto out_sleep;
  467. }
  468. ret = wl1251_build_null_data(wl);
  469. if (ret < 0)
  470. goto out_sleep;
  471. if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
  472. wl1251_debug(DEBUG_PSM, "psm enabled");
  473. wl->psm_requested = true;
  474. /*
  475. * We enter PSM only if we're already associated.
  476. * If we're not, we'll enter it when joining an SSID,
  477. * through the bss_info_changed() hook.
  478. */
  479. ret = wl1251_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
  480. } else if (!(conf->flags & IEEE80211_CONF_PS) &&
  481. wl->psm_requested) {
  482. wl1251_debug(DEBUG_PSM, "psm disabled");
  483. wl->psm_requested = false;
  484. if (wl->psm)
  485. ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
  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;
  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;
  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_ASSOC) {
  850. if (bss_conf->assoc) {
  851. wl->beacon_int = bss_conf->beacon_int;
  852. wl->dtim_period = bss_conf->dtim_period;
  853. /* FIXME: call join */
  854. wl->aid = bss_conf->aid;
  855. ret = wl1251_build_ps_poll(wl, wl->aid);
  856. if (ret < 0)
  857. goto out_sleep;
  858. ret = wl1251_acx_aid(wl, wl->aid);
  859. if (ret < 0)
  860. goto out_sleep;
  861. /* If we want to go in PSM but we're not there yet */
  862. if (wl->psm_requested && !wl->psm) {
  863. mode = STATION_POWER_SAVE_MODE;
  864. ret = wl1251_ps_set_mode(wl, mode);
  865. if (ret < 0)
  866. goto out_sleep;
  867. }
  868. } else {
  869. /* use defaults when not associated */
  870. wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
  871. wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
  872. }
  873. }
  874. if (changed & BSS_CHANGED_ERP_SLOT) {
  875. if (bss_conf->use_short_slot)
  876. ret = wl1251_acx_slot(wl, SLOT_TIME_SHORT);
  877. else
  878. ret = wl1251_acx_slot(wl, SLOT_TIME_LONG);
  879. if (ret < 0) {
  880. wl1251_warning("Set slot time failed %d", ret);
  881. goto out_sleep;
  882. }
  883. }
  884. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  885. if (bss_conf->use_short_preamble)
  886. wl1251_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
  887. else
  888. wl1251_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
  889. }
  890. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  891. if (bss_conf->use_cts_prot)
  892. ret = wl1251_acx_cts_protect(wl, CTSPROTECT_ENABLE);
  893. else
  894. ret = wl1251_acx_cts_protect(wl, CTSPROTECT_DISABLE);
  895. if (ret < 0) {
  896. wl1251_warning("Set ctsprotect failed %d", ret);
  897. goto out;
  898. }
  899. }
  900. if (changed & BSS_CHANGED_BSSID) {
  901. memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
  902. ret = wl1251_build_null_data(wl);
  903. if (ret < 0)
  904. goto out;
  905. if (wl->bss_type != BSS_TYPE_IBSS) {
  906. ret = wl1251_join(wl, wl->bss_type, wl->channel,
  907. wl->beacon_int, wl->dtim_period);
  908. if (ret < 0)
  909. goto out_sleep;
  910. wl1251_warning("Set ctsprotect failed %d", ret);
  911. goto out_sleep;
  912. }
  913. }
  914. if (changed & BSS_CHANGED_BEACON) {
  915. beacon = ieee80211_beacon_get(hw, vif);
  916. ret = wl1251_cmd_template_set(wl, CMD_BEACON, beacon->data,
  917. beacon->len);
  918. if (ret < 0) {
  919. dev_kfree_skb(beacon);
  920. goto out;
  921. }
  922. ret = wl1251_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
  923. beacon->len);
  924. dev_kfree_skb(beacon);
  925. if (ret < 0)
  926. goto out;
  927. ret = wl1251_join(wl, wl->bss_type, wl->beacon_int,
  928. wl->channel, wl->dtim_period);
  929. if (ret < 0)
  930. goto out;
  931. }
  932. out_sleep:
  933. wl1251_ps_elp_sleep(wl);
  934. out:
  935. mutex_unlock(&wl->mutex);
  936. }
  937. /* can't be const, mac80211 writes to this */
  938. static struct ieee80211_rate wl1251_rates[] = {
  939. { .bitrate = 10,
  940. .hw_value = 0x1,
  941. .hw_value_short = 0x1, },
  942. { .bitrate = 20,
  943. .hw_value = 0x2,
  944. .hw_value_short = 0x2,
  945. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  946. { .bitrate = 55,
  947. .hw_value = 0x4,
  948. .hw_value_short = 0x4,
  949. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  950. { .bitrate = 110,
  951. .hw_value = 0x20,
  952. .hw_value_short = 0x20,
  953. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  954. { .bitrate = 60,
  955. .hw_value = 0x8,
  956. .hw_value_short = 0x8, },
  957. { .bitrate = 90,
  958. .hw_value = 0x10,
  959. .hw_value_short = 0x10, },
  960. { .bitrate = 120,
  961. .hw_value = 0x40,
  962. .hw_value_short = 0x40, },
  963. { .bitrate = 180,
  964. .hw_value = 0x80,
  965. .hw_value_short = 0x80, },
  966. { .bitrate = 240,
  967. .hw_value = 0x200,
  968. .hw_value_short = 0x200, },
  969. { .bitrate = 360,
  970. .hw_value = 0x400,
  971. .hw_value_short = 0x400, },
  972. { .bitrate = 480,
  973. .hw_value = 0x800,
  974. .hw_value_short = 0x800, },
  975. { .bitrate = 540,
  976. .hw_value = 0x1000,
  977. .hw_value_short = 0x1000, },
  978. };
  979. /* can't be const, mac80211 writes to this */
  980. static struct ieee80211_channel wl1251_channels[] = {
  981. { .hw_value = 1, .center_freq = 2412},
  982. { .hw_value = 2, .center_freq = 2417},
  983. { .hw_value = 3, .center_freq = 2422},
  984. { .hw_value = 4, .center_freq = 2427},
  985. { .hw_value = 5, .center_freq = 2432},
  986. { .hw_value = 6, .center_freq = 2437},
  987. { .hw_value = 7, .center_freq = 2442},
  988. { .hw_value = 8, .center_freq = 2447},
  989. { .hw_value = 9, .center_freq = 2452},
  990. { .hw_value = 10, .center_freq = 2457},
  991. { .hw_value = 11, .center_freq = 2462},
  992. { .hw_value = 12, .center_freq = 2467},
  993. { .hw_value = 13, .center_freq = 2472},
  994. };
  995. /* can't be const, mac80211 writes to this */
  996. static struct ieee80211_supported_band wl1251_band_2ghz = {
  997. .channels = wl1251_channels,
  998. .n_channels = ARRAY_SIZE(wl1251_channels),
  999. .bitrates = wl1251_rates,
  1000. .n_bitrates = ARRAY_SIZE(wl1251_rates),
  1001. };
  1002. static const struct ieee80211_ops wl1251_ops = {
  1003. .start = wl1251_op_start,
  1004. .stop = wl1251_op_stop,
  1005. .add_interface = wl1251_op_add_interface,
  1006. .remove_interface = wl1251_op_remove_interface,
  1007. .config = wl1251_op_config,
  1008. .configure_filter = wl1251_op_configure_filter,
  1009. .tx = wl1251_op_tx,
  1010. .set_key = wl1251_op_set_key,
  1011. .hw_scan = wl1251_op_hw_scan,
  1012. .bss_info_changed = wl1251_op_bss_info_changed,
  1013. .set_rts_threshold = wl1251_op_set_rts_threshold,
  1014. };
  1015. static int wl1251_register_hw(struct wl1251 *wl)
  1016. {
  1017. int ret;
  1018. if (wl->mac80211_registered)
  1019. return 0;
  1020. SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
  1021. ret = ieee80211_register_hw(wl->hw);
  1022. if (ret < 0) {
  1023. wl1251_error("unable to register mac80211 hw: %d", ret);
  1024. return ret;
  1025. }
  1026. wl->mac80211_registered = true;
  1027. wl1251_notice("loaded");
  1028. return 0;
  1029. }
  1030. int wl1251_init_ieee80211(struct wl1251 *wl)
  1031. {
  1032. int ret;
  1033. /* The tx descriptor buffer and the TKIP space */
  1034. wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
  1035. + WL1251_TKIP_IV_SPACE;
  1036. /* unit us */
  1037. /* FIXME: find a proper value */
  1038. wl->hw->channel_change_time = 10000;
  1039. wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
  1040. IEEE80211_HW_NOISE_DBM;
  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. ret = wl1251_register_hw(wl);
  1045. if (ret)
  1046. goto out;
  1047. wl1251_debugfs_init(wl);
  1048. wl1251_notice("initialized");
  1049. ret = 0;
  1050. out:
  1051. return ret;
  1052. }
  1053. EXPORT_SYMBOL_GPL(wl1251_init_ieee80211);
  1054. struct ieee80211_hw *wl1251_alloc_hw(void)
  1055. {
  1056. struct ieee80211_hw *hw;
  1057. struct wl1251 *wl;
  1058. int i;
  1059. static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
  1060. hw = ieee80211_alloc_hw(sizeof(*wl), &wl1251_ops);
  1061. if (!hw) {
  1062. wl1251_error("could not alloc ieee80211_hw");
  1063. return ERR_PTR(-ENOMEM);
  1064. }
  1065. wl = hw->priv;
  1066. memset(wl, 0, sizeof(*wl));
  1067. wl->hw = hw;
  1068. wl->data_in_count = 0;
  1069. skb_queue_head_init(&wl->tx_queue);
  1070. INIT_WORK(&wl->filter_work, wl1251_filter_work);
  1071. wl->channel = WL1251_DEFAULT_CHANNEL;
  1072. wl->scanning = false;
  1073. wl->default_key = 0;
  1074. wl->listen_int = 1;
  1075. wl->rx_counter = 0;
  1076. wl->rx_handled = 0;
  1077. wl->rx_current_buffer = 0;
  1078. wl->rx_last_id = 0;
  1079. wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
  1080. wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
  1081. wl->elp = false;
  1082. wl->psm = 0;
  1083. wl->psm_requested = false;
  1084. wl->tx_queue_stopped = false;
  1085. wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
  1086. wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
  1087. wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
  1088. for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
  1089. wl->tx_frames[i] = NULL;
  1090. wl->next_tx_complete = 0;
  1091. INIT_WORK(&wl->irq_work, wl1251_irq_work);
  1092. INIT_WORK(&wl->tx_work, wl1251_tx_work);
  1093. /*
  1094. * In case our MAC address is not correctly set,
  1095. * we use a random but Nokia MAC.
  1096. */
  1097. memcpy(wl->mac_addr, nokia_oui, 3);
  1098. get_random_bytes(wl->mac_addr + 3, 3);
  1099. wl->state = WL1251_STATE_OFF;
  1100. mutex_init(&wl->mutex);
  1101. wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
  1102. wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
  1103. wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
  1104. if (!wl->rx_descriptor) {
  1105. wl1251_error("could not allocate memory for rx descriptor");
  1106. ieee80211_free_hw(hw);
  1107. return ERR_PTR(-ENOMEM);
  1108. }
  1109. return hw;
  1110. }
  1111. EXPORT_SYMBOL_GPL(wl1251_alloc_hw);
  1112. int wl1251_free_hw(struct wl1251 *wl)
  1113. {
  1114. ieee80211_unregister_hw(wl->hw);
  1115. wl1251_debugfs_exit(wl);
  1116. kfree(wl->target_mem_map);
  1117. kfree(wl->data_path);
  1118. kfree(wl->fw);
  1119. wl->fw = NULL;
  1120. kfree(wl->nvs);
  1121. wl->nvs = NULL;
  1122. kfree(wl->rx_descriptor);
  1123. wl->rx_descriptor = NULL;
  1124. ieee80211_free_hw(wl->hw);
  1125. return 0;
  1126. }
  1127. EXPORT_SYMBOL_GPL(wl1251_free_hw);
  1128. MODULE_DESCRIPTION("TI wl1251 Wireles LAN Driver Core");
  1129. MODULE_LICENSE("GPL");
  1130. MODULE_AUTHOR("Kalle Valo <kalle.valo@nokia.com>");
  1131. MODULE_ALIAS("spi:wl12xx");