1 // SPDX-License-Identifier: GPL-2.0
3 * Implement cfg80211 ("iw") support.
5 * Copyright (C) 2009 M&N Solutions GmbH, 61191 Rosbach, Germany
6 * Holger Schurig <hs4233@mail.mn-solutions.de>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/hardirq.h>
13 #include <linux/sched.h>
14 #include <linux/wait.h>
15 #include <linux/slab.h>
16 #include <linux/ieee80211.h>
17 #include <net/cfg80211.h>
18 #include <asm/unaligned.h>
26 #define CHAN2G(_channel, _freq, _flags) { \
27 .band = NL80211_BAND_2GHZ, \
28 .center_freq = (_freq), \
29 .hw_value = (_channel), \
31 .max_antenna_gain = 0, \
35 static struct ieee80211_channel lbs_2ghz_channels
[] = {
52 #define RATETAB_ENT(_rate, _hw_value, _flags) { \
54 .hw_value = (_hw_value), \
59 /* Table 6 in section 3.2.1.1 */
60 static struct ieee80211_rate lbs_rates
[] = {
61 RATETAB_ENT(10, 0, 0),
62 RATETAB_ENT(20, 1, 0),
63 RATETAB_ENT(55, 2, 0),
64 RATETAB_ENT(110, 3, 0),
65 RATETAB_ENT(60, 9, 0),
66 RATETAB_ENT(90, 6, 0),
67 RATETAB_ENT(120, 7, 0),
68 RATETAB_ENT(180, 8, 0),
69 RATETAB_ENT(240, 9, 0),
70 RATETAB_ENT(360, 10, 0),
71 RATETAB_ENT(480, 11, 0),
72 RATETAB_ENT(540, 12, 0),
75 static struct ieee80211_supported_band lbs_band_2ghz
= {
76 .channels
= lbs_2ghz_channels
,
77 .n_channels
= ARRAY_SIZE(lbs_2ghz_channels
),
78 .bitrates
= lbs_rates
,
79 .n_bitrates
= ARRAY_SIZE(lbs_rates
),
83 static const u32 cipher_suites
[] = {
84 WLAN_CIPHER_SUITE_WEP40
,
85 WLAN_CIPHER_SUITE_WEP104
,
86 WLAN_CIPHER_SUITE_TKIP
,
87 WLAN_CIPHER_SUITE_CCMP
,
90 /* Time to stay on the channel */
91 #define LBS_DWELL_PASSIVE 100
92 #define LBS_DWELL_ACTIVE 40
95 /***************************************************************************
96 * Misc utility functions
98 * TLVs are Marvell specific. They are very similar to IEs, they have the
99 * same structure: type, length, data*. The only difference: for IEs, the
100 * type and length are u8, but for TLVs they're __le16.
104 * Convert NL80211's auth_type to the one from Libertas, see chapter 5.9.1
105 * in the firmware spec
107 static int lbs_auth_to_authtype(enum nl80211_auth_type auth_type
)
112 case NL80211_AUTHTYPE_OPEN_SYSTEM
:
113 case NL80211_AUTHTYPE_SHARED_KEY
:
116 case NL80211_AUTHTYPE_AUTOMATIC
:
117 ret
= NL80211_AUTHTYPE_OPEN_SYSTEM
;
119 case NL80211_AUTHTYPE_NETWORK_EAP
:
123 /* silence compiler */
131 * Various firmware commands need the list of supported rates, but with
132 * the hight-bit set for basic rates
134 static int lbs_add_rates(u8
*rates
)
138 for (i
= 0; i
< ARRAY_SIZE(lbs_rates
); i
++) {
139 u8 rate
= lbs_rates
[i
].bitrate
/ 5;
140 if (rate
== 0x02 || rate
== 0x04 ||
141 rate
== 0x0b || rate
== 0x16)
145 return ARRAY_SIZE(lbs_rates
);
149 /***************************************************************************
150 * TLV utility functions
152 * TLVs are Marvell specific. They are very similar to IEs, they have the
153 * same structure: type, length, data*. The only difference: for IEs, the
154 * type and length are u8, but for TLVs they're __le16.
161 #define LBS_MAX_SSID_TLV_SIZE \
162 (sizeof(struct mrvl_ie_header) \
163 + IEEE80211_MAX_SSID_LEN)
165 static int lbs_add_ssid_tlv(u8
*tlv
, const u8
*ssid
, int ssid_len
)
167 struct mrvl_ie_ssid_param_set
*ssid_tlv
= (void *)tlv
;
172 * ssid 4d 4e 54 45 53 54
174 ssid_tlv
->header
.type
= cpu_to_le16(TLV_TYPE_SSID
);
175 ssid_tlv
->header
.len
= cpu_to_le16(ssid_len
);
176 memcpy(ssid_tlv
->ssid
, ssid
, ssid_len
);
177 return sizeof(ssid_tlv
->header
) + ssid_len
;
182 * Add channel list TLV (section 8.4.2)
184 * Actual channel data comes from priv->wdev->wiphy->channels.
186 #define LBS_MAX_CHANNEL_LIST_TLV_SIZE \
187 (sizeof(struct mrvl_ie_header) \
188 + (LBS_SCAN_BEFORE_NAP * sizeof(struct chanscanparamset)))
190 static int lbs_add_channel_list_tlv(struct lbs_private
*priv
, u8
*tlv
,
191 int last_channel
, int active_scan
)
193 int chanscanparamsize
= sizeof(struct chanscanparamset
) *
194 (last_channel
- priv
->scan_channel
);
196 struct mrvl_ie_header
*header
= (void *) tlv
;
199 * TLV-ID CHANLIST 01 01
201 * channel 00 01 00 00 00 64 00
205 * min scan time 00 00
206 * max scan time 64 00
207 * channel 2 00 02 00 00 00 64 00
211 header
->type
= cpu_to_le16(TLV_TYPE_CHANLIST
);
212 header
->len
= cpu_to_le16(chanscanparamsize
);
213 tlv
+= sizeof(struct mrvl_ie_header
);
215 /* lbs_deb_scan("scan: channels %d to %d\n", priv->scan_channel,
217 memset(tlv
, 0, chanscanparamsize
);
219 while (priv
->scan_channel
< last_channel
) {
220 struct chanscanparamset
*param
= (void *) tlv
;
222 param
->radiotype
= CMD_SCAN_RADIO_TYPE_BG
;
224 priv
->scan_req
->channels
[priv
->scan_channel
]->hw_value
;
226 param
->maxscantime
= cpu_to_le16(LBS_DWELL_ACTIVE
);
228 param
->chanscanmode
.passivescan
= 1;
229 param
->maxscantime
= cpu_to_le16(LBS_DWELL_PASSIVE
);
231 tlv
+= sizeof(struct chanscanparamset
);
232 priv
->scan_channel
++;
234 return sizeof(struct mrvl_ie_header
) + chanscanparamsize
;
241 * The rates are in lbs_bg_rates[], but for the 802.11b
242 * rates the high bit is set. We add this TLV only because
243 * there's a firmware which otherwise doesn't report all
246 #define LBS_MAX_RATES_TLV_SIZE \
247 (sizeof(struct mrvl_ie_header) \
248 + (ARRAY_SIZE(lbs_rates)))
250 /* Adds a TLV with all rates the hardware supports */
251 static int lbs_add_supported_rates_tlv(u8
*tlv
)
254 struct mrvl_ie_rates_param_set
*rate_tlv
= (void *)tlv
;
259 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
261 rate_tlv
->header
.type
= cpu_to_le16(TLV_TYPE_RATES
);
262 tlv
+= sizeof(rate_tlv
->header
);
263 i
= lbs_add_rates(tlv
);
265 rate_tlv
->header
.len
= cpu_to_le16(i
);
266 return sizeof(rate_tlv
->header
) + i
;
269 /* Add common rates from a TLV and return the new end of the TLV */
271 add_ie_rates(u8
*tlv
, const u8
*ie
, int *nrates
)
273 int hw
, ap
, ap_max
= ie
[1];
276 /* Advance past IE header */
279 lbs_deb_hex(LBS_DEB_ASSOC
, "AP IE Rates", (u8
*) ie
, ap_max
);
281 for (hw
= 0; hw
< ARRAY_SIZE(lbs_rates
); hw
++) {
282 hw_rate
= lbs_rates
[hw
].bitrate
/ 5;
283 for (ap
= 0; ap
< ap_max
; ap
++) {
284 if (hw_rate
== (ie
[ap
] & 0x7f)) {
286 *nrates
= *nrates
+ 1;
294 * Adds a TLV with all rates the hardware *and* BSS supports.
296 static int lbs_add_common_rates_tlv(u8
*tlv
, struct cfg80211_bss
*bss
)
298 struct mrvl_ie_rates_param_set
*rate_tlv
= (void *)tlv
;
299 const u8
*rates_eid
, *ext_rates_eid
;
303 rates_eid
= ieee80211_bss_get_ie(bss
, WLAN_EID_SUPP_RATES
);
304 ext_rates_eid
= ieee80211_bss_get_ie(bss
, WLAN_EID_EXT_SUPP_RATES
);
307 * 01 00 TLV_TYPE_RATES
311 rate_tlv
->header
.type
= cpu_to_le16(TLV_TYPE_RATES
);
312 tlv
+= sizeof(rate_tlv
->header
);
314 /* Add basic rates */
316 tlv
= add_ie_rates(tlv
, rates_eid
, &n
);
318 /* Add extended rates, if any */
320 tlv
= add_ie_rates(tlv
, ext_rates_eid
, &n
);
322 lbs_deb_assoc("assoc: bss had no basic rate IE\n");
323 /* Fallback: add basic 802.11b rates */
332 rate_tlv
->header
.len
= cpu_to_le16(n
);
333 return sizeof(rate_tlv
->header
) + n
;
340 * This is only needed for newer firmware (V9 and up).
342 #define LBS_MAX_AUTH_TYPE_TLV_SIZE \
343 sizeof(struct mrvl_ie_auth_type)
345 static int lbs_add_auth_type_tlv(u8
*tlv
, enum nl80211_auth_type auth_type
)
347 struct mrvl_ie_auth_type
*auth
= (void *) tlv
;
350 * 1f 01 TLV_TYPE_AUTH_TYPE
354 auth
->header
.type
= cpu_to_le16(TLV_TYPE_AUTH_TYPE
);
355 auth
->header
.len
= cpu_to_le16(sizeof(*auth
)-sizeof(auth
->header
));
356 auth
->auth
= cpu_to_le16(lbs_auth_to_authtype(auth_type
));
357 return sizeof(*auth
);
362 * Add channel (phy ds) TLV
364 #define LBS_MAX_CHANNEL_TLV_SIZE \
365 sizeof(struct mrvl_ie_header)
367 static int lbs_add_channel_tlv(u8
*tlv
, u8 channel
)
369 struct mrvl_ie_ds_param_set
*ds
= (void *) tlv
;
372 * 03 00 TLV_TYPE_PHY_DS
376 ds
->header
.type
= cpu_to_le16(TLV_TYPE_PHY_DS
);
377 ds
->header
.len
= cpu_to_le16(sizeof(*ds
)-sizeof(ds
->header
));
378 ds
->channel
= channel
;
384 * Add (empty) CF param TLV of the form:
386 #define LBS_MAX_CF_PARAM_TLV_SIZE \
387 sizeof(struct mrvl_ie_header)
389 static int lbs_add_cf_param_tlv(u8
*tlv
)
391 struct mrvl_ie_cf_param_set
*cf
= (void *)tlv
;
398 * 00 00 cfpmaxduration
399 * 00 00 cfpdurationremaining
401 cf
->header
.type
= cpu_to_le16(TLV_TYPE_CF
);
402 cf
->header
.len
= cpu_to_le16(sizeof(*cf
)-sizeof(cf
->header
));
409 #define LBS_MAX_WPA_TLV_SIZE \
410 (sizeof(struct mrvl_ie_header) \
411 + 128 /* TODO: I guessed the size */)
413 static int lbs_add_wpa_tlv(u8
*tlv
, const u8
*ie
, u8 ie_len
)
418 * We need just convert an IE to an TLV. IEs use u8 for the header,
422 * but TLVs use __le16 instead:
429 tlv_len
= *tlv
++ = *ie
++;
433 /* the TLV is two bytes larger than the IE */
441 static int lbs_cfg_set_monitor_channel(struct wiphy
*wiphy
,
442 struct cfg80211_chan_def
*chandef
)
444 struct lbs_private
*priv
= wiphy_priv(wiphy
);
447 if (cfg80211_get_chandef_type(chandef
) != NL80211_CHAN_NO_HT
)
450 ret
= lbs_set_channel(priv
, chandef
->chan
->hw_value
);
456 static int lbs_cfg_set_mesh_channel(struct wiphy
*wiphy
,
457 struct net_device
*netdev
,
458 struct ieee80211_channel
*channel
)
460 struct lbs_private
*priv
= wiphy_priv(wiphy
);
463 if (netdev
!= priv
->mesh_dev
)
466 ret
= lbs_mesh_set_channel(priv
, channel
->hw_value
);
479 * When scanning, the firmware doesn't send a nul packet with the power-safe
480 * bit to the AP. So we cannot stay away from our current channel too long,
481 * otherwise we loose data. So take a "nap" while scanning every other
484 #define LBS_SCAN_BEFORE_NAP 4
488 * When the firmware reports back a scan-result, it gives us an "u8 rssi",
489 * which isn't really an RSSI, as it becomes larger when moving away from
490 * the AP. Anyway, we need to convert that into mBm.
492 #define LBS_SCAN_RSSI_TO_MBM(rssi) \
493 ((-(int)rssi + 3)*100)
495 static int lbs_ret_scan(struct lbs_private
*priv
, unsigned long dummy
,
496 struct cmd_header
*resp
)
498 struct cfg80211_bss
*bss
;
499 struct cmd_ds_802_11_scan_rsp
*scanresp
= (void *)resp
;
507 bsssize
= get_unaligned_le16(&scanresp
->bssdescriptsize
);
509 lbs_deb_scan("scan response: %d BSSs (%d bytes); resp size %d bytes\n",
510 scanresp
->nr_sets
, bsssize
, le16_to_cpu(resp
->size
));
512 if (scanresp
->nr_sets
== 0) {
518 * The general layout of the scan response is described in chapter
519 * 5.7.1. Basically we have a common part, then any number of BSS
520 * descriptor sections. Finally we have section with the same number
523 * cmd_ds_802_11_scan_rsp
536 * MrvlIEtypes_TsfFimestamp_t
542 pos
= scanresp
->bssdesc_and_tlvbuffer
;
544 lbs_deb_hex(LBS_DEB_SCAN
, "SCAN_RSP", scanresp
->bssdesc_and_tlvbuffer
,
545 scanresp
->bssdescriptsize
);
547 tsfdesc
= pos
+ bsssize
;
548 tsfsize
= 4 + 8 * scanresp
->nr_sets
;
549 lbs_deb_hex(LBS_DEB_SCAN
, "SCAN_TSF", (u8
*) tsfdesc
, tsfsize
);
551 /* Validity check: we expect a Marvell-Local TLV */
552 i
= get_unaligned_le16(tsfdesc
);
554 if (i
!= TLV_TYPE_TSFTIMESTAMP
) {
555 lbs_deb_scan("scan response: invalid TSF Timestamp %d\n", i
);
560 * Validity check: the TLV holds TSF values with 8 bytes each, so
561 * the size in the TLV must match the nr_sets value
563 i
= get_unaligned_le16(tsfdesc
);
565 if (i
/ 8 != scanresp
->nr_sets
) {
566 lbs_deb_scan("scan response: invalid number of TSF timestamp "
567 "sets (expected %d got %d)\n", scanresp
->nr_sets
,
572 for (i
= 0; i
< scanresp
->nr_sets
; i
++) {
581 const u8
*ssid
= NULL
;
584 int len
= get_unaligned_le16(pos
);
592 /* Packet time stamp */
594 /* Beacon interval */
595 intvl
= get_unaligned_le16(pos
);
598 capa
= get_unaligned_le16(pos
);
601 /* To find out the channel, we must parse the IEs */
604 * 6+1+8+2+2: size of BSSID, RSSI, time stamp, beacon
605 * interval, capabilities
607 ielen
= left
= len
- (6 + 1 + 8 + 2 + 2);
614 lbs_deb_scan("scan response: invalid IE fmt\n");
618 if (id
== WLAN_EID_DS_PARAMS
)
620 if (id
== WLAN_EID_SSID
) {
628 /* No channel, no luck */
630 struct wiphy
*wiphy
= priv
->wdev
->wiphy
;
631 int freq
= ieee80211_channel_to_frequency(chan_no
,
633 struct ieee80211_channel
*channel
=
634 ieee80211_get_channel(wiphy
, freq
);
636 lbs_deb_scan("scan: %pM, capa %04x, chan %2d, %*pE, %d dBm\n",
637 bssid
, capa
, chan_no
, ssid_len
, ssid
,
638 LBS_SCAN_RSSI_TO_MBM(rssi
)/100);
641 !(channel
->flags
& IEEE80211_CHAN_DISABLED
)) {
642 bss
= cfg80211_inform_bss(wiphy
, channel
,
643 CFG80211_BSS_FTYPE_UNKNOWN
,
644 bssid
, get_unaligned_le64(tsfdesc
),
645 capa
, intvl
, ie
, ielen
,
646 LBS_SCAN_RSSI_TO_MBM(rssi
),
648 cfg80211_put_bss(wiphy
, bss
);
651 lbs_deb_scan("scan response: missing BSS channel IE\n");
663 * Our scan command contains a TLV, consting of a SSID TLV, a channel list
664 * TLV and a rates TLV. Determine the maximum size of them:
666 #define LBS_SCAN_MAX_CMD_SIZE \
667 (sizeof(struct cmd_ds_802_11_scan) \
668 + LBS_MAX_SSID_TLV_SIZE \
669 + LBS_MAX_CHANNEL_LIST_TLV_SIZE \
670 + LBS_MAX_RATES_TLV_SIZE)
673 * Assumes priv->scan_req is initialized and valid
674 * Assumes priv->scan_channel is initialized
676 static void lbs_scan_worker(struct work_struct
*work
)
678 struct lbs_private
*priv
=
679 container_of(work
, struct lbs_private
, scan_work
.work
);
680 struct cmd_ds_802_11_scan
*scan_cmd
;
681 u8
*tlv
; /* pointer into our current, growing TLV storage area */
683 int running
, carrier
;
685 scan_cmd
= kzalloc(LBS_SCAN_MAX_CMD_SIZE
, GFP_KERNEL
);
686 if (scan_cmd
== NULL
)
689 /* prepare fixed part of scan command */
690 scan_cmd
->bsstype
= CMD_BSS_TYPE_ANY
;
692 /* stop network while we're away from our main channel */
693 running
= !netif_queue_stopped(priv
->dev
);
694 carrier
= netif_carrier_ok(priv
->dev
);
696 netif_stop_queue(priv
->dev
);
698 netif_carrier_off(priv
->dev
);
700 /* prepare fixed part of scan command */
701 tlv
= scan_cmd
->tlvbuffer
;
704 if (priv
->scan_req
->n_ssids
&& priv
->scan_req
->ssids
[0].ssid_len
> 0)
705 tlv
+= lbs_add_ssid_tlv(tlv
,
706 priv
->scan_req
->ssids
[0].ssid
,
707 priv
->scan_req
->ssids
[0].ssid_len
);
709 /* add channel TLVs */
710 last_channel
= priv
->scan_channel
+ LBS_SCAN_BEFORE_NAP
;
711 if (last_channel
> priv
->scan_req
->n_channels
)
712 last_channel
= priv
->scan_req
->n_channels
;
713 tlv
+= lbs_add_channel_list_tlv(priv
, tlv
, last_channel
,
714 priv
->scan_req
->n_ssids
);
717 tlv
+= lbs_add_supported_rates_tlv(tlv
);
719 if (priv
->scan_channel
< priv
->scan_req
->n_channels
) {
720 cancel_delayed_work(&priv
->scan_work
);
721 if (netif_running(priv
->dev
))
722 queue_delayed_work(priv
->work_thread
, &priv
->scan_work
,
723 msecs_to_jiffies(300));
726 /* This is the final data we are about to send */
727 scan_cmd
->hdr
.size
= cpu_to_le16(tlv
- (u8
*)scan_cmd
);
728 lbs_deb_hex(LBS_DEB_SCAN
, "SCAN_CMD", (void *)scan_cmd
,
730 lbs_deb_hex(LBS_DEB_SCAN
, "SCAN_TLV", scan_cmd
->tlvbuffer
,
731 tlv
- scan_cmd
->tlvbuffer
);
733 __lbs_cmd(priv
, CMD_802_11_SCAN
, &scan_cmd
->hdr
,
734 le16_to_cpu(scan_cmd
->hdr
.size
),
737 if (priv
->scan_channel
>= priv
->scan_req
->n_channels
) {
739 cancel_delayed_work(&priv
->scan_work
);
743 /* Restart network */
745 netif_carrier_on(priv
->dev
);
746 if (running
&& !priv
->tx_pending_len
)
747 netif_wake_queue(priv
->dev
);
751 /* Wake up anything waiting on scan completion */
752 if (priv
->scan_req
== NULL
) {
753 lbs_deb_scan("scan: waking up waiters\n");
754 wake_up_all(&priv
->scan_q
);
758 static void _internal_start_scan(struct lbs_private
*priv
, bool internal
,
759 struct cfg80211_scan_request
*request
)
761 lbs_deb_scan("scan: ssids %d, channels %d, ie_len %zd\n",
762 request
->n_ssids
, request
->n_channels
, request
->ie_len
);
764 priv
->scan_channel
= 0;
765 priv
->scan_req
= request
;
766 priv
->internal_scan
= internal
;
768 queue_delayed_work(priv
->work_thread
, &priv
->scan_work
,
769 msecs_to_jiffies(50));
773 * Clean up priv->scan_req. Should be used to handle the allocation details.
775 void lbs_scan_done(struct lbs_private
*priv
)
777 WARN_ON(!priv
->scan_req
);
779 if (priv
->internal_scan
) {
780 kfree(priv
->scan_req
);
782 struct cfg80211_scan_info info
= {
786 cfg80211_scan_done(priv
->scan_req
, &info
);
789 priv
->scan_req
= NULL
;
792 static int lbs_cfg_scan(struct wiphy
*wiphy
,
793 struct cfg80211_scan_request
*request
)
795 struct lbs_private
*priv
= wiphy_priv(wiphy
);
798 if (priv
->scan_req
|| delayed_work_pending(&priv
->scan_work
)) {
799 /* old scan request not yet processed */
804 _internal_start_scan(priv
, false, request
);
806 if (priv
->surpriseremoved
)
820 void lbs_send_disconnect_notification(struct lbs_private
*priv
,
821 bool locally_generated
)
823 cfg80211_disconnected(priv
->dev
, 0, NULL
, 0, locally_generated
,
827 void lbs_send_mic_failureevent(struct lbs_private
*priv
, u32 event
)
829 cfg80211_michael_mic_failure(priv
->dev
,
831 event
== MACREG_INT_CODE_MIC_ERR_MULTICAST
?
832 NL80211_KEYTYPE_GROUP
:
833 NL80211_KEYTYPE_PAIRWISE
,
848 * This removes all WEP keys
850 static int lbs_remove_wep_keys(struct lbs_private
*priv
)
852 struct cmd_ds_802_11_set_wep cmd
;
855 memset(&cmd
, 0, sizeof(cmd
));
856 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
857 cmd
.keyindex
= cpu_to_le16(priv
->wep_tx_key
);
858 cmd
.action
= cpu_to_le16(CMD_ACT_REMOVE
);
860 ret
= lbs_cmd_with_response(priv
, CMD_802_11_SET_WEP
, &cmd
);
868 static int lbs_set_wep_keys(struct lbs_private
*priv
)
870 struct cmd_ds_802_11_set_wep cmd
;
879 * action 02 00 ACT_ADD
881 * type for key 1 01 WEP40
885 * key 1 39 39 39 39 39 00 00 00
886 * 00 00 00 00 00 00 00 00
887 * key 2 00 00 00 00 00 00 00 00
888 * 00 00 00 00 00 00 00 00
889 * key 3 00 00 00 00 00 00 00 00
890 * 00 00 00 00 00 00 00 00
891 * key 4 00 00 00 00 00 00 00 00
893 if (priv
->wep_key_len
[0] || priv
->wep_key_len
[1] ||
894 priv
->wep_key_len
[2] || priv
->wep_key_len
[3]) {
895 /* Only set wep keys if we have at least one of them */
896 memset(&cmd
, 0, sizeof(cmd
));
897 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
898 cmd
.keyindex
= cpu_to_le16(priv
->wep_tx_key
);
899 cmd
.action
= cpu_to_le16(CMD_ACT_ADD
);
901 for (i
= 0; i
< 4; i
++) {
902 switch (priv
->wep_key_len
[i
]) {
903 case WLAN_KEY_LEN_WEP40
:
904 cmd
.keytype
[i
] = CMD_TYPE_WEP_40_BIT
;
906 case WLAN_KEY_LEN_WEP104
:
907 cmd
.keytype
[i
] = CMD_TYPE_WEP_104_BIT
;
913 memcpy(cmd
.keymaterial
[i
], priv
->wep_key
[i
],
914 priv
->wep_key_len
[i
]);
917 ret
= lbs_cmd_with_response(priv
, CMD_802_11_SET_WEP
, &cmd
);
919 /* Otherwise remove all wep keys */
920 ret
= lbs_remove_wep_keys(priv
);
928 * Enable/Disable RSN status
930 static int lbs_enable_rsn(struct lbs_private
*priv
, int enable
)
932 struct cmd_ds_802_11_enable_rsn cmd
;
940 * action 01 00 ACT_SET
943 memset(&cmd
, 0, sizeof(cmd
));
944 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
945 cmd
.action
= cpu_to_le16(CMD_ACT_SET
);
946 cmd
.enable
= cpu_to_le16(enable
);
948 ret
= lbs_cmd_with_response(priv
, CMD_802_11_ENABLE_RSN
, &cmd
);
955 * Set WPA/WPA key material
959 * like "struct cmd_ds_802_11_key_material", but with cmd_header. Once we
960 * get rid of WEXT, this should go into host.h
963 struct cmd_key_material
{
964 struct cmd_header hdr
;
967 struct MrvlIEtype_keyParamSet param
;
970 static int lbs_set_key_material(struct lbs_private
*priv
,
971 int key_type
, int key_info
,
972 const u8
*key
, u16 key_len
)
974 struct cmd_key_material cmd
;
978 * Example for WPA (TKIP):
985 * TLV type 00 01 key param
987 * key type 01 00 TKIP
988 * key info 06 00 UNICAST | ENABLED
992 memset(&cmd
, 0, sizeof(cmd
));
993 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
994 cmd
.action
= cpu_to_le16(CMD_ACT_SET
);
995 cmd
.param
.type
= cpu_to_le16(TLV_TYPE_KEY_MATERIAL
);
996 cmd
.param
.length
= cpu_to_le16(sizeof(cmd
.param
) - 4);
997 cmd
.param
.keytypeid
= cpu_to_le16(key_type
);
998 cmd
.param
.keyinfo
= cpu_to_le16(key_info
);
999 cmd
.param
.keylen
= cpu_to_le16(key_len
);
1001 memcpy(cmd
.param
.key
, key
, key_len
);
1003 ret
= lbs_cmd_with_response(priv
, CMD_802_11_KEY_MATERIAL
, &cmd
);
1010 * Sets the auth type (open, shared, etc) in the firmware. That
1011 * we use CMD_802_11_AUTHENTICATE is misleading, this firmware
1012 * command doesn't send an authentication frame at all, it just
1013 * stores the auth_type.
1015 static int lbs_set_authtype(struct lbs_private
*priv
,
1016 struct cfg80211_connect_params
*sme
)
1018 struct cmd_ds_802_11_authenticate cmd
;
1026 * BSS id 00 13 19 80 da 30
1028 * reserved 00 00 00 00 00 00 00 00 00 00
1030 memset(&cmd
, 0, sizeof(cmd
));
1031 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
1033 memcpy(cmd
.bssid
, sme
->bssid
, ETH_ALEN
);
1034 /* convert auth_type */
1035 ret
= lbs_auth_to_authtype(sme
->auth_type
);
1040 ret
= lbs_cmd_with_response(priv
, CMD_802_11_AUTHENTICATE
, &cmd
);
1048 * Create association request
1050 #define LBS_ASSOC_MAX_CMD_SIZE \
1051 (sizeof(struct cmd_ds_802_11_associate) \
1052 - 512 /* cmd_ds_802_11_associate.iebuf */ \
1053 + LBS_MAX_SSID_TLV_SIZE \
1054 + LBS_MAX_CHANNEL_TLV_SIZE \
1055 + LBS_MAX_CF_PARAM_TLV_SIZE \
1056 + LBS_MAX_AUTH_TYPE_TLV_SIZE \
1057 + LBS_MAX_WPA_TLV_SIZE)
1059 static int lbs_associate(struct lbs_private
*priv
,
1060 struct cfg80211_bss
*bss
,
1061 struct cfg80211_connect_params
*sme
)
1063 struct cmd_ds_802_11_associate_response
*resp
;
1064 struct cmd_ds_802_11_associate
*cmd
= kzalloc(LBS_ASSOC_MAX_CMD_SIZE
,
1067 size_t len
, resp_ie_len
;
1077 pos
= &cmd
->iebuf
[0];
1084 * BSS id 00 13 19 80 da 30
1085 * capabilities 11 00
1086 * listen interval 0a 00
1087 * beacon interval 00 00
1089 * TLVs xx (up to 512 bytes)
1091 cmd
->hdr
.command
= cpu_to_le16(CMD_802_11_ASSOCIATE
);
1093 /* Fill in static fields */
1094 memcpy(cmd
->bssid
, bss
->bssid
, ETH_ALEN
);
1095 cmd
->listeninterval
= cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL
);
1096 cmd
->capability
= cpu_to_le16(bss
->capability
);
1100 ssid_eid
= ieee80211_bss_get_ie(bss
, WLAN_EID_SSID
);
1102 pos
+= lbs_add_ssid_tlv(pos
, ssid_eid
+ 2, ssid_eid
[1]);
1104 lbs_deb_assoc("no SSID\n");
1107 /* add DS param TLV */
1109 pos
+= lbs_add_channel_tlv(pos
, bss
->channel
->hw_value
);
1111 lbs_deb_assoc("no channel\n");
1113 /* add (empty) CF param TLV */
1114 pos
+= lbs_add_cf_param_tlv(pos
);
1117 tmp
= pos
+ 4; /* skip Marvell IE header */
1118 pos
+= lbs_add_common_rates_tlv(pos
, bss
);
1119 lbs_deb_hex(LBS_DEB_ASSOC
, "Common Rates", tmp
, pos
- tmp
);
1121 /* add auth type TLV */
1122 if (MRVL_FW_MAJOR_REV(priv
->fwrelease
) >= 9)
1123 pos
+= lbs_add_auth_type_tlv(pos
, sme
->auth_type
);
1125 /* add WPA/WPA2 TLV */
1126 if (sme
->ie
&& sme
->ie_len
)
1127 pos
+= lbs_add_wpa_tlv(pos
, sme
->ie
, sme
->ie_len
);
1129 len
= (sizeof(*cmd
) - sizeof(cmd
->iebuf
)) +
1130 (u16
)(pos
- (u8
*) &cmd
->iebuf
);
1131 cmd
->hdr
.size
= cpu_to_le16(len
);
1133 lbs_deb_hex(LBS_DEB_ASSOC
, "ASSOC_CMD", (u8
*) cmd
,
1134 le16_to_cpu(cmd
->hdr
.size
));
1136 /* store for later use */
1137 memcpy(priv
->assoc_bss
, bss
->bssid
, ETH_ALEN
);
1139 ret
= lbs_cmd_with_response(priv
, CMD_802_11_ASSOCIATE
, cmd
);
1143 /* generate connect message to cfg80211 */
1145 resp
= (void *) cmd
; /* recast for easier field access */
1146 status
= le16_to_cpu(resp
->statuscode
);
1148 /* Older FW versions map the IEEE 802.11 Status Code in the association
1149 * response to the following values returned in resp->statuscode:
1151 * IEEE Status Code Marvell Status Code
1152 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
1153 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1154 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1155 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1156 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1157 * others -> 0x0003 ASSOC_RESULT_REFUSED
1159 * Other response codes:
1160 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
1161 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
1162 * association response from the AP)
1164 if (MRVL_FW_MAJOR_REV(priv
->fwrelease
) <= 8) {
1169 lbs_deb_assoc("invalid association parameters\n");
1170 status
= WLAN_STATUS_CAPS_UNSUPPORTED
;
1173 lbs_deb_assoc("timer expired while waiting for AP\n");
1174 status
= WLAN_STATUS_AUTH_TIMEOUT
;
1177 lbs_deb_assoc("association refused by AP\n");
1178 status
= WLAN_STATUS_ASSOC_DENIED_UNSPEC
;
1181 lbs_deb_assoc("authentication refused by AP\n");
1182 status
= WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION
;
1185 lbs_deb_assoc("association failure %d\n", status
);
1186 /* v5 OLPC firmware does return the AP status code if
1187 * it's not one of the values above. Let that through.
1193 lbs_deb_assoc("status %d, statuscode 0x%04x, capability 0x%04x, "
1194 "aid 0x%04x\n", status
, le16_to_cpu(resp
->statuscode
),
1195 le16_to_cpu(resp
->capability
), le16_to_cpu(resp
->aid
));
1197 resp_ie_len
= le16_to_cpu(resp
->hdr
.size
)
1200 cfg80211_connect_result(priv
->dev
,
1202 sme
->ie
, sme
->ie_len
,
1203 resp
->iebuf
, resp_ie_len
,
1208 /* TODO: get rid of priv->connect_status */
1209 priv
->connect_status
= LBS_CONNECTED
;
1210 netif_carrier_on(priv
->dev
);
1211 if (!priv
->tx_pending_len
)
1212 netif_tx_wake_all_queues(priv
->dev
);
1220 static struct cfg80211_scan_request
*
1221 _new_connect_scan_req(struct wiphy
*wiphy
, struct cfg80211_connect_params
*sme
)
1223 struct cfg80211_scan_request
*creq
= NULL
;
1224 int i
, n_channels
= ieee80211_get_num_supported_channels(wiphy
);
1225 enum nl80211_band band
;
1227 creq
= kzalloc(sizeof(*creq
) + sizeof(struct cfg80211_ssid
) +
1228 n_channels
* sizeof(void *),
1233 /* SSIDs come after channels */
1234 creq
->ssids
= (void *)&creq
->channels
[n_channels
];
1235 creq
->n_channels
= n_channels
;
1238 /* Scan all available channels */
1240 for (band
= 0; band
< NUM_NL80211_BANDS
; band
++) {
1243 if (!wiphy
->bands
[band
])
1246 for (j
= 0; j
< wiphy
->bands
[band
]->n_channels
; j
++) {
1247 /* ignore disabled channels */
1248 if (wiphy
->bands
[band
]->channels
[j
].flags
&
1249 IEEE80211_CHAN_DISABLED
)
1252 creq
->channels
[i
] = &wiphy
->bands
[band
]->channels
[j
];
1257 /* Set real number of channels specified in creq->channels[] */
1258 creq
->n_channels
= i
;
1260 /* Scan for the SSID we're going to connect to */
1261 memcpy(creq
->ssids
[0].ssid
, sme
->ssid
, sme
->ssid_len
);
1262 creq
->ssids
[0].ssid_len
= sme
->ssid_len
;
1264 /* No channels found... */
1272 static int lbs_cfg_connect(struct wiphy
*wiphy
, struct net_device
*dev
,
1273 struct cfg80211_connect_params
*sme
)
1275 struct lbs_private
*priv
= wiphy_priv(wiphy
);
1276 struct cfg80211_bss
*bss
= NULL
;
1278 u8 preamble
= RADIO_PREAMBLE_SHORT
;
1280 if (dev
== priv
->mesh_dev
)
1284 struct cfg80211_scan_request
*creq
;
1287 * Scan for the requested network after waiting for existing
1290 lbs_deb_assoc("assoc: waiting for existing scans\n");
1291 wait_event_interruptible_timeout(priv
->scan_q
,
1292 (priv
->scan_req
== NULL
),
1295 creq
= _new_connect_scan_req(wiphy
, sme
);
1301 lbs_deb_assoc("assoc: scanning for compatible AP\n");
1302 _internal_start_scan(priv
, true, creq
);
1304 lbs_deb_assoc("assoc: waiting for scan to complete\n");
1305 wait_event_interruptible_timeout(priv
->scan_q
,
1306 (priv
->scan_req
== NULL
),
1308 lbs_deb_assoc("assoc: scanning completed\n");
1311 /* Find the BSS we want using available scan results */
1312 bss
= cfg80211_get_bss(wiphy
, sme
->channel
, sme
->bssid
,
1313 sme
->ssid
, sme
->ssid_len
, IEEE80211_BSS_TYPE_ESS
,
1314 IEEE80211_PRIVACY_ANY
);
1316 wiphy_err(wiphy
, "assoc: bss %pM not in scan results\n",
1321 lbs_deb_assoc("trying %pM\n", bss
->bssid
);
1322 lbs_deb_assoc("cipher 0x%x, key index %d, key len %d\n",
1323 sme
->crypto
.cipher_group
,
1324 sme
->key_idx
, sme
->key_len
);
1326 /* As this is a new connection, clear locally stored WEP keys */
1327 priv
->wep_tx_key
= 0;
1328 memset(priv
->wep_key
, 0, sizeof(priv
->wep_key
));
1329 memset(priv
->wep_key_len
, 0, sizeof(priv
->wep_key_len
));
1331 /* set/remove WEP keys */
1332 switch (sme
->crypto
.cipher_group
) {
1333 case WLAN_CIPHER_SUITE_WEP40
:
1334 case WLAN_CIPHER_SUITE_WEP104
:
1335 /* Store provided WEP keys in priv-> */
1336 priv
->wep_tx_key
= sme
->key_idx
;
1337 priv
->wep_key_len
[sme
->key_idx
] = sme
->key_len
;
1338 memcpy(priv
->wep_key
[sme
->key_idx
], sme
->key
, sme
->key_len
);
1339 /* Set WEP keys and WEP mode */
1340 lbs_set_wep_keys(priv
);
1341 priv
->mac_control
|= CMD_ACT_MAC_WEP_ENABLE
;
1342 lbs_set_mac_control(priv
);
1343 /* No RSN mode for WEP */
1344 lbs_enable_rsn(priv
, 0);
1346 case 0: /* there's no WLAN_CIPHER_SUITE_NONE definition */
1348 * If we don't have no WEP, no WPA and no WPA2,
1349 * we remove all keys like in the WPA/WPA2 setup,
1350 * we just don't set RSN.
1352 * Therefore: fall-through
1354 case WLAN_CIPHER_SUITE_TKIP
:
1355 case WLAN_CIPHER_SUITE_CCMP
:
1356 /* Remove WEP keys and WEP mode */
1357 lbs_remove_wep_keys(priv
);
1358 priv
->mac_control
&= ~CMD_ACT_MAC_WEP_ENABLE
;
1359 lbs_set_mac_control(priv
);
1361 /* clear the WPA/WPA2 keys */
1362 lbs_set_key_material(priv
,
1363 KEY_TYPE_ID_WEP
, /* doesn't matter */
1364 KEY_INFO_WPA_UNICAST
,
1366 lbs_set_key_material(priv
,
1367 KEY_TYPE_ID_WEP
, /* doesn't matter */
1370 /* RSN mode for WPA/WPA2 */
1371 lbs_enable_rsn(priv
, sme
->crypto
.cipher_group
!= 0);
1374 wiphy_err(wiphy
, "unsupported cipher group 0x%x\n",
1375 sme
->crypto
.cipher_group
);
1380 ret
= lbs_set_authtype(priv
, sme
);
1381 if (ret
== -ENOTSUPP
) {
1382 wiphy_err(wiphy
, "unsupported authtype 0x%x\n", sme
->auth_type
);
1386 lbs_set_radio(priv
, preamble
, 1);
1388 /* Do the actual association */
1389 ret
= lbs_associate(priv
, bss
, sme
);
1393 cfg80211_put_bss(wiphy
, bss
);
1397 int lbs_disconnect(struct lbs_private
*priv
, u16 reason
)
1399 struct cmd_ds_802_11_deauthenticate cmd
;
1402 memset(&cmd
, 0, sizeof(cmd
));
1403 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
1404 /* Mildly ugly to use a locally store my own BSSID ... */
1405 memcpy(cmd
.macaddr
, &priv
->assoc_bss
, ETH_ALEN
);
1406 cmd
.reasoncode
= cpu_to_le16(reason
);
1408 ret
= lbs_cmd_with_response(priv
, CMD_802_11_DEAUTHENTICATE
, &cmd
);
1412 cfg80211_disconnected(priv
->dev
,
1416 priv
->connect_status
= LBS_DISCONNECTED
;
1421 static int lbs_cfg_disconnect(struct wiphy
*wiphy
, struct net_device
*dev
,
1424 struct lbs_private
*priv
= wiphy_priv(wiphy
);
1426 if (dev
== priv
->mesh_dev
)
1429 /* store for lbs_cfg_ret_disconnect() */
1430 priv
->disassoc_reason
= reason_code
;
1432 return lbs_disconnect(priv
, reason_code
);
1435 static int lbs_cfg_set_default_key(struct wiphy
*wiphy
,
1436 struct net_device
*netdev
,
1437 u8 key_index
, bool unicast
,
1440 struct lbs_private
*priv
= wiphy_priv(wiphy
);
1442 if (netdev
== priv
->mesh_dev
)
1445 if (key_index
!= priv
->wep_tx_key
) {
1446 lbs_deb_assoc("set_default_key: to %d\n", key_index
);
1447 priv
->wep_tx_key
= key_index
;
1448 lbs_set_wep_keys(priv
);
1455 static int lbs_cfg_add_key(struct wiphy
*wiphy
, struct net_device
*netdev
,
1456 u8 idx
, bool pairwise
, const u8
*mac_addr
,
1457 struct key_params
*params
)
1459 struct lbs_private
*priv
= wiphy_priv(wiphy
);
1464 if (netdev
== priv
->mesh_dev
)
1467 lbs_deb_assoc("add_key: cipher 0x%x, mac_addr %pM\n",
1468 params
->cipher
, mac_addr
);
1469 lbs_deb_assoc("add_key: key index %d, key len %d\n",
1470 idx
, params
->key_len
);
1471 if (params
->key_len
)
1472 lbs_deb_hex(LBS_DEB_CFG80211
, "KEY",
1473 params
->key
, params
->key_len
);
1475 lbs_deb_assoc("add_key: seq len %d\n", params
->seq_len
);
1476 if (params
->seq_len
)
1477 lbs_deb_hex(LBS_DEB_CFG80211
, "SEQ",
1478 params
->seq
, params
->seq_len
);
1480 switch (params
->cipher
) {
1481 case WLAN_CIPHER_SUITE_WEP40
:
1482 case WLAN_CIPHER_SUITE_WEP104
:
1483 /* actually compare if something has changed ... */
1484 if ((priv
->wep_key_len
[idx
] != params
->key_len
) ||
1485 memcmp(priv
->wep_key
[idx
],
1486 params
->key
, params
->key_len
) != 0) {
1487 priv
->wep_key_len
[idx
] = params
->key_len
;
1488 memcpy(priv
->wep_key
[idx
],
1489 params
->key
, params
->key_len
);
1490 lbs_set_wep_keys(priv
);
1493 case WLAN_CIPHER_SUITE_TKIP
:
1494 case WLAN_CIPHER_SUITE_CCMP
:
1495 key_info
= KEY_INFO_WPA_ENABLED
| ((idx
== 0)
1496 ? KEY_INFO_WPA_UNICAST
1497 : KEY_INFO_WPA_MCAST
);
1498 key_type
= (params
->cipher
== WLAN_CIPHER_SUITE_TKIP
)
1501 lbs_set_key_material(priv
,
1504 params
->key
, params
->key_len
);
1507 wiphy_err(wiphy
, "unhandled cipher 0x%x\n", params
->cipher
);
1516 static int lbs_cfg_del_key(struct wiphy
*wiphy
, struct net_device
*netdev
,
1517 u8 key_index
, bool pairwise
, const u8
*mac_addr
)
1520 lbs_deb_assoc("del_key: key_idx %d, mac_addr %pM\n",
1521 key_index
, mac_addr
);
1524 struct lbs_private
*priv
= wiphy_priv(wiphy
);
1526 * I think can keep this a NO-OP, because:
1528 * - we clear all keys whenever we do lbs_cfg_connect() anyway
1529 * - neither "iw" nor "wpa_supplicant" won't call this during
1530 * an ongoing connection
1531 * - TODO: but I have to check if this is still true when
1532 * I set the AP to periodic re-keying
1533 * - we've not kzallec() something when we've added a key at
1534 * lbs_cfg_connect() or lbs_cfg_add_key().
1536 * This causes lbs_cfg_del_key() only called at disconnect time,
1537 * where we'd just waste time deleting a key that is not going
1538 * to be used anyway.
1540 if (key_index
< 3 && priv
->wep_key_len
[key_index
]) {
1541 priv
->wep_key_len
[key_index
] = 0;
1542 lbs_set_wep_keys(priv
);
1554 static int lbs_cfg_get_station(struct wiphy
*wiphy
, struct net_device
*dev
,
1555 const u8
*mac
, struct station_info
*sinfo
)
1557 struct lbs_private
*priv
= wiphy_priv(wiphy
);
1562 sinfo
->filled
|= BIT(NL80211_STA_INFO_TX_BYTES
) |
1563 BIT(NL80211_STA_INFO_TX_PACKETS
) |
1564 BIT(NL80211_STA_INFO_RX_BYTES
) |
1565 BIT(NL80211_STA_INFO_RX_PACKETS
);
1566 sinfo
->tx_bytes
= priv
->dev
->stats
.tx_bytes
;
1567 sinfo
->tx_packets
= priv
->dev
->stats
.tx_packets
;
1568 sinfo
->rx_bytes
= priv
->dev
->stats
.rx_bytes
;
1569 sinfo
->rx_packets
= priv
->dev
->stats
.rx_packets
;
1571 /* Get current RSSI */
1572 ret
= lbs_get_rssi(priv
, &signal
, &noise
);
1574 sinfo
->signal
= signal
;
1575 sinfo
->filled
|= BIT(NL80211_STA_INFO_SIGNAL
);
1578 /* Convert priv->cur_rate from hw_value to NL80211 value */
1579 for (i
= 0; i
< ARRAY_SIZE(lbs_rates
); i
++) {
1580 if (priv
->cur_rate
== lbs_rates
[i
].hw_value
) {
1581 sinfo
->txrate
.legacy
= lbs_rates
[i
].bitrate
;
1582 sinfo
->filled
|= BIT(NL80211_STA_INFO_TX_BITRATE
);
1597 static int lbs_change_intf(struct wiphy
*wiphy
, struct net_device
*dev
,
1598 enum nl80211_iftype type
,
1599 struct vif_params
*params
)
1601 struct lbs_private
*priv
= wiphy_priv(wiphy
);
1604 if (dev
== priv
->mesh_dev
)
1608 case NL80211_IFTYPE_MONITOR
:
1609 case NL80211_IFTYPE_STATION
:
1610 case NL80211_IFTYPE_ADHOC
:
1616 if (priv
->iface_running
)
1617 ret
= lbs_set_iface_type(priv
, type
);
1620 priv
->wdev
->iftype
= type
;
1632 * The firmware needs the following bits masked out of the beacon-derived
1633 * capability field when associating/joining to a BSS:
1634 * 9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
1636 #define CAPINFO_MASK (~(0xda00))
1639 static void lbs_join_post(struct lbs_private
*priv
,
1640 struct cfg80211_ibss_params
*params
,
1641 u8
*bssid
, u16 capability
)
1643 u8 fake_ie
[2 + IEEE80211_MAX_SSID_LEN
+ /* ssid */
1644 2 + 4 + /* basic rates */
1645 2 + 1 + /* DS parameter */
1647 2 + 8]; /* extended rates */
1649 struct cfg80211_bss
*bss
;
1652 * For cfg80211_inform_bss, we'll need a fake IE, as we can't get
1653 * the real IE from the firmware. So we fabricate a fake IE based on
1654 * what the firmware actually sends (sniffed with wireshark).
1657 *fake
++ = WLAN_EID_SSID
;
1658 *fake
++ = params
->ssid_len
;
1659 memcpy(fake
, params
->ssid
, params
->ssid_len
);
1660 fake
+= params
->ssid_len
;
1661 /* Fake supported basic rates IE */
1662 *fake
++ = WLAN_EID_SUPP_RATES
;
1668 /* Fake DS channel IE */
1669 *fake
++ = WLAN_EID_DS_PARAMS
;
1671 *fake
++ = params
->chandef
.chan
->hw_value
;
1672 /* Fake IBSS params IE */
1673 *fake
++ = WLAN_EID_IBSS_PARAMS
;
1675 *fake
++ = 0; /* ATIM=0 */
1677 /* Fake extended rates IE, TODO: don't add this for 802.11b only,
1678 * but I don't know how this could be checked */
1679 *fake
++ = WLAN_EID_EXT_SUPP_RATES
;
1689 lbs_deb_hex(LBS_DEB_CFG80211
, "IE", fake_ie
, fake
- fake_ie
);
1691 bss
= cfg80211_inform_bss(priv
->wdev
->wiphy
,
1692 params
->chandef
.chan
,
1693 CFG80211_BSS_FTYPE_UNKNOWN
,
1697 params
->beacon_interval
,
1698 fake_ie
, fake
- fake_ie
,
1700 cfg80211_put_bss(priv
->wdev
->wiphy
, bss
);
1702 cfg80211_ibss_joined(priv
->dev
, bssid
, params
->chandef
.chan
,
1705 /* TODO: consider doing this at MACREG_INT_CODE_LINK_SENSED time */
1706 priv
->connect_status
= LBS_CONNECTED
;
1707 netif_carrier_on(priv
->dev
);
1708 if (!priv
->tx_pending_len
)
1709 netif_wake_queue(priv
->dev
);
1712 static int lbs_ibss_join_existing(struct lbs_private
*priv
,
1713 struct cfg80211_ibss_params
*params
,
1714 struct cfg80211_bss
*bss
)
1716 const u8
*rates_eid
;
1717 struct cmd_ds_802_11_ad_hoc_join cmd
;
1718 u8 preamble
= RADIO_PREAMBLE_SHORT
;
1721 /* TODO: set preamble based on scan result */
1722 ret
= lbs_set_radio(priv
, preamble
, 1);
1727 * Example CMD_802_11_AD_HOC_JOIN command:
1729 * command 2c 00 CMD_802_11_AD_HOC_JOIN
1733 * bssid 02 27 27 97 2f 96
1734 * ssid 49 42 53 53 00 00 00 00
1735 * 00 00 00 00 00 00 00 00
1736 * 00 00 00 00 00 00 00 00
1737 * 00 00 00 00 00 00 00 00
1738 * type 02 CMD_BSS_TYPE_IBSS
1739 * beacon period 64 00
1741 * timestamp 00 00 00 00 00 00 00 00
1742 * localtime 00 00 00 00 00 00 00 00
1746 * reserveed 00 00 00 00
1749 * IE IBSS atim 00 00
1750 * reserved 00 00 00 00
1752 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c 00
1753 * fail timeout ff 00
1756 memset(&cmd
, 0, sizeof(cmd
));
1757 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
1759 memcpy(cmd
.bss
.bssid
, bss
->bssid
, ETH_ALEN
);
1760 memcpy(cmd
.bss
.ssid
, params
->ssid
, params
->ssid_len
);
1761 cmd
.bss
.type
= CMD_BSS_TYPE_IBSS
;
1762 cmd
.bss
.beaconperiod
= cpu_to_le16(params
->beacon_interval
);
1763 cmd
.bss
.ds
.header
.id
= WLAN_EID_DS_PARAMS
;
1764 cmd
.bss
.ds
.header
.len
= 1;
1765 cmd
.bss
.ds
.channel
= params
->chandef
.chan
->hw_value
;
1766 cmd
.bss
.ibss
.header
.id
= WLAN_EID_IBSS_PARAMS
;
1767 cmd
.bss
.ibss
.header
.len
= 2;
1768 cmd
.bss
.ibss
.atimwindow
= 0;
1769 cmd
.bss
.capability
= cpu_to_le16(bss
->capability
& CAPINFO_MASK
);
1771 /* set rates to the intersection of our rates and the rates in the
1774 rates_eid
= ieee80211_bss_get_ie(bss
, WLAN_EID_SUPP_RATES
);
1776 lbs_add_rates(cmd
.bss
.rates
);
1779 u8 rates_max
= rates_eid
[1];
1780 u8
*rates
= cmd
.bss
.rates
;
1781 for (hw
= 0; hw
< ARRAY_SIZE(lbs_rates
); hw
++) {
1782 u8 hw_rate
= lbs_rates
[hw
].bitrate
/ 5;
1783 for (i
= 0; i
< rates_max
; i
++) {
1784 if (hw_rate
== (rates_eid
[i
+2] & 0x7f)) {
1785 u8 rate
= rates_eid
[i
+2];
1786 if (rate
== 0x02 || rate
== 0x04 ||
1787 rate
== 0x0b || rate
== 0x16)
1796 /* Only v8 and below support setting this */
1797 if (MRVL_FW_MAJOR_REV(priv
->fwrelease
) <= 8) {
1798 cmd
.failtimeout
= cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT
);
1799 cmd
.probedelay
= cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME
);
1801 ret
= lbs_cmd_with_response(priv
, CMD_802_11_AD_HOC_JOIN
, &cmd
);
1806 * This is a sample response to CMD_802_11_AD_HOC_JOIN:
1814 lbs_join_post(priv
, params
, bss
->bssid
, bss
->capability
);
1822 static int lbs_ibss_start_new(struct lbs_private
*priv
,
1823 struct cfg80211_ibss_params
*params
)
1825 struct cmd_ds_802_11_ad_hoc_start cmd
;
1826 struct cmd_ds_802_11_ad_hoc_result
*resp
=
1827 (struct cmd_ds_802_11_ad_hoc_result
*) &cmd
;
1828 u8 preamble
= RADIO_PREAMBLE_SHORT
;
1832 ret
= lbs_set_radio(priv
, preamble
, 1);
1837 * Example CMD_802_11_AD_HOC_START command:
1839 * command 2b 00 CMD_802_11_AD_HOC_START
1843 * ssid 54 45 53 54 00 00 00 00
1844 * 00 00 00 00 00 00 00 00
1845 * 00 00 00 00 00 00 00 00
1846 * 00 00 00 00 00 00 00 00
1848 * beacon period 64 00
1852 * IE IBSS atim 00 00
1853 * reserved 00 00 00 00
1857 * reserved 00 00 00 00
1860 * rates 82 84 8b 96 (basic rates with have bit 7 set)
1861 * 0c 12 18 24 30 48 60 6c
1864 memset(&cmd
, 0, sizeof(cmd
));
1865 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
1866 memcpy(cmd
.ssid
, params
->ssid
, params
->ssid_len
);
1867 cmd
.bsstype
= CMD_BSS_TYPE_IBSS
;
1868 cmd
.beaconperiod
= cpu_to_le16(params
->beacon_interval
);
1869 cmd
.ibss
.header
.id
= WLAN_EID_IBSS_PARAMS
;
1870 cmd
.ibss
.header
.len
= 2;
1871 cmd
.ibss
.atimwindow
= 0;
1872 cmd
.ds
.header
.id
= WLAN_EID_DS_PARAMS
;
1873 cmd
.ds
.header
.len
= 1;
1874 cmd
.ds
.channel
= params
->chandef
.chan
->hw_value
;
1875 /* Only v8 and below support setting probe delay */
1876 if (MRVL_FW_MAJOR_REV(priv
->fwrelease
) <= 8)
1877 cmd
.probedelay
= cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME
);
1878 /* TODO: mix in WLAN_CAPABILITY_PRIVACY */
1879 capability
= WLAN_CAPABILITY_IBSS
;
1880 cmd
.capability
= cpu_to_le16(capability
);
1881 lbs_add_rates(cmd
.rates
);
1884 ret
= lbs_cmd_with_response(priv
, CMD_802_11_AD_HOC_START
, &cmd
);
1889 * This is a sample response to CMD_802_11_AD_HOC_JOIN:
1896 * bssid 02 2b 7b 0f 86 0e
1898 lbs_join_post(priv
, params
, resp
->bssid
, capability
);
1905 static int lbs_join_ibss(struct wiphy
*wiphy
, struct net_device
*dev
,
1906 struct cfg80211_ibss_params
*params
)
1908 struct lbs_private
*priv
= wiphy_priv(wiphy
);
1910 struct cfg80211_bss
*bss
;
1912 if (dev
== priv
->mesh_dev
)
1915 if (!params
->chandef
.chan
) {
1920 ret
= lbs_set_channel(priv
, params
->chandef
.chan
->hw_value
);
1924 /* Search if someone is beaconing. This assumes that the
1925 * bss list is populated already */
1926 bss
= cfg80211_get_bss(wiphy
, params
->chandef
.chan
, params
->bssid
,
1927 params
->ssid
, params
->ssid_len
,
1928 IEEE80211_BSS_TYPE_IBSS
, IEEE80211_PRIVACY_ANY
);
1931 ret
= lbs_ibss_join_existing(priv
, params
, bss
);
1932 cfg80211_put_bss(wiphy
, bss
);
1934 ret
= lbs_ibss_start_new(priv
, params
);
1942 static int lbs_leave_ibss(struct wiphy
*wiphy
, struct net_device
*dev
)
1944 struct lbs_private
*priv
= wiphy_priv(wiphy
);
1945 struct cmd_ds_802_11_ad_hoc_stop cmd
;
1948 if (dev
== priv
->mesh_dev
)
1951 memset(&cmd
, 0, sizeof(cmd
));
1952 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
1953 ret
= lbs_cmd_with_response(priv
, CMD_802_11_AD_HOC_STOP
, &cmd
);
1955 /* TODO: consider doing this at MACREG_INT_CODE_ADHOC_BCN_LOST time */
1956 lbs_mac_event_disconnected(priv
, true);
1963 static int lbs_set_power_mgmt(struct wiphy
*wiphy
, struct net_device
*dev
,
1964 bool enabled
, int timeout
)
1966 struct lbs_private
*priv
= wiphy_priv(wiphy
);
1968 if (!(priv
->fwcapinfo
& FW_CAPINFO_PS
)) {
1974 /* firmware does not work well with too long latency with power saving
1975 * enabled, so do not enable it if there is only polling, no
1976 * interrupts (like in some sdio hosts which can only
1977 * poll for sdio irqs)
1979 if (priv
->is_polling
) {
1986 priv
->psmode
= LBS802_11POWERMODECAM
;
1987 if (priv
->psstate
!= PS_STATE_FULL_POWER
)
1988 lbs_set_ps_mode(priv
,
1989 PS_MODE_ACTION_EXIT_PS
,
1993 if (priv
->psmode
!= LBS802_11POWERMODECAM
)
1995 priv
->psmode
= LBS802_11POWERMODEMAX_PSP
;
1996 if (priv
->connect_status
== LBS_CONNECTED
)
1997 lbs_set_ps_mode(priv
, PS_MODE_ACTION_ENTER_PS
, true);
2005 static const struct cfg80211_ops lbs_cfg80211_ops
= {
2006 .set_monitor_channel
= lbs_cfg_set_monitor_channel
,
2007 .libertas_set_mesh_channel
= lbs_cfg_set_mesh_channel
,
2008 .scan
= lbs_cfg_scan
,
2009 .connect
= lbs_cfg_connect
,
2010 .disconnect
= lbs_cfg_disconnect
,
2011 .add_key
= lbs_cfg_add_key
,
2012 .del_key
= lbs_cfg_del_key
,
2013 .set_default_key
= lbs_cfg_set_default_key
,
2014 .get_station
= lbs_cfg_get_station
,
2015 .change_virtual_intf
= lbs_change_intf
,
2016 .join_ibss
= lbs_join_ibss
,
2017 .leave_ibss
= lbs_leave_ibss
,
2018 .set_power_mgmt
= lbs_set_power_mgmt
,
2023 * At this time lbs_private *priv doesn't even exist, so we just allocate
2024 * memory and don't initialize the wiphy further. This is postponed until we
2025 * can talk to the firmware and happens at registration time in
2026 * lbs_cfg_wiphy_register().
2028 struct wireless_dev
*lbs_cfg_alloc(struct device
*dev
)
2031 struct wireless_dev
*wdev
;
2033 wdev
= kzalloc(sizeof(struct wireless_dev
), GFP_KERNEL
);
2035 return ERR_PTR(-ENOMEM
);
2037 wdev
->wiphy
= wiphy_new(&lbs_cfg80211_ops
, sizeof(struct lbs_private
));
2039 dev_err(dev
, "cannot allocate wiphy\n");
2048 return ERR_PTR(ret
);
2052 static void lbs_cfg_set_regulatory_hint(struct lbs_private
*priv
)
2054 struct region_code_mapping
{
2059 /* Section 5.17.2 */
2060 static const struct region_code_mapping regmap
[] = {
2061 {"US ", 0x10}, /* US FCC */
2062 {"CA ", 0x20}, /* Canada */
2063 {"EU ", 0x30}, /* ETSI */
2064 {"ES ", 0x31}, /* Spain */
2065 {"FR ", 0x32}, /* France */
2066 {"JP ", 0x40}, /* Japan */
2070 for (i
= 0; i
< ARRAY_SIZE(regmap
); i
++)
2071 if (regmap
[i
].code
== priv
->regioncode
) {
2072 regulatory_hint(priv
->wdev
->wiphy
, regmap
[i
].cn
);
2077 static void lbs_reg_notifier(struct wiphy
*wiphy
,
2078 struct regulatory_request
*request
)
2080 struct lbs_private
*priv
= wiphy_priv(wiphy
);
2082 memcpy(priv
->country_code
, request
->alpha2
, sizeof(request
->alpha2
));
2083 if (lbs_iface_active(priv
))
2084 lbs_set_11d_domain_info(priv
);
2088 * This function get's called after lbs_setup_firmware() determined the
2089 * firmware capabities. So we can setup the wiphy according to our
2090 * hardware/firmware.
2092 int lbs_cfg_register(struct lbs_private
*priv
)
2094 struct wireless_dev
*wdev
= priv
->wdev
;
2097 wdev
->wiphy
->max_scan_ssids
= 1;
2098 wdev
->wiphy
->signal_type
= CFG80211_SIGNAL_TYPE_MBM
;
2100 wdev
->wiphy
->interface_modes
=
2101 BIT(NL80211_IFTYPE_STATION
) |
2102 BIT(NL80211_IFTYPE_ADHOC
);
2103 if (lbs_rtap_supported(priv
))
2104 wdev
->wiphy
->interface_modes
|= BIT(NL80211_IFTYPE_MONITOR
);
2105 if (lbs_mesh_activated(priv
))
2106 wdev
->wiphy
->interface_modes
|= BIT(NL80211_IFTYPE_MESH_POINT
);
2108 wdev
->wiphy
->bands
[NL80211_BAND_2GHZ
] = &lbs_band_2ghz
;
2111 * We could check priv->fwcapinfo && FW_CAPINFO_WPA, but I have
2112 * never seen a firmware without WPA
2114 wdev
->wiphy
->cipher_suites
= cipher_suites
;
2115 wdev
->wiphy
->n_cipher_suites
= ARRAY_SIZE(cipher_suites
);
2116 wdev
->wiphy
->reg_notifier
= lbs_reg_notifier
;
2118 ret
= wiphy_register(wdev
->wiphy
);
2120 pr_err("cannot register wiphy device\n");
2122 priv
->wiphy_registered
= true;
2124 ret
= register_netdev(priv
->dev
);
2126 pr_err("cannot register network device\n");
2128 INIT_DELAYED_WORK(&priv
->scan_work
, lbs_scan_worker
);
2130 lbs_cfg_set_regulatory_hint(priv
);
2135 void lbs_scan_deinit(struct lbs_private
*priv
)
2137 cancel_delayed_work_sync(&priv
->scan_work
);
2141 void lbs_cfg_free(struct lbs_private
*priv
)
2143 struct wireless_dev
*wdev
= priv
->wdev
;
2148 if (priv
->wiphy_registered
)
2149 wiphy_unregister(wdev
->wiphy
);
2152 wiphy_free(wdev
->wiphy
);