2 * Functions implementing wlan scan IOCTL and firmware command APIs
4 * IOCTL handlers as well as command preperation and response routines
5 * for sending scan commands to the firmware.
7 #include <linux/ctype.h>
9 #include <linux/netdevice.h>
10 #include <linux/wireless.h>
11 #include <linux/etherdevice.h>
13 #include <net/ieee80211.h>
14 #include <net/iw_handler.h>
16 #include <asm/unaligned.h>
24 //! Approximate amount of data needed to pass a scan result back to iwlist
25 #define MAX_SCAN_CELL_SIZE (IW_EV_ADDR_LEN \
32 + 40) /* 40 for WPAIE */
34 //! Memory needed to store a max sized channel List TLV for a firmware scan
35 #define CHAN_TLV_MAX_SIZE (sizeof(struct mrvlietypesheader) \
36 + (MRVDRV_MAX_CHANNELS_PER_SCAN \
37 * sizeof(struct chanscanparamset)))
39 //! Memory needed to store a max number/size SSID TLV for a firmware scan
40 #define SSID_TLV_MAX_SIZE (1 * sizeof(struct mrvlietypes_ssidparamset))
42 //! Maximum memory needed for a lbs_scan_cmd_config with all TLVs at max
43 #define MAX_SCAN_CFG_ALLOC (sizeof(struct lbs_scan_cmd_config) \
44 + sizeof(struct mrvlietypes_numprobes) \
48 //! The maximum number of channels the firmware can scan per command
49 #define MRVDRV_MAX_CHANNELS_PER_SCAN 14
52 * @brief Number of channels to scan per firmware scan command issuance.
54 * Number restricted to prevent hitting the limit on the amount of scan data
55 * returned in a single firmware scan command.
57 #define MRVDRV_CHANNELS_PER_SCAN_CMD 4
59 //! Scan time specified in the channel TLV for each channel for passive scans
60 #define MRVDRV_PASSIVE_SCAN_CHAN_TIME 100
62 //! Scan time specified in the channel TLV for each channel for active scans
63 #define MRVDRV_ACTIVE_SCAN_CHAN_TIME 100
65 static const u8 zeromac[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
66 static const u8 bcastmac[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
71 /*********************************************************************/
73 /* Misc helper functions */
75 /*********************************************************************/
77 static inline void clear_bss_descriptor (struct bss_descriptor * bss)
79 /* Don't blow away ->list, just BSS data */
80 memset(bss, 0, offsetof(struct bss_descriptor, list));
83 static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
84 struct bss_descriptor * match_bss)
86 if ( !secinfo->wep_enabled
87 && !secinfo->WPAenabled
88 && !secinfo->WPA2enabled
89 && match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC
90 && match_bss->rsn_ie[0] != MFIE_TYPE_RSN
91 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
97 static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
98 struct bss_descriptor * match_bss)
100 if ( secinfo->wep_enabled
101 && !secinfo->WPAenabled
102 && !secinfo->WPA2enabled
103 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
109 static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
110 struct bss_descriptor * match_bss)
112 if ( !secinfo->wep_enabled
113 && secinfo->WPAenabled
114 && (match_bss->wpa_ie[0] == MFIE_TYPE_GENERIC)
115 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
116 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
124 static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
125 struct bss_descriptor * match_bss)
127 if ( !secinfo->wep_enabled
128 && secinfo->WPA2enabled
129 && (match_bss->rsn_ie[0] == MFIE_TYPE_RSN)
130 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
131 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
139 static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
140 struct bss_descriptor * match_bss)
142 if ( !secinfo->wep_enabled
143 && !secinfo->WPAenabled
144 && !secinfo->WPA2enabled
145 && (match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC)
146 && (match_bss->rsn_ie[0] != MFIE_TYPE_RSN)
147 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
154 * @brief Check if a scanned network compatible with the driver settings
156 * WEP WPA WPA2 ad-hoc encrypt Network
157 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
158 * 0 0 0 0 NONE 0 0 0 yes No security
159 * 1 0 0 0 NONE 1 0 0 yes Static WEP
160 * 0 1 0 0 x 1x 1 x yes WPA
161 * 0 0 1 0 x 1x x 1 yes WPA2
162 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
163 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
166 * @param adapter A pointer to struct lbs_adapter
167 * @param index Index in scantable to check against current driver settings
168 * @param mode Network mode: Infrastructure or IBSS
170 * @return Index in scantable, or error code if negative
172 static int is_network_compatible(struct lbs_adapter *adapter,
173 struct bss_descriptor * bss, u8 mode)
177 lbs_deb_enter(LBS_DEB_SCAN);
179 if (bss->mode != mode)
182 if ((matched = match_bss_no_security(&adapter->secinfo, bss))) {
184 } else if ((matched = match_bss_static_wep(&adapter->secinfo, bss))) {
186 } else if ((matched = match_bss_wpa(&adapter->secinfo, bss))) {
188 "is_network_compatible() WPA: wpa_ie=%#x "
189 "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s "
190 "privacy=%#x\n", bss->wpa_ie[0], bss->rsn_ie[0],
191 adapter->secinfo.wep_enabled ? "e" : "d",
192 adapter->secinfo.WPAenabled ? "e" : "d",
193 adapter->secinfo.WPA2enabled ? "e" : "d",
194 (bss->capability & WLAN_CAPABILITY_PRIVACY));
196 } else if ((matched = match_bss_wpa2(&adapter->secinfo, bss))) {
198 "is_network_compatible() WPA2: wpa_ie=%#x "
199 "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s "
200 "privacy=%#x\n", bss->wpa_ie[0], bss->rsn_ie[0],
201 adapter->secinfo.wep_enabled ? "e" : "d",
202 adapter->secinfo.WPAenabled ? "e" : "d",
203 adapter->secinfo.WPA2enabled ? "e" : "d",
204 (bss->capability & WLAN_CAPABILITY_PRIVACY));
206 } else if ((matched = match_bss_dynamic_wep(&adapter->secinfo, bss))) {
208 "is_network_compatible() dynamic WEP: "
209 "wpa_ie=%#x wpa2_ie=%#x privacy=%#x\n",
210 bss->wpa_ie[0], bss->rsn_ie[0],
211 (bss->capability & WLAN_CAPABILITY_PRIVACY));
215 /* bss security settings don't match those configured on card */
217 "is_network_compatible() FAILED: wpa_ie=%#x "
218 "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s privacy=%#x\n",
219 bss->wpa_ie[0], bss->rsn_ie[0],
220 adapter->secinfo.wep_enabled ? "e" : "d",
221 adapter->secinfo.WPAenabled ? "e" : "d",
222 adapter->secinfo.WPA2enabled ? "e" : "d",
223 (bss->capability & WLAN_CAPABILITY_PRIVACY));
226 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
231 * @brief Compare two SSIDs
233 * @param ssid1 A pointer to ssid to compare
234 * @param ssid2 A pointer to ssid to compare
236 * @return 0--ssid is same, otherwise is different
238 int lbs_ssid_cmp(u8 *ssid1, u8 ssid1_len, u8 *ssid2, u8 ssid2_len)
240 if (ssid1_len != ssid2_len)
243 return memcmp(ssid1, ssid2, ssid1_len);
249 /*********************************************************************/
251 /* Main scanning support */
253 /*********************************************************************/
257 * @brief Create a channel list for the driver to scan based on region info
259 * Only used from lbs_scan_setup_scan_config()
261 * Use the driver region/band information to construct a comprehensive list
262 * of channels to scan. This routine is used for any scan that is not
263 * provided a specific channel list to scan.
265 * @param priv A pointer to struct lbs_private structure
266 * @param scanchanlist Output parameter: resulting channel list to scan
267 * @param filteredscan Flag indicating whether or not a BSSID or SSID filter
268 * is being sent in the command to firmware. Used to
269 * increase the number of channels sent in a scan
270 * command and to disable the firmware channel scan
275 static void lbs_scan_create_channel_list(struct lbs_private *priv,
276 struct chanscanparamset * scanchanlist,
280 struct lbs_adapter *adapter = priv->adapter;
281 struct region_channel *scanregion;
282 struct chan_freq_power *cfp;
288 lbs_deb_enter_args(LBS_DEB_SCAN, "filteredscan %d", filteredscan);
292 /* Set the default scan type to the user specified type, will later
293 * be changed to passive on a per channel basis if restricted by
294 * regulatory requirements (11d or 11h)
296 scantype = CMD_SCAN_TYPE_ACTIVE;
298 for (rgnidx = 0; rgnidx < ARRAY_SIZE(adapter->region_channel); rgnidx++) {
299 if (priv->adapter->enable11d &&
300 (adapter->connect_status != LBS_CONNECTED) &&
301 (adapter->mesh_connect_status != LBS_CONNECTED)) {
302 /* Scan all the supported chan for the first scan */
303 if (!adapter->universal_channel[rgnidx].valid)
305 scanregion = &adapter->universal_channel[rgnidx];
307 /* clear the parsed_region_chan for the first scan */
308 memset(&adapter->parsed_region_chan, 0x00,
309 sizeof(adapter->parsed_region_chan));
311 if (!adapter->region_channel[rgnidx].valid)
313 scanregion = &adapter->region_channel[rgnidx];
317 nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
319 cfp = scanregion->CFP + nextchan;
321 if (priv->adapter->enable11d) {
323 lbs_get_scan_type_11d(cfp->channel,
328 switch (scanregion->band) {
332 scanchanlist[chanidx].radiotype =
333 CMD_SCAN_RADIO_TYPE_BG;
337 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
338 scanchanlist[chanidx].maxscantime =
339 cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
340 scanchanlist[chanidx].chanscanmode.passivescan =
343 scanchanlist[chanidx].maxscantime =
344 cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
345 scanchanlist[chanidx].chanscanmode.passivescan =
349 scanchanlist[chanidx].channumber = cfp->channel;
352 scanchanlist[chanidx].chanscanmode.
360 /* Delayed partial scan worker */
361 void lbs_scan_worker(struct work_struct *work)
363 struct lbs_private *priv = container_of(work,
367 lbs_scan_networks(priv, NULL, 0);
372 * @brief Construct a lbs_scan_cmd_config structure to use in issue scan cmds
374 * Application layer or other functions can invoke lbs_scan_networks
375 * with a scan configuration supplied in a lbs_ioctl_user_scan_cfg struct.
376 * This structure is used as the basis of one or many lbs_scan_cmd_config
377 * commands that are sent to the command processing module and sent to
380 * Create a lbs_scan_cmd_config based on the following user supplied
381 * parameters (if present):
384 * - Number of Probes to be sent
387 * If the SSID or BSSID filter is not present, disable/clear the filter.
388 * If the number of probes is not set, use the adapter default setting
389 * Qualify the channel
391 * @param priv A pointer to struct lbs_private structure
392 * @param puserscanin NULL or pointer to scan configuration parameters
393 * @param ppchantlvout Output parameter: Pointer to the start of the
394 * channel TLV portion of the output scan config
395 * @param pscanchanlist Output parameter: Pointer to the resulting channel
397 * @param pmaxchanperscan Output parameter: Number of channels to scan for
398 * each issuance of the firmware scan command
399 * @param pfilteredscan Output parameter: Flag indicating whether or not
400 * a BSSID or SSID filter is being sent in the
401 * command to firmware. Used to increase the number
402 * of channels sent in a scan command and to
403 * disable the firmware channel scan filter.
404 * @param pscancurrentonly Output parameter: Flag indicating whether or not
405 * we are only scanning our current active channel
407 * @return resulting scan configuration
409 static struct lbs_scan_cmd_config *
410 lbs_scan_setup_scan_config(struct lbs_private *priv,
411 const struct lbs_ioctl_user_scan_cfg *puserscanin,
412 struct mrvlietypes_chanlistparamset ** ppchantlvout,
413 struct chanscanparamset * pscanchanlist,
414 int *pmaxchanperscan,
416 u8 * pscancurrentonly)
418 struct mrvlietypes_numprobes *pnumprobestlv;
419 struct mrvlietypes_ssidparamset *pssidtlv;
420 struct lbs_scan_cmd_config *pscancfgout = NULL;
429 lbs_deb_enter(LBS_DEB_SCAN);
431 pscancfgout = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
432 if (pscancfgout == NULL)
435 /* The tlvbufferlen is calculated for each scan command. The TLVs added
436 * in this routine will be preserved since the routine that sends
437 * the command will append channelTLVs at *ppchantlvout. The difference
438 * between the *ppchantlvout and the tlvbuffer start will be used
439 * to calculate the size of anything we add in this routine.
441 pscancfgout->tlvbufferlen = 0;
443 /* Running tlv pointer. Assigned to ppchantlvout at end of function
444 * so later routines know where channels can be added to the command buf
446 ptlvpos = pscancfgout->tlvbuffer;
449 * Set the initial scan paramters for progressive scanning. If a specific
450 * BSSID or SSID is used, the number of channels in the scan command
451 * will be increased to the absolute maximum
453 *pmaxchanperscan = MRVDRV_CHANNELS_PER_SCAN_CMD;
455 /* Initialize the scan as un-filtered by firmware, set to TRUE below if
456 * a SSID or BSSID filter is sent in the command
460 /* Initialize the scan as not being only on the current channel. If
461 * the channel list is customized, only contains one channel, and
462 * is the active channel, this is set true and data flow is not halted.
464 *pscancurrentonly = 0;
467 /* Set the bss type scan filter, use adapter setting if unset */
468 pscancfgout->bsstype =
469 puserscanin->bsstype ? puserscanin->bsstype : CMD_BSS_TYPE_ANY;
471 /* Set the number of probes to send, use adapter setting if unset */
472 numprobes = puserscanin->numprobes ? puserscanin->numprobes : 0;
475 * Set the BSSID filter to the incoming configuration,
476 * if non-zero. If not set, it will remain disabled (all zeros).
478 memcpy(pscancfgout->bssid, puserscanin->bssid,
479 sizeof(pscancfgout->bssid));
481 if (puserscanin->ssid_len) {
483 (struct mrvlietypes_ssidparamset *) pscancfgout->
485 pssidtlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
486 pssidtlv->header.len = cpu_to_le16(puserscanin->ssid_len);
487 memcpy(pssidtlv->ssid, puserscanin->ssid,
488 puserscanin->ssid_len);
489 ptlvpos += sizeof(pssidtlv->header) + puserscanin->ssid_len;
493 * The default number of channels sent in the command is low to
494 * ensure the response buffer from the firmware does not truncate
495 * scan results. That is not an issue with an SSID or BSSID
496 * filter applied to the scan results in the firmware.
498 if ( puserscanin->ssid_len
499 || (compare_ether_addr(pscancfgout->bssid, &zeromac[0]) != 0)) {
500 *pmaxchanperscan = MRVDRV_MAX_CHANNELS_PER_SCAN;
504 pscancfgout->bsstype = CMD_BSS_TYPE_ANY;
508 /* If the input config or adapter has the number of Probes set, add tlv */
510 pnumprobestlv = (struct mrvlietypes_numprobes *) ptlvpos;
511 pnumprobestlv->header.type = cpu_to_le16(TLV_TYPE_NUMPROBES);
512 pnumprobestlv->header.len = cpu_to_le16(2);
513 pnumprobestlv->numprobes = cpu_to_le16(numprobes);
515 ptlvpos += sizeof(*pnumprobestlv);
519 * Set the output for the channel TLV to the address in the tlv buffer
520 * past any TLVs that were added in this fuction (SSID, numprobes).
521 * channel TLVs will be added past this for each scan command, preserving
522 * the TLVs that were previously added.
524 *ppchantlvout = (struct mrvlietypes_chanlistparamset *) ptlvpos;
526 if (!puserscanin || !puserscanin->chanlist[0].channumber) {
527 /* Create a default channel scan list */
528 lbs_deb_scan("creating full region channel list\n");
529 lbs_scan_create_channel_list(priv, pscanchanlist,
535 chanidx < LBS_IOCTL_USER_SCAN_CHAN_MAX
536 && puserscanin->chanlist[chanidx].channumber; chanidx++) {
538 channel = puserscanin->chanlist[chanidx].channumber;
539 (pscanchanlist + chanidx)->channumber = channel;
541 radiotype = puserscanin->chanlist[chanidx].radiotype;
542 (pscanchanlist + chanidx)->radiotype = radiotype;
544 scantype = puserscanin->chanlist[chanidx].scantype;
546 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
548 chanidx)->chanscanmode.passivescan = 1;
551 chanidx)->chanscanmode.passivescan = 0;
554 if (puserscanin->chanlist[chanidx].scantime) {
555 scandur = puserscanin->chanlist[chanidx].scantime;
557 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
558 scandur = MRVDRV_PASSIVE_SCAN_CHAN_TIME;
560 scandur = MRVDRV_ACTIVE_SCAN_CHAN_TIME;
564 (pscanchanlist + chanidx)->minscantime =
565 cpu_to_le16(scandur);
566 (pscanchanlist + chanidx)->maxscantime =
567 cpu_to_le16(scandur);
570 /* Check if we are only scanning the current channel */
571 if ((chanidx == 1) &&
572 (puserscanin->chanlist[0].channumber ==
573 priv->adapter->curbssparams.channel)) {
574 *pscancurrentonly = 1;
575 lbs_deb_scan("scanning current channel only");
583 * @brief Construct and send multiple scan config commands to the firmware
585 * Only used from lbs_scan_networks()
587 * Previous routines have created a lbs_scan_cmd_config with any requested
588 * TLVs. This function splits the channel TLV into maxchanperscan lists
589 * and sends the portion of the channel TLV along with the other TLVs
590 * to the lbs_cmd routines for execution in the firmware.
592 * @param priv A pointer to struct lbs_private structure
593 * @param maxchanperscan Maximum number channels to be included in each
594 * scan command sent to firmware
595 * @param filteredscan Flag indicating whether or not a BSSID or SSID
596 * filter is being used for the firmware command
597 * scan command sent to firmware
598 * @param pscancfgout Scan configuration used for this scan.
599 * @param pchantlvout Pointer in the pscancfgout where the channel TLV
600 * should start. This is past any other TLVs that
601 * must be sent down in each firmware command.
602 * @param pscanchanlist List of channels to scan in maxchanperscan segments
604 * @return 0 or error return otherwise
606 static int lbs_scan_channel_list(struct lbs_private *priv,
609 struct lbs_scan_cmd_config *pscancfgout,
610 struct mrvlietypes_chanlistparamset * pchantlvout,
611 struct chanscanparamset * pscanchanlist,
612 const struct lbs_ioctl_user_scan_cfg *puserscanin,
615 struct chanscanparamset *ptmpchan;
616 struct chanscanparamset *pstartchan;
622 union iwreq_data wrqu;
624 lbs_deb_enter_args(LBS_DEB_SCAN, "maxchanperscan %d, filteredscan %d, "
625 "full_scan %d", maxchanperscan, filteredscan, full_scan);
627 if (!pscancfgout || !pchantlvout || !pscanchanlist) {
628 lbs_deb_scan("pscancfgout, pchantlvout or "
629 "pscanchanlist is NULL\n");
634 pchantlvout->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
636 /* Set the temp channel struct pointer to the start of the desired list */
637 ptmpchan = pscanchanlist;
639 if (priv->adapter->last_scanned_channel && !puserscanin)
640 ptmpchan += priv->adapter->last_scanned_channel;
642 /* Loop through the desired channel list, sending a new firmware scan
643 * commands for each maxchanperscan channels (or for 1,6,11 individually
644 * if configured accordingly)
646 while (ptmpchan->channumber) {
649 pchantlvout->header.len = 0;
650 scanband = ptmpchan->radiotype;
651 pstartchan = ptmpchan;
654 /* Construct the channel TLV for the scan command. Continue to
655 * insert channel TLVs until:
656 * - the tlvidx hits the maximum configured per scan command
657 * - the next channel to insert is 0 (end of desired channel list)
658 * - doneearly is set (controlling individual scanning of 1,6,11)
660 while (tlvidx < maxchanperscan && ptmpchan->channumber
661 && !doneearly && scanned < 2) {
663 lbs_deb_scan("channel %d, radio %d, passive %d, "
664 "dischanflt %d, maxscantime %d\n",
665 ptmpchan->channumber,
667 ptmpchan->chanscanmode.passivescan,
668 ptmpchan->chanscanmode.disablechanfilt,
669 ptmpchan->maxscantime);
671 /* Copy the current channel TLV to the command being prepared */
672 memcpy(pchantlvout->chanscanparam + tlvidx,
673 ptmpchan, sizeof(pchantlvout->chanscanparam));
675 /* Increment the TLV header length by the size appended */
676 /* Ew, it would be _so_ nice if we could just declare the
677 variable little-endian and let GCC handle it for us */
678 pchantlvout->header.len =
679 cpu_to_le16(le16_to_cpu(pchantlvout->header.len) +
680 sizeof(pchantlvout->chanscanparam));
683 * The tlv buffer length is set to the number of bytes of the
684 * between the channel tlv pointer and the start of the
685 * tlv buffer. This compensates for any TLVs that were appended
686 * before the channel list.
688 pscancfgout->tlvbufferlen = ((u8 *) pchantlvout
689 - pscancfgout->tlvbuffer);
691 /* Add the size of the channel tlv header and the data length */
692 pscancfgout->tlvbufferlen +=
693 (sizeof(pchantlvout->header)
694 + le16_to_cpu(pchantlvout->header.len));
696 /* Increment the index to the channel tlv we are constructing */
701 /* Stop the loop if the *current* channel is in the 1,6,11 set
702 * and we are not filtering on a BSSID or SSID.
704 if (!filteredscan && (ptmpchan->channumber == 1
705 || ptmpchan->channumber == 6
706 || ptmpchan->channumber == 11)) {
710 /* Increment the tmp pointer to the next channel to be scanned */
714 /* Stop the loop if the *next* channel is in the 1,6,11 set.
715 * This will cause it to be the only channel scanned on the next
718 if (!filteredscan && (ptmpchan->channumber == 1
719 || ptmpchan->channumber == 6
720 || ptmpchan->channumber == 11)) {
725 /* Send the scan command to the firmware with the specified cfg */
726 ret = lbs_prepare_and_send_command(priv, CMD_802_11_SCAN, 0,
728 if (scanned >= 2 && !full_scan) {
736 priv->adapter->last_scanned_channel = ptmpchan->channumber;
738 if (priv->adapter->last_scanned_channel) {
739 /* Schedule the next part of the partial scan */
740 if (!full_scan && !priv->adapter->surpriseremoved) {
741 cancel_delayed_work(&priv->scan_work);
742 queue_delayed_work(priv->work_thread, &priv->scan_work,
743 msecs_to_jiffies(300));
746 /* All done, tell userspace the scan table has been updated */
747 memset(&wrqu, 0, sizeof(union iwreq_data));
748 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
752 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
757 * Only used from lbs_scan_networks()
759 static void clear_selected_scan_list_entries(struct lbs_adapter *adapter,
760 const struct lbs_ioctl_user_scan_cfg *scan_cfg)
762 struct bss_descriptor *bss;
763 struct bss_descriptor *safe;
764 u32 clear_ssid_flag = 0, clear_bssid_flag = 0;
766 lbs_deb_enter(LBS_DEB_SCAN);
771 if (scan_cfg->clear_ssid && scan_cfg->ssid_len)
774 if (scan_cfg->clear_bssid
775 && (compare_ether_addr(scan_cfg->bssid, &zeromac[0]) != 0)
776 && (compare_ether_addr(scan_cfg->bssid, &bcastmac[0]) != 0)) {
777 clear_bssid_flag = 1;
780 if (!clear_ssid_flag && !clear_bssid_flag)
783 mutex_lock(&adapter->lock);
784 list_for_each_entry_safe (bss, safe, &adapter->network_list, list) {
787 /* Check for an SSID match */
789 && (bss->ssid_len == scan_cfg->ssid_len)
790 && !memcmp(bss->ssid, scan_cfg->ssid, bss->ssid_len))
793 /* Check for a BSSID match */
794 if ( clear_bssid_flag
795 && !compare_ether_addr(bss->bssid, scan_cfg->bssid))
799 list_move_tail (&bss->list, &adapter->network_free_list);
800 clear_bss_descriptor(bss);
803 mutex_unlock(&adapter->lock);
805 lbs_deb_leave(LBS_DEB_SCAN);
810 * @brief Internal function used to start a scan based on an input config
812 * Also used from debugfs
814 * Use the input user scan configuration information when provided in
815 * order to send the appropriate scan commands to firmware to populate or
816 * update the internal driver scan table
818 * @param priv A pointer to struct lbs_private structure
819 * @param puserscanin Pointer to the input configuration for the requested
821 * @param full_scan ???
823 * @return 0 or < 0 if error
825 int lbs_scan_networks(struct lbs_private *priv,
826 const struct lbs_ioctl_user_scan_cfg *puserscanin,
829 struct lbs_adapter *adapter = priv->adapter;
830 struct mrvlietypes_chanlistparamset *pchantlvout;
831 struct chanscanparamset * scan_chan_list = NULL;
832 struct lbs_scan_cmd_config *scan_cfg = NULL;
834 u8 scancurrentchanonly;
837 #ifdef CONFIG_LIBERTAS_DEBUG
838 struct bss_descriptor * iter_bss;
840 DECLARE_MAC_BUF(mac);
843 lbs_deb_enter_args(LBS_DEB_SCAN, "full_scan %d", full_scan);
845 /* Cancel any partial outstanding partial scans if this scan
848 if (full_scan && delayed_work_pending(&priv->scan_work))
849 cancel_delayed_work(&priv->scan_work);
851 scan_chan_list = kzalloc(sizeof(struct chanscanparamset) *
852 LBS_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
853 if (scan_chan_list == NULL) {
858 scan_cfg = lbs_scan_setup_scan_config(priv,
864 &scancurrentchanonly);
865 if (scan_cfg == NULL) {
870 clear_selected_scan_list_entries(adapter, puserscanin);
872 /* Keep the data path active if we are only scanning our current channel */
873 if (!scancurrentchanonly) {
874 netif_stop_queue(priv->dev);
875 netif_carrier_off(priv->dev);
876 if (priv->mesh_dev) {
877 netif_stop_queue(priv->mesh_dev);
878 netif_carrier_off(priv->mesh_dev);
882 ret = lbs_scan_channel_list(priv,
891 #ifdef CONFIG_LIBERTAS_DEBUG
892 /* Dump the scan table */
893 mutex_lock(&adapter->lock);
894 lbs_deb_scan("The scan table contains:\n");
895 list_for_each_entry (iter_bss, &adapter->network_list, list) {
896 lbs_deb_scan("scan %02d, %s, RSSI, %d, SSID '%s'\n",
897 i++, print_mac(mac, iter_bss->bssid), (s32) iter_bss->rssi,
898 escape_essid(iter_bss->ssid, iter_bss->ssid_len));
900 mutex_unlock(&adapter->lock);
903 if (adapter->connect_status == LBS_CONNECTED) {
904 netif_carrier_on(priv->dev);
905 netif_wake_queue(priv->dev);
908 if (priv->mesh_dev && (adapter->mesh_connect_status == LBS_CONNECTED)) {
909 netif_carrier_on(priv->mesh_dev);
910 netif_wake_queue(priv->mesh_dev);
918 kfree(scan_chan_list);
920 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
925 * @brief Interpret a BSS scan response returned from the firmware
927 * Parse the various fixed fields and IEs passed back for a a BSS probe
928 * response or beacon from the scan command. Record information as needed
929 * in the scan table struct bss_descriptor for that entry.
931 * @param bss Output parameter: Pointer to the BSS Entry
935 static int lbs_process_bss(struct bss_descriptor *bss,
936 u8 ** pbeaconinfo, int *bytesleft)
938 struct ieeetypes_fhparamset *pFH;
939 struct ieeetypes_dsparamset *pDS;
940 struct ieeetypes_cfparamset *pCF;
941 struct ieeetypes_ibssparamset *pibss;
942 DECLARE_MAC_BUF(mac);
943 struct ieeetypes_countryinfoset *pcountryinfo;
945 u8 n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
949 lbs_deb_enter(LBS_DEB_SCAN);
951 if (*bytesleft >= sizeof(beaconsize)) {
952 /* Extract & convert beacon size from the command buffer */
953 beaconsize = le16_to_cpu(get_unaligned((u16 *)*pbeaconinfo));
954 *bytesleft -= sizeof(beaconsize);
955 *pbeaconinfo += sizeof(beaconsize);
958 if (beaconsize == 0 || beaconsize > *bytesleft) {
959 *pbeaconinfo += *bytesleft;
965 /* Initialize the current working beacon pointer for this BSS iteration */
967 end = pos + beaconsize;
969 /* Advance the return beacon pointer past the current beacon */
970 *pbeaconinfo += beaconsize;
971 *bytesleft -= beaconsize;
973 memcpy(bss->bssid, pos, ETH_ALEN);
974 lbs_deb_scan("process_bss: AP BSSID %s\n", print_mac(mac, bss->bssid));
977 if ((end - pos) < 12) {
978 lbs_deb_scan("process_bss: Not enough bytes left\n");
984 * next 4 fields are RSSI, time stamp, beacon interval,
985 * and capability information
988 /* RSSI is 1 byte long */
990 lbs_deb_scan("process_bss: RSSI=%02X\n", *pos);
993 /* time stamp is 8 bytes long */
996 /* beacon interval is 2 bytes long */
997 bss->beaconperiod = le16_to_cpup((void *) pos);
1000 /* capability information is 2 bytes long */
1001 bss->capability = le16_to_cpup((void *) pos);
1002 lbs_deb_scan("process_bss: capabilities = 0x%4X\n", bss->capability);
1005 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
1006 lbs_deb_scan("process_bss: AP WEP enabled\n");
1007 if (bss->capability & WLAN_CAPABILITY_IBSS)
1008 bss->mode = IW_MODE_ADHOC;
1010 bss->mode = IW_MODE_INFRA;
1012 /* rest of the current buffer are IE's */
1013 lbs_deb_scan("process_bss: IE length for this AP = %zd\n", end - pos);
1014 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
1016 /* process variable IE */
1017 while (pos <= end - 2) {
1018 struct ieee80211_info_element * elem =
1019 (struct ieee80211_info_element *) pos;
1021 if (pos + elem->len > end) {
1022 lbs_deb_scan("process_bss: error in processing IE, "
1023 "bytes left < IE length\n");
1028 case MFIE_TYPE_SSID:
1029 bss->ssid_len = elem->len;
1030 memcpy(bss->ssid, elem->data, elem->len);
1031 lbs_deb_scan("ssid '%s', ssid length %u\n",
1032 escape_essid(bss->ssid, bss->ssid_len),
1036 case MFIE_TYPE_RATES:
1037 n_basic_rates = min_t(u8, MAX_RATES, elem->len);
1038 memcpy(bss->rates, elem->data, n_basic_rates);
1039 got_basic_rates = 1;
1042 case MFIE_TYPE_FH_SET:
1043 pFH = (struct ieeetypes_fhparamset *) pos;
1044 memmove(&bss->phyparamset.fhparamset, pFH,
1045 sizeof(struct ieeetypes_fhparamset));
1046 #if 0 /* I think we can store these LE */
1047 bss->phyparamset.fhparamset.dwelltime
1048 = le16_to_cpu(bss->phyparamset.fhparamset.dwelltime);
1052 case MFIE_TYPE_DS_SET:
1053 pDS = (struct ieeetypes_dsparamset *) pos;
1054 bss->channel = pDS->currentchan;
1055 memcpy(&bss->phyparamset.dsparamset, pDS,
1056 sizeof(struct ieeetypes_dsparamset));
1059 case MFIE_TYPE_CF_SET:
1060 pCF = (struct ieeetypes_cfparamset *) pos;
1061 memcpy(&bss->ssparamset.cfparamset, pCF,
1062 sizeof(struct ieeetypes_cfparamset));
1065 case MFIE_TYPE_IBSS_SET:
1066 pibss = (struct ieeetypes_ibssparamset *) pos;
1067 bss->atimwindow = le32_to_cpu(pibss->atimwindow);
1068 memmove(&bss->ssparamset.ibssparamset, pibss,
1069 sizeof(struct ieeetypes_ibssparamset));
1071 bss->ssparamset.ibssparamset.atimwindow
1072 = le16_to_cpu(bss->ssparamset.ibssparamset.atimwindow);
1076 case MFIE_TYPE_COUNTRY:
1077 pcountryinfo = (struct ieeetypes_countryinfoset *) pos;
1078 if (pcountryinfo->len < sizeof(pcountryinfo->countrycode)
1079 || pcountryinfo->len > 254) {
1080 lbs_deb_scan("process_bss: 11D- Err "
1081 "CountryInfo len =%d min=%zd max=254\n",
1083 sizeof(pcountryinfo->countrycode));
1088 memcpy(&bss->countryinfo,
1089 pcountryinfo, pcountryinfo->len + 2);
1090 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: 11d countryinfo",
1091 (u8 *) pcountryinfo,
1092 (u32) (pcountryinfo->len + 2));
1095 case MFIE_TYPE_RATES_EX:
1096 /* only process extended supported rate if data rate is
1097 * already found. Data rate IE should come before
1098 * extended supported rate IE
1100 if (!got_basic_rates)
1103 n_ex_rates = elem->len;
1104 if (n_basic_rates + n_ex_rates > MAX_RATES)
1105 n_ex_rates = MAX_RATES - n_basic_rates;
1107 p = bss->rates + n_basic_rates;
1108 memcpy(p, elem->data, n_ex_rates);
1111 case MFIE_TYPE_GENERIC:
1112 if (elem->len >= 4 &&
1113 elem->data[0] == 0x00 &&
1114 elem->data[1] == 0x50 &&
1115 elem->data[2] == 0xf2 &&
1116 elem->data[3] == 0x01) {
1117 bss->wpa_ie_len = min(elem->len + 2,
1119 memcpy(bss->wpa_ie, elem, bss->wpa_ie_len);
1120 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: WPA IE", bss->wpa_ie,
1122 } else if (elem->len >= MARVELL_MESH_IE_LENGTH &&
1123 elem->data[0] == 0x00 &&
1124 elem->data[1] == 0x50 &&
1125 elem->data[2] == 0x43 &&
1126 elem->data[3] == 0x04) {
1132 bss->rsn_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
1133 memcpy(bss->rsn_ie, elem, bss->rsn_ie_len);
1134 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE", bss->rsn_ie, elem->len);
1141 pos += elem->len + 2;
1145 bss->last_scanned = jiffies;
1146 lbs_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
1151 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1156 * @brief This function finds a specific compatible BSSID in the scan list
1158 * Used in association code
1160 * @param adapter A pointer to struct lbs_adapter
1161 * @param bssid BSSID to find in the scan list
1162 * @param mode Network mode: Infrastructure or IBSS
1164 * @return index in BSSID list, or error return code (< 0)
1166 struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_adapter *adapter,
1167 u8 * bssid, u8 mode)
1169 struct bss_descriptor * iter_bss;
1170 struct bss_descriptor * found_bss = NULL;
1172 lbs_deb_enter(LBS_DEB_SCAN);
1177 lbs_deb_hex(LBS_DEB_SCAN, "looking for",
1180 /* Look through the scan table for a compatible match. The loop will
1181 * continue past a matched bssid that is not compatible in case there
1182 * is an AP with multiple SSIDs assigned to the same BSSID
1184 mutex_lock(&adapter->lock);
1185 list_for_each_entry (iter_bss, &adapter->network_list, list) {
1186 if (compare_ether_addr(iter_bss->bssid, bssid))
1187 continue; /* bssid doesn't match */
1191 if (!is_network_compatible(adapter, iter_bss, mode))
1193 found_bss = iter_bss;
1196 found_bss = iter_bss;
1200 mutex_unlock(&adapter->lock);
1203 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
1208 * @brief This function finds ssid in ssid list.
1210 * Used in association code
1212 * @param adapter A pointer to struct lbs_adapter
1213 * @param ssid SSID to find in the list
1214 * @param bssid BSSID to qualify the SSID selection (if provided)
1215 * @param mode Network mode: Infrastructure or IBSS
1217 * @return index in BSSID list
1219 struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_adapter *adapter,
1220 u8 *ssid, u8 ssid_len, u8 * bssid, u8 mode,
1224 struct bss_descriptor * iter_bss = NULL;
1225 struct bss_descriptor * found_bss = NULL;
1226 struct bss_descriptor * tmp_oldest = NULL;
1228 lbs_deb_enter(LBS_DEB_SCAN);
1230 mutex_lock(&adapter->lock);
1232 list_for_each_entry (iter_bss, &adapter->network_list, list) {
1234 || (iter_bss->last_scanned < tmp_oldest->last_scanned))
1235 tmp_oldest = iter_bss;
1237 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
1238 ssid, ssid_len) != 0)
1239 continue; /* ssid doesn't match */
1240 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
1241 continue; /* bssid doesn't match */
1242 if ((channel > 0) && (iter_bss->channel != channel))
1243 continue; /* channel doesn't match */
1248 if (!is_network_compatible(adapter, iter_bss, mode))
1252 /* Found requested BSSID */
1253 found_bss = iter_bss;
1257 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1258 bestrssi = SCAN_RSSI(iter_bss->rssi);
1259 found_bss = iter_bss;
1264 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1265 bestrssi = SCAN_RSSI(iter_bss->rssi);
1266 found_bss = iter_bss;
1273 mutex_unlock(&adapter->lock);
1274 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
1279 * @brief This function finds the best SSID in the Scan List
1281 * Search the scan table for the best SSID that also matches the current
1282 * adapter network preference (infrastructure or adhoc)
1284 * @param adapter A pointer to struct lbs_adapter
1286 * @return index in BSSID list
1288 static struct bss_descriptor *lbs_find_best_ssid_in_list(
1289 struct lbs_adapter *adapter,
1293 struct bss_descriptor * iter_bss;
1294 struct bss_descriptor * best_bss = NULL;
1296 lbs_deb_enter(LBS_DEB_SCAN);
1298 mutex_lock(&adapter->lock);
1300 list_for_each_entry (iter_bss, &adapter->network_list, list) {
1304 if (!is_network_compatible(adapter, iter_bss, mode))
1306 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1308 bestrssi = SCAN_RSSI(iter_bss->rssi);
1309 best_bss = iter_bss;
1313 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1315 bestrssi = SCAN_RSSI(iter_bss->rssi);
1316 best_bss = iter_bss;
1321 mutex_unlock(&adapter->lock);
1322 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
1327 * @brief Find the AP with specific ssid in the scan list
1329 * Used from association worker.
1331 * @param priv A pointer to struct lbs_private structure
1332 * @param pSSID A pointer to AP's ssid
1334 * @return 0--success, otherwise--fail
1336 int lbs_find_best_network_ssid(struct lbs_private *priv,
1337 u8 *out_ssid, u8 *out_ssid_len, u8 preferred_mode, u8 *out_mode)
1339 struct lbs_adapter *adapter = priv->adapter;
1341 struct bss_descriptor * found;
1343 lbs_deb_enter(LBS_DEB_SCAN);
1345 lbs_scan_networks(priv, NULL, 1);
1346 if (adapter->surpriseremoved)
1349 wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
1351 found = lbs_find_best_ssid_in_list(adapter, preferred_mode);
1352 if (found && (found->ssid_len > 0)) {
1353 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1354 *out_ssid_len = found->ssid_len;
1355 *out_mode = found->mode;
1360 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1365 * @brief Scan Network
1367 * @param dev A pointer to net_device structure
1368 * @param info A pointer to iw_request_info structure
1369 * @param vwrq A pointer to iw_param structure
1370 * @param extra A pointer to extra data buf
1372 * @return 0 --success, otherwise fail
1374 int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
1375 struct iw_param *vwrq, char *extra)
1377 struct lbs_private *priv = dev->priv;
1378 struct lbs_adapter *adapter = priv->adapter;
1380 lbs_deb_enter(LBS_DEB_SCAN);
1382 if (!delayed_work_pending(&priv->scan_work)) {
1383 queue_delayed_work(priv->work_thread, &priv->scan_work,
1384 msecs_to_jiffies(50));
1387 if (adapter->surpriseremoved)
1390 lbs_deb_leave(LBS_DEB_SCAN);
1396 * @brief Send a scan command for all available channels filtered on a spec
1398 * Used in association code and from debugfs
1400 * @param priv A pointer to struct lbs_private structure
1401 * @param ssid A pointer to the SSID to scan for
1402 * @param ssid_len Length of the SSID
1403 * @param clear_ssid Should existing scan results with this SSID
1405 * @param prequestedssid A pointer to AP's ssid
1406 * @param keeppreviousscan Flag used to save/clear scan table before scan
1408 * @return 0-success, otherwise fail
1410 int lbs_send_specific_ssid_scan(struct lbs_private *priv,
1411 u8 *ssid, u8 ssid_len, u8 clear_ssid)
1413 struct lbs_adapter *adapter = priv->adapter;
1414 struct lbs_ioctl_user_scan_cfg scancfg;
1417 lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s', clear %d",
1418 escape_essid(ssid, ssid_len), clear_ssid);
1423 memset(&scancfg, 0x00, sizeof(scancfg));
1424 memcpy(scancfg.ssid, ssid, ssid_len);
1425 scancfg.ssid_len = ssid_len;
1426 scancfg.clear_ssid = clear_ssid;
1428 lbs_scan_networks(priv, &scancfg, 1);
1429 if (adapter->surpriseremoved) {
1433 wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
1436 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1443 /*********************************************************************/
1445 /* Support for Wireless Extensions */
1447 /*********************************************************************/
1449 #define MAX_CUSTOM_LEN 64
1451 static inline char *lbs_translate_scan(struct lbs_private *priv,
1452 char *start, char *stop,
1453 struct bss_descriptor *bss)
1455 struct lbs_adapter *adapter = priv->adapter;
1456 struct chan_freq_power *cfp;
1457 char *current_val; /* For rates */
1458 struct iw_event iwe; /* Temporary buffer */
1460 #define PERFECT_RSSI ((u8)50)
1461 #define WORST_RSSI ((u8)0)
1462 #define RSSI_DIFF ((u8)(PERFECT_RSSI - WORST_RSSI))
1465 lbs_deb_enter(LBS_DEB_SCAN);
1467 cfp = lbs_find_cfp_by_band_and_channel(adapter, 0, bss->channel);
1469 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
1474 /* First entry *MUST* be the AP BSSID */
1475 iwe.cmd = SIOCGIWAP;
1476 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1477 memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
1478 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_ADDR_LEN);
1481 iwe.cmd = SIOCGIWESSID;
1482 iwe.u.data.flags = 1;
1483 iwe.u.data.length = min((u32) bss->ssid_len, (u32) IW_ESSID_MAX_SIZE);
1484 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
1487 iwe.cmd = SIOCGIWMODE;
1488 iwe.u.mode = bss->mode;
1489 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_UINT_LEN);
1492 iwe.cmd = SIOCGIWFREQ;
1493 iwe.u.freq.m = (long)cfp->freq * 100000;
1495 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_FREQ_LEN);
1497 /* Add quality statistics */
1499 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
1500 iwe.u.qual.level = SCAN_RSSI(bss->rssi);
1502 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
1504 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
1505 (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
1506 (RSSI_DIFF * RSSI_DIFF);
1507 if (iwe.u.qual.qual > 100)
1508 iwe.u.qual.qual = 100;
1510 if (adapter->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
1511 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
1514 CAL_NF(adapter->NF[TYPE_BEACON][TYPE_NOAVG]);
1517 /* Locally created ad-hoc BSSs won't have beacons if this is the
1518 * only station in the adhoc network; so get signal strength
1519 * from receive statistics.
1521 if ((adapter->mode == IW_MODE_ADHOC)
1522 && adapter->adhoccreate
1523 && !lbs_ssid_cmp(adapter->curbssparams.ssid,
1524 adapter->curbssparams.ssid_len,
1525 bss->ssid, bss->ssid_len)) {
1527 snr = adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
1528 nf = adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
1529 iwe.u.qual.level = CAL_RSSI(snr, nf);
1531 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_QUAL_LEN);
1533 /* Add encryption capability */
1534 iwe.cmd = SIOCGIWENCODE;
1535 if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
1536 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1538 iwe.u.data.flags = IW_ENCODE_DISABLED;
1540 iwe.u.data.length = 0;
1541 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
1543 current_val = start + IW_EV_LCP_LEN;
1545 iwe.cmd = SIOCGIWRATE;
1546 iwe.u.bitrate.fixed = 0;
1547 iwe.u.bitrate.disabled = 0;
1548 iwe.u.bitrate.value = 0;
1550 for (j = 0; bss->rates[j] && (j < sizeof(bss->rates)); j++) {
1551 /* Bit rate given in 500 kb/s units */
1552 iwe.u.bitrate.value = bss->rates[j] * 500000;
1553 current_val = iwe_stream_add_value(start, current_val,
1554 stop, &iwe, IW_EV_PARAM_LEN);
1556 if ((bss->mode == IW_MODE_ADHOC)
1557 && !lbs_ssid_cmp(adapter->curbssparams.ssid,
1558 adapter->curbssparams.ssid_len,
1559 bss->ssid, bss->ssid_len)
1560 && adapter->adhoccreate) {
1561 iwe.u.bitrate.value = 22 * 500000;
1562 current_val = iwe_stream_add_value(start, current_val,
1563 stop, &iwe, IW_EV_PARAM_LEN);
1565 /* Check if we added any event */
1566 if((current_val - start) > IW_EV_LCP_LEN)
1567 start = current_val;
1569 memset(&iwe, 0, sizeof(iwe));
1570 if (bss->wpa_ie_len) {
1571 char buf[MAX_WPA_IE_LEN];
1572 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
1573 iwe.cmd = IWEVGENIE;
1574 iwe.u.data.length = bss->wpa_ie_len;
1575 start = iwe_stream_add_point(start, stop, &iwe, buf);
1578 memset(&iwe, 0, sizeof(iwe));
1579 if (bss->rsn_ie_len) {
1580 char buf[MAX_WPA_IE_LEN];
1581 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
1582 iwe.cmd = IWEVGENIE;
1583 iwe.u.data.length = bss->rsn_ie_len;
1584 start = iwe_stream_add_point(start, stop, &iwe, buf);
1588 char custom[MAX_CUSTOM_LEN];
1591 iwe.cmd = IWEVCUSTOM;
1592 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
1594 iwe.u.data.length = p - custom;
1595 if (iwe.u.data.length)
1596 start = iwe_stream_add_point(start, stop, &iwe, custom);
1600 lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
1605 * @brief Handle Retrieve scan table ioctl
1607 * @param dev A pointer to net_device structure
1608 * @param info A pointer to iw_request_info structure
1609 * @param dwrq A pointer to iw_point structure
1610 * @param extra A pointer to extra data buf
1612 * @return 0 --success, otherwise fail
1614 int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
1615 struct iw_point *dwrq, char *extra)
1617 #define SCAN_ITEM_SIZE 128
1618 struct lbs_private *priv = dev->priv;
1619 struct lbs_adapter *adapter = priv->adapter;
1622 char *stop = ev + dwrq->length;
1623 struct bss_descriptor * iter_bss;
1624 struct bss_descriptor * safe;
1626 lbs_deb_enter(LBS_DEB_SCAN);
1628 /* Update RSSI if current BSS is a locally created ad-hoc BSS */
1629 if ((adapter->mode == IW_MODE_ADHOC) && adapter->adhoccreate) {
1630 lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
1631 CMD_OPTION_WAITFORRSP, 0, NULL);
1634 mutex_lock(&adapter->lock);
1635 list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
1637 unsigned long stale_time;
1639 if (stop - ev < SCAN_ITEM_SIZE) {
1644 /* For mesh device, list only mesh networks */
1645 if (dev == priv->mesh_dev && !iter_bss->mesh)
1648 /* Prune old an old scan result */
1649 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1650 if (time_after(jiffies, stale_time)) {
1651 list_move_tail (&iter_bss->list,
1652 &adapter->network_free_list);
1653 clear_bss_descriptor(iter_bss);
1657 /* Translate to WE format this entry */
1658 next_ev = lbs_translate_scan(priv, ev, stop, iter_bss);
1659 if (next_ev == NULL)
1663 mutex_unlock(&adapter->lock);
1665 dwrq->length = (ev - extra);
1668 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", err);
1675 /*********************************************************************/
1677 /* Command execution */
1679 /*********************************************************************/
1683 * @brief Prepare a scan command to be sent to the firmware
1685 * Called from lbs_prepare_and_send_command() in cmd.c
1687 * Sends a fixed lenght data part (specifying the BSS type and BSSID filters)
1688 * as well as a variable number/length of TLVs to the firmware.
1690 * @param priv A pointer to struct lbs_private structure
1691 * @param cmd A pointer to cmd_ds_command structure to be sent to
1692 * firmware with the cmd_DS_801_11_SCAN structure
1693 * @param pdata_buf Void pointer cast of a lbs_scan_cmd_config struct used
1694 * to set the fields/TLVs for the command sent to firmware
1698 int lbs_cmd_80211_scan(struct lbs_private *priv,
1699 struct cmd_ds_command *cmd, void *pdata_buf)
1701 struct cmd_ds_802_11_scan *pscan = &cmd->params.scan;
1702 struct lbs_scan_cmd_config *pscancfg = pdata_buf;
1704 lbs_deb_enter(LBS_DEB_SCAN);
1706 /* Set fixed field variables in scan command */
1707 pscan->bsstype = pscancfg->bsstype;
1708 memcpy(pscan->bssid, pscancfg->bssid, ETH_ALEN);
1709 memcpy(pscan->tlvbuffer, pscancfg->tlvbuffer, pscancfg->tlvbufferlen);
1711 cmd->command = cpu_to_le16(CMD_802_11_SCAN);
1713 /* size is equal to the sizeof(fixed portions) + the TLV len + header */
1714 cmd->size = cpu_to_le16(sizeof(pscan->bsstype) + ETH_ALEN
1715 + pscancfg->tlvbufferlen + S_DS_GEN);
1717 lbs_deb_scan("SCAN_CMD: command 0x%04x, size %d, seqnum %d\n",
1718 le16_to_cpu(cmd->command), le16_to_cpu(cmd->size),
1719 le16_to_cpu(cmd->seqnum));
1721 lbs_deb_leave(LBS_DEB_SCAN);
1725 static inline int is_same_network(struct bss_descriptor *src,
1726 struct bss_descriptor *dst)
1728 /* A network is only a duplicate if the channel, BSSID, and ESSID
1729 * all match. We treat all <hidden> with the same BSSID and channel
1731 return ((src->ssid_len == dst->ssid_len) &&
1732 (src->channel == dst->channel) &&
1733 !compare_ether_addr(src->bssid, dst->bssid) &&
1734 !memcmp(src->ssid, dst->ssid, src->ssid_len));
1738 * @brief This function handles the command response of scan
1740 * Called from handle_cmd_response() in cmdrespc.
1742 * The response buffer for the scan command has the following
1745 * .-----------------------------------------------------------.
1746 * | header (4 * sizeof(u16)): Standard command response hdr |
1747 * .-----------------------------------------------------------.
1748 * | bufsize (u16) : sizeof the BSS Description data |
1749 * .-----------------------------------------------------------.
1750 * | NumOfSet (u8) : Number of BSS Descs returned |
1751 * .-----------------------------------------------------------.
1752 * | BSSDescription data (variable, size given in bufsize) |
1753 * .-----------------------------------------------------------.
1754 * | TLV data (variable, size calculated using header->size, |
1755 * | bufsize and sizeof the fixed fields above) |
1756 * .-----------------------------------------------------------.
1758 * @param priv A pointer to struct lbs_private structure
1759 * @param resp A pointer to cmd_ds_command
1763 int lbs_ret_80211_scan(struct lbs_private *priv, struct cmd_ds_command *resp)
1765 struct lbs_adapter *adapter = priv->adapter;
1766 struct cmd_ds_802_11_scan_rsp *pscan;
1767 struct bss_descriptor * iter_bss;
1768 struct bss_descriptor * safe;
1776 lbs_deb_enter(LBS_DEB_SCAN);
1778 /* Prune old entries from scan table */
1779 list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
1780 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1781 if (time_before(jiffies, stale_time))
1783 list_move_tail (&iter_bss->list, &adapter->network_free_list);
1784 clear_bss_descriptor(iter_bss);
1787 pscan = &resp->params.scanresp;
1789 if (pscan->nr_sets > MAX_NETWORK_COUNT) {
1791 "SCAN_RESP: too many scan results (%d, max %d)!!\n",
1792 pscan->nr_sets, MAX_NETWORK_COUNT);
1797 bytesleft = le16_to_cpu(get_unaligned((u16*)&pscan->bssdescriptsize));
1798 lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
1800 scanrespsize = le16_to_cpu(get_unaligned((u16*)&resp->size));
1801 lbs_deb_scan("SCAN_RESP: returned %d AP before parsing\n",
1804 pbssinfo = pscan->bssdesc_and_tlvbuffer;
1806 /* The size of the TLV buffer is equal to the entire command response
1807 * size (scanrespsize) minus the fixed fields (sizeof()'s), the
1808 * BSS Descriptions (bssdescriptsize as bytesLef) and the command
1809 * response header (S_DS_GEN)
1811 tlvbufsize = scanrespsize - (bytesleft + sizeof(pscan->bssdescriptsize)
1812 + sizeof(pscan->nr_sets)
1816 * Process each scan response returned (pscan->nr_sets). Save
1817 * the information in the newbssentry and then insert into the
1818 * driver scan table either as an update to an existing entry
1819 * or as an addition at the end of the table
1821 for (idx = 0; idx < pscan->nr_sets && bytesleft; idx++) {
1822 struct bss_descriptor new;
1823 struct bss_descriptor * found = NULL;
1824 struct bss_descriptor * oldest = NULL;
1825 DECLARE_MAC_BUF(mac);
1827 /* Process the data fields and IEs returned for this BSS */
1828 memset(&new, 0, sizeof (struct bss_descriptor));
1829 if (lbs_process_bss(&new, &pbssinfo, &bytesleft) != 0) {
1830 /* error parsing the scan response, skipped */
1831 lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1835 /* Try to find this bss in the scan table */
1836 list_for_each_entry (iter_bss, &adapter->network_list, list) {
1837 if (is_same_network(iter_bss, &new)) {
1842 if ((oldest == NULL) ||
1843 (iter_bss->last_scanned < oldest->last_scanned))
1848 /* found, clear it */
1849 clear_bss_descriptor(found);
1850 } else if (!list_empty(&adapter->network_free_list)) {
1851 /* Pull one from the free list */
1852 found = list_entry(adapter->network_free_list.next,
1853 struct bss_descriptor, list);
1854 list_move_tail(&found->list, &adapter->network_list);
1855 } else if (oldest) {
1856 /* If there are no more slots, expire the oldest */
1858 clear_bss_descriptor(found);
1859 list_move_tail(&found->list, &adapter->network_list);
1864 lbs_deb_scan("SCAN_RESP: BSSID = %s\n",
1865 print_mac(mac, new.bssid));
1867 /* Copy the locally created newbssentry to the scan table */
1868 memcpy(found, &new, offsetof(struct bss_descriptor, list));
1874 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);