cfg80211.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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, int ifindex,
  153. enum nl80211_iftype type, u32 *flags,
  154. struct vif_params *params)
  155. {
  156. struct net_device *ndev;
  157. struct wireless_dev *wdev;
  158. struct iwm_priv *iwm;
  159. u32 old_mode;
  160. /* we're under RTNL */
  161. ndev = __dev_get_by_index(&init_net, ifindex);
  162. if (!ndev)
  163. return -ENODEV;
  164. wdev = ndev->ieee80211_ptr;
  165. iwm = ndev_to_iwm(ndev);
  166. old_mode = iwm->conf.mode;
  167. switch (type) {
  168. case NL80211_IFTYPE_STATION:
  169. iwm->conf.mode = UMAC_MODE_BSS;
  170. break;
  171. case NL80211_IFTYPE_ADHOC:
  172. iwm->conf.mode = UMAC_MODE_IBSS;
  173. break;
  174. default:
  175. return -EOPNOTSUPP;
  176. }
  177. wdev->iftype = type;
  178. if ((old_mode == iwm->conf.mode) || !iwm->umac_profile)
  179. return 0;
  180. iwm->umac_profile->mode = cpu_to_le32(iwm->conf.mode);
  181. if (iwm->umac_profile_active) {
  182. int ret = iwm_invalidate_mlme_profile(iwm);
  183. if (ret < 0)
  184. IWM_ERR(iwm, "Couldn't invalidate profile\n");
  185. }
  186. return 0;
  187. }
  188. static int iwm_cfg80211_scan(struct wiphy *wiphy, struct net_device *ndev,
  189. struct cfg80211_scan_request *request)
  190. {
  191. struct iwm_priv *iwm = ndev_to_iwm(ndev);
  192. int ret;
  193. if (!test_bit(IWM_STATUS_READY, &iwm->status)) {
  194. IWM_ERR(iwm, "Scan while device is not ready\n");
  195. return -EIO;
  196. }
  197. if (test_bit(IWM_STATUS_SCANNING, &iwm->status)) {
  198. IWM_ERR(iwm, "Scanning already\n");
  199. return -EAGAIN;
  200. }
  201. if (test_bit(IWM_STATUS_SCAN_ABORTING, &iwm->status)) {
  202. IWM_ERR(iwm, "Scanning being aborted\n");
  203. return -EAGAIN;
  204. }
  205. set_bit(IWM_STATUS_SCANNING, &iwm->status);
  206. ret = iwm_scan_ssids(iwm, request->ssids, request->n_ssids);
  207. if (ret) {
  208. clear_bit(IWM_STATUS_SCANNING, &iwm->status);
  209. return ret;
  210. }
  211. iwm->scan_request = request;
  212. return 0;
  213. }
  214. static int iwm_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
  215. {
  216. struct iwm_priv *iwm = wiphy_to_iwm(wiphy);
  217. if (changed & WIPHY_PARAM_RTS_THRESHOLD &&
  218. (iwm->conf.rts_threshold != wiphy->rts_threshold)) {
  219. int ret;
  220. iwm->conf.rts_threshold = wiphy->rts_threshold;
  221. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  222. CFG_RTS_THRESHOLD,
  223. iwm->conf.rts_threshold);
  224. if (ret < 0)
  225. return ret;
  226. }
  227. if (changed & WIPHY_PARAM_FRAG_THRESHOLD &&
  228. (iwm->conf.frag_threshold != wiphy->frag_threshold)) {
  229. int ret;
  230. iwm->conf.frag_threshold = wiphy->frag_threshold;
  231. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_FA_CFG_FIX,
  232. CFG_FRAG_THRESHOLD,
  233. iwm->conf.frag_threshold);
  234. if (ret < 0)
  235. return ret;
  236. }
  237. return 0;
  238. }
  239. static int iwm_cfg80211_join_ibss(struct wiphy *wiphy, struct net_device *dev,
  240. struct cfg80211_ibss_params *params)
  241. {
  242. struct iwm_priv *iwm = wiphy_to_iwm(wiphy);
  243. struct ieee80211_channel *chan = params->channel;
  244. struct cfg80211_bss *bss;
  245. if (!test_bit(IWM_STATUS_READY, &iwm->status))
  246. return -EIO;
  247. /* UMAC doesn't support creating IBSS network with specified bssid.
  248. * This should be removed after we have join only mode supported. */
  249. if (params->bssid)
  250. return -EOPNOTSUPP;
  251. bss = cfg80211_get_ibss(iwm_to_wiphy(iwm), NULL,
  252. params->ssid, params->ssid_len);
  253. if (!bss) {
  254. iwm_scan_one_ssid(iwm, params->ssid, params->ssid_len);
  255. schedule_timeout_interruptible(2 * HZ);
  256. bss = cfg80211_get_ibss(iwm_to_wiphy(iwm), NULL,
  257. params->ssid, params->ssid_len);
  258. }
  259. /* IBSS join only mode is not supported by UMAC ATM */
  260. if (bss) {
  261. cfg80211_put_bss(bss);
  262. return -EOPNOTSUPP;
  263. }
  264. iwm->channel = ieee80211_frequency_to_channel(chan->center_freq);
  265. iwm->umac_profile->ibss.band = chan->band;
  266. iwm->umac_profile->ibss.channel = iwm->channel;
  267. iwm->umac_profile->ssid.ssid_len = params->ssid_len;
  268. memcpy(iwm->umac_profile->ssid.ssid, params->ssid, params->ssid_len);
  269. if (params->bssid)
  270. memcpy(&iwm->umac_profile->bssid[0], params->bssid, ETH_ALEN);
  271. return iwm_send_mlme_profile(iwm);
  272. }
  273. static int iwm_cfg80211_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
  274. {
  275. struct iwm_priv *iwm = wiphy_to_iwm(wiphy);
  276. if (iwm->umac_profile_active)
  277. return iwm_invalidate_mlme_profile(iwm);
  278. return 0;
  279. }
  280. static struct cfg80211_ops iwm_cfg80211_ops = {
  281. .change_virtual_intf = iwm_cfg80211_change_iface,
  282. .scan = iwm_cfg80211_scan,
  283. .set_wiphy_params = iwm_cfg80211_set_wiphy_params,
  284. .join_ibss = iwm_cfg80211_join_ibss,
  285. .leave_ibss = iwm_cfg80211_leave_ibss,
  286. };
  287. struct wireless_dev *iwm_wdev_alloc(int sizeof_bus, struct device *dev)
  288. {
  289. int ret = 0;
  290. struct wireless_dev *wdev;
  291. /*
  292. * We're trying to have the following memory
  293. * layout:
  294. *
  295. * +-------------------------+
  296. * | struct wiphy |
  297. * +-------------------------+
  298. * | struct iwm_priv |
  299. * +-------------------------+
  300. * | bus private data |
  301. * | (e.g. iwm_priv_sdio) |
  302. * +-------------------------+
  303. *
  304. */
  305. wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
  306. if (!wdev) {
  307. dev_err(dev, "Couldn't allocate wireless device\n");
  308. return ERR_PTR(-ENOMEM);
  309. }
  310. wdev->wiphy = wiphy_new(&iwm_cfg80211_ops,
  311. sizeof(struct iwm_priv) + sizeof_bus);
  312. if (!wdev->wiphy) {
  313. dev_err(dev, "Couldn't allocate wiphy device\n");
  314. ret = -ENOMEM;
  315. goto out_err_new;
  316. }
  317. set_wiphy_dev(wdev->wiphy, dev);
  318. wdev->wiphy->max_scan_ssids = UMAC_WIFI_IF_PROBE_OPTION_MAX;
  319. wdev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  320. BIT(NL80211_IFTYPE_ADHOC);
  321. wdev->wiphy->bands[IEEE80211_BAND_2GHZ] = &iwm_band_2ghz;
  322. wdev->wiphy->bands[IEEE80211_BAND_5GHZ] = &iwm_band_5ghz;
  323. wdev->wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
  324. ret = wiphy_register(wdev->wiphy);
  325. if (ret < 0) {
  326. dev_err(dev, "Couldn't register wiphy device\n");
  327. goto out_err_register;
  328. }
  329. return wdev;
  330. out_err_register:
  331. wiphy_free(wdev->wiphy);
  332. out_err_new:
  333. kfree(wdev);
  334. return ERR_PTR(ret);
  335. }
  336. void iwm_wdev_free(struct iwm_priv *iwm)
  337. {
  338. struct wireless_dev *wdev = iwm_to_wdev(iwm);
  339. if (!wdev)
  340. return;
  341. wiphy_unregister(wdev->wiphy);
  342. wiphy_free(wdev->wiphy);
  343. kfree(wdev);
  344. }