main.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  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. eth_zero_addr(wl->bssid);
  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_UAPSD |
  397. IEEE80211_VIF_SUPPORTS_CQM_RSSI;
  398. wl1251_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
  399. vif->type, vif->addr);
  400. mutex_lock(&wl->mutex);
  401. if (wl->vif) {
  402. ret = -EBUSY;
  403. goto out;
  404. }
  405. wl->vif = vif;
  406. switch (vif->type) {
  407. case NL80211_IFTYPE_STATION:
  408. wl->bss_type = BSS_TYPE_STA_BSS;
  409. break;
  410. case NL80211_IFTYPE_ADHOC:
  411. wl->bss_type = BSS_TYPE_IBSS;
  412. break;
  413. default:
  414. ret = -EOPNOTSUPP;
  415. goto out;
  416. }
  417. if (!ether_addr_equal_unaligned(wl->mac_addr, vif->addr)) {
  418. memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
  419. SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
  420. ret = wl1251_acx_station_id(wl);
  421. if (ret < 0)
  422. goto out;
  423. }
  424. out:
  425. mutex_unlock(&wl->mutex);
  426. return ret;
  427. }
  428. static void wl1251_op_remove_interface(struct ieee80211_hw *hw,
  429. struct ieee80211_vif *vif)
  430. {
  431. struct wl1251 *wl = hw->priv;
  432. mutex_lock(&wl->mutex);
  433. wl1251_debug(DEBUG_MAC80211, "mac80211 remove interface");
  434. wl->vif = NULL;
  435. eth_zero_addr(wl->bssid);
  436. mutex_unlock(&wl->mutex);
  437. }
  438. static int wl1251_build_null_data(struct wl1251 *wl)
  439. {
  440. struct sk_buff *skb = NULL;
  441. int size;
  442. void *ptr;
  443. int ret = -ENOMEM;
  444. if (wl->bss_type == BSS_TYPE_IBSS) {
  445. size = sizeof(struct wl12xx_null_data_template);
  446. ptr = NULL;
  447. } else {
  448. skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
  449. if (!skb)
  450. goto out;
  451. size = skb->len;
  452. ptr = skb->data;
  453. }
  454. ret = wl1251_cmd_template_set(wl, CMD_NULL_DATA, ptr, size);
  455. out:
  456. dev_kfree_skb(skb);
  457. if (ret)
  458. wl1251_warning("cmd buld null data failed: %d", ret);
  459. return ret;
  460. }
  461. static int wl1251_build_qos_null_data(struct wl1251 *wl)
  462. {
  463. struct ieee80211_qos_hdr template;
  464. memset(&template, 0, sizeof(template));
  465. memcpy(template.addr1, wl->bssid, ETH_ALEN);
  466. memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
  467. memcpy(template.addr3, wl->bssid, ETH_ALEN);
  468. template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  469. IEEE80211_STYPE_QOS_NULLFUNC |
  470. IEEE80211_FCTL_TODS);
  471. /* FIXME: not sure what priority to use here */
  472. template.qos_ctrl = cpu_to_le16(0);
  473. return wl1251_cmd_template_set(wl, CMD_QOS_NULL_DATA, &template,
  474. sizeof(template));
  475. }
  476. static bool wl1251_can_do_pm(struct ieee80211_conf *conf, struct wl1251 *wl)
  477. {
  478. return (conf->flags & IEEE80211_CONF_PS) && !wl->monitor_present;
  479. }
  480. static int wl1251_op_config(struct ieee80211_hw *hw, u32 changed)
  481. {
  482. struct wl1251 *wl = hw->priv;
  483. struct ieee80211_conf *conf = &hw->conf;
  484. int channel, ret = 0;
  485. channel = ieee80211_frequency_to_channel(
  486. conf->chandef.chan->center_freq);
  487. wl1251_debug(DEBUG_MAC80211,
  488. "mac80211 config ch %d monitor %s psm %s power %d",
  489. channel,
  490. conf->flags & IEEE80211_CONF_MONITOR ? "on" : "off",
  491. conf->flags & IEEE80211_CONF_PS ? "on" : "off",
  492. conf->power_level);
  493. mutex_lock(&wl->mutex);
  494. ret = wl1251_ps_elp_wakeup(wl);
  495. if (ret < 0)
  496. goto out;
  497. if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
  498. u32 mode;
  499. if (conf->flags & IEEE80211_CONF_MONITOR) {
  500. wl->monitor_present = true;
  501. mode = DF_SNIFF_MODE_ENABLE | DF_ENCRYPTION_DISABLE;
  502. } else {
  503. wl->monitor_present = false;
  504. mode = 0;
  505. }
  506. ret = wl1251_acx_feature_cfg(wl, mode);
  507. if (ret < 0)
  508. goto out_sleep;
  509. }
  510. if (channel != wl->channel) {
  511. wl->channel = channel;
  512. /*
  513. * Use ENABLE_RX command for channel switching when no
  514. * interface is present (monitor mode only).
  515. * This leaves the tx path disabled in firmware, whereas
  516. * the usual JOIN command seems to transmit some frames
  517. * at firmware level.
  518. */
  519. if (wl->vif == NULL) {
  520. wl->joined = false;
  521. ret = wl1251_cmd_data_path_rx(wl, wl->channel, 1);
  522. } else {
  523. ret = wl1251_join(wl, wl->bss_type, wl->channel,
  524. wl->beacon_int, wl->dtim_period);
  525. }
  526. if (ret < 0)
  527. goto out_sleep;
  528. }
  529. if (wl1251_can_do_pm(conf, wl) && !wl->psm_requested) {
  530. wl1251_debug(DEBUG_PSM, "psm enabled");
  531. wl->psm_requested = true;
  532. wl->dtim_period = conf->ps_dtim_period;
  533. ret = wl1251_acx_wr_tbtt_and_dtim(wl, wl->beacon_int,
  534. wl->dtim_period);
  535. /*
  536. * mac80211 enables PSM only if we're already associated.
  537. */
  538. ret = wl1251_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
  539. if (ret < 0)
  540. goto out_sleep;
  541. } else if (!wl1251_can_do_pm(conf, wl) && wl->psm_requested) {
  542. wl1251_debug(DEBUG_PSM, "psm disabled");
  543. wl->psm_requested = false;
  544. if (wl->station_mode != STATION_ACTIVE_MODE) {
  545. ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
  546. if (ret < 0)
  547. goto out_sleep;
  548. }
  549. }
  550. if (changed & IEEE80211_CONF_CHANGE_IDLE && !wl->scanning) {
  551. if (conf->flags & IEEE80211_CONF_IDLE) {
  552. ret = wl1251_ps_set_mode(wl, STATION_IDLE);
  553. if (ret < 0)
  554. goto out_sleep;
  555. } else {
  556. ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
  557. if (ret < 0)
  558. goto out_sleep;
  559. ret = wl1251_join(wl, wl->bss_type, wl->channel,
  560. wl->beacon_int, wl->dtim_period);
  561. if (ret < 0)
  562. goto out_sleep;
  563. }
  564. }
  565. if (conf->power_level != wl->power_level) {
  566. ret = wl1251_acx_tx_power(wl, conf->power_level);
  567. if (ret < 0)
  568. goto out_sleep;
  569. wl->power_level = conf->power_level;
  570. }
  571. out_sleep:
  572. wl1251_ps_elp_sleep(wl);
  573. out:
  574. mutex_unlock(&wl->mutex);
  575. return ret;
  576. }
  577. struct wl1251_filter_params {
  578. bool enabled;
  579. int mc_list_length;
  580. u8 mc_list[ACX_MC_ADDRESS_GROUP_MAX][ETH_ALEN];
  581. };
  582. static u64 wl1251_op_prepare_multicast(struct ieee80211_hw *hw,
  583. struct netdev_hw_addr_list *mc_list)
  584. {
  585. struct wl1251_filter_params *fp;
  586. struct netdev_hw_addr *ha;
  587. struct wl1251 *wl = hw->priv;
  588. if (unlikely(wl->state == WL1251_STATE_OFF))
  589. return 0;
  590. fp = kzalloc(sizeof(*fp), GFP_ATOMIC);
  591. if (!fp) {
  592. wl1251_error("Out of memory setting filters.");
  593. return 0;
  594. }
  595. /* update multicast filtering parameters */
  596. fp->mc_list_length = 0;
  597. if (netdev_hw_addr_list_count(mc_list) > ACX_MC_ADDRESS_GROUP_MAX) {
  598. fp->enabled = false;
  599. } else {
  600. fp->enabled = true;
  601. netdev_hw_addr_list_for_each(ha, mc_list) {
  602. memcpy(fp->mc_list[fp->mc_list_length],
  603. ha->addr, ETH_ALEN);
  604. fp->mc_list_length++;
  605. }
  606. }
  607. return (u64)(unsigned long)fp;
  608. }
  609. #define WL1251_SUPPORTED_FILTERS (FIF_ALLMULTI | \
  610. FIF_FCSFAIL | \
  611. FIF_BCN_PRBRESP_PROMISC | \
  612. FIF_CONTROL | \
  613. FIF_OTHER_BSS | \
  614. FIF_PROBE_REQ)
  615. static void wl1251_op_configure_filter(struct ieee80211_hw *hw,
  616. unsigned int changed,
  617. unsigned int *total, u64 multicast)
  618. {
  619. struct wl1251_filter_params *fp = (void *)(unsigned long)multicast;
  620. struct wl1251 *wl = hw->priv;
  621. int ret;
  622. wl1251_debug(DEBUG_MAC80211, "mac80211 configure filter");
  623. *total &= WL1251_SUPPORTED_FILTERS;
  624. changed &= WL1251_SUPPORTED_FILTERS;
  625. if (changed == 0) {
  626. /* no filters which we support changed */
  627. kfree(fp);
  628. return;
  629. }
  630. mutex_lock(&wl->mutex);
  631. wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
  632. wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
  633. if (*total & FIF_ALLMULTI)
  634. /*
  635. * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
  636. * all multicast frames
  637. */
  638. wl->rx_config &= ~CFG_MC_FILTER_EN;
  639. if (*total & FIF_FCSFAIL)
  640. wl->rx_filter |= CFG_RX_FCS_ERROR;
  641. if (*total & FIF_BCN_PRBRESP_PROMISC) {
  642. wl->rx_config &= ~CFG_BSSID_FILTER_EN;
  643. wl->rx_config &= ~CFG_SSID_FILTER_EN;
  644. }
  645. if (*total & FIF_CONTROL)
  646. wl->rx_filter |= CFG_RX_CTL_EN;
  647. if (*total & FIF_OTHER_BSS || is_zero_ether_addr(wl->bssid))
  648. wl->rx_config &= ~CFG_BSSID_FILTER_EN;
  649. if (*total & FIF_PROBE_REQ)
  650. wl->rx_filter |= CFG_RX_PREQ_EN;
  651. if (wl->state == WL1251_STATE_OFF)
  652. goto out;
  653. ret = wl1251_ps_elp_wakeup(wl);
  654. if (ret < 0)
  655. goto out;
  656. if (*total & FIF_ALLMULTI)
  657. ret = wl1251_acx_group_address_tbl(wl, false, NULL, 0);
  658. else if (fp)
  659. ret = wl1251_acx_group_address_tbl(wl, fp->enabled,
  660. fp->mc_list,
  661. fp->mc_list_length);
  662. if (ret < 0)
  663. goto out;
  664. /* send filters to firmware */
  665. wl1251_acx_rx_config(wl, wl->rx_config, wl->rx_filter);
  666. wl1251_ps_elp_sleep(wl);
  667. out:
  668. mutex_unlock(&wl->mutex);
  669. kfree(fp);
  670. }
  671. /* HW encryption */
  672. static int wl1251_set_key_type(struct wl1251 *wl,
  673. struct wl1251_cmd_set_keys *key,
  674. enum set_key_cmd cmd,
  675. struct ieee80211_key_conf *mac80211_key,
  676. const u8 *addr)
  677. {
  678. switch (mac80211_key->cipher) {
  679. case WLAN_CIPHER_SUITE_WEP40:
  680. case WLAN_CIPHER_SUITE_WEP104:
  681. if (is_broadcast_ether_addr(addr))
  682. key->key_type = KEY_WEP_DEFAULT;
  683. else
  684. key->key_type = KEY_WEP_ADDR;
  685. mac80211_key->hw_key_idx = mac80211_key->keyidx;
  686. break;
  687. case WLAN_CIPHER_SUITE_TKIP:
  688. if (is_broadcast_ether_addr(addr))
  689. key->key_type = KEY_TKIP_MIC_GROUP;
  690. else
  691. key->key_type = KEY_TKIP_MIC_PAIRWISE;
  692. mac80211_key->hw_key_idx = mac80211_key->keyidx;
  693. break;
  694. case WLAN_CIPHER_SUITE_CCMP:
  695. if (is_broadcast_ether_addr(addr))
  696. key->key_type = KEY_AES_GROUP;
  697. else
  698. key->key_type = KEY_AES_PAIRWISE;
  699. mac80211_key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  700. break;
  701. default:
  702. wl1251_error("Unknown key cipher 0x%x", mac80211_key->cipher);
  703. return -EOPNOTSUPP;
  704. }
  705. return 0;
  706. }
  707. static int wl1251_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  708. struct ieee80211_vif *vif,
  709. struct ieee80211_sta *sta,
  710. struct ieee80211_key_conf *key)
  711. {
  712. struct wl1251 *wl = hw->priv;
  713. struct wl1251_cmd_set_keys *wl_cmd;
  714. const u8 *addr;
  715. int ret;
  716. static const u8 bcast_addr[ETH_ALEN] =
  717. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  718. wl1251_debug(DEBUG_MAC80211, "mac80211 set key");
  719. wl_cmd = kzalloc(sizeof(*wl_cmd), GFP_KERNEL);
  720. if (!wl_cmd) {
  721. ret = -ENOMEM;
  722. goto out;
  723. }
  724. addr = sta ? sta->addr : bcast_addr;
  725. wl1251_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
  726. wl1251_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
  727. wl1251_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
  728. key->cipher, key->keyidx, key->keylen, key->flags);
  729. wl1251_dump(DEBUG_CRYPT, "KEY: ", key->key, key->keylen);
  730. if (is_zero_ether_addr(addr)) {
  731. /* We dont support TX only encryption */
  732. ret = -EOPNOTSUPP;
  733. goto out;
  734. }
  735. mutex_lock(&wl->mutex);
  736. switch (cmd) {
  737. case SET_KEY:
  738. if (wl->monitor_present) {
  739. ret = -EOPNOTSUPP;
  740. goto out_unlock;
  741. }
  742. wl_cmd->key_action = KEY_ADD_OR_REPLACE;
  743. break;
  744. case DISABLE_KEY:
  745. wl_cmd->key_action = KEY_REMOVE;
  746. break;
  747. default:
  748. wl1251_error("Unsupported key cmd 0x%x", cmd);
  749. break;
  750. }
  751. ret = wl1251_ps_elp_wakeup(wl);
  752. if (ret < 0)
  753. goto out_unlock;
  754. ret = wl1251_set_key_type(wl, wl_cmd, cmd, key, addr);
  755. if (ret < 0) {
  756. wl1251_error("Set KEY type failed");
  757. goto out_sleep;
  758. }
  759. if (wl_cmd->key_type != KEY_WEP_DEFAULT)
  760. memcpy(wl_cmd->addr, addr, ETH_ALEN);
  761. if ((wl_cmd->key_type == KEY_TKIP_MIC_GROUP) ||
  762. (wl_cmd->key_type == KEY_TKIP_MIC_PAIRWISE)) {
  763. /*
  764. * We get the key in the following form:
  765. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  766. * but the target is expecting:
  767. * TKIP - RX MIC - TX MIC
  768. */
  769. memcpy(wl_cmd->key, key->key, 16);
  770. memcpy(wl_cmd->key + 16, key->key + 24, 8);
  771. memcpy(wl_cmd->key + 24, key->key + 16, 8);
  772. } else {
  773. memcpy(wl_cmd->key, key->key, key->keylen);
  774. }
  775. wl_cmd->key_size = key->keylen;
  776. wl_cmd->id = key->keyidx;
  777. wl_cmd->ssid_profile = 0;
  778. wl1251_dump(DEBUG_CRYPT, "TARGET KEY: ", wl_cmd, sizeof(*wl_cmd));
  779. ret = wl1251_cmd_send(wl, CMD_SET_KEYS, wl_cmd, sizeof(*wl_cmd));
  780. if (ret < 0) {
  781. wl1251_warning("could not set keys");
  782. goto out_sleep;
  783. }
  784. out_sleep:
  785. wl1251_ps_elp_sleep(wl);
  786. out_unlock:
  787. mutex_unlock(&wl->mutex);
  788. out:
  789. kfree(wl_cmd);
  790. return ret;
  791. }
  792. static int wl1251_op_hw_scan(struct ieee80211_hw *hw,
  793. struct ieee80211_vif *vif,
  794. struct ieee80211_scan_request *hw_req)
  795. {
  796. struct cfg80211_scan_request *req = &hw_req->req;
  797. struct wl1251 *wl = hw->priv;
  798. struct sk_buff *skb;
  799. size_t ssid_len = 0;
  800. u8 *ssid = NULL;
  801. int ret;
  802. wl1251_debug(DEBUG_MAC80211, "mac80211 hw scan");
  803. if (req->n_ssids) {
  804. ssid = req->ssids[0].ssid;
  805. ssid_len = req->ssids[0].ssid_len;
  806. }
  807. mutex_lock(&wl->mutex);
  808. if (wl->scanning) {
  809. wl1251_debug(DEBUG_SCAN, "scan already in progress");
  810. ret = -EINVAL;
  811. goto out;
  812. }
  813. ret = wl1251_ps_elp_wakeup(wl);
  814. if (ret < 0)
  815. goto out;
  816. if (hw->conf.flags & IEEE80211_CONF_IDLE) {
  817. ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
  818. if (ret < 0)
  819. goto out_sleep;
  820. ret = wl1251_join(wl, wl->bss_type, wl->channel,
  821. wl->beacon_int, wl->dtim_period);
  822. if (ret < 0)
  823. goto out_sleep;
  824. }
  825. skb = ieee80211_probereq_get(wl->hw, wl->vif->addr, ssid, ssid_len,
  826. req->ie_len);
  827. if (!skb) {
  828. ret = -ENOMEM;
  829. goto out_idle;
  830. }
  831. if (req->ie_len)
  832. memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
  833. ret = wl1251_cmd_template_set(wl, CMD_PROBE_REQ, skb->data,
  834. skb->len);
  835. dev_kfree_skb(skb);
  836. if (ret < 0)
  837. goto out_idle;
  838. ret = wl1251_cmd_trigger_scan_to(wl, 0);
  839. if (ret < 0)
  840. goto out_idle;
  841. wl->scanning = true;
  842. ret = wl1251_cmd_scan(wl, ssid, ssid_len, req->channels,
  843. req->n_channels, WL1251_SCAN_NUM_PROBES);
  844. if (ret < 0) {
  845. wl1251_debug(DEBUG_SCAN, "scan failed %d", ret);
  846. wl->scanning = false;
  847. goto out_idle;
  848. }
  849. goto out_sleep;
  850. out_idle:
  851. if (hw->conf.flags & IEEE80211_CONF_IDLE)
  852. ret = wl1251_ps_set_mode(wl, STATION_IDLE);
  853. out_sleep:
  854. wl1251_ps_elp_sleep(wl);
  855. out:
  856. mutex_unlock(&wl->mutex);
  857. return ret;
  858. }
  859. static int wl1251_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  860. {
  861. struct wl1251 *wl = hw->priv;
  862. int ret;
  863. mutex_lock(&wl->mutex);
  864. ret = wl1251_ps_elp_wakeup(wl);
  865. if (ret < 0)
  866. goto out;
  867. ret = wl1251_acx_rts_threshold(wl, (u16) value);
  868. if (ret < 0)
  869. wl1251_warning("wl1251_op_set_rts_threshold failed: %d", ret);
  870. wl1251_ps_elp_sleep(wl);
  871. out:
  872. mutex_unlock(&wl->mutex);
  873. return ret;
  874. }
  875. static void wl1251_op_bss_info_changed(struct ieee80211_hw *hw,
  876. struct ieee80211_vif *vif,
  877. struct ieee80211_bss_conf *bss_conf,
  878. u32 changed)
  879. {
  880. struct wl1251 *wl = hw->priv;
  881. struct sk_buff *beacon, *skb;
  882. bool enable;
  883. int ret;
  884. wl1251_debug(DEBUG_MAC80211, "mac80211 bss info changed");
  885. mutex_lock(&wl->mutex);
  886. ret = wl1251_ps_elp_wakeup(wl);
  887. if (ret < 0)
  888. goto out;
  889. if (changed & BSS_CHANGED_CQM) {
  890. ret = wl1251_acx_low_rssi(wl, bss_conf->cqm_rssi_thold,
  891. WL1251_DEFAULT_LOW_RSSI_WEIGHT,
  892. WL1251_DEFAULT_LOW_RSSI_DEPTH,
  893. WL1251_ACX_LOW_RSSI_TYPE_EDGE);
  894. if (ret < 0)
  895. goto out;
  896. wl->rssi_thold = bss_conf->cqm_rssi_thold;
  897. }
  898. if ((changed & BSS_CHANGED_BSSID) &&
  899. memcmp(wl->bssid, bss_conf->bssid, ETH_ALEN)) {
  900. memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
  901. if (!is_zero_ether_addr(wl->bssid)) {
  902. ret = wl1251_build_null_data(wl);
  903. if (ret < 0)
  904. goto out_sleep;
  905. ret = wl1251_build_qos_null_data(wl);
  906. if (ret < 0)
  907. goto out_sleep;
  908. ret = wl1251_join(wl, wl->bss_type, wl->channel,
  909. wl->beacon_int, wl->dtim_period);
  910. if (ret < 0)
  911. goto out_sleep;
  912. }
  913. }
  914. if (changed & BSS_CHANGED_ASSOC) {
  915. if (bss_conf->assoc) {
  916. wl->beacon_int = bss_conf->beacon_int;
  917. skb = ieee80211_pspoll_get(wl->hw, wl->vif);
  918. if (!skb)
  919. goto out_sleep;
  920. ret = wl1251_cmd_template_set(wl, CMD_PS_POLL,
  921. skb->data,
  922. skb->len);
  923. dev_kfree_skb(skb);
  924. if (ret < 0)
  925. goto out_sleep;
  926. ret = wl1251_acx_aid(wl, bss_conf->aid);
  927. if (ret < 0)
  928. goto out_sleep;
  929. } else {
  930. /* use defaults when not associated */
  931. wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
  932. wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
  933. }
  934. }
  935. if (changed & BSS_CHANGED_ERP_SLOT) {
  936. if (bss_conf->use_short_slot)
  937. ret = wl1251_acx_slot(wl, SLOT_TIME_SHORT);
  938. else
  939. ret = wl1251_acx_slot(wl, SLOT_TIME_LONG);
  940. if (ret < 0) {
  941. wl1251_warning("Set slot time failed %d", ret);
  942. goto out_sleep;
  943. }
  944. }
  945. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  946. if (bss_conf->use_short_preamble)
  947. wl1251_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
  948. else
  949. wl1251_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
  950. }
  951. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  952. if (bss_conf->use_cts_prot)
  953. ret = wl1251_acx_cts_protect(wl, CTSPROTECT_ENABLE);
  954. else
  955. ret = wl1251_acx_cts_protect(wl, CTSPROTECT_DISABLE);
  956. if (ret < 0) {
  957. wl1251_warning("Set ctsprotect failed %d", ret);
  958. goto out_sleep;
  959. }
  960. }
  961. if (changed & BSS_CHANGED_ARP_FILTER) {
  962. __be32 addr = bss_conf->arp_addr_list[0];
  963. WARN_ON(wl->bss_type != BSS_TYPE_STA_BSS);
  964. enable = bss_conf->arp_addr_cnt == 1 && bss_conf->assoc;
  965. wl1251_acx_arp_ip_filter(wl, enable, addr);
  966. if (ret < 0)
  967. goto out_sleep;
  968. }
  969. if (changed & BSS_CHANGED_BEACON) {
  970. beacon = ieee80211_beacon_get(hw, vif);
  971. if (!beacon)
  972. goto out_sleep;
  973. ret = wl1251_cmd_template_set(wl, CMD_BEACON, beacon->data,
  974. beacon->len);
  975. if (ret < 0) {
  976. dev_kfree_skb(beacon);
  977. goto out_sleep;
  978. }
  979. ret = wl1251_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
  980. beacon->len);
  981. dev_kfree_skb(beacon);
  982. if (ret < 0)
  983. goto out_sleep;
  984. ret = wl1251_join(wl, wl->bss_type, wl->channel,
  985. wl->beacon_int, wl->dtim_period);
  986. if (ret < 0)
  987. goto out_sleep;
  988. }
  989. out_sleep:
  990. wl1251_ps_elp_sleep(wl);
  991. out:
  992. mutex_unlock(&wl->mutex);
  993. }
  994. /* can't be const, mac80211 writes to this */
  995. static struct ieee80211_rate wl1251_rates[] = {
  996. { .bitrate = 10,
  997. .hw_value = 0x1,
  998. .hw_value_short = 0x1, },
  999. { .bitrate = 20,
  1000. .hw_value = 0x2,
  1001. .hw_value_short = 0x2,
  1002. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  1003. { .bitrate = 55,
  1004. .hw_value = 0x4,
  1005. .hw_value_short = 0x4,
  1006. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  1007. { .bitrate = 110,
  1008. .hw_value = 0x20,
  1009. .hw_value_short = 0x20,
  1010. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  1011. { .bitrate = 60,
  1012. .hw_value = 0x8,
  1013. .hw_value_short = 0x8, },
  1014. { .bitrate = 90,
  1015. .hw_value = 0x10,
  1016. .hw_value_short = 0x10, },
  1017. { .bitrate = 120,
  1018. .hw_value = 0x40,
  1019. .hw_value_short = 0x40, },
  1020. { .bitrate = 180,
  1021. .hw_value = 0x80,
  1022. .hw_value_short = 0x80, },
  1023. { .bitrate = 240,
  1024. .hw_value = 0x200,
  1025. .hw_value_short = 0x200, },
  1026. { .bitrate = 360,
  1027. .hw_value = 0x400,
  1028. .hw_value_short = 0x400, },
  1029. { .bitrate = 480,
  1030. .hw_value = 0x800,
  1031. .hw_value_short = 0x800, },
  1032. { .bitrate = 540,
  1033. .hw_value = 0x1000,
  1034. .hw_value_short = 0x1000, },
  1035. };
  1036. /* can't be const, mac80211 writes to this */
  1037. static struct ieee80211_channel wl1251_channels[] = {
  1038. { .hw_value = 1, .center_freq = 2412},
  1039. { .hw_value = 2, .center_freq = 2417},
  1040. { .hw_value = 3, .center_freq = 2422},
  1041. { .hw_value = 4, .center_freq = 2427},
  1042. { .hw_value = 5, .center_freq = 2432},
  1043. { .hw_value = 6, .center_freq = 2437},
  1044. { .hw_value = 7, .center_freq = 2442},
  1045. { .hw_value = 8, .center_freq = 2447},
  1046. { .hw_value = 9, .center_freq = 2452},
  1047. { .hw_value = 10, .center_freq = 2457},
  1048. { .hw_value = 11, .center_freq = 2462},
  1049. { .hw_value = 12, .center_freq = 2467},
  1050. { .hw_value = 13, .center_freq = 2472},
  1051. };
  1052. static int wl1251_op_conf_tx(struct ieee80211_hw *hw,
  1053. struct ieee80211_vif *vif, u16 queue,
  1054. const struct ieee80211_tx_queue_params *params)
  1055. {
  1056. enum wl1251_acx_ps_scheme ps_scheme;
  1057. struct wl1251 *wl = hw->priv;
  1058. int ret;
  1059. mutex_lock(&wl->mutex);
  1060. wl1251_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
  1061. ret = wl1251_ps_elp_wakeup(wl);
  1062. if (ret < 0)
  1063. goto out;
  1064. /* mac80211 uses units of 32 usec */
  1065. ret = wl1251_acx_ac_cfg(wl, wl1251_tx_get_queue(queue),
  1066. params->cw_min, params->cw_max,
  1067. params->aifs, params->txop * 32);
  1068. if (ret < 0)
  1069. goto out_sleep;
  1070. if (params->uapsd)
  1071. ps_scheme = WL1251_ACX_PS_SCHEME_UPSD_TRIGGER;
  1072. else
  1073. ps_scheme = WL1251_ACX_PS_SCHEME_LEGACY;
  1074. ret = wl1251_acx_tid_cfg(wl, wl1251_tx_get_queue(queue),
  1075. CHANNEL_TYPE_EDCF,
  1076. wl1251_tx_get_queue(queue), ps_scheme,
  1077. WL1251_ACX_ACK_POLICY_LEGACY);
  1078. if (ret < 0)
  1079. goto out_sleep;
  1080. out_sleep:
  1081. wl1251_ps_elp_sleep(wl);
  1082. out:
  1083. mutex_unlock(&wl->mutex);
  1084. return ret;
  1085. }
  1086. static int wl1251_op_get_survey(struct ieee80211_hw *hw, int idx,
  1087. struct survey_info *survey)
  1088. {
  1089. struct wl1251 *wl = hw->priv;
  1090. struct ieee80211_conf *conf = &hw->conf;
  1091. if (idx != 0)
  1092. return -ENOENT;
  1093. survey->channel = conf->chandef.chan;
  1094. survey->filled = SURVEY_INFO_NOISE_DBM;
  1095. survey->noise = wl->noise;
  1096. return 0;
  1097. }
  1098. /* can't be const, mac80211 writes to this */
  1099. static struct ieee80211_supported_band wl1251_band_2ghz = {
  1100. .channels = wl1251_channels,
  1101. .n_channels = ARRAY_SIZE(wl1251_channels),
  1102. .bitrates = wl1251_rates,
  1103. .n_bitrates = ARRAY_SIZE(wl1251_rates),
  1104. };
  1105. static const struct ieee80211_ops wl1251_ops = {
  1106. .start = wl1251_op_start,
  1107. .stop = wl1251_op_stop,
  1108. .add_interface = wl1251_op_add_interface,
  1109. .remove_interface = wl1251_op_remove_interface,
  1110. .config = wl1251_op_config,
  1111. .prepare_multicast = wl1251_op_prepare_multicast,
  1112. .configure_filter = wl1251_op_configure_filter,
  1113. .tx = wl1251_op_tx,
  1114. .set_key = wl1251_op_set_key,
  1115. .hw_scan = wl1251_op_hw_scan,
  1116. .bss_info_changed = wl1251_op_bss_info_changed,
  1117. .set_rts_threshold = wl1251_op_set_rts_threshold,
  1118. .conf_tx = wl1251_op_conf_tx,
  1119. .get_survey = wl1251_op_get_survey,
  1120. };
  1121. static int wl1251_read_eeprom_byte(struct wl1251 *wl, off_t offset, u8 *data)
  1122. {
  1123. unsigned long timeout;
  1124. wl1251_reg_write32(wl, EE_ADDR, offset);
  1125. wl1251_reg_write32(wl, EE_CTL, EE_CTL_READ);
  1126. /* EE_CTL_READ clears when data is ready */
  1127. timeout = jiffies + msecs_to_jiffies(100);
  1128. while (1) {
  1129. if (!(wl1251_reg_read32(wl, EE_CTL) & EE_CTL_READ))
  1130. break;
  1131. if (time_after(jiffies, timeout))
  1132. return -ETIMEDOUT;
  1133. msleep(1);
  1134. }
  1135. *data = wl1251_reg_read32(wl, EE_DATA);
  1136. return 0;
  1137. }
  1138. static int wl1251_read_eeprom(struct wl1251 *wl, off_t offset,
  1139. u8 *data, size_t len)
  1140. {
  1141. size_t i;
  1142. int ret;
  1143. wl1251_reg_write32(wl, EE_START, 0);
  1144. for (i = 0; i < len; i++) {
  1145. ret = wl1251_read_eeprom_byte(wl, offset + i, &data[i]);
  1146. if (ret < 0)
  1147. return ret;
  1148. }
  1149. return 0;
  1150. }
  1151. static int wl1251_read_eeprom_mac(struct wl1251 *wl)
  1152. {
  1153. u8 mac[ETH_ALEN];
  1154. int i, ret;
  1155. wl1251_set_partition(wl, 0, 0, REGISTERS_BASE, REGISTERS_DOWN_SIZE);
  1156. ret = wl1251_read_eeprom(wl, 0x1c, mac, sizeof(mac));
  1157. if (ret < 0) {
  1158. wl1251_warning("failed to read MAC address from EEPROM");
  1159. return ret;
  1160. }
  1161. /* MAC is stored in reverse order */
  1162. for (i = 0; i < ETH_ALEN; i++)
  1163. wl->mac_addr[i] = mac[ETH_ALEN - i - 1];
  1164. return 0;
  1165. }
  1166. static int wl1251_register_hw(struct wl1251 *wl)
  1167. {
  1168. int ret;
  1169. if (wl->mac80211_registered)
  1170. return 0;
  1171. SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
  1172. ret = ieee80211_register_hw(wl->hw);
  1173. if (ret < 0) {
  1174. wl1251_error("unable to register mac80211 hw: %d", ret);
  1175. return ret;
  1176. }
  1177. wl->mac80211_registered = true;
  1178. wl1251_notice("loaded");
  1179. return 0;
  1180. }
  1181. int wl1251_init_ieee80211(struct wl1251 *wl)
  1182. {
  1183. int ret;
  1184. /* The tx descriptor buffer and the TKIP space */
  1185. wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
  1186. + WL1251_TKIP_IV_SPACE;
  1187. /* unit us */
  1188. /* FIXME: find a proper value */
  1189. ieee80211_hw_set(wl->hw, SIGNAL_DBM);
  1190. ieee80211_hw_set(wl->hw, SUPPORTS_PS);
  1191. wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  1192. BIT(NL80211_IFTYPE_ADHOC);
  1193. wl->hw->wiphy->max_scan_ssids = 1;
  1194. wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1251_band_2ghz;
  1195. wl->hw->queues = 4;
  1196. if (wl->use_eeprom)
  1197. wl1251_read_eeprom_mac(wl);
  1198. ret = wl1251_register_hw(wl);
  1199. if (ret)
  1200. goto out;
  1201. wl1251_debugfs_init(wl);
  1202. wl1251_notice("initialized");
  1203. ret = 0;
  1204. out:
  1205. return ret;
  1206. }
  1207. EXPORT_SYMBOL_GPL(wl1251_init_ieee80211);
  1208. struct ieee80211_hw *wl1251_alloc_hw(void)
  1209. {
  1210. struct ieee80211_hw *hw;
  1211. struct wl1251 *wl;
  1212. int i;
  1213. static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
  1214. hw = ieee80211_alloc_hw(sizeof(*wl), &wl1251_ops);
  1215. if (!hw) {
  1216. wl1251_error("could not alloc ieee80211_hw");
  1217. return ERR_PTR(-ENOMEM);
  1218. }
  1219. wl = hw->priv;
  1220. memset(wl, 0, sizeof(*wl));
  1221. wl->hw = hw;
  1222. wl->data_in_count = 0;
  1223. skb_queue_head_init(&wl->tx_queue);
  1224. INIT_DELAYED_WORK(&wl->elp_work, wl1251_elp_work);
  1225. wl->channel = WL1251_DEFAULT_CHANNEL;
  1226. wl->monitor_present = false;
  1227. wl->joined = false;
  1228. wl->scanning = false;
  1229. wl->bss_type = MAX_BSS_TYPE;
  1230. wl->default_key = 0;
  1231. wl->listen_int = 1;
  1232. wl->rx_counter = 0;
  1233. wl->rx_handled = 0;
  1234. wl->rx_current_buffer = 0;
  1235. wl->rx_last_id = 0;
  1236. wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
  1237. wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
  1238. wl->elp = false;
  1239. wl->station_mode = STATION_ACTIVE_MODE;
  1240. wl->psm_requested = false;
  1241. wl->psm_entry_retry = 0;
  1242. wl->tx_queue_stopped = false;
  1243. wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
  1244. wl->rssi_thold = 0;
  1245. wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
  1246. wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
  1247. wl->vif = NULL;
  1248. for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
  1249. wl->tx_frames[i] = NULL;
  1250. wl->next_tx_complete = 0;
  1251. INIT_WORK(&wl->irq_work, wl1251_irq_work);
  1252. INIT_WORK(&wl->tx_work, wl1251_tx_work);
  1253. /*
  1254. * In case our MAC address is not correctly set,
  1255. * we use a random but Nokia MAC.
  1256. */
  1257. memcpy(wl->mac_addr, nokia_oui, 3);
  1258. get_random_bytes(wl->mac_addr + 3, 3);
  1259. wl->state = WL1251_STATE_OFF;
  1260. mutex_init(&wl->mutex);
  1261. wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
  1262. wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
  1263. wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
  1264. if (!wl->rx_descriptor) {
  1265. wl1251_error("could not allocate memory for rx descriptor");
  1266. ieee80211_free_hw(hw);
  1267. return ERR_PTR(-ENOMEM);
  1268. }
  1269. return hw;
  1270. }
  1271. EXPORT_SYMBOL_GPL(wl1251_alloc_hw);
  1272. int wl1251_free_hw(struct wl1251 *wl)
  1273. {
  1274. ieee80211_unregister_hw(wl->hw);
  1275. wl1251_debugfs_exit(wl);
  1276. kfree(wl->target_mem_map);
  1277. kfree(wl->data_path);
  1278. vfree(wl->fw);
  1279. wl->fw = NULL;
  1280. kfree(wl->nvs);
  1281. wl->nvs = NULL;
  1282. kfree(wl->rx_descriptor);
  1283. wl->rx_descriptor = NULL;
  1284. ieee80211_free_hw(wl->hw);
  1285. return 0;
  1286. }
  1287. EXPORT_SYMBOL_GPL(wl1251_free_hw);
  1288. MODULE_DESCRIPTION("TI wl1251 Wireless LAN Driver Core");
  1289. MODULE_LICENSE("GPL");
  1290. MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");
  1291. MODULE_FIRMWARE(WL1251_FW_NAME);
  1292. MODULE_FIRMWARE(WL1251_NVS_NAME);