wl1251_main.c 32 KB

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