main.c 32 KB

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