2 * Copyright (c) 2012 Qualcomm Atheros, Inc.
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.
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.
17 #include <linux/kernel.h>
18 #include <linux/netdevice.h>
19 #include <linux/sched.h>
20 #include <linux/etherdevice.h>
21 #include <linux/wireless.h>
22 #include <linux/ieee80211.h>
23 #include <linux/slab.h>
24 #include <net/cfg80211.h>
29 #define CHAN60G(_channel, _flags) { \
30 .band = IEEE80211_BAND_60GHZ, \
31 .center_freq = 56160 + (2160 * (_channel)), \
32 .hw_value = (_channel), \
34 .max_antenna_gain = 0, \
38 static struct ieee80211_channel wil_60ghz_channels[] = {
42 /* channel 4 not supported yet */
45 static struct ieee80211_supported_band wil_band_60ghz = {
46 .channels = wil_60ghz_channels,
47 .n_channels = ARRAY_SIZE(wil_60ghz_channels),
51 .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K, /* TODO */
52 .ampdu_density = IEEE80211_HT_MPDU_DENSITY_8, /* TODO */
54 /* MCS 1..12 - SC PHY */
55 .rx_mask = {0xfe, 0x1f}, /* 1..12 */
56 .tx_params = IEEE80211_HT_MCS_TX_DEFINED, /* TODO */
61 static const struct ieee80211_txrx_stypes
62 wil_mgmt_stypes[NUM_NL80211_IFTYPES] = {
63 [NL80211_IFTYPE_STATION] = {
64 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
65 BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
66 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
67 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
69 [NL80211_IFTYPE_AP] = {
70 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
71 BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
72 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
73 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
75 [NL80211_IFTYPE_P2P_CLIENT] = {
76 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
77 BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
78 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
79 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
81 [NL80211_IFTYPE_P2P_GO] = {
82 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
83 BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
84 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
85 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
89 static const u32 wil_cipher_suites[] = {
90 WLAN_CIPHER_SUITE_GCMP,
93 int wil_iftype_nl2wmi(enum nl80211_iftype type)
96 enum nl80211_iftype nl;
97 enum wmi_network_type wmi;
99 {NL80211_IFTYPE_ADHOC, WMI_NETTYPE_ADHOC},
100 {NL80211_IFTYPE_STATION, WMI_NETTYPE_INFRA},
101 {NL80211_IFTYPE_AP, WMI_NETTYPE_AP},
102 {NL80211_IFTYPE_P2P_CLIENT, WMI_NETTYPE_P2P},
103 {NL80211_IFTYPE_P2P_GO, WMI_NETTYPE_P2P},
104 {NL80211_IFTYPE_MONITOR, WMI_NETTYPE_ADHOC}, /* FIXME */
108 for (i = 0; i < ARRAY_SIZE(__nl2wmi); i++) {
109 if (__nl2wmi[i].nl == type)
110 return __nl2wmi[i].wmi;
116 static int wil_cfg80211_get_station(struct wiphy *wiphy,
117 struct net_device *ndev,
118 u8 *mac, struct station_info *sinfo)
120 struct wil6210_priv *wil = wiphy_to_wil(wiphy);
122 struct wmi_notify_req_cmd cmd = {
127 if (memcmp(mac, wil->dst_addr[0], ETH_ALEN))
130 /* WMI_NOTIFY_REQ_DONE_EVENTID handler fills wil->stats.bf_mcs */
131 rc = wmi_call(wil, WMI_NOTIFY_REQ_CMDID, &cmd, sizeof(cmd),
132 WMI_NOTIFY_REQ_DONE_EVENTID, NULL, 0, 20);
136 sinfo->generation = wil->sinfo_gen;
138 sinfo->filled |= STATION_INFO_TX_BITRATE;
139 sinfo->txrate.flags = RATE_INFO_FLAGS_MCS | RATE_INFO_FLAGS_60G;
140 sinfo->txrate.mcs = wil->stats.bf_mcs;
141 sinfo->filled |= STATION_INFO_RX_BITRATE;
142 sinfo->rxrate.flags = RATE_INFO_FLAGS_MCS | RATE_INFO_FLAGS_60G;
143 sinfo->rxrate.mcs = wil->stats.last_mcs_rx;
145 if (test_bit(wil_status_fwconnected, &wil->status)) {
146 sinfo->filled |= STATION_INFO_SIGNAL;
147 sinfo->signal = 12; /* TODO: provide real value */
153 static int wil_cfg80211_change_iface(struct wiphy *wiphy,
154 struct net_device *ndev,
155 enum nl80211_iftype type, u32 *flags,
156 struct vif_params *params)
158 struct wil6210_priv *wil = wiphy_to_wil(wiphy);
159 struct wireless_dev *wdev = wil->wdev;
162 case NL80211_IFTYPE_STATION:
163 case NL80211_IFTYPE_AP:
164 case NL80211_IFTYPE_P2P_CLIENT:
165 case NL80211_IFTYPE_P2P_GO:
167 case NL80211_IFTYPE_MONITOR:
169 wil->monitor_flags = *flags;
171 wil->monitor_flags = 0;
183 static int wil_cfg80211_scan(struct wiphy *wiphy,
184 struct cfg80211_scan_request *request)
186 struct wil6210_priv *wil = wiphy_to_wil(wiphy);
187 struct wireless_dev *wdev = wil->wdev;
189 struct wmi_start_scan_cmd cmd;
194 if (wil->scan_request) {
195 wil_err(wil, "Already scanning\n");
199 /* check we are client side */
200 switch (wdev->iftype) {
201 case NL80211_IFTYPE_STATION:
202 case NL80211_IFTYPE_P2P_CLIENT:
208 /* FW don't support scan after connection attempt */
209 if (test_bit(wil_status_dontscan, &wil->status)) {
210 wil_err(wil, "Scan after connect attempt not supported\n");
214 wil->scan_request = request;
216 memset(&cmd, 0, sizeof(cmd));
217 cmd.cmd.num_channels = 0;
218 n = min(request->n_channels, 4U);
219 for (i = 0; i < n; i++) {
220 int ch = request->channels[i]->hw_value;
223 "Scan requested for unknown frequency %dMhz\n",
224 request->channels[i]->center_freq);
227 /* 0-based channel indexes */
228 cmd.cmd.channel_list[cmd.cmd.num_channels++].channel = ch - 1;
229 wil_dbg_misc(wil, "Scan for ch %d : %d MHz\n", ch,
230 request->channels[i]->center_freq);
233 return wmi_send(wil, WMI_START_SCAN_CMDID, &cmd, sizeof(cmd.cmd) +
234 cmd.cmd.num_channels * sizeof(cmd.cmd.channel_list[0]));
237 static int wil_cfg80211_connect(struct wiphy *wiphy,
238 struct net_device *ndev,
239 struct cfg80211_connect_params *sme)
241 struct wil6210_priv *wil = wiphy_to_wil(wiphy);
242 struct cfg80211_bss *bss;
243 struct wmi_connect_cmd conn;
249 bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
250 sme->ssid, sme->ssid_len,
251 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
253 wil_err(wil, "Unable to find BSS\n");
257 ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
259 wil_err(wil, "No SSID\n");
265 cfg80211_find_ie(WLAN_EID_RSN, sme->ie, sme->ie_len) :
268 if (sme->ie_len > WMI_MAX_IE_LEN) {
270 wil_err(wil, "IE too large (%td bytes)\n",
275 * For secure assoc, send:
276 * (1) WMI_DELETE_CIPHER_KEY_CMD
277 * (2) WMI_SET_APPIE_CMD
279 rc = wmi_del_cipher_key(wil, 0, bss->bssid);
281 wil_err(wil, "WMI_DELETE_CIPHER_KEY_CMD failed\n");
284 /* WMI_SET_APPIE_CMD */
285 rc = wmi_set_ie(wil, WMI_FRAME_ASSOC_REQ, sme->ie_len, sme->ie);
287 wil_err(wil, "WMI_SET_APPIE_CMD failed\n");
292 /* WMI_CONNECT_CMD */
293 memset(&conn, 0, sizeof(conn));
294 switch (bss->capability & 0x03) {
295 case WLAN_CAPABILITY_DMG_TYPE_AP:
296 conn.network_type = WMI_NETTYPE_INFRA;
298 case WLAN_CAPABILITY_DMG_TYPE_PBSS:
299 conn.network_type = WMI_NETTYPE_P2P;
302 wil_err(wil, "Unsupported BSS type, capability= 0x%04x\n",
307 conn.dot11_auth_mode = WMI_AUTH11_SHARED;
308 conn.auth_mode = WMI_AUTH_WPA2_PSK;
309 conn.pairwise_crypto_type = WMI_CRYPT_AES_GCMP;
310 conn.pairwise_crypto_len = 16;
312 conn.dot11_auth_mode = WMI_AUTH11_OPEN;
313 conn.auth_mode = WMI_AUTH_NONE;
316 conn.ssid_len = min_t(u8, ssid_eid[1], 32);
317 memcpy(conn.ssid, ssid_eid+2, conn.ssid_len);
319 ch = bss->channel->hw_value;
321 wil_err(wil, "BSS at unknown frequency %dMhz\n",
322 bss->channel->center_freq);
326 conn.channel = ch - 1;
328 memcpy(conn.bssid, bss->bssid, 6);
329 memcpy(conn.dst_mac, bss->bssid, 6);
331 * FW don't support scan after connection attempt
333 set_bit(wil_status_dontscan, &wil->status);
335 rc = wmi_send(wil, WMI_CONNECT_CMDID, &conn, sizeof(conn));
337 /* Connect can take lots of time */
338 mod_timer(&wil->connect_timer,
339 jiffies + msecs_to_jiffies(2000));
343 cfg80211_put_bss(wiphy, bss);
348 static int wil_cfg80211_disconnect(struct wiphy *wiphy,
349 struct net_device *ndev,
353 struct wil6210_priv *wil = wiphy_to_wil(wiphy);
355 rc = wmi_send(wil, WMI_DISCONNECT_CMDID, NULL, 0);
360 static int wil_cfg80211_set_channel(struct wiphy *wiphy,
361 struct cfg80211_chan_def *chandef)
363 struct wil6210_priv *wil = wiphy_to_wil(wiphy);
364 struct wireless_dev *wdev = wil->wdev;
366 wdev->preset_chandef = *chandef;
371 static int wil_cfg80211_add_key(struct wiphy *wiphy,
372 struct net_device *ndev,
373 u8 key_index, bool pairwise,
375 struct key_params *params)
377 struct wil6210_priv *wil = wiphy_to_wil(wiphy);
379 /* group key is not used */
383 return wmi_add_cipher_key(wil, key_index, mac_addr,
384 params->key_len, params->key);
387 static int wil_cfg80211_del_key(struct wiphy *wiphy,
388 struct net_device *ndev,
389 u8 key_index, bool pairwise,
392 struct wil6210_priv *wil = wiphy_to_wil(wiphy);
394 /* group key is not used */
398 return wmi_del_cipher_key(wil, key_index, mac_addr);
401 /* Need to be present or wiphy_new() will WARN */
402 static int wil_cfg80211_set_default_key(struct wiphy *wiphy,
403 struct net_device *ndev,
404 u8 key_index, bool unicast,
410 static int wil_cfg80211_start_ap(struct wiphy *wiphy,
411 struct net_device *ndev,
412 struct cfg80211_ap_settings *info)
415 struct wil6210_priv *wil = wiphy_to_wil(wiphy);
416 struct wireless_dev *wdev = ndev->ieee80211_ptr;
417 struct ieee80211_channel *channel = info->chandef.chan;
418 struct cfg80211_beacon_data *bcon = &info->beacon;
419 u8 wmi_nettype = wil_iftype_nl2wmi(wdev->iftype);
422 wil_err(wil, "AP: No channel???\n");
426 wil_dbg_misc(wil, "AP on Channel %d %d MHz, %s\n", channel->hw_value,
427 channel->center_freq, info->privacy ? "secure" : "open");
428 print_hex_dump_bytes("SSID ", DUMP_PREFIX_OFFSET,
429 info->ssid, info->ssid_len);
435 rc = wmi_set_ssid(wil, info->ssid_len, info->ssid);
439 rc = wmi_set_channel(wil, channel->hw_value);
443 /* MAC address - pre-requisite for other commands */
444 wmi_set_mac_address(wil, ndev->dev_addr);
447 /* bcon 'head IE's are not relevant for 60g band */
448 wmi_set_ie(wil, WMI_FRAME_BEACON, bcon->beacon_ies_len,
450 wmi_set_ie(wil, WMI_FRAME_PROBE_RESP, bcon->proberesp_ies_len,
451 bcon->proberesp_ies);
452 wmi_set_ie(wil, WMI_FRAME_ASSOC_RESP, bcon->assocresp_ies_len,
453 bcon->assocresp_ies);
455 wil->secure_pcp = info->privacy;
457 rc = wmi_set_bcon(wil, info->beacon_interval, wmi_nettype);
461 /* Rx VRING. After MAC and beacon */
462 rc = wil_rx_init(wil);
464 netif_carrier_on(ndev);
469 static int wil_cfg80211_stop_ap(struct wiphy *wiphy,
470 struct net_device *ndev)
473 struct wil6210_priv *wil = wiphy_to_wil(wiphy);
474 struct wireless_dev *wdev = ndev->ieee80211_ptr;
475 u8 wmi_nettype = wil_iftype_nl2wmi(wdev->iftype);
477 /* To stop beaconing, set BI to 0 */
478 rc = wmi_set_bcon(wil, 0, wmi_nettype);
483 static struct cfg80211_ops wil_cfg80211_ops = {
484 .scan = wil_cfg80211_scan,
485 .connect = wil_cfg80211_connect,
486 .disconnect = wil_cfg80211_disconnect,
487 .change_virtual_intf = wil_cfg80211_change_iface,
488 .get_station = wil_cfg80211_get_station,
489 .set_monitor_channel = wil_cfg80211_set_channel,
490 .add_key = wil_cfg80211_add_key,
491 .del_key = wil_cfg80211_del_key,
492 .set_default_key = wil_cfg80211_set_default_key,
494 .start_ap = wil_cfg80211_start_ap,
495 .stop_ap = wil_cfg80211_stop_ap,
498 static void wil_wiphy_init(struct wiphy *wiphy)
500 /* TODO: set real value */
501 wiphy->max_scan_ssids = 10;
502 wiphy->max_num_pmkids = 0 /* TODO: */;
503 wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
504 BIT(NL80211_IFTYPE_AP) |
505 BIT(NL80211_IFTYPE_MONITOR);
506 /* TODO: enable P2P when integrated with supplicant:
507 * BIT(NL80211_IFTYPE_P2P_CLIENT) | BIT(NL80211_IFTYPE_P2P_GO)
509 wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME |
510 WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
511 dev_warn(wiphy_dev(wiphy), "%s : flags = 0x%08x\n",
512 __func__, wiphy->flags);
513 wiphy->probe_resp_offload =
514 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
515 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
516 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
518 wiphy->bands[IEEE80211_BAND_60GHZ] = &wil_band_60ghz;
520 /* TODO: figure this out */
521 wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
523 wiphy->cipher_suites = wil_cipher_suites;
524 wiphy->n_cipher_suites = ARRAY_SIZE(wil_cipher_suites);
525 wiphy->mgmt_stypes = wil_mgmt_stypes;
528 struct wireless_dev *wil_cfg80211_init(struct device *dev)
531 struct wireless_dev *wdev;
533 wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
535 return ERR_PTR(-ENOMEM);
537 wdev->wiphy = wiphy_new(&wil_cfg80211_ops,
538 sizeof(struct wil6210_priv));
544 set_wiphy_dev(wdev->wiphy, dev);
545 wil_wiphy_init(wdev->wiphy);
547 rc = wiphy_register(wdev->wiphy);
554 wiphy_free(wdev->wiphy);
561 void wil_wdev_free(struct wil6210_priv *wil)
563 struct wireless_dev *wdev = wil_to_wdev(wil);
568 wiphy_unregister(wdev->wiphy);
569 wiphy_free(wdev->wiphy);