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 = NL80211_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 int 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
;
302 rates_eid
= ieee80211_bss_get_ie(bss
, WLAN_EID_SUPP_RATES
);
303 ext_rates_eid
= ieee80211_bss_get_ie(bss
, WLAN_EID_EXT_SUPP_RATES
);
306 * 01 00 TLV_TYPE_RATES
310 rate_tlv
->header
.type
= cpu_to_le16(TLV_TYPE_RATES
);
311 tlv
+= sizeof(rate_tlv
->header
);
313 /* Add basic rates */
315 tlv
= add_ie_rates(tlv
, rates_eid
, &n
);
317 /* Add extended rates, if any */
319 tlv
= add_ie_rates(tlv
, ext_rates_eid
, &n
);
321 lbs_deb_assoc("assoc: bss had no basic rate IE\n");
322 /* Fallback: add basic 802.11b rates */
331 rate_tlv
->header
.len
= cpu_to_le16(n
);
332 return sizeof(rate_tlv
->header
) + n
;
339 * This is only needed for newer firmware (V9 and up).
341 #define LBS_MAX_AUTH_TYPE_TLV_SIZE \
342 sizeof(struct mrvl_ie_auth_type)
344 static int lbs_add_auth_type_tlv(u8
*tlv
, enum nl80211_auth_type auth_type
)
346 struct mrvl_ie_auth_type
*auth
= (void *) tlv
;
349 * 1f 01 TLV_TYPE_AUTH_TYPE
353 auth
->header
.type
= cpu_to_le16(TLV_TYPE_AUTH_TYPE
);
354 auth
->header
.len
= cpu_to_le16(sizeof(*auth
)-sizeof(auth
->header
));
355 auth
->auth
= cpu_to_le16(lbs_auth_to_authtype(auth_type
));
356 return sizeof(*auth
);
361 * Add channel (phy ds) TLV
363 #define LBS_MAX_CHANNEL_TLV_SIZE \
364 sizeof(struct mrvl_ie_header)
366 static int lbs_add_channel_tlv(u8
*tlv
, u8 channel
)
368 struct mrvl_ie_ds_param_set
*ds
= (void *) tlv
;
371 * 03 00 TLV_TYPE_PHY_DS
375 ds
->header
.type
= cpu_to_le16(TLV_TYPE_PHY_DS
);
376 ds
->header
.len
= cpu_to_le16(sizeof(*ds
)-sizeof(ds
->header
));
377 ds
->channel
= channel
;
383 * Add (empty) CF param TLV of the form:
385 #define LBS_MAX_CF_PARAM_TLV_SIZE \
386 sizeof(struct mrvl_ie_header)
388 static int lbs_add_cf_param_tlv(u8
*tlv
)
390 struct mrvl_ie_cf_param_set
*cf
= (void *)tlv
;
397 * 00 00 cfpmaxduration
398 * 00 00 cfpdurationremaining
400 cf
->header
.type
= cpu_to_le16(TLV_TYPE_CF
);
401 cf
->header
.len
= cpu_to_le16(sizeof(*cf
)-sizeof(cf
->header
));
408 #define LBS_MAX_WPA_TLV_SIZE \
409 (sizeof(struct mrvl_ie_header) \
410 + 128 /* TODO: I guessed the size */)
412 static int lbs_add_wpa_tlv(u8
*tlv
, const u8
*ie
, u8 ie_len
)
417 * We need just convert an IE to an TLV. IEs use u8 for the header,
421 * but TLVs use __le16 instead:
428 tlv_len
= *tlv
++ = *ie
++;
432 /* the TLV is two bytes larger than the IE */
440 static int lbs_cfg_set_monitor_channel(struct wiphy
*wiphy
,
441 struct cfg80211_chan_def
*chandef
)
443 struct lbs_private
*priv
= wiphy_priv(wiphy
);
446 if (cfg80211_get_chandef_type(chandef
) != NL80211_CHAN_NO_HT
)
449 ret
= lbs_set_channel(priv
, chandef
->chan
->hw_value
);
455 static int lbs_cfg_set_mesh_channel(struct wiphy
*wiphy
,
456 struct net_device
*netdev
,
457 struct ieee80211_channel
*channel
)
459 struct lbs_private
*priv
= wiphy_priv(wiphy
);
462 if (netdev
!= priv
->mesh_dev
)
465 ret
= lbs_mesh_set_channel(priv
, channel
->hw_value
);
478 * When scanning, the firmware doesn't send a nul packet with the power-safe
479 * bit to the AP. So we cannot stay away from our current channel too long,
480 * otherwise we loose data. So take a "nap" while scanning every other
483 #define LBS_SCAN_BEFORE_NAP 4
487 * When the firmware reports back a scan-result, it gives us an "u8 rssi",
488 * which isn't really an RSSI, as it becomes larger when moving away from
489 * the AP. Anyway, we need to convert that into mBm.
491 #define LBS_SCAN_RSSI_TO_MBM(rssi) \
492 ((-(int)rssi + 3)*100)
494 static int lbs_ret_scan(struct lbs_private
*priv
, unsigned long dummy
,
495 struct cmd_header
*resp
)
497 struct cfg80211_bss
*bss
;
498 struct cmd_ds_802_11_scan_rsp
*scanresp
= (void *)resp
;
506 bsssize
= get_unaligned_le16(&scanresp
->bssdescriptsize
);
508 lbs_deb_scan("scan response: %d BSSs (%d bytes); resp size %d bytes\n",
509 scanresp
->nr_sets
, bsssize
, le16_to_cpu(resp
->size
));
511 if (scanresp
->nr_sets
== 0) {
517 * The general layout of the scan response is described in chapter
518 * 5.7.1. Basically we have a common part, then any number of BSS
519 * descriptor sections. Finally we have section with the same number
522 * cmd_ds_802_11_scan_rsp
535 * MrvlIEtypes_TsfFimestamp_t
541 pos
= scanresp
->bssdesc_and_tlvbuffer
;
543 lbs_deb_hex(LBS_DEB_SCAN
, "SCAN_RSP", scanresp
->bssdesc_and_tlvbuffer
,
544 scanresp
->bssdescriptsize
);
546 tsfdesc
= pos
+ bsssize
;
547 tsfsize
= 4 + 8 * scanresp
->nr_sets
;
548 lbs_deb_hex(LBS_DEB_SCAN
, "SCAN_TSF", (u8
*) tsfdesc
, tsfsize
);
550 /* Validity check: we expect a Marvell-Local TLV */
551 i
= get_unaligned_le16(tsfdesc
);
553 if (i
!= TLV_TYPE_TSFTIMESTAMP
) {
554 lbs_deb_scan("scan response: invalid TSF Timestamp %d\n", i
);
559 * Validity check: the TLV holds TSF values with 8 bytes each, so
560 * the size in the TLV must match the nr_sets value
562 i
= get_unaligned_le16(tsfdesc
);
564 if (i
/ 8 != scanresp
->nr_sets
) {
565 lbs_deb_scan("scan response: invalid number of TSF timestamp "
566 "sets (expected %d got %d)\n", scanresp
->nr_sets
,
571 for (i
= 0; i
< scanresp
->nr_sets
; i
++) {
580 const u8
*ssid
= NULL
;
583 int len
= get_unaligned_le16(pos
);
591 /* Packet time stamp */
593 /* Beacon interval */
594 intvl
= get_unaligned_le16(pos
);
597 capa
= get_unaligned_le16(pos
);
600 /* To find out the channel, we must parse the IEs */
603 * 6+1+8+2+2: size of BSSID, RSSI, time stamp, beacon
604 * interval, capabilities
606 ielen
= left
= len
- (6 + 1 + 8 + 2 + 2);
613 lbs_deb_scan("scan response: invalid IE fmt\n");
617 if (id
== WLAN_EID_DS_PARAMS
)
619 if (id
== WLAN_EID_SSID
) {
627 /* No channel, no luck */
629 struct wiphy
*wiphy
= priv
->wdev
->wiphy
;
630 int freq
= ieee80211_channel_to_frequency(chan_no
,
632 struct ieee80211_channel
*channel
=
633 ieee80211_get_channel(wiphy
, freq
);
635 lbs_deb_scan("scan: %pM, capa %04x, chan %2d, %*pE, %d dBm\n",
636 bssid
, capa
, chan_no
, ssid_len
, ssid
,
637 LBS_SCAN_RSSI_TO_MBM(rssi
)/100);
640 !(channel
->flags
& IEEE80211_CHAN_DISABLED
)) {
641 bss
= cfg80211_inform_bss(wiphy
, channel
,
642 CFG80211_BSS_FTYPE_UNKNOWN
,
643 bssid
, get_unaligned_le64(tsfdesc
),
644 capa
, intvl
, ie
, ielen
,
645 LBS_SCAN_RSSI_TO_MBM(rssi
),
647 cfg80211_put_bss(wiphy
, bss
);
650 lbs_deb_scan("scan response: missing BSS channel IE\n");
662 * Our scan command contains a TLV, consting of a SSID TLV, a channel list
663 * TLV and a rates TLV. Determine the maximum size of them:
665 #define LBS_SCAN_MAX_CMD_SIZE \
666 (sizeof(struct cmd_ds_802_11_scan) \
667 + LBS_MAX_SSID_TLV_SIZE \
668 + LBS_MAX_CHANNEL_LIST_TLV_SIZE \
669 + LBS_MAX_RATES_TLV_SIZE)
672 * Assumes priv->scan_req is initialized and valid
673 * Assumes priv->scan_channel is initialized
675 static void lbs_scan_worker(struct work_struct
*work
)
677 struct lbs_private
*priv
=
678 container_of(work
, struct lbs_private
, scan_work
.work
);
679 struct cmd_ds_802_11_scan
*scan_cmd
;
680 u8
*tlv
; /* pointer into our current, growing TLV storage area */
682 int running
, carrier
;
684 scan_cmd
= kzalloc(LBS_SCAN_MAX_CMD_SIZE
, GFP_KERNEL
);
685 if (scan_cmd
== NULL
)
688 /* prepare fixed part of scan command */
689 scan_cmd
->bsstype
= CMD_BSS_TYPE_ANY
;
691 /* stop network while we're away from our main channel */
692 running
= !netif_queue_stopped(priv
->dev
);
693 carrier
= netif_carrier_ok(priv
->dev
);
695 netif_stop_queue(priv
->dev
);
697 netif_carrier_off(priv
->dev
);
699 /* prepare fixed part of scan command */
700 tlv
= scan_cmd
->tlvbuffer
;
703 if (priv
->scan_req
->n_ssids
&& priv
->scan_req
->ssids
[0].ssid_len
> 0)
704 tlv
+= lbs_add_ssid_tlv(tlv
,
705 priv
->scan_req
->ssids
[0].ssid
,
706 priv
->scan_req
->ssids
[0].ssid_len
);
708 /* add channel TLVs */
709 last_channel
= priv
->scan_channel
+ LBS_SCAN_BEFORE_NAP
;
710 if (last_channel
> priv
->scan_req
->n_channels
)
711 last_channel
= priv
->scan_req
->n_channels
;
712 tlv
+= lbs_add_channel_list_tlv(priv
, tlv
, last_channel
,
713 priv
->scan_req
->n_ssids
);
716 tlv
+= lbs_add_supported_rates_tlv(tlv
);
718 if (priv
->scan_channel
< priv
->scan_req
->n_channels
) {
719 cancel_delayed_work(&priv
->scan_work
);
720 if (netif_running(priv
->dev
))
721 queue_delayed_work(priv
->work_thread
, &priv
->scan_work
,
722 msecs_to_jiffies(300));
725 /* This is the final data we are about to send */
726 scan_cmd
->hdr
.size
= cpu_to_le16(tlv
- (u8
*)scan_cmd
);
727 lbs_deb_hex(LBS_DEB_SCAN
, "SCAN_CMD", (void *)scan_cmd
,
729 lbs_deb_hex(LBS_DEB_SCAN
, "SCAN_TLV", scan_cmd
->tlvbuffer
,
730 tlv
- scan_cmd
->tlvbuffer
);
732 __lbs_cmd(priv
, CMD_802_11_SCAN
, &scan_cmd
->hdr
,
733 le16_to_cpu(scan_cmd
->hdr
.size
),
736 if (priv
->scan_channel
>= priv
->scan_req
->n_channels
) {
738 cancel_delayed_work(&priv
->scan_work
);
742 /* Restart network */
744 netif_carrier_on(priv
->dev
);
745 if (running
&& !priv
->tx_pending_len
)
746 netif_wake_queue(priv
->dev
);
750 /* Wake up anything waiting on scan completion */
751 if (priv
->scan_req
== NULL
) {
752 lbs_deb_scan("scan: waking up waiters\n");
753 wake_up_all(&priv
->scan_q
);
757 static void _internal_start_scan(struct lbs_private
*priv
, bool internal
,
758 struct cfg80211_scan_request
*request
)
760 lbs_deb_scan("scan: ssids %d, channels %d, ie_len %zd\n",
761 request
->n_ssids
, request
->n_channels
, request
->ie_len
);
763 priv
->scan_channel
= 0;
764 priv
->scan_req
= request
;
765 priv
->internal_scan
= internal
;
767 queue_delayed_work(priv
->work_thread
, &priv
->scan_work
,
768 msecs_to_jiffies(50));
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 struct cfg80211_scan_info info
= {
785 cfg80211_scan_done(priv
->scan_req
, &info
);
788 priv
->scan_req
= NULL
;
791 static int lbs_cfg_scan(struct wiphy
*wiphy
,
792 struct cfg80211_scan_request
*request
)
794 struct lbs_private
*priv
= wiphy_priv(wiphy
);
797 if (priv
->scan_req
|| delayed_work_pending(&priv
->scan_work
)) {
798 /* old scan request not yet processed */
803 _internal_start_scan(priv
, false, request
);
805 if (priv
->surpriseremoved
)
819 void lbs_send_disconnect_notification(struct lbs_private
*priv
,
820 bool locally_generated
)
822 cfg80211_disconnected(priv
->dev
, 0, NULL
, 0, locally_generated
,
826 void lbs_send_mic_failureevent(struct lbs_private
*priv
, u32 event
)
828 cfg80211_michael_mic_failure(priv
->dev
,
830 event
== MACREG_INT_CODE_MIC_ERR_MULTICAST
?
831 NL80211_KEYTYPE_GROUP
:
832 NL80211_KEYTYPE_PAIRWISE
,
847 * This removes all WEP keys
849 static int lbs_remove_wep_keys(struct lbs_private
*priv
)
851 struct cmd_ds_802_11_set_wep cmd
;
854 memset(&cmd
, 0, sizeof(cmd
));
855 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
856 cmd
.keyindex
= cpu_to_le16(priv
->wep_tx_key
);
857 cmd
.action
= cpu_to_le16(CMD_ACT_REMOVE
);
859 ret
= lbs_cmd_with_response(priv
, CMD_802_11_SET_WEP
, &cmd
);
867 static int lbs_set_wep_keys(struct lbs_private
*priv
)
869 struct cmd_ds_802_11_set_wep cmd
;
878 * action 02 00 ACT_ADD
880 * type for key 1 01 WEP40
884 * key 1 39 39 39 39 39 00 00 00
885 * 00 00 00 00 00 00 00 00
886 * key 2 00 00 00 00 00 00 00 00
887 * 00 00 00 00 00 00 00 00
888 * key 3 00 00 00 00 00 00 00 00
889 * 00 00 00 00 00 00 00 00
890 * key 4 00 00 00 00 00 00 00 00
892 if (priv
->wep_key_len
[0] || priv
->wep_key_len
[1] ||
893 priv
->wep_key_len
[2] || priv
->wep_key_len
[3]) {
894 /* Only set wep keys if we have at least one of them */
895 memset(&cmd
, 0, sizeof(cmd
));
896 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
897 cmd
.keyindex
= cpu_to_le16(priv
->wep_tx_key
);
898 cmd
.action
= cpu_to_le16(CMD_ACT_ADD
);
900 for (i
= 0; i
< 4; i
++) {
901 switch (priv
->wep_key_len
[i
]) {
902 case WLAN_KEY_LEN_WEP40
:
903 cmd
.keytype
[i
] = CMD_TYPE_WEP_40_BIT
;
905 case WLAN_KEY_LEN_WEP104
:
906 cmd
.keytype
[i
] = CMD_TYPE_WEP_104_BIT
;
912 memcpy(cmd
.keymaterial
[i
], priv
->wep_key
[i
],
913 priv
->wep_key_len
[i
]);
916 ret
= lbs_cmd_with_response(priv
, CMD_802_11_SET_WEP
, &cmd
);
918 /* Otherwise remove all wep keys */
919 ret
= lbs_remove_wep_keys(priv
);
927 * Enable/Disable RSN status
929 static int lbs_enable_rsn(struct lbs_private
*priv
, int enable
)
931 struct cmd_ds_802_11_enable_rsn cmd
;
939 * action 01 00 ACT_SET
942 memset(&cmd
, 0, sizeof(cmd
));
943 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
944 cmd
.action
= cpu_to_le16(CMD_ACT_SET
);
945 cmd
.enable
= cpu_to_le16(enable
);
947 ret
= lbs_cmd_with_response(priv
, CMD_802_11_ENABLE_RSN
, &cmd
);
954 * Set WPA/WPA key material
958 * like "struct cmd_ds_802_11_key_material", but with cmd_header. Once we
959 * get rid of WEXT, this should go into host.h
962 struct cmd_key_material
{
963 struct cmd_header hdr
;
966 struct MrvlIEtype_keyParamSet param
;
969 static int lbs_set_key_material(struct lbs_private
*priv
,
970 int key_type
, int key_info
,
971 const u8
*key
, u16 key_len
)
973 struct cmd_key_material cmd
;
977 * Example for WPA (TKIP):
984 * TLV type 00 01 key param
986 * key type 01 00 TKIP
987 * key info 06 00 UNICAST | ENABLED
991 memset(&cmd
, 0, sizeof(cmd
));
992 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
993 cmd
.action
= cpu_to_le16(CMD_ACT_SET
);
994 cmd
.param
.type
= cpu_to_le16(TLV_TYPE_KEY_MATERIAL
);
995 cmd
.param
.length
= cpu_to_le16(sizeof(cmd
.param
) - 4);
996 cmd
.param
.keytypeid
= cpu_to_le16(key_type
);
997 cmd
.param
.keyinfo
= cpu_to_le16(key_info
);
998 cmd
.param
.keylen
= cpu_to_le16(key_len
);
1000 memcpy(cmd
.param
.key
, key
, key_len
);
1002 ret
= lbs_cmd_with_response(priv
, CMD_802_11_KEY_MATERIAL
, &cmd
);
1009 * Sets the auth type (open, shared, etc) in the firmware. That
1010 * we use CMD_802_11_AUTHENTICATE is misleading, this firmware
1011 * command doesn't send an authentication frame at all, it just
1012 * stores the auth_type.
1014 static int lbs_set_authtype(struct lbs_private
*priv
,
1015 struct cfg80211_connect_params
*sme
)
1017 struct cmd_ds_802_11_authenticate cmd
;
1025 * BSS id 00 13 19 80 da 30
1027 * reserved 00 00 00 00 00 00 00 00 00 00
1029 memset(&cmd
, 0, sizeof(cmd
));
1030 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
1032 memcpy(cmd
.bssid
, sme
->bssid
, ETH_ALEN
);
1033 /* convert auth_type */
1034 ret
= lbs_auth_to_authtype(sme
->auth_type
);
1039 ret
= lbs_cmd_with_response(priv
, CMD_802_11_AUTHENTICATE
, &cmd
);
1047 * Create association request
1049 #define LBS_ASSOC_MAX_CMD_SIZE \
1050 (sizeof(struct cmd_ds_802_11_associate) \
1051 - 512 /* cmd_ds_802_11_associate.iebuf */ \
1052 + LBS_MAX_SSID_TLV_SIZE \
1053 + LBS_MAX_CHANNEL_TLV_SIZE \
1054 + LBS_MAX_CF_PARAM_TLV_SIZE \
1055 + LBS_MAX_AUTH_TYPE_TLV_SIZE \
1056 + LBS_MAX_WPA_TLV_SIZE)
1058 static int lbs_associate(struct lbs_private
*priv
,
1059 struct cfg80211_bss
*bss
,
1060 struct cfg80211_connect_params
*sme
)
1062 struct cmd_ds_802_11_associate_response
*resp
;
1063 struct cmd_ds_802_11_associate
*cmd
= kzalloc(LBS_ASSOC_MAX_CMD_SIZE
,
1066 size_t len
, resp_ie_len
;
1076 pos
= &cmd
->iebuf
[0];
1083 * BSS id 00 13 19 80 da 30
1084 * capabilities 11 00
1085 * listen interval 0a 00
1086 * beacon interval 00 00
1088 * TLVs xx (up to 512 bytes)
1090 cmd
->hdr
.command
= cpu_to_le16(CMD_802_11_ASSOCIATE
);
1092 /* Fill in static fields */
1093 memcpy(cmd
->bssid
, bss
->bssid
, ETH_ALEN
);
1094 cmd
->listeninterval
= cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL
);
1095 cmd
->capability
= cpu_to_le16(bss
->capability
);
1099 ssid_eid
= ieee80211_bss_get_ie(bss
, WLAN_EID_SSID
);
1101 pos
+= lbs_add_ssid_tlv(pos
, ssid_eid
+ 2, ssid_eid
[1]);
1103 lbs_deb_assoc("no SSID\n");
1106 /* add DS param TLV */
1108 pos
+= lbs_add_channel_tlv(pos
, bss
->channel
->hw_value
);
1110 lbs_deb_assoc("no channel\n");
1112 /* add (empty) CF param TLV */
1113 pos
+= lbs_add_cf_param_tlv(pos
);
1116 tmp
= pos
+ 4; /* skip Marvell IE header */
1117 pos
+= lbs_add_common_rates_tlv(pos
, bss
);
1118 lbs_deb_hex(LBS_DEB_ASSOC
, "Common Rates", tmp
, pos
- tmp
);
1120 /* add auth type TLV */
1121 if (MRVL_FW_MAJOR_REV(priv
->fwrelease
) >= 9)
1122 pos
+= lbs_add_auth_type_tlv(pos
, sme
->auth_type
);
1124 /* add WPA/WPA2 TLV */
1125 if (sme
->ie
&& sme
->ie_len
)
1126 pos
+= lbs_add_wpa_tlv(pos
, sme
->ie
, sme
->ie_len
);
1128 len
= (sizeof(*cmd
) - sizeof(cmd
->iebuf
)) +
1129 (u16
)(pos
- (u8
*) &cmd
->iebuf
);
1130 cmd
->hdr
.size
= cpu_to_le16(len
);
1132 lbs_deb_hex(LBS_DEB_ASSOC
, "ASSOC_CMD", (u8
*) cmd
,
1133 le16_to_cpu(cmd
->hdr
.size
));
1135 /* store for later use */
1136 memcpy(priv
->assoc_bss
, bss
->bssid
, ETH_ALEN
);
1138 ret
= lbs_cmd_with_response(priv
, CMD_802_11_ASSOCIATE
, cmd
);
1142 /* generate connect message to cfg80211 */
1144 resp
= (void *) cmd
; /* recast for easier field access */
1145 status
= le16_to_cpu(resp
->statuscode
);
1147 /* Older FW versions map the IEEE 802.11 Status Code in the association
1148 * response to the following values returned in resp->statuscode:
1150 * IEEE Status Code Marvell Status Code
1151 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
1152 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1153 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1154 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1155 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1156 * others -> 0x0003 ASSOC_RESULT_REFUSED
1158 * Other response codes:
1159 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
1160 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
1161 * association response from the AP)
1163 if (MRVL_FW_MAJOR_REV(priv
->fwrelease
) <= 8) {
1168 lbs_deb_assoc("invalid association parameters\n");
1169 status
= WLAN_STATUS_CAPS_UNSUPPORTED
;
1172 lbs_deb_assoc("timer expired while waiting for AP\n");
1173 status
= WLAN_STATUS_AUTH_TIMEOUT
;
1176 lbs_deb_assoc("association refused by AP\n");
1177 status
= WLAN_STATUS_ASSOC_DENIED_UNSPEC
;
1180 lbs_deb_assoc("authentication refused by AP\n");
1181 status
= WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION
;
1184 lbs_deb_assoc("association failure %d\n", status
);
1185 /* v5 OLPC firmware does return the AP status code if
1186 * it's not one of the values above. Let that through.
1192 lbs_deb_assoc("status %d, statuscode 0x%04x, capability 0x%04x, "
1193 "aid 0x%04x\n", status
, le16_to_cpu(resp
->statuscode
),
1194 le16_to_cpu(resp
->capability
), le16_to_cpu(resp
->aid
));
1196 resp_ie_len
= le16_to_cpu(resp
->hdr
.size
)
1199 cfg80211_connect_result(priv
->dev
,
1201 sme
->ie
, sme
->ie_len
,
1202 resp
->iebuf
, resp_ie_len
,
1207 /* TODO: get rid of priv->connect_status */
1208 priv
->connect_status
= LBS_CONNECTED
;
1209 netif_carrier_on(priv
->dev
);
1210 if (!priv
->tx_pending_len
)
1211 netif_tx_wake_all_queues(priv
->dev
);
1219 static struct cfg80211_scan_request
*
1220 _new_connect_scan_req(struct wiphy
*wiphy
, struct cfg80211_connect_params
*sme
)
1222 struct cfg80211_scan_request
*creq
= NULL
;
1223 int i
, n_channels
= ieee80211_get_num_supported_channels(wiphy
);
1224 enum nl80211_band band
;
1226 creq
= kzalloc(sizeof(*creq
) + sizeof(struct cfg80211_ssid
) +
1227 n_channels
* sizeof(void *),
1232 /* SSIDs come after channels */
1233 creq
->ssids
= (void *)&creq
->channels
[n_channels
];
1234 creq
->n_channels
= n_channels
;
1237 /* Scan all available channels */
1239 for (band
= 0; band
< NUM_NL80211_BANDS
; band
++) {
1242 if (!wiphy
->bands
[band
])
1245 for (j
= 0; j
< wiphy
->bands
[band
]->n_channels
; j
++) {
1246 /* ignore disabled channels */
1247 if (wiphy
->bands
[band
]->channels
[j
].flags
&
1248 IEEE80211_CHAN_DISABLED
)
1251 creq
->channels
[i
] = &wiphy
->bands
[band
]->channels
[j
];
1256 /* Set real number of channels specified in creq->channels[] */
1257 creq
->n_channels
= i
;
1259 /* Scan for the SSID we're going to connect to */
1260 memcpy(creq
->ssids
[0].ssid
, sme
->ssid
, sme
->ssid_len
);
1261 creq
->ssids
[0].ssid_len
= sme
->ssid_len
;
1263 /* No channels found... */
1271 static int lbs_cfg_connect(struct wiphy
*wiphy
, struct net_device
*dev
,
1272 struct cfg80211_connect_params
*sme
)
1274 struct lbs_private
*priv
= wiphy_priv(wiphy
);
1275 struct cfg80211_bss
*bss
= NULL
;
1277 u8 preamble
= RADIO_PREAMBLE_SHORT
;
1279 if (dev
== priv
->mesh_dev
)
1283 struct cfg80211_scan_request
*creq
;
1286 * Scan for the requested network after waiting for existing
1289 lbs_deb_assoc("assoc: waiting for existing scans\n");
1290 wait_event_interruptible_timeout(priv
->scan_q
,
1291 (priv
->scan_req
== NULL
),
1294 creq
= _new_connect_scan_req(wiphy
, sme
);
1300 lbs_deb_assoc("assoc: scanning for compatible AP\n");
1301 _internal_start_scan(priv
, true, creq
);
1303 lbs_deb_assoc("assoc: waiting for scan to complete\n");
1304 wait_event_interruptible_timeout(priv
->scan_q
,
1305 (priv
->scan_req
== NULL
),
1307 lbs_deb_assoc("assoc: scanning completed\n");
1310 /* Find the BSS we want using available scan results */
1311 bss
= cfg80211_get_bss(wiphy
, sme
->channel
, sme
->bssid
,
1312 sme
->ssid
, sme
->ssid_len
, IEEE80211_BSS_TYPE_ESS
,
1313 IEEE80211_PRIVACY_ANY
);
1315 wiphy_err(wiphy
, "assoc: bss %pM not in scan results\n",
1320 lbs_deb_assoc("trying %pM\n", bss
->bssid
);
1321 lbs_deb_assoc("cipher 0x%x, key index %d, key len %d\n",
1322 sme
->crypto
.cipher_group
,
1323 sme
->key_idx
, sme
->key_len
);
1325 /* As this is a new connection, clear locally stored WEP keys */
1326 priv
->wep_tx_key
= 0;
1327 memset(priv
->wep_key
, 0, sizeof(priv
->wep_key
));
1328 memset(priv
->wep_key_len
, 0, sizeof(priv
->wep_key_len
));
1330 /* set/remove WEP keys */
1331 switch (sme
->crypto
.cipher_group
) {
1332 case WLAN_CIPHER_SUITE_WEP40
:
1333 case WLAN_CIPHER_SUITE_WEP104
:
1334 /* Store provided WEP keys in priv-> */
1335 priv
->wep_tx_key
= sme
->key_idx
;
1336 priv
->wep_key_len
[sme
->key_idx
] = sme
->key_len
;
1337 memcpy(priv
->wep_key
[sme
->key_idx
], sme
->key
, sme
->key_len
);
1338 /* Set WEP keys and WEP mode */
1339 lbs_set_wep_keys(priv
);
1340 priv
->mac_control
|= CMD_ACT_MAC_WEP_ENABLE
;
1341 lbs_set_mac_control(priv
);
1342 /* No RSN mode for WEP */
1343 lbs_enable_rsn(priv
, 0);
1345 case 0: /* there's no WLAN_CIPHER_SUITE_NONE definition */
1347 * If we don't have no WEP, no WPA and no WPA2,
1348 * we remove all keys like in the WPA/WPA2 setup,
1349 * we just don't set RSN.
1351 * Therefore: fall-through
1353 case WLAN_CIPHER_SUITE_TKIP
:
1354 case WLAN_CIPHER_SUITE_CCMP
:
1355 /* Remove WEP keys and WEP mode */
1356 lbs_remove_wep_keys(priv
);
1357 priv
->mac_control
&= ~CMD_ACT_MAC_WEP_ENABLE
;
1358 lbs_set_mac_control(priv
);
1360 /* clear the WPA/WPA2 keys */
1361 lbs_set_key_material(priv
,
1362 KEY_TYPE_ID_WEP
, /* doesn't matter */
1363 KEY_INFO_WPA_UNICAST
,
1365 lbs_set_key_material(priv
,
1366 KEY_TYPE_ID_WEP
, /* doesn't matter */
1369 /* RSN mode for WPA/WPA2 */
1370 lbs_enable_rsn(priv
, sme
->crypto
.cipher_group
!= 0);
1373 wiphy_err(wiphy
, "unsupported cipher group 0x%x\n",
1374 sme
->crypto
.cipher_group
);
1379 ret
= lbs_set_authtype(priv
, sme
);
1380 if (ret
== -ENOTSUPP
) {
1381 wiphy_err(wiphy
, "unsupported authtype 0x%x\n", sme
->auth_type
);
1385 lbs_set_radio(priv
, preamble
, 1);
1387 /* Do the actual association */
1388 ret
= lbs_associate(priv
, bss
, sme
);
1392 cfg80211_put_bss(wiphy
, bss
);
1396 int lbs_disconnect(struct lbs_private
*priv
, u16 reason
)
1398 struct cmd_ds_802_11_deauthenticate cmd
;
1401 memset(&cmd
, 0, sizeof(cmd
));
1402 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
1403 /* Mildly ugly to use a locally store my own BSSID ... */
1404 memcpy(cmd
.macaddr
, &priv
->assoc_bss
, ETH_ALEN
);
1405 cmd
.reasoncode
= cpu_to_le16(reason
);
1407 ret
= lbs_cmd_with_response(priv
, CMD_802_11_DEAUTHENTICATE
, &cmd
);
1411 cfg80211_disconnected(priv
->dev
,
1415 priv
->connect_status
= LBS_DISCONNECTED
;
1420 static int lbs_cfg_disconnect(struct wiphy
*wiphy
, struct net_device
*dev
,
1423 struct lbs_private
*priv
= wiphy_priv(wiphy
);
1425 if (dev
== priv
->mesh_dev
)
1428 /* store for lbs_cfg_ret_disconnect() */
1429 priv
->disassoc_reason
= reason_code
;
1431 return lbs_disconnect(priv
, reason_code
);
1434 static int lbs_cfg_set_default_key(struct wiphy
*wiphy
,
1435 struct net_device
*netdev
,
1436 u8 key_index
, bool unicast
,
1439 struct lbs_private
*priv
= wiphy_priv(wiphy
);
1441 if (netdev
== priv
->mesh_dev
)
1444 if (key_index
!= priv
->wep_tx_key
) {
1445 lbs_deb_assoc("set_default_key: to %d\n", key_index
);
1446 priv
->wep_tx_key
= key_index
;
1447 lbs_set_wep_keys(priv
);
1454 static int lbs_cfg_add_key(struct wiphy
*wiphy
, struct net_device
*netdev
,
1455 u8 idx
, bool pairwise
, const u8
*mac_addr
,
1456 struct key_params
*params
)
1458 struct lbs_private
*priv
= wiphy_priv(wiphy
);
1463 if (netdev
== priv
->mesh_dev
)
1466 lbs_deb_assoc("add_key: cipher 0x%x, mac_addr %pM\n",
1467 params
->cipher
, mac_addr
);
1468 lbs_deb_assoc("add_key: key index %d, key len %d\n",
1469 idx
, params
->key_len
);
1470 if (params
->key_len
)
1471 lbs_deb_hex(LBS_DEB_CFG80211
, "KEY",
1472 params
->key
, params
->key_len
);
1474 lbs_deb_assoc("add_key: seq len %d\n", params
->seq_len
);
1475 if (params
->seq_len
)
1476 lbs_deb_hex(LBS_DEB_CFG80211
, "SEQ",
1477 params
->seq
, params
->seq_len
);
1479 switch (params
->cipher
) {
1480 case WLAN_CIPHER_SUITE_WEP40
:
1481 case WLAN_CIPHER_SUITE_WEP104
:
1482 /* actually compare if something has changed ... */
1483 if ((priv
->wep_key_len
[idx
] != params
->key_len
) ||
1484 memcmp(priv
->wep_key
[idx
],
1485 params
->key
, params
->key_len
) != 0) {
1486 priv
->wep_key_len
[idx
] = params
->key_len
;
1487 memcpy(priv
->wep_key
[idx
],
1488 params
->key
, params
->key_len
);
1489 lbs_set_wep_keys(priv
);
1492 case WLAN_CIPHER_SUITE_TKIP
:
1493 case WLAN_CIPHER_SUITE_CCMP
:
1494 key_info
= KEY_INFO_WPA_ENABLED
| ((idx
== 0)
1495 ? KEY_INFO_WPA_UNICAST
1496 : KEY_INFO_WPA_MCAST
);
1497 key_type
= (params
->cipher
== WLAN_CIPHER_SUITE_TKIP
)
1500 lbs_set_key_material(priv
,
1503 params
->key
, params
->key_len
);
1506 wiphy_err(wiphy
, "unhandled cipher 0x%x\n", params
->cipher
);
1515 static int lbs_cfg_del_key(struct wiphy
*wiphy
, struct net_device
*netdev
,
1516 u8 key_index
, bool pairwise
, const u8
*mac_addr
)
1519 lbs_deb_assoc("del_key: key_idx %d, mac_addr %pM\n",
1520 key_index
, mac_addr
);
1523 struct lbs_private
*priv
= wiphy_priv(wiphy
);
1525 * I think can keep this a NO-OP, because:
1527 * - we clear all keys whenever we do lbs_cfg_connect() anyway
1528 * - neither "iw" nor "wpa_supplicant" won't call this during
1529 * an ongoing connection
1530 * - TODO: but I have to check if this is still true when
1531 * I set the AP to periodic re-keying
1532 * - we've not kzallec() something when we've added a key at
1533 * lbs_cfg_connect() or lbs_cfg_add_key().
1535 * This causes lbs_cfg_del_key() only called at disconnect time,
1536 * where we'd just waste time deleting a key that is not going
1537 * to be used anyway.
1539 if (key_index
< 3 && priv
->wep_key_len
[key_index
]) {
1540 priv
->wep_key_len
[key_index
] = 0;
1541 lbs_set_wep_keys(priv
);
1553 static int lbs_cfg_get_station(struct wiphy
*wiphy
, struct net_device
*dev
,
1554 const u8
*mac
, struct station_info
*sinfo
)
1556 struct lbs_private
*priv
= wiphy_priv(wiphy
);
1561 sinfo
->filled
|= BIT(NL80211_STA_INFO_TX_BYTES
) |
1562 BIT(NL80211_STA_INFO_TX_PACKETS
) |
1563 BIT(NL80211_STA_INFO_RX_BYTES
) |
1564 BIT(NL80211_STA_INFO_RX_PACKETS
);
1565 sinfo
->tx_bytes
= priv
->dev
->stats
.tx_bytes
;
1566 sinfo
->tx_packets
= priv
->dev
->stats
.tx_packets
;
1567 sinfo
->rx_bytes
= priv
->dev
->stats
.rx_bytes
;
1568 sinfo
->rx_packets
= priv
->dev
->stats
.rx_packets
;
1570 /* Get current RSSI */
1571 ret
= lbs_get_rssi(priv
, &signal
, &noise
);
1573 sinfo
->signal
= signal
;
1574 sinfo
->filled
|= BIT(NL80211_STA_INFO_SIGNAL
);
1577 /* Convert priv->cur_rate from hw_value to NL80211 value */
1578 for (i
= 0; i
< ARRAY_SIZE(lbs_rates
); i
++) {
1579 if (priv
->cur_rate
== lbs_rates
[i
].hw_value
) {
1580 sinfo
->txrate
.legacy
= lbs_rates
[i
].bitrate
;
1581 sinfo
->filled
|= BIT(NL80211_STA_INFO_TX_BITRATE
);
1596 static int lbs_change_intf(struct wiphy
*wiphy
, struct net_device
*dev
,
1597 enum nl80211_iftype type
,
1598 struct vif_params
*params
)
1600 struct lbs_private
*priv
= wiphy_priv(wiphy
);
1603 if (dev
== priv
->mesh_dev
)
1607 case NL80211_IFTYPE_MONITOR
:
1608 case NL80211_IFTYPE_STATION
:
1609 case NL80211_IFTYPE_ADHOC
:
1615 if (priv
->iface_running
)
1616 ret
= lbs_set_iface_type(priv
, type
);
1619 priv
->wdev
->iftype
= type
;
1631 * The firmware needs the following bits masked out of the beacon-derived
1632 * capability field when associating/joining to a BSS:
1633 * 9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
1635 #define CAPINFO_MASK (~(0xda00))
1638 static void lbs_join_post(struct lbs_private
*priv
,
1639 struct cfg80211_ibss_params
*params
,
1640 u8
*bssid
, u16 capability
)
1642 u8 fake_ie
[2 + IEEE80211_MAX_SSID_LEN
+ /* ssid */
1643 2 + 4 + /* basic rates */
1644 2 + 1 + /* DS parameter */
1646 2 + 8]; /* extended rates */
1648 struct cfg80211_bss
*bss
;
1651 * For cfg80211_inform_bss, we'll need a fake IE, as we can't get
1652 * the real IE from the firmware. So we fabricate a fake IE based on
1653 * what the firmware actually sends (sniffed with wireshark).
1656 *fake
++ = WLAN_EID_SSID
;
1657 *fake
++ = params
->ssid_len
;
1658 memcpy(fake
, params
->ssid
, params
->ssid_len
);
1659 fake
+= params
->ssid_len
;
1660 /* Fake supported basic rates IE */
1661 *fake
++ = WLAN_EID_SUPP_RATES
;
1667 /* Fake DS channel IE */
1668 *fake
++ = WLAN_EID_DS_PARAMS
;
1670 *fake
++ = params
->chandef
.chan
->hw_value
;
1671 /* Fake IBSS params IE */
1672 *fake
++ = WLAN_EID_IBSS_PARAMS
;
1674 *fake
++ = 0; /* ATIM=0 */
1676 /* Fake extended rates IE, TODO: don't add this for 802.11b only,
1677 * but I don't know how this could be checked */
1678 *fake
++ = WLAN_EID_EXT_SUPP_RATES
;
1688 lbs_deb_hex(LBS_DEB_CFG80211
, "IE", fake_ie
, fake
- fake_ie
);
1690 bss
= cfg80211_inform_bss(priv
->wdev
->wiphy
,
1691 params
->chandef
.chan
,
1692 CFG80211_BSS_FTYPE_UNKNOWN
,
1696 params
->beacon_interval
,
1697 fake_ie
, fake
- fake_ie
,
1699 cfg80211_put_bss(priv
->wdev
->wiphy
, bss
);
1701 memcpy(priv
->wdev
->ssid
, params
->ssid
, params
->ssid_len
);
1702 priv
->wdev
->ssid_len
= params
->ssid_len
;
1704 cfg80211_ibss_joined(priv
->dev
, bssid
, params
->chandef
.chan
,
1707 /* TODO: consider doing this at MACREG_INT_CODE_LINK_SENSED time */
1708 priv
->connect_status
= LBS_CONNECTED
;
1709 netif_carrier_on(priv
->dev
);
1710 if (!priv
->tx_pending_len
)
1711 netif_wake_queue(priv
->dev
);
1714 static int lbs_ibss_join_existing(struct lbs_private
*priv
,
1715 struct cfg80211_ibss_params
*params
,
1716 struct cfg80211_bss
*bss
)
1718 const u8
*rates_eid
;
1719 struct cmd_ds_802_11_ad_hoc_join cmd
;
1720 u8 preamble
= RADIO_PREAMBLE_SHORT
;
1723 /* TODO: set preamble based on scan result */
1724 ret
= lbs_set_radio(priv
, preamble
, 1);
1729 * Example CMD_802_11_AD_HOC_JOIN command:
1731 * command 2c 00 CMD_802_11_AD_HOC_JOIN
1735 * bssid 02 27 27 97 2f 96
1736 * ssid 49 42 53 53 00 00 00 00
1737 * 00 00 00 00 00 00 00 00
1738 * 00 00 00 00 00 00 00 00
1739 * 00 00 00 00 00 00 00 00
1740 * type 02 CMD_BSS_TYPE_IBSS
1741 * beacon period 64 00
1743 * timestamp 00 00 00 00 00 00 00 00
1744 * localtime 00 00 00 00 00 00 00 00
1748 * reserveed 00 00 00 00
1751 * IE IBSS atim 00 00
1752 * reserved 00 00 00 00
1754 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c 00
1755 * fail timeout ff 00
1758 memset(&cmd
, 0, sizeof(cmd
));
1759 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
1761 memcpy(cmd
.bss
.bssid
, bss
->bssid
, ETH_ALEN
);
1762 memcpy(cmd
.bss
.ssid
, params
->ssid
, params
->ssid_len
);
1763 cmd
.bss
.type
= CMD_BSS_TYPE_IBSS
;
1764 cmd
.bss
.beaconperiod
= cpu_to_le16(params
->beacon_interval
);
1765 cmd
.bss
.ds
.header
.id
= WLAN_EID_DS_PARAMS
;
1766 cmd
.bss
.ds
.header
.len
= 1;
1767 cmd
.bss
.ds
.channel
= params
->chandef
.chan
->hw_value
;
1768 cmd
.bss
.ibss
.header
.id
= WLAN_EID_IBSS_PARAMS
;
1769 cmd
.bss
.ibss
.header
.len
= 2;
1770 cmd
.bss
.ibss
.atimwindow
= 0;
1771 cmd
.bss
.capability
= cpu_to_le16(bss
->capability
& CAPINFO_MASK
);
1773 /* set rates to the intersection of our rates and the rates in the
1776 rates_eid
= ieee80211_bss_get_ie(bss
, WLAN_EID_SUPP_RATES
);
1778 lbs_add_rates(cmd
.bss
.rates
);
1781 u8 rates_max
= rates_eid
[1];
1782 u8
*rates
= cmd
.bss
.rates
;
1783 for (hw
= 0; hw
< ARRAY_SIZE(lbs_rates
); hw
++) {
1784 u8 hw_rate
= lbs_rates
[hw
].bitrate
/ 5;
1785 for (i
= 0; i
< rates_max
; i
++) {
1786 if (hw_rate
== (rates_eid
[i
+2] & 0x7f)) {
1787 u8 rate
= rates_eid
[i
+2];
1788 if (rate
== 0x02 || rate
== 0x04 ||
1789 rate
== 0x0b || rate
== 0x16)
1798 /* Only v8 and below support setting this */
1799 if (MRVL_FW_MAJOR_REV(priv
->fwrelease
) <= 8) {
1800 cmd
.failtimeout
= cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT
);
1801 cmd
.probedelay
= cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME
);
1803 ret
= lbs_cmd_with_response(priv
, CMD_802_11_AD_HOC_JOIN
, &cmd
);
1808 * This is a sample response to CMD_802_11_AD_HOC_JOIN:
1816 lbs_join_post(priv
, params
, bss
->bssid
, bss
->capability
);
1824 static int lbs_ibss_start_new(struct lbs_private
*priv
,
1825 struct cfg80211_ibss_params
*params
)
1827 struct cmd_ds_802_11_ad_hoc_start cmd
;
1828 struct cmd_ds_802_11_ad_hoc_result
*resp
=
1829 (struct cmd_ds_802_11_ad_hoc_result
*) &cmd
;
1830 u8 preamble
= RADIO_PREAMBLE_SHORT
;
1834 ret
= lbs_set_radio(priv
, preamble
, 1);
1839 * Example CMD_802_11_AD_HOC_START command:
1841 * command 2b 00 CMD_802_11_AD_HOC_START
1845 * ssid 54 45 53 54 00 00 00 00
1846 * 00 00 00 00 00 00 00 00
1847 * 00 00 00 00 00 00 00 00
1848 * 00 00 00 00 00 00 00 00
1850 * beacon period 64 00
1854 * IE IBSS atim 00 00
1855 * reserved 00 00 00 00
1859 * reserved 00 00 00 00
1862 * rates 82 84 8b 96 (basic rates with have bit 7 set)
1863 * 0c 12 18 24 30 48 60 6c
1866 memset(&cmd
, 0, sizeof(cmd
));
1867 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
1868 memcpy(cmd
.ssid
, params
->ssid
, params
->ssid_len
);
1869 cmd
.bsstype
= CMD_BSS_TYPE_IBSS
;
1870 cmd
.beaconperiod
= cpu_to_le16(params
->beacon_interval
);
1871 cmd
.ibss
.header
.id
= WLAN_EID_IBSS_PARAMS
;
1872 cmd
.ibss
.header
.len
= 2;
1873 cmd
.ibss
.atimwindow
= 0;
1874 cmd
.ds
.header
.id
= WLAN_EID_DS_PARAMS
;
1875 cmd
.ds
.header
.len
= 1;
1876 cmd
.ds
.channel
= params
->chandef
.chan
->hw_value
;
1877 /* Only v8 and below support setting probe delay */
1878 if (MRVL_FW_MAJOR_REV(priv
->fwrelease
) <= 8)
1879 cmd
.probedelay
= cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME
);
1880 /* TODO: mix in WLAN_CAPABILITY_PRIVACY */
1881 capability
= WLAN_CAPABILITY_IBSS
;
1882 cmd
.capability
= cpu_to_le16(capability
);
1883 lbs_add_rates(cmd
.rates
);
1886 ret
= lbs_cmd_with_response(priv
, CMD_802_11_AD_HOC_START
, &cmd
);
1891 * This is a sample response to CMD_802_11_AD_HOC_JOIN:
1898 * bssid 02 2b 7b 0f 86 0e
1900 lbs_join_post(priv
, params
, resp
->bssid
, capability
);
1907 static int lbs_join_ibss(struct wiphy
*wiphy
, struct net_device
*dev
,
1908 struct cfg80211_ibss_params
*params
)
1910 struct lbs_private
*priv
= wiphy_priv(wiphy
);
1912 struct cfg80211_bss
*bss
;
1914 if (dev
== priv
->mesh_dev
)
1917 if (!params
->chandef
.chan
) {
1922 ret
= lbs_set_channel(priv
, params
->chandef
.chan
->hw_value
);
1926 /* Search if someone is beaconing. This assumes that the
1927 * bss list is populated already */
1928 bss
= cfg80211_get_bss(wiphy
, params
->chandef
.chan
, params
->bssid
,
1929 params
->ssid
, params
->ssid_len
,
1930 IEEE80211_BSS_TYPE_IBSS
, IEEE80211_PRIVACY_ANY
);
1933 ret
= lbs_ibss_join_existing(priv
, params
, bss
);
1934 cfg80211_put_bss(wiphy
, bss
);
1936 ret
= lbs_ibss_start_new(priv
, params
);
1944 static int lbs_leave_ibss(struct wiphy
*wiphy
, struct net_device
*dev
)
1946 struct lbs_private
*priv
= wiphy_priv(wiphy
);
1947 struct cmd_ds_802_11_ad_hoc_stop cmd
;
1950 if (dev
== priv
->mesh_dev
)
1953 memset(&cmd
, 0, sizeof(cmd
));
1954 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
1955 ret
= lbs_cmd_with_response(priv
, CMD_802_11_AD_HOC_STOP
, &cmd
);
1957 /* TODO: consider doing this at MACREG_INT_CODE_ADHOC_BCN_LOST time */
1958 lbs_mac_event_disconnected(priv
, true);
1965 static int lbs_set_power_mgmt(struct wiphy
*wiphy
, struct net_device
*dev
,
1966 bool enabled
, int timeout
)
1968 struct lbs_private
*priv
= wiphy_priv(wiphy
);
1970 if (!(priv
->fwcapinfo
& FW_CAPINFO_PS
)) {
1976 /* firmware does not work well with too long latency with power saving
1977 * enabled, so do not enable it if there is only polling, no
1978 * interrupts (like in some sdio hosts which can only
1979 * poll for sdio irqs)
1981 if (priv
->is_polling
) {
1988 priv
->psmode
= LBS802_11POWERMODECAM
;
1989 if (priv
->psstate
!= PS_STATE_FULL_POWER
)
1990 lbs_set_ps_mode(priv
,
1991 PS_MODE_ACTION_EXIT_PS
,
1995 if (priv
->psmode
!= LBS802_11POWERMODECAM
)
1997 priv
->psmode
= LBS802_11POWERMODEMAX_PSP
;
1998 if (priv
->connect_status
== LBS_CONNECTED
)
1999 lbs_set_ps_mode(priv
, PS_MODE_ACTION_ENTER_PS
, true);
2007 static const struct cfg80211_ops lbs_cfg80211_ops
= {
2008 .set_monitor_channel
= lbs_cfg_set_monitor_channel
,
2009 .libertas_set_mesh_channel
= lbs_cfg_set_mesh_channel
,
2010 .scan
= lbs_cfg_scan
,
2011 .connect
= lbs_cfg_connect
,
2012 .disconnect
= lbs_cfg_disconnect
,
2013 .add_key
= lbs_cfg_add_key
,
2014 .del_key
= lbs_cfg_del_key
,
2015 .set_default_key
= lbs_cfg_set_default_key
,
2016 .get_station
= lbs_cfg_get_station
,
2017 .change_virtual_intf
= lbs_change_intf
,
2018 .join_ibss
= lbs_join_ibss
,
2019 .leave_ibss
= lbs_leave_ibss
,
2020 .set_power_mgmt
= lbs_set_power_mgmt
,
2025 * At this time lbs_private *priv doesn't even exist, so we just allocate
2026 * memory and don't initialize the wiphy further. This is postponed until we
2027 * can talk to the firmware and happens at registration time in
2028 * lbs_cfg_wiphy_register().
2030 struct wireless_dev
*lbs_cfg_alloc(struct device
*dev
)
2033 struct wireless_dev
*wdev
;
2035 wdev
= kzalloc(sizeof(struct wireless_dev
), GFP_KERNEL
);
2037 return ERR_PTR(-ENOMEM
);
2039 wdev
->wiphy
= wiphy_new(&lbs_cfg80211_ops
, sizeof(struct lbs_private
));
2041 dev_err(dev
, "cannot allocate wiphy\n");
2050 return ERR_PTR(ret
);
2054 static void lbs_cfg_set_regulatory_hint(struct lbs_private
*priv
)
2056 struct region_code_mapping
{
2061 /* Section 5.17.2 */
2062 static const struct region_code_mapping regmap
[] = {
2063 {"US ", 0x10}, /* US FCC */
2064 {"CA ", 0x20}, /* Canada */
2065 {"EU ", 0x30}, /* ETSI */
2066 {"ES ", 0x31}, /* Spain */
2067 {"FR ", 0x32}, /* France */
2068 {"JP ", 0x40}, /* Japan */
2072 for (i
= 0; i
< ARRAY_SIZE(regmap
); i
++)
2073 if (regmap
[i
].code
== priv
->regioncode
) {
2074 regulatory_hint(priv
->wdev
->wiphy
, regmap
[i
].cn
);
2079 static void lbs_reg_notifier(struct wiphy
*wiphy
,
2080 struct regulatory_request
*request
)
2082 struct lbs_private
*priv
= wiphy_priv(wiphy
);
2084 memcpy(priv
->country_code
, request
->alpha2
, sizeof(request
->alpha2
));
2085 if (lbs_iface_active(priv
))
2086 lbs_set_11d_domain_info(priv
);
2090 * This function get's called after lbs_setup_firmware() determined the
2091 * firmware capabities. So we can setup the wiphy according to our
2092 * hardware/firmware.
2094 int lbs_cfg_register(struct lbs_private
*priv
)
2096 struct wireless_dev
*wdev
= priv
->wdev
;
2099 wdev
->wiphy
->max_scan_ssids
= 1;
2100 wdev
->wiphy
->signal_type
= CFG80211_SIGNAL_TYPE_MBM
;
2102 wdev
->wiphy
->interface_modes
=
2103 BIT(NL80211_IFTYPE_STATION
) |
2104 BIT(NL80211_IFTYPE_ADHOC
);
2105 if (lbs_rtap_supported(priv
))
2106 wdev
->wiphy
->interface_modes
|= BIT(NL80211_IFTYPE_MONITOR
);
2107 if (lbs_mesh_activated(priv
))
2108 wdev
->wiphy
->interface_modes
|= BIT(NL80211_IFTYPE_MESH_POINT
);
2110 wdev
->wiphy
->bands
[NL80211_BAND_2GHZ
] = &lbs_band_2ghz
;
2113 * We could check priv->fwcapinfo && FW_CAPINFO_WPA, but I have
2114 * never seen a firmware without WPA
2116 wdev
->wiphy
->cipher_suites
= cipher_suites
;
2117 wdev
->wiphy
->n_cipher_suites
= ARRAY_SIZE(cipher_suites
);
2118 wdev
->wiphy
->reg_notifier
= lbs_reg_notifier
;
2120 ret
= wiphy_register(wdev
->wiphy
);
2122 pr_err("cannot register wiphy device\n");
2124 priv
->wiphy_registered
= true;
2126 ret
= register_netdev(priv
->dev
);
2128 pr_err("cannot register network device\n");
2130 INIT_DELAYED_WORK(&priv
->scan_work
, lbs_scan_worker
);
2132 lbs_cfg_set_regulatory_hint(priv
);
2137 void lbs_scan_deinit(struct lbs_private
*priv
)
2139 cancel_delayed_work_sync(&priv
->scan_work
);
2143 void lbs_cfg_free(struct lbs_private
*priv
)
2145 struct wireless_dev
*wdev
= priv
->wdev
;
2150 if (priv
->wiphy_registered
)
2151 wiphy_unregister(wdev
->wiphy
);
2154 wiphy_free(wdev
->wiphy
);