main.c 36 KB

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