PageRenderTime 80ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/net/wireless/wl12xx/main.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2
C | 4095 lines | 3042 code | 698 blank | 355 comment | 458 complexity | f019852059d6ce130b8d50f428d81163 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * This file is part of wl1271
  3. *
  4. * Copyright (C) 2008-2010 Nokia Corporation
  5. *
  6. * Contact: Luciano Coelho <luciano.coelho@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/firmware.h>
  25. #include <linux/delay.h>
  26. #include <linux/spi/spi.h>
  27. #include <linux/crc32.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/slab.h>
  32. #include <linux/wl12xx.h>
  33. #include "wl12xx.h"
  34. #include "wl12xx_80211.h"
  35. #include "reg.h"
  36. #include "io.h"
  37. #include "event.h"
  38. #include "tx.h"
  39. #include "rx.h"
  40. #include "ps.h"
  41. #include "init.h"
  42. #include "debugfs.h"
  43. #include "cmd.h"
  44. #include "boot.h"
  45. #include "testmode.h"
  46. #include "scan.h"
  47. #define WL1271_BOOT_RETRIES 3
  48. static struct conf_drv_settings default_conf = {
  49. .sg = {
  50. .sta_params = {
  51. [CONF_SG_BT_PER_THRESHOLD] = 7500,
  52. [CONF_SG_HV3_MAX_OVERRIDE] = 0,
  53. [CONF_SG_BT_NFS_SAMPLE_INTERVAL] = 400,
  54. [CONF_SG_BT_LOAD_RATIO] = 200,
  55. [CONF_SG_AUTO_PS_MODE] = 1,
  56. [CONF_SG_AUTO_SCAN_PROBE_REQ] = 170,
  57. [CONF_SG_ACTIVE_SCAN_DURATION_FACTOR_HV3] = 50,
  58. [CONF_SG_ANTENNA_CONFIGURATION] = 0,
  59. [CONF_SG_BEACON_MISS_PERCENT] = 60,
  60. [CONF_SG_RATE_ADAPT_THRESH] = 12,
  61. [CONF_SG_RATE_ADAPT_SNR] = 0,
  62. [CONF_SG_WLAN_PS_BT_ACL_MASTER_MIN_BR] = 10,
  63. [CONF_SG_WLAN_PS_BT_ACL_MASTER_MAX_BR] = 30,
  64. [CONF_SG_WLAN_PS_MAX_BT_ACL_MASTER_BR] = 8,
  65. [CONF_SG_WLAN_PS_BT_ACL_SLAVE_MIN_BR] = 20,
  66. [CONF_SG_WLAN_PS_BT_ACL_SLAVE_MAX_BR] = 50,
  67. /* Note: with UPSD, this should be 4 */
  68. [CONF_SG_WLAN_PS_MAX_BT_ACL_SLAVE_BR] = 8,
  69. [CONF_SG_WLAN_PS_BT_ACL_MASTER_MIN_EDR] = 7,
  70. [CONF_SG_WLAN_PS_BT_ACL_MASTER_MAX_EDR] = 25,
  71. [CONF_SG_WLAN_PS_MAX_BT_ACL_MASTER_EDR] = 20,
  72. /* Note: with UPDS, this should be 15 */
  73. [CONF_SG_WLAN_PS_BT_ACL_SLAVE_MIN_EDR] = 8,
  74. /* Note: with UPDS, this should be 50 */
  75. [CONF_SG_WLAN_PS_BT_ACL_SLAVE_MAX_EDR] = 40,
  76. /* Note: with UPDS, this should be 10 */
  77. [CONF_SG_WLAN_PS_MAX_BT_ACL_SLAVE_EDR] = 20,
  78. [CONF_SG_RXT] = 1200,
  79. [CONF_SG_TXT] = 1000,
  80. [CONF_SG_ADAPTIVE_RXT_TXT] = 1,
  81. [CONF_SG_PS_POLL_TIMEOUT] = 10,
  82. [CONF_SG_UPSD_TIMEOUT] = 10,
  83. [CONF_SG_WLAN_ACTIVE_BT_ACL_MASTER_MIN_EDR] = 7,
  84. [CONF_SG_WLAN_ACTIVE_BT_ACL_MASTER_MAX_EDR] = 15,
  85. [CONF_SG_WLAN_ACTIVE_MAX_BT_ACL_MASTER_EDR] = 15,
  86. [CONF_SG_WLAN_ACTIVE_BT_ACL_SLAVE_MIN_EDR] = 8,
  87. [CONF_SG_WLAN_ACTIVE_BT_ACL_SLAVE_MAX_EDR] = 20,
  88. [CONF_SG_WLAN_ACTIVE_MAX_BT_ACL_SLAVE_EDR] = 15,
  89. [CONF_SG_WLAN_ACTIVE_BT_ACL_MIN_BR] = 20,
  90. [CONF_SG_WLAN_ACTIVE_BT_ACL_MAX_BR] = 50,
  91. [CONF_SG_WLAN_ACTIVE_MAX_BT_ACL_BR] = 10,
  92. [CONF_SG_PASSIVE_SCAN_DURATION_FACTOR_HV3] = 200,
  93. [CONF_SG_PASSIVE_SCAN_DURATION_FACTOR_A2DP] = 800,
  94. [CONF_SG_PASSIVE_SCAN_A2DP_BT_TIME] = 75,
  95. [CONF_SG_PASSIVE_SCAN_A2DP_WLAN_TIME] = 15,
  96. [CONF_SG_HV3_MAX_SERVED] = 6,
  97. [CONF_SG_DHCP_TIME] = 5000,
  98. [CONF_SG_ACTIVE_SCAN_DURATION_FACTOR_A2DP] = 100,
  99. },
  100. .ap_params = {
  101. [CONF_SG_BT_PER_THRESHOLD] = 7500,
  102. [CONF_SG_HV3_MAX_OVERRIDE] = 0,
  103. [CONF_SG_BT_NFS_SAMPLE_INTERVAL] = 400,
  104. [CONF_SG_BT_LOAD_RATIO] = 50,
  105. [CONF_SG_AUTO_PS_MODE] = 1,
  106. [CONF_SG_AUTO_SCAN_PROBE_REQ] = 170,
  107. [CONF_SG_ACTIVE_SCAN_DURATION_FACTOR_HV3] = 50,
  108. [CONF_SG_ANTENNA_CONFIGURATION] = 0,
  109. [CONF_SG_BEACON_MISS_PERCENT] = 60,
  110. [CONF_SG_RATE_ADAPT_THRESH] = 64,
  111. [CONF_SG_RATE_ADAPT_SNR] = 1,
  112. [CONF_SG_WLAN_PS_BT_ACL_MASTER_MIN_BR] = 10,
  113. [CONF_SG_WLAN_PS_BT_ACL_MASTER_MAX_BR] = 25,
  114. [CONF_SG_WLAN_PS_MAX_BT_ACL_MASTER_BR] = 25,
  115. [CONF_SG_WLAN_PS_BT_ACL_SLAVE_MIN_BR] = 20,
  116. [CONF_SG_WLAN_PS_BT_ACL_SLAVE_MAX_BR] = 25,
  117. [CONF_SG_WLAN_PS_MAX_BT_ACL_SLAVE_BR] = 25,
  118. [CONF_SG_WLAN_PS_BT_ACL_MASTER_MIN_EDR] = 7,
  119. [CONF_SG_WLAN_PS_BT_ACL_MASTER_MAX_EDR] = 25,
  120. [CONF_SG_WLAN_PS_MAX_BT_ACL_MASTER_EDR] = 25,
  121. [CONF_SG_WLAN_PS_BT_ACL_SLAVE_MIN_EDR] = 8,
  122. [CONF_SG_WLAN_PS_BT_ACL_SLAVE_MAX_EDR] = 25,
  123. [CONF_SG_WLAN_PS_MAX_BT_ACL_SLAVE_EDR] = 25,
  124. [CONF_SG_RXT] = 1200,
  125. [CONF_SG_TXT] = 1000,
  126. [CONF_SG_ADAPTIVE_RXT_TXT] = 1,
  127. [CONF_SG_PS_POLL_TIMEOUT] = 10,
  128. [CONF_SG_UPSD_TIMEOUT] = 10,
  129. [CONF_SG_WLAN_ACTIVE_BT_ACL_MASTER_MIN_EDR] = 7,
  130. [CONF_SG_WLAN_ACTIVE_BT_ACL_MASTER_MAX_EDR] = 15,
  131. [CONF_SG_WLAN_ACTIVE_MAX_BT_ACL_MASTER_EDR] = 15,
  132. [CONF_SG_WLAN_ACTIVE_BT_ACL_SLAVE_MIN_EDR] = 8,
  133. [CONF_SG_WLAN_ACTIVE_BT_ACL_SLAVE_MAX_EDR] = 20,
  134. [CONF_SG_WLAN_ACTIVE_MAX_BT_ACL_SLAVE_EDR] = 15,
  135. [CONF_SG_WLAN_ACTIVE_BT_ACL_MIN_BR] = 20,
  136. [CONF_SG_WLAN_ACTIVE_BT_ACL_MAX_BR] = 50,
  137. [CONF_SG_WLAN_ACTIVE_MAX_BT_ACL_BR] = 10,
  138. [CONF_SG_PASSIVE_SCAN_DURATION_FACTOR_HV3] = 200,
  139. [CONF_SG_PASSIVE_SCAN_DURATION_FACTOR_A2DP] = 800,
  140. [CONF_SG_PASSIVE_SCAN_A2DP_BT_TIME] = 75,
  141. [CONF_SG_PASSIVE_SCAN_A2DP_WLAN_TIME] = 15,
  142. [CONF_SG_HV3_MAX_SERVED] = 6,
  143. [CONF_SG_DHCP_TIME] = 5000,
  144. [CONF_SG_ACTIVE_SCAN_DURATION_FACTOR_A2DP] = 100,
  145. [CONF_SG_TEMP_PARAM_1] = 0,
  146. [CONF_SG_TEMP_PARAM_2] = 0,
  147. [CONF_SG_TEMP_PARAM_3] = 0,
  148. [CONF_SG_TEMP_PARAM_4] = 0,
  149. [CONF_SG_TEMP_PARAM_5] = 0,
  150. [CONF_SG_AP_BEACON_MISS_TX] = 3,
  151. [CONF_SG_RX_WINDOW_LENGTH] = 6,
  152. [CONF_SG_AP_CONNECTION_PROTECTION_TIME] = 50,
  153. [CONF_SG_TEMP_PARAM_6] = 1,
  154. },
  155. .state = CONF_SG_PROTECTIVE,
  156. },
  157. .rx = {
  158. .rx_msdu_life_time = 512000,
  159. .packet_detection_threshold = 0,
  160. .ps_poll_timeout = 15,
  161. .upsd_timeout = 15,
  162. .rts_threshold = IEEE80211_MAX_RTS_THRESHOLD,
  163. .rx_cca_threshold = 0,
  164. .irq_blk_threshold = 0xFFFF,
  165. .irq_pkt_threshold = 0,
  166. .irq_timeout = 600,
  167. .queue_type = CONF_RX_QUEUE_TYPE_LOW_PRIORITY,
  168. },
  169. .tx = {
  170. .tx_energy_detection = 0,
  171. .sta_rc_conf = {
  172. .enabled_rates = 0,
  173. .short_retry_limit = 10,
  174. .long_retry_limit = 10,
  175. .aflags = 0,
  176. },
  177. .ac_conf_count = 4,
  178. .ac_conf = {
  179. [CONF_TX_AC_BE] = {
  180. .ac = CONF_TX_AC_BE,
  181. .cw_min = 15,
  182. .cw_max = 63,
  183. .aifsn = 3,
  184. .tx_op_limit = 0,
  185. },
  186. [CONF_TX_AC_BK] = {
  187. .ac = CONF_TX_AC_BK,
  188. .cw_min = 15,
  189. .cw_max = 63,
  190. .aifsn = 7,
  191. .tx_op_limit = 0,
  192. },
  193. [CONF_TX_AC_VI] = {
  194. .ac = CONF_TX_AC_VI,
  195. .cw_min = 15,
  196. .cw_max = 63,
  197. .aifsn = CONF_TX_AIFS_PIFS,
  198. .tx_op_limit = 3008,
  199. },
  200. [CONF_TX_AC_VO] = {
  201. .ac = CONF_TX_AC_VO,
  202. .cw_min = 15,
  203. .cw_max = 63,
  204. .aifsn = CONF_TX_AIFS_PIFS,
  205. .tx_op_limit = 1504,
  206. },
  207. },
  208. .ap_max_tx_retries = 100,
  209. .tid_conf_count = 4,
  210. .tid_conf = {
  211. [CONF_TX_AC_BE] = {
  212. .queue_id = CONF_TX_AC_BE,
  213. .channel_type = CONF_CHANNEL_TYPE_EDCF,
  214. .tsid = CONF_TX_AC_BE,
  215. .ps_scheme = CONF_PS_SCHEME_LEGACY,
  216. .ack_policy = CONF_ACK_POLICY_LEGACY,
  217. .apsd_conf = {0, 0},
  218. },
  219. [CONF_TX_AC_BK] = {
  220. .queue_id = CONF_TX_AC_BK,
  221. .channel_type = CONF_CHANNEL_TYPE_EDCF,
  222. .tsid = CONF_TX_AC_BK,
  223. .ps_scheme = CONF_PS_SCHEME_LEGACY,
  224. .ack_policy = CONF_ACK_POLICY_LEGACY,
  225. .apsd_conf = {0, 0},
  226. },
  227. [CONF_TX_AC_VI] = {
  228. .queue_id = CONF_TX_AC_VI,
  229. .channel_type = CONF_CHANNEL_TYPE_EDCF,
  230. .tsid = CONF_TX_AC_VI,
  231. .ps_scheme = CONF_PS_SCHEME_LEGACY,
  232. .ack_policy = CONF_ACK_POLICY_LEGACY,
  233. .apsd_conf = {0, 0},
  234. },
  235. [CONF_TX_AC_VO] = {
  236. .queue_id = CONF_TX_AC_VO,
  237. .channel_type = CONF_CHANNEL_TYPE_EDCF,
  238. .tsid = CONF_TX_AC_VO,
  239. .ps_scheme = CONF_PS_SCHEME_LEGACY,
  240. .ack_policy = CONF_ACK_POLICY_LEGACY,
  241. .apsd_conf = {0, 0},
  242. },
  243. },
  244. .frag_threshold = IEEE80211_MAX_FRAG_THRESHOLD,
  245. .tx_compl_timeout = 700,
  246. .tx_compl_threshold = 4,
  247. .basic_rate = CONF_HW_BIT_RATE_1MBPS,
  248. .basic_rate_5 = CONF_HW_BIT_RATE_6MBPS,
  249. .tmpl_short_retry_limit = 10,
  250. .tmpl_long_retry_limit = 10,
  251. },
  252. .conn = {
  253. .wake_up_event = CONF_WAKE_UP_EVENT_DTIM,
  254. .listen_interval = 1,
  255. .bcn_filt_mode = CONF_BCN_FILT_MODE_ENABLED,
  256. .bcn_filt_ie_count = 2,
  257. .bcn_filt_ie = {
  258. [0] = {
  259. .ie = WLAN_EID_CHANNEL_SWITCH,
  260. .rule = CONF_BCN_RULE_PASS_ON_APPEARANCE,
  261. },
  262. [1] = {
  263. .ie = WLAN_EID_HT_INFORMATION,
  264. .rule = CONF_BCN_RULE_PASS_ON_CHANGE,
  265. },
  266. },
  267. .synch_fail_thold = 10,
  268. .bss_lose_timeout = 100,
  269. .beacon_rx_timeout = 10000,
  270. .broadcast_timeout = 20000,
  271. .rx_broadcast_in_ps = 1,
  272. .ps_poll_threshold = 10,
  273. .ps_poll_recovery_period = 700,
  274. .bet_enable = CONF_BET_MODE_ENABLE,
  275. .bet_max_consecutive = 50,
  276. .psm_entry_retries = 5,
  277. .psm_exit_retries = 16,
  278. .psm_entry_nullfunc_retries = 3,
  279. .psm_entry_hangover_period = 1,
  280. .keep_alive_interval = 55000,
  281. .max_listen_interval = 20,
  282. },
  283. .itrim = {
  284. .enable = false,
  285. .timeout = 50000,
  286. },
  287. .pm_config = {
  288. .host_clk_settling_time = 5000,
  289. .host_fast_wakeup_support = false
  290. },
  291. .roam_trigger = {
  292. .trigger_pacing = 1,
  293. .avg_weight_rssi_beacon = 20,
  294. .avg_weight_rssi_data = 10,
  295. .avg_weight_snr_beacon = 20,
  296. .avg_weight_snr_data = 10,
  297. },
  298. .scan = {
  299. .min_dwell_time_active = 7500,
  300. .max_dwell_time_active = 30000,
  301. .min_dwell_time_passive = 100000,
  302. .max_dwell_time_passive = 100000,
  303. .num_probe_reqs = 2,
  304. },
  305. .sched_scan = {
  306. /* sched_scan requires dwell times in TU instead of TU/1000 */
  307. .min_dwell_time_active = 8,
  308. .max_dwell_time_active = 30,
  309. .dwell_time_passive = 100,
  310. .dwell_time_dfs = 150,
  311. .num_probe_reqs = 2,
  312. .rssi_threshold = -90,
  313. .snr_threshold = 0,
  314. },
  315. .rf = {
  316. .tx_per_channel_power_compensation_2 = {
  317. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  318. },
  319. .tx_per_channel_power_compensation_5 = {
  320. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  321. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  322. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  323. },
  324. },
  325. .ht = {
  326. .tx_ba_win_size = 64,
  327. .inactivity_timeout = 10000,
  328. },
  329. .mem_wl127x = {
  330. .num_stations = 1,
  331. .ssid_profiles = 1,
  332. .rx_block_num = 70,
  333. .tx_min_block_num = 40,
  334. .dynamic_memory = 1,
  335. .min_req_tx_blocks = 100,
  336. .min_req_rx_blocks = 22,
  337. .tx_min = 27,
  338. },
  339. .mem_wl128x = {
  340. .num_stations = 1,
  341. .ssid_profiles = 1,
  342. .rx_block_num = 40,
  343. .tx_min_block_num = 40,
  344. .dynamic_memory = 1,
  345. .min_req_tx_blocks = 45,
  346. .min_req_rx_blocks = 22,
  347. .tx_min = 27,
  348. },
  349. .fm_coex = {
  350. .enable = true,
  351. .swallow_period = 5,
  352. .n_divider_fref_set_1 = 0xff, /* default */
  353. .n_divider_fref_set_2 = 12,
  354. .m_divider_fref_set_1 = 148,
  355. .m_divider_fref_set_2 = 0xffff, /* default */
  356. .coex_pll_stabilization_time = 0xffffffff, /* default */
  357. .ldo_stabilization_time = 0xffff, /* default */
  358. .fm_disturbed_band_margin = 0xff, /* default */
  359. .swallow_clk_diff = 0xff, /* default */
  360. },
  361. .hci_io_ds = HCI_IO_DS_6MA,
  362. };
  363. static void __wl1271_op_remove_interface(struct wl1271 *wl,
  364. bool reset_tx_queues);
  365. static void wl1271_free_ap_keys(struct wl1271 *wl);
  366. static void wl1271_device_release(struct device *dev)
  367. {
  368. }
  369. static struct platform_device wl1271_device = {
  370. .name = "wl1271",
  371. .id = -1,
  372. /* device model insists to have a release function */
  373. .dev = {
  374. .release = wl1271_device_release,
  375. },
  376. };
  377. static DEFINE_MUTEX(wl_list_mutex);
  378. static LIST_HEAD(wl_list);
  379. static int wl1271_dev_notify(struct notifier_block *me, unsigned long what,
  380. void *arg)
  381. {
  382. struct net_device *dev = arg;
  383. struct wireless_dev *wdev;
  384. struct wiphy *wiphy;
  385. struct ieee80211_hw *hw;
  386. struct wl1271 *wl;
  387. struct wl1271 *wl_temp;
  388. int ret = 0;
  389. /* Check that this notification is for us. */
  390. if (what != NETDEV_CHANGE)
  391. return NOTIFY_DONE;
  392. wdev = dev->ieee80211_ptr;
  393. if (wdev == NULL)
  394. return NOTIFY_DONE;
  395. wiphy = wdev->wiphy;
  396. if (wiphy == NULL)
  397. return NOTIFY_DONE;
  398. hw = wiphy_priv(wiphy);
  399. if (hw == NULL)
  400. return NOTIFY_DONE;
  401. wl_temp = hw->priv;
  402. mutex_lock(&wl_list_mutex);
  403. list_for_each_entry(wl, &wl_list, list) {
  404. if (wl == wl_temp)
  405. break;
  406. }
  407. mutex_unlock(&wl_list_mutex);
  408. if (wl != wl_temp)
  409. return NOTIFY_DONE;
  410. mutex_lock(&wl->mutex);
  411. if (wl->state == WL1271_STATE_OFF)
  412. goto out;
  413. if (!test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags))
  414. goto out;
  415. ret = wl1271_ps_elp_wakeup(wl);
  416. if (ret < 0)
  417. goto out;
  418. if ((dev->operstate == IF_OPER_UP) &&
  419. !test_and_set_bit(WL1271_FLAG_STA_STATE_SENT, &wl->flags)) {
  420. wl1271_cmd_set_sta_state(wl);
  421. wl1271_info("Association completed.");
  422. }
  423. wl1271_ps_elp_sleep(wl);
  424. out:
  425. mutex_unlock(&wl->mutex);
  426. return NOTIFY_OK;
  427. }
  428. static int wl1271_reg_notify(struct wiphy *wiphy,
  429. struct regulatory_request *request)
  430. {
  431. struct ieee80211_supported_band *band;
  432. struct ieee80211_channel *ch;
  433. int i;
  434. band = wiphy->bands[IEEE80211_BAND_5GHZ];
  435. for (i = 0; i < band->n_channels; i++) {
  436. ch = &band->channels[i];
  437. if (ch->flags & IEEE80211_CHAN_DISABLED)
  438. continue;
  439. if (ch->flags & IEEE80211_CHAN_RADAR)
  440. ch->flags |= IEEE80211_CHAN_NO_IBSS |
  441. IEEE80211_CHAN_PASSIVE_SCAN;
  442. }
  443. return 0;
  444. }
  445. static void wl1271_conf_init(struct wl1271 *wl)
  446. {
  447. /*
  448. * This function applies the default configuration to the driver. This
  449. * function is invoked upon driver load (spi probe.)
  450. *
  451. * The configuration is stored in a run-time structure in order to
  452. * facilitate for run-time adjustment of any of the parameters. Making
  453. * changes to the configuration structure will apply the new values on
  454. * the next interface up (wl1271_op_start.)
  455. */
  456. /* apply driver default configuration */
  457. memcpy(&wl->conf, &default_conf, sizeof(default_conf));
  458. }
  459. static int wl1271_plt_init(struct wl1271 *wl)
  460. {
  461. struct conf_tx_ac_category *conf_ac;
  462. struct conf_tx_tid *conf_tid;
  463. int ret, i;
  464. if (wl->chip.id == CHIP_ID_1283_PG20)
  465. ret = wl128x_cmd_general_parms(wl);
  466. else
  467. ret = wl1271_cmd_general_parms(wl);
  468. if (ret < 0)
  469. return ret;
  470. if (wl->chip.id == CHIP_ID_1283_PG20)
  471. ret = wl128x_cmd_radio_parms(wl);
  472. else
  473. ret = wl1271_cmd_radio_parms(wl);
  474. if (ret < 0)
  475. return ret;
  476. if (wl->chip.id != CHIP_ID_1283_PG20) {
  477. ret = wl1271_cmd_ext_radio_parms(wl);
  478. if (ret < 0)
  479. return ret;
  480. }
  481. if (ret < 0)
  482. return ret;
  483. /* Chip-specific initializations */
  484. ret = wl1271_chip_specific_init(wl);
  485. if (ret < 0)
  486. return ret;
  487. ret = wl1271_sta_init_templates_config(wl);
  488. if (ret < 0)
  489. return ret;
  490. ret = wl1271_acx_init_mem_config(wl);
  491. if (ret < 0)
  492. return ret;
  493. /* PHY layer config */
  494. ret = wl1271_init_phy_config(wl);
  495. if (ret < 0)
  496. goto out_free_memmap;
  497. ret = wl1271_acx_dco_itrim_params(wl);
  498. if (ret < 0)
  499. goto out_free_memmap;
  500. /* Initialize connection monitoring thresholds */
  501. ret = wl1271_acx_conn_monit_params(wl, false);
  502. if (ret < 0)
  503. goto out_free_memmap;
  504. /* Bluetooth WLAN coexistence */
  505. ret = wl1271_init_pta(wl);
  506. if (ret < 0)
  507. goto out_free_memmap;
  508. /* FM WLAN coexistence */
  509. ret = wl1271_acx_fm_coex(wl);
  510. if (ret < 0)
  511. goto out_free_memmap;
  512. /* Energy detection */
  513. ret = wl1271_init_energy_detection(wl);
  514. if (ret < 0)
  515. goto out_free_memmap;
  516. ret = wl1271_acx_sta_mem_cfg(wl);
  517. if (ret < 0)
  518. goto out_free_memmap;
  519. /* Default fragmentation threshold */
  520. ret = wl1271_acx_frag_threshold(wl, wl->conf.tx.frag_threshold);
  521. if (ret < 0)
  522. goto out_free_memmap;
  523. /* Default TID/AC configuration */
  524. BUG_ON(wl->conf.tx.tid_conf_count != wl->conf.tx.ac_conf_count);
  525. for (i = 0; i < wl->conf.tx.tid_conf_count; i++) {
  526. conf_ac = &wl->conf.tx.ac_conf[i];
  527. ret = wl1271_acx_ac_cfg(wl, conf_ac->ac, conf_ac->cw_min,
  528. conf_ac->cw_max, conf_ac->aifsn,
  529. conf_ac->tx_op_limit);
  530. if (ret < 0)
  531. goto out_free_memmap;
  532. conf_tid = &wl->conf.tx.tid_conf[i];
  533. ret = wl1271_acx_tid_cfg(wl, conf_tid->queue_id,
  534. conf_tid->channel_type,
  535. conf_tid->tsid,
  536. conf_tid->ps_scheme,
  537. conf_tid->ack_policy,
  538. conf_tid->apsd_conf[0],
  539. conf_tid->apsd_conf[1]);
  540. if (ret < 0)
  541. goto out_free_memmap;
  542. }
  543. /* Enable data path */
  544. ret = wl1271_cmd_data_path(wl, 1);
  545. if (ret < 0)
  546. goto out_free_memmap;
  547. /* Configure for CAM power saving (ie. always active) */
  548. ret = wl1271_acx_sleep_auth(wl, WL1271_PSM_CAM);
  549. if (ret < 0)
  550. goto out_free_memmap;
  551. /* configure PM */
  552. ret = wl1271_acx_pm_config(wl);
  553. if (ret < 0)
  554. goto out_free_memmap;
  555. return 0;
  556. out_free_memmap:
  557. kfree(wl->target_mem_map);
  558. wl->target_mem_map = NULL;
  559. return ret;
  560. }
  561. static void wl1271_irq_ps_regulate_link(struct wl1271 *wl, u8 hlid, u8 tx_blks)
  562. {
  563. bool fw_ps;
  564. /* only regulate station links */
  565. if (hlid < WL1271_AP_STA_HLID_START)
  566. return;
  567. fw_ps = test_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
  568. /*
  569. * Wake up from high level PS if the STA is asleep with too little
  570. * blocks in FW or if the STA is awake.
  571. */
  572. if (!fw_ps || tx_blks < WL1271_PS_STA_MAX_BLOCKS)
  573. wl1271_ps_link_end(wl, hlid);
  574. /* Start high-level PS if the STA is asleep with enough blocks in FW */
  575. else if (fw_ps && tx_blks >= WL1271_PS_STA_MAX_BLOCKS)
  576. wl1271_ps_link_start(wl, hlid, true);
  577. }
  578. static void wl1271_irq_update_links_status(struct wl1271 *wl,
  579. struct wl1271_fw_ap_status *status)
  580. {
  581. u32 cur_fw_ps_map;
  582. u8 hlid;
  583. cur_fw_ps_map = le32_to_cpu(status->link_ps_bitmap);
  584. if (wl->ap_fw_ps_map != cur_fw_ps_map) {
  585. wl1271_debug(DEBUG_PSM,
  586. "link ps prev 0x%x cur 0x%x changed 0x%x",
  587. wl->ap_fw_ps_map, cur_fw_ps_map,
  588. wl->ap_fw_ps_map ^ cur_fw_ps_map);
  589. wl->ap_fw_ps_map = cur_fw_ps_map;
  590. }
  591. for (hlid = WL1271_AP_STA_HLID_START; hlid < AP_MAX_LINKS; hlid++) {
  592. u8 cnt = status->tx_lnk_free_blks[hlid] -
  593. wl->links[hlid].prev_freed_blks;
  594. wl->links[hlid].prev_freed_blks =
  595. status->tx_lnk_free_blks[hlid];
  596. wl->links[hlid].allocated_blks -= cnt;
  597. wl1271_irq_ps_regulate_link(wl, hlid,
  598. wl->links[hlid].allocated_blks);
  599. }
  600. }
  601. static void wl1271_fw_status(struct wl1271 *wl,
  602. struct wl1271_fw_full_status *full_status)
  603. {
  604. struct wl1271_fw_common_status *status = &full_status->common;
  605. struct timespec ts;
  606. u32 old_tx_blk_count = wl->tx_blocks_available;
  607. u32 freed_blocks = 0;
  608. int i;
  609. if (wl->bss_type == BSS_TYPE_AP_BSS) {
  610. wl1271_raw_read(wl, FW_STATUS_ADDR, status,
  611. sizeof(struct wl1271_fw_ap_status), false);
  612. } else {
  613. wl1271_raw_read(wl, FW_STATUS_ADDR, status,
  614. sizeof(struct wl1271_fw_sta_status), false);
  615. }
  616. wl1271_debug(DEBUG_IRQ, "intr: 0x%x (fw_rx_counter = %d, "
  617. "drv_rx_counter = %d, tx_results_counter = %d)",
  618. status->intr,
  619. status->fw_rx_counter,
  620. status->drv_rx_counter,
  621. status->tx_results_counter);
  622. /* update number of available TX blocks */
  623. for (i = 0; i < NUM_TX_QUEUES; i++) {
  624. freed_blocks += le32_to_cpu(status->tx_released_blks[i]) -
  625. wl->tx_blocks_freed[i];
  626. wl->tx_blocks_freed[i] =
  627. le32_to_cpu(status->tx_released_blks[i]);
  628. }
  629. wl->tx_allocated_blocks -= freed_blocks;
  630. if (wl->bss_type == BSS_TYPE_AP_BSS) {
  631. /* Update num of allocated TX blocks per link and ps status */
  632. wl1271_irq_update_links_status(wl, &full_status->ap);
  633. wl->tx_blocks_available += freed_blocks;
  634. } else {
  635. int avail = full_status->sta.tx_total - wl->tx_allocated_blocks;
  636. /*
  637. * The FW might change the total number of TX memblocks before
  638. * we get a notification about blocks being released. Thus, the
  639. * available blocks calculation might yield a temporary result
  640. * which is lower than the actual available blocks. Keeping in
  641. * mind that only blocks that were allocated can be moved from
  642. * TX to RX, tx_blocks_available should never decrease here.
  643. */
  644. wl->tx_blocks_available = max((int)wl->tx_blocks_available,
  645. avail);
  646. }
  647. /* if more blocks are available now, tx work can be scheduled */
  648. if (wl->tx_blocks_available > old_tx_blk_count)
  649. clear_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags);
  650. /* update the host-chipset time offset */
  651. getnstimeofday(&ts);
  652. wl->time_offset = (timespec_to_ns(&ts) >> 10) -
  653. (s64)le32_to_cpu(status->fw_localtime);
  654. }
  655. static void wl1271_flush_deferred_work(struct wl1271 *wl)
  656. {
  657. struct sk_buff *skb;
  658. /* Pass all received frames to the network stack */
  659. while ((skb = skb_dequeue(&wl->deferred_rx_queue)))
  660. ieee80211_rx_ni(wl->hw, skb);
  661. /* Return sent skbs to the network stack */
  662. while ((skb = skb_dequeue(&wl->deferred_tx_queue)))
  663. ieee80211_tx_status(wl->hw, skb);
  664. }
  665. static void wl1271_netstack_work(struct work_struct *work)
  666. {
  667. struct wl1271 *wl =
  668. container_of(work, struct wl1271, netstack_work);
  669. do {
  670. wl1271_flush_deferred_work(wl);
  671. } while (skb_queue_len(&wl->deferred_rx_queue));
  672. }
  673. #define WL1271_IRQ_MAX_LOOPS 256
  674. irqreturn_t wl1271_irq(int irq, void *cookie)
  675. {
  676. int ret;
  677. u32 intr;
  678. int loopcount = WL1271_IRQ_MAX_LOOPS;
  679. struct wl1271 *wl = (struct wl1271 *)cookie;
  680. bool done = false;
  681. unsigned int defer_count;
  682. unsigned long flags;
  683. /* TX might be handled here, avoid redundant work */
  684. set_bit(WL1271_FLAG_TX_PENDING, &wl->flags);
  685. cancel_work_sync(&wl->tx_work);
  686. /*
  687. * In case edge triggered interrupt must be used, we cannot iterate
  688. * more than once without introducing race conditions with the hardirq.
  689. */
  690. if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ)
  691. loopcount = 1;
  692. mutex_lock(&wl->mutex);
  693. wl1271_debug(DEBUG_IRQ, "IRQ work");
  694. if (unlikely(wl->state == WL1271_STATE_OFF))
  695. goto out;
  696. ret = wl1271_ps_elp_wakeup(wl);
  697. if (ret < 0)
  698. goto out;
  699. while (!done && loopcount--) {
  700. /*
  701. * In order to avoid a race with the hardirq, clear the flag
  702. * before acknowledging the chip. Since the mutex is held,
  703. * wl1271_ps_elp_wakeup cannot be called concurrently.
  704. */
  705. clear_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
  706. smp_mb__after_clear_bit();
  707. wl1271_fw_status(wl, wl->fw_status);
  708. intr = le32_to_cpu(wl->fw_status->common.intr);
  709. intr &= WL1271_INTR_MASK;
  710. if (!intr) {
  711. done = true;
  712. continue;
  713. }
  714. if (unlikely(intr & WL1271_ACX_INTR_WATCHDOG)) {
  715. wl1271_error("watchdog interrupt received! "
  716. "starting recovery.");
  717. ieee80211_queue_work(wl->hw, &wl->recovery_work);
  718. /* restarting the chip. ignore any other interrupt. */
  719. goto out;
  720. }
  721. if (likely(intr & WL1271_ACX_INTR_DATA)) {
  722. wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_DATA");
  723. wl1271_rx(wl, &wl->fw_status->common);
  724. /* Check if any tx blocks were freed */
  725. spin_lock_irqsave(&wl->wl_lock, flags);
  726. if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
  727. wl->tx_queue_count) {
  728. spin_unlock_irqrestore(&wl->wl_lock, flags);
  729. /*
  730. * In order to avoid starvation of the TX path,
  731. * call the work function directly.
  732. */
  733. wl1271_tx_work_locked(wl);
  734. } else {
  735. spin_unlock_irqrestore(&wl->wl_lock, flags);
  736. }
  737. /* check for tx results */
  738. if (wl->fw_status->common.tx_results_counter !=
  739. (wl->tx_results_count & 0xff))
  740. wl1271_tx_complete(wl);
  741. /* Make sure the deferred queues don't get too long */
  742. defer_count = skb_queue_len(&wl->deferred_tx_queue) +
  743. skb_queue_len(&wl->deferred_rx_queue);
  744. if (defer_count > WL1271_DEFERRED_QUEUE_LIMIT)
  745. wl1271_flush_deferred_work(wl);
  746. }
  747. if (intr & WL1271_ACX_INTR_EVENT_A) {
  748. wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_EVENT_A");
  749. wl1271_event_handle(wl, 0);
  750. }
  751. if (intr & WL1271_ACX_INTR_EVENT_B) {
  752. wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_EVENT_B");
  753. wl1271_event_handle(wl, 1);
  754. }
  755. if (intr & WL1271_ACX_INTR_INIT_COMPLETE)
  756. wl1271_debug(DEBUG_IRQ,
  757. "WL1271_ACX_INTR_INIT_COMPLETE");
  758. if (intr & WL1271_ACX_INTR_HW_AVAILABLE)
  759. wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_HW_AVAILABLE");
  760. }
  761. wl1271_ps_elp_sleep(wl);
  762. out:
  763. spin_lock_irqsave(&wl->wl_lock, flags);
  764. /* In case TX was not handled here, queue TX work */
  765. clear_bit(WL1271_FLAG_TX_PENDING, &wl->flags);
  766. if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
  767. wl->tx_queue_count)
  768. ieee80211_queue_work(wl->hw, &wl->tx_work);
  769. spin_unlock_irqrestore(&wl->wl_lock, flags);
  770. mutex_unlock(&wl->mutex);
  771. return IRQ_HANDLED;
  772. }
  773. EXPORT_SYMBOL_GPL(wl1271_irq);
  774. static int wl1271_fetch_firmware(struct wl1271 *wl)
  775. {
  776. const struct firmware *fw;
  777. const char *fw_name;
  778. int ret;
  779. switch (wl->bss_type) {
  780. case BSS_TYPE_AP_BSS:
  781. if (wl->chip.id == CHIP_ID_1283_PG20)
  782. fw_name = WL128X_AP_FW_NAME;
  783. else
  784. fw_name = WL127X_AP_FW_NAME;
  785. break;
  786. case BSS_TYPE_IBSS:
  787. case BSS_TYPE_STA_BSS:
  788. if (wl->chip.id == CHIP_ID_1283_PG20)
  789. fw_name = WL128X_FW_NAME;
  790. else
  791. fw_name = WL1271_FW_NAME;
  792. break;
  793. default:
  794. wl1271_error("no compatible firmware for bss_type %d",
  795. wl->bss_type);
  796. return -EINVAL;
  797. }
  798. wl1271_debug(DEBUG_BOOT, "booting firmware %s", fw_name);
  799. ret = request_firmware(&fw, fw_name, wl1271_wl_to_dev(wl));
  800. if (ret < 0) {
  801. wl1271_error("could not get firmware: %d", ret);
  802. return ret;
  803. }
  804. if (fw->size % 4) {
  805. wl1271_error("firmware size is not multiple of 32 bits: %zu",
  806. fw->size);
  807. ret = -EILSEQ;
  808. goto out;
  809. }
  810. vfree(wl->fw);
  811. wl->fw_len = fw->size;
  812. wl->fw = vmalloc(wl->fw_len);
  813. if (!wl->fw) {
  814. wl1271_error("could not allocate memory for the firmware");
  815. ret = -ENOMEM;
  816. goto out;
  817. }
  818. memcpy(wl->fw, fw->data, wl->fw_len);
  819. wl->fw_bss_type = wl->bss_type;
  820. ret = 0;
  821. out:
  822. release_firmware(fw);
  823. return ret;
  824. }
  825. static int wl1271_fetch_nvs(struct wl1271 *wl)
  826. {
  827. const struct firmware *fw;
  828. int ret;
  829. ret = request_firmware(&fw, WL12XX_NVS_NAME, wl1271_wl_to_dev(wl));
  830. if (ret < 0) {
  831. wl1271_error("could not get nvs file: %d", ret);
  832. return ret;
  833. }
  834. wl->nvs = kmemdup(fw->data, fw->size, GFP_KERNEL);
  835. if (!wl->nvs) {
  836. wl1271_error("could not allocate memory for the nvs file");
  837. ret = -ENOMEM;
  838. goto out;
  839. }
  840. wl->nvs_len = fw->size;
  841. out:
  842. release_firmware(fw);
  843. return ret;
  844. }
  845. static void wl1271_recovery_work(struct work_struct *work)
  846. {
  847. struct wl1271 *wl =
  848. container_of(work, struct wl1271, recovery_work);
  849. mutex_lock(&wl->mutex);
  850. if (wl->state != WL1271_STATE_ON)
  851. goto out;
  852. wl1271_info("Hardware recovery in progress. FW ver: %s pc: 0x%x",
  853. wl->chip.fw_ver_str, wl1271_read32(wl, SCR_PAD4));
  854. if (test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags))
  855. ieee80211_connection_loss(wl->vif);
  856. /* Prevent spurious TX during FW restart */
  857. ieee80211_stop_queues(wl->hw);
  858. if (wl->sched_scanning) {
  859. ieee80211_sched_scan_stopped(wl->hw);
  860. wl->sched_scanning = false;
  861. }
  862. /* reboot the chipset */
  863. __wl1271_op_remove_interface(wl, false);
  864. ieee80211_restart_hw(wl->hw);
  865. /*
  866. * Its safe to enable TX now - the queues are stopped after a request
  867. * to restart the HW.
  868. */
  869. ieee80211_wake_queues(wl->hw);
  870. out:
  871. mutex_unlock(&wl->mutex);
  872. }
  873. static void wl1271_fw_wakeup(struct wl1271 *wl)
  874. {
  875. u32 elp_reg;
  876. elp_reg = ELPCTRL_WAKE_UP;
  877. wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
  878. }
  879. static int wl1271_setup(struct wl1271 *wl)
  880. {
  881. wl->fw_status = kmalloc(sizeof(*wl->fw_status), GFP_KERNEL);
  882. if (!wl->fw_status)
  883. return -ENOMEM;
  884. wl->tx_res_if = kmalloc(sizeof(*wl->tx_res_if), GFP_KERNEL);
  885. if (!wl->tx_res_if) {
  886. kfree(wl->fw_status);
  887. return -ENOMEM;
  888. }
  889. return 0;
  890. }
  891. static int wl1271_chip_wakeup(struct wl1271 *wl)
  892. {
  893. struct wl1271_partition_set partition;
  894. int ret = 0;
  895. msleep(WL1271_PRE_POWER_ON_SLEEP);
  896. ret = wl1271_power_on(wl);
  897. if (ret < 0)
  898. goto out;
  899. msleep(WL1271_POWER_ON_SLEEP);
  900. wl1271_io_reset(wl);
  901. wl1271_io_init(wl);
  902. /* We don't need a real memory partition here, because we only want
  903. * to use the registers at this point. */
  904. memset(&partition, 0, sizeof(partition));
  905. partition.reg.start = REGISTERS_BASE;
  906. partition.reg.size = REGISTERS_DOWN_SIZE;
  907. wl1271_set_partition(wl, &partition);
  908. /* ELP module wake up */
  909. wl1271_fw_wakeup(wl);
  910. /* whal_FwCtrl_BootSm() */
  911. /* 0. read chip id from CHIP_ID */
  912. wl->chip.id = wl1271_read32(wl, CHIP_ID_B);
  913. /* 1. check if chip id is valid */
  914. switch (wl->chip.id) {
  915. case CHIP_ID_1271_PG10:
  916. wl1271_warning("chip id 0x%x (1271 PG10) support is obsolete",
  917. wl->chip.id);
  918. ret = wl1271_setup(wl);
  919. if (ret < 0)
  920. goto out;
  921. break;
  922. case CHIP_ID_1271_PG20:
  923. wl1271_debug(DEBUG_BOOT, "chip id 0x%x (1271 PG20)",
  924. wl->chip.id);
  925. /* end-of-transaction flag should be set in wl127x AP mode */
  926. if (wl->bss_type == BSS_TYPE_AP_BSS)
  927. wl->quirks |= WL12XX_QUIRK_END_OF_TRANSACTION;
  928. ret = wl1271_setup(wl);
  929. if (ret < 0)
  930. goto out;
  931. break;
  932. case CHIP_ID_1283_PG20:
  933. wl1271_debug(DEBUG_BOOT, "chip id 0x%x (1283 PG20)",
  934. wl->chip.id);
  935. ret = wl1271_setup(wl);
  936. if (ret < 0)
  937. goto out;
  938. if (wl1271_set_block_size(wl))
  939. wl->quirks |= WL12XX_QUIRK_BLOCKSIZE_ALIGNMENT;
  940. break;
  941. case CHIP_ID_1283_PG10:
  942. default:
  943. wl1271_warning("unsupported chip id: 0x%x", wl->chip.id);
  944. ret = -ENODEV;
  945. goto out;
  946. }
  947. /* Make sure the firmware type matches the BSS type */
  948. if (wl->fw == NULL || wl->fw_bss_type != wl->bss_type) {
  949. ret = wl1271_fetch_firmware(wl);
  950. if (ret < 0)
  951. goto out;
  952. }
  953. /* No NVS from netlink, try to get it from the filesystem */
  954. if (wl->nvs == NULL) {
  955. ret = wl1271_fetch_nvs(wl);
  956. if (ret < 0)
  957. goto out;
  958. }
  959. out:
  960. return ret;
  961. }
  962. static unsigned int wl1271_get_fw_ver_quirks(struct wl1271 *wl)
  963. {
  964. unsigned int quirks = 0;
  965. unsigned int *fw_ver = wl->chip.fw_ver;
  966. /* Only for wl127x */
  967. if ((fw_ver[FW_VER_CHIP] == FW_VER_CHIP_WL127X) &&
  968. /* Check STA version */
  969. (((fw_ver[FW_VER_IF_TYPE] == FW_VER_IF_TYPE_STA) &&
  970. (fw_ver[FW_VER_MINOR] < FW_VER_MINOR_1_SPARE_STA_MIN)) ||
  971. /* Check AP version */
  972. ((fw_ver[FW_VER_IF_TYPE] == FW_VER_IF_TYPE_AP) &&
  973. (fw_ver[FW_VER_MINOR] < FW_VER_MINOR_1_SPARE_AP_MIN))))
  974. quirks |= WL12XX_QUIRK_USE_2_SPARE_BLOCKS;
  975. return quirks;
  976. }
  977. int wl1271_plt_start(struct wl1271 *wl)
  978. {
  979. int retries = WL1271_BOOT_RETRIES;
  980. int ret;
  981. mutex_lock(&wl->mutex);
  982. wl1271_notice("power up");
  983. if (wl->state != WL1271_STATE_OFF) {
  984. wl1271_error("cannot go into PLT state because not "
  985. "in off state: %d", wl->state);
  986. ret = -EBUSY;
  987. goto out;
  988. }
  989. wl->bss_type = BSS_TYPE_STA_BSS;
  990. while (retries) {
  991. retries--;
  992. ret = wl1271_chip_wakeup(wl);
  993. if (ret < 0)
  994. goto power_off;
  995. ret = wl1271_boot(wl);
  996. if (ret < 0)
  997. goto power_off;
  998. ret = wl1271_plt_init(wl);
  999. if (ret < 0)
  1000. goto irq_disable;
  1001. wl->state = WL1271_STATE_PLT;
  1002. wl1271_notice("firmware booted in PLT mode (%s)",
  1003. wl->chip.fw_ver_str);
  1004. /* Check if any quirks are needed with older fw versions */
  1005. wl->quirks |= wl1271_get_fw_ver_quirks(wl);
  1006. goto out;
  1007. irq_disable:
  1008. mutex_unlock(&wl->mutex);
  1009. /* Unlocking the mutex in the middle of handling is
  1010. inherently unsafe. In this case we deem it safe to do,
  1011. because we need to let any possibly pending IRQ out of
  1012. the system (and while we are WL1271_STATE_OFF the IRQ
  1013. work function will not do anything.) Also, any other
  1014. possible concurrent operations will fail due to the
  1015. current state, hence the wl1271 struct should be safe. */
  1016. wl1271_disable_interrupts(wl);
  1017. wl1271_flush_deferred_work(wl);
  1018. cancel_work_sync(&wl->netstack_work);
  1019. mutex_lock(&wl->mutex);
  1020. power_off:
  1021. wl1271_power_off(wl);
  1022. }
  1023. wl1271_error("firmware boot in PLT mode failed despite %d retries",
  1024. WL1271_BOOT_RETRIES);
  1025. out:
  1026. mutex_unlock(&wl->mutex);
  1027. return ret;
  1028. }
  1029. static int __wl1271_plt_stop(struct wl1271 *wl)
  1030. {
  1031. int ret = 0;
  1032. wl1271_notice("power down");
  1033. if (wl->state != WL1271_STATE_PLT) {
  1034. wl1271_error("cannot power down because not in PLT "
  1035. "state: %d", wl->state);
  1036. ret = -EBUSY;
  1037. goto out;
  1038. }
  1039. wl1271_power_off(wl);
  1040. wl->state = WL1271_STATE_OFF;
  1041. wl->rx_counter = 0;
  1042. mutex_unlock(&wl->mutex);
  1043. wl1271_disable_interrupts(wl);
  1044. wl1271_flush_deferred_work(wl);
  1045. cancel_work_sync(&wl->netstack_work);
  1046. cancel_work_sync(&wl->recovery_work);
  1047. mutex_lock(&wl->mutex);
  1048. out:
  1049. return ret;
  1050. }
  1051. int wl1271_plt_stop(struct wl1271 *wl)
  1052. {
  1053. int ret;
  1054. mutex_lock(&wl->mutex);
  1055. ret = __wl1271_plt_stop(wl);
  1056. mutex_unlock(&wl->mutex);
  1057. return ret;
  1058. }
  1059. static void wl1271_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  1060. {
  1061. struct wl1271 *wl = hw->priv;
  1062. unsigned long flags;
  1063. int q;
  1064. u8 hlid = 0;
  1065. q = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
  1066. if (wl->bss_type == BSS_TYPE_AP_BSS)
  1067. hlid = wl1271_tx_get_hlid(skb);
  1068. spin_lock_irqsave(&wl->wl_lock, flags);
  1069. wl->tx_queue_count++;
  1070. /*
  1071. * The workqueue is slow to process the tx_queue and we need stop
  1072. * the queue here, otherwise the queue will get too long.
  1073. */
  1074. if (wl->tx_queue_count >= WL1271_TX_QUEUE_HIGH_WATERMARK) {
  1075. wl1271_debug(DEBUG_TX, "op_tx: stopping queues");
  1076. ieee80211_stop_queues(wl->hw);
  1077. set_bit(WL1271_FLAG_TX_QUEUE_STOPPED, &wl->flags);
  1078. }
  1079. /* queue the packet */
  1080. if (wl->bss_type == BSS_TYPE_AP_BSS) {
  1081. wl1271_debug(DEBUG_TX, "queue skb hlid %d q %d", hlid, q);
  1082. skb_queue_tail(&wl->links[hlid].tx_queue[q], skb);
  1083. } else {
  1084. skb_queue_tail(&wl->tx_queue[q], skb);
  1085. }
  1086. /*
  1087. * The chip specific setup must run before the first TX packet -
  1088. * before that, the tx_work will not be initialized!
  1089. */
  1090. if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
  1091. !test_bit(WL1271_FLAG_TX_PENDING, &wl->flags))
  1092. ieee80211_queue_work(wl->hw, &wl->tx_work);
  1093. spin_unlock_irqrestore(&wl->wl_lock, flags);
  1094. }
  1095. int wl1271_tx_dummy_packet(struct wl1271 *wl)
  1096. {
  1097. unsigned long flags;
  1098. spin_lock_irqsave(&wl->wl_lock, flags);
  1099. set_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags);
  1100. wl->tx_queue_count++;
  1101. spin_unlock_irqrestore(&wl->wl_lock, flags);
  1102. /* The FW is low on RX memory blocks, so send the dummy packet asap */
  1103. if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags))
  1104. wl1271_tx_work_locked(wl);
  1105. /*
  1106. * If the FW TX is busy, TX work will be scheduled by the threaded
  1107. * interrupt handler function
  1108. */
  1109. return 0;
  1110. }
  1111. /*
  1112. * The size of the dummy packet should be at least 1400 bytes. However, in
  1113. * order to minimize the number of bus transactions, aligning it to 512 bytes
  1114. * boundaries could be beneficial, performance wise
  1115. */
  1116. #define TOTAL_TX_DUMMY_PACKET_SIZE (ALIGN(1400, 512))
  1117. static struct sk_buff *wl12xx_alloc_dummy_packet(struct wl1271 *wl)
  1118. {
  1119. struct sk_buff *skb;
  1120. struct ieee80211_hdr_3addr *hdr;
  1121. unsigned int dummy_packet_size;
  1122. dummy_packet_size = TOTAL_TX_DUMMY_PACKET_SIZE -
  1123. sizeof(struct wl1271_tx_hw_descr) - sizeof(*hdr);
  1124. skb = dev_alloc_skb(TOTAL_TX_DUMMY_PACKET_SIZE);
  1125. if (!skb) {
  1126. wl1271_warning("Failed to allocate a dummy packet skb");
  1127. return NULL;
  1128. }
  1129. skb_reserve(skb, sizeof(struct wl1271_tx_hw_descr));
  1130. hdr = (struct ieee80211_hdr_3addr *) skb_put(skb, sizeof(*hdr));
  1131. memset(hdr, 0, sizeof(*hdr));
  1132. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  1133. IEEE80211_STYPE_NULLFUNC |
  1134. IEEE80211_FCTL_TODS);
  1135. memset(skb_put(skb, dummy_packet_size), 0, dummy_packet_size);
  1136. /* Dummy packets require the TID to be management */
  1137. skb->priority = WL1271_TID_MGMT;
  1138. /* Initialize all fields that might be used */
  1139. skb_set_queue_mapping(skb, 0);
  1140. memset(IEEE80211_SKB_CB(skb), 0, sizeof(struct ieee80211_tx_info));
  1141. return skb;
  1142. }
  1143. static struct notifier_block wl1271_dev_notifier = {
  1144. .notifier_call = wl1271_dev_notify,
  1145. };
  1146. #ifdef CONFIG_PM
  1147. static int wl1271_configure_suspend(struct wl1271 *wl)
  1148. {
  1149. int ret;
  1150. if (wl->bss_type != BSS_TYPE_STA_BSS)
  1151. return 0;
  1152. mutex_lock(&wl->mutex);
  1153. ret = wl1271_ps_elp_wakeup(wl);
  1154. if (ret < 0)
  1155. goto out_unlock;
  1156. /* enter psm if needed*/
  1157. if (!test_bit(WL1271_FLAG_PSM, &wl->flags)) {
  1158. DECLARE_COMPLETION_ONSTACK(compl);
  1159. wl->ps_compl = &compl;
  1160. ret = wl1271_ps_set_mode(wl, STATION_POWER_SAVE_MODE,
  1161. wl->basic_rate, true);
  1162. if (ret < 0)
  1163. goto out_sleep;
  1164. /* we must unlock here so we will be able to get events */
  1165. wl1271_ps_elp_sleep(wl);
  1166. mutex_unlock(&wl->mutex);
  1167. ret = wait_for_completion_timeout(
  1168. &compl, msecs_to_jiffies(WL1271_PS_COMPLETE_TIMEOUT));
  1169. if (ret <= 0) {
  1170. wl1271_warning("couldn't enter ps mode!");
  1171. ret = -EBUSY;
  1172. goto out;
  1173. }
  1174. /* take mutex again, and wakeup */
  1175. mutex_lock(&wl->mutex);
  1176. ret = wl1271_ps_elp_wakeup(wl);
  1177. if (ret < 0)
  1178. goto out_unlock;
  1179. }
  1180. out_sleep:
  1181. wl1271_ps_elp_sleep(wl);
  1182. out_unlock:
  1183. mutex_unlock(&wl->mutex);
  1184. out:
  1185. return ret;
  1186. }
  1187. static void wl1271_configure_resume(struct wl1271 *wl)
  1188. {
  1189. int ret;
  1190. if (wl->bss_type != BSS_TYPE_STA_BSS)
  1191. return;
  1192. mutex_lock(&wl->mutex);
  1193. ret = wl1271_ps_elp_wakeup(wl);
  1194. if (ret < 0)
  1195. goto out;
  1196. /* exit psm if it wasn't configured */
  1197. if (!test_bit(WL1271_FLAG_PSM_REQUESTED, &wl->flags))
  1198. wl1271_ps_set_mode(wl, STATION_ACTIVE_MODE,
  1199. wl->basic_rate, true);
  1200. wl1271_ps_elp_sleep(wl);
  1201. out:
  1202. mutex_unlock(&wl->mutex);
  1203. }
  1204. static int wl1271_op_suspend(struct ieee80211_hw *hw,
  1205. struct cfg80211_wowlan *wow)
  1206. {
  1207. struct wl1271 *wl = hw->priv;
  1208. wl1271_debug(DEBUG_MAC80211, "mac80211 suspend wow=%d", !!wow);
  1209. wl->wow_enabled = !!wow;
  1210. if (wl->wow_enabled) {
  1211. int ret;
  1212. ret = wl1271_configure_suspend(wl);
  1213. if (ret < 0) {
  1214. wl1271_warning("couldn't prepare device to suspend");
  1215. return ret;
  1216. }
  1217. /* flush any remaining work */
  1218. wl1271_debug(DEBUG_MAC80211, "flushing remaining works");
  1219. flush_delayed_work(&wl->scan_complete_work);
  1220. /*
  1221. * disable and re-enable interrupts in order to flush
  1222. * the threaded_irq
  1223. */
  1224. wl1271_disable_interrupts(wl);
  1225. /*
  1226. * set suspended flag to avoid triggering a new threaded_irq
  1227. * work. no need for spinlock as interrupts are disabled.
  1228. */
  1229. set_bit(WL1271_FLAG_SUSPENDED, &wl->flags);
  1230. wl1271_enable_interrupts(wl);
  1231. flush_work(&wl->tx_work);
  1232. flush_delayed_work(&wl->pspoll_work);
  1233. flush_delayed_work(&wl->elp_work);
  1234. }
  1235. return 0;
  1236. }
  1237. static int wl1271_op_resume(struct ieee80211_hw *hw)
  1238. {
  1239. struct wl1271 *wl = hw->priv;
  1240. wl1271_debug(DEBUG_MAC80211, "mac80211 resume wow=%d",
  1241. wl->wow_enabled);
  1242. /*
  1243. * re-enable irq_work enqueuing, and call irq_work directly if
  1244. * there is a pending work.
  1245. */
  1246. if (wl->wow_enabled) {
  1247. struct wl1271 *wl = hw->priv;
  1248. unsigned long flags;
  1249. bool run_irq_work = false;
  1250. spin_lock_irqsave(&wl->wl_lock, flags);
  1251. clear_bit(WL1271_FLAG_SUSPENDED, &wl->flags);
  1252. if (test_and_clear_bit(WL1271_FLAG_PENDING_WORK, &wl->flags))
  1253. run_irq_work = true;
  1254. spin_unlock_irqrestore(&wl->wl_lock, flags);
  1255. if (run_irq_work) {
  1256. wl1271_debug(DEBUG_MAC80211,
  1257. "run postponed irq_work directly");
  1258. wl1271_irq(0, wl);
  1259. wl1271_enable_interrupts(wl);
  1260. }
  1261. wl1271_configure_resume(wl);
  1262. }
  1263. return 0;
  1264. }
  1265. #endif
  1266. static int wl1271_op_start(struct ieee80211_hw *hw)
  1267. {
  1268. wl1271_debug(DEBUG_MAC80211, "mac80211 start");
  1269. /*
  1270. * We have to delay the booting of the hardware because
  1271. * we need to know the local MAC address before downloading and
  1272. * initializing the firmware. The MAC address cannot be changed
  1273. * after boot, and without the proper MAC address, the firmware
  1274. * will not function properly.
  1275. *
  1276. * The MAC address is first known when the corresponding interface
  1277. * is added. That is where we will initialize the hardware.
  1278. *
  1279. * In addition, we currently have different firmwares for AP and managed
  1280. * operation. We will know which to boot according to interface type.
  1281. */
  1282. return 0;
  1283. }
  1284. static void wl1271_op_stop(struct ieee80211_hw *hw)
  1285. {
  1286. wl1271_debug(DEBUG_MAC80211, "mac80211 stop");
  1287. }
  1288. static int wl1271_op_add_interface(struct ieee80211_hw *hw,
  1289. struct ieee80211_vif *vif)
  1290. {
  1291. struct wl1271 *wl = hw->priv;
  1292. struct wiphy *wiphy = hw->wiphy;
  1293. int retries = WL1271_BOOT_RETRIES;
  1294. int ret = 0;
  1295. bool booted = false;
  1296. wl1271_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
  1297. vif->type, vif->addr);
  1298. mutex_lock(&wl->mutex);
  1299. if (wl->vif) {
  1300. wl1271_debug(DEBUG_MAC80211,
  1301. "multiple vifs are not supported yet");
  1302. ret = -EBUSY;
  1303. goto out;
  1304. }
  1305. /*
  1306. * in some very corner case HW recovery scenarios its possible to
  1307. * get here before __wl1271_op_remove_interface is complete, so
  1308. * opt out if that is the case.
  1309. */
  1310. if (test_bit(WL1271_FLAG_IF_INITIALIZED, &wl->flags)) {
  1311. ret = -EBUSY;
  1312. goto out;
  1313. }
  1314. switch (vif->type) {
  1315. case NL80211_IFTYPE_STATION:
  1316. wl->bss_type = BSS_TYPE_STA_BSS;
  1317. wl->set_bss_type = BSS_TYPE_STA_BSS;
  1318. break;
  1319. case NL80211_IFTYPE_ADHOC:
  1320. wl->bss_type = BSS_TYPE_IBSS;
  1321. wl->set_bss_type = BSS_TYPE_STA_BSS;
  1322. break;
  1323. case NL80211_IFTYPE_AP:
  1324. wl->bss_type = BSS_TYPE_AP_BSS;
  1325. break;
  1326. default:
  1327. ret = -EOPNOTSUPP;
  1328. goto out;
  1329. }
  1330. memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
  1331. if (wl->state != WL1271_STATE_OFF) {
  1332. wl1271_error("cannot start because not in off state: %d",
  1333. wl->state);
  1334. ret = -EBUSY;
  1335. goto out;
  1336. }
  1337. while (retries) {
  1338. retries--;
  1339. ret = wl1271_chip_wakeup(wl);
  1340. if (ret < 0)
  1341. goto power_off;
  1342. ret = wl1271_boot(wl);
  1343. if (ret < 0)
  1344. goto power_off;
  1345. ret = wl1271_hw_init(wl);
  1346. if (ret < 0)
  1347. goto irq_disable;
  1348. booted = true;
  1349. break;
  1350. irq_disable:
  1351. mutex_unlock(&wl->mutex);
  1352. /* Unlocking the mutex in the middle of handling is
  1353. inherently unsafe. In this case we deem it safe to do,
  1354. because we need to let any possibly pending IRQ out of
  1355. the system (and while we are WL1271_STATE_OFF the IRQ
  1356. work function will not do anything.) Also, any other
  1357. possible concurrent operations will fail due to the
  1358. current state, hence the wl1271 struct should be safe. */
  1359. wl1271_disable_interrupts(wl);
  1360. wl1271_flush_deferred_work(wl);
  1361. cancel_work_sync(&wl->netstack_work);
  1362. mutex_lock(&wl->mutex);
  1363. power_off:
  1364. wl1271_power_off(wl);
  1365. }
  1366. if (!booted) {
  1367. wl1271_error("firmware boot failed despite %d retries",
  1368. WL1271_BOOT_RETRIES);
  1369. goto out;
  1370. }
  1371. wl->vif = vif;
  1372. wl->state = WL1271_STATE_ON;
  1373. set_bit(WL1271_FLAG_IF_INITIALIZED, &wl->flags);
  1374. wl1271_info("firmware booted (%s)", wl->chip.fw_ver_str);
  1375. /* update hw/fw version info in wiphy struct */
  1376. wiphy->hw_version = wl->chip.id;
  1377. strncpy(wiphy->fw_version, wl->chip.fw_ver_str,
  1378. sizeof(wiphy->fw_version));
  1379. /* Check if any quirks are needed with older fw versions */
  1380. wl->quirks |= wl1271_get_fw_ver_quirks(wl);
  1381. /*
  1382. * Now we know if 11a is supported (info from the NVS), so disable
  1383. * 11a channels if not supported
  1384. */
  1385. if (!wl->enable_11a)
  1386. wiphy->bands[IEEE80211_BAND_5GHZ]->n_channels = 0;
  1387. wl1271_debug(DEBUG_MAC80211, "11a is %ssupported",
  1388. wl->enable_11a ? "" : "not ");
  1389. out:
  1390. mutex_unlock(&wl->mutex);
  1391. mutex_lock(&wl_list_mutex);
  1392. if (!ret)
  1393. list_add(&wl->list, &wl_list);
  1394. mutex_unlock(&wl_list_mutex);
  1395. return ret;
  1396. }
  1397. static void __wl1271_op_remove_interface(struct wl1271 *wl,
  1398. bool reset_tx_queues)
  1399. {
  1400. int i;
  1401. wl1271_debug(DEBUG_MAC80211, "mac80211 remove interface");
  1402. /* because of hardware recovery, we may get here twice */
  1403. if (wl->state != WL1271_STATE_ON)
  1404. return;
  1405. wl1271_info("down");
  1406. mutex_lock(&wl_list_mutex);
  1407. list_del(&wl->list);
  1408. mutex_unlock(&wl_list_mutex);
  1409. /* enable dyn ps just in case (if left on due to fw crash etc) */
  1410. if (wl->bss_type == BSS_TYPE_STA_BSS)
  1411. ieee80211_enable_dyn_ps(wl->vif);
  1412. if (wl->scan.state != WL1271_SCAN_STATE_IDLE) {
  1413. wl->scan.state = WL1271_SCAN_STATE_IDLE;
  1414. memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
  1415. wl->scan.req = NULL;
  1416. ieee80211_scan_completed(wl->hw, true);
  1417. }
  1418. /*
  1419. * this must be before the cancel_work calls below, so that the work
  1420. * functions don't perform further work.
  1421. */
  1422. wl->state = WL1271_STATE_OFF;
  1423. mutex_unlock(&wl->mutex);
  1424. wl1271_disable_interrupts(wl);
  1425. wl1271_flush_deferred_work(wl);
  1426. cancel_delayed_work_sync(&wl->scan_complete_work);
  1427. cancel_work_sync(&wl->netstack_work);
  1428. cancel_work_sync(&wl->tx_work);
  1429. cancel_delayed_work_sync(&wl->pspoll_work);
  1430. cancel_delayed_work_sync(&wl->elp_work);
  1431. mutex_lock(&wl->mutex);
  1432. /* let's notify MAC80211 about the remaining pending TX frames */
  1433. wl1271_tx_reset(wl, reset_tx_queues);
  1434. wl1271_power_off(wl);
  1435. memset(wl->bssid, 0, ETH_ALEN);
  1436. memset(wl->ssid, 0, IW_ESSID_MAX_SIZE + 1);
  1437. wl->ssid_len = 0;
  1438. wl->bss_type = MAX_BSS_TYPE;
  1439. wl->set_bss_type = MAX_BSS_TYPE;
  1440. wl->band = IEEE80211_BAND_2GHZ;
  1441. wl->rx_counter = 0;
  1442. wl->psm_entry_retry = 0;
  1443. wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
  1444. wl->tx_blocks_available = 0;
  1445. wl->tx_allocated_blocks = 0;
  1446. wl->tx_results_count = 0;
  1447. wl->tx_packets_count = 0;
  1448. wl->tx_security_last_seq = 0;
  1449. wl->tx_security_seq = 0;
  1450. wl->time_offset = 0;
  1451. wl->session_counter = 0;
  1452. wl->rate_set = CONF_TX_RATE_MASK_BASIC;
  1453. wl->vif = NULL;
  1454. wl->filters = 0;
  1455. wl1271_free_ap_keys(wl);
  1456. memset(wl->ap_hlid_map, 0, sizeof(wl->ap_hlid_map));
  1457. wl->ap_fw_ps_map = 0;
  1458. wl->ap_ps_map = 0;
  1459. wl->sched_scanning = false;
  1460. /*
  1461. * this is performed after the cancel_work calls and the associated
  1462. * mutex_lock, so that wl1271_op_add_interface does not accidentally
  1463. * get executed before all these vars have been reset.
  1464. */
  1465. wl->flags = 0;
  1466. for (i = 0; i < NUM_TX_QUEUES; i++)
  1467. wl->tx_blocks_freed[i] = 0;
  1468. wl1271_debugfs_reset(wl);
  1469. kfree(wl->fw_status);
  1470. wl->fw_status = NULL;
  1471. kfree(wl->tx_res_if);
  1472. wl->tx_res_if = NULL;
  1473. kfree(wl->target_mem_map);
  1474. wl->target_mem_map = NULL;
  1475. }
  1476. static void wl1271_op_remove_interface(struct ieee80211_hw *hw,
  1477. struct ieee80211_vif *vif)
  1478. {
  1479. struct wl1271 *wl = hw->priv;
  1480. mutex_lock(&wl->mutex);
  1481. /*
  1482. * wl->vif can be null here if someone shuts down the interface
  1483. * just when hardware recovery has been started.
  1484. */
  1485. if (wl->vif) {
  1486. WARN_ON(wl->vif != vif);
  1487. __wl1271_op_remove_interface(wl, true);
  1488. }
  1489. mutex_unlock(&wl->mutex);
  1490. cancel_work_sync(&wl->recovery_work);
  1491. }
  1492. void wl1271_configure_filters(struct wl1271 *wl, unsigned int filters)
  1493. {
  1494. wl1271_set_default_filters(wl);
  1495. /* combine requested filters with current filter config */
  1496. filters = wl->filters | filters;
  1497. wl1271_debug(DEBUG_FILTERS, "RX filters set: ");
  1498. if (filters & FIF_PROMISC_IN_BSS) {
  1499. wl1271_debug(DEBUG_FILTERS, " - FIF_PROMISC_IN_BSS");
  1500. wl->rx_config &= ~CFG_UNI_FILTER_EN;
  1501. wl->rx_config |= CFG_BSSID_FILTER_EN;
  1502. }
  1503. if (filters & FIF_BCN_PRBRESP_PROMISC) {
  1504. wl1271_debug(DEBUG_FILTERS, " - FIF_BCN_PRBRESP_PROMISC");
  1505. wl->rx_config &= ~CFG_BSSID_FILTER_EN;
  1506. wl->rx_config &= ~CFG_SSID_FILTER_EN;
  1507. }
  1508. if (filters & FIF_OTHER_BSS) {
  1509. wl1271_debug(DEBUG_FILTERS, " - FIF_OTHER_BSS");
  1510. wl->rx_config &= ~CFG_BSSID_FILTER_EN;
  1511. }
  1512. if (filters & FIF_CONTROL) {
  1513. wl1271_debug(DEBUG_FILTERS, " - FIF_CONTROL");
  1514. wl->rx_filter |= CFG_RX_CTL_EN;
  1515. }
  1516. if (filters & FIF_FCSFAIL) {
  1517. wl1271_debug(DEBUG_FILTERS, " - FIF_FCSFAIL");
  1518. wl->rx_filter |= CFG_RX_FCS_ERROR;
  1519. }
  1520. }
  1521. static int wl1271_dummy_join(struct wl1271 *wl)
  1522. {
  1523. int ret = 0;
  1524. /* we need to use a dummy BSSID for now */
  1525. static const u8 dummy_bssid[ETH_ALEN] = { 0x0b, 0xad, 0xde,
  1526. 0xad, 0xbe, 0xef };
  1527. memcpy(wl->bssid, dummy_bssid, ETH_ALEN);
  1528. /* pass through frames from all BSS */
  1529. wl1271_configure_filters(wl, FIF_OTHER_BSS);
  1530. ret = wl1271_cmd_join(wl, wl->set_bss_type);
  1531. if (ret < 0)
  1532. goto out;
  1533. set_bit(WL1271_FLAG_JOINED, &wl->flags);
  1534. out:
  1535. return ret;
  1536. }
  1537. static int wl1271_join(struct wl1271 *wl, bool set_assoc)
  1538. {
  1539. int ret;
  1540. /*
  1541. * One of the side effects of the JOIN command is that is clears
  1542. * WPA/WPA2 keys from the chipset. Performing a JOIN while associated
  1543. * to a WPA/WPA2 access point will therefore kill the data-path.
  1544. * Currently the only valid scenario for JOIN during association
  1545. * is on roaming, in which case we will also be given new keys.
  1546. * Keep the below message for now, unless it starts bothering
  1547. * users who really like to roam a lot :)
  1548. */
  1549. if (test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags))
  1550. wl1271_info("JOIN while associated.");
  1551. if (set_assoc)
  1552. set_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags);
  1553. ret = wl1271_cmd_join(wl, wl->set_bss_type);
  1554. if (ret < 0)
  1555. goto out;
  1556. set_bit(WL1271_FLAG_JOINED, &wl->flags);
  1557. if (!test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags))
  1558. goto out;
  1559. /*
  1560. * The join command disable the keep-alive mode, shut down its process,
  1561. * and also clear the template config, so we need to reset it all after
  1562. * the join. The acx_aid starts the keep-alive process, and the order
  1563. * of the commands below is relevant.
  1564. */
  1565. ret = wl1271_acx_keep_alive_mode(wl, true);
  1566. if (ret < 0)
  1567. goto out;
  1568. ret = wl1271_acx_aid(wl, wl->aid);
  1569. if (ret < 0)
  1570. goto out;
  1571. ret = wl1271_cmd_build_klv_null_data(wl);
  1572. if (ret < 0)
  1573. goto out;
  1574. ret = wl1271_acx_keep_alive_config(wl, CMD_TEMPL_KLV_IDX_NULL_DATA,
  1575. ACX_KEEP_ALIVE_TPL_VALID);
  1576. if (ret < 0)
  1577. goto out;
  1578. out:
  1579. return ret;
  1580. }
  1581. static int wl1271_unjoin(struct wl1271 *wl)
  1582. {
  1583. int ret;
  1584. /* to stop listening to a channel, we disconnect */
  1585. ret = wl1271_cmd_disconnect(wl);
  1586. if (ret < 0)
  1587. goto out;
  1588. clear_bit(WL1271_FLAG_JOINED, &wl->flags);
  1589. memset(wl->bssid, 0, ETH_ALEN);
  1590. /* stop filtering packets based on bssid */
  1591. wl1271_configure_filters(wl, FIF_OTHER_BSS);
  1592. out:
  1593. return ret;
  1594. }
  1595. static void wl1271_set_band_rate(struct wl1271 *wl)
  1596. {
  1597. if (wl->band == IEEE80211_BAND_2GHZ)
  1598. wl->basic_rate_set = wl->conf.tx.basic_rate;
  1599. else
  1600. wl->basic_rate_set = wl->conf.tx.basic_rate_5;
  1601. }
  1602. static int wl1271_sta_handle_idle(struct wl1271 *wl, bool idle)
  1603. {
  1604. int ret;
  1605. if (idle) {
  1606. if (test_bit(WL1271_FLAG_JOINED, &wl->flags)) {
  1607. ret = wl1271_unjoin(wl);
  1608. if (ret < 0)
  1609. goto out;
  1610. }
  1611. wl->rate_set = wl1271_tx_min_rate_get(wl);
  1612. ret = wl1271_acx_sta_rate_policies(wl);
  1613. if (ret < 0)
  1614. goto out;
  1615. ret = wl1271_acx_keep_alive_config(
  1616. wl, CMD_TEMPL_KLV_IDX_NULL_DATA,
  1617. ACX_KEEP_ALIVE_TPL_INVALID);
  1618. if (ret < 0)
  1619. goto out;
  1620. set_bit(WL1271_FLAG_IDLE, &wl->flags);
  1621. } else {
  1622. /* increment the session counter */
  1623. wl->session_counter++;
  1624. if (wl->session_counter >= SESSION_COUNTER_MAX)
  1625. wl->session_counter = 0;
  1626. /* The current firmware only supports sched_scan in idle */
  1627. if (wl->sched_scanning) {
  1628. wl1271_scan_sched_scan_stop(wl);
  1629. ieee80211_sched_scan_stopped(wl->hw);
  1630. }
  1631. ret = wl1271_dummy_join(wl);
  1632. if (ret < 0)
  1633. goto out;
  1634. clear_bit(WL1271_FLAG_IDLE, &wl->flags);
  1635. }
  1636. out:
  1637. return ret;
  1638. }
  1639. static int wl1271_op_config(struct ieee80211_hw *hw, u32 changed)
  1640. {
  1641. struct wl1271 *wl = hw->priv;
  1642. struct ieee80211_conf *conf = &hw->conf;
  1643. int channel, ret = 0;
  1644. bool is_ap;
  1645. channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
  1646. wl1271_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d %s"
  1647. " changed 0x%x",
  1648. channel,
  1649. conf->flags & IEEE80211_CONF_PS ? "on" : "off",
  1650. conf->power_level,
  1651. conf->flags & IEEE80211_CONF_IDLE ? "idle" : "in use",
  1652. changed);
  1653. /*
  1654. * mac80211 will go to idle nearly immediately after transmitting some
  1655. * frames, such as the deauth. To make sure those frames reach the air,
  1656. * wait here until the TX queue is fully flushed.
  1657. */
  1658. if ((changed & IEEE80211_CONF_CHANGE_IDLE) &&
  1659. (conf->flags & IEEE80211_CONF_IDLE))
  1660. wl1271_tx_flush(wl);
  1661. mutex_lock(&wl->mutex);
  1662. if (unlikely(wl->state == WL1271_STATE_OFF)) {
  1663. /* we support configuring the channel and band while off */
  1664. if ((changed & IEEE80211_CONF_CHANGE_CHANNEL)) {
  1665. wl->band = conf->channel->band;
  1666. wl->channel = channel;
  1667. }
  1668. goto out;
  1669. }
  1670. is_ap = (wl->bss_type == BSS_TYPE_AP_BSS);
  1671. ret = wl1271_ps_elp_wakeup(wl);
  1672. if (ret < 0)
  1673. goto out;
  1674. /* if the channel changes while joined, join again */
  1675. if (changed & IEEE80211_CONF_CHANGE_CHANNEL &&
  1676. ((wl->band != conf->channel->band) ||
  1677. (wl->channel != channel))) {
  1678. wl->band = conf->channel->band;
  1679. wl->channel = channel;
  1680. if (!is_ap) {
  1681. /*
  1682. * FIXME: the mac80211 should really provide a fixed
  1683. * rate to use here. for now, just use the smallest
  1684. * possible rate for the band as a fixed rate for
  1685. * association frames and other control messages.
  1686. */
  1687. if (!test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags))
  1688. wl1271_set_band_rate(wl);
  1689. wl->basic_rate = wl1271_tx_min_rate_get(wl);
  1690. ret = wl1271_acx_sta_rate_policies(wl);
  1691. if (ret < 0)
  1692. wl1271_warning("rate policy for channel "
  1693. "failed %d", ret);
  1694. if (test_bit(WL1271_FLAG_JOINED, &wl->flags)) {
  1695. ret = wl1271_join(wl, false);
  1696. if (ret < 0)
  1697. wl1271_warning("cmd join on channel "
  1698. "failed %d", ret);
  1699. }
  1700. }
  1701. }
  1702. if (changed & IEEE80211_CONF_CHANGE_IDLE && !is_ap) {
  1703. ret = wl1271_sta_handle_idle(wl,
  1704. conf->flags & IEEE80211_CONF_IDLE);
  1705. if (ret < 0)
  1706. wl1271_warning("idle mode change failed %d", ret);
  1707. }
  1708. /*
  1709. * if mac80211 changes the PSM mode, make sure the mode is not
  1710. * incorrectly changed after the pspoll failure active window.
  1711. */
  1712. if (changed & IEEE80211_CONF_CHANGE_PS)
  1713. clear_bit(WL1271_FLAG_PSPOLL_FAILURE, &wl->flags);
  1714. if (conf->flags & IEEE80211_CONF_PS &&
  1715. !test_bit(WL1271_FLAG_PSM_REQUESTED, &wl->flags)) {
  1716. set_bit(WL1271_FLAG_PSM_REQUESTED, &wl->flags);
  1717. /*
  1718. * We enter PSM only if we're already associated.
  1719. * If we're not, we'll enter it when joining an SSID,
  1720. * through the bss_info_changed() hook.
  1721. */
  1722. if (test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags)) {
  1723. wl1271_debug(DEBUG_PSM, "psm enabled");
  1724. ret = wl1271_ps_set_mode(wl, STATION_POWER_SAVE_MODE,
  1725. wl->basic_rate, true);
  1726. }
  1727. } else if (!(conf->flags & IEEE80211_CONF_PS) &&
  1728. test_bit(WL1271_FLAG_PSM_REQUESTED, &wl->flags)) {
  1729. wl1271_debug(DEBUG_PSM, "psm disabled");
  1730. clear_bit(WL1271_FLAG_PSM_REQUESTED, &wl->flags);
  1731. if (test_bit(WL1271_FLAG_PSM, &wl->flags))
  1732. ret = wl1271_ps_set_mode(wl, STATION_ACTIVE_MODE,
  1733. wl->basic_rate, true);
  1734. }
  1735. if (conf->power_level != wl->power_level) {
  1736. ret = wl1271_acx_tx_power(wl, conf->power_level);
  1737. if (ret < 0)
  1738. goto out_sleep;
  1739. wl->power_level = conf->power_level;
  1740. }
  1741. out_sleep:
  1742. wl1271_ps_elp_sleep(wl);
  1743. out:
  1744. mutex_unlock(&wl->mutex);
  1745. return ret;
  1746. }
  1747. struct wl1271_filter_params {
  1748. bool enabled;
  1749. int mc_list_length;
  1750. u8 mc_list[ACX_MC_ADDRESS_GROUP_MAX][ETH_ALEN];
  1751. };
  1752. static u64 wl1271_op_prepare_multicast(struct ieee80211_hw *hw,
  1753. struct netdev_hw_addr_list *mc_list)
  1754. {
  1755. struct wl1271_filter_params *fp;
  1756. struct netdev_hw_addr *ha;
  1757. struct wl1271 *wl = hw->priv;
  1758. if (unlikely(wl->state == WL1271_STATE_OFF))
  1759. return 0;
  1760. fp = kzalloc(sizeof(*fp), GFP_ATOMIC);
  1761. if (!fp) {
  1762. wl1271_error("Out of memory setting filters.");
  1763. return 0;
  1764. }
  1765. /* update multicast filtering parameters */
  1766. fp->mc_list_length = 0;
  1767. if (netdev_hw_addr_list_count(mc_list) > ACX_MC_ADDRESS_GROUP_MAX) {
  1768. fp->enabled = false;
  1769. } else {
  1770. fp->enabled = true;
  1771. netdev_hw_addr_list_for_each(ha, mc_list) {
  1772. memcpy(fp->mc_list[fp->mc_list_length],
  1773. ha->addr, ETH_ALEN);
  1774. fp->mc_list_length++;
  1775. }
  1776. }
  1777. return (u64)(unsigned long)fp;
  1778. }
  1779. #define WL1271_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
  1780. FIF_ALLMULTI | \
  1781. FIF_FCSFAIL | \
  1782. FIF_BCN_PRBRESP_PROMISC | \
  1783. FIF_CONTROL | \
  1784. FIF_OTHER_BSS)
  1785. static void wl1271_op_configure_filter(struct ieee80211_hw *hw,
  1786. unsigned int changed,
  1787. unsigned int *total, u64 multicast)
  1788. {
  1789. struct wl1271_filter_params *fp = (void *)(unsigned long)multicast;
  1790. struct wl1271 *wl = hw->priv;
  1791. int ret;
  1792. wl1271_debug(DEBUG_MAC80211, "mac80211 configure filter changed %x"
  1793. " total %x", changed, *total);
  1794. mutex_lock(&wl->mutex);
  1795. *total &= WL1271_SUPPORTED_FILTERS;
  1796. changed &= WL1271_SUPPORTED_FILTERS;
  1797. if (unlikely(wl->state == WL1271_STATE_OFF))
  1798. goto out;
  1799. ret = wl1271_ps_elp_wakeup(wl);
  1800. if (ret < 0)
  1801. goto out;
  1802. if (wl->bss_type != BSS_TYPE_AP_BSS) {
  1803. if (*total & FIF_ALLMULTI)
  1804. ret = wl1271_acx_group_address_tbl(wl, false, NULL, 0);
  1805. else if (fp)
  1806. ret = wl1271_acx_group_address_tbl(wl, fp->enabled,
  1807. fp->mc_list,
  1808. fp->mc_list_length);
  1809. if (ret < 0)
  1810. goto out_sleep;
  1811. }
  1812. /* determine, whether supported filter values have changed */
  1813. if (changed == 0)
  1814. goto out_sleep;
  1815. /* configure filters */
  1816. wl->filters = *total;
  1817. wl1271_configure_filters(wl, 0);
  1818. /* apply configured filters */
  1819. ret = wl1271_acx_rx_config(wl, wl->rx_config, wl->rx_filter);
  1820. if (ret < 0)
  1821. goto out_sleep;
  1822. out_sleep:
  1823. wl1271_ps_elp_sleep(wl);
  1824. out:
  1825. mutex_unlock(&wl->mutex);
  1826. kfree(fp);
  1827. }
  1828. static int wl1271_record_ap_key(struct wl1271 *wl, u8 id, u8 key_type,
  1829. u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
  1830. u16 tx_seq_16)
  1831. {
  1832. struct wl1271_ap_key *ap_key;
  1833. int i;
  1834. wl1271_debug(DEBUG_CRYPT, "record ap key id %d", (int)id);
  1835. if (key_size > MAX_KEY_SIZE)
  1836. return -EINVAL;
  1837. /*
  1838. * Find next free entry in ap_keys. Also check we are not replacing
  1839. * an existing key.
  1840. */
  1841. for (i = 0; i < MAX_NUM_KEYS; i++) {
  1842. if (wl->recorded_ap_keys[i] == NULL)
  1843. break;
  1844. if (wl->recorded_ap_keys[i]->id == id) {
  1845. wl1271_warning("trying to record key replacement");
  1846. return -EINVAL;
  1847. }
  1848. }
  1849. if (i == MAX_NUM_KEYS)
  1850. return -EBUSY;
  1851. ap_key = kzalloc(sizeof(*ap_key), GFP_KERNEL);
  1852. if (!ap_key)
  1853. return -ENOMEM;
  1854. ap_key->id = id;
  1855. ap_key->key_type = key_type;
  1856. ap_key->key_size = key_size;
  1857. memcpy(ap_key->key, key, key_size);
  1858. ap_key->hlid = hlid;
  1859. ap_key->tx_seq_32 = tx_seq_32;
  1860. ap_key->tx_seq_16 = tx_seq_16;
  1861. wl->recorded_ap_keys[i] = ap_key;
  1862. return 0;
  1863. }
  1864. static void wl1271_free_ap_keys(struct wl1271 *wl)
  1865. {
  1866. int i;
  1867. for (i = 0; i < MAX_NUM_KEYS; i++) {
  1868. kfree(wl->recorded_ap_keys[i]);
  1869. wl->recorded_ap_keys[i] = NULL;
  1870. }
  1871. }
  1872. static int wl1271_ap_init_hwenc(struct wl1271 *wl)
  1873. {
  1874. int i, ret = 0;
  1875. struct wl1271_ap_key *key;
  1876. bool wep_key_added = false;
  1877. for (i = 0; i < MAX_NUM_KEYS; i++) {
  1878. if (wl->recorded_ap_keys[i] == NULL)
  1879. break;
  1880. key = wl->recorded_ap_keys[i];
  1881. ret = wl1271_cmd_set_ap_key(wl, KEY_ADD_OR_REPLACE,
  1882. key->id, key->key_type,
  1883. key->key_size, key->key,
  1884. key->hlid, key->tx_seq_32,
  1885. key->tx_seq_16);
  1886. if (ret < 0)
  1887. goto out;
  1888. if (key->key_type == KEY_WEP)
  1889. wep_key_added = true;
  1890. }
  1891. if (wep_key_added) {
  1892. ret = wl1271_cmd_set_ap_default_wep_key(wl, wl->default_key);
  1893. if (ret < 0)
  1894. goto out;
  1895. }
  1896. out:
  1897. wl1271_free_ap_keys(wl);
  1898. return ret;
  1899. }
  1900. static int wl1271_set_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
  1901. u8 key_size, const u8 *key, u32 tx_seq_32,
  1902. u16 tx_seq_16, struct ieee80211_sta *sta)
  1903. {
  1904. int ret;
  1905. bool is_ap = (wl->bss_type == BSS_TYPE_AP_BSS);
  1906. if (is_ap) {
  1907. struct wl1271_station *wl_sta;
  1908. u8 hlid;
  1909. if (sta) {
  1910. wl_sta = (struct wl1271_station *)sta->drv_priv;
  1911. hlid = wl_sta->hlid;
  1912. } else {
  1913. hlid = WL1271_AP_BROADCAST_HLID;
  1914. }
  1915. if (!test_bit(WL1271_FLAG_AP_STARTED, &wl->flags)) {
  1916. /*
  1917. * We do not support removing keys after AP shutdown.
  1918. * Pretend we do to make mac80211 happy.
  1919. */
  1920. if (action != KEY_ADD_OR_REPLACE)
  1921. return 0;
  1922. ret = wl1271_record_ap_key(wl, id,
  1923. key_type, key_size,
  1924. key, hlid, tx_seq_32,
  1925. tx_seq_16);
  1926. } else {
  1927. ret = wl1271_cmd_set_ap_key(wl, action,
  1928. id, key_type, key_size,
  1929. key, hlid, tx_seq_32,
  1930. tx_seq_16);
  1931. }
  1932. if (ret < 0)
  1933. return ret;
  1934. } else {
  1935. const u8 *addr;
  1936. static const u8 bcast_addr[ETH_ALEN] = {
  1937. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  1938. };
  1939. addr = sta ? sta->addr : bcast_addr;
  1940. if (is_zero_ether_addr(addr)) {
  1941. /* We dont support TX only encryption */
  1942. return -EOPNOTSUPP;
  1943. }
  1944. /* The wl1271 does not allow to remove unicast keys - they
  1945. will be cleared automatically on next CMD_JOIN. Ignore the
  1946. request silently, as we dont want the mac80211 to emit
  1947. an error message. */
  1948. if (action == KEY_REMOVE && !is_broadcast_ether_addr(addr))
  1949. return 0;
  1950. ret = wl1271_cmd_set_sta_key(wl, action,
  1951. id, key_type, key_size,
  1952. key, addr, tx_seq_32,
  1953. tx_seq_16);
  1954. if (ret < 0)
  1955. return ret;
  1956. /* the default WEP key needs to be configured at least once */
  1957. if (key_type == KEY_WEP) {
  1958. ret = wl1271_cmd_set_sta_default_wep_key(wl,
  1959. wl->default_key);
  1960. if (ret < 0)
  1961. return ret;
  1962. }
  1963. }
  1964. return 0;
  1965. }
  1966. static int wl1271_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  1967. struct ieee80211_vif *vif,
  1968. struct ieee80211_sta *sta,
  1969. struct ieee80211_key_conf *key_conf)
  1970. {
  1971. struct wl1271 *wl = hw->priv;
  1972. int ret;
  1973. u32 tx_seq_32 = 0;
  1974. u16 tx_seq_16 = 0;
  1975. u8 key_type;
  1976. wl1271_debug(DEBUG_MAC80211, "mac80211 set key");
  1977. wl1271_debug(DEBUG_CRYPT, "CMD: 0x%x sta: %p", cmd, sta);
  1978. wl1271_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
  1979. key_conf->cipher, key_conf->keyidx,
  1980. key_conf->keylen, key_conf->flags);
  1981. wl1271_dump(DEBUG_CRYPT, "KEY: ", key_conf->key, key_conf->keylen);
  1982. mutex_lock(&wl->mutex);
  1983. if (unlikely(wl->state == WL1271_STATE_OFF)) {
  1984. ret = -EAGAIN;
  1985. goto out_unlock;
  1986. }
  1987. ret = wl1271_ps_elp_wakeup(wl);
  1988. if (ret < 0)
  1989. goto out_unlock;
  1990. switch (key_conf->cipher) {
  1991. case WLAN_CIPHER_SUITE_WEP40:
  1992. case WLAN_CIPHER_SUITE_WEP104:
  1993. key_type = KEY_WEP;
  1994. key_conf->hw_key_idx = key_conf->keyidx;
  1995. break;
  1996. case WLAN_CIPHER_SUITE_TKIP:
  1997. key_type = KEY_TKIP;
  1998. key_conf->hw_key_idx = key_conf->keyidx;
  1999. tx_seq_32 = WL1271_TX_SECURITY_HI32(wl->tx_security_seq);
  2000. tx_seq_16 = WL1271_TX_SECURITY_LO16(wl->tx_security_seq);
  2001. break;
  2002. case WLAN_CIPHER_SUITE_CCMP:
  2003. key_type = KEY_AES;
  2004. key_conf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2005. tx_seq_32 = WL1271_TX_SECURITY_HI32(wl->tx_security_seq);
  2006. tx_seq_16 = WL1271_TX_SECURITY_LO16(wl->tx_security_seq);
  2007. break;
  2008. case WL1271_CIPHER_SUITE_GEM:
  2009. key_type = KEY_GEM;
  2010. tx_seq_32 = WL1271_TX_SECURITY_HI32(wl->tx_security_seq);
  2011. tx_seq_16 = WL1271_TX_SECURITY_LO16(wl->tx_security_seq);
  2012. break;
  2013. default:
  2014. wl1271_error("Unknown key algo 0x%x", key_conf->cipher);
  2015. ret = -EOPNOTSUPP;
  2016. goto out_sleep;
  2017. }
  2018. switch (cmd) {
  2019. case SET_KEY:
  2020. ret = wl1271_set_key(wl, KEY_ADD_OR_REPLACE,
  2021. key_conf->keyidx, key_type,
  2022. key_conf->keylen, key_conf->key,
  2023. tx_seq_32, tx_seq_16, sta);
  2024. if (ret < 0) {
  2025. wl1271_error("Could not add or replace key");
  2026. goto out_sleep;
  2027. }
  2028. break;
  2029. case DISABLE_KEY:
  2030. ret = wl1271_set_key(wl, KEY_REMOVE,
  2031. key_conf->keyidx, key_type,
  2032. key_conf->keylen, key_conf->key,
  2033. 0, 0, sta);
  2034. if (ret < 0) {
  2035. wl1271_error("Could not remove key");
  2036. goto out_sleep;
  2037. }
  2038. break;
  2039. default:
  2040. wl1271_error("Unsupported key cmd 0x%x", cmd);
  2041. ret = -EOPNOTSUPP;
  2042. break;
  2043. }
  2044. out_sleep:
  2045. wl1271_ps_elp_sleep(wl);
  2046. out_unlock:
  2047. mutex_unlock(&wl->mutex);
  2048. return ret;
  2049. }
  2050. static int wl1271_op_hw_scan(struct ieee80211_hw *hw,
  2051. struct ieee80211_vif *vif,
  2052. struct cfg80211_scan_request *req)
  2053. {
  2054. struct wl1271 *wl = hw->priv;
  2055. int ret;
  2056. u8 *ssid = NULL;
  2057. size_t len = 0;
  2058. wl1271_debug(DEBUG_MAC80211, "mac80211 hw scan");
  2059. if (req->n_ssids) {
  2060. ssid = req->ssids[0].ssid;
  2061. len = req->ssids[0].ssid_len;
  2062. }
  2063. mutex_lock(&wl->mutex);
  2064. if (wl->state == WL1271_STATE_OFF) {
  2065. /*
  2066. * We cannot return -EBUSY here because cfg80211 will expect
  2067. * a call to ieee80211_scan_completed if we do - in this case
  2068. * there won't be any call.
  2069. */
  2070. ret = -EAGAIN;
  2071. goto out;
  2072. }
  2073. ret = wl1271_ps_elp_wakeup(wl);
  2074. if (ret < 0)
  2075. goto out;
  2076. ret = wl1271_scan(hw->priv, ssid, len, req);
  2077. wl1271_ps_elp_sleep(wl);
  2078. out:
  2079. mutex_unlock(&wl->mutex);
  2080. return ret;
  2081. }
  2082. static int wl1271_op_sched_scan_start(struct ieee80211_hw *hw,
  2083. struct ieee80211_vif *vif,
  2084. struct cfg80211_sched_scan_request *req,
  2085. struct ieee80211_sched_scan_ies *ies)
  2086. {
  2087. struct wl1271 *wl = hw->priv;
  2088. int ret;
  2089. wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_start");
  2090. mutex_lock(&wl->mutex);
  2091. ret = wl1271_ps_elp_wakeup(wl);
  2092. if (ret < 0)
  2093. goto out;
  2094. ret = wl1271_scan_sched_scan_config(wl, req, ies);
  2095. if (ret < 0)
  2096. goto out_sleep;
  2097. ret = wl1271_scan_sched_scan_start(wl);
  2098. if (ret < 0)
  2099. goto out_sleep;
  2100. wl->sched_scanning = true;
  2101. out_sleep:
  2102. wl1271_ps_elp_sleep(wl);
  2103. out:
  2104. mutex_unlock(&wl->mutex);
  2105. return ret;
  2106. }
  2107. static void wl1271_op_sched_scan_stop(struct ieee80211_hw *hw,
  2108. struct ieee80211_vif *vif)
  2109. {
  2110. struct wl1271 *wl = hw->priv;
  2111. int ret;
  2112. wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_stop");
  2113. mutex_lock(&wl->mutex);
  2114. ret = wl1271_ps_elp_wakeup(wl);
  2115. if (ret < 0)
  2116. goto out;
  2117. wl1271_scan_sched_scan_stop(wl);
  2118. wl1271_ps_elp_sleep(wl);
  2119. out:
  2120. mutex_unlock(&wl->mutex);
  2121. }
  2122. static int wl1271_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
  2123. {
  2124. struct wl1271 *wl = hw->priv;
  2125. int ret = 0;
  2126. mutex_lock(&wl->mutex);
  2127. if (unlikely(wl->state == WL1271_STATE_OFF)) {
  2128. ret = -EAGAIN;
  2129. goto out;
  2130. }
  2131. ret = wl1271_ps_elp_wakeup(wl);
  2132. if (ret < 0)
  2133. goto out;
  2134. ret = wl1271_acx_frag_threshold(wl, value);
  2135. if (ret < 0)
  2136. wl1271_warning("wl1271_op_set_frag_threshold failed: %d", ret);
  2137. wl1271_ps_elp_sleep(wl);
  2138. out:
  2139. mutex_unlock(&wl->mutex);
  2140. return ret;
  2141. }
  2142. static int wl1271_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  2143. {
  2144. struct wl1271 *wl = hw->priv;
  2145. int ret = 0;
  2146. mutex_lock(&wl->mutex);
  2147. if (unlikely(wl->state == WL1271_STATE_OFF)) {
  2148. ret = -EAGAIN;
  2149. goto out;
  2150. }
  2151. ret = wl1271_ps_elp_wakeup(wl);
  2152. if (ret < 0)
  2153. goto out;
  2154. ret = wl1271_acx_rts_threshold(wl, value);
  2155. if (ret < 0)
  2156. wl1271_warning("wl1271_op_set_rts_threshold failed: %d", ret);
  2157. wl1271_ps_elp_sleep(wl);
  2158. out:
  2159. mutex_unlock(&wl->mutex);
  2160. return ret;
  2161. }
  2162. static int wl1271_ssid_set(struct wl1271 *wl, struct sk_buff *skb,
  2163. int offset)
  2164. {
  2165. u8 ssid_len;
  2166. const u8 *ptr = cfg80211_find_ie(WLAN_EID_SSID, skb->data + offset,
  2167. skb->len - offset);
  2168. if (!ptr) {
  2169. wl1271_error("No SSID in IEs!");
  2170. return -ENOENT;
  2171. }
  2172. ssid_len = ptr[1];
  2173. if (ssid_len > IEEE80211_MAX_SSID_LEN) {
  2174. wl1271_error("SSID is too long!");
  2175. return -EINVAL;
  2176. }
  2177. wl->ssid_len = ssid_len;
  2178. memcpy(wl->ssid, ptr+2, ssid_len);
  2179. return 0;
  2180. }
  2181. static int wl1271_bss_erp_info_changed(struct wl1271 *wl,
  2182. struct ieee80211_bss_conf *bss_conf,
  2183. u32 changed)
  2184. {
  2185. int ret = 0;
  2186. if (changed & BSS_CHANGED_ERP_SLOT) {
  2187. if (bss_conf->use_short_slot)
  2188. ret = wl1271_acx_slot(wl, SLOT_TIME_SHORT);
  2189. else
  2190. ret = wl1271_acx_slot(wl, SLOT_TIME_LONG);
  2191. if (ret < 0) {
  2192. wl1271_warning("Set slot time failed %d", ret);
  2193. goto out;
  2194. }
  2195. }
  2196. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  2197. if (bss_conf->use_short_preamble)
  2198. wl1271_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
  2199. else
  2200. wl1271_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
  2201. }
  2202. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  2203. if (bss_conf->use_cts_prot)
  2204. ret = wl1271_acx_cts_protect(wl, CTSPROTECT_ENABLE);
  2205. else
  2206. ret = wl1271_acx_cts_protect(wl, CTSPROTECT_DISABLE);
  2207. if (ret < 0) {
  2208. wl1271_warning("Set ctsprotect failed %d", ret);
  2209. goto out;
  2210. }
  2211. }
  2212. out:
  2213. return ret;
  2214. }
  2215. static int wl1271_bss_beacon_info_changed(struct wl1271 *wl,
  2216. struct ieee80211_vif *vif,
  2217. struct ieee80211_bss_conf *bss_conf,
  2218. u32 changed)
  2219. {
  2220. bool is_ap = (wl->bss_type == BSS_TYPE_AP_BSS);
  2221. int ret = 0;
  2222. if ((changed & BSS_CHANGED_BEACON_INT)) {
  2223. wl1271_debug(DEBUG_MASTER, "beacon interval updated: %d",
  2224. bss_conf->beacon_int);
  2225. wl->beacon_int = bss_conf->beacon_int;
  2226. }
  2227. if ((changed & BSS_CHANGED_BEACON)) {
  2228. struct ieee80211_hdr *hdr;
  2229. int ieoffset = offsetof(struct ieee80211_mgmt,
  2230. u.beacon.variable);
  2231. struct sk_buff *beacon = ieee80211_beacon_get(wl->hw, vif);
  2232. u16 tmpl_id;
  2233. if (!beacon)
  2234. goto out;
  2235. wl1271_debug(DEBUG_MASTER, "beacon updated");
  2236. ret = wl1271_ssid_set(wl, beacon, ieoffset);
  2237. if (ret < 0) {
  2238. dev_kfree_skb(beacon);
  2239. goto out;
  2240. }
  2241. tmpl_id = is_ap ? CMD_TEMPL_AP_BEACON :
  2242. CMD_TEMPL_BEACON;
  2243. ret = wl1271_cmd_template_set(wl, tmpl_id,
  2244. beacon->data,
  2245. beacon->len, 0,
  2246. wl1271_tx_min_rate_get(wl));
  2247. if (ret < 0) {
  2248. dev_kfree_skb(beacon);
  2249. goto out;
  2250. }
  2251. hdr = (struct ieee80211_hdr *) beacon->data;
  2252. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  2253. IEEE80211_STYPE_PROBE_RESP);
  2254. tmpl_id = is_ap ? CMD_TEMPL_AP_PROBE_RESPONSE :
  2255. CMD_TEMPL_PROBE_RESPONSE;
  2256. ret = wl1271_cmd_template_set(wl,
  2257. tmpl_id,
  2258. beacon->data,
  2259. beacon->len, 0,
  2260. wl1271_tx_min_rate_get(wl));
  2261. dev_kfree_skb(beacon);
  2262. if (ret < 0)
  2263. goto out;
  2264. }
  2265. out:
  2266. return ret;
  2267. }
  2268. /* AP mode changes */
  2269. static void wl1271_bss_info_changed_ap(struct wl1271 *wl,
  2270. struct ieee80211_vif *vif,
  2271. struct ieee80211_bss_conf *bss_conf,
  2272. u32 changed)
  2273. {
  2274. int ret = 0;
  2275. if ((changed & BSS_CHANGED_BASIC_RATES)) {
  2276. u32 rates = bss_conf->basic_rates;
  2277. wl->basic_rate_set = wl1271_tx_enabled_rates_get(wl, rates);
  2278. wl->basic_rate = wl1271_tx_min_rate_get(wl);
  2279. ret = wl1271_init_ap_rates(wl);
  2280. if (ret < 0) {
  2281. wl1271_error("AP rate policy change failed %d", ret);
  2282. goto out;
  2283. }
  2284. ret = wl1271_ap_init_templates(wl);
  2285. if (ret < 0)
  2286. goto out;
  2287. }
  2288. ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf, changed);
  2289. if (ret < 0)
  2290. goto out;
  2291. if ((changed & BSS_CHANGED_BEACON_ENABLED)) {
  2292. if (bss_conf->enable_beacon) {
  2293. if (!test_bit(WL1271_FLAG_AP_STARTED, &wl->flags)) {
  2294. ret = wl1271_cmd_start_bss(wl);
  2295. if (ret < 0)
  2296. goto out;
  2297. set_bit(WL1271_FLAG_AP_STARTED, &wl->flags);
  2298. wl1271_debug(DEBUG_AP, "started AP");
  2299. ret = wl1271_ap_init_hwenc(wl);
  2300. if (ret < 0)
  2301. goto out;
  2302. }
  2303. } else {
  2304. if (test_bit(WL1271_FLAG_AP_STARTED, &wl->flags)) {
  2305. ret = wl1271_cmd_stop_bss(wl);
  2306. if (ret < 0)
  2307. goto out;
  2308. clear_bit(WL1271_FLAG_AP_STARTED, &wl->flags);
  2309. wl1271_debug(DEBUG_AP, "stopped AP");
  2310. }
  2311. }
  2312. }
  2313. if (changed & BSS_CHANGED_IBSS) {
  2314. wl1271_debug(DEBUG_ADHOC, "ibss_joined: %d",
  2315. bss_conf->ibss_joined);
  2316. if (bss_conf->ibss_joined) {
  2317. u32 rates = bss_conf->basic_rates;
  2318. wl->basic_rate_set = wl1271_tx_enabled_rates_get(wl,
  2319. rates);
  2320. wl->basic_rate = wl1271_tx_min_rate_get(wl);
  2321. /* by default, use 11b rates */
  2322. wl->rate_set = CONF_TX_IBSS_DEFAULT_RATES;
  2323. ret = wl1271_acx_sta_rate_policies(wl);
  2324. if (ret < 0)
  2325. goto out;
  2326. }
  2327. }
  2328. ret = wl1271_bss_erp_info_changed(wl, bss_conf, changed);
  2329. if (ret < 0)
  2330. goto out;
  2331. out:
  2332. return;
  2333. }
  2334. /* STA/IBSS mode changes */
  2335. static void wl1271_bss_info_changed_sta(struct wl1271 *wl,
  2336. struct ieee80211_vif *vif,
  2337. struct ieee80211_bss_conf *bss_conf,
  2338. u32 changed)
  2339. {
  2340. bool do_join = false, set_assoc = false;
  2341. bool is_ibss = (wl->bss_type == BSS_TYPE_IBSS);
  2342. u32 sta_rate_set = 0;
  2343. int ret;
  2344. struct ieee80211_sta *sta;
  2345. bool sta_exists = false;
  2346. struct ieee80211_sta_ht_cap sta_ht_cap;
  2347. if (is_ibss) {
  2348. ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf,
  2349. changed);
  2350. if (ret < 0)
  2351. goto out;
  2352. }
  2353. if ((changed & BSS_CHANGED_BEACON_INT) && is_ibss)
  2354. do_join = true;
  2355. /* Need to update the SSID (for filtering etc) */
  2356. if ((changed & BSS_CHANGED_BEACON) && is_ibss)
  2357. do_join = true;
  2358. if ((changed & BSS_CHANGED_BEACON_ENABLED) && is_ibss) {
  2359. wl1271_debug(DEBUG_ADHOC, "ad-hoc beaconing: %s",
  2360. bss_conf->enable_beacon ? "enabled" : "disabled");
  2361. if (bss_conf->enable_beacon)
  2362. wl->set_bss_type = BSS_TYPE_IBSS;
  2363. else
  2364. wl->set_bss_type = BSS_TYPE_STA_BSS;
  2365. do_join = true;
  2366. }
  2367. if ((changed & BSS_CHANGED_CQM)) {
  2368. bool enable = false;
  2369. if (bss_conf->cqm_rssi_thold)
  2370. enable = true;
  2371. ret = wl1271_acx_rssi_snr_trigger(wl, enable,
  2372. bss_conf->cqm_rssi_thold,
  2373. bss_conf->cqm_rssi_hyst);
  2374. if (ret < 0)
  2375. goto out;
  2376. wl->rssi_thold = bss_conf->cqm_rssi_thold;
  2377. }
  2378. if ((changed & BSS_CHANGED_BSSID) &&
  2379. /*
  2380. * Now we know the correct bssid, so we send a new join command
  2381. * and enable the BSSID filter
  2382. */
  2383. memcmp(wl->bssid, bss_conf->bssid, ETH_ALEN)) {
  2384. memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
  2385. if (!is_zero_ether_addr(wl->bssid)) {
  2386. ret = wl1271_cmd_build_null_data(wl);
  2387. if (ret < 0)
  2388. goto out;
  2389. ret = wl1271_build_qos_null_data(wl);
  2390. if (ret < 0)
  2391. goto out;
  2392. /* filter out all packets not from this BSSID */
  2393. wl1271_configure_filters(wl, 0);
  2394. /* Need to update the BSSID (for filtering etc) */
  2395. do_join = true;
  2396. }
  2397. }
  2398. rcu_read_lock();
  2399. sta = ieee80211_find_sta(vif, bss_conf->bssid);
  2400. if (sta) {
  2401. /* save the supp_rates of the ap */
  2402. sta_rate_set = sta->supp_rates[wl->hw->conf.channel->band];
  2403. if (sta->ht_cap.ht_supported)
  2404. sta_rate_set |=
  2405. (sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET);
  2406. sta_ht_cap = sta->ht_cap;
  2407. sta_exists = true;
  2408. }
  2409. rcu_read_unlock();
  2410. if (sta_exists) {
  2411. /* handle new association with HT and HT information change */
  2412. if ((changed & BSS_CHANGED_HT) &&
  2413. (bss_conf->channel_type != NL80211_CHAN_NO_HT)) {
  2414. ret = wl1271_acx_set_ht_capabilities(wl, &sta_ht_cap,
  2415. true);
  2416. if (ret < 0) {
  2417. wl1271_warning("Set ht cap true failed %d",
  2418. ret);
  2419. goto out;
  2420. }
  2421. ret = wl1271_acx_set_ht_information(wl,
  2422. bss_conf->ht_operation_mode);
  2423. if (ret < 0) {
  2424. wl1271_warning("Set ht information failed %d",
  2425. ret);
  2426. goto out;
  2427. }
  2428. }
  2429. /* handle new association without HT and disassociation */
  2430. else if (changed & BSS_CHANGED_ASSOC) {
  2431. ret = wl1271_acx_set_ht_capabilities(wl, &sta_ht_cap,
  2432. false);
  2433. if (ret < 0) {
  2434. wl1271_warning("Set ht cap false failed %d",
  2435. ret);
  2436. goto out;
  2437. }
  2438. }
  2439. }
  2440. if ((changed & BSS_CHANGED_ASSOC)) {
  2441. if (bss_conf->assoc) {
  2442. u32 rates;
  2443. int ieoffset;
  2444. wl->aid = bss_conf->aid;
  2445. set_assoc = true;
  2446. wl->ps_poll_failures = 0;
  2447. /*
  2448. * use basic rates from AP, and determine lowest rate
  2449. * to use with control frames.
  2450. */
  2451. rates = bss_conf->basic_rates;
  2452. wl->basic_rate_set = wl1271_tx_enabled_rates_get(wl,
  2453. rates);
  2454. wl->basic_rate = wl1271_tx_min_rate_get(wl);
  2455. if (sta_rate_set)
  2456. wl->rate_set = wl1271_tx_enabled_rates_get(wl,
  2457. sta_rate_set);
  2458. ret = wl1271_acx_sta_rate_policies(wl);
  2459. if (ret < 0)
  2460. goto out;
  2461. /*
  2462. * with wl1271, we don't need to update the
  2463. * beacon_int and dtim_period, because the firmware
  2464. * updates it by itself when the first beacon is
  2465. * received after a join.
  2466. */
  2467. ret = wl1271_cmd_build_ps_poll(wl, wl->aid);
  2468. if (ret < 0)
  2469. goto out;
  2470. /*
  2471. * Get a template for hardware connection maintenance
  2472. */
  2473. dev_kfree_skb(wl->probereq);
  2474. wl->probereq = wl1271_cmd_build_ap_probe_req(wl, NULL);
  2475. ieoffset = offsetof(struct ieee80211_mgmt,
  2476. u.probe_req.variable);
  2477. wl1271_ssid_set(wl, wl->probereq, ieoffset);
  2478. /* enable the connection monitoring feature */
  2479. ret = wl1271_acx_conn_monit_params(wl, true);
  2480. if (ret < 0)
  2481. goto out;
  2482. /* If we want to go in PSM but we're not there yet */
  2483. if (test_bit(WL1271_FLAG_PSM_REQUESTED, &wl->flags) &&
  2484. !test_bit(WL1271_FLAG_PSM, &wl->flags)) {
  2485. enum wl1271_cmd_ps_mode mode;
  2486. mode = STATION_POWER_SAVE_MODE;
  2487. ret = wl1271_ps_set_mode(wl, mode,
  2488. wl->basic_rate,
  2489. true);
  2490. if (ret < 0)
  2491. goto out;
  2492. }
  2493. } else {
  2494. /* use defaults when not associated */
  2495. bool was_assoc =
  2496. !!test_and_clear_bit(WL1271_FLAG_STA_ASSOCIATED,
  2497. &wl->flags);
  2498. clear_bit(WL1271_FLAG_STA_STATE_SENT, &wl->flags);
  2499. wl->aid = 0;
  2500. /* free probe-request template */
  2501. dev_kfree_skb(wl->probereq);
  2502. wl->probereq = NULL;
  2503. /* re-enable dynamic ps - just in case */
  2504. ieee80211_enable_dyn_ps(wl->vif);
  2505. /* revert back to minimum rates for the current band */
  2506. wl1271_set_band_rate(wl);
  2507. wl->basic_rate = wl1271_tx_min_rate_get(wl);
  2508. ret = wl1271_acx_sta_rate_policies(wl);
  2509. if (ret < 0)
  2510. goto out;
  2511. /* disable connection monitor features */
  2512. ret = wl1271_acx_conn_monit_params(wl, false);
  2513. /* Disable the keep-alive feature */
  2514. ret = wl1271_acx_keep_alive_mode(wl, false);
  2515. if (ret < 0)
  2516. goto out;
  2517. /* restore the bssid filter and go to dummy bssid */
  2518. if (was_assoc) {
  2519. wl1271_unjoin(wl);
  2520. wl1271_dummy_join(wl);
  2521. }
  2522. }
  2523. }
  2524. ret = wl1271_bss_erp_info_changed(wl, bss_conf, changed);
  2525. if (ret < 0)
  2526. goto out;
  2527. if (changed & BSS_CHANGED_ARP_FILTER) {
  2528. __be32 addr = bss_conf->arp_addr_list[0];
  2529. WARN_ON(wl->bss_type != BSS_TYPE_STA_BSS);
  2530. if (bss_conf->arp_addr_cnt == 1 &&
  2531. bss_conf->arp_filter_enabled) {
  2532. /*
  2533. * The template should have been configured only upon
  2534. * association. however, it seems that the correct ip
  2535. * isn't being set (when sending), so we have to
  2536. * reconfigure the template upon every ip change.
  2537. */
  2538. ret = wl1271_cmd_build_arp_rsp(wl, addr);
  2539. if (ret < 0) {
  2540. wl1271_warning("build arp rsp failed: %d", ret);
  2541. goto out;
  2542. }
  2543. ret = wl1271_acx_arp_ip_filter(wl,
  2544. ACX_ARP_FILTER_ARP_FILTERING,
  2545. addr);
  2546. } else
  2547. ret = wl1271_acx_arp_ip_filter(wl, 0, addr);
  2548. if (ret < 0)
  2549. goto out;
  2550. }
  2551. if (do_join) {
  2552. ret = wl1271_join(wl, set_assoc);
  2553. if (ret < 0) {
  2554. wl1271_warning("cmd join failed %d", ret);
  2555. goto out;
  2556. }
  2557. }
  2558. out:
  2559. return;
  2560. }
  2561. static void wl1271_op_bss_info_changed(struct ieee80211_hw *hw,
  2562. struct ieee80211_vif *vif,
  2563. struct ieee80211_bss_conf *bss_conf,
  2564. u32 changed)
  2565. {
  2566. struct wl1271 *wl = hw->priv;
  2567. bool is_ap = (wl->bss_type == BSS_TYPE_AP_BSS);
  2568. int ret;
  2569. wl1271_debug(DEBUG_MAC80211, "mac80211 bss info changed 0x%x",
  2570. (int)changed);
  2571. mutex_lock(&wl->mutex);
  2572. if (unlikely(wl->state == WL1271_STATE_OFF))
  2573. goto out;
  2574. ret = wl1271_ps_elp_wakeup(wl);
  2575. if (ret < 0)
  2576. goto out;
  2577. if (is_ap)
  2578. wl1271_bss_info_changed_ap(wl, vif, bss_conf, changed);
  2579. else
  2580. wl1271_bss_info_changed_sta(wl, vif, bss_conf, changed);
  2581. wl1271_ps_elp_sleep(wl);
  2582. out:
  2583. mutex_unlock(&wl->mutex);
  2584. }
  2585. static int wl1271_op_conf_tx(struct ieee80211_hw *hw, u16 queue,
  2586. const struct ieee80211_tx_queue_params *params)
  2587. {
  2588. struct wl1271 *wl = hw->priv;
  2589. u8 ps_scheme;
  2590. int ret = 0;
  2591. mutex_lock(&wl->mutex);
  2592. wl1271_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
  2593. if (params->uapsd)
  2594. ps_scheme = CONF_PS_SCHEME_UPSD_TRIGGER;
  2595. else
  2596. ps_scheme = CONF_PS_SCHEME_LEGACY;
  2597. if (wl->state == WL1271_STATE_OFF) {
  2598. /*
  2599. * If the state is off, the parameters will be recorded and
  2600. * configured on init. This happens in AP-mode.
  2601. */
  2602. struct conf_tx_ac_category *conf_ac =
  2603. &wl->conf.tx.ac_conf[wl1271_tx_get_queue(queue)];
  2604. struct conf_tx_tid *conf_tid =
  2605. &wl->conf.tx.tid_conf[wl1271_tx_get_queue(queue)];
  2606. conf_ac->ac = wl1271_tx_get_queue(queue);
  2607. conf_ac->cw_min = (u8)params->cw_min;
  2608. conf_ac->cw_max = params->cw_max;
  2609. conf_ac->aifsn = params->aifs;
  2610. conf_ac->tx_op_limit = params->txop << 5;
  2611. conf_tid->queue_id = wl1271_tx_get_queue(queue);
  2612. conf_tid->channel_type = CONF_CHANNEL_TYPE_EDCF;
  2613. conf_tid->tsid = wl1271_tx_get_queue(queue);
  2614. conf_tid->ps_scheme = ps_scheme;
  2615. conf_tid->ack_policy = CONF_ACK_POLICY_LEGACY;
  2616. conf_tid->apsd_conf[0] = 0;
  2617. conf_tid->apsd_conf[1] = 0;
  2618. goto out;
  2619. }
  2620. ret = wl1271_ps_elp_wakeup(wl);
  2621. if (ret < 0)
  2622. goto out;
  2623. /*
  2624. * the txop is confed in units of 32us by the mac80211,
  2625. * we need us
  2626. */
  2627. ret = wl1271_acx_ac_cfg(wl, wl1271_tx_get_queue(queue),
  2628. params->cw_min, params->cw_max,
  2629. params->aifs, params->txop << 5);
  2630. if (ret < 0)
  2631. goto out_sleep;
  2632. ret = wl1271_acx_tid_cfg(wl, wl1271_tx_get_queue(queue),
  2633. CONF_CHANNEL_TYPE_EDCF,
  2634. wl1271_tx_get_queue(queue),
  2635. ps_scheme, CONF_ACK_POLICY_LEGACY,
  2636. 0, 0);
  2637. out_sleep:
  2638. wl1271_ps_elp_sleep(wl);
  2639. out:
  2640. mutex_unlock(&wl->mutex);
  2641. return ret;
  2642. }
  2643. static u64 wl1271_op_get_tsf(struct ieee80211_hw *hw)
  2644. {
  2645. struct wl1271 *wl = hw->priv;
  2646. u64 mactime = ULLONG_MAX;
  2647. int ret;
  2648. wl1271_debug(DEBUG_MAC80211, "mac80211 get tsf");
  2649. mutex_lock(&wl->mutex);
  2650. if (unlikely(wl->state == WL1271_STATE_OFF))
  2651. goto out;
  2652. ret = wl1271_ps_elp_wakeup(wl);
  2653. if (ret < 0)
  2654. goto out;
  2655. ret = wl1271_acx_tsf_info(wl, &mactime);
  2656. if (ret < 0)
  2657. goto out_sleep;
  2658. out_sleep:
  2659. wl1271_ps_elp_sleep(wl);
  2660. out:
  2661. mutex_unlock(&wl->mutex);
  2662. return mactime;
  2663. }
  2664. static int wl1271_op_get_survey(struct ieee80211_hw *hw, int idx,
  2665. struct survey_info *survey)
  2666. {
  2667. struct wl1271 *wl = hw->priv;
  2668. struct ieee80211_conf *conf = &hw->conf;
  2669. if (idx != 0)
  2670. return -ENOENT;
  2671. survey->channel = conf->channel;
  2672. survey->filled = SURVEY_INFO_NOISE_DBM;
  2673. survey->noise = wl->noise;
  2674. return 0;
  2675. }
  2676. static int wl1271_allocate_sta(struct wl1271 *wl,
  2677. struct ieee80211_sta *sta,
  2678. u8 *hlid)
  2679. {
  2680. struct wl1271_station *wl_sta;
  2681. int id;
  2682. id = find_first_zero_bit(wl->ap_hlid_map, AP_MAX_STATIONS);
  2683. if (id >= AP_MAX_STATIONS) {
  2684. wl1271_warning("could not allocate HLID - too much stations");
  2685. return -EBUSY;
  2686. }
  2687. wl_sta = (struct wl1271_station *)sta->drv_priv;
  2688. __set_bit(id, wl->ap_hlid_map);
  2689. wl_sta->hlid = WL1271_AP_STA_HLID_START + id;
  2690. *hlid = wl_sta->hlid;
  2691. memcpy(wl->links[wl_sta->hlid].addr, sta->addr, ETH_ALEN);
  2692. return 0;
  2693. }
  2694. static void wl1271_free_sta(struct wl1271 *wl, u8 hlid)
  2695. {
  2696. int id = hlid - WL1271_AP_STA_HLID_START;
  2697. if (WARN_ON(!test_bit(id, wl->ap_hlid_map)))
  2698. return;
  2699. __clear_bit(id, wl->ap_hlid_map);
  2700. memset(wl->links[hlid].addr, 0, ETH_ALEN);
  2701. wl1271_tx_reset_link_queues(wl, hlid);
  2702. __clear_bit(hlid, &wl->ap_ps_map);
  2703. __clear_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
  2704. }
  2705. static int wl1271_op_sta_add(struct ieee80211_hw *hw,
  2706. struct ieee80211_vif *vif,
  2707. struct ieee80211_sta *sta)
  2708. {
  2709. struct wl1271 *wl = hw->priv;
  2710. int ret = 0;
  2711. u8 hlid;
  2712. mutex_lock(&wl->mutex);
  2713. if (unlikely(wl->state == WL1271_STATE_OFF))
  2714. goto out;
  2715. if (wl->bss_type != BSS_TYPE_AP_BSS)
  2716. goto out;
  2717. wl1271_debug(DEBUG_MAC80211, "mac80211 add sta %d", (int)sta->aid);
  2718. ret = wl1271_allocate_sta(wl, sta, &hlid);
  2719. if (ret < 0)
  2720. goto out;
  2721. ret = wl1271_ps_elp_wakeup(wl);
  2722. if (ret < 0)
  2723. goto out_free_sta;
  2724. ret = wl1271_cmd_add_sta(wl, sta, hlid);
  2725. if (ret < 0)
  2726. goto out_sleep;
  2727. out_sleep:
  2728. wl1271_ps_elp_sleep(wl);
  2729. out_free_sta:
  2730. if (ret < 0)
  2731. wl1271_free_sta(wl, hlid);
  2732. out:
  2733. mutex_unlock(&wl->mutex);
  2734. return ret;
  2735. }
  2736. static int wl1271_op_sta_remove(struct ieee80211_hw *hw,
  2737. struct ieee80211_vif *vif,
  2738. struct ieee80211_sta *sta)
  2739. {
  2740. struct wl1271 *wl = hw->priv;
  2741. struct wl1271_station *wl_sta;
  2742. int ret = 0, id;
  2743. mutex_lock(&wl->mutex);
  2744. if (unlikely(wl->state == WL1271_STATE_OFF))
  2745. goto out;
  2746. if (wl->bss_type != BSS_TYPE_AP_BSS)
  2747. goto out;
  2748. wl1271_debug(DEBUG_MAC80211, "mac80211 remove sta %d", (int)sta->aid);
  2749. wl_sta = (struct wl1271_station *)sta->drv_priv;
  2750. id = wl_sta->hlid - WL1271_AP_STA_HLID_START;
  2751. if (WARN_ON(!test_bit(id, wl->ap_hlid_map)))
  2752. goto out;
  2753. ret = wl1271_ps_elp_wakeup(wl);
  2754. if (ret < 0)
  2755. goto out;
  2756. ret = wl1271_cmd_remove_sta(wl, wl_sta->hlid);
  2757. if (ret < 0)
  2758. goto out_sleep;
  2759. wl1271_free_sta(wl, wl_sta->hlid);
  2760. out_sleep:
  2761. wl1271_ps_elp_sleep(wl);
  2762. out:
  2763. mutex_unlock(&wl->mutex);
  2764. return ret;
  2765. }
  2766. static int wl1271_op_ampdu_action(struct ieee80211_hw *hw,
  2767. struct ieee80211_vif *vif,
  2768. enum ieee80211_ampdu_mlme_action action,
  2769. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  2770. u8 buf_size)
  2771. {
  2772. struct wl1271 *wl = hw->priv;
  2773. int ret;
  2774. mutex_lock(&wl->mutex);
  2775. if (unlikely(wl->state == WL1271_STATE_OFF)) {
  2776. ret = -EAGAIN;
  2777. goto out;
  2778. }
  2779. ret = wl1271_ps_elp_wakeup(wl);
  2780. if (ret < 0)
  2781. goto out;
  2782. switch (action) {
  2783. case IEEE80211_AMPDU_RX_START:
  2784. if (wl->ba_support) {
  2785. ret = wl1271_acx_set_ba_receiver_session(wl, tid, *ssn,
  2786. true);
  2787. if (!ret)
  2788. wl->ba_rx_bitmap |= BIT(tid);
  2789. } else {
  2790. ret = -ENOTSUPP;
  2791. }
  2792. break;
  2793. case IEEE80211_AMPDU_RX_STOP:
  2794. ret = wl1271_acx_set_ba_receiver_session(wl, tid, 0, false);
  2795. if (!ret)
  2796. wl->ba_rx_bitmap &= ~BIT(tid);
  2797. break;
  2798. /*
  2799. * The BA initiator session management in FW independently.
  2800. * Falling break here on purpose for all TX APDU commands.
  2801. */
  2802. case IEEE80211_AMPDU_TX_START:
  2803. case IEEE80211_AMPDU_TX_STOP:
  2804. case IEEE80211_AMPDU_TX_OPERATIONAL:
  2805. ret = -EINVAL;
  2806. break;
  2807. default:
  2808. wl1271_error("Incorrect ampdu action id=%x\n", action);
  2809. ret = -EINVAL;
  2810. }
  2811. wl1271_ps_elp_sleep(wl);
  2812. out:
  2813. mutex_unlock(&wl->mutex);
  2814. return ret;
  2815. }
  2816. static bool wl1271_tx_frames_pending(struct ieee80211_hw *hw)
  2817. {
  2818. struct wl1271 *wl = hw->priv;
  2819. bool ret = false;
  2820. mutex_lock(&wl->mutex);
  2821. if (unlikely(wl->state == WL1271_STATE_OFF))
  2822. goto out;
  2823. /* packets are considered pending if in the TX queue or the FW */
  2824. ret = (wl->tx_queue_count > 0) || (wl->tx_frames_cnt > 0);
  2825. /* the above is appropriate for STA mode for PS purposes */
  2826. WARN_ON(wl->bss_type != BSS_TYPE_STA_BSS);
  2827. out:
  2828. mutex_unlock(&wl->mutex);
  2829. return ret;
  2830. }
  2831. /* can't be const, mac80211 writes to this */
  2832. static struct ieee80211_rate wl1271_rates[] = {
  2833. { .bitrate = 10,
  2834. .hw_value = CONF_HW_BIT_RATE_1MBPS,
  2835. .hw_value_short = CONF_HW_BIT_RATE_1MBPS, },
  2836. { .bitrate = 20,
  2837. .hw_value = CONF_HW_BIT_RATE_2MBPS,
  2838. .hw_value_short = CONF_HW_BIT_RATE_2MBPS,
  2839. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  2840. { .bitrate = 55,
  2841. .hw_value = CONF_HW_BIT_RATE_5_5MBPS,
  2842. .hw_value_short = CONF_HW_BIT_RATE_5_5MBPS,
  2843. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  2844. { .bitrate = 110,
  2845. .hw_value = CONF_HW_BIT_RATE_11MBPS,
  2846. .hw_value_short = CONF_HW_BIT_RATE_11MBPS,
  2847. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  2848. { .bitrate = 60,
  2849. .hw_value = CONF_HW_BIT_RATE_6MBPS,
  2850. .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
  2851. { .bitrate = 90,
  2852. .hw_value = CONF_HW_BIT_RATE_9MBPS,
  2853. .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
  2854. { .bitrate = 120,
  2855. .hw_value = CONF_HW_BIT_RATE_12MBPS,
  2856. .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
  2857. { .bitrate = 180,
  2858. .hw_value = CONF_HW_BIT_RATE_18MBPS,
  2859. .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
  2860. { .bitrate = 240,
  2861. .hw_value = CONF_HW_BIT_RATE_24MBPS,
  2862. .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
  2863. { .bitrate = 360,
  2864. .hw_value = CONF_HW_BIT_RATE_36MBPS,
  2865. .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
  2866. { .bitrate = 480,
  2867. .hw_value = CONF_HW_BIT_RATE_48MBPS,
  2868. .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
  2869. { .bitrate = 540,
  2870. .hw_value = CONF_HW_BIT_RATE_54MBPS,
  2871. .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
  2872. };
  2873. /* can't be const, mac80211 writes to this */
  2874. static struct ieee80211_channel wl1271_channels[] = {
  2875. { .hw_value = 1, .center_freq = 2412, .max_power = 25 },
  2876. { .hw_value = 2, .center_freq = 2417, .max_power = 25 },
  2877. { .hw_value = 3, .center_freq = 2422, .max_power = 25 },
  2878. { .hw_value = 4, .center_freq = 2427, .max_power = 25 },
  2879. { .hw_value = 5, .center_freq = 2432, .max_power = 25 },
  2880. { .hw_value = 6, .center_freq = 2437, .max_power = 25 },
  2881. { .hw_value = 7, .center_freq = 2442, .max_power = 25 },
  2882. { .hw_value = 8, .center_freq = 2447, .max_power = 25 },
  2883. { .hw_value = 9, .center_freq = 2452, .max_power = 25 },
  2884. { .hw_value = 10, .center_freq = 2457, .max_power = 25 },
  2885. { .hw_value = 11, .center_freq = 2462, .max_power = 25 },
  2886. { .hw_value = 12, .center_freq = 2467, .max_power = 25 },
  2887. { .hw_value = 13, .center_freq = 2472, .max_power = 25 },
  2888. { .hw_value = 14, .center_freq = 2484, .max_power = 25 },
  2889. };
  2890. /* mapping to indexes for wl1271_rates */
  2891. static const u8 wl1271_rate_to_idx_2ghz[] = {
  2892. /* MCS rates are used only with 11n */
  2893. 7, /* CONF_HW_RXTX_RATE_MCS7 */
  2894. 6, /* CONF_HW_RXTX_RATE_MCS6 */
  2895. 5, /* CONF_HW_RXTX_RATE_MCS5 */
  2896. 4, /* CONF_HW_RXTX_RATE_MCS4 */
  2897. 3, /* CONF_HW_RXTX_RATE_MCS3 */
  2898. 2, /* CONF_HW_RXTX_RATE_MCS2 */
  2899. 1, /* CONF_HW_RXTX_RATE_MCS1 */
  2900. 0, /* CONF_HW_RXTX_RATE_MCS0 */
  2901. 11, /* CONF_HW_RXTX_RATE_54 */
  2902. 10, /* CONF_HW_RXTX_RATE_48 */
  2903. 9, /* CONF_HW_RXTX_RATE_36 */
  2904. 8, /* CONF_HW_RXTX_RATE_24 */
  2905. /* TI-specific rate */
  2906. CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_22 */
  2907. 7, /* CONF_HW_RXTX_RATE_18 */
  2908. 6, /* CONF_HW_RXTX_RATE_12 */
  2909. 3, /* CONF_HW_RXTX_RATE_11 */
  2910. 5, /* CONF_HW_RXTX_RATE_9 */
  2911. 4, /* CONF_HW_RXTX_RATE_6 */
  2912. 2, /* CONF_HW_RXTX_RATE_5_5 */
  2913. 1, /* CONF_HW_RXTX_RATE_2 */
  2914. 0 /* CONF_HW_RXTX_RATE_1 */
  2915. };
  2916. /* 11n STA capabilities */
  2917. #define HW_RX_HIGHEST_RATE 72
  2918. #ifdef CONFIG_WL12XX_HT
  2919. #define WL12XX_HT_CAP { \
  2920. .cap = IEEE80211_HT_CAP_GRN_FLD | IEEE80211_HT_CAP_SGI_20 | \
  2921. (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT), \
  2922. .ht_supported = true, \
  2923. .ampdu_factor = IEEE80211_HT_MAX_AMPDU_8K, \
  2924. .ampdu_density = IEEE80211_HT_MPDU_DENSITY_8, \
  2925. .mcs = { \
  2926. .rx_mask = { 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, \
  2927. .rx_highest = cpu_to_le16(HW_RX_HIGHEST_RATE), \
  2928. .tx_params = IEEE80211_HT_MCS_TX_DEFINED, \
  2929. }, \
  2930. }
  2931. #else
  2932. #define WL12XX_HT_CAP { \
  2933. .ht_supported = false, \
  2934. }
  2935. #endif
  2936. /* can't be const, mac80211 writes to this */
  2937. static struct ieee80211_supported_band wl1271_band_2ghz = {
  2938. .channels = wl1271_channels,
  2939. .n_channels = ARRAY_SIZE(wl1271_channels),
  2940. .bitrates = wl1271_rates,
  2941. .n_bitrates = ARRAY_SIZE(wl1271_rates),
  2942. .ht_cap = WL12XX_HT_CAP,
  2943. };
  2944. /* 5 GHz data rates for WL1273 */
  2945. static struct ieee80211_rate wl1271_rates_5ghz[] = {
  2946. { .bitrate = 60,
  2947. .hw_value = CONF_HW_BIT_RATE_6MBPS,
  2948. .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
  2949. { .bitrate = 90,
  2950. .hw_value = CONF_HW_BIT_RATE_9MBPS,
  2951. .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
  2952. { .bitrate = 120,
  2953. .hw_value = CONF_HW_BIT_RATE_12MBPS,
  2954. .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
  2955. { .bitrate = 180,
  2956. .hw_value = CONF_HW_BIT_RATE_18MBPS,
  2957. .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
  2958. { .bitrate = 240,
  2959. .hw_value = CONF_HW_BIT_RATE_24MBPS,
  2960. .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
  2961. { .bitrate = 360,
  2962. .hw_value = CONF_HW_BIT_RATE_36MBPS,
  2963. .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
  2964. { .bitrate = 480,
  2965. .hw_value = CONF_HW_BIT_RATE_48MBPS,
  2966. .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
  2967. { .bitrate = 540,
  2968. .hw_value = CONF_HW_BIT_RATE_54MBPS,
  2969. .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
  2970. };
  2971. /* 5 GHz band channels for WL1273 */
  2972. static struct ieee80211_channel wl1271_channels_5ghz[] = {
  2973. { .hw_value = 7, .center_freq = 5035},
  2974. { .hw_value = 8, .center_freq = 5040},
  2975. { .hw_value = 9, .center_freq = 5045},
  2976. { .hw_value = 11, .center_freq = 5055},
  2977. { .hw_value = 12, .center_freq = 5060},
  2978. { .hw_value = 16, .center_freq = 5080},
  2979. { .hw_value = 34, .center_freq = 5170},
  2980. { .hw_value = 36, .center_freq = 5180},
  2981. { .hw_value = 38, .center_freq = 5190},
  2982. { .hw_value = 40, .center_freq = 5200},
  2983. { .hw_value = 42, .center_freq = 5210},
  2984. { .hw_value = 44, .center_freq = 5220},
  2985. { .hw_value = 46, .center_freq = 5230},
  2986. { .hw_value = 48, .center_freq = 5240},
  2987. { .hw_value = 52, .center_freq = 5260},
  2988. { .hw_value = 56, .center_freq = 5280},
  2989. { .hw_value = 60, .center_freq = 5300},
  2990. { .hw_value = 64, .center_freq = 5320},
  2991. { .hw_value = 100, .center_freq = 5500},
  2992. { .hw_value = 104, .center_freq = 5520},
  2993. { .hw_value = 108, .center_freq = 5540},
  2994. { .hw_value = 112, .center_freq = 5560},
  2995. { .hw_value = 116, .center_freq = 5580},
  2996. { .hw_value = 120, .center_freq = 5600},
  2997. { .hw_value = 124, .center_freq = 5620},
  2998. { .hw_value = 128, .center_freq = 5640},
  2999. { .hw_value = 132, .center_freq = 5660},
  3000. { .hw_value = 136, .center_freq = 5680},
  3001. { .hw_value = 140, .center_freq = 5700},
  3002. { .hw_value = 149, .center_freq = 5745},
  3003. { .hw_value = 153, .center_freq = 5765},
  3004. { .hw_value = 157, .center_freq = 5785},
  3005. { .hw_value = 161, .center_freq = 5805},
  3006. { .hw_value = 165, .center_freq = 5825},
  3007. };
  3008. /* mapping to indexes for wl1271_rates_5ghz */
  3009. static const u8 wl1271_rate_to_idx_5ghz[] = {
  3010. /* MCS rates are used only with 11n */
  3011. 7, /* CONF_HW_RXTX_RATE_MCS7 */
  3012. 6, /* CONF_HW_RXTX_RATE_MCS6 */
  3013. 5, /* CONF_HW_RXTX_RATE_MCS5 */
  3014. 4, /* CONF_HW_RXTX_RATE_MCS4 */
  3015. 3, /* CONF_HW_RXTX_RATE_MCS3 */
  3016. 2, /* CONF_HW_RXTX_RATE_MCS2 */
  3017. 1, /* CONF_HW_RXTX_RATE_MCS1 */
  3018. 0, /* CONF_HW_RXTX_RATE_MCS0 */
  3019. 7, /* CONF_HW_RXTX_RATE_54 */
  3020. 6, /* CONF_HW_RXTX_RATE_48 */
  3021. 5, /* CONF_HW_RXTX_RATE_36 */
  3022. 4, /* CONF_HW_RXTX_RATE_24 */
  3023. /* TI-specific rate */
  3024. CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_22 */
  3025. 3, /* CONF_HW_RXTX_RATE_18 */
  3026. 2, /* CONF_HW_RXTX_RATE_12 */
  3027. CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_11 */
  3028. 1, /* CONF_HW_RXTX_RATE_9 */
  3029. 0, /* CONF_HW_RXTX_RATE_6 */
  3030. CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_5_5 */
  3031. CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_2 */
  3032. CONF_HW_RXTX_RATE_UNSUPPORTED /* CONF_HW_RXTX_RATE_1 */
  3033. };
  3034. static struct ieee80211_supported_band wl1271_band_5ghz = {
  3035. .channels = wl1271_channels_5ghz,
  3036. .n_channels = ARRAY_SIZE(wl1271_channels_5ghz),
  3037. .bitrates = wl1271_rates_5ghz,
  3038. .n_bitrates = ARRAY_SIZE(wl1271_rates_5ghz),
  3039. .ht_cap = WL12XX_HT_CAP,
  3040. };
  3041. static const u8 *wl1271_band_rate_to_idx[] = {
  3042. [IEEE80211_BAND_2GHZ] = wl1271_rate_to_idx_2ghz,
  3043. [IEEE80211_BAND_5GHZ] = wl1271_rate_to_idx_5ghz
  3044. };
  3045. static const struct ieee80211_ops wl1271_ops = {
  3046. .start = wl1271_op_start,
  3047. .stop = wl1271_op_stop,
  3048. .add_interface = wl1271_op_add_interface,
  3049. .remove_interface = wl1271_op_remove_interface,
  3050. #ifdef CONFIG_PM
  3051. .suspend = wl1271_op_suspend,
  3052. .resume = wl1271_op_resume,
  3053. #endif
  3054. .config = wl1271_op_config,
  3055. .prepare_multicast = wl1271_op_prepare_multicast,
  3056. .configure_filter = wl1271_op_configure_filter,
  3057. .tx = wl1271_op_tx,
  3058. .set_key = wl1271_op_set_key,
  3059. .hw_scan = wl1271_op_hw_scan,
  3060. .sched_scan_start = wl1271_op_sched_scan_start,
  3061. .sched_scan_stop = wl1271_op_sched_scan_stop,
  3062. .bss_info_changed = wl1271_op_bss_info_changed,
  3063. .set_frag_threshold = wl1271_op_set_frag_threshold,
  3064. .set_rts_threshold = wl1271_op_set_rts_threshold,
  3065. .conf_tx = wl1271_op_conf_tx,
  3066. .get_tsf = wl1271_op_get_tsf,
  3067. .get_survey = wl1271_op_get_survey,
  3068. .sta_add = wl1271_op_sta_add,
  3069. .sta_remove = wl1271_op_sta_remove,
  3070. .ampdu_action = wl1271_op_ampdu_action,
  3071. .tx_frames_pending = wl1271_tx_frames_pending,
  3072. CFG80211_TESTMODE_CMD(wl1271_tm_cmd)
  3073. };
  3074. u8 wl1271_rate_to_idx(int rate, enum ieee80211_band band)
  3075. {
  3076. u8 idx;
  3077. BUG_ON(band >= sizeof(wl1271_band_rate_to_idx)/sizeof(u8 *));
  3078. if (unlikely(rate >= CONF_HW_RXTX_RATE_MAX)) {
  3079. wl1271_error("Illegal RX rate from HW: %d", rate);
  3080. return 0;
  3081. }
  3082. idx = wl1271_band_rate_to_idx[band][rate];
  3083. if (unlikely(idx == CONF_HW_RXTX_RATE_UNSUPPORTED)) {
  3084. wl1271_error("Unsupported RX rate from HW: %d", rate);
  3085. return 0;
  3086. }
  3087. return idx;
  3088. }
  3089. static ssize_t wl1271_sysfs_show_bt_coex_state(struct device *dev,
  3090. struct device_attribute *attr,
  3091. char *buf)
  3092. {
  3093. struct wl1271 *wl = dev_get_drvdata(dev);
  3094. ssize_t len;
  3095. len = PAGE_SIZE;
  3096. mutex_lock(&wl->mutex);
  3097. len = snprintf(buf, len, "%d\n\n0 - off\n1 - on\n",
  3098. wl->sg_enabled);
  3099. mutex_unlock(&wl->mutex);
  3100. return len;
  3101. }
  3102. static ssize_t wl1271_sysfs_store_bt_coex_state(struct device *dev,
  3103. struct device_attribute *attr,
  3104. const char *buf, size_t count)
  3105. {
  3106. struct wl1271 *wl = dev_get_drvdata(dev);
  3107. unsigned long res;
  3108. int ret;
  3109. ret = kstrtoul(buf, 10, &res);
  3110. if (ret < 0) {
  3111. wl1271_warning("incorrect value written to bt_coex_mode");
  3112. return count;
  3113. }
  3114. mutex_lock(&wl->mutex);
  3115. res = !!res;
  3116. if (res == wl->sg_enabled)
  3117. goto out;
  3118. wl->sg_enabled = res;
  3119. if (wl->state == WL1271_STATE_OFF)
  3120. goto out;
  3121. ret = wl1271_ps_elp_wakeup(wl);
  3122. if (ret < 0)
  3123. goto out;
  3124. wl1271_acx_sg_enable(wl, wl->sg_enabled);
  3125. wl1271_ps_elp_sleep(wl);
  3126. out:
  3127. mutex_unlock(&wl->mutex);
  3128. return count;
  3129. }
  3130. static DEVICE_ATTR(bt_coex_state, S_IRUGO | S_IWUSR,
  3131. wl1271_sysfs_show_bt_coex_state,
  3132. wl1271_sysfs_store_bt_coex_state);
  3133. static ssize_t wl1271_sysfs_show_hw_pg_ver(struct device *dev,
  3134. struct device_attribute *attr,
  3135. char *buf)
  3136. {
  3137. struct wl1271 *wl = dev_get_drvdata(dev);
  3138. ssize_t len;
  3139. len = PAGE_SIZE;
  3140. mutex_lock(&wl->mutex);
  3141. if (wl->hw_pg_ver >= 0)
  3142. len = snprintf(buf, len, "%d\n", wl->hw_pg_ver);
  3143. else
  3144. len = snprintf(buf, len, "n/a\n");
  3145. mutex_unlock(&wl->mutex);
  3146. return len;
  3147. }
  3148. static DEVICE_ATTR(hw_pg_ver, S_IRUGO | S_IWUSR,
  3149. wl1271_sysfs_show_hw_pg_ver, NULL);
  3150. int wl1271_register_hw(struct wl1271 *wl)
  3151. {
  3152. int ret;
  3153. if (wl->mac80211_registered)
  3154. return 0;
  3155. ret = wl1271_fetch_nvs(wl);
  3156. if (ret == 0) {
  3157. /* NOTE: The wl->nvs->nvs element must be first, in
  3158. * order to simplify the casting, we assume it is at
  3159. * the beginning of the wl->nvs structure.
  3160. */
  3161. u8 *nvs_ptr = (u8 *)wl->nvs;
  3162. wl->mac_addr[0] = nvs_ptr[11];
  3163. wl->mac_addr[1] = nvs_ptr[10];
  3164. wl->mac_addr[2] = nvs_ptr[6];
  3165. wl->mac_addr[3] = nvs_ptr[5];
  3166. wl->mac_addr[4] = nvs_ptr[4];
  3167. wl->mac_addr[5] = nvs_ptr[3];
  3168. }
  3169. SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
  3170. ret = ieee80211_register_hw(wl->hw);
  3171. if (ret < 0) {
  3172. wl1271_error("unable to register mac80211 hw: %d", ret);
  3173. return ret;
  3174. }
  3175. wl->mac80211_registered = true;
  3176. wl1271_debugfs_init(wl);
  3177. register_netdevice_notifier(&wl1271_dev_notifier);
  3178. wl1271_notice("loaded");
  3179. return 0;
  3180. }
  3181. EXPORT_SYMBOL_GPL(wl1271_register_hw);
  3182. void wl1271_unregister_hw(struct wl1271 *wl)
  3183. {
  3184. if (wl->state == WL1271_STATE_PLT)
  3185. __wl1271_plt_stop(wl);
  3186. unregister_netdevice_notifier(&wl1271_dev_notifier);
  3187. ieee80211_unregister_hw(wl->hw);
  3188. wl->mac80211_registered = false;
  3189. }
  3190. EXPORT_SYMBOL_GPL(wl1271_unregister_hw);
  3191. int wl1271_init_ieee80211(struct wl1271 *wl)
  3192. {
  3193. static const u32 cipher_suites[] = {
  3194. WLAN_CIPHER_SUITE_WEP40,
  3195. WLAN_CIPHER_SUITE_WEP104,
  3196. WLAN_CIPHER_SUITE_TKIP,
  3197. WLAN_CIPHER_SUITE_CCMP,
  3198. WL1271_CIPHER_SUITE_GEM,
  3199. };
  3200. /* The tx descriptor buffer and the TKIP space. */
  3201. wl->hw->extra_tx_headroom = WL1271_TKIP_IV_SPACE +
  3202. sizeof(struct wl1271_tx_hw_descr);
  3203. /* unit us */
  3204. /* FIXME: find a proper value */
  3205. wl->hw->channel_change_time = 10000;
  3206. wl->hw->max_listen_interval = wl->conf.conn.max_listen_interval;
  3207. wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
  3208. IEEE80211_HW_BEACON_FILTER |
  3209. IEEE80211_HW_SUPPORTS_PS |
  3210. IEEE80211_HW_SUPPORTS_UAPSD |
  3211. IEEE80211_HW_HAS_RATE_CONTROL |
  3212. IEEE80211_HW_CONNECTION_MONITOR |
  3213. IEEE80211_HW_SUPPORTS_CQM_RSSI |
  3214. IEEE80211_HW_REPORTS_TX_ACK_STATUS |
  3215. IEEE80211_HW_SPECTRUM_MGMT |
  3216. IEEE80211_HW_AP_LINK_PS;
  3217. wl->hw->wiphy->cipher_suites = cipher_suites;
  3218. wl->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
  3219. wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  3220. BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_AP);
  3221. wl->hw->wiphy->max_scan_ssids = 1;
  3222. /*
  3223. * Maximum length of elements in scanning probe request templates
  3224. * should be the maximum length possible for a template, without
  3225. * the IEEE80211 header of the template
  3226. */
  3227. wl->hw->wiphy->max_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
  3228. sizeof(struct ieee80211_header);
  3229. /* make sure all our channels fit in the scanned_ch bitmask */
  3230. BUILD_BUG_ON(ARRAY_SIZE(wl1271_channels) +
  3231. ARRAY_SIZE(wl1271_channels_5ghz) >
  3232. WL1271_MAX_CHANNELS);
  3233. /*
  3234. * We keep local copies of the band structs because we need to
  3235. * modify them on a per-device basis.
  3236. */
  3237. memcpy(&wl->bands[IEEE80211_BAND_2GHZ], &wl1271_band_2ghz,
  3238. sizeof(wl1271_band_2ghz));
  3239. memcpy(&wl->bands[IEEE80211_BAND_5GHZ], &wl1271_band_5ghz,
  3240. sizeof(wl1271_band_5ghz));
  3241. wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  3242. &wl->bands[IEEE80211_BAND_2GHZ];
  3243. wl->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  3244. &wl->bands[IEEE80211_BAND_5GHZ];
  3245. wl->hw->queues = 4;
  3246. wl->hw->max_rates = 1;
  3247. wl->hw->wiphy->reg_notifier = wl1271_reg_notify;
  3248. SET_IEEE80211_DEV(wl->hw, wl1271_wl_to_dev(wl));
  3249. wl->hw->sta_data_size = sizeof(struct wl1271_station);
  3250. wl->hw->max_rx_aggregation_subframes = 8;
  3251. return 0;
  3252. }
  3253. EXPORT_SYMBOL_GPL(wl1271_init_ieee80211);
  3254. #define WL1271_DEFAULT_CHANNEL 0
  3255. struct ieee80211_hw *wl1271_alloc_hw(void)
  3256. {
  3257. struct ieee80211_hw *hw;
  3258. struct platform_device *plat_dev = NULL;
  3259. struct wl1271 *wl;
  3260. int i, j, ret;
  3261. unsigned int order;
  3262. hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
  3263. if (!hw) {
  3264. wl1271_error("could not alloc ieee80211_hw");
  3265. ret = -ENOMEM;
  3266. goto err_hw_alloc;
  3267. }
  3268. plat_dev = kmemdup(&wl1271_device, sizeof(wl1271_device), GFP_KERNEL);
  3269. if (!plat_dev) {
  3270. wl1271_error("could not allocate platform_device");
  3271. ret = -ENOMEM;
  3272. goto err_plat_alloc;
  3273. }
  3274. wl = hw->priv;
  3275. memset(wl, 0, sizeof(*wl));
  3276. INIT_LIST_HEAD(&wl->list);
  3277. wl->hw = hw;
  3278. wl->plat_dev = plat_dev;
  3279. for (i = 0; i < NUM_TX_QUEUES; i++)
  3280. skb_queue_head_init(&wl->tx_queue[i]);
  3281. for (i = 0; i < NUM_TX_QUEUES; i++)
  3282. for (j = 0; j < AP_MAX_LINKS; j++)
  3283. skb_queue_head_init(&wl->links[j].tx_queue[i]);
  3284. skb_queue_head_init(&wl->deferred_rx_queue);
  3285. skb_queue_head_init(&wl->deferred_tx_queue);
  3286. INIT_DELAYED_WORK(&wl->elp_work, wl1271_elp_work);
  3287. INIT_DELAYED_WORK(&wl->pspoll_work, wl1271_pspoll_work);
  3288. INIT_WORK(&wl->netstack_work, wl1271_netstack_work);
  3289. INIT_WORK(&wl->tx_work, wl1271_tx_work);
  3290. INIT_WORK(&wl->recovery_work, wl1271_recovery_work);
  3291. INIT_DELAYED_WORK(&wl->scan_complete_work, wl1271_scan_complete_work);
  3292. wl->channel = WL1271_DEFAULT_CHANNEL;
  3293. wl->beacon_int = WL1271_DEFAULT_BEACON_INT;
  3294. wl->default_key = 0;
  3295. wl->rx_counter = 0;
  3296. wl->rx_config = WL1271_DEFAULT_STA_RX_CONFIG;
  3297. wl->rx_filter = WL1271_DEFAULT_STA_RX_FILTER;
  3298. wl->psm_entry_retry = 0;
  3299. wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
  3300. wl->basic_rate_set = CONF_TX_RATE_MASK_BASIC;
  3301. wl->basic_rate = CONF_TX_RATE_MASK_BASIC;
  3302. wl->rate_set = CONF_TX_RATE_MASK_BASIC;
  3303. wl->band = IEEE80211_BAND_2GHZ;
  3304. wl->vif = NULL;
  3305. wl->flags = 0;
  3306. wl->sg_enabled = true;
  3307. wl->hw_pg_ver = -1;
  3308. wl->bss_type = MAX_BSS_TYPE;
  3309. wl->set_bss_type = MAX_BSS_TYPE;
  3310. wl->fw_bss_type = MAX_BSS_TYPE;
  3311. wl->last_tx_hlid = 0;
  3312. wl->ap_ps_map = 0;
  3313. wl->ap_fw_ps_map = 0;
  3314. wl->quirks = 0;
  3315. wl->platform_quirks = 0;
  3316. wl->sched_scanning = false;
  3317. memset(wl->tx_frames_map, 0, sizeof(wl->tx_frames_map));
  3318. for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
  3319. wl->tx_frames[i] = NULL;
  3320. spin_lock_init(&wl->wl_lock);
  3321. wl->state = WL1271_STATE_OFF;
  3322. mutex_init(&wl->mutex);
  3323. /* Apply default driver configuration. */
  3324. wl1271_conf_init(wl);
  3325. order = get_order(WL1271_AGGR_BUFFER_SIZE);
  3326. wl->aggr_buf = (u8 *)__get_free_pages(GFP_KERNEL, order);
  3327. if (!wl->aggr_buf) {
  3328. ret = -ENOMEM;
  3329. goto err_hw;
  3330. }
  3331. wl->dummy_packet = wl12xx_alloc_dummy_packet(wl);
  3332. if (!wl->dummy_packet) {
  3333. ret = -ENOMEM;
  3334. goto err_aggr;
  3335. }
  3336. /* Register platform device */
  3337. ret = platform_device_register(wl->plat_dev);
  3338. if (ret) {
  3339. wl1271_error("couldn't register platform device");
  3340. goto err_dummy_packet;
  3341. }
  3342. dev_set_drvdata(&wl->plat_dev->dev, wl);
  3343. /* Create sysfs file to control bt coex state */
  3344. ret = device_create_file(&wl->plat_dev->dev, &dev_attr_bt_coex_state);
  3345. if (ret < 0) {
  3346. wl1271_error("failed to create sysfs file bt_coex_state");
  3347. goto err_platform;
  3348. }
  3349. /* Create sysfs file to get HW PG version */
  3350. ret = device_create_file(&wl->plat_dev->dev, &dev_attr_hw_pg_ver);
  3351. if (ret < 0) {
  3352. wl1271_error("failed to create sysfs file hw_pg_ver");
  3353. goto err_bt_coex_state;
  3354. }
  3355. return hw;
  3356. err_bt_coex_state:
  3357. device_remove_file(&wl->plat_dev->dev, &dev_attr_bt_coex_state);
  3358. err_platform:
  3359. platform_device_unregister(wl->plat_dev);
  3360. err_dummy_packet:
  3361. dev_kfree_skb(wl->dummy_packet);
  3362. err_aggr:
  3363. free_pages((unsigned long)wl->aggr_buf, order);
  3364. err_hw:
  3365. wl1271_debugfs_exit(wl);
  3366. kfree(plat_dev);
  3367. err_plat_alloc:
  3368. ieee80211_free_hw(hw);
  3369. err_hw_alloc:
  3370. return ERR_PTR(ret);
  3371. }
  3372. EXPORT_SYMBOL_GPL(wl1271_alloc_hw);
  3373. int wl1271_free_hw(struct wl1271 *wl)
  3374. {
  3375. platform_device_unregister(wl->plat_dev);
  3376. dev_kfree_skb(wl->dummy_packet);
  3377. free_pages((unsigned long)wl->aggr_buf,
  3378. get_order(WL1271_AGGR_BUFFER_SIZE));
  3379. kfree(wl->plat_dev);
  3380. wl1271_debugfs_exit(wl);
  3381. vfree(wl->fw);
  3382. wl->fw = NULL;
  3383. kfree(wl->nvs);
  3384. wl->nvs = NULL;
  3385. kfree(wl->fw_status);
  3386. kfree(wl->tx_res_if);
  3387. ieee80211_free_hw(wl->hw);
  3388. return 0;
  3389. }
  3390. EXPORT_SYMBOL_GPL(wl1271_free_hw);
  3391. u32 wl12xx_debug_level = DEBUG_NONE;
  3392. EXPORT_SYMBOL_GPL(wl12xx_debug_level);
  3393. module_param_named(debug_level, wl12xx_debug_level, uint, S_IRUSR | S_IWUSR);
  3394. MODULE_PARM_DESC(debug_level, "wl12xx debugging level");
  3395. MODULE_LICENSE("GPL");
  3396. MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
  3397. MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");