main.c 37 KB

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