2 * Implement cfg80211 ("iw") support.
4 * Copyright (C) 2009 M&N Solutions GmbH, 61191 Rosbach, Germany
5 * Holger Schurig <hs4233@mail.mn-solutions.de>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/hardirq.h>
12 #include <linux/sched.h>
13 #include <linux/wait.h>
14 #include <linux/slab.h>
15 #include <linux/ieee80211.h>
16 #include <net/cfg80211.h>
17 #include <asm/unaligned.h>
25 #define CHAN2G(_channel, _freq, _flags) { \
26 .band = IEEE80211_BAND_2GHZ, \
27 .center_freq = (_freq), \
28 .hw_value = (_channel), \
30 .max_antenna_gain = 0, \
34 static struct ieee80211_channel lbs_2ghz_channels[] = {
51 #define RATETAB_ENT(_rate, _hw_value, _flags) { \
53 .hw_value = (_hw_value), \
58 /* Table 6 in section 3.2.1.1 */
59 static struct ieee80211_rate lbs_rates[] = {
60 RATETAB_ENT(10, 0, 0),
61 RATETAB_ENT(20, 1, 0),
62 RATETAB_ENT(55, 2, 0),
63 RATETAB_ENT(110, 3, 0),
64 RATETAB_ENT(60, 9, 0),
65 RATETAB_ENT(90, 6, 0),
66 RATETAB_ENT(120, 7, 0),
67 RATETAB_ENT(180, 8, 0),
68 RATETAB_ENT(240, 9, 0),
69 RATETAB_ENT(360, 10, 0),
70 RATETAB_ENT(480, 11, 0),
71 RATETAB_ENT(540, 12, 0),
74 static struct ieee80211_supported_band lbs_band_2ghz = {
75 .channels = lbs_2ghz_channels,
76 .n_channels = ARRAY_SIZE(lbs_2ghz_channels),
77 .bitrates = lbs_rates,
78 .n_bitrates = ARRAY_SIZE(lbs_rates),
82 static const u32 cipher_suites[] = {
83 WLAN_CIPHER_SUITE_WEP40,
84 WLAN_CIPHER_SUITE_WEP104,
85 WLAN_CIPHER_SUITE_TKIP,
86 WLAN_CIPHER_SUITE_CCMP,
89 /* Time to stay on the channel */
90 #define LBS_DWELL_PASSIVE 100
91 #define LBS_DWELL_ACTIVE 40
94 /***************************************************************************
95 * Misc utility functions
97 * TLVs are Marvell specific. They are very similar to IEs, they have the
98 * same structure: type, length, data*. The only difference: for IEs, the
99 * type and length are u8, but for TLVs they're __le16.
103 * Convert NL80211's auth_type to the one from Libertas, see chapter 5.9.1
104 * in the firmware spec
106 static u8 lbs_auth_to_authtype(enum nl80211_auth_type auth_type)
111 case NL80211_AUTHTYPE_OPEN_SYSTEM:
112 case NL80211_AUTHTYPE_SHARED_KEY:
115 case NL80211_AUTHTYPE_AUTOMATIC:
116 ret = NL80211_AUTHTYPE_OPEN_SYSTEM;
118 case NL80211_AUTHTYPE_NETWORK_EAP:
122 /* silence compiler */
130 * Various firmware commands need the list of supported rates, but with
131 * the hight-bit set for basic rates
133 static int lbs_add_rates(u8 *rates)
137 for (i = 0; i < ARRAY_SIZE(lbs_rates); i++) {
138 u8 rate = lbs_rates[i].bitrate / 5;
139 if (rate == 0x02 || rate == 0x04 ||
140 rate == 0x0b || rate == 0x16)
144 return ARRAY_SIZE(lbs_rates);
148 /***************************************************************************
149 * TLV utility functions
151 * TLVs are Marvell specific. They are very similar to IEs, they have the
152 * same structure: type, length, data*. The only difference: for IEs, the
153 * type and length are u8, but for TLVs they're __le16.
160 #define LBS_MAX_SSID_TLV_SIZE \
161 (sizeof(struct mrvl_ie_header) \
162 + IEEE80211_MAX_SSID_LEN)
164 static int lbs_add_ssid_tlv(u8 *tlv, const u8 *ssid, int ssid_len)
166 struct mrvl_ie_ssid_param_set *ssid_tlv = (void *)tlv;
171 * ssid 4d 4e 54 45 53 54
173 ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
174 ssid_tlv->header.len = cpu_to_le16(ssid_len);
175 memcpy(ssid_tlv->ssid, ssid, ssid_len);
176 return sizeof(ssid_tlv->header) + ssid_len;
181 * Add channel list TLV (section 8.4.2)
183 * Actual channel data comes from priv->wdev->wiphy->channels.
185 #define LBS_MAX_CHANNEL_LIST_TLV_SIZE \
186 (sizeof(struct mrvl_ie_header) \
187 + (LBS_SCAN_BEFORE_NAP * sizeof(struct chanscanparamset)))
189 static int lbs_add_channel_list_tlv(struct lbs_private *priv, u8 *tlv,
190 int last_channel, int active_scan)
192 int chanscanparamsize = sizeof(struct chanscanparamset) *
193 (last_channel - priv->scan_channel);
195 struct mrvl_ie_header *header = (void *) tlv;
198 * TLV-ID CHANLIST 01 01
200 * channel 00 01 00 00 00 64 00
204 * min scan time 00 00
205 * max scan time 64 00
206 * channel 2 00 02 00 00 00 64 00
210 header->type = cpu_to_le16(TLV_TYPE_CHANLIST);
211 header->len = cpu_to_le16(chanscanparamsize);
212 tlv += sizeof(struct mrvl_ie_header);
214 /* lbs_deb_scan("scan: channels %d to %d\n", priv->scan_channel,
216 memset(tlv, 0, chanscanparamsize);
218 while (priv->scan_channel < last_channel) {
219 struct chanscanparamset *param = (void *) tlv;
221 param->radiotype = CMD_SCAN_RADIO_TYPE_BG;
223 priv->scan_req->channels[priv->scan_channel]->hw_value;
225 param->maxscantime = cpu_to_le16(LBS_DWELL_ACTIVE);
227 param->chanscanmode.passivescan = 1;
228 param->maxscantime = cpu_to_le16(LBS_DWELL_PASSIVE);
230 tlv += sizeof(struct chanscanparamset);
231 priv->scan_channel++;
233 return sizeof(struct mrvl_ie_header) + chanscanparamsize;
240 * The rates are in lbs_bg_rates[], but for the 802.11b
241 * rates the high bit is set. We add this TLV only because
242 * there's a firmware which otherwise doesn't report all
245 #define LBS_MAX_RATES_TLV_SIZE \
246 (sizeof(struct mrvl_ie_header) \
247 + (ARRAY_SIZE(lbs_rates)))
249 /* Adds a TLV with all rates the hardware supports */
250 static int lbs_add_supported_rates_tlv(u8 *tlv)
253 struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
258 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
260 rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
261 tlv += sizeof(rate_tlv->header);
262 i = lbs_add_rates(tlv);
264 rate_tlv->header.len = cpu_to_le16(i);
265 return sizeof(rate_tlv->header) + i;
268 /* Add common rates from a TLV and return the new end of the TLV */
270 add_ie_rates(u8 *tlv, const u8 *ie, int *nrates)
272 int hw, ap, ap_max = ie[1];
275 /* Advance past IE header */
278 lbs_deb_hex(LBS_DEB_ASSOC, "AP IE Rates", (u8 *) ie, ap_max);
280 for (hw = 0; hw < ARRAY_SIZE(lbs_rates); hw++) {
281 hw_rate = lbs_rates[hw].bitrate / 5;
282 for (ap = 0; ap < ap_max; ap++) {
283 if (hw_rate == (ie[ap] & 0x7f)) {
285 *nrates = *nrates + 1;
293 * Adds a TLV with all rates the hardware *and* BSS supports.
295 static int lbs_add_common_rates_tlv(u8 *tlv, struct cfg80211_bss *bss)
297 struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
298 const u8 *rates_eid, *ext_rates_eid;
301 rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
302 ext_rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
305 * 01 00 TLV_TYPE_RATES
309 rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
310 tlv += sizeof(rate_tlv->header);
312 /* Add basic rates */
314 tlv = add_ie_rates(tlv, rates_eid, &n);
316 /* Add extended rates, if any */
318 tlv = add_ie_rates(tlv, ext_rates_eid, &n);
320 lbs_deb_assoc("assoc: bss had no basic rate IE\n");
321 /* Fallback: add basic 802.11b rates */
329 rate_tlv->header.len = cpu_to_le16(n);
330 return sizeof(rate_tlv->header) + n;
337 * This is only needed for newer firmware (V9 and up).
339 #define LBS_MAX_AUTH_TYPE_TLV_SIZE \
340 sizeof(struct mrvl_ie_auth_type)
342 static int lbs_add_auth_type_tlv(u8 *tlv, enum nl80211_auth_type auth_type)
344 struct mrvl_ie_auth_type *auth = (void *) tlv;
347 * 1f 01 TLV_TYPE_AUTH_TYPE
351 auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
352 auth->header.len = cpu_to_le16(sizeof(*auth)-sizeof(auth->header));
353 auth->auth = cpu_to_le16(lbs_auth_to_authtype(auth_type));
354 return sizeof(*auth);
359 * Add channel (phy ds) TLV
361 #define LBS_MAX_CHANNEL_TLV_SIZE \
362 sizeof(struct mrvl_ie_header)
364 static int lbs_add_channel_tlv(u8 *tlv, u8 channel)
366 struct mrvl_ie_ds_param_set *ds = (void *) tlv;
369 * 03 00 TLV_TYPE_PHY_DS
373 ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
374 ds->header.len = cpu_to_le16(sizeof(*ds)-sizeof(ds->header));
375 ds->channel = channel;
381 * Add (empty) CF param TLV of the form:
383 #define LBS_MAX_CF_PARAM_TLV_SIZE \
384 sizeof(struct mrvl_ie_header)
386 static int lbs_add_cf_param_tlv(u8 *tlv)
388 struct mrvl_ie_cf_param_set *cf = (void *)tlv;
395 * 00 00 cfpmaxduration
396 * 00 00 cfpdurationremaining
398 cf->header.type = cpu_to_le16(TLV_TYPE_CF);
399 cf->header.len = cpu_to_le16(sizeof(*cf)-sizeof(cf->header));
406 #define LBS_MAX_WPA_TLV_SIZE \
407 (sizeof(struct mrvl_ie_header) \
408 + 128 /* TODO: I guessed the size */)
410 static int lbs_add_wpa_tlv(u8 *tlv, const u8 *ie, u8 ie_len)
415 * We need just convert an IE to an TLV. IEs use u8 for the header,
419 * but TLVs use __le16 instead:
426 tlv_len = *tlv++ = *ie++;
430 /* the TLV is two bytes larger than the IE */
438 static int lbs_cfg_set_channel(struct wiphy *wiphy,
439 struct net_device *netdev,
440 struct ieee80211_channel *channel,
441 enum nl80211_channel_type channel_type)
443 struct lbs_private *priv = wiphy_priv(wiphy);
446 lbs_deb_enter_args(LBS_DEB_CFG80211, "iface %s freq %d, type %d",
447 netdev_name(netdev), channel->center_freq, channel_type);
449 if (channel_type != NL80211_CHAN_NO_HT)
452 if (netdev == priv->mesh_dev)
453 ret = lbs_mesh_set_channel(priv, channel->hw_value);
455 ret = lbs_set_channel(priv, channel->hw_value);
458 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
469 * When scanning, the firmware doesn't send a nul packet with the power-safe
470 * bit to the AP. So we cannot stay away from our current channel too long,
471 * otherwise we loose data. So take a "nap" while scanning every other
474 #define LBS_SCAN_BEFORE_NAP 4
478 * When the firmware reports back a scan-result, it gives us an "u8 rssi",
479 * which isn't really an RSSI, as it becomes larger when moving away from
480 * the AP. Anyway, we need to convert that into mBm.
482 #define LBS_SCAN_RSSI_TO_MBM(rssi) \
483 ((-(int)rssi + 3)*100)
485 static int lbs_ret_scan(struct lbs_private *priv, unsigned long dummy,
486 struct cmd_header *resp)
488 struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
496 lbs_deb_enter(LBS_DEB_CFG80211);
498 bsssize = get_unaligned_le16(&scanresp->bssdescriptsize);
500 lbs_deb_scan("scan response: %d BSSs (%d bytes); resp size %d bytes\n",
501 scanresp->nr_sets, bsssize, le16_to_cpu(resp->size));
503 if (scanresp->nr_sets == 0) {
509 * The general layout of the scan response is described in chapter
510 * 5.7.1. Basically we have a common part, then any number of BSS
511 * descriptor sections. Finally we have section with the same number
514 * cmd_ds_802_11_scan_rsp
527 * MrvlIEtypes_TsfFimestamp_t
533 pos = scanresp->bssdesc_and_tlvbuffer;
535 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_RSP", scanresp->bssdesc_and_tlvbuffer,
536 scanresp->bssdescriptsize);
538 tsfdesc = pos + bsssize;
539 tsfsize = 4 + 8 * scanresp->nr_sets;
540 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TSF", (u8 *) tsfdesc, tsfsize);
542 /* Validity check: we expect a Marvell-Local TLV */
543 i = get_unaligned_le16(tsfdesc);
545 if (i != TLV_TYPE_TSFTIMESTAMP) {
546 lbs_deb_scan("scan response: invalid TSF Timestamp %d\n", i);
551 * Validity check: the TLV holds TSF values with 8 bytes each, so
552 * the size in the TLV must match the nr_sets value
554 i = get_unaligned_le16(tsfdesc);
556 if (i / 8 != scanresp->nr_sets) {
557 lbs_deb_scan("scan response: invalid number of TSF timestamp "
558 "sets (expected %d got %d)\n", scanresp->nr_sets,
563 for (i = 0; i < scanresp->nr_sets; i++) {
572 const u8 *ssid = NULL;
574 DECLARE_SSID_BUF(ssid_buf);
576 int len = get_unaligned_le16(pos);
584 /* Packet time stamp */
586 /* Beacon interval */
587 intvl = get_unaligned_le16(pos);
590 capa = get_unaligned_le16(pos);
593 /* To find out the channel, we must parse the IEs */
596 * 6+1+8+2+2: size of BSSID, RSSI, time stamp, beacon
597 * interval, capabilities
599 ielen = left = len - (6 + 1 + 8 + 2 + 2);
605 if (elen > left || elen == 0) {
606 lbs_deb_scan("scan response: invalid IE fmt\n");
610 if (id == WLAN_EID_DS_PARAMS)
612 if (id == WLAN_EID_SSID) {
620 /* No channel, no luck */
622 struct wiphy *wiphy = priv->wdev->wiphy;
623 int freq = ieee80211_channel_to_frequency(chan_no,
624 IEEE80211_BAND_2GHZ);
625 struct ieee80211_channel *channel =
626 ieee80211_get_channel(wiphy, freq);
628 lbs_deb_scan("scan: %pM, capa %04x, chan %2d, %s, "
630 bssid, capa, chan_no,
631 print_ssid(ssid_buf, ssid, ssid_len),
632 LBS_SCAN_RSSI_TO_MBM(rssi)/100);
635 !(channel->flags & IEEE80211_CHAN_DISABLED))
636 cfg80211_inform_bss(wiphy, channel,
637 bssid, get_unaligned_le64(tsfdesc),
638 capa, intvl, ie, ielen,
639 LBS_SCAN_RSSI_TO_MBM(rssi),
642 lbs_deb_scan("scan response: missing BSS channel IE\n");
649 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
655 * Our scan command contains a TLV, consting of a SSID TLV, a channel list
656 * TLV and a rates TLV. Determine the maximum size of them:
658 #define LBS_SCAN_MAX_CMD_SIZE \
659 (sizeof(struct cmd_ds_802_11_scan) \
660 + LBS_MAX_SSID_TLV_SIZE \
661 + LBS_MAX_CHANNEL_LIST_TLV_SIZE \
662 + LBS_MAX_RATES_TLV_SIZE)
665 * Assumes priv->scan_req is initialized and valid
666 * Assumes priv->scan_channel is initialized
668 static void lbs_scan_worker(struct work_struct *work)
670 struct lbs_private *priv =
671 container_of(work, struct lbs_private, scan_work.work);
672 struct cmd_ds_802_11_scan *scan_cmd;
673 u8 *tlv; /* pointer into our current, growing TLV storage area */
675 int running, carrier;
677 lbs_deb_enter(LBS_DEB_SCAN);
679 scan_cmd = kzalloc(LBS_SCAN_MAX_CMD_SIZE, GFP_KERNEL);
680 if (scan_cmd == NULL)
681 goto out_no_scan_cmd;
683 /* prepare fixed part of scan command */
684 scan_cmd->bsstype = CMD_BSS_TYPE_ANY;
686 /* stop network while we're away from our main channel */
687 running = !netif_queue_stopped(priv->dev);
688 carrier = netif_carrier_ok(priv->dev);
690 netif_stop_queue(priv->dev);
692 netif_carrier_off(priv->dev);
694 /* prepare fixed part of scan command */
695 tlv = scan_cmd->tlvbuffer;
698 if (priv->scan_req->n_ssids && priv->scan_req->ssids[0].ssid_len > 0)
699 tlv += lbs_add_ssid_tlv(tlv,
700 priv->scan_req->ssids[0].ssid,
701 priv->scan_req->ssids[0].ssid_len);
703 /* add channel TLVs */
704 last_channel = priv->scan_channel + LBS_SCAN_BEFORE_NAP;
705 if (last_channel > priv->scan_req->n_channels)
706 last_channel = priv->scan_req->n_channels;
707 tlv += lbs_add_channel_list_tlv(priv, tlv, last_channel,
708 priv->scan_req->n_ssids);
711 tlv += lbs_add_supported_rates_tlv(tlv);
713 if (priv->scan_channel < priv->scan_req->n_channels) {
714 cancel_delayed_work(&priv->scan_work);
715 if (netif_running(priv->dev))
716 queue_delayed_work(priv->work_thread, &priv->scan_work,
717 msecs_to_jiffies(300));
720 /* This is the final data we are about to send */
721 scan_cmd->hdr.size = cpu_to_le16(tlv - (u8 *)scan_cmd);
722 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
724 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
725 tlv - scan_cmd->tlvbuffer);
727 __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
728 le16_to_cpu(scan_cmd->hdr.size),
731 if (priv->scan_channel >= priv->scan_req->n_channels)
735 /* Restart network */
737 netif_carrier_on(priv->dev);
738 if (running && !priv->tx_pending_len)
739 netif_wake_queue(priv->dev);
743 /* Wake up anything waiting on scan completion */
744 if (priv->scan_req == NULL) {
745 lbs_deb_scan("scan: waking up waiters\n");
746 wake_up_all(&priv->scan_q);
750 lbs_deb_leave(LBS_DEB_SCAN);
753 static void _internal_start_scan(struct lbs_private *priv, bool internal,
754 struct cfg80211_scan_request *request)
756 lbs_deb_enter(LBS_DEB_CFG80211);
758 lbs_deb_scan("scan: ssids %d, channels %d, ie_len %zd\n",
759 request->n_ssids, request->n_channels, request->ie_len);
761 priv->scan_channel = 0;
762 queue_delayed_work(priv->work_thread, &priv->scan_work,
763 msecs_to_jiffies(50));
765 priv->scan_req = request;
766 priv->internal_scan = internal;
768 lbs_deb_leave(LBS_DEB_CFG80211);
772 * Clean up priv->scan_req. Should be used to handle the allocation details.
774 void lbs_scan_done(struct lbs_private *priv)
776 WARN_ON(!priv->scan_req);
778 if (priv->internal_scan)
779 kfree(priv->scan_req);
781 cfg80211_scan_done(priv->scan_req, false);
783 priv->scan_req = NULL;
786 static int lbs_cfg_scan(struct wiphy *wiphy,
787 struct net_device *dev,
788 struct cfg80211_scan_request *request)
790 struct lbs_private *priv = wiphy_priv(wiphy);
793 lbs_deb_enter(LBS_DEB_CFG80211);
795 if (priv->scan_req || delayed_work_pending(&priv->scan_work)) {
796 /* old scan request not yet processed */
801 _internal_start_scan(priv, false, request);
803 if (priv->surpriseremoved)
807 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
818 void lbs_send_disconnect_notification(struct lbs_private *priv)
820 lbs_deb_enter(LBS_DEB_CFG80211);
822 cfg80211_disconnected(priv->dev,
827 lbs_deb_leave(LBS_DEB_CFG80211);
830 void lbs_send_mic_failureevent(struct lbs_private *priv, u32 event)
832 lbs_deb_enter(LBS_DEB_CFG80211);
834 cfg80211_michael_mic_failure(priv->dev,
836 event == MACREG_INT_CODE_MIC_ERR_MULTICAST ?
837 NL80211_KEYTYPE_GROUP :
838 NL80211_KEYTYPE_PAIRWISE,
843 lbs_deb_leave(LBS_DEB_CFG80211);
855 * This removes all WEP keys
857 static int lbs_remove_wep_keys(struct lbs_private *priv)
859 struct cmd_ds_802_11_set_wep cmd;
862 lbs_deb_enter(LBS_DEB_CFG80211);
864 memset(&cmd, 0, sizeof(cmd));
865 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
866 cmd.keyindex = cpu_to_le16(priv->wep_tx_key);
867 cmd.action = cpu_to_le16(CMD_ACT_REMOVE);
869 ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
871 lbs_deb_leave(LBS_DEB_CFG80211);
878 static int lbs_set_wep_keys(struct lbs_private *priv)
880 struct cmd_ds_802_11_set_wep cmd;
884 lbs_deb_enter(LBS_DEB_CFG80211);
891 * action 02 00 ACT_ADD
893 * type for key 1 01 WEP40
897 * key 1 39 39 39 39 39 00 00 00
898 * 00 00 00 00 00 00 00 00
899 * key 2 00 00 00 00 00 00 00 00
900 * 00 00 00 00 00 00 00 00
901 * key 3 00 00 00 00 00 00 00 00
902 * 00 00 00 00 00 00 00 00
903 * key 4 00 00 00 00 00 00 00 00
905 if (priv->wep_key_len[0] || priv->wep_key_len[1] ||
906 priv->wep_key_len[2] || priv->wep_key_len[3]) {
907 /* Only set wep keys if we have at least one of them */
908 memset(&cmd, 0, sizeof(cmd));
909 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
910 cmd.keyindex = cpu_to_le16(priv->wep_tx_key);
911 cmd.action = cpu_to_le16(CMD_ACT_ADD);
913 for (i = 0; i < 4; i++) {
914 switch (priv->wep_key_len[i]) {
915 case WLAN_KEY_LEN_WEP40:
916 cmd.keytype[i] = CMD_TYPE_WEP_40_BIT;
918 case WLAN_KEY_LEN_WEP104:
919 cmd.keytype[i] = CMD_TYPE_WEP_104_BIT;
925 memcpy(cmd.keymaterial[i], priv->wep_key[i],
926 priv->wep_key_len[i]);
929 ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
931 /* Otherwise remove all wep keys */
932 ret = lbs_remove_wep_keys(priv);
935 lbs_deb_leave(LBS_DEB_CFG80211);
941 * Enable/Disable RSN status
943 static int lbs_enable_rsn(struct lbs_private *priv, int enable)
945 struct cmd_ds_802_11_enable_rsn cmd;
948 lbs_deb_enter_args(LBS_DEB_CFG80211, "%d", enable);
955 * action 01 00 ACT_SET
958 memset(&cmd, 0, sizeof(cmd));
959 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
960 cmd.action = cpu_to_le16(CMD_ACT_SET);
961 cmd.enable = cpu_to_le16(enable);
963 ret = lbs_cmd_with_response(priv, CMD_802_11_ENABLE_RSN, &cmd);
965 lbs_deb_leave(LBS_DEB_CFG80211);
971 * Set WPA/WPA key material
975 * like "struct cmd_ds_802_11_key_material", but with cmd_header. Once we
976 * get rid of WEXT, this should go into host.h
979 struct cmd_key_material {
980 struct cmd_header hdr;
983 struct MrvlIEtype_keyParamSet param;
986 static int lbs_set_key_material(struct lbs_private *priv,
989 u8 *key, u16 key_len)
991 struct cmd_key_material cmd;
994 lbs_deb_enter(LBS_DEB_CFG80211);
997 * Example for WPA (TKIP):
1004 * TLV type 00 01 key param
1006 * key type 01 00 TKIP
1007 * key info 06 00 UNICAST | ENABLED
1011 memset(&cmd, 0, sizeof(cmd));
1012 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1013 cmd.action = cpu_to_le16(CMD_ACT_SET);
1014 cmd.param.type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
1015 cmd.param.length = cpu_to_le16(sizeof(cmd.param) - 4);
1016 cmd.param.keytypeid = cpu_to_le16(key_type);
1017 cmd.param.keyinfo = cpu_to_le16(key_info);
1018 cmd.param.keylen = cpu_to_le16(key_len);
1020 memcpy(cmd.param.key, key, key_len);
1022 ret = lbs_cmd_with_response(priv, CMD_802_11_KEY_MATERIAL, &cmd);
1024 lbs_deb_leave(LBS_DEB_CFG80211);
1030 * Sets the auth type (open, shared, etc) in the firmware. That
1031 * we use CMD_802_11_AUTHENTICATE is misleading, this firmware
1032 * command doesn't send an authentication frame at all, it just
1033 * stores the auth_type.
1035 static int lbs_set_authtype(struct lbs_private *priv,
1036 struct cfg80211_connect_params *sme)
1038 struct cmd_ds_802_11_authenticate cmd;
1041 lbs_deb_enter_args(LBS_DEB_CFG80211, "%d", sme->auth_type);
1048 * BSS id 00 13 19 80 da 30
1050 * reserved 00 00 00 00 00 00 00 00 00 00
1052 memset(&cmd, 0, sizeof(cmd));
1053 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1055 memcpy(cmd.bssid, sme->bssid, ETH_ALEN);
1056 /* convert auth_type */
1057 ret = lbs_auth_to_authtype(sme->auth_type);
1062 ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
1065 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1071 * Create association request
1073 #define LBS_ASSOC_MAX_CMD_SIZE \
1074 (sizeof(struct cmd_ds_802_11_associate) \
1075 - 512 /* cmd_ds_802_11_associate.iebuf */ \
1076 + LBS_MAX_SSID_TLV_SIZE \
1077 + LBS_MAX_CHANNEL_TLV_SIZE \
1078 + LBS_MAX_CF_PARAM_TLV_SIZE \
1079 + LBS_MAX_AUTH_TYPE_TLV_SIZE \
1080 + LBS_MAX_WPA_TLV_SIZE)
1082 static int lbs_associate(struct lbs_private *priv,
1083 struct cfg80211_bss *bss,
1084 struct cfg80211_connect_params *sme)
1086 struct cmd_ds_802_11_associate_response *resp;
1087 struct cmd_ds_802_11_associate *cmd = kzalloc(LBS_ASSOC_MAX_CMD_SIZE,
1090 size_t len, resp_ie_len;
1093 u8 *pos = &(cmd->iebuf[0]);
1096 lbs_deb_enter(LBS_DEB_CFG80211);
1108 * BSS id 00 13 19 80 da 30
1109 * capabilities 11 00
1110 * listen interval 0a 00
1111 * beacon interval 00 00
1113 * TLVs xx (up to 512 bytes)
1115 cmd->hdr.command = cpu_to_le16(CMD_802_11_ASSOCIATE);
1117 /* Fill in static fields */
1118 memcpy(cmd->bssid, bss->bssid, ETH_ALEN);
1119 cmd->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
1120 cmd->capability = cpu_to_le16(bss->capability);
1123 ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
1125 pos += lbs_add_ssid_tlv(pos, ssid_eid + 2, ssid_eid[1]);
1127 lbs_deb_assoc("no SSID\n");
1129 /* add DS param TLV */
1131 pos += lbs_add_channel_tlv(pos, bss->channel->hw_value);
1133 lbs_deb_assoc("no channel\n");
1135 /* add (empty) CF param TLV */
1136 pos += lbs_add_cf_param_tlv(pos);
1139 tmp = pos + 4; /* skip Marvell IE header */
1140 pos += lbs_add_common_rates_tlv(pos, bss);
1141 lbs_deb_hex(LBS_DEB_ASSOC, "Common Rates", tmp, pos - tmp);
1143 /* add auth type TLV */
1144 if (MRVL_FW_MAJOR_REV(priv->fwrelease) >= 9)
1145 pos += lbs_add_auth_type_tlv(pos, sme->auth_type);
1147 /* add WPA/WPA2 TLV */
1148 if (sme->ie && sme->ie_len)
1149 pos += lbs_add_wpa_tlv(pos, sme->ie, sme->ie_len);
1151 len = (sizeof(*cmd) - sizeof(cmd->iebuf)) +
1152 (u16)(pos - (u8 *) &cmd->iebuf);
1153 cmd->hdr.size = cpu_to_le16(len);
1155 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_CMD", (u8 *) cmd,
1156 le16_to_cpu(cmd->hdr.size));
1158 /* store for later use */
1159 memcpy(priv->assoc_bss, bss->bssid, ETH_ALEN);
1161 ret = lbs_cmd_with_response(priv, CMD_802_11_ASSOCIATE, cmd);
1165 /* generate connect message to cfg80211 */
1167 resp = (void *) cmd; /* recast for easier field access */
1168 status = le16_to_cpu(resp->statuscode);
1170 /* Older FW versions map the IEEE 802.11 Status Code in the association
1171 * response to the following values returned in resp->statuscode:
1173 * IEEE Status Code Marvell Status Code
1174 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
1175 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1176 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1177 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1178 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1179 * others -> 0x0003 ASSOC_RESULT_REFUSED
1181 * Other response codes:
1182 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
1183 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
1184 * association response from the AP)
1186 if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8) {
1191 lbs_deb_assoc("invalid association parameters\n");
1192 status = WLAN_STATUS_CAPS_UNSUPPORTED;
1195 lbs_deb_assoc("timer expired while waiting for AP\n");
1196 status = WLAN_STATUS_AUTH_TIMEOUT;
1199 lbs_deb_assoc("association refused by AP\n");
1200 status = WLAN_STATUS_ASSOC_DENIED_UNSPEC;
1203 lbs_deb_assoc("authentication refused by AP\n");
1204 status = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
1207 lbs_deb_assoc("association failure %d\n", status);
1208 /* v5 OLPC firmware does return the AP status code if
1209 * it's not one of the values above. Let that through.
1215 lbs_deb_assoc("status %d, statuscode 0x%04x, capability 0x%04x, "
1216 "aid 0x%04x\n", status, le16_to_cpu(resp->statuscode),
1217 le16_to_cpu(resp->capability), le16_to_cpu(resp->aid));
1219 resp_ie_len = le16_to_cpu(resp->hdr.size)
1222 cfg80211_connect_result(priv->dev,
1224 sme->ie, sme->ie_len,
1225 resp->iebuf, resp_ie_len,
1230 /* TODO: get rid of priv->connect_status */
1231 priv->connect_status = LBS_CONNECTED;
1232 netif_carrier_on(priv->dev);
1233 if (!priv->tx_pending_len)
1234 netif_tx_wake_all_queues(priv->dev);
1238 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1242 static struct cfg80211_scan_request *
1243 _new_connect_scan_req(struct wiphy *wiphy, struct cfg80211_connect_params *sme)
1245 struct cfg80211_scan_request *creq = NULL;
1246 int i, n_channels = 0;
1247 enum ieee80211_band band;
1249 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1250 if (wiphy->bands[band])
1251 n_channels += wiphy->bands[band]->n_channels;
1254 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
1255 n_channels * sizeof(void *),
1260 /* SSIDs come after channels */
1261 creq->ssids = (void *)&creq->channels[n_channels];
1262 creq->n_channels = n_channels;
1265 /* Scan all available channels */
1267 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1270 if (!wiphy->bands[band])
1273 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
1274 /* ignore disabled channels */
1275 if (wiphy->bands[band]->channels[j].flags &
1276 IEEE80211_CHAN_DISABLED)
1279 creq->channels[i] = &wiphy->bands[band]->channels[j];
1284 /* Set real number of channels specified in creq->channels[] */
1285 creq->n_channels = i;
1287 /* Scan for the SSID we're going to connect to */
1288 memcpy(creq->ssids[0].ssid, sme->ssid, sme->ssid_len);
1289 creq->ssids[0].ssid_len = sme->ssid_len;
1291 /* No channels found... */
1299 static int lbs_cfg_connect(struct wiphy *wiphy, struct net_device *dev,
1300 struct cfg80211_connect_params *sme)
1302 struct lbs_private *priv = wiphy_priv(wiphy);
1303 struct cfg80211_bss *bss = NULL;
1305 u8 preamble = RADIO_PREAMBLE_SHORT;
1307 if (dev == priv->mesh_dev)
1310 lbs_deb_enter(LBS_DEB_CFG80211);
1313 struct cfg80211_scan_request *creq;
1316 * Scan for the requested network after waiting for existing
1319 lbs_deb_assoc("assoc: waiting for existing scans\n");
1320 wait_event_interruptible_timeout(priv->scan_q,
1321 (priv->scan_req == NULL),
1324 creq = _new_connect_scan_req(wiphy, sme);
1330 lbs_deb_assoc("assoc: scanning for compatible AP\n");
1331 _internal_start_scan(priv, true, creq);
1333 lbs_deb_assoc("assoc: waiting for scan to complete\n");
1334 wait_event_interruptible_timeout(priv->scan_q,
1335 (priv->scan_req == NULL),
1337 lbs_deb_assoc("assoc: scanning competed\n");
1340 /* Find the BSS we want using available scan results */
1341 bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
1342 sme->ssid, sme->ssid_len,
1343 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
1345 wiphy_err(wiphy, "assoc: bss %pM not in scan results\n",
1350 lbs_deb_assoc("trying %pM\n", bss->bssid);
1351 lbs_deb_assoc("cipher 0x%x, key index %d, key len %d\n",
1352 sme->crypto.cipher_group,
1353 sme->key_idx, sme->key_len);
1355 /* As this is a new connection, clear locally stored WEP keys */
1356 priv->wep_tx_key = 0;
1357 memset(priv->wep_key, 0, sizeof(priv->wep_key));
1358 memset(priv->wep_key_len, 0, sizeof(priv->wep_key_len));
1360 /* set/remove WEP keys */
1361 switch (sme->crypto.cipher_group) {
1362 case WLAN_CIPHER_SUITE_WEP40:
1363 case WLAN_CIPHER_SUITE_WEP104:
1364 /* Store provided WEP keys in priv-> */
1365 priv->wep_tx_key = sme->key_idx;
1366 priv->wep_key_len[sme->key_idx] = sme->key_len;
1367 memcpy(priv->wep_key[sme->key_idx], sme->key, sme->key_len);
1368 /* Set WEP keys and WEP mode */
1369 lbs_set_wep_keys(priv);
1370 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
1371 lbs_set_mac_control(priv);
1372 /* No RSN mode for WEP */
1373 lbs_enable_rsn(priv, 0);
1375 case 0: /* there's no WLAN_CIPHER_SUITE_NONE definition */
1377 * If we don't have no WEP, no WPA and no WPA2,
1378 * we remove all keys like in the WPA/WPA2 setup,
1379 * we just don't set RSN.
1381 * Therefore: fall-through
1383 case WLAN_CIPHER_SUITE_TKIP:
1384 case WLAN_CIPHER_SUITE_CCMP:
1385 /* Remove WEP keys and WEP mode */
1386 lbs_remove_wep_keys(priv);
1387 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
1388 lbs_set_mac_control(priv);
1390 /* clear the WPA/WPA2 keys */
1391 lbs_set_key_material(priv,
1392 KEY_TYPE_ID_WEP, /* doesn't matter */
1393 KEY_INFO_WPA_UNICAST,
1395 lbs_set_key_material(priv,
1396 KEY_TYPE_ID_WEP, /* doesn't matter */
1399 /* RSN mode for WPA/WPA2 */
1400 lbs_enable_rsn(priv, sme->crypto.cipher_group != 0);
1403 wiphy_err(wiphy, "unsupported cipher group 0x%x\n",
1404 sme->crypto.cipher_group);
1409 lbs_set_authtype(priv, sme);
1410 lbs_set_radio(priv, preamble, 1);
1412 /* Do the actual association */
1413 ret = lbs_associate(priv, bss, sme);
1417 cfg80211_put_bss(bss);
1418 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1422 int lbs_disconnect(struct lbs_private *priv, u16 reason)
1424 struct cmd_ds_802_11_deauthenticate cmd;
1427 memset(&cmd, 0, sizeof(cmd));
1428 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1429 /* Mildly ugly to use a locally store my own BSSID ... */
1430 memcpy(cmd.macaddr, &priv->assoc_bss, ETH_ALEN);
1431 cmd.reasoncode = cpu_to_le16(reason);
1433 ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
1437 cfg80211_disconnected(priv->dev,
1441 priv->connect_status = LBS_DISCONNECTED;
1446 static int lbs_cfg_disconnect(struct wiphy *wiphy, struct net_device *dev,
1449 struct lbs_private *priv = wiphy_priv(wiphy);
1451 if (dev == priv->mesh_dev)
1454 lbs_deb_enter_args(LBS_DEB_CFG80211, "reason_code %d", reason_code);
1456 /* store for lbs_cfg_ret_disconnect() */
1457 priv->disassoc_reason = reason_code;
1459 return lbs_disconnect(priv, reason_code);
1462 static int lbs_cfg_set_default_key(struct wiphy *wiphy,
1463 struct net_device *netdev,
1464 u8 key_index, bool unicast,
1467 struct lbs_private *priv = wiphy_priv(wiphy);
1469 if (netdev == priv->mesh_dev)
1472 lbs_deb_enter(LBS_DEB_CFG80211);
1474 if (key_index != priv->wep_tx_key) {
1475 lbs_deb_assoc("set_default_key: to %d\n", key_index);
1476 priv->wep_tx_key = key_index;
1477 lbs_set_wep_keys(priv);
1484 static int lbs_cfg_add_key(struct wiphy *wiphy, struct net_device *netdev,
1485 u8 idx, bool pairwise, const u8 *mac_addr,
1486 struct key_params *params)
1488 struct lbs_private *priv = wiphy_priv(wiphy);
1493 if (netdev == priv->mesh_dev)
1496 lbs_deb_enter(LBS_DEB_CFG80211);
1498 lbs_deb_assoc("add_key: cipher 0x%x, mac_addr %pM\n",
1499 params->cipher, mac_addr);
1500 lbs_deb_assoc("add_key: key index %d, key len %d\n",
1501 idx, params->key_len);
1502 if (params->key_len)
1503 lbs_deb_hex(LBS_DEB_CFG80211, "KEY",
1504 params->key, params->key_len);
1506 lbs_deb_assoc("add_key: seq len %d\n", params->seq_len);
1507 if (params->seq_len)
1508 lbs_deb_hex(LBS_DEB_CFG80211, "SEQ",
1509 params->seq, params->seq_len);
1511 switch (params->cipher) {
1512 case WLAN_CIPHER_SUITE_WEP40:
1513 case WLAN_CIPHER_SUITE_WEP104:
1514 /* actually compare if something has changed ... */
1515 if ((priv->wep_key_len[idx] != params->key_len) ||
1516 memcmp(priv->wep_key[idx],
1517 params->key, params->key_len) != 0) {
1518 priv->wep_key_len[idx] = params->key_len;
1519 memcpy(priv->wep_key[idx],
1520 params->key, params->key_len);
1521 lbs_set_wep_keys(priv);
1524 case WLAN_CIPHER_SUITE_TKIP:
1525 case WLAN_CIPHER_SUITE_CCMP:
1526 key_info = KEY_INFO_WPA_ENABLED | ((idx == 0)
1527 ? KEY_INFO_WPA_UNICAST
1528 : KEY_INFO_WPA_MCAST);
1529 key_type = (params->cipher == WLAN_CIPHER_SUITE_TKIP)
1532 lbs_set_key_material(priv,
1535 params->key, params->key_len);
1538 wiphy_err(wiphy, "unhandled cipher 0x%x\n", params->cipher);
1547 static int lbs_cfg_del_key(struct wiphy *wiphy, struct net_device *netdev,
1548 u8 key_index, bool pairwise, const u8 *mac_addr)
1551 lbs_deb_enter(LBS_DEB_CFG80211);
1553 lbs_deb_assoc("del_key: key_idx %d, mac_addr %pM\n",
1554 key_index, mac_addr);
1557 struct lbs_private *priv = wiphy_priv(wiphy);
1559 * I think can keep this a NO-OP, because:
1561 * - we clear all keys whenever we do lbs_cfg_connect() anyway
1562 * - neither "iw" nor "wpa_supplicant" won't call this during
1563 * an ongoing connection
1564 * - TODO: but I have to check if this is still true when
1565 * I set the AP to periodic re-keying
1566 * - we've not kzallec() something when we've added a key at
1567 * lbs_cfg_connect() or lbs_cfg_add_key().
1569 * This causes lbs_cfg_del_key() only called at disconnect time,
1570 * where we'd just waste time deleting a key that is not going
1571 * to be used anyway.
1573 if (key_index < 3 && priv->wep_key_len[key_index]) {
1574 priv->wep_key_len[key_index] = 0;
1575 lbs_set_wep_keys(priv);
1587 static int lbs_cfg_get_station(struct wiphy *wiphy, struct net_device *dev,
1588 u8 *mac, struct station_info *sinfo)
1590 struct lbs_private *priv = wiphy_priv(wiphy);
1595 lbs_deb_enter(LBS_DEB_CFG80211);
1597 sinfo->filled |= STATION_INFO_TX_BYTES |
1598 STATION_INFO_TX_PACKETS |
1599 STATION_INFO_RX_BYTES |
1600 STATION_INFO_RX_PACKETS;
1601 sinfo->tx_bytes = priv->dev->stats.tx_bytes;
1602 sinfo->tx_packets = priv->dev->stats.tx_packets;
1603 sinfo->rx_bytes = priv->dev->stats.rx_bytes;
1604 sinfo->rx_packets = priv->dev->stats.rx_packets;
1606 /* Get current RSSI */
1607 ret = lbs_get_rssi(priv, &signal, &noise);
1609 sinfo->signal = signal;
1610 sinfo->filled |= STATION_INFO_SIGNAL;
1613 /* Convert priv->cur_rate from hw_value to NL80211 value */
1614 for (i = 0; i < ARRAY_SIZE(lbs_rates); i++) {
1615 if (priv->cur_rate == lbs_rates[i].hw_value) {
1616 sinfo->txrate.legacy = lbs_rates[i].bitrate;
1617 sinfo->filled |= STATION_INFO_TX_BITRATE;
1629 * "Site survey", here just current channel and noise level
1632 static int lbs_get_survey(struct wiphy *wiphy, struct net_device *dev,
1633 int idx, struct survey_info *survey)
1635 struct lbs_private *priv = wiphy_priv(wiphy);
1639 if (dev == priv->mesh_dev)
1645 lbs_deb_enter(LBS_DEB_CFG80211);
1647 survey->channel = ieee80211_get_channel(wiphy,
1648 ieee80211_channel_to_frequency(priv->channel,
1649 IEEE80211_BAND_2GHZ));
1651 ret = lbs_get_rssi(priv, &signal, &noise);
1653 survey->filled = SURVEY_INFO_NOISE_DBM;
1654 survey->noise = noise;
1657 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1668 static int lbs_change_intf(struct wiphy *wiphy, struct net_device *dev,
1669 enum nl80211_iftype type, u32 *flags,
1670 struct vif_params *params)
1672 struct lbs_private *priv = wiphy_priv(wiphy);
1675 if (dev == priv->mesh_dev)
1679 case NL80211_IFTYPE_MONITOR:
1680 case NL80211_IFTYPE_STATION:
1681 case NL80211_IFTYPE_ADHOC:
1687 lbs_deb_enter(LBS_DEB_CFG80211);
1689 if (priv->iface_running)
1690 ret = lbs_set_iface_type(priv, type);
1693 priv->wdev->iftype = type;
1695 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1706 * The firmware needs the following bits masked out of the beacon-derived
1707 * capability field when associating/joining to a BSS:
1708 * 9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
1710 #define CAPINFO_MASK (~(0xda00))
1713 static void lbs_join_post(struct lbs_private *priv,
1714 struct cfg80211_ibss_params *params,
1715 u8 *bssid, u16 capability)
1717 u8 fake_ie[2 + IEEE80211_MAX_SSID_LEN + /* ssid */
1718 2 + 4 + /* basic rates */
1719 2 + 1 + /* DS parameter */
1721 2 + 8]; /* extended rates */
1724 lbs_deb_enter(LBS_DEB_CFG80211);
1727 * For cfg80211_inform_bss, we'll need a fake IE, as we can't get
1728 * the real IE from the firmware. So we fabricate a fake IE based on
1729 * what the firmware actually sends (sniffed with wireshark).
1732 *fake++ = WLAN_EID_SSID;
1733 *fake++ = params->ssid_len;
1734 memcpy(fake, params->ssid, params->ssid_len);
1735 fake += params->ssid_len;
1736 /* Fake supported basic rates IE */
1737 *fake++ = WLAN_EID_SUPP_RATES;
1743 /* Fake DS channel IE */
1744 *fake++ = WLAN_EID_DS_PARAMS;
1746 *fake++ = params->channel->hw_value;
1747 /* Fake IBSS params IE */
1748 *fake++ = WLAN_EID_IBSS_PARAMS;
1750 *fake++ = 0; /* ATIM=0 */
1752 /* Fake extended rates IE, TODO: don't add this for 802.11b only,
1753 * but I don't know how this could be checked */
1754 *fake++ = WLAN_EID_EXT_SUPP_RATES;
1764 lbs_deb_hex(LBS_DEB_CFG80211, "IE", fake_ie, fake - fake_ie);
1766 cfg80211_inform_bss(priv->wdev->wiphy,
1771 params->beacon_interval,
1772 fake_ie, fake - fake_ie,
1775 memcpy(priv->wdev->ssid, params->ssid, params->ssid_len);
1776 priv->wdev->ssid_len = params->ssid_len;
1778 cfg80211_ibss_joined(priv->dev, bssid, GFP_KERNEL);
1780 /* TODO: consider doing this at MACREG_INT_CODE_LINK_SENSED time */
1781 priv->connect_status = LBS_CONNECTED;
1782 netif_carrier_on(priv->dev);
1783 if (!priv->tx_pending_len)
1784 netif_wake_queue(priv->dev);
1786 lbs_deb_leave(LBS_DEB_CFG80211);
1789 static int lbs_ibss_join_existing(struct lbs_private *priv,
1790 struct cfg80211_ibss_params *params,
1791 struct cfg80211_bss *bss)
1793 const u8 *rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1794 struct cmd_ds_802_11_ad_hoc_join cmd;
1795 u8 preamble = RADIO_PREAMBLE_SHORT;
1798 lbs_deb_enter(LBS_DEB_CFG80211);
1800 /* TODO: set preamble based on scan result */
1801 ret = lbs_set_radio(priv, preamble, 1);
1806 * Example CMD_802_11_AD_HOC_JOIN command:
1808 * command 2c 00 CMD_802_11_AD_HOC_JOIN
1812 * bssid 02 27 27 97 2f 96
1813 * ssid 49 42 53 53 00 00 00 00
1814 * 00 00 00 00 00 00 00 00
1815 * 00 00 00 00 00 00 00 00
1816 * 00 00 00 00 00 00 00 00
1817 * type 02 CMD_BSS_TYPE_IBSS
1818 * beacon period 64 00
1820 * timestamp 00 00 00 00 00 00 00 00
1821 * localtime 00 00 00 00 00 00 00 00
1825 * reserveed 00 00 00 00
1828 * IE IBSS atim 00 00
1829 * reserved 00 00 00 00
1831 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c 00
1832 * fail timeout ff 00
1835 memset(&cmd, 0, sizeof(cmd));
1836 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1838 memcpy(cmd.bss.bssid, bss->bssid, ETH_ALEN);
1839 memcpy(cmd.bss.ssid, params->ssid, params->ssid_len);
1840 cmd.bss.type = CMD_BSS_TYPE_IBSS;
1841 cmd.bss.beaconperiod = cpu_to_le16(params->beacon_interval);
1842 cmd.bss.ds.header.id = WLAN_EID_DS_PARAMS;
1843 cmd.bss.ds.header.len = 1;
1844 cmd.bss.ds.channel = params->channel->hw_value;
1845 cmd.bss.ibss.header.id = WLAN_EID_IBSS_PARAMS;
1846 cmd.bss.ibss.header.len = 2;
1847 cmd.bss.ibss.atimwindow = 0;
1848 cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
1850 /* set rates to the intersection of our rates and the rates in the
1853 lbs_add_rates(cmd.bss.rates);
1856 u8 rates_max = rates_eid[1];
1857 u8 *rates = cmd.bss.rates;
1858 for (hw = 0; hw < ARRAY_SIZE(lbs_rates); hw++) {
1859 u8 hw_rate = lbs_rates[hw].bitrate / 5;
1860 for (i = 0; i < rates_max; i++) {
1861 if (hw_rate == (rates_eid[i+2] & 0x7f)) {
1862 u8 rate = rates_eid[i+2];
1863 if (rate == 0x02 || rate == 0x04 ||
1864 rate == 0x0b || rate == 0x16)
1872 /* Only v8 and below support setting this */
1873 if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8) {
1874 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
1875 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1877 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
1882 * This is a sample response to CMD_802_11_AD_HOC_JOIN:
1890 lbs_join_post(priv, params, bss->bssid, bss->capability);
1893 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1899 static int lbs_ibss_start_new(struct lbs_private *priv,
1900 struct cfg80211_ibss_params *params)
1902 struct cmd_ds_802_11_ad_hoc_start cmd;
1903 struct cmd_ds_802_11_ad_hoc_result *resp =
1904 (struct cmd_ds_802_11_ad_hoc_result *) &cmd;
1905 u8 preamble = RADIO_PREAMBLE_SHORT;
1909 lbs_deb_enter(LBS_DEB_CFG80211);
1911 ret = lbs_set_radio(priv, preamble, 1);
1916 * Example CMD_802_11_AD_HOC_START command:
1918 * command 2b 00 CMD_802_11_AD_HOC_START
1922 * ssid 54 45 53 54 00 00 00 00
1923 * 00 00 00 00 00 00 00 00
1924 * 00 00 00 00 00 00 00 00
1925 * 00 00 00 00 00 00 00 00
1927 * beacon period 64 00
1931 * IE IBSS atim 00 00
1932 * reserved 00 00 00 00
1936 * reserved 00 00 00 00
1939 * rates 82 84 8b 96 (basic rates with have bit 7 set)
1940 * 0c 12 18 24 30 48 60 6c
1943 memset(&cmd, 0, sizeof(cmd));
1944 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1945 memcpy(cmd.ssid, params->ssid, params->ssid_len);
1946 cmd.bsstype = CMD_BSS_TYPE_IBSS;
1947 cmd.beaconperiod = cpu_to_le16(params->beacon_interval);
1948 cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
1949 cmd.ibss.header.len = 2;
1950 cmd.ibss.atimwindow = 0;
1951 cmd.ds.header.id = WLAN_EID_DS_PARAMS;
1952 cmd.ds.header.len = 1;
1953 cmd.ds.channel = params->channel->hw_value;
1954 /* Only v8 and below support setting probe delay */
1955 if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8)
1956 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1957 /* TODO: mix in WLAN_CAPABILITY_PRIVACY */
1958 capability = WLAN_CAPABILITY_IBSS;
1959 cmd.capability = cpu_to_le16(capability);
1960 lbs_add_rates(cmd.rates);
1963 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
1968 * This is a sample response to CMD_802_11_AD_HOC_JOIN:
1975 * bssid 02 2b 7b 0f 86 0e
1977 lbs_join_post(priv, params, resp->bssid, capability);
1980 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1985 static int lbs_join_ibss(struct wiphy *wiphy, struct net_device *dev,
1986 struct cfg80211_ibss_params *params)
1988 struct lbs_private *priv = wiphy_priv(wiphy);
1990 struct cfg80211_bss *bss;
1991 DECLARE_SSID_BUF(ssid_buf);
1993 if (dev == priv->mesh_dev)
1996 lbs_deb_enter(LBS_DEB_CFG80211);
1998 if (!params->channel) {
2003 ret = lbs_set_channel(priv, params->channel->hw_value);
2007 /* Search if someone is beaconing. This assumes that the
2008 * bss list is populated already */
2009 bss = cfg80211_get_bss(wiphy, params->channel, params->bssid,
2010 params->ssid, params->ssid_len,
2011 WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS);
2014 ret = lbs_ibss_join_existing(priv, params, bss);
2015 cfg80211_put_bss(bss);
2017 ret = lbs_ibss_start_new(priv, params);
2021 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
2026 static int lbs_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2028 struct lbs_private *priv = wiphy_priv(wiphy);
2029 struct cmd_ds_802_11_ad_hoc_stop cmd;
2032 if (dev == priv->mesh_dev)
2035 lbs_deb_enter(LBS_DEB_CFG80211);
2037 memset(&cmd, 0, sizeof(cmd));
2038 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
2039 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
2041 /* TODO: consider doing this at MACREG_INT_CODE_ADHOC_BCN_LOST time */
2042 lbs_mac_event_disconnected(priv);
2044 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
2055 static struct cfg80211_ops lbs_cfg80211_ops = {
2056 .set_channel = lbs_cfg_set_channel,
2057 .scan = lbs_cfg_scan,
2058 .connect = lbs_cfg_connect,
2059 .disconnect = lbs_cfg_disconnect,
2060 .add_key = lbs_cfg_add_key,
2061 .del_key = lbs_cfg_del_key,
2062 .set_default_key = lbs_cfg_set_default_key,
2063 .get_station = lbs_cfg_get_station,
2064 .dump_survey = lbs_get_survey,
2065 .change_virtual_intf = lbs_change_intf,
2066 .join_ibss = lbs_join_ibss,
2067 .leave_ibss = lbs_leave_ibss,
2072 * At this time lbs_private *priv doesn't even exist, so we just allocate
2073 * memory and don't initialize the wiphy further. This is postponed until we
2074 * can talk to the firmware and happens at registration time in
2075 * lbs_cfg_wiphy_register().
2077 struct wireless_dev *lbs_cfg_alloc(struct device *dev)
2080 struct wireless_dev *wdev;
2082 lbs_deb_enter(LBS_DEB_CFG80211);
2084 wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
2086 dev_err(dev, "cannot allocate wireless device\n");
2087 return ERR_PTR(-ENOMEM);
2090 wdev->wiphy = wiphy_new(&lbs_cfg80211_ops, sizeof(struct lbs_private));
2092 dev_err(dev, "cannot allocate wiphy\n");
2097 lbs_deb_leave(LBS_DEB_CFG80211);
2102 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
2103 return ERR_PTR(ret);
2107 static void lbs_cfg_set_regulatory_hint(struct lbs_private *priv)
2109 struct region_code_mapping {
2114 /* Section 5.17.2 */
2115 static const struct region_code_mapping regmap[] = {
2116 {"US ", 0x10}, /* US FCC */
2117 {"CA ", 0x20}, /* Canada */
2118 {"EU ", 0x30}, /* ETSI */
2119 {"ES ", 0x31}, /* Spain */
2120 {"FR ", 0x32}, /* France */
2121 {"JP ", 0x40}, /* Japan */
2125 lbs_deb_enter(LBS_DEB_CFG80211);
2127 for (i = 0; i < ARRAY_SIZE(regmap); i++)
2128 if (regmap[i].code == priv->regioncode) {
2129 regulatory_hint(priv->wdev->wiphy, regmap[i].cn);
2133 lbs_deb_leave(LBS_DEB_CFG80211);
2138 * This function get's called after lbs_setup_firmware() determined the
2139 * firmware capabities. So we can setup the wiphy according to our
2140 * hardware/firmware.
2142 int lbs_cfg_register(struct lbs_private *priv)
2144 struct wireless_dev *wdev = priv->wdev;
2147 lbs_deb_enter(LBS_DEB_CFG80211);
2149 wdev->wiphy->max_scan_ssids = 1;
2150 wdev->wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
2152 wdev->wiphy->interface_modes =
2153 BIT(NL80211_IFTYPE_STATION) |
2154 BIT(NL80211_IFTYPE_ADHOC);
2155 if (lbs_rtap_supported(priv))
2156 wdev->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
2157 if (lbs_mesh_activated(priv))
2158 wdev->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MESH_POINT);
2160 wdev->wiphy->bands[IEEE80211_BAND_2GHZ] = &lbs_band_2ghz;
2163 * We could check priv->fwcapinfo && FW_CAPINFO_WPA, but I have
2164 * never seen a firmware without WPA
2166 wdev->wiphy->cipher_suites = cipher_suites;
2167 wdev->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
2168 wdev->wiphy->reg_notifier = lbs_reg_notifier;
2170 ret = wiphy_register(wdev->wiphy);
2172 pr_err("cannot register wiphy device\n");
2174 priv->wiphy_registered = true;
2176 ret = register_netdev(priv->dev);
2178 pr_err("cannot register network device\n");
2180 INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
2182 lbs_cfg_set_regulatory_hint(priv);
2184 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
2188 int lbs_reg_notifier(struct wiphy *wiphy,
2189 struct regulatory_request *request)
2191 struct lbs_private *priv = wiphy_priv(wiphy);
2194 lbs_deb_enter_args(LBS_DEB_CFG80211, "cfg80211 regulatory domain "
2195 "callback for domain %c%c\n", request->alpha2[0],
2196 request->alpha2[1]);
2198 ret = lbs_set_11d_domain_info(priv, request, wiphy->bands);
2200 lbs_deb_leave(LBS_DEB_CFG80211);
2204 void lbs_scan_deinit(struct lbs_private *priv)
2206 lbs_deb_enter(LBS_DEB_CFG80211);
2207 cancel_delayed_work_sync(&priv->scan_work);
2211 void lbs_cfg_free(struct lbs_private *priv)
2213 struct wireless_dev *wdev = priv->wdev;
2215 lbs_deb_enter(LBS_DEB_CFG80211);
2220 if (priv->wiphy_registered)
2221 wiphy_unregister(wdev->wiphy);
2224 wiphy_free(wdev->wiphy);