libertas: remove user-specified channel list
[safe/jmp/linux-2.6] / drivers / net / wireless / libertas / scan.c
1 /**
2   * Functions implementing wlan scan IOCTL and firmware command APIs
3   *
4   * IOCTL handlers as well as command preperation and response routines
5   *  for sending scan commands to the firmware.
6   */
7 #include <linux/ctype.h>
8 #include <linux/if.h>
9 #include <linux/netdevice.h>
10 #include <linux/wireless.h>
11 #include <linux/etherdevice.h>
12
13 #include <net/ieee80211.h>
14 #include <net/iw_handler.h>
15
16 #include <asm/unaligned.h>
17
18 #include "host.h"
19 #include "decl.h"
20 #include "dev.h"
21 #include "scan.h"
22 #include "join.h"
23
24 //! Approximate amount of data needed to pass a scan result back to iwlist
25 #define MAX_SCAN_CELL_SIZE  (IW_EV_ADDR_LEN             \
26                              + IW_ESSID_MAX_SIZE        \
27                              + IW_EV_UINT_LEN           \
28                              + IW_EV_FREQ_LEN           \
29                              + IW_EV_QUAL_LEN           \
30                              + IW_ESSID_MAX_SIZE        \
31                              + IW_EV_PARAM_LEN          \
32                              + 40)      /* 40 for WPAIE */
33
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)))
38
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))
41
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                             + CHAN_TLV_MAX_SIZE                 \
45                             + SSID_TLV_MAX_SIZE)
46
47 //! The maximum number of channels the firmware can scan per command
48 #define MRVDRV_MAX_CHANNELS_PER_SCAN   14
49
50 /**
51  * @brief Number of channels to scan per firmware scan command issuance.
52  *
53  *  Number restricted to prevent hitting the limit on the amount of scan data
54  *  returned in a single firmware scan command.
55  */
56 #define MRVDRV_CHANNELS_PER_SCAN_CMD   4
57
58 //! Scan time specified in the channel TLV for each channel for passive scans
59 #define MRVDRV_PASSIVE_SCAN_CHAN_TIME  100
60
61 //! Scan time specified in the channel TLV for each channel for active scans
62 #define MRVDRV_ACTIVE_SCAN_CHAN_TIME   100
63
64 static const u8 zeromac[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
65 static const u8 bcastmac[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
66
67
68
69
70 /*********************************************************************/
71 /*                                                                   */
72 /*  Misc helper functions                                            */
73 /*                                                                   */
74 /*********************************************************************/
75
76 static inline void clear_bss_descriptor (struct bss_descriptor * bss)
77 {
78         /* Don't blow away ->list, just BSS data */
79         memset(bss, 0, offsetof(struct bss_descriptor, list));
80 }
81
82 static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
83                         struct bss_descriptor * match_bss)
84 {
85         if (   !secinfo->wep_enabled
86             && !secinfo->WPAenabled
87             && !secinfo->WPA2enabled
88             && match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC
89             && match_bss->rsn_ie[0] != MFIE_TYPE_RSN
90             && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
91                 return 1;
92         }
93         return 0;
94 }
95
96 static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
97                         struct bss_descriptor * match_bss)
98 {
99         if ( secinfo->wep_enabled
100            && !secinfo->WPAenabled
101            && !secinfo->WPA2enabled
102            && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
103                 return 1;
104         }
105         return 0;
106 }
107
108 static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
109                         struct bss_descriptor * match_bss)
110 {
111         if (  !secinfo->wep_enabled
112            && secinfo->WPAenabled
113            && (match_bss->wpa_ie[0] == MFIE_TYPE_GENERIC)
114            /* privacy bit may NOT be set in some APs like LinkSys WRT54G
115               && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
116             */
117            ) {
118                 return 1;
119         }
120         return 0;
121 }
122
123 static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
124                         struct bss_descriptor * match_bss)
125 {
126         if (  !secinfo->wep_enabled
127            && secinfo->WPA2enabled
128            && (match_bss->rsn_ie[0] == MFIE_TYPE_RSN)
129            /* privacy bit may NOT be set in some APs like LinkSys WRT54G
130               && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
131             */
132            ) {
133                 return 1;
134         }
135         return 0;
136 }
137
138 static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
139                         struct bss_descriptor * match_bss)
140 {
141         if (  !secinfo->wep_enabled
142            && !secinfo->WPAenabled
143            && !secinfo->WPA2enabled
144            && (match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC)
145            && (match_bss->rsn_ie[0] != MFIE_TYPE_RSN)
146            && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
147                 return 1;
148         }
149         return 0;
150 }
151
152 /**
153  *  @brief Check if a scanned network compatible with the driver settings
154  *
155  *   WEP     WPA     WPA2    ad-hoc  encrypt                      Network
156  * enabled enabled  enabled   AES     mode   privacy  WPA  WPA2  Compatible
157  *    0       0        0       0      NONE      0      0    0   yes No security
158  *    1       0        0       0      NONE      1      0    0   yes Static WEP
159  *    0       1        0       0       x        1x     1    x   yes WPA
160  *    0       0        1       0       x        1x     x    1   yes WPA2
161  *    0       0        0       1      NONE      1      0    0   yes Ad-hoc AES
162  *    0       0        0       0     !=NONE     1      0    0   yes Dynamic WEP
163  *
164  *
165  *  @param adapter A pointer to struct lbs_adapter
166  *  @param index   Index in scantable to check against current driver settings
167  *  @param mode    Network mode: Infrastructure or IBSS
168  *
169  *  @return        Index in scantable, or error code if negative
170  */
171 static int is_network_compatible(struct lbs_adapter *adapter,
172                 struct bss_descriptor * bss, u8 mode)
173 {
174         int matched = 0;
175
176         lbs_deb_enter(LBS_DEB_SCAN);
177
178         if (bss->mode != mode)
179                 goto done;
180
181         if ((matched = match_bss_no_security(&adapter->secinfo, bss))) {
182                 goto done;
183         } else if ((matched = match_bss_static_wep(&adapter->secinfo, bss))) {
184                 goto done;
185         } else if ((matched = match_bss_wpa(&adapter->secinfo, bss))) {
186                 lbs_deb_scan(
187                        "is_network_compatible() WPA: wpa_ie=%#x "
188                        "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s "
189                        "privacy=%#x\n", bss->wpa_ie[0], bss->rsn_ie[0],
190                        adapter->secinfo.wep_enabled ? "e" : "d",
191                        adapter->secinfo.WPAenabled ? "e" : "d",
192                        adapter->secinfo.WPA2enabled ? "e" : "d",
193                        (bss->capability & WLAN_CAPABILITY_PRIVACY));
194                 goto done;
195         } else if ((matched = match_bss_wpa2(&adapter->secinfo, bss))) {
196                 lbs_deb_scan(
197                        "is_network_compatible() WPA2: wpa_ie=%#x "
198                        "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s "
199                        "privacy=%#x\n", bss->wpa_ie[0], bss->rsn_ie[0],
200                        adapter->secinfo.wep_enabled ? "e" : "d",
201                        adapter->secinfo.WPAenabled ? "e" : "d",
202                        adapter->secinfo.WPA2enabled ? "e" : "d",
203                        (bss->capability & WLAN_CAPABILITY_PRIVACY));
204                 goto done;
205         } else if ((matched = match_bss_dynamic_wep(&adapter->secinfo, bss))) {
206                 lbs_deb_scan(
207                        "is_network_compatible() dynamic WEP: "
208                        "wpa_ie=%#x wpa2_ie=%#x privacy=%#x\n",
209                        bss->wpa_ie[0], bss->rsn_ie[0],
210                        (bss->capability & WLAN_CAPABILITY_PRIVACY));
211                 goto done;
212         }
213
214         /* bss security settings don't match those configured on card */
215         lbs_deb_scan(
216                "is_network_compatible() FAILED: wpa_ie=%#x "
217                "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s privacy=%#x\n",
218                bss->wpa_ie[0], bss->rsn_ie[0],
219                adapter->secinfo.wep_enabled ? "e" : "d",
220                adapter->secinfo.WPAenabled ? "e" : "d",
221                adapter->secinfo.WPA2enabled ? "e" : "d",
222                (bss->capability & WLAN_CAPABILITY_PRIVACY));
223
224 done:
225         lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
226         return matched;
227 }
228
229 /**
230  *  @brief Compare two SSIDs
231  *
232  *  @param ssid1    A pointer to ssid to compare
233  *  @param ssid2    A pointer to ssid to compare
234  *
235  *  @return         0--ssid is same, otherwise is different
236  */
237 int lbs_ssid_cmp(u8 *ssid1, u8 ssid1_len, u8 *ssid2, u8 ssid2_len)
238 {
239         if (ssid1_len != ssid2_len)
240                 return -1;
241
242         return memcmp(ssid1, ssid2, ssid1_len);
243 }
244
245
246
247
248 /*********************************************************************/
249 /*                                                                   */
250 /*  Main scanning support                                            */
251 /*                                                                   */
252 /*********************************************************************/
253
254
255 /**
256  *  @brief Create a channel list for the driver to scan based on region info
257  *
258  *  Only used from lbs_scan_setup_scan_config()
259  *
260  *  Use the driver region/band information to construct a comprehensive list
261  *    of channels to scan.  This routine is used for any scan that is not
262  *    provided a specific channel list to scan.
263  *
264  *  @param priv          A pointer to struct lbs_private structure
265  *  @param scanchanlist  Output parameter: resulting channel list to scan
266  *  @param filteredscan  Flag indicating whether or not a BSSID or SSID filter
267  *                       is being sent in the command to firmware.  Used to
268  *                       increase the number of channels sent in a scan
269  *                       command and to disable the firmware channel scan
270  *                       filter.
271  *
272  *  @return              void
273  */
274 static void lbs_scan_create_channel_list(struct lbs_private *priv,
275                                           struct chanscanparamset * scanchanlist,
276                                           u8 filteredscan)
277 {
278
279         struct lbs_adapter *adapter = priv->adapter;
280         struct region_channel *scanregion;
281         struct chan_freq_power *cfp;
282         int rgnidx;
283         int chanidx;
284         int nextchan;
285         u8 scantype;
286
287         lbs_deb_enter_args(LBS_DEB_SCAN, "filteredscan %d", filteredscan);
288
289         chanidx = 0;
290
291         /* Set the default scan type to the user specified type, will later
292          *   be changed to passive on a per channel basis if restricted by
293          *   regulatory requirements (11d or 11h)
294          */
295         scantype = CMD_SCAN_TYPE_ACTIVE;
296
297         for (rgnidx = 0; rgnidx < ARRAY_SIZE(adapter->region_channel); rgnidx++) {
298                 if (priv->adapter->enable11d &&
299                     (adapter->connect_status != LBS_CONNECTED) &&
300                     (adapter->mesh_connect_status != LBS_CONNECTED)) {
301                         /* Scan all the supported chan for the first scan */
302                         if (!adapter->universal_channel[rgnidx].valid)
303                                 continue;
304                         scanregion = &adapter->universal_channel[rgnidx];
305
306                         /* clear the parsed_region_chan for the first scan */
307                         memset(&adapter->parsed_region_chan, 0x00,
308                                sizeof(adapter->parsed_region_chan));
309                 } else {
310                         if (!adapter->region_channel[rgnidx].valid)
311                                 continue;
312                         scanregion = &adapter->region_channel[rgnidx];
313                 }
314
315                 for (nextchan = 0;
316                      nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
317
318                         cfp = scanregion->CFP + nextchan;
319
320                         if (priv->adapter->enable11d) {
321                                 scantype =
322                                     lbs_get_scan_type_11d(cfp->channel,
323                                                            &adapter->
324                                                            parsed_region_chan);
325                         }
326
327                         switch (scanregion->band) {
328                         case BAND_B:
329                         case BAND_G:
330                         default:
331                                 scanchanlist[chanidx].radiotype =
332                                     CMD_SCAN_RADIO_TYPE_BG;
333                                 break;
334                         }
335
336                         if (scantype == CMD_SCAN_TYPE_PASSIVE) {
337                                 scanchanlist[chanidx].maxscantime =
338                                     cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
339                                 scanchanlist[chanidx].chanscanmode.passivescan =
340                                     1;
341                         } else {
342                                 scanchanlist[chanidx].maxscantime =
343                                     cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
344                                 scanchanlist[chanidx].chanscanmode.passivescan =
345                                     0;
346                         }
347
348                         scanchanlist[chanidx].channumber = cfp->channel;
349
350                         if (filteredscan) {
351                                 scanchanlist[chanidx].chanscanmode.
352                                     disablechanfilt = 1;
353                         }
354                 }
355         }
356 }
357
358
359 /* Delayed partial scan worker */
360 void lbs_scan_worker(struct work_struct *work)
361 {
362         struct lbs_private *priv = container_of(work,
363                 struct lbs_private,
364                 scan_work.work);
365
366         lbs_scan_networks(priv, NULL, 0);
367 }
368
369
370 /**
371  *  @brief Construct a lbs_scan_cmd_config structure to use in issue scan cmds
372  *
373  *  Application layer or other functions can invoke lbs_scan_networks
374  *    with a scan configuration supplied in a lbs_ioctl_user_scan_cfg struct.
375  *    This structure is used as the basis of one or many lbs_scan_cmd_config
376  *    commands that are sent to the command processing module and sent to
377  *    firmware.
378  *
379  *  Create a lbs_scan_cmd_config based on the following user supplied
380  *    parameters (if present):
381  *             - SSID filter
382  *             - BSSID filter
383  *             - Number of Probes to be sent
384  *             - channel list
385  *
386  *  If the SSID or BSSID filter is not present, disable/clear the filter.
387  *  Qualify the channel
388  *
389  *  @param priv             A pointer to struct lbs_private structure
390  *  @param puserscanin      NULL or pointer to scan configuration parameters
391  *  @param ppchantlvout     Output parameter: Pointer to the start of the
392  *                          channel TLV portion of the output scan config
393  *  @param pscanchanlist    Output parameter: Pointer to the resulting channel
394  *                          list to scan
395  *  @param pmaxchanperscan  Output parameter: Number of channels to scan for
396  *                          each issuance of the firmware scan command
397  *  @param pfilteredscan    Output parameter: Flag indicating whether or not
398  *                          a BSSID or SSID filter is being sent in the
399  *                          command to firmware.  Used to increase the number
400  *                          of channels sent in a scan command and to
401  *                          disable the firmware channel scan filter.
402  *  @param pscancurrentonly Output parameter: Flag indicating whether or not
403  *                          we are only scanning our current active channel
404  *
405  *  @return                 resulting scan configuration
406  */
407 static struct lbs_scan_cmd_config *
408 lbs_scan_setup_scan_config(struct lbs_private *priv,
409                             const struct lbs_ioctl_user_scan_cfg *puserscanin,
410                             struct mrvlietypes_chanlistparamset ** ppchantlvout,
411                             struct chanscanparamset * pscanchanlist,
412                             int *pmaxchanperscan,
413                             u8 * pfilteredscan,
414                             u8 * pscancurrentonly)
415 {
416         struct mrvlietypes_ssidparamset *pssidtlv;
417         struct lbs_scan_cmd_config *pscancfgout = NULL;
418         u8 *ptlvpos;
419
420         lbs_deb_enter(LBS_DEB_SCAN);
421
422         pscancfgout = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
423         if (pscancfgout == NULL)
424                 goto out;
425
426         /* The tlvbufferlen is calculated for each scan command.  The TLVs added
427          *   in this routine will be preserved since the routine that sends
428          *   the command will append channelTLVs at *ppchantlvout.  The difference
429          *   between the *ppchantlvout and the tlvbuffer start will be used
430          *   to calculate the size of anything we add in this routine.
431          */
432         pscancfgout->tlvbufferlen = 0;
433
434         /* Running tlv pointer.  Assigned to ppchantlvout at end of function
435          *  so later routines know where channels can be added to the command buf
436          */
437         ptlvpos = pscancfgout->tlvbuffer;
438
439         /*
440          * Set the initial scan paramters for progressive scanning.  If a specific
441          *   BSSID or SSID is used, the number of channels in the scan command
442          *   will be increased to the absolute maximum
443          */
444         *pmaxchanperscan = MRVDRV_CHANNELS_PER_SCAN_CMD;
445
446         /* Initialize the scan as un-filtered by firmware, set to TRUE below if
447          *   a SSID or BSSID filter is sent in the command
448          */
449         *pfilteredscan = 0;
450
451         /* Initialize the scan as not being only on the current channel.  If
452          *   the channel list is customized, only contains one channel, and
453          *   is the active channel, this is set true and data flow is not halted.
454          */
455         *pscancurrentonly = 0;
456
457         if (puserscanin) {
458                 /* Set the bss type scan filter, use adapter setting if unset */
459                 pscancfgout->bsstype =
460                     puserscanin->bsstype ? puserscanin->bsstype : CMD_BSS_TYPE_ANY;
461
462                 /*
463                  * Set the BSSID filter to the incoming configuration,
464                  *   if non-zero.  If not set, it will remain disabled (all zeros).
465                  */
466                 memcpy(pscancfgout->bssid, puserscanin->bssid,
467                        sizeof(pscancfgout->bssid));
468
469                 if (puserscanin->ssid_len) {
470                         pssidtlv =
471                             (struct mrvlietypes_ssidparamset *) pscancfgout->
472                             tlvbuffer;
473                         pssidtlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
474                         pssidtlv->header.len = cpu_to_le16(puserscanin->ssid_len);
475                         memcpy(pssidtlv->ssid, puserscanin->ssid,
476                                puserscanin->ssid_len);
477                         ptlvpos += sizeof(pssidtlv->header) + puserscanin->ssid_len;
478                 }
479
480                 /*
481                  *  The default number of channels sent in the command is low to
482                  *    ensure the response buffer from the firmware does not truncate
483                  *    scan results.  That is not an issue with an SSID or BSSID
484                  *    filter applied to the scan results in the firmware.
485                  */
486                 if (   puserscanin->ssid_len
487                     || (compare_ether_addr(pscancfgout->bssid, &zeromac[0]) != 0)) {
488                         *pmaxchanperscan = MRVDRV_MAX_CHANNELS_PER_SCAN;
489                         *pfilteredscan = 1;
490                 }
491         } else {
492                 pscancfgout->bsstype = CMD_BSS_TYPE_ANY;
493         }
494
495         /*
496          * Set the output for the channel TLV to the address in the tlv buffer
497          *   past any TLVs that were added in this fuction (SSID).
498          *   channel TLVs will be added past this for each scan command, preserving
499          *   the TLVs that were previously added.
500          */
501         *ppchantlvout = (struct mrvlietypes_chanlistparamset *) ptlvpos;
502
503         lbs_scan_create_channel_list(priv, pscanchanlist,
504                                       *pfilteredscan);
505 out:
506         return pscancfgout;
507 }
508
509 /**
510  *  @brief Construct and send multiple scan config commands to the firmware
511  *
512  *  Only used from lbs_scan_networks()
513  *
514  *  Previous routines have created a lbs_scan_cmd_config with any requested
515  *   TLVs.  This function splits the channel TLV into maxchanperscan lists
516  *   and sends the portion of the channel TLV along with the other TLVs
517  *   to the lbs_cmd routines for execution in the firmware.
518  *
519  *  @param priv            A pointer to struct lbs_private structure
520  *  @param maxchanperscan  Maximum number channels to be included in each
521  *                         scan command sent to firmware
522  *  @param filteredscan    Flag indicating whether or not a BSSID or SSID
523  *                         filter is being used for the firmware command
524  *                         scan command sent to firmware
525  *  @param pscancfgout     Scan configuration used for this scan.
526  *  @param pchantlvout     Pointer in the pscancfgout where the channel TLV
527  *                         should start.  This is past any other TLVs that
528  *                         must be sent down in each firmware command.
529  *  @param pscanchanlist   List of channels to scan in maxchanperscan segments
530  *
531  *  @return                0 or error return otherwise
532  */
533 static int lbs_scan_channel_list(struct lbs_private *priv,
534                                   int maxchanperscan,
535                                   u8 filteredscan,
536                                   struct lbs_scan_cmd_config *pscancfgout,
537                                   struct mrvlietypes_chanlistparamset * pchantlvout,
538                                   struct chanscanparamset * pscanchanlist,
539                                   const struct lbs_ioctl_user_scan_cfg *puserscanin,
540                                   int full_scan)
541 {
542         struct chanscanparamset *ptmpchan;
543         struct chanscanparamset *pstartchan;
544         u8 scanband;
545         int doneearly;
546         int tlvidx;
547         int ret = 0;
548         int scanned = 0;
549         union iwreq_data wrqu;
550
551         lbs_deb_enter_args(LBS_DEB_SCAN, "maxchanperscan %d, filteredscan %d, "
552                 "full_scan %d", maxchanperscan, filteredscan, full_scan);
553
554         if (!pscancfgout || !pchantlvout || !pscanchanlist) {
555                 lbs_deb_scan("pscancfgout, pchantlvout or "
556                         "pscanchanlist is NULL\n");
557                 ret = -1;
558                 goto out;
559         }
560
561         pchantlvout->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
562
563         /* Set the temp channel struct pointer to the start of the desired list */
564         ptmpchan = pscanchanlist;
565
566         if (priv->adapter->last_scanned_channel && !puserscanin)
567                 ptmpchan += priv->adapter->last_scanned_channel;
568
569         /* Loop through the desired channel list, sending a new firmware scan
570          *   commands for each maxchanperscan channels (or for 1,6,11 individually
571          *   if configured accordingly)
572          */
573         while (ptmpchan->channumber) {
574
575                 tlvidx = 0;
576                 pchantlvout->header.len = 0;
577                 scanband = ptmpchan->radiotype;
578                 pstartchan = ptmpchan;
579                 doneearly = 0;
580
581                 /* Construct the channel TLV for the scan command.  Continue to
582                  *  insert channel TLVs until:
583                  *    - the tlvidx hits the maximum configured per scan command
584                  *    - the next channel to insert is 0 (end of desired channel list)
585                  *    - doneearly is set (controlling individual scanning of 1,6,11)
586                  */
587                 while (tlvidx < maxchanperscan && ptmpchan->channumber
588                        && !doneearly && scanned < 2) {
589
590                         lbs_deb_scan("channel %d, radio %d, passive %d, "
591                                 "dischanflt %d, maxscantime %d\n",
592                                 ptmpchan->channumber,
593                                 ptmpchan->radiotype,
594                                      ptmpchan->chanscanmode.passivescan,
595                                      ptmpchan->chanscanmode.disablechanfilt,
596                                      ptmpchan->maxscantime);
597
598                         /* Copy the current channel TLV to the command being prepared */
599                         memcpy(pchantlvout->chanscanparam + tlvidx,
600                                ptmpchan, sizeof(pchantlvout->chanscanparam));
601
602                         /* Increment the TLV header length by the size appended */
603                         /* Ew, it would be _so_ nice if we could just declare the
604                            variable little-endian and let GCC handle it for us */
605                         pchantlvout->header.len =
606                                 cpu_to_le16(le16_to_cpu(pchantlvout->header.len) +
607                                             sizeof(pchantlvout->chanscanparam));
608
609                         /*
610                          *  The tlv buffer length is set to the number of bytes of the
611                          *    between the channel tlv pointer and the start of the
612                          *    tlv buffer.  This compensates for any TLVs that were appended
613                          *    before the channel list.
614                          */
615                         pscancfgout->tlvbufferlen = ((u8 *) pchantlvout
616                                                      - pscancfgout->tlvbuffer);
617
618                         /*  Add the size of the channel tlv header and the data length */
619                         pscancfgout->tlvbufferlen +=
620                             (sizeof(pchantlvout->header)
621                              + le16_to_cpu(pchantlvout->header.len));
622
623                         /* Increment the index to the channel tlv we are constructing */
624                         tlvidx++;
625
626                         doneearly = 0;
627
628                         /* Stop the loop if the *current* channel is in the 1,6,11 set
629                          *   and we are not filtering on a BSSID or SSID.
630                          */
631                         if (!filteredscan && (ptmpchan->channumber == 1
632                                               || ptmpchan->channumber == 6
633                                               || ptmpchan->channumber == 11)) {
634                                 doneearly = 1;
635                         }
636
637                         /* Increment the tmp pointer to the next channel to be scanned */
638                         ptmpchan++;
639                         scanned++;
640
641                         /* Stop the loop if the *next* channel is in the 1,6,11 set.
642                          *  This will cause it to be the only channel scanned on the next
643                          *  interation
644                          */
645                         if (!filteredscan && (ptmpchan->channumber == 1
646                                               || ptmpchan->channumber == 6
647                                               || ptmpchan->channumber == 11)) {
648                                 doneearly = 1;
649                         }
650                 }
651
652                 /* Send the scan command to the firmware with the specified cfg */
653                 ret = lbs_prepare_and_send_command(priv, CMD_802_11_SCAN, 0,
654                                             0, 0, pscancfgout);
655                 if (scanned >= 2 && !full_scan) {
656                         ret = 0;
657                         goto done;
658                 }
659                 scanned = 0;
660         }
661
662 done:
663         priv->adapter->last_scanned_channel = ptmpchan->channumber;
664
665         if (priv->adapter->last_scanned_channel) {
666                 /* Schedule the next part of the partial scan */
667                 if (!full_scan && !priv->adapter->surpriseremoved) {
668                         cancel_delayed_work(&priv->scan_work);
669                         queue_delayed_work(priv->work_thread, &priv->scan_work,
670                                            msecs_to_jiffies(300));
671                 }
672         } else {
673                 /* All done, tell userspace the scan table has been updated */
674                 memset(&wrqu, 0, sizeof(union iwreq_data));
675                 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
676         }
677
678 out:
679         lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
680         return ret;
681 }
682
683 /*
684  * Only used from lbs_scan_networks()
685 */
686 static void clear_selected_scan_list_entries(struct lbs_adapter *adapter,
687         const struct lbs_ioctl_user_scan_cfg *scan_cfg)
688 {
689         struct bss_descriptor *bss;
690         struct bss_descriptor *safe;
691         u32 clear_ssid_flag = 0, clear_bssid_flag = 0;
692
693         lbs_deb_enter(LBS_DEB_SCAN);
694
695         if (!scan_cfg)
696                 goto out;
697
698         if (scan_cfg->clear_ssid && scan_cfg->ssid_len)
699                 clear_ssid_flag = 1;
700
701         if (scan_cfg->clear_bssid
702             && (compare_ether_addr(scan_cfg->bssid, &zeromac[0]) != 0)
703             && (compare_ether_addr(scan_cfg->bssid, &bcastmac[0]) != 0)) {
704                 clear_bssid_flag = 1;
705         }
706
707         if (!clear_ssid_flag && !clear_bssid_flag)
708                 goto out;
709
710         mutex_lock(&adapter->lock);
711         list_for_each_entry_safe (bss, safe, &adapter->network_list, list) {
712                 u32 clear = 0;
713
714                 /* Check for an SSID match */
715                 if (   clear_ssid_flag
716                     && (bss->ssid_len == scan_cfg->ssid_len)
717                     && !memcmp(bss->ssid, scan_cfg->ssid, bss->ssid_len))
718                         clear = 1;
719
720                 /* Check for a BSSID match */
721                 if (   clear_bssid_flag
722                     && !compare_ether_addr(bss->bssid, scan_cfg->bssid))
723                         clear = 1;
724
725                 if (clear) {
726                         list_move_tail (&bss->list, &adapter->network_free_list);
727                         clear_bss_descriptor(bss);
728                 }
729         }
730         mutex_unlock(&adapter->lock);
731 out:
732         lbs_deb_leave(LBS_DEB_SCAN);
733 }
734
735
736 /**
737  *  @brief Internal function used to start a scan based on an input config
738  *
739  *  Also used from debugfs
740  *
741  *  Use the input user scan configuration information when provided in
742  *    order to send the appropriate scan commands to firmware to populate or
743  *    update the internal driver scan table
744  *
745  *  @param priv          A pointer to struct lbs_private structure
746  *  @param puserscanin   Pointer to the input configuration for the requested
747  *                       scan.
748  *  @param full_scan     ???
749  *
750  *  @return              0 or < 0 if error
751  */
752 int lbs_scan_networks(struct lbs_private *priv,
753         const struct lbs_ioctl_user_scan_cfg *puserscanin,
754                        int full_scan)
755 {
756         struct lbs_adapter *adapter = priv->adapter;
757         struct mrvlietypes_chanlistparamset *pchantlvout;
758         struct chanscanparamset * scan_chan_list = NULL;
759         struct lbs_scan_cmd_config *scan_cfg = NULL;
760         u8 filteredscan;
761         u8 scancurrentchanonly;
762         int maxchanperscan;
763         int ret;
764 #ifdef CONFIG_LIBERTAS_DEBUG
765         struct bss_descriptor * iter_bss;
766         int i = 0;
767         DECLARE_MAC_BUF(mac);
768 #endif
769
770         lbs_deb_enter_args(LBS_DEB_SCAN, "full_scan %d", full_scan);
771
772         /* Cancel any partial outstanding partial scans if this scan
773          * is a full scan.
774          */
775         if (full_scan && delayed_work_pending(&priv->scan_work))
776                 cancel_delayed_work(&priv->scan_work);
777
778         scan_chan_list = kzalloc(sizeof(struct chanscanparamset) *
779                                 LBS_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
780         if (scan_chan_list == NULL) {
781                 ret = -ENOMEM;
782                 goto out;
783         }
784
785         scan_cfg = lbs_scan_setup_scan_config(priv,
786                                                puserscanin,
787                                                &pchantlvout,
788                                                scan_chan_list,
789                                                &maxchanperscan,
790                                                &filteredscan,
791                                                &scancurrentchanonly);
792         if (scan_cfg == NULL) {
793                 ret = -ENOMEM;
794                 goto out;
795         }
796
797         clear_selected_scan_list_entries(adapter, puserscanin);
798
799         /* Keep the data path active if we are only scanning our current channel */
800         if (!scancurrentchanonly) {
801                 netif_stop_queue(priv->dev);
802                 netif_carrier_off(priv->dev);
803                 if (priv->mesh_dev) {
804                         netif_stop_queue(priv->mesh_dev);
805                         netif_carrier_off(priv->mesh_dev);
806                 }
807         }
808
809         ret = lbs_scan_channel_list(priv,
810                                      maxchanperscan,
811                                      filteredscan,
812                                      scan_cfg,
813                                      pchantlvout,
814                                      scan_chan_list,
815                                      puserscanin,
816                                      full_scan);
817
818 #ifdef CONFIG_LIBERTAS_DEBUG
819         /* Dump the scan table */
820         mutex_lock(&adapter->lock);
821         lbs_deb_scan("The scan table contains:\n");
822         list_for_each_entry (iter_bss, &adapter->network_list, list) {
823                 lbs_deb_scan("scan %02d, %s, RSSI, %d, SSID '%s'\n",
824                        i++, print_mac(mac, iter_bss->bssid), (s32) iter_bss->rssi,
825                        escape_essid(iter_bss->ssid, iter_bss->ssid_len));
826         }
827         mutex_unlock(&adapter->lock);
828 #endif
829
830         if (adapter->connect_status == LBS_CONNECTED) {
831                 netif_carrier_on(priv->dev);
832                 netif_wake_queue(priv->dev);
833         }
834
835         if (priv->mesh_dev && (adapter->mesh_connect_status == LBS_CONNECTED)) {
836                 netif_carrier_on(priv->mesh_dev);
837                 netif_wake_queue(priv->mesh_dev);
838         }
839
840 out:
841         if (scan_cfg)
842                 kfree(scan_cfg);
843
844         if (scan_chan_list)
845                 kfree(scan_chan_list);
846
847         lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
848         return ret;
849 }
850
851 /**
852  *  @brief Interpret a BSS scan response returned from the firmware
853  *
854  *  Parse the various fixed fields and IEs passed back for a a BSS probe
855  *   response or beacon from the scan command.  Record information as needed
856  *   in the scan table struct bss_descriptor for that entry.
857  *
858  *  @param bss  Output parameter: Pointer to the BSS Entry
859  *
860  *  @return             0 or -1
861  */
862 static int lbs_process_bss(struct bss_descriptor *bss,
863                                 u8 ** pbeaconinfo, int *bytesleft)
864 {
865         struct ieeetypes_fhparamset *pFH;
866         struct ieeetypes_dsparamset *pDS;
867         struct ieeetypes_cfparamset *pCF;
868         struct ieeetypes_ibssparamset *pibss;
869         DECLARE_MAC_BUF(mac);
870         struct ieeetypes_countryinfoset *pcountryinfo;
871         u8 *pos, *end, *p;
872         u8 n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
873         u16 beaconsize = 0;
874         int ret;
875
876         lbs_deb_enter(LBS_DEB_SCAN);
877
878         if (*bytesleft >= sizeof(beaconsize)) {
879                 /* Extract & convert beacon size from the command buffer */
880                 beaconsize = le16_to_cpu(get_unaligned((u16 *)*pbeaconinfo));
881                 *bytesleft -= sizeof(beaconsize);
882                 *pbeaconinfo += sizeof(beaconsize);
883         }
884
885         if (beaconsize == 0 || beaconsize > *bytesleft) {
886                 *pbeaconinfo += *bytesleft;
887                 *bytesleft = 0;
888                 ret = -1;
889                 goto done;
890         }
891
892         /* Initialize the current working beacon pointer for this BSS iteration */
893         pos = *pbeaconinfo;
894         end = pos + beaconsize;
895
896         /* Advance the return beacon pointer past the current beacon */
897         *pbeaconinfo += beaconsize;
898         *bytesleft -= beaconsize;
899
900         memcpy(bss->bssid, pos, ETH_ALEN);
901         lbs_deb_scan("process_bss: AP BSSID %s\n", print_mac(mac, bss->bssid));
902         pos += ETH_ALEN;
903
904         if ((end - pos) < 12) {
905                 lbs_deb_scan("process_bss: Not enough bytes left\n");
906                 ret = -1;
907                 goto done;
908         }
909
910         /*
911          * next 4 fields are RSSI, time stamp, beacon interval,
912          *   and capability information
913          */
914
915         /* RSSI is 1 byte long */
916         bss->rssi = *pos;
917         lbs_deb_scan("process_bss: RSSI=%02X\n", *pos);
918         pos++;
919
920         /* time stamp is 8 bytes long */
921         pos += 8;
922
923         /* beacon interval is 2 bytes long */
924         bss->beaconperiod = le16_to_cpup((void *) pos);
925         pos += 2;
926
927         /* capability information is 2 bytes long */
928         bss->capability = le16_to_cpup((void *) pos);
929         lbs_deb_scan("process_bss: capabilities = 0x%4X\n", bss->capability);
930         pos += 2;
931
932         if (bss->capability & WLAN_CAPABILITY_PRIVACY)
933                 lbs_deb_scan("process_bss: AP WEP enabled\n");
934         if (bss->capability & WLAN_CAPABILITY_IBSS)
935                 bss->mode = IW_MODE_ADHOC;
936         else
937                 bss->mode = IW_MODE_INFRA;
938
939         /* rest of the current buffer are IE's */
940         lbs_deb_scan("process_bss: IE length for this AP = %zd\n", end - pos);
941         lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
942
943         /* process variable IE */
944         while (pos <= end - 2) {
945                 struct ieee80211_info_element * elem =
946                         (struct ieee80211_info_element *) pos;
947
948                 if (pos + elem->len > end) {
949                         lbs_deb_scan("process_bss: error in processing IE, "
950                                "bytes left < IE length\n");
951                         break;
952                 }
953
954                 switch (elem->id) {
955                 case MFIE_TYPE_SSID:
956                         bss->ssid_len = elem->len;
957                         memcpy(bss->ssid, elem->data, elem->len);
958                         lbs_deb_scan("ssid '%s', ssid length %u\n",
959                                      escape_essid(bss->ssid, bss->ssid_len),
960                                      bss->ssid_len);
961                         break;
962
963                 case MFIE_TYPE_RATES:
964                         n_basic_rates = min_t(u8, MAX_RATES, elem->len);
965                         memcpy(bss->rates, elem->data, n_basic_rates);
966                         got_basic_rates = 1;
967                         break;
968
969                 case MFIE_TYPE_FH_SET:
970                         pFH = (struct ieeetypes_fhparamset *) pos;
971                         memmove(&bss->phyparamset.fhparamset, pFH,
972                                 sizeof(struct ieeetypes_fhparamset));
973 #if 0 /* I think we can store these LE */
974                         bss->phyparamset.fhparamset.dwelltime
975                             = le16_to_cpu(bss->phyparamset.fhparamset.dwelltime);
976 #endif
977                         break;
978
979                 case MFIE_TYPE_DS_SET:
980                         pDS = (struct ieeetypes_dsparamset *) pos;
981                         bss->channel = pDS->currentchan;
982                         memcpy(&bss->phyparamset.dsparamset, pDS,
983                                sizeof(struct ieeetypes_dsparamset));
984                         break;
985
986                 case MFIE_TYPE_CF_SET:
987                         pCF = (struct ieeetypes_cfparamset *) pos;
988                         memcpy(&bss->ssparamset.cfparamset, pCF,
989                                sizeof(struct ieeetypes_cfparamset));
990                         break;
991
992                 case MFIE_TYPE_IBSS_SET:
993                         pibss = (struct ieeetypes_ibssparamset *) pos;
994                         bss->atimwindow = le32_to_cpu(pibss->atimwindow);
995                         memmove(&bss->ssparamset.ibssparamset, pibss,
996                                 sizeof(struct ieeetypes_ibssparamset));
997 #if 0
998                         bss->ssparamset.ibssparamset.atimwindow
999                             = le16_to_cpu(bss->ssparamset.ibssparamset.atimwindow);
1000 #endif
1001                         break;
1002
1003                 case MFIE_TYPE_COUNTRY:
1004                         pcountryinfo = (struct ieeetypes_countryinfoset *) pos;
1005                         if (pcountryinfo->len < sizeof(pcountryinfo->countrycode)
1006                             || pcountryinfo->len > 254) {
1007                                 lbs_deb_scan("process_bss: 11D- Err "
1008                                        "CountryInfo len =%d min=%zd max=254\n",
1009                                        pcountryinfo->len,
1010                                        sizeof(pcountryinfo->countrycode));
1011                                 ret = -1;
1012                                 goto done;
1013                         }
1014
1015                         memcpy(&bss->countryinfo,
1016                                pcountryinfo, pcountryinfo->len + 2);
1017                         lbs_deb_hex(LBS_DEB_SCAN, "process_bss: 11d countryinfo",
1018                                 (u8 *) pcountryinfo,
1019                                 (u32) (pcountryinfo->len + 2));
1020                         break;
1021
1022                 case MFIE_TYPE_RATES_EX:
1023                         /* only process extended supported rate if data rate is
1024                          * already found. Data rate IE should come before
1025                          * extended supported rate IE
1026                          */
1027                         if (!got_basic_rates)
1028                                 break;
1029
1030                         n_ex_rates = elem->len;
1031                         if (n_basic_rates + n_ex_rates > MAX_RATES)
1032                                 n_ex_rates = MAX_RATES - n_basic_rates;
1033
1034                         p = bss->rates + n_basic_rates;
1035                         memcpy(p, elem->data, n_ex_rates);
1036                         break;
1037
1038                 case MFIE_TYPE_GENERIC:
1039                         if (elem->len >= 4 &&
1040                             elem->data[0] == 0x00 &&
1041                             elem->data[1] == 0x50 &&
1042                             elem->data[2] == 0xf2 &&
1043                             elem->data[3] == 0x01) {
1044                                 bss->wpa_ie_len = min(elem->len + 2,
1045                                                       MAX_WPA_IE_LEN);
1046                                 memcpy(bss->wpa_ie, elem, bss->wpa_ie_len);
1047                                 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: WPA IE", bss->wpa_ie,
1048                                             elem->len);
1049                         } else if (elem->len >= MARVELL_MESH_IE_LENGTH &&
1050                             elem->data[0] == 0x00 &&
1051                             elem->data[1] == 0x50 &&
1052                             elem->data[2] == 0x43 &&
1053                             elem->data[3] == 0x04) {
1054                                 bss->mesh = 1;
1055                         }
1056                         break;
1057
1058                 case MFIE_TYPE_RSN:
1059                         bss->rsn_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
1060                         memcpy(bss->rsn_ie, elem, bss->rsn_ie_len);
1061                         lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE", bss->rsn_ie, elem->len);
1062                         break;
1063
1064                 default:
1065                         break;
1066                 }
1067
1068                 pos += elem->len + 2;
1069         }
1070
1071         /* Timestamp */
1072         bss->last_scanned = jiffies;
1073         lbs_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
1074
1075         ret = 0;
1076
1077 done:
1078         lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1079         return ret;
1080 }
1081
1082 /**
1083  *  @brief This function finds a specific compatible BSSID in the scan list
1084  *
1085  *  Used in association code
1086  *
1087  *  @param adapter  A pointer to struct lbs_adapter
1088  *  @param bssid    BSSID to find in the scan list
1089  *  @param mode     Network mode: Infrastructure or IBSS
1090  *
1091  *  @return         index in BSSID list, or error return code (< 0)
1092  */
1093 struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_adapter *adapter,
1094                 u8 * bssid, u8 mode)
1095 {
1096         struct bss_descriptor * iter_bss;
1097         struct bss_descriptor * found_bss = NULL;
1098
1099         lbs_deb_enter(LBS_DEB_SCAN);
1100
1101         if (!bssid)
1102                 goto out;
1103
1104         lbs_deb_hex(LBS_DEB_SCAN, "looking for",
1105                 bssid, ETH_ALEN);
1106
1107         /* Look through the scan table for a compatible match.  The loop will
1108          *   continue past a matched bssid that is not compatible in case there
1109          *   is an AP with multiple SSIDs assigned to the same BSSID
1110          */
1111         mutex_lock(&adapter->lock);
1112         list_for_each_entry (iter_bss, &adapter->network_list, list) {
1113                 if (compare_ether_addr(iter_bss->bssid, bssid))
1114                         continue; /* bssid doesn't match */
1115                 switch (mode) {
1116                 case IW_MODE_INFRA:
1117                 case IW_MODE_ADHOC:
1118                         if (!is_network_compatible(adapter, iter_bss, mode))
1119                                 break;
1120                         found_bss = iter_bss;
1121                         break;
1122                 default:
1123                         found_bss = iter_bss;
1124                         break;
1125                 }
1126         }
1127         mutex_unlock(&adapter->lock);
1128
1129 out:
1130         lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
1131         return found_bss;
1132 }
1133
1134 /**
1135  *  @brief This function finds ssid in ssid list.
1136  *
1137  *  Used in association code
1138  *
1139  *  @param adapter  A pointer to struct lbs_adapter
1140  *  @param ssid     SSID to find in the list
1141  *  @param bssid    BSSID to qualify the SSID selection (if provided)
1142  *  @param mode     Network mode: Infrastructure or IBSS
1143  *
1144  *  @return         index in BSSID list
1145  */
1146 struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_adapter *adapter,
1147                    u8 *ssid, u8 ssid_len, u8 * bssid, u8 mode,
1148                    int channel)
1149 {
1150         u8 bestrssi = 0;
1151         struct bss_descriptor * iter_bss = NULL;
1152         struct bss_descriptor * found_bss = NULL;
1153         struct bss_descriptor * tmp_oldest = NULL;
1154
1155         lbs_deb_enter(LBS_DEB_SCAN);
1156
1157         mutex_lock(&adapter->lock);
1158
1159         list_for_each_entry (iter_bss, &adapter->network_list, list) {
1160                 if (   !tmp_oldest
1161                     || (iter_bss->last_scanned < tmp_oldest->last_scanned))
1162                         tmp_oldest = iter_bss;
1163
1164                 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
1165                                       ssid, ssid_len) != 0)
1166                         continue; /* ssid doesn't match */
1167                 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
1168                         continue; /* bssid doesn't match */
1169                 if ((channel > 0) && (iter_bss->channel != channel))
1170                         continue; /* channel doesn't match */
1171
1172                 switch (mode) {
1173                 case IW_MODE_INFRA:
1174                 case IW_MODE_ADHOC:
1175                         if (!is_network_compatible(adapter, iter_bss, mode))
1176                                 break;
1177
1178                         if (bssid) {
1179                                 /* Found requested BSSID */
1180                                 found_bss = iter_bss;
1181                                 goto out;
1182                         }
1183
1184                         if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1185                                 bestrssi = SCAN_RSSI(iter_bss->rssi);
1186                                 found_bss = iter_bss;
1187                         }
1188                         break;
1189                 case IW_MODE_AUTO:
1190                 default:
1191                         if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1192                                 bestrssi = SCAN_RSSI(iter_bss->rssi);
1193                                 found_bss = iter_bss;
1194                         }
1195                         break;
1196                 }
1197         }
1198
1199 out:
1200         mutex_unlock(&adapter->lock);
1201         lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
1202         return found_bss;
1203 }
1204
1205 /**
1206  *  @brief This function finds the best SSID in the Scan List
1207  *
1208  *  Search the scan table for the best SSID that also matches the current
1209  *   adapter network preference (infrastructure or adhoc)
1210  *
1211  *  @param adapter  A pointer to struct lbs_adapter
1212  *
1213  *  @return         index in BSSID list
1214  */
1215 static struct bss_descriptor *lbs_find_best_ssid_in_list(
1216         struct lbs_adapter *adapter,
1217         u8 mode)
1218 {
1219         u8 bestrssi = 0;
1220         struct bss_descriptor * iter_bss;
1221         struct bss_descriptor * best_bss = NULL;
1222
1223         lbs_deb_enter(LBS_DEB_SCAN);
1224
1225         mutex_lock(&adapter->lock);
1226
1227         list_for_each_entry (iter_bss, &adapter->network_list, list) {
1228                 switch (mode) {
1229                 case IW_MODE_INFRA:
1230                 case IW_MODE_ADHOC:
1231                         if (!is_network_compatible(adapter, iter_bss, mode))
1232                                 break;
1233                         if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1234                                 break;
1235                         bestrssi = SCAN_RSSI(iter_bss->rssi);
1236                         best_bss = iter_bss;
1237                         break;
1238                 case IW_MODE_AUTO:
1239                 default:
1240                         if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1241                                 break;
1242                         bestrssi = SCAN_RSSI(iter_bss->rssi);
1243                         best_bss = iter_bss;
1244                         break;
1245                 }
1246         }
1247
1248         mutex_unlock(&adapter->lock);
1249         lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
1250         return best_bss;
1251 }
1252
1253 /**
1254  *  @brief Find the AP with specific ssid in the scan list
1255  *
1256  *  Used from association worker.
1257  *
1258  *  @param priv         A pointer to struct lbs_private structure
1259  *  @param pSSID        A pointer to AP's ssid
1260  *
1261  *  @return             0--success, otherwise--fail
1262  */
1263 int lbs_find_best_network_ssid(struct lbs_private *priv,
1264                 u8 *out_ssid, u8 *out_ssid_len, u8 preferred_mode, u8 *out_mode)
1265 {
1266         struct lbs_adapter *adapter = priv->adapter;
1267         int ret = -1;
1268         struct bss_descriptor * found;
1269
1270         lbs_deb_enter(LBS_DEB_SCAN);
1271
1272         lbs_scan_networks(priv, NULL, 1);
1273         if (adapter->surpriseremoved)
1274                 goto out;
1275
1276         wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
1277
1278         found = lbs_find_best_ssid_in_list(adapter, preferred_mode);
1279         if (found && (found->ssid_len > 0)) {
1280                 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1281                 *out_ssid_len = found->ssid_len;
1282                 *out_mode = found->mode;
1283                 ret = 0;
1284         }
1285
1286 out:
1287         lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1288         return ret;
1289 }
1290
1291 /**
1292  *  @brief Scan Network
1293  *
1294  *  @param dev          A pointer to net_device structure
1295  *  @param info         A pointer to iw_request_info structure
1296  *  @param vwrq         A pointer to iw_param structure
1297  *  @param extra        A pointer to extra data buf
1298  *
1299  *  @return             0 --success, otherwise fail
1300  */
1301 int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
1302                   struct iw_param *vwrq, char *extra)
1303 {
1304         struct lbs_private *priv = dev->priv;
1305         struct lbs_adapter *adapter = priv->adapter;
1306
1307         lbs_deb_enter(LBS_DEB_SCAN);
1308
1309         if (!delayed_work_pending(&priv->scan_work)) {
1310                 queue_delayed_work(priv->work_thread, &priv->scan_work,
1311                                    msecs_to_jiffies(50));
1312         }
1313
1314         if (adapter->surpriseremoved)
1315                 return -1;
1316
1317         lbs_deb_leave(LBS_DEB_SCAN);
1318         return 0;
1319 }
1320
1321
1322 /**
1323  *  @brief Send a scan command for all available channels filtered on a spec
1324  *
1325  *  Used in association code and from debugfs
1326  *
1327  *  @param priv             A pointer to struct lbs_private structure
1328  *  @param ssid             A pointer to the SSID to scan for
1329  *  @param ssid_len         Length of the SSID
1330  *  @param clear_ssid       Should existing scan results with this SSID
1331  *                          be cleared?
1332  *  @param prequestedssid   A pointer to AP's ssid
1333  *  @param keeppreviousscan Flag used to save/clear scan table before scan
1334  *
1335  *  @return                0-success, otherwise fail
1336  */
1337 int lbs_send_specific_ssid_scan(struct lbs_private *priv,
1338                         u8 *ssid, u8 ssid_len, u8 clear_ssid)
1339 {
1340         struct lbs_adapter *adapter = priv->adapter;
1341         struct lbs_ioctl_user_scan_cfg scancfg;
1342         int ret = 0;
1343
1344         lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s', clear %d",
1345                 escape_essid(ssid, ssid_len), clear_ssid);
1346
1347         if (!ssid_len)
1348                 goto out;
1349
1350         memset(&scancfg, 0x00, sizeof(scancfg));
1351         memcpy(scancfg.ssid, ssid, ssid_len);
1352         scancfg.ssid_len = ssid_len;
1353         scancfg.clear_ssid = clear_ssid;
1354
1355         lbs_scan_networks(priv, &scancfg, 1);
1356         if (adapter->surpriseremoved) {
1357                 ret = -1;
1358                 goto out;
1359         }
1360         wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
1361
1362 out:
1363         lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1364         return ret;
1365 }
1366
1367
1368
1369
1370 /*********************************************************************/
1371 /*                                                                   */
1372 /*  Support for Wireless Extensions                                  */
1373 /*                                                                   */
1374 /*********************************************************************/
1375
1376 #define MAX_CUSTOM_LEN 64
1377
1378 static inline char *lbs_translate_scan(struct lbs_private *priv,
1379                                         char *start, char *stop,
1380                                         struct bss_descriptor *bss)
1381 {
1382         struct lbs_adapter *adapter = priv->adapter;
1383         struct chan_freq_power *cfp;
1384         char *current_val;      /* For rates */
1385         struct iw_event iwe;    /* Temporary buffer */
1386         int j;
1387 #define PERFECT_RSSI ((u8)50)
1388 #define WORST_RSSI   ((u8)0)
1389 #define RSSI_DIFF    ((u8)(PERFECT_RSSI - WORST_RSSI))
1390         u8 rssi;
1391
1392         lbs_deb_enter(LBS_DEB_SCAN);
1393
1394         cfp = lbs_find_cfp_by_band_and_channel(adapter, 0, bss->channel);
1395         if (!cfp) {
1396                 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
1397                 start = NULL;
1398                 goto out;
1399         }
1400
1401         /* First entry *MUST* be the AP BSSID */
1402         iwe.cmd = SIOCGIWAP;
1403         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1404         memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
1405         start = iwe_stream_add_event(start, stop, &iwe, IW_EV_ADDR_LEN);
1406
1407         /* SSID */
1408         iwe.cmd = SIOCGIWESSID;
1409         iwe.u.data.flags = 1;
1410         iwe.u.data.length = min((u32) bss->ssid_len, (u32) IW_ESSID_MAX_SIZE);
1411         start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
1412
1413         /* Mode */
1414         iwe.cmd = SIOCGIWMODE;
1415         iwe.u.mode = bss->mode;
1416         start = iwe_stream_add_event(start, stop, &iwe, IW_EV_UINT_LEN);
1417
1418         /* Frequency */
1419         iwe.cmd = SIOCGIWFREQ;
1420         iwe.u.freq.m = (long)cfp->freq * 100000;
1421         iwe.u.freq.e = 1;
1422         start = iwe_stream_add_event(start, stop, &iwe, IW_EV_FREQ_LEN);
1423
1424         /* Add quality statistics */
1425         iwe.cmd = IWEVQUAL;
1426         iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
1427         iwe.u.qual.level = SCAN_RSSI(bss->rssi);
1428
1429         rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
1430         iwe.u.qual.qual =
1431             (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
1432              (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
1433             (RSSI_DIFF * RSSI_DIFF);
1434         if (iwe.u.qual.qual > 100)
1435                 iwe.u.qual.qual = 100;
1436
1437         if (adapter->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
1438                 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
1439         } else {
1440                 iwe.u.qual.noise =
1441                     CAL_NF(adapter->NF[TYPE_BEACON][TYPE_NOAVG]);
1442         }
1443
1444         /* Locally created ad-hoc BSSs won't have beacons if this is the
1445          * only station in the adhoc network; so get signal strength
1446          * from receive statistics.
1447          */
1448         if ((adapter->mode == IW_MODE_ADHOC)
1449             && adapter->adhoccreate
1450             && !lbs_ssid_cmp(adapter->curbssparams.ssid,
1451                                   adapter->curbssparams.ssid_len,
1452                                   bss->ssid, bss->ssid_len)) {
1453                 int snr, nf;
1454                 snr = adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
1455                 nf = adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
1456                 iwe.u.qual.level = CAL_RSSI(snr, nf);
1457         }
1458         start = iwe_stream_add_event(start, stop, &iwe, IW_EV_QUAL_LEN);
1459
1460         /* Add encryption capability */
1461         iwe.cmd = SIOCGIWENCODE;
1462         if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
1463                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1464         } else {
1465                 iwe.u.data.flags = IW_ENCODE_DISABLED;
1466         }
1467         iwe.u.data.length = 0;
1468         start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
1469
1470         current_val = start + IW_EV_LCP_LEN;
1471
1472         iwe.cmd = SIOCGIWRATE;
1473         iwe.u.bitrate.fixed = 0;
1474         iwe.u.bitrate.disabled = 0;
1475         iwe.u.bitrate.value = 0;
1476
1477         for (j = 0; bss->rates[j] && (j < sizeof(bss->rates)); j++) {
1478                 /* Bit rate given in 500 kb/s units */
1479                 iwe.u.bitrate.value = bss->rates[j] * 500000;
1480                 current_val = iwe_stream_add_value(start, current_val,
1481                                          stop, &iwe, IW_EV_PARAM_LEN);
1482         }
1483         if ((bss->mode == IW_MODE_ADHOC)
1484             && !lbs_ssid_cmp(adapter->curbssparams.ssid,
1485                                   adapter->curbssparams.ssid_len,
1486                                   bss->ssid, bss->ssid_len)
1487             && adapter->adhoccreate) {
1488                 iwe.u.bitrate.value = 22 * 500000;
1489                 current_val = iwe_stream_add_value(start, current_val,
1490                                          stop, &iwe, IW_EV_PARAM_LEN);
1491         }
1492         /* Check if we added any event */
1493         if((current_val - start) > IW_EV_LCP_LEN)
1494                 start = current_val;
1495
1496         memset(&iwe, 0, sizeof(iwe));
1497         if (bss->wpa_ie_len) {
1498                 char buf[MAX_WPA_IE_LEN];
1499                 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
1500                 iwe.cmd = IWEVGENIE;
1501                 iwe.u.data.length = bss->wpa_ie_len;
1502                 start = iwe_stream_add_point(start, stop, &iwe, buf);
1503         }
1504
1505         memset(&iwe, 0, sizeof(iwe));
1506         if (bss->rsn_ie_len) {
1507                 char buf[MAX_WPA_IE_LEN];
1508                 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
1509                 iwe.cmd = IWEVGENIE;
1510                 iwe.u.data.length = bss->rsn_ie_len;
1511                 start = iwe_stream_add_point(start, stop, &iwe, buf);
1512         }
1513
1514         if (bss->mesh) {
1515                 char custom[MAX_CUSTOM_LEN];
1516                 char *p = custom;
1517
1518                 iwe.cmd = IWEVCUSTOM;
1519                 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
1520                               "mesh-type: olpc");
1521                 iwe.u.data.length = p - custom;
1522                 if (iwe.u.data.length)
1523                         start = iwe_stream_add_point(start, stop, &iwe, custom);
1524         }
1525
1526 out:
1527         lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
1528         return start;
1529 }
1530
1531 /**
1532  *  @brief  Handle Retrieve scan table ioctl
1533  *
1534  *  @param dev          A pointer to net_device structure
1535  *  @param info         A pointer to iw_request_info structure
1536  *  @param dwrq         A pointer to iw_point structure
1537  *  @param extra        A pointer to extra data buf
1538  *
1539  *  @return             0 --success, otherwise fail
1540  */
1541 int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
1542                   struct iw_point *dwrq, char *extra)
1543 {
1544 #define SCAN_ITEM_SIZE 128
1545         struct lbs_private *priv = dev->priv;
1546         struct lbs_adapter *adapter = priv->adapter;
1547         int err = 0;
1548         char *ev = extra;
1549         char *stop = ev + dwrq->length;
1550         struct bss_descriptor * iter_bss;
1551         struct bss_descriptor * safe;
1552
1553         lbs_deb_enter(LBS_DEB_SCAN);
1554
1555         /* Update RSSI if current BSS is a locally created ad-hoc BSS */
1556         if ((adapter->mode == IW_MODE_ADHOC) && adapter->adhoccreate) {
1557                 lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
1558                                         CMD_OPTION_WAITFORRSP, 0, NULL);
1559         }
1560
1561         mutex_lock(&adapter->lock);
1562         list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
1563                 char * next_ev;
1564                 unsigned long stale_time;
1565
1566                 if (stop - ev < SCAN_ITEM_SIZE) {
1567                         err = -E2BIG;
1568                         break;
1569                 }
1570
1571                 /* For mesh device, list only mesh networks */
1572                 if (dev == priv->mesh_dev && !iter_bss->mesh)
1573                         continue;
1574
1575                 /* Prune old an old scan result */
1576                 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1577                 if (time_after(jiffies, stale_time)) {
1578                         list_move_tail (&iter_bss->list,
1579                                         &adapter->network_free_list);
1580                         clear_bss_descriptor(iter_bss);
1581                         continue;
1582                 }
1583
1584                 /* Translate to WE format this entry */
1585                 next_ev = lbs_translate_scan(priv, ev, stop, iter_bss);
1586                 if (next_ev == NULL)
1587                         continue;
1588                 ev = next_ev;
1589         }
1590         mutex_unlock(&adapter->lock);
1591
1592         dwrq->length = (ev - extra);
1593         dwrq->flags = 0;
1594
1595         lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", err);
1596         return err;
1597 }
1598
1599
1600
1601
1602 /*********************************************************************/
1603 /*                                                                   */
1604 /*  Command execution                                                */
1605 /*                                                                   */
1606 /*********************************************************************/
1607
1608
1609 /**
1610  *  @brief Prepare a scan command to be sent to the firmware
1611  *
1612  *  Called from lbs_prepare_and_send_command() in cmd.c
1613  *
1614  *  Sends a fixed lenght data part (specifying the BSS type and BSSID filters)
1615  *  as well as a variable number/length of TLVs to the firmware.
1616  *
1617  *  @param priv       A pointer to struct lbs_private structure
1618  *  @param cmd        A pointer to cmd_ds_command structure to be sent to
1619  *                    firmware with the cmd_DS_801_11_SCAN structure
1620  *  @param pdata_buf  Void pointer cast of a lbs_scan_cmd_config struct used
1621  *                    to set the fields/TLVs for the command sent to firmware
1622  *
1623  *  @return           0 or -1
1624  */
1625 int lbs_cmd_80211_scan(struct lbs_private *priv,
1626                          struct cmd_ds_command *cmd, void *pdata_buf)
1627 {
1628         struct cmd_ds_802_11_scan *pscan = &cmd->params.scan;
1629         struct lbs_scan_cmd_config *pscancfg = pdata_buf;
1630
1631         lbs_deb_enter(LBS_DEB_SCAN);
1632
1633         /* Set fixed field variables in scan command */
1634         pscan->bsstype = pscancfg->bsstype;
1635         memcpy(pscan->bssid, pscancfg->bssid, ETH_ALEN);
1636         memcpy(pscan->tlvbuffer, pscancfg->tlvbuffer, pscancfg->tlvbufferlen);
1637
1638         cmd->command = cpu_to_le16(CMD_802_11_SCAN);
1639
1640         /* size is equal to the sizeof(fixed portions) + the TLV len + header */
1641         cmd->size = cpu_to_le16(sizeof(pscan->bsstype) + ETH_ALEN
1642                                 + pscancfg->tlvbufferlen + S_DS_GEN);
1643
1644         lbs_deb_scan("SCAN_CMD: command 0x%04x, size %d, seqnum %d\n",
1645                      le16_to_cpu(cmd->command), le16_to_cpu(cmd->size),
1646                      le16_to_cpu(cmd->seqnum));
1647
1648         lbs_deb_leave(LBS_DEB_SCAN);
1649         return 0;
1650 }
1651
1652 static inline int is_same_network(struct bss_descriptor *src,
1653                                   struct bss_descriptor *dst)
1654 {
1655         /* A network is only a duplicate if the channel, BSSID, and ESSID
1656          * all match.  We treat all <hidden> with the same BSSID and channel
1657          * as one network */
1658         return ((src->ssid_len == dst->ssid_len) &&
1659                 (src->channel == dst->channel) &&
1660                 !compare_ether_addr(src->bssid, dst->bssid) &&
1661                 !memcmp(src->ssid, dst->ssid, src->ssid_len));
1662 }
1663
1664 /**
1665  *  @brief This function handles the command response of scan
1666  *
1667  *  Called from handle_cmd_response() in cmdrespc.
1668  *
1669  *   The response buffer for the scan command has the following
1670  *      memory layout:
1671  *
1672  *     .-----------------------------------------------------------.
1673  *     |  header (4 * sizeof(u16)):  Standard command response hdr |
1674  *     .-----------------------------------------------------------.
1675  *     |  bufsize (u16) : sizeof the BSS Description data          |
1676  *     .-----------------------------------------------------------.
1677  *     |  NumOfSet (u8) : Number of BSS Descs returned             |
1678  *     .-----------------------------------------------------------.
1679  *     |  BSSDescription data (variable, size given in bufsize)    |
1680  *     .-----------------------------------------------------------.
1681  *     |  TLV data (variable, size calculated using header->size,  |
1682  *     |            bufsize and sizeof the fixed fields above)     |
1683  *     .-----------------------------------------------------------.
1684  *
1685  *  @param priv    A pointer to struct lbs_private structure
1686  *  @param resp    A pointer to cmd_ds_command
1687  *
1688  *  @return        0 or -1
1689  */
1690 int lbs_ret_80211_scan(struct lbs_private *priv, struct cmd_ds_command *resp)
1691 {
1692         struct lbs_adapter *adapter = priv->adapter;
1693         struct cmd_ds_802_11_scan_rsp *pscan;
1694         struct bss_descriptor * iter_bss;
1695         struct bss_descriptor * safe;
1696         u8 *pbssinfo;
1697         u16 scanrespsize;
1698         int bytesleft;
1699         int idx;
1700         int tlvbufsize;
1701         int ret;
1702
1703         lbs_deb_enter(LBS_DEB_SCAN);
1704
1705         /* Prune old entries from scan table */
1706         list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
1707                 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1708                 if (time_before(jiffies, stale_time))
1709                         continue;
1710                 list_move_tail (&iter_bss->list, &adapter->network_free_list);
1711                 clear_bss_descriptor(iter_bss);
1712         }
1713
1714         pscan = &resp->params.scanresp;
1715
1716         if (pscan->nr_sets > MAX_NETWORK_COUNT) {
1717                 lbs_deb_scan(
1718                        "SCAN_RESP: too many scan results (%d, max %d)!!\n",
1719                        pscan->nr_sets, MAX_NETWORK_COUNT);
1720                 ret = -1;
1721                 goto done;
1722         }
1723
1724         bytesleft = le16_to_cpu(get_unaligned((u16*)&pscan->bssdescriptsize));
1725         lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
1726
1727         scanrespsize = le16_to_cpu(get_unaligned((u16*)&resp->size));
1728         lbs_deb_scan("SCAN_RESP: returned %d AP before parsing\n",
1729                pscan->nr_sets);
1730
1731         pbssinfo = pscan->bssdesc_and_tlvbuffer;
1732
1733         /* The size of the TLV buffer is equal to the entire command response
1734          *   size (scanrespsize) minus the fixed fields (sizeof()'s), the
1735          *   BSS Descriptions (bssdescriptsize as bytesLef) and the command
1736          *   response header (S_DS_GEN)
1737          */
1738         tlvbufsize = scanrespsize - (bytesleft + sizeof(pscan->bssdescriptsize)
1739                                      + sizeof(pscan->nr_sets)
1740                                      + S_DS_GEN);
1741
1742         /*
1743          *  Process each scan response returned (pscan->nr_sets).  Save
1744          *    the information in the newbssentry and then insert into the
1745          *    driver scan table either as an update to an existing entry
1746          *    or as an addition at the end of the table
1747          */
1748         for (idx = 0; idx < pscan->nr_sets && bytesleft; idx++) {
1749                 struct bss_descriptor new;
1750                 struct bss_descriptor * found = NULL;
1751                 struct bss_descriptor * oldest = NULL;
1752                 DECLARE_MAC_BUF(mac);
1753
1754                 /* Process the data fields and IEs returned for this BSS */
1755                 memset(&new, 0, sizeof (struct bss_descriptor));
1756                 if (lbs_process_bss(&new, &pbssinfo, &bytesleft) != 0) {
1757                         /* error parsing the scan response, skipped */
1758                         lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1759                         continue;
1760                 }
1761
1762                 /* Try to find this bss in the scan table */
1763                 list_for_each_entry (iter_bss, &adapter->network_list, list) {
1764                         if (is_same_network(iter_bss, &new)) {
1765                                 found = iter_bss;
1766                                 break;
1767                         }
1768
1769                         if ((oldest == NULL) ||
1770                             (iter_bss->last_scanned < oldest->last_scanned))
1771                                 oldest = iter_bss;
1772                 }
1773
1774                 if (found) {
1775                         /* found, clear it */
1776                         clear_bss_descriptor(found);
1777                 } else if (!list_empty(&adapter->network_free_list)) {
1778                         /* Pull one from the free list */
1779                         found = list_entry(adapter->network_free_list.next,
1780                                            struct bss_descriptor, list);
1781                         list_move_tail(&found->list, &adapter->network_list);
1782                 } else if (oldest) {
1783                         /* If there are no more slots, expire the oldest */
1784                         found = oldest;
1785                         clear_bss_descriptor(found);
1786                         list_move_tail(&found->list, &adapter->network_list);
1787                 } else {
1788                         continue;
1789                 }
1790
1791                 lbs_deb_scan("SCAN_RESP: BSSID = %s\n",
1792                              print_mac(mac, new.bssid));
1793
1794                 /* Copy the locally created newbssentry to the scan table */
1795                 memcpy(found, &new, offsetof(struct bss_descriptor, list));
1796         }
1797
1798         ret = 0;
1799
1800 done:
1801         lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1802         return ret;
1803 }