[PATCH] libertas: make debug configurable
[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
12 #include <net/ieee80211.h>
13 #include <net/iw_handler.h>
14
15 #include "host.h"
16 #include "decl.h"
17 #include "dev.h"
18 #include "scan.h"
19
20 //! Approximate amount of data needed to pass a scan result back to iwlist
21 #define MAX_SCAN_CELL_SIZE  (IW_EV_ADDR_LEN             \
22                              + IW_ESSID_MAX_SIZE        \
23                              + IW_EV_UINT_LEN           \
24                              + IW_EV_FREQ_LEN           \
25                              + IW_EV_QUAL_LEN           \
26                              + IW_ESSID_MAX_SIZE        \
27                              + IW_EV_PARAM_LEN          \
28                              + 40)      /* 40 for WPAIE */
29
30 //! Memory needed to store a max sized channel List TLV for a firmware scan
31 #define CHAN_TLV_MAX_SIZE  (sizeof(struct mrvlietypesheader)    \
32                             + (MRVDRV_MAX_CHANNELS_PER_SCAN     \
33                                * sizeof(struct chanscanparamset)))
34
35 //! Memory needed to store a max number/size SSID TLV for a firmware scan
36 #define SSID_TLV_MAX_SIZE  (1 * sizeof(struct mrvlietypes_ssidparamset))
37
38 //! Maximum memory needed for a wlan_scan_cmd_config with all TLVs at max
39 #define MAX_SCAN_CFG_ALLOC (sizeof(struct wlan_scan_cmd_config)  \
40                             + sizeof(struct mrvlietypes_numprobes)   \
41                             + CHAN_TLV_MAX_SIZE                 \
42                             + SSID_TLV_MAX_SIZE)
43
44 //! The maximum number of channels the firmware can scan per command
45 #define MRVDRV_MAX_CHANNELS_PER_SCAN   14
46
47 /**
48  * @brief Number of channels to scan per firmware scan command issuance.
49  *
50  *  Number restricted to prevent hitting the limit on the amount of scan data
51  *  returned in a single firmware scan command.
52  */
53 #define MRVDRV_CHANNELS_PER_SCAN_CMD   4
54
55 //! Scan time specified in the channel TLV for each channel for passive scans
56 #define MRVDRV_PASSIVE_SCAN_CHAN_TIME  100
57
58 //! Scan time specified in the channel TLV for each channel for active scans
59 #define MRVDRV_ACTIVE_SCAN_CHAN_TIME   100
60
61 //! Macro to enable/disable SSID checking before storing a scan table
62 #ifdef DISCARD_BAD_SSID
63 #define CHECK_SSID_IS_VALID(x) ssid_valid(&bssidEntry.ssid)
64 #else
65 #define CHECK_SSID_IS_VALID(x) 1
66 #endif
67
68 /**
69  *  @brief Check if a scanned network compatible with the driver settings
70  *
71  *   WEP     WPA     WPA2    ad-hoc  encrypt                      Network
72  * enabled enabled  enabled   AES     mode   privacy  WPA  WPA2  Compatible
73  *    0       0        0       0      NONE      0      0    0   yes No security
74  *    1       0        0       0      NONE      1      0    0   yes Static WEP
75  *    0       1        0       0       x        1x     1    x   yes WPA
76  *    0       0        1       0       x        1x     x    1   yes WPA2
77  *    0       0        0       1      NONE      1      0    0   yes Ad-hoc AES
78  *    0       0        0       0     !=NONE     1      0    0   yes Dynamic WEP
79  *
80  *
81  *  @param adapter A pointer to wlan_adapter
82  *  @param index   Index in scantable to check against current driver settings
83  *  @param mode    Network mode: Infrastructure or IBSS
84  *
85  *  @return        Index in scantable, or error code if negative
86  */
87 static int is_network_compatible(wlan_adapter * adapter, int index, u8 mode)
88 {
89         lbs_deb_enter(LBS_DEB_ASSOC);
90
91         if (adapter->scantable[index].mode == mode) {
92                 if (   !adapter->secinfo.wep_enabled
93                     && !adapter->secinfo.WPAenabled
94                     && !adapter->secinfo.WPA2enabled
95                     && adapter->scantable[index].wpa_ie[0] != WPA_IE
96                     && adapter->scantable[index].rsn_ie[0] != WPA2_IE
97                     && !adapter->scantable[index].privacy) {
98                         /* no security */
99                         goto done;
100                 } else if (   adapter->secinfo.wep_enabled
101                            && !adapter->secinfo.WPAenabled
102                            && !adapter->secinfo.WPA2enabled
103                            && adapter->scantable[index].privacy) {
104                         /* static WEP enabled */
105                         goto done;
106                 } else if (   !adapter->secinfo.wep_enabled
107                            && adapter->secinfo.WPAenabled
108                            && !adapter->secinfo.WPA2enabled
109                            && (adapter->scantable[index].wpa_ie[0] == WPA_IE)
110                            /* privacy bit may NOT be set in some APs like LinkSys WRT54G
111                               && adapter->scantable[index].privacy */
112                     ) {
113                         /* WPA enabled */
114             lbs_deb_scan(
115                                "is_network_compatible() WPA: index=%d wpa_ie=%#x "
116                                "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s "
117                                "privacy=%#x\n", index,
118                                adapter->scantable[index].wpa_ie[0],
119                                adapter->scantable[index].rsn_ie[0],
120                                adapter->secinfo.wep_enabled ? "e" : "d",
121                                adapter->secinfo.WPAenabled ? "e" : "d",
122                                adapter->secinfo.WPA2enabled ? "e" : "d",
123                                adapter->scantable[index].privacy);
124                         goto done;
125                 } else if (   !adapter->secinfo.wep_enabled
126                            && !adapter->secinfo.WPAenabled
127                            && adapter->secinfo.WPA2enabled
128                            && (adapter->scantable[index].rsn_ie[0] == WPA2_IE)
129                            /* privacy bit may NOT be set in some APs like LinkSys WRT54G
130                               && adapter->scantable[index].privacy */
131                     ) {
132                         /* WPA2 enabled */
133             lbs_deb_scan(
134                                "is_network_compatible() WPA2: index=%d wpa_ie=%#x "
135                                "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s "
136                                "privacy=%#x\n", index,
137                                adapter->scantable[index].wpa_ie[0],
138                                adapter->scantable[index].rsn_ie[0],
139                                adapter->secinfo.wep_enabled ? "e" : "d",
140                                adapter->secinfo.WPAenabled ? "e" : "d",
141                                adapter->secinfo.WPA2enabled ? "e" : "d",
142                                adapter->scantable[index].privacy);
143                         goto done;
144                 } else if (   !adapter->secinfo.wep_enabled
145                            && !adapter->secinfo.WPAenabled
146                            && !adapter->secinfo.WPA2enabled
147                            && (adapter->scantable[index].wpa_ie[0] != WPA_IE)
148                            && (adapter->scantable[index].rsn_ie[0] != WPA2_IE)
149                            && adapter->scantable[index].privacy) {
150                         /* dynamic WEP enabled */
151             lbs_deb_scan(
152                                "is_network_compatible() dynamic WEP: index=%d "
153                                "wpa_ie=%#x wpa2_ie=%#x privacy=%#x\n",
154                                index,
155                                adapter->scantable[index].wpa_ie[0],
156                                adapter->scantable[index].rsn_ie[0],
157                                adapter->scantable[index].privacy);
158                         goto done;
159                 }
160
161                 /* security doesn't match */
162         lbs_deb_scan(
163                        "is_network_compatible() FAILED: index=%d wpa_ie=%#x "
164                        "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s privacy=%#x\n",
165                        index,
166                        adapter->scantable[index].wpa_ie[0],
167                        adapter->scantable[index].rsn_ie[0],
168                        adapter->secinfo.wep_enabled ? "e" : "d",
169                        adapter->secinfo.WPAenabled ? "e" : "d",
170                        adapter->secinfo.WPA2enabled ? "e" : "d",
171                        adapter->scantable[index].privacy);
172                 index = -ECONNREFUSED;
173                 goto done;
174         }
175
176         /* mode doesn't match */
177         index = -ENETUNREACH;
178
179 done:
180         lbs_deb_leave_args(LBS_DEB_SCAN, "index %d", index);
181         return index;
182 }
183
184 /**
185  *  @brief This function validates a SSID as being able to be printed
186  *
187  *  @param pssid   SSID structure to validate
188  *
189  *  @return        TRUE or FALSE
190  */
191 static u8 ssid_valid(struct WLAN_802_11_SSID *pssid)
192 {
193         int ssididx;
194
195         for (ssididx = 0; ssididx < pssid->ssidlength; ssididx++) {
196                 if (!isprint(pssid->ssid[ssididx])) {
197                         return 0;
198                 }
199         }
200
201         return 1;
202 }
203
204 /**
205  *  @brief Post process the scan table after a new scan command has completed
206  *
207  *  Inspect each entry of the scan table and try to find an entry that
208  *    matches our current associated/joined network from the scan.  If
209  *    one is found, update the stored copy of the bssdescriptor for our
210  *    current network.
211  *
212  *  Debug dump the current scan table contents if compiled accordingly.
213  *
214  *  @param priv   A pointer to wlan_private structure
215  *
216  *  @return       void
217  */
218 static void wlan_scan_process_results(wlan_private * priv)
219 {
220         wlan_adapter *adapter = priv->adapter;
221         int foundcurrent;
222         int i;
223
224         foundcurrent = 0;
225
226         if (adapter->connect_status == libertas_connected) {
227                 /* try to find the current BSSID in the new scan list */
228                 for (i = 0; i < adapter->numinscantable; i++) {
229                         if (!libertas_SSID_cmp(&adapter->scantable[i].ssid,
230                                      &adapter->curbssparams.ssid) &&
231                             !memcmp(adapter->curbssparams.bssid,
232                                     adapter->scantable[i].macaddress,
233                                     ETH_ALEN)) {
234                                 foundcurrent = 1;
235                         }
236                 }
237
238                 if (foundcurrent) {
239                         /* Make a copy of current BSSID descriptor */
240                         memcpy(&adapter->curbssparams.bssdescriptor,
241                                &adapter->scantable[i],
242                                sizeof(adapter->curbssparams.bssdescriptor));
243                 }
244         }
245
246         for (i = 0; i < adapter->numinscantable; i++) {
247                 lbs_deb_scan("Scan:(%02d) %02x:%02x:%02x:%02x:%02x:%02x, "
248                        "RSSI[%03d], SSID[%s]\n",
249                        i,
250                        adapter->scantable[i].macaddress[0],
251                        adapter->scantable[i].macaddress[1],
252                        adapter->scantable[i].macaddress[2],
253                        adapter->scantable[i].macaddress[3],
254                        adapter->scantable[i].macaddress[4],
255                        adapter->scantable[i].macaddress[5],
256                        (s32) adapter->scantable[i].rssi,
257                        adapter->scantable[i].ssid.ssid);
258         }
259 }
260
261 /**
262  *  @brief Create a channel list for the driver to scan based on region info
263  *
264  *  Use the driver region/band information to construct a comprehensive list
265  *    of channels to scan.  This routine is used for any scan that is not
266  *    provided a specific channel list to scan.
267  *
268  *  @param priv          A pointer to wlan_private structure
269  *  @param scanchanlist  Output parameter: resulting channel list to scan
270  *  @param filteredscan  Flag indicating whether or not a BSSID or SSID filter
271  *                       is being sent in the command to firmware.  Used to
272  *                       increase the number of channels sent in a scan
273  *                       command and to disable the firmware channel scan
274  *                       filter.
275  *
276  *  @return              void
277  */
278 static void wlan_scan_create_channel_list(wlan_private * priv,
279                                           struct chanscanparamset * scanchanlist,
280                                           u8 filteredscan)
281 {
282
283         wlan_adapter *adapter = priv->adapter;
284         struct region_channel *scanregion;
285         struct chan_freq_power *cfp;
286         int rgnidx;
287         int chanidx;
288         int nextchan;
289         u8 scantype;
290
291         chanidx = 0;
292
293         /* Set the default scan type to the user specified type, will later
294          *   be changed to passive on a per channel basis if restricted by
295          *   regulatory requirements (11d or 11h)
296          */
297         scantype = adapter->scantype;
298
299         for (rgnidx = 0; rgnidx < ARRAY_SIZE(adapter->region_channel); rgnidx++) {
300                 if (priv->adapter->enable11d &&
301                     adapter->connect_status != libertas_connected) {
302                         /* Scan all the supported chan for the first scan */
303                         if (!adapter->universal_channel[rgnidx].valid)
304                                 continue;
305                         scanregion = &adapter->universal_channel[rgnidx];
306
307                         /* clear the parsed_region_chan for the first scan */
308                         memset(&adapter->parsed_region_chan, 0x00,
309                                sizeof(adapter->parsed_region_chan));
310                 } else {
311                         if (!adapter->region_channel[rgnidx].valid)
312                                 continue;
313                         scanregion = &adapter->region_channel[rgnidx];
314                 }
315
316                 for (nextchan = 0;
317                      nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
318
319                         cfp = scanregion->CFP + nextchan;
320
321                         if (priv->adapter->enable11d) {
322                                 scantype =
323                                     libertas_get_scan_type_11d(cfp->channel,
324                                                            &adapter->
325                                                            parsed_region_chan);
326                         }
327
328                         switch (scanregion->band) {
329                         case BAND_B:
330                         case BAND_G:
331                         default:
332                                 scanchanlist[chanidx].radiotype =
333                                     cmd_scan_radio_type_bg;
334                                 break;
335                         }
336
337                         if (scantype == cmd_scan_type_passive) {
338                                 scanchanlist[chanidx].maxscantime =
339                                     cpu_to_le16
340                                     (MRVDRV_PASSIVE_SCAN_CHAN_TIME);
341                                 scanchanlist[chanidx].chanscanmode.passivescan =
342                                     1;
343                         } else {
344                                 scanchanlist[chanidx].maxscantime =
345                                     cpu_to_le16
346                                     (MRVDRV_ACTIVE_SCAN_CHAN_TIME);
347                                 scanchanlist[chanidx].chanscanmode.passivescan =
348                                     0;
349                         }
350
351                         scanchanlist[chanidx].channumber = cfp->channel;
352
353                         if (filteredscan) {
354                                 scanchanlist[chanidx].chanscanmode.
355                                     disablechanfilt = 1;
356                         }
357                 }
358         }
359 }
360
361 /**
362  *  @brief Construct a wlan_scan_cmd_config structure to use in issue scan cmds
363  *
364  *  Application layer or other functions can invoke wlan_scan_networks
365  *    with a scan configuration supplied in a wlan_ioctl_user_scan_cfg struct.
366  *    This structure is used as the basis of one or many wlan_scan_cmd_config
367  *    commands that are sent to the command processing module and sent to
368  *    firmware.
369  *
370  *  Create a wlan_scan_cmd_config based on the following user supplied
371  *    parameters (if present):
372  *             - SSID filter
373  *             - BSSID filter
374  *             - Number of Probes to be sent
375  *             - channel list
376  *
377  *  If the SSID or BSSID filter is not present, disable/clear the filter.
378  *  If the number of probes is not set, use the adapter default setting
379  *  Qualify the channel
380  *
381  *  @param priv             A pointer to wlan_private structure
382  *  @param puserscanin      NULL or pointer to scan configuration parameters
383  *  @param ppchantlvout     Output parameter: Pointer to the start of the
384  *                          channel TLV portion of the output scan config
385  *  @param pscanchanlist    Output parameter: Pointer to the resulting channel
386  *                          list to scan
387  *  @param pmaxchanperscan  Output parameter: Number of channels to scan for
388  *                          each issuance of the firmware scan command
389  *  @param pfilteredscan    Output parameter: Flag indicating whether or not
390  *                          a BSSID or SSID filter is being sent in the
391  *                          command to firmware.  Used to increase the number
392  *                          of channels sent in a scan command and to
393  *                          disable the firmware channel scan filter.
394  *  @param pscancurrentonly Output parameter: Flag indicating whether or not
395  *                          we are only scanning our current active channel
396  *
397  *  @return                 resulting scan configuration
398  */
399 static struct wlan_scan_cmd_config *
400 wlan_scan_setup_scan_config(wlan_private * priv,
401                             const struct wlan_ioctl_user_scan_cfg * puserscanin,
402                             struct mrvlietypes_chanlistparamset ** ppchantlvout,
403                             struct chanscanparamset * pscanchanlist,
404                             int *pmaxchanperscan,
405                             u8 * pfilteredscan,
406                             u8 * pscancurrentonly)
407 {
408         wlan_adapter *adapter = priv->adapter;
409         const u8 zeromac[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
410         struct mrvlietypes_numprobes *pnumprobestlv;
411         struct mrvlietypes_ssidparamset *pssidtlv;
412         struct wlan_scan_cmd_config * pscancfgout = NULL;
413         u8 *ptlvpos;
414         u16 numprobes;
415         u16 ssidlen;
416         int chanidx;
417         int scantype;
418         int scandur;
419         int channel;
420         int radiotype;
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
459                 /* Set the bss type scan filter, use adapter setting if unset */
460                 pscancfgout->bsstype =
461                     (puserscanin->bsstype ? puserscanin->bsstype : adapter->
462                      scanmode);
463
464                 /* Set the number of probes to send, use adapter setting if unset */
465                 numprobes = (puserscanin->numprobes ? puserscanin->numprobes :
466                              adapter->scanprobes);
467
468                 /*
469                  * Set the BSSID filter to the incoming configuration,
470                  *   if non-zero.  If not set, it will remain disabled (all zeros).
471                  */
472                 memcpy(pscancfgout->specificBSSID,
473                        puserscanin->specificBSSID,
474                        sizeof(pscancfgout->specificBSSID));
475
476                 ssidlen = strlen(puserscanin->specificSSID);
477
478                 if (ssidlen) {
479                         pssidtlv =
480                             (struct mrvlietypes_ssidparamset *) pscancfgout->
481                             tlvbuffer;
482                         pssidtlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
483                         pssidtlv->header.len = cpu_to_le16(ssidlen);
484                         memcpy(pssidtlv->ssid, puserscanin->specificSSID,
485                                ssidlen);
486                         ptlvpos += sizeof(pssidtlv->header) + ssidlen;
487                 }
488
489                 /*
490                  *  The default number of channels sent in the command is low to
491                  *    ensure the response buffer from the firmware does not truncate
492                  *    scan results.  That is not an issue with an SSID or BSSID
493                  *    filter applied to the scan results in the firmware.
494                  */
495                 if (ssidlen || (memcmp(pscancfgout->specificBSSID,
496                                        &zeromac, sizeof(zeromac)) != 0)) {
497                         *pmaxchanperscan = MRVDRV_MAX_CHANNELS_PER_SCAN;
498                         *pfilteredscan = 1;
499                 }
500         } else {
501                 pscancfgout->bsstype = adapter->scanmode;
502                 numprobes = adapter->scanprobes;
503         }
504
505         /* If the input config or adapter has the number of Probes set, add tlv */
506         if (numprobes) {
507                 pnumprobestlv = (struct mrvlietypes_numprobes *) ptlvpos;
508                 pnumprobestlv->header.type =
509                     cpu_to_le16(TLV_TYPE_NUMPROBES);
510                 pnumprobestlv->header.len = sizeof(pnumprobestlv->numprobes);
511                 pnumprobestlv->numprobes = cpu_to_le16(numprobes);
512
513                 ptlvpos +=
514                     sizeof(pnumprobestlv->header) + pnumprobestlv->header.len;
515
516                 pnumprobestlv->header.len =
517                     cpu_to_le16(pnumprobestlv->header.len);
518         }
519
520         /*
521          * Set the output for the channel TLV to the address in the tlv buffer
522          *   past any TLVs that were added in this fuction (SSID, numprobes).
523          *   channel TLVs will be added past this for each scan command, preserving
524          *   the TLVs that were previously added.
525          */
526         *ppchantlvout = (struct mrvlietypes_chanlistparamset *) ptlvpos;
527
528         if (puserscanin && puserscanin->chanlist[0].channumber) {
529
530                 lbs_deb_scan("Scan: Using supplied channel list\n");
531
532                 for (chanidx = 0;
533                      chanidx < WLAN_IOCTL_USER_SCAN_CHAN_MAX
534                      && puserscanin->chanlist[chanidx].channumber; chanidx++) {
535
536                         channel = puserscanin->chanlist[chanidx].channumber;
537                         (pscanchanlist + chanidx)->channumber = channel;
538
539                         radiotype = puserscanin->chanlist[chanidx].radiotype;
540                         (pscanchanlist + chanidx)->radiotype = radiotype;
541
542                         scantype = puserscanin->chanlist[chanidx].scantype;
543
544                         if (scantype == cmd_scan_type_passive) {
545                                 (pscanchanlist +
546                                  chanidx)->chanscanmode.passivescan = 1;
547                         } else {
548                                 (pscanchanlist +
549                                  chanidx)->chanscanmode.passivescan = 0;
550                         }
551
552                         if (puserscanin->chanlist[chanidx].scantime) {
553                                 scandur =
554                                     puserscanin->chanlist[chanidx].scantime;
555                         } else {
556                                 if (scantype == cmd_scan_type_passive) {
557                                         scandur = MRVDRV_PASSIVE_SCAN_CHAN_TIME;
558                                 } else {
559                                         scandur = MRVDRV_ACTIVE_SCAN_CHAN_TIME;
560                                 }
561                         }
562
563                         (pscanchanlist + chanidx)->minscantime =
564                             cpu_to_le16(scandur);
565                         (pscanchanlist + chanidx)->maxscantime =
566                             cpu_to_le16(scandur);
567                 }
568
569                 /* Check if we are only scanning the current channel */
570                 if ((chanidx == 1) && (puserscanin->chanlist[0].channumber
571                                        ==
572                                        priv->adapter->curbssparams.channel)) {
573                         *pscancurrentonly = 1;
574                         lbs_deb_scan("Scan: Scanning current channel only");
575                 }
576
577         } else {
578                 lbs_deb_scan("Scan: Creating full region channel list\n");
579                 wlan_scan_create_channel_list(priv, pscanchanlist,
580                                               *pfilteredscan);
581         }
582
583 out:
584         return pscancfgout;
585 }
586
587 /**
588  *  @brief Construct and send multiple scan config commands to the firmware
589  *
590  *  Previous routines have created a wlan_scan_cmd_config with any requested
591  *   TLVs.  This function splits the channel TLV into maxchanperscan lists
592  *   and sends the portion of the channel TLV along with the other TLVs
593  *   to the wlan_cmd routines for execution in the firmware.
594  *
595  *  @param priv            A pointer to wlan_private structure
596  *  @param maxchanperscan  Maximum number channels to be included in each
597  *                         scan command sent to firmware
598  *  @param filteredscan    Flag indicating whether or not a BSSID or SSID
599  *                         filter is being used for the firmware command
600  *                         scan command sent to firmware
601  *  @param pscancfgout     Scan configuration used for this scan.
602  *  @param pchantlvout     Pointer in the pscancfgout where the channel TLV
603  *                         should start.  This is past any other TLVs that
604  *                         must be sent down in each firmware command.
605  *  @param pscanchanlist   List of channels to scan in maxchanperscan segments
606  *
607  *  @return                0 or error return otherwise
608  */
609 static int wlan_scan_channel_list(wlan_private * priv,
610                                   int maxchanperscan,
611                                   u8 filteredscan,
612                                   struct wlan_scan_cmd_config * pscancfgout,
613                                   struct mrvlietypes_chanlistparamset * pchantlvout,
614                                   struct chanscanparamset * pscanchanlist,
615                                   const struct wlan_ioctl_user_scan_cfg * puserscanin,
616                                   int full_scan)
617 {
618         struct chanscanparamset *ptmpchan;
619         struct chanscanparamset *pstartchan;
620         u8 scanband;
621         int doneearly;
622         int tlvidx;
623         int ret = 0;
624         int scanned = 0;
625         union iwreq_data wrqu;
626
627         lbs_deb_enter(LBS_DEB_ASSOC);
628
629         if (pscancfgout == 0 || pchantlvout == 0 || pscanchanlist == 0) {
630                 lbs_deb_scan("Scan: Null detect: %p, %p, %p\n",
631                        pscancfgout, pchantlvout, pscanchanlist);
632                 return -1;
633         }
634
635         pchantlvout->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
636
637         /* Set the temp channel struct pointer to the start of the desired list */
638         ptmpchan = pscanchanlist;
639
640         if (priv->adapter->last_scanned_channel && !puserscanin)
641                 ptmpchan += priv->adapter->last_scanned_channel;
642
643         /* Loop through the desired channel list, sending a new firmware scan
644          *   commands for each maxchanperscan channels (or for 1,6,11 individually
645          *   if configured accordingly)
646          */
647         while (ptmpchan->channumber) {
648
649                 tlvidx = 0;
650                 pchantlvout->header.len = 0;
651                 scanband = ptmpchan->radiotype;
652                 pstartchan = ptmpchan;
653                 doneearly = 0;
654
655                 /* Construct the channel TLV for the scan command.  Continue to
656                  *  insert channel TLVs until:
657                  *    - the tlvidx hits the maximum configured per scan command
658                  *    - the next channel to insert is 0 (end of desired channel list)
659                  *    - doneearly is set (controlling individual scanning of 1,6,11)
660                  */
661                 while (tlvidx < maxchanperscan && ptmpchan->channumber
662                        && !doneearly && scanned < 2) {
663
664             lbs_deb_scan(
665                     "Scan: Chan(%3d), Radio(%d), mode(%d,%d), Dur(%d)\n",
666                 ptmpchan->channumber, ptmpchan->radiotype,
667                 ptmpchan->chanscanmode.passivescan,
668                 ptmpchan->chanscanmode.disablechanfilt,
669                 ptmpchan->maxscantime);
670
671                         /* Copy the current channel TLV to the command being prepared */
672                         memcpy(pchantlvout->chanscanparam + tlvidx,
673                                ptmpchan, sizeof(pchantlvout->chanscanparam));
674
675                         /* Increment the TLV header length by the size appended */
676                         pchantlvout->header.len +=
677                             sizeof(pchantlvout->chanscanparam);
678
679                         /*
680                          *  The tlv buffer length is set to the number of bytes of the
681                          *    between the channel tlv pointer and the start of the
682                          *    tlv buffer.  This compensates for any TLVs that were appended
683                          *    before the channel list.
684                          */
685                         pscancfgout->tlvbufferlen = ((u8 *) pchantlvout
686                                                      - pscancfgout->tlvbuffer);
687
688                         /*  Add the size of the channel tlv header and the data length */
689                         pscancfgout->tlvbufferlen +=
690                             (sizeof(pchantlvout->header)
691                              + pchantlvout->header.len);
692
693                         /* Increment the index to the channel tlv we are constructing */
694                         tlvidx++;
695
696                         doneearly = 0;
697
698                         /* Stop the loop if the *current* channel is in the 1,6,11 set
699                          *   and we are not filtering on a BSSID or SSID.
700                          */
701                         if (!filteredscan && (ptmpchan->channumber == 1
702                                               || ptmpchan->channumber == 6
703                                               || ptmpchan->channumber == 11)) {
704                                 doneearly = 1;
705                         }
706
707                         /* Increment the tmp pointer to the next channel to be scanned */
708                         ptmpchan++;
709                         scanned++;
710
711                         /* Stop the loop if the *next* channel is in the 1,6,11 set.
712                          *  This will cause it to be the only channel scanned on the next
713                          *  interation
714                          */
715                         if (!filteredscan && (ptmpchan->channumber == 1
716                                               || ptmpchan->channumber == 6
717                                               || ptmpchan->channumber == 11)) {
718                                 doneearly = 1;
719                         }
720                 }
721
722                 /* Send the scan command to the firmware with the specified cfg */
723                 ret = libertas_prepare_and_send_command(priv, cmd_802_11_scan, 0,
724                                             0, 0, pscancfgout);
725                 if (scanned >= 2 && !full_scan) {
726                         priv->adapter->last_scanned_channel = ptmpchan->channumber;
727                         ret = 0;
728                         goto done;
729                 }
730                 scanned = 0;
731         }
732
733         priv->adapter->last_scanned_channel = ptmpchan->channumber;
734
735         memset(&wrqu, 0, sizeof(union iwreq_data));
736         wireless_send_event(priv->wlan_dev.netdev, SIOCGIWSCAN, &wrqu, NULL);
737
738 done:
739         lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
740         return ret;
741 }
742
743 /**
744  *  @brief Internal function used to start a scan based on an input config
745  *
746  *  Use the input user scan configuration information when provided in
747  *    order to send the appropriate scan commands to firmware to populate or
748  *    update the internal driver scan table
749  *
750  *  @param priv          A pointer to wlan_private structure
751  *  @param puserscanin   Pointer to the input configuration for the requested
752  *                       scan.
753  *
754  *  @return              0 or < 0 if error
755  */
756 int wlan_scan_networks(wlan_private * priv,
757                               const struct wlan_ioctl_user_scan_cfg * puserscanin,
758                               int full_scan)
759 {
760         wlan_adapter *adapter = priv->adapter;
761         struct mrvlietypes_chanlistparamset *pchantlvout;
762         struct chanscanparamset * scan_chan_list = NULL;
763         struct wlan_scan_cmd_config * scan_cfg = NULL;
764         u8 keeppreviousscan;
765         u8 filteredscan;
766         u8 scancurrentchanonly;
767         int maxchanperscan;
768         int ret;
769
770         lbs_deb_enter(LBS_DEB_ASSOC);
771
772         scan_chan_list = kzalloc(sizeof(struct chanscanparamset) *
773                                 WLAN_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
774         if (scan_chan_list == NULL) {
775                 ret = -ENOMEM;
776                 goto out;
777         }
778
779         scan_cfg = wlan_scan_setup_scan_config(priv,
780                                                puserscanin,
781                                                &pchantlvout,
782                                                scan_chan_list,
783                                                &maxchanperscan,
784                                                &filteredscan,
785                                                &scancurrentchanonly);
786         if (scan_cfg == NULL) {
787                 ret = -ENOMEM;
788                 goto out;
789         }
790
791         keeppreviousscan = 0;
792
793         if (puserscanin) {
794                 keeppreviousscan = puserscanin->keeppreviousscan;
795         }
796
797         if (adapter->last_scanned_channel)
798                 keeppreviousscan = 1;
799
800         if (!keeppreviousscan) {
801                 memset(adapter->scantable, 0x00,
802                        sizeof(struct bss_descriptor) * MRVDRV_MAX_BSSID_LIST);
803                 adapter->numinscantable = 0;
804         }
805
806         /* Keep the data path active if we are only scanning our current channel */
807         if (!scancurrentchanonly) {
808                 netif_stop_queue(priv->wlan_dev.netdev);
809                 netif_carrier_off(priv->wlan_dev.netdev);
810         }
811
812         ret = wlan_scan_channel_list(priv,
813                                      maxchanperscan,
814                                      filteredscan,
815                                      scan_cfg,
816                                      pchantlvout,
817                                      scan_chan_list,
818                                      puserscanin,
819                                      full_scan);
820
821         /*  Process the resulting scan table:
822          *    - Remove any bad ssids
823          *    - Update our current BSS information from scan data
824          */
825         wlan_scan_process_results(priv);
826
827         if (priv->adapter->connect_status == libertas_connected) {
828                 netif_carrier_on(priv->wlan_dev.netdev);
829                 netif_wake_queue(priv->wlan_dev.netdev);
830         }
831
832 out:
833         if (scan_cfg)
834                 kfree(scan_cfg);
835
836         if (scan_chan_list)
837                 kfree(scan_chan_list);
838
839         lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
840         return ret;
841 }
842
843 /**
844  *  @brief Inspect the scan response buffer for pointers to expected TLVs
845  *
846  *  TLVs can be included at the end of the scan response BSS information.
847  *    Parse the data in the buffer for pointers to TLVs that can potentially
848  *    be passed back in the response
849  *
850  *  @param ptlv        Pointer to the start of the TLV buffer to parse
851  *  @param tlvbufsize  size of the TLV buffer
852  *  @param ptsftlv     Output parameter: Pointer to the TSF TLV if found
853  *
854  *  @return            void
855  */
856 static
857 void wlan_ret_802_11_scan_get_tlv_ptrs(struct mrvlietypes_data * ptlv,
858                                        int tlvbufsize,
859                                        struct mrvlietypes_tsftimestamp ** ptsftlv)
860 {
861         struct mrvlietypes_data *pcurrenttlv;
862         int tlvbufleft;
863         u16 tlvtype;
864         u16 tlvlen;
865
866         pcurrenttlv = ptlv;
867         tlvbufleft = tlvbufsize;
868         *ptsftlv = NULL;
869
870         lbs_deb_scan("SCAN_RESP: tlvbufsize = %d\n", tlvbufsize);
871         lbs_dbg_hex("SCAN_RESP: TLV Buf", (u8 *) ptlv, tlvbufsize);
872
873         while (tlvbufleft >= sizeof(struct mrvlietypesheader)) {
874                 tlvtype = le16_to_cpu(pcurrenttlv->header.type);
875                 tlvlen = le16_to_cpu(pcurrenttlv->header.len);
876
877                 switch (tlvtype) {
878                 case TLV_TYPE_TSFTIMESTAMP:
879                         *ptsftlv = (struct mrvlietypes_tsftimestamp *) pcurrenttlv;
880                         break;
881
882                 default:
883                         lbs_deb_scan("SCAN_RESP: Unhandled TLV = %d\n",
884                                tlvtype);
885                         /* Give up, this seems corrupted */
886                         return;
887                 }               /* switch */
888
889                 tlvbufleft -= (sizeof(ptlv->header) + tlvlen);
890                 pcurrenttlv =
891                     (struct mrvlietypes_data *) (pcurrenttlv->Data + tlvlen);
892         }                       /* while */
893 }
894
895 /**
896  *  @brief Interpret a BSS scan response returned from the firmware
897  *
898  *  Parse the various fixed fields and IEs passed back for a a BSS probe
899  *   response or beacon from the scan command.  Record information as needed
900  *   in the scan table struct bss_descriptor for that entry.
901  *
902  *  @param pBSSIDEntry  Output parameter: Pointer to the BSS Entry
903  *
904  *  @return             0 or -1
905  */
906 static int InterpretBSSDescriptionWithIE(struct bss_descriptor * pBSSEntry,
907                                          u8 ** pbeaconinfo, int *bytesleft)
908 {
909         enum ieeetypes_elementid elemID;
910         struct ieeetypes_fhparamset *pFH;
911         struct ieeetypes_dsparamset *pDS;
912         struct ieeetypes_cfparamset *pCF;
913         struct ieeetypes_ibssparamset *pibss;
914         struct ieeetypes_capinfo *pcap;
915         struct WLAN_802_11_FIXED_IEs fixedie;
916         u8 *pcurrentptr;
917         u8 *pRate;
918         u8 elemlen;
919         u8 bytestocopy;
920         u8 ratesize;
921         u16 beaconsize;
922         u8 founddatarateie;
923         int bytesleftforcurrentbeacon;
924         int ret;
925
926         struct IE_WPA *pIe;
927         const u8 oui01[4] = { 0x00, 0x50, 0xf2, 0x01 };
928
929         struct ieeetypes_countryinfoset *pcountryinfo;
930
931         lbs_deb_enter(LBS_DEB_ASSOC);
932
933         founddatarateie = 0;
934         ratesize = 0;
935         beaconsize = 0;
936
937         if (*bytesleft >= sizeof(beaconsize)) {
938                 /* Extract & convert beacon size from the command buffer */
939                 memcpy(&beaconsize, *pbeaconinfo, sizeof(beaconsize));
940                 beaconsize = le16_to_cpu(beaconsize);
941                 *bytesleft -= sizeof(beaconsize);
942                 *pbeaconinfo += sizeof(beaconsize);
943         }
944
945         if (beaconsize == 0 || beaconsize > *bytesleft) {
946
947                 *pbeaconinfo += *bytesleft;
948                 *bytesleft = 0;
949
950                 return -1;
951         }
952
953         /* Initialize the current working beacon pointer for this BSS iteration */
954         pcurrentptr = *pbeaconinfo;
955
956         /* Advance the return beacon pointer past the current beacon */
957         *pbeaconinfo += beaconsize;
958         *bytesleft -= beaconsize;
959
960         bytesleftforcurrentbeacon = beaconsize;
961
962         memcpy(pBSSEntry->macaddress, pcurrentptr, ETH_ALEN);
963         lbs_deb_scan("InterpretIE: AP MAC Addr-%x:%x:%x:%x:%x:%x\n",
964                pBSSEntry->macaddress[0], pBSSEntry->macaddress[1],
965                pBSSEntry->macaddress[2], pBSSEntry->macaddress[3],
966                pBSSEntry->macaddress[4], pBSSEntry->macaddress[5]);
967
968         pcurrentptr += ETH_ALEN;
969         bytesleftforcurrentbeacon -= ETH_ALEN;
970
971         if (bytesleftforcurrentbeacon < 12) {
972                 lbs_deb_scan("InterpretIE: Not enough bytes left\n");
973                 return -1;
974         }
975
976         /*
977          * next 4 fields are RSSI, time stamp, beacon interval,
978          *   and capability information
979          */
980
981         /* RSSI is 1 byte long */
982         pBSSEntry->rssi = le32_to_cpu((long)(*pcurrentptr));
983         lbs_deb_scan("InterpretIE: RSSI=%02X\n", *pcurrentptr);
984         pcurrentptr += 1;
985         bytesleftforcurrentbeacon -= 1;
986
987         /* time stamp is 8 bytes long */
988         memcpy(fixedie.timestamp, pcurrentptr, 8);
989         memcpy(pBSSEntry->timestamp, pcurrentptr, 8);
990         pcurrentptr += 8;
991         bytesleftforcurrentbeacon -= 8;
992
993         /* beacon interval is 2 bytes long */
994         memcpy(&fixedie.beaconinterval, pcurrentptr, 2);
995         pBSSEntry->beaconperiod = le16_to_cpu(fixedie.beaconinterval);
996         pcurrentptr += 2;
997         bytesleftforcurrentbeacon -= 2;
998
999         /* capability information is 2 bytes long */
1000         memcpy(&fixedie.capabilities, pcurrentptr, 2);
1001         lbs_deb_scan("InterpretIE: fixedie.capabilities=0x%X\n",
1002                fixedie.capabilities);
1003         fixedie.capabilities = le16_to_cpu(fixedie.capabilities);
1004         pcap = (struct ieeetypes_capinfo *) & fixedie.capabilities;
1005         memcpy(&pBSSEntry->cap, pcap, sizeof(struct ieeetypes_capinfo));
1006         pcurrentptr += 2;
1007         bytesleftforcurrentbeacon -= 2;
1008
1009         /* rest of the current buffer are IE's */
1010         lbs_deb_scan("InterpretIE: IElength for this AP = %d\n",
1011                bytesleftforcurrentbeacon);
1012
1013         lbs_dbg_hex("InterpretIE: IE info", (u8 *) pcurrentptr,
1014                 bytesleftforcurrentbeacon);
1015
1016         if (pcap->privacy) {
1017                 lbs_deb_scan("InterpretIE: AP WEP enabled\n");
1018                 pBSSEntry->privacy = wlan802_11privfilter8021xWEP;
1019         } else {
1020                 pBSSEntry->privacy = wlan802_11privfilteracceptall;
1021         }
1022
1023         if (pcap->ibss == 1) {
1024                 pBSSEntry->mode = IW_MODE_ADHOC;
1025         } else {
1026                 pBSSEntry->mode = IW_MODE_INFRA;
1027         }
1028
1029         /* process variable IE */
1030         while (bytesleftforcurrentbeacon >= 2) {
1031                 elemID = (enum ieeetypes_elementid) (*((u8 *) pcurrentptr));
1032                 elemlen = *((u8 *) pcurrentptr + 1);
1033
1034                 if (bytesleftforcurrentbeacon < elemlen) {
1035                         lbs_deb_scan("InterpretIE: error in processing IE, "
1036                                "bytes left < IE length\n");
1037                         bytesleftforcurrentbeacon = 0;
1038                         continue;
1039                 }
1040
1041                 switch (elemID) {
1042
1043                 case SSID:
1044                         pBSSEntry->ssid.ssidlength = elemlen;
1045                         memcpy(pBSSEntry->ssid.ssid, (pcurrentptr + 2),
1046                                elemlen);
1047                         lbs_deb_scan("ssid: %32s", pBSSEntry->ssid.ssid);
1048                         break;
1049
1050                 case SUPPORTED_RATES:
1051                         memcpy(pBSSEntry->datarates, (pcurrentptr + 2),
1052                                elemlen);
1053                         memmove(pBSSEntry->libertas_supported_rates, (pcurrentptr + 2),
1054                                 elemlen);
1055                         ratesize = elemlen;
1056                         founddatarateie = 1;
1057                         break;
1058
1059                 case EXTRA_IE:
1060                         lbs_deb_scan("InterpretIE: EXTRA_IE Found!\n");
1061                         pBSSEntry->extra_ie = 1;
1062                         break;
1063
1064                 case FH_PARAM_SET:
1065                         pFH = (struct ieeetypes_fhparamset *) pcurrentptr;
1066                         memmove(&pBSSEntry->phyparamset.fhparamset, pFH,
1067                                 sizeof(struct ieeetypes_fhparamset));
1068                         pBSSEntry->phyparamset.fhparamset.dwelltime
1069                             =
1070                             le16_to_cpu(pBSSEntry->phyparamset.fhparamset.
1071                                              dwelltime);
1072                         break;
1073
1074                 case DS_PARAM_SET:
1075                         pDS = (struct ieeetypes_dsparamset *) pcurrentptr;
1076
1077                         pBSSEntry->channel = pDS->currentchan;
1078
1079                         memcpy(&pBSSEntry->phyparamset.dsparamset, pDS,
1080                                sizeof(struct ieeetypes_dsparamset));
1081                         break;
1082
1083                 case CF_PARAM_SET:
1084                         pCF = (struct ieeetypes_cfparamset *) pcurrentptr;
1085
1086                         memcpy(&pBSSEntry->ssparamset.cfparamset, pCF,
1087                                sizeof(struct ieeetypes_cfparamset));
1088                         break;
1089
1090                 case IBSS_PARAM_SET:
1091                         pibss = (struct ieeetypes_ibssparamset *) pcurrentptr;
1092                         pBSSEntry->atimwindow =
1093                             le32_to_cpu(pibss->atimwindow);
1094
1095                         memmove(&pBSSEntry->ssparamset.ibssparamset, pibss,
1096                                 sizeof(struct ieeetypes_ibssparamset));
1097
1098                         pBSSEntry->ssparamset.ibssparamset.atimwindow
1099                             =
1100                             le16_to_cpu(pBSSEntry->ssparamset.ibssparamset.
1101                                              atimwindow);
1102                         break;
1103
1104                         /* Handle Country Info IE */
1105                 case COUNTRY_INFO:
1106                         pcountryinfo =
1107                             (struct ieeetypes_countryinfoset *) pcurrentptr;
1108
1109                         if (pcountryinfo->len <
1110                             sizeof(pcountryinfo->countrycode)
1111                             || pcountryinfo->len > 254) {
1112                                 lbs_deb_scan("InterpretIE: 11D- Err "
1113                                        "CountryInfo len =%d min=%zd max=254\n",
1114                                        pcountryinfo->len,
1115                                        sizeof(pcountryinfo->countrycode));
1116                                 ret = -1;
1117                                 goto done;
1118                         }
1119
1120                         memcpy(&pBSSEntry->countryinfo,
1121                                pcountryinfo, pcountryinfo->len + 2);
1122                         lbs_dbg_hex("InterpretIE: 11D- CountryInfo:",
1123                                 (u8 *) pcountryinfo,
1124                                 (u32) (pcountryinfo->len + 2));
1125                         break;
1126
1127                 case EXTENDED_SUPPORTED_RATES:
1128                         /*
1129                          * only process extended supported rate
1130                          * if data rate is already found.
1131                          * data rate IE should come before
1132                          * extended supported rate IE
1133                          */
1134                         if (founddatarateie) {
1135                                 if ((elemlen + ratesize) > WLAN_SUPPORTED_RATES) {
1136                                         bytestocopy =
1137                                             (WLAN_SUPPORTED_RATES - ratesize);
1138                                 } else {
1139                                         bytestocopy = elemlen;
1140                                 }
1141
1142                                 pRate = (u8 *) pBSSEntry->datarates;
1143                                 pRate += ratesize;
1144                                 memmove(pRate, (pcurrentptr + 2), bytestocopy);
1145
1146                                 pRate = (u8 *) pBSSEntry->libertas_supported_rates;
1147
1148                                 pRate += ratesize;
1149                                 memmove(pRate, (pcurrentptr + 2), bytestocopy);
1150                         }
1151                         break;
1152
1153                 case VENDOR_SPECIFIC_221:
1154 #define IE_ID_LEN_FIELDS_BYTES 2
1155                         pIe = (struct IE_WPA *)pcurrentptr;
1156
1157                         if (memcmp(pIe->oui, oui01, sizeof(oui01)))
1158                                 break;
1159
1160                         pBSSEntry->wpa_ie_len = min_t(size_t,
1161                                 elemlen + IE_ID_LEN_FIELDS_BYTES,
1162                                 sizeof(pBSSEntry->wpa_ie));
1163                         memcpy(pBSSEntry->wpa_ie, pcurrentptr,
1164                                 pBSSEntry->wpa_ie_len);
1165                         lbs_dbg_hex("InterpretIE: Resp WPA_IE",
1166                                 pBSSEntry->wpa_ie, elemlen);
1167                         break;
1168                 case WPA2_IE:
1169                         pIe = (struct IE_WPA *)pcurrentptr;
1170
1171                         pBSSEntry->rsn_ie_len = min_t(size_t,
1172                                 elemlen + IE_ID_LEN_FIELDS_BYTES,
1173                                 sizeof(pBSSEntry->rsn_ie));
1174                         memcpy(pBSSEntry->rsn_ie, pcurrentptr,
1175                                 pBSSEntry->rsn_ie_len);
1176                         lbs_dbg_hex("InterpretIE: Resp WPA2_IE",
1177                                 pBSSEntry->rsn_ie, elemlen);
1178                         break;
1179                 case TIM:
1180                         break;
1181
1182                 case CHALLENGE_TEXT:
1183                         break;
1184                 }
1185
1186                 pcurrentptr += elemlen + 2;
1187
1188                 /* need to account for IE ID and IE len */
1189                 bytesleftforcurrentbeacon -= (elemlen + 2);
1190
1191         }                       /* while (bytesleftforcurrentbeacon > 2) */
1192         ret = 0;
1193
1194 done:
1195         lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1196         return ret;
1197 }
1198
1199 /**
1200  *  @brief Compare two SSIDs
1201  *
1202  *  @param ssid1    A pointer to ssid to compare
1203  *  @param ssid2    A pointer to ssid to compare
1204  *
1205  *  @return         0--ssid is same, otherwise is different
1206  */
1207 int libertas_SSID_cmp(struct WLAN_802_11_SSID *ssid1, struct WLAN_802_11_SSID *ssid2)
1208 {
1209         if (!ssid1 || !ssid2)
1210                 return -1;
1211
1212         if (ssid1->ssidlength != ssid2->ssidlength)
1213                 return -1;
1214
1215         return memcmp(ssid1->ssid, ssid2->ssid, ssid1->ssidlength);
1216 }
1217
1218 /**
1219  *  @brief This function finds a specific compatible BSSID in the scan list
1220  *
1221  *  @param adapter  A pointer to wlan_adapter
1222  *  @param bssid    BSSID to find in the scan list
1223  *  @param mode     Network mode: Infrastructure or IBSS
1224  *
1225  *  @return         index in BSSID list, or error return code (< 0)
1226  */
1227 int libertas_find_BSSID_in_list(wlan_adapter * adapter, u8 * bssid, u8 mode)
1228 {
1229         int ret = -ENETUNREACH;
1230         int i;
1231
1232         if (!bssid)
1233                 return -EFAULT;
1234
1235         lbs_deb_scan("FindBSSID: Num of BSSIDs = %d\n",
1236                adapter->numinscantable);
1237
1238         /* Look through the scan table for a compatible match. The ret return
1239          *   variable will be equal to the index in the scan table (greater
1240          *   than zero) if the network is compatible.  The loop will continue
1241          *   past a matched bssid that is not compatible in case there is an
1242          *   AP with multiple SSIDs assigned to the same BSSID
1243          */
1244         for (i = 0; ret < 0 && i < adapter->numinscantable; i++) {
1245                 if (!memcmp(adapter->scantable[i].macaddress, bssid, ETH_ALEN)) {
1246                         switch (mode) {
1247                         case IW_MODE_INFRA:
1248                         case IW_MODE_ADHOC:
1249                                 ret = is_network_compatible(adapter, i, mode);
1250                                 break;
1251                         default:
1252                                 ret = i;
1253                                 break;
1254                         }
1255                 }
1256         }
1257
1258         return ret;
1259 }
1260
1261 /**
1262  *  @brief This function finds ssid in ssid list.
1263  *
1264  *  @param adapter  A pointer to wlan_adapter
1265  *  @param ssid     SSID to find in the list
1266  *  @param bssid    BSSID to qualify the SSID selection (if provided)
1267  *  @param mode     Network mode: Infrastructure or IBSS
1268  *
1269  *  @return         index in BSSID list
1270  */
1271 int libertas_find_SSID_in_list(wlan_adapter * adapter,
1272                    struct WLAN_802_11_SSID *ssid, u8 * bssid, u8 mode)
1273 {
1274         int net = -ENETUNREACH;
1275         u8 bestrssi = 0;
1276         int i;
1277         int j;
1278
1279         lbs_deb_scan("Num of Entries in Table = %d\n", adapter->numinscantable);
1280
1281         for (i = 0; i < adapter->numinscantable; i++) {
1282                 if (!libertas_SSID_cmp(&adapter->scantable[i].ssid, ssid) &&
1283                     (!bssid ||
1284                      !memcmp(adapter->scantable[i].
1285                              macaddress, bssid, ETH_ALEN))) {
1286                         switch (mode) {
1287                         case IW_MODE_INFRA:
1288                         case IW_MODE_ADHOC:
1289                                 j = is_network_compatible(adapter, i, mode);
1290
1291                                 if (j >= 0) {
1292                                         if (bssid) {
1293                                                 return i;
1294                                         }
1295
1296                                         if (SCAN_RSSI
1297                                             (adapter->scantable[i].rssi)
1298                                             > bestrssi) {
1299                                                 bestrssi =
1300                                                     SCAN_RSSI(adapter->
1301                                                               scantable[i].
1302                                                               rssi);
1303                                                 net = i;
1304                                         }
1305                                 } else {
1306                                         if (net == -ENETUNREACH) {
1307                                                 net = j;
1308                                         }
1309                                 }
1310                                 break;
1311                         case IW_MODE_AUTO:
1312                         default:
1313                                 if (SCAN_RSSI(adapter->scantable[i].rssi)
1314                                     > bestrssi) {
1315                                         bestrssi =
1316                                             SCAN_RSSI(adapter->scantable[i].
1317                                                       rssi);
1318                                         net = i;
1319                                 }
1320                                 break;
1321                         }
1322                 }
1323         }
1324
1325         return net;
1326 }
1327
1328 /**
1329  *  @brief This function finds the best SSID in the Scan List
1330  *
1331  *  Search the scan table for the best SSID that also matches the current
1332  *   adapter network preference (infrastructure or adhoc)
1333  *
1334  *  @param adapter  A pointer to wlan_adapter
1335  *
1336  *  @return         index in BSSID list
1337  */
1338 int libertas_find_best_SSID_in_list(wlan_adapter * adapter, u8 mode)
1339 {
1340         int bestnet = -ENETUNREACH;
1341         u8 bestrssi = 0;
1342         int i;
1343
1344         lbs_deb_enter(LBS_DEB_ASSOC);
1345
1346         lbs_deb_scan("Num of BSSIDs = %d\n", adapter->numinscantable);
1347
1348         for (i = 0; i < adapter->numinscantable; i++) {
1349                 switch (mode) {
1350                 case IW_MODE_INFRA:
1351                 case IW_MODE_ADHOC:
1352                         if (is_network_compatible(adapter, i, mode) >= 0) {
1353                                 if (SCAN_RSSI(adapter->scantable[i].rssi) >
1354                                     bestrssi) {
1355                                         bestrssi =
1356                                             SCAN_RSSI(adapter->scantable[i].
1357                                                       rssi);
1358                                         bestnet = i;
1359                                 }
1360                         }
1361                         break;
1362                 case IW_MODE_AUTO:
1363                 default:
1364                         if (SCAN_RSSI(adapter->scantable[i].rssi) > bestrssi) {
1365                                 bestrssi =
1366                                     SCAN_RSSI(adapter->scantable[i].rssi);
1367                                 bestnet = i;
1368                         }
1369                         break;
1370                 }
1371         }
1372
1373         lbs_deb_leave_args(LBS_DEB_SCAN, "bestnet %d", bestnet);
1374         return bestnet;
1375 }
1376
1377 /**
1378  *  @brief Find the AP with specific ssid in the scan list
1379  *
1380  *  @param priv         A pointer to wlan_private structure
1381  *  @param pSSID        A pointer to AP's ssid
1382  *
1383  *  @return             0--success, otherwise--fail
1384  */
1385 int libertas_find_best_network_SSID(wlan_private * priv,
1386                                     struct WLAN_802_11_SSID *pSSID,
1387                                     u8 preferred_mode, u8 *out_mode)
1388 {
1389         wlan_adapter *adapter = priv->adapter;
1390         int ret = 0;
1391         struct bss_descriptor *preqbssid;
1392         int i;
1393
1394         lbs_deb_enter(LBS_DEB_ASSOC);
1395
1396         memset(pSSID, 0, sizeof(struct WLAN_802_11_SSID));
1397
1398         wlan_scan_networks(priv, NULL, 1);
1399         if (adapter->surpriseremoved)
1400                 return -1;
1401         wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
1402
1403         i = libertas_find_best_SSID_in_list(adapter, preferred_mode);
1404         if (i < 0) {
1405                 ret = -1;
1406                 goto out;
1407         }
1408
1409         preqbssid = &adapter->scantable[i];
1410         memcpy(pSSID, &preqbssid->ssid,
1411                sizeof(struct WLAN_802_11_SSID));
1412         *out_mode = preqbssid->mode;
1413
1414         if (!pSSID->ssidlength) {
1415                 ret = -1;
1416         }
1417
1418 out:
1419         lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1420         return ret;
1421 }
1422
1423 /**
1424  *  @brief Scan Network
1425  *
1426  *  @param dev          A pointer to net_device structure
1427  *  @param info         A pointer to iw_request_info structure
1428  *  @param vwrq         A pointer to iw_param structure
1429  *  @param extra        A pointer to extra data buf
1430  *
1431  *  @return             0 --success, otherwise fail
1432  */
1433 int libertas_set_scan(struct net_device *dev, struct iw_request_info *info,
1434                   struct iw_param *vwrq, char *extra)
1435 {
1436         wlan_private *priv = dev->priv;
1437         wlan_adapter *adapter = priv->adapter;
1438
1439         lbs_deb_enter(LBS_DEB_SCAN);
1440
1441         wlan_scan_networks(priv, NULL, 0);
1442
1443         if (adapter->surpriseremoved)
1444                 return -1;
1445
1446         lbs_deb_leave(LBS_DEB_SCAN);
1447         return 0;
1448 }
1449
1450 /**
1451  *  @brief Send a scan command for all available channels filtered on a spec
1452  *
1453  *  @param priv             A pointer to wlan_private structure
1454  *  @param prequestedssid   A pointer to AP's ssid
1455  *  @param keeppreviousscan Flag used to save/clear scan table before scan
1456  *
1457  *  @return                0-success, otherwise fail
1458  */
1459 int libertas_send_specific_SSID_scan(wlan_private * priv,
1460                          struct WLAN_802_11_SSID *prequestedssid,
1461                          u8 keeppreviousscan)
1462 {
1463         wlan_adapter *adapter = priv->adapter;
1464         struct wlan_ioctl_user_scan_cfg scancfg;
1465
1466         lbs_deb_enter(LBS_DEB_ASSOC);
1467
1468         if (prequestedssid == NULL) {
1469                 return -1;
1470         }
1471
1472         memset(&scancfg, 0x00, sizeof(scancfg));
1473
1474         memcpy(scancfg.specificSSID, prequestedssid->ssid,
1475                prequestedssid->ssidlength);
1476         scancfg.keeppreviousscan = keeppreviousscan;
1477
1478         wlan_scan_networks(priv, &scancfg, 1);
1479         if (adapter->surpriseremoved)
1480                 return -1;
1481         wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
1482
1483         lbs_deb_leave(LBS_DEB_ASSOC);
1484         return 0;
1485 }
1486
1487 /**
1488  *  @brief scan an AP with specific BSSID
1489  *
1490  *  @param priv             A pointer to wlan_private structure
1491  *  @param bssid            A pointer to AP's bssid
1492  *  @param keeppreviousscan Flag used to save/clear scan table before scan
1493  *
1494  *  @return          0-success, otherwise fail
1495  */
1496 int libertas_send_specific_BSSID_scan(wlan_private * priv, u8 * bssid, u8 keeppreviousscan)
1497 {
1498         struct wlan_ioctl_user_scan_cfg scancfg;
1499
1500         lbs_deb_enter(LBS_DEB_ASSOC);
1501
1502         if (bssid == NULL) {
1503                 return -1;
1504         }
1505
1506         memset(&scancfg, 0x00, sizeof(scancfg));
1507         memcpy(scancfg.specificBSSID, bssid, sizeof(scancfg.specificBSSID));
1508         scancfg.keeppreviousscan = keeppreviousscan;
1509
1510         wlan_scan_networks(priv, &scancfg, 1);
1511         if (priv->adapter->surpriseremoved)
1512                 return -1;
1513         wait_event_interruptible(priv->adapter->cmd_pending,
1514                 !priv->adapter->nr_cmd_pending);
1515
1516         lbs_deb_leave(LBS_DEB_ASSOC);
1517         return 0;
1518 }
1519
1520 /**
1521  *  @brief  Retrieve the scan table entries via wireless tools IOCTL call
1522  *
1523  *  @param dev          A pointer to net_device structure
1524  *  @param info         A pointer to iw_request_info structure
1525  *  @param dwrq         A pointer to iw_point structure
1526  *  @param extra        A pointer to extra data buf
1527  *
1528  *  @return             0 --success, otherwise fail
1529  */
1530 int libertas_get_scan(struct net_device *dev, struct iw_request_info *info,
1531                   struct iw_point *dwrq, char *extra)
1532 {
1533         wlan_private *priv = dev->priv;
1534         wlan_adapter *adapter = priv->adapter;
1535         int ret = 0;
1536         char *current_ev = extra;
1537         char *end_buf = extra + IW_SCAN_MAX_DATA;
1538         struct chan_freq_power *cfp;
1539         struct bss_descriptor *pscantable;
1540         char *current_val;      /* For rates */
1541         struct iw_event iwe;    /* Temporary buffer */
1542         int i;
1543         int j;
1544         int rate;
1545 #define PERFECT_RSSI ((u8)50)
1546 #define WORST_RSSI   ((u8)0)
1547 #define RSSI_DIFF    ((u8)(PERFECT_RSSI - WORST_RSSI))
1548         u8 rssi;
1549
1550         u8 buf[16 + 256 * 2];
1551         u8 *ptr;
1552
1553         lbs_deb_enter(LBS_DEB_ASSOC);
1554
1555         /*
1556          * if there's either commands in the queue or one being
1557          * processed return -EAGAIN for iwlist to retry later.
1558          */
1559         if (adapter->nr_cmd_pending)
1560                 return -EAGAIN;
1561
1562         if (adapter->last_scanned_channel) {
1563                 wlan_scan_networks(priv, NULL, 0);
1564                 return -EAGAIN;
1565         }
1566
1567         if (adapter->connect_status == libertas_connected)
1568                 lbs_deb_scan("Current ssid: %32s\n",
1569                        adapter->curbssparams.ssid.ssid);
1570
1571         lbs_deb_scan("Scan: Get: numinscantable = %d\n",
1572                adapter->numinscantable);
1573
1574         /* The old API using SIOCGIWAPLIST had a hard limit of IW_MAX_AP.
1575          * The new API using SIOCGIWSCAN is only limited by buffer size
1576          * WE-14 -> WE-16 the buffer is limited to IW_SCAN_MAX_DATA bytes
1577          * which is 4096.
1578          */
1579         for (i = 0; i < adapter->numinscantable; i++) {
1580                 if ((current_ev + MAX_SCAN_CELL_SIZE) >= end_buf) {
1581                         lbs_deb_scan("i=%d break out: current_ev=%p end_buf=%p "
1582                                "MAX_SCAN_CELL_SIZE=%zd\n",
1583                                i, current_ev, end_buf, MAX_SCAN_CELL_SIZE);
1584                         break;
1585                 }
1586
1587                 pscantable = &adapter->scantable[i];
1588
1589                 lbs_deb_scan("i=%d  ssid: %32s\n", i, pscantable->ssid.ssid);
1590
1591                 cfp =
1592                     libertas_find_cfp_by_band_and_channel(adapter, 0,
1593                                                  pscantable->channel);
1594                 if (!cfp) {
1595                         lbs_deb_scan("Invalid channel number %d\n",
1596                                pscantable->channel);
1597                         continue;
1598                 }
1599
1600                 if (!ssid_valid(&adapter->scantable[i].ssid)) {
1601                         continue;
1602                 }
1603
1604                 /* First entry *MUST* be the AP MAC address */
1605                 iwe.cmd = SIOCGIWAP;
1606                 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1607                 memcpy(iwe.u.ap_addr.sa_data,
1608                        &adapter->scantable[i].macaddress, ETH_ALEN);
1609
1610                 iwe.len = IW_EV_ADDR_LEN;
1611                 current_ev =
1612                     iwe_stream_add_event(current_ev, end_buf, &iwe, iwe.len);
1613
1614                 //Add the ESSID
1615                 iwe.u.data.length = adapter->scantable[i].ssid.ssidlength;
1616
1617                 if (iwe.u.data.length > 32) {
1618                         iwe.u.data.length = 32;
1619                 }
1620
1621                 iwe.cmd = SIOCGIWESSID;
1622                 iwe.u.data.flags = 1;
1623                 iwe.len = IW_EV_POINT_LEN + iwe.u.data.length;
1624                 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
1625                                                   adapter->scantable[i].ssid.
1626                                                   ssid);
1627
1628                 //Add mode
1629                 iwe.cmd = SIOCGIWMODE;
1630                 iwe.u.mode = adapter->scantable[i].mode;
1631                 iwe.len = IW_EV_UINT_LEN;
1632                 current_ev =
1633                     iwe_stream_add_event(current_ev, end_buf, &iwe, iwe.len);
1634
1635                 //frequency
1636                 iwe.cmd = SIOCGIWFREQ;
1637                 iwe.u.freq.m = (long)cfp->freq * 100000;
1638                 iwe.u.freq.e = 1;
1639                 iwe.len = IW_EV_FREQ_LEN;
1640                 current_ev =
1641                     iwe_stream_add_event(current_ev, end_buf, &iwe, iwe.len);
1642
1643                 /* Add quality statistics */
1644                 iwe.cmd = IWEVQUAL;
1645                 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
1646                 iwe.u.qual.level = SCAN_RSSI(adapter->scantable[i].rssi);
1647
1648                 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
1649                 iwe.u.qual.qual =
1650                     (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
1651                      (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
1652                     (RSSI_DIFF * RSSI_DIFF);
1653                 if (iwe.u.qual.qual > 100)
1654                         iwe.u.qual.qual = 100;
1655                 else if (iwe.u.qual.qual < 1)
1656                         iwe.u.qual.qual = 0;
1657
1658                 if (adapter->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
1659                         iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
1660                 } else {
1661                         iwe.u.qual.noise =
1662                             CAL_NF(adapter->NF[TYPE_BEACON][TYPE_NOAVG]);
1663                 }
1664                 if ((adapter->mode == IW_MODE_ADHOC) &&
1665                     !libertas_SSID_cmp(&adapter->curbssparams.ssid,
1666                              &adapter->scantable[i].ssid)
1667                     && adapter->adhoccreate) {
1668                         ret = libertas_prepare_and_send_command(priv,
1669                                                     cmd_802_11_rssi,
1670                                                     0,
1671                                                     cmd_option_waitforrsp,
1672                                                     0, NULL);
1673
1674                         if (!ret) {
1675                                 iwe.u.qual.level =
1676                                     CAL_RSSI(adapter->SNR[TYPE_RXPD][TYPE_AVG] /
1677                                              AVG_SCALE,
1678                                              adapter->NF[TYPE_RXPD][TYPE_AVG] /
1679                                              AVG_SCALE);
1680                         }
1681                 }
1682                 iwe.len = IW_EV_QUAL_LEN;
1683                 current_ev =
1684                     iwe_stream_add_event(current_ev, end_buf, &iwe, iwe.len);
1685
1686                 /* Add encryption capability */
1687                 iwe.cmd = SIOCGIWENCODE;
1688                 if (adapter->scantable[i].privacy) {
1689                         iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1690                 } else {
1691                         iwe.u.data.flags = IW_ENCODE_DISABLED;
1692                 }
1693                 iwe.u.data.length = 0;
1694                 iwe.len = IW_EV_POINT_LEN + iwe.u.data.length;
1695                 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
1696                                                   adapter->scantable->ssid.
1697                                                   ssid);
1698
1699                 current_val = current_ev + IW_EV_LCP_LEN;
1700
1701                 iwe.cmd = SIOCGIWRATE;
1702
1703                 iwe.u.bitrate.fixed = 0;
1704                 iwe.u.bitrate.disabled = 0;
1705                 iwe.u.bitrate.value = 0;
1706
1707                 /* Bit rate given in 500 kb/s units (+ 0x80) */
1708                 for (j = 0; j < sizeof(adapter->scantable[i].libertas_supported_rates);
1709                      j++) {
1710                         if (adapter->scantable[i].libertas_supported_rates[j] == 0) {
1711                                 break;
1712                         }
1713                         rate =
1714                             (adapter->scantable[i].libertas_supported_rates[j] & 0x7F) *
1715                             500000;
1716                         if (rate > iwe.u.bitrate.value) {
1717                                 iwe.u.bitrate.value = rate;
1718                         }
1719
1720                         iwe.u.bitrate.value =
1721                             (adapter->scantable[i].libertas_supported_rates[j]
1722                              & 0x7f) * 500000;
1723                         iwe.len = IW_EV_PARAM_LEN;
1724                         current_ev =
1725                             iwe_stream_add_value(current_ev, current_val,
1726                                                  end_buf, &iwe, iwe.len);
1727
1728                 }
1729                 if ((adapter->scantable[i].mode == IW_MODE_ADHOC)
1730                     && !libertas_SSID_cmp(&adapter->curbssparams.ssid,
1731                                 &adapter->scantable[i].ssid)
1732                     && adapter->adhoccreate) {
1733                         iwe.u.bitrate.value = 22 * 500000;
1734                 }
1735                 iwe.len = IW_EV_PARAM_LEN;
1736                 current_ev =
1737                     iwe_stream_add_value(current_ev, current_val, end_buf, &iwe,
1738                                          iwe.len);
1739
1740                 /* Add new value to event */
1741                 current_val = current_ev + IW_EV_LCP_LEN;
1742
1743                 if (adapter->scantable[i].rsn_ie[0] == WPA2_IE) {
1744                         memset(&iwe, 0, sizeof(iwe));
1745                         memset(buf, 0, sizeof(buf));
1746                         memcpy(buf, adapter->scantable[i].rsn_ie,
1747                                         adapter->scantable[i].rsn_ie_len);
1748                         iwe.cmd = IWEVGENIE;
1749                         iwe.u.data.length = adapter->scantable[i].rsn_ie_len;
1750                         iwe.len = IW_EV_POINT_LEN + iwe.u.data.length;
1751                         current_ev = iwe_stream_add_point(current_ev, end_buf,
1752                                         &iwe, buf);
1753                 }
1754                 if (adapter->scantable[i].wpa_ie[0] == WPA_IE) {
1755                         memset(&iwe, 0, sizeof(iwe));
1756                         memset(buf, 0, sizeof(buf));
1757                         memcpy(buf, adapter->scantable[i].wpa_ie,
1758                                         adapter->scantable[i].wpa_ie_len);
1759                         iwe.cmd = IWEVGENIE;
1760                         iwe.u.data.length = adapter->scantable[i].wpa_ie_len;
1761                         iwe.len = IW_EV_POINT_LEN + iwe.u.data.length;
1762                         current_ev = iwe_stream_add_point(current_ev, end_buf,
1763                                         &iwe, buf);
1764                 }
1765
1766
1767                 if (adapter->scantable[i].extra_ie != 0) {
1768                         memset(&iwe, 0, sizeof(iwe));
1769                         memset(buf, 0, sizeof(buf));
1770                         ptr = buf;
1771                         ptr += sprintf(ptr, "extra_ie");
1772                         iwe.u.data.length = strlen(buf);
1773
1774                         lbs_deb_scan("iwe.u.data.length %d\n",
1775                                iwe.u.data.length);
1776                         lbs_deb_scan("BUF: %s \n", buf);
1777
1778                         iwe.cmd = IWEVCUSTOM;
1779                         iwe.len = IW_EV_POINT_LEN + iwe.u.data.length;
1780                         current_ev =
1781                             iwe_stream_add_point(current_ev, end_buf, &iwe,
1782                                                  buf);
1783                 }
1784
1785                 current_val = current_ev + IW_EV_LCP_LEN;
1786
1787                 /*
1788                  * Check if we added any event
1789                  */
1790                 if ((current_val - current_ev) > IW_EV_LCP_LEN)
1791                         current_ev = current_val;
1792         }
1793
1794         dwrq->length = (current_ev - extra);
1795         dwrq->flags = 0;
1796
1797         lbs_deb_leave(LBS_DEB_ASSOC);
1798         return 0;
1799 }
1800
1801 /**
1802  *  @brief Prepare a scan command to be sent to the firmware
1803  *
1804  *  Use the wlan_scan_cmd_config sent to the command processing module in
1805  *   the libertas_prepare_and_send_command to configure a cmd_ds_802_11_scan command
1806  *   struct to send to firmware.
1807  *
1808  *  The fixed fields specifying the BSS type and BSSID filters as well as a
1809  *   variable number/length of TLVs are sent in the command to firmware.
1810  *
1811  *  @param priv       A pointer to wlan_private structure
1812  *  @param cmd        A pointer to cmd_ds_command structure to be sent to
1813  *                    firmware with the cmd_DS_801_11_SCAN structure
1814  *  @param pdata_buf  Void pointer cast of a wlan_scan_cmd_config struct used
1815  *                    to set the fields/TLVs for the command sent to firmware
1816  *
1817  *  @return           0 or -1
1818  *
1819  *  @sa wlan_scan_create_channel_list
1820  */
1821 int libertas_cmd_80211_scan(wlan_private * priv,
1822                          struct cmd_ds_command *cmd, void *pdata_buf)
1823 {
1824         struct cmd_ds_802_11_scan *pscan = &cmd->params.scan;
1825         struct wlan_scan_cmd_config *pscancfg;
1826
1827         lbs_deb_enter(LBS_DEB_ASSOC);
1828
1829         pscancfg = pdata_buf;
1830
1831         /* Set fixed field variables in scan command */
1832         pscan->bsstype = pscancfg->bsstype;
1833         memcpy(pscan->BSSID, pscancfg->specificBSSID, sizeof(pscan->BSSID));
1834         memcpy(pscan->tlvbuffer, pscancfg->tlvbuffer, pscancfg->tlvbufferlen);
1835
1836         cmd->command = cpu_to_le16(cmd_802_11_scan);
1837
1838         /* size is equal to the sizeof(fixed portions) + the TLV len + header */
1839         cmd->size = cpu_to_le16(sizeof(pscan->bsstype)
1840                                      + sizeof(pscan->BSSID)
1841                                      + pscancfg->tlvbufferlen + S_DS_GEN);
1842
1843         lbs_deb_scan("SCAN_CMD: command=%x, size=%x, seqnum=%x\n",
1844                cmd->command, cmd->size, cmd->seqnum);
1845
1846         lbs_deb_leave(LBS_DEB_ASSOC);
1847         return 0;
1848 }
1849
1850 /**
1851  *  @brief This function handles the command response of scan
1852  *
1853  *   The response buffer for the scan command has the following
1854  *      memory layout:
1855  *
1856  *     .-----------------------------------------------------------.
1857  *     |  header (4 * sizeof(u16)):  Standard command response hdr |
1858  *     .-----------------------------------------------------------.
1859  *     |  bufsize (u16) : sizeof the BSS Description data          |
1860  *     .-----------------------------------------------------------.
1861  *     |  NumOfSet (u8) : Number of BSS Descs returned             |
1862  *     .-----------------------------------------------------------.
1863  *     |  BSSDescription data (variable, size given in bufsize)    |
1864  *     .-----------------------------------------------------------.
1865  *     |  TLV data (variable, size calculated using header->size,  |
1866  *     |            bufsize and sizeof the fixed fields above)     |
1867  *     .-----------------------------------------------------------.
1868  *
1869  *  @param priv    A pointer to wlan_private structure
1870  *  @param resp    A pointer to cmd_ds_command
1871  *
1872  *  @return        0 or -1
1873  */
1874 int libertas_ret_80211_scan(wlan_private * priv, struct cmd_ds_command *resp)
1875 {
1876         wlan_adapter *adapter = priv->adapter;
1877         struct cmd_ds_802_11_scan_rsp *pscan;
1878         struct bss_descriptor newbssentry;
1879         struct mrvlietypes_data *ptlv;
1880         struct mrvlietypes_tsftimestamp *ptsftlv;
1881         u8 *pbssinfo;
1882         u16 scanrespsize;
1883         int bytesleft;
1884         int numintable;
1885         int bssIdx;
1886         int idx;
1887         int tlvbufsize;
1888         u64 tsfval;
1889         int ret;
1890
1891         lbs_deb_enter(LBS_DEB_ASSOC);
1892
1893         pscan = &resp->params.scanresp;
1894
1895         if (pscan->nr_sets > MRVDRV_MAX_BSSID_LIST) {
1896         lbs_deb_scan(
1897                        "SCAN_RESP: Invalid number of AP returned (%d)!!\n",
1898                        pscan->nr_sets);
1899                 ret = -1;
1900                 goto done;
1901         }
1902
1903         bytesleft = le16_to_cpu(pscan->bssdescriptsize);
1904         lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
1905
1906         scanrespsize = le16_to_cpu(resp->size);
1907         lbs_deb_scan("SCAN_RESP: returned %d AP before parsing\n",
1908                pscan->nr_sets);
1909
1910         numintable = adapter->numinscantable;
1911         pbssinfo = pscan->bssdesc_and_tlvbuffer;
1912
1913         /* The size of the TLV buffer is equal to the entire command response
1914          *   size (scanrespsize) minus the fixed fields (sizeof()'s), the
1915          *   BSS Descriptions (bssdescriptsize as bytesLef) and the command
1916          *   response header (S_DS_GEN)
1917          */
1918         tlvbufsize = scanrespsize - (bytesleft + sizeof(pscan->bssdescriptsize)
1919                                      + sizeof(pscan->nr_sets)
1920                                      + S_DS_GEN);
1921
1922         ptlv = (struct mrvlietypes_data *) (pscan->bssdesc_and_tlvbuffer + bytesleft);
1923
1924         /* Search the TLV buffer space in the scan response for any valid TLVs */
1925         wlan_ret_802_11_scan_get_tlv_ptrs(ptlv, tlvbufsize, &ptsftlv);
1926
1927         /*
1928          *  Process each scan response returned (pscan->nr_sets).  Save
1929          *    the information in the newbssentry and then insert into the
1930          *    driver scan table either as an update to an existing entry
1931          *    or as an addition at the end of the table
1932          */
1933         for (idx = 0; idx < pscan->nr_sets && bytesleft; idx++) {
1934                 /* Zero out the newbssentry we are about to store info in */
1935                 memset(&newbssentry, 0x00, sizeof(newbssentry));
1936
1937                 /* Process the data fields and IEs returned for this BSS */
1938                 if ((InterpretBSSDescriptionWithIE(&newbssentry,
1939                                                    &pbssinfo,
1940                                                    &bytesleft) ==
1941                      0)
1942                     && CHECK_SSID_IS_VALID(&newbssentry.ssid)) {
1943
1944             lbs_deb_scan(
1945                                "SCAN_RESP: BSSID = %02x:%02x:%02x:%02x:%02x:%02x\n",
1946                                newbssentry.macaddress[0],
1947                                newbssentry.macaddress[1],
1948                                newbssentry.macaddress[2],
1949                                newbssentry.macaddress[3],
1950                                newbssentry.macaddress[4],
1951                                newbssentry.macaddress[5]);
1952
1953                         /*
1954                          * Search the scan table for the same bssid
1955                          */
1956                         for (bssIdx = 0; bssIdx < numintable; bssIdx++) {
1957                                 if (memcmp(newbssentry.macaddress,
1958                                            adapter->scantable[bssIdx].
1959                                            macaddress,
1960                                            sizeof(newbssentry.macaddress)) ==
1961                                     0) {
1962                                         /*
1963                                          * If the SSID matches as well, it is a duplicate of
1964                                          *   this entry.  Keep the bssIdx set to this
1965                                          *   entry so we replace the old contents in the table
1966                                          */
1967                                         if ((newbssentry.ssid.ssidlength ==
1968                                              adapter->scantable[bssIdx].ssid.
1969                                              ssidlength)
1970                                             &&
1971                                             (memcmp
1972                                              (newbssentry.ssid.ssid,
1973                                               adapter->scantable[bssIdx].ssid.
1974                                               ssid,
1975                                               newbssentry.ssid.ssidlength) ==
1976                                              0)) {
1977                         lbs_deb_scan(
1978                                                        "SCAN_RESP: Duplicate of index: %d\n",
1979                                                        bssIdx);
1980                                                 break;
1981                                         }
1982                                 }
1983                         }
1984                         /*
1985                          * If the bssIdx is equal to the number of entries in the table,
1986                          *   the new entry was not a duplicate; append it to the scan
1987                          *   table
1988                          */
1989                         if (bssIdx == numintable) {
1990                                 /* Range check the bssIdx, keep it limited to the last entry */
1991                                 if (bssIdx == MRVDRV_MAX_BSSID_LIST) {
1992                                         bssIdx--;
1993                                 } else {
1994                                         numintable++;
1995                                 }
1996                         }
1997
1998                         /*
1999                          * If the TSF TLV was appended to the scan results, save the
2000                          *   this entries TSF value in the networktsf field.  The
2001                          *   networktsf is the firmware's TSF value at the time the
2002                          *   beacon or probe response was received.
2003                          */
2004                         if (ptsftlv) {
2005                                 memcpy(&tsfval, &ptsftlv->tsftable[idx],
2006                                        sizeof(tsfval));
2007                                 tsfval = le64_to_cpu(tsfval);
2008
2009                                 memcpy(&newbssentry.networktsf,
2010                                        &tsfval, sizeof(newbssentry.networktsf));
2011                         }
2012
2013                         /* Copy the locally created newbssentry to the scan table */
2014                         memcpy(&adapter->scantable[bssIdx],
2015                                &newbssentry,
2016                                sizeof(adapter->scantable[bssIdx]));
2017
2018                 } else {
2019
2020                         /* error parsing/interpreting the scan response, skipped */
2021                         lbs_deb_scan("SCAN_RESP: "
2022                                "InterpretBSSDescriptionWithIE returned ERROR\n");
2023                 }
2024         }
2025
2026         lbs_deb_scan("SCAN_RESP: Scanned %2d APs, %d valid, %d total\n",
2027                pscan->nr_sets, numintable - adapter->numinscantable,
2028                numintable);
2029
2030         /* Update the total number of BSSIDs in the scan table */
2031         adapter->numinscantable = numintable;
2032         ret = 0;
2033
2034 done:
2035         lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
2036         return ret;
2037 }