main.c 36 KB

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