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