cfg80211.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  1. /*
  2. * Copyright (c) 2004-2011 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "core.h"
  17. #include "cfg80211.h"
  18. #include "debug.h"
  19. #define RATETAB_ENT(_rate, _rateid, _flags) { \
  20. .bitrate = (_rate), \
  21. .flags = (_flags), \
  22. .hw_value = (_rateid), \
  23. }
  24. #define CHAN2G(_channel, _freq, _flags) { \
  25. .band = IEEE80211_BAND_2GHZ, \
  26. .hw_value = (_channel), \
  27. .center_freq = (_freq), \
  28. .flags = (_flags), \
  29. .max_antenna_gain = 0, \
  30. .max_power = 30, \
  31. }
  32. #define CHAN5G(_channel, _flags) { \
  33. .band = IEEE80211_BAND_5GHZ, \
  34. .hw_value = (_channel), \
  35. .center_freq = 5000 + (5 * (_channel)), \
  36. .flags = (_flags), \
  37. .max_antenna_gain = 0, \
  38. .max_power = 30, \
  39. }
  40. static struct ieee80211_rate ath6kl_rates[] = {
  41. RATETAB_ENT(10, 0x1, 0),
  42. RATETAB_ENT(20, 0x2, 0),
  43. RATETAB_ENT(55, 0x4, 0),
  44. RATETAB_ENT(110, 0x8, 0),
  45. RATETAB_ENT(60, 0x10, 0),
  46. RATETAB_ENT(90, 0x20, 0),
  47. RATETAB_ENT(120, 0x40, 0),
  48. RATETAB_ENT(180, 0x80, 0),
  49. RATETAB_ENT(240, 0x100, 0),
  50. RATETAB_ENT(360, 0x200, 0),
  51. RATETAB_ENT(480, 0x400, 0),
  52. RATETAB_ENT(540, 0x800, 0),
  53. };
  54. #define ath6kl_a_rates (ath6kl_rates + 4)
  55. #define ath6kl_a_rates_size 8
  56. #define ath6kl_g_rates (ath6kl_rates + 0)
  57. #define ath6kl_g_rates_size 12
  58. static struct ieee80211_channel ath6kl_2ghz_channels[] = {
  59. CHAN2G(1, 2412, 0),
  60. CHAN2G(2, 2417, 0),
  61. CHAN2G(3, 2422, 0),
  62. CHAN2G(4, 2427, 0),
  63. CHAN2G(5, 2432, 0),
  64. CHAN2G(6, 2437, 0),
  65. CHAN2G(7, 2442, 0),
  66. CHAN2G(8, 2447, 0),
  67. CHAN2G(9, 2452, 0),
  68. CHAN2G(10, 2457, 0),
  69. CHAN2G(11, 2462, 0),
  70. CHAN2G(12, 2467, 0),
  71. CHAN2G(13, 2472, 0),
  72. CHAN2G(14, 2484, 0),
  73. };
  74. static struct ieee80211_channel ath6kl_5ghz_a_channels[] = {
  75. CHAN5G(34, 0), CHAN5G(36, 0),
  76. CHAN5G(38, 0), CHAN5G(40, 0),
  77. CHAN5G(42, 0), CHAN5G(44, 0),
  78. CHAN5G(46, 0), CHAN5G(48, 0),
  79. CHAN5G(52, 0), CHAN5G(56, 0),
  80. CHAN5G(60, 0), CHAN5G(64, 0),
  81. CHAN5G(100, 0), CHAN5G(104, 0),
  82. CHAN5G(108, 0), CHAN5G(112, 0),
  83. CHAN5G(116, 0), CHAN5G(120, 0),
  84. CHAN5G(124, 0), CHAN5G(128, 0),
  85. CHAN5G(132, 0), CHAN5G(136, 0),
  86. CHAN5G(140, 0), CHAN5G(149, 0),
  87. CHAN5G(153, 0), CHAN5G(157, 0),
  88. CHAN5G(161, 0), CHAN5G(165, 0),
  89. CHAN5G(184, 0), CHAN5G(188, 0),
  90. CHAN5G(192, 0), CHAN5G(196, 0),
  91. CHAN5G(200, 0), CHAN5G(204, 0),
  92. CHAN5G(208, 0), CHAN5G(212, 0),
  93. CHAN5G(216, 0),
  94. };
  95. static struct ieee80211_supported_band ath6kl_band_2ghz = {
  96. .n_channels = ARRAY_SIZE(ath6kl_2ghz_channels),
  97. .channels = ath6kl_2ghz_channels,
  98. .n_bitrates = ath6kl_g_rates_size,
  99. .bitrates = ath6kl_g_rates,
  100. };
  101. static struct ieee80211_supported_band ath6kl_band_5ghz = {
  102. .n_channels = ARRAY_SIZE(ath6kl_5ghz_a_channels),
  103. .channels = ath6kl_5ghz_a_channels,
  104. .n_bitrates = ath6kl_a_rates_size,
  105. .bitrates = ath6kl_a_rates,
  106. };
  107. static int ath6kl_set_wpa_version(struct ath6kl *ar,
  108. enum nl80211_wpa_versions wpa_version)
  109. {
  110. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: %u\n", __func__, wpa_version);
  111. if (!wpa_version) {
  112. ar->auth_mode = NONE_AUTH;
  113. } else if (wpa_version & NL80211_WPA_VERSION_2) {
  114. ar->auth_mode = WPA2_AUTH;
  115. } else if (wpa_version & NL80211_WPA_VERSION_1) {
  116. ar->auth_mode = WPA_AUTH;
  117. } else {
  118. ath6kl_err("%s: %u not supported\n", __func__, wpa_version);
  119. return -ENOTSUPP;
  120. }
  121. return 0;
  122. }
  123. static int ath6kl_set_auth_type(struct ath6kl *ar,
  124. enum nl80211_auth_type auth_type)
  125. {
  126. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: 0x%x\n", __func__, auth_type);
  127. switch (auth_type) {
  128. case NL80211_AUTHTYPE_OPEN_SYSTEM:
  129. ar->dot11_auth_mode = OPEN_AUTH;
  130. break;
  131. case NL80211_AUTHTYPE_SHARED_KEY:
  132. ar->dot11_auth_mode = SHARED_AUTH;
  133. break;
  134. case NL80211_AUTHTYPE_NETWORK_EAP:
  135. ar->dot11_auth_mode = LEAP_AUTH;
  136. break;
  137. case NL80211_AUTHTYPE_AUTOMATIC:
  138. ar->dot11_auth_mode = OPEN_AUTH;
  139. ar->auto_auth_stage = AUTH_OPEN_IN_PROGRESS;
  140. break;
  141. default:
  142. ath6kl_err("%s: 0x%x not spported\n", __func__, auth_type);
  143. return -ENOTSUPP;
  144. }
  145. return 0;
  146. }
  147. static int ath6kl_set_cipher(struct ath6kl *ar, u32 cipher, bool ucast)
  148. {
  149. u8 *ar_cipher = ucast ? &ar->prwise_crypto : &ar->grp_crypto;
  150. u8 *ar_cipher_len = ucast ? &ar->prwise_crypto_len : &ar->grp_crpto_len;
  151. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: cipher 0x%x, ucast %u\n",
  152. __func__, cipher, ucast);
  153. switch (cipher) {
  154. case 0:
  155. /* our own hack to use value 0 as no crypto used */
  156. *ar_cipher = NONE_CRYPT;
  157. *ar_cipher_len = 0;
  158. break;
  159. case WLAN_CIPHER_SUITE_WEP40:
  160. *ar_cipher = WEP_CRYPT;
  161. *ar_cipher_len = 5;
  162. break;
  163. case WLAN_CIPHER_SUITE_WEP104:
  164. *ar_cipher = WEP_CRYPT;
  165. *ar_cipher_len = 13;
  166. break;
  167. case WLAN_CIPHER_SUITE_TKIP:
  168. *ar_cipher = TKIP_CRYPT;
  169. *ar_cipher_len = 0;
  170. break;
  171. case WLAN_CIPHER_SUITE_CCMP:
  172. *ar_cipher = AES_CRYPT;
  173. *ar_cipher_len = 0;
  174. break;
  175. default:
  176. ath6kl_err("cipher 0x%x not supported\n", cipher);
  177. return -ENOTSUPP;
  178. }
  179. return 0;
  180. }
  181. static void ath6kl_set_key_mgmt(struct ath6kl *ar, u32 key_mgmt)
  182. {
  183. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: 0x%x\n", __func__, key_mgmt);
  184. if (key_mgmt == WLAN_AKM_SUITE_PSK) {
  185. if (ar->auth_mode == WPA_AUTH)
  186. ar->auth_mode = WPA_PSK_AUTH;
  187. else if (ar->auth_mode == WPA2_AUTH)
  188. ar->auth_mode = WPA2_PSK_AUTH;
  189. } else if (key_mgmt != WLAN_AKM_SUITE_8021X) {
  190. ar->auth_mode = NONE_AUTH;
  191. }
  192. }
  193. static bool ath6kl_cfg80211_ready(struct ath6kl *ar)
  194. {
  195. if (!test_bit(WMI_READY, &ar->flag)) {
  196. ath6kl_err("wmi is not ready\n");
  197. return false;
  198. }
  199. if (!test_bit(WLAN_ENABLED, &ar->flag)) {
  200. ath6kl_err("wlan disabled\n");
  201. return false;
  202. }
  203. return true;
  204. }
  205. static int ath6kl_cfg80211_connect(struct wiphy *wiphy, struct net_device *dev,
  206. struct cfg80211_connect_params *sme)
  207. {
  208. struct ath6kl *ar = ath6kl_priv(dev);
  209. int status;
  210. ar->sme_state = SME_CONNECTING;
  211. if (!ath6kl_cfg80211_ready(ar))
  212. return -EIO;
  213. if (test_bit(DESTROY_IN_PROGRESS, &ar->flag)) {
  214. ath6kl_err("destroy in progress\n");
  215. return -EBUSY;
  216. }
  217. if (test_bit(SKIP_SCAN, &ar->flag) &&
  218. ((sme->channel && sme->channel->center_freq == 0) ||
  219. (sme->bssid && is_zero_ether_addr(sme->bssid)))) {
  220. ath6kl_err("SkipScan: channel or bssid invalid\n");
  221. return -EINVAL;
  222. }
  223. if (down_interruptible(&ar->sem)) {
  224. ath6kl_err("busy, couldn't get access\n");
  225. return -ERESTARTSYS;
  226. }
  227. if (test_bit(DESTROY_IN_PROGRESS, &ar->flag)) {
  228. ath6kl_err("busy, destroy in progress\n");
  229. up(&ar->sem);
  230. return -EBUSY;
  231. }
  232. if (ar->tx_pending[ath6kl_wmi_get_control_ep(ar->wmi)]) {
  233. /*
  234. * sleep until the command queue drains
  235. */
  236. wait_event_interruptible_timeout(ar->event_wq,
  237. ar->tx_pending[ath6kl_wmi_get_control_ep(ar->wmi)] == 0,
  238. WMI_TIMEOUT);
  239. if (signal_pending(current)) {
  240. ath6kl_err("cmd queue drain timeout\n");
  241. up(&ar->sem);
  242. return -EINTR;
  243. }
  244. }
  245. if (test_bit(CONNECTED, &ar->flag) &&
  246. ar->ssid_len == sme->ssid_len &&
  247. !memcmp(ar->ssid, sme->ssid, ar->ssid_len)) {
  248. ar->reconnect_flag = true;
  249. status = ath6kl_wmi_reconnect_cmd(ar->wmi, ar->req_bssid,
  250. ar->ch_hint);
  251. up(&ar->sem);
  252. if (status) {
  253. ath6kl_err("wmi_reconnect_cmd failed\n");
  254. return -EIO;
  255. }
  256. return 0;
  257. } else if (ar->ssid_len == sme->ssid_len &&
  258. !memcmp(ar->ssid, sme->ssid, ar->ssid_len)) {
  259. ath6kl_disconnect(ar);
  260. }
  261. memset(ar->ssid, 0, sizeof(ar->ssid));
  262. ar->ssid_len = sme->ssid_len;
  263. memcpy(ar->ssid, sme->ssid, sme->ssid_len);
  264. if (sme->channel)
  265. ar->ch_hint = sme->channel->center_freq;
  266. memset(ar->req_bssid, 0, sizeof(ar->req_bssid));
  267. if (sme->bssid && !is_broadcast_ether_addr(sme->bssid))
  268. memcpy(ar->req_bssid, sme->bssid, sizeof(ar->req_bssid));
  269. ath6kl_set_wpa_version(ar, sme->crypto.wpa_versions);
  270. status = ath6kl_set_auth_type(ar, sme->auth_type);
  271. if (status) {
  272. up(&ar->sem);
  273. return status;
  274. }
  275. if (sme->crypto.n_ciphers_pairwise)
  276. ath6kl_set_cipher(ar, sme->crypto.ciphers_pairwise[0], true);
  277. else
  278. ath6kl_set_cipher(ar, 0, true);
  279. ath6kl_set_cipher(ar, sme->crypto.cipher_group, false);
  280. if (sme->crypto.n_akm_suites)
  281. ath6kl_set_key_mgmt(ar, sme->crypto.akm_suites[0]);
  282. if ((sme->key_len) &&
  283. (ar->auth_mode == NONE_AUTH) && (ar->prwise_crypto == WEP_CRYPT)) {
  284. struct ath6kl_key *key = NULL;
  285. if (sme->key_idx < WMI_MIN_KEY_INDEX ||
  286. sme->key_idx > WMI_MAX_KEY_INDEX) {
  287. ath6kl_err("key index %d out of bounds\n",
  288. sme->key_idx);
  289. up(&ar->sem);
  290. return -ENOENT;
  291. }
  292. key = &ar->keys[sme->key_idx];
  293. key->key_len = sme->key_len;
  294. memcpy(key->key, sme->key, key->key_len);
  295. key->cipher = ar->prwise_crypto;
  296. ar->def_txkey_index = sme->key_idx;
  297. ath6kl_wmi_addkey_cmd(ar->wmi, sme->key_idx,
  298. ar->prwise_crypto,
  299. GROUP_USAGE | TX_USAGE,
  300. key->key_len,
  301. NULL,
  302. key->key, KEY_OP_INIT_VAL, NULL,
  303. NO_SYNC_WMIFLAG);
  304. }
  305. if (!ar->usr_bss_filter) {
  306. if (ath6kl_wmi_bssfilter_cmd(ar->wmi, ALL_BSS_FILTER, 0) != 0) {
  307. ath6kl_err("couldn't set bss filtering\n");
  308. up(&ar->sem);
  309. return -EIO;
  310. }
  311. }
  312. ar->nw_type = ar->next_mode;
  313. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  314. "%s: connect called with authmode %d dot11 auth %d"
  315. " PW crypto %d PW crypto len %d GRP crypto %d"
  316. " GRP crypto len %d channel hint %u\n",
  317. __func__,
  318. ar->auth_mode, ar->dot11_auth_mode, ar->prwise_crypto,
  319. ar->prwise_crypto_len, ar->grp_crypto,
  320. ar->grp_crpto_len, ar->ch_hint);
  321. ar->reconnect_flag = 0;
  322. status = ath6kl_wmi_connect_cmd(ar->wmi, ar->nw_type,
  323. ar->dot11_auth_mode, ar->auth_mode,
  324. ar->prwise_crypto,
  325. ar->prwise_crypto_len,
  326. ar->grp_crypto, ar->grp_crpto_len,
  327. ar->ssid_len, ar->ssid,
  328. ar->req_bssid, ar->ch_hint,
  329. ar->connect_ctrl_flags);
  330. up(&ar->sem);
  331. if (status == -EINVAL) {
  332. memset(ar->ssid, 0, sizeof(ar->ssid));
  333. ar->ssid_len = 0;
  334. ath6kl_err("invalid request\n");
  335. return -ENOENT;
  336. } else if (status) {
  337. ath6kl_err("ath6kl_wmi_connect_cmd failed\n");
  338. return -EIO;
  339. }
  340. if ((!(ar->connect_ctrl_flags & CONNECT_DO_WPA_OFFLOAD)) &&
  341. ((ar->auth_mode == WPA_PSK_AUTH)
  342. || (ar->auth_mode == WPA2_PSK_AUTH))) {
  343. mod_timer(&ar->disconnect_timer,
  344. jiffies + msecs_to_jiffies(DISCON_TIMER_INTVAL));
  345. }
  346. ar->connect_ctrl_flags &= ~CONNECT_DO_WPA_OFFLOAD;
  347. set_bit(CONNECT_PEND, &ar->flag);
  348. return 0;
  349. }
  350. void ath6kl_cfg80211_connect_event(struct ath6kl *ar, u16 channel,
  351. u8 *bssid, u16 listen_intvl,
  352. u16 beacon_intvl,
  353. enum network_type nw_type,
  354. u8 beacon_ie_len, u8 assoc_req_len,
  355. u8 assoc_resp_len, u8 *assoc_info)
  356. {
  357. u16 size = 0;
  358. u16 capability = 0;
  359. struct cfg80211_bss *bss = NULL;
  360. struct ieee80211_mgmt *mgmt = NULL;
  361. struct ieee80211_channel *ibss_ch = NULL;
  362. s32 signal = 50 * 100;
  363. u8 ie_buf_len = 0;
  364. unsigned char ie_buf[256];
  365. unsigned char *ptr_ie_buf = ie_buf;
  366. unsigned char *ieeemgmtbuf = NULL;
  367. u8 source_mac[ETH_ALEN];
  368. /* capinfo + listen interval */
  369. u8 assoc_req_ie_offset = sizeof(u16) + sizeof(u16);
  370. /* capinfo + status code + associd */
  371. u8 assoc_resp_ie_offset = sizeof(u16) + sizeof(u16) + sizeof(u16);
  372. u8 *assoc_req_ie = assoc_info + beacon_ie_len + assoc_req_ie_offset;
  373. u8 *assoc_resp_ie = assoc_info + beacon_ie_len + assoc_req_len +
  374. assoc_resp_ie_offset;
  375. assoc_req_len -= assoc_req_ie_offset;
  376. assoc_resp_len -= assoc_resp_ie_offset;
  377. ar->auto_auth_stage = AUTH_IDLE;
  378. if (nw_type & ADHOC_NETWORK) {
  379. if (ar->wdev->iftype != NL80211_IFTYPE_ADHOC) {
  380. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  381. "%s: ath6k not in ibss mode\n", __func__);
  382. return;
  383. }
  384. }
  385. if (nw_type & INFRA_NETWORK) {
  386. if (ar->wdev->iftype != NL80211_IFTYPE_STATION) {
  387. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  388. "%s: ath6k not in station mode\n", __func__);
  389. return;
  390. }
  391. }
  392. /*
  393. * Earlier we were updating the cfg about bss by making a beacon frame
  394. * only if the entry for bss is not there. This can have some issue if
  395. * ROAM event is generated and a heavy traffic is ongoing. The ROAM
  396. * event is handled through a work queue and by the time it really gets
  397. * handled, BSS would have been aged out. So it is better to update the
  398. * cfg about BSS irrespective of its entry being present right now or
  399. * not.
  400. */
  401. if (nw_type & ADHOC_NETWORK) {
  402. /* construct 802.11 mgmt beacon */
  403. if (ptr_ie_buf) {
  404. *ptr_ie_buf++ = WLAN_EID_SSID;
  405. *ptr_ie_buf++ = ar->ssid_len;
  406. memcpy(ptr_ie_buf, ar->ssid, ar->ssid_len);
  407. ptr_ie_buf += ar->ssid_len;
  408. *ptr_ie_buf++ = WLAN_EID_IBSS_PARAMS;
  409. *ptr_ie_buf++ = 2; /* length */
  410. *ptr_ie_buf++ = 0; /* ATIM window */
  411. *ptr_ie_buf++ = 0; /* ATIM window */
  412. /* TODO: update ibss params and include supported rates,
  413. * DS param set, extened support rates, wmm. */
  414. ie_buf_len = ptr_ie_buf - ie_buf;
  415. }
  416. capability |= WLAN_CAPABILITY_IBSS;
  417. if (ar->prwise_crypto == WEP_CRYPT)
  418. capability |= WLAN_CAPABILITY_PRIVACY;
  419. memcpy(source_mac, ar->net_dev->dev_addr, ETH_ALEN);
  420. ptr_ie_buf = ie_buf;
  421. } else {
  422. capability = *(u16 *) (&assoc_info[beacon_ie_len]);
  423. memcpy(source_mac, bssid, ETH_ALEN);
  424. ptr_ie_buf = assoc_req_ie;
  425. ie_buf_len = assoc_req_len;
  426. }
  427. size = offsetof(struct ieee80211_mgmt, u)
  428. + sizeof(mgmt->u.beacon)
  429. + ie_buf_len;
  430. ieeemgmtbuf = kzalloc(size, GFP_ATOMIC);
  431. if (!ieeemgmtbuf) {
  432. ath6kl_err("ieee mgmt buf alloc error\n");
  433. return;
  434. }
  435. mgmt = (struct ieee80211_mgmt *)ieeemgmtbuf;
  436. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  437. IEEE80211_STYPE_BEACON);
  438. memset(mgmt->da, 0xff, ETH_ALEN); /* broadcast addr */
  439. memcpy(mgmt->sa, source_mac, ETH_ALEN);
  440. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  441. mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_intvl);
  442. mgmt->u.beacon.capab_info = cpu_to_le16(capability);
  443. memcpy(mgmt->u.beacon.variable, ptr_ie_buf, ie_buf_len);
  444. ibss_ch = ieee80211_get_channel(ar->wdev->wiphy, (int)channel);
  445. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  446. "%s: inform bss with bssid %pM channel %d beacon_intvl %d capability 0x%x\n",
  447. __func__, mgmt->bssid, ibss_ch->hw_value,
  448. beacon_intvl, capability);
  449. bss = cfg80211_inform_bss_frame(ar->wdev->wiphy,
  450. ibss_ch, mgmt,
  451. size, signal, GFP_KERNEL);
  452. kfree(ieeemgmtbuf);
  453. cfg80211_put_bss(bss);
  454. if (nw_type & ADHOC_NETWORK) {
  455. cfg80211_ibss_joined(ar->net_dev, bssid, GFP_KERNEL);
  456. return;
  457. }
  458. if (ar->sme_state == SME_CONNECTING) {
  459. /* inform connect result to cfg80211 */
  460. ar->sme_state = SME_CONNECTED;
  461. cfg80211_connect_result(ar->net_dev, bssid,
  462. assoc_req_ie, assoc_req_len,
  463. assoc_resp_ie, assoc_resp_len,
  464. WLAN_STATUS_SUCCESS, GFP_KERNEL);
  465. } else if (ar->sme_state == SME_CONNECTED) {
  466. /* inform roam event to cfg80211 */
  467. cfg80211_roamed(ar->net_dev, ibss_ch, bssid,
  468. assoc_req_ie, assoc_req_len,
  469. assoc_resp_ie, assoc_resp_len, GFP_KERNEL);
  470. }
  471. }
  472. static int ath6kl_cfg80211_disconnect(struct wiphy *wiphy,
  473. struct net_device *dev, u16 reason_code)
  474. {
  475. struct ath6kl *ar = (struct ath6kl *)ath6kl_priv(dev);
  476. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: reason=%u\n", __func__,
  477. reason_code);
  478. if (!ath6kl_cfg80211_ready(ar))
  479. return -EIO;
  480. if (test_bit(DESTROY_IN_PROGRESS, &ar->flag)) {
  481. ath6kl_err("busy, destroy in progress\n");
  482. return -EBUSY;
  483. }
  484. if (down_interruptible(&ar->sem)) {
  485. ath6kl_err("busy, couldn't get access\n");
  486. return -ERESTARTSYS;
  487. }
  488. ar->reconnect_flag = 0;
  489. ath6kl_disconnect(ar);
  490. memset(ar->ssid, 0, sizeof(ar->ssid));
  491. ar->ssid_len = 0;
  492. if (!test_bit(SKIP_SCAN, &ar->flag))
  493. memset(ar->req_bssid, 0, sizeof(ar->req_bssid));
  494. up(&ar->sem);
  495. return 0;
  496. }
  497. void ath6kl_cfg80211_disconnect_event(struct ath6kl *ar, u8 reason,
  498. u8 *bssid, u8 assoc_resp_len,
  499. u8 *assoc_info, u16 proto_reason)
  500. {
  501. struct ath6kl_key *key = NULL;
  502. u16 status;
  503. if (ar->scan_req) {
  504. cfg80211_scan_done(ar->scan_req, true);
  505. ar->scan_req = NULL;
  506. }
  507. if (ar->nw_type & ADHOC_NETWORK) {
  508. if (ar->wdev->iftype != NL80211_IFTYPE_ADHOC) {
  509. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  510. "%s: ath6k not in ibss mode\n", __func__);
  511. return;
  512. }
  513. memset(bssid, 0, ETH_ALEN);
  514. cfg80211_ibss_joined(ar->net_dev, bssid, GFP_KERNEL);
  515. return;
  516. }
  517. if (ar->nw_type & INFRA_NETWORK) {
  518. if (ar->wdev->iftype != NL80211_IFTYPE_STATION) {
  519. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  520. "%s: ath6k not in station mode\n", __func__);
  521. return;
  522. }
  523. }
  524. if (!test_bit(CONNECT_PEND, &ar->flag)) {
  525. if (reason != DISCONNECT_CMD)
  526. ath6kl_wmi_disconnect_cmd(ar->wmi);
  527. return;
  528. }
  529. if (reason == NO_NETWORK_AVAIL) {
  530. /* connect cmd failed */
  531. ath6kl_wmi_disconnect_cmd(ar->wmi);
  532. return;
  533. }
  534. if (reason != DISCONNECT_CMD)
  535. return;
  536. if (!ar->auto_auth_stage) {
  537. clear_bit(CONNECT_PEND, &ar->flag);
  538. if (ar->sme_state == SME_CONNECTING) {
  539. cfg80211_connect_result(ar->net_dev,
  540. bssid, NULL, 0,
  541. NULL, 0,
  542. WLAN_STATUS_UNSPECIFIED_FAILURE,
  543. GFP_KERNEL);
  544. } else {
  545. cfg80211_disconnected(ar->net_dev, reason,
  546. NULL, 0, GFP_KERNEL);
  547. }
  548. ar->sme_state = SME_DISCONNECTED;
  549. return;
  550. }
  551. if (ar->dot11_auth_mode != OPEN_AUTH)
  552. return;
  553. /*
  554. * If the current auth algorithm is open, try shared and
  555. * make autoAuthStage idle. We do not make it leap for now
  556. * being.
  557. */
  558. key = &ar->keys[ar->def_txkey_index];
  559. if (down_interruptible(&ar->sem)) {
  560. ath6kl_err("busy, couldn't get access\n");
  561. return;
  562. }
  563. ar->dot11_auth_mode = SHARED_AUTH;
  564. ar->auto_auth_stage = AUTH_IDLE;
  565. ath6kl_wmi_addkey_cmd(ar->wmi,
  566. ar->def_txkey_index,
  567. ar->prwise_crypto,
  568. GROUP_USAGE | TX_USAGE,
  569. key->key_len, NULL,
  570. key->key,
  571. KEY_OP_INIT_VAL, NULL,
  572. NO_SYNC_WMIFLAG);
  573. status = ath6kl_wmi_connect_cmd(ar->wmi,
  574. ar->nw_type,
  575. ar->dot11_auth_mode,
  576. ar->auth_mode,
  577. ar->prwise_crypto,
  578. ar->prwise_crypto_len,
  579. ar->grp_crypto,
  580. ar->grp_crpto_len,
  581. ar->ssid_len,
  582. ar->ssid,
  583. ar->req_bssid,
  584. ar->ch_hint,
  585. ar->connect_ctrl_flags);
  586. up(&ar->sem);
  587. }
  588. static inline bool is_ch_11a(u16 ch)
  589. {
  590. return (!((ch >= 2412) && (ch <= 2484)));
  591. }
  592. /* struct ath6kl_node_table::nt_nodelock is locked when calling this */
  593. void ath6kl_cfg80211_scan_node(struct wiphy *wiphy, struct bss *ni)
  594. {
  595. struct ieee80211_mgmt *mgmt;
  596. struct ieee80211_channel *channel;
  597. struct ieee80211_supported_band *band;
  598. struct ath6kl_common_ie *cie;
  599. s32 signal;
  600. int freq;
  601. cie = &ni->ni_cie;
  602. if (is_ch_11a(cie->ie_chan))
  603. band = wiphy->bands[IEEE80211_BAND_5GHZ]; /* 11a */
  604. else if ((cie->ie_erp) || (cie->ie_xrates))
  605. band = wiphy->bands[IEEE80211_BAND_2GHZ]; /* 11g */
  606. else
  607. band = wiphy->bands[IEEE80211_BAND_2GHZ]; /* 11b */
  608. freq = cie->ie_chan;
  609. channel = ieee80211_get_channel(wiphy, freq);
  610. signal = ni->ni_snr * 100;
  611. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  612. "%s: bssid %pM ch %d freq %d size %d\n", __func__,
  613. ni->ni_macaddr, channel->hw_value, freq, ni->ni_framelen);
  614. /*
  615. * Both Beacon and Probe Response frames have same payload structure,
  616. * so it is fine to share the parser for both.
  617. */
  618. if (ni->ni_framelen < 8 + 2 + 2)
  619. return;
  620. mgmt = (struct ieee80211_mgmt *) (ni->ni_buf -
  621. offsetof(struct ieee80211_mgmt, u));
  622. cfg80211_inform_bss(wiphy, channel, ni->ni_macaddr,
  623. le64_to_cpu(mgmt->u.beacon.timestamp),
  624. le16_to_cpu(mgmt->u.beacon.capab_info),
  625. le16_to_cpu(mgmt->u.beacon.beacon_int),
  626. mgmt->u.beacon.variable,
  627. ni->ni_buf + ni->ni_framelen -
  628. mgmt->u.beacon.variable,
  629. signal, GFP_ATOMIC);
  630. }
  631. static int ath6kl_cfg80211_scan(struct wiphy *wiphy, struct net_device *ndev,
  632. struct cfg80211_scan_request *request)
  633. {
  634. struct ath6kl *ar = (struct ath6kl *)ath6kl_priv(ndev);
  635. int ret = 0;
  636. if (!ath6kl_cfg80211_ready(ar))
  637. return -EIO;
  638. if (!ar->usr_bss_filter) {
  639. if (ath6kl_wmi_bssfilter_cmd(ar->wmi,
  640. (test_bit(CONNECTED, &ar->flag) ?
  641. ALL_BUT_BSS_FILTER :
  642. ALL_BSS_FILTER), 0) != 0) {
  643. ath6kl_err("couldn't set bss filtering\n");
  644. return -EIO;
  645. }
  646. }
  647. if (request->n_ssids && request->ssids[0].ssid_len) {
  648. u8 i;
  649. if (request->n_ssids > (MAX_PROBED_SSID_INDEX - 1))
  650. request->n_ssids = MAX_PROBED_SSID_INDEX - 1;
  651. for (i = 0; i < request->n_ssids; i++)
  652. ath6kl_wmi_probedssid_cmd(ar->wmi, i + 1,
  653. SPECIFIC_SSID_FLAG,
  654. request->ssids[i].ssid_len,
  655. request->ssids[i].ssid);
  656. }
  657. if (ath6kl_wmi_startscan_cmd(ar->wmi, WMI_LONG_SCAN, 0,
  658. false, 0, 0, 0, NULL) != 0) {
  659. ath6kl_err("wmi_startscan_cmd failed\n");
  660. ret = -EIO;
  661. }
  662. ar->scan_req = request;
  663. return ret;
  664. }
  665. void ath6kl_cfg80211_scan_complete_event(struct ath6kl *ar, int status)
  666. {
  667. int i;
  668. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: status %d\n", __func__, status);
  669. if (!ar->scan_req)
  670. return;
  671. if ((status == -ECANCELED) || (status == -EBUSY)) {
  672. cfg80211_scan_done(ar->scan_req, true);
  673. goto out;
  674. }
  675. /* Translate data to cfg80211 mgmt format */
  676. wlan_iterate_nodes(&ar->scan_table, ar->wdev->wiphy);
  677. cfg80211_scan_done(ar->scan_req, false);
  678. if (ar->scan_req->n_ssids && ar->scan_req->ssids[0].ssid_len) {
  679. for (i = 0; i < ar->scan_req->n_ssids; i++) {
  680. ath6kl_wmi_probedssid_cmd(ar->wmi, i + 1,
  681. DISABLE_SSID_FLAG,
  682. 0, NULL);
  683. }
  684. }
  685. out:
  686. ar->scan_req = NULL;
  687. }
  688. static int ath6kl_cfg80211_add_key(struct wiphy *wiphy, struct net_device *ndev,
  689. u8 key_index, bool pairwise,
  690. const u8 *mac_addr,
  691. struct key_params *params)
  692. {
  693. struct ath6kl *ar = (struct ath6kl *)ath6kl_priv(ndev);
  694. struct ath6kl_key *key = NULL;
  695. u8 key_usage;
  696. u8 key_type;
  697. int status = 0;
  698. if (!ath6kl_cfg80211_ready(ar))
  699. return -EIO;
  700. if (key_index < WMI_MIN_KEY_INDEX || key_index > WMI_MAX_KEY_INDEX) {
  701. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  702. "%s: key index %d out of bounds\n", __func__,
  703. key_index);
  704. return -ENOENT;
  705. }
  706. key = &ar->keys[key_index];
  707. memset(key, 0, sizeof(struct ath6kl_key));
  708. if (pairwise)
  709. key_usage = PAIRWISE_USAGE;
  710. else
  711. key_usage = GROUP_USAGE;
  712. if (params) {
  713. if (params->key_len > WLAN_MAX_KEY_LEN ||
  714. params->seq_len > sizeof(key->seq))
  715. return -EINVAL;
  716. key->key_len = params->key_len;
  717. memcpy(key->key, params->key, key->key_len);
  718. key->seq_len = params->seq_len;
  719. memcpy(key->seq, params->seq, key->seq_len);
  720. key->cipher = params->cipher;
  721. }
  722. switch (key->cipher) {
  723. case WLAN_CIPHER_SUITE_WEP40:
  724. case WLAN_CIPHER_SUITE_WEP104:
  725. key_type = WEP_CRYPT;
  726. break;
  727. case WLAN_CIPHER_SUITE_TKIP:
  728. key_type = TKIP_CRYPT;
  729. break;
  730. case WLAN_CIPHER_SUITE_CCMP:
  731. key_type = AES_CRYPT;
  732. break;
  733. default:
  734. return -ENOTSUPP;
  735. }
  736. if (((ar->auth_mode == WPA_PSK_AUTH)
  737. || (ar->auth_mode == WPA2_PSK_AUTH))
  738. && (key_usage & GROUP_USAGE))
  739. del_timer(&ar->disconnect_timer);
  740. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  741. "%s: index %d, key_len %d, key_type 0x%x, key_usage 0x%x, seq_len %d\n",
  742. __func__, key_index, key->key_len, key_type,
  743. key_usage, key->seq_len);
  744. ar->def_txkey_index = key_index;
  745. status = ath6kl_wmi_addkey_cmd(ar->wmi, ar->def_txkey_index,
  746. key_type, key_usage, key->key_len,
  747. key->seq, key->key, KEY_OP_INIT_VAL,
  748. (u8 *) mac_addr, SYNC_BOTH_WMIFLAG);
  749. if (status)
  750. return -EIO;
  751. return 0;
  752. }
  753. static int ath6kl_cfg80211_del_key(struct wiphy *wiphy, struct net_device *ndev,
  754. u8 key_index, bool pairwise,
  755. const u8 *mac_addr)
  756. {
  757. struct ath6kl *ar = (struct ath6kl *)ath6kl_priv(ndev);
  758. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: index %d\n", __func__, key_index);
  759. if (!ath6kl_cfg80211_ready(ar))
  760. return -EIO;
  761. if (key_index < WMI_MIN_KEY_INDEX || key_index > WMI_MAX_KEY_INDEX) {
  762. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  763. "%s: key index %d out of bounds\n", __func__,
  764. key_index);
  765. return -ENOENT;
  766. }
  767. if (!ar->keys[key_index].key_len) {
  768. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  769. "%s: index %d is empty\n", __func__, key_index);
  770. return 0;
  771. }
  772. ar->keys[key_index].key_len = 0;
  773. return ath6kl_wmi_deletekey_cmd(ar->wmi, key_index);
  774. }
  775. static int ath6kl_cfg80211_get_key(struct wiphy *wiphy, struct net_device *ndev,
  776. u8 key_index, bool pairwise,
  777. const u8 *mac_addr, void *cookie,
  778. void (*callback) (void *cookie,
  779. struct key_params *))
  780. {
  781. struct ath6kl *ar = (struct ath6kl *)ath6kl_priv(ndev);
  782. struct ath6kl_key *key = NULL;
  783. struct key_params params;
  784. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: index %d\n", __func__, key_index);
  785. if (!ath6kl_cfg80211_ready(ar))
  786. return -EIO;
  787. if (key_index < WMI_MIN_KEY_INDEX || key_index > WMI_MAX_KEY_INDEX) {
  788. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  789. "%s: key index %d out of bounds\n", __func__,
  790. key_index);
  791. return -ENOENT;
  792. }
  793. key = &ar->keys[key_index];
  794. memset(&params, 0, sizeof(params));
  795. params.cipher = key->cipher;
  796. params.key_len = key->key_len;
  797. params.seq_len = key->seq_len;
  798. params.seq = key->seq;
  799. params.key = key->key;
  800. callback(cookie, &params);
  801. return key->key_len ? 0 : -ENOENT;
  802. }
  803. static int ath6kl_cfg80211_set_default_key(struct wiphy *wiphy,
  804. struct net_device *ndev,
  805. u8 key_index, bool unicast,
  806. bool multicast)
  807. {
  808. struct ath6kl *ar = (struct ath6kl *)ath6kl_priv(ndev);
  809. struct ath6kl_key *key = NULL;
  810. int status = 0;
  811. u8 key_usage;
  812. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: index %d\n", __func__, key_index);
  813. if (!ath6kl_cfg80211_ready(ar))
  814. return -EIO;
  815. if (key_index < WMI_MIN_KEY_INDEX || key_index > WMI_MAX_KEY_INDEX) {
  816. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  817. "%s: key index %d out of bounds\n",
  818. __func__, key_index);
  819. return -ENOENT;
  820. }
  821. if (!ar->keys[key_index].key_len) {
  822. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: invalid key index %d\n",
  823. __func__, key_index);
  824. return -EINVAL;
  825. }
  826. ar->def_txkey_index = key_index;
  827. key = &ar->keys[ar->def_txkey_index];
  828. key_usage = GROUP_USAGE;
  829. if (ar->prwise_crypto == WEP_CRYPT)
  830. key_usage |= TX_USAGE;
  831. status = ath6kl_wmi_addkey_cmd(ar->wmi, ar->def_txkey_index,
  832. ar->prwise_crypto, key_usage,
  833. key->key_len, key->seq, key->key,
  834. KEY_OP_INIT_VAL, NULL,
  835. SYNC_BOTH_WMIFLAG);
  836. if (status)
  837. return -EIO;
  838. return 0;
  839. }
  840. void ath6kl_cfg80211_tkip_micerr_event(struct ath6kl *ar, u8 keyid,
  841. bool ismcast)
  842. {
  843. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  844. "%s: keyid %d, ismcast %d\n", __func__, keyid, ismcast);
  845. cfg80211_michael_mic_failure(ar->net_dev, ar->bssid,
  846. (ismcast ? NL80211_KEYTYPE_GROUP :
  847. NL80211_KEYTYPE_PAIRWISE), keyid, NULL,
  848. GFP_KERNEL);
  849. }
  850. static int ath6kl_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
  851. {
  852. struct ath6kl *ar = (struct ath6kl *)wiphy_priv(wiphy);
  853. int ret;
  854. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: changed 0x%x\n", __func__,
  855. changed);
  856. if (!ath6kl_cfg80211_ready(ar))
  857. return -EIO;
  858. if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
  859. ret = ath6kl_wmi_set_rts_cmd(ar->wmi, wiphy->rts_threshold);
  860. if (ret != 0) {
  861. ath6kl_err("ath6kl_wmi_set_rts_cmd failed\n");
  862. return -EIO;
  863. }
  864. }
  865. return 0;
  866. }
  867. /*
  868. * The type nl80211_tx_power_setting replaces the following
  869. * data type from 2.6.36 onwards
  870. */
  871. static int ath6kl_cfg80211_set_txpower(struct wiphy *wiphy,
  872. enum nl80211_tx_power_setting type,
  873. int dbm)
  874. {
  875. struct ath6kl *ar = (struct ath6kl *)wiphy_priv(wiphy);
  876. u8 ath6kl_dbm;
  877. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: type 0x%x, dbm %d\n", __func__,
  878. type, dbm);
  879. if (!ath6kl_cfg80211_ready(ar))
  880. return -EIO;
  881. switch (type) {
  882. case NL80211_TX_POWER_AUTOMATIC:
  883. return 0;
  884. case NL80211_TX_POWER_LIMITED:
  885. ar->tx_pwr = ath6kl_dbm = dbm;
  886. break;
  887. default:
  888. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: type 0x%x not supported\n",
  889. __func__, type);
  890. return -EOPNOTSUPP;
  891. }
  892. ath6kl_wmi_set_tx_pwr_cmd(ar->wmi, ath6kl_dbm);
  893. return 0;
  894. }
  895. static int ath6kl_cfg80211_get_txpower(struct wiphy *wiphy, int *dbm)
  896. {
  897. struct ath6kl *ar = (struct ath6kl *)wiphy_priv(wiphy);
  898. if (!ath6kl_cfg80211_ready(ar))
  899. return -EIO;
  900. if (test_bit(CONNECTED, &ar->flag)) {
  901. ar->tx_pwr = 0;
  902. if (ath6kl_wmi_get_tx_pwr_cmd(ar->wmi) != 0) {
  903. ath6kl_err("ath6kl_wmi_get_tx_pwr_cmd failed\n");
  904. return -EIO;
  905. }
  906. wait_event_interruptible_timeout(ar->event_wq, ar->tx_pwr != 0,
  907. 5 * HZ);
  908. if (signal_pending(current)) {
  909. ath6kl_err("target did not respond\n");
  910. return -EINTR;
  911. }
  912. }
  913. *dbm = ar->tx_pwr;
  914. return 0;
  915. }
  916. static int ath6kl_cfg80211_set_power_mgmt(struct wiphy *wiphy,
  917. struct net_device *dev,
  918. bool pmgmt, int timeout)
  919. {
  920. struct ath6kl *ar = ath6kl_priv(dev);
  921. struct wmi_power_mode_cmd mode;
  922. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: pmgmt %d, timeout %d\n",
  923. __func__, pmgmt, timeout);
  924. if (!ath6kl_cfg80211_ready(ar))
  925. return -EIO;
  926. if (pmgmt) {
  927. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: max perf\n", __func__);
  928. mode.pwr_mode = REC_POWER;
  929. } else {
  930. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: rec power\n", __func__);
  931. mode.pwr_mode = MAX_PERF_POWER;
  932. }
  933. if (ath6kl_wmi_powermode_cmd(ar->wmi, mode.pwr_mode) != 0) {
  934. ath6kl_err("wmi_powermode_cmd failed\n");
  935. return -EIO;
  936. }
  937. return 0;
  938. }
  939. static int ath6kl_cfg80211_change_iface(struct wiphy *wiphy,
  940. struct net_device *ndev,
  941. enum nl80211_iftype type, u32 *flags,
  942. struct vif_params *params)
  943. {
  944. struct ath6kl *ar = ath6kl_priv(ndev);
  945. struct wireless_dev *wdev = ar->wdev;
  946. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: type %u\n", __func__, type);
  947. if (!ath6kl_cfg80211_ready(ar))
  948. return -EIO;
  949. switch (type) {
  950. case NL80211_IFTYPE_STATION:
  951. ar->next_mode = INFRA_NETWORK;
  952. break;
  953. case NL80211_IFTYPE_ADHOC:
  954. ar->next_mode = ADHOC_NETWORK;
  955. break;
  956. default:
  957. ath6kl_err("invalid interface type %u\n", type);
  958. return -EOPNOTSUPP;
  959. }
  960. wdev->iftype = type;
  961. return 0;
  962. }
  963. static int ath6kl_cfg80211_join_ibss(struct wiphy *wiphy,
  964. struct net_device *dev,
  965. struct cfg80211_ibss_params *ibss_param)
  966. {
  967. struct ath6kl *ar = ath6kl_priv(dev);
  968. int status;
  969. if (!ath6kl_cfg80211_ready(ar))
  970. return -EIO;
  971. ar->ssid_len = ibss_param->ssid_len;
  972. memcpy(ar->ssid, ibss_param->ssid, ar->ssid_len);
  973. if (ibss_param->channel)
  974. ar->ch_hint = ibss_param->channel->center_freq;
  975. if (ibss_param->channel_fixed) {
  976. /*
  977. * TODO: channel_fixed: The channel should be fixed, do not
  978. * search for IBSSs to join on other channels. Target
  979. * firmware does not support this feature, needs to be
  980. * updated.
  981. */
  982. return -EOPNOTSUPP;
  983. }
  984. memset(ar->req_bssid, 0, sizeof(ar->req_bssid));
  985. if (ibss_param->bssid && !is_broadcast_ether_addr(ibss_param->bssid))
  986. memcpy(ar->req_bssid, ibss_param->bssid, sizeof(ar->req_bssid));
  987. ath6kl_set_wpa_version(ar, 0);
  988. status = ath6kl_set_auth_type(ar, NL80211_AUTHTYPE_OPEN_SYSTEM);
  989. if (status)
  990. return status;
  991. if (ibss_param->privacy) {
  992. ath6kl_set_cipher(ar, WLAN_CIPHER_SUITE_WEP40, true);
  993. ath6kl_set_cipher(ar, WLAN_CIPHER_SUITE_WEP40, false);
  994. } else {
  995. ath6kl_set_cipher(ar, 0, true);
  996. ath6kl_set_cipher(ar, 0, false);
  997. }
  998. ar->nw_type = ar->next_mode;
  999. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  1000. "%s: connect called with authmode %d dot11 auth %d"
  1001. " PW crypto %d PW crypto len %d GRP crypto %d"
  1002. " GRP crypto len %d channel hint %u\n",
  1003. __func__,
  1004. ar->auth_mode, ar->dot11_auth_mode, ar->prwise_crypto,
  1005. ar->prwise_crypto_len, ar->grp_crypto,
  1006. ar->grp_crpto_len, ar->ch_hint);
  1007. status = ath6kl_wmi_connect_cmd(ar->wmi, ar->nw_type,
  1008. ar->dot11_auth_mode, ar->auth_mode,
  1009. ar->prwise_crypto,
  1010. ar->prwise_crypto_len,
  1011. ar->grp_crypto, ar->grp_crpto_len,
  1012. ar->ssid_len, ar->ssid,
  1013. ar->req_bssid, ar->ch_hint,
  1014. ar->connect_ctrl_flags);
  1015. set_bit(CONNECT_PEND, &ar->flag);
  1016. return 0;
  1017. }
  1018. static int ath6kl_cfg80211_leave_ibss(struct wiphy *wiphy,
  1019. struct net_device *dev)
  1020. {
  1021. struct ath6kl *ar = (struct ath6kl *)ath6kl_priv(dev);
  1022. if (!ath6kl_cfg80211_ready(ar))
  1023. return -EIO;
  1024. ath6kl_disconnect(ar);
  1025. memset(ar->ssid, 0, sizeof(ar->ssid));
  1026. ar->ssid_len = 0;
  1027. return 0;
  1028. }
  1029. static const u32 cipher_suites[] = {
  1030. WLAN_CIPHER_SUITE_WEP40,
  1031. WLAN_CIPHER_SUITE_WEP104,
  1032. WLAN_CIPHER_SUITE_TKIP,
  1033. WLAN_CIPHER_SUITE_CCMP,
  1034. };
  1035. static bool is_rate_legacy(s32 rate)
  1036. {
  1037. static const s32 legacy[] = { 1000, 2000, 5500, 11000,
  1038. 6000, 9000, 12000, 18000, 24000,
  1039. 36000, 48000, 54000
  1040. };
  1041. u8 i;
  1042. for (i = 0; i < ARRAY_SIZE(legacy); i++)
  1043. if (rate == legacy[i])
  1044. return true;
  1045. return false;
  1046. }
  1047. static bool is_rate_ht20(s32 rate, u8 *mcs, bool *sgi)
  1048. {
  1049. static const s32 ht20[] = { 6500, 13000, 19500, 26000, 39000,
  1050. 52000, 58500, 65000, 72200
  1051. };
  1052. u8 i;
  1053. for (i = 0; i < ARRAY_SIZE(ht20); i++) {
  1054. if (rate == ht20[i]) {
  1055. if (i == ARRAY_SIZE(ht20) - 1)
  1056. /* last rate uses sgi */
  1057. *sgi = true;
  1058. else
  1059. *sgi = false;
  1060. *mcs = i;
  1061. return true;
  1062. }
  1063. }
  1064. return false;
  1065. }
  1066. static bool is_rate_ht40(s32 rate, u8 *mcs, bool *sgi)
  1067. {
  1068. static const s32 ht40[] = { 13500, 27000, 40500, 54000,
  1069. 81000, 108000, 121500, 135000,
  1070. 150000
  1071. };
  1072. u8 i;
  1073. for (i = 0; i < ARRAY_SIZE(ht40); i++) {
  1074. if (rate == ht40[i]) {
  1075. if (i == ARRAY_SIZE(ht40) - 1)
  1076. /* last rate uses sgi */
  1077. *sgi = true;
  1078. else
  1079. *sgi = false;
  1080. *mcs = i;
  1081. return true;
  1082. }
  1083. }
  1084. return false;
  1085. }
  1086. static int ath6kl_get_station(struct wiphy *wiphy, struct net_device *dev,
  1087. u8 *mac, struct station_info *sinfo)
  1088. {
  1089. struct ath6kl *ar = ath6kl_priv(dev);
  1090. long left;
  1091. bool sgi;
  1092. s32 rate;
  1093. int ret;
  1094. u8 mcs;
  1095. if (memcmp(mac, ar->bssid, ETH_ALEN) != 0)
  1096. return -ENOENT;
  1097. if (down_interruptible(&ar->sem))
  1098. return -EBUSY;
  1099. set_bit(STATS_UPDATE_PEND, &ar->flag);
  1100. ret = ath6kl_wmi_get_stats_cmd(ar->wmi);
  1101. if (ret != 0) {
  1102. up(&ar->sem);
  1103. return -EIO;
  1104. }
  1105. left = wait_event_interruptible_timeout(ar->event_wq,
  1106. !test_bit(STATS_UPDATE_PEND,
  1107. &ar->flag),
  1108. WMI_TIMEOUT);
  1109. up(&ar->sem);
  1110. if (left == 0)
  1111. return -ETIMEDOUT;
  1112. else if (left < 0)
  1113. return left;
  1114. if (ar->target_stats.rx_byte) {
  1115. sinfo->rx_bytes = ar->target_stats.rx_byte;
  1116. sinfo->filled |= STATION_INFO_RX_BYTES;
  1117. sinfo->rx_packets = ar->target_stats.rx_pkt;
  1118. sinfo->filled |= STATION_INFO_RX_PACKETS;
  1119. }
  1120. if (ar->target_stats.tx_byte) {
  1121. sinfo->tx_bytes = ar->target_stats.tx_byte;
  1122. sinfo->filled |= STATION_INFO_TX_BYTES;
  1123. sinfo->tx_packets = ar->target_stats.tx_pkt;
  1124. sinfo->filled |= STATION_INFO_TX_PACKETS;
  1125. }
  1126. sinfo->signal = ar->target_stats.cs_rssi;
  1127. sinfo->filled |= STATION_INFO_SIGNAL;
  1128. rate = ar->target_stats.tx_ucast_rate;
  1129. if (is_rate_legacy(rate)) {
  1130. sinfo->txrate.legacy = rate / 100;
  1131. } else if (is_rate_ht20(rate, &mcs, &sgi)) {
  1132. if (sgi) {
  1133. sinfo->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
  1134. sinfo->txrate.mcs = mcs - 1;
  1135. } else {
  1136. sinfo->txrate.mcs = mcs;
  1137. }
  1138. sinfo->txrate.flags |= RATE_INFO_FLAGS_MCS;
  1139. } else if (is_rate_ht40(rate, &mcs, &sgi)) {
  1140. if (sgi) {
  1141. sinfo->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
  1142. sinfo->txrate.mcs = mcs - 1;
  1143. } else {
  1144. sinfo->txrate.mcs = mcs;
  1145. }
  1146. sinfo->txrate.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
  1147. sinfo->txrate.flags |= RATE_INFO_FLAGS_MCS;
  1148. } else {
  1149. ath6kl_warn("invalid rate: %d\n", rate);
  1150. return 0;
  1151. }
  1152. sinfo->filled |= STATION_INFO_TX_BITRATE;
  1153. return 0;
  1154. }
  1155. static int ath6kl_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
  1156. struct cfg80211_pmksa *pmksa)
  1157. {
  1158. struct ath6kl *ar = ath6kl_priv(netdev);
  1159. return ath6kl_wmi_setpmkid_cmd(ar->wmi, pmksa->bssid,
  1160. pmksa->pmkid, true);
  1161. }
  1162. static int ath6kl_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
  1163. struct cfg80211_pmksa *pmksa)
  1164. {
  1165. struct ath6kl *ar = ath6kl_priv(netdev);
  1166. return ath6kl_wmi_setpmkid_cmd(ar->wmi, pmksa->bssid,
  1167. pmksa->pmkid, false);
  1168. }
  1169. static int ath6kl_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev)
  1170. {
  1171. struct ath6kl *ar = ath6kl_priv(netdev);
  1172. if (test_bit(CONNECTED, &ar->flag))
  1173. return ath6kl_wmi_setpmkid_cmd(ar->wmi, ar->bssid, NULL, false);
  1174. return 0;
  1175. }
  1176. static struct cfg80211_ops ath6kl_cfg80211_ops = {
  1177. .change_virtual_intf = ath6kl_cfg80211_change_iface,
  1178. .scan = ath6kl_cfg80211_scan,
  1179. .connect = ath6kl_cfg80211_connect,
  1180. .disconnect = ath6kl_cfg80211_disconnect,
  1181. .add_key = ath6kl_cfg80211_add_key,
  1182. .get_key = ath6kl_cfg80211_get_key,
  1183. .del_key = ath6kl_cfg80211_del_key,
  1184. .set_default_key = ath6kl_cfg80211_set_default_key,
  1185. .set_wiphy_params = ath6kl_cfg80211_set_wiphy_params,
  1186. .set_tx_power = ath6kl_cfg80211_set_txpower,
  1187. .get_tx_power = ath6kl_cfg80211_get_txpower,
  1188. .set_power_mgmt = ath6kl_cfg80211_set_power_mgmt,
  1189. .join_ibss = ath6kl_cfg80211_join_ibss,
  1190. .leave_ibss = ath6kl_cfg80211_leave_ibss,
  1191. .get_station = ath6kl_get_station,
  1192. .set_pmksa = ath6kl_set_pmksa,
  1193. .del_pmksa = ath6kl_del_pmksa,
  1194. .flush_pmksa = ath6kl_flush_pmksa,
  1195. };
  1196. struct wireless_dev *ath6kl_cfg80211_init(struct device *dev)
  1197. {
  1198. int ret = 0;
  1199. struct wireless_dev *wdev;
  1200. wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
  1201. if (!wdev) {
  1202. ath6kl_err("couldn't allocate wireless device\n");
  1203. return NULL;
  1204. }
  1205. /* create a new wiphy for use with cfg80211 */
  1206. wdev->wiphy = wiphy_new(&ath6kl_cfg80211_ops, sizeof(struct ath6kl));
  1207. if (!wdev->wiphy) {
  1208. ath6kl_err("couldn't allocate wiphy device\n");
  1209. kfree(wdev);
  1210. return NULL;
  1211. }
  1212. /* set device pointer for wiphy */
  1213. set_wiphy_dev(wdev->wiphy, dev);
  1214. wdev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  1215. BIT(NL80211_IFTYPE_ADHOC);
  1216. /* max num of ssids that can be probed during scanning */
  1217. wdev->wiphy->max_scan_ssids = MAX_PROBED_SSID_INDEX;
  1218. wdev->wiphy->bands[IEEE80211_BAND_2GHZ] = &ath6kl_band_2ghz;
  1219. wdev->wiphy->bands[IEEE80211_BAND_5GHZ] = &ath6kl_band_5ghz;
  1220. wdev->wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
  1221. wdev->wiphy->cipher_suites = cipher_suites;
  1222. wdev->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
  1223. ret = wiphy_register(wdev->wiphy);
  1224. if (ret < 0) {
  1225. ath6kl_err("couldn't register wiphy device\n");
  1226. wiphy_free(wdev->wiphy);
  1227. kfree(wdev);
  1228. return NULL;
  1229. }
  1230. return wdev;
  1231. }
  1232. void ath6kl_cfg80211_deinit(struct ath6kl *ar)
  1233. {
  1234. struct wireless_dev *wdev = ar->wdev;
  1235. if (ar->scan_req) {
  1236. cfg80211_scan_done(ar->scan_req, true);
  1237. ar->scan_req = NULL;
  1238. }
  1239. if (!wdev)
  1240. return;
  1241. wiphy_unregister(wdev->wiphy);
  1242. wiphy_free(wdev->wiphy);
  1243. kfree(wdev);
  1244. }