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