cfg80211.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * Intel Wireless Multicomm 3200 WiFi driver
  3. *
  4. * Copyright (C) 2009 Intel Corporation <ilw@linux.intel.com>
  5. * Samuel Ortiz <samuel.ortiz@intel.com>
  6. * Zhu Yi <yi.zhu@intel.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 version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU 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 Street, Fifth Floor, Boston, MA
  20. * 02110-1301, USA.
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/wireless.h>
  26. #include <linux/ieee80211.h>
  27. #include <net/cfg80211.h>
  28. #include "iwm.h"
  29. #include "commands.h"
  30. #include "cfg80211.h"
  31. #include "debug.h"
  32. #define RATETAB_ENT(_rate, _rateid, _flags) \
  33. { \
  34. .bitrate = (_rate), \
  35. .hw_value = (_rateid), \
  36. .flags = (_flags), \
  37. }
  38. #define CHAN2G(_channel, _freq, _flags) { \
  39. .band = IEEE80211_BAND_2GHZ, \
  40. .center_freq = (_freq), \
  41. .hw_value = (_channel), \
  42. .flags = (_flags), \
  43. .max_antenna_gain = 0, \
  44. .max_power = 30, \
  45. }
  46. #define CHAN5G(_channel, _flags) { \
  47. .band = IEEE80211_BAND_5GHZ, \
  48. .center_freq = 5000 + (5 * (_channel)), \
  49. .hw_value = (_channel), \
  50. .flags = (_flags), \
  51. .max_antenna_gain = 0, \
  52. .max_power = 30, \
  53. }
  54. static struct ieee80211_rate iwm_rates[] = {
  55. RATETAB_ENT(10, 0x1, 0),
  56. RATETAB_ENT(20, 0x2, 0),
  57. RATETAB_ENT(55, 0x4, 0),
  58. RATETAB_ENT(110, 0x8, 0),
  59. RATETAB_ENT(60, 0x10, 0),
  60. RATETAB_ENT(90, 0x20, 0),
  61. RATETAB_ENT(120, 0x40, 0),
  62. RATETAB_ENT(180, 0x80, 0),
  63. RATETAB_ENT(240, 0x100, 0),
  64. RATETAB_ENT(360, 0x200, 0),
  65. RATETAB_ENT(480, 0x400, 0),
  66. RATETAB_ENT(540, 0x800, 0),
  67. };
  68. #define iwm_a_rates (iwm_rates + 4)
  69. #define iwm_a_rates_size 8
  70. #define iwm_g_rates (iwm_rates + 0)
  71. #define iwm_g_rates_size 12
  72. static struct ieee80211_channel iwm_2ghz_channels[] = {
  73. CHAN2G(1, 2412, 0),
  74. CHAN2G(2, 2417, 0),
  75. CHAN2G(3, 2422, 0),
  76. CHAN2G(4, 2427, 0),
  77. CHAN2G(5, 2432, 0),
  78. CHAN2G(6, 2437, 0),
  79. CHAN2G(7, 2442, 0),
  80. CHAN2G(8, 2447, 0),
  81. CHAN2G(9, 2452, 0),
  82. CHAN2G(10, 2457, 0),
  83. CHAN2G(11, 2462, 0),
  84. CHAN2G(12, 2467, 0),
  85. CHAN2G(13, 2472, 0),
  86. CHAN2G(14, 2484, 0),
  87. };
  88. static struct ieee80211_channel iwm_5ghz_a_channels[] = {
  89. CHAN5G(34, 0), CHAN5G(36, 0),
  90. CHAN5G(38, 0), CHAN5G(40, 0),
  91. CHAN5G(42, 0), CHAN5G(44, 0),
  92. CHAN5G(46, 0), CHAN5G(48, 0),
  93. CHAN5G(52, 0), CHAN5G(56, 0),
  94. CHAN5G(60, 0), CHAN5G(64, 0),
  95. CHAN5G(100, 0), CHAN5G(104, 0),
  96. CHAN5G(108, 0), CHAN5G(112, 0),
  97. CHAN5G(116, 0), CHAN5G(120, 0),
  98. CHAN5G(124, 0), CHAN5G(128, 0),
  99. CHAN5G(132, 0), CHAN5G(136, 0),
  100. CHAN5G(140, 0), CHAN5G(149, 0),
  101. CHAN5G(153, 0), CHAN5G(157, 0),
  102. CHAN5G(161, 0), CHAN5G(165, 0),
  103. CHAN5G(184, 0), CHAN5G(188, 0),
  104. CHAN5G(192, 0), CHAN5G(196, 0),
  105. CHAN5G(200, 0), CHAN5G(204, 0),
  106. CHAN5G(208, 0), CHAN5G(212, 0),
  107. CHAN5G(216, 0),
  108. };
  109. static struct ieee80211_supported_band iwm_band_2ghz = {
  110. .channels = iwm_2ghz_channels,
  111. .n_channels = ARRAY_SIZE(iwm_2ghz_channels),
  112. .bitrates = iwm_g_rates,
  113. .n_bitrates = iwm_g_rates_size,
  114. };
  115. static struct ieee80211_supported_band iwm_band_5ghz = {
  116. .channels = iwm_5ghz_a_channels,
  117. .n_channels = ARRAY_SIZE(iwm_5ghz_a_channels),
  118. .bitrates = iwm_a_rates,
  119. .n_bitrates = iwm_a_rates_size,
  120. };
  121. int iwm_cfg80211_inform_bss(struct iwm_priv *iwm)
  122. {
  123. struct wiphy *wiphy = iwm_to_wiphy(iwm);
  124. struct iwm_bss_info *bss, *next;
  125. struct iwm_umac_notif_bss_info *umac_bss;
  126. struct ieee80211_mgmt *mgmt;
  127. struct ieee80211_channel *channel;
  128. struct ieee80211_supported_band *band;
  129. s32 signal;
  130. int freq;
  131. list_for_each_entry_safe(bss, next, &iwm->bss_list, node) {
  132. umac_bss = bss->bss;
  133. mgmt = (struct ieee80211_mgmt *)(umac_bss->frame_buf);
  134. if (umac_bss->band == UMAC_BAND_2GHZ)
  135. band = wiphy->bands[IEEE80211_BAND_2GHZ];
  136. else if (umac_bss->band == UMAC_BAND_5GHZ)
  137. band = wiphy->bands[IEEE80211_BAND_5GHZ];
  138. else {
  139. IWM_ERR(iwm, "Invalid band: %d\n", umac_bss->band);
  140. return -EINVAL;
  141. }
  142. freq = ieee80211_channel_to_frequency(umac_bss->channel);
  143. channel = ieee80211_get_channel(wiphy, freq);
  144. signal = umac_bss->rssi * 100;
  145. if (!cfg80211_inform_bss_frame(wiphy, channel, mgmt,
  146. le16_to_cpu(umac_bss->frame_len),
  147. signal, GFP_KERNEL))
  148. return -EINVAL;
  149. }
  150. return 0;
  151. }
  152. static int iwm_cfg80211_change_iface(struct wiphy *wiphy,
  153. struct net_device *ndev,
  154. enum nl80211_iftype type, u32 *flags,
  155. struct vif_params *params)
  156. {
  157. struct wireless_dev *wdev;
  158. struct iwm_priv *iwm;
  159. u32 old_mode;
  160. wdev = ndev->ieee80211_ptr;
  161. iwm = ndev_to_iwm(ndev);
  162. old_mode = iwm->conf.mode;
  163. switch (type) {
  164. case NL80211_IFTYPE_STATION:
  165. iwm->conf.mode = UMAC_MODE_BSS;
  166. break;
  167. case NL80211_IFTYPE_ADHOC:
  168. iwm->conf.mode = UMAC_MODE_IBSS;
  169. break;
  170. default:
  171. return -EOPNOTSUPP;
  172. }
  173. wdev->iftype = type;
  174. if ((old_mode == iwm->conf.mode) || !iwm->umac_profile)
  175. return 0;
  176. iwm->umac_profile->mode = cpu_to_le32(iwm->conf.mode);
  177. if (iwm->umac_profile_active) {
  178. int ret = iwm_invalidate_mlme_profile(iwm);
  179. if (ret < 0)
  180. IWM_ERR(iwm, "Couldn't invalidate profile\n");
  181. }
  182. return 0;
  183. }
  184. static int iwm_cfg80211_scan(struct wiphy *wiphy, struct net_device *ndev,
  185. struct cfg80211_scan_request *request)
  186. {
  187. struct iwm_priv *iwm = ndev_to_iwm(ndev);
  188. int ret;
  189. if (!test_bit(IWM_STATUS_READY, &iwm->status)) {
  190. IWM_ERR(iwm, "Scan while device is not ready\n");
  191. return -EIO;
  192. }
  193. if (test_bit(IWM_STATUS_SCANNING, &iwm->status)) {
  194. IWM_ERR(iwm, "Scanning already\n");
  195. return -EAGAIN;
  196. }
  197. if (test_bit(IWM_STATUS_SCAN_ABORTING, &iwm->status)) {
  198. IWM_ERR(iwm, "Scanning being aborted\n");
  199. return -EAGAIN;
  200. }
  201. set_bit(IWM_STATUS_SCANNING, &iwm->status);
  202. ret = iwm_scan_ssids(iwm, request->ssids, request->n_ssids);
  203. if (ret) {
  204. clear_bit(IWM_STATUS_SCANNING, &iwm->status);
  205. return ret;
  206. }
  207. iwm->scan_request = request;
  208. return 0;
  209. }
  210. static int iwm_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
  211. {
  212. struct iwm_priv *iwm = wiphy_to_iwm(wiphy);
  213. if (changed & WIPHY_PARAM_RTS_THRESHOLD &&
  214. (iwm->conf.rts_threshold != wiphy->rts_threshold)) {
  215. int ret;
  216. iwm->conf.rts_threshold = wiphy->rts_threshold;
  217. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  218. CFG_RTS_THRESHOLD,
  219. iwm->conf.rts_threshold);
  220. if (ret < 0)
  221. return ret;
  222. }
  223. if (changed & WIPHY_PARAM_FRAG_THRESHOLD &&
  224. (iwm->conf.frag_threshold != wiphy->frag_threshold)) {
  225. int ret;
  226. iwm->conf.frag_threshold = wiphy->frag_threshold;
  227. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_FA_CFG_FIX,
  228. CFG_FRAG_THRESHOLD,
  229. iwm->conf.frag_threshold);
  230. if (ret < 0)
  231. return ret;
  232. }
  233. return 0;
  234. }
  235. static int iwm_cfg80211_join_ibss(struct wiphy *wiphy, struct net_device *dev,
  236. struct cfg80211_ibss_params *params)
  237. {
  238. struct iwm_priv *iwm = wiphy_to_iwm(wiphy);
  239. struct ieee80211_channel *chan = params->channel;
  240. struct cfg80211_bss *bss;
  241. if (!test_bit(IWM_STATUS_READY, &iwm->status))
  242. return -EIO;
  243. /* UMAC doesn't support creating IBSS network with specified bssid.
  244. * This should be removed after we have join only mode supported. */
  245. if (params->bssid)
  246. return -EOPNOTSUPP;
  247. bss = cfg80211_get_ibss(iwm_to_wiphy(iwm), NULL,
  248. params->ssid, params->ssid_len);
  249. if (!bss) {
  250. iwm_scan_one_ssid(iwm, params->ssid, params->ssid_len);
  251. schedule_timeout_interruptible(2 * HZ);
  252. bss = cfg80211_get_ibss(iwm_to_wiphy(iwm), NULL,
  253. params->ssid, params->ssid_len);
  254. }
  255. /* IBSS join only mode is not supported by UMAC ATM */
  256. if (bss) {
  257. cfg80211_put_bss(bss);
  258. return -EOPNOTSUPP;
  259. }
  260. iwm->channel = ieee80211_frequency_to_channel(chan->center_freq);
  261. iwm->umac_profile->ibss.band = chan->band;
  262. iwm->umac_profile->ibss.channel = iwm->channel;
  263. iwm->umac_profile->ssid.ssid_len = params->ssid_len;
  264. memcpy(iwm->umac_profile->ssid.ssid, params->ssid, params->ssid_len);
  265. if (params->bssid)
  266. memcpy(&iwm->umac_profile->bssid[0], params->bssid, ETH_ALEN);
  267. return iwm_send_mlme_profile(iwm);
  268. }
  269. static int iwm_cfg80211_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
  270. {
  271. struct iwm_priv *iwm = wiphy_to_iwm(wiphy);
  272. if (iwm->umac_profile_active)
  273. return iwm_invalidate_mlme_profile(iwm);
  274. return 0;
  275. }
  276. static struct cfg80211_ops iwm_cfg80211_ops = {
  277. .change_virtual_intf = iwm_cfg80211_change_iface,
  278. .scan = iwm_cfg80211_scan,
  279. .set_wiphy_params = iwm_cfg80211_set_wiphy_params,
  280. .join_ibss = iwm_cfg80211_join_ibss,
  281. .leave_ibss = iwm_cfg80211_leave_ibss,
  282. };
  283. struct wireless_dev *iwm_wdev_alloc(int sizeof_bus, struct device *dev)
  284. {
  285. int ret = 0;
  286. struct wireless_dev *wdev;
  287. /*
  288. * We're trying to have the following memory
  289. * layout:
  290. *
  291. * +-------------------------+
  292. * | struct wiphy |
  293. * +-------------------------+
  294. * | struct iwm_priv |
  295. * +-------------------------+
  296. * | bus private data |
  297. * | (e.g. iwm_priv_sdio) |
  298. * +-------------------------+
  299. *
  300. */
  301. wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
  302. if (!wdev) {
  303. dev_err(dev, "Couldn't allocate wireless device\n");
  304. return ERR_PTR(-ENOMEM);
  305. }
  306. wdev->wiphy = wiphy_new(&iwm_cfg80211_ops,
  307. sizeof(struct iwm_priv) + sizeof_bus);
  308. if (!wdev->wiphy) {
  309. dev_err(dev, "Couldn't allocate wiphy device\n");
  310. ret = -ENOMEM;
  311. goto out_err_new;
  312. }
  313. set_wiphy_dev(wdev->wiphy, dev);
  314. wdev->wiphy->max_scan_ssids = UMAC_WIFI_IF_PROBE_OPTION_MAX;
  315. wdev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  316. BIT(NL80211_IFTYPE_ADHOC);
  317. wdev->wiphy->bands[IEEE80211_BAND_2GHZ] = &iwm_band_2ghz;
  318. wdev->wiphy->bands[IEEE80211_BAND_5GHZ] = &iwm_band_5ghz;
  319. wdev->wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
  320. ret = wiphy_register(wdev->wiphy);
  321. if (ret < 0) {
  322. dev_err(dev, "Couldn't register wiphy device\n");
  323. goto out_err_register;
  324. }
  325. return wdev;
  326. out_err_register:
  327. wiphy_free(wdev->wiphy);
  328. out_err_new:
  329. kfree(wdev);
  330. return ERR_PTR(ret);
  331. }
  332. void iwm_wdev_free(struct iwm_priv *iwm)
  333. {
  334. struct wireless_dev *wdev = iwm_to_wdev(iwm);
  335. if (!wdev)
  336. return;
  337. wiphy_unregister(wdev->wiphy);
  338. wiphy_free(wdev->wiphy);
  339. kfree(wdev);
  340. }